Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-mtd@lists.infradead.org
Cc: Miquel Raynal <miquel.raynal@bootlin.com>,
	Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	Boris Brezillon <bbrezillon@kernel.org>,
	stable@vger.kernel.org
Subject: [PATCH] mtd: rawnand: ndfc: correct ECC errors in Smart Media byte order
Date: Sat,  5 Dec 2026 20:41:22 -0800	[thread overview]
Message-ID: <20261206044122.306763-1-rosenp@gmail.com> (raw)

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


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

                 reply	other threads:[~2026-12-06  4:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261206044122.306763-1-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=bbrezillon@kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --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