Skip to contentSkip to conversation

Fix NVMe, not only on Raspberry Pi 5

PATCH In review Torsten Duwe 3 participants 8 messages Jul 6 – Jul 21

lore
Share & tools
lore
#0 lore raw

v2 cover Fix NVMe, not only on Raspberry Pi 5

Hi,

Here's the rework of the central fix as requested by Neil, the DMA addresses as required by the NVMe device are no longer calculated "last minute" before going into the registers or into the memory locations, but early, as they occur. Hopefully talking variable names and member names are used to store the values when they are needed at a later point.

Changes from v1:
- rework address translation towards the source, storing the calculated dma (bus) values where applicable
- add a unit test for phys_to_bus
- automatically scan for any NVMes to maybe boot the Pi from
- remove some dead code
- add Peter's review and test tags

Changes from the single patch:
- the ternary in the macro is gone, explanation updated accordingly
- instead, device_get_dma_constraints is enhanced to try harder
- Raspberry Pi NVMe config added

Torsten Duwe (4):
core: Skip parent device nodes without a DT reference when looking for
dma-ranges
test: Add a unit test in phys_to_bus for dynamically created device nodes
nvme: Fix missing address translation for PCIe inbound access
configs: enable NVMe

 arch/sandbox/dts/test.dts   |  9 +++++
 configs/rpi_arm64_defconfig |  3 +-
 drivers/core/device.c       | 14 +++++++-
 drivers/nvme/nvme.c         | 71 ++++++++++++++-----------------------
 drivers/nvme/nvme.h         |  3 ++
 test/dm/phys2bus.c          | 25 ++++++++++++-
 6 files changed, 77 insertions(+), 48 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/4 core: Skip parent device nodes without a DT reference when looking for dma-ranges

From: Torsten Duwe <[email protected]>

If a device node got created dynamically, there is no guarantee that the parent node has an associated device tree node which could specify dma constraints. Especially PCI(e) enumeration adds intermediate "bus nodes", also dynamically.

Try harder to find the correct configuration by walking up the tree until a DT association is found.

---
 drivers/core/device.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/core/device.c +13 -1
index d365204ba11..b6d00cf4714 100644
@@ -459,7 +459,19 @@ static int device_get_dma_constraints(struct udevice *dev)
 	u64 size = 0;
 	int ret;
 
