From patchwork Thu Feb 20 07:44:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13983437 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 8D88E1E9907 for ; Thu, 20 Feb 2025 07:44:33 +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=1740037476; cv=none; b=ioVNsyRmFx6eYALlH0xbfIpEwe+zInIe03KZLV/G6JXXqdXSK/E2apYgu27zFTKZ7U1TBXLA8k5jIqBlzrWxSxqygXoErZZWpaBcpgl1DGlQNhKP665wrFlH1DoRAh/vHU5Xe9k9HTjl1++u126dS6QYROAaxr5XzWVDwZFojVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740037476; c=relaxed/simple; bh=Pajo6n58YQjMYvbXjpmcT2nx5LNJ3QUBwJ7/2kJOKqM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cHSYa98Qu7LroRtTbjicicZqi3lpPy7lH/+ztQTBSYhRxc627Gph5vxOPAaNvL80v6GuD8ZqJurz8ZJS5XpFYaXRtWIY23wR1Bit3KeDLiImV7DfYJ2khWSZk+OjyvhddRUGNjq87TcIMidYbnlT9JpS2d2XVScwdoAKsucoYA8= 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 1tl1EV-0004TP-1C; Thu, 20 Feb 2025 08:44:31 +0100 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 1tl1EU-001tqO-1t; Thu, 20 Feb 2025 08:44:30 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tl1EU-00CC1u-1g; Thu, 20 Feb 2025 08:44:30 +0100 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_Andersen?= , Christian Loehle Subject: [PATCH v2 1/6] mmc: core: Handle undervoltage events and register regulator notifiers Date: Thu, 20 Feb 2025 08:44:24 +0100 Message-Id: <20250220074429.2906141-2-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250220074429.2906141-1-o.rempel@pengutronix.de> References: <20250220074429.2906141-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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-mmc@vger.kernel.org Extend the MMC core to handle undervoltage events by implementing infrastructure to notify the MMC bus about voltage drops. 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. Signed-off-by: Oleksij Rempel --- drivers/mmc/core/core.c | 20 ++++++ drivers/mmc/core/core.h | 2 + drivers/mmc/core/regulator.c | 115 +++++++++++++++++++++++++++++++++++ include/linux/mmc/host.h | 8 +++ 4 files changed, 145 insertions(+) diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index 5241528f8b90..3ee6d83eb354 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -1399,6 +1399,26 @@ void mmc_power_cycle(struct mmc_host *host, u32 ocr) mmc_power_up(host, ocr); } +/** + * 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) +{ + if (!host->bus_ops->handle_undervoltage) + return 0; + + dev_warn(mmc_dev(host), "%s: Undervoltage detected, initiating emergency stop\n", + mmc_hostname(host)); + + return host->bus_ops->handle_undervoltage(host); +} + /* * 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 fc9c066e6468..b77f053039ab 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); }; 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); static inline void mmc_delay(unsigned int ms) { diff --git a/drivers/mmc/core/regulator.c b/drivers/mmc/core/regulator.c index 3dae2e9b7978..d3f3eb8ea3d7 100644 --- a/drivers/mmc/core/regulator.c +++ b/drivers/mmc/core/regulator.c @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -262,6 +263,98 @@ static inline int mmc_regulator_get_ocrmask(struct regulator *supply) #endif /* CONFIG_REGULATOR */ +static void mmc_undervoltage_workfn(struct work_struct *work) +{ + struct mmc_supply *supply; + struct mmc_host *mmc; + + supply = container_of(work, struct mmc_supply, uv_work); + mmc = container_of(supply, struct mmc_host, supply); + + mmc_handle_undervoltage(mmc); +} + +static int mmc_handle_regulator_event(struct mmc_host *mmc, + const char *regulator_name, + unsigned long event) +{ + switch (event) { + case REGULATOR_EVENT_UNDER_VOLTAGE: + if (mmc->undervoltage) + return NOTIFY_OK; + + mmc->undervoltage = true; + queue_work(system_highpri_wq, &mmc->supply.uv_work); + break; + default: + return NOTIFY_DONE; + } + + return NOTIFY_OK; +} + +static int mmc_vmmc_notifier_callback(struct notifier_block *nb, + unsigned long event, void *data) +{ + struct mmc_supply *supply; + struct mmc_host *mmc; + + supply = container_of(nb, struct mmc_supply, vmmc_nb); + mmc = container_of(supply, struct mmc_host, supply); + + return mmc_handle_regulator_event(mmc, "vmmc", event); +} + +static int mmc_vqmmc_notifier_callback(struct notifier_block *nb, + unsigned long event, void *data) +{ + struct mmc_supply *supply; + struct mmc_host *mmc; + + supply = container_of(nb, struct mmc_supply, vqmmc_nb); + mmc = container_of(supply, struct mmc_host, supply); + + return mmc_handle_regulator_event(mmc, "vqmmc", event); +} + +static int mmc_vqmmc2_notifier_callback(struct notifier_block *nb, + unsigned long event, void *data) +{ + struct mmc_supply *supply; + struct mmc_host *mmc; + + supply = container_of(nb, struct mmc_supply, vqmmc2_nb); + mmc = container_of(supply, struct mmc_host, supply); + + return mmc_handle_regulator_event(mmc, "vqmmc2", event); +} + +static void +mmc_register_regulator_notifier(struct mmc_host *mmc, + struct regulator *regulator, + struct notifier_block *nb, + int (*callback)(struct notifier_block *, + unsigned long, void *), + const char *name) +{ + struct device *dev = mmc_dev(mmc); + int ret; + + nb->notifier_call = callback; + ret = devm_regulator_register_notifier(regulator, nb); + if (ret) + dev_warn(dev, "Failed to register %s notifier: %pe\n", name, + ERR_PTR(ret)); +} + +static void mmc_undervoltage_work_cleanup(void *data) +{ + struct mmc_supply *supply = data; + + /* Ensure the work is canceled or flushed here */ + cancel_work_sync(&supply->uv_work); +} + /** * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host * @mmc: the host to regulate @@ -281,6 +374,13 @@ int mmc_regulator_get_supply(struct mmc_host *mmc) mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc"); mmc->supply.vqmmc2 = devm_regulator_get_optional(dev, "vqmmc2"); + INIT_WORK(&mmc->supply.uv_work, mmc_undervoltage_workfn); + + ret = devm_add_action_or_reset(dev, mmc_undervoltage_work_cleanup, + &mmc->supply); + if (ret) + return dev_err_probe(dev, ret, "Failed to add cleanup action\n"); + if (IS_ERR(mmc->supply.vmmc)) { if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER) return dev_err_probe(dev, -EPROBE_DEFER, @@ -293,6 +393,11 @@ int mmc_regulator_get_supply(struct mmc_host *mmc) mmc->ocr_avail = ret; else dev_warn(dev, "Failed getting OCR mask: %d\n", ret); + + mmc_register_regulator_notifier(mmc, mmc->supply.vmmc, + &mmc->supply.vmmc_nb, + mmc_vmmc_notifier_callback, + "vmmc"); } if (IS_ERR(mmc->supply.vqmmc)) { @@ -301,12 +406,22 @@ int mmc_regulator_get_supply(struct mmc_host *mmc) "vqmmc regulator not available\n"); dev_dbg(dev, "No vqmmc regulator found\n"); + } else { + mmc_register_regulator_notifier(mmc, mmc->supply.vqmmc, + &mmc->supply.vqmmc_nb, + mmc_vqmmc_notifier_callback, + "vqmmc"); } if (IS_ERR(mmc->supply.vqmmc2)) { if (PTR_ERR(mmc->supply.vqmmc2) == -EPROBE_DEFER) return -EPROBE_DEFER; dev_dbg(dev, "No vqmmc2 regulator found\n"); + } else { + mmc_register_regulator_notifier(mmc, mmc->supply.vqmmc2, + &mmc->supply.vqmmc2_nb, + mmc_vqmmc2_notifier_callback, + "vqmmc2"); } return 0; diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h index 68f09a955a90..4e147ad82804 100644 --- a/include/linux/mmc/host.h +++ b/include/linux/mmc/host.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -342,6 +343,12 @@ 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 notifier_block vqmmc_nb; /* Notifier for vqmmc */ + struct notifier_block vqmmc2_nb; /* Notifier for vqmmc2 */ + + struct work_struct uv_work; /* Undervoltage work */ }; struct mmc_ctx { @@ -493,6 +500,7 @@ 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 */ int rescan_disable; /* disable card detection */ int rescan_entered; /* used with nonremovable devices */ From patchwork Thu Feb 20 07:44:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13983441 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 955FA1E9B09 for ; Thu, 20 Feb 2025 07:44:33 +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=1740037477; cv=none; b=twiB1GvZZrc80jHL7IBSPw2bOZXYtVOIjvNhnaA6I0E68zJtkWTvO+XFtWZ97y+oegise2U43wShu137r+JHLbMYrq/vvdfFDzJxhfq+48AV3lZJIaRH5c+hTVxHSITTXOURx505Tll20tITxuBOxaGENFBI1GdNCwyLLAeZcxQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740037477; c=relaxed/simple; bh=Adh/Q3rnUh98xXUXofXQcb8ZYwFqHSH9eh8GRfjl3Jg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Uv3BRLjE4LUAgbYvoAfjjQhl6vsRU2OLvBOyMNcaEGB1xFEi1HkhQHlO+gPWxo4FwosT6sibpkJVmTGdqTFxAt5DoY71Jli13nrPkeqg3em5Hn5Sa5+IBtUxW6VdTEP8lBqmYhnFbJsR0DQd4pAGojV9yTmBxcH0SWp2Rdjbf2E= 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 1tl1EV-0004TQ-1C; Thu, 20 Feb 2025 08:44:31 +0100 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 1tl1EU-001tqP-1y; Thu, 20 Feb 2025 08:44:30 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tl1EU-00CC24-1l; Thu, 20 Feb 2025 08:44:30 +0100 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_Andersen?= , Christian Loehle Subject: [PATCH v2 2/6] mmc: core: make mmc_interrupt_hpi() global Date: Thu, 20 Feb 2025 08:44:25 +0100 Message-Id: <20250220074429.2906141-3-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250220074429.2906141-1-o.rempel@pengutronix.de> References: <20250220074429.2906141-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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-mmc@vger.kernel.org Make mmc_interrupt_hpi() non-static. This enables usage of HPI outside mmc_ops.c and will be used in a follow-up patch. Signed-off-by: Oleksij Rempel --- drivers/mmc/core/mmc_ops.c | 2 +- drivers/mmc/core/mmc_ops.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c index 5c8e62e8f331..dbb1b5ec4132 100644 --- a/drivers/mmc/core/mmc_ops.c +++ b/drivers/mmc/core/mmc_ops.c @@ -903,7 +903,7 @@ static int mmc_send_hpi_cmd(struct mmc_card *card) * Issued High Priority Interrupt, and check for card status * until out-of prg-state. */ -static int mmc_interrupt_hpi(struct mmc_card *card) +int mmc_interrupt_hpi(struct mmc_card *card) { int err; u32 status; diff --git a/drivers/mmc/core/mmc_ops.h b/drivers/mmc/core/mmc_ops.h index 0df3ebd900d1..a16361dc5909 100644 --- a/drivers/mmc/core/mmc_ops.h +++ b/drivers/mmc/core/mmc_ops.h @@ -37,6 +37,7 @@ int mmc_send_cid(struct mmc_host *host, u32 *cid); int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp); int mmc_spi_set_crc(struct mmc_host *host, int use_crc); int mmc_bus_test(struct mmc_card *card, u8 bus_width); +int mmc_interrupt_hpi(struct mmc_card *card); int mmc_can_ext_csd(struct mmc_card *card); int mmc_switch_status(struct mmc_card *card, bool crc_err_fatal); bool mmc_prepare_busy_cmd(struct mmc_host *host, struct mmc_command *cmd, From patchwork Thu Feb 20 07:44:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13983438 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 941501E9B08 for ; Thu, 20 Feb 2025 07:44:33 +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=1740037477; cv=none; b=YrJTjl2+MZtAU5MfL2vBmoS4KZRFfbin8lKdJWmk72wserHtzH02qBka4VAyVJgRS0h/l50M0gcuaNbdqho2eCAdxANRi7qUUGzXHrdyAewIgEUI7uZ+rMWotc7HOiP4RlK4qMO/ob8dbEitJ1udMCDdroORCemXVeot2G72ryY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740037477; c=relaxed/simple; bh=e6uUNN/5YcOI8FAcrOwzJaNiC1Ml4nY7w/gShuB8HF8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RFV2gnJJ4dJ5Yk5mK+p/1rRYc6/wx6UR9NuMhKPu0jZRI0XXKlKv4CtbJcgBH1B6rn8DZYrgmSUNOG9FzaunF/doK4CIT5NbyTJYC4po7Jp2YE1FTgIKIv6x8asC5MJ2t0h3Tbedv+rEeg061Hc04SwYs+MC9Zzw0xuAYMDFlSI= 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 1tl1EV-0004TR-1C; Thu, 20 Feb 2025 08:44:31 +0100 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 1tl1EU-001tqQ-21; Thu, 20 Feb 2025 08:44:30 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tl1EU-00CC2E-1q; Thu, 20 Feb 2025 08:44:30 +0100 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_Andersen?= , Christian Loehle Subject: [PATCH v2 3/6] mmc: core: refactor _mmc_suspend() for undervoltage handling Date: Thu, 20 Feb 2025 08:44:26 +0100 Message-Id: <20250220074429.2906141-4-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250220074429.2906141-1-o.rempel@pengutronix.de> References: <20250220074429.2906141-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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-mmc@vger.kernel.org Introduce an is_undervoltage parameter to _mmc_suspend() to apply a short power-off sequence and optionally flush the cache. This refactoring prepares for undervoltage support in a follow-up patch. Signed-off-by: Oleksij Rempel --- drivers/mmc/core/mmc.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index 6a23be214543..86b608843232 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -2104,20 +2104,27 @@ static int _mmc_flush_cache(struct mmc_host *host) return err; } -static int _mmc_suspend(struct mmc_host *host, bool is_suspend) +static int _mmc_suspend(struct mmc_host *host, bool is_suspend, + bool is_undervoltage) { + unsigned int notify_type; int err = 0; - unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT : - EXT_CSD_POWER_OFF_LONG; + + if (is_undervoltage || is_suspend) + notify_type = EXT_CSD_POWER_OFF_SHORT; + else + notify_type = EXT_CSD_POWER_OFF_LONG; mmc_claim_host(host); if (mmc_card_suspended(host->card)) goto out; - err = _mmc_flush_cache(host); - if (err) - goto out; + if (is_undervoltage) { + err = _mmc_flush_cache(host); + if (err) + goto out; + } if (mmc_can_poweroff_notify(host->card) && ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend || @@ -2144,7 +2151,7 @@ static int mmc_suspend(struct mmc_host *host) { int err; - err = _mmc_suspend(host, true); + err = _mmc_suspend(host, true, false); if (!err) { pm_runtime_disable(&host->card->dev); pm_runtime_set_suspended(&host->card->dev); @@ -2191,7 +2198,7 @@ static int mmc_shutdown(struct mmc_host *host) err = _mmc_resume(host); if (!err) - err = _mmc_suspend(host, false); + err = _mmc_suspend(host, false, false); return err; } @@ -2215,7 +2222,7 @@ static int mmc_runtime_suspend(struct mmc_host *host) if (!(host->caps & MMC_CAP_AGGRESSIVE_PM)) return 0; - err = _mmc_suspend(host, true); + err = _mmc_suspend(host, true, false); if (err) pr_err("%s: error %d doing aggressive suspend\n", mmc_hostname(host), err); From patchwork Thu Feb 20 07:44:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13983442 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 8FE0A1E9B05 for ; Thu, 20 Feb 2025 07:44:33 +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=1740037478; cv=none; b=LMoKzZFL/U1qRGAqOwNfABaQbjViWr/24Hk0vAUHSwJ/CqhQSHns2sVlCib23B6DYWHsiHeuOnWMRWnMpjUHsoDjgt+XlugJyqDhO53PI/Kka+0AoQNj66gSoJpaXO1LvnvKUXp/Do0HqwXkqATyOhiaa2eYZlAzpImXj8x9iJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740037478; c=relaxed/simple; bh=oSZ1L+BeRLKuAq0nI7S86bo/LhgGGt+J5L31FlMjqeM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ihvckYeeJD2uQr7qOAdj6haNLluwh9UZm1kWiQnLmG5Y5YYvgE/cAovYb3XKqy3Qabu7Oxx/hNn7d4xHlbjXJNHw7uKCtE3Fel1CLSMQe7ocdyjtUaO74vaIqX9OiJW1vNJhL8Sf3hV0n/908VK3wveWm1U0Zlu0cliE/19FYLs= 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 1tl1EV-0004TS-1C; Thu, 20 Feb 2025 08:44:31 +0100 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 1tl1EU-001tqS-27; Thu, 20 Feb 2025 08:44:30 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tl1EU-00CC2O-1t; Thu, 20 Feb 2025 08:44:30 +0100 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_Andersen?= , Christian Loehle Subject: [PATCH v2 4/6] mmc: core: add undervoltage handler for MMC/eMMC devices Date: Thu, 20 Feb 2025 08:44:27 +0100 Message-Id: <20250220074429.2906141-5-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250220074429.2906141-1-o.rempel@pengutronix.de> References: <20250220074429.2906141-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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-mmc@vger.kernel.org Introduce _mmc_handle_undervoltage() to handle undervoltage events for MMC/eMMC devices. This function interrupts ongoing operations using HPI and performs a controlled suspend. After completing the sequence, the card is marked as removed to prevent further interactions. To support this, introduce __mmc_suspend() and __mmc_resume() as internal helpers that omit mmc_claim_host()/mmc_release_host(), allowing them to be called when the host is already claimed. Update mmc_shutdown() to skip the normal shutdown sequence if the host is flagged as undervoltage to avoid repeating of the shutdown procedure. Signed-off-by: Oleksij Rempel --- drivers/mmc/core/mmc.c | 81 +++++++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index 86b608843232..e626213e7a52 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -2104,8 +2104,8 @@ static int _mmc_flush_cache(struct mmc_host *host) return err; } -static int _mmc_suspend(struct mmc_host *host, bool is_suspend, - bool is_undervoltage) +static int __mmc_suspend(struct mmc_host *host, bool is_suspend, + bool is_undervoltage) { unsigned int notify_type; int err = 0; @@ -2115,8 +2115,6 @@ static int _mmc_suspend(struct mmc_host *host, bool is_suspend, else notify_type = EXT_CSD_POWER_OFF_LONG; - mmc_claim_host(host); - if (mmc_card_suspended(host->card)) goto out; @@ -2140,7 +2138,18 @@ static int _mmc_suspend(struct mmc_host *host, bool is_suspend, mmc_card_set_suspended(host->card); } out: + return err; +} + +static int _mmc_suspend(struct mmc_host *host, bool is_suspend, + bool is_undervoltage) +{ + int err; + + mmc_claim_host(host); + err = __mmc_suspend(host, is_suspend, is_undervoltage); mmc_release_host(host); + return err; } @@ -2160,6 +2169,20 @@ static int mmc_suspend(struct mmc_host *host) return err; } +static int __mmc_resume(struct mmc_host *host) +{ + int err; + + if (!mmc_card_suspended(host->card)) + return 0; + + mmc_power_up(host, host->card->ocr); + err = mmc_init_card(host, host->card->ocr, host->card); + mmc_card_clr_suspended(host->card); + + return err; +} + /* * This function tries to determine if the same card is still present * and, if so, restore all state to it. @@ -2169,16 +2192,9 @@ static int _mmc_resume(struct mmc_host *host) int err = 0; mmc_claim_host(host); - - if (!mmc_card_suspended(host->card)) - goto out; - - mmc_power_up(host, host->card->ocr); - err = mmc_init_card(host, host->card->ocr, host->card); - mmc_card_clr_suspended(host->card); - -out: + err = __mmc_resume(host); mmc_release_host(host); + return err; } @@ -2189,6 +2205,9 @@ static int mmc_shutdown(struct mmc_host *host) { int err = 0; + if (host->undervoltage) + return 0; + /* * In a specific case for poweroff notify, we need to resume the card * before we can shutdown it properly. @@ -2280,6 +2299,41 @@ static int _mmc_hw_reset(struct mmc_host *host) return mmc_init_card(host, card->ocr, card); } +static int _mmc_handle_undervoltage(struct mmc_host *host) +{ + struct mmc_card *card = host->card; + int err = 0; + + mmc_claim_host(host); + + if (!host->card) + goto out; + + if (mmc_can_poweroff_notify(host->card) && + !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE)) + err = __mmc_resume(host); + + if (!err) { + err = mmc_interrupt_hpi(card); + if (err) + pr_err("%s: Interrupt HPI failed, error %d\n", + mmc_hostname(host), err); + + err = __mmc_suspend(host, false, true); + } + + if (err) + pr_err("%s: Undervoltage emergency stop failed\n", + mmc_hostname(host)); + + mmc_card_set_removed(host->card); + +out: + mmc_release_host(host); + + return err; +} + static const struct mmc_bus_ops mmc_ops = { .remove = mmc_remove, .detect = mmc_detect, @@ -2292,6 +2346,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, }; /* From patchwork Thu Feb 20 07:44:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13983440 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 8D9B51E9916 for ; Thu, 20 Feb 2025 07:44:33 +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=1740037477; cv=none; b=J/wGHw9IDFREKhdsgyd3TFo6U07lntJ9JJhKYIg7z5w2QQDCgjX9iKMMtmBaRNeIOTu4ICD9kjE9kjUVcKCSQhz2PJyMyPVNqRRtEjw1B9nDFxwvYdYkM6rKDmFyUMis6S+xKqGC0JlP33Hd55XqiiOSig2DFV08xjVdKy7eb1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740037477; c=relaxed/simple; bh=jBP3yn8EoczVygvEooEdxOeCaOGnWGEZlmynb+kkie8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ND/w++uq9+JnJIqEoT0bNGSTTH2AD0i+rdWBd+RRjcNt2UEd1QE7nBlywa01VpkewNej4UqzvuvMkAI7zI7e24hBtP3b2BV913Qo2+9wNoeFV4PhTjyJr75Y8ALXnmVfoeDMV64YcnEzyWZ2hhRC2umryVLdpGi/HTyivDIg83M= 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 1tl1EV-0004TT-1C; Thu, 20 Feb 2025 08:44:31 +0100 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 1tl1EU-001tqV-2B; Thu, 20 Feb 2025 08:44:30 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tl1EU-00CC2Y-1x; Thu, 20 Feb 2025 08:44:30 +0100 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_Andersen?= , Christian Loehle Subject: [PATCH v2 5/6] mmc: block: abort requests and suppress errors after undervoltage shutdown Date: Thu, 20 Feb 2025 08:44:28 +0100 Message-Id: <20250220074429.2906141-6-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250220074429.2906141-1-o.rempel@pengutronix.de> References: <20250220074429.2906141-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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-mmc@vger.kernel.org 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 4830628510e6..ecb87da0e257 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 |= 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 ab662f502fe7..f46e01988fe8 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_ctx *hctx, bool get_card, cqe_retune_ok; blk_status_t ret; - if (mmc_card_removed(mq->card)) { + if (mmc_card_removed(mq->card) || mq->card->host->undervoltage) { req->rq_flags |= RQF_QUIET; return BLK_STS_IOERR; } From patchwork Thu Feb 20 07:44:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13983443 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 92B821E9B07 for ; Thu, 20 Feb 2025 07:44:33 +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=1740037478; cv=none; b=fJYk0zE0LdG8ozEykz2PwNTr+Y8wgs27qDcGtfp5FsU9LXI/AI89M/peDw+mCoaBAeX3N/g4jSCyhGO/gpGRON1CDzcwIk5FZKGZBl294BU4do2axGytCblL74EgAmAzbtOS4+WUsYka74gVo2/9jeh8NrJdvWMLNuhlHeYO1x0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740037478; c=relaxed/simple; bh=G5incWbHtDEe7N+cSOTb3Owqxt38o2con0kl3Rz0mSA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GhqzeO43viv//OsKc/C0kuw4VADpLuanj5f/WpRdxUY07eWsobWWbQSZgXZyAzdqLLF+elhtMyuuhLG033YmD5nWimGE7N2upAG5vnfgPGr3euCRQZi01CjjiyQ3i4xxScx8C27W8hh8QF8iKIJzuHjGZUtS/G2gsYsM7Lp317o= 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 1tl1EV-0004TU-1C; Thu, 20 Feb 2025 08:44:31 +0100 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 1tl1EU-001tqX-2F; Thu, 20 Feb 2025 08:44:30 +0100 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1tl1EU-00CC2j-21; Thu, 20 Feb 2025 08:44:30 +0100 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_Andersen?= , Christian Loehle Subject: [PATCH v2 6/6] mmc: sdhci: prevent command execution after undervoltage shutdown Date: Thu, 20 Feb 2025 08:44:29 +0100 Message-Id: <20250220074429.2906141-7-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250220074429.2906141-1-o.rempel@pengutronix.de> References: <20250220074429.2906141-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: linux-mmc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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-mmc@vger.kernel.org Introduce an emergency_stop flag in struct mmc_host to block further MMC/SD commands after an undervoltage shutdown. If emergency_stop is set, sdhci_send_command() will reject new requests with -EBUSY and log a warning. This helps diagnose and identify code paths that may still attempt writes after the undervoltage shutdown sequence has completed. Signed-off-by: Oleksij Rempel --- drivers/mmc/core/mmc.c | 1 + drivers/mmc/host/sdhci.c | 9 +++++++++ include/linux/mmc/host.h | 1 + 3 files changed, 11 insertions(+) diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index e626213e7a52..8aa5881293d8 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -2326,6 +2326,7 @@ static int _mmc_handle_undervoltage(struct mmc_host *host) pr_err("%s: Undervoltage emergency stop failed\n", mmc_hostname(host)); + host->emergency_stop = 1; mmc_card_set_removed(host->card); out: diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c index f4a7733a8ad2..8d67f27e7d9e 100644 --- a/drivers/mmc/host/sdhci.c +++ b/drivers/mmc/host/sdhci.c @@ -1658,6 +1658,15 @@ static bool sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd) WARN_ON(host->cmd); + if (host->mmc->emergency_stop) { + pr_warn("%s: Ignoring normal request, emergency stop is active\n", + mmc_hostname(host->mmc)); + WARN_ON_ONCE(1); + + cmd->error = -EBUSY; + return true; + } + /* Initially, a command has no error */ cmd->error = 0; diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h index 4e147ad82804..5dfe2cdde59f 100644 --- a/include/linux/mmc/host.h +++ b/include/linux/mmc/host.h @@ -501,6 +501,7 @@ struct mmc_host { 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 */ + unsigned int emergency_stop:1; /* Emergency stop. No transfers are allowed. */ int rescan_disable; /* disable card detection */ int rescan_entered; /* used with nonremovable devices */