* [PATCH 0/5] mtd: docg3: fix spare-area transfers and ECC reporting
@ 2026-12-05 5:49 ` James Hilliard
0 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
DiskOnChip G3 exposes eight free bytes in its 16-byte spare area. An
AUTO read can return ECC bytes instead of those free bytes; an OOB-only
write of all eight free bytes is rejected. A later page write can also
pick up stale spare bytes instead of the bytes supplied by its caller.
Use the selected OOB layout for reads and writes. Retire staged bytes
after a successful program of their page or erase of their block pair.
On full-page reads, correct BCH errors in the buffer containing the
affected byte. Report the highest bitflip count across all pages read.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
James Hilliard (5):
mtd: docg3: check whole-page OOB writes in the selected mode
mtd: docg3: invalidate staged OOB on page write and block erase
mtd: docg3: correct BCH errors in the buffer holding the affected byte
mtd: docg3: return the requested spare bytes in the selected OOB mode
mtd: docg3: report the maximum bitflip count across read pages
drivers/mtd/devices/docg3.c | 266 +++++++++++++++++++++-----------------------
1 file changed, 128 insertions(+), 138 deletions(-)
---
base-commit: 4e5457fcbc14d21e7c004c3159e78fc63838029e
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next
change-id: 20261204-submit-docg3-oob-fixes-v1-3ac1e192f872
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 13+ messages in thread* [PATCH 1/5] mtd: docg3: check whole-page OOB writes in the selected mode
2026-12-05 5:49 ` James Hilliard
@ 2026-12-05 5:49 ` James Hilliard
-1 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
DiskOnChip G3 has eight free bytes in its 16-byte spare area. An
OOB-only AUTO write of all eight bytes fails. A 16-byte request
succeeds, but the driver stages and reports only the first eight bytes.
Require one page's worth of spare bytes in the selected mode for a
staged write. MEMWRITE supplies the data and OOB lengths separately:
two data pages can have spare bytes for only the first. Require a data
page for each supplied spare area, not a spare area for every data
page. Reject partial spare areas and nonzero spare offsets instead of
using the offset to index the caller's buffer.
Fixes: fb50b58e488d ("mtd: docg3: add write functions")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 117 +++++++++++++-------------------------------
1 file changed, 35 insertions(+), 82 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 1f0b1b0f4a83..9617b1ebba0f 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1288,30 +1288,6 @@ static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
return ret;
}
-/**
- * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
- * @ops: the oob operations
- *
- * Returns 0 or 1 if success, -EINVAL if invalid oob mode
- */
-static int doc_guess_autoecc(struct mtd_oob_ops *ops)
-{
- int autoecc;
-
- switch (ops->mode) {
- case MTD_OPS_PLACE_OOB:
- case MTD_OPS_AUTO_OOB:
- autoecc = 1;
- break;
- case MTD_OPS_RAW:
- autoecc = 0;
- break;
- default:
- autoecc = -EINVAL;
- }
- return autoecc;
-}
-
/**
* doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
* @dst: the target 16 bytes OOB buffer
@@ -1324,9 +1300,26 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
}
+static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops *ops)
+{
+ u32 avail = mtd_oobavail(mtd, ops);
+
+ if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB &&
+ ops->mode != MTD_OPS_RAW)
+ return false;
+ if (to % DOC_LAYOUT_PAGE_SIZE || ops->len % DOC_LAYOUT_PAGE_SIZE ||
+ (ops->ooblen && (ops->ooboffs || ops->ooblen % avail)))
+ return false;
+
+ if (!ops->len)
+ return ops->ooblen == avail;
+
+ return ops->ooblen / avail <= ops->len / DOC_LAYOUT_PAGE_SIZE;
+}
+
/**
* doc_backup_oob - Backup OOB into docg3 structure
- * @docg3: the device
+ * @mtd: flash device
* @to: the page offset in the chip
* @ops: the OOB size and buffer
*
@@ -1338,30 +1331,18 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
*
* The only reliable way would be for userland to call doc_write_oob() with both
* the page data _and_ the OOB area.
- *
- * Returns 0 if success, -EINVAL if ops content invalid
*/
-static int doc_backup_oob(struct docg3 *docg3, loff_t to,
- struct mtd_oob_ops *ops)
+static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
{
- int ooblen = ops->ooblen, autoecc;
-
- if (ooblen != DOC_LAYOUT_OOB_SIZE)
- return -EINVAL;
- autoecc = doc_guess_autoecc(ops);
- if (autoecc < 0)
- return autoecc;
+ struct docg3 *docg3 = mtd->priv;
docg3->oob_write_ofs = to;
- docg3->oob_autoecc = autoecc;
- if (ops->mode == MTD_OPS_AUTO_OOB) {
+ docg3->oob_autoecc = ops->mode != MTD_OPS_RAW;
+ if (ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
- ops->oobretlen = 8;
- } else {
+ else
memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
- ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
- }
- return 0;
+ ops->oobretlen = ops->ooblen;
}
/**
@@ -1372,12 +1353,12 @@ static int doc_backup_oob(struct docg3 *docg3, loff_t to,
* @ops: the mtd oob structure
*
* Either write OOB data into a temporary buffer, for the subsequent write
- * page. The provided OOB should be 16 bytes long. If a data buffer is provided
- * as well, issue the page write.
+ * page. An OOB-only write supplies one page in the selected mode. If a data
+ * buffer is provided as well, issue the page write.
* Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
* still be filled in if asked for).
*
- * Returns 0 is successful, EINVAL if length is not 14 bytes
+ * Returns 0 on success or a negative error code.
*/
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
struct mtd_oob_ops *ops)
@@ -1386,52 +1367,24 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
int ret, autoecc, oobdelta;
u8 *oobbuf = ops->oobbuf;
u8 *buf = ops->datbuf;
- size_t len, ooblen;
+ size_t len = ops->len, ooblen = ops->ooblen;
u8 oob[DOC_LAYOUT_OOB_SIZE];
- if (buf)
- len = ops->len;
- else
- len = 0;
- if (oobbuf)
- ooblen = ops->ooblen;
- else
- ooblen = 0;
-
- if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
- oobbuf += ops->ooboffs;
-
doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
ofs, ops->mode, buf, len, oobbuf, ooblen);
- switch (ops->mode) {
- case MTD_OPS_PLACE_OOB:
- case MTD_OPS_RAW:
- oobdelta = mtd->oobsize;
- break;
- case MTD_OPS_AUTO_OOB:
- oobdelta = mtd->oobavail;
- break;
- default:
- return -EINVAL;
- }
- if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
- (ofs % DOC_LAYOUT_PAGE_SIZE))
- return -EINVAL;
- if (len && ooblen &&
- (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
+ if (!doc_oob_write_valid(mtd, ofs, ops))
return -EINVAL;
ops->oobretlen = 0;
ops->retlen = 0;
ret = 0;
- if (len == 0 && ooblen == 0)
- return -EINVAL;
- if (len == 0 && ooblen > 0)
- return doc_backup_oob(docg3, ofs, ops);
+ if (!len) {
+ doc_backup_oob(mtd, ofs, ops);
+ return 0;
+ }
- autoecc = doc_guess_autoecc(ops);
- if (autoecc < 0)
- return autoecc;
+ oobdelta = mtd_oobavail(mtd, ops);
+ autoecc = ops->mode != MTD_OPS_RAW;
mutex_lock(&docg3->cascade->lock);
while (!ret && len > 0) {
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 1/5] mtd: docg3: check whole-page OOB writes in the selected mode
@ 2026-12-05 5:49 ` James Hilliard
0 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
DiskOnChip G3 has eight free bytes in its 16-byte spare area. An
OOB-only AUTO write of all eight bytes fails. A 16-byte request
succeeds, but the driver stages and reports only the first eight bytes.
Require one page's worth of spare bytes in the selected mode for a
staged write. MEMWRITE supplies the data and OOB lengths separately:
two data pages can have spare bytes for only the first. Require a data
page for each supplied spare area, not a spare area for every data
page. Reject partial spare areas and nonzero spare offsets instead of
using the offset to index the caller's buffer.
Fixes: fb50b58e488d ("mtd: docg3: add write functions")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 117 +++++++++++++-------------------------------
1 file changed, 35 insertions(+), 82 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 1f0b1b0f4a83..9617b1ebba0f 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1288,30 +1288,6 @@ static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
return ret;
}
-/**
- * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
- * @ops: the oob operations
- *
- * Returns 0 or 1 if success, -EINVAL if invalid oob mode
- */
-static int doc_guess_autoecc(struct mtd_oob_ops *ops)
-{
- int autoecc;
-
- switch (ops->mode) {
- case MTD_OPS_PLACE_OOB:
- case MTD_OPS_AUTO_OOB:
- autoecc = 1;
- break;
- case MTD_OPS_RAW:
- autoecc = 0;
- break;
- default:
- autoecc = -EINVAL;
- }
- return autoecc;
-}
-
/**
* doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
* @dst: the target 16 bytes OOB buffer
@@ -1324,9 +1300,26 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
}
+static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops *ops)
+{
+ u32 avail = mtd_oobavail(mtd, ops);
+
+ if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB &&
+ ops->mode != MTD_OPS_RAW)
+ return false;
+ if (to % DOC_LAYOUT_PAGE_SIZE || ops->len % DOC_LAYOUT_PAGE_SIZE ||
+ (ops->ooblen && (ops->ooboffs || ops->ooblen % avail)))
+ return false;
+
+ if (!ops->len)
+ return ops->ooblen == avail;
+
+ return ops->ooblen / avail <= ops->len / DOC_LAYOUT_PAGE_SIZE;
+}
+
/**
* doc_backup_oob - Backup OOB into docg3 structure
- * @docg3: the device
+ * @mtd: flash device
* @to: the page offset in the chip
* @ops: the OOB size and buffer
*
@@ -1338,30 +1331,18 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
*
* The only reliable way would be for userland to call doc_write_oob() with both
* the page data _and_ the OOB area.
- *
- * Returns 0 if success, -EINVAL if ops content invalid
*/
-static int doc_backup_oob(struct docg3 *docg3, loff_t to,
- struct mtd_oob_ops *ops)
+static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
{
- int ooblen = ops->ooblen, autoecc;
-
- if (ooblen != DOC_LAYOUT_OOB_SIZE)
- return -EINVAL;
- autoecc = doc_guess_autoecc(ops);
- if (autoecc < 0)
- return autoecc;
+ struct docg3 *docg3 = mtd->priv;
docg3->oob_write_ofs = to;
- docg3->oob_autoecc = autoecc;
- if (ops->mode == MTD_OPS_AUTO_OOB) {
+ docg3->oob_autoecc = ops->mode != MTD_OPS_RAW;
+ if (ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
- ops->oobretlen = 8;
- } else {
+ else
memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
- ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
- }
- return 0;
+ ops->oobretlen = ops->ooblen;
}
/**
@@ -1372,12 +1353,12 @@ static int doc_backup_oob(struct docg3 *docg3, loff_t to,
* @ops: the mtd oob structure
*
* Either write OOB data into a temporary buffer, for the subsequent write
- * page. The provided OOB should be 16 bytes long. If a data buffer is provided
- * as well, issue the page write.
+ * page. An OOB-only write supplies one page in the selected mode. If a data
+ * buffer is provided as well, issue the page write.
* Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
* still be filled in if asked for).
*
- * Returns 0 is successful, EINVAL if length is not 14 bytes
+ * Returns 0 on success or a negative error code.
*/
static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
struct mtd_oob_ops *ops)
@@ -1386,52 +1367,24 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
int ret, autoecc, oobdelta;
u8 *oobbuf = ops->oobbuf;
u8 *buf = ops->datbuf;
- size_t len, ooblen;
+ size_t len = ops->len, ooblen = ops->ooblen;
u8 oob[DOC_LAYOUT_OOB_SIZE];
- if (buf)
- len = ops->len;
- else
- len = 0;
- if (oobbuf)
- ooblen = ops->ooblen;
- else
- ooblen = 0;
-
- if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
- oobbuf += ops->ooboffs;
-
doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
ofs, ops->mode, buf, len, oobbuf, ooblen);
- switch (ops->mode) {
- case MTD_OPS_PLACE_OOB:
- case MTD_OPS_RAW:
- oobdelta = mtd->oobsize;
- break;
- case MTD_OPS_AUTO_OOB:
- oobdelta = mtd->oobavail;
- break;
- default:
- return -EINVAL;
- }
- if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
- (ofs % DOC_LAYOUT_PAGE_SIZE))
- return -EINVAL;
- if (len && ooblen &&
- (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
+ if (!doc_oob_write_valid(mtd, ofs, ops))
return -EINVAL;
ops->oobretlen = 0;
ops->retlen = 0;
ret = 0;
- if (len == 0 && ooblen == 0)
- return -EINVAL;
- if (len == 0 && ooblen > 0)
- return doc_backup_oob(docg3, ofs, ops);
+ if (!len) {
+ doc_backup_oob(mtd, ofs, ops);
+ return 0;
+ }
- autoecc = doc_guess_autoecc(ops);
- if (autoecc < 0)
- return autoecc;
+ oobdelta = mtd_oobavail(mtd, ops);
+ autoecc = ops->mode != MTD_OPS_RAW;
mutex_lock(&docg3->cascade->lock);
while (!ret && len > 0) {
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 2/5] mtd: docg3: invalidate staged OOB on page write and block erase
2026-12-05 5:49 ` James Hilliard
@ 2026-12-05 5:49 ` James Hilliard
-1 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A combined write at offset zero can program a zero-filled spare area
instead of the caller's bytes because the staging slot initially looks
valid. The slot can also outlive a page program or a block erase, so a
later data-only write can reuse spare bytes meant for an earlier write.
Start with an empty slot and give explicit OOB precedence. Protect the
staged address, bytes and ECC mode with the same lock as programming.
Use them only for a data-only write to the matching page. Retire the
slot when that page is programmed or its physical block pair is erased.
Keep the slot on a failed program and count only successfully
programmed pages in the returned data and OOB lengths.
Fixes: fb50b58e488d ("mtd: docg3: add write functions")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 55 ++++++++++++++++++++++++++++-----------------
1 file changed, 34 insertions(+), 21 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 9617b1ebba0f..5b03a596e100 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1186,6 +1186,7 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
struct docg3 *docg3 = mtd->priv;
uint64_t len;
int block0, block1, page, ret = 0, ofs = 0;
+ int staged_block = -1;
doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
@@ -1194,13 +1195,20 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
if (info->addr + info->len > mtd->size || page || ofs)
return -EINVAL;
+ mutex_lock(&docg3->cascade->lock);
+ if (docg3->oob_write_ofs >= 0)
+ calc_block_sector(docg3->oob_write_ofs, &staged_block, &block1,
+ &page, &ofs, docg3->reliable);
calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
docg3->reliable);
- mutex_lock(&docg3->cascade->lock);
doc_set_device_id(docg3, docg3->device_id);
doc_set_reliable_mode(docg3);
- for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
+ for (len = info->len; len > 0; len -= mtd->erasesize) {
ret = doc_erase_block(docg3, block0, block1);
+ if (ret)
+ break;
+ if (block0 == staged_block)
+ docg3->oob_write_ofs = -1;
block0 += 2;
block1 += 2;
}
@@ -1323,25 +1331,23 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops
* @to: the page offset in the chip
* @ops: the OOB size and buffer
*
- * As the docg3 should write a page with its OOB in one pass, and some userland
- * applications do write_oob() to setup the OOB and then write(), store the OOB
- * into a temporary storage. This is very dangerous, as 2 concurrent
- * applications could store an OOB, and then write their pages (which will
- * result into one having its OOB corrupted).
- *
- * The only reliable way would be for userland to call doc_write_oob() with both
- * the page data _and_ the OOB area.
+ * Some applications supply spare bytes before writing the page data.
+ * Another spare-only write can replace those bytes between the two calls.
+ * Callers that need the data and OOB to stay together can supply both in
+ * one write.
*/
static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
{
struct docg3 *docg3 = mtd->priv;
+ mutex_lock(&docg3->cascade->lock);
docg3->oob_write_ofs = to;
docg3->oob_autoecc = ops->mode != MTD_OPS_RAW;
if (ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
else
memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
+ mutex_unlock(&docg3->cascade->lock);
ops->oobretlen = ops->ooblen;
}
@@ -1352,11 +1358,9 @@ static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *
* size
* @ops: the mtd oob structure
*
- * Either write OOB data into a temporary buffer, for the subsequent write
- * page. An OOB-only write supplies one page in the selected mode. If a data
- * buffer is provided as well, issue the page write.
- * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
- * still be filled in if asked for).
+ * An OOB-only write stages one page's spare bytes. A data-only write uses
+ * staged bytes for the matching page, or a zero-filled spare area otherwise.
+ * A combined write uses the spare bytes supplied with the data.
*
* Returns 0 on success or a negative error code.
*/
@@ -1387,15 +1391,23 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
autoecc = ops->mode != MTD_OPS_RAW;
mutex_lock(&docg3->cascade->lock);
- while (!ret && len > 0) {
+ while (len > 0) {
+ int page_autoecc = autoecc;
+
memset(oob, 0, sizeof(oob));
- if (ofs == docg3->oob_write_ofs)
- memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
- else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
+ if (ooblen && ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(oob, oobbuf);
- else if (ooblen > 0)
+ else if (ooblen)
memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
- ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
+ else if (ofs == docg3->oob_write_ofs) {
+ memcpy(oob, docg3->oob_write_buf, sizeof(oob));
+ page_autoecc = docg3->oob_autoecc;
+ }
+ ret = doc_write_page(docg3, ofs, buf, oob, page_autoecc);
+ if (ret)
+ break;
+ if (ofs == docg3->oob_write_ofs)
+ docg3->oob_write_ofs = -1;
ofs += DOC_LAYOUT_PAGE_SIZE;
len -= DOC_LAYOUT_PAGE_SIZE;
@@ -1730,6 +1742,7 @@ static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
if (docg3->reliable == 2)
mtd->erasesize /= 2;
mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
+ docg3->oob_write_ofs = -1;
mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
mtd->_erase = doc_erase;
mtd->_read_oob = doc_read_oob;
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 2/5] mtd: docg3: invalidate staged OOB on page write and block erase
@ 2026-12-05 5:49 ` James Hilliard
0 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A combined write at offset zero can program a zero-filled spare area
instead of the caller's bytes because the staging slot initially looks
valid. The slot can also outlive a page program or a block erase, so a
later data-only write can reuse spare bytes meant for an earlier write.
Start with an empty slot and give explicit OOB precedence. Protect the
staged address, bytes and ECC mode with the same lock as programming.
Use them only for a data-only write to the matching page. Retire the
slot when that page is programmed or its physical block pair is erased.
Keep the slot on a failed program and count only successfully
programmed pages in the returned data and OOB lengths.
Fixes: fb50b58e488d ("mtd: docg3: add write functions")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 55 ++++++++++++++++++++++++++++-----------------
1 file changed, 34 insertions(+), 21 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 9617b1ebba0f..5b03a596e100 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1186,6 +1186,7 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
struct docg3 *docg3 = mtd->priv;
uint64_t len;
int block0, block1, page, ret = 0, ofs = 0;
+ int staged_block = -1;
doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
@@ -1194,13 +1195,20 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
if (info->addr + info->len > mtd->size || page || ofs)
return -EINVAL;
+ mutex_lock(&docg3->cascade->lock);
+ if (docg3->oob_write_ofs >= 0)
+ calc_block_sector(docg3->oob_write_ofs, &staged_block, &block1,
+ &page, &ofs, docg3->reliable);
calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
docg3->reliable);
- mutex_lock(&docg3->cascade->lock);
doc_set_device_id(docg3, docg3->device_id);
doc_set_reliable_mode(docg3);
- for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
+ for (len = info->len; len > 0; len -= mtd->erasesize) {
ret = doc_erase_block(docg3, block0, block1);
+ if (ret)
+ break;
+ if (block0 == staged_block)
+ docg3->oob_write_ofs = -1;
block0 += 2;
block1 += 2;
}
@@ -1323,25 +1331,23 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops
* @to: the page offset in the chip
* @ops: the OOB size and buffer
*
- * As the docg3 should write a page with its OOB in one pass, and some userland
- * applications do write_oob() to setup the OOB and then write(), store the OOB
- * into a temporary storage. This is very dangerous, as 2 concurrent
- * applications could store an OOB, and then write their pages (which will
- * result into one having its OOB corrupted).
- *
- * The only reliable way would be for userland to call doc_write_oob() with both
- * the page data _and_ the OOB area.
+ * Some applications supply spare bytes before writing the page data.
+ * Another spare-only write can replace those bytes between the two calls.
+ * Callers that need the data and OOB to stay together can supply both in
+ * one write.
*/
static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
{
struct docg3 *docg3 = mtd->priv;
+ mutex_lock(&docg3->cascade->lock);
docg3->oob_write_ofs = to;
docg3->oob_autoecc = ops->mode != MTD_OPS_RAW;
if (ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
else
memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
+ mutex_unlock(&docg3->cascade->lock);
ops->oobretlen = ops->ooblen;
}
@@ -1352,11 +1358,9 @@ static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *
* size
* @ops: the mtd oob structure
*
- * Either write OOB data into a temporary buffer, for the subsequent write
- * page. An OOB-only write supplies one page in the selected mode. If a data
- * buffer is provided as well, issue the page write.
- * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
- * still be filled in if asked for).
+ * An OOB-only write stages one page's spare bytes. A data-only write uses
+ * staged bytes for the matching page, or a zero-filled spare area otherwise.
+ * A combined write uses the spare bytes supplied with the data.
*
* Returns 0 on success or a negative error code.
*/
@@ -1387,15 +1391,23 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
autoecc = ops->mode != MTD_OPS_RAW;
mutex_lock(&docg3->cascade->lock);
- while (!ret && len > 0) {
+ while (len > 0) {
+ int page_autoecc = autoecc;
+
memset(oob, 0, sizeof(oob));
- if (ofs == docg3->oob_write_ofs)
- memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
- else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
+ if (ooblen && ops->mode == MTD_OPS_AUTO_OOB)
doc_fill_autooob(oob, oobbuf);
- else if (ooblen > 0)
+ else if (ooblen)
memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
- ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
+ else if (ofs == docg3->oob_write_ofs) {
+ memcpy(oob, docg3->oob_write_buf, sizeof(oob));
+ page_autoecc = docg3->oob_autoecc;
+ }
+ ret = doc_write_page(docg3, ofs, buf, oob, page_autoecc);
+ if (ret)
+ break;
+ if (ofs == docg3->oob_write_ofs)
+ docg3->oob_write_ofs = -1;
ofs += DOC_LAYOUT_PAGE_SIZE;
len -= DOC_LAYOUT_PAGE_SIZE;
@@ -1730,6 +1742,7 @@ static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
if (docg3->reliable == 2)
mtd->erasesize /= 2;
mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
+ docg3->oob_write_ofs = -1;
mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
mtd->_erase = doc_erase;
mtd->_read_oob = doc_read_oob;
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH 2/5] mtd: docg3: invalidate staged OOB on page write and block erase
2026-12-05 5:49 ` James Hilliard
(?)
@ 2026-12-05 6:03 ` sashiko-bot
-1 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05 6:03 UTC (permalink / raw)
To: James Hilliard, Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mtd: docg3: combined write consumes staged OOB on trailing
pages
- [Low] mtd: docg3: staged OOB survives failed erase in doc_erase()
--
commit 30ea8ea65f185fd87c50880ce4ae46feb2bd99fd
Author: James Hilliard <james.hilliard1@gmail.com>
mtd: docg3: invalidate staged OOB on page write and block erase
This commit starts the staged OOB slot empty, protects it with the
cascade lock, gives explicit OOB precedence in doc_write_oob(), and
clears the slot after the matching page is programmed or its block
pair is erased.
> diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
> index 9617b1ebba0f6..5b03a596e1007 100644
> --- a/drivers/mtd/devices/docg3.c
> +++ b/drivers/mtd/devices/docg3.c
[ ... ]
> @@ -1194,13 +1195,20 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
> if (info->addr + info->len > mtd->size || page || ofs)
> return -EINVAL;
>
> + mutex_lock(&docg3->cascade->lock);
> + if (docg3->oob_write_ofs >= 0)
> + calc_block_sector(docg3->oob_write_ofs, &staged_block, &block1,
> + &page, &ofs, docg3->reliable);
> calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
> docg3->reliable);
> - mutex_lock(&docg3->cascade->lock);
> doc_set_device_id(docg3, docg3->device_id);
> doc_set_reliable_mode(docg3);
> - for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
> + for (len = info->len; len > 0; len -= mtd->erasesize) {
> ret = doc_erase_block(docg3, block0, block1);
> + if (ret)
> + break;
> + if (block0 == staged_block)
> + docg3->oob_write_ofs = -1;
> block0 += 2;
> block1 += 2;
> }
[Severity: Low]
Can docg3->oob_write_ofs stay armed after doc_erase_block() has already
issued an erase to staged_block?
doc_erase_block() sends DOC_CMD_ERASECYCLE2 before waiting for status, so
a timeout (-EAGAIN) or single-plane failure (-EIO) can still leave the
block erased. Breaking before clearing oob_write_ofs lets a later
data-only write to that page consume stale pre-erase OOB bytes.
[ ... ]
> @@ -1387,15 +1391,23 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
> autoecc = ops->mode != MTD_OPS_RAW;
>
> mutex_lock(&docg3->cascade->lock);
> - while (!ret && len > 0) {
> + while (len > 0) {
> + int page_autoecc = autoecc;
> +
> memset(oob, 0, sizeof(oob));
> - if (ofs == docg3->oob_write_ofs)
> - memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
> - else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
> + if (ooblen && ops->mode == MTD_OPS_AUTO_OOB)
> doc_fill_autooob(oob, oobbuf);
> - else if (ooblen > 0)
> + else if (ooblen)
> memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
> - ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
> + else if (ofs == docg3->oob_write_ofs) {
> + memcpy(oob, docg3->oob_write_buf, sizeof(oob));
> + page_autoecc = docg3->oob_autoecc;
> + }
> + ret = doc_write_page(docg3, ofs, buf, oob, page_autoecc);
> + if (ret)
> + break;
> + if (ofs == docg3->oob_write_ofs)
> + docg3->oob_write_ofs = -1;
>
> ofs += DOC_LAYOUT_PAGE_SIZE;
> len -= DOC_LAYOUT_PAGE_SIZE;
[Severity: Medium]
Should this branch check !ops->ooblen instead of the loop-decremented
ooblen?
doc_oob_write_valid() allows combined writes where ops->ooblen covers
fewer pages than ops->len, as mtdchar_write_ioctl() can submit. Once
ooblen reaches zero mid-loop, a trailing page matching a staged
docg3->oob_write_ofs enters this branch.
That writes stale staged OOB bytes instead of zeros and overrides
page_autoecc with docg3->oob_autoecc, which can disable hardware ECC on an
ECC-protected write. Guarding this branch with !ops->ooblen prevents
that while still clearing the slot once the page is programmed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-submit-docg3-oob-fixes-v1-v1-0-ea0bf6c535ca@gmail.com?part=2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH 3/5] mtd: docg3: correct BCH errors in the buffer holding the affected byte
2026-12-05 5:49 ` James Hilliard
@ 2026-12-05 5:49 ` James Hilliard
-1 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
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
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 3/5] mtd: docg3: correct BCH errors in the buffer holding the affected byte
@ 2026-12-05 5:49 ` James Hilliard
0 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
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
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 4/5] mtd: docg3: return the requested spare bytes in the selected OOB mode
2026-12-05 5:49 ` James Hilliard
@ 2026-12-05 5:49 ` James Hilliard
-1 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
An AUTO read of eight bytes should return the seven page-information
bytes and the final free byte. DiskOnChip G3 instead requires reads in
multiples of 16 and copies physical spare bytes, including ECC. PLACE
reads also apply the spare offset to the destination instead of the page.
Read the physical spare area into a local buffer. Keep the chip's read
and drain phases, including the NOPs issued by an empty phase. Copy
AUTO bytes from the free regions and PLACE or RAW bytes from the
requested physical offset. Apply the offset only to the first page and
reject unknown modes before accessing the chip.
Fixes: efa2ca73a7bc ("mtd: Add DiskOnChip G3 support")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 52 ++++++++++++++++++++++++++++++---------------
1 file changed, 35 insertions(+), 17 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 5c1d667c8f8b..c00693aba84a 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -852,6 +852,12 @@ static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
*ofs = 0;
}
+static bool doc_oob_mode_valid(unsigned int mode)
+{
+ return mode == MTD_OPS_PLACE_OOB || mode == MTD_OPS_AUTO_OOB ||
+ mode == MTD_OPS_RAW;
+}
+
/**
* doc_read_oob - Read out of band bytes from flash
* @mtd: the device
@@ -871,6 +877,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;
+ unsigned int ooboffs = ops->ooboffs, oobavail = mtd_oobavail(mtd, ops);
u8 oob[DOC_LAYOUT_OOB_SIZE] __aligned(2);
u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
struct mtd_ecc_stats old_stats;
@@ -885,12 +892,9 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
else
ooblen = 0;
- if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
- oobbuf += ops->ooboffs;
-
doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
from, ops->mode, buf, len, oobbuf, ooblen);
- if (ooblen % DOC_LAYOUT_OOB_SIZE)
+ if (!doc_oob_mode_valid(ops->mode) || (ooblen && ooboffs >= oobavail))
return -EINVAL;
ops->oobretlen = 0;
@@ -900,10 +904,12 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
mutex_lock(&docg3->cascade->lock);
old_stats = mtd->ecc_stats;
while (ret >= 0 && (len > 0 || ooblen > 0)) {
+ unsigned int spare_len;
+
calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
docg3->reliable);
nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
- nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
+ nboob = min_t(size_t, ooblen, oobavail - ooboffs);
ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
if (ret < 0)
goto out;
@@ -919,16 +925,16 @@ 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, oob, 0, 0);
- if (ret < nboob)
- goto err_in_read;
- doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
- oob + nboob, 0, nboob % 2);
+ /* Both spare-area phases issue NOPs, even if one is empty. */
+ spare_len = nboob ? sizeof(oob) : 0;
+ doc_read_page_getbytes(docg3, spare_len, oob, 0, 0);
+ doc_read_page_getbytes(docg3, sizeof(oob) - spare_len,
+ oob + spare_len, 0, 0);
doc_get_bch_hw_ecc(docg3, hwecc);
eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
- if (nboob >= DOC_LAYOUT_OOB_SIZE) {
+ if (nboob) {
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);
@@ -958,15 +964,28 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
}
}
- if (nboob)
- memcpy(oobbuf, oob, nboob);
+ if (nboob && ops->mode == MTD_OPS_AUTO_OOB) {
+ int err;
+
+ err = mtd_ooblayout_get_databytes(mtd, oobbuf, oob,
+ ooboffs, nboob);
+ if (err) {
+ ret = err;
+ goto err_in_read;
+ }
+ } else if (nboob) {
+ memcpy(oobbuf, oob + ooboffs, nboob);
+ }
doc_read_page_finish(docg3);
ops->retlen += nbdata;
ops->oobretlen += nboob;
- buf += nbdata;
- oobbuf += nboob;
+ if (nbdata)
+ buf += nbdata;
+ if (nboob)
+ oobbuf += nboob;
len -= nbdata;
ooblen -= nboob;
+ ooboffs = 0;
from += DOC_LAYOUT_PAGE_SIZE;
skip = 0;
}
@@ -1316,8 +1335,7 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops
{
u32 avail = mtd_oobavail(mtd, ops);
- if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB &&
- ops->mode != MTD_OPS_RAW)
+ if (!doc_oob_mode_valid(ops->mode))
return false;
if (to % DOC_LAYOUT_PAGE_SIZE || ops->len % DOC_LAYOUT_PAGE_SIZE ||
(ops->ooblen && (ops->ooboffs || ops->ooblen % avail)))
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 4/5] mtd: docg3: return the requested spare bytes in the selected OOB mode
@ 2026-12-05 5:49 ` James Hilliard
0 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
An AUTO read of eight bytes should return the seven page-information
bytes and the final free byte. DiskOnChip G3 instead requires reads in
multiples of 16 and copies physical spare bytes, including ECC. PLACE
reads also apply the spare offset to the destination instead of the page.
Read the physical spare area into a local buffer. Keep the chip's read
and drain phases, including the NOPs issued by an empty phase. Copy
AUTO bytes from the free regions and PLACE or RAW bytes from the
requested physical offset. Apply the offset only to the first page and
reject unknown modes before accessing the chip.
Fixes: efa2ca73a7bc ("mtd: Add DiskOnChip G3 support")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 52 ++++++++++++++++++++++++++++++---------------
1 file changed, 35 insertions(+), 17 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 5c1d667c8f8b..c00693aba84a 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -852,6 +852,12 @@ static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
*ofs = 0;
}
+static bool doc_oob_mode_valid(unsigned int mode)
+{
+ return mode == MTD_OPS_PLACE_OOB || mode == MTD_OPS_AUTO_OOB ||
+ mode == MTD_OPS_RAW;
+}
+
/**
* doc_read_oob - Read out of band bytes from flash
* @mtd: the device
@@ -871,6 +877,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;
+ unsigned int ooboffs = ops->ooboffs, oobavail = mtd_oobavail(mtd, ops);
u8 oob[DOC_LAYOUT_OOB_SIZE] __aligned(2);
u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
struct mtd_ecc_stats old_stats;
@@ -885,12 +892,9 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
else
ooblen = 0;
- if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
- oobbuf += ops->ooboffs;
-
doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
from, ops->mode, buf, len, oobbuf, ooblen);
- if (ooblen % DOC_LAYOUT_OOB_SIZE)
+ if (!doc_oob_mode_valid(ops->mode) || (ooblen && ooboffs >= oobavail))
return -EINVAL;
ops->oobretlen = 0;
@@ -900,10 +904,12 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
mutex_lock(&docg3->cascade->lock);
old_stats = mtd->ecc_stats;
while (ret >= 0 && (len > 0 || ooblen > 0)) {
+ unsigned int spare_len;
+
calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
docg3->reliable);
nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
- nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
+ nboob = min_t(size_t, ooblen, oobavail - ooboffs);
ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
if (ret < 0)
goto out;
@@ -919,16 +925,16 @@ 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, oob, 0, 0);
- if (ret < nboob)
- goto err_in_read;
- doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
- oob + nboob, 0, nboob % 2);
+ /* Both spare-area phases issue NOPs, even if one is empty. */
+ spare_len = nboob ? sizeof(oob) : 0;
+ doc_read_page_getbytes(docg3, spare_len, oob, 0, 0);
+ doc_read_page_getbytes(docg3, sizeof(oob) - spare_len,
+ oob + spare_len, 0, 0);
doc_get_bch_hw_ecc(docg3, hwecc);
eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
- if (nboob >= DOC_LAYOUT_OOB_SIZE) {
+ if (nboob) {
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);
@@ -958,15 +964,28 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
}
}
- if (nboob)
- memcpy(oobbuf, oob, nboob);
+ if (nboob && ops->mode == MTD_OPS_AUTO_OOB) {
+ int err;
+
+ err = mtd_ooblayout_get_databytes(mtd, oobbuf, oob,
+ ooboffs, nboob);
+ if (err) {
+ ret = err;
+ goto err_in_read;
+ }
+ } else if (nboob) {
+ memcpy(oobbuf, oob + ooboffs, nboob);
+ }
doc_read_page_finish(docg3);
ops->retlen += nbdata;
ops->oobretlen += nboob;
- buf += nbdata;
- oobbuf += nboob;
+ if (nbdata)
+ buf += nbdata;
+ if (nboob)
+ oobbuf += nboob;
len -= nbdata;
ooblen -= nboob;
+ ooboffs = 0;
from += DOC_LAYOUT_PAGE_SIZE;
skip = 0;
}
@@ -1316,8 +1335,7 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops
{
u32 avail = mtd_oobavail(mtd, ops);
- if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB &&
- ops->mode != MTD_OPS_RAW)
+ if (!doc_oob_mode_valid(ops->mode))
return false;
if (to % DOC_LAYOUT_PAGE_SIZE || ops->len % DOC_LAYOUT_PAGE_SIZE ||
(ops->ooblen && (ops->ooboffs || ops->ooblen % avail)))
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 5/5] mtd: docg3: report the maximum bitflip count across read pages
2026-12-05 5:49 ` James Hilliard
@ 2026-12-05 5:49 ` James Hilliard
-1 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A clean last page can hide earlier bitflips in a multi-page read.
doc_read_oob() tracks the maximum number of bitflips in any page, but
sets its return value to zero each time it starts checking another page.
An MTD caller can therefore miss the indication that the read crossed
the device's bitflip threshold.
Return the maximum after the last successful page. Also record it in
the request statistics so a later failing page does not erase it.
Fixes: edbc4540e02c ("mtd: driver _read() returns max_bitflips; mtd_read() returns -EUCLEAN")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index c00693aba84a..051c6f56d59a 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -861,13 +861,13 @@ static bool doc_oob_mode_valid(unsigned int mode)
/**
* doc_read_oob - Read out of band bytes from flash
* @mtd: the device
- * @from: the offset from first block and first page, in bytes, aligned on page
- * size
+ * @from: byte offset in the device
* @ops: the mtd oob structure
*
* Reads flash memory OOB area of pages.
*
- * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
+ * Return: The maximum number of bitflips in any page read, or a negative
+ * error code.
*/
static int doc_read_oob(struct mtd_info *mtd, loff_t from,
struct mtd_oob_ops *ops)
@@ -960,7 +960,6 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
if (ret > 0) {
mtd->ecc_stats.corrected += ret;
max_bitflips = max(max_bitflips, ret);
- ret = max_bitflips;
}
}
@@ -989,6 +988,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
from += DOC_LAYOUT_PAGE_SIZE;
skip = 0;
}
+ if (ret >= 0)
+ ret = max_bitflips;
out:
if (ops->stats) {
@@ -996,6 +997,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
mtd->ecc_stats.failed - old_stats.failed;
ops->stats->corrected_bitflips +=
mtd->ecc_stats.corrected - old_stats.corrected;
+ ops->stats->max_bitflips = max_bitflips;
}
mutex_unlock(&docg3->cascade->lock);
return ret;
--
2.53.0
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH 5/5] mtd: docg3: report the maximum bitflip count across read pages
@ 2026-12-05 5:49 ` James Hilliard
0 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-12-05 5:49 UTC (permalink / raw)
To: Robert Jarzmik, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Ivan Djelic, David Woodhouse, Mike Dunn,
Brian Norris, Artem Bityutskiy
Cc: linux-mtd, linux-kernel, James Hilliard, stable
A clean last page can hide earlier bitflips in a multi-page read.
doc_read_oob() tracks the maximum number of bitflips in any page, but
sets its return value to zero each time it starts checking another page.
An MTD caller can therefore miss the indication that the read crossed
the device's bitflip threshold.
Return the maximum after the last successful page. Also record it in
the request statistics so a later failing page does not erase it.
Fixes: edbc4540e02c ("mtd: driver _read() returns max_bitflips; mtd_read() returns -EUCLEAN")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/mtd/devices/docg3.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index c00693aba84a..051c6f56d59a 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -861,13 +861,13 @@ static bool doc_oob_mode_valid(unsigned int mode)
/**
* doc_read_oob - Read out of band bytes from flash
* @mtd: the device
- * @from: the offset from first block and first page, in bytes, aligned on page
- * size
+ * @from: byte offset in the device
* @ops: the mtd oob structure
*
* Reads flash memory OOB area of pages.
*
- * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
+ * Return: The maximum number of bitflips in any page read, or a negative
+ * error code.
*/
static int doc_read_oob(struct mtd_info *mtd, loff_t from,
struct mtd_oob_ops *ops)
@@ -960,7 +960,6 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
if (ret > 0) {
mtd->ecc_stats.corrected += ret;
max_bitflips = max(max_bitflips, ret);
- ret = max_bitflips;
}
}
@@ -989,6 +988,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
from += DOC_LAYOUT_PAGE_SIZE;
skip = 0;
}
+ if (ret >= 0)
+ ret = max_bitflips;
out:
if (ops->stats) {
@@ -996,6 +997,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
mtd->ecc_stats.failed - old_stats.failed;
ops->stats->corrected_bitflips +=
mtd->ecc_stats.corrected - old_stats.corrected;
+ ops->stats->max_bitflips = max_bitflips;
}
mutex_unlock(&docg3->cascade->lock);
return ret;
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread