Skip to contentSkip to conversation

net: macb: Wake-on-Lan magic packet fixes and GEM handling

PATCH Superseded [email protected] 3 participants 13 messages Apr 21, 2020

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: macb: Wake-on-Lan magic packet fixes and GEM handling

From: Nicolas Ferre <[email protected]>

Hi,
Here is the 2nd serries to fix WoL magic-packet on the current macb driver. I also add, in the second part of this series the feature to GEM types of IPs. Please tell me if they should be separated; but the two last patches cannot go without the 5 fixes first ones.

MACB and GEM code must co-exist and as they don't share exactly the same register layout, I had to specialize a bit the suspend/resume paths and plug a specific IRQ handler in order to avoid overloading the "normal" IRQ hot path.

The use of dumb buffers for RX that Harini implemented in [1] might need to be considered for a follow-up patch series in order to address lower-power modes on some of the platforms.
For instance, I didn't have to implement dumb buffers for some of the simpler ARM9 platforms using MACB+FIFO types of controllers.

Please give feedback. Best regards,
Nicolas

[1]:
https://github.com/Xilinx/linux-xlnx/commit/e9648006e8d9132db2594e50e700af362b3c9226#diff-41909d180431659ccc1229aa30fd4e5a
https://github.com/Xilinx/linux-xlnx/commit/60a21c686f7e4e50489ae04b9bb1980b145e52ef

Changes in v2:
- Add patch 4/7 ("net: macb: fix macb_suspend() by removing call to netif_carrier_off()") needed for keeping phy state consistent
- Add patch 5/7 ("net: macb: fix call to pm_runtime in the suspend/resume functions") that prevent putting the macb in runtime pm suspend mode when WoL is used
- Collect review tags on 3 first patches from Florian: Thanks!
- Review of macb_resume() function
- Addition of pm_wakeup_event() in both MACB and GEM WoL IRQ handlers

Nicolas Ferre (7):
net: macb: fix wakeup test in runtime suspend/resume routines
net: macb: mark device wake capable when "magic-packet" property
present
net: macb: fix macb_get/set_wol() when moving to phylink
net: macb: fix macb_suspend() by removing call to netif_carrier_off()
net: macb: fix call to pm_runtime in the suspend/resume functions
net: macb: WoL support for GEM type of Ethernet controller
net: macb: Add WoL interrupt support for MACB type of Ethernet
controller

 drivers/net/ethernet/cadence/macb.h      |   3 +
 drivers/net/ethernet/cadence/macb_main.c | 202 +++++++++++++++++++----
 2 files changed, 173 insertions(+), 32 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/7 net: macb: fix wakeup test in runtime suspend/resume routines

From: Nicolas Ferre <[email protected]>

Use the proper struct device pointer to check if the wakeup flag and wakeup source are positioned.
Use the one passed by function call which is equivalent to &bp->dev->dev.parent.

It's preventing the trigger of a spurious interrupt in case the Wake-on-Lan feature is used.

Fixes bc1109d04c39 ("net: macb: Add pm runtime support")
Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
---
 drivers/net/ethernet/cadence/macb_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +2 -2
index a0e8c5bbabc0..d1b4d6b6d7c8 100644
@@ -4616,7 +4616,7 @@ static int __maybe_unused macb_runtime_suspend(struct device *dev)
 	struct net_device *netdev = dev_get_drvdata(dev);
 	struct macb *bp = netdev_priv(netdev);
 
