Skip to contentSkip to conversation

net: sfp: extend SMBus support

PATCH Superseded Jonas Jelonek 3 participants 12 messages May 7 – May 15

Subsystems (4)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v7 cover net: sfp: extend SMBus support

Today, the SFP driver only drives I2C adapters that advertise full I2C_FUNC_I2C, or SMBus-only adapters via single-byte transfers (with hwmon disabled). Several SoCs ship I2C/SMBus-only controllers that support more than just byte access -- e.g. word and I2C block -- and have SFP cages wired to them. Today, those adapters either work poorly or not at all.

This series teaches the SFP driver to use the larger SMBus access modes when the adapter advertises them, and along the way starts honoring i2c_adapter quirks on read/write length so adapters that cap below the SFP block size are handled correctly. Patch 1 is a small prep doing only the quirks handling; patch 2 extends the SMBus path itself.

Capability matrix supported by patch 2:

  - BYTE only:                   single-byte access (unchanged).
  - BYTE + WORD:                 word for >=2-byte chunks, byte tail.
  - I2C_BLOCK present:           block as the universal transport.
  - WORD only (no BYTE/BLOCK):   accepted with WARN_ONCE; works for

even-length transfers, odd-length transfers will error at xfer time.

Adapters with asymmetric R/W capabilities (e.g. only READ_I2C_BLOCK without WRITE_I2C_BLOCK) remain functionally correct but use the worse-supported direction's max for both directions, since i2c_max_block_size is a single field. No mainline I2C driver was seen advertising such asymmetry; per-direction sizes can be added later if needed.

---
v6 -> v7:
- use i2c_block_size instead of i2c_max_block_size (Maxime)
- move WARN_ONCE into 'else if ()' (Maxime)
- reword comments
- included Maxime's Reviewed-by for patch 1
v6: https://lore.kernel.org/netdev/[email protected]/

v5 -> v6:
- Split adapter-quirks handling into a separate prep patch (1/2).
- Use I2C_SMBUS_I2C_BLOCK_DATA in the block-write branch (was I2C_SMBUS_WORD_DATA), so block writes actually transfer this_len bytes (also flagged by Jakub's AI bot review).
- In sfp_smbus_read/write, check i2c_smbus_xfer() return before copying smbus_data into the caller's buffer.
- Use I2C_BLOCK as the universal transport when available (carries any length 1..32); drop the this_len > 2 guard on the block branches.
- Broaden the SMBus gate to also accept BLOCK-only adapters

    (Russell).

- Accept word-only adapters with WARN_ONCE rather than rejecting them (Andrew).
- Add a short comment in sfp_i2c_configure() explaining the access hierarchy (Maxime).
- Use the all-bits-set form via i2c_check_functionality() for the composite I2C_FUNC_SMBUS_* checks (Russell).
v5: https://lore.kernel.org/netdev/[email protected]/

v4 -> v5:
- made a more general approach, also covering word access
v4: https://lore.kernel.org/netdev/[email protected]/

v3 -> v4:
- fix formal issues
v3: https://lore.kernel.org/netdev/[email protected]/

v2 -> v3:
- fix previous attempt of v2 to fix return value
v2: https://lore.kernel.org/netdev/[email protected]/

v1 -> v2:
- return number of written bytes instead of zero
v1: https://lore.kernel.org/netdev/[email protected]/

---
Jonas Jelonek (2):
net: sfp: apply I2C adapter quirks to limit block size
net: sfp: extend SMBus support

 drivers/net/phy/sfp.c | 144 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 116 insertions(+), 28 deletions(-)

base-commit: dacf281771a9aed1a723b196120a0de8637910b9

Signature · 1 line
2.51.0
#1 lore raw

PATCH v7 1/2 net: sfp: apply I2C adapter quirks to limit block size

The SFP driver assumes all I2C adapters support reading and writing the pre-defined block size SFP_EEPROM_BLOCK_SIZE of 16 bytes. This constant was probably chosen based on good guesses and known limitations of a range of I2C adapters and SFP modules.

However, I2C adapters may even support less and usually need to specify this via I2C quirks. Theoretically, such an adapter may provide full functionality but only support a read and write length of e.g. 8 bytes. Currently, the SFP driver doesn't account for that.

Add handling for I2C quirks in SFP I2C configuration taking the fields max_read_len and max_write_len in struct i2c_adapter_quirks into account to further limit the maximum block size if needed.

---
 drivers/net/phy/sfp.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/net/phy/sfp.c +10 -2
index bd970f753beb..e58e29a1e8d2 100644
@@ -807,21 +807,29 @@ static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr,
 
 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
 {
+	size_t max_block_size;
+
 	sfp->i2c = i2c;
 
 	if (i2c_check_functionality(i2c, I2C_FUNC_I2C)) {
 		sfp->read = sfp_i2c_read;
 		sfp->write = sfp_i2c_write;
-		sfp->i2c_max_block_size = SFP_EEPROM_BLOCK_SIZE;
+		max_block_size = SFP_EEPROM_BLOCK_SIZE;
 	} else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA)) {
 		sfp->read = sfp_smbus_byte_read;
 		sfp->write = sfp_smbus_byte_write;
-		sfp->i2c_max_block_size = 1;
+		max_block_size = 1;
 	} else {
 		sfp->i2c = NULL;
 		return -EINVAL;
 	}
 
