1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2014-2016 Broadcom Corporation
4 * Copyright (c) 2016-2017 Broadcom Limited
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
11 #include <linux/module.h>
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
35 #include <linux/if_vlan.h>
36 #include <linux/rtc.h>
37 #include <linux/bpf.h>
41 #include <net/checksum.h>
42 #include <net/ip6_checksum.h>
43 #include <net/udp_tunnel.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>
55 #include "bnxt_sriov.h"
56 #include "bnxt_ethtool.h"
60 #define BNXT_TX_TIMEOUT (5 * HZ)
62 static const char version[] =
63 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
65 MODULE_LICENSE("GPL");
66 MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
67 MODULE_VERSION(DRV_MODULE_VERSION);
69 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
70 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
71 #define BNXT_RX_COPY_THRESH 256
73 #define BNXT_TX_PUSH_THRESH 164
108 /* indexed by enum above */
109 static const struct {
112 { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
113 { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
114 { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
115 { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
116 { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
117 { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
118 { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
119 { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
120 { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
121 { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
122 { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
123 { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
124 { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
125 { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
126 { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
127 { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
128 { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
129 { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
130 { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
131 { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
132 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
133 { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
134 { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
135 { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
136 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
137 { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
138 { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
139 { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
140 { "Broadcom NetXtreme-E Ethernet Virtual Function" },
141 { "Broadcom NetXtreme-C Ethernet Virtual Function" },
144 static const struct pci_device_id bnxt_pci_tbl[] = {
145 { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
146 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
147 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
148 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
149 { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
150 { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
151 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
152 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
153 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
154 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
155 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
156 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
157 { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
158 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
159 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
160 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
161 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
162 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
163 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
164 { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
165 { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
166 { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
167 { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
168 { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
169 { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
170 { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
171 { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
172 { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
173 { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
174 { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
175 { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
176 { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
177 #ifdef CONFIG_BNXT_SRIOV
178 { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
179 { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
180 { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
181 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
182 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
183 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
184 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
185 { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
190 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
192 static const u16 bnxt_vf_req_snif[] = {
195 HWRM_CFA_L2_FILTER_ALLOC,
198 static const u16 bnxt_async_events_arr[] = {
199 ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
200 ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
201 ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
202 ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
203 ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
206 static bool bnxt_vf_pciid(enum board_idx idx)
208 return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
211 #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
212 #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
213 #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
215 #define BNXT_CP_DB_REARM(db, raw_cons) \
216 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
218 #define BNXT_CP_DB(db, raw_cons) \
219 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
221 #define BNXT_CP_DB_IRQ_DIS(db) \
222 writel(DB_CP_IRQ_DIS_FLAGS, db)
224 const u16 bnxt_lhint_arr[] = {
225 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
226 TX_BD_FLAGS_LHINT_512_TO_1023,
227 TX_BD_FLAGS_LHINT_1024_TO_2047,
228 TX_BD_FLAGS_LHINT_1024_TO_2047,
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,
238 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
239 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
240 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
241 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
242 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
243 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
246 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
248 struct bnxt *bp = netdev_priv(dev);
250 struct tx_bd_ext *txbd1;
251 struct netdev_queue *txq;
254 unsigned int length, pad = 0;
255 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
257 struct pci_dev *pdev = bp->pdev;
258 struct bnxt_tx_ring_info *txr;
259 struct bnxt_sw_tx_bd *tx_buf;
261 i = skb_get_queue_mapping(skb);
262 if (unlikely(i >= bp->tx_nr_rings)) {
263 dev_kfree_skb_any(skb);
267 txq = netdev_get_tx_queue(dev, i);
268 txr = &bp->tx_ring[bp->tx_ring_map[i]];
271 free_size = bnxt_tx_avail(bp, txr);
272 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
273 netif_tx_stop_queue(txq);
274 return NETDEV_TX_BUSY;
278 len = skb_headlen(skb);
279 last_frag = skb_shinfo(skb)->nr_frags;
281 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
283 txbd->tx_bd_opaque = prod;
285 tx_buf = &txr->tx_buf_ring[prod];
287 tx_buf->nr_frags = last_frag;
291 if (skb_vlan_tag_present(skb)) {
292 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
293 skb_vlan_tag_get(skb);
294 /* Currently supports 8021Q, 8021AD vlan offloads
295 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
297 if (skb->vlan_proto == htons(ETH_P_8021Q))
298 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
301 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
302 struct tx_push_buffer *tx_push_buf = txr->tx_push;
303 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
304 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
305 void *pdata = tx_push_buf->data;
309 /* Set COAL_NOW to be ready quickly for the next push */
310 tx_push->tx_bd_len_flags_type =
311 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
312 TX_BD_TYPE_LONG_TX_BD |
313 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
314 TX_BD_FLAGS_COAL_NOW |
315 TX_BD_FLAGS_PACKET_END |
316 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
318 if (skb->ip_summed == CHECKSUM_PARTIAL)
319 tx_push1->tx_bd_hsize_lflags =
320 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
322 tx_push1->tx_bd_hsize_lflags = 0;
324 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
325 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
327 end = pdata + length;
328 end = PTR_ALIGN(end, 8) - 1;
331 skb_copy_from_linear_data(skb, pdata, len);
333 for (j = 0; j < last_frag; j++) {
334 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
337 fptr = skb_frag_address_safe(frag);
341 memcpy(pdata, fptr, skb_frag_size(frag));
342 pdata += skb_frag_size(frag);
345 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
346 txbd->tx_bd_haddr = txr->data_mapping;
347 prod = NEXT_TX(prod);
348 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
349 memcpy(txbd, tx_push1, sizeof(*txbd));
350 prod = NEXT_TX(prod);
352 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
356 netdev_tx_sent_queue(txq, skb->len);
357 wmb(); /* Sync is_push and byte queue before pushing data */
359 push_len = (length + sizeof(*tx_push) + 7) / 8;
361 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
362 __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
363 (push_len - 16) << 1);
365 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
373 if (length < BNXT_MIN_PKT_SIZE) {
374 pad = BNXT_MIN_PKT_SIZE - length;
375 if (skb_pad(skb, pad)) {
376 /* SKB already freed. */
380 length = BNXT_MIN_PKT_SIZE;
383 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
385 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
386 dev_kfree_skb_any(skb);
391 dma_unmap_addr_set(tx_buf, mapping, mapping);
392 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
393 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
395 txbd->tx_bd_haddr = cpu_to_le64(mapping);
397 prod = NEXT_TX(prod);
398 txbd1 = (struct tx_bd_ext *)
399 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
401 txbd1->tx_bd_hsize_lflags = 0;
402 if (skb_is_gso(skb)) {
405 if (skb->encapsulation)
406 hdr_len = skb_inner_network_offset(skb) +
407 skb_inner_network_header_len(skb) +
408 inner_tcp_hdrlen(skb);
410 hdr_len = skb_transport_offset(skb) +
413 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
415 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
416 length = skb_shinfo(skb)->gso_size;
417 txbd1->tx_bd_mss = cpu_to_le32(length);
419 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
420 txbd1->tx_bd_hsize_lflags =
421 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
422 txbd1->tx_bd_mss = 0;
426 flags |= bnxt_lhint_arr[length];
427 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
429 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
430 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
431 for (i = 0; i < last_frag; i++) {
432 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
434 prod = NEXT_TX(prod);
435 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
437 len = skb_frag_size(frag);
438 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
441 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
444 tx_buf = &txr->tx_buf_ring[prod];
445 dma_unmap_addr_set(tx_buf, mapping, mapping);
447 txbd->tx_bd_haddr = cpu_to_le64(mapping);
449 flags = len << TX_BD_LEN_SHIFT;
450 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
454 txbd->tx_bd_len_flags_type =
455 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
456 TX_BD_FLAGS_PACKET_END);
458 netdev_tx_sent_queue(txq, skb->len);
460 /* Sync BD data before updating doorbell */
463 prod = NEXT_TX(prod);
466 if (!skb->xmit_more || netif_xmit_stopped(txq))
467 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
473 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
474 if (skb->xmit_more && !tx_buf->is_push)
475 bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
477 netif_tx_stop_queue(txq);
479 /* netif_tx_stop_queue() must be done before checking
480 * tx index in bnxt_tx_avail() below, because in
481 * bnxt_tx_int(), we update tx index before checking for
482 * netif_tx_queue_stopped().
485 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
486 netif_tx_wake_queue(txq);
493 /* start back at beginning and unmap skb */
495 tx_buf = &txr->tx_buf_ring[prod];
497 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
498 skb_headlen(skb), PCI_DMA_TODEVICE);
499 prod = NEXT_TX(prod);
501 /* unmap remaining mapped pages */
502 for (i = 0; i < last_frag; i++) {
503 prod = NEXT_TX(prod);
504 tx_buf = &txr->tx_buf_ring[prod];
505 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
506 skb_frag_size(&skb_shinfo(skb)->frags[i]),
510 dev_kfree_skb_any(skb);
514 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
516 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
517 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
518 u16 cons = txr->tx_cons;
519 struct pci_dev *pdev = bp->pdev;
521 unsigned int tx_bytes = 0;
523 for (i = 0; i < nr_pkts; i++) {
524 struct bnxt_sw_tx_bd *tx_buf;
528 tx_buf = &txr->tx_buf_ring[cons];
529 cons = NEXT_TX(cons);
533 if (tx_buf->is_push) {
538 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
539 skb_headlen(skb), PCI_DMA_TODEVICE);
540 last = tx_buf->nr_frags;
542 for (j = 0; j < last; j++) {
543 cons = NEXT_TX(cons);
544 tx_buf = &txr->tx_buf_ring[cons];
547 dma_unmap_addr(tx_buf, mapping),
548 skb_frag_size(&skb_shinfo(skb)->frags[j]),
553 cons = NEXT_TX(cons);
555 tx_bytes += skb->len;
556 dev_kfree_skb_any(skb);
559 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
562 /* Need to make the tx_cons update visible to bnxt_start_xmit()
563 * before checking for netif_tx_queue_stopped(). Without the
564 * memory barrier, there is a small possibility that bnxt_start_xmit()
565 * will miss it and cause the queue to be stopped forever.
569 if (unlikely(netif_tx_queue_stopped(txq)) &&
570 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
571 __netif_tx_lock(txq, smp_processor_id());
572 if (netif_tx_queue_stopped(txq) &&
573 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
574 txr->dev_state != BNXT_DEV_STATE_CLOSING)
575 netif_tx_wake_queue(txq);
576 __netif_tx_unlock(txq);
580 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
583 struct device *dev = &bp->pdev->dev;
586 page = alloc_page(gfp);
590 *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
591 DMA_ATTR_WEAK_ORDERING);
592 if (dma_mapping_error(dev, *mapping)) {
596 *mapping += bp->rx_dma_offset;
600 static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
604 struct pci_dev *pdev = bp->pdev;
606 data = kmalloc(bp->rx_buf_size, gfp);
610 *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
611 bp->rx_buf_use_size, bp->rx_dir,
612 DMA_ATTR_WEAK_ORDERING);
614 if (dma_mapping_error(&pdev->dev, *mapping)) {
621 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
624 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
625 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
628 if (BNXT_RX_PAGE_MODE(bp)) {
629 struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
635 rx_buf->data_ptr = page_address(page) + bp->rx_offset;
637 u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
643 rx_buf->data_ptr = data + bp->rx_offset;
645 rx_buf->mapping = mapping;
647 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
651 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
653 u16 prod = rxr->rx_prod;
654 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
655 struct rx_bd *cons_bd, *prod_bd;
657 prod_rx_buf = &rxr->rx_buf_ring[prod];
658 cons_rx_buf = &rxr->rx_buf_ring[cons];
660 prod_rx_buf->data = data;
661 prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
663 prod_rx_buf->mapping = cons_rx_buf->mapping;
665 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
666 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
668 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
671 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
673 u16 next, max = rxr->rx_agg_bmap_size;
675 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
677 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
681 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
682 struct bnxt_rx_ring_info *rxr,
686 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
687 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
688 struct pci_dev *pdev = bp->pdev;
691 u16 sw_prod = rxr->rx_sw_agg_prod;
692 unsigned int offset = 0;
694 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
697 page = alloc_page(gfp);
701 rxr->rx_page_offset = 0;
703 offset = rxr->rx_page_offset;
704 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
705 if (rxr->rx_page_offset == PAGE_SIZE)
710 page = alloc_page(gfp);
715 mapping = dma_map_page_attrs(&pdev->dev, page, offset,
716 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
717 DMA_ATTR_WEAK_ORDERING);
718 if (dma_mapping_error(&pdev->dev, mapping)) {
723 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
724 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
726 __set_bit(sw_prod, rxr->rx_agg_bmap);
727 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
728 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
730 rx_agg_buf->page = page;
731 rx_agg_buf->offset = offset;
732 rx_agg_buf->mapping = mapping;
733 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
734 rxbd->rx_bd_opaque = sw_prod;
738 static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
741 struct bnxt *bp = bnapi->bp;
742 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
743 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
744 u16 prod = rxr->rx_agg_prod;
745 u16 sw_prod = rxr->rx_sw_agg_prod;
748 for (i = 0; i < agg_bufs; i++) {
750 struct rx_agg_cmp *agg;
751 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
752 struct rx_bd *prod_bd;
755 agg = (struct rx_agg_cmp *)
756 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
757 cons = agg->rx_agg_cmp_opaque;
758 __clear_bit(cons, rxr->rx_agg_bmap);
760 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
761 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
763 __set_bit(sw_prod, rxr->rx_agg_bmap);
764 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
765 cons_rx_buf = &rxr->rx_agg_ring[cons];
767 /* It is possible for sw_prod to be equal to cons, so
768 * set cons_rx_buf->page to NULL first.
770 page = cons_rx_buf->page;
771 cons_rx_buf->page = NULL;
772 prod_rx_buf->page = page;
773 prod_rx_buf->offset = cons_rx_buf->offset;
775 prod_rx_buf->mapping = cons_rx_buf->mapping;
777 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
779 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
780 prod_bd->rx_bd_opaque = sw_prod;
782 prod = NEXT_RX_AGG(prod);
783 sw_prod = NEXT_RX_AGG(sw_prod);
784 cp_cons = NEXT_CMP(cp_cons);
786 rxr->rx_agg_prod = prod;
787 rxr->rx_sw_agg_prod = sw_prod;
790 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
791 struct bnxt_rx_ring_info *rxr,
792 u16 cons, void *data, u8 *data_ptr,
794 unsigned int offset_and_len)
796 unsigned int payload = offset_and_len >> 16;
797 unsigned int len = offset_and_len & 0xffff;
798 struct skb_frag_struct *frag;
799 struct page *page = data;
800 u16 prod = rxr->rx_prod;
804 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
806 bnxt_reuse_rx_data(rxr, cons, data);
809 dma_addr -= bp->rx_dma_offset;
810 dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
811 DMA_ATTR_WEAK_ORDERING);
813 if (unlikely(!payload))
814 payload = eth_get_headlen(data_ptr, len);
816 skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
822 off = (void *)data_ptr - page_address(page);
823 skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
824 memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
825 payload + NET_IP_ALIGN);
827 frag = &skb_shinfo(skb)->frags[0];
828 skb_frag_size_sub(frag, payload);
829 frag->page_offset += payload;
830 skb->data_len -= payload;
831 skb->tail += payload;
836 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
837 struct bnxt_rx_ring_info *rxr, u16 cons,
838 void *data, u8 *data_ptr,
840 unsigned int offset_and_len)
842 u16 prod = rxr->rx_prod;
846 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
848 bnxt_reuse_rx_data(rxr, cons, data);
852 skb = build_skb(data, 0);
853 dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
854 bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
860 skb_reserve(skb, bp->rx_offset);
861 skb_put(skb, offset_and_len & 0xffff);
865 static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
866 struct sk_buff *skb, u16 cp_cons,
869 struct pci_dev *pdev = bp->pdev;
870 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
871 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
872 u16 prod = rxr->rx_agg_prod;
875 for (i = 0; i < agg_bufs; i++) {
877 struct rx_agg_cmp *agg;
878 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
882 agg = (struct rx_agg_cmp *)
883 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
884 cons = agg->rx_agg_cmp_opaque;
885 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
886 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
888 cons_rx_buf = &rxr->rx_agg_ring[cons];
889 skb_fill_page_desc(skb, i, cons_rx_buf->page,
890 cons_rx_buf->offset, frag_len);
891 __clear_bit(cons, rxr->rx_agg_bmap);
893 /* It is possible for bnxt_alloc_rx_page() to allocate
894 * a sw_prod index that equals the cons index, so we
895 * need to clear the cons entry now.
897 mapping = cons_rx_buf->mapping;
898 page = cons_rx_buf->page;
899 cons_rx_buf->page = NULL;
901 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
902 struct skb_shared_info *shinfo;
903 unsigned int nr_frags;
905 shinfo = skb_shinfo(skb);
906 nr_frags = --shinfo->nr_frags;
907 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
911 cons_rx_buf->page = page;
913 /* Update prod since possibly some pages have been
916 rxr->rx_agg_prod = prod;
917 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
921 dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
923 DMA_ATTR_WEAK_ORDERING);
925 skb->data_len += frag_len;
926 skb->len += frag_len;
927 skb->truesize += PAGE_SIZE;
929 prod = NEXT_RX_AGG(prod);
930 cp_cons = NEXT_CMP(cp_cons);
932 rxr->rx_agg_prod = prod;
936 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
937 u8 agg_bufs, u32 *raw_cons)
940 struct rx_agg_cmp *agg;
942 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
943 last = RING_CMP(*raw_cons);
944 agg = (struct rx_agg_cmp *)
945 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
946 return RX_AGG_CMP_VALID(agg, *raw_cons);
949 static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
953 struct bnxt *bp = bnapi->bp;
954 struct pci_dev *pdev = bp->pdev;
957 skb = napi_alloc_skb(&bnapi->napi, len);
961 dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
964 memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
967 dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
974 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
975 u32 *raw_cons, void *cmp)
977 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
978 struct rx_cmp *rxcmp = cmp;
979 u32 tmp_raw_cons = *raw_cons;
980 u8 cmp_type, agg_bufs = 0;
982 cmp_type = RX_CMP_TYPE(rxcmp);
984 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
985 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
987 RX_CMP_AGG_BUFS_SHIFT;
988 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
989 struct rx_tpa_end_cmp *tpa_end = cmp;
991 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
992 RX_TPA_END_CMP_AGG_BUFS) >>
993 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
997 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1000 *raw_cons = tmp_raw_cons;
1004 static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
1006 if (!rxr->bnapi->in_reset) {
1007 rxr->bnapi->in_reset = true;
1008 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
1009 schedule_work(&bp->sp_task);
1011 rxr->rx_next_cons = 0xffff;
1014 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1015 struct rx_tpa_start_cmp *tpa_start,
1016 struct rx_tpa_start_cmp_ext *tpa_start1)
1018 u8 agg_id = TPA_START_AGG_ID(tpa_start);
1020 struct bnxt_tpa_info *tpa_info;
1021 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1022 struct rx_bd *prod_bd;
1025 cons = tpa_start->rx_tpa_start_cmp_opaque;
1026 prod = rxr->rx_prod;
1027 cons_rx_buf = &rxr->rx_buf_ring[cons];
1028 prod_rx_buf = &rxr->rx_buf_ring[prod];
1029 tpa_info = &rxr->rx_tpa[agg_id];
1031 if (unlikely(cons != rxr->rx_next_cons)) {
1032 bnxt_sched_reset(bp, rxr);
1036 prod_rx_buf->data = tpa_info->data;
1037 prod_rx_buf->data_ptr = tpa_info->data_ptr;
1039 mapping = tpa_info->mapping;
1040 prod_rx_buf->mapping = mapping;
1042 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
1044 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1046 tpa_info->data = cons_rx_buf->data;
1047 tpa_info->data_ptr = cons_rx_buf->data_ptr;
1048 cons_rx_buf->data = NULL;
1049 tpa_info->mapping = cons_rx_buf->mapping;
1052 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1053 RX_TPA_START_CMP_LEN_SHIFT;
1054 if (likely(TPA_START_HASH_VALID(tpa_start))) {
1055 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
1057 tpa_info->hash_type = PKT_HASH_TYPE_L4;
1058 tpa_info->gso_type = SKB_GSO_TCPV4;
1059 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1061 tpa_info->gso_type = SKB_GSO_TCPV6;
1062 tpa_info->rss_hash =
1063 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1065 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1066 tpa_info->gso_type = 0;
1067 if (netif_msg_rx_err(bp))
1068 netdev_warn(bp->dev, "TPA packet without valid hash\n");
1070 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1071 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1072 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1074 rxr->rx_prod = NEXT_RX(prod);
1075 cons = NEXT_RX(cons);
1076 rxr->rx_next_cons = NEXT_RX(cons);
1077 cons_rx_buf = &rxr->rx_buf_ring[cons];
1079 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1080 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1081 cons_rx_buf->data = NULL;
1084 static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
1085 u16 cp_cons, u32 agg_bufs)
1088 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1091 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1092 int payload_off, int tcp_ts,
1093 struct sk_buff *skb)
1098 u16 outer_ip_off, inner_ip_off, inner_mac_off;
1099 u32 hdr_info = tpa_info->hdr_info;
1100 bool loopback = false;
1102 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1103 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1104 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1106 /* If the packet is an internal loopback packet, the offsets will
1107 * have an extra 4 bytes.
1109 if (inner_mac_off == 4) {
1111 } else if (inner_mac_off > 4) {
1112 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1115 /* We only support inner iPv4/ipv6. If we don't see the
1116 * correct protocol ID, it must be a loopback packet where
1117 * the offsets are off by 4.
1119 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1123 /* internal loopback packet, subtract all offsets by 4 */
1129 nw_off = inner_ip_off - ETH_HLEN;
1130 skb_set_network_header(skb, nw_off);
1131 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1132 struct ipv6hdr *iph = ipv6_hdr(skb);
1134 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1135 len = skb->len - skb_transport_offset(skb);
1137 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1139 struct iphdr *iph = ip_hdr(skb);
1141 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1142 len = skb->len - skb_transport_offset(skb);
1144 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1147 if (inner_mac_off) { /* tunnel */
1148 struct udphdr *uh = NULL;
1149 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1152 if (proto == htons(ETH_P_IP)) {
1153 struct iphdr *iph = (struct iphdr *)skb->data;
1155 if (iph->protocol == IPPROTO_UDP)
1156 uh = (struct udphdr *)(iph + 1);
1158 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1160 if (iph->nexthdr == IPPROTO_UDP)
1161 uh = (struct udphdr *)(iph + 1);
1165 skb_shinfo(skb)->gso_type |=
1166 SKB_GSO_UDP_TUNNEL_CSUM;
1168 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1175 #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1176 #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1178 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1179 int payload_off, int tcp_ts,
1180 struct sk_buff *skb)
1184 int len, nw_off, tcp_opt_len = 0;
1189 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1192 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1194 skb_set_network_header(skb, nw_off);
1196 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1197 len = skb->len - skb_transport_offset(skb);
1199 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1200 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1201 struct ipv6hdr *iph;
1203 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1205 skb_set_network_header(skb, nw_off);
1206 iph = ipv6_hdr(skb);
1207 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1208 len = skb->len - skb_transport_offset(skb);
1210 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1212 dev_kfree_skb_any(skb);
1216 if (nw_off) { /* tunnel */
1217 struct udphdr *uh = NULL;
1219 if (skb->protocol == htons(ETH_P_IP)) {
1220 struct iphdr *iph = (struct iphdr *)skb->data;
1222 if (iph->protocol == IPPROTO_UDP)
1223 uh = (struct udphdr *)(iph + 1);
1225 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1227 if (iph->nexthdr == IPPROTO_UDP)
1228 uh = (struct udphdr *)(iph + 1);
1232 skb_shinfo(skb)->gso_type |=
1233 SKB_GSO_UDP_TUNNEL_CSUM;
1235 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1242 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1243 struct bnxt_tpa_info *tpa_info,
1244 struct rx_tpa_end_cmp *tpa_end,
1245 struct rx_tpa_end_cmp_ext *tpa_end1,
1246 struct sk_buff *skb)
1252 segs = TPA_END_TPA_SEGS(tpa_end);
1256 NAPI_GRO_CB(skb)->count = segs;
1257 skb_shinfo(skb)->gso_size =
1258 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1259 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1260 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1261 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1262 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1263 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1265 tcp_gro_complete(skb);
1270 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1271 struct bnxt_napi *bnapi,
1273 struct rx_tpa_end_cmp *tpa_end,
1274 struct rx_tpa_end_cmp_ext *tpa_end1,
1277 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1278 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1279 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1280 u8 *data_ptr, agg_bufs;
1281 u16 cp_cons = RING_CMP(*raw_cons);
1283 struct bnxt_tpa_info *tpa_info;
1285 struct sk_buff *skb;
1288 if (unlikely(bnapi->in_reset)) {
1289 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1292 return ERR_PTR(-EBUSY);
1296 tpa_info = &rxr->rx_tpa[agg_id];
1297 data = tpa_info->data;
1298 data_ptr = tpa_info->data_ptr;
1300 len = tpa_info->len;
1301 mapping = tpa_info->mapping;
1303 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1304 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1307 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1308 return ERR_PTR(-EBUSY);
1310 *event |= BNXT_AGG_EVENT;
1311 cp_cons = NEXT_CMP(cp_cons);
1314 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1315 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1316 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1317 agg_bufs, (int)MAX_SKB_FRAGS);
1321 if (len <= bp->rx_copy_thresh) {
1322 skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1324 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1329 dma_addr_t new_mapping;
1331 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1333 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1337 tpa_info->data = new_data;
1338 tpa_info->data_ptr = new_data + bp->rx_offset;
1339 tpa_info->mapping = new_mapping;
1341 skb = build_skb(data, 0);
1342 dma_unmap_single_attrs(&bp->pdev->dev, mapping,
1343 bp->rx_buf_use_size, bp->rx_dir,
1344 DMA_ATTR_WEAK_ORDERING);
1348 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1351 skb_reserve(skb, bp->rx_offset);
1356 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1358 /* Page reuse already handled by bnxt_rx_pages(). */
1362 skb->protocol = eth_type_trans(skb, bp->dev);
1364 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1365 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1367 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1368 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1369 u16 vlan_proto = tpa_info->metadata >>
1370 RX_CMP_FLAGS2_METADATA_TPID_SFT;
1371 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
1373 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1376 skb_checksum_none_assert(skb);
1377 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1378 skb->ip_summed = CHECKSUM_UNNECESSARY;
1380 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1383 if (TPA_END_GRO(tpa_end))
1384 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1389 /* returns the following:
1390 * 1 - 1 packet successfully received
1391 * 0 - successful TPA_START, packet not completed yet
1392 * -EBUSY - completion ring does not have all the agg buffers yet
1393 * -ENOMEM - packet aborted due to out of memory
1394 * -EIO - packet aborted due to hw error indicated in BD
1396 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1399 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1400 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1401 struct net_device *dev = bp->dev;
1402 struct rx_cmp *rxcmp;
1403 struct rx_cmp_ext *rxcmp1;
1404 u32 tmp_raw_cons = *raw_cons;
1405 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1406 struct bnxt_sw_rx_bd *rx_buf;
1408 u8 *data_ptr, agg_bufs, cmp_type;
1409 dma_addr_t dma_addr;
1410 struct sk_buff *skb;
1415 rxcmp = (struct rx_cmp *)
1416 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1418 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1419 cp_cons = RING_CMP(tmp_raw_cons);
1420 rxcmp1 = (struct rx_cmp_ext *)
1421 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1423 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1426 cmp_type = RX_CMP_TYPE(rxcmp);
1428 prod = rxr->rx_prod;
1430 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1431 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1432 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1434 *event |= BNXT_RX_EVENT;
1435 goto next_rx_no_prod;
1437 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1438 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1439 (struct rx_tpa_end_cmp *)rxcmp,
1440 (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
1442 if (unlikely(IS_ERR(skb)))
1447 skb_record_rx_queue(skb, bnapi->index);
1448 napi_gro_receive(&bnapi->napi, skb);
1451 *event |= BNXT_RX_EVENT;
1452 goto next_rx_no_prod;
1455 cons = rxcmp->rx_cmp_opaque;
1456 rx_buf = &rxr->rx_buf_ring[cons];
1457 data = rx_buf->data;
1458 data_ptr = rx_buf->data_ptr;
1459 if (unlikely(cons != rxr->rx_next_cons)) {
1460 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1462 bnxt_sched_reset(bp, rxr);
1467 misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
1468 agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
1471 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1474 cp_cons = NEXT_CMP(cp_cons);
1475 *event |= BNXT_AGG_EVENT;
1477 *event |= BNXT_RX_EVENT;
1479 rx_buf->data = NULL;
1480 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1481 bnxt_reuse_rx_data(rxr, cons, data);
1483 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1489 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1490 dma_addr = rx_buf->mapping;
1492 if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
1497 if (len <= bp->rx_copy_thresh) {
1498 skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
1499 bnxt_reuse_rx_data(rxr, cons, data);
1507 if (rx_buf->data_ptr == data_ptr)
1508 payload = misc & RX_CMP_PAYLOAD_OFFSET;
1511 skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
1520 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1527 if (RX_CMP_HASH_VALID(rxcmp)) {
1528 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1529 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1531 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1532 if (hash_type != 1 && hash_type != 3)
1533 type = PKT_HASH_TYPE_L3;
1534 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1537 skb->protocol = eth_type_trans(skb, dev);
1539 if ((rxcmp1->rx_cmp_flags2 &
1540 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1541 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
1542 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1543 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
1544 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1546 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
1549 skb_checksum_none_assert(skb);
1550 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1551 if (dev->features & NETIF_F_RXCSUM) {
1552 skb->ip_summed = CHECKSUM_UNNECESSARY;
1553 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1556 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1557 if (dev->features & NETIF_F_RXCSUM)
1558 cpr->rx_l4_csum_errors++;
1562 skb_record_rx_queue(skb, bnapi->index);
1563 napi_gro_receive(&bnapi->napi, skb);
1567 rxr->rx_prod = NEXT_RX(prod);
1568 rxr->rx_next_cons = NEXT_RX(cons);
1571 *raw_cons = tmp_raw_cons;
1576 #define BNXT_GET_EVENT_PORT(data) \
1578 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1580 static int bnxt_async_event_process(struct bnxt *bp,
1581 struct hwrm_async_event_cmpl *cmpl)
1583 u16 event_id = le16_to_cpu(cmpl->event_id);
1585 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1587 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1588 u32 data1 = le32_to_cpu(cmpl->event_data1);
1589 struct bnxt_link_info *link_info = &bp->link_info;
1592 goto async_event_process_exit;
1593 if (data1 & 0x20000) {
1594 u16 fw_speed = link_info->force_link_speed;
1595 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1597 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1600 set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
1603 case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1604 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
1606 case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1607 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
1609 case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1610 u32 data1 = le32_to_cpu(cmpl->event_data1);
1611 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1616 if (bp->pf.port_id != port_id)
1619 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1622 case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1624 goto async_event_process_exit;
1625 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1628 goto async_event_process_exit;
1630 schedule_work(&bp->sp_task);
1631 async_event_process_exit:
1632 bnxt_ulp_async_events(bp, cmpl);
1636 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1638 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1639 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1640 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1641 (struct hwrm_fwd_req_cmpl *)txcmp;
1643 switch (cmpl_type) {
1644 case CMPL_BASE_TYPE_HWRM_DONE:
1645 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1646 if (seq_id == bp->hwrm_intr_seq_id)
1647 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1649 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1652 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1653 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1655 if ((vf_id < bp->pf.first_vf_id) ||
1656 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1657 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1662 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1663 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1664 schedule_work(&bp->sp_task);
1667 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1668 bnxt_async_event_process(bp,
1669 (struct hwrm_async_event_cmpl *)txcmp);
1678 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1680 struct bnxt_napi *bnapi = dev_instance;
1681 struct bnxt *bp = bnapi->bp;
1682 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1683 u32 cons = RING_CMP(cpr->cp_raw_cons);
1685 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1686 napi_schedule(&bnapi->napi);
1690 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1692 u32 raw_cons = cpr->cp_raw_cons;
1693 u16 cons = RING_CMP(raw_cons);
1694 struct tx_cmp *txcmp;
1696 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1698 return TX_CMP_VALID(txcmp, raw_cons);
1701 static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1703 struct bnxt_napi *bnapi = dev_instance;
1704 struct bnxt *bp = bnapi->bp;
1705 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1706 u32 cons = RING_CMP(cpr->cp_raw_cons);
1709 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1711 if (!bnxt_has_work(bp, cpr)) {
1712 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
1713 /* return if erroneous interrupt */
1714 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1718 /* disable ring IRQ */
1719 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1721 /* Return here if interrupt is shared and is disabled. */
1722 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1725 napi_schedule(&bnapi->napi);
1729 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1731 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1732 u32 raw_cons = cpr->cp_raw_cons;
1737 struct tx_cmp *txcmp;
1742 cons = RING_CMP(raw_cons);
1743 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1745 if (!TX_CMP_VALID(txcmp, raw_cons))
1748 /* The valid test of the entry must be done first before
1749 * reading any further.
1752 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1754 /* return full budget so NAPI will complete. */
1755 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1757 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1758 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1759 if (likely(rc >= 0))
1761 else if (rc == -EBUSY) /* partial completion */
1763 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1764 CMPL_BASE_TYPE_HWRM_DONE) ||
1765 (TX_CMP_TYPE(txcmp) ==
1766 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1767 (TX_CMP_TYPE(txcmp) ==
1768 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1769 bnxt_hwrm_handler(bp, txcmp);
1771 raw_cons = NEXT_RAW_CMP(raw_cons);
1773 if (rx_pkts == budget)
1777 if (event & BNXT_TX_EVENT) {
1778 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
1779 void __iomem *db = txr->tx_doorbell;
1780 u16 prod = txr->tx_prod;
1782 /* Sync BD data before updating doorbell */
1785 bnxt_db_write(bp, db, DB_KEY_TX | prod);
1788 cpr->cp_raw_cons = raw_cons;
1789 /* ACK completion ring before freeing tx ring and producing new
1790 * buffers in rx/agg rings to prevent overflowing the completion
1793 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1796 bnapi->tx_int(bp, bnapi, tx_pkts);
1798 if (event & BNXT_RX_EVENT) {
1799 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1801 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1802 if (event & BNXT_AGG_EVENT)
1803 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1804 DB_KEY_RX | rxr->rx_agg_prod);
1809 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1811 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1812 struct bnxt *bp = bnapi->bp;
1813 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1814 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1815 struct tx_cmp *txcmp;
1816 struct rx_cmp_ext *rxcmp1;
1817 u32 cp_cons, tmp_raw_cons;
1818 u32 raw_cons = cpr->cp_raw_cons;
1825 cp_cons = RING_CMP(raw_cons);
1826 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1828 if (!TX_CMP_VALID(txcmp, raw_cons))
1831 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1832 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1833 cp_cons = RING_CMP(tmp_raw_cons);
1834 rxcmp1 = (struct rx_cmp_ext *)
1835 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1837 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1840 /* force an error to recycle the buffer */
1841 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1842 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1844 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
1845 if (likely(rc == -EIO))
1847 else if (rc == -EBUSY) /* partial completion */
1849 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1850 CMPL_BASE_TYPE_HWRM_DONE)) {
1851 bnxt_hwrm_handler(bp, txcmp);
1854 "Invalid completion received on special ring\n");
1856 raw_cons = NEXT_RAW_CMP(raw_cons);
1858 if (rx_pkts == budget)
1862 cpr->cp_raw_cons = raw_cons;
1863 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1864 bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
1866 if (event & BNXT_AGG_EVENT)
1867 bnxt_db_write(bp, rxr->rx_agg_doorbell,
1868 DB_KEY_RX | rxr->rx_agg_prod);
1870 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1871 napi_complete_done(napi, rx_pkts);
1872 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1877 static int bnxt_poll(struct napi_struct *napi, int budget)
1879 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1880 struct bnxt *bp = bnapi->bp;
1881 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1885 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1887 if (work_done >= budget)
1890 if (!bnxt_has_work(bp, cpr)) {
1891 if (napi_complete_done(napi, work_done))
1892 BNXT_CP_DB_REARM(cpr->cp_doorbell,
1901 static void bnxt_free_tx_skbs(struct bnxt *bp)
1904 struct pci_dev *pdev = bp->pdev;
1909 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1910 for (i = 0; i < bp->tx_nr_rings; i++) {
1911 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
1914 for (j = 0; j < max_idx;) {
1915 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1916 struct sk_buff *skb = tx_buf->skb;
1926 if (tx_buf->is_push) {
1932 dma_unmap_single(&pdev->dev,
1933 dma_unmap_addr(tx_buf, mapping),
1937 last = tx_buf->nr_frags;
1939 for (k = 0; k < last; k++, j++) {
1940 int ring_idx = j & bp->tx_ring_mask;
1941 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1943 tx_buf = &txr->tx_buf_ring[ring_idx];
1946 dma_unmap_addr(tx_buf, mapping),
1947 skb_frag_size(frag), PCI_DMA_TODEVICE);
1951 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1955 static void bnxt_free_rx_skbs(struct bnxt *bp)
1957 int i, max_idx, max_agg_idx;
1958 struct pci_dev *pdev = bp->pdev;
1963 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1964 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1965 for (i = 0; i < bp->rx_nr_rings; i++) {
1966 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
1970 for (j = 0; j < MAX_TPA; j++) {
1971 struct bnxt_tpa_info *tpa_info =
1973 u8 *data = tpa_info->data;
1978 dma_unmap_single_attrs(&pdev->dev,
1980 bp->rx_buf_use_size,
1982 DMA_ATTR_WEAK_ORDERING);
1984 tpa_info->data = NULL;
1990 for (j = 0; j < max_idx; j++) {
1991 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1992 dma_addr_t mapping = rx_buf->mapping;
1993 void *data = rx_buf->data;
1998 rx_buf->data = NULL;
2000 if (BNXT_RX_PAGE_MODE(bp)) {
2001 mapping -= bp->rx_dma_offset;
2002 dma_unmap_page_attrs(&pdev->dev, mapping,
2003 PAGE_SIZE, bp->rx_dir,
2004 DMA_ATTR_WEAK_ORDERING);
2007 dma_unmap_single_attrs(&pdev->dev, mapping,
2008 bp->rx_buf_use_size,
2010 DMA_ATTR_WEAK_ORDERING);
2015 for (j = 0; j < max_agg_idx; j++) {
2016 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
2017 &rxr->rx_agg_ring[j];
2018 struct page *page = rx_agg_buf->page;
2023 dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
2026 DMA_ATTR_WEAK_ORDERING);
2028 rx_agg_buf->page = NULL;
2029 __clear_bit(j, rxr->rx_agg_bmap);
2034 __free_page(rxr->rx_page);
2035 rxr->rx_page = NULL;
2040 static void bnxt_free_skbs(struct bnxt *bp)
2042 bnxt_free_tx_skbs(bp);
2043 bnxt_free_rx_skbs(bp);
2046 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2048 struct pci_dev *pdev = bp->pdev;
2051 for (i = 0; i < ring->nr_pages; i++) {
2052 if (!ring->pg_arr[i])
2055 dma_free_coherent(&pdev->dev, ring->page_size,
2056 ring->pg_arr[i], ring->dma_arr[i]);
2058 ring->pg_arr[i] = NULL;
2061 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
2062 ring->pg_tbl, ring->pg_tbl_map);
2063 ring->pg_tbl = NULL;
2065 if (ring->vmem_size && *ring->vmem) {
2071 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
2074 struct pci_dev *pdev = bp->pdev;
2076 if (ring->nr_pages > 1) {
2077 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
2085 for (i = 0; i < ring->nr_pages; i++) {
2086 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
2090 if (!ring->pg_arr[i])
2093 if (ring->nr_pages > 1)
2094 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
2097 if (ring->vmem_size) {
2098 *ring->vmem = vzalloc(ring->vmem_size);
2105 static void bnxt_free_rx_rings(struct bnxt *bp)
2112 for (i = 0; i < bp->rx_nr_rings; i++) {
2113 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2114 struct bnxt_ring_struct *ring;
2117 bpf_prog_put(rxr->xdp_prog);
2122 kfree(rxr->rx_agg_bmap);
2123 rxr->rx_agg_bmap = NULL;
2125 ring = &rxr->rx_ring_struct;
2126 bnxt_free_ring(bp, ring);
2128 ring = &rxr->rx_agg_ring_struct;
2129 bnxt_free_ring(bp, ring);
2133 static int bnxt_alloc_rx_rings(struct bnxt *bp)
2135 int i, rc, agg_rings = 0, tpa_rings = 0;
2140 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2143 if (bp->flags & BNXT_FLAG_TPA)
2146 for (i = 0; i < bp->rx_nr_rings; i++) {
2147 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
2148 struct bnxt_ring_struct *ring;
2150 ring = &rxr->rx_ring_struct;
2152 rc = bnxt_alloc_ring(bp, ring);
2159 ring = &rxr->rx_agg_ring_struct;
2160 rc = bnxt_alloc_ring(bp, ring);
2164 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2165 mem_size = rxr->rx_agg_bmap_size / 8;
2166 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2167 if (!rxr->rx_agg_bmap)
2171 rxr->rx_tpa = kcalloc(MAX_TPA,
2172 sizeof(struct bnxt_tpa_info),
2182 static void bnxt_free_tx_rings(struct bnxt *bp)
2185 struct pci_dev *pdev = bp->pdev;
2190 for (i = 0; i < bp->tx_nr_rings; i++) {
2191 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2192 struct bnxt_ring_struct *ring;
2195 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2196 txr->tx_push, txr->tx_push_mapping);
2197 txr->tx_push = NULL;
2200 ring = &txr->tx_ring_struct;
2202 bnxt_free_ring(bp, ring);
2206 static int bnxt_alloc_tx_rings(struct bnxt *bp)
2209 struct pci_dev *pdev = bp->pdev;
2211 bp->tx_push_size = 0;
2212 if (bp->tx_push_thresh) {
2215 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2216 bp->tx_push_thresh);
2218 if (push_size > 256) {
2220 bp->tx_push_thresh = 0;
2223 bp->tx_push_size = push_size;
2226 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
2227 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2228 struct bnxt_ring_struct *ring;
2230 ring = &txr->tx_ring_struct;
2232 rc = bnxt_alloc_ring(bp, ring);
2236 if (bp->tx_push_size) {
2239 /* One pre-allocated DMA buffer to backup
2242 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2244 &txr->tx_push_mapping,
2250 mapping = txr->tx_push_mapping +
2251 sizeof(struct tx_push_bd);
2252 txr->data_mapping = cpu_to_le64(mapping);
2254 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
2256 ring->queue_id = bp->q_info[j].queue_id;
2257 if (i < bp->tx_nr_rings_xdp)
2259 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2265 static void bnxt_free_cp_rings(struct bnxt *bp)
2272 for (i = 0; i < bp->cp_nr_rings; i++) {
2273 struct bnxt_napi *bnapi = bp->bnapi[i];
2274 struct bnxt_cp_ring_info *cpr;
2275 struct bnxt_ring_struct *ring;
2280 cpr = &bnapi->cp_ring;
2281 ring = &cpr->cp_ring_struct;
2283 bnxt_free_ring(bp, ring);
2287 static int bnxt_alloc_cp_rings(struct bnxt *bp)
2291 for (i = 0; i < bp->cp_nr_rings; i++) {
2292 struct bnxt_napi *bnapi = bp->bnapi[i];
2293 struct bnxt_cp_ring_info *cpr;
2294 struct bnxt_ring_struct *ring;
2299 cpr = &bnapi->cp_ring;
2300 ring = &cpr->cp_ring_struct;
2302 rc = bnxt_alloc_ring(bp, ring);
2309 static void bnxt_init_ring_struct(struct bnxt *bp)
2313 for (i = 0; i < bp->cp_nr_rings; i++) {
2314 struct bnxt_napi *bnapi = bp->bnapi[i];
2315 struct bnxt_cp_ring_info *cpr;
2316 struct bnxt_rx_ring_info *rxr;
2317 struct bnxt_tx_ring_info *txr;
2318 struct bnxt_ring_struct *ring;
2323 cpr = &bnapi->cp_ring;
2324 ring = &cpr->cp_ring_struct;
2325 ring->nr_pages = bp->cp_nr_pages;
2326 ring->page_size = HW_CMPD_RING_SIZE;
2327 ring->pg_arr = (void **)cpr->cp_desc_ring;
2328 ring->dma_arr = cpr->cp_desc_mapping;
2329 ring->vmem_size = 0;
2331 rxr = bnapi->rx_ring;
2335 ring = &rxr->rx_ring_struct;
2336 ring->nr_pages = bp->rx_nr_pages;
2337 ring->page_size = HW_RXBD_RING_SIZE;
2338 ring->pg_arr = (void **)rxr->rx_desc_ring;
2339 ring->dma_arr = rxr->rx_desc_mapping;
2340 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2341 ring->vmem = (void **)&rxr->rx_buf_ring;
2343 ring = &rxr->rx_agg_ring_struct;
2344 ring->nr_pages = bp->rx_agg_nr_pages;
2345 ring->page_size = HW_RXBD_RING_SIZE;
2346 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2347 ring->dma_arr = rxr->rx_agg_desc_mapping;
2348 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2349 ring->vmem = (void **)&rxr->rx_agg_ring;
2352 txr = bnapi->tx_ring;
2356 ring = &txr->tx_ring_struct;
2357 ring->nr_pages = bp->tx_nr_pages;
2358 ring->page_size = HW_RXBD_RING_SIZE;
2359 ring->pg_arr = (void **)txr->tx_desc_ring;
2360 ring->dma_arr = txr->tx_desc_mapping;
2361 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2362 ring->vmem = (void **)&txr->tx_buf_ring;
2366 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2370 struct rx_bd **rx_buf_ring;
2372 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2373 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2377 rxbd = rx_buf_ring[i];
2381 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2382 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2383 rxbd->rx_bd_opaque = prod;
2388 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2390 struct net_device *dev = bp->dev;
2391 struct bnxt_rx_ring_info *rxr;
2392 struct bnxt_ring_struct *ring;
2396 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2397 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2399 if (NET_IP_ALIGN == 2)
2400 type |= RX_BD_FLAGS_SOP;
2402 rxr = &bp->rx_ring[ring_nr];
2403 ring = &rxr->rx_ring_struct;
2404 bnxt_init_rxbd_pages(ring, type);
2406 if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
2407 rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
2408 if (IS_ERR(rxr->xdp_prog)) {
2409 int rc = PTR_ERR(rxr->xdp_prog);
2411 rxr->xdp_prog = NULL;
2415 prod = rxr->rx_prod;
2416 for (i = 0; i < bp->rx_ring_size; i++) {
2417 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2418 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2419 ring_nr, i, bp->rx_ring_size);
2422 prod = NEXT_RX(prod);
2424 rxr->rx_prod = prod;
2425 ring->fw_ring_id = INVALID_HW_RING_ID;
2427 ring = &rxr->rx_agg_ring_struct;
2428 ring->fw_ring_id = INVALID_HW_RING_ID;
2430 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2433 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
2434 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2436 bnxt_init_rxbd_pages(ring, type);
2438 prod = rxr->rx_agg_prod;
2439 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2440 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2441 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2442 ring_nr, i, bp->rx_ring_size);
2445 prod = NEXT_RX_AGG(prod);
2447 rxr->rx_agg_prod = prod;
2449 if (bp->flags & BNXT_FLAG_TPA) {
2454 for (i = 0; i < MAX_TPA; i++) {
2455 data = __bnxt_alloc_rx_data(bp, &mapping,
2460 rxr->rx_tpa[i].data = data;
2461 rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
2462 rxr->rx_tpa[i].mapping = mapping;
2465 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2473 static void bnxt_init_cp_rings(struct bnxt *bp)
2477 for (i = 0; i < bp->cp_nr_rings; i++) {
2478 struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
2479 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
2481 ring->fw_ring_id = INVALID_HW_RING_ID;
2485 static int bnxt_init_rx_rings(struct bnxt *bp)
2489 if (BNXT_RX_PAGE_MODE(bp)) {
2490 bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
2491 bp->rx_dma_offset = XDP_PACKET_HEADROOM;
2493 bp->rx_offset = BNXT_RX_OFFSET;
2494 bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
2497 for (i = 0; i < bp->rx_nr_rings; i++) {
2498 rc = bnxt_init_one_rx_ring(bp, i);
2506 static int bnxt_init_tx_rings(struct bnxt *bp)
2510 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2513 for (i = 0; i < bp->tx_nr_rings; i++) {
2514 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
2515 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2517 ring->fw_ring_id = INVALID_HW_RING_ID;
2523 static void bnxt_free_ring_grps(struct bnxt *bp)
2525 kfree(bp->grp_info);
2526 bp->grp_info = NULL;
2529 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2534 bp->grp_info = kcalloc(bp->cp_nr_rings,
2535 sizeof(struct bnxt_ring_grp_info),
2540 for (i = 0; i < bp->cp_nr_rings; i++) {
2542 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2543 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2544 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2545 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2546 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2551 static void bnxt_free_vnics(struct bnxt *bp)
2553 kfree(bp->vnic_info);
2554 bp->vnic_info = NULL;
2558 static int bnxt_alloc_vnics(struct bnxt *bp)
2562 #ifdef CONFIG_RFS_ACCEL
2563 if (bp->flags & BNXT_FLAG_RFS)
2564 num_vnics += bp->rx_nr_rings;
2567 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2570 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2575 bp->nr_vnics = num_vnics;
2579 static void bnxt_init_vnics(struct bnxt *bp)
2583 for (i = 0; i < bp->nr_vnics; i++) {
2584 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2586 vnic->fw_vnic_id = INVALID_HW_RING_ID;
2587 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2588 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
2589 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2591 if (bp->vnic_info[i].rss_hash_key) {
2593 prandom_bytes(vnic->rss_hash_key,
2596 memcpy(vnic->rss_hash_key,
2597 bp->vnic_info[0].rss_hash_key,
2603 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2607 pages = ring_size / desc_per_pg;
2614 while (pages & (pages - 1))
2620 void bnxt_set_tpa_flags(struct bnxt *bp)
2622 bp->flags &= ~BNXT_FLAG_TPA;
2623 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
2625 if (bp->dev->features & NETIF_F_LRO)
2626 bp->flags |= BNXT_FLAG_LRO;
2627 if (bp->dev->features & NETIF_F_GRO)
2628 bp->flags |= BNXT_FLAG_GRO;
2631 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2634 void bnxt_set_ring_params(struct bnxt *bp)
2636 u32 ring_size, rx_size, rx_space;
2637 u32 agg_factor = 0, agg_ring_size = 0;
2639 /* 8 for CRC and VLAN */
2640 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2642 rx_space = rx_size + NET_SKB_PAD +
2643 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2645 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2646 ring_size = bp->rx_ring_size;
2647 bp->rx_agg_ring_size = 0;
2648 bp->rx_agg_nr_pages = 0;
2650 if (bp->flags & BNXT_FLAG_TPA)
2651 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
2653 bp->flags &= ~BNXT_FLAG_JUMBO;
2654 if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
2657 bp->flags |= BNXT_FLAG_JUMBO;
2658 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2659 if (jumbo_factor > agg_factor)
2660 agg_factor = jumbo_factor;
2662 agg_ring_size = ring_size * agg_factor;
2664 if (agg_ring_size) {
2665 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2667 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2668 u32 tmp = agg_ring_size;
2670 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2671 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2672 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2673 tmp, agg_ring_size);
2675 bp->rx_agg_ring_size = agg_ring_size;
2676 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2677 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2678 rx_space = rx_size + NET_SKB_PAD +
2679 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2682 bp->rx_buf_use_size = rx_size;
2683 bp->rx_buf_size = rx_space;
2685 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2686 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2688 ring_size = bp->tx_ring_size;
2689 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2690 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2692 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2693 bp->cp_ring_size = ring_size;
2695 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2696 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2697 bp->cp_nr_pages = MAX_CP_PAGES;
2698 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2699 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2700 ring_size, bp->cp_ring_size);
2702 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2703 bp->cp_ring_mask = bp->cp_bit - 1;
2706 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
2709 if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
2711 bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
2712 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
2713 bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
2714 bp->dev->hw_features &= ~NETIF_F_LRO;
2715 bp->dev->features &= ~NETIF_F_LRO;
2716 bp->rx_dir = DMA_BIDIRECTIONAL;
2717 bp->rx_skb_func = bnxt_rx_page_skb;
2719 bp->dev->max_mtu = BNXT_MAX_MTU;
2720 bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
2721 bp->rx_dir = DMA_FROM_DEVICE;
2722 bp->rx_skb_func = bnxt_rx_skb;
2727 static void bnxt_free_vnic_attributes(struct bnxt *bp)
2730 struct bnxt_vnic_info *vnic;
2731 struct pci_dev *pdev = bp->pdev;
2736 for (i = 0; i < bp->nr_vnics; i++) {
2737 vnic = &bp->vnic_info[i];
2739 kfree(vnic->fw_grp_ids);
2740 vnic->fw_grp_ids = NULL;
2742 kfree(vnic->uc_list);
2743 vnic->uc_list = NULL;
2745 if (vnic->mc_list) {
2746 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2747 vnic->mc_list, vnic->mc_list_mapping);
2748 vnic->mc_list = NULL;
2751 if (vnic->rss_table) {
2752 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2754 vnic->rss_table_dma_addr);
2755 vnic->rss_table = NULL;
2758 vnic->rss_hash_key = NULL;
2763 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2765 int i, rc = 0, size;
2766 struct bnxt_vnic_info *vnic;
2767 struct pci_dev *pdev = bp->pdev;
2770 for (i = 0; i < bp->nr_vnics; i++) {
2771 vnic = &bp->vnic_info[i];
2773 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2774 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2777 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2778 if (!vnic->uc_list) {
2785 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2786 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2788 dma_alloc_coherent(&pdev->dev,
2790 &vnic->mc_list_mapping,
2792 if (!vnic->mc_list) {
2798 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2799 max_rings = bp->rx_nr_rings;
2803 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2804 if (!vnic->fw_grp_ids) {
2809 if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
2810 !(vnic->flags & BNXT_VNIC_RSS_FLAG))
2813 /* Allocate rss table and hash key */
2814 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2815 &vnic->rss_table_dma_addr,
2817 if (!vnic->rss_table) {
2822 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2824 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2825 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2833 static void bnxt_free_hwrm_resources(struct bnxt *bp)
2835 struct pci_dev *pdev = bp->pdev;
2837 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2838 bp->hwrm_cmd_resp_dma_addr);
2840 bp->hwrm_cmd_resp_addr = NULL;
2841 if (bp->hwrm_dbg_resp_addr) {
2842 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2843 bp->hwrm_dbg_resp_addr,
2844 bp->hwrm_dbg_resp_dma_addr);
2846 bp->hwrm_dbg_resp_addr = NULL;
2850 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2852 struct pci_dev *pdev = bp->pdev;
2854 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2855 &bp->hwrm_cmd_resp_dma_addr,
2857 if (!bp->hwrm_cmd_resp_addr)
2859 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2860 HWRM_DBG_REG_BUF_SIZE,
2861 &bp->hwrm_dbg_resp_dma_addr,
2863 if (!bp->hwrm_dbg_resp_addr)
2864 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2869 static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
2871 if (bp->hwrm_short_cmd_req_addr) {
2872 struct pci_dev *pdev = bp->pdev;
2874 dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2875 bp->hwrm_short_cmd_req_addr,
2876 bp->hwrm_short_cmd_req_dma_addr);
2877 bp->hwrm_short_cmd_req_addr = NULL;
2881 static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
2883 struct pci_dev *pdev = bp->pdev;
2885 bp->hwrm_short_cmd_req_addr =
2886 dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
2887 &bp->hwrm_short_cmd_req_dma_addr,
2889 if (!bp->hwrm_short_cmd_req_addr)
2895 static void bnxt_free_stats(struct bnxt *bp)
2898 struct pci_dev *pdev = bp->pdev;
2900 if (bp->hw_rx_port_stats) {
2901 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2902 bp->hw_rx_port_stats,
2903 bp->hw_rx_port_stats_map);
2904 bp->hw_rx_port_stats = NULL;
2905 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2911 size = sizeof(struct ctx_hw_stats);
2913 for (i = 0; i < bp->cp_nr_rings; i++) {
2914 struct bnxt_napi *bnapi = bp->bnapi[i];
2915 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2917 if (cpr->hw_stats) {
2918 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2920 cpr->hw_stats = NULL;
2925 static int bnxt_alloc_stats(struct bnxt *bp)
2928 struct pci_dev *pdev = bp->pdev;
2930 size = sizeof(struct ctx_hw_stats);
2932 for (i = 0; i < bp->cp_nr_rings; i++) {
2933 struct bnxt_napi *bnapi = bp->bnapi[i];
2934 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2936 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2942 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2945 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
2946 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2947 sizeof(struct tx_port_stats) + 1024;
2949 bp->hw_rx_port_stats =
2950 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2951 &bp->hw_rx_port_stats_map,
2953 if (!bp->hw_rx_port_stats)
2956 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2958 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2959 sizeof(struct rx_port_stats) + 512;
2960 bp->flags |= BNXT_FLAG_PORT_STATS;
2965 static void bnxt_clear_ring_indices(struct bnxt *bp)
2972 for (i = 0; i < bp->cp_nr_rings; i++) {
2973 struct bnxt_napi *bnapi = bp->bnapi[i];
2974 struct bnxt_cp_ring_info *cpr;
2975 struct bnxt_rx_ring_info *rxr;
2976 struct bnxt_tx_ring_info *txr;
2981 cpr = &bnapi->cp_ring;
2982 cpr->cp_raw_cons = 0;
2984 txr = bnapi->tx_ring;
2990 rxr = bnapi->rx_ring;
2993 rxr->rx_agg_prod = 0;
2994 rxr->rx_sw_agg_prod = 0;
2995 rxr->rx_next_cons = 0;
3000 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
3002 #ifdef CONFIG_RFS_ACCEL
3005 /* Under rtnl_lock and all our NAPIs have been disabled. It's
3006 * safe to delete the hash table.
3008 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
3009 struct hlist_head *head;
3010 struct hlist_node *tmp;
3011 struct bnxt_ntuple_filter *fltr;
3013 head = &bp->ntp_fltr_hash_tbl[i];
3014 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
3015 hlist_del(&fltr->hash);
3020 kfree(bp->ntp_fltr_bmap);
3021 bp->ntp_fltr_bmap = NULL;
3023 bp->ntp_fltr_count = 0;
3027 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
3029 #ifdef CONFIG_RFS_ACCEL
3032 if (!(bp->flags & BNXT_FLAG_RFS))
3035 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
3036 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
3038 bp->ntp_fltr_count = 0;
3039 bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
3043 if (!bp->ntp_fltr_bmap)
3052 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
3054 bnxt_free_vnic_attributes(bp);
3055 bnxt_free_tx_rings(bp);
3056 bnxt_free_rx_rings(bp);
3057 bnxt_free_cp_rings(bp);
3058 bnxt_free_ntp_fltrs(bp, irq_re_init);
3060 bnxt_free_stats(bp);
3061 bnxt_free_ring_grps(bp);
3062 bnxt_free_vnics(bp);
3063 kfree(bp->tx_ring_map);
3064 bp->tx_ring_map = NULL;
3072 bnxt_clear_ring_indices(bp);
3076 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
3078 int i, j, rc, size, arr_size;
3082 /* Allocate bnapi mem pointer array and mem block for
3085 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
3087 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
3088 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
3094 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
3095 bp->bnapi[i] = bnapi;
3096 bp->bnapi[i]->index = i;
3097 bp->bnapi[i]->bp = bp;
3100 bp->rx_ring = kcalloc(bp->rx_nr_rings,
3101 sizeof(struct bnxt_rx_ring_info),
3106 for (i = 0; i < bp->rx_nr_rings; i++) {
3107 bp->rx_ring[i].bnapi = bp->bnapi[i];
3108 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
3111 bp->tx_ring = kcalloc(bp->tx_nr_rings,
3112 sizeof(struct bnxt_tx_ring_info),
3117 bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
3120 if (!bp->tx_ring_map)
3123 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
3126 j = bp->rx_nr_rings;
3128 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
3129 bp->tx_ring[i].bnapi = bp->bnapi[j];
3130 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
3131 bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
3132 if (i >= bp->tx_nr_rings_xdp) {
3133 bp->tx_ring[i].txq_index = i -
3134 bp->tx_nr_rings_xdp;
3135 bp->bnapi[j]->tx_int = bnxt_tx_int;
3137 bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
3138 bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
3142 rc = bnxt_alloc_stats(bp);
3146 rc = bnxt_alloc_ntp_fltrs(bp);
3150 rc = bnxt_alloc_vnics(bp);
3155 bnxt_init_ring_struct(bp);
3157 rc = bnxt_alloc_rx_rings(bp);
3161 rc = bnxt_alloc_tx_rings(bp);
3165 rc = bnxt_alloc_cp_rings(bp);
3169 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
3170 BNXT_VNIC_UCAST_FLAG;
3171 rc = bnxt_alloc_vnic_attributes(bp);
3177 bnxt_free_mem(bp, true);
3181 static void bnxt_disable_int(struct bnxt *bp)
3188 for (i = 0; i < bp->cp_nr_rings; i++) {
3189 struct bnxt_napi *bnapi = bp->bnapi[i];
3190 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3191 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3193 if (ring->fw_ring_id != INVALID_HW_RING_ID)
3194 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3198 static void bnxt_disable_int_sync(struct bnxt *bp)
3202 atomic_inc(&bp->intr_sem);
3204 bnxt_disable_int(bp);
3205 for (i = 0; i < bp->cp_nr_rings; i++)
3206 synchronize_irq(bp->irq_tbl[i].vector);
3209 static void bnxt_enable_int(struct bnxt *bp)
3213 atomic_set(&bp->intr_sem, 0);
3214 for (i = 0; i < bp->cp_nr_rings; i++) {
3215 struct bnxt_napi *bnapi = bp->bnapi[i];
3216 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3218 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
3222 void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
3223 u16 cmpl_ring, u16 target_id)
3225 struct input *req = request;
3227 req->req_type = cpu_to_le16(req_type);
3228 req->cmpl_ring = cpu_to_le16(cmpl_ring);
3229 req->target_id = cpu_to_le16(target_id);
3230 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
3233 static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
3234 int timeout, bool silent)
3236 int i, intr_process, rc, tmo_count;
3237 struct input *req = msg;
3239 __le32 *resp_len, *valid;
3240 u16 cp_ring_id, len = 0;
3241 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
3242 u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
3244 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
3245 memset(resp, 0, PAGE_SIZE);
3246 cp_ring_id = le16_to_cpu(req->cmpl_ring);
3247 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
3249 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
3250 void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
3251 struct hwrm_short_input short_input = {0};
3253 memcpy(short_cmd_req, req, msg_len);
3254 memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
3257 short_input.req_type = req->req_type;
3258 short_input.signature =
3259 cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
3260 short_input.size = cpu_to_le16(msg_len);
3261 short_input.req_addr =
3262 cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
3264 data = (u32 *)&short_input;
3265 msg_len = sizeof(short_input);
3267 /* Sync memory write before updating doorbell */
3270 max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
3273 /* Write request msg to hwrm channel */
3274 __iowrite32_copy(bp->bar0, data, msg_len / 4);
3276 for (i = msg_len; i < max_req_len; i += 4)
3277 writel(0, bp->bar0 + i);
3279 /* currently supports only one outstanding message */
3281 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
3283 /* Ring channel doorbell */
3284 writel(1, bp->bar0 + 0x100);
3287 timeout = DFLT_HWRM_CMD_TIMEOUT;
3290 tmo_count = timeout * 40;
3292 /* Wait until hwrm response cmpl interrupt is processed */
3293 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
3295 usleep_range(25, 40);
3298 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3299 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
3300 le16_to_cpu(req->req_type));
3304 /* Check if response len is updated */
3305 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
3306 for (i = 0; i < tmo_count; i++) {
3307 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3311 usleep_range(25, 40);
3314 if (i >= tmo_count) {
3315 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
3316 timeout, le16_to_cpu(req->req_type),
3317 le16_to_cpu(req->seq_id), len);
3321 /* Last word of resp contains valid bit */
3322 valid = bp->hwrm_cmd_resp_addr + len - 4;
3323 for (i = 0; i < 5; i++) {
3324 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3330 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
3331 timeout, le16_to_cpu(req->req_type),
3332 le16_to_cpu(req->seq_id), len, *valid);
3337 rc = le16_to_cpu(resp->error_code);
3339 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3340 le16_to_cpu(resp->req_type),
3341 le16_to_cpu(resp->seq_id), rc);
3345 int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3347 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
3350 int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3354 mutex_lock(&bp->hwrm_cmd_lock);
3355 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3356 mutex_unlock(&bp->hwrm_cmd_lock);
3360 int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3365 mutex_lock(&bp->hwrm_cmd_lock);
3366 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3367 mutex_unlock(&bp->hwrm_cmd_lock);
3371 int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
3374 struct hwrm_func_drv_rgtr_input req = {0};
3375 DECLARE_BITMAP(async_events_bmap, 256);
3376 u32 *events = (u32 *)async_events_bmap;
3379 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3382 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3384 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3385 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3386 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3388 if (bmap && bmap_size) {
3389 for (i = 0; i < bmap_size; i++) {
3390 if (test_bit(i, bmap))
3391 __set_bit(i, async_events_bmap);
3395 for (i = 0; i < 8; i++)
3396 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3398 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3401 static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3403 struct hwrm_func_drv_rgtr_input req = {0};
3405 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3408 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3409 FUNC_DRV_RGTR_REQ_ENABLES_VER);
3411 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
3412 req.ver_maj = DRV_VER_MAJ;
3413 req.ver_min = DRV_VER_MIN;
3414 req.ver_upd = DRV_VER_UPD;
3417 DECLARE_BITMAP(vf_req_snif_bmap, 256);
3418 u32 *data = (u32 *)vf_req_snif_bmap;
3421 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
3422 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3423 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3425 for (i = 0; i < 8; i++)
3426 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3429 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3432 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3435 static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3437 struct hwrm_func_drv_unrgtr_input req = {0};
3439 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3440 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3443 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3446 struct hwrm_tunnel_dst_port_free_input req = {0};
3448 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3449 req.tunnel_type = tunnel_type;
3451 switch (tunnel_type) {
3452 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3453 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3455 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3456 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3462 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3464 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3469 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3473 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3474 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3476 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3478 req.tunnel_type = tunnel_type;
3479 req.tunnel_dst_port_val = port;
3481 mutex_lock(&bp->hwrm_cmd_lock);
3482 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3484 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3489 switch (tunnel_type) {
3490 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
3491 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3493 case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
3494 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3501 mutex_unlock(&bp->hwrm_cmd_lock);
3505 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3507 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3508 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3510 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
3511 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3513 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3514 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3515 req.mask = cpu_to_le32(vnic->rx_mask);
3516 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3519 #ifdef CONFIG_RFS_ACCEL
3520 static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3521 struct bnxt_ntuple_filter *fltr)
3523 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3525 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3526 req.ntuple_filter_id = fltr->filter_id;
3527 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3530 #define BNXT_NTP_FLTR_FLAGS \
3531 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3532 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3533 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3534 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3535 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3536 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3537 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3538 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3539 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3540 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3541 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3542 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3543 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
3544 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
3546 #define BNXT_NTP_TUNNEL_FLTR_FLAG \
3547 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
3549 static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3550 struct bnxt_ntuple_filter *fltr)
3553 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3554 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3555 bp->hwrm_cmd_resp_addr;
3556 struct flow_keys *keys = &fltr->fkeys;
3557 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3559 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
3560 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
3562 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3564 req.ethertype = htons(ETH_P_IP);
3565 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
3566 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
3567 req.ip_protocol = keys->basic.ip_proto;
3569 if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
3572 req.ethertype = htons(ETH_P_IPV6);
3574 CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
3575 *(struct in6_addr *)&req.src_ipaddr[0] =
3576 keys->addrs.v6addrs.src;
3577 *(struct in6_addr *)&req.dst_ipaddr[0] =
3578 keys->addrs.v6addrs.dst;
3579 for (i = 0; i < 4; i++) {
3580 req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3581 req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
3584 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3585 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3586 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3587 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3589 if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
3590 req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
3592 CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
3595 req.src_port = keys->ports.src;
3596 req.src_port_mask = cpu_to_be16(0xffff);
3597 req.dst_port = keys->ports.dst;
3598 req.dst_port_mask = cpu_to_be16(0xffff);
3600 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
3601 mutex_lock(&bp->hwrm_cmd_lock);
3602 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3604 fltr->filter_id = resp->ntuple_filter_id;
3605 mutex_unlock(&bp->hwrm_cmd_lock);
3610 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3614 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3615 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3617 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
3618 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3619 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3621 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
3622 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
3624 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
3625 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
3626 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3627 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3628 req.l2_addr_mask[0] = 0xff;
3629 req.l2_addr_mask[1] = 0xff;
3630 req.l2_addr_mask[2] = 0xff;
3631 req.l2_addr_mask[3] = 0xff;
3632 req.l2_addr_mask[4] = 0xff;
3633 req.l2_addr_mask[5] = 0xff;
3635 mutex_lock(&bp->hwrm_cmd_lock);
3636 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3638 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3640 mutex_unlock(&bp->hwrm_cmd_lock);
3644 static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3646 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3649 /* Any associated ntuple filters will also be cleared by firmware. */
3650 mutex_lock(&bp->hwrm_cmd_lock);
3651 for (i = 0; i < num_of_vnics; i++) {
3652 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3654 for (j = 0; j < vnic->uc_filter_count; j++) {
3655 struct hwrm_cfa_l2_filter_free_input req = {0};
3657 bnxt_hwrm_cmd_hdr_init(bp, &req,
3658 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3660 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3662 rc = _hwrm_send_message(bp, &req, sizeof(req),
3665 vnic->uc_filter_count = 0;
3667 mutex_unlock(&bp->hwrm_cmd_lock);
3672 static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3674 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3675 struct hwrm_vnic_tpa_cfg_input req = {0};
3677 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3680 u16 mss = bp->dev->mtu - 40;
3681 u32 nsegs, n, segs = 0, flags;
3683 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3684 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3685 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3686 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3687 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3688 if (tpa_flags & BNXT_FLAG_GRO)
3689 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3691 req.flags = cpu_to_le32(flags);
3694 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
3695 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3696 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
3698 /* Number of segs are log2 units, and first packet is not
3699 * included as part of this units.
3701 if (mss <= BNXT_RX_PAGE_SIZE) {
3702 n = BNXT_RX_PAGE_SIZE / mss;
3703 nsegs = (MAX_SKB_FRAGS - 1) * n;
3705 n = mss / BNXT_RX_PAGE_SIZE;
3706 if (mss & (BNXT_RX_PAGE_SIZE - 1))
3708 nsegs = (MAX_SKB_FRAGS - n) / n;
3711 segs = ilog2(nsegs);
3712 req.max_agg_segs = cpu_to_le16(segs);
3713 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
3715 req.min_agg_len = cpu_to_le32(512);
3717 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3719 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3722 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3724 u32 i, j, max_rings;
3725 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3726 struct hwrm_vnic_rss_cfg_input req = {0};
3728 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
3731 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3733 req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
3734 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3735 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3736 max_rings = bp->rx_nr_rings - 1;
3738 max_rings = bp->rx_nr_rings;
3743 /* Fill the RSS indirection table with ring group ids */
3744 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3747 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3750 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3751 req.hash_key_tbl_addr =
3752 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3754 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3755 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3758 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3760 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3761 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3763 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3764 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3765 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3766 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3768 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3769 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3770 /* thresholds not implemented in firmware yet */
3771 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3772 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3773 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3774 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3777 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3780 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3782 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3783 req.rss_cos_lb_ctx_id =
3784 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
3786 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3787 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
3790 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3794 for (i = 0; i < bp->nr_vnics; i++) {
3795 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3797 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3798 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3799 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3802 bp->rsscos_nr_ctxs = 0;
3805 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
3808 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3809 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3810 bp->hwrm_cmd_resp_addr;
3812 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3815 mutex_lock(&bp->hwrm_cmd_lock);
3816 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3818 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
3819 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3820 mutex_unlock(&bp->hwrm_cmd_lock);
3825 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3827 unsigned int ring = 0, grp_idx;
3828 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3829 struct hwrm_vnic_cfg_input req = {0};
3832 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
3834 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3835 /* Only RSS support for now TBD: COS & LB */
3836 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3837 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3838 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3839 VNIC_CFG_REQ_ENABLES_MRU);
3840 } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
3842 cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
3843 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3844 VNIC_CFG_REQ_ENABLES_MRU);
3845 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
3847 req.rss_rule = cpu_to_le16(0xffff);
3850 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3851 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
3852 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3853 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3855 req.cos_rule = cpu_to_le16(0xffff);
3858 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
3860 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
3862 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3863 ring = bp->rx_nr_rings - 1;
3865 grp_idx = bp->rx_ring[ring].bnapi->index;
3866 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3867 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3869 req.lb_rule = cpu_to_le16(0xffff);
3870 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3873 #ifdef CONFIG_BNXT_SRIOV
3875 def_vlan = bp->vf.vlan;
3877 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
3878 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3879 if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
3881 cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
3883 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3886 static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3890 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3891 struct hwrm_vnic_free_input req = {0};
3893 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3895 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3897 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3900 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3905 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3909 for (i = 0; i < bp->nr_vnics; i++)
3910 bnxt_hwrm_vnic_free_one(bp, i);
3913 static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3914 unsigned int start_rx_ring_idx,
3915 unsigned int nr_rings)
3918 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
3919 struct hwrm_vnic_alloc_input req = {0};
3920 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3922 /* map ring groups to this vnic */
3923 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3924 grp_idx = bp->rx_ring[i].bnapi->index;
3925 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
3926 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
3930 bp->vnic_info[vnic_id].fw_grp_ids[j] =
3931 bp->grp_info[grp_idx].fw_grp_id;
3934 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3935 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
3937 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3939 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3941 mutex_lock(&bp->hwrm_cmd_lock);
3942 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3944 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3945 mutex_unlock(&bp->hwrm_cmd_lock);
3949 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
3951 struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
3952 struct hwrm_vnic_qcaps_input req = {0};
3955 if (bp->hwrm_spec_code < 0x10600)
3958 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
3959 mutex_lock(&bp->hwrm_cmd_lock);
3960 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3963 cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
3964 bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
3966 mutex_unlock(&bp->hwrm_cmd_lock);
3970 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3975 mutex_lock(&bp->hwrm_cmd_lock);
3976 for (i = 0; i < bp->rx_nr_rings; i++) {
3977 struct hwrm_ring_grp_alloc_input req = {0};
3978 struct hwrm_ring_grp_alloc_output *resp =
3979 bp->hwrm_cmd_resp_addr;
3980 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
3982 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3984 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3985 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3986 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3987 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
3989 rc = _hwrm_send_message(bp, &req, sizeof(req),
3994 bp->grp_info[grp_idx].fw_grp_id =
3995 le32_to_cpu(resp->ring_group_id);
3997 mutex_unlock(&bp->hwrm_cmd_lock);
4001 static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
4005 struct hwrm_ring_grp_free_input req = {0};
4010 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
4012 mutex_lock(&bp->hwrm_cmd_lock);
4013 for (i = 0; i < bp->cp_nr_rings; i++) {
4014 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
4017 cpu_to_le32(bp->grp_info[i].fw_grp_id);
4019 rc = _hwrm_send_message(bp, &req, sizeof(req),
4023 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4025 mutex_unlock(&bp->hwrm_cmd_lock);
4029 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
4030 struct bnxt_ring_struct *ring,
4031 u32 ring_type, u32 map_index,
4034 int rc = 0, err = 0;
4035 struct hwrm_ring_alloc_input req = {0};
4036 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4039 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
4042 if (ring->nr_pages > 1) {
4043 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
4044 /* Page size is in log2 units */
4045 req.page_size = BNXT_PAGE_SHIFT;
4046 req.page_tbl_depth = 1;
4048 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
4051 /* Association of ring index with doorbell index and MSIX number */
4052 req.logical_id = cpu_to_le16(map_index);
4054 switch (ring_type) {
4055 case HWRM_RING_ALLOC_TX:
4056 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
4057 /* Association of transmit ring with completion ring */
4059 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
4060 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
4061 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
4062 req.queue_id = cpu_to_le16(ring->queue_id);
4064 case HWRM_RING_ALLOC_RX:
4065 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4066 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
4068 case HWRM_RING_ALLOC_AGG:
4069 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
4070 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
4072 case HWRM_RING_ALLOC_CMPL:
4073 req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
4074 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
4075 if (bp->flags & BNXT_FLAG_USING_MSIX)
4076 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
4079 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
4084 mutex_lock(&bp->hwrm_cmd_lock);
4085 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4086 err = le16_to_cpu(resp->error_code);
4087 ring_id = le16_to_cpu(resp->ring_id);
4088 mutex_unlock(&bp->hwrm_cmd_lock);
4091 switch (ring_type) {
4092 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
4093 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
4097 case RING_FREE_REQ_RING_TYPE_RX:
4098 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
4102 case RING_FREE_REQ_RING_TYPE_TX:
4103 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
4108 netdev_err(bp->dev, "Invalid ring\n");
4112 ring->fw_ring_id = ring_id;
4116 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
4121 struct hwrm_func_cfg_input req = {0};
4123 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4124 req.fid = cpu_to_le16(0xffff);
4125 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4126 req.async_event_cr = cpu_to_le16(idx);
4127 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4129 struct hwrm_func_vf_cfg_input req = {0};
4131 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
4133 cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
4134 req.async_event_cr = cpu_to_le16(idx);
4135 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4140 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
4144 for (i = 0; i < bp->cp_nr_rings; i++) {
4145 struct bnxt_napi *bnapi = bp->bnapi[i];
4146 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4147 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4149 cpr->cp_doorbell = bp->bar1 + i * 0x80;
4150 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
4151 INVALID_STATS_CTX_ID);
4154 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4155 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
4158 rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
4160 netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
4164 for (i = 0; i < bp->tx_nr_rings; i++) {
4165 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4166 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4167 u32 map_idx = txr->bnapi->index;
4168 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
4170 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
4171 map_idx, fw_stats_ctx);
4174 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
4177 for (i = 0; i < bp->rx_nr_rings; i++) {
4178 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4179 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
4180 u32 map_idx = rxr->bnapi->index;
4182 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
4183 map_idx, INVALID_STATS_CTX_ID);
4186 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
4187 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
4188 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
4191 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4192 for (i = 0; i < bp->rx_nr_rings; i++) {
4193 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4194 struct bnxt_ring_struct *ring =
4195 &rxr->rx_agg_ring_struct;
4196 u32 grp_idx = rxr->bnapi->index;
4197 u32 map_idx = grp_idx + bp->rx_nr_rings;
4199 rc = hwrm_ring_alloc_send_msg(bp, ring,
4200 HWRM_RING_ALLOC_AGG,
4202 INVALID_STATS_CTX_ID);
4206 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
4207 writel(DB_KEY_RX | rxr->rx_agg_prod,
4208 rxr->rx_agg_doorbell);
4209 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
4216 static int hwrm_ring_free_send_msg(struct bnxt *bp,
4217 struct bnxt_ring_struct *ring,
4218 u32 ring_type, int cmpl_ring_id)
4221 struct hwrm_ring_free_input req = {0};
4222 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
4225 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
4226 req.ring_type = ring_type;
4227 req.ring_id = cpu_to_le16(ring->fw_ring_id);
4229 mutex_lock(&bp->hwrm_cmd_lock);
4230 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4231 error_code = le16_to_cpu(resp->error_code);
4232 mutex_unlock(&bp->hwrm_cmd_lock);
4234 if (rc || error_code) {
4235 switch (ring_type) {
4236 case RING_FREE_REQ_RING_TYPE_L2_CMPL:
4237 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
4240 case RING_FREE_REQ_RING_TYPE_RX:
4241 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
4244 case RING_FREE_REQ_RING_TYPE_TX:
4245 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
4249 netdev_err(bp->dev, "Invalid ring\n");
4256 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
4263 for (i = 0; i < bp->tx_nr_rings; i++) {
4264 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4265 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4266 u32 grp_idx = txr->bnapi->index;
4267 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4269 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4270 hwrm_ring_free_send_msg(bp, ring,
4271 RING_FREE_REQ_RING_TYPE_TX,
4272 close_path ? cmpl_ring_id :
4273 INVALID_HW_RING_ID);
4274 ring->fw_ring_id = INVALID_HW_RING_ID;
4278 for (i = 0; i < bp->rx_nr_rings; i++) {
4279 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4280 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
4281 u32 grp_idx = rxr->bnapi->index;
4282 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4284 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4285 hwrm_ring_free_send_msg(bp, ring,
4286 RING_FREE_REQ_RING_TYPE_RX,
4287 close_path ? cmpl_ring_id :
4288 INVALID_HW_RING_ID);
4289 ring->fw_ring_id = INVALID_HW_RING_ID;
4290 bp->grp_info[grp_idx].rx_fw_ring_id =
4295 for (i = 0; i < bp->rx_nr_rings; i++) {
4296 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4297 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
4298 u32 grp_idx = rxr->bnapi->index;
4299 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
4301 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4302 hwrm_ring_free_send_msg(bp, ring,
4303 RING_FREE_REQ_RING_TYPE_RX,
4304 close_path ? cmpl_ring_id :
4305 INVALID_HW_RING_ID);
4306 ring->fw_ring_id = INVALID_HW_RING_ID;
4307 bp->grp_info[grp_idx].agg_fw_ring_id =
4312 /* The completion rings are about to be freed. After that the
4313 * IRQ doorbell will not work anymore. So we need to disable
4316 bnxt_disable_int_sync(bp);
4318 for (i = 0; i < bp->cp_nr_rings; i++) {
4319 struct bnxt_napi *bnapi = bp->bnapi[i];
4320 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4321 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4323 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
4324 hwrm_ring_free_send_msg(bp, ring,
4325 RING_FREE_REQ_RING_TYPE_L2_CMPL,
4326 INVALID_HW_RING_ID);
4327 ring->fw_ring_id = INVALID_HW_RING_ID;
4328 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4333 /* Caller must hold bp->hwrm_cmd_lock */
4334 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
4336 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4337 struct hwrm_func_qcfg_input req = {0};
4340 if (bp->hwrm_spec_code < 0x10601)
4343 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4344 req.fid = cpu_to_le16(fid);
4345 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4347 *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
4352 static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
4354 struct hwrm_func_cfg_input req = {0};
4357 if (bp->hwrm_spec_code < 0x10601)
4363 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
4364 req.fid = cpu_to_le16(0xffff);
4365 req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
4366 req.num_tx_rings = cpu_to_le16(*tx_rings);
4367 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4371 mutex_lock(&bp->hwrm_cmd_lock);
4372 rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
4373 mutex_unlock(&bp->hwrm_cmd_lock);
4377 static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
4378 u32 buf_tmrs, u16 flags,
4379 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
4381 req->flags = cpu_to_le16(flags);
4382 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
4383 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
4384 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
4385 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
4386 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
4387 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
4388 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
4389 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
4392 int bnxt_hwrm_set_coal(struct bnxt *bp)
4395 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
4397 u16 max_buf, max_buf_irq;
4398 u16 buf_tmr, buf_tmr_irq;
4401 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
4402 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4403 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
4404 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
4406 /* Each rx completion (2 records) should be DMAed immediately.
4407 * DMA 1/4 of the completion buffers at a time.
4409 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
4410 /* max_buf must not be zero */
4411 max_buf = clamp_t(u16, max_buf, 1, 63);
4412 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
4413 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
4414 /* buf timer set to 1/4 of interrupt timer */
4415 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4416 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
4417 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4419 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4421 /* RING_IDLE generates more IRQs for lower latency. Enable it only
4422 * if coal_ticks is less than 25 us.
4424 if (bp->rx_coal_ticks < 25)
4425 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
4427 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4428 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
4430 /* max_buf must not be zero */
4431 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
4432 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
4433 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
4434 /* buf timer set to 1/4 of interrupt timer */
4435 buf_tmr = max_t(u16, buf_tmr / 4, 1);
4436 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
4437 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
4439 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
4440 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
4441 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
4443 mutex_lock(&bp->hwrm_cmd_lock);
4444 for (i = 0; i < bp->cp_nr_rings; i++) {
4445 struct bnxt_napi *bnapi = bp->bnapi[i];
4448 if (!bnapi->rx_ring)
4450 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4452 rc = _hwrm_send_message(bp, req, sizeof(*req),
4457 mutex_unlock(&bp->hwrm_cmd_lock);
4461 static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4464 struct hwrm_stat_ctx_free_input req = {0};
4469 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4472 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4474 mutex_lock(&bp->hwrm_cmd_lock);
4475 for (i = 0; i < bp->cp_nr_rings; i++) {
4476 struct bnxt_napi *bnapi = bp->bnapi[i];
4477 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4479 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4480 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4482 rc = _hwrm_send_message(bp, &req, sizeof(req),
4487 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4490 mutex_unlock(&bp->hwrm_cmd_lock);
4494 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4497 struct hwrm_stat_ctx_alloc_input req = {0};
4498 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4500 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4503 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4505 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
4507 mutex_lock(&bp->hwrm_cmd_lock);
4508 for (i = 0; i < bp->cp_nr_rings; i++) {
4509 struct bnxt_napi *bnapi = bp->bnapi[i];
4510 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4512 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4514 rc = _hwrm_send_message(bp, &req, sizeof(req),
4519 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4521 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4523 mutex_unlock(&bp->hwrm_cmd_lock);
4527 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4529 struct hwrm_func_qcfg_input req = {0};
4530 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
4533 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4534 req.fid = cpu_to_le16(0xffff);
4535 mutex_lock(&bp->hwrm_cmd_lock);
4536 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4538 goto func_qcfg_exit;
4540 #ifdef CONFIG_BNXT_SRIOV
4542 struct bnxt_vf_info *vf = &bp->vf;
4544 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4548 u16 flags = le16_to_cpu(resp->flags);
4550 if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
4551 FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED))
4552 bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
4553 if (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST)
4554 bp->flags |= BNXT_FLAG_MULTI_HOST;
4557 switch (resp->port_partition_type) {
4558 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4559 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4560 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4561 bp->port_partition_type = resp->port_partition_type;
4566 mutex_unlock(&bp->hwrm_cmd_lock);
4570 static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
4573 struct hwrm_func_qcaps_input req = {0};
4574 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4576 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4577 req.fid = cpu_to_le16(0xffff);
4579 mutex_lock(&bp->hwrm_cmd_lock);
4580 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4582 goto hwrm_func_qcaps_exit;
4584 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
4585 bp->flags |= BNXT_FLAG_ROCEV1_CAP;
4586 if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
4587 bp->flags |= BNXT_FLAG_ROCEV2_CAP;
4589 bp->tx_push_thresh = 0;
4591 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4592 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4595 struct bnxt_pf_info *pf = &bp->pf;
4597 pf->fw_fid = le16_to_cpu(resp->fid);
4598 pf->port_id = le16_to_cpu(resp->port_id);
4599 bp->dev->dev_port = pf->port_id;
4600 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
4601 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
4602 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4603 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4604 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4605 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4606 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4607 if (!pf->max_hw_ring_grps)
4608 pf->max_hw_ring_grps = pf->max_tx_rings;
4609 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4610 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4611 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4612 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4613 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4614 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4615 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4616 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4617 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4618 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4619 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4621 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED))
4622 bp->flags |= BNXT_FLAG_WOL_CAP;
4624 #ifdef CONFIG_BNXT_SRIOV
4625 struct bnxt_vf_info *vf = &bp->vf;
4627 vf->fw_fid = le16_to_cpu(resp->fid);
4629 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4630 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4631 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4632 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
4633 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4634 if (!vf->max_hw_ring_grps)
4635 vf->max_hw_ring_grps = vf->max_tx_rings;
4636 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4637 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4638 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4640 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
4641 mutex_unlock(&bp->hwrm_cmd_lock);
4643 if (is_valid_ether_addr(vf->mac_addr)) {
4644 /* overwrite netdev dev_adr with admin VF MAC */
4645 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
4647 eth_hw_addr_random(bp->dev);
4648 rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
4654 hwrm_func_qcaps_exit:
4655 mutex_unlock(&bp->hwrm_cmd_lock);
4659 static int bnxt_hwrm_func_reset(struct bnxt *bp)
4661 struct hwrm_func_reset_input req = {0};
4663 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4666 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4669 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4672 struct hwrm_queue_qportcfg_input req = {0};
4673 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4676 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4678 mutex_lock(&bp->hwrm_cmd_lock);
4679 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4683 if (!resp->max_configurable_queues) {
4687 bp->max_tc = resp->max_configurable_queues;
4688 bp->max_lltc = resp->max_configurable_lossless_queues;
4689 if (bp->max_tc > BNXT_MAX_QUEUE)
4690 bp->max_tc = BNXT_MAX_QUEUE;
4692 if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
4695 if (bp->max_lltc > bp->max_tc)
4696 bp->max_lltc = bp->max_tc;
4698 qptr = &resp->queue_id0;
4699 for (i = 0; i < bp->max_tc; i++) {
4700 bp->q_info[i].queue_id = *qptr++;
4701 bp->q_info[i].queue_profile = *qptr++;
4705 mutex_unlock(&bp->hwrm_cmd_lock);
4709 static int bnxt_hwrm_ver_get(struct bnxt *bp)
4712 struct hwrm_ver_get_input req = {0};
4713 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4716 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
4717 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4718 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4719 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4720 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4721 mutex_lock(&bp->hwrm_cmd_lock);
4722 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4724 goto hwrm_ver_get_exit;
4726 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4728 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4729 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
4730 if (resp->hwrm_intf_maj < 1) {
4731 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
4732 resp->hwrm_intf_maj, resp->hwrm_intf_min,
4733 resp->hwrm_intf_upd);
4734 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
4736 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
4737 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4738 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4740 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4741 if (!bp->hwrm_cmd_timeout)
4742 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4744 if (resp->hwrm_intf_maj >= 1)
4745 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4747 bp->chip_num = le16_to_cpu(resp->chip_num);
4748 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4750 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
4752 dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
4753 if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
4754 (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
4755 bp->flags |= BNXT_FLAG_SHORT_CMD;
4758 mutex_unlock(&bp->hwrm_cmd_lock);
4762 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
4764 #if IS_ENABLED(CONFIG_RTC_LIB)
4765 struct hwrm_fw_set_time_input req = {0};
4769 if (bp->hwrm_spec_code < 0x10400)
4772 do_gettimeofday(&tv);
4773 rtc_time_to_tm(tv.tv_sec, &tm);
4774 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
4775 req.year = cpu_to_le16(1900 + tm.tm_year);
4776 req.month = 1 + tm.tm_mon;
4777 req.day = tm.tm_mday;
4778 req.hour = tm.tm_hour;
4779 req.minute = tm.tm_min;
4780 req.second = tm.tm_sec;
4781 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4787 static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4790 struct bnxt_pf_info *pf = &bp->pf;
4791 struct hwrm_port_qstats_input req = {0};
4793 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4796 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4797 req.port_id = cpu_to_le16(pf->port_id);
4798 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4799 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4800 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4804 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4806 if (bp->vxlan_port_cnt) {
4807 bnxt_hwrm_tunnel_dst_port_free(
4808 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4810 bp->vxlan_port_cnt = 0;
4811 if (bp->nge_port_cnt) {
4812 bnxt_hwrm_tunnel_dst_port_free(
4813 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4815 bp->nge_port_cnt = 0;
4818 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4824 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4825 for (i = 0; i < bp->nr_vnics; i++) {
4826 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4828 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4836 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4840 for (i = 0; i < bp->nr_vnics; i++)
4841 bnxt_hwrm_vnic_set_rss(bp, i, false);
4844 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4847 if (bp->vnic_info) {
4848 bnxt_hwrm_clear_vnic_filter(bp);
4849 /* clear all RSS setting before free vnic ctx */
4850 bnxt_hwrm_clear_vnic_rss(bp);
4851 bnxt_hwrm_vnic_ctx_free(bp);
4852 /* before free the vnic, undo the vnic tpa settings */
4853 if (bp->flags & BNXT_FLAG_TPA)
4854 bnxt_set_tpa(bp, false);
4855 bnxt_hwrm_vnic_free(bp);
4857 bnxt_hwrm_ring_free(bp, close_path);
4858 bnxt_hwrm_ring_grp_free(bp);
4860 bnxt_hwrm_stat_ctx_free(bp);
4861 bnxt_hwrm_free_tunnel_ports(bp);
4865 static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4867 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
4870 if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
4873 /* allocate context for vnic */
4874 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
4876 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4878 goto vnic_setup_err;
4880 bp->rsscos_nr_ctxs++;
4882 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4883 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4885 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4887 goto vnic_setup_err;
4889 bp->rsscos_nr_ctxs++;
4893 /* configure default vnic, ring grp */
4894 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4896 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4898 goto vnic_setup_err;
4901 /* Enable RSS hashing on vnic */
4902 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4904 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4906 goto vnic_setup_err;
4909 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4910 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4912 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4921 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4923 #ifdef CONFIG_RFS_ACCEL
4926 for (i = 0; i < bp->rx_nr_rings; i++) {
4927 struct bnxt_vnic_info *vnic;
4928 u16 vnic_id = i + 1;
4931 if (vnic_id >= bp->nr_vnics)
4934 vnic = &bp->vnic_info[vnic_id];
4935 vnic->flags |= BNXT_VNIC_RFS_FLAG;
4936 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
4937 vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
4938 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
4940 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4944 rc = bnxt_setup_vnic(bp, vnic_id);
4954 /* Allow PF and VF with default VLAN to be in promiscuous mode */
4955 static bool bnxt_promisc_ok(struct bnxt *bp)
4957 #ifdef CONFIG_BNXT_SRIOV
4958 if (BNXT_VF(bp) && !bp->vf.vlan)
4964 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4966 unsigned int rc = 0;
4968 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4970 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4975 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4977 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4984 static int bnxt_cfg_rx_mode(struct bnxt *);
4985 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
4987 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4989 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
4991 unsigned int rx_nr_rings = bp->rx_nr_rings;
4994 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4996 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
5002 rc = bnxt_hwrm_ring_alloc(bp);
5004 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
5008 rc = bnxt_hwrm_ring_grp_alloc(bp);
5010 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
5014 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5017 /* default vnic 0 */
5018 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
5020 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
5024 rc = bnxt_setup_vnic(bp, 0);
5028 if (bp->flags & BNXT_FLAG_RFS) {
5029 rc = bnxt_alloc_rfs_vnics(bp);
5034 if (bp->flags & BNXT_FLAG_TPA) {
5035 rc = bnxt_set_tpa(bp, true);
5041 bnxt_update_vf_mac(bp);
5043 /* Filter for default vnic 0 */
5044 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
5046 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
5049 vnic->uc_filter_count = 1;
5051 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
5053 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
5054 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5056 if (bp->dev->flags & IFF_ALLMULTI) {
5057 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5058 vnic->mc_list_count = 0;
5062 bnxt_mc_list_updated(bp, &mask);
5063 vnic->rx_mask |= mask;
5066 rc = bnxt_cfg_rx_mode(bp);
5070 rc = bnxt_hwrm_set_coal(bp);
5072 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
5075 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5076 rc = bnxt_setup_nitroa0_vnic(bp);
5078 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
5083 bnxt_hwrm_func_qcfg(bp);
5084 netdev_update_features(bp->dev);
5090 bnxt_hwrm_resource_free(bp, 0, true);
5095 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
5097 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
5101 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
5103 bnxt_init_cp_rings(bp);
5104 bnxt_init_rx_rings(bp);
5105 bnxt_init_tx_rings(bp);
5106 bnxt_init_ring_grps(bp, irq_re_init);
5107 bnxt_init_vnics(bp);
5109 return bnxt_init_chip(bp, irq_re_init);
5112 static int bnxt_set_real_num_queues(struct bnxt *bp)
5115 struct net_device *dev = bp->dev;
5117 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
5118 bp->tx_nr_rings_xdp);
5122 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
5126 #ifdef CONFIG_RFS_ACCEL
5127 if (bp->flags & BNXT_FLAG_RFS)
5128 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
5134 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
5137 int _rx = *rx, _tx = *tx;
5140 *rx = min_t(int, _rx, max);
5141 *tx = min_t(int, _tx, max);
5146 while (_rx + _tx > max) {
5147 if (_rx > _tx && _rx > 1)
5158 static void bnxt_setup_msix(struct bnxt *bp)
5160 const int len = sizeof(bp->irq_tbl[0].name);
5161 struct net_device *dev = bp->dev;
5164 tcs = netdev_get_num_tc(dev);
5168 for (i = 0; i < tcs; i++) {
5169 count = bp->tx_nr_rings_per_tc;
5171 netdev_set_tc_queue(dev, i, count, off);
5175 for (i = 0; i < bp->cp_nr_rings; i++) {
5178 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5180 else if (i < bp->rx_nr_rings)
5185 snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
5187 bp->irq_tbl[i].handler = bnxt_msix;
5191 static void bnxt_setup_inta(struct bnxt *bp)
5193 const int len = sizeof(bp->irq_tbl[0].name);
5195 if (netdev_get_num_tc(bp->dev))
5196 netdev_reset_tc(bp->dev);
5198 snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
5200 bp->irq_tbl[0].handler = bnxt_inta;
5203 static int bnxt_setup_int_mode(struct bnxt *bp)
5207 if (bp->flags & BNXT_FLAG_USING_MSIX)
5208 bnxt_setup_msix(bp);
5210 bnxt_setup_inta(bp);
5212 rc = bnxt_set_real_num_queues(bp);
5216 #ifdef CONFIG_RFS_ACCEL
5217 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
5219 #if defined(CONFIG_BNXT_SRIOV)
5221 return bp->vf.max_rsscos_ctxs;
5223 return bp->pf.max_rsscos_ctxs;
5226 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
5228 #if defined(CONFIG_BNXT_SRIOV)
5230 return bp->vf.max_vnics;
5232 return bp->pf.max_vnics;
5236 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
5238 #if defined(CONFIG_BNXT_SRIOV)
5240 return bp->vf.max_stat_ctxs;
5242 return bp->pf.max_stat_ctxs;
5245 void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
5247 #if defined(CONFIG_BNXT_SRIOV)
5249 bp->vf.max_stat_ctxs = max;
5252 bp->pf.max_stat_ctxs = max;
5255 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
5257 #if defined(CONFIG_BNXT_SRIOV)
5259 return bp->vf.max_cp_rings;
5261 return bp->pf.max_cp_rings;
5264 void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
5266 #if defined(CONFIG_BNXT_SRIOV)
5268 bp->vf.max_cp_rings = max;
5271 bp->pf.max_cp_rings = max;
5274 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
5276 #if defined(CONFIG_BNXT_SRIOV)
5278 return min_t(unsigned int, bp->vf.max_irqs,
5279 bp->vf.max_cp_rings);
5281 return min_t(unsigned int, bp->pf.max_irqs, bp->pf.max_cp_rings);
5284 void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
5286 #if defined(CONFIG_BNXT_SRIOV)
5288 bp->vf.max_irqs = max_irqs;
5291 bp->pf.max_irqs = max_irqs;
5294 static int bnxt_init_msix(struct bnxt *bp)
5296 int i, total_vecs, rc = 0, min = 1;
5297 struct msix_entry *msix_ent;
5299 total_vecs = bnxt_get_max_func_irqs(bp);
5300 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
5304 for (i = 0; i < total_vecs; i++) {
5305 msix_ent[i].entry = i;
5306 msix_ent[i].vector = 0;
5309 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
5312 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
5313 if (total_vecs < 0) {
5315 goto msix_setup_exit;
5318 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
5320 for (i = 0; i < total_vecs; i++)
5321 bp->irq_tbl[i].vector = msix_ent[i].vector;
5323 bp->total_irqs = total_vecs;
5324 /* Trim rings based upon num of vectors allocated */
5325 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
5326 total_vecs, min == 1);
5328 goto msix_setup_exit;
5330 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
5331 bp->cp_nr_rings = (min == 1) ?
5332 max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
5333 bp->tx_nr_rings + bp->rx_nr_rings;
5337 goto msix_setup_exit;
5339 bp->flags |= BNXT_FLAG_USING_MSIX;
5344 netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
5347 pci_disable_msix(bp->pdev);
5352 static int bnxt_init_inta(struct bnxt *bp)
5354 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
5359 bp->rx_nr_rings = 1;
5360 bp->tx_nr_rings = 1;
5361 bp->cp_nr_rings = 1;
5362 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
5363 bp->flags |= BNXT_FLAG_SHARED_RINGS;
5364 bp->irq_tbl[0].vector = bp->pdev->irq;
5368 static int bnxt_init_int_mode(struct bnxt *bp)
5372 if (bp->flags & BNXT_FLAG_MSIX_CAP)
5373 rc = bnxt_init_msix(bp);
5375 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
5376 /* fallback to INTA */
5377 rc = bnxt_init_inta(bp);
5382 static void bnxt_clear_int_mode(struct bnxt *bp)
5384 if (bp->flags & BNXT_FLAG_USING_MSIX)
5385 pci_disable_msix(bp->pdev);
5389 bp->flags &= ~BNXT_FLAG_USING_MSIX;
5392 static void bnxt_free_irq(struct bnxt *bp)
5394 struct bnxt_irq *irq;
5397 #ifdef CONFIG_RFS_ACCEL
5398 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
5399 bp->dev->rx_cpu_rmap = NULL;
5404 for (i = 0; i < bp->cp_nr_rings; i++) {
5405 irq = &bp->irq_tbl[i];
5407 free_irq(irq->vector, bp->bnapi[i]);
5412 static int bnxt_request_irq(struct bnxt *bp)
5415 unsigned long flags = 0;
5416 #ifdef CONFIG_RFS_ACCEL
5417 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
5420 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
5421 flags = IRQF_SHARED;
5423 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
5424 struct bnxt_irq *irq = &bp->irq_tbl[i];
5425 #ifdef CONFIG_RFS_ACCEL
5426 if (rmap && bp->bnapi[i]->rx_ring) {
5427 rc = irq_cpu_rmap_add(rmap, irq->vector);
5429 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
5434 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
5444 static void bnxt_del_napi(struct bnxt *bp)
5451 for (i = 0; i < bp->cp_nr_rings; i++) {
5452 struct bnxt_napi *bnapi = bp->bnapi[i];
5454 napi_hash_del(&bnapi->napi);
5455 netif_napi_del(&bnapi->napi);
5457 /* We called napi_hash_del() before netif_napi_del(), we need
5458 * to respect an RCU grace period before freeing napi structures.
5463 static void bnxt_init_napi(struct bnxt *bp)
5466 unsigned int cp_nr_rings = bp->cp_nr_rings;
5467 struct bnxt_napi *bnapi;
5469 if (bp->flags & BNXT_FLAG_USING_MSIX) {
5470 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
5472 for (i = 0; i < cp_nr_rings; i++) {
5473 bnapi = bp->bnapi[i];
5474 netif_napi_add(bp->dev, &bnapi->napi,
5477 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
5478 bnapi = bp->bnapi[cp_nr_rings];
5479 netif_napi_add(bp->dev, &bnapi->napi,
5480 bnxt_poll_nitroa0, 64);
5483 bnapi = bp->bnapi[0];
5484 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
5488 static void bnxt_disable_napi(struct bnxt *bp)
5495 for (i = 0; i < bp->cp_nr_rings; i++)
5496 napi_disable(&bp->bnapi[i]->napi);
5499 static void bnxt_enable_napi(struct bnxt *bp)
5503 for (i = 0; i < bp->cp_nr_rings; i++) {
5504 bp->bnapi[i]->in_reset = false;
5505 napi_enable(&bp->bnapi[i]->napi);
5509 void bnxt_tx_disable(struct bnxt *bp)
5512 struct bnxt_tx_ring_info *txr;
5513 struct netdev_queue *txq;
5516 for (i = 0; i < bp->tx_nr_rings; i++) {
5517 txr = &bp->tx_ring[i];
5518 txq = netdev_get_tx_queue(bp->dev, i);
5519 txr->dev_state = BNXT_DEV_STATE_CLOSING;
5522 /* Stop all TX queues */
5523 netif_tx_disable(bp->dev);
5524 netif_carrier_off(bp->dev);
5527 void bnxt_tx_enable(struct bnxt *bp)
5530 struct bnxt_tx_ring_info *txr;
5531 struct netdev_queue *txq;
5533 for (i = 0; i < bp->tx_nr_rings; i++) {
5534 txr = &bp->tx_ring[i];
5535 txq = netdev_get_tx_queue(bp->dev, i);
5538 netif_tx_wake_all_queues(bp->dev);
5539 if (bp->link_info.link_up)
5540 netif_carrier_on(bp->dev);
5543 static void bnxt_report_link(struct bnxt *bp)
5545 if (bp->link_info.link_up) {
5547 const char *flow_ctrl;
5551 netif_carrier_on(bp->dev);
5552 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
5556 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
5557 flow_ctrl = "ON - receive & transmit";
5558 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
5559 flow_ctrl = "ON - transmit";
5560 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
5561 flow_ctrl = "ON - receive";
5564 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
5565 netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
5566 speed, duplex, flow_ctrl);
5567 if (bp->flags & BNXT_FLAG_EEE_CAP)
5568 netdev_info(bp->dev, "EEE is %s\n",
5569 bp->eee.eee_active ? "active" :
5571 fec = bp->link_info.fec_cfg;
5572 if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
5573 netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
5574 (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
5575 (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
5576 (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
5578 netif_carrier_off(bp->dev);
5579 netdev_err(bp->dev, "NIC Link is Down\n");
5583 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
5586 struct hwrm_port_phy_qcaps_input req = {0};
5587 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5588 struct bnxt_link_info *link_info = &bp->link_info;
5590 if (bp->hwrm_spec_code < 0x10201)
5593 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5595 mutex_lock(&bp->hwrm_cmd_lock);
5596 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5598 goto hwrm_phy_qcaps_exit;
5600 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5601 struct ethtool_eee *eee = &bp->eee;
5602 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5604 bp->flags |= BNXT_FLAG_EEE_CAP;
5605 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5606 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5607 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5608 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5609 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5611 if (resp->supported_speeds_auto_mode)
5612 link_info->support_auto_speeds =
5613 le16_to_cpu(resp->supported_speeds_auto_mode);
5615 hwrm_phy_qcaps_exit:
5616 mutex_unlock(&bp->hwrm_cmd_lock);
5620 static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5623 struct bnxt_link_info *link_info = &bp->link_info;
5624 struct hwrm_port_phy_qcfg_input req = {0};
5625 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5626 u8 link_up = link_info->link_up;
5629 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5631 mutex_lock(&bp->hwrm_cmd_lock);
5632 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5634 mutex_unlock(&bp->hwrm_cmd_lock);
5638 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5639 link_info->phy_link_status = resp->link;
5640 link_info->duplex = resp->duplex;
5641 link_info->pause = resp->pause;
5642 link_info->auto_mode = resp->auto_mode;
5643 link_info->auto_pause_setting = resp->auto_pause;
5644 link_info->lp_pause = resp->link_partner_adv_pause;
5645 link_info->force_pause_setting = resp->force_pause;
5646 link_info->duplex_setting = resp->duplex;
5647 if (link_info->phy_link_status == BNXT_LINK_LINK)
5648 link_info->link_speed = le16_to_cpu(resp->link_speed);
5650 link_info->link_speed = 0;
5651 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
5652 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5653 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
5654 link_info->lp_auto_link_speeds =
5655 le16_to_cpu(resp->link_partner_adv_speeds);
5656 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5657 link_info->phy_ver[0] = resp->phy_maj;
5658 link_info->phy_ver[1] = resp->phy_min;
5659 link_info->phy_ver[2] = resp->phy_bld;
5660 link_info->media_type = resp->media_type;
5661 link_info->phy_type = resp->phy_type;
5662 link_info->transceiver = resp->xcvr_pkg_type;
5663 link_info->phy_addr = resp->eee_config_phy_addr &
5664 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
5665 link_info->module_status = resp->module_status;
5667 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5668 struct ethtool_eee *eee = &bp->eee;
5671 eee->eee_active = 0;
5672 if (resp->eee_config_phy_addr &
5673 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5674 eee->eee_active = 1;
5675 fw_speeds = le16_to_cpu(
5676 resp->link_partner_adv_eee_link_speed_mask);
5677 eee->lp_advertised =
5678 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5681 /* Pull initial EEE config */
5682 if (!chng_link_state) {
5683 if (resp->eee_config_phy_addr &
5684 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5685 eee->eee_enabled = 1;
5687 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5689 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5691 if (resp->eee_config_phy_addr &
5692 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5695 eee->tx_lpi_enabled = 1;
5696 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5697 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5698 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5703 link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
5704 if (bp->hwrm_spec_code >= 0x10504)
5705 link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
5707 /* TODO: need to add more logic to report VF link */
5708 if (chng_link_state) {
5709 if (link_info->phy_link_status == BNXT_LINK_LINK)
5710 link_info->link_up = 1;
5712 link_info->link_up = 0;
5713 if (link_up != link_info->link_up)
5714 bnxt_report_link(bp);
5716 /* alwasy link down if not require to update link state */
5717 link_info->link_up = 0;
5719 mutex_unlock(&bp->hwrm_cmd_lock);
5721 diff = link_info->support_auto_speeds ^ link_info->advertising;
5722 if ((link_info->support_auto_speeds | diff) !=
5723 link_info->support_auto_speeds) {
5724 /* An advertised speed is no longer supported, so we need to
5725 * update the advertisement settings. Caller holds RTNL
5726 * so we can modify link settings.
5728 link_info->advertising = link_info->support_auto_speeds;
5729 if (link_info->autoneg & BNXT_AUTONEG_SPEED)
5730 bnxt_hwrm_set_link_setting(bp, true, false);
5735 static void bnxt_get_port_module_status(struct bnxt *bp)
5737 struct bnxt_link_info *link_info = &bp->link_info;
5738 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5741 if (bnxt_update_link(bp, true))
5744 module_status = link_info->module_status;
5745 switch (module_status) {
5746 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5747 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5748 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5749 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5751 if (bp->hwrm_spec_code >= 0x10201) {
5752 netdev_warn(bp->dev, "Module part number %s\n",
5753 resp->phy_vendor_partnumber);
5755 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5756 netdev_warn(bp->dev, "TX is disabled\n");
5757 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5758 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5763 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5765 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
5766 if (bp->hwrm_spec_code >= 0x10201)
5768 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
5769 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5770 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5771 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5772 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
5774 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5776 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5777 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5778 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5779 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5781 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
5782 if (bp->hwrm_spec_code >= 0x10201) {
5783 req->auto_pause = req->force_pause;
5784 req->enables |= cpu_to_le32(
5785 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5790 static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5791 struct hwrm_port_phy_cfg_input *req)
5793 u8 autoneg = bp->link_info.autoneg;
5794 u16 fw_link_speed = bp->link_info.req_link_speed;
5795 u16 advertising = bp->link_info.advertising;
5797 if (autoneg & BNXT_AUTONEG_SPEED) {
5799 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
5801 req->enables |= cpu_to_le32(
5802 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5803 req->auto_link_speed_mask = cpu_to_le16(advertising);
5805 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5807 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5809 req->force_link_speed = cpu_to_le16(fw_link_speed);
5810 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5813 /* tell chimp that the setting takes effect immediately */
5814 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5817 int bnxt_hwrm_set_pause(struct bnxt *bp)
5819 struct hwrm_port_phy_cfg_input req = {0};
5822 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5823 bnxt_hwrm_set_pause_common(bp, &req);
5825 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5826 bp->link_info.force_link_chng)
5827 bnxt_hwrm_set_link_common(bp, &req);
5829 mutex_lock(&bp->hwrm_cmd_lock);
5830 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5831 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5832 /* since changing of pause setting doesn't trigger any link
5833 * change event, the driver needs to update the current pause
5834 * result upon successfully return of the phy_cfg command
5836 bp->link_info.pause =
5837 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5838 bp->link_info.auto_pause_setting = 0;
5839 if (!bp->link_info.force_link_chng)
5840 bnxt_report_link(bp);
5842 bp->link_info.force_link_chng = false;
5843 mutex_unlock(&bp->hwrm_cmd_lock);
5847 static void bnxt_hwrm_set_eee(struct bnxt *bp,
5848 struct hwrm_port_phy_cfg_input *req)
5850 struct ethtool_eee *eee = &bp->eee;
5852 if (eee->eee_enabled) {
5854 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5856 if (eee->tx_lpi_enabled)
5857 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5859 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5861 req->flags |= cpu_to_le32(flags);
5862 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5863 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5864 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5866 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5870 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
5872 struct hwrm_port_phy_cfg_input req = {0};
5874 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5876 bnxt_hwrm_set_pause_common(bp, &req);
5878 bnxt_hwrm_set_link_common(bp, &req);
5881 bnxt_hwrm_set_eee(bp, &req);
5882 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5885 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5887 struct hwrm_port_phy_cfg_input req = {0};
5889 if (!BNXT_SINGLE_PF(bp))
5892 if (pci_num_vf(bp->pdev))
5895 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5896 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
5897 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5900 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
5902 struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
5903 struct hwrm_port_led_qcaps_input req = {0};
5904 struct bnxt_pf_info *pf = &bp->pf;
5907 if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
5910 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
5911 req.port_id = cpu_to_le16(pf->port_id);
5912 mutex_lock(&bp->hwrm_cmd_lock);
5913 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5915 mutex_unlock(&bp->hwrm_cmd_lock);
5918 if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
5921 bp->num_leds = resp->num_leds;
5922 memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
5924 for (i = 0; i < bp->num_leds; i++) {
5925 struct bnxt_led_info *led = &bp->leds[i];
5926 __le16 caps = led->led_state_caps;
5928 if (!led->led_group_id ||
5929 !BNXT_LED_ALT_BLINK_CAP(caps)) {
5935 mutex_unlock(&bp->hwrm_cmd_lock);
5939 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
5941 struct hwrm_wol_filter_alloc_input req = {0};
5942 struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
5945 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
5946 req.port_id = cpu_to_le16(bp->pf.port_id);
5947 req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
5948 req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
5949 memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
5950 mutex_lock(&bp->hwrm_cmd_lock);
5951 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5953 bp->wol_filter_id = resp->wol_filter_id;
5954 mutex_unlock(&bp->hwrm_cmd_lock);
5958 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
5960 struct hwrm_wol_filter_free_input req = {0};
5963 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
5964 req.port_id = cpu_to_le16(bp->pf.port_id);
5965 req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
5966 req.wol_filter_id = bp->wol_filter_id;
5967 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5971 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
5973 struct hwrm_wol_filter_qcfg_input req = {0};
5974 struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5975 u16 next_handle = 0;
5978 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
5979 req.port_id = cpu_to_le16(bp->pf.port_id);
5980 req.handle = cpu_to_le16(handle);
5981 mutex_lock(&bp->hwrm_cmd_lock);
5982 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5984 next_handle = le16_to_cpu(resp->next_handle);
5985 if (next_handle != 0) {
5986 if (resp->wol_type ==
5987 WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
5989 bp->wol_filter_id = resp->wol_filter_id;
5993 mutex_unlock(&bp->hwrm_cmd_lock);
5997 static void bnxt_get_wol_settings(struct bnxt *bp)
6001 if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
6005 handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
6006 } while (handle && handle != 0xffff);
6009 static bool bnxt_eee_config_ok(struct bnxt *bp)
6011 struct ethtool_eee *eee = &bp->eee;
6012 struct bnxt_link_info *link_info = &bp->link_info;
6014 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
6017 if (eee->eee_enabled) {
6019 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
6021 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6022 eee->eee_enabled = 0;
6025 if (eee->advertised & ~advertising) {
6026 eee->advertised = advertising & eee->supported;
6033 static int bnxt_update_phy_setting(struct bnxt *bp)
6036 bool update_link = false;
6037 bool update_pause = false;
6038 bool update_eee = false;
6039 struct bnxt_link_info *link_info = &bp->link_info;
6041 rc = bnxt_update_link(bp, true);
6043 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
6047 if (!BNXT_SINGLE_PF(bp))
6050 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6051 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
6052 link_info->req_flow_ctrl)
6053 update_pause = true;
6054 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
6055 link_info->force_pause_setting != link_info->req_flow_ctrl)
6056 update_pause = true;
6057 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
6058 if (BNXT_AUTO_MODE(link_info->auto_mode))
6060 if (link_info->req_link_speed != link_info->force_link_speed)
6062 if (link_info->req_duplex != link_info->duplex_setting)
6065 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
6067 if (link_info->advertising != link_info->auto_link_speeds)
6071 /* The last close may have shutdown the link, so need to call
6072 * PHY_CFG to bring it back up.
6074 if (!netif_carrier_ok(bp->dev))
6077 if (!bnxt_eee_config_ok(bp))
6081 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
6082 else if (update_pause)
6083 rc = bnxt_hwrm_set_pause(bp);
6085 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
6093 /* Common routine to pre-map certain register block to different GRC window.
6094 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
6095 * in PF and 3 windows in VF that can be customized to map in different
6098 static void bnxt_preset_reg_win(struct bnxt *bp)
6101 /* CAG registers map to GRC window #4 */
6102 writel(BNXT_CAG_REG_BASE,
6103 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
6107 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6111 bnxt_preset_reg_win(bp);
6112 netif_carrier_off(bp->dev);
6114 rc = bnxt_setup_int_mode(bp);
6116 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
6121 if ((bp->flags & BNXT_FLAG_RFS) &&
6122 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
6123 /* disable RFS if falling back to INTA */
6124 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
6125 bp->flags &= ~BNXT_FLAG_RFS;
6128 rc = bnxt_alloc_mem(bp, irq_re_init);
6130 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6131 goto open_err_free_mem;
6136 rc = bnxt_request_irq(bp);
6138 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
6143 bnxt_enable_napi(bp);
6145 rc = bnxt_init_nic(bp, irq_re_init);
6147 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6152 rc = bnxt_update_phy_setting(bp);
6154 netdev_warn(bp->dev, "failed to update phy settings\n");
6158 udp_tunnel_get_rx_info(bp->dev);
6160 set_bit(BNXT_STATE_OPEN, &bp->state);
6161 bnxt_enable_int(bp);
6162 /* Enable TX queues */
6164 mod_timer(&bp->timer, jiffies + bp->current_interval);
6165 /* Poll link status and check for SFP+ module status */
6166 bnxt_get_port_module_status(bp);
6171 bnxt_disable_napi(bp);
6177 bnxt_free_mem(bp, true);
6181 /* rtnl_lock held */
6182 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6186 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
6188 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
6194 /* rtnl_lock held, open the NIC half way by allocating all resources, but
6195 * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
6198 int bnxt_half_open_nic(struct bnxt *bp)
6202 rc = bnxt_alloc_mem(bp, false);
6204 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
6207 rc = bnxt_init_nic(bp, false);
6209 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
6216 bnxt_free_mem(bp, false);
6221 /* rtnl_lock held, this call can only be made after a previous successful
6222 * call to bnxt_half_open_nic().
6224 void bnxt_half_close_nic(struct bnxt *bp)
6226 bnxt_hwrm_resource_free(bp, false, false);
6228 bnxt_free_mem(bp, false);
6231 static int bnxt_open(struct net_device *dev)
6233 struct bnxt *bp = netdev_priv(dev);
6235 return __bnxt_open_nic(bp, true, true);
6238 int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
6242 #ifdef CONFIG_BNXT_SRIOV
6243 if (bp->sriov_cfg) {
6244 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
6246 BNXT_SRIOV_CFG_WAIT_TMO);
6248 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
6251 /* Change device state to avoid TX queue wake up's */
6252 bnxt_tx_disable(bp);
6254 clear_bit(BNXT_STATE_OPEN, &bp->state);
6255 smp_mb__after_atomic();
6256 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
6259 /* Flush rings and and disable interrupts */
6260 bnxt_shutdown_nic(bp, irq_re_init);
6262 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
6264 bnxt_disable_napi(bp);
6265 del_timer_sync(&bp->timer);
6272 bnxt_free_mem(bp, irq_re_init);
6276 static int bnxt_close(struct net_device *dev)
6278 struct bnxt *bp = netdev_priv(dev);
6280 bnxt_close_nic(bp, true, true);
6281 bnxt_hwrm_shutdown_link(bp);
6285 /* rtnl_lock held */
6286 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
6292 if (!netif_running(dev))
6299 if (!netif_running(dev))
6312 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6315 struct bnxt *bp = netdev_priv(dev);
6320 /* TODO check if we need to synchronize with bnxt_close path */
6321 for (i = 0; i < bp->cp_nr_rings; i++) {
6322 struct bnxt_napi *bnapi = bp->bnapi[i];
6323 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6324 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
6326 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
6327 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
6328 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
6330 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
6331 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
6332 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
6334 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
6335 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
6336 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
6338 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
6339 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
6340 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
6342 stats->rx_missed_errors +=
6343 le64_to_cpu(hw_stats->rx_discard_pkts);
6345 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
6347 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
6350 if (bp->flags & BNXT_FLAG_PORT_STATS) {
6351 struct rx_port_stats *rx = bp->hw_rx_port_stats;
6352 struct tx_port_stats *tx = bp->hw_tx_port_stats;
6354 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
6355 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
6356 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
6357 le64_to_cpu(rx->rx_ovrsz_frames) +
6358 le64_to_cpu(rx->rx_runt_frames);
6359 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
6360 le64_to_cpu(rx->rx_jbr_frames);
6361 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
6362 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
6363 stats->tx_errors = le64_to_cpu(tx->tx_err);
6367 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
6369 struct net_device *dev = bp->dev;
6370 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6371 struct netdev_hw_addr *ha;
6374 bool update = false;
6377 netdev_for_each_mc_addr(ha, dev) {
6378 if (mc_count >= BNXT_MAX_MC_ADDRS) {
6379 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6380 vnic->mc_list_count = 0;
6384 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
6385 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
6392 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
6394 if (mc_count != vnic->mc_list_count) {
6395 vnic->mc_list_count = mc_count;
6401 static bool bnxt_uc_list_updated(struct bnxt *bp)
6403 struct net_device *dev = bp->dev;
6404 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6405 struct netdev_hw_addr *ha;
6408 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
6411 netdev_for_each_uc_addr(ha, dev) {
6412 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
6420 static void bnxt_set_rx_mode(struct net_device *dev)
6422 struct bnxt *bp = netdev_priv(dev);
6423 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6424 u32 mask = vnic->rx_mask;
6425 bool mc_update = false;
6428 if (!netif_running(dev))
6431 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
6432 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
6433 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
6435 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
6436 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6438 uc_update = bnxt_uc_list_updated(bp);
6440 if (dev->flags & IFF_ALLMULTI) {
6441 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
6442 vnic->mc_list_count = 0;
6444 mc_update = bnxt_mc_list_updated(bp, &mask);
6447 if (mask != vnic->rx_mask || uc_update || mc_update) {
6448 vnic->rx_mask = mask;
6450 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
6451 schedule_work(&bp->sp_task);
6455 static int bnxt_cfg_rx_mode(struct bnxt *bp)
6457 struct net_device *dev = bp->dev;
6458 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
6459 struct netdev_hw_addr *ha;
6463 netif_addr_lock_bh(dev);
6464 uc_update = bnxt_uc_list_updated(bp);
6465 netif_addr_unlock_bh(dev);
6470 mutex_lock(&bp->hwrm_cmd_lock);
6471 for (i = 1; i < vnic->uc_filter_count; i++) {
6472 struct hwrm_cfa_l2_filter_free_input req = {0};
6474 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
6477 req.l2_filter_id = vnic->fw_l2_filter_id[i];
6479 rc = _hwrm_send_message(bp, &req, sizeof(req),
6482 mutex_unlock(&bp->hwrm_cmd_lock);
6484 vnic->uc_filter_count = 1;
6486 netif_addr_lock_bh(dev);
6487 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
6488 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
6490 netdev_for_each_uc_addr(ha, dev) {
6491 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
6493 vnic->uc_filter_count++;
6496 netif_addr_unlock_bh(dev);
6498 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
6499 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
6501 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
6503 vnic->uc_filter_count = i;
6509 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
6511 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
6517 /* If the chip and firmware supports RFS */
6518 static bool bnxt_rfs_supported(struct bnxt *bp)
6520 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6522 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6527 /* If runtime conditions support RFS */
6528 static bool bnxt_rfs_capable(struct bnxt *bp)
6530 #ifdef CONFIG_RFS_ACCEL
6531 int vnics, max_vnics, max_rss_ctxs;
6533 if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
6536 vnics = 1 + bp->rx_nr_rings;
6537 max_vnics = bnxt_get_max_func_vnics(bp);
6538 max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
6540 /* RSS contexts not a limiting factor */
6541 if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
6542 max_rss_ctxs = max_vnics;
6543 if (vnics > max_vnics || vnics > max_rss_ctxs) {
6544 netdev_warn(bp->dev,
6545 "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
6546 min(max_rss_ctxs - 1, max_vnics - 1));
6556 static netdev_features_t bnxt_fix_features(struct net_device *dev,
6557 netdev_features_t features)
6559 struct bnxt *bp = netdev_priv(dev);
6561 if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
6562 features &= ~NETIF_F_NTUPLE;
6564 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
6565 * turned on or off together.
6567 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
6568 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
6569 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6570 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6571 NETIF_F_HW_VLAN_STAG_RX);
6573 features |= NETIF_F_HW_VLAN_CTAG_RX |
6574 NETIF_F_HW_VLAN_STAG_RX;
6576 #ifdef CONFIG_BNXT_SRIOV
6579 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
6580 NETIF_F_HW_VLAN_STAG_RX);
6587 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
6589 struct bnxt *bp = netdev_priv(dev);
6590 u32 flags = bp->flags;
6593 bool re_init = false;
6594 bool update_tpa = false;
6596 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
6597 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
6598 flags |= BNXT_FLAG_GRO;
6599 if (features & NETIF_F_LRO)
6600 flags |= BNXT_FLAG_LRO;
6602 if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
6603 flags &= ~BNXT_FLAG_TPA;
6605 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6606 flags |= BNXT_FLAG_STRIP_VLAN;
6608 if (features & NETIF_F_NTUPLE)
6609 flags |= BNXT_FLAG_RFS;
6611 changes = flags ^ bp->flags;
6612 if (changes & BNXT_FLAG_TPA) {
6614 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
6615 (flags & BNXT_FLAG_TPA) == 0)
6619 if (changes & ~BNXT_FLAG_TPA)
6622 if (flags != bp->flags) {
6623 u32 old_flags = bp->flags;
6627 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6629 bnxt_set_ring_params(bp);
6634 bnxt_close_nic(bp, false, false);
6636 bnxt_set_ring_params(bp);
6638 return bnxt_open_nic(bp, false, false);
6641 rc = bnxt_set_tpa(bp,
6642 (flags & BNXT_FLAG_TPA) ?
6645 bp->flags = old_flags;
6651 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
6653 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
6654 int i = bnapi->index;
6659 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
6660 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
6664 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
6666 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
6667 int i = bnapi->index;
6672 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",
6673 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
6674 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
6675 rxr->rx_sw_agg_prod);
6678 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
6680 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
6681 int i = bnapi->index;
6683 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
6684 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
6687 static void bnxt_dbg_dump_states(struct bnxt *bp)
6690 struct bnxt_napi *bnapi;
6692 for (i = 0; i < bp->cp_nr_rings; i++) {
6693 bnapi = bp->bnapi[i];
6694 if (netif_msg_drv(bp)) {
6695 bnxt_dump_tx_sw_state(bnapi);
6696 bnxt_dump_rx_sw_state(bnapi);
6697 bnxt_dump_cp_sw_state(bnapi);
6702 static void bnxt_reset_task(struct bnxt *bp, bool silent)
6705 bnxt_dbg_dump_states(bp);
6706 if (netif_running(bp->dev)) {
6711 bnxt_close_nic(bp, false, false);
6712 rc = bnxt_open_nic(bp, false, false);
6718 static void bnxt_tx_timeout(struct net_device *dev)
6720 struct bnxt *bp = netdev_priv(dev);
6722 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
6723 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
6724 schedule_work(&bp->sp_task);
6727 #ifdef CONFIG_NET_POLL_CONTROLLER
6728 static void bnxt_poll_controller(struct net_device *dev)
6730 struct bnxt *bp = netdev_priv(dev);
6733 for (i = 0; i < bp->cp_nr_rings; i++) {
6734 struct bnxt_irq *irq = &bp->irq_tbl[i];
6736 disable_irq(irq->vector);
6737 irq->handler(irq->vector, bp->bnapi[i]);
6738 enable_irq(irq->vector);
6743 static void bnxt_timer(unsigned long data)
6745 struct bnxt *bp = (struct bnxt *)data;
6746 struct net_device *dev = bp->dev;
6748 if (!netif_running(dev))
6751 if (atomic_read(&bp->intr_sem) != 0)
6752 goto bnxt_restart_timer;
6754 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
6755 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
6756 schedule_work(&bp->sp_task);
6759 mod_timer(&bp->timer, jiffies + bp->current_interval);
6762 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
6764 /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
6765 * set. If the device is being closed, bnxt_close() may be holding
6766 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6767 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6769 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6773 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
6775 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6779 /* Only called from bnxt_sp_task() */
6780 static void bnxt_reset(struct bnxt *bp, bool silent)
6782 bnxt_rtnl_lock_sp(bp);
6783 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6784 bnxt_reset_task(bp, silent);
6785 bnxt_rtnl_unlock_sp(bp);
6788 static void bnxt_cfg_ntp_filters(struct bnxt *);
6790 static void bnxt_sp_task(struct work_struct *work)
6792 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6794 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6795 smp_mb__after_atomic();
6796 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6797 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6801 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6802 bnxt_cfg_rx_mode(bp);
6804 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6805 bnxt_cfg_ntp_filters(bp);
6806 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6807 bnxt_hwrm_exec_fwd_req(bp);
6808 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6809 bnxt_hwrm_tunnel_dst_port_alloc(
6811 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6813 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6814 bnxt_hwrm_tunnel_dst_port_free(
6815 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6817 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6818 bnxt_hwrm_tunnel_dst_port_alloc(
6820 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6822 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6823 bnxt_hwrm_tunnel_dst_port_free(
6824 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6826 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6827 bnxt_hwrm_port_qstats(bp);
6829 /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
6830 * must be the last functions to be called before exiting.
6832 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6835 if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
6837 bnxt_hwrm_phy_qcaps(bp);
6839 bnxt_rtnl_lock_sp(bp);
6840 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6841 rc = bnxt_update_link(bp, true);
6842 bnxt_rtnl_unlock_sp(bp);
6844 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6847 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
6848 bnxt_rtnl_lock_sp(bp);
6849 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6850 bnxt_get_port_module_status(bp);
6851 bnxt_rtnl_unlock_sp(bp);
6853 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6854 bnxt_reset(bp, false);
6856 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6857 bnxt_reset(bp, true);
6859 smp_mb__before_atomic();
6860 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6863 /* Under rtnl_lock */
6864 int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, int tcs, int tx_xdp)
6866 int max_rx, max_tx, tx_sets = 1;
6867 int tx_rings_needed;
6871 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
6877 rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
6884 tx_rings_needed = tx * tx_sets + tx_xdp;
6885 if (max_tx < tx_rings_needed)
6888 if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
6889 tx_rings_needed < (tx * tx_sets + tx_xdp))
6894 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
6897 pci_iounmap(pdev, bp->bar2);
6902 pci_iounmap(pdev, bp->bar1);
6907 pci_iounmap(pdev, bp->bar0);
6912 static void bnxt_cleanup_pci(struct bnxt *bp)
6914 bnxt_unmap_bars(bp, bp->pdev);
6915 pci_release_regions(bp->pdev);
6916 pci_disable_device(bp->pdev);
6919 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6922 struct bnxt *bp = netdev_priv(dev);
6924 SET_NETDEV_DEV(dev, &pdev->dev);
6926 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6927 rc = pci_enable_device(pdev);
6929 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6933 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6935 "Cannot find PCI device base address, aborting\n");
6937 goto init_err_disable;
6940 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6942 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6943 goto init_err_disable;
6946 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6947 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6948 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6949 goto init_err_disable;
6952 pci_set_master(pdev);
6957 bp->bar0 = pci_ioremap_bar(pdev, 0);
6959 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6961 goto init_err_release;
6964 bp->bar1 = pci_ioremap_bar(pdev, 2);
6966 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6968 goto init_err_release;
6971 bp->bar2 = pci_ioremap_bar(pdev, 4);
6973 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6975 goto init_err_release;
6978 pci_enable_pcie_error_reporting(pdev);
6980 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6982 spin_lock_init(&bp->ntp_fltr_lock);
6984 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6985 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6987 /* tick values in micro seconds */
6988 bp->rx_coal_ticks = 12;
6989 bp->rx_coal_bufs = 30;
6990 bp->rx_coal_ticks_irq = 1;
6991 bp->rx_coal_bufs_irq = 2;
6993 bp->tx_coal_ticks = 25;
6994 bp->tx_coal_bufs = 30;
6995 bp->tx_coal_ticks_irq = 2;
6996 bp->tx_coal_bufs_irq = 2;
6998 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
7000 init_timer(&bp->timer);
7001 bp->timer.data = (unsigned long)bp;
7002 bp->timer.function = bnxt_timer;
7003 bp->current_interval = BNXT_TIMER_INTERVAL;
7005 clear_bit(BNXT_STATE_OPEN, &bp->state);
7009 bnxt_unmap_bars(bp, pdev);
7010 pci_release_regions(pdev);
7013 pci_disable_device(pdev);
7019 /* rtnl_lock held */
7020 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
7022 struct sockaddr *addr = p;
7023 struct bnxt *bp = netdev_priv(dev);
7026 if (!is_valid_ether_addr(addr->sa_data))
7027 return -EADDRNOTAVAIL;
7029 rc = bnxt_approve_mac(bp, addr->sa_data);
7033 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
7036 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7037 if (netif_running(dev)) {
7038 bnxt_close_nic(bp, false, false);
7039 rc = bnxt_open_nic(bp, false, false);
7045 /* rtnl_lock held */
7046 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
7048 struct bnxt *bp = netdev_priv(dev);
7050 if (netif_running(dev))
7051 bnxt_close_nic(bp, false, false);
7054 bnxt_set_ring_params(bp);
7056 if (netif_running(dev))
7057 return bnxt_open_nic(bp, false, false);
7062 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
7064 struct bnxt *bp = netdev_priv(dev);
7068 if (tc > bp->max_tc) {
7069 netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
7074 if (netdev_get_num_tc(dev) == tc)
7077 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7080 rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
7081 tc, bp->tx_nr_rings_xdp);
7085 /* Needs to close the device and do hw resource re-allocations */
7086 if (netif_running(bp->dev))
7087 bnxt_close_nic(bp, true, false);
7090 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
7091 netdev_set_num_tc(dev, tc);
7093 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7094 netdev_reset_tc(dev);
7096 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7097 bp->tx_nr_rings + bp->rx_nr_rings;
7098 bp->num_stat_ctxs = bp->cp_nr_rings;
7100 if (netif_running(bp->dev))
7101 return bnxt_open_nic(bp, true, false);
7106 static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
7107 struct tc_to_netdev *ntc)
7109 if (ntc->type != TC_SETUP_MQPRIO)
7112 ntc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
7114 return bnxt_setup_mq_tc(dev, ntc->mqprio->num_tc);
7117 #ifdef CONFIG_RFS_ACCEL
7118 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
7119 struct bnxt_ntuple_filter *f2)
7121 struct flow_keys *keys1 = &f1->fkeys;
7122 struct flow_keys *keys2 = &f2->fkeys;
7124 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
7125 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
7126 keys1->ports.ports == keys2->ports.ports &&
7127 keys1->basic.ip_proto == keys2->basic.ip_proto &&
7128 keys1->basic.n_proto == keys2->basic.n_proto &&
7129 keys1->control.flags == keys2->control.flags &&
7130 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
7131 ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
7137 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
7138 u16 rxq_index, u32 flow_id)
7140 struct bnxt *bp = netdev_priv(dev);
7141 struct bnxt_ntuple_filter *fltr, *new_fltr;
7142 struct flow_keys *fkeys;
7143 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
7144 int rc = 0, idx, bit_id, l2_idx = 0;
7145 struct hlist_head *head;
7147 if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
7148 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
7151 netif_addr_lock_bh(dev);
7152 for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
7153 if (ether_addr_equal(eth->h_dest,
7154 vnic->uc_list + off)) {
7159 netif_addr_unlock_bh(dev);
7163 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
7167 fkeys = &new_fltr->fkeys;
7168 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
7169 rc = -EPROTONOSUPPORT;
7173 if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
7174 fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
7175 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
7176 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
7177 rc = -EPROTONOSUPPORT;
7180 if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
7181 bp->hwrm_spec_code < 0x10601) {
7182 rc = -EPROTONOSUPPORT;
7185 if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
7186 bp->hwrm_spec_code < 0x10601) {
7187 rc = -EPROTONOSUPPORT;
7191 memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
7192 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
7194 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
7195 head = &bp->ntp_fltr_hash_tbl[idx];
7197 hlist_for_each_entry_rcu(fltr, head, hash) {
7198 if (bnxt_fltr_match(fltr, new_fltr)) {
7206 spin_lock_bh(&bp->ntp_fltr_lock);
7207 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
7208 BNXT_NTP_FLTR_MAX_FLTR, 0);
7210 spin_unlock_bh(&bp->ntp_fltr_lock);
7215 new_fltr->sw_id = (u16)bit_id;
7216 new_fltr->flow_id = flow_id;
7217 new_fltr->l2_fltr_idx = l2_idx;
7218 new_fltr->rxq = rxq_index;
7219 hlist_add_head_rcu(&new_fltr->hash, head);
7220 bp->ntp_fltr_count++;
7221 spin_unlock_bh(&bp->ntp_fltr_lock);
7223 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
7224 schedule_work(&bp->sp_task);
7226 return new_fltr->sw_id;
7233 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7237 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
7238 struct hlist_head *head;
7239 struct hlist_node *tmp;
7240 struct bnxt_ntuple_filter *fltr;
7243 head = &bp->ntp_fltr_hash_tbl[i];
7244 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
7247 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
7248 if (rps_may_expire_flow(bp->dev, fltr->rxq,
7251 bnxt_hwrm_cfa_ntuple_filter_free(bp,
7256 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
7261 set_bit(BNXT_FLTR_VALID, &fltr->state);
7265 spin_lock_bh(&bp->ntp_fltr_lock);
7266 hlist_del_rcu(&fltr->hash);
7267 bp->ntp_fltr_count--;
7268 spin_unlock_bh(&bp->ntp_fltr_lock);
7270 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
7275 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
7276 netdev_info(bp->dev, "Receive PF driver unload event!");
7281 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
7285 #endif /* CONFIG_RFS_ACCEL */
7287 static void bnxt_udp_tunnel_add(struct net_device *dev,
7288 struct udp_tunnel_info *ti)
7290 struct bnxt *bp = netdev_priv(dev);
7292 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7295 if (!netif_running(dev))
7299 case UDP_TUNNEL_TYPE_VXLAN:
7300 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
7303 bp->vxlan_port_cnt++;
7304 if (bp->vxlan_port_cnt == 1) {
7305 bp->vxlan_port = ti->port;
7306 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
7307 schedule_work(&bp->sp_task);
7310 case UDP_TUNNEL_TYPE_GENEVE:
7311 if (bp->nge_port_cnt && bp->nge_port != ti->port)
7315 if (bp->nge_port_cnt == 1) {
7316 bp->nge_port = ti->port;
7317 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
7324 schedule_work(&bp->sp_task);
7327 static void bnxt_udp_tunnel_del(struct net_device *dev,
7328 struct udp_tunnel_info *ti)
7330 struct bnxt *bp = netdev_priv(dev);
7332 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
7335 if (!netif_running(dev))
7339 case UDP_TUNNEL_TYPE_VXLAN:
7340 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
7342 bp->vxlan_port_cnt--;
7344 if (bp->vxlan_port_cnt != 0)
7347 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
7349 case UDP_TUNNEL_TYPE_GENEVE:
7350 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
7354 if (bp->nge_port_cnt != 0)
7357 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
7363 schedule_work(&bp->sp_task);
7366 static const struct net_device_ops bnxt_netdev_ops = {
7367 .ndo_open = bnxt_open,
7368 .ndo_start_xmit = bnxt_start_xmit,
7369 .ndo_stop = bnxt_close,
7370 .ndo_get_stats64 = bnxt_get_stats64,
7371 .ndo_set_rx_mode = bnxt_set_rx_mode,
7372 .ndo_do_ioctl = bnxt_ioctl,
7373 .ndo_validate_addr = eth_validate_addr,
7374 .ndo_set_mac_address = bnxt_change_mac_addr,
7375 .ndo_change_mtu = bnxt_change_mtu,
7376 .ndo_fix_features = bnxt_fix_features,
7377 .ndo_set_features = bnxt_set_features,
7378 .ndo_tx_timeout = bnxt_tx_timeout,
7379 #ifdef CONFIG_BNXT_SRIOV
7380 .ndo_get_vf_config = bnxt_get_vf_config,
7381 .ndo_set_vf_mac = bnxt_set_vf_mac,
7382 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
7383 .ndo_set_vf_rate = bnxt_set_vf_bw,
7384 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
7385 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
7387 #ifdef CONFIG_NET_POLL_CONTROLLER
7388 .ndo_poll_controller = bnxt_poll_controller,
7390 .ndo_setup_tc = bnxt_setup_tc,
7391 #ifdef CONFIG_RFS_ACCEL
7392 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
7394 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
7395 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
7396 .ndo_xdp = bnxt_xdp,
7399 static void bnxt_remove_one(struct pci_dev *pdev)
7401 struct net_device *dev = pci_get_drvdata(pdev);
7402 struct bnxt *bp = netdev_priv(dev);
7405 bnxt_sriov_disable(bp);
7407 pci_disable_pcie_error_reporting(pdev);
7408 unregister_netdev(dev);
7409 cancel_work_sync(&bp->sp_task);
7412 bnxt_clear_int_mode(bp);
7413 bnxt_hwrm_func_drv_unrgtr(bp);
7414 bnxt_free_hwrm_resources(bp);
7415 bnxt_free_hwrm_short_cmd_req(bp);
7416 bnxt_ethtool_free(bp);
7421 bpf_prog_put(bp->xdp_prog);
7422 bnxt_cleanup_pci(bp);
7426 static int bnxt_probe_phy(struct bnxt *bp)
7429 struct bnxt_link_info *link_info = &bp->link_info;
7431 rc = bnxt_hwrm_phy_qcaps(bp);
7433 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
7438 rc = bnxt_update_link(bp, false);
7440 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
7445 /* Older firmware does not have supported_auto_speeds, so assume
7446 * that all supported speeds can be autonegotiated.
7448 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
7449 link_info->support_auto_speeds = link_info->support_speeds;
7451 /*initialize the ethool setting copy with NVM settings */
7452 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
7453 link_info->autoneg = BNXT_AUTONEG_SPEED;
7454 if (bp->hwrm_spec_code >= 0x10201) {
7455 if (link_info->auto_pause_setting &
7456 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
7457 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7459 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
7461 link_info->advertising = link_info->auto_link_speeds;
7463 link_info->req_link_speed = link_info->force_link_speed;
7464 link_info->req_duplex = link_info->duplex_setting;
7466 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
7467 link_info->req_flow_ctrl =
7468 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
7470 link_info->req_flow_ctrl = link_info->force_pause_setting;
7474 static int bnxt_get_max_irq(struct pci_dev *pdev)
7478 if (!pdev->msix_cap)
7481 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
7482 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
7485 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7488 int max_ring_grps = 0;
7490 #ifdef CONFIG_BNXT_SRIOV
7492 *max_tx = bp->vf.max_tx_rings;
7493 *max_rx = bp->vf.max_rx_rings;
7494 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
7495 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
7496 max_ring_grps = bp->vf.max_hw_ring_grps;
7500 *max_tx = bp->pf.max_tx_rings;
7501 *max_rx = bp->pf.max_rx_rings;
7502 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
7503 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
7504 max_ring_grps = bp->pf.max_hw_ring_grps;
7506 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
7510 if (bp->flags & BNXT_FLAG_AGG_RINGS)
7512 *max_rx = min_t(int, *max_rx, max_ring_grps);
7515 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
7519 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
7520 if (!rx || !tx || !cp)
7525 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
7528 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
7533 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7534 if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
7535 /* Not enough rings, try disabling agg rings. */
7536 bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7537 rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
7540 bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7541 bp->dev->hw_features &= ~NETIF_F_LRO;
7542 bp->dev->features &= ~NETIF_F_LRO;
7543 bnxt_set_ring_params(bp);
7546 if (bp->flags & BNXT_FLAG_ROCE_CAP) {
7547 int max_cp, max_stat, max_irq;
7549 /* Reserve minimum resources for RoCE */
7550 max_cp = bnxt_get_max_func_cp_rings(bp);
7551 max_stat = bnxt_get_max_func_stat_ctxs(bp);
7552 max_irq = bnxt_get_max_func_irqs(bp);
7553 if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
7554 max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
7555 max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
7558 max_cp -= BNXT_MIN_ROCE_CP_RINGS;
7559 max_irq -= BNXT_MIN_ROCE_CP_RINGS;
7560 max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
7561 max_cp = min_t(int, max_cp, max_irq);
7562 max_cp = min_t(int, max_cp, max_stat);
7563 rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
7570 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
7572 int dflt_rings, max_rx_rings, max_tx_rings, rc;
7575 bp->flags |= BNXT_FLAG_SHARED_RINGS;
7576 dflt_rings = netif_get_num_default_rss_queues();
7577 rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
7580 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
7581 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
7583 rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
7585 netdev_warn(bp->dev, "Unable to reserve tx rings\n");
7587 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
7588 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
7589 bp->tx_nr_rings + bp->rx_nr_rings;
7590 bp->num_stat_ctxs = bp->cp_nr_rings;
7591 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
7598 void bnxt_restore_pf_fw_resources(struct bnxt *bp)
7601 bnxt_hwrm_func_qcaps(bp);
7602 bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
7605 static void bnxt_parse_log_pcie_link(struct bnxt *bp)
7607 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
7608 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
7610 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
7611 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
7612 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
7614 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
7615 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
7616 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
7617 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
7621 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7623 static int version_printed;
7624 struct net_device *dev;
7628 if (pci_is_bridge(pdev))
7631 if (version_printed++ == 0)
7632 pr_info("%s", version);
7634 max_irqs = bnxt_get_max_irq(pdev);
7635 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
7639 bp = netdev_priv(dev);
7641 if (bnxt_vf_pciid(ent->driver_data))
7642 bp->flags |= BNXT_FLAG_VF;
7645 bp->flags |= BNXT_FLAG_MSIX_CAP;
7647 rc = bnxt_init_board(pdev, dev);
7651 dev->netdev_ops = &bnxt_netdev_ops;
7652 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
7653 dev->ethtool_ops = &bnxt_ethtool_ops;
7654 pci_set_drvdata(pdev, dev);
7656 rc = bnxt_alloc_hwrm_resources(bp);
7658 goto init_err_pci_clean;
7660 mutex_init(&bp->hwrm_cmd_lock);
7661 rc = bnxt_hwrm_ver_get(bp);
7663 goto init_err_pci_clean;
7665 if (bp->flags & BNXT_FLAG_SHORT_CMD) {
7666 rc = bnxt_alloc_hwrm_short_cmd_req(bp);
7668 goto init_err_pci_clean;
7671 rc = bnxt_hwrm_func_reset(bp);
7673 goto init_err_pci_clean;
7675 bnxt_hwrm_fw_set_time(bp);
7677 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7678 NETIF_F_TSO | NETIF_F_TSO6 |
7679 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
7680 NETIF_F_GSO_IPXIP4 |
7681 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7682 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
7683 NETIF_F_RXCSUM | NETIF_F_GRO;
7685 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
7686 dev->hw_features |= NETIF_F_LRO;
7688 dev->hw_enc_features =
7689 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
7690 NETIF_F_TSO | NETIF_F_TSO6 |
7691 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
7692 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
7693 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
7694 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
7695 NETIF_F_GSO_GRE_CSUM;
7696 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
7697 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
7698 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
7699 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
7700 dev->priv_flags |= IFF_UNICAST_FLT;
7702 /* MTU range: 60 - 9500 */
7703 dev->min_mtu = ETH_ZLEN;
7704 dev->max_mtu = BNXT_MAX_MTU;
7706 #ifdef CONFIG_BNXT_SRIOV
7707 init_waitqueue_head(&bp->sriov_cfg_wait);
7709 bp->gro_func = bnxt_gro_func_5730x;
7710 if (BNXT_CHIP_P4_PLUS(bp))
7711 bp->gro_func = bnxt_gro_func_5731x;
7713 bp->flags |= BNXT_FLAG_DOUBLE_DB;
7715 rc = bnxt_hwrm_func_drv_rgtr(bp);
7717 goto init_err_pci_clean;
7719 rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
7721 goto init_err_pci_clean;
7723 bp->ulp_probe = bnxt_ulp_probe;
7725 /* Get the MAX capabilities for this function */
7726 rc = bnxt_hwrm_func_qcaps(bp);
7728 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
7731 goto init_err_pci_clean;
7734 rc = bnxt_hwrm_queue_qportcfg(bp);
7736 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
7739 goto init_err_pci_clean;
7742 bnxt_hwrm_func_qcfg(bp);
7743 bnxt_hwrm_port_led_qcaps(bp);
7744 bnxt_ethtool_init(bp);
7747 bnxt_set_rx_skb_mode(bp, false);
7748 bnxt_set_tpa_flags(bp);
7749 bnxt_set_ring_params(bp);
7750 bnxt_set_max_func_irqs(bp, max_irqs);
7751 rc = bnxt_set_dflt_rings(bp, true);
7753 netdev_err(bp->dev, "Not enough rings available.\n");
7755 goto init_err_pci_clean;
7758 /* Default RSS hash cfg. */
7759 bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
7760 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
7761 VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
7762 VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
7763 if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
7764 bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
7765 bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
7766 VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
7769 bnxt_hwrm_vnic_qcaps(bp);
7770 if (bnxt_rfs_supported(bp)) {
7771 dev->hw_features |= NETIF_F_NTUPLE;
7772 if (bnxt_rfs_capable(bp)) {
7773 bp->flags |= BNXT_FLAG_RFS;
7774 dev->features |= NETIF_F_NTUPLE;
7778 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
7779 bp->flags |= BNXT_FLAG_STRIP_VLAN;
7781 rc = bnxt_probe_phy(bp);
7783 goto init_err_pci_clean;
7785 rc = bnxt_init_int_mode(bp);
7787 goto init_err_pci_clean;
7789 bnxt_get_wol_settings(bp);
7790 if (bp->flags & BNXT_FLAG_WOL_CAP)
7791 device_set_wakeup_enable(&pdev->dev, bp->wol);
7793 device_set_wakeup_capable(&pdev->dev, false);
7795 rc = register_netdev(dev);
7797 goto init_err_clr_int;
7799 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
7800 board_info[ent->driver_data].name,
7801 (long)pci_resource_start(pdev, 0), dev->dev_addr);
7803 bnxt_parse_log_pcie_link(bp);
7808 bnxt_clear_int_mode(bp);
7811 bnxt_cleanup_pci(bp);
7818 static void bnxt_shutdown(struct pci_dev *pdev)
7820 struct net_device *dev = pci_get_drvdata(pdev);
7827 bp = netdev_priv(dev);
7831 if (netif_running(dev))
7834 if (system_state == SYSTEM_POWER_OFF) {
7835 bnxt_ulp_shutdown(bp);
7836 bnxt_clear_int_mode(bp);
7837 pci_wake_from_d3(pdev, bp->wol);
7838 pci_set_power_state(pdev, PCI_D3hot);
7845 #ifdef CONFIG_PM_SLEEP
7846 static int bnxt_suspend(struct device *device)
7848 struct pci_dev *pdev = to_pci_dev(device);
7849 struct net_device *dev = pci_get_drvdata(pdev);
7850 struct bnxt *bp = netdev_priv(dev);
7854 if (netif_running(dev)) {
7855 netif_device_detach(dev);
7856 rc = bnxt_close(dev);
7858 bnxt_hwrm_func_drv_unrgtr(bp);
7863 static int bnxt_resume(struct device *device)
7865 struct pci_dev *pdev = to_pci_dev(device);
7866 struct net_device *dev = pci_get_drvdata(pdev);
7867 struct bnxt *bp = netdev_priv(dev);
7871 if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
7875 rc = bnxt_hwrm_func_reset(bp);
7880 bnxt_get_wol_settings(bp);
7881 if (netif_running(dev)) {
7882 rc = bnxt_open(dev);
7884 netif_device_attach(dev);
7892 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
7893 #define BNXT_PM_OPS (&bnxt_pm_ops)
7897 #define BNXT_PM_OPS NULL
7899 #endif /* CONFIG_PM_SLEEP */
7902 * bnxt_io_error_detected - called when PCI error is detected
7903 * @pdev: Pointer to PCI device
7904 * @state: The current pci connection state
7906 * This function is called after a PCI bus error affecting
7907 * this device has been detected.
7909 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
7910 pci_channel_state_t state)
7912 struct net_device *netdev = pci_get_drvdata(pdev);
7913 struct bnxt *bp = netdev_priv(netdev);
7915 netdev_info(netdev, "PCI I/O error detected\n");
7918 netif_device_detach(netdev);
7922 if (state == pci_channel_io_perm_failure) {
7924 return PCI_ERS_RESULT_DISCONNECT;
7927 if (netif_running(netdev))
7930 pci_disable_device(pdev);
7933 /* Request a slot slot reset. */
7934 return PCI_ERS_RESULT_NEED_RESET;
7938 * bnxt_io_slot_reset - called after the pci bus has been reset.
7939 * @pdev: Pointer to PCI device
7941 * Restart the card from scratch, as if from a cold-boot.
7942 * At this point, the card has exprienced a hard reset,
7943 * followed by fixups by BIOS, and has its config space
7944 * set up identically to what it was at cold boot.
7946 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
7948 struct net_device *netdev = pci_get_drvdata(pdev);
7949 struct bnxt *bp = netdev_priv(netdev);
7951 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
7953 netdev_info(bp->dev, "PCI Slot Reset\n");
7957 if (pci_enable_device(pdev)) {
7959 "Cannot re-enable PCI device after reset.\n");
7961 pci_set_master(pdev);
7963 err = bnxt_hwrm_func_reset(bp);
7964 if (!err && netif_running(netdev))
7965 err = bnxt_open(netdev);
7968 result = PCI_ERS_RESULT_RECOVERED;
7973 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
7978 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7981 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7982 err); /* non-fatal, continue */
7985 return PCI_ERS_RESULT_RECOVERED;
7989 * bnxt_io_resume - called when traffic can start flowing again.
7990 * @pdev: Pointer to PCI device
7992 * This callback is called when the error recovery driver tells
7993 * us that its OK to resume normal operation.
7995 static void bnxt_io_resume(struct pci_dev *pdev)
7997 struct net_device *netdev = pci_get_drvdata(pdev);
8001 netif_device_attach(netdev);
8006 static const struct pci_error_handlers bnxt_err_handler = {
8007 .error_detected = bnxt_io_error_detected,
8008 .slot_reset = bnxt_io_slot_reset,
8009 .resume = bnxt_io_resume
8012 static struct pci_driver bnxt_pci_driver = {
8013 .name = DRV_MODULE_NAME,
8014 .id_table = bnxt_pci_tbl,
8015 .probe = bnxt_init_one,
8016 .remove = bnxt_remove_one,
8017 .shutdown = bnxt_shutdown,
8018 .driver.pm = BNXT_PM_OPS,
8019 .err_handler = &bnxt_err_handler,
8020 #if defined(CONFIG_BNXT_SRIOV)
8021 .sriov_configure = bnxt_sriov_configure,
8025 module_pci_driver(bnxt_pci_driver);