2 * QEMU NVM Express Subsystem: nvme-subsys
4 * Copyright (c) 2021 Minwoo Im <minwoo.im.dev@gmail.com>
6 * This code is licensed under the GNU GPL v2. Refer COPYING.
9 #include "qemu/units.h"
10 #include "qemu/osdep.h"
11 #include "qemu/uuid.h"
13 #include "qemu/cutils.h"
14 #include "qapi/error.h"
15 #include "hw/qdev-properties.h"
16 #include "hw/qdev-core.h"
17 #include "hw/block/block.h"
18 #include "block/aio.h"
19 #include "block/accounting.h"
20 #include "sysemu/sysemu.h"
21 #include "hw/pci/pci.h"
23 #include "nvme-subsys.h"
25 int nvme_subsys_register_ctrl(NvmeCtrl
*n
, Error
**errp
)
27 NvmeSubsystem
*subsys
= n
->subsys
;
30 for (cntlid
= 0; cntlid
< ARRAY_SIZE(subsys
->ctrls
); cntlid
++) {
31 if (!subsys
->ctrls
[cntlid
]) {
36 if (cntlid
== ARRAY_SIZE(subsys
->ctrls
)) {
37 error_setg(errp
, "no more free controller id");
41 subsys
->ctrls
[cntlid
] = n
;
46 int nvme_subsys_register_ns(NvmeNamespace
*ns
, Error
**errp
)
48 NvmeSubsystem
*subsys
= ns
->subsys
;
52 if (subsys
->namespaces
[nvme_nsid(ns
)]) {
53 error_setg(errp
, "namespace %d already registerd to subsy %s",
54 nvme_nsid(ns
), subsys
->parent_obj
.id
);
58 subsys
->namespaces
[nvme_nsid(ns
)] = ns
;
60 for (i
= 0; i
< ARRAY_SIZE(subsys
->ctrls
); i
++) {
63 if (n
&& nvme_register_namespace(n
, ns
, errp
)) {
71 static void nvme_subsys_setup(NvmeSubsystem
*subsys
)
73 const char *nqn
= subsys
->params
.nqn
?
74 subsys
->params
.nqn
: subsys
->parent_obj
.id
;
76 snprintf((char *)subsys
->subnqn
, sizeof(subsys
->subnqn
),
77 "nqn.2019-08.org.qemu:%s", nqn
);
80 static void nvme_subsys_realize(DeviceState
*dev
, Error
**errp
)
82 NvmeSubsystem
*subsys
= NVME_SUBSYS(dev
);
84 nvme_subsys_setup(subsys
);
87 static Property nvme_subsystem_props
[] = {
88 DEFINE_PROP_STRING("nqn", NvmeSubsystem
, params
.nqn
),
89 DEFINE_PROP_END_OF_LIST(),
92 static void nvme_subsys_class_init(ObjectClass
*oc
, void *data
)
94 DeviceClass
*dc
= DEVICE_CLASS(oc
);
96 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
98 dc
->realize
= nvme_subsys_realize
;
99 dc
->desc
= "Virtual NVMe subsystem";
101 device_class_set_props(dc
, nvme_subsystem_props
);
104 static const TypeInfo nvme_subsys_info
= {
105 .name
= TYPE_NVME_SUBSYS
,
106 .parent
= TYPE_DEVICE
,
107 .class_init
= nvme_subsys_class_init
,
108 .instance_size
= sizeof(NvmeSubsystem
),
111 static void nvme_subsys_register_types(void)
113 type_register_static(&nvme_subsys_info
);
116 type_init(nvme_subsys_register_types
)