-	if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
+	if (!CONFIG_IS_ENABLED(DM_DMA) || !parent)
+		return 0;
+
+	/* Look for the first node in the parent chain */
+	while (parent) {
+		if (dev_has_ofnode(parent))
+			break;
+
+		parent = dev_get_parent(parent);
+	}
+
+	/* No parents have a node, bail out */
+	if (!parent)
 		return 0;
 
 	/*
Signature · 1 line
2.54.0
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] core: Skip parent device nodes without a DT reference when looking for dma-ranges

Quoted 7 lines · Torsten Duwe

From: Torsten Duwe <[email protected]>

If a device node got created dynamically, there is no guarantee that the parent node has an associated device tree node which could specify dma constraints. Especially PCI(e) enumeration adds intermediate "bus nodes", also dynamically.

Quoted 7 lines

Try harder to find the correct configuration by walking up the tree until a DT association is found.

Quoted 29 lines
---
  drivers/core/device.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
drivers/core/device.c +13 -1
index d365204ba11..b6d00cf4714 100644
@@ -459,7 +459,19 @@ static int device_get_dma_constraints(struct udevice *dev)
  	u64 size = 0;
  	int ret;
 
-	if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
+	if (!CONFIG_IS_ENABLED(DM_DMA) || !parent)
+		return 0;
+
+	/* Look for the first node in the parent chain */
+	while (parent) {
+		if (dev_has_ofnode(parent))
+			break;
+
+		parent = dev_get_parent(parent);
+	}
+
+	/* No parents have a node, bail out */
+	if (!parent)
  		return 0;
 
  	/*
#3 lore raw

PATCH v2 2/4 test: Add a unit test in phys_to_bus for dynamically created device nodes

In reply to Torsten Duwe

From: Torsten Duwe <[email protected]>

In order to test whether device_get_dma_constraints() is now able to skip device nodes without a DT association, temporarily remove those on 2 newly- created nodes.

---

This test indeed makes a different whether the previous change is applied or not, but *only* reliably for the non-flat-tree case (!?). On the flat tree, it always fails after (reverse) applying it.

Either the previous patch is incomplete, or, more likely, the test assumptions here are not all valid. Suggestions welcome (especially how to create device nodes from a device tree without a reference to their origin ;-)

---
 arch/sandbox/dts/test.dts |  9 +++++++++
 test/dm/phys2bus.c        | 25 ++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)
arch/sandbox/dts/test.dts +9 -0
index 0887de4333b..1d92e85c739 100644
@@ -656,6 +656,15 @@
 		subnode@0 {
 			compatible = "denx,u-boot-fdt-test";
 		};
+		bus@7 {
+			#address-cells = <1>;
+			#size-cells = <1>;
+			compatible = "denx,u-boot-test-bus";
+
+			subnode@8 {
+				  compatible = "denx,u-boot-fdt-test";
+			};
+		};
 	};
 
 	mmio-bus@1 {
test/dm/phys2bus.c +25 -1
index 67f33904943..858ed5410e2 100644
@@ -15,7 +15,8 @@
 
 static int dm_test_phys_to_bus(struct unit_test_state *uts)
 {
-	struct udevice *dev;
+	struct udevice *dev, *pdev;
+	ofnode psave, nsave;
 	ofnode node;
 
 	node = ofnode_path("/mmio-bus@0");
@@ -31,6 +32,29 @@ static int dm_test_phys_to_bus(struct unit_test_state *uts)
 	ut_asserteq_addr((void*)0x1003ffffULL, (void*)dev_phys_to_bus(dev, 0x3ffff));
 	ut_asserteq_addr((void*)0x3ffffULL, (void*)(ulong)dev_bus_to_phys(dev, 0x1003ffff));
 
+#if CONFIG_IS_ENABLED(OF_REAL)
+	/* dma-ranges are on the grandparent bus node,
+	 *  OF nodes neither here nor at parent
+	 */
+	node = ofnode_path("/mmio-bus@0/bus@7");
+	ut_assert(ofnode_valid(node));
+	ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &pdev));
+
+	/* pretend the devices were discovered dynamically and have no OF node */
+	psave = pdev->node_;
+	pdev->node_.np = NULL;
+	node = ofnode_path("/mmio-bus@0/bus@7/subnode@8");
+	ut_assert(ofnode_valid(node));
+	nsave = dev->node_;
+	dev->node_.np = NULL;
+	ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
+
+	ut_asserteq_addr((void *)0x1003ffffULL, (void *)dev_phys_to_bus(dev, 0x3ffff));
+	ut_asserteq_addr((void *)0x3ffffULL, (void *)(ulong)dev_bus_to_phys(dev, 0x1003ffff));
+
+	pdev->node_ = psave;
+	dev->node_ = nsave;
+#endif
 	return 0;
 }
 DM_TEST(dm_test_phys_to_bus, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Signature · 1 line
2.54.0
#4 lore raw

PATCH v2 3/4 nvme: Fix missing address translation for PCIe inbound access

In reply to Torsten Duwe

From: Torsten Duwe <[email protected]>

U-Boot currently does not account for PCIe bridges with a non-zero inbound access offset when talking NVMe, it only works on platforms where this offset happens to be zero.

This patch enhances the NVMe driver with the ability to also handle these cases. It also removes the unused function nvme_get_features().

---
I tried to consistently use uintptr_t when doing arithmetic on pointers and dma_addr_t to hold, well, DMA addresses ;)

