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/units.h" 10eb2e8974SMinwoo Im #include "qemu/osdep.h" 11eb2e8974SMinwoo Im #include "qemu/uuid.h" 12eb2e8974SMinwoo Im #include "qemu/iov.h" 13eb2e8974SMinwoo Im #include "qemu/cutils.h" 14eb2e8974SMinwoo Im #include "qapi/error.h" 15eb2e8974SMinwoo Im #include "hw/qdev-properties.h" 16eb2e8974SMinwoo Im #include "hw/qdev-core.h" 17eb2e8974SMinwoo Im #include "hw/block/block.h" 18eb2e8974SMinwoo Im #include "block/aio.h" 19eb2e8974SMinwoo Im #include "block/accounting.h" 20eb2e8974SMinwoo Im #include "sysemu/sysemu.h" 21eb2e8974SMinwoo Im #include "hw/pci/pci.h" 22eb2e8974SMinwoo Im #include "nvme.h" 23eb2e8974SMinwoo Im #include "nvme-subsys.h" 24eb2e8974SMinwoo Im 25*e36a261dSMinwoo Im int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp) 26*e36a261dSMinwoo Im { 27*e36a261dSMinwoo Im NvmeSubsystem *subsys = n->subsys; 28*e36a261dSMinwoo Im int cntlid; 29*e36a261dSMinwoo Im 30*e36a261dSMinwoo Im for (cntlid = 0; cntlid < ARRAY_SIZE(subsys->ctrls); cntlid++) { 31*e36a261dSMinwoo Im if (!subsys->ctrls[cntlid]) { 32*e36a261dSMinwoo Im break; 33*e36a261dSMinwoo Im } 34*e36a261dSMinwoo Im } 35*e36a261dSMinwoo Im 36*e36a261dSMinwoo Im if (cntlid == ARRAY_SIZE(subsys->ctrls)) { 37*e36a261dSMinwoo Im error_setg(errp, "no more free controller id"); 38*e36a261dSMinwoo Im return -1; 39*e36a261dSMinwoo Im } 40*e36a261dSMinwoo Im 41*e36a261dSMinwoo Im subsys->ctrls[cntlid] = n; 42*e36a261dSMinwoo Im 43*e36a261dSMinwoo Im return cntlid; 44*e36a261dSMinwoo Im } 45*e36a261dSMinwoo Im 46eb2e8974SMinwoo Im static void nvme_subsys_setup(NvmeSubsystem *subsys) 47eb2e8974SMinwoo Im { 48eb2e8974SMinwoo Im const char *nqn = subsys->params.nqn ? 49eb2e8974SMinwoo Im subsys->params.nqn : subsys->parent_obj.id; 50eb2e8974SMinwoo Im 51eb2e8974SMinwoo Im snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn), 52eb2e8974SMinwoo Im "nqn.2019-08.org.qemu:%s", nqn); 53eb2e8974SMinwoo Im } 54eb2e8974SMinwoo Im 55eb2e8974SMinwoo Im static void nvme_subsys_realize(DeviceState *dev, Error **errp) 56eb2e8974SMinwoo Im { 57eb2e8974SMinwoo Im NvmeSubsystem *subsys = NVME_SUBSYS(dev); 58eb2e8974SMinwoo Im 59eb2e8974SMinwoo Im nvme_subsys_setup(subsys); 60eb2e8974SMinwoo Im } 61eb2e8974SMinwoo Im 62eb2e8974SMinwoo Im static Property nvme_subsystem_props[] = { 63eb2e8974SMinwoo Im DEFINE_PROP_STRING("nqn", NvmeSubsystem, params.nqn), 64eb2e8974SMinwoo Im DEFINE_PROP_END_OF_LIST(), 65eb2e8974SMinwoo Im }; 66eb2e8974SMinwoo Im 67eb2e8974SMinwoo Im static void nvme_subsys_class_init(ObjectClass *oc, void *data) 68eb2e8974SMinwoo Im { 69eb2e8974SMinwoo Im DeviceClass *dc = DEVICE_CLASS(oc); 70eb2e8974SMinwoo Im 71eb2e8974SMinwoo Im set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 72eb2e8974SMinwoo Im 73eb2e8974SMinwoo Im dc->realize = nvme_subsys_realize; 74eb2e8974SMinwoo Im dc->desc = "Virtual NVMe subsystem"; 75eb2e8974SMinwoo Im 76eb2e8974SMinwoo Im device_class_set_props(dc, nvme_subsystem_props); 77eb2e8974SMinwoo Im } 78eb2e8974SMinwoo Im 79eb2e8974SMinwoo Im static const TypeInfo nvme_subsys_info = { 80eb2e8974SMinwoo Im .name = TYPE_NVME_SUBSYS, 81eb2e8974SMinwoo Im .parent = TYPE_DEVICE, 82eb2e8974SMinwoo Im .class_init = nvme_subsys_class_init, 83eb2e8974SMinwoo Im .instance_size = sizeof(NvmeSubsystem), 84eb2e8974SMinwoo Im }; 85eb2e8974SMinwoo Im 86eb2e8974SMinwoo Im static void nvme_subsys_register_types(void) 87eb2e8974SMinwoo Im { 88eb2e8974SMinwoo Im type_register_static(&nvme_subsys_info); 89eb2e8974SMinwoo Im } 90eb2e8974SMinwoo Im 91eb2e8974SMinwoo Im type_init(nvme_subsys_register_types) 92