Skip to contentSkip to conversation

net: phy: fix duplicate eth_phy binding

PATCH Superseded Pranav Tilak 2 participants 4 messages May 13 – May 17

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 net: phy: fix duplicate eth_phy binding

When both CONFIG_PHY_ETHERNET_ID and CONFIG_DM_ETH_PHY are enabled, eth_phy_binds_nodes() called from eth_post_bind() already binds the ethernet PHY node to eth_phy_generic_drv. However, phy_connect_phy_id() called via phy_connect() also binds the same PHY node, resulting in duplicate entries in the DM tree.

Fix this by checking at the beginning of phy_connect() whether the PHY is already bound via uclass_find_device_by_phandle(). If so, skip all generic binding methods and fall through to phy_find_by_mask(). Also assign phydev->node from the already bound DM device.

Fixes 68a4d1506109 ("net: phy: Bind ETH_PHY uclass driver to each new PHY")

---
Changes in v5:
- Use int ret instead of bool phy_bound and goto out

Changes in v4:
- Removed IS_ENABLED(CONFIG_DM_ETH_PHY), use runtime check via uclass_find_device_by_phandle()
- Fix indentation of binding methods block
- Assign phydev->node from the bound DM device's ofnode

Changes in v3:
- Moved duplicate binding check to beginning of phy_connect()

Changes in v2:
- Move duplicate binding check to caller phy_connect()
- Update commit description

---
 drivers/net/phy/phy.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/net/phy/phy.c +13 -0
index d7e0c4fe02d..7d698f00c09 100644
@@ -24,6 +24,7 @@
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/compiler.h>
+#include <dm/uclass-internal.h>
 
 /* Generic PHY support and helper functions */
 
