Skip to contentSkip to conversation

Improve the UFS driver link hibernation code

PATCH Superseded Bart Van Assche 2 participants 11 messages Sep 9, 2024 – Sep 11, 2024

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Bart Van Assche

author reviewer
#0 lore raw

v2 cover Improve the UFS driver link hibernation code

Hi Martin,

UFS controllers can behave as follows with regard to link hibernation:
(a) The UIC completion interrupt is not generated after having submitted a power management command.
(b) The UIC completion interrupt is generated and reenabling the UIC completion interrupt does not affect the link hibernation state.
(c) The UIC completion interrupt is generated and reenabling the UIC completion interrupt causes the link to exit the link hibernation state.

Support these cases as follows:
* Support (a) by setting UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS in the host controller driver and by disabling UIC completion interrupts before submitting a power management command.
* Support (b) and (c) by leaving UIC completion interrupts enabled while submitting a power management command.

Please consider this patch series for the next merge window.

Thanks,

Bart.

Changes compared to v1 of this patch series:
- A patch that improves the struct ufs_hba documentation has been added.
- Patch 2/2 has been split into two patches.
- Instead of leaving the UIC completion interrupt enabled, disable it if UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS has been set.

Bart Van Assche (4):
scsi: ufs: core: Improve the struct ufs_hba documentation
scsi: ufs: core: Make ufshcd_uic_cmd_compl() easier to read
scsi: ufs: core: Always initialize the UIC done completion
scsi: ufs: core: Change the approach for power change UIC commands

 drivers/ufs/core/ufshcd.c       | 47 ++++++++++++++++++++-------------
 drivers/ufs/host/ufs-mediatek.c |  1 +
 drivers/ufs/host/ufs-qcom.c     |  2 ++
 include/ufs/ufshcd.h            | 13 ++++++---
 4 files changed, 41 insertions(+), 22 deletions(-)
author reviewer
#1 lore raw

PATCH v2 1/4 scsi: ufs: core: Improve the struct ufs_hba documentation

Make the role of the structure members related to UIC command processing more clear.

---
 include/ufs/ufshcd.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/ufs/ufshcd.h +4 -3
index a43b14276bc3..85933775c9f3 100644
@@ -868,9 +868,10 @@ enum ufshcd_mcq_opr {
  * @tmf_tag_set: TMF tag set.
  * @tmf_queue: Used to allocate TMF tags.
  * @tmf_rqs: array with pointers to TMF requests while these are in progress.
- * @active_uic_cmd: handle of active UIC command
- * @uic_cmd_mutex: mutex for UIC command
- * @uic_async_done: completion used during UIC processing
+ * @active_uic_cmd: pointer to active UIC command.
+ * @uic_cmd_mutex: mutex used for serializing UIC command processing.
+ * @uic_async_done: completion used to wait for power mode or hibernation state
+ *	changes.
  * @ufshcd_state: UFSHCD state
  * @eh_flags: Error handling flags
  * @intr_mask: Interrupt Mask Bits
maintainer Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] scsi: ufs: core: Improve the struct ufs_hba documentation

Quoted 25 lines · Bart Van Assche

External email : Please do not click links or open attachments until you have verified the sender or the content.
Make the role of the structure members related to UIC command
processing
more clear.

---
 include/ufs/ufshcd.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/ufs/ufshcd.h +0 -0
