Subject: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

Per ONFI specification (Rev. 4.0), if all parameter pages have invalid
CRC values, the bit-wise majority may be used to recover the contents of
the parameter pages from the parameter page copies present.

Signed-off-by: Jane Wan <[email protected]>
---
drivers/mtd/nand/raw/nand_base.c | 46 +++++++++++++++++++++++++++++++++-----
1 file changed, 41 insertions(+), 5 deletions(-)

diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index 72f3a89..a7c2507 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -5086,6 +5086,34 @@ static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
return ret;
}

+#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01)
+
+/*
+ * Recover data with bit-wise majority
+ */
+static void nand_bit_wise_majority(const void **srcbufs,
+ void *dstbuf,
+ unsigned int nbufs,
+ unsigned int bufsize)
+{
+ int i, j, k;
+ u8 v, m;
+ u8 *p;
+
+ p = *(u8 **)srcbufs;
+ for (i = 0; i < bufsize; i++) {
+ v = 0;
+ for (j = 0; j < 8; j++) {
+ m = 0;
+ for (k = 0; k < nbufs; k++)
+ m += GET_BIT(j, p[k*bufsize + i]);
+ if (m > nbufs/2)
+ v |= BIT(j);
+ }
+ ((u8 *)dstbuf)[i] = v;
+ }
+}
+
/*
* Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
*/
@@ -5102,7 +5130,7 @@ static int nand_flash_detect_onfi(struct nand_chip *chip)
return 0;

/* ONFI chip: allocate a buffer to hold its parameter page */
- p = kzalloc(sizeof(*p), GFP_KERNEL);
+ p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
if (!p)
return -ENOMEM;

@@ -5113,21 +5141,29 @@ static int nand_flash_detect_onfi(struct nand_chip *chip)
}

for (i = 0; i < 3; i++) {
- ret = nand_read_data_op(chip, p, sizeof(*p), true);
+ ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
if (ret) {
ret = 0;
goto free_onfi_param_page;
}

- if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
+ if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
le16_to_cpu(p->crc)) {
+ if (i)
+ memcpy(p, &p[i], sizeof(*p));
break;
}
}

if (i == 3) {
- pr_err("Could not find valid ONFI parameter page; aborting\n");
- goto free_onfi_param_page;
+ pr_err("Could not find valid ONFI parameter page\n");
+ pr_info("Recover ONFI params with bit-wise majority\n");
+ nand_bit_wise_majority((const void **)&p, p, 3, sizeof(*p));
+ if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
+ le16_to_cpu(p->crc)) {
+ pr_err("ONFI parameter recovery failed, aborting\n");
+ goto free_onfi_param_page;
+ }
}

/* Check version */
--
1.7.9.5



2018-05-10 12:04:05

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

Hi Jane,

Subject prefix should be "[PATCH v5] ...", the 2/2 is no longer valid
since you only have one patch here.

On Wed, 9 May 2018 19:46:40 -0700
Jane Wan <[email protected]> wrote:

> Per ONFI specification (Rev. 4.0), if all parameter pages have invalid
> CRC values, the bit-wise majority may be used to recover the contents of
> the parameter pages from the parameter page copies present.
>
> Signed-off-by: Jane Wan <[email protected]>
> ---

There should be a changelog here describing what has changed in each
version of the patch.

> drivers/mtd/nand/raw/nand_base.c | 46 +++++++++++++++++++++++++++++++++-----
> 1 file changed, 41 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
> index 72f3a89..a7c2507 100644
> --- a/drivers/mtd/nand/raw/nand_base.c
> +++ b/drivers/mtd/nand/raw/nand_base.c
> @@ -5086,6 +5086,34 @@ static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
> return ret;
> }
>
> +#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01)

Not sure we need that macro, see below.

> +
> +/*
> + * Recover data with bit-wise majority
> + */
> +static void nand_bit_wise_majority(const void **srcbufs,
> + void *dstbuf,
> + unsigned int nbufs,
> + unsigned int bufsize)

I'd prefer to have nbufs just after srcbufs and named nsrcbufs:

