mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 1/5] mtd: docg3: check whole-page OOB writes in the selected mode
Date: Fri, 04 Dec 2026 22:49:27 -0700	[thread overview]
Message-ID: <20261204-submit-docg3-oob-fixes-v1-v1-1-ea0bf6c535ca@gmail.com> (raw)
In-Reply-To: <20261204-submit-docg3-oob-fixes-v1-v1-0-ea0bf6c535ca@gmail.com>

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


  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 ` James Hilliard [this message]
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 ` [PATCH 3/5] mtd: docg3: correct BCH errors in the buffer holding the affected byte James Hilliard
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-1-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®