hw/ufs: Support MCQ runtime registers, Task Management (TMR), and fault injection
cover hw/ufs: Support MCQ runtime registers, Task Management (TMR), and fault injection
This patch series builds directly on top of the recently reviewed patch:
"[PATCH v3] hw/ufs: Reset controller and MCQ state on HCE transition to 0" (Message-ID: <[email protected]>)
Based-on: <[email protected]>
### Background & Motivation
When running upstream Linux kernel UFS driver error recovery and task aborts (e.g., ufshcd_abort() or ufshcd_mcq_abort()), Linux interacts with UFS Multi- Circular Queue (MCQ) runtime registers and issues UTP Task Management Requests (TMR) via UTMRLDBR. Currently in QEMU:
1. Writing to MCQ operational registers (SQnRTC, SQnCTI, SQnIS, SQnIE, CQnIS, CQnIE, CQnIACR) logs "invalid register offset" warnings.
2. TMR registers (UTMRLDBR, UTMRLCLR, UTMRLRSR) are unsupported. Task aborts time out and unnecessarily escalate to full device or host resets.
3. When task abort cancels an in-flight command, lack of SCSIRequest tracking presents Use-After-Free (UAF) hazards during asynchronous block AIO callbacks.
### Series Overview
- Patch 1 (hw/ufs: Support MCQ runtime interrupt and queue status registers):
Implements UFSHCI 5.2.1/5.6 per-queue runtime registers using QEMU REG32/FIELD macros. Implements SQSTART/SQSTOP queue gating, SQ_ICU queue cleanup reporting, and W1C interrupt handling. Dynamically recalculates active CQ interrupts to clear global IS.CQES, preventing virtual interrupt storms. Cleans operational registers during HCE 1 -> 0 while preserving MCQCONFIG.MAC capability.
- Patch 2 (hw/ufs: Implement Task Management Request (TMR) handling):
Implements Task Management Request processing via UTMRLDBR. Tracks sreq in UfsRequest and safely cancels pending SCSIRequests via scsi_req_cancel() to eliminate UAF hazards. Supports dual-queue tag lookups across legacy UTRL and MCQ queues. For UFS_QUERY_TASK, returns FUNCTION SUCCEEDED (0x08) if the task is pending in the device/controller, and FUNCTION COMPLETE (0x00) if the task does not exist, adhering strictly to SAM-5 and JEDEC specifications. Latches pending doorbell bits in utmrldbr upon UTMRLDBR write per UFSHCI 5.2.1 Section 5.3.3. Preserves dword_2 header OCS masks and returns completion codes in both response and output_param1 to satisfy both JEDEC specifications and Linux kernel driver validation. Clears TMR registers during HCE reset.
- Patch 3 (hw/ufs: Add experimental fault injection properties for task abort testing):
Adds experimental QOM properties x-hold-tag and x-hold-mode to ufs-pci and ufs-sysbus devices. Decouples UFS_HOLD_TAG_NONE (0xffffffff) to preserve Tag 0 testing and UFS_HOLD_TAG_ANY (0xfffffffe) to automatically sniff target direct I/O (READ_10/WRITE_10 with LBA >= 512) while allowing guest boot metadata requests to proceed unimpeded. Supports runtime dynamic modification via QOM (qom-set / qom-get) on realized devices, enabling continuous multi-case CI testing in a single running VM without reboots.
### CI/CD Closed-Loop & Regression Prevention Vision Together with the companion Linux kernel patch:
"[PATCH] scsi: ufs: core: Add fault injection for task abort failures"
these QEMU changes establish a deterministic closed-loop verification testbed for UFS exception cases (abort successes, abort rejections, transitions, and hardware timeouts). This provides a reliable foundation to set up regular automated CI/CD regression suites, ensuring long-term health and zero regressions for future Linux kernel UFS driver development as well as QEMU UFS emulation.
### Testing & Verification
- Style: 0 errors, 0 warnings across all patches via ./scripts/checkpatch.pl.
- Emulation Matrix: Verified in ARM64 QEMU Linux kernel environment across 5 scenarios (Abort Success, Abort Failed, In-Transition, TMR Timeout, and Normal I/O) in a single running VM session without rebooting. All cases recovered cleanly according to spec, and subsequent direct I/O read/write data comparisons passed with 100% binary integrity (cmp PASS on /dev/sda).
Stanley Jhu (3):
hw/ufs: Support MCQ runtime interrupt and queue status registers hw/ufs: Implement Task Management Request (TMR) handling hw/ufs: Add experimental fault injection properties for task abort
testing
hw/ufs/lu.c | 7 + hw/ufs/trace-events | 5 + hw/ufs/ufs-pci.c | 4 + hw/ufs/ufs-sysbus.c | 4 + hw/ufs/ufs.c | 450 +++++++++++++++++++++++++++++++++++++++++++- hw/ufs/ufs.h | 55 ++++++ include/block/ufs.h | 9 + 7 files changed, 531 insertions(+), 3 deletions(-)
Signature · 1 line
2.55.0.979.g7e5102b832-goog
PATCH 1/3 hw/ufs: Support MCQ runtime interrupt and queue status registers
According 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 accesses SQnRTC, SQnCTI, SQnIS, SQnIE, CQnIS, CQnIE, and CQnIACR. Currently, QEMU logs "invalid register offset" warnings for these registers.
Implement handling for these operational registers:
- Define REG32 and FIELD macros for SQRTC (STOP, ICU) and SQRTS (STS, CUS, RTC).
- SQnRTC: Handle SQSTART (bit 0=0) and SQSTOP (bit 0=1) commands to update SQnRTS (Run-Time Status), scheduling or cancelling the SQ bottom half. Enforce SQSTOP in ufs_mcq_process_sq() to halt processing when stopped.
- SQnRTC / SQnRTS: Handle SQ_ICU (bit 1=1) queue cleanup command, reporting SQ_CUS (bit 1=1) and RTC completion code 0 in SQnRTS.
- SQnCTI: Store Completion Timeout Interval configuration.
- SQnIS: Handle Write-1-to-Clear interrupt status and invoke ufs_irq_check().
- CQnIS: Handle Write-1-to-Clear interrupt status. Recalculate whether any CQ has pending interrupts, and clear the global CQES (CQ Event Status) bit in IS before calling ufs_irq_check() to prevent IRQ storms.
- SQnIE / CQnIE: Store Interrupt Enable configuration.
- CQnIACR: Store Interrupt Aggregation Control Register configuration.
- In ufs_hce_reset(), reset operational registers (utriacr, utrlclr, ie, utrlba/utrlbau) while preserving the Max Active Channels (MAC) capability field in MCQCONFIG according to params.mcq.
- Add trace_ufs_write_mcq_op_reg trace event with explicit offset cast.
--- hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 69 +++++++++++++++++++++++++++++++++++++++++++-- include/block/ufs.h | 9 ++++++ 3 files changed, 77 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 +67 -2
107 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.979.g7e5102b832-goog
PATCH 2/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 writes to UTMRLDBR, the controller processes descriptors, dispatches the requested function (e.g., UFS_ABORT_TASK, UFS_QUERY_TASK), returns the completion code in the response UPIU, and triggers the UTMRCS interrupt.
Currently, QEMU treats UTMRLDBR, UTMRLCLR, and UTMRLRSR as unsupported. Consequently, when Linux kernel SCSI error recovery invokes task aborts via ufshcd_abort() or ufshcd_mcq_abort(), requests time out and escalate unnecessarily to full device or host resets.
Implement Task Management Request handling:
- Add sreq tracking in UfsRequest and cancel pending SCSIRequests in ufs_clear_req() to eliminate Use-After-Free hazards during abort.
- Implement ufs_find_req_by_tag() supporting both legacy UTRL and MCQ execution queues.
- Implement ufs_process_tmr() to dispatch UFS_QUERY_TASK and UFS_ABORT_TASK, releasing resources and returning completion response UPIU.
- For UFS_QUERY_TASK, return UFS_UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED if the task is pending in the controller/device, and return UFS_UPIU_TASK_MANAGEMENT_FUNC_COMPL if the task does not exist.
- Set both rsp_header.response and output_param1 to satisfy both JEDEC and Linux kernel driver response validation.
- Mask and preserve descriptor header dword_2 when updating OCS.
- Connect A_UTMRLDBR, A_UTMRLCLR, and A_UTMRLRSR in ufs_write_reg.
- Latch pending doorbell bits in u->reg.utmrldbr upon UTMRLDBR write.
- In ufs_hce_reset(), reset task management operational registers (utmrlrsr, utmrlclr, utmrlba/utmrlbau).
- Add trace_ufs_process_tmr trace event.
--- hw/ufs/lu.c | 7 +++ hw/ufs/trace-events | 1 + hw/ufs/ufs.c | 122 +++++++++++++++++++++++++++++++++++++++++++- hw/ufs/ufs.h | 1 + 4 files changed, 130 insertions(+), 1 deletion(-)
hw/ufs/lu.c +7 -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);
@@ -389,6 +395,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/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 +121 -1
157 lines not on this page · raw message
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.979.g7e5102b832-goog
PATCH 3/3 hw/ufs: Add experimental fault injection properties for task abort testing
In reply to Stanley JhuAdd experimental properties x-hold-tag and x-hold-mode to ufs-pci and ufs-sysbus devices to facilitate end-to-end testing of host driver error handling and task abort recovery routines.
When x-hold-tag is set to a specific tag (0..255), the controller holds (delays completing) the matching transfer request until a Task Management Request (TMR) targeting the held request is issued by the host driver. If x-hold-tag=0xfffffffe (UFS_HOLD_TAG_ANY), the controller automatically intercepts the first READ_10/WRITE_10 request with LBA >= 512, avoiding guest boot-time metadata requests. The default value is 0xffffffff (UFS_HOLD_TAG_NONE), which disables fault injection while preserving full testability of Tag 0.
The x-hold-mode property controls how the held request and subsequent TMR are resolved:
- "abort-success": TMR ABORT_TASK aborts the held request, clears its state, and returns SUCCESS (COMPL).
- "abort-failed": TMR ABORT_TASK returns TASK_MANAGEMENT_FUNC_FAILED.
- "in-transition": TMR QUERY_TASK / ABORT_TASK completes the held request successfully and returns COMPL (simulating a command completing in transition).
- "timeout": TMR ABORT_TASK does not complete, triggering TMR timeout.
Both properties allow runtime modification on realized devices via QOM (e.g., QMP qom-set), enabling continuous, multi-case fault injection testing within a single running VM without requiring reboot cycles.
Together with the companion Linux kernel patch:
"[PATCH] scsi: ufs: core: Add fault injection for task abort failures"
these QEMU changes provide a deterministic testbed to verify UFS exception cases and ensure controller/driver health. This setup is particularly useful for establishing automated CI/CD pipelines to prevent regressions across future Linux kernel UFS driver and QEMU emulation changes.
--- hw/ufs/trace-events | 3 + hw/ufs/ufs-pci.c | 4 + hw/ufs/ufs-sysbus.c | 4 + hw/ufs/ufs.c | 289 +++++++++++++++++++++++++++++++++++++++++--- hw/ufs/ufs.h | 54 +++++++++ 5 files changed, 339 insertions(+), 15 deletions(-)
hw/ufs/trace-events +3 -0
@@ -16,6 +16,9 @@ ufs_mcq_create_sq(uint8_t sqid, uint8_t cqid, uint64_t addr, uint16_t size) "mcq 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_inject_hold_req(uint8_t tag, uint32_t lba) "held command tag %"PRIu8", lba %"PRIu32"" +ufs_set_hold_tag(uint32_t tag) "hold-tag set to 0x%"PRIx32"" +ufs_set_hold_mode(const char *mode) "hold-mode set to %s" ufs_hce_reset(void) "HCE 1 -> 0 reset: cancelling BHs, resetting MCQ and request lists" # error condition
hw/ufs/ufs-pci.c +4 -0
@@ -72,6 +72,10 @@ static const Property ufs_pci_props[] = { 0x400),DEFINE_PROP_UINT32 ("wb-min-size" , UfsPciState, ufs.params.wb_min_size,0x100 ), +DEFINE_PROP_UNSIGNED ("x-hold-tag" , UfsPciState, ufs.params.x_hold_tag, +UFS_HOLD_TAG_NONE , ufs_prop_hold_tag, uint32_t), +DEFINE_PROP ("x-hold-mode" , UfsPciState, ufs.params.x_hold_mode, + ufs_prop_hold_mode, char *), }; static const VMStateDescription ufs_pci_vmstate = {
hw/ufs/ufs-sysbus.c +4 -0
@@ -45,6 +45,10 @@ static const Property ufs_sysbus_props[] = { 0x400),DEFINE_PROP_UINT32 ("wb-min-size" , SysbusUfsState, ufs.params.wb_min_size,0x100 ), +DEFINE_PROP_UNSIGNED ("x-hold-tag" , SysbusUfsState, ufs.params.x_hold_tag, +UFS_HOLD_TAG_NONE , ufs_prop_hold_tag, uint32_t), +DEFINE_PROP ("x-hold-mode" , SysbusUfsState, ufs.params.x_hold_mode, + ufs_prop_hold_mode, char *), }; static const VMStateDescription ufs_sysbus_vmstate = {
hw/ufs/ufs.c +274 -15
333 lines not on this page · raw message
hw/ufs/ufs.h +54 -0
82 lines not on this page · raw message
Signature · 1 line
2.55.0.979.g7e5102b832-goog