2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Shunsuke Mie
5 * Copyright (c) 2018 Leon Dang
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * bhyve PCIe-NVMe device emulation.
33 * -s <n>,nvme,devpath,maxq=#,qsz=#,ioslots=#,sectsz=#,ser=A-Z
40 * maxq = max number of queues
41 * qsz = max elements in each queue
42 * ioslots = max number of concurrent io requests
43 * sectsz = sector size (defaults to blockif sector size)
44 * ser = serial number (20-chars max)
49 - create async event for smart and log
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #include <sys/types.h>
60 #include <semaphore.h>
68 #include <machine/atomic.h>
69 #include <machine/vmm.h>
72 #include <dev/nvme/nvme.h>
79 static int nvme_debug = 0;
80 #define DPRINTF(params) if (nvme_debug) printf params
81 #define WPRINTF(params) printf params
83 /* defaults; can be overridden */
84 #define NVME_MSIX_BAR 4
86 #define NVME_IOSLOTS 8
88 #define NVME_QUEUES 16
89 #define NVME_MAX_QENTRIES 2048
91 #define NVME_PRP2_ITEMS (PAGE_SIZE/sizeof(uint64_t))
92 #define NVME_MAX_BLOCKIOVS 512
96 #define NVME_DOORBELL_OFFSET offsetof(struct nvme_registers, doorbell)
98 enum nvme_controller_register_offsets {
99 NVME_CR_CAP_LOW = 0x00,
100 NVME_CR_CAP_HI = 0x04,
102 NVME_CR_INTMS = 0x0c,
103 NVME_CR_INTMC = 0x10,
108 NVME_CR_ASQ_LOW = 0x28,
109 NVME_CR_ASQ_HI = 0x2c,
110 NVME_CR_ACQ_LOW = 0x30,
111 NVME_CR_ACQ_HI = 0x34,
114 enum nvme_cmd_cdw11 {
115 NVME_CMD_CDW11_PC = 0x0001,
116 NVME_CMD_CDW11_IEN = 0x0002,
117 NVME_CMD_CDW11_IV = 0xFFFF0000,
120 #define NVME_CQ_INTEN 0x01
121 #define NVME_CQ_INTCOAL 0x02
123 struct nvme_completion_queue {
124 struct nvme_completion *qbase;
126 uint16_t tail; /* nvme progress */
127 uint16_t head; /* guest progress */
133 struct nvme_submission_queue {
134 struct nvme_command *qbase;
136 uint16_t head; /* nvme progress */
137 uint16_t tail; /* guest progress */
138 uint16_t cqid; /* completion queue id */
139 int busy; /* queue is being processed */
143 enum nvme_storage_type {
144 NVME_STOR_BLOCKIF = 0,
148 struct pci_nvme_blockstore {
149 enum nvme_storage_type type;
153 uint32_t sectsz_bits;
156 struct pci_nvme_ioreq {
157 struct pci_nvme_softc *sc;
158 struct pci_nvme_ioreq *next;
159 struct nvme_submission_queue *nvme_sq;
162 /* command information */
167 uint64_t prev_gpaddr;
171 * lock if all iovs consumed (big IO);
172 * complete transaction before continuing
177 struct blockif_req io_req;
179 /* pad to fit up to 512 page descriptors from guest IO request */
180 struct iovec iovpadding[NVME_MAX_BLOCKIOVS-BLOCKIF_IOV_MAX];
183 struct pci_nvme_softc {
184 struct pci_devinst *nsc_pi;
188 struct nvme_registers regs;
190 struct nvme_namespace_data nsdata;
191 struct nvme_controller_data ctrldata;
193 struct pci_nvme_blockstore nvstore;
195 uint16_t max_qentries; /* max entries per queue */
197 uint32_t num_cqueues;
198 uint32_t num_squeues;
200 struct pci_nvme_ioreq *ioreqs;
201 struct pci_nvme_ioreq *ioreqs_free; /* free list of ioreqs */
202 uint32_t pending_ios;
206 /* status and guest memory mapped queues */
207 struct nvme_completion_queue *compl_queues;
208 struct nvme_submission_queue *submit_queues;
210 /* controller features */
211 uint32_t intr_coales_aggr_time; /* 0x08: uS to delay intr */
212 uint32_t intr_coales_aggr_thresh; /* 0x08: compl-Q entries */
213 uint32_t async_ev_config; /* 0x0B: async event config */
217 static void pci_nvme_io_partial(struct blockif_req *br, int err);
219 /* Controller Configuration utils */
220 #define NVME_CC_GET_EN(cc) \
221 ((cc) >> NVME_CC_REG_EN_SHIFT & NVME_CC_REG_EN_MASK)
222 #define NVME_CC_GET_CSS(cc) \
223 ((cc) >> NVME_CC_REG_CSS_SHIFT & NVME_CC_REG_CSS_MASK)
224 #define NVME_CC_GET_SHN(cc) \
225 ((cc) >> NVME_CC_REG_SHN_SHIFT & NVME_CC_REG_SHN_MASK)
226 #define NVME_CC_GET_IOSQES(cc) \
227 ((cc) >> NVME_CC_REG_IOSQES_SHIFT & NVME_CC_REG_IOSQES_MASK)
228 #define NVME_CC_GET_IOCQES(cc) \
229 ((cc) >> NVME_CC_REG_IOCQES_SHIFT & NVME_CC_REG_IOCQES_MASK)
231 #define NVME_CC_WRITE_MASK \
232 ((NVME_CC_REG_EN_MASK << NVME_CC_REG_EN_SHIFT) | \
233 (NVME_CC_REG_IOSQES_MASK << NVME_CC_REG_IOSQES_SHIFT) | \
234 (NVME_CC_REG_IOCQES_MASK << NVME_CC_REG_IOCQES_SHIFT))
236 #define NVME_CC_NEN_WRITE_MASK \
237 ((NVME_CC_REG_CSS_MASK << NVME_CC_REG_CSS_SHIFT) | \
238 (NVME_CC_REG_MPS_MASK << NVME_CC_REG_MPS_SHIFT) | \
239 (NVME_CC_REG_AMS_MASK << NVME_CC_REG_AMS_SHIFT))
241 /* Controller Status utils */
242 #define NVME_CSTS_GET_RDY(sts) \
243 ((sts) >> NVME_CSTS_REG_RDY_SHIFT & NVME_CSTS_REG_RDY_MASK)
245 #define NVME_CSTS_RDY (1 << NVME_CSTS_REG_RDY_SHIFT)
247 /* Completion Queue status word utils */
248 #define NVME_STATUS_P (1 << NVME_STATUS_P_SHIFT)
249 #define NVME_STATUS_MASK \
250 ((NVME_STATUS_SCT_MASK << NVME_STATUS_SCT_SHIFT) |\
251 (NVME_STATUS_SC_MASK << NVME_STATUS_SC_SHIFT))
254 cpywithpad(char *dst, int dst_size, const char *src, char pad)
256 int len = strnlen(src, dst_size);
257 memcpy(dst, src, len);
258 memset(dst + len, pad, dst_size - len);
262 pci_nvme_status_tc(uint16_t *status, uint16_t type, uint16_t code)
265 *status &= ~NVME_STATUS_MASK;
266 *status |= (type & NVME_STATUS_SCT_MASK) << NVME_STATUS_SCT_SHIFT |
267 (code & NVME_STATUS_SC_MASK) << NVME_STATUS_SC_SHIFT;
271 pci_nvme_status_genc(uint16_t *status, uint16_t code)
274 pci_nvme_status_tc(status, NVME_SCT_GENERIC, code);
278 pci_nvme_toggle_phase(uint16_t *status, int prev)
282 *status &= ~NVME_STATUS_P;
284 *status |= NVME_STATUS_P;
288 pci_nvme_init_ctrldata(struct pci_nvme_softc *sc)
290 struct nvme_controller_data *cd = &sc->ctrldata;
295 cpywithpad((char *)cd->mn, sizeof(cd->mn), "bhyve-NVMe", ' ');
296 cpywithpad((char *)cd->fr, sizeof(cd->fr), "1.0", ' ');
298 /* Num of submission commands that we can handle at a time (2^rab) */
308 cd->mdts = 9; /* max data transfer size (2^mdts * CAP.MPSMIN) */
310 cd->ver = 0x00010300;
312 cd->oacs = 1 << NVME_CTRLR_DATA_OACS_FORMAT_SHIFT;
316 cd->lpa = 0; /* TODO: support some simple things like SMART */
317 cd->elpe = 0; /* max error log page entries */
318 cd->npss = 1; /* number of power states support */
320 /* Warning Composite Temperature Threshold */
323 cd->sqes = (6 << NVME_CTRLR_DATA_SQES_MAX_SHIFT) |
324 (6 << NVME_CTRLR_DATA_SQES_MIN_SHIFT);
325 cd->cqes = (4 << NVME_CTRLR_DATA_CQES_MAX_SHIFT) |
326 (4 << NVME_CTRLR_DATA_CQES_MIN_SHIFT);
327 cd->nn = 1; /* number of namespaces */
331 cd->power_state[0].mp = 10;
335 pci_nvme_init_nsdata(struct pci_nvme_softc *sc)
337 struct nvme_namespace_data *nd;
341 nd->nsze = sc->nvstore.size / sc->nvstore.sectsz;
345 /* Get LBA and backstore information from backing store */
347 /* LBA data-sz = 2^lbads */
348 nd->lbaf[0] = sc->nvstore.sectsz_bits << NVME_NS_DATA_LBAF_LBADS_SHIFT;
354 pci_nvme_reset_locked(struct pci_nvme_softc *sc)
356 DPRINTF(("%s\r\n", __func__));
358 sc->regs.cap_lo = (sc->max_qentries & NVME_CAP_LO_REG_MQES_MASK) |
359 (1 << NVME_CAP_LO_REG_CQR_SHIFT) |
360 (60 << NVME_CAP_LO_REG_TO_SHIFT);
362 sc->regs.cap_hi = 1 << NVME_CAP_HI_REG_CSS_NVM_SHIFT;
364 sc->regs.vs = 0x00010300; /* NVMe v1.3 */
369 sc->num_cqueues = sc->num_squeues = sc->max_queues;
370 if (sc->submit_queues != NULL) {
371 for (int i = 0; i <= sc->max_queues; i++) {
373 * The Admin Submission Queue is at index 0.
374 * It must not be changed at reset otherwise the
375 * emulation will be out of sync with the guest.
378 sc->submit_queues[i].qbase = NULL;
379 sc->submit_queues[i].size = 0;
380 sc->submit_queues[i].cqid = 0;
382 sc->compl_queues[i].qbase = NULL;
383 sc->compl_queues[i].size = 0;
385 sc->submit_queues[i].tail = 0;
386 sc->submit_queues[i].head = 0;
387 sc->submit_queues[i].busy = 0;
389 sc->compl_queues[i].tail = 0;
390 sc->compl_queues[i].head = 0;
393 sc->submit_queues = calloc(sc->max_queues + 1,
394 sizeof(struct nvme_submission_queue));
396 if (sc->compl_queues == NULL) {
397 sc->compl_queues = calloc(sc->max_queues + 1,
398 sizeof(struct nvme_completion_queue));
400 for (int i = 0; i <= sc->num_cqueues; i++)
401 pthread_mutex_init(&sc->compl_queues[i].mtx, NULL);
406 pci_nvme_reset(struct pci_nvme_softc *sc)
408 pthread_mutex_lock(&sc->mtx);
409 pci_nvme_reset_locked(sc);
410 pthread_mutex_unlock(&sc->mtx);
414 pci_nvme_init_controller(struct vmctx *ctx, struct pci_nvme_softc *sc)
418 DPRINTF(("%s\r\n", __func__));
420 asqs = (sc->regs.aqa & NVME_AQA_REG_ASQS_MASK) + 1;
421 sc->submit_queues[0].size = asqs;
422 sc->submit_queues[0].qbase = vm_map_gpa(ctx, sc->regs.asq,
423 sizeof(struct nvme_command) * asqs);
425 DPRINTF(("%s mapping Admin-SQ guest 0x%lx, host: %p\r\n",
426 __func__, sc->regs.asq, sc->submit_queues[0].qbase));
428 acqs = ((sc->regs.aqa >> NVME_AQA_REG_ACQS_SHIFT) &
429 NVME_AQA_REG_ACQS_MASK) + 1;
430 sc->compl_queues[0].size = acqs;
431 sc->compl_queues[0].qbase = vm_map_gpa(ctx, sc->regs.acq,
432 sizeof(struct nvme_completion) * acqs);
433 DPRINTF(("%s mapping Admin-CQ guest 0x%lx, host: %p\r\n",
434 __func__, sc->regs.acq, sc->compl_queues[0].qbase));
438 nvme_opc_delete_io_sq(struct pci_nvme_softc* sc, struct nvme_command* command,
439 struct nvme_completion* compl)
441 uint16_t qid = command->cdw10 & 0xffff;
443 DPRINTF(("%s DELETE_IO_SQ %u\r\n", __func__, qid));
444 if (qid == 0 || qid > sc->num_cqueues) {
445 WPRINTF(("%s NOT PERMITTED queue id %u / num_squeues %u\r\n",
446 __func__, qid, sc->num_squeues));
447 pci_nvme_status_tc(&compl->status, NVME_SCT_COMMAND_SPECIFIC,
448 NVME_SC_INVALID_QUEUE_IDENTIFIER);
452 sc->submit_queues[qid].qbase = NULL;
453 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
458 nvme_opc_create_io_sq(struct pci_nvme_softc* sc, struct nvme_command* command,
459 struct nvme_completion* compl)
461 if (command->cdw11 & NVME_CMD_CDW11_PC) {
462 uint16_t qid = command->cdw10 & 0xffff;
463 struct nvme_submission_queue *nsq;
465 if (qid > sc->num_squeues) {
466 WPRINTF(("%s queue index %u > num_squeues %u\r\n",
467 __func__, qid, sc->num_squeues));
468 pci_nvme_status_tc(&compl->status,
469 NVME_SCT_COMMAND_SPECIFIC,
470 NVME_SC_INVALID_QUEUE_IDENTIFIER);
474 nsq = &sc->submit_queues[qid];
475 nsq->size = ((command->cdw10 >> 16) & 0xffff) + 1;
477 nsq->qbase = vm_map_gpa(sc->nsc_pi->pi_vmctx, command->prp1,
478 sizeof(struct nvme_command) * (size_t)nsq->size);
479 nsq->cqid = (command->cdw11 >> 16) & 0xffff;
480 nsq->qpriority = (command->cdw11 >> 1) & 0x03;
482 DPRINTF(("%s sq %u size %u gaddr %p cqid %u\r\n", __func__,
483 qid, nsq->size, nsq->qbase, nsq->cqid));
485 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
487 DPRINTF(("%s completed creating IOSQ qid %u\r\n",
491 * Guest sent non-cont submission queue request.
492 * This setting is unsupported by this emulation.
494 WPRINTF(("%s unsupported non-contig (list-based) "
495 "create i/o submission queue\r\n", __func__));
497 pci_nvme_status_genc(&compl->status, NVME_SC_INVALID_FIELD);
503 nvme_opc_delete_io_cq(struct pci_nvme_softc* sc, struct nvme_command* command,
504 struct nvme_completion* compl)
506 uint16_t qid = command->cdw10 & 0xffff;
508 DPRINTF(("%s DELETE_IO_CQ %u\r\n", __func__, qid));
509 if (qid == 0 || qid > sc->num_cqueues) {
510 WPRINTF(("%s queue index %u / num_cqueues %u\r\n",
511 __func__, qid, sc->num_cqueues));
512 pci_nvme_status_tc(&compl->status, NVME_SCT_COMMAND_SPECIFIC,
513 NVME_SC_INVALID_QUEUE_IDENTIFIER);
517 sc->compl_queues[qid].qbase = NULL;
518 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
523 nvme_opc_create_io_cq(struct pci_nvme_softc* sc, struct nvme_command* command,
524 struct nvme_completion* compl)
526 if (command->cdw11 & NVME_CMD_CDW11_PC) {
527 uint16_t qid = command->cdw10 & 0xffff;
528 struct nvme_completion_queue *ncq;
530 if (qid > sc->num_cqueues) {
531 WPRINTF(("%s queue index %u > num_cqueues %u\r\n",
532 __func__, qid, sc->num_cqueues));
533 pci_nvme_status_tc(&compl->status,
534 NVME_SCT_COMMAND_SPECIFIC,
535 NVME_SC_INVALID_QUEUE_IDENTIFIER);
539 ncq = &sc->compl_queues[qid];
540 ncq->intr_en = (command->cdw11 & NVME_CMD_CDW11_IEN) >> 1;
541 ncq->intr_vec = (command->cdw11 >> 16) & 0xffff;
542 ncq->size = ((command->cdw10 >> 16) & 0xffff) + 1;
544 ncq->qbase = vm_map_gpa(sc->nsc_pi->pi_vmctx,
546 sizeof(struct nvme_command) * (size_t)ncq->size);
548 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
551 * Non-contig completion queue unsupported.
553 WPRINTF(("%s unsupported non-contig (list-based) "
554 "create i/o completion queue\r\n",
557 /* 0x12 = Invalid Use of Controller Memory Buffer */
558 pci_nvme_status_genc(&compl->status, 0x12);
565 nvme_opc_get_log_page(struct pci_nvme_softc* sc, struct nvme_command* command,
566 struct nvme_completion* compl)
568 uint32_t logsize = (1 + ((command->cdw10 >> 16) & 0xFFF)) * 2;
569 uint8_t logpage = command->cdw10 & 0xFF;
572 DPRINTF(("%s log page %u len %u\r\n", __func__, logpage, logsize));
574 if (logpage >= 1 && logpage <= 3)
575 data = vm_map_gpa(sc->nsc_pi->pi_vmctx, command->prp1,
578 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
581 case 0x01: /* Error information */
582 memset(data, 0, logsize > PAGE_SIZE ? PAGE_SIZE : logsize);
584 case 0x02: /* SMART/Health information */
585 /* TODO: present some smart info */
586 memset(data, 0, logsize > PAGE_SIZE ? PAGE_SIZE : logsize);
588 case 0x03: /* Firmware slot information */
589 memset(data, 0, logsize > PAGE_SIZE ? PAGE_SIZE : logsize);
592 WPRINTF(("%s get log page %x command not supported\r\n",
595 pci_nvme_status_tc(&compl->status, NVME_SCT_COMMAND_SPECIFIC,
596 NVME_SC_INVALID_LOG_PAGE);
603 nvme_opc_identify(struct pci_nvme_softc* sc, struct nvme_command* command,
604 struct nvme_completion* compl)
608 DPRINTF(("%s identify 0x%x nsid 0x%x\r\n", __func__,
609 command->cdw10 & 0xFF, command->nsid));
611 switch (command->cdw10 & 0xFF) {
612 case 0x00: /* return Identify Namespace data structure */
613 dest = vm_map_gpa(sc->nsc_pi->pi_vmctx, command->prp1,
615 memcpy(dest, &sc->nsdata, sizeof(sc->nsdata));
617 case 0x01: /* return Identify Controller data structure */
618 dest = vm_map_gpa(sc->nsc_pi->pi_vmctx, command->prp1,
619 sizeof(sc->ctrldata));
620 memcpy(dest, &sc->ctrldata, sizeof(sc->ctrldata));
622 case 0x02: /* list of 1024 active NSIDs > CDW1.NSID */
623 dest = vm_map_gpa(sc->nsc_pi->pi_vmctx, command->prp1,
624 sizeof(uint32_t) * 1024);
625 ((uint32_t *)dest)[0] = 1;
626 ((uint32_t *)dest)[1] = 0;
629 pci_nvme_status_genc(&compl->status,
630 NVME_SC_INVALID_NAMESPACE_OR_FORMAT);
632 case 0x03: /* list of NSID structures in CDW1.NSID, 4096 bytes */
639 DPRINTF(("%s unsupported identify command requested 0x%x\r\n",
640 __func__, command->cdw10 & 0xFF));
641 pci_nvme_status_genc(&compl->status, NVME_SC_INVALID_FIELD);
645 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
650 nvme_opc_set_features(struct pci_nvme_softc* sc, struct nvme_command* command,
651 struct nvme_completion* compl)
653 int feature = command->cdw10 & 0xFF;
656 DPRINTF(("%s feature 0x%x\r\n", __func__, feature));
660 case NVME_FEAT_ARBITRATION:
661 DPRINTF((" arbitration 0x%x\r\n", command->cdw11));
663 case NVME_FEAT_POWER_MANAGEMENT:
664 DPRINTF((" power management 0x%x\r\n", command->cdw11));
666 case NVME_FEAT_LBA_RANGE_TYPE:
667 DPRINTF((" lba range 0x%x\r\n", command->cdw11));
669 case NVME_FEAT_TEMPERATURE_THRESHOLD:
670 DPRINTF((" temperature threshold 0x%x\r\n", command->cdw11));
672 case NVME_FEAT_ERROR_RECOVERY:
673 DPRINTF((" error recovery 0x%x\r\n", command->cdw11));
675 case NVME_FEAT_VOLATILE_WRITE_CACHE:
676 DPRINTF((" volatile write cache 0x%x\r\n", command->cdw11));
678 case NVME_FEAT_NUMBER_OF_QUEUES:
679 sc->num_squeues = command->cdw11 & 0xFFFF;
680 sc->num_cqueues = (command->cdw11 >> 16) & 0xFFFF;
681 DPRINTF((" number of queues (submit %u, completion %u)\r\n",
682 sc->num_squeues, sc->num_cqueues));
684 if (sc->num_squeues == 0 || sc->num_squeues > sc->max_queues)
685 sc->num_squeues = sc->max_queues;
686 if (sc->num_cqueues == 0 || sc->num_cqueues > sc->max_queues)
687 sc->num_cqueues = sc->max_queues;
689 compl->cdw0 = (sc->num_squeues & 0xFFFF) |
690 ((sc->num_cqueues & 0xFFFF) << 16);
693 case NVME_FEAT_INTERRUPT_COALESCING:
694 DPRINTF((" interrupt coalescing 0x%x\r\n", command->cdw11));
697 sc->intr_coales_aggr_time = ((command->cdw11 >> 8) & 0xFF)*100;
699 sc->intr_coales_aggr_thresh = command->cdw11 & 0xFF;
701 case NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION:
702 iv = command->cdw11 & 0xFFFF;
704 DPRINTF((" interrupt vector configuration 0x%x\r\n",
707 for (uint32_t i = 0; i <= sc->num_cqueues; i++) {
708 if (sc->compl_queues[i].intr_vec == iv) {
709 if (command->cdw11 & (1 << 16))
710 sc->compl_queues[i].intr_en |=
713 sc->compl_queues[i].intr_en &=
718 case NVME_FEAT_WRITE_ATOMICITY:
719 DPRINTF((" write atomicity 0x%x\r\n", command->cdw11));
721 case NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
722 DPRINTF((" async event configuration 0x%x\r\n",
724 sc->async_ev_config = command->cdw11;
726 case NVME_FEAT_SOFTWARE_PROGRESS_MARKER:
727 DPRINTF((" software progress marker 0x%x\r\n",
731 DPRINTF((" autonomous power state transition 0x%x\r\n",
735 WPRINTF(("%s invalid feature\r\n", __func__));
736 pci_nvme_status_genc(&compl->status, NVME_SC_INVALID_FIELD);
740 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
745 nvme_opc_get_features(struct pci_nvme_softc* sc, struct nvme_command* command,
746 struct nvme_completion* compl)
748 int feature = command->cdw10 & 0xFF;
750 DPRINTF(("%s feature 0x%x\r\n", __func__, feature));
755 case NVME_FEAT_ARBITRATION:
756 DPRINTF((" arbitration\r\n"));
758 case NVME_FEAT_POWER_MANAGEMENT:
759 DPRINTF((" power management\r\n"));
761 case NVME_FEAT_LBA_RANGE_TYPE:
762 DPRINTF((" lba range\r\n"));
764 case NVME_FEAT_TEMPERATURE_THRESHOLD:
765 DPRINTF((" temperature threshold\r\n"));
766 switch ((command->cdw11 >> 20) & 0x3) {
768 /* Over temp threshold */
769 compl->cdw0 = 0xFFFF;
772 /* Under temp threshold */
776 WPRINTF((" invalid threshold type select\r\n"));
777 pci_nvme_status_genc(&compl->status,
778 NVME_SC_INVALID_FIELD);
782 case NVME_FEAT_ERROR_RECOVERY:
783 DPRINTF((" error recovery\r\n"));
785 case NVME_FEAT_VOLATILE_WRITE_CACHE:
786 DPRINTF((" volatile write cache\r\n"));
788 case NVME_FEAT_NUMBER_OF_QUEUES:
790 if (sc->num_squeues == 0)
791 compl->cdw0 |= sc->max_queues & 0xFFFF;
793 compl->cdw0 |= sc->num_squeues & 0xFFFF;
795 if (sc->num_cqueues == 0)
796 compl->cdw0 |= (sc->max_queues & 0xFFFF) << 16;
798 compl->cdw0 |= (sc->num_cqueues & 0xFFFF) << 16;
800 DPRINTF((" number of queues (submit %u, completion %u)\r\n",
801 compl->cdw0 & 0xFFFF,
802 (compl->cdw0 >> 16) & 0xFFFF));
805 case NVME_FEAT_INTERRUPT_COALESCING:
806 DPRINTF((" interrupt coalescing\r\n"));
808 case NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION:
809 DPRINTF((" interrupt vector configuration\r\n"));
811 case NVME_FEAT_WRITE_ATOMICITY:
812 DPRINTF((" write atomicity\r\n"));
814 case NVME_FEAT_ASYNC_EVENT_CONFIGURATION:
815 DPRINTF((" async event configuration\r\n"));
816 sc->async_ev_config = command->cdw11;
818 case NVME_FEAT_SOFTWARE_PROGRESS_MARKER:
819 DPRINTF((" software progress marker\r\n"));
822 DPRINTF((" autonomous power state transition\r\n"));
825 WPRINTF(("%s invalid feature 0x%x\r\n", __func__, feature));
826 pci_nvme_status_genc(&compl->status, NVME_SC_INVALID_FIELD);
830 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
835 nvme_opc_abort(struct pci_nvme_softc* sc, struct nvme_command* command,
836 struct nvme_completion* compl)
838 DPRINTF(("%s submission queue %u, command ID 0x%x\r\n", __func__,
839 command->cdw10 & 0xFFFF, (command->cdw10 >> 16) & 0xFFFF));
841 /* TODO: search for the command ID and abort it */
844 pci_nvme_status_genc(&compl->status, NVME_SC_SUCCESS);
849 nvme_opc_async_event_req(struct pci_nvme_softc* sc,
850 struct nvme_command* command, struct nvme_completion* compl)
852 DPRINTF(("%s async event request 0x%x\r\n", __func__, command->cdw11));
855 * TODO: raise events when they happen based on the Set Features cmd.
856 * These events happen async, so only set completion successful if
857 * there is an event reflective of the request to get event.
859 pci_nvme_status_tc(&compl->status, NVME_SCT_COMMAND_SPECIFIC,
860 NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED);
865 pci_nvme_handle_admin_cmd(struct pci_nvme_softc* sc, uint64_t value)
867 struct nvme_completion compl;
868 struct nvme_command *cmd;
869 struct nvme_submission_queue *sq;
870 struct nvme_completion_queue *cq;
874 DPRINTF(("%s index %u\r\n", __func__, (uint32_t)value));
876 sq = &sc->submit_queues[0];
878 sqhead = atomic_load_acq_short(&sq->head);
880 if (atomic_testandset_int(&sq->busy, 1)) {
881 DPRINTF(("%s SQ busy, head %u, tail %u\r\n",
882 __func__, sqhead, sq->tail));
886 DPRINTF(("sqhead %u, tail %u\r\n", sqhead, sq->tail));
888 while (sqhead != atomic_load_acq_short(&sq->tail)) {
889 cmd = &(sq->qbase)[sqhead];
893 case NVME_OPC_DELETE_IO_SQ:
894 DPRINTF(("%s command DELETE_IO_SQ\r\n", __func__));
895 do_intr |= nvme_opc_delete_io_sq(sc, cmd, &compl);
897 case NVME_OPC_CREATE_IO_SQ:
898 DPRINTF(("%s command CREATE_IO_SQ\r\n", __func__));
899 do_intr |= nvme_opc_create_io_sq(sc, cmd, &compl);
901 case NVME_OPC_DELETE_IO_CQ:
902 DPRINTF(("%s command DELETE_IO_CQ\r\n", __func__));
903 do_intr |= nvme_opc_delete_io_cq(sc, cmd, &compl);
905 case NVME_OPC_CREATE_IO_CQ:
906 DPRINTF(("%s command CREATE_IO_CQ\r\n", __func__));
907 do_intr |= nvme_opc_create_io_cq(sc, cmd, &compl);
909 case NVME_OPC_GET_LOG_PAGE:
910 DPRINTF(("%s command GET_LOG_PAGE\r\n", __func__));
911 do_intr |= nvme_opc_get_log_page(sc, cmd, &compl);
913 case NVME_OPC_IDENTIFY:
914 DPRINTF(("%s command IDENTIFY\r\n", __func__));
915 do_intr |= nvme_opc_identify(sc, cmd, &compl);
918 DPRINTF(("%s command ABORT\r\n", __func__));
919 do_intr |= nvme_opc_abort(sc, cmd, &compl);
921 case NVME_OPC_SET_FEATURES:
922 DPRINTF(("%s command SET_FEATURES\r\n", __func__));
923 do_intr |= nvme_opc_set_features(sc, cmd, &compl);
925 case NVME_OPC_GET_FEATURES:
926 DPRINTF(("%s command GET_FEATURES\r\n", __func__));
927 do_intr |= nvme_opc_get_features(sc, cmd, &compl);
929 case NVME_OPC_ASYNC_EVENT_REQUEST:
930 DPRINTF(("%s command ASYNC_EVENT_REQ\r\n", __func__));
931 /* XXX dont care, unhandled for now
932 do_intr |= nvme_opc_async_event_req(sc, cmd, &compl);
936 WPRINTF(("0x%x command is not implemented\r\n",
940 /* for now skip async event generation */
941 if (cmd->opc != NVME_OPC_ASYNC_EVENT_REQUEST) {
942 struct nvme_completion *cp;
945 cq = &sc->compl_queues[0];
947 cp = &(cq->qbase)[cq->tail];
952 phase = NVME_STATUS_GET_P(cp->status);
953 cp->status = compl.status;
954 pci_nvme_toggle_phase(&cp->status, phase);
956 cq->tail = (cq->tail + 1) % cq->size;
958 sqhead = (sqhead + 1) % sq->size;
961 DPRINTF(("setting sqhead %u\r\n", sqhead));
962 atomic_store_short(&sq->head, sqhead);
963 atomic_store_int(&sq->busy, 0);
966 pci_generate_msix(sc->nsc_pi, 0);
971 pci_nvme_append_iov_req(struct pci_nvme_softc *sc, struct pci_nvme_ioreq *req,
972 uint64_t gpaddr, size_t size, int do_write, uint64_t lba)
977 /* concatenate contig block-iovs to minimize number of iovs */
978 if ((req->prev_gpaddr + req->prev_size) == gpaddr) {
979 iovidx = req->io_req.br_iovcnt - 1;
981 req->io_req.br_iov[iovidx].iov_base =
982 paddr_guest2host(req->sc->nsc_pi->pi_vmctx,
983 req->prev_gpaddr, size);
985 req->prev_size += size;
986 req->io_req.br_resid += size;
988 req->io_req.br_iov[iovidx].iov_len = req->prev_size;
990 pthread_mutex_lock(&req->mtx);
992 iovidx = req->io_req.br_iovcnt;
993 if (iovidx == NVME_MAX_BLOCKIOVS) {
996 DPRINTF(("large I/O, doing partial req\r\n"));
999 req->io_req.br_iovcnt = 0;
1001 req->io_req.br_callback = pci_nvme_io_partial;
1004 err = blockif_read(sc->nvstore.ctx,
1007 err = blockif_write(sc->nvstore.ctx,
1010 /* wait until req completes before cont */
1012 pthread_cond_wait(&req->cv, &req->mtx);
1015 req->io_req.br_offset = lba;
1016 req->io_req.br_resid = 0;
1017 req->io_req.br_param = req;
1020 req->io_req.br_iov[iovidx].iov_base =
1021 paddr_guest2host(req->sc->nsc_pi->pi_vmctx,
1024 req->io_req.br_iov[iovidx].iov_len = size;
1026 req->prev_gpaddr = gpaddr;
1027 req->prev_size = size;
1028 req->io_req.br_resid += size;
1030 req->io_req.br_iovcnt++;
1032 pthread_mutex_unlock(&req->mtx);
1035 /* RAM buffer: read/write directly */
1036 void *p = sc->nvstore.ctx;
1039 if ((lba + size) > sc->nvstore.size) {
1040 WPRINTF(("%s write would overflow RAM\r\n", __func__));
1044 p = (void *)((uintptr_t)p + (uintptr_t)lba);
1045 gptr = paddr_guest2host(sc->nsc_pi->pi_vmctx, gpaddr, size);
1047 memcpy(p, gptr, size);
1049 memcpy(gptr, p, size);
1055 pci_nvme_set_completion(struct pci_nvme_softc *sc,
1056 struct nvme_submission_queue *sq, int sqid, uint16_t cid,
1057 uint32_t cdw0, uint16_t status, int ignore_busy)
1059 struct nvme_completion_queue *cq = &sc->compl_queues[sq->cqid];
1060 struct nvme_completion *compl;
1064 DPRINTF(("%s sqid %d cqid %u cid %u status: 0x%x 0x%x\r\n",
1065 __func__, sqid, sq->cqid, cid, NVME_STATUS_GET_SCT(status),
1066 NVME_STATUS_GET_SC(status)));
1068 pthread_mutex_lock(&cq->mtx);
1070 assert(cq->qbase != NULL);
1072 compl = &cq->qbase[cq->tail];
1074 compl->sqhd = atomic_load_acq_short(&sq->head);
1079 phase = NVME_STATUS_GET_P(compl->status);
1080 compl->status = status;
1081 pci_nvme_toggle_phase(&compl->status, phase);
1083 cq->tail = (cq->tail + 1) % cq->size;
1085 if (cq->intr_en & NVME_CQ_INTEN)
1088 pthread_mutex_unlock(&cq->mtx);
1090 if (ignore_busy || !atomic_load_acq_int(&sq->busy))
1092 pci_generate_msix(sc->nsc_pi, cq->intr_vec);
1096 pci_nvme_release_ioreq(struct pci_nvme_softc *sc, struct pci_nvme_ioreq *req)
1099 req->nvme_sq = NULL;
1102 pthread_mutex_lock(&sc->mtx);
1104 req->next = sc->ioreqs_free;
1105 sc->ioreqs_free = req;
1108 /* when no more IO pending, can set to ready if device reset/enabled */
1109 if (sc->pending_ios == 0 &&
1110 NVME_CC_GET_EN(sc->regs.cc) && !(NVME_CSTS_GET_RDY(sc->regs.csts)))
1111 sc->regs.csts |= NVME_CSTS_RDY;
1113 pthread_mutex_unlock(&sc->mtx);
1115 sem_post(&sc->iosemlock);
1118 static struct pci_nvme_ioreq *
1119 pci_nvme_get_ioreq(struct pci_nvme_softc *sc)
1121 struct pci_nvme_ioreq *req = NULL;;
1123 sem_wait(&sc->iosemlock);
1124 pthread_mutex_lock(&sc->mtx);
1126 req = sc->ioreqs_free;
1127 assert(req != NULL);
1129 sc->ioreqs_free = req->next;
1136 pthread_mutex_unlock(&sc->mtx);
1138 req->io_req.br_iovcnt = 0;
1139 req->io_req.br_offset = 0;
1140 req->io_req.br_resid = 0;
1141 req->io_req.br_param = req;
1142 req->prev_gpaddr = 0;
1149 pci_nvme_io_done(struct blockif_req *br, int err)
1151 struct pci_nvme_ioreq *req = br->br_param;
1152 struct nvme_submission_queue *sq = req->nvme_sq;
1153 uint16_t code, status;
1155 DPRINTF(("%s error %d %s\r\n", __func__, err, strerror(err)));
1157 /* TODO return correct error */
1158 code = err ? NVME_SC_DATA_TRANSFER_ERROR : NVME_SC_SUCCESS;
1159 pci_nvme_status_genc(&status, code);
1161 pci_nvme_set_completion(req->sc, sq, req->sqid, req->cid, 0, status, 0);
1162 pci_nvme_release_ioreq(req->sc, req);
1166 pci_nvme_io_partial(struct blockif_req *br, int err)
1168 struct pci_nvme_ioreq *req = br->br_param;
1170 DPRINTF(("%s error %d %s\r\n", __func__, err, strerror(err)));
1172 pthread_cond_signal(&req->cv);
1177 pci_nvme_handle_io_cmd(struct pci_nvme_softc* sc, uint16_t idx)
1179 struct nvme_submission_queue *sq;
1184 /* handle all submissions up to sq->tail index */
1185 sq = &sc->submit_queues[idx];
1187 if (atomic_testandset_int(&sq->busy, 1)) {
1188 DPRINTF(("%s sqid %u busy\r\n", __func__, idx));
1192 sqhead = atomic_load_acq_short(&sq->head);
1194 DPRINTF(("nvme_handle_io qid %u head %u tail %u cmdlist %p\r\n",
1195 idx, sqhead, sq->tail, sq->qbase));
1197 while (sqhead != atomic_load_acq_short(&sq->tail)) {
1198 struct nvme_command *cmd;
1199 struct pci_nvme_ioreq *req = NULL;
1201 uint64_t nblocks, bytes, size, cpsz;
1203 /* TODO: support scatter gather list handling */
1205 cmd = &sq->qbase[sqhead];
1206 sqhead = (sqhead + 1) % sq->size;
1208 lba = ((uint64_t)cmd->cdw11 << 32) | cmd->cdw10;
1210 if (cmd->opc == NVME_OPC_FLUSH) {
1211 pci_nvme_status_genc(&status, NVME_SC_SUCCESS);
1212 pci_nvme_set_completion(sc, sq, idx, cmd->cid, 0,
1216 } else if (cmd->opc == 0x08) {
1217 /* TODO: write zeroes */
1218 WPRINTF(("%s write zeroes lba 0x%lx blocks %u\r\n",
1219 __func__, lba, cmd->cdw12 & 0xFFFF));
1220 pci_nvme_status_genc(&status, NVME_SC_SUCCESS);
1221 pci_nvme_set_completion(sc, sq, idx, cmd->cid, 0,
1227 nblocks = (cmd->cdw12 & 0xFFFF) + 1;
1229 bytes = nblocks * sc->nvstore.sectsz;
1231 if (sc->nvstore.type == NVME_STOR_BLOCKIF) {
1232 req = pci_nvme_get_ioreq(sc);
1238 * If data starts mid-page and flows into the next page, then
1239 * increase page count
1242 DPRINTF(("[h%u:t%u:n%u] %s starting LBA 0x%lx blocks %lu "
1244 sqhead==0 ? sq->size-1 : sqhead-1, sq->tail, sq->size,
1245 cmd->opc == NVME_OPC_WRITE ?
1247 lba, nblocks, bytes));
1249 cmd->prp1 &= ~(0x03UL);
1250 cmd->prp2 &= ~(0x03UL);
1252 DPRINTF((" prp1 0x%lx prp2 0x%lx\r\n", cmd->prp1, cmd->prp2));
1255 lba *= sc->nvstore.sectsz;
1257 cpsz = PAGE_SIZE - (cmd->prp1 % PAGE_SIZE);
1263 req->io_req.br_offset = ((uint64_t)cmd->cdw11 << 32) |
1265 req->opc = cmd->opc;
1266 req->cid = cmd->cid;
1267 req->nsid = cmd->nsid;
1270 err = pci_nvme_append_iov_req(sc, req, cmd->prp1, cpsz,
1271 cmd->opc == NVME_OPC_WRITE, lba);
1278 if (size <= PAGE_SIZE) {
1279 /* prp2 is second (and final) page in transfer */
1281 err = pci_nvme_append_iov_req(sc, req, cmd->prp2,
1283 cmd->opc == NVME_OPC_WRITE,
1289 /* prp2 is pointer to a physical region page list */
1290 prp_list = paddr_guest2host(sc->nsc_pi->pi_vmctx,
1291 cmd->prp2, PAGE_SIZE);
1295 cpsz = MIN(size, PAGE_SIZE);
1298 * Move to linked physical region page list
1301 if (i == (NVME_PRP2_ITEMS-1) &&
1303 assert((prp_list[i] & (PAGE_SIZE-1)) == 0);
1304 prp_list = paddr_guest2host(
1305 sc->nsc_pi->pi_vmctx,
1306 prp_list[i], PAGE_SIZE);
1309 if (prp_list[i] == 0) {
1310 WPRINTF(("PRP2[%d] = 0 !!!\r\n", i));
1315 err = pci_nvme_append_iov_req(sc, req,
1317 cmd->opc == NVME_OPC_WRITE, lba);
1328 if (sc->nvstore.type == NVME_STOR_RAM) {
1329 uint16_t code, status;
1331 code = err ? NVME_SC_LBA_OUT_OF_RANGE :
1333 pci_nvme_status_genc(&status, code);
1335 pci_nvme_set_completion(sc, sq, idx, cmd->cid, 0,
1345 req->io_req.br_callback = pci_nvme_io_done;
1350 err = blockif_read(sc->nvstore.ctx, &req->io_req);
1352 case NVME_OPC_WRITE:
1353 err = blockif_write(sc->nvstore.ctx, &req->io_req);
1356 WPRINTF(("%s unhandled io command 0x%x\r\n",
1357 __func__, cmd->opc));
1365 pci_nvme_status_genc(&status,
1366 NVME_SC_DATA_TRANSFER_ERROR);
1368 pci_nvme_set_completion(sc, sq, idx, cmd->cid, 0,
1370 pci_nvme_release_ioreq(sc, req);
1374 atomic_store_short(&sq->head, sqhead);
1375 atomic_store_int(&sq->busy, 0);
1379 pci_nvme_handle_doorbell(struct vmctx *ctx, struct pci_nvme_softc* sc,
1380 uint64_t idx, int is_sq, uint64_t value)
1382 DPRINTF(("nvme doorbell %lu, %s, val 0x%lx\r\n",
1383 idx, is_sq ? "SQ" : "CQ", value & 0xFFFF));
1386 atomic_store_short(&sc->submit_queues[idx].tail,
1390 pci_nvme_handle_admin_cmd(sc, value);
1392 /* submission queue; handle new entries in SQ */
1393 if (idx > sc->num_squeues) {
1394 WPRINTF(("%s SQ index %lu overflow from "
1395 "guest (max %u)\r\n",
1396 __func__, idx, sc->num_squeues));
1399 pci_nvme_handle_io_cmd(sc, (uint16_t)idx);
1402 if (idx > sc->num_cqueues) {
1403 WPRINTF(("%s queue index %lu overflow from "
1404 "guest (max %u)\r\n",
1405 __func__, idx, sc->num_cqueues));
1409 sc->compl_queues[idx].head = (uint16_t)value;
1414 pci_nvme_bar0_reg_dumps(const char *func, uint64_t offset, int iswrite)
1416 const char *s = iswrite ? "WRITE" : "READ";
1419 case NVME_CR_CAP_LOW:
1420 DPRINTF(("%s %s NVME_CR_CAP_LOW\r\n", func, s));
1422 case NVME_CR_CAP_HI:
1423 DPRINTF(("%s %s NVME_CR_CAP_HI\r\n", func, s));
1426 DPRINTF(("%s %s NVME_CR_VS\r\n", func, s));
1429 DPRINTF(("%s %s NVME_CR_INTMS\r\n", func, s));
1432 DPRINTF(("%s %s NVME_CR_INTMC\r\n", func, s));
1435 DPRINTF(("%s %s NVME_CR_CC\r\n", func, s));
1438 DPRINTF(("%s %s NVME_CR_CSTS\r\n", func, s));
1441 DPRINTF(("%s %s NVME_CR_NSSR\r\n", func, s));
1444 DPRINTF(("%s %s NVME_CR_AQA\r\n", func, s));
1446 case NVME_CR_ASQ_LOW:
1447 DPRINTF(("%s %s NVME_CR_ASQ_LOW\r\n", func, s));
1449 case NVME_CR_ASQ_HI:
1450 DPRINTF(("%s %s NVME_CR_ASQ_HI\r\n", func, s));
1452 case NVME_CR_ACQ_LOW:
1453 DPRINTF(("%s %s NVME_CR_ACQ_LOW\r\n", func, s));
1455 case NVME_CR_ACQ_HI:
1456 DPRINTF(("%s %s NVME_CR_ACQ_HI\r\n", func, s));
1459 DPRINTF(("unknown nvme bar-0 offset 0x%lx\r\n", offset));
1465 pci_nvme_write_bar_0(struct vmctx *ctx, struct pci_nvme_softc* sc,
1466 uint64_t offset, int size, uint64_t value)
1470 if (offset >= NVME_DOORBELL_OFFSET) {
1471 uint64_t belloffset = offset - NVME_DOORBELL_OFFSET;
1472 uint64_t idx = belloffset / 8; /* door bell size = 2*int */
1473 int is_sq = (belloffset % 8) < 4;
1475 if (belloffset > ((sc->max_queues+1) * 8 - 4)) {
1476 WPRINTF(("guest attempted an overflow write offset "
1477 "0x%lx, val 0x%lx in %s",
1478 offset, value, __func__));
1482 pci_nvme_handle_doorbell(ctx, sc, idx, is_sq, value);
1486 DPRINTF(("nvme-write offset 0x%lx, size %d, value 0x%lx\r\n",
1487 offset, size, value));
1490 WPRINTF(("guest wrote invalid size %d (offset 0x%lx, "
1491 "val 0x%lx) to bar0 in %s",
1492 size, offset, value, __func__));
1493 /* TODO: shutdown device */
1497 pci_nvme_bar0_reg_dumps(__func__, offset, 1);
1499 pthread_mutex_lock(&sc->mtx);
1502 case NVME_CR_CAP_LOW:
1503 case NVME_CR_CAP_HI:
1510 /* MSI-X, so ignore */
1513 /* MSI-X, so ignore */
1516 ccreg = (uint32_t)value;
1518 DPRINTF(("%s NVME_CR_CC en %x css %x shn %x iosqes %u "
1521 NVME_CC_GET_EN(ccreg), NVME_CC_GET_CSS(ccreg),
1522 NVME_CC_GET_SHN(ccreg), NVME_CC_GET_IOSQES(ccreg),
1523 NVME_CC_GET_IOCQES(ccreg)));
1525 if (NVME_CC_GET_SHN(ccreg)) {
1526 /* perform shutdown - flush out data to backend */
1527 sc->regs.csts &= ~(NVME_CSTS_REG_SHST_MASK <<
1528 NVME_CSTS_REG_SHST_SHIFT);
1529 sc->regs.csts |= NVME_SHST_COMPLETE <<
1530 NVME_CSTS_REG_SHST_SHIFT;
1532 if (NVME_CC_GET_EN(ccreg) != NVME_CC_GET_EN(sc->regs.cc)) {
1533 if (NVME_CC_GET_EN(ccreg) == 0)
1534 /* transition 1-> causes controller reset */
1535 pci_nvme_reset_locked(sc);
1537 pci_nvme_init_controller(ctx, sc);
1540 /* Insert the iocqes, iosqes and en bits from the write */
1541 sc->regs.cc &= ~NVME_CC_WRITE_MASK;
1542 sc->regs.cc |= ccreg & NVME_CC_WRITE_MASK;
1543 if (NVME_CC_GET_EN(ccreg) == 0) {
1544 /* Insert the ams, mps and css bit fields */
1545 sc->regs.cc &= ~NVME_CC_NEN_WRITE_MASK;
1546 sc->regs.cc |= ccreg & NVME_CC_NEN_WRITE_MASK;
1547 sc->regs.csts &= ~NVME_CSTS_RDY;
1548 } else if (sc->pending_ios == 0) {
1549 sc->regs.csts |= NVME_CSTS_RDY;
1555 /* ignore writes; don't support subsystem reset */
1558 sc->regs.aqa = (uint32_t)value;
1560 case NVME_CR_ASQ_LOW:
1561 sc->regs.asq = (sc->regs.asq & (0xFFFFFFFF00000000)) |
1562 (0xFFFFF000 & value);
1564 case NVME_CR_ASQ_HI:
1565 sc->regs.asq = (sc->regs.asq & (0x00000000FFFFFFFF)) |
1568 case NVME_CR_ACQ_LOW:
1569 sc->regs.acq = (sc->regs.acq & (0xFFFFFFFF00000000)) |
1570 (0xFFFFF000 & value);
1572 case NVME_CR_ACQ_HI:
1573 sc->regs.acq = (sc->regs.acq & (0x00000000FFFFFFFF)) |
1577 DPRINTF(("%s unknown offset 0x%lx, value 0x%lx size %d\r\n",
1578 __func__, offset, value, size));
1580 pthread_mutex_unlock(&sc->mtx);
1584 pci_nvme_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
1585 int baridx, uint64_t offset, int size, uint64_t value)
1587 struct pci_nvme_softc* sc = pi->pi_arg;
1589 if (baridx == pci_msix_table_bar(pi) ||
1590 baridx == pci_msix_pba_bar(pi)) {
1591 DPRINTF(("nvme-write baridx %d, msix: off 0x%lx, size %d, "
1592 " value 0x%lx\r\n", baridx, offset, size, value));
1594 pci_emul_msix_twrite(pi, offset, size, value);
1600 pci_nvme_write_bar_0(ctx, sc, offset, size, value);
1604 DPRINTF(("%s unknown baridx %d, val 0x%lx\r\n",
1605 __func__, baridx, value));
1609 static uint64_t pci_nvme_read_bar_0(struct pci_nvme_softc* sc,
1610 uint64_t offset, int size)
1614 pci_nvme_bar0_reg_dumps(__func__, offset, 0);
1616 if (offset < NVME_DOORBELL_OFFSET) {
1617 void *p = &(sc->regs);
1618 pthread_mutex_lock(&sc->mtx);
1619 memcpy(&value, (void *)((uintptr_t)p + offset), size);
1620 pthread_mutex_unlock(&sc->mtx);
1623 WPRINTF(("pci_nvme: read invalid offset %ld\r\n", offset));
1634 value &= 0xFFFFFFFF;
1638 DPRINTF((" nvme-read offset 0x%lx, size %d -> value 0x%x\r\n",
1639 offset, size, (uint32_t)value));
1647 pci_nvme_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
1648 uint64_t offset, int size)
1650 struct pci_nvme_softc* sc = pi->pi_arg;
1652 if (baridx == pci_msix_table_bar(pi) ||
1653 baridx == pci_msix_pba_bar(pi)) {
1654 DPRINTF(("nvme-read bar: %d, msix: regoff 0x%lx, size %d\r\n",
1655 baridx, offset, size));
1657 return pci_emul_msix_tread(pi, offset, size);
1662 return pci_nvme_read_bar_0(sc, offset, size);
1665 DPRINTF(("unknown bar %d, 0x%lx\r\n", baridx, offset));
1673 pci_nvme_parse_opts(struct pci_nvme_softc *sc, char *opts)
1675 char bident[sizeof("XX:X:X")];
1676 char *uopt, *xopts, *config;
1680 sc->max_queues = NVME_QUEUES;
1681 sc->max_qentries = NVME_MAX_QENTRIES;
1682 sc->ioslots = NVME_IOSLOTS;
1683 sc->num_squeues = sc->max_queues;
1684 sc->num_cqueues = sc->max_queues;
1687 uopt = strdup(opts);
1689 snprintf(sc->ctrldata.sn, sizeof(sc->ctrldata.sn),
1690 "NVME-%d-%d", sc->nsc_pi->pi_slot, sc->nsc_pi->pi_func);
1691 for (xopts = strtok(uopt, ",");
1693 xopts = strtok(NULL, ",")) {
1695 if ((config = strchr(xopts, '=')) != NULL)
1698 if (!strcmp("maxq", xopts)) {
1699 sc->max_queues = atoi(config);
1700 } else if (!strcmp("qsz", xopts)) {
1701 sc->max_qentries = atoi(config);
1702 } else if (!strcmp("ioslots", xopts)) {
1703 sc->ioslots = atoi(config);
1704 } else if (!strcmp("sectsz", xopts)) {
1705 sectsz = atoi(config);
1706 } else if (!strcmp("ser", xopts)) {
1708 * This field indicates the Product Serial Number in
1709 * 7-bit ASCII, unused bytes should be space characters.
1712 cpywithpad((char *)sc->ctrldata.sn,
1713 sizeof(sc->ctrldata.sn), config, ' ');
1714 } else if (!strcmp("ram", xopts)) {
1715 uint64_t sz = strtoull(&xopts[4], NULL, 10);
1717 sc->nvstore.type = NVME_STOR_RAM;
1718 sc->nvstore.size = sz * 1024 * 1024;
1719 sc->nvstore.ctx = calloc(1, sc->nvstore.size);
1720 sc->nvstore.sectsz = 4096;
1721 sc->nvstore.sectsz_bits = 12;
1722 if (sc->nvstore.ctx == NULL) {
1723 perror("Unable to allocate RAM");
1727 } else if (optidx == 0) {
1728 snprintf(bident, sizeof(bident), "%d:%d",
1729 sc->nsc_pi->pi_slot, sc->nsc_pi->pi_func);
1730 sc->nvstore.ctx = blockif_open(xopts, bident);
1731 if (sc->nvstore.ctx == NULL) {
1732 perror("Could not open backing file");
1736 sc->nvstore.type = NVME_STOR_BLOCKIF;
1737 sc->nvstore.size = blockif_size(sc->nvstore.ctx);
1739 fprintf(stderr, "Invalid option %s\n", xopts);
1748 if (sc->nvstore.ctx == NULL || sc->nvstore.size == 0) {
1749 fprintf(stderr, "backing store not specified\n");
1752 if (sectsz == 512 || sectsz == 4096 || sectsz == 8192)
1753 sc->nvstore.sectsz = sectsz;
1754 else if (sc->nvstore.type != NVME_STOR_RAM)
1755 sc->nvstore.sectsz = blockif_sectsz(sc->nvstore.ctx);
1756 for (sc->nvstore.sectsz_bits = 9;
1757 (1 << sc->nvstore.sectsz_bits) < sc->nvstore.sectsz;
1758 sc->nvstore.sectsz_bits++);
1760 if (sc->max_queues <= 0 || sc->max_queues > NVME_QUEUES)
1761 sc->max_queues = NVME_QUEUES;
1763 if (sc->max_qentries <= 0) {
1764 fprintf(stderr, "Invalid qsz option\n");
1767 if (sc->ioslots <= 0) {
1768 fprintf(stderr, "Invalid ioslots option\n");
1776 pci_nvme_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
1778 struct pci_nvme_softc *sc;
1779 uint32_t pci_membar_sz;
1784 sc = calloc(1, sizeof(struct pci_nvme_softc));
1788 error = pci_nvme_parse_opts(sc, opts);
1794 sc->ioreqs = calloc(sc->ioslots, sizeof(struct pci_nvme_ioreq));
1795 for (int i = 0; i < sc->ioslots; i++) {
1796 if (i < (sc->ioslots-1))
1797 sc->ioreqs[i].next = &sc->ioreqs[i+1];
1798 pthread_mutex_init(&sc->ioreqs[i].mtx, NULL);
1799 pthread_cond_init(&sc->ioreqs[i].cv, NULL);
1801 sc->ioreqs_free = sc->ioreqs;
1802 sc->intr_coales_aggr_thresh = 1;
1804 pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0A0A);
1805 pci_set_cfgdata16(pi, PCIR_VENDOR, 0xFB5D);
1806 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
1807 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_STORAGE_NVM);
1808 pci_set_cfgdata8(pi, PCIR_PROGIF,
1809 PCIP_STORAGE_NVM_ENTERPRISE_NVMHCI_1_0);
1811 /* allocate size of nvme registers + doorbell space for all queues */
1812 pci_membar_sz = sizeof(struct nvme_registers) +
1813 2*sizeof(uint32_t)*(sc->max_queues);
1815 DPRINTF(("nvme membar size: %u\r\n", pci_membar_sz));
1817 error = pci_emul_alloc_bar(pi, 0, PCIBAR_MEM64, pci_membar_sz);
1819 WPRINTF(("%s pci alloc mem bar failed\r\n", __func__));
1823 error = pci_emul_add_msixcap(pi, sc->max_queues, NVME_MSIX_BAR);
1825 WPRINTF(("%s pci add msixcap failed\r\n", __func__));
1829 pthread_mutex_init(&sc->mtx, NULL);
1830 sem_init(&sc->iosemlock, 0, sc->ioslots);
1833 pci_nvme_init_ctrldata(sc);
1834 pci_nvme_init_nsdata(sc);
1836 pci_lintr_request(pi);
1843 struct pci_devemu pci_de_nvme = {
1845 .pe_init = pci_nvme_init,
1846 .pe_barwrite = pci_nvme_write,
1847 .pe_barread = pci_nvme_read
1849 PCI_EMUL_SET(pci_de_nvme);