Skip to contentSkip to conversation

Don't return NULL from get_phy_device() anymore

PATCH In review Sergei Shtylyov 5 participants 21 messages Apr 24, 2016 – Apr 27, 2016 1 message not in the archive

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

cover Don't return NULL from get_phy_device() anymore

Hello.

Here's the set of 5 patches against DaveM's 'net-next.git' repo. The first
patch makes get_phy_device() return only error values on error, the rest of the patches clean up the callers of that function...

[1/5] phylib-don-t-return-NULL-from-get_phy_device.patch [2/5] xgene-get_phy_device-doesn-t-return-NULL-anymore.patch [3/5] fixed_phy-get_phy_device-doesn-t-return-NULL-anymore.patch [4/5] mdio_bus-get_phy_device-doesn-t-return-NULL-anymore.patch [5/5] of_mdio-get_phy_device-doesn-t-return-NULL-anymore.patch

MBR, Sergei

#1 lore raw

PATCH 1/5 phylib: don't return NULL from get_phy_device()

Arnd Bergmann asked that get_phy_device() returns either NULL or the error value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead of NULL when the PHY ID registers read as all ones.

---
 drivers/net/phy/phy_device.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/phy_device.c +1 -1
Index: net-next/drivers/net/phy/phy_device.c
===================================================================
@@ -529,7 +529,7 @@ struct phy_device *get_phy_device(struct
 
 	/* If the phy_id is mostly Fs, there is no device there */
 	if ((phy_id & 0x1fffffff) == 0x1fffffff)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
 	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
 }
#12 lore raw

PATCH 2/5 xgene: get_phy_device() doesn't return NULL anymore

In reply to Sergei Shtylyov

Now that get_phy_device() no longer returns NULL on error, we don't need to check for it...

---
 drivers/net/ethernet/apm/xgene/xgene_enet_hw.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/apm/xgene/xgene_enet_hw.c +1 -1
