[PATCH v2 1/2] mmc: meson-mx-sdhc: complete failed DMA requests with zero bytes

From: ZHENPENG YUAN

Date: Tue Dec 01 2026 - 07:31:48 EST


The interrupt handler records FIFO and response errors in cmd->error,
while it records data CRC errors and timeouts in data->error. The
threaded handler checks only the latter before it unmaps the request
and accounts the full transfer. A command error can therefore report a
successful transfer length, and a data error can leak the DMA mapping.

Clear bytes_xfered at the start of each attempt. Wait for the controller
to finish or reset it before unmapping every data request. Account the
transfer only if the command and data both succeeded. Propagate a failed
ready poll as well, since the recovery reset aborts the transfer.

Check for an idle controller before programming the next command. Keep a
DMA mapping error local to its request so subsequent attempts can retry.

Fixes: e4bf1b0970ef ("mmc: host: meson-mx-sdhc: new driver for the Amlogic Meson SDHC host")
Link: https://lore.kernel.org/r/sashiko-outbox-192112@xxxxxxxxxx
Signed-off-by: ZHENPENG YUAN <yuanzhenpeng210@xxxxxxxxx>
---

Notes (meson-v2):
Changes since the RFC:
- Separate the DMA completion fix so it can be applied independently.
- Check command errors and idle-poll failures before reporting progress.

Sashiko flagged the command-error case in the RFC. Codex assisted with
the implementation and changelog for this revision.

drivers/mmc/host/meson-mx-sdhc-mmc.c | 85 ++++++++++++++++------------
1 file changed, 50 insertions(+), 35 deletions(-)

diff --git a/drivers/mmc/host/meson-mx-sdhc-mmc.c b/drivers/mmc/host/meson-mx-sdhc-mmc.c
index fb49ea71289e..1f8f8f0fbdb6 100644
--- a/drivers/mmc/host/meson-mx-sdhc-mmc.c
+++ b/drivers/mmc/host/meson-mx-sdhc-mmc.c
@@ -100,9 +100,9 @@ static void meson_mx_sdhc_clear_fifo(struct mmc_host *mmc)
FIELD_GET(MESON_SDHC_STAT_TXFIFO_CNT, stat));
}

-static void meson_mx_sdhc_wait_cmd_ready(struct mmc_host *mmc)
+static int meson_mx_sdhc_wait_cmd_ready(struct meson_mx_sdhc_host *host,
+ struct mmc_command *cmd)
{
- struct meson_mx_sdhc_host *host = mmc_priv(mmc);
u32 stat, esta;
int ret;

@@ -111,10 +111,11 @@ static void meson_mx_sdhc_wait_cmd_ready(struct mmc_host *mmc)
MESON_SDHC_WAIT_CMD_READY_SLEEP_US,
MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US);
if (ret) {
- dev_warn(mmc_dev(mmc),
+ dev_warn(mmc_dev(host->mmc),
"Failed to poll for CMD_BUSY while processing CMD%d\n",
- host->cmd->opcode);
+ cmd->opcode);
meson_mx_sdhc_reset(host);
+ return ret;
}

ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_ESTA, esta,
@@ -122,11 +123,13 @@ static void meson_mx_sdhc_wait_cmd_ready(struct mmc_host *mmc)
MESON_SDHC_WAIT_CMD_READY_SLEEP_US,
MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US);
if (ret) {
- dev_warn(mmc_dev(mmc),
+ dev_warn(mmc_dev(host->mmc),
"Failed to poll for ESTA[13:11] while processing CMD%d\n",
- host->cmd->opcode);
+ cmd->opcode);
meson_mx_sdhc_reset(host);
}
+
+ return ret;
}

