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/cutils.h"
19 #include "qemu/error-report.h"
20 #include "hw/block/block.h"
21 #include "hw/pci/pci.h"
22 #include "sysemu/sysemu.h"
23 #include "sysemu/block-backend.h"
24 #include "qapi/error.h"
26 #include "hw/qdev-properties.h"
27 #include "hw/qdev-core.h"
33 #define MIN_DISCARD_GRANULARITY (4 * KiB)
35 static int nvme_ns_init(NvmeNamespace
*ns
, Error
**errp
)
38 NvmeIdNs
*id_ns
= &ns
->id_ns
;
39 int lba_index
= NVME_ID_NS_FLBAS_INDEX(ns
->id_ns
.flbas
);
42 ns
->id_ns
.dlfeat
= 0x9;
44 id_ns
->lbaf
[lba_index
].ds
= 31 - clz32(ns
->blkconf
.logical_block_size
);
46 id_ns
->nsze
= cpu_to_le64(nvme_ns_nlbas(ns
));
48 ns
->csi
= NVME_CSI_NVM
;
50 /* no thin provisioning */
51 id_ns
->ncap
= id_ns
->nsze
;
52 id_ns
->nuse
= id_ns
->ncap
;
54 /* support DULBE and I/O optimization fields */
55 id_ns
->nsfeat
|= (0x4 | 0x10);
57 npdg
= ns
->blkconf
.discard_granularity
/ ns
->blkconf
.logical_block_size
;
59 if (bdrv_get_info(blk_bs(ns
->blkconf
.blk
), &bdi
) >= 0 &&
60 bdi
.cluster_size
> ns
->blkconf
.discard_granularity
) {
61 npdg
= bdi
.cluster_size
/ ns
->blkconf
.logical_block_size
;
64 id_ns
->npda
= id_ns
->npdg
= npdg
- 1;
66 if (nvme_ns_shared(ns
)) {
67 id_ns
->nmic
|= NVME_NMIC_NS_SHARED
;
71 id_ns
->mssrl
= cpu_to_le16(ns
->params
.mssrl
);
72 id_ns
->mcl
= cpu_to_le32(ns
->params
.mcl
);
73 id_ns
->msrc
= ns
->params
.msrc
;
78 static int nvme_ns_init_blk(NvmeNamespace
*ns
, Error
**errp
)
82 if (!blkconf_blocksizes(&ns
->blkconf
, errp
)) {
86 read_only
= !blk_supports_write_perm(ns
->blkconf
.blk
);
87 if (!blkconf_apply_backend_options(&ns
->blkconf
, read_only
, false, errp
)) {
91 if (ns
->blkconf
.discard_granularity
== -1) {
92 ns
->blkconf
.discard_granularity
=
93 MAX(ns
->blkconf
.logical_block_size
, MIN_DISCARD_GRANULARITY
);
96 ns
->size
= blk_getlength(ns
->blkconf
.blk
);
98 error_setg_errno(errp
, -ns
->size
, "could not get blockdev size");
105 static int nvme_ns_zoned_check_calc_geometry(NvmeNamespace
*ns
, Error
**errp
)
107 uint64_t zone_size
, zone_cap
;
108 uint32_t lbasz
= ns
->blkconf
.logical_block_size
;
110 /* Make sure that the values of ZNS properties are sane */
111 if (ns
->params
.zone_size_bs
) {
112 zone_size
= ns
->params
.zone_size_bs
;
114 zone_size
= NVME_DEFAULT_ZONE_SIZE
;
116 if (ns
->params
.zone_cap_bs
) {
117 zone_cap
= ns
->params
.zone_cap_bs
;
119 zone_cap
= zone_size
;
121 if (zone_cap
> zone_size
) {
122 error_setg(errp
, "zone capacity %"PRIu64
"B exceeds "
123 "zone size %"PRIu64
"B", zone_cap
, zone_size
);
126 if (zone_size
< lbasz
) {
127 error_setg(errp
, "zone size %"PRIu64
"B too small, "
128 "must be at least %"PRIu32
"B", zone_size
, lbasz
);
131 if (zone_cap
< lbasz
) {
132 error_setg(errp
, "zone capacity %"PRIu64
"B too small, "
133 "must be at least %"PRIu32
"B", zone_cap
, lbasz
);
138 * Save the main zone geometry values to avoid
139 * calculating them later again.
141 ns
->zone_size
= zone_size
/ lbasz
;
142 ns
->zone_capacity
= zone_cap
/ lbasz
;
143 ns
->num_zones
= ns
->size
/ lbasz
/ ns
->zone_size
;
145 /* Do a few more sanity checks of ZNS properties */
146 if (!ns
->num_zones
) {
148 "insufficient drive capacity, must be at least the size "
149 "of one zone (%"PRIu64
"B)", zone_size
);
153 if (ns
->params
.max_open_zones
> ns
->num_zones
) {
155 "max_open_zones value %u exceeds the number of zones %u",
156 ns
->params
.max_open_zones
, ns
->num_zones
);
159 if (ns
->params
.max_active_zones
> ns
->num_zones
) {
161 "max_active_zones value %u exceeds the number of zones %u",
162 ns
->params
.max_active_zones
, ns
->num_zones
);
166 if (ns
->params
.max_active_zones
) {
167 if (ns
->params
.max_open_zones
> ns
->params
.max_active_zones
) {
168 error_setg(errp
, "max_open_zones (%u) exceeds max_active_zones (%u)",
169 ns
->params
.max_open_zones
, ns
->params
.max_active_zones
);
173 if (!ns
->params
.max_open_zones
) {
174 ns
->params
.max_open_zones
= ns
->params
.max_active_zones
;
178 if (ns
->params
.zd_extension_size
) {
179 if (ns
->params
.zd_extension_size
& 0x3f) {
181 "zone descriptor extension size must be a multiple of 64B");
184 if ((ns
->params
.zd_extension_size
>> 6) > 0xff) {
185 error_setg(errp
, "zone descriptor extension size is too large");
193 static void nvme_ns_zoned_init_state(NvmeNamespace
*ns
)
195 uint64_t start
= 0, zone_size
= ns
->zone_size
;
196 uint64_t capacity
= ns
->num_zones
* zone_size
;
200 ns
->zone_array
= g_new0(NvmeZone
, ns
->num_zones
);
201 if (ns
->params
.zd_extension_size
) {
202 ns
->zd_extensions
= g_malloc0(ns
->params
.zd_extension_size
*
206 QTAILQ_INIT(&ns
->exp_open_zones
);
207 QTAILQ_INIT(&ns
->imp_open_zones
);
208 QTAILQ_INIT(&ns
->closed_zones
);
209 QTAILQ_INIT(&ns
->full_zones
);
211 zone
= ns
->zone_array
;
212 for (i
= 0; i
< ns
->num_zones
; i
++, zone
++) {
213 if (start
+ zone_size
> capacity
) {
214 zone_size
= capacity
- start
;
216 zone
->d
.zt
= NVME_ZONE_TYPE_SEQ_WRITE
;
217 nvme_set_zone_state(zone
, NVME_ZONE_STATE_EMPTY
);
219 zone
->d
.zcap
= ns
->zone_capacity
;
220 zone
->d
.zslba
= start
;
226 ns
->zone_size_log2
= 0;
227 if (is_power_of_2(ns
->zone_size
)) {
228 ns
->zone_size_log2
= 63 - clz64(ns
->zone_size
);
232 static void nvme_ns_init_zoned(NvmeNamespace
*ns
, int lba_index
)
234 NvmeIdNsZoned
*id_ns_z
;
236 nvme_ns_zoned_init_state(ns
);
238 id_ns_z
= g_malloc0(sizeof(NvmeIdNsZoned
));
240 /* MAR/MOR are zeroes-based, 0xffffffff means no limit */
241 id_ns_z
->mar
= cpu_to_le32(ns
->params
.max_active_zones
- 1);
242 id_ns_z
->mor
= cpu_to_le32(ns
->params
.max_open_zones
- 1);
244 id_ns_z
->ozcs
= ns
->params
.cross_zone_read
? 0x01 : 0x00;
246 id_ns_z
->lbafe
[lba_index
].zsze
= cpu_to_le64(ns
->zone_size
);
247 id_ns_z
->lbafe
[lba_index
].zdes
=
248 ns
->params
.zd_extension_size
>> 6; /* Units of 64B */
250 ns
->csi
= NVME_CSI_ZONED
;
251 ns
->id_ns
.nsze
= cpu_to_le64(ns
->num_zones
* ns
->zone_size
);
252 ns
->id_ns
.ncap
= ns
->id_ns
.nsze
;
253 ns
->id_ns
.nuse
= ns
->id_ns
.ncap
;
256 * The device uses the BDRV_BLOCK_ZERO flag to determine the "deallocated"
257 * status of logical blocks. Since the spec defines that logical blocks
258 * SHALL be deallocated when then zone is in the Empty or Offline states,
259 * we can only support DULBE if the zone size is a multiple of the
262 if (ns
->zone_size
% (ns
->id_ns
.npdg
+ 1)) {
263 warn_report("the zone size (%"PRIu64
" blocks) is not a multiple of "
264 "the calculated deallocation granularity (%d blocks); "
265 "DULBE support disabled",
266 ns
->zone_size
, ns
->id_ns
.npdg
+ 1);
268 ns
->id_ns
.nsfeat
&= ~0x4;
271 ns
->id_ns_zoned
= id_ns_z
;
274 static void nvme_clear_zone(NvmeNamespace
*ns
, NvmeZone
*zone
)
278 zone
->w_ptr
= zone
->d
.wp
;
279 state
= nvme_get_zone_state(zone
);
280 if (zone
->d
.wp
!= zone
->d
.zslba
||
281 (zone
->d
.za
& NVME_ZA_ZD_EXT_VALID
)) {
282 if (state
!= NVME_ZONE_STATE_CLOSED
) {
283 trace_pci_nvme_clear_ns_close(state
, zone
->d
.zslba
);
284 nvme_set_zone_state(zone
, NVME_ZONE_STATE_CLOSED
);
286 nvme_aor_inc_active(ns
);
287 QTAILQ_INSERT_HEAD(&ns
->closed_zones
, zone
, entry
);
289 trace_pci_nvme_clear_ns_reset(state
, zone
->d
.zslba
);
290 nvme_set_zone_state(zone
, NVME_ZONE_STATE_EMPTY
);
295 * Close all the zones that are currently open.
297 static void nvme_zoned_ns_shutdown(NvmeNamespace
*ns
)
299 NvmeZone
*zone
, *next
;
301 QTAILQ_FOREACH_SAFE(zone
, &ns
->closed_zones
, entry
, next
) {
302 QTAILQ_REMOVE(&ns
->closed_zones
, zone
, entry
);
303 nvme_aor_dec_active(ns
);
304 nvme_clear_zone(ns
, zone
);
306 QTAILQ_FOREACH_SAFE(zone
, &ns
->imp_open_zones
, entry
, next
) {
307 QTAILQ_REMOVE(&ns
->imp_open_zones
, zone
, entry
);
308 nvme_aor_dec_open(ns
);
309 nvme_aor_dec_active(ns
);
310 nvme_clear_zone(ns
, zone
);
312 QTAILQ_FOREACH_SAFE(zone
, &ns
->exp_open_zones
, entry
, next
) {
313 QTAILQ_REMOVE(&ns
->exp_open_zones
, zone
, entry
);
314 nvme_aor_dec_open(ns
);
315 nvme_aor_dec_active(ns
);
316 nvme_clear_zone(ns
, zone
);
319 assert(ns
->nr_open_zones
== 0);
322 static int nvme_ns_check_constraints(NvmeNamespace
*ns
, Error
**errp
)
324 if (!ns
->blkconf
.blk
) {
325 error_setg(errp
, "block backend not configured");
332 int nvme_ns_setup(NvmeNamespace
*ns
, Error
**errp
)
334 if (nvme_ns_check_constraints(ns
, errp
)) {
338 if (nvme_ns_init_blk(ns
, errp
)) {
342 if (nvme_ns_init(ns
, errp
)) {
345 if (ns
->params
.zoned
) {
346 if (nvme_ns_zoned_check_calc_geometry(ns
, errp
) != 0) {
349 nvme_ns_init_zoned(ns
, 0);
355 void nvme_ns_drain(NvmeNamespace
*ns
)
357 blk_drain(ns
->blkconf
.blk
);
360 void nvme_ns_shutdown(NvmeNamespace
*ns
)
362 blk_flush(ns
->blkconf
.blk
);
363 if (ns
->params
.zoned
) {
364 nvme_zoned_ns_shutdown(ns
);
368 void nvme_ns_cleanup(NvmeNamespace
*ns
)
370 if (ns
->params
.zoned
) {
371 g_free(ns
->id_ns_zoned
);
372 g_free(ns
->zone_array
);
373 g_free(ns
->zd_extensions
);
377 static void nvme_ns_realize(DeviceState
*dev
, Error
**errp
)
379 NvmeNamespace
*ns
= NVME_NS(dev
);
380 BusState
*s
= qdev_get_parent_bus(dev
);
381 NvmeCtrl
*n
= NVME(s
->parent
);
383 if (nvme_ns_setup(ns
, errp
)) {
388 if (nvme_subsys_register_ns(ns
, errp
)) {
392 if (nvme_register_namespace(n
, ns
, errp
)) {
398 static Property nvme_ns_props
[] = {
399 DEFINE_BLOCK_PROPERTIES(NvmeNamespace
, blkconf
),
400 DEFINE_PROP_LINK("subsys", NvmeNamespace
, subsys
, TYPE_NVME_SUBSYS
,
402 DEFINE_PROP_BOOL("detached", NvmeNamespace
, params
.detached
, false),
403 DEFINE_PROP_UINT32("nsid", NvmeNamespace
, params
.nsid
, 0),
404 DEFINE_PROP_UUID("uuid", NvmeNamespace
, params
.uuid
),
405 DEFINE_PROP_UINT16("mssrl", NvmeNamespace
, params
.mssrl
, 128),
406 DEFINE_PROP_UINT32("mcl", NvmeNamespace
, params
.mcl
, 128),
407 DEFINE_PROP_UINT8("msrc", NvmeNamespace
, params
.msrc
, 127),
408 DEFINE_PROP_BOOL("zoned", NvmeNamespace
, params
.zoned
, false),
409 DEFINE_PROP_SIZE("zoned.zone_size", NvmeNamespace
, params
.zone_size_bs
,
410 NVME_DEFAULT_ZONE_SIZE
),
411 DEFINE_PROP_SIZE("zoned.zone_capacity", NvmeNamespace
, params
.zone_cap_bs
,
413 DEFINE_PROP_BOOL("zoned.cross_read", NvmeNamespace
,
414 params
.cross_zone_read
, false),
415 DEFINE_PROP_UINT32("zoned.max_active", NvmeNamespace
,
416 params
.max_active_zones
, 0),
417 DEFINE_PROP_UINT32("zoned.max_open", NvmeNamespace
,
418 params
.max_open_zones
, 0),
419 DEFINE_PROP_UINT32("zoned.descr_ext_size", NvmeNamespace
,
420 params
.zd_extension_size
, 0),
421 DEFINE_PROP_END_OF_LIST(),
424 static void nvme_ns_class_init(ObjectClass
*oc
, void *data
)
426 DeviceClass
*dc
= DEVICE_CLASS(oc
);
428 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
430 dc
->bus_type
= TYPE_NVME_BUS
;
431 dc
->realize
= nvme_ns_realize
;
432 device_class_set_props(dc
, nvme_ns_props
);
433 dc
->desc
= "Virtual NVMe namespace";
436 static void nvme_ns_instance_init(Object
*obj
)
438 NvmeNamespace
*ns
= NVME_NS(obj
);
439 char *bootindex
= g_strdup_printf("/namespace@%d,0", ns
->params
.nsid
);
441 device_add_bootindex_property(obj
, &ns
->bootindex
, "bootindex",
442 bootindex
, DEVICE(obj
));
447 static const TypeInfo nvme_ns_info
= {
448 .name
= TYPE_NVME_NS
,
449 .parent
= TYPE_DEVICE
,
450 .class_init
= nvme_ns_class_init
,
451 .instance_size
= sizeof(NvmeNamespace
),
452 .instance_init
= nvme_ns_instance_init
,
455 static void nvme_ns_register_types(void)
457 type_register_static(&nvme_ns_info
);
460 type_init(nvme_ns_register_types
)