1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
4 * Copyright (c) 2016-2019 Broadcom Limited
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
11 #include <linux/module.h>
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
43 #include <net/checksum.h>
44 #include <net/ip6_checksum.h>
45 #include <net/udp_tunnel.h>
46 #include <linux/workqueue.h>
47 #include <linux/prefetch.h>
48 #include <linux/cache.h>
49 #include <linux/log2.h>
50 #include <linux/aer.h>
51 #include <linux/bitmap.h>
52 #include <linux/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>
62 #include "bnxt_sriov.h"
63 #include "bnxt_ethtool.h"
68 #include "bnxt_devlink.h"
69 #include "bnxt_debugfs.h"
71 #define BNXT_TX_TIMEOUT (5 * HZ)
72 #define BNXT_DEF_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_HW | \
75 MODULE_LICENSE("GPL");
76 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
78 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
79 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
80 #define BNXT_RX_COPY_THRESH 256
82 #define BNXT_TX_PUSH_THRESH 164
129 NETXTREME_E_P5_VF_HV,
132 /* indexed by enum above */
133 static const struct {
136 [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
137 [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
138 [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
139 [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
140 [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
141 [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
142 [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
143 [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
144 [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
145 [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
146 [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
147 [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
148 [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
149 [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
150 [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
151 [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
152 [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
153 [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
154 [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
155 [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
156 [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
157 [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
158 [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
159 [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
160 [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
161 [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
162 [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
163 [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
164 [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
165 [BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
166 [BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
167 [BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
168 [BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
169 [BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
170 [BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
171 [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
172 [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
173 [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
174 [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
175 [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
176 [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
177 [NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
178 [NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
179 [NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
180 [NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
183 static const struct pci_device_id bnxt_pci_tbl[] = {
184 { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
185 { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
186 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
187 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
188 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
189 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
190 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
191 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
192 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
193 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
194 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
195 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
196 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
197 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
198 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
199 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
200 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
201 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
202 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
203 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
204 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
205 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
206 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
207 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
208 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
209 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
210 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
211 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
212 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
213 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
214 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
215 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
216 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
217 { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
218 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
219 { PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
220 { PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
221 { PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
222 { PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
223 { PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
224 { PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
225 { PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
226 { PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
227 { PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
228 { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
229 { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
230 #ifdef CONFIG_BNXT_SRIOV
231 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
232 { PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
233 { PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
234 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
235 { PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
236 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
237 { PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
238 { PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
239 { PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
240 { PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
241 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
242 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
243 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
244 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
245 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
246 { PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
247 { PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
248 { PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
249 { PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
250 { PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
251 { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
256 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
258 static const u16 bnxt_vf_req_snif[] = {
262 HWRM_CFA_L2_FILTER_ALLOC,
265 static const u16 bnxt_async_events_arr[] = {
266 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
267 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
268 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
269 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
270 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
271 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
272 ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
273 ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
274 ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
275 ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
276 ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
279 static struct workqueue_struct *bnxt_pf_wq;
281 static bool bnxt_vf_pciid(enum board_idx idx)
283 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
284 idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
285 idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
286 idx == NETXTREME_E_P5_VF_HV);
289 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
290 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
291 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
293 #define BNXT_CP_DB_IRQ_DIS(db) \
294 writel(DB_CP_IRQ_DIS_FLAGS, db)
296 #define BNXT_DB_CQ(db, idx) \
297 writel(DB_CP_FLAGS | RING_CMP(idx), (db)->doorbell)
299 #define BNXT_DB_NQ_P5(db, idx) \
300 writeq((db)->db_key64 | DBR_TYPE_NQ | RING_CMP(idx), (db)->doorbell)
302 #define BNXT_DB_CQ_ARM(db, idx) \
303 writel(DB_CP_REARM_FLAGS | RING_CMP(idx), (db)->doorbell)
305 #define BNXT_DB_NQ_ARM_P5(db, idx) \
306 writeq((db)->db_key64 | DBR_TYPE_NQ_ARM | RING_CMP(idx), (db)->doorbell)
308 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
310 if (bp->flags & BNXT_FLAG_CHIP_P5)
311 BNXT_DB_NQ_P5(db, idx);
316 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
318 if (bp->flags & BNXT_FLAG_CHIP_P5)
319 BNXT_DB_NQ_ARM_P5(db, idx);
321 BNXT_DB_CQ_ARM(db, idx);
324 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
326 if (bp->flags & BNXT_FLAG_CHIP_P5)
327 writeq(db->db_key64 | DBR_TYPE_CQ_ARMALL | RING_CMP(idx),
333 const u16 bnxt_lhint_arr[] = {
334 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
335 TX_BD_FLAGS_LHINT_512_TO_1023,
336 TX_BD_FLAGS_LHINT_1024_TO_2047,
337 TX_BD_FLAGS_LHINT_1024_TO_2047,
338 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
339 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
340 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
341 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
342 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
343 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
344 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
345 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
346 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
347 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
348 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
349 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
350 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
351 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
352 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
355 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
357 struct metadata_dst *md_dst = skb_metadata_dst(skb);
359 if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
362 return md_dst->u.port_info.port_id;
365 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
368 bnxt_db_write(bp, &txr->tx_db, prod);
369 txr->kick_pending = 0;
372 static bool bnxt_txr_netif_try_stop_queue(struct bnxt *bp,
373 struct bnxt_tx_ring_info *txr,
374 struct netdev_queue *txq)
376 netif_tx_stop_queue(txq);
378 /* netif_tx_stop_queue() must be done before checking
379 * tx index in bnxt_tx_avail() below, because in
380 * bnxt_tx_int(), we update tx index before checking for
381 * netif_tx_queue_stopped().
384 if (bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh) {
385 netif_tx_wake_queue(txq);
392 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
394 struct bnxt *bp = netdev_priv(dev);
396 struct tx_bd_ext *txbd1;
397 struct netdev_queue *txq;
400 unsigned int length, pad = 0;
401 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
403 struct pci_dev *pdev = bp->pdev;
404 struct bnxt_tx_ring_info *txr;
405 struct bnxt_sw_tx_bd *tx_buf;
407 i = skb_get_queue_mapping(skb);
408 if (unlikely(i >= bp->tx_nr_rings)) {
409 dev_kfree_skb_any(skb);
410 atomic_long_inc(&dev->tx_dropped);
414 txq = netdev_get_tx_queue(dev, i);
415 txr = &bp->tx_ring[bp->tx_ring_map[i]];
418 free_size = bnxt_tx_avail(bp, txr);
419 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
420 /* We must have raced with NAPI cleanup */
421 if (net_ratelimit() && txr->kick_pending)
422 netif_warn(bp, tx_err, dev,
423 "bnxt: ring busy w/ flush pending!\n");
424 if (bnxt_txr_netif_try_stop_queue(bp, txr, txq))
425 return NETDEV_TX_BUSY;
429 len = skb_headlen(skb);
430 last_frag = skb_shinfo(skb)->nr_frags;
432 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
434 txbd->tx_bd_opaque = prod;
436 tx_buf = &txr->tx_buf_ring[prod];
438 tx_buf->nr_frags = last_frag;
441 cfa_action = bnxt_xmit_get_cfa_action(skb);
442 if (skb_vlan_tag_present(skb)) {
443 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
444 skb_vlan_tag_get(skb);
445 /* Currently supports 8021Q, 8021AD vlan offloads
446 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
448 if (skb->vlan_proto == htons(ETH_P_8021Q))
449 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
452 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
453 struct tx_push_buffer *tx_push_buf = txr->tx_push;
454 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
455 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
456 void __iomem *db = txr->tx_db.doorbell;
457 void *pdata = tx_push_buf->data;
461 /* Set COAL_NOW to be ready quickly for the next push */
462 tx_push->tx_bd_len_flags_type =
463 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
464 TX_BD_TYPE_LONG_TX_BD |
465 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
466 TX_BD_FLAGS_COAL_NOW |
467 TX_BD_FLAGS_PACKET_END |
468 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
470 if (skb->ip_summed == CHECKSUM_PARTIAL)
471 tx_push1->tx_bd_hsize_lflags =
472 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
474 tx_push1->tx_bd_hsize_lflags = 0;
476 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
477 tx_push1->tx_bd_cfa_action =
478 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
480 end = pdata + length;
481 end = PTR_ALIGN(end, 8) - 1;
484 skb_copy_from_linear_data(skb, pdata, len);
486 for (j = 0; j < last_frag; j++) {
487 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
490 fptr = skb_frag_address_safe(frag);
494 memcpy(pdata, fptr, skb_frag_size(frag));
495 pdata += skb_frag_size(frag);
498 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
499 txbd->tx_bd_haddr = txr->data_mapping;
500 prod = NEXT_TX(prod);
501 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
502 memcpy(txbd, tx_push1, sizeof(*txbd));
503 prod = NEXT_TX(prod);
505 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
509 netdev_tx_sent_queue(txq, skb->len);
510 wmb(); /* Sync is_push and byte queue before pushing data */
512 push_len = (length + sizeof(*tx_push) + 7) / 8;
514 __iowrite64_copy(db, tx_push_buf, 16);
515 __iowrite32_copy(db + 4, tx_push_buf + 1,
516 (push_len - 16) << 1);
518 __iowrite64_copy(db, tx_push_buf, push_len);
525 if (length < BNXT_MIN_PKT_SIZE) {
526 pad = BNXT_MIN_PKT_SIZE - length;
527 if (skb_pad(skb, pad))
528 /* SKB already freed. */
529 goto tx_kick_pending;
530 length = BNXT_MIN_PKT_SIZE;
533 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
535 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
538 dma_unmap_addr_set(tx_buf, mapping, mapping);
539 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
540 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
542 txbd->tx_bd_haddr = cpu_to_le64(mapping);
544 prod = NEXT_TX(prod);
545 txbd1 = (struct tx_bd_ext *)
546 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
548 txbd1->tx_bd_hsize_lflags = 0;
549 if (skb_is_gso(skb)) {
552 if (skb->encapsulation)
553 hdr_len = skb_inner_network_offset(skb) +
554 skb_inner_network_header_len(skb) +
555 inner_tcp_hdrlen(skb);
557 hdr_len = skb_transport_offset(skb) +
560 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
562 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
563 length = skb_shinfo(skb)->gso_size;
564 txbd1->tx_bd_mss = cpu_to_le32(length);
566 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
567 txbd1->tx_bd_hsize_lflags =
568 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
569 txbd1->tx_bd_mss = 0;
573 if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
574 dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
579 flags |= bnxt_lhint_arr[length];
580 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
582 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
583 txbd1->tx_bd_cfa_action =
584 cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
585 for (i = 0; i < last_frag; i++) {
586 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
588 prod = NEXT_TX(prod);
589 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
591 len = skb_frag_size(frag);
592 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
595 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
598 tx_buf = &txr->tx_buf_ring[prod];
599 dma_unmap_addr_set(tx_buf, mapping, mapping);
601 txbd->tx_bd_haddr = cpu_to_le64(mapping);
603 flags = len << TX_BD_LEN_SHIFT;
604 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
608 txbd->tx_bd_len_flags_type =
609 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
610 TX_BD_FLAGS_PACKET_END);
612 netdev_tx_sent_queue(txq, skb->len);
614 /* Sync BD data before updating doorbell */
617 prod = NEXT_TX(prod);
620 if (!netdev_xmit_more() || netif_xmit_stopped(txq))
621 bnxt_txr_db_kick(bp, txr, prod);
623 txr->kick_pending = 1;
627 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
628 if (netdev_xmit_more() && !tx_buf->is_push)
629 bnxt_txr_db_kick(bp, txr, prod);
631 bnxt_txr_netif_try_stop_queue(bp, txr, txq);
638 /* start back at beginning and unmap skb */
640 tx_buf = &txr->tx_buf_ring[prod];
641 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
642 skb_headlen(skb), PCI_DMA_TODEVICE);
643 prod = NEXT_TX(prod);
645 /* unmap remaining mapped pages */
646 for (i = 0; i < last_frag; i++) {
647 prod = NEXT_TX(prod);
648 tx_buf = &txr->tx_buf_ring[prod];
649 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
650 skb_frag_size(&skb_shinfo(skb)->frags[i]),
655 dev_kfree_skb_any(skb);
657 if (txr->kick_pending)
658 bnxt_txr_db_kick(bp, txr, txr->tx_prod);
659 txr->tx_buf_ring[txr->tx_prod].skb = NULL;
660 atomic_long_inc(&dev->tx_dropped);
664 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
666 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
667 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
668 u16 cons = txr->tx_cons;
669 struct pci_dev *pdev = bp->pdev;
671 unsigned int tx_bytes = 0;
673 for (i = 0; i < nr_pkts; i++) {
674 struct bnxt_sw_tx_bd *tx_buf;
678 tx_buf = &txr->tx_buf_ring[cons];
679 cons = NEXT_TX(cons);
683 if (tx_buf->is_push) {
688 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
689 skb_headlen(skb), PCI_DMA_TODEVICE);
690 last = tx_buf->nr_frags;
692 for (j = 0; j < last; j++) {
693 cons = NEXT_TX(cons);
694 tx_buf = &txr->tx_buf_ring[cons];
697 dma_unmap_addr(tx_buf, mapping),
698 skb_frag_size(&skb_shinfo(skb)->frags[j]),
703 cons = NEXT_TX(cons);
705 tx_bytes += skb->len;
706 dev_kfree_skb_any(skb);
709 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
712 /* Need to make the tx_cons update visible to bnxt_start_xmit()
713 * before checking for netif_tx_queue_stopped(). Without the
714 * memory barrier, there is a small possibility that bnxt_start_xmit()
715 * will miss it and cause the queue to be stopped forever.
719 if (unlikely(netif_tx_queue_stopped(txq)) &&
720 bnxt_tx_avail(bp, txr) >= bp->tx_wake_thresh &&
721 READ_ONCE(txr->dev_state) != BNXT_DEV_STATE_CLOSING)
722 netif_tx_wake_queue(txq);
725 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
726 struct bnxt_rx_ring_info *rxr,
729 struct device *dev = &bp->pdev->dev;
732 page = page_pool_dev_alloc_pages(rxr->page_pool);
736 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
737 DMA_ATTR_WEAK_ORDERING);
738 if (dma_mapping_error(dev, *mapping)) {
739 page_pool_recycle_direct(rxr->page_pool, page);
742 *mapping += bp->rx_dma_offset;
746 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
750 struct pci_dev *pdev = bp->pdev;
752 data = kmalloc(bp->rx_buf_size, gfp);
756 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
757 bp->rx_buf_use_size, bp->rx_dir,
758 DMA_ATTR_WEAK_ORDERING);
760 if (dma_mapping_error(&pdev->dev, *mapping)) {
767 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
770 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
771 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
774 if (BNXT_RX_PAGE_MODE(bp)) {
776 __bnxt_alloc_rx_page(bp, &mapping, rxr, gfp);
782 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
784 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
790 rx_buf->data_ptr = data + bp->rx_offset;
792 rx_buf->mapping = mapping;
794 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
798 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
800 u16 prod = rxr->rx_prod;
801 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
802 struct rx_bd *cons_bd, *prod_bd;
804 prod_rx_buf = &rxr->rx_buf_ring[prod];
805 cons_rx_buf = &rxr->rx_buf_ring[cons];
807 prod_rx_buf->data = data;
808 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
810 prod_rx_buf->mapping = cons_rx_buf->mapping;
812 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
813 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
815 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
818 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
820 u16 next, max = rxr->rx_agg_bmap_size;
822 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
824 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
828 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
829 struct bnxt_rx_ring_info *rxr,
833 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
834 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
835 struct pci_dev *pdev = bp->pdev;
838 u16 sw_prod = rxr->rx_sw_agg_prod;
839 unsigned int offset = 0;
841 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
844 page = alloc_page(gfp);
848 rxr->rx_page_offset = 0;
850 offset = rxr->rx_page_offset;
851 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
852 if (rxr->rx_page_offset == PAGE_SIZE)
857 page = alloc_page(gfp);
862 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
863 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
864 DMA_ATTR_WEAK_ORDERING);
865 if (dma_mapping_error(&pdev->dev, mapping)) {
870 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
871 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
873 __set_bit(sw_prod, rxr->rx_agg_bmap);
874 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
875 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
877 rx_agg_buf->page = page;
878 rx_agg_buf->offset = offset;
879 rx_agg_buf->mapping = mapping;
880 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
881 rxbd->rx_bd_opaque = sw_prod;
885 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
886 struct bnxt_cp_ring_info *cpr,
887 u16 cp_cons, u16 curr)
889 struct rx_agg_cmp *agg;
891 cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
892 agg = (struct rx_agg_cmp *)
893 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
897 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
898 struct bnxt_rx_ring_info *rxr,
899 u16 agg_id, u16 curr)
901 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
903 return &tpa_info->agg_arr[curr];
906 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
907 u16 start, u32 agg_bufs, bool tpa)
909 struct bnxt_napi *bnapi = cpr->bnapi;
910 struct bnxt *bp = bnapi->bp;
911 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
912 u16 prod = rxr->rx_agg_prod;
913 u16 sw_prod = rxr->rx_sw_agg_prod;
917 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
920 for (i = 0; i < agg_bufs; i++) {
922 struct rx_agg_cmp *agg;
923 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
924 struct rx_bd *prod_bd;
928 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
930 agg = bnxt_get_agg(bp, cpr, idx, start + i);
931 cons = agg->rx_agg_cmp_opaque;
932 __clear_bit(cons, rxr->rx_agg_bmap);
934 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
935 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
937 __set_bit(sw_prod, rxr->rx_agg_bmap);
938 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
939 cons_rx_buf = &rxr->rx_agg_ring[cons];
941 /* It is possible for sw_prod to be equal to cons, so
942 * set cons_rx_buf->page to NULL first.
944 page = cons_rx_buf->page;
945 cons_rx_buf->page = NULL;
946 prod_rx_buf->page = page;
947 prod_rx_buf->offset = cons_rx_buf->offset;
949 prod_rx_buf->mapping = cons_rx_buf->mapping;
951 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
953 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
954 prod_bd->rx_bd_opaque = sw_prod;
956 prod = NEXT_RX_AGG(prod);
957 sw_prod = NEXT_RX_AGG(sw_prod);
959 rxr->rx_agg_prod = prod;
960 rxr->rx_sw_agg_prod = sw_prod;
963 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
964 struct bnxt_rx_ring_info *rxr,
965 u16 cons, void *data, u8 *data_ptr,
967 unsigned int offset_and_len)
969 unsigned int payload = offset_and_len >> 16;
970 unsigned int len = offset_and_len & 0xffff;
972 struct page *page = data;
973 u16 prod = rxr->rx_prod;
977 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
979 bnxt_reuse_rx_data(rxr, cons, data);
982 dma_addr -= bp->rx_dma_offset;
983 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
984 DMA_ATTR_WEAK_ORDERING);
985 page_pool_release_page(rxr->page_pool, page);
987 if (unlikely(!payload))
988 payload = eth_get_headlen(bp->dev, data_ptr, len);
990 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
996 off = (void *)data_ptr - page_address(page);
997 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
998 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
999 payload + NET_IP_ALIGN);
1001 frag = &skb_shinfo(skb)->frags[0];
1002 skb_frag_size_sub(frag, payload);
1003 skb_frag_off_add(frag, payload);
1004 skb->data_len -= payload;
1005 skb->tail += payload;
1010 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1011 struct bnxt_rx_ring_info *rxr, u16 cons,
1012 void *data, u8 *data_ptr,
1013 dma_addr_t dma_addr,
1014 unsigned int offset_and_len)
1016 u16 prod = rxr->rx_prod;
1017 struct sk_buff *skb;
1020 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1021 if (unlikely(err)) {
1022 bnxt_reuse_rx_data(rxr, cons, data);
1026 skb = build_skb(data, 0);
1027 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1028 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
1034 skb_reserve(skb, bp->rx_offset);
1035 skb_put(skb, offset_and_len & 0xffff);
1039 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp,
1040 struct bnxt_cp_ring_info *cpr,
1041 struct sk_buff *skb, u16 idx,
1042 u32 agg_bufs, bool tpa)
1044 struct bnxt_napi *bnapi = cpr->bnapi;
1045 struct pci_dev *pdev = bp->pdev;
1046 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1047 u16 prod = rxr->rx_agg_prod;
1048 bool p5_tpa = false;
1051 if ((bp->flags & BNXT_FLAG_CHIP_P5) && tpa)
1054 for (i = 0; i < agg_bufs; i++) {
1056 struct rx_agg_cmp *agg;
1057 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1062 agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1064 agg = bnxt_get_agg(bp, cpr, idx, i);
1065 cons = agg->rx_agg_cmp_opaque;
1066 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1067 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1069 cons_rx_buf = &rxr->rx_agg_ring[cons];
1070 skb_fill_page_desc(skb, i, cons_rx_buf->page,
1071 cons_rx_buf->offset, frag_len);
1072 __clear_bit(cons, rxr->rx_agg_bmap);
1074 /* It is possible for bnxt_alloc_rx_page() to allocate
1075 * a sw_prod index that equals the cons index, so we
1076 * need to clear the cons entry now.
1078 mapping = cons_rx_buf->mapping;
1079 page = cons_rx_buf->page;
1080 cons_rx_buf->page = NULL;
1082 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1083 struct skb_shared_info *shinfo;
1084 unsigned int nr_frags;
1086 shinfo = skb_shinfo(skb);
1087 nr_frags = --shinfo->nr_frags;
1088 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
1092 cons_rx_buf->page = page;
1094 /* Update prod since possibly some pages have been
1095 * allocated already.
1097 rxr->rx_agg_prod = prod;
1098 bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1102 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1104 DMA_ATTR_WEAK_ORDERING);
1106 skb->data_len += frag_len;
1107 skb->len += frag_len;
1108 skb->truesize += PAGE_SIZE;
1110 prod = NEXT_RX_AGG(prod);
1112 rxr->rx_agg_prod = prod;
1116 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1117 u8 agg_bufs, u32 *raw_cons)
1120 struct rx_agg_cmp *agg;
1122 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1123 last = RING_CMP(*raw_cons);
1124 agg = (struct rx_agg_cmp *)
1125 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1126 return RX_AGG_CMP_VALID(agg, *raw_cons);
1129 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1133 struct bnxt *bp = bnapi->bp;
1134 struct pci_dev *pdev = bp->pdev;
1135 struct sk_buff *skb;
1137 skb = napi_alloc_skb(&bnapi->napi, len);
1141 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1144 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1145 len + NET_IP_ALIGN);
1147 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1154 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1155 u32 *raw_cons, void *cmp)
1157 struct rx_cmp *rxcmp = cmp;
1158 u32 tmp_raw_cons = *raw_cons;
1159 u8 cmp_type, agg_bufs = 0;
1161 cmp_type = RX_CMP_TYPE(rxcmp);
1163 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1164 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1166 RX_CMP_AGG_BUFS_SHIFT;
1167 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1168 struct rx_tpa_end_cmp *tpa_end = cmp;
1170 if (bp->flags & BNXT_FLAG_CHIP_P5)
1173 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1177 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1180 *raw_cons = tmp_raw_cons;
1184 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
1186 if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
1190 queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
1192 schedule_delayed_work(&bp->fw_reset_task, delay);
1195 static void bnxt_queue_sp_work(struct bnxt *bp)
1198 queue_work(bnxt_pf_wq, &bp->sp_task);
1200 schedule_work(&bp->sp_task);
1203 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1205 if (!rxr->bnapi->in_reset) {
1206 rxr->bnapi->in_reset = true;
1207 if (bp->flags & BNXT_FLAG_CHIP_P5)
1208 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1210 set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
1211 bnxt_queue_sp_work(bp);
1213 rxr->rx_next_cons = 0xffff;
1216 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1218 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1219 u16 idx = agg_id & MAX_TPA_P5_MASK;
1221 if (test_bit(idx, map->agg_idx_bmap))
1222 idx = find_first_zero_bit(map->agg_idx_bmap,
1223 BNXT_AGG_IDX_BMAP_SIZE);
1224 __set_bit(idx, map->agg_idx_bmap);
1225 map->agg_id_tbl[agg_id] = idx;
1229 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1231 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1233 __clear_bit(idx, map->agg_idx_bmap);
1236 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1238 struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1240 return map->agg_id_tbl[agg_id];
1243 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1244 struct rx_tpa_start_cmp *tpa_start,
1245 struct rx_tpa_start_cmp_ext *tpa_start1)
1247 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1248 struct bnxt_tpa_info *tpa_info;
1249 u16 cons, prod, agg_id;
1250 struct rx_bd *prod_bd;
1253 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1254 agg_id = TPA_START_AGG_ID_P5(tpa_start);
1255 agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1257 agg_id = TPA_START_AGG_ID(tpa_start);
1259 cons = tpa_start->rx_tpa_start_cmp_opaque;
1260 prod = rxr->rx_prod;
1261 cons_rx_buf = &rxr->rx_buf_ring[cons];
1262 prod_rx_buf = &rxr->rx_buf_ring[prod];
1263 tpa_info = &rxr->rx_tpa[agg_id];
1265 if (unlikely(cons != rxr->rx_next_cons ||
1266 TPA_START_ERROR(tpa_start))) {
1267 netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1268 cons, rxr->rx_next_cons,
1269 TPA_START_ERROR_CODE(tpa_start1));
1270 bnxt_sched_reset(bp, rxr);
1273 /* Store cfa_code in tpa_info to use in tpa_end
1274 * completion processing.
1276 tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1277 prod_rx_buf->data = tpa_info->data;
1278 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1280 mapping = tpa_info->mapping;
1281 prod_rx_buf->mapping = mapping;
1283 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1285 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1287 tpa_info->data = cons_rx_buf->data;
1288 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1289 cons_rx_buf->data = NULL;
1290 tpa_info->mapping = cons_rx_buf->mapping;
1293 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1294 RX_TPA_START_CMP_LEN_SHIFT;
1295 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1296 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1298 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1299 tpa_info->gso_type = SKB_GSO_TCPV4;
1300 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1301 if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
1302 tpa_info->gso_type = SKB_GSO_TCPV6;
1303 tpa_info->rss_hash =
1304 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1306 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1307 tpa_info->gso_type = 0;
1308 netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1310 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1311 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1312 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1313 tpa_info->agg_count = 0;
1315 rxr->rx_prod = NEXT_RX(prod);
1316 cons = NEXT_RX(cons);
1317 rxr->rx_next_cons = NEXT_RX(cons);
1318 cons_rx_buf = &rxr->rx_buf_ring[cons];
1320 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1321 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1322 cons_rx_buf->data = NULL;
1325 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1328 bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1332 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1334 struct udphdr *uh = NULL;
1336 if (ip_proto == htons(ETH_P_IP)) {
1337 struct iphdr *iph = (struct iphdr *)skb->data;
1339 if (iph->protocol == IPPROTO_UDP)
1340 uh = (struct udphdr *)(iph + 1);
1342 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1344 if (iph->nexthdr == IPPROTO_UDP)
1345 uh = (struct udphdr *)(iph + 1);
1349 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1351 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1356 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1357 int payload_off, int tcp_ts,
1358 struct sk_buff *skb)
1363 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1364 u32 hdr_info = tpa_info->hdr_info;
1365 bool loopback = false;
1367 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1368 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1369 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1371 /* If the packet is an internal loopback packet, the offsets will
1372 * have an extra 4 bytes.
1374 if (inner_mac_off == 4) {
1376 } else if (inner_mac_off > 4) {
1377 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1380 /* We only support inner iPv4/ipv6. If we don't see the
1381 * correct protocol ID, it must be a loopback packet where
1382 * the offsets are off by 4.
1384 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1388 /* internal loopback packet, subtract all offsets by 4 */
1394 nw_off = inner_ip_off - ETH_HLEN;
1395 skb_set_network_header(skb, nw_off);
1396 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1397 struct ipv6hdr *iph = ipv6_hdr(skb);
1399 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1400 len = skb->len - skb_transport_offset(skb);
1402 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1404 struct iphdr *iph = ip_hdr(skb);
1406 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1407 len = skb->len - skb_transport_offset(skb);
1409 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1412 if (inner_mac_off) { /* tunnel */
1413 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1416 bnxt_gro_tunnel(skb, proto);
1422 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1423 int payload_off, int tcp_ts,
1424 struct sk_buff *skb)
1427 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1428 u32 hdr_info = tpa_info->hdr_info;
1429 int iphdr_len, nw_off;
1431 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1432 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1433 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1435 nw_off = inner_ip_off - ETH_HLEN;
1436 skb_set_network_header(skb, nw_off);
1437 iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1438 sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1439 skb_set_transport_header(skb, nw_off + iphdr_len);
1441 if (inner_mac_off) { /* tunnel */
1442 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1445 bnxt_gro_tunnel(skb, proto);
1451 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1452 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1454 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1455 int payload_off, int tcp_ts,
1456 struct sk_buff *skb)
1460 int len, nw_off, tcp_opt_len = 0;
1465 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1468 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1470 skb_set_network_header(skb, nw_off);
1472 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1473 len = skb->len - skb_transport_offset(skb);
1475 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1476 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1477 struct ipv6hdr *iph;
1479 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1481 skb_set_network_header(skb, nw_off);
1482 iph = ipv6_hdr(skb);
1483 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1484 len = skb->len - skb_transport_offset(skb);
1486 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1488 dev_kfree_skb_any(skb);
1492 if (nw_off) /* tunnel */
1493 bnxt_gro_tunnel(skb, skb->protocol);
1498 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1499 struct bnxt_tpa_info *tpa_info,
1500 struct rx_tpa_end_cmp *tpa_end,
1501 struct rx_tpa_end_cmp_ext *tpa_end1,
1502 struct sk_buff *skb)
1508 segs = TPA_END_TPA_SEGS(tpa_end);
1512 NAPI_GRO_CB(skb)->count = segs;
1513 skb_shinfo(skb)->gso_size =
1514 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1515 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1516 if (bp->flags & BNXT_FLAG_CHIP_P5)
1517 payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1519 payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1520 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1522 tcp_gro_complete(skb);
1527 /* Given the cfa_code of a received packet determine which
1528 * netdev (vf-rep or PF) the packet is destined to.
1530 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1532 struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1534 /* if vf-rep dev is NULL, the must belongs to the PF */
1535 return dev ? dev : bp->dev;
1538 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1539 struct bnxt_cp_ring_info *cpr,
1541 struct rx_tpa_end_cmp *tpa_end,
1542 struct rx_tpa_end_cmp_ext *tpa_end1,
1545 struct bnxt_napi *bnapi = cpr->bnapi;
1546 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1547 u8 *data_ptr, agg_bufs;
1549 struct bnxt_tpa_info *tpa_info;
1551 struct sk_buff *skb;
1552 u16 idx = 0, agg_id;
1556 if (unlikely(bnapi->in_reset)) {
1557 int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1560 return ERR_PTR(-EBUSY);
1564 if (bp->flags & BNXT_FLAG_CHIP_P5) {
1565 agg_id = TPA_END_AGG_ID_P5(tpa_end);
1566 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1567 agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1568 tpa_info = &rxr->rx_tpa[agg_id];
1569 if (unlikely(agg_bufs != tpa_info->agg_count)) {
1570 netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1571 agg_bufs, tpa_info->agg_count);
1572 agg_bufs = tpa_info->agg_count;
1574 tpa_info->agg_count = 0;
1575 *event |= BNXT_AGG_EVENT;
1576 bnxt_free_agg_idx(rxr, agg_id);
1578 gro = !!(bp->flags & BNXT_FLAG_GRO);
1580 agg_id = TPA_END_AGG_ID(tpa_end);
1581 agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1582 tpa_info = &rxr->rx_tpa[agg_id];
1583 idx = RING_CMP(*raw_cons);
1585 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1586 return ERR_PTR(-EBUSY);
1588 *event |= BNXT_AGG_EVENT;
1589 idx = NEXT_CMP(idx);
1591 gro = !!TPA_END_GRO(tpa_end);
1593 data = tpa_info->data;
1594 data_ptr = tpa_info->data_ptr;
1596 len = tpa_info->len;
1597 mapping = tpa_info->mapping;
1599 if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1600 bnxt_abort_tpa(cpr, idx, agg_bufs);
1601 if (agg_bufs > MAX_SKB_FRAGS)
1602 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1603 agg_bufs, (int)MAX_SKB_FRAGS);
1607 if (len <= bp->rx_copy_thresh) {
1608 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1610 bnxt_abort_tpa(cpr, idx, agg_bufs);
1615 dma_addr_t new_mapping;
1617 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1619 bnxt_abort_tpa(cpr, idx, agg_bufs);
1623 tpa_info->data = new_data;
1624 tpa_info->data_ptr = new_data + bp->rx_offset;
1625 tpa_info->mapping = new_mapping;
1627 skb = build_skb(data, 0);
1628 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1629 bp->rx_buf_use_size, bp->rx_dir,
1630 DMA_ATTR_WEAK_ORDERING);
1634 bnxt_abort_tpa(cpr, idx, agg_bufs);
1637 skb_reserve(skb, bp->rx_offset);
1642 skb = bnxt_rx_pages(bp, cpr, skb, idx, agg_bufs, true);
1644 /* Page reuse already handled by bnxt_rx_pages(). */
1650 eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
1652 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1653 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1655 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1656 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1657 __be16 vlan_proto = htons(tpa_info->metadata >>
1658 RX_CMP_FLAGS2_METADATA_TPID_SFT);
1659 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1661 if (eth_type_vlan(vlan_proto)) {
1662 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1669 skb_checksum_none_assert(skb);
1670 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1671 skb->ip_summed = CHECKSUM_UNNECESSARY;
1673 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1677 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1682 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1683 struct rx_agg_cmp *rx_agg)
1685 u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1686 struct bnxt_tpa_info *tpa_info;
1688 agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1689 tpa_info = &rxr->rx_tpa[agg_id];
1690 BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1691 tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1694 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1695 struct sk_buff *skb)
1697 if (skb->dev != bp->dev) {
1698 /* this packet belongs to a vf-rep */
1699 bnxt_vf_rep_rx(bp, skb);
1702 skb_record_rx_queue(skb, bnapi->index);
1703 napi_gro_receive(&bnapi->napi, skb);
1706 /* returns the following:
1707 * 1 - 1 packet successfully received
1708 * 0 - successful TPA_START, packet not completed yet
1709 * -EBUSY - completion ring does not have all the agg buffers yet
1710 * -ENOMEM - packet aborted due to out of memory
1711 * -EIO - packet aborted due to hw error indicated in BD
1713 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1714 u32 *raw_cons, u8 *event)
1716 struct bnxt_napi *bnapi = cpr->bnapi;
1717 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1718 struct net_device *dev = bp->dev;
1719 struct rx_cmp *rxcmp;
1720 struct rx_cmp_ext *rxcmp1;
1721 u32 tmp_raw_cons = *raw_cons;
1722 u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1723 struct bnxt_sw_rx_bd *rx_buf;
1725 u8 *data_ptr, agg_bufs, cmp_type;
1726 dma_addr_t dma_addr;
1727 struct sk_buff *skb;
1732 rxcmp = (struct rx_cmp *)
1733 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1735 cmp_type = RX_CMP_TYPE(rxcmp);
1737 if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
1738 bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
1739 goto next_rx_no_prod_no_len;
1742 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1743 cp_cons = RING_CMP(tmp_raw_cons);
1744 rxcmp1 = (struct rx_cmp_ext *)
1745 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1747 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1750 /* The valid test of the entry must be done first before
1751 * reading any further.
1754 prod = rxr->rx_prod;
1756 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1757 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1758 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1760 *event |= BNXT_RX_EVENT;
1761 goto next_rx_no_prod_no_len;
1763 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1764 skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
1765 (struct rx_tpa_end_cmp *)rxcmp,
1766 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1773 bnxt_deliver_skb(bp, bnapi, skb);
1776 *event |= BNXT_RX_EVENT;
1777 goto next_rx_no_prod_no_len;
1780 cons = rxcmp->rx_cmp_opaque;
1781 if (unlikely(cons != rxr->rx_next_cons)) {
1782 int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
1784 /* 0xffff is forced error, don't print it */
1785 if (rxr->rx_next_cons != 0xffff)
1786 netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
1787 cons, rxr->rx_next_cons);
1788 bnxt_sched_reset(bp, rxr);
1791 goto next_rx_no_prod_no_len;
1793 rx_buf = &rxr->rx_buf_ring[cons];
1794 data = rx_buf->data;
1795 data_ptr = rx_buf->data_ptr;
1798 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1799 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1802 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1805 cp_cons = NEXT_CMP(cp_cons);
1806 *event |= BNXT_AGG_EVENT;
1808 *event |= BNXT_RX_EVENT;
1810 rx_buf->data = NULL;
1811 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1812 u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
1814 bnxt_reuse_rx_data(rxr, cons, data);
1816 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
1820 if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
1821 bnapi->cp_ring.sw_stats.rx.rx_buf_errors++;
1822 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
1823 !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
1824 netdev_warn_once(bp->dev, "RX buffer error %x\n",
1826 bnxt_sched_reset(bp, rxr);
1829 goto next_rx_no_len;
1832 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1833 dma_addr = rx_buf->mapping;
1835 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1840 if (len <= bp->rx_copy_thresh) {
1841 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1842 bnxt_reuse_rx_data(rxr, cons, data);
1845 bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
1853 if (rx_buf->data_ptr == data_ptr)
1854 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1857 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1866 skb = bnxt_rx_pages(bp, cpr, skb, cp_cons, agg_bufs, false);
1873 if (RX_CMP_HASH_VALID(rxcmp)) {
1874 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1875 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1877 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1878 if (hash_type != 1 && hash_type != 3)
1879 type = PKT_HASH_TYPE_L3;
1880 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1883 cfa_code = RX_CMP_CFA_CODE(rxcmp1);
1884 skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
1886 if ((rxcmp1->rx_cmp_flags2 &
1887 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1888 (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1889 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1890 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1891 __be16 vlan_proto = htons(meta_data >>
1892 RX_CMP_FLAGS2_METADATA_TPID_SFT);
1894 if (eth_type_vlan(vlan_proto)) {
1895 __vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1902 skb_checksum_none_assert(skb);
1903 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1904 if (dev->features & NETIF_F_RXCSUM) {
1905 skb->ip_summed = CHECKSUM_UNNECESSARY;
1906 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1909 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1910 if (dev->features & NETIF_F_RXCSUM)
1911 bnapi->cp_ring.sw_stats.rx.rx_l4_csum_errors++;
1915 bnxt_deliver_skb(bp, bnapi, skb);
1919 cpr->rx_packets += 1;
1920 cpr->rx_bytes += len;
1923 rxr->rx_prod = NEXT_RX(prod);
1924 rxr->rx_next_cons = NEXT_RX(cons);
1926 next_rx_no_prod_no_len:
1927 *raw_cons = tmp_raw_cons;
1932 /* In netpoll mode, if we are using a combined completion ring, we need to
1933 * discard the rx packets and recycle the buffers.
1935 static int bnxt_force_rx_discard(struct bnxt *bp,
1936 struct bnxt_cp_ring_info *cpr,
1937 u32 *raw_cons, u8 *event)
1939 u32 tmp_raw_cons = *raw_cons;
1940 struct rx_cmp_ext *rxcmp1;
1941 struct rx_cmp *rxcmp;
1945 cp_cons = RING_CMP(tmp_raw_cons);
1946 rxcmp = (struct rx_cmp *)
1947 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1949 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1950 cp_cons = RING_CMP(tmp_raw_cons);
1951 rxcmp1 = (struct rx_cmp_ext *)
1952 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1954 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1957 /* The valid test of the entry must be done first before
1958 * reading any further.
1961 cmp_type = RX_CMP_TYPE(rxcmp);
1962 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1963 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1964 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1965 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1966 struct rx_tpa_end_cmp_ext *tpa_end1;
1968 tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
1969 tpa_end1->rx_tpa_end_cmp_errors_v2 |=
1970 cpu_to_le32(RX_TPA_END_CMP_ERRORS);
1972 return bnxt_rx_pkt(bp, cpr, raw_cons, event);
1975 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
1977 struct bnxt_fw_health *fw_health = bp->fw_health;
1978 u32 reg = fw_health->regs[reg_idx];
1979 u32 reg_type, reg_off, val = 0;
1981 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
1982 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
1984 case BNXT_FW_HEALTH_REG_TYPE_CFG:
1985 pci_read_config_dword(bp->pdev, reg_off, &val);
1987 case BNXT_FW_HEALTH_REG_TYPE_GRC:
1988 reg_off = fw_health->mapped_regs[reg_idx];
1990 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
1991 val = readl(bp->bar0 + reg_off);
1993 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
1994 val = readl(bp->bar1 + reg_off);
1997 if (reg_idx == BNXT_FW_RESET_INPROG_REG)
1998 val &= fw_health->fw_reset_inprog_reg_mask;
2002 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2006 for (i = 0; i < bp->rx_nr_rings; i++) {
2007 u16 grp_idx = bp->rx_ring[i].bnapi->index;
2008 struct bnxt_ring_grp_info *grp_info;
2010 grp_info = &bp->grp_info[grp_idx];
2011 if (grp_info->agg_fw_ring_id == ring_id)
2014 return INVALID_HW_RING_ID;
2017 #define BNXT_GET_EVENT_PORT(data) \
2019 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2021 #define BNXT_EVENT_RING_TYPE(data2) \
2023 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2025 #define BNXT_EVENT_RING_TYPE_RX(data2) \
2026 (BNXT_EVENT_RING_TYPE(data2) == \
2027 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2029 static int bnxt_async_event_process(struct bnxt *bp,
2030 struct hwrm_async_event_cmpl *cmpl)
2032 u16 event_id = le16_to_cpu(cmpl->event_id);
2033 u32 data1 = le32_to_cpu(cmpl->event_data1);
2034 u32 data2 = le32_to_cpu(cmpl->event_data2);
2036 /* TODO CHIMP_FW: Define event id's for link change, error etc */
2038 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2039 struct bnxt_link_info *link_info = &bp->link_info;
2042 goto async_event_process_exit;
2044 /* print unsupported speed warning in forced speed mode only */
2045 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2046 (data1 & 0x20000)) {
2047 u16 fw_speed = link_info->force_link_speed;
2048 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2050 if (speed != SPEED_UNKNOWN)
2051 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2054 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2057 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2058 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2059 set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2061 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2062 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2064 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2065 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2067 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2068 u16 port_id = BNXT_GET_EVENT_PORT(data1);
2073 if (bp->pf.port_id != port_id)
2076 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2079 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2081 goto async_event_process_exit;
2082 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2084 case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2085 char *fatal_str = "non-fatal";
2088 goto async_event_process_exit;
2090 bp->fw_reset_timestamp = jiffies;
2091 bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2092 if (!bp->fw_reset_min_dsecs)
2093 bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2094 bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2095 if (!bp->fw_reset_max_dsecs)
2096 bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2097 if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2098 fatal_str = "fatal";
2099 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2101 netif_warn(bp, hw, bp->dev,
2102 "Firmware %s reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2103 fatal_str, data1, data2,
2104 bp->fw_reset_min_dsecs * 100,
2105 bp->fw_reset_max_dsecs * 100);
2106 set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2109 case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2110 struct bnxt_fw_health *fw_health = bp->fw_health;
2113 goto async_event_process_exit;
2115 if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2116 fw_health->enabled = false;
2117 netif_info(bp, drv, bp->dev,
2118 "Error recovery info: error recovery[0]\n");
2121 fw_health->master = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2122 fw_health->tmr_multiplier =
2123 DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2124 bp->current_interval * 10);
2125 fw_health->tmr_counter = fw_health->tmr_multiplier;
2126 if (!fw_health->enabled)
2127 fw_health->last_fw_heartbeat =
2128 bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2129 fw_health->last_fw_reset_cnt =
2130 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2131 netif_info(bp, drv, bp->dev,
2132 "Error recovery info: error recovery[1], master[%d], reset count[%u], health status: 0x%x\n",
2133 fw_health->master, fw_health->last_fw_reset_cnt,
2134 bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG));
2135 if (!fw_health->enabled) {
2136 /* Make sure tmr_counter is set and visible to
2137 * bnxt_health_check() before setting enabled to true.
2140 fw_health->enabled = true;
2142 goto async_event_process_exit;
2144 case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2145 netif_notice(bp, hw, bp->dev,
2146 "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2148 goto async_event_process_exit;
2149 case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2150 struct bnxt_rx_ring_info *rxr;
2153 if (bp->flags & BNXT_FLAG_CHIP_P5)
2154 goto async_event_process_exit;
2156 netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2157 BNXT_EVENT_RING_TYPE(data2), data1);
2158 if (!BNXT_EVENT_RING_TYPE_RX(data2))
2159 goto async_event_process_exit;
2161 grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2162 if (grp_idx == INVALID_HW_RING_ID) {
2163 netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2165 goto async_event_process_exit;
2167 rxr = bp->bnapi[grp_idx]->rx_ring;
2168 bnxt_sched_reset(bp, rxr);
2169 goto async_event_process_exit;
2172 goto async_event_process_exit;
2174 bnxt_queue_sp_work(bp);
2175 async_event_process_exit:
2176 bnxt_ulp_async_events(bp, cmpl);
2180 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2182 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2183 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2184 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2185 (struct hwrm_fwd_req_cmpl *)txcmp;
2187 switch (cmpl_type) {
2188 case CMPL_BASE_TYPE_HWRM_DONE:
2189 seq_id = le16_to_cpu(h_cmpl->sequence_id);
2190 if (seq_id == bp->hwrm_intr_seq_id)
2191 bp->hwrm_intr_seq_id = (u16)~bp->hwrm_intr_seq_id;
2193 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
2196 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2197 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2199 if ((vf_id < bp->pf.first_vf_id) ||
2200 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2201 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2206 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2207 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
2208 bnxt_queue_sp_work(bp);
2211 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2212 bnxt_async_event_process(bp,
2213 (struct hwrm_async_event_cmpl *)txcmp);
2222 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2224 struct bnxt_napi *bnapi = dev_instance;
2225 struct bnxt *bp = bnapi->bp;
2226 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2227 u32 cons = RING_CMP(cpr->cp_raw_cons);
2230 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2231 napi_schedule(&bnapi->napi);
2235 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2237 u32 raw_cons = cpr->cp_raw_cons;
2238 u16 cons = RING_CMP(raw_cons);
2239 struct tx_cmp *txcmp;
2241 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2243 return TX_CMP_VALID(txcmp, raw_cons);
2246 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
2248 struct bnxt_napi *bnapi = dev_instance;
2249 struct bnxt *bp = bnapi->bp;
2250 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2251 u32 cons = RING_CMP(cpr->cp_raw_cons);
2254 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2256 if (!bnxt_has_work(bp, cpr)) {
2257 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
2258 /* return if erroneous interrupt */
2259 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
2263 /* disable ring IRQ */
2264 BNXT_CP_DB_IRQ_DIS(cpr->cp_db.doorbell);
2266 /* Return here if interrupt is shared and is disabled. */
2267 if (unlikely(atomic_read(&bp->intr_sem) != 0))
2270 napi_schedule(&bnapi->napi);
2274 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2277 struct bnxt_napi *bnapi = cpr->bnapi;
2278 u32 raw_cons = cpr->cp_raw_cons;
2283 struct tx_cmp *txcmp;
2285 cpr->has_more_work = 0;
2286 cpr->had_work_done = 1;
2290 cons = RING_CMP(raw_cons);
2291 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2293 if (!TX_CMP_VALID(txcmp, raw_cons))
2296 /* The valid test of the entry must be done first before
2297 * reading any further.
2300 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
2302 /* return full budget so NAPI will complete. */
2303 if (unlikely(tx_pkts >= bp->tx_wake_thresh)) {
2305 raw_cons = NEXT_RAW_CMP(raw_cons);
2307 cpr->has_more_work = 1;
2310 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2312 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2314 rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2316 if (likely(rc >= 0))
2318 /* Increment rx_pkts when rc is -ENOMEM to count towards
2319 * the NAPI budget. Otherwise, we may potentially loop
2320 * here forever if we consistently cannot allocate
2323 else if (rc == -ENOMEM && budget)
2325 else if (rc == -EBUSY) /* partial completion */
2327 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
2328 CMPL_BASE_TYPE_HWRM_DONE) ||
2329 (TX_CMP_TYPE(txcmp) ==
2330 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
2331 (TX_CMP_TYPE(txcmp) ==
2332 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
2333 bnxt_hwrm_handler(bp, txcmp);
2335 raw_cons = NEXT_RAW_CMP(raw_cons);
2337 if (rx_pkts && rx_pkts == budget) {
2338 cpr->has_more_work = 1;
2343 if (event & BNXT_REDIRECT_EVENT)
2346 if (event & BNXT_TX_EVENT) {
2347 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
2348 u16 prod = txr->tx_prod;
2350 /* Sync BD data before updating doorbell */
2353 bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
2356 cpr->cp_raw_cons = raw_cons;
2357 bnapi->tx_pkts += tx_pkts;
2358 bnapi->events |= event;
2362 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi)
2364 if (bnapi->tx_pkts) {
2365 bnapi->tx_int(bp, bnapi, bnapi->tx_pkts);
2369 if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
2370 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2372 if (bnapi->events & BNXT_AGG_EVENT)
2373 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2374 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2379 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2382 struct bnxt_napi *bnapi = cpr->bnapi;
2385 rx_pkts = __bnxt_poll_work(bp, cpr, budget);
2387 /* ACK completion ring before freeing tx ring and producing new
2388 * buffers in rx/agg rings to prevent overflowing the completion
2391 bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
2393 __bnxt_poll_work_done(bp, bnapi);
2397 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
2399 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2400 struct bnxt *bp = bnapi->bp;
2401 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2402 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2403 struct tx_cmp *txcmp;
2404 struct rx_cmp_ext *rxcmp1;
2405 u32 cp_cons, tmp_raw_cons;
2406 u32 raw_cons = cpr->cp_raw_cons;
2407 bool flush_xdp = false;
2414 cp_cons = RING_CMP(raw_cons);
2415 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2417 if (!TX_CMP_VALID(txcmp, raw_cons))
2420 /* The valid test of the entry must be done first before
2421 * reading any further.
2424 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
2425 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
2426 cp_cons = RING_CMP(tmp_raw_cons);
2427 rxcmp1 = (struct rx_cmp_ext *)
2428 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2430 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2433 /* force an error to recycle the buffer */
2434 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2435 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2437 rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2438 if (likely(rc == -EIO) && budget)
2440 else if (rc == -EBUSY) /* partial completion */
2442 if (event & BNXT_REDIRECT_EVENT)
2444 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
2445 CMPL_BASE_TYPE_HWRM_DONE)) {
2446 bnxt_hwrm_handler(bp, txcmp);
2449 "Invalid completion received on special ring\n");
2451 raw_cons = NEXT_RAW_CMP(raw_cons);
2453 if (rx_pkts == budget)
2457 cpr->cp_raw_cons = raw_cons;
2458 BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
2459 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
2461 if (event & BNXT_AGG_EVENT)
2462 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
2466 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
2467 napi_complete_done(napi, rx_pkts);
2468 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2473 static int bnxt_poll(struct napi_struct *napi, int budget)
2475 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2476 struct bnxt *bp = bnapi->bp;
2477 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2481 work_done += bnxt_poll_work(bp, cpr, budget - work_done);
2483 if (work_done >= budget) {
2485 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2489 if (!bnxt_has_work(bp, cpr)) {
2490 if (napi_complete_done(napi, work_done))
2491 BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
2495 if (bp->flags & BNXT_FLAG_DIM) {
2496 struct dim_sample dim_sample = {};
2498 dim_update_sample(cpr->event_ctr,
2502 net_dim(&cpr->dim, dim_sample);
2507 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
2509 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2510 int i, work_done = 0;
2512 for (i = 0; i < 2; i++) {
2513 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2516 work_done += __bnxt_poll_work(bp, cpr2,
2517 budget - work_done);
2518 cpr->has_more_work |= cpr2->has_more_work;
2524 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
2527 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2530 for (i = 0; i < 2; i++) {
2531 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[i];
2532 struct bnxt_db_info *db;
2534 if (cpr2 && cpr2->had_work_done) {
2536 writeq(db->db_key64 | dbr_type |
2537 RING_CMP(cpr2->cp_raw_cons), db->doorbell);
2538 cpr2->had_work_done = 0;
2541 __bnxt_poll_work_done(bp, bnapi);
2544 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
2546 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
2547 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2548 u32 raw_cons = cpr->cp_raw_cons;
2549 struct bnxt *bp = bnapi->bp;
2550 struct nqe_cn *nqcmp;
2554 if (cpr->has_more_work) {
2555 cpr->has_more_work = 0;
2556 work_done = __bnxt_poll_cqs(bp, bnapi, budget);
2559 cons = RING_CMP(raw_cons);
2560 nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2562 if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
2563 if (cpr->has_more_work)
2566 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL);
2567 cpr->cp_raw_cons = raw_cons;
2568 if (napi_complete_done(napi, work_done))
2569 BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
2574 /* The valid test of the entry must be done first before
2575 * reading any further.
2579 if (nqcmp->type == cpu_to_le16(NQ_CN_TYPE_CQ_NOTIFICATION)) {
2580 u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
2581 struct bnxt_cp_ring_info *cpr2;
2583 /* No more budget for RX work */
2584 if (budget && work_done >= budget && idx == BNXT_RX_HDL)
2587 cpr2 = cpr->cp_ring_arr[idx];
2588 work_done += __bnxt_poll_work(bp, cpr2,
2589 budget - work_done);
2590 cpr->has_more_work |= cpr2->has_more_work;
2592 bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
2594 raw_cons = NEXT_RAW_CMP(raw_cons);
2596 __bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ);
2597 if (raw_cons != cpr->cp_raw_cons) {
2598 cpr->cp_raw_cons = raw_cons;
2599 BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
2604 static void bnxt_free_tx_skbs(struct bnxt *bp)
2607 struct pci_dev *pdev = bp->pdev;
2612 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
2613 for (i = 0; i < bp->tx_nr_rings; i++) {
2614 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2617 if (!txr->tx_buf_ring)
2620 for (j = 0; j < max_idx;) {
2621 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
2622 struct sk_buff *skb;
2625 if (i < bp->tx_nr_rings_xdp &&
2626 tx_buf->action == XDP_REDIRECT) {
2627 dma_unmap_single(&pdev->dev,
2628 dma_unmap_addr(tx_buf, mapping),
2629 dma_unmap_len(tx_buf, len),
2631 xdp_return_frame(tx_buf->xdpf);
2633 tx_buf->xdpf = NULL;
2646 if (tx_buf->is_push) {
2652 dma_unmap_single(&pdev->dev,
2653 dma_unmap_addr(tx_buf, mapping),
2657 last = tx_buf->nr_frags;
2659 for (k = 0; k < last; k++, j++) {
2660 int ring_idx = j & bp->tx_ring_mask;
2661 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2663 tx_buf = &txr->tx_buf_ring[ring_idx];
2666 dma_unmap_addr(tx_buf, mapping),
2667 skb_frag_size(frag), PCI_DMA_TODEVICE);
2671 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
2675 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr)
2677 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
2678 struct pci_dev *pdev = bp->pdev;
2679 struct bnxt_tpa_idx_map *map;
2680 int i, max_idx, max_agg_idx;
2682 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
2683 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
2685 goto skip_rx_tpa_free;
2687 for (i = 0; i < bp->max_tpa; i++) {
2688 struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
2689 u8 *data = tpa_info->data;
2694 dma_unmap_single_attrs(&pdev->dev, tpa_info->mapping,
2695 bp->rx_buf_use_size, bp->rx_dir,
2696 DMA_ATTR_WEAK_ORDERING);
2698 tpa_info->data = NULL;
2704 if (!rxr->rx_buf_ring)
2705 goto skip_rx_buf_free;
2707 for (i = 0; i < max_idx; i++) {
2708 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
2709 dma_addr_t mapping = rx_buf->mapping;
2710 void *data = rx_buf->data;
2715 rx_buf->data = NULL;
2716 if (BNXT_RX_PAGE_MODE(bp)) {
2717 mapping -= bp->rx_dma_offset;
2718 dma_unmap_page_attrs(&pdev->dev, mapping, PAGE_SIZE,
2720 DMA_ATTR_WEAK_ORDERING);
2721 page_pool_recycle_direct(rxr->page_pool, data);
2723 dma_unmap_single_attrs(&pdev->dev, mapping,
2724 bp->rx_buf_use_size, bp->rx_dir,
2725 DMA_ATTR_WEAK_ORDERING);
2731 if (!rxr->rx_agg_ring)
2732 goto skip_rx_agg_free;
2734 for (i = 0; i < max_agg_idx; i++) {
2735 struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
2736 struct page *page = rx_agg_buf->page;
2741 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2742 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
2743 DMA_ATTR_WEAK_ORDERING);
2745 rx_agg_buf->page = NULL;
2746 __clear_bit(i, rxr->rx_agg_bmap);
2753 __free_page(rxr->rx_page);
2754 rxr->rx_page = NULL;
2756 map = rxr->rx_tpa_idx_map;
2758 memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
2761 static void bnxt_free_rx_skbs(struct bnxt *bp)
2768 for (i = 0; i < bp->rx_nr_rings; i++)
2769 bnxt_free_one_rx_ring_skbs(bp, i);
2772 static void bnxt_free_skbs(struct bnxt *bp)
2774 bnxt_free_tx_skbs(bp);
2775 bnxt_free_rx_skbs(bp);
2778 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2780 struct pci_dev *pdev = bp->pdev;
2783 for (i = 0; i < rmem->nr_pages; i++) {
2784 if (!rmem->pg_arr[i])
2787 dma_free_coherent(&pdev->dev, rmem->page_size,
2788 rmem->pg_arr[i], rmem->dma_arr[i]);
2790 rmem->pg_arr[i] = NULL;
2793 size_t pg_tbl_size = rmem->nr_pages * 8;
2795 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2796 pg_tbl_size = rmem->page_size;
2797 dma_free_coherent(&pdev->dev, pg_tbl_size,
2798 rmem->pg_tbl, rmem->pg_tbl_map);
2799 rmem->pg_tbl = NULL;
2801 if (rmem->vmem_size && *rmem->vmem) {
2807 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
2809 struct pci_dev *pdev = bp->pdev;
2813 if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
2814 valid_bit = PTU_PTE_VALID;
2815 if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
2816 size_t pg_tbl_size = rmem->nr_pages * 8;
2818 if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
2819 pg_tbl_size = rmem->page_size;
2820 rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
2827 for (i = 0; i < rmem->nr_pages; i++) {
2828 u64 extra_bits = valid_bit;
2830 rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2834 if (!rmem->pg_arr[i])
2838 memset(rmem->pg_arr[i], rmem->init_val,
2840 if (rmem->nr_pages > 1 || rmem->depth > 0) {
2841 if (i == rmem->nr_pages - 2 &&
2842 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2843 extra_bits |= PTU_PTE_NEXT_TO_LAST;
2844 else if (i == rmem->nr_pages - 1 &&
2845 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
2846 extra_bits |= PTU_PTE_LAST;
2848 cpu_to_le64(rmem->dma_arr[i] | extra_bits);
2852 if (rmem->vmem_size) {
2853 *rmem->vmem = vzalloc(rmem->vmem_size);
2860 static void bnxt_free_tpa_info(struct bnxt *bp)
2864 for (i = 0; i < bp->rx_nr_rings; i++) {
2865 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2867 kfree(rxr->rx_tpa_idx_map);
2868 rxr->rx_tpa_idx_map = NULL;
2870 for (j = 0; j < bp->max_tpa; j++) {
2871 kfree(rxr->rx_tpa[j].agg_arr);
2872 rxr->rx_tpa[j].agg_arr = NULL;
2880 static int bnxt_alloc_tpa_info(struct bnxt *bp)
2884 bp->max_tpa = MAX_TPA;
2885 if (bp->flags & BNXT_FLAG_CHIP_P5) {
2886 if (!bp->max_tpa_v2)
2888 bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
2891 for (i = 0; i < bp->rx_nr_rings; i++) {
2892 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2893 struct rx_agg_cmp *agg;
2895 rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
2900 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
2902 for (j = 0; j < bp->max_tpa; j++) {
2903 agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
2906 rxr->rx_tpa[j].agg_arr = agg;
2908 rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
2910 if (!rxr->rx_tpa_idx_map)
2916 static void bnxt_free_rx_rings(struct bnxt *bp)
2923 bnxt_free_tpa_info(bp);
2924 for (i = 0; i < bp->rx_nr_rings; i++) {
2925 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2926 struct bnxt_ring_struct *ring;
2929 bpf_prog_put(rxr->xdp_prog);
2931 if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
2932 xdp_rxq_info_unreg(&rxr->xdp_rxq);
2934 page_pool_destroy(rxr->page_pool);
2935 rxr->page_pool = NULL;
2937 kfree(rxr->rx_agg_bmap);
2938 rxr->rx_agg_bmap = NULL;
2940 ring = &rxr->rx_ring_struct;
2941 bnxt_free_ring(bp, &ring->ring_mem);
2943 ring = &rxr->rx_agg_ring_struct;
2944 bnxt_free_ring(bp, &ring->ring_mem);
2948 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
2949 struct bnxt_rx_ring_info *rxr)
2951 struct page_pool_params pp = { 0 };
2953 pp.pool_size = bp->rx_ring_size;
2954 pp.nid = dev_to_node(&bp->pdev->dev);
2955 pp.dev = &bp->pdev->dev;
2956 pp.dma_dir = DMA_BIDIRECTIONAL;
2958 rxr->page_pool = page_pool_create(&pp);
2959 if (IS_ERR(rxr->page_pool)) {
2960 int err = PTR_ERR(rxr->page_pool);
2962 rxr->page_pool = NULL;
2968 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2970 int i, rc = 0, agg_rings = 0;
2975 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2978 for (i = 0; i < bp->rx_nr_rings; i++) {
2979 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2980 struct bnxt_ring_struct *ring;
2982 ring = &rxr->rx_ring_struct;
2984 rc = bnxt_alloc_rx_page_pool(bp, rxr);
2988 rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
2992 rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
2996 xdp_rxq_info_unreg(&rxr->xdp_rxq);
3000 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3008 ring = &rxr->rx_agg_ring_struct;
3009 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3014 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3015 mem_size = rxr->rx_agg_bmap_size / 8;
3016 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3017 if (!rxr->rx_agg_bmap)
3021 if (bp->flags & BNXT_FLAG_TPA)
3022 rc = bnxt_alloc_tpa_info(bp);
3026 static void bnxt_free_tx_rings(struct bnxt *bp)
3029 struct pci_dev *pdev = bp->pdev;
3034 for (i = 0; i < bp->tx_nr_rings; i++) {
3035 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3036 struct bnxt_ring_struct *ring;
3039 dma_free_coherent(&pdev->dev, bp->tx_push_size,
3040 txr->tx_push, txr->tx_push_mapping);
3041 txr->tx_push = NULL;
3044 ring = &txr->tx_ring_struct;
3046 bnxt_free_ring(bp, &ring->ring_mem);
3050 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3053 struct pci_dev *pdev = bp->pdev;
3055 bp->tx_push_size = 0;
3056 if (bp->tx_push_thresh) {
3059 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3060 bp->tx_push_thresh);
3062 if (push_size > 256) {
3064 bp->tx_push_thresh = 0;
3067 bp->tx_push_size = push_size;
3070 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3071 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3072 struct bnxt_ring_struct *ring;
3075 ring = &txr->tx_ring_struct;
3077 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3081 ring->grp_idx = txr->bnapi->index;
3082 if (bp->tx_push_size) {
3085 /* One pre-allocated DMA buffer to backup
3088 txr->tx_push = dma_alloc_coherent(&pdev->dev,
3090 &txr->tx_push_mapping,
3096 mapping = txr->tx_push_mapping +
3097 sizeof(struct tx_push_bd);
3098 txr->data_mapping = cpu_to_le64(mapping);
3100 qidx = bp->tc_to_qidx[j];
3101 ring->queue_id = bp->q_info[qidx].queue_id;
3102 if (i < bp->tx_nr_rings_xdp)
3104 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
3110 static void bnxt_free_cp_rings(struct bnxt *bp)
3117 for (i = 0; i < bp->cp_nr_rings; i++) {
3118 struct bnxt_napi *bnapi = bp->bnapi[i];
3119 struct bnxt_cp_ring_info *cpr;
3120 struct bnxt_ring_struct *ring;
3126 cpr = &bnapi->cp_ring;
3127 ring = &cpr->cp_ring_struct;
3129 bnxt_free_ring(bp, &ring->ring_mem);
3131 for (j = 0; j < 2; j++) {
3132 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3135 ring = &cpr2->cp_ring_struct;
3136 bnxt_free_ring(bp, &ring->ring_mem);
3138 cpr->cp_ring_arr[j] = NULL;
3144 static struct bnxt_cp_ring_info *bnxt_alloc_cp_sub_ring(struct bnxt *bp)
3146 struct bnxt_ring_mem_info *rmem;
3147 struct bnxt_ring_struct *ring;
3148 struct bnxt_cp_ring_info *cpr;
3151 cpr = kzalloc(sizeof(*cpr), GFP_KERNEL);
3155 ring = &cpr->cp_ring_struct;
3156 rmem = &ring->ring_mem;
3157 rmem->nr_pages = bp->cp_nr_pages;
3158 rmem->page_size = HW_CMPD_RING_SIZE;
3159 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3160 rmem->dma_arr = cpr->cp_desc_mapping;
3161 rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
3162 rc = bnxt_alloc_ring(bp, rmem);
3164 bnxt_free_ring(bp, rmem);
3171 static int bnxt_alloc_cp_rings(struct bnxt *bp)
3173 bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
3174 int i, rc, ulp_base_vec, ulp_msix;
3176 ulp_msix = bnxt_get_ulp_msix_num(bp);
3177 ulp_base_vec = bnxt_get_ulp_msix_base(bp);
3178 for (i = 0; i < bp->cp_nr_rings; i++) {
3179 struct bnxt_napi *bnapi = bp->bnapi[i];
3180 struct bnxt_cp_ring_info *cpr;
3181 struct bnxt_ring_struct *ring;
3186 cpr = &bnapi->cp_ring;
3188 ring = &cpr->cp_ring_struct;
3190 rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3194 if (ulp_msix && i >= ulp_base_vec)
3195 ring->map_idx = i + ulp_msix;
3199 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
3202 if (i < bp->rx_nr_rings) {
3203 struct bnxt_cp_ring_info *cpr2 =
3204 bnxt_alloc_cp_sub_ring(bp);
3206 cpr->cp_ring_arr[BNXT_RX_HDL] = cpr2;
3209 cpr2->bnapi = bnapi;
3211 if ((sh && i < bp->tx_nr_rings) ||
3212 (!sh && i >= bp->rx_nr_rings)) {
3213 struct bnxt_cp_ring_info *cpr2 =
3214 bnxt_alloc_cp_sub_ring(bp);
3216 cpr->cp_ring_arr[BNXT_TX_HDL] = cpr2;
3219 cpr2->bnapi = bnapi;
3225 static void bnxt_init_ring_struct(struct bnxt *bp)
3229 for (i = 0; i < bp->cp_nr_rings; i++) {
3230 struct bnxt_napi *bnapi = bp->bnapi[i];
3231 struct bnxt_ring_mem_info *rmem;
3232 struct bnxt_cp_ring_info *cpr;
3233 struct bnxt_rx_ring_info *rxr;
3234 struct bnxt_tx_ring_info *txr;
3235 struct bnxt_ring_struct *ring;
3240 cpr = &bnapi->cp_ring;
3241 ring = &cpr->cp_ring_struct;
3242 rmem = &ring->ring_mem;
3243 rmem->nr_pages = bp->cp_nr_pages;
3244 rmem->page_size = HW_CMPD_RING_SIZE;
3245 rmem->pg_arr = (void **)cpr->cp_desc_ring;
3246 rmem->dma_arr = cpr->cp_desc_mapping;
3247 rmem->vmem_size = 0;
3249 rxr = bnapi->rx_ring;
3253 ring = &rxr->rx_ring_struct;
3254 rmem = &ring->ring_mem;
3255 rmem->nr_pages = bp->rx_nr_pages;
3256 rmem->page_size = HW_RXBD_RING_SIZE;
3257 rmem->pg_arr = (void **)rxr->rx_desc_ring;
3258 rmem->dma_arr = rxr->rx_desc_mapping;
3259 rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
3260 rmem->vmem = (void **)&rxr->rx_buf_ring;
3262 ring = &rxr->rx_agg_ring_struct;
3263 rmem = &ring->ring_mem;
3264 rmem->nr_pages = bp->rx_agg_nr_pages;
3265 rmem->page_size = HW_RXBD_RING_SIZE;
3266 rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
3267 rmem->dma_arr = rxr->rx_agg_desc_mapping;
3268 rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
3269 rmem->vmem = (void **)&rxr->rx_agg_ring;
3272 txr = bnapi->tx_ring;
3276 ring = &txr->tx_ring_struct;
3277 rmem = &ring->ring_mem;
3278 rmem->nr_pages = bp->tx_nr_pages;
3279 rmem->page_size = HW_RXBD_RING_SIZE;
3280 rmem->pg_arr = (void **)txr->tx_desc_ring;
3281 rmem->dma_arr = txr->tx_desc_mapping;
3282 rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
3283 rmem->vmem = (void **)&txr->tx_buf_ring;
3287 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
3291 struct rx_bd **rx_buf_ring;
3293 rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
3294 for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
3298 rxbd = rx_buf_ring[i];
3302 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
3303 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
3304 rxbd->rx_bd_opaque = prod;
3309 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
3311 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
3312 struct net_device *dev = bp->dev;
3316 prod = rxr->rx_prod;
3317 for (i = 0; i < bp->rx_ring_size; i++) {
3318 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
3319 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
3320 ring_nr, i, bp->rx_ring_size);
3323 prod = NEXT_RX(prod);
3325 rxr->rx_prod = prod;
3327 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
3330 prod = rxr->rx_agg_prod;
3331 for (i = 0; i < bp->rx_agg_ring_size; i++) {
3332 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
3333 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
3334 ring_nr, i, bp->rx_ring_size);
3337 prod = NEXT_RX_AGG(prod);
3339 rxr->rx_agg_prod = prod;
3345 for (i = 0; i < bp->max_tpa; i++) {
3346 data = __bnxt_alloc_rx_data(bp, &mapping, GFP_KERNEL);
3350 rxr->rx_tpa[i].data = data;
3351 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
3352 rxr->rx_tpa[i].mapping = mapping;
3358 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
3360 struct bnxt_rx_ring_info *rxr;
3361 struct bnxt_ring_struct *ring;
3364 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
3365 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
3367 if (NET_IP_ALIGN == 2)
3368 type |= RX_BD_FLAGS_SOP;
3370 rxr = &bp->rx_ring[ring_nr];
3371 ring = &rxr->rx_ring_struct;
3372 bnxt_init_rxbd_pages(ring, type);
3374 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
3375 bpf_prog_add(bp->xdp_prog, 1);
3376 rxr->xdp_prog = bp->xdp_prog;
3378 ring->fw_ring_id = INVALID_HW_RING_ID;
3380 ring = &rxr->rx_agg_ring_struct;
3381 ring->fw_ring_id = INVALID_HW_RING_ID;
3383 if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
3384 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
3385 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
3387 bnxt_init_rxbd_pages(ring, type);
3390 return bnxt_alloc_one_rx_ring(bp, ring_nr);
3393 static void bnxt_init_cp_rings(struct bnxt *bp)
3397 for (i = 0; i < bp->cp_nr_rings; i++) {
3398 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
3399 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3401 ring->fw_ring_id = INVALID_HW_RING_ID;
3402 cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3403 cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3404 for (j = 0; j < 2; j++) {
3405 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
3410 ring = &cpr2->cp_ring_struct;
3411 ring->fw_ring_id = INVALID_HW_RING_ID;
3412 cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
3413 cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
3418 static int bnxt_init_rx_rings(struct bnxt *bp)
3422 if (BNXT_RX_PAGE_MODE(bp)) {
3423 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
3424 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
3426 bp->rx_offset = BNXT_RX_OFFSET;
3427 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
3430 for (i = 0; i < bp->rx_nr_rings; i++) {
3431 rc = bnxt_init_one_rx_ring(bp, i);
3439 static int bnxt_init_tx_rings(struct bnxt *bp)
3443 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
3444 BNXT_MIN_TX_DESC_CNT);
3446 for (i = 0; i < bp->tx_nr_rings; i++) {
3447 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3448 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3450 ring->fw_ring_id = INVALID_HW_RING_ID;
3456 static void bnxt_free_ring_grps(struct bnxt *bp)
3458 kfree(bp->grp_info);
3459 bp->grp_info = NULL;
3462 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
3467 bp->grp_info = kcalloc(bp->cp_nr_rings,
3468 sizeof(struct bnxt_ring_grp_info),
3473 for (i = 0; i < bp->cp_nr_rings; i++) {
3475 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
3476 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3477 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
3478 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
3479 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3484 static void bnxt_free_vnics(struct bnxt *bp)
3486 kfree(bp->vnic_info);
3487 bp->vnic_info = NULL;
3491 static int bnxt_alloc_vnics(struct bnxt *bp)
3495 #ifdef CONFIG_RFS_ACCEL
3496 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
3497 num_vnics += bp->rx_nr_rings;
3500 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3503 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
3508 bp->nr_vnics = num_vnics;
3512 static void bnxt_init_vnics(struct bnxt *bp)
3516 for (i = 0; i < bp->nr_vnics; i++) {
3517 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3520 vnic->fw_vnic_id = INVALID_HW_RING_ID;
3521 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
3522 vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
3524 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
3526 if (bp->vnic_info[i].rss_hash_key) {
3528 prandom_bytes(vnic->rss_hash_key,
3531 memcpy(vnic->rss_hash_key,
3532 bp->vnic_info[0].rss_hash_key,
3538 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
3542 pages = ring_size / desc_per_pg;
3549 while (pages & (pages - 1))
3555 void bnxt_set_tpa_flags(struct bnxt *bp)
3557 bp->flags &= ~BNXT_FLAG_TPA;
3558 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
3560 if (bp->dev->features & NETIF_F_LRO)
3561 bp->flags |= BNXT_FLAG_LRO;
3562 else if (bp->dev->features & NETIF_F_GRO_HW)
3563 bp->flags |= BNXT_FLAG_GRO;
3566 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
3569 void bnxt_set_ring_params(struct bnxt *bp)
3571 u32 ring_size, rx_size, rx_space, max_rx_cmpl;
3572 u32 agg_factor = 0, agg_ring_size = 0;
3574 /* 8 for CRC and VLAN */
3575 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
3577 rx_space = rx_size + NET_SKB_PAD +
3578 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3580 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
3581 ring_size = bp->rx_ring_size;
3582 bp->rx_agg_ring_size = 0;
3583 bp->rx_agg_nr_pages = 0;
3585 if (bp->flags & BNXT_FLAG_TPA)
3586 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
3588 bp->flags &= ~BNXT_FLAG_JUMBO;
3589 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
3592 bp->flags |= BNXT_FLAG_JUMBO;
3593 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
3594 if (jumbo_factor > agg_factor)
3595 agg_factor = jumbo_factor;
3597 agg_ring_size = ring_size * agg_factor;
3599 if (agg_ring_size) {
3600 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
3602 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
3603 u32 tmp = agg_ring_size;
3605 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
3606 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
3607 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
3608 tmp, agg_ring_size);
3610 bp->rx_agg_ring_size = agg_ring_size;
3611 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
3612 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
3613 rx_space = rx_size + NET_SKB_PAD +
3614 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3617 bp->rx_buf_use_size = rx_size;
3618 bp->rx_buf_size = rx_space;
3620 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
3621 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
3623 ring_size = bp->tx_ring_size;
3624 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
3625 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
3627 max_rx_cmpl = bp->rx_ring_size;
3628 /* MAX TPA needs to be added because TPA_START completions are
3629 * immediately recycled, so the TPA completions are not bound by
3632 if (bp->flags & BNXT_FLAG_TPA)
3633 max_rx_cmpl += bp->max_tpa;
3634 /* RX and TPA completions are 32-byte, all others are 16-byte */
3635 ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
3636 bp->cp_ring_size = ring_size;
3638 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
3639 if (bp->cp_nr_pages > MAX_CP_PAGES) {
3640 bp->cp_nr_pages = MAX_CP_PAGES;
3641 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
3642 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
3643 ring_size, bp->cp_ring_size);
3645 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
3646 bp->cp_ring_mask = bp->cp_bit - 1;
3649 /* Changing allocation mode of RX rings.
3650 * TODO: Update when extending xdp_rxq_info to support allocation modes.
3652 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
3655 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
3658 min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
3659 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
3660 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
3661 bp->rx_dir = DMA_BIDIRECTIONAL;
3662 bp->rx_skb_func = bnxt_rx_page_skb;
3663 /* Disable LRO or GRO_HW */
3664 netdev_update_features(bp->dev);
3666 bp->dev->max_mtu = bp->max_mtu;
3667 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
3668 bp->rx_dir = DMA_FROM_DEVICE;
3669 bp->rx_skb_func = bnxt_rx_skb;
3674 static void bnxt_free_vnic_attributes(struct bnxt *bp)
3677 struct bnxt_vnic_info *vnic;
3678 struct pci_dev *pdev = bp->pdev;
3683 for (i = 0; i < bp->nr_vnics; i++) {
3684 vnic = &bp->vnic_info[i];
3686 kfree(vnic->fw_grp_ids);
3687 vnic->fw_grp_ids = NULL;
3689 kfree(vnic->uc_list);
3690 vnic->uc_list = NULL;
3692 if (vnic->mc_list) {
3693 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
3694 vnic->mc_list, vnic->mc_list_mapping);
3695 vnic->mc_list = NULL;
3698 if (vnic->rss_table) {
3699 dma_free_coherent(&pdev->dev, vnic->rss_table_size,
3701 vnic->rss_table_dma_addr);
3702 vnic->rss_table = NULL;
3705 vnic->rss_hash_key = NULL;
3710 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
3712 int i, rc = 0, size;
3713 struct bnxt_vnic_info *vnic;
3714 struct pci_dev *pdev = bp->pdev;
3717 for (i = 0; i < bp->nr_vnics; i++) {
3718 vnic = &bp->vnic_info[i];
3720 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
3721 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
3724 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
3725 if (!vnic->uc_list) {
3732 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
3733 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
3735 dma_alloc_coherent(&pdev->dev,
3737 &vnic->mc_list_mapping,
3739 if (!vnic->mc_list) {
3745 if (bp->flags & BNXT_FLAG_CHIP_P5)
3746 goto vnic_skip_grps;
3748 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3749 max_rings = bp->rx_nr_rings;
3753 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
3754 if (!vnic->fw_grp_ids) {
3759 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
3760 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
3763 /* Allocate rss table and hash key */
3764 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
3765 if (bp->flags & BNXT_FLAG_CHIP_P5)
3766 size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
3768 vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
3769 vnic->rss_table = dma_alloc_coherent(&pdev->dev,
3770 vnic->rss_table_size,
3771 &vnic->rss_table_dma_addr,
3773 if (!vnic->rss_table) {
3778 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
3779 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
3787 static void bnxt_free_hwrm_resources(struct bnxt *bp)
3789 struct pci_dev *pdev = bp->pdev;
3791 if (bp->hwrm_cmd_resp_addr) {
3792 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
3793 bp->hwrm_cmd_resp_dma_addr);
3794 bp->hwrm_cmd_resp_addr = NULL;
3797 if (bp->hwrm_cmd_kong_resp_addr) {
3798 dma_free_coherent(&pdev->dev, PAGE_SIZE,
3799 bp->hwrm_cmd_kong_resp_addr,
3800 bp->hwrm_cmd_kong_resp_dma_addr);
3801 bp->hwrm_cmd_kong_resp_addr = NULL;
3805 static int bnxt_alloc_kong_hwrm_resources(struct bnxt *bp)
3807 struct pci_dev *pdev = bp->pdev;
3809 if (bp->hwrm_cmd_kong_resp_addr)
3812 bp->hwrm_cmd_kong_resp_addr =
3813 dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3814 &bp->hwrm_cmd_kong_resp_dma_addr,
3816 if (!bp->hwrm_cmd_kong_resp_addr)
3822 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
3824 struct pci_dev *pdev = bp->pdev;
3826 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3827 &bp->hwrm_cmd_resp_dma_addr,
3829 if (!bp->hwrm_cmd_resp_addr)
3835 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
3837 if (bp->hwrm_short_cmd_req_addr) {
3838 struct pci_dev *pdev = bp->pdev;
3840 dma_free_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3841 bp->hwrm_short_cmd_req_addr,
3842 bp->hwrm_short_cmd_req_dma_addr);
3843 bp->hwrm_short_cmd_req_addr = NULL;
3847 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
3849 struct pci_dev *pdev = bp->pdev;
3851 if (bp->hwrm_short_cmd_req_addr)
3854 bp->hwrm_short_cmd_req_addr =
3855 dma_alloc_coherent(&pdev->dev, bp->hwrm_max_ext_req_len,
3856 &bp->hwrm_short_cmd_req_dma_addr,
3858 if (!bp->hwrm_short_cmd_req_addr)
3864 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
3866 kfree(stats->hw_masks);
3867 stats->hw_masks = NULL;
3868 kfree(stats->sw_stats);
3869 stats->sw_stats = NULL;
3870 if (stats->hw_stats) {
3871 dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
3872 stats->hw_stats_map);
3873 stats->hw_stats = NULL;
3877 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
3880 stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
3881 &stats->hw_stats_map, GFP_KERNEL);
3882 if (!stats->hw_stats)
3885 stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
3886 if (!stats->sw_stats)
3890 stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
3891 if (!stats->hw_masks)
3897 bnxt_free_stats_mem(bp, stats);
3901 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
3905 for (i = 0; i < count; i++)
3909 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
3913 for (i = 0; i < count; i++)
3914 mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
3917 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
3918 struct bnxt_stats_mem *stats)
3920 struct hwrm_func_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
3921 struct hwrm_func_qstats_ext_input req = {0};
3925 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
3926 !(bp->flags & BNXT_FLAG_CHIP_P5))
3929 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QSTATS_EXT, -1, -1);
3930 req.fid = cpu_to_le16(0xffff);
3931 req.flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
3932 mutex_lock(&bp->hwrm_cmd_lock);
3933 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3937 hw_masks = &resp->rx_ucast_pkts;
3938 bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
3941 mutex_unlock(&bp->hwrm_cmd_lock);
3945 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
3946 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
3948 static void bnxt_init_stats(struct bnxt *bp)
3950 struct bnxt_napi *bnapi = bp->bnapi[0];
3951 struct bnxt_cp_ring_info *cpr;
3952 struct bnxt_stats_mem *stats;
3953 __le64 *rx_stats, *tx_stats;
3954 int rc, rx_count, tx_count;
3955 u64 *rx_masks, *tx_masks;
3959 cpr = &bnapi->cp_ring;
3960 stats = &cpr->stats;
3961 rc = bnxt_hwrm_func_qstat_ext(bp, stats);
3963 if (bp->flags & BNXT_FLAG_CHIP_P5)
3964 mask = (1ULL << 48) - 1;
3967 bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
3969 if (bp->flags & BNXT_FLAG_PORT_STATS) {
3970 stats = &bp->port_stats;
3971 rx_stats = stats->hw_stats;
3972 rx_masks = stats->hw_masks;
3973 rx_count = sizeof(struct rx_port_stats) / 8;
3974 tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3975 tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
3976 tx_count = sizeof(struct tx_port_stats) / 8;
3978 flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
3979 rc = bnxt_hwrm_port_qstats(bp, flags);
3981 mask = (1ULL << 40) - 1;
3983 bnxt_fill_masks(rx_masks, mask, rx_count);
3984 bnxt_fill_masks(tx_masks, mask, tx_count);
3986 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
3987 bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
3988 bnxt_hwrm_port_qstats(bp, 0);
3991 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
3992 stats = &bp->rx_port_stats_ext;
3993 rx_stats = stats->hw_stats;
3994 rx_masks = stats->hw_masks;
3995 rx_count = sizeof(struct rx_port_stats_ext) / 8;
3996 stats = &bp->tx_port_stats_ext;
3997 tx_stats = stats->hw_stats;
3998 tx_masks = stats->hw_masks;
3999 tx_count = sizeof(struct tx_port_stats_ext) / 8;
4001 flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4002 rc = bnxt_hwrm_port_qstats_ext(bp, flags);
4004 mask = (1ULL << 40) - 1;
4006 bnxt_fill_masks(rx_masks, mask, rx_count);
4008 bnxt_fill_masks(tx_masks, mask, tx_count);
4010 bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
4012 bnxt_copy_hw_masks(tx_masks, tx_stats,
4014 bnxt_hwrm_port_qstats_ext(bp, 0);
4019 static void bnxt_free_port_stats(struct bnxt *bp)
4021 bp->flags &= ~BNXT_FLAG_PORT_STATS;
4022 bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
4024 bnxt_free_stats_mem(bp, &bp->port_stats);
4025 bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
4026 bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
4029 static void bnxt_free_ring_stats(struct bnxt *bp)
4036 for (i = 0; i < bp->cp_nr_rings; i++) {
4037 struct bnxt_napi *bnapi = bp->bnapi[i];
4038 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4040 bnxt_free_stats_mem(bp, &cpr->stats);
4044 static int bnxt_alloc_stats(struct bnxt *bp)
4049 size = bp->hw_ring_stats_size;
4051 for (i = 0; i < bp->cp_nr_rings; i++) {
4052 struct bnxt_napi *bnapi = bp->bnapi[i];
4053 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4055 cpr->stats.len = size;
4056 rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
4060 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4063 if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
4066 if (bp->port_stats.hw_stats)
4067 goto alloc_ext_stats;
4069 bp->port_stats.len = BNXT_PORT_STATS_SIZE;
4070 rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
4074 bp->flags |= BNXT_FLAG_PORT_STATS;
4077 /* Display extended statistics only if FW supports it */
4078 if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
4079 if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
4082 if (bp->rx_port_stats_ext.hw_stats)
4083 goto alloc_tx_ext_stats;
4085 bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
4086 rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
4087 /* Extended stats are optional */
4092 if (bp->tx_port_stats_ext.hw_stats)
4095 if (bp->hwrm_spec_code >= 0x10902 ||
4096 (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
4097 bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
4098 rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
4099 /* Extended stats are optional */
4103 bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
4107 static void bnxt_clear_ring_indices(struct bnxt *bp)
4114 for (i = 0; i < bp->cp_nr_rings; i++) {
4115 struct bnxt_napi *bnapi = bp->bnapi[i];
4116 struct bnxt_cp_ring_info *cpr;
4117 struct bnxt_rx_ring_info *rxr;
4118 struct bnxt_tx_ring_info *txr;
4123 cpr = &bnapi->cp_ring;
4124 cpr->cp_raw_cons = 0;
4126 txr = bnapi->tx_ring;
4132 rxr = bnapi->rx_ring;
4135 rxr->rx_agg_prod = 0;
4136 rxr->rx_sw_agg_prod = 0;
4137 rxr->rx_next_cons = 0;
4142 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
4144 #ifdef CONFIG_RFS_ACCEL
4147 /* Under rtnl_lock and all our NAPIs have been disabled. It's
4148 * safe to delete the hash table.
4150 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
4151 struct hlist_head *head;
4152 struct hlist_node *tmp;
4153 struct bnxt_ntuple_filter *fltr;
4155 head = &bp->ntp_fltr_hash_tbl[i];
4156 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
4157 hlist_del(&fltr->hash);
4162 kfree(bp->ntp_fltr_bmap);
4163 bp->ntp_fltr_bmap = NULL;
4165 bp->ntp_fltr_count = 0;
4169 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
4171 #ifdef CONFIG_RFS_ACCEL
4174 if (!(bp->flags & BNXT_FLAG_RFS))
4177 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
4178 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
4180 bp->ntp_fltr_count = 0;
4181 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
4185 if (!bp->ntp_fltr_bmap)
4194 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
4196 bnxt_free_vnic_attributes(bp);
4197 bnxt_free_tx_rings(bp);
4198 bnxt_free_rx_rings(bp);
4199 bnxt_free_cp_rings(bp);
4200 bnxt_free_ntp_fltrs(bp, irq_re_init);
4202 bnxt_free_ring_stats(bp);
4203 if (!(bp->fw_cap & BNXT_FW_CAP_PORT_STATS_NO_RESET) ||
4204 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
4205 bnxt_free_port_stats(bp);
4206 bnxt_free_ring_grps(bp);
4207 bnxt_free_vnics(bp);
4208 kfree(bp->tx_ring_map);
4209 bp->tx_ring_map = NULL;
4217 bnxt_clear_ring_indices(bp);
4221 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
4223 int i, j, rc, size, arr_size;
4227 /* Allocate bnapi mem pointer array and mem block for
4230 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
4232 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
4233 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
4239 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
4240 bp->bnapi[i] = bnapi;
4241 bp->bnapi[i]->index = i;
4242 bp->bnapi[i]->bp = bp;
4243 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4244 struct bnxt_cp_ring_info *cpr =
4245 &bp->bnapi[i]->cp_ring;
4247 cpr->cp_ring_struct.ring_mem.flags =
4248 BNXT_RMEM_RING_PTE_FLAG;
4252 bp->rx_ring = kcalloc(bp->rx_nr_rings,
4253 sizeof(struct bnxt_rx_ring_info),
4258 for (i = 0; i < bp->rx_nr_rings; i++) {
4259 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4261 if (bp->flags & BNXT_FLAG_CHIP_P5) {
4262 rxr->rx_ring_struct.ring_mem.flags =
4263 BNXT_RMEM_RING_PTE_FLAG;
4264 rxr->rx_agg_ring_struct.ring_mem.flags =
4265 BNXT_RMEM_RING_PTE_FLAG;
4267 rxr->bnapi = bp->bnapi[i];
4268 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
4271 bp->tx_ring = kcalloc(bp->tx_nr_rings,
4272 sizeof(struct bnxt_tx_ring_info),
4277 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
4280 if (!bp->tx_ring_map)
4283 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4286 j = bp->rx_nr_rings;
4288 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
4289 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4291 if (bp->flags & BNXT_FLAG_CHIP_P5)
4292 txr->tx_ring_struct.ring_mem.flags =
4293 BNXT_RMEM_RING_PTE_FLAG;
4294 txr->bnapi = bp->bnapi[j];
4295 bp->bnapi[j]->tx_ring = txr;
4296 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
4297 if (i >= bp->tx_nr_rings_xdp) {
4298 txr->txq_index = i - bp->tx_nr_rings_xdp;
4299 bp->bnapi[j]->tx_int = bnxt_tx_int;
4301 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
4302 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
4306 rc = bnxt_alloc_stats(bp);
4309 bnxt_init_stats(bp);
4311 rc = bnxt_alloc_ntp_fltrs(bp);
4315 rc = bnxt_alloc_vnics(bp);
4320 bnxt_init_ring_struct(bp);
4322 rc = bnxt_alloc_rx_rings(bp);
4326 rc = bnxt_alloc_tx_rings(bp);
4330 rc = bnxt_alloc_cp_rings(bp);
4334 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
4335 BNXT_VNIC_UCAST_FLAG;
4336 rc = bnxt_alloc_vnic_attributes(bp);
4342 bnxt_free_mem(bp, true);
4346 static void bnxt_disable_int(struct bnxt *bp)
4353 for (i = 0; i < bp->cp_nr_rings; i++) {
4354 struct bnxt_napi *bnapi = bp->bnapi[i];
4355 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4356 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4358 if (ring->fw_ring_id != INVALID_HW_RING_ID)
4359 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
4363 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
4365 struct bnxt_napi *bnapi = bp->bnapi[n];
4366 struct bnxt_cp_ring_info *cpr;
4368 cpr = &bnapi->cp_ring;
4369 return cpr->cp_ring_struct.map_idx;
4372 static void bnxt_disable_int_sync(struct bnxt *bp)
4376 atomic_inc(&bp->intr_sem);
4378 bnxt_disable_int(bp);
4379 for (i = 0; i < bp->cp_nr_rings; i++) {
4380 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
4382 synchronize_irq(bp->irq_tbl[map_idx].vector);
4386 static void bnxt_enable_int(struct bnxt *bp)
4390 atomic_set(&bp->intr_sem, 0);
4391 for (i = 0; i < bp->cp_nr_rings; i++) {
4392 struct bnxt_napi *bnapi = bp->bnapi[i];
4393 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4395 bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
4399 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
4400 u16 cmpl_ring, u16 target_id)
4402 struct input *req = request;
4404 req->req_type = cpu_to_le16(req_type);
4405 req->cmpl_ring = cpu_to_le16(cmpl_ring);
4406 req->target_id = cpu_to_le16(target_id);
4407 if (bnxt_kong_hwrm_message(bp, req))
4408 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
4410 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
4413 static int bnxt_hwrm_to_stderr(u32 hwrm_err)
4416 case HWRM_ERR_CODE_SUCCESS:
4418 case HWRM_ERR_CODE_RESOURCE_LOCKED:
4420 case HWRM_ERR_CODE_RESOURCE_ACCESS_DENIED:
4422 case HWRM_ERR_CODE_RESOURCE_ALLOC_ERROR:
4424 case HWRM_ERR_CODE_INVALID_PARAMS:
4425 case HWRM_ERR_CODE_INVALID_FLAGS:
4426 case HWRM_ERR_CODE_INVALID_ENABLES:
4427 case HWRM_ERR_CODE_UNSUPPORTED_TLV:
4428 case HWRM_ERR_CODE_UNSUPPORTED_OPTION_ERR:
4430 case HWRM_ERR_CODE_NO_BUFFER:
4432 case HWRM_ERR_CODE_HOT_RESET_PROGRESS:
4433 case HWRM_ERR_CODE_BUSY:
4435 case HWRM_ERR_CODE_CMD_NOT_SUPPORTED:
4442 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
4443 int timeout, bool silent)
4445 int i, intr_process, rc, tmo_count;
4446 struct input *req = msg;
4449 u16 cp_ring_id, len = 0;
4450 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
4451 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
4452 struct hwrm_short_input short_input = {0};
4453 u32 doorbell_offset = BNXT_GRCPF_REG_CHIMP_COMM_TRIGGER;
4454 u32 bar_offset = BNXT_GRCPF_REG_CHIMP_COMM;
4455 u16 dst = BNXT_HWRM_CHNL_CHIMP;
4457 if (BNXT_NO_FW_ACCESS(bp) &&
4458 le16_to_cpu(req->req_type) != HWRM_FUNC_RESET)
4461 if (msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4462 if (msg_len > bp->hwrm_max_ext_req_len ||
4463 !bp->hwrm_short_cmd_req_addr)
4467 if (bnxt_hwrm_kong_chnl(bp, req)) {
4468 dst = BNXT_HWRM_CHNL_KONG;
4469 bar_offset = BNXT_GRCPF_REG_KONG_COMM;
4470 doorbell_offset = BNXT_GRCPF_REG_KONG_COMM_TRIGGER;
4471 resp = bp->hwrm_cmd_kong_resp_addr;
4474 memset(resp, 0, PAGE_SIZE);
4475 cp_ring_id = le16_to_cpu(req->cmpl_ring);
4476 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
4478 req->seq_id = cpu_to_le16(bnxt_get_hwrm_seq_id(bp, dst));
4479 /* currently supports only one outstanding message */
4481 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
4483 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
4484 msg_len > BNXT_HWRM_MAX_REQ_LEN) {
4485 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
4488 /* Set boundary for maximum extended request length for short
4489 * cmd format. If passed up from device use the max supported
4490 * internal req length.
4492 max_msg_len = bp->hwrm_max_ext_req_len;
4494 memcpy(short_cmd_req, req, msg_len);
4495 if (msg_len < max_msg_len)
4496 memset(short_cmd_req + msg_len, 0,
4497 max_msg_len - msg_len);
4499 short_input.req_type = req->req_type;
4500 short_input.signature =
4501 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
4502 short_input.size = cpu_to_le16(msg_len);
4503 short_input.req_addr =
4504 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
4506 data = (u32 *)&short_input;
4507 msg_len = sizeof(short_input);
4509 /* Sync memory write before updating doorbell */
4512 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
4515 /* Write request msg to hwrm channel */
4516 __iowrite32_copy(bp->bar0 + bar_offset, data, msg_len / 4);
4518 for (i = msg_len; i < max_req_len; i += 4)
4519 writel(0, bp->bar0 + bar_offset + i);
4521 /* Ring channel doorbell */
4522 writel(1, bp->bar0 + doorbell_offset);
4524 if (!pci_is_enabled(bp->pdev))
4528 timeout = DFLT_HWRM_CMD_TIMEOUT;
4529 /* convert timeout to usec */
4533 /* Short timeout for the first few iterations:
4534 * number of loops = number of loops for short timeout +
4535 * number of loops for standard timeout.
4537 tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
4538 timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
4539 tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
4542 u16 seq_id = bp->hwrm_intr_seq_id;
4544 /* Wait until hwrm response cmpl interrupt is processed */
4545 while (bp->hwrm_intr_seq_id != (u16)~seq_id &&
4547 /* Abort the wait for completion if the FW health
4550 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4552 /* on first few passes, just barely sleep */
4553 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4554 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4555 HWRM_SHORT_MAX_TIMEOUT);
4557 usleep_range(HWRM_MIN_TIMEOUT,
4561 if (bp->hwrm_intr_seq_id != (u16)~seq_id) {
4563 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
4564 le16_to_cpu(req->req_type));
4567 len = le16_to_cpu(resp->resp_len);
4568 valid = ((u8 *)resp) + len - 1;
4572 /* Check if response len is updated */
4573 for (i = 0; i < tmo_count; i++) {
4574 /* Abort the wait for completion if the FW health
4577 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
4579 len = le16_to_cpu(resp->resp_len);
4582 /* on first few passes, just barely sleep */
4583 if (i < HWRM_SHORT_TIMEOUT_COUNTER)
4584 usleep_range(HWRM_SHORT_MIN_TIMEOUT,
4585 HWRM_SHORT_MAX_TIMEOUT);
4587 usleep_range(HWRM_MIN_TIMEOUT,
4591 if (i >= tmo_count) {
4593 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
4594 HWRM_TOTAL_TIMEOUT(i),
4595 le16_to_cpu(req->req_type),
4596 le16_to_cpu(req->seq_id), len);
4600 /* Last byte of resp contains valid bit */
4601 valid = ((u8 *)resp) + len - 1;
4602 for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
4603 /* make sure we read from updated DMA memory */
4610 if (j >= HWRM_VALID_BIT_DELAY_USEC) {
4612 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
4613 HWRM_TOTAL_TIMEOUT(i),
4614 le16_to_cpu(req->req_type),
4615 le16_to_cpu(req->seq_id), len,
4621 /* Zero valid bit for compatibility. Valid bit in an older spec
4622 * may become a new field in a newer spec. We must make sure that
4623 * a new field not implemented by old spec will read zero.
4626 rc = le16_to_cpu(resp->error_code);
4628 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
4629 le16_to_cpu(resp->req_type),
4630 le16_to_cpu(resp->seq_id), rc);
4631 return bnxt_hwrm_to_stderr(rc);
4634 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4636 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
4639 int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4642 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4645 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
4649 mutex_lock(&bp->hwrm_cmd_lock);
4650 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
4651 mutex_unlock(&bp->hwrm_cmd_lock);
4655 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
4660 mutex_lock(&bp->hwrm_cmd_lock);
4661 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
4662 mutex_unlock(&bp->hwrm_cmd_lock);
4666 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
4669 struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
4670 struct hwrm_func_drv_rgtr_input req = {0};
4671 DECLARE_BITMAP(async_events_bmap, 256);
4672 u32 *events = (u32 *)async_events_bmap;
4676 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
4679 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
4680 FUNC_DRV_RGTR_REQ_ENABLES_VER |
4681 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4683 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
4684 flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
4685 if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
4686 flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
4687 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
4688 flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
4689 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
4690 req.flags = cpu_to_le32(flags);
4691 req.ver_maj_8b = DRV_VER_MAJ;
4692 req.ver_min_8b = DRV_VER_MIN;
4693 req.ver_upd_8b = DRV_VER_UPD;
4694 req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
4695 req.ver_min = cpu_to_le16(DRV_VER_MIN);
4696 req.ver_upd = cpu_to_le16(DRV_VER_UPD);
4702 memset(data, 0, sizeof(data));
4703 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
4704 u16 cmd = bnxt_vf_req_snif[i];
4705 unsigned int bit, idx;
4709 data[idx] |= 1 << bit;
4712 for (i = 0; i < 8; i++)
4713 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
4716 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
4719 if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
4720 req.flags |= cpu_to_le32(
4721 FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
4723 memset(async_events_bmap, 0, sizeof(async_events_bmap));
4724 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
4725 u16 event_id = bnxt_async_events_arr[i];
4727 if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
4728 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4730 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
4732 if (bmap && bmap_size) {
4733 for (i = 0; i < bmap_size; i++) {
4734 if (test_bit(i, bmap))
4735 __set_bit(i, async_events_bmap);
4738 for (i = 0; i < 8; i++)
4739 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
4743 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
4745 mutex_lock(&bp->hwrm_cmd_lock);
4746 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4748 set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
4750 cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
4751 bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
4753 mutex_unlock(&bp->hwrm_cmd_lock);
4757 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
4759 struct hwrm_func_drv_unrgtr_input req = {0};
4761 if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
4764 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
4765 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4768 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
4771 struct hwrm_tunnel_dst_port_free_input req = {0};
4773 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
4774 req.tunnel_type = tunnel_type;
4776 switch (tunnel_type) {
4777 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
4778 req.tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
4779 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
4781 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
4782 req.tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
4783 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
4789 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4791 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
4796 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
4800 struct hwrm_tunnel_dst_port_alloc_input req = {0};
4801 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4803 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
4805 req.tunnel_type = tunnel_type;
4806 req.tunnel_dst_port_val = port;
4808 mutex_lock(&bp->hwrm_cmd_lock);
4809 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4811 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
4816 switch (tunnel_type) {
4817 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
4818 bp->vxlan_fw_dst_port_id =
4819 le16_to_cpu(resp->tunnel_dst_port_id);
4821 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
4822 bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
4829 mutex_unlock(&bp->hwrm_cmd_lock);
4833 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
4835 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
4836 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4838 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
4839 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
4841 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
4842 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
4843 req.mask = cpu_to_le32(vnic->rx_mask);
4844 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4847 #ifdef CONFIG_RFS_ACCEL
4848 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
4849 struct bnxt_ntuple_filter *fltr)
4851 struct hwrm_cfa_ntuple_filter_free_input req = {0};
4853 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
4854 req.ntuple_filter_id = fltr->filter_id;
4855 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4858 #define BNXT_NTP_FLTR_FLAGS \
4859 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
4860 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
4861 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
4862 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
4863 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
4864 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
4865 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
4866 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
4867 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
4868 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
4869 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
4870 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
4871 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
4872 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
4874 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
4875 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
4877 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
4878 struct bnxt_ntuple_filter *fltr)
4880 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
4881 struct hwrm_cfa_ntuple_filter_alloc_output *resp;
4882 struct flow_keys *keys = &fltr->fkeys;
4883 struct bnxt_vnic_info *vnic;
4887 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
4888 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
4890 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
4891 flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
4892 req.dst_id = cpu_to_le16(fltr->rxq);
4894 vnic = &bp->vnic_info[fltr->rxq + 1];
4895 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
4897 req.flags = cpu_to_le32(flags);
4898 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
4900 req.ethertype = htons(ETH_P_IP);
4901 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
4902 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
4903 req.ip_protocol = keys->basic.ip_proto;
4905 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
4908 req.ethertype = htons(ETH_P_IPV6);
4910 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
4911 *(struct in6_addr *)&req.src_ipaddr[0] =
4912 keys->addrs.v6addrs.src;
4913 *(struct in6_addr *)&req.dst_ipaddr[0] =
4914 keys->addrs.v6addrs.dst;
4915 for (i = 0; i < 4; i++) {
4916 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4917 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
4920 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
4921 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4922 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
4923 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
4925 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
4926 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
4928 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
4931 req.src_port = keys->ports.src;
4932 req.src_port_mask = cpu_to_be16(0xffff);
4933 req.dst_port = keys->ports.dst;
4934 req.dst_port_mask = cpu_to_be16(0xffff);
4936 mutex_lock(&bp->hwrm_cmd_lock);
4937 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4939 resp = bnxt_get_hwrm_resp_addr(bp, &req);
4940 fltr->filter_id = resp->ntuple_filter_id;
4942 mutex_unlock(&bp->hwrm_cmd_lock);
4947 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
4951 struct hwrm_cfa_l2_filter_alloc_input req = {0};
4952 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4954 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
4955 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
4956 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
4958 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
4959 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
4961 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
4962 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
4963 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
4964 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
4965 req.l2_addr_mask[0] = 0xff;
4966 req.l2_addr_mask[1] = 0xff;
4967 req.l2_addr_mask[2] = 0xff;
4968 req.l2_addr_mask[3] = 0xff;
4969 req.l2_addr_mask[4] = 0xff;
4970 req.l2_addr_mask[5] = 0xff;
4972 mutex_lock(&bp->hwrm_cmd_lock);
4973 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4975 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
4977 mutex_unlock(&bp->hwrm_cmd_lock);
4981 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
4983 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
4986 /* Any associated ntuple filters will also be cleared by firmware. */
4987 mutex_lock(&bp->hwrm_cmd_lock);
4988 for (i = 0; i < num_of_vnics; i++) {
4989 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4991 for (j = 0; j < vnic->uc_filter_count; j++) {
4992 struct hwrm_cfa_l2_filter_free_input req = {0};
4994 bnxt_hwrm_cmd_hdr_init(bp, &req,
4995 HWRM_CFA_L2_FILTER_FREE, -1, -1);
4997 req.l2_filter_id = vnic->fw_l2_filter_id[j];
4999 rc = _hwrm_send_message(bp, &req, sizeof(req),
5002 vnic->uc_filter_count = 0;
5004 mutex_unlock(&bp->hwrm_cmd_lock);
5009 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
5011 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5012 u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
5013 struct hwrm_vnic_tpa_cfg_input req = {0};
5015 if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
5018 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
5021 u16 mss = bp->dev->mtu - 40;
5022 u32 nsegs, n, segs = 0, flags;
5024 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
5025 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
5026 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
5027 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
5028 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
5029 if (tpa_flags & BNXT_FLAG_GRO)
5030 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
5032 req.flags = cpu_to_le32(flags);
5035 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
5036 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
5037 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
5039 /* Number of segs are log2 units, and first packet is not
5040 * included as part of this units.
5042 if (mss <= BNXT_RX_PAGE_SIZE) {
5043 n = BNXT_RX_PAGE_SIZE / mss;
5044 nsegs = (MAX_SKB_FRAGS - 1) * n;
5046 n = mss / BNXT_RX_PAGE_SIZE;
5047 if (mss & (BNXT_RX_PAGE_SIZE - 1))
5049 nsegs = (MAX_SKB_FRAGS - n) / n;
5052 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5053 segs = MAX_TPA_SEGS_P5;
5054 max_aggs = bp->max_tpa;
5056 segs = ilog2(nsegs);
5058 req.max_agg_segs = cpu_to_le16(segs);
5059 req.max_aggs = cpu_to_le16(max_aggs);
5061 req.min_agg_len = cpu_to_le32(512);
5063 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5065 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5068 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
5070 struct bnxt_ring_grp_info *grp_info;
5072 grp_info = &bp->grp_info[ring->grp_idx];
5073 return grp_info->cp_fw_ring_id;
5076 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
5078 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5079 struct bnxt_napi *bnapi = rxr->bnapi;
5080 struct bnxt_cp_ring_info *cpr;
5082 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_RX_HDL];
5083 return cpr->cp_ring_struct.fw_ring_id;
5085 return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
5089 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
5091 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5092 struct bnxt_napi *bnapi = txr->bnapi;
5093 struct bnxt_cp_ring_info *cpr;
5095 cpr = bnapi->cp_ring.cp_ring_arr[BNXT_TX_HDL];
5096 return cpr->cp_ring_struct.fw_ring_id;
5098 return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
5102 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
5106 if (bp->flags & BNXT_FLAG_CHIP_P5)
5107 entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
5109 entries = HW_HASH_INDEX_SIZE;
5111 bp->rss_indir_tbl_entries = entries;
5112 bp->rss_indir_tbl = kmalloc_array(entries, sizeof(*bp->rss_indir_tbl),
5114 if (!bp->rss_indir_tbl)
5119 static void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp)
5121 u16 max_rings, max_entries, pad, i;
5123 if (!bp->rx_nr_rings)
5126 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5127 max_rings = bp->rx_nr_rings - 1;
5129 max_rings = bp->rx_nr_rings;
5131 max_entries = bnxt_get_rxfh_indir_size(bp->dev);
5133 for (i = 0; i < max_entries; i++)
5134 bp->rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
5136 pad = bp->rss_indir_tbl_entries - max_entries;
5138 memset(&bp->rss_indir_tbl[i], 0, pad * sizeof(u16));
5141 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
5143 u16 i, tbl_size, max_ring = 0;
5145 if (!bp->rss_indir_tbl)
5148 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5149 for (i = 0; i < tbl_size; i++)
5150 max_ring = max(max_ring, bp->rss_indir_tbl[i]);
5154 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
5156 if (bp->flags & BNXT_FLAG_CHIP_P5)
5157 return DIV_ROUND_UP(rx_rings, BNXT_RSS_TABLE_ENTRIES_P5);
5158 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5163 static void __bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5165 bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
5168 /* Fill the RSS indirection table with ring group ids */
5169 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
5171 j = bp->rss_indir_tbl[i];
5172 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
5176 static void __bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
5177 struct bnxt_vnic_info *vnic)
5179 __le16 *ring_tbl = vnic->rss_table;
5180 struct bnxt_rx_ring_info *rxr;
5183 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
5185 for (i = 0; i < tbl_size; i++) {
5188 j = bp->rss_indir_tbl[i];
5189 rxr = &bp->rx_ring[j];
5191 ring_id = rxr->rx_ring_struct.fw_ring_id;
5192 *ring_tbl++ = cpu_to_le16(ring_id);
5193 ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5194 *ring_tbl++ = cpu_to_le16(ring_id);
5198 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
5200 if (bp->flags & BNXT_FLAG_CHIP_P5)
5201 __bnxt_fill_hw_rss_tbl_p5(bp, vnic);
5203 __bnxt_fill_hw_rss_tbl(bp, vnic);
5206 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
5208 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5209 struct hwrm_vnic_rss_cfg_input req = {0};
5211 if ((bp->flags & BNXT_FLAG_CHIP_P5) ||
5212 vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
5215 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5217 bnxt_fill_hw_rss_tbl(bp, vnic);
5218 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5219 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5220 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
5221 req.hash_key_tbl_addr =
5222 cpu_to_le64(vnic->rss_hash_key_dma_addr);
5224 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5225 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5228 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp, u16 vnic_id, bool set_rss)
5230 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5231 struct hwrm_vnic_rss_cfg_input req = {0};
5232 dma_addr_t ring_tbl_map;
5235 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
5236 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5238 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5241 bnxt_fill_hw_rss_tbl(bp, vnic);
5242 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
5243 req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
5244 req.hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
5245 ring_tbl_map = vnic->rss_table_dma_addr;
5246 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
5247 for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
5250 req.ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
5251 req.ring_table_pair_index = i;
5252 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
5253 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5260 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
5262 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5263 struct hwrm_vnic_plcmodes_cfg_input req = {0};
5265 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
5266 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
5267 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
5268 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
5270 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
5271 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
5272 /* thresholds not implemented in firmware yet */
5273 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
5274 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
5275 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5276 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5279 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
5282 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
5284 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
5285 req.rss_cos_lb_ctx_id =
5286 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
5288 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5289 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
5292 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
5296 for (i = 0; i < bp->nr_vnics; i++) {
5297 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
5299 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
5300 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
5301 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
5304 bp->rsscos_nr_ctxs = 0;
5307 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
5310 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
5311 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
5312 bp->hwrm_cmd_resp_addr;
5314 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
5317 mutex_lock(&bp->hwrm_cmd_lock);
5318 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5320 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
5321 le16_to_cpu(resp->rss_cos_lb_ctx_id);
5322 mutex_unlock(&bp->hwrm_cmd_lock);
5327 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
5329 if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
5330 return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
5331 return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
5334 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
5336 unsigned int ring = 0, grp_idx;
5337 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5338 struct hwrm_vnic_cfg_input req = {0};
5341 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
5343 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5344 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
5346 req.default_rx_ring_id =
5347 cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
5348 req.default_cmpl_ring_id =
5349 cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
5351 cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
5352 VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
5355 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
5356 /* Only RSS support for now TBD: COS & LB */
5357 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
5358 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
5359 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5360 VNIC_CFG_REQ_ENABLES_MRU);
5361 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
5363 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
5364 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
5365 VNIC_CFG_REQ_ENABLES_MRU);
5366 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
5368 req.rss_rule = cpu_to_le16(0xffff);
5371 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
5372 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
5373 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
5374 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
5376 req.cos_rule = cpu_to_le16(0xffff);
5379 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5381 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
5383 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
5384 ring = bp->rx_nr_rings - 1;
5386 grp_idx = bp->rx_ring[ring].bnapi->index;
5387 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
5388 req.lb_rule = cpu_to_le16(0xffff);
5390 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + VLAN_HLEN);
5392 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
5393 #ifdef CONFIG_BNXT_SRIOV
5395 def_vlan = bp->vf.vlan;
5397 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
5398 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
5399 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
5400 req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
5402 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5405 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
5407 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
5408 struct hwrm_vnic_free_input req = {0};
5410 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
5412 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
5414 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5415 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
5419 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
5423 for (i = 0; i < bp->nr_vnics; i++)
5424 bnxt_hwrm_vnic_free_one(bp, i);
5427 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
5428 unsigned int start_rx_ring_idx,
5429 unsigned int nr_rings)
5432 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
5433 struct hwrm_vnic_alloc_input req = {0};
5434 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5435 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5437 if (bp->flags & BNXT_FLAG_CHIP_P5)
5438 goto vnic_no_ring_grps;
5440 /* map ring groups to this vnic */
5441 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
5442 grp_idx = bp->rx_ring[i].bnapi->index;
5443 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
5444 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
5448 vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
5452 for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
5453 vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
5455 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
5457 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
5459 mutex_lock(&bp->hwrm_cmd_lock);
5460 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5462 vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
5463 mutex_unlock(&bp->hwrm_cmd_lock);
5467 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
5469 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5470 struct hwrm_vnic_qcaps_input req = {0};
5473 bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
5474 bp->flags &= ~(BNXT_FLAG_NEW_RSS_CAP | BNXT_FLAG_ROCE_MIRROR_CAP);
5475 if (bp->hwrm_spec_code < 0x10600)
5478 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
5479 mutex_lock(&bp->hwrm_cmd_lock);
5480 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5482 u32 flags = le32_to_cpu(resp->flags);
5484 if (!(bp->flags & BNXT_FLAG_CHIP_P5) &&
5485 (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
5486 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
5488 VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
5489 bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
5491 /* Older P5 fw before EXT_HW_STATS support did not set
5492 * VLAN_STRIP_CAP properly.
5494 if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
5495 (BNXT_CHIP_P5_THOR(bp) &&
5496 !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
5497 bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
5498 bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
5499 if (bp->max_tpa_v2) {
5500 if (BNXT_CHIP_P5_THOR(bp))
5501 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
5503 bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5_SR2;
5506 mutex_unlock(&bp->hwrm_cmd_lock);
5510 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
5515 if (bp->flags & BNXT_FLAG_CHIP_P5)
5518 mutex_lock(&bp->hwrm_cmd_lock);
5519 for (i = 0; i < bp->rx_nr_rings; i++) {
5520 struct hwrm_ring_grp_alloc_input req = {0};
5521 struct hwrm_ring_grp_alloc_output *resp =
5522 bp->hwrm_cmd_resp_addr;
5523 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
5525 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
5527 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
5528 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
5529 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
5530 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
5532 rc = _hwrm_send_message(bp, &req, sizeof(req),
5537 bp->grp_info[grp_idx].fw_grp_id =
5538 le32_to_cpu(resp->ring_group_id);
5540 mutex_unlock(&bp->hwrm_cmd_lock);
5544 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
5547 struct hwrm_ring_grp_free_input req = {0};
5549 if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5))
5552 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
5554 mutex_lock(&bp->hwrm_cmd_lock);
5555 for (i = 0; i < bp->cp_nr_rings; i++) {
5556 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
5559 cpu_to_le32(bp->grp_info[i].fw_grp_id);
5561 _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5562 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
5564 mutex_unlock(&bp->hwrm_cmd_lock);
5567 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
5568 struct bnxt_ring_struct *ring,
5569 u32 ring_type, u32 map_index)
5571 int rc = 0, err = 0;
5572 struct hwrm_ring_alloc_input req = {0};
5573 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5574 struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
5575 struct bnxt_ring_grp_info *grp_info;
5578 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
5581 if (rmem->nr_pages > 1) {
5582 req.page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
5583 /* Page size is in log2 units */
5584 req.page_size = BNXT_PAGE_SHIFT;
5585 req.page_tbl_depth = 1;
5587 req.page_tbl_addr = cpu_to_le64(rmem->dma_arr[0]);
5590 /* Association of ring index with doorbell index and MSIX number */
5591 req.logical_id = cpu_to_le16(map_index);
5593 switch (ring_type) {
5594 case HWRM_RING_ALLOC_TX: {
5595 struct bnxt_tx_ring_info *txr;
5597 txr = container_of(ring, struct bnxt_tx_ring_info,
5599 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
5600 /* Association of transmit ring with completion ring */
5601 grp_info = &bp->grp_info[ring->grp_idx];
5602 req.cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
5603 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
5604 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5605 req.queue_id = cpu_to_le16(ring->queue_id);
5608 case HWRM_RING_ALLOC_RX:
5609 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5610 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
5611 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5614 /* Association of rx ring with stats context */
5615 grp_info = &bp->grp_info[ring->grp_idx];
5616 req.rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
5617 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5618 req.enables |= cpu_to_le32(
5619 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5620 if (NET_IP_ALIGN == 2)
5621 flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
5622 req.flags = cpu_to_le16(flags);
5625 case HWRM_RING_ALLOC_AGG:
5626 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5627 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
5628 /* Association of agg ring with rx ring */
5629 grp_info = &bp->grp_info[ring->grp_idx];
5630 req.rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
5631 req.rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
5632 req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
5633 req.enables |= cpu_to_le32(
5634 RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
5635 RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
5637 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
5639 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
5641 case HWRM_RING_ALLOC_CMPL:
5642 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
5643 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5644 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5645 /* Association of cp ring with nq */
5646 grp_info = &bp->grp_info[map_index];
5647 req.nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
5648 req.cq_handle = cpu_to_le64(ring->handle);
5649 req.enables |= cpu_to_le32(
5650 RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
5651 } else if (bp->flags & BNXT_FLAG_USING_MSIX) {
5652 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5655 case HWRM_RING_ALLOC_NQ:
5656 req.ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
5657 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
5658 if (bp->flags & BNXT_FLAG_USING_MSIX)
5659 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
5662 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
5667 mutex_lock(&bp->hwrm_cmd_lock);
5668 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5669 err = le16_to_cpu(resp->error_code);
5670 ring_id = le16_to_cpu(resp->ring_id);
5671 mutex_unlock(&bp->hwrm_cmd_lock);
5674 netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
5675 ring_type, rc, err);
5678 ring->fw_ring_id = ring_id;
5682 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
5687 struct hwrm_func_cfg_input req = {0};
5689 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
5690 req.fid = cpu_to_le16(0xffff);
5691 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5692 req.async_event_cr = cpu_to_le16(idx);
5693 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5695 struct hwrm_func_vf_cfg_input req = {0};
5697 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
5699 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
5700 req.async_event_cr = cpu_to_le16(idx);
5701 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5706 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
5707 u32 map_idx, u32 xid)
5709 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5711 db->doorbell = bp->bar1 + DB_PF_OFFSET_P5;
5713 db->doorbell = bp->bar1 + DB_VF_OFFSET_P5;
5714 switch (ring_type) {
5715 case HWRM_RING_ALLOC_TX:
5716 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
5718 case HWRM_RING_ALLOC_RX:
5719 case HWRM_RING_ALLOC_AGG:
5720 db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
5722 case HWRM_RING_ALLOC_CMPL:
5723 db->db_key64 = DBR_PATH_L2;
5725 case HWRM_RING_ALLOC_NQ:
5726 db->db_key64 = DBR_PATH_L2;
5729 db->db_key64 |= (u64)xid << DBR_XID_SFT;
5731 db->doorbell = bp->bar1 + map_idx * 0x80;
5732 switch (ring_type) {
5733 case HWRM_RING_ALLOC_TX:
5734 db->db_key32 = DB_KEY_TX;
5736 case HWRM_RING_ALLOC_RX:
5737 case HWRM_RING_ALLOC_AGG:
5738 db->db_key32 = DB_KEY_RX;
5740 case HWRM_RING_ALLOC_CMPL:
5741 db->db_key32 = DB_KEY_CP;
5747 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
5749 bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
5753 if (bp->flags & BNXT_FLAG_CHIP_P5)
5754 type = HWRM_RING_ALLOC_NQ;
5756 type = HWRM_RING_ALLOC_CMPL;
5757 for (i = 0; i < bp->cp_nr_rings; i++) {
5758 struct bnxt_napi *bnapi = bp->bnapi[i];
5759 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5760 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5761 u32 map_idx = ring->map_idx;
5762 unsigned int vector;
5764 vector = bp->irq_tbl[map_idx].vector;
5765 disable_irq_nosync(vector);
5766 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5771 bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
5772 bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5774 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
5777 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
5779 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
5783 type = HWRM_RING_ALLOC_TX;
5784 for (i = 0; i < bp->tx_nr_rings; i++) {
5785 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5786 struct bnxt_ring_struct *ring;
5789 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5790 struct bnxt_napi *bnapi = txr->bnapi;
5791 struct bnxt_cp_ring_info *cpr, *cpr2;
5792 u32 type2 = HWRM_RING_ALLOC_CMPL;
5794 cpr = &bnapi->cp_ring;
5795 cpr2 = cpr->cp_ring_arr[BNXT_TX_HDL];
5796 ring = &cpr2->cp_ring_struct;
5797 ring->handle = BNXT_TX_HDL;
5798 map_idx = bnapi->index;
5799 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5802 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5804 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5806 ring = &txr->tx_ring_struct;
5808 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5811 bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
5814 type = HWRM_RING_ALLOC_RX;
5815 for (i = 0; i < bp->rx_nr_rings; i++) {
5816 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5817 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5818 struct bnxt_napi *bnapi = rxr->bnapi;
5819 u32 map_idx = bnapi->index;
5821 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5824 bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
5825 /* If we have agg rings, post agg buffers first. */
5827 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5828 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
5829 if (bp->flags & BNXT_FLAG_CHIP_P5) {
5830 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5831 u32 type2 = HWRM_RING_ALLOC_CMPL;
5832 struct bnxt_cp_ring_info *cpr2;
5834 cpr2 = cpr->cp_ring_arr[BNXT_RX_HDL];
5835 ring = &cpr2->cp_ring_struct;
5836 ring->handle = BNXT_RX_HDL;
5837 rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
5840 bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
5842 bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
5847 type = HWRM_RING_ALLOC_AGG;
5848 for (i = 0; i < bp->rx_nr_rings; i++) {
5849 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5850 struct bnxt_ring_struct *ring =
5851 &rxr->rx_agg_ring_struct;
5852 u32 grp_idx = ring->grp_idx;
5853 u32 map_idx = grp_idx + bp->rx_nr_rings;
5855 rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
5859 bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
5861 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
5862 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
5863 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
5870 static int hwrm_ring_free_send_msg(struct bnxt *bp,
5871 struct bnxt_ring_struct *ring,
5872 u32 ring_type, int cmpl_ring_id)
5875 struct hwrm_ring_free_input req = {0};
5876 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
5879 if (BNXT_NO_FW_ACCESS(bp))
5882 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
5883 req.ring_type = ring_type;
5884 req.ring_id = cpu_to_le16(ring->fw_ring_id);
5886 mutex_lock(&bp->hwrm_cmd_lock);
5887 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5888 error_code = le16_to_cpu(resp->error_code);
5889 mutex_unlock(&bp->hwrm_cmd_lock);
5891 if (rc || error_code) {
5892 netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
5893 ring_type, rc, error_code);
5899 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
5907 for (i = 0; i < bp->tx_nr_rings; i++) {
5908 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5909 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
5911 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5912 u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
5914 hwrm_ring_free_send_msg(bp, ring,
5915 RING_FREE_REQ_RING_TYPE_TX,
5916 close_path ? cmpl_ring_id :
5917 INVALID_HW_RING_ID);
5918 ring->fw_ring_id = INVALID_HW_RING_ID;
5922 for (i = 0; i < bp->rx_nr_rings; i++) {
5923 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5924 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
5925 u32 grp_idx = rxr->bnapi->index;
5927 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5928 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5930 hwrm_ring_free_send_msg(bp, ring,
5931 RING_FREE_REQ_RING_TYPE_RX,
5932 close_path ? cmpl_ring_id :
5933 INVALID_HW_RING_ID);
5934 ring->fw_ring_id = INVALID_HW_RING_ID;
5935 bp->grp_info[grp_idx].rx_fw_ring_id =
5940 if (bp->flags & BNXT_FLAG_CHIP_P5)
5941 type = RING_FREE_REQ_RING_TYPE_RX_AGG;
5943 type = RING_FREE_REQ_RING_TYPE_RX;
5944 for (i = 0; i < bp->rx_nr_rings; i++) {
5945 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5946 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
5947 u32 grp_idx = rxr->bnapi->index;
5949 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5950 u32 cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
5952 hwrm_ring_free_send_msg(bp, ring, type,
5953 close_path ? cmpl_ring_id :
5954 INVALID_HW_RING_ID);
5955 ring->fw_ring_id = INVALID_HW_RING_ID;
5956 bp->grp_info[grp_idx].agg_fw_ring_id =
5961 /* The completion rings are about to be freed. After that the
5962 * IRQ doorbell will not work anymore. So we need to disable
5965 bnxt_disable_int_sync(bp);
5967 if (bp->flags & BNXT_FLAG_CHIP_P5)
5968 type = RING_FREE_REQ_RING_TYPE_NQ;
5970 type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
5971 for (i = 0; i < bp->cp_nr_rings; i++) {
5972 struct bnxt_napi *bnapi = bp->bnapi[i];
5973 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5974 struct bnxt_ring_struct *ring;
5977 for (j = 0; j < 2; j++) {
5978 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
5981 ring = &cpr2->cp_ring_struct;
5982 if (ring->fw_ring_id == INVALID_HW_RING_ID)
5984 hwrm_ring_free_send_msg(bp, ring,
5985 RING_FREE_REQ_RING_TYPE_L2_CMPL,
5986 INVALID_HW_RING_ID);
5987 ring->fw_ring_id = INVALID_HW_RING_ID;
5990 ring = &cpr->cp_ring_struct;
5991 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
5992 hwrm_ring_free_send_msg(bp, ring, type,
5993 INVALID_HW_RING_ID);
5994 ring->fw_ring_id = INVALID_HW_RING_ID;
5995 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
6000 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
6003 static int bnxt_hwrm_get_rings(struct bnxt *bp)
6005 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6006 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6007 struct hwrm_func_qcfg_input req = {0};
6010 if (bp->hwrm_spec_code < 0x10601)
6013 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6014 req.fid = cpu_to_le16(0xffff);
6015 mutex_lock(&bp->hwrm_cmd_lock);
6016 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6018 mutex_unlock(&bp->hwrm_cmd_lock);
6022 hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6023 if (BNXT_NEW_RM(bp)) {
6026 hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
6027 hw_resc->resv_hw_ring_grps =
6028 le32_to_cpu(resp->alloc_hw_ring_grps);
6029 hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
6030 cp = le16_to_cpu(resp->alloc_cmpl_rings);
6031 stats = le16_to_cpu(resp->alloc_stat_ctx);
6032 hw_resc->resv_irqs = cp;
6033 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6034 int rx = hw_resc->resv_rx_rings;
6035 int tx = hw_resc->resv_tx_rings;
6037 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6039 if (cp < (rx + tx)) {
6040 bnxt_trim_rings(bp, &rx, &tx, cp, false);
6041 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6043 hw_resc->resv_rx_rings = rx;
6044 hw_resc->resv_tx_rings = tx;
6046 hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
6047 hw_resc->resv_hw_ring_grps = rx;
6049 hw_resc->resv_cp_rings = cp;
6050 hw_resc->resv_stat_ctxs = stats;
6052 mutex_unlock(&bp->hwrm_cmd_lock);
6056 /* Caller must hold bp->hwrm_cmd_lock */
6057 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
6059 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6060 struct hwrm_func_qcfg_input req = {0};
6063 if (bp->hwrm_spec_code < 0x10601)
6066 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6067 req.fid = cpu_to_le16(fid);
6068 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6070 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
6075 static bool bnxt_rfs_supported(struct bnxt *bp);
6078 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
6079 int tx_rings, int rx_rings, int ring_grps,
6080 int cp_rings, int stats, int vnics)
6084 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
6085 req->fid = cpu_to_le16(0xffff);
6086 enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6087 req->num_tx_rings = cpu_to_le16(tx_rings);
6088 if (BNXT_NEW_RM(bp)) {
6089 enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
6090 enables |= stats ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6091 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6092 enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
6093 enables |= tx_rings + ring_grps ?
6094 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6095 enables |= rx_rings ?
6096 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6098 enables |= cp_rings ?
6099 FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6100 enables |= ring_grps ?
6101 FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS |
6102 FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6104 enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
6106 req->num_rx_rings = cpu_to_le16(rx_rings);
6107 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6108 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6109 req->num_msix = cpu_to_le16(cp_rings);
6110 req->num_rsscos_ctxs =
6111 cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6113 req->num_cmpl_rings = cpu_to_le16(cp_rings);
6114 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6115 req->num_rsscos_ctxs = cpu_to_le16(1);
6116 if (!(bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
6117 bnxt_rfs_supported(bp))
6118 req->num_rsscos_ctxs =
6119 cpu_to_le16(ring_grps + 1);
6121 req->num_stat_ctxs = cpu_to_le16(stats);
6122 req->num_vnics = cpu_to_le16(vnics);
6124 req->enables = cpu_to_le32(enables);
6128 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
6129 struct hwrm_func_vf_cfg_input *req, int tx_rings,
6130 int rx_rings, int ring_grps, int cp_rings,
6131 int stats, int vnics)
6135 bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
6136 enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
6137 enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
6138 FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
6139 enables |= stats ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
6140 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6141 enables |= tx_rings + ring_grps ?
6142 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6144 enables |= cp_rings ?
6145 FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
6146 enables |= ring_grps ?
6147 FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
6149 enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
6150 enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
6152 req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
6153 req->num_tx_rings = cpu_to_le16(tx_rings);
6154 req->num_rx_rings = cpu_to_le16(rx_rings);
6155 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6156 req->num_cmpl_rings = cpu_to_le16(tx_rings + ring_grps);
6157 req->num_rsscos_ctxs = cpu_to_le16(DIV_ROUND_UP(ring_grps, 64));
6159 req->num_cmpl_rings = cpu_to_le16(cp_rings);
6160 req->num_hw_ring_grps = cpu_to_le16(ring_grps);
6161 req->num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
6163 req->num_stat_ctxs = cpu_to_le16(stats);
6164 req->num_vnics = cpu_to_le16(vnics);
6166 req->enables = cpu_to_le32(enables);
6170 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6171 int ring_grps, int cp_rings, int stats, int vnics)
6173 struct hwrm_func_cfg_input req = {0};
6176 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6177 cp_rings, stats, vnics);
6181 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6185 if (bp->hwrm_spec_code < 0x10601)
6186 bp->hw_resc.resv_tx_rings = tx_rings;
6188 return bnxt_hwrm_get_rings(bp);
6192 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6193 int ring_grps, int cp_rings, int stats, int vnics)
6195 struct hwrm_func_vf_cfg_input req = {0};
6198 if (!BNXT_NEW_RM(bp)) {
6199 bp->hw_resc.resv_tx_rings = tx_rings;
6203 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6204 cp_rings, stats, vnics);
6205 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6209 return bnxt_hwrm_get_rings(bp);
6212 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
6213 int cp, int stat, int vnic)
6216 return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, stat,
6219 return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, stat,
6223 int bnxt_nq_rings_in_use(struct bnxt *bp)
6225 int cp = bp->cp_nr_rings;
6226 int ulp_msix, ulp_base;
6228 ulp_msix = bnxt_get_ulp_msix_num(bp);
6230 ulp_base = bnxt_get_ulp_msix_base(bp);
6232 if ((ulp_base + ulp_msix) > cp)
6233 cp = ulp_base + ulp_msix;
6238 static int bnxt_cp_rings_in_use(struct bnxt *bp)
6242 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6243 return bnxt_nq_rings_in_use(bp);
6245 cp = bp->tx_nr_rings + bp->rx_nr_rings;
6249 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
6251 int ulp_stat = bnxt_get_ulp_stat_ctxs(bp);
6252 int cp = bp->cp_nr_rings;
6257 if (bnxt_nq_rings_in_use(bp) > cp + bnxt_get_ulp_msix_num(bp))
6258 return bnxt_get_ulp_msix_base(bp) + ulp_stat;
6260 return cp + ulp_stat;
6263 /* Check if a default RSS map needs to be setup. This function is only
6264 * used on older firmware that does not require reserving RX rings.
6266 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
6268 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6270 /* The RSS map is valid for RX rings set to resv_rx_rings */
6271 if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
6272 hw_resc->resv_rx_rings = bp->rx_nr_rings;
6273 if (!netif_is_rxfh_configured(bp->dev))
6274 bnxt_set_dflt_rss_indir_tbl(bp);
6278 static bool bnxt_need_reserve_rings(struct bnxt *bp)
6280 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6281 int cp = bnxt_cp_rings_in_use(bp);
6282 int nq = bnxt_nq_rings_in_use(bp);
6283 int rx = bp->rx_nr_rings, stat;
6284 int vnic = 1, grp = rx;
6286 if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
6287 bp->hwrm_spec_code >= 0x10601)
6290 /* Old firmware does not need RX ring reservations but we still
6291 * need to setup a default RSS map when needed. With new firmware
6292 * we go through RX ring reservations first and then set up the
6293 * RSS map for the successfully reserved RX rings when needed.
6295 if (!BNXT_NEW_RM(bp)) {
6296 bnxt_check_rss_tbl_no_rmgr(bp);
6299 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6301 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6303 stat = bnxt_get_func_stat_ctxs(bp);
6304 if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
6305 hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
6306 (hw_resc->resv_hw_ring_grps != grp &&
6307 !(bp->flags & BNXT_FLAG_CHIP_P5)))
6309 if ((bp->flags & BNXT_FLAG_CHIP_P5) && BNXT_PF(bp) &&
6310 hw_resc->resv_irqs != nq)
6315 static int __bnxt_reserve_rings(struct bnxt *bp)
6317 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
6318 int cp = bnxt_nq_rings_in_use(bp);
6319 int tx = bp->tx_nr_rings;
6320 int rx = bp->rx_nr_rings;
6321 int grp, rx_rings, rc;
6325 if (!bnxt_need_reserve_rings(bp))
6328 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6330 if ((bp->flags & BNXT_FLAG_RFS) && !(bp->flags & BNXT_FLAG_CHIP_P5))
6332 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6334 grp = bp->rx_nr_rings;
6335 stat = bnxt_get_func_stat_ctxs(bp);
6337 rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, stat, vnic);
6341 tx = hw_resc->resv_tx_rings;
6342 if (BNXT_NEW_RM(bp)) {
6343 rx = hw_resc->resv_rx_rings;
6344 cp = hw_resc->resv_irqs;
6345 grp = hw_resc->resv_hw_ring_grps;
6346 vnic = hw_resc->resv_vnics;
6347 stat = hw_resc->resv_stat_ctxs;
6351 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
6355 if (netif_running(bp->dev))
6358 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
6359 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
6360 bp->dev->hw_features &= ~NETIF_F_LRO;
6361 bp->dev->features &= ~NETIF_F_LRO;
6362 bnxt_set_ring_params(bp);
6365 rx_rings = min_t(int, rx_rings, grp);
6366 cp = min_t(int, cp, bp->cp_nr_rings);
6367 if (stat > bnxt_get_ulp_stat_ctxs(bp))
6368 stat -= bnxt_get_ulp_stat_ctxs(bp);
6369 cp = min_t(int, cp, stat);
6370 rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
6371 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6373 cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
6374 bp->tx_nr_rings = tx;
6376 /* If we cannot reserve all the RX rings, reset the RSS map only
6377 * if absolutely necessary
6379 if (rx_rings != bp->rx_nr_rings) {
6380 netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
6381 rx_rings, bp->rx_nr_rings);
6382 if ((bp->dev->priv_flags & IFF_RXFH_CONFIGURED) &&
6383 (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
6384 bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
6385 bnxt_get_max_rss_ring(bp) >= rx_rings)) {
6386 netdev_warn(bp->dev, "RSS table entries reverting to default\n");
6387 bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
6390 bp->rx_nr_rings = rx_rings;
6391 bp->cp_nr_rings = cp;
6393 if (!tx || !rx || !cp || !grp || !vnic || !stat)
6396 if (!netif_is_rxfh_configured(bp->dev))
6397 bnxt_set_dflt_rss_indir_tbl(bp);
6402 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6403 int ring_grps, int cp_rings, int stats,
6406 struct hwrm_func_vf_cfg_input req = {0};
6409 if (!BNXT_NEW_RM(bp))
6412 __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6413 cp_rings, stats, vnics);
6414 flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
6415 FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6416 FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6417 FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6418 FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
6419 FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
6420 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6421 flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6423 req.flags = cpu_to_le32(flags);
6424 return hwrm_send_message_silent(bp, &req, sizeof(req),
6428 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6429 int ring_grps, int cp_rings, int stats,
6432 struct hwrm_func_cfg_input req = {0};
6435 __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
6436 cp_rings, stats, vnics);
6437 flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
6438 if (BNXT_NEW_RM(bp)) {
6439 flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
6440 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
6441 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
6442 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
6443 if (bp->flags & BNXT_FLAG_CHIP_P5)
6444 flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
6445 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
6447 flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
6450 req.flags = cpu_to_le32(flags);
6451 return hwrm_send_message_silent(bp, &req, sizeof(req),
6455 static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
6456 int ring_grps, int cp_rings, int stats,
6459 if (bp->hwrm_spec_code < 0x10801)
6463 return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
6464 ring_grps, cp_rings, stats,
6467 return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
6468 cp_rings, stats, vnics);
6471 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
6473 struct hwrm_ring_aggint_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
6474 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6475 struct hwrm_ring_aggint_qcaps_input req = {0};
6478 coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
6479 coal_cap->num_cmpl_dma_aggr_max = 63;
6480 coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
6481 coal_cap->cmpl_aggr_dma_tmr_max = 65535;
6482 coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
6483 coal_cap->int_lat_tmr_min_max = 65535;
6484 coal_cap->int_lat_tmr_max_max = 65535;
6485 coal_cap->num_cmpl_aggr_int_max = 65535;
6486 coal_cap->timer_units = 80;
6488 if (bp->hwrm_spec_code < 0x10902)
6491 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_AGGINT_QCAPS, -1, -1);
6492 mutex_lock(&bp->hwrm_cmd_lock);
6493 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6495 coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
6496 coal_cap->nq_params = le32_to_cpu(resp->nq_params);
6497 coal_cap->num_cmpl_dma_aggr_max =
6498 le16_to_cpu(resp->num_cmpl_dma_aggr_max);
6499 coal_cap->num_cmpl_dma_aggr_during_int_max =
6500 le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
6501 coal_cap->cmpl_aggr_dma_tmr_max =
6502 le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
6503 coal_cap->cmpl_aggr_dma_tmr_during_int_max =
6504 le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
6505 coal_cap->int_lat_tmr_min_max =
6506 le16_to_cpu(resp->int_lat_tmr_min_max);
6507 coal_cap->int_lat_tmr_max_max =
6508 le16_to_cpu(resp->int_lat_tmr_max_max);
6509 coal_cap->num_cmpl_aggr_int_max =
6510 le16_to_cpu(resp->num_cmpl_aggr_int_max);
6511 coal_cap->timer_units = le16_to_cpu(resp->timer_units);
6513 mutex_unlock(&bp->hwrm_cmd_lock);
6516 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
6518 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6520 return usec * 1000 / coal_cap->timer_units;
6523 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
6524 struct bnxt_coal *hw_coal,
6525 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
6527 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6528 u32 cmpl_params = coal_cap->cmpl_params;
6529 u16 val, tmr, max, flags = 0;
6531 max = hw_coal->bufs_per_record * 128;
6532 if (hw_coal->budget)
6533 max = hw_coal->bufs_per_record * hw_coal->budget;
6534 max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
6536 val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
6537 req->num_cmpl_aggr_int = cpu_to_le16(val);
6539 val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
6540 req->num_cmpl_dma_aggr = cpu_to_le16(val);
6542 val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
6543 coal_cap->num_cmpl_dma_aggr_during_int_max);
6544 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
6546 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
6547 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
6548 req->int_lat_tmr_max = cpu_to_le16(tmr);
6550 /* min timer set to 1/2 of interrupt timer */
6551 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
6553 val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
6554 req->int_lat_tmr_min = cpu_to_le16(val);
6555 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6558 /* buf timer set to 1/4 of interrupt timer */
6559 val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
6560 req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
6563 RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
6564 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
6565 val = clamp_t(u16, tmr, 1,
6566 coal_cap->cmpl_aggr_dma_tmr_during_int_max);
6567 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
6569 cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
6572 if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
6573 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
6574 if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
6575 hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
6576 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
6577 req->flags = cpu_to_le16(flags);
6578 req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
6581 /* Caller holds bp->hwrm_cmd_lock */
6582 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
6583 struct bnxt_coal *hw_coal)
6585 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req = {0};
6586 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6587 struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
6588 u32 nq_params = coal_cap->nq_params;
6591 if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
6594 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS,
6596 req.ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
6598 cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
6600 tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
6601 tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
6602 req.int_lat_tmr_min = cpu_to_le16(tmr);
6603 req.enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
6604 return _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6607 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
6609 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
6610 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6611 struct bnxt_coal coal;
6613 /* Tick values in micro seconds.
6614 * 1 coal_buf x bufs_per_record = 1 completion record.
6616 memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
6618 coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
6619 coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
6621 if (!bnapi->rx_ring)
6624 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6625 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6627 bnxt_hwrm_set_coal_params(bp, &coal, &req_rx);
6629 req_rx.ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
6631 return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
6635 int bnxt_hwrm_set_coal(struct bnxt *bp)
6638 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
6641 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
6642 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6643 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
6644 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
6646 bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, &req_rx);
6647 bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, &req_tx);
6649 mutex_lock(&bp->hwrm_cmd_lock);
6650 for (i = 0; i < bp->cp_nr_rings; i++) {
6651 struct bnxt_napi *bnapi = bp->bnapi[i];
6652 struct bnxt_coal *hw_coal;
6656 if (!bnapi->rx_ring) {
6657 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6660 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
6662 req->ring_id = cpu_to_le16(ring_id);
6664 rc = _hwrm_send_message(bp, req, sizeof(*req),
6669 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
6672 if (bnapi->rx_ring && bnapi->tx_ring) {
6674 ring_id = bnxt_cp_ring_for_tx(bp, bnapi->tx_ring);
6675 req->ring_id = cpu_to_le16(ring_id);
6676 rc = _hwrm_send_message(bp, req, sizeof(*req),
6682 hw_coal = &bp->rx_coal;
6684 hw_coal = &bp->tx_coal;
6685 __bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
6687 mutex_unlock(&bp->hwrm_cmd_lock);
6691 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
6693 struct hwrm_stat_ctx_clr_stats_input req0 = {0};
6694 struct hwrm_stat_ctx_free_input req = {0};
6700 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6703 bnxt_hwrm_cmd_hdr_init(bp, &req0, HWRM_STAT_CTX_CLR_STATS, -1, -1);
6704 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
6706 mutex_lock(&bp->hwrm_cmd_lock);
6707 for (i = 0; i < bp->cp_nr_rings; i++) {
6708 struct bnxt_napi *bnapi = bp->bnapi[i];
6709 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6711 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
6712 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
6713 if (BNXT_FW_MAJ(bp) <= 20) {
6714 req0.stat_ctx_id = req.stat_ctx_id;
6715 _hwrm_send_message(bp, &req0, sizeof(req0),
6718 _hwrm_send_message(bp, &req, sizeof(req),
6721 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
6724 mutex_unlock(&bp->hwrm_cmd_lock);
6727 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
6730 struct hwrm_stat_ctx_alloc_input req = {0};
6731 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
6733 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6736 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
6738 req.stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
6739 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
6741 mutex_lock(&bp->hwrm_cmd_lock);
6742 for (i = 0; i < bp->cp_nr_rings; i++) {
6743 struct bnxt_napi *bnapi = bp->bnapi[i];
6744 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6746 req.stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
6748 rc = _hwrm_send_message(bp, &req, sizeof(req),
6753 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
6755 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
6757 mutex_unlock(&bp->hwrm_cmd_lock);
6761 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
6763 struct hwrm_func_qcfg_input req = {0};
6764 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
6765 u32 min_db_offset = 0;
6769 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
6770 req.fid = cpu_to_le16(0xffff);
6771 mutex_lock(&bp->hwrm_cmd_lock);
6772 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6774 goto func_qcfg_exit;
6776 #ifdef CONFIG_BNXT_SRIOV
6778 struct bnxt_vf_info *vf = &bp->vf;
6780 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
6782 bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
6785 flags = le16_to_cpu(resp->flags);
6786 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
6787 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
6788 bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
6789 if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
6790 bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
6792 if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
6793 bp->flags |= BNXT_FLAG_MULTI_HOST;
6794 if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
6795 bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
6797 switch (resp->port_partition_type) {
6798 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
6799 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
6800 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
6801 bp->port_partition_type = resp->port_partition_type;
6804 if (bp->hwrm_spec_code < 0x10707 ||
6805 resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
6806 bp->br_mode = BRIDGE_MODE_VEB;
6807 else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
6808 bp->br_mode = BRIDGE_MODE_VEPA;
6810 bp->br_mode = BRIDGE_MODE_UNDEF;
6812 bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
6814 bp->max_mtu = BNXT_MAX_MTU;
6817 goto func_qcfg_exit;
6819 if (bp->flags & BNXT_FLAG_CHIP_P5) {
6821 min_db_offset = DB_PF_OFFSET_P5;
6823 min_db_offset = DB_VF_OFFSET_P5;
6825 bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
6827 if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
6828 bp->db_size <= min_db_offset)
6829 bp->db_size = pci_resource_len(bp->pdev, 2);
6832 mutex_unlock(&bp->hwrm_cmd_lock);
6836 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
6838 struct hwrm_func_backing_store_qcaps_input req = {0};
6839 struct hwrm_func_backing_store_qcaps_output *resp =
6840 bp->hwrm_cmd_resp_addr;
6843 if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) || bp->ctx)
6846 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_QCAPS, -1, -1);
6847 mutex_lock(&bp->hwrm_cmd_lock);
6848 rc = _hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
6850 struct bnxt_ctx_pg_info *ctx_pg;
6851 struct bnxt_ctx_mem_info *ctx;
6854 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
6859 ctx->qp_max_entries = le32_to_cpu(resp->qp_max_entries);
6860 ctx->qp_min_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
6861 ctx->qp_max_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
6862 ctx->qp_entry_size = le16_to_cpu(resp->qp_entry_size);
6863 ctx->srq_max_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
6864 ctx->srq_max_entries = le32_to_cpu(resp->srq_max_entries);
6865 ctx->srq_entry_size = le16_to_cpu(resp->srq_entry_size);
6866 ctx->cq_max_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
6867 ctx->cq_max_entries = le32_to_cpu(resp->cq_max_entries);
6868 ctx->cq_entry_size = le16_to_cpu(resp->cq_entry_size);
6869 ctx->vnic_max_vnic_entries =
6870 le16_to_cpu(resp->vnic_max_vnic_entries);
6871 ctx->vnic_max_ring_table_entries =
6872 le16_to_cpu(resp->vnic_max_ring_table_entries);
6873 ctx->vnic_entry_size = le16_to_cpu(resp->vnic_entry_size);
6874 ctx->stat_max_entries = le32_to_cpu(resp->stat_max_entries);
6875 ctx->stat_entry_size = le16_to_cpu(resp->stat_entry_size);
6876 ctx->tqm_entry_size = le16_to_cpu(resp->tqm_entry_size);
6877 ctx->tqm_min_entries_per_ring =
6878 le32_to_cpu(resp->tqm_min_entries_per_ring);
6879 ctx->tqm_max_entries_per_ring =
6880 le32_to_cpu(resp->tqm_max_entries_per_ring);
6881 ctx->tqm_entries_multiple = resp->tqm_entries_multiple;
6882 if (!ctx->tqm_entries_multiple)
6883 ctx->tqm_entries_multiple = 1;
6884 ctx->mrav_max_entries = le32_to_cpu(resp->mrav_max_entries);
6885 ctx->mrav_entry_size = le16_to_cpu(resp->mrav_entry_size);
6886 ctx->mrav_num_entries_units =
6887 le16_to_cpu(resp->mrav_num_entries_units);
6888 ctx->tim_entry_size = le16_to_cpu(resp->tim_entry_size);
6889 ctx->tim_max_entries = le32_to_cpu(resp->tim_max_entries);
6890 ctx->ctx_kind_initializer = resp->ctx_kind_initializer;
6891 ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
6892 if (!ctx->tqm_fp_rings_count)
6893 ctx->tqm_fp_rings_count = bp->max_q;
6894 else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
6895 ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
6897 tqm_rings = ctx->tqm_fp_rings_count + BNXT_MAX_TQM_SP_RINGS;
6898 ctx_pg = kcalloc(tqm_rings, sizeof(*ctx_pg), GFP_KERNEL);
6904 for (i = 0; i < tqm_rings; i++, ctx_pg++)
6905 ctx->tqm_mem[i] = ctx_pg;
6911 mutex_unlock(&bp->hwrm_cmd_lock);
6915 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
6918 BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
6919 if (rmem->depth >= 1) {
6920 if (rmem->depth == 2)
6924 *pg_dir = cpu_to_le64(rmem->pg_tbl_map);
6926 *pg_dir = cpu_to_le64(rmem->dma_arr[0]);
6930 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES \
6931 (FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP | \
6932 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ | \
6933 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ | \
6934 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC | \
6935 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
6937 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
6939 struct hwrm_func_backing_store_cfg_input req = {0};
6940 struct bnxt_ctx_mem_info *ctx = bp->ctx;
6941 struct bnxt_ctx_pg_info *ctx_pg;
6942 __le32 *num_entries;
6952 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BACKING_STORE_CFG, -1, -1);
6953 req.enables = cpu_to_le32(enables);
6955 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
6956 ctx_pg = &ctx->qp_mem;
6957 req.qp_num_entries = cpu_to_le32(ctx_pg->entries);
6958 req.qp_num_qp1_entries = cpu_to_le16(ctx->qp_min_qp1_entries);
6959 req.qp_num_l2_entries = cpu_to_le16(ctx->qp_max_l2_entries);
6960 req.qp_entry_size = cpu_to_le16(ctx->qp_entry_size);
6961 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6962 &req.qpc_pg_size_qpc_lvl,
6965 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
6966 ctx_pg = &ctx->srq_mem;
6967 req.srq_num_entries = cpu_to_le32(ctx_pg->entries);
6968 req.srq_num_l2_entries = cpu_to_le16(ctx->srq_max_l2_entries);
6969 req.srq_entry_size = cpu_to_le16(ctx->srq_entry_size);
6970 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6971 &req.srq_pg_size_srq_lvl,
6974 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
6975 ctx_pg = &ctx->cq_mem;
6976 req.cq_num_entries = cpu_to_le32(ctx_pg->entries);
6977 req.cq_num_l2_entries = cpu_to_le16(ctx->cq_max_l2_entries);
6978 req.cq_entry_size = cpu_to_le16(ctx->cq_entry_size);
6979 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, &req.cq_pg_size_cq_lvl,
6982 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
6983 ctx_pg = &ctx->vnic_mem;
6984 req.vnic_num_vnic_entries =
6985 cpu_to_le16(ctx->vnic_max_vnic_entries);
6986 req.vnic_num_ring_table_entries =
6987 cpu_to_le16(ctx->vnic_max_ring_table_entries);
6988 req.vnic_entry_size = cpu_to_le16(ctx->vnic_entry_size);
6989 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6990 &req.vnic_pg_size_vnic_lvl,
6991 &req.vnic_page_dir);
6993 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
6994 ctx_pg = &ctx->stat_mem;
6995 req.stat_num_entries = cpu_to_le32(ctx->stat_max_entries);
6996 req.stat_entry_size = cpu_to_le16(ctx->stat_entry_size);
6997 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
6998 &req.stat_pg_size_stat_lvl,
6999 &req.stat_page_dir);
7001 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
7002 ctx_pg = &ctx->mrav_mem;
7003 req.mrav_num_entries = cpu_to_le32(ctx_pg->entries);
7004 if (ctx->mrav_num_entries_units)
7006 FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
7007 req.mrav_entry_size = cpu_to_le16(ctx->mrav_entry_size);
7008 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7009 &req.mrav_pg_size_mrav_lvl,
7010 &req.mrav_page_dir);
7012 if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
7013 ctx_pg = &ctx->tim_mem;
7014 req.tim_num_entries = cpu_to_le32(ctx_pg->entries);
7015 req.tim_entry_size = cpu_to_le16(ctx->tim_entry_size);
7016 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
7017 &req.tim_pg_size_tim_lvl,
7020 for (i = 0, num_entries = &req.tqm_sp_num_entries,
7021 pg_attr = &req.tqm_sp_pg_size_tqm_sp_lvl,
7022 pg_dir = &req.tqm_sp_page_dir,
7023 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP;
7024 i < BNXT_MAX_TQM_RINGS;
7025 i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
7026 if (!(enables & ena))
7029 req.tqm_entry_size = cpu_to_le16(ctx->tqm_entry_size);
7030 ctx_pg = ctx->tqm_mem[i];
7031 *num_entries = cpu_to_le32(ctx_pg->entries);
7032 bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
7034 req.flags = cpu_to_le32(flags);
7035 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7038 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
7039 struct bnxt_ctx_pg_info *ctx_pg)
7041 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7043 rmem->page_size = BNXT_PAGE_SIZE;
7044 rmem->pg_arr = ctx_pg->ctx_pg_arr;
7045 rmem->dma_arr = ctx_pg->ctx_dma_arr;
7046 rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
7047 if (rmem->depth >= 1)
7048 rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
7049 return bnxt_alloc_ring(bp, rmem);
7052 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
7053 struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
7054 u8 depth, bool use_init_val)
7056 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7062 ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7063 if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
7064 ctx_pg->nr_pages = 0;
7067 if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
7071 ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
7073 if (!ctx_pg->ctx_pg_tbl)
7075 nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
7076 rmem->nr_pages = nr_tbls;
7077 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7080 for (i = 0; i < nr_tbls; i++) {
7081 struct bnxt_ctx_pg_info *pg_tbl;
7083 pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
7086 ctx_pg->ctx_pg_tbl[i] = pg_tbl;
7087 rmem = &pg_tbl->ring_mem;
7088 rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
7089 rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
7091 rmem->nr_pages = MAX_CTX_PAGES;
7093 rmem->init_val = bp->ctx->ctx_kind_initializer;
7094 if (i == (nr_tbls - 1)) {
7095 int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
7098 rmem->nr_pages = rem;
7100 rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
7105 rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
7106 if (rmem->nr_pages > 1 || depth)
7109 rmem->init_val = bp->ctx->ctx_kind_initializer;
7110 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
7115 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
7116 struct bnxt_ctx_pg_info *ctx_pg)
7118 struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
7120 if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
7121 ctx_pg->ctx_pg_tbl) {
7122 int i, nr_tbls = rmem->nr_pages;
7124 for (i = 0; i < nr_tbls; i++) {
7125 struct bnxt_ctx_pg_info *pg_tbl;
7126 struct bnxt_ring_mem_info *rmem2;
7128 pg_tbl = ctx_pg->ctx_pg_tbl[i];
7131 rmem2 = &pg_tbl->ring_mem;
7132 bnxt_free_ring(bp, rmem2);
7133 ctx_pg->ctx_pg_arr[i] = NULL;
7135 ctx_pg->ctx_pg_tbl[i] = NULL;
7137 kfree(ctx_pg->ctx_pg_tbl);
7138 ctx_pg->ctx_pg_tbl = NULL;
7140 bnxt_free_ring(bp, rmem);
7141 ctx_pg->nr_pages = 0;
7144 static void bnxt_free_ctx_mem(struct bnxt *bp)
7146 struct bnxt_ctx_mem_info *ctx = bp->ctx;
7152 if (ctx->tqm_mem[0]) {
7153 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
7154 bnxt_free_ctx_pg_tbls(bp, ctx->tqm_mem[i]);
7155 kfree(ctx->tqm_mem[0]);
7156 ctx->tqm_mem[0] = NULL;
7159 bnxt_free_ctx_pg_tbls(bp, &ctx->tim_mem);
7160 bnxt_free_ctx_pg_tbls(bp, &ctx->mrav_mem);
7161 bnxt_free_ctx_pg_tbls(bp, &ctx->stat_mem);
7162 bnxt_free_ctx_pg_tbls(bp, &ctx->vnic_mem);
7163 bnxt_free_ctx_pg_tbls(bp, &ctx->cq_mem);
7164 bnxt_free_ctx_pg_tbls(bp, &ctx->srq_mem);
7165 bnxt_free_ctx_pg_tbls(bp, &ctx->qp_mem);
7166 ctx->flags &= ~BNXT_CTX_FLAG_INITED;
7169 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
7171 struct bnxt_ctx_pg_info *ctx_pg;
7172 struct bnxt_ctx_mem_info *ctx;
7173 u32 mem_size, ena, entries;
7174 u32 entries_sp, min;
7181 rc = bnxt_hwrm_func_backing_store_qcaps(bp);
7183 netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
7188 if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
7191 if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
7197 ctx_pg = &ctx->qp_mem;
7198 ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries +
7200 mem_size = ctx->qp_entry_size * ctx_pg->entries;
7201 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7205 ctx_pg = &ctx->srq_mem;
7206 ctx_pg->entries = ctx->srq_max_l2_entries + extra_srqs;
7207 mem_size = ctx->srq_entry_size * ctx_pg->entries;
7208 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7212 ctx_pg = &ctx->cq_mem;
7213 ctx_pg->entries = ctx->cq_max_l2_entries + extra_qps * 2;
7214 mem_size = ctx->cq_entry_size * ctx_pg->entries;
7215 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, pg_lvl, true);
7219 ctx_pg = &ctx->vnic_mem;
7220 ctx_pg->entries = ctx->vnic_max_vnic_entries +
7221 ctx->vnic_max_ring_table_entries;
7222 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
7223 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7227 ctx_pg = &ctx->stat_mem;
7228 ctx_pg->entries = ctx->stat_max_entries;
7229 mem_size = ctx->stat_entry_size * ctx_pg->entries;
7230 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, true);
7235 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
7238 ctx_pg = &ctx->mrav_mem;
7239 /* 128K extra is needed to accommodate static AH context
7240 * allocation by f/w.
7242 num_mr = 1024 * 256;
7243 num_ah = 1024 * 128;
7244 ctx_pg->entries = num_mr + num_ah;
7245 mem_size = ctx->mrav_entry_size * ctx_pg->entries;
7246 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 2, true);
7249 ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
7250 if (ctx->mrav_num_entries_units)
7252 ((num_mr / ctx->mrav_num_entries_units) << 16) |
7253 (num_ah / ctx->mrav_num_entries_units);
7255 ctx_pg = &ctx->tim_mem;
7256 ctx_pg->entries = ctx->qp_mem.entries;
7257 mem_size = ctx->tim_entry_size * ctx_pg->entries;
7258 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7261 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
7264 min = ctx->tqm_min_entries_per_ring;
7265 entries_sp = ctx->vnic_max_vnic_entries + ctx->qp_max_l2_entries +
7266 2 * (extra_qps + ctx->qp_min_qp1_entries) + min;
7267 entries_sp = roundup(entries_sp, ctx->tqm_entries_multiple);
7268 entries = ctx->qp_max_l2_entries + 2 * (extra_qps + ctx->qp_min_qp1_entries);
7269 entries = roundup(entries, ctx->tqm_entries_multiple);
7270 entries = clamp_t(u32, entries, min, ctx->tqm_max_entries_per_ring);
7271 for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
7272 ctx_pg = ctx->tqm_mem[i];
7273 ctx_pg->entries = i ? entries : entries_sp;
7274 mem_size = ctx->tqm_entry_size * ctx_pg->entries;
7275 rc = bnxt_alloc_ctx_pg_tbls(bp, ctx_pg, mem_size, 1, false);
7278 ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
7280 ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
7281 rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
7283 netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
7287 ctx->flags |= BNXT_CTX_FLAG_INITED;
7291 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
7293 struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7294 struct hwrm_func_resource_qcaps_input req = {0};
7295 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7298 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
7299 req.fid = cpu_to_le16(0xffff);
7301 mutex_lock(&bp->hwrm_cmd_lock);
7302 rc = _hwrm_send_message_silent(bp, &req, sizeof(req),
7305 goto hwrm_func_resc_qcaps_exit;
7307 hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
7309 goto hwrm_func_resc_qcaps_exit;
7311 hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
7312 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7313 hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
7314 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7315 hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
7316 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7317 hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
7318 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7319 hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
7320 hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
7321 hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
7322 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7323 hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
7324 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7325 hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
7326 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7328 if (bp->flags & BNXT_FLAG_CHIP_P5) {
7329 u16 max_msix = le16_to_cpu(resp->max_msix);
7331 hw_resc->max_nqs = max_msix;
7332 hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
7336 struct bnxt_pf_info *pf = &bp->pf;
7338 pf->vf_resv_strategy =
7339 le16_to_cpu(resp->vf_reservation_strategy);
7340 if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
7341 pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
7343 hwrm_func_resc_qcaps_exit:
7344 mutex_unlock(&bp->hwrm_cmd_lock);
7348 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
7351 struct hwrm_func_qcaps_input req = {0};
7352 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
7353 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7354 u32 flags, flags_ext;
7356 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
7357 req.fid = cpu_to_le16(0xffff);
7359 mutex_lock(&bp->hwrm_cmd_lock);
7360 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7362 goto hwrm_func_qcaps_exit;
7364 flags = le32_to_cpu(resp->flags);
7365 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
7366 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
7367 if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
7368 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
7369 if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
7370 bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
7371 if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
7372 bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
7373 if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
7374 bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
7375 if (flags & FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
7376 bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
7377 if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
7378 bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
7379 if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
7380 bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
7382 flags_ext = le32_to_cpu(resp->flags_ext);
7383 if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
7384 bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
7386 bp->tx_push_thresh = 0;
7387 if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
7388 BNXT_FW_MAJ(bp) > 217)
7389 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
7391 hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
7392 hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
7393 hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
7394 hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
7395 hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
7396 if (!hw_resc->max_hw_ring_grps)
7397 hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
7398 hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
7399 hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
7400 hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
7403 struct bnxt_pf_info *pf = &bp->pf;
7405 pf->fw_fid = le16_to_cpu(resp->fid);
7406 pf->port_id = le16_to_cpu(resp->port_id);
7407 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
7408 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
7409 pf->max_vfs = le16_to_cpu(resp->max_vfs);
7410 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
7411 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
7412 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
7413 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
7414 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
7415 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
7416 bp->flags &= ~BNXT_FLAG_WOL_CAP;
7417 if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
7418 bp->flags |= BNXT_FLAG_WOL_CAP;
7420 #ifdef CONFIG_BNXT_SRIOV
7421 struct bnxt_vf_info *vf = &bp->vf;
7423 vf->fw_fid = le16_to_cpu(resp->fid);
7424 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
7428 hwrm_func_qcaps_exit:
7429 mutex_unlock(&bp->hwrm_cmd_lock);
7433 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
7435 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
7439 rc = __bnxt_hwrm_func_qcaps(bp);
7442 rc = bnxt_hwrm_queue_qportcfg(bp);
7444 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
7447 if (bp->hwrm_spec_code >= 0x10803) {
7448 rc = bnxt_alloc_ctx_mem(bp);
7451 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
7453 bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
7458 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
7460 struct hwrm_cfa_adv_flow_mgnt_qcaps_input req = {0};
7461 struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
7465 if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
7468 resp = bp->hwrm_cmd_resp_addr;
7469 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS, -1, -1);
7471 mutex_lock(&bp->hwrm_cmd_lock);
7472 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7474 goto hwrm_cfa_adv_qcaps_exit;
7476 flags = le32_to_cpu(resp->flags);
7478 CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
7479 bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
7481 hwrm_cfa_adv_qcaps_exit:
7482 mutex_unlock(&bp->hwrm_cmd_lock);
7486 static int __bnxt_alloc_fw_health(struct bnxt *bp)
7491 bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
7498 static int bnxt_alloc_fw_health(struct bnxt *bp)
7502 if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
7503 !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7506 rc = __bnxt_alloc_fw_health(bp);
7508 bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
7509 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7516 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
7518 writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
7519 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
7520 BNXT_FW_HEALTH_WIN_MAP_OFF);
7523 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
7530 __bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
7531 hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
7533 sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
7534 if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
7536 bp->fw_health->status_reliable = false;
7540 if (__bnxt_alloc_fw_health(bp)) {
7541 netdev_warn(bp->dev, "no memory for firmware status checks\n");
7545 status_loc = readl(hs + offsetof(struct hcomm_status, fw_status_loc));
7546 bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
7547 reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
7548 if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
7549 __bnxt_map_fw_health_reg(bp, status_loc);
7550 bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
7551 BNXT_FW_HEALTH_WIN_OFF(status_loc);
7554 bp->fw_health->status_reliable = true;
7557 static int bnxt_map_fw_health_regs(struct bnxt *bp)
7559 struct bnxt_fw_health *fw_health = bp->fw_health;
7560 u32 reg_base = 0xffffffff;
7563 /* Only pre-map the monitoring GRC registers using window 3 */
7564 for (i = 0; i < 4; i++) {
7565 u32 reg = fw_health->regs[i];
7567 if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
7569 if (reg_base == 0xffffffff)
7570 reg_base = reg & BNXT_GRC_BASE_MASK;
7571 if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
7573 fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
7575 if (reg_base == 0xffffffff)
7578 __bnxt_map_fw_health_reg(bp, reg_base);
7582 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
7584 struct hwrm_error_recovery_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
7585 struct bnxt_fw_health *fw_health = bp->fw_health;
7586 struct hwrm_error_recovery_qcfg_input req = {0};
7589 if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
7592 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_ERROR_RECOVERY_QCFG, -1, -1);
7593 mutex_lock(&bp->hwrm_cmd_lock);
7594 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7596 goto err_recovery_out;
7597 fw_health->flags = le32_to_cpu(resp->flags);
7598 if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
7599 !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
7601 goto err_recovery_out;
7603 fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
7604 fw_health->master_func_wait_dsecs =
7605 le32_to_cpu(resp->master_func_wait_period);
7606 fw_health->normal_func_wait_dsecs =
7607 le32_to_cpu(resp->normal_func_wait_period);
7608 fw_health->post_reset_wait_dsecs =
7609 le32_to_cpu(resp->master_func_wait_period_after_reset);
7610 fw_health->post_reset_max_wait_dsecs =
7611 le32_to_cpu(resp->max_bailout_time_after_reset);
7612 fw_health->regs[BNXT_FW_HEALTH_REG] =
7613 le32_to_cpu(resp->fw_health_status_reg);
7614 fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
7615 le32_to_cpu(resp->fw_heartbeat_reg);
7616 fw_health->regs[BNXT_FW_RESET_CNT_REG] =
7617 le32_to_cpu(resp->fw_reset_cnt_reg);
7618 fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
7619 le32_to_cpu(resp->reset_inprogress_reg);
7620 fw_health->fw_reset_inprog_reg_mask =
7621 le32_to_cpu(resp->reset_inprogress_reg_mask);
7622 fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
7623 if (fw_health->fw_reset_seq_cnt >= 16) {
7625 goto err_recovery_out;
7627 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
7628 fw_health->fw_reset_seq_regs[i] =
7629 le32_to_cpu(resp->reset_reg[i]);
7630 fw_health->fw_reset_seq_vals[i] =
7631 le32_to_cpu(resp->reset_reg_val[i]);
7632 fw_health->fw_reset_seq_delay_msec[i] =
7633 resp->delay_after_reset[i];
7636 mutex_unlock(&bp->hwrm_cmd_lock);
7638 rc = bnxt_map_fw_health_regs(bp);
7640 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
7644 static int bnxt_hwrm_func_reset(struct bnxt *bp)
7646 struct hwrm_func_reset_input req = {0};
7648 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
7651 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
7654 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
7656 struct hwrm_nvm_get_dev_info_output nvm_info;
7658 if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
7659 snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
7660 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
7661 nvm_info.nvm_cfg_ver_upd);
7664 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
7667 struct hwrm_queue_qportcfg_input req = {0};
7668 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
7672 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
7674 mutex_lock(&bp->hwrm_cmd_lock);
7675 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7679 if (!resp->max_configurable_queues) {
7683 bp->max_tc = resp->max_configurable_queues;
7684 bp->max_lltc = resp->max_configurable_lossless_queues;
7685 if (bp->max_tc > BNXT_MAX_QUEUE)
7686 bp->max_tc = BNXT_MAX_QUEUE;
7688 no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
7689 qptr = &resp->queue_id0;
7690 for (i = 0, j = 0; i < bp->max_tc; i++) {
7691 bp->q_info[j].queue_id = *qptr;
7692 bp->q_ids[i] = *qptr++;
7693 bp->q_info[j].queue_profile = *qptr++;
7694 bp->tc_to_qidx[j] = j;
7695 if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
7696 (no_rdma && BNXT_PF(bp)))
7699 bp->max_q = bp->max_tc;
7700 bp->max_tc = max_t(u8, j, 1);
7702 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
7705 if (bp->max_lltc > bp->max_tc)
7706 bp->max_lltc = bp->max_tc;
7709 mutex_unlock(&bp->hwrm_cmd_lock);
7713 static int __bnxt_hwrm_ver_get(struct bnxt *bp, bool silent)
7715 struct hwrm_ver_get_input req = {0};
7718 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
7719 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
7720 req.hwrm_intf_min = HWRM_VERSION_MINOR;
7721 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
7723 rc = bnxt_hwrm_do_send_msg(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT,
7728 static int bnxt_hwrm_ver_get(struct bnxt *bp)
7730 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
7731 u16 fw_maj, fw_min, fw_bld, fw_rsv;
7732 u32 dev_caps_cfg, hwrm_ver;
7735 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
7736 mutex_lock(&bp->hwrm_cmd_lock);
7737 rc = __bnxt_hwrm_ver_get(bp, false);
7739 goto hwrm_ver_get_exit;
7741 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
7743 bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
7744 resp->hwrm_intf_min_8b << 8 |
7745 resp->hwrm_intf_upd_8b;
7746 if (resp->hwrm_intf_maj_8b < 1) {
7747 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
7748 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7749 resp->hwrm_intf_upd_8b);
7750 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
7753 hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
7754 HWRM_VERSION_UPDATE;
7756 if (bp->hwrm_spec_code > hwrm_ver)
7757 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7758 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
7759 HWRM_VERSION_UPDATE);
7761 snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
7762 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
7763 resp->hwrm_intf_upd_8b);
7765 fw_maj = le16_to_cpu(resp->hwrm_fw_major);
7766 if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
7767 fw_min = le16_to_cpu(resp->hwrm_fw_minor);
7768 fw_bld = le16_to_cpu(resp->hwrm_fw_build);
7769 fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
7770 len = FW_VER_STR_LEN;
7772 fw_maj = resp->hwrm_fw_maj_8b;
7773 fw_min = resp->hwrm_fw_min_8b;
7774 fw_bld = resp->hwrm_fw_bld_8b;
7775 fw_rsv = resp->hwrm_fw_rsvd_8b;
7776 len = BC_HWRM_STR_LEN;
7778 bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
7779 snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
7782 if (strlen(resp->active_pkg_name)) {
7783 int fw_ver_len = strlen(bp->fw_ver_str);
7785 snprintf(bp->fw_ver_str + fw_ver_len,
7786 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
7787 resp->active_pkg_name);
7788 bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
7791 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
7792 if (!bp->hwrm_cmd_timeout)
7793 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
7795 if (resp->hwrm_intf_maj_8b >= 1) {
7796 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
7797 bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
7799 if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
7800 bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
7802 bp->chip_num = le16_to_cpu(resp->chip_num);
7803 bp->chip_rev = resp->chip_rev;
7804 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
7806 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
7808 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
7809 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
7810 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
7811 bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
7813 if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
7814 bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
7817 VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
7818 bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
7821 VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
7822 bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
7825 VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
7826 bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
7829 mutex_unlock(&bp->hwrm_cmd_lock);
7833 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
7835 struct hwrm_fw_set_time_input req = {0};
7837 time64_t now = ktime_get_real_seconds();
7839 if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
7840 bp->hwrm_spec_code < 0x10400)
7843 time64_to_tm(now, 0, &tm);
7844 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
7845 req.year = cpu_to_le16(1900 + tm.tm_year);
7846 req.month = 1 + tm.tm_mon;
7847 req.day = tm.tm_mday;
7848 req.hour = tm.tm_hour;
7849 req.minute = tm.tm_min;
7850 req.second = tm.tm_sec;
7851 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7854 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
7859 sw_tmp = (*sw & ~mask) | hw;
7860 if (hw < (*sw & mask))
7862 WRITE_ONCE(*sw, sw_tmp);
7865 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
7866 int count, bool ignore_zero)
7870 for (i = 0; i < count; i++) {
7871 u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
7873 if (ignore_zero && !hw)
7876 if (masks[i] == -1ULL)
7879 bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
7883 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
7885 if (!stats->hw_stats)
7888 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7889 stats->hw_masks, stats->len / 8, false);
7892 static void bnxt_accumulate_all_stats(struct bnxt *bp)
7894 struct bnxt_stats_mem *ring0_stats;
7895 bool ignore_zero = false;
7898 /* Chip bug. Counter intermittently becomes 0. */
7899 if (bp->flags & BNXT_FLAG_CHIP_P5)
7902 for (i = 0; i < bp->cp_nr_rings; i++) {
7903 struct bnxt_napi *bnapi = bp->bnapi[i];
7904 struct bnxt_cp_ring_info *cpr;
7905 struct bnxt_stats_mem *stats;
7907 cpr = &bnapi->cp_ring;
7908 stats = &cpr->stats;
7910 ring0_stats = stats;
7911 __bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
7912 ring0_stats->hw_masks,
7913 ring0_stats->len / 8, ignore_zero);
7915 if (bp->flags & BNXT_FLAG_PORT_STATS) {
7916 struct bnxt_stats_mem *stats = &bp->port_stats;
7917 __le64 *hw_stats = stats->hw_stats;
7918 u64 *sw_stats = stats->sw_stats;
7919 u64 *masks = stats->hw_masks;
7922 cnt = sizeof(struct rx_port_stats) / 8;
7923 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7925 hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7926 sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7927 masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
7928 cnt = sizeof(struct tx_port_stats) / 8;
7929 __bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
7931 if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
7932 bnxt_accumulate_stats(&bp->rx_port_stats_ext);
7933 bnxt_accumulate_stats(&bp->tx_port_stats_ext);
7937 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
7939 struct bnxt_pf_info *pf = &bp->pf;
7940 struct hwrm_port_qstats_input req = {0};
7942 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
7945 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7949 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
7950 req.port_id = cpu_to_le16(pf->port_id);
7951 req.tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
7952 BNXT_TX_PORT_STATS_BYTE_OFFSET);
7953 req.rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
7954 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7957 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
7959 struct hwrm_port_qstats_ext_output *resp = bp->hwrm_cmd_resp_addr;
7960 struct hwrm_queue_pri2cos_qcfg_input req2 = {0};
7961 struct hwrm_port_qstats_ext_input req = {0};
7962 struct bnxt_pf_info *pf = &bp->pf;
7966 if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
7969 if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
7972 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
7974 req.port_id = cpu_to_le16(pf->port_id);
7975 req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
7976 req.rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
7977 tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
7978 sizeof(struct tx_port_stats_ext) : 0;
7979 req.tx_stat_size = cpu_to_le16(tx_stat_size);
7980 req.tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
7981 mutex_lock(&bp->hwrm_cmd_lock);
7982 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
7984 bp->fw_rx_stats_ext_size = le16_to_cpu(resp->rx_stat_size) / 8;
7985 bp->fw_tx_stats_ext_size = tx_stat_size ?
7986 le16_to_cpu(resp->tx_stat_size) / 8 : 0;
7988 bp->fw_rx_stats_ext_size = 0;
7989 bp->fw_tx_stats_ext_size = 0;
7994 if (bp->fw_tx_stats_ext_size <=
7995 offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
7996 mutex_unlock(&bp->hwrm_cmd_lock);
7997 bp->pri2cos_valid = 0;
8001 bnxt_hwrm_cmd_hdr_init(bp, &req2, HWRM_QUEUE_PRI2COS_QCFG, -1, -1);
8002 req2.flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
8004 rc = _hwrm_send_message(bp, &req2, sizeof(req2), HWRM_CMD_TIMEOUT);
8006 struct hwrm_queue_pri2cos_qcfg_output *resp2;
8010 resp2 = bp->hwrm_cmd_resp_addr;
8011 pri2cos = &resp2->pri0_cos_queue_id;
8012 for (i = 0; i < 8; i++) {
8013 u8 queue_id = pri2cos[i];
8016 /* Per port queue IDs start from 0, 10, 20, etc */
8017 queue_idx = queue_id % 10;
8018 if (queue_idx > BNXT_MAX_QUEUE) {
8019 bp->pri2cos_valid = false;
8022 for (j = 0; j < bp->max_q; j++) {
8023 if (bp->q_ids[j] == queue_id)
8024 bp->pri2cos_idx[i] = queue_idx;
8027 bp->pri2cos_valid = 1;
8030 mutex_unlock(&bp->hwrm_cmd_lock);
8034 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
8036 if (bp->vxlan_fw_dst_port_id != INVALID_HW_RING_ID)
8037 bnxt_hwrm_tunnel_dst_port_free(
8038 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
8039 if (bp->nge_fw_dst_port_id != INVALID_HW_RING_ID)
8040 bnxt_hwrm_tunnel_dst_port_free(
8041 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
8044 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
8050 tpa_flags = bp->flags & BNXT_FLAG_TPA;
8051 else if (BNXT_NO_FW_ACCESS(bp))
8053 for (i = 0; i < bp->nr_vnics; i++) {
8054 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
8056 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
8064 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
8068 for (i = 0; i < bp->nr_vnics; i++)
8069 bnxt_hwrm_vnic_set_rss(bp, i, false);
8072 static void bnxt_clear_vnic(struct bnxt *bp)
8077 bnxt_hwrm_clear_vnic_filter(bp);
8078 if (!(bp->flags & BNXT_FLAG_CHIP_P5)) {
8079 /* clear all RSS setting before free vnic ctx */
8080 bnxt_hwrm_clear_vnic_rss(bp);
8081 bnxt_hwrm_vnic_ctx_free(bp);
8083 /* before free the vnic, undo the vnic tpa settings */
8084 if (bp->flags & BNXT_FLAG_TPA)
8085 bnxt_set_tpa(bp, false);
8086 bnxt_hwrm_vnic_free(bp);
8087 if (bp->flags & BNXT_FLAG_CHIP_P5)
8088 bnxt_hwrm_vnic_ctx_free(bp);
8091 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
8094 bnxt_clear_vnic(bp);
8095 bnxt_hwrm_ring_free(bp, close_path);
8096 bnxt_hwrm_ring_grp_free(bp);
8098 bnxt_hwrm_stat_ctx_free(bp);
8099 bnxt_hwrm_free_tunnel_ports(bp);
8103 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
8105 struct hwrm_func_cfg_input req = {0};
8107 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8108 req.fid = cpu_to_le16(0xffff);
8109 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
8110 if (br_mode == BRIDGE_MODE_VEB)
8111 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
8112 else if (br_mode == BRIDGE_MODE_VEPA)
8113 req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
8116 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8119 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
8121 struct hwrm_func_cfg_input req = {0};
8123 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
8126 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
8127 req.fid = cpu_to_le16(0xffff);
8128 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
8129 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
8131 req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
8133 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
8136 static int __bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8138 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
8141 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
8144 /* allocate context for vnic */
8145 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
8147 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8149 goto vnic_setup_err;
8151 bp->rsscos_nr_ctxs++;
8153 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8154 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
8156 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
8158 goto vnic_setup_err;
8160 bp->rsscos_nr_ctxs++;
8164 /* configure default vnic, ring grp */
8165 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8167 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8169 goto vnic_setup_err;
8172 /* Enable RSS hashing on vnic */
8173 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
8175 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
8177 goto vnic_setup_err;
8180 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8181 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8183 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8192 static int __bnxt_setup_vnic_p5(struct bnxt *bp, u16 vnic_id)
8196 nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
8197 for (i = 0; i < nr_ctxs; i++) {
8198 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, i);
8200 netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
8204 bp->rsscos_nr_ctxs++;
8209 rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic_id, true);
8211 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
8215 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
8217 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
8221 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8222 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
8224 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
8231 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
8233 if (bp->flags & BNXT_FLAG_CHIP_P5)
8234 return __bnxt_setup_vnic_p5(bp, vnic_id);
8236 return __bnxt_setup_vnic(bp, vnic_id);
8239 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
8241 #ifdef CONFIG_RFS_ACCEL
8244 if (bp->flags & BNXT_FLAG_CHIP_P5)
8247 for (i = 0; i < bp->rx_nr_rings; i++) {
8248 struct bnxt_vnic_info *vnic;
8249 u16 vnic_id = i + 1;
8252 if (vnic_id >= bp->nr_vnics)
8255 vnic = &bp->vnic_info[vnic_id];
8256 vnic->flags |= BNXT_VNIC_RFS_FLAG;
8257 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
8258 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
8259 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
8261 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
8265 rc = bnxt_setup_vnic(bp, vnic_id);
8275 /* Allow PF and VF with default VLAN to be in promiscuous mode */
8276 static bool bnxt_promisc_ok(struct bnxt *bp)
8278 #ifdef CONFIG_BNXT_SRIOV
8279 if (BNXT_VF(bp) && !bp->vf.vlan)
8285 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
8287 unsigned int rc = 0;
8289 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
8291 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8296 rc = bnxt_hwrm_vnic_cfg(bp, 1);
8298 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
8305 static int bnxt_cfg_rx_mode(struct bnxt *);
8306 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
8308 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
8310 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
8312 unsigned int rx_nr_rings = bp->rx_nr_rings;
8315 rc = bnxt_hwrm_stat_ctx_alloc(bp);
8317 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
8323 rc = bnxt_hwrm_ring_alloc(bp);
8325 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
8329 rc = bnxt_hwrm_ring_grp_alloc(bp);
8331 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
8335 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8338 /* default vnic 0 */
8339 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
8341 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
8346 bnxt_hwrm_func_qcfg(bp);
8348 rc = bnxt_setup_vnic(bp, 0);
8352 if (bp->flags & BNXT_FLAG_RFS) {
8353 rc = bnxt_alloc_rfs_vnics(bp);
8358 if (bp->flags & BNXT_FLAG_TPA) {
8359 rc = bnxt_set_tpa(bp, true);
8365 bnxt_update_vf_mac(bp);
8367 /* Filter for default vnic 0 */
8368 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
8370 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
8373 vnic->uc_filter_count = 1;
8376 if (bp->dev->flags & IFF_BROADCAST)
8377 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
8379 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
8380 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
8382 if (bp->dev->flags & IFF_ALLMULTI) {
8383 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
8384 vnic->mc_list_count = 0;
8388 bnxt_mc_list_updated(bp, &mask);
8389 vnic->rx_mask |= mask;
8392 rc = bnxt_cfg_rx_mode(bp);
8396 rc = bnxt_hwrm_set_coal(bp);
8398 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
8401 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8402 rc = bnxt_setup_nitroa0_vnic(bp);
8404 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
8409 bnxt_hwrm_func_qcfg(bp);
8410 netdev_update_features(bp->dev);
8416 bnxt_hwrm_resource_free(bp, 0, true);
8421 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
8423 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
8427 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
8429 bnxt_init_cp_rings(bp);
8430 bnxt_init_rx_rings(bp);
8431 bnxt_init_tx_rings(bp);
8432 bnxt_init_ring_grps(bp, irq_re_init);
8433 bnxt_init_vnics(bp);
8435 return bnxt_init_chip(bp, irq_re_init);
8438 static int bnxt_set_real_num_queues(struct bnxt *bp)
8441 struct net_device *dev = bp->dev;
8443 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
8444 bp->tx_nr_rings_xdp);
8448 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
8452 #ifdef CONFIG_RFS_ACCEL
8453 if (bp->flags & BNXT_FLAG_RFS)
8454 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
8460 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
8463 int _rx = *rx, _tx = *tx;
8466 *rx = min_t(int, _rx, max);
8467 *tx = min_t(int, _tx, max);
8472 while (_rx + _tx > max) {
8473 if (_rx > _tx && _rx > 1)
8484 static void bnxt_setup_msix(struct bnxt *bp)
8486 const int len = sizeof(bp->irq_tbl[0].name);
8487 struct net_device *dev = bp->dev;
8490 tcs = netdev_get_num_tc(dev);
8494 for (i = 0; i < tcs; i++) {
8495 count = bp->tx_nr_rings_per_tc;
8497 netdev_set_tc_queue(dev, i, count, off);
8501 for (i = 0; i < bp->cp_nr_rings; i++) {
8502 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8505 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8507 else if (i < bp->rx_nr_rings)
8512 snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
8514 bp->irq_tbl[map_idx].handler = bnxt_msix;
8518 static void bnxt_setup_inta(struct bnxt *bp)
8520 const int len = sizeof(bp->irq_tbl[0].name);
8522 if (netdev_get_num_tc(bp->dev))
8523 netdev_reset_tc(bp->dev);
8525 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
8527 bp->irq_tbl[0].handler = bnxt_inta;
8530 static int bnxt_init_int_mode(struct bnxt *bp);
8532 static int bnxt_setup_int_mode(struct bnxt *bp)
8537 rc = bnxt_init_int_mode(bp);
8538 if (rc || !bp->irq_tbl)
8539 return rc ?: -ENODEV;
8542 if (bp->flags & BNXT_FLAG_USING_MSIX)
8543 bnxt_setup_msix(bp);
8545 bnxt_setup_inta(bp);
8547 rc = bnxt_set_real_num_queues(bp);
8551 #ifdef CONFIG_RFS_ACCEL
8552 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
8554 return bp->hw_resc.max_rsscos_ctxs;
8557 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
8559 return bp->hw_resc.max_vnics;
8563 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
8565 return bp->hw_resc.max_stat_ctxs;
8568 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
8570 return bp->hw_resc.max_cp_rings;
8573 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
8575 unsigned int cp = bp->hw_resc.max_cp_rings;
8577 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8578 cp -= bnxt_get_ulp_msix_num(bp);
8583 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
8585 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8587 if (bp->flags & BNXT_FLAG_CHIP_P5)
8588 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
8590 return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
8593 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
8595 bp->hw_resc.max_irqs = max_irqs;
8598 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
8602 cp = bnxt_get_max_func_cp_rings_for_en(bp);
8603 if (bp->flags & BNXT_FLAG_CHIP_P5)
8604 return cp - bp->rx_nr_rings - bp->tx_nr_rings;
8606 return cp - bp->cp_nr_rings;
8609 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
8611 return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
8614 int bnxt_get_avail_msix(struct bnxt *bp, int num)
8616 int max_cp = bnxt_get_max_func_cp_rings(bp);
8617 int max_irq = bnxt_get_max_func_irqs(bp);
8618 int total_req = bp->cp_nr_rings + num;
8619 int max_idx, avail_msix;
8621 max_idx = bp->total_irqs;
8622 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
8623 max_idx = min_t(int, bp->total_irqs, max_cp);
8624 avail_msix = max_idx - bp->cp_nr_rings;
8625 if (!BNXT_NEW_RM(bp) || avail_msix >= num)
8628 if (max_irq < total_req) {
8629 num = max_irq - bp->cp_nr_rings;
8636 static int bnxt_get_num_msix(struct bnxt *bp)
8638 if (!BNXT_NEW_RM(bp))
8639 return bnxt_get_max_func_irqs(bp);
8641 return bnxt_nq_rings_in_use(bp);
8644 static int bnxt_init_msix(struct bnxt *bp)
8646 int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
8647 struct msix_entry *msix_ent;
8649 total_vecs = bnxt_get_num_msix(bp);
8650 max = bnxt_get_max_func_irqs(bp);
8651 if (total_vecs > max)
8657 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
8661 for (i = 0; i < total_vecs; i++) {
8662 msix_ent[i].entry = i;
8663 msix_ent[i].vector = 0;
8666 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
8669 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
8670 ulp_msix = bnxt_get_ulp_msix_num(bp);
8671 if (total_vecs < 0 || total_vecs < ulp_msix) {
8673 goto msix_setup_exit;
8676 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
8678 for (i = 0; i < total_vecs; i++)
8679 bp->irq_tbl[i].vector = msix_ent[i].vector;
8681 bp->total_irqs = total_vecs;
8682 /* Trim rings based upon num of vectors allocated */
8683 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
8684 total_vecs - ulp_msix, min == 1);
8686 goto msix_setup_exit;
8688 bp->cp_nr_rings = (min == 1) ?
8689 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
8690 bp->tx_nr_rings + bp->rx_nr_rings;
8694 goto msix_setup_exit;
8696 bp->flags |= BNXT_FLAG_USING_MSIX;
8701 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
8704 pci_disable_msix(bp->pdev);
8709 static int bnxt_init_inta(struct bnxt *bp)
8711 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
8716 bp->rx_nr_rings = 1;
8717 bp->tx_nr_rings = 1;
8718 bp->cp_nr_rings = 1;
8719 bp->flags |= BNXT_FLAG_SHARED_RINGS;
8720 bp->irq_tbl[0].vector = bp->pdev->irq;
8724 static int bnxt_init_int_mode(struct bnxt *bp)
8728 if (bp->flags & BNXT_FLAG_MSIX_CAP)
8729 rc = bnxt_init_msix(bp);
8731 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
8732 /* fallback to INTA */
8733 rc = bnxt_init_inta(bp);
8738 static void bnxt_clear_int_mode(struct bnxt *bp)
8740 if (bp->flags & BNXT_FLAG_USING_MSIX)
8741 pci_disable_msix(bp->pdev);
8745 bp->flags &= ~BNXT_FLAG_USING_MSIX;
8748 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
8750 int tcs = netdev_get_num_tc(bp->dev);
8751 bool irq_cleared = false;
8754 if (!bnxt_need_reserve_rings(bp))
8757 if (irq_re_init && BNXT_NEW_RM(bp) &&
8758 bnxt_get_num_msix(bp) != bp->total_irqs) {
8759 bnxt_ulp_irq_stop(bp);
8760 bnxt_clear_int_mode(bp);
8763 rc = __bnxt_reserve_rings(bp);
8766 rc = bnxt_init_int_mode(bp);
8767 bnxt_ulp_irq_restart(bp, rc);
8770 netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
8773 if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
8774 bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
8775 netdev_err(bp->dev, "tx ring reservation failure\n");
8776 netdev_reset_tc(bp->dev);
8777 if (bp->tx_nr_rings_xdp)
8778 bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
8780 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
8786 static void bnxt_free_irq(struct bnxt *bp)
8788 struct bnxt_irq *irq;
8791 #ifdef CONFIG_RFS_ACCEL
8792 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
8793 bp->dev->rx_cpu_rmap = NULL;
8795 if (!bp->irq_tbl || !bp->bnapi)
8798 for (i = 0; i < bp->cp_nr_rings; i++) {
8799 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8801 irq = &bp->irq_tbl[map_idx];
8802 if (irq->requested) {
8803 if (irq->have_cpumask) {
8804 irq_set_affinity_hint(irq->vector, NULL);
8805 free_cpumask_var(irq->cpu_mask);
8806 irq->have_cpumask = 0;
8808 free_irq(irq->vector, bp->bnapi[i]);
8815 static int bnxt_request_irq(struct bnxt *bp)
8818 unsigned long flags = 0;
8819 #ifdef CONFIG_RFS_ACCEL
8820 struct cpu_rmap *rmap;
8823 rc = bnxt_setup_int_mode(bp);
8825 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
8829 #ifdef CONFIG_RFS_ACCEL
8830 rmap = bp->dev->rx_cpu_rmap;
8832 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
8833 flags = IRQF_SHARED;
8835 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
8836 int map_idx = bnxt_cp_num_to_irq_num(bp, i);
8837 struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
8839 #ifdef CONFIG_RFS_ACCEL
8840 if (rmap && bp->bnapi[i]->rx_ring) {
8841 rc = irq_cpu_rmap_add(rmap, irq->vector);
8843 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
8848 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
8855 if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
8856 int numa_node = dev_to_node(&bp->pdev->dev);
8858 irq->have_cpumask = 1;
8859 cpumask_set_cpu(cpumask_local_spread(i, numa_node),
8861 rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
8863 netdev_warn(bp->dev,
8864 "Set affinity failed, IRQ = %d\n",
8873 static void bnxt_del_napi(struct bnxt *bp)
8880 for (i = 0; i < bp->cp_nr_rings; i++) {
8881 struct bnxt_napi *bnapi = bp->bnapi[i];
8883 __netif_napi_del(&bnapi->napi);
8885 /* We called __netif_napi_del(), we need
8886 * to respect an RCU grace period before freeing napi structures.
8891 static void bnxt_init_napi(struct bnxt *bp)
8894 unsigned int cp_nr_rings = bp->cp_nr_rings;
8895 struct bnxt_napi *bnapi;
8897 if (bp->flags & BNXT_FLAG_USING_MSIX) {
8898 int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
8900 if (bp->flags & BNXT_FLAG_CHIP_P5)
8901 poll_fn = bnxt_poll_p5;
8902 else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8904 for (i = 0; i < cp_nr_rings; i++) {
8905 bnapi = bp->bnapi[i];
8906 netif_napi_add(bp->dev, &bnapi->napi, poll_fn, 64);
8908 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
8909 bnapi = bp->bnapi[cp_nr_rings];
8910 netif_napi_add(bp->dev, &bnapi->napi,
8911 bnxt_poll_nitroa0, 64);
8914 bnapi = bp->bnapi[0];
8915 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
8919 static void bnxt_disable_napi(struct bnxt *bp)
8926 for (i = 0; i < bp->cp_nr_rings; i++) {
8927 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
8929 napi_disable(&bp->bnapi[i]->napi);
8930 if (bp->bnapi[i]->rx_ring)
8931 cancel_work_sync(&cpr->dim.work);
8935 static void bnxt_enable_napi(struct bnxt *bp)
8939 for (i = 0; i < bp->cp_nr_rings; i++) {
8940 struct bnxt_napi *bnapi = bp->bnapi[i];
8941 struct bnxt_cp_ring_info *cpr;
8943 cpr = &bnapi->cp_ring;
8944 if (bnapi->in_reset)
8945 cpr->sw_stats.rx.rx_resets++;
8946 bnapi->in_reset = false;
8948 if (bnapi->rx_ring) {
8949 INIT_WORK(&cpr->dim.work, bnxt_dim_work);
8950 cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
8952 napi_enable(&bnapi->napi);
8956 void bnxt_tx_disable(struct bnxt *bp)
8959 struct bnxt_tx_ring_info *txr;
8962 for (i = 0; i < bp->tx_nr_rings; i++) {
8963 txr = &bp->tx_ring[i];
8964 WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
8967 /* Make sure napi polls see @dev_state change */
8969 /* Drop carrier first to prevent TX timeout */
8970 netif_carrier_off(bp->dev);
8971 /* Stop all TX queues */
8972 netif_tx_disable(bp->dev);
8975 void bnxt_tx_enable(struct bnxt *bp)
8978 struct bnxt_tx_ring_info *txr;
8980 for (i = 0; i < bp->tx_nr_rings; i++) {
8981 txr = &bp->tx_ring[i];
8982 WRITE_ONCE(txr->dev_state, 0);
8984 /* Make sure napi polls see @dev_state change */
8986 netif_tx_wake_all_queues(bp->dev);
8987 if (bp->link_info.link_up)
8988 netif_carrier_on(bp->dev);
8991 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
8993 u8 active_fec = link_info->active_fec_sig_mode &
8994 PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
8996 switch (active_fec) {
8998 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
9000 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
9001 return "Clause 74 BaseR";
9002 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
9003 return "Clause 91 RS(528,514)";
9004 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
9005 return "Clause 91 RS544_1XN";
9006 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
9007 return "Clause 91 RS(544,514)";
9008 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
9009 return "Clause 91 RS272_1XN";
9010 case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
9011 return "Clause 91 RS(272,257)";
9015 static void bnxt_report_link(struct bnxt *bp)
9017 if (bp->link_info.link_up) {
9019 const char *flow_ctrl;
9023 netif_carrier_on(bp->dev);
9024 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
9025 if (speed == SPEED_UNKNOWN) {
9026 netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
9029 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
9033 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
9034 flow_ctrl = "ON - receive & transmit";
9035 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
9036 flow_ctrl = "ON - transmit";
9037 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
9038 flow_ctrl = "ON - receive";
9041 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
9042 speed, duplex, flow_ctrl);
9043 if (bp->flags & BNXT_FLAG_EEE_CAP)
9044 netdev_info(bp->dev, "EEE is %s\n",
9045 bp->eee.eee_active ? "active" :
9047 fec = bp->link_info.fec_cfg;
9048 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
9049 netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
9050 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
9051 bnxt_report_fec(&bp->link_info));
9053 netif_carrier_off(bp->dev);
9054 netdev_err(bp->dev, "NIC Link is Down\n");
9058 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
9060 if (!resp->supported_speeds_auto_mode &&
9061 !resp->supported_speeds_force_mode &&
9062 !resp->supported_pam4_speeds_auto_mode &&
9063 !resp->supported_pam4_speeds_force_mode)
9068 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
9071 struct hwrm_port_phy_qcaps_input req = {0};
9072 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9073 struct bnxt_link_info *link_info = &bp->link_info;
9075 bp->flags &= ~BNXT_FLAG_EEE_CAP;
9077 bp->test_info->flags &= ~(BNXT_TEST_FL_EXT_LPBK |
9078 BNXT_TEST_FL_AN_PHY_LPBK);
9079 if (bp->hwrm_spec_code < 0x10201)
9082 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
9084 mutex_lock(&bp->hwrm_cmd_lock);
9085 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9087 goto hwrm_phy_qcaps_exit;
9089 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
9090 struct ethtool_eee *eee = &bp->eee;
9091 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
9093 bp->flags |= BNXT_FLAG_EEE_CAP;
9094 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9095 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
9096 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
9097 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
9098 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
9100 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
9102 bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
9104 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_AUTONEG_LPBK_SUPPORTED) {
9106 bp->test_info->flags |= BNXT_TEST_FL_AN_PHY_LPBK;
9108 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_SHARED_PHY_CFG_SUPPORTED) {
9110 bp->fw_cap |= BNXT_FW_CAP_SHARED_PORT_CFG;
9112 if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_CUMULATIVE_COUNTERS_ON_RESET)
9113 bp->fw_cap |= BNXT_FW_CAP_PORT_STATS_NO_RESET;
9115 if (bp->hwrm_spec_code >= 0x10a01) {
9116 if (bnxt_phy_qcaps_no_speed(resp)) {
9117 link_info->phy_state = BNXT_PHY_STATE_DISABLED;
9118 netdev_warn(bp->dev, "Ethernet link disabled\n");
9119 } else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
9120 link_info->phy_state = BNXT_PHY_STATE_ENABLED;
9121 netdev_info(bp->dev, "Ethernet link enabled\n");
9122 /* Phy re-enabled, reprobe the speeds */
9123 link_info->support_auto_speeds = 0;
9124 link_info->support_pam4_auto_speeds = 0;
9127 if (resp->supported_speeds_auto_mode)
9128 link_info->support_auto_speeds =
9129 le16_to_cpu(resp->supported_speeds_auto_mode);
9130 if (resp->supported_pam4_speeds_auto_mode)
9131 link_info->support_pam4_auto_speeds =
9132 le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
9134 bp->port_count = resp->port_cnt;
9136 hwrm_phy_qcaps_exit:
9137 mutex_unlock(&bp->hwrm_cmd_lock);
9141 static bool bnxt_support_dropped(u16 advertising, u16 supported)
9143 u16 diff = advertising ^ supported;
9145 return ((supported | diff) != supported);
9148 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
9151 struct bnxt_link_info *link_info = &bp->link_info;
9152 struct hwrm_port_phy_qcfg_input req = {0};
9153 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9154 u8 link_up = link_info->link_up;
9155 bool support_changed = false;
9157 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
9159 mutex_lock(&bp->hwrm_cmd_lock);
9160 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9162 mutex_unlock(&bp->hwrm_cmd_lock);
9166 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
9167 link_info->phy_link_status = resp->link;
9168 link_info->duplex = resp->duplex_cfg;
9169 if (bp->hwrm_spec_code >= 0x10800)
9170 link_info->duplex = resp->duplex_state;
9171 link_info->pause = resp->pause;
9172 link_info->auto_mode = resp->auto_mode;
9173 link_info->auto_pause_setting = resp->auto_pause;
9174 link_info->lp_pause = resp->link_partner_adv_pause;
9175 link_info->force_pause_setting = resp->force_pause;
9176 link_info->duplex_setting = resp->duplex_cfg;
9177 if (link_info->phy_link_status == BNXT_LINK_LINK)
9178 link_info->link_speed = le16_to_cpu(resp->link_speed);
9180 link_info->link_speed = 0;
9181 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
9182 link_info->force_pam4_link_speed =
9183 le16_to_cpu(resp->force_pam4_link_speed);
9184 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
9185 link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
9186 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
9187 link_info->auto_pam4_link_speeds =
9188 le16_to_cpu(resp->auto_pam4_link_speed_mask);
9189 link_info->lp_auto_link_speeds =
9190 le16_to_cpu(resp->link_partner_adv_speeds);
9191 link_info->lp_auto_pam4_link_speeds =
9192 resp->link_partner_pam4_adv_speeds;
9193 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
9194 link_info->phy_ver[0] = resp->phy_maj;
9195 link_info->phy_ver[1] = resp->phy_min;
9196 link_info->phy_ver[2] = resp->phy_bld;
9197 link_info->media_type = resp->media_type;
9198 link_info->phy_type = resp->phy_type;
9199 link_info->transceiver = resp->xcvr_pkg_type;
9200 link_info->phy_addr = resp->eee_config_phy_addr &
9201 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
9202 link_info->module_status = resp->module_status;
9204 if (bp->flags & BNXT_FLAG_EEE_CAP) {
9205 struct ethtool_eee *eee = &bp->eee;
9208 eee->eee_active = 0;
9209 if (resp->eee_config_phy_addr &
9210 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
9211 eee->eee_active = 1;
9212 fw_speeds = le16_to_cpu(
9213 resp->link_partner_adv_eee_link_speed_mask);
9214 eee->lp_advertised =
9215 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9218 /* Pull initial EEE config */
9219 if (!chng_link_state) {
9220 if (resp->eee_config_phy_addr &
9221 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
9222 eee->eee_enabled = 1;
9224 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
9226 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
9228 if (resp->eee_config_phy_addr &
9229 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
9232 eee->tx_lpi_enabled = 1;
9233 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
9234 eee->tx_lpi_timer = le32_to_cpu(tmr) &
9235 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
9240 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
9241 if (bp->hwrm_spec_code >= 0x10504) {
9242 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
9243 link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
9245 /* TODO: need to add more logic to report VF link */
9246 if (chng_link_state) {
9247 if (link_info->phy_link_status == BNXT_LINK_LINK)
9248 link_info->link_up = 1;
9250 link_info->link_up = 0;
9251 if (link_up != link_info->link_up)
9252 bnxt_report_link(bp);
9254 /* alwasy link down if not require to update link state */
9255 link_info->link_up = 0;
9257 mutex_unlock(&bp->hwrm_cmd_lock);
9259 if (!BNXT_PHY_CFG_ABLE(bp))
9262 /* Check if any advertised speeds are no longer supported. The caller
9263 * holds the link_lock mutex, so we can modify link_info settings.
9265 if (bnxt_support_dropped(link_info->advertising,
9266 link_info->support_auto_speeds)) {
9267 link_info->advertising = link_info->support_auto_speeds;
9268 support_changed = true;
9270 if (bnxt_support_dropped(link_info->advertising_pam4,
9271 link_info->support_pam4_auto_speeds)) {
9272 link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
9273 support_changed = true;
9275 if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
9276 bnxt_hwrm_set_link_setting(bp, true, false);
9280 static void bnxt_get_port_module_status(struct bnxt *bp)
9282 struct bnxt_link_info *link_info = &bp->link_info;
9283 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
9286 if (bnxt_update_link(bp, true))
9289 module_status = link_info->module_status;
9290 switch (module_status) {
9291 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
9292 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
9293 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
9294 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
9296 if (bp->hwrm_spec_code >= 0x10201) {
9297 netdev_warn(bp->dev, "Module part number %s\n",
9298 resp->phy_vendor_partnumber);
9300 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
9301 netdev_warn(bp->dev, "TX is disabled\n");
9302 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
9303 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
9308 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9310 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
9311 if (bp->hwrm_spec_code >= 0x10201)
9313 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
9314 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9315 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
9316 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9317 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
9319 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9321 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
9322 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
9323 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
9324 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
9326 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
9327 if (bp->hwrm_spec_code >= 0x10201) {
9328 req->auto_pause = req->force_pause;
9329 req->enables |= cpu_to_le32(
9330 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
9335 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
9337 if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
9338 req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
9339 if (bp->link_info.advertising) {
9340 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
9341 req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
9343 if (bp->link_info.advertising_pam4) {
9345 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
9346 req->auto_link_pam4_speed_mask =
9347 cpu_to_le16(bp->link_info.advertising_pam4);
9349 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
9350 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
9352 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
9353 if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
9354 req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9355 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
9357 req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
9361 /* tell chimp that the setting takes effect immediately */
9362 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
9365 int bnxt_hwrm_set_pause(struct bnxt *bp)
9367 struct hwrm_port_phy_cfg_input req = {0};
9370 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9371 bnxt_hwrm_set_pause_common(bp, &req);
9373 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
9374 bp->link_info.force_link_chng)
9375 bnxt_hwrm_set_link_common(bp, &req);
9377 mutex_lock(&bp->hwrm_cmd_lock);
9378 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9379 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
9380 /* since changing of pause setting doesn't trigger any link
9381 * change event, the driver needs to update the current pause
9382 * result upon successfully return of the phy_cfg command
9384 bp->link_info.pause =
9385 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
9386 bp->link_info.auto_pause_setting = 0;
9387 if (!bp->link_info.force_link_chng)
9388 bnxt_report_link(bp);
9390 bp->link_info.force_link_chng = false;
9391 mutex_unlock(&bp->hwrm_cmd_lock);
9395 static void bnxt_hwrm_set_eee(struct bnxt *bp,
9396 struct hwrm_port_phy_cfg_input *req)
9398 struct ethtool_eee *eee = &bp->eee;
9400 if (eee->eee_enabled) {
9402 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
9404 if (eee->tx_lpi_enabled)
9405 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
9407 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
9409 req->flags |= cpu_to_le32(flags);
9410 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
9411 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
9412 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
9414 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
9418 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
9420 struct hwrm_port_phy_cfg_input req = {0};
9422 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9424 bnxt_hwrm_set_pause_common(bp, &req);
9426 bnxt_hwrm_set_link_common(bp, &req);
9429 bnxt_hwrm_set_eee(bp, &req);
9430 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9433 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
9435 struct hwrm_port_phy_cfg_input req = {0};
9437 if (!BNXT_SINGLE_PF(bp))
9440 if (pci_num_vf(bp->pdev))
9443 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
9444 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
9445 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9448 static int bnxt_fw_init_one(struct bnxt *bp);
9450 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
9452 struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
9453 struct hwrm_func_drv_if_change_input req = {0};
9454 bool fw_reset = !bp->irq_tbl;
9455 bool resc_reinit = false;
9459 if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
9462 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
9464 req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
9465 mutex_lock(&bp->hwrm_cmd_lock);
9466 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9468 flags = le32_to_cpu(resp->flags);
9469 mutex_unlock(&bp->hwrm_cmd_lock);
9476 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
9478 if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE)
9481 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
9482 netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
9483 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9486 if (resc_reinit || fw_reset) {
9488 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
9490 bnxt_free_ctx_mem(bp);
9494 rc = bnxt_fw_init_one(bp);
9496 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
9499 bnxt_clear_int_mode(bp);
9500 rc = bnxt_init_int_mode(bp);
9502 netdev_err(bp->dev, "init int mode failed\n");
9505 set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
9507 if (BNXT_NEW_RM(bp)) {
9508 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9510 rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9511 hw_resc->resv_cp_rings = 0;
9512 hw_resc->resv_stat_ctxs = 0;
9513 hw_resc->resv_irqs = 0;
9514 hw_resc->resv_tx_rings = 0;
9515 hw_resc->resv_rx_rings = 0;
9516 hw_resc->resv_hw_ring_grps = 0;
9517 hw_resc->resv_vnics = 0;
9519 bp->tx_nr_rings = 0;
9520 bp->rx_nr_rings = 0;
9527 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
9529 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
9530 struct hwrm_port_led_qcaps_input req = {0};
9531 struct bnxt_pf_info *pf = &bp->pf;
9535 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
9538 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
9539 req.port_id = cpu_to_le16(pf->port_id);
9540 mutex_lock(&bp->hwrm_cmd_lock);
9541 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9543 mutex_unlock(&bp->hwrm_cmd_lock);
9546 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
9549 bp->num_leds = resp->num_leds;
9550 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
9552 for (i = 0; i < bp->num_leds; i++) {
9553 struct bnxt_led_info *led = &bp->leds[i];
9554 __le16 caps = led->led_state_caps;
9556 if (!led->led_group_id ||
9557 !BNXT_LED_ALT_BLINK_CAP(caps)) {
9563 mutex_unlock(&bp->hwrm_cmd_lock);
9567 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
9569 struct hwrm_wol_filter_alloc_input req = {0};
9570 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
9573 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
9574 req.port_id = cpu_to_le16(bp->pf.port_id);
9575 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
9576 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
9577 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
9578 mutex_lock(&bp->hwrm_cmd_lock);
9579 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9581 bp->wol_filter_id = resp->wol_filter_id;
9582 mutex_unlock(&bp->hwrm_cmd_lock);
9586 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
9588 struct hwrm_wol_filter_free_input req = {0};
9590 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
9591 req.port_id = cpu_to_le16(bp->pf.port_id);
9592 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
9593 req.wol_filter_id = bp->wol_filter_id;
9594 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9597 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
9599 struct hwrm_wol_filter_qcfg_input req = {0};
9600 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
9601 u16 next_handle = 0;
9604 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
9605 req.port_id = cpu_to_le16(bp->pf.port_id);
9606 req.handle = cpu_to_le16(handle);
9607 mutex_lock(&bp->hwrm_cmd_lock);
9608 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9610 next_handle = le16_to_cpu(resp->next_handle);
9611 if (next_handle != 0) {
9612 if (resp->wol_type ==
9613 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
9615 bp->wol_filter_id = resp->wol_filter_id;
9619 mutex_unlock(&bp->hwrm_cmd_lock);
9623 static void bnxt_get_wol_settings(struct bnxt *bp)
9628 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
9632 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
9633 } while (handle && handle != 0xffff);
9636 #ifdef CONFIG_BNXT_HWMON
9637 static ssize_t bnxt_show_temp(struct device *dev,
9638 struct device_attribute *devattr, char *buf)
9640 struct hwrm_temp_monitor_query_input req = {0};
9641 struct hwrm_temp_monitor_query_output *resp;
9642 struct bnxt *bp = dev_get_drvdata(dev);
9646 resp = bp->hwrm_cmd_resp_addr;
9647 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9648 mutex_lock(&bp->hwrm_cmd_lock);
9649 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9651 len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
9652 mutex_unlock(&bp->hwrm_cmd_lock);
9657 static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
9659 static struct attribute *bnxt_attrs[] = {
9660 &sensor_dev_attr_temp1_input.dev_attr.attr,
9663 ATTRIBUTE_GROUPS(bnxt);
9665 static void bnxt_hwmon_close(struct bnxt *bp)
9667 if (bp->hwmon_dev) {
9668 hwmon_device_unregister(bp->hwmon_dev);
9669 bp->hwmon_dev = NULL;
9673 static void bnxt_hwmon_open(struct bnxt *bp)
9675 struct hwrm_temp_monitor_query_input req = {0};
9676 struct pci_dev *pdev = bp->pdev;
9679 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
9680 rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
9681 if (rc == -EACCES || rc == -EOPNOTSUPP) {
9682 bnxt_hwmon_close(bp);
9689 bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
9690 DRV_MODULE_NAME, bp,
9692 if (IS_ERR(bp->hwmon_dev)) {
9693 bp->hwmon_dev = NULL;
9694 dev_warn(&pdev->dev, "Cannot register hwmon device\n");
9698 static void bnxt_hwmon_close(struct bnxt *bp)
9702 static void bnxt_hwmon_open(struct bnxt *bp)
9707 static bool bnxt_eee_config_ok(struct bnxt *bp)
9709 struct ethtool_eee *eee = &bp->eee;
9710 struct bnxt_link_info *link_info = &bp->link_info;
9712 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
9715 if (eee->eee_enabled) {
9717 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
9719 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9720 eee->eee_enabled = 0;
9723 if (eee->advertised & ~advertising) {
9724 eee->advertised = advertising & eee->supported;
9731 static int bnxt_update_phy_setting(struct bnxt *bp)
9734 bool update_link = false;
9735 bool update_pause = false;
9736 bool update_eee = false;
9737 struct bnxt_link_info *link_info = &bp->link_info;
9739 rc = bnxt_update_link(bp, true);
9741 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
9745 if (!BNXT_SINGLE_PF(bp))
9748 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9749 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
9750 link_info->req_flow_ctrl)
9751 update_pause = true;
9752 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
9753 link_info->force_pause_setting != link_info->req_flow_ctrl)
9754 update_pause = true;
9755 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
9756 if (BNXT_AUTO_MODE(link_info->auto_mode))
9758 if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
9759 link_info->req_link_speed != link_info->force_link_speed)
9761 else if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
9762 link_info->req_link_speed != link_info->force_pam4_link_speed)
9764 if (link_info->req_duplex != link_info->duplex_setting)
9767 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
9769 if (link_info->advertising != link_info->auto_link_speeds ||
9770 link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
9774 /* The last close may have shutdown the link, so need to call
9775 * PHY_CFG to bring it back up.
9777 if (!bp->link_info.link_up)
9780 if (!bnxt_eee_config_ok(bp))
9784 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
9785 else if (update_pause)
9786 rc = bnxt_hwrm_set_pause(bp);
9788 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
9796 /* Common routine to pre-map certain register block to different GRC window.
9797 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
9798 * in PF and 3 windows in VF that can be customized to map in different
9801 static void bnxt_preset_reg_win(struct bnxt *bp)
9804 /* CAG registers map to GRC window #4 */
9805 writel(BNXT_CAG_REG_BASE,
9806 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
9810 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
9812 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9816 bnxt_preset_reg_win(bp);
9817 netif_carrier_off(bp->dev);
9819 /* Reserve rings now if none were reserved at driver probe. */
9820 rc = bnxt_init_dflt_ring_mode(bp);
9822 netdev_err(bp->dev, "Failed to reserve default rings at open\n");
9826 rc = bnxt_reserve_rings(bp, irq_re_init);
9829 if ((bp->flags & BNXT_FLAG_RFS) &&
9830 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
9831 /* disable RFS if falling back to INTA */
9832 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
9833 bp->flags &= ~BNXT_FLAG_RFS;
9836 rc = bnxt_alloc_mem(bp, irq_re_init);
9838 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9839 goto open_err_free_mem;
9844 rc = bnxt_request_irq(bp);
9846 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
9851 rc = bnxt_init_nic(bp, irq_re_init);
9853 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9857 bnxt_enable_napi(bp);
9858 bnxt_debug_dev_init(bp);
9861 mutex_lock(&bp->link_lock);
9862 rc = bnxt_update_phy_setting(bp);
9863 mutex_unlock(&bp->link_lock);
9865 netdev_warn(bp->dev, "failed to update phy settings\n");
9866 if (BNXT_SINGLE_PF(bp)) {
9867 bp->link_info.phy_retry = true;
9868 bp->link_info.phy_retry_expires =
9875 udp_tunnel_nic_reset_ntf(bp->dev);
9877 set_bit(BNXT_STATE_OPEN, &bp->state);
9878 bnxt_enable_int(bp);
9879 /* Enable TX queues */
9881 mod_timer(&bp->timer, jiffies + bp->current_interval);
9882 /* Poll link status and check for SFP+ module status */
9883 bnxt_get_port_module_status(bp);
9885 /* VF-reps may need to be re-opened after the PF is re-opened */
9887 bnxt_vf_reps_open(bp);
9896 bnxt_free_mem(bp, true);
9900 /* rtnl_lock held */
9901 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
9905 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
9908 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
9910 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
9916 /* rtnl_lock held, open the NIC half way by allocating all resources, but
9917 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
9920 int bnxt_half_open_nic(struct bnxt *bp)
9924 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9925 netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
9930 rc = bnxt_alloc_mem(bp, false);
9932 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
9935 rc = bnxt_init_nic(bp, false);
9937 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
9944 bnxt_free_mem(bp, false);
9949 /* rtnl_lock held, this call can only be made after a previous successful
9950 * call to bnxt_half_open_nic().
9952 void bnxt_half_close_nic(struct bnxt *bp)
9954 bnxt_hwrm_resource_free(bp, false, false);
9956 bnxt_free_mem(bp, false);
9959 static void bnxt_reenable_sriov(struct bnxt *bp)
9962 struct bnxt_pf_info *pf = &bp->pf;
9963 int n = pf->active_vfs;
9966 bnxt_cfg_hw_sriov(bp, &n, true);
9970 static int bnxt_open(struct net_device *dev)
9972 struct bnxt *bp = netdev_priv(dev);
9975 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
9976 netdev_err(bp->dev, "A previous firmware reset did not complete, aborting\n");
9980 rc = bnxt_hwrm_if_change(bp, true);
9983 rc = __bnxt_open_nic(bp, true, true);
9985 bnxt_hwrm_if_change(bp, false);
9987 if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
9988 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
9989 bnxt_ulp_start(bp, 0);
9990 bnxt_reenable_sriov(bp);
9993 bnxt_hwmon_open(bp);
9999 static bool bnxt_drv_busy(struct bnxt *bp)
10001 return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
10002 test_bit(BNXT_STATE_READ_STATS, &bp->state));
10005 static void bnxt_get_ring_stats(struct bnxt *bp,
10006 struct rtnl_link_stats64 *stats);
10008 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
10011 /* Close the VF-reps before closing PF */
10013 bnxt_vf_reps_close(bp);
10015 /* Change device state to avoid TX queue wake up's */
10016 bnxt_tx_disable(bp);
10018 clear_bit(BNXT_STATE_OPEN, &bp->state);
10019 smp_mb__after_atomic();
10020 while (bnxt_drv_busy(bp))
10023 /* Flush rings and and disable interrupts */
10024 bnxt_shutdown_nic(bp, irq_re_init);
10026 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
10028 bnxt_debug_dev_exit(bp);
10029 bnxt_disable_napi(bp);
10030 del_timer_sync(&bp->timer);
10031 bnxt_free_skbs(bp);
10033 /* Save ring stats before shutdown */
10034 if (bp->bnapi && irq_re_init)
10035 bnxt_get_ring_stats(bp, &bp->net_stats_prev);
10040 bnxt_free_mem(bp, irq_re_init);
10043 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
10047 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
10048 /* If we get here, it means firmware reset is in progress
10049 * while we are trying to close. We can safely proceed with
10050 * the close because we are holding rtnl_lock(). Some firmware
10051 * messages may fail as we proceed to close. We set the
10052 * ABORT_ERR flag here so that the FW reset thread will later
10053 * abort when it gets the rtnl_lock() and sees the flag.
10055 netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
10056 set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
10059 #ifdef CONFIG_BNXT_SRIOV
10060 if (bp->sriov_cfg) {
10061 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
10063 BNXT_SRIOV_CFG_WAIT_TMO);
10065 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
10068 __bnxt_close_nic(bp, irq_re_init, link_re_init);
10072 static int bnxt_close(struct net_device *dev)
10074 struct bnxt *bp = netdev_priv(dev);
10076 bnxt_hwmon_close(bp);
10077 bnxt_close_nic(bp, true, true);
10078 bnxt_hwrm_shutdown_link(bp);
10079 bnxt_hwrm_if_change(bp, false);
10083 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
10086 struct hwrm_port_phy_mdio_read_output *resp = bp->hwrm_cmd_resp_addr;
10087 struct hwrm_port_phy_mdio_read_input req = {0};
10090 if (bp->hwrm_spec_code < 0x10a00)
10091 return -EOPNOTSUPP;
10093 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_READ, -1, -1);
10094 req.port_id = cpu_to_le16(bp->pf.port_id);
10095 req.phy_addr = phy_addr;
10096 req.reg_addr = cpu_to_le16(reg & 0x1f);
10097 if (mdio_phy_id_is_c45(phy_addr)) {
10099 req.phy_addr = mdio_phy_id_prtad(phy_addr);
10100 req.dev_addr = mdio_phy_id_devad(phy_addr);
10101 req.reg_addr = cpu_to_le16(reg);
10104 mutex_lock(&bp->hwrm_cmd_lock);
10105 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10107 *val = le16_to_cpu(resp->reg_data);
10108 mutex_unlock(&bp->hwrm_cmd_lock);
10112 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
10115 struct hwrm_port_phy_mdio_write_input req = {0};
10117 if (bp->hwrm_spec_code < 0x10a00)
10118 return -EOPNOTSUPP;
10120 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_MDIO_WRITE, -1, -1);
10121 req.port_id = cpu_to_le16(bp->pf.port_id);
10122 req.phy_addr = phy_addr;
10123 req.reg_addr = cpu_to_le16(reg & 0x1f);
10124 if (mdio_phy_id_is_c45(phy_addr)) {
10126 req.phy_addr = mdio_phy_id_prtad(phy_addr);
10127 req.dev_addr = mdio_phy_id_devad(phy_addr);
10128 req.reg_addr = cpu_to_le16(reg);
10130 req.reg_data = cpu_to_le16(val);
10132 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10135 /* rtnl_lock held */
10136 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10138 struct mii_ioctl_data *mdio = if_mii(ifr);
10139 struct bnxt *bp = netdev_priv(dev);
10144 mdio->phy_id = bp->link_info.phy_addr;
10147 case SIOCGMIIREG: {
10148 u16 mii_regval = 0;
10150 if (!netif_running(dev))
10153 rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
10155 mdio->val_out = mii_regval;
10160 if (!netif_running(dev))
10163 return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
10170 return -EOPNOTSUPP;
10173 static void bnxt_get_ring_stats(struct bnxt *bp,
10174 struct rtnl_link_stats64 *stats)
10178 for (i = 0; i < bp->cp_nr_rings; i++) {
10179 struct bnxt_napi *bnapi = bp->bnapi[i];
10180 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10181 u64 *sw = cpr->stats.sw_stats;
10183 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
10184 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10185 stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
10187 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
10188 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
10189 stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
10191 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
10192 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
10193 stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
10195 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
10196 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
10197 stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
10199 stats->rx_missed_errors +=
10200 BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
10202 stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
10204 stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
10208 static void bnxt_add_prev_stats(struct bnxt *bp,
10209 struct rtnl_link_stats64 *stats)
10211 struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
10213 stats->rx_packets += prev_stats->rx_packets;
10214 stats->tx_packets += prev_stats->tx_packets;
10215 stats->rx_bytes += prev_stats->rx_bytes;
10216 stats->tx_bytes += prev_stats->tx_bytes;
10217 stats->rx_missed_errors += prev_stats->rx_missed_errors;
10218 stats->multicast += prev_stats->multicast;
10219 stats->tx_dropped += prev_stats->tx_dropped;
10223 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
10225 struct bnxt *bp = netdev_priv(dev);
10227 set_bit(BNXT_STATE_READ_STATS, &bp->state);
10228 /* Make sure bnxt_close_nic() sees that we are reading stats before
10229 * we check the BNXT_STATE_OPEN flag.
10231 smp_mb__after_atomic();
10232 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10233 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10234 *stats = bp->net_stats_prev;
10238 bnxt_get_ring_stats(bp, stats);
10239 bnxt_add_prev_stats(bp, stats);
10241 if (bp->flags & BNXT_FLAG_PORT_STATS) {
10242 u64 *rx = bp->port_stats.sw_stats;
10243 u64 *tx = bp->port_stats.sw_stats +
10244 BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10246 stats->rx_crc_errors =
10247 BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
10248 stats->rx_frame_errors =
10249 BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
10250 stats->rx_length_errors =
10251 BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
10252 BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
10253 BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
10255 BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
10256 BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
10257 stats->collisions =
10258 BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
10259 stats->tx_fifo_errors =
10260 BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
10261 stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
10263 clear_bit(BNXT_STATE_READ_STATS, &bp->state);
10266 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
10268 struct net_device *dev = bp->dev;
10269 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10270 struct netdev_hw_addr *ha;
10273 bool update = false;
10276 netdev_for_each_mc_addr(ha, dev) {
10277 if (mc_count >= BNXT_MAX_MC_ADDRS) {
10278 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10279 vnic->mc_list_count = 0;
10283 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
10284 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
10291 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
10293 if (mc_count != vnic->mc_list_count) {
10294 vnic->mc_list_count = mc_count;
10300 static bool bnxt_uc_list_updated(struct bnxt *bp)
10302 struct net_device *dev = bp->dev;
10303 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10304 struct netdev_hw_addr *ha;
10307 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
10310 netdev_for_each_uc_addr(ha, dev) {
10311 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
10319 static void bnxt_set_rx_mode(struct net_device *dev)
10321 struct bnxt *bp = netdev_priv(dev);
10322 struct bnxt_vnic_info *vnic;
10323 bool mc_update = false;
10327 if (!test_bit(BNXT_STATE_OPEN, &bp->state))
10330 vnic = &bp->vnic_info[0];
10331 mask = vnic->rx_mask;
10332 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
10333 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
10334 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
10335 CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
10337 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
10338 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10340 uc_update = bnxt_uc_list_updated(bp);
10342 if (dev->flags & IFF_BROADCAST)
10343 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10344 if (dev->flags & IFF_ALLMULTI) {
10345 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10346 vnic->mc_list_count = 0;
10348 mc_update = bnxt_mc_list_updated(bp, &mask);
10351 if (mask != vnic->rx_mask || uc_update || mc_update) {
10352 vnic->rx_mask = mask;
10354 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
10355 bnxt_queue_sp_work(bp);
10359 static int bnxt_cfg_rx_mode(struct bnxt *bp)
10361 struct net_device *dev = bp->dev;
10362 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
10363 struct netdev_hw_addr *ha;
10364 int i, off = 0, rc;
10367 netif_addr_lock_bh(dev);
10368 uc_update = bnxt_uc_list_updated(bp);
10369 netif_addr_unlock_bh(dev);
10374 mutex_lock(&bp->hwrm_cmd_lock);
10375 for (i = 1; i < vnic->uc_filter_count; i++) {
10376 struct hwrm_cfa_l2_filter_free_input req = {0};
10378 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
10381 req.l2_filter_id = vnic->fw_l2_filter_id[i];
10383 rc = _hwrm_send_message(bp, &req, sizeof(req),
10386 mutex_unlock(&bp->hwrm_cmd_lock);
10388 vnic->uc_filter_count = 1;
10390 netif_addr_lock_bh(dev);
10391 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
10392 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10394 netdev_for_each_uc_addr(ha, dev) {
10395 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
10397 vnic->uc_filter_count++;
10400 netif_addr_unlock_bh(dev);
10402 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
10403 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
10405 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
10407 vnic->uc_filter_count = i;
10413 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10414 if (rc && vnic->mc_list_count) {
10415 netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
10417 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10418 vnic->mc_list_count = 0;
10419 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
10422 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
10428 static bool bnxt_can_reserve_rings(struct bnxt *bp)
10430 #ifdef CONFIG_BNXT_SRIOV
10431 if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
10432 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
10434 /* No minimum rings were provisioned by the PF. Don't
10435 * reserve rings by default when device is down.
10437 if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
10440 if (!netif_running(bp->dev))
10447 /* If the chip and firmware supports RFS */
10448 static bool bnxt_rfs_supported(struct bnxt *bp)
10450 if (bp->flags & BNXT_FLAG_CHIP_P5) {
10451 if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
10455 /* 212 firmware is broken for aRFS */
10456 if (BNXT_FW_MAJ(bp) == 212)
10458 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
10460 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10465 /* If runtime conditions support RFS */
10466 static bool bnxt_rfs_capable(struct bnxt *bp)
10468 #ifdef CONFIG_RFS_ACCEL
10469 int vnics, max_vnics, max_rss_ctxs;
10471 if (bp->flags & BNXT_FLAG_CHIP_P5)
10472 return bnxt_rfs_supported(bp);
10473 if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
10476 vnics = 1 + bp->rx_nr_rings;
10477 max_vnics = bnxt_get_max_func_vnics(bp);
10478 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
10480 /* RSS contexts not a limiting factor */
10481 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
10482 max_rss_ctxs = max_vnics;
10483 if (vnics > max_vnics || vnics > max_rss_ctxs) {
10484 if (bp->rx_nr_rings > 1)
10485 netdev_warn(bp->dev,
10486 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
10487 min(max_rss_ctxs - 1, max_vnics - 1));
10491 if (!BNXT_NEW_RM(bp))
10494 if (vnics == bp->hw_resc.resv_vnics)
10497 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, vnics);
10498 if (vnics <= bp->hw_resc.resv_vnics)
10501 netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
10502 bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 0, 1);
10509 static netdev_features_t bnxt_fix_features(struct net_device *dev,
10510 netdev_features_t features)
10512 struct bnxt *bp = netdev_priv(dev);
10513 netdev_features_t vlan_features;
10515 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
10516 features &= ~NETIF_F_NTUPLE;
10518 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10519 features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
10521 if (!(features & NETIF_F_GRO))
10522 features &= ~NETIF_F_GRO_HW;
10524 if (features & NETIF_F_GRO_HW)
10525 features &= ~NETIF_F_LRO;
10527 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
10528 * turned on or off together.
10530 vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
10531 if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
10532 if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10533 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10534 else if (vlan_features)
10535 features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
10537 #ifdef CONFIG_BNXT_SRIOV
10538 if (BNXT_VF(bp) && bp->vf.vlan)
10539 features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
10544 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
10546 struct bnxt *bp = netdev_priv(dev);
10547 u32 flags = bp->flags;
10550 bool re_init = false;
10551 bool update_tpa = false;
10553 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
10554 if (features & NETIF_F_GRO_HW)
10555 flags |= BNXT_FLAG_GRO;
10556 else if (features & NETIF_F_LRO)
10557 flags |= BNXT_FLAG_LRO;
10559 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
10560 flags &= ~BNXT_FLAG_TPA;
10562 if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
10563 flags |= BNXT_FLAG_STRIP_VLAN;
10565 if (features & NETIF_F_NTUPLE)
10566 flags |= BNXT_FLAG_RFS;
10568 changes = flags ^ bp->flags;
10569 if (changes & BNXT_FLAG_TPA) {
10571 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
10572 (flags & BNXT_FLAG_TPA) == 0 ||
10573 (bp->flags & BNXT_FLAG_CHIP_P5))
10577 if (changes & ~BNXT_FLAG_TPA)
10580 if (flags != bp->flags) {
10581 u32 old_flags = bp->flags;
10583 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10586 bnxt_set_ring_params(bp);
10591 bnxt_close_nic(bp, false, false);
10594 bnxt_set_ring_params(bp);
10596 return bnxt_open_nic(bp, false, false);
10600 rc = bnxt_set_tpa(bp,
10601 (flags & BNXT_FLAG_TPA) ?
10604 bp->flags = old_flags;
10610 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
10613 struct hwrm_dbg_read_direct_output *resp = bp->hwrm_cmd_resp_addr;
10614 struct hwrm_dbg_read_direct_input req = {0};
10615 __le32 *dbg_reg_buf;
10616 dma_addr_t mapping;
10619 dbg_reg_buf = dma_alloc_coherent(&bp->pdev->dev, num_words * 4,
10620 &mapping, GFP_KERNEL);
10623 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_READ_DIRECT, -1, -1);
10624 req.host_dest_addr = cpu_to_le64(mapping);
10625 req.read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
10626 req.read_len32 = cpu_to_le32(num_words);
10627 mutex_lock(&bp->hwrm_cmd_lock);
10628 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10629 if (rc || resp->error_code) {
10631 goto dbg_rd_reg_exit;
10633 for (i = 0; i < num_words; i++)
10634 reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
10637 mutex_unlock(&bp->hwrm_cmd_lock);
10638 dma_free_coherent(&bp->pdev->dev, num_words * 4, dbg_reg_buf, mapping);
10642 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
10643 u32 ring_id, u32 *prod, u32 *cons)
10645 struct hwrm_dbg_ring_info_get_output *resp = bp->hwrm_cmd_resp_addr;
10646 struct hwrm_dbg_ring_info_get_input req = {0};
10649 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_DBG_RING_INFO_GET, -1, -1);
10650 req.ring_type = ring_type;
10651 req.fw_ring_id = cpu_to_le32(ring_id);
10652 mutex_lock(&bp->hwrm_cmd_lock);
10653 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
10655 *prod = le32_to_cpu(resp->producer_index);
10656 *cons = le32_to_cpu(resp->consumer_index);
10658 mutex_unlock(&bp->hwrm_cmd_lock);
10662 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
10664 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
10665 int i = bnapi->index;
10670 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
10671 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
10675 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
10677 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
10678 int i = bnapi->index;
10683 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",
10684 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
10685 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
10686 rxr->rx_sw_agg_prod);
10689 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
10691 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
10692 int i = bnapi->index;
10694 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
10695 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
10698 static void bnxt_dbg_dump_states(struct bnxt *bp)
10701 struct bnxt_napi *bnapi;
10703 for (i = 0; i < bp->cp_nr_rings; i++) {
10704 bnapi = bp->bnapi[i];
10705 if (netif_msg_drv(bp)) {
10706 bnxt_dump_tx_sw_state(bnapi);
10707 bnxt_dump_rx_sw_state(bnapi);
10708 bnxt_dump_cp_sw_state(bnapi);
10713 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
10715 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
10716 struct hwrm_ring_reset_input req = {0};
10717 struct bnxt_napi *bnapi = rxr->bnapi;
10718 struct bnxt_cp_ring_info *cpr;
10721 cpr = &bnapi->cp_ring;
10722 cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
10723 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_RESET, cp_ring_id, -1);
10724 req.ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
10725 req.ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
10726 return hwrm_send_message_silent(bp, &req, sizeof(req),
10730 static void bnxt_reset_task(struct bnxt *bp, bool silent)
10733 bnxt_dbg_dump_states(bp);
10734 if (netif_running(bp->dev)) {
10738 bnxt_close_nic(bp, false, false);
10739 bnxt_open_nic(bp, false, false);
10742 bnxt_close_nic(bp, true, false);
10743 rc = bnxt_open_nic(bp, true, false);
10744 bnxt_ulp_start(bp, rc);
10749 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
10751 struct bnxt *bp = netdev_priv(dev);
10753 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
10754 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
10755 bnxt_queue_sp_work(bp);
10758 static void bnxt_fw_health_check(struct bnxt *bp)
10760 struct bnxt_fw_health *fw_health = bp->fw_health;
10763 if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10766 /* Make sure it is enabled before checking the tmr_counter. */
10768 if (fw_health->tmr_counter) {
10769 fw_health->tmr_counter--;
10773 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10774 if (val == fw_health->last_fw_heartbeat)
10777 fw_health->last_fw_heartbeat = val;
10779 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10780 if (val != fw_health->last_fw_reset_cnt)
10783 fw_health->tmr_counter = fw_health->tmr_multiplier;
10787 set_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event);
10788 bnxt_queue_sp_work(bp);
10791 static void bnxt_timer(struct timer_list *t)
10793 struct bnxt *bp = from_timer(bp, t, timer);
10794 struct net_device *dev = bp->dev;
10796 if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
10799 if (atomic_read(&bp->intr_sem) != 0)
10800 goto bnxt_restart_timer;
10802 if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
10803 bnxt_fw_health_check(bp);
10805 if (bp->link_info.link_up && bp->stats_coal_ticks) {
10806 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
10807 bnxt_queue_sp_work(bp);
10810 if (bnxt_tc_flower_enabled(bp)) {
10811 set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
10812 bnxt_queue_sp_work(bp);
10815 #ifdef CONFIG_RFS_ACCEL
10816 if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count) {
10817 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
10818 bnxt_queue_sp_work(bp);
10820 #endif /*CONFIG_RFS_ACCEL*/
10822 if (bp->link_info.phy_retry) {
10823 if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
10824 bp->link_info.phy_retry = false;
10825 netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
10827 set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
10828 bnxt_queue_sp_work(bp);
10832 if ((bp->flags & BNXT_FLAG_CHIP_P5) && !bp->chip_rev &&
10833 netif_carrier_ok(dev)) {
10834 set_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event);
10835 bnxt_queue_sp_work(bp);
10837 bnxt_restart_timer:
10838 mod_timer(&bp->timer, jiffies + bp->current_interval);
10841 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
10843 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
10844 * set. If the device is being closed, bnxt_close() may be holding
10845 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
10846 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
10848 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10852 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
10854 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
10858 /* Only called from bnxt_sp_task() */
10859 static void bnxt_reset(struct bnxt *bp, bool silent)
10861 bnxt_rtnl_lock_sp(bp);
10862 if (test_bit(BNXT_STATE_OPEN, &bp->state))
10863 bnxt_reset_task(bp, silent);
10864 bnxt_rtnl_unlock_sp(bp);
10867 /* Only called from bnxt_sp_task() */
10868 static void bnxt_rx_ring_reset(struct bnxt *bp)
10872 bnxt_rtnl_lock_sp(bp);
10873 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
10874 bnxt_rtnl_unlock_sp(bp);
10877 /* Disable and flush TPA before resetting the RX ring */
10878 if (bp->flags & BNXT_FLAG_TPA)
10879 bnxt_set_tpa(bp, false);
10880 for (i = 0; i < bp->rx_nr_rings; i++) {
10881 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
10882 struct bnxt_cp_ring_info *cpr;
10885 if (!rxr->bnapi->in_reset)
10888 rc = bnxt_hwrm_rx_ring_reset(bp, i);
10890 if (rc == -EINVAL || rc == -EOPNOTSUPP)
10891 netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
10893 netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
10895 bnxt_reset_task(bp, true);
10898 bnxt_free_one_rx_ring_skbs(bp, i);
10900 rxr->rx_agg_prod = 0;
10901 rxr->rx_sw_agg_prod = 0;
10902 rxr->rx_next_cons = 0;
10903 rxr->bnapi->in_reset = false;
10904 bnxt_alloc_one_rx_ring(bp, i);
10905 cpr = &rxr->bnapi->cp_ring;
10906 cpr->sw_stats.rx.rx_resets++;
10907 if (bp->flags & BNXT_FLAG_AGG_RINGS)
10908 bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
10909 bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
10911 if (bp->flags & BNXT_FLAG_TPA)
10912 bnxt_set_tpa(bp, true);
10913 bnxt_rtnl_unlock_sp(bp);
10916 static void bnxt_fw_reset_close(struct bnxt *bp)
10919 /* When firmware is fatal state, disable PCI device to prevent
10920 * any potential bad DMAs before freeing kernel memory.
10922 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
10923 pci_disable_device(bp->pdev);
10924 __bnxt_close_nic(bp, true, false);
10925 bnxt_clear_int_mode(bp);
10926 bnxt_hwrm_func_drv_unrgtr(bp);
10927 if (pci_is_enabled(bp->pdev))
10928 pci_disable_device(bp->pdev);
10929 bnxt_free_ctx_mem(bp);
10934 static bool is_bnxt_fw_ok(struct bnxt *bp)
10936 struct bnxt_fw_health *fw_health = bp->fw_health;
10937 bool no_heartbeat = false, has_reset = false;
10940 val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
10941 if (val == fw_health->last_fw_heartbeat)
10942 no_heartbeat = true;
10944 val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
10945 if (val != fw_health->last_fw_reset_cnt)
10948 if (!no_heartbeat && has_reset)
10954 /* rtnl_lock is acquired before calling this function */
10955 static void bnxt_force_fw_reset(struct bnxt *bp)
10957 struct bnxt_fw_health *fw_health = bp->fw_health;
10960 if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
10961 test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
10964 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
10965 bnxt_fw_reset_close(bp);
10966 wait_dsecs = fw_health->master_func_wait_dsecs;
10967 if (fw_health->master) {
10968 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
10970 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
10972 bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
10973 wait_dsecs = fw_health->normal_func_wait_dsecs;
10974 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
10977 bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
10978 bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
10979 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
10982 void bnxt_fw_exception(struct bnxt *bp)
10984 netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
10985 set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
10986 bnxt_rtnl_lock_sp(bp);
10987 bnxt_force_fw_reset(bp);
10988 bnxt_rtnl_unlock_sp(bp);
10991 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
10994 static int bnxt_get_registered_vfs(struct bnxt *bp)
10996 #ifdef CONFIG_BNXT_SRIOV
11002 rc = bnxt_hwrm_func_qcfg(bp);
11004 netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
11007 if (bp->pf.registered_vfs)
11008 return bp->pf.registered_vfs;
11015 void bnxt_fw_reset(struct bnxt *bp)
11017 bnxt_rtnl_lock_sp(bp);
11018 if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
11019 !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11022 set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11023 if (bp->pf.active_vfs &&
11024 !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
11025 n = bnxt_get_registered_vfs(bp);
11027 netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
11029 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11030 dev_close(bp->dev);
11031 goto fw_reset_exit;
11032 } else if (n > 0) {
11033 u16 vf_tmo_dsecs = n * 10;
11035 if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
11036 bp->fw_reset_max_dsecs = vf_tmo_dsecs;
11037 bp->fw_reset_state =
11038 BNXT_FW_RESET_STATE_POLL_VF;
11039 bnxt_queue_fw_reset_work(bp, HZ / 10);
11040 goto fw_reset_exit;
11042 bnxt_fw_reset_close(bp);
11043 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11044 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11047 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11048 tmo = bp->fw_reset_min_dsecs * HZ / 10;
11050 bnxt_queue_fw_reset_work(bp, tmo);
11053 bnxt_rtnl_unlock_sp(bp);
11056 static void bnxt_chk_missed_irq(struct bnxt *bp)
11060 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
11063 for (i = 0; i < bp->cp_nr_rings; i++) {
11064 struct bnxt_napi *bnapi = bp->bnapi[i];
11065 struct bnxt_cp_ring_info *cpr;
11072 cpr = &bnapi->cp_ring;
11073 for (j = 0; j < 2; j++) {
11074 struct bnxt_cp_ring_info *cpr2 = cpr->cp_ring_arr[j];
11077 if (!cpr2 || cpr2->has_more_work ||
11078 !bnxt_has_work(bp, cpr2))
11081 if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
11082 cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
11085 fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
11086 bnxt_dbg_hwrm_ring_info_get(bp,
11087 DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
11088 fw_ring_id, &val[0], &val[1]);
11089 cpr->sw_stats.cmn.missed_irqs++;
11094 static void bnxt_cfg_ntp_filters(struct bnxt *);
11096 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
11098 struct bnxt_link_info *link_info = &bp->link_info;
11100 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
11101 link_info->autoneg = BNXT_AUTONEG_SPEED;
11102 if (bp->hwrm_spec_code >= 0x10201) {
11103 if (link_info->auto_pause_setting &
11104 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
11105 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11107 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
11109 link_info->advertising = link_info->auto_link_speeds;
11110 link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
11112 link_info->req_link_speed = link_info->force_link_speed;
11113 link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
11114 if (link_info->force_pam4_link_speed) {
11115 link_info->req_link_speed =
11116 link_info->force_pam4_link_speed;
11117 link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
11119 link_info->req_duplex = link_info->duplex_setting;
11121 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
11122 link_info->req_flow_ctrl =
11123 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
11125 link_info->req_flow_ctrl = link_info->force_pause_setting;
11128 static void bnxt_sp_task(struct work_struct *work)
11130 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
11132 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11133 smp_mb__after_atomic();
11134 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
11135 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11139 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
11140 bnxt_cfg_rx_mode(bp);
11142 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
11143 bnxt_cfg_ntp_filters(bp);
11144 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
11145 bnxt_hwrm_exec_fwd_req(bp);
11146 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
11147 netdev_info(bp->dev, "Receive PF driver unload event!\n");
11148 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
11149 bnxt_hwrm_port_qstats(bp, 0);
11150 bnxt_hwrm_port_qstats_ext(bp, 0);
11151 bnxt_accumulate_all_stats(bp);
11154 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
11157 mutex_lock(&bp->link_lock);
11158 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
11160 bnxt_hwrm_phy_qcaps(bp);
11162 rc = bnxt_update_link(bp, true);
11164 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
11167 if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
11169 bnxt_init_ethtool_link_settings(bp);
11170 mutex_unlock(&bp->link_lock);
11172 if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
11175 mutex_lock(&bp->link_lock);
11176 rc = bnxt_update_phy_setting(bp);
11177 mutex_unlock(&bp->link_lock);
11179 netdev_warn(bp->dev, "update phy settings retry failed\n");
11181 bp->link_info.phy_retry = false;
11182 netdev_info(bp->dev, "update phy settings retry succeeded\n");
11185 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
11186 mutex_lock(&bp->link_lock);
11187 bnxt_get_port_module_status(bp);
11188 mutex_unlock(&bp->link_lock);
11191 if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
11192 bnxt_tc_flow_stats_work(bp);
11194 if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
11195 bnxt_chk_missed_irq(bp);
11197 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
11198 * must be the last functions to be called before exiting.
11200 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
11201 bnxt_reset(bp, false);
11203 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
11204 bnxt_reset(bp, true);
11206 if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
11207 bnxt_rx_ring_reset(bp);
11209 if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event))
11210 bnxt_devlink_health_report(bp, BNXT_FW_RESET_NOTIFY_SP_EVENT);
11212 if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
11213 if (!is_bnxt_fw_ok(bp))
11214 bnxt_devlink_health_report(bp,
11215 BNXT_FW_EXCEPTION_SP_EVENT);
11218 smp_mb__before_atomic();
11219 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
11222 /* Under rtnl_lock */
11223 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
11226 int max_rx, max_tx, tx_sets = 1;
11227 int tx_rings_needed, stats;
11234 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
11241 tx_rings_needed = tx * tx_sets + tx_xdp;
11242 if (max_tx < tx_rings_needed)
11246 if ((bp->flags & (BNXT_FLAG_RFS | BNXT_FLAG_CHIP_P5)) == BNXT_FLAG_RFS)
11249 if (bp->flags & BNXT_FLAG_AGG_RINGS)
11251 cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
11253 if (BNXT_NEW_RM(bp)) {
11254 cp += bnxt_get_ulp_msix_num(bp);
11255 stats += bnxt_get_ulp_stat_ctxs(bp);
11257 return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
11261 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
11264 pci_iounmap(pdev, bp->bar2);
11269 pci_iounmap(pdev, bp->bar1);
11274 pci_iounmap(pdev, bp->bar0);
11279 static void bnxt_cleanup_pci(struct bnxt *bp)
11281 bnxt_unmap_bars(bp, bp->pdev);
11282 pci_release_regions(bp->pdev);
11283 if (pci_is_enabled(bp->pdev))
11284 pci_disable_device(bp->pdev);
11287 static void bnxt_init_dflt_coal(struct bnxt *bp)
11289 struct bnxt_coal *coal;
11291 /* Tick values in micro seconds.
11292 * 1 coal_buf x bufs_per_record = 1 completion record.
11294 coal = &bp->rx_coal;
11295 coal->coal_ticks = 10;
11296 coal->coal_bufs = 30;
11297 coal->coal_ticks_irq = 1;
11298 coal->coal_bufs_irq = 2;
11299 coal->idle_thresh = 50;
11300 coal->bufs_per_record = 2;
11301 coal->budget = 64; /* NAPI budget */
11303 coal = &bp->tx_coal;
11304 coal->coal_ticks = 28;
11305 coal->coal_bufs = 30;
11306 coal->coal_ticks_irq = 2;
11307 coal->coal_bufs_irq = 2;
11308 coal->bufs_per_record = 1;
11310 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
11313 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11315 #ifdef CONFIG_TEE_BNXT_FW
11316 int rc = tee_bnxt_fw_load();
11319 netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11323 netdev_err(bp->dev, "OP-TEE not supported\n");
11328 static int bnxt_fw_init_one_p1(struct bnxt *bp)
11333 rc = bnxt_hwrm_ver_get(bp);
11334 /* FW may be unresponsive after FLR. FLR must complete within 100 msec
11335 * so wait before continuing with recovery.
11339 bnxt_try_map_fw_health_reg(bp);
11341 if (bp->fw_health && bp->fw_health->status_reliable) {
11342 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11344 netdev_err(bp->dev,
11345 "Firmware not responding, status: 0x%x\n",
11347 if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11348 netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11349 rc = bnxt_fw_reset_via_optee(bp);
11351 rc = bnxt_hwrm_ver_get(bp);
11358 if (bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL) {
11359 rc = bnxt_alloc_kong_hwrm_resources(bp);
11361 bp->fw_cap &= ~BNXT_FW_CAP_KONG_MB_CHNL;
11364 if ((bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) ||
11365 bp->hwrm_max_ext_req_len > BNXT_HWRM_MAX_REQ_LEN) {
11366 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
11370 bnxt_nvm_cfg_ver_get(bp);
11372 rc = bnxt_hwrm_func_reset(bp);
11376 bnxt_hwrm_fw_set_time(bp);
11380 static int bnxt_fw_init_one_p2(struct bnxt *bp)
11384 /* Get the MAX capabilities for this function */
11385 rc = bnxt_hwrm_func_qcaps(bp);
11387 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
11392 rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
11394 netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
11397 if (bnxt_alloc_fw_health(bp)) {
11398 netdev_warn(bp->dev, "no memory for firmware error recovery\n");
11400 rc = bnxt_hwrm_error_recovery_qcfg(bp);
11402 netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
11406 rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
11410 bnxt_hwrm_func_qcfg(bp);
11411 bnxt_hwrm_vnic_qcaps(bp);
11412 bnxt_hwrm_port_led_qcaps(bp);
11413 bnxt_ethtool_init(bp);
11418 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
11420 bp->flags &= ~BNXT_FLAG_UDP_RSS_CAP;
11421 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
11422 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
11423 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
11424 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
11425 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
11426 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
11427 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
11428 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
11432 static void bnxt_set_dflt_rfs(struct bnxt *bp)
11434 struct net_device *dev = bp->dev;
11436 dev->hw_features &= ~NETIF_F_NTUPLE;
11437 dev->features &= ~NETIF_F_NTUPLE;
11438 bp->flags &= ~BNXT_FLAG_RFS;
11439 if (bnxt_rfs_supported(bp)) {
11440 dev->hw_features |= NETIF_F_NTUPLE;
11441 if (bnxt_rfs_capable(bp)) {
11442 bp->flags |= BNXT_FLAG_RFS;
11443 dev->features |= NETIF_F_NTUPLE;
11448 static void bnxt_fw_init_one_p3(struct bnxt *bp)
11450 struct pci_dev *pdev = bp->pdev;
11452 bnxt_set_dflt_rss_hash_type(bp);
11453 bnxt_set_dflt_rfs(bp);
11455 bnxt_get_wol_settings(bp);
11456 if (bp->flags & BNXT_FLAG_WOL_CAP)
11457 device_set_wakeup_enable(&pdev->dev, bp->wol);
11459 device_set_wakeup_capable(&pdev->dev, false);
11461 bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
11462 bnxt_hwrm_coal_params_qcaps(bp);
11465 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
11467 static int bnxt_fw_init_one(struct bnxt *bp)
11471 rc = bnxt_fw_init_one_p1(bp);
11473 netdev_err(bp->dev, "Firmware init phase 1 failed\n");
11476 rc = bnxt_fw_init_one_p2(bp);
11478 netdev_err(bp->dev, "Firmware init phase 2 failed\n");
11481 rc = bnxt_probe_phy(bp, false);
11484 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
11488 /* In case fw capabilities have changed, destroy the unneeded
11489 * reporters and create newly capable ones.
11491 bnxt_dl_fw_reporters_destroy(bp, false);
11492 bnxt_dl_fw_reporters_create(bp);
11493 bnxt_fw_init_one_p3(bp);
11497 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
11499 struct bnxt_fw_health *fw_health = bp->fw_health;
11500 u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
11501 u32 val = fw_health->fw_reset_seq_vals[reg_idx];
11502 u32 reg_type, reg_off, delay_msecs;
11504 delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
11505 reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
11506 reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
11507 switch (reg_type) {
11508 case BNXT_FW_HEALTH_REG_TYPE_CFG:
11509 pci_write_config_dword(bp->pdev, reg_off, val);
11511 case BNXT_FW_HEALTH_REG_TYPE_GRC:
11512 writel(reg_off & BNXT_GRC_BASE_MASK,
11513 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
11514 reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
11516 case BNXT_FW_HEALTH_REG_TYPE_BAR0:
11517 writel(val, bp->bar0 + reg_off);
11519 case BNXT_FW_HEALTH_REG_TYPE_BAR1:
11520 writel(val, bp->bar1 + reg_off);
11524 pci_read_config_dword(bp->pdev, 0, &val);
11525 msleep(delay_msecs);
11529 static void bnxt_reset_all(struct bnxt *bp)
11531 struct bnxt_fw_health *fw_health = bp->fw_health;
11534 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11535 bnxt_fw_reset_via_optee(bp);
11536 bp->fw_reset_timestamp = jiffies;
11540 if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
11541 for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
11542 bnxt_fw_reset_writel(bp, i);
11543 } else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
11544 struct hwrm_fw_reset_input req = {0};
11546 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_RESET, -1, -1);
11547 req.resp_addr = cpu_to_le64(bp->hwrm_cmd_kong_resp_dma_addr);
11548 req.embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
11549 req.selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
11550 req.flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
11551 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
11553 netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
11555 bp->fw_reset_timestamp = jiffies;
11558 static void bnxt_fw_reset_task(struct work_struct *work)
11560 struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
11563 if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
11564 netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
11568 switch (bp->fw_reset_state) {
11569 case BNXT_FW_RESET_STATE_POLL_VF: {
11570 int n = bnxt_get_registered_vfs(bp);
11574 netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
11575 n, jiffies_to_msecs(jiffies -
11576 bp->fw_reset_timestamp));
11577 goto fw_reset_abort;
11578 } else if (n > 0) {
11579 if (time_after(jiffies, bp->fw_reset_timestamp +
11580 (bp->fw_reset_max_dsecs * HZ / 10))) {
11581 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11582 bp->fw_reset_state = 0;
11583 netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
11587 bnxt_queue_fw_reset_work(bp, HZ / 10);
11590 bp->fw_reset_timestamp = jiffies;
11592 if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
11594 goto fw_reset_abort;
11596 bnxt_fw_reset_close(bp);
11597 if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
11598 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
11601 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11602 tmo = bp->fw_reset_min_dsecs * HZ / 10;
11605 bnxt_queue_fw_reset_work(bp, tmo);
11608 case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
11611 val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11612 if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
11613 !time_after(jiffies, bp->fw_reset_timestamp +
11614 (bp->fw_reset_max_dsecs * HZ / 10))) {
11615 bnxt_queue_fw_reset_work(bp, HZ / 5);
11619 if (!bp->fw_health->master) {
11620 u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
11622 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11623 bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
11626 bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
11629 case BNXT_FW_RESET_STATE_RESET_FW:
11630 bnxt_reset_all(bp);
11631 bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
11632 bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
11634 case BNXT_FW_RESET_STATE_ENABLE_DEV:
11635 if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
11638 val = bnxt_fw_health_readl(bp,
11639 BNXT_FW_RESET_INPROG_REG);
11641 netdev_warn(bp->dev, "FW reset inprog %x after min wait time.\n",
11644 clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
11645 if (pci_enable_device(bp->pdev)) {
11646 netdev_err(bp->dev, "Cannot re-enable PCI device\n");
11647 goto fw_reset_abort;
11649 pci_set_master(bp->pdev);
11650 bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
11652 case BNXT_FW_RESET_STATE_POLL_FW:
11653 bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
11654 rc = __bnxt_hwrm_ver_get(bp, true);
11656 if (time_after(jiffies, bp->fw_reset_timestamp +
11657 (bp->fw_reset_max_dsecs * HZ / 10))) {
11658 netdev_err(bp->dev, "Firmware reset aborted\n");
11659 goto fw_reset_abort_status;
11661 bnxt_queue_fw_reset_work(bp, HZ / 5);
11664 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
11665 bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
11667 case BNXT_FW_RESET_STATE_OPENING:
11668 while (!rtnl_trylock()) {
11669 bnxt_queue_fw_reset_work(bp, HZ / 10);
11672 rc = bnxt_open(bp->dev);
11674 netdev_err(bp->dev, "bnxt_open_nic() failed\n");
11675 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11676 dev_close(bp->dev);
11679 if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
11680 bp->fw_health->enabled) {
11681 bp->fw_health->last_fw_reset_cnt =
11682 bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
11684 bp->fw_reset_state = 0;
11685 /* Make sure fw_reset_state is 0 before clearing the flag */
11686 smp_mb__before_atomic();
11687 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11688 bnxt_ulp_start(bp, rc);
11690 bnxt_reenable_sriov(bp);
11691 bnxt_dl_health_recovery_done(bp);
11692 bnxt_dl_health_status_update(bp, true);
11698 fw_reset_abort_status:
11699 if (bp->fw_health->status_reliable ||
11700 (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
11701 u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11703 netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
11706 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
11707 if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
11708 bnxt_dl_health_status_update(bp, false);
11709 bp->fw_reset_state = 0;
11711 dev_close(bp->dev);
11715 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
11718 struct bnxt *bp = netdev_priv(dev);
11720 SET_NETDEV_DEV(dev, &pdev->dev);
11722 /* enable device (incl. PCI PM wakeup), and bus-mastering */
11723 rc = pci_enable_device(pdev);
11725 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
11729 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
11730 dev_err(&pdev->dev,
11731 "Cannot find PCI device base address, aborting\n");
11733 goto init_err_disable;
11736 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
11738 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
11739 goto init_err_disable;
11742 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
11743 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
11744 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
11746 goto init_err_release;
11749 pci_set_master(pdev);
11754 /* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
11755 * determines the BAR size.
11757 bp->bar0 = pci_ioremap_bar(pdev, 0);
11759 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
11761 goto init_err_release;
11764 bp->bar2 = pci_ioremap_bar(pdev, 4);
11766 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
11768 goto init_err_release;
11771 pci_enable_pcie_error_reporting(pdev);
11773 INIT_WORK(&bp->sp_task, bnxt_sp_task);
11774 INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
11776 spin_lock_init(&bp->ntp_fltr_lock);
11777 #if BITS_PER_LONG == 32
11778 spin_lock_init(&bp->db_lock);
11781 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
11782 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
11784 bnxt_init_dflt_coal(bp);
11786 timer_setup(&bp->timer, bnxt_timer, 0);
11787 bp->current_interval = BNXT_TIMER_INTERVAL;
11789 bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
11790 bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
11792 clear_bit(BNXT_STATE_OPEN, &bp->state);
11796 bnxt_unmap_bars(bp, pdev);
11797 pci_release_regions(pdev);
11800 pci_disable_device(pdev);
11806 /* rtnl_lock held */
11807 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
11809 struct sockaddr *addr = p;
11810 struct bnxt *bp = netdev_priv(dev);
11813 if (!is_valid_ether_addr(addr->sa_data))
11814 return -EADDRNOTAVAIL;
11816 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
11819 rc = bnxt_approve_mac(bp, addr->sa_data, true);
11823 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
11824 if (netif_running(dev)) {
11825 bnxt_close_nic(bp, false, false);
11826 rc = bnxt_open_nic(bp, false, false);
11832 /* rtnl_lock held */
11833 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
11835 struct bnxt *bp = netdev_priv(dev);
11837 if (netif_running(dev))
11838 bnxt_close_nic(bp, true, false);
11840 dev->mtu = new_mtu;
11841 bnxt_set_ring_params(bp);
11843 if (netif_running(dev))
11844 return bnxt_open_nic(bp, true, false);
11849 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
11851 struct bnxt *bp = netdev_priv(dev);
11855 if (tc > bp->max_tc) {
11856 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
11861 if (netdev_get_num_tc(dev) == tc)
11864 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11867 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
11868 sh, tc, bp->tx_nr_rings_xdp);
11872 /* Needs to close the device and do hw resource re-allocations */
11873 if (netif_running(bp->dev))
11874 bnxt_close_nic(bp, true, false);
11877 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
11878 netdev_set_num_tc(dev, tc);
11880 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
11881 netdev_reset_tc(dev);
11883 bp->tx_nr_rings += bp->tx_nr_rings_xdp;
11884 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
11885 bp->tx_nr_rings + bp->rx_nr_rings;
11887 if (netif_running(bp->dev))
11888 return bnxt_open_nic(bp, true, false);
11893 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
11896 struct bnxt *bp = cb_priv;
11898 if (!bnxt_tc_flower_enabled(bp) ||
11899 !tc_cls_can_offload_and_chain0(bp->dev, type_data))
11900 return -EOPNOTSUPP;
11903 case TC_SETUP_CLSFLOWER:
11904 return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
11906 return -EOPNOTSUPP;
11910 LIST_HEAD(bnxt_block_cb_list);
11912 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
11915 struct bnxt *bp = netdev_priv(dev);
11918 case TC_SETUP_BLOCK:
11919 return flow_block_cb_setup_simple(type_data,
11920 &bnxt_block_cb_list,
11921 bnxt_setup_tc_block_cb,
11923 case TC_SETUP_QDISC_MQPRIO: {
11924 struct tc_mqprio_qopt *mqprio = type_data;
11926 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
11928 return bnxt_setup_mq_tc(dev, mqprio->num_tc);
11931 return -EOPNOTSUPP;
11935 #ifdef CONFIG_RFS_ACCEL
11936 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
11937 struct bnxt_ntuple_filter *f2)
11939 struct flow_keys *keys1 = &f1->fkeys;
11940 struct flow_keys *keys2 = &f2->fkeys;
11942 if (keys1->basic.n_proto != keys2->basic.n_proto ||
11943 keys1->basic.ip_proto != keys2->basic.ip_proto)
11946 if (keys1->basic.n_proto == htons(ETH_P_IP)) {
11947 if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
11948 keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
11951 if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
11952 sizeof(keys1->addrs.v6addrs.src)) ||
11953 memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
11954 sizeof(keys1->addrs.v6addrs.dst)))
11958 if (keys1->ports.ports == keys2->ports.ports &&
11959 keys1->control.flags == keys2->control.flags &&
11960 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
11961 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
11967 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
11968 u16 rxq_index, u32 flow_id)
11970 struct bnxt *bp = netdev_priv(dev);
11971 struct bnxt_ntuple_filter *fltr, *new_fltr;
11972 struct flow_keys *fkeys;
11973 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
11974 int rc = 0, idx, bit_id, l2_idx = 0;
11975 struct hlist_head *head;
11978 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
11979 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
11982 netif_addr_lock_bh(dev);
11983 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
11984 if (ether_addr_equal(eth->h_dest,
11985 vnic->uc_list + off)) {
11990 netif_addr_unlock_bh(dev);
11994 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
11998 fkeys = &new_fltr->fkeys;
11999 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
12000 rc = -EPROTONOSUPPORT;
12004 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
12005 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
12006 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
12007 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
12008 rc = -EPROTONOSUPPORT;
12011 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
12012 bp->hwrm_spec_code < 0x10601) {
12013 rc = -EPROTONOSUPPORT;
12016 flags = fkeys->control.flags;
12017 if (((flags & FLOW_DIS_ENCAPSULATION) &&
12018 bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
12019 rc = -EPROTONOSUPPORT;
12023 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
12024 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
12026 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
12027 head = &bp->ntp_fltr_hash_tbl[idx];
12029 hlist_for_each_entry_rcu(fltr, head, hash) {
12030 if (bnxt_fltr_match(fltr, new_fltr)) {
12038 spin_lock_bh(&bp->ntp_fltr_lock);
12039 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
12040 BNXT_NTP_FLTR_MAX_FLTR, 0);
12042 spin_unlock_bh(&bp->ntp_fltr_lock);
12047 new_fltr->sw_id = (u16)bit_id;
12048 new_fltr->flow_id = flow_id;
12049 new_fltr->l2_fltr_idx = l2_idx;
12050 new_fltr->rxq = rxq_index;
12051 hlist_add_head_rcu(&new_fltr->hash, head);
12052 bp->ntp_fltr_count++;
12053 spin_unlock_bh(&bp->ntp_fltr_lock);
12055 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
12056 bnxt_queue_sp_work(bp);
12058 return new_fltr->sw_id;
12065 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12069 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
12070 struct hlist_head *head;
12071 struct hlist_node *tmp;
12072 struct bnxt_ntuple_filter *fltr;
12075 head = &bp->ntp_fltr_hash_tbl[i];
12076 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
12079 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
12080 if (rps_may_expire_flow(bp->dev, fltr->rxq,
12083 bnxt_hwrm_cfa_ntuple_filter_free(bp,
12088 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
12093 set_bit(BNXT_FLTR_VALID, &fltr->state);
12097 spin_lock_bh(&bp->ntp_fltr_lock);
12098 hlist_del_rcu(&fltr->hash);
12099 bp->ntp_fltr_count--;
12100 spin_unlock_bh(&bp->ntp_fltr_lock);
12102 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
12111 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
12115 #endif /* CONFIG_RFS_ACCEL */
12117 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
12118 unsigned int entry, struct udp_tunnel_info *ti)
12120 struct bnxt *bp = netdev_priv(netdev);
12123 if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
12124 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12126 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12128 return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
12131 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
12132 unsigned int entry, struct udp_tunnel_info *ti)
12134 struct bnxt *bp = netdev_priv(netdev);
12137 if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
12138 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
12140 cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
12142 return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
12145 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
12146 .set_port = bnxt_udp_tunnel_set_port,
12147 .unset_port = bnxt_udp_tunnel_unset_port,
12148 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
12149 UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
12151 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
12152 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
12156 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12157 struct net_device *dev, u32 filter_mask,
12160 struct bnxt *bp = netdev_priv(dev);
12162 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
12163 nlflags, filter_mask, NULL);
12166 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
12167 u16 flags, struct netlink_ext_ack *extack)
12169 struct bnxt *bp = netdev_priv(dev);
12170 struct nlattr *attr, *br_spec;
12173 if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
12174 return -EOPNOTSUPP;
12176 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12180 nla_for_each_nested(attr, br_spec, rem) {
12183 if (nla_type(attr) != IFLA_BRIDGE_MODE)
12186 if (nla_len(attr) < sizeof(mode))
12189 mode = nla_get_u16(attr);
12190 if (mode == bp->br_mode)
12193 rc = bnxt_hwrm_set_br_mode(bp, mode);
12195 bp->br_mode = mode;
12201 int bnxt_get_port_parent_id(struct net_device *dev,
12202 struct netdev_phys_item_id *ppid)
12204 struct bnxt *bp = netdev_priv(dev);
12206 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
12207 return -EOPNOTSUPP;
12209 /* The PF and it's VF-reps only support the switchdev framework */
12210 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
12211 return -EOPNOTSUPP;
12213 ppid->id_len = sizeof(bp->dsn);
12214 memcpy(ppid->id, bp->dsn, ppid->id_len);
12219 static struct devlink_port *bnxt_get_devlink_port(struct net_device *dev)
12221 struct bnxt *bp = netdev_priv(dev);
12223 return &bp->dl_port;
12226 static const struct net_device_ops bnxt_netdev_ops = {
12227 .ndo_open = bnxt_open,
12228 .ndo_start_xmit = bnxt_start_xmit,
12229 .ndo_stop = bnxt_close,
12230 .ndo_get_stats64 = bnxt_get_stats64,
12231 .ndo_set_rx_mode = bnxt_set_rx_mode,
12232 .ndo_do_ioctl = bnxt_ioctl,
12233 .ndo_validate_addr = eth_validate_addr,
12234 .ndo_set_mac_address = bnxt_change_mac_addr,
12235 .ndo_change_mtu = bnxt_change_mtu,
12236 .ndo_fix_features = bnxt_fix_features,
12237 .ndo_set_features = bnxt_set_features,
12238 .ndo_tx_timeout = bnxt_tx_timeout,
12239 #ifdef CONFIG_BNXT_SRIOV
12240 .ndo_get_vf_config = bnxt_get_vf_config,
12241 .ndo_set_vf_mac = bnxt_set_vf_mac,
12242 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
12243 .ndo_set_vf_rate = bnxt_set_vf_bw,
12244 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
12245 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
12246 .ndo_set_vf_trust = bnxt_set_vf_trust,
12248 .ndo_setup_tc = bnxt_setup_tc,
12249 #ifdef CONFIG_RFS_ACCEL
12250 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
12252 .ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
12253 .ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
12254 .ndo_bpf = bnxt_xdp,
12255 .ndo_xdp_xmit = bnxt_xdp_xmit,
12256 .ndo_bridge_getlink = bnxt_bridge_getlink,
12257 .ndo_bridge_setlink = bnxt_bridge_setlink,
12258 .ndo_get_devlink_port = bnxt_get_devlink_port,
12261 static void bnxt_remove_one(struct pci_dev *pdev)
12263 struct net_device *dev = pci_get_drvdata(pdev);
12264 struct bnxt *bp = netdev_priv(dev);
12267 bnxt_sriov_disable(bp);
12270 devlink_port_type_clear(&bp->dl_port);
12271 pci_disable_pcie_error_reporting(pdev);
12272 unregister_netdev(dev);
12273 clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
12274 /* Flush any pending tasks */
12275 cancel_work_sync(&bp->sp_task);
12276 cancel_delayed_work_sync(&bp->fw_reset_task);
12279 bnxt_dl_fw_reporters_destroy(bp, true);
12280 bnxt_dl_unregister(bp);
12281 bnxt_shutdown_tc(bp);
12283 bnxt_clear_int_mode(bp);
12284 bnxt_hwrm_func_drv_unrgtr(bp);
12285 bnxt_free_hwrm_resources(bp);
12286 bnxt_free_hwrm_short_cmd_req(bp);
12287 bnxt_ethtool_free(bp);
12291 kfree(bp->fw_health);
12292 bp->fw_health = NULL;
12293 bnxt_cleanup_pci(bp);
12294 bnxt_free_ctx_mem(bp);
12297 kfree(bp->rss_indir_tbl);
12298 bp->rss_indir_tbl = NULL;
12299 bnxt_free_port_stats(bp);
12303 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
12306 struct bnxt_link_info *link_info = &bp->link_info;
12308 rc = bnxt_hwrm_phy_qcaps(bp);
12310 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
12317 rc = bnxt_update_link(bp, false);
12319 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
12324 /* Older firmware does not have supported_auto_speeds, so assume
12325 * that all supported speeds can be autonegotiated.
12327 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
12328 link_info->support_auto_speeds = link_info->support_speeds;
12330 bnxt_init_ethtool_link_settings(bp);
12334 static int bnxt_get_max_irq(struct pci_dev *pdev)
12338 if (!pdev->msix_cap)
12341 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
12342 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
12345 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12348 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12349 int max_ring_grps = 0, max_irq;
12351 *max_tx = hw_resc->max_tx_rings;
12352 *max_rx = hw_resc->max_rx_rings;
12353 *max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
12354 max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
12355 bnxt_get_ulp_msix_num(bp),
12356 hw_resc->max_stat_ctxs - bnxt_get_ulp_stat_ctxs(bp));
12357 if (!(bp->flags & BNXT_FLAG_CHIP_P5))
12358 *max_cp = min_t(int, *max_cp, max_irq);
12359 max_ring_grps = hw_resc->max_hw_ring_grps;
12360 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
12364 if (bp->flags & BNXT_FLAG_AGG_RINGS)
12366 if (bp->flags & BNXT_FLAG_CHIP_P5) {
12367 bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
12368 /* On P5 chips, max_cp output param should be available NQs */
12371 *max_rx = min_t(int, *max_rx, max_ring_grps);
12374 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
12378 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
12381 if (!rx || !tx || !cp)
12384 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
12387 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
12392 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12393 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
12394 /* Not enough rings, try disabling agg rings. */
12395 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
12396 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
12398 /* set BNXT_FLAG_AGG_RINGS back for consistency */
12399 bp->flags |= BNXT_FLAG_AGG_RINGS;
12402 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
12403 bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12404 bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12405 bnxt_set_ring_params(bp);
12408 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
12409 int max_cp, max_stat, max_irq;
12411 /* Reserve minimum resources for RoCE */
12412 max_cp = bnxt_get_max_func_cp_rings(bp);
12413 max_stat = bnxt_get_max_func_stat_ctxs(bp);
12414 max_irq = bnxt_get_max_func_irqs(bp);
12415 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
12416 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
12417 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
12420 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
12421 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
12422 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
12423 max_cp = min_t(int, max_cp, max_irq);
12424 max_cp = min_t(int, max_cp, max_stat);
12425 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
12432 /* In initial default shared ring setting, each shared ring must have a
12435 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
12437 bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
12438 bp->rx_nr_rings = bp->cp_nr_rings;
12439 bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
12440 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12443 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
12445 int dflt_rings, max_rx_rings, max_tx_rings, rc;
12447 if (!bnxt_can_reserve_rings(bp))
12451 bp->flags |= BNXT_FLAG_SHARED_RINGS;
12452 dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
12453 /* Reduce default rings on multi-port cards so that total default
12454 * rings do not exceed CPU count.
12456 if (bp->port_count > 1) {
12458 max_t(int, num_online_cpus() / bp->port_count, 1);
12460 dflt_rings = min_t(int, dflt_rings, max_rings);
12462 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
12465 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
12466 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
12468 bnxt_trim_dflt_sh_rings(bp);
12470 bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
12471 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
12473 rc = __bnxt_reserve_rings(bp);
12475 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
12476 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12478 bnxt_trim_dflt_sh_rings(bp);
12480 /* Rings may have been trimmed, re-reserve the trimmed rings. */
12481 if (bnxt_need_reserve_rings(bp)) {
12482 rc = __bnxt_reserve_rings(bp);
12484 netdev_warn(bp->dev, "2nd rings reservation failed.\n");
12485 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12487 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12492 bp->tx_nr_rings = 0;
12493 bp->rx_nr_rings = 0;
12498 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
12502 if (bp->tx_nr_rings)
12505 bnxt_ulp_irq_stop(bp);
12506 bnxt_clear_int_mode(bp);
12507 rc = bnxt_set_dflt_rings(bp, true);
12509 netdev_err(bp->dev, "Not enough rings available.\n");
12510 goto init_dflt_ring_err;
12512 rc = bnxt_init_int_mode(bp);
12514 goto init_dflt_ring_err;
12516 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12518 bnxt_set_dflt_rfs(bp);
12520 init_dflt_ring_err:
12521 bnxt_ulp_irq_restart(bp, rc);
12525 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
12530 bnxt_hwrm_func_qcaps(bp);
12532 if (netif_running(bp->dev))
12533 __bnxt_close_nic(bp, true, false);
12535 bnxt_ulp_irq_stop(bp);
12536 bnxt_clear_int_mode(bp);
12537 rc = bnxt_init_int_mode(bp);
12538 bnxt_ulp_irq_restart(bp, rc);
12540 if (netif_running(bp->dev)) {
12542 dev_close(bp->dev);
12544 rc = bnxt_open_nic(bp, true, false);
12550 static int bnxt_init_mac_addr(struct bnxt *bp)
12555 memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
12557 #ifdef CONFIG_BNXT_SRIOV
12558 struct bnxt_vf_info *vf = &bp->vf;
12559 bool strict_approval = true;
12561 if (is_valid_ether_addr(vf->mac_addr)) {
12562 /* overwrite netdev dev_addr with admin VF MAC */
12563 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
12564 /* Older PF driver or firmware may not approve this
12567 strict_approval = false;
12569 eth_hw_addr_random(bp->dev);
12571 rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
12577 #define BNXT_VPD_LEN 512
12578 static void bnxt_vpd_read_info(struct bnxt *bp)
12580 struct pci_dev *pdev = bp->pdev;
12581 int i, len, pos, ro_size, size;
12585 vpd_data = kmalloc(BNXT_VPD_LEN, GFP_KERNEL);
12589 vpd_size = pci_read_vpd(pdev, 0, BNXT_VPD_LEN, vpd_data);
12590 if (vpd_size <= 0) {
12591 netdev_err(bp->dev, "Unable to read VPD\n");
12595 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
12597 netdev_err(bp->dev, "VPD READ-Only not found\n");
12601 ro_size = pci_vpd_lrdt_size(&vpd_data[i]);
12602 i += PCI_VPD_LRDT_TAG_SIZE;
12603 if (i + ro_size > vpd_size)
12606 pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12607 PCI_VPD_RO_KEYWORD_PARTNO);
12611 len = pci_vpd_info_field_size(&vpd_data[pos]);
12612 pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12613 if (len + pos > vpd_size)
12616 size = min(len, BNXT_VPD_FLD_LEN - 1);
12617 memcpy(bp->board_partno, &vpd_data[pos], size);
12620 pos = pci_vpd_find_info_keyword(vpd_data, i, ro_size,
12621 PCI_VPD_RO_KEYWORD_SERIALNO);
12625 len = pci_vpd_info_field_size(&vpd_data[pos]);
12626 pos += PCI_VPD_INFO_FLD_HDR_SIZE;
12627 if (len + pos > vpd_size)
12630 size = min(len, BNXT_VPD_FLD_LEN - 1);
12631 memcpy(bp->board_serialno, &vpd_data[pos], size);
12636 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
12638 struct pci_dev *pdev = bp->pdev;
12641 qword = pci_get_dsn(pdev);
12643 netdev_info(bp->dev, "Unable to read adapter's DSN\n");
12644 return -EOPNOTSUPP;
12647 put_unaligned_le64(qword, dsn);
12649 bp->flags |= BNXT_FLAG_DSN_VALID;
12653 static int bnxt_map_db_bar(struct bnxt *bp)
12657 bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
12663 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
12665 struct net_device *dev;
12669 if (pci_is_bridge(pdev))
12672 /* Clear any pending DMA transactions from crash kernel
12673 * while loading driver in capture kernel.
12675 if (is_kdump_kernel()) {
12676 pci_clear_master(pdev);
12680 max_irqs = bnxt_get_max_irq(pdev);
12681 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
12685 bp = netdev_priv(dev);
12686 bp->msg_enable = BNXT_DEF_MSG_ENABLE;
12687 bnxt_set_max_func_irqs(bp, max_irqs);
12689 if (bnxt_vf_pciid(ent->driver_data))
12690 bp->flags |= BNXT_FLAG_VF;
12692 if (pdev->msix_cap)
12693 bp->flags |= BNXT_FLAG_MSIX_CAP;
12695 rc = bnxt_init_board(pdev, dev);
12697 goto init_err_free;
12699 dev->netdev_ops = &bnxt_netdev_ops;
12700 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
12701 dev->ethtool_ops = &bnxt_ethtool_ops;
12702 pci_set_drvdata(pdev, dev);
12705 bnxt_vpd_read_info(bp);
12707 rc = bnxt_alloc_hwrm_resources(bp);
12709 goto init_err_pci_clean;
12711 mutex_init(&bp->hwrm_cmd_lock);
12712 mutex_init(&bp->link_lock);
12714 rc = bnxt_fw_init_one_p1(bp);
12716 goto init_err_pci_clean;
12718 if (BNXT_CHIP_P5(bp)) {
12719 bp->flags |= BNXT_FLAG_CHIP_P5;
12720 if (BNXT_CHIP_SR2(bp))
12721 bp->flags |= BNXT_FLAG_CHIP_SR2;
12724 rc = bnxt_alloc_rss_indir_tbl(bp);
12726 goto init_err_pci_clean;
12728 rc = bnxt_fw_init_one_p2(bp);
12730 goto init_err_pci_clean;
12732 rc = bnxt_map_db_bar(bp);
12734 dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
12736 goto init_err_pci_clean;
12739 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12740 NETIF_F_TSO | NETIF_F_TSO6 |
12741 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12742 NETIF_F_GSO_IPXIP4 |
12743 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12744 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
12745 NETIF_F_RXCSUM | NETIF_F_GRO;
12747 if (BNXT_SUPPORTS_TPA(bp))
12748 dev->hw_features |= NETIF_F_LRO;
12750 dev->hw_enc_features =
12751 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
12752 NETIF_F_TSO | NETIF_F_TSO6 |
12753 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
12754 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
12755 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
12756 dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
12758 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
12759 NETIF_F_GSO_GRE_CSUM;
12760 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
12761 if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
12762 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
12763 if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
12764 dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
12765 if (BNXT_SUPPORTS_TPA(bp))
12766 dev->hw_features |= NETIF_F_GRO_HW;
12767 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
12768 if (dev->features & NETIF_F_GRO_HW)
12769 dev->features &= ~NETIF_F_LRO;
12770 dev->priv_flags |= IFF_UNICAST_FLT;
12772 #ifdef CONFIG_BNXT_SRIOV
12773 init_waitqueue_head(&bp->sriov_cfg_wait);
12774 mutex_init(&bp->sriov_lock);
12776 if (BNXT_SUPPORTS_TPA(bp)) {
12777 bp->gro_func = bnxt_gro_func_5730x;
12778 if (BNXT_CHIP_P4(bp))
12779 bp->gro_func = bnxt_gro_func_5731x;
12780 else if (BNXT_CHIP_P5(bp))
12781 bp->gro_func = bnxt_gro_func_5750x;
12783 if (!BNXT_CHIP_P4_PLUS(bp))
12784 bp->flags |= BNXT_FLAG_DOUBLE_DB;
12786 bp->ulp_probe = bnxt_ulp_probe;
12788 rc = bnxt_init_mac_addr(bp);
12790 dev_err(&pdev->dev, "Unable to initialize mac address.\n");
12791 rc = -EADDRNOTAVAIL;
12792 goto init_err_pci_clean;
12796 /* Read the adapter's DSN to use as the eswitch switch_id */
12797 rc = bnxt_pcie_dsn_get(bp, bp->dsn);
12800 /* MTU range: 60 - FW defined max */
12801 dev->min_mtu = ETH_ZLEN;
12802 dev->max_mtu = bp->max_mtu;
12804 rc = bnxt_probe_phy(bp, true);
12806 goto init_err_pci_clean;
12808 bnxt_set_rx_skb_mode(bp, false);
12809 bnxt_set_tpa_flags(bp);
12810 bnxt_set_ring_params(bp);
12811 rc = bnxt_set_dflt_rings(bp, true);
12813 netdev_err(bp->dev, "Not enough rings available.\n");
12815 goto init_err_pci_clean;
12818 bnxt_fw_init_one_p3(bp);
12820 if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
12821 bp->flags |= BNXT_FLAG_STRIP_VLAN;
12823 rc = bnxt_init_int_mode(bp);
12825 goto init_err_pci_clean;
12827 /* No TC has been set yet and rings may have been trimmed due to
12828 * limited MSIX, so we re-initialize the TX rings per TC.
12830 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
12835 create_singlethread_workqueue("bnxt_pf_wq");
12837 dev_err(&pdev->dev, "Unable to create workqueue.\n");
12839 goto init_err_pci_clean;
12842 rc = bnxt_init_tc(bp);
12844 netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
12848 bnxt_dl_register(bp);
12850 rc = register_netdev(dev);
12852 goto init_err_cleanup;
12855 devlink_port_type_eth_set(&bp->dl_port, bp->dev);
12856 bnxt_dl_fw_reporters_create(bp);
12858 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
12859 board_info[ent->driver_data].name,
12860 (long)pci_resource_start(pdev, 0), dev->dev_addr);
12861 pcie_print_link_status(pdev);
12863 pci_save_state(pdev);
12867 bnxt_dl_unregister(bp);
12868 bnxt_shutdown_tc(bp);
12869 bnxt_clear_int_mode(bp);
12871 init_err_pci_clean:
12872 bnxt_hwrm_func_drv_unrgtr(bp);
12873 bnxt_free_hwrm_short_cmd_req(bp);
12874 bnxt_free_hwrm_resources(bp);
12875 bnxt_ethtool_free(bp);
12876 kfree(bp->fw_health);
12877 bp->fw_health = NULL;
12878 bnxt_cleanup_pci(bp);
12879 bnxt_free_ctx_mem(bp);
12882 kfree(bp->rss_indir_tbl);
12883 bp->rss_indir_tbl = NULL;
12890 static void bnxt_shutdown(struct pci_dev *pdev)
12892 struct net_device *dev = pci_get_drvdata(pdev);
12899 bp = netdev_priv(dev);
12901 goto shutdown_exit;
12903 if (netif_running(dev))
12906 bnxt_ulp_shutdown(bp);
12907 bnxt_clear_int_mode(bp);
12908 pci_disable_device(pdev);
12910 if (system_state == SYSTEM_POWER_OFF) {
12911 pci_wake_from_d3(pdev, bp->wol);
12912 pci_set_power_state(pdev, PCI_D3hot);
12919 #ifdef CONFIG_PM_SLEEP
12920 static int bnxt_suspend(struct device *device)
12922 struct net_device *dev = dev_get_drvdata(device);
12923 struct bnxt *bp = netdev_priv(dev);
12928 if (netif_running(dev)) {
12929 netif_device_detach(dev);
12930 rc = bnxt_close(dev);
12932 bnxt_hwrm_func_drv_unrgtr(bp);
12933 pci_disable_device(bp->pdev);
12934 bnxt_free_ctx_mem(bp);
12941 static int bnxt_resume(struct device *device)
12943 struct net_device *dev = dev_get_drvdata(device);
12944 struct bnxt *bp = netdev_priv(dev);
12948 rc = pci_enable_device(bp->pdev);
12950 netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
12954 pci_set_master(bp->pdev);
12955 if (bnxt_hwrm_ver_get(bp)) {
12959 rc = bnxt_hwrm_func_reset(bp);
12965 rc = bnxt_hwrm_func_qcaps(bp);
12969 if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
12974 bnxt_get_wol_settings(bp);
12975 if (netif_running(dev)) {
12976 rc = bnxt_open(dev);
12978 netif_device_attach(dev);
12982 bnxt_ulp_start(bp, rc);
12984 bnxt_reenable_sriov(bp);
12989 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
12990 #define BNXT_PM_OPS (&bnxt_pm_ops)
12994 #define BNXT_PM_OPS NULL
12996 #endif /* CONFIG_PM_SLEEP */
12999 * bnxt_io_error_detected - called when PCI error is detected
13000 * @pdev: Pointer to PCI device
13001 * @state: The current pci connection state
13003 * This function is called after a PCI bus error affecting
13004 * this device has been detected.
13006 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
13007 pci_channel_state_t state)
13009 struct net_device *netdev = pci_get_drvdata(pdev);
13010 struct bnxt *bp = netdev_priv(netdev);
13012 netdev_info(netdev, "PCI I/O error detected\n");
13015 netif_device_detach(netdev);
13019 if (state == pci_channel_io_perm_failure) {
13021 return PCI_ERS_RESULT_DISCONNECT;
13024 if (state == pci_channel_io_frozen)
13025 set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
13027 if (netif_running(netdev))
13028 bnxt_close(netdev);
13030 if (pci_is_enabled(pdev))
13031 pci_disable_device(pdev);
13032 bnxt_free_ctx_mem(bp);
13037 /* Request a slot slot reset. */
13038 return PCI_ERS_RESULT_NEED_RESET;
13042 * bnxt_io_slot_reset - called after the pci bus has been reset.
13043 * @pdev: Pointer to PCI device
13045 * Restart the card from scratch, as if from a cold-boot.
13046 * At this point, the card has exprienced a hard reset,
13047 * followed by fixups by BIOS, and has its config space
13048 * set up identically to what it was at cold boot.
13050 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
13052 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
13053 struct net_device *netdev = pci_get_drvdata(pdev);
13054 struct bnxt *bp = netdev_priv(netdev);
13057 netdev_info(bp->dev, "PCI Slot Reset\n");
13061 if (pci_enable_device(pdev)) {
13062 dev_err(&pdev->dev,
13063 "Cannot re-enable PCI device after reset.\n");
13065 pci_set_master(pdev);
13066 /* Upon fatal error, our device internal logic that latches to
13067 * BAR value is getting reset and will restore only upon
13068 * rewritting the BARs.
13070 * As pci_restore_state() does not re-write the BARs if the
13071 * value is same as saved value earlier, driver needs to
13072 * write the BARs to 0 to force restore, in case of fatal error.
13074 if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
13076 for (off = PCI_BASE_ADDRESS_0;
13077 off <= PCI_BASE_ADDRESS_5; off += 4)
13078 pci_write_config_dword(bp->pdev, off, 0);
13080 pci_restore_state(pdev);
13081 pci_save_state(pdev);
13083 err = bnxt_hwrm_func_reset(bp);
13085 result = PCI_ERS_RESULT_RECOVERED;
13094 * bnxt_io_resume - called when traffic can start flowing again.
13095 * @pdev: Pointer to PCI device
13097 * This callback is called when the error recovery driver tells
13098 * us that its OK to resume normal operation.
13100 static void bnxt_io_resume(struct pci_dev *pdev)
13102 struct net_device *netdev = pci_get_drvdata(pdev);
13103 struct bnxt *bp = netdev_priv(netdev);
13106 netdev_info(bp->dev, "PCI Slot Resume\n");
13109 err = bnxt_hwrm_func_qcaps(bp);
13110 if (!err && netif_running(netdev))
13111 err = bnxt_open(netdev);
13113 bnxt_ulp_start(bp, err);
13115 bnxt_reenable_sriov(bp);
13116 netif_device_attach(netdev);
13122 static const struct pci_error_handlers bnxt_err_handler = {
13123 .error_detected = bnxt_io_error_detected,
13124 .slot_reset = bnxt_io_slot_reset,
13125 .resume = bnxt_io_resume
13128 static struct pci_driver bnxt_pci_driver = {
13129 .name = DRV_MODULE_NAME,
13130 .id_table = bnxt_pci_tbl,
13131 .probe = bnxt_init_one,
13132 .remove = bnxt_remove_one,
13133 .shutdown = bnxt_shutdown,
13134 .driver.pm = BNXT_PM_OPS,
13135 .err_handler = &bnxt_err_handler,
13136 #if defined(CONFIG_BNXT_SRIOV)
13137 .sriov_configure = bnxt_sriov_configure,
13141 static int __init bnxt_init(void)
13146 err = pci_register_driver(&bnxt_pci_driver);
13155 static void __exit bnxt_exit(void)
13157 pci_unregister_driver(&bnxt_pci_driver);
13159 destroy_workqueue(bnxt_pf_wq);
13163 module_init(bnxt_init);
13164 module_exit(bnxt_exit);