Skip to contentSkip to conversation

sfp: sfp_read: split-up request when hw rx buffer is too small.

PATCH In review René van Dorst 3 participants 10 messages Jan 23, 2019 – Jan 25, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sfp: sfp_read: split-up request when hw rx buffer is too small.

Without this patch sfp code retries to read the full struct sfp_eeprom_id id out of the SFP eeprom. Sizeof(id) is 96 bytes.
My i2c hardware, Mediatek mt7621, has a rx buffer of 64 bytes. So sfp_read gets -NOSUPPORTED back on his turn return -EAGAIN. Same issue is with the SFP_EXT_STATUS data which is 92 bytes.

By split-up the request in multiple smaller requests with a max size of i2c max_read_len, we can readout the SFP module successfully.

Tested with MT7621 and two Fiberstore modules SFP-GB-GE-T and SFP-GE-BX.

---
 drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/net/phy/sfp.c +22 -1
index fd8bb998ae52..1352a19571cd 100644
@@ -367,7 +367,28 @@ static void sfp_set_state(struct sfp *sfp, unsigned int state)
 
 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
 {
-	return sfp->read(sfp, a2, addr, buf, len);
+	const struct i2c_adapter_quirks *q = sfp->i2c->quirks;
+	int ret;
+	size_t rx_bytes = 0;
+
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;
+	}
+
+	ret = sfp->read(sfp, a2, addr, buf, len);
+	if (ret < 0)
+		return ret;
+
+	rx_bytes += ret;
+
+	return rx_bytes;
 }
 
 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
Signature · 1 line
2.19.1
maintainer
#1 lore raw
Quoted 22 lines · René van Dorst

Without this patch sfp code retries to read the full struct sfp_eeprom_id id out of the SFP eeprom. Sizeof(id) is 96 bytes.
My i2c hardware, Mediatek mt7621, has a rx buffer of 64 bytes. So sfp_read gets -NOSUPPORTED back on his turn return -EAGAIN. Same issue is with the SFP_EXT_STATUS data which is 92 bytes.

By split-up the request in multiple smaller requests with a max size of i2c max_read_len, we can readout the SFP module successfully.

Tested with MT7621 and two Fiberstore modules SFP-GB-GE-T and SFP-GE-BX.

---
 drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/net/phy/sfp.c +0 -0
index fd8bb998ae52..1352a19571cd 100644
@@ -367,7 +367,28 @@ static void sfp_set_state(struct sfp *sfp, unsigned int state)
 

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 376

Quoted 10 lines
drivers/net/phy/sfp.c +7 -1
 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
 {
-	return sfp->read(sfp, a2, addr, buf, len);
+	const struct i2c_adapter_quirks *q = sfp->i2c->quirks;
+	int ret;
+	size_t rx_bytes = 0;
+
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);

Hi René

I think you want to pass MIN(len, q->max_read_len) to read().

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 382

Quoted 6 lines
drivers/net/phy/sfp.c +6 -0
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;

I would prefer you add ret, not q->max_read_len. There is a danger it returned less bytes than you asked for.

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 394

Quoted 12 lines
drivers/net/phy/sfp.c +9 -0
+	}
+
+	ret = sfp->read(sfp, a2, addr, buf, len);
+	if (ret < 0)
+		return ret;
+
+	rx_bytes += ret;
+
+	return rx_bytes;
 }
 
 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)

Doesn't write need the same handling?

Thanks
Andrew

#2 lore raw

Quoting Andrew Lunn <[email protected]>:

Quoted 38 lines · René van Dorst
Quoted 24 lines

Without this patch sfp code retries to read the full struct sfp_eeprom_id id out of the SFP eeprom. Sizeof(id) is 96 bytes.
My i2c hardware, Mediatek mt7621, has a rx buffer of 64 bytes. So sfp_read gets -NOSUPPORTED back on his turn return -EAGAIN. Same issue is with the SFP_EXT_STATUS data which is 92 bytes.

By split-up the request in multiple smaller requests with a max size of i2c max_read_len, we can readout the SFP module successfully.

Tested with MT7621 and two Fiberstore modules SFP-GB-GE-T and SFP-GE-BX.

---
 drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/net/phy/sfp.c +0 -0
index fd8bb998ae52..1352a19571cd 100644
@@ -367,7 +367,28 @@ static void sfp_set_state(struct sfp *sfp,  
 unsigned int state)
 
 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf,  

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, · line 378

