Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH v2 0/2] scsi: ufs-mediatek: fix va09 regulator lifecycle
@ 2026-11-18  3:20 Xiangwu Lai
  2026-11-18  3:20 ` [PATCH v2 1/2] scsi: ufs-mediatek: use devm_regulator_get() for the va09 regulator Xiangwu Lai
  2026-11-18  3:20 ` [PATCH v2 2/2] scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails Xiangwu Lai
  0 siblings, 2 replies; 4+ messages in thread
From: Xiangwu Lai @ 2026-11-18  3:20 UTC (permalink / raw)
  To: peter.wang, chaotian.jing
  Cc: chu.stanley, alim.akhtar, bvanassche, stanley.chu, sashiko-bot,
	linux-scsi, linux-mediatek, linux-kernel, Xiangwu Lai

This is v2 of the va09 regulator reference fix.  Two issues surfaced
while reviewing the devm conversion (many thanks to the Sashiko AI
review of v1):

- the devm release alone would trip
  WARN_ON(regulator->enable_count) in _regulator_put() on unbind,
  because ufs_mtk_init() powers the M-PHY (and thus va09) on and
  nothing powers it off again on the way out;

- the M-PHY power-off path can leave host->mphy_powered_on set when
  regulator_disable() fails, so the next power-on bails out early
  and never restores the PHY (pre-existing).

Patch 1 keeps the devm conversion and adds an .exit callback which
balances the enable count before the devm release, in the same spirit
as the .exit callbacks of ufs-exynos and ufs-qcom.  Patch 2 fixes
the pre-existing state desync.

Changes in v2:
- 1/2: additionally add an .exit callback powering the M-PHY off so
  the va09 enable count is balanced before devm releases the
  regulator.  Without it, _regulator_put() would warn on every
  driver unbind (Sashiko AI review, [Medium]).
- 2/2: new patch fixing the mphy state desync when va09 disable
  fails (Sashiko AI review, [High], pre-existing).

Link: https://lore.kernel.org/r/20261118014234.424-1-laixiangwu@cmss.chinamobile.com

Xiangwu Lai (2):
  scsi: ufs-mediatek: use devm_regulator_get() for the va09 regulator
  scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails

 drivers/ufs/host/ufs-mediatek.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

-- 
2.50.1.windows.1




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] scsi: ufs-mediatek: use devm_regulator_get() for the va09 regulator
  2026-11-18  3:20 [PATCH v2 0/2] scsi: ufs-mediatek: fix va09 regulator lifecycle Xiangwu Lai
@ 2026-11-18  3:20 ` Xiangwu Lai
  2026-11-18  3:20 ` [PATCH v2 2/2] scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails Xiangwu Lai
  1 sibling, 0 replies; 4+ messages in thread
From: Xiangwu Lai @ 2026-11-18  3:20 UTC (permalink / raw)
  To: peter.wang, chaotian.jing
  Cc: chu.stanley, alim.akhtar, bvanassche, stanley.chu, sashiko-bot,
	linux-scsi, linux-mediatek, linux-kernel, Xiangwu Lai

ufs_mtk_init_va09_pwr_ctrl() obtains the va09 regulator with
regulator_get() but the reference is never released with
regulator_put(), neither on driver unbind nor on probe failure. The
other regulators handled by this driver (dvfsrc-vcore) are already
obtained with devm_regulator_get_optional().

Use devm_regulator_get() so that the reference is released
automatically when the UFS device is detached.

This alone is not enough, though: ufs_mtk_init() powers the M-PHY on,
which enables the va09 regulator, and nothing powers it off again on
the way out -- the variant operations have no .exit callback and
ufshcd_pltfrm_remove() only resumes the device.  The devm release
would then hit WARN_ON(regulator->enable_count) in _regulator_put()
and leak the enable reference.

Balance the use count by adding an .exit callback which powers the
M-PHY off, in the same spirit as the .exit callbacks of ufs-exynos
and ufs-qcom.  ufshcd_hba_exit() invokes it both on driver unbind and
on ufshcd_init() failure after the variant init, so the enable count
is always balanced by the time devm releases the regulator.  It
relies on the state tracking in ufs_mtk_mphy_power_on() and is a
no-op if the M-PHY was never powered on.

