mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stanley Jhu <stanleyjhu@google.com>
To: linux-scsi@vger.kernel.org, "Martin K. Petersen" <mkp@kernel.org>
Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	 Bart Van Assche <bvanassche@acm.org>,
	Alim Akhtar <alim.akhtar@samsung.com>,
	 Avri Altman <avri.altman@sandisk.com>,
	Bean Huo <beanhuo@micron.com>,
	 Peter Wang <peter.wang@mediatek.com>,
	Stanley Chu <stanley.chu@mediatek.com>,
	 Can Guo <quic_cang@quicinc.com>,
	"Bao D. Nguyen" <quic_nguyenb@quicinc.com>,
	 Asutosh Das <quic_asutoshd@quicinc.com>,
	Hongjie Fang <hongjiefang@asrmicro.com>,
	 Praveen Talari <praveen.talari@oss.qualcomm.com>,
	HOYOUNG SEO <hy50.seo@samsung.com>,
	 Brian Kao <powenkao@google.com>, Jens Axboe <axboe@kernel.dk>,
	 "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
	linux-block@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: [PATCH 7/9] scsi: ufs: core: Unwind variant resume in EH when reset fails
Date: Thu, 12 Nov 2026 13:15:40 +0800	[thread overview]
Message-ID: <20261112051542.1539779-8-stanleyjhu@google.com> (raw)
In-Reply-To: <20261112051542.1539779-1-stanleyjhu@google.com>

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@vger.kernel.org # 7.3.x
Signed-off-by: Stanley Jhu <stanleyjhu@google.com>
---
 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


  parent reply	other threads:[~2026-11-12  5:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  5:15 [PATCH 0/9] scsi: ufs: core: Fix device management and PM recovery Stanley Jhu
2026-11-12  5:15 ` [PATCH 1/9] scsi: ufs: core: Do not poll unconfigured MCQ queues in the error handler Stanley Jhu
2026-11-12  5:15 ` [PATCH 2/9] scsi: ufs: core: Defer link recovery on reserved PM command timeout Stanley Jhu
2026-11-12  5:15 ` [PATCH 3/9] scsi: ufs: core: Wait for the reserved tag in ufshcd_get_dev_mgmt_cmd() Stanley Jhu
2026-11-12  5:15 ` [PATCH 4/9] scsi: ufs: core: Fail PM commands fast when fatal recovery is scheduled Stanley Jhu
2026-11-12  5:15 ` [PATCH 5/9] scsi: ufs: core: Disable W-LUN runtime PM in ufshcd_err_handling_prepare() Stanley Jhu
2026-11-12  5:15 ` [PATCH 6/9] block: Keep RPM_ACTIVE in blk_pre_runtime_resume() when already active Stanley Jhu
2026-11-12  5:15 ` Stanley Jhu [this message]
2026-11-12  5:15 ` [PATCH 8/9] scsi: ufs: core: Wake suspended consumer queues after host reset Stanley Jhu
2026-11-12  5:15 ` [PATCH 9/9] scsi: ufs: core: Do not recover the link concurrently with a PM callback Stanley Jhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112051542.1539779-8-stanleyjhu@google.com \
    --to=stanleyjhu@google.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=alim.akhtar@samsung.com \
    --cc=avri.altman@sandisk.com \
    --cc=axboe@kernel.dk \
    --cc=beanhuo@micron.com \
    --cc=bvanassche@acm.org \
    --cc=hongjiefang@asrmicro.com \
    --cc=hy50.seo@samsung.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=peter.wang@mediatek.com \
    --cc=powenkao@google.com \
    --cc=praveen.talari@oss.qualcomm.com \
    --cc=quic_asutoshd@quicinc.com \
    --cc=quic_cang@quicinc.com \
    --cc=quic_nguyenb@quicinc.com \
    --cc=rafael.j.wysocki@intel.com \
    --cc=stanley.chu@mediatek.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®