diff mbox series

[5/5] mmc: core: Add support for graceful host removal for SD

Message ID 20250320140040.162416-6-ulf.hansson@linaro.org (mailing list archive)
State New
Headers show
Series mmc: core: Add support for graceful host removal for eMMC/SD | expand

Commit Message

Ulf Hansson March 20, 2025, 2 p.m. UTC
An mmc host driver may allow to unbind from its corresponding host device.
If an SD card is attached to the host, the mmc core will just try to cut
the power for it, without obeying to the SD spec that potentially may
damage the card.

Let's fix this problem by implementing a graceful power-down of the card at
host removal.

Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
---
 drivers/mmc/core/sd.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

Comments

Wolfram Sang March 31, 2025, 8:30 a.m. UTC | #1
On Thu, Mar 20, 2025 at 03:00:36PM +0100, Ulf Hansson wrote:
> An mmc host driver may allow to unbind from its corresponding host device.
> If an SD card is attached to the host, the mmc core will just try to cut
> the power for it, without obeying to the SD spec that potentially may
> damage the card.
> 
> Let's fix this problem by implementing a graceful power-down of the card at
> host removal.
> 
> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>

Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Testing needs another day, can't do this via remotelab.
Wolfram Sang April 1, 2025, 8:50 a.m. UTC | #2
On Mon, Mar 31, 2025 at 10:30:35AM +0200, Wolfram Sang wrote:
> On Thu, Mar 20, 2025 at 03:00:36PM +0100, Ulf Hansson wrote:
> > An mmc host driver may allow to unbind from its corresponding host device.
> > If an SD card is attached to the host, the mmc core will just try to cut
> > the power for it, without obeying to the SD spec that potentially may
> > damage the card.
> > 
> > Let's fix this problem by implementing a graceful power-down of the card at
> > host removal.
> > 
> > Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
> 
> Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

I couldn't find a card yet which has SD_EXT_POWER_OFF_NOTIFY set, but at
least I can confirm that the actual code paths are taken:

Tested-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Wolfram Sang April 1, 2025, 8:51 a.m. UTC | #3
> Tested-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Which also means that I tested the whole series on a Renesas Salvator-X
board with a R-Car M3-W SoC (Gen3).
Ulf Hansson April 1, 2025, 11:51 a.m. UTC | #4
On Tue, 1 Apr 2025 at 10:51, Wolfram Sang
<wsa+renesas@sang-engineering.com> wrote:
>
>
> > Tested-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
>
> Which also means that I tested the whole series on a Renesas Salvator-X
> board with a R-Car M3-W SoC (Gen3).
>

Great thanks! I will send a new version addressing your comments on patch2.

Kind regards
Uffe
diff mbox series

Patch

diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index 8eba697d3d86..cb4254a43f85 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -1596,15 +1596,6 @@  static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
 	return err;
 }
 
-/*
- * Host is being removed. Free up the current card.
- */
-static void mmc_sd_remove(struct mmc_host *host)
-{
-	mmc_remove_card(host->card);
-	host->card = NULL;
-}
-
 /*
  * Card detection - card is alive.
  */
@@ -1630,7 +1621,8 @@  static void mmc_sd_detect(struct mmc_host *host)
 	mmc_put_card(host->card, NULL);
 
 	if (err) {
-		mmc_sd_remove(host);
+		mmc_remove_card(host->card);
+		host->card = NULL;
 
 		mmc_claim_host(host);
 		mmc_detach_bus(host);
@@ -1730,6 +1722,19 @@  static int _mmc_sd_suspend(struct mmc_host *host)
 	return err;
 }
 
+/*
+ * Host is being removed. Free up the current card and do a graceful power-off.
+ */
+static void mmc_sd_remove(struct mmc_host *host)
+{
+	get_device(&host->card->dev);
+	mmc_remove_card(host->card);
+
+	_mmc_sd_suspend(host);
+
+	put_device(&host->card->dev);
+	host->card = NULL;
+}
 /*
  * Callback for suspend
  */