Skip to content

Commit cd59f50

Browse files
committed
hw/nvme: always initialize a subsystem
If no nvme-subsys is explicitly configured, instantiate one. Reviewed-by: Jesper Wendel Devantier <[email protected]> Signed-off-by: Klaus Jensen <[email protected]>
1 parent 23a4b3e commit cd59f50

File tree

2 files changed

+42
-58
lines changed

2 files changed

+42
-58
lines changed

hw/nvme/ctrl.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8823,15 +8823,13 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)
88238823
id->psd[0].enlat = cpu_to_le32(0x10);
88248824
id->psd[0].exlat = cpu_to_le32(0x4);
88258825

8826-
if (n->subsys) {
8827-
id->cmic |= NVME_CMIC_MULTI_CTRL;
8828-
ctratt |= NVME_CTRATT_ENDGRPS;
8826+
id->cmic |= NVME_CMIC_MULTI_CTRL;
8827+
ctratt |= NVME_CTRATT_ENDGRPS;
88298828

8830-
id->endgidmax = cpu_to_le16(0x1);
8829+
id->endgidmax = cpu_to_le16(0x1);
88318830

8832-
if (n->subsys->endgrp.fdp.enabled) {
8833-
ctratt |= NVME_CTRATT_FDPS;
8834-
}
8831+
if (n->subsys->endgrp.fdp.enabled) {
8832+
ctratt |= NVME_CTRATT_FDPS;
88358833
}
88368834

88378835
id->ctratt = cpu_to_le32(ctratt);
@@ -8860,7 +8858,15 @@ static int nvme_init_subsys(NvmeCtrl *n, Error **errp)
88608858
int cntlid;
88618859

88628860
if (!n->subsys) {
8863-
return 0;
8861+
DeviceState *dev = qdev_new(TYPE_NVME_SUBSYS);
8862+
8863+
qdev_prop_set_string(dev, "nqn", n->params.serial);
8864+
8865+
if (!qdev_realize(dev, NULL, errp)) {
8866+
return -1;
8867+
}
8868+
8869+
n->subsys = NVME_SUBSYS(dev);
88648870
}
88658871

88668872
cntlid = nvme_subsys_register_ctrl(n, errp);
@@ -8950,17 +8956,15 @@ static void nvme_exit(PCIDevice *pci_dev)
89508956

89518957
nvme_ctrl_reset(n, NVME_RESET_FUNCTION);
89528958

8953-
if (n->subsys) {
8954-
for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
8955-
ns = nvme_ns(n, i);
8956-
if (ns) {
8957-
ns->attached--;
8958-
}
8959+
for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
8960+
ns = nvme_ns(n, i);
8961+
if (ns) {
8962+
ns->attached--;
89598963
}
8960-
8961-
nvme_subsys_unregister_ctrl(n->subsys, n);
89628964
}
89638965

8966+
nvme_subsys_unregister_ctrl(n->subsys, n);
8967+
89648968
g_free(n->cq);
89658969
g_free(n->sq);
89668970
g_free(n->aer_reqs);

hw/nvme/ns.c

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -727,33 +727,22 @@ static void nvme_ns_realize(DeviceState *dev, Error **errp)
727727
uint32_t nsid = ns->params.nsid;
728728
int i;
729729

730-
if (!n->subsys) {
731-
/* If no subsys, the ns cannot be attached to more than one ctrl. */
732-
ns->params.shared = false;
733-
if (ns->params.detached) {
734-
error_setg(errp, "detached requires that the nvme device is "
735-
"linked to an nvme-subsys device");
736-
return;
737-
}
738-
} else {
739-
/*
740-
* If this namespace belongs to a subsystem (through a link on the
741-
* controller device), reparent the device.
742-
*/
743-
if (!qdev_set_parent_bus(dev, &subsys->bus.parent_bus, errp)) {
744-
return;
745-
}
746-
ns->subsys = subsys;
747-
ns->endgrp = &subsys->endgrp;
730+
assert(subsys);
731+
732+
/* reparent to subsystem bus */
733+
if (!qdev_set_parent_bus(dev, &subsys->bus.parent_bus, errp)) {
734+
return;
748735
}
736+
ns->subsys = subsys;
737+
ns->endgrp = &subsys->endgrp;
749738

750739
if (nvme_ns_setup(ns, errp)) {
751740
return;
752741
}
753742

754743
if (!nsid) {
755744
for (i = 1; i <= NVME_MAX_NAMESPACES; i++) {
756-
if (nvme_ns(n, i) || nvme_subsys_ns(subsys, i)) {
745+
if (nvme_subsys_ns(subsys, i)) {
757746
continue;
758747
}
759748

@@ -765,38 +754,29 @@ static void nvme_ns_realize(DeviceState *dev, Error **errp)
765754
error_setg(errp, "no free namespace id");
766755
return;
767756
}
768-
} else {
769-
if (nvme_ns(n, nsid) || nvme_subsys_ns(subsys, nsid)) {
770-
error_setg(errp, "namespace id '%d' already allocated", nsid);
771-
return;
772-
}
757+
} else if (nvme_subsys_ns(subsys, nsid)) {
758+
error_setg(errp, "namespace id '%d' already allocated", nsid);
759+
return;
773760
}
774761

775-
if (subsys) {
776-
subsys->namespaces[nsid] = ns;
762+
subsys->namespaces[nsid] = ns;
777763

778-
ns->id_ns.endgid = cpu_to_le16(0x1);
779-
ns->id_ns_ind.endgrpid = cpu_to_le16(0x1);
764+
ns->id_ns.endgid = cpu_to_le16(0x1);
765+
ns->id_ns_ind.endgrpid = cpu_to_le16(0x1);
780766

781-
if (ns->params.detached) {
782-
return;
783-
}
767+
if (ns->params.detached) {
768+
return;
769+
}
784770

785-
if (ns->params.shared) {
786-
for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
787-
NvmeCtrl *ctrl = subsys->ctrls[i];
771+
if (ns->params.shared) {
772+
for (i = 0; i < ARRAY_SIZE(subsys->ctrls); i++) {
773+
NvmeCtrl *ctrl = subsys->ctrls[i];
788774

789-
if (ctrl && ctrl != SUBSYS_SLOT_RSVD) {
790-
nvme_attach_ns(ctrl, ns);
791-
}
775+
if (ctrl && ctrl != SUBSYS_SLOT_RSVD) {
776+
nvme_attach_ns(ctrl, ns);
792777
}
793-
794-
return;
795778
}
796-
797779
}
798-
799-
nvme_attach_ns(n, ns);
800780
}
801781

802782
static const Property nvme_ns_props[] = {

0 commit comments

Comments
 (0)