Fixes: cf137b3ea49a ("scsi: ufs-mediatek: Support VA09 regulator operations")
Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
---

Changes in v2:
- Keep devm_regulator_get() and additionally add an .exit callback
  that powers the M-PHY off, so the va09 enable count is balanced
  before the devm release.  Without it, _regulator_put() would hit
  WARN_ON(regulator->enable_count) on every driver unbind, since
  ufs_mtk_init() leaves the regulator enabled (reported by Sashiko
  AI review).

Link: https://lore.kernel.org/r/20261118014234.424-1-laixiangwu@cmss.chinamobile.com

 drivers/ufs/host/ufs-mediatek.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 5b6bff2cf..ebbc017d0 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -692,7 +692,7 @@ static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
 {
 	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
 
-	host->reg_va09 = regulator_get(hba->dev, "va09");
+	host->reg_va09 = devm_regulator_get(hba->dev, "va09");
 	if (IS_ERR(host->reg_va09))
 		dev_info(hba->dev, "failed to get va09");
 	else
@@ -1331,6 +1331,14 @@ static int ufs_mtk_init(struct ufs_hba *hba)
 	return err;
 }
 
+static void ufs_mtk_exit(struct ufs_hba *hba)
+{
+	struct ufs_mtk_host *host = ufshcd_get_variant(hba);
+
+	if (host)
+		ufs_mtk_mphy_power_on(hba, false);
+}
+
 static int ufs_mtk_negotiate_pwr_mode(struct ufs_hba *hba,
 				      const struct ufs_pa_layer_attr *dev_max_params,
 				      struct ufs_pa_layer_attr *dev_req_params)
@@ -2353,6 +2361,7 @@ static void ufs_mtk_config_scsi_dev(struct scsi_device *sdev)
 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
 	.name                = "mediatek.ufshci",
 	.init                = ufs_mtk_init,
+	.exit                = ufs_mtk_exit,
 	.get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
 	.setup_clocks        = ufs_mtk_setup_clocks,
 	.hce_enable_notify   = ufs_mtk_hce_enable_notify,
-- 
2.50.1.windows.1




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails
  2026-11-18  3:20 [PATCH v2 0/2] scsi: ufs-mediatek: fix va09 regulator lifecycle Xiangwu Lai
  2026-11-18  3:20 ` [PATCH v2 1/2] scsi: ufs-mediatek: use devm_regulator_get() for the va09 regulator Xiangwu Lai
@ 2026-11-18  3:20 ` Xiangwu Lai
  2026-11-18  3:37   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Xiangwu Lai @ 2026-11-18  3:20 UTC (permalink / raw)
  To: peter.wang, chaotian.jing
  Cc: chu.stanley, alim.akhtar, bvanassche, stanley.chu, sashiko-bot,
	linux-scsi, linux-mediatek, linux-kernel, Xiangwu Lai

In ufs_mtk_mphy_power_on(), the power-off path calls phy_power_off()
before disabling the va09 regulator.  If regulator_disable() fails,
the error is returned while host->mphy_powered_on is left set,
although the M-PHY is physically powered off already.  The next call
with on = true then bails out through the !(on ^ host->mphy_powered_on)
check without powering the M-PHY back on, leaving the UFS link dead
(I/O timeouts, controller lockups).

The M-PHY is off regardless of the va09 disable result, so update the
state flag in the power-off path and let the next power-on retry the
whole sequence.

Found by code inspection while reviewing the va09 error paths of the
driver; the failure has not been observed on real hardware.

Link: https://lore.kernel.org/r/sashiko-outbox-219916@kernel.org
Fixes: cf137b3ea49a ("scsi: ufs-mediatek: Support VA09 regulator operations")
Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
---
 drivers/ufs/host/ufs-mediatek.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index ebbc017d0..8d4b84d58 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -542,6 +542,14 @@ static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
 			ufs_mtk_va09_pwr_ctrl(res, 0);
 			ret = regulator_disable(host->reg_va09);
 		}