nvme_get_features mentions it's unused; so far it causes fruitless maintenance work and causes dead extra bytes in das U-Boot binary. It hasn't been neccessary for 9 years; one can revive it from git once needed.

---
 drivers/nvme/nvme.c | 71 +++++++++++++++++----------------------------
 drivers/nvme/nvme.h |  3 ++
 2 files changed, 29 insertions(+), 45 deletions(-)
drivers/nvme/nvme.c +28 -45
index 147a104149e..54284c0242b 100644
@@ -8,10 +8,10 @@
 #include <bootdev.h>
 #include <cpu_func.h>
 #include <dm.h>
-#include <errno.h>
 #include <log.h>
 #include <malloc.h>
 #include <memalign.h>
+#include <phys2bus.h>
 #include <time.h>
 #include <dm/device-internal.h>
 #include <linux/compat.h>
@@ -101,6 +101,8 @@ static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
 			printf("Error: malloc prp_pool fail\n");
 			return -ENOMEM;
 		}
+		dev->prp_pool_dma = dev_phys_to_bus(dev->udev,
+						    (uintptr_t)dev->prp_pool);
 		dev->prp_entry_num = num_pages * (prps_per_page - 1) + 1;
 	}
 
@@ -108,8 +110,10 @@ static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
 	i = 0;
 	while (nprps) {
 		if ((i == (prps_per_page - 1)) && nprps > 1) {
-			*(prp_pool + i) = cpu_to_le64((ulong)prp_pool +
-					page_size);
+			dma_addr_t prp_pool_dma =
+			    dev_phys_to_bus(dev->udev, (uintptr_t)prp_pool);
+			*(prp_pool + i) = cpu_to_le64(prp_pool_dma +
+								page_size);
 			i = 0;
 			prp_pool = (u64 *)((uintptr_t)prp_pool + page_size);
 		}
@@ -117,7 +121,7 @@ static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
 		dma_addr += page_size;
 		nprps--;
 	}
-	*prp2 = (ulong)dev->prp_pool;
+	*prp2 = dev->prp_pool_dma;
 
 	flush_dcache_range((ulong)dev->prp_pool, (ulong)dev->prp_pool +
 			   num_pages * page_size);
@@ -258,11 +262,15 @@ static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev,
 	nvmeq->cqes = (void *)memalign(4096, NVME_CQ_ALLOCATION);
 	if (!nvmeq->cqes)
 		goto free_nvmeq;
+	nvmeq->cq_dma_addr = dev_phys_to_bus(dev->udev,
+					     (uintptr_t)nvmeq->cqes);
 	memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(depth));
 
 	nvmeq->sq_cmds = (void *)memalign(4096, NVME_SQ_SIZE(depth));
 	if (!nvmeq->sq_cmds)
 		goto free_queue;
+	nvmeq->sq_dma_addr = dev_phys_to_bus(dev->udev,
+					     (uintptr_t)nvmeq->sq_cmds);
 	memset((void *)nvmeq->sq_cmds, 0, NVME_SQ_SIZE(depth));
 
 	nvmeq->dev = dev;
@@ -410,8 +416,8 @@ static int nvme_configure_admin_queue(struct nvme_dev *dev)
 	dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
 
 	writel(aqa, &dev->bar->aqa);
-	nvme_writeq((ulong)nvmeq->sq_cmds, &dev->bar->asq);
-	nvme_writeq((ulong)nvmeq->cqes, &dev->bar->acq);
+	nvme_writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
+	nvme_writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
 
 	result = nvme_enable_ctrl(dev);
 	if (result)
