Skip to contentSkip to conversation

net: phy: Create sysfs reciprocal links for attached_dev/phydev

PATCH Superseded Florian Fainelli 3 participants 19 messages May 24, 2017 – May 27, 2017

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: phy: Create sysfs reciprocal links for attached_dev/phydev

Hi David, Andrew,

This patch series addresses a device topology shortcoming where a program scanning /sys would not be able to establish a mapping between the network device and the PHY device.

In the process it turned out that no PHY device documentation existed for sysfs attributes.

Thanks!

Florian Fainelli (3):
net: phy: Create sysfs reciprocal links for attached_dev/phydev
net: sysfs: Document "phydev" symbolic link
net: sysfs: Document PHY device sysfs attributes

 Documentation/ABI/testing/sysfs-class-net        |  8 ++++++
 Documentation/ABI/testing/sysfs-class-net-phydev | 32 ++++++++++++++++++++++++
 drivers/net/phy/phy_device.c                     | 11 ++++++++
 3 files changed, 51 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-net-phydev
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/3 net: phy: Create sysfs reciprocal links for attached_dev/phydev

There is currently no way for a program scanning /sys to know whether a network device is attached to a particular PHY device, just like the PHY device is not pointed back to its attached network device.

Create a symbolic link in the network device's namespace named "phydev" which points to the PHY device and create a symbolic link in the PHY device's namespace named "attached_dev" that points back to the network device. These links are set up during phy_attach_direct() and removed during phy_detach() for symetry.

---
 drivers/net/phy/phy_device.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/net/phy/phy_device.c +11 -0
index 1219eeab69d1..8151477c7027 100644
@@ -960,6 +960,15 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 
 	phydev->attached_dev = dev;
 	dev->phydev = phydev;
+	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
+				"attached_dev");
+	if (err)
+		goto error;
+
+	err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
+				"phydev");
+	if (err)
+		goto error;
 
 	phydev->dev_flags = flags;
 
@@ -1050,6 +1059,8 @@ void phy_detach(struct phy_device *phydev)
 	struct mii_bus *bus;
 	int i;
 
+	sysfs_remove_link(&dev->dev.kobj, "phydev");
+	sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
 	phydev->attached_dev->phydev = NULL;
 	phydev->attached_dev = NULL;
 	phy_suspend(phydev);
Signature · 1 line
2.9.3
#2 lore raw
Quoted 5 lines
drivers/net/phy/phy_device.c +0 -0
@@ -960,6 +960,15 @@ int phy_attach_direct(struct net_device *dev, struct
 phy_device *phydev,
 
 	phydev->attached_dev = dev;
 	dev->phydev = phydev;

drivers/net/phy/phy_device.c int phy_attach_direct(struct net_device *dev, struct · line 973

Quoted 10 lines
drivers/net/phy/phy_device.c +9 -0
+	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
+				"attached_dev");
+	if (err)
+		goto error;
+
+	err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
+				"phydev");
+	if (err)
+		goto error;
 

Florian,

I knew that it is applied to net-next.
However, sysfs_create_link() fails when fixed phy (drivers/net/phy/fixed_phy.c) Do you have a chance to test with it?

- Woojung

#3 lore raw

Hi Woojung,

Quoted 17 lines · On 05/26/2017 04:34 PM, [email protected] wrote:
Quoted 5 lines
drivers/net/phy/phy_device.c +0 -0
@@ -960,6 +960,15 @@ int phy_attach_direct(struct net_device *dev, struct
 phy_device *phydev,
 
 	phydev->attached_dev = dev;
 	dev->phydev = phydev;

drivers/net/phy/phy_device.c int phy_attach_direct(struct net_device *dev, struct · line 973

Quoted 10 lines
drivers/net/phy/phy_device.c +9 -0
+	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
+				"attached_dev");
+	if (err)
+		goto error;
+
+	err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
+				"phydev");
+	if (err)
+		goto error;
 

Florian,

Quoted 3 lines

I knew that it is applied to net-next.
However, sysfs_create_link() fails when fixed phy (drivers/net/phy/fixed_phy.c) Do you have a chance to test with it?

I did, my main test system (BCM7445 w/ bcm_sf2) has one normal PHY driver and 3 fixed PHYs (including one for the CPU port/master netdev), see below.

What kind of error do you get here?