+	if (i2c->quirks && i2c->quirks->max_read_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_read_len);
+	if (i2c->quirks && i2c->quirks->max_write_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_write_len);
+
+	sfp->i2c_max_block_size = max_block_size;
 	return 0;
 }
 
Signature · 1 line
2.51.0
reviewer
#2 lore raw

Re: [PATCH net-next v7 1/2] net: sfp: apply I2C adapter quirks to limit block size

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
net: sfp: apply I2C adapter quirks to limit block size

This commit limits the SFP I2C block size based on the I2C adapter's max_read_len and max_write_len quirks.

Quoted 3 lines
drivers/net/phy/sfp.c +0 -0

[ ... ]

Quoted 4 lines
drivers/net/phy/sfp.c +0 -0
@@ -807,21 +807,29 @@ static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr,
 
 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
 {

[ ... ]

drivers/net/phy/sfp.c static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr, · line 830

Quoted 4 lines
drivers/net/phy/sfp.c +4 -0
+	if (i2c->quirks && i2c->quirks->max_read_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_read_len);
+	if (i2c->quirks && i2c->quirks->max_write_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_write_len);

Does applying max_write_len to max_block_size inadvertently throttle reads and break hardware monitoring?

The max_block_size dictates sfp->i2c_max_block_size, which subsequently sets sfp->i2c_block_size. This block size is exclusively used by sfp_i2c_read() to chunk read transfers. The write message in a chunked sfp_i2c_read() operation is always exactly 1 byte (the register address), irrespective of the read payload size. Restricting the read chunk size based on max_write_len appears to cause unintended side effects.

If an adapter has a max_write_len of 1, the i2c_block_size becomes 1. This forces 1-byte read chunking and explicitly skips hwmon device registration since sfp_hwmon_probe() requires an i2c_block_size >= 2.

Will this change actually protect writes against the max_write_len quirk?

Looking at sfp_i2c_write(), it ignores sfp->i2c_block_size and does not implement chunking. It unconditionally allocates 1 + len bytes and submits a single i2c_msg. Any write where 1 + len > max_write_len will still be rejected by the I2C core.

Quoted 4 lines
drivers/net/phy/sfp.c +2 -0
+
+	sfp->i2c_max_block_size = max_block_size;
 	return 0;
 }
#3 lore raw

Re: [PATCH net-next v7 1/2] net: sfp: apply I2C adapter quirks to limit block size

Hi,

Quoted 19 lines · Simon Horman

[ ... ]

drivers/net/phy/sfp.c static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr, · line 830

Quoted 4 lines
drivers/net/phy/sfp.c +4 -0
+	if (i2c->quirks && i2c->quirks->max_read_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_read_len);
+	if (i2c->quirks && i2c->quirks->max_write_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_write_len);

Does applying max_write_len to max_block_size inadvertently throttle reads and break hardware monitoring?

The max_block_size dictates sfp->i2c_max_block_size, which subsequently sets sfp->i2c_block_size. This block size is exclusively used by sfp_i2c_read() to chunk read transfers. The write message in a chunked sfp_i2c_read() operation is always exactly 1 byte (the register address), irrespective of the read payload size. Restricting the read chunk size based on max_write_len appears to cause unintended side effects.

If an adapter has a max_write_len of 1, the i2c_block_size becomes 1. This forces 1-byte read chunking and explicitly skips hwmon device registration since sfp_hwmon_probe() requires an i2c_block_size >= 2.

Quoted 7 lines

Will this change actually protect writes against the max_write_len quirk?

Looking at sfp_i2c_write(), it ignores sfp->i2c_block_size and does not implement chunking. It unconditionally allocates 1 + len bytes and submits a single i2c_msg. Any write where 1 + len > max_write_len will still be rejected by the I2C core.

The concern is structurally valid - the single i2c_block_size does conflate the two direction caps, and on an adapter with max_write_len < max_read_len the read path would be throttled while sfp_i2c_write (which doesn't chunk) wouldn't actually be protected by the write clamp.

However, a sweep of drivers/i2c/busses/ shows no in-tree adapter currently sets max_write_len < max_read_len; the only asymmetric quirks go the other way (read-only caps, or read ≤ write). In every existing case min(max_read_len, max_write_len) collapses to the read cap, so the clamp behaves identically to the situation without this change.

Given that this only has theoretical implications and there won't be a real-world impact right now, I'd keep this as-is. But if handling this properly is desired, I can add another patch splitting the size caps per direction and adding proper handling to sfp_i2c_write.

Quoted 4 lines

drivers/net/phy/sfp.c static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr, · line 834

Quoted 4 lines
drivers/net/phy/sfp.c +2 -0
+
+	sfp->i2c_max_block_size = max_block_size;
 	return 0;
 }

Regards,
Jonas Jelonek

reviewer
#4 lore raw

Re: [PATCH net-next v7 1/2] net: sfp: apply I2C adapter quirks to limit block size

Quoted 43 lines · Jonas Jelonek

Hi,

Quoted 19 lines · Simon Horman