@@ -437,7 +443,7 @@ static int nvme_alloc_cq(struct nvme_dev *dev, u16 qid,
 
 	memset(&c, 0, sizeof(c));
 	c.create_cq.opcode = nvme_admin_create_cq;
-	c.create_cq.prp1 = cpu_to_le64((ulong)nvmeq->cqes);
+	c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
 	c.create_cq.cqid = cpu_to_le16(qid);
 	c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
 	c.create_cq.cq_flags = cpu_to_le16(flags);
@@ -454,7 +460,7 @@ static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
 
 	memset(&c, 0, sizeof(c));
 	c.create_sq.opcode = nvme_admin_create_sq;
-	c.create_sq.prp1 = cpu_to_le64((ulong)nvmeq->sq_cmds);
+	c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
 	c.create_sq.sqid = cpu_to_le16(qid);
 	c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
 	c.create_sq.sq_flags = cpu_to_le16(flags);
@@ -464,13 +470,13 @@ static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
 }
 
 int nvme_identify(struct nvme_dev *dev, unsigned nsid,
-		  unsigned cns, dma_addr_t dma_addr)
+		  unsigned cns, dma_addr_t cpu_addr)
 {
 	struct nvme_command c;
 	u32 page_size = dev->page_size;
+	dma_addr_t dma_addr = dev_phys_to_bus(dev->udev, cpu_addr);
 	int offset = dma_addr & (page_size - 1);
 	int length = sizeof(struct nvme_id_ctrl);
-	dma_addr_t orig_dma_addr = dma_addr;
 	int ret;
 
 	memset(&c, 0, sizeof(c));
@@ -488,49 +494,22 @@ int nvme_identify(struct nvme_dev *dev, unsigned nsid,
 
 	c.identify.cns = cpu_to_le32(cns);
 
-	nvme_invalidate_cache_aligned((uintptr_t)orig_dma_addr,
+	nvme_invalidate_cache_aligned((uintptr_t)cpu_addr,
 				      sizeof(struct nvme_id_ctrl));
 
 	ret = nvme_submit_admin_cmd(dev, &c, NULL);
 	if (!ret)
-		nvme_invalidate_cache_aligned((uintptr_t)orig_dma_addr,
+		nvme_invalidate_cache_aligned((uintptr_t)cpu_addr,
 					      sizeof(struct nvme_id_ctrl));
 
 	return ret;
 }
 
-int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
-		      dma_addr_t dma_addr, u32 *result)
-{
-	struct nvme_command c;
-	int ret;
-
-	memset(&c, 0, sizeof(c));
-	c.features.opcode = nvme_admin_get_features;
-	c.features.nsid = cpu_to_le32(nsid);
-	c.features.prp1 = cpu_to_le64(dma_addr);
-	c.features.fid = cpu_to_le32(fid);
-
-	ret = nvme_submit_admin_cmd(dev, &c, result);
-
-	/*
-	 * TODO: Add some cache invalidation when a DMA buffer is involved
-	 * in the request, here and before the command gets submitted. The
-	 * buffer size varies by feature, also some features use a different
-	 * field in the command packet to hold the buffer address.
-	 * Section 5.21.1 (Set Features command) in the NVMe specification
-	 * details the buffer requirements for each feature.
-	 *
-	 * At the moment there is no user of this function.
-	 */
-
-	return ret;
-}
-
 int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
-		      dma_addr_t dma_addr, u32 *result)
+		      dma_addr_t cpu_addr, u32 *result)
 {
 	struct nvme_command c;
+	dma_addr_t dma_addr = dev_phys_to_bus(dev->udev, cpu_addr);
 
 	memset(&c, 0, sizeof(c));
 	c.features.opcode = nvme_admin_set_features;
@@ -769,7 +748,7 @@ static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
 	u64 prp2;
 	u64 total_len = blkcnt << desc->log2blksz;
 	u64 temp_len = total_len;
-	uintptr_t temp_buffer = (uintptr_t)buffer;
+	uintptr_t dma_addr = dev_phys_to_bus(udev, (uintptr_t)buffer);
 
 	u64 slba = blknr;
 	u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
@@ -797,19 +776,19 @@ static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
 		}
 
 		if (nvme_setup_prps(dev, &prp2,
-				    lbas << ns->lba_shift, temp_buffer))
+				    lbas << ns->lba_shift, dma_addr))
 			return -EIO;
 		c.rw.slba = cpu_to_le64(slba);
 		slba += lbas;
 		c.rw.length = cpu_to_le16(lbas - 1);
