mtd: rawnand: use bit-wise majority to recover the contents of ONFI parameter
Subsystems (2)
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.
--- drivers/mtd/nand/raw/nand_base.c | 46 +++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-)
drivers/mtd/nand/raw/nand_base.c +41 -5
@@ -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 */
Signature · 1 line
1.7.9.5
Hi Jane,
Subject prefix should be "[PATCH v5] ...", the 2/2 is no longer valid since you only have one patch here.
Quoted 6 lines · 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 static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5089
Quoted 12 lines
drivers/mtd/nand/raw/nand_base.c | 46 +++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-)drivers/mtd/nand/raw/nand_base.c +1
-0@@ -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.
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5097
Quoted 8 lines
drivers/mtd/nand/raw/nand_base.c +8
-0+ +/* +* 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)
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5103
Quoted 6 lines
drivers/mtd/nand/raw/nand_base.c +6
-0+{ +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.
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5105
Quoted 2 lines
drivers/mtd/nand/raw/nand_base.c +2
-0+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.
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5109
Quoted 4 lines
drivers/mtd/nand/raw/nand_base.c +4
-0+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++;
}drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5110
Quoted 1 line
drivers/mtd/nand/raw/nand_base.c +1
-0+if (m > nbufs/2 )
Space between operands and operators please
if (m > nbufs / 2)
Quoted 29 lines
drivers/mtd/nand/raw/nand_base.c +8
-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; }
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip *chip) · line 5158
Quoted 10 lines
drivers/mtd/nand/raw/nand_base.c +3
-1-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};drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip *chip) · line 5161
Quoted 5 lines
drivers/mtd/nand/raw/nand_base.c +3
-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))
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip *chip) · line 5169
Quoted 8 lines
drivers/mtd/nand/raw/nand_base.c +5
-0+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
Hi Boris,
I've sent v6 of the patch based on your comments.
Thanks.
Jane
Quoted 25 lines
-----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 parameterHi Jane,
Subject prefix should be "[PATCH v5] ...", the 2/2 is no longer valid since you only have one patch here.
Quoted 4 lines · 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.
Quoted 5 lines
Quoted 2 lines
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.
Quoted 12 lines
Quoted 2 lines
drivers/mtd/nand/raw/nand_base.c | 46+++++++++++++++++++++++++++++++++-----
drivers/mtd/nand/raw/nand_base.c static int
Quoted 8 lines
1 file changed, 41 insertions(+), 5 deletions(-)drivers/mtd/nand/raw/nand_base.c +0
-0@@ -5086,6 +5086,34 @@ static intnand_flash_detect_ext_param_page(struct
nand_chip*chip,
Quoted 6 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5089
Quoted 4 lines
drivers/mtd/nand/raw/nand_base.c +1
-0return ret; } +#define GET_BIT (bit, val) (((val) >> (bit)) & 0x01)Not sure we need that macro, see below.
[Jane] Removed.
Quoted 11 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5097
Quoted 8 lines
drivers/mtd/nand/raw/nand_base.c +7
-0+ +/* +* 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:
Quoted 4 lines
static void nand_bit_wise_majority(const void **srcbufs,
unsigned int nsrcbufs, void *dstbuf, unsigned int bufsize)
[Jane] changed as above in v6.
Quoted 10 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5103
Quoted 7 lines
drivers/mtd/nand/raw/nand_base.c +6
-0+{ +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.
Quoted 7 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5105
Quoted 3 lines
drivers/mtd/nand/raw/nand_base.c +2
-0+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).
Quoted 13 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5109
Quoted 5 lines
drivers/mtd/nand/raw/nand_base.c +4
-0+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++; }
Quoted 5 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_ext_param_page(struct nand_chip *chip, · line 5110
Quoted 1 line
drivers/mtd/nand/raw/nand_base.c +1
-0+if (m > nbufs/2 )Space between operands and operators please
if (m > nbufs / 2)
[Jane] Changed as suggested in v6. Thanks.
Quoted 45 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip
Quoted 11 lines
drivers/mtd/nand/raw/nand_base.c +6
-0+ 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)
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct
Quoted 9 lines
drivers/mtd/nand/raw/nand_base.c +1
-1return 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)Quoted 10 lines
drivers/mtd/nand/raw/nand_base.c +1
-1}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; }drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip *chip) · line 5158
Quoted 10 lines
drivers/mtd/nand/raw/nand_base.c +3
-1-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};
Quoted 8 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip *chip) · line 5161
Quoted 5 lines
drivers/mtd/nand/raw/nand_base.c +3
-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.
Quoted 13 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip *chip) · line 5169
Quoted 9 lines
drivers/mtd/nand/raw/nand_base.c +5
-0+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
Quoted 3 lines · Boris Brezillon
Quoted 1 line
+#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).
Quoted 6 lines
for (k = 0; k < nbufs; k++) {
const u8 *srcbuf = srcbufs[j];
if (srcbuf[i] & BIT(k))
m++;
}...which is effectively hweightXX().
Quoted 4 lines
drivers/mtd/nand/raw/nand_base.c static int nand_flash_detect_onfi(struct nand_chip *chip) · line 5135
Quoted 4 lines
drivers/mtd/nand/raw/nand_base.c +1
-1- p =kzalloc (sizeof (*p),GFP_KERNEL ); + p =kzalloc ((sizeof (*p) *3 ),GFP_KERNEL );if (!p)return -ENOMEM ;
...which is kcalloc().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 11 lines · On Mon, 14 May 2018 20:54:36 +0300 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Boris Brezillon
Quoted 1 line
+#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).
Quoted 8 lines
Quoted 6 lines
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.
Quoted 17 lines · Boris Brezillon
Quoted 14 lines · On Mon, 14 May 2018 20:54:36 +0300 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Boris Brezillon
Quoted 1 line
+#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).Quoted 3 lines
for (k = 0; k < nbufs; k++) { const u8 *srcbuf = srcbufs[j];
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
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().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 23 lines · On Tue, 15 May 2018 10:46:00 +0300 Andy Shevchenko <[email protected]>…
Quoted 21 lines · Boris Brezillon
Quoted 18 lines · On Mon, 14 May 2018 20:54:36 +0300 Andy Shevchenko <[email protected]>…
Quoted 3 lines · Boris Brezillon
Quoted 1 line
+#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).Quoted 6 lines
for (k = 0; k < nbufs; k++) { const u8 *srcbuf = srcbufs[j]; if (srcbuf[i] & BIT(k)) m++; }
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
...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.
Quoted 4 lines
The powerness of two of nbufs is another thing of _existing_ prototypes of
hweightXX().
Quoted 17 lines · Boris Brezillon
Quoted 14 lines · On Tue, 15 May 2018 10:46:00 +0300 Andy Shevchenko <[email protected]>…
Quoted 12 lines · Boris Brezillon
Quoted 8 lines · On Mon, 14 May 2018 20:54:36 +0300 Andy Shevchenko <[email protected]>…
Quoted 6 lines
for (k = 0; k < nbufs; k++) { const u8 *srcbuf = srcbufs[j]; if (srcbuf[i] & BIT(k)) m++; }...which is effectively
hweightXX().
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
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.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 27 lines · On Tue, 15 May 2018 23:23:02 +0300 Andy Shevchenko <[email protected]>…
Quoted 17 lines · Boris Brezillon
Quoted 14 lines · On Tue, 15 May 2018 10:46:00 +0300 Andy Shevchenko <[email protected]>…
Quoted 12 lines · Boris Brezillon
Quoted 8 lines · On Mon, 14 May 2018 20:54:36 +0300 Andy Shevchenko <[email protected]>…
Quoted 6 lines
for (k = 0; k < nbufs; k++) { const u8 *srcbuf = srcbufs[j]; if (srcbuf[i] & BIT(k)) m++; }...which is effectively
hweightXX().Quoted 7 lines
Quoted 4 lines
Quoted 1 line
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.
Quoted 8 lines
...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.
Quoted 9 lines
m++; }...and now it looks obvious:
m += hweight...(srcbuf[i])
_If_ nbufs is power of two we may use primitive helper.
Quoted 8 lines · Boris Brezillon
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.
Signature · 2 lines
With Best Regards, Andy Shevchenko