@@ -927,6 +928,14 @@ struct phy_device *phy_connect(struct mii_dev *bus, int addr,
 {
 	struct phy_device *phydev = NULL;
 	uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
+	struct udevice *phy_dev;
+	int ret;
+
+	/* Skip binding if PHY already bound by eth_phy_binds_nodes(). */
+	ret = uclass_find_device_by_phandle(UCLASS_ETH_PHY, dev,
+					    "phy-handle", &phy_dev);
+	if (!ret)
+		goto out;
 
 #ifdef CONFIG_PHY_FIXED
 	phydev = phy_connect_fixed(bus, dev);
@@ -947,9 +956,13 @@ struct phy_device *phy_connect(struct mii_dev *bus, int addr,
 		phydev = phy_connect_gmii2rgmii(bus, dev);
 #endif
 
+out:
 	if (!phydev)
 		phydev = phy_find_by_mask(bus, mask);
 
+	if (phydev && !ret && !ofnode_valid(phydev->node))
+		phydev->node = dev_ofnode(phy_dev);
+
 	if (phydev)
 		phy_connect_dev(phydev, dev, interface);
 	else
Signature · 1 line
2.34.1
#1 lore raw
Quoted 61 lines · Pranav Tilak

When both CONFIG_PHY_ETHERNET_ID and CONFIG_DM_ETH_PHY are enabled, eth_phy_binds_nodes() called from eth_post_bind() already binds the ethernet PHY node to eth_phy_generic_drv. However, phy_connect_phy_id() called via phy_connect() also binds the same PHY node, resulting in duplicate entries in the DM tree.

Fix this by checking at the beginning of phy_connect() whether the PHY is already bound via uclass_find_device_by_phandle(). If so, skip all generic binding methods and fall through to phy_find_by_mask(). Also assign phydev->node from the already bound DM device.

Fixes 68a4d1506109 ("net: phy: Bind ETH_PHY uclass driver to each new PHY")

---
Changes in v5:
- Use int ret instead of bool phy_bound and goto out

Changes in v4:
- Removed IS_ENABLED(CONFIG_DM_ETH_PHY), use runtime check via uclass_find_device_by_phandle()
- Fix indentation of binding methods block
- Assign phydev->node from the bound DM device's ofnode

Changes in v3:
- Moved duplicate binding check to beginning of phy_connect()

Changes in v2:
- Move duplicate binding check to caller phy_connect()
- Update commit description

---
  drivers/net/phy/phy.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/net/phy/phy.c +9 -0
index d7e0c4fe02d..7d698f00c09 100644
@@ -24,6 +24,7 @@
  #include <linux/delay.h>
  #include <linux/err.h>
  #include <linux/compiler.h>
+#include <dm/uclass-internal.h>
 
  /* Generic PHY support and helper functions */
 
@@ -927,6 +928,14 @@ struct phy_device *phy_connect(struct mii_dev *bus, int addr,
  {
  	struct phy_device *phydev = NULL;
  	uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
+	struct udevice *phy_dev;
+	int ret;
+
+	/* Skip binding if PHY already bound by eth_phy_binds_nodes(). */
+	ret = uclass_find_device_by_phandle(UCLASS_ETH_PHY, dev,
+					    "phy-handle", &phy_dev);
+	if (!ret)
+		goto out;
 
  #ifdef CONFIG_PHY_FIXED
  	phydev = phy_connect_fixed(bus, dev);

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev *bus, int addr, · line 964

Quoted 9 lines
drivers/net/phy/phy.c +3 -0
@@ -947,9 +956,13 @@ struct phy_device *phy_connect(struct mii_dev *bus, int addr,
  		phydev = phy_connect_gmii2rgmii(bus, dev);
  #endif
 
+out:
  	if (!phydev)
  		phydev = phy_find_by_mask(bus, mask);
 
+	if (phydev && !ret && !ofnode_valid(phydev->node))
+		phydev->node = dev_ofnode(phy_dev);

Is the !ret test necessary in this conditional ?

#2 lore raw

Public

Hi Marek,

Quoted 84 lines

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Wednesday, May 13, 2026 7:09 PM
To: Vinay Tilak, Pranav <[email protected]>; [email protected]; Simek, Michal <[email protected]>
Cc: git (AMD-Xilinx) <[email protected]>; Begari, Padmarao <[email protected]>; Jerome Forissier <[email protected]>; Tom Rini <[email protected]>; Peng Fan <[email protected]>; Patrice Chotard <[email protected]>; Lucien.Jheng <[email protected]>; Siddharth Vadapalli <s- [email protected]>; SkyLake.Huang <[email protected]>; Marek Vasut <[email protected]>; Ramon Fried <[email protected]>
Subject: Re: [PATCH v5] net: phy: fix duplicate eth_phy binding

Quoted 1 line · Pranav Tilak

When both CONFIG_PHY_ETHERNET_ID and CONFIG_DM_ETH_PHY are

enabled,

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev

Quoted 46 lines

eth_phy_binds_nodes() called from eth_post_bind() already binds the ethernet PHY node to eth_phy_generic_drv. However, phy_connect_phy_id() called via phy_connect() also binds the same PHY node, resulting in duplicate entries in the DM tree.

Fix this by checking at the beginning of phy_connect() whether the PHY is already bound via uclass_find_device_by_phandle(). If so, skip all generic binding methods and fall through to phy_find_by_mask(). Also assign phydev->node from the already bound DM device.

Fixes 68a4d1506109 ("net: phy: Bind ETH_PHY uclass driver to each new

PHY")

---
Changes in v5:
- Use int ret instead of bool phy_bound and goto out

Changes in v4:
- Removed IS_ENABLED(CONFIG_DM_ETH_PHY), use runtime check via uclass_find_device_by_phandle()
- Fix indentation of binding methods block
- Assign phydev->node from the bound DM device's ofnode

Changes in v3:
- Moved duplicate binding check to beginning of phy_connect()

Changes in v2:
- Move duplicate binding check to caller phy_connect()
- Update commit description

---
  drivers/net/phy/phy.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/net/phy/phy.c index renamed from drivers/net/phy/phy.c +0 -0

d7e0c4fe02d..7d698f00c09 100644

drivers/net/phy/phy.c +1 -0
@@ -24,6 +24,7 @@
  #include <linux/delay.h>
  #include <linux/err.h>
  #include <linux/compiler.h>
+#include <dm/uclass-internal.h>
 
  /* Generic PHY support and helper functions */
 
@@ -927,6 +928,14 @@ struct phy_device *phy_connect(struct mii_dev

*bus, int addr,

Quoted 4 lines
drivers/net/phy/phy.c +1 -0
  {
    struct phy_device *phydev = NULL;
    uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
+   struct udevice *phy_dev;

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev *bus, int addr,

Quoted 10 lines
drivers/net/phy/phy.c +7 -0
+   int ret;
+
+   /* Skip binding if PHY already bound by eth_phy_binds_nodes(). */
+   ret = uclass_find_device_by_phandle(UCLASS_ETH_PHY, dev,
+                                       "phy-handle", &phy_dev);
+   if (!ret)
+           goto out;
 
  #ifdef CONFIG_PHY_FIXED
    phydev = phy_connect_fixed(bus, dev); @@ -947,9 +956,13 @@

struct

Quoted 4 lines

phy_device *phy_connect(struct mii_dev *bus, int addr,

            phydev = phy_connect_gmii2rgmii(bus, dev);

#endif

Quoted 7 lines

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev *bus, int addr, · line 964

Quoted 6 lines
drivers/net/phy/phy.c +3 -0
+out:
    if (!phydev)
            phydev = phy_find_by_mask(bus, mask);
 
+   if (phydev && !ret && !ofnode_valid(phydev->node))
+           phydev->node = dev_ofnode(phy_dev);

Is the !ret test necessary in this conditional ?

Yes i checked, !ret is necessary as uclass_find_device_by_phandle() always sets phy_dev= NULL on entry, when it fails to find an already bound PHY phy_dev is NULL. Removing !ret would call dev_ofnode(NULL) which dereferences a NULL pointer and crashes.

!ret also serves as a guard for the assignment of phydev->node it should only happen when the PHY is already DM bound. When binding methods run (ret != 0), they already set phydev->node correctly so no need to again set phydev->node in that case.

The !ofnode_valid(phydev->node) check is redundant and can be dropped. In the ret == 0 path, only phy_find_by_mask() runs and it never sets phydev->node, so !ofnode_valid(phydev->node) is always true. Hence condition can be simplified to:

if (phydev && !ret)

    phydev->node = dev_ofnode(phy_dev);

Thanks & regards,
Pranav Tilak

#3 lore raw

On 5/15/26 1:13 PM, Vinay Tilak, Pranav wrote:

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/net/phy/phy.c index renamed from drivers/net/phy/phy.c +0 -0

d7e0c4fe02d..7d698f00c09 100644

drivers/net/phy/phy.c +1 -0
@@ -24,6 +24,7 @@
   #include <linux/delay.h>
   #include <linux/err.h>
   #include <linux/compiler.h>
+#include <dm/uclass-internal.h>
 
Quoted 8 lines
Quoted 8 lines

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev

Quoted 3 lines
drivers/net/phy/phy.c +0 -0
   /* Generic PHY support and helper functions */
 
@@ -927,6 +928,14 @@ struct phy_device *phy_connect(struct mii_dev

*bus, int addr,

Quoted 4 lines
drivers/net/phy/phy.c +1 -0
   {
     struct phy_device *phydev = NULL;
     uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff;
+   struct udevice *phy_dev;

Please pick a variable name which is not phy_dev , there is already a phydev variable in this function and the naming is VERY confusing.

Quoted 7 lines
Quoted 7 lines

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev *bus, int addr, · line 938

Quoted 7 lines
drivers/net/phy/phy.c +7 -0
+   int ret;
+
+   /* Skip binding if PHY already bound by eth_phy_binds_nodes(). */
+   ret = uclass_find_device_by_phandle(UCLASS_ETH_PHY, dev,
+                                       "phy-handle", &phy_dev);
+   if (!ret)
+           goto out;

You already check if (!ret) here ...

Quoted 11 lines
Quoted 11 lines

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev · line 940

Quoted 2 lines
drivers/net/phy/phy.c +0 -0
   #ifdef CONFIG_PHY_FIXED
     phydev = phy_connect_fixed(bus, dev); @@ -947,9 +956,13 @@

struct

Quoted 8 lines
drivers/net/phy/phy.c +0 -0
 phy_device *phy_connect(struct mii_dev *bus, int addr,
             phydev = phy_connect_gmii2rgmii(bus, dev);

#endif

+out:

     if (!phydev)
             phydev = phy_find_by_mask(bus, mask);
Quoted 5 lines
Quoted 3 lines

drivers/net/phy/phy.c struct phy_device *phy_connect(struct mii_dev *bus, int addr, · line 964

Quoted 2 lines
drivers/net/phy/phy.c +2 -0
+   if (phydev && !ret && !ofnode_valid(phydev->node))
+           phydev->node = dev_ofnode(phy_dev);

Is the !ret test necessary in this conditional ?

Yes i checked, !ret is necessary as uclass_find_device_by_phandle() always sets phy_dev= NULL on entry, when it fails to find an already bound PHY phy_dev is NULL. Removing !ret would call dev_ofnode(NULL) which dereferences a NULL pointer and crashes.

... so I think the goto jump target needs to be adjusted ?

In fact, I do not understand why does this code call phy_find_by_mask() if the PHY udevice was already found by uclass_find_device_by_phandle() ? Can you not convert that already found PHY udevice into phydev ?

Quoted 1 line

!ret also serves as a guard for the assignment of phydev->node it should only happen when the PHY is already DM bound. When binding methods run (ret != 0), they already set phydev->node correctly so no need to again set phydev->node in that case.

If uclass_find_device_by_phandle() does locate PHY udevice , it will return 0, and the !ret conditional will evaluate to true , so phydev->node = dev_ofnode(phy_dev); will run in that case ?

Quoted 5 lines

The !ofnode_valid(phydev->node) check is redundant and can be dropped. In the ret == 0 path, only phy_find_by_mask() runs and it never sets phydev->node, so !ofnode_valid(phydev->node) is always true. Hence condition can be simplified to:

if (phydev && !ret)

     phydev->node = dev_ofnode(phy_dev);

[...]