* [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions
@ 2025-10-03 7:03 Maxime Chevallier
2025-10-03 14:13 ` Kory Maincent
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Maxime Chevallier @ 2025-10-03 7:03 UTC (permalink / raw)
To: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet, Paolo Abeni,
Russell King, Heiner Kallweit
Cc: Maxime Chevallier, netdev, linux-kernel, thomas.petazzoni,
Florian Fainelli, Köry Maincent, Simon Horman,
Romain Gantois, Marek Behún
When accessing an MDIO register using single-byte smbus accesses, we have to
perform 2 consecutive operations targeting the same address,
first accessing the MSB then the LSB of the 16 bit register:
read_1_byte(addr); <- returns MSB of register at address 'addr'
read_1_byte(addr); <- returns LSB
Some PHY devices present in SFP such as the Broadcom 5461 don't like
seeing foreign i2c transactions in-between these 2 smbus accesses, and
will return the MSB a second time when trying to read the LSB :
read_1_byte(addr); <- returns MSB
i2c_transaction_for_other_device_on_the_bus();
read_1_byte(addr); <- returns MSB again
Given the already fragile nature of accessing PHYs/SFPs with single-byte
smbus accesses, it's safe to say that this Broadcom PHY may not be the
only one acting like this.
Let's therefore hold the i2c bus lock while performing our smbus
transactions to avoid interleaved accesses.
Fixes: d4bd3aca33c2 ("net: mdio: mdio-i2c: Add support for single-byte SMBus operations")
Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
drivers/net/mdio/mdio-i2c.c | 39 ++++++++++++++++++++++++-------------
1 file changed, 25 insertions(+), 14 deletions(-)
diff --git a/drivers/net/mdio/mdio-i2c.c b/drivers/net/mdio/mdio-i2c.c
index 53e96bfab542..ed20352a589a 100644
--- a/drivers/net/mdio/mdio-i2c.c
+++ b/drivers/net/mdio/mdio-i2c.c
@@ -116,17 +116,23 @@ static int smbus_byte_mii_read_default_c22(struct mii_bus *bus, int phy_id,
if (!i2c_mii_valid_phy_id(phy_id))
return 0;
- ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
- I2C_SMBUS_READ, reg,
- I2C_SMBUS_BYTE_DATA, &smbus_data);
+ i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
+
+ ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
+ I2C_SMBUS_READ, reg,
+ I2C_SMBUS_BYTE_DATA, &smbus_data);
if (ret < 0)
- return ret;
+ goto unlock;
val = (smbus_data.byte & 0xff) << 8;
- ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
- I2C_SMBUS_READ, reg,
- I2C_SMBUS_BYTE_DATA, &smbus_data);
+ ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
+ I2C_SMBUS_READ, reg,
+ I2C_SMBUS_BYTE_DATA, &smbus_data);
+
+unlock:
+ i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
+
if (ret < 0)
return ret;
@@ -147,17 +153,22 @@ static int smbus_byte_mii_write_default_c22(struct mii_bus *bus, int phy_id,
smbus_data.byte = (val & 0xff00) >> 8;
- ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
- I2C_SMBUS_WRITE, reg,
- I2C_SMBUS_BYTE_DATA, &smbus_data);
+ i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
+
+ ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
+ I2C_SMBUS_WRITE, reg,
+ I2C_SMBUS_BYTE_DATA, &smbus_data);
if (ret < 0)
- return ret;
+ goto unlock;
smbus_data.byte = val & 0xff;
- ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
- I2C_SMBUS_WRITE, reg,
- I2C_SMBUS_BYTE_DATA, &smbus_data);
+ ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
+ I2C_SMBUS_WRITE, reg,
+ I2C_SMBUS_BYTE_DATA, &smbus_data);
+
+unlock:
+ i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
return ret < 0 ? ret : 0;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions
2025-10-03 7:03 [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions Maxime Chevallier
@ 2025-10-03 14:13 ` Kory Maincent
2025-10-06 13:24 ` Andrew Lunn
2025-10-07 9:00 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Kory Maincent @ 2025-10-03 14:13 UTC (permalink / raw)
To: Maxime Chevallier
Cc: davem, Andrew Lunn, Jakub Kicinski, Eric Dumazet, Paolo Abeni,
Russell King, Heiner Kallweit, netdev, linux-kernel,
thomas.petazzoni, Florian Fainelli, Simon Horman, Romain Gantois,
Marek Behún
On Fri, 3 Oct 2025 09:03:06 +0200
Maxime Chevallier <maxime.chevallier@bootlin.com> wrote:
> When accessing an MDIO register using single-byte smbus accesses, we have to
> perform 2 consecutive operations targeting the same address,
> first accessing the MSB then the LSB of the 16 bit register:
>
> read_1_byte(addr); <- returns MSB of register at address 'addr'
> read_1_byte(addr); <- returns LSB
>
> Some PHY devices present in SFP such as the Broadcom 5461 don't like
> seeing foreign i2c transactions in-between these 2 smbus accesses, and
> will return the MSB a second time when trying to read the LSB :
>
> read_1_byte(addr); <- returns MSB
>
> i2c_transaction_for_other_device_on_the_bus();
>
> read_1_byte(addr); <- returns MSB again
>
> Given the already fragile nature of accessing PHYs/SFPs with single-byte
> smbus accesses, it's safe to say that this Broadcom PHY may not be the
> only one acting like this.
>
> Let's therefore hold the i2c bus lock while performing our smbus
> transactions to avoid interleaved accesses.
>
> Fixes: d4bd3aca33c2 ("net: mdio: mdio-i2c: Add support for single-byte SMBus
> operations") Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Thank you!
--
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions
2025-10-03 7:03 [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions Maxime Chevallier
2025-10-03 14:13 ` Kory Maincent
@ 2025-10-06 13:24 ` Andrew Lunn
2025-10-07 9:00 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2025-10-06 13:24 UTC (permalink / raw)
To: Maxime Chevallier
Cc: davem, Jakub Kicinski, Eric Dumazet, Paolo Abeni, Russell King,
Heiner Kallweit, netdev, linux-kernel, thomas.petazzoni,
Florian Fainelli, Köry Maincent, Simon Horman,
Romain Gantois, Marek Behún
On Fri, Oct 03, 2025 at 09:03:06AM +0200, Maxime Chevallier wrote:
> When accessing an MDIO register using single-byte smbus accesses, we have to
> perform 2 consecutive operations targeting the same address,
> first accessing the MSB then the LSB of the 16 bit register:
>
> read_1_byte(addr); <- returns MSB of register at address 'addr'
> read_1_byte(addr); <- returns LSB
>
> Some PHY devices present in SFP such as the Broadcom 5461 don't like
> seeing foreign i2c transactions in-between these 2 smbus accesses, and
> will return the MSB a second time when trying to read the LSB :
>
> read_1_byte(addr); <- returns MSB
>
> i2c_transaction_for_other_device_on_the_bus();
>
> read_1_byte(addr); <- returns MSB again
>
> Given the already fragile nature of accessing PHYs/SFPs with single-byte
> smbus accesses, it's safe to say that this Broadcom PHY may not be the
> only one acting like this.
>
> Let's therefore hold the i2c bus lock while performing our smbus
> transactions to avoid interleaved accesses.
>
> Fixes: d4bd3aca33c2 ("net: mdio: mdio-i2c: Add support for single-byte SMBus operations")
> Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Andrew
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions
2025-10-03 7:03 [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions Maxime Chevallier
2025-10-03 14:13 ` Kory Maincent
2025-10-06 13:24 ` Andrew Lunn
@ 2025-10-07 9:00 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-10-07 9:00 UTC (permalink / raw)
To: Maxime Chevallier
Cc: davem, andrew, kuba, edumazet, pabeni, linux, hkallweit1, netdev,
linux-kernel, thomas.petazzoni, f.fainelli, kory.maincent, horms,
romain.gantois, kabel
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <pabeni@redhat.com>:
On Fri, 3 Oct 2025 09:03:06 +0200 you wrote:
> When accessing an MDIO register using single-byte smbus accesses, we have to
> perform 2 consecutive operations targeting the same address,
> first accessing the MSB then the LSB of the 16 bit register:
>
> read_1_byte(addr); <- returns MSB of register at address 'addr'
> read_1_byte(addr); <- returns LSB
>
> [...]
Here is the summary with links:
- [net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions
https://git.kernel.org/netdev/net/c/4dc8b26a3ac2
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-10-07 9:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-03 7:03 [PATCH net] net: mdio: mdio-i2c: Hold the i2c bus lock during smbus transactions Maxime Chevallier
2025-10-03 14:13 ` Kory Maincent
2025-10-06 13:24 ` Andrew Lunn
2025-10-07 9:00 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox