hw/ufs: Support MCQ runtime registers and Task Management Requests
v3 cover hw/ufs: Support MCQ runtime registers and Task Management Requests
This patch series implements JEDEC UFSHCI specification compliance for Task Management Requests (TMR) and Multi-Circular Queue (MCQ) runtime status and interrupt registers in the QEMU UFS emulator, rebased onto the latest QEMU master (7074591d7).
Why:
In QEMU UFS emulation, Linux guest error recovery paths (such as SCSI abort during command timeout) issue Task Management Requests (TMR) via the UTP Task Management Request List (UTMRL). Furthermore, when MCQ is enabled, the Linux driver initializes and accesses per-queue runtime registers (SQnRTC, SQnRTS, SQnCTI, SQnIS, CQnIS, etc.). Currently, QEMU treats UTMRLDBR and MCQ operational registers as unsupported, triggering "invalid register offset" warnings and causing SCSI EH aborts to time out and escalate unnecessarily to full controller resets. Additionally, aborting an individual in-flight transfer request via TMR or MCQ queue cleanup requires tracking and cancelling its underlying SCSIRequest without purging the entire SCSI device.
What:
- Patch 1: Tracks SCSIRequest in UfsRequest and cancels pending requests via scsi_req_cancel() in ufs_clear_req() as preparation for Task Management Function (TMF) abort and MCQ queue cleanup support.
- Patch 2: Implements MCQ runtime operational registers (queue lifecycle control, SQnRTC.ICU cleanup via SQnCTI posting UFS_OCS_ABORTED CQE, per-queue interrupt status/enable, and global CQES sync).
- Patch 3: Implements Task Management Request handling for UTMRLDBR (supporting UFS_QUERY_TASK and UFS_ABORT_TASK for both legacy UTRL and MCQ queues, and clear-on-zero UTMRLCLR semantics).
Differences from v2:
- Rebase onto latest QEMU master (7074591d7)
- Frame Patch 1 as TMF preparation and assert(req != NULL) in callbacks
- Drop redundant u->resetting checks and implement SQRTC.ICU cleanup using SQnCTI to post UFS_OCS_ABORTED CQEs
- Defer MCQ request recycling in UFS_ABORT_TASK until SQRTC.ICU and fix UTMRLCLR clear-on-zero polarity
Tested:
Verified on QEMU ARM64 via qos-test and Linux MCQ EH without UAF.
Stanley Jhu (3):
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/lu.c | 8 ++ hw/ufs/trace-events | 2 + hw/ufs/ufs.c | 246 +++++++++++++++++++++++++++++++++++++++++++- hw/ufs/ufs.h | 1 + include/block/ufs.h | 16 +++ 5 files changed, 270 insertions(+), 3 deletions(-)
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
PATCH v3 1/3 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.310.g51773c2048-goog
PATCH v3 2/3 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:
- Support SQ start, stop, and cleanup (SQnRTC.ICU) using Submission Queue Cleanup Task Information (SQnCTI) to abort the target request and post a CQE with OCS = UFS_OCS_ABORTED.
- Implement write-1-to-clear per-queue interrupt status (SQnIS, CQnIS) and synchronize the global IS.CQES bit.
- Reset MCQ runtime operational registers across HCE resets.
--- hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 111 +++++++++++++++++++++++++++++++++++++++++++- include/block/ufs.h | 15 ++++++ 3 files changed, 125 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 +109 -2
161 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.310.g51773c2048-goog
Re: [PATCH v3 2/3] hw/ufs: Support MCQ runtime interrupt and queue status registers
hw/ufs/ufs.c static void ufs_mcq_process_db(UfsHc *u, uint8_t qid, uint32_t db) · line 1018
Quoted 12 lines · Stanley Jhu
hw/ufs/ufs.c +0
-0@@ -970,6 +982,45 @@ static void ufs_mcq_process_db(UfsHc *u, uint8_t qid, uint32_t db)[ ... skip 31 lines ... ]
hw/ufs/ufs.c +6
-0+ (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); + }
RTC = 0 reports successful cleanup, whereas a missing task must be reported with RTC = 1. If no request matches the nexus from SQCTI, this path posts no OCS = ABORTED CQE but still sets CUS = 1 and RTC = 0. Please report RTC = 1 for this path.
Signature · 1 line
Jeuk Kim <[email protected]>
Re: [PATCH v3 2/3] hw/ufs: Support MCQ runtime interrupt and queue status registers
Quoted 4 lines · Jeuk Kim
RTC = 0 reports successful cleanup, whereas a missing task must be reported with RTC = 1. If no request matches the nexus from SQCTI, this path posts no OCS = ABORTED CQE but still sets CUS = 1 and RTC = 0. Please report RTC = 1 for this path.
Fixed in v4.
PATCH v3 3/3 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.
- Handle UFS_QUERY_TASK and UFS_ABORT_TASK across legacy UTRL and MCQ queues. 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 UTMRL operational registers on HCE reset.
--- hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 129 +++++++++++++++++++++++++++++++++++++++++++- include/block/ufs.h | 1 + 3 files changed, 130 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 +128 -1
158 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.310.g51773c2048-goog
Re: [PATCH v3 3/3] hw/ufs: Implement Task Management Request (TMR) handling
hw/ufs/ufs.c static void ufs_hce_reset(UfsHc *u) · line 920
Quoted 12 lines · Stanley Jhu
hw/ufs/ufs.c +0
-0@@ -819,6 +823,119 @@ static void ufs_hce_reset(UfsHc *u)[ ... skip 92 lines ... ]
hw/ufs/ufs.c +6
-0+UFS_UPIU_TRANSACTION_TASK_RSP ; + desc.upiu_rsp.rsp_header.flags =0 ; + desc.upiu_rsp.rsp_header.lun = desc.upiu_req.req_header.lun; + desc.upiu_rsp.rsp_header.task_tag = + desc.upiu_req.req_header.task_tag; + desc.upiu_rsp.rsp_header.response = tm_resp;
tm_resp is a task-management service response for output_param1. Using it here puts reserved values such as 0x08 in the basic-header Response field.
hw/ufs/ufs.c static void ufs_hce_reset(UfsHc *u) · line 933
Quoted 13 lines
hw/ufs/ufs.c +13
-0+ desc.upiu_rsp.output_param1 =cpu_to_be32 (tm_resp); + +if (ufs_addr_write (u, desc_addr, &desc,sizeof (desc))) { +continue ; + } + +trace_ufs_process_tmr (tm_func, task_tag, tm_resp); + u->reg.utmrldbr &= ~(1 << i); + completed_mask |= (1 << i); + } + } + +if (completed_mask) {
UTMRCS should only be set when a completed UTMRD has its I bit set. As written, an I=0 polling request can still raise completion status and an IRQ.
Signature · 1 line
Jeuk Kim <[email protected]>
Re: [PATCH v3 3/3] hw/ufs: Implement Task Management Request (TMR) handling
Quoted 2 lines · Jeuk Kim
tm_respis a task-management service response foroutput_param1. Using it here puts reserved values such as0x08in the basic-header Response field.
Fixed in v4.
Quoted 2 lines
UTMRCS should only be set when a completed UTMRD has its I bit set. As written, an I=0 polling request can still raise completion status and an IRQ.
Fixed in v4.