Skip to contentSkip to conversation

net: hix5hd2_gmac: Support fixed-link

PATCH Superseded David Yang 3 participants 9 messages Nov 26 – Nov 27

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

David Yang

author
#0 lore raw

v4 cover net: hix5hd2_gmac: Support fixed-link

Support MAC-to-MAC link on hix5hd2_gmac. Also add and support mdio child node to suppress warnings when fixed linked, as in macb.

v4:
- reword in dt bindings
- register MDIO bus manually
- skip disabled mdio child
v3: https://lore.kernel.org/r/[email protected]
- use devm_*
- split mdio child node and fixed-link
v2: https://lore.kernel.org/r/[email protected]
- add mdio bus child node
v1: https://lore.kernel.org/r/[email protected]

David Yang (5):
net: hix5hd2_gmac: Use devm_ managed netdev and MDIO bus
dt-bindings: net: hisilicon,hisi-gmac-v1: Add mdio bus child node
net: hix5hd2_gmac: Use mdio child node for MDIO bus if it exists
dt-bindings: net: hisilicon,hisi-gmac-v1: Allow fixed-link
net: hix5hd2_gmac: Support fixed-link

 .../bindings/net/hisilicon,hisi-gmac-v1.yaml  | 25 +++++-
 drivers/net/ethernet/hisilicon/hix5hd2_gmac.c | 87 +++++++++++--------
 2 files changed, 77 insertions(+), 35 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v4 1/5 net: hix5hd2_gmac: Use devm_ managed netdev and MDIO bus

Let devres manage the netdev and the MDIO bus allocation, so that neither needs to be freed by hand in the error path and on remove. The MDIO bus itself is still registered and unregistered by hand, so that it is torn down before the MAC clocks are disabled on the probe error path.

---
 drivers/net/ethernet/hisilicon/hix5hd2_gmac.c | 20 +++++++------------
 1 file changed, 7 insertions(+), 13 deletions(-)
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +7 -13
index 12a5657653f8..44b05d41cb95 100644
@@ -1106,7 +1106,7 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 	struct mii_bus *bus;
 	int ret;
 
-	ndev = alloc_etherdev(sizeof(struct hix5hd2_priv));
+	ndev = devm_alloc_etherdev(dev, sizeof(struct hix5hd2_priv));
 	if (!ndev)
 		return -ENOMEM;
 
@@ -1121,26 +1121,26 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 	priv->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(priv->base)) {
 		ret = PTR_ERR(priv->base);
-		goto out_free_netdev;
+		return ret;
 	}
 
 	priv->ctrl_base = devm_platform_ioremap_resource(pdev, 1);
 	if (IS_ERR(priv->ctrl_base)) {
 		ret = PTR_ERR(priv->ctrl_base);
-		goto out_free_netdev;
+		return ret;
 	}
 
 	priv->mac_core_clk = devm_clk_get(&pdev->dev, "mac_core");
 	if (IS_ERR(priv->mac_core_clk)) {
 		netdev_err(ndev, "failed to get mac core clk\n");
 		ret = -ENODEV;
-		goto out_free_netdev;
+		return ret;
 	}
 
 	ret = clk_prepare_enable(priv->mac_core_clk);
 	if (ret < 0) {
 		netdev_err(ndev, "failed to enable mac core clk %d\n", ret);
-		goto out_free_netdev;
+		return ret;
 	}
 
 	priv->mac_ifc_clk = devm_clk_get(&pdev->dev, "mac_ifc");
@@ -1175,7 +1175,7 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 		hix5hd2_phy_reset(priv);
 	}
 
