Prepare for upstreaming Pixel 6 UFS support
cover Prepare for upstreaming Pixel 6 UFS support
Hi Martin,
The UFS controller in the Google Pixel 6 phone requires that SCSI command processing is suspended while reprogramming encryption keys. The patches in this series are a first step towards integrating support in the upstream kernel for the UFS controller in the Pixel 6. Please consider these patches for inclusion in kernel v5.19.
Note: instructions for downloading the Pixel 6 source code are available at
https://source.android.com/setup/build/building-kernels.
Thanks,
Bart.
Bart Van Assche (4):
scsi: ufs: Reduce the clock scaling latency
scsi: ufs: Move a clock scaling check
scsi: ufs: Pass the clock scaling timeout as an argument
scsi: ufs: Add suspend/resume SCSI command processing support
drivers/scsi/ufs/ufshcd.c | 50 ++++++++++++++++++++++++++++++++++----- drivers/scsi/ufs/ufshcd.h | 3 +++ 2 files changed, 47 insertions(+), 6 deletions(-)
PATCH 1/4 scsi: ufs: Reduce the clock scaling latency
Wait at most 20 ms before rechecking the doorbells instead of waiting for a potentially long time between doorbell checks.
--- drivers/scsi/ufs/ufshcd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/ufs/ufshcd.c +1 -1
@@ -1141,7 +1141,7 @@ static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, }spin_unlock_irqrestore (hba->host->host_lock, flags); -schedule (); +io_schedule_timeout (msecs_to_jiffies (20 ));if (ktime_to_us (ktime_sub (ktime_get (), start)) > wait_timeout_us) { timeout = true;
RE: [PATCH 1/4] scsi: ufs: Reduce the clock scaling latency
Quoted 4 lines
Wait at most 20 ms before rechecking the doorbells instead of waiting for a potentially long time between doorbell checks.
Signed-off-by Bart Van Assche <[email protected]>
Nit:
An upper bound of 20msec seems reasonable.
Maybe we should create some linkage to DOORBELL_CLR_TOUT_US?
Quoted 18 lines
--- drivers/scsi/ufs/ufshcd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/scsi/ufs/ufshcd.c +1
-1@@ -1141,7 +1141,7 @@ static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, }spin_unlock_irqrestore (hba->host->host_lock, flags); -schedule (); +io_schedule_timeout (msecs_to_jiffies (20 ));if (ktime_to_us (ktime_sub (ktime_get (), start)) > wait_timeout_us) {timeout = true;
Re: [PATCH 1/4] scsi: ufs: Reduce the clock scaling latency
Quoted 1 line · Avri Altman
Maybe we should create some linkage to
DOORBELL_CLR_TOUT_US?
Hi Avri,
Do you perhaps want me to change
io_schedule_timeout(msecs_to_jiffies(20));
into something like the following?
io_schedule_timeout(msecs_to_jiffies(min(20, remaining_time)));
Documenting that wait_timeout_us may be exceeded by 20 ms seems like the easier solution to me :-)
Thanks,
Bart.
RE: [PATCH 1/4] scsi: ufs: Reduce the clock scaling latency
Quoted 9 lines · Avri Altman
Quoted 1 line
Maybe we should create some linkage to
DOORBELL_CLR_TOUT_US?Hi Avri,
Do you perhaps want me to change
io_schedule_timeout(msecs_to_jiffies(20));
Quoted 7 lines
into something like the following?
io_schedule_timeout(msecs_to_jiffies(min(20, remaining_time)));Documenting that
wait_timeout_usmay be exceeded by 20 ms seems like the
easier solution to me :-)
I meant some fraction of DOORBELL_CLR_TOUT_US but that's fine - It is going away anyway.
Thanks,
Avri
Quoted 4 lines
Thanks,
Bart.
PATCH 2/4 scsi: ufs: Move a clock scaling check
In reply to Bart Van AsscheMove a check related to clock scaling into ufshcd_devfreq_scale(). This patch prepares for adding a second ufshcd_clock_scaling_prepare() caller.
--- drivers/scsi/ufs/ufshcd.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/scsi/ufs/ufshcd.c +9 -2
@@ -1223,8 +1223,7 @@ static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)ufshcd_scsi_block_requests (hba);down_write (&hba->clk_scaling_lock); -if (!hba->clk_scaling.is_allowed || -ufshcd_wait_for_doorbell_clr (hba,DOORBELL_CLR_TOUT_US )) { +if (ufshcd_wait_for_doorbell_clr (hba,DOORBELL_CLR_TOUT_US )) { ret = -EBUSY ;up_write (&hba->clk_scaling_lock);ufshcd_scsi_unblock_requests (hba);
@@ -1262,10 +1261,18 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)int ret =0 ;bool is_writelock = true; +if (!hba->clk_scaling.is_allowed) +return -EBUSY ; + ret =ufshcd_clock_scaling_prepare (hba);if (ret)return ret; +if (!hba->clk_scaling.is_allowed) { + ret = -EBUSY ; + goto out_unprepare; + } +/* scale down the gear before scaling down clocks */ if (!scale_up) { ret =ufshcd_scale_gear (hba, false);
RE: [PATCH 2/4] scsi: ufs: Move a clock scaling check
Quoted 4 lines
Move a check related to clock scaling into
ufshcd_devfreq_scale(). This patch prepares for adding a secondufshcd_clock_scaling_prepare()caller.
A caller for ufshcd_clock_scaling_prepare() in which clk_scaling is not allowed ?
Thanks,
Avri
Quoted 41 lines
Signed-off-by Bart Van Assche <[email protected]>--- drivers/scsi/ufs/ufshcd.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/scsi/ufs/ufshcd.c +1
-2@@ -1223,8 +1223,7 @@ static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)ufshcd_scsi_block_requests (hba );down_write (&hba->clk_scaling_lock); -if (!hba->clk_scaling.is_allowed || -ufshcd_wait_for_doorbell_clr (hba,DOORBELL_CLR_TOUT_US )) { +if (ufshcd_wait_for_doorbell_clr (hba,DOORBELL_CLR_TOUT_US )) { ret = -EBUSY ;up_write (&hba->clk_scaling_lock);ufshcd_scsi_unblock_requests(hba);drivers/scsi/ufs/ufshcd.c +8
-0@@ -1262,10 +1261,18 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)int ret =0 ;bool is_writelock = true; +if (!hba->clk_scaling.is_allowed) +return -EBUSY ; + ret =ufshcd_clock_scaling_prepare (hba);if (ret)return ret; +if (!hba->clk_scaling.is_allowed) { + ret = -EBUSY ; + goto out_unprepare; + } +/* scale down the gear before scaling down clocks */ if (!scale_up) {ret = ufshcd_scale_gear(hba, false);
Re: [PATCH 2/4] scsi: ufs: Move a clock scaling check
Quoted 5 lines · Avri Altman
Quoted 3 lines
Move a check related to clock scaling into
ufshcd_devfreq_scale(). This patch prepares for adding a secondufshcd_clock_scaling_prepare()caller.A caller for
ufshcd_clock_scaling_prepare()in whichclk_scalingis not allowed ?
Hi Avri,
This patch prepares for adding a caller from outside the clock scaling code path. I will integrate this text in the patch description.
Thanks,
Bart.
PATCH 3/4 scsi: ufs: Pass the clock scaling timeout as an argument
In reply to Bart Van AsschePrepare for adding an additional ufshcd_clock_scaling_prepare() call with a different timeout.
--- drivers/scsi/ufs/ufshcd.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/scsi/ufs/ufshcd.c +15 -4
@@ -1108,6 +1108,12 @@ static u32 ufshcd_pending_cmds(struct ufs_hba *hba)return pending; } +/* +* Wait until all pending SCSI commands and TMFs have finished or the timeout +* has expired. +* +* Return: 0 upon success; -EBUSY upon timeout. +*/ static int ufshcd_wait_for_doorbell_clr (struct ufs_hba *hba,u64 wait_timeout_us) {
@@ -1212,9 +1218,14 @@ static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)return ret; } -static int ufshcd_clock_scaling_prepare (struct ufs_hba *hba) +/* +* Wait until all pending SCSI commands and TMFs have finished or the timeout +* has expired. +* +* Return: 0 upon success; -EBUSY upon timeout. +*/ +static int ufshcd_clock_scaling_prepare (struct ufs_hba *hba,u64 timeout_us) { -#define DOORBELL_CLR_TOUT_US (1000 * 1000)/* 1 sec */ int ret =0 ;/* * make sure that there are no outstanding requests when
@@ -1223,7 +1234,7 @@ static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)ufshcd_scsi_block_requests (hba);down_write (&hba->clk_scaling_lock); -if (ufshcd_wait_for_doorbell_clr (hba,DOORBELL_CLR_TOUT_US )) { +if (ufshcd_wait_for_doorbell_clr (hba, timeout_us)) { ret = -EBUSY ;up_write (&hba->clk_scaling_lock);ufshcd_scsi_unblock_requests (hba);
@@ -1264,7 +1275,7 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)if (!hba->clk_scaling.is_allowed)return -EBUSY ; - ret =ufshcd_clock_scaling_prepare (hba); + ret =ufshcd_clock_scaling_prepare (hba,1 *USEC_PER_SEC );if (ret)return ret;
RE: [PATCH 3/4] scsi: ufs: Pass the clock scaling timeout as an argument
Quoted 4 lines
Prepare for adding an additional
ufshcd_clock_scaling_prepare()call with a different timeout.Signed-off-by Bart Van Assche <[email protected]>
Quoted 62 lines
--- drivers/scsi/ufs/ufshcd.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)drivers/scsi/ufs/ufshcd.c +2
-0@@ -1108,6 +1108,12 @@ static u32 ufshcd_pending_cmds(struct ufs_hba *hba)return pending; } +/* +* Wait until all pending SCSI commands and TMFs have finished or the timeout
+ * has expired.
+ *
+ * Return: 0 upon success;-EBUSYupon timeout.
+ */
static int ufshcd_wait_for_doorbell_clr(structufs_hba*hba,
u64wait_timeout_us)
{drivers/scsi/ufs/ufshcd.c +2
-1@@ -1212,9 +1218,14 @@ static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)return ret; } -static int ufshcd_clock_scaling_prepare (struct ufs_hba *hba) +/* +* Wait until all pending SCSI commands and TMFs have finished or the timeout
+ * has expired.
+ *
+ * Return: 0 upon success;-EBUSYupon timeout.
+ */
+static intufshcd_clock_scaling_prepare(struct ufs_hba *hba, u64 timeout_us)
{drivers/scsi/ufs/ufshcd.c +1
-2-#define DOORBELL_CLR_TOUT_US (1000 * 1000)/* 1 sec */ int ret =0 ;/* * make sure that there are no outstanding requests when @@ -1223,7 +1234,7 @@ static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)ufshcd_scsi_block_requests (hba );down_write (&hba->clk_scaling_lock); -if (ufshcd_wait_for_doorbell_clr (hba,DOORBELL_CLR_TOUT_US )) { +if (ufshcd_wait_for_doorbell_clr (hba, timeout_us)) { ret = -EBUSY ;up_write (&hba->clk_scaling_lock);ufshcd_scsi_unblock_requests(hba);drivers/scsi/ufs/ufshcd.c +1
-1@@ -1264,7 +1275,7 @@ static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)if (!hba->clk_scaling.is_allowed)return -EBUSY ; - ret =ufshcd_clock_scaling_prepare (hba); + ret =ufshcd_clock_scaling_prepare (hba,1 *USEC_PER_SEC );if (ret)return ret;
PATCH 4/4 scsi: ufs: Add suspend/resume SCSI command processing support
In reply to Bart Van AsscheThis functionality is needed by UFS drivers to e.g. suspend SCSI command processing while reprogramming encryption keys if the hardware does not support concurrent I/O and key reprogramming.
--- drivers/scsi/ufs/ufshcd.c | 20 ++++++++++++++++++++ drivers/scsi/ufs/ufshcd.h | 3 +++ 2 files changed, 23 insertions(+)
drivers/scsi/ufs/ufshcd.c +20 -0
@@ -1692,6 +1692,26 @@ static void ufshcd_ungate_work(struct work_struct *work)ufshcd_scsi_unblock_requests (hba); } +/* +* Block processing of new SCSI commands and wait until pending SCSI +* commands and TMFs have finished. ufshcd_exec_dev_cmd() and +* ufshcd_issue_devman_upiu_cmd() are not affected by this function. +* +* Return: 0 upon success; -EBUSY upon timeout. +*/ +int ufshcd_freeze_scsi_devs (struct ufs_hba *hba,u64 timeout_us) +{ +return ufshcd_clock_scaling_prepare (hba, timeout_us); +} +EXPORT_SYMBOL_GPL (ufshcd_freeze_scsi_devs); + +/* Resume processing of SCSI commands. */ +void ufshcd_unfreeze_scsi_devs (struct ufs_hba *hba) +{ +ufshcd_clock_scaling_unprepare (hba, true); +} +EXPORT_SYMBOL_GPL (ufshcd_unfreeze_scsi_devs); +/** * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. * Also, exit from hibern8 mode and set the link as active.
drivers/scsi/ufs/ufshcd.h +3 -0
@@ -1181,6 +1181,9 @@ void ufshcd_release(struct ufs_hba *hba);void ufshcd_clkgate_delay_set (struct device *dev,unsigned long value); +int ufshcd_freeze_scsi_devs (struct ufs_hba *hba,u64 timeout_us); +void ufshcd_unfreeze_scsi_devs (struct ufs_hba *hba); +void ufshcd_map_desc_id_to_length (struct ufs_hba *hba,enum desc_idn desc_id,int *desc_length);