-	if (!(device_may_wakeup(&bp->dev->dev))) {
+	if (!(device_may_wakeup(dev))) {
 		clk_disable_unprepare(bp->tx_clk);
 		clk_disable_unprepare(bp->hclk);
 		clk_disable_unprepare(bp->pclk);
@@ -4632,7 +4632,7 @@ static int __maybe_unused macb_runtime_resume(struct device *dev)
 	struct net_device *netdev = dev_get_drvdata(dev);
 	struct macb *bp = netdev_priv(netdev);
 
-	if (!(device_may_wakeup(&bp->dev->dev))) {
+	if (!(device_may_wakeup(dev))) {
 		clk_prepare_enable(bp->pclk);
 		clk_prepare_enable(bp->hclk);
 		clk_prepare_enable(bp->tx_clk);
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 2/7 net: macb: mark device wake capable when "magic-packet" property present

In reply to [email protected]

From: Nicolas Ferre <[email protected]>

Change the way the "magic-packet" DT property is handled in the macb_probe() function, matching DT binding documentation. Now we mark the device as "wakeup capable" instead of calling the device_init_wakeup() function that would enable the wakeup source.

For Ethernet WoL, enabling the wakeup_source is done by using ethtool and associated macb_set_wol() function that already calls device_set_wakeup_enable() for this purpose.

That would reduce power consumption by cutting more clocks if "magic-packet" property is set but WoL is not configured by ethtool.

Fixes 3e2a5e153906 ("net: macb: add wake-on-lan support via magic packet")
Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
Cc Sergio Prado <[email protected]>
---
 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 d1b4d6b6d7c8..629660d9f17e 100644
@@ -4384,7 +4384,7 @@ static int macb_probe(struct platform_device *pdev)
 	bp->wol = 0;
 	if (of_get_property(np, "magic-packet", NULL))
 		bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
-	device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
+	device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
 
 	spin_lock_init(&bp->lock);
 
Signature · 1 line
2.20.1
#3 lore raw

PATCH v2 3/7 net: macb: fix macb_get/set_wol() when moving to phylink

In reply to [email protected]

From: Nicolas Ferre <[email protected]>

Keep previous function goals and integrate phylink actions to them.

phylink_ethtool_get_wol() is not enough to figure out if Ethernet driver supports Wake-on-Lan.
Initialization of "supported" and "wolopts" members is done in phylink function, no need to keep them in calling function.

phylink_ethtool_set_wol() return value is not enough to determine if WoL is enabled for the calling Ethernet driver. Call it first but don't rely on its return value as most of simple PHY drivers don't implement a set_wol() function.

Fixes 7897b071ac3b ("net: macb: convert to phylink")
Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
Cc Antoine Tenart <[email protected]>
---
 drivers/net/ethernet/cadence/macb_main.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +10 -8
index 629660d9f17e..b17a33c60cd4 100644
@@ -2813,21 +2813,23 @@ static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
 {
 	struct macb *bp = netdev_priv(netdev);
 
-	wol->supported = 0;
-	wol->wolopts = 0;
-
-	if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET)
+	if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
 		phylink_ethtool_get_wol(bp->phylink, wol);
+		wol->supported |= WAKE_MAGIC;
+
+		if (bp->wol & MACB_WOL_ENABLED)
+			wol->wolopts |= WAKE_MAGIC;
+	}
 }
 
 static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
 {
 	struct macb *bp = netdev_priv(netdev);
-	int ret;
 
-	ret = phylink_ethtool_set_wol(bp->phylink, wol);
-	if (!ret)
-		return 0;
+	/* Pass the order to phylink layer.
+	 * Don't test return value as set_wol() is often not supported.
+	 */
+	phylink_ethtool_set_wol(bp->phylink, wol);
 
 	if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
 	    (wol->wolopts & ~WAKE_MAGIC))
Signature · 1 line
2.20.1
#4 lore raw

PATCH v2 4/7 net: macb: fix macb_suspend() by removing call to netif_carrier_off()

In reply to [email protected]

From: Nicolas Ferre <[email protected]>

As we now use the phylink call to phylink_stop() in the non-WoL path, there is no need for this call to netif_carrier_off() anymore. It can disturb the underlying phylink FSM.

Fixes 7897b071ac3b ("net: macb: convert to phylink")
Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
Cc Antoine Tenart <[email protected]>

---
Changes in v2:
- new in v2 serries

 drivers/net/ethernet/cadence/macb_main.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/ethernet/cadence/macb_main.c +0 -1
index b17a33c60cd4..72b8983a763a 100644
@@ -4562,7 +4562,6 @@ static int __maybe_unused macb_suspend(struct device *dev)
 			bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
 	}
 
-	netif_carrier_off(netdev);
 	if (bp->ptp_info)
 		bp->ptp_info->ptp_remove(netdev);
 	pm_runtime_force_suspend(dev);
Signature · 1 line
2.20.1
Reviewed-by on v2 4/7
#5 lore raw

Re: [PATCH v2 4/7] net: macb: fix macb_suspend() by removing call to netif_carrier_off()

Quoted 11 lines · On 4/21/2020 3:41 AM, [email protected] wrote:

From: Nicolas Ferre <[email protected]>

As we now use the phylink call to phylink_stop() in the non-WoL path, there is no need for this call to netif_carrier_off() anymore. It can disturb the underlying phylink FSM.

Fixes 7897b071ac3b ("net: macb: convert to phylink")
Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
Cc Antoine Tenart <[email protected]>
Signature · 1 line
Florian
#6 lore raw

PATCH v2 5/7 net: macb: fix call to pm_runtime in the suspend/resume functions

In reply to [email protected]

From: Nicolas Ferre <[email protected]>

The calls to pm_runtime_force_suspend/resume() functions are only relevant if the device is not configured to act as a WoL wakeup source. Add the device_may_wakeup() test before calling them.

Fixes 3e2a5e153906 ("net: macb: add wake-on-lan support via magic packet")
Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
Cc Sergio Prado <[email protected]>

---
Changes in v2:
- new in v2 serries

 drivers/net/ethernet/cadence/macb_main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +4 -2
index 72b8983a763a..8cf8e21fbb07 100644
@@ -4564,7 +4564,8 @@ static int __maybe_unused macb_suspend(struct device *dev)
 
 	if (bp->ptp_info)
 		bp->ptp_info->ptp_remove(netdev);
-	pm_runtime_force_suspend(dev);
+	if (!(device_may_wakeup(dev)))
+		pm_runtime_force_suspend(dev);
 
 	return 0;
 }
@@ -4579,7 +4580,8 @@ static int __maybe_unused macb_resume(struct device *dev)
 	if (!netif_running(netdev))
 		return 0;
 