# ls -l /sys/class/net/gphy/phydev
lrwxrwxrwx    1 root     root             0 Jan  1 00:00
/sys/class/net/gphy/phydev ->
../../../f0b403c0.mdio/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05
# ls -l /sys/class/net/*/phydev
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/eth0/phydev -> ../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:00
lrwxrwxrwx    1 root     root             0 Jan  1 00:00
/sys/class/net/gphy/phydev ->
../../../f0b403c0.mdio/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/moca/phydev -> ../../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:02
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/rgmii_1/phydev -> ../../../mdio_bus/sf2-1/sf2-1:00
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/rgmii_2/phydev -> ../../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:01
# ls -l /sys/class/mdio_bus/fixed-0/*/attached_dev
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/mdio_bus/fixed-0/fixed-0:00/attached_dev ->
../../../../rdb/f04a0000.ethernet/net/eth0
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/fixed-0/fixed-0:01/attached_dev ->
../../../../rdb/rdb:switch_top@f0b00000/f0b00000.ethernet_switch/net/rgmii_2
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/fixed-0/fixed-0:02/attached_dev ->
../../../../rdb/rdb:switch_top@f0b00000/f0b00000.ethernet_switch/net/moca
# ls -l /sys/class/mdio_bus/f0b403c0.mdio/f0b403c0.mdio\:05/attached_dev
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05/attached_dev ->
../../../../f0b00000.ethernet_switch/net/gphy
Quoted 3 lines

- Woojung

Signature · 1 line
Florian
#4 lore raw

Hi Florian,

Quoted 10 lines
Quoted 2 lines

I knew that it is applied to net-next.
However, sysfs_create_link() fails when fixed phy

(drivers/net/phy/fixed_phy.c)

Quoted 1 line

Do you have a chance to test with it?

I did, my main test system (BCM7445 w/ bcm_sf2) has one normal PHY driver and 3 fixed PHYs (including one for the CPU port/master netdev), see below.

What kind of error do you get here?

sysfs_create_link() returns -2 (-ENOENT).

Quoted 34 lines
# ls -l /sys/class/net/gphy/phydev
lrwxrwxrwx    1 root     root             0 Jan  1 00:00
/sys/class/net/gphy/phydev ->
../../../f0b403c0.mdio/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05
# ls -l /sys/class/net/*/phydev
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/eth0/phydev -> ../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:00
lrwxrwxrwx    1 root     root             0 Jan  1 00:00
/sys/class/net/gphy/phydev ->
../../../f0b403c0.mdio/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/moca/phydev -> ../../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:02
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/rgmii_1/phydev -> ../../../mdio_bus/sf2-1/sf2-1:00
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/rgmii_2/phydev -> ../../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:01
# ls -l /sys/class/mdio_bus/fixed-0/*/attached_dev
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/mdio_bus/fixed-0/fixed-0:00/attached_dev ->
../../../../rdb/f04a0000.ethernet/net/eth0
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/fixed-0/fixed-0:01/attached_dev ->
../../../../rdb/rdb:switch_top@f0b00000/f0b00000.ethernet_switch/net/rgm
ii_2
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/fixed-0/fixed-0:02/attached_dev ->
../../../../rdb/rdb:switch_top@f0b00000/f0b00000.ethernet_switch/net/moc
a
Quoted 4 lines
# ls -l /sys/class/mdio_bus/f0b403c0.mdio/f0b403c0.mdio\:05/attached_dev
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05/attached_dev ->
../../../../f0b00000.ethernet_switch/net/gphy

