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]>
23 * Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at
24 * offset 0 in BAR2 and supports only WDS, RDS and SQS for now.
27 #include "qemu/osdep.h"
28 #include "hw/block/block.h"
30 #include "hw/pci/msix.h"
31 #include "hw/pci/pci.h"
32 #include "sysemu/sysemu.h"
33 #include "qapi/error.h"
34 #include "qapi/visitor.h"
35 #include "sysemu/block-backend.h"
38 #include "qemu/cutils.h"
42 #define NVME_GUEST_ERR(trace, fmt, ...) \
44 (trace_##trace)(__VA_ARGS__); \
45 qemu_log_mask(LOG_GUEST_ERROR, #trace \
46 " in %s: " fmt "\n", __func__, ## __VA_ARGS__); \
49 static void nvme_process_sq(void *opaque
);
51 static void nvme_addr_read(NvmeCtrl
*n
, hwaddr addr
, void *buf
, int size
)
53 if (n
->cmbsz
&& addr
>= n
->ctrl_mem
.addr
&&
54 addr
< (n
->ctrl_mem
.addr
+ int128_get64(n
->ctrl_mem
.size
))) {
55 memcpy(buf
, (void *)&n
->cmbuf
[addr
- n
->ctrl_mem
.addr
], size
);
57 pci_dma_read(&n
->parent_obj
, addr
, buf
, size
);
61 static int nvme_check_sqid(NvmeCtrl
*n
, uint16_t sqid
)
63 return sqid
< n
->num_queues
&& n
->sq
[sqid
] != NULL
? 0 : -1;
66 static int nvme_check_cqid(NvmeCtrl
*n
, uint16_t cqid
)
68 return cqid
< n
->num_queues
&& n
->cq
[cqid
] != NULL
? 0 : -1;
71 static void nvme_inc_cq_tail(NvmeCQueue
*cq
)
74 if (cq
->tail
>= cq
->size
) {
76 cq
->phase
= !cq
->phase
;
80 static void nvme_inc_sq_head(NvmeSQueue
*sq
)
82 sq
->head
= (sq
->head
+ 1) % sq
->size
;
85 static uint8_t nvme_cq_full(NvmeCQueue
*cq
)
87 return (cq
->tail
+ 1) % cq
->size
== cq
->head
;
90 static uint8_t nvme_sq_empty(NvmeSQueue
*sq
)
92 return sq
->head
== sq
->tail
;
95 static void nvme_irq_check(NvmeCtrl
*n
)
97 if (msix_enabled(&(n
->parent_obj
))) {
100 if (~n
->bar
.intms
& n
->irq_status
) {
101 pci_irq_assert(&n
->parent_obj
);
103 pci_irq_deassert(&n
->parent_obj
);
107 static void nvme_irq_assert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
109 if (cq
->irq_enabled
) {
110 if (msix_enabled(&(n
->parent_obj
))) {
111 trace_nvme_irq_msix(cq
->vector
);
112 msix_notify(&(n
->parent_obj
), cq
->vector
);
114 trace_nvme_irq_pin();
115 assert(cq
->cqid
< 64);
116 n
->irq_status
|= 1 << cq
->cqid
;
120 trace_nvme_irq_masked();
124 static void nvme_irq_deassert(NvmeCtrl
*n
, NvmeCQueue
*cq
)
126 if (cq
->irq_enabled
) {
127 if (msix_enabled(&(n
->parent_obj
))) {
130 assert(cq
->cqid
< 64);
131 n
->irq_status
&= ~(1 << cq
->cqid
);
137 static uint16_t nvme_map_prp(QEMUSGList
*qsg
, QEMUIOVector
*iov
, uint64_t prp1
,
138 uint64_t prp2
, uint32_t len
, NvmeCtrl
*n
)
140 hwaddr trans_len
= n
->page_size
- (prp1
% n
->page_size
);
141 trans_len
= MIN(len
, trans_len
);
142 int num_prps
= (len
>> n
->page_bits
) + 1;
144 if (unlikely(!prp1
)) {
145 trace_nvme_err_invalid_prp();
146 return NVME_INVALID_FIELD
| NVME_DNR
;
147 } else if (n
->cmbsz
&& prp1
>= n
->ctrl_mem
.addr
&&
148 prp1
< n
->ctrl_mem
.addr
+ int128_get64(n
->ctrl_mem
.size
)) {
150 qemu_iovec_init(iov
, num_prps
);
151 qemu_iovec_add(iov
, (void *)&n
->cmbuf
[prp1
- n
->ctrl_mem
.addr
], trans_len
);
153 pci_dma_sglist_init(qsg
, &n
->parent_obj
, num_prps
);
154 qemu_sglist_add(qsg
, prp1
, trans_len
);
158 if (unlikely(!prp2
)) {
159 trace_nvme_err_invalid_prp2_missing();
162 if (len
> n
->page_size
) {
163 uint64_t prp_list
[n
->max_prp_ents
];
164 uint32_t nents
, prp_trans
;
167 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
168 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
169 nvme_addr_read(n
, prp2
, (void *)prp_list
, prp_trans
);
171 uint64_t prp_ent
= le64_to_cpu(prp_list
[i
]);
173 if (i
== n
->max_prp_ents
- 1 && len
> n
->page_size
) {
174 if (unlikely(!prp_ent
|| prp_ent
& (n
->page_size
- 1))) {
175 trace_nvme_err_invalid_prplist_ent(prp_ent
);
180 nents
= (len
+ n
->page_size
- 1) >> n
->page_bits
;
181 prp_trans
= MIN(n
->max_prp_ents
, nents
) * sizeof(uint64_t);
182 nvme_addr_read(n
, prp_ent
, (void *)prp_list
,
184 prp_ent
= le64_to_cpu(prp_list
[i
]);
187 if (unlikely(!prp_ent
|| prp_ent
& (n
->page_size
- 1))) {
188 trace_nvme_err_invalid_prplist_ent(prp_ent
);
192 trans_len
= MIN(len
, n
->page_size
);
194 qemu_sglist_add(qsg
, prp_ent
, trans_len
);
196 qemu_iovec_add(iov
, (void *)&n
->cmbuf
[prp_ent
- n
->ctrl_mem
.addr
], trans_len
);
202 if (unlikely(prp2
& (n
->page_size
- 1))) {
203 trace_nvme_err_invalid_prp2_align(prp2
);
207 qemu_sglist_add(qsg
, prp2
, len
);
209 qemu_iovec_add(iov
, (void *)&n
->cmbuf
[prp2
- n
->ctrl_mem
.addr
], trans_len
);
216 qemu_sglist_destroy(qsg
);
217 return NVME_INVALID_FIELD
| NVME_DNR
;
220 static uint16_t nvme_dma_read_prp(NvmeCtrl
*n
, uint8_t *ptr
, uint32_t len
,
221 uint64_t prp1
, uint64_t prp2
)
225 uint16_t status
= NVME_SUCCESS
;
227 trace_nvme_dma_read(prp1
, prp2
);
229 if (nvme_map_prp(&qsg
, &iov
, prp1
, prp2
, len
, n
)) {
230 return NVME_INVALID_FIELD
| NVME_DNR
;
233 if (unlikely(dma_buf_read(ptr
, len
, &qsg
))) {
234 trace_nvme_err_invalid_dma();
235 status
= NVME_INVALID_FIELD
| NVME_DNR
;
237 qemu_sglist_destroy(&qsg
);
239 if (unlikely(qemu_iovec_to_buf(&iov
, 0, ptr
, len
) != len
)) {
240 trace_nvme_err_invalid_dma();
241 status
= NVME_INVALID_FIELD
| NVME_DNR
;
243 qemu_iovec_destroy(&iov
);
248 static void nvme_post_cqes(void *opaque
)
250 NvmeCQueue
*cq
= opaque
;
251 NvmeCtrl
*n
= cq
->ctrl
;
252 NvmeRequest
*req
, *next
;
254 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
258 if (nvme_cq_full(cq
)) {
262 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
264 req
->cqe
.status
= cpu_to_le16((req
->status
<< 1) | cq
->phase
);
265 req
->cqe
.sq_id
= cpu_to_le16(sq
->sqid
);
266 req
->cqe
.sq_head
= cpu_to_le16(sq
->head
);
267 addr
= cq
->dma_addr
+ cq
->tail
* n
->cqe_size
;
268 nvme_inc_cq_tail(cq
);
269 pci_dma_write(&n
->parent_obj
, addr
, (void *)&req
->cqe
,
271 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
273 nvme_irq_assert(n
, cq
);
276 static void nvme_enqueue_req_completion(NvmeCQueue
*cq
, NvmeRequest
*req
)
278 assert(cq
->cqid
== req
->sq
->cqid
);
279 QTAILQ_REMOVE(&req
->sq
->out_req_list
, req
, entry
);
280 QTAILQ_INSERT_TAIL(&cq
->req_list
, req
, entry
);
281 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
284 static void nvme_rw_cb(void *opaque
, int ret
)
286 NvmeRequest
*req
= opaque
;
287 NvmeSQueue
*sq
= req
->sq
;
288 NvmeCtrl
*n
= sq
->ctrl
;
289 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
292 block_acct_done(blk_get_stats(n
->conf
.blk
), &req
->acct
);
293 req
->status
= NVME_SUCCESS
;
295 block_acct_failed(blk_get_stats(n
->conf
.blk
), &req
->acct
);
296 req
->status
= NVME_INTERNAL_DEV_ERROR
;
299 qemu_sglist_destroy(&req
->qsg
);
301 nvme_enqueue_req_completion(cq
, req
);
304 static uint16_t nvme_flush(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
308 block_acct_start(blk_get_stats(n
->conf
.blk
), &req
->acct
, 0,
310 req
->aiocb
= blk_aio_flush(n
->conf
.blk
, nvme_rw_cb
, req
);
312 return NVME_NO_COMPLETE
;
315 static uint16_t nvme_write_zeros(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
318 NvmeRwCmd
*rw
= (NvmeRwCmd
*)cmd
;
319 const uint8_t lba_index
= NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
);
320 const uint8_t data_shift
= ns
->id_ns
.lbaf
[lba_index
].ds
;
321 uint64_t slba
= le64_to_cpu(rw
->slba
);
322 uint32_t nlb
= le16_to_cpu(rw
->nlb
) + 1;
323 uint64_t aio_slba
= slba
<< (data_shift
- BDRV_SECTOR_BITS
);
324 uint32_t aio_nlb
= nlb
<< (data_shift
- BDRV_SECTOR_BITS
);
326 if (unlikely(slba
+ nlb
> ns
->id_ns
.nsze
)) {
327 trace_nvme_err_invalid_lba_range(slba
, nlb
, ns
->id_ns
.nsze
);
328 return NVME_LBA_RANGE
| NVME_DNR
;
332 block_acct_start(blk_get_stats(n
->conf
.blk
), &req
->acct
, 0,
334 req
->aiocb
= blk_aio_pwrite_zeroes(n
->conf
.blk
, aio_slba
, aio_nlb
,
335 BDRV_REQ_MAY_UNMAP
, nvme_rw_cb
, req
);
336 return NVME_NO_COMPLETE
;
339 static uint16_t nvme_rw(NvmeCtrl
*n
, NvmeNamespace
*ns
, NvmeCmd
*cmd
,
342 NvmeRwCmd
*rw
= (NvmeRwCmd
*)cmd
;
343 uint32_t nlb
= le32_to_cpu(rw
->nlb
) + 1;
344 uint64_t slba
= le64_to_cpu(rw
->slba
);
345 uint64_t prp1
= le64_to_cpu(rw
->prp1
);
346 uint64_t prp2
= le64_to_cpu(rw
->prp2
);
348 uint8_t lba_index
= NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
);
349 uint8_t data_shift
= ns
->id_ns
.lbaf
[lba_index
].ds
;
350 uint64_t data_size
= (uint64_t)nlb
<< data_shift
;
351 uint64_t data_offset
= slba
<< data_shift
;
352 int is_write
= rw
->opcode
== NVME_CMD_WRITE
? 1 : 0;
353 enum BlockAcctType acct
= is_write
? BLOCK_ACCT_WRITE
: BLOCK_ACCT_READ
;
355 trace_nvme_rw(is_write
? "write" : "read", nlb
, data_size
, slba
);
357 if (unlikely((slba
+ nlb
) > ns
->id_ns
.nsze
)) {
358 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
359 trace_nvme_err_invalid_lba_range(slba
, nlb
, ns
->id_ns
.nsze
);
360 return NVME_LBA_RANGE
| NVME_DNR
;
363 if (nvme_map_prp(&req
->qsg
, &req
->iov
, prp1
, prp2
, data_size
, n
)) {
364 block_acct_invalid(blk_get_stats(n
->conf
.blk
), acct
);
365 return NVME_INVALID_FIELD
| NVME_DNR
;
368 dma_acct_start(n
->conf
.blk
, &req
->acct
, &req
->qsg
, acct
);
369 if (req
->qsg
.nsg
> 0) {
371 req
->aiocb
= is_write
?
372 dma_blk_write(n
->conf
.blk
, &req
->qsg
, data_offset
, BDRV_SECTOR_SIZE
,
374 dma_blk_read(n
->conf
.blk
, &req
->qsg
, data_offset
, BDRV_SECTOR_SIZE
,
378 req
->aiocb
= is_write
?
379 blk_aio_pwritev(n
->conf
.blk
, data_offset
, &req
->iov
, 0, nvme_rw_cb
,
381 blk_aio_preadv(n
->conf
.blk
, data_offset
, &req
->iov
, 0, nvme_rw_cb
,
385 return NVME_NO_COMPLETE
;
388 static uint16_t nvme_io_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
391 uint32_t nsid
= le32_to_cpu(cmd
->nsid
);
393 if (unlikely(nsid
== 0 || nsid
> n
->num_namespaces
)) {
394 trace_nvme_err_invalid_ns(nsid
, n
->num_namespaces
);
395 return NVME_INVALID_NSID
| NVME_DNR
;
398 ns
= &n
->namespaces
[nsid
- 1];
399 switch (cmd
->opcode
) {
401 return nvme_flush(n
, ns
, cmd
, req
);
402 case NVME_CMD_WRITE_ZEROS
:
403 return nvme_write_zeros(n
, ns
, cmd
, req
);
406 return nvme_rw(n
, ns
, cmd
, req
);
408 trace_nvme_err_invalid_opc(cmd
->opcode
);
409 return NVME_INVALID_OPCODE
| NVME_DNR
;
413 static void nvme_free_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
)
415 n
->sq
[sq
->sqid
] = NULL
;
416 timer_del(sq
->timer
);
417 timer_free(sq
->timer
);
424 static uint16_t nvme_del_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
426 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
427 NvmeRequest
*req
, *next
;
430 uint16_t qid
= le16_to_cpu(c
->qid
);
432 if (unlikely(!qid
|| nvme_check_sqid(n
, qid
))) {
433 trace_nvme_err_invalid_del_sq(qid
);
434 return NVME_INVALID_QID
| NVME_DNR
;
437 trace_nvme_del_sq(qid
);
440 while (!QTAILQ_EMPTY(&sq
->out_req_list
)) {
441 req
= QTAILQ_FIRST(&sq
->out_req_list
);
443 blk_aio_cancel(req
->aiocb
);
445 if (!nvme_check_cqid(n
, sq
->cqid
)) {
446 cq
= n
->cq
[sq
->cqid
];
447 QTAILQ_REMOVE(&cq
->sq_list
, sq
, entry
);
450 QTAILQ_FOREACH_SAFE(req
, &cq
->req_list
, entry
, next
) {
452 QTAILQ_REMOVE(&cq
->req_list
, req
, entry
);
453 QTAILQ_INSERT_TAIL(&sq
->req_list
, req
, entry
);
462 static void nvme_init_sq(NvmeSQueue
*sq
, NvmeCtrl
*n
, uint64_t dma_addr
,
463 uint16_t sqid
, uint16_t cqid
, uint16_t size
)
469 sq
->dma_addr
= dma_addr
;
473 sq
->head
= sq
->tail
= 0;
474 sq
->io_req
= g_new(NvmeRequest
, sq
->size
);
476 QTAILQ_INIT(&sq
->req_list
);
477 QTAILQ_INIT(&sq
->out_req_list
);
478 for (i
= 0; i
< sq
->size
; i
++) {
479 sq
->io_req
[i
].sq
= sq
;
480 QTAILQ_INSERT_TAIL(&(sq
->req_list
), &sq
->io_req
[i
], entry
);
482 sq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_process_sq
, sq
);
486 QTAILQ_INSERT_TAIL(&(cq
->sq_list
), sq
, entry
);
490 static uint16_t nvme_create_sq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
493 NvmeCreateSq
*c
= (NvmeCreateSq
*)cmd
;
495 uint16_t cqid
= le16_to_cpu(c
->cqid
);
496 uint16_t sqid
= le16_to_cpu(c
->sqid
);
497 uint16_t qsize
= le16_to_cpu(c
->qsize
);
498 uint16_t qflags
= le16_to_cpu(c
->sq_flags
);
499 uint64_t prp1
= le64_to_cpu(c
->prp1
);
501 trace_nvme_create_sq(prp1
, sqid
, cqid
, qsize
, qflags
);
503 if (unlikely(!cqid
|| nvme_check_cqid(n
, cqid
))) {
504 trace_nvme_err_invalid_create_sq_cqid(cqid
);
505 return NVME_INVALID_CQID
| NVME_DNR
;
507 if (unlikely(!sqid
|| !nvme_check_sqid(n
, sqid
))) {
508 trace_nvme_err_invalid_create_sq_sqid(sqid
);
509 return NVME_INVALID_QID
| NVME_DNR
;
511 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
))) {
512 trace_nvme_err_invalid_create_sq_size(qsize
);
513 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
515 if (unlikely(!prp1
|| prp1
& (n
->page_size
- 1))) {
516 trace_nvme_err_invalid_create_sq_addr(prp1
);
517 return NVME_INVALID_FIELD
| NVME_DNR
;
519 if (unlikely(!(NVME_SQ_FLAGS_PC(qflags
)))) {
520 trace_nvme_err_invalid_create_sq_qflags(NVME_SQ_FLAGS_PC(qflags
));
521 return NVME_INVALID_FIELD
| NVME_DNR
;
523 sq
= g_malloc0(sizeof(*sq
));
524 nvme_init_sq(sq
, n
, prp1
, sqid
, cqid
, qsize
+ 1);
528 static void nvme_free_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
)
530 n
->cq
[cq
->cqid
] = NULL
;
531 timer_del(cq
->timer
);
532 timer_free(cq
->timer
);
533 msix_vector_unuse(&n
->parent_obj
, cq
->vector
);
539 static uint16_t nvme_del_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
541 NvmeDeleteQ
*c
= (NvmeDeleteQ
*)cmd
;
543 uint16_t qid
= le16_to_cpu(c
->qid
);
545 if (unlikely(!qid
|| nvme_check_cqid(n
, qid
))) {
546 trace_nvme_err_invalid_del_cq_cqid(qid
);
547 return NVME_INVALID_CQID
| NVME_DNR
;
551 if (unlikely(!QTAILQ_EMPTY(&cq
->sq_list
))) {
552 trace_nvme_err_invalid_del_cq_notempty(qid
);
553 return NVME_INVALID_QUEUE_DEL
;
555 trace_nvme_del_cq(qid
);
560 static void nvme_init_cq(NvmeCQueue
*cq
, NvmeCtrl
*n
, uint64_t dma_addr
,
561 uint16_t cqid
, uint16_t vector
, uint16_t size
, uint16_t irq_enabled
)
566 cq
->dma_addr
= dma_addr
;
568 cq
->irq_enabled
= irq_enabled
;
570 cq
->head
= cq
->tail
= 0;
571 QTAILQ_INIT(&cq
->req_list
);
572 QTAILQ_INIT(&cq
->sq_list
);
573 msix_vector_use(&n
->parent_obj
, cq
->vector
);
575 cq
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, nvme_post_cqes
, cq
);
578 static uint16_t nvme_create_cq(NvmeCtrl
*n
, NvmeCmd
*cmd
)
581 NvmeCreateCq
*c
= (NvmeCreateCq
*)cmd
;
582 uint16_t cqid
= le16_to_cpu(c
->cqid
);
583 uint16_t vector
= le16_to_cpu(c
->irq_vector
);
584 uint16_t qsize
= le16_to_cpu(c
->qsize
);
585 uint16_t qflags
= le16_to_cpu(c
->cq_flags
);
586 uint64_t prp1
= le64_to_cpu(c
->prp1
);
588 trace_nvme_create_cq(prp1
, cqid
, vector
, qsize
, qflags
,
589 NVME_CQ_FLAGS_IEN(qflags
) != 0);
591 if (unlikely(!cqid
|| !nvme_check_cqid(n
, cqid
))) {
592 trace_nvme_err_invalid_create_cq_cqid(cqid
);
593 return NVME_INVALID_CQID
| NVME_DNR
;
595 if (unlikely(!qsize
|| qsize
> NVME_CAP_MQES(n
->bar
.cap
))) {
596 trace_nvme_err_invalid_create_cq_size(qsize
);
597 return NVME_MAX_QSIZE_EXCEEDED
| NVME_DNR
;
599 if (unlikely(!prp1
)) {
600 trace_nvme_err_invalid_create_cq_addr(prp1
);
601 return NVME_INVALID_FIELD
| NVME_DNR
;
603 if (unlikely(vector
> n
->num_queues
)) {
604 trace_nvme_err_invalid_create_cq_vector(vector
);
605 return NVME_INVALID_IRQ_VECTOR
| NVME_DNR
;
607 if (unlikely(!(NVME_CQ_FLAGS_PC(qflags
)))) {
608 trace_nvme_err_invalid_create_cq_qflags(NVME_CQ_FLAGS_PC(qflags
));
609 return NVME_INVALID_FIELD
| NVME_DNR
;
612 cq
= g_malloc0(sizeof(*cq
));
613 nvme_init_cq(cq
, n
, prp1
, cqid
, vector
, qsize
+ 1,
614 NVME_CQ_FLAGS_IEN(qflags
));
618 static uint16_t nvme_identify_ctrl(NvmeCtrl
*n
, NvmeIdentify
*c
)
620 uint64_t prp1
= le64_to_cpu(c
->prp1
);
621 uint64_t prp2
= le64_to_cpu(c
->prp2
);
623 trace_nvme_identify_ctrl();
625 return nvme_dma_read_prp(n
, (uint8_t *)&n
->id_ctrl
, sizeof(n
->id_ctrl
),
629 static uint16_t nvme_identify_ns(NvmeCtrl
*n
, NvmeIdentify
*c
)
632 uint32_t nsid
= le32_to_cpu(c
->nsid
);
633 uint64_t prp1
= le64_to_cpu(c
->prp1
);
634 uint64_t prp2
= le64_to_cpu(c
->prp2
);
636 trace_nvme_identify_ns(nsid
);
638 if (unlikely(nsid
== 0 || nsid
> n
->num_namespaces
)) {
639 trace_nvme_err_invalid_ns(nsid
, n
->num_namespaces
);
640 return NVME_INVALID_NSID
| NVME_DNR
;
643 ns
= &n
->namespaces
[nsid
- 1];
645 return nvme_dma_read_prp(n
, (uint8_t *)&ns
->id_ns
, sizeof(ns
->id_ns
),
649 static uint16_t nvme_identify_nslist(NvmeCtrl
*n
, NvmeIdentify
*c
)
651 static const int data_len
= 4096;
652 uint32_t min_nsid
= le32_to_cpu(c
->nsid
);
653 uint64_t prp1
= le64_to_cpu(c
->prp1
);
654 uint64_t prp2
= le64_to_cpu(c
->prp2
);
659 trace_nvme_identify_nslist(min_nsid
);
661 list
= g_malloc0(data_len
);
662 for (i
= 0; i
< n
->num_namespaces
; i
++) {
666 list
[j
++] = cpu_to_le32(i
+ 1);
667 if (j
== data_len
/ sizeof(uint32_t)) {
671 ret
= nvme_dma_read_prp(n
, (uint8_t *)list
, data_len
, prp1
, prp2
);
677 static uint16_t nvme_identify(NvmeCtrl
*n
, NvmeCmd
*cmd
)
679 NvmeIdentify
*c
= (NvmeIdentify
*)cmd
;
681 switch (le32_to_cpu(c
->cns
)) {
683 return nvme_identify_ns(n
, c
);
685 return nvme_identify_ctrl(n
, c
);
687 return nvme_identify_nslist(n
, c
);
689 trace_nvme_err_invalid_identify_cns(le32_to_cpu(c
->cns
));
690 return NVME_INVALID_FIELD
| NVME_DNR
;
694 static uint16_t nvme_get_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
696 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
700 case NVME_VOLATILE_WRITE_CACHE
:
701 result
= blk_enable_write_cache(n
->conf
.blk
);
702 trace_nvme_getfeat_vwcache(result
? "enabled" : "disabled");
704 case NVME_NUMBER_OF_QUEUES
:
705 result
= cpu_to_le32((n
->num_queues
- 2) | ((n
->num_queues
- 2) << 16));
706 trace_nvme_getfeat_numq(result
);
709 trace_nvme_err_invalid_getfeat(dw10
);
710 return NVME_INVALID_FIELD
| NVME_DNR
;
713 req
->cqe
.result
= result
;
717 static uint16_t nvme_set_feature(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
719 uint32_t dw10
= le32_to_cpu(cmd
->cdw10
);
720 uint32_t dw11
= le32_to_cpu(cmd
->cdw11
);
723 case NVME_VOLATILE_WRITE_CACHE
:
724 blk_set_enable_write_cache(n
->conf
.blk
, dw11
& 1);
726 case NVME_NUMBER_OF_QUEUES
:
727 trace_nvme_setfeat_numq((dw11
& 0xFFFF) + 1,
728 ((dw11
>> 16) & 0xFFFF) + 1,
729 n
->num_queues
- 1, n
->num_queues
- 1);
731 cpu_to_le32((n
->num_queues
- 2) | ((n
->num_queues
- 2) << 16));
734 trace_nvme_err_invalid_setfeat(dw10
);
735 return NVME_INVALID_FIELD
| NVME_DNR
;
740 static uint16_t nvme_admin_cmd(NvmeCtrl
*n
, NvmeCmd
*cmd
, NvmeRequest
*req
)
742 switch (cmd
->opcode
) {
743 case NVME_ADM_CMD_DELETE_SQ
:
744 return nvme_del_sq(n
, cmd
);
745 case NVME_ADM_CMD_CREATE_SQ
:
746 return nvme_create_sq(n
, cmd
);
747 case NVME_ADM_CMD_DELETE_CQ
:
748 return nvme_del_cq(n
, cmd
);
749 case NVME_ADM_CMD_CREATE_CQ
:
750 return nvme_create_cq(n
, cmd
);
751 case NVME_ADM_CMD_IDENTIFY
:
752 return nvme_identify(n
, cmd
);
753 case NVME_ADM_CMD_SET_FEATURES
:
754 return nvme_set_feature(n
, cmd
, req
);
755 case NVME_ADM_CMD_GET_FEATURES
:
756 return nvme_get_feature(n
, cmd
, req
);
758 trace_nvme_err_invalid_admin_opc(cmd
->opcode
);
759 return NVME_INVALID_OPCODE
| NVME_DNR
;
763 static void nvme_process_sq(void *opaque
)
765 NvmeSQueue
*sq
= opaque
;
766 NvmeCtrl
*n
= sq
->ctrl
;
767 NvmeCQueue
*cq
= n
->cq
[sq
->cqid
];
774 while (!(nvme_sq_empty(sq
) || QTAILQ_EMPTY(&sq
->req_list
))) {
775 addr
= sq
->dma_addr
+ sq
->head
* n
->sqe_size
;
776 nvme_addr_read(n
, addr
, (void *)&cmd
, sizeof(cmd
));
777 nvme_inc_sq_head(sq
);
779 req
= QTAILQ_FIRST(&sq
->req_list
);
780 QTAILQ_REMOVE(&sq
->req_list
, req
, entry
);
781 QTAILQ_INSERT_TAIL(&sq
->out_req_list
, req
, entry
);
782 memset(&req
->cqe
, 0, sizeof(req
->cqe
));
783 req
->cqe
.cid
= cmd
.cid
;
785 status
= sq
->sqid
? nvme_io_cmd(n
, &cmd
, req
) :
786 nvme_admin_cmd(n
, &cmd
, req
);
787 if (status
!= NVME_NO_COMPLETE
) {
788 req
->status
= status
;
789 nvme_enqueue_req_completion(cq
, req
);
794 static void nvme_clear_ctrl(NvmeCtrl
*n
)
798 for (i
= 0; i
< n
->num_queues
; i
++) {
799 if (n
->sq
[i
] != NULL
) {
800 nvme_free_sq(n
->sq
[i
], n
);
803 for (i
= 0; i
< n
->num_queues
; i
++) {
804 if (n
->cq
[i
] != NULL
) {
805 nvme_free_cq(n
->cq
[i
], n
);
809 blk_flush(n
->conf
.blk
);
813 static int nvme_start_ctrl(NvmeCtrl
*n
)
815 uint32_t page_bits
= NVME_CC_MPS(n
->bar
.cc
) + 12;
816 uint32_t page_size
= 1 << page_bits
;
818 if (unlikely(n
->cq
[0])) {
819 trace_nvme_err_startfail_cq();
822 if (unlikely(n
->sq
[0])) {
823 trace_nvme_err_startfail_sq();
826 if (unlikely(!n
->bar
.asq
)) {
827 trace_nvme_err_startfail_nbarasq();
830 if (unlikely(!n
->bar
.acq
)) {
831 trace_nvme_err_startfail_nbaracq();
834 if (unlikely(n
->bar
.asq
& (page_size
- 1))) {
835 trace_nvme_err_startfail_asq_misaligned(n
->bar
.asq
);
838 if (unlikely(n
->bar
.acq
& (page_size
- 1))) {
839 trace_nvme_err_startfail_acq_misaligned(n
->bar
.acq
);
842 if (unlikely(NVME_CC_MPS(n
->bar
.cc
) <
843 NVME_CAP_MPSMIN(n
->bar
.cap
))) {
844 trace_nvme_err_startfail_page_too_small(
845 NVME_CC_MPS(n
->bar
.cc
),
846 NVME_CAP_MPSMIN(n
->bar
.cap
));
849 if (unlikely(NVME_CC_MPS(n
->bar
.cc
) >
850 NVME_CAP_MPSMAX(n
->bar
.cap
))) {
851 trace_nvme_err_startfail_page_too_large(
852 NVME_CC_MPS(n
->bar
.cc
),
853 NVME_CAP_MPSMAX(n
->bar
.cap
));
856 if (unlikely(NVME_CC_IOCQES(n
->bar
.cc
) <
857 NVME_CTRL_CQES_MIN(n
->id_ctrl
.cqes
))) {
858 trace_nvme_err_startfail_cqent_too_small(
859 NVME_CC_IOCQES(n
->bar
.cc
),
860 NVME_CTRL_CQES_MIN(n
->bar
.cap
));
863 if (unlikely(NVME_CC_IOCQES(n
->bar
.cc
) >
864 NVME_CTRL_CQES_MAX(n
->id_ctrl
.cqes
))) {
865 trace_nvme_err_startfail_cqent_too_large(
866 NVME_CC_IOCQES(n
->bar
.cc
),
867 NVME_CTRL_CQES_MAX(n
->bar
.cap
));
870 if (unlikely(NVME_CC_IOSQES(n
->bar
.cc
) <
871 NVME_CTRL_SQES_MIN(n
->id_ctrl
.sqes
))) {
872 trace_nvme_err_startfail_sqent_too_small(
873 NVME_CC_IOSQES(n
->bar
.cc
),
874 NVME_CTRL_SQES_MIN(n
->bar
.cap
));
877 if (unlikely(NVME_CC_IOSQES(n
->bar
.cc
) >
878 NVME_CTRL_SQES_MAX(n
->id_ctrl
.sqes
))) {
879 trace_nvme_err_startfail_sqent_too_large(
880 NVME_CC_IOSQES(n
->bar
.cc
),
881 NVME_CTRL_SQES_MAX(n
->bar
.cap
));
884 if (unlikely(!NVME_AQA_ASQS(n
->bar
.aqa
))) {
885 trace_nvme_err_startfail_asqent_sz_zero();
888 if (unlikely(!NVME_AQA_ACQS(n
->bar
.aqa
))) {
889 trace_nvme_err_startfail_acqent_sz_zero();
893 n
->page_bits
= page_bits
;
894 n
->page_size
= page_size
;
895 n
->max_prp_ents
= n
->page_size
/ sizeof(uint64_t);
896 n
->cqe_size
= 1 << NVME_CC_IOCQES(n
->bar
.cc
);
897 n
->sqe_size
= 1 << NVME_CC_IOSQES(n
->bar
.cc
);
898 nvme_init_cq(&n
->admin_cq
, n
, n
->bar
.acq
, 0, 0,
899 NVME_AQA_ACQS(n
->bar
.aqa
) + 1, 1);
900 nvme_init_sq(&n
->admin_sq
, n
, n
->bar
.asq
, 0, 0,
901 NVME_AQA_ASQS(n
->bar
.aqa
) + 1);
906 static void nvme_write_bar(NvmeCtrl
*n
, hwaddr offset
, uint64_t data
,
909 if (unlikely(offset
& (sizeof(uint32_t) - 1))) {
910 NVME_GUEST_ERR(nvme_ub_mmiowr_misaligned32
,
911 "MMIO write not 32-bit aligned,"
912 " offset=0x%"PRIx64
"", offset
);
913 /* should be ignored, fall through for now */
916 if (unlikely(size
< sizeof(uint32_t))) {
917 NVME_GUEST_ERR(nvme_ub_mmiowr_toosmall
,
918 "MMIO write smaller than 32-bits,"
919 " offset=0x%"PRIx64
", size=%u",
921 /* should be ignored, fall through for now */
925 case 0xc: /* INTMS */
926 if (unlikely(msix_enabled(&(n
->parent_obj
)))) {
927 NVME_GUEST_ERR(nvme_ub_mmiowr_intmask_with_msix
,
928 "undefined access to interrupt mask set"
929 " when MSI-X is enabled");
930 /* should be ignored, fall through for now */
932 n
->bar
.intms
|= data
& 0xffffffff;
933 n
->bar
.intmc
= n
->bar
.intms
;
934 trace_nvme_mmio_intm_set(data
& 0xffffffff,
938 case 0x10: /* INTMC */
939 if (unlikely(msix_enabled(&(n
->parent_obj
)))) {
940 NVME_GUEST_ERR(nvme_ub_mmiowr_intmask_with_msix
,
941 "undefined access to interrupt mask clr"
942 " when MSI-X is enabled");
943 /* should be ignored, fall through for now */
945 n
->bar
.intms
&= ~(data
& 0xffffffff);
946 n
->bar
.intmc
= n
->bar
.intms
;
947 trace_nvme_mmio_intm_clr(data
& 0xffffffff,
952 trace_nvme_mmio_cfg(data
& 0xffffffff);
953 /* Windows first sends data, then sends enable bit */
954 if (!NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
) &&
955 !NVME_CC_SHN(data
) && !NVME_CC_SHN(n
->bar
.cc
))
960 if (NVME_CC_EN(data
) && !NVME_CC_EN(n
->bar
.cc
)) {
962 if (unlikely(nvme_start_ctrl(n
))) {
963 trace_nvme_err_startfail();
964 n
->bar
.csts
= NVME_CSTS_FAILED
;
966 trace_nvme_mmio_start_success();
967 n
->bar
.csts
= NVME_CSTS_READY
;
969 } else if (!NVME_CC_EN(data
) && NVME_CC_EN(n
->bar
.cc
)) {
970 trace_nvme_mmio_stopped();
972 n
->bar
.csts
&= ~NVME_CSTS_READY
;
974 if (NVME_CC_SHN(data
) && !(NVME_CC_SHN(n
->bar
.cc
))) {
975 trace_nvme_mmio_shutdown_set();
978 n
->bar
.csts
|= NVME_CSTS_SHST_COMPLETE
;
979 } else if (!NVME_CC_SHN(data
) && NVME_CC_SHN(n
->bar
.cc
)) {
980 trace_nvme_mmio_shutdown_cleared();
981 n
->bar
.csts
&= ~NVME_CSTS_SHST_COMPLETE
;
985 case 0x1C: /* CSTS */
986 if (data
& (1 << 4)) {
987 NVME_GUEST_ERR(nvme_ub_mmiowr_ssreset_w1c_unsupported
,
988 "attempted to W1C CSTS.NSSRO"
989 " but CAP.NSSRS is zero (not supported)");
990 } else if (data
!= 0) {
991 NVME_GUEST_ERR(nvme_ub_mmiowr_ro_csts
,
992 "attempted to set a read only bit"
993 " of controller status");
996 case 0x20: /* NSSR */
997 if (data
== 0x4E564D65) {
998 trace_nvme_ub_mmiowr_ssreset_unsupported();
1000 /* The spec says that writes of other values have no effect */
1004 case 0x24: /* AQA */
1005 n
->bar
.aqa
= data
& 0xffffffff;
1006 trace_nvme_mmio_aqattr(data
& 0xffffffff);
1008 case 0x28: /* ASQ */
1010 trace_nvme_mmio_asqaddr(data
);
1012 case 0x2c: /* ASQ hi */
1013 n
->bar
.asq
|= data
<< 32;
1014 trace_nvme_mmio_asqaddr_hi(data
, n
->bar
.asq
);
1016 case 0x30: /* ACQ */
1017 trace_nvme_mmio_acqaddr(data
);
1020 case 0x34: /* ACQ hi */
1021 n
->bar
.acq
|= data
<< 32;
1022 trace_nvme_mmio_acqaddr_hi(data
, n
->bar
.acq
);
1024 case 0x38: /* CMBLOC */
1025 NVME_GUEST_ERR(nvme_ub_mmiowr_cmbloc_reserved
,
1026 "invalid write to reserved CMBLOC"
1027 " when CMBSZ is zero, ignored");
1029 case 0x3C: /* CMBSZ */
1030 NVME_GUEST_ERR(nvme_ub_mmiowr_cmbsz_readonly
,
1031 "invalid write to read only CMBSZ, ignored");
1034 NVME_GUEST_ERR(nvme_ub_mmiowr_invalid
,
1035 "invalid MMIO write,"
1036 " offset=0x%"PRIx64
", data=%"PRIx64
"",
1042 static uint64_t nvme_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
1044 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1045 uint8_t *ptr
= (uint8_t *)&n
->bar
;
1048 if (unlikely(addr
& (sizeof(uint32_t) - 1))) {
1049 NVME_GUEST_ERR(nvme_ub_mmiord_misaligned32
,
1050 "MMIO read not 32-bit aligned,"
1051 " offset=0x%"PRIx64
"", addr
);
1052 /* should RAZ, fall through for now */
1053 } else if (unlikely(size
< sizeof(uint32_t))) {
1054 NVME_GUEST_ERR(nvme_ub_mmiord_toosmall
,
1055 "MMIO read smaller than 32-bits,"
1056 " offset=0x%"PRIx64
"", addr
);
1057 /* should RAZ, fall through for now */
1060 if (addr
< sizeof(n
->bar
)) {
1061 memcpy(&val
, ptr
+ addr
, size
);
1063 NVME_GUEST_ERR(nvme_ub_mmiord_invalid_ofs
,
1064 "MMIO read beyond last register,"
1065 " offset=0x%"PRIx64
", returning 0", addr
);
1071 static void nvme_process_db(NvmeCtrl
*n
, hwaddr addr
, int val
)
1075 if (unlikely(addr
& ((1 << 2) - 1))) {
1076 NVME_GUEST_ERR(nvme_ub_db_wr_misaligned
,
1077 "doorbell write not 32-bit aligned,"
1078 " offset=0x%"PRIx64
", ignoring", addr
);
1082 if (((addr
- 0x1000) >> 2) & 1) {
1083 /* Completion queue doorbell write */
1085 uint16_t new_head
= val
& 0xffff;
1089 qid
= (addr
- (0x1000 + (1 << 2))) >> 3;
1090 if (unlikely(nvme_check_cqid(n
, qid
))) {
1091 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_cq
,
1092 "completion queue doorbell write"
1093 " for nonexistent queue,"
1094 " sqid=%"PRIu32
", ignoring", qid
);
1099 if (unlikely(new_head
>= cq
->size
)) {
1100 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_cqhead
,
1101 "completion queue doorbell write value"
1102 " beyond queue size, sqid=%"PRIu32
","
1103 " new_head=%"PRIu16
", ignoring",
1108 start_sqs
= nvme_cq_full(cq
) ? 1 : 0;
1109 cq
->head
= new_head
;
1112 QTAILQ_FOREACH(sq
, &cq
->sq_list
, entry
) {
1113 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
1115 timer_mod(cq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
1118 if (cq
->tail
== cq
->head
) {
1119 nvme_irq_deassert(n
, cq
);
1122 /* Submission queue doorbell write */
1124 uint16_t new_tail
= val
& 0xffff;
1127 qid
= (addr
- 0x1000) >> 3;
1128 if (unlikely(nvme_check_sqid(n
, qid
))) {
1129 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_sq
,
1130 "submission queue doorbell write"
1131 " for nonexistent queue,"
1132 " sqid=%"PRIu32
", ignoring", qid
);
1137 if (unlikely(new_tail
>= sq
->size
)) {
1138 NVME_GUEST_ERR(nvme_ub_db_wr_invalid_sqtail
,
1139 "submission queue doorbell write value"
1140 " beyond queue size, sqid=%"PRIu32
","
1141 " new_tail=%"PRIu16
", ignoring",
1146 sq
->tail
= new_tail
;
1147 timer_mod(sq
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 500);
1151 static void nvme_mmio_write(void *opaque
, hwaddr addr
, uint64_t data
,
1154 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1155 if (addr
< sizeof(n
->bar
)) {
1156 nvme_write_bar(n
, addr
, data
, size
);
1157 } else if (addr
>= 0x1000) {
1158 nvme_process_db(n
, addr
, data
);
1162 static const MemoryRegionOps nvme_mmio_ops
= {
1163 .read
= nvme_mmio_read
,
1164 .write
= nvme_mmio_write
,
1165 .endianness
= DEVICE_LITTLE_ENDIAN
,
1167 .min_access_size
= 2,
1168 .max_access_size
= 8,
1172 static void nvme_cmb_write(void *opaque
, hwaddr addr
, uint64_t data
,
1175 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1176 memcpy(&n
->cmbuf
[addr
], &data
, size
);
1179 static uint64_t nvme_cmb_read(void *opaque
, hwaddr addr
, unsigned size
)
1182 NvmeCtrl
*n
= (NvmeCtrl
*)opaque
;
1184 memcpy(&val
, &n
->cmbuf
[addr
], size
);
1188 static const MemoryRegionOps nvme_cmb_ops
= {
1189 .read
= nvme_cmb_read
,
1190 .write
= nvme_cmb_write
,
1191 .endianness
= DEVICE_LITTLE_ENDIAN
,
1193 .min_access_size
= 2,
1194 .max_access_size
= 8,
1198 static void nvme_realize(PCIDevice
*pci_dev
, Error
**errp
)
1200 NvmeCtrl
*n
= NVME(pci_dev
);
1201 NvmeIdCtrl
*id
= &n
->id_ctrl
;
1208 error_setg(errp
, "drive property not set");
1212 bs_size
= blk_getlength(n
->conf
.blk
);
1214 error_setg(errp
, "could not get backing file size");
1218 blkconf_serial(&n
->conf
, &n
->serial
);
1220 error_setg(errp
, "serial property not set");
1223 blkconf_blocksizes(&n
->conf
);
1224 if (!blkconf_apply_backend_options(&n
->conf
, blk_is_read_only(n
->conf
.blk
),
1229 pci_conf
= pci_dev
->config
;
1230 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
1231 pci_config_set_prog_interface(pci_dev
->config
, 0x2);
1232 pci_config_set_class(pci_dev
->config
, PCI_CLASS_STORAGE_EXPRESS
);
1233 pcie_endpoint_cap_init(&n
->parent_obj
, 0x80);
1235 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_END_OF_LIST(),
1348 static const VMStateDescription nvme_vmstate
= {
1353 static void nvme_class_init(ObjectClass
*oc
, void *data
)
1355 DeviceClass
*dc
= DEVICE_CLASS(oc
);
1356 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(oc
);
1358 pc
->realize
= nvme_realize
;
1359 pc
->exit
= nvme_exit
;
1360 pc
->class_id
= PCI_CLASS_STORAGE_EXPRESS
;
1361 pc
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1362 pc
->device_id
= 0x5845;
1365 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1366 dc
->desc
= "Non-Volatile Memory Express";
1367 dc
->props
= nvme_props
;
1368 dc
->vmsd
= &nvme_vmstate
;
1371 static void nvme_instance_init(Object
*obj
)
1373 NvmeCtrl
*s
= NVME(obj
);
1375 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1376 "bootindex", "/namespace@1,0",
1377 DEVICE(obj
), &error_abort
);
1380 static const TypeInfo nvme_info
= {
1382 .parent
= TYPE_PCI_DEVICE
,
1383 .instance_size
= sizeof(NvmeCtrl
),
1384 .class_init
= nvme_class_init
,
1385 .instance_init
= nvme_instance_init
,
1386 .interfaces
= (InterfaceInfo
[]) {
1387 { INTERFACE_PCIE_DEVICE
},
1392 static void nvme_register_types(void)
1394 type_register_static(&nvme_info
);
1397 type_init(nvme_register_types
)