block: Separate blk_is_writable() and blk_supports_write_perm()
[qemu.git] / hw / block / nvme-ns.c
blob2670787d2630f8a3d1b1c7f138b873960b17df6d
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/cutils.h"
18 #include "qemu/log.h"
19 #include "hw/block/block.h"
20 #include "hw/pci/pci.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/block-backend.h"
23 #include "qapi/error.h"
25 #include "hw/qdev-properties.h"
26 #include "hw/qdev-core.h"
28 #include "nvme.h"
29 #include "nvme-ns.h"
31 static void nvme_ns_init(NvmeNamespace *ns)
33 NvmeIdNs *id_ns = &ns->id_ns;
34 int lba_index = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
36 if (blk_get_flags(ns->blkconf.blk) & BDRV_O_UNMAP) {
37 ns->id_ns.dlfeat = 0x9;
40 id_ns->lbaf[lba_index].ds = 31 - clz32(ns->blkconf.logical_block_size);
42 id_ns->nsze = cpu_to_le64(nvme_ns_nlbas(ns));
44 /* no thin provisioning */
45 id_ns->ncap = id_ns->nsze;
46 id_ns->nuse = id_ns->ncap;
49 static int nvme_ns_init_blk(NvmeCtrl *n, NvmeNamespace *ns, Error **errp)
51 bool read_only;
53 if (!blkconf_blocksizes(&ns->blkconf, errp)) {
54 return -1;
57 read_only = !blk_supports_write_perm(ns->blkconf.blk);
58 if (!blkconf_apply_backend_options(&ns->blkconf, read_only, false, errp)) {
59 return -1;
62 ns->size = blk_getlength(ns->blkconf.blk);
63 if (ns->size < 0) {
64 error_setg_errno(errp, -ns->size, "could not get blockdev size");
65 return -1;
68 if (blk_enable_write_cache(ns->blkconf.blk)) {
69 n->features.vwc = 0x1;
72 return 0;
75 static int nvme_ns_check_constraints(NvmeNamespace *ns, Error **errp)
77 if (!ns->blkconf.blk) {
78 error_setg(errp, "block backend not configured");
79 return -1;
82 return 0;
85 int nvme_ns_setup(NvmeCtrl *n, NvmeNamespace *ns, Error **errp)
87 if (nvme_ns_check_constraints(ns, errp)) {
88 return -1;
91 if (nvme_ns_init_blk(n, ns, errp)) {
92 return -1;
95 nvme_ns_init(ns);
96 if (nvme_register_namespace(n, ns, errp)) {
97 return -1;
100 return 0;
103 void nvme_ns_drain(NvmeNamespace *ns)
105 blk_drain(ns->blkconf.blk);
108 void nvme_ns_flush(NvmeNamespace *ns)
110 blk_flush(ns->blkconf.blk);
113 static void nvme_ns_realize(DeviceState *dev, Error **errp)
115 NvmeNamespace *ns = NVME_NS(dev);
116 BusState *s = qdev_get_parent_bus(dev);
117 NvmeCtrl *n = NVME(s->parent);
118 Error *local_err = NULL;
120 if (nvme_ns_setup(n, ns, &local_err)) {
121 error_propagate_prepend(errp, local_err,
122 "could not setup namespace: ");
123 return;
127 static Property nvme_ns_props[] = {
128 DEFINE_BLOCK_PROPERTIES(NvmeNamespace, blkconf),
129 DEFINE_PROP_UINT32("nsid", NvmeNamespace, params.nsid, 0),
130 DEFINE_PROP_END_OF_LIST(),
133 static void nvme_ns_class_init(ObjectClass *oc, void *data)
135 DeviceClass *dc = DEVICE_CLASS(oc);
137 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
139 dc->bus_type = TYPE_NVME_BUS;
140 dc->realize = nvme_ns_realize;
141 device_class_set_props(dc, nvme_ns_props);
142 dc->desc = "Virtual NVMe namespace";
145 static void nvme_ns_instance_init(Object *obj)
147 NvmeNamespace *ns = NVME_NS(obj);
148 char *bootindex = g_strdup_printf("/namespace@%d,0", ns->params.nsid);
150 device_add_bootindex_property(obj, &ns->bootindex, "bootindex",
151 bootindex, DEVICE(obj));
153 g_free(bootindex);
156 static const TypeInfo nvme_ns_info = {
157 .name = TYPE_NVME_NS,
158 .parent = TYPE_DEVICE,
159 .class_init = nvme_ns_class_init,
160 .instance_size = sizeof(NvmeNamespace),
161 .instance_init = nvme_ns_instance_init,
164 static void nvme_ns_register_types(void)
166 type_register_static(&nvme_ns_info);
169 type_init(nvme_ns_register_types)