* [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions
@ 2015-12-21 14:42 Markus Pargmann
2015-12-21 14:42 ` [PATCH 2/2] mtd: gpmi: Add erased page bitflip correction Markus Pargmann
2016-01-04 9:44 ` [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Markus Pargmann @ 2015-12-21 14:42 UTC (permalink / raw)
To: barebox
This adds the bitflip check helper functions from the kernel. They are
used to check for bitflips in erased pages and correct them in the
buffer so that UBI can work with it. Unfortunately most nand controllers
do not have ECC for erased pages and don't do this on their own.
Signed-off-by: Markus Pargmann <mpa@pengutronix.de>
---
drivers/mtd/nand/nand_base.c | 128 +++++++++++++++++++++++++++++++++++++++++++
include/linux/mtd/nand.h | 5 ++
2 files changed, 133 insertions(+)
diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index ca48f59de1b1..554d3d2f96d4 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -907,6 +907,134 @@ out:
EXPORT_SYMBOL(nand_lock);
/**
+ * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
+ * @buf: buffer to test
+ * @len: buffer length
+ * @bitflips_threshold: maximum number of bitflips
+ *
+ * Check if a buffer contains only 0xff, which means the underlying region
+ * has been erased and is ready to be programmed.
+ * The bitflips_threshold specify the maximum number of bitflips before
+ * considering the region is not erased.
+ * Note: The logic of this function has been extracted from the memweight
+ * implementation, except that nand_check_erased_buf function exit before
+ * testing the whole buffer if the number of bitflips exceed the
+ * bitflips_threshold value.
+ *
+ * Returns a positive number of bitflips less than or equal to
+ * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
+ * threshold.
+ */
+static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
+{
+ const unsigned char *bitmap = buf;
+ int bitflips = 0;
+ int weight;
+
+ for (; len && ((uintptr_t)bitmap) % sizeof(long);
+ len--, bitmap++) {
+ weight = hweight8(*bitmap);
+ bitflips += BITS_PER_BYTE - weight;
+ if (unlikely(bitflips > bitflips_threshold))
+ return -EBADMSG;
+ }
+
+ for (; len >= sizeof(long);
+ len -= sizeof(long), bitmap += sizeof(long)) {
+ weight = hweight_long(*((unsigned long *)bitmap));
+ bitflips += BITS_PER_LONG - weight;
+ if (unlikely(bitflips > bitflips_threshold))
+ return -EBADMSG;
+ }
+
+ for (; len > 0; len--, bitmap++) {
+ weight = hweight8(*bitmap);
+ bitflips += BITS_PER_BYTE - weight;
+ if (unlikely(bitflips > bitflips_threshold))
+ return -EBADMSG;
+ }
+
+ return bitflips;
+}
+
+/**
+ * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
+ * 0xff data
+ * @data: data buffer to test
+ * @datalen: data length
+ * @ecc: ECC buffer
+ * @ecclen: ECC length
+ * @extraoob: extra OOB buffer
+ * @extraooblen: extra OOB length
+ * @bitflips_threshold: maximum number of bitflips
+ *
+ * Check if a data buffer and its associated ECC and OOB data contains only
+ * 0xff pattern, which means the underlying region has been erased and is
+ * ready to be programmed.
+ * The bitflips_threshold specify the maximum number of bitflips before
+ * considering the region as not erased.
+ *
+ * Note:
+ * 1/ ECC algorithms are working on pre-defined block sizes which are usually
+ * different from the NAND page size. When fixing bitflips, ECC engines will
+ * report the number of errors per chunk, and the NAND core infrastructure
+ * expect you to return the maximum number of bitflips for the whole page.
+ * This is why you should always use this function on a single chunk and
+ * not on the whole page. After checking each chunk you should update your
+ * max_bitflips value accordingly.
+ * 2/ When checking for bitflips in erased pages you should not only check
+ * the payload data but also their associated ECC data, because a user might
+ * have programmed almost all bits to 1 but a few. In this case, we
+ * shouldn't consider the chunk as erased, and checking ECC bytes prevent
+ * this case.
+ * 3/ The extraoob argument is optional, and should be used if some of your OOB
+ * data are protected by the ECC engine.
+ * It could also be used if you support subpages and want to attach some
+ * extra OOB data to an ECC chunk.
+ *
+ * Returns a positive number of bitflips less than or equal to
+ * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
+ * threshold. In case of success, the passed buffers are filled with 0xff.
+ */
+int nand_check_erased_ecc_chunk(void *data, int datalen,
+ void *ecc, int ecclen,
+ void *extraoob, int extraooblen,
+ int bitflips_threshold)
+{
+ int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
+
+ data_bitflips = nand_check_erased_buf(data, datalen,
+ bitflips_threshold);
+ if (data_bitflips < 0)
+ return data_bitflips;
+
+ bitflips_threshold -= data_bitflips;
+
+ ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
+ if (ecc_bitflips < 0)
+ return ecc_bitflips;
+
+ bitflips_threshold -= ecc_bitflips;
+
+ extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
+ bitflips_threshold);
+ if (extraoob_bitflips < 0)
+ return extraoob_bitflips;
+
+ if (data_bitflips)
+ memset(data, 0xff, datalen);
+
+ if (ecc_bitflips)
+ memset(ecc, 0xff, ecclen);
+
+ if (extraoob_bitflips)
+ memset(extraoob, 0xff, extraooblen);
+
+ return data_bitflips + ecc_bitflips + extraoob_bitflips;
+}
+EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
+
+/**
* nand_read_page_raw - [INTERN] read raw page data without ecc
* @mtd: mtd info structure
* @chip: nand chip info structure
diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
index f126cd9fbfbb..83d664e7ee06 100644
--- a/include/linux/mtd/nand.h
+++ b/include/linux/mtd/nand.h
@@ -46,6 +46,11 @@ extern int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
/* unlocks specified locked blocks */
extern int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
+extern int nand_check_erased_ecc_chunk(void *data, int datalen,
+ void *ecc, int ecclen,
+ void *extraoob, int extraooblen,
+ int bitflips_threshold);
+
/* The maximum number of NAND chips in an array */
#define NAND_MAX_CHIPS 8
--
2.6.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] mtd: gpmi: Add erased page bitflip correction
2015-12-21 14:42 [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions Markus Pargmann
@ 2015-12-21 14:42 ` Markus Pargmann
2016-01-04 9:44 ` [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Markus Pargmann @ 2015-12-21 14:42 UTC (permalink / raw)
To: barebox
Hardware ECC does not work for erased pages. However as soon as
something that is not 0xff is found in the page, hardware ECC assumes
this is valid data and produces an uncorrectable error ECC status. We
can use that to check for bitflips in erased pages and fix them if the
number of flipped bits is below the ecc_strength.
We need to move the memcpy above the for loop to be able to access the
buffer directly.
Signed-off-by: Markus Pargmann <mpa@pengutronix.de>
---
drivers/mtd/nand/nand_mxs.c | 42 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 40 insertions(+), 2 deletions(-)
diff --git a/drivers/mtd/nand/nand_mxs.c b/drivers/mtd/nand/nand_mxs.c
index b3767e6cbeda..7635e2a418df 100644
--- a/drivers/mtd/nand/nand_mxs.c
+++ b/drivers/mtd/nand/nand_mxs.c
@@ -737,6 +737,8 @@ static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
/* Read DMA completed, now do the mark swapping. */
mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
+ memcpy(buf, nand_info->data_buf, mtd->writesize);
+
/* Loop over status bytes, accumulating ECC status. */
status = nand_info->oob_buf + mxs_nand_aux_status_offset();
for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
@@ -747,6 +749,44 @@ static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
continue;
if (status[i] == 0xfe) {
+ int flips;
+
+ /*
+ * The ECC hardware has an uncorrectable ECC status
+ * code in case we have bitflips in an erased page. As
+ * nothing was written into this subpage the ECC is
+ * obviously wrong and we can not trust it. We assume
+ * at this point that we are reading an erased page and
+ * try to correct the bitflips in buffer up to
+ * ecc_strength bitflips. If this is a page with random
+ * data, we exceed this number of bitflips and have a
+ * ECC failure. Otherwise we use the corrected buffer.
+ */
+ if (i == 0) {
+ /* The first block includes metadata */
+ flips = nand_check_erased_ecc_chunk(
+ buf + i * MXS_NAND_CHUNK_DATA_CHUNK_SIZE,
+ MXS_NAND_CHUNK_DATA_CHUNK_SIZE,
+ NULL, 0,
+ nand_info->oob_buf,
+ MXS_NAND_METADATA_SIZE,
+ mtd->ecc_strength);
+ } else {
+ flips = nand_check_erased_ecc_chunk(
+ buf + i * MXS_NAND_CHUNK_DATA_CHUNK_SIZE,
+ MXS_NAND_CHUNK_DATA_CHUNK_SIZE,
+ NULL, 0,
+ NULL, 0,
+ mtd->ecc_strength);
+ }
+
+ if (flips > 0) {
+ max_bitflips = max_t(unsigned int,
+ max_bitflips, flips);
+ corrected += flips;
+ continue;
+ }
+
failed++;
continue;
}
@@ -772,8 +812,6 @@ static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
nand->oob_poi[0] = nand_info->oob_buf[0];
- memcpy(buf, nand_info->data_buf, mtd->writesize);
-
ret = 0;
rtn:
mxs_nand_return_dma_descs(nand_info);
--
2.6.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions
2015-12-21 14:42 [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions Markus Pargmann
2015-12-21 14:42 ` [PATCH 2/2] mtd: gpmi: Add erased page bitflip correction Markus Pargmann
@ 2016-01-04 9:44 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2016-01-04 9:44 UTC (permalink / raw)
To: Markus Pargmann; +Cc: barebox
On Mon, Dec 21, 2015 at 03:42:00PM +0100, Markus Pargmann wrote:
> This adds the bitflip check helper functions from the kernel. They are
> used to check for bitflips in erased pages and correct them in the
> buffer so that UBI can work with it. Unfortunately most nand controllers
> do not have ECC for erased pages and don't do this on their own.
>
> Signed-off-by: Markus Pargmann <mpa@pengutronix.de>
> ---
> drivers/mtd/nand/nand_base.c | 128 +++++++++++++++++++++++++++++++++++++++++++
> include/linux/mtd/nand.h | 5 ++
> 2 files changed, 133 insertions(+)
Applied, thanks
Sascha
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-01-04 9:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-21 14:42 [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions Markus Pargmann
2015-12-21 14:42 ` [PATCH 2/2] mtd: gpmi: Add erased page bitflip correction Markus Pargmann
2016-01-04 9:44 ` [PATCH 1/2] mtd: nand: Add erased page bitflip check helper functions Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox