[PATCH RFC net-next] net: phy: add (*register_phy)() hook to struct mii_bus

Daniel Golle posted 1 patch 2 weeks, 3 days ago
drivers/net/phy/phy_device.c | 8 ++++++++
include/linux/phy.h          | 3 +++
2 files changed, 11 insertions(+)
[PATCH RFC net-next] net: phy: add (*register_phy)() hook to struct mii_bus
Posted by Daniel Golle 2 weeks, 3 days ago
Some MDIO busses require to program PHY polling registers depending on
the PHY type. RealTek switch SoCs are the most prominent example of a
DSA switch which doesn't allow to program MAC speed, duplex and
flow-control settings without using PHY polling to do so.

Hence there is a need to inform the MDIO bus driver that a PHY has been
registered on the bus, as otherwise the bus driver will have to reinvent
and duplicate all the bus scanning logic for Clause-22 and Clause-45
PHYs.

Provide a simple hook in struct mii_bus which is called right after a
PHY has been registered on the bus.

Alternative ways which would allow to do the same things without
having to change any kernel code would of course be very welcome.

Link: https://github.com/openwrt/openwrt/pull/21515#discussion_r2714069716
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
---
 drivers/net/phy/phy_device.c | 8 ++++++++
 include/linux/phy.h          | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index f624218bf3664..97c7b69f7031b 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1130,8 +1130,16 @@ int phy_device_register(struct phy_device *phydev)
 		goto out;
 	}
 
+	if (phydev->mdio.bus->register_phy) {
+		err = phydev->mdio.bus->register_phy(phydev);
+		if (err)
+			goto register_hook_err;
+	}
+
 	return 0;
 
+ register_hook_err:
+	device_del(&phydev->mdio.dev);
  out:
 	/* Assert the reset signal */
 	phy_device_reset(phydev, 1);
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 5972f19af16da..1d38e27d20389 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -377,6 +377,9 @@ struct mii_bus {
 	/** @reset: Perform a reset of the bus */
 	int (*reset)(struct mii_bus *bus);
 
+	/** @register_phy: Called for each PHY detected on the bus */
+	int (*register_phy)(struct phy_device *phydev);
+
 	/** @stats: Statistic counters per device on the bus */
 	struct mdio_bus_stats stats[PHY_MAX_ADDR];
 
-- 
2.52.0
Re: [PATCH RFC net-next] net: phy: add (*register_phy)() hook to struct mii_bus
Posted by Andrew Lunn 2 weeks, 3 days ago
On Wed, Jan 21, 2026 at 09:12:56PM +0000, Daniel Golle wrote:
> Some MDIO busses require to program PHY polling registers depending on
> the PHY type. RealTek switch SoCs are the most prominent example of a
> DSA switch which doesn't allow to program MAC speed, duplex and
> flow-control settings without using PHY polling to do so.
> 
> Hence there is a need to inform the MDIO bus driver that a PHY has been
> registered on the bus, as otherwise the bus driver will have to reinvent
> and duplicate all the bus scanning logic for Clause-22 and Clause-45
> PHYs.
> 
> Provide a simple hook in struct mii_bus which is called right after a
> PHY has been registered on the bus.
> 
> Alternative ways which would allow to do the same things without
> having to change any kernel code would of course be very welcome.

You need to provide a user of this new API as part of the patchset. To
deciding if an API makes sense you often need to see both sides of it.

    Andrew

---
pw-bot: cr