[PATCH net-next 2/3] net: phy: broadcom: Add support for WAKE_FILTER

From: Florian Fainelli
Date: Tue May 16 2023 - 19:17:40 EST


Since the PHY is capable of matching any arbitrary Ethernet MAC
destination as a programmable wake-up pattern, add support for doing
that using the WAKE_FILTER and ethtool::rxnfc API. For instance, in
order to wake-up from the Ethernet MAC address corresponding to the IPv4
multicast IP address of 224.0.0.1, one could do:

ethtool -N eth0 flow-type ether dst 01:00:5e:00:00:fb loc 0 action -2
ethtool -n eth0
Total 1 rules

Filter: 0
Flow Type: Raw Ethernet
Src MAC addr: 00:00:00:00:00:00 mask: FF:FF:FF:FF:FF:FF
Dest MAC addr: 01:00:5E:00:00:FB mask: 00:00:00:00:00:00
Ethertype: 0x0 mask: 0xFFFF
Action: Wake-on-LAN
ethtool -s eth0 wol f

Signed-off-by: Florian Fainelli <florian.fainelli@xxxxxxxxxxxx>
---
drivers/net/phy/bcm-phy-lib.c | 147 +++++++++++++++++++++++++++++++++-
drivers/net/phy/bcm-phy-lib.h | 6 ++
drivers/net/phy/broadcom.c | 15 ++++
3 files changed, 167 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/bcm-phy-lib.c b/drivers/net/phy/bcm-phy-lib.c
index 5603d0a9ce96..546c21ce9775 100644
--- a/drivers/net/phy/bcm-phy-lib.c
+++ b/drivers/net/phy/bcm-phy-lib.c
@@ -822,7 +822,8 @@ EXPORT_SYMBOL_GPL(bcm_phy_cable_test_get_status_rdb);
WAKE_MCAST | \
WAKE_BCAST | \
WAKE_MAGIC | \
- WAKE_MAGICSECURE)
+ WAKE_MAGICSECURE | \
+ WAKE_FILTER)

