os-posix: Add os_set_daemonize()
[qemu/armbru.git] / hw / nvme / ns.c
blob8a3613d9ab0e87cf9d73466c8f045953515959f0
1 /*
2 * QEMU NVM Express Virtual Namespace
4 * Copyright (c) 2019 CNEX Labs
5 * Copyright (c) 2020 Samsung Electronics
7 * Authors:
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"
22 #include "nvme.h"
23 #include "trace.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;
31 BlockDriverInfo bdi;
32 int npdg, nlbas, ret;
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;
62 uint8_t ds;
63 uint16_t ms;
64 int i;
66 ns->csi = NVME_CSI_NVM;
67 ns->status = 0x0;
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 */
79 ++ns_count;
80 if (!ns->params.eui64 && ns->params.eui64_default) {
81 ns->params.eui64 = ns_count + NVME_EUI64_DEFAULT;
84 /* simple copy */
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);
91 ms = ns->params.ms;
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;
99 id_ns->dpc = 0x1f;
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] = {
108 [0] = { .ds = 9 },
109 [1] = { .ds = 9, .ms = 8 },
110 [2] = { .ds = 9, .ms = 16 },
111 [3] = { .ds = 9, .ms = 64 },
112 [4] = { .ds = 12 },
113 [5] = { .ds = 12, .ms = 8 },
114 [6] = { .ds = 12, .ms = 16 },
115 [7] = { .ds = 12, .ms = 64 },
118 ns->nlbaf = 8;
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) {
126 id_ns->flbas |= i;
127 goto lbaf_found;
132 /* add non-standard lba format */
133 id_ns->lbaf[ns->nlbaf].ds = ds;
134 id_ns->lbaf[ns->nlbaf].ms = ms;
135 ns->nlbaf++;
137 id_ns->flbas |= i;
140 lbaf_found:
141 id_ns_nvm->elbaf[i] = (ns->pif & 0x3) << 7;
142 id_ns->nlbaf = ns->nlbaf - 1;
143 nvme_ns_init_format(ns);
145 return 0;
148 static int nvme_ns_init_blk(NvmeNamespace *ns, Error **errp)
150 bool read_only;
152 if (!blkconf_blocksizes(&ns->blkconf, errp)) {
153 return -1;
156 read_only = !blk_supports_write_perm(ns->blkconf.blk);
157 if (!blkconf_apply_backend_options(&ns->blkconf, read_only, false, errp)) {
158 return -1;
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);
167 if (ns->size < 0) {
168 error_setg_errno(errp, -ns->size, "could not get blockdev size");
169 return -1;
172 return 0;
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;
182 } else {
183 zone_size = NVME_DEFAULT_ZONE_SIZE;
185 if (ns->params.zone_cap_bs) {
186 zone_cap = ns->params.zone_cap_bs;
187 } else {
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);
193 return -1;
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);
198 return -1;
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);
203 return -1;
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) {
216 error_setg(errp,
217 "insufficient drive capacity, must be at least the size "
218 "of one zone (%"PRIu64"B)", zone_size);
219 return -1;
222 return 0;
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;
229 NvmeZone *zone;
230 int i;
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 *
235 ns->num_zones);
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);
250 zone->d.za = 0;
251 zone->d.zcap = ns->zone_capacity;
252 zone->d.zslba = start;
253 zone->d.wp = start;
254 zone->w_ptr = start;
255 start += zone_size;
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;
267 int i;
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);
276 id_ns_z->zoc = 0;
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
313 * calculated NPDG.
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)
329 uint8_t state;
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);
341 } else {
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;
345 ns->zns.numzrwa++;
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");
385 return -1;
388 if (ns->params.pi) {
389 if (ns->params.pi > NVME_ID_NS_DPS_TYPE_3) {
390 error_setg(errp, "invalid 'pi' value");
391 return -1;
394 switch (ns->params.pif) {
395 case NVME_PI_GUARD_16:
396 pi_size = 8;
397 break;
398 case NVME_PI_GUARD_64:
399 pi_size = 16;
400 break;
401 default:
402 error_setg(errp, "invalid 'pif'");
403 return -1;
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);
409 return -1;
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);
416 return -1;
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);
425 return -1;
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 "
436 "multiple of 64B");
437 return -1;
439 if ((ns->params.zd_extension_size >> 6) > 0xff) {
440 error_setg(errp,
441 "zone descriptor extension size is too large");
442 return -1;
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);
452 return -1;
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);
464 return -1;
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)",
473 ns->params.numzrwa,
474 ns->params.max_active_zones);
475 return -1;
481 return 0;
484 int nvme_ns_setup(NvmeNamespace *ns, Error **errp)
486 if (nvme_ns_check_constraints(ns, errp)) {
487 return -1;
490 if (nvme_ns_init_blk(ns, errp)) {
491 return -1;
494 if (nvme_ns_init(ns, errp)) {
495 return -1;
497 if (ns->params.zoned) {
498 if (nvme_ns_zoned_check_calc_geometry(ns, errp) != 0) {
499 return -1;
501 nvme_ns_init_zoned(ns);
504 return 0;
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);
533 nvme_ns_drain(ns);
534 nvme_ns_shutdown(ns);
535 nvme_ns_cleanup(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;
545 int i;
547 if (!n->subsys) {
548 if (ns->params.detached) {
549 error_setg(errp, "detached requires that the nvme device is "
550 "linked to an nvme-subsys device");
551 return;
553 } else {
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)) {
559 return;
563 if (nvme_ns_setup(ns, errp)) {
564 return;
567 if (!nsid) {
568 for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
569 if (nvme_ns(n, i) || nvme_subsys_ns(subsys, i)) {
570 continue;
573 nsid = ns->params.nsid = i;
574 break;
577 if (!nsid) {
578 error_setg(errp, "no free namespace id");
579 return;
581 } else {
582 if (nvme_ns(n, nsid) || nvme_subsys_ns(subsys, nsid)) {
583 error_setg(errp, "namespace id '%d' already allocated", nsid);
584 return;
588 if (subsys) {
589 subsys->namespaces[nsid] = ns;
591 if (ns->params.detached) {
592 return;
595 if (ns->params.shared) {
596 for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
597 NvmeCtrl *ctrl = subsys->ctrls[i];
599 if (ctrl) {
600 nvme_attach_ns(ctrl, ns);
604 return;
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,
643 true),
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));
668 g_free(bootindex);
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)