From: James Hilliard <james.hilliard1@gmail.com>
To: Robert Jarzmik <robert.jarzmik@free.fr>,
Miquel Raynal <miquel.raynal@bootlin.com>,
Richard Weinberger <richard@nod.at>,
Vignesh Raghavendra <vigneshr@ti.com>,
Ivan Djelic <ivan.djelic@parrot.com>,
David Woodhouse <David.Woodhouse@intel.com>,
Mike Dunn <mikedunn@newsguy.com>,
Brian Norris <computersforpeace@gmail.com>,
Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
James Hilliard <james.hilliard1@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH 3/5] mtd: docg3: correct BCH errors in the buffer holding the affected byte
Date: Fri, 04 Dec 2026 22:49:29 -0700 [thread overview]
Message-ID: <20261204-submit-docg3-oob-fixes-v1-v1-3-ea0bf6c535ca@gmail.com> (raw)
In-Reply-To: <20261204-submit-docg3-oob-fixes-v1-v1-0-ea0bf6c535ca@gmail.com>
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@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
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
next prev parent reply other threads:[~2026-12-05 5:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 5:49 [PATCH 0/5] mtd: docg3: fix spare-area transfers and ECC reporting James Hilliard
2026-12-05 5:49 ` [PATCH 1/5] mtd: docg3: check whole-page OOB writes in the selected mode James Hilliard
2026-12-05 5:49 ` [PATCH 2/5] mtd: docg3: invalidate staged OOB on page write and block erase James Hilliard
2026-12-05 5:49 ` James Hilliard [this message]
2026-12-05 5:49 ` [PATCH 4/5] mtd: docg3: return the requested spare bytes in the selected OOB mode James Hilliard
2026-12-05 5:49 ` [PATCH 5/5] mtd: docg3: report the maximum bitflip count across read pages James Hilliard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204-submit-docg3-oob-fixes-v1-v1-3-ea0bf6c535ca@gmail.com \
--to=james.hilliard1@gmail.com \
--cc=David.Woodhouse@intel.com \
--cc=artem.bityutskiy@linux.intel.com \
--cc=computersforpeace@gmail.com \
--cc=ivan.djelic@parrot.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=mikedunn@newsguy.com \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=robert.jarzmik@free.fr \
--cc=stable@vger.kernel.org \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®