int bcm_phy_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
{
@@ -876,6 +877,12 @@ int bcm_phy_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
ctl &= ~BCM54XX_WOL_DIR_PKT_EN;
ctl &= ~(BCM54XX_WOL_SECKEY_OPT_MASK << BCM54XX_WOL_SECKEY_OPT_SHIFT);

+ /* For WAKE_FILTER, we have already programmed the desired MAC DA
+ * and associated mask by the time we get there.
+ */
+ if (wol->wolopts & WAKE_FILTER)
+ goto program_ctl;
+
/* When using WAKE_MAGIC, we program the magic pattern filter to match
* the device's MAC address and we accept any MAC DA in the Ethernet
* frame.
@@ -930,6 +937,7 @@ int bcm_phy_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
return ret;
}

+program_ctl:
if (wol->wolopts & WAKE_MAGICSECURE) {
ctl |= BCM54XX_WOL_SECKEY_OPT_6B <<
BCM54XX_WOL_SECKEY_OPT_SHIFT;
@@ -1034,6 +1042,143 @@ irqreturn_t bcm_phy_wol_isr(int irq, void *dev_id)
}
EXPORT_SYMBOL_GPL(bcm_phy_wol_isr);

+static int bcm_phy_get_rule(struct phy_device *phydev,
+ struct ethtool_rxnfc *nfc,
+ int loc)
+{
+ u8 da[ETH_ALEN];
+ unsigned int i;
+ int ret;
+
+ if (loc != 0)
+ return -EINVAL;
+
+ memset(nfc, 0, sizeof(*nfc));
+ nfc->flow_type = ETHER_FLOW;
+ nfc->fs.flow_type = ETHER_FLOW;
+
+ for (i = 0; i < sizeof(da) / 2; i++) {
+ ret = bcm_phy_read_exp(phydev,
+ BCM54XX_WOL_MPD_DATA2(2 - i));
+ if (ret < 0)
+ return ret;
+
+ da[i * 2] = ret >> 8;
+ da[i * 2 + 1] = ret & 0xff;
+ }
+ ether_addr_copy(nfc->fs.h_u.ether_spec.h_dest, da);
+
+ for (i = 0; i < sizeof(da) / 2; i++) {
+ ret = bcm_phy_read_exp(phydev,
+ BCM54XX_WOL_MASK(2 - i));
+ if (ret < 0)
+ return ret;
+
+ da[i * 2] = ret >> 8;
+ da[i * 2 + 1] = ret & 0xff;
+ }
+ ether_addr_copy(nfc->fs.m_u.ether_spec.h_dest, da);
+
+ nfc->fs.ring_cookie = RX_CLS_FLOW_WAKE;
+ nfc->fs.location = 0;
+
+ return 0;
+}
+
+static int bcm_phy_set_rule(struct phy_device *phydev,
+ struct ethtool_rxnfc *nfc)
+{
+ int ret = -EOPNOTSUPP;
+ unsigned int i;
+ const u8 *da;
+
+ /* We support only matching on the MAC DA, reject anything else */
+ if (nfc->fs.ring_cookie != RX_CLS_FLOW_WAKE ||
+ nfc->fs.location != 0 ||
+ nfc->fs.flow_type != ETHER_FLOW ||
+ nfc->fs.h_u.ether_spec.h_proto ||
+ !is_zero_ether_addr(nfc->fs.h_u.ether_spec.h_source) ||
+ nfc->fs.m_u.ether_spec.h_proto ||
+ !is_zero_ether_addr(nfc->fs.m_u.ether_spec.h_source))
+ return ret;
+
+ da = nfc->fs.h_u.ether_spec.h_dest;
+ for (i = 0; i < ETH_ALEN / 2; i++) {
+ ret = bcm_phy_write_exp(phydev,
+ BCM54XX_WOL_MPD_DATA2(2 - i),
+ da[i * 2] << 8 | da[i * 2 + 1]);
+ if (ret < 0)
+ return ret;
+ }
+
+ da = nfc->fs.m_u.ether_spec.h_dest;
+ for (i = 0; i < ETH_ALEN / 2; i++) {
+ ret = bcm_phy_write_exp(phydev,
+ BCM54XX_WOL_MASK(2 - i),
+ da[i * 2] << 8 | da[i * 2 + 1]);
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+int bcm_phy_get_rxnfc(struct phy_device *phydev,
+ struct ethtool_rxnfc *cmd, u32 *rule_locs)
+{
+ int err = 0;
+
+ switch (cmd->cmd) {
+ case ETHTOOL_GRXCLSRLCNT:
+ cmd->rule_cnt = 1;
+ cmd->data = 1 | RX_CLS_LOC_SPECIAL;
+ break;
+ case ETHTOOL_GRXCLSRULE:
+ err = bcm_phy_get_rule(phydev, cmd, cmd->fs.location);
+ break;
+ case ETHTOOL_GRXCLSRLALL:
+ rule_locs[0] = 0;
+ cmd->rule_cnt = 1;
+ cmd->data = 1;
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ break;
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(bcm_phy_get_rxnfc);
+
+int bcm_phy_set_rxnfc(struct phy_device *phydev,
+ struct ethtool_rxnfc *cmd,
+ bool *installed)
+{
+ int err = 0;
+
+ switch (cmd->cmd) {
+ case ETHTOOL_SRXCLSRLINS:
+ err = bcm_phy_set_rule(phydev, cmd);
+ if (err)
+ return err;
+
+ *installed = true;
+ break;
+ case ETHTOOL_SRXCLSRLDEL:
+ if (cmd->fs.location != 0)
+ return err;
+
+ *installed = false;
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ break;
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(bcm_phy_set_rxnfc);
+
MODULE_DESCRIPTION("Broadcom PHY Library");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Broadcom Corporation");
diff --git a/drivers/net/phy/bcm-phy-lib.h b/drivers/net/phy/bcm-phy-lib.h
index 2f30ce0cab0e..4881ea34e99c 100644
--- a/drivers/net/phy/bcm-phy-lib.h
+++ b/drivers/net/phy/bcm-phy-lib.h
@@ -118,4 +118,10 @@ int bcm_phy_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
void bcm_phy_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
irqreturn_t bcm_phy_wol_isr(int irq, void *dev_id);

+int bcm_phy_get_rxnfc(struct phy_device *phydev,
+ struct ethtool_rxnfc *nfc, u32 *rule_locs);
+int bcm_phy_set_rxnfc(struct phy_device *phydev,
+ struct ethtool_rxnfc *nfc,
+ bool *installed);
+
#endif /* _LINUX_BCM_PHY_LIB_H */
diff --git a/drivers/net/phy/broadcom.c b/drivers/net/phy/broadcom.c
index 822c8b01dc53..b4a8aba7d5ef 100644
--- a/drivers/net/phy/broadcom.c
+++ b/drivers/net/phy/broadcom.c
@@ -36,6 +36,7 @@ struct bcm54xx_phy_priv {
struct bcm_ptp_private *ptp;
int wake_irq;
bool wake_irq_enabled;
+ bool wake_filter_installed;
};

static bool bcm54xx_phy_can_wakeup(struct phy_device *phydev)
@@ -860,6 +861,8 @@ static int brcm_fet_suspend(struct phy_device *phydev)
static void bcm54xx_phy_get_wol(struct phy_device *phydev,
struct ethtool_wolinfo *wol)
{
+ struct bcm54xx_phy_priv *priv = phydev->priv;
+
/* We cannot wake-up if we do not have a dedicated PHY interrupt line
* or an out of band GPIO descriptor for wake-up. Zeroing
* wol->supported allows the caller (MAC driver) to play through and
@@ -871,6 +874,8 @@ static void bcm54xx_phy_get_wol(struct phy_device *phydev,
}

bcm_phy_get_wol(phydev, wol);
+ if (priv->wake_filter_installed)
+ wol->wolopts |= WAKE_FILTER;
}

static int bcm54xx_phy_set_wol(struct phy_device *phydev,
@@ -893,6 +898,14 @@ static int bcm54xx_phy_set_wol(struct phy_device *phydev,
return 0;
}

+static int bcm54xx_phy_set_rxnfc(struct phy_device *phydev,
+ struct ethtool_rxnfc *cmd)
+{
+ struct bcm54xx_phy_priv *priv = phydev->priv;
+
+ return bcm_phy_set_rxnfc(phydev, cmd, &priv->wake_filter_installed);
+}
+
static int bcm54xx_phy_probe(struct phy_device *phydev)
{
struct bcm54xx_phy_priv *priv;
@@ -1031,6 +1044,8 @@ static struct phy_driver broadcom_drivers[] = {
.resume = bcm54xx_resume,
.get_wol = bcm54xx_phy_get_wol,
.set_wol = bcm54xx_phy_set_wol,
+ .get_rxnfc = bcm_phy_get_rxnfc,
+ .set_rxnfc = bcm54xx_phy_set_rxnfc,
}, {
.phy_id = PHY_ID_BCM5461,
.phy_id_mask = 0xfffffff0,
--
2.34.1

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature