* [PATCH 0/3] scsi: ufs: core: Fix hibern8 and clock gating state handling
@ 2026-12-02 23:51 Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 1/3] scsi: ufs: core: Set CLKS_ON when ufshcd_gate_work() keeps clocks on Vladimir Zapolskiy
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Vladimir Zapolskiy @ 2026-12-02 23:51 UTC (permalink / raw)
To: James E.J. Bottomley, Martin K. Petersen, Bart Van Assche
Cc: Alim Akhtar, Bean Huo, Stanley Jhu, linux-scsi
The UFS core can lose track of the link and clock gating states when a
W-LUN resume fails or when the host is runtime suspended with an active
link. On SM8650 this led to a PHY calibration failure on host reset,
needless UIC errors and stalls of 8 to 11 seconds after every runtime
suspend.
The three fixes are independent of each other and can be applied in any
order:
* set CLKS_ON when ufshcd_gate_work() keeps the clocks on,
* enter hibern8 in ufshcd_gate_work() only on an active link,
* enter hibern8 before ufshcd_suspend() gates the clocks of an active
link on hosts with UFSHCD_CAP_HIBERN8_WITH_CLK_GATING.
The problems were found during the UFS stress testing on SM8650 (runtime
PM level matrix, LU resets during writes, fatal error injection with
the W-LUN runtime suspended).
Vladimir Zapolskiy (3):
scsi: ufs: core: Set CLKS_ON when ufshcd_gate_work() keeps clocks on
scsi: ufs: core: Enter hibern8 in ufshcd_gate_work() only on an active link
scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks
drivers/ufs/core/ufshcd.c | 31 +++++++++++++++++++++++--------
1 file changed, 23 insertions(+), 8 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/3] scsi: ufs: core: Set CLKS_ON when ufshcd_gate_work() keeps clocks on
2026-12-02 23:51 [PATCH 0/3] scsi: ufs: core: Fix hibern8 and clock gating state handling Vladimir Zapolskiy
@ 2026-12-02 23:51 ` Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 2/3] scsi: ufs: core: Enter hibern8 in ufshcd_gate_work() only on an active link Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 3/3] scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks Vladimir Zapolskiy
2 siblings, 0 replies; 5+ messages in thread
From: Vladimir Zapolskiy @ 2026-12-02 23:51 UTC (permalink / raw)
To: James E.J. Bottomley, Martin K. Petersen, Bart Van Assche
Cc: Alim Akhtar, Bean Huo, Stanley Jhu, linux-scsi
ufshcd_gate_work() returns without gating when a request is active,
the device is busy or the host is not operational, but leaves the state
at REQ_CLKS_OFF. ufshcd_hold() then queues the ungate work, which
enables the clocks a second time. This powers the PHY on twice, and
the next host reset powers it off only once, so the PHY is never power
cycled and its calibration fails with "Failed to calibrate PHY: -110".
Set CLKS_ON on these returns, as the is_suspended return already does.
Check the device and host state under clk_gating.lock as well, in the
lock order of __ufshcd_release(), so that no ufshcd_hold() or release
runs between the check and the state update.
Fixes: 1ab27c9cf8b6 ("ufs: Add support for clock gating")
Signed-off-by: Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
---
drivers/ufs/core/ufshcd.c | 21 ++++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 7d0e60e07302..3ec8e27df554 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -2056,6 +2056,7 @@ static void ufshcd_gate_work(struct work_struct *work)
{
struct ufs_hba *hba = container_of(work, struct ufs_hba,
clk_gating.gate_work.work);
+ bool keep_on;
int ret;
scoped_guard(spinlock_irqsave, &hba->clk_gating.lock) {
@@ -2073,14 +2074,20 @@ static void ufshcd_gate_work(struct work_struct *work)
return;
}
- if (hba->clk_gating.active_reqs)
- return;
- }
-
- scoped_guard(spinlock_irqsave, &hba->host->host_lock) {
- if (ufshcd_is_ufs_dev_busy(hba) ||
- hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
+ /*
+ * The clocks stay on. Do not leave the state at REQ_CLKS_OFF:
+ * ufshcd_hold() would take that for "gating done or running"
+ * and switch the clocks on a second time.
+ */
+ scoped_guard(spinlock_irqsave, &hba->host->host_lock)
+ keep_on = ufshcd_is_ufs_dev_busy(hba) ||
+ hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL;
+ if (hba->clk_gating.active_reqs || keep_on) {
+ hba->clk_gating.state = CLKS_ON;
+ trace_ufshcd_clk_gating(hba,
+ hba->clk_gating.state);
return;
+ }
}
/* put the link into hibern8 mode before turning off clocks */
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/3] scsi: ufs: core: Enter hibern8 in ufshcd_gate_work() only on an active link
2026-12-02 23:51 [PATCH 0/3] scsi: ufs: core: Fix hibern8 and clock gating state handling Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 1/3] scsi: ufs: core: Set CLKS_ON when ufshcd_gate_work() keeps clocks on Vladimir Zapolskiy
@ 2026-12-02 23:51 ` Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 3/3] scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks Vladimir Zapolskiy
2 siblings, 0 replies; 5+ messages in thread
From: Vladimir Zapolskiy @ 2026-12-02 23:51 UTC (permalink / raw)
To: James E.J. Bottomley, Martin K. Petersen, Bart Van Assche
Cc: Alim Akhtar, Bean Huo, Stanley Jhu, linux-scsi
ufshcd_gate_work() sends DME_HIBERNATE_ENTER without checking the link
state, while ufshcd_ungate_work() exits hibern8 only if the link is in
hibern8. After a failed W-LUN resume left the link in hibern8, the gate
work caused a UIC error, a 500 ms power mode change timeout and an error
handler run.
Enter hibern8 only if the link is active, otherwise only gate the clocks.
Fixes: 1ab27c9cf8b6 ("ufs: Add support for clock gating")
Signed-off-by: Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
---
drivers/ufs/core/ufshcd.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 3ec8e27df554..eedd74d7dbdd 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -2091,7 +2091,8 @@ static void ufshcd_gate_work(struct work_struct *work)
}
/* put the link into hibern8 mode before turning off clocks */
- if (ufshcd_can_hibern8_during_gating(hba)) {
+ if (ufshcd_can_hibern8_during_gating(hba) &&
+ ufshcd_is_link_active(hba)) {
ret = ufshcd_uic_hibern8_enter(hba);
if (ret) {
hba->clk_gating.state = CLKS_ON;
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/3] scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks
2026-12-02 23:51 [PATCH 0/3] scsi: ufs: core: Fix hibern8 and clock gating state handling Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 1/3] scsi: ufs: core: Set CLKS_ON when ufshcd_gate_work() keeps clocks on Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 2/3] scsi: ufs: core: Enter hibern8 in ufshcd_gate_work() only on an active link Vladimir Zapolskiy
@ 2026-12-02 23:51 ` Vladimir Zapolskiy
2026-12-03 0:14 ` sashiko-bot
2 siblings, 1 reply; 5+ messages in thread
From: Vladimir Zapolskiy @ 2026-12-02 23:51 UTC (permalink / raw)
To: James E.J. Bottomley, Martin K. Petersen, Bart Van Assche
Cc: Alim Akhtar, Bean Huo, Stanley Jhu, linux-scsi
Hosts with UFSHCD_CAP_HIBERN8_WITH_CLK_GATING may gate their clocks only
with the link in hibern8, and if ufshcd_gate_work() complies to it,
ufshcd_suspend() does not, because with rpm_lvl 0 or 2 it turns the
clocks off under an active link. As a result no request then completes
after the next resume until the host is reset, and every access after
a runtime suspend stalls for 8 to 11 seconds.
Put an active link into hibern8 first; the W-LUN resume exits it again.
If that fails, return -EBUSY to keep the host staying active without
a runtime PM error.
Fixes: 57d104c153d3 ("ufs: add UFS power management support")
Signed-off-by: Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
---
drivers/ufs/core/ufshcd.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index eedd74d7dbdd..b96f8a5592b1 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10809,6 +10809,13 @@ static int ufshcd_suspend(struct ufs_hba *hba)
if (!hba->is_powered)
return 0;
+
+ if (ufshcd_can_hibern8_during_gating(hba) &&
+ ufshcd_is_link_active(hba)) {
+ if (ufshcd_uic_hibern8_enter(hba))
+ return -EBUSY;
+ ufshcd_set_link_hibern8(hba);
+ }
/*
* Disable the host irq as host controller as there won't be any
* host controller transaction expected till resume.
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 3/3] scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks
2026-12-02 23:51 ` [PATCH 3/3] scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks Vladimir Zapolskiy
@ 2026-12-03 0:14 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 0:14 UTC (permalink / raw)
To: Vladimir Zapolskiy; +Cc: linux-scsi
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] scsi: ufs: core: link state mismatch between vops suspend and
resume
- [High] scsi: ufs: core: broken link state on hibern8 failure in
ufshcd_suspend()
- [Medium] scsi: ufs: core: link state mismatch in
ufshcd_rpm_ok_for_spm()
- [Medium] scsi: ufs: core: missing hibern8 rollback in ufshcd_suspend()
error path
- [Low] scsi: ufs: core: Fixes tag cites 57d104c153d3 instead of
1ab27c9cf8b6
--
commit 6a05a39d71fec3d3537f39c83c27329442fd5d45
Author: Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
> scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks
>
> Hosts with UFSHCD_CAP_HIBERN8_WITH_CLK_GATING may gate their clocks only
> with the link in hibern8, and if ufshcd_gate_work() complies to it,
> ufshcd_suspend() does not, because with rpm_lvl 0 or 2 it turns the
> clocks off under an active link. As a result no request then completes
> after the next resume until the host is reset, and every access after
> a runtime suspend stalls for 8 to 11 seconds.
>
> Put an active link into hibern8 first; the W-LUN resume exits it again.
> If that fails, return -EBUSY to keep the host staying active without
> a runtime PM error.
>
> Fixes: 57d104c153d3 ("ufs: add UFS power management support")
> Signed-off-by: Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
[Severity: Low]
57d104c153d3 ("ufs: add UFS power management support") predates UFS clock
gating and UFSHCD_CAP_HIBERN8_WITH_CLK_GATING. Should the Fixes: tag cite
1ab27c9cf8b6 ("ufs: Add support for clock gating") instead?
> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index eedd74d7dbddb..b96f8a5592b17 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -10809,6 +10809,13 @@ static int ufshcd_suspend(struct ufs_hba *hba)
>
> if (!hba->is_powered)
> return 0;
> +
> + if (ufshcd_can_hibern8_during_gating(hba) &&
> + ufshcd_is_link_active(hba)) {
> + if (ufshcd_uic_hibern8_enter(hba))
> + return -EBUSY;
[Severity: High]
Can a failure in ufshcd_uic_hibern8_enter() here leave the link in
UIC_LINK_BROKEN_STATE and cause the next W-LUN resume to fail?
Unlike __ufshcd_wl_suspend(), ufshcd_suspend() runs after the W-LUN has
suspended and cleared hba->pm_op_in_progress to false. When
ufshcd_uic_hibern8_enter() fails with !hba->pm_op_in_progress,
ufshcd_uic_pwr_ctrl() marks the link broken and schedules fatal error
handling:
drivers/ufs/core/ufshcd.c:ufshcd_uic_pwr_ctrl() {
...
if (ret && !hba->pm_op_in_progress) {
ufshcd_set_link_broken(hba);
ufshcd_schedule_eh_work(hba);
}
...
}
When __ufshcd_wl_resume() subsequently runs (either during system suspend
rollback while eh_work is blocked on hba->host_sem, or from
ufshcd_err_handling_prepare() -> ufshcd_rpm_get_sync() after runtime
suspend returns -EBUSY), it only checks ufshcd_is_link_hibern8() and
ufshcd_is_link_off(), skipping link recovery for UIC_LINK_BROKEN_STATE.
At PM level 2, __ufshcd_wl_resume() then calls ufshcd_set_dev_pwr_mode()
while hba->ufshcd_state is UFSHCD_STATE_EH_SCHEDULED_FATAL, and
ufshcd_queuecommand() immediately fails the START_STOP_UNIT command with
DID_BAD_TARGET.
> + ufshcd_set_link_hibern8(hba);
> + }
[Severity: High]
Does transitioning the link to Hibern8 in ufshcd_suspend() break link
state symmetry between ufshcd_vops_suspend() and ufshcd_vops_resume()?
At PM levels 0 and 2, __ufshcd_wl_suspend() runs ufshcd_vops_suspend()
while hba->uic_link_state is still UIC_LINK_ACTIVE_STATE, before
ufshcd_suspend() enters Hibern8 and sets UIC_LINK_HIBERN8_STATE. On
resume, __ufshcd_wl_resume() runs ufshcd_vops_resume() before exiting
Hibern8, so vops->resume() sees UIC_LINK_HIBERN8_STATE.
In drivers/ufs/host/ufs-exynos.c, exynos_ufs_suspend() and
exynos_ufs_resume() both gate PHY power on !ufshcd_is_link_active(hba):
drivers/ufs/host/ufs-exynos.c:exynos_ufs_suspend() {
...
if (!ufshcd_is_link_active(hba))
phy_power_off(ufs->phy);
return 0;
}
drivers/ufs/host/ufs-exynos.c:exynos_ufs_resume() {
...
if (!ufshcd_is_link_active(hba))
phy_power_on(ufs->phy);
return 0;
}
exynos_ufs_suspend() skips phy_power_off(), while exynos_ufs_resume()
calls phy_power_on(), leaking a ufs->phy->power_count reference on every
suspend/resume cycle. ufs_mtk_suspend() / ufs_mtk_resume() and
ufs_qcom_suspend() have similar checks on ufshcd_is_link_hibern8() and
ufshcd_is_link_active().
[Severity: Medium]
Does setting hba->uic_link_state to UIC_LINK_HIBERN8_STATE at rpm_lvl 0
or 2 also break ufshcd_rpm_ok_for_spm() during system suspend preparation?
drivers/ufs/core/ufshcd.c:ufshcd_rpm_ok_for_spm() {
...
link_state = ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl);
res = pm_runtime_suspended(dev) &&
hba->curr_dev_pwr_mode == dev_pwr_mode &&
hba->uic_link_state == link_state &&
!hba->dev_info.b_rpm_dev_flush_capable;
...
}
For PM levels 0 and 2, ufs_get_pm_lvl_to_link_pwr_state() returns
UIC_LINK_ACTIVE_STATE. If spm_lvl is 0 or 2, hba->uic_link_state is now
UIC_LINK_HIBERN8_STATE, so ufshcd_rpm_ok_for_spm() returns false and
__ufshcd_suspend_prepare() forces a runtime resume just to re-suspend into
the same state. Conversely, if spm_lvl is 1 or 3, hba->uic_link_state now
matches UIC_LINK_HIBERN8_STATE and ufshcd_rpm_ok_for_spm() returns true,
skipping system suspend even though __ufshcd_wl_suspend() and
ufshcd_vops_suspend() only ran the active-link path.
> /*
> * Disable the host irq as host controller as there won't be any
> * host controller transaction expected till resume.
> */
> ufshcd_disable_irq(hba);
> ret = ufshcd_setup_clocks(hba, false);
> if (ret) {
> ufshcd_enable_irq(hba);
> goto out;
> }
[Severity: Medium]
If ufshcd_setup_clocks(hba, false) fails here, does ufshcd_suspend()
return an error while leaving the link in Hibern8 and hba->uic_link_state
set to UIC_LINK_HIBERN8_STATE? Also, returning -EBUSY directly when
ufshcd_uic_hibern8_enter() fails bypasses the out: label and skips logging
UFS_EVT_SUSPEND_ERR via ufshcd_update_evt_hist().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202235113.1632498-1-vladimir.zapolskiy@linaro.org?part=3
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-03 0:14 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 23:51 [PATCH 0/3] scsi: ufs: core: Fix hibern8 and clock gating state handling Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 1/3] scsi: ufs: core: Set CLKS_ON when ufshcd_gate_work() keeps clocks on Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 2/3] scsi: ufs: core: Enter hibern8 in ufshcd_gate_work() only on an active link Vladimir Zapolskiy
2026-12-02 23:51 ` [PATCH 3/3] scsi: ufs: core: Enter hibern8 before ufshcd_suspend() gates the clocks Vladimir Zapolskiy
2026-12-03 0:14 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.