Skip to contentSkip to conversation

net: macb: Add XDP support and page pool integration

PATCH Superseded Paolo Valerio 2 participants 11 messages Mar 9 – Mar 11

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover net: macb: Add XDP support and page pool integration

Tested on Raspberry Pi 5.
With rpi5 is not possible to test the !RSC case. It would be nice to test it, as it potentially changes the offset of the first frame (hw shifts of NET_IP_ALIGN bytes on rx).

All the changes are intended for gem only.

The series consists of two main changes:

- Migration from netdev_alloc_skb() to page pool allocation model, enabling skb recycling.
This also adds support for multi-descriptor frame reception, removing the previous single-descriptor approach and avoiding potentially large contiguous allocations for e.g. jumbo frames with CONFIG_PAGE_SIZE_4KB.

- XDP support: Initial XDP implementation supporting major verdicts (XDP_PASS, XDP_DROP, XDP_REDIRECT, XDP_TX) along with the ndo_xdp_xmit function for packet redirection.

The driver now advertises NETDEV_XDP_ACT_BASIC, NETDEV_XDP_ACT_REDIRECT, NETDEV_XDP_ACT_NDO_XMIT capabilities.

Some numbers (Andrew)
=====================

Initially reported numbers (Mbps, w/ drops on the rx side):

https://lore.kernel.org/netdev/[email protected]/

The numbers are essentially always the same with these two packet sizes. cpu0 doesn't appear overloaded. Not sure if there's a bottlneck at the interface level on my hw, but it seems cpu can handle more.

Second test run (with no drops on the rx side):

tx side: iperf3 sending UDP (256B) packets roughly at line rate (no drops on the receiving side).

rx side:
- fixed cpu frequency
- single rxq on cpu 0
- iperf3 server on cpu 1-3
- average w/ mpstat 20s interval 20s count

Baseline:

  Average:     CPU    %usr    %sys    %soft  %idle
  Average:     all    3.64   15.05     0.87  80.42
  Average:       0    0.05    0.15    10.23  89.56
  Average:       1    4.20   16.98     0.00  78.81
  Average:       2    3.67   15.53     0.00  80.79
  Average:       3    4.07   16.83     0.00  79.09

Page pool:

  Average:     CPU    %usr    %sys    %soft  %idle
  Average:     all    3.76   14.91     0.74  80.57
  Average:       0    0.05    0.12     7.81  91.98
  Average:       1    3.88   15.53     0.00  80.58
  Average:       2    4.35   17.23     0.00  78.42
  Average:       3    4.22   16.63     0.00  79.11

Previous versions
=================

- v3: https://lore.kernel.org/netdev/[email protected]/
- v2: https://lore.kernel.org/netdev/[email protected]/
- v1: https://lore.kernel.org/netdev/[email protected]/
- RFC v2: https://lore.kernel.org/netdev/[email protected]/
- RFC v1: https://lore.kernel.org/netdev/[email protected]/

v3 -> v4
========
- Remove inline from gem_rx_data_len() (Paolo Abeni)
- Rate limit the netdev_{err,warn} in gem_rx() and gem_rx_refill() with to avoid log flooding under error conditions (Paolo Abeni)
- Fix missing newline in netdev_err() for MTU validation in ndo_change_mtu (Paolo Abeni)

v2 -> v3
========
- Drop mog_init_rings() call in macb_open() reintroduced by mistake while rebasing (Jakub Kicinski)
- Move [5/8] down in the series (was [6/8])
- s/void/sk_buff for the skb parameter instead of updating the documentation because of its void type as this seems to better fit the scope of the patch (Jakub Kicinski)
- Move [6/8] down in the series (was [7/8])
- Remove misleading check on tx buff type (MACB_TYPE_SKB) to improve clarity as all buffers are MACB_TYPE_SKB (Jakub Kicinski)
- Update commit message to better fit with the patch content
- Move [7/8] up in the series (was [5/8]) as XDP_TX has now been folded in this patch honoring the advertised features (Jakub Kicinski)
- explicitly return 0 in gem_create_page_pool() instead of err (Jakub Kicinski)
- Revisit gem_xdp_valid_mtu() and related helpers lifting the dependency with

    bp->rx_headroom. It now always uses XDP_PACKET_HEADROOM (Jakub Kicinski)

- Use xdp_return_frame() in workqueue context (Jakub Kicinski)
- macb_tx_error_task() now locks tx_ptr_lock (Jakub Kicinski)
- Remove redundant check for clarity (Jakub Kicinski)
- Take into account head adjustment in macb_xdp_submit_frame() while syncing in the XDP_TX path and for the XDP_PASS codepath.
- Some helpers renaming for clarity
- Do not to leave bp->prog pointer in gem_setup_xdp dangling on failure
- Revisit rx_buffer_size to better handle the RSC/non-RSC case
- Return error in ndo_xdp_xmit if the interface is not operating
- Handle packets count in macb_tx_complete() for the xdp case

