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.2, 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]>, \
21 * cmb_size_mb=<cmb_size_mb[optional]>, \
22 * num_queues=<N[optional]>
24 * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
25 * offset 0 in BAR2 and supports only WDS, RDS and SQS for now.
28 #include "qemu/osdep.h"
29 #include "qemu/units.h"
30 #include "hw/block/block.h"
32 #include "hw/pci/msix.h"
33 #include "hw/pci/pci.h"
34 #include "sysemu/sysemu.h"
35 #include "qapi/error.h"
36 #include "qapi/visitor.h"
37 #include "sysemu/block-backend.h"
40 #include "qemu/cutils.h"
44 #define NVME_GUEST_ERR(trace, fmt, ...) \
46 (trace_##trace)(__VA_ARGS__); \
47 qemu_log_mask(LOG_GUEST_ERROR, #trace \
48 " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
51 static void nvme_process_sq(void *opaque
);
53 static void nvme_addr_read(NvmeCtrl
*n
, hwaddr addr
, void *buf
, int size
)
55 if (n
->cmbsz
&& addr
>= n
->ctrl_mem
.addr
&&
56 addr
< (n
->ctrl_mem
.addr
+ int128_get64(n
->ctrl_mem
.size
))) {
57 memcpy(buf
, (void *)&n
->cmbuf
[addr
- n
->ctrl_mem
.addr
], size
);
59 pci_dma_read(&n
->parent_obj
, addr
, buf
, size
);
63 static int nvme_check_sqid(NvmeCtrl
*n
, uint16_t sqid
)
65 return sqid
< n
->num_queues
&& n
->sq
[sqid
] != NULL
? 0 : -1;
68 static int nvme_check_cqid(NvmeCtrl
*n
, uint16_t cqid
)
70 return cqid
< n
->num_queues
&& n
->cq
[cqid
] != NULL
? 0 : -1;
73 static void nvme_inc_cq_tail(NvmeCQueue
*cq
)
76 if (cq
->tail
>= cq
->size
) {
78 cq
->phase
= !cq
->phase
;
82 static void nvme_inc_sq_head(NvmeSQueue
*sq
)
84 sq
->head
= (sq
->head
+ 1) % sq
->size
;
87 static uint8_t nvme_cq_full(NvmeCQueue
*cq
)
89 return (cq
->tail
+ 1) % cq
->size
== cq
->head
;
92 static uint8_t nvme_sq_empty(NvmeSQueue
*sq
)
94 return sq
->head
== sq
->tail
;
97 static void nvme_irq_check(NvmeCtrl
*n
)
99 if (msix_enabled(&(n
->parent_obj
))) {
102 if (~n
->bar
.intms
& n
->irq_status
) {
103 pci_irq_assert(&n
->parent_obj
);
105 pci_irq_deassert(&n
->parent_obj
);
109 static void nvme_irq_assert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
111 if (cq
->irq_enabled
) {
112 if (msix_enabled(&(n
->parent_obj
))) {
113 trace_nvme_irq_msix(cq
->vector
);
114 msix_notify(&(n
->parent_obj
), cq
->vector
);
116 trace_nvme_irq_pin();
117 assert(cq
->cqid
< 64);
118 n
->irq_status
|= 1 << cq
->cqid
;
122 trace_nvme_irq_masked();
126 static void nvme_irq_deassert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
128 if (cq
->irq_enabled
) {
129 if (msix_enabled(&(n
->parent_obj
))) {
132 assert(cq
->cqid
< 64);
133 n
->irq_status
&= ~(1 << cq
->cqid
);
139 static uint16_t nvme_map_prp(QEMUSGList
*qsg
, QEMUIOVector
*iov
, uint64_t prp1
,
140 uint64_t prp2
, uint32_t len
, NvmeCtrl
*n
)
142 hwaddr trans_len
= n
->page_size
- (prp1
% n
->page_size
);
143 trans_len
= MIN(len
, trans_len
);
144 int num_prps
= (len
>> n
->page_bits
) + 1;
146 if (unlikely(!prp1
)) {
147 trace_nvme_err_invalid_prp();
148 return NVME_INVALID_FIELD
| NVME_DNR
;
149 } else if (n
->cmbsz
&& prp1
>= n
->ctrl_mem
.addr
&&
150 prp1
< n
->ctrl_mem
.addr
+ int128_get64(n
->ctrl_mem
.size
)) {
152 qemu_iovec_init(iov
, num_prps
);
153 qemu_iovec_add(iov
, (void *)&n
->cmbuf
[prp1
- n
->ctrl_mem
.addr
], trans_len
);
155 pci_dma_sglist_init(qsg
, &n
->parent_obj
, num_prps
);
156 qemu_sglist_add(qsg
, prp1
, trans_len
);
160 if (unlikely(!prp2
)) {
161 trace_nvme_err_invalid_prp2_missing();
164 if (len
> n
->page_size
) {
165 uint64_t prp_list
[n
->max_prp_ents
];
166 uint32_t nents
, prp_trans
;
169 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
170 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
171 nvme_addr_read(n
, prp2
, (void *)prp_list
, prp_trans
);
173 uint64_t prp_ent
= le64_to_cpu(prp_list
[i
]);
175 if (i
== n
->max_prp_ents
- 1 && len
> n
->page_size
) {
176 if (unlikely(!prp_ent
|| prp_ent
& (n
->page_size
- 1))) {
177 trace_nvme_err_invalid_prplist_ent(prp_ent
);
182 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
183 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
184 nvme_addr_read(n
, prp_ent
, (void *)prp_list
,
186 prp_ent
= le64_to_cpu(prp_list
[i
]);
189 if (unlikely(!prp_ent
|| prp_ent
& (n
->page_size
- 1))) {
190 trace_nvme_err_invalid_prplist_ent(prp_ent
);
194 trans_len
= MIN(len
, n
->page_size
);
196 qemu_sglist_add(qsg
, prp_ent
, trans_len
);
198 qemu_iovec_add(iov
, (void *)&n
->cmbuf
[prp_ent
- n
->ctrl_mem
.addr
], trans_len
);
204 if (unlikely(prp2
& (n
->page_size
- 1))) {
205 trace_nvme_err_invalid_prp2_align(prp2
);
209 qemu_sglist_add(qsg
, prp2
, len
);
211 qemu_iovec_add(iov
, (void *)&n
->cmbuf
[prp2
- n
->ctrl_mem
.addr
], trans_len
);
218 qemu_sglist_destroy(qsg
);
219 return NVME_INVALID_FIELD
| NVME_DNR
;
222 static uint16_t nvme_dma_read_prp(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
223 uint64_t prp1
, uint64_t prp2
)
227 uint16_t status
= NVME_SUCCESS
;
229 trace_nvme_dma_read(prp1
, prp2
);
231 if (nvme_map_prp(&qsg
, &iov
, prp1
, prp2
, len
, n
)) {
232 return NVME_INVALID_FIELD
| NVME_DNR
;
235 if (unlikely(dma_buf_read(ptr
, len
, &qsg
))) {
236 trace_nvme_err_invalid_dma();
237 status
= NVME_INVALID_FIELD
| NVME_DNR
;
239 qemu_sglist_destroy(&qsg
);
241 if (unlikely(qemu_iovec_to_buf(&iov
, 0, ptr
, len
) != len
)) {
242 trace_nvme_err_invalid_dma();
243 status
= NVME_INVALID_FIELD
| NVME_DNR
;
245 qemu_iovec_destroy(&iov
);
250 static void nvme_post_cqes(void *opaque
)
252 NvmeCQueue
*cq
= opaque
;
253 NvmeCtrl
*n
= cq
->ctrl
;
254 NvmeRequest
*req
, *next
;
256 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
260 if (nvme_cq_full(cq
)) {
264 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
266 req
->cqe
.status
= cpu_to_le16((req
->status
<< 1) | cq
->phase
);
267 req
->cqe
.sq_id
= cpu_to_le16(sq
->sqid
);
268 req
->cqe
.sq_head
= cpu_to_le16(sq
->head
);
269 addr
= cq
->dma_addr
+ cq
->tail
* n
->cqe_size
;
270 nvme_inc_cq_tail(cq
);
271 pci_dma_write(&n
->parent_obj
, addr
, (void *)&req
->cqe
,
273 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
275 nvme_irq_assert(n
, cq
);
278 static void nvme_enqueue_req_completion(NvmeCQueue
*cq
, NvmeRequest
*req
)
280 assert(cq
->cqid
== req
->sq
->cqid
);
281 QTAILQ_REMOVE(&req
->sq
->out_req_list
, req
, entry
);
282 QTAILQ_INSERT_TAIL(&cq
->req_list
, req
, entry
);
283 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
286 static void nvme_rw_cb(void *opaque
, int ret
)
288 NvmeRequest
*req
= opaque
;
289 NvmeSQueue
*sq
= req
->sq
;
290 NvmeCtrl
*n
= sq
->ctrl
;
291 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
294 block_acct_done(blk_get_stats(n
->conf
.blk
), &req
->acct
);
295 req
->status
= NVME_SUCCESS
;
297 block_acct_failed(blk_get_stats(n
->conf
.blk
), &req
->acct
);
298 req
->status
= NVME_INTERNAL_DEV_ERROR
;
301 qemu_sglist_destroy(&req
->qsg
);
303 nvme_enqueue_req_completion(cq
, req
);
306 static uint16_t nvme_flush(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
310 block_acct_start(blk_get_stats(n
->conf
.blk
), &req
->acct
, 0,
312 req
->aiocb
= blk_aio_flush(n
->conf
.blk
, nvme_rw_cb
, req
);
314 return NVME_NO_COMPLETE
;
317 static uint16_t nvme_write_zeros(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
320 NvmeRwCmd
*rw
= (NvmeRwCmd
*)cmd
;
321 const uint8_t lba_index
= NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
);
322 const uint8_t data_shift
= ns
->id_ns
.lbaf
[lba_index
].ds
;
323 uint64_t slba
= le64_to_cpu(rw
->slba
);
324 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
325 uint64_t aio_slba
= slba
<< (data_shift
- BDRV_SECTOR_BITS
);
326 uint32_t aio_nlb
= nlb
<< (data_shift
- BDRV_SECTOR_BITS
);
328 if (unlikely(slba
+ nlb
> ns
->id_ns
.nsze
)) {
329 trace_nvme_err_invalid_lba_range(slba
, nlb
, ns
->id_ns
.nsze
);
330 return NVME_LBA_RANGE
| NVME_DNR
;
334 block_acct_start(blk_get_stats(n
->conf
.blk
), &req
->acct
, 0,
336 req
->aiocb
= blk_aio_pwrite_zeroes(n
->conf
.blk
, aio_slba
, aio_nlb
,
337 BDRV_REQ_MAY_UNMAP
, nvme_rw_cb
, req
);
338 return NVME_NO_COMPLETE
;
341 static uint16_t nvme_rw(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
344 NvmeRwCmd
*rw
= (NvmeRwCmd
*)cmd
;
345 uint32_t nlb
= le32_to_cpu(rw
->nlb
) + 1;
346 uint64_t slba
= le64_to_cpu(rw
->slba
);
347 uint64_t prp1
= le64_to_cpu(rw
->prp1
);
348 uint64_t prp2
= le64_to_cpu(rw
->prp2
);
350 uint8_t lba_index
= NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
);
351 uint8_t data_shift
= ns
->id_ns
.lbaf
[lba_index
].ds
;
352 uint64_t data_size
= (uint64_t)nlb
<< data_shift
;
353 uint64_t data_offset
= slba
<< data_shift
;
354 int is_write
= rw
->opcode
== NVME_CMD_WRITE
? 1 : 0;
355 enum BlockAcctType acct
= is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
;
357 trace_nvme_rw(is_write
? "write" : "read", nlb
, data_size
, slba
);
359 if (unlikely((slba
+ nlb
) > ns
->id_ns
.nsze
)) {
360 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
361 trace_nvme_err_invalid_lba_range(slba
, nlb
, ns
->id_ns
.nsze
);
362 return NVME_LBA_RANGE
| NVME_DNR
;
365 if (nvme_map_prp(&req
->qsg
, &req
->iov
, prp1
, prp2
, data_size
, n
)) {
366 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
367 return NVME_INVALID_FIELD
| NVME_DNR
;
370 dma_acct_start(n
->conf
.blk
, &req
->acct
, &req
->qsg
, acct
);
371 if (req
->qsg
.nsg
> 0) {
373 req
->aiocb
= is_write
?
374 dma_blk_write(n
->conf
.blk
, &req
->qsg
, data_offset
, BDRV_SECTOR_SIZE
,
376 dma_blk_read(n
->conf
.blk
, &req
->qsg
, data_offset
, BDRV_SECTOR_SIZE
,
380 req
->aiocb
= is_write
?
381 blk_aio_pwritev(n
->conf
.blk
, data_offset
, &req
->iov
, 0, nvme_rw_cb
,
383 blk_aio_preadv(n
->conf
.blk
, data_offset
, &req
->iov
, 0, nvme_rw_cb
,
387 return NVME_NO_COMPLETE
;
390 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
393 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
395 if (unlikely(nsid
== 0 || nsid
> n
->num_namespaces
)) {
396 trace_nvme_err_invalid_ns(nsid
, n
->num_namespaces
);
397 return NVME_INVALID_NSID
| NVME_DNR
;
400 ns
= &n
->namespaces
[nsid
- 1];
401 switch (cmd
->opcode
) {
403 return nvme_flush(n
, ns
, cmd
, req
);
404 case NVME_CMD_WRITE_ZEROS
:
405 return nvme_write_zeros(n
, ns
, cmd
, req
);
408 return nvme_rw(n
, ns
, cmd
, req
);
410 trace_nvme_err_invalid_opc(cmd
->opcode
);
411 return NVME_INVALID_OPCODE
| NVME_DNR
;
415 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
417 n
->sq
[sq
->sqid
] = NULL
;
418 timer_del(sq
->timer
);
419 timer_free(sq
->timer
);
426 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
428 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
429 NvmeRequest
*req
, *next
;
432 uint16_t qid
= le16_to_cpu(c
->qid
);
434 if (unlikely(!qid
|| nvme_check_sqid(n
, qid
))) {
435 trace_nvme_err_invalid_del_sq(qid
);
436 return NVME_INVALID_QID
| NVME_DNR
;
439 trace_nvme_del_sq(qid
);
442 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
443 req
= QTAILQ_FIRST(&sq
->out_req_list
);
445 blk_aio_cancel(req
->aiocb
);
447 if (!nvme_check_cqid(n
, sq
->cqid
)) {
448 cq
= n
->cq
[sq
->cqid
];
449 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
452 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
454 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
455 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
464 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
465 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
471 sq
->dma_addr
= dma_addr
;
475 sq
->head
= sq
->tail
= 0;
476 sq
->io_req
= g_new(NvmeRequest
, sq
->size
);
478 QTAILQ_INIT(&sq
->req_list
);
479 QTAILQ_INIT(&sq
->out_req_list
);
480 for (i
= 0; i
< sq
->size
; i
++) {
481 sq
->io_req
[i
].sq
= sq
;
482 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
484 sq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_process_sq
, sq
);
488 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
492 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
495 NvmeCreateSq
*c
= (NvmeCreateSq
*)cmd
;
497 uint16_t cqid
= le16_to_cpu(c
->cqid
);
498 uint16_t sqid
= le16_to_cpu(c
->sqid
);
499 uint16_t qsize
= le16_to_cpu(c
->qsize
);
500 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
501 uint64_t prp1
= le64_to_cpu(c
->prp1
);
503 trace_nvme_create_sq(prp1
, sqid
, cqid
, qsize
, qflags
);
505 if (unlikely(!cqid
|| nvme_check_cqid(n
, cqid
))) {
506 trace_nvme_err_invalid_create_sq_cqid(cqid
);
507 return NVME_INVALID_CQID
| NVME_DNR
;
509 if (unlikely(!sqid
|| !nvme_check_sqid(n
, sqid
))) {
510 trace_nvme_err_invalid_create_sq_sqid(sqid
);
511 return NVME_INVALID_QID
| NVME_DNR
;
513 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
))) {
514 trace_nvme_err_invalid_create_sq_size(qsize
);
515 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
517 if (unlikely(!prp1
|| prp1
& (n
->page_size
- 1))) {
518 trace_nvme_err_invalid_create_sq_addr(prp1
);
519 return NVME_INVALID_FIELD
| NVME_DNR
;
521 if (unlikely(!(NVME_SQ_FLAGS_PC(qflags
)))) {
522 trace_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags
));
523 return NVME_INVALID_FIELD
| NVME_DNR
;
525 sq
= g_malloc0(sizeof(*sq
));
526 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
530 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
532 n
->cq
[cq
->cqid
] = NULL
;
533 timer_del(cq
->timer
);
534 timer_free(cq
->timer
);
535 msix_vector_unuse(&n
->parent_obj
, cq
->vector
);
541 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
543 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
545 uint16_t qid
= le16_to_cpu(c
->qid
);
547 if (unlikely(!qid
|| nvme_check_cqid(n
, qid
))) {
548 trace_nvme_err_invalid_del_cq_cqid(qid
);
549 return NVME_INVALID_CQID
| NVME_DNR
;
553 if (unlikely(!QTAILQ_EMPTY(&cq
->sq_list
))) {
554 trace_nvme_err_invalid_del_cq_notempty(qid
);
555 return NVME_INVALID_QUEUE_DEL
;
557 trace_nvme_del_cq(qid
);
562 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
563 uint16_t cqid
, uint16_t vector
, uint16_t size
, uint16_t irq_enabled
)
568 cq
->dma_addr
= dma_addr
;
570 cq
->irq_enabled
= irq_enabled
;
572 cq
->head
= cq
->tail
= 0;
573 QTAILQ_INIT(&cq
->req_list
);
574 QTAILQ_INIT(&cq
->sq_list
);
575 msix_vector_use(&n
->parent_obj
, cq
->vector
);
577 cq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_post_cqes
, cq
);
580 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
583 NvmeCreateCq
*c
= (NvmeCreateCq
*)cmd
;
584 uint16_t cqid
= le16_to_cpu(c
->cqid
);
585 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
586 uint16_t qsize
= le16_to_cpu(c
->qsize
);
587 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
588 uint64_t prp1
= le64_to_cpu(c
->prp1
);
590 trace_nvme_create_cq(prp1
, cqid
, vector
, qsize
, qflags
,
591 NVME_CQ_FLAGS_IEN(qflags
) != 0);
593 if (unlikely(!cqid
|| !nvme_check_cqid(n
, cqid
))) {
594 trace_nvme_err_invalid_create_cq_cqid(cqid
);
595 return NVME_INVALID_CQID
| NVME_DNR
;
597 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
))) {
598 trace_nvme_err_invalid_create_cq_size(qsize
);
599 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
601 if (unlikely(!prp1
)) {
602 trace_nvme_err_invalid_create_cq_addr(prp1
);
603 return NVME_INVALID_FIELD
| NVME_DNR
;
605 if (unlikely(vector
> n
->num_queues
)) {
606 trace_nvme_err_invalid_create_cq_vector(vector
);
607 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
609 if (unlikely(!(NVME_CQ_FLAGS_PC(qflags
)))) {
610 trace_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags
));
611 return NVME_INVALID_FIELD
| NVME_DNR
;
614 cq
= g_malloc0(sizeof(*cq
));
615 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
616 NVME_CQ_FLAGS_IEN(qflags
));
620 static uint16_t nvme_identify_ctrl(NvmeCtrl
*n
, NvmeIdentify
*c
)
622 uint64_t prp1
= le64_to_cpu(c
->prp1
);
623 uint64_t prp2
= le64_to_cpu(c
->prp2
);
625 trace_nvme_identify_ctrl();
627 return nvme_dma_read_prp(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
),
631 static uint16_t nvme_identify_ns(NvmeCtrl
*n
, NvmeIdentify
*c
)
634 uint32_t nsid
= le32_to_cpu(c
->nsid
);
635 uint64_t prp1
= le64_to_cpu(c
->prp1
);
636 uint64_t prp2
= le64_to_cpu(c
->prp2
);
638 trace_nvme_identify_ns(nsid
);
640 if (unlikely(nsid
== 0 || nsid
> n
->num_namespaces
)) {
641 trace_nvme_err_invalid_ns(nsid
, n
->num_namespaces
);
642 return NVME_INVALID_NSID
| NVME_DNR
;
645 ns
= &n
->namespaces
[nsid
- 1];
647 return nvme_dma_read_prp(n
, (uint8_t *)&ns
->id_ns
, sizeof(ns
->id_ns
),
651 static uint16_t nvme_identify_nslist(NvmeCtrl
*n
, NvmeIdentify
*c
)
653 static const int data_len
= 4 * KiB
;
654 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
655 uint64_t prp1
= le64_to_cpu(c
->prp1
);
656 uint64_t prp2
= le64_to_cpu(c
->prp2
);
661 trace_nvme_identify_nslist(min_nsid
);
663 list
= g_malloc0(data_len
);
664 for (i
= 0; i
< n
->num_namespaces
; i
++) {
668 list
[j
++] = cpu_to_le32(i
+ 1);
669 if (j
== data_len
/ sizeof(uint32_t)) {
673 ret
= nvme_dma_read_prp(n
, (uint8_t *)list
, data_len
, prp1
, prp2
);
679 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeCmd
*cmd
)
681 NvmeIdentify
*c
= (NvmeIdentify
*)cmd
;
683 switch (le32_to_cpu(c
->cns
)) {
685 return nvme_identify_ns(n
, c
);
687 return nvme_identify_ctrl(n
, c
);
689 return nvme_identify_nslist(n
, c
);
691 trace_nvme_err_invalid_identify_cns(le32_to_cpu(c
->cns
));
692 return NVME_INVALID_FIELD
| NVME_DNR
;
696 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
698 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
702 case NVME_VOLATILE_WRITE_CACHE
:
703 result
= blk_enable_write_cache(n
->conf
.blk
);
704 trace_nvme_getfeat_vwcache(result
? "enabled" : "disabled");
706 case NVME_NUMBER_OF_QUEUES
:
707 result
= cpu_to_le32((n
->num_queues
- 2) | ((n
->num_queues
- 2) << 16));
708 trace_nvme_getfeat_numq(result
);
711 trace_nvme_err_invalid_getfeat(dw10
);
712 return NVME_INVALID_FIELD
| NVME_DNR
;
715 req
->cqe
.result
= result
;
719 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
721 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
722 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
725 case NVME_VOLATILE_WRITE_CACHE
:
726 blk_set_enable_write_cache(n
->conf
.blk
, dw11
& 1);
728 case NVME_NUMBER_OF_QUEUES
:
729 trace_nvme_setfeat_numq((dw11
& 0xFFFF) + 1,
730 ((dw11
>> 16) & 0xFFFF) + 1,
731 n
->num_queues
- 1, n
->num_queues
- 1);
733 cpu_to_le32((n
->num_queues
- 2) | ((n
->num_queues
- 2) << 16));
736 trace_nvme_err_invalid_setfeat(dw10
);
737 return NVME_INVALID_FIELD
| NVME_DNR
;
742 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
744 switch (cmd
->opcode
) {
745 case NVME_ADM_CMD_DELETE_SQ
:
746 return nvme_del_sq(n
, cmd
);
747 case NVME_ADM_CMD_CREATE_SQ
:
748 return nvme_create_sq(n
, cmd
);
749 case NVME_ADM_CMD_DELETE_CQ
:
750 return nvme_del_cq(n
, cmd
);
751 case NVME_ADM_CMD_CREATE_CQ
:
752 return nvme_create_cq(n
, cmd
);
753 case NVME_ADM_CMD_IDENTIFY
:
754 return nvme_identify(n
, cmd
);
755 case NVME_ADM_CMD_SET_FEATURES
:
756 return nvme_set_feature(n
, cmd
, req
);
757 case NVME_ADM_CMD_GET_FEATURES
:
758 return nvme_get_feature(n
, cmd
, req
);
760 trace_nvme_err_invalid_admin_opc(cmd
->opcode
);
761 return NVME_INVALID_OPCODE
| NVME_DNR
;
765 static void nvme_process_sq(void *opaque
)
767 NvmeSQueue
*sq
= opaque
;
768 NvmeCtrl
*n
= sq
->ctrl
;
769 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
776 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
777 addr
= sq
->dma_addr
+ sq
->head
* n
->sqe_size
;
778 nvme_addr_read(n
, addr
, (void *)&cmd
, sizeof(cmd
));
779 nvme_inc_sq_head(sq
);
781 req
= QTAILQ_FIRST(&sq
->req_list
);
782 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
783 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
784 memset(&req
->cqe
, 0, sizeof(req
->cqe
));
785 req
->cqe
.cid
= cmd
.cid
;
787 status
= sq
->sqid
? nvme_io_cmd(n
, &cmd
, req
) :
788 nvme_admin_cmd(n
, &cmd
, req
);
789 if (status
!= NVME_NO_COMPLETE
) {
790 req
->status
= status
;
791 nvme_enqueue_req_completion(cq
, req
);
796 static void nvme_clear_ctrl(NvmeCtrl
*n
)
800 for (i
= 0; i
< n
->num_queues
; i
++) {
801 if (n
->sq
[i
] != NULL
) {
802 nvme_free_sq(n
->sq
[i
], n
);
805 for (i
= 0; i
< n
->num_queues
; i
++) {
806 if (n
->cq
[i
] != NULL
) {
807 nvme_free_cq(n
->cq
[i
], n
);
811 blk_flush(n
->conf
.blk
);
815 static int nvme_start_ctrl(NvmeCtrl
*n
)
817 uint32_t page_bits
= NVME_CC_MPS(n
->bar
.cc
) + 12;
818 uint32_t page_size
= 1 << page_bits
;
820 if (unlikely(n
->cq
[0])) {
821 trace_nvme_err_startfail_cq();
824 if (unlikely(n
->sq
[0])) {
825 trace_nvme_err_startfail_sq();
828 if (unlikely(!n
->bar
.asq
)) {
829 trace_nvme_err_startfail_nbarasq();
832 if (unlikely(!n
->bar
.acq
)) {
833 trace_nvme_err_startfail_nbaracq();
836 if (unlikely(n
->bar
.asq
& (page_size
- 1))) {
837 trace_nvme_err_startfail_asq_misaligned(n
->bar
.asq
);
840 if (unlikely(n
->bar
.acq
& (page_size
- 1))) {
841 trace_nvme_err_startfail_acq_misaligned(n
->bar
.acq
);
844 if (unlikely(NVME_CC_MPS(n
->bar
.cc
) <
845 NVME_CAP_MPSMIN(n
->bar
.cap
))) {
846 trace_nvme_err_startfail_page_too_small(
847 NVME_CC_MPS(n
->bar
.cc
),
848 NVME_CAP_MPSMIN(n
->bar
.cap
));
851 if (unlikely(NVME_CC_MPS(n
->bar
.cc
) >
852 NVME_CAP_MPSMAX(n
->bar
.cap
))) {
853 trace_nvme_err_startfail_page_too_large(
854 NVME_CC_MPS(n
->bar
.cc
),
855 NVME_CAP_MPSMAX(n
->bar
.cap
));
858 if (unlikely(NVME_CC_IOCQES(n
->bar
.cc
) <
859 NVME_CTRL_CQES_MIN(n
->id_ctrl
.cqes
))) {
860 trace_nvme_err_startfail_cqent_too_small(
861 NVME_CC_IOCQES(n
->bar
.cc
),
862 NVME_CTRL_CQES_MIN(n
->bar
.cap
));
865 if (unlikely(NVME_CC_IOCQES(n
->bar
.cc
) >
866 NVME_CTRL_CQES_MAX(n
->id_ctrl
.cqes
))) {
867 trace_nvme_err_startfail_cqent_too_large(
868 NVME_CC_IOCQES(n
->bar
.cc
),
869 NVME_CTRL_CQES_MAX(n
->bar
.cap
));
872 if (unlikely(NVME_CC_IOSQES(n
->bar
.cc
) <
873 NVME_CTRL_SQES_MIN(n
->id_ctrl
.sqes
))) {
874 trace_nvme_err_startfail_sqent_too_small(
875 NVME_CC_IOSQES(n
->bar
.cc
),
876 NVME_CTRL_SQES_MIN(n
->bar
.cap
));
879 if (unlikely(NVME_CC_IOSQES(n
->bar
.cc
) >
880 NVME_CTRL_SQES_MAX(n
->id_ctrl
.sqes
))) {
881 trace_nvme_err_startfail_sqent_too_large(
882 NVME_CC_IOSQES(n
->bar
.cc
),
883 NVME_CTRL_SQES_MAX(n
->bar
.cap
));
886 if (unlikely(!NVME_AQA_ASQS(n
->bar
.aqa
))) {
887 trace_nvme_err_startfail_asqent_sz_zero();
890 if (unlikely(!NVME_AQA_ACQS(n
->bar
.aqa
))) {
891 trace_nvme_err_startfail_acqent_sz_zero();
895 n
->page_bits
= page_bits
;
896 n
->page_size
= page_size
;
897 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
898 n
->cqe_size
= 1 << NVME_CC_IOCQES(n
->bar
.cc
);
899 n
->sqe_size
= 1 << NVME_CC_IOSQES(n
->bar
.cc
);
900 nvme_init_cq(&n
->admin_cq
, n
, n
->bar
.acq
, 0, 0,
901 NVME_AQA_ACQS(n
->bar
.aqa
) + 1, 1);
902 nvme_init_sq(&n
->admin_sq
, n
, n
->bar
.asq
, 0, 0,
903 NVME_AQA_ASQS(n
->bar
.aqa
) + 1);
908 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
911 if (unlikely(offset
& (sizeof(uint32_t) - 1))) {
912 NVME_GUEST_ERR(nvme_ub_mmiowr_misaligned32
,
913 "MMIO write not 32-bit aligned,"
914 " offset=0x%"PRIx64
"", offset
);
915 /* should be ignored, fall through for now */
918 if (unlikely(size
< sizeof(uint32_t))) {
919 NVME_GUEST_ERR(nvme_ub_mmiowr_toosmall
,
920 "MMIO write smaller than 32-bits,"
921 " offset=0x%"PRIx64
", size=%u",
923 /* should be ignored, fall through for now */
927 case 0xc: /* INTMS */
928 if (unlikely(msix_enabled(&(n
->parent_obj
)))) {
929 NVME_GUEST_ERR(nvme_ub_mmiowr_intmask_with_msix
,
930 "undefined access to interrupt mask set"
931 " when MSI-X is enabled");
932 /* should be ignored, fall through for now */
934 n
->bar
.intms
|= data
& 0xffffffff;
935 n
->bar
.intmc
= n
->bar
.intms
;
936 trace_nvme_mmio_intm_set(data
& 0xffffffff,
940 case 0x10: /* INTMC */
941 if (unlikely(msix_enabled(&(n
->parent_obj
)))) {
942 NVME_GUEST_ERR(nvme_ub_mmiowr_intmask_with_msix
,
943 "undefined access to interrupt mask clr"
944 " when MSI-X is enabled");
945 /* should be ignored, fall through for now */
947 n
->bar
.intms
&= ~(data
& 0xffffffff);
948 n
->bar
.intmc
= n
->bar
.intms
;
949 trace_nvme_mmio_intm_clr(data
& 0xffffffff,
954 trace_nvme_mmio_cfg(data
& 0xffffffff);
955 /* Windows first sends data, then sends enable bit */
956 if (!NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
) &&
957 !NVME_CC_SHN(data
) && !NVME_CC_SHN(n
->bar
.cc
))
962 if (NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
)) {
964 if (unlikely(nvme_start_ctrl(n
))) {
965 trace_nvme_err_startfail();
966 n
->bar
.csts
= NVME_CSTS_FAILED
;
968 trace_nvme_mmio_start_success();
969 n
->bar
.csts
= NVME_CSTS_READY
;
971 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(n
->bar
.cc
)) {
972 trace_nvme_mmio_stopped();
974 n
->bar
.csts
&= ~NVME_CSTS_READY
;
976 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(n
->bar
.cc
))) {
977 trace_nvme_mmio_shutdown_set();
980 n
->bar
.csts
|= NVME_CSTS_SHST_COMPLETE
;
981 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(n
->bar
.cc
)) {
982 trace_nvme_mmio_shutdown_cleared();
983 n
->bar
.csts
&= ~NVME_CSTS_SHST_COMPLETE
;
987 case 0x1C: /* CSTS */
988 if (data
& (1 << 4)) {
989 NVME_GUEST_ERR(nvme_ub_mmiowr_ssreset_w1c_unsupported
,
990 "attempted to W1C CSTS.NSSRO"
991 " but CAP.NSSRS is zero (not supported)");
992 } else if (data
!= 0) {
993 NVME_GUEST_ERR(nvme_ub_mmiowr_ro_csts
,
994 "attempted to set a read only bit"
995 " of controller status");
998 case 0x20: /* NSSR */
999 if (data
== 0x4E564D65) {
1000 trace_nvme_ub_mmiowr_ssreset_unsupported();
1002 /* The spec says that writes of other values have no effect */
1006 case 0x24: /* AQA */
1007 n
->bar
.aqa
= data
& 0xffffffff;
1008 trace_nvme_mmio_aqattr(data
& 0xffffffff);
1010 case 0x28: /* ASQ */
1012 trace_nvme_mmio_asqaddr(data
);
1014 case 0x2c: /* ASQ hi */
1015 n
->bar
.asq
|= data
<< 32;
1016 trace_nvme_mmio_asqaddr_hi(data
, n
->bar
.asq
);
1018 case 0x30: /* ACQ */
1019 trace_nvme_mmio_acqaddr(data
);
1022 case 0x34: /* ACQ hi */
1023 n
->bar
.acq
|= data
<< 32;
1024 trace_nvme_mmio_acqaddr_hi(data
, n
->bar
.acq
);
1026 case 0x38: /* CMBLOC */
1027 NVME_GUEST_ERR(nvme_ub_mmiowr_cmbloc_reserved
,
1028 "invalid write to reserved CMBLOC"
1029 " when CMBSZ is zero, ignored");
1031 case 0x3C: /* CMBSZ */
1032 NVME_GUEST_ERR(nvme_ub_mmiowr_cmbsz_readonly
,
1033 "invalid write to read only CMBSZ, ignored");
1036 NVME_GUEST_ERR(nvme_ub_mmiowr_invalid
,
1037 "invalid MMIO write,"
1038 " offset=0x%"PRIx64
", data=%"PRIx64
"",
1044 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
1046 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1047 uint8_t *ptr
= (uint8_t *)&n
->bar
;
1050 if (unlikely(addr
& (sizeof(uint32_t) - 1))) {
1051 NVME_GUEST_ERR(nvme_ub_mmiord_misaligned32
,
1052 "MMIO read not 32-bit aligned,"
1053 " offset=0x%"PRIx64
"", addr
);
1054 /* should RAZ, fall through for now */
1055 } else if (unlikely(size
< sizeof(uint32_t))) {
1056 NVME_GUEST_ERR(nvme_ub_mmiord_toosmall
,
1057 "MMIO read smaller than 32-bits,"
1058 " offset=0x%"PRIx64
"", addr
);
1059 /* should RAZ, fall through for now */
1062 if (addr
< sizeof(n
->bar
)) {
1063 memcpy(&val
, ptr
+ addr
, size
);
1065 NVME_GUEST_ERR(nvme_ub_mmiord_invalid_ofs
,
1066 "MMIO read beyond last register,"
1067 " offset=0x%"PRIx64
", returning 0", addr
);
1073 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
1077 if (unlikely(addr
& ((1 << 2) - 1))) {
1078 NVME_GUEST_ERR(nvme_ub_db_wr_misaligned
,
1079 "doorbell write not 32-bit aligned,"
1080 " offset=0x%"PRIx64
", ignoring", addr
);
1084 if (((addr
- 0x1000) >> 2) & 1) {
1085 /* Completion queue doorbell write */
1087 uint16_t new_head
= val
& 0xffff;
1091 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
1092 if (unlikely(nvme_check_cqid(n
, qid
))) {
1093 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_cq
,
1094 "completion queue doorbell write"
1095 " for nonexistent queue,"
1096 " sqid=%"PRIu32
", ignoring", qid
);
1101 if (unlikely(new_head
>= cq
->size
)) {
1102 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_cqhead
,
1103 "completion queue doorbell write value"
1104 " beyond queue size, sqid=%"PRIu32
","
1105 " new_head=%"PRIu16
", ignoring",
1110 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
1111 cq
->head
= new_head
;
1114 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
1115 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
1117 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
1120 if (cq
->tail
== cq
->head
) {
1121 nvme_irq_deassert(n
, cq
);
1124 /* Submission queue doorbell write */
1126 uint16_t new_tail
= val
& 0xffff;
1129 qid
= (addr
- 0x1000) >> 3;
1130 if (unlikely(nvme_check_sqid(n
, qid
))) {
1131 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_sq
,
1132 "submission queue doorbell write"
1133 " for nonexistent queue,"
1134 " sqid=%"PRIu32
", ignoring", qid
);
1139 if (unlikely(new_tail
>= sq
->size
)) {
1140 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_sqtail
,
1141 "submission queue doorbell write value"
1142 " beyond queue size, sqid=%"PRIu32
","
1143 " new_tail=%"PRIu16
", ignoring",
1148 sq
->tail
= new_tail
;
1149 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
1153 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
1156 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1157 if (addr
< sizeof(n
->bar
)) {
1158 nvme_write_bar(n
, addr
, data
, size
);
1159 } else if (addr
>= 0x1000) {
1160 nvme_process_db(n
, addr
, data
);
1164 static const MemoryRegionOps nvme_mmio_ops
= {
1165 .read
= nvme_mmio_read
,
1166 .write
= nvme_mmio_write
,
1167 .endianness
= DEVICE_LITTLE_ENDIAN
,
1169 .min_access_size
= 2,
1170 .max_access_size
= 8,
1174 static void nvme_cmb_write(void *opaque
, hwaddr addr
, uint64_t data
,
1177 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1178 memcpy(&n
->cmbuf
[addr
], &data
, size
);
1181 static uint64_t nvme_cmb_read(void *opaque
, hwaddr addr
, unsigned size
)
1184 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1186 memcpy(&val
, &n
->cmbuf
[addr
], size
);
1190 static const MemoryRegionOps nvme_cmb_ops
= {
1191 .read
= nvme_cmb_read
,
1192 .write
= nvme_cmb_write
,
1193 .endianness
= DEVICE_LITTLE_ENDIAN
,
1195 .min_access_size
= 2,
1196 .max_access_size
= 8,
1200 static void nvme_realize(PCIDevice
*pci_dev
, Error
**errp
)
1202 NvmeCtrl
*n
= NVME(pci_dev
);
1203 NvmeIdCtrl
*id
= &n
->id_ctrl
;
1210 error_setg(errp
, "drive property not set");
1214 bs_size
= blk_getlength(n
->conf
.blk
);
1216 error_setg(errp
, "could not get backing file size");
1221 error_setg(errp
, "serial property not set");
1224 blkconf_blocksizes(&n
->conf
);
1225 if (!blkconf_apply_backend_options(&n
->conf
, blk_is_read_only(n
->conf
.blk
),
1230 pci_conf
= pci_dev
->config
;
1231 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
1232 pci_config_set_prog_interface(pci_dev
->config
, 0x2);
1233 pci_config_set_class(pci_dev
->config
, PCI_CLASS_STORAGE_EXPRESS
);
1234 pcie_endpoint_cap_init(&n
->parent_obj
, 0x80);
1236 n
->num_namespaces
= 1;
1237 n
->reg_size
= pow2ceil(0x1004 + 2 * (n
->num_queues
+ 1) * 4);
1238 n
->ns_size
= bs_size
/ (uint64_t)n
->num_namespaces
;
1240 n
->namespaces
= g_new0(NvmeNamespace
, n
->num_namespaces
);
1241 n
->sq
= g_new0(NvmeSQueue
*, n
->num_queues
);
1242 n
->cq
= g_new0(NvmeCQueue
*, n
->num_queues
);
1244 memory_region_init_io(&n
->iomem
, OBJECT(n
), &nvme_mmio_ops
, n
,
1245 "nvme", n
->reg_size
);
1246 pci_register_bar(&n
->parent_obj
, 0,
1247 PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_64
,
1249 msix_init_exclusive_bar(&n
->parent_obj
, n
->num_queues
, 4, NULL
);
1251 id
->vid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_VENDOR_ID
));
1252 id
->ssvid
= cpu_to_le16(pci_get_word(pci_conf
+ PCI_SUBSYSTEM_VENDOR_ID
));
1253 strpadcpy((char *)id
->mn
, sizeof(id
->mn
), "QEMU NVMe Ctrl", ' ');
1254 strpadcpy((char *)id
->fr
, sizeof(id
->fr
), "1.0", ' ');
1255 strpadcpy((char *)id
->sn
, sizeof(id
->sn
), n
->serial
, ' ');
1260 id
->oacs
= cpu_to_le16(0);
1263 id
->sqes
= (0x6 << 4) | 0x6;
1264 id
->cqes
= (0x4 << 4) | 0x4;
1265 id
->nn
= cpu_to_le32(n
->num_namespaces
);
1266 id
->oncs
= cpu_to_le16(NVME_ONCS_WRITE_ZEROS
);
1267 id
->psd
[0].mp
= cpu_to_le16(0x9c4);
1268 id
->psd
[0].enlat
= cpu_to_le32(0x10);
1269 id
->psd
[0].exlat
= cpu_to_le32(0x4);
1270 if (blk_enable_write_cache(n
->conf
.blk
)) {
1275 NVME_CAP_SET_MQES(n
->bar
.cap
, 0x7ff);
1276 NVME_CAP_SET_CQR(n
->bar
.cap
, 1);
1277 NVME_CAP_SET_AMS(n
->bar
.cap
, 1);
1278 NVME_CAP_SET_TO(n
->bar
.cap
, 0xf);
1279 NVME_CAP_SET_CSS(n
->bar
.cap
, 1);
1280 NVME_CAP_SET_MPSMAX(n
->bar
.cap
, 4);
1282 n
->bar
.vs
= 0x00010200;
1283 n
->bar
.intmc
= n
->bar
.intms
= 0;
1285 if (n
->cmb_size_mb
) {
1287 NVME_CMBLOC_SET_BIR(n
->bar
.cmbloc
, 2);
1288 NVME_CMBLOC_SET_OFST(n
->bar
.cmbloc
, 0);
1290 NVME_CMBSZ_SET_SQS(n
->bar
.cmbsz
, 1);
1291 NVME_CMBSZ_SET_CQS(n
->bar
.cmbsz
, 0);
1292 NVME_CMBSZ_SET_LISTS(n
->bar
.cmbsz
, 0);
1293 NVME_CMBSZ_SET_RDS(n
->bar
.cmbsz
, 1);
1294 NVME_CMBSZ_SET_WDS(n
->bar
.cmbsz
, 1);
1295 NVME_CMBSZ_SET_SZU(n
->bar
.cmbsz
, 2); /* MBs */
1296 NVME_CMBSZ_SET_SZ(n
->bar
.cmbsz
, n
->cmb_size_mb
);
1298 n
->cmbloc
= n
->bar
.cmbloc
;
1299 n
->cmbsz
= n
->bar
.cmbsz
;
1301 n
->cmbuf
= g_malloc0(NVME_CMBSZ_GETSIZE(n
->bar
.cmbsz
));
1302 memory_region_init_io(&n
->ctrl_mem
, OBJECT(n
), &nvme_cmb_ops
, n
,
1303 "nvme-cmb", NVME_CMBSZ_GETSIZE(n
->bar
.cmbsz
));
1304 pci_register_bar(&n
->parent_obj
, NVME_CMBLOC_BIR(n
->bar
.cmbloc
),
1305 PCI_BASE_ADDRESS_SPACE_MEMORY
| PCI_BASE_ADDRESS_MEM_TYPE_64
|
1306 PCI_BASE_ADDRESS_MEM_PREFETCH
, &n
->ctrl_mem
);
1310 for (i
= 0; i
< n
->num_namespaces
; i
++) {
1311 NvmeNamespace
*ns
= &n
->namespaces
[i
];
1312 NvmeIdNs
*id_ns
= &ns
->id_ns
;
1319 id_ns
->lbaf
[0].ds
= BDRV_SECTOR_BITS
;
1320 id_ns
->ncap
= id_ns
->nuse
= id_ns
->nsze
=
1321 cpu_to_le64(n
->ns_size
>>
1322 id_ns
->lbaf
[NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
)].ds
);
1326 static void nvme_exit(PCIDevice
*pci_dev
)
1328 NvmeCtrl
*n
= NVME(pci_dev
);
1331 g_free(n
->namespaces
);
1335 memory_region_unref(&n
->ctrl_mem
);
1338 msix_uninit_exclusive_bar(pci_dev
);
1341 static Property nvme_props
[] = {
1342 DEFINE_BLOCK_PROPERTIES(NvmeCtrl
, conf
),
1343 DEFINE_PROP_STRING("serial", NvmeCtrl
, serial
),
1344 DEFINE_PROP_UINT32("cmb_size_mb", NvmeCtrl
, cmb_size_mb
, 0),
1345 DEFINE_PROP_UINT32("num_queues", NvmeCtrl
, num_queues
, 64),
1346 DEFINE_PROP_END_OF_LIST(),
1349 static const VMStateDescription nvme_vmstate
= {
1354 static void nvme_class_init(ObjectClass
*oc
, void *data
)
1356 DeviceClass
*dc
= DEVICE_CLASS(oc
);
1357 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
1359 pc
->realize
= nvme_realize
;
1360 pc
->exit
= nvme_exit
;
1361 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
1362 pc
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1363 pc
->device_id
= 0x5845;
1366 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1367 dc
->desc
= "Non-Volatile Memory Express";
1368 dc
->props
= nvme_props
;
1369 dc
->vmsd
= &nvme_vmstate
;
1372 static void nvme_instance_init(Object
*obj
)
1374 NvmeCtrl
*s
= NVME(obj
);
1376 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1377 "bootindex", "/namespace@1,0",
1378 DEVICE(obj
), &error_abort
);
1381 static const TypeInfo nvme_info
= {
1383 .parent
= TYPE_PCI_DEVICE
,
1384 .instance_size
= sizeof(NvmeCtrl
),
1385 .class_init
= nvme_class_init
,
1386 .instance_init
= nvme_instance_init
,
1387 .interfaces
= (InterfaceInfo
[]) {
1388 { INTERFACE_PCIE_DEVICE
},
1393 static void nvme_register_types(void)
1395 type_register_static(&nvme_info
);
1398 type_init(nvme_register_types
)