1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
6 * Netronome network device driver: Common functions between PF and VF
7 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8 * Jason McMullan <jason.mcmullan@netronome.com>
9 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
10 * Brad Petrus <brad.petrus@netronome.com>
11 * Chris Telfer <chris.telfer@netronome.com>
14 #include <linux/bitfield.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf_trace.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/interrupt.h>
25 #include <linux/ipv6.h>
27 #include <linux/overflow.h>
28 #include <linux/page_ref.h>
29 #include <linux/pci.h>
30 #include <linux/pci_regs.h>
31 #include <linux/msi.h>
32 #include <linux/ethtool.h>
33 #include <linux/log2.h>
34 #include <linux/if_vlan.h>
35 #include <linux/random.h>
36 #include <linux/vmalloc.h>
37 #include <linux/ktime.h>
39 #include <net/vxlan.h>
41 #include "nfpcore/nfp_nsp.h"
43 #include "nfp_net_ctrl.h"
45 #include "nfp_net_sriov.h"
49 * nfp_net_get_fw_version() - Read and parse the FW version
50 * @fw_ver: Output fw_version structure to read to
51 * @ctrl_bar: Mapped address of the control BAR
53 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
54 void __iomem *ctrl_bar)
58 reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
59 put_unaligned_le32(reg, fw_ver);
62 static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
64 return dma_map_single_attrs(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
65 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
66 dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
70 nfp_net_dma_sync_dev_rx(const struct nfp_net_dp *dp, dma_addr_t dma_addr)
72 dma_sync_single_for_device(dp->dev, dma_addr,
73 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
77 static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
79 dma_unmap_single_attrs(dp->dev, dma_addr,
80 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
81 dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
84 static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr,
87 dma_sync_single_for_cpu(dp->dev, dma_addr - NFP_NET_RX_BUF_HEADROOM,
93 * Firmware reconfig may take a while so we have two versions of it -
94 * synchronous and asynchronous (posted). All synchronous callers are holding
95 * RTNL so we don't have to worry about serializing them.
97 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
99 nn_writel(nn, NFP_NET_CFG_UPDATE, update);
100 /* ensure update is written before pinging HW */
102 nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
103 nn->reconfig_in_progress_update = update;
106 /* Pass 0 as update to run posted reconfigs. */
107 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
109 update |= nn->reconfig_posted;
110 nn->reconfig_posted = 0;
112 nfp_net_reconfig_start(nn, update);
114 nn->reconfig_timer_active = true;
115 mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
118 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
122 reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
125 if (reg & NFP_NET_CFG_UPDATE_ERR) {
126 nn_err(nn, "Reconfig error (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
127 reg, nn->reconfig_in_progress_update,
128 nn_readl(nn, NFP_NET_CFG_CTRL));
130 } else if (last_check) {
131 nn_err(nn, "Reconfig timeout (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
132 reg, nn->reconfig_in_progress_update,
133 nn_readl(nn, NFP_NET_CFG_CTRL));
140 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
142 bool timed_out = false;
144 /* Poll update field, waiting for NFP to ack the config */
145 while (!nfp_net_reconfig_check_done(nn, timed_out)) {
147 timed_out = time_is_before_eq_jiffies(deadline);
150 if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
153 return timed_out ? -EIO : 0;
156 static void nfp_net_reconfig_timer(struct timer_list *t)
158 struct nfp_net *nn = from_timer(nn, t, reconfig_timer);
160 spin_lock_bh(&nn->reconfig_lock);
162 nn->reconfig_timer_active = false;
164 /* If sync caller is present it will take over from us */
165 if (nn->reconfig_sync_present)
168 /* Read reconfig status and report errors */
169 nfp_net_reconfig_check_done(nn, true);
171 if (nn->reconfig_posted)
172 nfp_net_reconfig_start_async(nn, 0);
174 spin_unlock_bh(&nn->reconfig_lock);
178 * nfp_net_reconfig_post() - Post async reconfig request
179 * @nn: NFP Net device to reconfigure
180 * @update: The value for the update field in the BAR config
182 * Record FW reconfiguration request. Reconfiguration will be kicked off
183 * whenever reconfiguration machinery is idle. Multiple requests can be
186 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
188 spin_lock_bh(&nn->reconfig_lock);
190 /* Sync caller will kick off async reconf when it's done, just post */
191 if (nn->reconfig_sync_present) {
192 nn->reconfig_posted |= update;
196 /* Opportunistically check if the previous command is done */
197 if (!nn->reconfig_timer_active ||
198 nfp_net_reconfig_check_done(nn, false))
199 nfp_net_reconfig_start_async(nn, update);
201 nn->reconfig_posted |= update;
203 spin_unlock_bh(&nn->reconfig_lock);
206 static void nfp_net_reconfig_sync_enter(struct nfp_net *nn)
208 bool cancelled_timer = false;
209 u32 pre_posted_requests;
211 spin_lock_bh(&nn->reconfig_lock);
213 nn->reconfig_sync_present = true;
215 if (nn->reconfig_timer_active) {
216 nn->reconfig_timer_active = false;
217 cancelled_timer = true;
219 pre_posted_requests = nn->reconfig_posted;
220 nn->reconfig_posted = 0;
222 spin_unlock_bh(&nn->reconfig_lock);
224 if (cancelled_timer) {
225 del_timer_sync(&nn->reconfig_timer);
226 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
229 /* Run the posted reconfigs which were issued before we started */
230 if (pre_posted_requests) {
231 nfp_net_reconfig_start(nn, pre_posted_requests);
232 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
236 static void nfp_net_reconfig_wait_posted(struct nfp_net *nn)
238 nfp_net_reconfig_sync_enter(nn);
240 spin_lock_bh(&nn->reconfig_lock);
241 nn->reconfig_sync_present = false;
242 spin_unlock_bh(&nn->reconfig_lock);
246 * nfp_net_reconfig() - Reconfigure the firmware
247 * @nn: NFP Net device to reconfigure
248 * @update: The value for the update field in the BAR config
250 * Write the update word to the BAR and ping the reconfig queue. The
251 * poll until the firmware has acknowledged the update by zeroing the
254 * Return: Negative errno on error, 0 on success
256 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
260 nfp_net_reconfig_sync_enter(nn);
262 nfp_net_reconfig_start(nn, update);
263 ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
265 spin_lock_bh(&nn->reconfig_lock);
267 if (nn->reconfig_posted)
268 nfp_net_reconfig_start_async(nn, 0);
270 nn->reconfig_sync_present = false;
272 spin_unlock_bh(&nn->reconfig_lock);
278 * nfp_net_reconfig_mbox() - Reconfigure the firmware via the mailbox
279 * @nn: NFP Net device to reconfigure
280 * @mbox_cmd: The value for the mailbox command
282 * Helper function for mailbox updates
284 * Return: Negative errno on error, 0 on success
286 int nfp_net_reconfig_mbox(struct nfp_net *nn, u32 mbox_cmd)
288 u32 mbox = nn->tlv_caps.mbox_off;
291 if (!nfp_net_has_mbox(&nn->tlv_caps)) {
292 nn_err(nn, "no mailbox present, command: %u\n", mbox_cmd);
296 nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
298 ret = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
300 nn_err(nn, "Mailbox update error\n");
304 return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
307 /* Interrupt configuration and handling
311 * nfp_net_irq_unmask() - Unmask automasked interrupt
312 * @nn: NFP Network structure
313 * @entry_nr: MSI-X table entry
315 * Clear the ICR for the IRQ entry.
317 static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
319 nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
324 * nfp_net_irqs_alloc() - allocates MSI-X irqs
325 * @pdev: PCI device structure
326 * @irq_entries: Array to be initialized and used to hold the irq entries
327 * @min_irqs: Minimal acceptable number of interrupts
328 * @wanted_irqs: Target number of interrupts to allocate
330 * Return: Number of irqs obtained or 0 on error.
333 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
334 unsigned int min_irqs, unsigned int wanted_irqs)
339 for (i = 0; i < wanted_irqs; i++)
340 irq_entries[i].entry = i;
342 got_irqs = pci_enable_msix_range(pdev, irq_entries,
343 min_irqs, wanted_irqs);
345 dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
346 min_irqs, wanted_irqs, got_irqs);
350 if (got_irqs < wanted_irqs)
351 dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
352 wanted_irqs, got_irqs);
358 * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
359 * @nn: NFP Network structure
360 * @irq_entries: Table of allocated interrupts
361 * @n: Size of @irq_entries (number of entries to grab)
363 * After interrupts are allocated with nfp_net_irqs_alloc() this function
364 * should be called to assign them to a specific netdev (port).
367 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
370 struct nfp_net_dp *dp = &nn->dp;
372 nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
373 dp->num_r_vecs = nn->max_r_vecs;
375 memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
377 if (dp->num_rx_rings > dp->num_r_vecs ||
378 dp->num_tx_rings > dp->num_r_vecs)
379 dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
380 dp->num_rx_rings, dp->num_tx_rings,
383 dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
384 dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
385 dp->num_stack_tx_rings = dp->num_tx_rings;
389 * nfp_net_irqs_disable() - Disable interrupts
390 * @pdev: PCI device structure
392 * Undoes what @nfp_net_irqs_alloc() does.
394 void nfp_net_irqs_disable(struct pci_dev *pdev)
396 pci_disable_msix(pdev);
400 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
402 * @data: Opaque data structure
404 * Return: Indicate if the interrupt has been handled.
406 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
408 struct nfp_net_r_vector *r_vec = data;
410 napi_schedule_irqoff(&r_vec->napi);
412 /* The FW auto-masks any interrupt, either via the MASK bit in
413 * the MSI-X table or via the per entry ICR field. So there
414 * is no need to disable interrupts here.
419 static irqreturn_t nfp_ctrl_irq_rxtx(int irq, void *data)
421 struct nfp_net_r_vector *r_vec = data;
423 tasklet_schedule(&r_vec->tasklet);
429 * nfp_net_read_link_status() - Reread link status from control BAR
430 * @nn: NFP Network structure
432 static void nfp_net_read_link_status(struct nfp_net *nn)
438 spin_lock_irqsave(&nn->link_status_lock, flags);
440 sts = nn_readl(nn, NFP_NET_CFG_STS);
441 link_up = !!(sts & NFP_NET_CFG_STS_LINK);
443 if (nn->link_up == link_up)
446 nn->link_up = link_up;
448 set_bit(NFP_PORT_CHANGED, &nn->port->flags);
451 netif_carrier_on(nn->dp.netdev);
452 netdev_info(nn->dp.netdev, "NIC Link is Up\n");
454 netif_carrier_off(nn->dp.netdev);
455 netdev_info(nn->dp.netdev, "NIC Link is Down\n");
458 spin_unlock_irqrestore(&nn->link_status_lock, flags);
462 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
464 * @data: Opaque data structure
466 * Return: Indicate if the interrupt has been handled.
468 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
470 struct nfp_net *nn = data;
471 struct msix_entry *entry;
473 entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
475 nfp_net_read_link_status(nn);
477 nfp_net_irq_unmask(nn, entry->entry);
483 * nfp_net_irq_exn() - Interrupt service routine for exceptions
485 * @data: Opaque data structure
487 * Return: Indicate if the interrupt has been handled.
489 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
491 struct nfp_net *nn = data;
493 nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
494 /* XXX TO BE IMPLEMENTED */
499 * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
500 * @tx_ring: TX ring structure
501 * @r_vec: IRQ vector servicing this ring
503 * @is_xdp: Is this an XDP TX ring?
506 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
507 struct nfp_net_r_vector *r_vec, unsigned int idx,
510 struct nfp_net *nn = r_vec->nfp_net;
513 tx_ring->r_vec = r_vec;
514 tx_ring->is_xdp = is_xdp;
515 u64_stats_init(&tx_ring->r_vec->tx_sync);
517 tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
518 tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
522 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
523 * @rx_ring: RX ring structure
524 * @r_vec: IRQ vector servicing this ring
528 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
529 struct nfp_net_r_vector *r_vec, unsigned int idx)
531 struct nfp_net *nn = r_vec->nfp_net;
534 rx_ring->r_vec = r_vec;
535 u64_stats_init(&rx_ring->r_vec->rx_sync);
537 rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
538 rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
542 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
543 * @nn: NFP Network structure
544 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
545 * @format: printf-style format to construct the interrupt name
546 * @name: Pointer to allocated space for interrupt name
547 * @name_sz: Size of space for interrupt name
548 * @vector_idx: Index of MSI-X vector used for this interrupt
549 * @handler: IRQ handler to register for this interrupt
552 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
553 const char *format, char *name, size_t name_sz,
554 unsigned int vector_idx, irq_handler_t handler)
556 struct msix_entry *entry;
559 entry = &nn->irq_entries[vector_idx];
561 snprintf(name, name_sz, format, nfp_net_name(nn));
562 err = request_irq(entry->vector, handler, 0, name, nn);
564 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
568 nn_writeb(nn, ctrl_offset, entry->entry);
569 nfp_net_irq_unmask(nn, entry->entry);
575 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
576 * @nn: NFP Network structure
577 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
578 * @vector_idx: Index of MSI-X vector used for this interrupt
580 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
581 unsigned int vector_idx)
583 nn_writeb(nn, ctrl_offset, 0xff);
585 free_irq(nn->irq_entries[vector_idx].vector, nn);
590 * One queue controller peripheral queue is used for transmit. The
591 * driver en-queues packets for transmit by advancing the write
592 * pointer. The device indicates that packets have transmitted by
593 * advancing the read pointer. The driver maintains a local copy of
594 * the read and write pointer in @struct nfp_net_tx_ring. The driver
595 * keeps @wr_p in sync with the queue controller write pointer and can
596 * determine how many packets have been transmitted by comparing its
597 * copy of the read pointer @rd_p with the read pointer maintained by
598 * the queue controller peripheral.
602 * nfp_net_tx_full() - Check if the TX ring is full
603 * @tx_ring: TX ring to check
604 * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
606 * This function checks, based on the *host copy* of read/write
607 * pointer if a given TX ring is full. The real TX queue may have
608 * some newly made available slots.
610 * Return: True if the ring is full.
612 static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
614 return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
617 /* Wrappers for deciding when to stop and restart TX queues */
618 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
620 return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
623 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
625 return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
629 * nfp_net_tx_ring_stop() - stop tx ring
630 * @nd_q: netdev queue
631 * @tx_ring: driver tx queue structure
633 * Safely stop TX ring. Remember that while we are running .start_xmit()
634 * someone else may be cleaning the TX ring completions so we need to be
635 * extra careful here.
637 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
638 struct nfp_net_tx_ring *tx_ring)
640 netif_tx_stop_queue(nd_q);
642 /* We can race with the TX completion out of NAPI so recheck */
644 if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
645 netif_tx_start_queue(nd_q);
649 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
650 * @r_vec: per-ring structure
651 * @txbuf: Pointer to driver soft TX descriptor
652 * @txd: Pointer to HW TX descriptor
653 * @skb: Pointer to SKB
654 * @md_bytes: Prepend length
656 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
657 * Return error on packet header greater than maximum supported LSO header size.
659 static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
660 struct nfp_net_tx_buf *txbuf,
661 struct nfp_net_tx_desc *txd, struct sk_buff *skb,
664 u32 l3_offset, l4_offset, hdrlen;
667 if (!skb_is_gso(skb))
670 if (!skb->encapsulation) {
671 l3_offset = skb_network_offset(skb);
672 l4_offset = skb_transport_offset(skb);
673 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
675 l3_offset = skb_inner_network_offset(skb);
676 l4_offset = skb_inner_transport_offset(skb);
677 hdrlen = skb_inner_transport_header(skb) - skb->data +
678 inner_tcp_hdrlen(skb);
681 txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
682 txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
684 mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
685 txd->l3_offset = l3_offset - md_bytes;
686 txd->l4_offset = l4_offset - md_bytes;
687 txd->lso_hdrlen = hdrlen - md_bytes;
688 txd->mss = cpu_to_le16(mss);
689 txd->flags |= PCIE_DESC_TX_LSO;
691 u64_stats_update_begin(&r_vec->tx_sync);
693 u64_stats_update_end(&r_vec->tx_sync);
697 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
698 * @dp: NFP Net data path struct
699 * @r_vec: per-ring structure
700 * @txbuf: Pointer to driver soft TX descriptor
701 * @txd: Pointer to TX descriptor
702 * @skb: Pointer to SKB
704 * This function sets the TX checksum flags in the TX descriptor based
705 * on the configuration and the protocol of the packet to be transmitted.
707 static void nfp_net_tx_csum(struct nfp_net_dp *dp,
708 struct nfp_net_r_vector *r_vec,
709 struct nfp_net_tx_buf *txbuf,
710 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
712 struct ipv6hdr *ipv6h;
716 if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
719 if (skb->ip_summed != CHECKSUM_PARTIAL)
722 txd->flags |= PCIE_DESC_TX_CSUM;
723 if (skb->encapsulation)
724 txd->flags |= PCIE_DESC_TX_ENCAP;
726 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
727 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
729 if (iph->version == 4) {
730 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
731 l4_hdr = iph->protocol;
732 } else if (ipv6h->version == 6) {
733 l4_hdr = ipv6h->nexthdr;
735 nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
741 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
744 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
747 nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
751 u64_stats_update_begin(&r_vec->tx_sync);
752 if (skb->encapsulation)
753 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
755 r_vec->hw_csum_tx += txbuf->pkt_cnt;
756 u64_stats_update_end(&r_vec->tx_sync);
759 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
762 nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
763 tx_ring->wr_ptr_add = 0;
766 static int nfp_net_prep_port_id(struct sk_buff *skb)
768 struct metadata_dst *md_dst = skb_metadata_dst(skb);
773 if (unlikely(md_dst->type != METADATA_HW_PORT_MUX))
776 if (unlikely(skb_cow_head(skb, 8)))
779 data = skb_push(skb, 8);
780 put_unaligned_be32(NFP_NET_META_PORTID, data);
781 put_unaligned_be32(md_dst->u.port_info.port_id, data + 4);
787 * nfp_net_tx() - Main transmit entry point
788 * @skb: SKB to transmit
789 * @netdev: netdev structure
791 * Return: NETDEV_TX_OK on success.
793 static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
795 struct nfp_net *nn = netdev_priv(netdev);
796 const struct skb_frag_struct *frag;
797 int f, nr_frags, wr_idx, md_bytes;
798 struct nfp_net_tx_ring *tx_ring;
799 struct nfp_net_r_vector *r_vec;
800 struct nfp_net_tx_buf *txbuf;
801 struct nfp_net_tx_desc *txd;
802 struct netdev_queue *nd_q;
803 struct nfp_net_dp *dp;
809 qidx = skb_get_queue_mapping(skb);
810 tx_ring = &dp->tx_rings[qidx];
811 r_vec = tx_ring->r_vec;
813 nr_frags = skb_shinfo(skb)->nr_frags;
815 if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
816 nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
817 qidx, tx_ring->wr_p, tx_ring->rd_p);
818 nd_q = netdev_get_tx_queue(dp->netdev, qidx);
819 netif_tx_stop_queue(nd_q);
820 nfp_net_tx_xmit_more_flush(tx_ring);
821 u64_stats_update_begin(&r_vec->tx_sync);
823 u64_stats_update_end(&r_vec->tx_sync);
824 return NETDEV_TX_BUSY;
827 md_bytes = nfp_net_prep_port_id(skb);
828 if (unlikely(md_bytes < 0)) {
829 nfp_net_tx_xmit_more_flush(tx_ring);
830 dev_kfree_skb_any(skb);
834 /* Start with the head skbuf */
835 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
837 if (dma_mapping_error(dp->dev, dma_addr))
840 wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
842 /* Stash the soft descriptor of the head then initialize it */
843 txbuf = &tx_ring->txbufs[wr_idx];
845 txbuf->dma_addr = dma_addr;
848 txbuf->real_len = skb->len;
850 /* Build TX descriptor */
851 txd = &tx_ring->txds[wr_idx];
852 txd->offset_eop = (nr_frags ? 0 : PCIE_DESC_TX_EOP) | md_bytes;
853 txd->dma_len = cpu_to_le16(skb_headlen(skb));
854 nfp_desc_set_dma_addr(txd, dma_addr);
855 txd->data_len = cpu_to_le16(skb->len);
861 /* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
862 nfp_net_tx_tso(r_vec, txbuf, txd, skb, md_bytes);
863 nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
864 if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
865 txd->flags |= PCIE_DESC_TX_VLAN;
866 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
873 /* all descs must match except for in addr, length and eop */
874 second_half = txd->vals8[1];
876 for (f = 0; f < nr_frags; f++) {
877 frag = &skb_shinfo(skb)->frags[f];
878 fsize = skb_frag_size(frag);
880 dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
881 fsize, DMA_TO_DEVICE);
882 if (dma_mapping_error(dp->dev, dma_addr))
885 wr_idx = D_IDX(tx_ring, wr_idx + 1);
886 tx_ring->txbufs[wr_idx].skb = skb;
887 tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
888 tx_ring->txbufs[wr_idx].fidx = f;
890 txd = &tx_ring->txds[wr_idx];
891 txd->dma_len = cpu_to_le16(fsize);
892 nfp_desc_set_dma_addr(txd, dma_addr);
893 txd->offset_eop = md_bytes |
894 ((f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0);
895 txd->vals8[1] = second_half;
898 u64_stats_update_begin(&r_vec->tx_sync);
900 u64_stats_update_end(&r_vec->tx_sync);
903 skb_tx_timestamp(skb);
905 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
907 tx_ring->wr_p += nr_frags + 1;
908 if (nfp_net_tx_ring_should_stop(tx_ring))
909 nfp_net_tx_ring_stop(nd_q, tx_ring);
911 tx_ring->wr_ptr_add += nr_frags + 1;
912 if (__netdev_tx_sent_queue(nd_q, txbuf->real_len, skb->xmit_more))
913 nfp_net_tx_xmit_more_flush(tx_ring);
919 frag = &skb_shinfo(skb)->frags[f];
920 dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
921 skb_frag_size(frag), DMA_TO_DEVICE);
922 tx_ring->txbufs[wr_idx].skb = NULL;
923 tx_ring->txbufs[wr_idx].dma_addr = 0;
924 tx_ring->txbufs[wr_idx].fidx = -2;
927 wr_idx += tx_ring->cnt;
929 dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
930 skb_headlen(skb), DMA_TO_DEVICE);
931 tx_ring->txbufs[wr_idx].skb = NULL;
932 tx_ring->txbufs[wr_idx].dma_addr = 0;
933 tx_ring->txbufs[wr_idx].fidx = -2;
935 nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
936 nfp_net_tx_xmit_more_flush(tx_ring);
937 u64_stats_update_begin(&r_vec->tx_sync);
939 u64_stats_update_end(&r_vec->tx_sync);
940 dev_kfree_skb_any(skb);
945 * nfp_net_tx_complete() - Handled completed TX packets
946 * @tx_ring: TX ring structure
947 * @budget: NAPI budget (only used as bool to determine if in NAPI context)
949 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
951 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
952 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
953 struct netdev_queue *nd_q;
954 u32 done_pkts = 0, done_bytes = 0;
958 if (tx_ring->wr_p == tx_ring->rd_p)
961 /* Work out how many descriptors have been transmitted */
962 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
964 if (qcp_rd_p == tx_ring->qcp_rd_p)
967 todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
970 const struct skb_frag_struct *frag;
971 struct nfp_net_tx_buf *tx_buf;
976 idx = D_IDX(tx_ring, tx_ring->rd_p++);
977 tx_buf = &tx_ring->txbufs[idx];
983 nr_frags = skb_shinfo(skb)->nr_frags;
988 dma_unmap_single(dp->dev, tx_buf->dma_addr,
989 skb_headlen(skb), DMA_TO_DEVICE);
991 done_pkts += tx_buf->pkt_cnt;
992 done_bytes += tx_buf->real_len;
995 frag = &skb_shinfo(skb)->frags[fidx];
996 dma_unmap_page(dp->dev, tx_buf->dma_addr,
997 skb_frag_size(frag), DMA_TO_DEVICE);
1000 /* check for last gather fragment */
1001 if (fidx == nr_frags - 1)
1002 napi_consume_skb(skb, budget);
1004 tx_buf->dma_addr = 0;
1009 tx_ring->qcp_rd_p = qcp_rd_p;
1011 u64_stats_update_begin(&r_vec->tx_sync);
1012 r_vec->tx_bytes += done_bytes;
1013 r_vec->tx_pkts += done_pkts;
1014 u64_stats_update_end(&r_vec->tx_sync);
1019 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1020 netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
1021 if (nfp_net_tx_ring_should_wake(tx_ring)) {
1022 /* Make sure TX thread will see updated tx_ring->rd_p */
1025 if (unlikely(netif_tx_queue_stopped(nd_q)))
1026 netif_tx_wake_queue(nd_q);
1029 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1030 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1031 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1034 static bool nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
1036 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1037 u32 done_pkts = 0, done_bytes = 0;
1042 /* Work out how many descriptors have been transmitted */
1043 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1045 if (qcp_rd_p == tx_ring->qcp_rd_p)
1048 todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1050 done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
1051 todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
1053 tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
1057 idx = D_IDX(tx_ring, tx_ring->rd_p);
1060 done_bytes += tx_ring->txbufs[idx].real_len;
1063 u64_stats_update_begin(&r_vec->tx_sync);
1064 r_vec->tx_bytes += done_bytes;
1065 r_vec->tx_pkts += done_pkts;
1066 u64_stats_update_end(&r_vec->tx_sync);
1068 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1069 "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1070 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1076 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1077 * @dp: NFP Net data path struct
1078 * @tx_ring: TX ring structure
1080 * Assumes that the device is stopped, must be idempotent.
1083 nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1085 const struct skb_frag_struct *frag;
1086 struct netdev_queue *nd_q;
1088 while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
1089 struct nfp_net_tx_buf *tx_buf;
1090 struct sk_buff *skb;
1093 idx = D_IDX(tx_ring, tx_ring->rd_p);
1094 tx_buf = &tx_ring->txbufs[idx];
1096 skb = tx_ring->txbufs[idx].skb;
1097 nr_frags = skb_shinfo(skb)->nr_frags;
1099 if (tx_buf->fidx == -1) {
1101 dma_unmap_single(dp->dev, tx_buf->dma_addr,
1102 skb_headlen(skb), DMA_TO_DEVICE);
1104 /* unmap fragment */
1105 frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
1106 dma_unmap_page(dp->dev, tx_buf->dma_addr,
1107 skb_frag_size(frag), DMA_TO_DEVICE);
1110 /* check for last gather fragment */
1111 if (tx_buf->fidx == nr_frags - 1)
1112 dev_kfree_skb_any(skb);
1114 tx_buf->dma_addr = 0;
1118 tx_ring->qcp_rd_p++;
1122 memset(tx_ring->txds, 0, tx_ring->size);
1125 tx_ring->qcp_rd_p = 0;
1126 tx_ring->wr_ptr_add = 0;
1128 if (tx_ring->is_xdp || !dp->netdev)
1131 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1132 netdev_tx_reset_queue(nd_q);
1135 static void nfp_net_tx_timeout(struct net_device *netdev)
1137 struct nfp_net *nn = netdev_priv(netdev);
1140 for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
1141 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1143 nn_warn(nn, "TX timeout on ring: %d\n", i);
1145 nn_warn(nn, "TX watchdog timeout\n");
1148 /* Receive processing
1151 nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1153 unsigned int fl_bufsz;
1155 fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1156 fl_bufsz += dp->rx_dma_off;
1157 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1158 fl_bufsz += NFP_NET_MAX_PREPEND;
1160 fl_bufsz += dp->rx_offset;
1161 fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1163 fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1164 fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1170 nfp_net_free_frag(void *frag, bool xdp)
1173 skb_free_frag(frag);
1175 __free_page(virt_to_page(frag));
1179 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1180 * @dp: NFP Net data path struct
1181 * @dma_addr: Pointer to storage for DMA address (output param)
1183 * This function will allcate a new page frag, map it for DMA.
1185 * Return: allocated page frag or NULL on failure.
1187 static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1191 if (!dp->xdp_prog) {
1192 frag = netdev_alloc_frag(dp->fl_bufsz);
1196 page = alloc_page(GFP_KERNEL);
1197 frag = page ? page_address(page) : NULL;
1200 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1204 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1205 if (dma_mapping_error(dp->dev, *dma_addr)) {
1206 nfp_net_free_frag(frag, dp->xdp_prog);
1207 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1214 static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1218 if (!dp->xdp_prog) {
1219 frag = napi_alloc_frag(dp->fl_bufsz);
1220 if (unlikely(!frag))
1225 page = dev_alloc_page();
1226 if (unlikely(!page))
1228 frag = page_address(page);
1231 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1232 if (dma_mapping_error(dp->dev, *dma_addr)) {
1233 nfp_net_free_frag(frag, dp->xdp_prog);
1234 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1242 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1243 * @dp: NFP Net data path struct
1244 * @rx_ring: RX ring structure
1245 * @frag: page fragment buffer
1246 * @dma_addr: DMA address of skb mapping
1248 static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
1249 struct nfp_net_rx_ring *rx_ring,
1250 void *frag, dma_addr_t dma_addr)
1252 unsigned int wr_idx;
1254 wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1256 nfp_net_dma_sync_dev_rx(dp, dma_addr);
1258 /* Stash SKB and DMA address away */
1259 rx_ring->rxbufs[wr_idx].frag = frag;
1260 rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1262 /* Fill freelist descriptor */
1263 rx_ring->rxds[wr_idx].fld.reserved = 0;
1264 rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1265 nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
1266 dma_addr + dp->rx_dma_off);
1269 if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
1270 /* Update write pointer of the freelist queue. Make
1271 * sure all writes are flushed before telling the hardware.
1274 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
1279 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1280 * @rx_ring: RX ring structure
1282 * Assumes that the device is stopped, must be idempotent.
1284 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1286 unsigned int wr_idx, last_idx;
1288 /* wr_p == rd_p means ring was never fed FL bufs. RX rings are always
1289 * kept at cnt - 1 FL bufs.
1291 if (rx_ring->wr_p == 0 && rx_ring->rd_p == 0)
1294 /* Move the empty entry to the end of the list */
1295 wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1296 last_idx = rx_ring->cnt - 1;
1297 rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1298 rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1299 rx_ring->rxbufs[last_idx].dma_addr = 0;
1300 rx_ring->rxbufs[last_idx].frag = NULL;
1302 memset(rx_ring->rxds, 0, rx_ring->size);
1308 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1309 * @dp: NFP Net data path struct
1310 * @rx_ring: RX ring to remove buffers from
1312 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1313 * entries. After device is disabled nfp_net_rx_ring_reset() must be called
1314 * to restore required ring geometry.
1317 nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1318 struct nfp_net_rx_ring *rx_ring)
1322 for (i = 0; i < rx_ring->cnt - 1; i++) {
1323 /* NULL skb can only happen when initial filling of the ring
1324 * fails to allocate enough buffers and calls here to free
1325 * already allocated ones.
1327 if (!rx_ring->rxbufs[i].frag)
1330 nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1331 nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1332 rx_ring->rxbufs[i].dma_addr = 0;
1333 rx_ring->rxbufs[i].frag = NULL;
1338 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1339 * @dp: NFP Net data path struct
1340 * @rx_ring: RX ring to remove buffers from
1343 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1344 struct nfp_net_rx_ring *rx_ring)
1346 struct nfp_net_rx_buf *rxbufs;
1349 rxbufs = rx_ring->rxbufs;
1351 for (i = 0; i < rx_ring->cnt - 1; i++) {
1352 rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
1353 if (!rxbufs[i].frag) {
1354 nfp_net_rx_ring_bufs_free(dp, rx_ring);
1363 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1364 * @dp: NFP Net data path struct
1365 * @rx_ring: RX ring to fill
1368 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
1369 struct nfp_net_rx_ring *rx_ring)
1373 for (i = 0; i < rx_ring->cnt - 1; i++)
1374 nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1375 rx_ring->rxbufs[i].dma_addr);
1379 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1380 * @flags: RX descriptor flags field in CPU byte order
1382 static int nfp_net_rx_csum_has_errors(u16 flags)
1384 u16 csum_all_checked, csum_all_ok;
1386 csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1387 csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1389 return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1393 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1394 * @dp: NFP Net data path struct
1395 * @r_vec: per-ring structure
1396 * @rxd: Pointer to RX descriptor
1397 * @meta: Parsed metadata prepend
1398 * @skb: Pointer to SKB
1400 static void nfp_net_rx_csum(struct nfp_net_dp *dp,
1401 struct nfp_net_r_vector *r_vec,
1402 struct nfp_net_rx_desc *rxd,
1403 struct nfp_meta_parsed *meta, struct sk_buff *skb)
1405 skb_checksum_none_assert(skb);
1407 if (!(dp->netdev->features & NETIF_F_RXCSUM))
1410 if (meta->csum_type) {
1411 skb->ip_summed = meta->csum_type;
1412 skb->csum = meta->csum;
1413 u64_stats_update_begin(&r_vec->rx_sync);
1414 r_vec->hw_csum_rx_complete++;
1415 u64_stats_update_end(&r_vec->rx_sync);
1419 if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1420 u64_stats_update_begin(&r_vec->rx_sync);
1421 r_vec->hw_csum_rx_error++;
1422 u64_stats_update_end(&r_vec->rx_sync);
1426 /* Assume that the firmware will never report inner CSUM_OK unless outer
1427 * L4 headers were successfully parsed. FW will always report zero UDP
1428 * checksum as CSUM_OK.
1430 if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1431 rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1432 __skb_incr_checksum_unnecessary(skb);
1433 u64_stats_update_begin(&r_vec->rx_sync);
1434 r_vec->hw_csum_rx_ok++;
1435 u64_stats_update_end(&r_vec->rx_sync);
1438 if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1439 rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1440 __skb_incr_checksum_unnecessary(skb);
1441 u64_stats_update_begin(&r_vec->rx_sync);
1442 r_vec->hw_csum_rx_inner_ok++;
1443 u64_stats_update_end(&r_vec->rx_sync);
1448 nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
1449 unsigned int type, __be32 *hash)
1451 if (!(netdev->features & NETIF_F_RXHASH))
1455 case NFP_NET_RSS_IPV4:
1456 case NFP_NET_RSS_IPV6:
1457 case NFP_NET_RSS_IPV6_EX:
1458 meta->hash_type = PKT_HASH_TYPE_L3;
1461 meta->hash_type = PKT_HASH_TYPE_L4;
1465 meta->hash = get_unaligned_be32(hash);
1469 nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
1470 void *data, struct nfp_net_rx_desc *rxd)
1472 struct nfp_net_rx_hash *rx_hash = data;
1474 if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1477 nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
1482 nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
1483 void *data, int meta_len)
1487 meta_info = get_unaligned_be32(data);
1491 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1492 case NFP_NET_META_HASH:
1493 meta_info >>= NFP_NET_META_FIELD_SIZE;
1494 nfp_net_set_hash(netdev, meta,
1495 meta_info & NFP_NET_META_FIELD_MASK,
1499 case NFP_NET_META_MARK:
1500 meta->mark = get_unaligned_be32(data);
1503 case NFP_NET_META_PORTID:
1504 meta->portid = get_unaligned_be32(data);
1507 case NFP_NET_META_CSUM:
1508 meta->csum_type = CHECKSUM_COMPLETE;
1510 (__force __wsum)__get_unaligned_cpu32(data);
1517 meta_info >>= NFP_NET_META_FIELD_SIZE;
1524 nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
1525 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
1526 struct sk_buff *skb)
1528 u64_stats_update_begin(&r_vec->rx_sync);
1530 /* If we have both skb and rxbuf the replacement buffer allocation
1531 * must have failed, count this as an alloc failure.
1534 r_vec->rx_replace_buf_alloc_fail++;
1535 u64_stats_update_end(&r_vec->rx_sync);
1537 /* skb is build based on the frag, free_skb() would free the frag
1538 * so to be able to reuse it we need an extra ref.
1540 if (skb && rxbuf && skb->head == rxbuf->frag)
1541 page_ref_inc(virt_to_head_page(rxbuf->frag));
1543 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1545 dev_kfree_skb_any(skb);
1549 nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1550 struct nfp_net_tx_ring *tx_ring,
1551 struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1552 unsigned int pkt_len, bool *completed)
1554 struct nfp_net_tx_buf *txbuf;
1555 struct nfp_net_tx_desc *txd;
1558 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1560 nfp_net_xdp_complete(tx_ring);
1564 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1565 nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
1571 wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1573 /* Stash the soft descriptor of the head then initialize it */
1574 txbuf = &tx_ring->txbufs[wr_idx];
1576 nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
1578 txbuf->frag = rxbuf->frag;
1579 txbuf->dma_addr = rxbuf->dma_addr;
1582 txbuf->real_len = pkt_len;
1584 dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1585 pkt_len, DMA_BIDIRECTIONAL);
1587 /* Build TX descriptor */
1588 txd = &tx_ring->txds[wr_idx];
1589 txd->offset_eop = PCIE_DESC_TX_EOP;
1590 txd->dma_len = cpu_to_le16(pkt_len);
1591 nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1592 txd->data_len = cpu_to_le16(pkt_len);
1596 txd->lso_hdrlen = 0;
1599 tx_ring->wr_ptr_add++;
1604 * nfp_net_rx() - receive up to @budget packets on @rx_ring
1605 * @rx_ring: RX ring to receive from
1606 * @budget: NAPI budget
1608 * Note, this function is separated out from the napi poll function to
1609 * more cleanly separate packet receive code from other bookkeeping
1610 * functions performed in the napi poll function.
1612 * Return: Number of packets received.
1614 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1616 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1617 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1618 struct nfp_net_tx_ring *tx_ring;
1619 struct bpf_prog *xdp_prog;
1620 bool xdp_tx_cmpl = false;
1621 unsigned int true_bufsz;
1622 struct sk_buff *skb;
1623 int pkts_polled = 0;
1624 struct xdp_buff xdp;
1628 xdp_prog = READ_ONCE(dp->xdp_prog);
1629 true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1630 xdp.rxq = &rx_ring->xdp_rxq;
1631 tx_ring = r_vec->xdp_ring;
1633 while (pkts_polled < budget) {
1634 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1635 struct nfp_net_rx_buf *rxbuf;
1636 struct nfp_net_rx_desc *rxd;
1637 struct nfp_meta_parsed meta;
1638 struct net_device *netdev;
1639 dma_addr_t new_dma_addr;
1640 u32 meta_len_xdp = 0;
1643 idx = D_IDX(rx_ring, rx_ring->rd_p);
1645 rxd = &rx_ring->rxds[idx];
1646 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1649 /* Memory barrier to ensure that we won't do other reads
1650 * before the DD bit.
1654 memset(&meta, 0, sizeof(meta));
1659 rxbuf = &rx_ring->rxbufs[idx];
1661 * <-- [rx_offset] -->
1662 * ---------------------------------------------------------
1663 * | [XX] | metadata | packet | XXXX |
1664 * ---------------------------------------------------------
1665 * <---------------- data_len --------------->
1667 * The rx_offset is fixed for all packets, the meta_len can vary
1668 * on a packet by packet basis. If rx_offset is set to zero
1669 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1670 * buffer and is immediately followed by the packet (no [XX]).
1672 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1673 data_len = le16_to_cpu(rxd->rxd.data_len);
1674 pkt_len = data_len - meta_len;
1676 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1677 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1678 pkt_off += meta_len;
1680 pkt_off += dp->rx_offset;
1681 meta_off = pkt_off - meta_len;
1684 u64_stats_update_begin(&r_vec->rx_sync);
1686 r_vec->rx_bytes += pkt_len;
1687 u64_stats_update_end(&r_vec->rx_sync);
1689 if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1690 (dp->rx_offset && meta_len > dp->rx_offset))) {
1691 nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1693 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1697 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1700 if (!dp->chained_metadata_format) {
1701 nfp_net_set_hash_desc(dp->netdev, &meta,
1702 rxbuf->frag + meta_off, rxd);
1703 } else if (meta_len) {
1706 end = nfp_net_parse_meta(dp->netdev, &meta,
1707 rxbuf->frag + meta_off,
1709 if (unlikely(end != rxbuf->frag + pkt_off)) {
1710 nn_dp_warn(dp, "invalid RX packet metadata\n");
1711 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1717 if (xdp_prog && !meta.portid) {
1718 void *orig_data = rxbuf->frag + pkt_off;
1719 unsigned int dma_off;
1722 xdp.data_hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
1723 xdp.data = orig_data;
1724 xdp.data_meta = orig_data;
1725 xdp.data_end = orig_data + pkt_len;
1727 act = bpf_prog_run_xdp(xdp_prog, &xdp);
1729 pkt_len = xdp.data_end - xdp.data;
1730 pkt_off += xdp.data - orig_data;
1734 meta_len_xdp = xdp.data - xdp.data_meta;
1737 dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1738 if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1743 trace_xdp_exception(dp->netdev,
1747 bpf_warn_invalid_xdp_action(act);
1750 trace_xdp_exception(dp->netdev, xdp_prog, act);
1753 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1759 if (likely(!meta.portid)) {
1760 netdev = dp->netdev;
1761 } else if (meta.portid == NFP_META_PORT_ID_CTRL) {
1762 struct nfp_net *nn = netdev_priv(dp->netdev);
1764 nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off,
1766 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1772 nn = netdev_priv(dp->netdev);
1773 netdev = nfp_app_repr_get(nn->app, meta.portid);
1774 if (unlikely(!netdev)) {
1775 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1779 nfp_repr_inc_rx_stats(netdev, pkt_len);
1782 skb = build_skb(rxbuf->frag, true_bufsz);
1783 if (unlikely(!skb)) {
1784 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1787 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1788 if (unlikely(!new_frag)) {
1789 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1793 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1795 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1797 skb_reserve(skb, pkt_off);
1798 skb_put(skb, pkt_len);
1800 skb->mark = meta.mark;
1801 skb_set_hash(skb, meta.hash, meta.hash_type);
1803 skb_record_rx_queue(skb, rx_ring->idx);
1804 skb->protocol = eth_type_trans(skb, netdev);
1806 nfp_net_rx_csum(dp, r_vec, rxd, &meta, skb);
1808 if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1809 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1810 le16_to_cpu(rxd->rxd.vlan));
1812 skb_metadata_set(skb, meta_len_xdp);
1814 napi_gro_receive(&rx_ring->r_vec->napi, skb);
1818 if (tx_ring->wr_ptr_add)
1819 nfp_net_tx_xmit_more_flush(tx_ring);
1820 else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
1822 if (!nfp_net_xdp_complete(tx_ring))
1823 pkts_polled = budget;
1831 * nfp_net_poll() - napi poll function
1832 * @napi: NAPI structure
1833 * @budget: NAPI budget
1835 * Return: number of packets polled.
1837 static int nfp_net_poll(struct napi_struct *napi, int budget)
1839 struct nfp_net_r_vector *r_vec =
1840 container_of(napi, struct nfp_net_r_vector, napi);
1841 unsigned int pkts_polled = 0;
1844 nfp_net_tx_complete(r_vec->tx_ring, budget);
1846 pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1848 if (pkts_polled < budget)
1849 if (napi_complete_done(napi, pkts_polled))
1850 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1855 /* Control device data path
1859 nfp_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1860 struct sk_buff *skb, bool old)
1862 unsigned int real_len = skb->len, meta_len = 0;
1863 struct nfp_net_tx_ring *tx_ring;
1864 struct nfp_net_tx_buf *txbuf;
1865 struct nfp_net_tx_desc *txd;
1866 struct nfp_net_dp *dp;
1867 dma_addr_t dma_addr;
1870 dp = &r_vec->nfp_net->dp;
1871 tx_ring = r_vec->tx_ring;
1873 if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
1874 nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
1878 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1879 u64_stats_update_begin(&r_vec->tx_sync);
1881 u64_stats_update_end(&r_vec->tx_sync);
1883 __skb_queue_tail(&r_vec->queue, skb);
1885 __skb_queue_head(&r_vec->queue, skb);
1889 if (nfp_app_ctrl_has_meta(nn->app)) {
1890 if (unlikely(skb_headroom(skb) < 8)) {
1891 nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
1895 put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
1896 put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
1899 /* Start with the head skbuf */
1900 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
1902 if (dma_mapping_error(dp->dev, dma_addr))
1905 wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1907 /* Stash the soft descriptor of the head then initialize it */
1908 txbuf = &tx_ring->txbufs[wr_idx];
1910 txbuf->dma_addr = dma_addr;
1913 txbuf->real_len = real_len;
1915 /* Build TX descriptor */
1916 txd = &tx_ring->txds[wr_idx];
1917 txd->offset_eop = meta_len | PCIE_DESC_TX_EOP;
1918 txd->dma_len = cpu_to_le16(skb_headlen(skb));
1919 nfp_desc_set_dma_addr(txd, dma_addr);
1920 txd->data_len = cpu_to_le16(skb->len);
1924 txd->lso_hdrlen = 0;
1927 tx_ring->wr_ptr_add++;
1928 nfp_net_tx_xmit_more_flush(tx_ring);
1933 nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
1935 u64_stats_update_begin(&r_vec->tx_sync);
1937 u64_stats_update_end(&r_vec->tx_sync);
1938 dev_kfree_skb_any(skb);
1942 bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
1944 struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
1946 return nfp_ctrl_tx_one(nn, r_vec, skb, false);
1949 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
1951 struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
1954 spin_lock_bh(&r_vec->lock);
1955 ret = nfp_ctrl_tx_one(nn, r_vec, skb, false);
1956 spin_unlock_bh(&r_vec->lock);
1961 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
1963 struct sk_buff *skb;
1965 while ((skb = __skb_dequeue(&r_vec->queue)))
1966 if (nfp_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
1971 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
1973 u32 meta_type, meta_tag;
1975 if (!nfp_app_ctrl_has_meta(nn->app))
1981 meta_type = get_unaligned_be32(data);
1982 meta_tag = get_unaligned_be32(data + 4);
1984 return (meta_type == NFP_NET_META_PORTID &&
1985 meta_tag == NFP_META_PORT_ID_CTRL);
1989 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
1990 struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
1992 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1993 struct nfp_net_rx_buf *rxbuf;
1994 struct nfp_net_rx_desc *rxd;
1995 dma_addr_t new_dma_addr;
1996 struct sk_buff *skb;
2000 idx = D_IDX(rx_ring, rx_ring->rd_p);
2002 rxd = &rx_ring->rxds[idx];
2003 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
2006 /* Memory barrier to ensure that we won't do other reads
2007 * before the DD bit.
2013 rxbuf = &rx_ring->rxbufs[idx];
2014 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
2015 data_len = le16_to_cpu(rxd->rxd.data_len);
2016 pkt_len = data_len - meta_len;
2018 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
2019 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
2020 pkt_off += meta_len;
2022 pkt_off += dp->rx_offset;
2023 meta_off = pkt_off - meta_len;
2026 u64_stats_update_begin(&r_vec->rx_sync);
2028 r_vec->rx_bytes += pkt_len;
2029 u64_stats_update_end(&r_vec->rx_sync);
2031 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off, data_len);
2033 if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
2034 nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
2036 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2040 skb = build_skb(rxbuf->frag, dp->fl_bufsz);
2041 if (unlikely(!skb)) {
2042 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2045 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
2046 if (unlikely(!new_frag)) {
2047 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
2051 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
2053 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
2055 skb_reserve(skb, pkt_off);
2056 skb_put(skb, pkt_len);
2058 nfp_app_ctrl_rx(nn->app, skb);
2063 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
2065 struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
2066 struct nfp_net *nn = r_vec->nfp_net;
2067 struct nfp_net_dp *dp = &nn->dp;
2068 unsigned int budget = 512;
2070 while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--)
2076 static void nfp_ctrl_poll(unsigned long arg)
2078 struct nfp_net_r_vector *r_vec = (void *)arg;
2080 spin_lock(&r_vec->lock);
2081 nfp_net_tx_complete(r_vec->tx_ring, 0);
2082 __nfp_ctrl_tx_queued(r_vec);
2083 spin_unlock(&r_vec->lock);
2085 if (nfp_ctrl_rx(r_vec)) {
2086 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
2088 tasklet_schedule(&r_vec->tasklet);
2089 nn_dp_warn(&r_vec->nfp_net->dp,
2090 "control message budget exceeded!\n");
2094 /* Setup and Configuration
2098 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
2099 * @nn: NFP Network structure
2101 static void nfp_net_vecs_init(struct nfp_net *nn)
2103 struct nfp_net_r_vector *r_vec;
2106 nn->lsc_handler = nfp_net_irq_lsc;
2107 nn->exn_handler = nfp_net_irq_exn;
2109 for (r = 0; r < nn->max_r_vecs; r++) {
2110 struct msix_entry *entry;
2112 entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
2114 r_vec = &nn->r_vecs[r];
2115 r_vec->nfp_net = nn;
2116 r_vec->irq_entry = entry->entry;
2117 r_vec->irq_vector = entry->vector;
2119 if (nn->dp.netdev) {
2120 r_vec->handler = nfp_net_irq_rxtx;
2122 r_vec->handler = nfp_ctrl_irq_rxtx;
2124 __skb_queue_head_init(&r_vec->queue);
2125 spin_lock_init(&r_vec->lock);
2126 tasklet_init(&r_vec->tasklet, nfp_ctrl_poll,
2127 (unsigned long)r_vec);
2128 tasklet_disable(&r_vec->tasklet);
2131 cpumask_set_cpu(r, &r_vec->affinity_mask);
2136 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
2137 * @tx_ring: TX ring to free
2139 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
2141 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2142 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2144 kvfree(tx_ring->txbufs);
2147 dma_free_coherent(dp->dev, tx_ring->size,
2148 tx_ring->txds, tx_ring->dma);
2151 tx_ring->txbufs = NULL;
2152 tx_ring->txds = NULL;
2158 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
2159 * @dp: NFP Net data path struct
2160 * @tx_ring: TX Ring structure to allocate
2162 * Return: 0 on success, negative errno otherwise.
2165 nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
2167 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2169 tx_ring->cnt = dp->txd_cnt;
2171 tx_ring->size = array_size(tx_ring->cnt, sizeof(*tx_ring->txds));
2172 tx_ring->txds = dma_alloc_coherent(dp->dev, tx_ring->size,
2174 GFP_KERNEL | __GFP_NOWARN);
2175 if (!tx_ring->txds) {
2176 netdev_warn(dp->netdev, "failed to allocate TX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2181 tx_ring->txbufs = kvcalloc(tx_ring->cnt, sizeof(*tx_ring->txbufs),
2183 if (!tx_ring->txbufs)
2186 if (!tx_ring->is_xdp && dp->netdev)
2187 netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
2193 nfp_net_tx_ring_free(tx_ring);
2198 nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
2199 struct nfp_net_tx_ring *tx_ring)
2203 if (!tx_ring->is_xdp)
2206 for (i = 0; i < tx_ring->cnt; i++) {
2207 if (!tx_ring->txbufs[i].frag)
2210 nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
2211 __free_page(virt_to_page(tx_ring->txbufs[i].frag));
2216 nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
2217 struct nfp_net_tx_ring *tx_ring)
2219 struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
2222 if (!tx_ring->is_xdp)
2225 for (i = 0; i < tx_ring->cnt; i++) {
2226 txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
2227 if (!txbufs[i].frag) {
2228 nfp_net_tx_ring_bufs_free(dp, tx_ring);
2236 static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2240 dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
2245 for (r = 0; r < dp->num_tx_rings; r++) {
2248 if (r >= dp->num_stack_tx_rings)
2249 bias = dp->num_stack_tx_rings;
2251 nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
2254 if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
2257 if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
2265 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2267 nfp_net_tx_ring_free(&dp->tx_rings[r]);
2269 kfree(dp->tx_rings);
2273 static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
2277 for (r = 0; r < dp->num_tx_rings; r++) {
2278 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2279 nfp_net_tx_ring_free(&dp->tx_rings[r]);
2282 kfree(dp->tx_rings);
2286 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
2287 * @rx_ring: RX ring to free
2289 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
2291 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
2292 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2295 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
2296 kvfree(rx_ring->rxbufs);
2299 dma_free_coherent(dp->dev, rx_ring->size,
2300 rx_ring->rxds, rx_ring->dma);
2303 rx_ring->rxbufs = NULL;
2304 rx_ring->rxds = NULL;
2310 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
2311 * @dp: NFP Net data path struct
2312 * @rx_ring: RX ring to allocate
2314 * Return: 0 on success, negative errno otherwise.
2317 nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
2322 err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, dp->netdev,
2328 rx_ring->cnt = dp->rxd_cnt;
2329 rx_ring->size = array_size(rx_ring->cnt, sizeof(*rx_ring->rxds));
2330 rx_ring->rxds = dma_alloc_coherent(dp->dev, rx_ring->size,
2332 GFP_KERNEL | __GFP_NOWARN);
2333 if (!rx_ring->rxds) {
2334 netdev_warn(dp->netdev, "failed to allocate RX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2339 rx_ring->rxbufs = kvcalloc(rx_ring->cnt, sizeof(*rx_ring->rxbufs),
2341 if (!rx_ring->rxbufs)
2347 nfp_net_rx_ring_free(rx_ring);
2351 static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2355 dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
2360 for (r = 0; r < dp->num_rx_rings; r++) {
2361 nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
2363 if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
2366 if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
2374 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2376 nfp_net_rx_ring_free(&dp->rx_rings[r]);
2378 kfree(dp->rx_rings);
2382 static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
2386 for (r = 0; r < dp->num_rx_rings; r++) {
2387 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2388 nfp_net_rx_ring_free(&dp->rx_rings[r]);
2391 kfree(dp->rx_rings);
2395 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
2396 struct nfp_net_r_vector *r_vec, int idx)
2398 r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
2400 idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
2402 r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
2403 &dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
2407 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
2414 netif_napi_add(nn->dp.netdev, &r_vec->napi,
2415 nfp_net_poll, NAPI_POLL_WEIGHT);
2417 tasklet_enable(&r_vec->tasklet);
2419 snprintf(r_vec->name, sizeof(r_vec->name),
2420 "%s-rxtx-%d", nfp_net_name(nn), idx);
2421 err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
2425 netif_napi_del(&r_vec->napi);
2427 tasklet_disable(&r_vec->tasklet);
2429 nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
2432 disable_irq(r_vec->irq_vector);
2434 irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
2436 nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
2443 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
2445 irq_set_affinity_hint(r_vec->irq_vector, NULL);
2447 netif_napi_del(&r_vec->napi);
2449 tasklet_disable(&r_vec->tasklet);
2451 free_irq(r_vec->irq_vector, r_vec);
2455 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
2456 * @nn: NFP Net device to reconfigure
2458 void nfp_net_rss_write_itbl(struct nfp_net *nn)
2462 for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
2463 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
2464 get_unaligned_le32(nn->rss_itbl + i));
2468 * nfp_net_rss_write_key() - Write RSS hash key to device
2469 * @nn: NFP Net device to reconfigure
2471 void nfp_net_rss_write_key(struct nfp_net *nn)
2475 for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2476 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
2477 get_unaligned_le32(nn->rss_key + i));
2481 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2482 * @nn: NFP Net device to reconfigure
2484 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
2490 /* Compute factor used to convert coalesce '_usecs' parameters to
2491 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
2494 factor = nn->tlv_caps.me_freq_mhz / 16;
2496 /* copy RX interrupt coalesce parameters */
2497 value = (nn->rx_coalesce_max_frames << 16) |
2498 (factor * nn->rx_coalesce_usecs);
2499 for (i = 0; i < nn->dp.num_rx_rings; i++)
2500 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
2502 /* copy TX interrupt coalesce parameters */
2503 value = (nn->tx_coalesce_max_frames << 16) |
2504 (factor * nn->tx_coalesce_usecs);
2505 for (i = 0; i < nn->dp.num_tx_rings; i++)
2506 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
2510 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2511 * @nn: NFP Net device to reconfigure
2512 * @addr: MAC address to write
2514 * Writes the MAC address from the netdev to the device control BAR. Does not
2515 * perform the required reconfig. We do a bit of byte swapping dance because
2518 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
2520 nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
2521 nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
2524 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
2526 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
2527 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
2528 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
2530 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
2531 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
2532 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
2536 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2537 * @nn: NFP Net device to reconfigure
2539 * Warning: must be fully idempotent.
2541 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
2543 u32 new_ctrl, update;
2547 new_ctrl = nn->dp.ctrl;
2548 new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
2549 update = NFP_NET_CFG_UPDATE_GEN;
2550 update |= NFP_NET_CFG_UPDATE_MSIX;
2551 update |= NFP_NET_CFG_UPDATE_RING;
2553 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2554 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
2556 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2557 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2559 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2560 err = nfp_net_reconfig(nn, update);
2562 nn_err(nn, "Could not disable device: %d\n", err);
2564 for (r = 0; r < nn->dp.num_rx_rings; r++)
2565 nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
2566 for (r = 0; r < nn->dp.num_tx_rings; r++)
2567 nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
2568 for (r = 0; r < nn->dp.num_r_vecs; r++)
2569 nfp_net_vec_clear_ring_data(nn, r);
2571 nn->dp.ctrl = new_ctrl;
2575 nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
2576 struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2578 /* Write the DMA address, size and MSI-X info to the device */
2579 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
2580 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2581 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2585 nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
2586 struct nfp_net_tx_ring *tx_ring, unsigned int idx)
2588 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
2589 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2590 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2594 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2595 * @nn: NFP Net device to reconfigure
2597 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2599 u32 bufsz, new_ctrl, update = 0;
2603 new_ctrl = nn->dp.ctrl;
2605 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
2606 nfp_net_rss_write_key(nn);
2607 nfp_net_rss_write_itbl(nn);
2608 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
2609 update |= NFP_NET_CFG_UPDATE_RSS;
2612 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
2613 nfp_net_coalesce_write_cfg(nn);
2614 update |= NFP_NET_CFG_UPDATE_IRQMOD;
2617 for (r = 0; r < nn->dp.num_tx_rings; r++)
2618 nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
2619 for (r = 0; r < nn->dp.num_rx_rings; r++)
2620 nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2622 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
2623 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2625 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
2626 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2629 nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2631 nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
2633 bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
2634 nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
2637 new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2638 update |= NFP_NET_CFG_UPDATE_GEN;
2639 update |= NFP_NET_CFG_UPDATE_MSIX;
2640 update |= NFP_NET_CFG_UPDATE_RING;
2641 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2642 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2644 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2645 err = nfp_net_reconfig(nn, update);
2647 nfp_net_clear_config_and_disable(nn);
2651 nn->dp.ctrl = new_ctrl;
2653 for (r = 0; r < nn->dp.num_rx_rings; r++)
2654 nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2656 /* Since reconfiguration requests while NFP is down are ignored we
2657 * have to wipe the entire VXLAN configuration and reinitialize it.
2659 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2660 memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2661 memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2662 udp_tunnel_get_rx_info(nn->dp.netdev);
2669 * nfp_net_close_stack() - Quiesce the stack (part of close)
2670 * @nn: NFP Net device to reconfigure
2672 static void nfp_net_close_stack(struct nfp_net *nn)
2676 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2677 netif_carrier_off(nn->dp.netdev);
2678 nn->link_up = false;
2680 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2681 disable_irq(nn->r_vecs[r].irq_vector);
2682 napi_disable(&nn->r_vecs[r].napi);
2685 netif_tx_disable(nn->dp.netdev);
2689 * nfp_net_close_free_all() - Free all runtime resources
2690 * @nn: NFP Net device to reconfigure
2692 static void nfp_net_close_free_all(struct nfp_net *nn)
2696 nfp_net_tx_rings_free(&nn->dp);
2697 nfp_net_rx_rings_free(&nn->dp);
2699 for (r = 0; r < nn->dp.num_r_vecs; r++)
2700 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2702 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2703 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2707 * nfp_net_netdev_close() - Called when the device is downed
2708 * @netdev: netdev structure
2710 static int nfp_net_netdev_close(struct net_device *netdev)
2712 struct nfp_net *nn = netdev_priv(netdev);
2714 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2716 nfp_net_close_stack(nn);
2720 nfp_net_clear_config_and_disable(nn);
2721 nfp_port_configure(netdev, false);
2723 /* Step 3: Free resources
2725 nfp_net_close_free_all(nn);
2727 nn_dbg(nn, "%s down", netdev->name);
2731 void nfp_ctrl_close(struct nfp_net *nn)
2737 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2738 disable_irq(nn->r_vecs[r].irq_vector);
2739 tasklet_disable(&nn->r_vecs[r].tasklet);
2742 nfp_net_clear_config_and_disable(nn);
2744 nfp_net_close_free_all(nn);
2750 * nfp_net_open_stack() - Start the device from stack's perspective
2751 * @nn: NFP Net device to reconfigure
2753 static void nfp_net_open_stack(struct nfp_net *nn)
2757 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2758 napi_enable(&nn->r_vecs[r].napi);
2759 enable_irq(nn->r_vecs[r].irq_vector);
2762 netif_tx_wake_all_queues(nn->dp.netdev);
2764 enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2765 nfp_net_read_link_status(nn);
2768 static int nfp_net_open_alloc_all(struct nfp_net *nn)
2772 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2773 nn->exn_name, sizeof(nn->exn_name),
2774 NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2777 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2778 nn->lsc_name, sizeof(nn->lsc_name),
2779 NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2782 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2784 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2785 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2787 goto err_cleanup_vec_p;
2790 err = nfp_net_rx_rings_prepare(nn, &nn->dp);
2792 goto err_cleanup_vec;
2794 err = nfp_net_tx_rings_prepare(nn, &nn->dp);
2796 goto err_free_rx_rings;
2798 for (r = 0; r < nn->max_r_vecs; r++)
2799 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2804 nfp_net_rx_rings_free(&nn->dp);
2806 r = nn->dp.num_r_vecs;
2809 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2810 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2812 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2816 static int nfp_net_netdev_open(struct net_device *netdev)
2818 struct nfp_net *nn = netdev_priv(netdev);
2821 /* Step 1: Allocate resources for rings and the like
2822 * - Request interrupts
2823 * - Allocate RX and TX ring resources
2824 * - Setup initial RSS table
2826 err = nfp_net_open_alloc_all(nn);
2830 err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
2834 err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
2838 /* Step 2: Configure the NFP
2839 * - Ifup the physical interface if it exists
2840 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2841 * - Write MAC address (in case it changed)
2843 * - Set the Freelist buffer size
2846 err = nfp_port_configure(netdev, true);
2850 err = nfp_net_set_config_and_enable(nn);
2852 goto err_port_disable;
2854 /* Step 3: Enable for kernel
2855 * - put some freelist descriptors on each RX ring
2856 * - enable NAPI on each ring
2857 * - enable all TX queues
2860 nfp_net_open_stack(nn);
2865 nfp_port_configure(netdev, false);
2867 nfp_net_close_free_all(nn);
2871 int nfp_ctrl_open(struct nfp_net *nn)
2875 /* ring dumping depends on vNICs being opened/closed under rtnl */
2878 err = nfp_net_open_alloc_all(nn);
2882 err = nfp_net_set_config_and_enable(nn);
2886 for (r = 0; r < nn->dp.num_r_vecs; r++)
2887 enable_irq(nn->r_vecs[r].irq_vector);
2894 nfp_net_close_free_all(nn);
2900 static void nfp_net_set_rx_mode(struct net_device *netdev)
2902 struct nfp_net *nn = netdev_priv(netdev);
2905 new_ctrl = nn->dp.ctrl;
2907 if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
2908 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
2910 new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
2912 if (netdev->flags & IFF_PROMISC) {
2913 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2914 new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2916 nn_warn(nn, "FW does not support promiscuous mode\n");
2918 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2921 if (new_ctrl == nn->dp.ctrl)
2924 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2925 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2927 nn->dp.ctrl = new_ctrl;
2930 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
2934 for (i = 0; i < sizeof(nn->rss_itbl); i++)
2936 ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
2939 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
2941 struct nfp_net_dp new_dp = *dp;
2946 nn->dp.netdev->mtu = new_dp.mtu;
2948 if (!netif_is_rxfh_configured(nn->dp.netdev))
2949 nfp_net_rss_init_itbl(nn);
2952 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
2957 nfp_net_dp_swap(nn, dp);
2959 for (r = 0; r < nn->max_r_vecs; r++)
2960 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2962 err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
2966 if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
2967 err = netif_set_real_num_tx_queues(nn->dp.netdev,
2968 nn->dp.num_stack_tx_rings);
2973 return nfp_net_set_config_and_enable(nn);
2976 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
2978 struct nfp_net_dp *new;
2980 new = kmalloc(sizeof(*new), GFP_KERNEL);
2986 /* Clear things which need to be recomputed */
2988 new->tx_rings = NULL;
2989 new->rx_rings = NULL;
2990 new->num_r_vecs = 0;
2991 new->num_stack_tx_rings = 0;
2997 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
2998 struct netlink_ext_ack *extack)
3000 /* XDP-enabled tests */
3003 if (dp->fl_bufsz > PAGE_SIZE) {
3004 NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
3007 if (dp->num_tx_rings > nn->max_tx_rings) {
3008 NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
3015 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
3016 struct netlink_ext_ack *extack)
3020 dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
3022 dp->num_stack_tx_rings = dp->num_tx_rings;
3024 dp->num_stack_tx_rings -= dp->num_rx_rings;
3026 dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
3028 err = nfp_net_check_config(nn, dp, extack);
3032 if (!netif_running(dp->netdev)) {
3033 nfp_net_dp_swap(nn, dp);
3038 /* Prepare new rings */
3039 for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
3040 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
3043 goto err_cleanup_vecs;
3047 err = nfp_net_rx_rings_prepare(nn, dp);
3049 goto err_cleanup_vecs;
3051 err = nfp_net_tx_rings_prepare(nn, dp);
3055 /* Stop device, swap in new rings, try to start the firmware */
3056 nfp_net_close_stack(nn);
3057 nfp_net_clear_config_and_disable(nn);
3059 err = nfp_net_dp_swap_enable(nn, dp);
3063 nfp_net_clear_config_and_disable(nn);
3065 /* Try with old configuration and old rings */
3066 err2 = nfp_net_dp_swap_enable(nn, dp);
3068 nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
3071 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3072 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3074 nfp_net_rx_rings_free(dp);
3075 nfp_net_tx_rings_free(dp);
3077 nfp_net_open_stack(nn);
3084 nfp_net_rx_rings_free(dp);
3086 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3087 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3092 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
3094 struct nfp_net *nn = netdev_priv(netdev);
3095 struct nfp_net_dp *dp;
3098 err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
3102 dp = nfp_net_clone_dp(nn);
3108 return nfp_net_ring_reconfig(nn, dp, NULL);
3112 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3114 struct nfp_net *nn = netdev_priv(netdev);
3116 /* Priority tagged packets with vlan id 0 are processed by the
3117 * NFP as untagged packets
3122 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3123 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3126 return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD);
3130 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3132 struct nfp_net *nn = netdev_priv(netdev);
3134 /* Priority tagged packets with vlan id 0 are processed by the
3135 * NFP as untagged packets
3140 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3141 nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3144 return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL);
3147 static void nfp_net_stat64(struct net_device *netdev,
3148 struct rtnl_link_stats64 *stats)
3150 struct nfp_net *nn = netdev_priv(netdev);
3153 /* Collect software stats */
3154 for (r = 0; r < nn->max_r_vecs; r++) {
3155 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
3160 start = u64_stats_fetch_begin(&r_vec->rx_sync);
3161 data[0] = r_vec->rx_pkts;
3162 data[1] = r_vec->rx_bytes;
3163 data[2] = r_vec->rx_drops;
3164 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
3165 stats->rx_packets += data[0];
3166 stats->rx_bytes += data[1];
3167 stats->rx_dropped += data[2];
3170 start = u64_stats_fetch_begin(&r_vec->tx_sync);
3171 data[0] = r_vec->tx_pkts;
3172 data[1] = r_vec->tx_bytes;
3173 data[2] = r_vec->tx_errors;
3174 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
3175 stats->tx_packets += data[0];
3176 stats->tx_bytes += data[1];
3177 stats->tx_errors += data[2];
3180 /* Add in device stats */
3181 stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
3182 stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
3183 stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
3185 stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
3186 stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
3189 static int nfp_net_set_features(struct net_device *netdev,
3190 netdev_features_t features)
3192 netdev_features_t changed = netdev->features ^ features;
3193 struct nfp_net *nn = netdev_priv(netdev);
3197 /* Assume this is not called with features we have not advertised */
3199 new_ctrl = nn->dp.ctrl;
3201 if (changed & NETIF_F_RXCSUM) {
3202 if (features & NETIF_F_RXCSUM)
3203 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3205 new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
3208 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3209 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
3210 new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3212 new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
3215 if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
3216 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
3217 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3218 NFP_NET_CFG_CTRL_LSO;
3220 new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3223 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3224 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3225 new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3227 new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
3230 if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
3231 if (features & NETIF_F_HW_VLAN_CTAG_TX)
3232 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3234 new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
3237 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
3238 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
3239 new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3241 new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
3244 if (changed & NETIF_F_SG) {
3245 if (features & NETIF_F_SG)
3246 new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
3248 new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
3251 err = nfp_port_set_features(netdev, features);
3255 nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
3256 netdev->features, features, changed);
3258 if (new_ctrl == nn->dp.ctrl)
3261 nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
3262 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
3263 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
3267 nn->dp.ctrl = new_ctrl;
3272 static netdev_features_t
3273 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
3274 netdev_features_t features)
3278 /* We can't do TSO over double tagged packets (802.1AD) */
3279 features &= vlan_features_check(skb, features);
3281 if (!skb->encapsulation)
3284 /* Ensure that inner L4 header offset fits into TX descriptor field */
3285 if (skb_is_gso(skb)) {
3288 hdrlen = skb_inner_transport_header(skb) - skb->data +
3289 inner_tcp_hdrlen(skb);
3291 /* Assume worst case scenario of having longest possible
3292 * metadata prepend - 8B
3294 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
3295 features &= ~NETIF_F_GSO_MASK;
3298 /* VXLAN/GRE check */
3299 switch (vlan_get_protocol(skb)) {
3300 case htons(ETH_P_IP):
3301 l4_hdr = ip_hdr(skb)->protocol;
3303 case htons(ETH_P_IPV6):
3304 l4_hdr = ipv6_hdr(skb)->nexthdr;
3307 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3310 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
3311 skb->inner_protocol != htons(ETH_P_TEB) ||
3312 (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
3313 (l4_hdr == IPPROTO_UDP &&
3314 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
3315 sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
3316 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3322 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
3324 struct nfp_net *nn = netdev_priv(netdev);
3328 return nfp_port_get_phys_port_name(netdev, name, len);
3330 if (nn->dp.is_vf || nn->vnic_no_name)
3333 n = snprintf(name, len, "n%d", nn->id);
3341 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
3342 * @nn: NFP Net device to reconfigure
3343 * @idx: Index into the port table where new port should be written
3344 * @port: UDP port to configure (pass zero to remove VXLAN port)
3346 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
3350 nn->vxlan_ports[idx] = port;
3352 if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
3355 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
3356 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
3357 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
3358 be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
3359 be16_to_cpu(nn->vxlan_ports[i]));
3361 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
3365 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
3366 * @nn: NFP Network structure
3367 * @port: UDP port to look for
3369 * Return: if the port is already in the table -- it's position;
3370 * if the port is not in the table -- free position to use;
3371 * if the table is full -- -ENOSPC.
3373 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
3375 int i, free_idx = -ENOSPC;
3377 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
3378 if (nn->vxlan_ports[i] == port)
3380 if (!nn->vxlan_usecnt[i])
3387 static void nfp_net_add_vxlan_port(struct net_device *netdev,
3388 struct udp_tunnel_info *ti)
3390 struct nfp_net *nn = netdev_priv(netdev);
3393 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3396 idx = nfp_net_find_vxlan_idx(nn, ti->port);
3400 if (!nn->vxlan_usecnt[idx]++)
3401 nfp_net_set_vxlan_port(nn, idx, ti->port);
3404 static void nfp_net_del_vxlan_port(struct net_device *netdev,
3405 struct udp_tunnel_info *ti)
3407 struct nfp_net *nn = netdev_priv(netdev);
3410 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3413 idx = nfp_net_find_vxlan_idx(nn, ti->port);
3414 if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
3417 if (!--nn->vxlan_usecnt[idx])
3418 nfp_net_set_vxlan_port(nn, idx, 0);
3421 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
3423 struct bpf_prog *prog = bpf->prog;
3424 struct nfp_net_dp *dp;
3427 if (!xdp_attachment_flags_ok(&nn->xdp, bpf))
3430 if (!prog == !nn->dp.xdp_prog) {
3431 WRITE_ONCE(nn->dp.xdp_prog, prog);
3432 xdp_attachment_setup(&nn->xdp, bpf);
3436 dp = nfp_net_clone_dp(nn);
3440 dp->xdp_prog = prog;
3441 dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
3442 dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
3443 dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
3445 /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
3446 err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
3450 xdp_attachment_setup(&nn->xdp, bpf);
3454 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
3458 if (!xdp_attachment_flags_ok(&nn->xdp_hw, bpf))
3461 err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
3465 xdp_attachment_setup(&nn->xdp_hw, bpf);
3469 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
3471 struct nfp_net *nn = netdev_priv(netdev);
3473 switch (xdp->command) {
3474 case XDP_SETUP_PROG:
3475 return nfp_net_xdp_setup_drv(nn, xdp);
3476 case XDP_SETUP_PROG_HW:
3477 return nfp_net_xdp_setup_hw(nn, xdp);
3478 case XDP_QUERY_PROG:
3479 return xdp_attachment_query(&nn->xdp, xdp);
3480 case XDP_QUERY_PROG_HW:
3481 return xdp_attachment_query(&nn->xdp_hw, xdp);
3483 return nfp_app_bpf(nn->app, nn, xdp);
3487 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
3489 struct nfp_net *nn = netdev_priv(netdev);
3490 struct sockaddr *saddr = addr;
3493 err = eth_prepare_mac_addr_change(netdev, addr);
3497 nfp_net_write_mac_addr(nn, saddr->sa_data);
3499 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
3503 eth_commit_mac_addr_change(netdev, addr);
3508 const struct net_device_ops nfp_net_netdev_ops = {
3509 .ndo_init = nfp_app_ndo_init,
3510 .ndo_uninit = nfp_app_ndo_uninit,
3511 .ndo_open = nfp_net_netdev_open,
3512 .ndo_stop = nfp_net_netdev_close,
3513 .ndo_start_xmit = nfp_net_tx,
3514 .ndo_get_stats64 = nfp_net_stat64,
3515 .ndo_vlan_rx_add_vid = nfp_net_vlan_rx_add_vid,
3516 .ndo_vlan_rx_kill_vid = nfp_net_vlan_rx_kill_vid,
3517 .ndo_set_vf_mac = nfp_app_set_vf_mac,
3518 .ndo_set_vf_vlan = nfp_app_set_vf_vlan,
3519 .ndo_set_vf_spoofchk = nfp_app_set_vf_spoofchk,
3520 .ndo_get_vf_config = nfp_app_get_vf_config,
3521 .ndo_set_vf_link_state = nfp_app_set_vf_link_state,
3522 .ndo_setup_tc = nfp_port_setup_tc,
3523 .ndo_tx_timeout = nfp_net_tx_timeout,
3524 .ndo_set_rx_mode = nfp_net_set_rx_mode,
3525 .ndo_change_mtu = nfp_net_change_mtu,
3526 .ndo_set_mac_address = nfp_net_set_mac_address,
3527 .ndo_set_features = nfp_net_set_features,
3528 .ndo_features_check = nfp_net_features_check,
3529 .ndo_get_phys_port_name = nfp_net_get_phys_port_name,
3530 .ndo_udp_tunnel_add = nfp_net_add_vxlan_port,
3531 .ndo_udp_tunnel_del = nfp_net_del_vxlan_port,
3532 .ndo_bpf = nfp_net_xdp,
3533 .ndo_get_port_parent_id = nfp_port_get_port_parent_id,
3534 .ndo_get_devlink = nfp_devlink_get_devlink,
3538 * nfp_net_info() - Print general info about the NIC
3539 * @nn: NFP Net device to reconfigure
3541 void nfp_net_info(struct nfp_net *nn)
3543 nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3544 nn->dp.is_vf ? "VF " : "",
3545 nn->dp.num_tx_rings, nn->max_tx_rings,
3546 nn->dp.num_rx_rings, nn->max_rx_rings);
3547 nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
3548 nn->fw_ver.resv, nn->fw_ver.class,
3549 nn->fw_ver.major, nn->fw_ver.minor,
3551 nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
3553 nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
3554 nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
3555 nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
3556 nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
3557 nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
3558 nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
3559 nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
3560 nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
3561 nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
3562 nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO1 " : "",
3563 nn->cap & NFP_NET_CFG_CTRL_LSO2 ? "TSO2 " : "",
3564 nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS1 " : "",
3565 nn->cap & NFP_NET_CFG_CTRL_RSS2 ? "RSS2 " : "",
3566 nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
3567 nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
3568 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
3569 nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
3570 nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
3571 nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
3572 nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
3573 "RXCSUM_COMPLETE " : "",
3574 nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
3575 nfp_app_extra_cap(nn->app, nn));
3579 * nfp_net_alloc() - Allocate netdev and related structure
3581 * @ctrl_bar: PCI IOMEM with vNIC config memory
3582 * @needs_netdev: Whether to allocate a netdev for this vNIC
3583 * @max_tx_rings: Maximum number of TX rings supported by device
3584 * @max_rx_rings: Maximum number of RX rings supported by device
3586 * This function allocates a netdev device and fills in the initial
3587 * part of the @struct nfp_net structure. In case of control device
3588 * nfp_net structure is allocated without the netdev.
3590 * Return: NFP Net device structure, or ERR_PTR on error.
3593 nfp_net_alloc(struct pci_dev *pdev, void __iomem *ctrl_bar, bool needs_netdev,
3594 unsigned int max_tx_rings, unsigned int max_rx_rings)
3600 struct net_device *netdev;
3602 netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
3603 max_tx_rings, max_rx_rings);
3605 return ERR_PTR(-ENOMEM);
3607 SET_NETDEV_DEV(netdev, &pdev->dev);
3608 nn = netdev_priv(netdev);
3609 nn->dp.netdev = netdev;
3611 nn = vzalloc(sizeof(*nn));
3613 return ERR_PTR(-ENOMEM);
3616 nn->dp.dev = &pdev->dev;
3617 nn->dp.ctrl_bar = ctrl_bar;
3620 nn->max_tx_rings = max_tx_rings;
3621 nn->max_rx_rings = max_rx_rings;
3623 nn->dp.num_tx_rings = min_t(unsigned int,
3624 max_tx_rings, num_online_cpus());
3625 nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3626 netif_get_num_default_rss_queues());
3628 nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
3629 nn->dp.num_r_vecs = min_t(unsigned int,
3630 nn->dp.num_r_vecs, num_online_cpus());
3632 nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
3633 nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3635 spin_lock_init(&nn->reconfig_lock);
3636 spin_lock_init(&nn->link_status_lock);
3638 timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
3640 err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
3649 free_netdev(nn->dp.netdev);
3652 return ERR_PTR(err);
3656 * nfp_net_free() - Undo what @nfp_net_alloc() did
3657 * @nn: NFP Net device to reconfigure
3659 void nfp_net_free(struct nfp_net *nn)
3661 WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
3663 free_netdev(nn->dp.netdev);
3669 * nfp_net_rss_key_sz() - Get current size of the RSS key
3670 * @nn: NFP Net device instance
3672 * Return: size of the RSS key for currently selected hash function.
3674 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
3676 switch (nn->rss_hfunc) {
3677 case ETH_RSS_HASH_TOP:
3678 return NFP_NET_CFG_RSS_KEY_SZ;
3679 case ETH_RSS_HASH_XOR:
3681 case ETH_RSS_HASH_CRC32:
3685 nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
3690 * nfp_net_rss_init() - Set the initial RSS parameters
3691 * @nn: NFP Net device to reconfigure
3693 static void nfp_net_rss_init(struct nfp_net *nn)
3695 unsigned long func_bit, rss_cap_hfunc;
3698 /* Read the RSS function capability and select first supported func */
3699 reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
3700 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
3702 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
3703 NFP_NET_CFG_RSS_TOEPLITZ);
3705 func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
3706 if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3707 dev_warn(nn->dp.dev,
3708 "Bad RSS config, defaulting to Toeplitz hash\n");
3709 func_bit = ETH_RSS_HASH_TOP_BIT;
3711 nn->rss_hfunc = 1 << func_bit;
3713 netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3715 nfp_net_rss_init_itbl(nn);
3717 /* Enable IPv4/IPv6 TCP by default */
3718 nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
3719 NFP_NET_CFG_RSS_IPV6_TCP |
3720 FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3721 NFP_NET_CFG_RSS_MASK;
3725 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3726 * @nn: NFP Net device to reconfigure
3728 static void nfp_net_irqmod_init(struct nfp_net *nn)
3730 nn->rx_coalesce_usecs = 50;
3731 nn->rx_coalesce_max_frames = 64;
3732 nn->tx_coalesce_usecs = 50;
3733 nn->tx_coalesce_max_frames = 64;
3736 static void nfp_net_netdev_init(struct nfp_net *nn)
3738 struct net_device *netdev = nn->dp.netdev;
3740 nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
3742 netdev->mtu = nn->dp.mtu;
3744 /* Advertise/enable offloads based on capabilities
3746 * Note: netdev->features show the currently enabled features
3747 * and netdev->hw_features advertises which features are
3748 * supported. By default we enable most features.
3750 if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
3751 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3753 netdev->hw_features = NETIF_F_HIGHDMA;
3754 if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
3755 netdev->hw_features |= NETIF_F_RXCSUM;
3756 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3758 if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
3759 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3760 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3762 if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
3763 netdev->hw_features |= NETIF_F_SG;
3764 nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
3766 if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
3767 nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3768 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3769 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3770 NFP_NET_CFG_CTRL_LSO;
3772 if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
3773 netdev->hw_features |= NETIF_F_RXHASH;
3774 if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
3775 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3776 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
3777 nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
3779 if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
3780 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3781 netdev->hw_features |= NETIF_F_GSO_GRE;
3782 nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
3784 if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
3785 netdev->hw_enc_features = netdev->hw_features;
3787 netdev->vlan_features = netdev->hw_features;
3789 if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
3790 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3791 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3793 if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
3794 if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3795 nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
3797 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3798 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3801 if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
3802 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3803 nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3806 netdev->features = netdev->hw_features;
3808 if (nfp_app_has_tc(nn->app) && nn->port)
3809 netdev->hw_features |= NETIF_F_HW_TC;
3811 /* Advertise but disable TSO by default. */
3812 netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3813 nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3815 /* Finalise the netdev setup */
3816 netdev->netdev_ops = &nfp_net_netdev_ops;
3817 netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
3819 /* MTU range: 68 - hw-specific max */
3820 netdev->min_mtu = ETH_MIN_MTU;
3821 netdev->max_mtu = nn->max_mtu;
3823 netdev->gso_max_segs = NFP_NET_LSO_MAX_SEGS;
3825 netif_carrier_off(netdev);
3827 nfp_net_set_ethtool_ops(netdev);
3830 static int nfp_net_read_caps(struct nfp_net *nn)
3832 /* Get some of the read-only fields from the BAR */
3833 nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
3834 nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
3836 /* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
3837 * we allow use of non-chained metadata if RSS(v1) is the only
3838 * advertised capability requiring metadata.
3840 nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
3842 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
3843 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
3844 /* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
3845 * it has the same meaning as RSSv2.
3847 if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
3848 nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
3850 /* Determine RX packet/metadata boundary offset */
3851 if (nn->fw_ver.major >= 2) {
3854 reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
3855 if (reg > NFP_NET_MAX_PREPEND) {
3856 nn_err(nn, "Invalid rx offset: %d\n", reg);
3859 nn->dp.rx_offset = reg;
3861 nn->dp.rx_offset = NFP_NET_RX_OFFSET;
3864 /* For control vNICs mask out the capabilities app doesn't want. */
3866 nn->cap &= nn->app->type->ctrl_cap_mask;
3872 * nfp_net_init() - Initialise/finalise the nfp_net structure
3873 * @nn: NFP Net device structure
3875 * Return: 0 on success or negative errno on error.
3877 int nfp_net_init(struct nfp_net *nn)
3881 nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
3883 err = nfp_net_read_caps(nn);
3887 /* Set default MTU and Freelist buffer size */
3888 if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
3889 if (nn->app->ctrl_mtu <= nn->max_mtu) {
3890 nn->dp.mtu = nn->app->ctrl_mtu;
3892 if (nn->app->ctrl_mtu != NFP_APP_CTRL_MTU_MAX)
3893 nn_warn(nn, "app requested MTU above max supported %u > %u\n",
3894 nn->app->ctrl_mtu, nn->max_mtu);
3895 nn->dp.mtu = nn->max_mtu;
3897 } else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
3898 nn->dp.mtu = nn->max_mtu;
3900 nn->dp.mtu = NFP_NET_DEFAULT_MTU;
3902 nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
3904 if (nfp_app_ctrl_uses_data_vnics(nn->app))
3905 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
3907 if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
3908 nfp_net_rss_init(nn);
3909 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
3910 NFP_NET_CFG_CTRL_RSS;
3913 /* Allow L2 Broadcast and Multicast through by default, if supported */
3914 if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
3915 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
3917 /* Allow IRQ moderation, if supported */
3918 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
3919 nfp_net_irqmod_init(nn);
3920 nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
3924 nfp_net_netdev_init(nn);
3926 /* Stash the re-configuration queue away. First odd queue in TX Bar */
3927 nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
3929 /* Make sure the FW knows the netdev is supposed to be disabled here */
3930 nn_writel(nn, NFP_NET_CFG_CTRL, 0);
3931 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
3932 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
3933 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
3934 NFP_NET_CFG_UPDATE_GEN);
3938 nfp_net_vecs_init(nn);
3942 return register_netdev(nn->dp.netdev);
3946 * nfp_net_clean() - Undo what nfp_net_init() did.
3947 * @nn: NFP Net device structure
3949 void nfp_net_clean(struct nfp_net *nn)
3954 unregister_netdev(nn->dp.netdev);
3955 nfp_net_reconfig_wait_posted(nn);