-	pm_runtime_force_resume(dev);
+	if (!(device_may_wakeup(dev)))
+		pm_runtime_force_resume(dev);
 
 	if (bp->wol & MACB_WOL_ENABLED) {
 		macb_writel(bp, IDR, MACB_BIT(WOL));
Signature · 1 line
2.20.1
Reviewed-by on v2 5/7
#7 lore raw

Re: [PATCH v2 5/7] net: macb: fix call to pm_runtime in the suspend/resume functions

Quoted 23 lines · On 4/21/2020 3:41 AM, [email protected] wrote:

From: Nicolas Ferre <[email protected]>

The calls to pm_runtime_force_suspend/resume() functions are only relevant if the device is not configured to act as a WoL wakeup source. Add the device_may_wakeup() test before calling them.

Fixes 3e2a5e153906 ("net: macb: add wake-on-lan support via magic packet")
Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
Cc Sergio Prado <[email protected]>

---
Changes in v2:
- new in v2 serries

  drivers/net/ethernet/cadence/macb_main.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/ethernet/cadence/macb_main.c +0 -0
index 72b8983a763a..8cf8e21fbb07 100644

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_suspend(struct device *dev) · line 4568

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +2 -1
@@ -4564,7 +4564,8 @@ static int __maybe_unused macb_suspend(struct device *dev)
 
  	if (bp->ptp_info)
  		bp->ptp_info->ptp_remove(netdev);
-	pm_runtime_force_suspend(dev);
+	if (!(device_may_wakeup(dev)))
+		pm_runtime_force_suspend(dev);

Only if you need to respin, the parenthesis around device_may_wakeup() are not required:

Signature · 1 line
Florian
#8 lore raw

PATCH v2 6/7 net: macb: WoL support for GEM type of Ethernet controller

In reply to [email protected]

From: Nicolas Ferre <[email protected]>

Adapt the Wake-on-Lan feature to the Cadence GEM Ethernet controller. This controller has different register layout and cannot be handled by previous code.
We disable completely interrupts on all the queues but the queue 0. Handling of WoL interrupt is done in another interrupt handler positioned depending on the controller version used, just between suspend() and resume() calls.
It allows to lower pressure on the generic interrupt hot path by removing the need to handle 2 tests for each IRQ: the first figuring out the controller revision, the second for actually knowing if the WoL bit is set.

Queue management in suspend()/resume() functions inspired from RFC patch by Harini Katakam <[email protected]>, thanks!

Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>

---
Changes in v2:
- Addition of pm_wakeup_event() in WoL IRQ
- In macb_resume(), removal of setting the MPE bit in NCR register which is not needed in any case: IP is reset on the usual path and kept alive if WoL is used
- In macb_resume(), complete reset of the controller is kept only for non-WoL case. For the WoL case, we only replace the usual IRQ handler.

 drivers/net/ethernet/cadence/macb.h      |   3 +
 drivers/net/ethernet/cadence/macb_main.c | 134 ++++++++++++++++++++---
 2 files changed, 119 insertions(+), 18 deletions(-)
drivers/net/ethernet/cadence/macb.h +3 -0
index ab827fb4b6b9..4f1b41569260 100644
@@ -90,6 +90,7 @@
 #define GEM_SA3T		0x009C /* Specific3 Top */
 #define GEM_SA4B		0x00A0 /* Specific4 Bottom */
 #define GEM_SA4T		0x00A4 /* Specific4 Top */
+#define GEM_WOL			0x00b8 /* Wake on LAN */
 #define GEM_EFTSH		0x00e8 /* PTP Event Frame Transmitted Seconds Register 47:32 */
 #define GEM_EFRSH		0x00ec /* PTP Event Frame Received Seconds Register 47:32 */
 #define GEM_PEFTSH		0x00f0 /* PTP Peer Event Frame Transmitted Seconds Register 47:32 */
@@ -396,6 +397,8 @@
 #define MACB_PDRSFT_SIZE	1
 #define MACB_SRI_OFFSET		26 /* TSU Seconds Register Increment */
 #define MACB_SRI_SIZE		1
+#define GEM_WOL_OFFSET		28 /* Enable wake-on-lan interrupt */
+#define GEM_WOL_SIZE		1
 
 /* Timer increment fields */
 #define MACB_TI_CNS_OFFSET	0
drivers/net/ethernet/cadence/macb_main.c +116 -18

184 lines not on this page · raw message

Signature · 1 line
2.20.1
#9 lore raw

Re: [PATCH v2 6/7] net: macb: WoL support for GEM type of Ethernet controller

Hi Nicolas,

Quoted 126 lines · On 21.04.2020 13:41, [email protected] wrote:

From: Nicolas Ferre <[email protected]>

Adapt the Wake-on-Lan feature to the Cadence GEM Ethernet controller. This controller has different register layout and cannot be handled by previous code.
We disable completely interrupts on all the queues but the queue 0. Handling of WoL interrupt is done in another interrupt handler positioned depending on the controller version used, just between suspend() and resume() calls.
It allows to lower pressure on the generic interrupt hot path by removing the need to handle 2 tests for each IRQ: the first figuring out the controller revision, the second for actually knowing if the WoL bit is set.

Queue management in suspend()/resume() functions inspired from RFC patch by Harini Katakam <[email protected]>, thanks!

Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>

---
Changes in v2:
- Addition of pm_wakeup_event() in WoL IRQ
- In macb_resume(), removal of setting the MPE bit in NCR register which is not needed in any case: IP is reset on the usual path and kept alive if WoL is used
- In macb_resume(), complete reset of the controller is kept only for non-WoL case. For the WoL case, we only replace the usual IRQ handler.

 drivers/net/ethernet/cadence/macb.h      |   3 +
 drivers/net/ethernet/cadence/macb_main.c | 134 ++++++++++++++++++++---
 2 files changed, 119 insertions(+), 18 deletions(-)
drivers/net/ethernet/cadence/macb.h +3 -0
index ab827fb4b6b9..4f1b41569260 100644
@@ -90,6 +90,7 @@
 #define GEM_SA3T		0x009C /* Specific3 Top */
 #define GEM_SA4B		0x00A0 /* Specific4 Bottom */
 #define GEM_SA4T		0x00A4 /* Specific4 Top */
+#define GEM_WOL			0x00b8 /* Wake on LAN */
 #define GEM_EFTSH		0x00e8 /* PTP Event Frame Transmitted Seconds Register 47:32 */
 #define GEM_EFRSH		0x00ec /* PTP Event Frame Received Seconds Register 47:32 */
 #define GEM_PEFTSH		0x00f0 /* PTP Peer Event Frame Transmitted Seconds Register 47:32 */
@@ -396,6 +397,8 @@
 #define MACB_PDRSFT_SIZE	1
 #define MACB_SRI_OFFSET		26 /* TSU Seconds Register Increment */
 #define MACB_SRI_SIZE		1
+#define GEM_WOL_OFFSET		28 /* Enable wake-on-lan interrupt */
+#define GEM_WOL_SIZE		1
 
 /* Timer increment fields */
 #define MACB_TI_CNS_OFFSET	0
drivers/net/ethernet/cadence/macb_main.c +37 -8
index 8cf8e21fbb07..56ce39dd1cc0 100644
@@ -1513,6 +1513,35 @@ static void macb_tx_restart(struct macb_queue *queue)
 	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
 }
 