index a43b14276bc3..85933775c9f3 100644
@@ -868,9 +868,10 @@ enum ufshcd_mcq_opr {
  * @tmf_tag_set: TMF tag set.
  * @tmf_queue: Used to allocate TMF tags.
  * @tmf_rqs: array with pointers to TMF requests while these are in

progress.
- * @active_uic_cmd: handle of active UIC command
- * @uic_cmd_mutex: mutex for UIC command
- * @uic_async_done: completion used during UIC processing
+ * @active_uic_cmd: pointer to active UIC command.

Quoted 8 lines

+ * @uic_cmd_mutex: mutex used for serializing UIC command processing.
+ * @uic_async_done: completion used to wait for power mode or hibernation state
+ * changes.
* @ufshcd_state: UFSHCD state
* @eh_flags: Error handling flags
* @intr_mask: Interrupt Mask Bits

author reviewer
#3 lore raw

PATCH v2 2/4 scsi: ufs: core: Make ufshcd_uic_cmd_compl() easier to read

In reply to Bart Van Assche

Introduce a local variable for the expression hba->active_uic_cmd. Remove superfluous parentheses. No functionality has been changed.

---
 drivers/ufs/core/ufshcd.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
drivers/ufs/core/ufshcd.c +10 -11
index 8ea5a82503a9..134cba0ff512 100644
@@ -5458,31 +5458,30 @@ static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
 static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
 {
 	irqreturn_t retval = IRQ_NONE;
+	struct uic_command *cmd;
 
 	spin_lock(hba->host->host_lock);
+	cmd = hba->active_uic_cmd;
 	if (ufshcd_is_auto_hibern8_error(hba, intr_status))
 		hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
 
-	if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
-		hba->active_uic_cmd->argument2 |=
-			ufshcd_get_uic_cmd_result(hba);
-		hba->active_uic_cmd->argument3 =
-			ufshcd_get_dme_attr_val(hba);
+	if (intr_status & UIC_COMMAND_COMPL && cmd) {
+		cmd->argument2 |= ufshcd_get_uic_cmd_result(hba);
+		cmd->argument3 = ufshcd_get_dme_attr_val(hba);
 		if (!hba->uic_async_done)
-			hba->active_uic_cmd->cmd_active = 0;
-		complete(&hba->active_uic_cmd->done);
+			cmd->cmd_active = 0;
+		complete(&cmd->done);
 		retval = IRQ_HANDLED;
 	}
 
-	if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
-		hba->active_uic_cmd->cmd_active = 0;
+	if (intr_status & UFSHCD_UIC_PWR_MASK && hba->uic_async_done) {
+		cmd->cmd_active = 0;
 		complete(hba->uic_async_done);
 		retval = IRQ_HANDLED;
 	}
 
 	if (retval == IRQ_HANDLED)
-		ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
-					     UFS_CMD_COMP);
+		ufshcd_add_uic_command_trace(hba, cmd, UFS_CMD_COMP);
 	spin_unlock(hba->host->host_lock);
 	return retval;
 }
author reviewer
#4 lore raw

PATCH v2 3/4 scsi: ufs: core: Always initialize the UIC done completion

In reply to Bart Van Assche

Simplify __ufshcd_send_uic_cmd() by always initializing the uic_cmd::done completion. This is fine since the time required to initialize a completion is small compared to the time required to process an UIC command.

---
 drivers/ufs/core/ufshcd.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/ufs/core/ufshcd.c +4 -7
index 134cba0ff512..063fb66c6719 100644
@@ -2537,13 +2537,11 @@ ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
  * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
  * @hba: per adapter instance
  * @uic_cmd: UIC command
- * @completion: initialize the completion only if this is set to true
  *
  * Return: 0 only if success.
  */
 static int
-__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
-		      bool completion)
+__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
 {
 	lockdep_assert_held(&hba->uic_cmd_mutex);
 
@@ -2553,8 +2551,7 @@ __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
 		return -EIO;
 	}
 
-	if (completion)
-		init_completion(&uic_cmd->done);
+	init_completion(&uic_cmd->done);
 
 	uic_cmd->cmd_active = 1;
 	ufshcd_dispatch_uic_cmd(hba, uic_cmd);
@@ -2580,7 +2577,7 @@ int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
 	mutex_lock(&hba->uic_cmd_mutex);
 	ufshcd_add_delay_before_dme_cmd(hba);
 
-	ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
+	ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
 	if (!ret)
 		ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
 
@@ -4270,7 +4267,7 @@ static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
 		reenable_intr = true;
 	}
 	spin_unlock_irqrestore(hba->host->host_lock, flags);
