2021-01-24 05:54:45 +03:00
|
|
|
/*
|
|
|
|
* QEMU NVM Express Subsystem: nvme-subsys
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Minwoo Im <minwoo.im.dev@gmail.com>
|
|
|
|
*
|
|
|
|
* This code is licensed under the GNU GPL v2. Refer COPYING.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2023-02-20 14:59:26 +03:00
|
|
|
#include "qemu/units.h"
|
2021-01-24 05:54:45 +03:00
|
|
|
#include "qapi/error.h"
|
2021-04-14 21:41:20 +03:00
|
|
|
|
2021-01-24 05:54:45 +03:00
|
|
|
#include "nvme.h"
|
|
|
|
|
2023-02-20 14:59:26 +03:00
|
|
|
#define NVME_DEFAULT_RU_SIZE (96 * MiB)
|
|
|
|
|
2022-05-09 17:16:11 +03:00
|
|
|
static int nvme_subsys_reserve_cntlids(NvmeCtrl *n, int start, int num)
|
2021-01-24 05:54:48 +03:00
|
|
|
{
|
|
|
|
NvmeSubsystem *subsys = n->subsys;
|
2022-05-09 17:16:11 +03:00
|
|
|
NvmeSecCtrlList *list = &n->sec_ctrl_list;
|
|
|
|
NvmeSecCtrlEntry *sctrl;
|
|
|
|
int i, cnt = 0;
|
2021-01-24 05:54:48 +03:00
|
|
|
|
2022-05-09 17:16:11 +03:00
|
|
|
for (i = start; i < ARRAY_SIZE(subsys->ctrls) && cnt < num; i++) {
|
|
|
|
if (!subsys->ctrls[i]) {
|
|
|
|
sctrl = &list->sec[cnt];
|
|
|
|
sctrl->scid = cpu_to_le16(i);
|
|
|
|
subsys->ctrls[i] = SUBSYS_SLOT_RSVD;
|
|
|
|
cnt++;
|
2021-01-24 05:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 17:16:11 +03:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_subsys_unreserve_cntlids(NvmeCtrl *n)
|
|
|
|
{
|
|
|
|
NvmeSubsystem *subsys = n->subsys;
|
|
|
|
NvmeSecCtrlList *list = &n->sec_ctrl_list;
|
|
|
|
NvmeSecCtrlEntry *sctrl;
|
|
|
|
int i, cntlid;
|
|
|
|
|
|
|
|
for (i = 0; i < n->params.sriov_max_vfs; i++) {
|
|
|
|
sctrl = &list->sec[i];
|
|
|
|
cntlid = le16_to_cpu(sctrl->scid);
|
|
|
|
|
|
|
|
if (cntlid) {
|
|
|
|
assert(subsys->ctrls[cntlid] == SUBSYS_SLOT_RSVD);
|
|
|
|
subsys->ctrls[cntlid] = NULL;
|
|
|
|
sctrl->scid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp)
|
|
|
|
{
|
|
|
|
NvmeSubsystem *subsys = n->subsys;
|
|
|
|
NvmeSecCtrlEntry *sctrl = nvme_sctrl(n);
|
|
|
|
int cntlid, nsid, num_rsvd, num_vfs = n->params.sriov_max_vfs;
|
|
|
|
|
|
|
|
if (pci_is_vf(&n->parent_obj)) {
|
|
|
|
cntlid = le16_to_cpu(sctrl->scid);
|
|
|
|
} else {
|
|
|
|
for (cntlid = 0; cntlid < ARRAY_SIZE(subsys->ctrls); cntlid++) {
|
|
|
|
if (!subsys->ctrls[cntlid]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cntlid == ARRAY_SIZE(subsys->ctrls)) {
|
|
|
|
error_setg(errp, "no more free controller id");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_rsvd = nvme_subsys_reserve_cntlids(n, cntlid + 1, num_vfs);
|
|
|
|
if (num_rsvd != num_vfs) {
|
|
|
|
nvme_subsys_unreserve_cntlids(n);
|
|
|
|
error_setg(errp,
|
|
|
|
"no more free controller ids for secondary controllers");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-24 05:54:48 +03:00
|
|
|
}
|
|
|
|
|
2022-04-29 11:33:32 +03:00
|
|
|
if (!subsys->serial) {
|
|
|
|
subsys->serial = g_strdup(n->params.serial);
|
|
|
|
} else if (strcmp(subsys->serial, n->params.serial)) {
|
|
|
|
error_setg(errp, "invalid controller serial");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-24 05:54:48 +03:00
|
|
|
subsys->ctrls[cntlid] = n;
|
|
|
|
|
2021-09-09 12:43:08 +03:00
|
|
|
for (nsid = 1; nsid < ARRAY_SIZE(subsys->namespaces); nsid++) {
|
|
|
|
NvmeNamespace *ns = subsys->namespaces[nsid];
|
|
|
|
if (ns && ns->params.shared && !ns->params.detached) {
|
|
|
|
nvme_attach_ns(n, ns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 05:54:48 +03:00
|
|
|
return cntlid;
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:51:36 +03:00
|
|
|
void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeCtrl *n)
|
|
|
|
{
|
2022-05-09 17:16:11 +03:00
|
|
|
if (pci_is_vf(&n->parent_obj)) {
|
|
|
|
subsys->ctrls[n->cntlid] = SUBSYS_SLOT_RSVD;
|
|
|
|
} else {
|
|
|
|
subsys->ctrls[n->cntlid] = NULL;
|
|
|
|
nvme_subsys_unreserve_cntlids(n);
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:43:08 +03:00
|
|
|
n->cntlid = -1;
|
2021-07-06 11:51:36 +03:00
|
|
|
}
|
|
|
|
|
2023-02-20 14:59:26 +03:00
|
|
|
static bool nvme_calc_rgif(uint16_t nruh, uint16_t nrg, uint8_t *rgif)
|
|
|
|
{
|
|
|
|
uint16_t val;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (unlikely(nrg == 1)) {
|
|
|
|
/* PIDRG_NORGI scenario, all of pid is used for PHID */
|
|
|
|
*rgif = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = nrg;
|
|
|
|
i = 0;
|
|
|
|
while (val) {
|
|
|
|
val >>= 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
*rgif = i;
|
|
|
|
|
|
|
|
/* ensure remaining bits suffice to represent number of phids in a RG */
|
|
|
|
if (unlikely((UINT16_MAX >> i) < nruh)) {
|
|
|
|
*rgif = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nvme_subsys_setup_fdp(NvmeSubsystem *subsys, Error **errp)
|
|
|
|
{
|
|
|
|
NvmeEnduranceGroup *endgrp = &subsys->endgrp;
|
|
|
|
|
|
|
|
if (!subsys->params.fdp.runs) {
|
|
|
|
error_setg(errp, "fdp.runs must be non-zero");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
endgrp->fdp.runs = subsys->params.fdp.runs;
|
|
|
|
|
|
|
|
if (!subsys->params.fdp.nrg) {
|
|
|
|
error_setg(errp, "fdp.nrg must be non-zero");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
endgrp->fdp.nrg = subsys->params.fdp.nrg;
|
|
|
|
|
|
|
|
if (!subsys->params.fdp.nruh) {
|
|
|
|
error_setg(errp, "fdp.nruh must be non-zero");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
endgrp->fdp.nruh = subsys->params.fdp.nruh;
|
|
|
|
|
|
|
|
if (!nvme_calc_rgif(endgrp->fdp.nruh, endgrp->fdp.nrg, &endgrp->fdp.rgif)) {
|
|
|
|
error_setg(errp,
|
|
|
|
"cannot derive a valid rgif (nruh %"PRIu16" nrg %"PRIu32")",
|
|
|
|
endgrp->fdp.nruh, endgrp->fdp.nrg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
endgrp->fdp.ruhs = g_new(NvmeRuHandle, endgrp->fdp.nruh);
|
|
|
|
|
|
|
|
for (uint16_t ruhid = 0; ruhid < endgrp->fdp.nruh; ruhid++) {
|
|
|
|
endgrp->fdp.ruhs[ruhid] = (NvmeRuHandle) {
|
|
|
|
.ruht = NVME_RUHT_INITIALLY_ISOLATED,
|
|
|
|
.ruha = NVME_RUHA_UNUSED,
|
|
|
|
};
|
|
|
|
|
|
|
|
endgrp->fdp.ruhs[ruhid].rus = g_new(NvmeReclaimUnit, endgrp->fdp.nrg);
|
|
|
|
}
|
|
|
|
|
|
|
|
endgrp->fdp.enabled = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nvme_subsys_setup(NvmeSubsystem *subsys, Error **errp)
|
2021-01-24 05:54:45 +03:00
|
|
|
{
|
|
|
|
const char *nqn = subsys->params.nqn ?
|
|
|
|
subsys->params.nqn : subsys->parent_obj.id;
|
|
|
|
|
|
|
|
snprintf((char *)subsys->subnqn, sizeof(subsys->subnqn),
|
|
|
|
"nqn.2019-08.org.qemu:%s", nqn);
|
2023-02-20 14:59:26 +03:00
|
|
|
|
|
|
|
if (subsys->params.fdp.enabled && !nvme_subsys_setup_fdp(subsys, errp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-01-24 05:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nvme_subsys_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
NvmeSubsystem *subsys = NVME_SUBSYS(dev);
|
|
|
|
|
2021-09-23 15:11:51 +03:00
|
|
|
qbus_init(&subsys->bus, sizeof(NvmeBus), TYPE_NVME_BUS, dev, dev->id);
|
2021-04-23 19:55:11 +03:00
|
|
|
|
2023-02-20 14:59:26 +03:00
|
|
|
nvme_subsys_setup(subsys, errp);
|
2021-01-24 05:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Property nvme_subsystem_props[] = {
|
|
|
|
DEFINE_PROP_STRING("nqn", NvmeSubsystem, params.nqn),
|
2023-02-20 14:59:26 +03:00
|
|
|
DEFINE_PROP_BOOL("fdp", NvmeSubsystem, params.fdp.enabled, false),
|
|
|
|
DEFINE_PROP_SIZE("fdp.runs", NvmeSubsystem, params.fdp.runs,
|
|
|
|
NVME_DEFAULT_RU_SIZE),
|
|
|
|
DEFINE_PROP_UINT32("fdp.nrg", NvmeSubsystem, params.fdp.nrg, 1),
|
|
|
|
DEFINE_PROP_UINT16("fdp.nruh", NvmeSubsystem, params.fdp.nruh, 0),
|
2021-01-24 05:54:45 +03:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void nvme_subsys_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
|
|
|
|
set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
|
|
|
|
|
|
|
|
dc->realize = nvme_subsys_realize;
|
|
|
|
dc->desc = "Virtual NVMe subsystem";
|
2021-07-06 11:48:40 +03:00
|
|
|
dc->hotpluggable = false;
|
2021-01-24 05:54:45 +03:00
|
|
|
|
|
|
|
device_class_set_props(dc, nvme_subsystem_props);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo nvme_subsys_info = {
|
|
|
|
.name = TYPE_NVME_SUBSYS,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.class_init = nvme_subsys_class_init,
|
|
|
|
.instance_size = sizeof(NvmeSubsystem),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void nvme_subsys_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&nvme_subsys_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(nvme_subsys_register_types)
|