hw/ufs: Support Task Management Request (TMR) and MCQ status registers
v2 cover hw/ufs: Support Task Management Request (TMR) and MCQ status registers
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.
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, 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, pending SCSI requests in the block layer were not cancelled when requests were cleared or aborted in ufs_clear_req(), posing Use-After-Free hazards upon asynchronous AIO callbacks.
What:
- Patch 1: Tracks SCSIRequest in UfsRequest and cancels pending requests via scsi_req_cancel() in ufs_clear_req(), ensuring clean Block AIO teardown.
- Patch 2: Implements MCQ runtime operational registers (queue lifecycle control, per-queue interrupt status/enable, global CQES sync) and HCE=0 MMIO read protection.
- Patch 3: Implements Task Management Request handling for UTMRLDBR (supporting UFS_QUERY_TASK and UFS_ABORT_TASK for both legacy UTRL and MCQ queues).
Differences from v1:
- Split into two independent series: this series contains production specification compliance and memory safety fixes; experimental fault injection properties (x-hold-tag, x-hold-mode) have been moved to a separate follow-up patch based on this series.
- Separated scsi_req_cancel() into Patch 1 to isolate the block layer memory safety fix.
- Added HCE=0 operational register read guard (returning 0xffffffff) in Patch 2 per JEDEC UFSHCI specification.
- Cleaned up TMR completion handling and trace events in Patch 3.
Tested:
- Verified on ARM64 Linux guest (linux-next) running on QEMU.
- Verified ufshcd_mcq_make_queues_operational() succeeds without invalid register offset warnings.
- Verified SCSI command aborts and Task Management Requests via Linux SCSI error handling escalation.
- Verified 100% data integrity (cmp) on /dev/sda after abort and reset recovery.
- Passes ./scripts/checkpatch.pl with 0 errors and 0 warnings.
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 | 12 +++ hw/ufs/trace-events | 2 + hw/ufs/ufs.c | 196 +++++++++++++++++++++++++++++++++++++++++++- hw/ufs/ufs.h | 1 + include/block/ufs.h | 9 ++ 5 files changed, 217 insertions(+), 3 deletions(-)
Signature · 1 line
2.55.0.1007.g17ff1f9808-goog
PATCH v2 1/3 hw/ufs: Track SCSIRequest and cancel pending requests in ufs_clear_req
In the UFS emulator, outstanding SCSI requests are dispatched asynchronously to the block layer via scsi_req_enqueue(). When requests are cleared or aborted (e.g. during HCE reset or TMR aborts), ufs_clear_req() releases the scatter-gather list without cancelling the pending SCSIRequest. If an asynchronous AIO callback completes afterwards, ufs_scsi_command_complete() dereferences stale or freed request state, resulting in use-after-free hazards.
Track the active SCSIRequest in UfsRequest and explicitly cancel any in-flight requests in ufs_clear_req() before freeing request resources. To prevent dangling references during cancellation cascades, decouple and clear req->sreq in completion and cancellation handlers.
--- hw/ufs/lu.c | 12 ++++++++++++ hw/ufs/ufs.c | 6 ++++++ hw/ufs/ufs.h | 1 + 3 files changed, 19 insertions(+)
hw/ufs/lu.c +12 -0
@@ -173,6 +173,12 @@ 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; +if (!req) { +return ; + } + + 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 +196,11 @@ 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; + +if (req) { + req->sreq =NULL ; + } scsi_req->hba_private =NULL ;scsi_req_unref (scsi_req); }
@@ -389,6 +400,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.55.0.1007.g17ff1f9808-goog
Re: [PATCH v2 1/3] hw/ufs: Track SCSIRequest and cancel pending requests in ufs_clear_req
Quoted 7 lines · Stanley Jhu
In the UFS emulator, outstanding SCSI requests are dispatched asynchronously to the block layer via
scsi_req_enqueue(). When requests are cleared or aborted (e.g. during HCE reset or TMR aborts),ufs_clear_req()releases the scatter-gather list without cancelling the pending SCSIRequest. If an asynchronous AIO callback completes afterwards,ufs_scsi_command_complete()dereferences stale or freed request state, resulting in use-after-free hazards.
ufs_clear_req() currently releases request resources; it is not responsible for cancelling in-flight SCSI requests. Its existing callers ensure that the request is no longer in flight, so the current code does not have the UAF described here. Please describe this as preparation for TMF support.
hw/ufs/lu.c static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid) · line 178
Quoted 13 lines
hw/ufs/lu.c +3
-0@@ -173,6 +173,12 @@ 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; +if (!req) { +return ; + }
Can we use assert(req != NULL) here? We only clear hba_private in the completion and cancellation callbacks, so req should not be NULL here. If that invariant is broken, it would be better to catch the bug with an assertion than to silently return.
hw/ufs/lu.c static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid) · line 201
Quoted 7 lines
hw/ufs/lu.c +3
-0@@ -190,6 +196,11 @@ 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; + +if (req) {
Same here: can we use assert(req != NULL)?
Signature · 1 line
Jeuk Kim <[email protected]>
Re: [PATCH v2 1/3] hw/ufs: Track SCSIRequest and cancel pending requests in ufs_clear_req
Quoted 4 lines · Jeuk Kim
ufs_clear_req()currently releases request resources; it is not responsible for cancelling in-flight SCSI requests. Its existing callers ensure that the request is no longer in flight, so the current code does not have the UAF described here. Please describe this as preparation for TMF support.
Agreed. Updated the commit message in v3 to frame this as preparation for TMF support.
Quoted 12 lines
hw/ufs/lu.c static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid) · line 178
Quoted 3 lines
hw/ufs/lu.c +3
-0+if (!req) { +return ; + }Can we use assert(req != NULL) here? We only clear
hba_privatein the completion and cancellation callbacks, so req should not be NULL here. If that invariant is broken, it would be better to catch the bug with an assertion than to silently return.Review static void ufs_scsi_command_complete(SCSIRequest
Quoted 1 line
(quoted diff) +0
-0@@ -190,6 +196,11 @@ static void ufs_scsi_command_complete(SCSIRequest
*scsi_req,size_tresid)Quoted 1 line
Quoted 7 lines
hw/ufs/lu.c static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid) · line 201
Quoted 5 lines
static void
ufs_scsi_command_cancelled(SCSIRequest *scsi_req){hw/ufs/lu.c +3
-0+UfsRequest *req = scsi_req->hba_private; + +if (req) {Same here: can we use assert(req != NULL)?
Done in v3.
PATCH v2 2/3 hw/ufs: Support MCQ runtime interrupt and queue status registers
In reply to Stanley JhuAccording to JEDEC UFSHCI 5.2.1 and 5.6, Multi-Circular Queue (MCQ) architecture provides per-queue runtime control and interrupt registers. When Linux initializes MCQ via ufshcd_mcq_make_queues_operational(), it configures operational registers (SQnRTC, SQnCTI, SQnIS/IE, CQnIS/IE, CQnIACR). Currently, QEMU treats these offsets as unhandled, generating "invalid register offset" warnings and failing queue lifecycle transitions.
Implement MCQ runtime operational register handling:
- Support SQ run-time control (SQnRTC) to start, stop, and clean up submission queues, updating run-time status (SQnRTS) and synchronizing with the SQ processing bottom half.
- Support per-queue interrupt status and enable registers (SQnIS/IE, CQnIS/IE). Implement write-1-to-clear semantics and dynamically synchronize the global CQES (CQ Event Status) bit in IS to prevent interrupt storms.
- Store queue configuration for interrupt aggregation (CQnIACR) and completion timeout intervals (SQnCTI).
- Enforce controller reset state machine rules: guard MMIO reads when HCE=0, and reinitialize operational registers across HCE resets while preserving MCQ capability configurations.
--- hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 73 +++++++++++++++++++++++++++++++++++++++++++-- include/block/ufs.h | 9 ++++++ 3 files changed, 81 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 +71 -2
117 lines not on this page · raw message
include/block/ufs.h +9 -0
@@ -224,6 +224,15 @@ 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(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.55.0.1007.g17ff1f9808-goog
Re: [PATCH v2 2/3] hw/ufs: Support MCQ runtime interrupt and queue status registers
Quoted 7 lines · Stanley Jhu
[...]
processing bottom half.
- Support per-queue interrupt status and enable registers (SQnIS/IE, CQnIS/IE). Implement write-1-to-clear semantics and dynamically synchronize the global CQES (CQ Event Status) bit in IS to prevent interrupt storms.
- Store queue configuration for interrupt aggregation (CQnIACR) and completion timeout intervals (SQnCTI).
SQnCTI is Submission Queue Cleanup Task Information, not a completion timeout interval.
Quoted 7 lines
hw/ufs/ufs.c +0
-0
hw/ufs/ufs.c static void ufs_write_mcq_op_reg(UfsHc *u, hwaddr offset, uint32_t data, · line 1012
Quoted 9 lines
hw/ufs/ufs.c +6
-0@@ -990,6 +1004,38 @@ static void ufs_write_mcq_op_reg(UfsHc *u, hwaddr offset, uint32_t data, } opr->sq.tp = data;break ; + case offsetof(UfsMcqOpReg, sq.rtc): + opr->sq.rtc = data; +if (FIELD_EX32 (data,SQRTC ,ICU )) { +/* SQ_ICU: Initiate Cleanup (SQ_CUS = 1, RTC = 0) */ + opr->sq.rts =FIELD_DP32 (opr->sq.rts,SQRTS ,CUS ,1 ); + opr->sq.rts =FIELD_DP32 (opr->sq.rts,SQRTS ,RTC ,0 );
SQRTC.ICU reports successful cleanup without using SQCTI or changing any request state. Is the actual cleanup expected to be handled by the TMR path?
Signature · 1 line
Jeuk Kim <[email protected]>
Re: [PATCH v2 2/3] hw/ufs: Support MCQ runtime interrupt and queue status registers
Quoted 6 lines · Jeuk Kim
Quoted 1 line
- Store queue configuration for interrupt aggregation (CQnIACR) and
completion
Quoted 1 line
timeout intervals (SQnCTI).
SQnCTI is Submission Queue Cleanup Task Information, not a completion timeout interval.
Fixed in v3.
Quoted 9 lines
hw/ufs/ufs.c static void ufs_write_mcq_op_reg(UfsHc *u, hwaddr offset, uint32_t data, · line 1012
Quoted 6 lines
hw/ufs/ufs.c +6
-0+ case offsetof(UfsMcqOpReg, sq.rtc): + opr->sq.rtc = data; +if (FIELD_EX32 (data,SQRTC ,ICU )) { +/* SQ_ICU: Initiate Cleanup (SQ_CUS = 1, RTC = 0) */ + opr->sq.rts =FIELD_DP32 (opr->sq.rts,SQRTS ,CUS ,1 ); + opr->sq.rts =FIELD_DP32 (opr->sq.rts,SQRTS ,RTC ,0 );SQRTC.ICU reports successful cleanup without using SQCTI or changing any request state. Is the actual cleanup expected to be handled by the TMR path?
Implemented actual cleanup in v3: SQRTC.ICU now matches the target request via SQCTI, cancels any pending SCSIRequest, posts a CQE with OCS = UFS_OCS_ABORTED, and recycles the request slot.
PATCH v2 3/3 hw/ufs: Implement Task Management Request (TMR) handling
In reply to Stanley JhuAccording to JEDEC UFSHCI 5.2.1 (Section 7.3 "UTP Task Management"), the host controller processes Task Management Requests via the UTP Task Management Request List (UTMRL). When software triggers UTMRLDBR, the controller processes descriptors, executes the requested task management function (such as UFS_ABORT_TASK or UFS_QUERY_TASK), returns the response UPIU, and signals completion via the UTMRCS interrupt.
Currently, QEMU does not implement the UTMRL doorbell or operational registers. Consequently, when Linux SCSI error recovery initiates task aborts via ufshcd_abort() or ufshcd_mcq_abort(), the requests time out and unnecessarily escalate to full host controller resets.
Implement Task Management Request processing:
- Support UTMRL registers (UTMRLDBR, UTMRLCLR, UTMRLRSR) and latch doorbell events per Section 5.3.3.
- Handle UFS_ABORT_TASK and UFS_QUERY_TASK functions across both legacy UTRL and MCQ queues, locating outstanding requests by task tag.
- Return compliant response UPIUs with appropriate status codes (setting both response header and output parameters for Linux driver compatibility), and update descriptor OCS while preserving vendor/reserved fields.
- Reset UTMRL operational registers on HCE reset.
--- hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 117 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 117 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 +116 -1
146 lines not on this page · raw message
Signature · 1 line
2.55.0.1007.g17ff1f9808-goog
Re: [PATCH v2 3/3] hw/ufs: Implement Task Management Request (TMR) handling
hw/ufs/ufs.c static void ufs_hce_reset(UfsHc *u) · line 890
Quoted 12 lines · Stanley Jhu
hw/ufs/ufs.c +0
-0@@ -818,6 +822,111 @@ static void ufs_hce_reset(UfsHc *u)[ ... skip 63 lines ... ]
hw/ufs/ufs.c +6
-0+ +if (req) { +ufs_clear_req (req); + req->state =UFS_REQUEST_IDLE ; +if (ufs_mcq_req (req)) { +QTAILQ_INSERT_TAIL (&req->sq->req_list, req, entry);
A successful UFS_ABORT_TASK does not complete MCQ host-side cleanup. The host driver will next issue SQRTCy.ICU, which must post an OCS = ABORTED CQE. Please do not clear and recycle the request before that cleanup completes.
hw/ufs/ufs.c static void ufs_write_reg(UfsHc *u, hwaddr offset, uint32_t data, unsigned size) · line 996
Quoted 10 lines
hw/ufs/ufs.c +3
-1@@ -880,10 +989,16 @@ static void ufs_write_reg(UfsHc *u, hwaddr offset, uint32_t data, unsigned size)case A_MCQCONFIG : u->reg.mcqconfig = data;break ; -case A_UTRLCLR :case A_UTMRLDBR : +ufs_process_tmr (u, data); +break ;case A_UTMRLCLR : + u->reg.utmrldbr &= ~data;
UTMRLCLR is clear-on-zero, so this has the polarity backwards: zero clears the corresponding UTMRLDBR bit and one leaves it unchanged. This should be u->reg.utmrldbr &= data.
Signature · 1 line
Jeuk Kim <[email protected]>
Re: [PATCH v2 3/3] hw/ufs: Implement Task Management Request (TMR) handling
Quoted 10 lines · Jeuk Kim
hw/ufs/ufs.c static void ufs_hce_reset(UfsHc *u)
Quoted 5 lines
hw/ufs/ufs.c +5
-0+if (req) { +ufs_clear_req (req); + req->state =UFS_REQUEST_IDLE ; +if (ufs_mcq_req (req)) { + QTAILQ_INSERT_TAIL(&req->sq->req_list, req,entry);
A successful
UFS_ABORT_TASKdoes not complete MCQ host-side cleanup. The host driver will next issue SQRTCy.ICU, which must post an OCS = ABORTED CQE. Please do not clear and recycle the request before that cleanup completes.
Agreed. In v3, UFS_ABORT_TASK in MCQ mode cancels the underlying SCSIRequest and marks the request UFS_REQUEST_ERROR, deferring CQE posting (OCS = UFS_OCS_ABORTED) and slot recycling to SQRTC.ICU.
Quoted 6 lines
Quoted 2 lines
case
A_UTMRLCLR:
+u->reg.utmrldbr&= ~data;UTMRLCLR is clear-on-zero, so this has the polarity backwards: zero clears the corresponding UTMRLDBR bit and one leaves it unchanged. This should be
u->reg.utmrldbr &= data.
Fixed in v3.