[ ... ]

drivers/net/phy/sfp.c static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr, · line 830

Quoted 4 lines
drivers/net/phy/sfp.c +4 -0
+	if (i2c->quirks && i2c->quirks->max_read_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_read_len);
+	if (i2c->quirks && i2c->quirks->max_write_len)
+		max_block_size = min(max_block_size, i2c->quirks->max_write_len);

Does applying max_write_len to max_block_size inadvertently throttle reads and break hardware monitoring?

The max_block_size dictates sfp->i2c_max_block_size, which subsequently sets sfp->i2c_block_size. This block size is exclusively used by sfp_i2c_read() to chunk read transfers. The write message in a chunked sfp_i2c_read() operation is always exactly 1 byte (the register address), irrespective of the read payload size. Restricting the read chunk size based on max_write_len appears to cause unintended side effects.

If an adapter has a max_write_len of 1, the i2c_block_size becomes 1. This forces 1-byte read chunking and explicitly skips hwmon device registration since sfp_hwmon_probe() requires an i2c_block_size >= 2.

Quoted 7 lines

Will this change actually protect writes against the max_write_len quirk?

Looking at sfp_i2c_write(), it ignores sfp->i2c_block_size and does not implement chunking. It unconditionally allocates 1 + len bytes and submits a single i2c_msg. Any write where 1 + len > max_write_len will still be rejected by the I2C core.

The concern is structurally valid - the single i2c_block_size does conflate the two direction caps, and on an adapter with max_write_len < max_read_len the read path would be throttled while sfp_i2c_write (which doesn't chunk) wouldn't actually be protected by the write clamp.

However, a sweep of drivers/i2c/busses/ shows no in-tree adapter currently sets max_write_len < max_read_len; the only asymmetric quirks go the other way (read-only caps, or read ≤ write). In every existing case min(max_read_len, max_write_len) collapses to the read cap, so the clamp behaves identically to the situation without this change.

Quoted 4 lines

Given that this only has theoretical implications and there won't be a real-world impact right now, I'd keep this as-is. But if handling this properly is desired, I can add another patch splitting the size caps per direction and adding proper handling to sfp_i2c_write.

Thanks, I'm also unsure if it's worth fixing if it doesn't effect anything in practice.

Quoted 8 lines
Quoted 5 lines

drivers/net/phy/sfp.c static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr, · line 834

Quoted 5 lines
drivers/net/phy/sfp.c +2 -0
+
+	sfp->i2c_max_block_size = max_block_size;
 	return 0;
 }

Regards,
Jonas Jelonek

#5 lore raw

PATCH v7 2/2 net: sfp: extend SMBus support

In reply to Jonas Jelonek

Commit 7662abf4db94 ("net: phy: sfp: Add support for SMBus module access") added SMBus access for SFP modules, but limited it to single-byte transfers. As a side effect, hwmon is disabled (16-bit reads cannot be guaranteed atomic) and a warning is printed.

Many SMBus-only I2C controllers in the wild support more than just byte access, and SFP cages are often wired to such controllers rather than to a full-featured I2C controller -- e.g. the SMBus controllers in the Realtek longan and mango SoCs, which advertise word access and I2C block reads. Today, they cannot drive an SFP at all without falling back to the byte-only path.

Extend sfp_smbus_read()/sfp_smbus_write() so that, in addition to the existing byte access, they also use SMBus word access and SMBus I2C block access whenever the adapter advertises them. Both directions are handled in a single read and a single write helper that pick the largest supported transfer per chunk and fall back as needed.

I2C-block is preferred unconditionally when available: the protocol carries any length 1..32, so it can serve every chunk -- including the 1- and 2-byte tails -- without help from word or byte access. Note that this requires I2C_FUNC_SMBUS_I2C_BLOCK, which reads a caller-specified number of bytes. This deviates from the official SMBus Block Read (length is supplied by the slave) but is widely supported by Linux I2C controllers/drivers.

Capability matrix this implementation supports:

- BYTE only: works (unchanged behaviour); 1-byte
xfers, hwmon disabled.
- BYTE + WORD: word for >=2-byte chunks, byte for
trailing odd byte.
- I2C_BLOCK present (with or
without BYTE/WORD): block as the universal transport for
every chunk.
- WORD only (no BYTE/BLOCK): accepted with WARN_ONCE. Even-length transfers work; odd-length transfers (e.g. the 3-byte cotsworks fixup write) hit the BYTE branch which the adapter does not implement, so the xfer returns an error and the operation is aborted. No mainline I2C driver was found to advertise WORD without BYTE; the warning lets us learn about it if it ever shows up.

Adapters with asymmetric R/W capabilities (e.g. only READ_I2C_BLOCK but not WRITE_I2C_BLOCK) remain functionally correct -- the per-iteration fallback uses the direction-specific bits -- but the shared i2c_max_block_size is sized by the all-bits-set check, so a transfer in the better-supported direction is not upgraded. None of the mainline I2C bus drivers surveyed during review advertise such asymmetry; promoting i2c_max_block_size to per-direction sizes can be revisited if needed.

---
 drivers/net/phy/sfp.c | 134 +++++++++++++++++++++++++++++++++---------
 1 file changed, 107 insertions(+), 27 deletions(-)
