2 * Copyright (C) 2015-2017 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/bitfield.h>
45 #include <linux/bpf.h>
46 #include <linux/bpf_trace.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/interrupt.h>
55 #include <linux/ipv6.h>
56 #include <linux/page_ref.h>
57 #include <linux/pci.h>
58 #include <linux/pci_regs.h>
59 #include <linux/msi.h>
60 #include <linux/ethtool.h>
61 #include <linux/log2.h>
62 #include <linux/if_vlan.h>
63 #include <linux/random.h>
65 #include <linux/ktime.h>
67 #include <net/pkt_cls.h>
68 #include <net/vxlan.h>
70 #include "nfpcore/nfp_nsp.h"
71 #include "nfp_net_ctrl.h"
75 * nfp_net_get_fw_version() - Read and parse the FW version
76 * @fw_ver: Output fw_version structure to read to
77 * @ctrl_bar: Mapped address of the control BAR
79 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
80 void __iomem *ctrl_bar)
84 reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
85 put_unaligned_le32(reg, fw_ver);
88 static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
90 return dma_map_single_attrs(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
91 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
92 dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
96 nfp_net_dma_sync_dev_rx(const struct nfp_net_dp *dp, dma_addr_t dma_addr)
98 dma_sync_single_for_device(dp->dev, dma_addr,
99 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
103 static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
105 dma_unmap_single_attrs(dp->dev, dma_addr,
106 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
107 dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
110 static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr,
113 dma_sync_single_for_cpu(dp->dev, dma_addr - NFP_NET_RX_BUF_HEADROOM,
114 len, dp->rx_dma_dir);
119 * Firmware reconfig may take a while so we have two versions of it -
120 * synchronous and asynchronous (posted). All synchronous callers are holding
121 * RTNL so we don't have to worry about serializing them.
123 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
125 nn_writel(nn, NFP_NET_CFG_UPDATE, update);
126 /* ensure update is written before pinging HW */
128 nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
131 /* Pass 0 as update to run posted reconfigs. */
132 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
134 update |= nn->reconfig_posted;
135 nn->reconfig_posted = 0;
137 nfp_net_reconfig_start(nn, update);
139 nn->reconfig_timer_active = true;
140 mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
143 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
147 reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
150 if (reg & NFP_NET_CFG_UPDATE_ERR) {
151 nn_err(nn, "Reconfig error: 0x%08x\n", reg);
153 } else if (last_check) {
154 nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
161 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
163 bool timed_out = false;
165 /* Poll update field, waiting for NFP to ack the config */
166 while (!nfp_net_reconfig_check_done(nn, timed_out)) {
168 timed_out = time_is_before_eq_jiffies(deadline);
171 if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
174 return timed_out ? -EIO : 0;
177 static void nfp_net_reconfig_timer(unsigned long data)
179 struct nfp_net *nn = (void *)data;
181 spin_lock_bh(&nn->reconfig_lock);
183 nn->reconfig_timer_active = false;
185 /* If sync caller is present it will take over from us */
186 if (nn->reconfig_sync_present)
189 /* Read reconfig status and report errors */
190 nfp_net_reconfig_check_done(nn, true);
192 if (nn->reconfig_posted)
193 nfp_net_reconfig_start_async(nn, 0);
195 spin_unlock_bh(&nn->reconfig_lock);
199 * nfp_net_reconfig_post() - Post async reconfig request
200 * @nn: NFP Net device to reconfigure
201 * @update: The value for the update field in the BAR config
203 * Record FW reconfiguration request. Reconfiguration will be kicked off
204 * whenever reconfiguration machinery is idle. Multiple requests can be
207 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
209 spin_lock_bh(&nn->reconfig_lock);
211 /* Sync caller will kick off async reconf when it's done, just post */
212 if (nn->reconfig_sync_present) {
213 nn->reconfig_posted |= update;
217 /* Opportunistically check if the previous command is done */
218 if (!nn->reconfig_timer_active ||
219 nfp_net_reconfig_check_done(nn, false))
220 nfp_net_reconfig_start_async(nn, update);
222 nn->reconfig_posted |= update;
224 spin_unlock_bh(&nn->reconfig_lock);
228 * nfp_net_reconfig() - Reconfigure the firmware
229 * @nn: NFP Net device to reconfigure
230 * @update: The value for the update field in the BAR config
232 * Write the update word to the BAR and ping the reconfig queue. The
233 * poll until the firmware has acknowledged the update by zeroing the
236 * Return: Negative errno on error, 0 on success
238 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
240 bool cancelled_timer = false;
241 u32 pre_posted_requests;
244 spin_lock_bh(&nn->reconfig_lock);
246 nn->reconfig_sync_present = true;
248 if (nn->reconfig_timer_active) {
249 del_timer(&nn->reconfig_timer);
250 nn->reconfig_timer_active = false;
251 cancelled_timer = true;
253 pre_posted_requests = nn->reconfig_posted;
254 nn->reconfig_posted = 0;
256 spin_unlock_bh(&nn->reconfig_lock);
259 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
261 /* Run the posted reconfigs which were issued before we started */
262 if (pre_posted_requests) {
263 nfp_net_reconfig_start(nn, pre_posted_requests);
264 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
267 nfp_net_reconfig_start(nn, update);
268 ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
270 spin_lock_bh(&nn->reconfig_lock);
272 if (nn->reconfig_posted)
273 nfp_net_reconfig_start_async(nn, 0);
275 nn->reconfig_sync_present = false;
277 spin_unlock_bh(&nn->reconfig_lock);
282 /* Interrupt configuration and handling
286 * nfp_net_irq_unmask() - Unmask automasked interrupt
287 * @nn: NFP Network structure
288 * @entry_nr: MSI-X table entry
290 * Clear the ICR for the IRQ entry.
292 static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
294 nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
299 * nfp_net_irqs_alloc() - allocates MSI-X irqs
300 * @pdev: PCI device structure
301 * @irq_entries: Array to be initialized and used to hold the irq entries
302 * @min_irqs: Minimal acceptable number of interrupts
303 * @wanted_irqs: Target number of interrupts to allocate
305 * Return: Number of irqs obtained or 0 on error.
308 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
309 unsigned int min_irqs, unsigned int wanted_irqs)
314 for (i = 0; i < wanted_irqs; i++)
315 irq_entries[i].entry = i;
317 got_irqs = pci_enable_msix_range(pdev, irq_entries,
318 min_irqs, wanted_irqs);
320 dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
321 min_irqs, wanted_irqs, got_irqs);
325 if (got_irqs < wanted_irqs)
326 dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
327 wanted_irqs, got_irqs);
333 * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
334 * @nn: NFP Network structure
335 * @irq_entries: Table of allocated interrupts
336 * @n: Size of @irq_entries (number of entries to grab)
338 * After interrupts are allocated with nfp_net_irqs_alloc() this function
339 * should be called to assign them to a specific netdev (port).
342 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
345 struct nfp_net_dp *dp = &nn->dp;
347 nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
348 dp->num_r_vecs = nn->max_r_vecs;
350 memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
352 if (dp->num_rx_rings > dp->num_r_vecs ||
353 dp->num_tx_rings > dp->num_r_vecs)
354 dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
355 dp->num_rx_rings, dp->num_tx_rings,
358 dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
359 dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
360 dp->num_stack_tx_rings = dp->num_tx_rings;
364 * nfp_net_irqs_disable() - Disable interrupts
365 * @pdev: PCI device structure
367 * Undoes what @nfp_net_irqs_alloc() does.
369 void nfp_net_irqs_disable(struct pci_dev *pdev)
371 pci_disable_msix(pdev);
375 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
377 * @data: Opaque data structure
379 * Return: Indicate if the interrupt has been handled.
381 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
383 struct nfp_net_r_vector *r_vec = data;
385 napi_schedule_irqoff(&r_vec->napi);
387 /* The FW auto-masks any interrupt, either via the MASK bit in
388 * the MSI-X table or via the per entry ICR field. So there
389 * is no need to disable interrupts here.
394 bool nfp_net_link_changed_read_clear(struct nfp_net *nn)
399 spin_lock_irqsave(&nn->link_status_lock, flags);
400 ret = nn->link_changed;
401 nn->link_changed = false;
402 spin_unlock_irqrestore(&nn->link_status_lock, flags);
408 * nfp_net_read_link_status() - Reread link status from control BAR
409 * @nn: NFP Network structure
411 static void nfp_net_read_link_status(struct nfp_net *nn)
417 spin_lock_irqsave(&nn->link_status_lock, flags);
419 sts = nn_readl(nn, NFP_NET_CFG_STS);
420 link_up = !!(sts & NFP_NET_CFG_STS_LINK);
422 if (nn->link_up == link_up)
425 nn->link_up = link_up;
426 nn->link_changed = true;
429 netif_carrier_on(nn->dp.netdev);
430 netdev_info(nn->dp.netdev, "NIC Link is Up\n");
432 netif_carrier_off(nn->dp.netdev);
433 netdev_info(nn->dp.netdev, "NIC Link is Down\n");
436 spin_unlock_irqrestore(&nn->link_status_lock, flags);
440 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
442 * @data: Opaque data structure
444 * Return: Indicate if the interrupt has been handled.
446 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
448 struct nfp_net *nn = data;
449 struct msix_entry *entry;
451 entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
453 nfp_net_read_link_status(nn);
455 nfp_net_irq_unmask(nn, entry->entry);
461 * nfp_net_irq_exn() - Interrupt service routine for exceptions
463 * @data: Opaque data structure
465 * Return: Indicate if the interrupt has been handled.
467 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
469 struct nfp_net *nn = data;
471 nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
472 /* XXX TO BE IMPLEMENTED */
477 * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
478 * @tx_ring: TX ring structure
479 * @r_vec: IRQ vector servicing this ring
481 * @is_xdp: Is this an XDP TX ring?
484 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
485 struct nfp_net_r_vector *r_vec, unsigned int idx,
488 struct nfp_net *nn = r_vec->nfp_net;
491 tx_ring->r_vec = r_vec;
492 tx_ring->is_xdp = is_xdp;
494 tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
495 tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
499 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
500 * @rx_ring: RX ring structure
501 * @r_vec: IRQ vector servicing this ring
505 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
506 struct nfp_net_r_vector *r_vec, unsigned int idx)
508 struct nfp_net *nn = r_vec->nfp_net;
511 rx_ring->r_vec = r_vec;
513 rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
514 rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
518 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
519 * @netdev: netdev structure
521 static void nfp_net_vecs_init(struct net_device *netdev)
523 struct nfp_net *nn = netdev_priv(netdev);
524 struct nfp_net_r_vector *r_vec;
527 nn->lsc_handler = nfp_net_irq_lsc;
528 nn->exn_handler = nfp_net_irq_exn;
530 for (r = 0; r < nn->max_r_vecs; r++) {
531 struct msix_entry *entry;
533 entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
535 r_vec = &nn->r_vecs[r];
537 r_vec->handler = nfp_net_irq_rxtx;
538 r_vec->irq_entry = entry->entry;
539 r_vec->irq_vector = entry->vector;
541 cpumask_set_cpu(r, &r_vec->affinity_mask);
546 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
547 * @nn: NFP Network structure
548 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
549 * @format: printf-style format to construct the interrupt name
550 * @name: Pointer to allocated space for interrupt name
551 * @name_sz: Size of space for interrupt name
552 * @vector_idx: Index of MSI-X vector used for this interrupt
553 * @handler: IRQ handler to register for this interrupt
556 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
557 const char *format, char *name, size_t name_sz,
558 unsigned int vector_idx, irq_handler_t handler)
560 struct msix_entry *entry;
563 entry = &nn->irq_entries[vector_idx];
565 snprintf(name, name_sz, format, netdev_name(nn->dp.netdev));
566 err = request_irq(entry->vector, handler, 0, name, nn);
568 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
572 nn_writeb(nn, ctrl_offset, entry->entry);
578 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
579 * @nn: NFP Network structure
580 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
581 * @vector_idx: Index of MSI-X vector used for this interrupt
583 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
584 unsigned int vector_idx)
586 nn_writeb(nn, ctrl_offset, 0xff);
587 free_irq(nn->irq_entries[vector_idx].vector, nn);
592 * One queue controller peripheral queue is used for transmit. The
593 * driver en-queues packets for transmit by advancing the write
594 * pointer. The device indicates that packets have transmitted by
595 * advancing the read pointer. The driver maintains a local copy of
596 * the read and write pointer in @struct nfp_net_tx_ring. The driver
597 * keeps @wr_p in sync with the queue controller write pointer and can
598 * determine how many packets have been transmitted by comparing its
599 * copy of the read pointer @rd_p with the read pointer maintained by
600 * the queue controller peripheral.
604 * nfp_net_tx_full() - Check if the TX ring is full
605 * @tx_ring: TX ring to check
606 * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
608 * This function checks, based on the *host copy* of read/write
609 * pointer if a given TX ring is full. The real TX queue may have
610 * some newly made available slots.
612 * Return: True if the ring is full.
614 static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
616 return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
619 /* Wrappers for deciding when to stop and restart TX queues */
620 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
622 return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
625 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
627 return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
631 * nfp_net_tx_ring_stop() - stop tx ring
632 * @nd_q: netdev queue
633 * @tx_ring: driver tx queue structure
635 * Safely stop TX ring. Remember that while we are running .start_xmit()
636 * someone else may be cleaning the TX ring completions so we need to be
637 * extra careful here.
639 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
640 struct nfp_net_tx_ring *tx_ring)
642 netif_tx_stop_queue(nd_q);
644 /* We can race with the TX completion out of NAPI so recheck */
646 if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
647 netif_tx_start_queue(nd_q);
651 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
652 * @r_vec: per-ring structure
653 * @txbuf: Pointer to driver soft TX descriptor
654 * @txd: Pointer to HW TX descriptor
655 * @skb: Pointer to SKB
657 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
658 * Return error on packet header greater than maximum supported LSO header size.
660 static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
661 struct nfp_net_tx_buf *txbuf,
662 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
667 if (!skb_is_gso(skb))
670 if (!skb->encapsulation)
671 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
673 hdrlen = skb_inner_transport_header(skb) - skb->data +
674 inner_tcp_hdrlen(skb);
676 txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
677 txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
679 mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
680 txd->l4_offset = hdrlen;
681 txd->mss = cpu_to_le16(mss);
682 txd->flags |= PCIE_DESC_TX_LSO;
684 u64_stats_update_begin(&r_vec->tx_sync);
686 u64_stats_update_end(&r_vec->tx_sync);
690 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
691 * @dp: NFP Net data path struct
692 * @r_vec: per-ring structure
693 * @txbuf: Pointer to driver soft TX descriptor
694 * @txd: Pointer to TX descriptor
695 * @skb: Pointer to SKB
697 * This function sets the TX checksum flags in the TX descriptor based
698 * on the configuration and the protocol of the packet to be transmitted.
700 static void nfp_net_tx_csum(struct nfp_net_dp *dp,
701 struct nfp_net_r_vector *r_vec,
702 struct nfp_net_tx_buf *txbuf,
703 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
705 struct ipv6hdr *ipv6h;
709 if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
712 if (skb->ip_summed != CHECKSUM_PARTIAL)
715 txd->flags |= PCIE_DESC_TX_CSUM;
716 if (skb->encapsulation)
717 txd->flags |= PCIE_DESC_TX_ENCAP;
719 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
720 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
722 if (iph->version == 4) {
723 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
724 l4_hdr = iph->protocol;
725 } else if (ipv6h->version == 6) {
726 l4_hdr = ipv6h->nexthdr;
728 nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
734 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
737 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
740 nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
744 u64_stats_update_begin(&r_vec->tx_sync);
745 if (skb->encapsulation)
746 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
748 r_vec->hw_csum_tx += txbuf->pkt_cnt;
749 u64_stats_update_end(&r_vec->tx_sync);
752 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
755 nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
756 tx_ring->wr_ptr_add = 0;
760 * nfp_net_tx() - Main transmit entry point
761 * @skb: SKB to transmit
762 * @netdev: netdev structure
764 * Return: NETDEV_TX_OK on success.
766 static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
768 struct nfp_net *nn = netdev_priv(netdev);
769 const struct skb_frag_struct *frag;
770 struct nfp_net_tx_desc *txd, txdg;
771 struct nfp_net_tx_ring *tx_ring;
772 struct nfp_net_r_vector *r_vec;
773 struct nfp_net_tx_buf *txbuf;
774 struct netdev_queue *nd_q;
775 struct nfp_net_dp *dp;
783 qidx = skb_get_queue_mapping(skb);
784 tx_ring = &dp->tx_rings[qidx];
785 r_vec = tx_ring->r_vec;
786 nd_q = netdev_get_tx_queue(dp->netdev, qidx);
788 nr_frags = skb_shinfo(skb)->nr_frags;
790 if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
791 nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
792 qidx, tx_ring->wr_p, tx_ring->rd_p);
793 netif_tx_stop_queue(nd_q);
794 nfp_net_tx_xmit_more_flush(tx_ring);
795 u64_stats_update_begin(&r_vec->tx_sync);
797 u64_stats_update_end(&r_vec->tx_sync);
798 return NETDEV_TX_BUSY;
801 /* Start with the head skbuf */
802 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
804 if (dma_mapping_error(dp->dev, dma_addr))
807 wr_idx = tx_ring->wr_p & (tx_ring->cnt - 1);
809 /* Stash the soft descriptor of the head then initialize it */
810 txbuf = &tx_ring->txbufs[wr_idx];
812 txbuf->dma_addr = dma_addr;
815 txbuf->real_len = skb->len;
817 /* Build TX descriptor */
818 txd = &tx_ring->txds[wr_idx];
819 txd->offset_eop = (nr_frags == 0) ? PCIE_DESC_TX_EOP : 0;
820 txd->dma_len = cpu_to_le16(skb_headlen(skb));
821 nfp_desc_set_dma_addr(txd, dma_addr);
822 txd->data_len = cpu_to_le16(skb->len);
828 nfp_net_tx_tso(r_vec, txbuf, txd, skb);
830 nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
832 if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
833 txd->flags |= PCIE_DESC_TX_VLAN;
834 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
839 /* all descs must match except for in addr, length and eop */
842 for (f = 0; f < nr_frags; f++) {
843 frag = &skb_shinfo(skb)->frags[f];
844 fsize = skb_frag_size(frag);
846 dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
847 fsize, DMA_TO_DEVICE);
848 if (dma_mapping_error(dp->dev, dma_addr))
851 wr_idx = (wr_idx + 1) & (tx_ring->cnt - 1);
852 tx_ring->txbufs[wr_idx].skb = skb;
853 tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
854 tx_ring->txbufs[wr_idx].fidx = f;
856 txd = &tx_ring->txds[wr_idx];
858 txd->dma_len = cpu_to_le16(fsize);
859 nfp_desc_set_dma_addr(txd, dma_addr);
861 (f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
864 u64_stats_update_begin(&r_vec->tx_sync);
866 u64_stats_update_end(&r_vec->tx_sync);
869 netdev_tx_sent_queue(nd_q, txbuf->real_len);
871 tx_ring->wr_p += nr_frags + 1;
872 if (nfp_net_tx_ring_should_stop(tx_ring))
873 nfp_net_tx_ring_stop(nd_q, tx_ring);
875 tx_ring->wr_ptr_add += nr_frags + 1;
876 if (!skb->xmit_more || netif_xmit_stopped(nd_q))
877 nfp_net_tx_xmit_more_flush(tx_ring);
879 skb_tx_timestamp(skb);
886 frag = &skb_shinfo(skb)->frags[f];
887 dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
888 skb_frag_size(frag), DMA_TO_DEVICE);
889 tx_ring->txbufs[wr_idx].skb = NULL;
890 tx_ring->txbufs[wr_idx].dma_addr = 0;
891 tx_ring->txbufs[wr_idx].fidx = -2;
894 wr_idx += tx_ring->cnt;
896 dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
897 skb_headlen(skb), DMA_TO_DEVICE);
898 tx_ring->txbufs[wr_idx].skb = NULL;
899 tx_ring->txbufs[wr_idx].dma_addr = 0;
900 tx_ring->txbufs[wr_idx].fidx = -2;
902 nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
903 nfp_net_tx_xmit_more_flush(tx_ring);
904 u64_stats_update_begin(&r_vec->tx_sync);
906 u64_stats_update_end(&r_vec->tx_sync);
907 dev_kfree_skb_any(skb);
912 * nfp_net_tx_complete() - Handled completed TX packets
913 * @tx_ring: TX ring structure
915 * Return: Number of completed TX descriptors
917 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring)
919 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
920 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
921 const struct skb_frag_struct *frag;
922 struct netdev_queue *nd_q;
923 u32 done_pkts = 0, done_bytes = 0;
930 if (tx_ring->wr_p == tx_ring->rd_p)
933 /* Work out how many descriptors have been transmitted */
934 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
936 if (qcp_rd_p == tx_ring->qcp_rd_p)
939 if (qcp_rd_p > tx_ring->qcp_rd_p)
940 todo = qcp_rd_p - tx_ring->qcp_rd_p;
942 todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
945 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
948 skb = tx_ring->txbufs[idx].skb;
952 nr_frags = skb_shinfo(skb)->nr_frags;
953 fidx = tx_ring->txbufs[idx].fidx;
957 dma_unmap_single(dp->dev, tx_ring->txbufs[idx].dma_addr,
958 skb_headlen(skb), DMA_TO_DEVICE);
960 done_pkts += tx_ring->txbufs[idx].pkt_cnt;
961 done_bytes += tx_ring->txbufs[idx].real_len;
964 frag = &skb_shinfo(skb)->frags[fidx];
965 dma_unmap_page(dp->dev, tx_ring->txbufs[idx].dma_addr,
966 skb_frag_size(frag), DMA_TO_DEVICE);
969 /* check for last gather fragment */
970 if (fidx == nr_frags - 1)
971 dev_kfree_skb_any(skb);
973 tx_ring->txbufs[idx].dma_addr = 0;
974 tx_ring->txbufs[idx].skb = NULL;
975 tx_ring->txbufs[idx].fidx = -2;
978 tx_ring->qcp_rd_p = qcp_rd_p;
980 u64_stats_update_begin(&r_vec->tx_sync);
981 r_vec->tx_bytes += done_bytes;
982 r_vec->tx_pkts += done_pkts;
983 u64_stats_update_end(&r_vec->tx_sync);
985 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
986 netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
987 if (nfp_net_tx_ring_should_wake(tx_ring)) {
988 /* Make sure TX thread will see updated tx_ring->rd_p */
991 if (unlikely(netif_tx_queue_stopped(nd_q)))
992 netif_tx_wake_queue(nd_q);
995 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
996 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
997 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1000 static void nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
1002 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1003 u32 done_pkts = 0, done_bytes = 0;
1007 if (tx_ring->wr_p == tx_ring->rd_p)
1010 /* Work out how many descriptors have been transmitted */
1011 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1013 if (qcp_rd_p == tx_ring->qcp_rd_p)
1016 if (qcp_rd_p > tx_ring->qcp_rd_p)
1017 todo = qcp_rd_p - tx_ring->qcp_rd_p;
1019 todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
1023 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
1026 done_bytes += tx_ring->txbufs[idx].real_len;
1029 tx_ring->qcp_rd_p = qcp_rd_p;
1031 u64_stats_update_begin(&r_vec->tx_sync);
1032 r_vec->tx_bytes += done_bytes;
1033 r_vec->tx_pkts += done_pkts;
1034 u64_stats_update_end(&r_vec->tx_sync);
1036 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1037 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1038 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1042 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1043 * @dp: NFP Net data path struct
1044 * @tx_ring: TX ring structure
1046 * Assumes that the device is stopped
1049 nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1051 const struct skb_frag_struct *frag;
1052 struct netdev_queue *nd_q;
1054 while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
1055 struct nfp_net_tx_buf *tx_buf;
1056 struct sk_buff *skb;
1059 idx = tx_ring->rd_p & (tx_ring->cnt - 1);
1060 tx_buf = &tx_ring->txbufs[idx];
1062 skb = tx_ring->txbufs[idx].skb;
1063 nr_frags = skb_shinfo(skb)->nr_frags;
1065 if (tx_buf->fidx == -1) {
1067 dma_unmap_single(dp->dev, tx_buf->dma_addr,
1068 skb_headlen(skb), DMA_TO_DEVICE);
1070 /* unmap fragment */
1071 frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
1072 dma_unmap_page(dp->dev, tx_buf->dma_addr,
1073 skb_frag_size(frag), DMA_TO_DEVICE);
1076 /* check for last gather fragment */
1077 if (tx_buf->fidx == nr_frags - 1)
1078 dev_kfree_skb_any(skb);
1080 tx_buf->dma_addr = 0;
1084 tx_ring->qcp_rd_p++;
1088 memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
1091 tx_ring->qcp_rd_p = 0;
1092 tx_ring->wr_ptr_add = 0;
1094 if (tx_ring->is_xdp)
1097 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1098 netdev_tx_reset_queue(nd_q);
1101 static void nfp_net_tx_timeout(struct net_device *netdev)
1103 struct nfp_net *nn = netdev_priv(netdev);
1106 for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
1107 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1109 nn_warn(nn, "TX timeout on ring: %d\n", i);
1111 nn_warn(nn, "TX watchdog timeout\n");
1114 /* Receive processing
1117 nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1119 unsigned int fl_bufsz;
1121 fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1122 fl_bufsz += dp->rx_dma_off;
1123 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1124 fl_bufsz += NFP_NET_MAX_PREPEND;
1126 fl_bufsz += dp->rx_offset;
1127 fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1129 fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1130 fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1136 nfp_net_free_frag(void *frag, bool xdp)
1139 skb_free_frag(frag);
1141 __free_page(virt_to_page(frag));
1145 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1146 * @dp: NFP Net data path struct
1147 * @dma_addr: Pointer to storage for DMA address (output param)
1149 * This function will allcate a new page frag, map it for DMA.
1151 * Return: allocated page frag or NULL on failure.
1153 static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1158 frag = netdev_alloc_frag(dp->fl_bufsz);
1160 frag = page_address(alloc_page(GFP_KERNEL | __GFP_COLD));
1162 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1166 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1167 if (dma_mapping_error(dp->dev, *dma_addr)) {
1168 nfp_net_free_frag(frag, dp->xdp_prog);
1169 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1176 static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1181 frag = napi_alloc_frag(dp->fl_bufsz);
1183 frag = page_address(alloc_page(GFP_ATOMIC | __GFP_COLD));
1185 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1189 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1190 if (dma_mapping_error(dp->dev, *dma_addr)) {
1191 nfp_net_free_frag(frag, dp->xdp_prog);
1192 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1200 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1201 * @dp: NFP Net data path struct
1202 * @rx_ring: RX ring structure
1203 * @frag: page fragment buffer
1204 * @dma_addr: DMA address of skb mapping
1206 static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
1207 struct nfp_net_rx_ring *rx_ring,
1208 void *frag, dma_addr_t dma_addr)
1210 unsigned int wr_idx;
1212 wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
1214 nfp_net_dma_sync_dev_rx(dp, dma_addr);
1216 /* Stash SKB and DMA address away */
1217 rx_ring->rxbufs[wr_idx].frag = frag;
1218 rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1220 /* Fill freelist descriptor */
1221 rx_ring->rxds[wr_idx].fld.reserved = 0;
1222 rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1223 nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
1224 dma_addr + dp->rx_dma_off);
1227 rx_ring->wr_ptr_add++;
1228 if (rx_ring->wr_ptr_add >= NFP_NET_FL_BATCH) {
1229 /* Update write pointer of the freelist queue. Make
1230 * sure all writes are flushed before telling the hardware.
1233 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, rx_ring->wr_ptr_add);
1234 rx_ring->wr_ptr_add = 0;
1239 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1240 * @rx_ring: RX ring structure
1242 * Warning: Do *not* call if ring buffers were never put on the FW freelist
1243 * (i.e. device was not enabled)!
1245 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1247 unsigned int wr_idx, last_idx;
1249 /* Move the empty entry to the end of the list */
1250 wr_idx = rx_ring->wr_p & (rx_ring->cnt - 1);
1251 last_idx = rx_ring->cnt - 1;
1252 rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1253 rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1254 rx_ring->rxbufs[last_idx].dma_addr = 0;
1255 rx_ring->rxbufs[last_idx].frag = NULL;
1257 memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
1260 rx_ring->wr_ptr_add = 0;
1264 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1265 * @dp: NFP Net data path struct
1266 * @rx_ring: RX ring to remove buffers from
1268 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1269 * entries. After device is disabled nfp_net_rx_ring_reset() must be called
1270 * to restore required ring geometry.
1273 nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1274 struct nfp_net_rx_ring *rx_ring)
1278 for (i = 0; i < rx_ring->cnt - 1; i++) {
1279 /* NULL skb can only happen when initial filling of the ring
1280 * fails to allocate enough buffers and calls here to free
1281 * already allocated ones.
1283 if (!rx_ring->rxbufs[i].frag)
1286 nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1287 nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1288 rx_ring->rxbufs[i].dma_addr = 0;
1289 rx_ring->rxbufs[i].frag = NULL;
1294 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1295 * @dp: NFP Net data path struct
1296 * @rx_ring: RX ring to remove buffers from
1299 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1300 struct nfp_net_rx_ring *rx_ring)
1302 struct nfp_net_rx_buf *rxbufs;
1305 rxbufs = rx_ring->rxbufs;
1307 for (i = 0; i < rx_ring->cnt - 1; i++) {
1308 rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
1309 if (!rxbufs[i].frag) {
1310 nfp_net_rx_ring_bufs_free(dp, rx_ring);
1319 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1320 * @dp: NFP Net data path struct
1321 * @rx_ring: RX ring to fill
1324 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
1325 struct nfp_net_rx_ring *rx_ring)
1329 for (i = 0; i < rx_ring->cnt - 1; i++)
1330 nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1331 rx_ring->rxbufs[i].dma_addr);
1335 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1336 * @flags: RX descriptor flags field in CPU byte order
1338 static int nfp_net_rx_csum_has_errors(u16 flags)
1340 u16 csum_all_checked, csum_all_ok;
1342 csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1343 csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1345 return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1349 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1350 * @dp: NFP Net data path struct
1351 * @r_vec: per-ring structure
1352 * @rxd: Pointer to RX descriptor
1353 * @skb: Pointer to SKB
1355 static void nfp_net_rx_csum(struct nfp_net_dp *dp,
1356 struct nfp_net_r_vector *r_vec,
1357 struct nfp_net_rx_desc *rxd, struct sk_buff *skb)
1359 skb_checksum_none_assert(skb);
1361 if (!(dp->netdev->features & NETIF_F_RXCSUM))
1364 if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1365 u64_stats_update_begin(&r_vec->rx_sync);
1366 r_vec->hw_csum_rx_error++;
1367 u64_stats_update_end(&r_vec->rx_sync);
1371 /* Assume that the firmware will never report inner CSUM_OK unless outer
1372 * L4 headers were successfully parsed. FW will always report zero UDP
1373 * checksum as CSUM_OK.
1375 if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1376 rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1377 __skb_incr_checksum_unnecessary(skb);
1378 u64_stats_update_begin(&r_vec->rx_sync);
1379 r_vec->hw_csum_rx_ok++;
1380 u64_stats_update_end(&r_vec->rx_sync);
1383 if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1384 rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1385 __skb_incr_checksum_unnecessary(skb);
1386 u64_stats_update_begin(&r_vec->rx_sync);
1387 r_vec->hw_csum_rx_inner_ok++;
1388 u64_stats_update_end(&r_vec->rx_sync);
1393 nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
1394 unsigned int type, __be32 *hash)
1396 if (!(netdev->features & NETIF_F_RXHASH))
1400 case NFP_NET_RSS_IPV4:
1401 case NFP_NET_RSS_IPV6:
1402 case NFP_NET_RSS_IPV6_EX:
1403 meta->hash_type = PKT_HASH_TYPE_L3;
1406 meta->hash_type = PKT_HASH_TYPE_L4;
1410 meta->hash = get_unaligned_be32(hash);
1414 nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
1415 void *data, struct nfp_net_rx_desc *rxd)
1417 struct nfp_net_rx_hash *rx_hash = data;
1419 if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1422 nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
1427 nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
1428 void *data, int meta_len)
1432 meta_info = get_unaligned_be32(data);
1436 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1437 case NFP_NET_META_HASH:
1438 meta_info >>= NFP_NET_META_FIELD_SIZE;
1439 nfp_net_set_hash(netdev, meta,
1440 meta_info & NFP_NET_META_FIELD_MASK,
1444 case NFP_NET_META_MARK:
1445 meta->mark = get_unaligned_be32(data);
1452 meta_info >>= NFP_NET_META_FIELD_SIZE;
1459 nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
1460 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
1461 struct sk_buff *skb)
1463 u64_stats_update_begin(&r_vec->rx_sync);
1465 u64_stats_update_end(&r_vec->rx_sync);
1467 /* skb is build based on the frag, free_skb() would free the frag
1468 * so to be able to reuse it we need an extra ref.
1470 if (skb && rxbuf && skb->head == rxbuf->frag)
1471 page_ref_inc(virt_to_head_page(rxbuf->frag));
1473 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1475 dev_kfree_skb_any(skb);
1479 nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1480 struct nfp_net_tx_ring *tx_ring,
1481 struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1482 unsigned int pkt_len)
1484 struct nfp_net_tx_buf *txbuf;
1485 struct nfp_net_tx_desc *txd;
1488 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1489 nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf, NULL);
1493 wr_idx = tx_ring->wr_p & (tx_ring->cnt - 1);
1495 /* Stash the soft descriptor of the head then initialize it */
1496 txbuf = &tx_ring->txbufs[wr_idx];
1498 nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
1500 txbuf->frag = rxbuf->frag;
1501 txbuf->dma_addr = rxbuf->dma_addr;
1504 txbuf->real_len = pkt_len;
1506 dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1507 pkt_len, DMA_BIDIRECTIONAL);
1509 /* Build TX descriptor */
1510 txd = &tx_ring->txds[wr_idx];
1511 txd->offset_eop = PCIE_DESC_TX_EOP;
1512 txd->dma_len = cpu_to_le16(pkt_len);
1513 nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1514 txd->data_len = cpu_to_le16(pkt_len);
1521 tx_ring->wr_ptr_add++;
1525 static int nfp_net_run_xdp(struct bpf_prog *prog, void *data, void *hard_start,
1526 unsigned int *off, unsigned int *len)
1528 struct xdp_buff xdp;
1532 xdp.data_hard_start = hard_start;
1533 xdp.data = data + *off;
1534 xdp.data_end = data + *off + *len;
1536 orig_data = xdp.data;
1537 ret = bpf_prog_run_xdp(prog, &xdp);
1539 *len -= xdp.data - orig_data;
1540 *off += xdp.data - orig_data;
1546 * nfp_net_rx() - receive up to @budget packets on @rx_ring
1547 * @rx_ring: RX ring to receive from
1548 * @budget: NAPI budget
1550 * Note, this function is separated out from the napi poll function to
1551 * more cleanly separate packet receive code from other bookkeeping
1552 * functions performed in the napi poll function.
1554 * Return: Number of packets received.
1556 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1558 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1559 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1560 struct nfp_net_tx_ring *tx_ring;
1561 struct bpf_prog *xdp_prog;
1562 unsigned int true_bufsz;
1563 struct sk_buff *skb;
1564 int pkts_polled = 0;
1568 xdp_prog = READ_ONCE(dp->xdp_prog);
1569 true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1570 tx_ring = r_vec->xdp_ring;
1572 while (pkts_polled < budget) {
1573 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1574 struct nfp_net_rx_buf *rxbuf;
1575 struct nfp_net_rx_desc *rxd;
1576 struct nfp_meta_parsed meta;
1577 dma_addr_t new_dma_addr;
1580 idx = rx_ring->rd_p & (rx_ring->cnt - 1);
1582 rxd = &rx_ring->rxds[idx];
1583 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1586 /* Memory barrier to ensure that we won't do other reads
1587 * before the DD bit.
1591 memset(&meta, 0, sizeof(meta));
1596 rxbuf = &rx_ring->rxbufs[idx];
1598 * <-- [rx_offset] -->
1599 * ---------------------------------------------------------
1600 * | [XX] | metadata | packet | XXXX |
1601 * ---------------------------------------------------------
1602 * <---------------- data_len --------------->
1604 * The rx_offset is fixed for all packets, the meta_len can vary
1605 * on a packet by packet basis. If rx_offset is set to zero
1606 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1607 * buffer and is immediately followed by the packet (no [XX]).
1609 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1610 data_len = le16_to_cpu(rxd->rxd.data_len);
1611 pkt_len = data_len - meta_len;
1613 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1614 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1615 pkt_off += meta_len;
1617 pkt_off += dp->rx_offset;
1618 meta_off = pkt_off - meta_len;
1621 u64_stats_update_begin(&r_vec->rx_sync);
1623 r_vec->rx_bytes += pkt_len;
1624 u64_stats_update_end(&r_vec->rx_sync);
1626 if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1627 (dp->rx_offset && meta_len > dp->rx_offset))) {
1628 nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1630 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1634 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1637 if (!dp->chained_metadata_format) {
1638 nfp_net_set_hash_desc(dp->netdev, &meta,
1639 rxbuf->frag + meta_off, rxd);
1640 } else if (meta_len) {
1643 end = nfp_net_parse_meta(dp->netdev, &meta,
1644 rxbuf->frag + meta_off,
1646 if (unlikely(end != rxbuf->frag + pkt_off)) {
1647 nn_dp_warn(dp, "invalid RX packet metadata\n");
1648 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1654 if (xdp_prog && !(rxd->rxd.flags & PCIE_DESC_RX_BPF &&
1655 dp->bpf_offload_xdp)) {
1656 unsigned int dma_off;
1660 hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
1662 act = nfp_net_run_xdp(xdp_prog, rxbuf->frag, hard_start,
1663 &pkt_off, &pkt_len);
1668 dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1669 if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1673 trace_xdp_exception(dp->netdev,
1677 bpf_warn_invalid_xdp_action(act);
1679 trace_xdp_exception(dp->netdev, xdp_prog, act);
1681 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1687 skb = build_skb(rxbuf->frag, true_bufsz);
1688 if (unlikely(!skb)) {
1689 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1692 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1693 if (unlikely(!new_frag)) {
1694 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1698 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1700 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1702 skb_reserve(skb, pkt_off);
1703 skb_put(skb, pkt_len);
1705 skb->mark = meta.mark;
1706 skb_set_hash(skb, meta.hash, meta.hash_type);
1708 skb_record_rx_queue(skb, rx_ring->idx);
1709 skb->protocol = eth_type_trans(skb, dp->netdev);
1711 nfp_net_rx_csum(dp, r_vec, rxd, skb);
1713 if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1714 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1715 le16_to_cpu(rxd->rxd.vlan));
1717 napi_gro_receive(&rx_ring->r_vec->napi, skb);
1720 if (xdp_prog && tx_ring->wr_ptr_add)
1721 nfp_net_tx_xmit_more_flush(tx_ring);
1728 * nfp_net_poll() - napi poll function
1729 * @napi: NAPI structure
1730 * @budget: NAPI budget
1732 * Return: number of packets polled.
1734 static int nfp_net_poll(struct napi_struct *napi, int budget)
1736 struct nfp_net_r_vector *r_vec =
1737 container_of(napi, struct nfp_net_r_vector, napi);
1738 unsigned int pkts_polled = 0;
1741 nfp_net_tx_complete(r_vec->tx_ring);
1742 if (r_vec->rx_ring) {
1743 pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1744 if (r_vec->xdp_ring)
1745 nfp_net_xdp_complete(r_vec->xdp_ring);
1748 if (pkts_polled < budget)
1749 if (napi_complete_done(napi, pkts_polled))
1750 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1755 /* Setup and Configuration
1759 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
1760 * @tx_ring: TX ring to free
1762 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
1764 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1765 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1767 kfree(tx_ring->txbufs);
1770 dma_free_coherent(dp->dev, tx_ring->size,
1771 tx_ring->txds, tx_ring->dma);
1774 tx_ring->txbufs = NULL;
1775 tx_ring->txds = NULL;
1781 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
1782 * @dp: NFP Net data path struct
1783 * @tx_ring: TX Ring structure to allocate
1785 * Return: 0 on success, negative errno otherwise.
1788 nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1790 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1793 tx_ring->cnt = dp->txd_cnt;
1795 tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
1796 tx_ring->txds = dma_zalloc_coherent(dp->dev, tx_ring->size,
1797 &tx_ring->dma, GFP_KERNEL);
1801 sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
1802 tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
1803 if (!tx_ring->txbufs)
1806 if (!tx_ring->is_xdp)
1807 netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
1813 nfp_net_tx_ring_free(tx_ring);
1818 nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
1819 struct nfp_net_tx_ring *tx_ring)
1823 if (!tx_ring->is_xdp)
1826 for (i = 0; i < tx_ring->cnt; i++) {
1827 if (!tx_ring->txbufs[i].frag)
1830 nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
1831 __free_page(virt_to_page(tx_ring->txbufs[i].frag));
1836 nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
1837 struct nfp_net_tx_ring *tx_ring)
1839 struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
1842 if (!tx_ring->is_xdp)
1845 for (i = 0; i < tx_ring->cnt; i++) {
1846 txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
1847 if (!txbufs[i].frag) {
1848 nfp_net_tx_ring_bufs_free(dp, tx_ring);
1856 static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
1860 dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
1865 for (r = 0; r < dp->num_tx_rings; r++) {
1868 if (r >= dp->num_stack_tx_rings)
1869 bias = dp->num_stack_tx_rings;
1871 nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
1874 if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
1877 if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
1885 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
1887 nfp_net_tx_ring_free(&dp->tx_rings[r]);
1889 kfree(dp->tx_rings);
1893 static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
1897 for (r = 0; r < dp->num_tx_rings; r++) {
1898 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
1899 nfp_net_tx_ring_free(&dp->tx_rings[r]);
1902 kfree(dp->tx_rings);
1906 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
1907 * @rx_ring: RX ring to free
1909 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
1911 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1912 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1914 kfree(rx_ring->rxbufs);
1917 dma_free_coherent(dp->dev, rx_ring->size,
1918 rx_ring->rxds, rx_ring->dma);
1921 rx_ring->rxbufs = NULL;
1922 rx_ring->rxds = NULL;
1928 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
1929 * @dp: NFP Net data path struct
1930 * @rx_ring: RX ring to allocate
1932 * Return: 0 on success, negative errno otherwise.
1935 nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
1939 rx_ring->cnt = dp->rxd_cnt;
1940 rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
1941 rx_ring->rxds = dma_zalloc_coherent(dp->dev, rx_ring->size,
1942 &rx_ring->dma, GFP_KERNEL);
1946 sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
1947 rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
1948 if (!rx_ring->rxbufs)
1954 nfp_net_rx_ring_free(rx_ring);
1958 static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
1962 dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
1967 for (r = 0; r < dp->num_rx_rings; r++) {
1968 nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
1970 if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
1973 if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
1981 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
1983 nfp_net_rx_ring_free(&dp->rx_rings[r]);
1985 kfree(dp->rx_rings);
1989 static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
1993 for (r = 0; r < dp->num_rx_rings; r++) {
1994 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
1995 nfp_net_rx_ring_free(&dp->rx_rings[r]);
1998 kfree(dp->rx_rings);
2002 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
2003 struct nfp_net_r_vector *r_vec, int idx)
2005 r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
2007 idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
2009 r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
2010 &dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
2014 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
2020 netif_napi_add(nn->dp.netdev, &r_vec->napi,
2021 nfp_net_poll, NAPI_POLL_WEIGHT);
2023 snprintf(r_vec->name, sizeof(r_vec->name),
2024 "%s-rxtx-%d", nn->dp.netdev->name, idx);
2025 err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
2028 netif_napi_del(&r_vec->napi);
2029 nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
2032 disable_irq(r_vec->irq_vector);
2034 irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
2036 nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
2043 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
2045 irq_set_affinity_hint(r_vec->irq_vector, NULL);
2046 netif_napi_del(&r_vec->napi);
2047 free_irq(r_vec->irq_vector, r_vec);
2051 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
2052 * @nn: NFP Net device to reconfigure
2054 void nfp_net_rss_write_itbl(struct nfp_net *nn)
2058 for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
2059 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
2060 get_unaligned_le32(nn->rss_itbl + i));
2064 * nfp_net_rss_write_key() - Write RSS hash key to device
2065 * @nn: NFP Net device to reconfigure
2067 void nfp_net_rss_write_key(struct nfp_net *nn)
2071 for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2072 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
2073 get_unaligned_le32(nn->rss_key + i));
2077 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2078 * @nn: NFP Net device to reconfigure
2080 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
2086 /* Compute factor used to convert coalesce '_usecs' parameters to
2087 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
2090 factor = nn->me_freq_mhz / 16;
2092 /* copy RX interrupt coalesce parameters */
2093 value = (nn->rx_coalesce_max_frames << 16) |
2094 (factor * nn->rx_coalesce_usecs);
2095 for (i = 0; i < nn->dp.num_rx_rings; i++)
2096 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
2098 /* copy TX interrupt coalesce parameters */
2099 value = (nn->tx_coalesce_max_frames << 16) |
2100 (factor * nn->tx_coalesce_usecs);
2101 for (i = 0; i < nn->dp.num_tx_rings; i++)
2102 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
2106 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2107 * @nn: NFP Net device to reconfigure
2109 * Writes the MAC address from the netdev to the device control BAR. Does not
2110 * perform the required reconfig. We do a bit of byte swapping dance because
2113 static void nfp_net_write_mac_addr(struct nfp_net *nn)
2115 nn_writel(nn, NFP_NET_CFG_MACADDR + 0,
2116 get_unaligned_be32(nn->dp.netdev->dev_addr));
2117 nn_writew(nn, NFP_NET_CFG_MACADDR + 6,
2118 get_unaligned_be16(nn->dp.netdev->dev_addr + 4));
2121 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
2123 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
2124 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
2125 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
2127 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
2128 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
2129 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
2133 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2134 * @nn: NFP Net device to reconfigure
2136 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
2138 u32 new_ctrl, update;
2142 new_ctrl = nn->dp.ctrl;
2143 new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
2144 update = NFP_NET_CFG_UPDATE_GEN;
2145 update |= NFP_NET_CFG_UPDATE_MSIX;
2146 update |= NFP_NET_CFG_UPDATE_RING;
2148 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2149 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
2151 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2152 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2154 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2155 err = nfp_net_reconfig(nn, update);
2157 nn_err(nn, "Could not disable device: %d\n", err);
2159 for (r = 0; r < nn->dp.num_rx_rings; r++)
2160 nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
2161 for (r = 0; r < nn->dp.num_tx_rings; r++)
2162 nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
2163 for (r = 0; r < nn->dp.num_r_vecs; r++)
2164 nfp_net_vec_clear_ring_data(nn, r);
2166 nn->dp.ctrl = new_ctrl;
2170 nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
2171 struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2173 /* Write the DMA address, size and MSI-X info to the device */
2174 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
2175 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2176 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2180 nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
2181 struct nfp_net_tx_ring *tx_ring, unsigned int idx)
2183 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
2184 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2185 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2189 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2190 * @nn: NFP Net device to reconfigure
2192 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2194 u32 bufsz, new_ctrl, update = 0;
2198 new_ctrl = nn->dp.ctrl;
2200 if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
2201 nfp_net_rss_write_key(nn);
2202 nfp_net_rss_write_itbl(nn);
2203 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
2204 update |= NFP_NET_CFG_UPDATE_RSS;
2207 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2208 nfp_net_coalesce_write_cfg(nn);
2210 new_ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2211 update |= NFP_NET_CFG_UPDATE_IRQMOD;
2214 for (r = 0; r < nn->dp.num_tx_rings; r++)
2215 nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
2216 for (r = 0; r < nn->dp.num_rx_rings; r++)
2217 nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2219 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
2220 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2222 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
2223 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2225 nfp_net_write_mac_addr(nn);
2227 nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.netdev->mtu);
2229 bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
2230 nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
2233 new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2234 update |= NFP_NET_CFG_UPDATE_GEN;
2235 update |= NFP_NET_CFG_UPDATE_MSIX;
2236 update |= NFP_NET_CFG_UPDATE_RING;
2237 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2238 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2240 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2241 err = nfp_net_reconfig(nn, update);
2243 nfp_net_clear_config_and_disable(nn);
2247 nn->dp.ctrl = new_ctrl;
2249 for (r = 0; r < nn->dp.num_rx_rings; r++)
2250 nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2252 /* Since reconfiguration requests while NFP is down are ignored we
2253 * have to wipe the entire VXLAN configuration and reinitialize it.
2255 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2256 memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2257 memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2258 udp_tunnel_get_rx_info(nn->dp.netdev);
2265 * nfp_net_open_stack() - Start the device from stack's perspective
2266 * @nn: NFP Net device to reconfigure
2268 static void nfp_net_open_stack(struct nfp_net *nn)
2272 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2273 napi_enable(&nn->r_vecs[r].napi);
2274 enable_irq(nn->r_vecs[r].irq_vector);
2277 netif_tx_wake_all_queues(nn->dp.netdev);
2279 enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2280 nfp_net_read_link_status(nn);
2283 static int nfp_net_netdev_open(struct net_device *netdev)
2285 struct nfp_net *nn = netdev_priv(netdev);
2288 /* Step 1: Allocate resources for rings and the like
2289 * - Request interrupts
2290 * - Allocate RX and TX ring resources
2291 * - Setup initial RSS table
2293 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2294 nn->exn_name, sizeof(nn->exn_name),
2295 NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2298 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2299 nn->lsc_name, sizeof(nn->lsc_name),
2300 NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2303 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2305 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2306 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2308 goto err_cleanup_vec_p;
2311 err = nfp_net_rx_rings_prepare(nn, &nn->dp);
2313 goto err_cleanup_vec;
2315 err = nfp_net_tx_rings_prepare(nn, &nn->dp);
2317 goto err_free_rx_rings;
2319 for (r = 0; r < nn->max_r_vecs; r++)
2320 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2322 err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
2324 goto err_free_rings;
2326 err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
2328 goto err_free_rings;
2330 /* Step 2: Configure the NFP
2331 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2332 * - Write MAC address (in case it changed)
2334 * - Set the Freelist buffer size
2337 err = nfp_net_set_config_and_enable(nn);
2339 goto err_free_rings;
2341 /* Step 3: Enable for kernel
2342 * - put some freelist descriptors on each RX ring
2343 * - enable NAPI on each ring
2344 * - enable all TX queues
2347 nfp_net_open_stack(nn);
2352 nfp_net_tx_rings_free(&nn->dp);
2354 nfp_net_rx_rings_free(&nn->dp);
2356 r = nn->dp.num_r_vecs;
2359 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2360 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2362 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2367 * nfp_net_close_stack() - Quiescent the stack (part of close)
2368 * @nn: NFP Net device to reconfigure
2370 static void nfp_net_close_stack(struct nfp_net *nn)
2374 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2375 netif_carrier_off(nn->dp.netdev);
2376 nn->link_up = false;
2378 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2379 disable_irq(nn->r_vecs[r].irq_vector);
2380 napi_disable(&nn->r_vecs[r].napi);
2383 netif_tx_disable(nn->dp.netdev);
2387 * nfp_net_close_free_all() - Free all runtime resources
2388 * @nn: NFP Net device to reconfigure
2390 static void nfp_net_close_free_all(struct nfp_net *nn)
2394 for (r = 0; r < nn->dp.num_rx_rings; r++) {
2395 nfp_net_rx_ring_bufs_free(&nn->dp, &nn->dp.rx_rings[r]);
2396 nfp_net_rx_ring_free(&nn->dp.rx_rings[r]);
2398 for (r = 0; r < nn->dp.num_tx_rings; r++) {
2399 nfp_net_tx_ring_bufs_free(&nn->dp, &nn->dp.tx_rings[r]);
2400 nfp_net_tx_ring_free(&nn->dp.tx_rings[r]);
2402 for (r = 0; r < nn->dp.num_r_vecs; r++)
2403 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2405 kfree(nn->dp.rx_rings);
2406 kfree(nn->dp.tx_rings);
2408 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2409 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2413 * nfp_net_netdev_close() - Called when the device is downed
2414 * @netdev: netdev structure
2416 static int nfp_net_netdev_close(struct net_device *netdev)
2418 struct nfp_net *nn = netdev_priv(netdev);
2420 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2422 nfp_net_close_stack(nn);
2426 nfp_net_clear_config_and_disable(nn);
2428 /* Step 3: Free resources
2430 nfp_net_close_free_all(nn);
2432 nn_dbg(nn, "%s down", netdev->name);
2436 static void nfp_net_set_rx_mode(struct net_device *netdev)
2438 struct nfp_net *nn = netdev_priv(netdev);
2441 new_ctrl = nn->dp.ctrl;
2443 if (netdev->flags & IFF_PROMISC) {
2444 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2445 new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2447 nn_warn(nn, "FW does not support promiscuous mode\n");
2449 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2452 if (new_ctrl == nn->dp.ctrl)
2455 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2456 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2458 nn->dp.ctrl = new_ctrl;
2461 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
2465 for (i = 0; i < sizeof(nn->rss_itbl); i++)
2467 ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
2470 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
2472 struct nfp_net_dp new_dp = *dp;
2477 nn->dp.netdev->mtu = new_dp.mtu;
2479 if (!netif_is_rxfh_configured(nn->dp.netdev))
2480 nfp_net_rss_init_itbl(nn);
2483 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
2488 nfp_net_dp_swap(nn, dp);
2490 for (r = 0; r < nn->max_r_vecs; r++)
2491 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2493 err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
2497 if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
2498 err = netif_set_real_num_tx_queues(nn->dp.netdev,
2499 nn->dp.num_stack_tx_rings);
2504 return nfp_net_set_config_and_enable(nn);
2507 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
2509 struct nfp_net_dp *new;
2511 new = kmalloc(sizeof(*new), GFP_KERNEL);
2517 /* Clear things which need to be recomputed */
2519 new->tx_rings = NULL;
2520 new->rx_rings = NULL;
2521 new->num_r_vecs = 0;
2522 new->num_stack_tx_rings = 0;
2528 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
2529 struct netlink_ext_ack *extack)
2531 /* XDP-enabled tests */
2534 if (dp->fl_bufsz > PAGE_SIZE) {
2535 NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
2538 if (dp->num_tx_rings > nn->max_tx_rings) {
2539 NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
2546 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
2547 struct netlink_ext_ack *extack)
2551 dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
2553 dp->num_stack_tx_rings = dp->num_tx_rings;
2555 dp->num_stack_tx_rings -= dp->num_rx_rings;
2557 dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
2559 err = nfp_net_check_config(nn, dp, extack);
2563 if (!netif_running(dp->netdev)) {
2564 nfp_net_dp_swap(nn, dp);
2569 /* Prepare new rings */
2570 for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
2571 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2574 goto err_cleanup_vecs;
2578 err = nfp_net_rx_rings_prepare(nn, dp);
2580 goto err_cleanup_vecs;
2582 err = nfp_net_tx_rings_prepare(nn, dp);
2586 /* Stop device, swap in new rings, try to start the firmware */
2587 nfp_net_close_stack(nn);
2588 nfp_net_clear_config_and_disable(nn);
2590 err = nfp_net_dp_swap_enable(nn, dp);
2594 nfp_net_clear_config_and_disable(nn);
2596 /* Try with old configuration and old rings */
2597 err2 = nfp_net_dp_swap_enable(nn, dp);
2599 nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
2602 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
2603 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2605 nfp_net_rx_rings_free(dp);
2606 nfp_net_tx_rings_free(dp);
2608 nfp_net_open_stack(nn);
2615 nfp_net_rx_rings_free(dp);
2617 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
2618 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2623 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
2625 struct nfp_net *nn = netdev_priv(netdev);
2626 struct nfp_net_dp *dp;
2628 dp = nfp_net_clone_dp(nn);
2634 return nfp_net_ring_reconfig(nn, dp, NULL);
2637 static void nfp_net_stat64(struct net_device *netdev,
2638 struct rtnl_link_stats64 *stats)
2640 struct nfp_net *nn = netdev_priv(netdev);
2643 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2644 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
2649 start = u64_stats_fetch_begin(&r_vec->rx_sync);
2650 data[0] = r_vec->rx_pkts;
2651 data[1] = r_vec->rx_bytes;
2652 data[2] = r_vec->rx_drops;
2653 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
2654 stats->rx_packets += data[0];
2655 stats->rx_bytes += data[1];
2656 stats->rx_dropped += data[2];
2659 start = u64_stats_fetch_begin(&r_vec->tx_sync);
2660 data[0] = r_vec->tx_pkts;
2661 data[1] = r_vec->tx_bytes;
2662 data[2] = r_vec->tx_errors;
2663 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
2664 stats->tx_packets += data[0];
2665 stats->tx_bytes += data[1];
2666 stats->tx_errors += data[2];
2670 static bool nfp_net_ebpf_capable(struct nfp_net *nn)
2672 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
2673 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
2679 nfp_net_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
2680 struct tc_to_netdev *tc)
2682 struct nfp_net *nn = netdev_priv(netdev);
2684 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2686 if (proto != htons(ETH_P_ALL))
2689 if (tc->type == TC_SETUP_CLSBPF && nfp_net_ebpf_capable(nn)) {
2690 if (!nn->dp.bpf_offload_xdp)
2691 return nfp_net_bpf_offload(nn, tc->cls_bpf);
2699 static int nfp_net_set_features(struct net_device *netdev,
2700 netdev_features_t features)
2702 netdev_features_t changed = netdev->features ^ features;
2703 struct nfp_net *nn = netdev_priv(netdev);
2707 /* Assume this is not called with features we have not advertised */
2709 new_ctrl = nn->dp.ctrl;
2711 if (changed & NETIF_F_RXCSUM) {
2712 if (features & NETIF_F_RXCSUM)
2713 new_ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
2715 new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM;
2718 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2719 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
2720 new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2722 new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
2725 if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
2726 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
2727 new_ctrl |= NFP_NET_CFG_CTRL_LSO;
2729 new_ctrl &= ~NFP_NET_CFG_CTRL_LSO;
2732 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2733 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2734 new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2736 new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
2739 if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
2740 if (features & NETIF_F_HW_VLAN_CTAG_TX)
2741 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2743 new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
2746 if (changed & NETIF_F_SG) {
2747 if (features & NETIF_F_SG)
2748 new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
2750 new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
2753 if (changed & NETIF_F_HW_TC && nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
2754 nn_err(nn, "Cannot disable HW TC offload while in use\n");
2758 nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
2759 netdev->features, features, changed);
2761 if (new_ctrl == nn->dp.ctrl)
2764 nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
2765 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2766 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
2770 nn->dp.ctrl = new_ctrl;
2775 static netdev_features_t
2776 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
2777 netdev_features_t features)
2781 /* We can't do TSO over double tagged packets (802.1AD) */
2782 features &= vlan_features_check(skb, features);
2784 if (!skb->encapsulation)
2787 /* Ensure that inner L4 header offset fits into TX descriptor field */
2788 if (skb_is_gso(skb)) {
2791 hdrlen = skb_inner_transport_header(skb) - skb->data +
2792 inner_tcp_hdrlen(skb);
2794 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
2795 features &= ~NETIF_F_GSO_MASK;
2798 /* VXLAN/GRE check */
2799 switch (vlan_get_protocol(skb)) {
2800 case htons(ETH_P_IP):
2801 l4_hdr = ip_hdr(skb)->protocol;
2803 case htons(ETH_P_IPV6):
2804 l4_hdr = ipv6_hdr(skb)->nexthdr;
2807 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2810 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
2811 skb->inner_protocol != htons(ETH_P_TEB) ||
2812 (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
2813 (l4_hdr == IPPROTO_UDP &&
2814 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
2815 sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
2816 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2822 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
2824 struct nfp_net *nn = netdev_priv(netdev);
2830 if (!nn->eth_port->is_split)
2831 err = snprintf(name, len, "p%d", nn->eth_port->label_port);
2833 err = snprintf(name, len, "p%ds%d", nn->eth_port->label_port,
2834 nn->eth_port->label_subport);
2842 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
2843 * @nn: NFP Net device to reconfigure
2844 * @idx: Index into the port table where new port should be written
2845 * @port: UDP port to configure (pass zero to remove VXLAN port)
2847 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
2851 nn->vxlan_ports[idx] = port;
2853 if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
2856 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
2857 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
2858 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
2859 be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
2860 be16_to_cpu(nn->vxlan_ports[i]));
2862 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
2866 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
2867 * @nn: NFP Network structure
2868 * @port: UDP port to look for
2870 * Return: if the port is already in the table -- it's position;
2871 * if the port is not in the table -- free position to use;
2872 * if the table is full -- -ENOSPC.
2874 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
2876 int i, free_idx = -ENOSPC;
2878 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
2879 if (nn->vxlan_ports[i] == port)
2881 if (!nn->vxlan_usecnt[i])
2888 static void nfp_net_add_vxlan_port(struct net_device *netdev,
2889 struct udp_tunnel_info *ti)
2891 struct nfp_net *nn = netdev_priv(netdev);
2894 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2897 idx = nfp_net_find_vxlan_idx(nn, ti->port);
2901 if (!nn->vxlan_usecnt[idx]++)
2902 nfp_net_set_vxlan_port(nn, idx, ti->port);
2905 static void nfp_net_del_vxlan_port(struct net_device *netdev,
2906 struct udp_tunnel_info *ti)
2908 struct nfp_net *nn = netdev_priv(netdev);
2911 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2914 idx = nfp_net_find_vxlan_idx(nn, ti->port);
2915 if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
2918 if (!--nn->vxlan_usecnt[idx])
2919 nfp_net_set_vxlan_port(nn, idx, 0);
2922 static int nfp_net_xdp_offload(struct nfp_net *nn, struct bpf_prog *prog)
2924 struct tc_cls_bpf_offload cmd = {
2929 if (!nfp_net_ebpf_capable(nn))
2932 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
2933 if (!nn->dp.bpf_offload_xdp)
2934 return prog ? -EBUSY : 0;
2935 cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
2939 cmd.command = TC_CLSBPF_ADD;
2942 ret = nfp_net_bpf_offload(nn, &cmd);
2943 /* Stop offload if replace not possible */
2944 if (ret && cmd.command == TC_CLSBPF_REPLACE)
2945 nfp_net_xdp_offload(nn, NULL);
2946 nn->dp.bpf_offload_xdp = prog && !ret;
2950 static int nfp_net_xdp_setup(struct nfp_net *nn, struct netdev_xdp *xdp)
2952 struct bpf_prog *old_prog = nn->dp.xdp_prog;
2953 struct bpf_prog *prog = xdp->prog;
2954 struct nfp_net_dp *dp;
2957 if (!prog && !nn->dp.xdp_prog)
2959 if (prog && nn->dp.xdp_prog) {
2960 prog = xchg(&nn->dp.xdp_prog, prog);
2962 nfp_net_xdp_offload(nn, nn->dp.xdp_prog);
2966 dp = nfp_net_clone_dp(nn);
2970 dp->xdp_prog = prog;
2971 dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
2972 dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
2973 dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
2975 /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
2976 err = nfp_net_ring_reconfig(nn, dp, xdp->extack);
2981 bpf_prog_put(old_prog);
2983 nfp_net_xdp_offload(nn, nn->dp.xdp_prog);
2988 static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
2990 struct nfp_net *nn = netdev_priv(netdev);
2992 switch (xdp->command) {
2993 case XDP_SETUP_PROG:
2994 return nfp_net_xdp_setup(nn, xdp);
2995 case XDP_QUERY_PROG:
2996 xdp->prog_attached = !!nn->dp.xdp_prog;
3003 static const struct net_device_ops nfp_net_netdev_ops = {
3004 .ndo_open = nfp_net_netdev_open,
3005 .ndo_stop = nfp_net_netdev_close,
3006 .ndo_start_xmit = nfp_net_tx,
3007 .ndo_get_stats64 = nfp_net_stat64,
3008 .ndo_setup_tc = nfp_net_setup_tc,
3009 .ndo_tx_timeout = nfp_net_tx_timeout,
3010 .ndo_set_rx_mode = nfp_net_set_rx_mode,
3011 .ndo_change_mtu = nfp_net_change_mtu,
3012 .ndo_set_mac_address = eth_mac_addr,
3013 .ndo_set_features = nfp_net_set_features,
3014 .ndo_features_check = nfp_net_features_check,
3015 .ndo_get_phys_port_name = nfp_net_get_phys_port_name,
3016 .ndo_udp_tunnel_add = nfp_net_add_vxlan_port,
3017 .ndo_udp_tunnel_del = nfp_net_del_vxlan_port,
3018 .ndo_xdp = nfp_net_xdp,
3022 * nfp_net_info() - Print general info about the NIC
3023 * @nn: NFP Net device to reconfigure
3025 void nfp_net_info(struct nfp_net *nn)
3027 nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3028 nn->dp.is_vf ? "VF " : "",
3029 nn->dp.num_tx_rings, nn->max_tx_rings,
3030 nn->dp.num_rx_rings, nn->max_rx_rings);
3031 nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
3032 nn->fw_ver.resv, nn->fw_ver.class,
3033 nn->fw_ver.major, nn->fw_ver.minor,
3035 nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
3037 nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
3038 nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
3039 nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
3040 nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
3041 nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
3042 nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
3043 nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
3044 nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
3045 nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
3046 nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO " : "",
3047 nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS " : "",
3048 nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
3049 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
3050 nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
3051 nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
3052 nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
3053 nfp_net_ebpf_capable(nn) ? "BPF " : "");
3057 * nfp_net_netdev_alloc() - Allocate netdev and related structure
3059 * @max_tx_rings: Maximum number of TX rings supported by device
3060 * @max_rx_rings: Maximum number of RX rings supported by device
3062 * This function allocates a netdev device and fills in the initial
3063 * part of the @struct nfp_net structure.
3065 * Return: NFP Net device structure, or ERR_PTR on error.
3067 struct nfp_net *nfp_net_netdev_alloc(struct pci_dev *pdev,
3068 unsigned int max_tx_rings,
3069 unsigned int max_rx_rings)
3071 struct net_device *netdev;
3074 netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
3075 max_tx_rings, max_rx_rings);
3077 return ERR_PTR(-ENOMEM);
3079 SET_NETDEV_DEV(netdev, &pdev->dev);
3080 nn = netdev_priv(netdev);
3082 nn->dp.netdev = netdev;
3083 nn->dp.dev = &pdev->dev;
3086 nn->max_tx_rings = max_tx_rings;
3087 nn->max_rx_rings = max_rx_rings;
3089 nn->dp.num_tx_rings = min_t(unsigned int,
3090 max_tx_rings, num_online_cpus());
3091 nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3092 netif_get_num_default_rss_queues());
3094 nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
3095 nn->dp.num_r_vecs = min_t(unsigned int,
3096 nn->dp.num_r_vecs, num_online_cpus());
3098 nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
3099 nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3101 spin_lock_init(&nn->reconfig_lock);
3102 spin_lock_init(&nn->rx_filter_lock);
3103 spin_lock_init(&nn->link_status_lock);
3105 setup_timer(&nn->reconfig_timer,
3106 nfp_net_reconfig_timer, (unsigned long)nn);
3107 setup_timer(&nn->rx_filter_stats_timer,
3108 nfp_net_filter_stats_timer, (unsigned long)nn);
3114 * nfp_net_netdev_free() - Undo what @nfp_net_netdev_alloc() did
3115 * @nn: NFP Net device to reconfigure
3117 void nfp_net_netdev_free(struct nfp_net *nn)
3119 free_netdev(nn->dp.netdev);
3123 * nfp_net_rss_key_sz() - Get current size of the RSS key
3124 * @nn: NFP Net device instance
3126 * Return: size of the RSS key for currently selected hash function.
3128 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
3130 switch (nn->rss_hfunc) {
3131 case ETH_RSS_HASH_TOP:
3132 return NFP_NET_CFG_RSS_KEY_SZ;
3133 case ETH_RSS_HASH_XOR:
3135 case ETH_RSS_HASH_CRC32:
3139 nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
3144 * nfp_net_rss_init() - Set the initial RSS parameters
3145 * @nn: NFP Net device to reconfigure
3147 static void nfp_net_rss_init(struct nfp_net *nn)
3149 unsigned long func_bit, rss_cap_hfunc;
3152 /* Read the RSS function capability and select first supported func */
3153 reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
3154 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
3156 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
3157 NFP_NET_CFG_RSS_TOEPLITZ);
3159 func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
3160 if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3161 dev_warn(nn->dp.dev,
3162 "Bad RSS config, defaulting to Toeplitz hash\n");
3163 func_bit = ETH_RSS_HASH_TOP_BIT;
3165 nn->rss_hfunc = 1 << func_bit;
3167 netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3169 nfp_net_rss_init_itbl(nn);
3171 /* Enable IPv4/IPv6 TCP by default */
3172 nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
3173 NFP_NET_CFG_RSS_IPV6_TCP |
3174 FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3175 NFP_NET_CFG_RSS_MASK;
3179 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3180 * @nn: NFP Net device to reconfigure
3182 static void nfp_net_irqmod_init(struct nfp_net *nn)
3184 nn->rx_coalesce_usecs = 50;
3185 nn->rx_coalesce_max_frames = 64;
3186 nn->tx_coalesce_usecs = 50;
3187 nn->tx_coalesce_max_frames = 64;
3191 * nfp_net_netdev_init() - Initialise/finalise the netdev structure
3192 * @netdev: netdev structure
3194 * Return: 0 on success or negative errno on error.
3196 int nfp_net_netdev_init(struct net_device *netdev)
3198 struct nfp_net *nn = netdev_priv(netdev);
3201 nn->dp.chained_metadata_format = nn->fw_ver.major > 3;
3203 nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
3205 /* Get some of the read-only fields from the BAR */
3206 nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
3207 nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
3209 nfp_net_write_mac_addr(nn);
3211 /* Determine RX packet/metadata boundary offset */
3212 if (nn->fw_ver.major >= 2) {
3215 reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
3216 if (reg > NFP_NET_MAX_PREPEND) {
3217 nn_err(nn, "Invalid rx offset: %d\n", reg);
3220 nn->dp.rx_offset = reg;
3222 nn->dp.rx_offset = NFP_NET_RX_OFFSET;
3225 /* Set default MTU and Freelist buffer size */
3226 if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
3227 netdev->mtu = nn->max_mtu;
3229 netdev->mtu = NFP_NET_DEFAULT_MTU;
3230 nn->dp.mtu = netdev->mtu;
3231 nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
3233 /* Advertise/enable offloads based on capabilities
3235 * Note: netdev->features show the currently enabled features
3236 * and netdev->hw_features advertises which features are
3237 * supported. By default we enable most features.
3239 netdev->hw_features = NETIF_F_HIGHDMA;
3240 if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM) {
3241 netdev->hw_features |= NETIF_F_RXCSUM;
3242 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
3244 if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
3245 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3246 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3248 if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
3249 netdev->hw_features |= NETIF_F_SG;
3250 nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
3252 if ((nn->cap & NFP_NET_CFG_CTRL_LSO) && nn->fw_ver.major > 2) {
3253 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3254 nn->dp.ctrl |= NFP_NET_CFG_CTRL_LSO;
3256 if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
3257 netdev->hw_features |= NETIF_F_RXHASH;
3258 nfp_net_rss_init(nn);
3259 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RSS;
3261 if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
3262 nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
3263 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3264 netdev->hw_features |= NETIF_F_GSO_GRE |
3265 NETIF_F_GSO_UDP_TUNNEL;
3266 nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
3268 netdev->hw_enc_features = netdev->hw_features;
3271 netdev->vlan_features = netdev->hw_features;
3273 if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
3274 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3275 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3277 if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
3278 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3279 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3282 netdev->features = netdev->hw_features;
3284 if (nfp_net_ebpf_capable(nn))
3285 netdev->hw_features |= NETIF_F_HW_TC;
3287 /* Advertise but disable TSO by default. */
3288 netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3289 nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO;
3291 /* Allow L2 Broadcast and Multicast through by default, if supported */
3292 if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
3293 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
3294 if (nn->cap & NFP_NET_CFG_CTRL_L2MC)
3295 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2MC;
3297 /* Allow IRQ moderation, if supported */
3298 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
3299 nfp_net_irqmod_init(nn);
3300 nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
3303 /* Stash the re-configuration queue away. First odd queue in TX Bar */
3304 nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
3306 /* Make sure the FW knows the netdev is supposed to be disabled here */
3307 nn_writel(nn, NFP_NET_CFG_CTRL, 0);
3308 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
3309 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
3310 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
3311 NFP_NET_CFG_UPDATE_GEN);
3315 /* Finalise the netdev setup */
3316 netdev->netdev_ops = &nfp_net_netdev_ops;
3317 netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
3319 /* MTU range: 68 - hw-specific max */
3320 netdev->min_mtu = ETH_MIN_MTU;
3321 netdev->max_mtu = nn->max_mtu;
3323 netif_carrier_off(netdev);
3325 nfp_net_set_ethtool_ops(netdev);
3326 nfp_net_vecs_init(netdev);
3328 return register_netdev(netdev);
3332 * nfp_net_netdev_clean() - Undo what nfp_net_netdev_init() did.
3333 * @netdev: netdev structure
3335 void nfp_net_netdev_clean(struct net_device *netdev)
3337 struct nfp_net *nn = netdev_priv(netdev);
3339 unregister_netdev(nn->dp.netdev);
3341 if (nn->dp.xdp_prog)
3342 bpf_prog_put(nn->dp.xdp_prog);
3343 if (nn->dp.bpf_offload_xdp)
3344 nfp_net_xdp_offload(nn, NULL);