xref: /qemu/hw/nvme/subsys.c (revision cc6fb6bc506e6c47ed604fcb7b7413dff0b7d845)
1eb2e8974SMinwoo Im /*
2eb2e8974SMinwoo Im  * QEMU NVM Express Subsystem: nvme-subsys
3eb2e8974SMinwoo Im  *
4eb2e8974SMinwoo Im  * Copyright (c) 2021 Minwoo Im <minwoo.im.dev@gmail.com>
5eb2e8974SMinwoo Im  *
6eb2e8974SMinwoo Im  * This code is licensed under the GNU GPL v2.  Refer COPYING.
7eb2e8974SMinwoo Im  */
8eb2e8974SMinwoo Im 
9eb2e8974SMinwoo Im #include "qemu/osdep.h"
10eb2e8974SMinwoo Im #include "qapi/error.h"
117ef37c1cSKlaus Jensen 
12eb2e8974SMinwoo Im #include "nvme.h"
13eb2e8974SMinwoo Im 
14e36a261dSMinwoo Im int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp)
15e36a261dSMinwoo Im {
16e36a261dSMinwoo Im     NvmeSubsystem *subsys = n->subsys;
17e36a261dSMinwoo Im     int cntlid;
18e36a261dSMinwoo Im 
19e36a261dSMinwoo Im     for (cntlid = 0; cntlid < ARRAY_SIZE(subsys->ctrls); cntlid++) {
20e36a261dSMinwoo Im         if (!subsys->ctrls[cntlid]) {
21e36a261dSMinwoo Im             break;
22e36a261dSMinwoo Im         }
23e36a261dSMinwoo Im     }
24e36a261dSMinwoo Im 
25e36a261dSMinwoo Im     if (cntlid == ARRAY_SIZE(subsys->ctrls)) {
26e36a261dSMinwoo Im         error_setg(errp, "no more free controller id");
27e36a261dSMinwoo Im         return -1;
28e36a261dSMinwoo Im     }
29e36a261dSMinwoo Im 
30e36a261dSMinwoo Im     subsys->ctrls[cntlid] = n;
31e36a261dSMinwoo Im 
32e36a261dSMinwoo Im     return cntlid;
33e36a261dSMinwoo Im }
34e36a261dSMinwoo Im 
35eb2e8974SMinwoo Im static void nvme_subsys_setup(NvmeSubsystem *subsys)
36eb2e8974SMinwoo Im {
37eb2e8974SMinwoo Im     const char *nqn = subsys->params.nqn ?
38eb2e8974SMinwoo Im         subsys->params.nqn : subsys->parent_obj.id;
39eb2e8974SMinwoo Im 
40eb2e8974SMinwoo Im     snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn),
41eb2e8974SMinwoo Im              "nqn.2019-08.org.qemu:%s", nqn);
42eb2e8974SMinwoo Im }
43eb2e8974SMinwoo Im 
44eb2e8974SMinwoo Im static void nvme_subsys_realize(DeviceState *dev, Error **errp)
45eb2e8974SMinwoo Im {
46eb2e8974SMinwoo Im     NvmeSubsystem *subsys = NVME_SUBSYS(dev);
47eb2e8974SMinwoo Im 
48eb2e8974SMinwoo Im     nvme_subsys_setup(subsys);
49eb2e8974SMinwoo Im }
50eb2e8974SMinwoo Im 
51eb2e8974SMinwoo Im static Property nvme_subsystem_props[] = {
52eb2e8974SMinwoo Im     DEFINE_PROP_STRING("nqn", NvmeSubsystem, params.nqn),
53eb2e8974SMinwoo Im     DEFINE_PROP_END_OF_LIST(),
54eb2e8974SMinwoo Im };
55eb2e8974SMinwoo Im 
56eb2e8974SMinwoo Im static void nvme_subsys_class_init(ObjectClass *oc, void *data)
57eb2e8974SMinwoo Im {
58eb2e8974SMinwoo Im     DeviceClass *dc = DEVICE_CLASS(oc);
59eb2e8974SMinwoo Im 
60eb2e8974SMinwoo Im     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
61eb2e8974SMinwoo Im 
62eb2e8974SMinwoo Im     dc->realize = nvme_subsys_realize;
63eb2e8974SMinwoo Im     dc->desc = "Virtual NVMe subsystem";
64*cc6fb6bcSKlaus Jensen     dc->hotpluggable = false;
65eb2e8974SMinwoo Im 
66eb2e8974SMinwoo Im     device_class_set_props(dc, nvme_subsystem_props);
67eb2e8974SMinwoo Im }
68eb2e8974SMinwoo Im 
69eb2e8974SMinwoo Im static const TypeInfo nvme_subsys_info = {
70eb2e8974SMinwoo Im     .name = TYPE_NVME_SUBSYS,
71eb2e8974SMinwoo Im     .parent = TYPE_DEVICE,
72eb2e8974SMinwoo Im     .class_init = nvme_subsys_class_init,
73eb2e8974SMinwoo Im     .instance_size = sizeof(NvmeSubsystem),
74eb2e8974SMinwoo Im };
75eb2e8974SMinwoo Im 
76eb2e8974SMinwoo Im static void nvme_subsys_register_types(void)
77eb2e8974SMinwoo Im {
78eb2e8974SMinwoo Im     type_register_static(&nvme_subsys_info);
79eb2e8974SMinwoo Im }
80eb2e8974SMinwoo Im 
81eb2e8974SMinwoo Im type_init(nvme_subsys_register_types)
82