/sys/class/mdio_bus/devices/fixed-0:00/* exists.
But not /sys/class/net/eth0.. because Ethernet driver initialization failed.

- Woojung

#5 lore raw
Quoted 48 lines · On 05/26/2017 04:52 PM, [email protected] wrote:

Hi Florian,

Quoted 10 lines
Quoted 2 lines

I knew that it is applied to net-next.
However, sysfs_create_link() fails when fixed phy

(drivers/net/phy/fixed_phy.c)

Quoted 1 line

Do you have a chance to test with it?

I did, my main test system (BCM7445 w/ bcm_sf2) has one normal PHY driver and 3 fixed PHYs (including one for the CPU port/master netdev), see below.

What kind of error do you get here?

sysfs_create_link() returns -2 (-ENOENT).

Quoted 34 lines
# ls -l /sys/class/net/gphy/phydev
lrwxrwxrwx    1 root     root             0 Jan  1 00:00
/sys/class/net/gphy/phydev ->
../../../f0b403c0.mdio/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05
# ls -l /sys/class/net/*/phydev
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/eth0/phydev -> ../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:00
lrwxrwxrwx    1 root     root             0 Jan  1 00:00
/sys/class/net/gphy/phydev ->
../../../f0b403c0.mdio/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/moca/phydev -> ../../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:02
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/rgmii_1/phydev -> ../../../mdio_bus/sf2-1/sf2-1:00
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/net/rgmii_2/phydev -> ../../../../../Fixed MDIO
bus.0/mdio_bus/fixed-0/fixed-0:01
# ls -l /sys/class/mdio_bus/fixed-0/*/attached_dev
lrwxrwxrwx    1 root     root             0 Jan  1 00:01
/sys/class/mdio_bus/fixed-0/fixed-0:00/attached_dev ->
../../../../rdb/f04a0000.ethernet/net/eth0
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/fixed-0/fixed-0:01/attached_dev ->
../../../../rdb/rdb:switch_top@f0b00000/f0b00000.ethernet_switch/net/rgm
ii_2
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/fixed-0/fixed-0:02/attached_dev ->
../../../../rdb/rdb:switch_top@f0b00000/f0b00000.ethernet_switch/net/moc
a
Quoted 7 lines
Quoted 4 lines
# ls -l /sys/class/mdio_bus/f0b403c0.mdio/f0b403c0.mdio\:05/attached_dev
lrwxrwxrwx    1 root     root             0 Jan  1 00:02
/sys/class/mdio_bus/f0b403c0.mdio/f0b403c0.mdio:05/attached_dev ->
../../../../f0b00000.ethernet_switch/net/gphy

/sys/class/mdio_bus/devices/fixed-0:00/* exists.
But not /sys/class/net/eth0.. because Ethernet driver initialization failed.

OK, I am confused now. You are describing what is going on with your platform right? Can you describe a bit further here what is happening and with which type of interface? Is this with the CPU interface or something?

Signature · 1 line
Florian
#6 lore raw
Quoted 4 lines

OK, I am confused now. You are describing what is going on with your platform right? Can you describe a bit further here what is happening and with which type of interface? Is this with the CPU interface or something?

Yes. It's on our platform.
Like your platform, fixed phy is used to connect switch CPU port/master netdev. GMAC of SoC is cadence/macb.c with fixed phy modification.

static int macb_mii_probe(struct net_device *dev)

{
	...
	phydev = phy_find_first(bp->mii_bus);
	if (!phydev) {
		phydev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
		if (IS_ERR(phydev)) {
			netdev_err(dev, "no PHY found\n");
			return -ENXIO;
		}
	}
	...
When failed to find phydev from phy_find_first(), it forces to fixed phy.
	...
	/* attach the mac to the phy */
	ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
				 bp->phy_interface);

sysfs_create_lin() inside of phy_connect_direct() fails.

What is driver you are testing? I can check the file.

Thanks.
- Woojung

#7 lore raw
Quoted 26 lines · On 05/26/2017 05:20 PM, [email protected] wrote:
Quoted 4 lines

OK, I am confused now. You are describing what is going on with your platform right? Can you describe a bit further here what is happening and with which type of interface? Is this with the CPU interface or something?

Yes. It's on our platform.
Like your platform, fixed phy is used to connect switch CPU port/master netdev. GMAC of SoC is cadence/macb.c with fixed phy modification.

static int macb_mii_probe(struct net_device *dev)

{
	...
	phydev = phy_find_first(bp->mii_bus);
	if (!phydev) {
		phydev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
		if (IS_ERR(phydev)) {
			netdev_err(dev, "no PHY found\n");
			return -ENXIO;
		}> 	}
	...
When failed to find phydev from phy_find_first(), it forces to fixed phy.
	...
	/* attach the mac to the phy */
	ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
				 bp->phy_interface);
Quoted 1 line

sysfs_create_lin() inside of phy_connect_direct() fails.

OK, so here is what is happening: macb_mii_init() calls macb_mii_probe() and so by the time we call phy_connect_direct(), we have not called register_netdevice() yet, netdev_register_kobject() has not been called either, and so sysfs_create_link() fails....