-	bus = mdiobus_alloc();
+	bus = devm_mdiobus_alloc(dev);
 	if (bus == NULL) {
 		ret = -ENOMEM;
 		goto out_disable_clk;
@@ -1191,7 +1191,7 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 
 	ret = of_mdiobus_register(bus, node);
 	if (ret)
-		goto err_free_mdio;
+		goto out_disable_clk;
 
 	ret = of_get_phy_mode(node, &priv->phy_mode);
 	if (ret) {
@@ -1271,14 +1271,10 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 	of_node_put(priv->phy_node);
 err_mdiobus:
 	mdiobus_unregister(bus);
-err_free_mdio:
-	mdiobus_free(bus);
 out_disable_clk:
 	clk_disable_unprepare(priv->mac_ifc_clk);
 out_disable_mac_core_clk:
 	clk_disable_unprepare(priv->mac_core_clk);
-out_free_netdev:
-	free_netdev(ndev);
 
 	return ret;
 }
@@ -1291,13 +1287,11 @@ static void hix5hd2_dev_remove(struct platform_device *pdev)
 	unregister_netdev(ndev);
 	cancel_work_sync(&priv->tx_timeout_task);
 	mdiobus_unregister(priv->bus);
-	mdiobus_free(priv->bus);
 
 	if (HAS_CAP_TSO(priv->hw_cap))
 		hix5hd2_destroy_sg_desc_queue(priv);
 	hix5hd2_destroy_hw_desc_queue(priv);
 	of_node_put(priv->phy_node);
-	free_netdev(ndev);
 }
 
 static const struct of_device_id hix5hd2_of_match[] = {
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH v4 2/5 dt-bindings: net: hisilicon,hisi-gmac-v1: Add mdio bus child node

In reply to David Yang

This adds an optional mdio bus child node. If present, the mac will look for PHYs there instead of directly under the top-level node. This eliminates any ambiguity about whether child nodes are PHYs, and allows the MDIO bus to contain non-PHY devices.

---
 .../devicetree/bindings/net/hisilicon,hisi-gmac-v1.yaml    | 7 +++++++
 1 file changed, 7 insertions(+)
Documentation/devicetree/bindings/net/hisilicon,hisi-gmac-v1.yaml +7 -0
index 89ed6ab1905e..a71529825955 100644
@@ -53,6 +53,13 @@ properties:
       - const: mac_ifc
       - const: phy
 
+  mdio:
+    $ref: mdio.yaml#
+    unevaluatedProperties: false
+    description:
+      Node containing PHY children. If this node is not present, then PHYs will
+      be direct children.
+
   hisilicon,phy-reset-delays-us:
     description: Triplet of PHY reset delays in microseconds.
     items:
Signature · 1 line
2.53.0
author
#4 lore raw

PATCH v4 3/5 net: hix5hd2_gmac: Use mdio child node for MDIO bus if it exists

In reply to David Yang

This allows explicitly specifying which children are present on the mdio bus, like commit 4d98bb0d7ec2 ("net: macb: Use mdio child node for MDIO bus if it exists").

---
 drivers/net/ethernet/hisilicon/hix5hd2_gmac.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +4 -1
index 44b05d41cb95..b3b2aa1b7698 100644
@@ -1101,6 +1101,7 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct device_node *node = dev->of_node;
+	struct device_node *mdio_np;
 	struct net_device *ndev;
 	struct hix5hd2_priv *priv;
 	struct mii_bus *bus;
@@ -1189,7 +1190,9 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
 	priv->bus = bus;
 
-	ret = of_mdiobus_register(bus, node);
+	mdio_np = of_get_available_child_by_name(node, "mdio");
+	ret = of_mdiobus_register(bus, mdio_np ?: node);
+	of_node_put(mdio_np);
 	if (ret)
 		goto out_disable_clk;
 
Signature · 1 line
2.53.0
author
#5 lore raw

PATCH v4 4/5 dt-bindings: net: hisilicon,hisi-gmac-v1: Allow fixed-link

In reply to David Yang

With fixed-link there is no phy-handle, make phy-handle required only when no fixed-link is declared.

A fixed link gets no MDIO bus by default. In such case, MDIO devices must be placed under the mdio node, and PHY nodes directly under the ethernet node are rejected.

---
 .../bindings/net/hisilicon,hisi-gmac-v1.yaml  | 20 +++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/net/hisilicon,hisi-gmac-v1.yaml +18 -2
index a71529825955..2f1c0e0afb88 100644
@@ -13,6 +13,22 @@ maintainers:
 allOf:
   - $ref: /schemas/net/ethernet-controller.yaml#
   - $ref: /schemas/net/mdio.yaml#
+  - if:
+      not:
+        required:
+          - fixed-link
+    then:
+      required:
+        - phy-handle
+  - if:
+      required:
+        - fixed-link
+    then:
+      not:
+        required:
+          - phy-handle
+      patternProperties:
+        '@[0-9a-f]+$': false
 
 properties:
   compatible:
@@ -58,7 +74,8 @@ properties:
     unevaluatedProperties: false
     description:
       Node containing PHY children. If this node is not present, then PHYs will
-      be direct children.
+      be direct children. With fixed-link, MDIO devices must be placed under the
+      mdio node.
 
   hisilicon,phy-reset-delays-us:
     description: Triplet of PHY reset delays in microseconds.
@@ -74,7 +91,6 @@ required:
   - clocks
   - clock-names
   - phy-mode
-  - phy-handle
 
 unevaluatedProperties: false
 
Signature · 1 line
2.53.0
author
#6 lore raw

PATCH v4 5/5 net: hix5hd2_gmac: Support fixed-link

In reply to David Yang

On Hi3716cv200 hix5hd2_gmac is found directly linked to another MAC without PHY. The driver didn't take fixed-link into account and failed probe when the node has no phy-handle. Register a fixed PHY from the node in that case.

With a fixed link declared on the MAC node, of_mdiobus_register() treats the reg-less fixed-link child as a PHY candidate and walks all 32 bus addresses, printing "scan phy fixed-link at address X" for each address. Skip the MDIO bus registration for a fixed link, unless devices are described under an "mdio" child node, like
commit d0c3601f2c4e ("net: macb: Avoid 20s boot delay by skipping MDIO bus registration for fixed-link PHY").

of_phy_is_fixed_link() also returns true for managed = "in-band-status"; give phy-handle precedence over it, like fec and gianfar.

---
 drivers/net/ethernet/hisilicon/hix5hd2_gmac.c | 70 ++++++++++++-------
 1 file changed, 46 insertions(+), 24 deletions(-)
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +46 -24
index b3b2aa1b7698..eeb7221c0cde 100644
@@ -1176,37 +1176,55 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 		hix5hd2_phy_reset(priv);
 	}
 
