From nobody Fri Dec 19 22:03:28 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 916231E9B06 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=KZ5y9Xc7747d15S1eHd0fNtxXUbmpewFF2vywJ0gQuanq7L/4iDAN9eft09aWeha48Dz1AiasPirGbijrhSOw8sxJSMyS2wmFQZzNpHkwkx60Kr/EVmS9NWuy8PiyyLRq8CcoLUOj+tNDpbYci6r1E5YNgG+IEwaNpNFxMTIkOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740037477; c=relaxed/simple; bh=oSZ1L+BeRLKuAq0nI7S86bo/LhgGGt+J5L31FlMjqeM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=odPijVFh7dpO2GTXhyTIj3jWhGW4+eW/kUol3JzFf0mvGOeYt0pHgOhSTF/ylXqg5F99QC+fKDv3zil89GSjgWbwFBsDq9E1BKsDBujFrpU8WjSWjc29B8vbYt30GAMoEDkku+1egiCI7dwSWrz61zcPHlvQaYlNTLjIXzkYmVI= 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=20Andersen?= , 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-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. 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; } =20 -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 =3D 0; @@ -2115,8 +2115,6 @@ static int _mmc_suspend(struct mmc_host *host, bool i= s_suspend, else notify_type =3D EXT_CSD_POWER_OFF_LONG; =20 - mmc_claim_host(host); - if (mmc_card_suspended(host->card)) goto out; =20 @@ -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 =3D __mmc_suspend(host, is_suspend, is_undervoltage); mmc_release_host(host); + return err; } =20 @@ -2160,6 +2169,20 @@ static int mmc_suspend(struct mmc_host *host) return err; } =20 +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 =3D 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 =3D 0; =20 mmc_claim_host(host); - - if (!mmc_card_suspended(host->card)) - goto out; - - mmc_power_up(host, host->card->ocr); - err =3D mmc_init_card(host, host->card->ocr, host->card); - mmc_card_clr_suspended(host->card); - -out: + err =3D __mmc_resume(host); mmc_release_host(host); + return err; } =20 @@ -2189,6 +2205,9 @@ static int mmc_shutdown(struct mmc_host *host) { int err =3D 0; =20 + 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); } =20 +static int _mmc_handle_undervoltage(struct mmc_host *host) +{ + struct mmc_card *card =3D host->card; + int err =3D 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 =3D __mmc_resume(host); + + if (!err) { + err =3D mmc_interrupt_hpi(card); + if (err) + pr_err("%s: Interrupt HPI failed, error %d\n", + mmc_hostname(host), err); + + err =3D __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 =3D { .remove =3D mmc_remove, .detect =3D mmc_detect, @@ -2292,6 +2346,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