* [PATCH 0/2] hw/ufs: Fix mcq completion queue wraparound
@ 2025-12-22 12:35 Ilia Levi
2025-12-22 12:35 ` [PATCH 1/2] " Ilia Levi
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Ilia Levi @ 2025-12-22 12:35 UTC (permalink / raw)
To: qemu-devel; +Cc: ilia.levi, jeuk20.kim, farosas, lvivier, pbonzini
Currently the completion queue processing routine in UFS does not check
whether there is available space. This can cause CQ entries to be discarded
and overwritten. This series include a fix for the issue based on how NVMe
processes completions. It also includes a test to illustrate the issue.
Before applying the fix, the test will fail with:
ERROR:../tests/qtest/ufs-test.c:801:ufstest_mcq_cq_wraparound:
assertion failed (completed == num_requests): (0 == 32)
Ilia Levi (2):
hw/ufs: Fix mcq completion queue wraparound
tests/qtest/ufs-test: Add test for mcq completion queue wraparound
hw/ufs/ufs.c | 20 ++++++-
hw/ufs/ufs.h | 9 +++
tests/qtest/ufs-test.c | 125 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 153 insertions(+), 1 deletion(-)
--
2.49.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] hw/ufs: Fix mcq completion queue wraparound
2025-12-22 12:35 [PATCH 0/2] hw/ufs: Fix mcq completion queue wraparound Ilia Levi
@ 2025-12-22 12:35 ` Ilia Levi
2025-12-22 12:35 ` [PATCH 2/2] tests/qtest/ufs-test: Add test for " Ilia Levi
2026-01-02 0:07 ` [PATCH 0/2] hw/ufs: Fix " Jeuk Kim
2 siblings, 0 replies; 5+ messages in thread
From: Ilia Levi @ 2025-12-22 12:35 UTC (permalink / raw)
To: qemu-devel; +Cc: ilia.levi, jeuk20.kim, farosas, lvivier, pbonzini
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.
Signed-off-by: Ilia Levi <ilia.levi@intel.com>
---
hw/ufs/ufs.c | 20 +++++++++++++++++++-
hw/ufs/ufs.h | 9 +++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/hw/ufs/ufs.c b/hw/ufs/ufs.c
index 0577747f46..671b851de7 100644
--- a/hw/ufs/ufs.c
+++ b/hw/ufs/ufs.c
@@ -446,6 +446,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 =
@@ -468,6 +472,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);
}
@@ -777,10 +787,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;
diff --git a/hw/ufs/ufs.h b/hw/ufs/ufs.h
index 3799d97f30..13d964c5ae 100644
--- a/hw/ufs/ufs.h
+++ b/hw/ufs/ufs.h
@@ -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)
--
2.49.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] tests/qtest/ufs-test: Add test for mcq completion queue wraparound
2025-12-22 12:35 [PATCH 0/2] hw/ufs: Fix mcq completion queue wraparound Ilia Levi
2025-12-22 12:35 ` [PATCH 1/2] " Ilia Levi
@ 2025-12-22 12:35 ` Ilia Levi
2026-01-02 13:38 ` Fabiano Rosas
2026-01-02 0:07 ` [PATCH 0/2] hw/ufs: Fix " Jeuk Kim
2 siblings, 1 reply; 5+ messages in thread
From: Ilia Levi @ 2025-12-22 12:35 UTC (permalink / raw)
To: qemu-devel; +Cc: ilia.levi, jeuk20.kim, farosas, lvivier, pbonzini
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()
Signed-off-by: Ilia Levi <ilia.levi@intel.com>
---
tests/qtest/ufs-test.c | 125 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 125 insertions(+)
diff --git a/tests/qtest/ufs-test.c b/tests/qtest/ufs-test.c
index 4867ccf08a..a5f5243886 100644
--- a/tests/qtest/ufs-test.c
+++ b/tests/qtest/ufs-test.c
@@ -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,80 @@ 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 +491,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 +755,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 +1252,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,
--
2.49.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 0/2] hw/ufs: Fix mcq completion queue wraparound
2025-12-22 12:35 [PATCH 0/2] hw/ufs: Fix mcq completion queue wraparound Ilia Levi
2025-12-22 12:35 ` [PATCH 1/2] " Ilia Levi
2025-12-22 12:35 ` [PATCH 2/2] tests/qtest/ufs-test: Add test for " Ilia Levi
@ 2026-01-02 0:07 ` Jeuk Kim
2 siblings, 0 replies; 5+ messages in thread
From: Jeuk Kim @ 2026-01-02 0:07 UTC (permalink / raw)
To: Ilia Levi, qemu-devel; +Cc: jeuk20.kim, farosas, lvivier, pbonzini
On 12/22/2025 9:35 PM, Ilia Levi wrote:
> Currently the completion queue processing routine in UFS does not check
> whether there is available space. This can cause CQ entries to be discarded
> and overwritten. This series include a fix for the issue based on how NVMe
> processes completions. It also includes a test to illustrate the issue.
>
> Before applying the fix, the test will fail with:
> ERROR:../tests/qtest/ufs-test.c:801:ufstest_mcq_cq_wraparound:
> assertion failed (completed == num_requests): (0 == 32)
>
> Ilia Levi (2):
> hw/ufs: Fix mcq completion queue wraparound
> tests/qtest/ufs-test: Add test for mcq completion queue wraparound
>
> hw/ufs/ufs.c | 20 ++++++-
> hw/ufs/ufs.h | 9 +++
> tests/qtest/ufs-test.c | 125 +++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 153 insertions(+), 1 deletion(-)
Hi Ilia,
Sorry for the late reply. Thanks a lot for the fix and the test.
Reviewed-by: Jeuk Kim <jeuk20.kim@samsung.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] tests/qtest/ufs-test: Add test for mcq completion queue wraparound
2025-12-22 12:35 ` [PATCH 2/2] tests/qtest/ufs-test: Add test for " Ilia Levi
@ 2026-01-02 13:38 ` Fabiano Rosas
0 siblings, 0 replies; 5+ messages in thread
From: Fabiano Rosas @ 2026-01-02 13:38 UTC (permalink / raw)
To: Ilia Levi, qemu-devel; +Cc: ilia.levi, jeuk20.kim, lvivier, pbonzini
Ilia Levi <ilia.levi@intel.com> writes:
> 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()
>
> Signed-off-by: Ilia Levi <ilia.levi@intel.com>
Acked-by: Fabiano Rosas <farosas@suse.de>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-01-02 13:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-22 12:35 [PATCH 0/2] hw/ufs: Fix mcq completion queue wraparound Ilia Levi
2025-12-22 12:35 ` [PATCH 1/2] " Ilia Levi
2025-12-22 12:35 ` [PATCH 2/2] tests/qtest/ufs-test: Add test for " Ilia Levi
2026-01-02 13:38 ` Fabiano Rosas
2026-01-02 0:07 ` [PATCH 0/2] hw/ufs: Fix " Jeuk Kim
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.