Skip to contentSkip to conversation

net: stmmac: dwmac-motorcomm: shutdown hook and Wake-on-LAN from S5

PATCH In review Magnus Leatti 3 participants 9 messages Nov 20 – Nov 24

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover net: stmmac: dwmac-motorcomm: shutdown hook and Wake-on-LAN from S5

YT6801 accepts "ethtool -s eth0 wol g" today, but ignores magic packets. This series makes S5 Wake-on-LAN work. My patches here touches only the glue.

v1 called stmmac_suspend() from .shutdown and Yao rejected that [1]. This is the design proposed by Yao, remove the device at shutdown, reset the chip and let it arm itself.

Patch 1 just wires .shutdown to the existing remove.

Patch 2 arms WoL: after remove, it resets the chip, restores the cleared packet-wake bit, enables PME and drops to D3hot.

Changes from v1:

- Wake uses the eFuse address, not whatever the driver programmed. r8169 works the same way.
- Magic Packets only.
- D3hot now. No more polling so D3hot is clean.

Tested on a Celeron N3350 mini pc, NIC soldered, net-next at 6964ff21c5d8 with storm fix [2].

  net-next  wol g                   no wake, 3 of 3
  patched   wol g                   no wake on the address in use,
                                    wake on the eFuse address, 5 of 5
  patched   wol g, 15 min off       same, 1 of 1
  patched   wol ug                  same, 1 of 1
  patched   wol u, unicast frames   no wake, 1 of 1
  patched   wol d                   no wake, 1 of 1
  patched   wol g, interface down   no wake, 1 of 1
  patched   wol g, wakeup disabled  no wake, 1 of 1
  patched   wol g, reboot           reboots, link back, 3 of 3
  patched   wol g, kexec            new kernel, link back, 1 of 1
  patched   wol g, hibernate        same, resumes, link back, 2 of 2
  patched   wol g, cable out, in    no wake, 2 of 2

19 patched shutdowns, all clean.

Hibernate on this box ends in a normal power-off (S4 is off in the BIOS), so it goes through .shutdown and arms the same way.

The last row is a true power loss: So if power drops, it just won't wake. Meaning: It needs to be shut down cleanly through the driver again. FWIW the vendor driver does the same: wakes after a normal power-off, sleeps through everything after a power loss. So its a hardware thing and not something a driver can fix.

Giuseppe, your Tested-by was for the v1 design so I couldn't carry it over :(. I'm sorry, could you test again?

Sashiko's nagging on patch 1 of v1 [3] came from calling stmmac_suspend() in .shutdown. v2 doesn't, so it's gone.

BTW stmmac has an old bug here: if a resume fails, NAPI stays off, and then closing the interface just hangs in napi_disable().

Changes in v2:
- .shutdown no longer calls stmmac_suspend(). It removes the device.
- A reset arms the chip, no more OOB WoL or MAC address writes.
- D3hot at power-off, v1 stayed in D0.
- Magic packet only, on the eFuse address.
- The wake condition is read under RTNL.
- Added Suggested-by Yao on both patches.

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

[1] https://lore.kernel.org/netdev/avsTEglKKD_wl_Fi@pie/
[2] https://lore.kernel.org/netdev/[email protected]/
[3] https://lore.kernel.org/netdev/[email protected]/

Magnus Leatti (2):
net: stmmac: dwmac-motorcomm: remove the device at shutdown
net: stmmac: dwmac-motorcomm: support Wake-on-LAN from S5

 .../ethernet/stmicro/stmmac/dwmac-motorcomm.c | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)

base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e

Signature · 1 line
2.56.1
#1 lore raw

PATCH v2 1/2 net: stmmac: dwmac-motorcomm: remove the device at shutdown

No .shutdown here, so reboot, kexec and power-off leave the MAC and DMA running. This chip keeps its registers through S5 too.

