diff mbox series

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

Message ID 20190123212046.13020-1-opensource@vdorst.com
State Changes Requested
Delegated to: David Miller
Headers show
Series sfp: sfp_read: split-up request when hw rx buffer is too small. | expand

Commit Message

René van Dorst Jan. 23, 2019, 9:20 p.m. UTC
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.

Signed-off-by: René van Dorst <opensource@vdorst.com>
---
 drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

Comments

Andrew Lunn Jan. 23, 2019, 9:32 p.m. UTC | #1
On Wed, Jan 23, 2019 at 10:20:46PM +0100, René van Dorst wrote:
> 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.
> 
> Signed-off-by: René van Dorst <opensource@vdorst.com>
> ---
>  drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
>  1 file changed, 22 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
> index fd8bb998ae52..1352a19571cd 100644
> --- a/drivers/net/phy/sfp.c
> +++ b/drivers/net/phy/sfp.c
> @@ -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);

Hi René

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

> +		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.

> +	}
> +
> +	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
Florian Fainelli Jan. 23, 2019, 10:43 p.m. UTC | #2
On 1/23/19 1:20 PM, René van Dorst wrote:
> 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.
> 
> Signed-off-by: René van Dorst <opensource@vdorst.com>
> ---
>  drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
>  1 file changed, 22 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
> index fd8bb998ae52..1352a19571cd 100644
> --- a/drivers/net/phy/sfp.c
> +++ b/drivers/net/phy/sfp.c
> @@ -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;
> +	}

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.

> +
> +	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)
>
René van Dorst Jan. 24, 2019, 1:15 p.m. UTC | #3
Quoting Florian Fainelli <f.fainelli@gmail.com>:

> On 1/23/19 1:20 PM, René van Dorst wrote:
>> 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.
>>
>> Signed-off-by: René van Dorst <opensource@vdorst.com>
>> ---
>>  drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
>>  1 file changed, 22 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
>> index fd8bb998ae52..1352a19571cd 100644
>> --- a/drivers/net/phy/sfp.c
>> +++ b/drivers/net/phy/sfp.c
>> @@ -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;
>> +	}
>
> 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.

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
René van Dorst Jan. 24, 2019, 2:03 p.m. UTC | #4
Quoting Andrew Lunn <andrew@lunn.ch>:

> On Wed, Jan 23, 2019 at 10:20:46PM +0100, René van Dorst wrote:
>> 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.
>>
>> Signed-off-by: René van Dorst <opensource@vdorst.com>
>> ---
>>  drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
>>  1 file changed, 22 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
>> index fd8bb998ae52..1352a19571cd 100644
>> --- a/drivers/net/phy/sfp.c
>> +++ b/drivers/net/phy/sfp.c
>> @@ -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);
>
> Hi René
>
> 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.

>> +		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.

>> +	}
>> +
>> +	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?

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é
Andrew Lunn Jan. 24, 2019, 2:12 p.m. UTC | #5
> >>+	/* 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().
> 
> 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.

> >>+		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.

> 
> >>+		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.

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.

> 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
René van Dorst Jan. 24, 2019, 2:46 p.m. UTC | #6
Quoting Andrew Lunn <andrew@lunn.ch>:

>> >>+	/* 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().
>>
>> 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.
>
>> >>+		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.

>>
>> >>+		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.
>
> 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.
>
>> 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

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é
René van Dorst Jan. 25, 2019, 9:58 p.m. UTC | #7
Quoting René van Dorst <opensource@vdorst.com>:

> Quoting Florian Fainelli <f.fainelli@gmail.com>:
>
>> On 1/23/19 1:20 PM, René van Dorst wrote:
>>> 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.
>>>
>>> Signed-off-by: René van Dorst <opensource@vdorst.com>
>>> ---
>>> drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
>>> 1 file changed, 22 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
>>> index fd8bb998ae52..1352a19571cd 100644
>>> --- a/drivers/net/phy/sfp.c
>>> +++ b/drivers/net/phy/sfp.c
>>> @@ -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;
>>> +	}
>>
>> 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.
>
> 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.

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é

> 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
Florian Fainelli Jan. 25, 2019, 10:28 p.m. UTC | #8
On 1/25/19 1:58 PM, René van Dorst wrote:
> Quoting René van Dorst <opensource@vdorst.com>:
> 
>> Quoting Florian Fainelli <f.fainelli@gmail.com>:
>>
>>> On 1/23/19 1:20 PM, René van Dorst wrote:
>>>> 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.
>>>>
>>>> Signed-off-by: René van Dorst <opensource@vdorst.com>
>>>> ---
>>>> drivers/net/phy/sfp.c | 23 ++++++++++++++++++++++-
>>>> 1 file changed, 22 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
>>>> index fd8bb998ae52..1352a19571cd 100644
>>>> --- a/drivers/net/phy/sfp.c
>>>> +++ b/drivers/net/phy/sfp.c
>>>> @@ -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;
>>>> +    }
>>>
>>> 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.
>>
>> 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.
> 
> 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.

> 
> 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?
Andrew Lunn Jan. 25, 2019, 10:43 p.m. UTC | #9
> 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.

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
diff mbox series

Patch

diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
index fd8bb998ae52..1352a19571cd 100644
--- a/drivers/net/phy/sfp.c
+++ b/drivers/net/phy/sfp.c
@@ -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)