Skip to contentSkip to conversation

nvme: allow use of cmb on v1.4 controllers

PATCH Superseded Klaus Jensen 3 participants 6 messages Jan 14, 2021 – Jan 15, 2021 NVMe

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme: allow use of cmb on v1.4 controllers

From: Klaus Jensen <[email protected]>

Since NVMe v1.4 the Controller Memory Buffer must be explicitly enabled by the host.

---
 drivers/nvme/host/pci.c | 25 ++++++++++++++++++++++++-
 include/linux/nvme.h    |  6 ++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
drivers/nvme/host/pci.c +24 -1
index 50d9a20568a2..9c2bbb242994 100644
@@ -1787,7 +1787,8 @@ static u32 nvme_cmb_size(struct nvme_dev *dev)
 
 static void nvme_map_cmb(struct nvme_dev *dev)
 {
-	u64 size, offset;
+	u32 vs;
+	u64 size, offset, cmbmsc;
 	resource_size_t bar_size;
 	struct pci_dev *pdev = to_pci_dev(dev->dev);
 	int bar;
@@ -1795,6 +1796,15 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	if (dev->cmb_size)
 		return;
 
+	vs = readl(dev->bar + NVME_REG_VS);
+
+	if (vs >= NVME_VS(1, 4, 0)) {
+		if (!NVME_CAP_CMBS(dev->ctrl.cap))
+			return;
+
+		writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
+	}
+
 	dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
 	if (!dev->cmbsz)
 		return;
@@ -1805,6 +1815,19 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	bar = NVME_CMB_BIR(dev->cmbloc);
 	bar_size = pci_resource_len(pdev, bar);
 
+	if (vs >= NVME_VS(1, 4, 0)) {
+		cmbmsc = pci_resource_start(pdev, bar);
+		cmbmsc |= (NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE);
+
+		/*
+		 * Writing the low bits may cause the controller to check the
+		 * validity of the 52 bit controller base address in the CMBMSC
+		 * register, so write the high bits first.
+		 */
+		writel(cmbmsc >> 32, dev->bar + NVME_REG_CMBMSC + 4);
+		writel(cmbmsc, dev->bar + NVME_REG_CMBMSC);
+	}
+
 	if (offset > bar_size)
 		return;
 
include/linux/nvme.h +6 -0
index d92535997687..bfed36e342cc 100644
@@ -116,6 +116,9 @@ enum {
 	NVME_REG_BPMBL	= 0x0048,	/* Boot Partition Memory Buffer
 					 * Location
 					 */
+	NVME_REG_CMBMSC = 0x0050,	/* Controller Memory Buffer Memory
+					 * Space Control
+					 */
 	NVME_REG_PMRCAP	= 0x0e00,	/* Persistent Memory Capabilities */
 	NVME_REG_PMRCTL	= 0x0e04,	/* Persistent Memory Region Control */
 	NVME_REG_PMRSTS	= 0x0e08,	/* Persistent Memory Region Status */
@@ -135,6 +138,7 @@ enum {
 #define NVME_CAP_CSS(cap)	(((cap) >> 37) & 0xff)
 #define NVME_CAP_MPSMIN(cap)	(((cap) >> 48) & 0xf)
 #define NVME_CAP_MPSMAX(cap)	(((cap) >> 52) & 0xf)
+#define NVME_CAP_CMBS(cap)	(((cap) >> 57) & 0x1)
 
 #define NVME_CMB_BIR(cmbloc)	((cmbloc) & 0x7)
 #define NVME_CMB_OFST(cmbloc)	(((cmbloc) >> 12) & 0xfffff)
@@ -192,6 +196,8 @@ enum {
 	NVME_CSTS_SHST_OCCUR	= 1 << 2,
 	NVME_CSTS_SHST_CMPLT	= 2 << 2,
 	NVME_CSTS_SHST_MASK	= 3 << 2,
+	NVME_CMBMSC_CRE		= 1 << 0,
+	NVME_CMBMSC_CMSE	= 1 << 1,
 };
 
 struct nvme_id_power_state {
Signature · 7 lines
2.30.0


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#1 lore raw
Quoted 4 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Since NVMe v1.4 the Controller Memory Buffer must be explicitly enabled by the host.

Thanks, this has been on my TODO list for a while.

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1803

Quoted 5 lines
drivers/nvme/host/pci.c +5 -0
+	vs = readl(dev->bar + NVME_REG_VS);
+
+	if (vs >= NVME_VS(1, 4, 0)) {
+		if (!NVME_CAP_CMBS(dev->ctrl.cap))
+			return;

We should not check the version here, but the presence of the new register so that controllers claiming an older version complicance that implement the new scheme are supported as well.

Given that the old scheme is highly dangerous in virtualized enviroments we should also warn about it and eventually required an explicit opt-in to use it.

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1828

Quoted 11 lines
drivers/nvme/host/pci.c +11 -0
+	if (vs >= NVME_VS(1, 4, 0)) {
+		cmbmsc = pci_resource_start(pdev, bar);
+		cmbmsc |= (NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE);
+
+		/*
+		 * Writing the low bits may cause the controller to check the
+		 * validity of the 52 bit controller base address in the CMBMSC
+		 * register, so write the high bits first.
+		 */
+		writel(cmbmsc >> 32, dev->bar + NVME_REG_CMBMSC + 4);
+		writel(cmbmsc, dev->bar + NVME_REG_CMBMSC);

I think we should just use writeq here, with the hilo wrapper for 32-bit platforms here. The reason why NVMe has so avoided the 64-bit access is for buggy Apple controllers, but they are unlikely to implement CMB.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#2 lore raw
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Klaus Jensen

From: Klaus Jensen <[email protected]>

Since NVMe v1.4 the Controller Memory Buffer must be explicitly enabled by the host.

Thanks, this has been on my TODO list for a while.

Quoted 10 lines

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1803

Quoted 5 lines
drivers/nvme/host/pci.c +5 -0
+	vs = readl(dev->bar + NVME_REG_VS);
+
+	if (vs >= NVME_VS(1, 4, 0)) {
+		if (!NVME_CAP_CMBS(dev->ctrl.cap))
+			return;

We should not check the version here, but the presence of the new register so that controllers claiming an older version complicance that implement the new scheme are supported as well.

Understood!

Quoted 4 lines

Given that the old scheme is highly dangerous in virtualized enviroments we should also warn about it and eventually required an explicit opt-in to use it.

Not completely sure what warning you'd want, so I'll leave it to you to add an 'else dev_warn(...)'. :)

Quoted 7 lines
Quoted 7 lines
drivers/nvme/host/pci.c +7 -0
+	if (vs >= NVME_VS(1, 4, 0)) {
+		cmbmsc = pci_resource_start(pdev, bar);
+		cmbmsc |= (NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE);
+
+		/*
+		 * Writing the low bits may cause the controller to check the
+		 * validity of the 52 bit controller base address in the CMBMSC
Quoted 8 lines

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1828

Quoted 4 lines
drivers/nvme/host/pci.c +4 -0
+		 * register, so write the high bits first.
+		 */
+		writel(cmbmsc >> 32, dev->bar + NVME_REG_CMBMSC + 4);
+		writel(cmbmsc, dev->bar + NVME_REG_CMBMSC);

I think we should just use writeq here, with the hilo wrapper for 32-bit platforms here. The reason why NVMe has so avoided the 64-bit access is for buggy Apple controllers, but they are unlikely to implement CMB.

Right, I tried writeq, but on QEMU I observed two 4 byte writes in wrong order anyway.

hi_lo_writeq looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer
#3 lore raw
In reply to Klaus Jensen
Quoted 17 lines · Klaus Jensen
drivers/nvme/host/pci.c +9 -0
@@ -1795,6 +1796,15 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	if (dev->cmb_size)
 		return;
 
+	vs = readl(dev->bar + NVME_REG_VS);
+
+	if (vs >= NVME_VS(1, 4, 0)) {
+		if (!NVME_CAP_CMBS(dev->ctrl.cap))
+			return;
+
+		writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
+	}
+
 	dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
 	if (!dev->cmbsz)
 		return;

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1819

Quoted 5 lines
drivers/nvme/host/pci.c +2 -0
@@ -1805,6 +1815,19 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	bar = NVME_CMB_BIR(dev->cmbloc);
 	bar_size = pci_resource_len(pdev, bar);
 
+	if (vs >= NVME_VS(1, 4, 0)) {
+		cmbmsc = pci_resource_start(pdev, bar);

Shouldn't this have a '+ offset' in case the CMB doesn't start at the beginning of the bar?

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1828

Quoted 9 lines
drivers/nvme/host/pci.c +9 -0
+		cmbmsc |= (NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE);
+
+		/*
+		 * Writing the low bits may cause the controller to check the
+		 * validity of the 52 bit controller base address in the CMBMSC
+		 * register, so write the high bits first.
+		 */
+		writel(cmbmsc >> 32, dev->bar + NVME_REG_CMBMSC + 4);
+		writel(cmbmsc, dev->bar + NVME_REG_CMBMSC);

hi_lo_writeq()?

This should probably come after we've verified the '(offset > bar_size)' check below so that we're not enabling these capabilities before we know if they're within the BAR.

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1832

Quoted 4 lines
drivers/nvme/host/pci.c +2 -0
+	}
+
 	if (offset > bar_size)
 		return;

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer
#4 lore raw
Quoted 21 lines · Keith Busch
Quoted 20 lines · Klaus Jensen
drivers/nvme/host/pci.c +9 -0
@@ -1795,6 +1796,15 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	if (dev->cmb_size)
 		return;
 
+	vs = readl(dev->bar + NVME_REG_VS);
+
+	if (vs >= NVME_VS(1, 4, 0)) {
+		if (!NVME_CAP_CMBS(dev->ctrl.cap))
+			return;
+
+		writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
+	}
+
 	dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
 	if (!dev->cmbsz)
 		return;
@@ -1805,6 +1815,19 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	bar = NVME_CMB_BIR(dev->cmbloc);
 	bar_size = pci_resource_len(pdev, bar);
 
Quoted 5 lines

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1819

Quoted 2 lines
drivers/nvme/host/pci.c +2 -0
+	if (vs >= NVME_VS(1, 4, 0)) {
+		cmbmsc = pci_resource_start(pdev, bar);

Shouldn't this have a '+ offset' in case the CMB doesn't start at the beginning of the bar?

One other thing, I believe pci_bus_address() is required instead of pci_resource_start().

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#5 lore raw
In reply to Keith Busch
Quoted 21 lines · Keith Busch
Quoted 20 lines · Klaus Jensen
drivers/nvme/host/pci.c +9 -0
@@ -1795,6 +1796,15 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	if (dev->cmb_size)
 		return;
 
+	vs = readl(dev->bar + NVME_REG_VS);
+
+	if (vs >= NVME_VS(1, 4, 0)) {
+		if (!NVME_CAP_CMBS(dev->ctrl.cap))
+			return;
+
+		writel(NVME_CMBMSC_CRE, dev->bar + NVME_REG_CMBMSC);
+	}
+
 	dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
 	if (!dev->cmbsz)
 		return;
@@ -1805,6 +1815,19 @@ static void nvme_map_cmb(struct nvme_dev *dev)
 	bar = NVME_CMB_BIR(dev->cmbloc);
 	bar_size = pci_resource_len(pdev, bar);
 
Quoted 6 lines

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1819

Quoted 2 lines
drivers/nvme/host/pci.c +2 -0
+	if (vs >= NVME_VS(1, 4, 0)) {
+		cmbmsc = pci_resource_start(pdev, bar);

Shouldn't this have a '+ offset' in case the CMB doesn't start at the beginning of the bar?

Of course, good catch!

Quoted 12 lines

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1828

Quoted 9 lines
drivers/nvme/host/pci.c +9 -0
+		cmbmsc |= (NVME_CMBMSC_CRE | NVME_CMBMSC_CMSE);
+
+		/*
+		 * Writing the low bits may cause the controller to check the
+		 * validity of the 52 bit controller base address in the CMBMSC
+		 * register, so write the high bits first.
+		 */
+		writel(cmbmsc >> 32, dev->bar + NVME_REG_CMBMSC + 4);
+		writel(cmbmsc, dev->bar + NVME_REG_CMBMSC);

hi_lo_writeq()?

Ah, was looking for that...

Quoted 4 lines

This should probably come after we've verified the '(offset > bar_size)' check below so that we're not enabling these capabilities before we know if they're within the BAR.

Right.

Quoted 4 lines

drivers/nvme/host/pci.c static void nvme_map_cmb(struct nvme_dev *dev) · line 1832

Quoted 4 lines
drivers/nvme/host/pci.c +2 -0
+	}
+
 	if (offset > bar_size)
 		return;

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme