mmc: sh_mmcif: rework sg_miter handling
PATCH 1/1 mmc: sh_mmcif: rework sg_miter handling
From: Arnd Bergmann <[email protected]>
This driver is the only one that uses sg_miter_next() to get a persistent mapping across interrupts, which prevents us from converting the internal kmap() to a local mapping using SG_MITER_LOCAL flag.
Linus previously attempted to use atomic mappings here, which failed for the same reason. I gave it another try, with a mapping that is only active during a single sh_mmcif_mread_block()/sh_mmcif_mwrite_block() call.
This is mainly a revert of the patch that fixed the command 18 timeout, but tries to solve the problem by updating and checking data->bytes_xfered to keep track of when the transfer is finished.
Instead of calling sg_miter_stop() on error or completion, this now always stops the iterator after the data transfer and restarts it during the following interrupt.
---
Hi Wolfram and Linus,
Could you have a look at this? I'm not familiar with this driver, so there is a chance I got this completely wrong. If the change does make sense, maybe you can try testing it on one of the highmem-enabled boards.
drivers/mmc/host/sh_mmcif.c | 63 +++++++++++++------------------------ 1 file changed, 22 insertions(+), 41 deletions(-)
drivers/mmc/host/sh_mmcif.c +22 -41
@@ -604,7 +604,7 @@ static void sh_mmcif_single_read(struct sh_mmcif_host *host, BLOCK_SIZE_MASK) + 3;sg_miter_start (&host->sg_miter, data->sg, data->sg_len, -SG_MITER_TO_SG ); +SG_MITER_LOCAL |SG_MITER_TO_SG ); host->wait_for =MMCIF_WAIT_FOR_READ ;
@@ -621,7 +621,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)int i;if (host->sd_error) { -sg_miter_stop (sgm); data->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, data->error);return false;
@@ -629,7 +628,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)if (!sg_miter_next (sgm)) {/* This should not happen on single blocks */ -sg_miter_stop (sgm);return false; }
@@ -638,6 +636,8 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)for (i =0 ; i < host->blocksize /4 ; i++) *p++ =sh_mmcif_readl (host->addr,MMCIF_CE_DATA ); + data->bytes_xfered += host->blocksize; + sgm->consumed = host->blocksize;sg_miter_stop (&host->sg_miter);/* buffer read end */
@@ -650,7 +650,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)static void sh_mmcif_multi_read (struct sh_mmcif_host *host,struct mmc_request *mrq) { -struct sg_mapping_iter *sgm = &host->sg_miter;struct mmc_data *data = mrq->data;if (!data->sg_len || !data->sg->length)
@@ -659,14 +658,8 @@ static void sh_mmcif_multi_read(struct sh_mmcif_host *host, host->blocksize =sh_mmcif_readl (host->addr,MMCIF_CE_BLOCK_SET ) &BLOCK_SIZE_MASK ; -sg_miter_start (sgm, data->sg, data->sg_len, -SG_MITER_TO_SG ); - -/* Advance to the first sglist entry */ -if (!sg_miter_next (sgm)) { -sg_miter_stop (sgm); -return ; - } +sg_miter_start (&host->sg_miter, data->sg, data->sg_len, +SG_MITER_LOCAL |SG_MITER_TO_SG ); host->wait_for =MMCIF_WAIT_FOR_MREAD ;
@@ -682,27 +675,26 @@ static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)int i;if (host->sd_error) { -sg_miter_stop (sgm); data->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, data->error);return false; } +if (!sg_miter_next (sgm)) +return false; + p = sgm->addr;for (i =0 ; i < host->blocksize /4 ; i++) *p++ =sh_mmcif_readl (host->addr,MMCIF_CE_DATA ); + data->bytes_xfered += host->blocksize; sgm->consumed = host->blocksize; +sg_miter_stop (sgm);sh_mmcif_bitset (host,MMCIF_CE_INT_MASK ,MASK_MBUFREN ); -if (!sg_miter_next (sgm)) { -sg_miter_stop (sgm); -return false; - } - -return true; +return data->bytes_xfered < host->blocksize * data->blocks; } static void sh_mmcif_single_write(struct sh_mmcif_host *host,
@@ -714,7 +706,7 @@ static void sh_mmcif_single_write(struct sh_mmcif_host *host, BLOCK_SIZE_MASK) + 3;sg_miter_start (&host->sg_miter, data->sg, data->sg_len, -SG_MITER_FROM_SG ); +SG_MITER_LOCAL |SG_MITER_FROM_SG ); host->wait_for =MMCIF_WAIT_FOR_WRITE ;
@@ -731,7 +723,6 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)int i;if (host->sd_error) { -sg_miter_stop (sgm); data->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, data->error);return false;
@@ -739,7 +730,6 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)if (!sg_miter_next (sgm)) {/* This should not happen on single blocks */ -sg_miter_stop (sgm);return false; }
@@ -748,6 +738,8 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)for (i =0 ; i < host->blocksize /4 ; i++)sh_mmcif_writel (host->addr,MMCIF_CE_DATA , *p++); + data->bytes_xfered += host->blocksize; + sgm->consumed = host->blocksize;sg_miter_stop (&host->sg_miter);/* buffer write end */
@@ -760,7 +752,6 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)static void sh_mmcif_multi_write (struct sh_mmcif_host *host,struct mmc_request *mrq) { -struct sg_mapping_iter *sgm = &host->sg_miter;struct mmc_data *data = mrq->data;if (!data->sg_len || !data->sg->length)
@@ -769,14 +760,8 @@ static void sh_mmcif_multi_write(struct sh_mmcif_host *host, host->blocksize =sh_mmcif_readl (host->addr,MMCIF_CE_BLOCK_SET ) &BLOCK_SIZE_MASK ; -sg_miter_start (sgm, data->sg, data->sg_len, -SG_MITER_FROM_SG ); - -/* Advance to the first sglist entry */ -if (!sg_miter_next (sgm)) { -sg_miter_stop (sgm); -return ; - } +sg_miter_start (&host->sg_miter, data->sg, data->sg_len, +SG_MITER_LOCAL |SG_MITER_FROM_SG ); host->wait_for =MMCIF_WAIT_FOR_MWRITE ;
@@ -792,27 +777,26 @@ static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)int i;if (host->sd_error) { -sg_miter_stop (sgm); data->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, data->error);return false; } +if (!sg_miter_next (sgm)) +return false; + p = sgm->addr;for (i =0 ; i < host->blocksize /4 ; i++)sh_mmcif_writel (host->addr,MMCIF_CE_DATA , *p++); + data->bytes_xfered += host->blocksize; sgm->consumed = host->blocksize; - -if (!sg_miter_next (sgm)) { -sg_miter_stop (sgm); -return false; - } +sg_miter_stop (sgm);sh_mmcif_bitset (host,MMCIF_CE_INT_MASK ,MASK_MBUFWEN ); -return true; +return data->bytes_xfered < host->blocksize * data->blocks; } static void sh_mmcif_get_response(struct sh_mmcif_host *host,
@@ -1296,9 +1280,6 @@ static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)if (host->wait_for !=MMCIF_WAIT_FOR_STOP ) {struct mmc_data *data = mrq->data; -if (!mrq->cmd->error && data && !data->error) - data->bytes_xfered = - data->blocks * data->blksz;if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {sh_mmcif_stop_cmd (host, mrq);
Signature · 1 line
2.53.0
Hi Arnd,
Quoted 18 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
This driver is the only one that uses
sg_miter_next()to get a persistent mapping across interrupts, which prevents us from converting the internalkmap()to a local mapping usingSG_MITER_LOCALflag.Linus previously attempted to use atomic mappings here, which failed for the same reason. I gave it another try, with a mapping that is only active during a single sh_mmcif_mread_block()/sh_mmcif_mwrite_block() call.
This is mainly a revert of the patch that fixed the command 18 timeout, but tries to solve the problem by updating and checking
data->bytes_xferedto keep track of when the transfer is finished.Instead of calling
sg_miter_stop()on error or completion, this now always stops the iterator after the data transfer and restarts it during the following interrupt.
Quoted 5 lines
Cc Linus Walleij <[email protected]>Cc Geert Uytterhoeven <[email protected]>Cc Wolfram Sang <[email protected]>Fixes 4d4a2c71b862 ("mmc: sh_mmcif: Advance sg_miter before reading blocks")Signed-off-by Arnd Bergmann <[email protected]>
FWIW:
Quoted 6 lines
Hi Wolfram and Linus,
Could you have a look at this? I'm not familiar with this driver, so there is a chance I got this completely wrong. If the change does make sense, maybe you can try testing it on one of the highmem-enabled boards.
I don't have this hardware, but Geert does (he helped me with the previous patches to this driver), so it'd be great if he could give it a spin!
Yours,
Linus Walleij
Quoted 22 lines · Linus Walleij
Hi Arnd,
Quoted 18 lines · Arnd Bergmann
From: Arnd Bergmann <[email protected]>
This driver is the only one that uses
sg_miter_next()to get a persistent mapping across interrupts, which prevents us from converting the internalkmap()to a local mapping usingSG_MITER_LOCALflag.Linus previously attempted to use atomic mappings here, which failed for the same reason. I gave it another try, with a mapping that is only active during a single sh_mmcif_mread_block()/sh_mmcif_mwrite_block() call.
This is mainly a revert of the patch that fixed the command 18 timeout, but tries to solve the problem by updating and checking
data->bytes_xferedto keep track of when the transfer is finished.Instead of calling
sg_miter_stop()on error or completion, this now always stops the iterator after the data transfer and restarts it during the following interrupt.
Quoted 8 lines
Quoted 5 lines
Cc Linus Walleij <[email protected]>Cc Geert Uytterhoeven <[email protected]>Cc Wolfram Sang <[email protected]>Fixes 4d4a2c71b862 ("mmc: sh_mmcif: Advance sg_miter before reading blocks")Signed-off-by Arnd Bergmann <[email protected]>FWIW:
Acked-by Linus Walleij <[email protected]>
Thanks!
Quoted 10 lines
Quoted 6 lines
Hi Wolfram and Linus,
Could you have a look at this? I'm not familiar with this driver, so there is a chance I got this completely wrong. If the change does make sense, maybe you can try testing it on one of the highmem-enabled boards.
I don't have this hardware, but Geert does (he helped me with the previous patches to this driver), so it'd be great if he could give it a spin!
I'll post a v2 tomorrow with the bugs addressed that Sashiko found, no point testing this version. Maybe Wolfram can still take a look.
Arnd
Quoted 61 lines · On Wed, Nov 18, 2026, at 15:11, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] mmc:sh_mmcif: DMA transfers leavedata->bytes_xferedat 0
- [High] mmc:sh_mmcif: blksz + 3 insh_mmcif_read_block()andsh_mmcif_write_block()
- [Medium] mmc:sh_mmcif: PIO leavesdata->bytes_xferednon-zero on transfer error
--commit
6f62463df398beb43ce5f1bf65cbfc2642c791de
Author: Arnd Bergmann <[email protected]>mmc:
sh_mmcif: reworksg_miterhandlingThis commit converts the
sh_mmcifPIO scatterlist iterator to useSG_MITER_LOCALby mapping and stopping the iterator within each block transfer helper and tracking progress withdata->bytes_xfered.Quoted 30 lines
drivers/mmc/host/sh_mmcif.c +1
-3@@ -604,7 +604,7 @@ static void sh_mmcif_single_read(struct sh_mmcif_host *host, BLOCK_SIZE_MASK) + 3;sg_miter_start (&host->sg_miter, data->sg, data->sg_len, -SG_MITER_TO_SG ); +SG_MITER_LOCAL |SG_MITER_TO_SG ); host->wait_for =MMCIF_WAIT_FOR_READ ;@@ -621,7 +621,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)int i;if (host->sd_error) { -sg_miter_stop (sgm); data->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, data->error);return false;@@ -629,7 +628,6 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)if (!sg_miter_next (sgm)) {/* This should not happen on single blocks */ -sg_miter_stop (sgm);return false; }drivers/mmc/host/sh_mmcif.c static bool sh_mmcif_read_block(struct sh_mmcif_host *host) · line 641
Quoted 6 lines
drivers/mmc/host/sh_mmcif.c +2
-0@@ -638,6 +636,8 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)for (i =0 ; i < host->blocksize /4 ; i++) *p++ =sh_mmcif_readl (host->addr,MMCIF_CE_DATA ); + data->bytes_xfered += host->blocksize; + sgm->consumed = host->blocksize;sg_miter_stop (&host->sg_miter);[Severity: High]
Does settingsgm->consumedanddata->bytes_xferedtohost->blocksizehere (and insh_mmcif_write_block()) triggerWARN_ON(miter->consumed > miter->length)insg_miter_stop()and over-reportdata->bytes_xferedby 3 bytes?
Quoted 4 lines
sh_mmcif_single_read()andsh_mmcif_single_write()initializehost->blocksizeto (MMCIF_CE_BLOCK_SET&BLOCK_SIZE_MASK) + 3 to round up the 32-bit word loop, sohost->blocksizeisdata->blksz+ 3 whilesgm->lengthis onlydata->blksz.
I've changed this to no longer refer to host->blocksize now:
drivers/mmc/host/sh_mmcif.c +10 -10
@@ -636,8 +636,8 @@ static bool sh_mmcif_read_block(struct sh_mmcif_host *host)for (i =0 ; i < host->blocksize /4 ; i++) *p++ =sh_mmcif_readl (host->addr,MMCIF_CE_DATA ); - data->bytes_xfered += host->blocksize; - sgm->consumed = host->blocksize; + data->bytes_xfered +=4 *i; + sgm->consumed =4 *i;sg_miter_stop (&host->sg_miter);/* buffer read end */
@@ -688,13 +688,13 @@ static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)for (i =0 ; i < host->blocksize /4 ; i++) *p++ =sh_mmcif_readl (host->addr,MMCIF_CE_DATA ); - data->bytes_xfered += host->blocksize; - sgm->consumed = host->blocksize; + data->bytes_xfered +=4 *i; + sgm->consumed =4 *i;sg_miter_stop (sgm);sh_mmcif_bitset (host,MMCIF_CE_INT_MASK ,MASK_MBUFREN ); -return data->bytes_xfered < host->blocksize * data->blocks; +return data->bytes_xfered < data->blksz * data->blocks; } static void sh_mmcif_single_write(struct sh_mmcif_host *host,
@@ -738,8 +738,8 @@ static bool sh_mmcif_write_block(struct sh_mmcif_host *host)for (i =0 ; i < host->blocksize /4 ; i++)sh_mmcif_writel (host->addr,MMCIF_CE_DATA , *p++); - data->bytes_xfered += host->blocksize; - sgm->consumed = host->blocksize; + data->bytes_xfered +=4 * i; + sgm->consumed =4 * i;sg_miter_stop (&host->sg_miter);/* buffer write end */
@@ -790,13 +790,13 @@ static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)for (i =0 ; i < host->blocksize /4 ; i++)sh_mmcif_writel (host->addr,MMCIF_CE_DATA , *p++); - data->bytes_xfered += host->blocksize; - sgm->consumed = host->blocksize; + data->bytes_xfered +=4 * i; + sgm->consumed =4 * i;sg_miter_stop (sgm);sh_mmcif_bitset (host,MMCIF_CE_INT_MASK ,MASK_MBUFWEN ); -return data->bytes_xfered < host->blocksize * data->blocks; +return data->bytes_xfered < data->blksz * data->blocks; } static void sh_mmcif_get_response(struct sh_mmcif_host *host,
Quoted 27 lines
Quoted 27 lines
drivers/mmc/host/sh_mmcif.c +5
-6@@ -792,27 +777,26 @@ static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)int i;if (host->sd_error) { -sg_miter_stop (sgm); data->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, data->error);return false; } +if (!sg_miter_next (sgm)) +return false; + p = sgm->addr;for (i =0 ; i < host->blocksize /4 ; i++)sh_mmcif_writel (host->addr,MMCIF_CE_DATA , *p++); + data->bytes_xfered += host->blocksize; sgm->consumed = host->blocksize; - -if (!sg_miter_next (sgm)) { -sg_miter_stop (sgm); -return false; - } +sg_miter_stop (sgm);
Quoted 20 lines
drivers/mmc/host/sh_mmcif.c static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host) · line 800
Quoted 5 lines
drivers/mmc/host/sh_mmcif.c +1
-1sh_mmcif_bitset (host,MMCIF_CE_INT_MASK ,MASK_MBUFWEN ); -return true; +return data->bytes_xfered < host->blocksize * data->blocks; }[Severity: Medium]>
Signature · 12 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1 What happens if a PIO transfer encounters an error or timeout after one or more blocks have been transferred to or from MMCIF_CE_DATA? In the single-block and multi-block PIO helpers, data->bytes_xfered is incremented as soon as the controller FIFO is accessed, before MMCIF_WAIT_FOR_READ_END, MMCIF_WAIT_FOR_WRITE_END, or MMCIF_WAIT_FOR_STOP completes. If a later step or sh_mmcif_timeout_work() fails, data->error (or mrq->stop->error) is set while data->bytes_xfered remains non-zero, which causes mmc_blk_mq_rw_recovery() and mmc_blk_mq_complete_rq() to treat those unconfirmed bytes as successfully transferred with BLK_STS_OK.
Not sure what we actually want data->bytes_xfered to be in this case, I would guess that if the FIFO access went through, we should count it, but to be on the safe side I can reset the value in the error case and leave the behavior unchanged here:
drivers/mmc/host/sh_mmcif.c +3 -0
@@ -1253,6 +1253,7 @@ static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)break ;case MMCIF_WAIT_FOR_STOP :if (host->sd_error) { + mrq->data->bytes_xferred =0 ; mrq->stop->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, mrq->stop->error);break ;
@@ -1263,6 +1264,7 @@ static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)case MMCIF_WAIT_FOR_READ_END :case MMCIF_WAIT_FOR_WRITE_END :if (host->sd_error) { + mrq->data->bytes_xferred =0 ; mrq->data->error =sh_mmcif_error_manage (host);dev_dbg (dev,"%s(): %d\n" , __func__, mrq->data->error); }
@@ -1378,6 +1380,7 @@ static void sh_mmcif_timeout_work(struct work_struct *work)case MMCIF_WAIT_FOR_WRITE :case MMCIF_WAIT_FOR_READ_END :case MMCIF_WAIT_FOR_WRITE_END : + mrq->data->bytes_xferred =0 ; mrq->data->error =sh_mmcif_error_manage (host);break ;default :
Quoted 32 lines
drivers/mmc/host/sh_mmcif.c static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id) · line 1285
Quoted 10 lines
drivers/mmc/host/sh_mmcif.c +0
-3@@ -1296,9 +1280,6 @@ static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)if (host->wait_for !=MMCIF_WAIT_FOR_STOP ) {struct mmc_data *data = mrq->data; -if (!mrq->cmd->error && data && !data->error) - data->bytes_xfered = - data->blocks * data->blksz;if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {sh_mmcif_stop_cmd (host, mrq);[Severity: High]
Does removing this assignment leavedata->bytes_xferedat 0 for all DMA transfers?When DMA is used,
sh_mmcif_end_cmd()waits forhost->dma_completesynchronously and never calls the PIO block helpers, and only touchesdata->bytes_xferedto zero it on error:drivers/mmc/host/sh_mmcif.c:sh_mmcif_end_cmd() { ... if (data->error) { data->bytes_xfered = 0; /* Abort DMA */ if (data->flags & MMC_DATA_READ) dmaengine_terminate_sync(host->chan_rx); else dmaengine_terminate_sync(host->chan_tx); } ... }
Quoted 2 lines
Every successful DMA read or write will therefore complete with
data->bytes_xfered== 0 and fail in the MMC block layer.
I see. I could either put back the original line, or move it into sh_mmcif_end_cmd() for the DMA code path only:
@@ -1178,6 +1178,8 @@ static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
dmaengine_terminate_sync(host->chan_rx);
else
dmaengine_terminate_sync(host->chan_tx);
+ } else {
+ data->bytes_xfered = data->blksz * data->blocks;
}
/*Let me know if you have a preference, otherwise I'll go with this one.
Arnd
Hi Arnd, Linus,
Quoted 2 lines
I'll post a v2 tomorrow with the bugs addressed that Sashiko found, no point testing this version. Maybe Wolfram can still take a look.
I surely can do testing, the Lager board I have should be suitable. Reviewing - I'll try my best with v2, this is all ages ago...
Thanks for taking care of this!
Wolfram