mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mmc: sh_mmcif: rework sg_miter handling
@ 2026-11-18 12:57 Arnd Bergmann
  2026-11-18 17:47 ` Linus Walleij
  0 siblings, 1 reply; 4+ messages in thread
From: Arnd Bergmann @ 2026-11-18 12:57 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Arnd Bergmann, Linus Walleij, Geert Uytterhoeven, Ulf Hansson,
	Frank Li, Adrian Hunter, linux-mmc, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

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.

Cc: Linus Walleij <linusw@kernel.org>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Wolfram Sang <wsa+renesas@sang-engineering.com>
Fixes: 4d4a2c71b862 ("mmc: sh_mmcif: Advance sg_miter before reading blocks")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
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(-)

diff --git a/drivers/mmc/host/sh_mmcif.c b/drivers/mmc/host/sh_mmcif.c
index aa5a19d16c7f..7f1818831f93 100644
--- a/drivers/mmc/host/sh_mmcif.c
+++ b/drivers/mmc/host/sh_mmcif.c
@@ -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);
-- 
2.53.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mmc: sh_mmcif: rework sg_miter handling
  2026-11-18 12:57 [PATCH] mmc: sh_mmcif: rework sg_miter handling Arnd Bergmann
@ 2026-11-18 17:47 ` Linus Walleij
  2026-11-18 17:50   ` Arnd Bergmann
  0 siblings, 1 reply; 4+ messages in thread
From: Linus Walleij @ 2026-11-18 17:47 UTC (permalink / raw)
  To: Arnd Bergmann, Geert Uytterhoeven
  Cc: Wolfram Sang, Arnd Bergmann, Ulf Hansson, Frank Li,
	Adrian Hunter, linux-mmc, linux-kernel

Hi Arnd,

On Wed, Nov 18, 2026 at 2:45 PM Arnd Bergmann <arnd@kernel.org> wrote:

> From: Arnd Bergmann <arnd@arndb.de>
>
> 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.
>
> Cc: Linus Walleij <linusw@kernel.org>
> Cc: Geert Uytterhoeven <geert+renesas@glider.be>
> Cc: Wolfram Sang <wsa+renesas@sang-engineering.com>
> Fixes: 4d4a2c71b862 ("mmc: sh_mmcif: Advance sg_miter before reading blocks")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>

FWIW:
Acked-by: Linus Walleij <linusw@kernel.org>

> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mmc: sh_mmcif: rework sg_miter handling
  2026-11-18 17:47 ` Linus Walleij
@ 2026-11-18 17:50   ` Arnd Bergmann
  2026-11-18 20:17     ` Wolfram Sang
  0 siblings, 1 reply; 4+ messages in thread
From: Arnd Bergmann @ 2026-11-18 17:50 UTC (permalink / raw)
  To: Linus Walleij, Arnd Bergmann, Geert Uytterhoeven
  Cc: Wolfram Sang, Ulf Hansson, Frank Li, Adrian Hunter,
	linux-mmc @ vger . kernel . org, linux-kernel

On Wed, Nov 18, 2026, at 18:47, Linus Walleij wrote:
> Hi Arnd,
>
> On Wed, Nov 18, 2026 at 2:45 PM Arnd Bergmann <arnd@kernel.org> wrote:
>
>> From: Arnd Bergmann <arnd@arndb.de>
>>
>> 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.
>>
>> Cc: Linus Walleij <linusw@kernel.org>
>> Cc: Geert Uytterhoeven <geert+renesas@glider.be>
>> Cc: Wolfram Sang <wsa+renesas@sang-engineering.com>
>> Fixes: 4d4a2c71b862 ("mmc: sh_mmcif: Advance sg_miter before reading blocks")
>> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
>
> FWIW:
> Acked-by: Linus Walleij <linusw@kernel.org>

Thanks!

>> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mmc: sh_mmcif: rework sg_miter handling
  2026-11-18 17:50   ` Arnd Bergmann
@ 2026-11-18 20:17     ` Wolfram Sang
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2026-11-18 20:17 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Linus Walleij, Arnd Bergmann, Geert Uytterhoeven, Ulf Hansson,
	Frank Li, Adrian Hunter, linux-mmc @ vger . kernel . org,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 334 bytes --]

Hi Arnd, Linus,

> 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


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18 20:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 12:57 [PATCH] mmc: sh_mmcif: rework sg_miter handling Arnd Bergmann
2026-11-18 17:47 ` Linus Walleij
2026-11-18 17:50   ` Arnd Bergmann
2026-11-18 20:17     ` Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®