]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net: phy: add iterator mdiobus_for_each_phy
authorHeiner Kallweit <hkallweit1@gmail.com>
Sat, 25 Oct 2025 18:49:50 +0000 (20:49 +0200)
committerJakub Kicinski <kuba@kernel.org>
Thu, 30 Oct 2025 02:00:34 +0000 (19:00 -0700)
Add an iterator for all PHY's on a MII bus, and phy_find_next()
as a prerequisite.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Reviewed-by: Wei Fang <wei.fang@nxp.com>
Link: https://patch.msgid.link/cd112f15-401a-43d9-8525-9ff0965a68cd@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/phy/phy_device.c
include/linux/phy.h

index b7feaf0cb1df12630559e453ac6c70f79f55ec17..737747cf19069bb4b097d314956ac2860f7f4a2c 100644 (file)
@@ -1224,22 +1224,24 @@ int phy_get_c45_ids(struct phy_device *phydev)
 EXPORT_SYMBOL(phy_get_c45_ids);
 
 /**
- * phy_find_first - finds the first PHY device on the bus
+ * phy_find_next - finds the next PHY device on the bus
  * @bus: the target MII bus
+ * @pos: cursor
+ *
+ * Return: next phy_device on the bus, or NULL
  */
-struct phy_device *phy_find_first(struct mii_bus *bus)
+struct phy_device *phy_find_next(struct mii_bus *bus, struct phy_device *pos)
 {
-       struct phy_device *phydev;
-       int addr;
+       for (int addr = pos ? pos->mdio.addr + 1 : 0;
+            addr < PHY_MAX_ADDR; addr++) {
+               struct phy_device *phydev = mdiobus_get_phy(bus, addr);
 
-       for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
-               phydev = mdiobus_get_phy(bus, addr);
                if (phydev)
                        return phydev;
        }
        return NULL;
 }
-EXPORT_SYMBOL(phy_find_first);
+EXPORT_SYMBOL_GPL(phy_find_next);
 
 /**
  * phy_prepare_link - prepares the PHY layer to monitor link status
index 17a2cdc9f1a0f5698383e02799ae46f6213b0728..358dd6f0ff9651d707f9c8564b77975dff93b0bd 100644 (file)
@@ -1869,7 +1869,7 @@ int phy_sfp_probe(struct phy_device *phydev,
                  const struct sfp_upstream_ops *ops);
 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
                              phy_interface_t interface);
-struct phy_device *phy_find_first(struct mii_bus *bus);
+struct phy_device *phy_find_next(struct mii_bus *bus, struct phy_device *pos);
 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
                      u32 flags, phy_interface_t interface);
 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
@@ -1896,6 +1896,15 @@ bool phy_check_valid(int speed, int duplex, unsigned long *features);
 int phy_restart_aneg(struct phy_device *phydev);
 int phy_reset_after_clk_enable(struct phy_device *phydev);
 
+static inline struct phy_device *phy_find_first(struct mii_bus *bus)
+{
+       return phy_find_next(bus, NULL);
+}
+
+#define mdiobus_for_each_phy(_bus, _phydev)            \
+       for (_phydev = phy_find_first(_bus); _phydev;   \
+            _phydev = phy_find_next(_bus, _phydev))
+
 #if IS_ENABLED(CONFIG_PHYLIB)
 int phy_start_cable_test(struct phy_device *phydev,
                         struct netlink_ext_ack *extack);