v1 -> v2
========
- Fix potential NULL pointer dereference in gem_rx() when receiving an unexpected non-starting frame without an active skb (Jakub Kicinski).
- Make sure to release both the pending skb and the current buff in fragment overflow error path (Jakub Kicinski).
- Reuse the existing page pool instead of creating a new one in HRESP error recovery (Jakub Kicinski).
- s/page/buffer/ in netdev_err() print out (Théo Lebrun)
- Ensure pending queue->skb is released in gem_free_rx_buffers() (Théo Lebrun).
- Use NET_SKB_PAD for the headroom when no xdp program is attached
- Add netdev to pp_params (for pp stats)
- Move ip_summed handling when RX_EOF is set as RX_SUM may not be reliable when RX_EOF is not set.
- Sync for device no longer relies on PP_FLAG_DMA_SYNC_DEV reducing the sync size.
- Introduce rx_ip_align field in struct macb
- Fix incorrect DMA direction for page pool when attaching XDP program by restoring close/open sequence as in a previous version (Jakub Kicinski).
- Rename release_buff() to macb_tx_release_buff() for clarity (Théo Lebrun).
- Do not unmap dma for XDP_TX buffers by ensuring tx_buff->mapping is zero for MACB_TYPE_XDP_TX buff_type (Jakub Kicinski).
- Always return memory to pool on xdp_convert_buff_to_frame() failure (Jakub Kicinski).
- Remove skip_xdp label (Théo, this is a bit different from your suggestion, let me know if you have a strong preference here)

RFC v2 -> v1
============
- Removed bp->macbgem_ops.mog_init_rings(bp) call from macb_open()
- Fixed includes (remove unneeded, moved one from header to macb_main.c)
- Reverse xmas tree ordering (gem_rx, gem_rx_refill)
- print_hex_dump_debug() instead of print_hex_dump()
- Replaced rx frame length check with MACB_BIT(RX_EOF) for data_len calculation
- Removed NET_IP_ALIGN handling in rx buffer size calculation
- Updated debug format string to include rx_headroom and total size
- Changed types to unsigned int in helper functions and variable
- Removed unneeded line break

RFC v1 -> RFC v2
================
- Squashed 1/6 and 2/6
- Reworked rx_buffer_size computation. It no longer takes into
accounts extra room.
- A bunch of renaming (rx_offset => rx_headroom, removed MACB_MAX_PAD, MACB_PP_HEADROOM => XDP_PACKET_HEADROOM, data => ptr, xdp_q => xdp_rxq, macb_xdp() => gem_xdp(), macb_xdp_xmit() => gem_xdp_xmit())
- Deduplicated buffer size computation in gem_xdp_valid_mtu()
and gem_xdp_setup()
- gem_xdp_setup() no longer close()/open()
- Renaming from rx_skbuff to rx_buff is now got split in a separate commit
- Open-coded gem_page_pool_get_buff()
- Added missing rx_buff re-initialization in the error path during rx
- Page pool creation failure now fails the device open
- Moved xdp buff preparation inside gem_xdp_run()
- Added missing rcu_access_pointer()
- Turned return value in -EOPNOTSUPP for macb_xdp() on failure
- moved tx_skb to tx_buff renaming to a separate commit
- Removed some unneeded code and set MACB_TYPE_SKB for lp->rm9200_txq[desc].type as well
- Replaced !!addr with a dedicated bool in macb_xdp_submit_frame()

Paolo Valerio (7):
net: macb: rename rx_skbuff into rx_buff
net: macb: Add page pool support handle multi-descriptor frame rx
net: macb: use the current queue number for stats
net: macb: make macb_tx_skb generic
net: macb: generalize tx buffer handling
net: macb: add XDP support for gem
net: macb: introduce ndo_xdp_xmit support

Théo Lebrun (1):
net: macb: move Rx buffers alloc from link up to open

 drivers/net/ethernet/cadence/Kconfig     |   1 +
 drivers/net/ethernet/cadence/macb.h      |  43 +-
 drivers/net/ethernet/cadence/macb_main.c | 931 ++++++++++++++++++-----
 3 files changed, 758 insertions(+), 217 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/8 net: macb: move Rx buffers alloc from link up to open

From: Théo Lebrun <[email protected]>

mog_alloc_rx_buffers(), getting called at open, does not do rx buffer alloc on GEM. The bulk of the work is done by gem_rx_refill() filling up all slots with valid buffers.

gem_rx_refill() is called at link up by gem_init_rings() == bp->macbgem_ops.mog_init_rings().

