2 * Copyright (C) 2015 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * Netronome network device driver: Common functions between PF and VF
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
40 * Brad Petrus <brad.petrus@netronome.com>
41 * Chris Telfer <chris.telfer@netronome.com>
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/init.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/interrupt.h>
52 #include <linux/ipv6.h>
53 #include <linux/page_ref.h>
54 #include <linux/pci.h>
55 #include <linux/pci_regs.h>
56 #include <linux/msi.h>
57 #include <linux/ethtool.h>
58 #include <linux/log2.h>
59 #include <linux/if_vlan.h>
60 #include <linux/random.h>
62 #include <linux/ktime.h>
64 #include <net/pkt_cls.h>
65 #include <net/vxlan.h>
67 #include "nfp_net_ctrl.h"
71 * nfp_net_get_fw_version() - Read and parse the FW version
72 * @fw_ver: Output fw_version structure to read to
73 * @ctrl_bar: Mapped address of the control BAR
75 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
76 void __iomem *ctrl_bar)
80 reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
81 put_unaligned_le32(reg, fw_ver);
85 nfp_net_dma_map_rx(struct nfp_net *nn, void *frag, unsigned int bufsz,
88 return dma_map_single(&nn->pdev->dev, frag + NFP_NET_RX_BUF_HEADROOM,
89 bufsz - NFP_NET_RX_BUF_NON_DATA, direction);
93 nfp_net_dma_unmap_rx(struct nfp_net *nn, dma_addr_t dma_addr,
94 unsigned int bufsz, int direction)
96 dma_unmap_single(&nn->pdev->dev, dma_addr,
97 bufsz - NFP_NET_RX_BUF_NON_DATA, direction);
102 * Firmware reconfig may take a while so we have two versions of it -
103 * synchronous and asynchronous (posted). All synchronous callers are holding
104 * RTNL so we don't have to worry about serializing them.
106 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
108 nn_writel(nn, NFP_NET_CFG_UPDATE, update);
109 /* ensure update is written before pinging HW */
111 nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
114 /* Pass 0 as update to run posted reconfigs. */
115 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
117 update |= nn->reconfig_posted;
118 nn->reconfig_posted = 0;
120 nfp_net_reconfig_start(nn, update);
122 nn->reconfig_timer_active = true;
123 mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
126 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
130 reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
133 if (reg & NFP_NET_CFG_UPDATE_ERR) {
134 nn_err(nn, "Reconfig error: 0x%08x\n", reg);
136 } else if (last_check) {
137 nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
144 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
146 bool timed_out = false;
148 /* Poll update field, waiting for NFP to ack the config */
149 while (!nfp_net_reconfig_check_done(nn, timed_out)) {
151 timed_out = time_is_before_eq_jiffies(deadline);
154 if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
157 return timed_out ? -EIO : 0;
160 static void nfp_net_reconfig_timer(unsigned long data)
162 struct nfp_net *nn = (void *)data;
164 spin_lock_bh(&nn->reconfig_lock);
166 nn->reconfig_timer_active = false;
168 /* If sync caller is present it will take over from us */
169 if (nn->reconfig_sync_present)
172 /* Read reconfig status and report errors */
173 nfp_net_reconfig_check_done(nn, true);
175 if (nn->reconfig_posted)
176 nfp_net_reconfig_start_async(nn, 0);
178 spin_unlock_bh(&nn->reconfig_lock);
182 * nfp_net_reconfig_post() - Post async reconfig request
183 * @nn: NFP Net device to reconfigure
184 * @update: The value for the update field in the BAR config
186 * Record FW reconfiguration request. Reconfiguration will be kicked off
187 * whenever reconfiguration machinery is idle. Multiple requests can be
190 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
192 spin_lock_bh(&nn->reconfig_lock);
194 /* Sync caller will kick off async reconf when it's done, just post */
195 if (nn->reconfig_sync_present) {
196 nn->reconfig_posted |= update;
200 /* Opportunistically check if the previous command is done */
201 if (!nn->reconfig_timer_active ||
202 nfp_net_reconfig_check_done(nn, false))
203 nfp_net_reconfig_start_async(nn, update);
205 nn->reconfig_posted |= update;
207 spin_unlock_bh(&nn->reconfig_lock);
211 * nfp_net_reconfig() - Reconfigure the firmware
212 * @nn: NFP Net device to reconfigure
213 * @update: The value for the update field in the BAR config
215 * Write the update word to the BAR and ping the reconfig queue. The
216 * poll until the firmware has acknowledged the update by zeroing the
219 * Return: Negative errno on error, 0 on success
221 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
223 bool cancelled_timer = false;
224 u32 pre_posted_requests;
227 spin_lock_bh(&nn->reconfig_lock);
229 nn->reconfig_sync_present = true;
231 if (nn->reconfig_timer_active) {
232 del_timer(&nn->reconfig_timer);
233 nn->reconfig_timer_active = false;
234 cancelled_timer = true;
236 pre_posted_requests = nn->reconfig_posted;
237 nn->reconfig_posted = 0;
239 spin_unlock_bh(&nn->reconfig_lock);
242 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
244 /* Run the posted reconfigs which were issued before we started */
245 if (pre_posted_requests) {
246 nfp_net_reconfig_start(nn, pre_posted_requests);
247 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
250 nfp_net_reconfig_start(nn, update);
251 ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
253 spin_lock_bh(&nn->reconfig_lock);
255 if (nn->reconfig_posted)
256 nfp_net_reconfig_start_async(nn, 0);
258 nn->reconfig_sync_present = false;
260 spin_unlock_bh(&nn->reconfig_lock);
265 /* Interrupt configuration and handling
269 * nfp_net_irq_unmask() - Unmask automasked interrupt
270 * @nn: NFP Network structure
271 * @entry_nr: MSI-X table entry
273 * Clear the ICR for the IRQ entry.
275 static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
277 nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
282 * nfp_net_msix_alloc() - Try to allocate MSI-X irqs
283 * @nn: NFP Network structure
284 * @nr_vecs: Number of MSI-X vectors to allocate
286 * For MSI-X we want at least NFP_NET_NON_Q_VECTORS + 1 vectors.
288 * Return: Number of MSI-X vectors obtained or 0 on error.
290 static int nfp_net_msix_alloc(struct nfp_net *nn, int nr_vecs)
292 struct pci_dev *pdev = nn->pdev;
296 for (i = 0; i < nr_vecs; i++)
297 nn->irq_entries[i].entry = i;
299 nvecs = pci_enable_msix_range(pdev, nn->irq_entries,
300 NFP_NET_NON_Q_VECTORS + 1, nr_vecs);
302 nn_warn(nn, "Failed to enable MSI-X. Wanted %d-%d (err=%d)\n",
303 NFP_NET_NON_Q_VECTORS + 1, nr_vecs, nvecs);
311 * nfp_net_irqs_wanted() - Work out how many interrupt vectors we want
312 * @nn: NFP Network structure
314 * We want a vector per CPU (or ring), whatever is smaller plus
315 * NFP_NET_NON_Q_VECTORS for LSC etc.
317 * Return: Number of interrupts wanted
319 static int nfp_net_irqs_wanted(struct nfp_net *nn)
324 ncpus = num_online_cpus();
326 vecs = max_t(int, nn->num_tx_rings, nn->num_rx_rings);
327 vecs = min_t(int, vecs, ncpus);
329 return vecs + NFP_NET_NON_Q_VECTORS;
333 * nfp_net_irqs_alloc() - allocates MSI-X irqs
334 * @nn: NFP Network structure
336 * Return: Number of irqs obtained or 0 on error.
338 int nfp_net_irqs_alloc(struct nfp_net *nn)
342 wanted_irqs = nfp_net_irqs_wanted(nn);
344 nn->num_irqs = nfp_net_msix_alloc(nn, wanted_irqs);
345 if (nn->num_irqs == 0) {
346 nn_err(nn, "Failed to allocate MSI-X IRQs\n");
350 nn->num_r_vecs = nn->num_irqs - NFP_NET_NON_Q_VECTORS;
352 if (nn->num_irqs < wanted_irqs)
353 nn_warn(nn, "Unable to allocate %d vectors. Got %d instead\n",
354 wanted_irqs, nn->num_irqs);
360 * nfp_net_irqs_disable() - Disable interrupts
361 * @nn: NFP Network structure
363 * Undoes what @nfp_net_irqs_alloc() does.
365 void nfp_net_irqs_disable(struct nfp_net *nn)
367 pci_disable_msix(nn->pdev);
371 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
373 * @data: Opaque data structure
375 * Return: Indicate if the interrupt has been handled.
377 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
379 struct nfp_net_r_vector *r_vec = data;
381 napi_schedule_irqoff(&r_vec->napi);
383 /* The FW auto-masks any interrupt, either via the MASK bit in
384 * the MSI-X table or via the per entry ICR field. So there
385 * is no need to disable interrupts here.
391 * nfp_net_read_link_status() - Reread link status from control BAR
392 * @nn: NFP Network structure
394 static void nfp_net_read_link_status(struct nfp_net *nn)
400 spin_lock_irqsave(&nn->link_status_lock, flags);
402 sts = nn_readl(nn, NFP_NET_CFG_STS);
403 link_up = !!(sts & NFP_NET_CFG_STS_LINK);
405 if (nn->link_up == link_up)
408 nn->link_up = link_up;
411 netif_carrier_on(nn->netdev);
412 netdev_info(nn->netdev, "NIC Link is Up\n");
414 netif_carrier_off(nn->netdev);
415 netdev_info(nn->netdev, "NIC Link is Down\n");
418 spin_unlock_irqrestore(&nn->link_status_lock, flags);
422 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
424 * @data: Opaque data structure
426 * Return: Indicate if the interrupt has been handled.
428 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
430 struct nfp_net *nn = data;
432 nfp_net_read_link_status(nn);
434 nfp_net_irq_unmask(nn, NFP_NET_IRQ_LSC_IDX);
440 * nfp_net_irq_exn() - Interrupt service routine for exceptions
442 * @data: Opaque data structure
444 * Return: Indicate if the interrupt has been handled.
446 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
448 struct nfp_net *nn = data;
450 nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
451 /* XXX TO BE IMPLEMENTED */
456 * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
457 * @tx_ring: TX ring structure
458 * @r_vec: IRQ vector servicing this ring
462 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
463 struct nfp_net_r_vector *r_vec, unsigned int idx)
465 struct nfp_net *nn = r_vec->nfp_net;
468 tx_ring->r_vec = r_vec;
470 tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
471 tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
475 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
476 * @rx_ring: RX ring structure
477 * @r_vec: IRQ vector servicing this ring
481 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
482 struct nfp_net_r_vector *r_vec, unsigned int idx)
484 struct nfp_net *nn = r_vec->nfp_net;
487 rx_ring->r_vec = r_vec;
489 rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
490 rx_ring->rx_qcidx = rx_ring->fl_qcidx + (nn->stride_rx - 1);
492 rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
493 rx_ring->qcp_rx = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->rx_qcidx);
497 * nfp_net_irqs_assign() - Assign IRQs and setup rvecs.
498 * @netdev: netdev structure
500 static void nfp_net_irqs_assign(struct net_device *netdev)
502 struct nfp_net *nn = netdev_priv(netdev);
503 struct nfp_net_r_vector *r_vec;
506 /* Assumes nn->num_tx_rings == nn->num_rx_rings */
507 if (nn->num_tx_rings > nn->num_r_vecs) {
508 nn_warn(nn, "More rings (%d) than vectors (%d).\n",
509 nn->num_tx_rings, nn->num_r_vecs);
510 nn->num_tx_rings = nn->num_r_vecs;
511 nn->num_rx_rings = nn->num_r_vecs;
514 nn->lsc_handler = nfp_net_irq_lsc;
515 nn->exn_handler = nfp_net_irq_exn;
517 for (r = 0; r < nn->num_r_vecs; r++) {
518 r_vec = &nn->r_vecs[r];
520 r_vec->handler = nfp_net_irq_rxtx;
521 r_vec->irq_idx = NFP_NET_NON_Q_VECTORS + r;
523 cpumask_set_cpu(r, &r_vec->affinity_mask);
528 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
529 * @nn: NFP Network structure
530 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
531 * @format: printf-style format to construct the interrupt name
532 * @name: Pointer to allocated space for interrupt name
533 * @name_sz: Size of space for interrupt name
534 * @vector_idx: Index of MSI-X vector used for this interrupt
535 * @handler: IRQ handler to register for this interrupt
538 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
539 const char *format, char *name, size_t name_sz,
540 unsigned int vector_idx, irq_handler_t handler)
542 struct msix_entry *entry;
545 entry = &nn->irq_entries[vector_idx];
547 snprintf(name, name_sz, format, netdev_name(nn->netdev));
548 err = request_irq(entry->vector, handler, 0, name, nn);
550 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
554 nn_writeb(nn, ctrl_offset, vector_idx);
560 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
561 * @nn: NFP Network structure
562 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
563 * @vector_idx: Index of MSI-X vector used for this interrupt
565 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
566 unsigned int vector_idx)
568 nn_writeb(nn, ctrl_offset, 0xff);
569 free_irq(nn->irq_entries[vector_idx].vector, nn);
574 * One queue controller peripheral queue is used for transmit. The
575 * driver en-queues packets for transmit by advancing the write
576 * pointer. The device indicates that packets have transmitted by
577 * advancing the read pointer. The driver maintains a local copy of
578 * the read and write pointer in @struct nfp_net_tx_ring. The driver
579 * keeps @wr_p in sync with the queue controller write pointer and can
580 * determine how many packets have been transmitted by comparing its
581 * copy of the read pointer @rd_p with the read pointer maintained by
582 * the queue controller peripheral.
586 * nfp_net_tx_full() - Check if the TX ring is full
587 * @tx_ring: TX ring to check
588 * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
590 * This function checks, based on the *host copy* of read/write
591 * pointer if a given TX ring is full. The real TX queue may have
592 * some newly made available slots.
594 * Return: True if the ring is full.
596 static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
598 return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
601 /* Wrappers for deciding when to stop and restart TX queues */
602 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
604 return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
607 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
609 return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
613 * nfp_net_tx_ring_stop() - stop tx ring
614 * @nd_q: netdev queue
615 * @tx_ring: driver tx queue structure
617 * Safely stop TX ring. Remember that while we are running .start_xmit()
618 * someone else may be cleaning the TX ring completions so we need to be
619 * extra careful here.
621 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
622 struct nfp_net_tx_ring *tx_ring)
624 netif_tx_stop_queue(nd_q);
626 /* We can race with the TX completion out of NAPI so recheck */
628 if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
629 netif_tx_start_queue(nd_q);
633 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
634 * @nn: NFP Net device
635 * @r_vec: per-ring structure
636 * @txbuf: Pointer to driver soft TX descriptor
637 * @txd: Pointer to HW TX descriptor
638 * @skb: Pointer to SKB
640 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
641 * Return error on packet header greater than maximum supported LSO header size.
643 static void nfp_net_tx_tso(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
644 struct nfp_net_tx_buf *txbuf,
645 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
650 if (!skb_is_gso(skb))
653 if (!skb->encapsulation)
654 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
656 hdrlen = skb_inner_transport_header(skb) - skb->data +
657 inner_tcp_hdrlen(skb);
659 txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
660 txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
662 mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
663 txd->l4_offset = hdrlen;
664 txd->mss = cpu_to_le16(mss);
665 txd->flags |= PCIE_DESC_TX_LSO;
667 u64_stats_update_begin(&r_vec->tx_sync);
669 u64_stats_update_end(&r_vec->tx_sync);
673 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
674 * @nn: NFP Net device
675 * @r_vec: per-ring structure
676 * @txbuf: Pointer to driver soft TX descriptor
677 * @txd: Pointer to TX descriptor
678 * @skb: Pointer to SKB
680 * This function sets the TX checksum flags in the TX descriptor based
681 * on the configuration and the protocol of the packet to be transmitted.
683 static void nfp_net_tx_csum(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
684 struct nfp_net_tx_buf *txbuf,
685 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
687 struct ipv6hdr *ipv6h;
691 if (!(nn->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
694 if (skb->ip_summed != CHECKSUM_PARTIAL)
697 txd->flags |= PCIE_DESC_TX_CSUM;
698 if (skb->encapsulation)
699 txd->flags |= PCIE_DESC_TX_ENCAP;
701 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
702 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
704 if (iph->version == 4) {
705 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
706 l4_hdr = iph->protocol;
707 } else if (ipv6h->version == 6) {
708 l4_hdr = ipv6h->nexthdr;
710 nn_warn_ratelimit(nn, "partial checksum but ipv=%x!\n",
717 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
720 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
723 nn_warn_ratelimit(nn, "partial checksum but l4 proto=%x!\n",
728 u64_stats_update_begin(&r_vec->tx_sync);
729 if (skb->encapsulation)
730 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
732 r_vec->hw_csum_tx += txbuf->pkt_cnt;
733 u64_stats_update_end(&r_vec->tx_sync);
737 * nfp_net_tx() - Main transmit entry point
738 * @skb: SKB to transmit
739 * @netdev: netdev structure
741 * Return: NETDEV_TX_OK on success.
743 static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
745 struct nfp_net *nn = netdev_priv(netdev);
746 const struct skb_frag_struct *frag;
747 struct nfp_net_r_vector *r_vec;
748 struct nfp_net_tx_desc *txd, txdg;
749 struct nfp_net_tx_buf *txbuf;
750 struct nfp_net_tx_ring *tx_ring;
751 struct netdev_queue *nd_q;
758 qidx = skb_get_queue_mapping(skb);
759 tx_ring = &nn->tx_rings[qidx];
760 r_vec = tx_ring->r_vec;
761 nd_q = netdev_get_tx_queue(nn->netdev, qidx);
763 nr_frags = skb_shinfo(skb)->nr_frags;
765 if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
766 nn_warn_ratelimit(nn, "TX ring %d busy. wrp=%u rdp=%u\n",
767 qidx, tx_ring->wr_p, tx_ring->rd_p);
768 netif_tx_stop_queue(nd_q);
769 u64_stats_update_begin(&r_vec->tx_sync);
771 u64_stats_update_end(&r_vec->tx_sync);
772 return NETDEV_TX_BUSY;
775 /* Start with the head skbuf */
776 dma_addr = dma_map_single(&nn->pdev->dev, skb->data, skb_headlen(skb),
778 if (dma_mapping_error(&nn->pdev->dev, dma_addr))
781 wr_idx = tx_ring->wr_p & (tx_ring->cnt - 1);
783 /* Stash the soft descriptor of the head then initialize it */
784 txbuf = &tx_ring->txbufs[wr_idx];
786 txbuf->dma_addr = dma_addr;
789 txbuf->real_len = skb->len;
791 /* Build TX descriptor */
792 txd = &tx_ring->txds[wr_idx];
793 txd->offset_eop = (nr_frags == 0) ? PCIE_DESC_TX_EOP : 0;
794 txd->dma_len = cpu_to_le16(skb_headlen(skb));
795 nfp_desc_set_dma_addr(txd, dma_addr);
796 txd->data_len = cpu_to_le16(skb->len);
802 nfp_net_tx_tso(nn, r_vec, txbuf, txd, skb);
804 nfp_net_tx_csum(nn, r_vec, txbuf, txd, skb);
806 if (skb_vlan_tag_present(skb) && nn->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
807 txd->flags |= PCIE_DESC_TX_VLAN;
808 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
813 /* all descs must match except for in addr, length and eop */
816 for (f = 0; f < nr_frags; f++) {
817 frag = &skb_shinfo(skb)->frags[f];
818 fsize = skb_frag_size(frag);
820 dma_addr = skb_frag_dma_map(&nn->pdev->dev, frag, 0,
821 fsize, DMA_TO_DEVICE);
822 if (dma_mapping_error(&nn->pdev->dev, dma_addr))
825 wr_idx = (wr_idx + 1) & (tx_ring->cnt - 1);
826 tx_ring->txbufs[wr_idx].skb = skb;
827 tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
828 tx_ring->txbufs[wr_idx].fidx = f;
830 txd = &tx_ring->txds[wr_idx];
832 txd->dma_len = cpu_to_le16(fsize);
833 nfp_desc_set_dma_addr(txd, dma_addr);
835 (f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
838 u64_stats_update_begin(&r_vec->tx_sync);
840 u64_stats_update_end(&r_vec->tx_sync);
843 netdev_tx_sent_queue(nd_q, txbuf->real_len);
845 tx_ring->wr_p += nr_frags + 1;
846 if (nfp_net_tx_ring_should_stop(tx_ring))
847 nfp_net_tx_ring_stop(nd_q, tx_ring);
849 tx_ring->wr_ptr_add += nr_frags + 1;
850 if (!skb->xmit_more || netif_xmit_stopped(nd_q)) {
851 /* force memory write before we let HW know */
853 nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
854 tx_ring->wr_ptr_add = 0;
857 skb_tx_timestamp(skb);
864 frag = &skb_shinfo(skb)->frags[f];
865 dma_unmap_page(&nn->pdev->dev,
866 tx_ring->txbufs[wr_idx].dma_addr,
867 skb_frag_size(frag), DMA_TO_DEVICE);
868 tx_ring->txbufs[wr_idx].skb = NULL;
869 tx_ring->txbufs[wr_idx].dma_addr = 0;
870 tx_ring->txbufs[wr_idx].fidx = -2;
873 wr_idx += tx_ring->cnt;
875 dma_unmap_single(&nn->pdev->dev, tx_ring->txbufs[wr_idx].dma_addr,
876 skb_headlen(skb), DMA_TO_DEVICE);
877 tx_ring->txbufs[wr_idx].skb = NULL;
878 tx_ring->txbufs[wr_idx].dma_addr = 0;
879 tx_ring->txbufs[wr_idx].fidx = -2;
881 nn_warn_ratelimit(nn, "Failed to map DMA TX buffer\n");
882 u64_stats_update_begin(&r_vec->tx_sync);
884 u64_stats_update_end(&r_vec->tx_sync);
885 dev_kfree_skb_any(skb);
890 * nfp_net_tx_complete() - Handled completed TX packets
891 * @tx_ring: TX ring structure
893 * Return: Number of completed TX descriptors
895 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring)
897 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
898 struct nfp_net *nn = r_vec->nfp_net;
899 const struct skb_frag_struct *frag;
900 struct netdev_queue *nd_q;
901 u32 done_pkts = 0, done_bytes = 0;
908 /* Work out how many descriptors have been transmitted */
909 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
911 if (qcp_rd_p == tx_ring->qcp_rd_p)
914 if (qcp_rd_p > tx_ring->qcp_rd_p)
915 todo = qcp_rd_p - tx_ring->qcp_rd_p;
917 todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
920 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
923 skb = tx_ring->txbufs[idx].skb;
927 nr_frags = skb_shinfo(skb)->nr_frags;
928 fidx = tx_ring->txbufs[idx].fidx;
932 dma_unmap_single(&nn->pdev->dev,
933 tx_ring->txbufs[idx].dma_addr,
934 skb_headlen(skb), DMA_TO_DEVICE);
936 done_pkts += tx_ring->txbufs[idx].pkt_cnt;
937 done_bytes += tx_ring->txbufs[idx].real_len;
940 frag = &skb_shinfo(skb)->frags[fidx];
941 dma_unmap_page(&nn->pdev->dev,
942 tx_ring->txbufs[idx].dma_addr,
943 skb_frag_size(frag), DMA_TO_DEVICE);
946 /* check for last gather fragment */
947 if (fidx == nr_frags - 1)
948 dev_kfree_skb_any(skb);
950 tx_ring->txbufs[idx].dma_addr = 0;
951 tx_ring->txbufs[idx].skb = NULL;
952 tx_ring->txbufs[idx].fidx = -2;
955 tx_ring->qcp_rd_p = qcp_rd_p;
957 u64_stats_update_begin(&r_vec->tx_sync);
958 r_vec->tx_bytes += done_bytes;
959 r_vec->tx_pkts += done_pkts;
960 u64_stats_update_end(&r_vec->tx_sync);
962 nd_q = netdev_get_tx_queue(nn->netdev, tx_ring->idx);
963 netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
964 if (nfp_net_tx_ring_should_wake(tx_ring)) {
965 /* Make sure TX thread will see updated tx_ring->rd_p */
968 if (unlikely(netif_tx_queue_stopped(nd_q)))
969 netif_tx_wake_queue(nd_q);
972 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
973 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
974 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
978 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
979 * @nn: NFP Net device
980 * @tx_ring: TX ring structure
982 * Assumes that the device is stopped
985 nfp_net_tx_ring_reset(struct nfp_net *nn, struct nfp_net_tx_ring *tx_ring)
987 const struct skb_frag_struct *frag;
988 struct netdev_queue *nd_q;
989 struct pci_dev *pdev = nn->pdev;
991 while (tx_ring->rd_p != tx_ring->wr_p) {
992 int nr_frags, fidx, idx;
995 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
996 skb = tx_ring->txbufs[idx].skb;
997 nr_frags = skb_shinfo(skb)->nr_frags;
998 fidx = tx_ring->txbufs[idx].fidx;
1002 dma_unmap_single(&pdev->dev,
1003 tx_ring->txbufs[idx].dma_addr,
1004 skb_headlen(skb), DMA_TO_DEVICE);
1006 /* unmap fragment */
1007 frag = &skb_shinfo(skb)->frags[fidx];
1008 dma_unmap_page(&pdev->dev,
1009 tx_ring->txbufs[idx].dma_addr,
1010 skb_frag_size(frag), DMA_TO_DEVICE);
1013 /* check for last gather fragment */
1014 if (fidx == nr_frags - 1)
1015 dev_kfree_skb_any(skb);
1017 tx_ring->txbufs[idx].dma_addr = 0;
1018 tx_ring->txbufs[idx].skb = NULL;
1019 tx_ring->txbufs[idx].fidx = -2;
1021 tx_ring->qcp_rd_p++;
1025 memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
1028 tx_ring->qcp_rd_p = 0;
1029 tx_ring->wr_ptr_add = 0;
1031 nd_q = netdev_get_tx_queue(nn->netdev, tx_ring->idx);
1032 netdev_tx_reset_queue(nd_q);
1035 static void nfp_net_tx_timeout(struct net_device *netdev)
1037 struct nfp_net *nn = netdev_priv(netdev);
1040 for (i = 0; i < nn->num_tx_rings; i++) {
1041 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1043 nn_warn(nn, "TX timeout on ring: %d\n", i);
1045 nn_warn(nn, "TX watchdog timeout\n");
1048 /* Receive processing
1051 nfp_net_calc_fl_bufsz(struct nfp_net *nn, unsigned int mtu)
1053 unsigned int fl_bufsz;
1055 fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1056 if (nn->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1057 fl_bufsz += NFP_NET_MAX_PREPEND;
1059 fl_bufsz += nn->rx_offset;
1060 fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + mtu;
1062 fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1063 fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1069 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1070 * @rx_ring: RX ring structure of the skb
1071 * @dma_addr: Pointer to storage for DMA address (output param)
1072 * @fl_bufsz: size of freelist buffers
1074 * This function will allcate a new page frag, map it for DMA.
1076 * Return: allocated page frag or NULL on failure.
1079 nfp_net_rx_alloc_one(struct nfp_net_rx_ring *rx_ring, dma_addr_t *dma_addr,
1080 unsigned int fl_bufsz)
1082 struct nfp_net *nn = rx_ring->r_vec->nfp_net;
1085 frag = netdev_alloc_frag(fl_bufsz);
1087 nn_warn_ratelimit(nn, "Failed to alloc receive page frag\n");
1091 *dma_addr = nfp_net_dma_map_rx(nn, frag, fl_bufsz, DMA_FROM_DEVICE);
1092 if (dma_mapping_error(&nn->pdev->dev, *dma_addr)) {
1093 skb_free_frag(frag);
1094 nn_warn_ratelimit(nn, "Failed to map DMA RX buffer\n");
1101 static void *nfp_net_napi_alloc_one(struct nfp_net *nn, dma_addr_t *dma_addr)
1105 frag = napi_alloc_frag(nn->fl_bufsz);
1107 nn_warn_ratelimit(nn, "Failed to alloc receive page frag\n");
1111 *dma_addr = nfp_net_dma_map_rx(nn, frag, nn->fl_bufsz, DMA_FROM_DEVICE);
1112 if (dma_mapping_error(&nn->pdev->dev, *dma_addr)) {
1113 skb_free_frag(frag);
1114 nn_warn_ratelimit(nn, "Failed to map DMA RX buffer\n");
1122 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1123 * @rx_ring: RX ring structure
1124 * @frag: page fragment buffer
1125 * @dma_addr: DMA address of skb mapping
1127 static void nfp_net_rx_give_one(struct nfp_net_rx_ring *rx_ring,
1128 void *frag, dma_addr_t dma_addr)
1130 unsigned int wr_idx;
1132 wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
1134 /* Stash SKB and DMA address away */
1135 rx_ring->rxbufs[wr_idx].frag = frag;
1136 rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1138 /* Fill freelist descriptor */
1139 rx_ring->rxds[wr_idx].fld.reserved = 0;
1140 rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1141 nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld, dma_addr);
1144 rx_ring->wr_ptr_add++;
1145 if (rx_ring->wr_ptr_add >= NFP_NET_FL_BATCH) {
1146 /* Update write pointer of the freelist queue. Make
1147 * sure all writes are flushed before telling the hardware.
1150 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, rx_ring->wr_ptr_add);
1151 rx_ring->wr_ptr_add = 0;
1156 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1157 * @rx_ring: RX ring structure
1159 * Warning: Do *not* call if ring buffers were never put on the FW freelist
1160 * (i.e. device was not enabled)!
1162 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1164 unsigned int wr_idx, last_idx;
1166 /* Move the empty entry to the end of the list */
1167 wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
1168 last_idx = rx_ring->cnt - 1;
1169 rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1170 rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1171 rx_ring->rxbufs[last_idx].dma_addr = 0;
1172 rx_ring->rxbufs[last_idx].frag = NULL;
1174 memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
1177 rx_ring->wr_ptr_add = 0;
1181 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1182 * @nn: NFP Net device
1183 * @rx_ring: RX ring to remove buffers from
1185 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1186 * entries. After device is disabled nfp_net_rx_ring_reset() must be called
1187 * to restore required ring geometry.
1190 nfp_net_rx_ring_bufs_free(struct nfp_net *nn, struct nfp_net_rx_ring *rx_ring)
1194 for (i = 0; i < rx_ring->cnt - 1; i++) {
1195 /* NULL skb can only happen when initial filling of the ring
1196 * fails to allocate enough buffers and calls here to free
1197 * already allocated ones.
1199 if (!rx_ring->rxbufs[i].frag)
1202 nfp_net_dma_unmap_rx(nn, rx_ring->rxbufs[i].dma_addr,
1203 rx_ring->bufsz, DMA_FROM_DEVICE);
1204 skb_free_frag(rx_ring->rxbufs[i].frag);
1205 rx_ring->rxbufs[i].dma_addr = 0;
1206 rx_ring->rxbufs[i].frag = NULL;
1211 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1212 * @nn: NFP Net device
1213 * @rx_ring: RX ring to remove buffers from
1216 nfp_net_rx_ring_bufs_alloc(struct nfp_net *nn, struct nfp_net_rx_ring *rx_ring)
1218 struct nfp_net_rx_buf *rxbufs;
1221 rxbufs = rx_ring->rxbufs;
1223 for (i = 0; i < rx_ring->cnt - 1; i++) {
1225 nfp_net_rx_alloc_one(rx_ring, &rxbufs[i].dma_addr,
1227 if (!rxbufs[i].frag) {
1228 nfp_net_rx_ring_bufs_free(nn, rx_ring);
1237 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1238 * @rx_ring: RX ring to fill
1240 static void nfp_net_rx_ring_fill_freelist(struct nfp_net_rx_ring *rx_ring)
1244 for (i = 0; i < rx_ring->cnt - 1; i++)
1245 nfp_net_rx_give_one(rx_ring, rx_ring->rxbufs[i].frag,
1246 rx_ring->rxbufs[i].dma_addr);
1250 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1251 * @flags: RX descriptor flags field in CPU byte order
1253 static int nfp_net_rx_csum_has_errors(u16 flags)
1255 u16 csum_all_checked, csum_all_ok;
1257 csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1258 csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1260 return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1264 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1265 * @nn: NFP Net device
1266 * @r_vec: per-ring structure
1267 * @rxd: Pointer to RX descriptor
1268 * @skb: Pointer to SKB
1270 static void nfp_net_rx_csum(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1271 struct nfp_net_rx_desc *rxd, struct sk_buff *skb)
1273 skb_checksum_none_assert(skb);
1275 if (!(nn->netdev->features & NETIF_F_RXCSUM))
1278 if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1279 u64_stats_update_begin(&r_vec->rx_sync);
1280 r_vec->hw_csum_rx_error++;
1281 u64_stats_update_end(&r_vec->rx_sync);
1285 /* Assume that the firmware will never report inner CSUM_OK unless outer
1286 * L4 headers were successfully parsed. FW will always report zero UDP
1287 * checksum as CSUM_OK.
1289 if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1290 rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1291 __skb_incr_checksum_unnecessary(skb);
1292 u64_stats_update_begin(&r_vec->rx_sync);
1293 r_vec->hw_csum_rx_ok++;
1294 u64_stats_update_end(&r_vec->rx_sync);
1297 if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1298 rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1299 __skb_incr_checksum_unnecessary(skb);
1300 u64_stats_update_begin(&r_vec->rx_sync);
1301 r_vec->hw_csum_rx_inner_ok++;
1302 u64_stats_update_end(&r_vec->rx_sync);
1306 static void nfp_net_set_hash(struct net_device *netdev, struct sk_buff *skb,
1307 unsigned int type, __be32 *hash)
1309 if (!(netdev->features & NETIF_F_RXHASH))
1313 case NFP_NET_RSS_IPV4:
1314 case NFP_NET_RSS_IPV6:
1315 case NFP_NET_RSS_IPV6_EX:
1316 skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L3);
1319 skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L4);
1325 nfp_net_set_hash_desc(struct net_device *netdev, struct sk_buff *skb,
1326 struct nfp_net_rx_desc *rxd)
1328 struct nfp_net_rx_hash *rx_hash;
1330 if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1333 rx_hash = (struct nfp_net_rx_hash *)(skb->data - sizeof(*rx_hash));
1335 nfp_net_set_hash(netdev, skb, get_unaligned_be32(&rx_hash->hash_type),
1340 nfp_net_parse_meta(struct net_device *netdev, struct sk_buff *skb,
1343 u8 *data = skb->data - meta_len;
1346 meta_info = get_unaligned_be32(data);
1350 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1351 case NFP_NET_META_HASH:
1352 meta_info >>= NFP_NET_META_FIELD_SIZE;
1353 nfp_net_set_hash(netdev, skb,
1354 meta_info & NFP_NET_META_FIELD_MASK,
1358 case NFP_NET_META_MARK:
1359 skb->mark = get_unaligned_be32(data);
1366 meta_info >>= NFP_NET_META_FIELD_SIZE;
1373 nfp_net_rx_drop(struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring,
1374 struct nfp_net_rx_buf *rxbuf, struct sk_buff *skb)
1376 u64_stats_update_begin(&r_vec->rx_sync);
1378 u64_stats_update_end(&r_vec->rx_sync);
1380 /* skb is build based on the frag, free_skb() would free the frag
1381 * so to be able to reuse it we need an extra ref.
1383 if (skb && rxbuf && skb->head == rxbuf->frag)
1384 page_ref_inc(virt_to_head_page(rxbuf->frag));
1386 nfp_net_rx_give_one(rx_ring, rxbuf->frag, rxbuf->dma_addr);
1388 dev_kfree_skb_any(skb);
1392 * nfp_net_rx() - receive up to @budget packets on @rx_ring
1393 * @rx_ring: RX ring to receive from
1394 * @budget: NAPI budget
1396 * Note, this function is separated out from the napi poll function to
1397 * more cleanly separate packet receive code from other bookkeeping
1398 * functions performed in the napi poll function.
1400 * Return: Number of packets received.
1402 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1404 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1405 struct nfp_net *nn = r_vec->nfp_net;
1406 unsigned int data_len, meta_len;
1407 struct nfp_net_rx_buf *rxbuf;
1408 struct nfp_net_rx_desc *rxd;
1409 dma_addr_t new_dma_addr;
1410 struct sk_buff *skb;
1411 int pkts_polled = 0;
1415 while (pkts_polled < budget) {
1416 idx = rx_ring->rd_p & (rx_ring->cnt - 1);
1418 rxd = &rx_ring->rxds[idx];
1419 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1422 /* Memory barrier to ensure that we won't do other reads
1423 * before the DD bit.
1430 rxbuf = &rx_ring->rxbufs[idx];
1431 skb = build_skb(rxbuf->frag, nn->fl_bufsz);
1432 if (unlikely(!skb)) {
1433 nfp_net_rx_drop(r_vec, rx_ring, rxbuf, NULL);
1436 new_frag = nfp_net_napi_alloc_one(nn, &new_dma_addr);
1437 if (unlikely(!new_frag)) {
1438 nfp_net_rx_drop(r_vec, rx_ring, rxbuf, skb);
1442 nfp_net_dma_unmap_rx(nn, rx_ring->rxbufs[idx].dma_addr,
1443 nn->fl_bufsz, DMA_FROM_DEVICE);
1445 nfp_net_rx_give_one(rx_ring, new_frag, new_dma_addr);
1448 * <-- [rx_offset] -->
1449 * ---------------------------------------------------------
1450 * | [XX] | metadata | packet | XXXX |
1451 * ---------------------------------------------------------
1452 * <---------------- data_len --------------->
1454 * The rx_offset is fixed for all packets, the meta_len can vary
1455 * on a packet by packet basis. If rx_offset is set to zero
1456 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1457 * buffer and is immediately followed by the packet (no [XX]).
1459 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1460 data_len = le16_to_cpu(rxd->rxd.data_len);
1462 if (nn->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1463 skb_reserve(skb, NFP_NET_RX_BUF_HEADROOM + meta_len);
1466 NFP_NET_RX_BUF_HEADROOM + nn->rx_offset);
1467 skb_put(skb, data_len - meta_len);
1470 u64_stats_update_begin(&r_vec->rx_sync);
1472 r_vec->rx_bytes += skb->len;
1473 u64_stats_update_end(&r_vec->rx_sync);
1475 if (nn->fw_ver.major <= 3) {
1476 nfp_net_set_hash_desc(nn->netdev, skb, rxd);
1477 } else if (meta_len) {
1480 end = nfp_net_parse_meta(nn->netdev, skb, meta_len);
1481 if (unlikely(end != skb->data)) {
1482 nn_warn_ratelimit(nn, "invalid RX packet metadata\n");
1483 nfp_net_rx_drop(r_vec, rx_ring, NULL, skb);
1488 skb_record_rx_queue(skb, rx_ring->idx);
1489 skb->protocol = eth_type_trans(skb, nn->netdev);
1491 nfp_net_rx_csum(nn, r_vec, rxd, skb);
1493 if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1494 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1495 le16_to_cpu(rxd->rxd.vlan));
1497 napi_gro_receive(&rx_ring->r_vec->napi, skb);
1504 * nfp_net_poll() - napi poll function
1505 * @napi: NAPI structure
1506 * @budget: NAPI budget
1508 * Return: number of packets polled.
1510 static int nfp_net_poll(struct napi_struct *napi, int budget)
1512 struct nfp_net_r_vector *r_vec =
1513 container_of(napi, struct nfp_net_r_vector, napi);
1514 unsigned int pkts_polled;
1516 nfp_net_tx_complete(r_vec->tx_ring);
1518 pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1520 if (pkts_polled < budget) {
1521 napi_complete_done(napi, pkts_polled);
1522 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_idx);
1528 /* Setup and Configuration
1532 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
1533 * @tx_ring: TX ring to free
1535 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
1537 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1538 struct nfp_net *nn = r_vec->nfp_net;
1539 struct pci_dev *pdev = nn->pdev;
1541 kfree(tx_ring->txbufs);
1544 dma_free_coherent(&pdev->dev, tx_ring->size,
1545 tx_ring->txds, tx_ring->dma);
1548 tx_ring->txbufs = NULL;
1549 tx_ring->txds = NULL;
1555 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
1556 * @tx_ring: TX Ring structure to allocate
1557 * @cnt: Ring buffer count
1559 * Return: 0 on success, negative errno otherwise.
1561 static int nfp_net_tx_ring_alloc(struct nfp_net_tx_ring *tx_ring, u32 cnt)
1563 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1564 struct nfp_net *nn = r_vec->nfp_net;
1565 struct pci_dev *pdev = nn->pdev;
1570 tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
1571 tx_ring->txds = dma_zalloc_coherent(&pdev->dev, tx_ring->size,
1572 &tx_ring->dma, GFP_KERNEL);
1576 sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
1577 tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
1578 if (!tx_ring->txbufs)
1581 netif_set_xps_queue(nn->netdev, &r_vec->affinity_mask, tx_ring->idx);
1583 nn_dbg(nn, "TxQ%02d: QCidx=%02d cnt=%d dma=%#llx host=%p\n",
1584 tx_ring->idx, tx_ring->qcidx,
1585 tx_ring->cnt, (unsigned long long)tx_ring->dma, tx_ring->txds);
1590 nfp_net_tx_ring_free(tx_ring);
1594 static struct nfp_net_tx_ring *
1595 nfp_net_shadow_tx_rings_prepare(struct nfp_net *nn, u32 buf_cnt)
1597 struct nfp_net_tx_ring *rings;
1600 rings = kcalloc(nn->num_tx_rings, sizeof(*rings), GFP_KERNEL);
1604 for (r = 0; r < nn->num_tx_rings; r++) {
1605 nfp_net_tx_ring_init(&rings[r], nn->tx_rings[r].r_vec, r);
1607 if (nfp_net_tx_ring_alloc(&rings[r], buf_cnt))
1615 nfp_net_tx_ring_free(&rings[r]);
1620 static struct nfp_net_tx_ring *
1621 nfp_net_shadow_tx_rings_swap(struct nfp_net *nn, struct nfp_net_tx_ring *rings)
1623 struct nfp_net_tx_ring *old = nn->tx_rings;
1626 for (r = 0; r < nn->num_tx_rings; r++)
1627 old[r].r_vec->tx_ring = &rings[r];
1629 nn->tx_rings = rings;
1634 nfp_net_shadow_tx_rings_free(struct nfp_net *nn, struct nfp_net_tx_ring *rings)
1641 for (r = 0; r < nn->num_tx_rings; r++)
1642 nfp_net_tx_ring_free(&rings[r]);
1648 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
1649 * @rx_ring: RX ring to free
1651 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
1653 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1654 struct nfp_net *nn = r_vec->nfp_net;
1655 struct pci_dev *pdev = nn->pdev;
1657 kfree(rx_ring->rxbufs);
1660 dma_free_coherent(&pdev->dev, rx_ring->size,
1661 rx_ring->rxds, rx_ring->dma);
1664 rx_ring->rxbufs = NULL;
1665 rx_ring->rxds = NULL;
1671 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
1672 * @rx_ring: RX ring to allocate
1673 * @fl_bufsz: Size of buffers to allocate
1674 * @cnt: Ring buffer count
1676 * Return: 0 on success, negative errno otherwise.
1679 nfp_net_rx_ring_alloc(struct nfp_net_rx_ring *rx_ring, unsigned int fl_bufsz,
1682 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1683 struct nfp_net *nn = r_vec->nfp_net;
1684 struct pci_dev *pdev = nn->pdev;
1688 rx_ring->bufsz = fl_bufsz;
1690 rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
1691 rx_ring->rxds = dma_zalloc_coherent(&pdev->dev, rx_ring->size,
1692 &rx_ring->dma, GFP_KERNEL);
1696 sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
1697 rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
1698 if (!rx_ring->rxbufs)
1701 nn_dbg(nn, "RxQ%02d: FlQCidx=%02d RxQCidx=%02d cnt=%d dma=%#llx host=%p\n",
1702 rx_ring->idx, rx_ring->fl_qcidx, rx_ring->rx_qcidx,
1703 rx_ring->cnt, (unsigned long long)rx_ring->dma, rx_ring->rxds);
1708 nfp_net_rx_ring_free(rx_ring);
1712 static struct nfp_net_rx_ring *
1713 nfp_net_shadow_rx_rings_prepare(struct nfp_net *nn, unsigned int fl_bufsz,
1716 struct nfp_net_rx_ring *rings;
1719 rings = kcalloc(nn->num_rx_rings, sizeof(*rings), GFP_KERNEL);
1723 for (r = 0; r < nn->num_rx_rings; r++) {
1724 nfp_net_rx_ring_init(&rings[r], nn->rx_rings[r].r_vec, r);
1726 if (nfp_net_rx_ring_alloc(&rings[r], fl_bufsz, buf_cnt))
1729 if (nfp_net_rx_ring_bufs_alloc(nn, &rings[r]))
1737 nfp_net_rx_ring_bufs_free(nn, &rings[r]);
1739 nfp_net_rx_ring_free(&rings[r]);
1745 static struct nfp_net_rx_ring *
1746 nfp_net_shadow_rx_rings_swap(struct nfp_net *nn, struct nfp_net_rx_ring *rings)
1748 struct nfp_net_rx_ring *old = nn->rx_rings;
1751 for (r = 0; r < nn->num_rx_rings; r++)
1752 old[r].r_vec->rx_ring = &rings[r];
1754 nn->rx_rings = rings;
1759 nfp_net_shadow_rx_rings_free(struct nfp_net *nn, struct nfp_net_rx_ring *rings)
1766 for (r = 0; r < nn->num_r_vecs; r++) {
1767 nfp_net_rx_ring_bufs_free(nn, &rings[r]);
1768 nfp_net_rx_ring_free(&rings[r]);
1775 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1778 struct msix_entry *entry = &nn->irq_entries[r_vec->irq_idx];
1781 r_vec->tx_ring = &nn->tx_rings[idx];
1782 nfp_net_tx_ring_init(r_vec->tx_ring, r_vec, idx);
1784 r_vec->rx_ring = &nn->rx_rings[idx];
1785 nfp_net_rx_ring_init(r_vec->rx_ring, r_vec, idx);
1787 snprintf(r_vec->name, sizeof(r_vec->name),
1788 "%s-rxtx-%d", nn->netdev->name, idx);
1789 err = request_irq(entry->vector, r_vec->handler, 0, r_vec->name, r_vec);
1791 nn_err(nn, "Error requesting IRQ %d\n", entry->vector);
1794 disable_irq(entry->vector);
1797 netif_napi_add(nn->netdev, &r_vec->napi,
1798 nfp_net_poll, NAPI_POLL_WEIGHT);
1800 irq_set_affinity_hint(entry->vector, &r_vec->affinity_mask);
1802 nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, entry->vector, entry->entry);
1808 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
1810 struct msix_entry *entry = &nn->irq_entries[r_vec->irq_idx];
1812 irq_set_affinity_hint(entry->vector, NULL);
1813 netif_napi_del(&r_vec->napi);
1814 free_irq(entry->vector, r_vec);
1818 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
1819 * @nn: NFP Net device to reconfigure
1821 void nfp_net_rss_write_itbl(struct nfp_net *nn)
1825 for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
1826 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
1827 get_unaligned_le32(nn->rss_itbl + i));
1831 * nfp_net_rss_write_key() - Write RSS hash key to device
1832 * @nn: NFP Net device to reconfigure
1834 void nfp_net_rss_write_key(struct nfp_net *nn)
1838 for (i = 0; i < NFP_NET_CFG_RSS_KEY_SZ; i += 4)
1839 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
1840 get_unaligned_le32(nn->rss_key + i));
1844 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
1845 * @nn: NFP Net device to reconfigure
1847 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
1853 /* Compute factor used to convert coalesce '_usecs' parameters to
1854 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
1857 factor = nn->me_freq_mhz / 16;
1859 /* copy RX interrupt coalesce parameters */
1860 value = (nn->rx_coalesce_max_frames << 16) |
1861 (factor * nn->rx_coalesce_usecs);
1862 for (i = 0; i < nn->num_r_vecs; i++)
1863 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
1865 /* copy TX interrupt coalesce parameters */
1866 value = (nn->tx_coalesce_max_frames << 16) |
1867 (factor * nn->tx_coalesce_usecs);
1868 for (i = 0; i < nn->num_r_vecs; i++)
1869 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
1873 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
1874 * @nn: NFP Net device to reconfigure
1876 * Writes the MAC address from the netdev to the device control BAR. Does not
1877 * perform the required reconfig. We do a bit of byte swapping dance because
1880 static void nfp_net_write_mac_addr(struct nfp_net *nn)
1882 nn_writel(nn, NFP_NET_CFG_MACADDR + 0,
1883 get_unaligned_be32(nn->netdev->dev_addr));
1884 nn_writew(nn, NFP_NET_CFG_MACADDR + 6,
1885 get_unaligned_be16(nn->netdev->dev_addr + 4));
1888 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
1890 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
1891 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
1892 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
1894 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
1895 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
1896 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
1900 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
1901 * @nn: NFP Net device to reconfigure
1903 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
1905 u32 new_ctrl, update;
1909 new_ctrl = nn->ctrl;
1910 new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
1911 update = NFP_NET_CFG_UPDATE_GEN;
1912 update |= NFP_NET_CFG_UPDATE_MSIX;
1913 update |= NFP_NET_CFG_UPDATE_RING;
1915 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
1916 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
1918 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
1919 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
1921 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1922 err = nfp_net_reconfig(nn, update);
1924 nn_err(nn, "Could not disable device: %d\n", err);
1926 for (r = 0; r < nn->num_r_vecs; r++) {
1927 nfp_net_rx_ring_reset(nn->r_vecs[r].rx_ring);
1928 nfp_net_tx_ring_reset(nn, nn->r_vecs[r].tx_ring);
1929 nfp_net_vec_clear_ring_data(nn, r);
1932 nn->ctrl = new_ctrl;
1936 nfp_net_vec_write_ring_data(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1939 /* Write the DMA address, size and MSI-X info to the device */
1940 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), r_vec->rx_ring->dma);
1941 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(r_vec->rx_ring->cnt));
1942 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), r_vec->irq_idx);
1944 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), r_vec->tx_ring->dma);
1945 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(r_vec->tx_ring->cnt));
1946 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), r_vec->irq_idx);
1949 static int __nfp_net_set_config_and_enable(struct nfp_net *nn)
1951 u32 new_ctrl, update = 0;
1955 new_ctrl = nn->ctrl;
1957 if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
1958 nfp_net_rss_write_key(nn);
1959 nfp_net_rss_write_itbl(nn);
1960 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
1961 update |= NFP_NET_CFG_UPDATE_RSS;
1964 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
1965 nfp_net_coalesce_write_cfg(nn);
1967 new_ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
1968 update |= NFP_NET_CFG_UPDATE_IRQMOD;
1971 for (r = 0; r < nn->num_r_vecs; r++)
1972 nfp_net_vec_write_ring_data(nn, &nn->r_vecs[r], r);
1974 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->num_tx_rings == 64 ?
1975 0xffffffffffffffffULL : ((u64)1 << nn->num_tx_rings) - 1);
1977 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->num_rx_rings == 64 ?
1978 0xffffffffffffffffULL : ((u64)1 << nn->num_rx_rings) - 1);
1980 nfp_net_write_mac_addr(nn);
1982 nn_writel(nn, NFP_NET_CFG_MTU, nn->netdev->mtu);
1983 nn_writel(nn, NFP_NET_CFG_FLBUFSZ, nn->fl_bufsz);
1986 new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
1987 update |= NFP_NET_CFG_UPDATE_GEN;
1988 update |= NFP_NET_CFG_UPDATE_MSIX;
1989 update |= NFP_NET_CFG_UPDATE_RING;
1990 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
1991 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
1993 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1994 err = nfp_net_reconfig(nn, update);
1996 nn->ctrl = new_ctrl;
1998 for (r = 0; r < nn->num_r_vecs; r++)
1999 nfp_net_rx_ring_fill_freelist(nn->r_vecs[r].rx_ring);
2001 /* Since reconfiguration requests while NFP is down are ignored we
2002 * have to wipe the entire VXLAN configuration and reinitialize it.
2004 if (nn->ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2005 memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2006 memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2007 udp_tunnel_get_rx_info(nn->netdev);
2014 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2015 * @nn: NFP Net device to reconfigure
2017 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2021 err = __nfp_net_set_config_and_enable(nn);
2023 nfp_net_clear_config_and_disable(nn);
2029 * nfp_net_open_stack() - Start the device from stack's perspective
2030 * @nn: NFP Net device to reconfigure
2032 static void nfp_net_open_stack(struct nfp_net *nn)
2036 for (r = 0; r < nn->num_r_vecs; r++) {
2037 napi_enable(&nn->r_vecs[r].napi);
2038 enable_irq(nn->irq_entries[nn->r_vecs[r].irq_idx].vector);
2041 netif_tx_wake_all_queues(nn->netdev);
2043 enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2044 nfp_net_read_link_status(nn);
2047 static int nfp_net_netdev_open(struct net_device *netdev)
2049 struct nfp_net *nn = netdev_priv(netdev);
2052 if (nn->ctrl & NFP_NET_CFG_CTRL_ENABLE) {
2053 nn_err(nn, "Dev is already enabled: 0x%08x\n", nn->ctrl);
2057 /* Step 1: Allocate resources for rings and the like
2058 * - Request interrupts
2059 * - Allocate RX and TX ring resources
2060 * - Setup initial RSS table
2062 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2063 nn->exn_name, sizeof(nn->exn_name),
2064 NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2067 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2068 nn->lsc_name, sizeof(nn->lsc_name),
2069 NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2072 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2074 nn->rx_rings = kcalloc(nn->num_rx_rings, sizeof(*nn->rx_rings),
2076 if (!nn->rx_rings) {
2080 nn->tx_rings = kcalloc(nn->num_tx_rings, sizeof(*nn->tx_rings),
2082 if (!nn->tx_rings) {
2084 goto err_free_rx_rings;
2087 for (r = 0; r < nn->num_r_vecs; r++) {
2088 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2090 goto err_free_prev_vecs;
2092 err = nfp_net_tx_ring_alloc(nn->r_vecs[r].tx_ring, nn->txd_cnt);
2094 goto err_cleanup_vec_p;
2096 err = nfp_net_rx_ring_alloc(nn->r_vecs[r].rx_ring,
2097 nn->fl_bufsz, nn->rxd_cnt);
2099 goto err_free_tx_ring_p;
2101 err = nfp_net_rx_ring_bufs_alloc(nn, nn->r_vecs[r].rx_ring);
2103 goto err_flush_rx_ring_p;
2106 err = netif_set_real_num_tx_queues(netdev, nn->num_tx_rings);
2108 goto err_free_rings;
2110 err = netif_set_real_num_rx_queues(netdev, nn->num_rx_rings);
2112 goto err_free_rings;
2114 /* Step 2: Configure the NFP
2115 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2116 * - Write MAC address (in case it changed)
2118 * - Set the Freelist buffer size
2121 err = nfp_net_set_config_and_enable(nn);
2123 goto err_free_rings;
2125 /* Step 3: Enable for kernel
2126 * - put some freelist descriptors on each RX ring
2127 * - enable NAPI on each ring
2128 * - enable all TX queues
2131 nfp_net_open_stack(nn);
2139 nfp_net_rx_ring_bufs_free(nn, nn->r_vecs[r].rx_ring);
2140 err_flush_rx_ring_p:
2141 nfp_net_rx_ring_free(nn->r_vecs[r].rx_ring);
2143 nfp_net_tx_ring_free(nn->r_vecs[r].tx_ring);
2145 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2147 kfree(nn->tx_rings);
2149 kfree(nn->rx_rings);
2151 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2153 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2158 * nfp_net_close_stack() - Quiescent the stack (part of close)
2159 * @nn: NFP Net device to reconfigure
2161 static void nfp_net_close_stack(struct nfp_net *nn)
2165 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2166 netif_carrier_off(nn->netdev);
2167 nn->link_up = false;
2169 for (r = 0; r < nn->num_r_vecs; r++) {
2170 disable_irq(nn->irq_entries[nn->r_vecs[r].irq_idx].vector);
2171 napi_disable(&nn->r_vecs[r].napi);
2174 netif_tx_disable(nn->netdev);
2178 * nfp_net_close_free_all() - Free all runtime resources
2179 * @nn: NFP Net device to reconfigure
2181 static void nfp_net_close_free_all(struct nfp_net *nn)
2185 for (r = 0; r < nn->num_r_vecs; r++) {
2186 nfp_net_rx_ring_bufs_free(nn, nn->r_vecs[r].rx_ring);
2187 nfp_net_rx_ring_free(nn->r_vecs[r].rx_ring);
2188 nfp_net_tx_ring_free(nn->r_vecs[r].tx_ring);
2189 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2192 kfree(nn->rx_rings);
2193 kfree(nn->tx_rings);
2195 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2196 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2200 * nfp_net_netdev_close() - Called when the device is downed
2201 * @netdev: netdev structure
2203 static int nfp_net_netdev_close(struct net_device *netdev)
2205 struct nfp_net *nn = netdev_priv(netdev);
2207 if (!(nn->ctrl & NFP_NET_CFG_CTRL_ENABLE)) {
2208 nn_err(nn, "Dev is not up: 0x%08x\n", nn->ctrl);
2212 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2214 nfp_net_close_stack(nn);
2218 nfp_net_clear_config_and_disable(nn);
2220 /* Step 3: Free resources
2222 nfp_net_close_free_all(nn);
2224 nn_dbg(nn, "%s down", netdev->name);
2228 static void nfp_net_set_rx_mode(struct net_device *netdev)
2230 struct nfp_net *nn = netdev_priv(netdev);
2233 new_ctrl = nn->ctrl;
2235 if (netdev->flags & IFF_PROMISC) {
2236 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2237 new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2239 nn_warn(nn, "FW does not support promiscuous mode\n");
2241 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2244 if (new_ctrl == nn->ctrl)
2247 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2248 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2250 nn->ctrl = new_ctrl;
2253 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
2255 unsigned int old_mtu, old_fl_bufsz, new_fl_bufsz;
2256 struct nfp_net *nn = netdev_priv(netdev);
2257 struct nfp_net_rx_ring *tmp_rings;
2260 old_mtu = netdev->mtu;
2261 old_fl_bufsz = nn->fl_bufsz;
2262 new_fl_bufsz = nfp_net_calc_fl_bufsz(nn, new_mtu);
2264 if (!netif_running(netdev)) {
2265 netdev->mtu = new_mtu;
2266 nn->fl_bufsz = new_fl_bufsz;
2270 /* Prepare new rings */
2271 tmp_rings = nfp_net_shadow_rx_rings_prepare(nn, new_fl_bufsz,
2276 /* Stop device, swap in new rings, try to start the firmware */
2277 nfp_net_close_stack(nn);
2278 nfp_net_clear_config_and_disable(nn);
2280 tmp_rings = nfp_net_shadow_rx_rings_swap(nn, tmp_rings);
2282 netdev->mtu = new_mtu;
2283 nn->fl_bufsz = new_fl_bufsz;
2285 err = nfp_net_set_config_and_enable(nn);
2287 const int err_new = err;
2289 /* Try with old configuration and old rings */
2290 tmp_rings = nfp_net_shadow_rx_rings_swap(nn, tmp_rings);
2292 netdev->mtu = old_mtu;
2293 nn->fl_bufsz = old_fl_bufsz;
2295 err = __nfp_net_set_config_and_enable(nn);
2297 nn_err(nn, "Can't restore MTU - FW communication failed (%d,%d)\n",
2301 nfp_net_shadow_rx_rings_free(nn, tmp_rings);
2303 nfp_net_open_stack(nn);
2308 int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
2310 struct nfp_net_tx_ring *tx_rings = NULL;
2311 struct nfp_net_rx_ring *rx_rings = NULL;
2312 u32 old_rxd_cnt, old_txd_cnt;
2315 if (!netif_running(nn->netdev)) {
2316 nn->rxd_cnt = rxd_cnt;
2317 nn->txd_cnt = txd_cnt;
2321 old_rxd_cnt = nn->rxd_cnt;
2322 old_txd_cnt = nn->txd_cnt;
2324 /* Prepare new rings */
2325 if (nn->rxd_cnt != rxd_cnt) {
2326 rx_rings = nfp_net_shadow_rx_rings_prepare(nn, nn->fl_bufsz,
2331 if (nn->txd_cnt != txd_cnt) {
2332 tx_rings = nfp_net_shadow_tx_rings_prepare(nn, txd_cnt);
2334 nfp_net_shadow_rx_rings_free(nn, rx_rings);
2339 /* Stop device, swap in new rings, try to start the firmware */
2340 nfp_net_close_stack(nn);
2341 nfp_net_clear_config_and_disable(nn);
2344 rx_rings = nfp_net_shadow_rx_rings_swap(nn, rx_rings);
2346 tx_rings = nfp_net_shadow_tx_rings_swap(nn, tx_rings);
2348 nn->rxd_cnt = rxd_cnt;
2349 nn->txd_cnt = txd_cnt;
2351 err = nfp_net_set_config_and_enable(nn);
2353 const int err_new = err;
2355 /* Try with old configuration and old rings */
2357 rx_rings = nfp_net_shadow_rx_rings_swap(nn, rx_rings);
2359 tx_rings = nfp_net_shadow_tx_rings_swap(nn, tx_rings);
2361 nn->rxd_cnt = old_rxd_cnt;
2362 nn->txd_cnt = old_txd_cnt;
2364 err = __nfp_net_set_config_and_enable(nn);
2366 nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
2370 nfp_net_shadow_rx_rings_free(nn, rx_rings);
2371 nfp_net_shadow_tx_rings_free(nn, tx_rings);
2373 nfp_net_open_stack(nn);
2378 static struct rtnl_link_stats64 *nfp_net_stat64(struct net_device *netdev,
2379 struct rtnl_link_stats64 *stats)
2381 struct nfp_net *nn = netdev_priv(netdev);
2384 for (r = 0; r < nn->num_r_vecs; r++) {
2385 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
2390 start = u64_stats_fetch_begin(&r_vec->rx_sync);
2391 data[0] = r_vec->rx_pkts;
2392 data[1] = r_vec->rx_bytes;
2393 data[2] = r_vec->rx_drops;
2394 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
2395 stats->rx_packets += data[0];
2396 stats->rx_bytes += data[1];
2397 stats->rx_dropped += data[2];
2400 start = u64_stats_fetch_begin(&r_vec->tx_sync);
2401 data[0] = r_vec->tx_pkts;
2402 data[1] = r_vec->tx_bytes;
2403 data[2] = r_vec->tx_errors;
2404 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
2405 stats->tx_packets += data[0];
2406 stats->tx_bytes += data[1];
2407 stats->tx_errors += data[2];
2413 static bool nfp_net_ebpf_capable(struct nfp_net *nn)
2415 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
2416 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
2422 nfp_net_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
2423 struct tc_to_netdev *tc)
2425 struct nfp_net *nn = netdev_priv(netdev);
2427 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2429 if (proto != htons(ETH_P_ALL))
2432 if (tc->type == TC_SETUP_CLSBPF && nfp_net_ebpf_capable(nn))
2433 return nfp_net_bpf_offload(nn, handle, proto, tc->cls_bpf);
2438 static int nfp_net_set_features(struct net_device *netdev,
2439 netdev_features_t features)
2441 netdev_features_t changed = netdev->features ^ features;
2442 struct nfp_net *nn = netdev_priv(netdev);
2446 /* Assume this is not called with features we have not advertised */
2448 new_ctrl = nn->ctrl;
2450 if (changed & NETIF_F_RXCSUM) {
2451 if (features & NETIF_F_RXCSUM)
2452 new_ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
2454 new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM;
2457 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2458 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
2459 new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2461 new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
2464 if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
2465 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
2466 new_ctrl |= NFP_NET_CFG_CTRL_LSO;
2468 new_ctrl &= ~NFP_NET_CFG_CTRL_LSO;
2471 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2472 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2473 new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2475 new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
2478 if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
2479 if (features & NETIF_F_HW_VLAN_CTAG_TX)
2480 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2482 new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
2485 if (changed & NETIF_F_SG) {
2486 if (features & NETIF_F_SG)
2487 new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
2489 new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
2492 if (changed & NETIF_F_HW_TC && nn->ctrl & NFP_NET_CFG_CTRL_BPF) {
2493 nn_err(nn, "Cannot disable HW TC offload while in use\n");
2497 nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
2498 netdev->features, features, changed);
2500 if (new_ctrl == nn->ctrl)
2503 nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->ctrl, new_ctrl);
2504 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2505 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
2509 nn->ctrl = new_ctrl;
2514 static netdev_features_t
2515 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
2516 netdev_features_t features)
2520 /* We can't do TSO over double tagged packets (802.1AD) */
2521 features &= vlan_features_check(skb, features);
2523 if (!skb->encapsulation)
2526 /* Ensure that inner L4 header offset fits into TX descriptor field */
2527 if (skb_is_gso(skb)) {
2530 hdrlen = skb_inner_transport_header(skb) - skb->data +
2531 inner_tcp_hdrlen(skb);
2533 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
2534 features &= ~NETIF_F_GSO_MASK;
2537 /* VXLAN/GRE check */
2538 switch (vlan_get_protocol(skb)) {
2539 case htons(ETH_P_IP):
2540 l4_hdr = ip_hdr(skb)->protocol;
2542 case htons(ETH_P_IPV6):
2543 l4_hdr = ipv6_hdr(skb)->nexthdr;
2546 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2549 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
2550 skb->inner_protocol != htons(ETH_P_TEB) ||
2551 (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
2552 (l4_hdr == IPPROTO_UDP &&
2553 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
2554 sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
2555 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2561 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
2562 * @nn: NFP Net device to reconfigure
2563 * @idx: Index into the port table where new port should be written
2564 * @port: UDP port to configure (pass zero to remove VXLAN port)
2566 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
2570 nn->vxlan_ports[idx] = port;
2572 if (!(nn->ctrl & NFP_NET_CFG_CTRL_VXLAN))
2575 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
2576 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
2577 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
2578 be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
2579 be16_to_cpu(nn->vxlan_ports[i]));
2581 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
2585 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
2586 * @nn: NFP Network structure
2587 * @port: UDP port to look for
2589 * Return: if the port is already in the table -- it's position;
2590 * if the port is not in the table -- free position to use;
2591 * if the table is full -- -ENOSPC.
2593 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
2595 int i, free_idx = -ENOSPC;
2597 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
2598 if (nn->vxlan_ports[i] == port)
2600 if (!nn->vxlan_usecnt[i])
2607 static void nfp_net_add_vxlan_port(struct net_device *netdev,
2608 struct udp_tunnel_info *ti)
2610 struct nfp_net *nn = netdev_priv(netdev);
2613 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2616 idx = nfp_net_find_vxlan_idx(nn, ti->port);
2620 if (!nn->vxlan_usecnt[idx]++)
2621 nfp_net_set_vxlan_port(nn, idx, ti->port);
2624 static void nfp_net_del_vxlan_port(struct net_device *netdev,
2625 struct udp_tunnel_info *ti)
2627 struct nfp_net *nn = netdev_priv(netdev);
2630 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2633 idx = nfp_net_find_vxlan_idx(nn, ti->port);
2634 if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
2637 if (!--nn->vxlan_usecnt[idx])
2638 nfp_net_set_vxlan_port(nn, idx, 0);
2641 static const struct net_device_ops nfp_net_netdev_ops = {
2642 .ndo_open = nfp_net_netdev_open,
2643 .ndo_stop = nfp_net_netdev_close,
2644 .ndo_start_xmit = nfp_net_tx,
2645 .ndo_get_stats64 = nfp_net_stat64,
2646 .ndo_setup_tc = nfp_net_setup_tc,
2647 .ndo_tx_timeout = nfp_net_tx_timeout,
2648 .ndo_set_rx_mode = nfp_net_set_rx_mode,
2649 .ndo_change_mtu = nfp_net_change_mtu,
2650 .ndo_set_mac_address = eth_mac_addr,
2651 .ndo_set_features = nfp_net_set_features,
2652 .ndo_features_check = nfp_net_features_check,
2653 .ndo_udp_tunnel_add = nfp_net_add_vxlan_port,
2654 .ndo_udp_tunnel_del = nfp_net_del_vxlan_port,
2658 * nfp_net_info() - Print general info about the NIC
2659 * @nn: NFP Net device to reconfigure
2661 void nfp_net_info(struct nfp_net *nn)
2663 nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
2664 nn->is_vf ? "VF " : "",
2665 nn->num_tx_rings, nn->max_tx_rings,
2666 nn->num_rx_rings, nn->max_rx_rings);
2667 nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
2668 nn->fw_ver.resv, nn->fw_ver.class,
2669 nn->fw_ver.major, nn->fw_ver.minor,
2671 nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2673 nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
2674 nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
2675 nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
2676 nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
2677 nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
2678 nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
2679 nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
2680 nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
2681 nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
2682 nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO " : "",
2683 nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS " : "",
2684 nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
2685 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
2686 nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
2687 nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
2688 nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
2689 nfp_net_ebpf_capable(nn) ? "BPF " : "");
2693 * nfp_net_netdev_alloc() - Allocate netdev and related structure
2695 * @max_tx_rings: Maximum number of TX rings supported by device
2696 * @max_rx_rings: Maximum number of RX rings supported by device
2698 * This function allocates a netdev device and fills in the initial
2699 * part of the @struct nfp_net structure.
2701 * Return: NFP Net device structure, or ERR_PTR on error.
2703 struct nfp_net *nfp_net_netdev_alloc(struct pci_dev *pdev,
2704 int max_tx_rings, int max_rx_rings)
2706 struct net_device *netdev;
2710 netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
2711 max_tx_rings, max_rx_rings);
2713 return ERR_PTR(-ENOMEM);
2715 SET_NETDEV_DEV(netdev, &pdev->dev);
2716 nn = netdev_priv(netdev);
2718 nn->netdev = netdev;
2721 nn->max_tx_rings = max_tx_rings;
2722 nn->max_rx_rings = max_rx_rings;
2724 nqs = netif_get_num_default_rss_queues();
2725 nn->num_tx_rings = min_t(int, nqs, max_tx_rings);
2726 nn->num_rx_rings = min_t(int, nqs, max_rx_rings);
2728 nn->txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
2729 nn->rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
2731 spin_lock_init(&nn->reconfig_lock);
2732 spin_lock_init(&nn->rx_filter_lock);
2733 spin_lock_init(&nn->link_status_lock);
2735 setup_timer(&nn->reconfig_timer,
2736 nfp_net_reconfig_timer, (unsigned long)nn);
2737 setup_timer(&nn->rx_filter_stats_timer,
2738 nfp_net_filter_stats_timer, (unsigned long)nn);
2744 * nfp_net_netdev_free() - Undo what @nfp_net_netdev_alloc() did
2745 * @nn: NFP Net device to reconfigure
2747 void nfp_net_netdev_free(struct nfp_net *nn)
2749 free_netdev(nn->netdev);
2753 * nfp_net_rss_init() - Set the initial RSS parameters
2754 * @nn: NFP Net device to reconfigure
2756 static void nfp_net_rss_init(struct nfp_net *nn)
2760 netdev_rss_key_fill(nn->rss_key, NFP_NET_CFG_RSS_KEY_SZ);
2762 for (i = 0; i < sizeof(nn->rss_itbl); i++)
2764 ethtool_rxfh_indir_default(i, nn->num_rx_rings);
2766 /* Enable IPv4/IPv6 TCP by default */
2767 nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
2768 NFP_NET_CFG_RSS_IPV6_TCP |
2769 NFP_NET_CFG_RSS_TOEPLITZ |
2770 NFP_NET_CFG_RSS_MASK;
2774 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
2775 * @nn: NFP Net device to reconfigure
2777 static void nfp_net_irqmod_init(struct nfp_net *nn)
2779 nn->rx_coalesce_usecs = 50;
2780 nn->rx_coalesce_max_frames = 64;
2781 nn->tx_coalesce_usecs = 50;
2782 nn->tx_coalesce_max_frames = 64;
2786 * nfp_net_netdev_init() - Initialise/finalise the netdev structure
2787 * @netdev: netdev structure
2789 * Return: 0 on success or negative errno on error.
2791 int nfp_net_netdev_init(struct net_device *netdev)
2793 struct nfp_net *nn = netdev_priv(netdev);
2796 /* Get some of the read-only fields from the BAR */
2797 nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
2798 nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
2800 nfp_net_write_mac_addr(nn);
2802 /* Determine RX packet/metadata boundary offset */
2803 if (nn->fw_ver.major >= 2)
2804 nn->rx_offset = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
2806 nn->rx_offset = NFP_NET_RX_OFFSET;
2808 /* Set default MTU and Freelist buffer size */
2809 if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
2810 netdev->mtu = nn->max_mtu;
2812 netdev->mtu = NFP_NET_DEFAULT_MTU;
2813 nn->fl_bufsz = nfp_net_calc_fl_bufsz(nn, netdev->mtu);
2815 /* Advertise/enable offloads based on capabilities
2817 * Note: netdev->features show the currently enabled features
2818 * and netdev->hw_features advertises which features are
2819 * supported. By default we enable most features.
2821 netdev->hw_features = NETIF_F_HIGHDMA;
2822 if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM) {
2823 netdev->hw_features |= NETIF_F_RXCSUM;
2824 nn->ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
2826 if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
2827 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2828 nn->ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2830 if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
2831 netdev->hw_features |= NETIF_F_SG;
2832 nn->ctrl |= NFP_NET_CFG_CTRL_GATHER;
2834 if ((nn->cap & NFP_NET_CFG_CTRL_LSO) && nn->fw_ver.major > 2) {
2835 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2836 nn->ctrl |= NFP_NET_CFG_CTRL_LSO;
2838 if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
2839 netdev->hw_features |= NETIF_F_RXHASH;
2840 nfp_net_rss_init(nn);
2841 nn->ctrl |= NFP_NET_CFG_CTRL_RSS;
2843 if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
2844 nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
2845 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2846 netdev->hw_features |= NETIF_F_GSO_GRE |
2847 NETIF_F_GSO_UDP_TUNNEL;
2848 nn->ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
2850 netdev->hw_enc_features = netdev->hw_features;
2853 netdev->vlan_features = netdev->hw_features;
2855 if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
2856 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2857 nn->ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2859 if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
2860 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2861 nn->ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2864 netdev->features = netdev->hw_features;
2866 if (nfp_net_ebpf_capable(nn))
2867 netdev->hw_features |= NETIF_F_HW_TC;
2869 /* Advertise but disable TSO by default. */
2870 netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
2872 /* Allow L2 Broadcast and Multicast through by default, if supported */
2873 if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
2874 nn->ctrl |= NFP_NET_CFG_CTRL_L2BC;
2875 if (nn->cap & NFP_NET_CFG_CTRL_L2MC)
2876 nn->ctrl |= NFP_NET_CFG_CTRL_L2MC;
2878 /* Allow IRQ moderation, if supported */
2879 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2880 nfp_net_irqmod_init(nn);
2881 nn->ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2884 /* Stash the re-configuration queue away. First odd queue in TX Bar */
2885 nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
2887 /* Make sure the FW knows the netdev is supposed to be disabled here */
2888 nn_writel(nn, NFP_NET_CFG_CTRL, 0);
2889 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2890 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2891 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
2892 NFP_NET_CFG_UPDATE_GEN);
2896 /* Finalise the netdev setup */
2897 netdev->netdev_ops = &nfp_net_netdev_ops;
2898 netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
2900 /* MTU range: 68 - hw-specific max */
2901 netdev->min_mtu = ETH_MIN_MTU;
2902 netdev->max_mtu = nn->max_mtu;
2904 netif_carrier_off(netdev);
2906 nfp_net_set_ethtool_ops(netdev);
2907 nfp_net_irqs_assign(netdev);
2909 return register_netdev(netdev);
2913 * nfp_net_netdev_clean() - Undo what nfp_net_netdev_init() did.
2914 * @netdev: netdev structure
2916 void nfp_net_netdev_clean(struct net_device *netdev)
2918 unregister_netdev(netdev);