[net-next,2/2] net: stmmac: dwmac-sun8i: support ethtool rx-all

Message ID 20261122-submit-sun8i-rxall-v1-v1-2-607789772c56@gmail.com (mailing list archive)
State New
Headers
Series net: stmmac: support ethtool rx-all on sun8i |

Commit Message

James Hilliard Nov. 22, 2026, 7:45 a.m. UTC
Enable packet captures of complete frames with wire errors on sun8i.
Report these frames to the core with an unverified checksum so it
can deliver them when rx-all is enabled. DMA and incomplete-frame
errors still require dropping the frame.

Program runt forwarding at initialization and when rx-all changes.
Serialize live updates of RX_CTL1 so changing the receive mode cannot
overwrite a concurrent DMA start, stop or threshold update.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c | 73 +++++++++++++----------
 1 file changed, 41 insertions(+), 32 deletions(-)
  

Comments

Andrew Lunn Nov. 22, 2026, 6:38 p.m. UTC | #1
> +static void sun8i_dwmac_update_rx_ctl1(struct stmmac_priv *priv, u32 clear, u32 set)
>  {
> +	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
>  	u32 v;
>  
> -	v = readl(ioaddr + EMAC_RX_CTL1);
> -	v |= EMAC_RX_DMA_START;
> -	v |= EMAC_RX_DMA_EN;
> -	writel(v, ioaddr + EMAC_RX_CTL1);
> +	guard(spinlock_irqsave)(&gmac->rx_ctl1_lock);
> +	v = readl(priv->ioaddr + EMAC_RX_CTL1);
> +	writel((v & ~clear) | set, priv->ioaddr + EMAC_RX_CTL1);

Netdev does not like these magical guard constructs. scoped_guard is
acceptable, but good old fashioned lock/unlock is preferred.

It is also unclear why this now needs locks, when before it did not.

This patch is too big, does too many things. Please break it up into
smaller changes, with good commit messages, which are obviously
correct.

	Andrew
  
Andrew Lunn Nov. 22, 2026, 6:41 p.m. UTC | #2
> @@ -198,6 +195,7 @@ static const struct emac_variant emac_variant_h616_emac1 = {
>  
>  /* Used in RX_CTL1*/
>  #define EMAC_RX_MD              BIT(1)
> +#define EMAC_RX_RUNT_FRM	BIT(2)

This patch highlighted that EMAC_RX_MD is using spaces, not
tabs. Please could you add one more patch to the series fixing it.

      Thanks
	Andrew
  
James Hilliard Nov. 22, 2026, 8:05 p.m. UTC | #3
On Sun, Nov 22, 2026 at 11:39 AM Andrew Lunn <andrew@lunn.ch> wrote:
>
> > +static void sun8i_dwmac_update_rx_ctl1(struct stmmac_priv *priv, u32 clear, u32 set)
> >  {
> > +     struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
> >       u32 v;
> >
> > -     v = readl(ioaddr + EMAC_RX_CTL1);
> > -     v |= EMAC_RX_DMA_START;
> > -     v |= EMAC_RX_DMA_EN;
> > -     writel(v, ioaddr + EMAC_RX_CTL1);
> > +     guard(spinlock_irqsave)(&gmac->rx_ctl1_lock);
> > +     v = readl(priv->ioaddr + EMAC_RX_CTL1);
> > +     writel((v & ~clear) | set, priv->ioaddr + EMAC_RX_CTL1);
>
> Netdev does not like these magical guard constructs. scoped_guard is
> acceptable, but good old fashioned lock/unlock is preferred.
>
> It is also unclear why this now needs locks, when before it did not.

I added the lock because changing rx-all adds a new writer to RX_CTL1
while the interface is up. The runt-forwarding bit shares that register
with the RX DMA enable bit. If ethtool read the register just before
another CPU stopped RX DMA, writing it back with runt forwarding
enabled could also turn DMA back on.

I went back through the callers to see whether that can actually
happen. RTNL serializes the ethtool change with opening and closing the
interface, resetting it, and reconfiguring RX queues. Neither the sun8i
IRQ handler nor its TX completion path requests a DMA threshold change.
System suspend does stop RX DMA outside RTNL, but userspace is frozen
before the driver reaches that point on the normal sun8i suspend path.

So although the register is shared, I couldn't find a path where the
new writer races an existing one. I think we can drop the lock and
leave the existing DMA callbacks alone.

> This patch is too big, does too many things. Please break it up into
> smaller changes, with good commit messages, which are obviously
> correct.
>
>         Andrew
>
  
Andrew Lunn Nov. 22, 2026, 8:57 p.m. UTC | #4
On Sun, Nov 22, 2026 at 01:05:48PM -0700, James Hilliard wrote:
> On Sun, Nov 22, 2026 at 11:39 AM Andrew Lunn <andrew@lunn.ch> wrote:
> >
> > > +static void sun8i_dwmac_update_rx_ctl1(struct stmmac_priv *priv, u32 clear, u32 set)
> > >  {
> > > +     struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
> > >       u32 v;
> > >
> > > -     v = readl(ioaddr + EMAC_RX_CTL1);
> > > -     v |= EMAC_RX_DMA_START;
> > > -     v |= EMAC_RX_DMA_EN;
> > > -     writel(v, ioaddr + EMAC_RX_CTL1);
> > > +     guard(spinlock_irqsave)(&gmac->rx_ctl1_lock);
> > > +     v = readl(priv->ioaddr + EMAC_RX_CTL1);
> > > +     writel((v & ~clear) | set, priv->ioaddr + EMAC_RX_CTL1);
> >
> > Netdev does not like these magical guard constructs. scoped_guard is
> > acceptable, but good old fashioned lock/unlock is preferred.
> >
> > It is also unclear why this now needs locks, when before it did not.
> 
> I added the lock because changing rx-all adds a new writer to RX_CTL1
> while the interface is up. The runt-forwarding bit shares that register
> with the RX DMA enable bit. If ethtool read the register just before
> another CPU stopped RX DMA, writing it back with runt forwarding
> enabled could also turn DMA back on.
> 
> I went back through the callers to see whether that can actually
> happen. RTNL serializes the ethtool change with opening and closing the
> interface, resetting it, and reconfiguring RX queues. Neither the sun8i
> IRQ handler nor its TX completion path requests a DMA threshold change.
> System suspend does stop RX DMA outside RTNL, but userspace is frozen
> before the driver reaches that point on the normal sun8i suspend path.
> 
> So although the register is shared, I couldn't find a path where the
> new writer races an existing one. I think we can drop the lock and
> leave the existing DMA callbacks alone.

And this is where, small changes, with good commit messages helps
you. In order to write the commit message for a small patch adding a
new lock, you would of done this investigation in order to write the
commit message, answering the question "Why?"

I don't say this just for reviewers benefit. It also helps you get
your code correct.

     Andrew
  

Patch

diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
index 0ad2203f76c3..8398788d334e 100644
--- a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
+++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun8i.c
@@ -21,16 +21,12 @@ 
 #include <linux/pm_runtime.h>
 #include <linux/regulator/consumer.h>
 #include <linux/regmap.h>
+#include <linux/spinlock.h>
 #include <linux/stmmac.h>
 
 #include "stmmac.h"
 #include "stmmac_platform.h"
 
-/* General notes on dwmac-sun8i:
- * Locking: no locking is necessary in this file because all necessary locking
- *		is done in the "stmmac files"
- */
-
 /* struct emac_variant - Describe dwmac-sun8i hardware variant
  * @syscon_field		reg_field for the syscon's gmac register
  * @soc_has_internal_phy:	Does the MAC embed an internal PHY
@@ -73,6 +69,7 @@  struct sunxi_priv_data {
 	bool internal_phy_powered;
 	bool use_internal_phy;
 	void *mux_handle;
+	spinlock_t rx_ctl1_lock; /* Protects live RX_CTL1 updates */
 };
 
 /* EMAC clock register @ 0x30 in the "system control" address range */
@@ -198,6 +195,7 @@  static const struct emac_variant emac_variant_h616_emac1 = {
 
 /* Used in RX_CTL1*/
 #define EMAC_RX_MD              BIT(1)
+#define EMAC_RX_RUNT_FRM	BIT(2)
 #define EMAC_RX_ERR_FRM		BIT(3)
 #define EMAC_RX_TH_MASK		GENMASK(5, 4)
 #define EMAC_RX_TH_32		0
@@ -442,25 +440,32 @@  static void sun8i_dwmac_dma_stop_tx(struct stmmac_priv *priv,
 	writel(v, ioaddr + EMAC_TX_CTL1);
 }
 
-static void sun8i_dwmac_dma_start_rx(struct stmmac_priv *priv,
-				     void __iomem *ioaddr, u32 chan)
+static void sun8i_dwmac_update_rx_ctl1(struct stmmac_priv *priv, u32 clear, u32 set)
 {
+	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
 	u32 v;
 
-	v = readl(ioaddr + EMAC_RX_CTL1);
-	v |= EMAC_RX_DMA_START;
-	v |= EMAC_RX_DMA_EN;
-	writel(v, ioaddr + EMAC_RX_CTL1);
+	guard(spinlock_irqsave)(&gmac->rx_ctl1_lock);
+	v = readl(priv->ioaddr + EMAC_RX_CTL1);
+	writel((v & ~clear) | set, priv->ioaddr + EMAC_RX_CTL1);
+}
+
+static void sun8i_dwmac_dma_start_rx(struct stmmac_priv *priv,
+				     void __iomem *ioaddr, u32 chan)
+{
+	sun8i_dwmac_update_rx_ctl1(priv, 0, EMAC_RX_DMA_START | EMAC_RX_DMA_EN);
 }
 
 static void sun8i_dwmac_dma_stop_rx(struct stmmac_priv *priv,
 				    void __iomem *ioaddr, u32 chan)
 {
-	u32 v;
+	sun8i_dwmac_update_rx_ctl1(priv, EMAC_RX_DMA_EN, 0);
+}
 
-	v = readl(ioaddr + EMAC_RX_CTL1);
-	v &= ~EMAC_RX_DMA_EN;
-	writel(v, ioaddr + EMAC_RX_CTL1);
+static void sun8i_dwmac_set_rx_all(struct stmmac_priv *priv, bool enable)
+{
+	sun8i_dwmac_update_rx_ctl1(priv, EMAC_RX_RUNT_FRM,
+				   enable ? EMAC_RX_RUNT_FRM : 0);
 }
 
 static int sun8i_dwmac_dma_interrupt(struct stmmac_priv *priv,
@@ -539,24 +544,23 @@  static void sun8i_dwmac_dma_operation_mode_rx(struct stmmac_priv *priv,
 					      void __iomem *ioaddr, int mode,
 					      u32 channel, int fifosz, u8 qmode)
 {
-	u32 v;
+	u32 threshold = 0;
 
-	v = readl(ioaddr + EMAC_RX_CTL1);
 	if (mode == SF_DMA_MODE) {
-		v |= EMAC_RX_MD;
-	} else {
-		v &= ~EMAC_RX_MD;
-		v &= ~EMAC_RX_TH_MASK;
-		if (mode < 32)
-			v |= EMAC_RX_TH_32;
-		else if (mode < 64)
-			v |= EMAC_RX_TH_64;
-		else if (mode < 96)
-			v |= EMAC_RX_TH_96;
-		else if (mode < 128)
-			v |= EMAC_RX_TH_128;
+		sun8i_dwmac_update_rx_ctl1(priv, 0, EMAC_RX_MD);
+		return;
 	}
-	writel(v, ioaddr + EMAC_RX_CTL1);
+
+	if (mode < 32)
+		threshold = EMAC_RX_TH_32;
+	else if (mode < 64)
+		threshold = EMAC_RX_TH_64;
+	else if (mode < 96)
+		threshold = EMAC_RX_TH_96;
+	else if (mode < 128)
+		threshold = EMAC_RX_TH_128;
+
+	sun8i_dwmac_update_rx_ctl1(priv, EMAC_RX_MD | EMAC_RX_TH_MASK, threshold);
 }
 
 static void sun8i_dwmac_dma_operation_mode_tx(struct stmmac_priv *priv,
@@ -648,7 +652,10 @@  static void sun8i_dwmac_core_init(struct mac_device_info *hw,
 	writel(v, ioaddr + EMAC_BASIC_CTL1);
 
 	/* Forward checksum failures to software before starting RX. */
-	writel(EMAC_RX_ERR_FRM, ioaddr + EMAC_RX_CTL1);
+	v = EMAC_RX_ERR_FRM;
+	if (dev->features & NETIF_F_RXALL)
+		v |= EMAC_RX_RUNT_FRM;
+	writel(v, ioaddr + EMAC_RX_CTL1);
 }
 
 static void sun8i_dwmac_set_mac(void __iomem *ioaddr, bool enable)
@@ -1052,6 +1059,7 @@  static const struct stmmac_ops sun8i_dwmac_ops = {
 	.set_mac = sun8i_dwmac_set_mac,
 	.dump_regs = sun8i_dwmac_dump_mac_regs,
 	.rx_ipc = sun8i_dwmac_rx_ipc,
+	.set_rx_all = sun8i_dwmac_set_rx_all,
 	.set_filter = sun8i_dwmac_set_filter,
 	.flow_ctrl = sun8i_dwmac_flow_ctrl,
 	.set_umac_addr = sun8i_dwmac_set_umac_addr,
@@ -1126,7 +1134,7 @@  static int sun8i_dwmac_rx_status(struct stmmac_extra_stats *x, struct dma_desc *
 	if (unlikely(runt || (status & wire_errors))) {
 		if (runt && !(status & RDES0_LENGTH_ERROR))
 			x->rx_length++;
-		return discard_frame;
+		return rx_error_frame | csum_none;
 	}
 
 	/* Only a checked IP frame is eligible for CHECKSUM_UNNECESSARY. */
@@ -1218,6 +1226,7 @@  static int sun8i_dwmac_probe(struct platform_device *pdev)
 	if (!gmac)
 		return -ENOMEM;
 
+	spin_lock_init(&gmac->rx_ctl1_lock);
 	gmac->variant = of_device_get_match_data(&pdev->dev);
 	if (!gmac->variant) {
 		dev_err(&pdev->dev, "Missing dwmac-sun8i variant\n");