Move operation to macb_open(), mostly to allow it to fail early and loudly rather than init the device with Rx mostly broken.

About bool fail_early:
- When called from macb_open(), ring init fails as soon as a queue cannot be refilled.
- When called from macb_hresp_error_task(), we do our best to reinit the device: we still iterate over all queues and try refilling all even if a previous queue failed.

---
 drivers/net/ethernet/cadence/macb.h      |  2 +-
 drivers/net/ethernet/cadence/macb_main.c | 35 ++++++++++++++++++------
 2 files changed, 28 insertions(+), 9 deletions(-)
drivers/net/ethernet/cadence/macb.h +1 -1
index c69828b27dae..0acc188fe547 100644
@@ -1195,7 +1195,7 @@ struct macb_queue;
 struct macb_or_gem_ops {
 	int	(*mog_alloc_rx_buffers)(struct macb *bp);
 	void	(*mog_free_rx_buffers)(struct macb *bp);
-	void	(*mog_init_rings)(struct macb *bp);
+	int	(*mog_init_rings)(struct macb *bp, bool fail_early);
 	int	(*mog_rx)(struct macb_queue *queue, struct napi_struct *napi,
 			  int budget);
 };
drivers/net/ethernet/cadence/macb_main.c +27 -8
index 3dcae4d5f74c..d59588ef2f3f 100644
@@ -1382,13 +1382,14 @@ static int macb_tx_complete(struct macb_queue *queue, int budget)
 	return packets;
 }
 
-static void gem_rx_refill(struct macb_queue *queue)
+static int gem_rx_refill(struct macb_queue *queue)
 {
 	unsigned int		entry;
 	struct sk_buff		*skb;
 	dma_addr_t		paddr;
 	struct macb *bp = queue->bp;
 	struct macb_dma_desc *desc;
+	int err = 0;
 
 	while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
 			bp->rx_ring_size) > 0) {
@@ -1405,6 +1406,7 @@ static void gem_rx_refill(struct macb_queue *queue)
 			if (unlikely(!skb)) {
 				netdev_err(bp->dev,
 					   "Unable to allocate sk_buff\n");
+				err = -ENOMEM;
 				break;
 			}
 
@@ -1454,6 +1456,7 @@ static void gem_rx_refill(struct macb_queue *queue)
 
 	netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
 			queue, queue->rx_prepared_head, queue->rx_tail);
+	return err;
 }
 
 /* Mark DMA descriptors from begin up to and not including end as unused */
@@ -1906,7 +1909,7 @@ static void macb_hresp_error_task(struct work_struct *work)
 	netif_tx_stop_all_queues(dev);
 	netif_carrier_off(dev);
 
-	bp->macbgem_ops.mog_init_rings(bp);
+	bp->macbgem_ops.mog_init_rings(bp, false);
 
 	/* Initialize TX and RX buffers */
 	macb_init_buffers(bp);
@@ -2680,8 +2683,6 @@ static int macb_alloc_consistent(struct macb *bp)
 		if (!queue->tx_skb)
 			goto out_err;
 	}
-	if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
-		goto out_err;
 
 	/* Required for tie off descriptor for PM cases */
 	if (!(bp->caps & MACB_CAPS_QUEUE_DISABLE)) {
@@ -2693,6 +2694,11 @@ static int macb_alloc_consistent(struct macb *bp)
 			goto out_err;
 	}
 
+	if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
+		goto out_err;
+	if (bp->macbgem_ops.mog_init_rings(bp, true))
+		goto out_err;
+
 	return 0;
 
 out_err:
@@ -2713,11 +2719,13 @@ static void macb_init_tieoff(struct macb *bp)
 	desc->ctrl = 0;
 }
 
-static void gem_init_rings(struct macb *bp)
+static int gem_init_rings(struct macb *bp, bool fail_early)
 {
 	struct macb_queue *queue;
 	struct macb_dma_desc *desc = NULL;
+	int last_err = 0;
 	unsigned int q;
+	int err;
 	int i;
 
 	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
@@ -2733,13 +2741,24 @@ static void gem_init_rings(struct macb *bp)
 		queue->rx_tail = 0;
 		queue->rx_prepared_head = 0;
 
-		gem_rx_refill(queue);
+		/* We get called in two cases:
+		 *  - open: we can propagate alloc errors (so fail early),
+		 *  - HRESP error: cannot propagate, we attempt to reinit
+		 *    all queues in case of failure.
+		 */
+		err = gem_rx_refill(queue);
+		if (err) {
+			last_err = err;
+			if (fail_early)
+				break;
+		}
 	}
 
 	macb_init_tieoff(bp);
+	return last_err;
 }
 
