Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] mtd: rawnand: ndfc: correct ECC errors in Smart Media byte order
@ 2026-12-06  4:41 Rosen Penev
  0 siblings, 0 replies; only message in thread
From: Rosen Penev @ 2026-12-06  4:41 UTC (permalink / raw)
  To: linux-mtd
  Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Boris Brezillon, stable

The NDFC calculates its ECC in Smart Media byte order, and since commit
418b2e56b8a6 ("[MTD] NAND: use SmartMedia ECC byte order for ndfc") the
driver selects MTD_NAND_ECC_SMC (now MTD_NAND_ECC_SW_HAMMING_SMC) so the
software Hamming code corrects errors in that order.

Commit 309600c14e36 ("mtd: rawnand: Allow selection of ECC byte ordering
at runtime") replaced the compile time option with the
NAND_ECC_SOFT_HAMMING_SM_ORDER flag, but sets that flag only for the
software ECC engine. The NDFC uses the on-host engine, so
rawnand_sw_hamming_correct() has corrected its errors in the regular
byte order since then. That swaps the two nibbles of the faulty byte
address within each 256-byte step: a single bit error at byte 0x01 of a
step is "corrected" by flipping the same bit at byte 0x10. The read
succeeds and reports corrected bitflips, but the data now has two bad
bits instead of one. UBI then scrubs the block and writes the damaged
data back with a valid ECC.

mtd_nandbiterrs on a Meraki MX60 (APM82181) shows it:

  mtd_nandbiterrs: Inserted biterror @ 1025/7
  mtd_nandbiterrs: Read reported 4 corrected bit errors
  mtd_nandbiterrs: Error: page offset 1025, expected 80, got 00
  mtd_nandbiterrs: Error: page offset 1040, expected 48, got c8
  mtd_nandbiterrs: ECC failure, read data is incorrect despite read success

Correct the errors in Smart Media byte order, as fsmc_nand does for its
hardware engine.

Fixes: 309600c14e36 ("mtd: rawnand: Allow selection of ECC byte ordering at runtime")
Cc: stable@vger.kernel.org
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/mtd/nand/raw/ndfc.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/ndfc.c b/drivers/mtd/nand/raw/ndfc.c
index d7b119028a71..0ebe23726e0c 100644
--- a/drivers/mtd/nand/raw/ndfc.c
+++ b/drivers/mtd/nand/raw/ndfc.c
@@ -18,6 +18,7 @@
  */
 #include <linux/module.h>
 #include <linux/mtd/rawnand.h>
+#include <linux/mtd/nand-ecc-sw-hamming.h>
 #include <linux/mtd/partitions.h>
 #include <linux/mtd/ndfc.h>
 #include <linux/slab.h>
@@ -101,6 +102,14 @@ static int ndfc_calculate_ecc(struct nand_chip *chip,
 	return 0;
 }
 
+static int ndfc_correct_ecc(struct nand_chip *chip, u_char *buf,
+			    u_char *read_ecc, u_char *calc_ecc)
+{
+	/* The ECC bytes come from ndfc_calculate_ecc() in SMC order */
+	return ecc_sw_hamming_correct(buf, read_ecc, calc_ecc,
+				      chip->ecc.size, true);
+}
+
 /*
  * Speedups for buffer read/write/verify
  *
@@ -146,7 +155,7 @@ static int ndfc_chip_init(struct ndfc_controller *ndfc,
 	chip->controller = &ndfc->ndfc_control;
 	chip->legacy.read_buf = ndfc_read_buf;
 	chip->legacy.write_buf = ndfc_write_buf;
-	chip->ecc.correct = rawnand_sw_hamming_correct;
+	chip->ecc.correct = ndfc_correct_ecc;
 	chip->ecc.hwctl = ndfc_enable_hwecc;
 	chip->ecc.calculate = ndfc_calculate_ecc;
 	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-06  4:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  4:41 [PATCH] mtd: rawnand: ndfc: correct ECC errors in Smart Media byte order Rosen Penev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox