Skip to contentSkip to conversation

net: phy: add iterator phy_for_each

PATCH Superseded Heiner Kallweit 5 participants 12 messages Oct 17, 2025 – Oct 22, 2025

Subsystems (6)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Heiner Kallweit

author maintainer
#0 lore raw

cover net: phy: add iterator phy_for_each

Add and use an iterator for all PHY's on a MII bus, and phy_find_next() as a prerequisite.

Heiner Kallweit (4):
net: phy: add iterator phy_for_each
net: fec: use new iterator phy_for_each
net: davinci_mdio: use new iterator phy_for_each
net: phy: use new iterator phy_for_each in mdiobus_prevent_c45_scan

 drivers/net/ethernet/freescale/fec_main.c |  8 ++------
 drivers/net/ethernet/ti/davinci_mdio.c    | 14 +++++---------
 drivers/net/phy/mdio_bus_provider.c       | 13 ++++---------
 drivers/net/phy/phy_device.c              | 14 +++++++-------
 include/linux/phy.h                       | 11 ++++++++++-
 5 files changed, 28 insertions(+), 32 deletions(-)
Signature · 1 line
2.51.1.dirty
author maintainer
#1 lore raw

PATCH 1/4 net: phy: add iterator phy_for_each

Add an iterator for all PHY's on a MII bus, and phy_find_next() as a prerequisite.

---
 drivers/net/phy/phy_device.c | 14 +++++++-------
 include/linux/phy.h          | 11 ++++++++++-
 2 files changed, 17 insertions(+), 8 deletions(-)
drivers/net/phy/phy_device.c +7 -7
index 7a67c900e..72d58b38d 100644
@@ -1214,22 +1214,22 @@ 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
  */
-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
include/linux/phy.h +10 -1
index 3c7634482..b0497f47b 100644
@@ -1848,7 +1848,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,
@@ -1875,6 +1875,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 phy_for_each(_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);
Signature · 1 line
2.51.1.dirty
author maintainer
#5 lore raw

PATCH 2/4 net: fec: use new iterator phy_for_each

In reply to Heiner Kallweit

Use new iterator phy_for_each() to simplify the code.

---
 drivers/net/ethernet/freescale/fec_main.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/net/ethernet/freescale/fec_main.c +2 -6
index 1edcfaee6..d7c39ef2d 100644
@@ -2552,7 +2552,6 @@ static int fec_enet_mii_init(struct platform_device *pdev)
 	int err = -ENXIO;
 	u32 mii_speed, holdtime;
 	u32 bus_freq;
-	int addr;
 
 	/*
 	 * The i.MX28 dual fec interfaces are not equal.
@@ -2667,11 +2666,8 @@ static int fec_enet_mii_init(struct platform_device *pdev)
 	of_node_put(node);
 
 	/* find all the PHY devices on the bus and set mac_managed_pm to true */
-	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
-		phydev = mdiobus_get_phy(fep->mii_bus, addr);
-		if (phydev)
-			phydev->mac_managed_pm = true;
-	}
+	phy_for_each(fep->mii_bus, phydev)
+		phydev->mac_managed_pm = true;
 
 	mii_cnt++;
 
Signature · 1 line
2.51.1.dirty
author maintainer
#7 lore raw

PATCH 3/4 net: davinci_mdio: use new iterator phy_for_each

In reply to Heiner Kallweit

Use new iterator phy_for_each() to simplify the code.

---
 drivers/net/ethernet/ti/davinci_mdio.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/net/ethernet/ti/davinci_mdio.c +5 -9
index 68507126b..5a7ed5ebf 100644
@@ -548,8 +548,8 @@ static int davinci_mdio_probe(struct platform_device *pdev)
 	struct davinci_mdio_data *data;
 	struct resource *res;
 	struct phy_device *phy;
-	int ret, addr;
 	int autosuspend_delay_ms = -1;
+	int ret;
 
 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
 	if (!data)
@@ -652,14 +652,10 @@ static int davinci_mdio_probe(struct platform_device *pdev)
 		goto bail_out;
 
 	/* scan and dump the bus */
-	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
-		phy = mdiobus_get_phy(data->bus, addr);
-		if (phy) {
-			dev_info(dev, "phy[%d]: device %s, driver %s\n",
-				 phy->mdio.addr, phydev_name(phy),
-				 phy->drv ? phy->drv->name : "unknown");
-		}
-	}
+	phy_for_each(data->bus, phy)
+		dev_info(dev, "phy[%d]: device %s, driver %s\n",
+			 phy->mdio.addr, phydev_name(phy),
+			 phy->drv ? phy->drv->name : "unknown");
 
 	return 0;
 
Signature · 1 line
2.51.1.dirty
author maintainer
#9 lore raw