-static void macb_init_rings(struct macb *bp)
+static int macb_init_rings(struct macb *bp, bool fail_early)
 {
 	int i;
 	struct macb_dma_desc *desc = NULL;
@@ -2756,6 +2775,7 @@ static void macb_init_rings(struct macb *bp)
 	desc->ctrl |= MACB_BIT(TX_WRAP);
 
 	macb_init_tieoff(bp);
+	return 0;
 }
 
 static void macb_reset_hw(struct macb *bp)
@@ -3087,7 +3107,6 @@ static int macb_open(struct net_device *dev)
 		goto pm_exit;
 	}
 
-	bp->macbgem_ops.mog_init_rings(bp);
 	macb_init_buffers(bp);
 
 	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Signature · 1 line
2.53.0
#2 lore raw

PATCH v4 2/8 net: macb: rename rx_skbuff into rx_buff

In reply to Paolo Valerio

This is a preparation commit as the field in later patches will no longer accomomdate skbuffs, but more generic frames.

---
 drivers/net/ethernet/cadence/macb.h      |  2 +-
 drivers/net/ethernet/cadence/macb_main.c | 24 ++++++++++++------------
 2 files changed, 13 insertions(+), 13 deletions(-)
drivers/net/ethernet/cadence/macb.h +1 -1
index 0acc188fe547..d84ec528c12f 100644
@@ -1277,7 +1277,7 @@ struct macb_queue {
 	unsigned int		rx_tail;
 	unsigned int		rx_prepared_head;
 	struct macb_dma_desc	*rx_ring;
-	struct sk_buff		**rx_skbuff;
+	void			**rx_buff;
 	void			*rx_buffers;
 	struct napi_struct	napi_rx;
 	struct queue_stats stats;
drivers/net/ethernet/cadence/macb_main.c +12 -12
index d59588ef2f3f..dead0283512d 100644
@@ -1400,7 +1400,7 @@ static int gem_rx_refill(struct macb_queue *queue)
 
 		desc = macb_rx_desc(queue, entry);
 
-		if (!queue->rx_skbuff[entry]) {
+		if (!queue->rx_buff[entry]) {
 			/* allocate sk_buff for this free entry in ring */
 			skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
 			if (unlikely(!skb)) {
@@ -1419,7 +1419,7 @@ static int gem_rx_refill(struct macb_queue *queue)
 				break;
 			}
 
-			queue->rx_skbuff[entry] = skb;
+			queue->rx_buff[entry] = skb;
 
 			if (entry == bp->rx_ring_size - 1)
 				paddr |= MACB_BIT(RX_WRAP);
@@ -1522,7 +1522,7 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
 			queue->stats.rx_dropped++;
 			break;
 		}
-		skb = queue->rx_skbuff[entry];
+		skb = queue->rx_buff[entry];
 		if (unlikely(!skb)) {
 			netdev_err(bp->dev,
 				   "inconsistent Rx descriptor chain\n");
@@ -1531,7 +1531,7 @@ static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
 			break;
 		}
 		/* now everything is ready for receiving packet */
-		queue->rx_skbuff[entry] = NULL;
+		queue->rx_buff[entry] = NULL;
 		len = ctrl & bp->rx_frm_len_mask;
 
 		netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
@@ -2531,11 +2531,11 @@ static void gem_free_rx_buffers(struct macb *bp)
 	int i;
 
 	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
-		if (!queue->rx_skbuff)
+		if (!queue->rx_buff)
 			continue;
 
 		for (i = 0; i < bp->rx_ring_size; i++) {
-			skb = queue->rx_skbuff[i];
+			skb = queue->rx_buff[i];
 
 			if (!skb)
 				continue;
@@ -2549,8 +2549,8 @@ static void gem_free_rx_buffers(struct macb *bp)
 			skb = NULL;
 		}
 
-		kfree(queue->rx_skbuff);
-		queue->rx_skbuff = NULL;
+		kfree(queue->rx_buff);
+		queue->rx_buff = NULL;
 	}
 }
 
@@ -2613,13 +2613,13 @@ static int gem_alloc_rx_buffers(struct macb *bp)
 
 	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
 		size = bp->rx_ring_size * sizeof(struct sk_buff *);
-		queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
-		if (!queue->rx_skbuff)
+		queue->rx_buff = kzalloc(size, GFP_KERNEL);
+		if (!queue->rx_buff)
 			return -ENOMEM;
 		else
 			netdev_dbg(bp->dev,
-				   "Allocated %d RX struct sk_buff entries at %p\n",
-				   bp->rx_ring_size, queue->rx_skbuff);
+				   "Allocated %d RX buff entries at %p\n",
+				   bp->rx_ring_size, queue->rx_buff);
 	}
 	return 0;
 }
Signature · 1 line
2.53.0
#3 lore raw

