Skip to contentSkip to conversation

scsi: ufs: Let devices remain runtime suspended during system suspend

PATCH Superseded Adrian Hunter 3 participants 9 messages Sep 22, 2021 – Sep 30, 2021

Subsystems (2)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover scsi: ufs: Let devices remain runtime suspended during system suspend

Hi

UFS devices can remain runtime suspended at system suspend time, if the conditions are right. Add support for that, first fixing the impediments.

Changes in V5:

scsi: ufs: Fix error handler clear ua deadlock
Update commit message
Try to abort REQUEST SENSE if it times out

Changes in V4:

scsi: ufs: Fix error handler clear ua deadlock

Do request-sense directly

Changes in V3:

scsi: ufs: Fix error handler clear ua deadlock

Correct commit message.
Amend stable tags to add dependent cherry picks

Changes in V2:

scsi: ufs: Let devices remain runtime suspended during system suspend

The ufs-hisi driver uses different RPM and SPM, but it is made explicit by a new parameter to suspend prepare.

Adrian Hunter (3):
scsi: ufs: Fix error handler clear ua deadlock
scsi: ufs: Fix runtime PM dependencies getting broken
scsi: ufs: Let devices remain runtime suspended during system suspend

 drivers/scsi/scsi_pm.c      |  16 ++-
 drivers/scsi/ufs/ufs-hisi.c |   8 +-
 drivers/scsi/ufs/ufs.h      |   3 +-
 drivers/scsi/ufs/ufshcd.c   | 257 ++++++++++++++++++++++++++++++++------------
 drivers/scsi/ufs/ufshcd.h   |  21 +++-
 include/scsi/scsi_device.h  |   1 +
 include/trace/events/ufs.h  |   5 +-
 7 files changed, 233 insertions(+), 78 deletions(-)

Regards
Adrian

#1 lore raw

PATCH v5 1/3 scsi: ufs: Fix error handler clear ua deadlock

There is no guarantee to be able to enter the queue if requests are blocked. That is because freezing the queue will block entry to the queue, but freezing also waits for outstanding requests which can make no progress while the queue is blocked.

That situation can happen when the error handler issues requests to clear unit attention condition. Requests are blocked if the ufshcd_state is UFSHCD_STATE_EH_SCHEDULED_FATAL, which can happen as a result of prior error handler activity. Requests cannot make progress when ufshcd_state is UFSHCD_STATE_EH_SCHEDULED_FATAL, and only the error handler can change that, so if the error handler is waiting to enter the queue and blk_mq_freeze_queue() is waiting for outstanding requests, they will deadlock.

Fix by issuing REQUEST_SENSE directly avoiding the SCSI queues, in a similar fashion as other device commands.

Fixes b294ff3e34490f ("scsi: ufs: core: Enable power management for wlun")
---
 drivers/scsi/ufs/ufs.h     |   3 +-
 drivers/scsi/ufs/ufshcd.c  | 203 +++++++++++++++++++++++++++----------
 drivers/scsi/ufs/ufshcd.h  |   9 ++
 include/trace/events/ufs.h |   5 +-
 4 files changed, 162 insertions(+), 58 deletions(-)