Let me think about a way to solve that, even though I am leaning towards ignoring the errors from sysfs_create_link() rather than fixing each and every Ethernet driver to make it probe its MII bus *after* calling register_netdevice()....

Quoted 2 lines

What is driver you are testing? I can check the file.

Drivers involved are the following:

drivers/net/ethernet/broadcom/bcmsysport.c,
drivers/net/dsa/bcm_sf2.c
drivers/net/ethernet/broadcom/genet/
drivers/net/phy/bcm7xxx.c

Signature · 1 line
Florian
#8 lore raw

Hi Florian,

Quoted 4 lines

OK, so here is what is happening: macb_mii_init() calls macb_mii_probe() and so by the time we call phy_connect_direct(), we have not called register_netdevice() yet, netdev_register_kobject() has not been called either, and so sysfs_create_link() fails....

I just found same thing.
Yes, register_netdev() was not called at phy_connect_direct() time.

Quoted 4 lines

Let me think about a way to solve that, even though I am leaning towards ignoring the errors from sysfs_create_link() rather than fixing each and every Ethernet driver to make it probe its MII bus *after* calling register_netdevice()....

Agree.

Quoted 9 lines
Quoted 2 lines

What is driver you are testing? I can check the file.

Drivers involved are the following:

drivers/net/ethernet/broadcom/bcmsysport.c,
drivers/net/dsa/bcm_sf2.c
drivers/net/ethernet/broadcom/genet/
drivers/net/phy/bcm7xxx.c

Thanks for list of files.

- Woojung

#9 lore raw

Hi Woojung,

Quoted 6 lines · On 05/26/2017 05:43 PM, [email protected] wrote:

Hi Florian,

Quoted 4 lines

OK, so here is what is happening: macb_mii_init() calls macb_mii_probe() and so by the time we call phy_connect_direct(), we have not called register_netdevice() yet, netdev_register_kobject() has not been called either, and so sysfs_create_link() fails....

Quoted 8 lines

I just found same thing.
Yes, register_netdev() was not called at phy_connect_direct() time.

Quoted 4 lines

Let me think about a way to solve that, even though I am leaning towards ignoring the errors from sysfs_create_link() rather than fixing each and every Ethernet driver to make it probe its MII bus *after* calling register_netdevice()....

Agree.

Thanks, would the following work for you? I don't want to do something more complex than that, although, if we really wanted to see this information, we could imagine having netdev_register_kobject() check whether phydev->dev.kobj is valid, and set the symbolic link at that point... The problem with that approach is that we are no longer symetrical within the core PHYLIB code (phy_attach_direct and phy_detach).

Let me know if this works so I can make a formal submission:

drivers/net/phy/phy_device.c +0 -0
index f84414b8f2ee..daad816ee1d1 100644
@@ -960,14 +960,20 @@ int phy_attach_direct(struct net_device *dev,

struct phy_device *phydev,

        phydev->attached_dev = dev;
        dev->phydev = phydev;

+
+ /* Some Ethernet drivers try to connect to a PHY device before
+ * calling register_netdevice(). register_netdevice() does
ultimately
+ * lead to netdev_register_kobject() which would do the
dev->dev.kobj
+ * initialization. Here we explicitly ignore those particular errors
+ */

        err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
                                "attached_dev");
-       if (err)
+       if (err && err != -ENOENT)
                goto error;

        err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
                                "phydev");
-       if (err)
+       if (err && err != -ENOENT)
                goto error;

        phydev->dev_flags = flags;
Signature · 1 line
Florian
#10 lore raw

Hi Florian,

Quoted 37 lines
Quoted 4 lines
Quoted 4 lines

OK, so here is what is happening: macb_mii_init() calls macb_mii_probe() and so by the time we call phy_connect_direct(), we have not called register_netdevice() yet, netdev_register_kobject() has not been called either, and so sysfs_create_link() fails....

Quoted 8 lines

I just found same thing.
Yes, register_netdev() was not called at phy_connect_direct() time.

Quoted 4 lines

Let me think about a way to solve that, even though I am leaning towards ignoring the errors from sysfs_create_link() rather than fixing each and every Ethernet driver to make it probe its MII bus *after* calling register_netdevice()....

Agree.

Thanks, would the following work for you? I don't want to do something more complex than that, although, if we really wanted to see this information, we could imagine having netdev_register_kobject() check whether phydev->dev.kobj is valid, and set the symbolic link at that point... The problem with that approach is that we are no longer symetrical within the core PHYLIB code (phy_attach_direct and phy_detach).