+		/*
+		 * The M-PHY is off regardless of the va09 result, so keep
+		 * the state in sync with the hardware.  Otherwise the next
+		 * power-on bails out through the
+		 * !(on ^ host->mphy_powered_on) check without powering the
+		 * M-PHY back on.
+		 */
+		host->mphy_powered_on = false;
 	}
 out:
 	if (ret) {
-- 
2.50.1.windows.1




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails
  2026-11-18  3:20 ` [PATCH v2 2/2] scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails Xiangwu Lai
@ 2026-11-18  3:37   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-18  3:37 UTC (permalink / raw)
  To: Xiangwu Lai; +Cc: linux-scsi

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] scsi: ufs-mediatek: reg_va09 enable_count leak in
  ufs_mtk_mphy_power_on()
- [Medium] scsi: ufs-mediatek: M-PHY left off during ufs_mtk_suspend()
  error recovery
--

commit 228f00d5e6d65bb4a18b8cbe9dee1c4060e6f74c
Author: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>

scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails

This commit sets host->mphy_powered_on to false unconditionally in the
power-off branch of ufs_mtk_mphy_power_on() so that a subsequent power-on
attempt does not bail out early if disabling the va09 regulator failed.

Link: https://lore.kernel.org/r/sashiko-outbox-219916@kernel.org

> diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
> index 4895fe3b2a99e..b1ad983f80ae9 100644
> --- a/drivers/ufs/host/ufs-mediatek.c
> +++ b/drivers/ufs/host/ufs-mediatek.c
> @@ -543,6 +543,14 @@ static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
>  			ufs_mtk_va09_pwr_ctrl(res, 0);
>  			ret = regulator_disable(host->reg_va09);
>  		}
> +		/*
> +		 * The M-PHY is off regardless of the va09 result, so keep
> +		 * the state in sync with the hardware.  Otherwise the next
> +		 * power-on bails out through the
> +		 * !(on ^ host->mphy_powered_on) check without powering the
> +		 * M-PHY back on.
> +		 */
> +		host->mphy_powered_on = false;

[Severity: Medium]
Does setting host->mphy_powered_on = false when regulator_disable() fails
leak an enable_count reference on host->reg_va09?

When _regulator_disable() fails in _regulator_do_disable(), it returns the
error before calling _regulator_handle_consumer_disable(), so
host->reg_va09->enable_count remains 1.  With host->mphy_powered_on cleared,
the next ufs_mtk_mphy_power_on(hba, true) call invokes
regulator_enable(host->reg_va09) again and increments enable_count to 2.
Subsequent power-off calls only decrement enable_count once from 2 to 1,
preventing VA09 from ever being physically disabled and triggering
WARN_ON(regulator->enable_count) in _regulator_put() on driver unbind.

[Severity: Medium]
Does returning the regulator_disable() error with the M-PHY powered off
break error recovery in ufs_mtk_suspend()?

When ufs_mtk_mphy_power_on(hba, false) returns an error to
ufs_mtk_suspend(), ufs_mtk_suspend() jumps to fail, calls
ufshcd_set_link_off(hba), and returns -EAGAIN to __ufshcd_wl_suspend():

drivers/ufs/core/ufshcd.c:__ufshcd_wl_suspend() {
	...
	ret = ufshcd_vops_suspend(hba, pm_op, POST_CHANGE);
	if (ret)
		goto set_link_active;

	goto out;

set_link_active:
	...
	if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
		ufshcd_set_link_active(hba);
	else if (ufshcd_is_link_off(hba))
		ufshcd_host_reset_and_restore(hba);
	...
}

__ufshcd_wl_suspend() aborts the suspend and immediately runs
ufshcd_host_reset_and_restore(hba) without calling ufs_mtk_resume().
Because neither ufshcd_host_reset_and_restore() nor its vops callbacks call
ufs_mtk_mphy_power_on(hba, true), host reset and link startup run while the
M-PHY and VA09 power control are still off.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118032015.974-1-laixiangwu@cmss.chinamobile.com?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18  3:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18  3:20 [PATCH v2 0/2] scsi: ufs-mediatek: fix va09 regulator lifecycle Xiangwu Lai
2026-11-18  3:20 ` [PATCH v2 1/2] scsi: ufs-mediatek: use devm_regulator_get() for the va09 regulator Xiangwu Lai
2026-11-18  3:20 ` [PATCH v2 2/2] scsi: ufs-mediatek: keep mphy state in sync when va09 disable fails Xiangwu Lai
2026-11-18  3:37   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox