Merge remote-tracking branch 'remotes/xtensa/tags/20180402-xtensa' into staging
[qemu/ar7.git] / hw / block / nvme.c
blob85d24064009624fdcc48eccd00c36ccf66f07c30
1 /*
2 * QEMU NVM Express Controller
4 * Copyright (c) 2012, Intel Corporation
6 * Written by Keith Busch <keith.busch@intel.com>
8 * This code is licensed under the GNU GPL v2 or later.
9 */
11 /**
12 * Reference Specs: http://www.nvmexpress.org, 1.2, 1.1, 1.0e
14 * http://www.nvmexpress.org/resources/
17 /**
18 * Usage: add options:
19 * -drive file=<file>,if=none,id=<drive_id>
20 * -device nvme,drive=<drive_id>,serial=<serial>,id=<id[optional]>, \
21 * cmb_size_mb=<cmb_size_mb[optional]>
23 * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
24 * offset 0 in BAR2 and supports only WDS, RDS and SQS for now.
27 #include "qemu/osdep.h"
28 #include "hw/block/block.h"
29 #include "hw/hw.h"
30 #include "hw/pci/msix.h"
31 #include "hw/pci/pci.h"
32 #include "sysemu/sysemu.h"
33 #include "qapi/error.h"
34 #include "qapi/visitor.h"
35 #include "sysemu/block-backend.h"
37 #include "qemu/log.h"
38 #include "trace.h"
39 #include "nvme.h"
41 #define NVME_GUEST_ERR(trace, fmt, ...) \
42 do { \
43 (trace_##trace)(__VA_ARGS__); \
44 qemu_log_mask(LOG_GUEST_ERROR, #trace \
45 " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
46 } while (0)
48 static void nvme_process_sq(void *opaque);
50 static void nvme_addr_read(NvmeCtrl *n, hwaddr addr, void *buf, int size)
52 if (n->cmbsz && addr >= n->ctrl_mem.addr &&
53 addr < (n->ctrl_mem.addr + int128_get64(n->ctrl_mem.size))) {
54 memcpy(buf, (void *)&n->cmbuf[addr - n->ctrl_mem.addr], size);
55 } else {
56 pci_dma_read(&n->parent_obj, addr, buf, size);
60 static int nvme_check_sqid(NvmeCtrl *n, uint16_t sqid)
62 return sqid < n->num_queues && n->sq[sqid] != NULL ? 0 : -1;
65 static int nvme_check_cqid(NvmeCtrl *n, uint16_t cqid)
67 return cqid < n->num_queues && n->cq[cqid] != NULL ? 0 : -1;
70 static void nvme_inc_cq_tail(NvmeCQueue *cq)
72 cq->tail++;
73 if (cq->tail >= cq->size) {
74 cq->tail = 0;
75 cq->phase = !cq->phase;
79 static void nvme_inc_sq_head(NvmeSQueue *sq)
81 sq->head = (sq->head + 1) % sq->size;
84 static uint8_t nvme_cq_full(NvmeCQueue *cq)
86 return (cq->tail + 1) % cq->size == cq->head;
89 static uint8_t nvme_sq_empty(NvmeSQueue *sq)
91 return sq->head == sq->tail;
94 static void nvme_irq_check(NvmeCtrl *n)
96 if (msix_enabled(&(n->parent_obj))) {
97 return;
99 if (~n->bar.intms & n->irq_status) {
100 pci_irq_assert(&n->parent_obj);
101 } else {
102 pci_irq_deassert(&n->parent_obj);
106 static void nvme_irq_assert(NvmeCtrl *n, NvmeCQueue *cq)
108 if (cq->irq_enabled) {
109 if (msix_enabled(&(n->parent_obj))) {
110 trace_nvme_irq_msix(cq->vector);
111 msix_notify(&(n->parent_obj), cq->vector);
112 } else {
113 trace_nvme_irq_pin();
114 assert(cq->cqid < 64);
115 n->irq_status |= 1 << cq->cqid;
116 nvme_irq_check(n);
118 } else {
119 trace_nvme_irq_masked();
123 static void nvme_irq_deassert(NvmeCtrl *n, NvmeCQueue *cq)
125 if (cq->irq_enabled) {
126 if (msix_enabled(&(n->parent_obj))) {
127 return;
128 } else {
129 assert(cq->cqid < 64);
130 n->irq_status &= ~(1 << cq->cqid);
131 nvme_irq_check(n);
136 static uint16_t nvme_map_prp(QEMUSGList *qsg, QEMUIOVector *iov, uint64_t prp1,
137 uint64_t prp2, uint32_t len, NvmeCtrl *n)
139 hwaddr trans_len = n->page_size - (prp1 % n->page_size);
140 trans_len = MIN(len, trans_len);
141 int num_prps = (len >> n->page_bits) + 1;
143 if (unlikely(!prp1)) {
144 trace_nvme_err_invalid_prp();
145 return NVME_INVALID_FIELD | NVME_DNR;
146 } else if (n->cmbsz && prp1 >= n->ctrl_mem.addr &&
147 prp1 < n->ctrl_mem.addr + int128_get64(n->ctrl_mem.size)) {
148 qsg->nsg = 0;
149 qemu_iovec_init(iov, num_prps);
150 qemu_iovec_add(iov, (void *)&n->cmbuf[prp1 - n->ctrl_mem.addr], trans_len);
151 } else {
152 pci_dma_sglist_init(qsg, &n->parent_obj, num_prps);
153 qemu_sglist_add(qsg, prp1, trans_len);
155 len -= trans_len;
156 if (len) {
157 if (unlikely(!prp2)) {
158 trace_nvme_err_invalid_prp2_missing();
159 goto unmap;
161 if (len > n->page_size) {
162 uint64_t prp_list[n->max_prp_ents];
163 uint32_t nents, prp_trans;
164 int i = 0;
166 nents = (len + n->page_size - 1) >> n->page_bits;
167 prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
168 nvme_addr_read(n, prp2, (void *)prp_list, prp_trans);
169 while (len != 0) {
170 uint64_t prp_ent = le64_to_cpu(prp_list[i]);
172 if (i == n->max_prp_ents - 1 && len > n->page_size) {
173 if (unlikely(!prp_ent || prp_ent & (n->page_size - 1))) {
174 trace_nvme_err_invalid_prplist_ent(prp_ent);
175 goto unmap;
178 i = 0;
179 nents = (len + n->page_size - 1) >> n->page_bits;
180 prp_trans = MIN(n->max_prp_ents, nents) * sizeof(uint64_t);
181 nvme_addr_read(n, prp_ent, (void *)prp_list,
182 prp_trans);
183 prp_ent = le64_to_cpu(prp_list[i]);
186 if (unlikely(!prp_ent || prp_ent & (n->page_size - 1))) {
187 trace_nvme_err_invalid_prplist_ent(prp_ent);
188 goto unmap;
191 trans_len = MIN(len, n->page_size);
192 if (qsg->nsg){
193 qemu_sglist_add(qsg, prp_ent, trans_len);
194 } else {
195 qemu_iovec_add(iov, (void *)&n->cmbuf[prp_ent - n->ctrl_mem.addr], trans_len);
197 len -= trans_len;
198 i++;
200 } else {
201 if (unlikely(prp2 & (n->page_size - 1))) {
202 trace_nvme_err_invalid_prp2_align(prp2);
203 goto unmap;
205 if (qsg->nsg) {
206 qemu_sglist_add(qsg, prp2, len);
207 } else {
208 qemu_iovec_add(iov, (void *)&n->cmbuf[prp2 - n->ctrl_mem.addr], trans_len);
212 return NVME_SUCCESS;
214 unmap:
215 qemu_sglist_destroy(qsg);
216 return NVME_INVALID_FIELD | NVME_DNR;
219 static uint16_t nvme_dma_read_prp(NvmeCtrl *n, uint8_t *ptr, uint32_t len,
220 uint64_t prp1, uint64_t prp2)
222 QEMUSGList qsg;
223 QEMUIOVector iov;
224 uint16_t status = NVME_SUCCESS;
226 trace_nvme_dma_read(prp1, prp2);
228 if (nvme_map_prp(&qsg, &iov, prp1, prp2, len, n)) {
229 return NVME_INVALID_FIELD | NVME_DNR;
231 if (qsg.nsg > 0) {
232 if (unlikely(dma_buf_read(ptr, len, &qsg))) {
233 trace_nvme_err_invalid_dma();
234 status = NVME_INVALID_FIELD | NVME_DNR;
236 qemu_sglist_destroy(&qsg);
237 } else {
238 if (unlikely(qemu_iovec_to_buf(&iov, 0, ptr, len) != len)) {
239 trace_nvme_err_invalid_dma();
240 status = NVME_INVALID_FIELD | NVME_DNR;
242 qemu_iovec_destroy(&iov);
244 return status;
247 static void nvme_post_cqes(void *opaque)
249 NvmeCQueue *cq = opaque;
250 NvmeCtrl *n = cq->ctrl;
251 NvmeRequest *req, *next;
253 QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
254 NvmeSQueue *sq;
255 hwaddr addr;
257 if (nvme_cq_full(cq)) {
258 break;
261 QTAILQ_REMOVE(&cq->req_list, req, entry);
262 sq = req->sq;
263 req->cqe.status = cpu_to_le16((req->status << 1) | cq->phase);
264 req->cqe.sq_id = cpu_to_le16(sq->sqid);
265 req->cqe.sq_head = cpu_to_le16(sq->head);
266 addr = cq->dma_addr + cq->tail * n->cqe_size;
267 nvme_inc_cq_tail(cq);
268 pci_dma_write(&n->parent_obj, addr, (void *)&req->cqe,
269 sizeof(req->cqe));
270 QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
272 nvme_irq_assert(n, cq);
275 static void nvme_enqueue_req_completion(NvmeCQueue *cq, NvmeRequest *req)
277 assert(cq->cqid == req->sq->cqid);
278 QTAILQ_REMOVE(&req->sq->out_req_list, req, entry);
279 QTAILQ_INSERT_TAIL(&cq->req_list, req, entry);
280 timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
283 static void nvme_rw_cb(void *opaque, int ret)
285 NvmeRequest *req = opaque;
286 NvmeSQueue *sq = req->sq;
287 NvmeCtrl *n = sq->ctrl;
288 NvmeCQueue *cq = n->cq[sq->cqid];
290 if (!ret) {
291 block_acct_done(blk_get_stats(n->conf.blk), &req->acct);
292 req->status = NVME_SUCCESS;
293 } else {
294 block_acct_failed(blk_get_stats(n->conf.blk), &req->acct);
295 req->status = NVME_INTERNAL_DEV_ERROR;
297 if (req->has_sg) {
298 qemu_sglist_destroy(&req->qsg);
300 nvme_enqueue_req_completion(cq, req);
303 static uint16_t nvme_flush(NvmeCtrl *n, NvmeNamespace *ns, NvmeCmd *cmd,
304 NvmeRequest *req)
306 req->has_sg = false;
307 block_acct_start(blk_get_stats(n->conf.blk), &req->acct, 0,
308 BLOCK_ACCT_FLUSH);
309 req->aiocb = blk_aio_flush(n->conf.blk, nvme_rw_cb, req);
311 return NVME_NO_COMPLETE;
314 static uint16_t nvme_write_zeros(NvmeCtrl *n, NvmeNamespace *ns, NvmeCmd *cmd,
315 NvmeRequest *req)
317 NvmeRwCmd *rw = (NvmeRwCmd *)cmd;
318 const uint8_t lba_index = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
319 const uint8_t data_shift = ns->id_ns.lbaf[lba_index].ds;
320 uint64_t slba = le64_to_cpu(rw->slba);
321 uint32_t nlb = le16_to_cpu(rw->nlb) + 1;
322 uint64_t aio_slba = slba << (data_shift - BDRV_SECTOR_BITS);
323 uint32_t aio_nlb = nlb << (data_shift - BDRV_SECTOR_BITS);
325 if (unlikely(slba + nlb > ns->id_ns.nsze)) {
326 trace_nvme_err_invalid_lba_range(slba, nlb, ns->id_ns.nsze);
327 return NVME_LBA_RANGE | NVME_DNR;
330 req->has_sg = false;
331 block_acct_start(blk_get_stats(n->conf.blk), &req->acct, 0,
332 BLOCK_ACCT_WRITE);
333 req->aiocb = blk_aio_pwrite_zeroes(n->conf.blk, aio_slba, aio_nlb,
334 BDRV_REQ_MAY_UNMAP, nvme_rw_cb, req);
335 return NVME_NO_COMPLETE;
338 static uint16_t nvme_rw(NvmeCtrl *n, NvmeNamespace *ns, NvmeCmd *cmd,
339 NvmeRequest *req)
341 NvmeRwCmd *rw = (NvmeRwCmd *)cmd;
342 uint32_t nlb = le32_to_cpu(rw->nlb) + 1;
343 uint64_t slba = le64_to_cpu(rw->slba);
344 uint64_t prp1 = le64_to_cpu(rw->prp1);
345 uint64_t prp2 = le64_to_cpu(rw->prp2);
347 uint8_t lba_index = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
348 uint8_t data_shift = ns->id_ns.lbaf[lba_index].ds;
349 uint64_t data_size = (uint64_t)nlb << data_shift;
350 uint64_t data_offset = slba << data_shift;
351 int is_write = rw->opcode == NVME_CMD_WRITE ? 1 : 0;
352 enum BlockAcctType acct = is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ;
354 trace_nvme_rw(is_write ? "write" : "read", nlb, data_size, slba);
356 if (unlikely((slba + nlb) > ns->id_ns.nsze)) {
357 block_acct_invalid(blk_get_stats(n->conf.blk), acct);
358 trace_nvme_err_invalid_lba_range(slba, nlb, ns->id_ns.nsze);
359 return NVME_LBA_RANGE | NVME_DNR;
362 if (nvme_map_prp(&req->qsg, &req->iov, prp1, prp2, data_size, n)) {
363 block_acct_invalid(blk_get_stats(n->conf.blk), acct);
364 return NVME_INVALID_FIELD | NVME_DNR;
367 dma_acct_start(n->conf.blk, &req->acct, &req->qsg, acct);
368 if (req->qsg.nsg > 0) {
369 req->has_sg = true;
370 req->aiocb = is_write ?
371 dma_blk_write(n->conf.blk, &req->qsg, data_offset, BDRV_SECTOR_SIZE,
372 nvme_rw_cb, req) :
373 dma_blk_read(n->conf.blk, &req->qsg, data_offset, BDRV_SECTOR_SIZE,
374 nvme_rw_cb, req);
375 } else {
376 req->has_sg = false;
377 req->aiocb = is_write ?
378 blk_aio_pwritev(n->conf.blk, data_offset, &req->iov, 0, nvme_rw_cb,
379 req) :
380 blk_aio_preadv(n->conf.blk, data_offset, &req->iov, 0, nvme_rw_cb,
381 req);
384 return NVME_NO_COMPLETE;
387 static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
389 NvmeNamespace *ns;
390 uint32_t nsid = le32_to_cpu(cmd->nsid);
392 if (unlikely(nsid == 0 || nsid > n->num_namespaces)) {
393 trace_nvme_err_invalid_ns(nsid, n->num_namespaces);
394 return NVME_INVALID_NSID | NVME_DNR;
397 ns = &n->namespaces[nsid - 1];
398 switch (cmd->opcode) {
399 case NVME_CMD_FLUSH:
400 return nvme_flush(n, ns, cmd, req);
401 case NVME_CMD_WRITE_ZEROS:
402 return nvme_write_zeros(n, ns, cmd, req);
403 case NVME_CMD_WRITE:
404 case NVME_CMD_READ:
405 return nvme_rw(n, ns, cmd, req);
406 default:
407 trace_nvme_err_invalid_opc(cmd->opcode);
408 return NVME_INVALID_OPCODE | NVME_DNR;
412 static void nvme_free_sq(NvmeSQueue *sq, NvmeCtrl *n)
414 n->sq[sq->sqid] = NULL;
415 timer_del(sq->timer);
416 timer_free(sq->timer);
417 g_free(sq->io_req);
418 if (sq->sqid) {
419 g_free(sq);
423 static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeCmd *cmd)
425 NvmeDeleteQ *c = (NvmeDeleteQ *)cmd;
426 NvmeRequest *req, *next;
427 NvmeSQueue *sq;
428 NvmeCQueue *cq;
429 uint16_t qid = le16_to_cpu(c->qid);
431 if (unlikely(!qid || nvme_check_sqid(n, qid))) {
432 trace_nvme_err_invalid_del_sq(qid);
433 return NVME_INVALID_QID | NVME_DNR;
436 trace_nvme_del_sq(qid);
438 sq = n->sq[qid];
439 while (!QTAILQ_EMPTY(&sq->out_req_list)) {
440 req = QTAILQ_FIRST(&sq->out_req_list);
441 assert(req->aiocb);
442 blk_aio_cancel(req->aiocb);
444 if (!nvme_check_cqid(n, sq->cqid)) {
445 cq = n->cq[sq->cqid];
446 QTAILQ_REMOVE(&cq->sq_list, sq, entry);
448 nvme_post_cqes(cq);
449 QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
450 if (req->sq == sq) {
451 QTAILQ_REMOVE(&cq->req_list, req, entry);
452 QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
457 nvme_free_sq(sq, n);
458 return NVME_SUCCESS;
461 static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr,
462 uint16_t sqid, uint16_t cqid, uint16_t size)
464 int i;
465 NvmeCQueue *cq;
467 sq->ctrl = n;
468 sq->dma_addr = dma_addr;
469 sq->sqid = sqid;
470 sq->size = size;
471 sq->cqid = cqid;
472 sq->head = sq->tail = 0;
473 sq->io_req = g_new(NvmeRequest, sq->size);
475 QTAILQ_INIT(&sq->req_list);
476 QTAILQ_INIT(&sq->out_req_list);
477 for (i = 0; i < sq->size; i++) {
478 sq->io_req[i].sq = sq;
479 QTAILQ_INSERT_TAIL(&(sq->req_list), &sq->io_req[i], entry);
481 sq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_process_sq, sq);
483 assert(n->cq[cqid]);
484 cq = n->cq[cqid];
485 QTAILQ_INSERT_TAIL(&(cq->sq_list), sq, entry);
486 n->sq[sqid] = sq;
489 static uint16_t nvme_create_sq(NvmeCtrl *n, NvmeCmd *cmd)
491 NvmeSQueue *sq;
492 NvmeCreateSq *c = (NvmeCreateSq *)cmd;
494 uint16_t cqid = le16_to_cpu(c->cqid);
495 uint16_t sqid = le16_to_cpu(c->sqid);
496 uint16_t qsize = le16_to_cpu(c->qsize);
497 uint16_t qflags = le16_to_cpu(c->sq_flags);
498 uint64_t prp1 = le64_to_cpu(c->prp1);
500 trace_nvme_create_sq(prp1, sqid, cqid, qsize, qflags);
502 if (unlikely(!cqid || nvme_check_cqid(n, cqid))) {
503 trace_nvme_err_invalid_create_sq_cqid(cqid);
504 return NVME_INVALID_CQID | NVME_DNR;
506 if (unlikely(!sqid || !nvme_check_sqid(n, sqid))) {
507 trace_nvme_err_invalid_create_sq_sqid(sqid);
508 return NVME_INVALID_QID | NVME_DNR;
510 if (unlikely(!qsize || qsize > NVME_CAP_MQES(n->bar.cap))) {
511 trace_nvme_err_invalid_create_sq_size(qsize);
512 return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
514 if (unlikely(!prp1 || prp1 & (n->page_size - 1))) {
515 trace_nvme_err_invalid_create_sq_addr(prp1);
516 return NVME_INVALID_FIELD | NVME_DNR;
518 if (unlikely(!(NVME_SQ_FLAGS_PC(qflags)))) {
519 trace_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags));
520 return NVME_INVALID_FIELD | NVME_DNR;
522 sq = g_malloc0(sizeof(*sq));
523 nvme_init_sq(sq, n, prp1, sqid, cqid, qsize + 1);
524 return NVME_SUCCESS;
527 static void nvme_free_cq(NvmeCQueue *cq, NvmeCtrl *n)
529 n->cq[cq->cqid] = NULL;
530 timer_del(cq->timer);
531 timer_free(cq->timer);
532 msix_vector_unuse(&n->parent_obj, cq->vector);
533 if (cq->cqid) {
534 g_free(cq);
538 static uint16_t nvme_del_cq(NvmeCtrl *n, NvmeCmd *cmd)
540 NvmeDeleteQ *c = (NvmeDeleteQ *)cmd;
541 NvmeCQueue *cq;
542 uint16_t qid = le16_to_cpu(c->qid);
544 if (unlikely(!qid || nvme_check_cqid(n, qid))) {
545 trace_nvme_err_invalid_del_cq_cqid(qid);
546 return NVME_INVALID_CQID | NVME_DNR;
549 cq = n->cq[qid];
550 if (unlikely(!QTAILQ_EMPTY(&cq->sq_list))) {
551 trace_nvme_err_invalid_del_cq_notempty(qid);
552 return NVME_INVALID_QUEUE_DEL;
554 trace_nvme_del_cq(qid);
555 nvme_free_cq(cq, n);
556 return NVME_SUCCESS;
559 static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n, uint64_t dma_addr,
560 uint16_t cqid, uint16_t vector, uint16_t size, uint16_t irq_enabled)
562 cq->ctrl = n;
563 cq->cqid = cqid;
564 cq->size = size;
565 cq->dma_addr = dma_addr;
566 cq->phase = 1;
567 cq->irq_enabled = irq_enabled;
568 cq->vector = vector;
569 cq->head = cq->tail = 0;
570 QTAILQ_INIT(&cq->req_list);
571 QTAILQ_INIT(&cq->sq_list);
572 msix_vector_use(&n->parent_obj, cq->vector);
573 n->cq[cqid] = cq;
574 cq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_post_cqes, cq);
577 static uint16_t nvme_create_cq(NvmeCtrl *n, NvmeCmd *cmd)
579 NvmeCQueue *cq;
580 NvmeCreateCq *c = (NvmeCreateCq *)cmd;
581 uint16_t cqid = le16_to_cpu(c->cqid);
582 uint16_t vector = le16_to_cpu(c->irq_vector);
583 uint16_t qsize = le16_to_cpu(c->qsize);
584 uint16_t qflags = le16_to_cpu(c->cq_flags);
585 uint64_t prp1 = le64_to_cpu(c->prp1);
587 trace_nvme_create_cq(prp1, cqid, vector, qsize, qflags,
588 NVME_CQ_FLAGS_IEN(qflags) != 0);
590 if (unlikely(!cqid || !nvme_check_cqid(n, cqid))) {
591 trace_nvme_err_invalid_create_cq_cqid(cqid);
592 return NVME_INVALID_CQID | NVME_DNR;
594 if (unlikely(!qsize || qsize > NVME_CAP_MQES(n->bar.cap))) {
595 trace_nvme_err_invalid_create_cq_size(qsize);
596 return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
598 if (unlikely(!prp1)) {
599 trace_nvme_err_invalid_create_cq_addr(prp1);
600 return NVME_INVALID_FIELD | NVME_DNR;
602 if (unlikely(vector > n->num_queues)) {
603 trace_nvme_err_invalid_create_cq_vector(vector);
604 return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
606 if (unlikely(!(NVME_CQ_FLAGS_PC(qflags)))) {
607 trace_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags));
608 return NVME_INVALID_FIELD | NVME_DNR;
611 cq = g_malloc0(sizeof(*cq));
612 nvme_init_cq(cq, n, prp1, cqid, vector, qsize + 1,
613 NVME_CQ_FLAGS_IEN(qflags));
614 return NVME_SUCCESS;
617 static uint16_t nvme_identify_ctrl(NvmeCtrl *n, NvmeIdentify *c)
619 uint64_t prp1 = le64_to_cpu(c->prp1);
620 uint64_t prp2 = le64_to_cpu(c->prp2);
622 trace_nvme_identify_ctrl();
624 return nvme_dma_read_prp(n, (uint8_t *)&n->id_ctrl, sizeof(n->id_ctrl),
625 prp1, prp2);
628 static uint16_t nvme_identify_ns(NvmeCtrl *n, NvmeIdentify *c)
630 NvmeNamespace *ns;
631 uint32_t nsid = le32_to_cpu(c->nsid);
632 uint64_t prp1 = le64_to_cpu(c->prp1);
633 uint64_t prp2 = le64_to_cpu(c->prp2);
635 trace_nvme_identify_ns(nsid);
637 if (unlikely(nsid == 0 || nsid > n->num_namespaces)) {
638 trace_nvme_err_invalid_ns(nsid, n->num_namespaces);
639 return NVME_INVALID_NSID | NVME_DNR;
642 ns = &n->namespaces[nsid - 1];
644 return nvme_dma_read_prp(n, (uint8_t *)&ns->id_ns, sizeof(ns->id_ns),
645 prp1, prp2);
648 static uint16_t nvme_identify_nslist(NvmeCtrl *n, NvmeIdentify *c)
650 static const int data_len = 4096;
651 uint32_t min_nsid = le32_to_cpu(c->nsid);
652 uint64_t prp1 = le64_to_cpu(c->prp1);
653 uint64_t prp2 = le64_to_cpu(c->prp2);
654 uint32_t *list;
655 uint16_t ret;
656 int i, j = 0;
658 trace_nvme_identify_nslist(min_nsid);
660 list = g_malloc0(data_len);
661 for (i = 0; i < n->num_namespaces; i++) {
662 if (i < min_nsid) {
663 continue;
665 list[j++] = cpu_to_le32(i + 1);
666 if (j == data_len / sizeof(uint32_t)) {
667 break;
670 ret = nvme_dma_read_prp(n, (uint8_t *)list, data_len, prp1, prp2);
671 g_free(list);
672 return ret;
676 static uint16_t nvme_identify(NvmeCtrl *n, NvmeCmd *cmd)
678 NvmeIdentify *c = (NvmeIdentify *)cmd;
680 switch (le32_to_cpu(c->cns)) {
681 case 0x00:
682 return nvme_identify_ns(n, c);
683 case 0x01:
684 return nvme_identify_ctrl(n, c);
685 case 0x02:
686 return nvme_identify_nslist(n, c);
687 default:
688 trace_nvme_err_invalid_identify_cns(le32_to_cpu(c->cns));
689 return NVME_INVALID_FIELD | NVME_DNR;
693 static uint16_t nvme_get_feature(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
695 uint32_t dw10 = le32_to_cpu(cmd->cdw10);
696 uint32_t result;
698 switch (dw10) {
699 case NVME_VOLATILE_WRITE_CACHE:
700 result = blk_enable_write_cache(n->conf.blk);
701 trace_nvme_getfeat_vwcache(result ? "enabled" : "disabled");
702 break;
703 case NVME_NUMBER_OF_QUEUES:
704 result = cpu_to_le32((n->num_queues - 2) | ((n->num_queues - 2) << 16));
705 trace_nvme_getfeat_numq(result);
706 break;
707 default:
708 trace_nvme_err_invalid_getfeat(dw10);
709 return NVME_INVALID_FIELD | NVME_DNR;
712 req->cqe.result = result;
713 return NVME_SUCCESS;
716 static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
718 uint32_t dw10 = le32_to_cpu(cmd->cdw10);
719 uint32_t dw11 = le32_to_cpu(cmd->cdw11);
721 switch (dw10) {
722 case NVME_VOLATILE_WRITE_CACHE:
723 blk_set_enable_write_cache(n->conf.blk, dw11 & 1);
724 break;
725 case NVME_NUMBER_OF_QUEUES:
726 trace_nvme_setfeat_numq((dw11 & 0xFFFF) + 1,
727 ((dw11 >> 16) & 0xFFFF) + 1,
728 n->num_queues - 1, n->num_queues - 1);
729 req->cqe.result =
730 cpu_to_le32((n->num_queues - 2) | ((n->num_queues - 2) << 16));
731 break;
732 default:
733 trace_nvme_err_invalid_setfeat(dw10);
734 return NVME_INVALID_FIELD | NVME_DNR;
736 return NVME_SUCCESS;
739 static uint16_t nvme_admin_cmd(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
741 switch (cmd->opcode) {
742 case NVME_ADM_CMD_DELETE_SQ:
743 return nvme_del_sq(n, cmd);
744 case NVME_ADM_CMD_CREATE_SQ:
745 return nvme_create_sq(n, cmd);
746 case NVME_ADM_CMD_DELETE_CQ:
747 return nvme_del_cq(n, cmd);
748 case NVME_ADM_CMD_CREATE_CQ:
749 return nvme_create_cq(n, cmd);
750 case NVME_ADM_CMD_IDENTIFY:
751 return nvme_identify(n, cmd);
752 case NVME_ADM_CMD_SET_FEATURES:
753 return nvme_set_feature(n, cmd, req);
754 case NVME_ADM_CMD_GET_FEATURES:
755 return nvme_get_feature(n, cmd, req);
756 default:
757 trace_nvme_err_invalid_admin_opc(cmd->opcode);
758 return NVME_INVALID_OPCODE | NVME_DNR;
762 static void nvme_process_sq(void *opaque)
764 NvmeSQueue *sq = opaque;
765 NvmeCtrl *n = sq->ctrl;
766 NvmeCQueue *cq = n->cq[sq->cqid];
768 uint16_t status;
769 hwaddr addr;
770 NvmeCmd cmd;
771 NvmeRequest *req;
773 while (!(nvme_sq_empty(sq) || QTAILQ_EMPTY(&sq->req_list))) {
774 addr = sq->dma_addr + sq->head * n->sqe_size;
775 nvme_addr_read(n, addr, (void *)&cmd, sizeof(cmd));
776 nvme_inc_sq_head(sq);
778 req = QTAILQ_FIRST(&sq->req_list);
779 QTAILQ_REMOVE(&sq->req_list, req, entry);
780 QTAILQ_INSERT_TAIL(&sq->out_req_list, req, entry);
781 memset(&req->cqe, 0, sizeof(req->cqe));
782 req->cqe.cid = cmd.cid;
784 status = sq->sqid ? nvme_io_cmd(n, &cmd, req) :
785 nvme_admin_cmd(n, &cmd, req);
786 if (status != NVME_NO_COMPLETE) {
787 req->status = status;
788 nvme_enqueue_req_completion(cq, req);
793 static void nvme_clear_ctrl(NvmeCtrl *n)
795 int i;
797 for (i = 0; i < n->num_queues; i++) {
798 if (n->sq[i] != NULL) {
799 nvme_free_sq(n->sq[i], n);
802 for (i = 0; i < n->num_queues; i++) {
803 if (n->cq[i] != NULL) {
804 nvme_free_cq(n->cq[i], n);
808 blk_flush(n->conf.blk);
809 n->bar.cc = 0;
812 static int nvme_start_ctrl(NvmeCtrl *n)
814 uint32_t page_bits = NVME_CC_MPS(n->bar.cc) + 12;
815 uint32_t page_size = 1 << page_bits;
817 if (unlikely(n->cq[0])) {
818 trace_nvme_err_startfail_cq();
819 return -1;
821 if (unlikely(n->sq[0])) {
822 trace_nvme_err_startfail_sq();
823 return -1;
825 if (unlikely(!n->bar.asq)) {
826 trace_nvme_err_startfail_nbarasq();
827 return -1;
829 if (unlikely(!n->bar.acq)) {
830 trace_nvme_err_startfail_nbaracq();
831 return -1;
833 if (unlikely(n->bar.asq & (page_size - 1))) {
834 trace_nvme_err_startfail_asq_misaligned(n->bar.asq);
835 return -1;
837 if (unlikely(n->bar.acq & (page_size - 1))) {
838 trace_nvme_err_startfail_acq_misaligned(n->bar.acq);
839 return -1;
841 if (unlikely(NVME_CC_MPS(n->bar.cc) <
842 NVME_CAP_MPSMIN(n->bar.cap))) {
843 trace_nvme_err_startfail_page_too_small(
844 NVME_CC_MPS(n->bar.cc),
845 NVME_CAP_MPSMIN(n->bar.cap));
846 return -1;
848 if (unlikely(NVME_CC_MPS(n->bar.cc) >
849 NVME_CAP_MPSMAX(n->bar.cap))) {
850 trace_nvme_err_startfail_page_too_large(
851 NVME_CC_MPS(n->bar.cc),
852 NVME_CAP_MPSMAX(n->bar.cap));
853 return -1;
855 if (unlikely(NVME_CC_IOCQES(n->bar.cc) <
856 NVME_CTRL_CQES_MIN(n->id_ctrl.cqes))) {
857 trace_nvme_err_startfail_cqent_too_small(
858 NVME_CC_IOCQES(n->bar.cc),
859 NVME_CTRL_CQES_MIN(n->bar.cap));
860 return -1;
862 if (unlikely(NVME_CC_IOCQES(n->bar.cc) >
863 NVME_CTRL_CQES_MAX(n->id_ctrl.cqes))) {
864 trace_nvme_err_startfail_cqent_too_large(
865 NVME_CC_IOCQES(n->bar.cc),
866 NVME_CTRL_CQES_MAX(n->bar.cap));
867 return -1;
869 if (unlikely(NVME_CC_IOSQES(n->bar.cc) <
870 NVME_CTRL_SQES_MIN(n->id_ctrl.sqes))) {
871 trace_nvme_err_startfail_sqent_too_small(
872 NVME_CC_IOSQES(n->bar.cc),
873 NVME_CTRL_SQES_MIN(n->bar.cap));
874 return -1;
876 if (unlikely(NVME_CC_IOSQES(n->bar.cc) >
877 NVME_CTRL_SQES_MAX(n->id_ctrl.sqes))) {
878 trace_nvme_err_startfail_sqent_too_large(
879 NVME_CC_IOSQES(n->bar.cc),
880 NVME_CTRL_SQES_MAX(n->bar.cap));
881 return -1;
883 if (unlikely(!NVME_AQA_ASQS(n->bar.aqa))) {
884 trace_nvme_err_startfail_asqent_sz_zero();
885 return -1;
887 if (unlikely(!NVME_AQA_ACQS(n->bar.aqa))) {
888 trace_nvme_err_startfail_acqent_sz_zero();
889 return -1;
892 n->page_bits = page_bits;
893 n->page_size = page_size;
894 n->max_prp_ents = n->page_size / sizeof(uint64_t);
895 n->cqe_size = 1 << NVME_CC_IOCQES(n->bar.cc);
896 n->sqe_size = 1 << NVME_CC_IOSQES(n->bar.cc);
897 nvme_init_cq(&n->admin_cq, n, n->bar.acq, 0, 0,
898 NVME_AQA_ACQS(n->bar.aqa) + 1, 1);
899 nvme_init_sq(&n->admin_sq, n, n->bar.asq, 0, 0,
900 NVME_AQA_ASQS(n->bar.aqa) + 1);
902 return 0;
905 static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data,
906 unsigned size)
908 if (unlikely(offset & (sizeof(uint32_t) - 1))) {
909 NVME_GUEST_ERR(nvme_ub_mmiowr_misaligned32,
910 "MMIO write not 32-bit aligned,"
911 " offset=0x%"PRIx64"", offset);
912 /* should be ignored, fall through for now */
915 if (unlikely(size < sizeof(uint32_t))) {
916 NVME_GUEST_ERR(nvme_ub_mmiowr_toosmall,
917 "MMIO write smaller than 32-bits,"
918 " offset=0x%"PRIx64", size=%u",
919 offset, size);
920 /* should be ignored, fall through for now */
923 switch (offset) {
924 case 0xc: /* INTMS */
925 if (unlikely(msix_enabled(&(n->parent_obj)))) {
926 NVME_GUEST_ERR(nvme_ub_mmiowr_intmask_with_msix,
927 "undefined access to interrupt mask set"
928 " when MSI-X is enabled");
929 /* should be ignored, fall through for now */
931 n->bar.intms |= data & 0xffffffff;
932 n->bar.intmc = n->bar.intms;
933 trace_nvme_mmio_intm_set(data & 0xffffffff,
934 n->bar.intmc);
935 nvme_irq_check(n);
936 break;
937 case 0x10: /* INTMC */
938 if (unlikely(msix_enabled(&(n->parent_obj)))) {
939 NVME_GUEST_ERR(nvme_ub_mmiowr_intmask_with_msix,
940 "undefined access to interrupt mask clr"
941 " when MSI-X is enabled");
942 /* should be ignored, fall through for now */
944 n->bar.intms &= ~(data & 0xffffffff);
945 n->bar.intmc = n->bar.intms;
946 trace_nvme_mmio_intm_clr(data & 0xffffffff,
947 n->bar.intmc);
948 nvme_irq_check(n);
949 break;
950 case 0x14: /* CC */
951 trace_nvme_mmio_cfg(data & 0xffffffff);
952 /* Windows first sends data, then sends enable bit */
953 if (!NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc) &&
954 !NVME_CC_SHN(data) && !NVME_CC_SHN(n->bar.cc))
956 n->bar.cc = data;
959 if (NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc)) {
960 n->bar.cc = data;
961 if (unlikely(nvme_start_ctrl(n))) {
962 trace_nvme_err_startfail();
963 n->bar.csts = NVME_CSTS_FAILED;
964 } else {
965 trace_nvme_mmio_start_success();
966 n->bar.csts = NVME_CSTS_READY;
968 } else if (!NVME_CC_EN(data) && NVME_CC_EN(n->bar.cc)) {
969 trace_nvme_mmio_stopped();
970 nvme_clear_ctrl(n);
971 n->bar.csts &= ~NVME_CSTS_READY;
973 if (NVME_CC_SHN(data) && !(NVME_CC_SHN(n->bar.cc))) {
974 trace_nvme_mmio_shutdown_set();
975 nvme_clear_ctrl(n);
976 n->bar.cc = data;
977 n->bar.csts |= NVME_CSTS_SHST_COMPLETE;
978 } else if (!NVME_CC_SHN(data) && NVME_CC_SHN(n->bar.cc)) {
979 trace_nvme_mmio_shutdown_cleared();
980 n->bar.csts &= ~NVME_CSTS_SHST_COMPLETE;
981 n->bar.cc = data;
983 break;
984 case 0x1C: /* CSTS */
985 if (data & (1 << 4)) {
986 NVME_GUEST_ERR(nvme_ub_mmiowr_ssreset_w1c_unsupported,
987 "attempted to W1C CSTS.NSSRO"
988 " but CAP.NSSRS is zero (not supported)");
989 } else if (data != 0) {
990 NVME_GUEST_ERR(nvme_ub_mmiowr_ro_csts,
991 "attempted to set a read only bit"
992 " of controller status");
994 break;
995 case 0x20: /* NSSR */
996 if (data == 0x4E564D65) {
997 trace_nvme_ub_mmiowr_ssreset_unsupported();
998 } else {
999 /* The spec says that writes of other values have no effect */
1000 return;
1002 break;
1003 case 0x24: /* AQA */
1004 n->bar.aqa = data & 0xffffffff;
1005 trace_nvme_mmio_aqattr(data & 0xffffffff);
1006 break;
1007 case 0x28: /* ASQ */
1008 n->bar.asq = data;
1009 trace_nvme_mmio_asqaddr(data);
1010 break;
1011 case 0x2c: /* ASQ hi */
1012 n->bar.asq |= data << 32;
1013 trace_nvme_mmio_asqaddr_hi(data, n->bar.asq);
1014 break;
1015 case 0x30: /* ACQ */
1016 trace_nvme_mmio_acqaddr(data);
1017 n->bar.acq = data;
1018 break;
1019 case 0x34: /* ACQ hi */
1020 n->bar.acq |= data << 32;
1021 trace_nvme_mmio_acqaddr_hi(data, n->bar.acq);
1022 break;
1023 case 0x38: /* CMBLOC */
1024 NVME_GUEST_ERR(nvme_ub_mmiowr_cmbloc_reserved,
1025 "invalid write to reserved CMBLOC"
1026 " when CMBSZ is zero, ignored");
1027 return;
1028 case 0x3C: /* CMBSZ */
1029 NVME_GUEST_ERR(nvme_ub_mmiowr_cmbsz_readonly,
1030 "invalid write to read only CMBSZ, ignored");
1031 return;
1032 default:
1033 NVME_GUEST_ERR(nvme_ub_mmiowr_invalid,
1034 "invalid MMIO write,"
1035 " offset=0x%"PRIx64", data=%"PRIx64"",
1036 offset, data);
1037 break;
1041 static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
1043 NvmeCtrl *n = (NvmeCtrl *)opaque;
1044 uint8_t *ptr = (uint8_t *)&n->bar;
1045 uint64_t val = 0;
1047 if (unlikely(addr & (sizeof(uint32_t) - 1))) {
1048 NVME_GUEST_ERR(nvme_ub_mmiord_misaligned32,
1049 "MMIO read not 32-bit aligned,"
1050 " offset=0x%"PRIx64"", addr);
1051 /* should RAZ, fall through for now */
1052 } else if (unlikely(size < sizeof(uint32_t))) {
1053 NVME_GUEST_ERR(nvme_ub_mmiord_toosmall,
1054 "MMIO read smaller than 32-bits,"
1055 " offset=0x%"PRIx64"", addr);
1056 /* should RAZ, fall through for now */
1059 if (addr < sizeof(n->bar)) {
1060 memcpy(&val, ptr + addr, size);
1061 } else {
1062 NVME_GUEST_ERR(nvme_ub_mmiord_invalid_ofs,
1063 "MMIO read beyond last register,"
1064 " offset=0x%"PRIx64", returning 0", addr);
1067 return val;
1070 static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
1072 uint32_t qid;
1074 if (unlikely(addr & ((1 << 2) - 1))) {
1075 NVME_GUEST_ERR(nvme_ub_db_wr_misaligned,
1076 "doorbell write not 32-bit aligned,"
1077 " offset=0x%"PRIx64", ignoring", addr);
1078 return;
1081 if (((addr - 0x1000) >> 2) & 1) {
1082 /* Completion queue doorbell write */
1084 uint16_t new_head = val & 0xffff;
1085 int start_sqs;
1086 NvmeCQueue *cq;
1088 qid = (addr - (0x1000 + (1 << 2))) >> 3;
1089 if (unlikely(nvme_check_cqid(n, qid))) {
1090 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_cq,
1091 "completion queue doorbell write"
1092 " for nonexistent queue,"
1093 " sqid=%"PRIu32", ignoring", qid);
1094 return;
1097 cq = n->cq[qid];
1098 if (unlikely(new_head >= cq->size)) {
1099 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_cqhead,
1100 "completion queue doorbell write value"
1101 " beyond queue size, sqid=%"PRIu32","
1102 " new_head=%"PRIu16", ignoring",
1103 qid, new_head);
1104 return;
1107 start_sqs = nvme_cq_full(cq) ? 1 : 0;
1108 cq->head = new_head;
1109 if (start_sqs) {
1110 NvmeSQueue *sq;
1111 QTAILQ_FOREACH(sq, &cq->sq_list, entry) {
1112 timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
1114 timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
1117 if (cq->tail == cq->head) {
1118 nvme_irq_deassert(n, cq);
1120 } else {
1121 /* Submission queue doorbell write */
1123 uint16_t new_tail = val & 0xffff;
1124 NvmeSQueue *sq;
1126 qid = (addr - 0x1000) >> 3;
1127 if (unlikely(nvme_check_sqid(n, qid))) {
1128 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_sq,
1129 "submission queue doorbell write"
1130 " for nonexistent queue,"
1131 " sqid=%"PRIu32", ignoring", qid);
1132 return;
1135 sq = n->sq[qid];
1136 if (unlikely(new_tail >= sq->size)) {
1137 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_sqtail,
1138 "submission queue doorbell write value"
1139 " beyond queue size, sqid=%"PRIu32","
1140 " new_tail=%"PRIu16", ignoring",
1141 qid, new_tail);
1142 return;
1145 sq->tail = new_tail;
1146 timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
1150 static void nvme_mmio_write(void *opaque, hwaddr addr, uint64_t data,
1151 unsigned size)
1153 NvmeCtrl *n = (NvmeCtrl *)opaque;
1154 if (addr < sizeof(n->bar)) {
1155 nvme_write_bar(n, addr, data, size);
1156 } else if (addr >= 0x1000) {
1157 nvme_process_db(n, addr, data);
1161 static const MemoryRegionOps nvme_mmio_ops = {
1162 .read = nvme_mmio_read,
1163 .write = nvme_mmio_write,
1164 .endianness = DEVICE_LITTLE_ENDIAN,
1165 .impl = {
1166 .min_access_size = 2,
1167 .max_access_size = 8,
1171 static void nvme_cmb_write(void *opaque, hwaddr addr, uint64_t data,
1172 unsigned size)
1174 NvmeCtrl *n = (NvmeCtrl *)opaque;
1175 memcpy(&n->cmbuf[addr], &data, size);
1178 static uint64_t nvme_cmb_read(void *opaque, hwaddr addr, unsigned size)
1180 uint64_t val;
1181 NvmeCtrl *n = (NvmeCtrl *)opaque;
1183 memcpy(&val, &n->cmbuf[addr], size);
1184 return val;
1187 static const MemoryRegionOps nvme_cmb_ops = {
1188 .read = nvme_cmb_read,
1189 .write = nvme_cmb_write,
1190 .endianness = DEVICE_LITTLE_ENDIAN,
1191 .impl = {
1192 .min_access_size = 2,
1193 .max_access_size = 8,
1197 static void nvme_realize(PCIDevice *pci_dev, Error **errp)
1199 NvmeCtrl *n = NVME(pci_dev);
1200 NvmeIdCtrl *id = &n->id_ctrl;
1202 int i;
1203 int64_t bs_size;
1204 uint8_t *pci_conf;
1206 if (!n->conf.blk) {
1207 error_setg(errp, "drive property not set");
1208 return;
1211 bs_size = blk_getlength(n->conf.blk);
1212 if (bs_size < 0) {
1213 error_setg(errp, "could not get backing file size");
1214 return;
1217 blkconf_serial(&n->conf, &n->serial);
1218 if (!n->serial) {
1219 error_setg(errp, "serial property not set");
1220 return;
1222 blkconf_blocksizes(&n->conf);
1223 if (!blkconf_apply_backend_options(&n->conf, blk_is_read_only(n->conf.blk),
1224 false, errp)) {
1225 return;
1228 pci_conf = pci_dev->config;
1229 pci_conf[PCI_INTERRUPT_PIN] = 1;
1230 pci_config_set_prog_interface(pci_dev->config, 0x2);
1231 pci_config_set_class(pci_dev->config, PCI_CLASS_STORAGE_EXPRESS);
1232 pcie_endpoint_cap_init(&n->parent_obj, 0x80);
1234 n->num_namespaces = 1;
1235 n->num_queues = 64;
1236 n->reg_size = pow2ceil(0x1004 + 2 * (n->num_queues + 1) * 4);
1237 n->ns_size = bs_size / (uint64_t)n->num_namespaces;
1239 n->namespaces = g_new0(NvmeNamespace, n->num_namespaces);
1240 n->sq = g_new0(NvmeSQueue *, n->num_queues);
1241 n->cq = g_new0(NvmeCQueue *, n->num_queues);
1243 memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n,
1244 "nvme", n->reg_size);
1245 pci_register_bar(&n->parent_obj, 0,
1246 PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64,
1247 &n->iomem);
1248 msix_init_exclusive_bar(&n->parent_obj, n->num_queues, 4, NULL);
1250 id->vid = cpu_to_le16(pci_get_word(pci_conf + PCI_VENDOR_ID));
1251 id->ssvid = cpu_to_le16(pci_get_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID));
1252 strpadcpy((char *)id->mn, sizeof(id->mn), "QEMU NVMe Ctrl", ' ');
1253 strpadcpy((char *)id->fr, sizeof(id->fr), "1.0", ' ');
1254 strpadcpy((char *)id->sn, sizeof(id->sn), n->serial, ' ');
1255 id->rab = 6;
1256 id->ieee[0] = 0x00;
1257 id->ieee[1] = 0x02;
1258 id->ieee[2] = 0xb3;
1259 id->oacs = cpu_to_le16(0);
1260 id->frmw = 7 << 1;
1261 id->lpa = 1 << 0;
1262 id->sqes = (0x6 << 4) | 0x6;
1263 id->cqes = (0x4 << 4) | 0x4;
1264 id->nn = cpu_to_le32(n->num_namespaces);
1265 id->oncs = cpu_to_le16(NVME_ONCS_WRITE_ZEROS);
1266 id->psd[0].mp = cpu_to_le16(0x9c4);
1267 id->psd[0].enlat = cpu_to_le32(0x10);
1268 id->psd[0].exlat = cpu_to_le32(0x4);
1269 if (blk_enable_write_cache(n->conf.blk)) {
1270 id->vwc = 1;
1273 n->bar.cap = 0;
1274 NVME_CAP_SET_MQES(n->bar.cap, 0x7ff);
1275 NVME_CAP_SET_CQR(n->bar.cap, 1);
1276 NVME_CAP_SET_AMS(n->bar.cap, 1);
1277 NVME_CAP_SET_TO(n->bar.cap, 0xf);
1278 NVME_CAP_SET_CSS(n->bar.cap, 1);
1279 NVME_CAP_SET_MPSMAX(n->bar.cap, 4);
1281 n->bar.vs = 0x00010200;
1282 n->bar.intmc = n->bar.intms = 0;
1284 if (n->cmb_size_mb) {
1286 NVME_CMBLOC_SET_BIR(n->bar.cmbloc, 2);
1287 NVME_CMBLOC_SET_OFST(n->bar.cmbloc, 0);
1289 NVME_CMBSZ_SET_SQS(n->bar.cmbsz, 1);
1290 NVME_CMBSZ_SET_CQS(n->bar.cmbsz, 0);
1291 NVME_CMBSZ_SET_LISTS(n->bar.cmbsz, 0);
1292 NVME_CMBSZ_SET_RDS(n->bar.cmbsz, 1);
1293 NVME_CMBSZ_SET_WDS(n->bar.cmbsz, 1);
1294 NVME_CMBSZ_SET_SZU(n->bar.cmbsz, 2); /* MBs */
1295 NVME_CMBSZ_SET_SZ(n->bar.cmbsz, n->cmb_size_mb);
1297 n->cmbloc = n->bar.cmbloc;
1298 n->cmbsz = n->bar.cmbsz;
1300 n->cmbuf = g_malloc0(NVME_CMBSZ_GETSIZE(n->bar.cmbsz));
1301 memory_region_init_io(&n->ctrl_mem, OBJECT(n), &nvme_cmb_ops, n,
1302 "nvme-cmb", NVME_CMBSZ_GETSIZE(n->bar.cmbsz));
1303 pci_register_bar(&n->parent_obj, NVME_CMBLOC_BIR(n->bar.cmbloc),
1304 PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64 |
1305 PCI_BASE_ADDRESS_MEM_PREFETCH, &n->ctrl_mem);
1309 for (i = 0; i < n->num_namespaces; i++) {
1310 NvmeNamespace *ns = &n->namespaces[i];
1311 NvmeIdNs *id_ns = &ns->id_ns;
1312 id_ns->nsfeat = 0;
1313 id_ns->nlbaf = 0;
1314 id_ns->flbas = 0;
1315 id_ns->mc = 0;
1316 id_ns->dpc = 0;
1317 id_ns->dps = 0;
1318 id_ns->lbaf[0].ds = BDRV_SECTOR_BITS;
1319 id_ns->ncap = id_ns->nuse = id_ns->nsze =
1320 cpu_to_le64(n->ns_size >>
1321 id_ns->lbaf[NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas)].ds);
1325 static void nvme_exit(PCIDevice *pci_dev)
1327 NvmeCtrl *n = NVME(pci_dev);
1329 nvme_clear_ctrl(n);
1330 g_free(n->namespaces);
1331 g_free(n->cq);
1332 g_free(n->sq);
1333 if (n->cmbsz) {
1334 memory_region_unref(&n->ctrl_mem);
1337 msix_uninit_exclusive_bar(pci_dev);
1340 static Property nvme_props[] = {
1341 DEFINE_BLOCK_PROPERTIES(NvmeCtrl, conf),
1342 DEFINE_PROP_STRING("serial", NvmeCtrl, serial),
1343 DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl, cmb_size_mb, 0),
1344 DEFINE_PROP_END_OF_LIST(),
1347 static const VMStateDescription nvme_vmstate = {
1348 .name = "nvme",
1349 .unmigratable = 1,
1352 static void nvme_class_init(ObjectClass *oc, void *data)
1354 DeviceClass *dc = DEVICE_CLASS(oc);
1355 PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
1357 pc->realize = nvme_realize;
1358 pc->exit = nvme_exit;
1359 pc->class_id = PCI_CLASS_STORAGE_EXPRESS;
1360 pc->vendor_id = PCI_VENDOR_ID_INTEL;
1361 pc->device_id = 0x5845;
1362 pc->revision = 2;
1364 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1365 dc->desc = "Non-Volatile Memory Express";
1366 dc->props = nvme_props;
1367 dc->vmsd = &nvme_vmstate;
1370 static void nvme_instance_init(Object *obj)
1372 NvmeCtrl *s = NVME(obj);
1374 device_add_bootindex_property(obj, &s->conf.bootindex,
1375 "bootindex", "/namespace@1,0",
1376 DEVICE(obj), &error_abort);
1379 static const TypeInfo nvme_info = {
1380 .name = "nvme",
1381 .parent = TYPE_PCI_DEVICE,
1382 .instance_size = sizeof(NvmeCtrl),
1383 .class_init = nvme_class_init,
1384 .instance_init = nvme_instance_init,
1385 .interfaces = (InterfaceInfo[]) {
1386 { INTERFACE_PCIE_DEVICE },
1391 static void nvme_register_types(void)
1393 type_register_static(&nvme_info);
1396 type_init(nvme_register_types)