Let me know if this works so I can make a formal submission:

drivers/net/phy/phy_device.c +3 -0
index f84414b8f2ee..daad816ee1d1 100644
@@ -960,14 +960,20 @@ int phy_attach_direct(struct net_device *dev,
 struct phy_device *phydev,
 
        phydev->attached_dev = dev;
        dev->phydev = phydev;
+
+       /* Some Ethernet drivers try to connect to a PHY device before
+        * calling register_netdevice(). register_netdevice() does

ultimately
+ * lead to netdev_register_kobject() which would do the
dev->dev.kobj

drivers/net/phy/phy_device.c

Quoted 7 lines
drivers/net/phy/phy_device.c +3 -1
+        * initialization. Here we explicitly ignore those particular errors
+        */
        err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
                                "attached_dev");
-       if (err)
+       if (err && err != -ENOENT)
                goto error;

This one fine. However, next one returns -14 (-EFAULT)

drivers/net/phy/phy_device.c

Quoted 5 lines
        err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
                                "phydev");
drivers/net/phy/phy_device.c +1 -1
-       if (err)
+       if (err && err != -ENOENT)
                goto error;

No need to call 2nd sysfs_create_link(), how about following?

	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,

"attached_dev");
- if (err)
+ if (err && err != -ENOENT)
goto error;

- err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
- "phydev");
- if (err)
- goto error;
+ if (!err) {
+ err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
+ "phydev");
+ if (err)
+ goto error;
+ }

phydev->dev_flags = flags;

- Woojung

#11 lore raw
Quoted 60 lines · On 05/26/2017 06:00 PM, [email protected] wrote:

Hi Florian,

Quoted 37 lines
Quoted 4 lines
Quoted 4 lines

OK, so here is what is happening: macb_mii_init() calls macb_mii_probe() and so by the time we call phy_connect_direct(), we have not called register_netdevice() yet, netdev_register_kobject() has not been called either, and so sysfs_create_link() fails....

Quoted 8 lines

I just found same thing.
Yes, register_netdev() was not called at phy_connect_direct() time.

Quoted 4 lines

Let me think about a way to solve that, even though I am leaning towards ignoring the errors from sysfs_create_link() rather than fixing each and every Ethernet driver to make it probe its MII bus *after* calling register_netdevice()....

Agree.

Thanks, would the following work for you? I don't want to do something more complex than that, although, if we really wanted to see this information, we could imagine having netdev_register_kobject() check whether phydev->dev.kobj is valid, and set the symbolic link at that point... The problem with that approach is that we are no longer symetrical within the core PHYLIB code (phy_attach_direct and phy_detach).

Let me know if this works so I can make a formal submission:

drivers/net/phy/phy_device.c +3 -0
index f84414b8f2ee..daad816ee1d1 100644
@@ -960,14 +960,20 @@ int phy_attach_direct(struct net_device *dev,
 struct phy_device *phydev,
 
        phydev->attached_dev = dev;
        dev->phydev = phydev;
+
+       /* Some Ethernet drivers try to connect to a PHY device before
+        * calling register_netdevice(). register_netdevice() does

ultimately
+ * lead to netdev_register_kobject() which would do the
dev->dev.kobj

drivers/net/phy/phy_device.c

Quoted 7 lines
drivers/net/phy/phy_device.c +3 -1
+        * initialization. Here we explicitly ignore those particular errors
+        */
        err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
                                "attached_dev");
-       if (err)
+       if (err && err != -ENOENT)
                goto error;

This one fine. However, next one returns -14 (-EFAULT)

drivers/net/phy/phy_device.c

Quoted 5 lines
        err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
                                "phydev");
drivers/net/phy/phy_device.c +1 -1
-       if (err)
+       if (err && err != -ENOENT)
                goto error;

No need to call 2nd sysfs_create_link(), how about following?

	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
(quoted diff) +1 -1
 				"attached_dev");
-	if (err)
+	if (err && err != -ENOENT)
 		goto error;
 

Review

Quoted 10 lines
(quoted diff) +6 -4
-	err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
-				"phydev");
-	if (err)
-		goto error;
+	if (!err) {
+		err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
+					"phydev");
+		if (err)
+			goto error;
+	}

Yes, that's a very valid point, how about we even do this:

drivers/net/phy/phy_device.c +0 -0
index f84414b8f2ee..dc666ec13651 100644
@@ -960,15 +960,21 @@ int phy_attach_direct(struct net_device *dev,

struct phy_device *phydev,

        phydev->attached_dev = dev;
        dev->phydev = phydev;
+
+       /* Some Ethernet drivers try to connect to a PHY device before
+        * calling register_netdevice() -> netdev_register_kobject() and
+        * does the dev->dev.kobj initialization. Here we only check for
+        * success which indicates that the network device kobject is
+        * ready.
+        */
        err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
                                "attached_dev");

- if (err)
- goto error;
-
- err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
- "phydev");
- if (err)
- goto error;
+ if (!err) {
+ err = sysfs_create_link(&dev->dev.kobj,
&phydev->mdio.dev.kobj,
+ "phydev");
+ if (err)
+ goto error;
+ }

        phydev->dev_flags = flags;
Signature · 1 line
Florian
#12 lore raw
Quoted 36 lines

Yes, that's a very valid point, how about we even do this:

drivers/net/phy/phy_device.c +13 -7
index f84414b8f2ee..dc666ec13651 100644
@@ -960,15 +960,21 @@ int phy_attach_direct(struct net_device *dev,
 struct phy_device *phydev,
 
        phydev->attached_dev = dev;
        dev->phydev = phydev;
+
+       /* Some Ethernet drivers try to connect to a PHY device before
+        * calling register_netdevice() -> netdev_register_kobject() and
+        * does the dev->dev.kobj initialization. Here we only check for
+        * success which indicates that the network device kobject is
+        * ready.
+        */
        err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
                                "attached_dev");
-       if (err)
-               goto error;
-
-       err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
-                               "phydev");
-       if (err)
-               goto error;
+       if (!err) {
+               err = sysfs_create_link(&dev->dev.kobj,
 &phydev->mdio.dev.kobj,
+                                       "phydev");
+               if (err)
+                       goto error;
+       }
 
Quoted 2 lines
        phydev->dev_flags = flags;

Looks better and clean.

How about sysfs_remove_link() in phy_detach()?

- Woojung

#13 lore raw
Quoted 36 lines · On 05/26/2017 06:31 PM, [email protected] wrote:
Quoted 36 lines

Yes, that's a very valid point, how about we even do this:

drivers/net/phy/phy_device.c +13 -7
index f84414b8f2ee..dc666ec13651 100644
@@ -960,15 +960,21 @@ int phy_attach_direct(struct net_device *dev,
 struct phy_device *phydev,
 
        phydev->attached_dev = dev;
        dev->phydev = phydev;
+
+       /* Some Ethernet drivers try to connect to a PHY device before
+        * calling register_netdevice() -> netdev_register_kobject() and
+        * does the dev->dev.kobj initialization. Here we only check for
+        * success which indicates that the network device kobject is
+        * ready.
+        */
        err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
                                "attached_dev");
-       if (err)
-               goto error;
-
-       err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
-                               "phydev");
-       if (err)
-               goto error;
+       if (!err) {
+               err = sysfs_create_link(&dev->dev.kobj,
 &phydev->mdio.dev.kobj,
+                                       "phydev");
+               if (err)
+                       goto error;
+       }
 
Quoted 5 lines
Quoted 2 lines
        phydev->dev_flags = flags;

Looks better and clean.

How about sysfs_remove_link() in phy_detach()?

Good catch, I was able to reproduce the situation in which macb calls phy_connect_direct(), will submit a patch shortly. Thanks!

Signature · 1 line
Florian
#14 lore raw

PATCH 2/3 net: sysfs: Document "phydev" symbolic link

In reply to Florian Fainelli

Now that we link the network device to its PHY device, document this sysfs symbolic link.

---
 Documentation/ABI/testing/sysfs-class-net | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/ABI/testing/sysfs-class-net +8 -0
index 668604fc8e06..6856da99b6f7 100644
@@ -251,3 +251,11 @@ Contact:	[email protected]
 Description:
 		Indicates the unique physical switch identifier of a switch this
 		port belongs to, as a string.
+
+What:		/sys/class/net/<iface>/phydev
+Date:		May 2017
+KernelVersion:	4.13
+Contact:	[email protected]
+Description:
+		Symbolic link to the PHY device this network device is attached
+		to.
Signature · 1 line
2.9.3
#15 lore raw