+static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
+{
+	struct macb_queue *queue = dev_id;
+	struct macb *bp = queue->bp;
+	u32 status;
+
+	status = queue_readl(queue, ISR);
+
+	if (unlikely(!status))
+		return IRQ_NONE;
+
+	spin_lock(&bp->lock);
+
+	if (status & GEM_BIT(WOL)) {
+		queue_writel(queue, IDR, GEM_BIT(WOL));
+		gem_writel(bp, WOL, 0);
+		netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
+			    (unsigned int)(queue - bp->queues),
+			    (unsigned long)status);
+		if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+			queue_writel(queue, ISR, GEM_BIT(WOL));
+		pm_wakeup_event(&bp->pdev->dev, 0);
+	}
+
+	spin_unlock(&bp->lock);
+
+	return IRQ_HANDLED;
+}
+
 static irqreturn_t macb_interrupt(int irq, void *dev_id)
 {
 	struct macb_queue *queue = dev_id;
@@ -3306,6 +3335,8 @@ static const struct ethtool_ops macb_ethtool_ops = {
 static const struct ethtool_ops gem_ethtool_ops = {
 	.get_regs_len		= macb_get_regs_len,
 	.get_regs		= macb_get_regs,
+	.get_wol		= macb_get_wol,
+	.set_wol		= macb_set_wol,
 	.get_link		= ethtool_op_get_link,
 	.get_ts_info		= macb_get_ts_info,
 	.get_ethtool_stats	= gem_get_ethtool_stats,
@@ -4534,20 +4565,54 @@ static int __maybe_unused macb_suspend(struct device *dev)
 	struct macb_queue *queue = bp->queues;
 	unsigned long flags;
 	unsigned int q;
+	int err;
 
 	if (!netif_running(netdev))
 		return 0;
 
 	if (bp->wol & MACB_WOL_ENABLED) {
-		macb_writel(bp, IER, MACB_BIT(WOL));
-		macb_writel(bp, WOL, MACB_BIT(MAG));
-		enable_irq_wake(bp->queues[0].irq);
-		netif_device_detach(netdev);
-	} else {
-		netif_device_detach(netdev);
+		spin_lock_irqsave(&bp->lock, flags);
+		/* Flush all status bits */
+		macb_writel(bp, TSR, -1);
+		macb_writel(bp, RSR, -1);
 		for (q = 0, queue = bp->queues; q < bp->num_queues;
-		     ++q, ++queue)
-			napi_disable(&queue->napi);
+		     ++q, ++queue) {

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_suspend(struct device *dev) · line 4589

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+			/* Disable all interrupts */
+			queue_writel(queue, IDR, -1);
+			queue_readl(queue, ISR);
+			if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+				queue_writel(queue, ISR, -1);
+		}
+		/* Change interrupt handler and
+		 * Enable WoL IRQ on queue 0
+		 */
+		if (macb_is_gem(bp)) {

Couldn't this starting here:

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_suspend(struct device *dev) · line 4598

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +9 -0
+			devm_free_irq(dev, bp->queues[0].irq, bp->queues);
+			err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
+					       IRQF_SHARED, netdev->name, bp->queues);
+			if (err) {
+				dev_err(dev,
+					"Unable to request IRQ %d (error %d)\n",
+					bp->queues[0].irq, err);
+				return err;
+			}

ending ^ (and the equivalent in resume function) be avoided by moving the content of gem_wol_interrupt:

+ if (status & GEM_BIT(WOL)) {
+ queue_writel(queue, IDR, GEM_BIT(WOL));
+ gem_writel(bp, WOL, 0);
+ netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
+ (unsigned int)(queue - bp->queues),
+ (unsigned long)status);
+ if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+ queue_writel(queue, ISR, GEM_BIT(WOL));
+ pm_wakeup_event(&bp->pdev->dev, 0);
+ }

at the beginning of macb_interrupt() and issuing a return after pm_wakeup_event()?

Instead of these:

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_suspend(struct device *dev) · line 4604

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +6 -0
+			queue_writel(bp->queues, IER, GEM_BIT(WOL));
+			gem_writel(bp, WOL, MACB_BIT(MAG));
+		} else {
+			queue_writel(bp->queues, IER, MACB_BIT(WOL));
+			macb_writel(bp, WOL, MACB_BIT(MAG));
+		}

You could use:

		queue_writel(bp->queues, IER, GEM_BIT(WOL));
		macb_or_gem_writel(bp, WOL, MACB_BIT(MAG))
Quoted 74 lines
drivers/net/ethernet/cadence/macb_main.c +51 -4
+		spin_unlock_irqrestore(&bp->lock, flags);
+
+		enable_irq_wake(bp->queues[0].irq);
+	}
+
+	netif_device_detach(netdev);
+	for (q = 0, queue = bp->queues; q < bp->num_queues;
+	     ++q, ++queue)
+		napi_disable(&queue->napi);
+
+	if (!(bp->wol & MACB_WOL_ENABLED)) {
 		rtnl_lock();
 		phylink_stop(bp->phylink);
 		rtnl_unlock();
@@ -4575,7 +4640,9 @@ static int __maybe_unused macb_resume(struct device *dev)
 	struct net_device *netdev = dev_get_drvdata(dev);
 	struct macb *bp = netdev_priv(netdev);
 	struct macb_queue *queue = bp->queues;
+	unsigned long flags;
 	unsigned int q;
+	int err;
 
 	if (!netif_running(netdev))
 		return 0;
@@ -4584,29 +4651,60 @@ static int __maybe_unused macb_resume(struct device *dev)
 		pm_runtime_force_resume(dev);
 
 	if (bp->wol & MACB_WOL_ENABLED) {
-		macb_writel(bp, IDR, MACB_BIT(WOL));
-		macb_writel(bp, WOL, 0);
+		spin_lock_irqsave(&bp->lock, flags);
+		/* Disable WoL */
+		if (macb_is_gem(bp)) {
+			queue_writel(bp->queues, IDR, GEM_BIT(WOL));
+			gem_writel(bp, WOL, 0);
+		} else {
+			queue_writel(bp->queues, IDR, MACB_BIT(WOL));
+			macb_writel(bp, WOL, 0);
+		}
+		/* Clear ISR on queue 0 */
+		queue_readl(bp->queues, ISR);
+		if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+			queue_writel(bp->queues, ISR, -1);
+		/* Replace interrupt handler on queue 0 */
+		devm_free_irq(dev, bp->queues[0].irq, bp->queues);
+		err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
+				       IRQF_SHARED, netdev->name, bp->queues);
+		if (err) {
+			dev_err(dev,
+				"Unable to request IRQ %d (error %d)\n",
+				bp->queues[0].irq, err);
+			return err;
+		}
+		/* Enable interrupts */
+		for (q = 0, queue = bp->queues; q < bp->num_queues;
+		     ++q, ++queue)
+			queue_writel(queue, IER,
+				     bp->rx_intr_mask |
+				     MACB_TX_INT_FLAGS |
+				     MACB_BIT(HRESP));
+		spin_unlock_irqrestore(&bp->lock, flags);
+
 		disable_irq_wake(bp->queues[0].irq);
-	} else {
-		macb_writel(bp, NCR, MACB_BIT(MPE));
+	}
+
+	for (q = 0, queue = bp->queues; q < bp->num_queues;
+	     ++q, ++queue)
+		napi_enable(&queue->napi);
 
