[PATCH 2/2] mtd: onenand: take the spare-area column from the OOB request

From: James Hilliard

Date: Sat Dec 05 2026 - 01:53:51 EST


Reading spare bytes at an address in the middle of a data page can
return bytes from the wrong column or reject a valid AUTO read. A
combined write can likewise put the spare bytes in the wrong place.
The data address selects the page; ooboffs selects the first spare
byte on that page.

Use ooboffs only while spare bytes remain and start at column zero on
subsequent pages. Read partial spare areas into the chip's existing
buffer before extracting the requested bytes: an odd column is valid
for MTD but not for a 16-bit BufferRAM read. Have the bad-block marker
writer pass its word offset in ooboffs as well.

Fixes: 8593fbc68b0d ("[MTD] Rework the out of band handling completely")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/onenand/onenand_base.c | 127 ++++++++++----------------------
1 file changed, 39 insertions(+), 88 deletions(-)

diff --git a/drivers/mtd/nand/onenand/onenand_base.c b/drivers/mtd/nand/onenand/onenand_base.c
index 810966b75aa4..cfd038b5e6cd 100644
--- a/drivers/mtd/nand/onenand/onenand_base.c
+++ b/drivers/mtd/nand/onenand/onenand_base.c
@@ -1042,21 +1042,34 @@ static void onenand_release_device(struct mtd_info *mtd)
}

/**
- * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
+ * onenand_transfer_oob - copy spare bytes from the current BufferRAM
* @mtd: MTD device structure
- * @buf: destination address
- * @column: oob offset to read from
- * @thislen: oob length to read
+ * @ops: OOB read request
+ * @read: number of spare bytes already copied
+ *
+ * Return: Number of bytes copied from this page.
*/
-static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
- int thislen)
+static size_t onenand_transfer_oob(struct mtd_info *mtd, struct mtd_oob_ops *ops, size_t read)
{
struct onenand_chip *this = mtd->priv;
+ unsigned int column = read ? 0 : ops->ooboffs;
+ size_t len = min_t(size_t, mtd_oobavail(mtd, ops) - column, ops->ooblen - read);
+ u8 *buf = ops->oobbuf + read;
+
+ if (ops->mode == MTD_OPS_PLACE_OOB && len == mtd->oobsize) {
+ this->read_bufferram(mtd, ONENAND_SPARERAM, buf, 0, len);
+ return len;
+ }

+ /* BufferRAM is word-addressed; a spare byte can start at any column. */
this->read_bufferram(mtd, ONENAND_SPARERAM, this->oob_buf, 0,
mtd->oobsize);
- return mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf,
- column, thislen);
+ if (ops->mode == MTD_OPS_AUTO_OOB)
+ mtd_ooblayout_get_databytes(mtd, buf, this->oob_buf, column, len);
+ else
+ memcpy(buf, this->oob_buf + column, len);
+
+ return len;
}

/**
@@ -1117,20 +1130,15 @@ static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
struct onenand_chip *this = mtd->priv;
struct mtd_ecc_stats stats;
size_t len = ops->len;
- size_t ooblen = ops->ooblen;
u_char *buf = ops->datbuf;
- u_char *oobbuf = ops->oobbuf;
int read = 0, column, thislen;
- int oobread = 0, oobcolumn, thisooblen, oobsize;
+ size_t oobread = 0;
int ret = 0;
int writesize = this->writesize;

pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
(int)len);

- oobsize = mtd_oobavail(mtd, ops);
- oobcolumn = from & (mtd->oobsize - 1);
-
/* Do not allow reads past end of device */
if (from + len > mtd->size) {
printk(KERN_ERR "%s: Attempt read beyond end of device\n",
@@ -1165,18 +1173,8 @@ static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
}

this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
- if (oobbuf) {
- thisooblen = oobsize - oobcolumn;
- thisooblen = min_t(int, thisooblen, ooblen - oobread);
-
- if (ops->mode == MTD_OPS_AUTO_OOB)
- onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
- else
- this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
- oobread += thisooblen;
- oobbuf += thisooblen;
- oobcolumn = 0;
- }
+ if (oobread < ops->ooblen)
+ oobread += onenand_transfer_oob(mtd, ops, oobread);

read += thislen;
if (read == len)
@@ -1218,20 +1216,15 @@ static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
struct onenand_chip *this = mtd->priv;
struct mtd_ecc_stats stats;
size_t len = ops->len;
- size_t ooblen = ops->ooblen;
u_char *buf = ops->datbuf;
- u_char *oobbuf = ops->oobbuf;
int read = 0, column, thislen;
- int oobread = 0, oobcolumn, thisooblen, oobsize;
+ size_t oobread = 0;
int ret = 0, boundary = 0;
int writesize = this->writesize;

pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
(int)len);