static void nand_bit_wise_majority(const void **srcbufs,
unsigned int nsrcbufs,
void *dstbuf,
unsigned int bufsize)

> +{
> + int i, j, k;
> + u8 v, m;
> + u8 *p;
> +
> + p = *(u8 **)srcbufs;

Nope, I'd like to support the cases where srcbufs are not contiguous,
so that does not work.

> + for (i = 0; i < bufsize; i++) {
> + v = 0;

You can declare the v variable here, since its scope is limited to the
for loop. BTW, v, m, can't we pick better names? I guess v is for val,
but I'm not even sure what m stands for.

> + for (j = 0; j < 8; j++) {
> + m = 0;
> + for (k = 0; k < nbufs; k++)
> + m += GET_BIT(j, p[k*bufsize + i]);

for (k = 0; k < nbufs; k++) {
const u8 *srcbuf = srcbufs[j];

if (srcbuf[i] & BIT(k))
m++;
}

> + if (m > nbufs/2)

Space between operands and operators please

if (m > nbufs / 2)

> + v |= BIT(j);
> + }
> + ((u8 *)dstbuf)[i] = v;
> + }
> +}
> +
> /*
> * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
> */
> @@ -5102,7 +5130,7 @@ static int nand_flash_detect_onfi(struct nand_chip *chip)
> return 0;
>
> /* ONFI chip: allocate a buffer to hold its parameter page */
> - p = kzalloc(sizeof(*p), GFP_KERNEL);
> + p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
> if (!p)
> return -ENOMEM;
>
> @@ -5113,21 +5141,29 @@ static int nand_flash_detect_onfi(struct nand_chip *chip)
> }
>
> for (i = 0; i < 3; i++) {
> - ret = nand_read_data_op(chip, p, sizeof(*p), true);
> + ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
> if (ret) {
> ret = 0;
> goto free_onfi_param_page;
> }
>
> - if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
> + if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
> le16_to_cpu(p->crc)) {
> + if (i)
> + memcpy(p, &p[i], sizeof(*p));
> break;
> }
> }
>
> if (i == 3) {

const void *srcbufs[3] = {p, p + 1, p + 2};

> - pr_err("Could not find valid ONFI parameter page; aborting\n");
> - goto free_onfi_param_page;
> + pr_err("Could not find valid ONFI parameter page\n");
> + pr_info("Recover ONFI params with bit-wise majority\n");
> + nand_bit_wise_majority((const void **)&p, p, 3, sizeof(*p));

nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
sizeof(*p))

> + if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
> + le16_to_cpu(p->crc)) {
> + pr_err("ONFI parameter recovery failed, aborting\n");
> + goto free_onfi_param_page;
> + }
> }
>
> /* Check version */

Thanks,

Boris

Subject: RE: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

Hi Boris,

I've sent v6 of the patch based on your comments.

Thanks.
Jane

> -----Original Message-----
> From: Boris Brezillon [mailto:[email protected]]
> Sent: Thursday, May 10, 2018 5:03 AM
> To: Wan, Jane (Nokia - US/Sunnyvale) <[email protected]>
> Cc: [email protected]; [email protected];
> [email protected]; [email protected]; [email protected];
> [email protected]; [email protected];
> [email protected]; [email protected];
> [email protected]; [email protected]; linux-
> [email protected]; Bos, Ties (Nokia - US/Sunnyvale) <[email protected]>
> Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the
> contents of ONFI parameter
>
> Hi Jane,
>
> Subject prefix should be "[PATCH v5] ...", the 2/2 is no longer valid since you only
> have one patch here.
>
> On Wed, 9 May 2018 19:46:40 -0700
> Jane Wan <[email protected]> wrote:
>
> > Per ONFI specification (Rev. 4.0), if all parameter pages have invalid
> > CRC values, the bit-wise majority may be used to recover the contents
> > of the parameter pages from the parameter page copies present.
> >
> > Signed-off-by: Jane Wan <[email protected]>
> > ---
>
> There should be a changelog here describing what has changed in each version
> of the patch.

[Jane] Added the changelogs in v6.

>
> > drivers/mtd/nand/raw/nand_base.c | 46
> +++++++++++++++++++++++++++++++++-----
> > 1 file changed, 41 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/mtd/nand/raw/nand_base.c
> > b/drivers/mtd/nand/raw/nand_base.c
> > index 72f3a89..a7c2507 100644
> > --- a/drivers/mtd/nand/raw/nand_base.c
> > +++ b/drivers/mtd/nand/raw/nand_base.c
> > @@ -5086,6 +5086,34 @@ static int
> nand_flash_detect_ext_param_page(struct nand_chip *chip,
> > return ret;
> > }
> >
> > +#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01)
>
> Not sure we need that macro, see below.

[Jane] Removed.

>
> > +
> > +/*
> > + * Recover data with bit-wise majority */ static void
> > +nand_bit_wise_majority(const void **srcbufs,
> > + void *dstbuf,
> > + unsigned int nbufs,
> > + unsigned int bufsize)
>
> I'd prefer to have nbufs just after srcbufs and named nsrcbufs:
>
> static void nand_bit_wise_majority(const void **srcbufs,
> unsigned int nsrcbufs,
> void *dstbuf,
> unsigned int bufsize)

[Jane] changed as above in v6.

>
> > +{
> > + int i, j, k;
> > + u8 v, m;
> > + u8 *p;
> > +
> > + p = *(u8 **)srcbufs;
>
> Nope, I'd like to support the cases where srcbufs are not contiguous, so that
> does not work.

[Jane] Changed as you suggested to support non-contiguous srcbufs.

>
> > + for (i = 0; i < bufsize; i++) {
> > + v = 0;
>
> You can declare the v variable here, since its scope is limited to the for loop.
> BTW, v, m, can't we pick better names? I guess v is for val, but I'm not even sure
> what m stands for.

[Jane] changed the variables to cnt and val in v6. The "m" was for majority, now changed to cnt (counts for 1s).

>
> > + for (j = 0; j < 8; j++) {
> > + m = 0;
> > + for (k = 0; k < nbufs; k++)
> > + m += GET_BIT(j, p[k*bufsize + i]);
>
> for (k = 0; k < nbufs; k++) {
> const u8 *srcbuf = srcbufs[j];
>
> if (srcbuf[i] & BIT(k))
> m++;
> }
>
> > + if (m > nbufs/2)
>
> Space between operands and operators please
>
> if (m > nbufs / 2)

[Jane] Changed as suggested in v6. Thanks.

>
> > + v |= BIT(j);
> > + }
> > + ((u8 *)dstbuf)[i] = v;
> > + }
> > +}
> > +
> > /*
> > * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
> > */
> > @@ -5102,7 +5130,7 @@ static int nand_flash_detect_onfi(struct nand_chip
> *chip)
> > return 0;
> >
> > /* ONFI chip: allocate a buffer to hold its parameter page */
> > - p = kzalloc(sizeof(*p), GFP_KERNEL);
> > + p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
> > if (!p)
> > return -ENOMEM;
> >
> > @@ -5113,21 +5141,29 @@ static int nand_flash_detect_onfi(struct
> nand_chip *chip)
> > }
> >
> > for (i = 0; i < 3; i++) {
> > - ret = nand_read_data_op(chip, p, sizeof(*p), true);
> > + ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
> > if (ret) {
> > ret = 0;
> > goto free_onfi_param_page;
> > }
> >
> > - if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
> > + if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
> > le16_to_cpu(p->crc)) {
> > + if (i)
> > + memcpy(p, &p[i], sizeof(*p));
> > break;
> > }
> > }
> >
> > if (i == 3) {
>
> const void *srcbufs[3] = {p, p + 1, p + 2};
>
> > - pr_err("Could not find valid ONFI parameter page; aborting\n");
> > - goto free_onfi_param_page;
> > + pr_err("Could not find valid ONFI parameter page\n");
> > + pr_info("Recover ONFI params with bit-wise majority\n");
> > + nand_bit_wise_majority((const void **)&p, p, 3, sizeof(*p));
>
> nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
> sizeof(*p))