+	if (!(bp->wol & MACB_WOL_ENABLED)) {
 		if (netdev->hw_features & NETIF_F_NTUPLE)
 			gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_resume(struct device *dev) · line 4702

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +3 -3
 
 		if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
 			macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
 
-		for (q = 0, queue = bp->queues; q < bp->num_queues;
-		     ++q, ++queue)
-			napi_enable(&queue->napi);
+		macb_init_hw(bp);
+		macb_set_rx_mode(netdev);
+		macb_restore_features(bp);

Even WoL may not be available in backup and self-refresh mode (present on SAMA5D2 devices), switching to BSR mode while WoL is enabled may lead to ethernet being unavailable after resuming. Did you manage to test this scenario?

Quoted 12 lines
drivers/net/ethernet/cadence/macb_main.c +0 -3
 		rtnl_lock();
 		phylink_start(bp->phylink);
 		rtnl_unlock();
 	}
 
-	macb_init_hw(bp);
-	macb_set_rx_mode(netdev);
-	macb_restore_features(bp);
 	netif_device_attach(netdev);
 	if (bp->ptp_info)
 		bp->ptp_info->ptp_init(netdev);
#10 lore raw

Re: [PATCH v2 6/7] net: macb: WoL support for GEM type of Ethernet controller

Quoted 1 line · On 21/04/2020 at 17:13, Claudiu Beznea - M18063 wrote:

Hi Nicolas,

Claudiu,

Thanks for your feedback.

Quoted 177 lines · On 21.04.2020 13:41, [email protected] wrote:
Quoted 126 lines

From: Nicolas Ferre <[email protected]>

