[PATCH net-next v2] net: macb: In shared MDIO usecase make MDIO producer ethernet node to probe first

From: Radhey Shyam Pandey
Date: Thu Jun 30 2022 - 15:55:35 EST


In shared MDIO suspend/resume usecase for ex. with MDIO producer
(0xff0c0000) eth1 and MDIO consumer(0xff0b0000) eth0 there is a
constraint that ethernet interface(ff0c0000) MDIO bus producer
has to be resumed before the consumer ethernet interface(ff0b0000).

However above constraint is not met when GEM0(ff0b0000) is resumed first.
There is phy_error on GEM0 and interface becomes non-functional on resume.

suspend:
[ 46.477795] macb ff0c0000.ethernet eth1: Link is Down
[ 46.483058] macb ff0c0000.ethernet: gem-ptp-timer ptp clock unregistered.
[ 46.490097] macb ff0b0000.ethernet eth0: Link is Down
[ 46.495298] macb ff0b0000.ethernet: gem-ptp-timer ptp clock unregistered.

resume:
[ 46.633840] macb ff0b0000.ethernet eth0: configuring for phy/sgmii link mode
macb_mdio_read -> pm_runtime_get_sync(GEM1) it return -EACCES error.

The suspend/resume is dependent on probe order so to fix this dependency
ensure that MDIO producer ethernet node is always probed first followed
by MDIO consumer ethernet node.

During MDIO registration find out if MDIO bus is shared and check if MDIO
producer platform node(traverse by 'phy-handle' property) is bound. If not
bound then defer the MDIO consumer ethernet node probe. Doing it ensures
that in suspend/resume MDIO producer is resumed followed by MDIO consumer
ethernet node.

Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey@xxxxxxx>
---
Changes for v2:
- check presence of drvdata instead of using device_is_bound()
to fix module compilation. Idea derived from ongoing
onboard_usb_hub driver series[1].
[1]: https://lore.kernel.org/all/20220622144857.v23.2.I7c9a1f1d6ced41dd8310e8a03da666a32364e790@changeid
Listed in v21 changes.
- CC Saravana, Greg, Rafael and ethernet phy maintainers.

Background notes:
As an alternative to this defer probe approach i also explored using
devicelink framework in ndo_open and create a link between consumer and
producer and that solves suspend/resume issue but incase MDIO producer
probe fails MDIO consumer ethernet node remain non-functional. So a
simpler solution seem to defer MDIO consumer ethernet probe till all
dependencies are met.

Please suggest if there is better of solving MDIO producer dependency.
Copied below DTS snippet for reference.

ethernet@ff0b0000 {
is-internal-pcspma;
phy-handle = <0x8f>;
phys = <0x17 0x0 0x8 0x0 0x0>;
compatible = "cdns,zynqmp-gem", "cdns,gem";
status = "okay";
<snip>
xlnx,ptp-enet-clock = <0x0>;
phandle = <0x58>;
};

ethernet@ff0c0000 {
phy-handle = <0x91>;
pinctrl-0 = <0x90>;
pinctrl-names = "default";
compatible = "cdns,zynqmp-gem", "cdns,gem";
status = "okay";
<snip>
mdio {
phandle = <0x99>;
#size-cells = <0x0>;
#address-cells = <0x1>;

ethernet-phy@8 {
phandle = <0x91>;
reset-gpios = <0x8b 0x6 0x1>;
reset-deassert-us = <0x118>;
reset-assert-us = <0x64>;
ti,dp83867-rxctrl-strap-quirk;
ti,fifo-depth = <0x1>;
ti,tx-internal-delay = <0xa>;
ti,rx-internal-delay = <0x8>;
reg = <0x8>;
compatible = "ethernet-phy-id2000.a231";
#phy-cells = <0x1>;
};

ethernet-phy@4 {
phandle = <0x8f>;
reset-gpios = <0x8b 0x5 0x1>;
reset-deassert-us = <0x118>;
reset-assert-us = <0x64>;
ti,dp83867-rxctrl-strap-quirk;
ti,fifo-depth = <0x1>;
ti,tx-internal-delay = <0xa>;
ti,rx-internal-delay = <0x8>;
reg = <0x4>;
compatible = "ethernet-phy-id2000.a231";
#phy-cells = <0x1>;
};
};
};
---
drivers/net/ethernet/cadence/macb_main.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index d0ea8dbfa213..88b95d4cacaf 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -853,7 +853,8 @@ static int macb_mii_probe(struct net_device *dev)

static int macb_mdiobus_register(struct macb *bp)
{
- struct device_node *child, *np = bp->pdev->dev.of_node;
+ struct device_node *child, *np = bp->pdev->dev.of_node, *mdio_np, *dev_np;
+ struct platform_device *mdio_pdev;

/* If we have a child named mdio, probe it instead of looking for PHYs
* directly under the MAC node
@@ -884,6 +885,26 @@ static int macb_mdiobus_register(struct macb *bp)
return of_mdiobus_register(bp->mii_bus, np);
}

+ /* For shared MDIO usecases find out MDIO producer platform
+ * device node by traversing through phy-handle DT property.
+ */
+ np = of_parse_phandle(np, "phy-handle", 0);
+ mdio_np = of_get_parent(np);
+ of_node_put(np);
+ dev_np = of_get_parent(mdio_np);
+ of_node_put(mdio_np);
+ mdio_pdev = of_find_device_by_node(dev_np);
+ of_node_put(dev_np);
+
+ /* Check MDIO producer device driver data to see if it's probed */
+ if (mdio_pdev && !dev_get_drvdata(&mdio_pdev->dev)) {
+ platform_device_put(mdio_pdev);
+ netdev_info(bp->dev, "Defer probe as mdio producer %s is not probed\n",
+ dev_name(&mdio_pdev->dev));
+ return -EPROBE_DEFER;
+ }
+
+ platform_device_put(mdio_pdev);
return mdiobus_register(bp->mii_bus);
}

--
2.1.1