drivers/net/phy/sfp.c +107 -27
index e58e29a1e8d2..16d41d7ee632 100644
@@ -14,6 +14,7 @@
 #include <linux/platform_device.h>
 #include <linux/rtnetlink.h>
 #include <linux/slab.h>
+#include <linux/unaligned.h>
 #include <linux/workqueue.h>
 
 #include "sfp.h"
@@ -756,50 +757,110 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
 	return ret == ARRAY_SIZE(msgs) ? len : 0;
 }
 
-static int sfp_smbus_byte_read(struct sfp *sfp, bool a2, u8 dev_addr,
-			       void *buf, size_t len)
+static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			  size_t len)
 {
 	union i2c_smbus_data smbus_data;
 	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
 	u8 *data = buf;
 	int ret;
 
-	while (len) {
-		ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
-				     I2C_SMBUS_READ, dev_addr,
-				     I2C_SMBUS_BYTE_DATA, &smbus_data);
-		if (ret < 0)
-			return ret;
+	functionality = i2c_get_functionality(sfp->i2c);
 
-		*data = smbus_data.byte;
+	while (len) {
+		this_len = min(len, sfp->i2c_block_size);
+
+		if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			memcpy(data, &smbus_data.block[1], this_len);
+			transferred = this_len;
+		} else if (this_len >= 2 &&
+			   (functionality & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_WORD_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			put_unaligned_le16(smbus_data.word, data);
+			transferred = 2;
+		} else {
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_BYTE_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			*data = smbus_data.byte;
+			transferred = 1;
+		}
 
-		len--;
-		data++;
-		dev_addr++;
+		data += transferred;
+		len -= transferred;
+		dev_addr += transferred;
 	}
 
 	return data - (u8 *)buf;
 }
 
-static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr,
-				void *buf, size_t len)
+static int sfp_smbus_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			   size_t len)
 {
 	union i2c_smbus_data smbus_data;
 	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
 	u8 *data = buf;
 	int ret;
 
+	functionality = i2c_get_functionality(sfp->i2c);
+
 	while (len) {
-		smbus_data.byte = *data;
-		ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
-				     I2C_SMBUS_WRITE, dev_addr,
-				     I2C_SMBUS_BYTE_DATA, &smbus_data);
-		if (ret)
-			return ret;
+		this_len = min(len, sfp->i2c_block_size);
+
+		if (functionality & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			memcpy(&smbus_data.block[1], data, this_len);
+
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = this_len;
+		} else if (this_len >= 2 &&
+			   (functionality & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
+			smbus_data.word = get_unaligned_le16(data);
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_WORD_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = 2;
+		} else {
+			smbus_data.byte = *data;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_BYTE_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = 1;
+		}
 
-		len--;
-		data++;
-		dev_addr++;
+		data += transferred;
+		len -= transferred;
+		dev_addr += transferred;
 	}
 
 	return data - (u8 *)buf;
@@ -815,10 +876,29 @@ static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
 		sfp->read = sfp_i2c_read;
 		sfp->write = sfp_i2c_write;
 		max_block_size = SFP_EEPROM_BLOCK_SIZE;
-	} else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA)) {
-		sfp->read = sfp_smbus_byte_read;
-		sfp->write = sfp_smbus_byte_write;
-		max_block_size = 1;
+	} else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) ||
+		   i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) {
+		/* Either protocol alone covers any length: I2C-block carries
+		 * 1..32 bytes per xfer, byte iterates one byte at a time.
+		 */
+		sfp->read = sfp_smbus_read;
+		sfp->write = sfp_smbus_write;
+
+		if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK))
+			max_block_size = SFP_EEPROM_BLOCK_SIZE;
+		else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA))
+			max_block_size = 2;
+		else
+			max_block_size = 1;
+	} else if (WARN_ONCE(i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA),
+			     "SMBus word-only adapter; odd-length transfers will fail\n")) {
+		/* Word-only: even-length xfers work; odd-length xfers fall
+		 * to BYTE, which the adapter does not advertise and will
+		 * likely fail.
+		 */
+		sfp->read = sfp_smbus_read;
+		sfp->write = sfp_smbus_write;
+		max_block_size = 2;
 	} else {
 		sfp->i2c = NULL;
 		return -EINVAL;
Signature · 1 line
2.51.0
Reviewed-by on v7 2/2
#6 lore raw
Quoted 57 lines · Jonas Jelonek

Commit 7662abf4db94 ("net: phy: sfp: Add support for SMBus module access") added SMBus access for SFP modules, but limited it to single-byte transfers. As a side effect, hwmon is disabled (16-bit reads cannot be guaranteed atomic) and a warning is printed.

Many SMBus-only I2C controllers in the wild support more than just byte access, and SFP cages are often wired to such controllers rather than to a full-featured I2C controller -- e.g. the SMBus controllers in the Realtek longan and mango SoCs, which advertise word access and I2C block reads. Today, they cannot drive an SFP at all without falling back to the byte-only path.

Extend sfp_smbus_read()/sfp_smbus_write() so that, in addition to the existing byte access, they also use SMBus word access and SMBus I2C block access whenever the adapter advertises them. Both directions are handled in a single read and a single write helper that pick the largest supported transfer per chunk and fall back as needed.

I2C-block is preferred unconditionally when available: the protocol carries any length 1..32, so it can serve every chunk -- including the 1- and 2-byte tails -- without help from word or byte access. Note that this requires I2C_FUNC_SMBUS_I2C_BLOCK, which reads a caller-specified number of bytes. This deviates from the official SMBus Block Read (length is supplied by the slave) but is widely supported by Linux I2C controllers/drivers.

Capability matrix this implementation supports:

- BYTE only: works (unchanged behaviour); 1-byte
xfers, hwmon disabled.
- BYTE + WORD: word for >=2-byte chunks, byte for
trailing odd byte.
- I2C_BLOCK present (with or
without BYTE/WORD): block as the universal transport for
every chunk.
- WORD only (no BYTE/BLOCK): accepted with WARN_ONCE. Even-length transfers work; odd-length transfers (e.g. the 3-byte cotsworks fixup write) hit the BYTE branch which the adapter does not implement, so the xfer returns an error and the operation is aborted. No mainline I2C driver was found to advertise WORD without BYTE; the warning lets us learn about it if it ever shows up.

Adapters with asymmetric R/W capabilities (e.g. only READ_I2C_BLOCK but not WRITE_I2C_BLOCK) remain functionally correct -- the per-iteration fallback uses the direction-specific bits -- but the shared i2c_max_block_size is sized by the all-bits-set check, so a transfer in the better-supported direction is not upgraded. None of the mainline I2C bus drivers surveyed during review advertise such asymmetry; promoting i2c_max_block_size to per-direction sizes can be revisited if needed.

Quoted 1 line

Thank you !

Maxime

Quoted 184 lines
---
 drivers/net/phy/sfp.c | 134 +++++++++++++++++++++++++++++++++---------
 1 file changed, 107 insertions(+), 27 deletions(-)
drivers/net/phy/sfp.c +107 -27
index e58e29a1e8d2..16d41d7ee632 100644
@@ -14,6 +14,7 @@
 #include <linux/platform_device.h>
 #include <linux/rtnetlink.h>
 #include <linux/slab.h>
+#include <linux/unaligned.h>
 #include <linux/workqueue.h>
 
 #include "sfp.h"
@@ -756,50 +757,110 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
 	return ret == ARRAY_SIZE(msgs) ? len : 0;
 }
 
