Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / nvme / subsys.c
blob77deaf2c2c97317ea981ba8659f0aea00dee6dca
1 /*
2 * QEMU NVM Express Subsystem: nvme-subsys
4 * Copyright (c) 2021 Minwoo Im <minwoo.im.dev@gmail.com>
6 * This code is licensed under the GNU GPL v2. Refer COPYING.
7 */
9 #include "qemu/osdep.h"
10 #include "qemu/units.h"
11 #include "qapi/error.h"
13 #include "nvme.h"
15 #define NVME_DEFAULT_RU_SIZE (96 * MiB)
17 static int nvme_subsys_reserve_cntlids(NvmeCtrl *n, int start, int num)
19 NvmeSubsystem *subsys = n->subsys;
20 NvmeSecCtrlEntry *list = n->sec_ctrl_list;
21 NvmeSecCtrlEntry *sctrl;
22 int i, cnt = 0;
24 for (i = start; i < ARRAY_SIZE(subsys->ctrls) && cnt < num; i++) {
25 if (!subsys->ctrls[i]) {
26 sctrl = &list[cnt];
27 sctrl->scid = cpu_to_le16(i);
28 subsys->ctrls[i] = SUBSYS_SLOT_RSVD;
29 cnt++;
33 return cnt;
36 static void nvme_subsys_unreserve_cntlids(NvmeCtrl *n)
38 NvmeSubsystem *subsys = n->subsys;
39 NvmeSecCtrlEntry *list = n->sec_ctrl_list;
40 NvmeSecCtrlEntry *sctrl;
41 int i, cntlid;
43 for (i = 0; i < n->params.sriov_max_vfs; i++) {
44 sctrl = &list[i];
45 cntlid = le16_to_cpu(sctrl->scid);
47 if (cntlid) {
48 assert(subsys->ctrls[cntlid] == SUBSYS_SLOT_RSVD);
49 subsys->ctrls[cntlid] = NULL;
50 sctrl->scid = 0;
55 int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp)
57 NvmeSubsystem *subsys = n->subsys;
58 NvmeSecCtrlEntry *sctrl = nvme_sctrl(n);
59 int cntlid, nsid, num_rsvd, num_vfs = n->params.sriov_max_vfs;
61 if (pci_is_vf(&n->parent_obj)) {
62 cntlid = le16_to_cpu(sctrl->scid);
63 } else {
64 n->sec_ctrl_list = g_new0(NvmeSecCtrlEntry, num_vfs);
66 for (cntlid = 0; cntlid < ARRAY_SIZE(subsys->ctrls); cntlid++) {
67 if (!subsys->ctrls[cntlid]) {
68 break;
72 if (cntlid == ARRAY_SIZE(subsys->ctrls)) {
73 error_setg(errp, "no more free controller id");
74 return -1;
77 num_rsvd = nvme_subsys_reserve_cntlids(n, cntlid + 1, num_vfs);
78 if (num_rsvd != num_vfs) {
79 nvme_subsys_unreserve_cntlids(n);
80 error_setg(errp,
81 "no more free controller ids for secondary controllers");
82 return -1;
86 if (!subsys->serial) {
87 subsys->serial = g_strdup(n->params.serial);
88 } else if (strcmp(subsys->serial, n->params.serial)) {
89 error_setg(errp, "invalid controller serial");
90 return -1;
93 subsys->ctrls[cntlid] = n;
95 for (nsid = 1; nsid < ARRAY_SIZE(subsys->namespaces); nsid++) {
96 NvmeNamespace *ns = subsys->namespaces[nsid];
97 if (ns && ns->params.shared && !ns->params.detached) {
98 nvme_attach_ns(n, ns);
102 return cntlid;
105 void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeCtrl *n)
107 if (pci_is_vf(&n->parent_obj)) {
108 subsys->ctrls[n->cntlid] = SUBSYS_SLOT_RSVD;
109 } else {
110 subsys->ctrls[n->cntlid] = NULL;
111 nvme_subsys_unreserve_cntlids(n);
114 n->cntlid = -1;
117 static bool nvme_calc_rgif(uint16_t nruh, uint16_t nrg, uint8_t *rgif)
119 uint16_t val;
120 unsigned int i;
122 if (unlikely(nrg == 1)) {
123 /* PIDRG_NORGI scenario, all of pid is used for PHID */
124 *rgif = 0;
125 return true;
128 val = nrg;
129 i = 0;
130 while (val) {
131 val >>= 1;
132 i++;
134 *rgif = i;
136 /* ensure remaining bits suffice to represent number of phids in a RG */
137 if (unlikely((UINT16_MAX >> i) < nruh)) {
138 *rgif = 0;
139 return false;
142 return true;
145 static bool nvme_subsys_setup_fdp(NvmeSubsystem *subsys, Error **errp)
147 NvmeEnduranceGroup *endgrp = &subsys->endgrp;
149 if (!subsys->params.fdp.runs) {
150 error_setg(errp, "fdp.runs must be non-zero");
151 return false;
154 endgrp->fdp.runs = subsys->params.fdp.runs;
156 if (!subsys->params.fdp.nrg) {
157 error_setg(errp, "fdp.nrg must be non-zero");
158 return false;
161 endgrp->fdp.nrg = subsys->params.fdp.nrg;
163 if (!subsys->params.fdp.nruh ||
164 subsys->params.fdp.nruh > NVME_FDP_MAXPIDS) {
165 error_setg(errp, "fdp.nruh must be non-zero and less than %u",
166 NVME_FDP_MAXPIDS);
167 return false;
170 endgrp->fdp.nruh = subsys->params.fdp.nruh;
172 if (!nvme_calc_rgif(endgrp->fdp.nruh, endgrp->fdp.nrg, &endgrp->fdp.rgif)) {
173 error_setg(errp,
174 "cannot derive a valid rgif (nruh %"PRIu16" nrg %"PRIu32")",
175 endgrp->fdp.nruh, endgrp->fdp.nrg);
176 return false;
179 endgrp->fdp.ruhs = g_new(NvmeRuHandle, endgrp->fdp.nruh);
181 for (uint16_t ruhid = 0; ruhid < endgrp->fdp.nruh; ruhid++) {
182 endgrp->fdp.ruhs[ruhid] = (NvmeRuHandle) {
183 .ruht = NVME_RUHT_INITIALLY_ISOLATED,
184 .ruha = NVME_RUHA_UNUSED,
187 endgrp->fdp.ruhs[ruhid].rus = g_new(NvmeReclaimUnit, endgrp->fdp.nrg);
190 endgrp->fdp.enabled = true;
192 return true;
195 static bool nvme_subsys_setup(NvmeSubsystem *subsys, Error **errp)
197 const char *nqn = subsys->params.nqn ?
198 subsys->params.nqn : subsys->parent_obj.id;
200 snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn),
201 "nqn.2019-08.org.qemu:%s", nqn);
203 if (subsys->params.fdp.enabled && !nvme_subsys_setup_fdp(subsys, errp)) {
204 return false;
207 return true;
210 static void nvme_subsys_realize(DeviceState *dev, Error **errp)
212 NvmeSubsystem *subsys = NVME_SUBSYS(dev);
214 qbus_init(&subsys->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id);
216 nvme_subsys_setup(subsys, errp);
219 static Property nvme_subsystem_props[] = {
220 DEFINE_PROP_STRING("nqn", NvmeSubsystem, params.nqn),
221 DEFINE_PROP_BOOL("fdp", NvmeSubsystem, params.fdp.enabled, false),
222 DEFINE_PROP_SIZE("fdp.runs", NvmeSubsystem, params.fdp.runs,
223 NVME_DEFAULT_RU_SIZE),
224 DEFINE_PROP_UINT32("fdp.nrg", NvmeSubsystem, params.fdp.nrg, 1),
225 DEFINE_PROP_UINT16("fdp.nruh", NvmeSubsystem, params.fdp.nruh, 0),
226 DEFINE_PROP_END_OF_LIST(),
229 static void nvme_subsys_class_init(ObjectClass *oc, void *data)
231 DeviceClass *dc = DEVICE_CLASS(oc);
233 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
235 dc->realize = nvme_subsys_realize;
236 dc->desc = "Virtual NVMe subsystem";
237 dc->hotpluggable = false;
239 device_class_set_props(dc, nvme_subsystem_props);
242 static const TypeInfo nvme_subsys_info = {
243 .name = TYPE_NVME_SUBSYS,
244 .parent = TYPE_DEVICE,
245 .class_init = nvme_subsys_class_init,
246 .instance_size = sizeof(NvmeSubsystem),
249 static void nvme_subsys_register_types(void)
251 type_register_static(&nvme_subsys_info);
254 type_init(nvme_subsys_register_types)