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 --- 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 #include +#include #include #include #include @@ -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