ufs queue
From: Jeuk Kim <[email protected]>
The following changes since commit d03c3e522eb0696dcfc9c2cf643431eaaf51ca0f:
Merge tag 'pull-vfio-20260113' of https://github.com/legoater/qemu into staging (2026-01-14 02:37:13 +1100)
are available in the Git repository at:
https://gitlab.com/jeuk20.kim/qemu.git tags/pull-ufs-20260115
for you to fetch changes up to 0995e513c7c46412ccb5b11a2e71b9c7145997b0:
tests/qtest/ufs-test: Add test for mcq completion queue wraparound (2026-01-15 13:45:04 +0900)
----------------------------------------------------------------
ufs: MCQ fixes and tests
- Mask PRDT data byte count to 18 bits
- Fix MCQ completion queue wraparound
----------------------------------------------------------------
Ilia Levi (2):
hw/ufs: Fix mcq completion queue wraparound tests/qtest/ufs-test: Add test for mcq completion queue wraparound
Jeuk Kim (1):
hw/ufs: Ensure DBC of PRDT uses only lower 18 bits
hw/ufs/ufs.c | 23 ++++++++- hw/ufs/ufs.h | 9 ++++ tests/qtest/ufs-test.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 2 deletions(-)
[PULL 1/3] hw/ufs: Ensure DBC of PRDT uses only lower 18 bits
The UFS spec defines the PRDT data byte count as an 18-bit field. This commit masks the value to the lower 18 bits to prevent incorrect transfer lengths and ensure compliance.
--- hw/ufs/ufs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/ufs/ufs.c +2 -1
@@ -224,7 +224,8 @@ static MemTxResult ufs_dma_read_prdt(UfsRequest *req)for (uint16_t i =0 ; i < prdt_len; ++i) {hwaddr data_dma_addr =le64_to_cpu (prd_entries[i].addr); -uint32_t data_byte_count =le32_to_cpu (prd_entries[i].size) +1 ; +uint32_t data_byte_count = +le32_to_cpu (prd_entries[i].size &0x3ffff ) +1 ;qemu_sglist_add (req->sg, data_dma_addr, data_byte_count); req->data_len += data_byte_count; }
Signature · 1 line
2.43.0
[PULL 2/3] hw/ufs: Fix mcq completion queue wraparound
In reply to Jeuk KimFrom: Ilia Levi <[email protected]>
Currently, ufs_mcq_process_cq() writes to the CQ without checking whether there is available space. This can cause CQ entries to be discarded and overwritten. The solution is to stop writing when CQ is full and exert backpressure on the affected SQs. This is similar to how NVMe CQs operate.
--- hw/ufs/ufs.c | 20 +++++++++++++++++++- hw/ufs/ufs.h | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-)
hw/ufs/ufs.c +19 -1
@@ -447,6 +447,10 @@ static void ufs_mcq_process_cq(void *opaque) QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) { +if (ufs_mcq_cq_full (u, cq->cqid)) { +break ; + } +ufs_dma_write_rsp_upiu (req); req->cqe.utp_addr =
@@ -469,6 +473,12 @@ static void ufs_mcq_process_cq(void *opaque) tail = (tail +sizeof (req->cqe)) % (cq->size *sizeof (req->cqe));ufs_mcq_update_cq_tail (u, cq->cqid, tail); +if (QTAILQ_EMPTY (&req->sq->req_list) && + !ufs_mcq_sq_empty (u, req->sq->sqid)) { +/* Dequeueing from SQ was blocked due to lack of free requests */ +qemu_bh_schedule (req->sq->bh); + } +ufs_clear_req (req);QTAILQ_INSERT_TAIL (&req->sq->req_list, req, entry); }
@@ -778,10 +788,18 @@ static void ufs_write_mcq_op_reg(UfsHc *u, hwaddr offset, uint32_t data, } opr->sq.tp = data;break ; - case offsetof(UfsMcqOpReg, cq.hp): + case offsetof(UfsMcqOpReg, cq.hp): { +UfsCq *cq = u->cq[qid]; + +if (ufs_mcq_cq_full (u, qid) && !QTAILQ_EMPTY (&cq->req_list)) { +/* Enqueueing to CQ was blocked because it was full */ +qemu_bh_schedule (cq->bh); + } + opr->cq.hp = data;ufs_mcq_update_cq_head (u, qid, data);break ; + } case offsetof(UfsMcqOpReg, cq_int.is): opr->cq_int.is &= ~data;break ;
hw/ufs/ufs.h +9 -0
@@ -200,6 +200,15 @@ static inline bool ufs_mcq_cq_empty(UfsHc *u, uint32_t qid)return ufs_mcq_cq_tail (u, qid) ==ufs_mcq_cq_head (u, qid); } +static inline bool ufs_mcq_cq_full (UfsHc *u,uint32_t qid) +{ +uint32_t tail =ufs_mcq_cq_tail (u, qid); +uint16_t cq_size = u->cq[qid]->size; + + tail = (tail +sizeof (UfsCqEntry)) % (sizeof (UfsCqEntry) * cq_size); +return tail ==ufs_mcq_cq_head (u, qid); +} +#define TYPE_UFS "ufs"#define UFS (obj) OBJECT_CHECK(UfsHc, (obj), TYPE_UFS)
Signature · 1 line
2.43.0
[PULL 3/3] tests/qtest/ufs-test: Add test for mcq completion queue wraparound
In reply to Jeuk KimFrom: Ilia Levi <[email protected]>
Added a test that sends 32 NOP Out commands asynchronously. Since the CQ has 31 entries by default, this tests the scenario where CQ processing needs to wait for space to become available.
Additionally, added two minor fixes to existing tests:
* advance CQ head after reading from CQ
* initialize command descriptor slots bitmap in ufs_init()
--- tests/qtest/ufs-test.c | 126 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+)
tests/qtest/ufs-test.c +126 -0
@@ -166,6 +166,7 @@ __ufs_send_transfer_request_mcq(QUfs *ufs, uint8_t lun, cqhp =ufs_rreg (ufs, ufs->cqdao[TEST_QID ]); cqentry_addr = ufs->cqlba[TEST_QID ] + cqhp;qtest_memread (ufs->dev.bus->qts, cqentry_addr, &cqentry,sizeof (cqentry)); + cqhp = (cqhp +sizeof (UfsCqEntry)) % (QUEUE_SIZE *sizeof (UfsCqEntry));ufs_wreg (ufs, ufs->cqdao[TEST_QID ], cqhp);return cqentry.status;
@@ -208,6 +209,81 @@ static enum UtpOcsCodes ufs_send_nop_out(QUfs *ufs, UtpUpiuRsp *rsp_out)return ret; } +static bool ufs_mcq_sq_has_space (QUfs *ufs) +{ +uint32_t sqhp =ufs_rreg (ufs, ufs->sqdao[TEST_QID ]); +uint32_t sqtp =ufs_rreg (ufs, ufs->sqdao[TEST_QID ] +0x4 ); +uint32_t next_sqtp = + (sqtp +sizeof (UfsSqEntry)) % (QUEUE_SIZE *sizeof (UfsSqEntry)); +return next_sqtp != sqhp; +} + +static void +__ufs_send_transfer_request_mcq_async (QUfs *ufs,uint8_t lun, +const UtpTransferReqDesc *utrd) +{ +uint32_t sqtp; +uint64_t utrd_addr; + +/* Wait for SQ space */ +while (!ufs_mcq_sq_has_space (ufs)) { +qtest_clock_step (ufs->dev.bus->qts,100 ); + } + + sqtp =ufs_rreg (ufs, ufs->sqdao[TEST_QID ] +0x4 ); + utrd_addr = ufs->sqlba[TEST_QID ] + sqtp; +qtest_memwrite (ufs->dev.bus->qts, utrd_addr, utrd,sizeof (*utrd)); + sqtp = (sqtp +sizeof (UfsSqEntry)) % (QUEUE_SIZE *sizeof (UfsSqEntry)); +ufs_wreg (ufs, ufs->sqdao[TEST_QID ] +0x4 , sqtp); +} + +static int ufs_mcq_send_nop_out_async (QUfs *ufs) +{ +int cmd_desc_slot =alloc_cmd_desc_slot (ufs); +uint64_t req_upiu_addr = + ufs->cmd_desc_addr + cmd_desc_slot *UTP_COMMAND_DESCRIPTOR_SIZE ; + +/* Build up request upiu */ +UtpUpiuReq req_upiu = {0 }; + req_upiu.header.trans_type =UFS_UPIU_TRANSACTION_NOP_OUT ; + req_upiu.header.task_tag = cmd_desc_slot; +qtest_memwrite (ufs->dev.bus->qts, req_upiu_addr, &req_upiu, +sizeof (req_upiu)); + +/* Build up utp transfer request descriptor */ +UtpTransferReqDesc utrd = +ufs_build_req_utrd (req_upiu_addr,UFS_UTP_NO_DATA_TRANSFER ,0 ); + +/* Send Transfer Request */ +__ufs_send_transfer_request_mcq_async (ufs,0 , &utrd); + +return cmd_desc_slot; +} + +static int ufs_mcq_poll_cq (QUfs *ufs,UfsCqEntry *cqe,uint16_t n_cqe) +{ +uint32_t cqhp, cqtp; +uint64_t cqe_addr; +int ix =0 ; + + cqhp =ufs_rreg (ufs, ufs->cqdao[TEST_QID ]); + cqtp =ufs_rreg (ufs, ufs->cqdao[TEST_QID ] +0x4 ); + +while (cqhp != cqtp && ix < n_cqe) { +/* read completion entry */ + cqe_addr = ufs->cqlba[TEST_QID ] + cqhp; +qtest_memread (ufs->dev.bus->qts, cqe_addr, &cqe[ix],sizeof (cqe[ix])); + +/* advance completion queue head pointer */ + cqhp = (cqhp +sizeof (UfsCqEntry)) % (QUEUE_SIZE *sizeof (UfsCqEntry)); + ix++; + } + +ufs_wreg (ufs, ufs->cqdao[TEST_QID ], cqhp); + +return ix; +} + static enum UtpOcsCodes ufs_send_query(QUfs *ufs, uint8_t query_function, uint8_t query_opcode, uint8_t idn, uint8_t index, uint8_t selector,
@@ -416,6 +492,7 @@ static void ufs_init(QUfs *ufs, QGuestAllocator *alloc)ufs_wreg (ufs,A_UTRIACR ,0 );/* Enable transfer request */ +bitmap_zero (ufs->cmd_desc_bitmap,UFS_MAX_CMD_DESC ); ufs->cmd_desc_addr =guest_alloc (alloc,UFS_MAX_CMD_DESC *UTP_COMMAND_DESCRIPTOR_SIZE ); ufs->data_buffer_addr =
@@ -679,6 +756,53 @@ static void ufstest_read_write(void *obj, void *data, QGuestAllocator *alloc)ufs_exit (ufs, alloc); } +static void ufstest_mcq_cq_wraparound (void *obj,void *data, +QGuestAllocator *alloc) +{ +QUfs *ufs = obj; +UfsCqEntry cqe[QUEUE_SIZE ]; +const int num_requests =QUEUE_SIZE ; +int i, completed =0 ; + +ufs_init (ufs, alloc); + +/* Ensure MCQ is supported */ +g_assert_true (ufs->support_mcq); + +for (i =0 ; i < num_requests; ++i) { +ufs_mcq_send_nop_out_async (ufs); + } + +while (completed != num_requests) { +int n_cqe =ufs_mcq_poll_cq (ufs, cqe,ARRAY_SIZE (cqe)); +if (!n_cqe) { +break ; + } + +for (i =0 ; i < n_cqe; ++i) { +uint64_t ucdba; +uint64_t rsp_upiu_addr; +UtpUpiuRsp rsp_upiu; +uint8_t tag; + + g_assert_cmpuint(cqe[i].status, ==, UFS_OCS_SUCCESS); + + ucdba =le64_to_cpu (cqe[i].utp_addr) &MAKE_64BIT_MASK (7 ,57 ); + rsp_upiu_addr = ucdba +UTP_RESPONSE_UPIU_OFFSET ; +qtest_memread (ufs->dev.bus->qts, rsp_upiu_addr, &rsp_upiu, +sizeof (rsp_upiu)); + + tag = rsp_upiu.header.task_tag; +release_cmd_desc_slot (ufs, tag); + } + + completed += n_cqe; + } + + g_assert_cmpint(completed, ==, num_requests); +ufs_exit (ufs, alloc); +} +static void ufstest_query_flag_request (void *obj,void *data,QGuestAllocator *alloc) {
@@ -1129,6 +1253,8 @@ static void ufs_register_nodes(void)qos_add_test ("init" ,"ufs" , ufstest_init,NULL );qos_add_test ("legacy-read-write" ,"ufs" , ufstest_read_write, &io_test_opts);qos_add_test ("mcq-read-write" ,"ufs" , ufstest_read_write, &mcq_test_opts); +qos_add_test ("mcq-cq-wraparound" ,"ufs" , ufstest_mcq_cq_wraparound, + &mcq_test_opts);qos_add_test ("query-flag" ,"ufs" , ufstest_query_flag_request, &io_test_opts); qos_add_test("query-attribute", "ufs", ufstest_query_attr_request,
Signature · 1 line
2.43.0
Re: [PULL 1/3] hw/ufs: Ensure DBC of PRDT uses only lower 18 bits
In reply to Jeuk KimQuoted 14 lines · Jeuk Kim
The UFS spec defines the PRDT data byte count as an 18-bit field. This commit masks the value to the lower 18 bits to prevent incorrect transfer lengths and ensure compliance.
Signed-off-by Jeuk Kim <[email protected]>--- hw/ufs/ufs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/ufs/ufs.c +0
-0
hw/ufs/ufs.c static MemTxResult ufs_dma_read_prdt(UfsRequest *req) · line 231
Quoted 9 lines
hw/ufs/ufs.c +2
-1@@ -224,7 +224,8 @@ static MemTxResult ufs_dma_read_prdt(UfsRequest *req)for (uint16_t i =0 ; i < prdt_len; ++i) {hwaddr data_dma_addr =le64_to_cpu (prd_entries[i].addr); -uint32_t data_byte_count =le32_to_cpu (prd_entries[i].size) +1 ; +uint32_t data_byte_count = +le32_to_cpu (prd_entries[i].size &0x3ffff ) +1 ;qemu_sglist_add (req->sg, data_dma_addr, data_byte_count); req->data_len += data_byte_count; }
This fails on a big-endian host:
https://gitlab.com/qemu-project/qemu/-/jobs/12725280508
You need to move the mask outside of the le32_to_cpu.
r~
Re: [PULL 1/3] hw/ufs: Ensure DBC of PRDT uses only lower 18 bits
Quoted 28 lines · Richard Henderson
Quoted 23 lines · Jeuk Kim
The UFS spec defines the PRDT data byte count as an 18-bit field. This commit masks the value to the lower 18 bits to prevent incorrect transfer lengths and ensure compliance.
Signed-off-by Jeuk Kim <[email protected]>---
hw/ufs/ufs.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)hw/ufs/ufs.c +3
-1@@ -224,7 +224,8 @@ static MemTxResult ufs_dma_read_prdt(UfsRequest *req) for (uint16_t i = 0; i < prdt_len; ++i) { hwaddr data_dma_addr = le64_to_cpu(prd_entries[i].addr); - uint32_t data_byte_count = le32_to_cpu(prd_entries[i].size) +1 ; + uint32_t data_byte_count = + le32_to_cpu(prd_entries[i].size & 0x3ffff) + 1; qemu_sglist_add(req->sg, data_dma_addr, data_byte_count); req->data_len += data_byte_count;Quoted 1 line
}
This fails on a big-endian host:
Quoted 6 lines
https://gitlab.com/qemu-project/qemu/-/jobs/12725280508
You need to move the mask outside of the
le32_to_cpu.r~
Thanks for letting me know.
I’ll fix it and send an updated patch.