Index: net-next/drivers/net/ethernet/apm/xgene/xgene_enet_hw.c
===================================================================
@@ -824,7 +824,7 @@ static int xgene_mdiobus_register(struct
 		return -EINVAL;
 
 	phy = get_phy_device(mdio, phy_id, false);
-	if (!phy || IS_ERR(phy))
+	if (IS_ERR(phy))
 		return -EIO;
 
 	ret = phy_device_register(phy);
#14 lore raw

PATCH 3/5 fixed_phy: get_phy_device() doesn't return NULL anymore

In reply to Sergei Shtylyov

Now that get_phy_device() no longer returns NULL on error, we don't need to check for it...

---
 drivers/net/phy/fixed_phy.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/fixed_phy.c +1 -1
Index: net-next/drivers/net/phy/fixed_phy.c
===================================================================
@@ -328,7 +328,7 @@ struct phy_device *fixed_phy_register(un
 		return ERR_PTR(ret);
 
 	phy = get_phy_device(fmb->mii_bus, phy_addr, false);
-	if (!phy || IS_ERR(phy)) {
+	if (IS_ERR(phy)) {
 		fixed_phy_del(phy_addr);
 		return ERR_PTR(-EINVAL);
 	}
#16 lore raw

PATCH 4/5 mdio_bus: get_phy_device() doesn't return NULL anymore

In reply to Sergei Shtylyov

Now that get_phy_device() no longer returns NULL on error, we don't need to check for it...

---
 drivers/net/phy/mdio_bus.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/mdio_bus.c +1 -1
Index: net-next/drivers/net/phy/mdio_bus.c
===================================================================
@@ -419,7 +419,7 @@ struct phy_device *mdiobus_scan(struct m
 	int err;
 
 	phydev = get_phy_device(bus, addr, false);
-	if (IS_ERR(phydev) || phydev == NULL)
+	if (IS_ERR(phydev))
 		return phydev;
 
 	/*
#18 lore raw

PATCH 5/5 of_mdio: get_phy_device() doesn't return NULL anymore

In reply to Sergei Shtylyov

Now that get_phy_device() no longer returns NULL on error, we don't need to check for it...

---
 drivers/of/of_mdio.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/of/of_mdio.c +1 -1
Index: net-next/drivers/of/of_mdio.c
===================================================================
@@ -56,7 +56,7 @@ static void of_mdiobus_register_phy(stru
 		phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
 	else
 		phy = get_phy_device(mdio, addr, is_c45);
-	if (IS_ERR_OR_NULL(phy))
+	if (IS_ERR(phy))
 		return;
 
 	rc = irq_of_parse_and_map(child, 0);
#19 lore raw

Re: [PATCH 5/5] of_mdio: get_phy_device() doesn't return NULL anymore

Quoted 5 lines · Sergei Shtylyov

Now that get_phy_device() no longer returns NULL on error, we don't need to check for it...

Oops, forgot to CC: the DT people, doing that now...

Quoted 17 lines
---
  drivers/of/of_mdio.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Index: net-next/drivers/of/of_mdio.c
===================================================================

drivers/of/of_mdio.c +1 -1
@@ -56,7 +56,7 @@ static void of_mdiobus_register_phy(stru
  		phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
  	else
  		phy = get_phy_device(mdio, addr, is_c45);
-	if (IS_ERR_OR_NULL(phy))
+	if (IS_ERR(phy))
  		return;
 
  	rc = irq_of_parse_and_map(child, 0);
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

In reply to Sergei Shtylyov
Quoted 6 lines · Sergei Shtylyov

Arnd Bergmann asked that get_phy_device() returns either NULL or the error value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead of NULL when the PHY ID registers read as all ones.

Signature · 1 line
Florian
maintainer
#21 lore raw
In reply to Sergei Shtylyov

From: Sergei Shtylyov <[email protected]>
Date: Sun, 24 Apr 2016 20:23:03 +0300

Quoted 3 lines

Here's the set of 5 patches against DaveM's 'net-next.git' repo. The first
patch makes get_phy_device() return only error values on error, the rest of the patches clean up the callers of that function...

Series applied, thanks.

maintainer
#4 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

In reply to a message not in the archive
Quoted 21 lines · Vivien Didelot

Hi David, All,

Quoted 17 lines · Sergei Shtylyov

Arnd Bergmann asked that get_phy_device() returns either NULL or the error value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead of NULL when the PHY ID registers read as all ones.

---
 drivers/net/phy/phy_device.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: net-next/drivers/net/phy/phy_device.c
===================================================================

drivers/net/phy/phy_device.c +0 -0
@@ -529,7 +529,7 @@ struct phy_device *get_phy_device(struct
 
Quoted 7 lines

drivers/net/phy/phy_device.c struct phy_device *get_phy_device(struct · line 535

Quoted 7 lines
drivers/net/phy/phy_device.c +1 -1
 	/* If the phy_id is mostly Fs, there is no device there */
 	if ((phy_id & 0x1fffffff) == 0x1fffffff)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
 	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
 }

This change is wrong, it needs reverting, or the call sights need fixing to expect ENODEV.

The point is, the device not being there is not an error, with respect to the code calling this function.

It gets called by mdiobus_scan()

struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)

{
        struct phy_device *phydev;
        int err;

        phydev = get_phy_device(bus, addr, false);
        if (IS_ERR(phydev) || phydev == NULL)
                return phydev;

So before, we return NULL, if the device was not there. Now we return ERR_PTR(-ENODEV).

This is being called by:

int __mdiobus_register(struct mii_bus *bus, struct module *owner)

{
        struct mdio_device *mdiodev;
...
        for (i = 0; i < PHY_MAX_ADDR; i++) {
                if ((bus->phy_mask & (1 << i)) == 0) {
                        struct phy_device *phydev;

                        phydev = mdiobus_scan(bus, i);
                        if (IS_ERR(phydev)) {
                                err = PTR_ERR(phydev);
                                goto error;
                        }
                }
        }

This is treating ERR_PTR(-ENODEV) as a fatal error, where as before IS_ERR(NULL) would be false and it would continue scanning other addresses on the bus.

Please revert this, or fix all the callsites such that ENODEV is not a fatal error.

Andrew

#5 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

Hello.

Quoted 24 lines · Andrew Lunn
Quoted 24 lines
Quoted 22 lines · Sergei Shtylyov

Arnd Bergmann asked that get_phy_device() returns either NULL or the error value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead of NULL when the PHY ID registers read as all ones.

---
  drivers/net/phy/phy_device.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Index: net-next/drivers/net/phy/phy_device.c
===================================================================

drivers/net/phy/phy_device.c +1 -1
@@ -529,7 +529,7 @@ struct phy_device *get_phy_device(struct
 
  	/* If the phy_id is mostly Fs, there is no device there */
  	if ((phy_id & 0x1fffffff) == 0x1fffffff)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
Quoted 5 lines
Quoted 2 lines

drivers/net/phy/phy_device.c struct phy_device *get_phy_device(struct · line 535

Quoted 2 lines
drivers/net/phy/phy_device.c +0 -0
  	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  }

This change is wrong, it needs reverting, or the call sights need fixing to expect ENODEV.

So this function had a good reason to return NULL, as it turned out... :-(

Quoted 35 lines

The point is, the device not being there is not an error, with respect to the code calling this function.

It gets called by mdiobus_scan()

struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)

{
         struct phy_device *phydev;
         int err;

         phydev = get_phy_device(bus, addr, false);
         if (IS_ERR(phydev) || phydev == NULL)
                 return phydev;

So before, we return NULL, if the device was not there. Now we return ERR_PTR(-ENODEV).

This is being called by:

int __mdiobus_register(struct mii_bus *bus, struct module *owner)

{
         struct mdio_device *mdiodev;
...
         for (i = 0; i < PHY_MAX_ADDR; i++) {
                 if ((bus->phy_mask & (1 << i)) == 0) {
                         struct phy_device *phydev;

                         phydev = mdiobus_scan(bus, i);
                         if (IS_ERR(phydev)) {
                                 err = PTR_ERR(phydev);
                                 goto error;
                         }
                 }
         }
Quoted 3 lines

This is treating ERR_PTR(-ENODEV) as a fatal error, where as before IS_ERR(NULL) would be false and it would continue scanning other addresses on the bus.

Thank you for the detailed analysis! (And shame on me for the lack of it.)

Quoted 2 lines

Please revert this, or fix all the callsites such that ENODEV is not a fatal error.

OK, I'll do what DaveM decides.

Quoted 1 line

Andrew

MBR, Sergei

#10 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

In reply to a message not in the archive

Hello.

Quoted 27 lines · Vivien Didelot
Quoted 16 lines

Arnd Bergmann asked that get_phy_device() returns either NULL or the error value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead of NULL when the PHY ID registers read as all ones.

---
  drivers/net/phy/phy_device.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Index: net-next/drivers/net/phy/phy_device.c
===================================================================

drivers/net/phy/phy_device.c struct phy_device *get_phy_device(struct · line 535

Quoted 8 lines
drivers/net/phy/phy_device.c +1 -1
@@ -529,7 +529,7 @@ struct phy_device *get_phy_device(struct
 
  	/* If the phy_id is mostly Fs, there is no device there */
  	if ((phy_id & 0x1fffffff) == 0x1fffffff)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
  	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  }

This particular commit, merged as:

Quoted 8 lines
     b74766a0a0fe ("phylib: don't return NULL from get_phy_device()")

breaks my 3-switch DSA setup with the following error:

fec: probe of 400d1000.ethernet failed with error -22

Reverting c971c0e580a6 ("Merge branch 'get_phy_device-retval'") restores a working setup.

I think I was able to follow this to the get_phy_device() call in
fixed_phy_register() but I'm unable to see why it fails now and didn't before. Are you using fixed_phy.c at all?

Quoted 3 lines

Thanks,

Vivien

MBR, Sergei

#11 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

Quoted 37 lines · Sergei Shtylyov

Hello.

Quoted 33 lines · Vivien Didelot
Quoted 18 lines

Arnd Bergmann asked that get_phy_device() returns either NULL or the error
value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead
of NULL when the PHY ID registers read as all ones.

---
  drivers/net/phy/phy_device.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Index: net-next/drivers/net/phy/phy_device.c
===================================================================

drivers/net/phy/phy_device.c struct phy_device *get_phy_device(struct · line 535

Quoted 8 lines
drivers/net/phy/phy_device.c +1 -1
@@ -529,7 +529,7 @@ struct phy_device *get_phy_device(struct
 
      /* If the phy_id is mostly Fs, there is no device there */
      if ((phy_id & 0x1fffffff) == 0x1fffffff)
-        return NULL;
+        return ERR_PTR(-ENODEV);
 
      return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  }

This particular commit, merged as:

     b74766a0a0fe ("phylib: don't return NULL from get_phy_device()")

breaks my 3-switch DSA setup with the following error:

Quoted 8 lines
Quoted 4 lines

fec: probe of 400d1000.ethernet failed with error -22

Reverting c971c0e580a6 ("Merge branch 'get_phy_device-retval'") restores a working setup.

I think I was able to follow this to the get_phy_device() call in
fixed_phy_register() but I'm unable to see why it fails now and didn't before. Are you using fixed_phy.c at all?

I am using fixed_phy.c and yes, this fails for me too, sorry for not catching that when you posted the patches :/

Signature · 1 line
Florian
maintainer
#6 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

In reply to Sergei Shtylyov

From: Sergei Shtylyov <[email protected]>
Date: Wed, 27 Apr 2016 23:09:37 +0300

Quoted 6 lines · Andrew Lunn
Quoted 3 lines

Please revert this, or fix all the callsites such that ENODEV is not a fatal error.

OK, I'll do what DaveM decides.

If you feel confident getting all the ENODEV checks right, please just do that.

Thanks.

#8 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

In reply to Andrew Lunn
Quoted 68 lines · Andrew Lunn
Quoted 21 lines · Vivien Didelot

Hi David, All,

Quoted 17 lines · Sergei Shtylyov

Arnd Bergmann asked that get_phy_device() returns either NULL or the error value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead of NULL when the PHY ID registers read as all ones.

---
 drivers/net/phy/phy_device.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: net-next/drivers/net/phy/phy_device.c
===================================================================

drivers/net/phy/phy_device.c +0 -0
@@ -529,7 +529,7 @@ struct phy_device *get_phy_device(struct
 
Quoted 7 lines

drivers/net/phy/phy_device.c struct phy_device *get_phy_device(struct · line 535

Quoted 7 lines
drivers/net/phy/phy_device.c +1 -1
 	/* If the phy_id is mostly Fs, there is no device there */
 	if ((phy_id & 0x1fffffff) == 0x1fffffff)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
 	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
 }

This change is wrong, it needs reverting, or the call sights need fixing to expect ENODEV.

The point is, the device not being there is not an error, with respect to the code calling this function.

It gets called by mdiobus_scan()

struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)

{
        struct phy_device *phydev;
        int err;

        phydev = get_phy_device(bus, addr, false);
        if (IS_ERR(phydev) || phydev == NULL)
                return phydev;

So before, we return NULL, if the device was not there. Now we return ERR_PTR(-ENODEV).

This is being called by:

int __mdiobus_register(struct mii_bus *bus, struct module *owner)

{
        struct mdio_device *mdiodev;
...
        for (i = 0; i < PHY_MAX_ADDR; i++) {
                if ((bus->phy_mask & (1 << i)) == 0) {
                        struct phy_device *phydev;

                        phydev = mdiobus_scan(bus, i);
                        if (IS_ERR(phydev)) {
                                err = PTR_ERR(phydev);
                                goto error;
                        }
                }
        }
Quoted 6 lines

This is treating ERR_PTR(-ENODEV) as a fatal error, where as before IS_ERR(NULL) would be false and it would continue scanning other addresses on the bus.

Please revert this, or fix all the callsites such that ENODEV is not a fatal error.

So the one you pointed out in __mdiobus_register() is definitively needed, though I did get a different issue than Vivien's (-EBUSY vs. -EINVAL). The get_phy_device() in drivers/of/of_mdio.c probably needs something similar too, here is what I locally have for the moment:

drivers/net/phy/mdio_bus.c +0 -0
index 499003ee8055..94a27b028dd8 100644
@@ -333,7 +333,7 @@ int __mdiobus_register(struct mii_bus *bus, struct

module *owner)

                        struct phy_device *phydev;

                        phydev = mdiobus_scan(bus, i);
-                       if (IS_ERR(phydev)) {
+                       if (IS_ERR(phydev) && PTR_ERR(phydev) != -ENODEV) {
                                err = PTR_ERR(phydev);
                                goto error;
                        }
Signature · 1 line
Florian
#7 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

In reply to Sergei Shtylyov
Quoted 75 lines · On Wednesday 27 April 2016 23:09:37 Sergei Shtylyov wrote:

Hello.

Quoted 24 lines · Andrew Lunn
Quoted 24 lines
Quoted 22 lines · Sergei Shtylyov

Arnd Bergmann asked that get_phy_device() returns either NULL or the error value, not both on error. Do as he said, return ERR_PTR(-ENODEV) instead of NULL when the PHY ID registers read as all ones.

---
  drivers/net/phy/phy_device.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Index: net-next/drivers/net/phy/phy_device.c
===================================================================

drivers/net/phy/phy_device.c +1 -1
@@ -529,7 +529,7 @@ struct phy_device *get_phy_device(struct
 
  	/* If the phy_id is mostly Fs, there is no device there */
  	if ((phy_id & 0x1fffffff) == 0x1fffffff)
-		return NULL;
+		return ERR_PTR(-ENODEV);
 
Quoted 5 lines
Quoted 2 lines

drivers/net/phy/phy_device.c struct phy_device *get_phy_device(struct · line 535

Quoted 2 lines
drivers/net/phy/phy_device.c +0 -0
  	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  }

This change is wrong, it needs reverting, or the call sights need fixing to expect ENODEV.

    So this function had a good reason to return NULL, as it turned out... :-(
Quoted 35 lines

The point is, the device not being there is not an error, with respect to the code calling this function.

It gets called by mdiobus_scan()

struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)

{
         struct phy_device *phydev;
         int err;

         phydev = get_phy_device(bus, addr, false);
         if (IS_ERR(phydev) || phydev == NULL)
                 return phydev;

So before, we return NULL, if the device was not there. Now we return ERR_PTR(-ENODEV).

This is being called by:

int __mdiobus_register(struct mii_bus *bus, struct module *owner)

{
         struct mdio_device *mdiodev;
...
         for (i = 0; i < PHY_MAX_ADDR; i++) {
                 if ((bus->phy_mask & (1 << i)) == 0) {
                         struct phy_device *phydev;

                         phydev = mdiobus_scan(bus, i);
                         if (IS_ERR(phydev)) {
                                 err = PTR_ERR(phydev);
                                 goto error;
                         }
                 }
         }
Quoted 3 lines

This is treating ERR_PTR(-ENODEV) as a fatal error, where as before IS_ERR(NULL) would be false and it would continue scanning other addresses on the bus.

Quoted 6 lines

Thank you for the detailed analysis! (And shame on me for the lack of it.)

Quoted 2 lines

Please revert this, or fix all the callsites such that ENODEV is not a fatal error.

OK, I'll do what DaveM decides.

I found one other user that remains broken: pxa168_init_phy() looks wrong before and after the patch:

        pep->phy = mdiobus_scan(pep->smi_bus, pep->phy_addr);
        if (!pep->phy)
                return -ENODEV;

        err = phy_connect_direct(dev, pep->phy, pxa168_eth_adjust_link,
                                 pep->phy_intf);

as phy_connect_direct() will go on and dereference an error pointer. This should check for IS_ERR(), and with the patches applied, we can drop the !pep->phy check.

Arnd

#9 lore raw

Re: [PATCH 1/5] phylib: don't return NULL from get_phy_device()

In reply to Florian Fainelli
Quoted 13 lines · On Wednesday 27 April 2016 14:47:29 Florian Fainelli wrote:
drivers/net/phy/mdio_bus.c +1 -1
index 499003ee8055..94a27b028dd8 100644
@@ -333,7 +333,7 @@ int __mdiobus_register(struct mii_bus *bus, struct
 module *owner)
                        struct phy_device *phydev;
 
                        phydev = mdiobus_scan(bus, i);
-                       if (IS_ERR(phydev)) {
+                       if (IS_ERR(phydev) && PTR_ERR(phydev) != -ENODEV) {
                                err = PTR_ERR(phydev);
                                goto error;
Quoted 3 lines
                        }

I think that is an improvement over the original code, and better than reverting the series. Out of the three callers of mdiobus_scan, I already commented on drivers/net/ethernet/marvell/pxa168_eth.c being wrong to start with, and drivers/net/ethernet/cadence/macb.c seems to require the same fix that you did here for mdio_bus.c

Arnd