[PATCH net v2] net: dsa: mt7530: fix impossible MDIO address and issue warning

Russell King (Oracle) linux at armlinux.org.uk
Tue Apr 30 15:27:31 PDT 2024


On Tue, Apr 30, 2024 at 11:01:17PM +0100, Daniel Golle wrote:
> +	/* Only MDIO bus address 7, 15, 23 and 31 are valid options */
> +	if (~(mdiodev->addr & 0x7) & 0x7) {

So the common thing about the three addresses you mention are that they
all have the least significant three bits set. So I'd suggest to spell
that out in the comment:

	/* Only MDIO bus addresses 7, 15, 23, and 31 are valid options,
	 * which all have the least significant three bits set. Check
	 * for this.
	 */

The test here is also not obvious, so I would suggest:

	if ((mdiodev->addr & 7) != 7) {

which is much easier to read and ties up with the above comment.

> +		/* If the address in DT must be wrong, make a good guess about
> +		 * the most likely intention, and issue a warning.
> +		 */
> +		int correct_addr = ((((mdiodev->addr - 7) & ~0x7) % 0x20) + 7) & 0x1f;

Huh? Again, not obvious what this is doing. So, I threw this into a C
program that wraps the thing in a for() loop from 0..31 to see what it
produces.

addr range	result
0-6		31
7-14		7
15-22		15
23-30		23
31		31

Is it really sane to be suggesting "31" for values 0-6 ?

> +
> +		dev_warn(&mdiodev->dev, FW_WARN
> +			 "impossible switch MDIO address in device tree: %d, assuming %d\n",
> +			 mdiodev->addr, correct_addr);
> +		mdiodev->addr = correct_addr;

Sorry, but no. You must not change the mdiodev address. The address
member is used to index arrays in the MDIO bus, and changing it will
end up corrupting those arrays.

For example, when a MDIO device is registered:

	mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;

when it is unregistered:

        if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
                return -EINVAL;

will fail, and a dangling pointer will be left at the original
address. Moreover, the reset control (if any) will not be put.

If the MDIO device address is wrong, then you can either fail or
maybe create a new mdio device with the correct address - but the
latter may get quite icky from a coding point of view. You would
have to tear down this other device when the original incorrect
one is unbound from the driver.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!



More information about the Linux-mediatek mailing list