Quoted 10 lines
drivers/net/phy/sfp.c +7 -1
 size_t len)
 {
-	return sfp->read(sfp, a2, addr, buf, len);
+	const struct i2c_adapter_quirks *q = sfp->i2c->quirks;
+	int ret;
+	size_t rx_bytes = 0;
+
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);

Hi René

Quoted 1 line

I think you want to pass MIN(len, q->max_read_len) to read().

Hi Andrew,

max_read_len is 0 when there is no quirk.
I can write it a bit differently depending on the outcome of my other email.

Quoted 9 lines

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 382

Quoted 6 lines
drivers/net/phy/sfp.c +6 -0
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;

I would prefer you add ret, not q->max_read_len. There is a danger it returned less bytes than you asked for.

Getting less bytes then asked is already an error I think.
I could check the return size and directly return the number of bytes that I have. The callers are checking for size and they can retry if wanted. So that should not be an issue.

Quoted 11 lines
Quoted 11 lines
drivers/net/phy/sfp.c +9 -0
+	}
+
+	ret = sfp->read(sfp, a2, addr, buf, len);
+	if (ret < 0)
+		return ret;
+
+	rx_bytes += ret;
+
+	return rx_bytes;
 }
Quoted 4 lines
Quoted 2 lines

static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf,
size_t len)

Doesn't write need the same handling?

By reading the SSF spec we can write to a user writable EERPOM area of 120 bytes. But the current code has only has 1 sfp_write for a byte value.

So for now I should say no.

Greats,

René

maintainer
#3 lore raw
Quoted 6 lines
Quoted 6 lines

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 376

Quoted 3 lines
drivers/net/phy/sfp.c +3 -0
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);

Hi René

Quoted 6 lines
Quoted 1 line

I think you want to pass MIN(len, q->max_read_len) to read().

Hi Andrew,

max_read_len is 0 when there is no quirk.
I can write it a bit differently depending on the outcome of my other email.

Hi René

No, you misunderstood me.

Quoted 1 line
Quoted 1 line

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 376

Quoted 1 line
drivers/net/phy/sfp.c +1 -0
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);

Say max_read_len = 64

The SFP code asks to read 68 bytes. The first call to read() is going to ask for 64 bytes. The second call is going to also ask for 64 bytes, writing 60 bytes passed the end of buf. Bad things then happen.

Quoted 11 lines
Quoted 10 lines

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 382

Quoted 7 lines
drivers/net/phy/sfp.c +6 -0
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;

I would prefer you add ret, not q->max_read_len. There is a danger it returned less bytes than you asked for.

Quoted 4 lines

Getting less bytes then asked is already an error I think.
I could check the return size and directly return the number of bytes that I have. The callers are checking for size and they can retry if wanted. So that should not be an issue.

If that is true, why is rx_bytes += ret, where as all the others are += q->max_read_len. Please be consistent. The general pattern of a read function in POSIX systems is that it returns how many bytes were actually returned. So i would always use += ret.

Quoted 5 lines

By reading the SSF spec we can write to a user writable EERPOM area of 120 bytes.
But the current code has only has 1 sfp_write for a byte value.

So for now I should say no.

So how about adding a WARN_ON. If the request is bigger than what the quirk allows, make it very obvious we have an issue.

Andrew

#4 lore raw

Quoting Andrew Lunn <[email protected]>:

Quoted 17 lines
Quoted 6 lines
Quoted 6 lines

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 376

Quoted 3 lines
drivers/net/phy/sfp.c +3 -0
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);

Hi René

Quoted 6 lines
Quoted 1 line

I think you want to pass MIN(len, q->max_read_len) to read().

Hi Andrew,

max_read_len is 0 when there is no quirk.
I can write it a bit differently depending on the outcome of my other email.

Hi René

No, you misunderstood me.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 376

Quoted 1 line
drivers/net/phy/sfp.c +1 -0
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);

Say max_read_len = 64

The SFP code asks to read 68 bytes. The first call to read() is going to ask for 64 bytes. The second call is going to also ask for 64 bytes, writing 60 bytes passed the end of buf. Bad things then happen.

Hi Andrew,

This can't happen, while loop is only executed when len > max_read_len. len is reduced after a successful read in the while loop.

So sizes <= max_read_len is handled by sfp->read below the while loop.

Quoted 25 lines
Quoted 11 lines
Quoted 10 lines

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 382

Quoted 7 lines
drivers/net/phy/sfp.c +6 -0
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;

I would prefer you add ret, not q->max_read_len. There is a danger it returned less bytes than you asked for.

Quoted 4 lines

Getting less bytes then asked is already an error I think.
I could check the return size and directly return the number of bytes that I have. The callers are checking for size and they can retry if wanted. So that should not be an issue.

If that is true, why is rx_bytes += ret, where as all the others are += q->max_read_len. Please be consistent. The general pattern of a read function in POSIX systems is that it returns how many bytes were actually returned. So i would always use += ret.

Quoted 4 lines

By reading the SSF spec we can write to a user writable EERPOM area of 120 bytes.
But the current code has only has 1 sfp_write for a byte value.

Quoted 6 lines
Quoted 1 line

So for now I should say no.

So how about adding a WARN_ON. If the request is bigger than what the quirk allows, make it very obvious we have an issue.

Andrew

i2c-core already reports an error in i2c_check_for_quirks [0].
Is that sufficient?
This is how I know that something was wrong.
But the driver should not retry it for infinitely like what is happening with read. And keeps spamming the error logs.

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/i2c/i2c-core-base.c?h=v5.0-rc3#n1787

Greats,

René

#5 lore raw
In reply to René van Dorst
Quoted 29 lines · René van Dorst

Without this patch sfp code retries to read the full struct sfp_eeprom_id id out of the SFP eeprom. Sizeof(id) is 96 bytes.
My i2c hardware, Mediatek mt7621, has a rx buffer of 64 bytes. So sfp_read gets -NOSUPPORTED back on his turn return -EAGAIN. Same issue is with the SFP_EXT_STATUS data which is 92 bytes.

By split-up the request in multiple smaller requests with a max size of i2c max_read_len, we can readout the SFP module successfully.

Tested with MT7621 and two Fiberstore modules SFP-GB-GE-T and SFP-GE-BX.

---
 drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/net/phy/sfp.c +4 -1
index fd8bb998ae52..1352a19571cd 100644
@@ -367,7 +367,28 @@ static void sfp_set_state(struct sfp *sfp, unsigned int state)
 
 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
 {
-	return sfp->read(sfp, a2, addr, buf, len);
+	const struct i2c_adapter_quirks *q = sfp->i2c->quirks;
+	int ret;
+	size_t rx_bytes = 0;
+

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, unsigned int state) · line 383

Quoted 10 lines
drivers/net/phy/sfp.c +10 -0
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;
+	}

sfp->read defaults to sfp_i2c_read() but it is possible to override that by registering a custom sfp_bus instance (nothing does it today, but that could obviously change), so there is no guarantee that sfp->i2c->quirks is applicable unless sfp_i2c_read() is used.

sfp_i2c_read() is presumably where the max_read_len splitting should occur, or better yet, should not i2c_transfer() take care of that on its own? That way there would be no layering violation of having to fetch the quirk bitmask for the i2c adapter being used, that is something that should belong in the core i2c framework.

Quoted 12 lines
drivers/net/phy/sfp.c +8 -0
+
+	ret = sfp->read(sfp, a2, addr, buf, len);
+	if (ret < 0)
+		return ret;
+
+	rx_bytes += ret;
+
+	return rx_bytes;
 }
 
 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
Signature · 1 line
Florian
#6 lore raw

Quoting Florian Fainelli <[email protected]>:

Quoted 48 lines · René van Dorst
Quoted 31 lines

Without this patch sfp code retries to read the full struct sfp_eeprom_id id out of the SFP eeprom. Sizeof(id) is 96 bytes.
My i2c hardware, Mediatek mt7621, has a rx buffer of 64 bytes. So sfp_read gets -NOSUPPORTED back on his turn return -EAGAIN. Same issue is with the SFP_EXT_STATUS data which is 92 bytes.

By split-up the request in multiple smaller requests with a max size of i2c max_read_len, we can readout the SFP module successfully.

Tested with MT7621 and two Fiberstore modules SFP-GB-GE-T and SFP-GE-BX.

---
 drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/net/phy/sfp.c +4 -1
index fd8bb998ae52..1352a19571cd 100644
@@ -367,7 +367,28 @@ static void sfp_set_state(struct sfp *sfp,  
 unsigned int state)
 
 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf,  
 size_t len)
 {
-	return sfp->read(sfp, a2, addr, buf, len);
+	const struct i2c_adapter_quirks *q = sfp->i2c->quirks;
+	int ret;
+	size_t rx_bytes = 0;
+

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, · line 385

Quoted 10 lines
drivers/net/phy/sfp.c +10 -0
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;
+	}