-	ret = __ufshcd_send_uic_cmd(hba, cmd, false);
+	ret = __ufshcd_send_uic_cmd(hba, cmd);
 	if (ret) {
 		dev_err(hba->dev,
 			"pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
#5 lore raw

Re: [PATCH v2 3/4] scsi: ufs: core: Always initialize the UIC done completion

Quoted 10 lines · Bart Van Assche

External email : Please do not click links or open attachments until you have verified the sender or the content.
Simplify __ufshcd_send_uic_cmd() by always initializing the
uic_cmd::done completion. This is fine since the time required to initialize a completion is small compared to the time required to process an UIC command.

Hi Bart,

The time to compare should not be with the UIC command process time. It should be compared with the time for the "if (completion)" conditional
expression. We cannot justify increasing the latency of sending a UIC command just because the UIC command process time is longer.

Thanks.
Peter

author reviewer
#6 lore raw

Re: [PATCH v2 3/4] scsi: ufs: core: Always initialize the UIC done completion

Quoted 11 lines · Peter Wang (王信友)
Quoted 4 lines · Bart Van Assche

Simplify __ufshcd_send_uic_cmd() by always initializing the
uic_cmd::done completion. This is fine since the time required to initialize a completion is small compared to the time required to process an UIC command.

The time to compare should not be with the UIC command process time. It should be compared with the time for the "if (completion)" conditional
expression. We cannot justify increasing the latency of sending a UIC command just because the UIC command process time is longer.

Although I appreciate your concern about performance, I don't think that the above feedback is appropriate. On my test setup UIC commands take between 20 and 1500 microseconds to complete. A single init_completion() call takes about 0.01 microseconds. So I don't think that the time spent in init_completion() matters for UIC commands. Additionally, this patch only introduces an init_completion() call for power management commands and not any other type of UIC command.

Thanks,

Bart.

maintainer Reviewed-by
#7 lore raw

Re: [PATCH v2 3/4] scsi: ufs: core: Always initialize the UIC done completion

Quoted 31 lines · Bart Van Assche

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 7 lines · Peter Wang (王信友)
Quoted 4 lines · Bart Van Assche

Simplify __ufshcd_send_uic_cmd() by always initializing the
uic_cmd::done completion. This is fine since the time required to initialize a completion is small compared to the time required to process an UIC command.

The time to compare should not be with the UIC command process

time.

Quoted 3 lines

It should be compared with the time for the "if (completion)" conditional
expression. We cannot justify increasing the latency of sending a

UIC

Quoted 1 line

command just because the UIC command process time is longer.

Although I appreciate your concern about performance, I don't think that
the above feedback is appropriate. On my test setup UIC commands take between 20 and 1500 microseconds to complete. A single init_completion()
call takes about 0.01 microseconds. So I don't think that the time spent
in init_completion() matters for UIC commands. Additionally, this patch
only introduces an init_completion() call for power management commands
and not any other type of UIC command.

Quoted 4 lines

Thanks,

Bart.

Hi Bart,

I have concerns about adding latency just for power management commands.
This is because when DVFS needs performance and wants to scale up, it will take extra time to complete the scaling up process. However, I agree that such a 0.01ms delay may not have a significant impact on the overall scale-up and read/write process.
It might be helpful to add this information to the patch description.

Thank you.
Peter

author reviewer
#8 lore raw

PATCH v2 4/4 scsi: ufs: core: Change the approach for power change UIC commands

In reply to Bart Van Assche

For some host controllers it is required that UIC completion interrupts are disabled while a power control command is submitted while for other host controllers it is required that UIC completion interrupts remain enabled. Hence introduce a quirk for preserving the current behavior and leave UIC completion interrupts enabled if that quirk has not been set. Although it has not yet been observed that the UIC completion interrupt is reported after the power mode change interrupt, handle this case by adding a wait_for_completion_timeout() call on uic_cmd::done.

Note: the code for toggling the UIC completion interrupt was introduced by commit d75f7fe495cf ("scsi: ufs: reduce the interrupts for power mode change requests").

---
 drivers/ufs/core/ufshcd.c       | 15 ++++++++++++++-
 drivers/ufs/host/ufs-mediatek.c |  1 +
 drivers/ufs/host/ufs-qcom.c     |  2 ++
 include/ufs/ufshcd.h            |  6 ++++++
 4 files changed, 23 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +14 -1
index 063fb66c6719..23cd6f4a6ca2 100644
@@ -4257,7 +4257,8 @@ static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
 		goto out_unlock;
 	}
 	hba->uic_async_done = &uic_async_done;
-	if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
+	if (hba->quirks & UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS &&
+	    ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
 		ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
 		/*
 		 * Make sure UIC command completion interrupt is disabled before
@@ -4275,6 +4276,7 @@ static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
 		goto out;
 	}
 
+	/* Wait for power mode change interrupt. */
 	if (!wait_for_completion_timeout(hba->uic_async_done,
 					 msecs_to_jiffies(uic_cmd_timeout))) {
 		dev_err(hba->dev,
@@ -4291,6 +4293,17 @@ static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
 		goto out;
 	}
 
+	if (!reenable_intr) {
+		/* Wait for UIC completion interrupt. */
+		ret = wait_for_completion_timeout(&cmd->done,
+					  msecs_to_jiffies(uic_cmd_timeout));
+		WARN_ON_ONCE(ret < 0);
+		if (ret == 0)
+			dev_err(hba->dev, "UIC command %#x timed out\n",
+				cmd->command);
+		ret = 0;
+	}
+
 check_upmcrs:
 	status = ufshcd_get_upmcrs(hba);
 	if (status != PWR_LOCAL) {
drivers/ufs/host/ufs-mediatek.c +1 -0
index 02c9064284e1..4e18ecc54f9f 100644
@@ -1021,6 +1021,7 @@ static int ufs_mtk_init(struct ufs_hba *hba)
 	hba->quirks |= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL;
 	hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_INTR;
 	hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_RTC;
+	hba->quirks |= UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS;
 	hba->vps->wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(80);
 
 	if (host->caps & UFS_MTK_CAP_DISABLE_AH8)
drivers/ufs/host/ufs-qcom.c +2 -0
index 810e637047d0..07a62de80d2e 100644
@@ -852,6 +852,8 @@ static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
 {
 	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
 
+	hba->quirks |= UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS;
+
 	if (host->hw_ver.major == 0x2)
 		hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
 
include/ufs/ufshcd.h +6 -0
index 85933775c9f3..787c44a341a7 100644
@@ -676,6 +676,12 @@ enum ufshcd_quirks {
 	 * the standard best practice for managing keys).
 	 */
 	UFSHCD_QUIRK_KEYS_IN_PRDT			= 1 << 24,
+
+	/*
+	 * Disable the UIC interrupt before submitting any power mode change
+	 * commands.
+	 */
+	UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS	= 1 << 25,
 };
 
 enum ufshcd_caps {
#9 lore raw

Re: [PATCH v2 4/4] scsi: ufs: core: Change the approach for power change UIC commands

Quoted 87 lines · Bart Van Assche

External email : Please do not click links or open attachments until you have verified the sender or the content.
For some host controllers it is required that UIC completion
interrupts are
disabled while a power control command is submitted while for other host
controllers it is required that UIC completion interrupts remain enabled.
Hence introduce a quirk for preserving the current behavior and leave UIC
completion interrupts enabled if that quirk has not been set. Although it
has not yet been observed that the UIC completion interrupt is reported
after the power mode change interrupt, handle this case by adding a wait_for_completion_timeout() call on uic_cmd::done.

Note: the code for toggling the UIC completion interrupt was introduced
by commit d75f7fe495cf ("scsi: ufs: reduce the interrupts for power mode
change requests").

---
 drivers/ufs/core/ufshcd.c       | 15 ++++++++++++++-
 drivers/ufs/host/ufs-mediatek.c |  1 +
 drivers/ufs/host/ufs-qcom.c     |  2 ++
 include/ufs/ufshcd.h            |  6 ++++++
 4 files changed, 23 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +2 -1
index 063fb66c6719..23cd6f4a6ca2 100644
@@ -4257,7 +4257,8 @@ static int ufshcd_uic_pwr_ctrl(struct ufs_hba
 *hba, struct uic_command *cmd)
 		goto out_unlock;
 	}
 	hba->uic_async_done = &uic_async_done;
-	if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) &
 UIC_COMMAND_COMPL) {
+	if (hba->quirks & UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS &&
+	    ufshcd_readl(hba, REG_INTERRUPT_ENABLE) &
 UIC_COMMAND_COMPL) {
drivers/ufs/core/ufshcd.c +0 -0
 		ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
 		/*
 		 * Make sure UIC command completion interrupt is

disabled before

drivers/ufs/core/ufshcd.c +1 -0
@@ -4275,6 +4276,7 @@ static int ufshcd_uic_pwr_ctrl(struct ufs_hba
 *hba, struct uic_command *cmd)
 		goto out;
 	}
 
+	/* Wait for power mode change interrupt. */
 	if (!wait_for_completion_timeout(hba->uic_async_done,
 					 msecs_to_jiffies(uic_cmd_timeo

ut))) {

drivers/ufs/core/ufshcd.c +11 -0
 		dev_err(hba->dev,
@@ -4291,6 +4293,17 @@ static int ufshcd_uic_pwr_ctrl(struct ufs_hba
 *hba, struct uic_command *cmd)
 		goto out;
 	}
 
+	if (!reenable_intr) {
+		/* Wait for UIC completion interrupt. */
+		ret = wait_for_completion_timeout(&cmd->done,
+					  msecs_to_jiffies(uic_cmd_time
 out));
+		WARN_ON_ONCE(ret < 0);
+		if (ret == 0)
+			dev_err(hba->dev, "UIC command %#x timed
 out\n",
+				cmd->command);
+		ret = 0;
+	}
+

check_upmcrs:

drivers/ufs/core/ufshcd.c +0 -0
 	status = ufshcd_get_upmcrs(hba);
 	if (status != PWR_LOCAL) {
drivers/ufs/host/ufs- renamed from drivers/ufs/host/ufs-mediatek.c +0 -0

mediatek.c
index 02c9064284e1..4e18ecc54f9f 100644

drivers/ufs/host/ufs-mediatek.c static int ufs_mtk_init(struct ufs_hba *hba) · line 1026

Quoted 6 lines
drivers/ufs/host/ufs-mediatek.c +1 -0
@@ -1021,6 +1021,7 @@ static int ufs_mtk_init(struct ufs_hba *hba)
 	hba->quirks |= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL;
 	hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_INTR;
 	hba->quirks |= UFSHCD_QUIRK_MCQ_BROKEN_RTC;
+	hba->quirks |= UFSHCD_QUIRK_DISABLE_UIC_INTR_FOR_PWR_CMDS;
 
 

Hi Bart,

I'm not sure what other SoC host think, but the meaning of a "quirk" should be to use it in situations that do not follow the spec and require special handling. However, MediaTek designs according to the spec, so using a quirk may give the impression that MediaTek does not follow the spec.

Moreover, it shouldn't be the case that only MediaTek and Qualcomm need to add this quirk."

Thanks.
Peter

Quoted 1 line
drivers/ufs/host/ufs-mediatek.c +0 -0
 enum ufshcd_caps {
author reviewer
#10 lore raw

Re: [PATCH v2 4/4] scsi: ufs: core: Change the approach for power change UIC commands

Quoted 8 lines · Peter Wang (王信友)

I'm not sure what other SoC host think, but the meaning of a "quirk" should be to use it in situations that do not follow the spec and require special handling. However, MediaTek designs according to the spec, so using a quirk may give the impression that MediaTek does not follow the spec.

Moreover, it shouldn't be the case that only MediaTek and Qualcomm need to add this quirk."

Got it. Let me think further about this and see whether there is perhaps a better solution.

Thanks,

Bart.