-static int sfp_smbus_byte_read(struct sfp *sfp, bool a2, u8 dev_addr,
-			       void *buf, size_t len)
+static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			  size_t len)
 {
 	union i2c_smbus_data smbus_data;
 	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
 	u8 *data = buf;
 	int ret;
 
-	while (len) {
-		ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
-				     I2C_SMBUS_READ, dev_addr,
-				     I2C_SMBUS_BYTE_DATA, &smbus_data);
-		if (ret < 0)
-			return ret;
+	functionality = i2c_get_functionality(sfp->i2c);
 
-		*data = smbus_data.byte;
+	while (len) {
+		this_len = min(len, sfp->i2c_block_size);
+
+		if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			memcpy(data, &smbus_data.block[1], this_len);
+			transferred = this_len;
+		} else if (this_len >= 2 &&
+			   (functionality & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_WORD_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			put_unaligned_le16(smbus_data.word, data);
+			transferred = 2;
+		} else {
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_BYTE_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			*data = smbus_data.byte;
+			transferred = 1;
+		}
 
-		len--;
-		data++;
-		dev_addr++;
+		data += transferred;
+		len -= transferred;
+		dev_addr += transferred;
 	}
 
 	return data - (u8 *)buf;
 }
 
-static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr,
-				void *buf, size_t len)
+static int sfp_smbus_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			   size_t len)
 {
 	union i2c_smbus_data smbus_data;
 	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
 	u8 *data = buf;
 	int ret;
 
+	functionality = i2c_get_functionality(sfp->i2c);
+
 	while (len) {
-		smbus_data.byte = *data;
-		ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
-				     I2C_SMBUS_WRITE, dev_addr,
-				     I2C_SMBUS_BYTE_DATA, &smbus_data);
-		if (ret)
-			return ret;
+		this_len = min(len, sfp->i2c_block_size);
+
+		if (functionality & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			memcpy(&smbus_data.block[1], data, this_len);
+
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = this_len;
+		} else if (this_len >= 2 &&
+			   (functionality & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
+			smbus_data.word = get_unaligned_le16(data);
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_WORD_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = 2;
+		} else {
+			smbus_data.byte = *data;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_BYTE_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = 1;
+		}
 
-		len--;
-		data++;
-		dev_addr++;
+		data += transferred;
+		len -= transferred;
+		dev_addr += transferred;
 	}
 
 	return data - (u8 *)buf;
@@ -815,10 +876,29 @@ static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
 		sfp->read = sfp_i2c_read;
 		sfp->write = sfp_i2c_write;
 		max_block_size = SFP_EEPROM_BLOCK_SIZE;
-	} else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA)) {
-		sfp->read = sfp_smbus_byte_read;
-		sfp->write = sfp_smbus_byte_write;
-		max_block_size = 1;
+	} else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) ||
+		   i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) {
+		/* Either protocol alone covers any length: I2C-block carries
+		 * 1..32 bytes per xfer, byte iterates one byte at a time.
+		 */
+		sfp->read = sfp_smbus_read;
+		sfp->write = sfp_smbus_write;
+
+		if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK))
+			max_block_size = SFP_EEPROM_BLOCK_SIZE;
+		else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA))
+			max_block_size = 2;
+		else
+			max_block_size = 1;
+	} else if (WARN_ONCE(i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA),
+			     "SMBus word-only adapter; odd-length transfers will fail\n")) {
+		/* Word-only: even-length xfers work; odd-length xfers fall
+		 * to BYTE, which the adapter does not advertise and will
+		 * likely fail.
+		 */
+		sfp->read = sfp_smbus_read;
+		sfp->write = sfp_smbus_write;
+		max_block_size = 2;
 	} else {
 		sfp->i2c = NULL;
 		return -EINVAL;
reviewer
#7 lore raw
In reply to Jonas Jelonek

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
net: sfp: extend SMBus support

This commit extends SFP module SMBus support by adding word and I2C block access capabilities. It implements single read and write helpers that pick the largest supported transfer per chunk and fall back to smaller chunks as needed.

Quoted 4 lines
drivers/net/phy/sfp.c +0 -0
index e58e29a1e8d27..16d41d7ee6328 100644

[ ... ]

Quoted 1 line
drivers/net/phy/sfp.c +0 -0
@@ -756,50 +757,110 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,

[ ... ]

Quoted 14 lines
drivers/net/phy/sfp.c +14 -0
+static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			  size_t len)
+{
+	union i2c_smbus_data smbus_data;
+	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
+	u8 *data = buf;
+	int ret;
+
+	functionality = i2c_get_functionality(sfp->i2c);
+
+	while (len) {
+		this_len = min(len, sfp->i2c_block_size);

drivers/net/phy/sfp.c static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, · line 783

Quoted 10 lines
drivers/net/phy/sfp.c +10 -0
+
+		if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			memcpy(data, &smbus_data.block[1], this_len);

Can this sequence leak uninitialized kernel stack memory to the output buffer?

When performing an I2C_SMBUS_I2C_BLOCK_DATA transfer, i2c_smbus_xfer() updates smbus_data.block[0] with the actual number of bytes read from the bus upon success.

If the adapter or device returns fewer bytes than requested (a short read), smbus_data.block[0] will be less than this_len. Because memcpy() unconditionally copies this_len bytes, it appears it would overrun the valid data and copy uninitialized memory from the smbus_data stack variable.

Since the SFP EEPROM data is accessible to userspace via ethtool, could this expose uninitialized kernel stack memory to userspace?

Should the code verify that smbus_data.block[0] == this_len, or use smbus_data.block[0] as the actual transferred length?

#8 lore raw

Hi,

Quoted 40 lines · Simon Horman

From: 'Simon Horman' <[email protected]>

[ ... ]

Quoted 1 line
(quoted diff) +0 -0
@@ -756,50 +757,110 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,

[ ... ]

Quoted 14 lines
drivers/net/phy/sfp.c +14 -0
+static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			  size_t len)
+{
+	union i2c_smbus_data smbus_data;
+	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
+	u8 *data = buf;
+	int ret;
+
+	functionality = i2c_get_functionality(sfp->i2c);
+
+	while (len) {
+		this_len = min(len, sfp->i2c_block_size);

drivers/net/phy/sfp.c static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, · line 783

Quoted 10 lines
drivers/net/phy/sfp.c +10 -0
+
+		if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			memcpy(data, &smbus_data.block[1], this_len);

Can this sequence leak uninitialized kernel stack memory to the output buffer?

When performing an I2C_SMBUS_I2C_BLOCK_DATA transfer, i2c_smbus_xfer() updates smbus_data.block[0] with the actual number of bytes read from the bus upon success.

If the adapter or device returns fewer bytes than requested (a short read), smbus_data.block[0] will be less than this_len. Because memcpy() unconditionally copies this_len bytes, it appears it would overrun the valid data and copy uninitialized memory from the smbus_data stack variable.

Quoted 5 lines

Since the SFP EEPROM data is accessible to userspace via ethtool, could this expose uninitialized kernel stack memory to userspace?

Should the code verify that smbus_data.block[0] == this_len, or use smbus_data.block[0] as the actual transferred length?

Valid point, indeed uninitialized memory might be leaked here. I wouldn't use the proposed fix though. Maybe I missed it but It doesn't seem to be the contract that I2C/SMBus drivers need to update that field after read. So using that for a check seems wrong. As a fix I will just make sure no uninitialized memory is leaked.

If there are other suggestions please raise your hand, otherwise I'll sned the follow-up soon.

Regards,
Jonas Jelonek

reviewer
#9 lore raw
Quoted 46 lines · Jonas Jelonek

Hi,

Quoted 43 lines · Simon Horman

From: 'Simon Horman' <[email protected]>

[ ... ]

Quoted 1 line
(quoted diff) +0 -0
@@ -756,50 +757,110 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,

[ ... ]

Quoted 14 lines
drivers/net/phy/sfp.c +14 -0
+static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			  size_t len)
+{
+	union i2c_smbus_data smbus_data;
+	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
+	u8 *data = buf;
+	int ret;
+
+	functionality = i2c_get_functionality(sfp->i2c);
+
+	while (len) {
+		this_len = min(len, sfp->i2c_block_size);

drivers/net/phy/sfp.c static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, · line 783

Quoted 10 lines
drivers/net/phy/sfp.c +10 -0
+
+		if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			memcpy(data, &smbus_data.block[1], this_len);

Can this sequence leak uninitialized kernel stack memory to the output buffer?

When performing an I2C_SMBUS_I2C_BLOCK_DATA transfer, i2c_smbus_xfer() updates smbus_data.block[0] with the actual number of bytes read from the bus upon success.

If the adapter or device returns fewer bytes than requested (a short read), smbus_data.block[0] will be less than this_len. Because memcpy() unconditionally copies this_len bytes, it appears it would overrun the valid data and copy uninitialized memory from the smbus_data stack variable.

Since the SFP EEPROM data is accessible to userspace via ethtool, could this expose uninitialized kernel stack memory to userspace?

Quoted 8 lines
Quoted 2 lines

Should the code verify that smbus_data.block[0] == this_len, or use smbus_data.block[0] as the actual transferred length?

Valid point, indeed uninitialized memory might be leaked here. I wouldn't use the proposed fix though. Maybe I missed it but It doesn't seem to be the contract that I2C/SMBus drivers need to update that field after read. So using that for a check seems wrong. As a fix I will just make sure no uninitialized memory is leaked.

Yes, that sounds good to me.

Quoted 7 lines

If there are other suggestions please raise your hand, otherwise I'll sned the follow-up soon.

Regards,
Jonas Jelonek

#10 lore raw
In reply to Jonas Jelonek

Hi Jonas,

Quoted 57 lines · Jonas Jelonek

Commit 7662abf4db94 ("net: phy: sfp: Add support for SMBus module access") added SMBus access for SFP modules, but limited it to single-byte transfers. As a side effect, hwmon is disabled (16-bit reads cannot be guaranteed atomic) and a warning is printed.

Many SMBus-only I2C controllers in the wild support more than just byte access, and SFP cages are often wired to such controllers rather than to a full-featured I2C controller -- e.g. the SMBus controllers in the Realtek longan and mango SoCs, which advertise word access and I2C block reads. Today, they cannot drive an SFP at all without falling back to the byte-only path.

Extend sfp_smbus_read()/sfp_smbus_write() so that, in addition to the existing byte access, they also use SMBus word access and SMBus I2C block access whenever the adapter advertises them. Both directions are handled in a single read and a single write helper that pick the largest supported transfer per chunk and fall back as needed.

I2C-block is preferred unconditionally when available: the protocol carries any length 1..32, so it can serve every chunk -- including the 1- and 2-byte tails -- without help from word or byte access. Note that this requires I2C_FUNC_SMBUS_I2C_BLOCK, which reads a caller-specified number of bytes. This deviates from the official SMBus Block Read (length is supplied by the slave) but is widely supported by Linux I2C controllers/drivers.

Capability matrix this implementation supports:

- BYTE only: works (unchanged behaviour); 1-byte
xfers, hwmon disabled.
- BYTE + WORD: word for >=2-byte chunks, byte for
trailing odd byte.
- I2C_BLOCK present (with or
without BYTE/WORD): block as the universal transport for
every chunk.
- WORD only (no BYTE/BLOCK): accepted with WARN_ONCE. Even-length transfers work; odd-length transfers (e.g. the 3-byte cotsworks fixup write) hit the BYTE branch which the adapter does not implement, so the xfer returns an error and the operation is aborted. No mainline I2C driver was found to advertise WORD without BYTE; the warning lets us learn about it if it ever shows up.

Adapters with asymmetric R/W capabilities (e.g. only READ_I2C_BLOCK but not WRITE_I2C_BLOCK) remain functionally correct -- the per-iteration fallback uses the direction-specific bits -- but the shared i2c_max_block_size is sized by the all-bits-set check, so a transfer in the better-supported direction is not upgraded. None of the mainline I2C bus drivers surveyed during review advertise such asymmetry; promoting i2c_max_block_size to per-direction sizes can be revisited if needed.

Quoted 1 line

I gave this a test and it seems to work fine with all modules I have :)

I tested on both boards that have single-byte smbus, and ones with a real i2c controller. I had to hack around in the i2c controller to pretend it only supported smbus, that did the trick.

I'm unsure if you're going to followup with the sashiko review on this particular patch, it does seem to raise a valid point. I'll followup with tested-by/reviewed-by on the next iteration :)

Thanks for this work :)

Maxime

Quoted 184 lines
---
  drivers/net/phy/sfp.c | 134 +++++++++++++++++++++++++++++++++---------
  1 file changed, 107 insertions(+), 27 deletions(-)
drivers/net/phy/sfp.c +107 -27
index e58e29a1e8d2..16d41d7ee632 100644
@@ -14,6 +14,7 @@
  #include <linux/platform_device.h>
  #include <linux/rtnetlink.h>
  #include <linux/slab.h>
+#include <linux/unaligned.h>
  #include <linux/workqueue.h>
 
  #include "sfp.h"
@@ -756,50 +757,110 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
  	return ret == ARRAY_SIZE(msgs) ? len : 0;
  }
 
-static int sfp_smbus_byte_read(struct sfp *sfp, bool a2, u8 dev_addr,
-			       void *buf, size_t len)
+static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			  size_t len)
  {
  	union i2c_smbus_data smbus_data;
  	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
  	u8 *data = buf;
  	int ret;
 
-	while (len) {
-		ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
-				     I2C_SMBUS_READ, dev_addr,
-				     I2C_SMBUS_BYTE_DATA, &smbus_data);
-		if (ret < 0)
-			return ret;
+	functionality = i2c_get_functionality(sfp->i2c);
 
-		*data = smbus_data.byte;
+	while (len) {
+		this_len = min(len, sfp->i2c_block_size);
+
+		if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			memcpy(data, &smbus_data.block[1], this_len);
+			transferred = this_len;
+		} else if (this_len >= 2 &&
+			   (functionality & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_WORD_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			put_unaligned_le16(smbus_data.word, data);
+			transferred = 2;
+		} else {
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_READ, dev_addr,
+					     I2C_SMBUS_BYTE_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			*data = smbus_data.byte;
+			transferred = 1;
+		}
 
-		len--;
-		data++;
-		dev_addr++;
+		data += transferred;
+		len -= transferred;
+		dev_addr += transferred;
  	}
 
  	return data - (u8 *)buf;
  }
 
