2 * NVMe block driver based on vfio
4 * Copyright 2016 - 2018 Red Hat, Inc.
7 * Fam Zheng <famz@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include <linux/vfio.h>
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/module.h"
22 #include "qemu/cutils.h"
23 #include "qemu/option.h"
24 #include "qemu/vfio-helpers.h"
25 #include "block/block_int.h"
26 #include "sysemu/replay.h"
29 #include "block/nvme.h"
31 #define NVME_SQ_ENTRY_BYTES 64
32 #define NVME_CQ_ENTRY_BYTES 16
33 #define NVME_QUEUE_SIZE 128
34 #define NVME_BAR_SIZE 8192
37 * We have to leave one slot empty as that is the full queue case where
40 #define NVME_NUM_REQS (NVME_QUEUE_SIZE - 1)
42 typedef struct BDRVNVMeState BDRVNVMeState
;
48 /* Hardware MMIO register */
49 volatile uint32_t *doorbell
;
53 BlockCompletionFunc
*cb
;
57 uint64_t prp_list_iova
;
58 int free_req_next
; /* q->reqs[] index of next free req */
64 /* Read from I/O code path, initialized under BQL */
68 /* Fields protected by BQL */
69 uint8_t *prp_list_pages
;
71 /* Fields protected by @lock */
72 CoQueue free_req_queue
;
76 NVMeRequest reqs
[NVME_NUM_REQS
];
80 /* Thread-safe, no lock necessary */
81 QEMUBH
*completion_bh
;
84 /* Memory mapped registers */
85 typedef volatile struct {
94 #define INDEX_IO(n) (1 + n)
96 /* This driver shares a single MSIX IRQ for the admin and I/O queues */
98 MSIX_SHARED_IRQ_IDX
= 0,
102 struct BDRVNVMeState
{
103 AioContext
*aio_context
;
106 /* The submission/completion queue pairs.
110 NVMeQueuePair
**queues
;
113 /* How many uint32_t elements does each doorbell entry take. */
114 size_t doorbell_scale
;
115 bool write_cache_supported
;
116 EventNotifier irq_notifier
[MSIX_IRQ_COUNT
];
118 uint64_t nsze
; /* Namespace size reported by identify command */
119 int nsid
; /* The namespace id to read/write data. */
122 uint64_t max_transfer
;
125 bool supports_write_zeroes
;
126 bool supports_discard
;
128 CoMutex dma_map_lock
;
129 CoQueue dma_flush_queue
;
131 /* Total size of mapped qiov, accessed under dma_map_lock */
134 /* PCI address (required for nvme_refresh_filename()) */
138 #define NVME_BLOCK_OPT_DEVICE "device"
139 #define NVME_BLOCK_OPT_NAMESPACE "namespace"
141 static void nvme_process_completion_bh(void *opaque
);
143 static QemuOptsList runtime_opts
= {
145 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
148 .name
= NVME_BLOCK_OPT_DEVICE
,
149 .type
= QEMU_OPT_STRING
,
150 .help
= "NVMe PCI device address",
153 .name
= NVME_BLOCK_OPT_NAMESPACE
,
154 .type
= QEMU_OPT_NUMBER
,
155 .help
= "NVMe namespace",
157 { /* end of list */ }
161 static void nvme_init_queue(BDRVNVMeState
*s
, NVMeQueue
*q
,
162 int nentries
, int entry_bytes
, Error
**errp
)
167 bytes
= ROUND_UP(nentries
* entry_bytes
, s
->page_size
);
168 q
->head
= q
->tail
= 0;
169 q
->queue
= qemu_try_memalign(s
->page_size
, bytes
);
171 error_setg(errp
, "Cannot allocate queue");
174 memset(q
->queue
, 0, bytes
);
175 r
= qemu_vfio_dma_map(s
->vfio
, q
->queue
, bytes
, false, &q
->iova
);
177 error_setg(errp
, "Cannot map queue");
181 static void nvme_free_queue_pair(NVMeQueuePair
*q
)
183 if (q
->completion_bh
) {
184 qemu_bh_delete(q
->completion_bh
);
186 qemu_vfree(q
->prp_list_pages
);
187 qemu_vfree(q
->sq
.queue
);
188 qemu_vfree(q
->cq
.queue
);
189 qemu_mutex_destroy(&q
->lock
);
193 static void nvme_free_req_queue_cb(void *opaque
)
195 NVMeQueuePair
*q
= opaque
;
197 qemu_mutex_lock(&q
->lock
);
198 while (qemu_co_enter_next(&q
->free_req_queue
, &q
->lock
)) {
199 /* Retry all pending requests */
201 qemu_mutex_unlock(&q
->lock
);
204 static NVMeQueuePair
*nvme_create_queue_pair(BDRVNVMeState
*s
,
205 AioContext
*aio_context
,
210 Error
*local_err
= NULL
;
212 uint64_t prp_list_iova
;
214 q
= g_try_new0(NVMeQueuePair
, 1);
218 q
->prp_list_pages
= qemu_try_memalign(s
->page_size
,
219 s
->page_size
* NVME_NUM_REQS
);
220 if (!q
->prp_list_pages
) {
223 memset(q
->prp_list_pages
, 0, s
->page_size
* NVME_NUM_REQS
);
224 qemu_mutex_init(&q
->lock
);
227 qemu_co_queue_init(&q
->free_req_queue
);
228 q
->completion_bh
= aio_bh_new(aio_context
, nvme_process_completion_bh
, q
);
229 r
= qemu_vfio_dma_map(s
->vfio
, q
->prp_list_pages
,
230 s
->page_size
* NVME_NUM_REQS
,
231 false, &prp_list_iova
);
235 q
->free_req_head
= -1;
236 for (i
= 0; i
< NVME_NUM_REQS
; i
++) {
237 NVMeRequest
*req
= &q
->reqs
[i
];
239 req
->free_req_next
= q
->free_req_head
;
240 q
->free_req_head
= i
;
241 req
->prp_list_page
= q
->prp_list_pages
+ i
* s
->page_size
;
242 req
->prp_list_iova
= prp_list_iova
+ i
* s
->page_size
;
245 nvme_init_queue(s
, &q
->sq
, size
, NVME_SQ_ENTRY_BYTES
, &local_err
);
247 error_propagate(errp
, local_err
);
250 q
->sq
.doorbell
= &s
->regs
->doorbells
[idx
* s
->doorbell_scale
].sq_tail
;
252 nvme_init_queue(s
, &q
->cq
, size
, NVME_CQ_ENTRY_BYTES
, &local_err
);
254 error_propagate(errp
, local_err
);
257 q
->cq
.doorbell
= &s
->regs
->doorbells
[idx
* s
->doorbell_scale
].cq_head
;
261 nvme_free_queue_pair(q
);
266 static void nvme_kick(NVMeQueuePair
*q
)
268 BDRVNVMeState
*s
= q
->s
;
270 if (s
->plugged
|| !q
->need_kick
) {
273 trace_nvme_kick(s
, q
->index
);
274 assert(!(q
->sq
.tail
& 0xFF00));
275 /* Fence the write to submission queue entry before notifying the device. */
277 *q
->sq
.doorbell
= cpu_to_le32(q
->sq
.tail
);
278 q
->inflight
+= q
->need_kick
;
282 /* Find a free request element if any, otherwise:
283 * a) if in coroutine context, try to wait for one to become available;
284 * b) if not in coroutine, return NULL;
286 static NVMeRequest
*nvme_get_free_req(NVMeQueuePair
*q
)
290 qemu_mutex_lock(&q
->lock
);
292 while (q
->free_req_head
== -1) {
293 if (qemu_in_coroutine()) {
294 trace_nvme_free_req_queue_wait(q
);
295 qemu_co_queue_wait(&q
->free_req_queue
, &q
->lock
);
297 qemu_mutex_unlock(&q
->lock
);
302 req
= &q
->reqs
[q
->free_req_head
];
303 q
->free_req_head
= req
->free_req_next
;
304 req
->free_req_next
= -1;
306 qemu_mutex_unlock(&q
->lock
);
311 static void nvme_put_free_req_locked(NVMeQueuePair
*q
, NVMeRequest
*req
)
313 req
->free_req_next
= q
->free_req_head
;
314 q
->free_req_head
= req
- q
->reqs
;
318 static void nvme_wake_free_req_locked(NVMeQueuePair
*q
)
320 if (!qemu_co_queue_empty(&q
->free_req_queue
)) {
321 replay_bh_schedule_oneshot_event(q
->s
->aio_context
,
322 nvme_free_req_queue_cb
, q
);
326 /* Insert a request in the freelist and wake waiters */
327 static void nvme_put_free_req_and_wake(NVMeQueuePair
*q
, NVMeRequest
*req
)
329 qemu_mutex_lock(&q
->lock
);
330 nvme_put_free_req_locked(q
, req
);
331 nvme_wake_free_req_locked(q
);
332 qemu_mutex_unlock(&q
->lock
);
335 static inline int nvme_translate_error(const NvmeCqe
*c
)
337 uint16_t status
= (le16_to_cpu(c
->status
) >> 1) & 0xFF;
339 trace_nvme_error(le32_to_cpu(c
->result
),
340 le16_to_cpu(c
->sq_head
),
341 le16_to_cpu(c
->sq_id
),
343 le16_to_cpu(status
));
358 static bool nvme_process_completion(NVMeQueuePair
*q
)
360 BDRVNVMeState
*s
= q
->s
;
361 bool progress
= false;
366 trace_nvme_process_completion(s
, q
->index
, q
->inflight
);
368 trace_nvme_process_completion_queue_plugged(s
, q
->index
);
373 * Support re-entrancy when a request cb() function invokes aio_poll().
374 * Pending completions must be visible to aio_poll() so that a cb()
375 * function can wait for the completion of another request.
377 * The aio_poll() loop will execute our BH and we'll resume completion
380 qemu_bh_schedule(q
->completion_bh
);
382 assert(q
->inflight
>= 0);
383 while (q
->inflight
) {
387 c
= (NvmeCqe
*)&q
->cq
.queue
[q
->cq
.head
* NVME_CQ_ENTRY_BYTES
];
388 if ((le16_to_cpu(c
->status
) & 0x1) == q
->cq_phase
) {
391 ret
= nvme_translate_error(c
);
392 q
->cq
.head
= (q
->cq
.head
+ 1) % NVME_QUEUE_SIZE
;
394 q
->cq_phase
= !q
->cq_phase
;
396 cid
= le16_to_cpu(c
->cid
);
397 if (cid
== 0 || cid
> NVME_QUEUE_SIZE
) {
398 fprintf(stderr
, "Unexpected CID in completion queue: %" PRIu32
"\n",
402 trace_nvme_complete_command(s
, q
->index
, cid
);
403 preq
= &q
->reqs
[cid
- 1];
405 assert(req
.cid
== cid
);
407 nvme_put_free_req_locked(q
, preq
);
408 preq
->cb
= preq
->opaque
= NULL
;
410 qemu_mutex_unlock(&q
->lock
);
411 req
.cb(req
.opaque
, ret
);
412 qemu_mutex_lock(&q
->lock
);
416 /* Notify the device so it can post more completions. */
418 *q
->cq
.doorbell
= cpu_to_le32(q
->cq
.head
);
419 nvme_wake_free_req_locked(q
);
422 qemu_bh_cancel(q
->completion_bh
);
427 static void nvme_process_completion_bh(void *opaque
)
429 NVMeQueuePair
*q
= opaque
;
432 * We're being invoked because a nvme_process_completion() cb() function
433 * called aio_poll(). The callback may be waiting for further completions
434 * so notify the device that it has space to fill in more completions now.
437 *q
->cq
.doorbell
= cpu_to_le32(q
->cq
.head
);
438 nvme_wake_free_req_locked(q
);
440 nvme_process_completion(q
);
443 static void nvme_trace_command(const NvmeCmd
*cmd
)
447 if (!trace_event_get_state_backends(TRACE_NVME_SUBMIT_COMMAND_RAW
)) {
450 for (i
= 0; i
< 8; ++i
) {
451 uint8_t *cmdp
= (uint8_t *)cmd
+ i
* 8;
452 trace_nvme_submit_command_raw(cmdp
[0], cmdp
[1], cmdp
[2], cmdp
[3],
453 cmdp
[4], cmdp
[5], cmdp
[6], cmdp
[7]);
457 static void nvme_submit_command(NVMeQueuePair
*q
, NVMeRequest
*req
,
458 NvmeCmd
*cmd
, BlockCompletionFunc cb
,
463 req
->opaque
= opaque
;
464 cmd
->cid
= cpu_to_le32(req
->cid
);
466 trace_nvme_submit_command(q
->s
, q
->index
, req
->cid
);
467 nvme_trace_command(cmd
);
468 qemu_mutex_lock(&q
->lock
);
469 memcpy((uint8_t *)q
->sq
.queue
+
470 q
->sq
.tail
* NVME_SQ_ENTRY_BYTES
, cmd
, sizeof(*cmd
));
471 q
->sq
.tail
= (q
->sq
.tail
+ 1) % NVME_QUEUE_SIZE
;
474 nvme_process_completion(q
);
475 qemu_mutex_unlock(&q
->lock
);
478 static void nvme_cmd_sync_cb(void *opaque
, int ret
)
485 static int nvme_cmd_sync(BlockDriverState
*bs
, NVMeQueuePair
*q
,
488 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
490 int ret
= -EINPROGRESS
;
491 req
= nvme_get_free_req(q
);
495 nvme_submit_command(q
, req
, cmd
, nvme_cmd_sync_cb
, &ret
);
497 AIO_WAIT_WHILE(aio_context
, ret
== -EINPROGRESS
);
501 static void nvme_identify(BlockDriverState
*bs
, int namespace, Error
**errp
)
503 BDRVNVMeState
*s
= bs
->opaque
;
513 .opcode
= NVME_ADM_CMD_IDENTIFY
,
514 .cdw10
= cpu_to_le32(0x1),
517 id
= qemu_try_memalign(s
->page_size
, sizeof(*id
));
519 error_setg(errp
, "Cannot allocate buffer for identify response");
522 r
= qemu_vfio_dma_map(s
->vfio
, id
, sizeof(*id
), true, &iova
);
524 error_setg(errp
, "Cannot map buffer for DMA");
528 memset(id
, 0, sizeof(*id
));
529 cmd
.dptr
.prp1
= cpu_to_le64(iova
);
530 if (nvme_cmd_sync(bs
, s
->queues
[INDEX_ADMIN
], &cmd
)) {
531 error_setg(errp
, "Failed to identify controller");
535 if (le32_to_cpu(id
->ctrl
.nn
) < namespace) {
536 error_setg(errp
, "Invalid namespace");
539 s
->write_cache_supported
= le32_to_cpu(id
->ctrl
.vwc
) & 0x1;
540 s
->max_transfer
= (id
->ctrl
.mdts
? 1 << id
->ctrl
.mdts
: 0) * s
->page_size
;
541 /* For now the page list buffer per command is one page, to hold at most
542 * s->page_size / sizeof(uint64_t) entries. */
543 s
->max_transfer
= MIN_NON_ZERO(s
->max_transfer
,
544 s
->page_size
/ sizeof(uint64_t) * s
->page_size
);
546 oncs
= le16_to_cpu(id
->ctrl
.oncs
);
547 s
->supports_write_zeroes
= !!(oncs
& NVME_ONCS_WRITE_ZEROES
);
548 s
->supports_discard
= !!(oncs
& NVME_ONCS_DSM
);
550 memset(id
, 0, sizeof(*id
));
552 cmd
.nsid
= cpu_to_le32(namespace);
553 if (nvme_cmd_sync(bs
, s
->queues
[INDEX_ADMIN
], &cmd
)) {
554 error_setg(errp
, "Failed to identify namespace");
558 s
->nsze
= le64_to_cpu(id
->ns
.nsze
);
559 lbaf
= &id
->ns
.lbaf
[NVME_ID_NS_FLBAS_INDEX(id
->ns
.flbas
)];
561 if (NVME_ID_NS_DLFEAT_WRITE_ZEROES(id
->ns
.dlfeat
) &&
562 NVME_ID_NS_DLFEAT_READ_BEHAVIOR(id
->ns
.dlfeat
) ==
563 NVME_ID_NS_DLFEAT_READ_BEHAVIOR_ZEROES
) {
564 bs
->supported_write_flags
|= BDRV_REQ_MAY_UNMAP
;
568 error_setg(errp
, "Namespaces with metadata are not yet supported");
572 if (lbaf
->ds
< BDRV_SECTOR_BITS
|| lbaf
->ds
> 12 ||
573 (1 << lbaf
->ds
) > s
->page_size
)
575 error_setg(errp
, "Namespace has unsupported block size (2^%d)",
580 s
->blkshift
= lbaf
->ds
;
582 qemu_vfio_dma_unmap(s
->vfio
, id
);
586 static bool nvme_poll_queue(NVMeQueuePair
*q
)
588 bool progress
= false;
590 const size_t cqe_offset
= q
->cq
.head
* NVME_CQ_ENTRY_BYTES
;
591 NvmeCqe
*cqe
= (NvmeCqe
*)&q
->cq
.queue
[cqe_offset
];
594 * Do an early check for completions. q->lock isn't needed because
595 * nvme_process_completion() only runs in the event loop thread and
596 * cannot race with itself.
598 if ((le16_to_cpu(cqe
->status
) & 0x1) == q
->cq_phase
) {
602 qemu_mutex_lock(&q
->lock
);
603 while (nvme_process_completion(q
)) {
607 qemu_mutex_unlock(&q
->lock
);
612 static bool nvme_poll_queues(BDRVNVMeState
*s
)
614 bool progress
= false;
617 for (i
= 0; i
< s
->nr_queues
; i
++) {
618 if (nvme_poll_queue(s
->queues
[i
])) {
625 static void nvme_handle_event(EventNotifier
*n
)
627 BDRVNVMeState
*s
= container_of(n
, BDRVNVMeState
,
628 irq_notifier
[MSIX_SHARED_IRQ_IDX
]);
630 trace_nvme_handle_event(s
);
631 event_notifier_test_and_clear(n
);
635 static bool nvme_add_io_queue(BlockDriverState
*bs
, Error
**errp
)
637 BDRVNVMeState
*s
= bs
->opaque
;
638 int n
= s
->nr_queues
;
641 int queue_size
= NVME_QUEUE_SIZE
;
643 q
= nvme_create_queue_pair(s
, bdrv_get_aio_context(bs
),
644 n
, queue_size
, errp
);
649 .opcode
= NVME_ADM_CMD_CREATE_CQ
,
650 .dptr
.prp1
= cpu_to_le64(q
->cq
.iova
),
651 .cdw10
= cpu_to_le32(((queue_size
- 1) << 16) | (n
& 0xFFFF)),
652 .cdw11
= cpu_to_le32(0x3),
654 if (nvme_cmd_sync(bs
, s
->queues
[INDEX_ADMIN
], &cmd
)) {
655 error_setg(errp
, "Failed to create CQ io queue [%d]", n
);
659 .opcode
= NVME_ADM_CMD_CREATE_SQ
,
660 .dptr
.prp1
= cpu_to_le64(q
->sq
.iova
),
661 .cdw10
= cpu_to_le32(((queue_size
- 1) << 16) | (n
& 0xFFFF)),
662 .cdw11
= cpu_to_le32(0x1 | (n
<< 16)),
664 if (nvme_cmd_sync(bs
, s
->queues
[INDEX_ADMIN
], &cmd
)) {
665 error_setg(errp
, "Failed to create SQ io queue [%d]", n
);
668 s
->queues
= g_renew(NVMeQueuePair
*, s
->queues
, n
+ 1);
673 nvme_free_queue_pair(q
);
677 static bool nvme_poll_cb(void *opaque
)
679 EventNotifier
*e
= opaque
;
680 BDRVNVMeState
*s
= container_of(e
, BDRVNVMeState
,
681 irq_notifier
[MSIX_SHARED_IRQ_IDX
]);
683 trace_nvme_poll_cb(s
);
684 return nvme_poll_queues(s
);
687 static int nvme_init(BlockDriverState
*bs
, const char *device
, int namespace,
690 BDRVNVMeState
*s
= bs
->opaque
;
691 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
695 uint64_t deadline
, now
;
696 Error
*local_err
= NULL
;
698 qemu_co_mutex_init(&s
->dma_map_lock
);
699 qemu_co_queue_init(&s
->dma_flush_queue
);
700 s
->device
= g_strdup(device
);
702 s
->aio_context
= bdrv_get_aio_context(bs
);
703 ret
= event_notifier_init(&s
->irq_notifier
[MSIX_SHARED_IRQ_IDX
], 0);
705 error_setg(errp
, "Failed to init event notifier");
709 s
->vfio
= qemu_vfio_open_pci(device
, errp
);
715 s
->regs
= qemu_vfio_pci_map_bar(s
->vfio
, 0, 0, NVME_BAR_SIZE
, errp
);
721 /* Perform initialize sequence as described in NVMe spec "7.6.1
722 * Initialization". */
724 cap
= le64_to_cpu(s
->regs
->ctrl
.cap
);
725 if (!(cap
& (1ULL << 37))) {
726 error_setg(errp
, "Device doesn't support NVMe command set");
731 s
->page_size
= MAX(4096, 1 << (12 + ((cap
>> 48) & 0xF)));
732 s
->doorbell_scale
= (4 << (((cap
>> 32) & 0xF))) / sizeof(uint32_t);
733 bs
->bl
.opt_mem_alignment
= s
->page_size
;
734 timeout_ms
= MIN(500 * ((cap
>> 24) & 0xFF), 30000);
736 /* Reset device to get a clean state. */
737 s
->regs
->ctrl
.cc
= cpu_to_le32(le32_to_cpu(s
->regs
->ctrl
.cc
) & 0xFE);
738 /* Wait for CSTS.RDY = 0. */
739 deadline
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ms
* SCALE_MS
;
740 while (le32_to_cpu(s
->regs
->ctrl
.csts
) & 0x1) {
741 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) > deadline
) {
742 error_setg(errp
, "Timeout while waiting for device to reset (%"
750 /* Set up admin queue. */
751 s
->queues
= g_new(NVMeQueuePair
*, 1);
752 s
->queues
[INDEX_ADMIN
] = nvme_create_queue_pair(s
, aio_context
, 0,
755 if (!s
->queues
[INDEX_ADMIN
]) {
760 QEMU_BUILD_BUG_ON(NVME_QUEUE_SIZE
& 0xF000);
761 s
->regs
->ctrl
.aqa
= cpu_to_le32((NVME_QUEUE_SIZE
<< 16) | NVME_QUEUE_SIZE
);
762 s
->regs
->ctrl
.asq
= cpu_to_le64(s
->queues
[INDEX_ADMIN
]->sq
.iova
);
763 s
->regs
->ctrl
.acq
= cpu_to_le64(s
->queues
[INDEX_ADMIN
]->cq
.iova
);
765 /* After setting up all control registers we can enable device now. */
766 s
->regs
->ctrl
.cc
= cpu_to_le32((ctz32(NVME_CQ_ENTRY_BYTES
) << 20) |
767 (ctz32(NVME_SQ_ENTRY_BYTES
) << 16) |
769 /* Wait for CSTS.RDY = 1. */
770 now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
771 deadline
= now
+ timeout_ms
* 1000000;
772 while (!(le32_to_cpu(s
->regs
->ctrl
.csts
) & 0x1)) {
773 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) > deadline
) {
774 error_setg(errp
, "Timeout while waiting for device to start (%"
782 ret
= qemu_vfio_pci_init_irq(s
->vfio
, s
->irq_notifier
,
783 VFIO_PCI_MSIX_IRQ_INDEX
, errp
);
787 aio_set_event_notifier(bdrv_get_aio_context(bs
),
788 &s
->irq_notifier
[MSIX_SHARED_IRQ_IDX
],
789 false, nvme_handle_event
, nvme_poll_cb
);
791 nvme_identify(bs
, namespace, &local_err
);
793 error_propagate(errp
, local_err
);
798 /* Set up command queues. */
799 if (!nvme_add_io_queue(bs
, errp
)) {
803 /* Cleaning up is done in nvme_file_open() upon error. */
807 /* Parse a filename in the format of nvme://XXXX:XX:XX.X/X. Example:
809 * nvme://0000:44:00.0/1
811 * where the "nvme://" is a fixed form of the protocol prefix, the middle part
812 * is the PCI address, and the last part is the namespace number starting from
813 * 1 according to the NVMe spec. */
814 static void nvme_parse_filename(const char *filename
, QDict
*options
,
817 int pref
= strlen("nvme://");
819 if (strlen(filename
) > pref
&& !strncmp(filename
, "nvme://", pref
)) {
820 const char *tmp
= filename
+ pref
;
822 const char *namespace;
824 const char *slash
= strchr(tmp
, '/');
826 qdict_put_str(options
, NVME_BLOCK_OPT_DEVICE
, tmp
);
829 device
= g_strndup(tmp
, slash
- tmp
);
830 qdict_put_str(options
, NVME_BLOCK_OPT_DEVICE
, device
);
832 namespace = slash
+ 1;
833 if (*namespace && qemu_strtoul(namespace, NULL
, 10, &ns
)) {
834 error_setg(errp
, "Invalid namespace '%s', positive number expected",
838 qdict_put_str(options
, NVME_BLOCK_OPT_NAMESPACE
,
839 *namespace ? namespace : "1");
843 static int nvme_enable_disable_write_cache(BlockDriverState
*bs
, bool enable
,
847 BDRVNVMeState
*s
= bs
->opaque
;
849 .opcode
= NVME_ADM_CMD_SET_FEATURES
,
850 .nsid
= cpu_to_le32(s
->nsid
),
851 .cdw10
= cpu_to_le32(0x06),
852 .cdw11
= cpu_to_le32(enable
? 0x01 : 0x00),
855 ret
= nvme_cmd_sync(bs
, s
->queues
[INDEX_ADMIN
], &cmd
);
857 error_setg(errp
, "Failed to configure NVMe write cache");
862 static void nvme_close(BlockDriverState
*bs
)
865 BDRVNVMeState
*s
= bs
->opaque
;
867 for (i
= 0; i
< s
->nr_queues
; ++i
) {
868 nvme_free_queue_pair(s
->queues
[i
]);
871 aio_set_event_notifier(bdrv_get_aio_context(bs
),
872 &s
->irq_notifier
[MSIX_SHARED_IRQ_IDX
],
874 event_notifier_cleanup(&s
->irq_notifier
[MSIX_SHARED_IRQ_IDX
]);
875 qemu_vfio_pci_unmap_bar(s
->vfio
, 0, (void *)s
->regs
, 0, NVME_BAR_SIZE
);
876 qemu_vfio_close(s
->vfio
);
881 static int nvme_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
888 BDRVNVMeState
*s
= bs
->opaque
;
890 bs
->supported_write_flags
= BDRV_REQ_FUA
;
892 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
893 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
894 device
= qemu_opt_get(opts
, NVME_BLOCK_OPT_DEVICE
);
896 error_setg(errp
, "'" NVME_BLOCK_OPT_DEVICE
"' option is required");
901 namespace = qemu_opt_get_number(opts
, NVME_BLOCK_OPT_NAMESPACE
, 1);
902 ret
= nvme_init(bs
, device
, namespace, errp
);
907 if (flags
& BDRV_O_NOCACHE
) {
908 if (!s
->write_cache_supported
) {
910 "NVMe controller doesn't support write cache configuration");
913 ret
= nvme_enable_disable_write_cache(bs
, !(flags
& BDRV_O_NOCACHE
),
926 static int64_t nvme_getlength(BlockDriverState
*bs
)
928 BDRVNVMeState
*s
= bs
->opaque
;
929 return s
->nsze
<< s
->blkshift
;
932 static uint32_t nvme_get_blocksize(BlockDriverState
*bs
)
934 BDRVNVMeState
*s
= bs
->opaque
;
935 assert(s
->blkshift
>= BDRV_SECTOR_BITS
&& s
->blkshift
<= 12);
936 return UINT32_C(1) << s
->blkshift
;
939 static int nvme_probe_blocksizes(BlockDriverState
*bs
, BlockSizes
*bsz
)
941 uint32_t blocksize
= nvme_get_blocksize(bs
);
942 bsz
->phys
= blocksize
;
943 bsz
->log
= blocksize
;
947 /* Called with s->dma_map_lock */
948 static coroutine_fn
int nvme_cmd_unmap_qiov(BlockDriverState
*bs
,
952 BDRVNVMeState
*s
= bs
->opaque
;
954 s
->dma_map_count
-= qiov
->size
;
955 if (!s
->dma_map_count
&& !qemu_co_queue_empty(&s
->dma_flush_queue
)) {
956 r
= qemu_vfio_dma_reset_temporary(s
->vfio
);
958 qemu_co_queue_restart_all(&s
->dma_flush_queue
);
964 /* Called with s->dma_map_lock */
965 static coroutine_fn
int nvme_cmd_map_qiov(BlockDriverState
*bs
, NvmeCmd
*cmd
,
966 NVMeRequest
*req
, QEMUIOVector
*qiov
)
968 BDRVNVMeState
*s
= bs
->opaque
;
969 uint64_t *pagelist
= req
->prp_list_page
;
974 assert(QEMU_IS_ALIGNED(qiov
->size
, s
->page_size
));
975 assert(qiov
->size
/ s
->page_size
<= s
->page_size
/ sizeof(uint64_t));
976 for (i
= 0; i
< qiov
->niov
; ++i
) {
980 r
= qemu_vfio_dma_map(s
->vfio
,
981 qiov
->iov
[i
].iov_base
,
982 qiov
->iov
[i
].iov_len
,
984 if (r
== -ENOMEM
&& retry
) {
986 trace_nvme_dma_flush_queue_wait(s
);
987 if (s
->dma_map_count
) {
988 trace_nvme_dma_map_flush(s
);
989 qemu_co_queue_wait(&s
->dma_flush_queue
, &s
->dma_map_lock
);
991 r
= qemu_vfio_dma_reset_temporary(s
->vfio
);
1002 for (j
= 0; j
< qiov
->iov
[i
].iov_len
/ s
->page_size
; j
++) {
1003 pagelist
[entries
++] = cpu_to_le64(iova
+ j
* s
->page_size
);
1005 trace_nvme_cmd_map_qiov_iov(s
, i
, qiov
->iov
[i
].iov_base
,
1006 qiov
->iov
[i
].iov_len
/ s
->page_size
);
1009 s
->dma_map_count
+= qiov
->size
;
1011 assert(entries
<= s
->page_size
/ sizeof(uint64_t));
1016 cmd
->dptr
.prp1
= pagelist
[0];
1020 cmd
->dptr
.prp1
= pagelist
[0];
1021 cmd
->dptr
.prp2
= pagelist
[1];
1024 cmd
->dptr
.prp1
= pagelist
[0];
1025 cmd
->dptr
.prp2
= cpu_to_le64(req
->prp_list_iova
+ sizeof(uint64_t));
1028 trace_nvme_cmd_map_qiov(s
, cmd
, req
, qiov
, entries
);
1029 for (i
= 0; i
< entries
; ++i
) {
1030 trace_nvme_cmd_map_qiov_pages(s
, i
, pagelist
[i
]);
1034 /* No need to unmap [0 - i) iovs even if we've failed, since we don't
1035 * increment s->dma_map_count. This is okay for fixed mapping memory areas
1036 * because they are already mapped before calling this function; for
1037 * temporary mappings, a later nvme_cmd_(un)map_qiov will reclaim by
1038 * calling qemu_vfio_dma_reset_temporary when necessary. */
1048 static void nvme_rw_cb_bh(void *opaque
)
1050 NVMeCoData
*data
= opaque
;
1051 qemu_coroutine_enter(data
->co
);
1054 static void nvme_rw_cb(void *opaque
, int ret
)
1056 NVMeCoData
*data
= opaque
;
1059 /* The rw coroutine hasn't yielded, don't try to enter. */
1062 replay_bh_schedule_oneshot_event(data
->ctx
, nvme_rw_cb_bh
, data
);
1065 static coroutine_fn
int nvme_co_prw_aligned(BlockDriverState
*bs
,
1066 uint64_t offset
, uint64_t bytes
,
1072 BDRVNVMeState
*s
= bs
->opaque
;
1073 NVMeQueuePair
*ioq
= s
->queues
[INDEX_IO(0)];
1076 uint32_t cdw12
= (((bytes
>> s
->blkshift
) - 1) & 0xFFFF) |
1077 (flags
& BDRV_REQ_FUA
? 1 << 30 : 0);
1079 .opcode
= is_write
? NVME_CMD_WRITE
: NVME_CMD_READ
,
1080 .nsid
= cpu_to_le32(s
->nsid
),
1081 .cdw10
= cpu_to_le32((offset
>> s
->blkshift
) & 0xFFFFFFFF),
1082 .cdw11
= cpu_to_le32(((offset
>> s
->blkshift
) >> 32) & 0xFFFFFFFF),
1083 .cdw12
= cpu_to_le32(cdw12
),
1086 .ctx
= bdrv_get_aio_context(bs
),
1087 .ret
= -EINPROGRESS
,
1090 trace_nvme_prw_aligned(s
, is_write
, offset
, bytes
, flags
, qiov
->niov
);
1091 assert(s
->nr_queues
> 1);
1092 req
= nvme_get_free_req(ioq
);
1095 qemu_co_mutex_lock(&s
->dma_map_lock
);
1096 r
= nvme_cmd_map_qiov(bs
, &cmd
, req
, qiov
);
1097 qemu_co_mutex_unlock(&s
->dma_map_lock
);
1099 nvme_put_free_req_and_wake(ioq
, req
);
1102 nvme_submit_command(ioq
, req
, &cmd
, nvme_rw_cb
, &data
);
1104 data
.co
= qemu_coroutine_self();
1105 while (data
.ret
== -EINPROGRESS
) {
1106 qemu_coroutine_yield();
1109 qemu_co_mutex_lock(&s
->dma_map_lock
);
1110 r
= nvme_cmd_unmap_qiov(bs
, qiov
);
1111 qemu_co_mutex_unlock(&s
->dma_map_lock
);
1116 trace_nvme_rw_done(s
, is_write
, offset
, bytes
, data
.ret
);
1120 static inline bool nvme_qiov_aligned(BlockDriverState
*bs
,
1121 const QEMUIOVector
*qiov
)
1124 BDRVNVMeState
*s
= bs
->opaque
;
1126 for (i
= 0; i
< qiov
->niov
; ++i
) {
1127 if (!QEMU_PTR_IS_ALIGNED(qiov
->iov
[i
].iov_base
, s
->page_size
) ||
1128 !QEMU_IS_ALIGNED(qiov
->iov
[i
].iov_len
, s
->page_size
)) {
1129 trace_nvme_qiov_unaligned(qiov
, i
, qiov
->iov
[i
].iov_base
,
1130 qiov
->iov
[i
].iov_len
, s
->page_size
);
1137 static int nvme_co_prw(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
1138 QEMUIOVector
*qiov
, bool is_write
, int flags
)
1140 BDRVNVMeState
*s
= bs
->opaque
;
1142 uint8_t *buf
= NULL
;
1143 QEMUIOVector local_qiov
;
1145 assert(QEMU_IS_ALIGNED(offset
, s
->page_size
));
1146 assert(QEMU_IS_ALIGNED(bytes
, s
->page_size
));
1147 assert(bytes
<= s
->max_transfer
);
1148 if (nvme_qiov_aligned(bs
, qiov
)) {
1149 return nvme_co_prw_aligned(bs
, offset
, bytes
, qiov
, is_write
, flags
);
1151 trace_nvme_prw_buffered(s
, offset
, bytes
, qiov
->niov
, is_write
);
1152 buf
= qemu_try_memalign(s
->page_size
, bytes
);
1157 qemu_iovec_init(&local_qiov
, 1);
1159 qemu_iovec_to_buf(qiov
, 0, buf
, bytes
);
1161 qemu_iovec_add(&local_qiov
, buf
, bytes
);
1162 r
= nvme_co_prw_aligned(bs
, offset
, bytes
, &local_qiov
, is_write
, flags
);
1163 qemu_iovec_destroy(&local_qiov
);
1164 if (!r
&& !is_write
) {
1165 qemu_iovec_from_buf(qiov
, 0, buf
, bytes
);
1171 static coroutine_fn
int nvme_co_preadv(BlockDriverState
*bs
,
1172 uint64_t offset
, uint64_t bytes
,
1173 QEMUIOVector
*qiov
, int flags
)
1175 return nvme_co_prw(bs
, offset
, bytes
, qiov
, false, flags
);
1178 static coroutine_fn
int nvme_co_pwritev(BlockDriverState
*bs
,
1179 uint64_t offset
, uint64_t bytes
,
1180 QEMUIOVector
*qiov
, int flags
)
1182 return nvme_co_prw(bs
, offset
, bytes
, qiov
, true, flags
);
1185 static coroutine_fn
int nvme_co_flush(BlockDriverState
*bs
)
1187 BDRVNVMeState
*s
= bs
->opaque
;
1188 NVMeQueuePair
*ioq
= s
->queues
[INDEX_IO(0)];
1191 .opcode
= NVME_CMD_FLUSH
,
1192 .nsid
= cpu_to_le32(s
->nsid
),
1195 .ctx
= bdrv_get_aio_context(bs
),
1196 .ret
= -EINPROGRESS
,
1199 assert(s
->nr_queues
> 1);
1200 req
= nvme_get_free_req(ioq
);
1202 nvme_submit_command(ioq
, req
, &cmd
, nvme_rw_cb
, &data
);
1204 data
.co
= qemu_coroutine_self();
1205 if (data
.ret
== -EINPROGRESS
) {
1206 qemu_coroutine_yield();
1213 static coroutine_fn
int nvme_co_pwrite_zeroes(BlockDriverState
*bs
,
1216 BdrvRequestFlags flags
)
1218 BDRVNVMeState
*s
= bs
->opaque
;
1219 NVMeQueuePair
*ioq
= s
->queues
[INDEX_IO(0)];
1222 uint32_t cdw12
= ((bytes
>> s
->blkshift
) - 1) & 0xFFFF;
1224 if (!s
->supports_write_zeroes
) {
1229 .opcode
= NVME_CMD_WRITE_ZEROES
,
1230 .nsid
= cpu_to_le32(s
->nsid
),
1231 .cdw10
= cpu_to_le32((offset
>> s
->blkshift
) & 0xFFFFFFFF),
1232 .cdw11
= cpu_to_le32(((offset
>> s
->blkshift
) >> 32) & 0xFFFFFFFF),
1236 .ctx
= bdrv_get_aio_context(bs
),
1237 .ret
= -EINPROGRESS
,
1240 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1244 if (flags
& BDRV_REQ_FUA
) {
1248 cmd
.cdw12
= cpu_to_le32(cdw12
);
1250 trace_nvme_write_zeroes(s
, offset
, bytes
, flags
);
1251 assert(s
->nr_queues
> 1);
1252 req
= nvme_get_free_req(ioq
);
1255 nvme_submit_command(ioq
, req
, &cmd
, nvme_rw_cb
, &data
);
1257 data
.co
= qemu_coroutine_self();
1258 while (data
.ret
== -EINPROGRESS
) {
1259 qemu_coroutine_yield();
1262 trace_nvme_rw_done(s
, true, offset
, bytes
, data
.ret
);
1267 static int coroutine_fn
nvme_co_pdiscard(BlockDriverState
*bs
,
1271 BDRVNVMeState
*s
= bs
->opaque
;
1272 NVMeQueuePair
*ioq
= s
->queues
[INDEX_IO(0)];
1275 QEMUIOVector local_qiov
;
1279 .opcode
= NVME_CMD_DSM
,
1280 .nsid
= cpu_to_le32(s
->nsid
),
1281 .cdw10
= cpu_to_le32(0), /*number of ranges - 0 based*/
1282 .cdw11
= cpu_to_le32(1 << 2), /*deallocate bit*/
1286 .ctx
= bdrv_get_aio_context(bs
),
1287 .ret
= -EINPROGRESS
,
1290 if (!s
->supports_discard
) {
1294 assert(s
->nr_queues
> 1);
1296 buf
= qemu_try_memalign(s
->page_size
, s
->page_size
);
1300 memset(buf
, 0, s
->page_size
);
1301 buf
->nlb
= cpu_to_le32(bytes
>> s
->blkshift
);
1302 buf
->slba
= cpu_to_le64(offset
>> s
->blkshift
);
1305 qemu_iovec_init(&local_qiov
, 1);
1306 qemu_iovec_add(&local_qiov
, buf
, 4096);
1308 req
= nvme_get_free_req(ioq
);
1311 qemu_co_mutex_lock(&s
->dma_map_lock
);
1312 ret
= nvme_cmd_map_qiov(bs
, &cmd
, req
, &local_qiov
);
1313 qemu_co_mutex_unlock(&s
->dma_map_lock
);
1316 nvme_put_free_req_and_wake(ioq
, req
);
1320 trace_nvme_dsm(s
, offset
, bytes
);
1322 nvme_submit_command(ioq
, req
, &cmd
, nvme_rw_cb
, &data
);
1324 data
.co
= qemu_coroutine_self();
1325 while (data
.ret
== -EINPROGRESS
) {
1326 qemu_coroutine_yield();
1329 qemu_co_mutex_lock(&s
->dma_map_lock
);
1330 ret
= nvme_cmd_unmap_qiov(bs
, &local_qiov
);
1331 qemu_co_mutex_unlock(&s
->dma_map_lock
);
1338 trace_nvme_dsm_done(s
, offset
, bytes
, ret
);
1340 qemu_iovec_destroy(&local_qiov
);
1347 static int nvme_reopen_prepare(BDRVReopenState
*reopen_state
,
1348 BlockReopenQueue
*queue
, Error
**errp
)
1353 static void nvme_refresh_filename(BlockDriverState
*bs
)
1355 BDRVNVMeState
*s
= bs
->opaque
;
1357 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
), "nvme://%s/%i",
1358 s
->device
, s
->nsid
);
1361 static void nvme_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1363 BDRVNVMeState
*s
= bs
->opaque
;
1365 bs
->bl
.opt_mem_alignment
= s
->page_size
;
1366 bs
->bl
.request_alignment
= s
->page_size
;
1367 bs
->bl
.max_transfer
= s
->max_transfer
;
1370 static void nvme_detach_aio_context(BlockDriverState
*bs
)
1372 BDRVNVMeState
*s
= bs
->opaque
;
1374 for (int i
= 0; i
< s
->nr_queues
; i
++) {
1375 NVMeQueuePair
*q
= s
->queues
[i
];
1377 qemu_bh_delete(q
->completion_bh
);
1378 q
->completion_bh
= NULL
;
1381 aio_set_event_notifier(bdrv_get_aio_context(bs
),
1382 &s
->irq_notifier
[MSIX_SHARED_IRQ_IDX
],
1386 static void nvme_attach_aio_context(BlockDriverState
*bs
,
1387 AioContext
*new_context
)
1389 BDRVNVMeState
*s
= bs
->opaque
;
1391 s
->aio_context
= new_context
;
1392 aio_set_event_notifier(new_context
, &s
->irq_notifier
[MSIX_SHARED_IRQ_IDX
],
1393 false, nvme_handle_event
, nvme_poll_cb
);
1395 for (int i
= 0; i
< s
->nr_queues
; i
++) {
1396 NVMeQueuePair
*q
= s
->queues
[i
];
1399 aio_bh_new(new_context
, nvme_process_completion_bh
, q
);
1403 static void nvme_aio_plug(BlockDriverState
*bs
)
1405 BDRVNVMeState
*s
= bs
->opaque
;
1406 assert(!s
->plugged
);
1410 static void nvme_aio_unplug(BlockDriverState
*bs
)
1413 BDRVNVMeState
*s
= bs
->opaque
;
1416 for (i
= INDEX_IO(0); i
< s
->nr_queues
; i
++) {
1417 NVMeQueuePair
*q
= s
->queues
[i
];
1418 qemu_mutex_lock(&q
->lock
);
1420 nvme_process_completion(q
);
1421 qemu_mutex_unlock(&q
->lock
);
1425 static void nvme_register_buf(BlockDriverState
*bs
, void *host
, size_t size
)
1428 BDRVNVMeState
*s
= bs
->opaque
;
1430 ret
= qemu_vfio_dma_map(s
->vfio
, host
, size
, false, NULL
);
1432 /* FIXME: we may run out of IOVA addresses after repeated
1433 * bdrv_register_buf/bdrv_unregister_buf, because nvme_vfio_dma_unmap
1434 * doesn't reclaim addresses for fixed mappings. */
1435 error_report("nvme_register_buf failed: %s", strerror(-ret
));
1439 static void nvme_unregister_buf(BlockDriverState
*bs
, void *host
)
1441 BDRVNVMeState
*s
= bs
->opaque
;
1443 qemu_vfio_dma_unmap(s
->vfio
, host
);
1446 static const char *const nvme_strong_runtime_opts
[] = {
1447 NVME_BLOCK_OPT_DEVICE
,
1448 NVME_BLOCK_OPT_NAMESPACE
,
1453 static BlockDriver bdrv_nvme
= {
1454 .format_name
= "nvme",
1455 .protocol_name
= "nvme",
1456 .instance_size
= sizeof(BDRVNVMeState
),
1458 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
1459 .create_opts
= &bdrv_create_opts_simple
,
1461 .bdrv_parse_filename
= nvme_parse_filename
,
1462 .bdrv_file_open
= nvme_file_open
,
1463 .bdrv_close
= nvme_close
,
1464 .bdrv_getlength
= nvme_getlength
,
1465 .bdrv_probe_blocksizes
= nvme_probe_blocksizes
,
1467 .bdrv_co_preadv
= nvme_co_preadv
,
1468 .bdrv_co_pwritev
= nvme_co_pwritev
,
1470 .bdrv_co_pwrite_zeroes
= nvme_co_pwrite_zeroes
,
1471 .bdrv_co_pdiscard
= nvme_co_pdiscard
,
1473 .bdrv_co_flush_to_disk
= nvme_co_flush
,
1474 .bdrv_reopen_prepare
= nvme_reopen_prepare
,
1476 .bdrv_refresh_filename
= nvme_refresh_filename
,
1477 .bdrv_refresh_limits
= nvme_refresh_limits
,
1478 .strong_runtime_opts
= nvme_strong_runtime_opts
,
1480 .bdrv_detach_aio_context
= nvme_detach_aio_context
,
1481 .bdrv_attach_aio_context
= nvme_attach_aio_context
,
1483 .bdrv_io_plug
= nvme_aio_plug
,
1484 .bdrv_io_unplug
= nvme_aio_unplug
,
1486 .bdrv_register_buf
= nvme_register_buf
,
1487 .bdrv_unregister_buf
= nvme_unregister_buf
,
1490 static void bdrv_nvme_init(void)
1492 bdrv_register(&bdrv_nvme
);
1495 block_init(bdrv_nvme_init
);