]> git.proxmox.com Git - mirror_qemu.git/blob - block/nvme.c
block/nvme: switch to a NVMeRequest freelist
[mirror_qemu.git] / block / nvme.c
1 /*
2 * NVMe block driver based on vfio
3 *
4 * Copyright 2016 - 2018 Red Hat, Inc.
5 *
6 * Authors:
7 * Fam Zheng <famz@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
9 *
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.
12 */
13
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"
27 #include "trace.h"
28
29 #include "block/nvme.h"
30
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
35
36 /*
37 * We have to leave one slot empty as that is the full queue case where
38 * head == tail + 1.
39 */
40 #define NVME_NUM_REQS (NVME_QUEUE_SIZE - 1)
41
42 typedef struct {
43 int32_t head, tail;
44 uint8_t *queue;
45 uint64_t iova;
46 /* Hardware MMIO register */
47 volatile uint32_t *doorbell;
48 } NVMeQueue;
49
50 typedef struct {
51 BlockCompletionFunc *cb;
52 void *opaque;
53 int cid;
54 void *prp_list_page;
55 uint64_t prp_list_iova;
56 int free_req_next; /* q->reqs[] index of next free req */
57 } NVMeRequest;
58
59 typedef struct {
60 CoQueue free_req_queue;
61 QemuMutex lock;
62
63 /* Fields protected by BQL */
64 int index;
65 uint8_t *prp_list_pages;
66
67 /* Fields protected by @lock */
68 NVMeQueue sq, cq;
69 int cq_phase;
70 int free_req_head;
71 NVMeRequest reqs[NVME_NUM_REQS];
72 bool busy;
73 int need_kick;
74 int inflight;
75 } NVMeQueuePair;
76
77 /* Memory mapped registers */
78 typedef volatile struct {
79 uint64_t cap;
80 uint32_t vs;
81 uint32_t intms;
82 uint32_t intmc;
83 uint32_t cc;
84 uint32_t reserved0;
85 uint32_t csts;
86 uint32_t nssr;
87 uint32_t aqa;
88 uint64_t asq;
89 uint64_t acq;
90 uint32_t cmbloc;
91 uint32_t cmbsz;
92 uint8_t reserved1[0xec0];
93 uint8_t cmd_set_specfic[0x100];
94 uint32_t doorbells[];
95 } NVMeRegs;
96
97 QEMU_BUILD_BUG_ON(offsetof(NVMeRegs, doorbells) != 0x1000);
98
99 typedef struct {
100 AioContext *aio_context;
101 QEMUVFIOState *vfio;
102 NVMeRegs *regs;
103 /* The submission/completion queue pairs.
104 * [0]: admin queue.
105 * [1..]: io queues.
106 */
107 NVMeQueuePair **queues;
108 int nr_queues;
109 size_t page_size;
110 /* How many uint32_t elements does each doorbell entry take. */
111 size_t doorbell_scale;
112 bool write_cache_supported;
113 EventNotifier irq_notifier;
114
115 uint64_t nsze; /* Namespace size reported by identify command */
116 int nsid; /* The namespace id to read/write data. */
117 int blkshift;
118
119 uint64_t max_transfer;
120 bool plugged;
121
122 bool supports_write_zeroes;
123 bool supports_discard;
124
125 CoMutex dma_map_lock;
126 CoQueue dma_flush_queue;
127
128 /* Total size of mapped qiov, accessed under dma_map_lock */
129 int dma_map_count;
130
131 /* PCI address (required for nvme_refresh_filename()) */
132 char *device;
133 } BDRVNVMeState;
134
135 #define NVME_BLOCK_OPT_DEVICE "device"
136 #define NVME_BLOCK_OPT_NAMESPACE "namespace"
137
138 static QemuOptsList runtime_opts = {
139 .name = "nvme",
140 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
141 .desc = {
142 {
143 .name = NVME_BLOCK_OPT_DEVICE,
144 .type = QEMU_OPT_STRING,
145 .help = "NVMe PCI device address",
146 },
147 {
148 .name = NVME_BLOCK_OPT_NAMESPACE,
149 .type = QEMU_OPT_NUMBER,
150 .help = "NVMe namespace",
151 },
152 { /* end of list */ }
153 },
154 };
155
156 static void nvme_init_queue(BlockDriverState *bs, NVMeQueue *q,
157 int nentries, int entry_bytes, Error **errp)
158 {
159 BDRVNVMeState *s = bs->opaque;
160 size_t bytes;
161 int r;
162
163 bytes = ROUND_UP(nentries * entry_bytes, s->page_size);
164 q->head = q->tail = 0;
165 q->queue = qemu_try_blockalign0(bs, bytes);
166
167 if (!q->queue) {
168 error_setg(errp, "Cannot allocate queue");
169 return;
170 }
171 r = qemu_vfio_dma_map(s->vfio, q->queue, bytes, false, &q->iova);
172 if (r) {
173 error_setg(errp, "Cannot map queue");
174 }
175 }
176
177 static void nvme_free_queue_pair(BlockDriverState *bs, NVMeQueuePair *q)
178 {
179 qemu_vfree(q->prp_list_pages);
180 qemu_vfree(q->sq.queue);
181 qemu_vfree(q->cq.queue);
182 qemu_mutex_destroy(&q->lock);
183 g_free(q);
184 }
185
186 static void nvme_free_req_queue_cb(void *opaque)
187 {
188 NVMeQueuePair *q = opaque;
189
190 qemu_mutex_lock(&q->lock);
191 while (qemu_co_enter_next(&q->free_req_queue, &q->lock)) {
192 /* Retry all pending requests */
193 }
194 qemu_mutex_unlock(&q->lock);
195 }
196
197 static NVMeQueuePair *nvme_create_queue_pair(BlockDriverState *bs,
198 int idx, int size,
199 Error **errp)
200 {
201 int i, r;
202 BDRVNVMeState *s = bs->opaque;
203 Error *local_err = NULL;
204 NVMeQueuePair *q = g_new0(NVMeQueuePair, 1);
205 uint64_t prp_list_iova;
206
207 qemu_mutex_init(&q->lock);
208 q->index = idx;
209 qemu_co_queue_init(&q->free_req_queue);
210 q->prp_list_pages = qemu_blockalign0(bs, s->page_size * NVME_NUM_REQS);
211 r = qemu_vfio_dma_map(s->vfio, q->prp_list_pages,
212 s->page_size * NVME_NUM_REQS,
213 false, &prp_list_iova);
214 if (r) {
215 goto fail;
216 }
217 q->free_req_head = -1;
218 for (i = 0; i < NVME_NUM_REQS; i++) {
219 NVMeRequest *req = &q->reqs[i];
220 req->cid = i + 1;
221 req->free_req_next = q->free_req_head;
222 q->free_req_head = i;
223 req->prp_list_page = q->prp_list_pages + i * s->page_size;
224 req->prp_list_iova = prp_list_iova + i * s->page_size;
225 }
226
227 nvme_init_queue(bs, &q->sq, size, NVME_SQ_ENTRY_BYTES, &local_err);
228 if (local_err) {
229 error_propagate(errp, local_err);
230 goto fail;
231 }
232 q->sq.doorbell = &s->regs->doorbells[idx * 2 * s->doorbell_scale];
233
234 nvme_init_queue(bs, &q->cq, size, NVME_CQ_ENTRY_BYTES, &local_err);
235 if (local_err) {
236 error_propagate(errp, local_err);
237 goto fail;
238 }
239 q->cq.doorbell = &s->regs->doorbells[(idx * 2 + 1) * s->doorbell_scale];
240
241 return q;
242 fail:
243 nvme_free_queue_pair(bs, q);
244 return NULL;
245 }
246
247 /* With q->lock */
248 static void nvme_kick(BDRVNVMeState *s, NVMeQueuePair *q)
249 {
250 if (s->plugged || !q->need_kick) {
251 return;
252 }
253 trace_nvme_kick(s, q->index);
254 assert(!(q->sq.tail & 0xFF00));
255 /* Fence the write to submission queue entry before notifying the device. */
256 smp_wmb();
257 *q->sq.doorbell = cpu_to_le32(q->sq.tail);
258 q->inflight += q->need_kick;
259 q->need_kick = 0;
260 }
261
262 /* Find a free request element if any, otherwise:
263 * a) if in coroutine context, try to wait for one to become available;
264 * b) if not in coroutine, return NULL;
265 */
266 static NVMeRequest *nvme_get_free_req(NVMeQueuePair *q)
267 {
268 NVMeRequest *req;
269
270 qemu_mutex_lock(&q->lock);
271
272 while (q->free_req_head == -1) {
273 if (qemu_in_coroutine()) {
274 trace_nvme_free_req_queue_wait(q);
275 qemu_co_queue_wait(&q->free_req_queue, &q->lock);
276 } else {
277 qemu_mutex_unlock(&q->lock);
278 return NULL;
279 }
280 }
281
282 req = &q->reqs[q->free_req_head];
283 q->free_req_head = req->free_req_next;
284 req->free_req_next = -1;
285
286 qemu_mutex_unlock(&q->lock);
287 return req;
288 }
289
290 /* With q->lock */
291 static void nvme_put_free_req_locked(NVMeQueuePair *q, NVMeRequest *req)
292 {
293 req->free_req_next = q->free_req_head;
294 q->free_req_head = req - q->reqs;
295 }
296
297 /* With q->lock */
298 static void nvme_wake_free_req_locked(BDRVNVMeState *s, NVMeQueuePair *q)
299 {
300 if (!qemu_co_queue_empty(&q->free_req_queue)) {
301 replay_bh_schedule_oneshot_event(s->aio_context,
302 nvme_free_req_queue_cb, q);
303 }
304 }
305
306 /* Insert a request in the freelist and wake waiters */
307 static void nvme_put_free_req_and_wake(BDRVNVMeState *s, NVMeQueuePair *q,
308 NVMeRequest *req)
309 {
310 qemu_mutex_lock(&q->lock);
311 nvme_put_free_req_locked(q, req);
312 nvme_wake_free_req_locked(s, q);
313 qemu_mutex_unlock(&q->lock);
314 }
315
316 static inline int nvme_translate_error(const NvmeCqe *c)
317 {
318 uint16_t status = (le16_to_cpu(c->status) >> 1) & 0xFF;
319 if (status) {
320 trace_nvme_error(le32_to_cpu(c->result),
321 le16_to_cpu(c->sq_head),
322 le16_to_cpu(c->sq_id),
323 le16_to_cpu(c->cid),
324 le16_to_cpu(status));
325 }
326 switch (status) {
327 case 0:
328 return 0;
329 case 1:
330 return -ENOSYS;
331 case 2:
332 return -EINVAL;
333 default:
334 return -EIO;
335 }
336 }
337
338 /* With q->lock */
339 static bool nvme_process_completion(BDRVNVMeState *s, NVMeQueuePair *q)
340 {
341 bool progress = false;
342 NVMeRequest *preq;
343 NVMeRequest req;
344 NvmeCqe *c;
345
346 trace_nvme_process_completion(s, q->index, q->inflight);
347 if (q->busy || s->plugged) {
348 trace_nvme_process_completion_queue_busy(s, q->index);
349 return false;
350 }
351 q->busy = true;
352 assert(q->inflight >= 0);
353 while (q->inflight) {
354 int ret;
355 int16_t cid;
356
357 c = (NvmeCqe *)&q->cq.queue[q->cq.head * NVME_CQ_ENTRY_BYTES];
358 if ((le16_to_cpu(c->status) & 0x1) == q->cq_phase) {
359 break;
360 }
361 ret = nvme_translate_error(c);
362 q->cq.head = (q->cq.head + 1) % NVME_QUEUE_SIZE;
363 if (!q->cq.head) {
364 q->cq_phase = !q->cq_phase;
365 }
366 cid = le16_to_cpu(c->cid);
367 if (cid == 0 || cid > NVME_QUEUE_SIZE) {
368 fprintf(stderr, "Unexpected CID in completion queue: %" PRIu32 "\n",
369 cid);
370 continue;
371 }
372 trace_nvme_complete_command(s, q->index, cid);
373 preq = &q->reqs[cid - 1];
374 req = *preq;
375 assert(req.cid == cid);
376 assert(req.cb);
377 nvme_put_free_req_locked(q, preq);
378 preq->cb = preq->opaque = NULL;
379 qemu_mutex_unlock(&q->lock);
380 req.cb(req.opaque, ret);
381 qemu_mutex_lock(&q->lock);
382 q->inflight--;
383 progress = true;
384 }
385 if (progress) {
386 /* Notify the device so it can post more completions. */
387 smp_mb_release();
388 *q->cq.doorbell = cpu_to_le32(q->cq.head);
389 nvme_wake_free_req_locked(s, q);
390 }
391 q->busy = false;
392 return progress;
393 }
394
395 static void nvme_trace_command(const NvmeCmd *cmd)
396 {
397 int i;
398
399 for (i = 0; i < 8; ++i) {
400 uint8_t *cmdp = (uint8_t *)cmd + i * 8;
401 trace_nvme_submit_command_raw(cmdp[0], cmdp[1], cmdp[2], cmdp[3],
402 cmdp[4], cmdp[5], cmdp[6], cmdp[7]);
403 }
404 }
405
406 static void nvme_submit_command(BDRVNVMeState *s, NVMeQueuePair *q,
407 NVMeRequest *req,
408 NvmeCmd *cmd, BlockCompletionFunc cb,
409 void *opaque)
410 {
411 assert(!req->cb);
412 req->cb = cb;
413 req->opaque = opaque;
414 cmd->cid = cpu_to_le32(req->cid);
415
416 trace_nvme_submit_command(s, q->index, req->cid);
417 nvme_trace_command(cmd);
418 qemu_mutex_lock(&q->lock);
419 memcpy((uint8_t *)q->sq.queue +
420 q->sq.tail * NVME_SQ_ENTRY_BYTES, cmd, sizeof(*cmd));
421 q->sq.tail = (q->sq.tail + 1) % NVME_QUEUE_SIZE;
422 q->need_kick++;
423 nvme_kick(s, q);
424 nvme_process_completion(s, q);
425 qemu_mutex_unlock(&q->lock);
426 }
427
428 static void nvme_cmd_sync_cb(void *opaque, int ret)
429 {
430 int *pret = opaque;
431 *pret = ret;
432 aio_wait_kick();
433 }
434
435 static int nvme_cmd_sync(BlockDriverState *bs, NVMeQueuePair *q,
436 NvmeCmd *cmd)
437 {
438 NVMeRequest *req;
439 BDRVNVMeState *s = bs->opaque;
440 int ret = -EINPROGRESS;
441 req = nvme_get_free_req(q);
442 if (!req) {
443 return -EBUSY;
444 }
445 nvme_submit_command(s, q, req, cmd, nvme_cmd_sync_cb, &ret);
446
447 BDRV_POLL_WHILE(bs, ret == -EINPROGRESS);
448 return ret;
449 }
450
451 static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
452 {
453 BDRVNVMeState *s = bs->opaque;
454 NvmeIdCtrl *idctrl;
455 NvmeIdNs *idns;
456 NvmeLBAF *lbaf;
457 uint8_t *resp;
458 uint16_t oncs;
459 int r;
460 uint64_t iova;
461 NvmeCmd cmd = {
462 .opcode = NVME_ADM_CMD_IDENTIFY,
463 .cdw10 = cpu_to_le32(0x1),
464 };
465
466 resp = qemu_try_blockalign0(bs, sizeof(NvmeIdCtrl));
467 if (!resp) {
468 error_setg(errp, "Cannot allocate buffer for identify response");
469 goto out;
470 }
471 idctrl = (NvmeIdCtrl *)resp;
472 idns = (NvmeIdNs *)resp;
473 r = qemu_vfio_dma_map(s->vfio, resp, sizeof(NvmeIdCtrl), true, &iova);
474 if (r) {
475 error_setg(errp, "Cannot map buffer for DMA");
476 goto out;
477 }
478 cmd.prp1 = cpu_to_le64(iova);
479
480 if (nvme_cmd_sync(bs, s->queues[0], &cmd)) {
481 error_setg(errp, "Failed to identify controller");
482 goto out;
483 }
484
485 if (le32_to_cpu(idctrl->nn) < namespace) {
486 error_setg(errp, "Invalid namespace");
487 goto out;
488 }
489 s->write_cache_supported = le32_to_cpu(idctrl->vwc) & 0x1;
490 s->max_transfer = (idctrl->mdts ? 1 << idctrl->mdts : 0) * s->page_size;
491 /* For now the page list buffer per command is one page, to hold at most
492 * s->page_size / sizeof(uint64_t) entries. */
493 s->max_transfer = MIN_NON_ZERO(s->max_transfer,
494 s->page_size / sizeof(uint64_t) * s->page_size);
495
496 oncs = le16_to_cpu(idctrl->oncs);
497 s->supports_write_zeroes = !!(oncs & NVME_ONCS_WRITE_ZEROS);
498 s->supports_discard = !!(oncs & NVME_ONCS_DSM);
499
500 memset(resp, 0, 4096);
501
502 cmd.cdw10 = 0;
503 cmd.nsid = cpu_to_le32(namespace);
504 if (nvme_cmd_sync(bs, s->queues[0], &cmd)) {
505 error_setg(errp, "Failed to identify namespace");
506 goto out;
507 }
508
509 s->nsze = le64_to_cpu(idns->nsze);
510 lbaf = &idns->lbaf[NVME_ID_NS_FLBAS_INDEX(idns->flbas)];
511
512 if (NVME_ID_NS_DLFEAT_WRITE_ZEROES(idns->dlfeat) &&
513 NVME_ID_NS_DLFEAT_READ_BEHAVIOR(idns->dlfeat) ==
514 NVME_ID_NS_DLFEAT_READ_BEHAVIOR_ZEROES) {
515 bs->supported_write_flags |= BDRV_REQ_MAY_UNMAP;
516 }
517
518 if (lbaf->ms) {
519 error_setg(errp, "Namespaces with metadata are not yet supported");
520 goto out;
521 }
522
523 if (lbaf->ds < BDRV_SECTOR_BITS || lbaf->ds > 12 ||
524 (1 << lbaf->ds) > s->page_size)
525 {
526 error_setg(errp, "Namespace has unsupported block size (2^%d)",
527 lbaf->ds);
528 goto out;
529 }
530
531 s->blkshift = lbaf->ds;
532 out:
533 qemu_vfio_dma_unmap(s->vfio, resp);
534 qemu_vfree(resp);
535 }
536
537 static bool nvme_poll_queues(BDRVNVMeState *s)
538 {
539 bool progress = false;
540 int i;
541
542 for (i = 0; i < s->nr_queues; i++) {
543 NVMeQueuePair *q = s->queues[i];
544 const size_t cqe_offset = q->cq.head * NVME_CQ_ENTRY_BYTES;
545 NvmeCqe *cqe = (NvmeCqe *)&q->cq.queue[cqe_offset];
546
547 /*
548 * Do an early check for completions. q->lock isn't needed because
549 * nvme_process_completion() only runs in the event loop thread and
550 * cannot race with itself.
551 */
552 if ((le16_to_cpu(cqe->status) & 0x1) == q->cq_phase) {
553 continue;
554 }
555
556 qemu_mutex_lock(&q->lock);
557 while (nvme_process_completion(s, q)) {
558 /* Keep polling */
559 progress = true;
560 }
561 qemu_mutex_unlock(&q->lock);
562 }
563 return progress;
564 }
565
566 static void nvme_handle_event(EventNotifier *n)
567 {
568 BDRVNVMeState *s = container_of(n, BDRVNVMeState, irq_notifier);
569
570 trace_nvme_handle_event(s);
571 event_notifier_test_and_clear(n);
572 nvme_poll_queues(s);
573 }
574
575 static bool nvme_add_io_queue(BlockDriverState *bs, Error **errp)
576 {
577 BDRVNVMeState *s = bs->opaque;
578 int n = s->nr_queues;
579 NVMeQueuePair *q;
580 NvmeCmd cmd;
581 int queue_size = NVME_QUEUE_SIZE;
582
583 q = nvme_create_queue_pair(bs, n, queue_size, errp);
584 if (!q) {
585 return false;
586 }
587 cmd = (NvmeCmd) {
588 .opcode = NVME_ADM_CMD_CREATE_CQ,
589 .prp1 = cpu_to_le64(q->cq.iova),
590 .cdw10 = cpu_to_le32(((queue_size - 1) << 16) | (n & 0xFFFF)),
591 .cdw11 = cpu_to_le32(0x3),
592 };
593 if (nvme_cmd_sync(bs, s->queues[0], &cmd)) {
594 error_setg(errp, "Failed to create io queue [%d]", n);
595 nvme_free_queue_pair(bs, q);
596 return false;
597 }
598 cmd = (NvmeCmd) {
599 .opcode = NVME_ADM_CMD_CREATE_SQ,
600 .prp1 = cpu_to_le64(q->sq.iova),
601 .cdw10 = cpu_to_le32(((queue_size - 1) << 16) | (n & 0xFFFF)),
602 .cdw11 = cpu_to_le32(0x1 | (n << 16)),
603 };
604 if (nvme_cmd_sync(bs, s->queues[0], &cmd)) {
605 error_setg(errp, "Failed to create io queue [%d]", n);
606 nvme_free_queue_pair(bs, q);
607 return false;
608 }
609 s->queues = g_renew(NVMeQueuePair *, s->queues, n + 1);
610 s->queues[n] = q;
611 s->nr_queues++;
612 return true;
613 }
614
615 static bool nvme_poll_cb(void *opaque)
616 {
617 EventNotifier *e = opaque;
618 BDRVNVMeState *s = container_of(e, BDRVNVMeState, irq_notifier);
619
620 trace_nvme_poll_cb(s);
621 return nvme_poll_queues(s);
622 }
623
624 static int nvme_init(BlockDriverState *bs, const char *device, int namespace,
625 Error **errp)
626 {
627 BDRVNVMeState *s = bs->opaque;
628 int ret;
629 uint64_t cap;
630 uint64_t timeout_ms;
631 uint64_t deadline, now;
632 Error *local_err = NULL;
633
634 qemu_co_mutex_init(&s->dma_map_lock);
635 qemu_co_queue_init(&s->dma_flush_queue);
636 s->device = g_strdup(device);
637 s->nsid = namespace;
638 s->aio_context = bdrv_get_aio_context(bs);
639 ret = event_notifier_init(&s->irq_notifier, 0);
640 if (ret) {
641 error_setg(errp, "Failed to init event notifier");
642 return ret;
643 }
644
645 s->vfio = qemu_vfio_open_pci(device, errp);
646 if (!s->vfio) {
647 ret = -EINVAL;
648 goto out;
649 }
650
651 s->regs = qemu_vfio_pci_map_bar(s->vfio, 0, 0, NVME_BAR_SIZE, errp);
652 if (!s->regs) {
653 ret = -EINVAL;
654 goto out;
655 }
656
657 /* Perform initialize sequence as described in NVMe spec "7.6.1
658 * Initialization". */
659
660 cap = le64_to_cpu(s->regs->cap);
661 if (!(cap & (1ULL << 37))) {
662 error_setg(errp, "Device doesn't support NVMe command set");
663 ret = -EINVAL;
664 goto out;
665 }
666
667 s->page_size = MAX(4096, 1 << (12 + ((cap >> 48) & 0xF)));
668 s->doorbell_scale = (4 << (((cap >> 32) & 0xF))) / sizeof(uint32_t);
669 bs->bl.opt_mem_alignment = s->page_size;
670 timeout_ms = MIN(500 * ((cap >> 24) & 0xFF), 30000);
671
672 /* Reset device to get a clean state. */
673 s->regs->cc = cpu_to_le32(le32_to_cpu(s->regs->cc) & 0xFE);
674 /* Wait for CSTS.RDY = 0. */
675 deadline = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ms * 1000000ULL;
676 while (le32_to_cpu(s->regs->csts) & 0x1) {
677 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) > deadline) {
678 error_setg(errp, "Timeout while waiting for device to reset (%"
679 PRId64 " ms)",
680 timeout_ms);
681 ret = -ETIMEDOUT;
682 goto out;
683 }
684 }
685
686 /* Set up admin queue. */
687 s->queues = g_new(NVMeQueuePair *, 1);
688 s->queues[0] = nvme_create_queue_pair(bs, 0, NVME_QUEUE_SIZE, errp);
689 if (!s->queues[0]) {
690 ret = -EINVAL;
691 goto out;
692 }
693 s->nr_queues = 1;
694 QEMU_BUILD_BUG_ON(NVME_QUEUE_SIZE & 0xF000);
695 s->regs->aqa = cpu_to_le32((NVME_QUEUE_SIZE << 16) | NVME_QUEUE_SIZE);
696 s->regs->asq = cpu_to_le64(s->queues[0]->sq.iova);
697 s->regs->acq = cpu_to_le64(s->queues[0]->cq.iova);
698
699 /* After setting up all control registers we can enable device now. */
700 s->regs->cc = cpu_to_le32((ctz32(NVME_CQ_ENTRY_BYTES) << 20) |
701 (ctz32(NVME_SQ_ENTRY_BYTES) << 16) |
702 0x1);
703 /* Wait for CSTS.RDY = 1. */
704 now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
705 deadline = now + timeout_ms * 1000000;
706 while (!(le32_to_cpu(s->regs->csts) & 0x1)) {
707 if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) > deadline) {
708 error_setg(errp, "Timeout while waiting for device to start (%"
709 PRId64 " ms)",
710 timeout_ms);
711 ret = -ETIMEDOUT;
712 goto out;
713 }
714 }
715
716 ret = qemu_vfio_pci_init_irq(s->vfio, &s->irq_notifier,
717 VFIO_PCI_MSIX_IRQ_INDEX, errp);
718 if (ret) {
719 goto out;
720 }
721 aio_set_event_notifier(bdrv_get_aio_context(bs), &s->irq_notifier,
722 false, nvme_handle_event, nvme_poll_cb);
723
724 nvme_identify(bs, namespace, &local_err);
725 if (local_err) {
726 error_propagate(errp, local_err);
727 ret = -EIO;
728 goto out;
729 }
730
731 /* Set up command queues. */
732 if (!nvme_add_io_queue(bs, errp)) {
733 ret = -EIO;
734 }
735 out:
736 /* Cleaning up is done in nvme_file_open() upon error. */
737 return ret;
738 }
739
740 /* Parse a filename in the format of nvme://XXXX:XX:XX.X/X. Example:
741 *
742 * nvme://0000:44:00.0/1
743 *
744 * where the "nvme://" is a fixed form of the protocol prefix, the middle part
745 * is the PCI address, and the last part is the namespace number starting from
746 * 1 according to the NVMe spec. */
747 static void nvme_parse_filename(const char *filename, QDict *options,
748 Error **errp)
749 {
750 int pref = strlen("nvme://");
751
752 if (strlen(filename) > pref && !strncmp(filename, "nvme://", pref)) {
753 const char *tmp = filename + pref;
754 char *device;
755 const char *namespace;
756 unsigned long ns;
757 const char *slash = strchr(tmp, '/');
758 if (!slash) {
759 qdict_put_str(options, NVME_BLOCK_OPT_DEVICE, tmp);
760 return;
761 }
762 device = g_strndup(tmp, slash - tmp);
763 qdict_put_str(options, NVME_BLOCK_OPT_DEVICE, device);
764 g_free(device);
765 namespace = slash + 1;
766 if (*namespace && qemu_strtoul(namespace, NULL, 10, &ns)) {
767 error_setg(errp, "Invalid namespace '%s', positive number expected",
768 namespace);
769 return;
770 }
771 qdict_put_str(options, NVME_BLOCK_OPT_NAMESPACE,
772 *namespace ? namespace : "1");
773 }
774 }
775
776 static int nvme_enable_disable_write_cache(BlockDriverState *bs, bool enable,
777 Error **errp)
778 {
779 int ret;
780 BDRVNVMeState *s = bs->opaque;
781 NvmeCmd cmd = {
782 .opcode = NVME_ADM_CMD_SET_FEATURES,
783 .nsid = cpu_to_le32(s->nsid),
784 .cdw10 = cpu_to_le32(0x06),
785 .cdw11 = cpu_to_le32(enable ? 0x01 : 0x00),
786 };
787
788 ret = nvme_cmd_sync(bs, s->queues[0], &cmd);
789 if (ret) {
790 error_setg(errp, "Failed to configure NVMe write cache");
791 }
792 return ret;
793 }
794
795 static void nvme_close(BlockDriverState *bs)
796 {
797 int i;
798 BDRVNVMeState *s = bs->opaque;
799
800 for (i = 0; i < s->nr_queues; ++i) {
801 nvme_free_queue_pair(bs, s->queues[i]);
802 }
803 g_free(s->queues);
804 aio_set_event_notifier(bdrv_get_aio_context(bs), &s->irq_notifier,
805 false, NULL, NULL);
806 event_notifier_cleanup(&s->irq_notifier);
807 qemu_vfio_pci_unmap_bar(s->vfio, 0, (void *)s->regs, 0, NVME_BAR_SIZE);
808 qemu_vfio_close(s->vfio);
809
810 g_free(s->device);
811 }
812
813 static int nvme_file_open(BlockDriverState *bs, QDict *options, int flags,
814 Error **errp)
815 {
816 const char *device;
817 QemuOpts *opts;
818 int namespace;
819 int ret;
820 BDRVNVMeState *s = bs->opaque;
821
822 bs->supported_write_flags = BDRV_REQ_FUA;
823
824 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
825 qemu_opts_absorb_qdict(opts, options, &error_abort);
826 device = qemu_opt_get(opts, NVME_BLOCK_OPT_DEVICE);
827 if (!device) {
828 error_setg(errp, "'" NVME_BLOCK_OPT_DEVICE "' option is required");
829 qemu_opts_del(opts);
830 return -EINVAL;
831 }
832
833 namespace = qemu_opt_get_number(opts, NVME_BLOCK_OPT_NAMESPACE, 1);
834 ret = nvme_init(bs, device, namespace, errp);
835 qemu_opts_del(opts);
836 if (ret) {
837 goto fail;
838 }
839 if (flags & BDRV_O_NOCACHE) {
840 if (!s->write_cache_supported) {
841 error_setg(errp,
842 "NVMe controller doesn't support write cache configuration");
843 ret = -EINVAL;
844 } else {
845 ret = nvme_enable_disable_write_cache(bs, !(flags & BDRV_O_NOCACHE),
846 errp);
847 }
848 if (ret) {
849 goto fail;
850 }
851 }
852 return 0;
853 fail:
854 nvme_close(bs);
855 return ret;
856 }
857
858 static int64_t nvme_getlength(BlockDriverState *bs)
859 {
860 BDRVNVMeState *s = bs->opaque;
861 return s->nsze << s->blkshift;
862 }
863
864 static uint32_t nvme_get_blocksize(BlockDriverState *bs)
865 {
866 BDRVNVMeState *s = bs->opaque;
867 assert(s->blkshift >= BDRV_SECTOR_BITS && s->blkshift <= 12);
868 return UINT32_C(1) << s->blkshift;
869 }
870
871 static int nvme_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
872 {
873 uint32_t blocksize = nvme_get_blocksize(bs);
874 bsz->phys = blocksize;
875 bsz->log = blocksize;
876 return 0;
877 }
878
879 /* Called with s->dma_map_lock */
880 static coroutine_fn int nvme_cmd_unmap_qiov(BlockDriverState *bs,
881 QEMUIOVector *qiov)
882 {
883 int r = 0;
884 BDRVNVMeState *s = bs->opaque;
885
886 s->dma_map_count -= qiov->size;
887 if (!s->dma_map_count && !qemu_co_queue_empty(&s->dma_flush_queue)) {
888 r = qemu_vfio_dma_reset_temporary(s->vfio);
889 if (!r) {
890 qemu_co_queue_restart_all(&s->dma_flush_queue);
891 }
892 }
893 return r;
894 }
895
896 /* Called with s->dma_map_lock */
897 static coroutine_fn int nvme_cmd_map_qiov(BlockDriverState *bs, NvmeCmd *cmd,
898 NVMeRequest *req, QEMUIOVector *qiov)
899 {
900 BDRVNVMeState *s = bs->opaque;
901 uint64_t *pagelist = req->prp_list_page;
902 int i, j, r;
903 int entries = 0;
904
905 assert(qiov->size);
906 assert(QEMU_IS_ALIGNED(qiov->size, s->page_size));
907 assert(qiov->size / s->page_size <= s->page_size / sizeof(uint64_t));
908 for (i = 0; i < qiov->niov; ++i) {
909 bool retry = true;
910 uint64_t iova;
911 try_map:
912 r = qemu_vfio_dma_map(s->vfio,
913 qiov->iov[i].iov_base,
914 qiov->iov[i].iov_len,
915 true, &iova);
916 if (r == -ENOMEM && retry) {
917 retry = false;
918 trace_nvme_dma_flush_queue_wait(s);
919 if (s->dma_map_count) {
920 trace_nvme_dma_map_flush(s);
921 qemu_co_queue_wait(&s->dma_flush_queue, &s->dma_map_lock);
922 } else {
923 r = qemu_vfio_dma_reset_temporary(s->vfio);
924 if (r) {
925 goto fail;
926 }
927 }
928 goto try_map;
929 }
930 if (r) {
931 goto fail;
932 }
933
934 for (j = 0; j < qiov->iov[i].iov_len / s->page_size; j++) {
935 pagelist[entries++] = cpu_to_le64(iova + j * s->page_size);
936 }
937 trace_nvme_cmd_map_qiov_iov(s, i, qiov->iov[i].iov_base,
938 qiov->iov[i].iov_len / s->page_size);
939 }
940
941 s->dma_map_count += qiov->size;
942
943 assert(entries <= s->page_size / sizeof(uint64_t));
944 switch (entries) {
945 case 0:
946 abort();
947 case 1:
948 cmd->prp1 = pagelist[0];
949 cmd->prp2 = 0;
950 break;
951 case 2:
952 cmd->prp1 = pagelist[0];
953 cmd->prp2 = pagelist[1];
954 break;
955 default:
956 cmd->prp1 = pagelist[0];
957 cmd->prp2 = cpu_to_le64(req->prp_list_iova + sizeof(uint64_t));
958 break;
959 }
960 trace_nvme_cmd_map_qiov(s, cmd, req, qiov, entries);
961 for (i = 0; i < entries; ++i) {
962 trace_nvme_cmd_map_qiov_pages(s, i, pagelist[i]);
963 }
964 return 0;
965 fail:
966 /* No need to unmap [0 - i) iovs even if we've failed, since we don't
967 * increment s->dma_map_count. This is okay for fixed mapping memory areas
968 * because they are already mapped before calling this function; for
969 * temporary mappings, a later nvme_cmd_(un)map_qiov will reclaim by
970 * calling qemu_vfio_dma_reset_temporary when necessary. */
971 return r;
972 }
973
974 typedef struct {
975 Coroutine *co;
976 int ret;
977 AioContext *ctx;
978 } NVMeCoData;
979
980 static void nvme_rw_cb_bh(void *opaque)
981 {
982 NVMeCoData *data = opaque;
983 qemu_coroutine_enter(data->co);
984 }
985
986 static void nvme_rw_cb(void *opaque, int ret)
987 {
988 NVMeCoData *data = opaque;
989 data->ret = ret;
990 if (!data->co) {
991 /* The rw coroutine hasn't yielded, don't try to enter. */
992 return;
993 }
994 replay_bh_schedule_oneshot_event(data->ctx, nvme_rw_cb_bh, data);
995 }
996
997 static coroutine_fn int nvme_co_prw_aligned(BlockDriverState *bs,
998 uint64_t offset, uint64_t bytes,
999 QEMUIOVector *qiov,
1000 bool is_write,
1001 int flags)
1002 {
1003 int r;
1004 BDRVNVMeState *s = bs->opaque;
1005 NVMeQueuePair *ioq = s->queues[1];
1006 NVMeRequest *req;
1007
1008 uint32_t cdw12 = (((bytes >> s->blkshift) - 1) & 0xFFFF) |
1009 (flags & BDRV_REQ_FUA ? 1 << 30 : 0);
1010 NvmeCmd cmd = {
1011 .opcode = is_write ? NVME_CMD_WRITE : NVME_CMD_READ,
1012 .nsid = cpu_to_le32(s->nsid),
1013 .cdw10 = cpu_to_le32((offset >> s->blkshift) & 0xFFFFFFFF),
1014 .cdw11 = cpu_to_le32(((offset >> s->blkshift) >> 32) & 0xFFFFFFFF),
1015 .cdw12 = cpu_to_le32(cdw12),
1016 };
1017 NVMeCoData data = {
1018 .ctx = bdrv_get_aio_context(bs),
1019 .ret = -EINPROGRESS,
1020 };
1021
1022 trace_nvme_prw_aligned(s, is_write, offset, bytes, flags, qiov->niov);
1023 assert(s->nr_queues > 1);
1024 req = nvme_get_free_req(ioq);
1025 assert(req);
1026
1027 qemu_co_mutex_lock(&s->dma_map_lock);
1028 r = nvme_cmd_map_qiov(bs, &cmd, req, qiov);
1029 qemu_co_mutex_unlock(&s->dma_map_lock);
1030 if (r) {
1031 nvme_put_free_req_and_wake(s, ioq, req);
1032 return r;
1033 }
1034 nvme_submit_command(s, ioq, req, &cmd, nvme_rw_cb, &data);
1035
1036 data.co = qemu_coroutine_self();
1037 while (data.ret == -EINPROGRESS) {
1038 qemu_coroutine_yield();
1039 }
1040
1041 qemu_co_mutex_lock(&s->dma_map_lock);
1042 r = nvme_cmd_unmap_qiov(bs, qiov);
1043 qemu_co_mutex_unlock(&s->dma_map_lock);
1044 if (r) {
1045 return r;
1046 }
1047
1048 trace_nvme_rw_done(s, is_write, offset, bytes, data.ret);
1049 return data.ret;
1050 }
1051
1052 static inline bool nvme_qiov_aligned(BlockDriverState *bs,
1053 const QEMUIOVector *qiov)
1054 {
1055 int i;
1056 BDRVNVMeState *s = bs->opaque;
1057
1058 for (i = 0; i < qiov->niov; ++i) {
1059 if (!QEMU_PTR_IS_ALIGNED(qiov->iov[i].iov_base, s->page_size) ||
1060 !QEMU_IS_ALIGNED(qiov->iov[i].iov_len, s->page_size)) {
1061 trace_nvme_qiov_unaligned(qiov, i, qiov->iov[i].iov_base,
1062 qiov->iov[i].iov_len, s->page_size);
1063 return false;
1064 }
1065 }
1066 return true;
1067 }
1068
1069 static int nvme_co_prw(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
1070 QEMUIOVector *qiov, bool is_write, int flags)
1071 {
1072 BDRVNVMeState *s = bs->opaque;
1073 int r;
1074 uint8_t *buf = NULL;
1075 QEMUIOVector local_qiov;
1076
1077 assert(QEMU_IS_ALIGNED(offset, s->page_size));
1078 assert(QEMU_IS_ALIGNED(bytes, s->page_size));
1079 assert(bytes <= s->max_transfer);
1080 if (nvme_qiov_aligned(bs, qiov)) {
1081 return nvme_co_prw_aligned(bs, offset, bytes, qiov, is_write, flags);
1082 }
1083 trace_nvme_prw_buffered(s, offset, bytes, qiov->niov, is_write);
1084 buf = qemu_try_blockalign(bs, bytes);
1085
1086 if (!buf) {
1087 return -ENOMEM;
1088 }
1089 qemu_iovec_init(&local_qiov, 1);
1090 if (is_write) {
1091 qemu_iovec_to_buf(qiov, 0, buf, bytes);
1092 }
1093 qemu_iovec_add(&local_qiov, buf, bytes);
1094 r = nvme_co_prw_aligned(bs, offset, bytes, &local_qiov, is_write, flags);
1095 qemu_iovec_destroy(&local_qiov);
1096 if (!r && !is_write) {
1097 qemu_iovec_from_buf(qiov, 0, buf, bytes);
1098 }
1099 qemu_vfree(buf);
1100 return r;
1101 }
1102
1103 static coroutine_fn int nvme_co_preadv(BlockDriverState *bs,
1104 uint64_t offset, uint64_t bytes,
1105 QEMUIOVector *qiov, int flags)
1106 {
1107 return nvme_co_prw(bs, offset, bytes, qiov, false, flags);
1108 }
1109
1110 static coroutine_fn int nvme_co_pwritev(BlockDriverState *bs,
1111 uint64_t offset, uint64_t bytes,
1112 QEMUIOVector *qiov, int flags)
1113 {
1114 return nvme_co_prw(bs, offset, bytes, qiov, true, flags);
1115 }
1116
1117 static coroutine_fn int nvme_co_flush(BlockDriverState *bs)
1118 {
1119 BDRVNVMeState *s = bs->opaque;
1120 NVMeQueuePair *ioq = s->queues[1];
1121 NVMeRequest *req;
1122 NvmeCmd cmd = {
1123 .opcode = NVME_CMD_FLUSH,
1124 .nsid = cpu_to_le32(s->nsid),
1125 };
1126 NVMeCoData data = {
1127 .ctx = bdrv_get_aio_context(bs),
1128 .ret = -EINPROGRESS,
1129 };
1130
1131 assert(s->nr_queues > 1);
1132 req = nvme_get_free_req(ioq);
1133 assert(req);
1134 nvme_submit_command(s, ioq, req, &cmd, nvme_rw_cb, &data);
1135
1136 data.co = qemu_coroutine_self();
1137 if (data.ret == -EINPROGRESS) {
1138 qemu_coroutine_yield();
1139 }
1140
1141 return data.ret;
1142 }
1143
1144
1145 static coroutine_fn int nvme_co_pwrite_zeroes(BlockDriverState *bs,
1146 int64_t offset,
1147 int bytes,
1148 BdrvRequestFlags flags)
1149 {
1150 BDRVNVMeState *s = bs->opaque;
1151 NVMeQueuePair *ioq = s->queues[1];
1152 NVMeRequest *req;
1153
1154 uint32_t cdw12 = ((bytes >> s->blkshift) - 1) & 0xFFFF;
1155
1156 if (!s->supports_write_zeroes) {
1157 return -ENOTSUP;
1158 }
1159
1160 NvmeCmd cmd = {
1161 .opcode = NVME_CMD_WRITE_ZEROS,
1162 .nsid = cpu_to_le32(s->nsid),
1163 .cdw10 = cpu_to_le32((offset >> s->blkshift) & 0xFFFFFFFF),
1164 .cdw11 = cpu_to_le32(((offset >> s->blkshift) >> 32) & 0xFFFFFFFF),
1165 };
1166
1167 NVMeCoData data = {
1168 .ctx = bdrv_get_aio_context(bs),
1169 .ret = -EINPROGRESS,
1170 };
1171
1172 if (flags & BDRV_REQ_MAY_UNMAP) {
1173 cdw12 |= (1 << 25);
1174 }
1175
1176 if (flags & BDRV_REQ_FUA) {
1177 cdw12 |= (1 << 30);
1178 }
1179
1180 cmd.cdw12 = cpu_to_le32(cdw12);
1181
1182 trace_nvme_write_zeroes(s, offset, bytes, flags);
1183 assert(s->nr_queues > 1);
1184 req = nvme_get_free_req(ioq);
1185 assert(req);
1186
1187 nvme_submit_command(s, ioq, req, &cmd, nvme_rw_cb, &data);
1188
1189 data.co = qemu_coroutine_self();
1190 while (data.ret == -EINPROGRESS) {
1191 qemu_coroutine_yield();
1192 }
1193
1194 trace_nvme_rw_done(s, true, offset, bytes, data.ret);
1195 return data.ret;
1196 }
1197
1198
1199 static int coroutine_fn nvme_co_pdiscard(BlockDriverState *bs,
1200 int64_t offset,
1201 int bytes)
1202 {
1203 BDRVNVMeState *s = bs->opaque;
1204 NVMeQueuePair *ioq = s->queues[1];
1205 NVMeRequest *req;
1206 NvmeDsmRange *buf;
1207 QEMUIOVector local_qiov;
1208 int ret;
1209
1210 NvmeCmd cmd = {
1211 .opcode = NVME_CMD_DSM,
1212 .nsid = cpu_to_le32(s->nsid),
1213 .cdw10 = cpu_to_le32(0), /*number of ranges - 0 based*/
1214 .cdw11 = cpu_to_le32(1 << 2), /*deallocate bit*/
1215 };
1216
1217 NVMeCoData data = {
1218 .ctx = bdrv_get_aio_context(bs),
1219 .ret = -EINPROGRESS,
1220 };
1221
1222 if (!s->supports_discard) {
1223 return -ENOTSUP;
1224 }
1225
1226 assert(s->nr_queues > 1);
1227
1228 buf = qemu_try_blockalign0(bs, s->page_size);
1229 if (!buf) {
1230 return -ENOMEM;
1231 }
1232
1233 buf->nlb = cpu_to_le32(bytes >> s->blkshift);
1234 buf->slba = cpu_to_le64(offset >> s->blkshift);
1235 buf->cattr = 0;
1236
1237 qemu_iovec_init(&local_qiov, 1);
1238 qemu_iovec_add(&local_qiov, buf, 4096);
1239
1240 req = nvme_get_free_req(ioq);
1241 assert(req);
1242
1243 qemu_co_mutex_lock(&s->dma_map_lock);
1244 ret = nvme_cmd_map_qiov(bs, &cmd, req, &local_qiov);
1245 qemu_co_mutex_unlock(&s->dma_map_lock);
1246
1247 if (ret) {
1248 nvme_put_free_req_and_wake(s, ioq, req);
1249 goto out;
1250 }
1251
1252 trace_nvme_dsm(s, offset, bytes);
1253
1254 nvme_submit_command(s, ioq, req, &cmd, nvme_rw_cb, &data);
1255
1256 data.co = qemu_coroutine_self();
1257 while (data.ret == -EINPROGRESS) {
1258 qemu_coroutine_yield();
1259 }
1260
1261 qemu_co_mutex_lock(&s->dma_map_lock);
1262 ret = nvme_cmd_unmap_qiov(bs, &local_qiov);
1263 qemu_co_mutex_unlock(&s->dma_map_lock);
1264
1265 if (ret) {
1266 goto out;
1267 }
1268
1269 ret = data.ret;
1270 trace_nvme_dsm_done(s, offset, bytes, ret);
1271 out:
1272 qemu_iovec_destroy(&local_qiov);
1273 qemu_vfree(buf);
1274 return ret;
1275
1276 }
1277
1278
1279 static int nvme_reopen_prepare(BDRVReopenState *reopen_state,
1280 BlockReopenQueue *queue, Error **errp)
1281 {
1282 return 0;
1283 }
1284
1285 static void nvme_refresh_filename(BlockDriverState *bs)
1286 {
1287 BDRVNVMeState *s = bs->opaque;
1288
1289 snprintf(bs->exact_filename, sizeof(bs->exact_filename), "nvme://%s/%i",
1290 s->device, s->nsid);
1291 }
1292
1293 static void nvme_refresh_limits(BlockDriverState *bs, Error **errp)
1294 {
1295 BDRVNVMeState *s = bs->opaque;
1296
1297 bs->bl.opt_mem_alignment = s->page_size;
1298 bs->bl.request_alignment = s->page_size;
1299 bs->bl.max_transfer = s->max_transfer;
1300 }
1301
1302 static void nvme_detach_aio_context(BlockDriverState *bs)
1303 {
1304 BDRVNVMeState *s = bs->opaque;
1305
1306 aio_set_event_notifier(bdrv_get_aio_context(bs), &s->irq_notifier,
1307 false, NULL, NULL);
1308 }
1309
1310 static void nvme_attach_aio_context(BlockDriverState *bs,
1311 AioContext *new_context)
1312 {
1313 BDRVNVMeState *s = bs->opaque;
1314
1315 s->aio_context = new_context;
1316 aio_set_event_notifier(new_context, &s->irq_notifier,
1317 false, nvme_handle_event, nvme_poll_cb);
1318 }
1319
1320 static void nvme_aio_plug(BlockDriverState *bs)
1321 {
1322 BDRVNVMeState *s = bs->opaque;
1323 assert(!s->plugged);
1324 s->plugged = true;
1325 }
1326
1327 static void nvme_aio_unplug(BlockDriverState *bs)
1328 {
1329 int i;
1330 BDRVNVMeState *s = bs->opaque;
1331 assert(s->plugged);
1332 s->plugged = false;
1333 for (i = 1; i < s->nr_queues; i++) {
1334 NVMeQueuePair *q = s->queues[i];
1335 qemu_mutex_lock(&q->lock);
1336 nvme_kick(s, q);
1337 nvme_process_completion(s, q);
1338 qemu_mutex_unlock(&q->lock);
1339 }
1340 }
1341
1342 static void nvme_register_buf(BlockDriverState *bs, void *host, size_t size)
1343 {
1344 int ret;
1345 BDRVNVMeState *s = bs->opaque;
1346
1347 ret = qemu_vfio_dma_map(s->vfio, host, size, false, NULL);
1348 if (ret) {
1349 /* FIXME: we may run out of IOVA addresses after repeated
1350 * bdrv_register_buf/bdrv_unregister_buf, because nvme_vfio_dma_unmap
1351 * doesn't reclaim addresses for fixed mappings. */
1352 error_report("nvme_register_buf failed: %s", strerror(-ret));
1353 }
1354 }
1355
1356 static void nvme_unregister_buf(BlockDriverState *bs, void *host)
1357 {
1358 BDRVNVMeState *s = bs->opaque;
1359
1360 qemu_vfio_dma_unmap(s->vfio, host);
1361 }
1362
1363 static const char *const nvme_strong_runtime_opts[] = {
1364 NVME_BLOCK_OPT_DEVICE,
1365 NVME_BLOCK_OPT_NAMESPACE,
1366
1367 NULL
1368 };
1369
1370 static BlockDriver bdrv_nvme = {
1371 .format_name = "nvme",
1372 .protocol_name = "nvme",
1373 .instance_size = sizeof(BDRVNVMeState),
1374
1375 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
1376 .create_opts = &bdrv_create_opts_simple,
1377
1378 .bdrv_parse_filename = nvme_parse_filename,
1379 .bdrv_file_open = nvme_file_open,
1380 .bdrv_close = nvme_close,
1381 .bdrv_getlength = nvme_getlength,
1382 .bdrv_probe_blocksizes = nvme_probe_blocksizes,
1383
1384 .bdrv_co_preadv = nvme_co_preadv,
1385 .bdrv_co_pwritev = nvme_co_pwritev,
1386
1387 .bdrv_co_pwrite_zeroes = nvme_co_pwrite_zeroes,
1388 .bdrv_co_pdiscard = nvme_co_pdiscard,
1389
1390 .bdrv_co_flush_to_disk = nvme_co_flush,
1391 .bdrv_reopen_prepare = nvme_reopen_prepare,
1392
1393 .bdrv_refresh_filename = nvme_refresh_filename,
1394 .bdrv_refresh_limits = nvme_refresh_limits,
1395 .strong_runtime_opts = nvme_strong_runtime_opts,
1396
1397 .bdrv_detach_aio_context = nvme_detach_aio_context,
1398 .bdrv_attach_aio_context = nvme_attach_aio_context,
1399
1400 .bdrv_io_plug = nvme_aio_plug,
1401 .bdrv_io_unplug = nvme_aio_unplug,
1402
1403 .bdrv_register_buf = nvme_register_buf,
1404 .bdrv_unregister_buf = nvme_unregister_buf,
1405 };
1406
1407 static void bdrv_nvme_init(void)
1408 {
1409 bdrv_register(&bdrv_nvme);
1410 }
1411
1412 block_init(bdrv_nvme_init);