Skip to contentSkip to conversation

net: phy: allow scanning busses with missing phys

PATCH Applied Alexandre Belloni 4 participants 7 messages Apr 24, 2018 – Apr 25, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: phy: allow scanning busses with missing phys

Some MDIO busses will error out when trying to read a phy address with no phy present at that address. In that case, probing the bus will fail because __mdiobus_register() is scanning the bus for all possible phys addresses.

In case MII_PHYSID1 returns -EIO or -ENODEV, consider there is no phy at this address and set the phy ID to 0xffffffff which is then properly handled in get_phy_device().

---
 drivers/net/phy/phy_device.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/net/phy/phy_device.c +10 -1
index ac23322a32e1..9e4ba8e80a18 100644
@@ -535,8 +535,17 @@ static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 
 	/* Grab the bits from PHYIR1, and put them in the upper half */
 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
-	if (phy_reg < 0)
+	if (phy_reg < 0) {
+		/* if there is no device, return without an error so scanning
+		 * the bus works properly
+		 */
+		if (phy_reg == -EIO || phy_reg == -ENODEV) {
+			*phy_id = 0xffffffff;
+			return 0;
+		}
+
 		return -EIO;
+	}
 
 	*phy_id = (phy_reg & 0xffff) << 16;
 
Signature · 1 line
2.17.0
#1 lore raw
Quoted 8 lines · Alexandre Belloni

Some MDIO busses will error out when trying to read a phy address with no phy present at that address. In that case, probing the bus will fail because __mdiobus_register() is scanning the bus for all possible phys addresses.

In case MII_PHYSID1 returns -EIO or -ENODEV, consider there is no phy at this address and set the phy ID to 0xffffffff which is then properly handled in get_phy_device().

Humm, why not have your MDIO bus implementation do the scanning itself in a reset() callback, which happens before probing the bus, and based on the results, set phy_mask accordingly such that only PHYs present are populated?

My only concern with your change is that we are having a special treatment for EIO and ENODEV, so we must make sure MDIO bus drivers are all conforming to that.

Quoted 31 lines
---
 drivers/net/phy/phy_device.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/net/phy/phy_device.c +10 -1
index ac23322a32e1..9e4ba8e80a18 100644
@@ -535,8 +535,17 @@ static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 
 	/* Grab the bits from PHYIR1, and put them in the upper half */
 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
-	if (phy_reg < 0)
+	if (phy_reg < 0) {
+		/* if there is no device, return without an error so scanning
+		 * the bus works properly
+		 */
+		if (phy_reg == -EIO || phy_reg == -ENODEV) {
+			*phy_id = 0xffffffff;
+			return 0;
+		}
+
 		return -EIO;
+	}
 
 	*phy_id = (phy_reg & 0xffff) << 16;
 
Signature · 1 line
Florian
maintainer
#2 lore raw
Quoted 8 lines · Florian Fainelli
Quoted 5 lines · Alexandre Belloni

Some MDIO busses will error out when trying to read a phy address with no phy present at that address. In that case, probing the bus will fail because __mdiobus_register() is scanning the bus for all possible phys addresses.

Quoted 8 lines
Quoted 3 lines

In case MII_PHYSID1 returns -EIO or -ENODEV, consider there is no phy at this address and set the phy ID to 0xffffffff which is then properly handled in get_phy_device().

Humm, why not have your MDIO bus implementation do the scanning itself in a reset() callback, which happens before probing the bus, and based on the results, set phy_mask accordingly such that only PHYs present are populated?

Hi Florian

Seems a bit odd have the driver do this, when the core could.

Quoted 3 lines

My only concern with your change is that we are having a special treatment for EIO and ENODEV, so we must make sure MDIO bus drivers are all conforming to that.

I don't see how it could be a problem. It used to be any error was a real error, and would stop the bus from loading. It now means there is nothing there. The only possible side effect is an mdio bus driver might remain loaded without any devices if all reads return ENODEV/EIO, were as before it would probably never load.

Andrew

#3 lore raw
Quoted 13 lines · Andrew Lunn
Quoted 12 lines · Florian Fainelli
Quoted 8 lines · Alexandre Belloni

Some MDIO busses will error out when trying to read a phy address with no phy present at that address. In that case, probing the bus will fail because __mdiobus_register() is scanning the bus for all possible phys addresses.

In case MII_PHYSID1 returns -EIO or -ENODEV, consider there is no phy at this address and set the phy ID to 0xffffffff which is then properly handled in get_phy_device().