-	bus = devm_mdiobus_alloc(dev);
-	if (bus == NULL) {
-		ret = -ENOMEM;
+	priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
+
+	/* In the case of a fixed PHY, the DT node associated
+	 * to the PHY is the Ethernet MAC DT node.
+	 */
+	if (!priv->phy_node && of_phy_is_fixed_link(node)) {
+		ret = of_phy_register_fixed_link(node);
+		if (ret < 0)
+			goto out_disable_clk;
+		priv->phy_node = of_node_get(node);
+	} else if (!priv->phy_node) {
+		netdev_err(ndev, "not find phy-handle\n");
+		ret = -EINVAL;
 		goto out_disable_clk;
 	}
 
-	bus->priv = priv;
-	bus->name = "hix5hd2_mii_bus";
-	bus->read = hix5hd2_mdio_read;
-	bus->write = hix5hd2_mdio_write;
-	bus->parent = &pdev->dev;
-	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
-	priv->bus = bus;
-
+	bus = NULL;
 	mdio_np = of_get_available_child_by_name(node, "mdio");
-	ret = of_mdiobus_register(bus, mdio_np ?: node);
+	if (mdio_np || (priv->phy_node != node &&
+			!of_phy_is_fixed_link(node))) {
+		bus = devm_mdiobus_alloc(dev);
+		if (!bus) {
+			ret = -ENOMEM;
+			of_node_put(mdio_np);
+			goto out_phy_node;
+		}
+
+		bus->priv = priv;
+		bus->name = "hix5hd2_mii_bus";
+		bus->read = hix5hd2_mdio_read;
+		bus->write = hix5hd2_mdio_write;
+		bus->parent = &pdev->dev;
+		snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
+			 dev_name(&pdev->dev));
+		priv->bus = bus;
+
+		ret = of_mdiobus_register(bus, mdio_np ?: node);
+		if (ret) {
+			of_node_put(mdio_np);
+			bus = NULL;
+			goto out_phy_node;
+		}
+	}
 	of_node_put(mdio_np);
-	if (ret)
-		goto out_disable_clk;
 
 	ret = of_get_phy_mode(node, &priv->phy_mode);
 	if (ret) {
 		netdev_err(ndev, "not find phy-mode\n");
-		goto err_mdiobus;
-	}
-
-	priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
-	if (!priv->phy_node) {
-		netdev_err(ndev, "not find phy-handle\n");
-		ret = -EINVAL;
-		goto err_mdiobus;
+		goto out_phy_node;
 	}
 
 	ndev->irq = platform_get_irq(pdev, 0);
