Skip to contentSkip to conversation

net: phy: smsc: add WoL support to LAN8740/LAN8742 PHYs

PATCH Superseded [email protected] 3 participants 4 messages Jun 21, 2023 – Jun 27, 2023

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 net: phy: smsc: add WoL support to LAN8740/LAN8742 PHYs

From: Tristram Ha <[email protected]>

Microchip LAN8740/LAN8742 PHYs support basic unicast, broadcast, and Magic Packet WoL. They have one pattern filter matching up to 128 bytes of frame data, which can be used to implement ARP or multicast WoL.

ARP WoL matches any ARP frame with broadcast address.

Multicast WoL matches any multicast frame.

---
v3
- Do not try to get IPv4 and IPv6 address from the driver
- As a result only generic ARP and multicast support are provided

v2
- use in_dev_put() only when IP support is enabled

v1
- use in_dev_get() to retrieve IP address to avoid compiler warning
- use ipv6_get_lladdr() to retrieve IPv6 address
- use that function only when IPv6 support is enabled
- export that function in addrconf.c
- program the MAC address in a loop
- always set datalen in lan874x_chk_wol_pattern()
- add spaces around "<<"
- select CRC16 in Kconfig as crc16() is used in driver

 drivers/net/phy/Kconfig |   1 +
 drivers/net/phy/smsc.c  | 262 +++++++++++++++++++++++++++++++++++++++-
 include/linux/smscphy.h |  34 ++++++
 3 files changed, 295 insertions(+), 2 deletions(-)
drivers/net/phy/Kconfig +1 -0
index 78e6981650d9..c59fb072343b 100644
@@ -344,6 +344,7 @@ config ROCKCHIP_PHY
 
 config SMSC_PHY
 	tristate "SMSC PHYs"
+	select CRC16
 	help
 	  Currently supports the LAN83C185, LAN8187 and LAN8700 PHYs
 
drivers/net/phy/smsc.c +260 -2

304 lines not on this page · raw message

include/linux/smscphy.h +34 -0
index e1c88627755a..1a6a851d2cf8 100644
@@ -38,4 +38,38 @@ int smsc_phy_set_tunable(struct phy_device *phydev,
 			 struct ethtool_tunable *tuna, const void *data);
 int smsc_phy_probe(struct phy_device *phydev);
 
+#define MII_LAN874X_PHY_MMD_WOL_WUCSR		0x8010
+#define MII_LAN874X_PHY_MMD_WOL_WUF_CFGA	0x8011
+#define MII_LAN874X_PHY_MMD_WOL_WUF_CFGB	0x8012
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK0	0x8021
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK1	0x8022
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK2	0x8023
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK3	0x8024
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK4	0x8025
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK5	0x8026
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK6	0x8027
+#define MII_LAN874X_PHY_MMD_WOL_WUF_MASK7	0x8028
+#define MII_LAN874X_PHY_MMD_WOL_RX_ADDRA	0x8061
+#define MII_LAN874X_PHY_MMD_WOL_RX_ADDRB	0x8062
+#define MII_LAN874X_PHY_MMD_WOL_RX_ADDRC	0x8063
+#define MII_LAN874X_PHY_MMD_MCFGR		0x8064
+
+#define MII_LAN874X_PHY_PME1_SET		(2 << 13)
+#define MII_LAN874X_PHY_PME2_SET		(2 << 11)
+#define MII_LAN874X_PHY_PME_SELF_CLEAR		BIT(9)
+#define MII_LAN874X_PHY_WOL_PFDA_FR		BIT(7)
+#define MII_LAN874X_PHY_WOL_WUFR		BIT(6)
+#define MII_LAN874X_PHY_WOL_MPR			BIT(5)
+#define MII_LAN874X_PHY_WOL_BCAST_FR		BIT(4)
+#define MII_LAN874X_PHY_WOL_PFDAEN		BIT(3)
+#define MII_LAN874X_PHY_WOL_WUEN		BIT(2)
+#define MII_LAN874X_PHY_WOL_MPEN		BIT(1)
+#define MII_LAN874X_PHY_WOL_BCSTEN		BIT(0)
+
+#define MII_LAN874X_PHY_WOL_FILTER_EN		BIT(15)
+#define MII_LAN874X_PHY_WOL_FILTER_MCASTTEN	BIT(9)
+#define MII_LAN874X_PHY_WOL_FILTER_BCSTEN	BIT(8)
+
+#define MII_LAN874X_PHY_PME_SELF_CLEAR_DELAY	0x1000 /* 81 milliseconds */
+
 #endif /* __LINUX_SMSCPHY_H__ */
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 1 line · On Wed, 2023-06-21 at 16:00 -0700, [email protected] wrote:
(quoted diff) +0 -0
@@ -258,6 +265,249 @@ int lan87xx_read_status(struct phy_device *phydev)

[...]

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev) · line 335

Quoted 2 lines
drivers/net/phy/smsc.c +2 -0
+static int lan874x_chk_wol_pattern(const u8 pattern[], const u16 *mask,
+				   u8 len, u8 *data, u8 *datalen)

I think it would be nice adding some comments here describing the implemented logic, for future memory.

