netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Russell King (Oracle)" <linux@armlinux.org.uk>
To: Daniel Golle <daniel@makrotopia.org>
Cc: "Arınç ÜNAL" <arinc.unal@arinc9.com>,
	"Felix Fietkau" <nbd@nbd.name>, "Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Simon Horman" <horms@kernel.org>,
	"DENG Qingfang" <dqfext@gmail.com>,
	"Sean Wang" <sean.wang@mediatek.com>,
	"Andrew Lunn" <andrew@lunn.ch>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Vladimir Oltean" <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Matthias Brugger" <matthias.bgg@gmail.com>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"Landen Chao" <Landen.Chao@mediatek.com>,
	devicetree@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org
Subject: Re: [PATCH net v2] net: dsa: mt7530: fix impossible MDIO address and issue warning
Date: Tue, 30 Apr 2024 23:27:31 +0100	[thread overview]
Message-ID: <ZjFwU24kReNJHKdY@shell.armlinux.org.uk> (raw)
In-Reply-To: <11f5f127d0350e72569c36f9060b6e642dfaddbb.1714514208.git.daniel@makrotopia.org>

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!

      parent reply	other threads:[~2024-04-30 22:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-30 22:01 [PATCH net v2] net: dsa: mt7530: fix impossible MDIO address and issue warning Daniel Golle
2024-04-30 22:08 ` Florian Fainelli
2024-04-30 22:27 ` Russell King (Oracle) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZjFwU24kReNJHKdY@shell.armlinux.org.uk \
    --to=linux@armlinux.org.uk \
    --cc=Landen.Chao@mediatek.com \
    --cc=andrew@lunn.ch \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=arinc.unal@arinc9.com \
    --cc=conor+dt@kernel.org \
    --cc=daniel@makrotopia.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=dqfext@gmail.com \
    --cc=edumazet@google.com \
    --cc=f.fainelli@gmail.com \
    --cc=horms@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=nbd@nbd.name \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=robh@kernel.org \
    --cc=sean.wang@mediatek.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).