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/osdep.h"
10 #include "qapi/error.h"
14 int nvme_subsys_register_ctrl(NvmeCtrl
*n
, Error
**errp
)
16 NvmeSubsystem
*subsys
= n
->subsys
;
19 for (cntlid
= 0; cntlid
< ARRAY_SIZE(subsys
->ctrls
); cntlid
++) {
20 if (!subsys
->ctrls
[cntlid
]) {
25 if (cntlid
== ARRAY_SIZE(subsys
->ctrls
)) {
26 error_setg(errp
, "no more free controller id");
30 subsys
->ctrls
[cntlid
] = n
;
35 static void nvme_subsys_setup(NvmeSubsystem
*subsys
)
37 const char *nqn
= subsys
->params
.nqn
?
38 subsys
->params
.nqn
: subsys
->parent_obj
.id
;
40 snprintf((char *)subsys
->subnqn
, sizeof(subsys
->subnqn
),
41 "nqn.2019-08.org.qemu:%s", nqn
);
44 static void nvme_subsys_realize(DeviceState
*dev
, Error
**errp
)
46 NvmeSubsystem
*subsys
= NVME_SUBSYS(dev
);
48 nvme_subsys_setup(subsys
);
51 static Property nvme_subsystem_props
[] = {
52 DEFINE_PROP_STRING("nqn", NvmeSubsystem
, params
.nqn
),
53 DEFINE_PROP_END_OF_LIST(),
56 static void nvme_subsys_class_init(ObjectClass
*oc
, void *data
)
58 DeviceClass
*dc
= DEVICE_CLASS(oc
);
60 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
62 dc
->realize
= nvme_subsys_realize
;
63 dc
->desc
= "Virtual NVMe subsystem";
65 device_class_set_props(dc
, nvme_subsystem_props
);
68 static const TypeInfo nvme_subsys_info
= {
69 .name
= TYPE_NVME_SUBSYS
,
70 .parent
= TYPE_DEVICE
,
71 .class_init
= nvme_subsys_class_init
,
72 .instance_size
= sizeof(NvmeSubsystem
),
75 static void nvme_subsys_register_types(void)
77 type_register_static(&nvme_subsys_info
);
80 type_init(nvme_subsys_register_types
)