net: stmmac: fix setting RE and TE inappropriately
Subsystems (4)
cover net: stmmac: fix setting RE and TE inappropriately
Hi,
This series addresses inappropriate setting of the receive and transmit enables in the GMAC control register identified back in
https://lore.kernel.org/r/[email protected]
The databook is clear for the receive enable, that this should not be set until the initialisation of the MAC and DMA has been completed. The previous RFC patch series ("net: stmmac: fix resume failures due to RX clock") which moves phylink_resume() solves that, but we are left with these enables being set when the link is down. This is not correct.
Sadly, when XDP support was added, new calls to netif_carrier_on() and netif_carrier_off() were added, which are incorrect in drivers that make use of phylink - by doing so, the driver has no guarantee that the .mac_link_up() and .mac_link_down() methods will be called in sequence anymore. This is fixed in patch 1.
We remove manipulation of the RE and TE bits from the start_tx(), stop_tx(), start_rx() and stop_rx() methods for each core.
Finally, we remove the stmmac_mac_set() call from stmmac_hw_setup().
drivers/net/ethernet/stmicro/stmmac/dwmac4_lib.c | 8 -------- drivers/net/ethernet/stmicro/stmmac/dwxgmac2_dma.c | 12 ------------ drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 17 ++++++----------- 3 files changed, 6 insertions(+), 31 deletions(-)
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
PATCH 1/3 net: stmmac: call phylink_start() and phylink_stop() in XDP functions
Phylink does not permit drivers to mess with the netif carrier, as this will de-synchronise phylink with the MAC driver. Moreover, setting and clearing the TE and RE bits via stmmac_mac_set() in this path is also wrong as the link may not be up.
Replace the netif_carrier_on(), netif_carrier_off() and stmmac_mac_set() calls with the appropriate phylink_start() and phylink_stop() calls, thereby allowing phylink to manage the netif carrier and TE/RE bits through the .mac_link_up() and .mac_link_down() methods.
Note that RE should only be set after the DMA is ready to avoid the receive FIFO between the MAC and DMA blocks overflowing, so phylink_start() needs to be placed after DMA has been started.
--- drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +6 -8
@@ -6922,6 +6922,8 @@ void stmmac_xdp_release(struct net_device *dev)/* Ensure tx function is not running */ netif_tx_disable (dev); +phylink_stop (priv->phylink); +/* Disable NAPI process */ stmmac_disable_all_queues (priv);
@@ -6937,14 +6939,10 @@ void stmmac_xdp_release(struct net_device *dev)/* Release and free the Rx/Tx resources */ free_dma_desc_resources (priv, &priv->dma_conf); -/* Disable the MAC Rx/Tx */ -stmmac_mac_set (priv, priv->ioaddr, false); -/* set trans_start so we don't get spurious * watchdogs during reset */ netif_trans_update (dev); -netif_carrier_off (dev); }int stmmac_xdp_open (struct net_device *dev)
@@ -7026,25 +7024,25 @@ int stmmac_xdp_open(struct net_device *dev)hrtimer_setup (&tx_q->txtimer, stmmac_tx_timer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); } -/* Enable the MAC Rx/Tx */ -stmmac_mac_set (priv, priv->ioaddr, true); -/* Start Rx & Tx DMA Channels */ stmmac_start_all_dma (priv); +phylink_start (priv->phylink); + ret =stmmac_request_irq (dev);if (ret) goto irq_error;/* Enable NAPI process*/ stmmac_enable_all_queues (priv); -netif_carrier_on (dev);netif_tx_start_all_queues (dev);stmmac_enable_all_dma_irq (priv);return 0 ; irq_error: +phylink_stop (priv->phylink); +for (chan =0 ; chan < priv->plat->tx_queues_to_use; chan++)hrtimer_cancel (&priv->dma_conf.tx_queue[chan].txtimer);
Signature · 1 line
2.30.2
Jakub Kicinski
maintainerRe: [PATCH net-next 1/3] net: stmmac: call phylink_start() and phylink_stop() in XDP functions
Quoted 11 lines · On Sat, 12 Apr 2025 10:34:42 +0100 Russell King (Oracle) wrote:
Phylink does not permit drivers to mess with the netif carrier, as this will de-synchronise phylink with the MAC driver. Moreover, setting and clearing the TE and RE bits via
stmmac_mac_set()in this path is also wrong as the link may not be up.Replace the
netif_carrier_on(),netif_carrier_off()andstmmac_mac_set()calls with the appropriatephylink_start()andphylink_stop()calls, thereby allowing phylink to manage the netif carrier and TE/RE bits through the .mac_link_up() and .mac_link_down() methods.
Quoted 3 lines
Note that RE should only be set after the DMA is ready to avoid the receive FIFO between the MAC and DMA blocks overflowing, so
phylink_start()needs to be placed after DMA has been started.
IIUC this will case a link loss when XDP is installed, if not disregard the reset of the email.
Any idea why it's necessary to mess with the link for XDP changes? Is there no way to discard all the traffic and let the queues go idle without dropping the link?
I think we should mention in the commit message that the side effect is link loss on XDP on / off. I don't know of any other driver which would need this, stmmac is a real gift..
Re: [PATCH net-next 1/3] net: stmmac: call phylink_start() and phylink_stop() in XDP functions
Quoted 12 lines · Jakub Kicinski
Quoted 11 lines · On Sat, 12 Apr 2025 10:34:42 +0100 Russell King (Oracle) wrote:
Phylink does not permit drivers to mess with the netif carrier, as this will de-synchronise phylink with the MAC driver. Moreover, setting and clearing the TE and RE bits via
stmmac_mac_set()in this path is also wrong as the link may not be up.Replace the
netif_carrier_on(),netif_carrier_off()andstmmac_mac_set()calls with the appropriatephylink_start()andphylink_stop()calls, thereby allowing phylink to manage the netif carrier and TE/RE bits through the .mac_link_up() and .mac_link_down() methods.
Quoted 6 lines
Quoted 3 lines
Note that RE should only be set after the DMA is ready to avoid the receive FIFO between the MAC and DMA blocks overflowing, so
phylink_start()needs to be placed after DMA has been started.IIUC this will case a link loss when XDP is installed, if not disregard the reset of the email.
It will, because the author who added XDP support to stmmac decided it was easier to tear everything down and rebuild, which meant (presumably) that it was necessary to use netif_carrier_off() to stop the net layer queueing packets to the driver. I'm just guessing - I know nothing about XDP, and never knowingly used it.
Quoted 1 line
Any idea why it's necessary to mess with the link for XDP changes?
Depends what you mean by "link". If you're asking why it messes with netif_carrier_foo(), my best guess is as above. However, phylink drivers are not allowed to mess with the netif_carrier state (as the commit message states.) This is not a new requirement, it's always been this way with phylink, and this pre-dates the addition of XDP to this driver.
As long as the code requires the netif_carrier to be turned off, the only way to guarantee that in a phylink using driver is as per this patch.
I'm guessing that the reason it does this is because it completely takes down the MAC and tx/rx rings to reprogram everything from scratch, and thus any interference from a packet coming in to be transmitted is going to cause problems.
Quoted 3 lines
I think we should mention in the commit message that the side effect is link loss on XDP on / off. I don't know of any other driver which would need this, stmmac is a real gift..
I'll add that. However, it would be nice to find a different solution for XDP on this driver.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Re: [PATCH net-next 1/3] net: stmmac: call phylink_start() and phylink_stop() in XDP functions
Quoted 19 lines · Russell King (Oracle)
Quoted 2 lines · Jakub Kicinski
IIUC this will case a link loss when XDP is installed, if not disregard the reset of the email.
It will, because the author who added XDP support to stmmac decided it was easier to tear everything down and rebuild, which meant (presumably) that it was necessary to use
netif_carrier_off()to stop the net layer queueing packets to the driver. I'm just guessing - I know nothing about XDP, and never knowingly used it.Quoted 1 line
Any idea why it's necessary to mess with the link for XDP changes?
Depends what you mean by "link". If you're asking why it messes with
netif_carrier_foo(), my best guess is as above. However, phylink drivers are not allowed to mess with thenetif_carrierstate (as the commit message states.) This is not a new requirement, it's always been this way with phylink, and this pre-dates the addition of XDP to this driver.
Quoted 8 lines
As long as the code requires the
netif_carrierto be turned off, the only way to guarantee that in a phylink using driver is as per this patch.I'm guessing that the reason it does this is because it completely takes down the MAC and tx/rx rings to reprogram everything from scratch, and thus any interference from a packet coming in to be transmitted is going to cause problems.
I'd like the "what do you mean by link" clarified before I update the commit message.
If you're referring to the carrier state via netif_carrier_off() / netif_carrier_on(), then nothing actually changes in that respect because the carrier manipulation is being done by the driver today, behind phylink's back. That changes to inside phylink with phylink's knowledge.
It is my understanding that netif_carrier_off() / netif_carrier_on() get notified to userspace, so this is visible today when XDP changes.
If you are referring to the messages that appear on the kernel console, then yes, phylink will print those in addition, which actually makes it more consistent with what's being reported to userspace.
Depending which you are referring to changes what I should say in the commit message. E.g.
"We retain the changes to carrier state, which are already being reported to userspace as link loss/link gain events, but we gain kernel messages reporting the link state."
if you're referring to the carrier state. Or maybe:
"This change will have the side effect of printing link messages to the kernel log, even though the physical link hasn't changed state. This matches the carrier state."
if you're referring to the additional kernel messages.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Jakub Kicinski
maintainerRe: [PATCH net-next 1/3] net: stmmac: call phylink_start() and phylink_stop() in XDP functions
Quoted 3 lines · On Wed, 16 Apr 2025 19:03:19 +0100 Russell King (Oracle) wrote:
"This change will have the side effect of printing link messages to the kernel log, even though the physical link hasn't changed state. This matches the carrier state."
So I did misunderstand. I thought we lose physical link. This paragraph looks good, then, it'd correct my guess.
PATCH 2/3 net: stmmac: remove _RE and _TE in (start|stop)_(tx|rx)() methods
In reply to Russell King (Oracle)Remove fiddling with _TE and _RE in the GMAC control register in the start_tx/stop_tx/start_rx/stop_rx() methods as this should be handled by stmmac_mac_link_up() and stmmac_mac_link_down() and not during initialisation.
--- drivers/net/ethernet/stmicro/stmmac/dwmac4_lib.c | 8 -------- drivers/net/ethernet/stmicro/stmmac/dwxgmac2_dma.c | 12 ------------ 2 files changed, 20 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac4_lib.c +0 -8
@@ -50,10 +50,6 @@ void dwmac4_dma_start_tx(struct stmmac_priv *priv, void __iomem *ioaddr, value |=DMA_CONTROL_ST ;writel (value, ioaddr +DMA_CHAN_TX_CONTROL (dwmac4_addrs, chan)); - - value =readl (ioaddr +GMAC_CONFIG ); - value |=GMAC_CONFIG_TE ; -writel (value, ioaddr +GMAC_CONFIG ); } void dwmac4_dma_stop_tx(struct stmmac_priv *priv, void __iomem *ioaddr,
@@ -77,10 +73,6 @@ void dwmac4_dma_start_rx(struct stmmac_priv *priv, void __iomem *ioaddr, value |=DMA_CONTROL_SR ;writel (value, ioaddr +DMA_CHAN_RX_CONTROL (dwmac4_addrs, chan)); - - value =readl (ioaddr +GMAC_CONFIG ); - value |=GMAC_CONFIG_RE ; -writel (value, ioaddr +GMAC_CONFIG ); } void dwmac4_dma_stop_rx(struct stmmac_priv *priv, void __iomem *ioaddr,
drivers/net/ethernet/stmicro/stmmac/dwxgmac2_dma.c +0 -12
@@ -288,10 +288,6 @@ static void dwxgmac2_dma_start_tx(struct stmmac_priv *priv, value =readl (ioaddr +XGMAC_DMA_CH_TX_CONTROL (chan)); value |=XGMAC_TXST ;writel (value, ioaddr +XGMAC_DMA_CH_TX_CONTROL (chan)); - - value =readl (ioaddr +XGMAC_TX_CONFIG ); - value |=XGMAC_CONFIG_TE ; -writel (value, ioaddr +XGMAC_TX_CONFIG ); } static void dwxgmac2_dma_stop_tx(struct stmmac_priv *priv, void __iomem *ioaddr,
@@ -302,10 +298,6 @@ static void dwxgmac2_dma_stop_tx(struct stmmac_priv *priv, void __iomem *ioaddr, value =readl (ioaddr +XGMAC_DMA_CH_TX_CONTROL (chan)); value &= ~XGMAC_TXST ;writel (value, ioaddr +XGMAC_DMA_CH_TX_CONTROL (chan)); - - value =readl (ioaddr +XGMAC_TX_CONFIG ); - value &= ~XGMAC_CONFIG_TE ; -writel (value, ioaddr +XGMAC_TX_CONFIG ); } static void dwxgmac2_dma_start_rx(struct stmmac_priv *priv,
@@ -316,10 +308,6 @@ static void dwxgmac2_dma_start_rx(struct stmmac_priv *priv, value =readl (ioaddr +XGMAC_DMA_CH_RX_CONTROL (chan)); value |=XGMAC_RXST ;writel (value, ioaddr +XGMAC_DMA_CH_RX_CONTROL (chan)); - - value =readl (ioaddr +XGMAC_RX_CONFIG ); - value |=XGMAC_CONFIG_RE ; -writel (value, ioaddr +XGMAC_RX_CONFIG ); } static void dwxgmac2_dma_stop_rx(struct stmmac_priv *priv, void __iomem *ioaddr,
Signature · 1 line
2.30.2
PATCH 3/3 net: stmmac: leave enabling _RE and _TE to stmmac_mac_link_up()
In reply to Russell King (Oracle)We only need to enable the MAC receiver and transmiter only when the link has come up.
With commit "net: stmmac: move phylink_resume() after resume setup is complete" we move the race between stmmac_mac_link_up() and stmmac_hw_setup(), ensuring that stmmac_mac_link_up() happens afterwards. This patch is a pre-requisit of this change.
Remove the unnecessary call to stmmac_mac_set(, true) in stmmac_hw_setup().
--- drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 3 --- 1 file changed, 3 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0 -3
@@ -3533,9 +3533,6 @@ static int stmmac_hw_setup(struct net_device *dev, bool ptp_register) priv->hw->rx_csum =0 ; } -/* Enable the MAC Rx/Tx */ -stmmac_mac_set (priv, priv->ioaddr, true); -/* Set the HW DMA mode and the COE */ stmmac_dma_operation_mode (priv);
Signature · 1 line
2.30.2