-		c.rw.prp1 = cpu_to_le64(temp_buffer);
+		c.rw.prp1 = cpu_to_le64(dma_addr);
 		c.rw.prp2 = cpu_to_le64(prp2);
 		status = nvme_submit_sync_cmd(dev->queues[NVME_IO_Q],
 				&c, NULL, IO_TIMEOUT);
 		if (status)
 			break;
 		temp_len -= (u32)lbas << ns->lba_shift;
-		temp_buffer += lbas << ns->lba_shift;
+		dma_addr += lbas << ns->lba_shift;
 	}
 
 	if (read)
@@ -884,6 +863,8 @@ int nvme_init(struct udevice *udev)
 		printf("Error: %s: Out of memory!\n", udev->name);
 		goto free_queue;
 	}
+
+	ndev->prp_pool_dma = dev_phys_to_bus(udev, (uintptr_t)ndev->prp_pool);
 	ndev->prp_entry_num = MAX_PRP_POOL >> 3;
 
 	ret = nvme_setup_io_queues(ndev);
drivers/nvme/nvme.h +3 -0
index bc1d612dde4..7c6ed4d9b55 100644
@@ -619,6 +619,7 @@ struct nvme_dev {
 	u32 page_size;
 	u8 vwc;
 	u64 *prp_pool;
+	uintptr_t prp_pool_dma;
 	u32 prp_entry_num;
 	u32 nn;
 };
@@ -637,7 +638,9 @@ enum nvme_queue_id {
 struct nvme_queue {
 	struct nvme_dev *dev;
 	struct nvme_command *sq_cmds;
+	dma_addr_t sq_dma_addr;
 	struct nvme_completion *cqes;
+	dma_addr_t cq_dma_addr;
 	u32 __iomem *q_db;
 	u16 q_depth;
 	s16 cq_vector;
Signature · 1 line
2.54.0
Reviewed-by Neil Armstrong on v2 3/4
#5 lore raw

Re: [PATCH v2 3/4] nvme: Fix missing address translation for PCIe inbound access

Quoted 242 lines · Torsten Duwe

From: Torsten Duwe <[email protected]>

U-Boot currently does not account for PCIe bridges with a non-zero inbound access offset when talking NVMe, it only works on platforms where this offset happens to be zero.

This patch enhances the NVMe driver with the ability to also handle these cases. It also removes the unused function nvme_get_features().

---
I tried to consistently use uintptr_t when doing arithmetic on pointers and dma_addr_t to hold, well, DMA addresses ;)

nvme_get_features mentions it's unused; so far it causes fruitless maintenance work and causes dead extra bytes in das U-Boot binary. It hasn't been neccessary for 9 years; one can revive it from git once needed.

---
  drivers/nvme/nvme.c | 71 +++++++++++++++++----------------------------
  drivers/nvme/nvme.h |  3 ++
  2 files changed, 29 insertions(+), 45 deletions(-)
drivers/nvme/nvme.c +28 -45
index 147a104149e..54284c0242b 100644
@@ -8,10 +8,10 @@
  #include <bootdev.h>
  #include <cpu_func.h>
  #include <dm.h>
-#include <errno.h>
  #include <log.h>
  #include <malloc.h>
  #include <memalign.h>
+#include <phys2bus.h>
  #include <time.h>
  #include <dm/device-internal.h>
  #include <linux/compat.h>
@@ -101,6 +101,8 @@ static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
  			printf("Error: malloc prp_pool fail\n");
  			return -ENOMEM;
  		}
+		dev->prp_pool_dma = dev_phys_to_bus(dev->udev,
+						    (uintptr_t)dev->prp_pool);
  		dev->prp_entry_num = num_pages * (prps_per_page - 1) + 1;
  	}
 
