[PATCH 7/9] scsi: ufs: core: Unwind variant resume in EH when reset fails

From: Stanley Jhu

Date: Thu Nov 12 2026 - 00:19:38 EST


ufshcd_err_handling_prepare() resumes the host variant with
UFS_SYSTEM_PM or UFS_RUNTIME_PM when hba->is_sys_suspended is true or
the UFS device W-LUN is runtime suspended. When no host reset
succeeds, ufshcd_err_handling_unprepare() does not undo that resume.

Along the runtime PM path:

1. __ufshcd_wl_suspend() suspends the variant, and the W-LUN enters
RPM_SUSPENDED.
2. A fatal controller error schedules ufshcd_err_handler().
3. ufshcd_err_handling_prepare() calls ufshcd_vops_resume() with
UFS_RUNTIME_PM.
4. ufshcd_reset_and_restore() fails and sets hba->ufshcd_state to
UFSHCD_STATE_ERROR.
5. ufshcd_err_handling_unprepare() returns without calling
ufshcd_vops_suspend(), and the variant stays runtime-resumed on the
dead host.

Along the system PM path:

1. ufshcd_wl_suspend() suspends the variant and sets
hba->is_sys_suspended.
2. START STOP UNIT fails on all three attempts in ufshcd_wl_resume().
3. __ufshcd_wl_resume() suspends the variant again and returns -EIO.
4. A fatal controller error schedules ufshcd_err_handler().
5. ufshcd_err_handling_prepare() calls ufshcd_vops_resume() with
UFS_SYSTEM_PM.
6. ufshcd_reset_and_restore() fails and leaves the variant
system-resumed on the dead host.

Suspend the variant in ufshcd_err_handling_unprepare() when no host
reset in the error handler run succeeded, so that the variant power
state matches the W-LUN and system power state. Suspending the variant
there needs three more changes.

First, track hba->vops_suspended in ufshcd_vops_suspend() and
ufshcd_vops_resume(). The link is active after a successful host reset,
so ufshcd_err_handling_unprepare() keeps the variant resumed even when
the W-LUN stays RPM_SUSPENDED. hba->vops_suspended then makes the next
__ufshcd_wl_resume() skip the variant resume callback.

Second, set hba->force_reset in ufshcd_err_handler() when
hba->is_sys_suspended is true. A non-fatal UIC error otherwise ends
error handling without a host reset. ufshcd_err_handling_unprepare()
would then suspend the variant before it unquiesces the queues.

Third, keep pm_recovered set once a host reset succeeds. Error recovery
may run several passes. A pass with a successful reset runs
ufshcd_recover_pm_error(), which clears hba->is_sys_suspended and may
activate the W-LUN before a later pass fails to reset the host.

Fixes: c72e79c0ad2b ("scsi: ufs: Recover HBA runtime PM error in error handler")
Cc: stable@xxxxxxxxxxxxxxx # 7.3.x
Signed-off-by: Stanley Jhu <stanleyjhu@xxxxxxxxxx>
---
drivers/ufs/core/ufshcd-priv.h | 19 +++++++++++----
drivers/ufs/core/ufshcd.c | 43 +++++++++++++++++++++++++++-------
include/ufs/ufshcd.h | 3 +++
3 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/drivers/ufs/core/ufshcd-priv.h b/drivers/ufs/core/ufshcd-priv.h
index 99f654b79035..a63f7567017e 100644
--- a/drivers/ufs/core/ufshcd-priv.h
+++ b/drivers/ufs/core/ufshcd-priv.h
@@ -238,18 +238,29 @@ static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba)
static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op,
enum ufs_notify_change_status status)
{
+ int ret = 0;
+
if (hba->vops && hba->vops->suspend)
- return hba->vops->suspend(hba, op, status);
+ ret = hba->vops->suspend(hba, op, status);
+ if (!ret && status == POST_CHANGE)
+ hba->vops_suspended = true;

- return 0;
+ return ret;
}

static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
{
+ int ret = 0;
+
+ if (!hba->vops_suspended)
+ return 0;
+
if (hba->vops && hba->vops->resume)
- return hba->vops->resume(hba, op);
+ ret = hba->vops->resume(hba, op);
+ if (!ret)
+ hba->vops_suspended = false;

- return 0;
+ return ret;
}

static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 7e3f9abc7e7c..286ffb44772d 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -6907,8 +6907,16 @@ static void ufshcd_clk_scaling_suspend(struct ufs_hba *hba, bool suspend)
}
}

-static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
+/*
+ * Return true if the W-LUN or the system is suspended and the host variant is
+ * not suspended when this function returns. This includes the case where an
+ * earlier error handler run left the variant resumed and ufshcd_vops_resume()
+ * returns 0 without calling the variant.
+ */
+static bool ufshcd_err_handling_prepare(struct ufs_hba *hba)
{
+ bool vops_resumed = false;
+
/*
* A WLUN resume failure could potentially lead to the HBA being
* runtime suspended, so take an extra reference on hba->dev.
@@ -6934,7 +6942,7 @@ static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
if (!ufshcd_is_clkgating_allowed(hba))
ufshcd_setup_clocks(hba, true);
pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM;
- ufshcd_vops_resume(hba, pm_op);
+ vops_resumed = !ufshcd_vops_resume(hba, pm_op);
} else {
ufshcd_hold(hba);
if (ufshcd_is_clkscaling_supported(hba) &&
@@ -6950,11 +6958,24 @@ static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
*/
blk_mq_unquiesce_queue(hba->host->pseudo_sdev->request_queue);
cancel_work_sync(&hba->eeh_work);
+ return vops_resumed;
}