drivers/scsi/ufs/ufs.h +2 -1
index 8c6b38b1b142..c60c6d7c62c1 100644
@@ -612,7 +612,8 @@ struct ufs_dev_info {
 enum ufs_trace_str_t {
 	UFS_CMD_SEND, UFS_CMD_COMP, UFS_DEV_COMP,
 	UFS_QUERY_SEND, UFS_QUERY_COMP, UFS_QUERY_ERR,
-	UFS_TM_SEND, UFS_TM_COMP, UFS_TM_ERR
+	UFS_TM_SEND, UFS_TM_COMP, UFS_TM_ERR,
+	UFS_SENSE_SEND, UFS_SENSE_COMP, UFS_SENSE_ERR,
 };
 
 /*
drivers/scsi/ufs/ufshcd.c +147 -56

279 lines not on this page · raw message

drivers/scsi/ufs/ufshcd.h +9 -0
index 41f6e06f9185..06e743a5937a 100644
@@ -56,6 +56,8 @@ struct ufs_hba;
 enum dev_cmd_type {
 	DEV_CMD_TYPE_NOP		= 0x0,
 	DEV_CMD_TYPE_QUERY		= 0x1,
+	DEV_CMD_TYPE_SENSE		= 0x2,
+	DEV_CMD_TYPE_INVALID		= 0xff,
 };
 
 enum ufs_event_type {
@@ -236,17 +238,24 @@ struct ufs_query {
 	struct ufs_query_res response;
 };
 
+/* UFS_DEV_CMD_BUF_SZ must be a power of 2 and bigger than UFS_SENSE_SIZE */
+#define UFS_DEV_CMD_BUF_SZ 32
+
 /**
  * struct ufs_dev_cmd - all assosiated fields with device management commands
  * @type: device management command type - Query, NOP OUT
  * @lock: lock to allow one command at a time
  * @complete: internal commands completion
+ * @buf: buffer for data
+ * @buf_dma_addr: DMA address of @buf
  */
 struct ufs_dev_cmd {
 	enum dev_cmd_type type;
 	struct mutex lock;
 	struct completion *complete;
 	struct ufs_query query;
+	void *buf;
+	dma_addr_t buf_dma_addr;
 };
 
 /**
include/trace/events/ufs.h +4 -1
index 599739ee7b20..efa2af4a3a5b 100644
@@ -46,7 +46,10 @@
 	EM(UFS_QUERY_ERR,	"query_complete_err")		\
 	EM(UFS_TM_SEND,		"tm_send")			\
 	EM(UFS_TM_COMP,		"tm_complete")			\
-	EMe(UFS_TM_ERR,		"tm_complete_err")
+	EM(UFS_TM_ERR,		"tm_complete_err")		\
+	EM(UFS_SENSE_SEND,	"sense_send")			\
+	EM(UFS_SENSE_COMP,	"sense_complete")		\
+	EMe(UFS_SENSE_ERR,	"sense_complete_err")
 
 #define UFS_CMD_TRACE_TSF_TYPES					\
 	EM(UFS_TSF_CDB,		"CDB")		                \
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH V5 1/3] scsi: ufs: Fix error handler clear ua deadlock

Quoted 1 line

+static int ufshcd_request_sense_direct(struct ufs_hba *hba, u8 wlun)

..

How about using ufshcd_compose_dev_cmd w/ modifying the function, ufshcd_prepare_utp_scsi_cmd_upiu and something ? I think it can be made w/ small changes. And for prdt, it seems to be good because it's tiny.

#3 lore raw

Re: [PATCH V5 1/3] scsi: ufs: Fix error handler clear ua deadlock

Quoted 7 lines · Kiwoong Kim
Quoted 1 line

+static int ufshcd_request_sense_direct(struct ufs_hba *hba, u8 wlun)

..

How about using ufshcd_compose_dev_cmd w/ modifying the function, ufshcd_prepare_utp_scsi_cmd_upiu and something ? I think it can be made w/ small changes. And for prdt, it seems to be good because it's tiny.

I have done that in V6

#4 lore raw

Re: [PATCH V5 1/3] scsi: ufs: Fix error handler clear ua deadlock

In reply to Adrian Hunter

(I post this again because this isn't attached to the mail thread)

UFSHCI specifies Data Byte Count (DBC) like this:
A '0' based value that indicates the length, in bytes, of the data block. A maximum of length of 256KB may exist for any entry. Bits 1:0 of this field shall be 11b to indicate Dword granularity.
A value of '3' indicates 4 bytes, '7' indicates 8 bytes, etc.

That means the size value should be aligned with 4. And as I know it's 18. Yes, its buffer is enough but if a host doesn't access memory with four-byte alignment, it could cause problems.

Thanks.
Kiwoong Kim

#5 lore raw

Re: [PATCH V5 1/3] scsi: ufs: Fix error handler clear ua deadlock

Quoted 11 lines · Kiwoong Kim

(I post this again because this isn't attached to the mail thread)

UFSHCI specifies Data Byte Count (DBC) like this:
A '0' based value that indicates the length, in bytes, of the data block. A maximum of length of 256KB may exist for any entry. Bits 1:0 of this field shall be 11b to indicate Dword granularity.
A value of '3' indicates 4 bytes, '7' indicates 8 bytes, etc.

That means the size value should be aligned with 4. And as I know it's 18. Yes, its buffer is enough but if a host doesn't access memory with four-byte alignment, it could cause problems.

Fixed in V6

#6 lore raw

PATCH v5 2/3 scsi: ufs: Fix runtime PM dependencies getting broken

In reply to Adrian Hunter

UFS SCSI devices make use of device links to establish PM dependencies. However, SCSI PM will force devices' runtime PM state to be active during system resume. That can break runtime PM dependencies for UFS devices. Fix by adding a flag 'preserve_rpm' to let UFS SCSI devices opt-out of the unwanted behaviour.

Fixes b294ff3e34490f ("scsi: ufs: core: Enable power management for wlun")
---
 drivers/scsi/scsi_pm.c     | 16 +++++++++++-----
 drivers/scsi/ufs/ufshcd.c  |  1 +
 include/scsi/scsi_device.h |  1 +
 3 files changed, 13 insertions(+), 5 deletions(-)
drivers/scsi/scsi_pm.c +11 -5
index 3717eea37ecb..0557c1ad304d 100644
@@ -73,13 +73,22 @@ static int scsi_dev_type_resume(struct device *dev,
 		int (*cb)(struct device *, const struct dev_pm_ops *))
 {
 	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
+	struct scsi_device *sdev = NULL;
+	bool preserve_rpm = false;
 	int err = 0;
 
+	if (scsi_is_sdev_device(dev)) {
+		sdev = to_scsi_device(dev);
+		preserve_rpm = sdev->preserve_rpm;
+		if (preserve_rpm && pm_runtime_suspended(dev))
+			return 0;
+	}
+
 	err = cb(dev, pm);
 	scsi_device_resume(to_scsi_device(dev));
 	dev_dbg(dev, "scsi resume: %d\n", err);
 
-	if (err == 0) {
+	if (err == 0 && !preserve_rpm) {
 		pm_runtime_disable(dev);
 		err = pm_runtime_set_active(dev);
 		pm_runtime_enable(dev);
@@ -91,11 +100,8 @@ static int scsi_dev_type_resume(struct device *dev,
 		 *
 		 * The resume hook will correct runtime PM status of the disk.
 		 */
-		if (!err && scsi_is_sdev_device(dev)) {
-			struct scsi_device *sdev = to_scsi_device(dev);
-
+		if (!err && sdev)
 			blk_set_runtime_active(sdev->request_queue);
-		}
 	}
 
 	return err;
