hw/block/nvme: misc cmb/pmr patches
cover hw/block/nvme: misc cmb/pmr patches
From: Klaus Jensen <[email protected]>
This is a resend of "hw/block/nvme: allow cmb and pmr to coexist" with some more PMR work added (PMR RDS/WDS support).
This includes a resurrection of Andrzej's series[1] from back July.
Andrzej's main patch basically moved the the CMB from BAR 2 into an offset in BAR 4 (located after the MSI-X table and PBA). Having an offset on the CMB causes a bunch of calculations related to address mapping to change.
So, since I couldn't get the patch to apply cleanly I took a stab at implementing the suggestion I originally came up with: simply move the MSI-X table and PBA from BAR 4 into BAR 0 (up-aligned to a 4 KiB boundary, after the main NVMe controller registers). This way we can keep the CMB at offset zero in its own BAR and free up BAR 4 for use by PMR. This makes the patch simpler and does not impact any of the existing address mapping code.
Andrzej, I would prefer an Ack from you, since I pretty much voided your main patch.
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
Andrzej Jakowski (1):
hw/block/nvme: indicate CMB support through controller capabilities
register
Klaus Jensen (6):
hw/block/nvme: move msix table and pba to BAR 0 hw/block/nvme: allow cmb and pmr to coexist hw/block/nvme: fix controller reset/shutdown logic hw/block/nvme: rename CAP_PMR_{SHIFT,MASK} to CAP_PMRS_{SHIFT,MASK} hw/block/nvme: remove redundant zeroing of PMR registers hw/block/nvme: disable PMR at boot up
Naveen Nagar (1):
hw/block/nvme: add PMR RDS/WDS support
hw/block/nvme.h | 2 + include/block/nvme.h | 15 ++- hw/block/nvme.c | 216 ++++++++++++++++++++++++++++++------------- 3 files changed, 164 insertions(+), 69 deletions(-)
Signature · 1 line
2.29.2
PATCH 1/8 hw/block/nvme: indicate CMB support through controller capabilities register
From: Andrzej Jakowski <[email protected]>
This patch sets CMBS bit in controller capabilities register when user configures NVMe driver with CMB support, so capabilites are correctly reported to guest OS.
--- include/block/nvme.h | 10 +++++++--- hw/block/nvme.c | 1 + 2 files changed, 8 insertions(+), 3 deletions(-)
include/block/nvme.h +7 -3
@@ -36,6 +36,7 @@ enum NvmeCapShift {CAP_MPSMIN_SHIFT =48 ,CAP_MPSMAX_SHIFT =52 ,CAP_PMR_SHIFT =56 , + CAP_CMB_SHIFT = 57, }; enum NvmeCapMask {
@@ -49,6 +50,7 @@ enum NvmeCapMask {CAP_MPSMIN_MASK =0xf ,CAP_MPSMAX_MASK =0xf ,CAP_PMR_MASK =0x1 , + CAP_CMB_MASK = 0x1, };#define NVME_CAP_MQES (cap) (((cap) >> CAP_MQES_SHIFT) & CAP_MQES_MASK)
@@ -78,9 +80,11 @@ enum NvmeCapMask {#define NVME_CAP_SET_MPSMIN (cap, val) (cap |= (uint64_t)(val & CAP_MPSMIN_MASK)\ << CAP_MPSMIN_SHIFT)#define NVME_CAP_SET_MPSMAX (cap, val) (cap |= (uint64_t)(val & CAP_MPSMAX_MASK)\ - << CAP_MPSMAX_SHIFT) -#define NVME_CAP_SET_PMRS (cap, val) (cap |= (uint64_t)(val & CAP_PMR_MASK)\ - << CAP_PMR_SHIFT) + << CAP_MPSMAX_SHIFT) +#define NVME_CAP_SET_PMRS (cap, val) (cap |= (uint64_t)(val & CAP_PMR_MASK) \ + << CAP_PMR_SHIFT) +#define NVME_CAP_SET_CMBS (cap, val) (cap |= (uint64_t)(val & CAP_CMB_MASK) \ + << CAP_CMB_SHIFT)enum NvmeCapCss {NVME_CAP_CSS_NVM =1 <<0 ,
hw/block/nvme.c +1 -0
@@ -3046,6 +3046,7 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)NVME_CAP_SET_CSS (n->bar.cap,NVME_CAP_CSS_NVM );NVME_CAP_SET_CSS (n->bar.cap,NVME_CAP_CSS_ADMIN_ONLY );NVME_CAP_SET_MPSMAX (n->bar.cap,4 ); +NVME_CAP_SET_CMBS (n->bar.cap, n->params.cmb_size_mb ?1 :0 ); n->bar.vs =NVME_SPEC_VER ; n->bar.intmc = n->bar.intms =0 ;
Signature · 1 line
2.29.2
PATCH 2/8 hw/block/nvme: move msix table and pba to BAR 0
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
In the interest of supporting both CMB and PMR to be enabled on the same device, move the MSI-X table and pending bit out of BAR 4 and into BAR 0.
This is a simplified version of the patch contributed by Andrzej Jakowski (see [1]). Leaving the CMB at offset 0 removes the need for changes to CMB address mapping code.
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
--- hw/block/nvme.h | 1 + hw/block/nvme.c | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-)
hw/block/nvme.h +1 -0
@@ -118,6 +118,7 @@ typedef struct NvmeFeatureVal { typedef struct NvmeCtrl { PCIDevice parent_obj; + MemoryRegion bar0; MemoryRegion iomem; MemoryRegion ctrl_mem; NvmeBar bar;
hw/block/nvme.c +22 -2
@@ -2952,6 +2952,8 @@ static void nvme_init_pmr(NvmeCtrl *n, PCIDevice *pci_dev)static void nvme_init_pci (NvmeCtrl *n,PCIDevice *pci_dev,Error **errp) {uint8_t *pci_conf = pci_dev->config; +uint64_t bar_size, msix_table_size, msix_pba_size; +unsigned msix_table_offset, msix_pba_offset; pci_conf[PCI_INTERRUPT_PIN ] =1 ;pci_config_set_prog_interface (pci_conf,0x2 );
@@ -2967,11 +2969,29 @@ static void nvme_init_pci(NvmeCtrl *n, PCIDevice *pci_dev, Error **errp)pci_config_set_class (pci_conf,PCI_CLASS_STORAGE_EXPRESS );pcie_endpoint_cap_init (pci_dev,0x80 ); + bar_size =QEMU_ALIGN_UP (n->reg_size,4 * KiB); + msix_table_offset = bar_size; + msix_table_size =PCI_MSIX_ENTRY_SIZE * n->params.msix_qsize; + + bar_size += msix_table_size; + bar_size =QEMU_ALIGN_UP (bar_size,4 * KiB); + msix_pba_offset = bar_size; + msix_pba_size =QEMU_ALIGN_UP (n->params.msix_qsize,64 ) /8 ; + + bar_size += msix_pba_size; + bar_size =pow2ceil (bar_size); + +memory_region_init (&n->bar0,OBJECT (n),"nvme-bar0" , bar_size);memory_region_init_io (&n->iomem,OBJECT (n), &nvme_mmio_ops, n,"nvme" , n->reg_size); +memory_region_add_subregion (&n->bar0,0 , &n->iomem); +pci_register_bar (pci_dev,0 ,PCI_BASE_ADDRESS_SPACE_MEMORY | -PCI_BASE_ADDRESS_MEM_TYPE_64 , &n->iomem); -if (msix_init_exclusive_bar (pci_dev, n->params.msix_qsize,4 , errp)) { +PCI_BASE_ADDRESS_MEM_TYPE_64 , &n->bar0); + +if (msix_init (pci_dev, n->params.msix_qsize, + &n->bar0,0 , msix_table_offset, + &n->bar0,0 , msix_pba_offset,0 , errp)) {return ; }
Signature · 1 line
2.29.2
PATCH 3/8 hw/block/nvme: allow cmb and pmr to coexist
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
With BAR 4 now free to use, allow PMR and CMB to be enabled simultaneously.
--- hw/block/nvme.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
hw/block/nvme.c +8 -9
@@ -28,14 +28,13 @@* Note cmb_size_mb denotes size of CMB in MB. CMB is assumed to be at * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. * -* cmb_size_mb= and pmrdev= options are mutually exclusive due to limitation -* in available BAR's. cmb_size_mb= will take precedence over pmrdev= when -* both provided. * Enabling pmr emulation can be achieved by pointing to memory-backend-file. * For example: * -object memory-backend-file,id=<mem_id>,share=on,mem-path=<file_path>, \ * size=<size> .... -device nvme,...,pmrdev=<mem_id> * +* The PMR will use BAR 4/5 exclusively. +* * * nvme device parameters * ~~~~~~~~~~~~~~~~~~~~~~
@@ -76,7 +75,7 @@#define NVME_DB_SIZE 4#define NVME_SPEC_VER 0x00010300#define NVME_CMB_BIR 2 -#define NVME_PMR_BIR 2 +#define NVME_PMR_BIR 4#define NVME_TEMPERATURE 0x143#define NVME_TEMPERATURE_WARNING 0x157#define NVME_TEMPERATURE_CRITICAL 0x175
@@ -2812,7 +2811,7 @@ static void nvme_check_constraints(NvmeCtrl *n, Error **errp)return ; } -if (!n->params.cmb_size_mb && n->pmrdev) { +if (n->pmrdev) {if (host_memory_backend_is_mapped (n->pmrdev)) {error_setg (errp,"can't use already busy memdev: %s" ,object_get_canonical_path_component (OBJECT (n->pmrdev)));
@@ -2902,9 +2901,6 @@ static void nvme_init_cmb(NvmeCtrl *n, PCIDevice *pci_dev)static void nvme_init_pmr (NvmeCtrl *n,PCIDevice *pci_dev) { -/* Controller Capabilities register */ -NVME_CAP_SET_PMRS (n->bar.cap,1 ); -/* PMR Capabities register */ n->bar.pmrcap =0 ;NVME_PMRCAP_SET_RDS (n->bar.pmrcap,0 );
@@ -2997,7 +2993,9 @@ static void nvme_init_pci(NvmeCtrl *n, PCIDevice *pci_dev, Error **errp)if (n->params.cmb_size_mb) {nvme_init_cmb (n, pci_dev); - }else if (n->pmrdev) { + } + +if (n->pmrdev) {nvme_init_pmr (n, pci_dev); } }
@@ -3067,6 +3065,7 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)NVME_CAP_SET_CSS (n->bar.cap,NVME_CAP_CSS_ADMIN_ONLY );NVME_CAP_SET_MPSMAX (n->bar.cap,4 );NVME_CAP_SET_CMBS (n->bar.cap, n->params.cmb_size_mb ?1 :0 ); +NVME_CAP_SET_PMRS (n->bar.cap, n->pmrdev ?1 :0 ); n->bar.vs =NVME_SPEC_VER ; n->bar.intmc = n->bar.intms =0 ;
Signature · 1 line
2.29.2
PATCH 4/8 hw/block/nvme: fix controller reset/shutdown logic
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
Differentiate between Controller Level Reset (i.e. CC.EN transitions from 1 to 0) and Controller Shutdown (i.e. host sets CC.SHN to 01b or 10b). Prior to this patch the controller would perform the equivalent of a Controller Level Reset when the host set CC.SHN to 01b or 10b. This erroneously entails clearing CC.
For a Shutdown, it is the host that *should* delete any I/O queues. Setting CC.EN to 0 is not part of the Shutdown procedure and it is specifically recommended that the host does NOT do this. The host should also stop issuing new I/O commands and allow any outstanding ones to complete before setting CC.SHN. The only guarantee the controller must give is that it is safe to power off the controller when CSTS.SHST indicates that shutdown processing is complete. Do so by issuing flushes for the attached namespaces and the PMR (if in use) and set CSTS.SHST to 10b. Specifically do not care about draining since the host *should* not have left any outstanding commands around. If it did, behavior is undefined.
On the other hand, a Controller Level Reset is not about being safe for shutdown - so only drain here, do not explicitly flush.
--- hw/block/nvme.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-)
hw/block/nvme.c +24 -13
@@ -2262,7 +2262,26 @@ static void nvme_process_sq(void *opaque) } } -static void nvme_clear_ctrl (NvmeCtrl *n) +static void nvme_ctrl_shutdown (NvmeCtrl *n) +{ +NvmeNamespace *ns; +int i; + +if (n->pmrdev) { +memory_region_msync (&n->pmrdev->mr,0 , n->pmrdev->size); + } + +for (i =1 ; i <= n->num_namespaces; i++) { + ns =nvme_ns (n, i); +if (!ns) { +continue ; + } + +nvme_ns_flush (ns); + } +} + +static void nvme_ctrl_reset (NvmeCtrl *n) {NvmeNamespace *ns;int i;
@@ -2297,15 +2316,6 @@ static void nvme_clear_ctrl(NvmeCtrl *n) n->outstanding_aers =0 ; n->qs_created = false; -for (i =1 ; i <= n->num_namespaces; i++) { - ns =nvme_ns (n, i); -if (!ns) { -continue ; - } - -nvme_ns_flush (ns); - } - n->bar.cc =0 ; }
@@ -2473,12 +2483,12 @@ static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data, } }else if (!NVME_CC_EN (data) &&NVME_CC_EN (n->bar.cc)) {trace_pci_nvme_mmio_stopped (); -nvme_clear_ctrl (n); +nvme_ctrl_reset (n); n->bar.csts &= ~NVME_CSTS_READY ; }if (NVME_CC_SHN (data) && !(NVME_CC_SHN (n->bar.cc))) {trace_pci_nvme_mmio_shutdown_set (); -nvme_clear_ctrl (n); +nvme_ctrl_shutdown (n); n->bar.cc = data; n->bar.csts |=NVME_CSTS_SHST_COMPLETE ; }else if (!NVME_CC_SHN (data) &&NVME_CC_SHN (n->bar.cc)) {
@@ -3110,7 +3120,8 @@ static void nvme_exit(PCIDevice *pci_dev) {NvmeCtrl *n =NVME (pci_dev); -nvme_clear_ctrl (n); +nvme_ctrl_reset (n); +g_free (n->cq);g_free (n->sq);g_free (n->aer_reqs);
Signature · 1 line
2.29.2
PATCH 5/8 hw/block/nvme: rename CAP_PMR_{SHIFT, MASK} to CAP_PMRS_{SHIFT, MASK}
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
Use the correct field name.
--- include/block/nvme.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/block/nvme.h +4 -4
@@ -35,7 +35,7 @@ enum NvmeCapShift {CAP_CSS_SHIFT =37 ,CAP_MPSMIN_SHIFT =48 ,CAP_MPSMAX_SHIFT =52 , -CAP_PMR_SHIFT =56 , +CAP_PMRS_SHIFT =56 , CAP_CMB_SHIFT = 57, };
@@ -49,7 +49,7 @@ enum NvmeCapMask {CAP_CSS_MASK =0xff ,CAP_MPSMIN_MASK =0xf ,CAP_MPSMAX_MASK =0xf , -CAP_PMR_MASK =0x1 , +CAP_PMRS_MASK =0x1 , CAP_CMB_MASK = 0x1, };
@@ -81,8 +81,8 @@ enum NvmeCapMask { << CAP_MPSMIN_SHIFT)#define NVME_CAP_SET_MPSMAX (cap, val) (cap |= (uint64_t)(val & CAP_MPSMAX_MASK)\ << CAP_MPSMAX_SHIFT) -#define NVME_CAP_SET_PMRS (cap, val) (cap |= (uint64_t)(val & CAP_PMR_MASK) \ - << CAP_PMR_SHIFT) +#define NVME_CAP_SET_PMRS (cap, val) (cap |= (uint64_t)(val & CAP_PMRS_MASK) \ + << CAP_PMRS_SHIFT)#define NVME_CAP_SET_CMBS (cap, val) (cap |= (uint64_t)(val & CAP_CMB_MASK) \ << CAP_CMB_SHIFT)
Signature · 1 line
2.29.2
PATCH 6/8 hw/block/nvme: remove redundant zeroing of PMR registers
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
The controller registers are initially zero. Remove the redundant zeroing.
--- hw/block/nvme.c | 35 ----------------------------------- 1 file changed, 35 deletions(-)
hw/block/nvme.c +0 -35
@@ -2911,43 +2911,8 @@ static void nvme_init_cmb(NvmeCtrl *n, PCIDevice *pci_dev)static void nvme_init_pmr (NvmeCtrl *n,PCIDevice *pci_dev) { -/* PMR Capabities register */ - n->bar.pmrcap =0 ; -NVME_PMRCAP_SET_RDS (n->bar.pmrcap,0 ); -NVME_PMRCAP_SET_WDS (n->bar.pmrcap,0 );NVME_PMRCAP_SET_BIR (n->bar.pmrcap,NVME_PMR_BIR ); -NVME_PMRCAP_SET_PMRTU (n->bar.pmrcap,0 ); -/* Turn on bit 1 support */ NVME_PMRCAP_SET_PMRWBM (n->bar.pmrcap,0x02 ); -NVME_PMRCAP_SET_PMRTO (n->bar.pmrcap,0 ); -NVME_PMRCAP_SET_CMSS (n->bar.pmrcap,0 ); - -/* PMR Control register */ - n->bar.pmrctl =0 ; -NVME_PMRCTL_SET_EN (n->bar.pmrctl,0 ); - -/* PMR Status register */ - n->bar.pmrsts =0 ; -NVME_PMRSTS_SET_ERR (n->bar.pmrsts,0 ); -NVME_PMRSTS_SET_NRDY (n->bar.pmrsts,0 ); -NVME_PMRSTS_SET_HSTS (n->bar.pmrsts,0 ); -NVME_PMRSTS_SET_CBAI (n->bar.pmrsts,0 ); - -/* PMR Elasticity Buffer Size register */ - n->bar.pmrebs =0 ; -NVME_PMREBS_SET_PMRSZU (n->bar.pmrebs,0 ); -NVME_PMREBS_SET_RBB (n->bar.pmrebs,0 ); -NVME_PMREBS_SET_PMRWBZ (n->bar.pmrebs,0 ); - -/* PMR Sustained Write Throughput register */ - n->bar.pmrswtp =0 ; -NVME_PMRSWTP_SET_PMRSWTU (n->bar.pmrswtp,0 ); -NVME_PMRSWTP_SET_PMRSWTV (n->bar.pmrswtp,0 ); - -/* PMR Memory Space Control register */ - n->bar.pmrmsc =0 ; -NVME_PMRMSC_SET_CMSE (n->bar.pmrmsc,0 ); -NVME_PMRMSC_SET_CBA (n->bar.pmrmsc,0 ); pci_register_bar(pci_dev, NVME_PMRCAP_BIR(n->bar.pmrcap), PCI_BASE_ADDRESS_SPACE_MEMORY |
Signature · 1 line
2.29.2
PATCH 7/8 hw/block/nvme: disable PMR at boot up
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
The PMR should not be enabled at boot up. Disable the PMR MemoryRegion initially and implement MMIO for PMRCTL, allowing the host to enable the PMR explicitly.
--- hw/block/nvme.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
hw/block/nvme.c +12 -2
@@ -2549,8 +2549,16 @@ static void nvme_write_bar(NvmeCtrl *n, hwaddr offset, uint64_t data,NVME_GUEST_ERR (pci_nvme_ub_mmiowr_pmrcap_readonly,"invalid write to PMRCAP register, ignored" );return ; -case 0xE04 :/* TODO PMRCTL */ -break ; +case 0xE04 :/* PMRCTL */ + n->bar.pmrctl = data; +if (NVME_PMRCTL_EN (data)) { +memory_region_set_enabled (&n->pmrdev->mr, true); + n->bar.pmrsts =0 ; + }else { +memory_region_set_enabled (&n->pmrdev->mr, false); +NVME_PMRSTS_SET_NRDY (n->bar.pmrsts,1 ); + } +return ;case 0xE08 :/* PMRSTS */ NVME_GUEST_ERR (pci_nvme_ub_mmiowr_pmrsts_readonly,"invalid write to PMRSTS register, ignored" );
@@ -2918,6 +2926,8 @@ static void nvme_init_pmr(NvmeCtrl *n, PCIDevice *pci_dev)PCI_BASE_ADDRESS_SPACE_MEMORY |PCI_BASE_ADDRESS_MEM_TYPE_64 | PCI_BASE_ADDRESS_MEM_PREFETCH, &n->pmrdev->mr); + +memory_region_set_enabled (&n->pmrdev->mr, false); }static void nvme_init_pci (NvmeCtrl *n,PCIDevice *pci_dev,Error **errp)
Signature · 1 line
2.29.2
PATCH 8/8 hw/block/nvme: add PMR RDS/WDS support
In reply to Klaus JensenFrom: Naveen Nagar <[email protected]>
Add support for the PMRMSCL and PMRMSCU MMIO registers. This allows adding RDS/WDS support for PMR as well.
--- hw/block/nvme.h | 1 + include/block/nvme.h | 1 + hw/block/nvme.c | 88 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 3 deletions(-)
hw/block/nvme.h +1 -0
@@ -142,6 +142,7 @@ typedef struct NvmeCtrl {uint64_t timestamp_set_qemu_clock_ms;/* QEMU clock time */ uint64_t starttime_ms;uint16_t temperature; +bool pmr_cmse;HostMemoryBackend *pmrdev;
include/block/nvme.h +1 -0
@@ -62,6 +62,7 @@ enum NvmeCapMask {#define NVME_CAP_CSS (cap) (((cap) >> CAP_CSS_SHIFT) & CAP_CSS_MASK)#define NVME_CAP_MPSMIN (cap)(((cap) >> CAP_MPSMIN_SHIFT) & CAP_MPSMIN_MASK)#define NVME_CAP_MPSMAX (cap)(((cap) >> CAP_MPSMAX_SHIFT) & CAP_MPSMAX_MASK) +#define NVME_CAP_PMRS (cap) (((cap) >> CAP_PMRS_SHIFT) & CAP_PMRS_MASK)#define NVME_CAP_SET_MQES (cap, val) (cap |= (uint64_t)(val & CAP_MQES_MASK) \ << CAP_MQES_SHIFT)
hw/block/nvme.c +85 -3
132 lines not on this page · raw message
Signature · 1 line
2.29.2