[PATCH v11 16/26] mtd: rawnand: sunxi: limit DMA subpage ECC results to the requested range

From: James Hilliard

Date: Fri Nov 20 2026 - 20:12:44 EST


A read of part of a NAND page can return -EBADMSG even when every ECC
step covering the requested bytes is readable. The DMA path transfers
from the start of the page and accounts for errors in that entire
prefix. An uncorrectable error in an earlier step therefore increments
the failure count that the NAND core checks for this read. Corrections
in earlier steps can likewise inflate the reported bitflip count.

Keep the DMA transfer unchanged, but only process ECC results and
perform raw erased-chunk checks for steps overlapping the request.
Keep hardware slots and user-data register indices relative to the start
of each transferred batch.

Fixes: 2de85e733601 ("mtd: nand: sunxi: Actually use DMA for subpage reads")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/sunxi_nand.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
index 7f93ce226f24..5415ac6960b4 100644
--- a/drivers/mtd/nand/raw/sunxi_nand.c
+++ b/drivers/mtd/nand/raw/sunxi_nand.c
@@ -1106,6 +1106,7 @@ static int sunxi_nfc_read_column(struct nand_chip *nand, int page,
struct sunxi_nfc_ecc_read {
u8 *buf;
int page;
+ int first_req_step;
unsigned int max_bitflips;
bool oob_required;
bool dma;
@@ -1234,6 +1235,9 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
int bitflips;

user_data_sz = sunxi_nfc_user_data_sz(sunxi_nand, step);
+ if (step < read->first_req_step)
+ continue;
+
if (status & NFC_ECC_ERR(slot)) {
/* Preserve PIO data before the raw erased-page check. */
if (!read->dma && !randomized)
@@ -1294,7 +1298,7 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
for (slot = 0; slot < nsteps; slot++) {
int step = first_step + slot;

- if (!(status & NFC_ECC_ERR(slot)))
+ if (step < read->first_req_step || !(status & NFC_ECC_ERR(slot)))
continue;

ret = sunxi_nfc_hw_ecc_read_error(nand, read, step);
@@ -1451,12 +1455,14 @@ static int sunxi_nfc_hw_ecc_read_batch_dma(struct nand_chip *nand,
}

static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, u8 *buf,
- int oob_required, int page, int nchunks)
+ int oob_required, int page,
+ int first_req_step, int end_step)
{
struct mtd_info *mtd = nand_to_mtd(nand);
struct sunxi_nfc_ecc_read read = {
.buf = buf,
.page = page,
+ .first_req_step = first_req_step,
.oob_required = oob_required,
.dma = true,
};
@@ -1464,8 +1470,9 @@ static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, u8 *buf,
unsigned int failed = mtd->ecc_stats.failed;
int first_step, batch_steps, ret;

- for (first_step = 0; first_step < nchunks; first_step += batch_steps) {
- batch_steps = sunxi_nfc_dma_batch_steps(nand, first_step, nchunks);
+ /* PAGE_OP transfers a prefix; only the requested steps contribute ECC results. */
+ for (first_step = 0; first_step < end_step; first_step += batch_steps) {
+ batch_steps = sunxi_nfc_dma_batch_steps(nand, first_step, end_step);
ret = sunxi_nfc_hw_ecc_read_batch_dma(nand, &read, first_step, batch_steps);
if (ret)
goto err_stats;
@@ -1618,7 +1625,7 @@ static int sunxi_nfc_hw_ecc_read_page_dma(struct nand_chip *nand, u8 *buf,
if (ret)
return ret;

- ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, oob_required, page,
+ ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, oob_required, page, 0,
nand->ecc.steps);
if (ret >= 0)
return ret;
@@ -1641,14 +1648,15 @@ static int sunxi_nfc_hw_ecc_read_subpage_dma(struct nand_chip *nand,
u32 data_offs, u32 readlen,
u8 *buf, int page)
{
- int nchunks = DIV_ROUND_UP(data_offs + readlen, nand->ecc.size);
+ int first_step = data_offs / nand->ecc.size;
+ int end_step = DIV_ROUND_UP(data_offs + readlen, nand->ecc.size);
int ret;

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);
+ ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, false, page, first_step, end_step);
if (ret >= 0)
return ret;


--
2.53.0