mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] mtd: rawnand: restore SmartMedia setup in r852
@ 2026-11-09  1:29 James Hilliard
  2026-11-09  1:29 ` [PATCH 1/4] mtd: rawnand: sm_common: restore the first ECC region offset James Hilliard
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: James Hilliard @ 2026-11-09  1:29 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: Maxim Levitsky, Boris Brezillon, Brian Norris, linux-mtd,
	linux-kernel, James Hilliard, stable

r852 installs its own NAND controller, but the shared SmartMedia setup is
still attached to the unused legacy dummy controller. Run that setup from
r852's attach_chip callback so the media-specific OOB layout, bad-block
handling and page-size checks take effect. Restore the hardware-ECC
default during probe so the PCI path selects a valid engine without a
NAND device-tree node.

Before restoring the shared setup, correct the first ECC region's offset,
remove recursive NAND locking from the bad-block marking path, and select
the standard OOB writer for r852's contiguous spare area. These keep ECC
bytes away from the metadata and bad-block writes within the spare area.

Assisted-by: Codex:gpt-6-astra
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>

---
James Hilliard (4):
      mtd: rawnand: sm_common: restore the first ECC region offset
      mtd: rawnand: sm_common: avoid recursive locking when marking bad blocks
      mtd: rawnand: r852: use the standard OOB write operation
      mtd: rawnand: r852: restore SmartMedia and ECC initialization

 drivers/mtd/nand/raw/r852.c      |  9 +++++++
 drivers/mtd/nand/raw/sm_common.c | 55 ++++++++++++++++++++++------------------
 drivers/mtd/nand/raw/sm_common.h |  3 +++
 3 files changed, 42 insertions(+), 25 deletions(-)
---
base-commit: 30daaf3e7897b4a8d9b72e5b0c86dd6e2a4d7d85
change-id: 20261108-submit-r852-smartmedia-attach-v1-290991aae0aa

Best regards,
--  
James Hilliard <james.hilliard1@gmail.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] mtd: rawnand: sm_common: restore the first ECC region offset
  2026-11-09  1:29 [PATCH 0/4] mtd: rawnand: restore SmartMedia setup in r852 James Hilliard
@ 2026-11-09  1:29 ` James Hilliard
  2026-11-09  1:29 ` [PATCH 2/4] mtd: rawnand: sm_common: avoid recursive locking when marking bad blocks James Hilliard
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: James Hilliard @ 2026-11-09  1:29 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: Maxim Levitsky, Boris Brezillon, Brian Norris, linux-mtd,
	linux-kernel, James Hilliard, stable

The conversion to mtd_ooblayout_ops moved the first ECC region from bytes
8-10 to bytes 5-7. Those bytes contain the bad-block marker and the first
logical-block address, so software ECC writes can overwrite them and ECC
reads use the wrong bytes.

Derive both ECC region offsets from struct sm_oob, restoring the original
8-10 and 13-15 layout without overlapping the metadata.

Fixes: 987b913c2b7e ("mtd: nand: sm_common: switch to mtd_ooblayout_ops")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/mtd/nand/raw/sm_common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/sm_common.c b/drivers/mtd/nand/raw/sm_common.c
index e238784c8c3e..a3a3a05cc5a4 100644
--- a/drivers/mtd/nand/raw/sm_common.c
+++ b/drivers/mtd/nand/raw/sm_common.c
@@ -16,7 +16,8 @@ static int oob_sm_ooblayout_ecc(struct mtd_info *mtd, int section,
 		return -ERANGE;
 
 	oobregion->length = 3;
-	oobregion->offset = ((section + 1) * 8) - 3;
+	oobregion->offset = section ? offsetof(struct sm_oob, ecc1) :
+				      offsetof(struct sm_oob, ecc2);
 
 	return 0;
 }

-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] mtd: rawnand: sm_common: avoid recursive locking when marking bad blocks
  2026-11-09  1:29 [PATCH 0/4] mtd: rawnand: restore SmartMedia setup in r852 James Hilliard
  2026-11-09  1:29 ` [PATCH 1/4] mtd: rawnand: sm_common: restore the first ECC region offset James Hilliard