@@ -1271,9 +1289,11 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 	netif_napi_del(&priv->napi);
 	hix5hd2_destroy_hw_desc_queue(priv);
 out_phy_node:
+	if (priv->phy_node == node)
+		of_phy_deregister_fixed_link(node);
 	of_node_put(priv->phy_node);
-err_mdiobus:
-	mdiobus_unregister(bus);
+	if (bus)
+		mdiobus_unregister(bus);
 out_disable_clk:
 	clk_disable_unprepare(priv->mac_ifc_clk);
 out_disable_mac_core_clk:
@@ -1294,6 +1314,8 @@ static void hix5hd2_dev_remove(struct platform_device *pdev)
 	if (HAS_CAP_TSO(priv->hw_cap))
 		hix5hd2_destroy_sg_desc_queue(priv);
 	hix5hd2_destroy_hw_desc_queue(priv);
+	if (priv->phy_node == pdev->dev.of_node)
+		of_phy_deregister_fixed_link(pdev->dev.of_node);
 	of_node_put(priv->phy_node);
 }
 
Signature · 1 line
2.53.0
#7 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] net: hix5hd2_gmac: NULL dereference in hix5hd2_dev_remove() on fixed-link - [Medium] net…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] net: hix5hd2_gmac: NULL dereference in hix5hd2_dev_remove() on fixed-link
- [Medium] net: hix5hd2_gmac: broken phy-handle precedence in hix5hd2_dev_probe()
--

commit 557f552e688f91f0f04fafd2daf29129d331e0f6
Author: David Yang <[email protected]>

net: hix5hd2_gmac: Support fixed-link

This commit adds fixed-link support to hix5hd2_gmac by registering a fixed PHY when no phy-handle is present and skipping MDIO bus registration unless an mdio child node is defined.

Quoted 30 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +13 -6
index b3b2aa1b76988..eeb7221c0cde8 100644
@@ -1176,37 +1176,55 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 		hix5hd2_phy_reset(priv);
 	}
 
-	bus = devm_mdiobus_alloc(dev);
-	if (bus == NULL) {
-		ret = -ENOMEM;
+	priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
+
+	/* In the case of a fixed PHY, the DT node associated
+	 * to the PHY is the Ethernet MAC DT node.
+	 */
+	if (!priv->phy_node && of_phy_is_fixed_link(node)) {
+		ret = of_phy_register_fixed_link(node);
+		if (ret < 0)
+			goto out_disable_clk;
+		priv->phy_node = of_node_get(node);
+	} else if (!priv->phy_node) {
+		netdev_err(ndev, "not find phy-handle\n");
+		ret = -EINVAL;
 		goto out_disable_clk;
 	}
 
-	bus->priv = priv;
-	bus->name = "hix5hd2_mii_bus";
-	bus->read = hix5hd2_mdio_read;

drivers/net/ethernet/hisilicon/hix5hd2_gmac.c static int hix5hd2_dev_probe(struct platform_device *pdev) · line 1198

Quoted 10 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +3 -6
-	bus->write = hix5hd2_mdio_write;
-	bus->parent = &pdev->dev;
-	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
-	priv->bus = bus;
-
+	bus = NULL;
 	mdio_np = of_get_available_child_by_name(node, "mdio");
-	ret = of_mdiobus_register(bus, mdio_np ?: node);
+	if (mdio_np || (priv->phy_node != node &&
+			!of_phy_is_fixed_link(node))) {

[Severity: Medium]
Does checking !of_phy_is_fixed_link(node) here break phy-handle precedence when the MAC node has both phy-handle and managed = "in-band-status" without an mdio child node?

In that configuration of_phy_is_fixed_link(node) is true even though priv->phy_node != node, so hix5hd2_dev_probe() skips registering the MDIO bus and of_phy_connect() in hix5hd2_net_open() fails to find the PHY. Since priv->phy_node == node already tracks whether a fixed link was registered, checking if (mdio_np || priv->phy_node != node) would avoid this.

[ ... ]

Quoted 15 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +4 -2
@@ -1271,9 +1289,11 @@ static int hix5hd2_dev_probe(struct platform_device *pdev)
 	netif_napi_del(&priv->napi);
 	hix5hd2_destroy_hw_desc_queue(priv);
 out_phy_node:
+	if (priv->phy_node == node)
+		of_phy_deregister_fixed_link(node);
 	of_node_put(priv->phy_node);
-err_mdiobus:
-	mdiobus_unregister(bus);
+	if (bus)
+		mdiobus_unregister(bus);
 out_disable_clk:
 	clk_disable_unprepare(priv->mac_ifc_clk);
 out_disable_mac_core_clk:

drivers/net/ethernet/hisilicon/hix5hd2_gmac.c static void hix5hd2_dev_remove(struct platform_device *pdev) · line 1320

Quoted 7 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +2 -0
@@ -1294,6 +1314,8 @@ static void hix5hd2_dev_remove(struct platform_device *pdev)
 	if (HAS_CAP_TSO(priv->hw_cap))
 		hix5hd2_destroy_sg_desc_queue(priv);
 	hix5hd2_destroy_hw_desc_queue(priv);
+	if (priv->phy_node == pdev->dev.of_node)
+		of_phy_deregister_fixed_link(pdev->dev.of_node);
 	of_node_put(priv->phy_node);
 }

[Severity: High]
Does hix5hd2_dev_remove() trigger a NULL pointer dereference in mdiobus_unregister(priv->bus) when a fixed-link device has no mdio child node?

When MDIO bus registration is skipped in hix5hd2_dev_probe(), priv->bus remains NULL. The probe error path guards mdiobus_unregister() with if (bus), but hix5hd2_dev_remove() still calls mdiobus_unregister(priv->bus) unconditionally, and mdiobus_unregister() immediately dereferences bus->state.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#3 lore raw

Re: [PATCH net-next v4 2/5] dt-bindings: net: hisilicon,hisi-gmac-v1: Add mdio bus child node

In reply to David Yang
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] In this commit the binding hisilicon,hisi-gmac-v1.yaml adds an optional mdio child node and says that PHYs sit under it when it is…

--

dt-bindings: net: hisilicon,hisi-gmac-v1: Add mdio bus child node

This adds an optional mdio child node to the hisi-gmac-v1 binding. When the node is present, the MAC looks for PHYs under it instead of directly under the top-level node.

Quoted 5 lines
Documentation/devicetree/bindings/net/hisilicon,hisi-gmac-v1.yaml +0 -0
index 89ed6ab1905e1..a715298259552 100644

Documentation/devicetree/bindings/net/hisilicon,hisi-gmac-v1.yaml properties: · line 61

Quoted 9 lines
Documentation/devicetree/bindings/net/hisilicon,hisi-gmac-v1.yaml +6 -0
@@ -53,6 +53,13 @@ properties:
       - const: mac_ifc
       - const: phy
 
+  mdio:
+    $ref: mdio.yaml#
+    unevaluatedProperties: false
+    description:
+      Node containing PHY children. If this node is not present, then PHYs will
+      be direct children.

[Severity: Low]
With only this commit applied, does the driver honour the new mdio child?

hix5hd2_dev_probe() in drivers/net/ethernet/hisilicon/hix5hd2_gmac.c still registers the MDIO bus on the top-level MAC node:

hix5hd2_dev_probe() {
    ...
	ret = of_mdiobus_register(bus, node);
    ...
}

The binding now accepts PHYs placed under mdio { }. A DT using that layout would leave the PHY unregistered at this point in the series. of_phy_connect() in hix5hd2_net_open() would then fail to find a phy_device for the phy-handle.

The next patch in the series, "net: hix5hd2_gmac: Use mdio child node for MDIO bus if it exists", looks up the child with of_get_available_child_by_name(node, "mdio"). It then calls of_mdiobus_register(bus, mdio_np ?: node), which closes the gap.

This is the usual dt-bindings-first order for a series, so this is likely fine as is. It is noted here only for completeness.

Quoted 3 lines
Documentation/devicetree/bindings/net/hisilicon,hisi-gmac-v1.yaml +0 -0
   hisilicon,phy-reset-delays-us:
     description: Triplet of PHY reset delays in microseconds.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126005524.4156940-1-mmyangfl%40gmail.com
#8 lore raw
In reply to David Yang
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] hix5hd2_dev_remove() in drivers/net/ethernet/hisilicon/hix5hd2_gmac.c still calls…
- [Medium] The commit message says phy-handle takes precedence over of_phy_is_fixed_link() (for managed = "in-band-status").

--

net: hix5hd2_gmac: Support fixed-link