Quoted 26 lines
drivers/net/phy/smsc.c +26 -0
+{
+	size_t i, j, k;
+	int ret = 0;
+	u16 bits;
+
+	i = 0;
+	k = 0;
+	while (len > 0) {
+		bits = *mask;
+		for (j = 0; j < 16; j++, i++, len--) {
+			/* No more pattern. */
+			if (!len) {
+				/* The rest of bitmap is not empty. */
+				if (bits)
+					ret = i + 1;
+				break;
+			}
+			if (bits & 1)
+				data[k++] = pattern[i];
+			bits >>= 1;
+		}
+		mask++;
+	}
+	*datalen = k;
+	return ret;
+}

[...]

Quoted 45 lines
drivers/net/phy/smsc.c +45 -0
+static int lan874x_set_wol(struct phy_device *phydev,
+			   struct ethtool_wolinfo *wol)
+{
+	struct net_device *ndev = phydev->attached_dev;
+	struct smsc_phy_priv *priv = phydev->priv;
+	u16 val, val_wucsr;
+	u8 data[128];
+	u8 datalen;
+	int rc;
+
+	if (wol->wolopts & WAKE_PHY)
+		return -EOPNOTSUPP;
+
+	/* lan874x has only one WoL filter pattern */
+	if ((wol->wolopts & (WAKE_ARP | WAKE_MCAST)) ==
+	    (WAKE_ARP | WAKE_MCAST)) {
+		phydev_info(phydev,
+			    "lan874x WoL supports one of ARP|MCAST at a time\n");
+		return -EOPNOTSUPP;
+	}
+
+	rc = phy_read_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR);
+	if (rc < 0)
+		return rc;
+
+	val_wucsr = rc;
+
+	if (wol->wolopts & WAKE_UCAST)
+		val_wucsr |= MII_LAN874X_PHY_WOL_PFDAEN;
+	else
+		val_wucsr &= ~MII_LAN874X_PHY_WOL_PFDAEN;
+
+	if (wol->wolopts & WAKE_BCAST)
+		val_wucsr |= MII_LAN874X_PHY_WOL_BCSTEN;
+	else
+		val_wucsr &= ~MII_LAN874X_PHY_WOL_BCSTEN;
+
+	if (wol->wolopts & WAKE_MAGIC)
+		val_wucsr |= MII_LAN874X_PHY_WOL_MPEN;
+	else
+		val_wucsr &= ~MII_LAN874X_PHY_WOL_MPEN;
+
+	/* Need to use pattern matching */
+	if (wol->wolopts & (WAKE_ARP | WAKE_MCAST))
+		val_wucsr |= MII_LAN874X_PHY_WOL_WUEN;

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev) · line 455

Quoted 10 lines
drivers/net/phy/smsc.c +10 -0
+	else
+		val_wucsr &= ~MII_LAN874X_PHY_WOL_WUEN;
+
+	if (wol->wolopts & WAKE_ARP) {
+		u8 pattern[14] = {
+			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+			0x08, 0x06 };
+		u16 mask[1] = { 0x303F };
+		u8 len = 14;

'len' is never changed, you could use instead some macro with a meaningful name.

Quoted 9 lines
drivers/net/phy/smsc.c +9 -0
+
+		rc = lan874x_chk_wol_pattern(pattern, mask, len, data,
+					     &datalen);
+		if (rc)
+			phydev_dbg(phydev, "pattern not valid at %d\n", rc);
+
+		/* Need to match broadcast destination address. */
+		val = MII_LAN874X_PHY_WOL_FILTER_BCSTEN;
+		rc = lan874x_set_wol_pattern(phydev, val, data, datalen, mask,

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev) · line 474

Quoted 10 lines
drivers/net/phy/smsc.c +10 -0
+					     len);
+		if (rc < 0)
+			return rc;
+		priv->wol_arp = true;
+	}
+
+	if (wol->wolopts & WAKE_MCAST) {
+		u8 pattern[6] = { 0x33, 0x33, 0xFF, 0x00, 0x00, 0x00 };
+		u16 mask[1] = { 0x0007 };
+		u8 len = 0;

Same here, but now 'len' is 0, which makes the following lan874x_chk_wol_pattern() a no-op. Was '3' in the initial revision, is the above an unintentional change?!? Otherwise it would be clearer avoid the lan874x_set_wol_pattern() call.

Thanks!

Paolo

maintainer
#2 lore raw
Quoted 10 lines

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev) · line 455

Quoted 7 lines
drivers/net/phy/smsc.c +7 -0
+	if (wol->wolopts & WAKE_ARP) {
+		u8 pattern[14] = {
+			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+			0x08, 0x06 };
+		u16 mask[1] = { 0x303F };
+		u8 len = 14;

'len' is never changed, you could use instead some macro with a meaningful name.

This looks to be ETH_HLEN.

Andrew

#3 lore raw
In reply to Paolo Abeni
Quoted 114 lines
Quoted 1 line
(quoted diff) +0 -0
@@ -258,6 +265,249 @@ int lan87xx_read_status(struct phy_device *phydev)

[...]

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev) · line 335