Adapt the Wake-on-Lan feature to the Cadence GEM Ethernet controller. This controller has different register layout and cannot be handled by previous code.
We disable completely interrupts on all the queues but the queue 0. Handling of WoL interrupt is done in another interrupt handler positioned depending on the controller version used, just between suspend() and resume() calls.
It allows to lower pressure on the generic interrupt hot path by removing the need to handle 2 tests for each IRQ: the first figuring out the controller revision, the second for actually knowing if the WoL bit is set.

Queue management in suspend()/resume() functions inspired from RFC patch by Harini Katakam <[email protected]>, thanks!

Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>

---
Changes in v2:
- Addition of pm_wakeup_event() in WoL IRQ
- In macb_resume(), removal of setting the MPE bit in NCR register which is not needed in any case: IP is reset on the usual path and kept alive if WoL is used
- In macb_resume(), complete reset of the controller is kept only for non-WoL case. For the WoL case, we only replace the usual IRQ handler.

  drivers/net/ethernet/cadence/macb.h      |   3 +
  drivers/net/ethernet/cadence/macb_main.c | 134 ++++++++++++++++++++---
  2 files changed, 119 insertions(+), 18 deletions(-)
drivers/net/ethernet/cadence/macb.h +3 -0
index ab827fb4b6b9..4f1b41569260 100644
@@ -90,6 +90,7 @@
  #define GEM_SA3T		0x009C /* Specific3 Top */
  #define GEM_SA4B		0x00A0 /* Specific4 Bottom */
  #define GEM_SA4T		0x00A4 /* Specific4 Top */
+#define GEM_WOL			0x00b8 /* Wake on LAN */
  #define GEM_EFTSH		0x00e8 /* PTP Event Frame Transmitted Seconds Register 47:32 */
  #define GEM_EFRSH		0x00ec /* PTP Event Frame Received Seconds Register 47:32 */
  #define GEM_PEFTSH		0x00f0 /* PTP Peer Event Frame Transmitted Seconds Register 47:32 */
@@ -396,6 +397,8 @@
  #define MACB_PDRSFT_SIZE	1
  #define MACB_SRI_OFFSET		26 /* TSU Seconds Register Increment */
  #define MACB_SRI_SIZE		1
+#define GEM_WOL_OFFSET		28 /* Enable wake-on-lan interrupt */
+#define GEM_WOL_SIZE		1
 
  /* Timer increment fields */
  #define MACB_TI_CNS_OFFSET	0
drivers/net/ethernet/cadence/macb_main.c +37 -8
index 8cf8e21fbb07..56ce39dd1cc0 100644
@@ -1513,6 +1513,35 @@ static void macb_tx_restart(struct macb_queue *queue)
  	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  }
 