[Jane] Changed in v6. Thanks.

>
> > + if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
> > + le16_to_cpu(p->crc)) {
> > + pr_err("ONFI parameter recovery failed, aborting\n");
> > + goto free_onfi_param_page;
> > + }
> > }
> >
> > /* Check version */
>
> Thanks,
>
> Boris

2018-05-14 17:59:41

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

On Thu, May 10, 2018 at 3:03 PM, Boris Brezillon
<[email protected]> wrote:

>> +#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01)
>
> Not sure we need that macro, see below.

+1. We have too many nice helpers for bit manipulations
(for_each_set_bit() as an example).


> for (k = 0; k < nbufs; k++) {
> const u8 *srcbuf = srcbufs[j];
>
> if (srcbuf[i] & BIT(k))
> m++;
> }

...which is effectively hweightXX().

>> - p = kzalloc(sizeof(*p), GFP_KERNEL);
>> + p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
>> if (!p)
>> return -ENOMEM;

...which is kcalloc().


--
With Best Regards,
Andy Shevchenko

2018-05-15 07:36:15

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

On Mon, 14 May 2018 20:54:36 +0300
Andy Shevchenko <[email protected]> wrote:

> On Thu, May 10, 2018 at 3:03 PM, Boris Brezillon
> <[email protected]> wrote:
>
> >> +#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01)
> >
> > Not sure we need that macro, see below.
>
> +1. We have too many nice helpers for bit manipulations
> (for_each_set_bit() as an example).
>
>
> > for (k = 0; k < nbufs; k++) {
> > const u8 *srcbuf = srcbufs[j];
> >
> > if (srcbuf[i] & BIT(k))
> > m++;
> > }
>
> ...which is effectively hweightXX().

No it's not.

2018-05-15 07:46:26

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

On Tue, May 15, 2018 at 10:35 AM, Boris Brezillon
<[email protected]> wrote:
> On Mon, 14 May 2018 20:54:36 +0300
> Andy Shevchenko <[email protected]> wrote:
>
>> On Thu, May 10, 2018 at 3:03 PM, Boris Brezillon
>> <[email protected]> wrote:
>>
>> >> +#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01)
>> >
>> > Not sure we need that macro, see below.
>>
>> +1. We have too many nice helpers for bit manipulations
>> (for_each_set_bit() as an example).
>>
>>
>> > for (k = 0; k < nbufs; k++) {
>> > const u8 *srcbuf = srcbufs[j];
>> >
>> > if (srcbuf[i] & BIT(k))
>> > m++;
>> > }
>>
>> ...which is effectively hweightXX().
>
> No it's not.

I don't see how "not". In the loop everithing except m and k are
invariants. What did I miss?

The powerness of two of nbufs is another thing of _existing_
prototypes of hweightXX().

--
With Best Regards,
Andy Shevchenko

2018-05-15 08:04:51

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

On Tue, 15 May 2018 10:46:00 +0300
Andy Shevchenko <[email protected]> wrote:

> On Tue, May 15, 2018 at 10:35 AM, Boris Brezillon
> <[email protected]> wrote:
> > On Mon, 14 May 2018 20:54:36 +0300
> > Andy Shevchenko <[email protected]> wrote:
> >
> >> On Thu, May 10, 2018 at 3:03 PM, Boris Brezillon
> >> <[email protected]> wrote:
> >>
> >> >> +#define GET_BIT(bit, val) (((val) >> (bit)) & 0x01)
> >> >
> >> > Not sure we need that macro, see below.
> >>
> >> +1. We have too many nice helpers for bit manipulations
> >> (for_each_set_bit() as an example).
> >>
> >>
> >> > for (k = 0; k < nbufs; k++) {
> >> > const u8 *srcbuf = srcbufs[j];
> >> >
> >> > if (srcbuf[i] & BIT(k))
> >> > m++;
> >> > }
> >>
> >> ...which is effectively hweightXX().
> >
> > No it's not.
>
> I don't see how "not". In the loop everithing except m and k are
> invariants. What did I miss?

