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.
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 "qemu/osdep.h"
24 #include "hw/block/block.h"
26 #include "hw/pci/msix.h"
27 #include "hw/pci/pci.h"
28 #include "sysemu/sysemu.h"
29 #include "qapi/error.h"
30 #include "qapi/visitor.h"
31 #include "sysemu/block-backend.h"
35 static void nvme_process_sq(void *opaque
);
37 static int nvme_check_sqid(NvmeCtrl
*n
, uint16_t sqid
)
39 return sqid
< n
->num_queues
&& n
->sq
[sqid
] != NULL
? 0 : -1;
42 static int nvme_check_cqid(NvmeCtrl
*n
, uint16_t cqid
)
44 return cqid
< n
->num_queues
&& n
->cq
[cqid
] != NULL
? 0 : -1;
47 static void nvme_inc_cq_tail(NvmeCQueue
*cq
)
50 if (cq
->tail
>= cq
->size
) {
52 cq
->phase
= !cq
->phase
;
56 static void nvme_inc_sq_head(NvmeSQueue
*sq
)
58 sq
->head
= (sq
->head
+ 1) % sq
->size
;
61 static uint8_t nvme_cq_full(NvmeCQueue
*cq
)
63 return (cq
->tail
+ 1) % cq
->size
== cq
->head
;
66 static uint8_t nvme_sq_empty(NvmeSQueue
*sq
)
68 return sq
->head
== sq
->tail
;
71 static void nvme_isr_notify(NvmeCtrl
*n
, NvmeCQueue
*cq
)
73 if (cq
->irq_enabled
) {
74 if (msix_enabled(&(n
->parent_obj
))) {
75 msix_notify(&(n
->parent_obj
), cq
->vector
);
77 pci_irq_pulse(&n
->parent_obj
);
82 static uint16_t nvme_map_prp(QEMUSGList
*qsg
, uint64_t prp1
, uint64_t prp2
,
83 uint32_t len
, NvmeCtrl
*n
)
85 hwaddr trans_len
= n
->page_size
- (prp1
% n
->page_size
);
86 trans_len
= MIN(len
, trans_len
);
87 int num_prps
= (len
>> n
->page_bits
) + 1;
90 return NVME_INVALID_FIELD
| NVME_DNR
;
93 pci_dma_sglist_init(qsg
, &n
->parent_obj
, num_prps
);
94 qemu_sglist_add(qsg
, prp1
, trans_len
);
100 if (len
> n
->page_size
) {
101 uint64_t prp_list
[n
->max_prp_ents
];
102 uint32_t nents
, prp_trans
;
105 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
106 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
107 pci_dma_read(&n
->parent_obj
, prp2
, (void *)prp_list
, prp_trans
);
109 uint64_t prp_ent
= le64_to_cpu(prp_list
[i
]);
111 if (i
== n
->max_prp_ents
- 1 && len
> n
->page_size
) {
112 if (!prp_ent
|| prp_ent
& (n
->page_size
- 1)) {
117 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
118 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
119 pci_dma_read(&n
->parent_obj
, prp_ent
, (void *)prp_list
,
121 prp_ent
= le64_to_cpu(prp_list
[i
]);
124 if (!prp_ent
|| prp_ent
& (n
->page_size
- 1)) {
128 trans_len
= MIN(len
, n
->page_size
);
129 qemu_sglist_add(qsg
, prp_ent
, trans_len
);
134 if (prp2
& (n
->page_size
- 1)) {
137 qemu_sglist_add(qsg
, prp2
, len
);
143 qemu_sglist_destroy(qsg
);
144 return NVME_INVALID_FIELD
| NVME_DNR
;
147 static uint16_t nvme_dma_read_prp(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
148 uint64_t prp1
, uint64_t prp2
)
152 if (nvme_map_prp(&qsg
, prp1
, prp2
, len
, n
)) {
153 return NVME_INVALID_FIELD
| NVME_DNR
;
155 if (dma_buf_read(ptr
, len
, &qsg
)) {
156 qemu_sglist_destroy(&qsg
);
157 return NVME_INVALID_FIELD
| NVME_DNR
;
159 qemu_sglist_destroy(&qsg
);
163 static void nvme_post_cqes(void *opaque
)
165 NvmeCQueue
*cq
= opaque
;
166 NvmeCtrl
*n
= cq
->ctrl
;
167 NvmeRequest
*req
, *next
;
169 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
173 if (nvme_cq_full(cq
)) {
177 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
179 req
->cqe
.status
= cpu_to_le16((req
->status
<< 1) | cq
->phase
);
180 req
->cqe
.sq_id
= cpu_to_le16(sq
->sqid
);
181 req
->cqe
.sq_head
= cpu_to_le16(sq
->head
);
182 addr
= cq
->dma_addr
+ cq
->tail
* n
->cqe_size
;
183 nvme_inc_cq_tail(cq
);
184 pci_dma_write(&n
->parent_obj
, addr
, (void *)&req
->cqe
,
186 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
188 nvme_isr_notify(n
, cq
);
191 static void nvme_enqueue_req_completion(NvmeCQueue
*cq
, NvmeRequest
*req
)
193 assert(cq
->cqid
== req
->sq
->cqid
);
194 QTAILQ_REMOVE(&req
->sq
->out_req_list
, req
, entry
);
195 QTAILQ_INSERT_TAIL(&cq
->req_list
, req
, entry
);
196 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
199 static void nvme_rw_cb(void *opaque
, int ret
)
201 NvmeRequest
*req
= opaque
;
202 NvmeSQueue
*sq
= req
->sq
;
203 NvmeCtrl
*n
= sq
->ctrl
;
204 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
207 block_acct_done(blk_get_stats(n
->conf
.blk
), &req
->acct
);
208 req
->status
= NVME_SUCCESS
;
210 block_acct_failed(blk_get_stats(n
->conf
.blk
), &req
->acct
);
211 req
->status
= NVME_INTERNAL_DEV_ERROR
;
214 qemu_sglist_destroy(&req
->qsg
);
216 nvme_enqueue_req_completion(cq
, req
);
219 static uint16_t nvme_flush(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
223 block_acct_start(blk_get_stats(n
->conf
.blk
), &req
->acct
, 0,
225 req
->aiocb
= blk_aio_flush(n
->conf
.blk
, nvme_rw_cb
, req
);
227 return NVME_NO_COMPLETE
;
230 static uint16_t nvme_rw(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
233 NvmeRwCmd
*rw
= (NvmeRwCmd
*)cmd
;
234 uint32_t nlb
= le32_to_cpu(rw
->nlb
) + 1;
235 uint64_t slba
= le64_to_cpu(rw
->slba
);
236 uint64_t prp1
= le64_to_cpu(rw
->prp1
);
237 uint64_t prp2
= le64_to_cpu(rw
->prp2
);
239 uint8_t lba_index
= NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
);
240 uint8_t data_shift
= ns
->id_ns
.lbaf
[lba_index
].ds
;
241 uint64_t data_size
= (uint64_t)nlb
<< data_shift
;
242 uint64_t data_offset
= slba
<< data_shift
;
243 int is_write
= rw
->opcode
== NVME_CMD_WRITE
? 1 : 0;
244 enum BlockAcctType acct
= is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
;
246 if ((slba
+ nlb
) > ns
->id_ns
.nsze
) {
247 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
248 return NVME_LBA_RANGE
| NVME_DNR
;
251 if (nvme_map_prp(&req
->qsg
, prp1
, prp2
, data_size
, n
)) {
252 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
253 return NVME_INVALID_FIELD
| NVME_DNR
;
256 assert((nlb
<< data_shift
) == req
->qsg
.size
);
259 dma_acct_start(n
->conf
.blk
, &req
->acct
, &req
->qsg
, acct
);
260 req
->aiocb
= is_write
?
261 dma_blk_write(n
->conf
.blk
, &req
->qsg
, data_offset
, BDRV_SECTOR_SIZE
,
263 dma_blk_read(n
->conf
.blk
, &req
->qsg
, data_offset
, BDRV_SECTOR_SIZE
,
266 return NVME_NO_COMPLETE
;
269 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
272 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
274 if (nsid
== 0 || nsid
> n
->num_namespaces
) {
275 return NVME_INVALID_NSID
| NVME_DNR
;
278 ns
= &n
->namespaces
[nsid
- 1];
279 switch (cmd
->opcode
) {
281 return nvme_flush(n
, ns
, cmd
, req
);
284 return nvme_rw(n
, ns
, cmd
, req
);
286 return NVME_INVALID_OPCODE
| NVME_DNR
;
290 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
292 n
->sq
[sq
->sqid
] = NULL
;
293 timer_del(sq
->timer
);
294 timer_free(sq
->timer
);
301 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
303 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
304 NvmeRequest
*req
, *next
;
307 uint16_t qid
= le16_to_cpu(c
->qid
);
309 if (!qid
|| nvme_check_sqid(n
, qid
)) {
310 return NVME_INVALID_QID
| NVME_DNR
;
314 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
315 req
= QTAILQ_FIRST(&sq
->out_req_list
);
317 blk_aio_cancel(req
->aiocb
);
319 if (!nvme_check_cqid(n
, sq
->cqid
)) {
320 cq
= n
->cq
[sq
->cqid
];
321 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
324 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
326 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
327 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
336 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
337 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
343 sq
->dma_addr
= dma_addr
;
347 sq
->head
= sq
->tail
= 0;
348 sq
->io_req
= g_new(NvmeRequest
, sq
->size
);
350 QTAILQ_INIT(&sq
->req_list
);
351 QTAILQ_INIT(&sq
->out_req_list
);
352 for (i
= 0; i
< sq
->size
; i
++) {
353 sq
->io_req
[i
].sq
= sq
;
354 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
356 sq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_process_sq
, sq
);
360 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
364 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
367 NvmeCreateSq
*c
= (NvmeCreateSq
*)cmd
;
369 uint16_t cqid
= le16_to_cpu(c
->cqid
);
370 uint16_t sqid
= le16_to_cpu(c
->sqid
);
371 uint16_t qsize
= le16_to_cpu(c
->qsize
);
372 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
373 uint64_t prp1
= le64_to_cpu(c
->prp1
);
375 if (!cqid
|| nvme_check_cqid(n
, cqid
)) {
376 return NVME_INVALID_CQID
| NVME_DNR
;
378 if (!sqid
|| !nvme_check_sqid(n
, sqid
)) {
379 return NVME_INVALID_QID
| NVME_DNR
;
381 if (!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
)) {
382 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
384 if (!prp1
|| prp1
& (n
->page_size
- 1)) {
385 return NVME_INVALID_FIELD
| NVME_DNR
;
387 if (!(NVME_SQ_FLAGS_PC(qflags
))) {
388 return NVME_INVALID_FIELD
| NVME_DNR
;
390 sq
= g_malloc0(sizeof(*sq
));
391 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
395 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
397 n
->cq
[cq
->cqid
] = NULL
;
398 timer_del(cq
->timer
);
399 timer_free(cq
->timer
);
400 msix_vector_unuse(&n
->parent_obj
, cq
->vector
);
406 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
408 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
410 uint16_t qid
= le16_to_cpu(c
->qid
);
412 if (!qid
|| nvme_check_cqid(n
, qid
)) {
413 return NVME_INVALID_CQID
| NVME_DNR
;
417 if (!QTAILQ_EMPTY(&cq
->sq_list
)) {
418 return NVME_INVALID_QUEUE_DEL
;
424 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
425 uint16_t cqid
, uint16_t vector
, uint16_t size
, uint16_t irq_enabled
)
430 cq
->dma_addr
= dma_addr
;
432 cq
->irq_enabled
= irq_enabled
;
434 cq
->head
= cq
->tail
= 0;
435 QTAILQ_INIT(&cq
->req_list
);
436 QTAILQ_INIT(&cq
->sq_list
);
437 msix_vector_use(&n
->parent_obj
, cq
->vector
);
439 cq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_post_cqes
, cq
);
442 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
445 NvmeCreateCq
*c
= (NvmeCreateCq
*)cmd
;
446 uint16_t cqid
= le16_to_cpu(c
->cqid
);
447 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
448 uint16_t qsize
= le16_to_cpu(c
->qsize
);
449 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
450 uint64_t prp1
= le64_to_cpu(c
->prp1
);
452 if (!cqid
|| !nvme_check_cqid(n
, cqid
)) {
453 return NVME_INVALID_CQID
| NVME_DNR
;
455 if (!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
)) {
456 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
459 return NVME_INVALID_FIELD
| NVME_DNR
;
461 if (vector
> n
->num_queues
) {
462 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
464 if (!(NVME_CQ_FLAGS_PC(qflags
))) {
465 return NVME_INVALID_FIELD
| NVME_DNR
;
468 cq
= g_malloc0(sizeof(*cq
));
469 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
470 NVME_CQ_FLAGS_IEN(qflags
));
474 static uint16_t nvme_identify_ctrl(NvmeCtrl
*n
, NvmeIdentify
*c
)
476 uint64_t prp1
= le64_to_cpu(c
->prp1
);
477 uint64_t prp2
= le64_to_cpu(c
->prp2
);
479 return nvme_dma_read_prp(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
),
483 static uint16_t nvme_identify_ns(NvmeCtrl
*n
, NvmeIdentify
*c
)
486 uint32_t nsid
= le32_to_cpu(c
->nsid
);
487 uint64_t prp1
= le64_to_cpu(c
->prp1
);
488 uint64_t prp2
= le64_to_cpu(c
->prp2
);
490 if (nsid
== 0 || nsid
> n
->num_namespaces
) {
491 return NVME_INVALID_NSID
| NVME_DNR
;
494 ns
= &n
->namespaces
[nsid
- 1];
495 return nvme_dma_read_prp(n
, (uint8_t *)&ns
->id_ns
, sizeof(ns
->id_ns
),
499 static uint16_t nvme_identify_nslist(NvmeCtrl
*n
, NvmeIdentify
*c
)
501 static const int data_len
= 4096;
502 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
503 uint64_t prp1
= le64_to_cpu(c
->prp1
);
504 uint64_t prp2
= le64_to_cpu(c
->prp2
);
509 list
= g_malloc0(data_len
);
510 for (i
= 0; i
< n
->num_namespaces
; i
++) {
514 list
[j
++] = cpu_to_le32(i
+ 1);
515 if (j
== data_len
/ sizeof(uint32_t)) {
519 ret
= nvme_dma_read_prp(n
, (uint8_t *)list
, data_len
, prp1
, prp2
);
525 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeCmd
*cmd
)
527 NvmeIdentify
*c
= (NvmeIdentify
*)cmd
;
529 switch (le32_to_cpu(c
->cns
)) {
531 return nvme_identify_ns(n
, c
);
533 return nvme_identify_ctrl(n
, c
);
535 return nvme_identify_nslist(n
, c
);
537 return NVME_INVALID_FIELD
| NVME_DNR
;
541 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
543 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
547 case NVME_VOLATILE_WRITE_CACHE
:
548 result
= blk_enable_write_cache(n
->conf
.blk
);
550 case NVME_NUMBER_OF_QUEUES
:
551 result
= cpu_to_le32((n
->num_queues
- 1) | ((n
->num_queues
- 1) << 16));
554 return NVME_INVALID_FIELD
| NVME_DNR
;
557 req
->cqe
.result
= result
;
561 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
563 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
564 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
567 case NVME_VOLATILE_WRITE_CACHE
:
568 blk_set_enable_write_cache(n
->conf
.blk
, dw11
& 1);
570 case NVME_NUMBER_OF_QUEUES
:
572 cpu_to_le32((n
->num_queues
- 1) | ((n
->num_queues
- 1) << 16));
575 return NVME_INVALID_FIELD
| NVME_DNR
;
580 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
582 switch (cmd
->opcode
) {
583 case NVME_ADM_CMD_DELETE_SQ
:
584 return nvme_del_sq(n
, cmd
);
585 case NVME_ADM_CMD_CREATE_SQ
:
586 return nvme_create_sq(n
, cmd
);
587 case NVME_ADM_CMD_DELETE_CQ
:
588 return nvme_del_cq(n
, cmd
);
589 case NVME_ADM_CMD_CREATE_CQ
:
590 return nvme_create_cq(n
, cmd
);
591 case NVME_ADM_CMD_IDENTIFY
:
592 return nvme_identify(n
, cmd
);
593 case NVME_ADM_CMD_SET_FEATURES
:
594 return nvme_set_feature(n
, cmd
, req
);
595 case NVME_ADM_CMD_GET_FEATURES
:
596 return nvme_get_feature(n
, cmd
, req
);
598 return NVME_INVALID_OPCODE
| NVME_DNR
;
602 static void nvme_process_sq(void *opaque
)
604 NvmeSQueue
*sq
= opaque
;
605 NvmeCtrl
*n
= sq
->ctrl
;
606 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
613 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
614 addr
= sq
->dma_addr
+ sq
->head
* n
->sqe_size
;
615 pci_dma_read(&n
->parent_obj
, addr
, (void *)&cmd
, sizeof(cmd
));
616 nvme_inc_sq_head(sq
);
618 req
= QTAILQ_FIRST(&sq
->req_list
);
619 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
620 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
621 memset(&req
->cqe
, 0, sizeof(req
->cqe
));
622 req
->cqe
.cid
= cmd
.cid
;
624 status
= sq
->sqid
? nvme_io_cmd(n
, &cmd
, req
) :
625 nvme_admin_cmd(n
, &cmd
, req
);
626 if (status
!= NVME_NO_COMPLETE
) {
627 req
->status
= status
;
628 nvme_enqueue_req_completion(cq
, req
);
633 static void nvme_clear_ctrl(NvmeCtrl
*n
)
637 for (i
= 0; i
< n
->num_queues
; i
++) {
638 if (n
->sq
[i
] != NULL
) {
639 nvme_free_sq(n
->sq
[i
], n
);
642 for (i
= 0; i
< n
->num_queues
; i
++) {
643 if (n
->cq
[i
] != NULL
) {
644 nvme_free_cq(n
->cq
[i
], n
);
648 blk_flush(n
->conf
.blk
);
652 static int nvme_start_ctrl(NvmeCtrl
*n
)
654 uint32_t page_bits
= NVME_CC_MPS(n
->bar
.cc
) + 12;
655 uint32_t page_size
= 1 << page_bits
;
657 if (n
->cq
[0] || n
->sq
[0] || !n
->bar
.asq
|| !n
->bar
.acq
||
658 n
->bar
.asq
& (page_size
- 1) || n
->bar
.acq
& (page_size
- 1) ||
659 NVME_CC_MPS(n
->bar
.cc
) < NVME_CAP_MPSMIN(n
->bar
.cap
) ||
660 NVME_CC_MPS(n
->bar
.cc
) > NVME_CAP_MPSMAX(n
->bar
.cap
) ||
661 NVME_CC_IOCQES(n
->bar
.cc
) < NVME_CTRL_CQES_MIN(n
->id_ctrl
.cqes
) ||
662 NVME_CC_IOCQES(n
->bar
.cc
) > NVME_CTRL_CQES_MAX(n
->id_ctrl
.cqes
) ||
663 NVME_CC_IOSQES(n
->bar
.cc
) < NVME_CTRL_SQES_MIN(n
->id_ctrl
.sqes
) ||
664 NVME_CC_IOSQES(n
->bar
.cc
) > NVME_CTRL_SQES_MAX(n
->id_ctrl
.sqes
) ||
665 !NVME_AQA_ASQS(n
->bar
.aqa
) || !NVME_AQA_ACQS(n
->bar
.aqa
)) {
669 n
->page_bits
= page_bits
;
670 n
->page_size
= page_size
;
671 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
672 n
->cqe_size
= 1 << NVME_CC_IOCQES(n
->bar
.cc
);
673 n
->sqe_size
= 1 << NVME_CC_IOSQES(n
->bar
.cc
);
674 nvme_init_cq(&n
->admin_cq
, n
, n
->bar
.acq
, 0, 0,
675 NVME_AQA_ACQS(n
->bar
.aqa
) + 1, 1);
676 nvme_init_sq(&n
->admin_sq
, n
, n
->bar
.asq
, 0, 0,
677 NVME_AQA_ASQS(n
->bar
.aqa
) + 1);
682 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
687 n
->bar
.intms
|= data
& 0xffffffff;
688 n
->bar
.intmc
= n
->bar
.intms
;
691 n
->bar
.intms
&= ~(data
& 0xffffffff);
692 n
->bar
.intmc
= n
->bar
.intms
;
695 /* Windows first sends data, then sends enable bit */
696 if (!NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
) &&
697 !NVME_CC_SHN(data
) && !NVME_CC_SHN(n
->bar
.cc
))
702 if (NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
)) {
704 if (nvme_start_ctrl(n
)) {
705 n
->bar
.csts
= NVME_CSTS_FAILED
;
707 n
->bar
.csts
= NVME_CSTS_READY
;
709 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(n
->bar
.cc
)) {
711 n
->bar
.csts
&= ~NVME_CSTS_READY
;
713 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(n
->bar
.cc
))) {
716 n
->bar
.csts
|= NVME_CSTS_SHST_COMPLETE
;
717 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(n
->bar
.cc
)) {
718 n
->bar
.csts
&= ~NVME_CSTS_SHST_COMPLETE
;
723 n
->bar
.aqa
= data
& 0xffffffff;
729 n
->bar
.asq
|= data
<< 32;
735 n
->bar
.acq
|= data
<< 32;
742 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
744 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
745 uint8_t *ptr
= (uint8_t *)&n
->bar
;
748 if (addr
< sizeof(n
->bar
)) {
749 memcpy(&val
, ptr
+ addr
, size
);
754 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
758 if (addr
& ((1 << 2) - 1)) {
762 if (((addr
- 0x1000) >> 2) & 1) {
763 uint16_t new_head
= val
& 0xffff;
767 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
768 if (nvme_check_cqid(n
, qid
)) {
773 if (new_head
>= cq
->size
) {
777 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
781 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
782 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
784 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
787 if (cq
->tail
!= cq
->head
) {
788 nvme_isr_notify(n
, cq
);
791 uint16_t new_tail
= val
& 0xffff;
794 qid
= (addr
- 0x1000) >> 3;
795 if (nvme_check_sqid(n
, qid
)) {
800 if (new_tail
>= sq
->size
) {
805 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
809 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
812 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
813 if (addr
< sizeof(n
->bar
)) {
814 nvme_write_bar(n
, addr
, data
, size
);
815 } else if (addr
>= 0x1000) {
816 nvme_process_db(n
, addr
, data
);
820 static const MemoryRegionOps nvme_mmio_ops
= {
821 .read
= nvme_mmio_read
,
822 .write
= nvme_mmio_write
,
823 .endianness
= DEVICE_LITTLE_ENDIAN
,
825 .min_access_size
= 2,
826 .max_access_size
= 8,
830 static int nvme_init(PCIDevice
*pci_dev
)
832 NvmeCtrl
*n
= NVME(pci_dev
);
833 NvmeIdCtrl
*id
= &n
->id_ctrl
;
843 bs_size
= blk_getlength(n
->conf
.blk
);
848 blkconf_serial(&n
->conf
, &n
->serial
);
852 blkconf_blocksizes(&n
->conf
);
853 blkconf_apply_backend_options(&n
->conf
);
855 pci_conf
= pci_dev
->config
;
856 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
857 pci_config_set_prog_interface(pci_dev
->config
, 0x2);
858 pci_config_set_class(pci_dev
->config
, PCI_CLASS_STORAGE_EXPRESS
);
859 pcie_endpoint_cap_init(&n
->parent_obj
, 0x80);
861 n
->num_namespaces
= 1;
863 n
->reg_size
= pow2ceil(0x1004 + 2 * (n
->num_queues
+ 1) * 4);
864 n
->ns_size
= bs_size
/ (uint64_t)n
->num_namespaces
;
866 n
->namespaces
= g_new0(NvmeNamespace
, n
->num_namespaces
);
867 n
->sq
= g_new0(NvmeSQueue
*, n
->num_queues
);
868 n
->cq
= g_new0(NvmeCQueue
*, n
->num_queues
);
870 memory_region_init_io(&n
->iomem
, OBJECT(n
), &nvme_mmio_ops
, n
,
871 "nvme", n
->reg_size
);
872 pci_register_bar(&n
->parent_obj
, 0,
873 PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_64
,
875 msix_init_exclusive_bar(&n
->parent_obj
, n
->num_queues
, 4);
877 id
->vid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_VENDOR_ID
));
878 id
->ssvid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_SUBSYSTEM_VENDOR_ID
));
879 strpadcpy((char *)id
->mn
, sizeof(id
->mn
), "QEMU NVMe Ctrl", ' ');
880 strpadcpy((char *)id
->fr
, sizeof(id
->fr
), "1.0", ' ');
881 strpadcpy((char *)id
->sn
, sizeof(id
->sn
), n
->serial
, ' ');
886 id
->oacs
= cpu_to_le16(0);
889 id
->sqes
= (0x6 << 4) | 0x6;
890 id
->cqes
= (0x4 << 4) | 0x4;
891 id
->nn
= cpu_to_le32(n
->num_namespaces
);
892 id
->psd
[0].mp
= cpu_to_le16(0x9c4);
893 id
->psd
[0].enlat
= cpu_to_le32(0x10);
894 id
->psd
[0].exlat
= cpu_to_le32(0x4);
895 if (blk_enable_write_cache(n
->conf
.blk
)) {
900 NVME_CAP_SET_MQES(n
->bar
.cap
, 0x7ff);
901 NVME_CAP_SET_CQR(n
->bar
.cap
, 1);
902 NVME_CAP_SET_AMS(n
->bar
.cap
, 1);
903 NVME_CAP_SET_TO(n
->bar
.cap
, 0xf);
904 NVME_CAP_SET_CSS(n
->bar
.cap
, 1);
905 NVME_CAP_SET_MPSMAX(n
->bar
.cap
, 4);
907 n
->bar
.vs
= 0x00010100;
908 n
->bar
.intmc
= n
->bar
.intms
= 0;
910 for (i
= 0; i
< n
->num_namespaces
; i
++) {
911 NvmeNamespace
*ns
= &n
->namespaces
[i
];
912 NvmeIdNs
*id_ns
= &ns
->id_ns
;
919 id_ns
->lbaf
[0].ds
= BDRV_SECTOR_BITS
;
920 id_ns
->ncap
= id_ns
->nuse
= id_ns
->nsze
=
921 cpu_to_le64(n
->ns_size
>>
922 id_ns
->lbaf
[NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
)].ds
);
927 static void nvme_exit(PCIDevice
*pci_dev
)
929 NvmeCtrl
*n
= NVME(pci_dev
);
932 g_free(n
->namespaces
);
935 msix_uninit_exclusive_bar(pci_dev
);
938 static Property nvme_props
[] = {
939 DEFINE_BLOCK_PROPERTIES(NvmeCtrl
, conf
),
940 DEFINE_PROP_STRING("serial", NvmeCtrl
, serial
),
941 DEFINE_PROP_END_OF_LIST(),
944 static const VMStateDescription nvme_vmstate
= {
949 static void nvme_class_init(ObjectClass
*oc
, void *data
)
951 DeviceClass
*dc
= DEVICE_CLASS(oc
);
952 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
954 pc
->init
= nvme_init
;
955 pc
->exit
= nvme_exit
;
956 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
957 pc
->vendor_id
= PCI_VENDOR_ID_INTEL
;
958 pc
->device_id
= 0x5845;
962 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
963 dc
->desc
= "Non-Volatile Memory Express";
964 dc
->props
= nvme_props
;
965 dc
->vmsd
= &nvme_vmstate
;
968 static void nvme_instance_init(Object
*obj
)
970 NvmeCtrl
*s
= NVME(obj
);
972 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
973 "bootindex", "/namespace@1,0",
974 DEVICE(obj
), &error_abort
);
977 static const TypeInfo nvme_info
= {
979 .parent
= TYPE_PCI_DEVICE
,
980 .instance_size
= sizeof(NvmeCtrl
),
981 .class_init
= nvme_class_init
,
982 .instance_init
= nvme_instance_init
,
985 static void nvme_register_types(void)
987 type_register_static(&nvme_info
);
990 type_init(nvme_register_types
)