2 * QEMU NVM Express Controller
4 * Copyright (c) 2012, Intel Corporation
8 * This code is licensed under the GNU GPL v2 or later.
12 * Reference Specs: http://www.nvmexpress.org, 1.1, 1.0e
14 * http://www.nvmexpress.org/resources/
19 * -drive file=<file>,if=none,id=<drive_id>
20 * -device nvme,drive=<drive_id>,serial=<serial>,id=<id[optional]>
23 #include <hw/block/block.h>
25 #include <hw/pci/msix.h>
26 #include <hw/pci/pci.h>
27 #include "sysemu/sysemu.h"
28 #include "qapi/visitor.h"
29 #include "sysemu/block-backend.h"
33 static void nvme_process_sq(void *opaque);
35 static int nvme_check_sqid(NvmeCtrl *n, uint16_t sqid)
37 return sqid < n->num_queues && n->sq[sqid] != NULL ? 0 : -1;
40 static int nvme_check_cqid(NvmeCtrl *n, uint16_t cqid)
42 return cqid < n->num_queues && n->cq[cqid] != NULL ? 0 : -1;
45 static void nvme_inc_cq_tail(NvmeCQueue *cq)
48 if (cq->tail >= cq->size) {
50 cq->phase = !cq->phase;
54 static void nvme_inc_sq_head(NvmeSQueue *sq)
56 sq->head = (sq->head + 1) % sq->size;
59 static uint8_t nvme_cq_full(NvmeCQueue *cq)
61 return (cq->tail + 1) % cq->size == cq->head;
64 static uint8_t nvme_sq_empty(NvmeSQueue *sq)
66 return sq->head == sq->tail;
69 static void nvme_isr_notify(NvmeCtrl *n, NvmeCQueue *cq)
71 if (cq->irq_enabled) {
72 if (msix_enabled(&(n->parent_obj))) {
73 msix_notify(&(n->parent_obj), cq->vector);
75 pci_irq_pulse(&n->parent_obj);
80 static uint16_t nvme_map_prp(QEMUSGList *qsg, uint64_t prp1, uint64_t prp2,
81 uint32_t len, NvmeCtrl *n)
83 hwaddr trans_len = n->page_size - (prp1 % n->page_size);
84 trans_len = MIN(len, trans_len);
85 int num_prps = (len >> n->page_bits) + 1;
88 return NVME_INVALID_FIELD | NVME_DNR;
91 pci_dma_sglist_init(qsg, &n->parent_obj, num_prps);
92 qemu_sglist_add(qsg, prp1, trans_len);
98 if (len > n->page_size) {
99 uint64_t prp_list[n->max_prp_ents];
100 uint32_t nents, prp_trans;
103 nents = (len + n->page_size - 1) >> n->page_bits;
104 prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
105 pci_dma_read(&n->parent_obj, prp2, (void *)prp_list, prp_trans);
107 uint64_t prp_ent = le64_to_cpu(prp_list[i]);
109 if (i == n->max_prp_ents - 1 && len > n->page_size) {
110 if (!prp_ent || prp_ent & (n->page_size - 1)) {
115 nents = (len + n->page_size - 1) >> n->page_bits;
116 prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
117 pci_dma_read(&n->parent_obj, prp_ent, (void *)prp_list,
119 prp_ent = le64_to_cpu(prp_list[i]);
122 if (!prp_ent || prp_ent & (n->page_size - 1)) {
126 trans_len = MIN(len, n->page_size);
127 qemu_sglist_add(qsg, prp_ent, trans_len);
132 if (prp2 & (n->page_size - 1)) {
135 qemu_sglist_add(qsg, prp2, len);
141 qemu_sglist_destroy(qsg);
142 return NVME_INVALID_FIELD | NVME_DNR;
145 static uint16_t nvme_dma_read_prp(NvmeCtrl *n, uint8_t *ptr, uint32_t len,
146 uint64_t prp1, uint64_t prp2)
150 if (nvme_map_prp(&qsg, prp1, prp2, len, n)) {
151 return NVME_INVALID_FIELD | NVME_DNR;
153 if (dma_buf_read(ptr, len, &qsg)) {
154 qemu_sglist_destroy(&qsg);
155 return NVME_INVALID_FIELD | NVME_DNR;
157 qemu_sglist_destroy(&qsg);
161 static void nvme_post_cqes(void *opaque)
163 NvmeCQueue *cq = opaque;
164 NvmeCtrl *n = cq->ctrl;
165 NvmeRequest *req, *next;
167 QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
171 if (nvme_cq_full(cq)) {
175 QTAILQ_REMOVE(&cq->req_list, req, entry);
177 req->cqe.status = cpu_to_le16((req->status << 1) | cq->phase);
178 req->cqe.sq_id = cpu_to_le16(sq->sqid);
179 req->cqe.sq_head = cpu_to_le16(sq->head);
180 addr = cq->dma_addr + cq->tail * n->cqe_size;
181 nvme_inc_cq_tail(cq);
182 pci_dma_write(&n->parent_obj, addr, (void *)&req->cqe,
184 QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
186 nvme_isr_notify(n, cq);
189 static void nvme_enqueue_req_completion(NvmeCQueue *cq, NvmeRequest *req)
191 assert(cq->cqid == req->sq->cqid);
192 QTAILQ_REMOVE(&req->sq->out_req_list, req, entry);
193 QTAILQ_INSERT_TAIL(&cq->req_list, req, entry);
194 timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
197 static void nvme_rw_cb(void *opaque, int ret)
199 NvmeRequest *req = opaque;
200 NvmeSQueue *sq = req->sq;
201 NvmeCtrl *n = sq->ctrl;
202 NvmeCQueue *cq = n->cq[sq->cqid];
204 block_acct_done(blk_get_stats(n->conf.blk), &req->acct);
206 req->status = NVME_SUCCESS;
208 req->status = NVME_INTERNAL_DEV_ERROR;
211 qemu_sglist_destroy(&req->qsg);
213 nvme_enqueue_req_completion(cq, req);
216 static uint16_t nvme_flush(NvmeCtrl *n, NvmeNamespace *ns, NvmeCmd *cmd,
220 block_acct_start(blk_get_stats(n->conf.blk), &req->acct, 0,
222 req->aiocb = blk_aio_flush(n->conf.blk, nvme_rw_cb, req);
224 return NVME_NO_COMPLETE;
227 static uint16_t nvme_rw(NvmeCtrl *n, NvmeNamespace *ns, NvmeCmd *cmd,
230 NvmeRwCmd *rw = (NvmeRwCmd *)cmd;
231 uint32_t nlb = le32_to_cpu(rw->nlb) + 1;
232 uint64_t slba = le64_to_cpu(rw->slba);
233 uint64_t prp1 = le64_to_cpu(rw->prp1);
234 uint64_t prp2 = le64_to_cpu(rw->prp2);
236 uint8_t lba_index = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
237 uint8_t data_shift = ns->id_ns.lbaf[lba_index].ds;
238 uint64_t data_size = (uint64_t)nlb << data_shift;
239 uint64_t aio_slba = slba << (data_shift - BDRV_SECTOR_BITS);
240 int is_write = rw->opcode == NVME_CMD_WRITE ? 1 : 0;
242 if ((slba + nlb) > ns->id_ns.nsze) {
243 return NVME_LBA_RANGE | NVME_DNR;
245 if (nvme_map_prp(&req->qsg, prp1, prp2, data_size, n)) {
246 return NVME_INVALID_FIELD | NVME_DNR;
248 assert((nlb << data_shift) == req->qsg.size);
251 dma_acct_start(n->conf.blk, &req->acct, &req->qsg,
252 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
253 req->aiocb = is_write ?
254 dma_blk_write(n->conf.blk, &req->qsg, aio_slba, nvme_rw_cb, req) :
255 dma_blk_read(n->conf.blk, &req->qsg, aio_slba, nvme_rw_cb, req);
257 return NVME_NO_COMPLETE;
260 static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
263 uint32_t nsid = le32_to_cpu(cmd->nsid);
265 if (nsid == 0 || nsid > n->num_namespaces) {
266 return NVME_INVALID_NSID | NVME_DNR;
269 ns = &n->namespaces[nsid - 1];
270 switch (cmd->opcode) {
272 return nvme_flush(n, ns, cmd, req);
275 return nvme_rw(n, ns, cmd, req);
277 return NVME_INVALID_OPCODE | NVME_DNR;
281 static void nvme_free_sq(NvmeSQueue *sq, NvmeCtrl *n)
283 n->sq[sq->sqid] = NULL;
284 timer_del(sq->timer);
285 timer_free(sq->timer);
292 static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeCmd *cmd)
294 NvmeDeleteQ *c = (NvmeDeleteQ *)cmd;
295 NvmeRequest *req, *next;
298 uint16_t qid = le16_to_cpu(c->qid);
300 if (!qid || nvme_check_sqid(n, qid)) {
301 return NVME_INVALID_QID | NVME_DNR;
305 while (!QTAILQ_EMPTY(&sq->out_req_list)) {
306 req = QTAILQ_FIRST(&sq->out_req_list);
308 blk_aio_cancel(req->aiocb);
310 if (!nvme_check_cqid(n, sq->cqid)) {
311 cq = n->cq[sq->cqid];
312 QTAILQ_REMOVE(&cq->sq_list, sq, entry);
315 QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
317 QTAILQ_REMOVE(&cq->req_list, req, entry);
318 QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
327 static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr,
328 uint16_t sqid, uint16_t cqid, uint16_t size)
334 sq->dma_addr = dma_addr;
338 sq->head = sq->tail = 0;
339 sq->io_req = g_new(NvmeRequest, sq->size);
341 QTAILQ_INIT(&sq->req_list);
342 QTAILQ_INIT(&sq->out_req_list);
343 for (i = 0; i < sq->size; i++) {
344 sq->io_req[i].sq = sq;
345 QTAILQ_INSERT_TAIL(&(sq->req_list), &sq->io_req[i], entry);
347 sq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_process_sq, sq);
351 QTAILQ_INSERT_TAIL(&(cq->sq_list), sq, entry);
355 static uint16_t nvme_create_sq(NvmeCtrl *n, NvmeCmd *cmd)
358 NvmeCreateSq *c = (NvmeCreateSq *)cmd;
360 uint16_t cqid = le16_to_cpu(c->cqid);
361 uint16_t sqid = le16_to_cpu(c->sqid);
362 uint16_t qsize = le16_to_cpu(c->qsize);
363 uint16_t qflags = le16_to_cpu(c->sq_flags);
364 uint64_t prp1 = le64_to_cpu(c->prp1);
366 if (!cqid || nvme_check_cqid(n, cqid)) {
367 return NVME_INVALID_CQID | NVME_DNR;
369 if (!sqid || (sqid && !nvme_check_sqid(n, sqid))) {
370 return NVME_INVALID_QID | NVME_DNR;
372 if (!qsize || qsize > NVME_CAP_MQES(n->bar.cap)) {
373 return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
375 if (!prp1 || prp1 & (n->page_size - 1)) {
376 return NVME_INVALID_FIELD | NVME_DNR;
378 if (!(NVME_SQ_FLAGS_PC(qflags))) {
379 return NVME_INVALID_FIELD | NVME_DNR;
381 sq = g_malloc0(sizeof(*sq));
382 nvme_init_sq(sq, n, prp1, sqid, cqid, qsize + 1);
386 static void nvme_free_cq(NvmeCQueue *cq, NvmeCtrl *n)
388 n->cq[cq->cqid] = NULL;
389 timer_del(cq->timer);
390 timer_free(cq->timer);
391 msix_vector_unuse(&n->parent_obj, cq->vector);
397 static uint16_t nvme_del_cq(NvmeCtrl *n, NvmeCmd *cmd)
399 NvmeDeleteQ *c = (NvmeDeleteQ *)cmd;
401 uint16_t qid = le16_to_cpu(c->qid);
403 if (!qid || nvme_check_cqid(n, qid)) {
404 return NVME_INVALID_CQID | NVME_DNR;
408 if (!QTAILQ_EMPTY(&cq->sq_list)) {
409 return NVME_INVALID_QUEUE_DEL;
415 static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n, uint64_t dma_addr,
416 uint16_t cqid, uint16_t vector, uint16_t size, uint16_t irq_enabled)
421 cq->dma_addr = dma_addr;
423 cq->irq_enabled = irq_enabled;
425 cq->head = cq->tail = 0;
426 QTAILQ_INIT(&cq->req_list);
427 QTAILQ_INIT(&cq->sq_list);
428 msix_vector_use(&n->parent_obj, cq->vector);
430 cq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_post_cqes, cq);
433 static uint16_t nvme_create_cq(NvmeCtrl *n, NvmeCmd *cmd)
436 NvmeCreateCq *c = (NvmeCreateCq *)cmd;
437 uint16_t cqid = le16_to_cpu(c->cqid);
438 uint16_t vector = le16_to_cpu(c->irq_vector);
439 uint16_t qsize = le16_to_cpu(c->qsize);
440 uint16_t qflags = le16_to_cpu(c->cq_flags);
441 uint64_t prp1 = le64_to_cpu(c->prp1);
443 if (!cqid || (cqid && !nvme_check_cqid(n, cqid))) {
444 return NVME_INVALID_CQID | NVME_DNR;
446 if (!qsize || qsize > NVME_CAP_MQES(n->bar.cap)) {
447 return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
450 return NVME_INVALID_FIELD | NVME_DNR;
452 if (vector > n->num_queues) {
453 return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
455 if (!(NVME_CQ_FLAGS_PC(qflags))) {
456 return NVME_INVALID_FIELD | NVME_DNR;
459 cq = g_malloc0(sizeof(*cq));
460 nvme_init_cq(cq, n, prp1, cqid, vector, qsize + 1,
461 NVME_CQ_FLAGS_IEN(qflags));
465 static uint16_t nvme_identify(NvmeCtrl *n, NvmeCmd *cmd)
468 NvmeIdentify *c = (NvmeIdentify *)cmd;
469 uint32_t cns = le32_to_cpu(c->cns);
470 uint32_t nsid = le32_to_cpu(c->nsid);
471 uint64_t prp1 = le64_to_cpu(c->prp1);
472 uint64_t prp2 = le64_to_cpu(c->prp2);
475 return nvme_dma_read_prp(n, (uint8_t *)&n->id_ctrl, sizeof(n->id_ctrl),
478 if (nsid == 0 || nsid > n->num_namespaces) {
479 return NVME_INVALID_NSID | NVME_DNR;
482 ns = &n->namespaces[nsid - 1];
483 return nvme_dma_read_prp(n, (uint8_t *)&ns->id_ns, sizeof(ns->id_ns),
487 static uint16_t nvme_get_feature(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
489 uint32_t dw10 = le32_to_cpu(cmd->cdw10);
493 case NVME_VOLATILE_WRITE_CACHE:
494 result = blk_enable_write_cache(n->conf.blk);
496 case NVME_NUMBER_OF_QUEUES:
497 result = cpu_to_le32((n->num_queues - 1) | ((n->num_queues - 1) << 16));
500 return NVME_INVALID_FIELD | NVME_DNR;
503 req->cqe.result = result;
507 static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
509 uint32_t dw10 = le32_to_cpu(cmd->cdw10);
510 uint32_t dw11 = le32_to_cpu(cmd->cdw11);
513 case NVME_VOLATILE_WRITE_CACHE:
514 blk_set_enable_write_cache(n->conf.blk, dw11 & 1);
516 case NVME_NUMBER_OF_QUEUES:
518 cpu_to_le32((n->num_queues - 1) | ((n->num_queues - 1) << 16));
521 return NVME_INVALID_FIELD | NVME_DNR;
526 static uint16_t nvme_admin_cmd(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
528 switch (cmd->opcode) {
529 case NVME_ADM_CMD_DELETE_SQ:
530 return nvme_del_sq(n, cmd);
531 case NVME_ADM_CMD_CREATE_SQ:
532 return nvme_create_sq(n, cmd);
533 case NVME_ADM_CMD_DELETE_CQ:
534 return nvme_del_cq(n, cmd);
535 case NVME_ADM_CMD_CREATE_CQ:
536 return nvme_create_cq(n, cmd);
537 case NVME_ADM_CMD_IDENTIFY:
538 return nvme_identify(n, cmd);
539 case NVME_ADM_CMD_SET_FEATURES:
540 return nvme_set_feature(n, cmd, req);
541 case NVME_ADM_CMD_GET_FEATURES:
542 return nvme_get_feature(n, cmd, req);
544 return NVME_INVALID_OPCODE | NVME_DNR;
548 static void nvme_process_sq(void *opaque)
550 NvmeSQueue *sq = opaque;
551 NvmeCtrl *n = sq->ctrl;
552 NvmeCQueue *cq = n->cq[sq->cqid];
559 while (!(nvme_sq_empty(sq) || QTAILQ_EMPTY(&sq->req_list))) {
560 addr = sq->dma_addr + sq->head * n->sqe_size;
561 pci_dma_read(&n->parent_obj, addr, (void *)&cmd, sizeof(cmd));
562 nvme_inc_sq_head(sq);
564 req = QTAILQ_FIRST(&sq->req_list);
565 QTAILQ_REMOVE(&sq->req_list, req, entry);
566 QTAILQ_INSERT_TAIL(&sq->out_req_list, req, entry);
567 memset(&req->cqe, 0, sizeof(req->cqe));
568 req->cqe.cid = cmd.cid;
570 status = sq->sqid ? nvme_io_cmd(n, &cmd, req) :
571 nvme_admin_cmd(n, &cmd, req);
572 if (status != NVME_NO_COMPLETE) {
573 req->status = status;
574 nvme_enqueue_req_completion(cq, req);
579 static void nvme_clear_ctrl(NvmeCtrl *n)
583 for (i = 0; i < n->num_queues; i++) {
584 if (n->sq[i] != NULL) {
585 nvme_free_sq(n->sq[i], n);
588 for (i = 0; i < n->num_queues; i++) {
589 if (n->cq[i] != NULL) {
590 nvme_free_cq(n->cq[i], n);
594 blk_flush(n->conf.blk);
598 static int nvme_start_ctrl(NvmeCtrl *n)
600 uint32_t page_bits = NVME_CC_MPS(n->bar.cc) + 12;
601 uint32_t page_size = 1 << page_bits;
603 if (n->cq[0] || n->sq[0] || !n->bar.asq || !n->bar.acq ||
604 n->bar.asq & (page_size - 1) || n->bar.acq & (page_size - 1) ||
605 NVME_CC_MPS(n->bar.cc) < NVME_CAP_MPSMIN(n->bar.cap) ||
606 NVME_CC_MPS(n->bar.cc) > NVME_CAP_MPSMAX(n->bar.cap) ||
607 NVME_CC_IOCQES(n->bar.cc) < NVME_CTRL_CQES_MIN(n->id_ctrl.cqes) ||
608 NVME_CC_IOCQES(n->bar.cc) > NVME_CTRL_CQES_MAX(n->id_ctrl.cqes) ||
609 NVME_CC_IOSQES(n->bar.cc) < NVME_CTRL_SQES_MIN(n->id_ctrl.sqes) ||
610 NVME_CC_IOSQES(n->bar.cc) > NVME_CTRL_SQES_MAX(n->id_ctrl.sqes) ||
611 !NVME_AQA_ASQS(n->bar.aqa) || !NVME_AQA_ACQS(n->bar.aqa)) {
615 n->page_bits = page_bits;
616 n->page_size = page_size;
617 n->max_prp_ents = n->page_size / sizeof(uint64_t);
618 n->cqe_size = 1 << NVME_CC_IOCQES(n->bar.cc);
619 n->sqe_size = 1 << NVME_CC_IOSQES(n->bar.cc);
620 nvme_init_cq(&n->admin_cq, n, n->bar.acq, 0, 0,
621 NVME_AQA_ACQS(n->bar.aqa) + 1, 1);
622 nvme_init_sq(&n->admin_sq, n, n->bar.asq, 0, 0,
623 NVME_AQA_ASQS(n->bar.aqa) + 1);
628 static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data,
633 n->bar.intms |= data & 0xffffffff;
634 n->bar.intmc = n->bar.intms;
637 n->bar.intms &= ~(data & 0xffffffff);
638 n->bar.intmc = n->bar.intms;
641 /* Windows first sends data, then sends enable bit */
642 if (!NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc) &&
643 !NVME_CC_SHN(data) && !NVME_CC_SHN(n->bar.cc))
648 if (NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc)) {
650 if (nvme_start_ctrl(n)) {
651 n->bar.csts = NVME_CSTS_FAILED;
653 n->bar.csts = NVME_CSTS_READY;
655 } else if (!NVME_CC_EN(data) && NVME_CC_EN(n->bar.cc)) {
657 n->bar.csts &= ~NVME_CSTS_READY;
659 if (NVME_CC_SHN(data) && !(NVME_CC_SHN(n->bar.cc))) {
662 n->bar.csts |= NVME_CSTS_SHST_COMPLETE;
663 } else if (!NVME_CC_SHN(data) && NVME_CC_SHN(n->bar.cc)) {
664 n->bar.csts &= ~NVME_CSTS_SHST_COMPLETE;
669 n->bar.aqa = data & 0xffffffff;
675 n->bar.asq |= data << 32;
681 n->bar.acq |= data << 32;
688 static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
690 NvmeCtrl *n = (NvmeCtrl *)opaque;
691 uint8_t *ptr = (uint8_t *)&n->bar;
694 if (addr < sizeof(n->bar)) {
695 memcpy(&val, ptr + addr, size);
700 static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
704 if (addr & ((1 << 2) - 1)) {
708 if (((addr - 0x1000) >> 2) & 1) {
709 uint16_t new_head = val & 0xffff;
713 qid = (addr - (0x1000 + (1 << 2))) >> 3;
714 if (nvme_check_cqid(n, qid)) {
719 if (new_head >= cq->size) {
723 start_sqs = nvme_cq_full(cq) ? 1 : 0;
727 QTAILQ_FOREACH(sq, &cq->sq_list, entry) {
728 timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
730 timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
733 if (cq->tail != cq->head) {
734 nvme_isr_notify(n, cq);
737 uint16_t new_tail = val & 0xffff;
740 qid = (addr - 0x1000) >> 3;
741 if (nvme_check_sqid(n, qid)) {
746 if (new_tail >= sq->size) {
751 timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
755 static void nvme_mmio_write(void *opaque, hwaddr addr, uint64_t data,
758 NvmeCtrl *n = (NvmeCtrl *)opaque;
759 if (addr < sizeof(n->bar)) {
760 nvme_write_bar(n, addr, data, size);
761 } else if (addr >= 0x1000) {
762 nvme_process_db(n, addr, data);
766 static const MemoryRegionOps nvme_mmio_ops = {
767 .read = nvme_mmio_read,
768 .write = nvme_mmio_write,
769 .endianness = DEVICE_LITTLE_ENDIAN,
771 .min_access_size = 2,
772 .max_access_size = 8,
776 static int nvme_init(PCIDevice *pci_dev)
778 NvmeCtrl *n = NVME(pci_dev);
779 NvmeIdCtrl *id = &n->id_ctrl;
789 bs_size = blk_getlength(n->conf.blk);
794 blkconf_serial(&n->conf, &n->serial);
798 blkconf_blocksizes(&n->conf);
800 pci_conf = pci_dev->config;
801 pci_conf[PCI_INTERRUPT_PIN] = 1;
802 pci_config_set_prog_interface(pci_dev->config, 0x2);
803 pci_config_set_class(pci_dev->config, PCI_CLASS_STORAGE_EXPRESS);
804 pcie_endpoint_cap_init(&n->parent_obj, 0x80);
806 n->num_namespaces = 1;
808 n->reg_size = pow2ceil(0x1004 + 2 * (n->num_queues + 1) * 4);
809 n->ns_size = bs_size / (uint64_t)n->num_namespaces;
811 n->namespaces = g_new0(NvmeNamespace, n->num_namespaces);
812 n->sq = g_new0(NvmeSQueue *, n->num_queues);
813 n->cq = g_new0(NvmeCQueue *, n->num_queues);
815 memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n,
816 "nvme", n->reg_size);
817 pci_register_bar(&n->parent_obj, 0,
818 PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64,
820 msix_init_exclusive_bar(&n->parent_obj, n->num_queues, 4);
822 id->vid = cpu_to_le16(pci_get_word(pci_conf + PCI_VENDOR_ID));
823 id->ssvid = cpu_to_le16(pci_get_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID));
824 strpadcpy((char *)id->mn, sizeof(id->mn), "QEMU NVMe Ctrl", ' ');
825 strpadcpy((char *)id->fr, sizeof(id->fr), "1.0", ' ');
826 strpadcpy((char *)id->sn, sizeof(id->sn), n->serial, ' ');
831 id->oacs = cpu_to_le16(0);
834 id->sqes = (0x6 << 4) | 0x6;
835 id->cqes = (0x4 << 4) | 0x4;
836 id->nn = cpu_to_le32(n->num_namespaces);
837 id->psd[0].mp = cpu_to_le16(0x9c4);
838 id->psd[0].enlat = cpu_to_le32(0x10);
839 id->psd[0].exlat = cpu_to_le32(0x4);
840 if (blk_enable_write_cache(n->conf.blk)) {
845 NVME_CAP_SET_MQES(n->bar.cap, 0x7ff);
846 NVME_CAP_SET_CQR(n->bar.cap, 1);
847 NVME_CAP_SET_AMS(n->bar.cap, 1);
848 NVME_CAP_SET_TO(n->bar.cap, 0xf);
849 NVME_CAP_SET_CSS(n->bar.cap, 1);
850 NVME_CAP_SET_MPSMAX(n->bar.cap, 4);
852 n->bar.vs = 0x00010100;
853 n->bar.intmc = n->bar.intms = 0;
855 for (i = 0; i < n->num_namespaces; i++) {
856 NvmeNamespace *ns = &n->namespaces[i];
857 NvmeIdNs *id_ns = &ns->id_ns;
864 id_ns->lbaf[0].ds = BDRV_SECTOR_BITS;
865 id_ns->ncap = id_ns->nuse = id_ns->nsze =
866 cpu_to_le64(n->ns_size >>
867 id_ns->lbaf[NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas)].ds);
872 static void nvme_exit(PCIDevice *pci_dev)
874 NvmeCtrl *n = NVME(pci_dev);
877 g_free(n->namespaces);
880 msix_uninit_exclusive_bar(pci_dev);
883 static Property nvme_props[] = {
884 DEFINE_BLOCK_PROPERTIES(NvmeCtrl, conf),
885 DEFINE_PROP_STRING("serial", NvmeCtrl, serial),
886 DEFINE_PROP_END_OF_LIST(),
889 static const VMStateDescription nvme_vmstate = {
894 static void nvme_class_init(ObjectClass *oc, void *data)
896 DeviceClass *dc = DEVICE_CLASS(oc);
897 PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
899 pc->init = nvme_init;
900 pc->exit = nvme_exit;
901 pc->class_id = PCI_CLASS_STORAGE_EXPRESS;
902 pc->vendor_id = PCI_VENDOR_ID_INTEL;
903 pc->device_id = 0x5845;
907 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
908 dc->desc = "Non-Volatile Memory Express";
909 dc->props = nvme_props;
910 dc->vmsd = &nvme_vmstate;
913 static void nvme_get_bootindex(Object *obj, Visitor *v, void *opaque,
914 const char *name, Error **errp)
916 NvmeCtrl *s = NVME(obj);
918 visit_type_int32(v, &s->conf.bootindex, name, errp);
921 static void nvme_set_bootindex(Object *obj, Visitor *v, void *opaque,
922 const char *name, Error **errp)
924 NvmeCtrl *s = NVME(obj);
926 Error *local_err = NULL;
928 visit_type_int32(v, &boot_index, name, &local_err);
932 /* check whether bootindex is present in fw_boot_order list */
933 check_boot_index(boot_index, &local_err);
937 /* change bootindex to a new one */
938 s->conf.bootindex = boot_index;
942 error_propagate(errp, local_err);
946 static void nvme_instance_init(Object *obj)
948 object_property_add(obj, "bootindex", "int32",
950 nvme_set_bootindex, NULL, NULL, NULL);
951 object_property_set_int(obj, -1, "bootindex", NULL);
954 static const TypeInfo nvme_info = {
956 .parent = TYPE_PCI_DEVICE,
957 .instance_size = sizeof(NvmeCtrl),
958 .class_init = nvme_class_init,
959 .instance_init = nvme_instance_init,
962 static void nvme_register_types(void)
964 type_register_static(&nvme_info);
967 type_init(nvme_register_types)