We're not counting the number of bits set in an uXX var, but the number
of set bits at the same position in different buffers.

>
> The powerness of two of nbufs is another thing of _existing_
> prototypes of hweightXX().
>

2018-05-15 20:23:41

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

On Tue, May 15, 2018 at 11:03 AM, Boris Brezillon
<[email protected]> wrote:
> On Tue, 15 May 2018 10:46:00 +0300
> Andy Shevchenko <[email protected]> wrote:
>
>> On Tue, May 15, 2018 at 10:35 AM, Boris Brezillon
>> <[email protected]> wrote:
>> > On Mon, 14 May 2018 20:54:36 +0300
>> > Andy Shevchenko <[email protected]> wrote:

>> >> > for (k = 0; k < nbufs; k++) {
>> >> > const u8 *srcbuf = srcbufs[j];
>> >> >
>> >> > if (srcbuf[i] & BIT(k))
>> >> > m++;
>> >> > }
>> >>
>> >> ...which is effectively hweightXX().
>> >
>> > No it's not.
>>
>> I don't see how "not". In the loop everithing except m and k are
>> invariants. What did I miss?
>
> We're not counting the number of bits set in an uXX var, but the number
> of set bits at the same position in different buffers.

...on big picture. The excerpt above is hweight() against srcbuf[i].

Let's rewrite it like this:

const u8 *srcbuf = srcbufs[j];

for (k = 0; k < nbufs; k++) {
if (srcbuf[i] & BIT(k))
m++;
}

...and now it looks obvious:

m += hweight...(srcbuf[i])

_If_ nbufs is power of two we may use primitive helper.

--
With Best Regards,
Andy Shevchenko

2018-05-15 20:35:49

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

On Tue, 15 May 2018 23:23:02 +0300
Andy Shevchenko <[email protected]> wrote:

> On Tue, May 15, 2018 at 11:03 AM, Boris Brezillon
> <[email protected]> wrote:
> > On Tue, 15 May 2018 10:46:00 +0300
> > Andy Shevchenko <[email protected]> wrote:
> >
> >> On Tue, May 15, 2018 at 10:35 AM, Boris Brezillon
> >> <[email protected]> wrote:
> >> > On Mon, 14 May 2018 20:54:36 +0300
> >> > Andy Shevchenko <[email protected]> wrote:
>
> >> >> > for (k = 0; k < nbufs; k++) {
> >> >> > const u8 *srcbuf = srcbufs[j];
> >> >> >
> >> >> > if (srcbuf[i] & BIT(k))
> >> >> > m++;
> >> >> > }
> >> >>
> >> >> ...which is effectively hweightXX().
> >> >
> >> > No it's not.
> >>
> >> I don't see how "not". In the loop everithing except m and k are
> >> invariants. What did I miss?
> >
> > We're not counting the number of bits set in an uXX var, but the number
> > of set bits at the same position in different buffers.
>
> ...on big picture. The excerpt above is hweight() against srcbuf[i].
>
> Let's rewrite it like this:
>
> const u8 *srcbuf = srcbufs[j];
>
> for (k = 0; k < nbufs; k++) {
> if (srcbuf[i] & BIT(k))

I made a mistake in my code sample, it's

if (srcbuf[i] & BIT(j))

If you look at v6, you'll see it's been fixed by Jane.

> m++;
> }
>
> ...and now it looks obvious:
>
> m += hweight...(srcbuf[i])
>
> _If_ nbufs is power of two we may use primitive helper.
>


2018-05-15 21:03:24

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 2/2] mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter

On Tue, May 15, 2018 at 11:35 PM, Boris Brezillon
<[email protected]> wrote:
> On Tue, 15 May 2018 23:23:02 +0300
> Andy Shevchenko <[email protected]> wrote:

> I made a mistake in my code sample, it's
>
> if (srcbuf[i] & BIT(j))
>
> If you look at v6, you'll see it's been fixed by Jane.

In this case, indeed.

--
With Best Regards,
Andy Shevchenko