2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * Netronome network device driver: Common functions between PF and VF
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
40 * Brad Petrus <brad.petrus@netronome.com>
41 * Chris Telfer <chris.telfer@netronome.com>
44 #include <linux/bitfield.h>
45 #include <linux/bpf.h>
46 #include <linux/bpf_trace.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/interrupt.h>
55 #include <linux/ipv6.h>
56 #include <linux/page_ref.h>
57 #include <linux/pci.h>
58 #include <linux/pci_regs.h>
59 #include <linux/msi.h>
60 #include <linux/ethtool.h>
61 #include <linux/log2.h>
62 #include <linux/if_vlan.h>
63 #include <linux/random.h>
65 #include <linux/ktime.h>
67 #include <net/pkt_cls.h>
68 #include <net/vxlan.h>
70 #include "nfpcore/nfp_nsp_eth.h"
71 #include "nfp_net_ctrl.h"
75 * nfp_net_get_fw_version() - Read and parse the FW version
76 * @fw_ver: Output fw_version structure to read to
77 * @ctrl_bar: Mapped address of the control BAR
79 void nfp_net_get_fw_version(struct nfp_net_fw_version
*fw_ver
,
80 void __iomem
*ctrl_bar
)
84 reg
= readl(ctrl_bar
+ NFP_NET_CFG_VERSION
);
85 put_unaligned_le32(reg
, fw_ver
);
88 static dma_addr_t
nfp_net_dma_map_rx(struct nfp_net_dp
*dp
, void *frag
)
90 return dma_map_single(dp
->dev
, frag
+ NFP_NET_RX_BUF_HEADROOM
,
91 dp
->fl_bufsz
- NFP_NET_RX_BUF_NON_DATA
,
95 static void nfp_net_dma_unmap_rx(struct nfp_net_dp
*dp
, dma_addr_t dma_addr
)
97 dma_unmap_single(dp
->dev
, dma_addr
,
98 dp
->fl_bufsz
- NFP_NET_RX_BUF_NON_DATA
,
104 * Firmware reconfig may take a while so we have two versions of it -
105 * synchronous and asynchronous (posted). All synchronous callers are holding
106 * RTNL so we don't have to worry about serializing them.
108 static void nfp_net_reconfig_start(struct nfp_net
*nn
, u32 update
)
110 nn_writel(nn
, NFP_NET_CFG_UPDATE
, update
);
111 /* ensure update is written before pinging HW */
113 nfp_qcp_wr_ptr_add(nn
->qcp_cfg
, 1);
116 /* Pass 0 as update to run posted reconfigs. */
117 static void nfp_net_reconfig_start_async(struct nfp_net
*nn
, u32 update
)
119 update
|= nn
->reconfig_posted
;
120 nn
->reconfig_posted
= 0;
122 nfp_net_reconfig_start(nn
, update
);
124 nn
->reconfig_timer_active
= true;
125 mod_timer(&nn
->reconfig_timer
, jiffies
+ NFP_NET_POLL_TIMEOUT
* HZ
);
128 static bool nfp_net_reconfig_check_done(struct nfp_net
*nn
, bool last_check
)
132 reg
= nn_readl(nn
, NFP_NET_CFG_UPDATE
);
135 if (reg
& NFP_NET_CFG_UPDATE_ERR
) {
136 nn_err(nn
, "Reconfig error: 0x%08x\n", reg
);
138 } else if (last_check
) {
139 nn_err(nn
, "Reconfig timeout: 0x%08x\n", reg
);
146 static int nfp_net_reconfig_wait(struct nfp_net
*nn
, unsigned long deadline
)
148 bool timed_out
= false;
150 /* Poll update field, waiting for NFP to ack the config */
151 while (!nfp_net_reconfig_check_done(nn
, timed_out
)) {
153 timed_out
= time_is_before_eq_jiffies(deadline
);
156 if (nn_readl(nn
, NFP_NET_CFG_UPDATE
) & NFP_NET_CFG_UPDATE_ERR
)
159 return timed_out
? -EIO
: 0;
162 static void nfp_net_reconfig_timer(unsigned long data
)
164 struct nfp_net
*nn
= (void *)data
;
166 spin_lock_bh(&nn
->reconfig_lock
);
168 nn
->reconfig_timer_active
= false;
170 /* If sync caller is present it will take over from us */
171 if (nn
->reconfig_sync_present
)
174 /* Read reconfig status and report errors */
175 nfp_net_reconfig_check_done(nn
, true);
177 if (nn
->reconfig_posted
)
178 nfp_net_reconfig_start_async(nn
, 0);
180 spin_unlock_bh(&nn
->reconfig_lock
);
184 * nfp_net_reconfig_post() - Post async reconfig request
185 * @nn: NFP Net device to reconfigure
186 * @update: The value for the update field in the BAR config
188 * Record FW reconfiguration request. Reconfiguration will be kicked off
189 * whenever reconfiguration machinery is idle. Multiple requests can be
192 static void nfp_net_reconfig_post(struct nfp_net
*nn
, u32 update
)
194 spin_lock_bh(&nn
->reconfig_lock
);
196 /* Sync caller will kick off async reconf when it's done, just post */
197 if (nn
->reconfig_sync_present
) {
198 nn
->reconfig_posted
|= update
;
202 /* Opportunistically check if the previous command is done */
203 if (!nn
->reconfig_timer_active
||
204 nfp_net_reconfig_check_done(nn
, false))
205 nfp_net_reconfig_start_async(nn
, update
);
207 nn
->reconfig_posted
|= update
;
209 spin_unlock_bh(&nn
->reconfig_lock
);
213 * nfp_net_reconfig() - Reconfigure the firmware
214 * @nn: NFP Net device to reconfigure
215 * @update: The value for the update field in the BAR config
217 * Write the update word to the BAR and ping the reconfig queue. The
218 * poll until the firmware has acknowledged the update by zeroing the
221 * Return: Negative errno on error, 0 on success
223 int nfp_net_reconfig(struct nfp_net
*nn
, u32 update
)
225 bool cancelled_timer
= false;
226 u32 pre_posted_requests
;
229 spin_lock_bh(&nn
->reconfig_lock
);
231 nn
->reconfig_sync_present
= true;
233 if (nn
->reconfig_timer_active
) {
234 del_timer(&nn
->reconfig_timer
);
235 nn
->reconfig_timer_active
= false;
236 cancelled_timer
= true;
238 pre_posted_requests
= nn
->reconfig_posted
;
239 nn
->reconfig_posted
= 0;
241 spin_unlock_bh(&nn
->reconfig_lock
);
244 nfp_net_reconfig_wait(nn
, nn
->reconfig_timer
.expires
);
246 /* Run the posted reconfigs which were issued before we started */
247 if (pre_posted_requests
) {
248 nfp_net_reconfig_start(nn
, pre_posted_requests
);
249 nfp_net_reconfig_wait(nn
, jiffies
+ HZ
* NFP_NET_POLL_TIMEOUT
);
252 nfp_net_reconfig_start(nn
, update
);
253 ret
= nfp_net_reconfig_wait(nn
, jiffies
+ HZ
* NFP_NET_POLL_TIMEOUT
);
255 spin_lock_bh(&nn
->reconfig_lock
);
257 if (nn
->reconfig_posted
)
258 nfp_net_reconfig_start_async(nn
, 0);
260 nn
->reconfig_sync_present
= false;
262 spin_unlock_bh(&nn
->reconfig_lock
);
267 /* Interrupt configuration and handling
271 * nfp_net_irq_unmask() - Unmask automasked interrupt
272 * @nn: NFP Network structure
273 * @entry_nr: MSI-X table entry
275 * Clear the ICR for the IRQ entry.
277 static void nfp_net_irq_unmask(struct nfp_net
*nn
, unsigned int entry_nr
)
279 nn_writeb(nn
, NFP_NET_CFG_ICR(entry_nr
), NFP_NET_CFG_ICR_UNMASKED
);
284 * nfp_net_irqs_alloc() - allocates MSI-X irqs
285 * @pdev: PCI device structure
286 * @irq_entries: Array to be initialized and used to hold the irq entries
287 * @min_irqs: Minimal acceptable number of interrupts
288 * @wanted_irqs: Target number of interrupts to allocate
290 * Return: Number of irqs obtained or 0 on error.
293 nfp_net_irqs_alloc(struct pci_dev
*pdev
, struct msix_entry
*irq_entries
,
294 unsigned int min_irqs
, unsigned int wanted_irqs
)
299 for (i
= 0; i
< wanted_irqs
; i
++)
300 irq_entries
[i
].entry
= i
;
302 got_irqs
= pci_enable_msix_range(pdev
, irq_entries
,
303 min_irqs
, wanted_irqs
);
305 dev_err(&pdev
->dev
, "Failed to enable %d-%d MSI-X (err=%d)\n",
306 min_irqs
, wanted_irqs
, got_irqs
);
310 if (got_irqs
< wanted_irqs
)
311 dev_warn(&pdev
->dev
, "Unable to allocate %d IRQs got only %d\n",
312 wanted_irqs
, got_irqs
);
318 * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
319 * @nn: NFP Network structure
320 * @irq_entries: Table of allocated interrupts
321 * @n: Size of @irq_entries (number of entries to grab)
323 * After interrupts are allocated with nfp_net_irqs_alloc() this function
324 * should be called to assign them to a specific netdev (port).
327 nfp_net_irqs_assign(struct nfp_net
*nn
, struct msix_entry
*irq_entries
,
330 struct nfp_net_dp
*dp
= &nn
->dp
;
332 nn
->max_r_vecs
= n
- NFP_NET_NON_Q_VECTORS
;
333 dp
->num_r_vecs
= nn
->max_r_vecs
;
335 memcpy(nn
->irq_entries
, irq_entries
, sizeof(*irq_entries
) * n
);
337 if (dp
->num_rx_rings
> dp
->num_r_vecs
||
338 dp
->num_tx_rings
> dp
->num_r_vecs
)
339 dev_warn(nn
->dp
.dev
, "More rings (%d,%d) than vectors (%d).\n",
340 dp
->num_rx_rings
, dp
->num_tx_rings
,
343 dp
->num_rx_rings
= min(dp
->num_r_vecs
, dp
->num_rx_rings
);
344 dp
->num_tx_rings
= min(dp
->num_r_vecs
, dp
->num_tx_rings
);
345 dp
->num_stack_tx_rings
= dp
->num_tx_rings
;
349 * nfp_net_irqs_disable() - Disable interrupts
350 * @pdev: PCI device structure
352 * Undoes what @nfp_net_irqs_alloc() does.
354 void nfp_net_irqs_disable(struct pci_dev
*pdev
)
356 pci_disable_msix(pdev
);
360 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
362 * @data: Opaque data structure
364 * Return: Indicate if the interrupt has been handled.
366 static irqreturn_t
nfp_net_irq_rxtx(int irq
, void *data
)
368 struct nfp_net_r_vector
*r_vec
= data
;
370 napi_schedule_irqoff(&r_vec
->napi
);
372 /* The FW auto-masks any interrupt, either via the MASK bit in
373 * the MSI-X table or via the per entry ICR field. So there
374 * is no need to disable interrupts here.
380 * nfp_net_read_link_status() - Reread link status from control BAR
381 * @nn: NFP Network structure
383 static void nfp_net_read_link_status(struct nfp_net
*nn
)
389 spin_lock_irqsave(&nn
->link_status_lock
, flags
);
391 sts
= nn_readl(nn
, NFP_NET_CFG_STS
);
392 link_up
= !!(sts
& NFP_NET_CFG_STS_LINK
);
394 if (nn
->link_up
== link_up
)
397 nn
->link_up
= link_up
;
400 netif_carrier_on(nn
->dp
.netdev
);
401 netdev_info(nn
->dp
.netdev
, "NIC Link is Up\n");
403 netif_carrier_off(nn
->dp
.netdev
);
404 netdev_info(nn
->dp
.netdev
, "NIC Link is Down\n");
407 spin_unlock_irqrestore(&nn
->link_status_lock
, flags
);
411 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
413 * @data: Opaque data structure
415 * Return: Indicate if the interrupt has been handled.
417 static irqreturn_t
nfp_net_irq_lsc(int irq
, void *data
)
419 struct nfp_net
*nn
= data
;
420 struct msix_entry
*entry
;
422 entry
= &nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
];
424 nfp_net_read_link_status(nn
);
426 nfp_net_irq_unmask(nn
, entry
->entry
);
432 * nfp_net_irq_exn() - Interrupt service routine for exceptions
434 * @data: Opaque data structure
436 * Return: Indicate if the interrupt has been handled.
438 static irqreturn_t
nfp_net_irq_exn(int irq
, void *data
)
440 struct nfp_net
*nn
= data
;
442 nn_err(nn
, "%s: UNIMPLEMENTED.\n", __func__
);
443 /* XXX TO BE IMPLEMENTED */
448 * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
449 * @tx_ring: TX ring structure
450 * @r_vec: IRQ vector servicing this ring
454 nfp_net_tx_ring_init(struct nfp_net_tx_ring
*tx_ring
,
455 struct nfp_net_r_vector
*r_vec
, unsigned int idx
)
457 struct nfp_net
*nn
= r_vec
->nfp_net
;
460 tx_ring
->r_vec
= r_vec
;
462 tx_ring
->qcidx
= tx_ring
->idx
* nn
->stride_tx
;
463 tx_ring
->qcp_q
= nn
->tx_bar
+ NFP_QCP_QUEUE_OFF(tx_ring
->qcidx
);
467 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
468 * @rx_ring: RX ring structure
469 * @r_vec: IRQ vector servicing this ring
473 nfp_net_rx_ring_init(struct nfp_net_rx_ring
*rx_ring
,
474 struct nfp_net_r_vector
*r_vec
, unsigned int idx
)
476 struct nfp_net
*nn
= r_vec
->nfp_net
;
479 rx_ring
->r_vec
= r_vec
;
481 rx_ring
->fl_qcidx
= rx_ring
->idx
* nn
->stride_rx
;
482 rx_ring
->rx_qcidx
= rx_ring
->fl_qcidx
+ (nn
->stride_rx
- 1);
484 rx_ring
->qcp_fl
= nn
->rx_bar
+ NFP_QCP_QUEUE_OFF(rx_ring
->fl_qcidx
);
485 rx_ring
->qcp_rx
= nn
->rx_bar
+ NFP_QCP_QUEUE_OFF(rx_ring
->rx_qcidx
);
489 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
490 * @netdev: netdev structure
492 static void nfp_net_vecs_init(struct net_device
*netdev
)
494 struct nfp_net
*nn
= netdev_priv(netdev
);
495 struct nfp_net_r_vector
*r_vec
;
498 nn
->lsc_handler
= nfp_net_irq_lsc
;
499 nn
->exn_handler
= nfp_net_irq_exn
;
501 for (r
= 0; r
< nn
->max_r_vecs
; r
++) {
502 struct msix_entry
*entry
;
504 entry
= &nn
->irq_entries
[NFP_NET_NON_Q_VECTORS
+ r
];
506 r_vec
= &nn
->r_vecs
[r
];
508 r_vec
->handler
= nfp_net_irq_rxtx
;
509 r_vec
->irq_entry
= entry
->entry
;
510 r_vec
->irq_vector
= entry
->vector
;
512 cpumask_set_cpu(r
, &r_vec
->affinity_mask
);
517 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
518 * @nn: NFP Network structure
519 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
520 * @format: printf-style format to construct the interrupt name
521 * @name: Pointer to allocated space for interrupt name
522 * @name_sz: Size of space for interrupt name
523 * @vector_idx: Index of MSI-X vector used for this interrupt
524 * @handler: IRQ handler to register for this interrupt
527 nfp_net_aux_irq_request(struct nfp_net
*nn
, u32 ctrl_offset
,
528 const char *format
, char *name
, size_t name_sz
,
529 unsigned int vector_idx
, irq_handler_t handler
)
531 struct msix_entry
*entry
;
534 entry
= &nn
->irq_entries
[vector_idx
];
536 snprintf(name
, name_sz
, format
, netdev_name(nn
->dp
.netdev
));
537 err
= request_irq(entry
->vector
, handler
, 0, name
, nn
);
539 nn_err(nn
, "Failed to request IRQ %d (err=%d).\n",
543 nn_writeb(nn
, ctrl_offset
, entry
->entry
);
549 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
550 * @nn: NFP Network structure
551 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
552 * @vector_idx: Index of MSI-X vector used for this interrupt
554 static void nfp_net_aux_irq_free(struct nfp_net
*nn
, u32 ctrl_offset
,
555 unsigned int vector_idx
)
557 nn_writeb(nn
, ctrl_offset
, 0xff);
558 free_irq(nn
->irq_entries
[vector_idx
].vector
, nn
);
563 * One queue controller peripheral queue is used for transmit. The
564 * driver en-queues packets for transmit by advancing the write
565 * pointer. The device indicates that packets have transmitted by
566 * advancing the read pointer. The driver maintains a local copy of
567 * the read and write pointer in @struct nfp_net_tx_ring. The driver
568 * keeps @wr_p in sync with the queue controller write pointer and can
569 * determine how many packets have been transmitted by comparing its
570 * copy of the read pointer @rd_p with the read pointer maintained by
571 * the queue controller peripheral.
575 * nfp_net_tx_full() - Check if the TX ring is full
576 * @tx_ring: TX ring to check
577 * @dcnt: Number of descriptors that need to be enqueued (must be >= 1)
579 * This function checks, based on the *host copy* of read/write
580 * pointer if a given TX ring is full. The real TX queue may have
581 * some newly made available slots.
583 * Return: True if the ring is full.
585 static int nfp_net_tx_full(struct nfp_net_tx_ring
*tx_ring
, int dcnt
)
587 return (tx_ring
->wr_p
- tx_ring
->rd_p
) >= (tx_ring
->cnt
- dcnt
);
590 /* Wrappers for deciding when to stop and restart TX queues */
591 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring
*tx_ring
)
593 return !nfp_net_tx_full(tx_ring
, MAX_SKB_FRAGS
* 4);
596 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring
*tx_ring
)
598 return nfp_net_tx_full(tx_ring
, MAX_SKB_FRAGS
+ 1);
602 * nfp_net_tx_ring_stop() - stop tx ring
603 * @nd_q: netdev queue
604 * @tx_ring: driver tx queue structure
606 * Safely stop TX ring. Remember that while we are running .start_xmit()
607 * someone else may be cleaning the TX ring completions so we need to be
608 * extra careful here.
610 static void nfp_net_tx_ring_stop(struct netdev_queue
*nd_q
,
611 struct nfp_net_tx_ring
*tx_ring
)
613 netif_tx_stop_queue(nd_q
);
615 /* We can race with the TX completion out of NAPI so recheck */
617 if (unlikely(nfp_net_tx_ring_should_wake(tx_ring
)))
618 netif_tx_start_queue(nd_q
);
622 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
623 * @r_vec: per-ring structure
624 * @txbuf: Pointer to driver soft TX descriptor
625 * @txd: Pointer to HW TX descriptor
626 * @skb: Pointer to SKB
628 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
629 * Return error on packet header greater than maximum supported LSO header size.
631 static void nfp_net_tx_tso(struct nfp_net_r_vector
*r_vec
,
632 struct nfp_net_tx_buf
*txbuf
,
633 struct nfp_net_tx_desc
*txd
, struct sk_buff
*skb
)
638 if (!skb_is_gso(skb
))
641 if (!skb
->encapsulation
)
642 hdrlen
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
644 hdrlen
= skb_inner_transport_header(skb
) - skb
->data
+
645 inner_tcp_hdrlen(skb
);
647 txbuf
->pkt_cnt
= skb_shinfo(skb
)->gso_segs
;
648 txbuf
->real_len
+= hdrlen
* (txbuf
->pkt_cnt
- 1);
650 mss
= skb_shinfo(skb
)->gso_size
& PCIE_DESC_TX_MSS_MASK
;
651 txd
->l4_offset
= hdrlen
;
652 txd
->mss
= cpu_to_le16(mss
);
653 txd
->flags
|= PCIE_DESC_TX_LSO
;
655 u64_stats_update_begin(&r_vec
->tx_sync
);
657 u64_stats_update_end(&r_vec
->tx_sync
);
661 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
662 * @dp: NFP Net data path struct
663 * @r_vec: per-ring structure
664 * @txbuf: Pointer to driver soft TX descriptor
665 * @txd: Pointer to TX descriptor
666 * @skb: Pointer to SKB
668 * This function sets the TX checksum flags in the TX descriptor based
669 * on the configuration and the protocol of the packet to be transmitted.
671 static void nfp_net_tx_csum(struct nfp_net_dp
*dp
,
672 struct nfp_net_r_vector
*r_vec
,
673 struct nfp_net_tx_buf
*txbuf
,
674 struct nfp_net_tx_desc
*txd
, struct sk_buff
*skb
)
676 struct ipv6hdr
*ipv6h
;
680 if (!(dp
->ctrl
& NFP_NET_CFG_CTRL_TXCSUM
))
683 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
686 txd
->flags
|= PCIE_DESC_TX_CSUM
;
687 if (skb
->encapsulation
)
688 txd
->flags
|= PCIE_DESC_TX_ENCAP
;
690 iph
= skb
->encapsulation
? inner_ip_hdr(skb
) : ip_hdr(skb
);
691 ipv6h
= skb
->encapsulation
? inner_ipv6_hdr(skb
) : ipv6_hdr(skb
);
693 if (iph
->version
== 4) {
694 txd
->flags
|= PCIE_DESC_TX_IP4_CSUM
;
695 l4_hdr
= iph
->protocol
;
696 } else if (ipv6h
->version
== 6) {
697 l4_hdr
= ipv6h
->nexthdr
;
699 nn_dp_warn(dp
, "partial checksum but ipv=%x!\n", iph
->version
);
705 txd
->flags
|= PCIE_DESC_TX_TCP_CSUM
;
708 txd
->flags
|= PCIE_DESC_TX_UDP_CSUM
;
711 nn_dp_warn(dp
, "partial checksum but l4 proto=%x!\n", l4_hdr
);
715 u64_stats_update_begin(&r_vec
->tx_sync
);
716 if (skb
->encapsulation
)
717 r_vec
->hw_csum_tx_inner
+= txbuf
->pkt_cnt
;
719 r_vec
->hw_csum_tx
+= txbuf
->pkt_cnt
;
720 u64_stats_update_end(&r_vec
->tx_sync
);
723 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring
*tx_ring
)
726 nfp_qcp_wr_ptr_add(tx_ring
->qcp_q
, tx_ring
->wr_ptr_add
);
727 tx_ring
->wr_ptr_add
= 0;
731 * nfp_net_tx() - Main transmit entry point
732 * @skb: SKB to transmit
733 * @netdev: netdev structure
735 * Return: NETDEV_TX_OK on success.
737 static int nfp_net_tx(struct sk_buff
*skb
, struct net_device
*netdev
)
739 struct nfp_net
*nn
= netdev_priv(netdev
);
740 const struct skb_frag_struct
*frag
;
741 struct nfp_net_tx_desc
*txd
, txdg
;
742 struct nfp_net_tx_ring
*tx_ring
;
743 struct nfp_net_r_vector
*r_vec
;
744 struct nfp_net_tx_buf
*txbuf
;
745 struct netdev_queue
*nd_q
;
746 struct nfp_net_dp
*dp
;
754 qidx
= skb_get_queue_mapping(skb
);
755 tx_ring
= &dp
->tx_rings
[qidx
];
756 r_vec
= tx_ring
->r_vec
;
757 nd_q
= netdev_get_tx_queue(dp
->netdev
, qidx
);
759 nr_frags
= skb_shinfo(skb
)->nr_frags
;
761 if (unlikely(nfp_net_tx_full(tx_ring
, nr_frags
+ 1))) {
762 nn_dp_warn(dp
, "TX ring %d busy. wrp=%u rdp=%u\n",
763 qidx
, tx_ring
->wr_p
, tx_ring
->rd_p
);
764 netif_tx_stop_queue(nd_q
);
765 u64_stats_update_begin(&r_vec
->tx_sync
);
767 u64_stats_update_end(&r_vec
->tx_sync
);
768 return NETDEV_TX_BUSY
;
771 /* Start with the head skbuf */
772 dma_addr
= dma_map_single(dp
->dev
, skb
->data
, skb_headlen(skb
),
774 if (dma_mapping_error(dp
->dev
, dma_addr
))
777 wr_idx
= tx_ring
->wr_p
& (tx_ring
->cnt
- 1);
779 /* Stash the soft descriptor of the head then initialize it */
780 txbuf
= &tx_ring
->txbufs
[wr_idx
];
782 txbuf
->dma_addr
= dma_addr
;
785 txbuf
->real_len
= skb
->len
;
787 /* Build TX descriptor */
788 txd
= &tx_ring
->txds
[wr_idx
];
789 txd
->offset_eop
= (nr_frags
== 0) ? PCIE_DESC_TX_EOP
: 0;
790 txd
->dma_len
= cpu_to_le16(skb_headlen(skb
));
791 nfp_desc_set_dma_addr(txd
, dma_addr
);
792 txd
->data_len
= cpu_to_le16(skb
->len
);
798 nfp_net_tx_tso(r_vec
, txbuf
, txd
, skb
);
800 nfp_net_tx_csum(dp
, r_vec
, txbuf
, txd
, skb
);
802 if (skb_vlan_tag_present(skb
) && dp
->ctrl
& NFP_NET_CFG_CTRL_TXVLAN
) {
803 txd
->flags
|= PCIE_DESC_TX_VLAN
;
804 txd
->vlan
= cpu_to_le16(skb_vlan_tag_get(skb
));
809 /* all descs must match except for in addr, length and eop */
812 for (f
= 0; f
< nr_frags
; f
++) {
813 frag
= &skb_shinfo(skb
)->frags
[f
];
814 fsize
= skb_frag_size(frag
);
816 dma_addr
= skb_frag_dma_map(dp
->dev
, frag
, 0,
817 fsize
, DMA_TO_DEVICE
);
818 if (dma_mapping_error(dp
->dev
, dma_addr
))
821 wr_idx
= (wr_idx
+ 1) & (tx_ring
->cnt
- 1);
822 tx_ring
->txbufs
[wr_idx
].skb
= skb
;
823 tx_ring
->txbufs
[wr_idx
].dma_addr
= dma_addr
;
824 tx_ring
->txbufs
[wr_idx
].fidx
= f
;
826 txd
= &tx_ring
->txds
[wr_idx
];
828 txd
->dma_len
= cpu_to_le16(fsize
);
829 nfp_desc_set_dma_addr(txd
, dma_addr
);
831 (f
== nr_frags
- 1) ? PCIE_DESC_TX_EOP
: 0;
834 u64_stats_update_begin(&r_vec
->tx_sync
);
836 u64_stats_update_end(&r_vec
->tx_sync
);
839 netdev_tx_sent_queue(nd_q
, txbuf
->real_len
);
841 tx_ring
->wr_p
+= nr_frags
+ 1;
842 if (nfp_net_tx_ring_should_stop(tx_ring
))
843 nfp_net_tx_ring_stop(nd_q
, tx_ring
);
845 tx_ring
->wr_ptr_add
+= nr_frags
+ 1;
846 if (!skb
->xmit_more
|| netif_xmit_stopped(nd_q
))
847 nfp_net_tx_xmit_more_flush(tx_ring
);
849 skb_tx_timestamp(skb
);
856 frag
= &skb_shinfo(skb
)->frags
[f
];
857 dma_unmap_page(dp
->dev
, tx_ring
->txbufs
[wr_idx
].dma_addr
,
858 skb_frag_size(frag
), DMA_TO_DEVICE
);
859 tx_ring
->txbufs
[wr_idx
].skb
= NULL
;
860 tx_ring
->txbufs
[wr_idx
].dma_addr
= 0;
861 tx_ring
->txbufs
[wr_idx
].fidx
= -2;
864 wr_idx
+= tx_ring
->cnt
;
866 dma_unmap_single(dp
->dev
, tx_ring
->txbufs
[wr_idx
].dma_addr
,
867 skb_headlen(skb
), DMA_TO_DEVICE
);
868 tx_ring
->txbufs
[wr_idx
].skb
= NULL
;
869 tx_ring
->txbufs
[wr_idx
].dma_addr
= 0;
870 tx_ring
->txbufs
[wr_idx
].fidx
= -2;
872 nn_dp_warn(dp
, "Failed to map DMA TX buffer\n");
873 u64_stats_update_begin(&r_vec
->tx_sync
);
875 u64_stats_update_end(&r_vec
->tx_sync
);
876 dev_kfree_skb_any(skb
);
881 * nfp_net_tx_complete() - Handled completed TX packets
882 * @tx_ring: TX ring structure
884 * Return: Number of completed TX descriptors
886 static void nfp_net_tx_complete(struct nfp_net_tx_ring
*tx_ring
)
888 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
889 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
890 const struct skb_frag_struct
*frag
;
891 struct netdev_queue
*nd_q
;
892 u32 done_pkts
= 0, done_bytes
= 0;
899 /* Work out how many descriptors have been transmitted */
900 qcp_rd_p
= nfp_qcp_rd_ptr_read(tx_ring
->qcp_q
);
902 if (qcp_rd_p
== tx_ring
->qcp_rd_p
)
905 if (qcp_rd_p
> tx_ring
->qcp_rd_p
)
906 todo
= qcp_rd_p
- tx_ring
->qcp_rd_p
;
908 todo
= qcp_rd_p
+ tx_ring
->cnt
- tx_ring
->qcp_rd_p
;
911 idx
= tx_ring
->rd_p
& (tx_ring
->cnt
- 1);
914 skb
= tx_ring
->txbufs
[idx
].skb
;
918 nr_frags
= skb_shinfo(skb
)->nr_frags
;
919 fidx
= tx_ring
->txbufs
[idx
].fidx
;
923 dma_unmap_single(dp
->dev
, tx_ring
->txbufs
[idx
].dma_addr
,
924 skb_headlen(skb
), DMA_TO_DEVICE
);
926 done_pkts
+= tx_ring
->txbufs
[idx
].pkt_cnt
;
927 done_bytes
+= tx_ring
->txbufs
[idx
].real_len
;
930 frag
= &skb_shinfo(skb
)->frags
[fidx
];
931 dma_unmap_page(dp
->dev
, tx_ring
->txbufs
[idx
].dma_addr
,
932 skb_frag_size(frag
), DMA_TO_DEVICE
);
935 /* check for last gather fragment */
936 if (fidx
== nr_frags
- 1)
937 dev_kfree_skb_any(skb
);
939 tx_ring
->txbufs
[idx
].dma_addr
= 0;
940 tx_ring
->txbufs
[idx
].skb
= NULL
;
941 tx_ring
->txbufs
[idx
].fidx
= -2;
944 tx_ring
->qcp_rd_p
= qcp_rd_p
;
946 u64_stats_update_begin(&r_vec
->tx_sync
);
947 r_vec
->tx_bytes
+= done_bytes
;
948 r_vec
->tx_pkts
+= done_pkts
;
949 u64_stats_update_end(&r_vec
->tx_sync
);
951 nd_q
= netdev_get_tx_queue(dp
->netdev
, tx_ring
->idx
);
952 netdev_tx_completed_queue(nd_q
, done_pkts
, done_bytes
);
953 if (nfp_net_tx_ring_should_wake(tx_ring
)) {
954 /* Make sure TX thread will see updated tx_ring->rd_p */
957 if (unlikely(netif_tx_queue_stopped(nd_q
)))
958 netif_tx_wake_queue(nd_q
);
961 WARN_ONCE(tx_ring
->wr_p
- tx_ring
->rd_p
> tx_ring
->cnt
,
962 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
963 tx_ring
->rd_p
, tx_ring
->wr_p
, tx_ring
->cnt
);
966 static void nfp_net_xdp_complete(struct nfp_net_tx_ring
*tx_ring
)
968 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
969 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
970 u32 done_pkts
= 0, done_bytes
= 0;
974 /* Work out how many descriptors have been transmitted */
975 qcp_rd_p
= nfp_qcp_rd_ptr_read(tx_ring
->qcp_q
);
977 if (qcp_rd_p
== tx_ring
->qcp_rd_p
)
980 if (qcp_rd_p
> tx_ring
->qcp_rd_p
)
981 todo
= qcp_rd_p
- tx_ring
->qcp_rd_p
;
983 todo
= qcp_rd_p
+ tx_ring
->cnt
- tx_ring
->qcp_rd_p
;
986 idx
= tx_ring
->rd_p
& (tx_ring
->cnt
- 1);
989 if (!tx_ring
->txbufs
[idx
].frag
)
992 nfp_net_dma_unmap_rx(dp
, tx_ring
->txbufs
[idx
].dma_addr
);
993 __free_page(virt_to_page(tx_ring
->txbufs
[idx
].frag
));
996 done_bytes
+= tx_ring
->txbufs
[idx
].real_len
;
998 tx_ring
->txbufs
[idx
].dma_addr
= 0;
999 tx_ring
->txbufs
[idx
].frag
= NULL
;
1000 tx_ring
->txbufs
[idx
].fidx
= -2;
1003 tx_ring
->qcp_rd_p
= qcp_rd_p
;
1005 u64_stats_update_begin(&r_vec
->tx_sync
);
1006 r_vec
->tx_bytes
+= done_bytes
;
1007 r_vec
->tx_pkts
+= done_pkts
;
1008 u64_stats_update_end(&r_vec
->tx_sync
);
1010 WARN_ONCE(tx_ring
->wr_p
- tx_ring
->rd_p
> tx_ring
->cnt
,
1011 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1012 tx_ring
->rd_p
, tx_ring
->wr_p
, tx_ring
->cnt
);
1016 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1017 * @dp: NFP Net data path struct
1018 * @tx_ring: TX ring structure
1020 * Assumes that the device is stopped
1023 nfp_net_tx_ring_reset(struct nfp_net_dp
*dp
, struct nfp_net_tx_ring
*tx_ring
)
1025 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
1026 const struct skb_frag_struct
*frag
;
1027 struct netdev_queue
*nd_q
;
1029 while (tx_ring
->rd_p
!= tx_ring
->wr_p
) {
1030 struct nfp_net_tx_buf
*tx_buf
;
1033 idx
= tx_ring
->rd_p
& (tx_ring
->cnt
- 1);
1034 tx_buf
= &tx_ring
->txbufs
[idx
];
1036 if (tx_ring
== r_vec
->xdp_ring
) {
1037 nfp_net_dma_unmap_rx(dp
, tx_buf
->dma_addr
);
1038 __free_page(virt_to_page(tx_ring
->txbufs
[idx
].frag
));
1040 struct sk_buff
*skb
= tx_ring
->txbufs
[idx
].skb
;
1041 int nr_frags
= skb_shinfo(skb
)->nr_frags
;
1043 if (tx_buf
->fidx
== -1) {
1045 dma_unmap_single(dp
->dev
, tx_buf
->dma_addr
,
1049 /* unmap fragment */
1050 frag
= &skb_shinfo(skb
)->frags
[tx_buf
->fidx
];
1051 dma_unmap_page(dp
->dev
, tx_buf
->dma_addr
,
1052 skb_frag_size(frag
),
1056 /* check for last gather fragment */
1057 if (tx_buf
->fidx
== nr_frags
- 1)
1058 dev_kfree_skb_any(skb
);
1061 tx_buf
->dma_addr
= 0;
1065 tx_ring
->qcp_rd_p
++;
1069 memset(tx_ring
->txds
, 0, sizeof(*tx_ring
->txds
) * tx_ring
->cnt
);
1072 tx_ring
->qcp_rd_p
= 0;
1073 tx_ring
->wr_ptr_add
= 0;
1075 if (tx_ring
== r_vec
->xdp_ring
)
1078 nd_q
= netdev_get_tx_queue(dp
->netdev
, tx_ring
->idx
);
1079 netdev_tx_reset_queue(nd_q
);
1082 static void nfp_net_tx_timeout(struct net_device
*netdev
)
1084 struct nfp_net
*nn
= netdev_priv(netdev
);
1087 for (i
= 0; i
< nn
->dp
.netdev
->real_num_tx_queues
; i
++) {
1088 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev
, i
)))
1090 nn_warn(nn
, "TX timeout on ring: %d\n", i
);
1092 nn_warn(nn
, "TX watchdog timeout\n");
1095 /* Receive processing
1098 nfp_net_calc_fl_bufsz(struct nfp_net_dp
*dp
)
1100 unsigned int fl_bufsz
;
1102 fl_bufsz
= NFP_NET_RX_BUF_HEADROOM
;
1103 fl_bufsz
+= dp
->rx_dma_off
;
1104 if (dp
->rx_offset
== NFP_NET_CFG_RX_OFFSET_DYNAMIC
)
1105 fl_bufsz
+= NFP_NET_MAX_PREPEND
;
1107 fl_bufsz
+= dp
->rx_offset
;
1108 fl_bufsz
+= ETH_HLEN
+ VLAN_HLEN
* 2 + dp
->mtu
;
1110 fl_bufsz
= SKB_DATA_ALIGN(fl_bufsz
);
1111 fl_bufsz
+= SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
1117 nfp_net_free_frag(void *frag
, bool xdp
)
1120 skb_free_frag(frag
);
1122 __free_page(virt_to_page(frag
));
1126 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1127 * @dp: NFP Net data path struct
1128 * @rx_ring: RX ring structure of the skb
1129 * @dma_addr: Pointer to storage for DMA address (output param)
1131 * This function will allcate a new page frag, map it for DMA.
1133 * Return: allocated page frag or NULL on failure.
1136 nfp_net_rx_alloc_one(struct nfp_net_dp
*dp
, struct nfp_net_rx_ring
*rx_ring
,
1137 dma_addr_t
*dma_addr
)
1142 frag
= netdev_alloc_frag(dp
->fl_bufsz
);
1144 frag
= page_address(alloc_page(GFP_KERNEL
| __GFP_COLD
));
1146 nn_dp_warn(dp
, "Failed to alloc receive page frag\n");
1150 *dma_addr
= nfp_net_dma_map_rx(dp
, frag
);
1151 if (dma_mapping_error(dp
->dev
, *dma_addr
)) {
1152 nfp_net_free_frag(frag
, dp
->xdp_prog
);
1153 nn_dp_warn(dp
, "Failed to map DMA RX buffer\n");
1160 static void *nfp_net_napi_alloc_one(struct nfp_net_dp
*dp
, dma_addr_t
*dma_addr
)
1165 frag
= napi_alloc_frag(dp
->fl_bufsz
);
1167 frag
= page_address(alloc_page(GFP_ATOMIC
| __GFP_COLD
));
1169 nn_dp_warn(dp
, "Failed to alloc receive page frag\n");
1173 *dma_addr
= nfp_net_dma_map_rx(dp
, frag
);
1174 if (dma_mapping_error(dp
->dev
, *dma_addr
)) {
1175 nfp_net_free_frag(frag
, dp
->xdp_prog
);
1176 nn_dp_warn(dp
, "Failed to map DMA RX buffer\n");
1184 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1185 * @dp: NFP Net data path struct
1186 * @rx_ring: RX ring structure
1187 * @frag: page fragment buffer
1188 * @dma_addr: DMA address of skb mapping
1190 static void nfp_net_rx_give_one(const struct nfp_net_dp
*dp
,
1191 struct nfp_net_rx_ring
*rx_ring
,
1192 void *frag
, dma_addr_t dma_addr
)
1194 unsigned int wr_idx
;
1196 wr_idx
= rx_ring
->wr_p
& (rx_ring
->cnt
- 1);
1198 /* Stash SKB and DMA address away */
1199 rx_ring
->rxbufs
[wr_idx
].frag
= frag
;
1200 rx_ring
->rxbufs
[wr_idx
].dma_addr
= dma_addr
;
1202 /* Fill freelist descriptor */
1203 rx_ring
->rxds
[wr_idx
].fld
.reserved
= 0;
1204 rx_ring
->rxds
[wr_idx
].fld
.meta_len_dd
= 0;
1205 nfp_desc_set_dma_addr(&rx_ring
->rxds
[wr_idx
].fld
,
1206 dma_addr
+ dp
->rx_dma_off
);
1209 rx_ring
->wr_ptr_add
++;
1210 if (rx_ring
->wr_ptr_add
>= NFP_NET_FL_BATCH
) {
1211 /* Update write pointer of the freelist queue. Make
1212 * sure all writes are flushed before telling the hardware.
1215 nfp_qcp_wr_ptr_add(rx_ring
->qcp_fl
, rx_ring
->wr_ptr_add
);
1216 rx_ring
->wr_ptr_add
= 0;
1221 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1222 * @rx_ring: RX ring structure
1224 * Warning: Do *not* call if ring buffers were never put on the FW freelist
1225 * (i.e. device was not enabled)!
1227 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring
*rx_ring
)
1229 unsigned int wr_idx
, last_idx
;
1231 /* Move the empty entry to the end of the list */
1232 wr_idx
= rx_ring
->wr_p
& (rx_ring
->cnt
- 1);
1233 last_idx
= rx_ring
->cnt
- 1;
1234 rx_ring
->rxbufs
[wr_idx
].dma_addr
= rx_ring
->rxbufs
[last_idx
].dma_addr
;
1235 rx_ring
->rxbufs
[wr_idx
].frag
= rx_ring
->rxbufs
[last_idx
].frag
;
1236 rx_ring
->rxbufs
[last_idx
].dma_addr
= 0;
1237 rx_ring
->rxbufs
[last_idx
].frag
= NULL
;
1239 memset(rx_ring
->rxds
, 0, sizeof(*rx_ring
->rxds
) * rx_ring
->cnt
);
1242 rx_ring
->wr_ptr_add
= 0;
1246 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1247 * @dp: NFP Net data path struct
1248 * @rx_ring: RX ring to remove buffers from
1250 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1251 * entries. After device is disabled nfp_net_rx_ring_reset() must be called
1252 * to restore required ring geometry.
1255 nfp_net_rx_ring_bufs_free(struct nfp_net_dp
*dp
,
1256 struct nfp_net_rx_ring
*rx_ring
)
1260 for (i
= 0; i
< rx_ring
->cnt
- 1; i
++) {
1261 /* NULL skb can only happen when initial filling of the ring
1262 * fails to allocate enough buffers and calls here to free
1263 * already allocated ones.
1265 if (!rx_ring
->rxbufs
[i
].frag
)
1268 nfp_net_dma_unmap_rx(dp
, rx_ring
->rxbufs
[i
].dma_addr
);
1269 nfp_net_free_frag(rx_ring
->rxbufs
[i
].frag
, dp
->xdp_prog
);
1270 rx_ring
->rxbufs
[i
].dma_addr
= 0;
1271 rx_ring
->rxbufs
[i
].frag
= NULL
;
1276 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1277 * @dp: NFP Net data path struct
1278 * @rx_ring: RX ring to remove buffers from
1281 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp
*dp
,
1282 struct nfp_net_rx_ring
*rx_ring
)
1284 struct nfp_net_rx_buf
*rxbufs
;
1287 rxbufs
= rx_ring
->rxbufs
;
1289 for (i
= 0; i
< rx_ring
->cnt
- 1; i
++) {
1291 nfp_net_rx_alloc_one(dp
, rx_ring
, &rxbufs
[i
].dma_addr
);
1292 if (!rxbufs
[i
].frag
) {
1293 nfp_net_rx_ring_bufs_free(dp
, rx_ring
);
1302 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1303 * @dp: NFP Net data path struct
1304 * @rx_ring: RX ring to fill
1307 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp
*dp
,
1308 struct nfp_net_rx_ring
*rx_ring
)
1312 for (i
= 0; i
< rx_ring
->cnt
- 1; i
++)
1313 nfp_net_rx_give_one(dp
, rx_ring
, rx_ring
->rxbufs
[i
].frag
,
1314 rx_ring
->rxbufs
[i
].dma_addr
);
1318 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1319 * @flags: RX descriptor flags field in CPU byte order
1321 static int nfp_net_rx_csum_has_errors(u16 flags
)
1323 u16 csum_all_checked
, csum_all_ok
;
1325 csum_all_checked
= flags
& __PCIE_DESC_RX_CSUM_ALL
;
1326 csum_all_ok
= flags
& __PCIE_DESC_RX_CSUM_ALL_OK
;
1328 return csum_all_checked
!= (csum_all_ok
<< PCIE_DESC_RX_CSUM_OK_SHIFT
);
1332 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1333 * @dp: NFP Net data path struct
1334 * @r_vec: per-ring structure
1335 * @rxd: Pointer to RX descriptor
1336 * @skb: Pointer to SKB
1338 static void nfp_net_rx_csum(struct nfp_net_dp
*dp
,
1339 struct nfp_net_r_vector
*r_vec
,
1340 struct nfp_net_rx_desc
*rxd
, struct sk_buff
*skb
)
1342 skb_checksum_none_assert(skb
);
1344 if (!(dp
->netdev
->features
& NETIF_F_RXCSUM
))
1347 if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd
->rxd
.flags
))) {
1348 u64_stats_update_begin(&r_vec
->rx_sync
);
1349 r_vec
->hw_csum_rx_error
++;
1350 u64_stats_update_end(&r_vec
->rx_sync
);
1354 /* Assume that the firmware will never report inner CSUM_OK unless outer
1355 * L4 headers were successfully parsed. FW will always report zero UDP
1356 * checksum as CSUM_OK.
1358 if (rxd
->rxd
.flags
& PCIE_DESC_RX_TCP_CSUM_OK
||
1359 rxd
->rxd
.flags
& PCIE_DESC_RX_UDP_CSUM_OK
) {
1360 __skb_incr_checksum_unnecessary(skb
);
1361 u64_stats_update_begin(&r_vec
->rx_sync
);
1362 r_vec
->hw_csum_rx_ok
++;
1363 u64_stats_update_end(&r_vec
->rx_sync
);
1366 if (rxd
->rxd
.flags
& PCIE_DESC_RX_I_TCP_CSUM_OK
||
1367 rxd
->rxd
.flags
& PCIE_DESC_RX_I_UDP_CSUM_OK
) {
1368 __skb_incr_checksum_unnecessary(skb
);
1369 u64_stats_update_begin(&r_vec
->rx_sync
);
1370 r_vec
->hw_csum_rx_inner_ok
++;
1371 u64_stats_update_end(&r_vec
->rx_sync
);
1375 static void nfp_net_set_hash(struct net_device
*netdev
, struct sk_buff
*skb
,
1376 unsigned int type
, __be32
*hash
)
1378 if (!(netdev
->features
& NETIF_F_RXHASH
))
1382 case NFP_NET_RSS_IPV4
:
1383 case NFP_NET_RSS_IPV6
:
1384 case NFP_NET_RSS_IPV6_EX
:
1385 skb_set_hash(skb
, get_unaligned_be32(hash
), PKT_HASH_TYPE_L3
);
1388 skb_set_hash(skb
, get_unaligned_be32(hash
), PKT_HASH_TYPE_L4
);
1394 nfp_net_set_hash_desc(struct net_device
*netdev
, struct sk_buff
*skb
,
1395 void *data
, struct nfp_net_rx_desc
*rxd
)
1397 struct nfp_net_rx_hash
*rx_hash
= data
;
1399 if (!(rxd
->rxd
.flags
& PCIE_DESC_RX_RSS
))
1402 nfp_net_set_hash(netdev
, skb
, get_unaligned_be32(&rx_hash
->hash_type
),
1407 nfp_net_parse_meta(struct net_device
*netdev
, struct sk_buff
*skb
,
1408 void *data
, int meta_len
)
1412 meta_info
= get_unaligned_be32(data
);
1416 switch (meta_info
& NFP_NET_META_FIELD_MASK
) {
1417 case NFP_NET_META_HASH
:
1418 meta_info
>>= NFP_NET_META_FIELD_SIZE
;
1419 nfp_net_set_hash(netdev
, skb
,
1420 meta_info
& NFP_NET_META_FIELD_MASK
,
1424 case NFP_NET_META_MARK
:
1425 skb
->mark
= get_unaligned_be32(data
);
1432 meta_info
>>= NFP_NET_META_FIELD_SIZE
;
1439 nfp_net_rx_drop(const struct nfp_net_dp
*dp
, struct nfp_net_r_vector
*r_vec
,
1440 struct nfp_net_rx_ring
*rx_ring
, struct nfp_net_rx_buf
*rxbuf
,
1441 struct sk_buff
*skb
)
1443 u64_stats_update_begin(&r_vec
->rx_sync
);
1445 u64_stats_update_end(&r_vec
->rx_sync
);
1447 /* skb is build based on the frag, free_skb() would free the frag
1448 * so to be able to reuse it we need an extra ref.
1450 if (skb
&& rxbuf
&& skb
->head
== rxbuf
->frag
)
1451 page_ref_inc(virt_to_head_page(rxbuf
->frag
));
1453 nfp_net_rx_give_one(dp
, rx_ring
, rxbuf
->frag
, rxbuf
->dma_addr
);
1455 dev_kfree_skb_any(skb
);
1459 nfp_net_tx_xdp_buf(struct nfp_net_dp
*dp
, struct nfp_net_rx_ring
*rx_ring
,
1460 struct nfp_net_tx_ring
*tx_ring
,
1461 struct nfp_net_rx_buf
*rxbuf
, unsigned int dma_off
,
1462 unsigned int pkt_len
)
1464 struct nfp_net_tx_buf
*txbuf
;
1465 struct nfp_net_tx_desc
*txd
;
1466 dma_addr_t new_dma_addr
;
1470 if (unlikely(nfp_net_tx_full(tx_ring
, 1))) {
1471 nfp_net_rx_drop(dp
, rx_ring
->r_vec
, rx_ring
, rxbuf
, NULL
);
1475 new_frag
= nfp_net_napi_alloc_one(dp
, &new_dma_addr
);
1476 if (unlikely(!new_frag
)) {
1477 nfp_net_rx_drop(dp
, rx_ring
->r_vec
, rx_ring
, rxbuf
, NULL
);
1480 nfp_net_rx_give_one(dp
, rx_ring
, new_frag
, new_dma_addr
);
1482 wr_idx
= tx_ring
->wr_p
& (tx_ring
->cnt
- 1);
1484 /* Stash the soft descriptor of the head then initialize it */
1485 txbuf
= &tx_ring
->txbufs
[wr_idx
];
1486 txbuf
->frag
= rxbuf
->frag
;
1487 txbuf
->dma_addr
= rxbuf
->dma_addr
;
1490 txbuf
->real_len
= pkt_len
;
1492 dma_sync_single_for_device(dp
->dev
, rxbuf
->dma_addr
+ dma_off
,
1493 pkt_len
, DMA_BIDIRECTIONAL
);
1495 /* Build TX descriptor */
1496 txd
= &tx_ring
->txds
[wr_idx
];
1497 txd
->offset_eop
= PCIE_DESC_TX_EOP
;
1498 txd
->dma_len
= cpu_to_le16(pkt_len
);
1499 nfp_desc_set_dma_addr(txd
, rxbuf
->dma_addr
+ dma_off
);
1500 txd
->data_len
= cpu_to_le16(pkt_len
);
1507 tx_ring
->wr_ptr_add
++;
1511 static int nfp_net_run_xdp(struct bpf_prog
*prog
, void *data
, void *hard_start
,
1512 unsigned int *off
, unsigned int *len
)
1514 struct xdp_buff xdp
;
1518 xdp
.data_hard_start
= hard_start
;
1519 xdp
.data
= data
+ *off
;
1520 xdp
.data_end
= data
+ *off
+ *len
;
1522 orig_data
= xdp
.data
;
1523 ret
= bpf_prog_run_xdp(prog
, &xdp
);
1525 *len
-= xdp
.data
- orig_data
;
1526 *off
+= xdp
.data
- orig_data
;
1532 * nfp_net_rx() - receive up to @budget packets on @rx_ring
1533 * @rx_ring: RX ring to receive from
1534 * @budget: NAPI budget
1536 * Note, this function is separated out from the napi poll function to
1537 * more cleanly separate packet receive code from other bookkeeping
1538 * functions performed in the napi poll function.
1540 * Return: Number of packets received.
1542 static int nfp_net_rx(struct nfp_net_rx_ring
*rx_ring
, int budget
)
1544 struct nfp_net_r_vector
*r_vec
= rx_ring
->r_vec
;
1545 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
1546 struct nfp_net_tx_ring
*tx_ring
;
1547 struct bpf_prog
*xdp_prog
;
1548 unsigned int true_bufsz
;
1549 struct sk_buff
*skb
;
1550 int pkts_polled
= 0;
1554 xdp_prog
= READ_ONCE(dp
->xdp_prog
);
1555 true_bufsz
= xdp_prog
? PAGE_SIZE
: dp
->fl_bufsz
;
1556 tx_ring
= r_vec
->xdp_ring
;
1558 while (pkts_polled
< budget
) {
1559 unsigned int meta_len
, data_len
, data_off
, pkt_len
;
1560 u8 meta_prepend
[NFP_NET_MAX_PREPEND
];
1561 struct nfp_net_rx_buf
*rxbuf
;
1562 struct nfp_net_rx_desc
*rxd
;
1563 dma_addr_t new_dma_addr
;
1567 idx
= rx_ring
->rd_p
& (rx_ring
->cnt
- 1);
1569 rxd
= &rx_ring
->rxds
[idx
];
1570 if (!(rxd
->rxd
.meta_len_dd
& PCIE_DESC_RX_DD
))
1573 /* Memory barrier to ensure that we won't do other reads
1574 * before the DD bit.
1581 rxbuf
= &rx_ring
->rxbufs
[idx
];
1583 * <-- [rx_offset] -->
1584 * ---------------------------------------------------------
1585 * | [XX] | metadata | packet | XXXX |
1586 * ---------------------------------------------------------
1587 * <---------------- data_len --------------->
1589 * The rx_offset is fixed for all packets, the meta_len can vary
1590 * on a packet by packet basis. If rx_offset is set to zero
1591 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1592 * buffer and is immediately followed by the packet (no [XX]).
1594 meta_len
= rxd
->rxd
.meta_len_dd
& PCIE_DESC_RX_META_LEN_MASK
;
1595 data_len
= le16_to_cpu(rxd
->rxd
.data_len
);
1596 pkt_len
= data_len
- meta_len
;
1598 if (dp
->rx_offset
== NFP_NET_CFG_RX_OFFSET_DYNAMIC
)
1599 data_off
= NFP_NET_RX_BUF_HEADROOM
+ meta_len
;
1601 data_off
= NFP_NET_RX_BUF_HEADROOM
+ dp
->rx_offset
;
1602 data_off
+= dp
->rx_dma_off
;
1605 u64_stats_update_begin(&r_vec
->rx_sync
);
1607 r_vec
->rx_bytes
+= pkt_len
;
1608 u64_stats_update_end(&r_vec
->rx_sync
);
1610 /* Pointer to start of metadata */
1611 meta
= rxbuf
->frag
+ data_off
- meta_len
;
1613 if (unlikely(meta_len
> NFP_NET_MAX_PREPEND
||
1614 (dp
->rx_offset
&& meta_len
> dp
->rx_offset
))) {
1615 nn_dp_warn(dp
, "oversized RX packet metadata %u\n",
1617 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, NULL
);
1621 if (xdp_prog
&& !(rxd
->rxd
.flags
& PCIE_DESC_RX_BPF
&&
1622 dp
->bpf_offload_xdp
)) {
1623 unsigned int dma_off
;
1627 hard_start
= rxbuf
->frag
+ NFP_NET_RX_BUF_HEADROOM
;
1628 dma_off
= data_off
- NFP_NET_RX_BUF_HEADROOM
;
1629 dma_sync_single_for_cpu(dp
->dev
, rxbuf
->dma_addr
,
1633 /* Move prepend out of the way */
1634 if (xdp_prog
->xdp_adjust_head
) {
1635 memcpy(meta_prepend
, meta
, meta_len
);
1636 meta
= meta_prepend
;
1639 act
= nfp_net_run_xdp(xdp_prog
, rxbuf
->frag
, hard_start
,
1640 &data_off
, &pkt_len
);
1645 dma_off
= data_off
- NFP_NET_RX_BUF_HEADROOM
;
1646 if (unlikely(!nfp_net_tx_xdp_buf(dp
, rx_ring
,
1650 trace_xdp_exception(dp
->netdev
,
1654 bpf_warn_invalid_xdp_action(act
);
1656 trace_xdp_exception(dp
->netdev
, xdp_prog
, act
);
1658 nfp_net_rx_give_one(dp
, rx_ring
, rxbuf
->frag
,
1664 skb
= build_skb(rxbuf
->frag
, true_bufsz
);
1665 if (unlikely(!skb
)) {
1666 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, NULL
);
1669 new_frag
= nfp_net_napi_alloc_one(dp
, &new_dma_addr
);
1670 if (unlikely(!new_frag
)) {
1671 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, rxbuf
, skb
);
1675 nfp_net_dma_unmap_rx(dp
, rxbuf
->dma_addr
);
1677 nfp_net_rx_give_one(dp
, rx_ring
, new_frag
, new_dma_addr
);
1679 skb_reserve(skb
, data_off
);
1680 skb_put(skb
, pkt_len
);
1682 if (!dp
->chained_metadata_format
) {
1683 nfp_net_set_hash_desc(dp
->netdev
, skb
, meta
, rxd
);
1684 } else if (meta_len
) {
1687 end
= nfp_net_parse_meta(dp
->netdev
, skb
, meta
,
1689 if (unlikely(end
!= meta
+ meta_len
)) {
1690 nn_dp_warn(dp
, "invalid RX packet metadata\n");
1691 nfp_net_rx_drop(dp
, r_vec
, rx_ring
, NULL
, skb
);
1696 skb_record_rx_queue(skb
, rx_ring
->idx
);
1697 skb
->protocol
= eth_type_trans(skb
, dp
->netdev
);
1699 nfp_net_rx_csum(dp
, r_vec
, rxd
, skb
);
1701 if (rxd
->rxd
.flags
& PCIE_DESC_RX_VLAN
)
1702 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
),
1703 le16_to_cpu(rxd
->rxd
.vlan
));
1705 napi_gro_receive(&rx_ring
->r_vec
->napi
, skb
);
1708 if (xdp_prog
&& tx_ring
->wr_ptr_add
)
1709 nfp_net_tx_xmit_more_flush(tx_ring
);
1716 * nfp_net_poll() - napi poll function
1717 * @napi: NAPI structure
1718 * @budget: NAPI budget
1720 * Return: number of packets polled.
1722 static int nfp_net_poll(struct napi_struct
*napi
, int budget
)
1724 struct nfp_net_r_vector
*r_vec
=
1725 container_of(napi
, struct nfp_net_r_vector
, napi
);
1726 unsigned int pkts_polled
= 0;
1729 nfp_net_tx_complete(r_vec
->tx_ring
);
1730 if (r_vec
->rx_ring
) {
1731 pkts_polled
= nfp_net_rx(r_vec
->rx_ring
, budget
);
1732 if (r_vec
->xdp_ring
)
1733 nfp_net_xdp_complete(r_vec
->xdp_ring
);
1736 if (pkts_polled
< budget
)
1737 if (napi_complete_done(napi
, pkts_polled
))
1738 nfp_net_irq_unmask(r_vec
->nfp_net
, r_vec
->irq_entry
);
1743 /* Setup and Configuration
1747 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
1748 * @tx_ring: TX ring to free
1750 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring
*tx_ring
)
1752 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
1753 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
1755 kfree(tx_ring
->txbufs
);
1758 dma_free_coherent(dp
->dev
, tx_ring
->size
,
1759 tx_ring
->txds
, tx_ring
->dma
);
1762 tx_ring
->txbufs
= NULL
;
1763 tx_ring
->txds
= NULL
;
1769 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
1770 * @dp: NFP Net data path struct
1771 * @tx_ring: TX Ring structure to allocate
1772 * @is_xdp: True if ring will be used for XDP
1774 * Return: 0 on success, negative errno otherwise.
1777 nfp_net_tx_ring_alloc(struct nfp_net_dp
*dp
, struct nfp_net_tx_ring
*tx_ring
,
1780 struct nfp_net_r_vector
*r_vec
= tx_ring
->r_vec
;
1783 tx_ring
->cnt
= dp
->txd_cnt
;
1785 tx_ring
->size
= sizeof(*tx_ring
->txds
) * tx_ring
->cnt
;
1786 tx_ring
->txds
= dma_zalloc_coherent(dp
->dev
, tx_ring
->size
,
1787 &tx_ring
->dma
, GFP_KERNEL
);
1791 sz
= sizeof(*tx_ring
->txbufs
) * tx_ring
->cnt
;
1792 tx_ring
->txbufs
= kzalloc(sz
, GFP_KERNEL
);
1793 if (!tx_ring
->txbufs
)
1797 netif_set_xps_queue(dp
->netdev
, &r_vec
->affinity_mask
,
1803 nfp_net_tx_ring_free(tx_ring
);
1807 static int nfp_net_tx_rings_prepare(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
1811 dp
->tx_rings
= kcalloc(dp
->num_tx_rings
, sizeof(*dp
->tx_rings
),
1816 for (r
= 0; r
< dp
->num_tx_rings
; r
++) {
1819 if (r
>= dp
->num_stack_tx_rings
)
1820 bias
= dp
->num_stack_tx_rings
;
1822 nfp_net_tx_ring_init(&dp
->tx_rings
[r
], &nn
->r_vecs
[r
- bias
],
1825 if (nfp_net_tx_ring_alloc(dp
, &dp
->tx_rings
[r
], bias
))
1833 nfp_net_tx_ring_free(&dp
->tx_rings
[r
]);
1834 kfree(dp
->tx_rings
);
1838 static void nfp_net_tx_rings_free(struct nfp_net_dp
*dp
)
1842 for (r
= 0; r
< dp
->num_tx_rings
; r
++)
1843 nfp_net_tx_ring_free(&dp
->tx_rings
[r
]);
1845 kfree(dp
->tx_rings
);
1849 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
1850 * @rx_ring: RX ring to free
1852 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring
*rx_ring
)
1854 struct nfp_net_r_vector
*r_vec
= rx_ring
->r_vec
;
1855 struct nfp_net_dp
*dp
= &r_vec
->nfp_net
->dp
;
1857 kfree(rx_ring
->rxbufs
);
1860 dma_free_coherent(dp
->dev
, rx_ring
->size
,
1861 rx_ring
->rxds
, rx_ring
->dma
);
1864 rx_ring
->rxbufs
= NULL
;
1865 rx_ring
->rxds
= NULL
;
1871 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
1872 * @dp: NFP Net data path struct
1873 * @rx_ring: RX ring to allocate
1875 * Return: 0 on success, negative errno otherwise.
1878 nfp_net_rx_ring_alloc(struct nfp_net_dp
*dp
, struct nfp_net_rx_ring
*rx_ring
)
1882 rx_ring
->cnt
= dp
->rxd_cnt
;
1883 rx_ring
->size
= sizeof(*rx_ring
->rxds
) * rx_ring
->cnt
;
1884 rx_ring
->rxds
= dma_zalloc_coherent(dp
->dev
, rx_ring
->size
,
1885 &rx_ring
->dma
, GFP_KERNEL
);
1889 sz
= sizeof(*rx_ring
->rxbufs
) * rx_ring
->cnt
;
1890 rx_ring
->rxbufs
= kzalloc(sz
, GFP_KERNEL
);
1891 if (!rx_ring
->rxbufs
)
1897 nfp_net_rx_ring_free(rx_ring
);
1901 static int nfp_net_rx_rings_prepare(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
1905 dp
->rx_rings
= kcalloc(dp
->num_rx_rings
, sizeof(*dp
->rx_rings
),
1910 for (r
= 0; r
< dp
->num_rx_rings
; r
++) {
1911 nfp_net_rx_ring_init(&dp
->rx_rings
[r
], &nn
->r_vecs
[r
], r
);
1913 if (nfp_net_rx_ring_alloc(dp
, &dp
->rx_rings
[r
]))
1916 if (nfp_net_rx_ring_bufs_alloc(dp
, &dp
->rx_rings
[r
]))
1924 nfp_net_rx_ring_bufs_free(dp
, &dp
->rx_rings
[r
]);
1926 nfp_net_rx_ring_free(&dp
->rx_rings
[r
]);
1928 kfree(dp
->rx_rings
);
1932 static void nfp_net_rx_rings_free(struct nfp_net_dp
*dp
)
1936 for (r
= 0; r
< dp
->num_rx_rings
; r
++) {
1937 nfp_net_rx_ring_bufs_free(dp
, &dp
->rx_rings
[r
]);
1938 nfp_net_rx_ring_free(&dp
->rx_rings
[r
]);
1941 kfree(dp
->rx_rings
);
1945 nfp_net_vector_assign_rings(struct nfp_net_dp
*dp
,
1946 struct nfp_net_r_vector
*r_vec
, int idx
)
1948 r_vec
->rx_ring
= idx
< dp
->num_rx_rings
? &dp
->rx_rings
[idx
] : NULL
;
1950 idx
< dp
->num_stack_tx_rings
? &dp
->tx_rings
[idx
] : NULL
;
1952 r_vec
->xdp_ring
= idx
< dp
->num_tx_rings
- dp
->num_stack_tx_rings
?
1953 &dp
->tx_rings
[dp
->num_stack_tx_rings
+ idx
] : NULL
;
1957 nfp_net_prepare_vector(struct nfp_net
*nn
, struct nfp_net_r_vector
*r_vec
,
1963 netif_napi_add(nn
->dp
.netdev
, &r_vec
->napi
,
1964 nfp_net_poll
, NAPI_POLL_WEIGHT
);
1966 snprintf(r_vec
->name
, sizeof(r_vec
->name
),
1967 "%s-rxtx-%d", nn
->dp
.netdev
->name
, idx
);
1968 err
= request_irq(r_vec
->irq_vector
, r_vec
->handler
, 0, r_vec
->name
,
1971 netif_napi_del(&r_vec
->napi
);
1972 nn_err(nn
, "Error requesting IRQ %d\n", r_vec
->irq_vector
);
1975 disable_irq(r_vec
->irq_vector
);
1977 irq_set_affinity_hint(r_vec
->irq_vector
, &r_vec
->affinity_mask
);
1979 nn_dbg(nn
, "RV%02d: irq=%03d/%03d\n", idx
, r_vec
->irq_vector
,
1986 nfp_net_cleanup_vector(struct nfp_net
*nn
, struct nfp_net_r_vector
*r_vec
)
1988 irq_set_affinity_hint(r_vec
->irq_vector
, NULL
);
1989 netif_napi_del(&r_vec
->napi
);
1990 free_irq(r_vec
->irq_vector
, r_vec
);
1994 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
1995 * @nn: NFP Net device to reconfigure
1997 void nfp_net_rss_write_itbl(struct nfp_net
*nn
)
2001 for (i
= 0; i
< NFP_NET_CFG_RSS_ITBL_SZ
; i
+= 4)
2002 nn_writel(nn
, NFP_NET_CFG_RSS_ITBL
+ i
,
2003 get_unaligned_le32(nn
->rss_itbl
+ i
));
2007 * nfp_net_rss_write_key() - Write RSS hash key to device
2008 * @nn: NFP Net device to reconfigure
2010 void nfp_net_rss_write_key(struct nfp_net
*nn
)
2014 for (i
= 0; i
< nfp_net_rss_key_sz(nn
); i
+= 4)
2015 nn_writel(nn
, NFP_NET_CFG_RSS_KEY
+ i
,
2016 get_unaligned_le32(nn
->rss_key
+ i
));
2020 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2021 * @nn: NFP Net device to reconfigure
2023 void nfp_net_coalesce_write_cfg(struct nfp_net
*nn
)
2029 /* Compute factor used to convert coalesce '_usecs' parameters to
2030 * ME timestamp ticks. There are 16 ME clock cycles for each timestamp
2033 factor
= nn
->me_freq_mhz
/ 16;
2035 /* copy RX interrupt coalesce parameters */
2036 value
= (nn
->rx_coalesce_max_frames
<< 16) |
2037 (factor
* nn
->rx_coalesce_usecs
);
2038 for (i
= 0; i
< nn
->dp
.num_rx_rings
; i
++)
2039 nn_writel(nn
, NFP_NET_CFG_RXR_IRQ_MOD(i
), value
);
2041 /* copy TX interrupt coalesce parameters */
2042 value
= (nn
->tx_coalesce_max_frames
<< 16) |
2043 (factor
* nn
->tx_coalesce_usecs
);
2044 for (i
= 0; i
< nn
->dp
.num_tx_rings
; i
++)
2045 nn_writel(nn
, NFP_NET_CFG_TXR_IRQ_MOD(i
), value
);
2049 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2050 * @nn: NFP Net device to reconfigure
2052 * Writes the MAC address from the netdev to the device control BAR. Does not
2053 * perform the required reconfig. We do a bit of byte swapping dance because
2056 static void nfp_net_write_mac_addr(struct nfp_net
*nn
)
2058 nn_writel(nn
, NFP_NET_CFG_MACADDR
+ 0,
2059 get_unaligned_be32(nn
->dp
.netdev
->dev_addr
));
2060 nn_writew(nn
, NFP_NET_CFG_MACADDR
+ 6,
2061 get_unaligned_be16(nn
->dp
.netdev
->dev_addr
+ 4));
2064 static void nfp_net_vec_clear_ring_data(struct nfp_net
*nn
, unsigned int idx
)
2066 nn_writeq(nn
, NFP_NET_CFG_RXR_ADDR(idx
), 0);
2067 nn_writeb(nn
, NFP_NET_CFG_RXR_SZ(idx
), 0);
2068 nn_writeb(nn
, NFP_NET_CFG_RXR_VEC(idx
), 0);
2070 nn_writeq(nn
, NFP_NET_CFG_TXR_ADDR(idx
), 0);
2071 nn_writeb(nn
, NFP_NET_CFG_TXR_SZ(idx
), 0);
2072 nn_writeb(nn
, NFP_NET_CFG_TXR_VEC(idx
), 0);
2076 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2077 * @nn: NFP Net device to reconfigure
2079 static void nfp_net_clear_config_and_disable(struct nfp_net
*nn
)
2081 u32 new_ctrl
, update
;
2085 new_ctrl
= nn
->dp
.ctrl
;
2086 new_ctrl
&= ~NFP_NET_CFG_CTRL_ENABLE
;
2087 update
= NFP_NET_CFG_UPDATE_GEN
;
2088 update
|= NFP_NET_CFG_UPDATE_MSIX
;
2089 update
|= NFP_NET_CFG_UPDATE_RING
;
2091 if (nn
->cap
& NFP_NET_CFG_CTRL_RINGCFG
)
2092 new_ctrl
&= ~NFP_NET_CFG_CTRL_RINGCFG
;
2094 nn_writeq(nn
, NFP_NET_CFG_TXRS_ENABLE
, 0);
2095 nn_writeq(nn
, NFP_NET_CFG_RXRS_ENABLE
, 0);
2097 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
2098 err
= nfp_net_reconfig(nn
, update
);
2100 nn_err(nn
, "Could not disable device: %d\n", err
);
2102 for (r
= 0; r
< nn
->dp
.num_rx_rings
; r
++)
2103 nfp_net_rx_ring_reset(&nn
->dp
.rx_rings
[r
]);
2104 for (r
= 0; r
< nn
->dp
.num_tx_rings
; r
++)
2105 nfp_net_tx_ring_reset(&nn
->dp
, &nn
->dp
.tx_rings
[r
]);
2106 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++)
2107 nfp_net_vec_clear_ring_data(nn
, r
);
2109 nn
->dp
.ctrl
= new_ctrl
;
2113 nfp_net_rx_ring_hw_cfg_write(struct nfp_net
*nn
,
2114 struct nfp_net_rx_ring
*rx_ring
, unsigned int idx
)
2116 /* Write the DMA address, size and MSI-X info to the device */
2117 nn_writeq(nn
, NFP_NET_CFG_RXR_ADDR(idx
), rx_ring
->dma
);
2118 nn_writeb(nn
, NFP_NET_CFG_RXR_SZ(idx
), ilog2(rx_ring
->cnt
));
2119 nn_writeb(nn
, NFP_NET_CFG_RXR_VEC(idx
), rx_ring
->r_vec
->irq_entry
);
2123 nfp_net_tx_ring_hw_cfg_write(struct nfp_net
*nn
,
2124 struct nfp_net_tx_ring
*tx_ring
, unsigned int idx
)
2126 nn_writeq(nn
, NFP_NET_CFG_TXR_ADDR(idx
), tx_ring
->dma
);
2127 nn_writeb(nn
, NFP_NET_CFG_TXR_SZ(idx
), ilog2(tx_ring
->cnt
));
2128 nn_writeb(nn
, NFP_NET_CFG_TXR_VEC(idx
), tx_ring
->r_vec
->irq_entry
);
2131 static int __nfp_net_set_config_and_enable(struct nfp_net
*nn
)
2133 u32 new_ctrl
, update
= 0;
2137 new_ctrl
= nn
->dp
.ctrl
;
2139 if (nn
->cap
& NFP_NET_CFG_CTRL_RSS
) {
2140 nfp_net_rss_write_key(nn
);
2141 nfp_net_rss_write_itbl(nn
);
2142 nn_writel(nn
, NFP_NET_CFG_RSS_CTRL
, nn
->rss_cfg
);
2143 update
|= NFP_NET_CFG_UPDATE_RSS
;
2146 if (nn
->cap
& NFP_NET_CFG_CTRL_IRQMOD
) {
2147 nfp_net_coalesce_write_cfg(nn
);
2149 new_ctrl
|= NFP_NET_CFG_CTRL_IRQMOD
;
2150 update
|= NFP_NET_CFG_UPDATE_IRQMOD
;
2153 for (r
= 0; r
< nn
->dp
.num_tx_rings
; r
++)
2154 nfp_net_tx_ring_hw_cfg_write(nn
, &nn
->dp
.tx_rings
[r
], r
);
2155 for (r
= 0; r
< nn
->dp
.num_rx_rings
; r
++)
2156 nfp_net_rx_ring_hw_cfg_write(nn
, &nn
->dp
.rx_rings
[r
], r
);
2158 nn_writeq(nn
, NFP_NET_CFG_TXRS_ENABLE
, nn
->dp
.num_tx_rings
== 64 ?
2159 0xffffffffffffffffULL
: ((u64
)1 << nn
->dp
.num_tx_rings
) - 1);
2161 nn_writeq(nn
, NFP_NET_CFG_RXRS_ENABLE
, nn
->dp
.num_rx_rings
== 64 ?
2162 0xffffffffffffffffULL
: ((u64
)1 << nn
->dp
.num_rx_rings
) - 1);
2164 nfp_net_write_mac_addr(nn
);
2166 nn_writel(nn
, NFP_NET_CFG_MTU
, nn
->dp
.netdev
->mtu
);
2167 nn_writel(nn
, NFP_NET_CFG_FLBUFSZ
,
2168 nn
->dp
.fl_bufsz
- NFP_NET_RX_BUF_NON_DATA
);
2171 new_ctrl
|= NFP_NET_CFG_CTRL_ENABLE
;
2172 update
|= NFP_NET_CFG_UPDATE_GEN
;
2173 update
|= NFP_NET_CFG_UPDATE_MSIX
;
2174 update
|= NFP_NET_CFG_UPDATE_RING
;
2175 if (nn
->cap
& NFP_NET_CFG_CTRL_RINGCFG
)
2176 new_ctrl
|= NFP_NET_CFG_CTRL_RINGCFG
;
2178 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
2179 err
= nfp_net_reconfig(nn
, update
);
2181 nn
->dp
.ctrl
= new_ctrl
;
2183 for (r
= 0; r
< nn
->dp
.num_rx_rings
; r
++)
2184 nfp_net_rx_ring_fill_freelist(&nn
->dp
, &nn
->dp
.rx_rings
[r
]);
2186 /* Since reconfiguration requests while NFP is down are ignored we
2187 * have to wipe the entire VXLAN configuration and reinitialize it.
2189 if (nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_VXLAN
) {
2190 memset(&nn
->vxlan_ports
, 0, sizeof(nn
->vxlan_ports
));
2191 memset(&nn
->vxlan_usecnt
, 0, sizeof(nn
->vxlan_usecnt
));
2192 udp_tunnel_get_rx_info(nn
->dp
.netdev
);
2199 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2200 * @nn: NFP Net device to reconfigure
2202 static int nfp_net_set_config_and_enable(struct nfp_net
*nn
)
2206 err
= __nfp_net_set_config_and_enable(nn
);
2208 nfp_net_clear_config_and_disable(nn
);
2214 * nfp_net_open_stack() - Start the device from stack's perspective
2215 * @nn: NFP Net device to reconfigure
2217 static void nfp_net_open_stack(struct nfp_net
*nn
)
2221 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2222 napi_enable(&nn
->r_vecs
[r
].napi
);
2223 enable_irq(nn
->r_vecs
[r
].irq_vector
);
2226 netif_tx_wake_all_queues(nn
->dp
.netdev
);
2228 enable_irq(nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
].vector
);
2229 nfp_net_read_link_status(nn
);
2232 static int nfp_net_netdev_open(struct net_device
*netdev
)
2234 struct nfp_net
*nn
= netdev_priv(netdev
);
2237 if (nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_ENABLE
) {
2238 nn_err(nn
, "Dev is already enabled: 0x%08x\n", nn
->dp
.ctrl
);
2242 /* Step 1: Allocate resources for rings and the like
2243 * - Request interrupts
2244 * - Allocate RX and TX ring resources
2245 * - Setup initial RSS table
2247 err
= nfp_net_aux_irq_request(nn
, NFP_NET_CFG_EXN
, "%s-exn",
2248 nn
->exn_name
, sizeof(nn
->exn_name
),
2249 NFP_NET_IRQ_EXN_IDX
, nn
->exn_handler
);
2252 err
= nfp_net_aux_irq_request(nn
, NFP_NET_CFG_LSC
, "%s-lsc",
2253 nn
->lsc_name
, sizeof(nn
->lsc_name
),
2254 NFP_NET_IRQ_LSC_IDX
, nn
->lsc_handler
);
2257 disable_irq(nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
].vector
);
2259 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2260 err
= nfp_net_prepare_vector(nn
, &nn
->r_vecs
[r
], r
);
2262 goto err_cleanup_vec_p
;
2265 err
= nfp_net_rx_rings_prepare(nn
, &nn
->dp
);
2267 goto err_cleanup_vec
;
2269 err
= nfp_net_tx_rings_prepare(nn
, &nn
->dp
);
2271 goto err_free_rx_rings
;
2273 for (r
= 0; r
< nn
->max_r_vecs
; r
++)
2274 nfp_net_vector_assign_rings(&nn
->dp
, &nn
->r_vecs
[r
], r
);
2276 err
= netif_set_real_num_tx_queues(netdev
, nn
->dp
.num_stack_tx_rings
);
2278 goto err_free_rings
;
2280 err
= netif_set_real_num_rx_queues(netdev
, nn
->dp
.num_rx_rings
);
2282 goto err_free_rings
;
2284 /* Step 2: Configure the NFP
2285 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2286 * - Write MAC address (in case it changed)
2288 * - Set the Freelist buffer size
2291 err
= nfp_net_set_config_and_enable(nn
);
2293 goto err_free_rings
;
2295 /* Step 3: Enable for kernel
2296 * - put some freelist descriptors on each RX ring
2297 * - enable NAPI on each ring
2298 * - enable all TX queues
2301 nfp_net_open_stack(nn
);
2306 nfp_net_tx_rings_free(&nn
->dp
);
2308 nfp_net_rx_rings_free(&nn
->dp
);
2310 r
= nn
->dp
.num_r_vecs
;
2313 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
2314 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_LSC
, NFP_NET_IRQ_LSC_IDX
);
2316 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_EXN
, NFP_NET_IRQ_EXN_IDX
);
2321 * nfp_net_close_stack() - Quiescent the stack (part of close)
2322 * @nn: NFP Net device to reconfigure
2324 static void nfp_net_close_stack(struct nfp_net
*nn
)
2328 disable_irq(nn
->irq_entries
[NFP_NET_IRQ_LSC_IDX
].vector
);
2329 netif_carrier_off(nn
->dp
.netdev
);
2330 nn
->link_up
= false;
2332 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2333 disable_irq(nn
->r_vecs
[r
].irq_vector
);
2334 napi_disable(&nn
->r_vecs
[r
].napi
);
2337 netif_tx_disable(nn
->dp
.netdev
);
2341 * nfp_net_close_free_all() - Free all runtime resources
2342 * @nn: NFP Net device to reconfigure
2344 static void nfp_net_close_free_all(struct nfp_net
*nn
)
2348 for (r
= 0; r
< nn
->dp
.num_rx_rings
; r
++) {
2349 nfp_net_rx_ring_bufs_free(&nn
->dp
, &nn
->dp
.rx_rings
[r
]);
2350 nfp_net_rx_ring_free(&nn
->dp
.rx_rings
[r
]);
2352 for (r
= 0; r
< nn
->dp
.num_tx_rings
; r
++)
2353 nfp_net_tx_ring_free(&nn
->dp
.tx_rings
[r
]);
2354 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++)
2355 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
2357 kfree(nn
->dp
.rx_rings
);
2358 kfree(nn
->dp
.tx_rings
);
2360 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_LSC
, NFP_NET_IRQ_LSC_IDX
);
2361 nfp_net_aux_irq_free(nn
, NFP_NET_CFG_EXN
, NFP_NET_IRQ_EXN_IDX
);
2365 * nfp_net_netdev_close() - Called when the device is downed
2366 * @netdev: netdev structure
2368 static int nfp_net_netdev_close(struct net_device
*netdev
)
2370 struct nfp_net
*nn
= netdev_priv(netdev
);
2372 if (!(nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_ENABLE
)) {
2373 nn_err(nn
, "Dev is not up: 0x%08x\n", nn
->dp
.ctrl
);
2377 /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2379 nfp_net_close_stack(nn
);
2383 nfp_net_clear_config_and_disable(nn
);
2385 /* Step 3: Free resources
2387 nfp_net_close_free_all(nn
);
2389 nn_dbg(nn
, "%s down", netdev
->name
);
2393 static void nfp_net_set_rx_mode(struct net_device
*netdev
)
2395 struct nfp_net
*nn
= netdev_priv(netdev
);
2398 new_ctrl
= nn
->dp
.ctrl
;
2400 if (netdev
->flags
& IFF_PROMISC
) {
2401 if (nn
->cap
& NFP_NET_CFG_CTRL_PROMISC
)
2402 new_ctrl
|= NFP_NET_CFG_CTRL_PROMISC
;
2404 nn_warn(nn
, "FW does not support promiscuous mode\n");
2406 new_ctrl
&= ~NFP_NET_CFG_CTRL_PROMISC
;
2409 if (new_ctrl
== nn
->dp
.ctrl
)
2412 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
2413 nfp_net_reconfig_post(nn
, NFP_NET_CFG_UPDATE_GEN
);
2415 nn
->dp
.ctrl
= new_ctrl
;
2418 static void nfp_net_rss_init_itbl(struct nfp_net
*nn
)
2422 for (i
= 0; i
< sizeof(nn
->rss_itbl
); i
++)
2424 ethtool_rxfh_indir_default(i
, nn
->dp
.num_rx_rings
);
2427 static void nfp_net_dp_swap(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2429 struct nfp_net_dp new_dp
= *dp
;
2434 nn
->dp
.netdev
->mtu
= new_dp
.mtu
;
2436 if (!netif_is_rxfh_configured(nn
->dp
.netdev
))
2437 nfp_net_rss_init_itbl(nn
);
2440 static int nfp_net_dp_swap_enable(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2445 nfp_net_dp_swap(nn
, dp
);
2447 for (r
= 0; r
< nn
->max_r_vecs
; r
++)
2448 nfp_net_vector_assign_rings(&nn
->dp
, &nn
->r_vecs
[r
], r
);
2450 err
= netif_set_real_num_rx_queues(nn
->dp
.netdev
, nn
->dp
.num_rx_rings
);
2454 if (nn
->dp
.netdev
->real_num_tx_queues
!= nn
->dp
.num_stack_tx_rings
) {
2455 err
= netif_set_real_num_tx_queues(nn
->dp
.netdev
,
2456 nn
->dp
.num_stack_tx_rings
);
2461 return __nfp_net_set_config_and_enable(nn
);
2464 struct nfp_net_dp
*nfp_net_clone_dp(struct nfp_net
*nn
)
2466 struct nfp_net_dp
*new;
2468 new = kmalloc(sizeof(*new), GFP_KERNEL
);
2474 /* Clear things which need to be recomputed */
2476 new->tx_rings
= NULL
;
2477 new->rx_rings
= NULL
;
2478 new->num_r_vecs
= 0;
2479 new->num_stack_tx_rings
= 0;
2484 static int nfp_net_check_config(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2486 /* XDP-enabled tests */
2489 if (dp
->fl_bufsz
> PAGE_SIZE
) {
2490 nn_warn(nn
, "MTU too large w/ XDP enabled\n");
2493 if (dp
->num_tx_rings
> nn
->max_tx_rings
) {
2494 nn_warn(nn
, "Insufficient number of TX rings w/ XDP enabled\n");
2501 int nfp_net_ring_reconfig(struct nfp_net
*nn
, struct nfp_net_dp
*dp
)
2505 dp
->fl_bufsz
= nfp_net_calc_fl_bufsz(dp
);
2507 dp
->num_stack_tx_rings
= dp
->num_tx_rings
;
2509 dp
->num_stack_tx_rings
-= dp
->num_rx_rings
;
2511 dp
->num_r_vecs
= max(dp
->num_rx_rings
, dp
->num_stack_tx_rings
);
2513 err
= nfp_net_check_config(nn
, dp
);
2517 if (!netif_running(dp
->netdev
)) {
2518 nfp_net_dp_swap(nn
, dp
);
2523 /* Prepare new rings */
2524 for (r
= nn
->dp
.num_r_vecs
; r
< dp
->num_r_vecs
; r
++) {
2525 err
= nfp_net_prepare_vector(nn
, &nn
->r_vecs
[r
], r
);
2528 goto err_cleanup_vecs
;
2532 err
= nfp_net_rx_rings_prepare(nn
, dp
);
2534 goto err_cleanup_vecs
;
2536 err
= nfp_net_tx_rings_prepare(nn
, dp
);
2540 /* Stop device, swap in new rings, try to start the firmware */
2541 nfp_net_close_stack(nn
);
2542 nfp_net_clear_config_and_disable(nn
);
2544 err
= nfp_net_dp_swap_enable(nn
, dp
);
2548 nfp_net_clear_config_and_disable(nn
);
2550 /* Try with old configuration and old rings */
2551 err2
= nfp_net_dp_swap_enable(nn
, dp
);
2553 nn_err(nn
, "Can't restore ring config - FW communication failed (%d,%d)\n",
2556 for (r
= dp
->num_r_vecs
- 1; r
>= nn
->dp
.num_r_vecs
; r
--)
2557 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
2559 nfp_net_rx_rings_free(dp
);
2560 nfp_net_tx_rings_free(dp
);
2562 nfp_net_open_stack(nn
);
2569 nfp_net_rx_rings_free(dp
);
2571 for (r
= dp
->num_r_vecs
- 1; r
>= nn
->dp
.num_r_vecs
; r
--)
2572 nfp_net_cleanup_vector(nn
, &nn
->r_vecs
[r
]);
2577 static int nfp_net_change_mtu(struct net_device
*netdev
, int new_mtu
)
2579 struct nfp_net
*nn
= netdev_priv(netdev
);
2580 struct nfp_net_dp
*dp
;
2582 dp
= nfp_net_clone_dp(nn
);
2588 return nfp_net_ring_reconfig(nn
, dp
);
2591 static void nfp_net_stat64(struct net_device
*netdev
,
2592 struct rtnl_link_stats64
*stats
)
2594 struct nfp_net
*nn
= netdev_priv(netdev
);
2597 for (r
= 0; r
< nn
->dp
.num_r_vecs
; r
++) {
2598 struct nfp_net_r_vector
*r_vec
= &nn
->r_vecs
[r
];
2603 start
= u64_stats_fetch_begin(&r_vec
->rx_sync
);
2604 data
[0] = r_vec
->rx_pkts
;
2605 data
[1] = r_vec
->rx_bytes
;
2606 data
[2] = r_vec
->rx_drops
;
2607 } while (u64_stats_fetch_retry(&r_vec
->rx_sync
, start
));
2608 stats
->rx_packets
+= data
[0];
2609 stats
->rx_bytes
+= data
[1];
2610 stats
->rx_dropped
+= data
[2];
2613 start
= u64_stats_fetch_begin(&r_vec
->tx_sync
);
2614 data
[0] = r_vec
->tx_pkts
;
2615 data
[1] = r_vec
->tx_bytes
;
2616 data
[2] = r_vec
->tx_errors
;
2617 } while (u64_stats_fetch_retry(&r_vec
->tx_sync
, start
));
2618 stats
->tx_packets
+= data
[0];
2619 stats
->tx_bytes
+= data
[1];
2620 stats
->tx_errors
+= data
[2];
2624 static bool nfp_net_ebpf_capable(struct nfp_net
*nn
)
2626 if (nn
->cap
& NFP_NET_CFG_CTRL_BPF
&&
2627 nn_readb(nn
, NFP_NET_CFG_BPF_ABI
) == NFP_NET_BPF_ABI
)
2633 nfp_net_setup_tc(struct net_device
*netdev
, u32 handle
, __be16 proto
,
2634 struct tc_to_netdev
*tc
)
2636 struct nfp_net
*nn
= netdev_priv(netdev
);
2638 if (TC_H_MAJ(handle
) != TC_H_MAJ(TC_H_INGRESS
))
2640 if (proto
!= htons(ETH_P_ALL
))
2643 if (tc
->type
== TC_SETUP_CLSBPF
&& nfp_net_ebpf_capable(nn
)) {
2644 if (!nn
->dp
.bpf_offload_xdp
)
2645 return nfp_net_bpf_offload(nn
, tc
->cls_bpf
);
2653 static int nfp_net_set_features(struct net_device
*netdev
,
2654 netdev_features_t features
)
2656 netdev_features_t changed
= netdev
->features
^ features
;
2657 struct nfp_net
*nn
= netdev_priv(netdev
);
2661 /* Assume this is not called with features we have not advertised */
2663 new_ctrl
= nn
->dp
.ctrl
;
2665 if (changed
& NETIF_F_RXCSUM
) {
2666 if (features
& NETIF_F_RXCSUM
)
2667 new_ctrl
|= NFP_NET_CFG_CTRL_RXCSUM
;
2669 new_ctrl
&= ~NFP_NET_CFG_CTRL_RXCSUM
;
2672 if (changed
& (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
)) {
2673 if (features
& (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
))
2674 new_ctrl
|= NFP_NET_CFG_CTRL_TXCSUM
;
2676 new_ctrl
&= ~NFP_NET_CFG_CTRL_TXCSUM
;
2679 if (changed
& (NETIF_F_TSO
| NETIF_F_TSO6
)) {
2680 if (features
& (NETIF_F_TSO
| NETIF_F_TSO6
))
2681 new_ctrl
|= NFP_NET_CFG_CTRL_LSO
;
2683 new_ctrl
&= ~NFP_NET_CFG_CTRL_LSO
;
2686 if (changed
& NETIF_F_HW_VLAN_CTAG_RX
) {
2687 if (features
& NETIF_F_HW_VLAN_CTAG_RX
)
2688 new_ctrl
|= NFP_NET_CFG_CTRL_RXVLAN
;
2690 new_ctrl
&= ~NFP_NET_CFG_CTRL_RXVLAN
;
2693 if (changed
& NETIF_F_HW_VLAN_CTAG_TX
) {
2694 if (features
& NETIF_F_HW_VLAN_CTAG_TX
)
2695 new_ctrl
|= NFP_NET_CFG_CTRL_TXVLAN
;
2697 new_ctrl
&= ~NFP_NET_CFG_CTRL_TXVLAN
;
2700 if (changed
& NETIF_F_SG
) {
2701 if (features
& NETIF_F_SG
)
2702 new_ctrl
|= NFP_NET_CFG_CTRL_GATHER
;
2704 new_ctrl
&= ~NFP_NET_CFG_CTRL_GATHER
;
2707 if (changed
& NETIF_F_HW_TC
&& nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_BPF
) {
2708 nn_err(nn
, "Cannot disable HW TC offload while in use\n");
2712 nn_dbg(nn
, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
2713 netdev
->features
, features
, changed
);
2715 if (new_ctrl
== nn
->dp
.ctrl
)
2718 nn_dbg(nn
, "NIC ctrl: 0x%x -> 0x%x\n", nn
->dp
.ctrl
, new_ctrl
);
2719 nn_writel(nn
, NFP_NET_CFG_CTRL
, new_ctrl
);
2720 err
= nfp_net_reconfig(nn
, NFP_NET_CFG_UPDATE_GEN
);
2724 nn
->dp
.ctrl
= new_ctrl
;
2729 static netdev_features_t
2730 nfp_net_features_check(struct sk_buff
*skb
, struct net_device
*dev
,
2731 netdev_features_t features
)
2735 /* We can't do TSO over double tagged packets (802.1AD) */
2736 features
&= vlan_features_check(skb
, features
);
2738 if (!skb
->encapsulation
)
2741 /* Ensure that inner L4 header offset fits into TX descriptor field */
2742 if (skb_is_gso(skb
)) {
2745 hdrlen
= skb_inner_transport_header(skb
) - skb
->data
+
2746 inner_tcp_hdrlen(skb
);
2748 if (unlikely(hdrlen
> NFP_NET_LSO_MAX_HDR_SZ
))
2749 features
&= ~NETIF_F_GSO_MASK
;
2752 /* VXLAN/GRE check */
2753 switch (vlan_get_protocol(skb
)) {
2754 case htons(ETH_P_IP
):
2755 l4_hdr
= ip_hdr(skb
)->protocol
;
2757 case htons(ETH_P_IPV6
):
2758 l4_hdr
= ipv6_hdr(skb
)->nexthdr
;
2761 return features
& ~(NETIF_F_CSUM_MASK
| NETIF_F_GSO_MASK
);
2764 if (skb
->inner_protocol_type
!= ENCAP_TYPE_ETHER
||
2765 skb
->inner_protocol
!= htons(ETH_P_TEB
) ||
2766 (l4_hdr
!= IPPROTO_UDP
&& l4_hdr
!= IPPROTO_GRE
) ||
2767 (l4_hdr
== IPPROTO_UDP
&&
2768 (skb_inner_mac_header(skb
) - skb_transport_header(skb
) !=
2769 sizeof(struct udphdr
) + sizeof(struct vxlanhdr
))))
2770 return features
& ~(NETIF_F_CSUM_MASK
| NETIF_F_GSO_MASK
);
2776 nfp_net_get_phys_port_name(struct net_device
*netdev
, char *name
, size_t len
)
2778 struct nfp_net
*nn
= netdev_priv(netdev
);
2784 if (!nn
->eth_port
->is_split
)
2785 err
= snprintf(name
, len
, "p%d", nn
->eth_port
->label_port
);
2787 err
= snprintf(name
, len
, "p%ds%d", nn
->eth_port
->label_port
,
2788 nn
->eth_port
->label_subport
);
2796 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
2797 * @nn: NFP Net device to reconfigure
2798 * @idx: Index into the port table where new port should be written
2799 * @port: UDP port to configure (pass zero to remove VXLAN port)
2801 static void nfp_net_set_vxlan_port(struct nfp_net
*nn
, int idx
, __be16 port
)
2805 nn
->vxlan_ports
[idx
] = port
;
2807 if (!(nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_VXLAN
))
2810 BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS
& 1);
2811 for (i
= 0; i
< NFP_NET_N_VXLAN_PORTS
; i
+= 2)
2812 nn_writel(nn
, NFP_NET_CFG_VXLAN_PORT
+ i
* sizeof(port
),
2813 be16_to_cpu(nn
->vxlan_ports
[i
+ 1]) << 16 |
2814 be16_to_cpu(nn
->vxlan_ports
[i
]));
2816 nfp_net_reconfig_post(nn
, NFP_NET_CFG_UPDATE_VXLAN
);
2820 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
2821 * @nn: NFP Network structure
2822 * @port: UDP port to look for
2824 * Return: if the port is already in the table -- it's position;
2825 * if the port is not in the table -- free position to use;
2826 * if the table is full -- -ENOSPC.
2828 static int nfp_net_find_vxlan_idx(struct nfp_net
*nn
, __be16 port
)
2830 int i
, free_idx
= -ENOSPC
;
2832 for (i
= 0; i
< NFP_NET_N_VXLAN_PORTS
; i
++) {
2833 if (nn
->vxlan_ports
[i
] == port
)
2835 if (!nn
->vxlan_usecnt
[i
])
2842 static void nfp_net_add_vxlan_port(struct net_device
*netdev
,
2843 struct udp_tunnel_info
*ti
)
2845 struct nfp_net
*nn
= netdev_priv(netdev
);
2848 if (ti
->type
!= UDP_TUNNEL_TYPE_VXLAN
)
2851 idx
= nfp_net_find_vxlan_idx(nn
, ti
->port
);
2855 if (!nn
->vxlan_usecnt
[idx
]++)
2856 nfp_net_set_vxlan_port(nn
, idx
, ti
->port
);
2859 static void nfp_net_del_vxlan_port(struct net_device
*netdev
,
2860 struct udp_tunnel_info
*ti
)
2862 struct nfp_net
*nn
= netdev_priv(netdev
);
2865 if (ti
->type
!= UDP_TUNNEL_TYPE_VXLAN
)
2868 idx
= nfp_net_find_vxlan_idx(nn
, ti
->port
);
2869 if (idx
== -ENOSPC
|| !nn
->vxlan_usecnt
[idx
])
2872 if (!--nn
->vxlan_usecnt
[idx
])
2873 nfp_net_set_vxlan_port(nn
, idx
, 0);
2876 static int nfp_net_xdp_offload(struct nfp_net
*nn
, struct bpf_prog
*prog
)
2878 struct tc_cls_bpf_offload cmd
= {
2883 if (!nfp_net_ebpf_capable(nn
))
2886 if (nn
->dp
.ctrl
& NFP_NET_CFG_CTRL_BPF
) {
2887 if (!nn
->dp
.bpf_offload_xdp
)
2888 return prog
? -EBUSY
: 0;
2889 cmd
.command
= prog
? TC_CLSBPF_REPLACE
: TC_CLSBPF_DESTROY
;
2893 cmd
.command
= TC_CLSBPF_ADD
;
2896 ret
= nfp_net_bpf_offload(nn
, &cmd
);
2897 /* Stop offload if replace not possible */
2898 if (ret
&& cmd
.command
== TC_CLSBPF_REPLACE
)
2899 nfp_net_xdp_offload(nn
, NULL
);
2900 nn
->dp
.bpf_offload_xdp
= prog
&& !ret
;
2904 static int nfp_net_xdp_setup(struct nfp_net
*nn
, struct bpf_prog
*prog
)
2906 struct bpf_prog
*old_prog
= nn
->dp
.xdp_prog
;
2907 struct nfp_net_dp
*dp
;
2910 if (!prog
&& !nn
->dp
.xdp_prog
)
2912 if (prog
&& nn
->dp
.xdp_prog
) {
2913 prog
= xchg(&nn
->dp
.xdp_prog
, prog
);
2915 nfp_net_xdp_offload(nn
, nn
->dp
.xdp_prog
);
2919 dp
= nfp_net_clone_dp(nn
);
2923 dp
->xdp_prog
= prog
;
2924 dp
->num_tx_rings
+= prog
? nn
->dp
.num_rx_rings
: -nn
->dp
.num_rx_rings
;
2925 dp
->rx_dma_dir
= prog
? DMA_BIDIRECTIONAL
: DMA_FROM_DEVICE
;
2927 dp
->rx_dma_off
= XDP_PACKET_HEADROOM
-
2928 (nn
->dp
.rx_offset
?: NFP_NET_MAX_PREPEND
);
2932 /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
2933 err
= nfp_net_ring_reconfig(nn
, dp
);
2938 bpf_prog_put(old_prog
);
2940 nfp_net_xdp_offload(nn
, nn
->dp
.xdp_prog
);
2945 static int nfp_net_xdp(struct net_device
*netdev
, struct netdev_xdp
*xdp
)
2947 struct nfp_net
*nn
= netdev_priv(netdev
);
2949 switch (xdp
->command
) {
2950 case XDP_SETUP_PROG
:
2951 return nfp_net_xdp_setup(nn
, xdp
->prog
);
2952 case XDP_QUERY_PROG
:
2953 xdp
->prog_attached
= !!nn
->dp
.xdp_prog
;
2960 static const struct net_device_ops nfp_net_netdev_ops
= {
2961 .ndo_open
= nfp_net_netdev_open
,
2962 .ndo_stop
= nfp_net_netdev_close
,
2963 .ndo_start_xmit
= nfp_net_tx
,
2964 .ndo_get_stats64
= nfp_net_stat64
,
2965 .ndo_setup_tc
= nfp_net_setup_tc
,
2966 .ndo_tx_timeout
= nfp_net_tx_timeout
,
2967 .ndo_set_rx_mode
= nfp_net_set_rx_mode
,
2968 .ndo_change_mtu
= nfp_net_change_mtu
,
2969 .ndo_set_mac_address
= eth_mac_addr
,
2970 .ndo_set_features
= nfp_net_set_features
,
2971 .ndo_features_check
= nfp_net_features_check
,
2972 .ndo_get_phys_port_name
= nfp_net_get_phys_port_name
,
2973 .ndo_udp_tunnel_add
= nfp_net_add_vxlan_port
,
2974 .ndo_udp_tunnel_del
= nfp_net_del_vxlan_port
,
2975 .ndo_xdp
= nfp_net_xdp
,
2979 * nfp_net_info() - Print general info about the NIC
2980 * @nn: NFP Net device to reconfigure
2982 void nfp_net_info(struct nfp_net
*nn
)
2984 nn_info(nn
, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
2985 nn
->dp
.is_vf
? "VF " : "",
2986 nn
->dp
.num_tx_rings
, nn
->max_tx_rings
,
2987 nn
->dp
.num_rx_rings
, nn
->max_rx_rings
);
2988 nn_info(nn
, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
2989 nn
->fw_ver
.resv
, nn
->fw_ver
.class,
2990 nn
->fw_ver
.major
, nn
->fw_ver
.minor
,
2992 nn_info(nn
, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2994 nn
->cap
& NFP_NET_CFG_CTRL_PROMISC
? "PROMISC " : "",
2995 nn
->cap
& NFP_NET_CFG_CTRL_L2BC
? "L2BCFILT " : "",
2996 nn
->cap
& NFP_NET_CFG_CTRL_L2MC
? "L2MCFILT " : "",
2997 nn
->cap
& NFP_NET_CFG_CTRL_RXCSUM
? "RXCSUM " : "",
2998 nn
->cap
& NFP_NET_CFG_CTRL_TXCSUM
? "TXCSUM " : "",
2999 nn
->cap
& NFP_NET_CFG_CTRL_RXVLAN
? "RXVLAN " : "",
3000 nn
->cap
& NFP_NET_CFG_CTRL_TXVLAN
? "TXVLAN " : "",
3001 nn
->cap
& NFP_NET_CFG_CTRL_SCATTER
? "SCATTER " : "",
3002 nn
->cap
& NFP_NET_CFG_CTRL_GATHER
? "GATHER " : "",
3003 nn
->cap
& NFP_NET_CFG_CTRL_LSO
? "TSO " : "",
3004 nn
->cap
& NFP_NET_CFG_CTRL_RSS
? "RSS " : "",
3005 nn
->cap
& NFP_NET_CFG_CTRL_L2SWITCH
? "L2SWITCH " : "",
3006 nn
->cap
& NFP_NET_CFG_CTRL_MSIXAUTO
? "AUTOMASK " : "",
3007 nn
->cap
& NFP_NET_CFG_CTRL_IRQMOD
? "IRQMOD " : "",
3008 nn
->cap
& NFP_NET_CFG_CTRL_VXLAN
? "VXLAN " : "",
3009 nn
->cap
& NFP_NET_CFG_CTRL_NVGRE
? "NVGRE " : "",
3010 nfp_net_ebpf_capable(nn
) ? "BPF " : "");
3014 * nfp_net_netdev_alloc() - Allocate netdev and related structure
3016 * @max_tx_rings: Maximum number of TX rings supported by device
3017 * @max_rx_rings: Maximum number of RX rings supported by device
3019 * This function allocates a netdev device and fills in the initial
3020 * part of the @struct nfp_net structure.
3022 * Return: NFP Net device structure, or ERR_PTR on error.
3024 struct nfp_net
*nfp_net_netdev_alloc(struct pci_dev
*pdev
,
3025 unsigned int max_tx_rings
,
3026 unsigned int max_rx_rings
)
3028 struct net_device
*netdev
;
3031 netdev
= alloc_etherdev_mqs(sizeof(struct nfp_net
),
3032 max_tx_rings
, max_rx_rings
);
3034 return ERR_PTR(-ENOMEM
);
3036 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
3037 nn
= netdev_priv(netdev
);
3039 nn
->dp
.netdev
= netdev
;
3040 nn
->dp
.dev
= &pdev
->dev
;
3043 nn
->max_tx_rings
= max_tx_rings
;
3044 nn
->max_rx_rings
= max_rx_rings
;
3046 nn
->dp
.num_tx_rings
= min_t(unsigned int,
3047 max_tx_rings
, num_online_cpus());
3048 nn
->dp
.num_rx_rings
= min_t(unsigned int, max_rx_rings
,
3049 netif_get_num_default_rss_queues());
3051 nn
->dp
.num_r_vecs
= max(nn
->dp
.num_tx_rings
, nn
->dp
.num_rx_rings
);
3052 nn
->dp
.num_r_vecs
= min_t(unsigned int,
3053 nn
->dp
.num_r_vecs
, num_online_cpus());
3055 nn
->dp
.txd_cnt
= NFP_NET_TX_DESCS_DEFAULT
;
3056 nn
->dp
.rxd_cnt
= NFP_NET_RX_DESCS_DEFAULT
;
3058 spin_lock_init(&nn
->reconfig_lock
);
3059 spin_lock_init(&nn
->rx_filter_lock
);
3060 spin_lock_init(&nn
->link_status_lock
);
3062 setup_timer(&nn
->reconfig_timer
,
3063 nfp_net_reconfig_timer
, (unsigned long)nn
);
3064 setup_timer(&nn
->rx_filter_stats_timer
,
3065 nfp_net_filter_stats_timer
, (unsigned long)nn
);
3071 * nfp_net_netdev_free() - Undo what @nfp_net_netdev_alloc() did
3072 * @nn: NFP Net device to reconfigure
3074 void nfp_net_netdev_free(struct nfp_net
*nn
)
3076 free_netdev(nn
->dp
.netdev
);
3080 * nfp_net_rss_key_sz() - Get current size of the RSS key
3081 * @nn: NFP Net device instance
3083 * Return: size of the RSS key for currently selected hash function.
3085 unsigned int nfp_net_rss_key_sz(struct nfp_net
*nn
)
3087 switch (nn
->rss_hfunc
) {
3088 case ETH_RSS_HASH_TOP
:
3089 return NFP_NET_CFG_RSS_KEY_SZ
;
3090 case ETH_RSS_HASH_XOR
:
3092 case ETH_RSS_HASH_CRC32
:
3096 nn_warn(nn
, "Unknown hash function: %u\n", nn
->rss_hfunc
);
3101 * nfp_net_rss_init() - Set the initial RSS parameters
3102 * @nn: NFP Net device to reconfigure
3104 static void nfp_net_rss_init(struct nfp_net
*nn
)
3106 unsigned long func_bit
, rss_cap_hfunc
;
3109 /* Read the RSS function capability and select first supported func */
3110 reg
= nn_readl(nn
, NFP_NET_CFG_RSS_CAP
);
3111 rss_cap_hfunc
= FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC
, reg
);
3113 rss_cap_hfunc
= FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC
,
3114 NFP_NET_CFG_RSS_TOEPLITZ
);
3116 func_bit
= find_first_bit(&rss_cap_hfunc
, NFP_NET_CFG_RSS_HFUNCS
);
3117 if (func_bit
== NFP_NET_CFG_RSS_HFUNCS
) {
3118 dev_warn(nn
->dp
.dev
,
3119 "Bad RSS config, defaulting to Toeplitz hash\n");
3120 func_bit
= ETH_RSS_HASH_TOP_BIT
;
3122 nn
->rss_hfunc
= 1 << func_bit
;
3124 netdev_rss_key_fill(nn
->rss_key
, nfp_net_rss_key_sz(nn
));
3126 nfp_net_rss_init_itbl(nn
);
3128 /* Enable IPv4/IPv6 TCP by default */
3129 nn
->rss_cfg
= NFP_NET_CFG_RSS_IPV4_TCP
|
3130 NFP_NET_CFG_RSS_IPV6_TCP
|
3131 FIELD_PREP(NFP_NET_CFG_RSS_HFUNC
, nn
->rss_hfunc
) |
3132 NFP_NET_CFG_RSS_MASK
;
3136 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3137 * @nn: NFP Net device to reconfigure
3139 static void nfp_net_irqmod_init(struct nfp_net
*nn
)
3141 nn
->rx_coalesce_usecs
= 50;
3142 nn
->rx_coalesce_max_frames
= 64;
3143 nn
->tx_coalesce_usecs
= 50;
3144 nn
->tx_coalesce_max_frames
= 64;
3148 * nfp_net_netdev_init() - Initialise/finalise the netdev structure
3149 * @netdev: netdev structure
3151 * Return: 0 on success or negative errno on error.
3153 int nfp_net_netdev_init(struct net_device
*netdev
)
3155 struct nfp_net
*nn
= netdev_priv(netdev
);
3158 /* XDP calls for 256 byte packet headroom which wouldn't fit in a u8.
3159 * We, however, reuse the metadata prepend space for XDP buffers which
3160 * is at least 1 byte long and as long as XDP headroom doesn't increase
3161 * above 256 the *extra* XDP headroom will fit on 8 bits.
3163 BUILD_BUG_ON(XDP_PACKET_HEADROOM
> 256);
3165 nn
->dp
.chained_metadata_format
= nn
->fw_ver
.major
> 3;
3167 nn
->dp
.rx_dma_dir
= DMA_FROM_DEVICE
;
3169 /* Get some of the read-only fields from the BAR */
3170 nn
->cap
= nn_readl(nn
, NFP_NET_CFG_CAP
);
3171 nn
->max_mtu
= nn_readl(nn
, NFP_NET_CFG_MAX_MTU
);
3173 nfp_net_write_mac_addr(nn
);
3175 /* Determine RX packet/metadata boundary offset */
3176 if (nn
->fw_ver
.major
>= 2) {
3179 reg
= nn_readl(nn
, NFP_NET_CFG_RX_OFFSET
);
3180 if (reg
> NFP_NET_MAX_PREPEND
) {
3181 nn_err(nn
, "Invalid rx offset: %d\n", reg
);
3184 nn
->dp
.rx_offset
= reg
;
3186 nn
->dp
.rx_offset
= NFP_NET_RX_OFFSET
;
3189 /* Set default MTU and Freelist buffer size */
3190 if (nn
->max_mtu
< NFP_NET_DEFAULT_MTU
)
3191 netdev
->mtu
= nn
->max_mtu
;
3193 netdev
->mtu
= NFP_NET_DEFAULT_MTU
;
3194 nn
->dp
.mtu
= netdev
->mtu
;
3195 nn
->dp
.fl_bufsz
= nfp_net_calc_fl_bufsz(&nn
->dp
);
3197 /* Advertise/enable offloads based on capabilities
3199 * Note: netdev->features show the currently enabled features
3200 * and netdev->hw_features advertises which features are
3201 * supported. By default we enable most features.
3203 netdev
->hw_features
= NETIF_F_HIGHDMA
;
3204 if (nn
->cap
& NFP_NET_CFG_CTRL_RXCSUM
) {
3205 netdev
->hw_features
|= NETIF_F_RXCSUM
;
3206 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_RXCSUM
;
3208 if (nn
->cap
& NFP_NET_CFG_CTRL_TXCSUM
) {
3209 netdev
->hw_features
|= NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
3210 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_TXCSUM
;
3212 if (nn
->cap
& NFP_NET_CFG_CTRL_GATHER
) {
3213 netdev
->hw_features
|= NETIF_F_SG
;
3214 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_GATHER
;
3216 if ((nn
->cap
& NFP_NET_CFG_CTRL_LSO
) && nn
->fw_ver
.major
> 2) {
3217 netdev
->hw_features
|= NETIF_F_TSO
| NETIF_F_TSO6
;
3218 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_LSO
;
3220 if (nn
->cap
& NFP_NET_CFG_CTRL_RSS
) {
3221 netdev
->hw_features
|= NETIF_F_RXHASH
;
3222 nfp_net_rss_init(nn
);
3223 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_RSS
;
3225 if (nn
->cap
& NFP_NET_CFG_CTRL_VXLAN
&&
3226 nn
->cap
& NFP_NET_CFG_CTRL_NVGRE
) {
3227 if (nn
->cap
& NFP_NET_CFG_CTRL_LSO
)
3228 netdev
->hw_features
|= NETIF_F_GSO_GRE
|
3229 NETIF_F_GSO_UDP_TUNNEL
;
3230 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_VXLAN
| NFP_NET_CFG_CTRL_NVGRE
;
3232 netdev
->hw_enc_features
= netdev
->hw_features
;
3235 netdev
->vlan_features
= netdev
->hw_features
;
3237 if (nn
->cap
& NFP_NET_CFG_CTRL_RXVLAN
) {
3238 netdev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_RX
;
3239 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_RXVLAN
;
3241 if (nn
->cap
& NFP_NET_CFG_CTRL_TXVLAN
) {
3242 netdev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_TX
;
3243 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_TXVLAN
;
3246 netdev
->features
= netdev
->hw_features
;
3248 if (nfp_net_ebpf_capable(nn
))
3249 netdev
->hw_features
|= NETIF_F_HW_TC
;
3251 /* Advertise but disable TSO by default. */
3252 netdev
->features
&= ~(NETIF_F_TSO
| NETIF_F_TSO6
);
3254 /* Allow L2 Broadcast and Multicast through by default, if supported */
3255 if (nn
->cap
& NFP_NET_CFG_CTRL_L2BC
)
3256 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_L2BC
;
3257 if (nn
->cap
& NFP_NET_CFG_CTRL_L2MC
)
3258 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_L2MC
;
3260 /* Allow IRQ moderation, if supported */
3261 if (nn
->cap
& NFP_NET_CFG_CTRL_IRQMOD
) {
3262 nfp_net_irqmod_init(nn
);
3263 nn
->dp
.ctrl
|= NFP_NET_CFG_CTRL_IRQMOD
;
3266 /* Stash the re-configuration queue away. First odd queue in TX Bar */
3267 nn
->qcp_cfg
= nn
->tx_bar
+ NFP_QCP_QUEUE_ADDR_SZ
;
3269 /* Make sure the FW knows the netdev is supposed to be disabled here */
3270 nn_writel(nn
, NFP_NET_CFG_CTRL
, 0);
3271 nn_writeq(nn
, NFP_NET_CFG_TXRS_ENABLE
, 0);
3272 nn_writeq(nn
, NFP_NET_CFG_RXRS_ENABLE
, 0);
3273 err
= nfp_net_reconfig(nn
, NFP_NET_CFG_UPDATE_RING
|
3274 NFP_NET_CFG_UPDATE_GEN
);
3278 /* Finalise the netdev setup */
3279 netdev
->netdev_ops
= &nfp_net_netdev_ops
;
3280 netdev
->watchdog_timeo
= msecs_to_jiffies(5 * 1000);
3282 /* MTU range: 68 - hw-specific max */
3283 netdev
->min_mtu
= ETH_MIN_MTU
;
3284 netdev
->max_mtu
= nn
->max_mtu
;
3286 netif_carrier_off(netdev
);
3288 nfp_net_set_ethtool_ops(netdev
);
3289 nfp_net_vecs_init(netdev
);
3291 return register_netdev(netdev
);
3295 * nfp_net_netdev_clean() - Undo what nfp_net_netdev_init() did.
3296 * @netdev: netdev structure
3298 void nfp_net_netdev_clean(struct net_device
*netdev
)
3300 struct nfp_net
*nn
= netdev_priv(netdev
);
3302 if (nn
->dp
.xdp_prog
)
3303 bpf_prog_put(nn
->dp
.xdp_prog
);
3304 if (nn
->dp
.bpf_offload_xdp
)
3305 nfp_net_xdp_offload(nn
, NULL
);
3306 unregister_netdev(nn
->dp
.netdev
);