[PATCH 3/5] mtd: docg3: correct BCH errors in the buffer holding the affected byte
From: James Hilliard
Date: Sat Dec 05 2026 - 00:49:51 EST
A full-page read can report a corrected error in the spare area while
returning the wrong byte. The BCH codeword covers 512 data bytes and
eight spare bytes, but all corrections are applied relative to the
separate data buffer. A spare correction therefore writes past the
page data instead of fixing the spare byte.
Apply each correction to the appropriate buffer before copying the
requested spare bytes. Leave the stored BCH parity outside the data
correction range.
Fixes: d13d19ece39f ("mtd: docg3: add ECC correction code")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/devices/docg3.c | 48 ++++++++++++++++++++++++---------------------
1 file changed, 26 insertions(+), 22 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 5b03a596e100..5c1d667c8f8b 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -620,16 +620,14 @@ static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
/**
* doc_ecc_bch_fix_data - Fix if need be read data from flash
* @docg3: the device
- * @buf: the buffer of read data (512 + 7 + 1 bytes)
+ * @buf: the main data of the page
+ * @oob: the physical spare area of the page
* @hwecc: the hardware calculated ECC.
* It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
* area data, and calc_ecc the ECC calculated by the hardware generator.
*
- * Checks if the received data matches the ECC, and if an error is detected,
- * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
- * understands the (data, ecc, syndroms) in an inverted order in comparison to
- * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
- * bit6 and bit 1, ...) for all ECC data.
+ * BCH protects the main data and the first eight spare bytes. Apply each
+ * correction to the buffer containing the affected byte.
*
* The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
* algorithm is used to decode this. However the hw operates on page
@@ -637,10 +635,10 @@ static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
* requiring that the bits be reversed on the result. Thanks to Ivan
* Djelic for his analysis.
*
- * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
- * errors were detected and cannot be fixed.
+ * Return: The number of bit errors reported by the decoder, or -EBADMSG
+ * if the errors cannot be corrected.
*/
-static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
+static int doc_ecc_bch_fix_data(struct docg3 *docg3, u8 *buf, u8 *oob, u8 *hwecc)
{
u8 ecc[DOC_ECC_BCH_SIZE];
int errorpos[DOC_ECC_BCH_T], i, numerrs;
@@ -654,12 +652,15 @@ static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
if (numerrs < 0)
goto out;
- for (i = 0; i < numerrs; i++)
- errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
- for (i = 0; i < numerrs; i++)
- if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
- /* error is located in data, correct it */
- change_bit(errorpos[i], buf);
+ for (i = 0; i < numerrs; i++) {
+ unsigned int byte = errorpos[i] / 8;
+ u8 mask = BIT(7 - (errorpos[i] % 8));
+
+ if (byte < DOC_LAYOUT_PAGE_SIZE)
+ buf[byte] ^= mask;
+ else if (byte < DOC_ECC_BCH_COVERED_BYTES)
+ oob[byte - DOC_LAYOUT_PAGE_SIZE] ^= mask;
+ }
out:
doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
return numerrs;
@@ -870,6 +871,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
u8 *oobbuf = ops->oobbuf;
u8 *buf = ops->datbuf;
size_t len, ooblen, nbdata, nboob;
+ u8 oob[DOC_LAYOUT_OOB_SIZE] __aligned(2);
u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
struct mtd_ecc_stats old_stats;
int max_bitflips = 0;
@@ -917,20 +919,20 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
doc_read_page_getbytes(docg3,
DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
NULL, 0, (skip + nbdata) % 2);
- ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0);
+ ret = doc_read_page_getbytes(docg3, nboob, oob, 0, 0);
if (ret < nboob)
goto err_in_read;
doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
- NULL, 0, nboob % 2);
+ oob + nboob, 0, nboob % 2);
doc_get_bch_hw_ecc(docg3, hwecc);
eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
if (nboob >= DOC_LAYOUT_OOB_SIZE) {
- doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf);
- doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
- doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8);
- doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
+ doc_dbg("OOB - INFO: %*phC\n", 7, oob);
+ doc_dbg("OOB - HAMMING: %02x\n", oob[7]);
+ doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oob + 8);
+ doc_dbg("OOB - UNUSED: %02x\n", oob[15]);
}
doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc);
@@ -944,7 +946,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
(eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
(ops->mode != MTD_OPS_RAW) &&
(nbdata == DOC_LAYOUT_PAGE_SIZE)) {
- ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
+ ret = doc_ecc_bch_fix_data(docg3, buf, oob, hwecc);
if (ret < 0) {
mtd->ecc_stats.failed++;
ret = -EBADMSG;
@@ -956,6 +958,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
}
}
+ if (nboob)
+ memcpy(oobbuf, oob, nboob);
doc_read_page_finish(docg3);
ops->retlen += nbdata;
ops->oobretlen += nboob;
--
2.53.0