@@ -108,8 +110,10 @@ static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
  	i = 0;
  	while (nprps) {
  		if ((i == (prps_per_page - 1)) && nprps > 1) {
-			*(prp_pool + i) = cpu_to_le64((ulong)prp_pool +
-					page_size);
+			dma_addr_t prp_pool_dma =
+			    dev_phys_to_bus(dev->udev, (uintptr_t)prp_pool);
+			*(prp_pool + i) = cpu_to_le64(prp_pool_dma +
+								page_size);
  			i = 0;
  			prp_pool = (u64 *)((uintptr_t)prp_pool + page_size);
  		}
@@ -117,7 +121,7 @@ static int nvme_setup_prps(struct nvme_dev *dev, u64 *prp2,
  		dma_addr += page_size;
  		nprps--;
  	}
-	*prp2 = (ulong)dev->prp_pool;
+	*prp2 = dev->prp_pool_dma;
 
  	flush_dcache_range((ulong)dev->prp_pool, (ulong)dev->prp_pool +
  			   num_pages * page_size);
@@ -258,11 +262,15 @@ static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev,
  	nvmeq->cqes = (void *)memalign(4096, NVME_CQ_ALLOCATION);
  	if (!nvmeq->cqes)
  		goto free_nvmeq;
+	nvmeq->cq_dma_addr = dev_phys_to_bus(dev->udev,
+					     (uintptr_t)nvmeq->cqes);
  	memset((void *)nvmeq->cqes, 0, NVME_CQ_SIZE(depth));
 
  	nvmeq->sq_cmds = (void *)memalign(4096, NVME_SQ_SIZE(depth));
  	if (!nvmeq->sq_cmds)
  		goto free_queue;
+	nvmeq->sq_dma_addr = dev_phys_to_bus(dev->udev,
+					     (uintptr_t)nvmeq->sq_cmds);
  	memset((void *)nvmeq->sq_cmds, 0, NVME_SQ_SIZE(depth));
 
  	nvmeq->dev = dev;
@@ -410,8 +416,8 @@ static int nvme_configure_admin_queue(struct nvme_dev *dev)
  	dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
 
  	writel(aqa, &dev->bar->aqa);
-	nvme_writeq((ulong)nvmeq->sq_cmds, &dev->bar->asq);
-	nvme_writeq((ulong)nvmeq->cqes, &dev->bar->acq);
+	nvme_writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
+	nvme_writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
 
  	result = nvme_enable_ctrl(dev);
  	if (result)
@@ -437,7 +443,7 @@ static int nvme_alloc_cq(struct nvme_dev *dev, u16 qid,
 
  	memset(&c, 0, sizeof(c));
  	c.create_cq.opcode = nvme_admin_create_cq;
-	c.create_cq.prp1 = cpu_to_le64((ulong)nvmeq->cqes);
+	c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  	c.create_cq.cqid = cpu_to_le16(qid);
  	c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  	c.create_cq.cq_flags = cpu_to_le16(flags);
@@ -454,7 +460,7 @@ static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
 
  	memset(&c, 0, sizeof(c));
  	c.create_sq.opcode = nvme_admin_create_sq;
-	c.create_sq.prp1 = cpu_to_le64((ulong)nvmeq->sq_cmds);
+	c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  	c.create_sq.sqid = cpu_to_le16(qid);
  	c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  	c.create_sq.sq_flags = cpu_to_le16(flags);
@@ -464,13 +470,13 @@ static int nvme_alloc_sq(struct nvme_dev *dev, u16 qid,
  }
 
  int nvme_identify(struct nvme_dev *dev, unsigned nsid,
-		  unsigned cns, dma_addr_t dma_addr)
+		  unsigned cns, dma_addr_t cpu_addr)
  {
  	struct nvme_command c;
  	u32 page_size = dev->page_size;
+	dma_addr_t dma_addr = dev_phys_to_bus(dev->udev, cpu_addr);
  	int offset = dma_addr & (page_size - 1);
  	int length = sizeof(struct nvme_id_ctrl);
-	dma_addr_t orig_dma_addr = dma_addr;
  	int ret;
 
  	memset(&c, 0, sizeof(c));
@@ -488,49 +494,22 @@ int nvme_identify(struct nvme_dev *dev, unsigned nsid,
 
  	c.identify.cns = cpu_to_le32(cns);
 
-	nvme_invalidate_cache_aligned((uintptr_t)orig_dma_addr,
+	nvme_invalidate_cache_aligned((uintptr_t)cpu_addr,
  				      sizeof(struct nvme_id_ctrl));
 
  	ret = nvme_submit_admin_cmd(dev, &c, NULL);
  	if (!ret)
-		nvme_invalidate_cache_aligned((uintptr_t)orig_dma_addr,
+		nvme_invalidate_cache_aligned((uintptr_t)cpu_addr,
  					      sizeof(struct nvme_id_ctrl));
 
  	return ret;
  }
 
-int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
-		      dma_addr_t dma_addr, u32 *result)
-{
-	struct nvme_command c;
-	int ret;
-
-	memset(&c, 0, sizeof(c));
-	c.features.opcode = nvme_admin_get_features;
-	c.features.nsid = cpu_to_le32(nsid);
-	c.features.prp1 = cpu_to_le64(dma_addr);
-	c.features.fid = cpu_to_le32(fid);
-
-	ret = nvme_submit_admin_cmd(dev, &c, result);
-
-	/*
-	 * TODO: Add some cache invalidation when a DMA buffer is involved
-	 * in the request, here and before the command gets submitted. The
-	 * buffer size varies by feature, also some features use a different
-	 * field in the command packet to hold the buffer address.
-	 * Section 5.21.1 (Set Features command) in the NVMe specification
-	 * details the buffer requirements for each feature.
-	 *
-	 * At the moment there is no user of this function.
-	 */
-
-	return ret;
-}
-
  int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
-		      dma_addr_t dma_addr, u32 *result)
+		      dma_addr_t cpu_addr, u32 *result)
  {
  	struct nvme_command c;
+	dma_addr_t dma_addr = dev_phys_to_bus(dev->udev, cpu_addr);
 
  	memset(&c, 0, sizeof(c));
  	c.features.opcode = nvme_admin_set_features;
@@ -769,7 +748,7 @@ static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
  	u64 prp2;
  	u64 total_len = blkcnt << desc->log2blksz;
  	u64 temp_len = total_len;
-	uintptr_t temp_buffer = (uintptr_t)buffer;
+	uintptr_t dma_addr = dev_phys_to_bus(udev, (uintptr_t)buffer);
 
  	u64 slba = blknr;
  	u16 lbas = 1 << (dev->max_transfer_shift - ns->lba_shift);
@@ -797,19 +776,19 @@ static ulong nvme_blk_rw(struct udevice *udev, lbaint_t blknr,
  		}
 
  		if (nvme_setup_prps(dev, &prp2,
-				    lbas << ns->lba_shift, temp_buffer))
+				    lbas << ns->lba_shift, dma_addr))
  			return -EIO;
  		c.rw.slba = cpu_to_le64(slba);
  		slba += lbas;
  		c.rw.length = cpu_to_le16(lbas - 1);
-		c.rw.prp1 = cpu_to_le64(temp_buffer);
+		c.rw.prp1 = cpu_to_le64(dma_addr);
  		c.rw.prp2 = cpu_to_le64(prp2);
  		status = nvme_submit_sync_cmd(dev->queues[NVME_IO_Q],
  				&c, NULL, IO_TIMEOUT);
  		if (status)
  			break;
  		temp_len -= (u32)lbas << ns->lba_shift;
-		temp_buffer += lbas << ns->lba_shift;
+		dma_addr += lbas << ns->lba_shift;
  	}
 
  	if (read)
@@ -884,6 +863,8 @@ int nvme_init(struct udevice *udev)
  		printf("Error: %s: Out of memory!\n", udev->name);
  		goto free_queue;
  	}
+
+	ndev->prp_pool_dma = dev_phys_to_bus(udev, (uintptr_t)ndev->prp_pool);
  	ndev->prp_entry_num = MAX_PRP_POOL >> 3;
 
  	ret = nvme_setup_io_queues(ndev);
drivers/nvme/nvme.h +1 -0
index bc1d612dde4..7c6ed4d9b55 100644
@@ -619,6 +619,7 @@ struct nvme_dev {
  	u32 page_size;
  	u8 vwc;
  	u64 *prp_pool;
+	uintptr_t prp_pool_dma;
  	u32 prp_entry_num;
  	u32 nn;
  };

drivers/nvme/nvme.h enum nvme_queue_id { · line 646

Quoted 9 lines
drivers/nvme/nvme.h +2 -0
@@ -637,7 +638,9 @@ enum nvme_queue_id {
  struct nvme_queue {
  	struct nvme_dev *dev;
  	struct nvme_command *sq_cmds;
+	dma_addr_t sq_dma_addr;
  	struct nvme_completion *cqes;
+	dma_addr_t cq_dma_addr;
  	u32 __iomem *q_db;
  	u16 q_depth;
  	s16 cq_vector;

Thanks,
Neil

#6 lore raw

PATCH v2 4/4 configs: enable NVMe

In reply to Torsten Duwe

From: Torsten Duwe <[email protected]>

Enable NVMe in the Raspberry Pi 64-Bit default config and scan for devices.

---
 configs/rpi_arm64_defconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
configs/rpi_arm64_defconfig +2 -1
index cdcf05ea6db..fb0fdce1d7f 100644
@@ -16,7 +16,7 @@ CONFIG_BOOTSTD_DEFAULTS=y
 CONFIG_OF_BOARD_SETUP=y
 CONFIG_FDT_SIMPLEFB=y
 CONFIG_USE_PREBOOT=y
-CONFIG_PREBOOT="pci enum; usb start;"
+CONFIG_PREBOOT="pci enum; usb start; nvme scan;"
 CONFIG_SYS_PBSIZE=1049
 # CONFIG_DISPLAY_CPUINFO is not set
 # CONFIG_DISPLAY_BOARDINFO is not set
@@ -44,6 +44,7 @@ CONFIG_MMC_SDHCI_SDMA=y
 CONFIG_MMC_SDHCI_BCM2835=y
 CONFIG_MMC_SDHCI_BCMSTB=y
 CONFIG_BCMGENET=y
+CONFIG_NVME_PCI=y
 CONFIG_PCI_BRCMSTB=y
 CONFIG_PINCTRL=y
 # CONFIG_PINCTRL_GENERIC is not set
Signature · 1 line
2.54.0
#7 lore raw

Re: (subset) [PATCH v2 0/4] Fix NVMe, not only on Raspberry Pi 5

In reply to Torsten Duwe

Hi,

Quoted 8 lines · On Mon, 06 Jul 2026 19:20:28 +0200, [email protected] wrote:

Here's the rework of the central fix as requested by Neil, the DMA addresses as required by the NVMe device are no longer calculated "last minute" before going into the registers or into the memory locations, but early, as they occur. Hopefully talking variable names and member names are used to store the values when they are needed at a later point.

[...]

Thanks, Applied to https://git.u-boot-project.org/u-boot/custodians/neil.armstrong/u-boot-nvme (u-boot-nvme-next)

[3/4] nvme: Fix missing address translation for PCIe inbound access

      https://git.u-boot-project.org/u-boot/custodians/neil.armstrong/u-boot-nvme/-/commit/09b1c0f98c0b0fe0291e6dac49d0013e782b438d
Signature · 1 line
Neil