[PATCH v11 07/26] mtd: rawnand: sunxi: propagate page-setup and erased-check errors

From: James Hilliard

Date: Fri Nov 20 2026 - 20:11:47 EST


If the NAND read command or ready wait times out, an ECC read can report
success with stale data: the callbacks ignore the error from
nand_read_page_op() and continue with the page transfer. Return the
setup error before starting PIO or DMA.

After an ECC error, the driver checks the physical data and OOB for
an erased chunk. It rereads main data if scrambling was enabled and
always rereads OOB. If either reread fails, propagate that error
instead of checking stale data or counting a transport failure as an
ECC result. Leave uncorrectable media errors in the ECC statistics
for the NAND core's normal retry handling.

Route PIO read errors through ECC disable. DMA has already released its
transfer resources before interpreting the results, and its caller
retries a failed read in PIO. Restore the corrected/failed counters from
before that DMA attempt so earlier steps are not counted again by the
retry; leave bad-block counters unchanged.

Fixes: 97d90da8a886 ("mtd: nand: provide several helpers to do common NAND operations")
Fixes: 25f815f66a14 ("mtd: nand: force drivers to explicitly send READ/PROG commands")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/sunxi_nand.c | 45 ++++++++++++++++++++++++++++++---------
1 file changed, 35 insertions(+), 10 deletions(-)

diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
index 9d7e3e6f22b2..c5e429318de2 100644
--- a/drivers/mtd/nand/raw/sunxi_nand.c
+++ b/drivers/mtd/nand/raw/sunxi_nand.c
@@ -1120,11 +1120,16 @@ static int sunxi_nfc_hw_ecc_read_error(struct nand_chip *nand,
int ret;

/* Check the physical representation for bitflips in erased pages. */
- if (nand->options & NAND_NEED_SCRAMBLING)
- sunxi_nfc_read_column(nand, read->page, data_off, data, ecc->size);
+ if (nand->options & NAND_NEED_SCRAMBLING) {
+ ret = sunxi_nfc_read_column(nand, read->page, data_off, data, ecc->size);
+ if (ret)
+ return ret;
+ }

- sunxi_nfc_read_column(nand, read->page, nand_to_mtd(nand)->writesize + oob_off,
- oob, oob_len);
+ ret = sunxi_nfc_read_column(nand, read->page, nand_to_mtd(nand)->writesize + oob_off,
+ oob, oob_len);
+ if (ret)
+ return ret;

ret = nand_check_erased_ecc_chunk(data, ecc->size, oob, oob_len, NULL, 0,
ecc->strength);
@@ -1308,6 +1313,7 @@ static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, u8 *buf,
{
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
+ struct mtd_info *mtd = nand_to_mtd(nand);
struct nand_ecc_ctrl *ecc = &nand->ecc;
struct sunxi_nfc_ecc_read read = {
.buf = buf,
@@ -1315,6 +1321,8 @@ static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, u8 *buf,
.oob_required = oob_required,
.dma = true,
};
+ unsigned int corrected = mtd->ecc_stats.corrected;
+ unsigned int failed = mtd->ecc_stats.failed;
struct scatterlist sg;
int ret, i;
u32 status, pattern_found, wait;
@@ -1366,9 +1374,19 @@ static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, u8 *buf,
ret = sunxi_nfc_oob_read_batch(nand, &read, status, pattern_found,
0, nchunks);
if (ret)
- return ret;
+ goto err_stats;

- return sunxi_nfc_oob_finish_read(nand, &read, NULL);
+ ret = sunxi_nfc_oob_finish_read(nand, &read, NULL);
+ if (ret < 0)
+ goto err_stats;
+
+ return ret;
+
+err_stats:
+ /* The caller retries the whole read in PIO mode. */
+ mtd->ecc_stats.corrected = corrected;
+ mtd->ecc_stats.failed = failed;
+ return ret;
}

static int sunxi_nfc_hw_ecc_write_chunk(struct nand_chip *nand,
@@ -1453,7 +1471,9 @@ static int sunxi_nfc_hw_ecc_read_chunks_pio(struct nand_chip *nand, u8 *buf,
};
int ret, step, cur_off = 0;

- nand_read_page_op(nand, page, 0, NULL, 0);
+ ret = nand_read_page_op(nand, page, 0, NULL, 0);
+ if (ret)
+ return ret;

sunxi_nfc_hw_ecc_enable(nand);

@@ -1461,10 +1481,11 @@ static int sunxi_nfc_hw_ecc_read_chunks_pio(struct nand_chip *nand, u8 *buf,
for (step = first_step; step < end_step; step++) {
ret = sunxi_nfc_hw_ecc_read_chunk(nand, &read, step, &cur_off);
if (ret < 0)
- return ret;
+ goto out;
}

ret = sunxi_nfc_oob_finish_read(nand, &read, &cur_off);
+out:
sunxi_nfc_hw_ecc_disable(nand);

return ret;
@@ -1482,7 +1503,9 @@ static int sunxi_nfc_hw_ecc_read_page_dma(struct nand_chip *nand, u8 *buf,
{
int ret;

- nand_read_page_op(nand, page, 0, NULL, 0);
+ ret = nand_read_page_op(nand, page, 0, NULL, 0);
+ if (ret)
+ return ret;

ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, oob_required, page,
nand->ecc.steps);
@@ -1510,7 +1533,9 @@ static int sunxi_nfc_hw_ecc_read_subpage_dma(struct nand_chip *nand,
int nchunks = DIV_ROUND_UP(data_offs + readlen, nand->ecc.size);
int ret;

- nand_read_page_op(nand, page, 0, NULL, 0);
+ ret = nand_read_page_op(nand, page, 0, NULL, 0);
+ if (ret)
+ return ret;

ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, false, page, nchunks);
if (ret >= 0)

--
2.53.0