1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
10 #include <linux/module.h>
12 #include <linux/stringify.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/errno.h>
16 #include <linux/ioport.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/interrupt.h>
20 #include <linux/pci.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/bitops.h>
27 #include <linux/irq.h>
28 #include <linux/delay.h>
29 #include <asm/byteorder.h>
31 #include <linux/time.h>
32 #include <linux/mii.h>
34 #include <linux/if_vlan.h>
38 #include <net/checksum.h>
39 #include <net/ip6_checksum.h>
40 #include <net/udp_tunnel.h>
41 #ifdef CONFIG_NET_RX_BUSY_POLL
42 #include <net/busy_poll.h>
44 #include <linux/workqueue.h>
45 #include <linux/prefetch.h>
46 #include <linux/cache.h>
47 #include <linux/log2.h>
48 #include <linux/aer.h>
49 #include <linux/bitmap.h>
50 #include <linux/cpu_rmap.h>
54 #include "bnxt_sriov.h"
55 #include "bnxt_ethtool.h"
57 #define BNXT_TX_TIMEOUT (5 * HZ)
59 static const char version[] =
60 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
62 MODULE_LICENSE("GPL");
63 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
64 MODULE_VERSION(DRV_MODULE_VERSION);
66 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
67 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
68 #define BNXT_RX_COPY_THRESH 256
70 #define BNXT_TX_PUSH_THRESH 164
94 /* indexed by enum above */
98 { "Broadcom BCM57301 NetXtreme-C Single-port 10Gb Ethernet" },
99 { "Broadcom BCM57302 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
100 { "Broadcom BCM57304 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
101 { "Broadcom BCM57311 NetXtreme-C Single-port 10Gb Ethernet" },
102 { "Broadcom BCM57312 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
103 { "Broadcom BCM57402 NetXtreme-E Dual-port 10Gb Ethernet" },
104 { "Broadcom BCM57404 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
105 { "Broadcom BCM57406 NetXtreme-E Dual-port 10GBase-T Ethernet" },
106 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
107 { "Broadcom BCM57412 NetXtreme-E Dual-port 10Gb Ethernet" },
108 { "Broadcom BCM57414 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
109 { "Broadcom BCM57416 NetXtreme-E Dual-port 10GBase-T Ethernet" },
110 { "Broadcom BCM57417 NetXtreme-E Dual-port 10GBase-T Ethernet" },
111 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
112 { "Broadcom BCM57314 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
113 { "Broadcom BCM57304 NetXtreme-C Ethernet Virtual Function" },
114 { "Broadcom BCM57404 NetXtreme-E Ethernet Virtual Function" },
115 { "Broadcom BCM57414 NetXtreme-E Ethernet Virtual Function" },
116 { "Broadcom BCM57314 NetXtreme-E Ethernet Virtual Function" },
119 static const struct pci_device_id bnxt_pci_tbl[] = {
120 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
121 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
122 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
123 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
124 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
125 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
126 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
127 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
128 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57404_NPAR },
129 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
130 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
131 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
132 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
133 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57414_NPAR },
134 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
135 #ifdef CONFIG_BNXT_SRIOV
136 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = BCM57304_VF },
137 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = BCM57404_VF },
138 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = BCM57414_VF },
139 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = BCM57314_VF },
144 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
146 static const u16 bnxt_vf_req_snif[] = {
149 HWRM_CFA_L2_FILTER_ALLOC,
152 static const u16 bnxt_async_events_arr[] = {
153 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
154 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
155 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
156 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
157 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
160 static bool bnxt_vf_pciid(enum board_idx idx)
162 return (idx == BCM57304_VF || idx == BCM57404_VF ||
163 idx == BCM57314_VF || idx == BCM57414_VF);
166 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
167 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
168 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
170 #define BNXT_CP_DB_REARM(db, raw_cons) \
171 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
173 #define BNXT_CP_DB(db, raw_cons) \
174 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
176 #define BNXT_CP_DB_IRQ_DIS(db) \
177 writel(DB_CP_IRQ_DIS_FLAGS, db)
179 static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
181 /* Tell compiler to fetch tx indices from memory. */
184 return bp->tx_ring_size -
185 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
188 static const u16 bnxt_lhint_arr[] = {
189 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
190 TX_BD_FLAGS_LHINT_512_TO_1023,
191 TX_BD_FLAGS_LHINT_1024_TO_2047,
192 TX_BD_FLAGS_LHINT_1024_TO_2047,
193 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
194 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
195 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
196 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
197 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
198 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
199 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
200 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
201 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
202 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
203 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
204 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
205 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
206 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
207 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
210 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
212 struct bnxt *bp = netdev_priv(dev);
214 struct tx_bd_ext *txbd1;
215 struct netdev_queue *txq;
218 unsigned int length, pad = 0;
219 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
221 struct pci_dev *pdev = bp->pdev;
222 struct bnxt_tx_ring_info *txr;
223 struct bnxt_sw_tx_bd *tx_buf;
225 i = skb_get_queue_mapping(skb);
226 if (unlikely(i >= bp->tx_nr_rings)) {
227 dev_kfree_skb_any(skb);
231 txr = &bp->tx_ring[i];
232 txq = netdev_get_tx_queue(dev, i);
235 free_size = bnxt_tx_avail(bp, txr);
236 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
237 netif_tx_stop_queue(txq);
238 return NETDEV_TX_BUSY;
242 len = skb_headlen(skb);
243 last_frag = skb_shinfo(skb)->nr_frags;
245 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
247 txbd->tx_bd_opaque = prod;
249 tx_buf = &txr->tx_buf_ring[prod];
251 tx_buf->nr_frags = last_frag;
255 if (skb_vlan_tag_present(skb)) {
256 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
257 skb_vlan_tag_get(skb);
258 /* Currently supports 8021Q, 8021AD vlan offloads
259 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
261 if (skb->vlan_proto == htons(ETH_P_8021Q))
262 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
265 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
266 struct tx_push_buffer *tx_push_buf = txr->tx_push;
267 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
268 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
269 void *pdata = tx_push_buf->data;
273 /* Set COAL_NOW to be ready quickly for the next push */
274 tx_push->tx_bd_len_flags_type =
275 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
276 TX_BD_TYPE_LONG_TX_BD |
277 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
278 TX_BD_FLAGS_COAL_NOW |
279 TX_BD_FLAGS_PACKET_END |
280 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
282 if (skb->ip_summed == CHECKSUM_PARTIAL)
283 tx_push1->tx_bd_hsize_lflags =
284 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
286 tx_push1->tx_bd_hsize_lflags = 0;
288 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
289 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
291 end = pdata + length;
292 end = PTR_ALIGN(end, 8) - 1;
295 skb_copy_from_linear_data(skb, pdata, len);
297 for (j = 0; j < last_frag; j++) {
298 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
301 fptr = skb_frag_address_safe(frag);
305 memcpy(pdata, fptr, skb_frag_size(frag));
306 pdata += skb_frag_size(frag);
309 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
310 txbd->tx_bd_haddr = txr->data_mapping;
311 prod = NEXT_TX(prod);
312 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
313 memcpy(txbd, tx_push1, sizeof(*txbd));
314 prod = NEXT_TX(prod);
316 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
320 netdev_tx_sent_queue(txq, skb->len);
321 wmb(); /* Sync is_push and byte queue before pushing data */
323 push_len = (length + sizeof(*tx_push) + 7) / 8;
325 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
326 __iowrite64_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
329 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
337 if (length < BNXT_MIN_PKT_SIZE) {
338 pad = BNXT_MIN_PKT_SIZE - length;
339 if (skb_pad(skb, pad)) {
340 /* SKB already freed. */
344 length = BNXT_MIN_PKT_SIZE;
347 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
349 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
350 dev_kfree_skb_any(skb);
355 dma_unmap_addr_set(tx_buf, mapping, mapping);
356 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
357 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
359 txbd->tx_bd_haddr = cpu_to_le64(mapping);
361 prod = NEXT_TX(prod);
362 txbd1 = (struct tx_bd_ext *)
363 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
365 txbd1->tx_bd_hsize_lflags = 0;
366 if (skb_is_gso(skb)) {
369 if (skb->encapsulation)
370 hdr_len = skb_inner_network_offset(skb) +
371 skb_inner_network_header_len(skb) +
372 inner_tcp_hdrlen(skb);
374 hdr_len = skb_transport_offset(skb) +
377 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
379 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
380 length = skb_shinfo(skb)->gso_size;
381 txbd1->tx_bd_mss = cpu_to_le32(length);
383 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
384 txbd1->tx_bd_hsize_lflags =
385 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
386 txbd1->tx_bd_mss = 0;
390 flags |= bnxt_lhint_arr[length];
391 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
393 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
394 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
395 for (i = 0; i < last_frag; i++) {
396 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
398 prod = NEXT_TX(prod);
399 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
401 len = skb_frag_size(frag);
402 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
405 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
408 tx_buf = &txr->tx_buf_ring[prod];
409 dma_unmap_addr_set(tx_buf, mapping, mapping);
411 txbd->tx_bd_haddr = cpu_to_le64(mapping);
413 flags = len << TX_BD_LEN_SHIFT;
414 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
418 txbd->tx_bd_len_flags_type =
419 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
420 TX_BD_FLAGS_PACKET_END);
422 netdev_tx_sent_queue(txq, skb->len);
424 /* Sync BD data before updating doorbell */
427 prod = NEXT_TX(prod);
430 writel(DB_KEY_TX | prod, txr->tx_doorbell);
431 writel(DB_KEY_TX | prod, txr->tx_doorbell);
437 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
438 netif_tx_stop_queue(txq);
440 /* netif_tx_stop_queue() must be done before checking
441 * tx index in bnxt_tx_avail() below, because in
442 * bnxt_tx_int(), we update tx index before checking for
443 * netif_tx_queue_stopped().
446 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
447 netif_tx_wake_queue(txq);
454 /* start back at beginning and unmap skb */
456 tx_buf = &txr->tx_buf_ring[prod];
458 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
459 skb_headlen(skb), PCI_DMA_TODEVICE);
460 prod = NEXT_TX(prod);
462 /* unmap remaining mapped pages */
463 for (i = 0; i < last_frag; i++) {
464 prod = NEXT_TX(prod);
465 tx_buf = &txr->tx_buf_ring[prod];
466 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
467 skb_frag_size(&skb_shinfo(skb)->frags[i]),
471 dev_kfree_skb_any(skb);
475 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
477 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
478 int index = txr - &bp->tx_ring[0];
479 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
480 u16 cons = txr->tx_cons;
481 struct pci_dev *pdev = bp->pdev;
483 unsigned int tx_bytes = 0;
485 for (i = 0; i < nr_pkts; i++) {
486 struct bnxt_sw_tx_bd *tx_buf;
490 tx_buf = &txr->tx_buf_ring[cons];
491 cons = NEXT_TX(cons);
495 if (tx_buf->is_push) {
500 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_headlen(skb), PCI_DMA_TODEVICE);
502 last = tx_buf->nr_frags;
504 for (j = 0; j < last; j++) {
505 cons = NEXT_TX(cons);
506 tx_buf = &txr->tx_buf_ring[cons];
509 dma_unmap_addr(tx_buf, mapping),
510 skb_frag_size(&skb_shinfo(skb)->frags[j]),
515 cons = NEXT_TX(cons);
517 tx_bytes += skb->len;
518 dev_kfree_skb_any(skb);
521 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
524 /* Need to make the tx_cons update visible to bnxt_start_xmit()
525 * before checking for netif_tx_queue_stopped(). Without the
526 * memory barrier, there is a small possibility that bnxt_start_xmit()
527 * will miss it and cause the queue to be stopped forever.
531 if (unlikely(netif_tx_queue_stopped(txq)) &&
532 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
533 __netif_tx_lock(txq, smp_processor_id());
534 if (netif_tx_queue_stopped(txq) &&
535 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
536 txr->dev_state != BNXT_DEV_STATE_CLOSING)
537 netif_tx_wake_queue(txq);
538 __netif_tx_unlock(txq);
542 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
546 struct pci_dev *pdev = bp->pdev;
548 data = kmalloc(bp->rx_buf_size, gfp);
552 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
553 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
555 if (dma_mapping_error(&pdev->dev, *mapping)) {
562 static inline int bnxt_alloc_rx_data(struct bnxt *bp,
563 struct bnxt_rx_ring_info *rxr,
566 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
567 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
571 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
576 dma_unmap_addr_set(rx_buf, mapping, mapping);
578 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
583 static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
586 u16 prod = rxr->rx_prod;
587 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
588 struct rx_bd *cons_bd, *prod_bd;
590 prod_rx_buf = &rxr->rx_buf_ring[prod];
591 cons_rx_buf = &rxr->rx_buf_ring[cons];
593 prod_rx_buf->data = data;
595 dma_unmap_addr_set(prod_rx_buf, mapping,
596 dma_unmap_addr(cons_rx_buf, mapping));
598 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
599 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
601 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
604 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
606 u16 next, max = rxr->rx_agg_bmap_size;
608 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
610 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
614 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
615 struct bnxt_rx_ring_info *rxr,
619 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
620 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
621 struct pci_dev *pdev = bp->pdev;
624 u16 sw_prod = rxr->rx_sw_agg_prod;
625 unsigned int offset = 0;
627 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
630 page = alloc_page(gfp);
634 rxr->rx_page_offset = 0;
636 offset = rxr->rx_page_offset;
637 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
638 if (rxr->rx_page_offset == PAGE_SIZE)
643 page = alloc_page(gfp);
648 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
650 if (dma_mapping_error(&pdev->dev, mapping)) {
655 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
656 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
658 __set_bit(sw_prod, rxr->rx_agg_bmap);
659 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
660 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
662 rx_agg_buf->page = page;
663 rx_agg_buf->offset = offset;
664 rx_agg_buf->mapping = mapping;
665 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
666 rxbd->rx_bd_opaque = sw_prod;
670 static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
673 struct bnxt *bp = bnapi->bp;
674 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
675 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
676 u16 prod = rxr->rx_agg_prod;
677 u16 sw_prod = rxr->rx_sw_agg_prod;
680 for (i = 0; i < agg_bufs; i++) {
682 struct rx_agg_cmp *agg;
683 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
684 struct rx_bd *prod_bd;
687 agg = (struct rx_agg_cmp *)
688 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
689 cons = agg->rx_agg_cmp_opaque;
690 __clear_bit(cons, rxr->rx_agg_bmap);
692 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
693 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
695 __set_bit(sw_prod, rxr->rx_agg_bmap);
696 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
697 cons_rx_buf = &rxr->rx_agg_ring[cons];
699 /* It is possible for sw_prod to be equal to cons, so
700 * set cons_rx_buf->page to NULL first.
702 page = cons_rx_buf->page;
703 cons_rx_buf->page = NULL;
704 prod_rx_buf->page = page;
705 prod_rx_buf->offset = cons_rx_buf->offset;
707 prod_rx_buf->mapping = cons_rx_buf->mapping;
709 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
711 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
712 prod_bd->rx_bd_opaque = sw_prod;
714 prod = NEXT_RX_AGG(prod);
715 sw_prod = NEXT_RX_AGG(sw_prod);
716 cp_cons = NEXT_CMP(cp_cons);
718 rxr->rx_agg_prod = prod;
719 rxr->rx_sw_agg_prod = sw_prod;
722 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
723 struct bnxt_rx_ring_info *rxr, u16 cons,
724 u16 prod, u8 *data, dma_addr_t dma_addr,
730 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
732 bnxt_reuse_rx_data(rxr, cons, data);
736 skb = build_skb(data, 0);
737 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
744 skb_reserve(skb, BNXT_RX_OFFSET);
749 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
750 struct sk_buff *skb, u16 cp_cons,
753 struct pci_dev *pdev = bp->pdev;
754 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
755 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
756 u16 prod = rxr->rx_agg_prod;
759 for (i = 0; i < agg_bufs; i++) {
761 struct rx_agg_cmp *agg;
762 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
766 agg = (struct rx_agg_cmp *)
767 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
768 cons = agg->rx_agg_cmp_opaque;
769 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
770 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
772 cons_rx_buf = &rxr->rx_agg_ring[cons];
773 skb_fill_page_desc(skb, i, cons_rx_buf->page,
774 cons_rx_buf->offset, frag_len);
775 __clear_bit(cons, rxr->rx_agg_bmap);
777 /* It is possible for bnxt_alloc_rx_page() to allocate
778 * a sw_prod index that equals the cons index, so we
779 * need to clear the cons entry now.
781 mapping = dma_unmap_addr(cons_rx_buf, mapping);
782 page = cons_rx_buf->page;
783 cons_rx_buf->page = NULL;
785 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
786 struct skb_shared_info *shinfo;
787 unsigned int nr_frags;
789 shinfo = skb_shinfo(skb);
790 nr_frags = --shinfo->nr_frags;
791 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
795 cons_rx_buf->page = page;
797 /* Update prod since possibly some pages have been
800 rxr->rx_agg_prod = prod;
801 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
805 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
808 skb->data_len += frag_len;
809 skb->len += frag_len;
810 skb->truesize += PAGE_SIZE;
812 prod = NEXT_RX_AGG(prod);
813 cp_cons = NEXT_CMP(cp_cons);
815 rxr->rx_agg_prod = prod;
819 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
820 u8 agg_bufs, u32 *raw_cons)
823 struct rx_agg_cmp *agg;
825 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
826 last = RING_CMP(*raw_cons);
827 agg = (struct rx_agg_cmp *)
828 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
829 return RX_AGG_CMP_VALID(agg, *raw_cons);
832 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
836 struct bnxt *bp = bnapi->bp;
837 struct pci_dev *pdev = bp->pdev;
840 skb = napi_alloc_skb(&bnapi->napi, len);
844 dma_sync_single_for_cpu(&pdev->dev, mapping,
845 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
847 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
849 dma_sync_single_for_device(&pdev->dev, mapping,
857 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
858 u32 *raw_cons, void *cmp)
860 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
861 struct rx_cmp *rxcmp = cmp;
862 u32 tmp_raw_cons = *raw_cons;
863 u8 cmp_type, agg_bufs = 0;
865 cmp_type = RX_CMP_TYPE(rxcmp);
867 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
868 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
870 RX_CMP_AGG_BUFS_SHIFT;
871 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
872 struct rx_tpa_end_cmp *tpa_end = cmp;
874 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
875 RX_TPA_END_CMP_AGG_BUFS) >>
876 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
880 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
883 *raw_cons = tmp_raw_cons;
887 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
889 if (!rxr->bnapi->in_reset) {
890 rxr->bnapi->in_reset = true;
891 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
892 schedule_work(&bp->sp_task);
894 rxr->rx_next_cons = 0xffff;
897 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
898 struct rx_tpa_start_cmp *tpa_start,
899 struct rx_tpa_start_cmp_ext *tpa_start1)
901 u8 agg_id = TPA_START_AGG_ID(tpa_start);
903 struct bnxt_tpa_info *tpa_info;
904 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
905 struct rx_bd *prod_bd;
908 cons = tpa_start->rx_tpa_start_cmp_opaque;
910 cons_rx_buf = &rxr->rx_buf_ring[cons];
911 prod_rx_buf = &rxr->rx_buf_ring[prod];
912 tpa_info = &rxr->rx_tpa[agg_id];
914 if (unlikely(cons != rxr->rx_next_cons)) {
915 bnxt_sched_reset(bp, rxr);
919 prod_rx_buf->data = tpa_info->data;
921 mapping = tpa_info->mapping;
922 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
924 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
926 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
928 tpa_info->data = cons_rx_buf->data;
929 cons_rx_buf->data = NULL;
930 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
933 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
934 RX_TPA_START_CMP_LEN_SHIFT;
935 if (likely(TPA_START_HASH_VALID(tpa_start))) {
936 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
938 tpa_info->hash_type = PKT_HASH_TYPE_L4;
939 tpa_info->gso_type = SKB_GSO_TCPV4;
940 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
942 tpa_info->gso_type = SKB_GSO_TCPV6;
944 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
946 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
947 tpa_info->gso_type = 0;
948 if (netif_msg_rx_err(bp))
949 netdev_warn(bp->dev, "TPA packet without valid hash\n");
951 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
952 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
953 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
955 rxr->rx_prod = NEXT_RX(prod);
956 cons = NEXT_RX(cons);
957 rxr->rx_next_cons = NEXT_RX(cons);
958 cons_rx_buf = &rxr->rx_buf_ring[cons];
960 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
961 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
962 cons_rx_buf->data = NULL;
965 static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
966 u16 cp_cons, u32 agg_bufs)
969 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
972 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
973 int payload_off, int tcp_ts,
979 u16 outer_ip_off, inner_ip_off, inner_mac_off;
980 u32 hdr_info = tpa_info->hdr_info;
981 bool loopback = false;
983 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
984 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
985 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
987 /* If the packet is an internal loopback packet, the offsets will
988 * have an extra 4 bytes.
990 if (inner_mac_off == 4) {
992 } else if (inner_mac_off > 4) {
993 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
996 /* We only support inner iPv4/ipv6. If we don't see the
997 * correct protocol ID, it must be a loopback packet where
998 * the offsets are off by 4.
1000 if (proto != htons(ETH_P_IP) && proto && htons(ETH_P_IPV6))
1004 /* internal loopback packet, subtract all offsets by 4 */
1010 nw_off = inner_ip_off - ETH_HLEN;
1011 skb_set_network_header(skb, nw_off);
1012 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1013 struct ipv6hdr *iph = ipv6_hdr(skb);
1015 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1016 len = skb->len - skb_transport_offset(skb);
1018 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1020 struct iphdr *iph = ip_hdr(skb);
1022 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1023 len = skb->len - skb_transport_offset(skb);
1025 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1028 if (inner_mac_off) { /* tunnel */
1029 struct udphdr *uh = NULL;
1030 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1033 if (proto == htons(ETH_P_IP)) {
1034 struct iphdr *iph = (struct iphdr *)skb->data;
1036 if (iph->protocol == IPPROTO_UDP)
1037 uh = (struct udphdr *)(iph + 1);
1039 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1041 if (iph->nexthdr == IPPROTO_UDP)
1042 uh = (struct udphdr *)(iph + 1);
1046 skb_shinfo(skb)->gso_type |=
1047 SKB_GSO_UDP_TUNNEL_CSUM;
1049 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1056 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1057 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1059 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1060 int payload_off, int tcp_ts,
1061 struct sk_buff *skb)
1065 int len, nw_off, tcp_opt_len;
1070 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1073 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1075 skb_set_network_header(skb, nw_off);
1077 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1078 len = skb->len - skb_transport_offset(skb);
1080 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1081 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1082 struct ipv6hdr *iph;
1084 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1086 skb_set_network_header(skb, nw_off);
1087 iph = ipv6_hdr(skb);
1088 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1089 len = skb->len - skb_transport_offset(skb);
1091 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1093 dev_kfree_skb_any(skb);
1096 tcp_gro_complete(skb);
1098 if (nw_off) { /* tunnel */
1099 struct udphdr *uh = NULL;
1101 if (skb->protocol == htons(ETH_P_IP)) {
1102 struct iphdr *iph = (struct iphdr *)skb->data;
1104 if (iph->protocol == IPPROTO_UDP)
1105 uh = (struct udphdr *)(iph + 1);
1107 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1109 if (iph->nexthdr == IPPROTO_UDP)
1110 uh = (struct udphdr *)(iph + 1);
1114 skb_shinfo(skb)->gso_type |=
1115 SKB_GSO_UDP_TUNNEL_CSUM;
1117 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1124 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1125 struct bnxt_tpa_info *tpa_info,
1126 struct rx_tpa_end_cmp *tpa_end,
1127 struct rx_tpa_end_cmp_ext *tpa_end1,
1128 struct sk_buff *skb)
1134 segs = TPA_END_TPA_SEGS(tpa_end);
1138 NAPI_GRO_CB(skb)->count = segs;
1139 skb_shinfo(skb)->gso_size =
1140 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1141 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1142 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1143 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1144 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1145 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1150 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1151 struct bnxt_napi *bnapi,
1153 struct rx_tpa_end_cmp *tpa_end,
1154 struct rx_tpa_end_cmp_ext *tpa_end1,
1157 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1158 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1159 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1161 u16 cp_cons = RING_CMP(*raw_cons);
1163 struct bnxt_tpa_info *tpa_info;
1165 struct sk_buff *skb;
1167 if (unlikely(bnapi->in_reset)) {
1168 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1171 return ERR_PTR(-EBUSY);
1175 tpa_info = &rxr->rx_tpa[agg_id];
1176 data = tpa_info->data;
1178 len = tpa_info->len;
1179 mapping = tpa_info->mapping;
1181 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1182 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1185 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1186 return ERR_PTR(-EBUSY);
1189 cp_cons = NEXT_CMP(cp_cons);
1192 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1193 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1194 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1195 agg_bufs, (int)MAX_SKB_FRAGS);
1199 if (len <= bp->rx_copy_thresh) {
1200 skb = bnxt_copy_skb(bnapi, data, len, mapping);
1202 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1207 dma_addr_t new_mapping;
1209 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1211 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1215 tpa_info->data = new_data;
1216 tpa_info->mapping = new_mapping;
1218 skb = build_skb(data, 0);
1219 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1220 PCI_DMA_FROMDEVICE);
1224 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1227 skb_reserve(skb, BNXT_RX_OFFSET);
1232 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1234 /* Page reuse already handled by bnxt_rx_pages(). */
1238 skb->protocol = eth_type_trans(skb, bp->dev);
1240 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1241 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1243 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1244 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1245 u16 vlan_proto = tpa_info->metadata >>
1246 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1247 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
1249 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1252 skb_checksum_none_assert(skb);
1253 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1254 skb->ip_summed = CHECKSUM_UNNECESSARY;
1256 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1259 if (TPA_END_GRO(tpa_end))
1260 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1265 /* returns the following:
1266 * 1 - 1 packet successfully received
1267 * 0 - successful TPA_START, packet not completed yet
1268 * -EBUSY - completion ring does not have all the agg buffers yet
1269 * -ENOMEM - packet aborted due to out of memory
1270 * -EIO - packet aborted due to hw error indicated in BD
1272 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1275 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1276 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1277 struct net_device *dev = bp->dev;
1278 struct rx_cmp *rxcmp;
1279 struct rx_cmp_ext *rxcmp1;
1280 u32 tmp_raw_cons = *raw_cons;
1281 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1282 struct bnxt_sw_rx_bd *rx_buf;
1284 u8 *data, agg_bufs, cmp_type;
1285 dma_addr_t dma_addr;
1286 struct sk_buff *skb;
1289 rxcmp = (struct rx_cmp *)
1290 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1292 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1293 cp_cons = RING_CMP(tmp_raw_cons);
1294 rxcmp1 = (struct rx_cmp_ext *)
1295 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1297 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1300 cmp_type = RX_CMP_TYPE(rxcmp);
1302 prod = rxr->rx_prod;
1304 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1305 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1306 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1308 goto next_rx_no_prod;
1310 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1311 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1312 (struct rx_tpa_end_cmp *)rxcmp,
1313 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1316 if (unlikely(IS_ERR(skb)))
1321 skb_record_rx_queue(skb, bnapi->index);
1322 skb_mark_napi_id(skb, &bnapi->napi);
1323 if (bnxt_busy_polling(bnapi))
1324 netif_receive_skb(skb);
1326 napi_gro_receive(&bnapi->napi, skb);
1329 goto next_rx_no_prod;
1332 cons = rxcmp->rx_cmp_opaque;
1333 rx_buf = &rxr->rx_buf_ring[cons];
1334 data = rx_buf->data;
1335 if (unlikely(cons != rxr->rx_next_cons)) {
1336 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1338 bnxt_sched_reset(bp, rxr);
1343 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1344 RX_CMP_AGG_BUFS_SHIFT;
1347 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1350 cp_cons = NEXT_CMP(cp_cons);
1354 rx_buf->data = NULL;
1355 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1356 bnxt_reuse_rx_data(rxr, cons, data);
1358 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1364 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1365 dma_addr = dma_unmap_addr(rx_buf, mapping);
1367 if (len <= bp->rx_copy_thresh) {
1368 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1369 bnxt_reuse_rx_data(rxr, cons, data);
1375 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1383 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1390 if (RX_CMP_HASH_VALID(rxcmp)) {
1391 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1392 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1394 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1395 if (hash_type != 1 && hash_type != 3)
1396 type = PKT_HASH_TYPE_L3;
1397 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1400 skb->protocol = eth_type_trans(skb, dev);
1402 if ((rxcmp1->rx_cmp_flags2 &
1403 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1404 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1405 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1406 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
1407 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1409 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1412 skb_checksum_none_assert(skb);
1413 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1414 if (dev->features & NETIF_F_RXCSUM) {
1415 skb->ip_summed = CHECKSUM_UNNECESSARY;
1416 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1419 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1420 if (dev->features & NETIF_F_RXCSUM)
1421 cpr->rx_l4_csum_errors++;
1425 skb_record_rx_queue(skb, bnapi->index);
1426 skb_mark_napi_id(skb, &bnapi->napi);
1427 if (bnxt_busy_polling(bnapi))
1428 netif_receive_skb(skb);
1430 napi_gro_receive(&bnapi->napi, skb);
1434 rxr->rx_prod = NEXT_RX(prod);
1435 rxr->rx_next_cons = NEXT_RX(cons);
1438 *raw_cons = tmp_raw_cons;
1443 #define BNXT_GET_EVENT_PORT(data) \
1445 HWRM_ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1447 static int bnxt_async_event_process(struct bnxt *bp,
1448 struct hwrm_async_event_cmpl *cmpl)
1450 u16 event_id = le16_to_cpu(cmpl->event_id);
1452 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1454 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1455 u32 data1 = le32_to_cpu(cmpl->event_data1);
1456 struct bnxt_link_info *link_info = &bp->link_info;
1459 goto async_event_process_exit;
1460 if (data1 & 0x20000) {
1461 u16 fw_speed = link_info->force_link_speed;
1462 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1464 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1469 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1470 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1472 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1473 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1475 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1476 u32 data1 = le32_to_cpu(cmpl->event_data1);
1477 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1482 if (bp->pf.port_id != port_id)
1485 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1488 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1490 goto async_event_process_exit;
1491 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1494 netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
1496 goto async_event_process_exit;
1498 schedule_work(&bp->sp_task);
1499 async_event_process_exit:
1503 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1505 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1506 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1507 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1508 (struct hwrm_fwd_req_cmpl *)txcmp;
1510 switch (cmpl_type) {
1511 case CMPL_BASE_TYPE_HWRM_DONE:
1512 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1513 if (seq_id == bp->hwrm_intr_seq_id)
1514 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1516 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1519 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1520 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1522 if ((vf_id < bp->pf.first_vf_id) ||
1523 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1524 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1529 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1530 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1531 schedule_work(&bp->sp_task);
1534 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1535 bnxt_async_event_process(bp,
1536 (struct hwrm_async_event_cmpl *)txcmp);
1545 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1547 struct bnxt_napi *bnapi = dev_instance;
1548 struct bnxt *bp = bnapi->bp;
1549 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1550 u32 cons = RING_CMP(cpr->cp_raw_cons);
1552 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1553 napi_schedule(&bnapi->napi);
1557 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1559 u32 raw_cons = cpr->cp_raw_cons;
1560 u16 cons = RING_CMP(raw_cons);
1561 struct tx_cmp *txcmp;
1563 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1565 return TX_CMP_VALID(txcmp, raw_cons);
1568 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1570 struct bnxt_napi *bnapi = dev_instance;
1571 struct bnxt *bp = bnapi->bp;
1572 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1573 u32 cons = RING_CMP(cpr->cp_raw_cons);
1576 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1578 if (!bnxt_has_work(bp, cpr)) {
1579 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
1580 /* return if erroneous interrupt */
1581 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1585 /* disable ring IRQ */
1586 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1588 /* Return here if interrupt is shared and is disabled. */
1589 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1592 napi_schedule(&bnapi->napi);
1596 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1598 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1599 u32 raw_cons = cpr->cp_raw_cons;
1603 bool rx_event = false;
1604 bool agg_event = false;
1605 struct tx_cmp *txcmp;
1610 cons = RING_CMP(raw_cons);
1611 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1613 if (!TX_CMP_VALID(txcmp, raw_cons))
1616 /* The valid test of the entry must be done first before
1617 * reading any further.
1620 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1622 /* return full budget so NAPI will complete. */
1623 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1625 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1626 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1627 if (likely(rc >= 0))
1629 else if (rc == -EBUSY) /* partial completion */
1632 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1633 CMPL_BASE_TYPE_HWRM_DONE) ||
1634 (TX_CMP_TYPE(txcmp) ==
1635 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1636 (TX_CMP_TYPE(txcmp) ==
1637 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1638 bnxt_hwrm_handler(bp, txcmp);
1640 raw_cons = NEXT_RAW_CMP(raw_cons);
1642 if (rx_pkts == budget)
1646 cpr->cp_raw_cons = raw_cons;
1647 /* ACK completion ring before freeing tx ring and producing new
1648 * buffers in rx/agg rings to prevent overflowing the completion
1651 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1654 bnxt_tx_int(bp, bnapi, tx_pkts);
1657 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1659 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1660 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1662 writel(DB_KEY_RX | rxr->rx_agg_prod,
1663 rxr->rx_agg_doorbell);
1664 writel(DB_KEY_RX | rxr->rx_agg_prod,
1665 rxr->rx_agg_doorbell);
1671 static int bnxt_poll(struct napi_struct *napi, int budget)
1673 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1674 struct bnxt *bp = bnapi->bp;
1675 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1678 if (!bnxt_lock_napi(bnapi))
1682 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1684 if (work_done >= budget)
1687 if (!bnxt_has_work(bp, cpr)) {
1688 napi_complete(napi);
1689 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1694 bnxt_unlock_napi(bnapi);
1698 #ifdef CONFIG_NET_RX_BUSY_POLL
1699 static int bnxt_busy_poll(struct napi_struct *napi)
1701 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1702 struct bnxt *bp = bnapi->bp;
1703 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1704 int rx_work, budget = 4;
1706 if (atomic_read(&bp->intr_sem) != 0)
1707 return LL_FLUSH_FAILED;
1709 if (!bnxt_lock_poll(bnapi))
1710 return LL_FLUSH_BUSY;
1712 rx_work = bnxt_poll_work(bp, bnapi, budget);
1714 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1716 bnxt_unlock_poll(bnapi);
1721 static void bnxt_free_tx_skbs(struct bnxt *bp)
1724 struct pci_dev *pdev = bp->pdev;
1729 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1730 for (i = 0; i < bp->tx_nr_rings; i++) {
1731 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
1734 for (j = 0; j < max_idx;) {
1735 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1736 struct sk_buff *skb = tx_buf->skb;
1746 if (tx_buf->is_push) {
1752 dma_unmap_single(&pdev->dev,
1753 dma_unmap_addr(tx_buf, mapping),
1757 last = tx_buf->nr_frags;
1759 for (k = 0; k < last; k++, j++) {
1760 int ring_idx = j & bp->tx_ring_mask;
1761 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1763 tx_buf = &txr->tx_buf_ring[ring_idx];
1766 dma_unmap_addr(tx_buf, mapping),
1767 skb_frag_size(frag), PCI_DMA_TODEVICE);
1771 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1775 static void bnxt_free_rx_skbs(struct bnxt *bp)
1777 int i, max_idx, max_agg_idx;
1778 struct pci_dev *pdev = bp->pdev;
1783 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1784 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1785 for (i = 0; i < bp->rx_nr_rings; i++) {
1786 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
1790 for (j = 0; j < MAX_TPA; j++) {
1791 struct bnxt_tpa_info *tpa_info =
1793 u8 *data = tpa_info->data;
1800 dma_unmap_addr(tpa_info, mapping),
1801 bp->rx_buf_use_size,
1802 PCI_DMA_FROMDEVICE);
1804 tpa_info->data = NULL;
1810 for (j = 0; j < max_idx; j++) {
1811 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1812 u8 *data = rx_buf->data;
1817 dma_unmap_single(&pdev->dev,
1818 dma_unmap_addr(rx_buf, mapping),
1819 bp->rx_buf_use_size,
1820 PCI_DMA_FROMDEVICE);
1822 rx_buf->data = NULL;
1827 for (j = 0; j < max_agg_idx; j++) {
1828 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1829 &rxr->rx_agg_ring[j];
1830 struct page *page = rx_agg_buf->page;
1835 dma_unmap_page(&pdev->dev,
1836 dma_unmap_addr(rx_agg_buf, mapping),
1837 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
1839 rx_agg_buf->page = NULL;
1840 __clear_bit(j, rxr->rx_agg_bmap);
1845 __free_page(rxr->rx_page);
1846 rxr->rx_page = NULL;
1851 static void bnxt_free_skbs(struct bnxt *bp)
1853 bnxt_free_tx_skbs(bp);
1854 bnxt_free_rx_skbs(bp);
1857 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1859 struct pci_dev *pdev = bp->pdev;
1862 for (i = 0; i < ring->nr_pages; i++) {
1863 if (!ring->pg_arr[i])
1866 dma_free_coherent(&pdev->dev, ring->page_size,
1867 ring->pg_arr[i], ring->dma_arr[i]);
1869 ring->pg_arr[i] = NULL;
1872 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1873 ring->pg_tbl, ring->pg_tbl_map);
1874 ring->pg_tbl = NULL;
1876 if (ring->vmem_size && *ring->vmem) {
1882 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1885 struct pci_dev *pdev = bp->pdev;
1887 if (ring->nr_pages > 1) {
1888 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1896 for (i = 0; i < ring->nr_pages; i++) {
1897 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
1901 if (!ring->pg_arr[i])
1904 if (ring->nr_pages > 1)
1905 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
1908 if (ring->vmem_size) {
1909 *ring->vmem = vzalloc(ring->vmem_size);
1916 static void bnxt_free_rx_rings(struct bnxt *bp)
1923 for (i = 0; i < bp->rx_nr_rings; i++) {
1924 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
1925 struct bnxt_ring_struct *ring;
1930 kfree(rxr->rx_agg_bmap);
1931 rxr->rx_agg_bmap = NULL;
1933 ring = &rxr->rx_ring_struct;
1934 bnxt_free_ring(bp, ring);
1936 ring = &rxr->rx_agg_ring_struct;
1937 bnxt_free_ring(bp, ring);
1941 static int bnxt_alloc_rx_rings(struct bnxt *bp)
1943 int i, rc, agg_rings = 0, tpa_rings = 0;
1948 if (bp->flags & BNXT_FLAG_AGG_RINGS)
1951 if (bp->flags & BNXT_FLAG_TPA)
1954 for (i = 0; i < bp->rx_nr_rings; i++) {
1955 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
1956 struct bnxt_ring_struct *ring;
1958 ring = &rxr->rx_ring_struct;
1960 rc = bnxt_alloc_ring(bp, ring);
1967 ring = &rxr->rx_agg_ring_struct;
1968 rc = bnxt_alloc_ring(bp, ring);
1972 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
1973 mem_size = rxr->rx_agg_bmap_size / 8;
1974 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
1975 if (!rxr->rx_agg_bmap)
1979 rxr->rx_tpa = kcalloc(MAX_TPA,
1980 sizeof(struct bnxt_tpa_info),
1990 static void bnxt_free_tx_rings(struct bnxt *bp)
1993 struct pci_dev *pdev = bp->pdev;
1998 for (i = 0; i < bp->tx_nr_rings; i++) {
1999 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2000 struct bnxt_ring_struct *ring;
2003 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2004 txr->tx_push, txr->tx_push_mapping);
2005 txr->tx_push = NULL;
2008 ring = &txr->tx_ring_struct;
2010 bnxt_free_ring(bp, ring);
2014 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2017 struct pci_dev *pdev = bp->pdev;
2019 bp->tx_push_size = 0;
2020 if (bp->tx_push_thresh) {
2023 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2024 bp->tx_push_thresh);
2026 if (push_size > 256) {
2028 bp->tx_push_thresh = 0;
2031 bp->tx_push_size = push_size;
2034 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2035 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2036 struct bnxt_ring_struct *ring;
2038 ring = &txr->tx_ring_struct;
2040 rc = bnxt_alloc_ring(bp, ring);
2044 if (bp->tx_push_size) {
2047 /* One pre-allocated DMA buffer to backup
2050 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2052 &txr->tx_push_mapping,
2058 mapping = txr->tx_push_mapping +
2059 sizeof(struct tx_push_bd);
2060 txr->data_mapping = cpu_to_le64(mapping);
2062 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
2064 ring->queue_id = bp->q_info[j].queue_id;
2065 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2071 static void bnxt_free_cp_rings(struct bnxt *bp)
2078 for (i = 0; i < bp->cp_nr_rings; i++) {
2079 struct bnxt_napi *bnapi = bp->bnapi[i];
2080 struct bnxt_cp_ring_info *cpr;
2081 struct bnxt_ring_struct *ring;
2086 cpr = &bnapi->cp_ring;
2087 ring = &cpr->cp_ring_struct;
2089 bnxt_free_ring(bp, ring);
2093 static int bnxt_alloc_cp_rings(struct bnxt *bp)
2097 for (i = 0; i < bp->cp_nr_rings; i++) {
2098 struct bnxt_napi *bnapi = bp->bnapi[i];
2099 struct bnxt_cp_ring_info *cpr;
2100 struct bnxt_ring_struct *ring;
2105 cpr = &bnapi->cp_ring;
2106 ring = &cpr->cp_ring_struct;
2108 rc = bnxt_alloc_ring(bp, ring);
2115 static void bnxt_init_ring_struct(struct bnxt *bp)
2119 for (i = 0; i < bp->cp_nr_rings; i++) {
2120 struct bnxt_napi *bnapi = bp->bnapi[i];
2121 struct bnxt_cp_ring_info *cpr;
2122 struct bnxt_rx_ring_info *rxr;
2123 struct bnxt_tx_ring_info *txr;
2124 struct bnxt_ring_struct *ring;
2129 cpr = &bnapi->cp_ring;
2130 ring = &cpr->cp_ring_struct;
2131 ring->nr_pages = bp->cp_nr_pages;
2132 ring->page_size = HW_CMPD_RING_SIZE;
2133 ring->pg_arr = (void **)cpr->cp_desc_ring;
2134 ring->dma_arr = cpr->cp_desc_mapping;
2135 ring->vmem_size = 0;
2137 rxr = bnapi->rx_ring;
2141 ring = &rxr->rx_ring_struct;
2142 ring->nr_pages = bp->rx_nr_pages;
2143 ring->page_size = HW_RXBD_RING_SIZE;
2144 ring->pg_arr = (void **)rxr->rx_desc_ring;
2145 ring->dma_arr = rxr->rx_desc_mapping;
2146 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2147 ring->vmem = (void **)&rxr->rx_buf_ring;
2149 ring = &rxr->rx_agg_ring_struct;
2150 ring->nr_pages = bp->rx_agg_nr_pages;
2151 ring->page_size = HW_RXBD_RING_SIZE;
2152 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2153 ring->dma_arr = rxr->rx_agg_desc_mapping;
2154 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2155 ring->vmem = (void **)&rxr->rx_agg_ring;
2158 txr = bnapi->tx_ring;
2162 ring = &txr->tx_ring_struct;
2163 ring->nr_pages = bp->tx_nr_pages;
2164 ring->page_size = HW_RXBD_RING_SIZE;
2165 ring->pg_arr = (void **)txr->tx_desc_ring;
2166 ring->dma_arr = txr->tx_desc_mapping;
2167 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2168 ring->vmem = (void **)&txr->tx_buf_ring;
2172 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2176 struct rx_bd **rx_buf_ring;
2178 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2179 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2183 rxbd = rx_buf_ring[i];
2187 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2188 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2189 rxbd->rx_bd_opaque = prod;
2194 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2196 struct net_device *dev = bp->dev;
2197 struct bnxt_rx_ring_info *rxr;
2198 struct bnxt_ring_struct *ring;
2202 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2203 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2205 if (NET_IP_ALIGN == 2)
2206 type |= RX_BD_FLAGS_SOP;
2208 rxr = &bp->rx_ring[ring_nr];
2209 ring = &rxr->rx_ring_struct;
2210 bnxt_init_rxbd_pages(ring, type);
2212 prod = rxr->rx_prod;
2213 for (i = 0; i < bp->rx_ring_size; i++) {
2214 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2215 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2216 ring_nr, i, bp->rx_ring_size);
2219 prod = NEXT_RX(prod);
2221 rxr->rx_prod = prod;
2222 ring->fw_ring_id = INVALID_HW_RING_ID;
2224 ring = &rxr->rx_agg_ring_struct;
2225 ring->fw_ring_id = INVALID_HW_RING_ID;
2227 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2230 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
2231 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2233 bnxt_init_rxbd_pages(ring, type);
2235 prod = rxr->rx_agg_prod;
2236 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2237 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2238 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2239 ring_nr, i, bp->rx_ring_size);
2242 prod = NEXT_RX_AGG(prod);
2244 rxr->rx_agg_prod = prod;
2246 if (bp->flags & BNXT_FLAG_TPA) {
2251 for (i = 0; i < MAX_TPA; i++) {
2252 data = __bnxt_alloc_rx_data(bp, &mapping,
2257 rxr->rx_tpa[i].data = data;
2258 rxr->rx_tpa[i].mapping = mapping;
2261 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2269 static int bnxt_init_rx_rings(struct bnxt *bp)
2273 for (i = 0; i < bp->rx_nr_rings; i++) {
2274 rc = bnxt_init_one_rx_ring(bp, i);
2282 static int bnxt_init_tx_rings(struct bnxt *bp)
2286 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2289 for (i = 0; i < bp->tx_nr_rings; i++) {
2290 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2291 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2293 ring->fw_ring_id = INVALID_HW_RING_ID;
2299 static void bnxt_free_ring_grps(struct bnxt *bp)
2301 kfree(bp->grp_info);
2302 bp->grp_info = NULL;
2305 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2310 bp->grp_info = kcalloc(bp->cp_nr_rings,
2311 sizeof(struct bnxt_ring_grp_info),
2316 for (i = 0; i < bp->cp_nr_rings; i++) {
2318 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2319 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2320 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2321 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2322 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2327 static void bnxt_free_vnics(struct bnxt *bp)
2329 kfree(bp->vnic_info);
2330 bp->vnic_info = NULL;
2334 static int bnxt_alloc_vnics(struct bnxt *bp)
2338 #ifdef CONFIG_RFS_ACCEL
2339 if (bp->flags & BNXT_FLAG_RFS)
2340 num_vnics += bp->rx_nr_rings;
2343 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2348 bp->nr_vnics = num_vnics;
2352 static void bnxt_init_vnics(struct bnxt *bp)
2356 for (i = 0; i < bp->nr_vnics; i++) {
2357 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2359 vnic->fw_vnic_id = INVALID_HW_RING_ID;
2360 vnic->fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
2361 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2363 if (bp->vnic_info[i].rss_hash_key) {
2365 prandom_bytes(vnic->rss_hash_key,
2368 memcpy(vnic->rss_hash_key,
2369 bp->vnic_info[0].rss_hash_key,
2375 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2379 pages = ring_size / desc_per_pg;
2386 while (pages & (pages - 1))
2392 static void bnxt_set_tpa_flags(struct bnxt *bp)
2394 bp->flags &= ~BNXT_FLAG_TPA;
2395 if (bp->dev->features & NETIF_F_LRO)
2396 bp->flags |= BNXT_FLAG_LRO;
2397 if (bp->dev->features & NETIF_F_GRO)
2398 bp->flags |= BNXT_FLAG_GRO;
2401 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2404 void bnxt_set_ring_params(struct bnxt *bp)
2406 u32 ring_size, rx_size, rx_space;
2407 u32 agg_factor = 0, agg_ring_size = 0;
2409 /* 8 for CRC and VLAN */
2410 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2412 rx_space = rx_size + NET_SKB_PAD +
2413 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2415 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2416 ring_size = bp->rx_ring_size;
2417 bp->rx_agg_ring_size = 0;
2418 bp->rx_agg_nr_pages = 0;
2420 if (bp->flags & BNXT_FLAG_TPA)
2421 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
2423 bp->flags &= ~BNXT_FLAG_JUMBO;
2424 if (rx_space > PAGE_SIZE) {
2427 bp->flags |= BNXT_FLAG_JUMBO;
2428 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2429 if (jumbo_factor > agg_factor)
2430 agg_factor = jumbo_factor;
2432 agg_ring_size = ring_size * agg_factor;
2434 if (agg_ring_size) {
2435 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2437 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2438 u32 tmp = agg_ring_size;
2440 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2441 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2442 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2443 tmp, agg_ring_size);
2445 bp->rx_agg_ring_size = agg_ring_size;
2446 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2447 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2448 rx_space = rx_size + NET_SKB_PAD +
2449 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2452 bp->rx_buf_use_size = rx_size;
2453 bp->rx_buf_size = rx_space;
2455 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2456 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2458 ring_size = bp->tx_ring_size;
2459 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2460 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2462 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2463 bp->cp_ring_size = ring_size;
2465 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2466 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2467 bp->cp_nr_pages = MAX_CP_PAGES;
2468 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2469 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2470 ring_size, bp->cp_ring_size);
2472 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2473 bp->cp_ring_mask = bp->cp_bit - 1;
2476 static void bnxt_free_vnic_attributes(struct bnxt *bp)
2479 struct bnxt_vnic_info *vnic;
2480 struct pci_dev *pdev = bp->pdev;
2485 for (i = 0; i < bp->nr_vnics; i++) {
2486 vnic = &bp->vnic_info[i];
2488 kfree(vnic->fw_grp_ids);
2489 vnic->fw_grp_ids = NULL;
2491 kfree(vnic->uc_list);
2492 vnic->uc_list = NULL;
2494 if (vnic->mc_list) {
2495 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2496 vnic->mc_list, vnic->mc_list_mapping);
2497 vnic->mc_list = NULL;
2500 if (vnic->rss_table) {
2501 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2503 vnic->rss_table_dma_addr);
2504 vnic->rss_table = NULL;
2507 vnic->rss_hash_key = NULL;
2512 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2514 int i, rc = 0, size;
2515 struct bnxt_vnic_info *vnic;
2516 struct pci_dev *pdev = bp->pdev;
2519 for (i = 0; i < bp->nr_vnics; i++) {
2520 vnic = &bp->vnic_info[i];
2522 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2523 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2526 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2527 if (!vnic->uc_list) {
2534 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2535 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2537 dma_alloc_coherent(&pdev->dev,
2539 &vnic->mc_list_mapping,
2541 if (!vnic->mc_list) {
2547 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2548 max_rings = bp->rx_nr_rings;
2552 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2553 if (!vnic->fw_grp_ids) {
2558 /* Allocate rss table and hash key */
2559 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2560 &vnic->rss_table_dma_addr,
2562 if (!vnic->rss_table) {
2567 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2569 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2570 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2578 static void bnxt_free_hwrm_resources(struct bnxt *bp)
2580 struct pci_dev *pdev = bp->pdev;
2582 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2583 bp->hwrm_cmd_resp_dma_addr);
2585 bp->hwrm_cmd_resp_addr = NULL;
2586 if (bp->hwrm_dbg_resp_addr) {
2587 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2588 bp->hwrm_dbg_resp_addr,
2589 bp->hwrm_dbg_resp_dma_addr);
2591 bp->hwrm_dbg_resp_addr = NULL;
2595 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2597 struct pci_dev *pdev = bp->pdev;
2599 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2600 &bp->hwrm_cmd_resp_dma_addr,
2602 if (!bp->hwrm_cmd_resp_addr)
2604 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2605 HWRM_DBG_REG_BUF_SIZE,
2606 &bp->hwrm_dbg_resp_dma_addr,
2608 if (!bp->hwrm_dbg_resp_addr)
2609 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2614 static void bnxt_free_stats(struct bnxt *bp)
2617 struct pci_dev *pdev = bp->pdev;
2619 if (bp->hw_rx_port_stats) {
2620 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2621 bp->hw_rx_port_stats,
2622 bp->hw_rx_port_stats_map);
2623 bp->hw_rx_port_stats = NULL;
2624 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2630 size = sizeof(struct ctx_hw_stats);
2632 for (i = 0; i < bp->cp_nr_rings; i++) {
2633 struct bnxt_napi *bnapi = bp->bnapi[i];
2634 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2636 if (cpr->hw_stats) {
2637 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2639 cpr->hw_stats = NULL;
2644 static int bnxt_alloc_stats(struct bnxt *bp)
2647 struct pci_dev *pdev = bp->pdev;
2649 size = sizeof(struct ctx_hw_stats);
2651 for (i = 0; i < bp->cp_nr_rings; i++) {
2652 struct bnxt_napi *bnapi = bp->bnapi[i];
2653 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2655 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2661 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2665 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2666 sizeof(struct tx_port_stats) + 1024;
2668 bp->hw_rx_port_stats =
2669 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2670 &bp->hw_rx_port_stats_map,
2672 if (!bp->hw_rx_port_stats)
2675 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2677 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2678 sizeof(struct rx_port_stats) + 512;
2679 bp->flags |= BNXT_FLAG_PORT_STATS;
2684 static void bnxt_clear_ring_indices(struct bnxt *bp)
2691 for (i = 0; i < bp->cp_nr_rings; i++) {
2692 struct bnxt_napi *bnapi = bp->bnapi[i];
2693 struct bnxt_cp_ring_info *cpr;
2694 struct bnxt_rx_ring_info *rxr;
2695 struct bnxt_tx_ring_info *txr;
2700 cpr = &bnapi->cp_ring;
2701 cpr->cp_raw_cons = 0;
2703 txr = bnapi->tx_ring;
2709 rxr = bnapi->rx_ring;
2712 rxr->rx_agg_prod = 0;
2713 rxr->rx_sw_agg_prod = 0;
2714 rxr->rx_next_cons = 0;
2719 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2721 #ifdef CONFIG_RFS_ACCEL
2724 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2725 * safe to delete the hash table.
2727 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2728 struct hlist_head *head;
2729 struct hlist_node *tmp;
2730 struct bnxt_ntuple_filter *fltr;
2732 head = &bp->ntp_fltr_hash_tbl[i];
2733 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2734 hlist_del(&fltr->hash);
2739 kfree(bp->ntp_fltr_bmap);
2740 bp->ntp_fltr_bmap = NULL;
2742 bp->ntp_fltr_count = 0;
2746 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2748 #ifdef CONFIG_RFS_ACCEL
2751 if (!(bp->flags & BNXT_FLAG_RFS))
2754 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2755 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2757 bp->ntp_fltr_count = 0;
2758 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2761 if (!bp->ntp_fltr_bmap)
2770 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2772 bnxt_free_vnic_attributes(bp);
2773 bnxt_free_tx_rings(bp);
2774 bnxt_free_rx_rings(bp);
2775 bnxt_free_cp_rings(bp);
2776 bnxt_free_ntp_fltrs(bp, irq_re_init);
2778 bnxt_free_stats(bp);
2779 bnxt_free_ring_grps(bp);
2780 bnxt_free_vnics(bp);
2788 bnxt_clear_ring_indices(bp);
2792 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2794 int i, j, rc, size, arr_size;
2798 /* Allocate bnapi mem pointer array and mem block for
2801 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2803 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2804 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2810 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2811 bp->bnapi[i] = bnapi;
2812 bp->bnapi[i]->index = i;
2813 bp->bnapi[i]->bp = bp;
2816 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2817 sizeof(struct bnxt_rx_ring_info),
2822 for (i = 0; i < bp->rx_nr_rings; i++) {
2823 bp->rx_ring[i].bnapi = bp->bnapi[i];
2824 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2827 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2828 sizeof(struct bnxt_tx_ring_info),
2833 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2836 j = bp->rx_nr_rings;
2838 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2839 bp->tx_ring[i].bnapi = bp->bnapi[j];
2840 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
2843 rc = bnxt_alloc_stats(bp);
2847 rc = bnxt_alloc_ntp_fltrs(bp);
2851 rc = bnxt_alloc_vnics(bp);
2856 bnxt_init_ring_struct(bp);
2858 rc = bnxt_alloc_rx_rings(bp);
2862 rc = bnxt_alloc_tx_rings(bp);
2866 rc = bnxt_alloc_cp_rings(bp);
2870 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2871 BNXT_VNIC_UCAST_FLAG;
2872 rc = bnxt_alloc_vnic_attributes(bp);
2878 bnxt_free_mem(bp, true);
2882 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2883 u16 cmpl_ring, u16 target_id)
2885 struct input *req = request;
2887 req->req_type = cpu_to_le16(req_type);
2888 req->cmpl_ring = cpu_to_le16(cmpl_ring);
2889 req->target_id = cpu_to_le16(target_id);
2890 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
2893 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
2894 int timeout, bool silent)
2896 int i, intr_process, rc, tmo_count;
2897 struct input *req = msg;
2899 __le32 *resp_len, *valid;
2900 u16 cp_ring_id, len = 0;
2901 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
2903 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
2904 memset(resp, 0, PAGE_SIZE);
2905 cp_ring_id = le16_to_cpu(req->cmpl_ring);
2906 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
2908 /* Write request msg to hwrm channel */
2909 __iowrite32_copy(bp->bar0, data, msg_len / 4);
2911 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
2912 writel(0, bp->bar0 + i);
2914 /* currently supports only one outstanding message */
2916 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
2918 /* Ring channel doorbell */
2919 writel(1, bp->bar0 + 0x100);
2922 timeout = DFLT_HWRM_CMD_TIMEOUT;
2925 tmo_count = timeout * 40;
2927 /* Wait until hwrm response cmpl interrupt is processed */
2928 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
2930 usleep_range(25, 40);
2933 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
2934 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
2935 le16_to_cpu(req->req_type));
2939 /* Check if response len is updated */
2940 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
2941 for (i = 0; i < tmo_count; i++) {
2942 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
2946 usleep_range(25, 40);
2949 if (i >= tmo_count) {
2950 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
2951 timeout, le16_to_cpu(req->req_type),
2952 le16_to_cpu(req->seq_id), len);
2956 /* Last word of resp contains valid bit */
2957 valid = bp->hwrm_cmd_resp_addr + len - 4;
2958 for (i = 0; i < 5; i++) {
2959 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
2965 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
2966 timeout, le16_to_cpu(req->req_type),
2967 le16_to_cpu(req->seq_id), len, *valid);
2972 rc = le16_to_cpu(resp->error_code);
2974 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
2975 le16_to_cpu(resp->req_type),
2976 le16_to_cpu(resp->seq_id), rc);
2980 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
2982 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
2985 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
2989 mutex_lock(&bp->hwrm_cmd_lock);
2990 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
2991 mutex_unlock(&bp->hwrm_cmd_lock);
2995 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3000 mutex_lock(&bp->hwrm_cmd_lock);
3001 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3002 mutex_unlock(&bp->hwrm_cmd_lock);
3006 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3008 struct hwrm_func_drv_rgtr_input req = {0};
3010 DECLARE_BITMAP(async_events_bmap, 256);
3011 u32 *events = (u32 *)async_events_bmap;
3013 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3016 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3017 FUNC_DRV_RGTR_REQ_ENABLES_VER |
3018 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3020 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3021 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3022 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3024 for (i = 0; i < 8; i++)
3025 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3027 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
3028 req.ver_maj = DRV_VER_MAJ;
3029 req.ver_min = DRV_VER_MIN;
3030 req.ver_upd = DRV_VER_UPD;
3033 DECLARE_BITMAP(vf_req_snif_bmap, 256);
3034 u32 *data = (u32 *)vf_req_snif_bmap;
3036 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
3037 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3038 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3040 for (i = 0; i < 8; i++)
3041 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3044 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3047 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3050 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3052 struct hwrm_func_drv_unrgtr_input req = {0};
3054 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3055 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3058 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3061 struct hwrm_tunnel_dst_port_free_input req = {0};
3063 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3064 req.tunnel_type = tunnel_type;
3066 switch (tunnel_type) {
3067 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3068 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3070 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3071 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3077 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3079 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3084 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3088 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3089 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3091 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3093 req.tunnel_type = tunnel_type;
3094 req.tunnel_dst_port_val = port;
3096 mutex_lock(&bp->hwrm_cmd_lock);
3097 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3099 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3104 if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN)
3105 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3107 else if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE)
3108 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3110 mutex_unlock(&bp->hwrm_cmd_lock);
3114 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3116 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3117 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3119 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
3120 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3122 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3123 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3124 req.mask = cpu_to_le32(vnic->rx_mask);
3125 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3128 #ifdef CONFIG_RFS_ACCEL
3129 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3130 struct bnxt_ntuple_filter *fltr)
3132 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3134 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3135 req.ntuple_filter_id = fltr->filter_id;
3136 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3139 #define BNXT_NTP_FLTR_FLAGS \
3140 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3141 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3142 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3143 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3144 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3145 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3146 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3147 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3148 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3149 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3150 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3151 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3152 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
3153 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
3155 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3156 struct bnxt_ntuple_filter *fltr)
3159 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3160 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3161 bp->hwrm_cmd_resp_addr;
3162 struct flow_keys *keys = &fltr->fkeys;
3163 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3165 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
3166 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[0];
3168 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3170 req.ethertype = htons(ETH_P_IP);
3171 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
3172 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
3173 req.ip_protocol = keys->basic.ip_proto;
3175 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3176 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3177 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3178 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3180 req.src_port = keys->ports.src;
3181 req.src_port_mask = cpu_to_be16(0xffff);
3182 req.dst_port = keys->ports.dst;
3183 req.dst_port_mask = cpu_to_be16(0xffff);
3185 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
3186 mutex_lock(&bp->hwrm_cmd_lock);
3187 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3189 fltr->filter_id = resp->ntuple_filter_id;
3190 mutex_unlock(&bp->hwrm_cmd_lock);
3195 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3199 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3200 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3202 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
3203 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX |
3204 CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
3205 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
3207 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
3208 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
3209 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3210 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3211 req.l2_addr_mask[0] = 0xff;
3212 req.l2_addr_mask[1] = 0xff;
3213 req.l2_addr_mask[2] = 0xff;
3214 req.l2_addr_mask[3] = 0xff;
3215 req.l2_addr_mask[4] = 0xff;
3216 req.l2_addr_mask[5] = 0xff;
3218 mutex_lock(&bp->hwrm_cmd_lock);
3219 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3221 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3223 mutex_unlock(&bp->hwrm_cmd_lock);
3227 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3229 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3232 /* Any associated ntuple filters will also be cleared by firmware. */
3233 mutex_lock(&bp->hwrm_cmd_lock);
3234 for (i = 0; i < num_of_vnics; i++) {
3235 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3237 for (j = 0; j < vnic->uc_filter_count; j++) {
3238 struct hwrm_cfa_l2_filter_free_input req = {0};
3240 bnxt_hwrm_cmd_hdr_init(bp, &req,
3241 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3243 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3245 rc = _hwrm_send_message(bp, &req, sizeof(req),
3248 vnic->uc_filter_count = 0;
3250 mutex_unlock(&bp->hwrm_cmd_lock);
3255 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3257 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3258 struct hwrm_vnic_tpa_cfg_input req = {0};
3260 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3263 u16 mss = bp->dev->mtu - 40;
3264 u32 nsegs, n, segs = 0, flags;
3266 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3267 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3268 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3269 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3270 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3271 if (tpa_flags & BNXT_FLAG_GRO)
3272 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3274 req.flags = cpu_to_le32(flags);
3277 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
3278 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3279 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
3281 /* Number of segs are log2 units, and first packet is not
3282 * included as part of this units.
3284 if (mss <= BNXT_RX_PAGE_SIZE) {
3285 n = BNXT_RX_PAGE_SIZE / mss;
3286 nsegs = (MAX_SKB_FRAGS - 1) * n;
3288 n = mss / BNXT_RX_PAGE_SIZE;
3289 if (mss & (BNXT_RX_PAGE_SIZE - 1))
3291 nsegs = (MAX_SKB_FRAGS - n) / n;
3294 segs = ilog2(nsegs);
3295 req.max_agg_segs = cpu_to_le16(segs);
3296 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
3298 req.min_agg_len = cpu_to_le32(512);
3300 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3302 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3305 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3307 u32 i, j, max_rings;
3308 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3309 struct hwrm_vnic_rss_cfg_input req = {0};
3311 if (vnic->fw_rss_cos_lb_ctx == INVALID_HW_RING_ID)
3314 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3316 vnic->hash_type = BNXT_RSS_HASH_TYPE_FLAG_IPV4 |
3317 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV4 |
3318 BNXT_RSS_HASH_TYPE_FLAG_IPV6 |
3319 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV6;
3321 req.hash_type = cpu_to_le32(vnic->hash_type);
3323 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3324 max_rings = bp->rx_nr_rings;
3328 /* Fill the RSS indirection table with ring group ids */
3329 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3332 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3335 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3336 req.hash_key_tbl_addr =
3337 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3339 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx);
3340 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3343 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3345 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3346 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3348 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3349 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3350 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3351 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3353 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3354 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3355 /* thresholds not implemented in firmware yet */
3356 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3357 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3358 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3359 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3362 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id)
3364 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3366 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3367 req.rss_cos_lb_ctx_id =
3368 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx);
3370 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3371 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
3374 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3378 for (i = 0; i < bp->nr_vnics; i++) {
3379 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3381 if (vnic->fw_rss_cos_lb_ctx != INVALID_HW_RING_ID)
3382 bnxt_hwrm_vnic_ctx_free_one(bp, i);
3384 bp->rsscos_nr_ctxs = 0;
3387 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id)
3390 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3391 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3392 bp->hwrm_cmd_resp_addr;
3394 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3397 mutex_lock(&bp->hwrm_cmd_lock);
3398 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3400 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx =
3401 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3402 mutex_unlock(&bp->hwrm_cmd_lock);
3407 static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3409 unsigned int ring = 0, grp_idx;
3410 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3411 struct hwrm_vnic_cfg_input req = {0};
3414 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
3415 /* Only RSS support for now TBD: COS & LB */
3416 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP |
3417 VNIC_CFG_REQ_ENABLES_RSS_RULE);
3418 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx);
3419 req.cos_rule = cpu_to_le16(0xffff);
3420 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3422 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
3425 grp_idx = bp->rx_ring[ring].bnapi->index;
3426 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3427 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3429 req.lb_rule = cpu_to_le16(0xffff);
3430 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3433 #ifdef CONFIG_BNXT_SRIOV
3435 def_vlan = bp->vf.vlan;
3437 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
3438 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3440 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3443 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3447 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3448 struct hwrm_vnic_free_input req = {0};
3450 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3452 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3454 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3457 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3462 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3466 for (i = 0; i < bp->nr_vnics; i++)
3467 bnxt_hwrm_vnic_free_one(bp, i);
3470 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3471 unsigned int start_rx_ring_idx,
3472 unsigned int nr_rings)
3475 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
3476 struct hwrm_vnic_alloc_input req = {0};
3477 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3479 /* map ring groups to this vnic */
3480 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3481 grp_idx = bp->rx_ring[i].bnapi->index;
3482 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
3483 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
3487 bp->vnic_info[vnic_id].fw_grp_ids[j] =
3488 bp->grp_info[grp_idx].fw_grp_id;
3491 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
3493 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3495 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3497 mutex_lock(&bp->hwrm_cmd_lock);
3498 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3500 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3501 mutex_unlock(&bp->hwrm_cmd_lock);
3505 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3510 mutex_lock(&bp->hwrm_cmd_lock);
3511 for (i = 0; i < bp->rx_nr_rings; i++) {
3512 struct hwrm_ring_grp_alloc_input req = {0};
3513 struct hwrm_ring_grp_alloc_output *resp =
3514 bp->hwrm_cmd_resp_addr;
3515 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
3517 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3519 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3520 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3521 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3522 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
3524 rc = _hwrm_send_message(bp, &req, sizeof(req),
3529 bp->grp_info[grp_idx].fw_grp_id =
3530 le32_to_cpu(resp->ring_group_id);
3532 mutex_unlock(&bp->hwrm_cmd_lock);
3536 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3540 struct hwrm_ring_grp_free_input req = {0};
3545 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3547 mutex_lock(&bp->hwrm_cmd_lock);
3548 for (i = 0; i < bp->cp_nr_rings; i++) {
3549 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3552 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3554 rc = _hwrm_send_message(bp, &req, sizeof(req),
3558 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3560 mutex_unlock(&bp->hwrm_cmd_lock);
3564 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3565 struct bnxt_ring_struct *ring,
3566 u32 ring_type, u32 map_index,
3569 int rc = 0, err = 0;
3570 struct hwrm_ring_alloc_input req = {0};
3571 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3574 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3577 if (ring->nr_pages > 1) {
3578 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3579 /* Page size is in log2 units */
3580 req.page_size = BNXT_PAGE_SHIFT;
3581 req.page_tbl_depth = 1;
3583 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3586 /* Association of ring index with doorbell index and MSIX number */
3587 req.logical_id = cpu_to_le16(map_index);
3589 switch (ring_type) {
3590 case HWRM_RING_ALLOC_TX:
3591 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3592 /* Association of transmit ring with completion ring */
3594 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3595 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3596 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3597 req.queue_id = cpu_to_le16(ring->queue_id);
3599 case HWRM_RING_ALLOC_RX:
3600 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3601 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3603 case HWRM_RING_ALLOC_AGG:
3604 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3605 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3607 case HWRM_RING_ALLOC_CMPL:
3608 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3609 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3610 if (bp->flags & BNXT_FLAG_USING_MSIX)
3611 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3614 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3619 mutex_lock(&bp->hwrm_cmd_lock);
3620 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3621 err = le16_to_cpu(resp->error_code);
3622 ring_id = le16_to_cpu(resp->ring_id);
3623 mutex_unlock(&bp->hwrm_cmd_lock);
3626 switch (ring_type) {
3627 case RING_FREE_REQ_RING_TYPE_CMPL:
3628 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3632 case RING_FREE_REQ_RING_TYPE_RX:
3633 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3637 case RING_FREE_REQ_RING_TYPE_TX:
3638 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3643 netdev_err(bp->dev, "Invalid ring\n");
3647 ring->fw_ring_id = ring_id;
3651 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3655 for (i = 0; i < bp->cp_nr_rings; i++) {
3656 struct bnxt_napi *bnapi = bp->bnapi[i];
3657 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3658 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3660 cpr->cp_doorbell = bp->bar1 + i * 0x80;
3661 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3662 INVALID_STATS_CTX_ID);
3665 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3666 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
3669 for (i = 0; i < bp->tx_nr_rings; i++) {
3670 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3671 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3672 u32 map_idx = txr->bnapi->index;
3673 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
3675 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3676 map_idx, fw_stats_ctx);
3679 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
3682 for (i = 0; i < bp->rx_nr_rings; i++) {
3683 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3684 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
3685 u32 map_idx = rxr->bnapi->index;
3687 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3688 map_idx, INVALID_STATS_CTX_ID);
3691 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
3692 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
3693 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
3696 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3697 for (i = 0; i < bp->rx_nr_rings; i++) {
3698 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3699 struct bnxt_ring_struct *ring =
3700 &rxr->rx_agg_ring_struct;
3701 u32 grp_idx = rxr->bnapi->index;
3702 u32 map_idx = grp_idx + bp->rx_nr_rings;
3704 rc = hwrm_ring_alloc_send_msg(bp, ring,
3705 HWRM_RING_ALLOC_AGG,
3707 INVALID_STATS_CTX_ID);
3711 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
3712 writel(DB_KEY_RX | rxr->rx_agg_prod,
3713 rxr->rx_agg_doorbell);
3714 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
3721 static int hwrm_ring_free_send_msg(struct bnxt *bp,
3722 struct bnxt_ring_struct *ring,
3723 u32 ring_type, int cmpl_ring_id)
3726 struct hwrm_ring_free_input req = {0};
3727 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3730 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
3731 req.ring_type = ring_type;
3732 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3734 mutex_lock(&bp->hwrm_cmd_lock);
3735 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3736 error_code = le16_to_cpu(resp->error_code);
3737 mutex_unlock(&bp->hwrm_cmd_lock);
3739 if (rc || error_code) {
3740 switch (ring_type) {
3741 case RING_FREE_REQ_RING_TYPE_CMPL:
3742 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3745 case RING_FREE_REQ_RING_TYPE_RX:
3746 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3749 case RING_FREE_REQ_RING_TYPE_TX:
3750 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3754 netdev_err(bp->dev, "Invalid ring\n");
3761 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
3768 for (i = 0; i < bp->tx_nr_rings; i++) {
3769 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3770 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3771 u32 grp_idx = txr->bnapi->index;
3772 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
3774 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3775 hwrm_ring_free_send_msg(bp, ring,
3776 RING_FREE_REQ_RING_TYPE_TX,
3777 close_path ? cmpl_ring_id :
3778 INVALID_HW_RING_ID);
3779 ring->fw_ring_id = INVALID_HW_RING_ID;
3783 for (i = 0; i < bp->rx_nr_rings; i++) {
3784 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3785 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
3786 u32 grp_idx = rxr->bnapi->index;
3787 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
3789 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3790 hwrm_ring_free_send_msg(bp, ring,
3791 RING_FREE_REQ_RING_TYPE_RX,
3792 close_path ? cmpl_ring_id :
3793 INVALID_HW_RING_ID);
3794 ring->fw_ring_id = INVALID_HW_RING_ID;
3795 bp->grp_info[grp_idx].rx_fw_ring_id =
3800 for (i = 0; i < bp->rx_nr_rings; i++) {
3801 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3802 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
3803 u32 grp_idx = rxr->bnapi->index;
3804 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
3806 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3807 hwrm_ring_free_send_msg(bp, ring,
3808 RING_FREE_REQ_RING_TYPE_RX,
3809 close_path ? cmpl_ring_id :
3810 INVALID_HW_RING_ID);
3811 ring->fw_ring_id = INVALID_HW_RING_ID;
3812 bp->grp_info[grp_idx].agg_fw_ring_id =
3817 for (i = 0; i < bp->cp_nr_rings; i++) {
3818 struct bnxt_napi *bnapi = bp->bnapi[i];
3819 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3820 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3822 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3823 hwrm_ring_free_send_msg(bp, ring,
3824 RING_FREE_REQ_RING_TYPE_CMPL,
3825 INVALID_HW_RING_ID);
3826 ring->fw_ring_id = INVALID_HW_RING_ID;
3827 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3832 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
3833 u32 buf_tmrs, u16 flags,
3834 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3836 req->flags = cpu_to_le16(flags);
3837 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
3838 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
3839 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
3840 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
3841 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
3842 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
3843 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
3844 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
3847 int bnxt_hwrm_set_coal(struct bnxt *bp)
3850 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
3852 u16 max_buf, max_buf_irq;
3853 u16 buf_tmr, buf_tmr_irq;
3856 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
3857 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3858 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
3859 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3861 /* Each rx completion (2 records) should be DMAed immediately.
3862 * DMA 1/4 of the completion buffers at a time.
3864 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
3865 /* max_buf must not be zero */
3866 max_buf = clamp_t(u16, max_buf, 1, 63);
3867 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
3868 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
3869 /* buf timer set to 1/4 of interrupt timer */
3870 buf_tmr = max_t(u16, buf_tmr / 4, 1);
3871 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
3872 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
3874 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
3876 /* RING_IDLE generates more IRQs for lower latency. Enable it only
3877 * if coal_ticks is less than 25 us.
3879 if (bp->rx_coal_ticks < 25)
3880 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
3882 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
3883 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
3885 /* max_buf must not be zero */
3886 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
3887 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
3888 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
3889 /* buf timer set to 1/4 of interrupt timer */
3890 buf_tmr = max_t(u16, buf_tmr / 4, 1);
3891 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
3892 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
3894 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
3895 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
3896 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
3898 mutex_lock(&bp->hwrm_cmd_lock);
3899 for (i = 0; i < bp->cp_nr_rings; i++) {
3900 struct bnxt_napi *bnapi = bp->bnapi[i];
3903 if (!bnapi->rx_ring)
3905 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
3907 rc = _hwrm_send_message(bp, req, sizeof(*req),
3912 mutex_unlock(&bp->hwrm_cmd_lock);
3916 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
3919 struct hwrm_stat_ctx_free_input req = {0};
3924 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
3926 mutex_lock(&bp->hwrm_cmd_lock);
3927 for (i = 0; i < bp->cp_nr_rings; i++) {
3928 struct bnxt_napi *bnapi = bp->bnapi[i];
3929 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3931 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
3932 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
3934 rc = _hwrm_send_message(bp, &req, sizeof(req),
3939 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
3942 mutex_unlock(&bp->hwrm_cmd_lock);
3946 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
3949 struct hwrm_stat_ctx_alloc_input req = {0};
3950 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3952 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
3954 req.update_period_ms = cpu_to_le32(1000);
3956 mutex_lock(&bp->hwrm_cmd_lock);
3957 for (i = 0; i < bp->cp_nr_rings; i++) {
3958 struct bnxt_napi *bnapi = bp->bnapi[i];
3959 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3961 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
3963 rc = _hwrm_send_message(bp, &req, sizeof(req),
3968 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
3970 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
3972 mutex_unlock(&bp->hwrm_cmd_lock);
3976 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
3978 struct hwrm_func_qcfg_input req = {0};
3979 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
3982 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
3983 req.fid = cpu_to_le16(0xffff);
3984 mutex_lock(&bp->hwrm_cmd_lock);
3985 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3987 goto func_qcfg_exit;
3989 #ifdef CONFIG_BNXT_SRIOV
3991 struct bnxt_vf_info *vf = &bp->vf;
3993 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
3996 switch (resp->port_partition_type) {
3997 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
3998 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
3999 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4000 bp->port_partition_type = resp->port_partition_type;
4005 mutex_unlock(&bp->hwrm_cmd_lock);
4009 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
4012 struct hwrm_func_qcaps_input req = {0};
4013 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4015 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4016 req.fid = cpu_to_le16(0xffff);
4018 mutex_lock(&bp->hwrm_cmd_lock);
4019 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4021 goto hwrm_func_qcaps_exit;
4024 struct bnxt_pf_info *pf = &bp->pf;
4026 pf->fw_fid = le16_to_cpu(resp->fid);
4027 pf->port_id = le16_to_cpu(resp->port_id);
4028 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
4029 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
4030 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4031 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4032 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4033 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4034 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4035 if (!pf->max_hw_ring_grps)
4036 pf->max_hw_ring_grps = pf->max_tx_rings;
4037 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4038 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4039 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4040 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4041 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4042 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4043 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4044 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4045 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4046 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4047 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4049 #ifdef CONFIG_BNXT_SRIOV
4050 struct bnxt_vf_info *vf = &bp->vf;
4052 vf->fw_fid = le16_to_cpu(resp->fid);
4053 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
4054 if (is_valid_ether_addr(vf->mac_addr))
4055 /* overwrite netdev dev_adr with admin VF MAC */
4056 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
4058 random_ether_addr(bp->dev->dev_addr);
4060 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4061 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4062 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4063 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4064 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4065 if (!vf->max_hw_ring_grps)
4066 vf->max_hw_ring_grps = vf->max_tx_rings;
4067 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4068 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4069 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4073 bp->tx_push_thresh = 0;
4075 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4076 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4078 hwrm_func_qcaps_exit:
4079 mutex_unlock(&bp->hwrm_cmd_lock);
4083 static int bnxt_hwrm_func_reset(struct bnxt *bp)
4085 struct hwrm_func_reset_input req = {0};
4087 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4090 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4093 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4096 struct hwrm_queue_qportcfg_input req = {0};
4097 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4100 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4102 mutex_lock(&bp->hwrm_cmd_lock);
4103 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4107 if (!resp->max_configurable_queues) {
4111 bp->max_tc = resp->max_configurable_queues;
4112 if (bp->max_tc > BNXT_MAX_QUEUE)
4113 bp->max_tc = BNXT_MAX_QUEUE;
4115 qptr = &resp->queue_id0;
4116 for (i = 0; i < bp->max_tc; i++) {
4117 bp->q_info[i].queue_id = *qptr++;
4118 bp->q_info[i].queue_profile = *qptr++;
4122 mutex_unlock(&bp->hwrm_cmd_lock);
4126 static int bnxt_hwrm_ver_get(struct bnxt *bp)
4129 struct hwrm_ver_get_input req = {0};
4130 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4132 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
4133 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4134 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4135 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4136 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4137 mutex_lock(&bp->hwrm_cmd_lock);
4138 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4140 goto hwrm_ver_get_exit;
4142 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4144 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4145 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
4146 if (resp->hwrm_intf_maj < 1) {
4147 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
4148 resp->hwrm_intf_maj, resp->hwrm_intf_min,
4149 resp->hwrm_intf_upd);
4150 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
4152 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
4153 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4154 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4156 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4157 if (!bp->hwrm_cmd_timeout)
4158 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4160 if (resp->hwrm_intf_maj >= 1)
4161 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4163 bp->chip_num = le16_to_cpu(resp->chip_num);
4166 mutex_unlock(&bp->hwrm_cmd_lock);
4170 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4173 struct bnxt_pf_info *pf = &bp->pf;
4174 struct hwrm_port_qstats_input req = {0};
4176 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4179 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4180 req.port_id = cpu_to_le16(pf->port_id);
4181 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4182 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4183 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4187 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4189 if (bp->vxlan_port_cnt) {
4190 bnxt_hwrm_tunnel_dst_port_free(
4191 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4193 bp->vxlan_port_cnt = 0;
4194 if (bp->nge_port_cnt) {
4195 bnxt_hwrm_tunnel_dst_port_free(
4196 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4198 bp->nge_port_cnt = 0;
4201 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4207 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4208 for (i = 0; i < bp->nr_vnics; i++) {
4209 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4211 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4219 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4223 for (i = 0; i < bp->nr_vnics; i++)
4224 bnxt_hwrm_vnic_set_rss(bp, i, false);
4227 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4230 if (bp->vnic_info) {
4231 bnxt_hwrm_clear_vnic_filter(bp);
4232 /* clear all RSS setting before free vnic ctx */
4233 bnxt_hwrm_clear_vnic_rss(bp);
4234 bnxt_hwrm_vnic_ctx_free(bp);
4235 /* before free the vnic, undo the vnic tpa settings */
4236 if (bp->flags & BNXT_FLAG_TPA)
4237 bnxt_set_tpa(bp, false);
4238 bnxt_hwrm_vnic_free(bp);
4240 bnxt_hwrm_ring_free(bp, close_path);
4241 bnxt_hwrm_ring_grp_free(bp);
4243 bnxt_hwrm_stat_ctx_free(bp);
4244 bnxt_hwrm_free_tunnel_ports(bp);
4248 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4252 /* allocate context for vnic */
4253 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id);
4255 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4257 goto vnic_setup_err;
4259 bp->rsscos_nr_ctxs++;
4261 /* configure default vnic, ring grp */
4262 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4264 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4266 goto vnic_setup_err;
4269 /* Enable RSS hashing on vnic */
4270 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4272 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4274 goto vnic_setup_err;
4277 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4278 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4280 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4289 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4291 #ifdef CONFIG_RFS_ACCEL
4294 for (i = 0; i < bp->rx_nr_rings; i++) {
4295 u16 vnic_id = i + 1;
4298 if (vnic_id >= bp->nr_vnics)
4301 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
4302 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
4304 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4308 rc = bnxt_setup_vnic(bp, vnic_id);
4318 static int bnxt_cfg_rx_mode(struct bnxt *);
4319 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
4321 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4323 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
4327 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4329 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4335 rc = bnxt_hwrm_ring_alloc(bp);
4337 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4341 rc = bnxt_hwrm_ring_grp_alloc(bp);
4343 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4347 /* default vnic 0 */
4348 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, bp->rx_nr_rings);
4350 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4354 rc = bnxt_setup_vnic(bp, 0);
4358 if (bp->flags & BNXT_FLAG_RFS) {
4359 rc = bnxt_alloc_rfs_vnics(bp);
4364 if (bp->flags & BNXT_FLAG_TPA) {
4365 rc = bnxt_set_tpa(bp, true);
4371 bnxt_update_vf_mac(bp);
4373 /* Filter for default vnic 0 */
4374 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4376 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4379 vnic->uc_filter_count = 1;
4381 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
4383 if ((bp->dev->flags & IFF_PROMISC) && BNXT_PF(bp))
4384 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4386 if (bp->dev->flags & IFF_ALLMULTI) {
4387 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4388 vnic->mc_list_count = 0;
4392 bnxt_mc_list_updated(bp, &mask);
4393 vnic->rx_mask |= mask;
4396 rc = bnxt_cfg_rx_mode(bp);
4400 rc = bnxt_hwrm_set_coal(bp);
4402 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
4406 bnxt_hwrm_func_qcfg(bp);
4407 netdev_update_features(bp->dev);
4413 bnxt_hwrm_resource_free(bp, 0, true);
4418 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4420 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4424 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4426 bnxt_init_rx_rings(bp);
4427 bnxt_init_tx_rings(bp);
4428 bnxt_init_ring_grps(bp, irq_re_init);
4429 bnxt_init_vnics(bp);
4431 return bnxt_init_chip(bp, irq_re_init);
4434 static void bnxt_disable_int(struct bnxt *bp)
4441 for (i = 0; i < bp->cp_nr_rings; i++) {
4442 struct bnxt_napi *bnapi = bp->bnapi[i];
4443 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4445 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4449 static void bnxt_enable_int(struct bnxt *bp)
4453 atomic_set(&bp->intr_sem, 0);
4454 for (i = 0; i < bp->cp_nr_rings; i++) {
4455 struct bnxt_napi *bnapi = bp->bnapi[i];
4456 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4458 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
4462 static int bnxt_set_real_num_queues(struct bnxt *bp)
4465 struct net_device *dev = bp->dev;
4467 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4471 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4475 #ifdef CONFIG_RFS_ACCEL
4476 if (bp->flags & BNXT_FLAG_RFS)
4477 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
4483 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4486 int _rx = *rx, _tx = *tx;
4489 *rx = min_t(int, _rx, max);
4490 *tx = min_t(int, _tx, max);
4495 while (_rx + _tx > max) {
4496 if (_rx > _tx && _rx > 1)
4507 static int bnxt_setup_msix(struct bnxt *bp)
4509 struct msix_entry *msix_ent;
4510 struct net_device *dev = bp->dev;
4511 int i, total_vecs, rc = 0, min = 1;
4512 const int len = sizeof(bp->irq_tbl[0].name);
4514 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4515 total_vecs = bp->cp_nr_rings;
4517 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4521 for (i = 0; i < total_vecs; i++) {
4522 msix_ent[i].entry = i;
4523 msix_ent[i].vector = 0;
4526 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4529 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
4530 if (total_vecs < 0) {
4532 goto msix_setup_exit;
4535 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4539 /* Trim rings based upon num of vectors allocated */
4540 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
4541 total_vecs, min == 1);
4543 goto msix_setup_exit;
4545 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4546 tcs = netdev_get_num_tc(dev);
4548 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4549 if (bp->tx_nr_rings_per_tc == 0) {
4550 netdev_reset_tc(dev);
4551 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4555 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4556 for (i = 0; i < tcs; i++) {
4557 count = bp->tx_nr_rings_per_tc;
4559 netdev_set_tc_queue(dev, i, count, off);
4563 bp->cp_nr_rings = total_vecs;
4565 for (i = 0; i < bp->cp_nr_rings; i++) {
4568 bp->irq_tbl[i].vector = msix_ent[i].vector;
4569 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4571 else if (i < bp->rx_nr_rings)
4576 snprintf(bp->irq_tbl[i].name, len,
4577 "%s-%s-%d", dev->name, attr, i);
4578 bp->irq_tbl[i].handler = bnxt_msix;
4580 rc = bnxt_set_real_num_queues(bp);
4582 goto msix_setup_exit;
4585 goto msix_setup_exit;
4587 bp->flags |= BNXT_FLAG_USING_MSIX;
4592 netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
4593 pci_disable_msix(bp->pdev);
4598 static int bnxt_setup_inta(struct bnxt *bp)
4601 const int len = sizeof(bp->irq_tbl[0].name);
4603 if (netdev_get_num_tc(bp->dev))
4604 netdev_reset_tc(bp->dev);
4606 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
4611 bp->rx_nr_rings = 1;
4612 bp->tx_nr_rings = 1;
4613 bp->cp_nr_rings = 1;
4614 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4615 bp->flags |= BNXT_FLAG_SHARED_RINGS;
4616 bp->irq_tbl[0].vector = bp->pdev->irq;
4617 snprintf(bp->irq_tbl[0].name, len,
4618 "%s-%s-%d", bp->dev->name, "TxRx", 0);
4619 bp->irq_tbl[0].handler = bnxt_inta;
4620 rc = bnxt_set_real_num_queues(bp);
4624 static int bnxt_setup_int_mode(struct bnxt *bp)
4628 if (bp->flags & BNXT_FLAG_MSIX_CAP)
4629 rc = bnxt_setup_msix(bp);
4631 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
4632 /* fallback to INTA */
4633 rc = bnxt_setup_inta(bp);
4638 static void bnxt_free_irq(struct bnxt *bp)
4640 struct bnxt_irq *irq;
4643 #ifdef CONFIG_RFS_ACCEL
4644 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4645 bp->dev->rx_cpu_rmap = NULL;
4650 for (i = 0; i < bp->cp_nr_rings; i++) {
4651 irq = &bp->irq_tbl[i];
4653 free_irq(irq->vector, bp->bnapi[i]);
4656 if (bp->flags & BNXT_FLAG_USING_MSIX)
4657 pci_disable_msix(bp->pdev);
4662 static int bnxt_request_irq(struct bnxt *bp)
4665 unsigned long flags = 0;
4666 #ifdef CONFIG_RFS_ACCEL
4667 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4670 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
4671 flags = IRQF_SHARED;
4673 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4674 struct bnxt_irq *irq = &bp->irq_tbl[i];
4675 #ifdef CONFIG_RFS_ACCEL
4676 if (rmap && bp->bnapi[i]->rx_ring) {
4677 rc = irq_cpu_rmap_add(rmap, irq->vector);
4679 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
4684 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
4694 static void bnxt_del_napi(struct bnxt *bp)
4701 for (i = 0; i < bp->cp_nr_rings; i++) {
4702 struct bnxt_napi *bnapi = bp->bnapi[i];
4704 napi_hash_del(&bnapi->napi);
4705 netif_napi_del(&bnapi->napi);
4709 static void bnxt_init_napi(struct bnxt *bp)
4712 struct bnxt_napi *bnapi;
4714 if (bp->flags & BNXT_FLAG_USING_MSIX) {
4715 for (i = 0; i < bp->cp_nr_rings; i++) {
4716 bnapi = bp->bnapi[i];
4717 netif_napi_add(bp->dev, &bnapi->napi,
4721 bnapi = bp->bnapi[0];
4722 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
4726 static void bnxt_disable_napi(struct bnxt *bp)
4733 for (i = 0; i < bp->cp_nr_rings; i++) {
4734 napi_disable(&bp->bnapi[i]->napi);
4735 bnxt_disable_poll(bp->bnapi[i]);
4739 static void bnxt_enable_napi(struct bnxt *bp)
4743 for (i = 0; i < bp->cp_nr_rings; i++) {
4744 bp->bnapi[i]->in_reset = false;
4745 bnxt_enable_poll(bp->bnapi[i]);
4746 napi_enable(&bp->bnapi[i]->napi);
4750 static void bnxt_tx_disable(struct bnxt *bp)
4753 struct bnxt_tx_ring_info *txr;
4754 struct netdev_queue *txq;
4757 for (i = 0; i < bp->tx_nr_rings; i++) {
4758 txr = &bp->tx_ring[i];
4759 txq = netdev_get_tx_queue(bp->dev, i);
4760 __netif_tx_lock(txq, smp_processor_id());
4761 txr->dev_state = BNXT_DEV_STATE_CLOSING;
4762 __netif_tx_unlock(txq);
4765 /* Stop all TX queues */
4766 netif_tx_disable(bp->dev);
4767 netif_carrier_off(bp->dev);
4770 static void bnxt_tx_enable(struct bnxt *bp)
4773 struct bnxt_tx_ring_info *txr;
4774 struct netdev_queue *txq;
4776 for (i = 0; i < bp->tx_nr_rings; i++) {
4777 txr = &bp->tx_ring[i];
4778 txq = netdev_get_tx_queue(bp->dev, i);
4781 netif_tx_wake_all_queues(bp->dev);
4782 if (bp->link_info.link_up)
4783 netif_carrier_on(bp->dev);
4786 static void bnxt_report_link(struct bnxt *bp)
4788 if (bp->link_info.link_up) {
4790 const char *flow_ctrl;
4793 netif_carrier_on(bp->dev);
4794 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
4798 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
4799 flow_ctrl = "ON - receive & transmit";
4800 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
4801 flow_ctrl = "ON - transmit";
4802 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
4803 flow_ctrl = "ON - receive";
4806 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
4807 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
4808 speed, duplex, flow_ctrl);
4809 if (bp->flags & BNXT_FLAG_EEE_CAP)
4810 netdev_info(bp->dev, "EEE is %s\n",
4811 bp->eee.eee_active ? "active" :
4814 netif_carrier_off(bp->dev);
4815 netdev_err(bp->dev, "NIC Link is Down\n");
4819 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
4822 struct hwrm_port_phy_qcaps_input req = {0};
4823 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4824 struct bnxt_link_info *link_info = &bp->link_info;
4826 if (bp->hwrm_spec_code < 0x10201)
4829 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
4831 mutex_lock(&bp->hwrm_cmd_lock);
4832 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4834 goto hwrm_phy_qcaps_exit;
4836 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
4837 struct ethtool_eee *eee = &bp->eee;
4838 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
4840 bp->flags |= BNXT_FLAG_EEE_CAP;
4841 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
4842 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
4843 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
4844 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
4845 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
4847 link_info->support_auto_speeds =
4848 le16_to_cpu(resp->supported_speeds_auto_mode);
4850 hwrm_phy_qcaps_exit:
4851 mutex_unlock(&bp->hwrm_cmd_lock);
4855 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
4858 struct bnxt_link_info *link_info = &bp->link_info;
4859 struct hwrm_port_phy_qcfg_input req = {0};
4860 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4861 u8 link_up = link_info->link_up;
4863 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
4865 mutex_lock(&bp->hwrm_cmd_lock);
4866 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4868 mutex_unlock(&bp->hwrm_cmd_lock);
4872 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
4873 link_info->phy_link_status = resp->link;
4874 link_info->duplex = resp->duplex;
4875 link_info->pause = resp->pause;
4876 link_info->auto_mode = resp->auto_mode;
4877 link_info->auto_pause_setting = resp->auto_pause;
4878 link_info->lp_pause = resp->link_partner_adv_pause;
4879 link_info->force_pause_setting = resp->force_pause;
4880 link_info->duplex_setting = resp->duplex;
4881 if (link_info->phy_link_status == BNXT_LINK_LINK)
4882 link_info->link_speed = le16_to_cpu(resp->link_speed);
4884 link_info->link_speed = 0;
4885 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
4886 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
4887 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
4888 link_info->lp_auto_link_speeds =
4889 le16_to_cpu(resp->link_partner_adv_speeds);
4890 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
4891 link_info->phy_ver[0] = resp->phy_maj;
4892 link_info->phy_ver[1] = resp->phy_min;
4893 link_info->phy_ver[2] = resp->phy_bld;
4894 link_info->media_type = resp->media_type;
4895 link_info->phy_type = resp->phy_type;
4896 link_info->transceiver = resp->xcvr_pkg_type;
4897 link_info->phy_addr = resp->eee_config_phy_addr &
4898 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
4899 link_info->module_status = resp->module_status;
4901 if (bp->flags & BNXT_FLAG_EEE_CAP) {
4902 struct ethtool_eee *eee = &bp->eee;
4905 eee->eee_active = 0;
4906 if (resp->eee_config_phy_addr &
4907 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
4908 eee->eee_active = 1;
4909 fw_speeds = le16_to_cpu(
4910 resp->link_partner_adv_eee_link_speed_mask);
4911 eee->lp_advertised =
4912 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
4915 /* Pull initial EEE config */
4916 if (!chng_link_state) {
4917 if (resp->eee_config_phy_addr &
4918 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
4919 eee->eee_enabled = 1;
4921 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
4923 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
4925 if (resp->eee_config_phy_addr &
4926 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
4929 eee->tx_lpi_enabled = 1;
4930 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
4931 eee->tx_lpi_timer = le32_to_cpu(tmr) &
4932 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
4936 /* TODO: need to add more logic to report VF link */
4937 if (chng_link_state) {
4938 if (link_info->phy_link_status == BNXT_LINK_LINK)
4939 link_info->link_up = 1;
4941 link_info->link_up = 0;
4942 if (link_up != link_info->link_up)
4943 bnxt_report_link(bp);
4945 /* alwasy link down if not require to update link state */
4946 link_info->link_up = 0;
4948 mutex_unlock(&bp->hwrm_cmd_lock);
4952 static void bnxt_get_port_module_status(struct bnxt *bp)
4954 struct bnxt_link_info *link_info = &bp->link_info;
4955 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
4958 if (bnxt_update_link(bp, true))
4961 module_status = link_info->module_status;
4962 switch (module_status) {
4963 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
4964 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
4965 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
4966 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
4968 if (bp->hwrm_spec_code >= 0x10201) {
4969 netdev_warn(bp->dev, "Module part number %s\n",
4970 resp->phy_vendor_partnumber);
4972 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
4973 netdev_warn(bp->dev, "TX is disabled\n");
4974 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
4975 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
4980 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
4982 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
4983 if (bp->hwrm_spec_code >= 0x10201)
4985 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
4986 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
4987 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
4988 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
4989 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
4991 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
4993 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
4994 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
4995 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
4996 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
4998 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
4999 if (bp->hwrm_spec_code >= 0x10201) {
5000 req->auto_pause = req->force_pause;
5001 req->enables |= cpu_to_le32(
5002 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5007 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5008 struct hwrm_port_phy_cfg_input *req)
5010 u8 autoneg = bp->link_info.autoneg;
5011 u16 fw_link_speed = bp->link_info.req_link_speed;
5012 u32 advertising = bp->link_info.advertising;
5014 if (autoneg & BNXT_AUTONEG_SPEED) {
5016 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
5018 req->enables |= cpu_to_le32(
5019 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5020 req->auto_link_speed_mask = cpu_to_le16(advertising);
5022 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5024 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5026 req->force_link_speed = cpu_to_le16(fw_link_speed);
5027 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5030 /* tell chimp that the setting takes effect immediately */
5031 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5034 int bnxt_hwrm_set_pause(struct bnxt *bp)
5036 struct hwrm_port_phy_cfg_input req = {0};
5039 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5040 bnxt_hwrm_set_pause_common(bp, &req);
5042 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5043 bp->link_info.force_link_chng)
5044 bnxt_hwrm_set_link_common(bp, &req);
5046 mutex_lock(&bp->hwrm_cmd_lock);
5047 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5048 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5049 /* since changing of pause setting doesn't trigger any link
5050 * change event, the driver needs to update the current pause
5051 * result upon successfully return of the phy_cfg command
5053 bp->link_info.pause =
5054 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5055 bp->link_info.auto_pause_setting = 0;
5056 if (!bp->link_info.force_link_chng)
5057 bnxt_report_link(bp);
5059 bp->link_info.force_link_chng = false;
5060 mutex_unlock(&bp->hwrm_cmd_lock);
5064 static void bnxt_hwrm_set_eee(struct bnxt *bp,
5065 struct hwrm_port_phy_cfg_input *req)
5067 struct ethtool_eee *eee = &bp->eee;
5069 if (eee->eee_enabled) {
5071 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5073 if (eee->tx_lpi_enabled)
5074 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5076 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5078 req->flags |= cpu_to_le32(flags);
5079 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5080 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5081 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5083 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5087 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
5089 struct hwrm_port_phy_cfg_input req = {0};
5091 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5093 bnxt_hwrm_set_pause_common(bp, &req);
5095 bnxt_hwrm_set_link_common(bp, &req);
5098 bnxt_hwrm_set_eee(bp, &req);
5099 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5102 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5104 struct hwrm_port_phy_cfg_input req = {0};
5106 if (!BNXT_SINGLE_PF(bp))
5109 if (pci_num_vf(bp->pdev))
5112 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5113 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DOWN);
5114 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5117 static bool bnxt_eee_config_ok(struct bnxt *bp)
5119 struct ethtool_eee *eee = &bp->eee;
5120 struct bnxt_link_info *link_info = &bp->link_info;
5122 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5125 if (eee->eee_enabled) {
5127 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5129 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5130 eee->eee_enabled = 0;
5133 if (eee->advertised & ~advertising) {
5134 eee->advertised = advertising & eee->supported;
5141 static int bnxt_update_phy_setting(struct bnxt *bp)
5144 bool update_link = false;
5145 bool update_pause = false;
5146 bool update_eee = false;
5147 struct bnxt_link_info *link_info = &bp->link_info;
5149 rc = bnxt_update_link(bp, true);
5151 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5155 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5156 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5157 link_info->req_flow_ctrl)
5158 update_pause = true;
5159 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5160 link_info->force_pause_setting != link_info->req_flow_ctrl)
5161 update_pause = true;
5162 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5163 if (BNXT_AUTO_MODE(link_info->auto_mode))
5165 if (link_info->req_link_speed != link_info->force_link_speed)
5167 if (link_info->req_duplex != link_info->duplex_setting)
5170 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5172 if (link_info->advertising != link_info->auto_link_speeds)
5176 if (!bnxt_eee_config_ok(bp))
5180 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
5181 else if (update_pause)
5182 rc = bnxt_hwrm_set_pause(bp);
5184 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5192 /* Common routine to pre-map certain register block to different GRC window.
5193 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5194 * in PF and 3 windows in VF that can be customized to map in different
5197 static void bnxt_preset_reg_win(struct bnxt *bp)
5200 /* CAG registers map to GRC window #4 */
5201 writel(BNXT_CAG_REG_BASE,
5202 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5206 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5210 bnxt_preset_reg_win(bp);
5211 netif_carrier_off(bp->dev);
5213 rc = bnxt_setup_int_mode(bp);
5215 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5220 if ((bp->flags & BNXT_FLAG_RFS) &&
5221 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5222 /* disable RFS if falling back to INTA */
5223 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5224 bp->flags &= ~BNXT_FLAG_RFS;
5227 rc = bnxt_alloc_mem(bp, irq_re_init);
5229 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5230 goto open_err_free_mem;
5235 rc = bnxt_request_irq(bp);
5237 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5242 bnxt_enable_napi(bp);
5244 rc = bnxt_init_nic(bp, irq_re_init);
5246 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5251 rc = bnxt_update_phy_setting(bp);
5253 netdev_warn(bp->dev, "failed to update phy settings\n");
5257 udp_tunnel_get_rx_info(bp->dev);
5259 set_bit(BNXT_STATE_OPEN, &bp->state);
5260 bnxt_enable_int(bp);
5261 /* Enable TX queues */
5263 mod_timer(&bp->timer, jiffies + bp->current_interval);
5264 /* Poll link status and check for SFP+ module status */
5265 bnxt_get_port_module_status(bp);
5270 bnxt_disable_napi(bp);
5276 bnxt_free_mem(bp, true);
5280 /* rtnl_lock held */
5281 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5285 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5287 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5293 static int bnxt_open(struct net_device *dev)
5295 struct bnxt *bp = netdev_priv(dev);
5298 if (!test_bit(BNXT_STATE_FN_RST_DONE, &bp->state)) {
5299 rc = bnxt_hwrm_func_reset(bp);
5301 netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
5306 /* Do func_reset during the 1st PF open only to prevent killing
5307 * the VFs when the PF is brought down and up.
5310 set_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
5312 return __bnxt_open_nic(bp, true, true);
5315 static void bnxt_disable_int_sync(struct bnxt *bp)
5319 atomic_inc(&bp->intr_sem);
5320 if (!netif_running(bp->dev))
5323 bnxt_disable_int(bp);
5324 for (i = 0; i < bp->cp_nr_rings; i++)
5325 synchronize_irq(bp->irq_tbl[i].vector);
5328 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5332 #ifdef CONFIG_BNXT_SRIOV
5333 if (bp->sriov_cfg) {
5334 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5336 BNXT_SRIOV_CFG_WAIT_TMO);
5338 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5341 /* Change device state to avoid TX queue wake up's */
5342 bnxt_tx_disable(bp);
5344 clear_bit(BNXT_STATE_OPEN, &bp->state);
5345 smp_mb__after_atomic();
5346 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5349 /* Flush rings before disabling interrupts */
5350 bnxt_shutdown_nic(bp, irq_re_init);
5352 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5354 bnxt_disable_napi(bp);
5355 bnxt_disable_int_sync(bp);
5356 del_timer_sync(&bp->timer);
5363 bnxt_free_mem(bp, irq_re_init);
5367 static int bnxt_close(struct net_device *dev)
5369 struct bnxt *bp = netdev_priv(dev);
5371 bnxt_close_nic(bp, true, true);
5372 bnxt_hwrm_shutdown_link(bp);
5376 /* rtnl_lock held */
5377 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5383 if (!netif_running(dev))
5390 if (!netif_running(dev))
5402 static struct rtnl_link_stats64 *
5403 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5406 struct bnxt *bp = netdev_priv(dev);
5408 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5413 /* TODO check if we need to synchronize with bnxt_close path */
5414 for (i = 0; i < bp->cp_nr_rings; i++) {
5415 struct bnxt_napi *bnapi = bp->bnapi[i];
5416 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5417 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5419 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5420 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5421 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5423 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5424 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5425 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5427 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5428 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5429 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5431 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5432 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5433 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5435 stats->rx_missed_errors +=
5436 le64_to_cpu(hw_stats->rx_discard_pkts);
5438 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5440 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5443 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5444 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5445 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5447 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5448 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5449 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5450 le64_to_cpu(rx->rx_ovrsz_frames) +
5451 le64_to_cpu(rx->rx_runt_frames);
5452 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5453 le64_to_cpu(rx->rx_jbr_frames);
5454 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5455 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5456 stats->tx_errors = le64_to_cpu(tx->tx_err);
5462 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5464 struct net_device *dev = bp->dev;
5465 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5466 struct netdev_hw_addr *ha;
5469 bool update = false;
5472 netdev_for_each_mc_addr(ha, dev) {
5473 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5474 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5475 vnic->mc_list_count = 0;
5479 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5480 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5487 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5489 if (mc_count != vnic->mc_list_count) {
5490 vnic->mc_list_count = mc_count;
5496 static bool bnxt_uc_list_updated(struct bnxt *bp)
5498 struct net_device *dev = bp->dev;
5499 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5500 struct netdev_hw_addr *ha;
5503 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5506 netdev_for_each_uc_addr(ha, dev) {
5507 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5515 static void bnxt_set_rx_mode(struct net_device *dev)
5517 struct bnxt *bp = netdev_priv(dev);
5518 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5519 u32 mask = vnic->rx_mask;
5520 bool mc_update = false;
5523 if (!netif_running(dev))
5526 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5527 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5528 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5530 /* Only allow PF to be in promiscuous mode */
5531 if ((dev->flags & IFF_PROMISC) && BNXT_PF(bp))
5532 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5534 uc_update = bnxt_uc_list_updated(bp);
5536 if (dev->flags & IFF_ALLMULTI) {
5537 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5538 vnic->mc_list_count = 0;
5540 mc_update = bnxt_mc_list_updated(bp, &mask);
5543 if (mask != vnic->rx_mask || uc_update || mc_update) {
5544 vnic->rx_mask = mask;
5546 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5547 schedule_work(&bp->sp_task);
5551 static int bnxt_cfg_rx_mode(struct bnxt *bp)
5553 struct net_device *dev = bp->dev;
5554 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5555 struct netdev_hw_addr *ha;
5559 netif_addr_lock_bh(dev);
5560 uc_update = bnxt_uc_list_updated(bp);
5561 netif_addr_unlock_bh(dev);
5566 mutex_lock(&bp->hwrm_cmd_lock);
5567 for (i = 1; i < vnic->uc_filter_count; i++) {
5568 struct hwrm_cfa_l2_filter_free_input req = {0};
5570 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5573 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5575 rc = _hwrm_send_message(bp, &req, sizeof(req),
5578 mutex_unlock(&bp->hwrm_cmd_lock);
5580 vnic->uc_filter_count = 1;
5582 netif_addr_lock_bh(dev);
5583 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5584 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5586 netdev_for_each_uc_addr(ha, dev) {
5587 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5589 vnic->uc_filter_count++;
5592 netif_addr_unlock_bh(dev);
5594 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5595 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5597 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5599 vnic->uc_filter_count = i;
5605 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5607 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5613 static bool bnxt_rfs_capable(struct bnxt *bp)
5615 #ifdef CONFIG_RFS_ACCEL
5616 struct bnxt_pf_info *pf = &bp->pf;
5619 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5622 vnics = 1 + bp->rx_nr_rings;
5623 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics)
5632 static netdev_features_t bnxt_fix_features(struct net_device *dev,
5633 netdev_features_t features)
5635 struct bnxt *bp = netdev_priv(dev);
5637 if (!bnxt_rfs_capable(bp))
5638 features &= ~NETIF_F_NTUPLE;
5640 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5641 * turned on or off together.
5643 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
5644 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
5645 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
5646 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5647 NETIF_F_HW_VLAN_STAG_RX);
5649 features |= NETIF_F_HW_VLAN_CTAG_RX |
5650 NETIF_F_HW_VLAN_STAG_RX;
5652 #ifdef CONFIG_BNXT_SRIOV
5655 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5656 NETIF_F_HW_VLAN_STAG_RX);
5663 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
5665 struct bnxt *bp = netdev_priv(dev);
5666 u32 flags = bp->flags;
5669 bool re_init = false;
5670 bool update_tpa = false;
5672 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
5673 if ((features & NETIF_F_GRO) && (bp->pdev->revision > 0))
5674 flags |= BNXT_FLAG_GRO;
5675 if (features & NETIF_F_LRO)
5676 flags |= BNXT_FLAG_LRO;
5678 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5679 flags |= BNXT_FLAG_STRIP_VLAN;
5681 if (features & NETIF_F_NTUPLE)
5682 flags |= BNXT_FLAG_RFS;
5684 changes = flags ^ bp->flags;
5685 if (changes & BNXT_FLAG_TPA) {
5687 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
5688 (flags & BNXT_FLAG_TPA) == 0)
5692 if (changes & ~BNXT_FLAG_TPA)
5695 if (flags != bp->flags) {
5696 u32 old_flags = bp->flags;
5700 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
5702 bnxt_set_ring_params(bp);
5707 bnxt_close_nic(bp, false, false);
5709 bnxt_set_ring_params(bp);
5711 return bnxt_open_nic(bp, false, false);
5714 rc = bnxt_set_tpa(bp,
5715 (flags & BNXT_FLAG_TPA) ?
5718 bp->flags = old_flags;
5724 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
5726 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
5727 int i = bnapi->index;
5732 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
5733 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
5737 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
5739 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
5740 int i = bnapi->index;
5745 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",
5746 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
5747 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
5748 rxr->rx_sw_agg_prod);
5751 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
5753 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5754 int i = bnapi->index;
5756 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
5757 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
5760 static void bnxt_dbg_dump_states(struct bnxt *bp)
5763 struct bnxt_napi *bnapi;
5765 for (i = 0; i < bp->cp_nr_rings; i++) {
5766 bnapi = bp->bnapi[i];
5767 if (netif_msg_drv(bp)) {
5768 bnxt_dump_tx_sw_state(bnapi);
5769 bnxt_dump_rx_sw_state(bnapi);
5770 bnxt_dump_cp_sw_state(bnapi);
5775 static void bnxt_reset_task(struct bnxt *bp, bool silent)
5778 bnxt_dbg_dump_states(bp);
5779 if (netif_running(bp->dev)) {
5780 bnxt_close_nic(bp, false, false);
5781 bnxt_open_nic(bp, false, false);
5785 static void bnxt_tx_timeout(struct net_device *dev)
5787 struct bnxt *bp = netdev_priv(dev);
5789 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
5790 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
5791 schedule_work(&bp->sp_task);
5794 #ifdef CONFIG_NET_POLL_CONTROLLER
5795 static void bnxt_poll_controller(struct net_device *dev)
5797 struct bnxt *bp = netdev_priv(dev);
5800 for (i = 0; i < bp->cp_nr_rings; i++) {
5801 struct bnxt_irq *irq = &bp->irq_tbl[i];
5803 disable_irq(irq->vector);
5804 irq->handler(irq->vector, bp->bnapi[i]);
5805 enable_irq(irq->vector);
5810 static void bnxt_timer(unsigned long data)
5812 struct bnxt *bp = (struct bnxt *)data;
5813 struct net_device *dev = bp->dev;
5815 if (!netif_running(dev))
5818 if (atomic_read(&bp->intr_sem) != 0)
5819 goto bnxt_restart_timer;
5821 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
5822 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
5823 schedule_work(&bp->sp_task);
5826 mod_timer(&bp->timer, jiffies + bp->current_interval);
5829 /* Only called from bnxt_sp_task() */
5830 static void bnxt_reset(struct bnxt *bp, bool silent)
5832 /* bnxt_reset_task() calls bnxt_close_nic() which waits
5833 * for BNXT_STATE_IN_SP_TASK to clear.
5834 * If there is a parallel dev_close(), bnxt_close() may be holding
5835 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
5836 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
5838 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5840 if (test_bit(BNXT_STATE_OPEN, &bp->state))
5841 bnxt_reset_task(bp, silent);
5842 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5846 static void bnxt_cfg_ntp_filters(struct bnxt *);
5848 static void bnxt_sp_task(struct work_struct *work)
5850 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
5853 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5854 smp_mb__after_atomic();
5855 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
5856 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5860 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
5861 bnxt_cfg_rx_mode(bp);
5863 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
5864 bnxt_cfg_ntp_filters(bp);
5865 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
5866 rc = bnxt_update_link(bp, true);
5868 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
5871 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
5872 bnxt_hwrm_exec_fwd_req(bp);
5873 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
5874 bnxt_hwrm_tunnel_dst_port_alloc(
5876 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5878 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
5879 bnxt_hwrm_tunnel_dst_port_free(
5880 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
5882 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
5883 bnxt_hwrm_tunnel_dst_port_alloc(
5885 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
5887 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
5888 bnxt_hwrm_tunnel_dst_port_free(
5889 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
5891 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
5892 bnxt_reset(bp, false);
5894 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
5895 bnxt_reset(bp, true);
5897 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
5898 bnxt_get_port_module_status(bp);
5900 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
5901 bnxt_hwrm_port_qstats(bp);
5903 smp_mb__before_atomic();
5904 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
5907 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
5910 struct bnxt *bp = netdev_priv(dev);
5912 SET_NETDEV_DEV(dev, &pdev->dev);
5914 /* enable device (incl. PCI PM wakeup), and bus-mastering */
5915 rc = pci_enable_device(pdev);
5917 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
5921 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
5923 "Cannot find PCI device base address, aborting\n");
5925 goto init_err_disable;
5928 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
5930 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
5931 goto init_err_disable;
5934 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
5935 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
5936 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
5937 goto init_err_disable;
5940 pci_set_master(pdev);
5945 bp->bar0 = pci_ioremap_bar(pdev, 0);
5947 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
5949 goto init_err_release;
5952 bp->bar1 = pci_ioremap_bar(pdev, 2);
5954 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
5956 goto init_err_release;
5959 bp->bar2 = pci_ioremap_bar(pdev, 4);
5961 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
5963 goto init_err_release;
5966 pci_enable_pcie_error_reporting(pdev);
5968 INIT_WORK(&bp->sp_task, bnxt_sp_task);
5970 spin_lock_init(&bp->ntp_fltr_lock);
5972 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
5973 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
5975 /* tick values in micro seconds */
5976 bp->rx_coal_ticks = 12;
5977 bp->rx_coal_bufs = 30;
5978 bp->rx_coal_ticks_irq = 1;
5979 bp->rx_coal_bufs_irq = 2;
5981 bp->tx_coal_ticks = 25;
5982 bp->tx_coal_bufs = 30;
5983 bp->tx_coal_ticks_irq = 2;
5984 bp->tx_coal_bufs_irq = 2;
5986 init_timer(&bp->timer);
5987 bp->timer.data = (unsigned long)bp;
5988 bp->timer.function = bnxt_timer;
5989 bp->current_interval = BNXT_TIMER_INTERVAL;
5991 clear_bit(BNXT_STATE_OPEN, &bp->state);
5997 pci_iounmap(pdev, bp->bar2);
6002 pci_iounmap(pdev, bp->bar1);
6007 pci_iounmap(pdev, bp->bar0);
6011 pci_release_regions(pdev);
6014 pci_disable_device(pdev);
6020 /* rtnl_lock held */
6021 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6023 struct sockaddr *addr = p;
6024 struct bnxt *bp = netdev_priv(dev);
6027 if (!is_valid_ether_addr(addr->sa_data))
6028 return -EADDRNOTAVAIL;
6030 rc = bnxt_approve_mac(bp, addr->sa_data);
6034 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6037 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6038 if (netif_running(dev)) {
6039 bnxt_close_nic(bp, false, false);
6040 rc = bnxt_open_nic(bp, false, false);
6046 /* rtnl_lock held */
6047 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6049 struct bnxt *bp = netdev_priv(dev);
6051 if (new_mtu < 60 || new_mtu > 9000)
6054 if (netif_running(dev))
6055 bnxt_close_nic(bp, false, false);
6058 bnxt_set_ring_params(bp);
6060 if (netif_running(dev))
6061 return bnxt_open_nic(bp, false, false);
6066 static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6067 struct tc_to_netdev *ntc)
6069 struct bnxt *bp = netdev_priv(dev);
6072 if (ntc->type != TC_SETUP_MQPRIO)
6077 if (tc > bp->max_tc) {
6078 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6083 if (netdev_get_num_tc(dev) == tc)
6087 int max_rx_rings, max_tx_rings, rc;
6090 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6093 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
6094 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
6098 /* Needs to close the device and do hw resource re-allocations */
6099 if (netif_running(bp->dev))
6100 bnxt_close_nic(bp, true, false);
6103 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6104 netdev_set_num_tc(dev, tc);
6106 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6107 netdev_reset_tc(dev);
6109 bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
6110 bp->num_stat_ctxs = bp->cp_nr_rings;
6112 if (netif_running(bp->dev))
6113 return bnxt_open_nic(bp, true, false);
6118 #ifdef CONFIG_RFS_ACCEL
6119 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6120 struct bnxt_ntuple_filter *f2)
6122 struct flow_keys *keys1 = &f1->fkeys;
6123 struct flow_keys *keys2 = &f2->fkeys;
6125 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6126 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6127 keys1->ports.ports == keys2->ports.ports &&
6128 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6129 keys1->basic.n_proto == keys2->basic.n_proto &&
6130 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr))
6136 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6137 u16 rxq_index, u32 flow_id)
6139 struct bnxt *bp = netdev_priv(dev);
6140 struct bnxt_ntuple_filter *fltr, *new_fltr;
6141 struct flow_keys *fkeys;
6142 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
6143 int rc = 0, idx, bit_id;
6144 struct hlist_head *head;
6146 if (skb->encapsulation)
6147 return -EPROTONOSUPPORT;
6149 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6153 fkeys = &new_fltr->fkeys;
6154 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6155 rc = -EPROTONOSUPPORT;
6159 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6160 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6161 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6162 rc = -EPROTONOSUPPORT;
6166 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6168 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6169 head = &bp->ntp_fltr_hash_tbl[idx];
6171 hlist_for_each_entry_rcu(fltr, head, hash) {
6172 if (bnxt_fltr_match(fltr, new_fltr)) {
6180 spin_lock_bh(&bp->ntp_fltr_lock);
6181 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6182 BNXT_NTP_FLTR_MAX_FLTR, 0);
6184 spin_unlock_bh(&bp->ntp_fltr_lock);
6189 new_fltr->sw_id = (u16)bit_id;
6190 new_fltr->flow_id = flow_id;
6191 new_fltr->rxq = rxq_index;
6192 hlist_add_head_rcu(&new_fltr->hash, head);
6193 bp->ntp_fltr_count++;
6194 spin_unlock_bh(&bp->ntp_fltr_lock);
6196 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6197 schedule_work(&bp->sp_task);
6199 return new_fltr->sw_id;
6206 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6210 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6211 struct hlist_head *head;
6212 struct hlist_node *tmp;
6213 struct bnxt_ntuple_filter *fltr;
6216 head = &bp->ntp_fltr_hash_tbl[i];
6217 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6220 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6221 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6224 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6229 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6234 set_bit(BNXT_FLTR_VALID, &fltr->state);
6238 spin_lock_bh(&bp->ntp_fltr_lock);
6239 hlist_del_rcu(&fltr->hash);
6240 bp->ntp_fltr_count--;
6241 spin_unlock_bh(&bp->ntp_fltr_lock);
6243 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6248 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6249 netdev_info(bp->dev, "Receive PF driver unload event!");
6254 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6258 #endif /* CONFIG_RFS_ACCEL */
6260 static void bnxt_udp_tunnel_add(struct net_device *dev,
6261 struct udp_tunnel_info *ti)
6263 struct bnxt *bp = netdev_priv(dev);
6265 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6268 if (!netif_running(dev))
6272 case UDP_TUNNEL_TYPE_VXLAN:
6273 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6276 bp->vxlan_port_cnt++;
6277 if (bp->vxlan_port_cnt == 1) {
6278 bp->vxlan_port = ti->port;
6279 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
6280 schedule_work(&bp->sp_task);
6283 case UDP_TUNNEL_TYPE_GENEVE:
6284 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6288 if (bp->nge_port_cnt == 1) {
6289 bp->nge_port = ti->port;
6290 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6297 schedule_work(&bp->sp_task);
6300 static void bnxt_udp_tunnel_del(struct net_device *dev,
6301 struct udp_tunnel_info *ti)
6303 struct bnxt *bp = netdev_priv(dev);
6305 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6308 if (!netif_running(dev))
6312 case UDP_TUNNEL_TYPE_VXLAN:
6313 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6315 bp->vxlan_port_cnt--;
6317 if (bp->vxlan_port_cnt != 0)
6320 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6322 case UDP_TUNNEL_TYPE_GENEVE:
6323 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6327 if (bp->nge_port_cnt != 0)
6330 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6336 schedule_work(&bp->sp_task);
6339 static const struct net_device_ops bnxt_netdev_ops = {
6340 .ndo_open = bnxt_open,
6341 .ndo_start_xmit = bnxt_start_xmit,
6342 .ndo_stop = bnxt_close,
6343 .ndo_get_stats64 = bnxt_get_stats64,
6344 .ndo_set_rx_mode = bnxt_set_rx_mode,
6345 .ndo_do_ioctl = bnxt_ioctl,
6346 .ndo_validate_addr = eth_validate_addr,
6347 .ndo_set_mac_address = bnxt_change_mac_addr,
6348 .ndo_change_mtu = bnxt_change_mtu,
6349 .ndo_fix_features = bnxt_fix_features,
6350 .ndo_set_features = bnxt_set_features,
6351 .ndo_tx_timeout = bnxt_tx_timeout,
6352 #ifdef CONFIG_BNXT_SRIOV
6353 .ndo_get_vf_config = bnxt_get_vf_config,
6354 .ndo_set_vf_mac = bnxt_set_vf_mac,
6355 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6356 .ndo_set_vf_rate = bnxt_set_vf_bw,
6357 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6358 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6360 #ifdef CONFIG_NET_POLL_CONTROLLER
6361 .ndo_poll_controller = bnxt_poll_controller,
6363 .ndo_setup_tc = bnxt_setup_tc,
6364 #ifdef CONFIG_RFS_ACCEL
6365 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6367 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6368 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
6369 #ifdef CONFIG_NET_RX_BUSY_POLL
6370 .ndo_busy_poll = bnxt_busy_poll,
6374 static void bnxt_remove_one(struct pci_dev *pdev)
6376 struct net_device *dev = pci_get_drvdata(pdev);
6377 struct bnxt *bp = netdev_priv(dev);
6380 bnxt_sriov_disable(bp);
6382 pci_disable_pcie_error_reporting(pdev);
6383 unregister_netdev(dev);
6384 cancel_work_sync(&bp->sp_task);
6387 bnxt_hwrm_func_drv_unrgtr(bp);
6388 bnxt_free_hwrm_resources(bp);
6389 pci_iounmap(pdev, bp->bar2);
6390 pci_iounmap(pdev, bp->bar1);
6391 pci_iounmap(pdev, bp->bar0);
6394 pci_release_regions(pdev);
6395 pci_disable_device(pdev);
6398 static int bnxt_probe_phy(struct bnxt *bp)
6401 struct bnxt_link_info *link_info = &bp->link_info;
6403 rc = bnxt_hwrm_phy_qcaps(bp);
6405 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6410 rc = bnxt_update_link(bp, false);
6412 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6417 /* Older firmware does not have supported_auto_speeds, so assume
6418 * that all supported speeds can be autonegotiated.
6420 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6421 link_info->support_auto_speeds = link_info->support_speeds;
6423 /*initialize the ethool setting copy with NVM settings */
6424 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
6425 link_info->autoneg = BNXT_AUTONEG_SPEED;
6426 if (bp->hwrm_spec_code >= 0x10201) {
6427 if (link_info->auto_pause_setting &
6428 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6429 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6431 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6433 link_info->advertising = link_info->auto_link_speeds;
6435 link_info->req_link_speed = link_info->force_link_speed;
6436 link_info->req_duplex = link_info->duplex_setting;
6438 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6439 link_info->req_flow_ctrl =
6440 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6442 link_info->req_flow_ctrl = link_info->force_pause_setting;
6446 static int bnxt_get_max_irq(struct pci_dev *pdev)
6450 if (!pdev->msix_cap)
6453 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6454 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6457 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6460 int max_ring_grps = 0;
6462 #ifdef CONFIG_BNXT_SRIOV
6464 *max_tx = bp->vf.max_tx_rings;
6465 *max_rx = bp->vf.max_rx_rings;
6466 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6467 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
6468 max_ring_grps = bp->vf.max_hw_ring_grps;
6472 *max_tx = bp->pf.max_tx_rings;
6473 *max_rx = bp->pf.max_rx_rings;
6474 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6475 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6476 max_ring_grps = bp->pf.max_hw_ring_grps;
6479 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6481 *max_rx = min_t(int, *max_rx, max_ring_grps);
6484 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6488 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6489 if (!rx || !tx || !cp)
6494 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6497 static int bnxt_set_dflt_rings(struct bnxt *bp)
6499 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6503 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6504 dflt_rings = netif_get_num_default_rss_queues();
6505 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
6508 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6509 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6510 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6511 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6512 bp->tx_nr_rings + bp->rx_nr_rings;
6513 bp->num_stat_ctxs = bp->cp_nr_rings;
6517 static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6519 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6520 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6522 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6523 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6524 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6526 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6527 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6528 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6529 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6533 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6535 static int version_printed;
6536 struct net_device *dev;
6540 if (version_printed++ == 0)
6541 pr_info("%s", version);
6543 max_irqs = bnxt_get_max_irq(pdev);
6544 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6548 bp = netdev_priv(dev);
6550 if (bnxt_vf_pciid(ent->driver_data))
6551 bp->flags |= BNXT_FLAG_VF;
6554 bp->flags |= BNXT_FLAG_MSIX_CAP;
6556 rc = bnxt_init_board(pdev, dev);
6560 dev->netdev_ops = &bnxt_netdev_ops;
6561 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6562 dev->ethtool_ops = &bnxt_ethtool_ops;
6564 pci_set_drvdata(pdev, dev);
6566 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6567 NETIF_F_TSO | NETIF_F_TSO6 |
6568 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
6569 NETIF_F_GSO_IPXIP4 |
6570 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6571 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
6572 NETIF_F_RXCSUM | NETIF_F_LRO | NETIF_F_GRO;
6574 dev->hw_enc_features =
6575 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6576 NETIF_F_TSO | NETIF_F_TSO6 |
6577 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
6578 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6579 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
6580 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
6581 NETIF_F_GSO_GRE_CSUM;
6582 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
6583 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
6584 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
6585 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
6586 dev->priv_flags |= IFF_UNICAST_FLT;
6588 #ifdef CONFIG_BNXT_SRIOV
6589 init_waitqueue_head(&bp->sriov_cfg_wait);
6591 rc = bnxt_alloc_hwrm_resources(bp);
6595 mutex_init(&bp->hwrm_cmd_lock);
6596 rc = bnxt_hwrm_ver_get(bp);
6600 bp->gro_func = bnxt_gro_func_5730x;
6601 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
6602 bp->gro_func = bnxt_gro_func_5731x;
6604 rc = bnxt_hwrm_func_drv_rgtr(bp);
6608 /* Get the MAX capabilities for this function */
6609 rc = bnxt_hwrm_func_qcaps(bp);
6611 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
6617 rc = bnxt_hwrm_queue_qportcfg(bp);
6619 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
6625 bnxt_hwrm_func_qcfg(bp);
6627 bnxt_set_tpa_flags(bp);
6628 bnxt_set_ring_params(bp);
6630 bp->pf.max_irqs = max_irqs;
6631 #if defined(CONFIG_BNXT_SRIOV)
6633 bp->vf.max_irqs = max_irqs;
6635 bnxt_set_dflt_rings(bp);
6638 dev->hw_features |= NETIF_F_NTUPLE;
6639 if (bnxt_rfs_capable(bp)) {
6640 bp->flags |= BNXT_FLAG_RFS;
6641 dev->features |= NETIF_F_NTUPLE;
6645 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
6646 bp->flags |= BNXT_FLAG_STRIP_VLAN;
6648 rc = bnxt_probe_phy(bp);
6652 rc = register_netdev(dev);
6656 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
6657 board_info[ent->driver_data].name,
6658 (long)pci_resource_start(pdev, 0), dev->dev_addr);
6660 bnxt_parse_log_pcie_link(bp);
6665 pci_iounmap(pdev, bp->bar0);
6666 pci_release_regions(pdev);
6667 pci_disable_device(pdev);
6675 * bnxt_io_error_detected - called when PCI error is detected
6676 * @pdev: Pointer to PCI device
6677 * @state: The current pci connection state
6679 * This function is called after a PCI bus error affecting
6680 * this device has been detected.
6682 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
6683 pci_channel_state_t state)
6685 struct net_device *netdev = pci_get_drvdata(pdev);
6686 struct bnxt *bp = netdev_priv(netdev);
6688 netdev_info(netdev, "PCI I/O error detected\n");
6691 netif_device_detach(netdev);
6693 if (state == pci_channel_io_perm_failure) {
6695 return PCI_ERS_RESULT_DISCONNECT;
6698 if (netif_running(netdev))
6701 /* So that func_reset will be done during slot_reset */
6702 clear_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
6703 pci_disable_device(pdev);
6706 /* Request a slot slot reset. */
6707 return PCI_ERS_RESULT_NEED_RESET;
6711 * bnxt_io_slot_reset - called after the pci bus has been reset.
6712 * @pdev: Pointer to PCI device
6714 * Restart the card from scratch, as if from a cold-boot.
6715 * At this point, the card has exprienced a hard reset,
6716 * followed by fixups by BIOS, and has its config space
6717 * set up identically to what it was at cold boot.
6719 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
6721 struct net_device *netdev = pci_get_drvdata(pdev);
6722 struct bnxt *bp = netdev_priv(netdev);
6724 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
6726 netdev_info(bp->dev, "PCI Slot Reset\n");
6730 if (pci_enable_device(pdev)) {
6732 "Cannot re-enable PCI device after reset.\n");
6734 pci_set_master(pdev);
6736 if (netif_running(netdev))
6737 err = bnxt_open(netdev);
6740 result = PCI_ERS_RESULT_RECOVERED;
6743 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
6748 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6751 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
6752 err); /* non-fatal, continue */
6755 return PCI_ERS_RESULT_RECOVERED;
6759 * bnxt_io_resume - called when traffic can start flowing again.
6760 * @pdev: Pointer to PCI device
6762 * This callback is called when the error recovery driver tells
6763 * us that its OK to resume normal operation.
6765 static void bnxt_io_resume(struct pci_dev *pdev)
6767 struct net_device *netdev = pci_get_drvdata(pdev);
6771 netif_device_attach(netdev);
6776 static const struct pci_error_handlers bnxt_err_handler = {
6777 .error_detected = bnxt_io_error_detected,
6778 .slot_reset = bnxt_io_slot_reset,
6779 .resume = bnxt_io_resume
6782 static struct pci_driver bnxt_pci_driver = {
6783 .name = DRV_MODULE_NAME,
6784 .id_table = bnxt_pci_tbl,
6785 .probe = bnxt_init_one,
6786 .remove = bnxt_remove_one,
6787 .err_handler = &bnxt_err_handler,
6788 #if defined(CONFIG_BNXT_SRIOV)
6789 .sriov_configure = bnxt_sriov_configure,
6793 module_pci_driver(bnxt_pci_driver);