Re: [PATCH net-next v2] net: stmmac: don't create a MDIO bus if unnecessary

From: Andrew Halaney
Date: Wed Dec 06 2023 - 18:58:49 EST


On Wed, Dec 06, 2023 at 05:46:09PM -0600, Andrew Halaney wrote:
> The stmmac_dt_phy() function, which parses the devicetree node of the
> MAC and ultimately causes MDIO bus allocation, misinterprets what
> fixed-link means in relation to the MAC's MDIO bus. This results in
> a MDIO bus being created in situations it need not be.
>
> Currently a MDIO bus is created if the description is either:
>
> 1. Not fixed-link
> 2. fixed-link but contains a MDIO bus as well
>
> The "1" case above isn't always accurate. If there's a phy-handle,
> it could be referencing a phy on another MDIO controller's bus[1]. In
> this case currently the MAC will make a MDIO bus and scan it all
> anyways unnecessarily.
>
> There's also a lot of upstream devicetrees[2] that expect a MDIO bus to
> be created and scanned for a phy. This case can also be inferred from
> the platform description by not having a phy-handle && not being
> fixed-link. This hits case "1" in the current driver's logic.
>
> Let's improve the logic to create a MDIO bus if either:
>
> - Devicetree contains a MDIO bus
> - !fixed-link && !phy-handle (legacy handling)
>
> Below upstream devicetree snippets can be found that explain some of
> the cases above more concretely.
>
> Here's[0] a devicetree example where the MAC is both fixed-link and
> driving a switch on MDIO (case "2" above). This needs a MDIO bus to
> be created:
>
> &fec1 {
> phy-mode = "rmii";
>
> fixed-link {
> speed = <100>;
> full-duplex;
> };
>
> mdio1: mdio {
> switch0: switch0@0 {
> compatible = "marvell,mv88e6190";
> pinctrl-0 = <&pinctrl_gpio_switch0>;
> };
> };
> };
>
> Here's[1] an example where there is no MDIO bus or fixed-link for
> the ethernet1 MAC, so no MDIO bus should be created since ethernet0
> is the MDIO master for ethernet1's phy:
>
> &ethernet0 {
> phy-mode = "sgmii";
> phy-handle = <&sgmii_phy0>;
>
> mdio {
> compatible = "snps,dwmac-mdio";
> sgmii_phy0: phy@8 {
> compatible = "ethernet-phy-id0141.0dd4";
> reg = <0x8>;
> device_type = "ethernet-phy";
> };
>
> sgmii_phy1: phy@a {
> compatible = "ethernet-phy-id0141.0dd4";
> reg = <0xa>;
> device_type = "ethernet-phy";
> };
> };
> };
>
> &ethernet1 {
> phy-mode = "sgmii";
> phy-handle = <&sgmii_phy1>;
> };
>
> Finally there's descriptions like this[2] which don't describe the
> MDIO bus but expect it to be created and the whole address space
> scanned for a phy since there's no phy-handle or fixed-link described:
>
> &gmac {
> phy-supply = <&vcc_lan>;
> phy-mode = "rmii";
> snps,reset-gpio = <&gpio3 RK_PB4 GPIO_ACTIVE_HIGH>;
> snps,reset-active-low;
> snps,reset-delays-us = <0 10000 1000000>;
> };
>
> [0] https://elixir.bootlin.com/linux/v6.5-rc5/source/arch/arm/boot/dts/nxp/vf/vf610-zii-ssmb-dtu.dts
> [1] https://elixir.bootlin.com/linux/v6.6-rc5/source/arch/arm64/boot/dts/qcom/sa8775p-ride.dts
> [2] https://elixir.bootlin.com/linux/v6.6-rc5/source/arch/arm64/boot/dts/rockchip/rk3368-r88.dts#L164
>
> Co-developed-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
> Signed-off-by: Andrew Halaney <ahalaney@xxxxxxxxxx>
> ---

Gah, I failed to describe my changes since Bart's v1 when picking this
up with b4 to make v2. Whoops!

Changes since v1:
- Handle the fixed-link + mdio case (Andrew Lunn)
- Reworded commit message
- Handle the "legacy" case still mentioned in the commit
- Bit further refactoring of the function