Re: [PATCH v2 1/3] net: sfp: add workaround for Realtek RTL8672 and RTL9601C chips

From: Pali Rohár
Date: Thu Jan 07 2021 - 14:18:30 EST


On Thursday 07 January 2021 18:19:23 Andrew Lunn wrote:
> > + if (sfp->i2c_block_size < 2) {
> > + dev_info(sfp->dev, "skipping hwmon device registration "
> > + "due to broken EEPROM\n");
> > + dev_info(sfp->dev, "diagnostic EEPROM area cannot be read "
> > + "atomically to guarantee data coherency\n");
>
> Strings like this are the exception to the 80 character rule. People
> grep for them, and when they are split, they are harder to find.

Ok. I will fix it.

> > -static int sfp_quirk_i2c_block_size(const struct sfp_eeprom_base *base)
> > +static bool sfp_id_needs_byte_io(struct sfp *sfp, void *buf, size_t len)
> > {
> > - if (!memcmp(base->vendor_name, "VSOL ", 16))
> > - return 1;
> > - if (!memcmp(base->vendor_name, "OEM ", 16) &&
> > - !memcmp(base->vendor_pn, "V2801F ", 16))
> > - return 1;
> > + size_t i, block_size = sfp->i2c_block_size;
> >
> > - /* Some modules can't cope with long reads */
> > - return 16;
> > -}
> > + /* Already using byte IO */
> > + if (block_size == 1)
> > + return false;
>
> This seems counter intuitive. We don't need byte IO because we are
> doing btye IO? Can we return True here?

I do not know this part was written by Russel.

Currently function is used in a way if sfp subsystem should switch to
byte IO. So if we are already using byte IO we are not going to do
switch and therefore false is returning.

At least this is how I understood why 'return false' is there.

> >
> > -static void sfp_quirks_base(struct sfp *sfp, const struct sfp_eeprom_base *base)
> > -{
> > - sfp->i2c_block_size = sfp_quirk_i2c_block_size(base);
> > + for (i = 1; i < len; i += block_size) {
> > + if (memchr_inv(buf + i, '\0', block_size - 1))
> > + return false;
> > + }
>
> Is the loop needed?

Originally I wanted to use just four memcmp() calls but Russel told me
that code should be generic (in case in future initial block size would
be changed, which is a good argument) and come up with this code with
for-loop.

So I think loop is needed.

> I also wonder if on the last iteration of the loop you go passed the
> end of buf? Don't you need a min(block_size -1, len - i) or
> similar?

You are right, if code is generic this needs to be fixed to prevent
reading reading undefined memory. I will replace it by proposed min(...)
call.