drivers/scsi/ufs/ufshcd.c +1 -0
index d725390aea99..39fc6070d2f7 100644
@@ -5025,6 +5025,7 @@ static int ufshcd_slave_configure(struct scsi_device *sdev)
 		pm_runtime_get_noresume(&sdev->sdev_gendev);
 	else if (ufshcd_is_rpm_autosuspend_allowed(hba))
 		sdev->rpm_autosuspend = 1;
+	sdev->preserve_rpm = 1;
 
 	ufshcd_crypto_setup_rq_keyslot_manager(hba, q);
 
include/scsi/scsi_device.h +1 -0
index b97e142a7ca9..24884600d2d4 100644
@@ -196,6 +196,7 @@ struct scsi_device {
 	unsigned no_read_disc_info:1;	/* Avoid READ_DISC_INFO cmds */
 	unsigned no_read_capacity_16:1; /* Avoid READ_CAPACITY_16 cmds */
 	unsigned try_rc_10_first:1;	/* Try READ_CAPACACITY_10 first */
+	unsigned preserve_rpm:1;	/* Preserve runtime PM */
 	unsigned security_supported:1;	/* Supports Security Protocols */
 	unsigned is_visible:1;	/* is the device visible in sysfs */
 	unsigned wce_default_on:1;	/* Cache is ON by default */
Signature · 1 line
2.17.1
#7 lore raw

PATCH v5 3/3 scsi: ufs: Let devices remain runtime suspended during system suspend

In reply to Adrian Hunter

If the UFS Device WLUN is runtime suspended and is in the same power mode, link state and b_rpm_dev_flush_capable (BKOP or WB buffer flush etc) state, then it can remain runtime suspended instead of being runtime resumed and then system suspended.

The following patches have cleared the way for that to happen:
scsi: ufs: Fix runtime PM dependencies getting broken
scsi: ufs: Fix error handler clear ua deadlock

So amend the logic accordingly.

Note, the ufs-hisi driver uses different RPM and SPM, but it is made explicit by a new parameter to suspend prepare.

---
 drivers/scsi/ufs/ufs-hisi.c |  8 +++++-
 drivers/scsi/ufs/ufshcd.c   | 53 ++++++++++++++++++++++++++++---------
 drivers/scsi/ufs/ufshcd.h   | 12 ++++++++-
 3 files changed, 58 insertions(+), 15 deletions(-)
drivers/scsi/ufs/ufs-hisi.c +7 -1
index 6b706de8354b..4a08fb35642c 100644
@@ -396,6 +396,12 @@ static int ufs_hisi_pwr_change_notify(struct ufs_hba *hba,
 	return ret;
 }
 
+static int ufs_hisi_suspend_prepare(struct device *dev)
+{
+	/* RPM and SPM are different. Refer ufs_hisi_suspend() */
+	return __ufshcd_suspend_prepare(dev, false);
+}
+
 static int ufs_hisi_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
 {
 	struct ufs_hisi_host *host = ufshcd_get_variant(hba);
@@ -574,7 +580,7 @@ static int ufs_hisi_remove(struct platform_device *pdev)
 static const struct dev_pm_ops ufs_hisi_pm_ops = {
 	SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
 	SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
-	.prepare	 = ufshcd_suspend_prepare,
+	.prepare	 = ufs_hisi_suspend_prepare,
 	.complete	 = ufshcd_resume_complete,
 };
 
drivers/scsi/ufs/ufshcd.c +40 -13

73 lines not on this page · raw message

drivers/scsi/ufs/ufshcd.h +11 -1
index 06e743a5937a..c64a92ef15a4 100644
@@ -929,7 +929,6 @@ struct ufs_hba {
 #endif
 	u32 luns_avail;
 	bool complete_put;
-	bool rpmb_complete_put;
 };
 
 /* Returns true if clocks can be gated. Otherwise false */
@@ -1189,6 +1188,7 @@ int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
 
 int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable);
 int ufshcd_suspend_prepare(struct device *dev);
+int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm);
 void ufshcd_resume_complete(struct device *dev);
 
 /* Wrapper functions for safely calling variant operations */
