[PATCH net-next v4 1/4] net: phy: Map C45 probe read errors to -ENODEV during bus scan

From: Florian Fainelli

Date: Wed Nov 18 2026 - 20:00:16 EST


get_phy_c22_id() maps a bus read error of -EIO or -ENODEV to -ENODEV, so
that mdiobus_scan_bus_c22() carries on with the next address when nothing
answers. get_phy_c45_ids() only does so for phy_c45_probe_present(); a
failed read of the devices in package registers is turned into -EIO, which
mdiobus_scan_bus_c45() treats as fatal and which makes the whole
__mdiobus_register() fail.

MDIO controllers that report a read from an empty address as a bus error
(e.g. MDIO_READ_FAIL on the UniMAC MDIO block) and that provide read_c45()
with unmasked addresses therefore cannot be registered: the C45 scan fails
on the first empty address.

Let get_phy_c45_devs_in_pkg() return the bus error and apply the same
mapping as get_phy_c22_id() to the two places that look for a device at an
address: the loop over the MMDs and the MMD 0 fallback. In the loop, an MMD
whose read fails that way is handled like one that reads as all Fs, except
that the remaining MMDs are not probed: the scan goes straight to MMD 0, and
-ENODEV is only returned if that does not answer either, so PHYs that only
respond on MMD 0 are still found while an empty address costs a bounded
number of bus reads.
Errors from the reads of the per-MMD identifiers, which only happen once a
device has answered, are still reported as -EIO.

Fixes: 17b447539408 ("net: phy: c45 scanning: Don't consider -ENODEV fatal")
Reviewed-by: Nicolai Buchwitz <nb@xxxxxxxxxxx>
Signed-off-by: Florian Fainelli <florian.fainelli@xxxxxxxxxxxx>
---
drivers/net/phy/phy_device.c | 48 ++++++++++++++++++++++++++++--------
1 file changed, 38 insertions(+), 10 deletions(-)

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index b39e945cebd7..79f3fa1e9a03 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -780,7 +780,7 @@ static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
* Description: reads devices in package registers of a MMD at @dev_addr
* from PHY at @addr on @bus.
*
- * Returns: 0 on success, -EIO on failure.
+ * Returns: 0 on success, or the negative error code from the bus read.
*/
static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
u32 *devices_in_package)
@@ -789,17 +789,30 @@ static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,

phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
if (phy_reg < 0)
- return -EIO;
+ return phy_reg;
*devices_in_package = phy_reg << 16;

phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
if (phy_reg < 0)
- return -EIO;
+ return phy_reg;
*devices_in_package |= phy_reg;

return 0;
}

+/* get_phy_c45_probe_err - map a bus error seen while looking for a device.
+ * @err: negative error code returned by a bus read
+ *
+ * Like get_phy_c22_id(), report -ENODEV when the bus read failed with -EIO or
+ * -ENODEV, which is how a controller signals that nothing answered at this
+ * address, so that bus scanning carries on with the next address. Anything
+ * else is reported as -EIO.
+ */
+static int get_phy_c45_probe_err(int err)
+{
+ return (err == -EIO || err == -ENODEV) ? -ENODEV : -EIO;
+}
+
/**
* get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
* @bus: the target MII bus
@@ -810,8 +823,9 @@ static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
* the PHY identifiers for each device. Return the "devices in package"
* and identifiers in @c45_ids.
*
- * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
- * the "devices in package" is invalid or no device responds.
+ * Returns zero on success, %-ENODEV if the "devices in package" is invalid or
+ * if no MMD responds (including when the bus reports -EIO or -ENODEV for the
+ * reads that look for a device), or %-EIO on any other bus access error.
*/
static int get_phy_c45_ids(struct mii_bus *bus, int addr,
struct phy_c45_device_ids *c45_ids)
@@ -844,8 +858,20 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
continue;
}
phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
- if (phy_reg < 0)
- return -EIO;
+ if (phy_reg < 0) {
+ ret = get_phy_c45_probe_err(phy_reg);
+ if (ret != -ENODEV)
+ return ret;
+
+ /* The MMD did not answer: treat it like a read of all
+ * Fs, which is what controllers that do not check the
+ * turnaround return, and go straight to the MMD 0
+ * probe below. Do not probe the remaining MMDs, each
+ * one costs two bus reads on a slow bus.
+ */
+ devs_in_pkg = 0xffffffff;
+ break;
+ }
}

if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
@@ -855,7 +881,8 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
*/
phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
if (phy_reg < 0)
- return -EIO;
+ /* returning -ENODEV doesn't stop bus scanning */
+ return get_phy_c45_probe_err(phy_reg);

/* no device there, let's get out of here */
if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
@@ -1077,8 +1104,9 @@ EXPORT_SYMBOL(phy_device_remove);
* phy_get_c45_ids - Read 802.3-c45 IDs for phy device.
* @phydev: phy_device structure to read 802.3-c45 IDs
*
- * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
- * the "devices in package" is invalid.
+ * Returns zero on success, %-ENODEV if the "devices in package" is invalid or
+ * no MMD responds (this includes a bus read failing with -EIO or -ENODEV while
+ * looking for a device), or %-EIO on any other bus access error.
*/
int phy_get_c45_ids(struct phy_device *phydev)
{
--
2.34.1