hw/ufs: Support MCQ runtime registers and Task Management Requests
v4 cover hw/ufs: Support MCQ runtime registers and Task Management Requests
This series adds support for Task Management Requests (TMR) and Multi-Circular Queue (MCQ) runtime status, interrupt, and queue cleanup operations in the QEMU UFS emulator.
Why:
Linux UFS error recovery issues Task Management Requests (TMR) via the UTP Task Management Request List (UTMRL). In MCQ mode, the driver also uses per-queue operational registers (SQnRTC, SQnRTS, SQnCTI, CQnIS) to stop queues and clean up aborted tasks, and nullifies unfetched SQEs by setting Command Type to 0xF. QEMU does not yet model these paths or track per-request SCSIRequest pointers needed to cancel individual in-flight commands. Per-SQ interrupt status/enable bits (SQIS.SSS/SUS, SQIE, and IS.SQES) remain unmodeled.
What:
- Patch 1: Track SCSIRequest in UfsRequest and cancel it in ufs_clear_req().
- Patch 2: Implement MCQ runtime operational registers and SQnRTC.ICU cleanup.
- Patch 3: Implement UTMRL doorbell and TMR handling (QUERY_TASK and ABORT_TASK).
- Patch 4: Skip nullified MCQ SQEs (CT = 0xF) and post CQEs with UFS_OCS_ABORTED.
Differences from v3:
- Report SQRTS.RTC (0..3) and preserve SQRTS.CUS across SQ_START (Jeuk)
- Separate TMR rsp_header.response from output_param1 and gate IS.UTMRCS on the UTMRD I-bit (Jeuk)
- Drop non-spec MMIO configuration register resets in ufs_hce_reset()
- Complete nullified MCQ SQEs (CT = 0xF) with UFS_OCS_ABORTED
Tested:
Verified on QEMU ARM64 with qos-test and Linux guest MCQ TMR abort, SQ cleanup, and host reset.
Stanley Jhu (4):
hw/ufs: Track SCSIRequest and cancel pending requests in ufs_clear_req hw/ufs: Support MCQ runtime interrupt and queue status registers hw/ufs: Implement Task Management Request (TMR) handling hw/ufs: Skip nullified MCQ submission queue entries with OCS_ABORTED
hw/ufs/lu.c | 8 ++ hw/ufs/trace-events | 2 + hw/ufs/ufs.c | 268 +++++++++++++++++++++++++++++++++++++++++++- hw/ufs/ufs.h | 1 + include/block/ufs.h | 17 +++ 5 files changed, 292 insertions(+), 4 deletions(-)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH v4 1/4 hw/ufs: Track SCSIRequest and cancel pending requests in ufs_clear_req
Existing callers of ufs_clear_req() only invoke it after requests are no longer in flight. In preparation for Task Management Request (TMR) task abort and MCQ queue cleanup support, where individual in-flight requests are aborted without purging the entire SCSI device, track the active SCSIRequest in UfsRequest and cancel any pending request via scsi_req_cancel() in ufs_clear_req().
--- hw/ufs/lu.c | 8 ++++++++ hw/ufs/ufs.c | 6 ++++++ hw/ufs/ufs.h | 1 + 3 files changed, 15 insertions(+)
hw/ufs/lu.c +8 -0
@@ -173,6 +173,9 @@ static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid)int16_t status = scsi_req->status;uint32_t transfered_len = scsi_req->cmd.xfer - resid; +assert (req !=NULL ); + req->sreq =NULL ; +/* WB / HID accounting should only happen for successful commands */ if (status ==GOOD ) {ufs_wb_process_write_req (req, transfered_len);
@@ -190,6 +193,10 @@ static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid)static void ufs_scsi_command_cancelled (SCSIRequest *scsi_req) { +UfsRequest *req = scsi_req->hba_private; + +assert (req !=NULL ); + req->sreq =NULL ; scsi_req->hba_private =NULL ;scsi_req_unref (scsi_req); }
@@ -389,6 +396,7 @@ static UfsReqResult ufs_process_scsi_cmd(UfsLu *lu, UfsRequest *req)SCSIRequest *scsi_req =scsi_req_new (lu->scsi_dev, task_tag, lu->lun, req->req_upiu.sc.cdb,UFS_CDB_SIZE , req); + req->sreq = scsi_req;uint32_t len =scsi_req_enqueue (scsi_req);if (len) {
hw/ufs/ufs.c +6 -0
@@ -2221,6 +2221,12 @@ void ufs_complete_req(UfsRequest *req, UfsReqResult req_result)static void ufs_clear_req (UfsRequest *req) { +if (req->sreq !=NULL ) { +SCSIRequest *sreq = req->sreq; + req->sreq =NULL ; +scsi_req_cancel (sreq); + } +if (req->sg !=NULL ) {qemu_sglist_destroy (req->sg);g_free (req->sg);
hw/ufs/ufs.h +1 -0
@@ -60,6 +60,7 @@ typedef struct UfsRequest {UtpUpiuRsp rsp_upiu;/* for scsi command */ +SCSIRequest *sreq;QEMUSGList *sg;uint32_t data_len;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH v4 2/4 hw/ufs: Support MCQ runtime interrupt and queue status registers
In reply to Stanley JhuImplement Multi-Circular Queue (MCQ) runtime operational registers (SQnRTC, SQnRTS, SQnCTI, SQnIS/IE, CQnIS/IE, CQnIACR) per JEDEC UFSHCI:
- Handle SQRTC.STOP transitions (0 -> 1 stop and 1 -> 0 resume) and SQRTC.ICU cleanup using Submission Queue Cleanup Task Information (SQCTI). Immediately clear SQRTS.CUS and SQRTS.RTC on ICU = 1, post a CQE with OCS = UFS_OCS_ABORTED on an active matching task, and set SQRTS.CUS = 1 with SQRTS.RTC = 0 (success), 1 (task not found), 2 (SQ not stopped), or 3 (SQ disabled).
- Implement write-1-to-clear per-queue interrupt status (SQnIS, CQnIS) and synchronize the global IS.CQES bit.
--- hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 112 +++++++++++++++++++++++++++++++++++++++++++- include/block/ufs.h | 15 ++++++ 3 files changed, 126 insertions(+), 2 deletions(-)
hw/ufs/trace-events +1 -0
@@ -14,6 +14,7 @@ ufs_process_uiccmd(uint32_t uiccmd, uint32_t ucmdarg1, uint32_t ucmdarg2, uint32 ufs_mcq_complete_req(uint8_t qid) "sqid %"PRIu8"" ufs_mcq_create_sq(uint8_t sqid, uint8_t cqid, uint64_t addr, uint16_t size) "mcq create sq sqid %"PRIu8", cqid %"PRIu8", addr 0x%"PRIx64", size %"PRIu16"" ufs_mcq_create_cq(uint8_t cqid, uint64_t addr, uint16_t size) "mcq create cq cqid %"PRIu8", addr 0x%"PRIx64", size %"PRIu16"" +ufs_write_mcq_op_reg(uint8_t qid, uint32_t offset, uint32_t data) "qid %"PRIu8", offset 0x%"PRIx32", data 0x%"PRIx32"" ufs_hce_reset(void) "HCE 1 -> 0 reset: cancelling BHs, resetting MCQ and request lists" # error condition
hw/ufs/ufs.c +110 -2
155 lines not on this page · raw message
include/block/ufs.h +15 -0
@@ -224,6 +224,21 @@ typedef struct QEMU_PACKED UfsMcqSqReg {uint32_t rts; } UfsMcqSqReg; +REG32(SQRTC, offsetof(UfsMcqSqReg, rtc)) + FIELD(SQRTC, STOP, 0, 1) + FIELD(SQRTC, ICU, 1, 1) + +REG32(SQCTI, offsetof(UfsMcqSqReg, cti)) + FIELD(SQCTI, TASK_TAG, 0, 8) + FIELD(SQCTI, LUN, 8, 8) + FIELD(SQCTI, IID, 16, 4) + FIELD(SQCTI, EXT_IID, 20, 4) + +REG32(SQRTS, offsetof(UfsMcqSqReg, rts)) + FIELD(SQRTS, STS, 0, 1) + FIELD(SQRTS, CUS, 1, 1) + FIELD(SQRTS, RTC, 4, 4) + typedef struct QEMU_PACKED UfsMcqCqReg {uint32_t hp;uint32_t tp;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH v4 3/4 hw/ufs: Implement Task Management Request (TMR) handling
In reply to Stanley JhuImplement UTP Task Management Request List (UTMRL) doorbell and TMR processing per JEDEC UFSHCI:
- Support UTMRLDBR, UTMRLCLR (clear-on-zero), and UTMRLRSR registers, and gate IS.UTMRCS interrupt assertion on the UTMRD Interrupt (I) bit.
- Handle UFS_QUERY_TASK and UFS_ABORT_TASK across legacy UTRL and MCQ queues, reporting overall Target Success/Failure in rsp_header.response and the TM Service Response in output_param1. For MCQ, UFS_ABORT_TASK cancels the active SCSIRequest and defers CQE posting (OCS = UFS_OCS_ABORTED) and slot recycling to subsequent SQRTC.ICU cleanup.
- Reset UTMRLRSR on HCE reset.
--- hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 133 +++++++++++++++++++++++++++++++++++++++++++- include/block/ufs.h | 1 + 3 files changed, 134 insertions(+), 1 deletion(-)
hw/ufs/trace-events +1 -0
@@ -15,6 +15,7 @@ ufs_mcq_complete_req(uint8_t qid) "sqid %"PRIu8"" ufs_mcq_create_sq(uint8_t sqid, uint8_t cqid, uint64_t addr, uint16_t size) "mcq create sq sqid %"PRIu8", cqid %"PRIu8", addr 0x%"PRIx64", size %"PRIu16"" ufs_mcq_create_cq(uint8_t cqid, uint64_t addr, uint16_t size) "mcq create cq cqid %"PRIu8", addr 0x%"PRIx64", size %"PRIu16"" ufs_write_mcq_op_reg(uint8_t qid, uint32_t offset, uint32_t data) "qid %"PRIu8", offset 0x%"PRIx32", data 0x%"PRIx32"" +ufs_process_tmr(uint8_t func, uint32_t tag, uint8_t resp) "query_func 0x%"PRIx8", task_tag %"PRIu32", tm_resp 0x%"PRIx8"" ufs_hce_reset(void) "HCE 1 -> 0 reset: cancelling BHs, resetting MCQ and request lists" # error condition
hw/ufs/ufs.c +132 -1
154 lines not on this page · raw message
include/block/ufs.h +1 -0
@@ -141,6 +141,7 @@ REG32(UTMRLBAU, offsetof(UfsReg, utmrlbau)) REG32(UTMRLDBR, offsetof(UfsReg, utmrldbr)) REG32(UTMRLCLR, offsetof(UfsReg, utmrlclr)) REG32(UTMRLRSR, offsetof(UfsReg, utmrlrsr)) + FIELD(UTMRLRSR, UTMRLRSR, 0, 1) REG32(UICCMD, offsetof(UfsReg, uiccmd)) REG32(UCMDARG1, offsetof(UfsReg, ucmdarg1))REG32 (UCMDARG2 , offsetof(UfsReg , ucmdarg2))
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH v4 4/4 hw/ufs: Skip nullified MCQ submission queue entries with OCS_ABORTED
In reply to Stanley JhuPer JEDEC UFSHCI (JESD223G Section 6.1.1 and Section 4.4.6 Step 2B), host software in MCQ mode may stop a Submission Queue and nullify an unfetched SQE by setting its UTRD Command Type (DW0 bits 31:28) to 0xF. When the Submission Queue is resumed, the host controller shall skip execution of the nullified transfer request and post a Completion Queue Entry (CQE) to the Completion Queue with OCS = ABORTED (0x06).
In ufs_mcq_process_sq(), check for UFS_UTP_CMD_TYPE_NULL (0xF), read the Request UPIU header on a best-effort basis via ufs_dma_read_req_upiu() to populate cqe.task_tag and cqe.lun for UFSHCI 4.1+ completion decoding, and unconditionally post a CQE with UFS_OCS_ABORTED without executing the command or writing a Response UPIU for the nullified entry.
--- hw/ufs/ufs.c | 17 ++++++++++++++++- include/block/ufs.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-)
hw/ufs/ufs.c +16 -1
@@ -36,6 +36,7 @@static void ufs_exec_req (UfsRequest *req);static void ufs_clear_req (UfsRequest *req); +static void ufs_mcq_process_cq (void *opaque);static inline uint64_t ufs_mcq_reg_addr (UfsHc *u,int qid) {
@@ -474,6 +475,17 @@ static void ufs_mcq_process_sq(void *opaque)ufs_mcq_init_req (sq->u, req, sq);memcpy (&req->utrd, &sqe,sizeof (req->utrd)); +if ((le32_to_cpu (sqe.header.dword_0) >>28 ) ==UFS_UTP_CMD_TYPE_NULL ) { +ufs_dma_read_req_upiu (req); + req->utrd.header.dword_2 =cpu_to_le32 ( + (le32_to_cpu (req->utrd.header.dword_2) & ~UFS_MASK_OCS ) | +UFS_OCS_ABORTED ); + req->state =UFS_REQUEST_COMPLETE ; +QTAILQ_INSERT_TAIL (&sq->cq->req_list, req, entry); +ufs_mcq_process_cq (sq->cq); +continue ; + } + req->state =UFS_REQUEST_RUNNING ;ufs_exec_req (req); }
@@ -493,7 +505,10 @@ static void ufs_mcq_process_cq(void *opaque)break ; } -ufs_dma_write_rsp_upiu (req); +if ((le32_to_cpu (req->utrd.header.dword_0) >>28 ) != +UFS_UTP_CMD_TYPE_NULL ) { +ufs_dma_write_rsp_upiu (req); + }/* UTRD/CQE are LE; round-trip through host to keep BE correct. */ uint64_t ucdba =
include/block/ufs.h +1 -0
@@ -665,6 +665,7 @@ enum {UFS_UTP_CMD_TYPE_SCSI =0x0 ,UFS_UTP_CMD_TYPE_UFS =0x1 ,UFS_UTP_CMD_TYPE_DEV_MANAGE =0x2 , + UFS_UTP_CMD_TYPE_NULL = 0xf, };/* To accommodate UFS2.0 required Command type */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Quoted 48 lines
This series adds support for Task Management Requests (TMR) and Multi-Circular Queue (MCQ) runtime status, interrupt, and queue cleanup operations in the QEMU UFS emulator.
Why:
Linux UFS error recovery issues Task Management Requests (TMR) via the UTP Task Management Request List (UTMRL). In MCQ mode, the driver also uses per-queue operational registers (SQnRTC, SQnRTS, SQnCTI, CQnIS) to stop queues and clean up aborted tasks, and nullifies unfetched SQEs by setting Command Type to0xF. QEMU does not yet model these paths or track per-request SCSIRequest pointers needed to cancel individual in-flight commands. Per-SQ interrupt status/enable bits (SQIS.SSS/SUS, SQIE, and IS.SQES) remain unmodeled.What:
- Patch 1: Track SCSIRequest in UfsRequest and cancel it inufs_clear_req().
- Patch 2: Implement MCQ runtime operational registers and SQnRTC.ICU cleanup.
- Patch 3: Implement UTMRL doorbell and TMR handling (QUERY_TASKandABORT_TASK).
- Patch 4: Skip nullified MCQ SQEs (CT =0xF) and post CQEs withUFS_OCS_ABORTED.Differences from v3:
- Report SQRTS.RTC (0..3) and preserve SQRTS.CUS acrossSQ_START(Jeuk)
- Separate TMR rsp_header.response fromoutput_param1and gate IS.UTMRCS on the UTMRD I-bit (Jeuk)
- Drop non-spec MMIO configuration register resets inufs_hce_reset()
- Complete nullified MCQ SQEs (CT =0xF) withUFS_OCS_ABORTEDTested:
Verified on QEMU ARM64 with qos-test and Linux guest MCQ TMR abort, SQ cleanup, and host reset.Stanley Jhu (4):
hw/ufs: Track SCSIRequest and cancel pending requests inufs_clear_reqhw/ufs: Support MCQ runtime interrupt and queue status registers hw/ufs: Implement Task Management Request (TMR) handling hw/ufs: Skip nullified MCQ submission queue entries withOCS_ABORTEDhw/ufs/lu.c | 8 ++ hw/ufs/trace-events | 2 + hw/ufs/ufs.c | 268 +++++++++++++++++++++++++++++++++++++++++++- hw/ufs/ufs.h | 1 + include/block/ufs.h | 17 +++ 5 files changed, 292 insertions(+), 4 deletions(-)
Quoted 2 lines
--
2.56.0.rc1.315.gc6ed9934b7-goog
Thank you for your contribution.
Signature · 1 line
Jeuk Kim <[email protected]>