block/nvme: Improve debugging experience and minor fixes
cover block/nvme: Improve debugging experience and minor fixes
Another set of boring patches, in preparation of
supporting multiple queues (the next series).
Based-on: <[email protected]> "util/vfio-helpers: Improve debugging experience"
https://www.mail-archive.com/[email protected]/msg75637.html
Philippe Mathieu-Daudé (15):
block/nvme: Move nvme_poll_cb() earlier
block/nvme: Trace nvme_poll_queue() per queue
block/nvme: Use unsigned integer for queue counter/size block/nvme: Improve nvme_free_req_queue_wait() trace information block/nvme: Trace queue pair creation/deletion
block/nvme: Make nvme_identify() return boolean indicating error block/nvme: Make nvme_init_queue() return boolean indicating error block/nvme: Pass AioContext argument to nvme_add_io_queue() block/nvme: Introduce Completion Queue definitions block/nvme: Use definitions instead of magic values in add_io_queue() block/nvme: Trace controller capabilities
block/nvme: Simplify device reset
block/nvme: Simplify ADMIN queue access
block/nvme: Report warning with warn_report()
block/nvme: Set request_alignment at initialization
include/block/nvme.h | 17 ++++--- block/nvme.c | 116 +++++++++++++++++++++++++------------------ block/trace-events | 17 ++++--- 3 files changed, 88 insertions(+), 62 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/15 block/nvme: Move nvme_poll_cb() earlier
We are going to use this callback in nvme_add_io_queue() in few commits. To avoid forward-declaring it, move it before. No logical change.
--- block/nvme.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
block/nvme.c +10 -10
@@ -627,6 +627,16 @@ static void nvme_handle_event(EventNotifier *n)nvme_poll_queues (s); } +static bool nvme_poll_cb (void *opaque) +{ +EventNotifier *e = opaque; +BDRVNVMeState *s =container_of (e, BDRVNVMeState, + irq_notifier[MSIX_SHARED_IRQ_IDX ]); + +trace_nvme_poll_cb (s); +return nvme_poll_queues (s); +} +static bool nvme_add_io_queue (BlockDriverState *bs,Error **errp) {BDRVNVMeState *s = bs->opaque;
@@ -669,16 +679,6 @@ out_error:return false; } -static bool nvme_poll_cb (void *opaque) -{ -EventNotifier *e = opaque; -BDRVNVMeState *s =container_of (e, BDRVNVMeState, - irq_notifier[MSIX_SHARED_IRQ_IDX ]); - -trace_nvme_poll_cb (s); -return nvme_poll_queues (s); -} -static int nvme_init (BlockDriverState *bs,const char *device,int namespace,Error **errp) {
Signature · 1 line
2.26.2
PATCH 2/15 block/nvme: Trace nvme_poll_queue() per queue
In reply to Philippe Mathieu-DaudéAs we want to enable multiple queues, report the event in each nvme_poll_queue() call, rather than once in the callback calling nvme_poll_queues().
--- block/nvme.c | 2 +- block/trace-events | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
block/nvme.c +1 -1
@@ -585,6 +585,7 @@ static bool nvme_poll_queue(NVMeQueuePair *q)const size_t cqe_offset = q->cq.head *NVME_CQ_ENTRY_BYTES ;NvmeCqe *cqe = (NvmeCqe *)&q->cq.queue[cqe_offset]; +trace_nvme_poll_queue (q->s, q->index);/* * Do an early check for completions. q->lock isn't needed because * nvme_process_completion() only runs in the event loop thread and
@@ -633,7 +634,6 @@ static bool nvme_poll_cb(void *opaque)BDRVNVMeState *s =container_of (e, BDRVNVMeState, irq_notifier[MSIX_SHARED_IRQ_IDX ]); -trace_nvme_poll_cb (s);return nvme_poll_queues (s); }
block/trace-events +1 -1
@@ -143,7 +143,7 @@ nvme_complete_command(void *s, int index, int cid) "s %p queue %d cid %d" nvme_submit_command(void *s, int index, int cid) "s %p queue %d cid %d" nvme_submit_command_raw(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7) "%02x %02x %02x %02x %02x %02x %02x %02x" nvme_handle_event(void *s) "s %p" -nvme_poll_cb(void *s) "s %p" +nvme_poll_queue(void *s, unsigned q_index) "s %p q #%u" nvme_prw_aligned(void *s, int is_write, uint64_t offset, uint64_t bytes, int flags, int niov) "s %p is_write %d offset %"PRId64" bytes %"PRId64" flags %d niov %d" nvme_write_zeroes(void *s, uint64_t offset, uint64_t bytes, int flags) "s %p offset %"PRId64" bytes %"PRId64" flags %d" nvme_qiov_unaligned(const void *qiov, int n, void *base, size_t size, int align) "qiov %p n %d base %p size 0x%zx align 0x%x"
Signature · 1 line
2.26.2
PATCH 3/15 block/nvme: Use unsigned integer for queue counter/size
In reply to Philippe Mathieu-DaudéWe can not have negative queue count/size/index, use unsigned type.
--- block/nvme.c | 24 +++++++++++------------- block/trace-events | 10 +++++----- 2 files changed, 16 insertions(+), 18 deletions(-)
block/nvme.c +11 -13
@@ -103,7 +103,7 @@ struct BDRVNVMeState {* [1..]: io queues. */ NVMeQueuePair **queues; -int nr_queues; +unsigned nr_queues;size_t page_size;/* How many uint32_t elements does each doorbell entry take. */ size_t doorbell_scale;
@@ -154,7 +154,7 @@ static QemuOptsList runtime_opts = { };static void nvme_init_queue (BDRVNVMeState *s,NVMeQueue *q, -int nentries,int entry_bytes,Error **errp) +unsigned nentries,size_t entry_bytes,Error **errp) {size_t bytes;int r;
@@ -198,7 +198,7 @@ static void nvme_free_req_queue_cb(void *opaque)static NVMeQueuePair *nvme_create_queue_pair (BDRVNVMeState *s,AioContext *aio_context, -int idx,int size, +unsigned idx,size_t size,Error **errp) {int i, r;
@@ -640,10 +640,10 @@ static bool nvme_poll_cb(void *opaque)static bool nvme_add_io_queue (BlockDriverState *bs,Error **errp) {BDRVNVMeState *s = bs->opaque; -int n = s->nr_queues; +unsigned n = s->nr_queues;NVMeQueuePair *q;NvmeCmd cmd; -int queue_size =NVME_QUEUE_SIZE ; +unsigned queue_size =NVME_QUEUE_SIZE ; q =nvme_create_queue_pair (s,bdrv_get_aio_context (bs), n, queue_size, errp);
@@ -657,7 +657,7 @@ static bool nvme_add_io_queue(BlockDriverState *bs, Error **errp) .cdw11 = cpu_to_le32(0x3), };if (nvme_cmd_sync (bs, s->queues[INDEX_ADMIN ], &cmd)) { -error_setg (errp,"Failed to create CQ io queue [%d]" , n); +error_setg (errp,"Failed to create CQ io queue [%u]" , n); goto out_error; } cmd = (NvmeCmd ) {
@@ -667,7 +667,7 @@ static bool nvme_add_io_queue(BlockDriverState *bs, Error **errp) .cdw11 = cpu_to_le32(0x1 | (n << 16)), };if (nvme_cmd_sync (bs, s->queues[INDEX_ADMIN ], &cmd)) { -error_setg (errp,"Failed to create SQ io queue [%d]" , n); +error_setg (errp,"Failed to create SQ io queue [%u]" , n); goto out_error; } s->queues = g_renew(NVMeQueuePair *, s->queues, n + 1);
@@ -869,10 +869,9 @@ static int nvme_enable_disable_write_cache(BlockDriverState *bs, bool enable,static void nvme_close (BlockDriverState *bs) { -int i;BDRVNVMeState *s = bs->opaque; -for (i =0 ; i < s->nr_queues; ++i) { +for (unsigned i =0 ; i < s->nr_queues; ++i) {nvme_free_queue_pair (s->queues[i]); }g_free (s->queues);
@@ -1380,7 +1379,7 @@ static void nvme_detach_aio_context(BlockDriverState *bs) {BDRVNVMeState *s = bs->opaque; -for (int i =0 ; i < s->nr_queues; i++) { +for (unsigned i =0 ; i < s->nr_queues; i++) {NVMeQueuePair *q = s->queues[i];qemu_bh_delete (q->completion_bh);
@@ -1401,7 +1400,7 @@ static void nvme_attach_aio_context(BlockDriverState *bs,aio_set_event_notifier (new_context, &s->irq_notifier[MSIX_SHARED_IRQ_IDX ], false, nvme_handle_event, nvme_poll_cb); -for (int i =0 ; i < s->nr_queues; i++) { +for (unsigned i =0 ; i < s->nr_queues; i++) {NVMeQueuePair *q = s->queues[i]; q->completion_bh =
@@ -1418,11 +1417,10 @@ static void nvme_aio_plug(BlockDriverState *bs)static void nvme_aio_unplug (BlockDriverState *bs) { -int i;BDRVNVMeState *s = bs->opaque;assert (s->plugged); s->plugged = false; -for (i =INDEX_IO (0 ); i < s->nr_queues; i++) { +for (unsigned i =INDEX_IO (0 ); i < s->nr_queues; i++) {NVMeQueuePair *q = s->queues[i];qemu_mutex_lock (&q->lock);nvme_kick (q);
block/trace-events +5 -5
@@ -134,13 +134,13 @@ qed_aio_write_postfill(void *s, void *acb, uint64_t start, size_t len, uint64_t qed_aio_write_main(void *s, void *acb, int ret, uint64_t offset, size_t len) "s %p acb %p ret %d offset %"PRIu64" len %zu" # nvme.c -nvme_kick(void *s, int queue) "s %p queue %d" +nvme_kick(void *s, unsigned q_index) "s %p q #%u" nvme_dma_flush_queue_wait(void *s) "s %p" nvme_error(int cmd_specific, int sq_head, int sqid, int cid, int status) "cmd_specific %d sq_head %d sqid %d cid %d status 0x%x" -nvme_process_completion(void *s, int index, int inflight) "s %p queue %d inflight %d" -nvme_process_completion_queue_plugged(void *s, int index) "s %p queue %d" -nvme_complete_command(void *s, int index, int cid) "s %p queue %d cid %d" -nvme_submit_command(void *s, int index, int cid) "s %p queue %d cid %d" +nvme_process_completion(void *s, unsigned q_index, int inflight) "s %p q #%u inflight %d" +nvme_process_completion_queue_plugged(void *s, unsigned q_index) "s %p q #%u" +nvme_complete_command(void *s, unsigned q_index, int cid) "s %p q #%u cid %d" +nvme_submit_command(void *s, unsigned q_index, int cid) "s %p q #%u cid %d" nvme_submit_command_raw(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7) "%02x %02x %02x %02x %02x %02x %02x %02x" nvme_handle_event(void *s) "s %p" nvme_poll_queue(void *s, unsigned q_index) "s %p q #%u"
Signature · 1 line
2.26.2
PATCH 4/15 block/nvme: Improve nvme_free_req_queue_wait() trace information
In reply to Philippe Mathieu-DaudéWhat we want to trace is the block driver state and the queue index.
--- block/nvme.c | 2 +- block/trace-events | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
block/nvme.c +1 -1
@@ -286,7 +286,7 @@ static NVMeRequest *nvme_get_free_req(NVMeQueuePair *q)while (q->free_req_head ==-1 ) {if (qemu_in_coroutine ()) { -trace_nvme_free_req_queue_wait (q); +trace_nvme_free_req_queue_wait (q->s, q->index);qemu_co_queue_wait (&q->free_req_queue, &q->lock); }else {qemu_mutex_unlock (&q->lock);
block/trace-events +1 -1
@@ -152,7 +152,7 @@ nvme_rw_done(void *s, int is_write, uint64_t offset, uint64_t bytes, int ret) "s nvme_dsm(void *s, uint64_t offset, uint64_t bytes) "s %p offset %"PRId64" bytes %"PRId64"" nvme_dsm_done(void *s, uint64_t offset, uint64_t bytes, int ret) "s %p offset %"PRId64" bytes %"PRId64" ret %d" nvme_dma_map_flush(void *s) "s %p" -nvme_free_req_queue_wait(void *q) "q %p" +nvme_free_req_queue_wait(void *s, unsigned q_index) "s %p q #%u" nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) "s %p cmd %p req %p qiov %p entries %d" nvme_cmd_map_qiov_pages(void *s, int i, uint64_t page) "s %p page[%d] 0x%"PRIx64 nvme_cmd_map_qiov_iov(void *s, int i, void *page, int pages) "s %p iov[%d] %p pages %d"
Signature · 1 line
2.26.2
PATCH 5/15 block/nvme: Trace queue pair creation/deletion
In reply to Philippe Mathieu-Daudé--- block/nvme.c | 3 +++ block/trace-events | 2 ++ 2 files changed, 5 insertions(+)
block/nvme.c +3 -0
@@ -175,6 +175,7 @@ static void nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,static void nvme_free_queue_pair (NVMeQueuePair *q) { +trace_nvme_free_queue_pair (q->index, q);if (q->completion_bh) {qemu_bh_delete (q->completion_bh); }
@@ -210,6 +211,8 @@ static NVMeQueuePair *nvme_create_queue_pair(BDRVNVMeState *s,if (!q) {return NULL ; } +trace_nvme_create_queue_pair (idx, q, size, aio_context, +event_notifier_get_fd (s->irq_notifier)); q->prp_list_pages =qemu_try_memalign (s->page_size, s->page_size *NVME_NUM_REQS );if (!q->prp_list_pages) {
block/trace-events +2 -0
@@ -153,6 +153,8 @@ nvme_dsm(void *s, uint64_t offset, uint64_t bytes) "s %p offset %"PRId64" bytes nvme_dsm_done(void *s, uint64_t offset, uint64_t bytes, int ret) "s %p offset %"PRId64" bytes %"PRId64" ret %d" nvme_dma_map_flush(void *s) "s %p" nvme_free_req_queue_wait(void *s, unsigned q_index) "s %p q #%u" +nvme_create_queue_pair(unsigned q_index, void *q, unsigned size, void *aio_context, int fd) "index %u q %p size %u aioctx %p fd %d" +nvme_free_queue_pair(unsigned q_index, void *q) "index %u q %p" nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) "s %p cmd %p req %p qiov %p entries %d" nvme_cmd_map_qiov_pages(void *s, int i, uint64_t page) "s %p page[%d] 0x%"PRIx64 nvme_cmd_map_qiov_iov(void *s, int i, void *page, int pages) "s %p iov[%d] %p pages %d"
Signature · 1 line
2.26.2
PATCH 6/15 block/nvme: Make nvme_identify() return boolean indicating error
In reply to Philippe Mathieu-DaudéJust for consistency, following the example documented since commit e3fe3988d7 ("error: Document Error API usage rules"), return a boolean value indicating an error is set or not.
--- block/nvme.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
block/nvme.c +6 -1
@@ -496,9 +496,11 @@ static int nvme_cmd_sync(BlockDriverState *bs, NVMeQueuePair *q,return ret; } -static void nvme_identify (BlockDriverState *bs,int namespace,Error **errp) +/* Returns true on success, false on failure. */ +static bool nvme_identify (BlockDriverState *bs,int namespace,Error **errp) {BDRVNVMeState *s = bs->opaque; +bool ret = false;union {NvmeIdCtrl ctrl;NvmeIdNs ns;
@@ -575,10 +577,13 @@ static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp) goto out; } + ret = true; s->blkshift = lbaf->ds; out:qemu_vfio_dma_unmap (s->vfio, id);qemu_vfree (id); + +return ret; }static bool nvme_poll_queue (NVMeQueuePair *q)
Signature · 1 line
2.26.2
PATCH 7/15 block/nvme: Make nvme_init_queue() return boolean indicating error
In reply to Philippe Mathieu-DaudéJust for consistency, following the example documented since commit e3fe3988d7 ("error: Document Error API usage rules"), return a boolean value indicating an error is set or not. This simplifies a bit nvme_create_queue_pair().
--- block/nvme.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
block/nvme.c +6 -9
@@ -153,7 +153,8 @@ static QemuOptsList runtime_opts = { }, }; -static void nvme_init_queue (BDRVNVMeState *s,NVMeQueue *q, +/* Returns true on success, false on failure. */ +static bool nvme_init_queue (BDRVNVMeState *s,NVMeQueue *q,unsigned nentries,size_t entry_bytes,Error **errp) {size_t bytes;
@@ -164,13 +165,14 @@ static void nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q, q->queue =qemu_try_memalign (s->page_size, bytes);if (!q->queue) {error_setg (errp,"Cannot allocate queue" ); -return ; +return false; }memset (q->queue,0 , bytes); r =qemu_vfio_dma_map (s->vfio, q->queue, bytes, false, &q->iova, errp);if (r) {error_prepend (errp,"Cannot map queue: " ); } +return r ==0 ; }static void nvme_free_queue_pair (NVMeQueuePair *q)
@@ -203,7 +205,6 @@ static NVMeQueuePair *nvme_create_queue_pair(BDRVNVMeState *s, Error **errp) {int i, r; -Error *local_err =NULL ;NVMeQueuePair *q;uint64_t prp_list_iova;
@@ -240,16 +241,12 @@ static NVMeQueuePair *nvme_create_queue_pair(BDRVNVMeState *s, req->prp_list_iova = prp_list_iova + i * s->page_size; } -nvme_init_queue (s, &q->sq, size,NVME_SQ_ENTRY_BYTES , &local_err); -if (local_err) { -error_propagate (errp, local_err); +if (!nvme_init_queue (s, &q->sq, size,NVME_SQ_ENTRY_BYTES , errp)) { goto fail; } q->sq.doorbell = &s->doorbells[idx * s->doorbell_scale].sq_tail; -nvme_init_queue (s, &q->cq, size,NVME_CQ_ENTRY_BYTES , &local_err); -if (local_err) { -error_propagate (errp, local_err); +if (!nvme_init_queue (s, &q->cq, size,NVME_CQ_ENTRY_BYTES , errp)) { goto fail; } q->cq.doorbell = &s->doorbells[idx * s->doorbell_scale].cq_head;
Signature · 1 line
2.26.2
PATCH 8/15 block/nvme: Pass AioContext argument to nvme_add_io_queue()
In reply to Philippe Mathieu-DaudéWe want to get ride of the BlockDriverState pointer at some point, so pass aio_context along.
--- block/nvme.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
block/nvme.c +5 -4
@@ -642,7 +642,9 @@ static bool nvme_poll_cb(void *opaque)return nvme_poll_queues (s); } -static bool nvme_add_io_queue (BlockDriverState *bs,Error **errp) +/* Returns true on success, false on failure. */ +static bool nvme_add_io_queue (BlockDriverState *bs, +AioContext *aio_context,Error **errp) {BDRVNVMeState *s = bs->opaque;unsigned n = s->nr_queues;
@@ -650,8 +652,7 @@ static bool nvme_add_io_queue(BlockDriverState *bs, Error **errp)NvmeCmd cmd;unsigned queue_size =NVME_QUEUE_SIZE ; - q =nvme_create_queue_pair (s,bdrv_get_aio_context (bs), - n, queue_size, errp); + q =nvme_create_queue_pair (s, aio_context, n, queue_size, errp);if (!q) {return false; }
@@ -805,7 +806,7 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace, }/* Set up command queues. */ -if (!nvme_add_io_queue (bs, errp)) { +if (!nvme_add_io_queue (bs, aio_context, errp)) { ret = -EIO ; } out:
Signature · 1 line
2.26.2
PATCH 9/15 block/nvme: Introduce Completion Queue definitions
In reply to Philippe Mathieu-DaudéRename Submission Queue flags with 'Sq' and introduce Completion Queue flag definitions.
--- include/block/nvme.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
include/block/nvme.h +11 -6
@@ -491,6 +491,11 @@ typedef struct QEMU_PACKED NvmeCreateCq {#define NVME_CQ_FLAGS_PC (cq_flags) (cq_flags & 0x1)#define NVME_CQ_FLAGS_IEN (cq_flags) ((cq_flags >> 1) & 0x1) +enum NvmeFlagsCq { +NVME_CQ_PC =1 , +NVME_CQ_IEN =2 , +}; + typedef struct QEMU_PACKED NvmeCreateSq {uint8_t opcode;uint8_t flags;
@@ -508,12 +513,12 @@ typedef struct QEMU_PACKED NvmeCreateSq {#define NVME_SQ_FLAGS_PC (sq_flags) (sq_flags & 0x1)#define NVME_SQ_FLAGS_QPRIO (sq_flags) ((sq_flags >> 1) & 0x3) -enum NvmeQueueFlags { -NVME_Q_PC =1 , -NVME_Q_PRIO_URGENT =0 , -NVME_Q_PRIO_HIGH =1 , -NVME_Q_PRIO_NORMAL =2 , -NVME_Q_PRIO_LOW =3 , +enum NvmeFlagsSq { +NVME_SQ_PC =1 , +NVME_SQ_PRIO_URGENT =0 , +NVME_SQ_PRIO_HIGH =1 , +NVME_SQ_PRIO_NORMAL =2 , +NVME_SQ_PRIO_LOW =3 , }; typedef struct QEMU_PACKED NvmeIdentify {
Signature · 1 line
2.26.2
PATCH 10/15 block/nvme: Use definitions instead of magic values in add_io_queue()
In reply to Philippe Mathieu-DaudéReplace magic values by definitions, and simplifiy since the number of queues will never reach 64K.
--- block/nvme.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
block/nvme.c +5 -4
@@ -652,6 +652,7 @@ static bool nvme_add_io_queue(BlockDriverState *bs,NvmeCmd cmd;unsigned queue_size =NVME_QUEUE_SIZE ; +assert (n <=UINT16_MAX ); q =nvme_create_queue_pair (s, aio_context, n, queue_size, errp);if (!q) {return false;
@@ -659,8 +660,8 @@ static bool nvme_add_io_queue(BlockDriverState *bs, cmd = (NvmeCmd ) { .opcode =NVME_ADM_CMD_CREATE_CQ , .dptr.prp1 =cpu_to_le64 (q->cq.iova), - .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | (n &0xFFFF )), - .cdw11 =cpu_to_le32 (0x3 ), + .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | n), + .cdw11 =cpu_to_le32 (NVME_CQ_IEN |NVME_CQ_PC ), };if (nvme_cmd_sync (bs, s->queues[INDEX_ADMIN ], &cmd)) {error_setg (errp,"Failed to create CQ io queue [%u]" , n);
@@ -669,8 +670,8 @@ static bool nvme_add_io_queue(BlockDriverState *bs, cmd = (NvmeCmd ) { .opcode =NVME_ADM_CMD_CREATE_SQ , .dptr.prp1 =cpu_to_le64 (q->sq.iova), - .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | (n &0xFFFF )), - .cdw11 =cpu_to_le32 (0x1 | (n <<16 )), + .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | n), + .cdw11 =cpu_to_le32 (NVME_SQ_PC | (n <<16 )), };if (nvme_cmd_sync (bs, s->queues[INDEX_ADMIN ], &cmd)) {error_setg (errp,"Failed to create SQ io queue [%u]" , n);
Signature · 1 line
2.26.2
Re: [PATCH 10/15] block/nvme: Use definitions instead of magic values in add_io_queue()
Quoted 21 lines · Philippe Mathieu-Daudé
Replace magic values by definitions, and simplifiy since the number of queues will never reach 64K.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- block/nvme.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)block/nvme.c +1
-0@@ -652,6 +652,7 @@ static bool nvme_add_io_queue(BlockDriverState *bs,NvmeCmd cmd;unsigned queue_size =NVME_QUEUE_SIZE ; +assert (n <=UINT16_MAX ); q =nvme_create_queue_pair (s, aio_context, n, queue_size, errp);if (!q) {return false;
block/nvme.c static bool nvme_add_io_queue(BlockDriverState *bs, · line 663
Quoted 6 lines
block/nvme.c +1
-2@@ -659,8 +660,8 @@ static bool nvme_add_io_queue(BlockDriverState *bs, cmd = (NvmeCmd ) { .opcode =NVME_ADM_CMD_CREATE_CQ , .dptr.prp1 =cpu_to_le64 (q->cq.iova), - .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | (n &0xFFFF )), - .cdw11 =cpu_to_le32 (0x3 ), + .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | n),
We can use NVME_NUM_REQS here and drop the queue_size variable.
Quoted 16 lines
block/nvme.c +3
-2+ .cdw11 = cpu_to_le32(NVME_CQ_IEN | NVME_CQ_PC), };if (nvme_cmd_sync (bs, s->queues[INDEX_ADMIN ], &cmd)) {error_setg (errp,"Failed to create CQ io queue [%u]" , n);@@ -669,8 +670,8 @@ static bool nvme_add_io_queue(BlockDriverState *bs, cmd = (NvmeCmd ) { .opcode =NVME_ADM_CMD_CREATE_SQ , .dptr.prp1 =cpu_to_le64 (q->sq.iova), - .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | (n &0xFFFF )), - .cdw11 =cpu_to_le32 (0x1 | (n <<16 )), + .cdw10 =cpu_to_le32 (((queue_size -1 ) <<16 ) | n), + .cdw11 =cpu_to_le32 (NVME_SQ_PC | (n <<16 )), };if (nvme_cmd_sync (bs, s->queues[INDEX_ADMIN ], &cmd)) {error_setg (errp,"Failed to create SQ io queue [%u]" , n);
PATCH 11/15 block/nvme: Trace controller capabilities
In reply to Philippe Mathieu-DaudéControllers have different capabilities and report them in the CAP register. We are particularly interested by the page size limits.
--- block/nvme.c | 10 ++++++++++ block/trace-events | 1 + 2 files changed, 11 insertions(+)
block/nvme.c +10 -0
@@ -725,6 +725,16 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace, * Initialization". */ cap =le64_to_cpu (regs->cap); +trace_nvme_controller_capability ("Maximum Queue Entries Supported" , +NVME_CAP_MQES (cap)); +trace_nvme_controller_capability ("Contiguous Queues Required" , +NVME_CAP_CQR (cap)); +trace_nvme_controller_capability ("Subsystem Reset Supported" , +NVME_CAP_NSSRS (cap)); +trace_nvme_controller_capability ("Memory Page Size Minimum" , +NVME_CAP_MPSMIN (cap)); +trace_nvme_controller_capability ("Memory Page Size Maximum" , +NVME_CAP_MPSMAX (cap));if (!NVME_CAP_CSS (cap)) {error_setg (errp,"Device doesn't support NVMe command set" ); ret = -EINVAL ;
block/trace-events +1 -0
@@ -134,6 +134,7 @@ qed_aio_write_postfill(void *s, void *acb, uint64_t start, size_t len, uint64_t qed_aio_write_main(void *s, void *acb, int ret, uint64_t offset, size_t len) "s %p acb %p ret %d offset %"PRIu64" len %zu" # nvme.c +nvme_controller_capability(const char *desc, uint64_t value) "%s: %"PRIu64 nvme_kick(void *s, unsigned q_index) "s %p q #%u" nvme_dma_flush_queue_wait(void *s) "s %p" nvme_error(int cmd_specific, int sq_head, int sqid, int cid, int status) "cmd_specific %d sq_head %d sqid %d cid %d status 0x%x"
Signature · 1 line
2.26.2
PATCH 12/15 block/nvme: Simplify device reset
In reply to Philippe Mathieu-DaudéAvoid multiple endianess conversion by using device endianess.
--- block/nvme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/nvme.c +1 -1
@@ -747,7 +747,7 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace, timeout_ms =MIN (500 *NVME_CAP_TO (cap),30000 );/* Reset device to get a clean state. */ - regs->cc =cpu_to_le32 (le32_to_cpu (regs->cc) &0xFE ); + regs->cc &=const_le32 (0xFE );/* Wait for CSTS.RDY = 0. */ deadline =qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) + timeout_ms *SCALE_MS ;while (NVME_CSTS_RDY (le32_to_cpu (regs->csts))) {
Signature · 1 line
2.26.2
PATCH 13/15 block/nvme: Simplify ADMIN queue access
In reply to Philippe Mathieu-DaudéWe don't need to dereference from BDRVNVMeState each time. Use a NVMeQueuePair pointer to the admin queue and use it. The nvme_init() becomes easier to review, matching the style of nvme_add_io_queue().
--- block/nvme.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
block/nvme.c +6 -4
@@ -690,6 +690,7 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace, Error **errp) {BDRVNVMeState *s = bs->opaque; +NVMeQueuePair *q;AioContext *aio_context =bdrv_get_aio_context (bs);int ret;uint64_t cap;
@@ -769,19 +770,20 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace,/* Set up admin queue. */ s->queues = g_new(NVMeQueuePair *, 1); - s->queues[INDEX_ADMIN ] =nvme_create_queue_pair (s, aio_context,0 , + q =nvme_create_queue_pair (s, aio_context,0 ,NVME_QUEUE_SIZE , errp); -if (!s->queues[INDEX_ADMIN ]) { +if (!q) { ret = -EINVAL ; goto out; } + s->queues[INDEX_ADMIN ] = q; s->nr_queues =1 ;QEMU_BUILD_BUG_ON (NVME_QUEUE_SIZE &0xF000 ); regs->aqa =cpu_to_le32 ((NVME_QUEUE_SIZE <<AQA_ACQS_SHIFT ) | (NVME_QUEUE_SIZE <<AQA_ASQS_SHIFT )); - regs->asq =cpu_to_le64 (s->queues[INDEX_ADMIN ]->sq.iova); - regs->acq =cpu_to_le64 (s->queues[INDEX_ADMIN ]->cq.iova); + regs->asq =cpu_to_le64 (q->sq.iova); + regs->acq =cpu_to_le64 (q->cq.iova);/* After setting up all control registers we can enable device now. */ regs->cc = cpu_to_le32((ctz32(NVME_CQ_ENTRY_BYTES) << CC_IOCQES_SHIFT) |
Signature · 1 line
2.26.2
PATCH 14/15 block/nvme: Report warning with warn_report()
In reply to Philippe Mathieu-DaudéInstead of displaying warning on stderr, use warn_report() which also displays it on the monitor.
--- block/nvme.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
block/nvme.c +2 -2
@@ -390,8 +390,8 @@ static bool nvme_process_completion(NVMeQueuePair *q) } cid =le16_to_cpu (c->cid);if (cid ==0 || cid >NVME_QUEUE_SIZE ) { -fprintf (stderr,"Unexpected CID in completion queue: %" PRIu32"\n" , - cid); +warn_report ("NVMe: Unexpected CID in completion queue: %" PRIu32", " +"queue size: %u" , cid,NVME_QUEUE_SIZE );continue ; }trace_nvme_complete_command (s, q->index, cid);
Signature · 1 line
2.26.2
PATCH 15/15 block/nvme: Set request_alignment at initialization
In reply to Philippe Mathieu-DaudéWhen introducing this driver in commit bdd6a90a9e5 ("block: Add VFIO based NVMe driver") we correctly set the request_alignment in nvme_refresh_limits() but forgot to set it at initialization. Do it now.
--- block/nvme.c | 1 + 1 file changed, 1 insertion(+)
block/nvme.c +1 -0
@@ -745,6 +745,7 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace, s->page_size =MAX (4096 ,1 <<NVME_CAP_MPSMIN (cap)); s->doorbell_scale = (4 <<NVME_CAP_DSTRD (cap)) /sizeof (uint32_t ); bs->bl.opt_mem_alignment = s->page_size; + bs->bl.request_alignment = s->page_size; timeout_ms =MIN (500 *NVME_CAP_TO (cap),30000 );/* Reset device to get a clean state. */
Signature · 1 line
2.26.2