sfp->read defaults to sfp_i2c_read() but it is possible to override that by registering a custom sfp_bus instance (nothing does it today, but that could obviously change), so there is no guarantee that sfp->i2c->quirks is applicable unless sfp_i2c_read() is used.

Quoted 5 lines

sfp_i2c_read() is presumably where the max_read_len splitting should occur, or better yet, should not i2c_transfer() take care of that on its own? That way there would be no layering violation of having to fetch the quirk bitmask for the i2c adapter being used, that is something that should belong in the core i2c framework.

Yes it is better to put it in sfp_i2c_read().

I think it is best to handle the split-up within the driver. The driver knows how to talk to the device and may apply device quirks.

Also tda1004x [0] and TPM [1] driver also handles it within the driver itself.

TPM driver just try to send the want size and split-up request to I2C_SMBUS_BLOCK_MAX when a -EOPNOTSUPP returns, just retries it a number of times.

I can do the same but I have to pick a minimum size.
Looking in SSF-8472rev12.2.1 they don't limit the way you access the device. So use I2C_SMBUS_BLOCK_MAX of 32 bytes is sufficient or lookup the i2c quirk max_read_len is also an option.

Grepping thru the i2c busses I see only 2 devices which has less then 32 bytes of buffer. i2c-nvidia-gpu (Nvidia GPU) and i2c-pmcmsp (microcontroller MSP71xx). Both are unlikely to be used for these kind of applications.

I think I2C_SMBUS_BLOCK_MAX is safe to use.

Greats,

René

[0] https://elixir.bootlin.com/linux/v5.0-rc3/source/drivers/media/dvb-frontends/tda1004x.c#L319
[1] https://elixir.bootlin.com/linux/v5.0-rc3/source/drivers/char/tpm/tpm_i2c_infineon.c#L158

#7 lore raw

Quoting René van Dorst <[email protected]>:

Quoted 62 lines

Quoting Florian Fainelli <[email protected]>:

Quoted 47 lines · René van Dorst
Quoted 30 lines

Without this patch sfp code retries to read the full struct sfp_eeprom_id id out of the SFP eeprom. Sizeof(id) is 96 bytes.
My i2c hardware, Mediatek mt7621, has a rx buffer of 64 bytes. So sfp_read gets -NOSUPPORTED back on his turn return -EAGAIN. Same issue is with the SFP_EXT_STATUS data which is 92 bytes.

By split-up the request in multiple smaller requests with a max size of i2c max_read_len, we can readout the SFP module successfully.

Tested with MT7621 and two Fiberstore modules SFP-GB-GE-T and SFP-GE-BX.

---
drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-