Hook .shutdown to stmmac_dvr_remove(): interface closed, DMA and MAC stopped, PHY dropped.

---
 drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +1 -0
index 42f0b11a7b60..63ce2301a0f6 100644
@@ -374,6 +374,7 @@ static struct pci_driver dwmac_motorcomm_pci_driver = {
 	.id_table = dwmac_motorcomm_pci_id_table,
 	.probe = motorcomm_probe,
 	.remove = motorcomm_remove,
+	.shutdown = motorcomm_remove,
 	.driver = {
 		.pm = &stmmac_simple_pm_ops,
 	},
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH net-next v2 1/2] net: stmmac: dwmac-motorcomm: remove the device at shutdown

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch wires .shutdown to the full driver teardown:
motorcomm_remove(), which calls stmmac_dvr_remove().
- [Low] The commit message describes a defect: without .shutdown, reboot, kexec and power-off leave the MAC and DMA running.

--

net: stmmac: dwmac-motorcomm: remove the device at shutdown

This commit sets the .shutdown callback of the Motorcomm YT6801 PCI glue driver to motorcomm_remove(). As a result, stmmac_dvr_remove() runs on reboot, kexec and power-off, so the interface is closed and the DMA and MAC are stopped before the next kernel or power state.

Quoted 2 lines

No .shutdown here, so reboot, kexec and power-off leave the MAC and DMA running. This chip keeps its registers through S5 too.

[Severity: Low]
Should this carry a Fixes: tag?

The problem described here is real for kexec. motorcomm_probe() calls pci_set_master() before motorcomm_reset(), and pci_device_shutdown() only clears bus mastering without resetting the DMA engine.

The dwmac_motorcomm_pci_driver struct without a .shutdown callback came in with:

Fixes 02ff155ea281 ("net: stmmac: Add glue driver for Motorcomm YT6801 ethernet controller")

Without the tag, stable trees that carry this driver won't pick up the change. A backport of this patch as written would also bring in the double teardown described below.

drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c static struct pci_driver dwmac_motorcomm_pci_driver = { · line 377

Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +1 -0
index 42f0b11a7b606..63ce2301a0f69 100644
@@ -374,6 +374,7 @@ static struct pci_driver dwmac_motorcomm_pci_driver = {
 	.id_table = dwmac_motorcomm_pci_id_table,
 	.probe = motorcomm_probe,
 	.remove = motorcomm_remove,
+	.shutdown = motorcomm_remove,

[Severity: Medium]
With this change, can stmmac_dvr_remove() run twice on the same binding?

device_shutdown()->pci_device_shutdown() only calls drv->shutdown(). It does not unbind the driver. device_lock(dev) is released as soon as the callback returns, and drvdata still points to the devm-allocated net_device.

The device is still bound, so a later .remove can still happen. Examples are a sysfs unbind, rmmod, or a pciehp/Thunderbolt surprise removal handled by the parent port. The parent port is shut down after its children, because device_shutdown() walks the device list backwards. Any of these goes through:

pci_device_remove()
motorcomm_remove()
stmmac_dvr_remove()
unregister_netdev(ndev)

stmmac_dvr_remove() is not safe to call twice:

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c:stmmac_dvr_remove() {
    ...
	unregister_netdev(ndev);
    ...
	phylink_destroy(priv->phylink);
    ...
	destroy_workqueue(priv->wq);
	mutex_destroy(&priv->lock);
	mutex_destroy(&priv->est_lock);
	bitmap_free(priv->af_xdp_zc_qps);
    ...
}

It clears none of these pointers and sets no flag to record that teardown has already happened. On the second call, unregister_netdevice_many_notify() would hit this with rtnl_mutex held:

	BUG_ON(dev->reg_state != NETREG_REGISTERED);

With CONFIG_BUG=n, the second call would instead lead to:

- a use-after-free and double free of the phylink in phylink_destroy()
- a use-after-free in destroy_workqueue()
- a double bitmap_free()

The follow-up patch "net: stmmac: dwmac-motorcomm: support Wake-on-LAN from S5" still calls stmmac_dvr_remove() unconditionally from motorcomm_shutdown(). The problem is therefore still present at the end of the series.

For comparison, the shutdown hook in dwmac-sun8i only quiesces the hardware. Could this driver use a shutdown that only quiesces the device instead? That would mean detaching and closing the interface under rtnl, stopping DMA and MAC, and powering down or arming the PHY. Another option would be to make the remove path safe to call twice.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120102754.778283-1-magnusleatti%40kernelriot.com
#3 lore raw

Re: [PATCH net-next v2 1/2] net: stmmac: dwmac-motorcomm: remove the device at shutdown

Quoted 2 lines · On Tue, Nov 24, 2026 at 10:57:44AM +0000, [email protected] wrote:

[Severity: Low]
Should this carry a Fixes: tag?

No. I never saw anything break without the hook, so it's not a fix. net-next, no tag.

Quoted 3 lines

[Severity: Medium]
With this change, can stmmac_dvr_remove() run twice on the same binding?

Only if something unbinds or unplugs the NIC *after* .shutdown ran, in the middle of a power-off. funeth, otx2, nfp VF and ps3_gelic wire .shutdown to their remove the same way, and it's the design that came out of the v1 review [1]. Keeping it.

[1] https://lore.kernel.org/netdev/avsebFtnIASV-KpV@pie/

Magnus

maintainer Reviewed-by on v2 1/2
#4 lore raw

Re: [PATCH net-next v2 1/2] net: stmmac: dwmac-motorcomm: remove the device at shutdown

In reply to Magnus Leatti
Quoted 8 lines · Magnus Leatti

No .shutdown here, so reboot, kexec and power-off leave the MAC and DMA running. This chip keeps its registers through S5 too.

Hook .shutdown to stmmac_dvr_remove(): interface closed, DMA and MAC stopped, PHY dropped.

Maxime

Quoted 16 lines
---
 drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +1 -0
index 42f0b11a7b60..63ce2301a0f6 100644
@@ -374,6 +374,7 @@ static struct pci_driver dwmac_motorcomm_pci_driver = {
 	.id_table = dwmac_motorcomm_pci_id_table,
 	.probe = motorcomm_probe,
 	.remove = motorcomm_remove,
+	.shutdown = motorcomm_remove,
 	.driver = {
 		.pm = &stmmac_simple_pm_ops,
 	},
#5 lore raw

PATCH v2 2/2 net: stmmac: dwmac-motorcomm: support Wake-on-LAN from S5

In reply to Magnus Leatti

"wol g" is accepted, but the chip sleeps right through magic packets in S5: motorcomm_init() turns OOB WoL off and nothing turns it back on at power-off.

Turns out the chip can just arm itself. Hit it with a reset, OOB WoL comes right back on, and about 2-3 s later it has the PHY up, receiver running, the eFuse address loaded into the first MAC filter, and PMT ready for magic packets. Timed that from userspace without a driver bound.

So on a power-off with "wol g": remove, reset, set WOL_CTRL_PKT_EN again because the reset clears it, enable PME, D3hot.

Notes:

- It wakes on the eFuse address, not the software one. Same as r8169.
- Magic packets only. "wol u", reboot, kexec and interface down stop at the remove.
- Hibernation that powers off through .shutdown arms too, and resume copes without a reset. Tried twice: woke on the packet, link back.
- After a power loss (cable out and back in, BIOS set to stay off) it won't wake until the next clean shutdown. The vendor driver does the same, so that's the hardware.

With this the box wakes on a magic packet to the eFuse address, 5 of
5. Without it, 0 of 3.

---
 .../ethernet/stmicro/stmmac/dwmac-motorcomm.c | 38 ++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +37 -1
index 63ce2301a0f6..34abbdb645ec 100644
@@ -12,6 +12,7 @@
 #include <linux/iopoll.h>
 #include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/rtnetlink.h>
 #include <linux/slab.h>
 #include <linux/stmmac.h>
 
@@ -27,6 +28,9 @@
 #define EPHY_CTRL				0x1004
 /* Clearing this bit asserts resets for internal MDIO bus and PHY */
 #define  EPHY_MDIO_PHY_RESET			BIT(0)
+#define WOL_CTRL				0x100c
+/* Magic and remote wake-up packet enable */
+#define  WOL_CTRL_PKT_EN			BIT(1)
 #define OOB_WOL_CTRL				0x1010
 #define  OOB_WOL_CTRL_DIS			BIT(0)
 #define MGMT_INT_CTRL0				0x1100
@@ -363,6 +367,38 @@ static void motorcomm_remove(struct pci_dev *pdev)
 	stmmac_dvr_remove(&pdev->dev);
 }
 
+static void motorcomm_shutdown(struct pci_dev *pdev)
+{
+	struct net_device *ndev = dev_get_drvdata(&pdev->dev);
+	struct stmmac_priv *spriv = netdev_priv(ndev);
+	struct dwmac_motorcomm_priv *priv = spriv->plat->bsp_priv;
+	bool wake;
+	u32 reg;
+
+	/* Userspace is still running and may close the interface */
+	rtnl_lock();
+	wake = system_state == SYSTEM_POWER_OFF && netif_running(ndev) &&
+	       (spriv->wolopts & WAKE_MAGIC) && device_may_wakeup(&pdev->dev);
+	rtnl_unlock();
+
+	/* The netdev is unregistered after this. priv and its mapping stay. */
+	stmmac_dvr_remove(&pdev->dev);
+
+	if (!wake)
+		return;
+
+	/* Reset re-enables OOB WoL; left alone, chip arms itself (eFuse MAC) */
+	motorcomm_reset(priv);
+
+	/* The reset has cleared it. Without it the chip does not wake. */
+	reg = readl(priv->base + WOL_CTRL);
+	reg |= WOL_CTRL_PKT_EN;
+	writel(reg, priv->base + WOL_CTRL);
+
+	pci_wake_from_d3(pdev, true);
+	pci_set_power_state(pdev, PCI_D3hot);
+}
+
 static const struct pci_device_id dwmac_motorcomm_pci_id_table[] = {
 	{ PCI_DEVICE(PCI_VENDOR_ID_MOTORCOMM, 0x6801) },
 	{ },
@@ -374,7 +410,7 @@ static struct pci_driver dwmac_motorcomm_pci_driver = {
 	.id_table = dwmac_motorcomm_pci_id_table,
 	.probe = motorcomm_probe,
 	.remove = motorcomm_remove,
-	.shutdown = motorcomm_remove,
+	.shutdown = motorcomm_shutdown,
 	.driver = {
 		.pm = &stmmac_simple_pm_ops,
 	},
Signature · 1 line
2.56.1
#6 lore raw

Re: [PATCH net-next v2 2/2] net: stmmac: dwmac-motorcomm: support Wake-on-LAN from S5

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c, motorcomm_shutdown() arms S5 Wake-on-LAN even when the eFuse holds no…
- [Medium] motorcomm_shutdown() makes its wake decision under rtnl (lines 379-382), then drops rtnl before stmmac_dvr_remove() ->…
- [Low] motorcomm_shutdown() calls motorcomm_reset() (line 391) and then immediately reads, modifies and writes WOL_CTRL (lines 394-396), with…

--

net: stmmac: dwmac-motorcomm: support Wake-on-LAN from S5

This adds a dedicated .shutdown callback to the Motorcomm YT6801 glue driver. On power-off with magic packet WoL enabled, it removes the stmmac device, then resets the chip so that it re-arms OOB WoL from its eFuse MAC. After that it sets WOL_CTRL_PKT_EN, enables PME and puts the device into D3hot.

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +0 -0
index 63ce2301a0f69..34abbdb645ecb 100644

[ ... ]

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +6 -0
@@ -363,6 +367,38 @@ static void motorcomm_remove(struct pci_dev *pdev)
 	stmmac_dvr_remove(&pdev->dev);
 }
 
+static void motorcomm_shutdown(struct pci_dev *pdev)
+{
+	struct net_device *ndev = dev_get_drvdata(&pdev->dev);
+	struct stmmac_priv *spriv = netdev_priv(ndev);
+	struct dwmac_motorcomm_priv *priv = spriv->plat->bsp_priv;
+	bool wake;

drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c static void motorcomm_remove(struct pci_dev *pdev) · line 385

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +10 -0
+	u32 reg;
+
+	/* Userspace is still running and may close the interface */
+	rtnl_lock();
+	wake = system_state == SYSTEM_POWER_OFF && netif_running(ndev) &&
+	       (spriv->wolopts & WAKE_MAGIC) && device_may_wakeup(&pdev->dev);
+	rtnl_unlock();
+
+	/* The netdev is unregistered after this. priv and its mapping stay. */
+	stmmac_dvr_remove(&pdev->dev);

[Severity: Medium]
Can wake become stale between rtnl_unlock() and stmmac_dvr_remove()?

The comment above says userspace is still running, and device_shutdown() does not freeze tasks. Once rtnl is dropped, another CPU can take it and run "ip link set down" (dev_close()) or "ethtool -s ethX wol d":

phylink_ethtool_set_wol()
stmmac_mac_wol_set()

    device_set_wakeup_enable(priv->device, !!wolopts);
    ...
    priv->wolopts = wolopts;

Writes to the power/wakeup sysfs attribute don't take rtnl at all.

In each of these cases wake still holds the earlier value of true. The code below would then still reset the chip, set WOL_CTRL_PKT_EN and call pci_wake_from_d3(pdev, true).

Could the machine then power on from a magic packet after WoL was turned off or the interface was taken down? That seems to go against this part of the commit message:

- Magic packets only. "wol u", reboot, kexec and interface down stop at the remove.

Would it help to call netif_device_detach() under rtnl before unlocking, or to check the wakeup settings again after the netdev is unregistered?

drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c static void motorcomm_remove(struct pci_dev *pdev) · line 391

Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +6 -0
+
+	if (!wake)
+		return;
+
+	/* Reset re-enables OOB WoL; left alone, chip arms itself (eFuse MAC) */
+	motorcomm_reset(priv);

[Severity: Medium]
What happens here on boards whose eFuse holds no MAC address?

motorcomm_probe() already handles that case by falling back to a random address:

drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c:motorcomm_probe() {
    ...
	ret = motorcomm_efuse_read_mac(&pdev->dev, priv, res.mac);
	if (ret == -ENOENT) {
		dev_warn(&pdev->dev, "eFuse contains no valid MAC address\n");
		dev_warn(&pdev->dev, "fallback to random MAC address\n");

		eth_random_addr(res.mac);
    ...
}

That result isn't stored anywhere, and the wake decision in motorcomm_shutdown() doesn't check for it.

The commit message says that after this reset the chip loads the eFuse address into the first MAC filter and wakes only on that address. With an empty eFuse there is no address to load.

Wake is still armed, and ethtool still reports "Wake-on: g". A magic packet sent to the netdev's address can't match, because that address is random and changes on every boot. The user gets no warning.

Should the shutdown path skip arming, or at least warn, when probe fell back to a random MAC?

The commit message also says:

- It wakes on the eFuse address, not the software one. Same as r8169.

That is only true when the eFuse is programmed. rtl_shutdown() writes the reported permanent address back into the receive filter before arming:

drivers/net/ethernet/realtek/r8169_main.c:rtl_shutdown() {
    ...
	/* Restore original MAC address */
	rtl_rar_set(tp, tp->dev->perm_addr);

	if (system_state == SYSTEM_POWER_OFF && !tp->dash_enabled)
		pci_prepare_to_sleep(pdev);
}

So r8169 always wakes on the address it reports as permanent, including a random fallback. Could the r8169 comparison be qualified?

drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c static void motorcomm_remove(struct pci_dev *pdev) · line 396

Quoted 5 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +5 -0
+
+	/* The reset has cleared it. Without it the chip does not wake. */
+	reg = readl(priv->base + WOL_CTRL);
+	reg |= WOL_CTRL_PKT_EN;
+	writel(reg, priv->base + WOL_CTRL);

[Severity: Low]
Is it safe to change WOL_CTRL immediately after motorcomm_reset(), with no delay?

motorcomm_probe() does the same reset and then waits:

	motorcomm_reset(priv);

	/*
	 * After system reset, the eFuse controller needs time to load
	 * its internal data. Without this delay, eFuse reads return
	 * all zeros, causing MAC address detection to fail.
	 */
	usleep_range(2000, 5000);

The commit message also says the chip keeps changing its own state for about 2-3 s after the reset.

On some boards that post-reset load might write WOL_CTRL, for example through an eFuse patch entry. Could it then overwrite WOL_CTRL_PKT_EN, so the device powers off with packet wake disabled?

The 5 of 5 result shows this works on the board that was tested. Even so, should this path use the same post-reset delay as probe, or read the register back after writing it?

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +4 -0
+
+	pci_wake_from_d3(pdev, true);
+	pci_set_power_state(pdev, PCI_D3hot);
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120102754.778283-1-magnusleatti%40kernelriot.com
#7 lore raw

Re: [PATCH net-next v2 2/2] net: stmmac: dwmac-motorcomm: support Wake-on-LAN from S5

Quoted 2 lines · On Tue, Nov 24, 2026 at 10:57:46AM +0000, [email protected] wrote:

[Severity: Medium]
Can wake become stale between rtnl_unlock() and stmmac_dvr_remove()?

Yes. unregister_netdev() takes RTNL, so I can't hold it across remove. And power/wakeup doesn't take RTNL anyway. Worst case: someone disables WoL *during* power-off and it stays armed.

Quoted 2 lines

[Severity: Medium]
What happens here on boards whose eFuse holds no MAC address?

I don't know, and I can't test it: my board has the eFuse programmed. Probe already warns about the random MAC on those boards.

Quoted 3 lines

[Severity: Low]
Is it safe to change WOL_CTRL immediately after motorcomm_reset(), with no delay?

[...]

Quoted 3 lines

On some boards that post-reset load might write WOL_CTRL, for example through an eFuse patch entry. Could it then overwrite WOL_CTRL_PKT_EN, so the device powers off with packet wake disabled?

That's a guess. The delay in probe is for the eFuse reads, and the write sticks here: without that bit the chip doesn't wake, and it woke 9 of 9.

Magnus

maintainer Reviewed-by on v2 2/2
#8 lore raw

Re: [PATCH net-next v2 2/2] net: stmmac: dwmac-motorcomm: support Wake-on-LAN from S5

In reply to Magnus Leatti
Quoted 24 lines · Magnus Leatti

"wol g" is accepted, but the chip sleeps right through magic packets in S5: motorcomm_init() turns OOB WoL off and nothing turns it back on at power-off.

Turns out the chip can just arm itself. Hit it with a reset, OOB WoL comes right back on, and about 2-3 s later it has the PHY up, receiver running, the eFuse address loaded into the first MAC filter, and PMT ready for magic packets. Timed that from userspace without a driver bound.

So on a power-off with "wol g": remove, reset, set WOL_CTRL_PKT_EN again because the reset clears it, enable PME, D3hot.

Notes:

- It wakes on the eFuse address, not the software one. Same as r8169.
- Magic packets only. "wol u", reboot, kexec and interface down stop at the remove.
- Hibernation that powers off through .shutdown arms too, and resume copes without a reset. Tried twice: woke on the packet, link back.
- After a power loss (cable out and back in, BIOS set to stay off) it won't wake until the next clean shutdown. The vendor driver does the same, so that's the hardware.

Quoted 5 lines

With this the box wakes on a magic packet to the eFuse address, 5 of
5. Without it, 0 of 3.

My PCI-fu isn't that great, but LGTM. I would have liked to test, but my motorcomm PCIe card let out the magic smoke yesterday :(

Maxime

Quoted 74 lines
---
 .../ethernet/stmicro/stmmac/dwmac-motorcomm.c | 38 ++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
drivers/net/ethernet/stmicro/stmmac/dwmac-motorcomm.c +37 -1
index 63ce2301a0f6..34abbdb645ec 100644
@@ -12,6 +12,7 @@
 #include <linux/iopoll.h>
 #include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/rtnetlink.h>
 #include <linux/slab.h>
 #include <linux/stmmac.h>
 
@@ -27,6 +28,9 @@
 #define EPHY_CTRL				0x1004
 /* Clearing this bit asserts resets for internal MDIO bus and PHY */
 #define  EPHY_MDIO_PHY_RESET			BIT(0)
+#define WOL_CTRL				0x100c
+/* Magic and remote wake-up packet enable */
+#define  WOL_CTRL_PKT_EN			BIT(1)
 #define OOB_WOL_CTRL				0x1010
 #define  OOB_WOL_CTRL_DIS			BIT(0)
 #define MGMT_INT_CTRL0				0x1100
@@ -363,6 +367,38 @@ static void motorcomm_remove(struct pci_dev *pdev)
 	stmmac_dvr_remove(&pdev->dev);
 }
 
+static void motorcomm_shutdown(struct pci_dev *pdev)
+{
+	struct net_device *ndev = dev_get_drvdata(&pdev->dev);
+	struct stmmac_priv *spriv = netdev_priv(ndev);
+	struct dwmac_motorcomm_priv *priv = spriv->plat->bsp_priv;
+	bool wake;
+	u32 reg;
+
+	/* Userspace is still running and may close the interface */
+	rtnl_lock();
+	wake = system_state == SYSTEM_POWER_OFF && netif_running(ndev) &&
+	       (spriv->wolopts & WAKE_MAGIC) && device_may_wakeup(&pdev->dev);
+	rtnl_unlock();
+
+	/* The netdev is unregistered after this. priv and its mapping stay. */
+	stmmac_dvr_remove(&pdev->dev);
+
+	if (!wake)
+		return;
+
+	/* Reset re-enables OOB WoL; left alone, chip arms itself (eFuse MAC) */
+	motorcomm_reset(priv);
+
+	/* The reset has cleared it. Without it the chip does not wake. */
+	reg = readl(priv->base + WOL_CTRL);
+	reg |= WOL_CTRL_PKT_EN;
+	writel(reg, priv->base + WOL_CTRL);
+
+	pci_wake_from_d3(pdev, true);
+	pci_set_power_state(pdev, PCI_D3hot);
+}
+
 static const struct pci_device_id dwmac_motorcomm_pci_id_table[] = {
 	{ PCI_DEVICE(PCI_VENDOR_ID_MOTORCOMM, 0x6801) },
 	{ },
@@ -374,7 +410,7 @@ static struct pci_driver dwmac_motorcomm_pci_driver = {
 	.id_table = dwmac_motorcomm_pci_id_table,
 	.probe = motorcomm_probe,
 	.remove = motorcomm_remove,
-	.shutdown = motorcomm_remove,
+	.shutdown = motorcomm_shutdown,
 	.driver = {
 		.pm = &stmmac_simple_pm_ops,
 	},