@ 2026-11-09  1:29 ` James Hilliard
  2026-11-09  1:29 ` [PATCH 3/4] mtd: rawnand: r852: use the standard OOB write operation James Hilliard
  2026-11-09  1:29 ` [PATCH 4/4] mtd: rawnand: r852: restore SmartMedia and ECC initialization James Hilliard
  3 siblings, 0 replies; 5+ messages in thread
From: James Hilliard @ 2026-11-09  1:29 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: Maxim Levitsky, Boris Brezillon, Brian Norris, linux-mtd,
	linux-kernel, James Hilliard, stable

nand_block_markbad_lowlevel() holds the NAND chip and controller locks
when it invokes the legacy block_markbad callback. sm_block_markbad()
calls mtd_write_oob(), which re-enters nand_write_oob() and waits for the
same locks, deadlocking the bad-block marking operation.

Write the marker through the chip's OOB callback under the existing locks.
Keep the reset, target selection, write-protection check and page-cache
invalidation performed by the OOB write path, including the broken-xD
write-protection exception. Preserve the 0x0f marker and fill the rest of
the OOB with 0xff.

Use the OOB callback's return value directly. This also avoids comparing
an 8-byte OOB write on 256-byte-page cards against the 16-byte sector OOB
size and incorrectly reporting a successful write as an error.

Fixes: 5a0edb251ae9 ("mtd: nand: refactor chip->block_markbad interface")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/mtd/nand/raw/sm_common.c | 44 ++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/drivers/mtd/nand/raw/sm_common.c b/drivers/mtd/nand/raw/sm_common.c
index a3a3a05cc5a4..a6387111d271 100644
--- a/drivers/mtd/nand/raw/sm_common.c
+++ b/drivers/mtd/nand/raw/sm_common.c
@@ -100,30 +100,38 @@ static const struct mtd_ooblayout_ops oob_sm_small_ops = {
 static int sm_block_markbad(struct nand_chip *chip, loff_t ofs)
 {
 	struct mtd_info *mtd = nand_to_mtd(chip);
-	struct mtd_oob_ops ops = { };
-	struct sm_oob oob;
+	int chipnr = ofs >> chip->chip_shift;
+	int page = ofs >> chip->page_shift;
+	u8 status;
 	int ret;
 
-	memset(&oob, -1, SM_OOB_SIZE);
-	oob.block_status = 0x0F;
+	ret = nand_reset(chip, chipnr);
+	if (ret)
+		return ret;
 
-	/* As long as this function is called on erase block boundaries
-		it will work correctly for 256 byte nand */
-	ops.mode = MTD_OPS_PLACE_OOB;
-	ops.ooboffs = 0;
-	ops.ooblen = mtd->oobsize;
-	ops.oobbuf = (void *)&oob;
-	ops.datbuf = NULL;
+	nand_select_target(chip, chipnr);
+	if (!(chip->options & NAND_BROKEN_XD) &&
+	    !chip->controller->controller_wp) {
+		ret = nand_status_op(chip, &status);
+		if (ret)
+			goto out;
+		if (!(status & NAND_STATUS_WP)) {
+			ret = -EROFS;
+			goto out;
+		}
+	}
 
+	if (page == chip->pagecache.page)
+		chip->pagecache.page = -1;
 
-	ret = mtd_write_oob(mtd, ofs, &ops);
-	if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) {
-		pr_notice("sm_common: can't mark sector at %i as bad\n",
-			  (int)ofs);
-		return -EIO;
-	}
+	memset(chip->oob_poi, 0xff, mtd->oobsize);
+	chip->oob_poi[chip->badblockpos] = 0x0f;
 
-	return 0;
+	/* The NAND core already holds the chip and controller locks. */
+	ret = chip->ecc.write_oob(chip, page & chip->pagemask);
+out:
+	nand_deselect_target(chip);
+	return ret;
 }
 
 static struct nand_flash_dev nand_smartmedia_flash_ids[] = {

-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] mtd: rawnand: r852: use the standard OOB write operation
  2026-11-09  1:29 [PATCH 0/4] mtd: rawnand: restore SmartMedia setup in r852 James Hilliard
  2026-11-09  1:29 ` [PATCH 1/4] mtd: rawnand: sm_common: restore the first ECC region offset James Hilliard
  2026-11-09  1:29 ` [PATCH 2/4] mtd: rawnand: sm_common: avoid recursive locking when marking bad blocks James Hilliard
@ 2026-11-09  1:29 ` James Hilliard
  2026-11-09  1:29 ` [PATCH 4/4] mtd: rawnand: r852: restore SmartMedia and ECC initialization James Hilliard
  3 siblings, 0 replies; 5+ messages in thread
From: James Hilliard @ 2026-11-09  1:29 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: Maxim Levitsky, Boris Brezillon, Brian Norris, linux-mtd,
	linux-kernel, James Hilliard, stable

r852 uses interleaved ECC placement to transfer a complete 512-byte sector
and its 16-byte spare area through the hardware ECC engine. It supplies a
custom OOB reader but leaves the OOB writer unset, so the NAND core selects
nand_write_oob_syndrome().

With one ECC step and no prepad or postpad, that writer starts programming
at steps * (size + bytes), or byte 528. The subsequent 16-byte transfer
starts beyond the spare area instead of writing bytes 512-527. Raw OOB
writes inherit the same callback.

Set write_oob to nand_write_oob_std(), which writes the spare area starting
at the page's data size. Keep the interleaved page callbacks for full-page
ECC transfers.

Fixes: 67e054e91924 ("mtd: nand: Add driver for Ricoh xD/SmartMedia reader")
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/mtd/nand/raw/r852.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/mtd/nand/raw/r852.c b/drivers/mtd/nand/raw/r852.c
index 92c47d351c27..e78606b97b42 100644
--- a/drivers/mtd/nand/raw/r852.c
+++ b/drivers/mtd/nand/raw/r852.c
@@ -833,6 +833,7 @@ static int r852_attach_chip(struct nand_chip *chip)
 
 	/* TODO: hack */
 	chip->ecc.read_oob = r852_read_oob;
+	chip->ecc.write_oob = nand_write_oob_std;
 
 	return 0;
 }

-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] mtd: rawnand: r852: restore SmartMedia and ECC initialization
  2026-11-09  1:29 [PATCH 0/4] mtd: rawnand: restore SmartMedia setup in r852 James Hilliard
                   ` (2 preceding siblings ...)
  2026-11-09  1:29 ` [PATCH 3/4] mtd: rawnand: r852: use the standard OOB write operation James Hilliard
@ 2026-11-09  1:29 ` James Hilliard
  3 siblings, 0 replies; 5+ messages in thread