This patch adds fixed-link support to hix5hd2_gmac. When the node has no phy-handle, the driver now registers a fixed PHY from the MAC node. It also skips MDIO bus registration for a fixed link unless an "mdio" child node is present.

Quoted 4 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +0 -0
index b3b2aa1b76988..eeb7221c0cde8 100644

[ ... ]

drivers/net/ethernet/hisilicon/hix5hd2_gmac.c static int hix5hd2_dev_probe(struct platform_device *pdev) · line 1198

Quoted 5 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +3 -1
+	bus = NULL;
 	mdio_np = of_get_available_child_by_name(node, "mdio");
-	ret = of_mdiobus_register(bus, mdio_np ?: node);
+	if (mdio_np || (priv->phy_node != node &&
+			!of_phy_is_fixed_link(node))) {

[Severity: Medium]
The commit message says:

of_phy_is_fixed_link() also returns true for managed = "in-band-status"; give phy-handle precedence over it, like fec and gianfar.

Does this condition keep that precedence for bus registration?

priv->phy_node == node only happens when the fixed-link branch ran, and of_phy_is_fixed_link(node) is already true there. So the test reduces to "mdio_np || !of_phy_is_fixed_link(node)".

of_phy_is_fixed_link() in drivers/net/mdio/of_mdio.c returns true for any managed value other than "auto":

	err = of_property_read_string(np, "managed", &managed);
	if (err == 0 && strcmp(managed, "auto") != 0)
		return true;

Consider a node with phy-handle, managed = "in-band-status", and the PHY as a direct child of the MAC node with no "mdio" subnode. The hisilicon,hisi-gmac-v1.yaml binding only forbids direct PHY children when fixed-link is present, so this layout is allowed.

For that node of_mdiobus_register() is never called, so the PHY is never created. hix5hd2_net_open() then fails here:

	phy = of_phy_connect(dev, priv->phy_node,
			     &hix5hd2_adjust_link, 0, priv->phy_mode);
	if (!phy) {
		...
		return -ENODEV;

Before this patch the bus was always registered on mdio_np ?: node, and this layout worked. This configuration also leaves priv->bus NULL, which leads to the remove issue below.

Would "mdio_np || priv->phy_node != node" be the intended test here?

Quoted 15 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +15 -0
+		bus = devm_mdiobus_alloc(dev);
+		if (!bus) {
+			ret = -ENOMEM;
+			of_node_put(mdio_np);
+			goto out_phy_node;
+		}
+
+		bus->priv = priv;
+		bus->name = "hix5hd2_mii_bus";
+		bus->read = hix5hd2_mdio_read;
+		bus->write = hix5hd2_mdio_write;
+		bus->parent = &pdev->dev;
+		snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
+			 dev_name(&pdev->dev));
+		priv->bus = bus;

[ ... ]

drivers/net/ethernet/hisilicon/hix5hd2_gmac.c static void hix5hd2_dev_remove(struct platform_device *pdev) · line 1320

Quoted 8 lines
drivers/net/ethernet/hisilicon/hix5hd2_gmac.c +2 -0
@@ -1294,6 +1314,8 @@ static void hix5hd2_dev_remove(struct platform_device *pdev)
 	if (HAS_CAP_TSO(priv->hw_cap))
 		hix5hd2_destroy_sg_desc_queue(priv);
 	hix5hd2_destroy_hw_desc_queue(priv);
+	if (priv->phy_node == pdev->dev.of_node)
+		of_phy_deregister_fixed_link(pdev->dev.of_node);
 	of_node_put(priv->phy_node);
 }

[Severity: High]
Can hix5hd2_dev_remove() dereference a NULL priv->bus now?

Just above this hunk, remove still does:

	unregister_netdev(ndev);
	cancel_work_sync(&priv->tx_timeout_task);
	mdiobus_unregister(priv->bus);

With this patch, hix5hd2_dev_probe() only allocates the bus and sets priv->bus inside the new conditional block. Take the fixed-link case this patch adds support for, with no phy-handle and no "mdio" child. Probe succeeds and priv->bus stays NULL, because the private area from devm_alloc_etherdev() is zeroed.

On unbind or rmmod, mdiobus_unregister() dereferences bus right away:

	if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
		return;

The probe error path gained an "if (bus)" check. Does remove need the same check on priv->bus?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126005524.4156940-1-mmyangfl%40gmail.com