PATCH v4 3/8 net: macb: Add page pool support handle multi-descriptor frame rx

In reply to Paolo Valerio

Use the page pool allocator for the data buffers and enable skb recycling support, instead of relying on netdev_alloc_skb allocating the entire skb during the refill.

The patch also add support for receiving network frames that span multiple DMA descriptors in the Cadence MACB/GEM Ethernet driver.

The patch removes the requirement that limited frame reception to a single descriptor (RX_SOF && RX_EOF), also avoiding potential contiguous multi-page allocation for large frames.

---
 drivers/net/ethernet/cadence/Kconfig     |   1 +
 drivers/net/ethernet/cadence/macb.h      |   5 +
 drivers/net/ethernet/cadence/macb_main.c | 412 ++++++++++++++++-------
 3 files changed, 292 insertions(+), 126 deletions(-)
drivers/net/ethernet/cadence/Kconfig +1 -0
index 5b2a461dfd28..ae500f717433 100644
@@ -25,6 +25,7 @@ config MACB
 	depends on PTP_1588_CLOCK_OPTIONAL
 	select PHYLINK
 	select CRC32
+	select PAGE_POOL
 	help
 	  The Cadence MACB ethernet interface is found on many Atmel AT32 and
 	  AT91 parts.  This driver also supports the Cadence GEM (Gigabit
drivers/net/ethernet/cadence/macb.h +5 -0
index d84ec528c12f..2c6ba1b63aab 100644
@@ -14,6 +14,7 @@
 #include <linux/interrupt.h>
 #include <linux/phy/phy.h>
 #include <linux/workqueue.h>
+#include <net/page_pool/helpers.h>
 
 #define MACB_GREGS_NBR 16
 #define MACB_GREGS_VERSION 2
@@ -1281,6 +1282,8 @@ struct macb_queue {
 	void			*rx_buffers;
 	struct napi_struct	napi_rx;
 	struct queue_stats stats;
+	struct page_pool	*page_pool;
+	struct sk_buff		*skb;
 };
 
 struct ethtool_rx_fs_item {
@@ -1304,6 +1307,8 @@ struct macb {
 	struct macb_dma_desc	*rx_ring_tieoff;
 	dma_addr_t		rx_ring_tieoff_dma;
 	size_t			rx_buffer_size;
+	size_t			rx_headroom;
+	unsigned int		rx_ip_align;
 
 	unsigned int		rx_ring_size;
 	unsigned int		tx_ring_size;
drivers/net/ethernet/cadence/macb_main.c +286 -126

561 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

PATCH v4 4/8 net: macb: use the current queue number for stats

In reply to Paolo Valerio

gem_get_ethtool_stats calculates the size of the statistics data to copy always considering maximum number of queues.

The patch makes sure the statistics are copied only for the active queues as returned in the string set count op.

---
 drivers/net/ethernet/cadence/macb_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb_main.c +1 -1
index e295065b293b..5bf653e5879e 100644
@@ -3436,7 +3436,7 @@ static void gem_get_ethtool_stats(struct net_device *dev,
 	spin_lock_irq(&bp->stats_lock);
 	gem_update_stats(bp);
 	memcpy(data, &bp->ethtool_stats, sizeof(u64)
-			* (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
+			* (GEM_STATS_LEN + QUEUE_STATS_LEN * bp->num_queues));
 	spin_unlock_irq(&bp->stats_lock);
 }
 
Signature · 1 line
2.53.0
#5 lore raw

PATCH v4 5/8 net: macb: make macb_tx_skb generic

In reply to Paolo Valerio

The macb_tx_skb structure is renamed to macb_tx_buff with no functional changes.

This is a preparatory step for adding xdp xmit support.

---
 drivers/net/ethernet/cadence/macb.h      |  8 +-
 drivers/net/ethernet/cadence/macb_main.c | 96 ++++++++++++------------
 2 files changed, 52 insertions(+), 52 deletions(-)
drivers/net/ethernet/cadence/macb.h +4 -4
index 2c6ba1b63aab..1cc626088174 100644
@@ -965,7 +965,7 @@ struct macb_dma_desc_ptp {
 /* Scaled PPM fraction */
 #define PPM_FRACTION	16
 
-/* struct macb_tx_skb - data about an skb which is being transmitted
+/* struct macb_tx_buff - data about an skb which is being transmitted
  * @skb: skb currently being transmitted, only set for the last buffer
  *       of the frame
  * @mapping: DMA address of the skb's fragment buffer
@@ -973,7 +973,7 @@ struct macb_dma_desc_ptp {
  * @mapped_as_page: true when buffer was mapped with skb_frag_dma_map(),
  *                  false when buffer was mapped with dma_map_single()
  */
-struct macb_tx_skb {
+struct macb_tx_buff {
 	struct sk_buff		*skb;
 	dma_addr_t		mapping;
 	size_t			size;
@@ -1267,7 +1267,7 @@ struct macb_queue {
 	spinlock_t		tx_ptr_lock;
 	unsigned int		tx_head, tx_tail;
 	struct macb_dma_desc	*tx_ring;
-	struct macb_tx_skb	*tx_skb;
+	struct macb_tx_buff	*tx_buff;
 	dma_addr_t		tx_ring_dma;
 	struct work_struct	tx_error_task;
 	bool			txubr_pending;
@@ -1345,7 +1345,7 @@ struct macb {
 	phy_interface_t		phy_interface;
 
 	/* AT91RM9200 transmit queue (1 on wire + 1 queued) */
-	struct macb_tx_skb	rm9200_txq[2];
+	struct macb_tx_buff	rm9200_txq[2];
 	unsigned int		max_tx_length;
 
 	u64			ethtool_stats[GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES];
drivers/net/ethernet/cadence/macb_main.c +48 -48

229 lines not on this page · raw message

Signature · 1 line
2.53.0
#6 lore raw

PATCH v4 6/8 net: macb: generalize tx buffer handling

In reply to Paolo Valerio

Rename struct macb_tx_buff member skb to ptr and introduce macb_tx_buff_type to identify the buffer type macb_tx_buff represents.

Currently the buffer can only be MACB_TYPE_SKB, so the sk_buff case is handled unconditionally in the tx path.
The remaining type handling will be handled by subsequent patches.

This is the last preparatory step for XDP xmit support.

---
 drivers/net/ethernet/cadence/macb.h      | 25 ++++++++-----
 drivers/net/ethernet/cadence/macb_main.c | 45 ++++++++++++++----------
 2 files changed, 43 insertions(+), 27 deletions(-)
drivers/net/ethernet/cadence/macb.h +17 -8
index 1cc626088174..d8c581394b98 100644
@@ -965,19 +965,28 @@ struct macb_dma_desc_ptp {
 /* Scaled PPM fraction */
 #define PPM_FRACTION	16
 
-/* struct macb_tx_buff - data about an skb which is being transmitted
- * @skb: skb currently being transmitted, only set for the last buffer
- *       of the frame
- * @mapping: DMA address of the skb's fragment buffer
+enum macb_tx_buff_type {
+	MACB_TYPE_SKB,
+	MACB_TYPE_XDP_TX,
+	MACB_TYPE_XDP_NDO,
+};
+
+/* struct macb_tx_buff - data about an skb or xdp frame which is being
+ * transmitted.
+ * @ptr: pointer to skb or xdp frame being transmitted, only set
+ *        for the last buffer for sk_buff
+ * @mapping: DMA address of the skb's fragment or xdp buffer
  * @size: size of the DMA mapped buffer
  * @mapped_as_page: true when buffer was mapped with skb_frag_dma_map(),
  *                  false when buffer was mapped with dma_map_single()
+ * @type: type of buffer (MACB_TYPE_SKB, MACB_TYPE_XDP_TX, MACB_TYPE_XDP_NDO)
  */
 struct macb_tx_buff {
-	struct sk_buff		*skb;
-	dma_addr_t		mapping;
-	size_t			size;
-	bool			mapped_as_page;
+	void				*ptr;
+	dma_addr_t			mapping;
+	size_t				size;
+	bool				mapped_as_page;
+	enum macb_tx_buff_type		type;
 };
 
 /* Hardware-collected statistics. Used when updating the network
drivers/net/ethernet/cadence/macb_main.c +26 -19

121 lines not on this page · raw message

Signature · 1 line
2.53.0
#7 lore raw

PATCH v4 7/8 net: macb: add XDP support for gem

In reply to Paolo Valerio

Introduce basic XDP support for macb/gem with the XDP_TX, XDP_PASS, XDP_DROP, XDP_REDIRECT verdict support.

---
 drivers/net/ethernet/cadence/macb.h      |   3 +
 drivers/net/ethernet/cadence/macb_main.c | 362 ++++++++++++++++++++---
 2 files changed, 331 insertions(+), 34 deletions(-)
drivers/net/ethernet/cadence/macb.h +3 -0
index d8c581394b98..a1cec805ee92 100644
@@ -15,6 +15,7 @@
 #include <linux/phy/phy.h>
 #include <linux/workqueue.h>
 #include <net/page_pool/helpers.h>
+#include <net/xdp.h>
 
 #define MACB_GREGS_NBR 16
 #define MACB_GREGS_VERSION 2
@@ -1293,6 +1294,7 @@ struct macb_queue {
 	struct queue_stats stats;
 	struct page_pool	*page_pool;
 	struct sk_buff		*skb;
+	struct xdp_rxq_info	xdp_rxq;
 };
 
 struct ethtool_rx_fs_item {
@@ -1398,6 +1400,7 @@ struct macb {
 
 	struct macb_pm_data pm_data;
 	const struct macb_usrio_config *usrio;
+	struct bpf_prog	__rcu *prog;
 };
 
 #ifdef CONFIG_MACB_USE_HWSTAMP
drivers/net/ethernet/cadence/macb_main.c +328 -34

604 lines not on this page · raw message

Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v4 7/8] net: macb: add XDP support for gem

Hi Paolo,

Nice series! One issue I spotted that interacts with the EEE support that's currently in net-next:

drivers/net/ethernet/cadence/macb_main.c static void discard_partial_frame(struct macb_queue *queue, unsigned int begin, · line 1637

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+	spin_lock(&bp->lock);
+	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
+	spin_unlock(&bp->lock);

macb_xdp_submit_frame() writes TSTART without first waking the MAC from LPI. The regular TX path in macb_start_xmit() calls macb_tx_lpi_wake(bp) before TSTART for this reason -- the GEM MAC has no auto-wake mechanism, so when TXLPIEN is set in NCR, TX is blocked and frames will sit in the FIFO indefinitely.

This affects XDP_TX, XDP_REDIRECT and ndo_xdp_xmit. The fix should be straightforward:

	spin_lock(&bp->lock);
	macb_tx_lpi_wake(bp);
	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
	spin_unlock(&bp->lock);

macb_tx_lpi_wake() is a no-op when EEE is not active, so there's no overhead in the non-EEE case.

Thanks,
Nicolai

#9 lore raw

Re: [PATCH v4 7/8] net: macb: add XDP support for gem

Quoted 5 lines · Nicolai Buchwitz

Hi Paolo,

Nice series! One issue I spotted that interacts with the EEE support that's currently in net-next:

Hi Nicolai,

Quoted 13 lines

drivers/net/ethernet/cadence/macb_main.c static void discard_partial_frame(struct macb_queue *queue, unsigned int begin, · line 1637

Quoted 3 lines
drivers/net/ethernet/cadence/macb_main.c +3 -0
+	spin_lock(&bp->lock);
+	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
+	spin_unlock(&bp->lock);

macb_xdp_submit_frame() writes TSTART without first waking the MAC from LPI. The regular TX path in macb_start_xmit() calls macb_tx_lpi_wake(bp) before TSTART for this reason -- the GEM MAC has no auto-wake mechanism, so when TXLPIEN is set in NCR, TX is blocked and frames will sit in the FIFO indefinitely.

This affects XDP_TX, XDP_REDIRECT and ndo_xdp_xmit. The fix should be straightforward:

Quoted 8 lines
	spin_lock(&bp->lock);
	macb_tx_lpi_wake(bp);
	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
	spin_unlock(&bp->lock);

macb_tx_lpi_wake() is a no-op when EEE is not active, so there's no overhead in the non-EEE case.

good catch! I'll make sure to add it to the next version, thanks.

#10 lore raw

PATCH v4 8/8 net: macb: introduce ndo_xdp_xmit support

In reply to Paolo Valerio

Introduce ndo_xdp_xmit function for redirection, update macb_xdp_submit_frame() accordingly, and advertise NETDEV_XDP_ACT_NDO_XMIT capability.

---
 drivers/net/ethernet/cadence/macb_main.c | 73 +++++++++++++++++++-----
 1 file changed, 58 insertions(+), 15 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +58 -15
index 5351979e21ee..838447258498 100644
@@ -1103,11 +1103,13 @@ static void macb_tx_release_buff(void *buff, enum macb_tx_buff_type type, int bu
 {
 	if (type == MACB_TYPE_SKB) {
 		napi_consume_skb(buff, budget);
-	} else {
+	} else if (type == MACB_TYPE_XDP_TX) {
 		if (!budget)
 			xdp_return_frame(buff);
 		else
 			xdp_return_frame_rx_napi(buff);
+	} else {
+		xdp_return_frame(buff);
 	}
 }
 
@@ -1565,20 +1567,24 @@ static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
 }
 
 static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf,
-				 struct net_device *dev, dma_addr_t addr)
+				 struct net_device *dev, bool dma_map,
+				 dma_addr_t addr)
 {
+	enum macb_tx_buff_type buff_type;
 	struct macb_tx_buff *tx_buff;
 	int cpu = smp_processor_id();
 	struct macb_dma_desc *desc;
 	struct macb_queue *queue;
 	unsigned int next_head;
 	unsigned long flags;
+	dma_addr_t mapping;
 	u16 queue_index;
 	int err = 0;
 	u32 ctrl;
 
 	queue_index = cpu % bp->num_queues;
 	queue = &bp->queues[queue_index];
+	buff_type = dma_map ? MACB_TYPE_XDP_NDO : MACB_TYPE_XDP_TX;
 
 	spin_lock_irqsave(&queue->tx_ptr_lock, flags);
 
@@ -1591,14 +1597,23 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf,
 		goto unlock;
 	}
 
-	/* progs can adjust the head. Sync and set the adjusted one.
-	 * This also implicitly takes into account ip alignment,
-	 * if present.
-	 */
-	addr += xdpf->headroom + sizeof(*xdpf);
-
-	dma_sync_single_for_device(&bp->pdev->dev, addr,
-				   xdpf->len, DMA_BIDIRECTIONAL);
+	if (dma_map) {
+		mapping = dma_map_single(&bp->pdev->dev,
+					 xdpf->data,
+					 xdpf->len, DMA_TO_DEVICE);
+		if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
+			err = -ENOMEM;
+			goto unlock;
+		}
+	} else {
+		/* progs can adjust the head. Sync and set the adjusted one.
+		 * This also implicitly takes into account ip alignment,
+		 * if present.
+		 */
+		mapping = addr + xdpf->headroom + sizeof(*xdpf);
+		dma_sync_single_for_device(&bp->pdev->dev, mapping,
+					   xdpf->len, DMA_BIDIRECTIONAL);
+	}
 
 	next_head = queue->tx_head + 1;
 
@@ -1609,8 +1624,8 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf,
 	desc = macb_tx_desc(queue, queue->tx_head);
 	tx_buff = macb_tx_buff(queue, queue->tx_head);
 	tx_buff->ptr = xdpf;
-	tx_buff->type = MACB_TYPE_XDP_TX;
-	tx_buff->mapping = 0;
+	tx_buff->type = buff_type;
+	tx_buff->mapping = dma_map ? mapping : 0;
 	tx_buff->size = xdpf->len;
 	tx_buff->mapped_as_page = false;
 
@@ -1621,7 +1636,7 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf,
 		ctrl |= MACB_BIT(TX_WRAP);
 
 	/* Set TX buffer descriptor */
-	macb_set_addr(bp, desc, addr);
+	macb_set_addr(bp, desc, mapping);
 	/* desc->addr must be visible to hardware before clearing
 	 * 'TX_USED' bit in desc->ctrl.
 	 */
@@ -1645,6 +1660,32 @@ static int macb_xdp_submit_frame(struct macb *bp, struct xdp_frame *xdpf,
 	return err;
 }
 
+static int gem_xdp_xmit(struct net_device *dev, int num_frame,
+			struct xdp_frame **frames, u32 flags)
+{
+	struct macb *bp = netdev_priv(dev);
+	u32 xmitted = 0;
+	int i;
+
+	if (!macb_is_gem(bp))
+		return -EOPNOTSUPP;
+
+	if (unlikely(!netif_carrier_ok(dev)))
+		return -ENETDOWN;
+
+	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
+		return -EINVAL;
+
+	for (i = 0; i < num_frame; i++) {
+		if (macb_xdp_submit_frame(bp, frames[i], dev, true, 0))
+			break;
+
+		xmitted++;
+	}
+
+	return xmitted;
+}
+
 static u32 gem_xdp_run(struct macb_queue *queue, void *buff_head,
 		       unsigned int *len, unsigned int *headroom,
 		       dma_addr_t addr)
@@ -1682,7 +1723,7 @@ static u32 gem_xdp_run(struct macb_queue *queue, void *buff_head,
 	case XDP_TX:
 		xdpf = xdp_convert_buff_to_frame(&xdp);
 		if (unlikely(!xdpf) || macb_xdp_submit_frame(queue->bp, xdpf,
-							     dev, addr)) {
+							     dev, false, addr)) {
 			act = XDP_DROP;
 			break;
 		}
@@ -4932,6 +4973,7 @@ static const struct net_device_ops macb_netdev_ops = {
 	.ndo_hwtstamp_get	= macb_hwtstamp_get,
 	.ndo_setup_tc		= macb_setup_tc,
 	.ndo_bpf		= gem_xdp,
+	.ndo_xdp_xmit		= gem_xdp_xmit,
 };
 
 /* Configure peripheral capabilities according to device tree
@@ -6236,7 +6278,8 @@ static int macb_probe(struct platform_device *pdev)
 			bp->rx_ip_align = NET_IP_ALIGN;
 
 		dev->xdp_features = NETDEV_XDP_ACT_BASIC |
-				    NETDEV_XDP_ACT_REDIRECT;
+				    NETDEV_XDP_ACT_REDIRECT |
+				    NETDEV_XDP_ACT_NDO_XMIT;
 	}
 
 	netif_carrier_off(dev);
Signature · 1 line
2.53.0