From nobody Thu Oct 9 20:24:06 2025 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF8462882DE for ; Mon, 16 Jun 2025 10:29:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; cv=none; b=qCt8W9+f+N9WU5i2w9DYWbFpf0vrxUHq9R26Avz0mphB/Omztri/MjfzfcO0gsltVBv3zFmKhNhEgUhVettYybq5xzsV6gz0Xq4HwscoTBVTCkIcc+ctB9Dkq3I0d7xnjk22DXx9F9I+5yp1jxHR0Du/fj7EE24wgaucgdRdfgY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; c=relaxed/simple; bh=Q/bz7cLGnxrkgw49KB7Wdcbln1bJdw/wxLlmVPiNTdQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=J9bCR7CjL57C8CU2rYNq+F3Ta1+StSEFnbwh68aK0bdHQXkmNBeZUZbpopUyg4Mh9ZAiaLnzxm/pDK+hoy0KEkvSBdxtEc+jEkKYxxJRCyyD3ISXOVvOug6J2tFzJxqbqHH6+PHHzTJe/6NhuGrMZqh9VVYqA2ZAMJfSS42RR+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uR75b-0008T0-0A; Mon, 16 Jun 2025 12:29:19 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uR75a-003n2x-0O; Mon, 16 Jun 2025 12:29:18 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uR75a-0072Is-0A; Mon, 16 Jun 2025 12:29:18 +0200 From: Oleksij Rempel To: Ulf Hansson Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, Greg Kroah-Hartman , Mark Brown , "Rafael J. Wysocki" , =?UTF-8?q?S=C3=B8ren=20Andersen?= , Christian Loehle , Adrian Hunter , Avri Altman Subject: [PATCH v6 1/4] mmc: core: Add infrastructure for undervoltage handling Date: Mon, 16 Jun 2025 12:29:14 +0200 Message-Id: <20250616102917.1677116-2-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250616102917.1677116-1-o.rempel@pengutronix.de> References: <20250616102917.1677116-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implement the core infrastructure to allow MMC bus types to handle REGULATOR_EVENT_UNDER_VOLTAGE events from power regulators. This is primarily aimed at allowing devices like eMMC to perform an emergency shutdown to prevent data corruption when a power failure is imminent. This patch introduces: - A new 'handle_undervoltage' function pointer to 'struct mmc_bus_ops'. Bus drivers (e.g., for eMMC) can implement this to define their emergency procedures. - A workqueue ('uv_work') in 'struct mmc_supply' to handle the event asynchronously in a high-priority context. - A new function 'mmc_handle_undervoltage()' which is called from the workqueue. It stops the host queue to prevent races with card removal, checks for the bus op, and invokes the handler. - Functions to register and unregister the regulator notifier, intended to be called by bus drivers like 'mmc_attach_mmc' when a compatible card is detected. The notifier is only registered for the main 'vmmc' supply, as undervoltage handling for 'vqmmc' or 'vqmmc2' is not required at this time. Signed-off-by: Oleksij Rempel --- changes v6: - Rewrite commit message to be more technical per reviewer feedback. - Address race conditions by using __mmc_stop_host() instead of only claiming the host in the undervoltage handler. - Move notifier registration from mmc_regulator_get_supply() to the end of a successful card initialization in mmc_attach_mmc(), ensuring it only runs for capable cards. - Centralize notifier unregistration in mmc_remove_card() to correctly handle all card removal and error paths. - Add 'undervoltage_notify_registered' flag to struct mmc_host to reliably track the notifier state. - Consolidate multiple notifier callbacks into a single, generic handler. - Remove premature notifier support for vqmmc and vqmmc2 regulators. - Move INIT_WORK() for the undervoltage workqueue to mmc_alloc_host(). changes v3: - filter supported cards at early stage - add locking in mmc_handle_regulator_event() - claim/release host in mmc_handle_undervoltage() Background & Decision at LPC24: This solution was proposed and refined during LPC24 in the talk "Graceful Under Pressure: Prioritizing Shutdown to Protect Your Data in Embedded Systems," which aimed to address how Linux should handle power fluctuations in embedded devices to prevent data corruption or storage damage. At the time, multiple possible solutions were considered: 1. Triggering a system-wide suspend or shutdown: when undervoltage is detected, with device-specific prioritization to ensure critical components shut down first. - This approach was disliked by Greg Kroah-Hartman, as it introduced complexity and was not suitable for all use cases. 2. Notifying relevant devices through the regulator framework: to allow graceful per-device handling. - This approach was agreed upon as the most acceptable by participants in the discussion, including Greg Kroah-Hartman, Mark Brown, and Rafael J. Wysocki. - This patch implements that decision by integrating undervoltage handling into the MMC subsystem. --- drivers/mmc/core/bus.c | 5 +++ drivers/mmc/core/core.c | 33 +++++++++++++++ drivers/mmc/core/core.h | 2 + drivers/mmc/core/host.c | 3 ++ drivers/mmc/core/mmc.c | 6 +++ drivers/mmc/core/regulator.c | 82 ++++++++++++++++++++++++++++++++++++ include/linux/mmc/host.h | 10 +++++ 7 files changed, 141 insertions(+) diff --git a/drivers/mmc/core/bus.c b/drivers/mmc/core/bus.c index 1cf64e0952fb..aa3454cb0702 100644 --- a/drivers/mmc/core/bus.c +++ b/drivers/mmc/core/bus.c @@ -394,6 +394,11 @@ void mmc_remove_card(struct mmc_card *card) { struct mmc_host *host =3D card->host; =20 + if (host->undervoltage_notify_registered) { + mmc_regulator_unregister_undervoltage_notifier(host); + host->undervoltage_notify_registered =3D false; + } + mmc_remove_card_debugfs(card); =20 if (mmc_card_present(card)) { diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index a0e2dce70434..0b000246481c 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -1399,6 +1399,39 @@ void mmc_power_cycle(struct mmc_host *host, u32 ocr) mmc_power_up(host, ocr); } =20 +/** + * mmc_handle_undervoltage - Handle an undervoltage event on the MMC bus + * @host: The MMC host that detected the undervoltage condition + * + * This function is called when an undervoltage event is detected on one of + * the MMC regulators. + * + * Returns: 0 on success or a negative error code on failure. + */ +int mmc_handle_undervoltage(struct mmc_host *host) +{ + int ret; + + /* Stop the host to prevent races with card removal */ + __mmc_stop_host(host); + + mmc_claim_host(host); + + if (!host->bus_ops || !host->bus_ops->handle_undervoltage) { + mmc_release_host(host); + return 0; + } + + dev_warn(mmc_dev(host), "%s: Undervoltage detected, initiating emergency = stop\n", + mmc_hostname(host)); + + ret =3D host->bus_ops->handle_undervoltage(host); + + mmc_release_host(host); + + return ret; +} + /* * Assign a mmc bus handler to a host. Only one bus handler may control a * host at any given time. diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h index 622085cd766f..31e5918f6e95 100644 --- a/drivers/mmc/core/core.h +++ b/drivers/mmc/core/core.h @@ -31,6 +31,7 @@ struct mmc_bus_ops { int (*sw_reset)(struct mmc_host *); bool (*cache_enabled)(struct mmc_host *); int (*flush_cache)(struct mmc_host *); + int (*handle_undervoltage)(struct mmc_host *host); }; =20 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops); @@ -59,6 +60,7 @@ void mmc_power_off(struct mmc_host *host); void mmc_power_cycle(struct mmc_host *host, u32 ocr); void mmc_set_initial_state(struct mmc_host *host); u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max); +int mmc_handle_undervoltage(struct mmc_host *host); =20 static inline void mmc_delay(unsigned int ms) { diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c index f14671ea5716..6fd07318c139 100644 --- a/drivers/mmc/core/host.c +++ b/drivers/mmc/core/host.c @@ -564,6 +564,9 @@ struct mmc_host *mmc_alloc_host(int extra, struct devic= e *dev) INIT_WORK(&host->sdio_irq_work, sdio_irq_work); timer_setup(&host->retune_timer, mmc_retune_timer, 0); =20 + INIT_WORK(&host->supply.uv_work, mmc_undervoltage_workfn); + host->undervoltage_notify_registered =3D false; + /* * By default, hosts do not support SGIO or large requests. * They have to set these according to their abilities. diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index 5be9b42d5057..5689ab6eefe1 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -2370,6 +2370,12 @@ int mmc_attach_mmc(struct mmc_host *host) goto remove_card; =20 mmc_claim_host(host); + + if (host->card->ext_csd.power_off_notification =3D=3D EXT_CSD_POWER_ON) { + mmc_regulator_register_undervoltage_notifier(host); + host->undervoltage_notify_registered =3D true; + } + return 0; =20 remove_card: diff --git a/drivers/mmc/core/regulator.c b/drivers/mmc/core/regulator.c index 3dae2e9b7978..85c318fd98fa 100644 --- a/drivers/mmc/core/regulator.c +++ b/drivers/mmc/core/regulator.c @@ -7,6 +7,7 @@ #include #include #include +#include =20 #include =20 @@ -262,6 +263,87 @@ static inline int mmc_regulator_get_ocrmask(struct reg= ulator *supply) =20 #endif /* CONFIG_REGULATOR */ =20 +/* To be called from a high-priority workqueue */ +void mmc_undervoltage_workfn(struct work_struct *work) +{ + struct mmc_supply *supply; + struct mmc_host *host; + + supply =3D container_of(work, struct mmc_supply, uv_work); + host =3D container_of(supply, struct mmc_host, supply); + + mmc_handle_undervoltage(host); +} +EXPORT_SYMBOL_GPL(mmc_undervoltage_workfn); + +static int mmc_handle_regulator_event(struct notifier_block *nb, + unsigned long event, void *data) +{ + struct mmc_supply *supply =3D container_of(nb, struct mmc_supply, + vmmc_nb); + struct mmc_host *host =3D container_of(supply, struct mmc_host, supply); + unsigned long flags; + + switch (event) { + case REGULATOR_EVENT_UNDER_VOLTAGE: + spin_lock_irqsave(&host->lock, flags); + if (host->undervoltage) { + spin_unlock_irqrestore(&host->lock, flags); + return NOTIFY_OK; + } + + host->undervoltage =3D true; + spin_unlock_irqrestore(&host->lock, flags); + + queue_work(system_highpri_wq, &host->supply.uv_work); + break; + default: + return NOTIFY_DONE; + } + + return NOTIFY_OK; +} + +/** + * mmc_regulator_register_undervoltage_notifier - Register for undervoltage + * events + * @host: MMC host + * + * To be called by a bus driver when a card supporting graceful shutdown + * is attached. + */ +void mmc_regulator_register_undervoltage_notifier(struct mmc_host *host) +{ + int ret; + + if (IS_ERR_OR_NULL(host->supply.vmmc)) + return; + + host->supply.vmmc_nb.notifier_call =3D mmc_handle_regulator_event; + ret =3D regulator_register_notifier(host->supply.vmmc, + &host->supply.vmmc_nb); + if (ret) + dev_warn(mmc_dev(host), "Failed to register vmmc notifier: %d\n", ret); +} +EXPORT_SYMBOL_GPL(mmc_regulator_register_undervoltage_notifier); + +/** + * mmc_regulator_unregister_undervoltage_notifier - Unregister undervoltage + * notifier + * @host: MMC host + */ +void mmc_regulator_unregister_undervoltage_notifier(struct mmc_host *host) +{ + if (IS_ERR_OR_NULL(host->supply.vmmc)) + return; + + regulator_unregister_notifier(host->supply.vmmc, &host->supply.vmmc_nb); + + /* Ensure any pending work is flushed before the card is removed */ + cancel_work_sync(&host->supply.uv_work); +} +EXPORT_SYMBOL_GPL(mmc_regulator_unregister_undervoltage_notifier); + /** * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a h= ost * @mmc: the host to regulate diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h index 68f09a955a90..9b7bb4b7150c 100644 --- a/include/linux/mmc/host.h +++ b/include/linux/mmc/host.h @@ -11,6 +11,7 @@ #include #include #include +#include =20 #include #include @@ -342,6 +343,9 @@ struct mmc_supply { struct regulator *vmmc; /* Card power supply */ struct regulator *vqmmc; /* Optional Vccq supply */ struct regulator *vqmmc2; /* Optional supply for phy */ + + struct notifier_block vmmc_nb; /* Notifier for vmmc */ + struct work_struct uv_work; /* Undervoltage work */ }; =20 struct mmc_ctx { @@ -493,6 +497,9 @@ struct mmc_host { unsigned int retune_crc_disable:1; /* don't trigger retune upon crc */ unsigned int can_dma_map_merge:1; /* merging can be used */ unsigned int vqmmc_enabled:1; /* vqmmc regulator is enabled */ + unsigned int undervoltage:1; /* Undervoltage state */ + /* Undervoltage notifier is registered */ + unsigned int undervoltage_notify_registered:1; =20 int rescan_disable; /* disable card detection */ int rescan_entered; /* used with nonremovable devices */ @@ -659,6 +666,9 @@ static inline int mmc_regulator_set_vqmmc2(struct mmc_h= ost *mmc, int mmc_regulator_get_supply(struct mmc_host *mmc); int mmc_regulator_enable_vqmmc(struct mmc_host *mmc); void mmc_regulator_disable_vqmmc(struct mmc_host *mmc); +void mmc_regulator_register_undervoltage_notifier(struct mmc_host *host); +void mmc_regulator_unregister_undervoltage_notifier(struct mmc_host *host); +void mmc_undervoltage_workfn(struct work_struct *work); =20 static inline int mmc_card_is_removable(struct mmc_host *host) { --=20 2.39.5 From nobody Thu Oct 9 20:24:06 2025 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF7C42836B0 for ; Mon, 16 Jun 2025 10:29:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; cv=none; b=c0iDbC+NsyovO+PKMbRQgrgx4KKvXxD+f8cDu0U7A3c3A6HmRIP6edS/zVj7DXi5kacaiKRHDZs7rS90a3SXoT75zNuA//VLpLsvGmdJznbRr6AhEYJqoAp8BJ09Y7cDrXzesr2min6tlfYF4b1rb0rO7z1jRe/CVsJaPAxRJ6s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; c=relaxed/simple; bh=qv0qCLd1/75bFRo3erSxMr7Y4wQHNfzx+FDc5u6nH3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mJRY4/BDm7H8fBzj7Ro8Xps2r+bdnVFZuuKMZwIb/n/QyW+STWzJ8cqeX2nIBY/kFGkKNZHzjRX/x5cDvTXg1FAD2qXGBm6auClWFtLRnibM2/aMt7Frl5xuZD6gaQ3Vj2e8VtdoUdrUEo1lpc9ySAIMyRWw4icaVZZ8pF+0Eic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uR75b-0008T1-0A; Mon, 16 Jun 2025 12:29:19 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uR75a-003n2y-0S; Mon, 16 Jun 2025 12:29:18 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uR75a-0072J5-0F; Mon, 16 Jun 2025 12:29:18 +0200 From: Oleksij Rempel To: Ulf Hansson Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, Greg Kroah-Hartman , Mark Brown , "Rafael J. Wysocki" , =?UTF-8?q?S=C3=B8ren=20Andersen?= , Christian Loehle , Adrian Hunter , Avri Altman Subject: [PATCH v6 2/4] mmc: core: Add MMC_POWEROFF_UNDERVOLTAGE support in _mmc_suspend() Date: Mon, 16 Jun 2025 12:29:15 +0200 Message-Id: <20250616102917.1677116-3-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250616102917.1677116-1-o.rempel@pengutronix.de> References: <20250616102917.1677116-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce MMC_POWEROFF_UNDERVOLTAGE as a new mmc_poweroff_type value and adjust _mmc_suspend() to skip cache flush for this case. This prepares for undervoltage handling in a follow-up patch. Signed-off-by: Oleksij Rempel --- changes v5: - rebase against latest mmc/next - use MMC_POWEROFF_UNDERVOLTAGE enum instead of function parameter. changes v3: - add comments - make sure _mmc_flush_cache is not executed in the undervoltage case --- drivers/mmc/core/mmc.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index 5689ab6eefe1..6812df679ba9 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -36,6 +36,7 @@ enum mmc_poweroff_type { MMC_POWEROFF_SUSPEND, MMC_POWEROFF_SHUTDOWN, + MMC_POWEROFF_UNDERVOLTAGE, MMC_POWEROFF_UNBIND, }; =20 @@ -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; =20 - err =3D _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 !=3D MMC_POWEROFF_UNDERVOLTAGE) { + err =3D _mmc_flush_cache(host); + if (err) + goto out; + } =20 if (mmc_card_can_poweroff_notify(host->card) && mmc_host_can_poweroff_notify(host, pm_type)) --=20 2.39.5 From nobody Thu Oct 9 20:24:06 2025 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF8DA28E594 for ; Mon, 16 Jun 2025 10:29:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; cv=none; b=mRdNFMgRjzwARaw2RYBtDh9UTnoLdgOQ7A5hRgUsOvSEEnJcbPp6JyDJNdp8SWY3PRtYLCYTdiyzDTEmdNrZ8+pUbDmOYGjVZVgkJbqPdMdH0VON51jcZY+ktqIEI3uxE4QZzRohp+J+Fn2KldqD8sDDOnxo3QjdrKD+G3NyJNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; c=relaxed/simple; bh=m5hcZ5y48d+2Z8bAMd+te/qHD+psMsZlibq4f9Qo/s0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZgiRT9njdpOMojPn+ZlU9qRJUePIyPJpMGCQc/AXCRE96G/8AvdLNcc8ImiGq1Mtc1tWhBM/F9BPhW0BRCr1EdudzwBF0kG/IXPRzmZmxA3TXqL2YW40Br4Mxec0U30G2BSBFqveMaGSOvgs8LoSQmu+q9oLSwk8dq/hd0nH6yA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uR75b-0008T2-0A; Mon, 16 Jun 2025 12:29:19 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uR75a-003n2z-0a; Mon, 16 Jun 2025 12:29:18 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uR75a-0072JF-0J; Mon, 16 Jun 2025 12:29:18 +0200 From: Oleksij Rempel To: Ulf Hansson Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, Greg Kroah-Hartman , Mark Brown , "Rafael J. Wysocki" , =?UTF-8?q?S=C3=B8ren=20Andersen?= , Christian Loehle , Adrian Hunter , Avri Altman Subject: [PATCH v6 3/4] mmc: core: add undervoltage handler for MMC/eMMC devices Date: Mon, 16 Jun 2025 12:29:16 +0200 Message-Id: <20250616102917.1677116-4-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250616102917.1677116-1-o.rempel@pengutronix.de> References: <20250616102917.1677116-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce `_mmc_handle_undervoltage()` to handle undervoltage events for MMC/eMMC devices. The handler performs a controlled emergency suspend and then marks the card as removed to prevent further I/O. This is achieved by calling a new internal helper, `__mmc_suspend()`, with `MMC_POWEROFF_UNDERVOLTAGE`. This ensures a fast power-down sequence by using the short power-off notification and skipping the cache flush. If power-off notify is not supported, it falls back to sleep or deselect. Signed-off-by: Oleksij Rempel --- changes v6: - Refactor suspend logic: move cache flush skipping during undervoltage to a separate, preceding commit. - update commit message changes v5: - Rebased on top of patch introducing enum mmc_poweroff_type - Updated call to __mmc_suspend() to use MMC_POWEROFF_UNDERVOLTAGE - Dropped __mmc_resume() helper, as it is no longer needed - Updated commit message to reflect API change and code removal changes v4: - Drop HPI step. changes v3: - reword commit message. - add comments in the code - do not try to resume sleeping device --- drivers/mmc/core/mmc.c | 76 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index 6812df679ba9..fe4fc2ad261e 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -2120,7 +2120,7 @@ static int _mmc_flush_cache(struct mmc_host *host) return err; } =20 -static int _mmc_suspend(struct mmc_host *host, enum mmc_poweroff_type pm_t= ype) +static int __mmc_suspend(struct mmc_host *host, enum mmc_poweroff_type pm_= type) { unsigned int notify_type =3D EXT_CSD_POWER_OFF_SHORT; int err =3D 0; @@ -2128,8 +2128,6 @@ static int _mmc_suspend(struct mmc_host *host, enum m= mc_poweroff_type pm_type) if (pm_type =3D=3D MMC_POWEROFF_SHUTDOWN) notify_type =3D EXT_CSD_POWER_OFF_LONG; =20 - mmc_claim_host(host); - if (mmc_card_suspended(host->card)) goto out; =20 @@ -2156,7 +2154,17 @@ static int _mmc_suspend(struct mmc_host *host, enum = mmc_poweroff_type pm_type) mmc_card_set_suspended(host->card); } out: + return err; +} + +static int _mmc_suspend(struct mmc_host *host, enum mmc_poweroff_type pm_t= ype) +{ + int err; + + mmc_claim_host(host); + err =3D __mmc_suspend(host, pm_type); mmc_release_host(host); + return err; } =20 @@ -2219,6 +2227,13 @@ static int mmc_shutdown(struct mmc_host *host) { int err =3D 0; =20 + /* + * In case of undervoltage, the card will be powered off by + * _mmc_handle_undervoltage() + */ + if (host->undervoltage) + 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 @@ -2309,6 +2324,60 @@ static int _mmc_hw_reset(struct mmc_host *host) return mmc_init_card(host, card->ocr, card); } =20 +/** + * _mmc_handle_undervoltage - Handle an undervoltage event for MMC/eMMC de= vices + * @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: + * 1. If no card is present, return immediately. + * 2. Perform an emergency suspend using EXT_CSD_POWER_OFF_SHORT if possib= le. + * - If power-off notify is not supported, fallback mechanisms like sle= ep or + * deselecting the card are attempted. + * - Cache flushing is skipped to reduce execution time. + * 3. Mark the card as removed to prevent further interactions after + * undervoltage. + * + * Note: This function does not handle host claiming or releasing. The cal= ler + * 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 =3D host->card; + int err; + + /* If there is no card attached, nothing to do */ + if (!card) + return 0; + + /* + * 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 =3D __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 =3D { .remove =3D mmc_remove, .detect =3D mmc_detect, @@ -2321,6 +2390,7 @@ static const struct mmc_bus_ops mmc_ops =3D { .hw_reset =3D _mmc_hw_reset, .cache_enabled =3D _mmc_cache_enabled, .flush_cache =3D _mmc_flush_cache, + .handle_undervoltage =3D _mmc_handle_undervoltage, }; =20 /* --=20 2.39.5 From nobody Thu Oct 9 20:24:06 2025 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF94D294A11 for ; Mon, 16 Jun 2025 10:29:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; cv=none; b=bPHyI5buyCtcPv/WGkoeFJXOht3RUOud9fFjgVU5llqSHIjVJSpUAsBmNgPe6PD3+wh5L/KQBvboBhOIcX70bn5G1/x19cyIaypTJFn3/jOqahjSOtiexJhN0hfURfbMo7M2ItgRXYNqFWknZjd20Z6fpeZgAh5HVFZdhQgBeuA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750069763; c=relaxed/simple; bh=BqfmnFlGx47i/Fb4Ot7l93J2aT1bI8O8B8tWiT7Gav0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pO0wIggdvQoUD3BqYqWHvx7uHax9KV4P6lfUzp67xloZxEl9Iuh+HhiuUuzWTcOaDKev3yt+UfGq8x77mRt6etR5WY8v7tDwhVoHQRRr8f+T3Z5i3o96L5ac7ZIds07bV+WZrXdyU+G5QU083v5/rvO5+qYBkQ+Oi5SB4y/1ZuQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uR75b-0008T3-0A; Mon, 16 Jun 2025 12:29:19 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uR75a-003n31-0c; Mon, 16 Jun 2025 12:29:18 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1uR75a-0072JP-0N; Mon, 16 Jun 2025 12:29:18 +0200 From: Oleksij Rempel To: Ulf Hansson Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, Greg Kroah-Hartman , Mark Brown , "Rafael J. Wysocki" , =?UTF-8?q?S=C3=B8ren=20Andersen?= , Christian Loehle , Adrian Hunter , Avri Altman Subject: [PATCH v6 4/4] mmc: block: abort requests and suppress errors after undervoltage shutdown Date: Mon, 16 Jun 2025 12:29:17 +0200 Message-Id: <20250616102917.1677116-5-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250616102917.1677116-1-o.rempel@pengutronix.de> References: <20250616102917.1677116-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extend the existing card removal checks in mmc_blk_mq_complete_rq() and mmc_mq_queue_rq() to also account for the undervoltage state. If the host has entered undervoltage shutdown, mark requests as quiet and abort them early to prevent unnecessary retries and error logging. This ensures no further operations are attempted on the card after an emergency stop. Signed-off-by: Oleksij Rempel --- drivers/mmc/core/block.c | 2 +- drivers/mmc/core/queue.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/mmc/core/block.c b/drivers/mmc/core/block.c index 9cc47bf94804..bc57bb8514f6 100644 --- a/drivers/mmc/core/block.c +++ b/drivers/mmc/core/block.c @@ -2129,7 +2129,7 @@ static void mmc_blk_mq_complete_rq(struct mmc_queue *= mq, struct request *req) } else if (mqrq->retries++ < MMC_MAX_RETRIES) { blk_mq_requeue_request(req, true); } else { - if (mmc_card_removed(mq->card)) + if (mmc_card_removed(mq->card) || mq->card->host->undervoltage) req->rq_flags |=3D RQF_QUIET; blk_mq_end_request(req, BLK_STS_IOERR); } diff --git a/drivers/mmc/core/queue.c b/drivers/mmc/core/queue.c index 284856c8f655..1dd71ea6927d 100644 --- a/drivers/mmc/core/queue.c +++ b/drivers/mmc/core/queue.c @@ -239,7 +239,7 @@ static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ct= x *hctx, bool get_card, cqe_retune_ok; blk_status_t ret; =20 - if (mmc_card_removed(mq->card)) { + if (mmc_card_removed(mq->card) || mq->card->host->undervoltage) { req->rq_flags |=3D RQF_QUIET; return BLK_STS_IOERR; } --=20 2.39.5