xref: /qemu/hw/nvme/subsys.c (revision 5ffbaeed164da1a87619a3abfadee0c7d63ea1c4)
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 
35b0fde9e8SKlaus Jensen void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeCtrl *n)
36b0fde9e8SKlaus Jensen {
37b0fde9e8SKlaus Jensen     subsys->ctrls[n->cntlid] = NULL;
38b0fde9e8SKlaus Jensen }
39b0fde9e8SKlaus Jensen 
40eb2e8974SMinwoo Im static void nvme_subsys_setup(NvmeSubsystem *subsys)
41eb2e8974SMinwoo Im {
42eb2e8974SMinwoo Im     const char *nqn = subsys->params.nqn ?
43eb2e8974SMinwoo Im         subsys->params.nqn : subsys->parent_obj.id;
44eb2e8974SMinwoo Im 
45eb2e8974SMinwoo Im     snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn),
46eb2e8974SMinwoo Im              "nqn.2019-08.org.qemu:%s", nqn);
47eb2e8974SMinwoo Im }
48eb2e8974SMinwoo Im 
49eb2e8974SMinwoo Im static void nvme_subsys_realize(DeviceState *dev, Error **errp)
50eb2e8974SMinwoo Im {
51eb2e8974SMinwoo Im     NvmeSubsystem *subsys = NVME_SUBSYS(dev);
52eb2e8974SMinwoo Im 
53*5ffbaeedSKlaus Jensen     qbus_create_inplace(&subsys->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev,
54*5ffbaeedSKlaus Jensen                         dev->id);
55*5ffbaeedSKlaus Jensen 
56eb2e8974SMinwoo Im     nvme_subsys_setup(subsys);
57eb2e8974SMinwoo Im }
58eb2e8974SMinwoo Im 
59eb2e8974SMinwoo Im static Property nvme_subsystem_props[] = {
60eb2e8974SMinwoo Im     DEFINE_PROP_STRING("nqn", NvmeSubsystem, params.nqn),
61eb2e8974SMinwoo Im     DEFINE_PROP_END_OF_LIST(),
62eb2e8974SMinwoo Im };
63eb2e8974SMinwoo Im 
64eb2e8974SMinwoo Im static void nvme_subsys_class_init(ObjectClass *oc, void *data)
65eb2e8974SMinwoo Im {
66eb2e8974SMinwoo Im     DeviceClass *dc = DEVICE_CLASS(oc);
67eb2e8974SMinwoo Im 
68eb2e8974SMinwoo Im     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
69eb2e8974SMinwoo Im 
70eb2e8974SMinwoo Im     dc->realize = nvme_subsys_realize;
71eb2e8974SMinwoo Im     dc->desc = "Virtual NVMe subsystem";
72cc6fb6bcSKlaus Jensen     dc->hotpluggable = false;
73eb2e8974SMinwoo Im 
74eb2e8974SMinwoo Im     device_class_set_props(dc, nvme_subsystem_props);
75eb2e8974SMinwoo Im }
76eb2e8974SMinwoo Im 
77eb2e8974SMinwoo Im static const TypeInfo nvme_subsys_info = {
78eb2e8974SMinwoo Im     .name = TYPE_NVME_SUBSYS,
79eb2e8974SMinwoo Im     .parent = TYPE_DEVICE,
80eb2e8974SMinwoo Im     .class_init = nvme_subsys_class_init,
81eb2e8974SMinwoo Im     .instance_size = sizeof(NvmeSubsystem),
82eb2e8974SMinwoo Im };
83eb2e8974SMinwoo Im 
84eb2e8974SMinwoo Im static void nvme_subsys_register_types(void)
85eb2e8974SMinwoo Im {
86eb2e8974SMinwoo Im     type_register_static(&nvme_subsys_info);
87eb2e8974SMinwoo Im }
88eb2e8974SMinwoo Im 
89eb2e8974SMinwoo Im type_init(nvme_subsys_register_types)
90