@@ -1397,6 +1397,16 @@ static inline int ufshcd_rpm_put_sync(struct ufs_hba *hba)
 	return pm_runtime_put_sync(&hba->sdev_ufs_device->sdev_gendev);
 }
 
+static inline void ufshcd_rpm_get_noresume(struct ufs_hba *hba)
+{
+	pm_runtime_get_noresume(&hba->sdev_ufs_device->sdev_gendev);
+}
+
+static inline int ufshcd_rpm_resume(struct ufs_hba *hba)
+{
+	return pm_runtime_resume(&hba->sdev_ufs_device->sdev_gendev);
+}
+
 static inline int ufshcd_rpm_put(struct ufs_hba *hba)
 {
 	return pm_runtime_put(&hba->sdev_ufs_device->sdev_gendev);
Signature · 1 line
2.17.1
Reviewed-by on v5 3/3
#8 lore raw
Quoted 9 lines · Adrian Hunter

If the UFS Device WLUN is runtime suspended and is in the same power mode, link state and b_rpm_dev_flush_capable (BKOP or WB buffer flush etc) state, then it can remain runtime suspended instead of being runtime resumed and then system suspended.

The following patches have cleared the way for that to happen:
scsi: ufs: Fix runtime PM dependencies getting broken
scsi: ufs: Fix error handler clear ua deadlock

Quoted 7 lines

So amend the logic accordingly.

Note, the ufs-hisi driver uses different RPM and SPM, but it is made explicit by a new parameter to suspend prepare.

---

LGTM.

Quoted 148 lines
  drivers/scsi/ufs/ufs-hisi.c |  8 +++++-
  drivers/scsi/ufs/ufshcd.c   | 53 ++++++++++++++++++++++++++++---------
  drivers/scsi/ufs/ufshcd.h   | 12 ++++++++-
  3 files changed, 58 insertions(+), 15 deletions(-)
drivers/scsi/ufs/ufs-hisi.c +7 -1
index 6b706de8354b..4a08fb35642c 100644
@@ -396,6 +396,12 @@ static int ufs_hisi_pwr_change_notify(struct ufs_hba *hba,
  	return ret;
  }
 
+static int ufs_hisi_suspend_prepare(struct device *dev)
+{
+	/* RPM and SPM are different. Refer ufs_hisi_suspend() */
+	return __ufshcd_suspend_prepare(dev, false);
+}
+
  static int ufs_hisi_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  {
  	struct ufs_hisi_host *host = ufshcd_get_variant(hba);
@@ -574,7 +580,7 @@ static int ufs_hisi_remove(struct platform_device *pdev)
  static const struct dev_pm_ops ufs_hisi_pm_ops = {
  	SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
  	SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
-	.prepare	 = ufshcd_suspend_prepare,
+	.prepare	 = ufs_hisi_suspend_prepare,
  	.complete	 = ufshcd_resume_complete,
  };
 
drivers/scsi/ufs/ufshcd.c +40 -13
index 39fc6070d2f7..64e81ece1aae 100644
@@ -9797,14 +9797,30 @@ void ufshcd_resume_complete(struct device *dev)
  		ufshcd_rpm_put(hba);
  		hba->complete_put = false;
  	}
-	if (hba->rpmb_complete_put) {
-		ufshcd_rpmb_rpm_put(hba);
-		hba->rpmb_complete_put = false;
-	}
  }
  EXPORT_SYMBOL_GPL(ufshcd_resume_complete);
 
