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
104 /* indexed by enum above */
105 static const struct {
108 { "Broadcom BCM57301 NetXtreme-C Single-port 10Gb Ethernet" },
109 { "Broadcom BCM57302 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
110 { "Broadcom BCM57304 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
111 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
112 { "Broadcom BCM58700 Nitro 4-port 1Gb/2.5Gb/10Gb Ethernet" },
113 { "Broadcom BCM57311 NetXtreme-C Single-port 10Gb Ethernet" },
114 { "Broadcom BCM57312 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
115 { "Broadcom BCM57402 NetXtreme-E Dual-port 10Gb Ethernet" },
116 { "Broadcom BCM57404 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
117 { "Broadcom BCM57406 NetXtreme-E Dual-port 10GBase-T Ethernet" },
118 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
119 { "Broadcom BCM57407 NetXtreme-E Dual-port 10GBase-T Ethernet" },
120 { "Broadcom BCM57412 NetXtreme-E Dual-port 10Gb Ethernet" },
121 { "Broadcom BCM57414 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
122 { "Broadcom BCM57416 NetXtreme-E Dual-port 10GBase-T Ethernet" },
123 { "Broadcom BCM57417 NetXtreme-E Dual-port 10GBase-T Ethernet" },
124 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
125 { "Broadcom BCM57314 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
126 { "Broadcom BCM57417 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
127 { "Broadcom BCM57416 NetXtreme-E Dual-port 10Gb Ethernet" },
128 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
129 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
130 { "Broadcom BCM57407 NetXtreme-E Dual-port 25Gb Ethernet" },
131 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
132 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
133 { "Broadcom BCM57304 NetXtreme-C Ethernet Virtual Function" },
134 { "Broadcom BCM57404 NetXtreme-E Ethernet Virtual Function" },
135 { "Broadcom BCM57414 NetXtreme-E Ethernet Virtual Function" },
136 { "Broadcom BCM57314 NetXtreme-E Ethernet Virtual Function" },
139 static const struct pci_device_id bnxt_pci_tbl[] = {
140 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
141 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
142 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
143 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
144 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
145 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
146 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
147 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
148 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
149 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
150 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
151 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
152 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
153 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
154 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
155 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
156 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
157 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
158 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
159 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
160 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
161 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
162 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
163 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
164 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
165 #ifdef CONFIG_BNXT_SRIOV
166 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = BCM57304_VF },
167 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = BCM57404_VF },
168 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = BCM57414_VF },
169 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = BCM57314_VF },
174 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
176 static const u16 bnxt_vf_req_snif[] = {
179 HWRM_CFA_L2_FILTER_ALLOC,
182 static const u16 bnxt_async_events_arr[] = {
183 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
184 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
185 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
186 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
187 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
190 static bool bnxt_vf_pciid(enum board_idx idx)
192 return (idx == BCM57304_VF || idx == BCM57404_VF ||
193 idx == BCM57314_VF || idx == BCM57414_VF);
196 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
197 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
198 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
200 #define BNXT_CP_DB_REARM(db, raw_cons) \
201 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
203 #define BNXT_CP_DB(db, raw_cons) \
204 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
206 #define BNXT_CP_DB_IRQ_DIS(db) \
207 writel(DB_CP_IRQ_DIS_FLAGS, db)
209 static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
211 /* Tell compiler to fetch tx indices from memory. */
214 return bp->tx_ring_size -
215 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
218 static const u16 bnxt_lhint_arr[] = {
219 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
220 TX_BD_FLAGS_LHINT_512_TO_1023,
221 TX_BD_FLAGS_LHINT_1024_TO_2047,
222 TX_BD_FLAGS_LHINT_1024_TO_2047,
223 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
224 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
225 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
226 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
227 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
228 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
229 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
230 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
231 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
232 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
233 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
234 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
235 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
236 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
237 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
242 struct bnxt *bp = netdev_priv(dev);
244 struct tx_bd_ext *txbd1;
245 struct netdev_queue *txq;
248 unsigned int length, pad = 0;
249 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
251 struct pci_dev *pdev = bp->pdev;
252 struct bnxt_tx_ring_info *txr;
253 struct bnxt_sw_tx_bd *tx_buf;
255 i = skb_get_queue_mapping(skb);
256 if (unlikely(i >= bp->tx_nr_rings)) {
257 dev_kfree_skb_any(skb);
261 txr = &bp->tx_ring[i];
262 txq = netdev_get_tx_queue(dev, i);
265 free_size = bnxt_tx_avail(bp, txr);
266 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
267 netif_tx_stop_queue(txq);
268 return NETDEV_TX_BUSY;
272 len = skb_headlen(skb);
273 last_frag = skb_shinfo(skb)->nr_frags;
275 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
277 txbd->tx_bd_opaque = prod;
279 tx_buf = &txr->tx_buf_ring[prod];
281 tx_buf->nr_frags = last_frag;
285 if (skb_vlan_tag_present(skb)) {
286 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
287 skb_vlan_tag_get(skb);
288 /* Currently supports 8021Q, 8021AD vlan offloads
289 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
291 if (skb->vlan_proto == htons(ETH_P_8021Q))
292 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
295 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
296 struct tx_push_buffer *tx_push_buf = txr->tx_push;
297 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
298 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
299 void *pdata = tx_push_buf->data;
303 /* Set COAL_NOW to be ready quickly for the next push */
304 tx_push->tx_bd_len_flags_type =
305 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
306 TX_BD_TYPE_LONG_TX_BD |
307 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
308 TX_BD_FLAGS_COAL_NOW |
309 TX_BD_FLAGS_PACKET_END |
310 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
312 if (skb->ip_summed == CHECKSUM_PARTIAL)
313 tx_push1->tx_bd_hsize_lflags =
314 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
316 tx_push1->tx_bd_hsize_lflags = 0;
318 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
319 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
321 end = pdata + length;
322 end = PTR_ALIGN(end, 8) - 1;
325 skb_copy_from_linear_data(skb, pdata, len);
327 for (j = 0; j < last_frag; j++) {
328 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
331 fptr = skb_frag_address_safe(frag);
335 memcpy(pdata, fptr, skb_frag_size(frag));
336 pdata += skb_frag_size(frag);
339 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
340 txbd->tx_bd_haddr = txr->data_mapping;
341 prod = NEXT_TX(prod);
342 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
343 memcpy(txbd, tx_push1, sizeof(*txbd));
344 prod = NEXT_TX(prod);
346 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
350 netdev_tx_sent_queue(txq, skb->len);
351 wmb(); /* Sync is_push and byte queue before pushing data */
353 push_len = (length + sizeof(*tx_push) + 7) / 8;
355 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
356 __iowrite64_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
359 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
367 if (length < BNXT_MIN_PKT_SIZE) {
368 pad = BNXT_MIN_PKT_SIZE - length;
369 if (skb_pad(skb, pad)) {
370 /* SKB already freed. */
374 length = BNXT_MIN_PKT_SIZE;
377 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
379 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
380 dev_kfree_skb_any(skb);
385 dma_unmap_addr_set(tx_buf, mapping, mapping);
386 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
387 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
389 txbd->tx_bd_haddr = cpu_to_le64(mapping);
391 prod = NEXT_TX(prod);
392 txbd1 = (struct tx_bd_ext *)
393 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
395 txbd1->tx_bd_hsize_lflags = 0;
396 if (skb_is_gso(skb)) {
399 if (skb->encapsulation)
400 hdr_len = skb_inner_network_offset(skb) +
401 skb_inner_network_header_len(skb) +
402 inner_tcp_hdrlen(skb);
404 hdr_len = skb_transport_offset(skb) +
407 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
409 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
410 length = skb_shinfo(skb)->gso_size;
411 txbd1->tx_bd_mss = cpu_to_le32(length);
413 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
414 txbd1->tx_bd_hsize_lflags =
415 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
416 txbd1->tx_bd_mss = 0;
420 flags |= bnxt_lhint_arr[length];
421 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
423 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
424 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
425 for (i = 0; i < last_frag; i++) {
426 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
428 prod = NEXT_TX(prod);
429 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
431 len = skb_frag_size(frag);
432 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
435 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
438 tx_buf = &txr->tx_buf_ring[prod];
439 dma_unmap_addr_set(tx_buf, mapping, mapping);
441 txbd->tx_bd_haddr = cpu_to_le64(mapping);
443 flags = len << TX_BD_LEN_SHIFT;
444 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
448 txbd->tx_bd_len_flags_type =
449 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
450 TX_BD_FLAGS_PACKET_END);
452 netdev_tx_sent_queue(txq, skb->len);
454 /* Sync BD data before updating doorbell */
457 prod = NEXT_TX(prod);
460 writel(DB_KEY_TX | prod, txr->tx_doorbell);
461 writel(DB_KEY_TX | prod, txr->tx_doorbell);
467 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
468 netif_tx_stop_queue(txq);
470 /* netif_tx_stop_queue() must be done before checking
471 * tx index in bnxt_tx_avail() below, because in
472 * bnxt_tx_int(), we update tx index before checking for
473 * netif_tx_queue_stopped().
476 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
477 netif_tx_wake_queue(txq);
484 /* start back at beginning and unmap skb */
486 tx_buf = &txr->tx_buf_ring[prod];
488 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
489 skb_headlen(skb), PCI_DMA_TODEVICE);
490 prod = NEXT_TX(prod);
492 /* unmap remaining mapped pages */
493 for (i = 0; i < last_frag; i++) {
494 prod = NEXT_TX(prod);
495 tx_buf = &txr->tx_buf_ring[prod];
496 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
497 skb_frag_size(&skb_shinfo(skb)->frags[i]),
501 dev_kfree_skb_any(skb);
505 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
507 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
508 int index = txr - &bp->tx_ring[0];
509 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
510 u16 cons = txr->tx_cons;
511 struct pci_dev *pdev = bp->pdev;
513 unsigned int tx_bytes = 0;
515 for (i = 0; i < nr_pkts; i++) {
516 struct bnxt_sw_tx_bd *tx_buf;
520 tx_buf = &txr->tx_buf_ring[cons];
521 cons = NEXT_TX(cons);
525 if (tx_buf->is_push) {
530 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
531 skb_headlen(skb), PCI_DMA_TODEVICE);
532 last = tx_buf->nr_frags;
534 for (j = 0; j < last; j++) {
535 cons = NEXT_TX(cons);
536 tx_buf = &txr->tx_buf_ring[cons];
539 dma_unmap_addr(tx_buf, mapping),
540 skb_frag_size(&skb_shinfo(skb)->frags[j]),
545 cons = NEXT_TX(cons);
547 tx_bytes += skb->len;
548 dev_kfree_skb_any(skb);
551 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
554 /* Need to make the tx_cons update visible to bnxt_start_xmit()
555 * before checking for netif_tx_queue_stopped(). Without the
556 * memory barrier, there is a small possibility that bnxt_start_xmit()
557 * will miss it and cause the queue to be stopped forever.
561 if (unlikely(netif_tx_queue_stopped(txq)) &&
562 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
563 __netif_tx_lock(txq, smp_processor_id());
564 if (netif_tx_queue_stopped(txq) &&
565 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
566 txr->dev_state != BNXT_DEV_STATE_CLOSING)
567 netif_tx_wake_queue(txq);
568 __netif_tx_unlock(txq);
572 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
576 struct pci_dev *pdev = bp->pdev;
578 data = kmalloc(bp->rx_buf_size, gfp);
582 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
583 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
585 if (dma_mapping_error(&pdev->dev, *mapping)) {
592 static inline int bnxt_alloc_rx_data(struct bnxt *bp,
593 struct bnxt_rx_ring_info *rxr,
596 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
597 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
601 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
606 dma_unmap_addr_set(rx_buf, mapping, mapping);
608 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
613 static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
616 u16 prod = rxr->rx_prod;
617 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
618 struct rx_bd *cons_bd, *prod_bd;
620 prod_rx_buf = &rxr->rx_buf_ring[prod];
621 cons_rx_buf = &rxr->rx_buf_ring[cons];
623 prod_rx_buf->data = data;
625 dma_unmap_addr_set(prod_rx_buf, mapping,
626 dma_unmap_addr(cons_rx_buf, mapping));
628 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
629 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
631 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
634 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
636 u16 next, max = rxr->rx_agg_bmap_size;
638 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
640 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
644 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
645 struct bnxt_rx_ring_info *rxr,
649 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
650 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
651 struct pci_dev *pdev = bp->pdev;
654 u16 sw_prod = rxr->rx_sw_agg_prod;
655 unsigned int offset = 0;
657 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
660 page = alloc_page(gfp);
664 rxr->rx_page_offset = 0;
666 offset = rxr->rx_page_offset;
667 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
668 if (rxr->rx_page_offset == PAGE_SIZE)
673 page = alloc_page(gfp);
678 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
680 if (dma_mapping_error(&pdev->dev, mapping)) {
685 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
686 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
688 __set_bit(sw_prod, rxr->rx_agg_bmap);
689 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
690 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
692 rx_agg_buf->page = page;
693 rx_agg_buf->offset = offset;
694 rx_agg_buf->mapping = mapping;
695 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
696 rxbd->rx_bd_opaque = sw_prod;
700 static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
703 struct bnxt *bp = bnapi->bp;
704 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
705 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
706 u16 prod = rxr->rx_agg_prod;
707 u16 sw_prod = rxr->rx_sw_agg_prod;
710 for (i = 0; i < agg_bufs; i++) {
712 struct rx_agg_cmp *agg;
713 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
714 struct rx_bd *prod_bd;
717 agg = (struct rx_agg_cmp *)
718 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
719 cons = agg->rx_agg_cmp_opaque;
720 __clear_bit(cons, rxr->rx_agg_bmap);
722 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
723 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
725 __set_bit(sw_prod, rxr->rx_agg_bmap);
726 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
727 cons_rx_buf = &rxr->rx_agg_ring[cons];
729 /* It is possible for sw_prod to be equal to cons, so
730 * set cons_rx_buf->page to NULL first.
732 page = cons_rx_buf->page;
733 cons_rx_buf->page = NULL;
734 prod_rx_buf->page = page;
735 prod_rx_buf->offset = cons_rx_buf->offset;
737 prod_rx_buf->mapping = cons_rx_buf->mapping;
739 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
741 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
742 prod_bd->rx_bd_opaque = sw_prod;
744 prod = NEXT_RX_AGG(prod);
745 sw_prod = NEXT_RX_AGG(sw_prod);
746 cp_cons = NEXT_CMP(cp_cons);
748 rxr->rx_agg_prod = prod;
749 rxr->rx_sw_agg_prod = sw_prod;
752 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
753 struct bnxt_rx_ring_info *rxr, u16 cons,
754 u16 prod, u8 *data, dma_addr_t dma_addr,
760 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
762 bnxt_reuse_rx_data(rxr, cons, data);
766 skb = build_skb(data, 0);
767 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
774 skb_reserve(skb, BNXT_RX_OFFSET);
779 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
780 struct sk_buff *skb, u16 cp_cons,
783 struct pci_dev *pdev = bp->pdev;
784 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
785 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
786 u16 prod = rxr->rx_agg_prod;
789 for (i = 0; i < agg_bufs; i++) {
791 struct rx_agg_cmp *agg;
792 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
796 agg = (struct rx_agg_cmp *)
797 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
798 cons = agg->rx_agg_cmp_opaque;
799 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
800 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
802 cons_rx_buf = &rxr->rx_agg_ring[cons];
803 skb_fill_page_desc(skb, i, cons_rx_buf->page,
804 cons_rx_buf->offset, frag_len);
805 __clear_bit(cons, rxr->rx_agg_bmap);
807 /* It is possible for bnxt_alloc_rx_page() to allocate
808 * a sw_prod index that equals the cons index, so we
809 * need to clear the cons entry now.
811 mapping = dma_unmap_addr(cons_rx_buf, mapping);
812 page = cons_rx_buf->page;
813 cons_rx_buf->page = NULL;
815 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
816 struct skb_shared_info *shinfo;
817 unsigned int nr_frags;
819 shinfo = skb_shinfo(skb);
820 nr_frags = --shinfo->nr_frags;
821 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
825 cons_rx_buf->page = page;
827 /* Update prod since possibly some pages have been
830 rxr->rx_agg_prod = prod;
831 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
835 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
838 skb->data_len += frag_len;
839 skb->len += frag_len;
840 skb->truesize += PAGE_SIZE;
842 prod = NEXT_RX_AGG(prod);
843 cp_cons = NEXT_CMP(cp_cons);
845 rxr->rx_agg_prod = prod;
849 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
850 u8 agg_bufs, u32 *raw_cons)
853 struct rx_agg_cmp *agg;
855 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
856 last = RING_CMP(*raw_cons);
857 agg = (struct rx_agg_cmp *)
858 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
859 return RX_AGG_CMP_VALID(agg, *raw_cons);
862 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
866 struct bnxt *bp = bnapi->bp;
867 struct pci_dev *pdev = bp->pdev;
870 skb = napi_alloc_skb(&bnapi->napi, len);
874 dma_sync_single_for_cpu(&pdev->dev, mapping,
875 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
877 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
879 dma_sync_single_for_device(&pdev->dev, mapping,
887 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
888 u32 *raw_cons, void *cmp)
890 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
891 struct rx_cmp *rxcmp = cmp;
892 u32 tmp_raw_cons = *raw_cons;
893 u8 cmp_type, agg_bufs = 0;
895 cmp_type = RX_CMP_TYPE(rxcmp);
897 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
898 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
900 RX_CMP_AGG_BUFS_SHIFT;
901 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
902 struct rx_tpa_end_cmp *tpa_end = cmp;
904 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
905 RX_TPA_END_CMP_AGG_BUFS) >>
906 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
910 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
913 *raw_cons = tmp_raw_cons;
917 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
919 if (!rxr->bnapi->in_reset) {
920 rxr->bnapi->in_reset = true;
921 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
922 schedule_work(&bp->sp_task);
924 rxr->rx_next_cons = 0xffff;
927 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
928 struct rx_tpa_start_cmp *tpa_start,
929 struct rx_tpa_start_cmp_ext *tpa_start1)
931 u8 agg_id = TPA_START_AGG_ID(tpa_start);
933 struct bnxt_tpa_info *tpa_info;
934 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
935 struct rx_bd *prod_bd;
938 cons = tpa_start->rx_tpa_start_cmp_opaque;
940 cons_rx_buf = &rxr->rx_buf_ring[cons];
941 prod_rx_buf = &rxr->rx_buf_ring[prod];
942 tpa_info = &rxr->rx_tpa[agg_id];
944 if (unlikely(cons != rxr->rx_next_cons)) {
945 bnxt_sched_reset(bp, rxr);
949 prod_rx_buf->data = tpa_info->data;
951 mapping = tpa_info->mapping;
952 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
954 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
956 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
958 tpa_info->data = cons_rx_buf->data;
959 cons_rx_buf->data = NULL;
960 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
963 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
964 RX_TPA_START_CMP_LEN_SHIFT;
965 if (likely(TPA_START_HASH_VALID(tpa_start))) {
966 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
968 tpa_info->hash_type = PKT_HASH_TYPE_L4;
969 tpa_info->gso_type = SKB_GSO_TCPV4;
970 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
972 tpa_info->gso_type = SKB_GSO_TCPV6;
974 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
976 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
977 tpa_info->gso_type = 0;
978 if (netif_msg_rx_err(bp))
979 netdev_warn(bp->dev, "TPA packet without valid hash\n");
981 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
982 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
983 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
985 rxr->rx_prod = NEXT_RX(prod);
986 cons = NEXT_RX(cons);
987 rxr->rx_next_cons = NEXT_RX(cons);
988 cons_rx_buf = &rxr->rx_buf_ring[cons];
990 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
991 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
992 cons_rx_buf->data = NULL;
995 static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
996 u16 cp_cons, u32 agg_bufs)
999 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1002 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1003 int payload_off, int tcp_ts,
1004 struct sk_buff *skb)
1009 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1010 u32 hdr_info = tpa_info->hdr_info;
1011 bool loopback = false;
1013 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1014 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1015 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1017 /* If the packet is an internal loopback packet, the offsets will
1018 * have an extra 4 bytes.
1020 if (inner_mac_off == 4) {
1022 } else if (inner_mac_off > 4) {
1023 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1026 /* We only support inner iPv4/ipv6. If we don't see the
1027 * correct protocol ID, it must be a loopback packet where
1028 * the offsets are off by 4.
1030 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1034 /* internal loopback packet, subtract all offsets by 4 */
1040 nw_off = inner_ip_off - ETH_HLEN;
1041 skb_set_network_header(skb, nw_off);
1042 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1043 struct ipv6hdr *iph = ipv6_hdr(skb);
1045 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1046 len = skb->len - skb_transport_offset(skb);
1048 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1050 struct iphdr *iph = ip_hdr(skb);
1052 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1053 len = skb->len - skb_transport_offset(skb);
1055 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1058 if (inner_mac_off) { /* tunnel */
1059 struct udphdr *uh = NULL;
1060 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1063 if (proto == htons(ETH_P_IP)) {
1064 struct iphdr *iph = (struct iphdr *)skb->data;
1066 if (iph->protocol == IPPROTO_UDP)
1067 uh = (struct udphdr *)(iph + 1);
1069 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1071 if (iph->nexthdr == IPPROTO_UDP)
1072 uh = (struct udphdr *)(iph + 1);
1076 skb_shinfo(skb)->gso_type |=
1077 SKB_GSO_UDP_TUNNEL_CSUM;
1079 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1086 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1087 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1089 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1090 int payload_off, int tcp_ts,
1091 struct sk_buff *skb)
1095 int len, nw_off, tcp_opt_len;
1100 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1103 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1105 skb_set_network_header(skb, nw_off);
1107 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1108 len = skb->len - skb_transport_offset(skb);
1110 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1111 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1112 struct ipv6hdr *iph;
1114 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1116 skb_set_network_header(skb, nw_off);
1117 iph = ipv6_hdr(skb);
1118 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1119 len = skb->len - skb_transport_offset(skb);
1121 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1123 dev_kfree_skb_any(skb);
1126 tcp_gro_complete(skb);
1128 if (nw_off) { /* tunnel */
1129 struct udphdr *uh = NULL;
1131 if (skb->protocol == htons(ETH_P_IP)) {
1132 struct iphdr *iph = (struct iphdr *)skb->data;
1134 if (iph->protocol == IPPROTO_UDP)
1135 uh = (struct udphdr *)(iph + 1);
1137 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1139 if (iph->nexthdr == IPPROTO_UDP)
1140 uh = (struct udphdr *)(iph + 1);
1144 skb_shinfo(skb)->gso_type |=
1145 SKB_GSO_UDP_TUNNEL_CSUM;
1147 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1154 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1155 struct bnxt_tpa_info *tpa_info,
1156 struct rx_tpa_end_cmp *tpa_end,
1157 struct rx_tpa_end_cmp_ext *tpa_end1,
1158 struct sk_buff *skb)
1164 segs = TPA_END_TPA_SEGS(tpa_end);
1168 NAPI_GRO_CB(skb)->count = segs;
1169 skb_shinfo(skb)->gso_size =
1170 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1171 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1172 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1173 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1174 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1175 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1180 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1181 struct bnxt_napi *bnapi,
1183 struct rx_tpa_end_cmp *tpa_end,
1184 struct rx_tpa_end_cmp_ext *tpa_end1,
1187 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1188 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1189 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1191 u16 cp_cons = RING_CMP(*raw_cons);
1193 struct bnxt_tpa_info *tpa_info;
1195 struct sk_buff *skb;
1197 if (unlikely(bnapi->in_reset)) {
1198 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1201 return ERR_PTR(-EBUSY);
1205 tpa_info = &rxr->rx_tpa[agg_id];
1206 data = tpa_info->data;
1208 len = tpa_info->len;
1209 mapping = tpa_info->mapping;
1211 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1212 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1215 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1216 return ERR_PTR(-EBUSY);
1219 cp_cons = NEXT_CMP(cp_cons);
1222 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1223 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1224 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1225 agg_bufs, (int)MAX_SKB_FRAGS);
1229 if (len <= bp->rx_copy_thresh) {
1230 skb = bnxt_copy_skb(bnapi, data, len, mapping);
1232 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1237 dma_addr_t new_mapping;
1239 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1241 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1245 tpa_info->data = new_data;
1246 tpa_info->mapping = new_mapping;
1248 skb = build_skb(data, 0);
1249 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1250 PCI_DMA_FROMDEVICE);
1254 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1257 skb_reserve(skb, BNXT_RX_OFFSET);
1262 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1264 /* Page reuse already handled by bnxt_rx_pages(). */
1268 skb->protocol = eth_type_trans(skb, bp->dev);
1270 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1271 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1273 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1274 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1275 u16 vlan_proto = tpa_info->metadata >>
1276 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1277 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
1279 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1282 skb_checksum_none_assert(skb);
1283 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1284 skb->ip_summed = CHECKSUM_UNNECESSARY;
1286 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1289 if (TPA_END_GRO(tpa_end))
1290 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1295 /* returns the following:
1296 * 1 - 1 packet successfully received
1297 * 0 - successful TPA_START, packet not completed yet
1298 * -EBUSY - completion ring does not have all the agg buffers yet
1299 * -ENOMEM - packet aborted due to out of memory
1300 * -EIO - packet aborted due to hw error indicated in BD
1302 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1305 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1306 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1307 struct net_device *dev = bp->dev;
1308 struct rx_cmp *rxcmp;
1309 struct rx_cmp_ext *rxcmp1;
1310 u32 tmp_raw_cons = *raw_cons;
1311 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1312 struct bnxt_sw_rx_bd *rx_buf;
1314 u8 *data, agg_bufs, cmp_type;
1315 dma_addr_t dma_addr;
1316 struct sk_buff *skb;
1319 rxcmp = (struct rx_cmp *)
1320 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1322 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1323 cp_cons = RING_CMP(tmp_raw_cons);
1324 rxcmp1 = (struct rx_cmp_ext *)
1325 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1327 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1330 cmp_type = RX_CMP_TYPE(rxcmp);
1332 prod = rxr->rx_prod;
1334 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1335 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1336 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1338 goto next_rx_no_prod;
1340 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1341 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1342 (struct rx_tpa_end_cmp *)rxcmp,
1343 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1346 if (unlikely(IS_ERR(skb)))
1351 skb_record_rx_queue(skb, bnapi->index);
1352 skb_mark_napi_id(skb, &bnapi->napi);
1353 if (bnxt_busy_polling(bnapi))
1354 netif_receive_skb(skb);
1356 napi_gro_receive(&bnapi->napi, skb);
1359 goto next_rx_no_prod;
1362 cons = rxcmp->rx_cmp_opaque;
1363 rx_buf = &rxr->rx_buf_ring[cons];
1364 data = rx_buf->data;
1365 if (unlikely(cons != rxr->rx_next_cons)) {
1366 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1368 bnxt_sched_reset(bp, rxr);
1373 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1374 RX_CMP_AGG_BUFS_SHIFT;
1377 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1380 cp_cons = NEXT_CMP(cp_cons);
1384 rx_buf->data = NULL;
1385 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1386 bnxt_reuse_rx_data(rxr, cons, data);
1388 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1394 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1395 dma_addr = dma_unmap_addr(rx_buf, mapping);
1397 if (len <= bp->rx_copy_thresh) {
1398 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1399 bnxt_reuse_rx_data(rxr, cons, data);
1405 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1413 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1420 if (RX_CMP_HASH_VALID(rxcmp)) {
1421 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1422 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1424 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1425 if (hash_type != 1 && hash_type != 3)
1426 type = PKT_HASH_TYPE_L3;
1427 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1430 skb->protocol = eth_type_trans(skb, dev);
1432 if ((rxcmp1->rx_cmp_flags2 &
1433 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1434 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1435 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1436 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
1437 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1439 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1442 skb_checksum_none_assert(skb);
1443 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1444 if (dev->features & NETIF_F_RXCSUM) {
1445 skb->ip_summed = CHECKSUM_UNNECESSARY;
1446 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1449 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1450 if (dev->features & NETIF_F_RXCSUM)
1451 cpr->rx_l4_csum_errors++;
1455 skb_record_rx_queue(skb, bnapi->index);
1456 skb_mark_napi_id(skb, &bnapi->napi);
1457 if (bnxt_busy_polling(bnapi))
1458 netif_receive_skb(skb);
1460 napi_gro_receive(&bnapi->napi, skb);
1464 rxr->rx_prod = NEXT_RX(prod);
1465 rxr->rx_next_cons = NEXT_RX(cons);
1468 *raw_cons = tmp_raw_cons;
1473 #define BNXT_GET_EVENT_PORT(data) \
1475 HWRM_ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1477 static int bnxt_async_event_process(struct bnxt *bp,
1478 struct hwrm_async_event_cmpl *cmpl)
1480 u16 event_id = le16_to_cpu(cmpl->event_id);
1482 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1484 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1485 u32 data1 = le32_to_cpu(cmpl->event_data1);
1486 struct bnxt_link_info *link_info = &bp->link_info;
1489 goto async_event_process_exit;
1490 if (data1 & 0x20000) {
1491 u16 fw_speed = link_info->force_link_speed;
1492 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1494 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1499 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1500 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1502 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1503 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1505 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1506 u32 data1 = le32_to_cpu(cmpl->event_data1);
1507 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1512 if (bp->pf.port_id != port_id)
1515 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1518 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1520 goto async_event_process_exit;
1521 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1524 netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
1526 goto async_event_process_exit;
1528 schedule_work(&bp->sp_task);
1529 async_event_process_exit:
1533 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1535 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1536 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1537 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1538 (struct hwrm_fwd_req_cmpl *)txcmp;
1540 switch (cmpl_type) {
1541 case CMPL_BASE_TYPE_HWRM_DONE:
1542 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1543 if (seq_id == bp->hwrm_intr_seq_id)
1544 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1546 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1549 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1550 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1552 if ((vf_id < bp->pf.first_vf_id) ||
1553 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1554 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1559 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1560 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1561 schedule_work(&bp->sp_task);
1564 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1565 bnxt_async_event_process(bp,
1566 (struct hwrm_async_event_cmpl *)txcmp);
1575 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1577 struct bnxt_napi *bnapi = dev_instance;
1578 struct bnxt *bp = bnapi->bp;
1579 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1580 u32 cons = RING_CMP(cpr->cp_raw_cons);
1582 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1583 napi_schedule(&bnapi->napi);
1587 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1589 u32 raw_cons = cpr->cp_raw_cons;
1590 u16 cons = RING_CMP(raw_cons);
1591 struct tx_cmp *txcmp;
1593 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1595 return TX_CMP_VALID(txcmp, raw_cons);
1598 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1600 struct bnxt_napi *bnapi = dev_instance;
1601 struct bnxt *bp = bnapi->bp;
1602 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1603 u32 cons = RING_CMP(cpr->cp_raw_cons);
1606 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1608 if (!bnxt_has_work(bp, cpr)) {
1609 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
1610 /* return if erroneous interrupt */
1611 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1615 /* disable ring IRQ */
1616 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1618 /* Return here if interrupt is shared and is disabled. */
1619 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1622 napi_schedule(&bnapi->napi);
1626 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1628 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1629 u32 raw_cons = cpr->cp_raw_cons;
1633 bool rx_event = false;
1634 bool agg_event = false;
1635 struct tx_cmp *txcmp;
1640 cons = RING_CMP(raw_cons);
1641 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1643 if (!TX_CMP_VALID(txcmp, raw_cons))
1646 /* The valid test of the entry must be done first before
1647 * reading any further.
1650 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1652 /* return full budget so NAPI will complete. */
1653 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1655 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1656 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1657 if (likely(rc >= 0))
1659 else if (rc == -EBUSY) /* partial completion */
1662 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1663 CMPL_BASE_TYPE_HWRM_DONE) ||
1664 (TX_CMP_TYPE(txcmp) ==
1665 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1666 (TX_CMP_TYPE(txcmp) ==
1667 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1668 bnxt_hwrm_handler(bp, txcmp);
1670 raw_cons = NEXT_RAW_CMP(raw_cons);
1672 if (rx_pkts == budget)
1676 cpr->cp_raw_cons = raw_cons;
1677 /* ACK completion ring before freeing tx ring and producing new
1678 * buffers in rx/agg rings to prevent overflowing the completion
1681 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1684 bnxt_tx_int(bp, bnapi, tx_pkts);
1687 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1689 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1690 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1692 writel(DB_KEY_RX | rxr->rx_agg_prod,
1693 rxr->rx_agg_doorbell);
1694 writel(DB_KEY_RX | rxr->rx_agg_prod,
1695 rxr->rx_agg_doorbell);
1701 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1703 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1704 struct bnxt *bp = bnapi->bp;
1705 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1706 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1707 struct tx_cmp *txcmp;
1708 struct rx_cmp_ext *rxcmp1;
1709 u32 cp_cons, tmp_raw_cons;
1710 u32 raw_cons = cpr->cp_raw_cons;
1712 bool agg_event = false;
1717 cp_cons = RING_CMP(raw_cons);
1718 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1720 if (!TX_CMP_VALID(txcmp, raw_cons))
1723 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1724 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1725 cp_cons = RING_CMP(tmp_raw_cons);
1726 rxcmp1 = (struct rx_cmp_ext *)
1727 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1729 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1732 /* force an error to recycle the buffer */
1733 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1734 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1736 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1737 if (likely(rc == -EIO))
1739 else if (rc == -EBUSY) /* partial completion */
1741 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1742 CMPL_BASE_TYPE_HWRM_DONE)) {
1743 bnxt_hwrm_handler(bp, txcmp);
1746 "Invalid completion received on special ring\n");
1748 raw_cons = NEXT_RAW_CMP(raw_cons);
1750 if (rx_pkts == budget)
1754 cpr->cp_raw_cons = raw_cons;
1755 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1756 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1757 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1760 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1761 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1764 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1765 napi_complete(napi);
1766 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1771 static int bnxt_poll(struct napi_struct *napi, int budget)
1773 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1774 struct bnxt *bp = bnapi->bp;
1775 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1778 if (!bnxt_lock_napi(bnapi))
1782 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1784 if (work_done >= budget)
1787 if (!bnxt_has_work(bp, cpr)) {
1788 napi_complete(napi);
1789 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1794 bnxt_unlock_napi(bnapi);
1798 #ifdef CONFIG_NET_RX_BUSY_POLL
1799 static int bnxt_busy_poll(struct napi_struct *napi)
1801 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1802 struct bnxt *bp = bnapi->bp;
1803 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1804 int rx_work, budget = 4;
1806 if (atomic_read(&bp->intr_sem) != 0)
1807 return LL_FLUSH_FAILED;
1809 if (!bnxt_lock_poll(bnapi))
1810 return LL_FLUSH_BUSY;
1812 rx_work = bnxt_poll_work(bp, bnapi, budget);
1814 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1816 bnxt_unlock_poll(bnapi);
1821 static void bnxt_free_tx_skbs(struct bnxt *bp)
1824 struct pci_dev *pdev = bp->pdev;
1829 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1830 for (i = 0; i < bp->tx_nr_rings; i++) {
1831 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
1834 for (j = 0; j < max_idx;) {
1835 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1836 struct sk_buff *skb = tx_buf->skb;
1846 if (tx_buf->is_push) {
1852 dma_unmap_single(&pdev->dev,
1853 dma_unmap_addr(tx_buf, mapping),
1857 last = tx_buf->nr_frags;
1859 for (k = 0; k < last; k++, j++) {
1860 int ring_idx = j & bp->tx_ring_mask;
1861 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1863 tx_buf = &txr->tx_buf_ring[ring_idx];
1866 dma_unmap_addr(tx_buf, mapping),
1867 skb_frag_size(frag), PCI_DMA_TODEVICE);
1871 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1875 static void bnxt_free_rx_skbs(struct bnxt *bp)
1877 int i, max_idx, max_agg_idx;
1878 struct pci_dev *pdev = bp->pdev;
1883 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1884 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1885 for (i = 0; i < bp->rx_nr_rings; i++) {
1886 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
1890 for (j = 0; j < MAX_TPA; j++) {
1891 struct bnxt_tpa_info *tpa_info =
1893 u8 *data = tpa_info->data;
1900 dma_unmap_addr(tpa_info, mapping),
1901 bp->rx_buf_use_size,
1902 PCI_DMA_FROMDEVICE);
1904 tpa_info->data = NULL;
1910 for (j = 0; j < max_idx; j++) {
1911 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1912 u8 *data = rx_buf->data;
1917 dma_unmap_single(&pdev->dev,
1918 dma_unmap_addr(rx_buf, mapping),
1919 bp->rx_buf_use_size,
1920 PCI_DMA_FROMDEVICE);
1922 rx_buf->data = NULL;
1927 for (j = 0; j < max_agg_idx; j++) {
1928 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1929 &rxr->rx_agg_ring[j];
1930 struct page *page = rx_agg_buf->page;
1935 dma_unmap_page(&pdev->dev,
1936 dma_unmap_addr(rx_agg_buf, mapping),
1937 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
1939 rx_agg_buf->page = NULL;
1940 __clear_bit(j, rxr->rx_agg_bmap);
1945 __free_page(rxr->rx_page);
1946 rxr->rx_page = NULL;
1951 static void bnxt_free_skbs(struct bnxt *bp)
1953 bnxt_free_tx_skbs(bp);
1954 bnxt_free_rx_skbs(bp);
1957 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1959 struct pci_dev *pdev = bp->pdev;
1962 for (i = 0; i < ring->nr_pages; i++) {
1963 if (!ring->pg_arr[i])
1966 dma_free_coherent(&pdev->dev, ring->page_size,
1967 ring->pg_arr[i], ring->dma_arr[i]);
1969 ring->pg_arr[i] = NULL;
1972 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1973 ring->pg_tbl, ring->pg_tbl_map);
1974 ring->pg_tbl = NULL;
1976 if (ring->vmem_size && *ring->vmem) {
1982 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1985 struct pci_dev *pdev = bp->pdev;
1987 if (ring->nr_pages > 1) {
1988 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1996 for (i = 0; i < ring->nr_pages; i++) {
1997 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2001 if (!ring->pg_arr[i])
2004 if (ring->nr_pages > 1)
2005 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2008 if (ring->vmem_size) {
2009 *ring->vmem = vzalloc(ring->vmem_size);
2016 static void bnxt_free_rx_rings(struct bnxt *bp)
2023 for (i = 0; i < bp->rx_nr_rings; i++) {
2024 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2025 struct bnxt_ring_struct *ring;
2030 kfree(rxr->rx_agg_bmap);
2031 rxr->rx_agg_bmap = NULL;
2033 ring = &rxr->rx_ring_struct;
2034 bnxt_free_ring(bp, ring);
2036 ring = &rxr->rx_agg_ring_struct;
2037 bnxt_free_ring(bp, ring);
2041 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2043 int i, rc, agg_rings = 0, tpa_rings = 0;
2048 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2051 if (bp->flags & BNXT_FLAG_TPA)
2054 for (i = 0; i < bp->rx_nr_rings; i++) {
2055 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2056 struct bnxt_ring_struct *ring;
2058 ring = &rxr->rx_ring_struct;
2060 rc = bnxt_alloc_ring(bp, ring);
2067 ring = &rxr->rx_agg_ring_struct;
2068 rc = bnxt_alloc_ring(bp, ring);
2072 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2073 mem_size = rxr->rx_agg_bmap_size / 8;
2074 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2075 if (!rxr->rx_agg_bmap)
2079 rxr->rx_tpa = kcalloc(MAX_TPA,
2080 sizeof(struct bnxt_tpa_info),
2090 static void bnxt_free_tx_rings(struct bnxt *bp)
2093 struct pci_dev *pdev = bp->pdev;
2098 for (i = 0; i < bp->tx_nr_rings; i++) {
2099 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2100 struct bnxt_ring_struct *ring;
2103 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2104 txr->tx_push, txr->tx_push_mapping);
2105 txr->tx_push = NULL;
2108 ring = &txr->tx_ring_struct;
2110 bnxt_free_ring(bp, ring);
2114 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2117 struct pci_dev *pdev = bp->pdev;
2119 bp->tx_push_size = 0;
2120 if (bp->tx_push_thresh) {
2123 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2124 bp->tx_push_thresh);
2126 if (push_size > 256) {
2128 bp->tx_push_thresh = 0;
2131 bp->tx_push_size = push_size;
2134 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2135 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2136 struct bnxt_ring_struct *ring;
2138 ring = &txr->tx_ring_struct;
2140 rc = bnxt_alloc_ring(bp, ring);
2144 if (bp->tx_push_size) {
2147 /* One pre-allocated DMA buffer to backup
2150 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2152 &txr->tx_push_mapping,
2158 mapping = txr->tx_push_mapping +
2159 sizeof(struct tx_push_bd);
2160 txr->data_mapping = cpu_to_le64(mapping);
2162 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
2164 ring->queue_id = bp->q_info[j].queue_id;
2165 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2171 static void bnxt_free_cp_rings(struct bnxt *bp)
2178 for (i = 0; i < bp->cp_nr_rings; i++) {
2179 struct bnxt_napi *bnapi = bp->bnapi[i];
2180 struct bnxt_cp_ring_info *cpr;
2181 struct bnxt_ring_struct *ring;
2186 cpr = &bnapi->cp_ring;
2187 ring = &cpr->cp_ring_struct;
2189 bnxt_free_ring(bp, ring);
2193 static int bnxt_alloc_cp_rings(struct bnxt *bp)
2197 for (i = 0; i < bp->cp_nr_rings; i++) {
2198 struct bnxt_napi *bnapi = bp->bnapi[i];
2199 struct bnxt_cp_ring_info *cpr;
2200 struct bnxt_ring_struct *ring;
2205 cpr = &bnapi->cp_ring;
2206 ring = &cpr->cp_ring_struct;
2208 rc = bnxt_alloc_ring(bp, ring);
2215 static void bnxt_init_ring_struct(struct bnxt *bp)
2219 for (i = 0; i < bp->cp_nr_rings; i++) {
2220 struct bnxt_napi *bnapi = bp->bnapi[i];
2221 struct bnxt_cp_ring_info *cpr;
2222 struct bnxt_rx_ring_info *rxr;
2223 struct bnxt_tx_ring_info *txr;
2224 struct bnxt_ring_struct *ring;
2229 cpr = &bnapi->cp_ring;
2230 ring = &cpr->cp_ring_struct;
2231 ring->nr_pages = bp->cp_nr_pages;
2232 ring->page_size = HW_CMPD_RING_SIZE;
2233 ring->pg_arr = (void **)cpr->cp_desc_ring;
2234 ring->dma_arr = cpr->cp_desc_mapping;
2235 ring->vmem_size = 0;
2237 rxr = bnapi->rx_ring;
2241 ring = &rxr->rx_ring_struct;
2242 ring->nr_pages = bp->rx_nr_pages;
2243 ring->page_size = HW_RXBD_RING_SIZE;
2244 ring->pg_arr = (void **)rxr->rx_desc_ring;
2245 ring->dma_arr = rxr->rx_desc_mapping;
2246 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2247 ring->vmem = (void **)&rxr->rx_buf_ring;
2249 ring = &rxr->rx_agg_ring_struct;
2250 ring->nr_pages = bp->rx_agg_nr_pages;
2251 ring->page_size = HW_RXBD_RING_SIZE;
2252 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2253 ring->dma_arr = rxr->rx_agg_desc_mapping;
2254 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2255 ring->vmem = (void **)&rxr->rx_agg_ring;
2258 txr = bnapi->tx_ring;
2262 ring = &txr->tx_ring_struct;
2263 ring->nr_pages = bp->tx_nr_pages;
2264 ring->page_size = HW_RXBD_RING_SIZE;
2265 ring->pg_arr = (void **)txr->tx_desc_ring;
2266 ring->dma_arr = txr->tx_desc_mapping;
2267 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2268 ring->vmem = (void **)&txr->tx_buf_ring;
2272 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2276 struct rx_bd **rx_buf_ring;
2278 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2279 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2283 rxbd = rx_buf_ring[i];
2287 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2288 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2289 rxbd->rx_bd_opaque = prod;
2294 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2296 struct net_device *dev = bp->dev;
2297 struct bnxt_rx_ring_info *rxr;
2298 struct bnxt_ring_struct *ring;
2302 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2303 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2305 if (NET_IP_ALIGN == 2)
2306 type |= RX_BD_FLAGS_SOP;
2308 rxr = &bp->rx_ring[ring_nr];
2309 ring = &rxr->rx_ring_struct;
2310 bnxt_init_rxbd_pages(ring, type);
2312 prod = rxr->rx_prod;
2313 for (i = 0; i < bp->rx_ring_size; i++) {
2314 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2315 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2316 ring_nr, i, bp->rx_ring_size);
2319 prod = NEXT_RX(prod);
2321 rxr->rx_prod = prod;
2322 ring->fw_ring_id = INVALID_HW_RING_ID;
2324 ring = &rxr->rx_agg_ring_struct;
2325 ring->fw_ring_id = INVALID_HW_RING_ID;
2327 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2330 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
2331 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2333 bnxt_init_rxbd_pages(ring, type);
2335 prod = rxr->rx_agg_prod;
2336 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2337 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2338 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2339 ring_nr, i, bp->rx_ring_size);
2342 prod = NEXT_RX_AGG(prod);
2344 rxr->rx_agg_prod = prod;
2346 if (bp->flags & BNXT_FLAG_TPA) {
2351 for (i = 0; i < MAX_TPA; i++) {
2352 data = __bnxt_alloc_rx_data(bp, &mapping,
2357 rxr->rx_tpa[i].data = data;
2358 rxr->rx_tpa[i].mapping = mapping;
2361 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2369 static int bnxt_init_rx_rings(struct bnxt *bp)
2373 for (i = 0; i < bp->rx_nr_rings; i++) {
2374 rc = bnxt_init_one_rx_ring(bp, i);
2382 static int bnxt_init_tx_rings(struct bnxt *bp)
2386 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2389 for (i = 0; i < bp->tx_nr_rings; i++) {
2390 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2391 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2393 ring->fw_ring_id = INVALID_HW_RING_ID;
2399 static void bnxt_free_ring_grps(struct bnxt *bp)
2401 kfree(bp->grp_info);
2402 bp->grp_info = NULL;
2405 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2410 bp->grp_info = kcalloc(bp->cp_nr_rings,
2411 sizeof(struct bnxt_ring_grp_info),
2416 for (i = 0; i < bp->cp_nr_rings; i++) {
2418 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2419 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2420 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2421 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2422 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2427 static void bnxt_free_vnics(struct bnxt *bp)
2429 kfree(bp->vnic_info);
2430 bp->vnic_info = NULL;
2434 static int bnxt_alloc_vnics(struct bnxt *bp)
2438 #ifdef CONFIG_RFS_ACCEL
2439 if (bp->flags & BNXT_FLAG_RFS)
2440 num_vnics += bp->rx_nr_rings;
2443 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2446 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2451 bp->nr_vnics = num_vnics;
2455 static void bnxt_init_vnics(struct bnxt *bp)
2459 for (i = 0; i < bp->nr_vnics; i++) {
2460 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2462 vnic->fw_vnic_id = INVALID_HW_RING_ID;
2463 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2464 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
2465 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2467 if (bp->vnic_info[i].rss_hash_key) {
2469 prandom_bytes(vnic->rss_hash_key,
2472 memcpy(vnic->rss_hash_key,
2473 bp->vnic_info[0].rss_hash_key,
2479 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2483 pages = ring_size / desc_per_pg;
2490 while (pages & (pages - 1))
2496 static void bnxt_set_tpa_flags(struct bnxt *bp)
2498 bp->flags &= ~BNXT_FLAG_TPA;
2499 if (bp->dev->features & NETIF_F_LRO)
2500 bp->flags |= BNXT_FLAG_LRO;
2501 if (bp->dev->features & NETIF_F_GRO)
2502 bp->flags |= BNXT_FLAG_GRO;
2505 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2508 void bnxt_set_ring_params(struct bnxt *bp)
2510 u32 ring_size, rx_size, rx_space;
2511 u32 agg_factor = 0, agg_ring_size = 0;
2513 /* 8 for CRC and VLAN */
2514 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2516 rx_space = rx_size + NET_SKB_PAD +
2517 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2519 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2520 ring_size = bp->rx_ring_size;
2521 bp->rx_agg_ring_size = 0;
2522 bp->rx_agg_nr_pages = 0;
2524 if (bp->flags & BNXT_FLAG_TPA)
2525 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
2527 bp->flags &= ~BNXT_FLAG_JUMBO;
2528 if (rx_space > PAGE_SIZE) {
2531 bp->flags |= BNXT_FLAG_JUMBO;
2532 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2533 if (jumbo_factor > agg_factor)
2534 agg_factor = jumbo_factor;
2536 agg_ring_size = ring_size * agg_factor;
2538 if (agg_ring_size) {
2539 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2541 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2542 u32 tmp = agg_ring_size;
2544 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2545 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2546 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2547 tmp, agg_ring_size);
2549 bp->rx_agg_ring_size = agg_ring_size;
2550 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2551 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2552 rx_space = rx_size + NET_SKB_PAD +
2553 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2556 bp->rx_buf_use_size = rx_size;
2557 bp->rx_buf_size = rx_space;
2559 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2560 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2562 ring_size = bp->tx_ring_size;
2563 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2564 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2566 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2567 bp->cp_ring_size = ring_size;
2569 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2570 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2571 bp->cp_nr_pages = MAX_CP_PAGES;
2572 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2573 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2574 ring_size, bp->cp_ring_size);
2576 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2577 bp->cp_ring_mask = bp->cp_bit - 1;
2580 static void bnxt_free_vnic_attributes(struct bnxt *bp)
2583 struct bnxt_vnic_info *vnic;
2584 struct pci_dev *pdev = bp->pdev;
2589 for (i = 0; i < bp->nr_vnics; i++) {
2590 vnic = &bp->vnic_info[i];
2592 kfree(vnic->fw_grp_ids);
2593 vnic->fw_grp_ids = NULL;
2595 kfree(vnic->uc_list);
2596 vnic->uc_list = NULL;
2598 if (vnic->mc_list) {
2599 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2600 vnic->mc_list, vnic->mc_list_mapping);
2601 vnic->mc_list = NULL;
2604 if (vnic->rss_table) {
2605 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2607 vnic->rss_table_dma_addr);
2608 vnic->rss_table = NULL;
2611 vnic->rss_hash_key = NULL;
2616 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2618 int i, rc = 0, size;
2619 struct bnxt_vnic_info *vnic;
2620 struct pci_dev *pdev = bp->pdev;
2623 for (i = 0; i < bp->nr_vnics; i++) {
2624 vnic = &bp->vnic_info[i];
2626 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2627 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2630 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2631 if (!vnic->uc_list) {
2638 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2639 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2641 dma_alloc_coherent(&pdev->dev,
2643 &vnic->mc_list_mapping,
2645 if (!vnic->mc_list) {
2651 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2652 max_rings = bp->rx_nr_rings;
2656 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2657 if (!vnic->fw_grp_ids) {
2662 /* Allocate rss table and hash key */
2663 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2664 &vnic->rss_table_dma_addr,
2666 if (!vnic->rss_table) {
2671 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2673 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2674 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2682 static void bnxt_free_hwrm_resources(struct bnxt *bp)
2684 struct pci_dev *pdev = bp->pdev;
2686 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2687 bp->hwrm_cmd_resp_dma_addr);
2689 bp->hwrm_cmd_resp_addr = NULL;
2690 if (bp->hwrm_dbg_resp_addr) {
2691 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2692 bp->hwrm_dbg_resp_addr,
2693 bp->hwrm_dbg_resp_dma_addr);
2695 bp->hwrm_dbg_resp_addr = NULL;
2699 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2701 struct pci_dev *pdev = bp->pdev;
2703 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2704 &bp->hwrm_cmd_resp_dma_addr,
2706 if (!bp->hwrm_cmd_resp_addr)
2708 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2709 HWRM_DBG_REG_BUF_SIZE,
2710 &bp->hwrm_dbg_resp_dma_addr,
2712 if (!bp->hwrm_dbg_resp_addr)
2713 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2718 static void bnxt_free_stats(struct bnxt *bp)
2721 struct pci_dev *pdev = bp->pdev;
2723 if (bp->hw_rx_port_stats) {
2724 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2725 bp->hw_rx_port_stats,
2726 bp->hw_rx_port_stats_map);
2727 bp->hw_rx_port_stats = NULL;
2728 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2734 size = sizeof(struct ctx_hw_stats);
2736 for (i = 0; i < bp->cp_nr_rings; i++) {
2737 struct bnxt_napi *bnapi = bp->bnapi[i];
2738 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2740 if (cpr->hw_stats) {
2741 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2743 cpr->hw_stats = NULL;
2748 static int bnxt_alloc_stats(struct bnxt *bp)
2751 struct pci_dev *pdev = bp->pdev;
2753 size = sizeof(struct ctx_hw_stats);
2755 for (i = 0; i < bp->cp_nr_rings; i++) {
2756 struct bnxt_napi *bnapi = bp->bnapi[i];
2757 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2759 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2765 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2768 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
2769 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2770 sizeof(struct tx_port_stats) + 1024;
2772 bp->hw_rx_port_stats =
2773 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2774 &bp->hw_rx_port_stats_map,
2776 if (!bp->hw_rx_port_stats)
2779 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2781 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2782 sizeof(struct rx_port_stats) + 512;
2783 bp->flags |= BNXT_FLAG_PORT_STATS;
2788 static void bnxt_clear_ring_indices(struct bnxt *bp)
2795 for (i = 0; i < bp->cp_nr_rings; i++) {
2796 struct bnxt_napi *bnapi = bp->bnapi[i];
2797 struct bnxt_cp_ring_info *cpr;
2798 struct bnxt_rx_ring_info *rxr;
2799 struct bnxt_tx_ring_info *txr;
2804 cpr = &bnapi->cp_ring;
2805 cpr->cp_raw_cons = 0;
2807 txr = bnapi->tx_ring;
2813 rxr = bnapi->rx_ring;
2816 rxr->rx_agg_prod = 0;
2817 rxr->rx_sw_agg_prod = 0;
2818 rxr->rx_next_cons = 0;
2823 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2825 #ifdef CONFIG_RFS_ACCEL
2828 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2829 * safe to delete the hash table.
2831 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2832 struct hlist_head *head;
2833 struct hlist_node *tmp;
2834 struct bnxt_ntuple_filter *fltr;
2836 head = &bp->ntp_fltr_hash_tbl[i];
2837 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2838 hlist_del(&fltr->hash);
2843 kfree(bp->ntp_fltr_bmap);
2844 bp->ntp_fltr_bmap = NULL;
2846 bp->ntp_fltr_count = 0;
2850 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2852 #ifdef CONFIG_RFS_ACCEL
2855 if (!(bp->flags & BNXT_FLAG_RFS))
2858 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2859 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2861 bp->ntp_fltr_count = 0;
2862 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2865 if (!bp->ntp_fltr_bmap)
2874 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2876 bnxt_free_vnic_attributes(bp);
2877 bnxt_free_tx_rings(bp);
2878 bnxt_free_rx_rings(bp);
2879 bnxt_free_cp_rings(bp);
2880 bnxt_free_ntp_fltrs(bp, irq_re_init);
2882 bnxt_free_stats(bp);
2883 bnxt_free_ring_grps(bp);
2884 bnxt_free_vnics(bp);
2892 bnxt_clear_ring_indices(bp);
2896 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2898 int i, j, rc, size, arr_size;
2902 /* Allocate bnapi mem pointer array and mem block for
2905 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2907 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2908 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2914 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2915 bp->bnapi[i] = bnapi;
2916 bp->bnapi[i]->index = i;
2917 bp->bnapi[i]->bp = bp;
2920 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2921 sizeof(struct bnxt_rx_ring_info),
2926 for (i = 0; i < bp->rx_nr_rings; i++) {
2927 bp->rx_ring[i].bnapi = bp->bnapi[i];
2928 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2931 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2932 sizeof(struct bnxt_tx_ring_info),
2937 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2940 j = bp->rx_nr_rings;
2942 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2943 bp->tx_ring[i].bnapi = bp->bnapi[j];
2944 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
2947 rc = bnxt_alloc_stats(bp);
2951 rc = bnxt_alloc_ntp_fltrs(bp);
2955 rc = bnxt_alloc_vnics(bp);
2960 bnxt_init_ring_struct(bp);
2962 rc = bnxt_alloc_rx_rings(bp);
2966 rc = bnxt_alloc_tx_rings(bp);
2970 rc = bnxt_alloc_cp_rings(bp);
2974 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2975 BNXT_VNIC_UCAST_FLAG;
2976 rc = bnxt_alloc_vnic_attributes(bp);
2982 bnxt_free_mem(bp, true);
2986 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2987 u16 cmpl_ring, u16 target_id)
2989 struct input *req = request;
2991 req->req_type = cpu_to_le16(req_type);
2992 req->cmpl_ring = cpu_to_le16(cmpl_ring);
2993 req->target_id = cpu_to_le16(target_id);
2994 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
2997 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
2998 int timeout, bool silent)
3000 int i, intr_process, rc, tmo_count;
3001 struct input *req = msg;
3003 __le32 *resp_len, *valid;
3004 u16 cp_ring_id, len = 0;
3005 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3007 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
3008 memset(resp, 0, PAGE_SIZE);
3009 cp_ring_id = le16_to_cpu(req->cmpl_ring);
3010 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3012 /* Write request msg to hwrm channel */
3013 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3015 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
3016 writel(0, bp->bar0 + i);
3018 /* currently supports only one outstanding message */
3020 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
3022 /* Ring channel doorbell */
3023 writel(1, bp->bar0 + 0x100);
3026 timeout = DFLT_HWRM_CMD_TIMEOUT;
3029 tmo_count = timeout * 40;
3031 /* Wait until hwrm response cmpl interrupt is processed */
3032 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
3034 usleep_range(25, 40);
3037 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3038 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
3039 le16_to_cpu(req->req_type));
3043 /* Check if response len is updated */
3044 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
3045 for (i = 0; i < tmo_count; i++) {
3046 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3050 usleep_range(25, 40);
3053 if (i >= tmo_count) {
3054 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
3055 timeout, le16_to_cpu(req->req_type),
3056 le16_to_cpu(req->seq_id), len);
3060 /* Last word of resp contains valid bit */
3061 valid = bp->hwrm_cmd_resp_addr + len - 4;
3062 for (i = 0; i < 5; i++) {
3063 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3069 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
3070 timeout, le16_to_cpu(req->req_type),
3071 le16_to_cpu(req->seq_id), len, *valid);
3076 rc = le16_to_cpu(resp->error_code);
3078 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3079 le16_to_cpu(resp->req_type),
3080 le16_to_cpu(resp->seq_id), rc);
3084 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3086 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
3089 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3093 mutex_lock(&bp->hwrm_cmd_lock);
3094 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3095 mutex_unlock(&bp->hwrm_cmd_lock);
3099 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3104 mutex_lock(&bp->hwrm_cmd_lock);
3105 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3106 mutex_unlock(&bp->hwrm_cmd_lock);
3110 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3112 struct hwrm_func_drv_rgtr_input req = {0};
3114 DECLARE_BITMAP(async_events_bmap, 256);
3115 u32 *events = (u32 *)async_events_bmap;
3117 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3120 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3121 FUNC_DRV_RGTR_REQ_ENABLES_VER |
3122 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3124 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3125 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3126 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3128 for (i = 0; i < 8; i++)
3129 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3131 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
3132 req.ver_maj = DRV_VER_MAJ;
3133 req.ver_min = DRV_VER_MIN;
3134 req.ver_upd = DRV_VER_UPD;
3137 DECLARE_BITMAP(vf_req_snif_bmap, 256);
3138 u32 *data = (u32 *)vf_req_snif_bmap;
3140 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
3141 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3142 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3144 for (i = 0; i < 8; i++)
3145 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3148 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3151 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3154 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3156 struct hwrm_func_drv_unrgtr_input req = {0};
3158 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3159 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3162 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3165 struct hwrm_tunnel_dst_port_free_input req = {0};
3167 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3168 req.tunnel_type = tunnel_type;
3170 switch (tunnel_type) {
3171 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3172 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3174 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3175 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3181 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3183 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3188 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3192 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3193 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3195 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3197 req.tunnel_type = tunnel_type;
3198 req.tunnel_dst_port_val = port;
3200 mutex_lock(&bp->hwrm_cmd_lock);
3201 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3203 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3208 if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN)
3209 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3211 else if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE)
3212 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3214 mutex_unlock(&bp->hwrm_cmd_lock);
3218 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3220 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3221 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3223 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
3224 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3226 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3227 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3228 req.mask = cpu_to_le32(vnic->rx_mask);
3229 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3232 #ifdef CONFIG_RFS_ACCEL
3233 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3234 struct bnxt_ntuple_filter *fltr)
3236 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3238 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3239 req.ntuple_filter_id = fltr->filter_id;
3240 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3243 #define BNXT_NTP_FLTR_FLAGS \
3244 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3245 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3246 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3247 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3248 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3249 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3250 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3251 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3252 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3253 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3254 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3255 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3256 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
3257 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
3259 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3260 struct bnxt_ntuple_filter *fltr)
3263 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3264 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3265 bp->hwrm_cmd_resp_addr;
3266 struct flow_keys *keys = &fltr->fkeys;
3267 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3269 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
3270 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
3272 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3274 req.ethertype = htons(ETH_P_IP);
3275 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
3276 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
3277 req.ip_protocol = keys->basic.ip_proto;
3279 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3280 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3281 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3282 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3284 req.src_port = keys->ports.src;
3285 req.src_port_mask = cpu_to_be16(0xffff);
3286 req.dst_port = keys->ports.dst;
3287 req.dst_port_mask = cpu_to_be16(0xffff);
3289 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
3290 mutex_lock(&bp->hwrm_cmd_lock);
3291 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3293 fltr->filter_id = resp->ntuple_filter_id;
3294 mutex_unlock(&bp->hwrm_cmd_lock);
3299 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3303 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3304 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3306 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
3307 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3308 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3310 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
3311 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
3313 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
3314 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
3315 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3316 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3317 req.l2_addr_mask[0] = 0xff;
3318 req.l2_addr_mask[1] = 0xff;
3319 req.l2_addr_mask[2] = 0xff;
3320 req.l2_addr_mask[3] = 0xff;
3321 req.l2_addr_mask[4] = 0xff;
3322 req.l2_addr_mask[5] = 0xff;
3324 mutex_lock(&bp->hwrm_cmd_lock);
3325 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3327 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3329 mutex_unlock(&bp->hwrm_cmd_lock);
3333 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3335 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3338 /* Any associated ntuple filters will also be cleared by firmware. */
3339 mutex_lock(&bp->hwrm_cmd_lock);
3340 for (i = 0; i < num_of_vnics; i++) {
3341 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3343 for (j = 0; j < vnic->uc_filter_count; j++) {
3344 struct hwrm_cfa_l2_filter_free_input req = {0};
3346 bnxt_hwrm_cmd_hdr_init(bp, &req,
3347 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3349 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3351 rc = _hwrm_send_message(bp, &req, sizeof(req),
3354 vnic->uc_filter_count = 0;
3356 mutex_unlock(&bp->hwrm_cmd_lock);
3361 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3363 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3364 struct hwrm_vnic_tpa_cfg_input req = {0};
3366 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3369 u16 mss = bp->dev->mtu - 40;
3370 u32 nsegs, n, segs = 0, flags;
3372 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3373 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3374 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3375 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3376 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3377 if (tpa_flags & BNXT_FLAG_GRO)
3378 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3380 req.flags = cpu_to_le32(flags);
3383 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
3384 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3385 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
3387 /* Number of segs are log2 units, and first packet is not
3388 * included as part of this units.
3390 if (mss <= BNXT_RX_PAGE_SIZE) {
3391 n = BNXT_RX_PAGE_SIZE / mss;
3392 nsegs = (MAX_SKB_FRAGS - 1) * n;
3394 n = mss / BNXT_RX_PAGE_SIZE;
3395 if (mss & (BNXT_RX_PAGE_SIZE - 1))
3397 nsegs = (MAX_SKB_FRAGS - n) / n;
3400 segs = ilog2(nsegs);
3401 req.max_agg_segs = cpu_to_le16(segs);
3402 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
3404 req.min_agg_len = cpu_to_le32(512);
3406 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3408 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3411 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3413 u32 i, j, max_rings;
3414 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3415 struct hwrm_vnic_rss_cfg_input req = {0};
3417 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
3420 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3422 vnic->hash_type = BNXT_RSS_HASH_TYPE_FLAG_IPV4 |
3423 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV4 |
3424 BNXT_RSS_HASH_TYPE_FLAG_IPV6 |
3425 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV6;
3427 req.hash_type = cpu_to_le32(vnic->hash_type);
3429 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3430 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3431 max_rings = bp->rx_nr_rings - 1;
3433 max_rings = bp->rx_nr_rings;
3438 /* Fill the RSS indirection table with ring group ids */
3439 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3442 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3445 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3446 req.hash_key_tbl_addr =
3447 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3449 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3450 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3453 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3455 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3456 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3458 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3459 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3460 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3461 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3463 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3464 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3465 /* thresholds not implemented in firmware yet */
3466 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3467 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3468 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3469 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3472 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3475 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3477 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3478 req.rss_cos_lb_ctx_id =
3479 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
3481 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3482 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
3485 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3489 for (i = 0; i < bp->nr_vnics; i++) {
3490 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3492 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3493 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3494 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3497 bp->rsscos_nr_ctxs = 0;
3500 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
3503 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3504 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3505 bp->hwrm_cmd_resp_addr;
3507 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3510 mutex_lock(&bp->hwrm_cmd_lock);
3511 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3513 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
3514 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3515 mutex_unlock(&bp->hwrm_cmd_lock);
3520 static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3522 unsigned int ring = 0, grp_idx;
3523 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3524 struct hwrm_vnic_cfg_input req = {0};
3527 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
3529 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3530 /* Only RSS support for now TBD: COS & LB */
3531 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3532 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3533 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3534 VNIC_CFG_REQ_ENABLES_MRU);
3536 req.rss_rule = cpu_to_le16(0xffff);
3539 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3540 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
3541 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3542 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3544 req.cos_rule = cpu_to_le16(0xffff);
3547 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3549 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
3551 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3552 ring = bp->rx_nr_rings - 1;
3554 grp_idx = bp->rx_ring[ring].bnapi->index;
3555 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3556 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3558 req.lb_rule = cpu_to_le16(0xffff);
3559 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3562 #ifdef CONFIG_BNXT_SRIOV
3564 def_vlan = bp->vf.vlan;
3566 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
3567 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3569 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3572 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3576 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3577 struct hwrm_vnic_free_input req = {0};
3579 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3581 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3583 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3586 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3591 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3595 for (i = 0; i < bp->nr_vnics; i++)
3596 bnxt_hwrm_vnic_free_one(bp, i);
3599 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3600 unsigned int start_rx_ring_idx,
3601 unsigned int nr_rings)
3604 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
3605 struct hwrm_vnic_alloc_input req = {0};
3606 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3608 /* map ring groups to this vnic */
3609 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3610 grp_idx = bp->rx_ring[i].bnapi->index;
3611 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
3612 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
3616 bp->vnic_info[vnic_id].fw_grp_ids[j] =
3617 bp->grp_info[grp_idx].fw_grp_id;
3620 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3621 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
3623 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3625 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3627 mutex_lock(&bp->hwrm_cmd_lock);
3628 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3630 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3631 mutex_unlock(&bp->hwrm_cmd_lock);
3635 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3640 mutex_lock(&bp->hwrm_cmd_lock);
3641 for (i = 0; i < bp->rx_nr_rings; i++) {
3642 struct hwrm_ring_grp_alloc_input req = {0};
3643 struct hwrm_ring_grp_alloc_output *resp =
3644 bp->hwrm_cmd_resp_addr;
3645 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
3647 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3649 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3650 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3651 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3652 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
3654 rc = _hwrm_send_message(bp, &req, sizeof(req),
3659 bp->grp_info[grp_idx].fw_grp_id =
3660 le32_to_cpu(resp->ring_group_id);
3662 mutex_unlock(&bp->hwrm_cmd_lock);
3666 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3670 struct hwrm_ring_grp_free_input req = {0};
3675 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3677 mutex_lock(&bp->hwrm_cmd_lock);
3678 for (i = 0; i < bp->cp_nr_rings; i++) {
3679 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3682 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3684 rc = _hwrm_send_message(bp, &req, sizeof(req),
3688 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3690 mutex_unlock(&bp->hwrm_cmd_lock);
3694 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3695 struct bnxt_ring_struct *ring,
3696 u32 ring_type, u32 map_index,
3699 int rc = 0, err = 0;
3700 struct hwrm_ring_alloc_input req = {0};
3701 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3704 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3707 if (ring->nr_pages > 1) {
3708 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3709 /* Page size is in log2 units */
3710 req.page_size = BNXT_PAGE_SHIFT;
3711 req.page_tbl_depth = 1;
3713 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3716 /* Association of ring index with doorbell index and MSIX number */
3717 req.logical_id = cpu_to_le16(map_index);
3719 switch (ring_type) {
3720 case HWRM_RING_ALLOC_TX:
3721 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3722 /* Association of transmit ring with completion ring */
3724 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3725 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3726 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3727 req.queue_id = cpu_to_le16(ring->queue_id);
3729 case HWRM_RING_ALLOC_RX:
3730 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3731 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3733 case HWRM_RING_ALLOC_AGG:
3734 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3735 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3737 case HWRM_RING_ALLOC_CMPL:
3738 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3739 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3740 if (bp->flags & BNXT_FLAG_USING_MSIX)
3741 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3744 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3749 mutex_lock(&bp->hwrm_cmd_lock);
3750 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3751 err = le16_to_cpu(resp->error_code);
3752 ring_id = le16_to_cpu(resp->ring_id);
3753 mutex_unlock(&bp->hwrm_cmd_lock);
3756 switch (ring_type) {
3757 case RING_FREE_REQ_RING_TYPE_CMPL:
3758 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3762 case RING_FREE_REQ_RING_TYPE_RX:
3763 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3767 case RING_FREE_REQ_RING_TYPE_TX:
3768 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3773 netdev_err(bp->dev, "Invalid ring\n");
3777 ring->fw_ring_id = ring_id;
3781 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3785 for (i = 0; i < bp->cp_nr_rings; i++) {
3786 struct bnxt_napi *bnapi = bp->bnapi[i];
3787 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3788 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3790 cpr->cp_doorbell = bp->bar1 + i * 0x80;
3791 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3792 INVALID_STATS_CTX_ID);
3795 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3796 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
3799 for (i = 0; i < bp->tx_nr_rings; i++) {
3800 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3801 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3802 u32 map_idx = txr->bnapi->index;
3803 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
3805 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3806 map_idx, fw_stats_ctx);
3809 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
3812 for (i = 0; i < bp->rx_nr_rings; i++) {
3813 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3814 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
3815 u32 map_idx = rxr->bnapi->index;
3817 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3818 map_idx, INVALID_STATS_CTX_ID);
3821 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
3822 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
3823 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
3826 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3827 for (i = 0; i < bp->rx_nr_rings; i++) {
3828 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3829 struct bnxt_ring_struct *ring =
3830 &rxr->rx_agg_ring_struct;
3831 u32 grp_idx = rxr->bnapi->index;
3832 u32 map_idx = grp_idx + bp->rx_nr_rings;
3834 rc = hwrm_ring_alloc_send_msg(bp, ring,
3835 HWRM_RING_ALLOC_AGG,
3837 INVALID_STATS_CTX_ID);
3841 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
3842 writel(DB_KEY_RX | rxr->rx_agg_prod,
3843 rxr->rx_agg_doorbell);
3844 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
3851 static int hwrm_ring_free_send_msg(struct bnxt *bp,
3852 struct bnxt_ring_struct *ring,
3853 u32 ring_type, int cmpl_ring_id)
3856 struct hwrm_ring_free_input req = {0};
3857 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3860 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
3861 req.ring_type = ring_type;
3862 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3864 mutex_lock(&bp->hwrm_cmd_lock);
3865 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3866 error_code = le16_to_cpu(resp->error_code);
3867 mutex_unlock(&bp->hwrm_cmd_lock);
3869 if (rc || error_code) {
3870 switch (ring_type) {
3871 case RING_FREE_REQ_RING_TYPE_CMPL:
3872 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3875 case RING_FREE_REQ_RING_TYPE_RX:
3876 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3879 case RING_FREE_REQ_RING_TYPE_TX:
3880 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3884 netdev_err(bp->dev, "Invalid ring\n");
3891 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
3898 for (i = 0; i < bp->tx_nr_rings; i++) {
3899 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3900 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
3901 u32 grp_idx = txr->bnapi->index;
3902 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
3904 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3905 hwrm_ring_free_send_msg(bp, ring,
3906 RING_FREE_REQ_RING_TYPE_TX,
3907 close_path ? cmpl_ring_id :
3908 INVALID_HW_RING_ID);
3909 ring->fw_ring_id = INVALID_HW_RING_ID;
3913 for (i = 0; i < bp->rx_nr_rings; i++) {
3914 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3915 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
3916 u32 grp_idx = rxr->bnapi->index;
3917 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
3919 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3920 hwrm_ring_free_send_msg(bp, ring,
3921 RING_FREE_REQ_RING_TYPE_RX,
3922 close_path ? cmpl_ring_id :
3923 INVALID_HW_RING_ID);
3924 ring->fw_ring_id = INVALID_HW_RING_ID;
3925 bp->grp_info[grp_idx].rx_fw_ring_id =
3930 for (i = 0; i < bp->rx_nr_rings; i++) {
3931 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3932 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
3933 u32 grp_idx = rxr->bnapi->index;
3934 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
3936 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3937 hwrm_ring_free_send_msg(bp, ring,
3938 RING_FREE_REQ_RING_TYPE_RX,
3939 close_path ? cmpl_ring_id :
3940 INVALID_HW_RING_ID);
3941 ring->fw_ring_id = INVALID_HW_RING_ID;
3942 bp->grp_info[grp_idx].agg_fw_ring_id =
3947 for (i = 0; i < bp->cp_nr_rings; i++) {
3948 struct bnxt_napi *bnapi = bp->bnapi[i];
3949 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3950 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3952 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3953 hwrm_ring_free_send_msg(bp, ring,
3954 RING_FREE_REQ_RING_TYPE_CMPL,
3955 INVALID_HW_RING_ID);
3956 ring->fw_ring_id = INVALID_HW_RING_ID;
3957 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
3962 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
3963 u32 buf_tmrs, u16 flags,
3964 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3966 req->flags = cpu_to_le16(flags);
3967 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
3968 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
3969 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
3970 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
3971 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
3972 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
3973 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
3974 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
3977 int bnxt_hwrm_set_coal(struct bnxt *bp)
3980 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
3982 u16 max_buf, max_buf_irq;
3983 u16 buf_tmr, buf_tmr_irq;
3986 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
3987 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3988 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
3989 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3991 /* Each rx completion (2 records) should be DMAed immediately.
3992 * DMA 1/4 of the completion buffers at a time.
3994 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
3995 /* max_buf must not be zero */
3996 max_buf = clamp_t(u16, max_buf, 1, 63);
3997 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
3998 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
3999 /* buf timer set to 1/4 of interrupt timer */
4000 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4001 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4002 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4004 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4006 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4007 * if coal_ticks is less than 25 us.
4009 if (bp->rx_coal_ticks < 25)
4010 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4012 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4013 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4015 /* max_buf must not be zero */
4016 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4017 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4018 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4019 /* buf timer set to 1/4 of interrupt timer */
4020 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4021 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4022 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4024 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4025 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4026 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
4028 mutex_lock(&bp->hwrm_cmd_lock);
4029 for (i = 0; i < bp->cp_nr_rings; i++) {
4030 struct bnxt_napi *bnapi = bp->bnapi[i];
4033 if (!bnapi->rx_ring)
4035 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4037 rc = _hwrm_send_message(bp, req, sizeof(*req),
4042 mutex_unlock(&bp->hwrm_cmd_lock);
4046 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4049 struct hwrm_stat_ctx_free_input req = {0};
4054 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4057 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4059 mutex_lock(&bp->hwrm_cmd_lock);
4060 for (i = 0; i < bp->cp_nr_rings; i++) {
4061 struct bnxt_napi *bnapi = bp->bnapi[i];
4062 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4064 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4065 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4067 rc = _hwrm_send_message(bp, &req, sizeof(req),
4072 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4075 mutex_unlock(&bp->hwrm_cmd_lock);
4079 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4082 struct hwrm_stat_ctx_alloc_input req = {0};
4083 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4085 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4088 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4090 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
4092 mutex_lock(&bp->hwrm_cmd_lock);
4093 for (i = 0; i < bp->cp_nr_rings; i++) {
4094 struct bnxt_napi *bnapi = bp->bnapi[i];
4095 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4097 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4099 rc = _hwrm_send_message(bp, &req, sizeof(req),
4104 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4106 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4108 mutex_unlock(&bp->hwrm_cmd_lock);
4112 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4114 struct hwrm_func_qcfg_input req = {0};
4115 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4118 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4119 req.fid = cpu_to_le16(0xffff);
4120 mutex_lock(&bp->hwrm_cmd_lock);
4121 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4123 goto func_qcfg_exit;
4125 #ifdef CONFIG_BNXT_SRIOV
4127 struct bnxt_vf_info *vf = &bp->vf;
4129 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4132 switch (resp->port_partition_type) {
4133 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4134 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4135 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4136 bp->port_partition_type = resp->port_partition_type;
4141 mutex_unlock(&bp->hwrm_cmd_lock);
4145 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
4148 struct hwrm_func_qcaps_input req = {0};
4149 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4151 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4152 req.fid = cpu_to_le16(0xffff);
4154 mutex_lock(&bp->hwrm_cmd_lock);
4155 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4157 goto hwrm_func_qcaps_exit;
4160 struct bnxt_pf_info *pf = &bp->pf;
4162 pf->fw_fid = le16_to_cpu(resp->fid);
4163 pf->port_id = le16_to_cpu(resp->port_id);
4164 bp->dev->dev_port = pf->port_id;
4165 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
4166 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
4167 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4168 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4169 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4170 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4171 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4172 if (!pf->max_hw_ring_grps)
4173 pf->max_hw_ring_grps = pf->max_tx_rings;
4174 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4175 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4176 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4177 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4178 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4179 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4180 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4181 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4182 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4183 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4184 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4186 #ifdef CONFIG_BNXT_SRIOV
4187 struct bnxt_vf_info *vf = &bp->vf;
4189 vf->fw_fid = le16_to_cpu(resp->fid);
4190 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
4191 if (is_valid_ether_addr(vf->mac_addr))
4192 /* overwrite netdev dev_adr with admin VF MAC */
4193 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
4195 random_ether_addr(bp->dev->dev_addr);
4197 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4198 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4199 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4200 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4201 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4202 if (!vf->max_hw_ring_grps)
4203 vf->max_hw_ring_grps = vf->max_tx_rings;
4204 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4205 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4206 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4210 bp->tx_push_thresh = 0;
4212 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4213 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4215 hwrm_func_qcaps_exit:
4216 mutex_unlock(&bp->hwrm_cmd_lock);
4220 static int bnxt_hwrm_func_reset(struct bnxt *bp)
4222 struct hwrm_func_reset_input req = {0};
4224 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4227 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4230 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4233 struct hwrm_queue_qportcfg_input req = {0};
4234 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4237 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4239 mutex_lock(&bp->hwrm_cmd_lock);
4240 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4244 if (!resp->max_configurable_queues) {
4248 bp->max_tc = resp->max_configurable_queues;
4249 if (bp->max_tc > BNXT_MAX_QUEUE)
4250 bp->max_tc = BNXT_MAX_QUEUE;
4252 qptr = &resp->queue_id0;
4253 for (i = 0; i < bp->max_tc; i++) {
4254 bp->q_info[i].queue_id = *qptr++;
4255 bp->q_info[i].queue_profile = *qptr++;
4259 mutex_unlock(&bp->hwrm_cmd_lock);
4263 static int bnxt_hwrm_ver_get(struct bnxt *bp)
4266 struct hwrm_ver_get_input req = {0};
4267 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4269 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
4270 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4271 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4272 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4273 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4274 mutex_lock(&bp->hwrm_cmd_lock);
4275 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4277 goto hwrm_ver_get_exit;
4279 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4281 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4282 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
4283 if (resp->hwrm_intf_maj < 1) {
4284 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
4285 resp->hwrm_intf_maj, resp->hwrm_intf_min,
4286 resp->hwrm_intf_upd);
4287 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
4289 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
4290 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4291 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4293 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4294 if (!bp->hwrm_cmd_timeout)
4295 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4297 if (resp->hwrm_intf_maj >= 1)
4298 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4300 bp->chip_num = le16_to_cpu(resp->chip_num);
4301 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4303 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
4306 mutex_unlock(&bp->hwrm_cmd_lock);
4310 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4313 struct bnxt_pf_info *pf = &bp->pf;
4314 struct hwrm_port_qstats_input req = {0};
4316 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4319 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4320 req.port_id = cpu_to_le16(pf->port_id);
4321 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4322 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4323 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4327 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4329 if (bp->vxlan_port_cnt) {
4330 bnxt_hwrm_tunnel_dst_port_free(
4331 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4333 bp->vxlan_port_cnt = 0;
4334 if (bp->nge_port_cnt) {
4335 bnxt_hwrm_tunnel_dst_port_free(
4336 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4338 bp->nge_port_cnt = 0;
4341 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4347 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4348 for (i = 0; i < bp->nr_vnics; i++) {
4349 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4351 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4359 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4363 for (i = 0; i < bp->nr_vnics; i++)
4364 bnxt_hwrm_vnic_set_rss(bp, i, false);
4367 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4370 if (bp->vnic_info) {
4371 bnxt_hwrm_clear_vnic_filter(bp);
4372 /* clear all RSS setting before free vnic ctx */
4373 bnxt_hwrm_clear_vnic_rss(bp);
4374 bnxt_hwrm_vnic_ctx_free(bp);
4375 /* before free the vnic, undo the vnic tpa settings */
4376 if (bp->flags & BNXT_FLAG_TPA)
4377 bnxt_set_tpa(bp, false);
4378 bnxt_hwrm_vnic_free(bp);
4380 bnxt_hwrm_ring_free(bp, close_path);
4381 bnxt_hwrm_ring_grp_free(bp);
4383 bnxt_hwrm_stat_ctx_free(bp);
4384 bnxt_hwrm_free_tunnel_ports(bp);
4388 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4392 /* allocate context for vnic */
4393 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
4395 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4397 goto vnic_setup_err;
4399 bp->rsscos_nr_ctxs++;
4401 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4402 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4404 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4406 goto vnic_setup_err;
4408 bp->rsscos_nr_ctxs++;
4411 /* configure default vnic, ring grp */
4412 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4414 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4416 goto vnic_setup_err;
4419 /* Enable RSS hashing on vnic */
4420 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4422 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4424 goto vnic_setup_err;
4427 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4428 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4430 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4439 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4441 #ifdef CONFIG_RFS_ACCEL
4444 for (i = 0; i < bp->rx_nr_rings; i++) {
4445 u16 vnic_id = i + 1;
4448 if (vnic_id >= bp->nr_vnics)
4451 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
4452 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
4454 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4458 rc = bnxt_setup_vnic(bp, vnic_id);
4468 /* Allow PF and VF with default VLAN to be in promiscuous mode */
4469 static bool bnxt_promisc_ok(struct bnxt *bp)
4471 #ifdef CONFIG_BNXT_SRIOV
4472 if (BNXT_VF(bp) && !bp->vf.vlan)
4478 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4480 unsigned int rc = 0;
4482 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4484 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4489 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4491 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4498 static int bnxt_cfg_rx_mode(struct bnxt *);
4499 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
4501 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4503 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
4505 unsigned int rx_nr_rings = bp->rx_nr_rings;
4508 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4510 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4516 rc = bnxt_hwrm_ring_alloc(bp);
4518 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4522 rc = bnxt_hwrm_ring_grp_alloc(bp);
4524 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4528 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4531 /* default vnic 0 */
4532 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
4534 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4538 rc = bnxt_setup_vnic(bp, 0);
4542 if (bp->flags & BNXT_FLAG_RFS) {
4543 rc = bnxt_alloc_rfs_vnics(bp);
4548 if (bp->flags & BNXT_FLAG_TPA) {
4549 rc = bnxt_set_tpa(bp, true);
4555 bnxt_update_vf_mac(bp);
4557 /* Filter for default vnic 0 */
4558 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4560 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4563 vnic->uc_filter_count = 1;
4565 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
4567 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
4568 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4570 if (bp->dev->flags & IFF_ALLMULTI) {
4571 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4572 vnic->mc_list_count = 0;
4576 bnxt_mc_list_updated(bp, &mask);
4577 vnic->rx_mask |= mask;
4580 rc = bnxt_cfg_rx_mode(bp);
4584 rc = bnxt_hwrm_set_coal(bp);
4586 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
4589 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4590 rc = bnxt_setup_nitroa0_vnic(bp);
4592 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4597 bnxt_hwrm_func_qcfg(bp);
4598 netdev_update_features(bp->dev);
4604 bnxt_hwrm_resource_free(bp, 0, true);
4609 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4611 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4615 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4617 bnxt_init_rx_rings(bp);
4618 bnxt_init_tx_rings(bp);
4619 bnxt_init_ring_grps(bp, irq_re_init);
4620 bnxt_init_vnics(bp);
4622 return bnxt_init_chip(bp, irq_re_init);
4625 static void bnxt_disable_int(struct bnxt *bp)
4632 for (i = 0; i < bp->cp_nr_rings; i++) {
4633 struct bnxt_napi *bnapi = bp->bnapi[i];
4634 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4636 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4640 static void bnxt_enable_int(struct bnxt *bp)
4644 atomic_set(&bp->intr_sem, 0);
4645 for (i = 0; i < bp->cp_nr_rings; i++) {
4646 struct bnxt_napi *bnapi = bp->bnapi[i];
4647 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4649 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
4653 static int bnxt_set_real_num_queues(struct bnxt *bp)
4656 struct net_device *dev = bp->dev;
4658 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4662 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4666 #ifdef CONFIG_RFS_ACCEL
4667 if (bp->flags & BNXT_FLAG_RFS)
4668 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
4674 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4677 int _rx = *rx, _tx = *tx;
4680 *rx = min_t(int, _rx, max);
4681 *tx = min_t(int, _tx, max);
4686 while (_rx + _tx > max) {
4687 if (_rx > _tx && _rx > 1)
4698 static int bnxt_setup_msix(struct bnxt *bp)
4700 struct msix_entry *msix_ent;
4701 struct net_device *dev = bp->dev;
4702 int i, total_vecs, rc = 0, min = 1;
4703 const int len = sizeof(bp->irq_tbl[0].name);
4705 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4706 total_vecs = bp->cp_nr_rings;
4708 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4712 for (i = 0; i < total_vecs; i++) {
4713 msix_ent[i].entry = i;
4714 msix_ent[i].vector = 0;
4717 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4720 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
4721 if (total_vecs < 0) {
4723 goto msix_setup_exit;
4726 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4730 /* Trim rings based upon num of vectors allocated */
4731 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
4732 total_vecs, min == 1);
4734 goto msix_setup_exit;
4736 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4737 tcs = netdev_get_num_tc(dev);
4739 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4740 if (bp->tx_nr_rings_per_tc == 0) {
4741 netdev_reset_tc(dev);
4742 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4746 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4747 for (i = 0; i < tcs; i++) {
4748 count = bp->tx_nr_rings_per_tc;
4750 netdev_set_tc_queue(dev, i, count, off);
4754 bp->cp_nr_rings = total_vecs;
4756 for (i = 0; i < bp->cp_nr_rings; i++) {
4759 bp->irq_tbl[i].vector = msix_ent[i].vector;
4760 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4762 else if (i < bp->rx_nr_rings)
4767 snprintf(bp->irq_tbl[i].name, len,
4768 "%s-%s-%d", dev->name, attr, i);
4769 bp->irq_tbl[i].handler = bnxt_msix;
4771 rc = bnxt_set_real_num_queues(bp);
4773 goto msix_setup_exit;
4776 goto msix_setup_exit;
4778 bp->flags |= BNXT_FLAG_USING_MSIX;
4783 netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
4784 pci_disable_msix(bp->pdev);
4789 static int bnxt_setup_inta(struct bnxt *bp)
4792 const int len = sizeof(bp->irq_tbl[0].name);
4794 if (netdev_get_num_tc(bp->dev))
4795 netdev_reset_tc(bp->dev);
4797 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
4802 bp->rx_nr_rings = 1;
4803 bp->tx_nr_rings = 1;
4804 bp->cp_nr_rings = 1;
4805 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4806 bp->flags |= BNXT_FLAG_SHARED_RINGS;
4807 bp->irq_tbl[0].vector = bp->pdev->irq;
4808 snprintf(bp->irq_tbl[0].name, len,
4809 "%s-%s-%d", bp->dev->name, "TxRx", 0);
4810 bp->irq_tbl[0].handler = bnxt_inta;
4811 rc = bnxt_set_real_num_queues(bp);
4815 static int bnxt_setup_int_mode(struct bnxt *bp)
4819 if (bp->flags & BNXT_FLAG_MSIX_CAP)
4820 rc = bnxt_setup_msix(bp);
4822 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
4823 /* fallback to INTA */
4824 rc = bnxt_setup_inta(bp);
4829 static void bnxt_free_irq(struct bnxt *bp)
4831 struct bnxt_irq *irq;
4834 #ifdef CONFIG_RFS_ACCEL
4835 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4836 bp->dev->rx_cpu_rmap = NULL;
4841 for (i = 0; i < bp->cp_nr_rings; i++) {
4842 irq = &bp->irq_tbl[i];
4844 free_irq(irq->vector, bp->bnapi[i]);
4847 if (bp->flags & BNXT_FLAG_USING_MSIX)
4848 pci_disable_msix(bp->pdev);
4853 static int bnxt_request_irq(struct bnxt *bp)
4856 unsigned long flags = 0;
4857 #ifdef CONFIG_RFS_ACCEL
4858 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4861 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
4862 flags = IRQF_SHARED;
4864 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4865 struct bnxt_irq *irq = &bp->irq_tbl[i];
4866 #ifdef CONFIG_RFS_ACCEL
4867 if (rmap && bp->bnapi[i]->rx_ring) {
4868 rc = irq_cpu_rmap_add(rmap, irq->vector);
4870 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
4875 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
4885 static void bnxt_del_napi(struct bnxt *bp)
4892 for (i = 0; i < bp->cp_nr_rings; i++) {
4893 struct bnxt_napi *bnapi = bp->bnapi[i];
4895 napi_hash_del(&bnapi->napi);
4896 netif_napi_del(&bnapi->napi);
4900 static void bnxt_init_napi(struct bnxt *bp)
4903 unsigned int cp_nr_rings = bp->cp_nr_rings;
4904 struct bnxt_napi *bnapi;
4906 if (bp->flags & BNXT_FLAG_USING_MSIX) {
4907 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4909 for (i = 0; i < cp_nr_rings; i++) {
4910 bnapi = bp->bnapi[i];
4911 netif_napi_add(bp->dev, &bnapi->napi,
4914 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4915 bnapi = bp->bnapi[cp_nr_rings];
4916 netif_napi_add(bp->dev, &bnapi->napi,
4917 bnxt_poll_nitroa0, 64);
4918 napi_hash_add(&bnapi->napi);
4921 bnapi = bp->bnapi[0];
4922 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
4926 static void bnxt_disable_napi(struct bnxt *bp)
4933 for (i = 0; i < bp->cp_nr_rings; i++) {
4934 napi_disable(&bp->bnapi[i]->napi);
4935 bnxt_disable_poll(bp->bnapi[i]);
4939 static void bnxt_enable_napi(struct bnxt *bp)
4943 for (i = 0; i < bp->cp_nr_rings; i++) {
4944 bp->bnapi[i]->in_reset = false;
4945 bnxt_enable_poll(bp->bnapi[i]);
4946 napi_enable(&bp->bnapi[i]->napi);
4950 static void bnxt_tx_disable(struct bnxt *bp)
4953 struct bnxt_tx_ring_info *txr;
4954 struct netdev_queue *txq;
4957 for (i = 0; i < bp->tx_nr_rings; i++) {
4958 txr = &bp->tx_ring[i];
4959 txq = netdev_get_tx_queue(bp->dev, i);
4960 txr->dev_state = BNXT_DEV_STATE_CLOSING;
4963 /* Stop all TX queues */
4964 netif_tx_disable(bp->dev);
4965 netif_carrier_off(bp->dev);
4968 static void bnxt_tx_enable(struct bnxt *bp)
4971 struct bnxt_tx_ring_info *txr;
4972 struct netdev_queue *txq;
4974 for (i = 0; i < bp->tx_nr_rings; i++) {
4975 txr = &bp->tx_ring[i];
4976 txq = netdev_get_tx_queue(bp->dev, i);
4979 netif_tx_wake_all_queues(bp->dev);
4980 if (bp->link_info.link_up)
4981 netif_carrier_on(bp->dev);
4984 static void bnxt_report_link(struct bnxt *bp)
4986 if (bp->link_info.link_up) {
4988 const char *flow_ctrl;
4991 netif_carrier_on(bp->dev);
4992 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
4996 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
4997 flow_ctrl = "ON - receive & transmit";
4998 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
4999 flow_ctrl = "ON - transmit";
5000 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5001 flow_ctrl = "ON - receive";
5004 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5005 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
5006 speed, duplex, flow_ctrl);
5007 if (bp->flags & BNXT_FLAG_EEE_CAP)
5008 netdev_info(bp->dev, "EEE is %s\n",
5009 bp->eee.eee_active ? "active" :
5012 netif_carrier_off(bp->dev);
5013 netdev_err(bp->dev, "NIC Link is Down\n");
5017 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5020 struct hwrm_port_phy_qcaps_input req = {0};
5021 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5022 struct bnxt_link_info *link_info = &bp->link_info;
5024 if (bp->hwrm_spec_code < 0x10201)
5027 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5029 mutex_lock(&bp->hwrm_cmd_lock);
5030 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5032 goto hwrm_phy_qcaps_exit;
5034 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5035 struct ethtool_eee *eee = &bp->eee;
5036 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5038 bp->flags |= BNXT_FLAG_EEE_CAP;
5039 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5040 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5041 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5042 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5043 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5045 link_info->support_auto_speeds =
5046 le16_to_cpu(resp->supported_speeds_auto_mode);
5048 hwrm_phy_qcaps_exit:
5049 mutex_unlock(&bp->hwrm_cmd_lock);
5053 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5056 struct bnxt_link_info *link_info = &bp->link_info;
5057 struct hwrm_port_phy_qcfg_input req = {0};
5058 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5059 u8 link_up = link_info->link_up;
5061 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5063 mutex_lock(&bp->hwrm_cmd_lock);
5064 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5066 mutex_unlock(&bp->hwrm_cmd_lock);
5070 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5071 link_info->phy_link_status = resp->link;
5072 link_info->duplex = resp->duplex;
5073 link_info->pause = resp->pause;
5074 link_info->auto_mode = resp->auto_mode;
5075 link_info->auto_pause_setting = resp->auto_pause;
5076 link_info->lp_pause = resp->link_partner_adv_pause;
5077 link_info->force_pause_setting = resp->force_pause;
5078 link_info->duplex_setting = resp->duplex;
5079 if (link_info->phy_link_status == BNXT_LINK_LINK)
5080 link_info->link_speed = le16_to_cpu(resp->link_speed);
5082 link_info->link_speed = 0;
5083 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
5084 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5085 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
5086 link_info->lp_auto_link_speeds =
5087 le16_to_cpu(resp->link_partner_adv_speeds);
5088 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5089 link_info->phy_ver[0] = resp->phy_maj;
5090 link_info->phy_ver[1] = resp->phy_min;
5091 link_info->phy_ver[2] = resp->phy_bld;
5092 link_info->media_type = resp->media_type;
5093 link_info->phy_type = resp->phy_type;
5094 link_info->transceiver = resp->xcvr_pkg_type;
5095 link_info->phy_addr = resp->eee_config_phy_addr &
5096 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
5097 link_info->module_status = resp->module_status;
5099 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5100 struct ethtool_eee *eee = &bp->eee;
5103 eee->eee_active = 0;
5104 if (resp->eee_config_phy_addr &
5105 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5106 eee->eee_active = 1;
5107 fw_speeds = le16_to_cpu(
5108 resp->link_partner_adv_eee_link_speed_mask);
5109 eee->lp_advertised =
5110 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5113 /* Pull initial EEE config */
5114 if (!chng_link_state) {
5115 if (resp->eee_config_phy_addr &
5116 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5117 eee->eee_enabled = 1;
5119 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5121 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5123 if (resp->eee_config_phy_addr &
5124 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5127 eee->tx_lpi_enabled = 1;
5128 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5129 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5130 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5134 /* TODO: need to add more logic to report VF link */
5135 if (chng_link_state) {
5136 if (link_info->phy_link_status == BNXT_LINK_LINK)
5137 link_info->link_up = 1;
5139 link_info->link_up = 0;
5140 if (link_up != link_info->link_up)
5141 bnxt_report_link(bp);
5143 /* alwasy link down if not require to update link state */
5144 link_info->link_up = 0;
5146 mutex_unlock(&bp->hwrm_cmd_lock);
5150 static void bnxt_get_port_module_status(struct bnxt *bp)
5152 struct bnxt_link_info *link_info = &bp->link_info;
5153 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5156 if (bnxt_update_link(bp, true))
5159 module_status = link_info->module_status;
5160 switch (module_status) {
5161 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5162 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5163 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5164 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5166 if (bp->hwrm_spec_code >= 0x10201) {
5167 netdev_warn(bp->dev, "Module part number %s\n",
5168 resp->phy_vendor_partnumber);
5170 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5171 netdev_warn(bp->dev, "TX is disabled\n");
5172 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5173 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5178 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5180 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
5181 if (bp->hwrm_spec_code >= 0x10201)
5183 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
5184 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5185 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5186 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5187 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
5189 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5191 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5192 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5193 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5194 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5196 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
5197 if (bp->hwrm_spec_code >= 0x10201) {
5198 req->auto_pause = req->force_pause;
5199 req->enables |= cpu_to_le32(
5200 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5205 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5206 struct hwrm_port_phy_cfg_input *req)
5208 u8 autoneg = bp->link_info.autoneg;
5209 u16 fw_link_speed = bp->link_info.req_link_speed;
5210 u32 advertising = bp->link_info.advertising;
5212 if (autoneg & BNXT_AUTONEG_SPEED) {
5214 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
5216 req->enables |= cpu_to_le32(
5217 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5218 req->auto_link_speed_mask = cpu_to_le16(advertising);
5220 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5222 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5224 req->force_link_speed = cpu_to_le16(fw_link_speed);
5225 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5228 /* tell chimp that the setting takes effect immediately */
5229 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5232 int bnxt_hwrm_set_pause(struct bnxt *bp)
5234 struct hwrm_port_phy_cfg_input req = {0};
5237 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5238 bnxt_hwrm_set_pause_common(bp, &req);
5240 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5241 bp->link_info.force_link_chng)
5242 bnxt_hwrm_set_link_common(bp, &req);
5244 mutex_lock(&bp->hwrm_cmd_lock);
5245 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5246 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5247 /* since changing of pause setting doesn't trigger any link
5248 * change event, the driver needs to update the current pause
5249 * result upon successfully return of the phy_cfg command
5251 bp->link_info.pause =
5252 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5253 bp->link_info.auto_pause_setting = 0;
5254 if (!bp->link_info.force_link_chng)
5255 bnxt_report_link(bp);
5257 bp->link_info.force_link_chng = false;
5258 mutex_unlock(&bp->hwrm_cmd_lock);
5262 static void bnxt_hwrm_set_eee(struct bnxt *bp,
5263 struct hwrm_port_phy_cfg_input *req)
5265 struct ethtool_eee *eee = &bp->eee;
5267 if (eee->eee_enabled) {
5269 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5271 if (eee->tx_lpi_enabled)
5272 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5274 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5276 req->flags |= cpu_to_le32(flags);
5277 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5278 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5279 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5281 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5285 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
5287 struct hwrm_port_phy_cfg_input req = {0};
5289 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5291 bnxt_hwrm_set_pause_common(bp, &req);
5293 bnxt_hwrm_set_link_common(bp, &req);
5296 bnxt_hwrm_set_eee(bp, &req);
5297 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5300 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5302 struct hwrm_port_phy_cfg_input req = {0};
5304 if (!BNXT_SINGLE_PF(bp))
5307 if (pci_num_vf(bp->pdev))
5310 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5311 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DOWN);
5312 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5315 static bool bnxt_eee_config_ok(struct bnxt *bp)
5317 struct ethtool_eee *eee = &bp->eee;
5318 struct bnxt_link_info *link_info = &bp->link_info;
5320 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5323 if (eee->eee_enabled) {
5325 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5327 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5328 eee->eee_enabled = 0;
5331 if (eee->advertised & ~advertising) {
5332 eee->advertised = advertising & eee->supported;
5339 static int bnxt_update_phy_setting(struct bnxt *bp)
5342 bool update_link = false;
5343 bool update_pause = false;
5344 bool update_eee = false;
5345 struct bnxt_link_info *link_info = &bp->link_info;
5347 rc = bnxt_update_link(bp, true);
5349 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5353 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5354 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5355 link_info->req_flow_ctrl)
5356 update_pause = true;
5357 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5358 link_info->force_pause_setting != link_info->req_flow_ctrl)
5359 update_pause = true;
5360 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5361 if (BNXT_AUTO_MODE(link_info->auto_mode))
5363 if (link_info->req_link_speed != link_info->force_link_speed)
5365 if (link_info->req_duplex != link_info->duplex_setting)
5368 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5370 if (link_info->advertising != link_info->auto_link_speeds)
5374 if (!bnxt_eee_config_ok(bp))
5378 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
5379 else if (update_pause)
5380 rc = bnxt_hwrm_set_pause(bp);
5382 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5390 /* Common routine to pre-map certain register block to different GRC window.
5391 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5392 * in PF and 3 windows in VF that can be customized to map in different
5395 static void bnxt_preset_reg_win(struct bnxt *bp)
5398 /* CAG registers map to GRC window #4 */
5399 writel(BNXT_CAG_REG_BASE,
5400 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5404 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5408 bnxt_preset_reg_win(bp);
5409 netif_carrier_off(bp->dev);
5411 rc = bnxt_setup_int_mode(bp);
5413 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5418 if ((bp->flags & BNXT_FLAG_RFS) &&
5419 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5420 /* disable RFS if falling back to INTA */
5421 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5422 bp->flags &= ~BNXT_FLAG_RFS;
5425 rc = bnxt_alloc_mem(bp, irq_re_init);
5427 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5428 goto open_err_free_mem;
5433 rc = bnxt_request_irq(bp);
5435 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5440 bnxt_enable_napi(bp);
5442 rc = bnxt_init_nic(bp, irq_re_init);
5444 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5449 rc = bnxt_update_phy_setting(bp);
5451 netdev_warn(bp->dev, "failed to update phy settings\n");
5455 udp_tunnel_get_rx_info(bp->dev);
5457 set_bit(BNXT_STATE_OPEN, &bp->state);
5458 bnxt_enable_int(bp);
5459 /* Enable TX queues */
5461 mod_timer(&bp->timer, jiffies + bp->current_interval);
5462 /* Poll link status and check for SFP+ module status */
5463 bnxt_get_port_module_status(bp);
5468 bnxt_disable_napi(bp);
5474 bnxt_free_mem(bp, true);
5478 /* rtnl_lock held */
5479 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5483 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5485 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5491 static int bnxt_open(struct net_device *dev)
5493 struct bnxt *bp = netdev_priv(dev);
5496 if (!test_bit(BNXT_STATE_FN_RST_DONE, &bp->state)) {
5497 rc = bnxt_hwrm_func_reset(bp);
5499 netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
5504 /* Do func_reset during the 1st PF open only to prevent killing
5505 * the VFs when the PF is brought down and up.
5508 set_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
5510 return __bnxt_open_nic(bp, true, true);
5513 static void bnxt_disable_int_sync(struct bnxt *bp)
5517 atomic_inc(&bp->intr_sem);
5518 if (!netif_running(bp->dev))
5521 bnxt_disable_int(bp);
5522 for (i = 0; i < bp->cp_nr_rings; i++)
5523 synchronize_irq(bp->irq_tbl[i].vector);
5526 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5530 #ifdef CONFIG_BNXT_SRIOV
5531 if (bp->sriov_cfg) {
5532 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5534 BNXT_SRIOV_CFG_WAIT_TMO);
5536 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5539 /* Change device state to avoid TX queue wake up's */
5540 bnxt_tx_disable(bp);
5542 clear_bit(BNXT_STATE_OPEN, &bp->state);
5543 smp_mb__after_atomic();
5544 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5547 /* Flush rings before disabling interrupts */
5548 bnxt_shutdown_nic(bp, irq_re_init);
5550 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5552 bnxt_disable_napi(bp);
5553 bnxt_disable_int_sync(bp);
5554 del_timer_sync(&bp->timer);
5561 bnxt_free_mem(bp, irq_re_init);
5565 static int bnxt_close(struct net_device *dev)
5567 struct bnxt *bp = netdev_priv(dev);
5569 bnxt_close_nic(bp, true, true);
5570 bnxt_hwrm_shutdown_link(bp);
5574 /* rtnl_lock held */
5575 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5581 if (!netif_running(dev))
5588 if (!netif_running(dev))
5600 static struct rtnl_link_stats64 *
5601 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5604 struct bnxt *bp = netdev_priv(dev);
5606 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5611 /* TODO check if we need to synchronize with bnxt_close path */
5612 for (i = 0; i < bp->cp_nr_rings; i++) {
5613 struct bnxt_napi *bnapi = bp->bnapi[i];
5614 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5615 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5617 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5618 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5619 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5621 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5622 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5623 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5625 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5626 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5627 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5629 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5630 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5631 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5633 stats->rx_missed_errors +=
5634 le64_to_cpu(hw_stats->rx_discard_pkts);
5636 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5638 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5641 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5642 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5643 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5645 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5646 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5647 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5648 le64_to_cpu(rx->rx_ovrsz_frames) +
5649 le64_to_cpu(rx->rx_runt_frames);
5650 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5651 le64_to_cpu(rx->rx_jbr_frames);
5652 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5653 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5654 stats->tx_errors = le64_to_cpu(tx->tx_err);
5660 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5662 struct net_device *dev = bp->dev;
5663 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5664 struct netdev_hw_addr *ha;
5667 bool update = false;
5670 netdev_for_each_mc_addr(ha, dev) {
5671 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5672 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5673 vnic->mc_list_count = 0;
5677 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5678 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5685 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5687 if (mc_count != vnic->mc_list_count) {
5688 vnic->mc_list_count = mc_count;
5694 static bool bnxt_uc_list_updated(struct bnxt *bp)
5696 struct net_device *dev = bp->dev;
5697 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5698 struct netdev_hw_addr *ha;
5701 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5704 netdev_for_each_uc_addr(ha, dev) {
5705 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5713 static void bnxt_set_rx_mode(struct net_device *dev)
5715 struct bnxt *bp = netdev_priv(dev);
5716 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5717 u32 mask = vnic->rx_mask;
5718 bool mc_update = false;
5721 if (!netif_running(dev))
5724 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5725 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5726 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5728 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
5729 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5731 uc_update = bnxt_uc_list_updated(bp);
5733 if (dev->flags & IFF_ALLMULTI) {
5734 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5735 vnic->mc_list_count = 0;
5737 mc_update = bnxt_mc_list_updated(bp, &mask);
5740 if (mask != vnic->rx_mask || uc_update || mc_update) {
5741 vnic->rx_mask = mask;
5743 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5744 schedule_work(&bp->sp_task);
5748 static int bnxt_cfg_rx_mode(struct bnxt *bp)
5750 struct net_device *dev = bp->dev;
5751 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5752 struct netdev_hw_addr *ha;
5756 netif_addr_lock_bh(dev);
5757 uc_update = bnxt_uc_list_updated(bp);
5758 netif_addr_unlock_bh(dev);
5763 mutex_lock(&bp->hwrm_cmd_lock);
5764 for (i = 1; i < vnic->uc_filter_count; i++) {
5765 struct hwrm_cfa_l2_filter_free_input req = {0};
5767 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5770 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5772 rc = _hwrm_send_message(bp, &req, sizeof(req),
5775 mutex_unlock(&bp->hwrm_cmd_lock);
5777 vnic->uc_filter_count = 1;
5779 netif_addr_lock_bh(dev);
5780 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5781 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5783 netdev_for_each_uc_addr(ha, dev) {
5784 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5786 vnic->uc_filter_count++;
5789 netif_addr_unlock_bh(dev);
5791 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5792 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5794 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5796 vnic->uc_filter_count = i;
5802 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5804 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5810 static bool bnxt_rfs_capable(struct bnxt *bp)
5812 #ifdef CONFIG_RFS_ACCEL
5813 struct bnxt_pf_info *pf = &bp->pf;
5816 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5819 vnics = 1 + bp->rx_nr_rings;
5820 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics) {
5821 netdev_warn(bp->dev,
5822 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
5823 min(pf->max_rsscos_ctxs - 1, pf->max_vnics - 1));
5833 static netdev_features_t bnxt_fix_features(struct net_device *dev,
5834 netdev_features_t features)
5836 struct bnxt *bp = netdev_priv(dev);
5838 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
5839 features &= ~NETIF_F_NTUPLE;
5841 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5842 * turned on or off together.
5844 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
5845 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
5846 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
5847 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5848 NETIF_F_HW_VLAN_STAG_RX);
5850 features |= NETIF_F_HW_VLAN_CTAG_RX |
5851 NETIF_F_HW_VLAN_STAG_RX;
5853 #ifdef CONFIG_BNXT_SRIOV
5856 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5857 NETIF_F_HW_VLAN_STAG_RX);
5864 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
5866 struct bnxt *bp = netdev_priv(dev);
5867 u32 flags = bp->flags;
5870 bool re_init = false;
5871 bool update_tpa = false;
5873 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
5874 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
5875 flags |= BNXT_FLAG_GRO;
5876 if (features & NETIF_F_LRO)
5877 flags |= BNXT_FLAG_LRO;
5879 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5880 flags |= BNXT_FLAG_STRIP_VLAN;
5882 if (features & NETIF_F_NTUPLE)
5883 flags |= BNXT_FLAG_RFS;
5885 changes = flags ^ bp->flags;
5886 if (changes & BNXT_FLAG_TPA) {
5888 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
5889 (flags & BNXT_FLAG_TPA) == 0)
5893 if (changes & ~BNXT_FLAG_TPA)
5896 if (flags != bp->flags) {
5897 u32 old_flags = bp->flags;
5901 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
5903 bnxt_set_ring_params(bp);
5908 bnxt_close_nic(bp, false, false);
5910 bnxt_set_ring_params(bp);
5912 return bnxt_open_nic(bp, false, false);
5915 rc = bnxt_set_tpa(bp,
5916 (flags & BNXT_FLAG_TPA) ?
5919 bp->flags = old_flags;
5925 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
5927 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
5928 int i = bnapi->index;
5933 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
5934 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
5938 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
5940 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
5941 int i = bnapi->index;
5946 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",
5947 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
5948 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
5949 rxr->rx_sw_agg_prod);
5952 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
5954 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5955 int i = bnapi->index;
5957 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
5958 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
5961 static void bnxt_dbg_dump_states(struct bnxt *bp)
5964 struct bnxt_napi *bnapi;
5966 for (i = 0; i < bp->cp_nr_rings; i++) {
5967 bnapi = bp->bnapi[i];
5968 if (netif_msg_drv(bp)) {
5969 bnxt_dump_tx_sw_state(bnapi);
5970 bnxt_dump_rx_sw_state(bnapi);
5971 bnxt_dump_cp_sw_state(bnapi);
5976 static void bnxt_reset_task(struct bnxt *bp, bool silent)
5979 bnxt_dbg_dump_states(bp);
5980 if (netif_running(bp->dev)) {
5981 bnxt_close_nic(bp, false, false);
5982 bnxt_open_nic(bp, false, false);
5986 static void bnxt_tx_timeout(struct net_device *dev)
5988 struct bnxt *bp = netdev_priv(dev);
5990 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
5991 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
5992 schedule_work(&bp->sp_task);
5995 #ifdef CONFIG_NET_POLL_CONTROLLER
5996 static void bnxt_poll_controller(struct net_device *dev)
5998 struct bnxt *bp = netdev_priv(dev);
6001 for (i = 0; i < bp->cp_nr_rings; i++) {
6002 struct bnxt_irq *irq = &bp->irq_tbl[i];
6004 disable_irq(irq->vector);
6005 irq->handler(irq->vector, bp->bnapi[i]);
6006 enable_irq(irq->vector);
6011 static void bnxt_timer(unsigned long data)
6013 struct bnxt *bp = (struct bnxt *)data;
6014 struct net_device *dev = bp->dev;
6016 if (!netif_running(dev))
6019 if (atomic_read(&bp->intr_sem) != 0)
6020 goto bnxt_restart_timer;
6022 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6023 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6024 schedule_work(&bp->sp_task);
6027 mod_timer(&bp->timer, jiffies + bp->current_interval);
6030 /* Only called from bnxt_sp_task() */
6031 static void bnxt_reset(struct bnxt *bp, bool silent)
6033 /* bnxt_reset_task() calls bnxt_close_nic() which waits
6034 * for BNXT_STATE_IN_SP_TASK to clear.
6035 * If there is a parallel dev_close(), bnxt_close() may be holding
6036 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6037 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6039 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6041 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6042 bnxt_reset_task(bp, silent);
6043 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6047 static void bnxt_cfg_ntp_filters(struct bnxt *);
6049 static void bnxt_sp_task(struct work_struct *work)
6051 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6054 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6055 smp_mb__after_atomic();
6056 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6057 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6061 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6062 bnxt_cfg_rx_mode(bp);
6064 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6065 bnxt_cfg_ntp_filters(bp);
6066 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6067 rc = bnxt_update_link(bp, true);
6069 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6072 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6073 bnxt_hwrm_exec_fwd_req(bp);
6074 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6075 bnxt_hwrm_tunnel_dst_port_alloc(
6077 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6079 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6080 bnxt_hwrm_tunnel_dst_port_free(
6081 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6083 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6084 bnxt_hwrm_tunnel_dst_port_alloc(
6086 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6088 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6089 bnxt_hwrm_tunnel_dst_port_free(
6090 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6092 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6093 bnxt_reset(bp, false);
6095 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6096 bnxt_reset(bp, true);
6098 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
6099 bnxt_get_port_module_status(bp);
6101 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6102 bnxt_hwrm_port_qstats(bp);
6104 smp_mb__before_atomic();
6105 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6108 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6111 struct bnxt *bp = netdev_priv(dev);
6113 SET_NETDEV_DEV(dev, &pdev->dev);
6115 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6116 rc = pci_enable_device(pdev);
6118 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6122 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6124 "Cannot find PCI device base address, aborting\n");
6126 goto init_err_disable;
6129 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6131 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6132 goto init_err_disable;
6135 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6136 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6137 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6138 goto init_err_disable;
6141 pci_set_master(pdev);
6146 bp->bar0 = pci_ioremap_bar(pdev, 0);
6148 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6150 goto init_err_release;
6153 bp->bar1 = pci_ioremap_bar(pdev, 2);
6155 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6157 goto init_err_release;
6160 bp->bar2 = pci_ioremap_bar(pdev, 4);
6162 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6164 goto init_err_release;
6167 pci_enable_pcie_error_reporting(pdev);
6169 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6171 spin_lock_init(&bp->ntp_fltr_lock);
6173 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6174 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6176 /* tick values in micro seconds */
6177 bp->rx_coal_ticks = 12;
6178 bp->rx_coal_bufs = 30;
6179 bp->rx_coal_ticks_irq = 1;
6180 bp->rx_coal_bufs_irq = 2;
6182 bp->tx_coal_ticks = 25;
6183 bp->tx_coal_bufs = 30;
6184 bp->tx_coal_ticks_irq = 2;
6185 bp->tx_coal_bufs_irq = 2;
6187 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6189 init_timer(&bp->timer);
6190 bp->timer.data = (unsigned long)bp;
6191 bp->timer.function = bnxt_timer;
6192 bp->current_interval = BNXT_TIMER_INTERVAL;
6194 clear_bit(BNXT_STATE_OPEN, &bp->state);
6200 pci_iounmap(pdev, bp->bar2);
6205 pci_iounmap(pdev, bp->bar1);
6210 pci_iounmap(pdev, bp->bar0);
6214 pci_release_regions(pdev);
6217 pci_disable_device(pdev);
6223 /* rtnl_lock held */
6224 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6226 struct sockaddr *addr = p;
6227 struct bnxt *bp = netdev_priv(dev);
6230 if (!is_valid_ether_addr(addr->sa_data))
6231 return -EADDRNOTAVAIL;
6233 rc = bnxt_approve_mac(bp, addr->sa_data);
6237 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6240 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6241 if (netif_running(dev)) {
6242 bnxt_close_nic(bp, false, false);
6243 rc = bnxt_open_nic(bp, false, false);
6249 /* rtnl_lock held */
6250 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6252 struct bnxt *bp = netdev_priv(dev);
6254 if (new_mtu < 60 || new_mtu > 9500)
6257 if (netif_running(dev))
6258 bnxt_close_nic(bp, false, false);
6261 bnxt_set_ring_params(bp);
6263 if (netif_running(dev))
6264 return bnxt_open_nic(bp, false, false);
6269 static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6270 struct tc_to_netdev *ntc)
6272 struct bnxt *bp = netdev_priv(dev);
6275 if (ntc->type != TC_SETUP_MQPRIO)
6280 if (tc > bp->max_tc) {
6281 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6286 if (netdev_get_num_tc(dev) == tc)
6290 int max_rx_rings, max_tx_rings, rc;
6293 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6296 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
6297 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
6301 /* Needs to close the device and do hw resource re-allocations */
6302 if (netif_running(bp->dev))
6303 bnxt_close_nic(bp, true, false);
6306 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6307 netdev_set_num_tc(dev, tc);
6309 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6310 netdev_reset_tc(dev);
6312 bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
6313 bp->num_stat_ctxs = bp->cp_nr_rings;
6315 if (netif_running(bp->dev))
6316 return bnxt_open_nic(bp, true, false);
6321 #ifdef CONFIG_RFS_ACCEL
6322 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6323 struct bnxt_ntuple_filter *f2)
6325 struct flow_keys *keys1 = &f1->fkeys;
6326 struct flow_keys *keys2 = &f2->fkeys;
6328 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6329 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6330 keys1->ports.ports == keys2->ports.ports &&
6331 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6332 keys1->basic.n_proto == keys2->basic.n_proto &&
6333 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
6334 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
6340 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6341 u16 rxq_index, u32 flow_id)
6343 struct bnxt *bp = netdev_priv(dev);
6344 struct bnxt_ntuple_filter *fltr, *new_fltr;
6345 struct flow_keys *fkeys;
6346 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
6347 int rc = 0, idx, bit_id, l2_idx = 0;
6348 struct hlist_head *head;
6350 if (skb->encapsulation)
6351 return -EPROTONOSUPPORT;
6353 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
6354 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6357 netif_addr_lock_bh(dev);
6358 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
6359 if (ether_addr_equal(eth->h_dest,
6360 vnic->uc_list + off)) {
6365 netif_addr_unlock_bh(dev);
6369 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6373 fkeys = &new_fltr->fkeys;
6374 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6375 rc = -EPROTONOSUPPORT;
6379 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6380 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6381 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6382 rc = -EPROTONOSUPPORT;
6386 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
6387 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6389 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6390 head = &bp->ntp_fltr_hash_tbl[idx];
6392 hlist_for_each_entry_rcu(fltr, head, hash) {
6393 if (bnxt_fltr_match(fltr, new_fltr)) {
6401 spin_lock_bh(&bp->ntp_fltr_lock);
6402 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6403 BNXT_NTP_FLTR_MAX_FLTR, 0);
6405 spin_unlock_bh(&bp->ntp_fltr_lock);
6410 new_fltr->sw_id = (u16)bit_id;
6411 new_fltr->flow_id = flow_id;
6412 new_fltr->l2_fltr_idx = l2_idx;
6413 new_fltr->rxq = rxq_index;
6414 hlist_add_head_rcu(&new_fltr->hash, head);
6415 bp->ntp_fltr_count++;
6416 spin_unlock_bh(&bp->ntp_fltr_lock);
6418 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6419 schedule_work(&bp->sp_task);
6421 return new_fltr->sw_id;
6428 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6432 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6433 struct hlist_head *head;
6434 struct hlist_node *tmp;
6435 struct bnxt_ntuple_filter *fltr;
6438 head = &bp->ntp_fltr_hash_tbl[i];
6439 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6442 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6443 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6446 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6451 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6456 set_bit(BNXT_FLTR_VALID, &fltr->state);
6460 spin_lock_bh(&bp->ntp_fltr_lock);
6461 hlist_del_rcu(&fltr->hash);
6462 bp->ntp_fltr_count--;
6463 spin_unlock_bh(&bp->ntp_fltr_lock);
6465 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6470 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6471 netdev_info(bp->dev, "Receive PF driver unload event!");
6476 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6480 #endif /* CONFIG_RFS_ACCEL */
6482 static void bnxt_udp_tunnel_add(struct net_device *dev,
6483 struct udp_tunnel_info *ti)
6485 struct bnxt *bp = netdev_priv(dev);
6487 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6490 if (!netif_running(dev))
6494 case UDP_TUNNEL_TYPE_VXLAN:
6495 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6498 bp->vxlan_port_cnt++;
6499 if (bp->vxlan_port_cnt == 1) {
6500 bp->vxlan_port = ti->port;
6501 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
6502 schedule_work(&bp->sp_task);
6505 case UDP_TUNNEL_TYPE_GENEVE:
6506 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6510 if (bp->nge_port_cnt == 1) {
6511 bp->nge_port = ti->port;
6512 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6519 schedule_work(&bp->sp_task);
6522 static void bnxt_udp_tunnel_del(struct net_device *dev,
6523 struct udp_tunnel_info *ti)
6525 struct bnxt *bp = netdev_priv(dev);
6527 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6530 if (!netif_running(dev))
6534 case UDP_TUNNEL_TYPE_VXLAN:
6535 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6537 bp->vxlan_port_cnt--;
6539 if (bp->vxlan_port_cnt != 0)
6542 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6544 case UDP_TUNNEL_TYPE_GENEVE:
6545 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6549 if (bp->nge_port_cnt != 0)
6552 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6558 schedule_work(&bp->sp_task);
6561 static const struct net_device_ops bnxt_netdev_ops = {
6562 .ndo_open = bnxt_open,
6563 .ndo_start_xmit = bnxt_start_xmit,
6564 .ndo_stop = bnxt_close,
6565 .ndo_get_stats64 = bnxt_get_stats64,
6566 .ndo_set_rx_mode = bnxt_set_rx_mode,
6567 .ndo_do_ioctl = bnxt_ioctl,
6568 .ndo_validate_addr = eth_validate_addr,
6569 .ndo_set_mac_address = bnxt_change_mac_addr,
6570 .ndo_change_mtu = bnxt_change_mtu,
6571 .ndo_fix_features = bnxt_fix_features,
6572 .ndo_set_features = bnxt_set_features,
6573 .ndo_tx_timeout = bnxt_tx_timeout,
6574 #ifdef CONFIG_BNXT_SRIOV
6575 .ndo_get_vf_config = bnxt_get_vf_config,
6576 .ndo_set_vf_mac = bnxt_set_vf_mac,
6577 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6578 .ndo_set_vf_rate = bnxt_set_vf_bw,
6579 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6580 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6582 #ifdef CONFIG_NET_POLL_CONTROLLER
6583 .ndo_poll_controller = bnxt_poll_controller,
6585 .ndo_setup_tc = bnxt_setup_tc,
6586 #ifdef CONFIG_RFS_ACCEL
6587 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6589 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6590 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
6591 #ifdef CONFIG_NET_RX_BUSY_POLL
6592 .ndo_busy_poll = bnxt_busy_poll,
6596 static void bnxt_remove_one(struct pci_dev *pdev)
6598 struct net_device *dev = pci_get_drvdata(pdev);
6599 struct bnxt *bp = netdev_priv(dev);
6602 bnxt_sriov_disable(bp);
6604 pci_disable_pcie_error_reporting(pdev);
6605 unregister_netdev(dev);
6606 cancel_work_sync(&bp->sp_task);
6609 bnxt_hwrm_func_drv_unrgtr(bp);
6610 bnxt_free_hwrm_resources(bp);
6611 pci_iounmap(pdev, bp->bar2);
6612 pci_iounmap(pdev, bp->bar1);
6613 pci_iounmap(pdev, bp->bar0);
6616 pci_release_regions(pdev);
6617 pci_disable_device(pdev);
6620 static int bnxt_probe_phy(struct bnxt *bp)
6623 struct bnxt_link_info *link_info = &bp->link_info;
6625 rc = bnxt_hwrm_phy_qcaps(bp);
6627 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6632 rc = bnxt_update_link(bp, false);
6634 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6639 /* Older firmware does not have supported_auto_speeds, so assume
6640 * that all supported speeds can be autonegotiated.
6642 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6643 link_info->support_auto_speeds = link_info->support_speeds;
6645 /*initialize the ethool setting copy with NVM settings */
6646 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
6647 link_info->autoneg = BNXT_AUTONEG_SPEED;
6648 if (bp->hwrm_spec_code >= 0x10201) {
6649 if (link_info->auto_pause_setting &
6650 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6651 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6653 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6655 link_info->advertising = link_info->auto_link_speeds;
6657 link_info->req_link_speed = link_info->force_link_speed;
6658 link_info->req_duplex = link_info->duplex_setting;
6660 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6661 link_info->req_flow_ctrl =
6662 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6664 link_info->req_flow_ctrl = link_info->force_pause_setting;
6668 static int bnxt_get_max_irq(struct pci_dev *pdev)
6672 if (!pdev->msix_cap)
6675 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6676 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6679 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6682 int max_ring_grps = 0;
6684 #ifdef CONFIG_BNXT_SRIOV
6686 *max_tx = bp->vf.max_tx_rings;
6687 *max_rx = bp->vf.max_rx_rings;
6688 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6689 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
6690 max_ring_grps = bp->vf.max_hw_ring_grps;
6694 *max_tx = bp->pf.max_tx_rings;
6695 *max_rx = bp->pf.max_rx_rings;
6696 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6697 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6698 max_ring_grps = bp->pf.max_hw_ring_grps;
6700 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
6704 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6706 *max_rx = min_t(int, *max_rx, max_ring_grps);
6709 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6713 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6714 if (!rx || !tx || !cp)
6719 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6722 static int bnxt_set_dflt_rings(struct bnxt *bp)
6724 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6728 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6729 dflt_rings = netif_get_num_default_rss_queues();
6730 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
6733 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6734 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6735 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6736 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6737 bp->tx_nr_rings + bp->rx_nr_rings;
6738 bp->num_stat_ctxs = bp->cp_nr_rings;
6739 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6746 static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6748 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6749 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6751 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6752 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6753 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6755 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6756 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6757 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6758 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6762 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6764 static int version_printed;
6765 struct net_device *dev;
6769 if (pdev->device == 0x16cd && pci_is_bridge(pdev))
6772 if (version_printed++ == 0)
6773 pr_info("%s", version);
6775 max_irqs = bnxt_get_max_irq(pdev);
6776 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6780 bp = netdev_priv(dev);
6782 if (bnxt_vf_pciid(ent->driver_data))
6783 bp->flags |= BNXT_FLAG_VF;
6786 bp->flags |= BNXT_FLAG_MSIX_CAP;
6788 rc = bnxt_init_board(pdev, dev);
6792 dev->netdev_ops = &bnxt_netdev_ops;
6793 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6794 dev->ethtool_ops = &bnxt_ethtool_ops;
6796 pci_set_drvdata(pdev, dev);
6798 rc = bnxt_alloc_hwrm_resources(bp);
6802 mutex_init(&bp->hwrm_cmd_lock);
6803 rc = bnxt_hwrm_ver_get(bp);
6807 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6808 NETIF_F_TSO | NETIF_F_TSO6 |
6809 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
6810 NETIF_F_GSO_IPXIP4 |
6811 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6812 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
6813 NETIF_F_RXCSUM | NETIF_F_GRO;
6815 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6816 dev->hw_features |= NETIF_F_LRO;
6818 dev->hw_enc_features =
6819 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6820 NETIF_F_TSO | NETIF_F_TSO6 |
6821 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
6822 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6823 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
6824 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
6825 NETIF_F_GSO_GRE_CSUM;
6826 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
6827 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
6828 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
6829 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
6830 dev->priv_flags |= IFF_UNICAST_FLT;
6832 #ifdef CONFIG_BNXT_SRIOV
6833 init_waitqueue_head(&bp->sriov_cfg_wait);
6835 bp->gro_func = bnxt_gro_func_5730x;
6836 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
6837 bp->gro_func = bnxt_gro_func_5731x;
6839 rc = bnxt_hwrm_func_drv_rgtr(bp);
6843 /* Get the MAX capabilities for this function */
6844 rc = bnxt_hwrm_func_qcaps(bp);
6846 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
6852 rc = bnxt_hwrm_queue_qportcfg(bp);
6854 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
6860 bnxt_hwrm_func_qcfg(bp);
6862 bnxt_set_tpa_flags(bp);
6863 bnxt_set_ring_params(bp);
6865 bp->pf.max_irqs = max_irqs;
6866 #if defined(CONFIG_BNXT_SRIOV)
6868 bp->vf.max_irqs = max_irqs;
6870 bnxt_set_dflt_rings(bp);
6872 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6873 dev->hw_features |= NETIF_F_NTUPLE;
6874 if (bnxt_rfs_capable(bp)) {
6875 bp->flags |= BNXT_FLAG_RFS;
6876 dev->features |= NETIF_F_NTUPLE;
6880 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
6881 bp->flags |= BNXT_FLAG_STRIP_VLAN;
6883 rc = bnxt_probe_phy(bp);
6887 rc = register_netdev(dev);
6891 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
6892 board_info[ent->driver_data].name,
6893 (long)pci_resource_start(pdev, 0), dev->dev_addr);
6895 bnxt_parse_log_pcie_link(bp);
6900 pci_iounmap(pdev, bp->bar0);
6901 pci_release_regions(pdev);
6902 pci_disable_device(pdev);
6910 * bnxt_io_error_detected - called when PCI error is detected
6911 * @pdev: Pointer to PCI device
6912 * @state: The current pci connection state
6914 * This function is called after a PCI bus error affecting
6915 * this device has been detected.
6917 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
6918 pci_channel_state_t state)
6920 struct net_device *netdev = pci_get_drvdata(pdev);
6921 struct bnxt *bp = netdev_priv(netdev);
6923 netdev_info(netdev, "PCI I/O error detected\n");
6926 netif_device_detach(netdev);
6928 if (state == pci_channel_io_perm_failure) {
6930 return PCI_ERS_RESULT_DISCONNECT;
6933 if (netif_running(netdev))
6936 /* So that func_reset will be done during slot_reset */
6937 clear_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
6938 pci_disable_device(pdev);
6941 /* Request a slot slot reset. */
6942 return PCI_ERS_RESULT_NEED_RESET;
6946 * bnxt_io_slot_reset - called after the pci bus has been reset.
6947 * @pdev: Pointer to PCI device
6949 * Restart the card from scratch, as if from a cold-boot.
6950 * At this point, the card has exprienced a hard reset,
6951 * followed by fixups by BIOS, and has its config space
6952 * set up identically to what it was at cold boot.
6954 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
6956 struct net_device *netdev = pci_get_drvdata(pdev);
6957 struct bnxt *bp = netdev_priv(netdev);
6959 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
6961 netdev_info(bp->dev, "PCI Slot Reset\n");
6965 if (pci_enable_device(pdev)) {
6967 "Cannot re-enable PCI device after reset.\n");
6969 pci_set_master(pdev);
6971 if (netif_running(netdev))
6972 err = bnxt_open(netdev);
6975 result = PCI_ERS_RESULT_RECOVERED;
6978 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
6983 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6986 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
6987 err); /* non-fatal, continue */
6990 return PCI_ERS_RESULT_RECOVERED;
6994 * bnxt_io_resume - called when traffic can start flowing again.
6995 * @pdev: Pointer to PCI device
6997 * This callback is called when the error recovery driver tells
6998 * us that its OK to resume normal operation.
7000 static void bnxt_io_resume(struct pci_dev *pdev)
7002 struct net_device *netdev = pci_get_drvdata(pdev);
7006 netif_device_attach(netdev);
7011 static const struct pci_error_handlers bnxt_err_handler = {
7012 .error_detected = bnxt_io_error_detected,
7013 .slot_reset = bnxt_io_slot_reset,
7014 .resume = bnxt_io_resume
7017 static struct pci_driver bnxt_pci_driver = {
7018 .name = DRV_MODULE_NAME,
7019 .id_table = bnxt_pci_tbl,
7020 .probe = bnxt_init_one,
7021 .remove = bnxt_remove_one,
7022 .err_handler = &bnxt_err_handler,
7023 #if defined(CONFIG_BNXT_SRIOV)
7024 .sriov_configure = bnxt_sriov_configure,
7028 module_pci_driver(bnxt_pci_driver);