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
;
65 ns
->csi
= NVME_CSI_NVM
;
68 ns
->id_ns
.dlfeat
= 0x1;
70 /* support DULBE and I/O optimization fields */
71 id_ns
->nsfeat
|= (0x4 | 0x10);
73 if (ns
->params
.shared
) {
74 id_ns
->nmic
|= NVME_NMIC_NS_SHARED
;
77 /* Substitute a missing EUI-64 by an autogenerated one */
79 if (!ns
->params
.eui64
&& ns
->params
.eui64_default
) {
80 ns
->params
.eui64
= ns_count
+ NVME_EUI64_DEFAULT
;
84 id_ns
->mssrl
= cpu_to_le16(ns
->params
.mssrl
);
85 id_ns
->mcl
= cpu_to_le32(ns
->params
.mcl
);
86 id_ns
->msrc
= ns
->params
.msrc
;
87 id_ns
->eui64
= cpu_to_be64(ns
->params
.eui64
);
89 ds
= 31 - clz32(ns
->blkconf
.logical_block_size
);
92 id_ns
->mc
= NVME_ID_NS_MC_EXTENDED
| NVME_ID_NS_MC_SEPARATE
;
94 if (ms
&& ns
->params
.mset
) {
95 id_ns
->flbas
|= NVME_ID_NS_FLBAS_EXTENDED
;
99 id_ns
->dps
= ns
->params
.pi
;
100 if (ns
->params
.pi
&& ns
->params
.pil
) {
101 id_ns
->dps
|= NVME_ID_NS_DPS_FIRST_EIGHT
;
104 static const NvmeLBAF lbaf
[16] = {
106 [1] = { .ds
= 9, .ms
= 8 },
107 [2] = { .ds
= 9, .ms
= 16 },
108 [3] = { .ds
= 9, .ms
= 64 },
110 [5] = { .ds
= 12, .ms
= 8 },
111 [6] = { .ds
= 12, .ms
= 16 },
112 [7] = { .ds
= 12, .ms
= 64 },
115 memcpy(&id_ns
->lbaf
, &lbaf
, sizeof(lbaf
));
118 for (i
= 0; i
<= id_ns
->nlbaf
; i
++) {
119 NvmeLBAF
*lbaf
= &id_ns
->lbaf
[i
];
120 if (lbaf
->ds
== ds
) {
121 if (lbaf
->ms
== ms
) {
128 /* add non-standard lba format */
130 id_ns
->lbaf
[id_ns
->nlbaf
].ds
= ds
;
131 id_ns
->lbaf
[id_ns
->nlbaf
].ms
= ms
;
132 id_ns
->flbas
|= id_ns
->nlbaf
;
135 nvme_ns_init_format(ns
);
140 static int nvme_ns_init_blk(NvmeNamespace
*ns
, Error
**errp
)
144 if (!blkconf_blocksizes(&ns
->blkconf
, errp
)) {
148 read_only
= !blk_supports_write_perm(ns
->blkconf
.blk
);
149 if (!blkconf_apply_backend_options(&ns
->blkconf
, read_only
, false, errp
)) {
153 if (ns
->blkconf
.discard_granularity
== -1) {
154 ns
->blkconf
.discard_granularity
=
155 MAX(ns
->blkconf
.logical_block_size
, MIN_DISCARD_GRANULARITY
);
158 ns
->size
= blk_getlength(ns
->blkconf
.blk
);
160 error_setg_errno(errp
, -ns
->size
, "could not get blockdev size");
167 static int nvme_ns_zoned_check_calc_geometry(NvmeNamespace
*ns
, Error
**errp
)
169 uint64_t zone_size
, zone_cap
;
171 /* Make sure that the values of ZNS properties are sane */
172 if (ns
->params
.zone_size_bs
) {
173 zone_size
= ns
->params
.zone_size_bs
;
175 zone_size
= NVME_DEFAULT_ZONE_SIZE
;
177 if (ns
->params
.zone_cap_bs
) {
178 zone_cap
= ns
->params
.zone_cap_bs
;
180 zone_cap
= zone_size
;
182 if (zone_cap
> zone_size
) {
183 error_setg(errp
, "zone capacity %"PRIu64
"B exceeds "
184 "zone size %"PRIu64
"B", zone_cap
, zone_size
);
187 if (zone_size
< ns
->lbasz
) {
188 error_setg(errp
, "zone size %"PRIu64
"B too small, "
189 "must be at least %zuB", zone_size
, ns
->lbasz
);
192 if (zone_cap
< ns
->lbasz
) {
193 error_setg(errp
, "zone capacity %"PRIu64
"B too small, "
194 "must be at least %zuB", zone_cap
, ns
->lbasz
);
199 * Save the main zone geometry values to avoid
200 * calculating them later again.
202 ns
->zone_size
= zone_size
/ ns
->lbasz
;
203 ns
->zone_capacity
= zone_cap
/ ns
->lbasz
;
204 ns
->num_zones
= le64_to_cpu(ns
->id_ns
.nsze
) / ns
->zone_size
;
206 /* Do a few more sanity checks of ZNS properties */
207 if (!ns
->num_zones
) {
209 "insufficient drive capacity, must be at least the size "
210 "of one zone (%"PRIu64
"B)", zone_size
);
217 static void nvme_ns_zoned_init_state(NvmeNamespace
*ns
)
219 uint64_t start
= 0, zone_size
= ns
->zone_size
;
220 uint64_t capacity
= ns
->num_zones
* zone_size
;
224 ns
->zone_array
= g_new0(NvmeZone
, ns
->num_zones
);
225 if (ns
->params
.zd_extension_size
) {
226 ns
->zd_extensions
= g_malloc0(ns
->params
.zd_extension_size
*
230 QTAILQ_INIT(&ns
->exp_open_zones
);
231 QTAILQ_INIT(&ns
->imp_open_zones
);
232 QTAILQ_INIT(&ns
->closed_zones
);
233 QTAILQ_INIT(&ns
->full_zones
);
235 zone
= ns
->zone_array
;
236 for (i
= 0; i
< ns
->num_zones
; i
++, zone
++) {
237 if (start
+ zone_size
> capacity
) {
238 zone_size
= capacity
- start
;
240 zone
->d
.zt
= NVME_ZONE_TYPE_SEQ_WRITE
;
241 nvme_set_zone_state(zone
, NVME_ZONE_STATE_EMPTY
);
243 zone
->d
.zcap
= ns
->zone_capacity
;
244 zone
->d
.zslba
= start
;
250 ns
->zone_size_log2
= 0;
251 if (is_power_of_2(ns
->zone_size
)) {
252 ns
->zone_size_log2
= 63 - clz64(ns
->zone_size
);
256 static void nvme_ns_init_zoned(NvmeNamespace
*ns
)
258 NvmeIdNsZoned
*id_ns_z
;
261 nvme_ns_zoned_init_state(ns
);
263 id_ns_z
= g_malloc0(sizeof(NvmeIdNsZoned
));
265 /* MAR/MOR are zeroes-based, FFFFFFFFFh means no limit */
266 id_ns_z
->mar
= cpu_to_le32(ns
->params
.max_active_zones
- 1);
267 id_ns_z
->mor
= cpu_to_le32(ns
->params
.max_open_zones
- 1);
269 id_ns_z
->ozcs
= ns
->params
.cross_zone_read
? 0x01 : 0x00;
271 for (i
= 0; i
<= ns
->id_ns
.nlbaf
; i
++) {
272 id_ns_z
->lbafe
[i
].zsze
= cpu_to_le64(ns
->zone_size
);
273 id_ns_z
->lbafe
[i
].zdes
=
274 ns
->params
.zd_extension_size
>> 6; /* Units of 64B */
277 ns
->csi
= NVME_CSI_ZONED
;
278 ns
->id_ns
.nsze
= cpu_to_le64(ns
->num_zones
* ns
->zone_size
);
279 ns
->id_ns
.ncap
= ns
->id_ns
.nsze
;
280 ns
->id_ns
.nuse
= ns
->id_ns
.ncap
;
283 * The device uses the BDRV_BLOCK_ZERO flag to determine the "deallocated"
284 * status of logical blocks. Since the spec defines that logical blocks
285 * SHALL be deallocated when then zone is in the Empty or Offline states,
286 * we can only support DULBE if the zone size is a multiple of the
289 if (ns
->zone_size
% (ns
->id_ns
.npdg
+ 1)) {
290 warn_report("the zone size (%"PRIu64
" blocks) is not a multiple of "
291 "the calculated deallocation granularity (%d blocks); "
292 "DULBE support disabled",
293 ns
->zone_size
, ns
->id_ns
.npdg
+ 1);
295 ns
->id_ns
.nsfeat
&= ~0x4;
298 ns
->id_ns_zoned
= id_ns_z
;
301 static void nvme_clear_zone(NvmeNamespace
*ns
, NvmeZone
*zone
)
305 zone
->w_ptr
= zone
->d
.wp
;
306 state
= nvme_get_zone_state(zone
);
307 if (zone
->d
.wp
!= zone
->d
.zslba
||
308 (zone
->d
.za
& NVME_ZA_ZD_EXT_VALID
)) {
309 if (state
!= NVME_ZONE_STATE_CLOSED
) {
310 trace_pci_nvme_clear_ns_close(state
, zone
->d
.zslba
);
311 nvme_set_zone_state(zone
, NVME_ZONE_STATE_CLOSED
);
313 nvme_aor_inc_active(ns
);
314 QTAILQ_INSERT_HEAD(&ns
->closed_zones
, zone
, entry
);
316 trace_pci_nvme_clear_ns_reset(state
, zone
->d
.zslba
);
317 nvme_set_zone_state(zone
, NVME_ZONE_STATE_EMPTY
);
322 * Close all the zones that are currently open.
324 static void nvme_zoned_ns_shutdown(NvmeNamespace
*ns
)
326 NvmeZone
*zone
, *next
;
328 QTAILQ_FOREACH_SAFE(zone
, &ns
->closed_zones
, entry
, next
) {
329 QTAILQ_REMOVE(&ns
->closed_zones
, zone
, entry
);
330 nvme_aor_dec_active(ns
);
331 nvme_clear_zone(ns
, zone
);
333 QTAILQ_FOREACH_SAFE(zone
, &ns
->imp_open_zones
, entry
, next
) {
334 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
335 nvme_aor_dec_open(ns
);
336 nvme_aor_dec_active(ns
);
337 nvme_clear_zone(ns
, zone
);
339 QTAILQ_FOREACH_SAFE(zone
, &ns
->exp_open_zones
, entry
, next
) {
340 QTAILQ_REMOVE(&ns
->exp_open_zones
, zone
, entry
);
341 nvme_aor_dec_open(ns
);
342 nvme_aor_dec_active(ns
);
343 nvme_clear_zone(ns
, zone
);
346 assert(ns
->nr_open_zones
== 0);
349 static int nvme_ns_check_constraints(NvmeNamespace
*ns
, Error
**errp
)
351 if (!ns
->blkconf
.blk
) {
352 error_setg(errp
, "block backend not configured");
356 if (ns
->params
.pi
&& ns
->params
.ms
< 8) {
357 error_setg(errp
, "at least 8 bytes of metadata required to enable "
358 "protection information");
362 if (ns
->params
.nsid
> NVME_MAX_NAMESPACES
) {
363 error_setg(errp
, "invalid namespace id (must be between 0 and %d)",
364 NVME_MAX_NAMESPACES
);
368 if (ns
->params
.zoned
) {
369 if (ns
->params
.max_active_zones
) {
370 if (ns
->params
.max_open_zones
> ns
->params
.max_active_zones
) {
371 error_setg(errp
, "max_open_zones (%u) exceeds "
372 "max_active_zones (%u)", ns
->params
.max_open_zones
,
373 ns
->params
.max_active_zones
);
377 if (!ns
->params
.max_open_zones
) {
378 ns
->params
.max_open_zones
= ns
->params
.max_active_zones
;
382 if (ns
->params
.zd_extension_size
) {
383 if (ns
->params
.zd_extension_size
& 0x3f) {
384 error_setg(errp
, "zone descriptor extension size must be a "
388 if ((ns
->params
.zd_extension_size
>> 6) > 0xff) {
390 "zone descriptor extension size is too large");
399 int nvme_ns_setup(NvmeNamespace
*ns
, Error
**errp
)
401 if (nvme_ns_check_constraints(ns
, errp
)) {
405 if (nvme_ns_init_blk(ns
, errp
)) {
409 if (nvme_ns_init(ns
, errp
)) {
412 if (ns
->params
.zoned
) {
413 if (nvme_ns_zoned_check_calc_geometry(ns
, errp
) != 0) {
416 nvme_ns_init_zoned(ns
);
422 void nvme_ns_drain(NvmeNamespace
*ns
)
424 blk_drain(ns
->blkconf
.blk
);
427 void nvme_ns_shutdown(NvmeNamespace
*ns
)
429 blk_flush(ns
->blkconf
.blk
);
430 if (ns
->params
.zoned
) {
431 nvme_zoned_ns_shutdown(ns
);
435 void nvme_ns_cleanup(NvmeNamespace
*ns
)
437 if (ns
->params
.zoned
) {
438 g_free(ns
->id_ns_zoned
);
439 g_free(ns
->zone_array
);
440 g_free(ns
->zd_extensions
);
444 static void nvme_ns_unrealize(DeviceState
*dev
)
446 NvmeNamespace
*ns
= NVME_NS(dev
);
449 nvme_ns_shutdown(ns
);
453 static void nvme_ns_realize(DeviceState
*dev
, Error
**errp
)
455 NvmeNamespace
*ns
= NVME_NS(dev
);
456 BusState
*s
= qdev_get_parent_bus(dev
);
457 NvmeCtrl
*n
= NVME(s
->parent
);
458 NvmeSubsystem
*subsys
= n
->subsys
;
459 uint32_t nsid
= ns
->params
.nsid
;
463 if (ns
->params
.detached
) {
464 error_setg(errp
, "detached requires that the nvme device is "
465 "linked to an nvme-subsys device");
469 if (ns
->params
.shared
) {
470 error_setg(errp
, "shared requires that the nvme device is "
471 "linked to an nvme-subsys device");
476 * If this namespace belongs to a subsystem (through a link on the
477 * controller device), reparent the device.
479 if (!qdev_set_parent_bus(dev
, &subsys
->bus
.parent_bus
, errp
)) {
484 if (nvme_ns_setup(ns
, errp
)) {
489 for (i
= 1; i
<= NVME_MAX_NAMESPACES
; i
++) {
490 if (nvme_ns(n
, i
) || nvme_subsys_ns(subsys
, i
)) {
494 nsid
= ns
->params
.nsid
= i
;
499 error_setg(errp
, "no free namespace id");
503 if (nvme_ns(n
, nsid
) || nvme_subsys_ns(subsys
, nsid
)) {
504 error_setg(errp
, "namespace id '%d' already allocated", nsid
);
510 subsys
->namespaces
[nsid
] = ns
;
512 if (ns
->params
.detached
) {
516 if (ns
->params
.shared
) {
517 for (i
= 0; i
< ARRAY_SIZE(subsys
->ctrls
); i
++) {
518 NvmeCtrl
*ctrl
= subsys
->ctrls
[i
];
521 nvme_attach_ns(ctrl
, ns
);
529 nvme_attach_ns(n
, ns
);
532 static Property nvme_ns_props
[] = {
533 DEFINE_BLOCK_PROPERTIES(NvmeNamespace
, blkconf
),
534 DEFINE_PROP_BOOL("detached", NvmeNamespace
, params
.detached
, false),
535 DEFINE_PROP_BOOL("shared", NvmeNamespace
, params
.shared
, false),
536 DEFINE_PROP_UINT32("nsid", NvmeNamespace
, params
.nsid
, 0),
537 DEFINE_PROP_UUID("uuid", NvmeNamespace
, params
.uuid
),
538 DEFINE_PROP_UINT64("eui64", NvmeNamespace
, params
.eui64
, 0),
539 DEFINE_PROP_UINT16("ms", NvmeNamespace
, params
.ms
, 0),
540 DEFINE_PROP_UINT8("mset", NvmeNamespace
, params
.mset
, 0),
541 DEFINE_PROP_UINT8("pi", NvmeNamespace
, params
.pi
, 0),
542 DEFINE_PROP_UINT8("pil", NvmeNamespace
, params
.pil
, 0),
543 DEFINE_PROP_UINT16("mssrl", NvmeNamespace
, params
.mssrl
, 128),
544 DEFINE_PROP_UINT32("mcl", NvmeNamespace
, params
.mcl
, 128),
545 DEFINE_PROP_UINT8("msrc", NvmeNamespace
, params
.msrc
, 127),
546 DEFINE_PROP_BOOL("zoned", NvmeNamespace
, params
.zoned
, false),
547 DEFINE_PROP_SIZE("zoned.zone_size", NvmeNamespace
, params
.zone_size_bs
,
548 NVME_DEFAULT_ZONE_SIZE
),
549 DEFINE_PROP_SIZE("zoned.zone_capacity", NvmeNamespace
, params
.zone_cap_bs
,
551 DEFINE_PROP_BOOL("zoned.cross_read", NvmeNamespace
,
552 params
.cross_zone_read
, false),
553 DEFINE_PROP_UINT32("zoned.max_active", NvmeNamespace
,
554 params
.max_active_zones
, 0),
555 DEFINE_PROP_UINT32("zoned.max_open", NvmeNamespace
,
556 params
.max_open_zones
, 0),
557 DEFINE_PROP_UINT32("zoned.descr_ext_size", NvmeNamespace
,
558 params
.zd_extension_size
, 0),
559 DEFINE_PROP_BOOL("eui64-default", NvmeNamespace
, params
.eui64_default
,
561 DEFINE_PROP_END_OF_LIST(),
564 static void nvme_ns_class_init(ObjectClass
*oc
, void *data
)
566 DeviceClass
*dc
= DEVICE_CLASS(oc
);
568 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
570 dc
->bus_type
= TYPE_NVME_BUS
;
571 dc
->realize
= nvme_ns_realize
;
572 dc
->unrealize
= nvme_ns_unrealize
;
573 device_class_set_props(dc
, nvme_ns_props
);
574 dc
->desc
= "Virtual NVMe namespace";
577 static void nvme_ns_instance_init(Object
*obj
)
579 NvmeNamespace
*ns
= NVME_NS(obj
);
580 char *bootindex
= g_strdup_printf("/namespace@%d,0", ns
->params
.nsid
);
582 device_add_bootindex_property(obj
, &ns
->bootindex
, "bootindex",
583 bootindex
, DEVICE(obj
));
588 static const TypeInfo nvme_ns_info
= {
589 .name
= TYPE_NVME_NS
,
590 .parent
= TYPE_DEVICE
,
591 .class_init
= nvme_ns_class_init
,
592 .instance_size
= sizeof(NvmeNamespace
),
593 .instance_init
= nvme_ns_instance_init
,
596 static void nvme_ns_register_types(void)
598 type_register_static(&nvme_ns_info
);
601 type_init(nvme_ns_register_types
)