-int ufshcd_suspend_prepare(struct device *dev)
+static bool ufshcd_rpm_ok_for_spm(struct ufs_hba *hba)
+{
+	struct device *dev = &hba->sdev_ufs_device->sdev_gendev;
+	enum ufs_dev_pwr_mode dev_pwr_mode;
+	enum uic_link_state link_state;
+	unsigned long flags;
+	bool res;
+
+	spin_lock_irqsave(&dev->power.lock, flags);
+	dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl);
+	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;
+	spin_unlock_irqrestore(&dev->power.lock, flags);
+
+	return res;
+}
+
+int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm)
  {
  	struct ufs_hba *hba = dev_get_drvdata(dev);
  	int ret;
@@ -9816,19 +9832,30 @@ int ufshcd_suspend_prepare(struct device *dev)
  	 * Refer ufshcd_resume_complete()
  	 */
  	if (hba->sdev_ufs_device) {
-		ret = ufshcd_rpm_get_sync(hba);
-		if (ret < 0 && ret != -EACCES) {
-			ufshcd_rpm_put(hba);
-			return ret;
+		/* Prevent runtime suspend */
+		ufshcd_rpm_get_noresume(hba);
+		/*
+		 * Check if already runtime suspended in same state as system
+		 * suspend would be.
+		 */
+		if (!rpm_ok_for_spm || !ufshcd_rpm_ok_for_spm(hba)) {
+			/* RPM state is not ok for SPM, so runtime resume */
+			ret = ufshcd_rpm_resume(hba);
+			if (ret < 0 && ret != -EACCES) {
+				ufshcd_rpm_put(hba);
+				return ret;
+			}
  		}
  		hba->complete_put = true;
  	}
-	if (hba->sdev_rpmb) {
-		ufshcd_rpmb_rpm_get_sync(hba);
-		hba->rpmb_complete_put = true;
-	}
  	return 0;
  }
+EXPORT_SYMBOL_GPL(__ufshcd_suspend_prepare);
+
+int ufshcd_suspend_prepare(struct device *dev)
+{
+	return __ufshcd_suspend_prepare(dev, true);
+}
  EXPORT_SYMBOL_GPL(ufshcd_suspend_prepare);
 
  #ifdef CONFIG_PM_SLEEP
drivers/scsi/ufs/ufshcd.h +11 -1
index 06e743a5937a..c64a92ef15a4 100644
@@ -929,7 +929,6 @@ struct ufs_hba {
  #endif
  	u32 luns_avail;
  	bool complete_put;
-	bool rpmb_complete_put;
  };
 
  /* Returns true if clocks can be gated. Otherwise false */
@@ -1189,6 +1188,7 @@ int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
 
  int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable);
  int ufshcd_suspend_prepare(struct device *dev);
+int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm);
  void ufshcd_resume_complete(struct device *dev);
 
  /* Wrapper functions for safely calling variant operations */
@@ -1397,6 +1397,16 @@ static inline int ufshcd_rpm_put_sync(struct ufs_hba *hba)
  	return pm_runtime_put_sync(&hba->sdev_ufs_device->sdev_gendev);
  }
 
+static inline void ufshcd_rpm_get_noresume(struct ufs_hba *hba)
+{
+	pm_runtime_get_noresume(&hba->sdev_ufs_device->sdev_gendev);
+}
+
+static inline int ufshcd_rpm_resume(struct ufs_hba *hba)
+{
+	return pm_runtime_resume(&hba->sdev_ufs_device->sdev_gendev);
+}
+
  static inline int ufshcd_rpm_put(struct ufs_hba *hba)
  {
  	return pm_runtime_put(&hba->sdev_ufs_device->sdev_gendev);
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
Linux Foundation Collaborative Project