[PATCH v4 2/2] ufs: mcq: poll OCS after sq cleanup for device management commands

From: Kiwoong Kim

Date: Mon Nov 02 2026 - 00:54:08 EST


Extend ufshcd_mcq_sq_cleanup() to poll the OCS field in the UTRD of a
device management command after a successful SQ_CUS poll. UFSHCI does not
specify the order between the OCS update and the SQ_CUS update, so OCS may
still be stale when cleanup returns. Without this poll, a late response to
a timed-out query can arrive while a retry is already in flight, leaving
the host with two query contexts and raising IS.UTPES.

The poll is limited to device management commands because
ufshcd_compl_one_cqe() copies the OCS from the CQE into the UTRD only for
them. For SCSI commands, the OCS in the UTRD stays
OCS_INVALID_COMMAND_STATUS, so polling it would always time out.

The poll waits until OCS is no longer OCS_INVALID_COMMAND_STATUS or
MCQ_POLL_US elapses. -ETIMEDOUT is returned only if OCS is still
OCS_INVALID_COMMAND_STATUS after the poll. Any other value, such as
OCS_ABORTED reported by the host controller for the cleaned-up request,
means the cleanup has completed.

Here is the timeline of an actual case for this that happened with kernel 6.18.
1. The host sent a query but the device didn't respond with it for a long time.

[ 7384.156142] [4: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_mcq_sq_cleanup, hwq 0: cleanup return code (RTC) 0
[ 7384.156154] [4: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_query_flag: Sending flag query for idn 14 failed, err = -11

2. So after the query was timed out, the driver did SQ cleanup.
Then the host updated CQ update and raised IS.CQES,
Unfortunately, complete() was called the driver's ISR afterward.

3. The driver started to retried the query. In the meantime, it set OCS to 0xF.
Then the driver didn't wait for completion due to the previous complete() call,
and just checked OCS.

[ 7384.156163] [4: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_query_flag: Sending flag query for idn 14 failed, err = 15

4. The driver retried the query again, but the device didn't return anything,
so the query was also timed-out.

[ 7385.660108] [3: kworker/u40:5:10671] exynos-ufs 38300000.ufs: ufshcd_query_flag: Sending flag query for idn 14 failed, err = -11

5. A few seconds later, the device returned the response of the 1st query,
but since the host was holding two query contexts and couldn't handle it,
it raised IS.UTPES.

Fixes: 8d7290348992 ("scsi: ufs: mcq: Add supporting functions for MCQ abort")
Signed-off-by: Kiwoong Kim <kwmad.kim@xxxxxxxxxxx>
---
drivers/ufs/core/ufs-mcq.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)

diff --git a/drivers/ufs/core/ufs-mcq.c b/drivers/ufs/core/ufs-mcq.c
index 8106d55f4041..7c2aa0e9466a 100644
--- a/drivers/ufs/core/ufs-mcq.c
+++ b/drivers/ufs/core/ufs-mcq.c
@@ -550,6 +550,13 @@ static int ufshcd_mcq_sq_start(struct ufs_hba *hba, struct ufs_hw_queue *hwq)
return err;
}

+static enum utp_ocs ufshcd_mcq_read_ocs(struct ufs_hw_queue *hwq,
+ struct ufshcd_lrb *lrbp)
+{
+ guard(spinlock_irqsave)(&hwq->cq_lock);
+ return READ_ONCE(lrbp->utr_descriptor_ptr->header.ocs);
+}
+
/**
* ufshcd_mcq_sq_cleanup - Clean up submission queue resources
* associated with the pending command.
@@ -609,6 +616,20 @@ int ufshcd_mcq_sq_cleanup(struct ufs_hba *hba, int task_tag)
__func__, id,
FIELD_GET(SQ_ICU_ERR_CODE_MASK, readl(reg)));

+ /*
+ * For device management commands, there is no order guaranteed between
+ * OCS update and CUS update, which is why we need to wait here. SCSI
+ * commands are excluded because the OCS in their UTRD is not updated
+ * from the CQE.
+ */
+ if (!err && blk_mq_is_reserved_rq(rq)) {
+ enum utp_ocs ocs;
+
+ err = read_poll_timeout(ufshcd_mcq_read_ocs, ocs,
+ ocs != OCS_INVALID_COMMAND_STATUS, 20,
+ MCQ_POLL_US, false, hwq, lrbp);
+ }
+
if (ufshcd_mcq_sq_start(hba, hwq))
err = -ETIMEDOUT;

--
2.43.0