-static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr,
-				void *buf, size_t len)
+static int sfp_smbus_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
+			   size_t len)
  {
  	union i2c_smbus_data smbus_data;
  	u8 bus_addr = a2 ? 0x51 : 0x50;
+	size_t this_len, transferred;
+	u32 functionality;
  	u8 *data = buf;
  	int ret;
 
+	functionality = i2c_get_functionality(sfp->i2c);
+
  	while (len) {
-		smbus_data.byte = *data;
-		ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
-				     I2C_SMBUS_WRITE, dev_addr,
-				     I2C_SMBUS_BYTE_DATA, &smbus_data);
-		if (ret)
-			return ret;
+		this_len = min(len, sfp->i2c_block_size);
+
+		if (functionality & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) {
+			smbus_data.block[0] = this_len;
+			memcpy(&smbus_data.block[1], data, this_len);
+
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = this_len;
+		} else if (this_len >= 2 &&
+			   (functionality & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
+			smbus_data.word = get_unaligned_le16(data);
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_WORD_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = 2;
+		} else {
+			smbus_data.byte = *data;
+			ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0,
+					     I2C_SMBUS_WRITE, dev_addr,
+					     I2C_SMBUS_BYTE_DATA, &smbus_data);
+			if (ret < 0)
+				return ret;
+
+			transferred = 1;
+		}
 
-		len--;
-		data++;
-		dev_addr++;
+		data += transferred;
+		len -= transferred;
+		dev_addr += transferred;
  	}
 
  	return data - (u8 *)buf;