PATCH 4/4 net: phy: use new iterator phy_for_each in mdiobus_prevent_c45_scan

In reply to Heiner Kallweit

Use new iterator phy_for_each() to simplify the code.

---
 drivers/net/phy/mdio_bus_provider.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/phy/mdio_bus_provider.c +4 -9
index a2391d4b7..20792480d 100644
@@ -249,20 +249,15 @@ static int mdiobus_scan_bus_c45(struct mii_bus *bus)
  */
 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
 {
-	int i;
+	struct phy_device *phydev;
 
-	for (i = 0; i < PHY_MAX_ADDR; i++) {
-		struct phy_device *phydev;
-		u32 oui;
-
-		phydev = mdiobus_get_phy(bus, i);
-		if (!phydev)
-			continue;
-		oui = phydev->phy_id >> 10;
+	phy_for_each(bus, phydev) {
+		u32 oui = phydev->phy_id >> 10;
 
 		if (oui == MICREL_OUI)
 			return true;
 	}
+
 	return false;
 }
 
Signature · 1 line
2.51.1.dirty
Reviewed-by on v1 1/4
#2 lore raw
In reply to Heiner Kallweit
Quoted 62 lines · Heiner Kallweit

Add an iterator for all PHY's on a MII bus, and phy_find_next() as a prerequisite.

---
  drivers/net/phy/phy_device.c | 14 +++++++-------
  include/linux/phy.h          | 11 ++++++++++-
  2 files changed, 17 insertions(+), 8 deletions(-)
drivers/net/phy/phy_device.c +7 -7
index 7a67c900e..72d58b38d 100644
@@ -1214,22 +1214,22 @@ 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
   */
-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
include/linux/phy.h +3 -1
index 3c7634482..b0497f47b 100644
@@ -1848,7 +1848,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,
@@ -1875,6 +1875,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)
+{

include/linux/phy.h bool phy_check_valid(int speed, int duplex, unsigned long *features); · line 1889

Quoted 10 lines
include/linux/phy.h +7 -0
+	return phy_find_next(bus, NULL);
+}
+
+#define phy_for_each(_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);
Reviewed-by on v1 2/4
#6 lore raw

Re: [PATCH net-next 2/4] net: fec: use new iterator phy_for_each

In reply to Heiner Kallweit
Quoted 23 lines · Heiner Kallweit

Use new iterator phy_for_each() to simplify the code.

---
  drivers/net/ethernet/freescale/fec_main.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
drivers/net/ethernet/freescale/fec_main.c +0 -1
index 1edcfaee6..d7c39ef2d 100644
@@ -2552,7 +2552,6 @@ static int fec_enet_mii_init(struct platform_device *pdev)
  	int err = -ENXIO;
  	u32 mii_speed, holdtime;
  	u32 bus_freq;
-	int addr;
 
  	/*
  	 * The i.MX28 dual fec interfaces are not equal.
@@ -2667,11 +2666,8 @@ static int fec_enet_mii_init(struct platform_device *pdev)
  	of_node_put(node);
 
  	/* find all the PHY devices on the bus and set mac_managed_pm to true */

drivers/net/ethernet/freescale/fec_main.c static int fec_enet_mii_init(struct platform_device *pdev) · line 2673

Quoted 10 lines
drivers/net/ethernet/freescale/fec_main.c +2 -5
-	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
-		phydev = mdiobus_get_phy(fep->mii_bus, addr);
-		if (phydev)
-			phydev->mac_managed_pm = true;
-	}
+	phy_for_each(fep->mii_bus, phydev)
+		phydev->mac_managed_pm = true;
 
  	mii_cnt++;
 
Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH net-next 3/4] net: davinci_mdio: use new iterator phy_for_each

In reply to Heiner Kallweit
Quoted 30 lines · Heiner Kallweit

Use new iterator phy_for_each() to simplify the code.

---
  drivers/net/ethernet/ti/davinci_mdio.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
drivers/net/ethernet/ti/davinci_mdio.c +1 -6
index 68507126b..5a7ed5ebf 100644
@@ -548,8 +548,8 @@ static int davinci_mdio_probe(struct platform_device *pdev)
  	struct davinci_mdio_data *data;
  	struct resource *res;
  	struct phy_device *phy;
-	int ret, addr;
  	int autosuspend_delay_ms = -1;
+	int ret;
 
  	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  	if (!data)
@@ -652,14 +652,10 @@ static int davinci_mdio_probe(struct platform_device *pdev)
  		goto bail_out;
 
  	/* scan and dump the bus */
-	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
-		phy = mdiobus_get_phy(data->bus, addr);
-		if (phy) {
-			dev_info(dev, "phy[%d]: device %s, driver %s\n",
-				 phy->mdio.addr, phydev_name(phy),

drivers/net/ethernet/ti/davinci_mdio.c static int davinci_mdio_probe(struct platform_device *pdev) · line 661

Quoted 10 lines
drivers/net/ethernet/ti/davinci_mdio.c +4 -3
-				 phy->drv ? phy->drv->name : "unknown");
-		}
-	}
+	phy_for_each(data->bus, phy)
+		dev_info(dev, "phy[%d]: device %s, driver %s\n",
+			 phy->mdio.addr, phydev_name(phy),
+			 phy->drv ? phy->drv->name : "unknown");
 
  	return 0;
 
Reviewed-by on v1 4/4
#10 lore raw

Re: [PATCH net-next 4/4] net: phy: use new iterator phy_for_each in mdiobus_prevent_c45_scan

In reply to Heiner Kallweit
Quoted 26 lines · Heiner Kallweit

Use new iterator phy_for_each() to simplify the code.

---
  drivers/net/phy/mdio_bus_provider.c | 13 ++++---------
  1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/phy/mdio_bus_provider.c +1 -9
index a2391d4b7..20792480d 100644
@@ -249,20 +249,15 @@ static int mdiobus_scan_bus_c45(struct mii_bus *bus)
   */
  static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
  {
-	int i;
+	struct phy_device *phydev;
 
-	for (i = 0; i < PHY_MAX_ADDR; i++) {
-		struct phy_device *phydev;
-		u32 oui;
-
-		phydev = mdiobus_get_phy(bus, i);
-		if (!phydev)
-			continue;
-		oui = phydev->phy_id >> 10;

drivers/net/phy/mdio_bus_provider.c static int mdiobus_scan_bus_c45(struct mii_bus *bus) · line 263

Quoted 10 lines
drivers/net/phy/mdio_bus_provider.c +3 -0
+	phy_for_each(bus, phydev) {
+		u32 oui = phydev->phy_id >> 10;
 
  		if (oui == MICREL_OUI)
  			return true;
  	}
+
  	return false;
  }
 
maintainer Reviewed-by on the v1 series
#11 lore raw
In reply to Heiner Kallweit
Quoted 16 lines

Add and use an iterator for all PHY's on a MII bus, and phy_find_next() as a prerequisite.

Heiner Kallweit (4):
net: phy: add iterator phy_for_each
net: fec: use new iterator phy_for_each
net: davinci_mdio: use new iterator phy_for_each
net: phy: use new iterator phy_for_each in mdiobus_prevent_c45_scan

 drivers/net/ethernet/freescale/fec_main.c |  8 ++------
 drivers/net/ethernet/ti/davinci_mdio.c    | 14 +++++---------
 drivers/net/phy/mdio_bus_provider.c       | 13 ++++---------
 drivers/net/phy/phy_device.c              | 14 +++++++-------
 include/linux/phy.h                       | 11 ++++++++++-
 5 files changed, 28 insertions(+), 32 deletions(-)
Quoted 2 lines

--
2.51.1.dirty

For this patch set.

maintainer
#3 lore raw
In reply to Heiner Kallweit

include/linux/phy.h bool phy_check_valid(int speed, int duplex, unsigned long *features); · line 1885

Quoted 3 lines · On Fri, 17 Oct 2025 22:41:12 +0200 Heiner Kallweit wrote:
include/linux/phy.h +3 -0
+#define phy_for_each(_bus, _phydev)			\
+	for (_phydev = phy_find_first(_bus); _phydev;	\
+	     _phydev = phy_find_next(_bus, _phydev))

Wouldn't this better be called mii_for_each_phy() or mii_bus_for_each_phy() ?

To an outsider and with PHY typologies it may not be sufficiently obvious that this helper is used to iterate a all addresses on a MII bus.

maintainer
#4 lore raw
Quoted 11 lines · Jakub Kicinski

include/linux/phy.h bool phy_check_valid(int speed, int duplex, unsigned long *features); · line 1885

Quoted 3 lines · On Fri, 17 Oct 2025 22:41:12 +0200 Heiner Kallweit wrote:
include/linux/phy.h +3 -0
+#define phy_for_each(_bus, _phydev)			\
+	for (_phydev = phy_find_first(_bus); _phydev;	\
+	     _phydev = phy_find_next(_bus, _phydev))

Wouldn't this better be called mii_for_each_phy() or mii_bus_for_each_phy() ?

To an outsider and with PHY typologies it may not be sufficiently obvious that this helper is used to iterate a all addresses on a MII bus.

The naming scheme suggest it either needs to start with phy_ or mdiobus_.

mdiobus_for_each_phy() might be the best name, if you want to indicate it iterates an mdio bus. It should also be noted that it will ignore devices on the bus which are not PHYs. So we need phy in the name somewhere.

Andrew