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 "qemu/units.h" 11 #include "qapi/error.h" 12 13 #include "nvme.h" 14 15 #define NVME_DEFAULT_RU_SIZE (96 * MiB) 16 17 static int nvme_subsys_reserve_cntlids(NvmeCtrl *n, int start, int num) 18 { 19 NvmeSubsystem *subsys = n->subsys; 20 NvmeSecCtrlList *list = &n->sec_ctrl_list; 21 NvmeSecCtrlEntry *sctrl; 22 int i, cnt = 0; 23 24 for (i = start; i < ARRAY_SIZE(subsys->ctrls) && cnt < num; i++) { 25 if (!subsys->ctrls[i]) { 26 sctrl = &list->sec[cnt]; 27 sctrl->scid = cpu_to_le16(i); 28 subsys->ctrls[i] = SUBSYS_SLOT_RSVD; 29 cnt++; 30 } 31 } 32 33 return cnt; 34 } 35 36 static void nvme_subsys_unreserve_cntlids(NvmeCtrl *n) 37 { 38 NvmeSubsystem *subsys = n->subsys; 39 NvmeSecCtrlList *list = &n->sec_ctrl_list; 40 NvmeSecCtrlEntry *sctrl; 41 int i, cntlid; 42 43 for (i = 0; i < n->params.sriov_max_vfs; i++) { 44 sctrl = &list->sec[i]; 45 cntlid = le16_to_cpu(sctrl->scid); 46 47 if (cntlid) { 48 assert(subsys->ctrls[cntlid] == SUBSYS_SLOT_RSVD); 49 subsys->ctrls[cntlid] = NULL; 50 sctrl->scid = 0; 51 } 52 } 53 } 54 55 int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp) 56 { 57 NvmeSubsystem *subsys = n->subsys; 58 NvmeSecCtrlEntry *sctrl = nvme_sctrl(n); 59 int cntlid, nsid, num_rsvd, num_vfs = n->params.sriov_max_vfs; 60 61 if (pci_is_vf(&n->parent_obj)) { 62 cntlid = le16_to_cpu(sctrl->scid); 63 } else { 64 for (cntlid = 0; cntlid < ARRAY_SIZE(subsys->ctrls); cntlid++) { 65 if (!subsys->ctrls[cntlid]) { 66 break; 67 } 68 } 69 70 if (cntlid == ARRAY_SIZE(subsys->ctrls)) { 71 error_setg(errp, "no more free controller id"); 72 return -1; 73 } 74 75 num_rsvd = nvme_subsys_reserve_cntlids(n, cntlid + 1, num_vfs); 76 if (num_rsvd != num_vfs) { 77 nvme_subsys_unreserve_cntlids(n); 78 error_setg(errp, 79 "no more free controller ids for secondary controllers"); 80 return -1; 81 } 82 } 83 84 if (!subsys->serial) { 85 subsys->serial = g_strdup(n->params.serial); 86 } else if (strcmp(subsys->serial, n->params.serial)) { 87 error_setg(errp, "invalid controller serial"); 88 return -1; 89 } 90 91 subsys->ctrls[cntlid] = n; 92 93 for (nsid = 1; nsid < ARRAY_SIZE(subsys->namespaces); nsid++) { 94 NvmeNamespace *ns = subsys->namespaces[nsid]; 95 if (ns && ns->params.shared && !ns->params.detached) { 96 nvme_attach_ns(n, ns); 97 } 98 } 99 100 return cntlid; 101 } 102 103 void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeCtrl *n) 104 { 105 if (pci_is_vf(&n->parent_obj)) { 106 subsys->ctrls[n->cntlid] = SUBSYS_SLOT_RSVD; 107 } else { 108 subsys->ctrls[n->cntlid] = NULL; 109 nvme_subsys_unreserve_cntlids(n); 110 } 111 112 n->cntlid = -1; 113 } 114 115 static bool nvme_calc_rgif(uint16_t nruh, uint16_t nrg, uint8_t *rgif) 116 { 117 uint16_t val; 118 unsigned int i; 119 120 if (unlikely(nrg == 1)) { 121 /* PIDRG_NORGI scenario, all of pid is used for PHID */ 122 *rgif = 0; 123 return true; 124 } 125 126 val = nrg; 127 i = 0; 128 while (val) { 129 val >>= 1; 130 i++; 131 } 132 *rgif = i; 133 134 /* ensure remaining bits suffice to represent number of phids in a RG */ 135 if (unlikely((UINT16_MAX >> i) < nruh)) { 136 *rgif = 0; 137 return false; 138 } 139 140 return true; 141 } 142 143 static bool nvme_subsys_setup_fdp(NvmeSubsystem *subsys, Error **errp) 144 { 145 NvmeEnduranceGroup *endgrp = &subsys->endgrp; 146 147 if (!subsys->params.fdp.runs) { 148 error_setg(errp, "fdp.runs must be non-zero"); 149 return false; 150 } 151 152 endgrp->fdp.runs = subsys->params.fdp.runs; 153 154 if (!subsys->params.fdp.nrg) { 155 error_setg(errp, "fdp.nrg must be non-zero"); 156 return false; 157 } 158 159 endgrp->fdp.nrg = subsys->params.fdp.nrg; 160 161 if (!subsys->params.fdp.nruh) { 162 error_setg(errp, "fdp.nruh must be non-zero"); 163 return false; 164 } 165 166 endgrp->fdp.nruh = subsys->params.fdp.nruh; 167 168 if (!nvme_calc_rgif(endgrp->fdp.nruh, endgrp->fdp.nrg, &endgrp->fdp.rgif)) { 169 error_setg(errp, 170 "cannot derive a valid rgif (nruh %"PRIu16" nrg %"PRIu32")", 171 endgrp->fdp.nruh, endgrp->fdp.nrg); 172 return false; 173 } 174 175 endgrp->fdp.ruhs = g_new(NvmeRuHandle, endgrp->fdp.nruh); 176 177 for (uint16_t ruhid = 0; ruhid < endgrp->fdp.nruh; ruhid++) { 178 endgrp->fdp.ruhs[ruhid] = (NvmeRuHandle) { 179 .ruht = NVME_RUHT_INITIALLY_ISOLATED, 180 .ruha = NVME_RUHA_UNUSED, 181 }; 182 183 endgrp->fdp.ruhs[ruhid].rus = g_new(NvmeReclaimUnit, endgrp->fdp.nrg); 184 } 185 186 endgrp->fdp.enabled = true; 187 188 return true; 189 } 190 191 static bool nvme_subsys_setup(NvmeSubsystem *subsys, Error **errp) 192 { 193 const char *nqn = subsys->params.nqn ? 194 subsys->params.nqn : subsys->parent_obj.id; 195 196 snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn), 197 "nqn.2019-08.org.qemu:%s", nqn); 198 199 if (subsys->params.fdp.enabled && !nvme_subsys_setup_fdp(subsys, errp)) { 200 return false; 201 } 202 203 return true; 204 } 205 206 static void nvme_subsys_realize(DeviceState *dev, Error **errp) 207 { 208 NvmeSubsystem *subsys = NVME_SUBSYS(dev); 209 210 qbus_init(&subsys->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id); 211 212 nvme_subsys_setup(subsys, errp); 213 } 214 215 static Property nvme_subsystem_props[] = { 216 DEFINE_PROP_STRING("nqn", NvmeSubsystem, params.nqn), 217 DEFINE_PROP_BOOL("fdp", NvmeSubsystem, params.fdp.enabled, false), 218 DEFINE_PROP_SIZE("fdp.runs", NvmeSubsystem, params.fdp.runs, 219 NVME_DEFAULT_RU_SIZE), 220 DEFINE_PROP_UINT32("fdp.nrg", NvmeSubsystem, params.fdp.nrg, 1), 221 DEFINE_PROP_UINT16("fdp.nruh", NvmeSubsystem, params.fdp.nruh, 0), 222 DEFINE_PROP_END_OF_LIST(), 223 }; 224 225 static void nvme_subsys_class_init(ObjectClass *oc, void *data) 226 { 227 DeviceClass *dc = DEVICE_CLASS(oc); 228 229 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 230 231 dc->realize = nvme_subsys_realize; 232 dc->desc = "Virtual NVMe subsystem"; 233 dc->hotpluggable = false; 234 235 device_class_set_props(dc, nvme_subsystem_props); 236 } 237 238 static const TypeInfo nvme_subsys_info = { 239 .name = TYPE_NVME_SUBSYS, 240 .parent = TYPE_DEVICE, 241 .class_init = nvme_subsys_class_init, 242 .instance_size = sizeof(NvmeSubsystem), 243 }; 244 245 static void nvme_subsys_register_types(void) 246 { 247 type_register_static(&nvme_subsys_info); 248 } 249 250 type_init(nvme_subsys_register_types) 251