]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mmc: core: add undervoltage handler for MMC/eMMC devices
authorOleksij Rempel <o.rempel@pengutronix.de>
Thu, 21 Aug 2025 13:07:51 +0000 (15:07 +0200)
committerUlf Hansson <ulf.hansson@linaro.org>
Fri, 22 Aug 2025 10:09:22 +0000 (12:09 +0200)
Add infrastructure to handle regulator undervoltage events for MMC/eMMC
cards. When an undervoltage is detected, the new handler performs a
controlled emergency suspend using a short power-off notification,
skipping the cache flush to maximize the chance of a safe shutdown.
After the operation, the card is marked as removed to prevent further
I/O and possible data corruption.

This is implemented by introducing MMC_POWEROFF_UNDERVOLTAGE to the
mmc_poweroff_type enum and refactoring the suspend logic into an
internal __mmc_suspend() helper that allows the caller to skip the cache
flush if required. The undervoltage handler is registered as a bus
operation and invoked from the core undervoltage path.

If power-off notification is not supported by the card, the handler
falls back to sleep or deselecting the card.

Additionally, update the shutdown path to avoid redundant shutdown
steps if the card is already removed

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/r/20250821130751.2089587-3-o.rempel@pengutronix.de
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
drivers/mmc/core/mmc.c

index 5be9b42d5057eb7d0bd5840a15a93119716724cf..3e7d9437477c76ec8e423a11c13427917e154b96 100644 (file)
@@ -36,6 +36,7 @@
 enum mmc_poweroff_type {
        MMC_POWEROFF_SUSPEND,
        MMC_POWEROFF_SHUTDOWN,
+       MMC_POWEROFF_UNDERVOLTAGE,
        MMC_POWEROFF_UNBIND,
 };
 
@@ -2132,9 +2133,15 @@ static int _mmc_suspend(struct mmc_host *host, enum mmc_poweroff_type pm_type)
        if (mmc_card_suspended(host->card))
                goto out;
 
-       err = _mmc_flush_cache(host);
-       if (err)
-               goto out;
+       /*
+        * For the undervoltage case, we care more about device integrity.
+        * Avoid cache flush and notify the device to power off quickly.
+        */
+       if (pm_type != MMC_POWEROFF_UNDERVOLTAGE) {
+               err = _mmc_flush_cache(host);
+               if (err)
+                       goto out;
+       }
 
        if (mmc_card_can_poweroff_notify(host->card) &&
            mmc_host_can_poweroff_notify(host, pm_type))
@@ -2212,6 +2219,13 @@ static int mmc_shutdown(struct mmc_host *host)
 {
        int err = 0;
 
+       /*
+        * In case of undervoltage, the card will be powered off (removed) by
+        * _mmc_handle_undervoltage()
+        */
+       if (mmc_card_removed(host->card))
+               return 0;
+
        /*
         * If the card remains suspended at this point and it was done by using
         * the sleep-cmd (CMD5), we may need to re-initialize it first, to allow
@@ -2302,6 +2316,55 @@ static int _mmc_hw_reset(struct mmc_host *host)
        return mmc_init_card(host, card->ocr, card);
 }
 
+/**
+ * _mmc_handle_undervoltage - Handle an undervoltage event for MMC/eMMC devices
+ * @host: MMC host structure
+ *
+ * This function is triggered when an undervoltage condition is detected.
+ * It attempts to transition the device into a low-power or safe state to
+ * prevent data corruption.
+ *
+ * Steps performed:
+ * - Perform an emergency suspend using EXT_CSD_POWER_OFF_SHORT if possible.
+ *    - If power-off notify is not supported, fallback mechanisms like sleep or
+ *      deselecting the card are attempted.
+ *    - Cache flushing is skipped to reduce execution time.
+ * - Mark the card as removed to prevent further interactions after
+ *    undervoltage.
+ *
+ * Note: This function does not handle host claiming or releasing. The caller
+ *      must ensure that the host is properly claimed before calling this
+ *      function and released afterward.
+ *
+ * Returns: 0 on success, or a negative error code if any step fails.
+ */
+static int _mmc_handle_undervoltage(struct mmc_host *host)
+{
+       struct mmc_card *card = host->card;
+       int err;
+
+       /*
+        * Perform an emergency suspend to power off the eMMC quickly.
+        * This ensures the device enters a safe state before power is lost.
+        * We first attempt EXT_CSD_POWER_OFF_SHORT, but if power-off notify
+        * is not supported, we fall back to sleep mode or deselecting the card.
+        * Cache flushing is skipped to minimize delay.
+        */
+       err = _mmc_suspend(host, MMC_POWEROFF_UNDERVOLTAGE);
+       if (err)
+               pr_err("%s: undervoltage suspend failed: %pe\n",
+                      mmc_hostname(host), ERR_PTR(err));
+
+       /*
+        * Mark the card as removed to prevent further operations.
+        * This ensures the system does not attempt to access the device
+        * after an undervoltage event, avoiding potential corruption.
+        */
+       mmc_card_set_removed(card);
+
+       return err;
+}
+
 static const struct mmc_bus_ops mmc_ops = {
        .remove = mmc_remove,
        .detect = mmc_detect,
@@ -2314,6 +2377,7 @@ static const struct mmc_bus_ops mmc_ops = {
        .hw_reset = _mmc_hw_reset,
        .cache_enabled = _mmc_cache_enabled,
        .flush_cache = _mmc_flush_cache,
+       .handle_undervoltage = _mmc_handle_undervoltage,
 };
 
 /*