From: James Hilliard @ 2026-11-09  1:29 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: Maxim Levitsky, Boris Brezillon, Brian Norris, linux-mtd,
	linux-kernel, James Hilliard, stable

r852 uses its own NAND controller and attach_chip callback, but
sm_register_device() installs sm_attach_chip() on the unused legacy
dummy controller. nand_scan_with_ids() only calls the active controller's
callback, so the SmartMedia setup is skipped.

This leaves the generic OOB layout and bad-block criteria in place,
omits the SmartMedia bad-block marking callback, and skips rejection of
unsupported page sizes.

The same conversion removed the hardware-ECC default from probe. This
PCI driver does not set a NAND device-tree node, so rawnand_dt_init()
returns without selecting an ECC engine. The zero-initialized engine
type remains invalid and nand_scan_tail() fails with -EINVAL.

Restore the hardware-ECC default in probe. Export sm_attach_chip() and
call it from r852_attach_chip() before the ECC-engine check, so both
hardware and software ECC configurations get the media layout. Propagate
its error before scanning finishes or the MTD device is published.
Remove the unused dummy-controller setup; r852 is the only caller of
sm_register_device().

Fixes: 7ef969a04228 ("mtd: rawnand: r852: Move the ECC initialization to ->attach_chip()")
Closes: https://lore.kernel.org/r/sashiko-outbox-204208@kernel.org/
Cc: stable@vger.kernel.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/mtd/nand/raw/r852.c      | 8 ++++++++
 drivers/mtd/nand/raw/sm_common.c | 8 ++------
 drivers/mtd/nand/raw/sm_common.h | 3 +++
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/mtd/nand/raw/r852.c b/drivers/mtd/nand/raw/r852.c
index e78606b97b42..ce7def018099 100644
--- a/drivers/mtd/nand/raw/r852.c
+++ b/drivers/mtd/nand/raw/r852.c
@@ -820,6 +820,12 @@ static irqreturn_t r852_irq(int irq, void *data)
 
 static int r852_attach_chip(struct nand_chip *chip)
 {
+	int ret;
+
+	ret = sm_attach_chip(chip);
+	if (ret)
+		return ret;
+
 	if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
 		return 0;
 
@@ -883,6 +889,8 @@ static int  r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
 	chip->legacy.read_buf = r852_read_buf;
 	chip->legacy.write_buf = r852_write_buf;
 
+	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
+
 	/* init our device structure */
 	dev = kzalloc_obj(struct r852_device);
 
diff --git a/drivers/mtd/nand/raw/sm_common.c b/drivers/mtd/nand/raw/sm_common.c
index a6387111d271..291aa21dff4f 100644
--- a/drivers/mtd/nand/raw/sm_common.c
+++ b/drivers/mtd/nand/raw/sm_common.c
@@ -167,7 +167,7 @@ static struct nand_flash_dev nand_xd_flash_ids[] = {
 	{NULL}
 };
 
-static int sm_attach_chip(struct nand_chip *chip)
+int sm_attach_chip(struct nand_chip *chip)
 {
 	struct mtd_info *mtd = nand_to_mtd(chip);
 
@@ -186,10 +186,7 @@ static int sm_attach_chip(struct nand_chip *chip)
 
 	return 0;
 }
-
-static const struct nand_controller_ops sm_controller_ops = {
-	.attach_chip = sm_attach_chip,
-};
+EXPORT_SYMBOL_GPL(sm_attach_chip);
 
 int sm_register_device(struct mtd_info *mtd, int smartmedia)
 {
@@ -200,7 +197,6 @@ int sm_register_device(struct mtd_info *mtd, int smartmedia)
 	chip->options |= NAND_SKIP_BBTSCAN;
 
 	/* Scan for card properties */
-	chip->legacy.dummy_controller.ops = &sm_controller_ops;
 	flash_ids = smartmedia ? nand_smartmedia_flash_ids : nand_xd_flash_ids;
 	ret = nand_scan_with_ids(chip, 1, flash_ids);
 	if (ret)
diff --git a/drivers/mtd/nand/raw/sm_common.h b/drivers/mtd/nand/raw/sm_common.h
index 57fc9f86f9ee..599fce778fb2 100644
--- a/drivers/mtd/nand/raw/sm_common.h
+++ b/drivers/mtd/nand/raw/sm_common.h
@@ -33,6 +33,9 @@ struct sm_oob {
 #define SM_SMALL_OOB_SIZE	8
 
 
+struct nand_chip;
+
+int sm_attach_chip(struct nand_chip *chip);
 int sm_register_device(struct mtd_info *mtd, int smartmedia);
 
 

-- 
2.53.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-09  1:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09  1:29 [PATCH 0/4] mtd: rawnand: restore SmartMedia setup in r852 James Hilliard
2026-11-09  1:29 ` [PATCH 1/4] mtd: rawnand: sm_common: restore the first ECC region offset James Hilliard
2026-11-09  1:29 ` [PATCH 2/4] mtd: rawnand: sm_common: avoid recursive locking when marking bad blocks James Hilliard
2026-11-09  1:29 ` [PATCH 3/4] mtd: rawnand: r852: use the standard OOB write operation James Hilliard
2026-11-09  1:29 ` [PATCH 4/4] mtd: rawnand: r852: restore SmartMedia and ECC initialization James Hilliard

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®