hw/block/nvme: support namespace attachment
cover hw/block/nvme: support namespace attachment
Hello,
This series supports namespace attachment: attach and detach. It means that this series also introduced a scheme for allocated namespace which is detached, but allocated in a NVMe subsystem. Given that now we have nvme-subsys device to specify a NVMe subsystem, it can manage detached namespaces from controllers in the subsystem itself.
Tested:
-device nvme-subsys,id=subsys0 \ -device nvme,serial=foo,id=nvme0,subsys=subsys0 \ -device nvme-ns,id=ns1,drive=drv0,nsid=1,subsys=subsys0,zoned=false \ -device nvme-ns,id=ns2,drive=drv1,nsid=2,subsys=subsys0,zoned=true \ -device nvme-ns,id=ns3,drive=drv2,nsid=3,subsys=subsys0,detached=true,zoned=false \ -device nvme-ns,id=ns4,drive=drv3,nsid=4,subsys=subsys0,detached=true,zoned=true \
root@vm:~# nvme list Node SN Model Namespace Usage Format FW Rev --------------------- -------------------- ---------------------------------------- --------- -------------------------- ---------------- -------- /dev/nvme0n1 foo QEMU NVMe Ctrl 1 268.44 MB / 268.44 MB 512 B + 0 B 1.0 /dev/nvme0n2 foo QEMU NVMe Ctrl 2 268.44 MB / 268.44 MB 512 B + 0 B 1.0
root@vm:~# nvme attach-ns /dev/nvme0 --namespace-id=3 --controllers=0 attach-ns: Success, nsid:3 root@vm:~# nvme attach-ns /dev/nvme0 --namespace-id=4 --controllers=0 attach-ns: Success, nsid:4 root@vm:~# echo 1 > /sys/class/nvme/nvme0/rescan_controller
root@vm:~# nvme list Node SN Model Namespace Usage Format FW Rev --------------------- -------------------- ---------------------------------------- --------- -------------------------- ---------------- -------- /dev/nvme0n1 foo QEMU NVMe Ctrl 1 268.44 MB / 268.44 MB 512 B + 0 B 1.0 /dev/nvme0n2 foo QEMU NVMe Ctrl 2 268.44 MB / 268.44 MB 512 B + 0 B 1.0 /dev/nvme0n3 foo QEMU NVMe Ctrl 3 268.44 MB / 268.44 MB 512 B + 0 B 1.0 /dev/nvme0n4 foo QEMU NVMe Ctrl 4 268.44 MB / 268.44 MB 512 B + 0 B 1.0
root@vm:~# nvme detach-ns /dev/nvme0 --namespace-id=3 --controllers=0 detach-ns: Success, nsid:3 root@vm:~# nvme detach-ns /dev/nvme0 --namespace-id=4 --controllers=0 detach-ns: Success, nsid:4 root@vm:~# echo 1 > /sys/class/nvme/nvme0/rescan_controller
root@vm:~# nvme list Node SN Model Namespace Usage Format FW Rev --------------------- -------------------- ---------------------------------------- --------- -------------------------- ---------------- -------- /dev/nvme0n1 foo QEMU NVMe Ctrl 1 268.44 MB / 268.44 MB 512 B + 0 B 1.0 /dev/nvme0n2 foo QEMU NVMe Ctrl 2 268.44 MB / 268.44 MB 512 B + 0 B 1.0
Thanks,
Minwoo Im (6):
hw/block/nvme: support namespace detach hw/block/nvme: fix namespaces array to 1-based hw/block/nvme: fix allocated namespace list to 256 hw/block/nvme: support allocated namespace type hw/block/nvme: refactor nvme_select_ns_iocs hw/block/nvme: support namespace attachment command
hw/block/nvme-ns.c | 1 + hw/block/nvme-ns.h | 1 + hw/block/nvme-subsys.h | 28 +++++- hw/block/nvme.c | 199 ++++++++++++++++++++++++++++++++++------- hw/block/nvme.h | 33 +++++++ hw/block/trace-events | 2 + include/block/nvme.h | 5 ++ 7 files changed, 234 insertions(+), 35 deletions(-)
Signature · 1 line
2.17.1
PATCH 2/6 hw/block/nvme: fix namespaces array to 1-based
subsys->namespaces array used to be sized to NVME_SUBSYS_MAX_NAMESPACES. But subsys->namespaces are being accessed with 1-based namespace id which means the very first array entry will always be empty(NULL).
--- hw/block/nvme-subsys.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/nvme-subsys.h +1 -1
@@ -24,7 +24,7 @@ typedef struct NvmeSubsystem {NvmeCtrl *ctrls[NVME_SUBSYS_MAX_CTRLS ];/* Allocated namespaces for this subsystem */ -NvmeNamespace *namespaces[NVME_SUBSYS_MAX_NAMESPACES ]; +NvmeNamespace *namespaces[NVME_SUBSYS_MAX_NAMESPACES +1 ]; } NvmeSubsystem;int nvme_subsys_register_ctrl (NvmeCtrl *n,Error **errp);
Signature · 1 line
2.17.1
PATCH 1/6 hw/block/nvme: support namespace detach
In reply to Minwoo ImGiven that now we have nvme-subsys device supported, we can manage namespace allocated, but not attached: detached. This patch introduced a parameter for nvme-ns device named 'detached'. This parameter indicates whether the given namespace device is detached from a entire NVMe subsystem('subsys' given case, shared namespace) or a controller('bus' given case, private namespace).
- Allocated namespace
1) Shared ns in the subsystem 'subsys0':
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,subsys=subsys0,detached=true
2) Private ns for the controller 'nvme0' of the subsystem 'subsys0':
-device nvme-subsys,id=subsys0
-device nvme,serial=foo,id=nvme0,subsys=subsys0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
3) (Invalid case) Controller 'nvme0' has no subsystem to manage ns:
-device nvme,serial=foo,id=nvme0
-device nvme-ns,id=ns1,drive=blknvme0,nsid=1,bus=nvme0,detached=true
--- hw/block/nvme-ns.c | 1 + hw/block/nvme-ns.h | 1 + hw/block/nvme-subsys.h | 1 + hw/block/nvme.c | 41 +++++++++++++++++++++++++++++++++++++++-- hw/block/nvme.h | 22 ++++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-)
hw/block/nvme-ns.c +1 -0
@@ -393,6 +393,7 @@ static Property nvme_ns_props[] = {DEFINE_BLOCK_PROPERTIES (NvmeNamespace, blkconf),DEFINE_PROP_LINK ("subsys" , NvmeNamespace, subsys,TYPE_NVME_SUBSYS , NvmeSubsystem *), +DEFINE_PROP_BOOL ("detached" , NvmeNamespace, params.detached, false),DEFINE_PROP_UINT32 ("nsid" , NvmeNamespace, params.nsid,0 ),DEFINE_PROP_UUID ("uuid" , NvmeNamespace, params.uuid), DEFINE_PROP_UINT16("mssrl", NvmeNamespace, params.mssrl, 128),
hw/block/nvme-ns.h +1 -0
@@ -26,6 +26,7 @@ typedef struct NvmeZone { } NvmeZone; typedef struct NvmeNamespaceParams { + bool detached; uint32_t nsid; QemuUUID uuid;
hw/block/nvme-subsys.h +1 -0
@@ -23,6 +23,7 @@ typedef struct NvmeSubsystem {uint8_t subnqn[256 ];NvmeCtrl *ctrls[NVME_SUBSYS_MAX_CTRLS ]; +/* Allocated namespaces for this subsystem */ NvmeNamespace *namespaces[NVME_SUBSYS_MAX_NAMESPACES ]; } NvmeSubsystem;
hw/block/nvme.c +39 -2
65 lines not on this page · raw message
hw/block/nvme.h +22 -0
@@ -176,6 +176,10 @@ typedef struct NvmeCtrl {NvmeSubsystem *subsys;NvmeNamespace namespace; +/* +* Attached namespaces to this controller. If subsys is not given, all +* namespaces in this list will always be attached. +*/ NvmeNamespace *namespaces[NVME_MAX_NAMESPACES ];NvmeSQueue **sq;NvmeCQueue **cq;
@@ -194,6 +198,24 @@ static inline NvmeNamespace *nvme_ns(NvmeCtrl *n, uint32_t nsid)return n->namespaces[nsid -1 ]; } +static inline bool nvme_ns_is_attached (NvmeCtrl *n,NvmeNamespace *ns) +{ +int nsid; + +for (nsid =1 ; nsid <= n->num_namespaces; nsid++) { +if (nvme_ns (n, nsid) == ns) { +return true; + } + } + +return false; +} + +static inline void nvme_ns_attach (NvmeCtrl *n,NvmeNamespace *ns) +{ + n->namespaces[nvme_nsid (ns) -1 ] = ns; +} +static inline NvmeCQueue *nvme_cq (NvmeRequest *req) {NvmeSQueue *sq = req->sq;
Signature · 1 line
2.17.1
PATCH 3/6 hw/block/nvme: fix allocated namespace list to 256
In reply to Minwoo ImExpand allocated namespace list (subsys->namespaces) to have 256 entries which is a value lager than at least NVME_MAX_NAMESPACES which is for attached namespace list in a controller.
Allocated namespace list should at least larger than attached namespace list.
n->num_namespaces = NVME_MAX_NAMESPACES;
The above line will set the NN field by id->nn so that the subsystem should also prepare at least this number of namespace list entries.
--- hw/block/nvme-subsys.h | 2 +- hw/block/nvme.h | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
hw/block/nvme-subsys.h +1 -1
@@ -14,7 +14,7 @@ OBJECT_CHECK(NvmeSubsystem, (obj), TYPE_NVME_SUBSYS)#define NVME_SUBSYS_MAX_CTRLS 32 -#define NVME_SUBSYS_MAX_NAMESPACES 32 +#define NVME_SUBSYS_MAX_NAMESPACES 256typedef struct NvmeCtrl NvmeCtrl ;typedef struct NvmeNamespace NvmeNamespace ;
hw/block/nvme.h +6 -0
@@ -10,6 +10,12 @@#define NVME_DEFAULT_ZONE_SIZE (128 * MiB)#define NVME_DEFAULT_MAX_ZA_SIZE (128 * KiB) +/* +* Subsystem namespace list for allocated namespaces should be larger than +* attached namespace list in a controller. +*/ +QEMU_BUILD_BUG_ON (NVME_MAX_NAMESPACES >NVME_SUBSYS_MAX_NAMESPACES ); + typedef struct NvmeParams { char *serial; uint32_t num_queues; /* deprecated since 5.1 */
Signature · 1 line
2.17.1
PATCH 4/6 hw/block/nvme: support allocated namespace type
In reply to Minwoo ImFrom NVMe spec 1.4b "6.1.5. NSID and Namespace Relationships" defines valid namespace types:
- Unallocated: Not exists in the NVMe subsystem
- Allocated: Exists in the NVMe subsystem
- Inactive: Not attached to the controller
- Active: Attached to the controller
This patch added support for allocated, but not attached namespace type:
!nvme_ns(n, nsid) && nvme_subsys_ns(n->subsys, nsid)
nvme_ns() returns attached namespace instance of the given controller and nvme_subsys_ns() returns allocated namespace instance in the subsystem.
--- hw/block/nvme-subsys.h | 13 +++++++++ hw/block/nvme.c | 63 +++++++++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 16 deletions(-)
hw/block/nvme-subsys.h +13 -0
@@ -30,4 +30,17 @@ typedef struct NvmeSubsystem {int nvme_subsys_register_ctrl (NvmeCtrl *n,Error **errp);int nvme_subsys_register_ns (NvmeNamespace *ns,Error **errp); +/* +* Return allocated namespace of the specified nsid in the subsystem. +*/ +static inline NvmeNamespace *nvme_subsys_ns (NvmeSubsystem *subsys, +uint32_t nsid) +{ +if (!subsys) { +return NULL ; + } + +return subsys->namespaces[nsid]; +} +#endif /* NVME_SUBSYS_H */
hw/block/nvme.c +47 -16
128 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 5/6 hw/block/nvme: refactor nvme_select_ns_iocs
In reply to Minwoo ImThis patch has no functional changes. This patch just refactored nvme_select_ns_iocs() to iterate the attached namespaces of the controlller and make it invoke __nvme_select_ns_iocs().
--- hw/block/nvme.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)
hw/block/nvme.c +21 -15
@@ -3896,6 +3896,25 @@ static void nvme_ctrl_shutdown(NvmeCtrl *n) } } +static void __nvme_select_ns_iocs (NvmeCtrl *n,NvmeNamespace *ns) +{ + ns->iocs = nvme_cse_iocs_none; +switch (ns->csi) { +case NVME_CSI_NVM : +if (NVME_CC_CSS (n->bar.cc) !=NVME_CC_CSS_ADMIN_ONLY ) { + ns->iocs = nvme_cse_iocs_nvm; + } +break ; +case NVME_CSI_ZONED : +if (NVME_CC_CSS (n->bar.cc) ==NVME_CC_CSS_CSI ) { + ns->iocs = nvme_cse_iocs_zoned; + }else if (NVME_CC_CSS (n->bar.cc) ==NVME_CC_CSS_NVM ) { + ns->iocs = nvme_cse_iocs_nvm; + } +break ; + } +} +static void nvme_select_ns_iocs (NvmeCtrl *n) {NvmeNamespace *ns;
@@ -3906,21 +3925,8 @@ static void nvme_select_ns_iocs(NvmeCtrl *n)if (!ns) {continue ; } - ns->iocs = nvme_cse_iocs_none; -switch (ns->csi) { -case NVME_CSI_NVM : -if (NVME_CC_CSS (n->bar.cc) !=NVME_CC_CSS_ADMIN_ONLY ) { - ns->iocs = nvme_cse_iocs_nvm; - } -break ; -case NVME_CSI_ZONED : -if (NVME_CC_CSS (n->bar.cc) ==NVME_CC_CSS_CSI ) { - ns->iocs = nvme_cse_iocs_zoned; - }else if (NVME_CC_CSS (n->bar.cc) ==NVME_CC_CSS_NVM ) { - ns->iocs = nvme_cse_iocs_nvm; - } -break ; - } + +__nvme_select_ns_iocs (n, ns); } }
Signature · 1 line
2.17.1
PATCH 6/6 hw/block/nvme: support namespace attachment command
In reply to Minwoo ImThis patch supports Namespace Attachment command for the pre-defined nvme-ns device nodes. Of course, attach/detach namespace should only be supported in case 'subsys' is given. This is because if we detach a namespace from a controller, somebody needs to manage the detached, but allocated namespace in the NVMe subsystem.
--- hw/block/nvme-subsys.h | 10 +++++++ hw/block/nvme.c | 59 ++++++++++++++++++++++++++++++++++++++++++ hw/block/nvme.h | 5 ++++ hw/block/trace-events | 2 ++ include/block/nvme.h | 5 ++++ 5 files changed, 81 insertions(+)
hw/block/nvme-subsys.h +10 -0
@@ -30,6 +30,16 @@ typedef struct NvmeSubsystem {int nvme_subsys_register_ctrl (NvmeCtrl *n,Error **errp);int nvme_subsys_register_ns (NvmeNamespace *ns,Error **errp); +static inline NvmeCtrl *nvme_subsys_ctrl (NvmeSubsystem *subsys, +uint32_t cntlid) +{ +if (!subsys) { +return NULL ; + } + +return subsys->ctrls[cntlid]; +} +/* * Return allocated namespace of the specified nsid in the subsystem. */
hw/block/nvme.c +59 -0
77 lines not on this page · raw message
hw/block/nvme.h +5 -0
@@ -222,6 +222,11 @@ static inline void nvme_ns_attach(NvmeCtrl *n, NvmeNamespace *ns) n->namespaces[nvme_nsid (ns) -1 ] = ns; } +static inline void nvme_ns_detach (NvmeCtrl *n,NvmeNamespace *ns) +{ + n->namespaces[nvme_nsid (ns) -1 ] =NULL ; +} +static inline NvmeCQueue *nvme_cq (NvmeRequest *req) {NvmeSQueue *sq = req->sq;
hw/block/trace-events +2 -0
@@ -80,6 +80,8 @@ pci_nvme_aer(uint16_t cid) "cid %"PRIu16"" pci_nvme_aer_aerl_exceeded(void) "aerl exceeded" pci_nvme_aer_masked(uint8_t type, uint8_t mask) "type 0x%"PRIx8" mask 0x%"PRIx8"" pci_nvme_aer_post_cqe(uint8_t typ, uint8_t info, uint8_t log_page) "type 0x%"PRIx8" info 0x%"PRIx8" lid 0x%"PRIx8"" +pci_nvme_ns_attachment(uint16_t cid, uint8_t sel) "cid %"PRIu16", sel=0x%"PRIx8"" +pci_nvme_ns_attachment_attach(uint16_t cntlid, uint32_t nsid) "cntlid=0x%"PRIx16", nsid=0x%"PRIx32"" pci_nvme_enqueue_event(uint8_t typ, uint8_t info, uint8_t log_page) "type 0x%"PRIx8" info 0x%"PRIx8" lid 0x%"PRIx8"" pci_nvme_enqueue_event_noqueue(int queued) "queued %d" pci_nvme_enqueue_event_masked(uint8_t typ) "type 0x%"PRIx8""
include/block/nvme.h +5 -0
@@ -566,6 +566,7 @@ enum NvmeAdminCommands {NVME_ADM_CMD_ASYNC_EV_REQ =0x0c ,NVME_ADM_CMD_ACTIVATE_FW =0x10 ,NVME_ADM_CMD_DOWNLOAD_FW =0x11 , +NVME_ADM_CMD_NS_ATTACHMENT =0x15 ,NVME_ADM_CMD_FORMAT_NVM =0x80 ,NVME_ADM_CMD_SECURITY_SEND =0x81 , NVME_ADM_CMD_SECURITY_RECV = 0x82,
@@ -836,6 +837,9 @@ enum NvmeStatusCodes {NVME_FEAT_NOT_CHANGEABLE =0x010e ,NVME_FEAT_NOT_NS_SPEC =0x010f ,NVME_FW_REQ_SUSYSTEM_RESET =0x0110 , +NVME_NS_ALREADY_ATTACHED =0x0118 , +NVME_NS_NOT_ATTACHED =0x011A , +NVME_NS_CTRL_LIST_INVALID =0x011C ,NVME_CONFLICTING_ATTRS =0x0180 ,NVME_INVALID_PROT_INFO =0x0181 , NVME_WRITE_TO_RO = 0x0182,
@@ -951,6 +955,7 @@ typedef struct QEMU_PACKED NvmePSD {uint8_t resv[16 ]; } NvmePSD; +#define NVME_CONTROLLER_LIST_SIZE 2048#define NVME_IDENTIFY_DATA_SIZE 4096enum NvmeIdCns {
Signature · 1 line
2.17.1