nfp: add VLAN filtering support
[linux-2.6/btrfs-unstable.git] / drivers / net / ethernet / netronome / nfp / nfp_net_common.c
blob2bdddd1ae6661a1f764ee74c937700782d519bb2
1 /*
2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
35 * nfp_net_common.c
36 * Netronome network device driver: Common functions between PF and VF
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
40 * Brad Petrus <brad.petrus@netronome.com>
41 * Chris Telfer <chris.telfer@netronome.com>
44 #include <linux/bitfield.h>
45 #include <linux/bpf.h>
46 #include <linux/bpf_trace.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
50 #include <linux/fs.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/interrupt.h>
54 #include <linux/ip.h>
55 #include <linux/ipv6.h>
56 #include <linux/page_ref.h>
57 #include <linux/pci.h>
58 #include <linux/pci_regs.h>
59 #include <linux/msi.h>
60 #include <linux/ethtool.h>
61 #include <linux/log2.h>
62 #include <linux/if_vlan.h>
63 #include <linux/random.h>
64 #include <linux/vmalloc.h>
65 #include <linux/ktime.h>
67 #include <net/vxlan.h>
69 #include "nfpcore/nfp_nsp.h"
70 #include "nfp_app.h"
71 #include "nfp_net_ctrl.h"
72 #include "nfp_net.h"
73 #include "nfp_port.h"
75 /**
76 * nfp_net_get_fw_version() - Read and parse the FW version
77 * @fw_ver: Output fw_version structure to read to
78 * @ctrl_bar: Mapped address of the control BAR
80 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
81 void __iomem *ctrl_bar)
83 u32 reg;
85 reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
86 put_unaligned_le32(reg, fw_ver);
89 static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
91 return dma_map_single_attrs(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
92 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
93 dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
96 static void
97 nfp_net_dma_sync_dev_rx(const struct nfp_net_dp *dp, dma_addr_t dma_addr)
99 dma_sync_single_for_device(dp->dev, dma_addr,
100 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
101 dp->rx_dma_dir);
104 static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
106 dma_unmap_single_attrs(dp->dev, dma_addr,
107 dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
108 dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
111 static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr,
112 unsigned int len)
114 dma_sync_single_for_cpu(dp->dev, dma_addr - NFP_NET_RX_BUF_HEADROOM,
115 len, dp->rx_dma_dir);
118 /* Firmware reconfig
120 * Firmware reconfig may take a while so we have two versions of it -
121 * synchronous and asynchronous (posted). All synchronous callers are holding
122 * RTNL so we don't have to worry about serializing them.
124 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
126 nn_writel(nn, NFP_NET_CFG_UPDATE, update);
127 /* ensure update is written before pinging HW */
128 nn_pci_flush(nn);
129 nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
132 /* Pass 0 as update to run posted reconfigs. */
133 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
135 update |= nn->reconfig_posted;
136 nn->reconfig_posted = 0;
138 nfp_net_reconfig_start(nn, update);
140 nn->reconfig_timer_active = true;
141 mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
144 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
146 u32 reg;
148 reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
149 if (reg == 0)
150 return true;
151 if (reg & NFP_NET_CFG_UPDATE_ERR) {
152 nn_err(nn, "Reconfig error: 0x%08x\n", reg);
153 return true;
154 } else if (last_check) {
155 nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
156 return true;
159 return false;
162 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
164 bool timed_out = false;
166 /* Poll update field, waiting for NFP to ack the config */
167 while (!nfp_net_reconfig_check_done(nn, timed_out)) {
168 msleep(1);
169 timed_out = time_is_before_eq_jiffies(deadline);
172 if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
173 return -EIO;
175 return timed_out ? -EIO : 0;
178 static void nfp_net_reconfig_timer(unsigned long data)
180 struct nfp_net *nn = (void *)data;
182 spin_lock_bh(&nn->reconfig_lock);
184 nn->reconfig_timer_active = false;
186 /* If sync caller is present it will take over from us */
187 if (nn->reconfig_sync_present)
188 goto done;
190 /* Read reconfig status and report errors */
191 nfp_net_reconfig_check_done(nn, true);
193 if (nn->reconfig_posted)
194 nfp_net_reconfig_start_async(nn, 0);
195 done:
196 spin_unlock_bh(&nn->reconfig_lock);
200 * nfp_net_reconfig_post() - Post async reconfig request
201 * @nn: NFP Net device to reconfigure
202 * @update: The value for the update field in the BAR config
204 * Record FW reconfiguration request. Reconfiguration will be kicked off
205 * whenever reconfiguration machinery is idle. Multiple requests can be
206 * merged together!
208 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
210 spin_lock_bh(&nn->reconfig_lock);
212 /* Sync caller will kick off async reconf when it's done, just post */
213 if (nn->reconfig_sync_present) {
214 nn->reconfig_posted |= update;
215 goto done;
218 /* Opportunistically check if the previous command is done */
219 if (!nn->reconfig_timer_active ||
220 nfp_net_reconfig_check_done(nn, false))
221 nfp_net_reconfig_start_async(nn, update);
222 else
223 nn->reconfig_posted |= update;
224 done:
225 spin_unlock_bh(&nn->reconfig_lock);
229 * nfp_net_reconfig() - Reconfigure the firmware
230 * @nn: NFP Net device to reconfigure
231 * @update: The value for the update field in the BAR config
233 * Write the update word to the BAR and ping the reconfig queue. The
234 * poll until the firmware has acknowledged the update by zeroing the
235 * update word.
237 * Return: Negative errno on error, 0 on success
239 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
241 bool cancelled_timer = false;
242 u32 pre_posted_requests;
243 int ret;
245 spin_lock_bh(&nn->reconfig_lock);
247 nn->reconfig_sync_present = true;
249 if (nn->reconfig_timer_active) {
250 del_timer(&nn->reconfig_timer);
251 nn->reconfig_timer_active = false;
252 cancelled_timer = true;
254 pre_posted_requests = nn->reconfig_posted;
255 nn->reconfig_posted = 0;
257 spin_unlock_bh(&nn->reconfig_lock);
259 if (cancelled_timer)
260 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
262 /* Run the posted reconfigs which were issued before we started */
263 if (pre_posted_requests) {
264 nfp_net_reconfig_start(nn, pre_posted_requests);
265 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
268 nfp_net_reconfig_start(nn, update);
269 ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
271 spin_lock_bh(&nn->reconfig_lock);
273 if (nn->reconfig_posted)
274 nfp_net_reconfig_start_async(nn, 0);
276 nn->reconfig_sync_present = false;
278 spin_unlock_bh(&nn->reconfig_lock);
280 return ret;
284 * nfp_net_reconfig_mbox() - Reconfigure the firmware via the mailbox
285 * @nn: NFP Net device to reconfigure
286 * @mbox_cmd: The value for the mailbox command
288 * Helper function for mailbox updates
290 * Return: Negative errno on error, 0 on success
292 static int nfp_net_reconfig_mbox(struct nfp_net *nn, u32 mbox_cmd)
294 int ret;
296 nn_writeq(nn, NFP_NET_CFG_MBOX_CMD, mbox_cmd);
298 ret = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
299 if (ret) {
300 nn_err(nn, "Mailbox update error\n");
301 return ret;
304 return -nn_readl(nn, NFP_NET_CFG_MBOX_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);
320 nn_pci_flush(nn);
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.
332 unsigned int
333 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
334 unsigned int min_irqs, unsigned int wanted_irqs)
336 unsigned int i;
337 int got_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);
344 if (got_irqs < 0) {
345 dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
346 min_irqs, wanted_irqs, got_irqs);
347 return 0;
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);
354 return 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).
366 void
367 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
368 unsigned int n)
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,
381 dp->num_r_vecs);
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.
401 * @irq: Interrupt
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.
416 return IRQ_HANDLED;
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);
425 return IRQ_HANDLED;
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)
434 unsigned long flags;
435 bool link_up;
436 u32 sts;
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)
444 goto out;
446 nn->link_up = link_up;
447 if (nn->port)
448 set_bit(NFP_PORT_CHANGED, &nn->port->flags);
450 if (nn->link_up) {
451 netif_carrier_on(nn->dp.netdev);
452 netdev_info(nn->dp.netdev, "NIC Link is Up\n");
453 } else {
454 netif_carrier_off(nn->dp.netdev);
455 netdev_info(nn->dp.netdev, "NIC Link is Down\n");
457 out:
458 spin_unlock_irqrestore(&nn->link_status_lock, flags);
462 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
463 * @irq: Interrupt
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);
479 return IRQ_HANDLED;
483 * nfp_net_irq_exn() - Interrupt service routine for exceptions
484 * @irq: Interrupt
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 */
495 return IRQ_HANDLED;
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
502 * @idx: Ring index
503 * @is_xdp: Is this an XDP TX ring?
505 static void
506 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
507 struct nfp_net_r_vector *r_vec, unsigned int idx,
508 bool is_xdp)
510 struct nfp_net *nn = r_vec->nfp_net;
512 tx_ring->idx = idx;
513 tx_ring->r_vec = r_vec;
514 tx_ring->is_xdp = is_xdp;
516 tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
517 tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
521 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
522 * @rx_ring: RX ring structure
523 * @r_vec: IRQ vector servicing this ring
524 * @idx: Ring index
526 static void
527 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
528 struct nfp_net_r_vector *r_vec, unsigned int idx)
530 struct nfp_net *nn = r_vec->nfp_net;
532 rx_ring->idx = idx;
533 rx_ring->r_vec = r_vec;
535 rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
536 rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
540 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
541 * @nn: NFP Network structure
542 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
543 * @format: printf-style format to construct the interrupt name
544 * @name: Pointer to allocated space for interrupt name
545 * @name_sz: Size of space for interrupt name
546 * @vector_idx: Index of MSI-X vector used for this interrupt
547 * @handler: IRQ handler to register for this interrupt
549 static int
550 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
551 const char *format, char *name, size_t name_sz,
552 unsigned int vector_idx, irq_handler_t handler)
554 struct msix_entry *entry;
555 int err;
557 entry = &nn->irq_entries[vector_idx];
559 snprintf(name, name_sz, format, nfp_net_name(nn));
560 err = request_irq(entry->vector, handler, 0, name, nn);
561 if (err) {
562 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
563 entry->vector, err);
564 return err;
566 nn_writeb(nn, ctrl_offset, entry->entry);
568 return 0;
572 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
573 * @nn: NFP Network structure
574 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
575 * @vector_idx: Index of MSI-X vector used for this interrupt
577 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
578 unsigned int vector_idx)
580 nn_writeb(nn, ctrl_offset, 0xff);
581 free_irq(nn->irq_entries[vector_idx].vector, nn);
584 /* Transmit
586 * One queue controller peripheral queue is used for transmit. The
587 * driver en-queues packets for transmit by advancing the write
588 * pointer. The device indicates that packets have transmitted by
589 * advancing the read pointer. The driver maintains a local copy of
590 * the read and write pointer in @struct nfp_net_tx_ring. The driver
591 * keeps @wr_p in sync with the queue controller write pointer and can
592 * determine how many packets have been transmitted by comparing its
593 * copy of the read pointer @rd_p with the read pointer maintained by
594 * the queue controller peripheral.
598 * nfp_net_tx_full() - Check if the TX ring is full
599 * @tx_ring: TX ring to check
600 * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
602 * This function checks, based on the *host copy* of read/write
603 * pointer if a given TX ring is full. The real TX queue may have
604 * some newly made available slots.
606 * Return: True if the ring is full.
608 static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
610 return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
613 /* Wrappers for deciding when to stop and restart TX queues */
614 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
616 return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
619 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
621 return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
625 * nfp_net_tx_ring_stop() - stop tx ring
626 * @nd_q: netdev queue
627 * @tx_ring: driver tx queue structure
629 * Safely stop TX ring. Remember that while we are running .start_xmit()
630 * someone else may be cleaning the TX ring completions so we need to be
631 * extra careful here.
633 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
634 struct nfp_net_tx_ring *tx_ring)
636 netif_tx_stop_queue(nd_q);
638 /* We can race with the TX completion out of NAPI so recheck */
639 smp_mb();
640 if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
641 netif_tx_start_queue(nd_q);
645 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
646 * @r_vec: per-ring structure
647 * @txbuf: Pointer to driver soft TX descriptor
648 * @txd: Pointer to HW TX descriptor
649 * @skb: Pointer to SKB
651 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
652 * Return error on packet header greater than maximum supported LSO header size.
654 static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
655 struct nfp_net_tx_buf *txbuf,
656 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
658 u32 hdrlen;
659 u16 mss;
661 if (!skb_is_gso(skb))
662 return;
664 if (!skb->encapsulation) {
665 txd->l3_offset = skb_network_offset(skb);
666 txd->l4_offset = skb_transport_offset(skb);
667 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
668 } else {
669 txd->l3_offset = skb_inner_network_offset(skb);
670 txd->l4_offset = skb_inner_transport_offset(skb);
671 hdrlen = skb_inner_transport_header(skb) - skb->data +
672 inner_tcp_hdrlen(skb);
675 txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
676 txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
678 mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
679 txd->lso_hdrlen = hdrlen;
680 txd->mss = cpu_to_le16(mss);
681 txd->flags |= PCIE_DESC_TX_LSO;
683 u64_stats_update_begin(&r_vec->tx_sync);
684 r_vec->tx_lso++;
685 u64_stats_update_end(&r_vec->tx_sync);
689 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
690 * @dp: NFP Net data path struct
691 * @r_vec: per-ring structure
692 * @txbuf: Pointer to driver soft TX descriptor
693 * @txd: Pointer to TX descriptor
694 * @skb: Pointer to SKB
696 * This function sets the TX checksum flags in the TX descriptor based
697 * on the configuration and the protocol of the packet to be transmitted.
699 static void nfp_net_tx_csum(struct nfp_net_dp *dp,
700 struct nfp_net_r_vector *r_vec,
701 struct nfp_net_tx_buf *txbuf,
702 struct nfp_net_tx_desc *txd, struct sk_buff *skb)
704 struct ipv6hdr *ipv6h;
705 struct iphdr *iph;
706 u8 l4_hdr;
708 if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
709 return;
711 if (skb->ip_summed != CHECKSUM_PARTIAL)
712 return;
714 txd->flags |= PCIE_DESC_TX_CSUM;
715 if (skb->encapsulation)
716 txd->flags |= PCIE_DESC_TX_ENCAP;
718 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
719 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
721 if (iph->version == 4) {
722 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
723 l4_hdr = iph->protocol;
724 } else if (ipv6h->version == 6) {
725 l4_hdr = ipv6h->nexthdr;
726 } else {
727 nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
728 return;
731 switch (l4_hdr) {
732 case IPPROTO_TCP:
733 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
734 break;
735 case IPPROTO_UDP:
736 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
737 break;
738 default:
739 nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
740 return;
743 u64_stats_update_begin(&r_vec->tx_sync);
744 if (skb->encapsulation)
745 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
746 else
747 r_vec->hw_csum_tx += txbuf->pkt_cnt;
748 u64_stats_update_end(&r_vec->tx_sync);
751 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
753 wmb();
754 nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
755 tx_ring->wr_ptr_add = 0;
759 * nfp_net_tx() - Main transmit entry point
760 * @skb: SKB to transmit
761 * @netdev: netdev structure
763 * Return: NETDEV_TX_OK on success.
765 static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
767 struct nfp_net *nn = netdev_priv(netdev);
768 const struct skb_frag_struct *frag;
769 struct nfp_net_tx_desc *txd, txdg;
770 struct nfp_net_tx_ring *tx_ring;
771 struct nfp_net_r_vector *r_vec;
772 struct nfp_net_tx_buf *txbuf;
773 struct netdev_queue *nd_q;
774 struct nfp_net_dp *dp;
775 dma_addr_t dma_addr;
776 unsigned int fsize;
777 int f, nr_frags;
778 int wr_idx;
779 u16 qidx;
781 dp = &nn->dp;
782 qidx = skb_get_queue_mapping(skb);
783 tx_ring = &dp->tx_rings[qidx];
784 r_vec = tx_ring->r_vec;
785 nd_q = netdev_get_tx_queue(dp->netdev, qidx);
787 nr_frags = skb_shinfo(skb)->nr_frags;
789 if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
790 nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
791 qidx, tx_ring->wr_p, tx_ring->rd_p);
792 netif_tx_stop_queue(nd_q);
793 nfp_net_tx_xmit_more_flush(tx_ring);
794 u64_stats_update_begin(&r_vec->tx_sync);
795 r_vec->tx_busy++;
796 u64_stats_update_end(&r_vec->tx_sync);
797 return NETDEV_TX_BUSY;
800 /* Start with the head skbuf */
801 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
802 DMA_TO_DEVICE);
803 if (dma_mapping_error(dp->dev, dma_addr))
804 goto err_free;
806 wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
808 /* Stash the soft descriptor of the head then initialize it */
809 txbuf = &tx_ring->txbufs[wr_idx];
810 txbuf->skb = skb;
811 txbuf->dma_addr = dma_addr;
812 txbuf->fidx = -1;
813 txbuf->pkt_cnt = 1;
814 txbuf->real_len = skb->len;
816 /* Build TX descriptor */
817 txd = &tx_ring->txds[wr_idx];
818 txd->offset_eop = (nr_frags == 0) ? PCIE_DESC_TX_EOP : 0;
819 txd->dma_len = cpu_to_le16(skb_headlen(skb));
820 nfp_desc_set_dma_addr(txd, dma_addr);
821 txd->data_len = cpu_to_le16(skb->len);
823 txd->flags = 0;
824 txd->mss = 0;
825 txd->lso_hdrlen = 0;
827 /* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
828 nfp_net_tx_tso(r_vec, txbuf, txd, skb);
829 nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
830 if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
831 txd->flags |= PCIE_DESC_TX_VLAN;
832 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
835 /* Gather DMA */
836 if (nr_frags > 0) {
837 /* all descs must match except for in addr, length and eop */
838 txdg = *txd;
840 for (f = 0; f < nr_frags; f++) {
841 frag = &skb_shinfo(skb)->frags[f];
842 fsize = skb_frag_size(frag);
844 dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
845 fsize, DMA_TO_DEVICE);
846 if (dma_mapping_error(dp->dev, dma_addr))
847 goto err_unmap;
849 wr_idx = D_IDX(tx_ring, wr_idx + 1);
850 tx_ring->txbufs[wr_idx].skb = skb;
851 tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
852 tx_ring->txbufs[wr_idx].fidx = f;
854 txd = &tx_ring->txds[wr_idx];
855 *txd = txdg;
856 txd->dma_len = cpu_to_le16(fsize);
857 nfp_desc_set_dma_addr(txd, dma_addr);
858 txd->offset_eop =
859 (f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
862 u64_stats_update_begin(&r_vec->tx_sync);
863 r_vec->tx_gather++;
864 u64_stats_update_end(&r_vec->tx_sync);
867 netdev_tx_sent_queue(nd_q, txbuf->real_len);
869 tx_ring->wr_p += nr_frags + 1;
870 if (nfp_net_tx_ring_should_stop(tx_ring))
871 nfp_net_tx_ring_stop(nd_q, tx_ring);
873 tx_ring->wr_ptr_add += nr_frags + 1;
874 if (!skb->xmit_more || netif_xmit_stopped(nd_q))
875 nfp_net_tx_xmit_more_flush(tx_ring);
877 skb_tx_timestamp(skb);
879 return NETDEV_TX_OK;
881 err_unmap:
882 --f;
883 while (f >= 0) {
884 frag = &skb_shinfo(skb)->frags[f];
885 dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
886 skb_frag_size(frag), DMA_TO_DEVICE);
887 tx_ring->txbufs[wr_idx].skb = NULL;
888 tx_ring->txbufs[wr_idx].dma_addr = 0;
889 tx_ring->txbufs[wr_idx].fidx = -2;
890 wr_idx = wr_idx - 1;
891 if (wr_idx < 0)
892 wr_idx += tx_ring->cnt;
894 dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
895 skb_headlen(skb), DMA_TO_DEVICE);
896 tx_ring->txbufs[wr_idx].skb = NULL;
897 tx_ring->txbufs[wr_idx].dma_addr = 0;
898 tx_ring->txbufs[wr_idx].fidx = -2;
899 err_free:
900 nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
901 nfp_net_tx_xmit_more_flush(tx_ring);
902 u64_stats_update_begin(&r_vec->tx_sync);
903 r_vec->tx_errors++;
904 u64_stats_update_end(&r_vec->tx_sync);
905 dev_kfree_skb_any(skb);
906 return NETDEV_TX_OK;
910 * nfp_net_tx_complete() - Handled completed TX packets
911 * @tx_ring: TX ring structure
913 * Return: Number of completed TX descriptors
915 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring)
917 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
918 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
919 const struct skb_frag_struct *frag;
920 struct netdev_queue *nd_q;
921 u32 done_pkts = 0, done_bytes = 0;
922 struct sk_buff *skb;
923 int todo, nr_frags;
924 u32 qcp_rd_p;
925 int fidx;
926 int idx;
928 if (tx_ring->wr_p == tx_ring->rd_p)
929 return;
931 /* Work out how many descriptors have been transmitted */
932 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
934 if (qcp_rd_p == tx_ring->qcp_rd_p)
935 return;
937 todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
939 while (todo--) {
940 idx = D_IDX(tx_ring, tx_ring->rd_p++);
942 skb = tx_ring->txbufs[idx].skb;
943 if (!skb)
944 continue;
946 nr_frags = skb_shinfo(skb)->nr_frags;
947 fidx = tx_ring->txbufs[idx].fidx;
949 if (fidx == -1) {
950 /* unmap head */
951 dma_unmap_single(dp->dev, tx_ring->txbufs[idx].dma_addr,
952 skb_headlen(skb), DMA_TO_DEVICE);
954 done_pkts += tx_ring->txbufs[idx].pkt_cnt;
955 done_bytes += tx_ring->txbufs[idx].real_len;
956 } else {
957 /* unmap fragment */
958 frag = &skb_shinfo(skb)->frags[fidx];
959 dma_unmap_page(dp->dev, tx_ring->txbufs[idx].dma_addr,
960 skb_frag_size(frag), DMA_TO_DEVICE);
963 /* check for last gather fragment */
964 if (fidx == nr_frags - 1)
965 dev_kfree_skb_any(skb);
967 tx_ring->txbufs[idx].dma_addr = 0;
968 tx_ring->txbufs[idx].skb = NULL;
969 tx_ring->txbufs[idx].fidx = -2;
972 tx_ring->qcp_rd_p = qcp_rd_p;
974 u64_stats_update_begin(&r_vec->tx_sync);
975 r_vec->tx_bytes += done_bytes;
976 r_vec->tx_pkts += done_pkts;
977 u64_stats_update_end(&r_vec->tx_sync);
979 if (!dp->netdev)
980 return;
982 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
983 netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
984 if (nfp_net_tx_ring_should_wake(tx_ring)) {
985 /* Make sure TX thread will see updated tx_ring->rd_p */
986 smp_mb();
988 if (unlikely(netif_tx_queue_stopped(nd_q)))
989 netif_tx_wake_queue(nd_q);
992 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
993 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
994 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
997 static bool nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
999 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1000 u32 done_pkts = 0, done_bytes = 0;
1001 bool done_all;
1002 int idx, todo;
1003 u32 qcp_rd_p;
1005 /* Work out how many descriptors have been transmitted */
1006 qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1008 if (qcp_rd_p == tx_ring->qcp_rd_p)
1009 return true;
1011 todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1013 done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
1014 todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
1016 tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
1018 done_pkts = todo;
1019 while (todo--) {
1020 idx = D_IDX(tx_ring, tx_ring->rd_p);
1021 tx_ring->rd_p++;
1023 done_bytes += tx_ring->txbufs[idx].real_len;
1026 u64_stats_update_begin(&r_vec->tx_sync);
1027 r_vec->tx_bytes += done_bytes;
1028 r_vec->tx_pkts += done_pkts;
1029 u64_stats_update_end(&r_vec->tx_sync);
1031 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1032 "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1033 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1035 return done_all;
1039 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1040 * @dp: NFP Net data path struct
1041 * @tx_ring: TX ring structure
1043 * Assumes that the device is stopped
1045 static void
1046 nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1048 const struct skb_frag_struct *frag;
1049 struct netdev_queue *nd_q;
1051 while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
1052 struct nfp_net_tx_buf *tx_buf;
1053 struct sk_buff *skb;
1054 int idx, nr_frags;
1056 idx = D_IDX(tx_ring, tx_ring->rd_p);
1057 tx_buf = &tx_ring->txbufs[idx];
1059 skb = tx_ring->txbufs[idx].skb;
1060 nr_frags = skb_shinfo(skb)->nr_frags;
1062 if (tx_buf->fidx == -1) {
1063 /* unmap head */
1064 dma_unmap_single(dp->dev, tx_buf->dma_addr,
1065 skb_headlen(skb), DMA_TO_DEVICE);
1066 } else {
1067 /* unmap fragment */
1068 frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
1069 dma_unmap_page(dp->dev, tx_buf->dma_addr,
1070 skb_frag_size(frag), DMA_TO_DEVICE);
1073 /* check for last gather fragment */
1074 if (tx_buf->fidx == nr_frags - 1)
1075 dev_kfree_skb_any(skb);
1077 tx_buf->dma_addr = 0;
1078 tx_buf->skb = NULL;
1079 tx_buf->fidx = -2;
1081 tx_ring->qcp_rd_p++;
1082 tx_ring->rd_p++;
1085 memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
1086 tx_ring->wr_p = 0;
1087 tx_ring->rd_p = 0;
1088 tx_ring->qcp_rd_p = 0;
1089 tx_ring->wr_ptr_add = 0;
1091 if (tx_ring->is_xdp || !dp->netdev)
1092 return;
1094 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1095 netdev_tx_reset_queue(nd_q);
1098 static void nfp_net_tx_timeout(struct net_device *netdev)
1100 struct nfp_net *nn = netdev_priv(netdev);
1101 int i;
1103 for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
1104 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1105 continue;
1106 nn_warn(nn, "TX timeout on ring: %d\n", i);
1108 nn_warn(nn, "TX watchdog timeout\n");
1111 /* Receive processing
1113 static unsigned int
1114 nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1116 unsigned int fl_bufsz;
1118 fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1119 fl_bufsz += dp->rx_dma_off;
1120 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1121 fl_bufsz += NFP_NET_MAX_PREPEND;
1122 else
1123 fl_bufsz += dp->rx_offset;
1124 fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1126 fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1127 fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1129 return fl_bufsz;
1132 static void
1133 nfp_net_free_frag(void *frag, bool xdp)
1135 if (!xdp)
1136 skb_free_frag(frag);
1137 else
1138 __free_page(virt_to_page(frag));
1142 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1143 * @dp: NFP Net data path struct
1144 * @dma_addr: Pointer to storage for DMA address (output param)
1146 * This function will allcate a new page frag, map it for DMA.
1148 * Return: allocated page frag or NULL on failure.
1150 static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1152 void *frag;
1154 if (!dp->xdp_prog)
1155 frag = netdev_alloc_frag(dp->fl_bufsz);
1156 else
1157 frag = page_address(alloc_page(GFP_KERNEL | __GFP_COLD));
1158 if (!frag) {
1159 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1160 return NULL;
1163 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1164 if (dma_mapping_error(dp->dev, *dma_addr)) {
1165 nfp_net_free_frag(frag, dp->xdp_prog);
1166 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1167 return NULL;
1170 return frag;
1173 static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1175 void *frag;
1177 if (!dp->xdp_prog)
1178 frag = napi_alloc_frag(dp->fl_bufsz);
1179 else
1180 frag = page_address(alloc_page(GFP_ATOMIC | __GFP_COLD));
1181 if (!frag) {
1182 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1183 return NULL;
1186 *dma_addr = nfp_net_dma_map_rx(dp, frag);
1187 if (dma_mapping_error(dp->dev, *dma_addr)) {
1188 nfp_net_free_frag(frag, dp->xdp_prog);
1189 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1190 return NULL;
1193 return frag;
1197 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1198 * @dp: NFP Net data path struct
1199 * @rx_ring: RX ring structure
1200 * @frag: page fragment buffer
1201 * @dma_addr: DMA address of skb mapping
1203 static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
1204 struct nfp_net_rx_ring *rx_ring,
1205 void *frag, dma_addr_t dma_addr)
1207 unsigned int wr_idx;
1209 wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1211 nfp_net_dma_sync_dev_rx(dp, dma_addr);
1213 /* Stash SKB and DMA address away */
1214 rx_ring->rxbufs[wr_idx].frag = frag;
1215 rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1217 /* Fill freelist descriptor */
1218 rx_ring->rxds[wr_idx].fld.reserved = 0;
1219 rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1220 nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
1221 dma_addr + dp->rx_dma_off);
1223 rx_ring->wr_p++;
1224 if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
1225 /* Update write pointer of the freelist queue. Make
1226 * sure all writes are flushed before telling the hardware.
1228 wmb();
1229 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
1234 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1235 * @rx_ring: RX ring structure
1237 * Warning: Do *not* call if ring buffers were never put on the FW freelist
1238 * (i.e. device was not enabled)!
1240 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1242 unsigned int wr_idx, last_idx;
1244 /* Move the empty entry to the end of the list */
1245 wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1246 last_idx = rx_ring->cnt - 1;
1247 rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1248 rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1249 rx_ring->rxbufs[last_idx].dma_addr = 0;
1250 rx_ring->rxbufs[last_idx].frag = NULL;
1252 memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
1253 rx_ring->wr_p = 0;
1254 rx_ring->rd_p = 0;
1258 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1259 * @dp: NFP Net data path struct
1260 * @rx_ring: RX ring to remove buffers from
1262 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1263 * entries. After device is disabled nfp_net_rx_ring_reset() must be called
1264 * to restore required ring geometry.
1266 static void
1267 nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1268 struct nfp_net_rx_ring *rx_ring)
1270 unsigned int i;
1272 for (i = 0; i < rx_ring->cnt - 1; i++) {
1273 /* NULL skb can only happen when initial filling of the ring
1274 * fails to allocate enough buffers and calls here to free
1275 * already allocated ones.
1277 if (!rx_ring->rxbufs[i].frag)
1278 continue;
1280 nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1281 nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1282 rx_ring->rxbufs[i].dma_addr = 0;
1283 rx_ring->rxbufs[i].frag = NULL;
1288 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1289 * @dp: NFP Net data path struct
1290 * @rx_ring: RX ring to remove buffers from
1292 static int
1293 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1294 struct nfp_net_rx_ring *rx_ring)
1296 struct nfp_net_rx_buf *rxbufs;
1297 unsigned int i;
1299 rxbufs = rx_ring->rxbufs;
1301 for (i = 0; i < rx_ring->cnt - 1; i++) {
1302 rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
1303 if (!rxbufs[i].frag) {
1304 nfp_net_rx_ring_bufs_free(dp, rx_ring);
1305 return -ENOMEM;
1309 return 0;
1313 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1314 * @dp: NFP Net data path struct
1315 * @rx_ring: RX ring to fill
1317 static void
1318 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
1319 struct nfp_net_rx_ring *rx_ring)
1321 unsigned int i;
1323 for (i = 0; i < rx_ring->cnt - 1; i++)
1324 nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1325 rx_ring->rxbufs[i].dma_addr);
1329 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1330 * @flags: RX descriptor flags field in CPU byte order
1332 static int nfp_net_rx_csum_has_errors(u16 flags)
1334 u16 csum_all_checked, csum_all_ok;
1336 csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1337 csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1339 return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1343 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1344 * @dp: NFP Net data path struct
1345 * @r_vec: per-ring structure
1346 * @rxd: Pointer to RX descriptor
1347 * @meta: Parsed metadata prepend
1348 * @skb: Pointer to SKB
1350 static void nfp_net_rx_csum(struct nfp_net_dp *dp,
1351 struct nfp_net_r_vector *r_vec,
1352 struct nfp_net_rx_desc *rxd,
1353 struct nfp_meta_parsed *meta, struct sk_buff *skb)
1355 skb_checksum_none_assert(skb);
1357 if (!(dp->netdev->features & NETIF_F_RXCSUM))
1358 return;
1360 if (meta->csum_type) {
1361 skb->ip_summed = meta->csum_type;
1362 skb->csum = meta->csum;
1363 u64_stats_update_begin(&r_vec->rx_sync);
1364 r_vec->hw_csum_rx_ok++;
1365 u64_stats_update_end(&r_vec->rx_sync);
1366 return;
1369 if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1370 u64_stats_update_begin(&r_vec->rx_sync);
1371 r_vec->hw_csum_rx_error++;
1372 u64_stats_update_end(&r_vec->rx_sync);
1373 return;
1376 /* Assume that the firmware will never report inner CSUM_OK unless outer
1377 * L4 headers were successfully parsed. FW will always report zero UDP
1378 * checksum as CSUM_OK.
1380 if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1381 rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1382 __skb_incr_checksum_unnecessary(skb);
1383 u64_stats_update_begin(&r_vec->rx_sync);
1384 r_vec->hw_csum_rx_ok++;
1385 u64_stats_update_end(&r_vec->rx_sync);
1388 if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1389 rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1390 __skb_incr_checksum_unnecessary(skb);
1391 u64_stats_update_begin(&r_vec->rx_sync);
1392 r_vec->hw_csum_rx_inner_ok++;
1393 u64_stats_update_end(&r_vec->rx_sync);
1397 static void
1398 nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
1399 unsigned int type, __be32 *hash)
1401 if (!(netdev->features & NETIF_F_RXHASH))
1402 return;
1404 switch (type) {
1405 case NFP_NET_RSS_IPV4:
1406 case NFP_NET_RSS_IPV6:
1407 case NFP_NET_RSS_IPV6_EX:
1408 meta->hash_type = PKT_HASH_TYPE_L3;
1409 break;
1410 default:
1411 meta->hash_type = PKT_HASH_TYPE_L4;
1412 break;
1415 meta->hash = get_unaligned_be32(hash);
1418 static void
1419 nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
1420 void *data, struct nfp_net_rx_desc *rxd)
1422 struct nfp_net_rx_hash *rx_hash = data;
1424 if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1425 return;
1427 nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
1428 &rx_hash->hash);
1431 static void *
1432 nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
1433 void *data, int meta_len)
1435 u32 meta_info;
1437 meta_info = get_unaligned_be32(data);
1438 data += 4;
1440 while (meta_info) {
1441 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1442 case NFP_NET_META_HASH:
1443 meta_info >>= NFP_NET_META_FIELD_SIZE;
1444 nfp_net_set_hash(netdev, meta,
1445 meta_info & NFP_NET_META_FIELD_MASK,
1446 (__be32 *)data);
1447 data += 4;
1448 break;
1449 case NFP_NET_META_MARK:
1450 meta->mark = get_unaligned_be32(data);
1451 data += 4;
1452 break;
1453 case NFP_NET_META_CSUM:
1454 meta->csum_type = CHECKSUM_COMPLETE;
1455 meta->csum =
1456 (__force __wsum)__get_unaligned_cpu32(data);
1457 data += 4;
1458 break;
1459 default:
1460 return NULL;
1463 meta_info >>= NFP_NET_META_FIELD_SIZE;
1466 return data;
1469 static void
1470 nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
1471 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
1472 struct sk_buff *skb)
1474 u64_stats_update_begin(&r_vec->rx_sync);
1475 r_vec->rx_drops++;
1476 u64_stats_update_end(&r_vec->rx_sync);
1478 /* skb is build based on the frag, free_skb() would free the frag
1479 * so to be able to reuse it we need an extra ref.
1481 if (skb && rxbuf && skb->head == rxbuf->frag)
1482 page_ref_inc(virt_to_head_page(rxbuf->frag));
1483 if (rxbuf)
1484 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1485 if (skb)
1486 dev_kfree_skb_any(skb);
1489 static bool
1490 nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1491 struct nfp_net_tx_ring *tx_ring,
1492 struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1493 unsigned int pkt_len, bool *completed)
1495 struct nfp_net_tx_buf *txbuf;
1496 struct nfp_net_tx_desc *txd;
1497 int wr_idx;
1499 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1500 if (!*completed) {
1501 nfp_net_xdp_complete(tx_ring);
1502 *completed = true;
1505 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1506 nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
1507 NULL);
1508 return false;
1512 wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1514 /* Stash the soft descriptor of the head then initialize it */
1515 txbuf = &tx_ring->txbufs[wr_idx];
1517 nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
1519 txbuf->frag = rxbuf->frag;
1520 txbuf->dma_addr = rxbuf->dma_addr;
1521 txbuf->fidx = -1;
1522 txbuf->pkt_cnt = 1;
1523 txbuf->real_len = pkt_len;
1525 dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1526 pkt_len, DMA_BIDIRECTIONAL);
1528 /* Build TX descriptor */
1529 txd = &tx_ring->txds[wr_idx];
1530 txd->offset_eop = PCIE_DESC_TX_EOP;
1531 txd->dma_len = cpu_to_le16(pkt_len);
1532 nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1533 txd->data_len = cpu_to_le16(pkt_len);
1535 txd->flags = 0;
1536 txd->mss = 0;
1537 txd->lso_hdrlen = 0;
1539 tx_ring->wr_p++;
1540 tx_ring->wr_ptr_add++;
1541 return true;
1544 static int nfp_net_run_xdp(struct bpf_prog *prog, void *data, void *hard_start,
1545 unsigned int *off, unsigned int *len)
1547 struct xdp_buff xdp;
1548 void *orig_data;
1549 int ret;
1551 xdp.data_hard_start = hard_start;
1552 xdp.data = data + *off;
1553 xdp.data_end = data + *off + *len;
1555 orig_data = xdp.data;
1556 ret = bpf_prog_run_xdp(prog, &xdp);
1558 *len -= xdp.data - orig_data;
1559 *off += xdp.data - orig_data;
1561 return ret;
1565 * nfp_net_rx() - receive up to @budget packets on @rx_ring
1566 * @rx_ring: RX ring to receive from
1567 * @budget: NAPI budget
1569 * Note, this function is separated out from the napi poll function to
1570 * more cleanly separate packet receive code from other bookkeeping
1571 * functions performed in the napi poll function.
1573 * Return: Number of packets received.
1575 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1577 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1578 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1579 struct nfp_net_tx_ring *tx_ring;
1580 struct bpf_prog *xdp_prog;
1581 bool xdp_tx_cmpl = false;
1582 unsigned int true_bufsz;
1583 struct sk_buff *skb;
1584 int pkts_polled = 0;
1585 int idx;
1587 rcu_read_lock();
1588 xdp_prog = READ_ONCE(dp->xdp_prog);
1589 true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1590 tx_ring = r_vec->xdp_ring;
1592 while (pkts_polled < budget) {
1593 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1594 struct nfp_net_rx_buf *rxbuf;
1595 struct nfp_net_rx_desc *rxd;
1596 struct nfp_meta_parsed meta;
1597 dma_addr_t new_dma_addr;
1598 void *new_frag;
1600 idx = D_IDX(rx_ring, rx_ring->rd_p);
1602 rxd = &rx_ring->rxds[idx];
1603 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1604 break;
1606 /* Memory barrier to ensure that we won't do other reads
1607 * before the DD bit.
1609 dma_rmb();
1611 memset(&meta, 0, sizeof(meta));
1613 rx_ring->rd_p++;
1614 pkts_polled++;
1616 rxbuf = &rx_ring->rxbufs[idx];
1617 /* < meta_len >
1618 * <-- [rx_offset] -->
1619 * ---------------------------------------------------------
1620 * | [XX] | metadata | packet | XXXX |
1621 * ---------------------------------------------------------
1622 * <---------------- data_len --------------->
1624 * The rx_offset is fixed for all packets, the meta_len can vary
1625 * on a packet by packet basis. If rx_offset is set to zero
1626 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1627 * buffer and is immediately followed by the packet (no [XX]).
1629 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1630 data_len = le16_to_cpu(rxd->rxd.data_len);
1631 pkt_len = data_len - meta_len;
1633 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1634 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1635 pkt_off += meta_len;
1636 else
1637 pkt_off += dp->rx_offset;
1638 meta_off = pkt_off - meta_len;
1640 /* Stats update */
1641 u64_stats_update_begin(&r_vec->rx_sync);
1642 r_vec->rx_pkts++;
1643 r_vec->rx_bytes += pkt_len;
1644 u64_stats_update_end(&r_vec->rx_sync);
1646 if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1647 (dp->rx_offset && meta_len > dp->rx_offset))) {
1648 nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1649 meta_len);
1650 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1651 continue;
1654 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1655 data_len);
1657 if (!dp->chained_metadata_format) {
1658 nfp_net_set_hash_desc(dp->netdev, &meta,
1659 rxbuf->frag + meta_off, rxd);
1660 } else if (meta_len) {
1661 void *end;
1663 end = nfp_net_parse_meta(dp->netdev, &meta,
1664 rxbuf->frag + meta_off,
1665 meta_len);
1666 if (unlikely(end != rxbuf->frag + pkt_off)) {
1667 nn_dp_warn(dp, "invalid RX packet metadata\n");
1668 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1669 NULL);
1670 continue;
1674 if (xdp_prog && !(rxd->rxd.flags & PCIE_DESC_RX_BPF &&
1675 dp->bpf_offload_xdp)) {
1676 unsigned int dma_off;
1677 void *hard_start;
1678 int act;
1680 hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
1682 act = nfp_net_run_xdp(xdp_prog, rxbuf->frag, hard_start,
1683 &pkt_off, &pkt_len);
1684 switch (act) {
1685 case XDP_PASS:
1686 break;
1687 case XDP_TX:
1688 dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1689 if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1690 tx_ring, rxbuf,
1691 dma_off,
1692 pkt_len,
1693 &xdp_tx_cmpl)))
1694 trace_xdp_exception(dp->netdev,
1695 xdp_prog, act);
1696 continue;
1697 default:
1698 bpf_warn_invalid_xdp_action(act);
1699 /* fall through */
1700 case XDP_ABORTED:
1701 trace_xdp_exception(dp->netdev, xdp_prog, act);
1702 /* fall through */
1703 case XDP_DROP:
1704 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1705 rxbuf->dma_addr);
1706 continue;
1710 skb = build_skb(rxbuf->frag, true_bufsz);
1711 if (unlikely(!skb)) {
1712 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1713 continue;
1715 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1716 if (unlikely(!new_frag)) {
1717 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1718 continue;
1721 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1723 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1725 skb_reserve(skb, pkt_off);
1726 skb_put(skb, pkt_len);
1728 skb->mark = meta.mark;
1729 skb_set_hash(skb, meta.hash, meta.hash_type);
1731 skb_record_rx_queue(skb, rx_ring->idx);
1732 skb->protocol = eth_type_trans(skb, dp->netdev);
1734 nfp_net_rx_csum(dp, r_vec, rxd, &meta, skb);
1736 if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1737 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1738 le16_to_cpu(rxd->rxd.vlan));
1740 napi_gro_receive(&rx_ring->r_vec->napi, skb);
1743 if (xdp_prog) {
1744 if (tx_ring->wr_ptr_add)
1745 nfp_net_tx_xmit_more_flush(tx_ring);
1746 else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
1747 !xdp_tx_cmpl)
1748 if (!nfp_net_xdp_complete(tx_ring))
1749 pkts_polled = budget;
1751 rcu_read_unlock();
1753 return pkts_polled;
1757 * nfp_net_poll() - napi poll function
1758 * @napi: NAPI structure
1759 * @budget: NAPI budget
1761 * Return: number of packets polled.
1763 static int nfp_net_poll(struct napi_struct *napi, int budget)
1765 struct nfp_net_r_vector *r_vec =
1766 container_of(napi, struct nfp_net_r_vector, napi);
1767 unsigned int pkts_polled = 0;
1769 if (r_vec->tx_ring)
1770 nfp_net_tx_complete(r_vec->tx_ring);
1771 if (r_vec->rx_ring)
1772 pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1774 if (pkts_polled < budget)
1775 if (napi_complete_done(napi, pkts_polled))
1776 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1778 return pkts_polled;
1781 /* Control device data path
1784 static bool
1785 nfp_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1786 struct sk_buff *skb, bool old)
1788 unsigned int real_len = skb->len, meta_len = 0;
1789 struct nfp_net_tx_ring *tx_ring;
1790 struct nfp_net_tx_buf *txbuf;
1791 struct nfp_net_tx_desc *txd;
1792 struct nfp_net_dp *dp;
1793 dma_addr_t dma_addr;
1794 int wr_idx;
1796 dp = &r_vec->nfp_net->dp;
1797 tx_ring = r_vec->tx_ring;
1799 if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
1800 nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
1801 goto err_free;
1804 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1805 u64_stats_update_begin(&r_vec->tx_sync);
1806 r_vec->tx_busy++;
1807 u64_stats_update_end(&r_vec->tx_sync);
1808 if (!old)
1809 __skb_queue_tail(&r_vec->queue, skb);
1810 else
1811 __skb_queue_head(&r_vec->queue, skb);
1812 return true;
1815 if (nfp_app_ctrl_has_meta(nn->app)) {
1816 if (unlikely(skb_headroom(skb) < 8)) {
1817 nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
1818 goto err_free;
1820 meta_len = 8;
1821 put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
1822 put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
1825 /* Start with the head skbuf */
1826 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
1827 DMA_TO_DEVICE);
1828 if (dma_mapping_error(dp->dev, dma_addr))
1829 goto err_dma_warn;
1831 wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1833 /* Stash the soft descriptor of the head then initialize it */
1834 txbuf = &tx_ring->txbufs[wr_idx];
1835 txbuf->skb = skb;
1836 txbuf->dma_addr = dma_addr;
1837 txbuf->fidx = -1;
1838 txbuf->pkt_cnt = 1;
1839 txbuf->real_len = real_len;
1841 /* Build TX descriptor */
1842 txd = &tx_ring->txds[wr_idx];
1843 txd->offset_eop = meta_len | PCIE_DESC_TX_EOP;
1844 txd->dma_len = cpu_to_le16(skb_headlen(skb));
1845 nfp_desc_set_dma_addr(txd, dma_addr);
1846 txd->data_len = cpu_to_le16(skb->len);
1848 txd->flags = 0;
1849 txd->mss = 0;
1850 txd->lso_hdrlen = 0;
1852 tx_ring->wr_p++;
1853 tx_ring->wr_ptr_add++;
1854 nfp_net_tx_xmit_more_flush(tx_ring);
1856 return false;
1858 err_dma_warn:
1859 nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
1860 err_free:
1861 u64_stats_update_begin(&r_vec->tx_sync);
1862 r_vec->tx_errors++;
1863 u64_stats_update_end(&r_vec->tx_sync);
1864 dev_kfree_skb_any(skb);
1865 return false;
1868 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
1870 struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
1871 bool ret;
1873 spin_lock_bh(&r_vec->lock);
1874 ret = nfp_ctrl_tx_one(nn, r_vec, skb, false);
1875 spin_unlock_bh(&r_vec->lock);
1877 return ret;
1880 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
1882 struct sk_buff *skb;
1884 while ((skb = __skb_dequeue(&r_vec->queue)))
1885 if (nfp_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
1886 return;
1889 static bool
1890 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
1892 u32 meta_type, meta_tag;
1894 if (!nfp_app_ctrl_has_meta(nn->app))
1895 return !meta_len;
1897 if (meta_len != 8)
1898 return false;
1900 meta_type = get_unaligned_be32(data);
1901 meta_tag = get_unaligned_be32(data + 4);
1903 return (meta_type == NFP_NET_META_PORTID &&
1904 meta_tag == NFP_META_PORT_ID_CTRL);
1907 static bool
1908 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
1909 struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
1911 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1912 struct nfp_net_rx_buf *rxbuf;
1913 struct nfp_net_rx_desc *rxd;
1914 dma_addr_t new_dma_addr;
1915 struct sk_buff *skb;
1916 void *new_frag;
1917 int idx;
1919 idx = D_IDX(rx_ring, rx_ring->rd_p);
1921 rxd = &rx_ring->rxds[idx];
1922 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1923 return false;
1925 /* Memory barrier to ensure that we won't do other reads
1926 * before the DD bit.
1928 dma_rmb();
1930 rx_ring->rd_p++;
1932 rxbuf = &rx_ring->rxbufs[idx];
1933 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1934 data_len = le16_to_cpu(rxd->rxd.data_len);
1935 pkt_len = data_len - meta_len;
1937 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1938 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1939 pkt_off += meta_len;
1940 else
1941 pkt_off += dp->rx_offset;
1942 meta_off = pkt_off - meta_len;
1944 /* Stats update */
1945 u64_stats_update_begin(&r_vec->rx_sync);
1946 r_vec->rx_pkts++;
1947 r_vec->rx_bytes += pkt_len;
1948 u64_stats_update_end(&r_vec->rx_sync);
1950 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off, data_len);
1952 if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
1953 nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
1954 meta_len);
1955 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1956 return true;
1959 skb = build_skb(rxbuf->frag, dp->fl_bufsz);
1960 if (unlikely(!skb)) {
1961 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1962 return true;
1964 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1965 if (unlikely(!new_frag)) {
1966 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1967 return true;
1970 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1972 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1974 skb_reserve(skb, pkt_off);
1975 skb_put(skb, pkt_len);
1977 nfp_app_ctrl_rx(nn->app, skb);
1979 return true;
1982 static void nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
1984 struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
1985 struct nfp_net *nn = r_vec->nfp_net;
1986 struct nfp_net_dp *dp = &nn->dp;
1988 while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring))
1989 continue;
1992 static void nfp_ctrl_poll(unsigned long arg)
1994 struct nfp_net_r_vector *r_vec = (void *)arg;
1996 spin_lock_bh(&r_vec->lock);
1997 nfp_net_tx_complete(r_vec->tx_ring);
1998 __nfp_ctrl_tx_queued(r_vec);
1999 spin_unlock_bh(&r_vec->lock);
2001 nfp_ctrl_rx(r_vec);
2003 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
2006 /* Setup and Configuration
2010 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
2011 * @nn: NFP Network structure
2013 static void nfp_net_vecs_init(struct nfp_net *nn)
2015 struct nfp_net_r_vector *r_vec;
2016 int r;
2018 nn->lsc_handler = nfp_net_irq_lsc;
2019 nn->exn_handler = nfp_net_irq_exn;
2021 for (r = 0; r < nn->max_r_vecs; r++) {
2022 struct msix_entry *entry;
2024 entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
2026 r_vec = &nn->r_vecs[r];
2027 r_vec->nfp_net = nn;
2028 r_vec->irq_entry = entry->entry;
2029 r_vec->irq_vector = entry->vector;
2031 if (nn->dp.netdev) {
2032 r_vec->handler = nfp_net_irq_rxtx;
2033 } else {
2034 r_vec->handler = nfp_ctrl_irq_rxtx;
2036 __skb_queue_head_init(&r_vec->queue);
2037 spin_lock_init(&r_vec->lock);
2038 tasklet_init(&r_vec->tasklet, nfp_ctrl_poll,
2039 (unsigned long)r_vec);
2040 tasklet_disable(&r_vec->tasklet);
2043 cpumask_set_cpu(r, &r_vec->affinity_mask);
2048 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
2049 * @tx_ring: TX ring to free
2051 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
2053 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2054 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2056 kfree(tx_ring->txbufs);
2058 if (tx_ring->txds)
2059 dma_free_coherent(dp->dev, tx_ring->size,
2060 tx_ring->txds, tx_ring->dma);
2062 tx_ring->cnt = 0;
2063 tx_ring->txbufs = NULL;
2064 tx_ring->txds = NULL;
2065 tx_ring->dma = 0;
2066 tx_ring->size = 0;
2070 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
2071 * @dp: NFP Net data path struct
2072 * @tx_ring: TX Ring structure to allocate
2074 * Return: 0 on success, negative errno otherwise.
2076 static int
2077 nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
2079 struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2080 int sz;
2082 tx_ring->cnt = dp->txd_cnt;
2084 tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
2085 tx_ring->txds = dma_zalloc_coherent(dp->dev, tx_ring->size,
2086 &tx_ring->dma, GFP_KERNEL);
2087 if (!tx_ring->txds)
2088 goto err_alloc;
2090 sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
2091 tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
2092 if (!tx_ring->txbufs)
2093 goto err_alloc;
2095 if (!tx_ring->is_xdp && dp->netdev)
2096 netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
2097 tx_ring->idx);
2099 return 0;
2101 err_alloc:
2102 nfp_net_tx_ring_free(tx_ring);
2103 return -ENOMEM;
2106 static void
2107 nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
2108 struct nfp_net_tx_ring *tx_ring)
2110 unsigned int i;
2112 if (!tx_ring->is_xdp)
2113 return;
2115 for (i = 0; i < tx_ring->cnt; i++) {
2116 if (!tx_ring->txbufs[i].frag)
2117 return;
2119 nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
2120 __free_page(virt_to_page(tx_ring->txbufs[i].frag));
2124 static int
2125 nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
2126 struct nfp_net_tx_ring *tx_ring)
2128 struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
2129 unsigned int i;
2131 if (!tx_ring->is_xdp)
2132 return 0;
2134 for (i = 0; i < tx_ring->cnt; i++) {
2135 txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
2136 if (!txbufs[i].frag) {
2137 nfp_net_tx_ring_bufs_free(dp, tx_ring);
2138 return -ENOMEM;
2142 return 0;
2145 static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2147 unsigned int r;
2149 dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
2150 GFP_KERNEL);
2151 if (!dp->tx_rings)
2152 return -ENOMEM;
2154 for (r = 0; r < dp->num_tx_rings; r++) {
2155 int bias = 0;
2157 if (r >= dp->num_stack_tx_rings)
2158 bias = dp->num_stack_tx_rings;
2160 nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
2161 r, bias);
2163 if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
2164 goto err_free_prev;
2166 if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
2167 goto err_free_ring;
2170 return 0;
2172 err_free_prev:
2173 while (r--) {
2174 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2175 err_free_ring:
2176 nfp_net_tx_ring_free(&dp->tx_rings[r]);
2178 kfree(dp->tx_rings);
2179 return -ENOMEM;
2182 static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
2184 unsigned int r;
2186 for (r = 0; r < dp->num_tx_rings; r++) {
2187 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2188 nfp_net_tx_ring_free(&dp->tx_rings[r]);
2191 kfree(dp->tx_rings);
2195 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
2196 * @rx_ring: RX ring to free
2198 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
2200 struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
2201 struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2203 kfree(rx_ring->rxbufs);
2205 if (rx_ring->rxds)
2206 dma_free_coherent(dp->dev, rx_ring->size,
2207 rx_ring->rxds, rx_ring->dma);
2209 rx_ring->cnt = 0;
2210 rx_ring->rxbufs = NULL;
2211 rx_ring->rxds = NULL;
2212 rx_ring->dma = 0;
2213 rx_ring->size = 0;
2217 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
2218 * @dp: NFP Net data path struct
2219 * @rx_ring: RX ring to allocate
2221 * Return: 0 on success, negative errno otherwise.
2223 static int
2224 nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
2226 int sz;
2228 rx_ring->cnt = dp->rxd_cnt;
2229 rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
2230 rx_ring->rxds = dma_zalloc_coherent(dp->dev, rx_ring->size,
2231 &rx_ring->dma, GFP_KERNEL);
2232 if (!rx_ring->rxds)
2233 goto err_alloc;
2235 sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
2236 rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
2237 if (!rx_ring->rxbufs)
2238 goto err_alloc;
2240 return 0;
2242 err_alloc:
2243 nfp_net_rx_ring_free(rx_ring);
2244 return -ENOMEM;
2247 static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2249 unsigned int r;
2251 dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
2252 GFP_KERNEL);
2253 if (!dp->rx_rings)
2254 return -ENOMEM;
2256 for (r = 0; r < dp->num_rx_rings; r++) {
2257 nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
2259 if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
2260 goto err_free_prev;
2262 if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
2263 goto err_free_ring;
2266 return 0;
2268 err_free_prev:
2269 while (r--) {
2270 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2271 err_free_ring:
2272 nfp_net_rx_ring_free(&dp->rx_rings[r]);
2274 kfree(dp->rx_rings);
2275 return -ENOMEM;
2278 static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
2280 unsigned int r;
2282 for (r = 0; r < dp->num_rx_rings; r++) {
2283 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2284 nfp_net_rx_ring_free(&dp->rx_rings[r]);
2287 kfree(dp->rx_rings);
2290 static void
2291 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
2292 struct nfp_net_r_vector *r_vec, int idx)
2294 r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
2295 r_vec->tx_ring =
2296 idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
2298 r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
2299 &dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
2302 static int
2303 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
2304 int idx)
2306 int err;
2308 /* Setup NAPI */
2309 if (nn->dp.netdev)
2310 netif_napi_add(nn->dp.netdev, &r_vec->napi,
2311 nfp_net_poll, NAPI_POLL_WEIGHT);
2312 else
2313 tasklet_enable(&r_vec->tasklet);
2315 snprintf(r_vec->name, sizeof(r_vec->name),
2316 "%s-rxtx-%d", nfp_net_name(nn), idx);
2317 err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
2318 r_vec);
2319 if (err) {
2320 if (nn->dp.netdev)
2321 netif_napi_del(&r_vec->napi);
2322 else
2323 tasklet_disable(&r_vec->tasklet);
2325 nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
2326 return err;
2328 disable_irq(r_vec->irq_vector);
2330 irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
2332 nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
2333 r_vec->irq_entry);
2335 return 0;
2338 static void
2339 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
2341 irq_set_affinity_hint(r_vec->irq_vector, NULL);
2342 if (nn->dp.netdev)
2343 netif_napi_del(&r_vec->napi);
2344 else
2345 tasklet_disable(&r_vec->tasklet);
2347 free_irq(r_vec->irq_vector, r_vec);
2351 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
2352 * @nn: NFP Net device to reconfigure
2354 void nfp_net_rss_write_itbl(struct nfp_net *nn)
2356 int i;
2358 for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
2359 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
2360 get_unaligned_le32(nn->rss_itbl + i));
2364 * nfp_net_rss_write_key() - Write RSS hash key to device
2365 * @nn: NFP Net device to reconfigure
2367 void nfp_net_rss_write_key(struct nfp_net *nn)
2369 int i;
2371 for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2372 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
2373 get_unaligned_le32(nn->rss_key + i));
2377 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2378 * @nn: NFP Net device to reconfigure
2380 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
2382 u8 i;
2383 u32 factor;
2384 u32 value;
2386 /* Compute factor used to convert coalesce '_usecs' parameters to
2387 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
2388 * count.
2390 factor = nn->me_freq_mhz / 16;
2392 /* copy RX interrupt coalesce parameters */
2393 value = (nn->rx_coalesce_max_frames << 16) |
2394 (factor * nn->rx_coalesce_usecs);
2395 for (i = 0; i < nn->dp.num_rx_rings; i++)
2396 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
2398 /* copy TX interrupt coalesce parameters */
2399 value = (nn->tx_coalesce_max_frames << 16) |
2400 (factor * nn->tx_coalesce_usecs);
2401 for (i = 0; i < nn->dp.num_tx_rings; i++)
2402 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
2406 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2407 * @nn: NFP Net device to reconfigure
2408 * @addr: MAC address to write
2410 * Writes the MAC address from the netdev to the device control BAR. Does not
2411 * perform the required reconfig. We do a bit of byte swapping dance because
2412 * firmware is LE.
2414 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
2416 nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
2417 nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
2420 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
2422 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
2423 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
2424 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
2426 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
2427 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
2428 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
2432 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2433 * @nn: NFP Net device to reconfigure
2435 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
2437 u32 new_ctrl, update;
2438 unsigned int r;
2439 int err;
2441 new_ctrl = nn->dp.ctrl;
2442 new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
2443 update = NFP_NET_CFG_UPDATE_GEN;
2444 update |= NFP_NET_CFG_UPDATE_MSIX;
2445 update |= NFP_NET_CFG_UPDATE_RING;
2447 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2448 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
2450 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2451 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2453 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2454 err = nfp_net_reconfig(nn, update);
2455 if (err)
2456 nn_err(nn, "Could not disable device: %d\n", err);
2458 for (r = 0; r < nn->dp.num_rx_rings; r++)
2459 nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
2460 for (r = 0; r < nn->dp.num_tx_rings; r++)
2461 nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
2462 for (r = 0; r < nn->dp.num_r_vecs; r++)
2463 nfp_net_vec_clear_ring_data(nn, r);
2465 nn->dp.ctrl = new_ctrl;
2468 static void
2469 nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
2470 struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2472 /* Write the DMA address, size and MSI-X info to the device */
2473 nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
2474 nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2475 nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2478 static void
2479 nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
2480 struct nfp_net_tx_ring *tx_ring, unsigned int idx)
2482 nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
2483 nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2484 nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2488 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2489 * @nn: NFP Net device to reconfigure
2491 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2493 u32 bufsz, new_ctrl, update = 0;
2494 unsigned int r;
2495 int err;
2497 new_ctrl = nn->dp.ctrl;
2499 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
2500 nfp_net_rss_write_key(nn);
2501 nfp_net_rss_write_itbl(nn);
2502 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
2503 update |= NFP_NET_CFG_UPDATE_RSS;
2506 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
2507 nfp_net_coalesce_write_cfg(nn);
2508 update |= NFP_NET_CFG_UPDATE_IRQMOD;
2511 for (r = 0; r < nn->dp.num_tx_rings; r++)
2512 nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
2513 for (r = 0; r < nn->dp.num_rx_rings; r++)
2514 nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2516 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
2517 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2519 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
2520 0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2522 if (nn->dp.netdev)
2523 nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2525 nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
2527 bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
2528 nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
2530 /* Enable device */
2531 new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2532 update |= NFP_NET_CFG_UPDATE_GEN;
2533 update |= NFP_NET_CFG_UPDATE_MSIX;
2534 update |= NFP_NET_CFG_UPDATE_RING;
2535 if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2536 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2538 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2539 err = nfp_net_reconfig(nn, update);
2540 if (err) {
2541 nfp_net_clear_config_and_disable(nn);
2542 return err;
2545 nn->dp.ctrl = new_ctrl;
2547 for (r = 0; r < nn->dp.num_rx_rings; r++)
2548 nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2550 /* Since reconfiguration requests while NFP is down are ignored we
2551 * have to wipe the entire VXLAN configuration and reinitialize it.
2553 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2554 memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2555 memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2556 udp_tunnel_get_rx_info(nn->dp.netdev);
2559 return 0;
2563 * nfp_net_close_stack() - Quiesce the stack (part of close)
2564 * @nn: NFP Net device to reconfigure
2566 static void nfp_net_close_stack(struct nfp_net *nn)
2568 unsigned int r;
2570 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2571 netif_carrier_off(nn->dp.netdev);
2572 nn->link_up = false;
2574 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2575 disable_irq(nn->r_vecs[r].irq_vector);
2576 napi_disable(&nn->r_vecs[r].napi);
2579 netif_tx_disable(nn->dp.netdev);
2583 * nfp_net_close_free_all() - Free all runtime resources
2584 * @nn: NFP Net device to reconfigure
2586 static void nfp_net_close_free_all(struct nfp_net *nn)
2588 unsigned int r;
2590 nfp_net_tx_rings_free(&nn->dp);
2591 nfp_net_rx_rings_free(&nn->dp);
2593 for (r = 0; r < nn->dp.num_r_vecs; r++)
2594 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2596 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2597 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2601 * nfp_net_netdev_close() - Called when the device is downed
2602 * @netdev: netdev structure
2604 static int nfp_net_netdev_close(struct net_device *netdev)
2606 struct nfp_net *nn = netdev_priv(netdev);
2608 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2610 nfp_net_close_stack(nn);
2612 /* Step 2: Tell NFP
2614 nfp_net_clear_config_and_disable(nn);
2616 /* Step 3: Free resources
2618 nfp_net_close_free_all(nn);
2620 nn_dbg(nn, "%s down", netdev->name);
2621 return 0;
2624 void nfp_ctrl_close(struct nfp_net *nn)
2626 int r;
2628 rtnl_lock();
2630 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2631 disable_irq(nn->r_vecs[r].irq_vector);
2632 tasklet_disable(&nn->r_vecs[r].tasklet);
2635 nfp_net_clear_config_and_disable(nn);
2637 nfp_net_close_free_all(nn);
2639 rtnl_unlock();
2643 * nfp_net_open_stack() - Start the device from stack's perspective
2644 * @nn: NFP Net device to reconfigure
2646 static void nfp_net_open_stack(struct nfp_net *nn)
2648 unsigned int r;
2650 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2651 napi_enable(&nn->r_vecs[r].napi);
2652 enable_irq(nn->r_vecs[r].irq_vector);
2655 netif_tx_wake_all_queues(nn->dp.netdev);
2657 enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2658 nfp_net_read_link_status(nn);
2661 static int nfp_net_open_alloc_all(struct nfp_net *nn)
2663 int err, r;
2665 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2666 nn->exn_name, sizeof(nn->exn_name),
2667 NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2668 if (err)
2669 return err;
2670 err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2671 nn->lsc_name, sizeof(nn->lsc_name),
2672 NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2673 if (err)
2674 goto err_free_exn;
2675 disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2677 for (r = 0; r < nn->dp.num_r_vecs; r++) {
2678 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2679 if (err)
2680 goto err_cleanup_vec_p;
2683 err = nfp_net_rx_rings_prepare(nn, &nn->dp);
2684 if (err)
2685 goto err_cleanup_vec;
2687 err = nfp_net_tx_rings_prepare(nn, &nn->dp);
2688 if (err)
2689 goto err_free_rx_rings;
2691 for (r = 0; r < nn->max_r_vecs; r++)
2692 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2694 return 0;
2696 err_free_rx_rings:
2697 nfp_net_rx_rings_free(&nn->dp);
2698 err_cleanup_vec:
2699 r = nn->dp.num_r_vecs;
2700 err_cleanup_vec_p:
2701 while (r--)
2702 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2703 nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2704 err_free_exn:
2705 nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2706 return err;
2709 static int nfp_net_netdev_open(struct net_device *netdev)
2711 struct nfp_net *nn = netdev_priv(netdev);
2712 int err;
2714 /* Step 1: Allocate resources for rings and the like
2715 * - Request interrupts
2716 * - Allocate RX and TX ring resources
2717 * - Setup initial RSS table
2719 err = nfp_net_open_alloc_all(nn);
2720 if (err)
2721 return err;
2723 err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
2724 if (err)
2725 goto err_free_all;
2727 err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
2728 if (err)
2729 goto err_free_all;
2731 /* Step 2: Configure the NFP
2732 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2733 * - Write MAC address (in case it changed)
2734 * - Set the MTU
2735 * - Set the Freelist buffer size
2736 * - Enable the FW
2738 err = nfp_net_set_config_and_enable(nn);
2739 if (err)
2740 goto err_free_all;
2742 /* Step 3: Enable for kernel
2743 * - put some freelist descriptors on each RX ring
2744 * - enable NAPI on each ring
2745 * - enable all TX queues
2746 * - set link state
2748 nfp_net_open_stack(nn);
2750 return 0;
2752 err_free_all:
2753 nfp_net_close_free_all(nn);
2754 return err;
2757 int nfp_ctrl_open(struct nfp_net *nn)
2759 int err, r;
2761 /* ring dumping depends on vNICs being opened/closed under rtnl */
2762 rtnl_lock();
2764 err = nfp_net_open_alloc_all(nn);
2765 if (err)
2766 goto err_unlock;
2768 err = nfp_net_set_config_and_enable(nn);
2769 if (err)
2770 goto err_free_all;
2772 for (r = 0; r < nn->dp.num_r_vecs; r++)
2773 enable_irq(nn->r_vecs[r].irq_vector);
2775 rtnl_unlock();
2777 return 0;
2779 err_free_all:
2780 nfp_net_close_free_all(nn);
2781 err_unlock:
2782 rtnl_unlock();
2783 return err;
2786 static void nfp_net_set_rx_mode(struct net_device *netdev)
2788 struct nfp_net *nn = netdev_priv(netdev);
2789 u32 new_ctrl;
2791 new_ctrl = nn->dp.ctrl;
2793 if (netdev->flags & IFF_PROMISC) {
2794 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2795 new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2796 else
2797 nn_warn(nn, "FW does not support promiscuous mode\n");
2798 } else {
2799 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2802 if (new_ctrl == nn->dp.ctrl)
2803 return;
2805 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2806 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2808 nn->dp.ctrl = new_ctrl;
2811 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
2813 int i;
2815 for (i = 0; i < sizeof(nn->rss_itbl); i++)
2816 nn->rss_itbl[i] =
2817 ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
2820 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
2822 struct nfp_net_dp new_dp = *dp;
2824 *dp = nn->dp;
2825 nn->dp = new_dp;
2827 nn->dp.netdev->mtu = new_dp.mtu;
2829 if (!netif_is_rxfh_configured(nn->dp.netdev))
2830 nfp_net_rss_init_itbl(nn);
2833 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
2835 unsigned int r;
2836 int err;
2838 nfp_net_dp_swap(nn, dp);
2840 for (r = 0; r < nn->max_r_vecs; r++)
2841 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2843 err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
2844 if (err)
2845 return err;
2847 if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
2848 err = netif_set_real_num_tx_queues(nn->dp.netdev,
2849 nn->dp.num_stack_tx_rings);
2850 if (err)
2851 return err;
2854 return nfp_net_set_config_and_enable(nn);
2857 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
2859 struct nfp_net_dp *new;
2861 new = kmalloc(sizeof(*new), GFP_KERNEL);
2862 if (!new)
2863 return NULL;
2865 *new = nn->dp;
2867 /* Clear things which need to be recomputed */
2868 new->fl_bufsz = 0;
2869 new->tx_rings = NULL;
2870 new->rx_rings = NULL;
2871 new->num_r_vecs = 0;
2872 new->num_stack_tx_rings = 0;
2874 return new;
2877 static int
2878 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
2879 struct netlink_ext_ack *extack)
2881 /* XDP-enabled tests */
2882 if (!dp->xdp_prog)
2883 return 0;
2884 if (dp->fl_bufsz > PAGE_SIZE) {
2885 NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
2886 return -EINVAL;
2888 if (dp->num_tx_rings > nn->max_tx_rings) {
2889 NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
2890 return -EINVAL;
2893 return 0;
2896 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
2897 struct netlink_ext_ack *extack)
2899 int r, err;
2901 dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
2903 dp->num_stack_tx_rings = dp->num_tx_rings;
2904 if (dp->xdp_prog)
2905 dp->num_stack_tx_rings -= dp->num_rx_rings;
2907 dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
2909 err = nfp_net_check_config(nn, dp, extack);
2910 if (err)
2911 goto exit_free_dp;
2913 if (!netif_running(dp->netdev)) {
2914 nfp_net_dp_swap(nn, dp);
2915 err = 0;
2916 goto exit_free_dp;
2919 /* Prepare new rings */
2920 for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
2921 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2922 if (err) {
2923 dp->num_r_vecs = r;
2924 goto err_cleanup_vecs;
2928 err = nfp_net_rx_rings_prepare(nn, dp);
2929 if (err)
2930 goto err_cleanup_vecs;
2932 err = nfp_net_tx_rings_prepare(nn, dp);
2933 if (err)
2934 goto err_free_rx;
2936 /* Stop device, swap in new rings, try to start the firmware */
2937 nfp_net_close_stack(nn);
2938 nfp_net_clear_config_and_disable(nn);
2940 err = nfp_net_dp_swap_enable(nn, dp);
2941 if (err) {
2942 int err2;
2944 nfp_net_clear_config_and_disable(nn);
2946 /* Try with old configuration and old rings */
2947 err2 = nfp_net_dp_swap_enable(nn, dp);
2948 if (err2)
2949 nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
2950 err, err2);
2952 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
2953 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2955 nfp_net_rx_rings_free(dp);
2956 nfp_net_tx_rings_free(dp);
2958 nfp_net_open_stack(nn);
2959 exit_free_dp:
2960 kfree(dp);
2962 return err;
2964 err_free_rx:
2965 nfp_net_rx_rings_free(dp);
2966 err_cleanup_vecs:
2967 for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
2968 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2969 kfree(dp);
2970 return err;
2973 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
2975 struct nfp_net *nn = netdev_priv(netdev);
2976 struct nfp_net_dp *dp;
2978 dp = nfp_net_clone_dp(nn);
2979 if (!dp)
2980 return -ENOMEM;
2982 dp->mtu = new_mtu;
2984 return nfp_net_ring_reconfig(nn, dp, NULL);
2987 static int
2988 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
2990 struct nfp_net *nn = netdev_priv(netdev);
2992 /* Priority tagged packets with vlan id 0 are processed by the
2993 * NFP as untagged packets
2995 if (!vid)
2996 return 0;
2998 nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_VID, vid);
2999 nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_PROTO, ETH_P_8021Q);
3001 return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD);
3004 static int
3005 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3007 struct nfp_net *nn = netdev_priv(netdev);
3009 /* Priority tagged packets with vlan id 0 are processed by the
3010 * NFP as untagged packets
3012 if (!vid)
3013 return 0;
3015 nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_VID, vid);
3016 nn_writew(nn, NFP_NET_CFG_VLAN_FILTER_PROTO, ETH_P_8021Q);
3018 return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL);
3021 static void nfp_net_stat64(struct net_device *netdev,
3022 struct rtnl_link_stats64 *stats)
3024 struct nfp_net *nn = netdev_priv(netdev);
3025 int r;
3027 for (r = 0; r < nn->dp.num_r_vecs; r++) {
3028 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
3029 u64 data[3];
3030 unsigned int start;
3032 do {
3033 start = u64_stats_fetch_begin(&r_vec->rx_sync);
3034 data[0] = r_vec->rx_pkts;
3035 data[1] = r_vec->rx_bytes;
3036 data[2] = r_vec->rx_drops;
3037 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
3038 stats->rx_packets += data[0];
3039 stats->rx_bytes += data[1];
3040 stats->rx_dropped += data[2];
3042 do {
3043 start = u64_stats_fetch_begin(&r_vec->tx_sync);
3044 data[0] = r_vec->tx_pkts;
3045 data[1] = r_vec->tx_bytes;
3046 data[2] = r_vec->tx_errors;
3047 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
3048 stats->tx_packets += data[0];
3049 stats->tx_bytes += data[1];
3050 stats->tx_errors += data[2];
3054 static int
3055 nfp_net_setup_tc(struct net_device *netdev, u32 handle, u32 chain_index,
3056 __be16 proto, struct tc_to_netdev *tc)
3058 struct nfp_net *nn = netdev_priv(netdev);
3060 if (chain_index)
3061 return -EOPNOTSUPP;
3063 return nfp_app_setup_tc(nn->app, netdev, handle, proto, tc);
3066 static int nfp_net_set_features(struct net_device *netdev,
3067 netdev_features_t features)
3069 netdev_features_t changed = netdev->features ^ features;
3070 struct nfp_net *nn = netdev_priv(netdev);
3071 u32 new_ctrl;
3072 int err;
3074 /* Assume this is not called with features we have not advertised */
3076 new_ctrl = nn->dp.ctrl;
3078 if (changed & NETIF_F_RXCSUM) {
3079 if (features & NETIF_F_RXCSUM)
3080 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3081 else
3082 new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
3085 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3086 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
3087 new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3088 else
3089 new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
3092 if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
3093 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
3094 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3095 NFP_NET_CFG_CTRL_LSO;
3096 else
3097 new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3100 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3101 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3102 new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3103 else
3104 new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
3107 if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
3108 if (features & NETIF_F_HW_VLAN_CTAG_TX)
3109 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3110 else
3111 new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
3114 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
3115 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
3116 new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3117 else
3118 new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
3121 if (changed & NETIF_F_SG) {
3122 if (features & NETIF_F_SG)
3123 new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
3124 else
3125 new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
3128 if (changed & NETIF_F_HW_TC && nfp_app_tc_busy(nn->app, nn)) {
3129 nn_err(nn, "Cannot disable HW TC offload while in use\n");
3130 return -EBUSY;
3133 nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
3134 netdev->features, features, changed);
3136 if (new_ctrl == nn->dp.ctrl)
3137 return 0;
3139 nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
3140 nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
3141 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
3142 if (err)
3143 return err;
3145 nn->dp.ctrl = new_ctrl;
3147 return 0;
3150 static netdev_features_t
3151 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
3152 netdev_features_t features)
3154 u8 l4_hdr;
3156 /* We can't do TSO over double tagged packets (802.1AD) */
3157 features &= vlan_features_check(skb, features);
3159 if (!skb->encapsulation)
3160 return features;
3162 /* Ensure that inner L4 header offset fits into TX descriptor field */
3163 if (skb_is_gso(skb)) {
3164 u32 hdrlen;
3166 hdrlen = skb_inner_transport_header(skb) - skb->data +
3167 inner_tcp_hdrlen(skb);
3169 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
3170 features &= ~NETIF_F_GSO_MASK;
3173 /* VXLAN/GRE check */
3174 switch (vlan_get_protocol(skb)) {
3175 case htons(ETH_P_IP):
3176 l4_hdr = ip_hdr(skb)->protocol;
3177 break;
3178 case htons(ETH_P_IPV6):
3179 l4_hdr = ipv6_hdr(skb)->nexthdr;
3180 break;
3181 default:
3182 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3185 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
3186 skb->inner_protocol != htons(ETH_P_TEB) ||
3187 (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
3188 (l4_hdr == IPPROTO_UDP &&
3189 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
3190 sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
3191 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3193 return features;
3197 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
3198 * @nn: NFP Net device to reconfigure
3199 * @idx: Index into the port table where new port should be written
3200 * @port: UDP port to configure (pass zero to remove VXLAN port)
3202 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
3204 int i;
3206 nn->vxlan_ports[idx] = port;
3208 if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
3209 return;
3211 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
3212 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
3213 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
3214 be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
3215 be16_to_cpu(nn->vxlan_ports[i]));
3217 nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
3221 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
3222 * @nn: NFP Network structure
3223 * @port: UDP port to look for
3225 * Return: if the port is already in the table -- it's position;
3226 * if the port is not in the table -- free position to use;
3227 * if the table is full -- -ENOSPC.
3229 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
3231 int i, free_idx = -ENOSPC;
3233 for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
3234 if (nn->vxlan_ports[i] == port)
3235 return i;
3236 if (!nn->vxlan_usecnt[i])
3237 free_idx = i;
3240 return free_idx;
3243 static void nfp_net_add_vxlan_port(struct net_device *netdev,
3244 struct udp_tunnel_info *ti)
3246 struct nfp_net *nn = netdev_priv(netdev);
3247 int idx;
3249 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3250 return;
3252 idx = nfp_net_find_vxlan_idx(nn, ti->port);
3253 if (idx == -ENOSPC)
3254 return;
3256 if (!nn->vxlan_usecnt[idx]++)
3257 nfp_net_set_vxlan_port(nn, idx, ti->port);
3260 static void nfp_net_del_vxlan_port(struct net_device *netdev,
3261 struct udp_tunnel_info *ti)
3263 struct nfp_net *nn = netdev_priv(netdev);
3264 int idx;
3266 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3267 return;
3269 idx = nfp_net_find_vxlan_idx(nn, ti->port);
3270 if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
3271 return;
3273 if (!--nn->vxlan_usecnt[idx])
3274 nfp_net_set_vxlan_port(nn, idx, 0);
3277 static int nfp_net_xdp_setup(struct nfp_net *nn, struct netdev_xdp *xdp)
3279 struct bpf_prog *old_prog = nn->dp.xdp_prog;
3280 struct bpf_prog *prog = xdp->prog;
3281 struct nfp_net_dp *dp;
3282 int err;
3284 if (!prog && !nn->dp.xdp_prog)
3285 return 0;
3286 if (prog && nn->dp.xdp_prog) {
3287 prog = xchg(&nn->dp.xdp_prog, prog);
3288 bpf_prog_put(prog);
3289 nfp_app_xdp_offload(nn->app, nn, nn->dp.xdp_prog);
3290 return 0;
3293 dp = nfp_net_clone_dp(nn);
3294 if (!dp)
3295 return -ENOMEM;
3297 dp->xdp_prog = prog;
3298 dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
3299 dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
3300 dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
3302 /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
3303 err = nfp_net_ring_reconfig(nn, dp, xdp->extack);
3304 if (err)
3305 return err;
3307 if (old_prog)
3308 bpf_prog_put(old_prog);
3310 nfp_app_xdp_offload(nn->app, nn, nn->dp.xdp_prog);
3312 return 0;
3315 static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
3317 struct nfp_net *nn = netdev_priv(netdev);
3319 switch (xdp->command) {
3320 case XDP_SETUP_PROG:
3321 return nfp_net_xdp_setup(nn, xdp);
3322 case XDP_QUERY_PROG:
3323 xdp->prog_attached = !!nn->dp.xdp_prog;
3324 xdp->prog_id = nn->dp.xdp_prog ? nn->dp.xdp_prog->aux->id : 0;
3325 return 0;
3326 default:
3327 return -EINVAL;
3331 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
3333 struct nfp_net *nn = netdev_priv(netdev);
3334 struct sockaddr *saddr = addr;
3335 int err;
3337 err = eth_prepare_mac_addr_change(netdev, addr);
3338 if (err)
3339 return err;
3341 nfp_net_write_mac_addr(nn, saddr->sa_data);
3343 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
3344 if (err)
3345 return err;
3347 eth_commit_mac_addr_change(netdev, addr);
3349 return 0;
3352 const struct net_device_ops nfp_net_netdev_ops = {
3353 .ndo_open = nfp_net_netdev_open,
3354 .ndo_stop = nfp_net_netdev_close,
3355 .ndo_start_xmit = nfp_net_tx,
3356 .ndo_get_stats64 = nfp_net_stat64,
3357 .ndo_vlan_rx_add_vid = nfp_net_vlan_rx_add_vid,
3358 .ndo_vlan_rx_kill_vid = nfp_net_vlan_rx_kill_vid,
3359 .ndo_setup_tc = nfp_net_setup_tc,
3360 .ndo_tx_timeout = nfp_net_tx_timeout,
3361 .ndo_set_rx_mode = nfp_net_set_rx_mode,
3362 .ndo_change_mtu = nfp_net_change_mtu,
3363 .ndo_set_mac_address = nfp_net_set_mac_address,
3364 .ndo_set_features = nfp_net_set_features,
3365 .ndo_features_check = nfp_net_features_check,
3366 .ndo_get_phys_port_name = nfp_port_get_phys_port_name,
3367 .ndo_udp_tunnel_add = nfp_net_add_vxlan_port,
3368 .ndo_udp_tunnel_del = nfp_net_del_vxlan_port,
3369 .ndo_xdp = nfp_net_xdp,
3373 * nfp_net_info() - Print general info about the NIC
3374 * @nn: NFP Net device to reconfigure
3376 void nfp_net_info(struct nfp_net *nn)
3378 nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3379 nn->dp.is_vf ? "VF " : "",
3380 nn->dp.num_tx_rings, nn->max_tx_rings,
3381 nn->dp.num_rx_rings, nn->max_rx_rings);
3382 nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
3383 nn->fw_ver.resv, nn->fw_ver.class,
3384 nn->fw_ver.major, nn->fw_ver.minor,
3385 nn->max_mtu);
3386 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",
3387 nn->cap,
3388 nn->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
3389 nn->cap & NFP_NET_CFG_CTRL_L2BC ? "L2BCFILT " : "",
3390 nn->cap & NFP_NET_CFG_CTRL_L2MC ? "L2MCFILT " : "",
3391 nn->cap & NFP_NET_CFG_CTRL_RXCSUM ? "RXCSUM " : "",
3392 nn->cap & NFP_NET_CFG_CTRL_TXCSUM ? "TXCSUM " : "",
3393 nn->cap & NFP_NET_CFG_CTRL_RXVLAN ? "RXVLAN " : "",
3394 nn->cap & NFP_NET_CFG_CTRL_TXVLAN ? "TXVLAN " : "",
3395 nn->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
3396 nn->cap & NFP_NET_CFG_CTRL_GATHER ? "GATHER " : "",
3397 nn->cap & NFP_NET_CFG_CTRL_LSO ? "TSO1 " : "",
3398 nn->cap & NFP_NET_CFG_CTRL_LSO2 ? "TSO2 " : "",
3399 nn->cap & NFP_NET_CFG_CTRL_RSS ? "RSS1 " : "",
3400 nn->cap & NFP_NET_CFG_CTRL_RSS2 ? "RSS2 " : "",
3401 nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
3402 nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
3403 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
3404 nn->cap & NFP_NET_CFG_CTRL_IRQMOD ? "IRQMOD " : "",
3405 nn->cap & NFP_NET_CFG_CTRL_VXLAN ? "VXLAN " : "",
3406 nn->cap & NFP_NET_CFG_CTRL_NVGRE ? "NVGRE " : "",
3407 nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
3408 "RXCSUM_COMPLETE " : "",
3409 nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
3410 nfp_app_extra_cap(nn->app, nn));
3414 * nfp_net_alloc() - Allocate netdev and related structure
3415 * @pdev: PCI device
3416 * @needs_netdev: Whether to allocate a netdev for this vNIC
3417 * @max_tx_rings: Maximum number of TX rings supported by device
3418 * @max_rx_rings: Maximum number of RX rings supported by device
3420 * This function allocates a netdev device and fills in the initial
3421 * part of the @struct nfp_net structure. In case of control device
3422 * nfp_net structure is allocated without the netdev.
3424 * Return: NFP Net device structure, or ERR_PTR on error.
3426 struct nfp_net *nfp_net_alloc(struct pci_dev *pdev, bool needs_netdev,
3427 unsigned int max_tx_rings,
3428 unsigned int max_rx_rings)
3430 struct nfp_net *nn;
3432 if (needs_netdev) {
3433 struct net_device *netdev;
3435 netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
3436 max_tx_rings, max_rx_rings);
3437 if (!netdev)
3438 return ERR_PTR(-ENOMEM);
3440 SET_NETDEV_DEV(netdev, &pdev->dev);
3441 nn = netdev_priv(netdev);
3442 nn->dp.netdev = netdev;
3443 } else {
3444 nn = vzalloc(sizeof(*nn));
3445 if (!nn)
3446 return ERR_PTR(-ENOMEM);
3449 nn->dp.dev = &pdev->dev;
3450 nn->pdev = pdev;
3452 nn->max_tx_rings = max_tx_rings;
3453 nn->max_rx_rings = max_rx_rings;
3455 nn->dp.num_tx_rings = min_t(unsigned int,
3456 max_tx_rings, num_online_cpus());
3457 nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3458 netif_get_num_default_rss_queues());
3460 nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
3461 nn->dp.num_r_vecs = min_t(unsigned int,
3462 nn->dp.num_r_vecs, num_online_cpus());
3464 nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
3465 nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3467 spin_lock_init(&nn->reconfig_lock);
3468 spin_lock_init(&nn->link_status_lock);
3470 setup_timer(&nn->reconfig_timer,
3471 nfp_net_reconfig_timer, (unsigned long)nn);
3473 return nn;
3477 * nfp_net_free() - Undo what @nfp_net_alloc() did
3478 * @nn: NFP Net device to reconfigure
3480 void nfp_net_free(struct nfp_net *nn)
3482 if (nn->dp.netdev)
3483 free_netdev(nn->dp.netdev);
3484 else
3485 vfree(nn);
3489 * nfp_net_rss_key_sz() - Get current size of the RSS key
3490 * @nn: NFP Net device instance
3492 * Return: size of the RSS key for currently selected hash function.
3494 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
3496 switch (nn->rss_hfunc) {
3497 case ETH_RSS_HASH_TOP:
3498 return NFP_NET_CFG_RSS_KEY_SZ;
3499 case ETH_RSS_HASH_XOR:
3500 return 0;
3501 case ETH_RSS_HASH_CRC32:
3502 return 4;
3505 nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
3506 return 0;
3510 * nfp_net_rss_init() - Set the initial RSS parameters
3511 * @nn: NFP Net device to reconfigure
3513 static void nfp_net_rss_init(struct nfp_net *nn)
3515 unsigned long func_bit, rss_cap_hfunc;
3516 u32 reg;
3518 /* Read the RSS function capability and select first supported func */
3519 reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
3520 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
3521 if (!rss_cap_hfunc)
3522 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
3523 NFP_NET_CFG_RSS_TOEPLITZ);
3525 func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
3526 if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3527 dev_warn(nn->dp.dev,
3528 "Bad RSS config, defaulting to Toeplitz hash\n");
3529 func_bit = ETH_RSS_HASH_TOP_BIT;
3531 nn->rss_hfunc = 1 << func_bit;
3533 netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3535 nfp_net_rss_init_itbl(nn);
3537 /* Enable IPv4/IPv6 TCP by default */
3538 nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
3539 NFP_NET_CFG_RSS_IPV6_TCP |
3540 FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3541 NFP_NET_CFG_RSS_MASK;
3545 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3546 * @nn: NFP Net device to reconfigure
3548 static void nfp_net_irqmod_init(struct nfp_net *nn)
3550 nn->rx_coalesce_usecs = 50;
3551 nn->rx_coalesce_max_frames = 64;
3552 nn->tx_coalesce_usecs = 50;
3553 nn->tx_coalesce_max_frames = 64;
3556 static void nfp_net_netdev_init(struct nfp_net *nn)
3558 struct net_device *netdev = nn->dp.netdev;
3560 nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
3562 netdev->mtu = nn->dp.mtu;
3564 /* Advertise/enable offloads based on capabilities
3566 * Note: netdev->features show the currently enabled features
3567 * and netdev->hw_features advertises which features are
3568 * supported. By default we enable most features.
3570 if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
3571 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3573 netdev->hw_features = NETIF_F_HIGHDMA;
3574 if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
3575 netdev->hw_features |= NETIF_F_RXCSUM;
3576 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3578 if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
3579 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3580 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3582 if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
3583 netdev->hw_features |= NETIF_F_SG;
3584 nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
3586 if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
3587 nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3588 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3589 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3590 NFP_NET_CFG_CTRL_LSO;
3592 if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
3593 netdev->hw_features |= NETIF_F_RXHASH;
3594 if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
3595 nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
3596 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3597 netdev->hw_features |= NETIF_F_GSO_GRE |
3598 NETIF_F_GSO_UDP_TUNNEL;
3599 nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
3601 netdev->hw_enc_features = netdev->hw_features;
3604 netdev->vlan_features = netdev->hw_features;
3606 if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
3607 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3608 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3610 if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
3611 if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3612 nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
3613 } else {
3614 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3615 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3618 if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
3619 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3620 nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3623 netdev->features = netdev->hw_features;
3625 if (nfp_app_has_tc(nn->app))
3626 netdev->hw_features |= NETIF_F_HW_TC;
3628 /* Advertise but disable TSO by default. */
3629 netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3630 nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3632 /* Finalise the netdev setup */
3633 netdev->netdev_ops = &nfp_net_netdev_ops;
3634 netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
3636 /* MTU range: 68 - hw-specific max */
3637 netdev->min_mtu = ETH_MIN_MTU;
3638 netdev->max_mtu = nn->max_mtu;
3640 netif_carrier_off(netdev);
3642 nfp_net_set_ethtool_ops(netdev);
3646 * nfp_net_init() - Initialise/finalise the nfp_net structure
3647 * @nn: NFP Net device structure
3649 * Return: 0 on success or negative errno on error.
3651 int nfp_net_init(struct nfp_net *nn)
3653 int err;
3655 nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
3657 /* Get some of the read-only fields from the BAR */
3658 nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
3659 nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
3661 /* Chained metadata is signalled by capabilities except in version 4 */
3662 nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
3663 !nn->dp.netdev ||
3664 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
3665 if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
3666 nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
3668 /* Determine RX packet/metadata boundary offset */
3669 if (nn->fw_ver.major >= 2) {
3670 u32 reg;
3672 reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
3673 if (reg > NFP_NET_MAX_PREPEND) {
3674 nn_err(nn, "Invalid rx offset: %d\n", reg);
3675 return -EINVAL;
3677 nn->dp.rx_offset = reg;
3678 } else {
3679 nn->dp.rx_offset = NFP_NET_RX_OFFSET;
3682 /* Set default MTU and Freelist buffer size */
3683 if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
3684 nn->dp.mtu = nn->max_mtu;
3685 else
3686 nn->dp.mtu = NFP_NET_DEFAULT_MTU;
3687 nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
3689 if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
3690 nfp_net_rss_init(nn);
3691 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
3692 NFP_NET_CFG_CTRL_RSS;
3695 /* Allow L2 Broadcast and Multicast through by default, if supported */
3696 if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
3697 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
3698 if (nn->cap & NFP_NET_CFG_CTRL_L2MC)
3699 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2MC;
3701 /* Allow IRQ moderation, if supported */
3702 if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
3703 nfp_net_irqmod_init(nn);
3704 nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
3707 if (nn->dp.netdev)
3708 nfp_net_netdev_init(nn);
3710 /* Stash the re-configuration queue away. First odd queue in TX Bar */
3711 nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
3713 /* Make sure the FW knows the netdev is supposed to be disabled here */
3714 nn_writel(nn, NFP_NET_CFG_CTRL, 0);
3715 nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
3716 nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
3717 err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
3718 NFP_NET_CFG_UPDATE_GEN);
3719 if (err)
3720 return err;
3722 nfp_net_vecs_init(nn);
3724 if (!nn->dp.netdev)
3725 return 0;
3726 return register_netdev(nn->dp.netdev);
3730 * nfp_net_clean() - Undo what nfp_net_init() did.
3731 * @nn: NFP Net device structure
3733 void nfp_net_clean(struct nfp_net *nn)
3735 if (!nn->dp.netdev)
3736 return;
3738 unregister_netdev(nn->dp.netdev);
3740 if (nn->dp.xdp_prog)
3741 bpf_prog_put(nn->dp.xdp_prog);