@@ -815,10 +876,29 @@ static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
  		sfp->read = sfp_i2c_read;
  		sfp->write = sfp_i2c_write;
  		max_block_size = SFP_EEPROM_BLOCK_SIZE;
-	} else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA)) {
-		sfp->read = sfp_smbus_byte_read;
-		sfp->write = sfp_smbus_byte_write;
-		max_block_size = 1;
+	} else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) ||
+		   i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) {
+		/* Either protocol alone covers any length: I2C-block carries
+		 * 1..32 bytes per xfer, byte iterates one byte at a time.
+		 */
+		sfp->read = sfp_smbus_read;
+		sfp->write = sfp_smbus_write;
+
+		if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK))
+			max_block_size = SFP_EEPROM_BLOCK_SIZE;
+		else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA))
+			max_block_size = 2;
+		else
+			max_block_size = 1;
+	} else if (WARN_ONCE(i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA),
+			     "SMBus word-only adapter; odd-length transfers will fail\n")) {
+		/* Word-only: even-length xfers work; odd-length xfers fall
+		 * to BYTE, which the adapter does not advertise and will
+		 * likely fail.
+		 */
+		sfp->read = sfp_smbus_read;
+		sfp->write = sfp_smbus_write;
+		max_block_size = 2;
  	} else {
  		sfp->i2c = NULL;
  		return -EINVAL;
#11 lore raw

Hi Maxime,

Quoted 6 lines · Maxime Chevallier

Hi Jonas,

[...]
I gave this a test and it seems to work fine with all modules I have :)

I tested on both boards that have single-byte smbus, and ones with a real i2c controller. I had to hack around in the i2c controller to pretend it only supported smbus, that did the trick.

Happy to hear that, thanks for the testing effort :)

Quoted 3 lines

I'm unsure if you're going to followup with the sashiko review on this particular patch, it does seem to raise a valid point. I'll followup with tested-by/reviewed-by on the next iteration :)

Yes, that's a valid point. I'll provide a follow up where this is fixed.

Quoted 5 lines

Thanks for this work :)

Maxime

[...]

Regards,
Jonas Jelonek