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