1 file changed, 22 insertions(+), 1 deletion(-)
drivers/net/phy/sfp.c +4 -1
index fd8bb998ae52..1352a19571cd 100644
@@ -367,7 +367,28 @@ static void sfp_set_state(struct sfp *sfp,  
 unsigned int state)
 
 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
 {
-	return sfp->read(sfp, a2, addr, buf, len);
+	const struct i2c_adapter_quirks *q = sfp->i2c->quirks;
+	int ret;
+	size_t rx_bytes = 0;
+

drivers/net/phy/sfp.c static void sfp_set_state(struct sfp *sfp, · line 384

Quoted 10 lines
drivers/net/phy/sfp.c +10 -0
+	/* Many i2c hw have limited rx buffers, split-up request when needed. */
+	while ((q->max_read_len) && (len > q->max_read_len)) {
+		ret = sfp->read(sfp, a2, addr, buf, q->max_read_len);
+		if (ret < 0)
+			return ret;
+		rx_bytes += ret;
+		addr += q->max_read_len;
+		buf += q->max_read_len;
+		len -= q->max_read_len;
+	}

sfp->read defaults to sfp_i2c_read() but it is possible to override that by registering a custom sfp_bus instance (nothing does it today, but that could obviously change), so there is no guarantee that sfp->i2c->quirks is applicable unless sfp_i2c_read() is used.

Quoted 5 lines

sfp_i2c_read() is presumably where the max_read_len splitting should occur, or better yet, should not i2c_transfer() take care of that on its own? That way there would be no layering violation of having to fetch the quirk bitmask for the i2c adapter being used, that is something that should belong in the core i2c framework.

Yes it is better to put it in sfp_i2c_read().

I think it is best to handle the split-up within the driver. The driver knows how to talk to the device and may apply device quirks.

Also tda1004x [0] and TPM [1] driver also handles it within the driver itself.

Quoted 8 lines

TPM driver just try to send the want size and split-up request to I2C_SMBUS_BLOCK_MAX when a -EOPNOTSUPP returns, just retries it a number of times.

I can do the same but I have to pick a minimum size.
Looking in SSF-8472rev12.2.1 they don't limit the way you access the device. So use I2C_SMBUS_BLOCK_MAX of 32 bytes is sufficient or lookup the i2c quirk max_read_len is also an option.

Hi Florian,

I did a test in sfp_i2c_read() with reducing the read size when -EOPNOTSUPP returns like TPM driver. But this always produces noise in kernel log about the "msg too long" if the device is init at boot or when plugged-in. Devices with SFP_EXT_STATUS generates also an 2nd error.
So I prefer to use the quirk max_read_len.

dmesg output reduce read size when -EOPNOTSUPP returns.

[  134.220000] i2c i2c-0: adapter quirk: msg too long (addr 0x0050, size 96, read)
[  135.250000] sfp sfp-lan5: module FiberStore       SFP-GB-GE-T      rev B    sn <snip>      dc <snip>
[  141.150000] sfp sfp-lan5: module removed
[  150.950000] i2c i2c-0: adapter quirk: msg too long (addr 0x0050, size 96, read)
[  151.980000] sfp sfp-lan5: module FiberStore       SFP-GE-BX        rev A0   sn <snip>      dc <snip>
[  151.990000] i2c i2c-0: adapter quirk: msg too long (addr 0x0051, size 92, read)

dmesg output with read size always reduced to quiek max_read_len.

[   27.350000] sfp sfp-lan5: module FiberStore       SFP-GE-BX        rev A0   sn <snip>      dc <snip>
[   34.540000] sfp sfp-lan5: module removed
[   39.360000] sfp sfp-lan5: module FiberStore       SFP-GB-GE-T      rev B    sn <snip>      dc <snip>

So shall I spin v2 with quirk max_read_len as max read size?

Greats,

René

Quoted 12 lines

Grepping thru the i2c busses I see only 2 devices which has less then 32 bytes of buffer. i2c-nvidia-gpu (Nvidia GPU) and i2c-pmcmsp (microcontroller MSP71xx). Both are unlikely to be used for these kind of applications.

I think I2C_SMBUS_BLOCK_MAX is safe to use.

Greats,

René

[0] https://elixir.bootlin.com/linux/v5.0-rc3/source/drivers/media/dvb-frontends/tda1004x.c#L319
[1] https://elixir.bootlin.com/linux/v5.0-rc3/source/drivers/char/tpm/tpm_i2c_infineon.c#L158

#8 lore raw
Quoted 81 lines · René van Dorst

Quoting René van Dorst <[email protected]>:

Quoted 73 lines

Quoting Florian Fainelli <[email protected]>:

Quoted 52 lines · René van Dorst
Quoted 37 lines

Without this patch sfp code retries to read the full struct sfp_eeprom_id
id out of the SFP eeprom. Sizeof(id) is 96 bytes.
My i2c hardware, Mediatek mt7621, has a rx buffer of 64 bytes. So sfp_read gets -NOSUPPORTED back on his turn return -EAGAIN. Same issue is with the SFP_EXT_STATUS data which is 92 bytes.

By split-up the request in multiple smaller requests with a max size of i2c
max_read_len, we can readout the SFP module successfully.

Tested with MT7621 and two Fiberstore modules SFP-GB-GE-T and SFP-GE-BX.

---
drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-

1 file changed, 22 insertions(+), 1 deletion(-)
drivers/net/phy/sfp.c +5 -1
index fd8bb998ae52..1352a19571cd 100644
@@ -367,7 +367,28 @@ static void sfp_set_state(struct sfp *sfp,
 unsigned int state)
 
 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
 size_t len)
 {
-    return sfp->read(sfp, a2, addr, buf, len);
+    const struct i2c_adapter_quirks *q = sfp->i2c->quirks;
+    int ret;
+    size_t rx_bytes = 0;
+
+    /* Many i2c hw have limited rx buffers, split-up request when

needed. */
+    while ((q->max_read_len) && (len > q->max_read_len)) {

Quoted 8 lines

+        ret = sfp->read(sfp, a2, addr, buf, q->max_read_len); +        if (ret < 0)
+            return ret;
+        rx_bytes += ret;
+        addr += q->max_read_len;
+        buf += q->max_read_len;
+        len -= q->max_read_len;
+    }

sfp->read defaults to sfp_i2c_read() but it is possible to override that by registering a custom sfp_bus instance (nothing does it today, but that could obviously change), so there is no guarantee that sfp->i2c->quirks is applicable unless sfp_i2c_read() is used.

Quoted 5 lines

sfp_i2c_read() is presumably where the max_read_len splitting should occur, or better yet, should not i2c_transfer() take care of that on its own? That way there would be no layering violation of having to fetch the quirk bitmask for the i2c adapter being used, that is something that should belong in the core i2c framework.

Yes it is better to put it in sfp_i2c_read().

I think it is best to handle the split-up within the driver. The driver knows how to talk to the device and may apply device quirks.

Also tda1004x [0] and TPM [1] driver also handles it within the driver itself.

TPM driver just try to send the want size and split-up request to I2C_SMBUS_BLOCK_MAX when a -EOPNOTSUPP returns, just retries it a number of
times.

Quoted 5 lines

I can do the same but I have to pick a minimum size. Looking in SSF-8472rev12.2.1 they don't limit the way you access the device.
So use I2C_SMBUS_BLOCK_MAX of 32 bytes is sufficient or lookup the i2c quirk max_read_len is also an option.

Quoted 7 lines

Hi Florian,

I did a test in sfp_i2c_read() with reducing the read size when -EOPNOTSUPP returns like TPM driver. But this always produces noise in kernel log about the "msg too long" if the device is init at boot or when plugged-in. Devices with SFP_EXT_STATUS generates also an 2nd error.
So I prefer to use the quirk max_read_len.

My point still stands, we have an abstraction layer through the i2c core which sits between clients and adapters.

If you have a simple read into a buffer transaction (like what we have here), then you can provide a safe accessor function that takes care of looking at the max_read_len quirk and splitting things into the appropriate number of transaction. Then we can also eliminate the "msg too long" annoying message since we are now using an appropriate function.

The fact that there are multiple drivers that need to be patched to split i2c transaction is an indication that this is not IMHO addressed at the right level, especially if what they do is as simple as an i2c_transfer() into a single buffer and require no quirky transaction.

Andrew, Heiner and Russell might have a different view on this.

Quoted 22 lines

dmesg output reduce read size when -EOPNOTSUPP returns.
[  134.220000] i2c i2c-0: adapter quirk: msg too long (addr 0x0050, size 96, read)
[  135.250000] sfp sfp-lan5: module FiberStore       SFP-GB-GE-T rev B    sn <snip>      dc <snip>
[  141.150000] sfp sfp-lan5: module removed
[  150.950000] i2c i2c-0: adapter quirk: msg too long (addr 0x0050, size 96, read)
[  151.980000] sfp sfp-lan5: module FiberStore       SFP-GE-BX rev A0   sn <snip>      dc <snip>
[  151.990000] i2c i2c-0: adapter quirk: msg too long (addr 0x0051, size 92, read)

dmesg output with read size always reduced to quiek max_read_len. [   27.350000] sfp sfp-lan5: module FiberStore       SFP-GE-BX rev A0   sn <snip>      dc <snip>
[   34.540000] sfp sfp-lan5: module removed
[   39.360000] sfp sfp-lan5: module FiberStore       SFP-GB-GE-T rev B    sn <snip>      dc <snip>

So shall I spin v2 with quirk max_read_len as max read size?

Signature · 1 line
Florian
maintainer
#9 lore raw
Quoted 9 lines

My point still stands, we have an abstraction layer through the i2c core which sits between clients and adapters.

If you have a simple read into a buffer transaction (like what we have here), then you can provide a safe accessor function that takes care of looking at the max_read_len quirk and splitting things into the appropriate number of transaction. Then we can also eliminate the "msg too long" annoying message since we are now using an appropriate function.

Quoted 6 lines

The fact that there are multiple drivers that need to be patched to split i2c transaction is an indication that this is not IMHO addressed at the right level, especially if what they do is as simple as an i2c_transfer() into a single buffer and require no quirky transaction.

Andrew, Heiner and Russell might have a different view on this.

Hi Florian

I suspect the issue is, only the driver knows if the device supports splitting a read into multiple reads without having to do something in between. Some device might require you do an address setup between each read, for example.

However, the core could offer an i2c_transfer_segmentable(), which does the segmentation as required here, and the driver can then elect to use that, not i2c_transfer().

Andrew