static void meson_mx_sdhc_start_cmd(struct mmc_host *mmc,
@@ -223,8 +226,6 @@ static void meson_mx_sdhc_start_cmd(struct mmc_host *mmc,
regmap_write(host->regmap, MESON_SDHC_ADDR,
sg_dma_address(cmd->data->sg));

- meson_mx_sdhc_wait_cmd_ready(mmc);
-
if (cmd->data)
host->platform->set_pdma(mmc);

@@ -382,12 +383,19 @@ static void meson_mx_sdhc_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
struct meson_mx_sdhc_host *host = mmc_priv(mmc);
struct mmc_command *cmd = mrq->cmd;
+ int ret = host->error;

- if (!host->error)
- host->error = meson_mx_sdhc_map_dma(mmc, mrq);
+ if (mrq->data) {
+ mrq->data->error = 0;
+ mrq->data->bytes_xfered = 0;
+ }

- if (host->error) {
- cmd->error = host->error;
+ if (!ret)
+ ret = meson_mx_sdhc_wait_cmd_ready(host, cmd);
+ if (!ret)
+ ret = meson_mx_sdhc_map_dma(mmc, mrq);
+ if (ret) {
+ cmd->error = ret;
mmc_request_done(mmc, mrq);
return;
}
@@ -572,38 +580,35 @@ static irqreturn_t meson_mx_sdhc_irq_thread(int irq, void *irq_data)
{
struct meson_mx_sdhc_host *host = irq_data;
struct mmc_command *cmd;
+ struct mmc_data *data;
u32 val;
+ int ret;

cmd = host->cmd;
if (WARN_ON(!cmd))
return IRQ_HANDLED;

- if (cmd->data && !cmd->data->error) {
- if (!host->platform->hardware_flush_all_cmds &&
- cmd->data->flags & MMC_DATA_READ) {
- meson_mx_sdhc_wait_cmd_ready(host->mmc);
-
- /*
- * If MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH was
- * previously 0x1 then it has to be set to 0x3. If it
- * was 0x0 before then it has to be set to 0x2. Without
- * this reading SD cards sometimes transfers garbage,
- * which results in cards not being detected due to:
- * unrecognised SCR structure version <random number>
- */
- val = FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH,
- 2);
- regmap_update_bits(host->regmap, MESON_SDHC_PDMA, val,
- val);
- }
-
- dma_unmap_sg(mmc_dev(host->mmc), cmd->data->sg,
- cmd->data->sg_len, mmc_get_dma_dir(cmd->data));
+ data = cmd->data;
+ if (data && !cmd->error && !data->error &&
+ !host->platform->hardware_flush_all_cmds &&
+ data->flags & MMC_DATA_READ) {
+ ret = meson_mx_sdhc_wait_cmd_ready(host, cmd);
+ if (ret)
+ goto done;

- cmd->data->bytes_xfered = cmd->data->blksz * cmd->data->blocks;
+ /*
+ * If MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH was previously 0x1
+ * then it has to be set to 0x3. If it was 0x0 before then it
+ * has to be set to 0x2. Without this reading SD cards sometimes
+ * transfers garbage, which results in cards not being detected.
+ */
+ val = FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH, 2);
+ regmap_update_bits(host->regmap, MESON_SDHC_PDMA, val, val);
}

- meson_mx_sdhc_wait_cmd_ready(host->mmc);
+ ret = meson_mx_sdhc_wait_cmd_ready(host, cmd);
+ if (ret)
+ goto done;

if (cmd->flags & MMC_RSP_136) {
cmd->resp[0] = meson_mx_sdhc_read_response(host, 4);
@@ -625,6 +630,16 @@ static irqreturn_t meson_mx_sdhc_irq_thread(int irq, void *irq_data)
*/
meson_mx_sdhc_clear_fifo(host->mmc);

+done:
+ if (ret && !cmd->error)
+ cmd->error = ret;
+ if (data) {
+ dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
+ mmc_get_dma_dir(data));
+ if (!cmd->error && !data->error)
+ data->bytes_xfered = data->blksz * data->blocks;
+ }
+
meson_mx_sdhc_request_done(host);

return IRQ_HANDLED;
--
2.54.0.windows.1