2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * Netronome network device driver: Common functions between PF and VF
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
40 * Brad Petrus <brad.petrus@netronome.com>
41 * Chris Telfer <chris.telfer@netronome.com>
44 #include <linux/bitfield.h>
45 #include <linux/bpf.h>
46 #include <linux/bpf_trace.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/interrupt.h>
55 #include <linux/ipv6.h>
56 #include <linux/page_ref.h>
57 #include <linux/pci.h>
58 #include <linux/pci_regs.h>
59 #include <linux/msi.h>
60 #include <linux/ethtool.h>
61 #include <linux/log2.h>
62 #include <linux/if_vlan.h>
63 #include <linux/random.h>
64 #include <linux/vmalloc.h>
65 #include <linux/ktime.h>
67 #include <net/switchdev.h>
68 #include <net/vxlan.h>
70 #include "nfpcore/nfp_nsp.h"
72 #include "nfp_net_ctrl.h"
77 * nfp_net_get_fw_version() - Read and parse the FW version
78 * @fw_ver: Output fw_version structure to read to
79 * @ctrl_bar: Mapped address of the control BAR
81 void nfp_net_get_fw_version(struct nfp_net_fw_version
*fw_ver
,
82 void __iomem
*ctrl_bar
)
86 reg
= readl(ctrl_bar
+ NFP_NET_CFG_VERSION
);
87 put_unaligned_le32(reg
, fw_ver
);
90 static dma_addr_t
nfp_net_dma_map_rx(struct nfp_net_dp
*dp
, void *frag
)
92 return dma_map_single_attrs(dp
->dev
, frag
+ NFP_NET_RX_BUF_HEADROOM
,
93 dp
->fl_bufsz
- NFP_NET_RX_BUF_NON_DATA
,
94 dp
->rx_dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
98 nfp_net_dma_sync_dev_rx(const struct nfp_net_dp
*dp
, dma_addr_t dma_addr
)
100 dma_sync_single_for_device(dp
->dev
, dma_addr
,
101 dp
->fl_bufsz
- NFP_NET_RX_BUF_NON_DATA
,
105 static void nfp_net_dma_unmap_rx(struct nfp_net_dp
*dp
, dma_addr_t dma_addr
)
107 dma_unmap_single_attrs(dp
->dev
, dma_addr
,
108 dp
->fl_bufsz
- NFP_NET_RX_BUF_NON_DATA
,
109 dp
->rx_dma_dir
, DMA_ATTR_SKIP_CPU_SYNC
);
112 static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp
*dp
, dma_addr_t dma_addr
,
115 dma_sync_single_for_cpu(dp
->dev
, dma_addr
- NFP_NET_RX_BUF_HEADROOM
,
116 len
, dp
->rx_dma_dir
);
121 * Firmware reconfig may take a while so we have two versions of it -
122 * synchronous and asynchronous (posted). All synchronous callers are holding
123 * RTNL so we don't have to worry about serializing them.
125 static void nfp_net_reconfig_start(struct nfp_net
*nn
, u32 update
)
127 nn_writel(nn
, NFP_NET_CFG_UPDATE
, update
);
128 /* ensure update is written before pinging HW */
130 nfp_qcp_wr_ptr_add(nn
->qcp_cfg
, 1);
133 /* Pass 0 as update to run posted reconfigs. */
134 static void nfp_net_reconfig_start_async(struct nfp_net
*nn
, u32 update
)
136 update
|= nn
->reconfig_posted
;
137 nn
->reconfig_posted
= 0;
139 nfp_net_reconfig_start(nn
, update
);
141 nn
->reconfig_timer_active
= true;
142 mod_timer(&nn
->reconfig_timer
, jiffies
+ NFP_NET_POLL_TIMEOUT
* HZ
);
145 static bool nfp_net_reconfig_check_done(struct nfp_net
*nn
, bool last_check
)
149 reg
= nn_readl(nn
, NFP_NET_CFG_UPDATE
);
152 if (reg
& NFP_NET_CFG_UPDATE_ERR
) {
153 nn_err(nn
, "Reconfig error: 0x%08x\n", reg
);
155 } else if (last_check
) {
156 nn_err(nn
, "Reconfig timeout: 0x%08x\n", reg
);
163 static int nfp_net_reconfig_wait(struct nfp_net
*nn
, unsigned long deadline
)
165 bool timed_out
= false;
167 /* Poll update field, waiting for NFP to ack the config */
168 while (!nfp_net_reconfig_check_done(nn
, timed_out
)) {
170 timed_out
= time_is_before_eq_jiffies(deadline
);
173 if (nn_readl(nn
, NFP_NET_CFG_UPDATE
) & NFP_NET_CFG_UPDATE_ERR
)
176 return timed_out
? -EIO
: 0;
179 static void nfp_net_reconfig_timer(unsigned long data
)
181 struct nfp_net
*nn
= (void *)data
;
183 spin_lock_bh(&nn
->reconfig_lock
);
185 nn
->reconfig_timer_active
= false;
187 /* If sync caller is present it will take over from us */
188 if (nn
->reconfig_sync_present
)
191 /* Read reconfig status and report errors */
192 nfp_net_reconfig_check_done(nn
, true);
194 if (nn
->reconfig_posted
)
195 nfp_net_reconfig_start_async(nn
, 0);
197 spin_unlock_bh(&nn
->reconfig_lock
);
201 * nfp_net_reconfig_post() - Post async reconfig request
202 * @nn: NFP Net device to reconfigure
203 * @update: The value for the update field in the BAR config
205 * Record FW reconfiguration request. Reconfiguration will be kicked off
206 * whenever reconfiguration machinery is idle. Multiple requests can be
209 static void nfp_net_reconfig_post(struct nfp_net
*nn
, u32 update
)
211 spin_lock_bh(&nn
->reconfig_lock
);
213 /* Sync caller will kick off async reconf when it's done, just post */
214 if (nn
->reconfig_sync_present
) {
215 nn
->reconfig_posted
|= update
;
219 /* Opportunistically check if the previous command is done */
220 if (!nn
->reconfig_timer_active
||
221 nfp_net_reconfig_check_done(nn
, false))
222 nfp_net_reconfig_start_async(nn
, update
);
224 nn
->reconfig_posted
|= update
;
226 spin_unlock_bh(&nn
->reconfig_lock
);
230 * nfp_net_reconfig() - Reconfigure the firmware
231 * @nn: NFP Net device to reconfigure
232 * @update: The value for the update field in the BAR config
234 * Write the update word to the BAR and ping the reconfig queue. The
235 * poll until the firmware has acknowledged the update by zeroing the
238 * Return: Negative errno on error, 0 on success
240 int nfp_net_reconfig(struct nfp_net
*nn
, u32 update
)
242 bool cancelled_timer
= false;
243 u32 pre_posted_requests
;
246 spin_lock_bh(&nn
->reconfig_lock
);
248 nn
->reconfig_sync_present
= true;
250 if (nn
->reconfig_timer_active
) {
251 del_timer(&nn
->reconfig_timer
);
252 nn
->reconfig_timer_active
= false;
253 cancelled_timer
= true;
255 pre_posted_requests
= nn
->reconfig_posted
;
256 nn
->reconfig_posted
= 0;
258 spin_unlock_bh(&nn
->reconfig_lock
);
261 nfp_net_reconfig_wait(nn
, nn
->reconfig_timer
.expires
);
263 /* Run the posted reconfigs which were issued before we started */
264 if (pre_posted_requests
) {
265 nfp_net_reconfig_start(nn
, pre_posted_requests
);
266 nfp_net_reconfig_wait(nn
, jiffies
+ HZ
* NFP_NET_POLL_TIMEOUT
);
269 nfp_net_reconfig_start(nn
, update
);
270 ret
= nfp_net_reconfig_wait(nn
, jiffies
+ HZ
* NFP_NET_POLL_TIMEOUT
);
272 spin_lock_bh(&nn
->reconfig_lock
);
274 if (nn
->reconfig_posted
)
275 nfp_net_reconfig_start_async(nn
, 0);
277 nn
->reconfig_sync_present
= false;
279 spin_unlock_bh(&nn
->reconfig_lock
);
285 * nfp_net_reconfig_mbox() - Reconfigure the firmware via the mailbox
286 * @nn: NFP Net device to reconfigure
287 * @mbox_cmd: The value for the mailbox command
289 * Helper function for mailbox updates
291 * Return: Negative errno on error, 0 on success
293 static int nfp_net_reconfig_mbox(struct nfp_net
*nn
, u32 mbox_cmd
)
297 nn_writeq(nn
, NFP_NET_CFG_MBOX_CMD
, mbox_cmd
);
299 ret
= nfp_net_reconfig(nn
, NFP_NET_CFG_UPDATE_MBOX
);
301 nn_err(nn
, "Mailbox update error\n");
305 return -nn_readl(nn
, NFP_NET_CFG_MBOX_RET
);
308 /* Interrupt configuration and handling
312 * nfp_net_irq_unmask() - Unmask automasked interrupt
313 * @nn: NFP Network structure
314 * @entry_nr: MSI-X table entry
316 * Clear the ICR for the IRQ entry.
318 static void nfp_net_irq_unmask(struct nfp_net
*nn
, unsigned int entry_nr
)
320 nn_writeb(nn
, NFP_NET_CFG_ICR(entry_nr
), NFP_NET_CFG_ICR_UNMASKED
);
325 * nfp_net_irqs_alloc() - allocates MSI-X irqs
326 * @pdev: PCI device structure
327 * @irq_entries: Array to be initialized and used to hold the irq entries
328 * @min_irqs: Minimal acceptable number of interrupts
329 * @wanted_irqs: Target number of interrupts to allocate
331 * Return: Number of irqs obtained or 0 on error.
334 nfp_net_irqs_alloc(struct pci_dev
*pdev
, struct msix_entry
*irq_entries
,
335 unsigned int min_irqs
, unsigned int wanted_irqs
)
340 for (i
= 0; i
< wanted_irqs
; i
++)
341 irq_entries
[i
].entry
= i
;
343 got_irqs
= pci_enable_msix_range(pdev
, irq_entries
,
344 min_irqs
, wanted_irqs
);
346 dev_err(&pdev
->dev
, "Failed to enable %d-%d MSI-X (err=%d)\n",
347 min_irqs
, wanted_irqs
, got_irqs
);
351 if (got_irqs
< wanted_irqs
)
352 dev_warn(&pdev
->dev
, "Unable to allocate %d IRQs got only %d\n",
353 wanted_irqs
, got_irqs
);
359 * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
360 * @nn: NFP Network structure
361 * @irq_entries: Table of allocated interrupts
362 * @n: Size of @irq_entries (number of entries to grab)
364 * After interrupts are allocated with nfp_net_irqs_alloc() this function
365 * should be called to assign them to a specific netdev (port).
368 nfp_net_irqs_assign(struct nfp_net
*nn
, struct msix_entry
*irq_entries
,
371 struct nfp_net_dp
*dp
= &nn
->dp
;
373 nn
->max_r_vecs
= n
- NFP_NET_NON_Q_VECTORS
;
374 dp
->num_r_vecs
= nn
->max_r_vecs
;
376 memcpy(nn
->irq_entries
, irq_entries
, sizeof(*irq_entries
) * n
);
378 if (dp
->num_rx_rings
> dp
->num_r_vecs
||
379 dp
->num_tx_rings
> dp
->num_r_vecs
)
380 dev_warn(nn
->dp
.dev
, "More rings (%d,%d) than vectors (%d).\n",
381 dp
->num_rx_rings
, dp
->num_tx_rings
,
384 dp
->num_rx_rings
= min(dp
->num_r_vecs
, dp
->num_rx_rings
);
385 dp
->num_tx_rings
= min(dp
->num_r_vecs
, dp
->num_tx_rings
);
386 dp
->num_stack_tx_rings
= dp
->num_tx_rings
;
390 * nfp_net_irqs_disable() - Disable interrupts
391 * @pdev: PCI device structure
393 * Undoes what @nfp_net_irqs_alloc() does.
395 void nfp_net_irqs_disable(struct pci_dev
*pdev
)
397 pci_disable_msix(pdev
);
401 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
403 * @data: Opaque data structure
405 * Return: Indicate if the interrupt has been handled.
407 static irqreturn_t
nfp_net_irq_rxtx(int irq
, void *data
)
409 struct nfp_net_r_vector
*r_vec
= data
;
411 napi_schedule_irqoff(&r_vec
->napi
);
413 /* The FW auto-masks any interrupt, either via the MASK bit in
414 * the MSI-X table or via the per entry ICR field. So there
415 * is no need to disable interrupts here.
420 static irqreturn_t
nfp_ctrl_irq_rxtx(int irq
, void *data
)
422 struct nfp_net_r_vector
*r_vec
= data
;
424 tasklet_schedule(&r_vec
->tasklet
);
430 * nfp_net_read_link_status() - Reread link status from control BAR
431 * @nn: NFP Network structure
433 static void nfp_net_read_link_status(struct nfp_net
*nn
)
439 spin_lock_irqsave(&nn
->link_status_lock
, flags
);
441 sts
= nn_readl(nn
, NFP_NET_CFG_STS
);
442 link_up
= !!(sts
& NFP_NET_CFG_STS_LINK
);
444 if (nn
->link_up
== link_up
)
447 nn
->link_up
= link_up
;
449 set_bit(NFP_PORT_CHANGED
, &nn
->port
->flags
);
452 netif_carrier_on(nn
->dp
.netdev
);
453 netdev_info(nn
->dp
.netdev
, "NIC Link is Up\n");
455 netif_carrier_off(nn
->dp
.netdev
);
456 netdev_info(nn
->dp
.netdev
, "NIC Link is Down\n");
459 spin_unlock_irqrestore(&nn
->link_status_lock
, flags
);
463 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
465 * @data: Opaque data structure
467 * Return: Indicate if the interrupt has been handled.
469 static irqreturn_t
nfp_net_irq_lsc(int irq
, void *data
)
471 struct nfp_net
*nn
= data
;
472 struct msix_entry
*entry
;
474 entry
= &nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
];
476 nfp_net_read_link_status(nn
);
478 nfp_net_irq_unmask(nn
, entry
->entry
);
484 * nfp_net_irq_exn() - Interrupt service routine for exceptions
486 * @data: Opaque data structure
488 * Return: Indicate if the interrupt has been handled.
490 static irqreturn_t
nfp_net_irq_exn(int irq
, void *data
)
492 struct nfp_net
*nn
= data
;
494 nn_err(nn
, "%s: UNIMPLEMENTED.\n", __func__
);
495 /* XXX TO BE IMPLEMENTED */
500 * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
501 * @tx_ring: TX ring structure
502 * @r_vec: IRQ vector servicing this ring
504 * @is_xdp: Is this an XDP TX ring?
507 nfp_net_tx_ring_init(struct nfp_net_tx_ring
*tx_ring
,
508 struct nfp_net_r_vector
*r_vec
, unsigned int idx
,
511 struct nfp_net
*nn
= r_vec
->nfp_net
;
514 tx_ring
->r_vec
= r_vec
;
515 tx_ring
->is_xdp
= is_xdp
;
516 u64_stats_init(&tx_ring
->r_vec
->tx_sync
);
518 tx_ring
->qcidx
= tx_ring
->idx
* nn
->stride_tx
;
519 tx_ring
->qcp_q
= nn
->tx_bar
+ NFP_QCP_QUEUE_OFF(tx_ring
->qcidx
);
523 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
524 * @rx_ring: RX ring structure
525 * @r_vec: IRQ vector servicing this ring
529 nfp_net_rx_ring_init(struct nfp_net_rx_ring
*rx_ring
,
530 struct nfp_net_r_vector
*r_vec
, unsigned int idx
)
532 struct nfp_net
*nn
= r_vec
->nfp_net
;
535 rx_ring
->r_vec
= r_vec
;
536 u64_stats_init(&rx_ring
->r_vec
->rx_sync
);
538 rx_ring
->fl_qcidx
= rx_ring
->idx
* nn
->stride_rx
;
539 rx_ring
->qcp_fl
= nn
->rx_bar
+ NFP_QCP_QUEUE_OFF(rx_ring
->fl_qcidx
);
543 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
544 * @nn: NFP Network structure
545 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
546 * @format: printf-style format to construct the interrupt name
547 * @name: Pointer to allocated space for interrupt name
548 * @name_sz: Size of space for interrupt name
549 * @vector_idx: Index of MSI-X vector used for this interrupt
550 * @handler: IRQ handler to register for this interrupt
553 nfp_net_aux_irq_request(struct nfp_net
*nn
, u32 ctrl_offset
,
554 const char *format
, char *name
, size_t name_sz
,
555 unsigned int vector_idx
, irq_handler_t handler
)
557 struct msix_entry
*entry
;
560 entry
= &nn
->irq_entries
[vector_idx
];
562 snprintf(name
, name_sz
, format
, nfp_net_name(nn
));
563 err
= request_irq(entry
->vector
, handler
, 0, name
, nn
);
565 nn_err(nn
, "Failed to request IRQ %d (err=%d).\n",
569 nn_writeb(nn
, ctrl_offset
, entry
->entry
);
575 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
576 * @nn: NFP Network structure
577 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
578 * @vector_idx: Index of MSI-X vector used for this interrupt
580 static void nfp_net_aux_irq_free(struct nfp_net
*nn
, u32 ctrl_offset
,
581 unsigned int vector_idx
)
583 nn_writeb(nn
, ctrl_offset
, 0xff);
584 free_irq(nn
->irq_entries
[vector_idx
].vector
, nn
);
589 * One queue controller peripheral queue is used for transmit. The
590 * driver en-queues packets for transmit by advancing the write
591 * pointer. The device indicates that packets have transmitted by
592 * advancing the read pointer. The driver maintains a local copy of
593 * the read and write pointer in @struct nfp_net_tx_ring. The driver
594 * keeps @wr_p in sync with the queue controller write pointer and can
595 * determine how many packets have been transmitted by comparing its
596 * copy of the read pointer @rd_p with the read pointer maintained by
597 * the queue controller peripheral.
601 * nfp_net_tx_full() - Check if the TX ring is full
602 * @tx_ring: TX ring to check
603 * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
605 * This function checks, based on the *host copy* of read/write
606 * pointer if a given TX ring is full. The real TX queue may have
607 * some newly made available slots.
609 * Return: True if the ring is full.
611 static int nfp_net_tx_full(struct nfp_net_tx_ring
*tx_ring
, int dcnt
)
613 return (tx_ring
->wr_p
- tx_ring
->rd_p
) >= (tx_ring
->cnt
- dcnt
);
616 /* Wrappers for deciding when to stop and restart TX queues */
617 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring
*tx_ring
)
619 return !nfp_net_tx_full(tx_ring
, MAX_SKB_FRAGS
* 4);
622 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring
*tx_ring
)
624 return nfp_net_tx_full(tx_ring
, MAX_SKB_FRAGS
+ 1);
628 * nfp_net_tx_ring_stop() - stop tx ring
629 * @nd_q: netdev queue
630 * @tx_ring: driver tx queue structure
632 * Safely stop TX ring. Remember that while we are running .start_xmit()
633 * someone else may be cleaning the TX ring completions so we need to be
634 * extra careful here.
636 static void nfp_net_tx_ring_stop(struct netdev_queue
*nd_q
,
637 struct nfp_net_tx_ring
*tx_ring
)
639 netif_tx_stop_queue(nd_q
);
641 /* We can race with the TX completion out of NAPI so recheck */
643 if (unlikely(nfp_net_tx_ring_should_wake(tx_ring
)))
644 netif_tx_start_queue(nd_q
);
648 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
649 * @r_vec: per-ring structure
650 * @txbuf: Pointer to driver soft TX descriptor
651 * @txd: Pointer to HW TX descriptor
652 * @skb: Pointer to SKB
654 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
655 * Return error on packet header greater than maximum supported LSO header size.
657 static void nfp_net_tx_tso(struct nfp_net_r_vector
*r_vec
,
658 struct nfp_net_tx_buf
*txbuf
,
659 struct nfp_net_tx_desc
*txd
, struct sk_buff
*skb
)
664 if (!skb_is_gso(skb
))
667 if (!skb
->encapsulation
) {
668 txd
->l3_offset
= skb_network_offset(skb
);
669 txd
->l4_offset
= skb_transport_offset(skb
);
670 hdrlen
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
672 txd
->l3_offset
= skb_inner_network_offset(skb
);
673 txd
->l4_offset
= skb_inner_transport_offset(skb
);
674 hdrlen
= skb_inner_transport_header(skb
) - skb
->data
+
675 inner_tcp_hdrlen(skb
);
678 txbuf
->pkt_cnt
= skb_shinfo(skb
)->gso_segs
;
679 txbuf
->real_len
+= hdrlen
* (txbuf
->pkt_cnt
- 1);
681 mss
= skb_shinfo(skb
)->gso_size
& PCIE_DESC_TX_MSS_MASK
;
682 txd
->lso_hdrlen
= hdrlen
;
683 txd
->mss
= cpu_to_le16(mss
);
684 txd
->flags
|= PCIE_DESC_TX_LSO
;
686 u64_stats_update_begin(&r_vec
->tx_sync
);
688 u64_stats_update_end(&r_vec
->tx_sync
);
692 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
693 * @dp: NFP Net data path struct
694 * @r_vec: per-ring structure
695 * @txbuf: Pointer to driver soft TX descriptor
696 * @txd: Pointer to TX descriptor
697 * @skb: Pointer to SKB
699 * This function sets the TX checksum flags in the TX descriptor based
700 * on the configuration and the protocol of the packet to be transmitted.
702 static void nfp_net_tx_csum(struct nfp_net_dp
*dp
,
703 struct nfp_net_r_vector
*r_vec
,
704 struct nfp_net_tx_buf
*txbuf
,
705 struct nfp_net_tx_desc
*txd
, struct sk_buff
*skb
)
707 struct ipv6hdr
*ipv6h
;
711 if (!(dp
->ctrl
& NFP_NET_CFG_CTRL_TXCSUM
))
714 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
717 txd
->flags
|= PCIE_DESC_TX_CSUM
;
718 if (skb
->encapsulation
)
719 txd
->flags
|= PCIE_DESC_TX_ENCAP
;
721 iph
= skb
->encapsulation
? inner_ip_hdr(skb
) : ip_hdr(skb
);
722 ipv6h
= skb
->encapsulation
? inner_ipv6_hdr(skb
) : ipv6_hdr(skb
);
724 if (iph
->version
== 4) {
725 txd
->flags
|= PCIE_DESC_TX_IP4_CSUM
;
726 l4_hdr
= iph
->protocol
;
727 } else if (ipv6h
->version
== 6) {
728 l4_hdr
= ipv6h
->nexthdr
;
730 nn_dp_warn(dp
, "partial checksum but ipv=%x!\n", iph
->version
);
736 txd
->flags
|= PCIE_DESC_TX_TCP_CSUM
;
739 txd
->flags
|= PCIE_DESC_TX_UDP_CSUM
;
742 nn_dp_warn(dp
, "partial checksum but l4 proto=%x!\n", l4_hdr
);
746 u64_stats_update_begin(&r_vec
->tx_sync
);
747 if (skb
->encapsulation
)
748 r_vec
->hw_csum_tx_inner
+= txbuf
->pkt_cnt
;
750 r_vec
->hw_csum_tx
+= txbuf
->pkt_cnt
;
751 u64_stats_update_end(&r_vec
->tx_sync
);
754 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring
*tx_ring
)
757 nfp_qcp_wr_ptr_add(tx_ring
->qcp_q
, tx_ring
->wr_ptr_add
);
758 tx_ring
->wr_ptr_add
= 0;
761 static int nfp_net_prep_port_id(struct sk_buff
*skb
)
763 struct metadata_dst
*md_dst
= skb_metadata_dst(skb
);
768 if (unlikely(md_dst
->type
!= METADATA_HW_PORT_MUX
))
771 if (unlikely(skb_cow_head(skb
, 8)))
774 data
= skb_push(skb
, 8);
775 put_unaligned_be32(NFP_NET_META_PORTID
, data
);
776 put_unaligned_be32(md_dst
->u
.port_info
.port_id
, data
+ 4);
782 * nfp_net_tx() - Main transmit entry point
783 * @skb: SKB to transmit
784 * @netdev: netdev structure
786 * Return: NETDEV_TX_OK on success.
788 static int nfp_net_tx(struct sk_buff
*skb
, struct net_device
*netdev
)
790 struct nfp_net
*nn
= netdev_priv(netdev
);
791 const struct skb_frag_struct
*frag
;
792 struct nfp_net_tx_desc
*txd
, txdg
;
793 int f
, nr_frags
, wr_idx
, md_bytes
;
794 struct nfp_net_tx_ring
*tx_ring
;
795 struct nfp_net_r_vector
*r_vec
;
796 struct nfp_net_tx_buf
*txbuf
;
797 struct netdev_queue
*nd_q
;
798 struct nfp_net_dp
*dp
;
804 qidx
= skb_get_queue_mapping(skb
);
805 tx_ring
= &dp
->tx_rings
[qidx
];
806 r_vec
= tx_ring
->r_vec
;
807 nd_q
= netdev_get_tx_queue(dp
->netdev
, qidx
);
809 nr_frags
= skb_shinfo(skb
)->nr_frags
;
811 if (unlikely(nfp_net_tx_full(tx_ring
, nr_frags
+ 1))) {
812 nn_dp_warn(dp
, "TX ring %d busy. wrp=%u rdp=%u\n",
813 qidx
, tx_ring
->wr_p
, tx_ring
->rd_p
);
814 netif_tx_stop_queue(nd_q
);
815 nfp_net_tx_xmit_more_flush(tx_ring
);
816 u64_stats_update_begin(&r_vec
->tx_sync
);
818 u64_stats_update_end(&r_vec
->tx_sync
);
819 return NETDEV_TX_BUSY
;
822 md_bytes
= nfp_net_prep_port_id(skb
);
823 if (unlikely(md_bytes
< 0)) {
824 nfp_net_tx_xmit_more_flush(tx_ring
);
825 dev_kfree_skb_any(skb
);
829 /* Start with the head skbuf */
830 dma_addr
= dma_map_single(dp
->dev
, skb
->data
, skb_headlen(skb
),
832 if (dma_mapping_error(dp
->dev
, dma_addr
))
835 wr_idx
= D_IDX(tx_ring
, tx_ring
->wr_p
);
837 /* Stash the soft descriptor of the head then initialize it */
838 txbuf
= &tx_ring
->txbufs
[wr_idx
];
840 txbuf
->dma_addr
= dma_addr
;
843 txbuf
->real_len
= skb
->len
;
845 /* Build TX descriptor */
846 txd
= &tx_ring
->txds
[wr_idx
];
847 txd
->offset_eop
= (nr_frags
? 0 : PCIE_DESC_TX_EOP
) | md_bytes
;
848 txd
->dma_len
= cpu_to_le16(skb_headlen(skb
));
849 nfp_desc_set_dma_addr(txd
, dma_addr
);
850 txd
->data_len
= cpu_to_le16(skb
->len
);
856 /* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
857 nfp_net_tx_tso(r_vec
, txbuf
, txd
, skb
);
858 nfp_net_tx_csum(dp
, r_vec
, txbuf
, txd
, skb
);
859 if (skb_vlan_tag_present(skb
) && dp
->ctrl
& NFP_NET_CFG_CTRL_TXVLAN
) {
860 txd
->flags
|= PCIE_DESC_TX_VLAN
;
861 txd
->vlan
= cpu_to_le16(skb_vlan_tag_get(skb
));
866 /* all descs must match except for in addr, length and eop */
869 for (f
= 0; f
< nr_frags
; f
++) {
870 frag
= &skb_shinfo(skb
)->frags
[f
];
871 fsize
= skb_frag_size(frag
);
873 dma_addr
= skb_frag_dma_map(dp
->dev
, frag
, 0,
874 fsize
, DMA_TO_DEVICE
);
875 if (dma_mapping_error(dp
->dev
, dma_addr
))
878 wr_idx
= D_IDX(tx_ring
, wr_idx
+ 1);
879 tx_ring
->txbufs
[wr_idx
].skb
= skb
;
880 tx_ring
->txbufs
[wr_idx
].dma_addr
= dma_addr
;
881 tx_ring
->txbufs
[wr_idx
].fidx
= f
;
883 txd
= &tx_ring
->txds
[wr_idx
];
885 txd
->dma_len
= cpu_to_le16(fsize
);
886 nfp_desc_set_dma_addr(txd
, dma_addr
);
888 (f
== nr_frags
- 1) ? PCIE_DESC_TX_EOP
: 0;
891 u64_stats_update_begin(&r_vec
->tx_sync
);
893 u64_stats_update_end(&r_vec
->tx_sync
);
896 netdev_tx_sent_queue(nd_q
, txbuf
->real_len
);
898 skb_tx_timestamp(skb
);
900 tx_ring
->wr_p
+= nr_frags
+ 1;
901 if (nfp_net_tx_ring_should_stop(tx_ring
))
902 nfp_net_tx_ring_stop(nd_q
, tx_ring
);
904 tx_ring
->wr_ptr_add
+= nr_frags
+ 1;
905 if (!skb
->xmit_more
|| netif_xmit_stopped(nd_q
))
906 nfp_net_tx_xmit_more_flush(tx_ring
);
912 frag
= &skb_shinfo(skb
)->frags
[f
];
913 dma_unmap_page(dp
->dev
, tx_ring
->txbufs
[wr_idx
].dma_addr
,
914 skb_frag_size(frag
), DMA_TO_DEVICE
);
915 tx_ring
->txbufs
[wr_idx
].skb
= NULL
;
916 tx_ring
->txbufs
[wr_idx
].dma_addr
= 0;
917 tx_ring
->txbufs
[wr_idx
].fidx
= -2;
920 wr_idx
+= tx_ring
->cnt
;
922 dma_unmap_single(dp
->dev
, tx_ring
->txbufs
[wr_idx
].dma_addr
,
923 skb_headlen(skb
), DMA_TO_DEVICE
);
924 tx_ring
->txbufs
[wr_idx
].skb
= NULL
;
925 tx_ring
->txbufs
[wr_idx
].dma_addr
= 0;
926 tx_ring
->txbufs
[wr_idx
].fidx
= -2;
928 nn_dp_warn(dp
, "Failed to map DMA TX buffer\n");
929 nfp_net_tx_xmit_more_flush(tx_ring
);
930 u64_stats_update_begin(&r_vec
->tx_sync
);
932 u64_stats_update_end(&r_vec
->tx_sync
);
933 dev_kfree_skb_any(skb
);
938 * nfp_net_tx_complete() - Handled completed TX packets
939 * @tx_ring: TX ring structure
941 * Return: Number of completed TX descriptors
943 static void nfp_net_tx_complete(struct nfp_net_tx_ring
*tx_ring
)
945 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
946 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
947 const struct skb_frag_struct
*frag
;
948 struct netdev_queue
*nd_q
;
949 u32 done_pkts
= 0, done_bytes
= 0;
956 if (tx_ring
->wr_p
== tx_ring
->rd_p
)
959 /* Work out how many descriptors have been transmitted */
960 qcp_rd_p
= nfp_qcp_rd_ptr_read(tx_ring
->qcp_q
);
962 if (qcp_rd_p
== tx_ring
->qcp_rd_p
)
965 todo
= D_IDX(tx_ring
, qcp_rd_p
- tx_ring
->qcp_rd_p
);
968 idx
= D_IDX(tx_ring
, tx_ring
->rd_p
++);
970 skb
= tx_ring
->txbufs
[idx
].skb
;
974 nr_frags
= skb_shinfo(skb
)->nr_frags
;
975 fidx
= tx_ring
->txbufs
[idx
].fidx
;
979 dma_unmap_single(dp
->dev
, tx_ring
->txbufs
[idx
].dma_addr
,
980 skb_headlen(skb
), DMA_TO_DEVICE
);
982 done_pkts
+= tx_ring
->txbufs
[idx
].pkt_cnt
;
983 done_bytes
+= tx_ring
->txbufs
[idx
].real_len
;
986 frag
= &skb_shinfo(skb
)->frags
[fidx
];
987 dma_unmap_page(dp
->dev
, tx_ring
->txbufs
[idx
].dma_addr
,
988 skb_frag_size(frag
), DMA_TO_DEVICE
);
991 /* check for last gather fragment */
992 if (fidx
== nr_frags
- 1)
993 dev_kfree_skb_any(skb
);
995 tx_ring
->txbufs
[idx
].dma_addr
= 0;
996 tx_ring
->txbufs
[idx
].skb
= NULL
;
997 tx_ring
->txbufs
[idx
].fidx
= -2;
1000 tx_ring
->qcp_rd_p
= qcp_rd_p
;
1002 u64_stats_update_begin(&r_vec
->tx_sync
);
1003 r_vec
->tx_bytes
+= done_bytes
;
1004 r_vec
->tx_pkts
+= done_pkts
;
1005 u64_stats_update_end(&r_vec
->tx_sync
);
1010 nd_q
= netdev_get_tx_queue(dp
->netdev
, tx_ring
->idx
);
1011 netdev_tx_completed_queue(nd_q
, done_pkts
, done_bytes
);
1012 if (nfp_net_tx_ring_should_wake(tx_ring
)) {
1013 /* Make sure TX thread will see updated tx_ring->rd_p */
1016 if (unlikely(netif_tx_queue_stopped(nd_q
)))
1017 netif_tx_wake_queue(nd_q
);
1020 WARN_ONCE(tx_ring
->wr_p
- tx_ring
->rd_p
> tx_ring
->cnt
,
1021 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1022 tx_ring
->rd_p
, tx_ring
->wr_p
, tx_ring
->cnt
);
1025 static bool nfp_net_xdp_complete(struct nfp_net_tx_ring
*tx_ring
)
1027 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
1028 u32 done_pkts
= 0, done_bytes
= 0;
1033 /* Work out how many descriptors have been transmitted */
1034 qcp_rd_p
= nfp_qcp_rd_ptr_read(tx_ring
->qcp_q
);
1036 if (qcp_rd_p
== tx_ring
->qcp_rd_p
)
1039 todo
= D_IDX(tx_ring
, qcp_rd_p
- tx_ring
->qcp_rd_p
);
1041 done_all
= todo
<= NFP_NET_XDP_MAX_COMPLETE
;
1042 todo
= min(todo
, NFP_NET_XDP_MAX_COMPLETE
);
1044 tx_ring
->qcp_rd_p
= D_IDX(tx_ring
, tx_ring
->qcp_rd_p
+ todo
);
1048 idx
= D_IDX(tx_ring
, tx_ring
->rd_p
);
1051 done_bytes
+= tx_ring
->txbufs
[idx
].real_len
;
1054 u64_stats_update_begin(&r_vec
->tx_sync
);
1055 r_vec
->tx_bytes
+= done_bytes
;
1056 r_vec
->tx_pkts
+= done_pkts
;
1057 u64_stats_update_end(&r_vec
->tx_sync
);
1059 WARN_ONCE(tx_ring
->wr_p
- tx_ring
->rd_p
> tx_ring
->cnt
,
1060 "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1061 tx_ring
->rd_p
, tx_ring
->wr_p
, tx_ring
->cnt
);
1067 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1068 * @dp: NFP Net data path struct
1069 * @tx_ring: TX ring structure
1071 * Assumes that the device is stopped
1074 nfp_net_tx_ring_reset(struct nfp_net_dp
*dp
, struct nfp_net_tx_ring
*tx_ring
)
1076 const struct skb_frag_struct
*frag
;
1077 struct netdev_queue
*nd_q
;
1079 while (!tx_ring
->is_xdp
&& tx_ring
->rd_p
!= tx_ring
->wr_p
) {
1080 struct nfp_net_tx_buf
*tx_buf
;
1081 struct sk_buff
*skb
;
1084 idx
= D_IDX(tx_ring
, tx_ring
->rd_p
);
1085 tx_buf
= &tx_ring
->txbufs
[idx
];
1087 skb
= tx_ring
->txbufs
[idx
].skb
;
1088 nr_frags
= skb_shinfo(skb
)->nr_frags
;
1090 if (tx_buf
->fidx
== -1) {
1092 dma_unmap_single(dp
->dev
, tx_buf
->dma_addr
,
1093 skb_headlen(skb
), DMA_TO_DEVICE
);
1095 /* unmap fragment */
1096 frag
= &skb_shinfo(skb
)->frags
[tx_buf
->fidx
];
1097 dma_unmap_page(dp
->dev
, tx_buf
->dma_addr
,
1098 skb_frag_size(frag
), DMA_TO_DEVICE
);
1101 /* check for last gather fragment */
1102 if (tx_buf
->fidx
== nr_frags
- 1)
1103 dev_kfree_skb_any(skb
);
1105 tx_buf
->dma_addr
= 0;
1109 tx_ring
->qcp_rd_p
++;
1113 memset(tx_ring
->txds
, 0, sizeof(*tx_ring
->txds
) * tx_ring
->cnt
);
1116 tx_ring
->qcp_rd_p
= 0;
1117 tx_ring
->wr_ptr_add
= 0;
1119 if (tx_ring
->is_xdp
|| !dp
->netdev
)
1122 nd_q
= netdev_get_tx_queue(dp
->netdev
, tx_ring
->idx
);
1123 netdev_tx_reset_queue(nd_q
);
1126 static void nfp_net_tx_timeout(struct net_device
*netdev
)
1128 struct nfp_net
*nn
= netdev_priv(netdev
);
1131 for (i
= 0; i
< nn
->dp
.netdev
->real_num_tx_queues
; i
++) {
1132 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev
, i
)))
1134 nn_warn(nn
, "TX timeout on ring: %d\n", i
);
1136 nn_warn(nn
, "TX watchdog timeout\n");
1139 /* Receive processing
1142 nfp_net_calc_fl_bufsz(struct nfp_net_dp
*dp
)
1144 unsigned int fl_bufsz
;
1146 fl_bufsz
= NFP_NET_RX_BUF_HEADROOM
;
1147 fl_bufsz
+= dp
->rx_dma_off
;
1148 if (dp
->rx_offset
== NFP_NET_CFG_RX_OFFSET_DYNAMIC
)
1149 fl_bufsz
+= NFP_NET_MAX_PREPEND
;
1151 fl_bufsz
+= dp
->rx_offset
;
1152 fl_bufsz
+= ETH_HLEN
+ VLAN_HLEN
* 2 + dp
->mtu
;
1154 fl_bufsz
= SKB_DATA_ALIGN(fl_bufsz
);
1155 fl_bufsz
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
1161 nfp_net_free_frag(void *frag
, bool xdp
)
1164 skb_free_frag(frag
);
1166 __free_page(virt_to_page(frag
));
1170 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1171 * @dp: NFP Net data path struct
1172 * @dma_addr: Pointer to storage for DMA address (output param)
1174 * This function will allcate a new page frag, map it for DMA.
1176 * Return: allocated page frag or NULL on failure.
1178 static void *nfp_net_rx_alloc_one(struct nfp_net_dp
*dp
, dma_addr_t
*dma_addr
)
1183 frag
= netdev_alloc_frag(dp
->fl_bufsz
);
1185 frag
= page_address(alloc_page(GFP_KERNEL
| __GFP_COLD
));
1187 nn_dp_warn(dp
, "Failed to alloc receive page frag\n");
1191 *dma_addr
= nfp_net_dma_map_rx(dp
, frag
);
1192 if (dma_mapping_error(dp
->dev
, *dma_addr
)) {
1193 nfp_net_free_frag(frag
, dp
->xdp_prog
);
1194 nn_dp_warn(dp
, "Failed to map DMA RX buffer\n");
1201 static void *nfp_net_napi_alloc_one(struct nfp_net_dp
*dp
, dma_addr_t
*dma_addr
)
1206 frag
= napi_alloc_frag(dp
->fl_bufsz
);
1208 frag
= page_address(alloc_page(GFP_ATOMIC
| __GFP_COLD
));
1210 nn_dp_warn(dp
, "Failed to alloc receive page frag\n");
1214 *dma_addr
= nfp_net_dma_map_rx(dp
, frag
);
1215 if (dma_mapping_error(dp
->dev
, *dma_addr
)) {
1216 nfp_net_free_frag(frag
, dp
->xdp_prog
);
1217 nn_dp_warn(dp
, "Failed to map DMA RX buffer\n");
1225 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1226 * @dp: NFP Net data path struct
1227 * @rx_ring: RX ring structure
1228 * @frag: page fragment buffer
1229 * @dma_addr: DMA address of skb mapping
1231 static void nfp_net_rx_give_one(const struct nfp_net_dp
*dp
,
1232 struct nfp_net_rx_ring
*rx_ring
,
1233 void *frag
, dma_addr_t dma_addr
)
1235 unsigned int wr_idx
;
1237 wr_idx
= D_IDX(rx_ring
, rx_ring
->wr_p
);
1239 nfp_net_dma_sync_dev_rx(dp
, dma_addr
);
1241 /* Stash SKB and DMA address away */
1242 rx_ring
->rxbufs
[wr_idx
].frag
= frag
;
1243 rx_ring
->rxbufs
[wr_idx
].dma_addr
= dma_addr
;
1245 /* Fill freelist descriptor */
1246 rx_ring
->rxds
[wr_idx
].fld
.reserved
= 0;
1247 rx_ring
->rxds
[wr_idx
].fld
.meta_len_dd
= 0;
1248 nfp_desc_set_dma_addr(&rx_ring
->rxds
[wr_idx
].fld
,
1249 dma_addr
+ dp
->rx_dma_off
);
1252 if (!(rx_ring
->wr_p
% NFP_NET_FL_BATCH
)) {
1253 /* Update write pointer of the freelist queue. Make
1254 * sure all writes are flushed before telling the hardware.
1257 nfp_qcp_wr_ptr_add(rx_ring
->qcp_fl
, NFP_NET_FL_BATCH
);
1262 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1263 * @rx_ring: RX ring structure
1265 * Warning: Do *not* call if ring buffers were never put on the FW freelist
1266 * (i.e. device was not enabled)!
1268 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring
*rx_ring
)
1270 unsigned int wr_idx
, last_idx
;
1272 /* Move the empty entry to the end of the list */
1273 wr_idx
= D_IDX(rx_ring
, rx_ring
->wr_p
);
1274 last_idx
= rx_ring
->cnt
- 1;
1275 rx_ring
->rxbufs
[wr_idx
].dma_addr
= rx_ring
->rxbufs
[last_idx
].dma_addr
;
1276 rx_ring
->rxbufs
[wr_idx
].frag
= rx_ring
->rxbufs
[last_idx
].frag
;
1277 rx_ring
->rxbufs
[last_idx
].dma_addr
= 0;
1278 rx_ring
->rxbufs
[last_idx
].frag
= NULL
;
1280 memset(rx_ring
->rxds
, 0, sizeof(*rx_ring
->rxds
) * rx_ring
->cnt
);
1286 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1287 * @dp: NFP Net data path struct
1288 * @rx_ring: RX ring to remove buffers from
1290 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1291 * entries. After device is disabled nfp_net_rx_ring_reset() must be called
1292 * to restore required ring geometry.
1295 nfp_net_rx_ring_bufs_free(struct nfp_net_dp
*dp
,
1296 struct nfp_net_rx_ring
*rx_ring
)
1300 for (i
= 0; i
< rx_ring
->cnt
- 1; i
++) {
1301 /* NULL skb can only happen when initial filling of the ring
1302 * fails to allocate enough buffers and calls here to free
1303 * already allocated ones.
1305 if (!rx_ring
->rxbufs
[i
].frag
)
1308 nfp_net_dma_unmap_rx(dp
, rx_ring
->rxbufs
[i
].dma_addr
);
1309 nfp_net_free_frag(rx_ring
->rxbufs
[i
].frag
, dp
->xdp_prog
);
1310 rx_ring
->rxbufs
[i
].dma_addr
= 0;
1311 rx_ring
->rxbufs
[i
].frag
= NULL
;
1316 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1317 * @dp: NFP Net data path struct
1318 * @rx_ring: RX ring to remove buffers from
1321 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp
*dp
,
1322 struct nfp_net_rx_ring
*rx_ring
)
1324 struct nfp_net_rx_buf
*rxbufs
;
1327 rxbufs
= rx_ring
->rxbufs
;
1329 for (i
= 0; i
< rx_ring
->cnt
- 1; i
++) {
1330 rxbufs
[i
].frag
= nfp_net_rx_alloc_one(dp
, &rxbufs
[i
].dma_addr
);
1331 if (!rxbufs
[i
].frag
) {
1332 nfp_net_rx_ring_bufs_free(dp
, rx_ring
);
1341 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1342 * @dp: NFP Net data path struct
1343 * @rx_ring: RX ring to fill
1346 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp
*dp
,
1347 struct nfp_net_rx_ring
*rx_ring
)
1351 for (i
= 0; i
< rx_ring
->cnt
- 1; i
++)
1352 nfp_net_rx_give_one(dp
, rx_ring
, rx_ring
->rxbufs
[i
].frag
,
1353 rx_ring
->rxbufs
[i
].dma_addr
);
1357 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1358 * @flags: RX descriptor flags field in CPU byte order
1360 static int nfp_net_rx_csum_has_errors(u16 flags
)
1362 u16 csum_all_checked
, csum_all_ok
;
1364 csum_all_checked
= flags
& __PCIE_DESC_RX_CSUM_ALL
;
1365 csum_all_ok
= flags
& __PCIE_DESC_RX_CSUM_ALL_OK
;
1367 return csum_all_checked
!= (csum_all_ok
<< PCIE_DESC_RX_CSUM_OK_SHIFT
);
1371 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1372 * @dp: NFP Net data path struct
1373 * @r_vec: per-ring structure
1374 * @rxd: Pointer to RX descriptor
1375 * @meta: Parsed metadata prepend
1376 * @skb: Pointer to SKB
1378 static void nfp_net_rx_csum(struct nfp_net_dp
*dp
,
1379 struct nfp_net_r_vector
*r_vec
,
1380 struct nfp_net_rx_desc
*rxd
,
1381 struct nfp_meta_parsed
*meta
, struct sk_buff
*skb
)
1383 skb_checksum_none_assert(skb
);
1385 if (!(dp
->netdev
->features
& NETIF_F_RXCSUM
))
1388 if (meta
->csum_type
) {
1389 skb
->ip_summed
= meta
->csum_type
;
1390 skb
->csum
= meta
->csum
;
1391 u64_stats_update_begin(&r_vec
->rx_sync
);
1392 r_vec
->hw_csum_rx_ok
++;
1393 u64_stats_update_end(&r_vec
->rx_sync
);
1397 if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd
->rxd
.flags
))) {
1398 u64_stats_update_begin(&r_vec
->rx_sync
);
1399 r_vec
->hw_csum_rx_error
++;
1400 u64_stats_update_end(&r_vec
->rx_sync
);
1404 /* Assume that the firmware will never report inner CSUM_OK unless outer
1405 * L4 headers were successfully parsed. FW will always report zero UDP
1406 * checksum as CSUM_OK.
1408 if (rxd
->rxd
.flags
& PCIE_DESC_RX_TCP_CSUM_OK
||
1409 rxd
->rxd
.flags
& PCIE_DESC_RX_UDP_CSUM_OK
) {
1410 __skb_incr_checksum_unnecessary(skb
);
1411 u64_stats_update_begin(&r_vec
->rx_sync
);
1412 r_vec
->hw_csum_rx_ok
++;
1413 u64_stats_update_end(&r_vec
->rx_sync
);
1416 if (rxd
->rxd
.flags
& PCIE_DESC_RX_I_TCP_CSUM_OK
||
1417 rxd
->rxd
.flags
& PCIE_DESC_RX_I_UDP_CSUM_OK
) {
1418 __skb_incr_checksum_unnecessary(skb
);
1419 u64_stats_update_begin(&r_vec
->rx_sync
);
1420 r_vec
->hw_csum_rx_inner_ok
++;
1421 u64_stats_update_end(&r_vec
->rx_sync
);
1426 nfp_net_set_hash(struct net_device
*netdev
, struct nfp_meta_parsed
*meta
,
1427 unsigned int type
, __be32
*hash
)
1429 if (!(netdev
->features
& NETIF_F_RXHASH
))
1433 case NFP_NET_RSS_IPV4
:
1434 case NFP_NET_RSS_IPV6
:
1435 case NFP_NET_RSS_IPV6_EX
:
1436 meta
->hash_type
= PKT_HASH_TYPE_L3
;
1439 meta
->hash_type
= PKT_HASH_TYPE_L4
;
1443 meta
->hash
= get_unaligned_be32(hash
);
1447 nfp_net_set_hash_desc(struct net_device
*netdev
, struct nfp_meta_parsed
*meta
,
1448 void *data
, struct nfp_net_rx_desc
*rxd
)
1450 struct nfp_net_rx_hash
*rx_hash
= data
;
1452 if (!(rxd
->rxd
.flags
& PCIE_DESC_RX_RSS
))
1455 nfp_net_set_hash(netdev
, meta
, get_unaligned_be32(&rx_hash
->hash_type
),
1460 nfp_net_parse_meta(struct net_device
*netdev
, struct nfp_meta_parsed
*meta
,
1461 void *data
, int meta_len
)
1465 meta_info
= get_unaligned_be32(data
);
1469 switch (meta_info
& NFP_NET_META_FIELD_MASK
) {
1470 case NFP_NET_META_HASH
:
1471 meta_info
>>= NFP_NET_META_FIELD_SIZE
;
1472 nfp_net_set_hash(netdev
, meta
,
1473 meta_info
& NFP_NET_META_FIELD_MASK
,
1477 case NFP_NET_META_MARK
:
1478 meta
->mark
= get_unaligned_be32(data
);
1481 case NFP_NET_META_PORTID
:
1482 meta
->portid
= get_unaligned_be32(data
);
1485 case NFP_NET_META_CSUM
:
1486 meta
->csum_type
= CHECKSUM_COMPLETE
;
1488 (__force __wsum
)__get_unaligned_cpu32(data
);
1495 meta_info
>>= NFP_NET_META_FIELD_SIZE
;
1502 nfp_net_rx_drop(const struct nfp_net_dp
*dp
, struct nfp_net_r_vector
*r_vec
,
1503 struct nfp_net_rx_ring
*rx_ring
, struct nfp_net_rx_buf
*rxbuf
,
1504 struct sk_buff
*skb
)
1506 u64_stats_update_begin(&r_vec
->rx_sync
);
1508 u64_stats_update_end(&r_vec
->rx_sync
);
1510 /* skb is build based on the frag, free_skb() would free the frag
1511 * so to be able to reuse it we need an extra ref.
1513 if (skb
&& rxbuf
&& skb
->head
== rxbuf
->frag
)
1514 page_ref_inc(virt_to_head_page(rxbuf
->frag
));
1516 nfp_net_rx_give_one(dp
, rx_ring
, rxbuf
->frag
, rxbuf
->dma_addr
);
1518 dev_kfree_skb_any(skb
);
1522 nfp_net_tx_xdp_buf(struct nfp_net_dp
*dp
, struct nfp_net_rx_ring
*rx_ring
,
1523 struct nfp_net_tx_ring
*tx_ring
,
1524 struct nfp_net_rx_buf
*rxbuf
, unsigned int dma_off
,
1525 unsigned int pkt_len
, bool *completed
)
1527 struct nfp_net_tx_buf
*txbuf
;
1528 struct nfp_net_tx_desc
*txd
;
1531 if (unlikely(nfp_net_tx_full(tx_ring
, 1))) {
1533 nfp_net_xdp_complete(tx_ring
);
1537 if (unlikely(nfp_net_tx_full(tx_ring
, 1))) {
1538 nfp_net_rx_drop(dp
, rx_ring
->r_vec
, rx_ring
, rxbuf
,
1544 wr_idx
= D_IDX(tx_ring
, tx_ring
->wr_p
);
1546 /* Stash the soft descriptor of the head then initialize it */
1547 txbuf
= &tx_ring
->txbufs
[wr_idx
];
1549 nfp_net_rx_give_one(dp
, rx_ring
, txbuf
->frag
, txbuf
->dma_addr
);
1551 txbuf
->frag
= rxbuf
->frag
;
1552 txbuf
->dma_addr
= rxbuf
->dma_addr
;
1555 txbuf
->real_len
= pkt_len
;
1557 dma_sync_single_for_device(dp
->dev
, rxbuf
->dma_addr
+ dma_off
,
1558 pkt_len
, DMA_BIDIRECTIONAL
);
1560 /* Build TX descriptor */
1561 txd
= &tx_ring
->txds
[wr_idx
];
1562 txd
->offset_eop
= PCIE_DESC_TX_EOP
;
1563 txd
->dma_len
= cpu_to_le16(pkt_len
);
1564 nfp_desc_set_dma_addr(txd
, rxbuf
->dma_addr
+ dma_off
);
1565 txd
->data_len
= cpu_to_le16(pkt_len
);
1569 txd
->lso_hdrlen
= 0;
1572 tx_ring
->wr_ptr_add
++;
1576 static int nfp_net_run_xdp(struct bpf_prog
*prog
, void *data
, void *hard_start
,
1577 unsigned int *off
, unsigned int *len
)
1579 struct xdp_buff xdp
;
1583 xdp
.data_hard_start
= hard_start
;
1584 xdp
.data
= data
+ *off
;
1585 xdp
.data_end
= data
+ *off
+ *len
;
1587 orig_data
= xdp
.data
;
1588 ret
= bpf_prog_run_xdp(prog
, &xdp
);
1590 *len
-= xdp
.data
- orig_data
;
1591 *off
+= xdp
.data
- orig_data
;
1597 * nfp_net_rx() - receive up to @budget packets on @rx_ring
1598 * @rx_ring: RX ring to receive from
1599 * @budget: NAPI budget
1601 * Note, this function is separated out from the napi poll function to
1602 * more cleanly separate packet receive code from other bookkeeping
1603 * functions performed in the napi poll function.
1605 * Return: Number of packets received.
1607 static int nfp_net_rx(struct nfp_net_rx_ring
*rx_ring
, int budget
)
1609 struct nfp_net_r_vector
*r_vec
= rx_ring
->r_vec
;
1610 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
1611 struct nfp_net_tx_ring
*tx_ring
;
1612 struct bpf_prog
*xdp_prog
;
1613 bool xdp_tx_cmpl
= false;
1614 unsigned int true_bufsz
;
1615 struct sk_buff
*skb
;
1616 int pkts_polled
= 0;
1620 xdp_prog
= READ_ONCE(dp
->xdp_prog
);
1621 true_bufsz
= xdp_prog
? PAGE_SIZE
: dp
->fl_bufsz
;
1622 tx_ring
= r_vec
->xdp_ring
;
1624 while (pkts_polled
< budget
) {
1625 unsigned int meta_len
, data_len
, meta_off
, pkt_len
, pkt_off
;
1626 struct nfp_net_rx_buf
*rxbuf
;
1627 struct nfp_net_rx_desc
*rxd
;
1628 struct nfp_meta_parsed meta
;
1629 struct net_device
*netdev
;
1630 dma_addr_t new_dma_addr
;
1633 idx
= D_IDX(rx_ring
, rx_ring
->rd_p
);
1635 rxd
= &rx_ring
->rxds
[idx
];
1636 if (!(rxd
->rxd
.meta_len_dd
& PCIE_DESC_RX_DD
))
1639 /* Memory barrier to ensure that we won't do other reads
1640 * before the DD bit.
1644 memset(&meta
, 0, sizeof(meta
));
1649 rxbuf
= &rx_ring
->rxbufs
[idx
];
1651 * <-- [rx_offset] -->
1652 * ---------------------------------------------------------
1653 * | [XX] | metadata | packet | XXXX |
1654 * ---------------------------------------------------------
1655 * <---------------- data_len --------------->
1657 * The rx_offset is fixed for all packets, the meta_len can vary
1658 * on a packet by packet basis. If rx_offset is set to zero
1659 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1660 * buffer and is immediately followed by the packet (no [XX]).
1662 meta_len
= rxd
->rxd
.meta_len_dd
& PCIE_DESC_RX_META_LEN_MASK
;
1663 data_len
= le16_to_cpu(rxd
->rxd
.data_len
);
1664 pkt_len
= data_len
- meta_len
;
1666 pkt_off
= NFP_NET_RX_BUF_HEADROOM
+ dp
->rx_dma_off
;
1667 if (dp
->rx_offset
== NFP_NET_CFG_RX_OFFSET_DYNAMIC
)
1668 pkt_off
+= meta_len
;
1670 pkt_off
+= dp
->rx_offset
;
1671 meta_off
= pkt_off
- meta_len
;
1674 u64_stats_update_begin(&r_vec
->rx_sync
);
1676 r_vec
->rx_bytes
+= pkt_len
;
1677 u64_stats_update_end(&r_vec
->rx_sync
);
1679 if (unlikely(meta_len
> NFP_NET_MAX_PREPEND
||
1680 (dp
->rx_offset
&& meta_len
> dp
->rx_offset
))) {
1681 nn_dp_warn(dp
, "oversized RX packet metadata %u\n",
1683 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, NULL
);
1687 nfp_net_dma_sync_cpu_rx(dp
, rxbuf
->dma_addr
+ meta_off
,
1690 if (!dp
->chained_metadata_format
) {
1691 nfp_net_set_hash_desc(dp
->netdev
, &meta
,
1692 rxbuf
->frag
+ meta_off
, rxd
);
1693 } else if (meta_len
) {
1696 end
= nfp_net_parse_meta(dp
->netdev
, &meta
,
1697 rxbuf
->frag
+ meta_off
,
1699 if (unlikely(end
!= rxbuf
->frag
+ pkt_off
)) {
1700 nn_dp_warn(dp
, "invalid RX packet metadata\n");
1701 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
,
1707 if (xdp_prog
&& !(rxd
->rxd
.flags
& PCIE_DESC_RX_BPF
&&
1708 dp
->bpf_offload_xdp
) && !meta
.portid
) {
1709 unsigned int dma_off
;
1713 hard_start
= rxbuf
->frag
+ NFP_NET_RX_BUF_HEADROOM
;
1715 act
= nfp_net_run_xdp(xdp_prog
, rxbuf
->frag
, hard_start
,
1716 &pkt_off
, &pkt_len
);
1721 dma_off
= pkt_off
- NFP_NET_RX_BUF_HEADROOM
;
1722 if (unlikely(!nfp_net_tx_xdp_buf(dp
, rx_ring
,
1727 trace_xdp_exception(dp
->netdev
,
1731 bpf_warn_invalid_xdp_action(act
);
1734 trace_xdp_exception(dp
->netdev
, xdp_prog
, act
);
1737 nfp_net_rx_give_one(dp
, rx_ring
, rxbuf
->frag
,
1743 skb
= build_skb(rxbuf
->frag
, true_bufsz
);
1744 if (unlikely(!skb
)) {
1745 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, NULL
);
1748 new_frag
= nfp_net_napi_alloc_one(dp
, &new_dma_addr
);
1749 if (unlikely(!new_frag
)) {
1750 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, skb
);
1754 nfp_net_dma_unmap_rx(dp
, rxbuf
->dma_addr
);
1756 nfp_net_rx_give_one(dp
, rx_ring
, new_frag
, new_dma_addr
);
1758 if (likely(!meta
.portid
)) {
1759 netdev
= dp
->netdev
;
1763 nn
= netdev_priv(dp
->netdev
);
1764 netdev
= nfp_app_repr_get(nn
->app
, meta
.portid
);
1765 if (unlikely(!netdev
)) {
1766 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, NULL
, skb
);
1769 nfp_repr_inc_rx_stats(netdev
, pkt_len
);
1772 skb_reserve(skb
, pkt_off
);
1773 skb_put(skb
, pkt_len
);
1775 skb
->mark
= meta
.mark
;
1776 skb_set_hash(skb
, meta
.hash
, meta
.hash_type
);
1778 skb_record_rx_queue(skb
, rx_ring
->idx
);
1779 skb
->protocol
= eth_type_trans(skb
, netdev
);
1781 nfp_net_rx_csum(dp
, r_vec
, rxd
, &meta
, skb
);
1783 if (rxd
->rxd
.flags
& PCIE_DESC_RX_VLAN
)
1784 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
),
1785 le16_to_cpu(rxd
->rxd
.vlan
));
1787 napi_gro_receive(&rx_ring
->r_vec
->napi
, skb
);
1791 if (tx_ring
->wr_ptr_add
)
1792 nfp_net_tx_xmit_more_flush(tx_ring
);
1793 else if (unlikely(tx_ring
->wr_p
!= tx_ring
->rd_p
) &&
1795 if (!nfp_net_xdp_complete(tx_ring
))
1796 pkts_polled
= budget
;
1804 * nfp_net_poll() - napi poll function
1805 * @napi: NAPI structure
1806 * @budget: NAPI budget
1808 * Return: number of packets polled.
1810 static int nfp_net_poll(struct napi_struct
*napi
, int budget
)
1812 struct nfp_net_r_vector
*r_vec
=
1813 container_of(napi
, struct nfp_net_r_vector
, napi
);
1814 unsigned int pkts_polled
= 0;
1817 nfp_net_tx_complete(r_vec
->tx_ring
);
1819 pkts_polled
= nfp_net_rx(r_vec
->rx_ring
, budget
);
1821 if (pkts_polled
< budget
)
1822 if (napi_complete_done(napi
, pkts_polled
))
1823 nfp_net_irq_unmask(r_vec
->nfp_net
, r_vec
->irq_entry
);
1828 /* Control device data path
1832 nfp_ctrl_tx_one(struct nfp_net
*nn
, struct nfp_net_r_vector
*r_vec
,
1833 struct sk_buff
*skb
, bool old
)
1835 unsigned int real_len
= skb
->len
, meta_len
= 0;
1836 struct nfp_net_tx_ring
*tx_ring
;
1837 struct nfp_net_tx_buf
*txbuf
;
1838 struct nfp_net_tx_desc
*txd
;
1839 struct nfp_net_dp
*dp
;
1840 dma_addr_t dma_addr
;
1843 dp
= &r_vec
->nfp_net
->dp
;
1844 tx_ring
= r_vec
->tx_ring
;
1846 if (WARN_ON_ONCE(skb_shinfo(skb
)->nr_frags
)) {
1847 nn_dp_warn(dp
, "Driver's CTRL TX does not implement gather\n");
1851 if (unlikely(nfp_net_tx_full(tx_ring
, 1))) {
1852 u64_stats_update_begin(&r_vec
->tx_sync
);
1854 u64_stats_update_end(&r_vec
->tx_sync
);
1856 __skb_queue_tail(&r_vec
->queue
, skb
);
1858 __skb_queue_head(&r_vec
->queue
, skb
);
1862 if (nfp_app_ctrl_has_meta(nn
->app
)) {
1863 if (unlikely(skb_headroom(skb
) < 8)) {
1864 nn_dp_warn(dp
, "CTRL TX on skb without headroom\n");
1868 put_unaligned_be32(NFP_META_PORT_ID_CTRL
, skb_push(skb
, 4));
1869 put_unaligned_be32(NFP_NET_META_PORTID
, skb_push(skb
, 4));
1872 /* Start with the head skbuf */
1873 dma_addr
= dma_map_single(dp
->dev
, skb
->data
, skb_headlen(skb
),
1875 if (dma_mapping_error(dp
->dev
, dma_addr
))
1878 wr_idx
= D_IDX(tx_ring
, tx_ring
->wr_p
);
1880 /* Stash the soft descriptor of the head then initialize it */
1881 txbuf
= &tx_ring
->txbufs
[wr_idx
];
1883 txbuf
->dma_addr
= dma_addr
;
1886 txbuf
->real_len
= real_len
;
1888 /* Build TX descriptor */
1889 txd
= &tx_ring
->txds
[wr_idx
];
1890 txd
->offset_eop
= meta_len
| PCIE_DESC_TX_EOP
;
1891 txd
->dma_len
= cpu_to_le16(skb_headlen(skb
));
1892 nfp_desc_set_dma_addr(txd
, dma_addr
);
1893 txd
->data_len
= cpu_to_le16(skb
->len
);
1897 txd
->lso_hdrlen
= 0;
1900 tx_ring
->wr_ptr_add
++;
1901 nfp_net_tx_xmit_more_flush(tx_ring
);
1906 nn_dp_warn(dp
, "Failed to DMA map TX CTRL buffer\n");
1908 u64_stats_update_begin(&r_vec
->tx_sync
);
1910 u64_stats_update_end(&r_vec
->tx_sync
);
1911 dev_kfree_skb_any(skb
);
1915 bool nfp_ctrl_tx(struct nfp_net
*nn
, struct sk_buff
*skb
)
1917 struct nfp_net_r_vector
*r_vec
= &nn
->r_vecs
[0];
1920 spin_lock_bh(&r_vec
->lock
);
1921 ret
= nfp_ctrl_tx_one(nn
, r_vec
, skb
, false);
1922 spin_unlock_bh(&r_vec
->lock
);
1927 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector
*r_vec
)
1929 struct sk_buff
*skb
;
1931 while ((skb
= __skb_dequeue(&r_vec
->queue
)))
1932 if (nfp_ctrl_tx_one(r_vec
->nfp_net
, r_vec
, skb
, true))
1937 nfp_ctrl_meta_ok(struct nfp_net
*nn
, void *data
, unsigned int meta_len
)
1939 u32 meta_type
, meta_tag
;
1941 if (!nfp_app_ctrl_has_meta(nn
->app
))
1947 meta_type
= get_unaligned_be32(data
);
1948 meta_tag
= get_unaligned_be32(data
+ 4);
1950 return (meta_type
== NFP_NET_META_PORTID
&&
1951 meta_tag
== NFP_META_PORT_ID_CTRL
);
1955 nfp_ctrl_rx_one(struct nfp_net
*nn
, struct nfp_net_dp
*dp
,
1956 struct nfp_net_r_vector
*r_vec
, struct nfp_net_rx_ring
*rx_ring
)
1958 unsigned int meta_len
, data_len
, meta_off
, pkt_len
, pkt_off
;
1959 struct nfp_net_rx_buf
*rxbuf
;
1960 struct nfp_net_rx_desc
*rxd
;
1961 dma_addr_t new_dma_addr
;
1962 struct sk_buff
*skb
;
1966 idx
= D_IDX(rx_ring
, rx_ring
->rd_p
);
1968 rxd
= &rx_ring
->rxds
[idx
];
1969 if (!(rxd
->rxd
.meta_len_dd
& PCIE_DESC_RX_DD
))
1972 /* Memory barrier to ensure that we won't do other reads
1973 * before the DD bit.
1979 rxbuf
= &rx_ring
->rxbufs
[idx
];
1980 meta_len
= rxd
->rxd
.meta_len_dd
& PCIE_DESC_RX_META_LEN_MASK
;
1981 data_len
= le16_to_cpu(rxd
->rxd
.data_len
);
1982 pkt_len
= data_len
- meta_len
;
1984 pkt_off
= NFP_NET_RX_BUF_HEADROOM
+ dp
->rx_dma_off
;
1985 if (dp
->rx_offset
== NFP_NET_CFG_RX_OFFSET_DYNAMIC
)
1986 pkt_off
+= meta_len
;
1988 pkt_off
+= dp
->rx_offset
;
1989 meta_off
= pkt_off
- meta_len
;
1992 u64_stats_update_begin(&r_vec
->rx_sync
);
1994 r_vec
->rx_bytes
+= pkt_len
;
1995 u64_stats_update_end(&r_vec
->rx_sync
);
1997 nfp_net_dma_sync_cpu_rx(dp
, rxbuf
->dma_addr
+ meta_off
, data_len
);
1999 if (unlikely(!nfp_ctrl_meta_ok(nn
, rxbuf
->frag
+ meta_off
, meta_len
))) {
2000 nn_dp_warn(dp
, "incorrect metadata for ctrl packet (%d)\n",
2002 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, NULL
);
2006 skb
= build_skb(rxbuf
->frag
, dp
->fl_bufsz
);
2007 if (unlikely(!skb
)) {
2008 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, NULL
);
2011 new_frag
= nfp_net_napi_alloc_one(dp
, &new_dma_addr
);
2012 if (unlikely(!new_frag
)) {
2013 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, skb
);
2017 nfp_net_dma_unmap_rx(dp
, rxbuf
->dma_addr
);
2019 nfp_net_rx_give_one(dp
, rx_ring
, new_frag
, new_dma_addr
);
2021 skb_reserve(skb
, pkt_off
);
2022 skb_put(skb
, pkt_len
);
2024 nfp_app_ctrl_rx(nn
->app
, skb
);
2029 static void nfp_ctrl_rx(struct nfp_net_r_vector
*r_vec
)
2031 struct nfp_net_rx_ring
*rx_ring
= r_vec
->rx_ring
;
2032 struct nfp_net
*nn
= r_vec
->nfp_net
;
2033 struct nfp_net_dp
*dp
= &nn
->dp
;
2035 while (nfp_ctrl_rx_one(nn
, dp
, r_vec
, rx_ring
))
2039 static void nfp_ctrl_poll(unsigned long arg
)
2041 struct nfp_net_r_vector
*r_vec
= (void *)arg
;
2043 spin_lock_bh(&r_vec
->lock
);
2044 nfp_net_tx_complete(r_vec
->tx_ring
);
2045 __nfp_ctrl_tx_queued(r_vec
);
2046 spin_unlock_bh(&r_vec
->lock
);
2050 nfp_net_irq_unmask(r_vec
->nfp_net
, r_vec
->irq_entry
);
2053 /* Setup and Configuration
2057 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
2058 * @nn: NFP Network structure
2060 static void nfp_net_vecs_init(struct nfp_net
*nn
)
2062 struct nfp_net_r_vector
*r_vec
;
2065 nn
->lsc_handler
= nfp_net_irq_lsc
;
2066 nn
->exn_handler
= nfp_net_irq_exn
;
2068 for (r
= 0; r
< nn
->max_r_vecs
; r
++) {
2069 struct msix_entry
*entry
;
2071 entry
= &nn
->irq_entries
[NFP_NET_NON_Q_VECTORS
+ r
];
2073 r_vec
= &nn
->r_vecs
[r
];
2074 r_vec
->nfp_net
= nn
;
2075 r_vec
->irq_entry
= entry
->entry
;
2076 r_vec
->irq_vector
= entry
->vector
;
2078 if (nn
->dp
.netdev
) {
2079 r_vec
->handler
= nfp_net_irq_rxtx
;
2081 r_vec
->handler
= nfp_ctrl_irq_rxtx
;
2083 __skb_queue_head_init(&r_vec
->queue
);
2084 spin_lock_init(&r_vec
->lock
);
2085 tasklet_init(&r_vec
->tasklet
, nfp_ctrl_poll
,
2086 (unsigned long)r_vec
);
2087 tasklet_disable(&r_vec
->tasklet
);
2090 cpumask_set_cpu(r
, &r_vec
->affinity_mask
);
2095 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
2096 * @tx_ring: TX ring to free
2098 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring
*tx_ring
)
2100 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
2101 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
2103 kfree(tx_ring
->txbufs
);
2106 dma_free_coherent(dp
->dev
, tx_ring
->size
,
2107 tx_ring
->txds
, tx_ring
->dma
);
2110 tx_ring
->txbufs
= NULL
;
2111 tx_ring
->txds
= NULL
;
2117 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
2118 * @dp: NFP Net data path struct
2119 * @tx_ring: TX Ring structure to allocate
2121 * Return: 0 on success, negative errno otherwise.
2124 nfp_net_tx_ring_alloc(struct nfp_net_dp
*dp
, struct nfp_net_tx_ring
*tx_ring
)
2126 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
2129 tx_ring
->cnt
= dp
->txd_cnt
;
2131 tx_ring
->size
= sizeof(*tx_ring
->txds
) * tx_ring
->cnt
;
2132 tx_ring
->txds
= dma_zalloc_coherent(dp
->dev
, tx_ring
->size
,
2133 &tx_ring
->dma
, GFP_KERNEL
);
2137 sz
= sizeof(*tx_ring
->txbufs
) * tx_ring
->cnt
;
2138 tx_ring
->txbufs
= kzalloc(sz
, GFP_KERNEL
);
2139 if (!tx_ring
->txbufs
)
2142 if (!tx_ring
->is_xdp
&& dp
->netdev
)
2143 netif_set_xps_queue(dp
->netdev
, &r_vec
->affinity_mask
,
2149 nfp_net_tx_ring_free(tx_ring
);
2154 nfp_net_tx_ring_bufs_free(struct nfp_net_dp
*dp
,
2155 struct nfp_net_tx_ring
*tx_ring
)
2159 if (!tx_ring
->is_xdp
)
2162 for (i
= 0; i
< tx_ring
->cnt
; i
++) {
2163 if (!tx_ring
->txbufs
[i
].frag
)
2166 nfp_net_dma_unmap_rx(dp
, tx_ring
->txbufs
[i
].dma_addr
);
2167 __free_page(virt_to_page(tx_ring
->txbufs
[i
].frag
));
2172 nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp
*dp
,
2173 struct nfp_net_tx_ring
*tx_ring
)
2175 struct nfp_net_tx_buf
*txbufs
= tx_ring
->txbufs
;
2178 if (!tx_ring
->is_xdp
)
2181 for (i
= 0; i
< tx_ring
->cnt
; i
++) {
2182 txbufs
[i
].frag
= nfp_net_rx_alloc_one(dp
, &txbufs
[i
].dma_addr
);
2183 if (!txbufs
[i
].frag
) {
2184 nfp_net_tx_ring_bufs_free(dp
, tx_ring
);
2192 static int nfp_net_tx_rings_prepare(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2196 dp
->tx_rings
= kcalloc(dp
->num_tx_rings
, sizeof(*dp
->tx_rings
),
2201 for (r
= 0; r
< dp
->num_tx_rings
; r
++) {
2204 if (r
>= dp
->num_stack_tx_rings
)
2205 bias
= dp
->num_stack_tx_rings
;
2207 nfp_net_tx_ring_init(&dp
->tx_rings
[r
], &nn
->r_vecs
[r
- bias
],
2210 if (nfp_net_tx_ring_alloc(dp
, &dp
->tx_rings
[r
]))
2213 if (nfp_net_tx_ring_bufs_alloc(dp
, &dp
->tx_rings
[r
]))
2221 nfp_net_tx_ring_bufs_free(dp
, &dp
->tx_rings
[r
]);
2223 nfp_net_tx_ring_free(&dp
->tx_rings
[r
]);
2225 kfree(dp
->tx_rings
);
2229 static void nfp_net_tx_rings_free(struct nfp_net_dp
*dp
)
2233 for (r
= 0; r
< dp
->num_tx_rings
; r
++) {
2234 nfp_net_tx_ring_bufs_free(dp
, &dp
->tx_rings
[r
]);
2235 nfp_net_tx_ring_free(&dp
->tx_rings
[r
]);
2238 kfree(dp
->tx_rings
);
2242 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
2243 * @rx_ring: RX ring to free
2245 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring
*rx_ring
)
2247 struct nfp_net_r_vector
*r_vec
= rx_ring
->r_vec
;
2248 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
2250 kfree(rx_ring
->rxbufs
);
2253 dma_free_coherent(dp
->dev
, rx_ring
->size
,
2254 rx_ring
->rxds
, rx_ring
->dma
);
2257 rx_ring
->rxbufs
= NULL
;
2258 rx_ring
->rxds
= NULL
;
2264 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
2265 * @dp: NFP Net data path struct
2266 * @rx_ring: RX ring to allocate
2268 * Return: 0 on success, negative errno otherwise.
2271 nfp_net_rx_ring_alloc(struct nfp_net_dp
*dp
, struct nfp_net_rx_ring
*rx_ring
)
2275 rx_ring
->cnt
= dp
->rxd_cnt
;
2276 rx_ring
->size
= sizeof(*rx_ring
->rxds
) * rx_ring
->cnt
;
2277 rx_ring
->rxds
= dma_zalloc_coherent(dp
->dev
, rx_ring
->size
,
2278 &rx_ring
->dma
, GFP_KERNEL
);
2282 sz
= sizeof(*rx_ring
->rxbufs
) * rx_ring
->cnt
;
2283 rx_ring
->rxbufs
= kzalloc(sz
, GFP_KERNEL
);
2284 if (!rx_ring
->rxbufs
)
2290 nfp_net_rx_ring_free(rx_ring
);
2294 static int nfp_net_rx_rings_prepare(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2298 dp
->rx_rings
= kcalloc(dp
->num_rx_rings
, sizeof(*dp
->rx_rings
),
2303 for (r
= 0; r
< dp
->num_rx_rings
; r
++) {
2304 nfp_net_rx_ring_init(&dp
->rx_rings
[r
], &nn
->r_vecs
[r
], r
);
2306 if (nfp_net_rx_ring_alloc(dp
, &dp
->rx_rings
[r
]))
2309 if (nfp_net_rx_ring_bufs_alloc(dp
, &dp
->rx_rings
[r
]))
2317 nfp_net_rx_ring_bufs_free(dp
, &dp
->rx_rings
[r
]);
2319 nfp_net_rx_ring_free(&dp
->rx_rings
[r
]);
2321 kfree(dp
->rx_rings
);
2325 static void nfp_net_rx_rings_free(struct nfp_net_dp
*dp
)
2329 for (r
= 0; r
< dp
->num_rx_rings
; r
++) {
2330 nfp_net_rx_ring_bufs_free(dp
, &dp
->rx_rings
[r
]);
2331 nfp_net_rx_ring_free(&dp
->rx_rings
[r
]);
2334 kfree(dp
->rx_rings
);
2338 nfp_net_vector_assign_rings(struct nfp_net_dp
*dp
,
2339 struct nfp_net_r_vector
*r_vec
, int idx
)
2341 r_vec
->rx_ring
= idx
< dp
->num_rx_rings
? &dp
->rx_rings
[idx
] : NULL
;
2343 idx
< dp
->num_stack_tx_rings
? &dp
->tx_rings
[idx
] : NULL
;
2345 r_vec
->xdp_ring
= idx
< dp
->num_tx_rings
- dp
->num_stack_tx_rings
?
2346 &dp
->tx_rings
[dp
->num_stack_tx_rings
+ idx
] : NULL
;
2350 nfp_net_prepare_vector(struct nfp_net
*nn
, struct nfp_net_r_vector
*r_vec
,
2357 netif_napi_add(nn
->dp
.netdev
, &r_vec
->napi
,
2358 nfp_net_poll
, NAPI_POLL_WEIGHT
);
2360 tasklet_enable(&r_vec
->tasklet
);
2362 snprintf(r_vec
->name
, sizeof(r_vec
->name
),
2363 "%s-rxtx-%d", nfp_net_name(nn
), idx
);
2364 err
= request_irq(r_vec
->irq_vector
, r_vec
->handler
, 0, r_vec
->name
,
2368 netif_napi_del(&r_vec
->napi
);
2370 tasklet_disable(&r_vec
->tasklet
);
2372 nn_err(nn
, "Error requesting IRQ %d\n", r_vec
->irq_vector
);
2375 disable_irq(r_vec
->irq_vector
);
2377 irq_set_affinity_hint(r_vec
->irq_vector
, &r_vec
->affinity_mask
);
2379 nn_dbg(nn
, "RV%02d: irq=%03d/%03d\n", idx
, r_vec
->irq_vector
,
2386 nfp_net_cleanup_vector(struct nfp_net
*nn
, struct nfp_net_r_vector
*r_vec
)
2388 irq_set_affinity_hint(r_vec
->irq_vector
, NULL
);
2390 netif_napi_del(&r_vec
->napi
);
2392 tasklet_disable(&r_vec
->tasklet
);
2394 free_irq(r_vec
->irq_vector
, r_vec
);
2398 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
2399 * @nn: NFP Net device to reconfigure
2401 void nfp_net_rss_write_itbl(struct nfp_net
*nn
)
2405 for (i
= 0; i
< NFP_NET_CFG_RSS_ITBL_SZ
; i
+= 4)
2406 nn_writel(nn
, NFP_NET_CFG_RSS_ITBL
+ i
,
2407 get_unaligned_le32(nn
->rss_itbl
+ i
));
2411 * nfp_net_rss_write_key() - Write RSS hash key to device
2412 * @nn: NFP Net device to reconfigure
2414 void nfp_net_rss_write_key(struct nfp_net
*nn
)
2418 for (i
= 0; i
< nfp_net_rss_key_sz(nn
); i
+= 4)
2419 nn_writel(nn
, NFP_NET_CFG_RSS_KEY
+ i
,
2420 get_unaligned_le32(nn
->rss_key
+ i
));
2424 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2425 * @nn: NFP Net device to reconfigure
2427 void nfp_net_coalesce_write_cfg(struct nfp_net
*nn
)
2433 /* Compute factor used to convert coalesce '_usecs' parameters to
2434 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
2437 factor
= nn
->me_freq_mhz
/ 16;
2439 /* copy RX interrupt coalesce parameters */
2440 value
= (nn
->rx_coalesce_max_frames
<< 16) |
2441 (factor
* nn
->rx_coalesce_usecs
);
2442 for (i
= 0; i
< nn
->dp
.num_rx_rings
; i
++)
2443 nn_writel(nn
, NFP_NET_CFG_RXR_IRQ_MOD(i
), value
);
2445 /* copy TX interrupt coalesce parameters */
2446 value
= (nn
->tx_coalesce_max_frames
<< 16) |
2447 (factor
* nn
->tx_coalesce_usecs
);
2448 for (i
= 0; i
< nn
->dp
.num_tx_rings
; i
++)
2449 nn_writel(nn
, NFP_NET_CFG_TXR_IRQ_MOD(i
), value
);
2453 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2454 * @nn: NFP Net device to reconfigure
2455 * @addr: MAC address to write
2457 * Writes the MAC address from the netdev to the device control BAR. Does not
2458 * perform the required reconfig. We do a bit of byte swapping dance because
2461 static void nfp_net_write_mac_addr(struct nfp_net
*nn
, const u8
*addr
)
2463 nn_writel(nn
, NFP_NET_CFG_MACADDR
+ 0, get_unaligned_be32(addr
));
2464 nn_writew(nn
, NFP_NET_CFG_MACADDR
+ 6, get_unaligned_be16(addr
+ 4));
2467 static void nfp_net_vec_clear_ring_data(struct nfp_net
*nn
, unsigned int idx
)
2469 nn_writeq(nn
, NFP_NET_CFG_RXR_ADDR(idx
), 0);
2470 nn_writeb(nn
, NFP_NET_CFG_RXR_SZ(idx
), 0);
2471 nn_writeb(nn
, NFP_NET_CFG_RXR_VEC(idx
), 0);
2473 nn_writeq(nn
, NFP_NET_CFG_TXR_ADDR(idx
), 0);
2474 nn_writeb(nn
, NFP_NET_CFG_TXR_SZ(idx
), 0);
2475 nn_writeb(nn
, NFP_NET_CFG_TXR_VEC(idx
), 0);
2479 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2480 * @nn: NFP Net device to reconfigure
2482 static void nfp_net_clear_config_and_disable(struct nfp_net
*nn
)
2484 u32 new_ctrl
, update
;
2488 new_ctrl
= nn
->dp
.ctrl
;
2489 new_ctrl
&= ~NFP_NET_CFG_CTRL_ENABLE
;
2490 update
= NFP_NET_CFG_UPDATE_GEN
;
2491 update
|= NFP_NET_CFG_UPDATE_MSIX
;
2492 update
|= NFP_NET_CFG_UPDATE_RING
;
2494 if (nn
->cap
& NFP_NET_CFG_CTRL_RINGCFG
)
2495 new_ctrl
&= ~NFP_NET_CFG_CTRL_RINGCFG
;
2497 nn_writeq(nn
, NFP_NET_CFG_TXRS_ENABLE
, 0);
2498 nn_writeq(nn
, NFP_NET_CFG_RXRS_ENABLE
, 0);
2500 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
2501 err
= nfp_net_reconfig(nn
, update
);
2503 nn_err(nn
, "Could not disable device: %d\n", err
);
2505 for (r
= 0; r
< nn
->dp
.num_rx_rings
; r
++)
2506 nfp_net_rx_ring_reset(&nn
->dp
.rx_rings
[r
]);
2507 for (r
= 0; r
< nn
->dp
.num_tx_rings
; r
++)
2508 nfp_net_tx_ring_reset(&nn
->dp
, &nn
->dp
.tx_rings
[r
]);
2509 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++)
2510 nfp_net_vec_clear_ring_data(nn
, r
);
2512 nn
->dp
.ctrl
= new_ctrl
;
2516 nfp_net_rx_ring_hw_cfg_write(struct nfp_net
*nn
,
2517 struct nfp_net_rx_ring
*rx_ring
, unsigned int idx
)
2519 /* Write the DMA address, size and MSI-X info to the device */
2520 nn_writeq(nn
, NFP_NET_CFG_RXR_ADDR(idx
), rx_ring
->dma
);
2521 nn_writeb(nn
, NFP_NET_CFG_RXR_SZ(idx
), ilog2(rx_ring
->cnt
));
2522 nn_writeb(nn
, NFP_NET_CFG_RXR_VEC(idx
), rx_ring
->r_vec
->irq_entry
);
2526 nfp_net_tx_ring_hw_cfg_write(struct nfp_net
*nn
,
2527 struct nfp_net_tx_ring
*tx_ring
, unsigned int idx
)
2529 nn_writeq(nn
, NFP_NET_CFG_TXR_ADDR(idx
), tx_ring
->dma
);
2530 nn_writeb(nn
, NFP_NET_CFG_TXR_SZ(idx
), ilog2(tx_ring
->cnt
));
2531 nn_writeb(nn
, NFP_NET_CFG_TXR_VEC(idx
), tx_ring
->r_vec
->irq_entry
);
2535 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2536 * @nn: NFP Net device to reconfigure
2538 static int nfp_net_set_config_and_enable(struct nfp_net
*nn
)
2540 u32 bufsz
, new_ctrl
, update
= 0;
2544 new_ctrl
= nn
->dp
.ctrl
;
2546 if (nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_RSS_ANY
) {
2547 nfp_net_rss_write_key(nn
);
2548 nfp_net_rss_write_itbl(nn
);
2549 nn_writel(nn
, NFP_NET_CFG_RSS_CTRL
, nn
->rss_cfg
);
2550 update
|= NFP_NET_CFG_UPDATE_RSS
;
2553 if (nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_IRQMOD
) {
2554 nfp_net_coalesce_write_cfg(nn
);
2555 update
|= NFP_NET_CFG_UPDATE_IRQMOD
;
2558 for (r
= 0; r
< nn
->dp
.num_tx_rings
; r
++)
2559 nfp_net_tx_ring_hw_cfg_write(nn
, &nn
->dp
.tx_rings
[r
], r
);
2560 for (r
= 0; r
< nn
->dp
.num_rx_rings
; r
++)
2561 nfp_net_rx_ring_hw_cfg_write(nn
, &nn
->dp
.rx_rings
[r
], r
);
2563 nn_writeq(nn
, NFP_NET_CFG_TXRS_ENABLE
, nn
->dp
.num_tx_rings
== 64 ?
2564 0xffffffffffffffffULL
: ((u64
)1 << nn
->dp
.num_tx_rings
) - 1);
2566 nn_writeq(nn
, NFP_NET_CFG_RXRS_ENABLE
, nn
->dp
.num_rx_rings
== 64 ?
2567 0xffffffffffffffffULL
: ((u64
)1 << nn
->dp
.num_rx_rings
) - 1);
2570 nfp_net_write_mac_addr(nn
, nn
->dp
.netdev
->dev_addr
);
2572 nn_writel(nn
, NFP_NET_CFG_MTU
, nn
->dp
.mtu
);
2574 bufsz
= nn
->dp
.fl_bufsz
- nn
->dp
.rx_dma_off
- NFP_NET_RX_BUF_NON_DATA
;
2575 nn_writel(nn
, NFP_NET_CFG_FLBUFSZ
, bufsz
);
2578 new_ctrl
|= NFP_NET_CFG_CTRL_ENABLE
;
2579 update
|= NFP_NET_CFG_UPDATE_GEN
;
2580 update
|= NFP_NET_CFG_UPDATE_MSIX
;
2581 update
|= NFP_NET_CFG_UPDATE_RING
;
2582 if (nn
->cap
& NFP_NET_CFG_CTRL_RINGCFG
)
2583 new_ctrl
|= NFP_NET_CFG_CTRL_RINGCFG
;
2585 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
2586 err
= nfp_net_reconfig(nn
, update
);
2588 nfp_net_clear_config_and_disable(nn
);
2592 nn
->dp
.ctrl
= new_ctrl
;
2594 for (r
= 0; r
< nn
->dp
.num_rx_rings
; r
++)
2595 nfp_net_rx_ring_fill_freelist(&nn
->dp
, &nn
->dp
.rx_rings
[r
]);
2597 /* Since reconfiguration requests while NFP is down are ignored we
2598 * have to wipe the entire VXLAN configuration and reinitialize it.
2600 if (nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_VXLAN
) {
2601 memset(&nn
->vxlan_ports
, 0, sizeof(nn
->vxlan_ports
));
2602 memset(&nn
->vxlan_usecnt
, 0, sizeof(nn
->vxlan_usecnt
));
2603 udp_tunnel_get_rx_info(nn
->dp
.netdev
);
2610 * nfp_net_close_stack() - Quiesce the stack (part of close)
2611 * @nn: NFP Net device to reconfigure
2613 static void nfp_net_close_stack(struct nfp_net
*nn
)
2617 disable_irq(nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
].vector
);
2618 netif_carrier_off(nn
->dp
.netdev
);
2619 nn
->link_up
= false;
2621 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2622 disable_irq(nn
->r_vecs
[r
].irq_vector
);
2623 napi_disable(&nn
->r_vecs
[r
].napi
);
2626 netif_tx_disable(nn
->dp
.netdev
);
2630 * nfp_net_close_free_all() - Free all runtime resources
2631 * @nn: NFP Net device to reconfigure
2633 static void nfp_net_close_free_all(struct nfp_net
*nn
)
2637 nfp_net_tx_rings_free(&nn
->dp
);
2638 nfp_net_rx_rings_free(&nn
->dp
);
2640 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++)
2641 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
2643 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_LSC
, NFP_NET_IRQ_LSC_IDX
);
2644 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_EXN
, NFP_NET_IRQ_EXN_IDX
);
2648 * nfp_net_netdev_close() - Called when the device is downed
2649 * @netdev: netdev structure
2651 static int nfp_net_netdev_close(struct net_device
*netdev
)
2653 struct nfp_net
*nn
= netdev_priv(netdev
);
2655 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2657 nfp_net_close_stack(nn
);
2661 nfp_net_clear_config_and_disable(nn
);
2663 /* Step 3: Free resources
2665 nfp_net_close_free_all(nn
);
2667 nn_dbg(nn
, "%s down", netdev
->name
);
2671 void nfp_ctrl_close(struct nfp_net
*nn
)
2677 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2678 disable_irq(nn
->r_vecs
[r
].irq_vector
);
2679 tasklet_disable(&nn
->r_vecs
[r
].tasklet
);
2682 nfp_net_clear_config_and_disable(nn
);
2684 nfp_net_close_free_all(nn
);
2690 * nfp_net_open_stack() - Start the device from stack's perspective
2691 * @nn: NFP Net device to reconfigure
2693 static void nfp_net_open_stack(struct nfp_net
*nn
)
2697 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2698 napi_enable(&nn
->r_vecs
[r
].napi
);
2699 enable_irq(nn
->r_vecs
[r
].irq_vector
);
2702 netif_tx_wake_all_queues(nn
->dp
.netdev
);
2704 enable_irq(nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
].vector
);
2705 nfp_net_read_link_status(nn
);
2708 static int nfp_net_open_alloc_all(struct nfp_net
*nn
)
2712 err
= nfp_net_aux_irq_request(nn
, NFP_NET_CFG_EXN
, "%s-exn",
2713 nn
->exn_name
, sizeof(nn
->exn_name
),
2714 NFP_NET_IRQ_EXN_IDX
, nn
->exn_handler
);
2717 err
= nfp_net_aux_irq_request(nn
, NFP_NET_CFG_LSC
, "%s-lsc",
2718 nn
->lsc_name
, sizeof(nn
->lsc_name
),
2719 NFP_NET_IRQ_LSC_IDX
, nn
->lsc_handler
);
2722 disable_irq(nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
].vector
);
2724 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2725 err
= nfp_net_prepare_vector(nn
, &nn
->r_vecs
[r
], r
);
2727 goto err_cleanup_vec_p
;
2730 err
= nfp_net_rx_rings_prepare(nn
, &nn
->dp
);
2732 goto err_cleanup_vec
;
2734 err
= nfp_net_tx_rings_prepare(nn
, &nn
->dp
);
2736 goto err_free_rx_rings
;
2738 for (r
= 0; r
< nn
->max_r_vecs
; r
++)
2739 nfp_net_vector_assign_rings(&nn
->dp
, &nn
->r_vecs
[r
], r
);
2744 nfp_net_rx_rings_free(&nn
->dp
);
2746 r
= nn
->dp
.num_r_vecs
;
2749 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
2750 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_LSC
, NFP_NET_IRQ_LSC_IDX
);
2752 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_EXN
, NFP_NET_IRQ_EXN_IDX
);
2756 static int nfp_net_netdev_open(struct net_device
*netdev
)
2758 struct nfp_net
*nn
= netdev_priv(netdev
);
2761 /* Step 1: Allocate resources for rings and the like
2762 * - Request interrupts
2763 * - Allocate RX and TX ring resources
2764 * - Setup initial RSS table
2766 err
= nfp_net_open_alloc_all(nn
);
2770 err
= netif_set_real_num_tx_queues(netdev
, nn
->dp
.num_stack_tx_rings
);
2774 err
= netif_set_real_num_rx_queues(netdev
, nn
->dp
.num_rx_rings
);
2778 /* Step 2: Configure the NFP
2779 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2780 * - Write MAC address (in case it changed)
2782 * - Set the Freelist buffer size
2785 err
= nfp_net_set_config_and_enable(nn
);
2789 /* Step 3: Enable for kernel
2790 * - put some freelist descriptors on each RX ring
2791 * - enable NAPI on each ring
2792 * - enable all TX queues
2795 nfp_net_open_stack(nn
);
2800 nfp_net_close_free_all(nn
);
2804 int nfp_ctrl_open(struct nfp_net
*nn
)
2808 /* ring dumping depends on vNICs being opened/closed under rtnl */
2811 err
= nfp_net_open_alloc_all(nn
);
2815 err
= nfp_net_set_config_and_enable(nn
);
2819 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++)
2820 enable_irq(nn
->r_vecs
[r
].irq_vector
);
2827 nfp_net_close_free_all(nn
);
2833 static void nfp_net_set_rx_mode(struct net_device
*netdev
)
2835 struct nfp_net
*nn
= netdev_priv(netdev
);
2838 new_ctrl
= nn
->dp
.ctrl
;
2840 if (netdev
->flags
& IFF_PROMISC
) {
2841 if (nn
->cap
& NFP_NET_CFG_CTRL_PROMISC
)
2842 new_ctrl
|= NFP_NET_CFG_CTRL_PROMISC
;
2844 nn_warn(nn
, "FW does not support promiscuous mode\n");
2846 new_ctrl
&= ~NFP_NET_CFG_CTRL_PROMISC
;
2849 if (new_ctrl
== nn
->dp
.ctrl
)
2852 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
2853 nfp_net_reconfig_post(nn
, NFP_NET_CFG_UPDATE_GEN
);
2855 nn
->dp
.ctrl
= new_ctrl
;
2858 static void nfp_net_rss_init_itbl(struct nfp_net
*nn
)
2862 for (i
= 0; i
< sizeof(nn
->rss_itbl
); i
++)
2864 ethtool_rxfh_indir_default(i
, nn
->dp
.num_rx_rings
);
2867 static void nfp_net_dp_swap(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2869 struct nfp_net_dp new_dp
= *dp
;
2874 nn
->dp
.netdev
->mtu
= new_dp
.mtu
;
2876 if (!netif_is_rxfh_configured(nn
->dp
.netdev
))
2877 nfp_net_rss_init_itbl(nn
);
2880 static int nfp_net_dp_swap_enable(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2885 nfp_net_dp_swap(nn
, dp
);
2887 for (r
= 0; r
< nn
->max_r_vecs
; r
++)
2888 nfp_net_vector_assign_rings(&nn
->dp
, &nn
->r_vecs
[r
], r
);
2890 err
= netif_set_real_num_rx_queues(nn
->dp
.netdev
, nn
->dp
.num_rx_rings
);
2894 if (nn
->dp
.netdev
->real_num_tx_queues
!= nn
->dp
.num_stack_tx_rings
) {
2895 err
= netif_set_real_num_tx_queues(nn
->dp
.netdev
,
2896 nn
->dp
.num_stack_tx_rings
);
2901 return nfp_net_set_config_and_enable(nn
);
2904 struct nfp_net_dp
*nfp_net_clone_dp(struct nfp_net
*nn
)
2906 struct nfp_net_dp
*new;
2908 new = kmalloc(sizeof(*new), GFP_KERNEL
);
2914 /* Clear things which need to be recomputed */
2916 new->tx_rings
= NULL
;
2917 new->rx_rings
= NULL
;
2918 new->num_r_vecs
= 0;
2919 new->num_stack_tx_rings
= 0;
2925 nfp_net_check_config(struct nfp_net
*nn
, struct nfp_net_dp
*dp
,
2926 struct netlink_ext_ack
*extack
)
2928 /* XDP-enabled tests */
2931 if (dp
->fl_bufsz
> PAGE_SIZE
) {
2932 NL_SET_ERR_MSG_MOD(extack
, "MTU too large w/ XDP enabled");
2935 if (dp
->num_tx_rings
> nn
->max_tx_rings
) {
2936 NL_SET_ERR_MSG_MOD(extack
, "Insufficient number of TX rings w/ XDP enabled");
2943 int nfp_net_ring_reconfig(struct nfp_net
*nn
, struct nfp_net_dp
*dp
,
2944 struct netlink_ext_ack
*extack
)
2948 dp
->fl_bufsz
= nfp_net_calc_fl_bufsz(dp
);
2950 dp
->num_stack_tx_rings
= dp
->num_tx_rings
;
2952 dp
->num_stack_tx_rings
-= dp
->num_rx_rings
;
2954 dp
->num_r_vecs
= max(dp
->num_rx_rings
, dp
->num_stack_tx_rings
);
2956 err
= nfp_net_check_config(nn
, dp
, extack
);
2960 if (!netif_running(dp
->netdev
)) {
2961 nfp_net_dp_swap(nn
, dp
);
2966 /* Prepare new rings */
2967 for (r
= nn
->dp
.num_r_vecs
; r
< dp
->num_r_vecs
; r
++) {
2968 err
= nfp_net_prepare_vector(nn
, &nn
->r_vecs
[r
], r
);
2971 goto err_cleanup_vecs
;
2975 err
= nfp_net_rx_rings_prepare(nn
, dp
);
2977 goto err_cleanup_vecs
;
2979 err
= nfp_net_tx_rings_prepare(nn
, dp
);
2983 /* Stop device, swap in new rings, try to start the firmware */
2984 nfp_net_close_stack(nn
);
2985 nfp_net_clear_config_and_disable(nn
);
2987 err
= nfp_net_dp_swap_enable(nn
, dp
);
2991 nfp_net_clear_config_and_disable(nn
);
2993 /* Try with old configuration and old rings */
2994 err2
= nfp_net_dp_swap_enable(nn
, dp
);
2996 nn_err(nn
, "Can't restore ring config - FW communication failed (%d,%d)\n",
2999 for (r
= dp
->num_r_vecs
- 1; r
>= nn
->dp
.num_r_vecs
; r
--)
3000 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
3002 nfp_net_rx_rings_free(dp
);
3003 nfp_net_tx_rings_free(dp
);
3005 nfp_net_open_stack(nn
);
3012 nfp_net_rx_rings_free(dp
);
3014 for (r
= dp
->num_r_vecs
- 1; r
>= nn
->dp
.num_r_vecs
; r
--)
3015 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
3020 static int nfp_net_change_mtu(struct net_device
*netdev
, int new_mtu
)
3022 struct nfp_net
*nn
= netdev_priv(netdev
);
3023 struct nfp_net_dp
*dp
;
3025 dp
= nfp_net_clone_dp(nn
);
3031 return nfp_net_ring_reconfig(nn
, dp
, NULL
);
3035 nfp_net_vlan_rx_add_vid(struct net_device
*netdev
, __be16 proto
, u16 vid
)
3037 struct nfp_net
*nn
= netdev_priv(netdev
);
3039 /* Priority tagged packets with vlan id 0 are processed by the
3040 * NFP as untagged packets
3045 nn_writew(nn
, NFP_NET_CFG_VLAN_FILTER_VID
, vid
);
3046 nn_writew(nn
, NFP_NET_CFG_VLAN_FILTER_PROTO
, ETH_P_8021Q
);
3048 return nfp_net_reconfig_mbox(nn
, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD
);
3052 nfp_net_vlan_rx_kill_vid(struct net_device
*netdev
, __be16 proto
, u16 vid
)
3054 struct nfp_net
*nn
= netdev_priv(netdev
);
3056 /* Priority tagged packets with vlan id 0 are processed by the
3057 * NFP as untagged packets
3062 nn_writew(nn
, NFP_NET_CFG_VLAN_FILTER_VID
, vid
);
3063 nn_writew(nn
, NFP_NET_CFG_VLAN_FILTER_PROTO
, ETH_P_8021Q
);
3065 return nfp_net_reconfig_mbox(nn
, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL
);
3068 static void nfp_net_stat64(struct net_device
*netdev
,
3069 struct rtnl_link_stats64
*stats
)
3071 struct nfp_net
*nn
= netdev_priv(netdev
);
3074 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
3075 struct nfp_net_r_vector
*r_vec
= &nn
->r_vecs
[r
];
3080 start
= u64_stats_fetch_begin(&r_vec
->rx_sync
);
3081 data
[0] = r_vec
->rx_pkts
;
3082 data
[1] = r_vec
->rx_bytes
;
3083 data
[2] = r_vec
->rx_drops
;
3084 } while (u64_stats_fetch_retry(&r_vec
->rx_sync
, start
));
3085 stats
->rx_packets
+= data
[0];
3086 stats
->rx_bytes
+= data
[1];
3087 stats
->rx_dropped
+= data
[2];
3090 start
= u64_stats_fetch_begin(&r_vec
->tx_sync
);
3091 data
[0] = r_vec
->tx_pkts
;
3092 data
[1] = r_vec
->tx_bytes
;
3093 data
[2] = r_vec
->tx_errors
;
3094 } while (u64_stats_fetch_retry(&r_vec
->tx_sync
, start
));
3095 stats
->tx_packets
+= data
[0];
3096 stats
->tx_bytes
+= data
[1];
3097 stats
->tx_errors
+= data
[2];
3101 static int nfp_net_set_features(struct net_device
*netdev
,
3102 netdev_features_t features
)
3104 netdev_features_t changed
= netdev
->features
^ features
;
3105 struct nfp_net
*nn
= netdev_priv(netdev
);
3109 /* Assume this is not called with features we have not advertised */
3111 new_ctrl
= nn
->dp
.ctrl
;
3113 if (changed
& NETIF_F_RXCSUM
) {
3114 if (features
& NETIF_F_RXCSUM
)
3115 new_ctrl
|= nn
->cap
& NFP_NET_CFG_CTRL_RXCSUM_ANY
;
3117 new_ctrl
&= ~NFP_NET_CFG_CTRL_RXCSUM_ANY
;
3120 if (changed
& (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
)) {
3121 if (features
& (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
))
3122 new_ctrl
|= NFP_NET_CFG_CTRL_TXCSUM
;
3124 new_ctrl
&= ~NFP_NET_CFG_CTRL_TXCSUM
;
3127 if (changed
& (NETIF_F_TSO
| NETIF_F_TSO6
)) {
3128 if (features
& (NETIF_F_TSO
| NETIF_F_TSO6
))
3129 new_ctrl
|= nn
->cap
& NFP_NET_CFG_CTRL_LSO2
?:
3130 NFP_NET_CFG_CTRL_LSO
;
3132 new_ctrl
&= ~NFP_NET_CFG_CTRL_LSO_ANY
;
3135 if (changed
& NETIF_F_HW_VLAN_CTAG_RX
) {
3136 if (features
& NETIF_F_HW_VLAN_CTAG_RX
)
3137 new_ctrl
|= NFP_NET_CFG_CTRL_RXVLAN
;
3139 new_ctrl
&= ~NFP_NET_CFG_CTRL_RXVLAN
;
3142 if (changed
& NETIF_F_HW_VLAN_CTAG_TX
) {
3143 if (features
& NETIF_F_HW_VLAN_CTAG_TX
)
3144 new_ctrl
|= NFP_NET_CFG_CTRL_TXVLAN
;
3146 new_ctrl
&= ~NFP_NET_CFG_CTRL_TXVLAN
;
3149 if (changed
& NETIF_F_HW_VLAN_CTAG_FILTER
) {
3150 if (features
& NETIF_F_HW_VLAN_CTAG_FILTER
)
3151 new_ctrl
|= NFP_NET_CFG_CTRL_CTAG_FILTER
;
3153 new_ctrl
&= ~NFP_NET_CFG_CTRL_CTAG_FILTER
;
3156 if (changed
& NETIF_F_SG
) {
3157 if (features
& NETIF_F_SG
)
3158 new_ctrl
|= NFP_NET_CFG_CTRL_GATHER
;
3160 new_ctrl
&= ~NFP_NET_CFG_CTRL_GATHER
;
3163 if (changed
& NETIF_F_HW_TC
&& nfp_app_tc_busy(nn
->app
, nn
)) {
3164 nn_err(nn
, "Cannot disable HW TC offload while in use\n");
3168 nn_dbg(nn
, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
3169 netdev
->features
, features
, changed
);
3171 if (new_ctrl
== nn
->dp
.ctrl
)
3174 nn_dbg(nn
, "NIC ctrl: 0x%x -> 0x%x\n", nn
->dp
.ctrl
, new_ctrl
);
3175 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
3176 err
= nfp_net_reconfig(nn
, NFP_NET_CFG_UPDATE_GEN
);
3180 nn
->dp
.ctrl
= new_ctrl
;
3185 static netdev_features_t
3186 nfp_net_features_check(struct sk_buff
*skb
, struct net_device
*dev
,
3187 netdev_features_t features
)
3191 /* We can't do TSO over double tagged packets (802.1AD) */
3192 features
&= vlan_features_check(skb
, features
);
3194 if (!skb
->encapsulation
)
3197 /* Ensure that inner L4 header offset fits into TX descriptor field */
3198 if (skb_is_gso(skb
)) {
3201 hdrlen
= skb_inner_transport_header(skb
) - skb
->data
+
3202 inner_tcp_hdrlen(skb
);
3204 if (unlikely(hdrlen
> NFP_NET_LSO_MAX_HDR_SZ
))
3205 features
&= ~NETIF_F_GSO_MASK
;
3208 /* VXLAN/GRE check */
3209 switch (vlan_get_protocol(skb
)) {
3210 case htons(ETH_P_IP
):
3211 l4_hdr
= ip_hdr(skb
)->protocol
;
3213 case htons(ETH_P_IPV6
):
3214 l4_hdr
= ipv6_hdr(skb
)->nexthdr
;
3217 return features
& ~(NETIF_F_CSUM_MASK
| NETIF_F_GSO_MASK
);
3220 if (skb
->inner_protocol_type
!= ENCAP_TYPE_ETHER
||
3221 skb
->inner_protocol
!= htons(ETH_P_TEB
) ||
3222 (l4_hdr
!= IPPROTO_UDP
&& l4_hdr
!= IPPROTO_GRE
) ||
3223 (l4_hdr
== IPPROTO_UDP
&&
3224 (skb_inner_mac_header(skb
) - skb_transport_header(skb
) !=
3225 sizeof(struct udphdr
) + sizeof(struct vxlanhdr
))))
3226 return features
& ~(NETIF_F_CSUM_MASK
| NETIF_F_GSO_MASK
);
3232 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
3233 * @nn: NFP Net device to reconfigure
3234 * @idx: Index into the port table where new port should be written
3235 * @port: UDP port to configure (pass zero to remove VXLAN port)
3237 static void nfp_net_set_vxlan_port(struct nfp_net
*nn
, int idx
, __be16 port
)
3241 nn
->vxlan_ports
[idx
] = port
;
3243 if (!(nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_VXLAN
))
3246 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS
& 1);
3247 for (i
= 0; i
< NFP_NET_N_VXLAN_PORTS
; i
+= 2)
3248 nn_writel(nn
, NFP_NET_CFG_VXLAN_PORT
+ i
* sizeof(port
),
3249 be16_to_cpu(nn
->vxlan_ports
[i
+ 1]) << 16 |
3250 be16_to_cpu(nn
->vxlan_ports
[i
]));
3252 nfp_net_reconfig_post(nn
, NFP_NET_CFG_UPDATE_VXLAN
);
3256 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
3257 * @nn: NFP Network structure
3258 * @port: UDP port to look for
3260 * Return: if the port is already in the table -- it's position;
3261 * if the port is not in the table -- free position to use;
3262 * if the table is full -- -ENOSPC.
3264 static int nfp_net_find_vxlan_idx(struct nfp_net
*nn
, __be16 port
)
3266 int i
, free_idx
= -ENOSPC
;
3268 for (i
= 0; i
< NFP_NET_N_VXLAN_PORTS
; i
++) {
3269 if (nn
->vxlan_ports
[i
] == port
)
3271 if (!nn
->vxlan_usecnt
[i
])
3278 static void nfp_net_add_vxlan_port(struct net_device
*netdev
,
3279 struct udp_tunnel_info
*ti
)
3281 struct nfp_net
*nn
= netdev_priv(netdev
);
3284 if (ti
->type
!= UDP_TUNNEL_TYPE_VXLAN
)
3287 idx
= nfp_net_find_vxlan_idx(nn
, ti
->port
);
3291 if (!nn
->vxlan_usecnt
[idx
]++)
3292 nfp_net_set_vxlan_port(nn
, idx
, ti
->port
);
3295 static void nfp_net_del_vxlan_port(struct net_device
*netdev
,
3296 struct udp_tunnel_info
*ti
)
3298 struct nfp_net
*nn
= netdev_priv(netdev
);
3301 if (ti
->type
!= UDP_TUNNEL_TYPE_VXLAN
)
3304 idx
= nfp_net_find_vxlan_idx(nn
, ti
->port
);
3305 if (idx
== -ENOSPC
|| !nn
->vxlan_usecnt
[idx
])
3308 if (!--nn
->vxlan_usecnt
[idx
])
3309 nfp_net_set_vxlan_port(nn
, idx
, 0);
3313 nfp_net_xdp_setup_drv(struct nfp_net
*nn
, struct bpf_prog
*prog
,
3314 struct netlink_ext_ack
*extack
)
3316 struct nfp_net_dp
*dp
;
3318 if (!prog
== !nn
->dp
.xdp_prog
) {
3319 WRITE_ONCE(nn
->dp
.xdp_prog
, prog
);
3323 dp
= nfp_net_clone_dp(nn
);
3327 dp
->xdp_prog
= prog
;
3328 dp
->num_tx_rings
+= prog
? nn
->dp
.num_rx_rings
: -nn
->dp
.num_rx_rings
;
3329 dp
->rx_dma_dir
= prog
? DMA_BIDIRECTIONAL
: DMA_FROM_DEVICE
;
3330 dp
->rx_dma_off
= prog
? XDP_PACKET_HEADROOM
- nn
->dp
.rx_offset
: 0;
3332 /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
3333 return nfp_net_ring_reconfig(nn
, dp
, extack
);
3337 nfp_net_xdp_setup(struct nfp_net
*nn
, struct bpf_prog
*prog
, u32 flags
,
3338 struct netlink_ext_ack
*extack
)
3340 struct bpf_prog
*drv_prog
, *offload_prog
;
3343 if (nn
->xdp_prog
&& (flags
^ nn
->xdp_flags
) & XDP_FLAGS_MODES
)
3346 /* Load both when no flags set to allow easy activation of driver path
3347 * when program is replaced by one which can't be offloaded.
3349 drv_prog
= flags
& XDP_FLAGS_HW_MODE
? NULL
: prog
;
3350 offload_prog
= flags
& XDP_FLAGS_DRV_MODE
? NULL
: prog
;
3352 err
= nfp_net_xdp_setup_drv(nn
, drv_prog
, extack
);
3356 err
= nfp_app_xdp_offload(nn
->app
, nn
, offload_prog
);
3357 if (err
&& flags
& XDP_FLAGS_HW_MODE
)
3361 bpf_prog_put(nn
->xdp_prog
);
3362 nn
->xdp_prog
= prog
;
3363 nn
->xdp_flags
= flags
;
3368 static int nfp_net_xdp(struct net_device
*netdev
, struct netdev_xdp
*xdp
)
3370 struct nfp_net
*nn
= netdev_priv(netdev
);
3372 switch (xdp
->command
) {
3373 case XDP_SETUP_PROG
:
3374 case XDP_SETUP_PROG_HW
:
3375 return nfp_net_xdp_setup(nn
, xdp
->prog
, xdp
->flags
,
3377 case XDP_QUERY_PROG
:
3378 xdp
->prog_attached
= !!nn
->xdp_prog
;
3379 if (nn
->dp
.bpf_offload_xdp
)
3380 xdp
->prog_attached
= XDP_ATTACHED_HW
;
3381 xdp
->prog_id
= nn
->xdp_prog
? nn
->xdp_prog
->aux
->id
: 0;
3388 static int nfp_net_set_mac_address(struct net_device
*netdev
, void *addr
)
3390 struct nfp_net
*nn
= netdev_priv(netdev
);
3391 struct sockaddr
*saddr
= addr
;
3394 err
= eth_prepare_mac_addr_change(netdev
, addr
);
3398 nfp_net_write_mac_addr(nn
, saddr
->sa_data
);
3400 err
= nfp_net_reconfig(nn
, NFP_NET_CFG_UPDATE_MACADDR
);
3404 eth_commit_mac_addr_change(netdev
, addr
);
3409 const struct net_device_ops nfp_net_netdev_ops
= {
3410 .ndo_open
= nfp_net_netdev_open
,
3411 .ndo_stop
= nfp_net_netdev_close
,
3412 .ndo_start_xmit
= nfp_net_tx
,
3413 .ndo_get_stats64
= nfp_net_stat64
,
3414 .ndo_vlan_rx_add_vid
= nfp_net_vlan_rx_add_vid
,
3415 .ndo_vlan_rx_kill_vid
= nfp_net_vlan_rx_kill_vid
,
3416 .ndo_setup_tc
= nfp_port_setup_tc
,
3417 .ndo_tx_timeout
= nfp_net_tx_timeout
,
3418 .ndo_set_rx_mode
= nfp_net_set_rx_mode
,
3419 .ndo_change_mtu
= nfp_net_change_mtu
,
3420 .ndo_set_mac_address
= nfp_net_set_mac_address
,
3421 .ndo_set_features
= nfp_net_set_features
,
3422 .ndo_features_check
= nfp_net_features_check
,
3423 .ndo_get_phys_port_name
= nfp_port_get_phys_port_name
,
3424 .ndo_udp_tunnel_add
= nfp_net_add_vxlan_port
,
3425 .ndo_udp_tunnel_del
= nfp_net_del_vxlan_port
,
3426 .ndo_xdp
= nfp_net_xdp
,
3430 * nfp_net_info() - Print general info about the NIC
3431 * @nn: NFP Net device to reconfigure
3433 void nfp_net_info(struct nfp_net
*nn
)
3435 nn_info(nn
, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3436 nn
->dp
.is_vf
? "VF " : "",
3437 nn
->dp
.num_tx_rings
, nn
->max_tx_rings
,
3438 nn
->dp
.num_rx_rings
, nn
->max_rx_rings
);
3439 nn_info(nn
, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
3440 nn
->fw_ver
.resv
, nn
->fw_ver
.class,
3441 nn
->fw_ver
.major
, nn
->fw_ver
.minor
,
3443 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",
3445 nn
->cap
& NFP_NET_CFG_CTRL_PROMISC
? "PROMISC " : "",
3446 nn
->cap
& NFP_NET_CFG_CTRL_L2BC
? "L2BCFILT " : "",
3447 nn
->cap
& NFP_NET_CFG_CTRL_L2MC
? "L2MCFILT " : "",
3448 nn
->cap
& NFP_NET_CFG_CTRL_RXCSUM
? "RXCSUM " : "",
3449 nn
->cap
& NFP_NET_CFG_CTRL_TXCSUM
? "TXCSUM " : "",
3450 nn
->cap
& NFP_NET_CFG_CTRL_RXVLAN
? "RXVLAN " : "",
3451 nn
->cap
& NFP_NET_CFG_CTRL_TXVLAN
? "TXVLAN " : "",
3452 nn
->cap
& NFP_NET_CFG_CTRL_SCATTER
? "SCATTER " : "",
3453 nn
->cap
& NFP_NET_CFG_CTRL_GATHER
? "GATHER " : "",
3454 nn
->cap
& NFP_NET_CFG_CTRL_LSO
? "TSO1 " : "",
3455 nn
->cap
& NFP_NET_CFG_CTRL_LSO2
? "TSO2 " : "",
3456 nn
->cap
& NFP_NET_CFG_CTRL_RSS
? "RSS1 " : "",
3457 nn
->cap
& NFP_NET_CFG_CTRL_RSS2
? "RSS2 " : "",
3458 nn
->cap
& NFP_NET_CFG_CTRL_CTAG_FILTER
? "CTAG_FILTER " : "",
3459 nn
->cap
& NFP_NET_CFG_CTRL_L2SWITCH
? "L2SWITCH " : "",
3460 nn
->cap
& NFP_NET_CFG_CTRL_MSIXAUTO
? "AUTOMASK " : "",
3461 nn
->cap
& NFP_NET_CFG_CTRL_IRQMOD
? "IRQMOD " : "",
3462 nn
->cap
& NFP_NET_CFG_CTRL_VXLAN
? "VXLAN " : "",
3463 nn
->cap
& NFP_NET_CFG_CTRL_NVGRE
? "NVGRE " : "",
3464 nn
->cap
& NFP_NET_CFG_CTRL_CSUM_COMPLETE
?
3465 "RXCSUM_COMPLETE " : "",
3466 nn
->cap
& NFP_NET_CFG_CTRL_LIVE_ADDR
? "LIVE_ADDR " : "",
3467 nfp_app_extra_cap(nn
->app
, nn
));
3471 * nfp_net_alloc() - Allocate netdev and related structure
3473 * @needs_netdev: Whether to allocate a netdev for this vNIC
3474 * @max_tx_rings: Maximum number of TX rings supported by device
3475 * @max_rx_rings: Maximum number of RX rings supported by device
3477 * This function allocates a netdev device and fills in the initial
3478 * part of the @struct nfp_net structure. In case of control device
3479 * nfp_net structure is allocated without the netdev.
3481 * Return: NFP Net device structure, or ERR_PTR on error.
3483 struct nfp_net
*nfp_net_alloc(struct pci_dev
*pdev
, bool needs_netdev
,
3484 unsigned int max_tx_rings
,
3485 unsigned int max_rx_rings
)
3490 struct net_device
*netdev
;
3492 netdev
= alloc_etherdev_mqs(sizeof(struct nfp_net
),
3493 max_tx_rings
, max_rx_rings
);
3495 return ERR_PTR(-ENOMEM
);
3497 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
3498 nn
= netdev_priv(netdev
);
3499 nn
->dp
.netdev
= netdev
;
3501 nn
= vzalloc(sizeof(*nn
));
3503 return ERR_PTR(-ENOMEM
);
3506 nn
->dp
.dev
= &pdev
->dev
;
3509 nn
->max_tx_rings
= max_tx_rings
;
3510 nn
->max_rx_rings
= max_rx_rings
;
3512 nn
->dp
.num_tx_rings
= min_t(unsigned int,
3513 max_tx_rings
, num_online_cpus());
3514 nn
->dp
.num_rx_rings
= min_t(unsigned int, max_rx_rings
,
3515 netif_get_num_default_rss_queues());
3517 nn
->dp
.num_r_vecs
= max(nn
->dp
.num_tx_rings
, nn
->dp
.num_rx_rings
);
3518 nn
->dp
.num_r_vecs
= min_t(unsigned int,
3519 nn
->dp
.num_r_vecs
, num_online_cpus());
3521 nn
->dp
.txd_cnt
= NFP_NET_TX_DESCS_DEFAULT
;
3522 nn
->dp
.rxd_cnt
= NFP_NET_RX_DESCS_DEFAULT
;
3524 spin_lock_init(&nn
->reconfig_lock
);
3525 spin_lock_init(&nn
->link_status_lock
);
3527 setup_timer(&nn
->reconfig_timer
,
3528 nfp_net_reconfig_timer
, (unsigned long)nn
);
3534 * nfp_net_free() - Undo what @nfp_net_alloc() did
3535 * @nn: NFP Net device to reconfigure
3537 void nfp_net_free(struct nfp_net
*nn
)
3540 bpf_prog_put(nn
->xdp_prog
);
3543 free_netdev(nn
->dp
.netdev
);
3549 * nfp_net_rss_key_sz() - Get current size of the RSS key
3550 * @nn: NFP Net device instance
3552 * Return: size of the RSS key for currently selected hash function.
3554 unsigned int nfp_net_rss_key_sz(struct nfp_net
*nn
)
3556 switch (nn
->rss_hfunc
) {
3557 case ETH_RSS_HASH_TOP
:
3558 return NFP_NET_CFG_RSS_KEY_SZ
;
3559 case ETH_RSS_HASH_XOR
:
3561 case ETH_RSS_HASH_CRC32
:
3565 nn_warn(nn
, "Unknown hash function: %u\n", nn
->rss_hfunc
);
3570 * nfp_net_rss_init() - Set the initial RSS parameters
3571 * @nn: NFP Net device to reconfigure
3573 static void nfp_net_rss_init(struct nfp_net
*nn
)
3575 unsigned long func_bit
, rss_cap_hfunc
;
3578 /* Read the RSS function capability and select first supported func */
3579 reg
= nn_readl(nn
, NFP_NET_CFG_RSS_CAP
);
3580 rss_cap_hfunc
= FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC
, reg
);
3582 rss_cap_hfunc
= FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC
,
3583 NFP_NET_CFG_RSS_TOEPLITZ
);
3585 func_bit
= find_first_bit(&rss_cap_hfunc
, NFP_NET_CFG_RSS_HFUNCS
);
3586 if (func_bit
== NFP_NET_CFG_RSS_HFUNCS
) {
3587 dev_warn(nn
->dp
.dev
,
3588 "Bad RSS config, defaulting to Toeplitz hash\n");
3589 func_bit
= ETH_RSS_HASH_TOP_BIT
;
3591 nn
->rss_hfunc
= 1 << func_bit
;
3593 netdev_rss_key_fill(nn
->rss_key
, nfp_net_rss_key_sz(nn
));
3595 nfp_net_rss_init_itbl(nn
);
3597 /* Enable IPv4/IPv6 TCP by default */
3598 nn
->rss_cfg
= NFP_NET_CFG_RSS_IPV4_TCP
|
3599 NFP_NET_CFG_RSS_IPV6_TCP
|
3600 FIELD_PREP(NFP_NET_CFG_RSS_HFUNC
, nn
->rss_hfunc
) |
3601 NFP_NET_CFG_RSS_MASK
;
3605 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3606 * @nn: NFP Net device to reconfigure
3608 static void nfp_net_irqmod_init(struct nfp_net
*nn
)
3610 nn
->rx_coalesce_usecs
= 50;
3611 nn
->rx_coalesce_max_frames
= 64;
3612 nn
->tx_coalesce_usecs
= 50;
3613 nn
->tx_coalesce_max_frames
= 64;
3616 static void nfp_net_netdev_init(struct nfp_net
*nn
)
3618 struct net_device
*netdev
= nn
->dp
.netdev
;
3620 nfp_net_write_mac_addr(nn
, nn
->dp
.netdev
->dev_addr
);
3622 netdev
->mtu
= nn
->dp
.mtu
;
3624 /* Advertise/enable offloads based on capabilities
3626 * Note: netdev->features show the currently enabled features
3627 * and netdev->hw_features advertises which features are
3628 * supported. By default we enable most features.
3630 if (nn
->cap
& NFP_NET_CFG_CTRL_LIVE_ADDR
)
3631 netdev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
3633 netdev
->hw_features
= NETIF_F_HIGHDMA
;
3634 if (nn
->cap
& NFP_NET_CFG_CTRL_RXCSUM_ANY
) {
3635 netdev
->hw_features
|= NETIF_F_RXCSUM
;
3636 nn
->dp
.ctrl
|= nn
->cap
& NFP_NET_CFG_CTRL_RXCSUM_ANY
;
3638 if (nn
->cap
& NFP_NET_CFG_CTRL_TXCSUM
) {
3639 netdev
->hw_features
|= NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
3640 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_TXCSUM
;
3642 if (nn
->cap
& NFP_NET_CFG_CTRL_GATHER
) {
3643 netdev
->hw_features
|= NETIF_F_SG
;
3644 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_GATHER
;
3646 if ((nn
->cap
& NFP_NET_CFG_CTRL_LSO
&& nn
->fw_ver
.major
> 2) ||
3647 nn
->cap
& NFP_NET_CFG_CTRL_LSO2
) {
3648 netdev
->hw_features
|= NETIF_F_TSO
| NETIF_F_TSO6
;
3649 nn
->dp
.ctrl
|= nn
->cap
& NFP_NET_CFG_CTRL_LSO2
?:
3650 NFP_NET_CFG_CTRL_LSO
;
3652 if (nn
->cap
& NFP_NET_CFG_CTRL_RSS_ANY
)
3653 netdev
->hw_features
|= NETIF_F_RXHASH
;
3654 if (nn
->cap
& NFP_NET_CFG_CTRL_VXLAN
&&
3655 nn
->cap
& NFP_NET_CFG_CTRL_NVGRE
) {
3656 if (nn
->cap
& NFP_NET_CFG_CTRL_LSO
)
3657 netdev
->hw_features
|= NETIF_F_GSO_GRE
|
3658 NETIF_F_GSO_UDP_TUNNEL
;
3659 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_VXLAN
| NFP_NET_CFG_CTRL_NVGRE
;
3661 netdev
->hw_enc_features
= netdev
->hw_features
;
3664 netdev
->vlan_features
= netdev
->hw_features
;
3666 if (nn
->cap
& NFP_NET_CFG_CTRL_RXVLAN
) {
3667 netdev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_RX
;
3668 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_RXVLAN
;
3670 if (nn
->cap
& NFP_NET_CFG_CTRL_TXVLAN
) {
3671 if (nn
->cap
& NFP_NET_CFG_CTRL_LSO2
) {
3672 nn_warn(nn
, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
3674 netdev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
;
3675 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_TXVLAN
;
3678 if (nn
->cap
& NFP_NET_CFG_CTRL_CTAG_FILTER
) {
3679 netdev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_FILTER
;
3680 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_CTAG_FILTER
;
3683 netdev
->features
= netdev
->hw_features
;
3685 if (nfp_app_has_tc(nn
->app
))
3686 netdev
->hw_features
|= NETIF_F_HW_TC
;
3688 /* Advertise but disable TSO by default. */
3689 netdev
->features
&= ~(NETIF_F_TSO
| NETIF_F_TSO6
);
3690 nn
->dp
.ctrl
&= ~NFP_NET_CFG_CTRL_LSO_ANY
;
3692 /* Finalise the netdev setup */
3693 netdev
->netdev_ops
= &nfp_net_netdev_ops
;
3694 netdev
->watchdog_timeo
= msecs_to_jiffies(5 * 1000);
3696 SWITCHDEV_SET_OPS(netdev
, &nfp_port_switchdev_ops
);
3698 /* MTU range: 68 - hw-specific max */
3699 netdev
->min_mtu
= ETH_MIN_MTU
;
3700 netdev
->max_mtu
= nn
->max_mtu
;
3702 netif_carrier_off(netdev
);
3704 nfp_net_set_ethtool_ops(netdev
);
3708 * nfp_net_init() - Initialise/finalise the nfp_net structure
3709 * @nn: NFP Net device structure
3711 * Return: 0 on success or negative errno on error.
3713 int nfp_net_init(struct nfp_net
*nn
)
3717 nn
->dp
.rx_dma_dir
= DMA_FROM_DEVICE
;
3719 /* Get some of the read-only fields from the BAR */
3720 nn
->cap
= nn_readl(nn
, NFP_NET_CFG_CAP
);
3721 nn
->max_mtu
= nn_readl(nn
, NFP_NET_CFG_MAX_MTU
);
3723 /* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
3724 * we allow use of non-chained metadata if RSS(v1) is the only
3725 * advertised capability requiring metadata.
3727 nn
->dp
.chained_metadata_format
= nn
->fw_ver
.major
== 4 ||
3729 !(nn
->cap
& NFP_NET_CFG_CTRL_RSS
) ||
3730 nn
->cap
& NFP_NET_CFG_CTRL_CHAIN_META
;
3731 /* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
3732 * it has the same meaning as RSSv2.
3734 if (nn
->dp
.chained_metadata_format
&& nn
->fw_ver
.major
!= 4)
3735 nn
->cap
&= ~NFP_NET_CFG_CTRL_RSS
;
3737 /* Determine RX packet/metadata boundary offset */
3738 if (nn
->fw_ver
.major
>= 2) {
3741 reg
= nn_readl(nn
, NFP_NET_CFG_RX_OFFSET
);
3742 if (reg
> NFP_NET_MAX_PREPEND
) {
3743 nn_err(nn
, "Invalid rx offset: %d\n", reg
);
3746 nn
->dp
.rx_offset
= reg
;
3748 nn
->dp
.rx_offset
= NFP_NET_RX_OFFSET
;
3751 /* Set default MTU and Freelist buffer size */
3752 if (nn
->max_mtu
< NFP_NET_DEFAULT_MTU
)
3753 nn
->dp
.mtu
= nn
->max_mtu
;
3755 nn
->dp
.mtu
= NFP_NET_DEFAULT_MTU
;
3756 nn
->dp
.fl_bufsz
= nfp_net_calc_fl_bufsz(&nn
->dp
);
3758 if (nn
->cap
& NFP_NET_CFG_CTRL_RSS_ANY
) {
3759 nfp_net_rss_init(nn
);
3760 nn
->dp
.ctrl
|= nn
->cap
& NFP_NET_CFG_CTRL_RSS2
?:
3761 NFP_NET_CFG_CTRL_RSS
;
3764 /* Allow L2 Broadcast and Multicast through by default, if supported */
3765 if (nn
->cap
& NFP_NET_CFG_CTRL_L2BC
)
3766 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_L2BC
;
3767 if (nn
->cap
& NFP_NET_CFG_CTRL_L2MC
)
3768 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_L2MC
;
3770 /* Allow IRQ moderation, if supported */
3771 if (nn
->cap
& NFP_NET_CFG_CTRL_IRQMOD
) {
3772 nfp_net_irqmod_init(nn
);
3773 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_IRQMOD
;
3777 nfp_net_netdev_init(nn
);
3779 /* Stash the re-configuration queue away. First odd queue in TX Bar */
3780 nn
->qcp_cfg
= nn
->tx_bar
+ NFP_QCP_QUEUE_ADDR_SZ
;
3782 /* Make sure the FW knows the netdev is supposed to be disabled here */
3783 nn_writel(nn
, NFP_NET_CFG_CTRL
, 0);
3784 nn_writeq(nn
, NFP_NET_CFG_TXRS_ENABLE
, 0);
3785 nn_writeq(nn
, NFP_NET_CFG_RXRS_ENABLE
, 0);
3786 err
= nfp_net_reconfig(nn
, NFP_NET_CFG_UPDATE_RING
|
3787 NFP_NET_CFG_UPDATE_GEN
);
3791 nfp_net_vecs_init(nn
);
3795 return register_netdev(nn
->dp
.netdev
);
3799 * nfp_net_clean() - Undo what nfp_net_init() did.
3800 * @nn: NFP Net device structure
3802 void nfp_net_clean(struct nfp_net
*nn
)
3807 unregister_netdev(nn
->dp
.netdev
);