Quoted 8 lines
Quoted 4 lines

Humm, why not have your MDIO bus implementation do the scanning itself in a reset() callback, which happens before probing the bus, and based on the results, set phy_mask accordingly such that only PHYs present are populated?

Hi Florian

Seems a bit odd have the driver do this, when the core could.

I could see arguments for doing it in either location, if this is done in the driver, the driver is responsible for knowing what addresses will respond, which does not seem to big of a stretch. If done by core, we lift the driver from taking care of that. Either way is fine, really.

Quoted 10 lines
Quoted 4 lines

My only concern with your change is that we are having a special treatment for EIO and ENODEV, so we must make sure MDIO bus drivers are all conforming to that.

I don't see how it could be a problem. It used to be any error was a real error, and would stop the bus from loading. It now means there is nothing there. The only possible side effect is an mdio bus driver might remain loaded without any devices if all reads return ENODEV/EIO, were as before it would probably never load.

Right, though that does not seem to be a big problem.

Quoted 3 lines

Andrew

Signature · 1 line
Florian
#4 lore raw
In reply to Florian Fainelli
Quoted 12 lines · Florian Fainelli
Quoted 8 lines · Alexandre Belloni

Some MDIO busses will error out when trying to read a phy address with no phy present at that address. In that case, probing the bus will fail because __mdiobus_register() is scanning the bus for all possible phys addresses.

In case MII_PHYSID1 returns -EIO or -ENODEV, consider there is no phy at this address and set the phy ID to 0xffffffff which is then properly handled in get_phy_device().

Quoted 9 lines

Humm, why not have your MDIO bus implementation do the scanning itself in a reset() callback, which happens before probing the bus, and based on the results, set phy_mask accordingly such that only PHYs present are populated?

My only concern with your change is that we are having a special treatment for EIO and ENODEV, so we must make sure MDIO bus drivers are all conforming to that.

That was what I was doing in [1] but it seems that Andrew preferred this way.

The third solution I was seeing was to return phy_reg instead of -EIO so the MDIO driver can return -ENODEV and that would be passed to get_phy_device(). __mdiobus_register() seems to handle -ENODEV properly.

My coccinelle-fu is not great but the following drivers can return -ENODEV from their read callback:

drivers/net/ethernet/marvell/mvmdio.c drivers/net/ethernet/hisilicon/hix5hd2_gmac.c (seeing the error message, this has probably been copy pasted)

[1] https://marc.info/?l=linux-netdev&m=152183609927933&w=2

Quoted 34 lines
Quoted 31 lines
---
 drivers/net/phy/phy_device.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/net/phy/phy_device.c +10 -1
index ac23322a32e1..9e4ba8e80a18 100644
@@ -535,8 +535,17 @@ static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 
 	/* Grab the bits from PHYIR1, and put them in the upper half */
 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
-	if (phy_reg < 0)
+	if (phy_reg < 0) {
+		/* if there is no device, return without an error so scanning
+		 * the bus works properly
+		 */
+		if (phy_reg == -EIO || phy_reg == -ENODEV) {
+			*phy_id = 0xffffffff;
+			return 0;
+		}
+
 		return -EIO;
+	}
 
 	*phy_id = (phy_reg & 0xffff) << 16;
 
Signature · 1 line
Florian
Signature · 3 lines
Alexandre Belloni, Bootlin (formerly Free Electrons)
Embedded Linux and Kernel engineering
https://bootlin.com
Reviewed-by on v1 1/1
#5 lore raw
In reply to Alexandre Belloni
Quoted 11 lines · Alexandre Belloni

Some MDIO busses will error out when trying to read a phy address with no phy present at that address. In that case, probing the bus will fail because __mdiobus_register() is scanning the bus for all possible phys addresses.

In case MII_PHYSID1 returns -EIO or -ENODEV, consider there is no phy at this address and set the phy ID to 0xffffffff which is then properly handled in get_phy_device().

Signature · 1 line
Florian
maintainer Applied
#6 lore raw
In reply to Alexandre Belloni

From: Alexandre Belloni <[email protected]>
Date: Tue, 24 Apr 2018 18:09:04 +0200

Quoted 11 lines

Some MDIO busses will error out when trying to read a phy address with no phy present at that address. In that case, probing the bus will fail because __mdiobus_register() is scanning the bus for all possible phys addresses.

In case MII_PHYSID1 returns -EIO or -ENODEV, consider there is no phy at this address and set the phy ID to 0xffffffff which is then properly handled in get_phy_device().

Applied, thank you.