[PATCH 4/5] mtd: docg3: return the requested spare bytes in the selected OOB mode
From: James Hilliard
Date: Sat Dec 05 2026 - 00:49:47 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
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