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 <hw/block/block.h>
25 #include <hw/pci/msix.h>
26 #include <hw/pci/pci.h>
27 #include "sysemu/sysemu.h"
28 #include "qapi/visitor.h"
29 #include "sysemu/block-backend.h"
33 static void nvme_process_sq(void *opaque
);
35 static int nvme_check_sqid(NvmeCtrl
*n
, uint16_t sqid
)
37 return sqid
< n
->num_queues
&& n
->sq
[sqid
] != NULL
? 0 : -1;
40 static int nvme_check_cqid(NvmeCtrl
*n
, uint16_t cqid
)
42 return cqid
< n
->num_queues
&& n
->cq
[cqid
] != NULL
? 0 : -1;
45 static void nvme_inc_cq_tail(NvmeCQueue
*cq
)
48 if (cq
->tail
>= cq
->size
) {
50 cq
->phase
= !cq
->phase
;
54 static void nvme_inc_sq_head(NvmeSQueue
*sq
)
56 sq
->head
= (sq
->head
+ 1) % sq
->size
;
59 static uint8_t nvme_cq_full(NvmeCQueue
*cq
)
61 return (cq
->tail
+ 1) % cq
->size
== cq
->head
;
64 static uint8_t nvme_sq_empty(NvmeSQueue
*sq
)
66 return sq
->head
== sq
->tail
;
69 static void nvme_isr_notify(NvmeCtrl
*n
, NvmeCQueue
*cq
)
71 if (cq
->irq_enabled
) {
72 if (msix_enabled(&(n
->parent_obj
))) {
73 msix_notify(&(n
->parent_obj
), cq
->vector
);
75 pci_irq_pulse(&n
->parent_obj
);
80 static uint16_t nvme_map_prp(QEMUSGList
*qsg
, uint64_t prp1
, uint64_t prp2
,
81 uint32_t len
, NvmeCtrl
*n
)
83 hwaddr trans_len
= n
->page_size
- (prp1
% n
->page_size
);
84 trans_len
= MIN(len
, trans_len
);
85 int num_prps
= (len
>> n
->page_bits
) + 1;
88 return NVME_INVALID_FIELD
| NVME_DNR
;
91 pci_dma_sglist_init(qsg
, &n
->parent_obj
, num_prps
);
92 qemu_sglist_add(qsg
, prp1
, trans_len
);
98 if (len
> n
->page_size
) {
99 uint64_t prp_list
[n
->max_prp_ents
];
100 uint32_t nents
, prp_trans
;
103 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
104 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
105 pci_dma_read(&n
->parent_obj
, prp2
, (void *)prp_list
, prp_trans
);
107 uint64_t prp_ent
= le64_to_cpu(prp_list
[i
]);
109 if (i
== n
->max_prp_ents
- 1 && len
> n
->page_size
) {
110 if (!prp_ent
|| prp_ent
& (n
->page_size
- 1)) {
115 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
116 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
117 pci_dma_read(&n
->parent_obj
, prp_ent
, (void *)prp_list
,
119 prp_ent
= le64_to_cpu(prp_list
[i
]);
122 if (!prp_ent
|| prp_ent
& (n
->page_size
- 1)) {
126 trans_len
= MIN(len
, n
->page_size
);
127 qemu_sglist_add(qsg
, prp_ent
, trans_len
);
132 if (prp2
& (n
->page_size
- 1)) {
135 qemu_sglist_add(qsg
, prp2
, len
);
141 qemu_sglist_destroy(qsg
);
142 return NVME_INVALID_FIELD
| NVME_DNR
;
145 static uint16_t nvme_dma_read_prp(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
146 uint64_t prp1
, uint64_t prp2
)
150 if (nvme_map_prp(&qsg
, prp1
, prp2
, len
, n
)) {
151 return NVME_INVALID_FIELD
| NVME_DNR
;
153 if (dma_buf_read(ptr
, len
, &qsg
)) {
154 qemu_sglist_destroy(&qsg
);
155 return NVME_INVALID_FIELD
| NVME_DNR
;
157 qemu_sglist_destroy(&qsg
);
161 static void nvme_post_cqes(void *opaque
)
163 NvmeCQueue
*cq
= opaque
;
164 NvmeCtrl
*n
= cq
->ctrl
;
165 NvmeRequest
*req
, *next
;
167 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
171 if (nvme_cq_full(cq
)) {
175 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
177 req
->cqe
.status
= cpu_to_le16((req
->status
<< 1) | cq
->phase
);
178 req
->cqe
.sq_id
= cpu_to_le16(sq
->sqid
);
179 req
->cqe
.sq_head
= cpu_to_le16(sq
->head
);
180 addr
= cq
->dma_addr
+ cq
->tail
* n
->cqe_size
;
181 nvme_inc_cq_tail(cq
);
182 pci_dma_write(&n
->parent_obj
, addr
, (void *)&req
->cqe
,
184 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
186 nvme_isr_notify(n
, cq
);
189 static void nvme_enqueue_req_completion(NvmeCQueue
*cq
, NvmeRequest
*req
)
191 assert(cq
->cqid
== req
->sq
->cqid
);
192 QTAILQ_REMOVE(&req
->sq
->out_req_list
, req
, entry
);
193 QTAILQ_INSERT_TAIL(&cq
->req_list
, req
, entry
);
194 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
197 static void nvme_rw_cb(void *opaque
, int ret
)
199 NvmeRequest
*req
= opaque
;
200 NvmeSQueue
*sq
= req
->sq
;
201 NvmeCtrl
*n
= sq
->ctrl
;
202 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
205 block_acct_done(blk_get_stats(n
->conf
.blk
), &req
->acct
);
206 req
->status
= NVME_SUCCESS
;
208 block_acct_failed(blk_get_stats(n
->conf
.blk
), &req
->acct
);
209 req
->status
= NVME_INTERNAL_DEV_ERROR
;
212 qemu_sglist_destroy(&req
->qsg
);
214 nvme_enqueue_req_completion(cq
, req
);
217 static uint16_t nvme_flush(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
221 block_acct_start(blk_get_stats(n
->conf
.blk
), &req
->acct
, 0,
223 req
->aiocb
= blk_aio_flush(n
->conf
.blk
, nvme_rw_cb
, req
);
225 return NVME_NO_COMPLETE
;
228 static uint16_t nvme_rw(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
231 NvmeRwCmd
*rw
= (NvmeRwCmd
*)cmd
;
232 uint32_t nlb
= le32_to_cpu(rw
->nlb
) + 1;
233 uint64_t slba
= le64_to_cpu(rw
->slba
);
234 uint64_t prp1
= le64_to_cpu(rw
->prp1
);
235 uint64_t prp2
= le64_to_cpu(rw
->prp2
);
237 uint8_t lba_index
= NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
);
238 uint8_t data_shift
= ns
->id_ns
.lbaf
[lba_index
].ds
;
239 uint64_t data_size
= (uint64_t)nlb
<< data_shift
;
240 uint64_t aio_slba
= slba
<< (data_shift
- BDRV_SECTOR_BITS
);
241 int is_write
= rw
->opcode
== NVME_CMD_WRITE
? 1 : 0;
242 enum BlockAcctType acct
= is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
;
244 if ((slba
+ nlb
) > ns
->id_ns
.nsze
) {
245 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
246 return NVME_LBA_RANGE
| NVME_DNR
;
249 if (nvme_map_prp(&req
->qsg
, prp1
, prp2
, data_size
, n
)) {
250 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
251 return NVME_INVALID_FIELD
| NVME_DNR
;
254 assert((nlb
<< data_shift
) == req
->qsg
.size
);
257 dma_acct_start(n
->conf
.blk
, &req
->acct
, &req
->qsg
, acct
);
258 req
->aiocb
= is_write
?
259 dma_blk_write(n
->conf
.blk
, &req
->qsg
, aio_slba
, nvme_rw_cb
, req
) :
260 dma_blk_read(n
->conf
.blk
, &req
->qsg
, aio_slba
, nvme_rw_cb
, req
);
262 return NVME_NO_COMPLETE
;
265 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
268 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
270 if (nsid
== 0 || nsid
> n
->num_namespaces
) {
271 return NVME_INVALID_NSID
| NVME_DNR
;
274 ns
= &n
->namespaces
[nsid
- 1];
275 switch (cmd
->opcode
) {
277 return nvme_flush(n
, ns
, cmd
, req
);
280 return nvme_rw(n
, ns
, cmd
, req
);
282 return NVME_INVALID_OPCODE
| NVME_DNR
;
286 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
288 n
->sq
[sq
->sqid
] = NULL
;
289 timer_del(sq
->timer
);
290 timer_free(sq
->timer
);
297 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
299 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
300 NvmeRequest
*req
, *next
;
303 uint16_t qid
= le16_to_cpu(c
->qid
);
305 if (!qid
|| nvme_check_sqid(n
, qid
)) {
306 return NVME_INVALID_QID
| NVME_DNR
;
310 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
311 req
= QTAILQ_FIRST(&sq
->out_req_list
);
313 blk_aio_cancel(req
->aiocb
);
315 if (!nvme_check_cqid(n
, sq
->cqid
)) {
316 cq
= n
->cq
[sq
->cqid
];
317 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
320 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
322 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
323 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
332 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
333 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
339 sq
->dma_addr
= dma_addr
;
343 sq
->head
= sq
->tail
= 0;
344 sq
->io_req
= g_new(NvmeRequest
, sq
->size
);
346 QTAILQ_INIT(&sq
->req_list
);
347 QTAILQ_INIT(&sq
->out_req_list
);
348 for (i
= 0; i
< sq
->size
; i
++) {
349 sq
->io_req
[i
].sq
= sq
;
350 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
352 sq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_process_sq
, sq
);
356 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
360 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
363 NvmeCreateSq
*c
= (NvmeCreateSq
*)cmd
;
365 uint16_t cqid
= le16_to_cpu(c
->cqid
);
366 uint16_t sqid
= le16_to_cpu(c
->sqid
);
367 uint16_t qsize
= le16_to_cpu(c
->qsize
);
368 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
369 uint64_t prp1
= le64_to_cpu(c
->prp1
);
371 if (!cqid
|| nvme_check_cqid(n
, cqid
)) {
372 return NVME_INVALID_CQID
| NVME_DNR
;
374 if (!sqid
|| (sqid
&& !nvme_check_sqid(n
, sqid
))) {
375 return NVME_INVALID_QID
| NVME_DNR
;
377 if (!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
)) {
378 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
380 if (!prp1
|| prp1
& (n
->page_size
- 1)) {
381 return NVME_INVALID_FIELD
| NVME_DNR
;
383 if (!(NVME_SQ_FLAGS_PC(qflags
))) {
384 return NVME_INVALID_FIELD
| NVME_DNR
;
386 sq
= g_malloc0(sizeof(*sq
));
387 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
391 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
393 n
->cq
[cq
->cqid
] = NULL
;
394 timer_del(cq
->timer
);
395 timer_free(cq
->timer
);
396 msix_vector_unuse(&n
->parent_obj
, cq
->vector
);
402 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
404 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
406 uint16_t qid
= le16_to_cpu(c
->qid
);
408 if (!qid
|| nvme_check_cqid(n
, qid
)) {
409 return NVME_INVALID_CQID
| NVME_DNR
;
413 if (!QTAILQ_EMPTY(&cq
->sq_list
)) {
414 return NVME_INVALID_QUEUE_DEL
;
420 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
421 uint16_t cqid
, uint16_t vector
, uint16_t size
, uint16_t irq_enabled
)
426 cq
->dma_addr
= dma_addr
;
428 cq
->irq_enabled
= irq_enabled
;
430 cq
->head
= cq
->tail
= 0;
431 QTAILQ_INIT(&cq
->req_list
);
432 QTAILQ_INIT(&cq
->sq_list
);
433 msix_vector_use(&n
->parent_obj
, cq
->vector
);
435 cq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_post_cqes
, cq
);
438 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
441 NvmeCreateCq
*c
= (NvmeCreateCq
*)cmd
;
442 uint16_t cqid
= le16_to_cpu(c
->cqid
);
443 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
444 uint16_t qsize
= le16_to_cpu(c
->qsize
);
445 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
446 uint64_t prp1
= le64_to_cpu(c
->prp1
);
448 if (!cqid
|| (cqid
&& !nvme_check_cqid(n
, cqid
))) {
449 return NVME_INVALID_CQID
| NVME_DNR
;
451 if (!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
)) {
452 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
455 return NVME_INVALID_FIELD
| NVME_DNR
;
457 if (vector
> n
->num_queues
) {
458 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
460 if (!(NVME_CQ_FLAGS_PC(qflags
))) {
461 return NVME_INVALID_FIELD
| NVME_DNR
;
464 cq
= g_malloc0(sizeof(*cq
));
465 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
466 NVME_CQ_FLAGS_IEN(qflags
));
470 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeCmd
*cmd
)
473 NvmeIdentify
*c
= (NvmeIdentify
*)cmd
;
474 uint32_t cns
= le32_to_cpu(c
->cns
);
475 uint32_t nsid
= le32_to_cpu(c
->nsid
);
476 uint64_t prp1
= le64_to_cpu(c
->prp1
);
477 uint64_t prp2
= le64_to_cpu(c
->prp2
);
480 return nvme_dma_read_prp(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
),
483 if (nsid
== 0 || nsid
> n
->num_namespaces
) {
484 return NVME_INVALID_NSID
| NVME_DNR
;
487 ns
= &n
->namespaces
[nsid
- 1];
488 return nvme_dma_read_prp(n
, (uint8_t *)&ns
->id_ns
, sizeof(ns
->id_ns
),
492 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
494 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
498 case NVME_VOLATILE_WRITE_CACHE
:
499 result
= blk_enable_write_cache(n
->conf
.blk
);
501 case NVME_NUMBER_OF_QUEUES
:
502 result
= cpu_to_le32((n
->num_queues
- 1) | ((n
->num_queues
- 1) << 16));
505 return NVME_INVALID_FIELD
| NVME_DNR
;
508 req
->cqe
.result
= result
;
512 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
514 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
515 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
518 case NVME_VOLATILE_WRITE_CACHE
:
519 blk_set_enable_write_cache(n
->conf
.blk
, dw11
& 1);
521 case NVME_NUMBER_OF_QUEUES
:
523 cpu_to_le32((n
->num_queues
- 1) | ((n
->num_queues
- 1) << 16));
526 return NVME_INVALID_FIELD
| NVME_DNR
;
531 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
533 switch (cmd
->opcode
) {
534 case NVME_ADM_CMD_DELETE_SQ
:
535 return nvme_del_sq(n
, cmd
);
536 case NVME_ADM_CMD_CREATE_SQ
:
537 return nvme_create_sq(n
, cmd
);
538 case NVME_ADM_CMD_DELETE_CQ
:
539 return nvme_del_cq(n
, cmd
);
540 case NVME_ADM_CMD_CREATE_CQ
:
541 return nvme_create_cq(n
, cmd
);
542 case NVME_ADM_CMD_IDENTIFY
:
543 return nvme_identify(n
, cmd
);
544 case NVME_ADM_CMD_SET_FEATURES
:
545 return nvme_set_feature(n
, cmd
, req
);
546 case NVME_ADM_CMD_GET_FEATURES
:
547 return nvme_get_feature(n
, cmd
, req
);
549 return NVME_INVALID_OPCODE
| NVME_DNR
;
553 static void nvme_process_sq(void *opaque
)
555 NvmeSQueue
*sq
= opaque
;
556 NvmeCtrl
*n
= sq
->ctrl
;
557 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
564 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
565 addr
= sq
->dma_addr
+ sq
->head
* n
->sqe_size
;
566 pci_dma_read(&n
->parent_obj
, addr
, (void *)&cmd
, sizeof(cmd
));
567 nvme_inc_sq_head(sq
);
569 req
= QTAILQ_FIRST(&sq
->req_list
);
570 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
571 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
572 memset(&req
->cqe
, 0, sizeof(req
->cqe
));
573 req
->cqe
.cid
= cmd
.cid
;
575 status
= sq
->sqid
? nvme_io_cmd(n
, &cmd
, req
) :
576 nvme_admin_cmd(n
, &cmd
, req
);
577 if (status
!= NVME_NO_COMPLETE
) {
578 req
->status
= status
;
579 nvme_enqueue_req_completion(cq
, req
);
584 static void nvme_clear_ctrl(NvmeCtrl
*n
)
588 for (i
= 0; i
< n
->num_queues
; i
++) {
589 if (n
->sq
[i
] != NULL
) {
590 nvme_free_sq(n
->sq
[i
], n
);
593 for (i
= 0; i
< n
->num_queues
; i
++) {
594 if (n
->cq
[i
] != NULL
) {
595 nvme_free_cq(n
->cq
[i
], n
);
599 blk_flush(n
->conf
.blk
);
603 static int nvme_start_ctrl(NvmeCtrl
*n
)
605 uint32_t page_bits
= NVME_CC_MPS(n
->bar
.cc
) + 12;
606 uint32_t page_size
= 1 << page_bits
;
608 if (n
->cq
[0] || n
->sq
[0] || !n
->bar
.asq
|| !n
->bar
.acq
||
609 n
->bar
.asq
& (page_size
- 1) || n
->bar
.acq
& (page_size
- 1) ||
610 NVME_CC_MPS(n
->bar
.cc
) < NVME_CAP_MPSMIN(n
->bar
.cap
) ||
611 NVME_CC_MPS(n
->bar
.cc
) > NVME_CAP_MPSMAX(n
->bar
.cap
) ||
612 NVME_CC_IOCQES(n
->bar
.cc
) < NVME_CTRL_CQES_MIN(n
->id_ctrl
.cqes
) ||
613 NVME_CC_IOCQES(n
->bar
.cc
) > NVME_CTRL_CQES_MAX(n
->id_ctrl
.cqes
) ||
614 NVME_CC_IOSQES(n
->bar
.cc
) < NVME_CTRL_SQES_MIN(n
->id_ctrl
.sqes
) ||
615 NVME_CC_IOSQES(n
->bar
.cc
) > NVME_CTRL_SQES_MAX(n
->id_ctrl
.sqes
) ||
616 !NVME_AQA_ASQS(n
->bar
.aqa
) || !NVME_AQA_ACQS(n
->bar
.aqa
)) {
620 n
->page_bits
= page_bits
;
621 n
->page_size
= page_size
;
622 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
623 n
->cqe_size
= 1 << NVME_CC_IOCQES(n
->bar
.cc
);
624 n
->sqe_size
= 1 << NVME_CC_IOSQES(n
->bar
.cc
);
625 nvme_init_cq(&n
->admin_cq
, n
, n
->bar
.acq
, 0, 0,
626 NVME_AQA_ACQS(n
->bar
.aqa
) + 1, 1);
627 nvme_init_sq(&n
->admin_sq
, n
, n
->bar
.asq
, 0, 0,
628 NVME_AQA_ASQS(n
->bar
.aqa
) + 1);
633 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
638 n
->bar
.intms
|= data
& 0xffffffff;
639 n
->bar
.intmc
= n
->bar
.intms
;
642 n
->bar
.intms
&= ~(data
& 0xffffffff);
643 n
->bar
.intmc
= n
->bar
.intms
;
646 /* Windows first sends data, then sends enable bit */
647 if (!NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
) &&
648 !NVME_CC_SHN(data
) && !NVME_CC_SHN(n
->bar
.cc
))
653 if (NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
)) {
655 if (nvme_start_ctrl(n
)) {
656 n
->bar
.csts
= NVME_CSTS_FAILED
;
658 n
->bar
.csts
= NVME_CSTS_READY
;
660 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(n
->bar
.cc
)) {
662 n
->bar
.csts
&= ~NVME_CSTS_READY
;
664 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(n
->bar
.cc
))) {
667 n
->bar
.csts
|= NVME_CSTS_SHST_COMPLETE
;
668 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(n
->bar
.cc
)) {
669 n
->bar
.csts
&= ~NVME_CSTS_SHST_COMPLETE
;
674 n
->bar
.aqa
= data
& 0xffffffff;
680 n
->bar
.asq
|= data
<< 32;
686 n
->bar
.acq
|= data
<< 32;
693 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
695 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
696 uint8_t *ptr
= (uint8_t *)&n
->bar
;
699 if (addr
< sizeof(n
->bar
)) {
700 memcpy(&val
, ptr
+ addr
, size
);
705 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
709 if (addr
& ((1 << 2) - 1)) {
713 if (((addr
- 0x1000) >> 2) & 1) {
714 uint16_t new_head
= val
& 0xffff;
718 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
719 if (nvme_check_cqid(n
, qid
)) {
724 if (new_head
>= cq
->size
) {
728 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
732 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
733 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
735 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
738 if (cq
->tail
!= cq
->head
) {
739 nvme_isr_notify(n
, cq
);
742 uint16_t new_tail
= val
& 0xffff;
745 qid
= (addr
- 0x1000) >> 3;
746 if (nvme_check_sqid(n
, qid
)) {
751 if (new_tail
>= sq
->size
) {
756 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
760 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
763 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
764 if (addr
< sizeof(n
->bar
)) {
765 nvme_write_bar(n
, addr
, data
, size
);
766 } else if (addr
>= 0x1000) {
767 nvme_process_db(n
, addr
, data
);
771 static const MemoryRegionOps nvme_mmio_ops
= {
772 .read
= nvme_mmio_read
,
773 .write
= nvme_mmio_write
,
774 .endianness
= DEVICE_LITTLE_ENDIAN
,
776 .min_access_size
= 2,
777 .max_access_size
= 8,
781 static int nvme_init(PCIDevice
*pci_dev
)
783 NvmeCtrl
*n
= NVME(pci_dev
);
784 NvmeIdCtrl
*id
= &n
->id_ctrl
;
794 bs_size
= blk_getlength(n
->conf
.blk
);
799 blkconf_serial(&n
->conf
, &n
->serial
);
803 blkconf_blocksizes(&n
->conf
);
805 pci_conf
= pci_dev
->config
;
806 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
807 pci_config_set_prog_interface(pci_dev
->config
, 0x2);
808 pci_config_set_class(pci_dev
->config
, PCI_CLASS_STORAGE_EXPRESS
);
809 pcie_endpoint_cap_init(&n
->parent_obj
, 0x80);
811 n
->num_namespaces
= 1;
813 n
->reg_size
= pow2ceil(0x1004 + 2 * (n
->num_queues
+ 1) * 4);
814 n
->ns_size
= bs_size
/ (uint64_t)n
->num_namespaces
;
816 n
->namespaces
= g_new0(NvmeNamespace
, n
->num_namespaces
);
817 n
->sq
= g_new0(NvmeSQueue
*, n
->num_queues
);
818 n
->cq
= g_new0(NvmeCQueue
*, n
->num_queues
);
820 memory_region_init_io(&n
->iomem
, OBJECT(n
), &nvme_mmio_ops
, n
,
821 "nvme", n
->reg_size
);
822 pci_register_bar(&n
->parent_obj
, 0,
823 PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_64
,
825 msix_init_exclusive_bar(&n
->parent_obj
, n
->num_queues
, 4);
827 id
->vid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_VENDOR_ID
));
828 id
->ssvid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_SUBSYSTEM_VENDOR_ID
));
829 strpadcpy((char *)id
->mn
, sizeof(id
->mn
), "QEMU NVMe Ctrl", ' ');
830 strpadcpy((char *)id
->fr
, sizeof(id
->fr
), "1.0", ' ');
831 strpadcpy((char *)id
->sn
, sizeof(id
->sn
), n
->serial
, ' ');
836 id
->oacs
= cpu_to_le16(0);
839 id
->sqes
= (0x6 << 4) | 0x6;
840 id
->cqes
= (0x4 << 4) | 0x4;
841 id
->nn
= cpu_to_le32(n
->num_namespaces
);
842 id
->psd
[0].mp
= cpu_to_le16(0x9c4);
843 id
->psd
[0].enlat
= cpu_to_le32(0x10);
844 id
->psd
[0].exlat
= cpu_to_le32(0x4);
845 if (blk_enable_write_cache(n
->conf
.blk
)) {
850 NVME_CAP_SET_MQES(n
->bar
.cap
, 0x7ff);
851 NVME_CAP_SET_CQR(n
->bar
.cap
, 1);
852 NVME_CAP_SET_AMS(n
->bar
.cap
, 1);
853 NVME_CAP_SET_TO(n
->bar
.cap
, 0xf);
854 NVME_CAP_SET_CSS(n
->bar
.cap
, 1);
855 NVME_CAP_SET_MPSMAX(n
->bar
.cap
, 4);
857 n
->bar
.vs
= 0x00010100;
858 n
->bar
.intmc
= n
->bar
.intms
= 0;
860 for (i
= 0; i
< n
->num_namespaces
; i
++) {
861 NvmeNamespace
*ns
= &n
->namespaces
[i
];
862 NvmeIdNs
*id_ns
= &ns
->id_ns
;
869 id_ns
->lbaf
[0].ds
= BDRV_SECTOR_BITS
;
870 id_ns
->ncap
= id_ns
->nuse
= id_ns
->nsze
=
871 cpu_to_le64(n
->ns_size
>>
872 id_ns
->lbaf
[NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
)].ds
);
877 static void nvme_exit(PCIDevice
*pci_dev
)
879 NvmeCtrl
*n
= NVME(pci_dev
);
882 g_free(n
->namespaces
);
885 msix_uninit_exclusive_bar(pci_dev
);
888 static Property nvme_props
[] = {
889 DEFINE_BLOCK_PROPERTIES(NvmeCtrl
, conf
),
890 DEFINE_PROP_STRING("serial", NvmeCtrl
, serial
),
891 DEFINE_PROP_END_OF_LIST(),
894 static const VMStateDescription nvme_vmstate
= {
899 static void nvme_class_init(ObjectClass
*oc
, void *data
)
901 DeviceClass
*dc
= DEVICE_CLASS(oc
);
902 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
904 pc
->init
= nvme_init
;
905 pc
->exit
= nvme_exit
;
906 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
907 pc
->vendor_id
= PCI_VENDOR_ID_INTEL
;
908 pc
->device_id
= 0x5845;
912 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
913 dc
->desc
= "Non-Volatile Memory Express";
914 dc
->props
= nvme_props
;
915 dc
->vmsd
= &nvme_vmstate
;
918 static void nvme_get_bootindex(Object
*obj
, Visitor
*v
, void *opaque
,
919 const char *name
, Error
**errp
)
921 NvmeCtrl
*s
= NVME(obj
);
923 visit_type_int32(v
, &s
->conf
.bootindex
, name
, errp
);
926 static void nvme_set_bootindex(Object
*obj
, Visitor
*v
, void *opaque
,
927 const char *name
, Error
**errp
)
929 NvmeCtrl
*s
= NVME(obj
);
931 Error
*local_err
= NULL
;
933 visit_type_int32(v
, &boot_index
, name
, &local_err
);
937 /* check whether bootindex is present in fw_boot_order list */
938 check_boot_index(boot_index
, &local_err
);
942 /* change bootindex to a new one */
943 s
->conf
.bootindex
= boot_index
;
947 error_propagate(errp
, local_err
);
951 static void nvme_instance_init(Object
*obj
)
953 object_property_add(obj
, "bootindex", "int32",
955 nvme_set_bootindex
, NULL
, NULL
, NULL
);
956 object_property_set_int(obj
, -1, "bootindex", NULL
);
959 static const TypeInfo nvme_info
= {
961 .parent
= TYPE_PCI_DEVICE
,
962 .instance_size
= sizeof(NvmeCtrl
),
963 .class_init
= nvme_class_init
,
964 .instance_init
= nvme_instance_init
,
967 static void nvme_register_types(void)
969 type_register_static(&nvme_info
);
972 type_init(nvme_register_types
)