PATCH 3/3 net: sysfs: Document PHY device sysfs attributes

In reply to Florian Fainelli

Document the different sysfs attributes that exist for PHY devices:
attached_dev, phy_has_fixups, phy_id and phy_interface.

---
 Documentation/ABI/testing/sysfs-class-net-phydev | 32 ++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-net-phydev
Documentation/ABI/testing/sysfs-class-net-phydev new file +32 -0
new file mode 100644
index 000000000000..a05831667c3d
@@ -0,0 +1,32 @@
+What:		/sys/class/mdio_bus/<bus>/<device>/attached_dev
+Date:		May 2017
+KernelVersion:	4.13
+Contact:	[email protected]
+Description:
+		Symbolic link to the network device this PHY device is
+		attached to.
+
+What:		/sys/class/mdio_bus/<bus>/<device>/phy_has_fixups
+Date:		February 2014
+KernelVersion:	3.15
+Contact:	[email protected]
+Description:
+		Boolean value indicating whether the PHY device has
+		any fixups registered against it (phy_register_fixup)
+
+What:		/sys/class/mdio_bus/<bus>/<device>/phy_id
+Date:		November 2012
+KernelVersion:	3.8
+Contact:	[email protected]
+Description:
+		32-bit hexadecimal value corresponding to the PHY device's OUI,
+		model and revision number.
+
+What:		/sys/class/mdio_bus/<bus>/<device>/phy_interface
+Date:		February 2014
+KernelVersion:	3.15
+Contact:	[email protected]
+Description:
+		String value indicating the PHY interface, possible
+		values are in include/linux/phy.h function phy_modes.
+
Signature · 1 line
2.9.3
maintainer
#16 lore raw

Re: [PATCH net-next 3/3] net: sysfs: Document PHY device sysfs attributes

Documentation/ABI/testing/sysfs-class-net-phydev line 31

Quoted 7 lines
Documentation/ABI/testing/sysfs-class-net-phydev new file +7 -0
+What:		/sys/class/mdio_bus/<bus>/<device>/phy_interface
+Date:		February 2014
+KernelVersion:	3.15
+Contact:	[email protected]
+Description:
+		String value indicating the PHY interface, possible
+		values are in include/linux/phy.h function phy_modes.

Hi Florian

Does this suggest that these strings should be moved to include/uapi/linux/phy.h?

Andrew

#17 lore raw

Re: [PATCH net-next 3/3] net: sysfs: Document PHY device sysfs attributes

Hi Andrew,

Quoted 10 lines · Andrew Lunn

Documentation/ABI/testing/sysfs-class-net-phydev line 31

Quoted 7 lines
Documentation/ABI/testing/sysfs-class-net-phydev new file +7 -0
+What:		/sys/class/mdio_bus/<bus>/<device>/phy_interface
+Date:		February 2014
+KernelVersion:	3.15
+Contact:	[email protected]
+Description:
+		String value indicating the PHY interface, possible
+		values are in include/linux/phy.h function phy_modes.

Hi Florian

Quoted 2 lines

Does this suggest that these strings should be moved to include/uapi/linux/phy.h?

Humm, I suppose we could do that, although I am not sure ally sure what we would be putting in there (at least for now).

Signature · 1 line
Florian
#18 lore raw

Re: [PATCH net-next 3/3] net: sysfs: Document PHY device sysfs attributes

Quoted 13 lines · Florian Fainelli

Hi Andrew,

Quoted 10 lines · Andrew Lunn

Documentation/ABI/testing/sysfs-class-net-phydev line 31

Quoted 7 lines
Documentation/ABI/testing/sysfs-class-net-phydev new file +7 -0
+What:		/sys/class/mdio_bus/<bus>/<device>/phy_interface
+Date:		February 2014
+KernelVersion:	3.15
+Contact:	[email protected]
+Description:
+		String value indicating the PHY interface, possible
+		values are in include/linux/phy.h function phy_modes.

Hi Florian

Quoted 5 lines
Quoted 2 lines

Does this suggest that these strings should be moved to include/uapi/linux/phy.h?

Humm, I suppose we could do that, although I am not sure ally sure what we would be putting in there (at least for now).

I actually prefer to put the possible values in the sysfs documentation files instead of in a UAPI header. Will submit a v2.

Signature · 1 line
Florian