[PATCH net] net: bgmac: unregister the fixed PHY on probe failure and removal

From: Florian Fainelli

Date: Thu Nov 19 2026 - 19:31:05 EST


bgmac registers a fixed PHY, either for a chip without a PHY with
bgmac_phy_connect_direct(), or through a fixed-link device tree node with
of_phy_get_and_connect(). The error path of bgmac_enet_probe() and
bgmac_enet_remove() only call phy_disconnect(), and
bgmac_phy_connect_direct() does not release the PHY when connecting to
it fails. The phy_device is leaked, along with one of the limited number of
fixed PHY ids, which can make a later probe fail after repeated rebinds.

Unregister the fixed PHY after disconnecting from it, in the same way
as other drivers do, and when connecting to the PHY fails.

Fixes: c25b23b8a387 ("bgmac: register fixed PHY for ARM BCM470X / BCM5301X chipsets")
Signed-off-by: Florian Fainelli <florian.fainelli@xxxxxxxxxxxx>
---
drivers/net/ethernet/broadcom/bgmac.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/bgmac.c b/drivers/net/ethernet/broadcom/bgmac.c
index 9ef7307edd41..54092bfacd18 100644
--- a/drivers/net/ethernet/broadcom/bgmac.c
+++ b/drivers/net/ethernet/broadcom/bgmac.c
@@ -15,6 +15,7 @@
#include <linux/bcm47xx_nvram.h>
#include <linux/phy.h>
#include <linux/phy_fixed.h>
+#include <linux/of_mdio.h>
#include <net/dsa.h>
#include "bgmac.h"

@@ -1475,6 +1476,7 @@ int bgmac_phy_connect_direct(struct bgmac *bgmac)
PHY_INTERFACE_MODE_MII);
if (err) {
dev_err(bgmac->dev, "Connecting PHY failed\n");
+ fixed_phy_unregister(phy_dev);
return err;
}

@@ -1482,6 +1484,22 @@ int bgmac_phy_connect_direct(struct bgmac *bgmac)
}
EXPORT_SYMBOL_GPL(bgmac_phy_connect_direct);

+/* Disconnect the PHY and release a fixed PHY registered for the connection. */
+static void bgmac_phy_disconnect(struct bgmac *bgmac)
+{
+ struct device_node *np = bgmac->dev->of_node;
+ struct phy_device *phydev = bgmac->net_dev->phydev;
+
+ if (!phydev)
+ return;
+
+ phy_disconnect(phydev);
+ if (np && of_phy_is_fixed_link(np))
+ of_phy_deregister_fixed_link(np);
+ else if (phy_is_pseudo_fixed_link(phydev))
+ fixed_phy_unregister(phydev);
+}
+
struct bgmac *bgmac_alloc(struct device *dev)
{
struct net_device *net_dev;
@@ -1575,7 +1593,7 @@ int bgmac_enet_probe(struct bgmac *bgmac)
return 0;

err_phy_disconnect:
- phy_disconnect(net_dev->phydev);
+ bgmac_phy_disconnect(bgmac);
err_dma_free:
bgmac_dma_free(bgmac);
err_out:
@@ -1587,7 +1605,7 @@ EXPORT_SYMBOL_GPL(bgmac_enet_probe);
void bgmac_enet_remove(struct bgmac *bgmac)
{
unregister_netdev(bgmac->net_dev);
- phy_disconnect(bgmac->net_dev->phydev);
+ bgmac_phy_disconnect(bgmac);
netif_napi_del(&bgmac->napi);
bgmac_dma_free(bgmac);
}
--
2.53.0