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
, nvme_rw_cb
, req
) :
262 dma_blk_read(n
->conf
.blk
, &req
->qsg
, data_offset
, nvme_rw_cb
, req
);
264 return NVME_NO_COMPLETE
;
267 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
270 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
272 if (nsid
== 0 || nsid
> n
->num_namespaces
) {
273 return NVME_INVALID_NSID
| NVME_DNR
;
276 ns
= &n
->namespaces
[nsid
- 1];
277 switch (cmd
->opcode
) {
279 return nvme_flush(n
, ns
, cmd
, req
);
282 return nvme_rw(n
, ns
, cmd
, req
);
284 return NVME_INVALID_OPCODE
| NVME_DNR
;
288 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
290 n
->sq
[sq
->sqid
] = NULL
;
291 timer_del(sq
->timer
);
292 timer_free(sq
->timer
);
299 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
301 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
302 NvmeRequest
*req
, *next
;
305 uint16_t qid
= le16_to_cpu(c
->qid
);
307 if (!qid
|| nvme_check_sqid(n
, qid
)) {
308 return NVME_INVALID_QID
| NVME_DNR
;
312 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
313 req
= QTAILQ_FIRST(&sq
->out_req_list
);
315 blk_aio_cancel(req
->aiocb
);
317 if (!nvme_check_cqid(n
, sq
->cqid
)) {
318 cq
= n
->cq
[sq
->cqid
];
319 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
322 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
324 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
325 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
334 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
335 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
341 sq
->dma_addr
= dma_addr
;
345 sq
->head
= sq
->tail
= 0;
346 sq
->io_req
= g_new(NvmeRequest
, sq
->size
);
348 QTAILQ_INIT(&sq
->req_list
);
349 QTAILQ_INIT(&sq
->out_req_list
);
350 for (i
= 0; i
< sq
->size
; i
++) {
351 sq
->io_req
[i
].sq
= sq
;
352 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
354 sq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_process_sq
, sq
);
358 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
362 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
365 NvmeCreateSq
*c
= (NvmeCreateSq
*)cmd
;
367 uint16_t cqid
= le16_to_cpu(c
->cqid
);
368 uint16_t sqid
= le16_to_cpu(c
->sqid
);
369 uint16_t qsize
= le16_to_cpu(c
->qsize
);
370 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
371 uint64_t prp1
= le64_to_cpu(c
->prp1
);
373 if (!cqid
|| nvme_check_cqid(n
, cqid
)) {
374 return NVME_INVALID_CQID
| NVME_DNR
;
376 if (!sqid
|| (sqid
&& !nvme_check_sqid(n
, sqid
))) {
377 return NVME_INVALID_QID
| NVME_DNR
;
379 if (!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
)) {
380 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
382 if (!prp1
|| prp1
& (n
->page_size
- 1)) {
383 return NVME_INVALID_FIELD
| NVME_DNR
;
385 if (!(NVME_SQ_FLAGS_PC(qflags
))) {
386 return NVME_INVALID_FIELD
| NVME_DNR
;
388 sq
= g_malloc0(sizeof(*sq
));
389 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
393 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
395 n
->cq
[cq
->cqid
] = NULL
;
396 timer_del(cq
->timer
);
397 timer_free(cq
->timer
);
398 msix_vector_unuse(&n
->parent_obj
, cq
->vector
);
404 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
406 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
408 uint16_t qid
= le16_to_cpu(c
->qid
);
410 if (!qid
|| nvme_check_cqid(n
, qid
)) {
411 return NVME_INVALID_CQID
| NVME_DNR
;
415 if (!QTAILQ_EMPTY(&cq
->sq_list
)) {
416 return NVME_INVALID_QUEUE_DEL
;
422 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
423 uint16_t cqid
, uint16_t vector
, uint16_t size
, uint16_t irq_enabled
)
428 cq
->dma_addr
= dma_addr
;
430 cq
->irq_enabled
= irq_enabled
;
432 cq
->head
= cq
->tail
= 0;
433 QTAILQ_INIT(&cq
->req_list
);
434 QTAILQ_INIT(&cq
->sq_list
);
435 msix_vector_use(&n
->parent_obj
, cq
->vector
);
437 cq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_post_cqes
, cq
);
440 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
443 NvmeCreateCq
*c
= (NvmeCreateCq
*)cmd
;
444 uint16_t cqid
= le16_to_cpu(c
->cqid
);
445 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
446 uint16_t qsize
= le16_to_cpu(c
->qsize
);
447 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
448 uint64_t prp1
= le64_to_cpu(c
->prp1
);
450 if (!cqid
|| (cqid
&& !nvme_check_cqid(n
, cqid
))) {
451 return NVME_INVALID_CQID
| NVME_DNR
;
453 if (!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
)) {
454 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
457 return NVME_INVALID_FIELD
| NVME_DNR
;
459 if (vector
> n
->num_queues
) {
460 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
462 if (!(NVME_CQ_FLAGS_PC(qflags
))) {
463 return NVME_INVALID_FIELD
| NVME_DNR
;
466 cq
= g_malloc0(sizeof(*cq
));
467 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
468 NVME_CQ_FLAGS_IEN(qflags
));
472 static uint16_t nvme_identify_ctrl(NvmeCtrl
*n
, NvmeIdentify
*c
)
474 uint64_t prp1
= le64_to_cpu(c
->prp1
);
475 uint64_t prp2
= le64_to_cpu(c
->prp2
);
477 return nvme_dma_read_prp(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
),
481 static uint16_t nvme_identify_ns(NvmeCtrl
*n
, NvmeIdentify
*c
)
484 uint32_t nsid
= le32_to_cpu(c
->nsid
);
485 uint64_t prp1
= le64_to_cpu(c
->prp1
);
486 uint64_t prp2
= le64_to_cpu(c
->prp2
);
488 if (nsid
== 0 || nsid
> n
->num_namespaces
) {
489 return NVME_INVALID_NSID
| NVME_DNR
;
492 ns
= &n
->namespaces
[nsid
- 1];
493 return nvme_dma_read_prp(n
, (uint8_t *)&ns
->id_ns
, sizeof(ns
->id_ns
),
497 static uint16_t nvme_identify_nslist(NvmeCtrl
*n
, NvmeIdentify
*c
)
499 static const int data_len
= 4096;
500 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
501 uint64_t prp1
= le64_to_cpu(c
->prp1
);
502 uint64_t prp2
= le64_to_cpu(c
->prp2
);
507 list
= g_malloc0(data_len
);
508 for (i
= 0; i
< n
->num_namespaces
; i
++) {
512 list
[j
++] = cpu_to_le32(i
+ 1);
513 if (j
== data_len
/ sizeof(uint32_t)) {
517 ret
= nvme_dma_read_prp(n
, (uint8_t *)list
, data_len
, prp1
, prp2
);
523 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeCmd
*cmd
)
525 NvmeIdentify
*c
= (NvmeIdentify
*)cmd
;
527 switch (le32_to_cpu(c
->cns
)) {
529 return nvme_identify_ns(n
, c
);
531 return nvme_identify_ctrl(n
, c
);
533 return nvme_identify_nslist(n
, c
);
535 return NVME_INVALID_FIELD
| NVME_DNR
;
539 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
541 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
545 case NVME_VOLATILE_WRITE_CACHE
:
546 result
= blk_enable_write_cache(n
->conf
.blk
);
548 case NVME_NUMBER_OF_QUEUES
:
549 result
= cpu_to_le32((n
->num_queues
- 1) | ((n
->num_queues
- 1) << 16));
552 return NVME_INVALID_FIELD
| NVME_DNR
;
555 req
->cqe
.result
= result
;
559 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
561 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
562 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
565 case NVME_VOLATILE_WRITE_CACHE
:
566 blk_set_enable_write_cache(n
->conf
.blk
, dw11
& 1);
568 case NVME_NUMBER_OF_QUEUES
:
570 cpu_to_le32((n
->num_queues
- 1) | ((n
->num_queues
- 1) << 16));
573 return NVME_INVALID_FIELD
| NVME_DNR
;
578 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
580 switch (cmd
->opcode
) {
581 case NVME_ADM_CMD_DELETE_SQ
:
582 return nvme_del_sq(n
, cmd
);
583 case NVME_ADM_CMD_CREATE_SQ
:
584 return nvme_create_sq(n
, cmd
);
585 case NVME_ADM_CMD_DELETE_CQ
:
586 return nvme_del_cq(n
, cmd
);
587 case NVME_ADM_CMD_CREATE_CQ
:
588 return nvme_create_cq(n
, cmd
);
589 case NVME_ADM_CMD_IDENTIFY
:
590 return nvme_identify(n
, cmd
);
591 case NVME_ADM_CMD_SET_FEATURES
:
592 return nvme_set_feature(n
, cmd
, req
);
593 case NVME_ADM_CMD_GET_FEATURES
:
594 return nvme_get_feature(n
, cmd
, req
);
596 return NVME_INVALID_OPCODE
| NVME_DNR
;
600 static void nvme_process_sq(void *opaque
)
602 NvmeSQueue
*sq
= opaque
;
603 NvmeCtrl
*n
= sq
->ctrl
;
604 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
611 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
612 addr
= sq
->dma_addr
+ sq
->head
* n
->sqe_size
;
613 pci_dma_read(&n
->parent_obj
, addr
, (void *)&cmd
, sizeof(cmd
));
614 nvme_inc_sq_head(sq
);
616 req
= QTAILQ_FIRST(&sq
->req_list
);
617 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
618 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
619 memset(&req
->cqe
, 0, sizeof(req
->cqe
));
620 req
->cqe
.cid
= cmd
.cid
;
622 status
= sq
->sqid
? nvme_io_cmd(n
, &cmd
, req
) :
623 nvme_admin_cmd(n
, &cmd
, req
);
624 if (status
!= NVME_NO_COMPLETE
) {
625 req
->status
= status
;
626 nvme_enqueue_req_completion(cq
, req
);
631 static void nvme_clear_ctrl(NvmeCtrl
*n
)
635 for (i
= 0; i
< n
->num_queues
; i
++) {
636 if (n
->sq
[i
] != NULL
) {
637 nvme_free_sq(n
->sq
[i
], n
);
640 for (i
= 0; i
< n
->num_queues
; i
++) {
641 if (n
->cq
[i
] != NULL
) {
642 nvme_free_cq(n
->cq
[i
], n
);
646 blk_flush(n
->conf
.blk
);
650 static int nvme_start_ctrl(NvmeCtrl
*n
)
652 uint32_t page_bits
= NVME_CC_MPS(n
->bar
.cc
) + 12;
653 uint32_t page_size
= 1 << page_bits
;
655 if (n
->cq
[0] || n
->sq
[0] || !n
->bar
.asq
|| !n
->bar
.acq
||
656 n
->bar
.asq
& (page_size
- 1) || n
->bar
.acq
& (page_size
- 1) ||
657 NVME_CC_MPS(n
->bar
.cc
) < NVME_CAP_MPSMIN(n
->bar
.cap
) ||
658 NVME_CC_MPS(n
->bar
.cc
) > NVME_CAP_MPSMAX(n
->bar
.cap
) ||
659 NVME_CC_IOCQES(n
->bar
.cc
) < NVME_CTRL_CQES_MIN(n
->id_ctrl
.cqes
) ||
660 NVME_CC_IOCQES(n
->bar
.cc
) > NVME_CTRL_CQES_MAX(n
->id_ctrl
.cqes
) ||
661 NVME_CC_IOSQES(n
->bar
.cc
) < NVME_CTRL_SQES_MIN(n
->id_ctrl
.sqes
) ||
662 NVME_CC_IOSQES(n
->bar
.cc
) > NVME_CTRL_SQES_MAX(n
->id_ctrl
.sqes
) ||
663 !NVME_AQA_ASQS(n
->bar
.aqa
) || !NVME_AQA_ACQS(n
->bar
.aqa
)) {
667 n
->page_bits
= page_bits
;
668 n
->page_size
= page_size
;
669 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
670 n
->cqe_size
= 1 << NVME_CC_IOCQES(n
->bar
.cc
);
671 n
->sqe_size
= 1 << NVME_CC_IOSQES(n
->bar
.cc
);
672 nvme_init_cq(&n
->admin_cq
, n
, n
->bar
.acq
, 0, 0,
673 NVME_AQA_ACQS(n
->bar
.aqa
) + 1, 1);
674 nvme_init_sq(&n
->admin_sq
, n
, n
->bar
.asq
, 0, 0,
675 NVME_AQA_ASQS(n
->bar
.aqa
) + 1);
680 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
685 n
->bar
.intms
|= data
& 0xffffffff;
686 n
->bar
.intmc
= n
->bar
.intms
;
689 n
->bar
.intms
&= ~(data
& 0xffffffff);
690 n
->bar
.intmc
= n
->bar
.intms
;
693 /* Windows first sends data, then sends enable bit */
694 if (!NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
) &&
695 !NVME_CC_SHN(data
) && !NVME_CC_SHN(n
->bar
.cc
))
700 if (NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
)) {
702 if (nvme_start_ctrl(n
)) {
703 n
->bar
.csts
= NVME_CSTS_FAILED
;
705 n
->bar
.csts
= NVME_CSTS_READY
;
707 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(n
->bar
.cc
)) {
709 n
->bar
.csts
&= ~NVME_CSTS_READY
;
711 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(n
->bar
.cc
))) {
714 n
->bar
.csts
|= NVME_CSTS_SHST_COMPLETE
;
715 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(n
->bar
.cc
)) {
716 n
->bar
.csts
&= ~NVME_CSTS_SHST_COMPLETE
;
721 n
->bar
.aqa
= data
& 0xffffffff;
727 n
->bar
.asq
|= data
<< 32;
733 n
->bar
.acq
|= data
<< 32;
740 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
742 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
743 uint8_t *ptr
= (uint8_t *)&n
->bar
;
746 if (addr
< sizeof(n
->bar
)) {
747 memcpy(&val
, ptr
+ addr
, size
);
752 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
756 if (addr
& ((1 << 2) - 1)) {
760 if (((addr
- 0x1000) >> 2) & 1) {
761 uint16_t new_head
= val
& 0xffff;
765 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
766 if (nvme_check_cqid(n
, qid
)) {
771 if (new_head
>= cq
->size
) {
775 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
779 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
780 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
782 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
785 if (cq
->tail
!= cq
->head
) {
786 nvme_isr_notify(n
, cq
);
789 uint16_t new_tail
= val
& 0xffff;
792 qid
= (addr
- 0x1000) >> 3;
793 if (nvme_check_sqid(n
, qid
)) {
798 if (new_tail
>= sq
->size
) {
803 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
807 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
810 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
811 if (addr
< sizeof(n
->bar
)) {
812 nvme_write_bar(n
, addr
, data
, size
);
813 } else if (addr
>= 0x1000) {
814 nvme_process_db(n
, addr
, data
);
818 static const MemoryRegionOps nvme_mmio_ops
= {
819 .read
= nvme_mmio_read
,
820 .write
= nvme_mmio_write
,
821 .endianness
= DEVICE_LITTLE_ENDIAN
,
823 .min_access_size
= 2,
824 .max_access_size
= 8,
828 static int nvme_init(PCIDevice
*pci_dev
)
830 NvmeCtrl
*n
= NVME(pci_dev
);
831 NvmeIdCtrl
*id
= &n
->id_ctrl
;
841 bs_size
= blk_getlength(n
->conf
.blk
);
846 blkconf_serial(&n
->conf
, &n
->serial
);
850 blkconf_blocksizes(&n
->conf
);
851 blkconf_apply_backend_options(&n
->conf
);
853 pci_conf
= pci_dev
->config
;
854 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
855 pci_config_set_prog_interface(pci_dev
->config
, 0x2);
856 pci_config_set_class(pci_dev
->config
, PCI_CLASS_STORAGE_EXPRESS
);
857 pcie_endpoint_cap_init(&n
->parent_obj
, 0x80);
859 n
->num_namespaces
= 1;
861 n
->reg_size
= pow2ceil(0x1004 + 2 * (n
->num_queues
+ 1) * 4);
862 n
->ns_size
= bs_size
/ (uint64_t)n
->num_namespaces
;
864 n
->namespaces
= g_new0(NvmeNamespace
, n
->num_namespaces
);
865 n
->sq
= g_new0(NvmeSQueue
*, n
->num_queues
);
866 n
->cq
= g_new0(NvmeCQueue
*, n
->num_queues
);
868 memory_region_init_io(&n
->iomem
, OBJECT(n
), &nvme_mmio_ops
, n
,
869 "nvme", n
->reg_size
);
870 pci_register_bar(&n
->parent_obj
, 0,
871 PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_64
,
873 msix_init_exclusive_bar(&n
->parent_obj
, n
->num_queues
, 4);
875 id
->vid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_VENDOR_ID
));
876 id
->ssvid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_SUBSYSTEM_VENDOR_ID
));
877 strpadcpy((char *)id
->mn
, sizeof(id
->mn
), "QEMU NVMe Ctrl", ' ');
878 strpadcpy((char *)id
->fr
, sizeof(id
->fr
), "1.0", ' ');
879 strpadcpy((char *)id
->sn
, sizeof(id
->sn
), n
->serial
, ' ');
884 id
->oacs
= cpu_to_le16(0);
887 id
->sqes
= (0x6 << 4) | 0x6;
888 id
->cqes
= (0x4 << 4) | 0x4;
889 id
->nn
= cpu_to_le32(n
->num_namespaces
);
890 id
->psd
[0].mp
= cpu_to_le16(0x9c4);
891 id
->psd
[0].enlat
= cpu_to_le32(0x10);
892 id
->psd
[0].exlat
= cpu_to_le32(0x4);
893 if (blk_enable_write_cache(n
->conf
.blk
)) {
898 NVME_CAP_SET_MQES(n
->bar
.cap
, 0x7ff);
899 NVME_CAP_SET_CQR(n
->bar
.cap
, 1);
900 NVME_CAP_SET_AMS(n
->bar
.cap
, 1);
901 NVME_CAP_SET_TO(n
->bar
.cap
, 0xf);
902 NVME_CAP_SET_CSS(n
->bar
.cap
, 1);
903 NVME_CAP_SET_MPSMAX(n
->bar
.cap
, 4);
905 n
->bar
.vs
= 0x00010100;
906 n
->bar
.intmc
= n
->bar
.intms
= 0;
908 for (i
= 0; i
< n
->num_namespaces
; i
++) {
909 NvmeNamespace
*ns
= &n
->namespaces
[i
];
910 NvmeIdNs
*id_ns
= &ns
->id_ns
;
917 id_ns
->lbaf
[0].ds
= BDRV_SECTOR_BITS
;
918 id_ns
->ncap
= id_ns
->nuse
= id_ns
->nsze
=
919 cpu_to_le64(n
->ns_size
>>
920 id_ns
->lbaf
[NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
)].ds
);
925 static void nvme_exit(PCIDevice
*pci_dev
)
927 NvmeCtrl
*n
= NVME(pci_dev
);
930 g_free(n
->namespaces
);
933 msix_uninit_exclusive_bar(pci_dev
);
936 static Property nvme_props
[] = {
937 DEFINE_BLOCK_PROPERTIES(NvmeCtrl
, conf
),
938 DEFINE_PROP_STRING("serial", NvmeCtrl
, serial
),
939 DEFINE_PROP_END_OF_LIST(),
942 static const VMStateDescription nvme_vmstate
= {
947 static void nvme_class_init(ObjectClass
*oc
, void *data
)
949 DeviceClass
*dc
= DEVICE_CLASS(oc
);
950 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
952 pc
->init
= nvme_init
;
953 pc
->exit
= nvme_exit
;
954 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
955 pc
->vendor_id
= PCI_VENDOR_ID_INTEL
;
956 pc
->device_id
= 0x5845;
960 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
961 dc
->desc
= "Non-Volatile Memory Express";
962 dc
->props
= nvme_props
;
963 dc
->vmsd
= &nvme_vmstate
;
966 static void nvme_instance_init(Object
*obj
)
968 NvmeCtrl
*s
= NVME(obj
);
970 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
971 "bootindex", "/namespace@1,0",
972 DEVICE(obj
), &error_abort
);
975 static const TypeInfo nvme_info
= {
977 .parent
= TYPE_PCI_DEVICE
,
978 .instance_size
= sizeof(NvmeCtrl
),
979 .class_init
= nvme_class_init
,
980 .instance_init
= nvme_instance_init
,
983 static void nvme_register_types(void)
985 type_register_static(&nvme_info
);
988 type_init(nvme_register_types
)