2 * QEMU NVM Express Virtual Namespace
4 * Copyright (c) 2019 CNEX Labs
5 * Copyright (c) 2020 Samsung Electronics
8 * Klaus Jensen <k.jensen@samsung.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See the
11 * COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qemu/units.h"
17 #include "qemu/error-report.h"
18 #include "qapi/error.h"
19 #include "sysemu/sysemu.h"
20 #include "sysemu/block-backend.h"
25 #define MIN_DISCARD_GRANULARITY (4 * KiB)
26 #define NVME_DEFAULT_ZONE_SIZE (128 * MiB)
28 void nvme_ns_init_format(NvmeNamespace
*ns
)
30 NvmeIdNs
*id_ns
= &ns
->id_ns
;
34 ns
->lbaf
= id_ns
->lbaf
[NVME_ID_NS_FLBAS_INDEX(id_ns
->flbas
)];
35 ns
->lbasz
= 1 << ns
->lbaf
.ds
;
37 nlbas
= ns
->size
/ (ns
->lbasz
+ ns
->lbaf
.ms
);
39 id_ns
->nsze
= cpu_to_le64(nlbas
);
41 /* no thin provisioning */
42 id_ns
->ncap
= id_ns
->nsze
;
43 id_ns
->nuse
= id_ns
->ncap
;
45 ns
->moff
= (int64_t)nlbas
<< ns
->lbaf
.ds
;
47 npdg
= ns
->blkconf
.discard_granularity
/ ns
->lbasz
;
49 ret
= bdrv_get_info(blk_bs(ns
->blkconf
.blk
), &bdi
);
50 if (ret
>= 0 && bdi
.cluster_size
> ns
->blkconf
.discard_granularity
) {
51 npdg
= bdi
.cluster_size
/ ns
->lbasz
;
54 id_ns
->npda
= id_ns
->npdg
= npdg
- 1;
57 static int nvme_ns_init(NvmeNamespace
*ns
, Error
**errp
)
59 static uint64_t ns_count
;
60 NvmeIdNs
*id_ns
= &ns
->id_ns
;
61 NvmeIdNsNvm
*id_ns_nvm
= &ns
->id_ns_nvm
;
66 ns
->csi
= NVME_CSI_NVM
;
69 ns
->id_ns
.dlfeat
= 0x1;
71 /* support DULBE and I/O optimization fields */
72 id_ns
->nsfeat
|= (0x4 | 0x10);
74 if (ns
->params
.shared
) {
75 id_ns
->nmic
|= NVME_NMIC_NS_SHARED
;
78 /* Substitute a missing EUI-64 by an autogenerated one */
80 if (!ns
->params
.eui64
&& ns
->params
.eui64_default
) {
81 ns
->params
.eui64
= ns_count
+ NVME_EUI64_DEFAULT
;
85 id_ns
->mssrl
= cpu_to_le16(ns
->params
.mssrl
);
86 id_ns
->mcl
= cpu_to_le32(ns
->params
.mcl
);
87 id_ns
->msrc
= ns
->params
.msrc
;
88 id_ns
->eui64
= cpu_to_be64(ns
->params
.eui64
);
90 ds
= 31 - clz32(ns
->blkconf
.logical_block_size
);
93 id_ns
->mc
= NVME_ID_NS_MC_EXTENDED
| NVME_ID_NS_MC_SEPARATE
;
95 if (ms
&& ns
->params
.mset
) {
96 id_ns
->flbas
|= NVME_ID_NS_FLBAS_EXTENDED
;
100 id_ns
->dps
= ns
->params
.pi
;
101 if (ns
->params
.pi
&& ns
->params
.pil
) {
102 id_ns
->dps
|= NVME_ID_NS_DPS_FIRST_EIGHT
;
105 ns
->pif
= ns
->params
.pif
;
107 static const NvmeLBAF lbaf
[16] = {
109 [1] = { .ds
= 9, .ms
= 8 },
110 [2] = { .ds
= 9, .ms
= 16 },
111 [3] = { .ds
= 9, .ms
= 64 },
113 [5] = { .ds
= 12, .ms
= 8 },
114 [6] = { .ds
= 12, .ms
= 16 },
115 [7] = { .ds
= 12, .ms
= 64 },
120 memcpy(&id_ns
->lbaf
, &lbaf
, sizeof(lbaf
));
122 for (i
= 0; i
< ns
->nlbaf
; i
++) {
123 NvmeLBAF
*lbaf
= &id_ns
->lbaf
[i
];
124 if (lbaf
->ds
== ds
) {
125 if (lbaf
->ms
== ms
) {
132 /* add non-standard lba format */
133 id_ns
->lbaf
[ns
->nlbaf
].ds
= ds
;
134 id_ns
->lbaf
[ns
->nlbaf
].ms
= ms
;
141 id_ns_nvm
->elbaf
[i
] = (ns
->pif
& 0x3) << 7;
142 id_ns
->nlbaf
= ns
->nlbaf
- 1;
143 nvme_ns_init_format(ns
);
148 static int nvme_ns_init_blk(NvmeNamespace
*ns
, Error
**errp
)
152 if (!blkconf_blocksizes(&ns
->blkconf
, errp
)) {
156 read_only
= !blk_supports_write_perm(ns
->blkconf
.blk
);
157 if (!blkconf_apply_backend_options(&ns
->blkconf
, read_only
, false, errp
)) {
161 if (ns
->blkconf
.discard_granularity
== -1) {
162 ns
->blkconf
.discard_granularity
=
163 MAX(ns
->blkconf
.logical_block_size
, MIN_DISCARD_GRANULARITY
);
166 ns
->size
= blk_getlength(ns
->blkconf
.blk
);
168 error_setg_errno(errp
, -ns
->size
, "could not get blockdev size");
175 static int nvme_ns_zoned_check_calc_geometry(NvmeNamespace
*ns
, Error
**errp
)
177 uint64_t zone_size
, zone_cap
;
179 /* Make sure that the values of ZNS properties are sane */
180 if (ns
->params
.zone_size_bs
) {
181 zone_size
= ns
->params
.zone_size_bs
;
183 zone_size
= NVME_DEFAULT_ZONE_SIZE
;
185 if (ns
->params
.zone_cap_bs
) {
186 zone_cap
= ns
->params
.zone_cap_bs
;
188 zone_cap
= zone_size
;
190 if (zone_cap
> zone_size
) {
191 error_setg(errp
, "zone capacity %"PRIu64
"B exceeds "
192 "zone size %"PRIu64
"B", zone_cap
, zone_size
);
195 if (zone_size
< ns
->lbasz
) {
196 error_setg(errp
, "zone size %"PRIu64
"B too small, "
197 "must be at least %zuB", zone_size
, ns
->lbasz
);
200 if (zone_cap
< ns
->lbasz
) {
201 error_setg(errp
, "zone capacity %"PRIu64
"B too small, "
202 "must be at least %zuB", zone_cap
, ns
->lbasz
);
207 * Save the main zone geometry values to avoid
208 * calculating them later again.
210 ns
->zone_size
= zone_size
/ ns
->lbasz
;
211 ns
->zone_capacity
= zone_cap
/ ns
->lbasz
;
212 ns
->num_zones
= le64_to_cpu(ns
->id_ns
.nsze
) / ns
->zone_size
;
214 /* Do a few more sanity checks of ZNS properties */
215 if (!ns
->num_zones
) {
217 "insufficient drive capacity, must be at least the size "
218 "of one zone (%"PRIu64
"B)", zone_size
);
225 static void nvme_ns_zoned_init_state(NvmeNamespace
*ns
)
227 uint64_t start
= 0, zone_size
= ns
->zone_size
;
228 uint64_t capacity
= ns
->num_zones
* zone_size
;
232 ns
->zone_array
= g_new0(NvmeZone
, ns
->num_zones
);
233 if (ns
->params
.zd_extension_size
) {
234 ns
->zd_extensions
= g_malloc0(ns
->params
.zd_extension_size
*
238 QTAILQ_INIT(&ns
->exp_open_zones
);
239 QTAILQ_INIT(&ns
->imp_open_zones
);
240 QTAILQ_INIT(&ns
->closed_zones
);
241 QTAILQ_INIT(&ns
->full_zones
);
243 zone
= ns
->zone_array
;
244 for (i
= 0; i
< ns
->num_zones
; i
++, zone
++) {
245 if (start
+ zone_size
> capacity
) {
246 zone_size
= capacity
- start
;
248 zone
->d
.zt
= NVME_ZONE_TYPE_SEQ_WRITE
;
249 nvme_set_zone_state(zone
, NVME_ZONE_STATE_EMPTY
);
251 zone
->d
.zcap
= ns
->zone_capacity
;
252 zone
->d
.zslba
= start
;
258 ns
->zone_size_log2
= 0;
259 if (is_power_of_2(ns
->zone_size
)) {
260 ns
->zone_size_log2
= 63 - clz64(ns
->zone_size
);
264 static void nvme_ns_init_zoned(NvmeNamespace
*ns
)
266 NvmeIdNsZoned
*id_ns_z
;
269 nvme_ns_zoned_init_state(ns
);
271 id_ns_z
= g_malloc0(sizeof(NvmeIdNsZoned
));
273 /* MAR/MOR are zeroes-based, FFFFFFFFFh means no limit */
274 id_ns_z
->mar
= cpu_to_le32(ns
->params
.max_active_zones
- 1);
275 id_ns_z
->mor
= cpu_to_le32(ns
->params
.max_open_zones
- 1);
277 id_ns_z
->ozcs
= ns
->params
.cross_zone_read
?
278 NVME_ID_NS_ZONED_OZCS_RAZB
: 0x00;
280 for (i
= 0; i
<= ns
->id_ns
.nlbaf
; i
++) {
281 id_ns_z
->lbafe
[i
].zsze
= cpu_to_le64(ns
->zone_size
);
282 id_ns_z
->lbafe
[i
].zdes
=
283 ns
->params
.zd_extension_size
>> 6; /* Units of 64B */
286 if (ns
->params
.zrwas
) {
287 ns
->zns
.numzrwa
= ns
->params
.numzrwa
?
288 ns
->params
.numzrwa
: ns
->num_zones
;
290 ns
->zns
.zrwas
= ns
->params
.zrwas
>> ns
->lbaf
.ds
;
291 ns
->zns
.zrwafg
= ns
->params
.zrwafg
>> ns
->lbaf
.ds
;
293 id_ns_z
->ozcs
|= NVME_ID_NS_ZONED_OZCS_ZRWASUP
;
294 id_ns_z
->zrwacap
= NVME_ID_NS_ZONED_ZRWACAP_EXPFLUSHSUP
;
296 id_ns_z
->numzrwa
= cpu_to_le32(ns
->params
.numzrwa
);
297 id_ns_z
->zrwas
= cpu_to_le16(ns
->zns
.zrwas
);
298 id_ns_z
->zrwafg
= cpu_to_le16(ns
->zns
.zrwafg
);
301 id_ns_z
->ozcs
= cpu_to_le16(id_ns_z
->ozcs
);
303 ns
->csi
= NVME_CSI_ZONED
;
304 ns
->id_ns
.nsze
= cpu_to_le64(ns
->num_zones
* ns
->zone_size
);
305 ns
->id_ns
.ncap
= ns
->id_ns
.nsze
;
306 ns
->id_ns
.nuse
= ns
->id_ns
.ncap
;
309 * The device uses the BDRV_BLOCK_ZERO flag to determine the "deallocated"
310 * status of logical blocks. Since the spec defines that logical blocks
311 * SHALL be deallocated when then zone is in the Empty or Offline states,
312 * we can only support DULBE if the zone size is a multiple of the
315 if (ns
->zone_size
% (ns
->id_ns
.npdg
+ 1)) {
316 warn_report("the zone size (%"PRIu64
" blocks) is not a multiple of "
317 "the calculated deallocation granularity (%d blocks); "
318 "DULBE support disabled",
319 ns
->zone_size
, ns
->id_ns
.npdg
+ 1);
321 ns
->id_ns
.nsfeat
&= ~0x4;
324 ns
->id_ns_zoned
= id_ns_z
;
327 static void nvme_clear_zone(NvmeNamespace
*ns
, NvmeZone
*zone
)
331 zone
->w_ptr
= zone
->d
.wp
;
332 state
= nvme_get_zone_state(zone
);
333 if (zone
->d
.wp
!= zone
->d
.zslba
||
334 (zone
->d
.za
& NVME_ZA_ZD_EXT_VALID
)) {
335 if (state
!= NVME_ZONE_STATE_CLOSED
) {
336 trace_pci_nvme_clear_ns_close(state
, zone
->d
.zslba
);
337 nvme_set_zone_state(zone
, NVME_ZONE_STATE_CLOSED
);
339 nvme_aor_inc_active(ns
);
340 QTAILQ_INSERT_HEAD(&ns
->closed_zones
, zone
, entry
);
342 trace_pci_nvme_clear_ns_reset(state
, zone
->d
.zslba
);
343 if (zone
->d
.za
& NVME_ZA_ZRWA_VALID
) {
344 zone
->d
.za
&= ~NVME_ZA_ZRWA_VALID
;
347 nvme_set_zone_state(zone
, NVME_ZONE_STATE_EMPTY
);
352 * Close all the zones that are currently open.
354 static void nvme_zoned_ns_shutdown(NvmeNamespace
*ns
)
356 NvmeZone
*zone
, *next
;
358 QTAILQ_FOREACH_SAFE(zone
, &ns
->closed_zones
, entry
, next
) {
359 QTAILQ_REMOVE(&ns
->closed_zones
, zone
, entry
);
360 nvme_aor_dec_active(ns
);
361 nvme_clear_zone(ns
, zone
);
363 QTAILQ_FOREACH_SAFE(zone
, &ns
->imp_open_zones
, entry
, next
) {
364 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
365 nvme_aor_dec_open(ns
);
366 nvme_aor_dec_active(ns
);
367 nvme_clear_zone(ns
, zone
);
369 QTAILQ_FOREACH_SAFE(zone
, &ns
->exp_open_zones
, entry
, next
) {
370 QTAILQ_REMOVE(&ns
->exp_open_zones
, zone
, entry
);
371 nvme_aor_dec_open(ns
);
372 nvme_aor_dec_active(ns
);
373 nvme_clear_zone(ns
, zone
);
376 assert(ns
->nr_open_zones
== 0);
379 static int nvme_ns_check_constraints(NvmeNamespace
*ns
, Error
**errp
)
381 unsigned int pi_size
;
383 if (!ns
->blkconf
.blk
) {
384 error_setg(errp
, "block backend not configured");
389 if (ns
->params
.pi
> NVME_ID_NS_DPS_TYPE_3
) {
390 error_setg(errp
, "invalid 'pi' value");
394 switch (ns
->params
.pif
) {
395 case NVME_PI_GUARD_16
:
398 case NVME_PI_GUARD_64
:
402 error_setg(errp
, "invalid 'pif'");
406 if (ns
->params
.ms
< pi_size
) {
407 error_setg(errp
, "at least %u bytes of metadata required to "
408 "enable protection information", pi_size
);
413 if (ns
->params
.nsid
> NVME_MAX_NAMESPACES
) {
414 error_setg(errp
, "invalid namespace id (must be between 0 and %d)",
415 NVME_MAX_NAMESPACES
);
419 if (ns
->params
.zoned
) {
420 if (ns
->params
.max_active_zones
) {
421 if (ns
->params
.max_open_zones
> ns
->params
.max_active_zones
) {
422 error_setg(errp
, "max_open_zones (%u) exceeds "
423 "max_active_zones (%u)", ns
->params
.max_open_zones
,
424 ns
->params
.max_active_zones
);
428 if (!ns
->params
.max_open_zones
) {
429 ns
->params
.max_open_zones
= ns
->params
.max_active_zones
;
433 if (ns
->params
.zd_extension_size
) {
434 if (ns
->params
.zd_extension_size
& 0x3f) {
435 error_setg(errp
, "zone descriptor extension size must be a "
439 if ((ns
->params
.zd_extension_size
>> 6) > 0xff) {
441 "zone descriptor extension size is too large");
446 if (ns
->params
.zrwas
) {
447 if (ns
->params
.zrwas
% ns
->blkconf
.logical_block_size
) {
448 error_setg(errp
, "zone random write area size (zoned.zrwas "
449 "%"PRIu64
") must be a multiple of the logical "
450 "block size (logical_block_size %"PRIu32
")",
451 ns
->params
.zrwas
, ns
->blkconf
.logical_block_size
);
455 if (ns
->params
.zrwafg
== -1) {
456 ns
->params
.zrwafg
= ns
->blkconf
.logical_block_size
;
459 if (ns
->params
.zrwas
% ns
->params
.zrwafg
) {
460 error_setg(errp
, "zone random write area size (zoned.zrwas "
461 "%"PRIu64
") must be a multiple of the zone random "
462 "write area flush granularity (zoned.zrwafg, "
463 "%"PRIu64
")", ns
->params
.zrwas
, ns
->params
.zrwafg
);
467 if (ns
->params
.max_active_zones
) {
468 if (ns
->params
.numzrwa
> ns
->params
.max_active_zones
) {
469 error_setg(errp
, "number of zone random write area "
470 "resources (zoned.numzrwa, %d) must be less "
471 "than or equal to maximum active resources "
472 "(zoned.max_active_zones, %d)",
474 ns
->params
.max_active_zones
);
484 int nvme_ns_setup(NvmeNamespace
*ns
, Error
**errp
)
486 if (nvme_ns_check_constraints(ns
, errp
)) {
490 if (nvme_ns_init_blk(ns
, errp
)) {
494 if (nvme_ns_init(ns
, errp
)) {
497 if (ns
->params
.zoned
) {
498 if (nvme_ns_zoned_check_calc_geometry(ns
, errp
) != 0) {
501 nvme_ns_init_zoned(ns
);
507 void nvme_ns_drain(NvmeNamespace
*ns
)
509 blk_drain(ns
->blkconf
.blk
);
512 void nvme_ns_shutdown(NvmeNamespace
*ns
)
514 blk_flush(ns
->blkconf
.blk
);
515 if (ns
->params
.zoned
) {
516 nvme_zoned_ns_shutdown(ns
);
520 void nvme_ns_cleanup(NvmeNamespace
*ns
)
522 if (ns
->params
.zoned
) {
523 g_free(ns
->id_ns_zoned
);
524 g_free(ns
->zone_array
);
525 g_free(ns
->zd_extensions
);
529 static void nvme_ns_unrealize(DeviceState
*dev
)
531 NvmeNamespace
*ns
= NVME_NS(dev
);
534 nvme_ns_shutdown(ns
);
538 static void nvme_ns_realize(DeviceState
*dev
, Error
**errp
)
540 NvmeNamespace
*ns
= NVME_NS(dev
);
541 BusState
*s
= qdev_get_parent_bus(dev
);
542 NvmeCtrl
*n
= NVME(s
->parent
);
543 NvmeSubsystem
*subsys
= n
->subsys
;
544 uint32_t nsid
= ns
->params
.nsid
;
548 if (ns
->params
.detached
) {
549 error_setg(errp
, "detached requires that the nvme device is "
550 "linked to an nvme-subsys device");
555 * If this namespace belongs to a subsystem (through a link on the
556 * controller device), reparent the device.
558 if (!qdev_set_parent_bus(dev
, &subsys
->bus
.parent_bus
, errp
)) {
563 if (nvme_ns_setup(ns
, errp
)) {
568 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
569 if (nvme_ns(n
, i
) || nvme_subsys_ns(subsys
, i
)) {
573 nsid
= ns
->params
.nsid
= i
;
578 error_setg(errp
, "no free namespace id");
582 if (nvme_ns(n
, nsid
) || nvme_subsys_ns(subsys
, nsid
)) {
583 error_setg(errp
, "namespace id '%d' already allocated", nsid
);
589 subsys
->namespaces
[nsid
] = ns
;
591 if (ns
->params
.detached
) {
595 if (ns
->params
.shared
) {
596 for (i
= 0; i
< ARRAY_SIZE(subsys
->ctrls
); i
++) {
597 NvmeCtrl
*ctrl
= subsys
->ctrls
[i
];
600 nvme_attach_ns(ctrl
, ns
);
608 nvme_attach_ns(n
, ns
);
611 static Property nvme_ns_props
[] = {
612 DEFINE_BLOCK_PROPERTIES(NvmeNamespace
, blkconf
),
613 DEFINE_PROP_BOOL("detached", NvmeNamespace
, params
.detached
, false),
614 DEFINE_PROP_BOOL("shared", NvmeNamespace
, params
.shared
, true),
615 DEFINE_PROP_UINT32("nsid", NvmeNamespace
, params
.nsid
, 0),
616 DEFINE_PROP_UUID("uuid", NvmeNamespace
, params
.uuid
),
617 DEFINE_PROP_UINT64("eui64", NvmeNamespace
, params
.eui64
, 0),
618 DEFINE_PROP_UINT16("ms", NvmeNamespace
, params
.ms
, 0),
619 DEFINE_PROP_UINT8("mset", NvmeNamespace
, params
.mset
, 0),
620 DEFINE_PROP_UINT8("pi", NvmeNamespace
, params
.pi
, 0),
621 DEFINE_PROP_UINT8("pil", NvmeNamespace
, params
.pil
, 0),
622 DEFINE_PROP_UINT8("pif", NvmeNamespace
, params
.pif
, 0),
623 DEFINE_PROP_UINT16("mssrl", NvmeNamespace
, params
.mssrl
, 128),
624 DEFINE_PROP_UINT32("mcl", NvmeNamespace
, params
.mcl
, 128),
625 DEFINE_PROP_UINT8("msrc", NvmeNamespace
, params
.msrc
, 127),
626 DEFINE_PROP_BOOL("zoned", NvmeNamespace
, params
.zoned
, false),
627 DEFINE_PROP_SIZE("zoned.zone_size", NvmeNamespace
, params
.zone_size_bs
,
628 NVME_DEFAULT_ZONE_SIZE
),
629 DEFINE_PROP_SIZE("zoned.zone_capacity", NvmeNamespace
, params
.zone_cap_bs
,
631 DEFINE_PROP_BOOL("zoned.cross_read", NvmeNamespace
,
632 params
.cross_zone_read
, false),
633 DEFINE_PROP_UINT32("zoned.max_active", NvmeNamespace
,
634 params
.max_active_zones
, 0),
635 DEFINE_PROP_UINT32("zoned.max_open", NvmeNamespace
,
636 params
.max_open_zones
, 0),
637 DEFINE_PROP_UINT32("zoned.descr_ext_size", NvmeNamespace
,
638 params
.zd_extension_size
, 0),
639 DEFINE_PROP_UINT32("zoned.numzrwa", NvmeNamespace
, params
.numzrwa
, 0),
640 DEFINE_PROP_SIZE("zoned.zrwas", NvmeNamespace
, params
.zrwas
, 0),
641 DEFINE_PROP_SIZE("zoned.zrwafg", NvmeNamespace
, params
.zrwafg
, -1),
642 DEFINE_PROP_BOOL("eui64-default", NvmeNamespace
, params
.eui64_default
,
644 DEFINE_PROP_END_OF_LIST(),
647 static void nvme_ns_class_init(ObjectClass
*oc
, void *data
)
649 DeviceClass
*dc
= DEVICE_CLASS(oc
);
651 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
653 dc
->bus_type
= TYPE_NVME_BUS
;
654 dc
->realize
= nvme_ns_realize
;
655 dc
->unrealize
= nvme_ns_unrealize
;
656 device_class_set_props(dc
, nvme_ns_props
);
657 dc
->desc
= "Virtual NVMe namespace";
660 static void nvme_ns_instance_init(Object
*obj
)
662 NvmeNamespace
*ns
= NVME_NS(obj
);
663 char *bootindex
= g_strdup_printf("/namespace@%d,0", ns
->params
.nsid
);
665 device_add_bootindex_property(obj
, &ns
->bootindex
, "bootindex",
666 bootindex
, DEVICE(obj
));
671 static const TypeInfo nvme_ns_info
= {
672 .name
= TYPE_NVME_NS
,
673 .parent
= TYPE_DEVICE
,
674 .class_init
= nvme_ns_class_init
,
675 .instance_size
= sizeof(NvmeNamespace
),
676 .instance_init
= nvme_ns_instance_init
,
679 static void nvme_ns_register_types(void)
681 type_register_static(&nvme_ns_info
);
684 type_init(nvme_ns_register_types
)