+static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
+{
+	struct macb_queue *queue = dev_id;
+	struct macb *bp = queue->bp;
+	u32 status;
+
+	status = queue_readl(queue, ISR);
+
+	if (unlikely(!status))
+		return IRQ_NONE;
+
+	spin_lock(&bp->lock);
+
+	if (status & GEM_BIT(WOL)) {
+		queue_writel(queue, IDR, GEM_BIT(WOL));
+		gem_writel(bp, WOL, 0);
+		netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
+			    (unsigned int)(queue - bp->queues),
+			    (unsigned long)status);
+		if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+			queue_writel(queue, ISR, GEM_BIT(WOL));
+		pm_wakeup_event(&bp->pdev->dev, 0);
+	}
+
+	spin_unlock(&bp->lock);
+
+	return IRQ_HANDLED;
+}
+
  static irqreturn_t macb_interrupt(int irq, void *dev_id)
  {
  	struct macb_queue *queue = dev_id;
@@ -3306,6 +3335,8 @@ static const struct ethtool_ops macb_ethtool_ops = {
  static const struct ethtool_ops gem_ethtool_ops = {
  	.get_regs_len		= macb_get_regs_len,
  	.get_regs		= macb_get_regs,
+	.get_wol		= macb_get_wol,
+	.set_wol		= macb_set_wol,
  	.get_link		= ethtool_op_get_link,
  	.get_ts_info		= macb_get_ts_info,
  	.get_ethtool_stats	= gem_get_ethtool_stats,
@@ -4534,20 +4565,54 @@ static int __maybe_unused macb_suspend(struct device *dev)
  	struct macb_queue *queue = bp->queues;
  	unsigned long flags;
  	unsigned int q;
+	int err;
 
  	if (!netif_running(netdev))
  		return 0;
 
  	if (bp->wol & MACB_WOL_ENABLED) {
-		macb_writel(bp, IER, MACB_BIT(WOL));
-		macb_writel(bp, WOL, MACB_BIT(MAG));
-		enable_irq_wake(bp->queues[0].irq);
-		netif_device_detach(netdev);
-	} else {
-		netif_device_detach(netdev);
+		spin_lock_irqsave(&bp->lock, flags);
+		/* Flush all status bits */
+		macb_writel(bp, TSR, -1);
+		macb_writel(bp, RSR, -1);
  		for (q = 0, queue = bp->queues; q < bp->num_queues;
-		     ++q, ++queue)
-			napi_disable(&queue->napi);
+		     ++q, ++queue) {

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_suspend(struct device *dev) · line 4589

Quoted 10 lines
drivers/net/ethernet/cadence/macb_main.c +10 -0
+			/* Disable all interrupts */
+			queue_writel(queue, IDR, -1);
+			queue_readl(queue, ISR);
+			if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+				queue_writel(queue, ISR, -1);
+		}
+		/* Change interrupt handler and
+		 * Enable WoL IRQ on queue 0
+		 */
+		if (macb_is_gem(bp)) {

Couldn't this starting here:

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_suspend(struct device *dev) · line 4598

Quoted 9 lines
drivers/net/ethernet/cadence/macb_main.c +9 -0
+			devm_free_irq(dev, bp->queues[0].irq, bp->queues);
+			err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
+					       IRQF_SHARED, netdev->name, bp->queues);
+			if (err) {
+				dev_err(dev,
+					"Unable to request IRQ %d (error %d)\n",
+					bp->queues[0].irq, err);
+				return err;
+			}

ending ^ (and the equivalent in resume function) be avoided by moving the content of gem_wol_interrupt:

drivers/net/ethernet/cadence/macb_main.c +10 -0
+	if (status & GEM_BIT(WOL)) {
+		queue_writel(queue, IDR, GEM_BIT(WOL));
+		gem_writel(bp, WOL, 0);
+		netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
+			    (unsigned int)(queue - bp->queues),
+			    (unsigned long)status);
+		if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+			queue_writel(queue, ISR, GEM_BIT(WOL));
+		pm_wakeup_event(&bp->pdev->dev, 0);
+	}

at the beginning of macb_interrupt() and issuing a return after pm_wakeup_event()?

Instead of these:

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_suspend(struct device *dev) · line 4604

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +6 -0
+			queue_writel(bp->queues, IER, GEM_BIT(WOL));
+			gem_writel(bp, WOL, MACB_BIT(MAG));
+		} else {
+			queue_writel(bp->queues, IER, MACB_BIT(WOL));
+			macb_writel(bp, WOL, MACB_BIT(MAG));
+		}

You could use:

Quoted 2 lines
		queue_writel(bp->queues, IER, GEM_BIT(WOL));
		macb_or_gem_writel(bp, WOL, MACB_BIT(MAG))

This was my first though, even if it added one more test to the interrupt routine even when WoL is not enabled.

But where it's becoming annoying is that what you highlight above as:
if (status & GEM_BIT(WOL))
for GEM, is actually:
if (status & MACB_BIT(WOL))
for MACB...
which means that we have to test controller's revision for each interrupt before knowing if the WoL one is activated or not. To my mind, it's really loosing CPU power where we are already chasing for it in our little SoC configurations. Moreover for something which is fairly specialized to some use cases.

If found a solution by specializing the IRQ routine just during suspend/resume.

Quoted 78 lines
Quoted 78 lines
drivers/net/ethernet/cadence/macb_main.c +51 -4
+		spin_unlock_irqrestore(&bp->lock, flags);
+
+		enable_irq_wake(bp->queues[0].irq);
+	}
+
+	netif_device_detach(netdev);
+	for (q = 0, queue = bp->queues; q < bp->num_queues;
+	     ++q, ++queue)
+		napi_disable(&queue->napi);
+
+	if (!(bp->wol & MACB_WOL_ENABLED)) {
  		rtnl_lock();
  		phylink_stop(bp->phylink);
  		rtnl_unlock();
@@ -4575,7 +4640,9 @@ static int __maybe_unused macb_resume(struct device *dev)
  	struct net_device *netdev = dev_get_drvdata(dev);
  	struct macb *bp = netdev_priv(netdev);
  	struct macb_queue *queue = bp->queues;
+	unsigned long flags;
  	unsigned int q;
+	int err;
 
  	if (!netif_running(netdev))
  		return 0;
@@ -4584,29 +4651,60 @@ static int __maybe_unused macb_resume(struct device *dev)
  		pm_runtime_force_resume(dev);
 
  	if (bp->wol & MACB_WOL_ENABLED) {
-		macb_writel(bp, IDR, MACB_BIT(WOL));
-		macb_writel(bp, WOL, 0);
+		spin_lock_irqsave(&bp->lock, flags);
+		/* Disable WoL */
+		if (macb_is_gem(bp)) {
+			queue_writel(bp->queues, IDR, GEM_BIT(WOL));
+			gem_writel(bp, WOL, 0);
+		} else {
+			queue_writel(bp->queues, IDR, MACB_BIT(WOL));
+			macb_writel(bp, WOL, 0);
+		}
+		/* Clear ISR on queue 0 */
+		queue_readl(bp->queues, ISR);
+		if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+			queue_writel(bp->queues, ISR, -1);
+		/* Replace interrupt handler on queue 0 */
+		devm_free_irq(dev, bp->queues[0].irq, bp->queues);
+		err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
+				       IRQF_SHARED, netdev->name, bp->queues);
+		if (err) {
+			dev_err(dev,
+				"Unable to request IRQ %d (error %d)\n",
+				bp->queues[0].irq, err);
+			return err;
+		}
+		/* Enable interrupts */
+		for (q = 0, queue = bp->queues; q < bp->num_queues;
+		     ++q, ++queue)
+			queue_writel(queue, IER,
+				     bp->rx_intr_mask |
+				     MACB_TX_INT_FLAGS |
+				     MACB_BIT(HRESP));
+		spin_unlock_irqrestore(&bp->lock, flags);
+
  		disable_irq_wake(bp->queues[0].irq);
-	} else {
-		macb_writel(bp, NCR, MACB_BIT(MPE));
+	}
+
+	for (q = 0, queue = bp->queues; q < bp->num_queues;
+	     ++q, ++queue)
+		napi_enable(&queue->napi);
 
