gdbstub: Fix qOffsets packet detection
[qemu/qmp-unstable.git] / hw / block / nvme.c
blob4b6d5e607860c7929de19c6d0ab00832013c9dd0
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.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]>
23 #include <hw/block/block.h>
24 #include <hw/hw.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"
31 #include "nvme.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)
47 cq->tail++;
48 if (cq->tail >= cq->size) {
49 cq->tail = 0;
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);
74 } else {
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;
87 if (!prp1) {
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);
93 len -= trans_len;
94 if (len) {
95 if (!prp2) {
96 goto unmap;
98 if (len > n->page_size) {
99 uint64_t prp_list[n->max_prp_ents];
100 uint32_t nents, prp_trans;
101 int i = 0;
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);
106 while (len != 0) {
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)) {
111 goto unmap;
114 i = 0;
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,
118 prp_trans);
119 prp_ent = le64_to_cpu(prp_list[i]);
122 if (!prp_ent || prp_ent & (n->page_size - 1)) {
123 goto unmap;
126 trans_len = MIN(len, n->page_size);
127 qemu_sglist_add(qsg, prp_ent, trans_len);
128 len -= trans_len;
129 i++;
131 } else {
132 if (prp2 & (n->page_size - 1)) {
133 goto unmap;
135 qemu_sglist_add(qsg, prp2, len);
138 return NVME_SUCCESS;
140 unmap:
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)
148 QEMUSGList qsg;
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 return NVME_SUCCESS;
160 static void nvme_post_cqes(void *opaque)
162 NvmeCQueue *cq = opaque;
163 NvmeCtrl *n = cq->ctrl;
164 NvmeRequest *req, *next;
166 QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
167 NvmeSQueue *sq;
168 hwaddr addr;
170 if (nvme_cq_full(cq)) {
171 break;
174 QTAILQ_REMOVE(&cq->req_list, req, entry);
175 sq = req->sq;
176 req->cqe.status = cpu_to_le16((req->status << 1) | cq->phase);
177 req->cqe.sq_id = cpu_to_le16(sq->sqid);
178 req->cqe.sq_head = cpu_to_le16(sq->head);
179 addr = cq->dma_addr + cq->tail * n->cqe_size;
180 nvme_inc_cq_tail(cq);
181 pci_dma_write(&n->parent_obj, addr, (void *)&req->cqe,
182 sizeof(req->cqe));
183 QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
185 nvme_isr_notify(n, cq);
188 static void nvme_enqueue_req_completion(NvmeCQueue *cq, NvmeRequest *req)
190 assert(cq->cqid == req->sq->cqid);
191 QTAILQ_REMOVE(&req->sq->out_req_list, req, entry);
192 QTAILQ_INSERT_TAIL(&cq->req_list, req, entry);
193 timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
196 static void nvme_rw_cb(void *opaque, int ret)
198 NvmeRequest *req = opaque;
199 NvmeSQueue *sq = req->sq;
200 NvmeCtrl *n = sq->ctrl;
201 NvmeCQueue *cq = n->cq[sq->cqid];
203 block_acct_done(blk_get_stats(n->conf.blk), &req->acct);
204 if (!ret) {
205 req->status = NVME_SUCCESS;
206 } else {
207 req->status = NVME_INTERNAL_DEV_ERROR;
210 qemu_sglist_destroy(&req->qsg);
211 nvme_enqueue_req_completion(cq, req);
214 static uint16_t nvme_rw(NvmeCtrl *n, NvmeNamespace *ns, NvmeCmd *cmd,
215 NvmeRequest *req)
217 NvmeRwCmd *rw = (NvmeRwCmd *)cmd;
218 uint32_t nlb = le32_to_cpu(rw->nlb) + 1;
219 uint64_t slba = le64_to_cpu(rw->slba);
220 uint64_t prp1 = le64_to_cpu(rw->prp1);
221 uint64_t prp2 = le64_to_cpu(rw->prp2);
223 uint8_t lba_index = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
224 uint8_t data_shift = ns->id_ns.lbaf[lba_index].ds;
225 uint64_t data_size = (uint64_t)nlb << data_shift;
226 uint64_t aio_slba = slba << (data_shift - BDRV_SECTOR_BITS);
227 int is_write = rw->opcode == NVME_CMD_WRITE ? 1 : 0;
229 if ((slba + nlb) > ns->id_ns.nsze) {
230 return NVME_LBA_RANGE | NVME_DNR;
232 if (nvme_map_prp(&req->qsg, prp1, prp2, data_size, n)) {
233 return NVME_INVALID_FIELD | NVME_DNR;
235 assert((nlb << data_shift) == req->qsg.size);
237 dma_acct_start(n->conf.blk, &req->acct, &req->qsg,
238 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
239 req->aiocb = is_write ?
240 dma_blk_write(n->conf.blk, &req->qsg, aio_slba, nvme_rw_cb, req) :
241 dma_blk_read(n->conf.blk, &req->qsg, aio_slba, nvme_rw_cb, req);
243 return NVME_NO_COMPLETE;
246 static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
248 NvmeNamespace *ns;
249 uint32_t nsid = le32_to_cpu(cmd->nsid);
251 if (nsid == 0 || nsid > n->num_namespaces) {
252 return NVME_INVALID_NSID | NVME_DNR;
255 ns = &n->namespaces[nsid - 1];
256 switch (cmd->opcode) {
257 case NVME_CMD_FLUSH:
258 return NVME_SUCCESS;
259 case NVME_CMD_WRITE:
260 case NVME_CMD_READ:
261 return nvme_rw(n, ns, cmd, req);
262 default:
263 return NVME_INVALID_OPCODE | NVME_DNR;
267 static void nvme_free_sq(NvmeSQueue *sq, NvmeCtrl *n)
269 n->sq[sq->sqid] = NULL;
270 timer_del(sq->timer);
271 timer_free(sq->timer);
272 g_free(sq->io_req);
273 if (sq->sqid) {
274 g_free(sq);
278 static uint16_t nvme_del_sq(NvmeCtrl *n, NvmeCmd *cmd)
280 NvmeDeleteQ *c = (NvmeDeleteQ *)cmd;
281 NvmeRequest *req, *next;
282 NvmeSQueue *sq;
283 NvmeCQueue *cq;
284 uint16_t qid = le16_to_cpu(c->qid);
286 if (!qid || nvme_check_sqid(n, qid)) {
287 return NVME_INVALID_QID | NVME_DNR;
290 sq = n->sq[qid];
291 while (!QTAILQ_EMPTY(&sq->out_req_list)) {
292 req = QTAILQ_FIRST(&sq->out_req_list);
293 assert(req->aiocb);
294 blk_aio_cancel(req->aiocb);
296 if (!nvme_check_cqid(n, sq->cqid)) {
297 cq = n->cq[sq->cqid];
298 QTAILQ_REMOVE(&cq->sq_list, sq, entry);
300 nvme_post_cqes(cq);
301 QTAILQ_FOREACH_SAFE(req, &cq->req_list, entry, next) {
302 if (req->sq == sq) {
303 QTAILQ_REMOVE(&cq->req_list, req, entry);
304 QTAILQ_INSERT_TAIL(&sq->req_list, req, entry);
309 nvme_free_sq(sq, n);
310 return NVME_SUCCESS;
313 static void nvme_init_sq(NvmeSQueue *sq, NvmeCtrl *n, uint64_t dma_addr,
314 uint16_t sqid, uint16_t cqid, uint16_t size)
316 int i;
317 NvmeCQueue *cq;
319 sq->ctrl = n;
320 sq->dma_addr = dma_addr;
321 sq->sqid = sqid;
322 sq->size = size;
323 sq->cqid = cqid;
324 sq->head = sq->tail = 0;
325 sq->io_req = g_new(NvmeRequest, sq->size);
327 QTAILQ_INIT(&sq->req_list);
328 QTAILQ_INIT(&sq->out_req_list);
329 for (i = 0; i < sq->size; i++) {
330 sq->io_req[i].sq = sq;
331 QTAILQ_INSERT_TAIL(&(sq->req_list), &sq->io_req[i], entry);
333 sq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_process_sq, sq);
335 assert(n->cq[cqid]);
336 cq = n->cq[cqid];
337 QTAILQ_INSERT_TAIL(&(cq->sq_list), sq, entry);
338 n->sq[sqid] = sq;
341 static uint16_t nvme_create_sq(NvmeCtrl *n, NvmeCmd *cmd)
343 NvmeSQueue *sq;
344 NvmeCreateSq *c = (NvmeCreateSq *)cmd;
346 uint16_t cqid = le16_to_cpu(c->cqid);
347 uint16_t sqid = le16_to_cpu(c->sqid);
348 uint16_t qsize = le16_to_cpu(c->qsize);
349 uint16_t qflags = le16_to_cpu(c->sq_flags);
350 uint64_t prp1 = le64_to_cpu(c->prp1);
352 if (!cqid || nvme_check_cqid(n, cqid)) {
353 return NVME_INVALID_CQID | NVME_DNR;
355 if (!sqid || (sqid && !nvme_check_sqid(n, sqid))) {
356 return NVME_INVALID_QID | NVME_DNR;
358 if (!qsize || qsize > NVME_CAP_MQES(n->bar.cap)) {
359 return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
361 if (!prp1 || prp1 & (n->page_size - 1)) {
362 return NVME_INVALID_FIELD | NVME_DNR;
364 if (!(NVME_SQ_FLAGS_PC(qflags))) {
365 return NVME_INVALID_FIELD | NVME_DNR;
367 sq = g_malloc0(sizeof(*sq));
368 nvme_init_sq(sq, n, prp1, sqid, cqid, qsize + 1);
369 return NVME_SUCCESS;
372 static void nvme_free_cq(NvmeCQueue *cq, NvmeCtrl *n)
374 n->cq[cq->cqid] = NULL;
375 timer_del(cq->timer);
376 timer_free(cq->timer);
377 msix_vector_unuse(&n->parent_obj, cq->vector);
378 if (cq->cqid) {
379 g_free(cq);
383 static uint16_t nvme_del_cq(NvmeCtrl *n, NvmeCmd *cmd)
385 NvmeDeleteQ *c = (NvmeDeleteQ *)cmd;
386 NvmeCQueue *cq;
387 uint16_t qid = le16_to_cpu(c->qid);
389 if (!qid || nvme_check_cqid(n, qid)) {
390 return NVME_INVALID_CQID | NVME_DNR;
393 cq = n->cq[qid];
394 if (!QTAILQ_EMPTY(&cq->sq_list)) {
395 return NVME_INVALID_QUEUE_DEL;
397 nvme_free_cq(cq, n);
398 return NVME_SUCCESS;
401 static void nvme_init_cq(NvmeCQueue *cq, NvmeCtrl *n, uint64_t dma_addr,
402 uint16_t cqid, uint16_t vector, uint16_t size, uint16_t irq_enabled)
404 cq->ctrl = n;
405 cq->cqid = cqid;
406 cq->size = size;
407 cq->dma_addr = dma_addr;
408 cq->phase = 1;
409 cq->irq_enabled = irq_enabled;
410 cq->vector = vector;
411 cq->head = cq->tail = 0;
412 QTAILQ_INIT(&cq->req_list);
413 QTAILQ_INIT(&cq->sq_list);
414 msix_vector_use(&n->parent_obj, cq->vector);
415 n->cq[cqid] = cq;
416 cq->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, nvme_post_cqes, cq);
419 static uint16_t nvme_create_cq(NvmeCtrl *n, NvmeCmd *cmd)
421 NvmeCQueue *cq;
422 NvmeCreateCq *c = (NvmeCreateCq *)cmd;
423 uint16_t cqid = le16_to_cpu(c->cqid);
424 uint16_t vector = le16_to_cpu(c->irq_vector);
425 uint16_t qsize = le16_to_cpu(c->qsize);
426 uint16_t qflags = le16_to_cpu(c->cq_flags);
427 uint64_t prp1 = le64_to_cpu(c->prp1);
429 if (!cqid || (cqid && !nvme_check_cqid(n, cqid))) {
430 return NVME_INVALID_CQID | NVME_DNR;
432 if (!qsize || qsize > NVME_CAP_MQES(n->bar.cap)) {
433 return NVME_MAX_QSIZE_EXCEEDED | NVME_DNR;
435 if (!prp1) {
436 return NVME_INVALID_FIELD | NVME_DNR;
438 if (vector > n->num_queues) {
439 return NVME_INVALID_IRQ_VECTOR | NVME_DNR;
441 if (!(NVME_CQ_FLAGS_PC(qflags))) {
442 return NVME_INVALID_FIELD | NVME_DNR;
445 cq = g_malloc0(sizeof(*cq));
446 nvme_init_cq(cq, n, prp1, cqid, vector, qsize + 1,
447 NVME_CQ_FLAGS_IEN(qflags));
448 return NVME_SUCCESS;
451 static uint16_t nvme_identify(NvmeCtrl *n, NvmeCmd *cmd)
453 NvmeNamespace *ns;
454 NvmeIdentify *c = (NvmeIdentify *)cmd;
455 uint32_t cns = le32_to_cpu(c->cns);
456 uint32_t nsid = le32_to_cpu(c->nsid);
457 uint64_t prp1 = le64_to_cpu(c->prp1);
458 uint64_t prp2 = le64_to_cpu(c->prp2);
460 if (cns) {
461 return nvme_dma_read_prp(n, (uint8_t *)&n->id_ctrl, sizeof(n->id_ctrl),
462 prp1, prp2);
464 if (nsid == 0 || nsid > n->num_namespaces) {
465 return NVME_INVALID_NSID | NVME_DNR;
468 ns = &n->namespaces[nsid - 1];
469 return nvme_dma_read_prp(n, (uint8_t *)&ns->id_ns, sizeof(ns->id_ns),
470 prp1, prp2);
473 static uint16_t nvme_get_feature(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
475 uint32_t dw10 = le32_to_cpu(cmd->cdw10);
477 switch (dw10) {
478 case NVME_NUMBER_OF_QUEUES:
479 req->cqe.result =
480 cpu_to_le32((n->num_queues - 1) | ((n->num_queues - 1) << 16));
481 break;
482 case NVME_VOLATILE_WRITE_CACHE:
483 req->cqe.result = cpu_to_le32(1);
484 break;
485 default:
486 return NVME_INVALID_FIELD | NVME_DNR;
488 return NVME_SUCCESS;
491 static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
493 uint32_t dw10 = le32_to_cpu(cmd->cdw10);
495 switch (dw10) {
496 case NVME_NUMBER_OF_QUEUES:
497 req->cqe.result =
498 cpu_to_le32((n->num_queues - 1) | ((n->num_queues - 1) << 16));
499 break;
500 default:
501 return NVME_INVALID_FIELD | NVME_DNR;
503 return NVME_SUCCESS;
506 static uint16_t nvme_admin_cmd(NvmeCtrl *n, NvmeCmd *cmd, NvmeRequest *req)
508 switch (cmd->opcode) {
509 case NVME_ADM_CMD_DELETE_SQ:
510 return nvme_del_sq(n, cmd);
511 case NVME_ADM_CMD_CREATE_SQ:
512 return nvme_create_sq(n, cmd);
513 case NVME_ADM_CMD_DELETE_CQ:
514 return nvme_del_cq(n, cmd);
515 case NVME_ADM_CMD_CREATE_CQ:
516 return nvme_create_cq(n, cmd);
517 case NVME_ADM_CMD_IDENTIFY:
518 return nvme_identify(n, cmd);
519 case NVME_ADM_CMD_SET_FEATURES:
520 return nvme_set_feature(n, cmd, req);
521 case NVME_ADM_CMD_GET_FEATURES:
522 return nvme_get_feature(n, cmd, req);
523 default:
524 return NVME_INVALID_OPCODE | NVME_DNR;
528 static void nvme_process_sq(void *opaque)
530 NvmeSQueue *sq = opaque;
531 NvmeCtrl *n = sq->ctrl;
532 NvmeCQueue *cq = n->cq[sq->cqid];
534 uint16_t status;
535 hwaddr addr;
536 NvmeCmd cmd;
537 NvmeRequest *req;
539 while (!(nvme_sq_empty(sq) || QTAILQ_EMPTY(&sq->req_list))) {
540 addr = sq->dma_addr + sq->head * n->sqe_size;
541 pci_dma_read(&n->parent_obj, addr, (void *)&cmd, sizeof(cmd));
542 nvme_inc_sq_head(sq);
544 req = QTAILQ_FIRST(&sq->req_list);
545 QTAILQ_REMOVE(&sq->req_list, req, entry);
546 QTAILQ_INSERT_TAIL(&sq->out_req_list, req, entry);
547 memset(&req->cqe, 0, sizeof(req->cqe));
548 req->cqe.cid = cmd.cid;
550 status = sq->sqid ? nvme_io_cmd(n, &cmd, req) :
551 nvme_admin_cmd(n, &cmd, req);
552 if (status != NVME_NO_COMPLETE) {
553 req->status = status;
554 nvme_enqueue_req_completion(cq, req);
559 static void nvme_clear_ctrl(NvmeCtrl *n)
561 int i;
563 for (i = 0; i < n->num_queues; i++) {
564 if (n->sq[i] != NULL) {
565 nvme_free_sq(n->sq[i], n);
568 for (i = 0; i < n->num_queues; i++) {
569 if (n->cq[i] != NULL) {
570 nvme_free_cq(n->cq[i], n);
574 blk_flush(n->conf.blk);
575 n->bar.cc = 0;
578 static int nvme_start_ctrl(NvmeCtrl *n)
580 uint32_t page_bits = NVME_CC_MPS(n->bar.cc) + 12;
581 uint32_t page_size = 1 << page_bits;
583 if (n->cq[0] || n->sq[0] || !n->bar.asq || !n->bar.acq ||
584 n->bar.asq & (page_size - 1) || n->bar.acq & (page_size - 1) ||
585 NVME_CC_MPS(n->bar.cc) < NVME_CAP_MPSMIN(n->bar.cap) ||
586 NVME_CC_MPS(n->bar.cc) > NVME_CAP_MPSMAX(n->bar.cap) ||
587 NVME_CC_IOCQES(n->bar.cc) < NVME_CTRL_CQES_MIN(n->id_ctrl.cqes) ||
588 NVME_CC_IOCQES(n->bar.cc) > NVME_CTRL_CQES_MAX(n->id_ctrl.cqes) ||
589 NVME_CC_IOSQES(n->bar.cc) < NVME_CTRL_SQES_MIN(n->id_ctrl.sqes) ||
590 NVME_CC_IOSQES(n->bar.cc) > NVME_CTRL_SQES_MAX(n->id_ctrl.sqes) ||
591 !NVME_AQA_ASQS(n->bar.aqa) || !NVME_AQA_ACQS(n->bar.aqa)) {
592 return -1;
595 n->page_bits = page_bits;
596 n->page_size = page_size;
597 n->max_prp_ents = n->page_size / sizeof(uint64_t);
598 n->cqe_size = 1 << NVME_CC_IOCQES(n->bar.cc);
599 n->sqe_size = 1 << NVME_CC_IOSQES(n->bar.cc);
600 nvme_init_cq(&n->admin_cq, n, n->bar.acq, 0, 0,
601 NVME_AQA_ACQS(n->bar.aqa) + 1, 1);
602 nvme_init_sq(&n->admin_sq, n, n->bar.asq, 0, 0,
603 NVME_AQA_ASQS(n->bar.aqa) + 1);
605 return 0;
608 static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data,
609 unsigned size)
611 switch (offset) {
612 case 0xc:
613 n->bar.intms |= data & 0xffffffff;
614 n->bar.intmc = n->bar.intms;
615 break;
616 case 0x10:
617 n->bar.intms &= ~(data & 0xffffffff);
618 n->bar.intmc = n->bar.intms;
619 break;
620 case 0x14:
621 /* Windows first sends data, then sends enable bit */
622 if (!NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc) &&
623 !NVME_CC_SHN(data) && !NVME_CC_SHN(n->bar.cc))
625 n->bar.cc = data;
628 if (NVME_CC_EN(data) && !NVME_CC_EN(n->bar.cc)) {
629 n->bar.cc = data;
630 if (nvme_start_ctrl(n)) {
631 n->bar.csts = NVME_CSTS_FAILED;
632 } else {
633 n->bar.csts = NVME_CSTS_READY;
635 } else if (!NVME_CC_EN(data) && NVME_CC_EN(n->bar.cc)) {
636 nvme_clear_ctrl(n);
637 n->bar.csts &= ~NVME_CSTS_READY;
639 if (NVME_CC_SHN(data) && !(NVME_CC_SHN(n->bar.cc))) {
640 nvme_clear_ctrl(n);
641 n->bar.cc = data;
642 n->bar.csts |= NVME_CSTS_SHST_COMPLETE;
643 } else if (!NVME_CC_SHN(data) && NVME_CC_SHN(n->bar.cc)) {
644 n->bar.csts &= ~NVME_CSTS_SHST_COMPLETE;
645 n->bar.cc = data;
647 break;
648 case 0x24:
649 n->bar.aqa = data & 0xffffffff;
650 break;
651 case 0x28:
652 n->bar.asq = data;
653 break;
654 case 0x2c:
655 n->bar.asq |= data << 32;
656 break;
657 case 0x30:
658 n->bar.acq = data;
659 break;
660 case 0x34:
661 n->bar.acq |= data << 32;
662 break;
663 default:
664 break;
668 static uint64_t nvme_mmio_read(void *opaque, hwaddr addr, unsigned size)
670 NvmeCtrl *n = (NvmeCtrl *)opaque;
671 uint8_t *ptr = (uint8_t *)&n->bar;
672 uint64_t val = 0;
674 if (addr < sizeof(n->bar)) {
675 memcpy(&val, ptr + addr, size);
677 return val;
680 static void nvme_process_db(NvmeCtrl *n, hwaddr addr, int val)
682 uint32_t qid;
684 if (addr & ((1 << 2) - 1)) {
685 return;
688 if (((addr - 0x1000) >> 2) & 1) {
689 uint16_t new_head = val & 0xffff;
690 int start_sqs;
691 NvmeCQueue *cq;
693 qid = (addr - (0x1000 + (1 << 2))) >> 3;
694 if (nvme_check_cqid(n, qid)) {
695 return;
698 cq = n->cq[qid];
699 if (new_head >= cq->size) {
700 return;
703 start_sqs = nvme_cq_full(cq) ? 1 : 0;
704 cq->head = new_head;
705 if (start_sqs) {
706 NvmeSQueue *sq;
707 QTAILQ_FOREACH(sq, &cq->sq_list, entry) {
708 timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
710 timer_mod(cq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
713 if (cq->tail != cq->head) {
714 nvme_isr_notify(n, cq);
716 } else {
717 uint16_t new_tail = val & 0xffff;
718 NvmeSQueue *sq;
720 qid = (addr - 0x1000) >> 3;
721 if (nvme_check_sqid(n, qid)) {
722 return;
725 sq = n->sq[qid];
726 if (new_tail >= sq->size) {
727 return;
730 sq->tail = new_tail;
731 timer_mod(sq->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500);
735 static void nvme_mmio_write(void *opaque, hwaddr addr, uint64_t data,
736 unsigned size)
738 NvmeCtrl *n = (NvmeCtrl *)opaque;
739 if (addr < sizeof(n->bar)) {
740 nvme_write_bar(n, addr, data, size);
741 } else if (addr >= 0x1000) {
742 nvme_process_db(n, addr, data);
746 static const MemoryRegionOps nvme_mmio_ops = {
747 .read = nvme_mmio_read,
748 .write = nvme_mmio_write,
749 .endianness = DEVICE_LITTLE_ENDIAN,
750 .impl = {
751 .min_access_size = 2,
752 .max_access_size = 8,
756 static int nvme_init(PCIDevice *pci_dev)
758 NvmeCtrl *n = NVME(pci_dev);
759 NvmeIdCtrl *id = &n->id_ctrl;
761 int i;
762 int64_t bs_size;
763 uint8_t *pci_conf;
765 if (!n->conf.blk) {
766 return -1;
769 bs_size = blk_getlength(n->conf.blk);
770 if (bs_size < 0) {
771 return -1;
774 blkconf_serial(&n->conf, &n->serial);
775 if (!n->serial) {
776 return -1;
778 blkconf_blocksizes(&n->conf);
780 pci_conf = pci_dev->config;
781 pci_conf[PCI_INTERRUPT_PIN] = 1;
782 pci_config_set_prog_interface(pci_dev->config, 0x2);
783 pci_config_set_class(pci_dev->config, PCI_CLASS_STORAGE_EXPRESS);
784 pcie_endpoint_cap_init(&n->parent_obj, 0x80);
786 n->num_namespaces = 1;
787 n->num_queues = 64;
788 n->reg_size = 1 << qemu_fls(0x1004 + 2 * (n->num_queues + 1) * 4);
789 n->ns_size = bs_size / (uint64_t)n->num_namespaces;
791 n->namespaces = g_new0(NvmeNamespace, n->num_namespaces);
792 n->sq = g_new0(NvmeSQueue *, n->num_queues);
793 n->cq = g_new0(NvmeCQueue *, n->num_queues);
795 memory_region_init_io(&n->iomem, OBJECT(n), &nvme_mmio_ops, n,
796 "nvme", n->reg_size);
797 pci_register_bar(&n->parent_obj, 0,
798 PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64,
799 &n->iomem);
800 msix_init_exclusive_bar(&n->parent_obj, n->num_queues, 4);
802 id->vid = cpu_to_le16(pci_get_word(pci_conf + PCI_VENDOR_ID));
803 id->ssvid = cpu_to_le16(pci_get_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID));
804 strpadcpy((char *)id->mn, sizeof(id->mn), "QEMU NVMe Ctrl", ' ');
805 strpadcpy((char *)id->fr, sizeof(id->fr), "1.0", ' ');
806 strpadcpy((char *)id->sn, sizeof(id->sn), n->serial, ' ');
807 id->rab = 6;
808 id->ieee[0] = 0x00;
809 id->ieee[1] = 0x02;
810 id->ieee[2] = 0xb3;
811 id->oacs = cpu_to_le16(0);
812 id->frmw = 7 << 1;
813 id->lpa = 1 << 0;
814 id->sqes = (0x6 << 4) | 0x6;
815 id->cqes = (0x4 << 4) | 0x4;
816 id->nn = cpu_to_le32(n->num_namespaces);
817 id->psd[0].mp = cpu_to_le16(0x9c4);
818 id->psd[0].enlat = cpu_to_le32(0x10);
819 id->psd[0].exlat = cpu_to_le32(0x4);
821 n->bar.cap = 0;
822 NVME_CAP_SET_MQES(n->bar.cap, 0x7ff);
823 NVME_CAP_SET_CQR(n->bar.cap, 1);
824 NVME_CAP_SET_AMS(n->bar.cap, 1);
825 NVME_CAP_SET_TO(n->bar.cap, 0xf);
826 NVME_CAP_SET_CSS(n->bar.cap, 1);
827 NVME_CAP_SET_MPSMAX(n->bar.cap, 4);
829 n->bar.vs = 0x00010100;
830 n->bar.intmc = n->bar.intms = 0;
832 for (i = 0; i < n->num_namespaces; i++) {
833 NvmeNamespace *ns = &n->namespaces[i];
834 NvmeIdNs *id_ns = &ns->id_ns;
835 id_ns->nsfeat = 0;
836 id_ns->nlbaf = 0;
837 id_ns->flbas = 0;
838 id_ns->mc = 0;
839 id_ns->dpc = 0;
840 id_ns->dps = 0;
841 id_ns->lbaf[0].ds = BDRV_SECTOR_BITS;
842 id_ns->ncap = id_ns->nuse = id_ns->nsze =
843 cpu_to_le64(n->ns_size >>
844 id_ns->lbaf[NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas)].ds);
846 return 0;
849 static void nvme_exit(PCIDevice *pci_dev)
851 NvmeCtrl *n = NVME(pci_dev);
853 nvme_clear_ctrl(n);
854 g_free(n->namespaces);
855 g_free(n->cq);
856 g_free(n->sq);
857 msix_uninit_exclusive_bar(pci_dev);
860 static Property nvme_props[] = {
861 DEFINE_BLOCK_PROPERTIES(NvmeCtrl, conf),
862 DEFINE_PROP_STRING("serial", NvmeCtrl, serial),
863 DEFINE_PROP_END_OF_LIST(),
866 static const VMStateDescription nvme_vmstate = {
867 .name = "nvme",
868 .unmigratable = 1,
871 static void nvme_class_init(ObjectClass *oc, void *data)
873 DeviceClass *dc = DEVICE_CLASS(oc);
874 PCIDeviceClass *pc = PCI_DEVICE_CLASS(oc);
876 pc->init = nvme_init;
877 pc->exit = nvme_exit;
878 pc->class_id = PCI_CLASS_STORAGE_EXPRESS;
879 pc->vendor_id = PCI_VENDOR_ID_INTEL;
880 pc->device_id = 0x5845;
881 pc->revision = 1;
882 pc->is_express = 1;
884 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
885 dc->desc = "Non-Volatile Memory Express";
886 dc->props = nvme_props;
887 dc->vmsd = &nvme_vmstate;
890 static void nvme_get_bootindex(Object *obj, Visitor *v, void *opaque,
891 const char *name, Error **errp)
893 NvmeCtrl *s = NVME(obj);
895 visit_type_int32(v, &s->conf.bootindex, name, errp);
898 static void nvme_set_bootindex(Object *obj, Visitor *v, void *opaque,
899 const char *name, Error **errp)
901 NvmeCtrl *s = NVME(obj);
902 int32_t boot_index;
903 Error *local_err = NULL;
905 visit_type_int32(v, &boot_index, name, &local_err);
906 if (local_err) {
907 goto out;
909 /* check whether bootindex is present in fw_boot_order list */
910 check_boot_index(boot_index, &local_err);
911 if (local_err) {
912 goto out;
914 /* change bootindex to a new one */
915 s->conf.bootindex = boot_index;
917 out:
918 if (local_err) {
919 error_propagate(errp, local_err);
923 static void nvme_instance_init(Object *obj)
925 object_property_add(obj, "bootindex", "int32",
926 nvme_get_bootindex,
927 nvme_set_bootindex, NULL, NULL, NULL);
928 object_property_set_int(obj, -1, "bootindex", NULL);
931 static const TypeInfo nvme_info = {
932 .name = "nvme",
933 .parent = TYPE_PCI_DEVICE,
934 .instance_size = sizeof(NvmeCtrl),
935 .class_init = nvme_class_init,
936 .instance_init = nvme_instance_init,
939 static void nvme_register_types(void)
941 type_register_static(&nvme_info);
944 type_init(nvme_register_types)