-static void ufshcd_err_handling_unprepare(struct ufs_hba *hba)
+static void ufshcd_err_handling_unprepare(struct ufs_hba *hba,
+ bool vops_resumed, bool pm_recovered)
{
blk_mq_quiesce_queue_nowait(hba->host->pseudo_sdev->request_queue);
+ /*
+ * !pm_recovered: no recovery ran, so the W-LUN or system is
+ * suspended.
+ */
+ if (vops_resumed && !pm_recovered) {
+ enum ufs_pm_op pm_op = hba->is_sys_suspended ?
+ UFS_SYSTEM_PM : UFS_RUNTIME_PM;
+
+ ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE);
+ ufshcd_vops_suspend(hba, pm_op, POST_CHANGE);
+ }
blk_mq_unquiesce_tagset(&hba->host->tag_set);
ufshcd_release(hba);
if (ufshcd_is_clkscaling_supported(hba))
@@ -7116,7 +7137,10 @@ static void ufshcd_err_handler(struct work_struct *work)
int retries = MAX_ERR_HANDLER_RETRIES;
struct ufs_hba *hba;
unsigned long flags;
+ /* Set once a reset succeeds and ufshcd_recover_pm_error() has run. */
+ bool pm_recovered = false;
bool needs_restore;
+ bool vops_resumed;
bool needs_reset;
bool stop;
int pmc_err;
@@ -7166,7 +7190,7 @@ static void ufshcd_err_handler(struct work_struct *work)
ufshcd_set_eh_wlun_fence(hba);
spin_unlock_irqrestore(&hba->host->host_lock, flags);

- ufshcd_err_handling_prepare(hba);
+ vops_resumed = ufshcd_err_handling_prepare(hba);

spin_lock_irqsave(&hba->host->host_lock, flags);
ufshcd_set_eh_in_progress(hba);
@@ -7184,7 +7208,8 @@ static void ufshcd_err_handler(struct work_struct *work)

if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
hba->ufshcd_state = UFSHCD_STATE_RESET;
- if (pm_runtime_status_suspended(&hba->ufs_device_wlun->sdev_gendev))
+ if (pm_runtime_status_suspended(&hba->ufs_device_wlun->sdev_gendev) ||
+ hba->is_sys_suspended)
hba->force_reset = true;
/*
* A full reset and restore might have happened after preparation
@@ -7292,11 +7317,13 @@ static void ufshcd_err_handler(struct work_struct *work)
hba->force_reset = false;
spin_unlock_irqrestore(&hba->host->host_lock, flags);
err = ufshcd_reset_and_restore(hba);
- if (err)
+ if (err) {
dev_err(hba->dev, "%s: reset and restore failed with err %d\n",
__func__, err);
- else
+ } else {
+ pm_recovered = true;
ufshcd_recover_pm_error(hba);
+ }
spin_lock_irqsave(&hba->host->host_lock, flags);
}

@@ -7318,7 +7345,7 @@ static void ufshcd_err_handler(struct work_struct *work)
ufshcd_clear_eh_wlun_fence(hba);
ufshcd_clear_eh_in_progress(hba);
spin_unlock_irqrestore(&hba->host->host_lock, flags);
- ufshcd_err_handling_unprepare(hba);
+ ufshcd_err_handling_unprepare(hba, vops_resumed, pm_recovered);
up(&hba->host_sem);

dev_info(hba->dev, "%s finished; HBA state %s\n", __func__,
diff --git a/include/ufs/ufshcd.h b/include/ufs/ufshcd.h
index be62165b785e..df366d11f6ae 100644
--- a/include/ufs/ufshcd.h
+++ b/include/ufs/ufshcd.h
@@ -1038,6 +1038,8 @@ enum ufshcd_mcq_opr {
* @devfreq: frequency scaling information owned by the devfreq core
* @clk_scaling: frequency scaling information owned by the UFS driver
* @is_sys_suspended: UFS device has been suspended because of system suspend
+ * @vops_suspended: set by a successful POST_CHANGE ufshcd_vops_suspend(),
+ * cleared by a successful ufshcd_vops_resume()
* @urgent_bkops_lvl: keeps track of urgent bkops level for device
* @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for
* device is known or not.
@@ -1226,6 +1228,7 @@ struct ufs_hba {
struct devfreq *devfreq;
struct ufs_clk_scaling clk_scaling;
bool is_sys_suspended;
+ bool vops_suspended;

enum bkops_status urgent_bkops_lvl;
bool is_urgent_bkops_lvl_checked;
--
2.56.0.567.gdb80ae8cd5-goog