+	if (!(bp->wol & MACB_WOL_ENABLED)) {
  		if (netdev->hw_features & NETIF_F_NTUPLE)
  			gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
 
  		if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  			macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
 
Quoted 11 lines

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_resume(struct device *dev) · line 4702

Quoted 6 lines
drivers/net/ethernet/cadence/macb_main.c +3 -3
-		for (q = 0, queue = bp->queues; q < bp->num_queues;
-		     ++q, ++queue)
-			napi_enable(&queue->napi);
+		macb_init_hw(bp);
+		macb_set_rx_mode(netdev);
+		macb_restore_features(bp);

Even WoL may not be available in backup and self-refresh mode (present on SAMA5D2 devices), switching to BSR mode while WoL is enabled may lead to ethernet being unavailable after resuming. Did you manage to test this scenario?

WoL is not available for BSR. But... it's an interesting scenario indeed ;-) Which I didn't test.

I have to think about it and come back with a solution to this. Thanks Claudiu for having found that.

Quoted 11 lines

drivers/net/ethernet/cadence/macb_main.c static int __maybe_unused macb_resume(struct device *dev) · line 4710

Quoted 11 lines
drivers/net/ethernet/cadence/macb_main.c +0 -3
  		rtnl_lock();
  		phylink_start(bp->phylink);
  		rtnl_unlock();
  	}
 
-	macb_init_hw(bp);
-	macb_set_rx_mode(netdev);
-	macb_restore_features(bp);
  	netif_device_attach(netdev);
  	if (bp->ptp_info)
  		bp->ptp_info->ptp_init(netdev);

Best regards,

Signature · 1 line
Nicolas Ferre
#11 lore raw

PATCH v2 7/7 net: macb: Add WoL interrupt support for MACB type of Ethernet controller

In reply to [email protected]

From: Nicolas Ferre <[email protected]>

Handle the Wake-on-Lan interrupt for the Cadence MACB Ethernet controller.
As we do for the GEM version, we handle of WoL interrupt in a specialized interrupt handler for MACB version that is positionned just between suspend() and resume() calls.

Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>

---
Changes in v2:
- Addition of pm_wakeup_event() in WoL IRQ

 drivers/net/ethernet/cadence/macb_main.c | 39 +++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb_main.c +38 -1
index 56ce39dd1cc0..4249be829aaf 100644
@@ -1513,6 +1513,35 @@ static void macb_tx_restart(struct macb_queue *queue)
 	macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
 }
 
+static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
+{
+	struct macb_queue *queue = dev_id;
+	struct macb *bp = queue->bp;
+	u32 status;
+
+	status = queue_readl(queue, ISR);
+
+	if (unlikely(!status))
+		return IRQ_NONE;
+
+	spin_lock(&bp->lock);
+
+	if (status & MACB_BIT(WOL)) {
+		queue_writel(queue, IDR, MACB_BIT(WOL));
+		macb_writel(bp, WOL, 0);
+		netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
+			    (unsigned int)(queue - bp->queues),
+			    (unsigned long)status);
+		if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
+			queue_writel(queue, ISR, MACB_BIT(WOL));
+		pm_wakeup_event(&bp->pdev->dev, 0);
+	}
+
+	spin_unlock(&bp->lock);
+
+	return IRQ_HANDLED;
+}
+
 static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
 {
 	struct macb_queue *queue = dev_id;
@@ -4586,8 +4615,8 @@ static int __maybe_unused macb_suspend(struct device *dev)
 		/* Change interrupt handler and
 		 * Enable WoL IRQ on queue 0
 		 */
+		devm_free_irq(dev, bp->queues[0].irq, bp->queues);
 		if (macb_is_gem(bp)) {
-			devm_free_irq(dev, bp->queues[0].irq, bp->queues);
 			err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
 					       IRQF_SHARED, netdev->name, bp->queues);
 			if (err) {
@@ -4599,6 +4628,14 @@ static int __maybe_unused macb_suspend(struct device *dev)
 			queue_writel(bp->queues, IER, GEM_BIT(WOL));
 			gem_writel(bp, WOL, MACB_BIT(MAG));
 		} else {
+			err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
+					       IRQF_SHARED, netdev->name, bp->queues);
+			if (err) {
+				dev_err(dev,
+					"Unable to request IRQ %d (error %d)\n",
+					bp->queues[0].irq, err);
+				return err;
+			}
 			queue_writel(bp->queues, IER, MACB_BIT(WOL));
 			macb_writel(bp, WOL, MACB_BIT(MAG));
 		}
Signature · 1 line
2.20.1
Reviewed-by on v2 7/7
#12 lore raw

Re: [PATCH v2 7/7] net: macb: Add WoL interrupt support for MACB type of Ethernet controller

Quoted 11 lines · On 4/21/2020 3:41 AM, [email protected] wrote:

From: Nicolas Ferre <[email protected]>

Handle the Wake-on-Lan interrupt for the Cadence MACB Ethernet controller.
As we do for the GEM version, we handle of WoL interrupt in a specialized interrupt handler for MACB version that is positionned just between suspend() and resume() calls.

Cc Claudiu Beznea <[email protected]>
Cc Harini Katakam <[email protected]>
Signature · 1 line
Florian