Quoted 2 lines
drivers/net/phy/smsc.c +2 -0
+static int lan874x_chk_wol_pattern(const u8 pattern[], const u16 *mask,
+                                u8 len, u8 *data, u8 *datalen)

I think it would be nice adding some comments here describing the implemented logic, for future memory.

Quoted 26 lines
drivers/net/phy/smsc.c +26 -0
+{
+     size_t i, j, k;
+     int ret = 0;
+     u16 bits;
+
+     i = 0;
+     k = 0;
+     while (len > 0) {
+             bits = *mask;
+             for (j = 0; j < 16; j++, i++, len--) {
+                     /* No more pattern. */
+                     if (!len) {
+                             /* The rest of bitmap is not empty. */
+                             if (bits)
+                                     ret = i + 1;
+                             break;
+                     }
+                     if (bits & 1)
+                             data[k++] = pattern[i];
+                     bits >>= 1;
+             }
+             mask++;
+     }
+     *datalen = k;
+     return ret;
+}

[...]

Quoted 12 lines
drivers/net/phy/smsc.c +12 -0
+static int lan874x_set_wol(struct phy_device *phydev,
+                        struct ethtool_wolinfo *wol)
+{
+     struct net_device *ndev = phydev->attached_dev;
+     struct smsc_phy_priv *priv = phydev->priv;
+     u16 val, val_wucsr;
+     u8 data[128];
+     u8 datalen;
+     int rc;
+
+     if (wol->wolopts & WAKE_PHY)
+             return -EOPNOTSUPP;

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev)

Quoted 10 lines
drivers/net/phy/smsc.c +10 -0
+
+     /* lan874x has only one WoL filter pattern */
+     if ((wol->wolopts & (WAKE_ARP | WAKE_MCAST)) ==
+         (WAKE_ARP | WAKE_MCAST)) {
+             phydev_info(phydev,
+                         "lan874x WoL supports one of ARP|MCAST at a time\n");
+             return -EOPNOTSUPP;
+     }
+
+     rc = phy_read_mmd(phydev, MDIO_MMD_PCS,

MII_LAN874X_PHY_MMD_WOL_WUCSR);

Quoted 23 lines
drivers/net/phy/smsc.c +23 -0
+     if (rc < 0)
+             return rc;
+
+     val_wucsr = rc;
+
+     if (wol->wolopts & WAKE_UCAST)
+             val_wucsr |= MII_LAN874X_PHY_WOL_PFDAEN;
+     else
+             val_wucsr &= ~MII_LAN874X_PHY_WOL_PFDAEN;
+
+     if (wol->wolopts & WAKE_BCAST)
+             val_wucsr |= MII_LAN874X_PHY_WOL_BCSTEN;
+     else
+             val_wucsr &= ~MII_LAN874X_PHY_WOL_BCSTEN;
+
+     if (wol->wolopts & WAKE_MAGIC)
+             val_wucsr |= MII_LAN874X_PHY_WOL_MPEN;
+     else
+             val_wucsr &= ~MII_LAN874X_PHY_WOL_MPEN;
+
+     /* Need to use pattern matching */
+     if (wol->wolopts & (WAKE_ARP | WAKE_MCAST))
+             val_wucsr |= MII_LAN874X_PHY_WOL_WUEN;

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev) · line 455

Quoted 10 lines
drivers/net/phy/smsc.c +10 -0
+     else
+             val_wucsr &= ~MII_LAN874X_PHY_WOL_WUEN;
+
+     if (wol->wolopts & WAKE_ARP) {
+             u8 pattern[14] = {
+                     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                     0x08, 0x06 };
+             u16 mask[1] = { 0x303F };
+             u8 len = 14;

'len' is never changed, you could use instead some macro with a meaningful name.

Quoted 16 lines
drivers/net/phy/smsc.c +16 -0
+
+             rc = lan874x_chk_wol_pattern(pattern, mask, len, data,
+                                          &datalen);
+             if (rc)
+                     phydev_dbg(phydev, "pattern not valid at %d\n", rc);
+
+             /* Need to match broadcast destination address. */
+             val = MII_LAN874X_PHY_WOL_FILTER_BCSTEN;
+             rc = lan874x_set_wol_pattern(phydev, val, data, datalen, mask,
+                                          len);
+             if (rc < 0)
+                     return rc;
+             priv->wol_arp = true;
+     }
+
+     if (wol->wolopts & WAKE_MCAST) {
Quoted 8 lines

drivers/net/phy/smsc.c int lan87xx_read_status(struct phy_device *phydev) · line 474

Quoted 3 lines
drivers/net/phy/smsc.c +3 -0
+             u8 pattern[6] = { 0x33, 0x33, 0xFF, 0x00, 0x00, 0x00 };
+             u16 mask[1] = { 0x0007 };
+             u8 len = 0;

Same here, but now 'len' is 0, which makes the following lan874x_chk_wol_pattern() a no-op. Was '3' in the initial revision, is the above an unintentional change?!? Otherwise it would be clearer avoid the lan874x_set_wol_pattern() call.

Another patch will be submitted to address your issues.