- oobsize = mtd_oobavail(mtd, ops);
- oobcolumn = from & (mtd->oobsize - 1);
-
/* Do not allow reads past end of device */
if ((from + len) > mtd->size) {
printk(KERN_ERR "%s: Attempt read beyond end of device\n",
@@ -1283,18 +1276,8 @@ static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);

/* Read oob area if needed */
- if (oobbuf) {
- thisooblen = oobsize - oobcolumn;
- thisooblen = min_t(int, thisooblen, ooblen - oobread);
-
- if (ops->mode == MTD_OPS_AUTO_OOB)
- onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
- else
- this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
- oobread += thisooblen;
- oobbuf += thisooblen;
- oobcolumn = 0;
- }
+ if (oobread < ops->ooblen)
+ oobread += onenand_transfer_oob(mtd, ops, oobread);

/* See if we are done */
read += thislen;
@@ -1346,28 +1329,16 @@ static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
{
struct onenand_chip *this = mtd->priv;
struct mtd_ecc_stats stats;
- int thislen, column, oobsize;
size_t read = 0, len = ops->ooblen;
- unsigned int mode = ops->mode;
- u_char *buf = ops->oobbuf;
int ret = 0, readcmd;

- from += ops->ooboffs;
-
pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
(int)len);

/* Initialize return length value */
ops->oobretlen = 0;

- if (mode == MTD_OPS_AUTO_OOB)
- oobsize = mtd->oobavail;
- else
- oobsize = mtd->oobsize;
-
- column = from & (mtd->oobsize - 1);
-
- if (unlikely(column >= oobsize)) {
+ if (unlikely(len && ops->ooboffs >= mtd_oobavail(mtd, ops))) {
printk(KERN_ERR "%s: Attempted to start read outside oob\n",
__func__);
return -EINVAL;
@@ -1380,8 +1351,6 @@ static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
while (read < len) {
cond_resched();

- thislen = min_t(size_t, oobsize - column, len - read);
-
this->command(mtd, readcmd, from, mtd->oobsize);

onenand_update_bufferram(mtd, from, 0);
@@ -1396,24 +1365,9 @@ static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
break;
}

- if (mode == MTD_OPS_AUTO_OOB)
- onenand_transfer_auto_oob(mtd, buf, column, thislen);
- else
- this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
-
- read += thislen;
-
- if (read == len)
- break;
-
- buf += thislen;
-
- /* Read more? */
- if (read < len) {
- /* Page size */
+ read += onenand_transfer_oob(mtd, ops, read);
+ if (read < len)
from += mtd->writesize;
- column = 0;
- }
}

ops->oobretlen = read;
@@ -1799,7 +1753,7 @@ static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
struct onenand_chip *this = mtd->priv;
int written = 0, column, thislen = 0, subpage = 0;
int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
- int oobwritten = 0, oobcolumn, thisooblen, oobsize;
+ size_t oobwritten = 0;
size_t len = ops->len;
size_t ooblen = ops->ooblen;
const u_char *buf = ops->datbuf;
@@ -1824,8 +1778,6 @@ static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
/* Check zero length */
if (!len)
return 0;
- oobsize = mtd_oobavail(mtd, ops);
- oobcolumn = to & (mtd->oobsize - 1);

column = to & (mtd->writesize - 1);

@@ -1835,7 +1787,6 @@ static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
u_char *wbuf = (u_char *) buf;

thislen = min_t(int, mtd->writesize - column, len - written);
- thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);

cond_resched();

@@ -1851,7 +1802,11 @@ static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,

this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);

- if (oob) {
+ if (oobwritten < ooblen) {
+ unsigned int oobcolumn = oobwritten ? 0 : ops->ooboffs;
+ size_t thisooblen = min_t(size_t, ooblen - oobwritten,
+ mtd_oobavail(mtd, ops) - oobcolumn);
+
oobbuf = this->oob_buf;

/* We send data to spare ram with oobsize
@@ -1864,7 +1819,6 @@ static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,

oobwritten += thisooblen;
oob += thisooblen;
- oobcolumn = 0;
} else
oobbuf = (u_char *) ffchars;

@@ -1985,8 +1939,6 @@ static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
const u_char *buf = ops->oobbuf;
unsigned int mode = ops->mode;

- to += ops->ooboffs;
-
pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
(int)len);

@@ -1998,7 +1950,7 @@ static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
else
oobsize = mtd->oobsize;

- column = to & (mtd->oobsize - 1);
+ column = ops->ooboffs;

if (unlikely(column >= oobsize)) {
printk(KERN_ERR "%s: Attempted to start write outside oob\n",
@@ -2448,7 +2400,8 @@ static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
.mode = MTD_OPS_PLACE_OOB,
.ooblen = 2,
.oobbuf = buf,
- .ooboffs = 0,
+ /* Write both bytes of the word containing the marker. */
+ .ooboffs = this->badblockpos & ~1,
};
int block;

@@ -2457,8 +2410,6 @@ static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
if (bbm->bbt)
bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);

- /* We write two bytes, so we don't have to mess with 16-bit access */
- ofs += mtd->oobsize + (this->badblockpos & ~0x01);
/* FIXME : What to do when marking SLC block in partition
* with MLC erasesize? For now, it is not advisable to
* create partitions containing both SLC and MLC regions.

--
2.53.0