mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] vfio: selftests: Add NVMe driver
@ 2026-12-04  1:52 Josh Hilke
  0 siblings, 0 replies; only message in thread
From: Josh Hilke @ 2026-12-04  1:52 UTC (permalink / raw)
  To: David Matlack, Alex Williamson
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	linux-nvme, kvm, linux-kselftest, linux-doc, linux-kernel,
	Josh Hilke

Add a VFIO selftest driver for NVMe controllers. It binds to any PCI
function with the NVMe PCI class code 01:08:02, so vfio_pci_driver_test
can run on the NVMe SSDs that are common in servers, and on the nvme
device that QEMU emulates.

NVMe has no memory-to-memory copy command, so the driver implements a
memcpy through logical blocks of the first active namespace.
memcpy_start() writes @src to those blocks and waits for the write. It
then queues @count reads of the blocks into @dst, along with @count - 1
more writes of @src. The driver must wait for the first write because
the controller can run the commands of a queue in any order. The later
writes store the same data, so every read sees @src. All of these
commands are in flight when memcpy_start() returns. The driver spreads
them over up to 16 queues, because one queue holds only as many
commands as the controller allows, 2047 on the QEMU device. The memcpy
commands go on I/O queues that have interrupts disabled. send_msi()
sends a flush on one more I/O queue that has interrupts enabled.

The driver refuses to overwrite data. The first 4 KiB of the namespace
hold a header that marks the blocks after it as scratch space for VFIO
selftests. On the first memcpy, the driver accepts a header from an
earlier run. Without one, it checks that the header and the scratch
space are all zero, and then writes the header. If they are not all
zero, the test fails and tells the user how to zero the range.
send_msi() needs no scratch space, so tests that only use it work on
any NVMe device.

On some controllers, a DMA error from the unmapped-IOVA tests leaves the
submission queue in an error state that only a Function Level Reset
clears. After a failed memcpy, the driver disables the controller, resets
the function with VFIO_DEVICE_RESET, and initializes the controller
again.

The driver needs a controller that supports the NVM command set, 4 KiB
memory pages, at least two I/O queue pairs, and at least two MSI-X
vectors, along with a namespace that has no metadata and a logical block
size of at most 4 KiB. Controllers that need one of the quirks in
drivers/nvme/host/pci.c are not supported.

The driver uses the definitions in include/linux/nvme.h through a
symbolic link, like the other drivers do with their kernel headers.

Users can verify the driver in QEMU with these commands, after they
build the kernel and the VFIO selftests:

truncate -s 64M /tmp/nvme.img
vng \
--run arch/x86/boot/bzImage \
--user root \
--disable-microvm \
--memory 4G \
--cpus 4 \
--qemu-opts="-M q35,accel=kvm,kernel-irqchip=split" \
--qemu-opts="-device intel-iommu,intremap=on,caching-mode=on" \
--qemu-opts="-drive file=/tmp/nvme.img,if=none,id=nvm,format=raw" \
--qemu-opts="-device nvme,serial=deadbeef,drive=nvm,addr=10.0" \
--append "console=ttyS0 intel_iommu=on" \
--exec "modprobe vfio-pci && \
  ./tools/testing/selftests/vfio/scripts/setup.sh 0000:00:10.0 && \
  ./tools/testing/selftests/vfio/scripts/run.sh \
  ./tools/testing/selftests/vfio/vfio_pci_driver_test"

Signed-off-by: Josh Hilke <jrhilke@google.com>
---
v1: https://lore.kernel.org/all/20261124020814.1679635-1-jrhilke@google.com/

Changes in v2:
- Rebased onto vfio/next and dropped #include <uuid/uuid.h> from nvme.c.
  Commit 09ee515d5e3e, "vfio: selftests: Remove libuuid dependency", now
  provides uuid_t through libvfio.
- Added a 4 KiB scratch header at LBA 0 and zero-checking before the
  first memcpy, so the driver refuses to overwrite non-zero data.
- Queued count - 1 writes of @src alongside the count reads of @dst so
  both DMA reads and DMA writes stay in flight during memcpy_start().
- Wrapped static_assert() before including nvme.h to fix clang builds
  under C11/C99.
- Moved BAR0, MSI-X count, CAP.CSS, and CAP.MPSMIN checks into
  nvme_probe() with dev_err() messages.
- Defined NVME_CTRL_PAGE_SIZE for controller page sizing and alignment,
  and renamed the ien parameter in nvme_create_io_queue() to
  irq_enabled.

Testing:
- Ran vfio_pci_driver_test on QEMU's emulated nvme device and on x86_64
  and arm64 servers with Samsung NVMe SR-IOV VFs. All 35/35 passed on
  each. On both servers, vfio_pci_device_test passed 5/5.
- Built the VFIO selftests with gcc on x86_64 and arm64.
- Built the VFIO selftests with clang on x86_64 using
  -Wno-gnu-variable-sized-type-not-at-end to work around a pre-existing
  mlx5_ifc.h warning also present on the base commit. nvme.c compiles
  cleanly without that flag.

 Documentation/driver-api/vfio-selftests.rst   |   6 +
 .../selftests/vfio/lib/drivers/nvme/nvme.c    | 919 ++++++++++++++++++
 .../selftests/vfio/lib/drivers/nvme/nvme.h    |   1 +
 tools/testing/selftests/vfio/lib/libvfio.mk   |   1 +
 .../selftests/vfio/lib/vfio_pci_driver.c      |   2 +
 5 files changed, 929 insertions(+)
 create mode 100644 tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
 create mode 120000 tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h

diff --git a/Documentation/driver-api/vfio-selftests.rst b/Documentation/driver-api/vfio-selftests.rst
index 9d39a22519d6..567262ac3d55 100644
--- a/Documentation/driver-api/vfio-selftests.rst
+++ b/Documentation/driver-api/vfio-selftests.rst
@@ -39,10 +39,16 @@ includes driver support for the following devices:
 - Intel IOAT (I/O Acceleration Technology) DMA engines
 - Intel DSA (Data Streaming Accelerator)
 - NVIDIA Falcon engines (found in many NVIDIA GPUs, e.g., GTX 1080 or RTX 2080)
+- NVMe controllers
 
 Note that using Intel DSA devices requires setting the module parameter
 ``vfio_pci.disable_denylist=Y`` to allow ``vfio-pci`` to bind to DSA devices.
 
+Note that the NVMe driver writes to the start of the first active namespace.
+To avoid destroying data, it refuses to write unless that space is all zero,
+or was zeroed and claimed by an earlier run (marked by a header in the first
+4 KiB).
+
 Setting up Devices
 ------------------
 
diff --git a/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
new file mode 100644
index 000000000000..4ae07c2c9a30
--- /dev/null
+++ b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c
@@ -0,0 +1,919 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <stddef.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <asm/barrier.h>
+#include <linux/align.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/pci_ids.h>
+#include <linux/pci_regs.h>
+#include <linux/sizes.h>
+#include <linux/stddef.h>
+#include <linux/types.h>
+
+#include <libvfio.h>
+
+/*
+ * nvme.h needs struct_group() and uses the kernel's static_assert(), which
+ * makes the message optional. <assert.h> defines static_assert() as
+ * _Static_assert(), which needs one before C23.
+ */
+#ifndef struct_group
+#define struct_group(NAME, MEMBERS...)	\
+	__struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
+#endif
+
+#undef static_assert
+#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
+#ifndef __static_assert
+#define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
+#endif
+
+#include "nvme.h"
+
+#define NVME_CTRL_PAGE_SIZE		SZ_4K
+#define NVME_ADMIN_QID			0
+#define NVME_MSI_QID			1
+#define NVME_MAX_POLL_QUEUES		16
+#define NVME_IO_MSIX_VEC		1
+#define NVME_ADMIN_QUEUE_DEPTH		64
+
+/* A queue of depth N holds N - 1 commands. msiq has one in flight. */
+#define NVME_MSI_QUEUE_DEPTH		2
+
+/*
+ * Number of entries in the rings of all polled queues together, when the
+ * controller grants NVME_MAX_POLL_QUEUES of them. This bounds max_memcpy_count
+ * so tests that churn through many memcpys, such as memcpy_storm, finish
+ * within their timeouts on slow controllers.
+ */
+#define NVME_POLL_TOTAL_ENTRIES		8192
+
+/* Rings for the polled queues, plus page alignment of each ring. */
+#define NVME_POLL_RINGS_SIZE		(NVME_POLL_TOTAL_ENTRIES *		\
+					 (sizeof(struct nvme_command) +		\
+					  sizeof(struct nvme_completion)) +	\
+					 NVME_MAX_POLL_QUEUES * 2 *		\
+					 NVME_CTRL_PAGE_SIZE)
+
+#define NVME_PRP_LIST_ENTRIES		(NVME_CTRL_PAGE_SIZE / sizeof(__le64))
+
+/* The driver builds one PRP list page, which covers 2 MiB at any offset. */
+#define NVME_MAX_XFER_SIZE		SZ_2M
+#define NVME_MIN_LBA_SHIFT		9
+#define NVME_MAX_LBA_SHIFT		12
+#define NVME_TIMEOUT_MS			5000ULL
+#define NVME_CAP_TIMEOUT_UNIT_MS	500ULL
+#define NVME_POLL_INTERVAL_US		1000
+
+/*
+ * The first NVME_SCRATCH_HDR_SIZE bytes of the namespace hold a header that
+ * marks the namespace as scratch space for VFIO selftests. memcpy commands
+ * use the blocks after it, so the header survives a test that crashes.
+ */
+#define NVME_SCRATCH_HDR_SIZE		SZ_4K
+#define NVME_SCRATCH_MAGIC		"VFIO selftests NVMe scratch v1"
+#define NVME_SCRATCH_CHUNK		SZ_64K
+
+struct nvme_scratch_header {
+	/* NVME_SCRATCH_MAGIC if the driver claimed this namespace. */
+	char magic[32];
+
+	/* Bytes after the header that the driver has verified were zero. */
+	__le64 size;
+};
+
+struct nvme_queue {
+	/* Byte offsets of the submission and completion rings in the region. */
+	u64 sq_offset;
+	u64 cq_offset;
+
+	/* Number of entries in each ring. */
+	u32 depth;
+
+	/* Commands submitted whose completions are not yet consumed. */
+	u32 inflight;
+
+	/* Queue identifier, which also selects the doorbell registers. */
+	u16 qid;
+
+	/* Index of the next submission entry to write. */
+	u16 sq_tail;
+
+	/* Index of the next completion entry to read. */
+	u16 cq_head;
+
+	/* Expected phase tag of the next completion entry. */
+	u16 cq_phase;
+};
+
+struct nvme_state {
+	/* Queue for admin commands, which set up the controller. */
+	struct nvme_queue adminq;
+
+	/*
+	 * I/O queue whose completions raise device->driver.msi. It carries
+	 * only the flush command of nvme_send_msi().
+	 */
+	struct nvme_queue msiq;
+
+	/*
+	 * I/O queues whose completions raise no interrupt. They carry all
+	 * memcpy commands. Their rings start at poll_rings.
+	 */
+	struct nvme_queue pollq[NVME_MAX_POLL_QUEUES];
+
+	/* Number of entries of pollq that are in use. */
+	u32 nr_pollqs;
+
+	/* Distance in bytes between two doorbell registers. */
+	u32 db_stride;
+
+	/* The namespace that the memcpy commands use. */
+	u32 nsid;
+
+	/* Power-of-two shift for converting bytes to logical blocks. */
+	u32 lba_shift;
+
+	/* Set once nvme_claim_scratch() has verified the scratch space. */
+	bool scratch_claimed;
+
+	/* Result of the write in nvme_memcpy_start(). */
+	int memcpy_err;
+
+	/* DMA buffers addressed by the byte offsets held in adminq and msiq. */
+	struct nvme_command admin_sq[NVME_ADMIN_QUEUE_DEPTH] __aligned(NVME_CTRL_PAGE_SIZE);
+	struct nvme_completion admin_cq[NVME_ADMIN_QUEUE_DEPTH] __aligned(NVME_CTRL_PAGE_SIZE);
+	struct nvme_command msi_sq[NVME_MSI_QUEUE_DEPTH] __aligned(NVME_CTRL_PAGE_SIZE);
+	struct nvme_completion msi_cq[NVME_MSI_QUEUE_DEPTH] __aligned(NVME_CTRL_PAGE_SIZE);
+
+	/* Identify data, written by the device. */
+	struct nvme_id_ctrl id_ctrl __aligned(NVME_CTRL_PAGE_SIZE);
+	struct nvme_id_ns id_ns __aligned(NVME_CTRL_PAGE_SIZE);
+	__le32 ns_list[NVME_IDENTIFY_DATA_SIZE / sizeof(__le32)] __aligned(NVME_CTRL_PAGE_SIZE);
+
+	/*
+	 * PRP lists for @src and @dst of the current memcpy. Every command of a
+	 * memcpy uses the same two buffers, so the queued commands share them.
+	 * The synchronous commands on scratch_buf also use prp_src.
+	 */
+	__le64 prp_src[NVME_PRP_LIST_ENTRIES] __aligned(NVME_CTRL_PAGE_SIZE);
+	__le64 prp_dst[NVME_PRP_LIST_ENTRIES] __aligned(NVME_CTRL_PAGE_SIZE);
+
+	/*
+	 * Bounce buffer. The driver reads and writes the scratch header through
+	 * it, and reads the scratch space through it for the zero check.
+	 */
+	u8 scratch_buf[NVME_SCRATCH_CHUNK] __aligned(NVME_CTRL_PAGE_SIZE);
+
+	/* Rings of the polled queues, up to the end of the region. */
+	u8 poll_rings[] __aligned(NVME_CTRL_PAGE_SIZE);
+};
+
+static inline struct nvme_state *to_nvme_state(struct vfio_pci_device *device)
+{
+	return device->driver.region.vaddr;
+}
+
+static inline struct nvme_command *nvme_sq(struct vfio_pci_device *device,
+					   struct nvme_queue *q)
+{
+	return device->driver.region.vaddr + q->sq_offset;
+}
+
+static inline struct nvme_completion *nvme_cq(struct vfio_pci_device *device,
+					      struct nvme_queue *q)
+{
+	return device->driver.region.vaddr + q->cq_offset;
+}
+
+/* Offset of the SQ tail doorbell of @qid, or the CQ head doorbell if @cq. */
+static u64 nvme_db_offset(struct nvme_state *state, u16 qid, bool cq)
+{
+	return NVME_REG_DBS + (2 * qid + cq) * state->db_stride;
+}
+
+static void nvme_ring_sq_db(struct vfio_pci_device *device,
+			    struct nvme_queue *q)
+{
+	struct nvme_state *state = to_nvme_state(device);
+
+	/* Make the commands visible to the device before the doorbell. */
+	dma_wmb();
+	writel(q->sq_tail,
+	       device->bars[0].vaddr + nvme_db_offset(state, q->qid, false));
+}
+
+static void nvme_ring_cq_db(struct vfio_pci_device *device,
+			    struct nvme_queue *q)
+{
+	struct nvme_state *state = to_nvme_state(device);
+
+	/* Finish reading the completion before releasing the slot. */
+	dma_rmb();
+	writel(q->cq_head,
+	       device->bars[0].vaddr + nvme_db_offset(state, q->qid, true));
+}
+
+static void nvme_queue_init(struct vfio_pci_device *device,
+			    struct nvme_queue *q, u16 qid, u32 depth,
+			    u64 sq_offset, u64 cq_offset)
+{
+	q->sq_offset = sq_offset;
+	q->cq_offset = cq_offset;
+	q->depth = depth;
+	q->inflight = 0;
+	q->qid = qid;
+	q->sq_tail = 0;
+	q->cq_head = 0;
+	q->cq_phase = 1;
+
+	memset(nvme_sq(device, q), 0, depth * sizeof(struct nvme_command));
+	memset(nvme_cq(device, q), 0, depth * sizeof(struct nvme_completion));
+}
+
+static void nvme_sq_push(struct vfio_pci_device *device, struct nvme_queue *q,
+			 const struct nvme_command *cmd)
+{
+	struct nvme_command *sq = nvme_sq(device, q);
+
+	memcpy(&sq[q->sq_tail], cmd, sizeof(*cmd));
+	sq[q->sq_tail].common.command_id = q->sq_tail;
+	q->sq_tail = (q->sq_tail + 1) % q->depth;
+	q->inflight++;
+}
+
+static void nvme_sq_submit(struct vfio_pci_device *device, struct nvme_queue *q,
+			   const struct nvme_command *cmd)
+{
+	nvme_sq_push(device, q, cmd);
+	nvme_ring_sq_db(device, q);
+}
+
+/* Consume the next completion on @q if the device has posted it. */
+static bool nvme_cq_consume(struct vfio_pci_device *device, struct nvme_queue *q,
+			    struct nvme_completion *cqe)
+{
+	struct nvme_completion *entry = &nvme_cq(device, q)[q->cq_head];
+
+	if ((le16_to_cpu(READ_ONCE(entry->status)) & 1) != q->cq_phase)
+		return false;
+
+	/* Do not read the completion before the phase tag is observed. */
+	dma_rmb();
+	memcpy(cqe, entry, sizeof(*cqe));
+
+	/* The controller inverts the phase tag on each pass through the ring. */
+	q->cq_head = (q->cq_head + 1) % q->depth;
+	if (q->cq_head == 0)
+		q->cq_phase ^= 1;
+	q->inflight--;
+
+	return true;
+}
+
+static int nvme_cq_wait(struct vfio_pci_device *device, struct nvme_queue *q,
+			struct nvme_completion *cqe)
+{
+	int timeout_ms = NVME_TIMEOUT_MS;
+
+	while (!nvme_cq_consume(device, q, cqe)) {
+		if (timeout_ms-- == 0)
+			return -ETIMEDOUT;
+		usleep(NVME_POLL_INTERVAL_US);
+	}
+
+	nvme_ring_cq_db(device, q);
+
+	return 0;
+}
+
+/* Return the Status Code Type and Status Code of @cqe. */
+static u16 nvme_status(const struct nvme_completion *cqe)
+{
+	return (le16_to_cpu(cqe->status) >> 1) & NVME_SCT_SC_MASK;
+}
+
+static int nvme_probe(struct vfio_pci_device *device)
+{
+	void *bar0 = device->bars[0].vaddr;
+	u32 class_code;
+	u64 cap;
+
+	class_code = vfio_pci_config_readl(device, PCI_CLASS_REVISION) >> 8;
+	if (class_code != PCI_CLASS_STORAGE_EXPRESS)
+		return -EINVAL;
+
+	if (!bar0) {
+		dev_err(device, "nvme: BAR0 is not mapped\n");
+		return -EINVAL;
+	}
+
+	/*
+	 * Admin completions always use vector 0. The queue for send_msi() uses
+	 * vector 1, so admin commands never signal the eventfd that the tests
+	 * check.
+	 */
+	if (device->msix_info.count < 2) {
+		dev_err(device, "nvme: Need at least 2 MSI-X vectors\n");
+		return -EINVAL;
+	}
+
+	cap = readq(bar0 + NVME_REG_CAP);
+	if (cap == ~0ULL) {
+		dev_err(device, "nvme: Controller did not respond\n");
+		return -EINVAL;
+	}
+	if (!(NVME_CAP_CSS(cap) & NVME_CAP_CSS_NVM)) {
+		dev_err(device, "nvme: No NVM command set\n");
+		return -EINVAL;
+	}
+	if (NVME_CAP_MPSMIN(cap)) {
+		dev_err(device, "nvme: No support for 4 KiB memory pages\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static u32 nvme_submit_admin_cmd(struct vfio_pci_device *device,
+				 struct nvme_command *cmd)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	struct nvme_completion cqe = {};
+	int ret;
+
+	nvme_sq_submit(device, &state->adminq, cmd);
+	ret = nvme_cq_wait(device, &state->adminq, &cqe);
+	VFIO_ASSERT_EQ(ret, 0, "Admin command 0x%x timed out",
+		       cmd->common.opcode);
+	VFIO_ASSERT_EQ(nvme_status(&cqe), 0,
+		       "Admin command 0x%x failed with status 0x%x",
+		       cmd->common.opcode, nvme_status(&cqe));
+
+	return le32_to_cpu(cqe.result.u32);
+}
+
+/* Return the number of I/O queue pairs that the controller allocated. */
+static u32 nvme_set_num_queues(struct vfio_pci_device *device, u16 nr_queues)
+{
+	struct nvme_command cmd = {};
+	u32 nr_sqs, nr_cqs;
+	u32 result;
+
+	cmd.features.opcode = nvme_admin_set_features;
+	cmd.features.fid = cpu_to_le32(NVME_FEAT_NUM_QUEUES);
+	cmd.features.dword11 = cpu_to_le32((u32)(nr_queues - 1) << 16 |
+					   (nr_queues - 1));
+
+	/* Dword 0 holds zero-based counts: SQs in bits 15:0, CQs in bits 31:16. */
+	result = nvme_submit_admin_cmd(device, &cmd);
+	nr_sqs = (result & 0xffff) + 1;
+	nr_cqs = (result >> 16) + 1;
+
+	/* A queue pair needs one SQ and one CQ. */
+	return min(nr_sqs, nr_cqs);
+}
+
+static void nvme_identify(struct vfio_pci_device *device, u32 nsid, u8 cns,
+			  void *buf)
+{
+	struct nvme_command cmd = {};
+
+	cmd.identify.opcode = nvme_admin_identify;
+	cmd.identify.nsid = cpu_to_le32(nsid);
+	cmd.identify.dptr.prp1 = cpu_to_le64(to_iova(device, buf));
+	cmd.identify.cns = cns;
+
+	nvme_submit_admin_cmd(device, &cmd);
+}
+
+static void nvme_enable_irqs(struct vfio_pci_device *device)
+{
+	device->driver.msi = NVME_IO_MSIX_VEC;
+	vfio_pci_msix_enable(device, 0, NVME_IO_MSIX_VEC + 1);
+}
+
+/*
+ * Poll until the controller's ready bit equals @rdy. The controller sets the
+ * bit after the driver enables the controller, and clears it after the driver
+ * disables the controller. The controller reports the longest time that this
+ * can take, in units of 500 ms. Wait that long, or 5 seconds if the controller
+ * reports less.
+ */
+static int nvme_wait_rdy(struct vfio_pci_device *device, bool rdy)
+{
+	void *bar0 = device->bars[0].vaddr;
+	int timeout_ms;
+	u32 csts;
+	u64 cap;
+
+	cap = readq(bar0 + NVME_REG_CAP);
+	timeout_ms = max(NVME_TIMEOUT_MS,
+			 NVME_CAP_TIMEOUT(cap) * NVME_CAP_TIMEOUT_UNIT_MS);
+
+	for (;;) {
+		csts = readl(bar0 + NVME_REG_CSTS);
+		/* A function that does not respond returns all ones. */
+		if (csts == ~0U)
+			return -ENODEV;
+		if (rdy && (csts & NVME_CSTS_CFS))
+			return -EIO;
+		if (!!(csts & NVME_CSTS_RDY) == rdy)
+			return 0;
+		if (timeout_ms-- == 0)
+			return -ETIMEDOUT;
+		usleep(NVME_POLL_INTERVAL_US);
+	}
+}
+
+static int nvme_disable_ctrl(struct vfio_pci_device *device)
+{
+	void *bar0 = device->bars[0].vaddr;
+	u32 cc;
+
+	cc = readl(bar0 + NVME_REG_CC);
+	if (cc & NVME_CC_ENABLE)
+		writel(cc & ~NVME_CC_ENABLE, bar0 + NVME_REG_CC);
+
+	return nvme_wait_rdy(device, false);
+}
+
+static void nvme_enable_ctrl(struct vfio_pci_device *device)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	void *bar0 = device->bars[0].vaddr;
+	u32 aqa, cc;
+	u64 cap;
+	int ret;
+
+	vfio_pci_cmd_set(device, PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
+			 PCI_COMMAND_INTX_DISABLE);
+
+	ret = nvme_disable_ctrl(device);
+	VFIO_ASSERT_EQ(ret, 0, "Controller did not disable (CSTS 0x%x)",
+		       readl(bar0 + NVME_REG_CSTS));
+
+	cap = readq(bar0 + NVME_REG_CAP);
+	state->db_stride = 4 << NVME_CAP_STRIDE(cap);
+
+	nvme_queue_init(device, &state->adminq, NVME_ADMIN_QID,
+			NVME_ADMIN_QUEUE_DEPTH,
+			offsetof(struct nvme_state, admin_sq),
+			offsetof(struct nvme_state, admin_cq));
+	nvme_queue_init(device, &state->msiq, NVME_MSI_QID, NVME_MSI_QUEUE_DEPTH,
+			offsetof(struct nvme_state, msi_sq),
+			offsetof(struct nvme_state, msi_cq));
+
+	/*
+	 * No command creates the admin queue. Its depth and ring addresses go
+	 * into controller registers instead.
+	 */
+	aqa = (NVME_ADMIN_QUEUE_DEPTH - 1) << 16 | (NVME_ADMIN_QUEUE_DEPTH - 1);
+	writel(aqa, bar0 + NVME_REG_AQA);
+	writeq(to_iova(device, state->admin_sq), bar0 + NVME_REG_ASQ);
+	writeq(to_iova(device, state->admin_cq), bar0 + NVME_REG_ACQ);
+
+	/*
+	 * Configure the NVM command set, 4 KiB memory pages, round-robin
+	 * arbitration, and 64-byte SQ and 16-byte CQ entries. The PRP math in
+	 * nvme_set_prp() depends on 4 KiB pages. Then enable the controller.
+	 */
+	cc = NVME_CC_CSS_NVM | NVME_CC_AMS_RR | NVME_CC_IOSQES | NVME_CC_IOCQES;
+	writel(cc, bar0 + NVME_REG_CC);
+	writel(cc | NVME_CC_ENABLE, bar0 + NVME_REG_CC);
+
+	ret = nvme_wait_rdy(device, true);
+	VFIO_ASSERT_EQ(ret, 0, "Controller did not become ready (CSTS 0x%x)",
+		       readl(bar0 + NVME_REG_CSTS));
+}
+
+static void nvme_create_io_queue(struct vfio_pci_device *device,
+				 struct nvme_queue *q, bool irq_enabled)
+{
+	u16 flags = NVME_QUEUE_PHYS_CONTIG;
+	struct nvme_command cmd = {};
+
+	if (irq_enabled)
+		flags |= NVME_CQ_IRQ_ENABLED;
+
+	cmd.create_cq.opcode = nvme_admin_create_cq;
+	cmd.create_cq.prp1 = cpu_to_le64(to_iova(device, nvme_cq(device, q)));
+	cmd.create_cq.cqid = cpu_to_le16(q->qid);
+	cmd.create_cq.qsize = cpu_to_le16(q->depth - 1);
+	cmd.create_cq.cq_flags = cpu_to_le16(flags);
+	cmd.create_cq.irq_vector = cpu_to_le16(irq_enabled ? device->driver.msi : 0);
+	nvme_submit_admin_cmd(device, &cmd);
+
+	memset(&cmd, 0, sizeof(cmd));
+	cmd.create_sq.opcode = nvme_admin_create_sq;
+	cmd.create_sq.prp1 = cpu_to_le64(to_iova(device, nvme_sq(device, q)));
+	cmd.create_sq.sqid = cpu_to_le16(q->qid);
+	cmd.create_sq.qsize = cpu_to_le16(q->depth - 1);
+	cmd.create_sq.sq_flags = cpu_to_le16(NVME_QUEUE_PHYS_CONTIG);
+	cmd.create_sq.cqid = cpu_to_le16(q->qid);
+	nvme_submit_admin_cmd(device, &cmd);
+}
+
+/*
+ * Split the region from poll_rings to its end evenly between the polled
+ * queues. Each queue gets the largest depth that fits, up to the largest
+ * queue size that the controller supports.
+ */
+static void nvme_init_pollqs(struct vfio_pci_device *device)
+{
+	u64 offset = offsetof(struct nvme_state, poll_rings);
+	struct nvme_state *state = to_nvme_state(device);
+	u64 bytes, sq_bytes, cq_bytes, cap;
+	u32 depth, i;
+
+	cap = readq(device->bars[0].vaddr + NVME_REG_CAP);
+	bytes = (device->driver.region.size - offset) / state->nr_pollqs;
+	VFIO_ASSERT_GT(bytes, 2 * NVME_CTRL_PAGE_SIZE);
+	depth = min_t(u64, NVME_CAP_MQES(cap) + 1,
+		      (bytes - 2 * NVME_CTRL_PAGE_SIZE) /
+		      (sizeof(struct nvme_command) +
+		       sizeof(struct nvme_completion)));
+	VFIO_ASSERT_GT(depth, 1);
+
+	sq_bytes = ALIGN((u64)depth * sizeof(struct nvme_command),
+			 NVME_CTRL_PAGE_SIZE);
+	cq_bytes = ALIGN((u64)depth * sizeof(struct nvme_completion),
+			 NVME_CTRL_PAGE_SIZE);
+
+	for (i = 0; i < state->nr_pollqs; i++) {
+		nvme_queue_init(device, &state->pollq[i], NVME_MSI_QID + 1 + i,
+				depth, offset, offset + sq_bytes);
+		offset += sq_bytes + cq_bytes;
+	}
+}
+
+static void nvme_create_io_queues(struct vfio_pci_device *device)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	u32 granted, i;
+	u32 last_qid;
+	u64 db_end;
+
+	granted = nvme_set_num_queues(device, 1 + NVME_MAX_POLL_QUEUES);
+	VFIO_ASSERT_GE(granted, 2,
+		       "Controller granted %u I/O queue pairs, need at least 2 (1 MSI + 1 polled)",
+		       granted);
+	state->nr_pollqs = min_t(u32, granted - 1, NVME_MAX_POLL_QUEUES);
+
+	/* The CQ head doorbell of the last queue is the highest one. */
+	last_qid = NVME_MSI_QID + state->nr_pollqs;
+	db_end = nvme_db_offset(state, last_qid, true) + sizeof(u32);
+	VFIO_ASSERT_LE(db_end, device->bars[0].info.size);
+
+	nvme_init_pollqs(device);
+
+	nvme_create_io_queue(device, &state->msiq, true);
+	for (i = 0; i < state->nr_pollqs; i++)
+		nvme_create_io_queue(device, &state->pollq[i], false);
+}
+
+/* Use the first active namespace for the memcpy commands. */
+static void nvme_find_nsid(struct vfio_pci_device *device)
+{
+	struct nvme_state *state = to_nvme_state(device);
+
+	/* The list holds the active NSIDs above 0, in increasing order. */
+	nvme_identify(device, 0, NVME_ID_CNS_NS_ACTIVE_LIST, state->ns_list);
+	state->nsid = le32_to_cpu(state->ns_list[0]);
+	VFIO_ASSERT_NE(state->nsid, 0, "Controller has no active namespace");
+}
+
+/*
+ * Record the logical block size of the namespace. The LBA Format that FLBAS
+ * selects reports it as a power-of-two shift.
+ */
+static void nvme_set_ns_geometry(struct nvme_state *state)
+{
+	struct nvme_lbaf *lbaf;
+
+	lbaf = &state->id_ns.lbaf[nvme_lbaf_index(state->id_ns.flbas)];
+
+	VFIO_ASSERT_NE(le64_to_cpu(state->id_ns.nsze), 0,
+		       "Namespace %u has size 0", state->nsid);
+	VFIO_ASSERT_EQ(le16_to_cpu(lbaf->ms), 0,
+		       "Namespace %u has metadata, format it without metadata",
+		       state->nsid);
+	VFIO_ASSERT_GE(lbaf->ds, NVME_MIN_LBA_SHIFT,
+		       "Namespace %u has an invalid LBA data size", state->nsid);
+	VFIO_ASSERT_LE(lbaf->ds, NVME_MAX_LBA_SHIFT,
+		       "Namespace %u has logical blocks larger than 4 KiB",
+		       state->nsid);
+
+	state->lba_shift = lbaf->ds;
+}
+
+static void nvme_set_limits(struct vfio_pci_device *device)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	u64 max_size, nsze;
+	u8 mdts;
+
+	/*
+	 * The controller reports the largest transfer of one command as a
+	 * power-of-two number of its smallest memory pages, and 0 means no
+	 * limit. nvme_probe() checks that this page is 4 KiB. Both limits are
+	 * powers of two of at least 4 KiB, so the result is a whole number of
+	 * logical blocks.
+	 */
+	mdts = state->id_ctrl.mdts;
+	max_size = NVME_MAX_XFER_SIZE;
+	if (mdts && mdts < 32)
+		max_size = min_t(u64, max_size, (u64)NVME_CTRL_PAGE_SIZE << mdts);
+
+	/* All memcpy operations use the logical blocks after the header. */
+	nsze = le64_to_cpu(state->id_ns.nsze) << state->lba_shift;
+	VFIO_ASSERT_GT(nsze, NVME_SCRATCH_HDR_SIZE,
+		       "Namespace %u is too small", state->nsid);
+	max_size = min_t(u64, max_size, nsze - NVME_SCRATCH_HDR_SIZE);
+
+	device->driver.max_memcpy_size = max_size;
+	/*
+	 * A ring of depth entries holds at most depth - 1 commands. A memcpy
+	 * of count copies has 2 * count - 1 commands in flight.
+	 */
+	device->driver.max_memcpy_count = (state->nr_pollqs *
+					   (state->pollq[0].depth - 1) + 1) / 2;
+}
+
+static void nvme_init(struct vfio_pci_device *device)
+{
+	struct nvme_state *state = to_nvme_state(device);
+
+	VFIO_ASSERT_GE(device->driver.region.size, sizeof(*state));
+	memset(state, 0, sizeof(*state));
+
+	nvme_enable_irqs(device);
+	nvme_enable_ctrl(device);
+
+	nvme_identify(device, 0, NVME_ID_CNS_CTRL, &state->id_ctrl);
+	nvme_find_nsid(device);
+	nvme_identify(device, state->nsid, NVME_ID_CNS_NS, &state->id_ns);
+	nvme_set_ns_geometry(state);
+	nvme_create_io_queues(device);
+	nvme_set_limits(device);
+}
+
+static void nvme_remove(struct vfio_pci_device *device)
+{
+	nvme_disable_ctrl(device);
+	vfio_pci_cmd_clear(device, PCI_COMMAND_MASTER);
+	vfio_pci_msix_disable(device);
+}
+
+/*
+ * Return the controller to a working state after a failed command. On some
+ * controllers, a DMA read that the IOMMU blocks, which the unmapped-IOVA tests
+ * cause on purpose, leaves the submission queue in an error state. A Function
+ * Level Reset clears it. Disable the controller first so it stops DMA before
+ * the reset arrives.
+ */
+static void nvme_recover(struct vfio_pci_device *device)
+{
+	nvme_disable_ctrl(device);
+	vfio_pci_device_reset(device);
+	vfio_pci_msix_reenable(device, 0, device->driver.msi + 1);
+	nvme_enable_ctrl(device);
+	nvme_create_io_queues(device);
+}
+
+/*
+ * prp1 points at @buf. If the transfer reaches one more page, prp2 points at
+ * it. If it reaches more, prp2 points at @prp_list, which holds the address
+ * of each page after the first. The driver configures the controller for
+ * 4 KiB memory pages.
+ */
+static void nvme_set_prp(struct vfio_pci_device *device,
+			 struct nvme_command *cmd, iova_t buf, u64 size,
+			 __le64 *prp_list)
+{
+	u64 first = NVME_CTRL_PAGE_SIZE - (buf & (NVME_CTRL_PAGE_SIZE - 1));
+	u64 nr_entries, i;
+
+	VFIO_ASSERT_TRUE(IS_ALIGNED(buf, 4), "PRP1 0x%lx is not dword aligned",
+			 buf);
+	cmd->rw.dptr.prp1 = cpu_to_le64(buf);
+	if (size <= first)
+		return;
+
+	nr_entries = DIV_ROUND_UP(size - first, NVME_CTRL_PAGE_SIZE);
+	VFIO_ASSERT_LE(nr_entries, NVME_PRP_LIST_ENTRIES);
+	for (i = 0; i < nr_entries; i++)
+		prp_list[i] = cpu_to_le64(buf + first + i * NVME_CTRL_PAGE_SIZE);
+
+	if (nr_entries == 1)
+		cmd->rw.dptr.prp2 = prp_list[0];
+	else
+		cmd->rw.dptr.prp2 = cpu_to_le64(to_iova(device, prp_list));
+}
+
+/*
+ * Build a read or write command covering @size bytes from byte @offset,
+ * transferring to or from @buf. Both @offset and @size must be aligned to the
+ * logical block size.
+ */
+static void nvme_prep_rw_cmd(struct vfio_pci_device *device,
+			     struct nvme_command *cmd, u8 opcode, u64 offset,
+			     iova_t buf, u64 size, __le64 *prp_list)
+{
+	struct nvme_state *state = to_nvme_state(device);
+
+	VFIO_ASSERT_GT(size, 0);
+	VFIO_ASSERT_TRUE(IS_ALIGNED(offset, 1ULL << state->lba_shift));
+	VFIO_ASSERT_TRUE(IS_ALIGNED(size, 1ULL << state->lba_shift));
+
+	memset(cmd, 0, sizeof(*cmd));
+	cmd->rw.opcode = opcode;
+	cmd->rw.nsid = cpu_to_le32(state->nsid);
+	cmd->rw.slba = cpu_to_le64(offset >> state->lba_shift);
+	nvme_set_prp(device, cmd, buf, size, prp_list);
+	cmd->rw.length = cpu_to_le16((size >> state->lba_shift) - 1);
+}
+
+static int nvme_io_wait(struct vfio_pci_device *device, struct nvme_queue *q)
+{
+	struct nvme_completion cqe;
+	int ret;
+
+	ret = nvme_cq_wait(device, q, &cqe);
+	if (ret)
+		return ret;
+
+	return nvme_status(&cqe) ? -EIO : 0;
+}
+
+/* Synchronously read or write @size bytes at byte @offset of the namespace. */
+static int nvme_scratch_rw(struct vfio_pci_device *device, u8 opcode,
+			   u64 offset, u64 size)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	struct nvme_command cmd;
+
+	nvme_prep_rw_cmd(device, &cmd, opcode, offset,
+			 to_iova(device, state->scratch_buf), size,
+			 state->prp_src);
+	nvme_sq_submit(device, &state->pollq[0], &cmd);
+
+	return nvme_io_wait(device, &state->pollq[0]);
+}
+
+static bool nvme_scratch_buf_is_zero(struct nvme_state *state, u64 size)
+{
+	u64 *p = (u64 *)state->scratch_buf;
+	u64 i;
+
+	for (i = 0; i < size / sizeof(*p); i++) {
+		if (p[i])
+			return false;
+	}
+
+	return true;
+}
+
+/*
+ * Refuse to overwrite data that the driver did not put there. The namespace
+ * is scratch space if it starts with a header that covers max_memcpy_size.
+ * Otherwise, claim it by checking that every byte the driver could write is
+ * zero, and then writing the header. If the header covers less than
+ * max_memcpy_size, only the bytes past what it covers need to be zero.
+ */
+static void nvme_claim_scratch(struct vfio_pci_device *device)
+{
+	struct nvme_scratch_header *hdr = (void *)to_nvme_state(device)->scratch_buf;
+	struct nvme_state *state = to_nvme_state(device);
+	u64 max_size = device->driver.max_memcpy_size;
+	u64 start = 0, end, offset, size, chunk;
+	struct nvme_command cmd;
+	int ret;
+
+	ret = nvme_scratch_rw(device, nvme_cmd_read, 0, NVME_SCRATCH_HDR_SIZE);
+	VFIO_ASSERT_EQ(ret, 0, "Failed to read the scratch header");
+
+	if (!memcmp(hdr->magic, NVME_SCRATCH_MAGIC, sizeof(NVME_SCRATCH_MAGIC))) {
+		if (le64_to_cpu(hdr->size) >= max_size)
+			return;
+
+		start = NVME_SCRATCH_HDR_SIZE + le64_to_cpu(hdr->size);
+	}
+
+	chunk = min_t(u64, NVME_SCRATCH_CHUNK, max_size);
+	end = NVME_SCRATCH_HDR_SIZE + max_size;
+	for (offset = start; offset < end; offset += size) {
+		size = min_t(u64, end - offset, chunk);
+
+		ret = nvme_scratch_rw(device, nvme_cmd_read, offset, size);
+		VFIO_ASSERT_EQ(ret, 0, "Failed to read namespace %u",
+			       state->nsid);
+
+		VFIO_ASSERT_TRUE(nvme_scratch_buf_is_zero(state, size),
+				 "Namespace %u: bytes [0x%lx, 0x%lx) are not all zero.\n"
+				 "The NVMe driver only writes to zeroed space, to avoid\n"
+				 "destroying data. Zero that range to allow it, e.g.\n"
+				 "  blkdiscard -z -l %lu /dev/nvmeXn%u\n",
+				 state->nsid, start, end, end, state->nsid);
+	}
+
+	memset(state->scratch_buf, 0, NVME_SCRATCH_HDR_SIZE);
+	memcpy(hdr->magic, NVME_SCRATCH_MAGIC, sizeof(NVME_SCRATCH_MAGIC));
+	hdr->size = cpu_to_le64(max_size);
+
+	ret = nvme_scratch_rw(device, nvme_cmd_write, 0, NVME_SCRATCH_HDR_SIZE);
+	VFIO_ASSERT_EQ(ret, 0, "Failed to write the scratch header");
+
+	/* Make sure the header survives a reset or FLR of the controller. */
+	memset(&cmd, 0, sizeof(cmd));
+	cmd.common.opcode = nvme_cmd_flush;
+	cmd.common.nsid = cpu_to_le32(state->nsid);
+	nvme_sq_submit(device, &state->pollq[0], &cmd);
+	VFIO_ASSERT_EQ(nvme_io_wait(device, &state->pollq[0]), 0,
+		       "Failed to flush the scratch header");
+}
+
+/*
+ * Copy @src to @dst through the logical blocks after the scratch header.
+ * A write stores @src first. Then the driver queues @count reads of those
+ * blocks into @dst along with @count - 1 more writes of @src, spread over the
+ * polled queues.
+ *
+ * Unlike the vfio_pci_driver_ops contract, this function does not return at
+ * once. It waits for the first write to complete. The controller can run the
+ * commands of a queue in any order, and the reads must see the data of the
+ * write. The later writes store the same data, so every read sees @src. All of
+ * these commands are in flight when this function returns.
+ */
+static void nvme_memcpy_start(struct vfio_pci_device *device,
+			      iova_t src, iova_t dst, u64 size, u64 count)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	u64 nr = count ? 2 * count - 1 : 0;
+	struct nvme_command write, read;
+	u64 i;
+
+	if (!state->scratch_claimed) {
+		nvme_claim_scratch(device);
+		state->scratch_claimed = true;
+	}
+
+	nvme_prep_rw_cmd(device, &write, nvme_cmd_write, NVME_SCRATCH_HDR_SIZE,
+			 src, size, state->prp_src);
+	nvme_sq_submit(device, &state->pollq[0], &write);
+	state->memcpy_err = nvme_io_wait(device, &state->pollq[0]);
+	if (state->memcpy_err)
+		return;
+
+	nvme_prep_rw_cmd(device, &read, nvme_cmd_read, NVME_SCRATCH_HDR_SIZE,
+			 dst, size, state->prp_dst);
+	for (i = 0; i < nr; i++)
+		nvme_sq_push(device, &state->pollq[i % state->nr_pollqs],
+			     i % 2 ? &write : &read);
+	for (i = 0; i < min_t(u64, nr, state->nr_pollqs); i++)
+		nvme_ring_sq_db(device, &state->pollq[i]);
+}
+
+static int nvme_memcpy_wait(struct vfio_pci_device *device)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	int ret = state->memcpy_err;
+	u32 i;
+
+	for (i = 0; !ret && i < state->nr_pollqs; i++) {
+		while (!ret && state->pollq[i].inflight)
+			ret = nvme_io_wait(device, &state->pollq[i]);
+	}
+
+	if (ret)
+		nvme_recover(device);
+
+	return ret;
+}
+
+static void nvme_send_msi(struct vfio_pci_device *device)
+{
+	struct nvme_state *state = to_nvme_state(device);
+	struct nvme_command cmd = {};
+
+	cmd.common.opcode = nvme_cmd_flush;
+	cmd.common.nsid = cpu_to_le32(state->nsid);
+
+	nvme_sq_submit(device, &state->msiq, &cmd);
+	VFIO_ASSERT_EQ(nvme_io_wait(device, &state->msiq), 0);
+}
+
+const struct vfio_pci_driver_ops nvme_ops = {
+	.name = "nvme",
+	.region_size = sizeof(struct nvme_state) + NVME_POLL_RINGS_SIZE,
+	.probe = nvme_probe,
+	.init = nvme_init,
+	.remove = nvme_remove,
+	.memcpy_start = nvme_memcpy_start,
+	.memcpy_wait = nvme_memcpy_wait,
+	.send_msi = nvme_send_msi,
+};
diff --git a/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h
new file mode 120000
index 000000000000..32258e9c9311
--- /dev/null
+++ b/tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h
@@ -0,0 +1 @@
+../../../../../../../include/linux/nvme.h
\ No newline at end of file
diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/selftests/vfio/lib/libvfio.mk
index c9202665c7f0..2a7dac3730b8 100644
--- a/tools/testing/selftests/vfio/lib/libvfio.mk
+++ b/tools/testing/selftests/vfio/lib/libvfio.mk
@@ -20,6 +20,7 @@ endif
 LIBVFIO_C += drivers/mlx5/mlx5.c
 LIBVFIO_C += drivers/nv_falcon/nv_falcon.c
 LIBVFIO_C += drivers/igb/igb.c
+LIBVFIO_C += drivers/nvme/nvme.c
 
 LIBVFIO_OUTPUT := $(OUTPUT)/libvfio
 
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_driver.c b/tools/testing/selftests/vfio/lib/vfio_pci_driver.c
index b31abfe2c7ff..f54b4ac505dd 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_driver.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_driver.c
@@ -9,6 +9,7 @@ extern struct vfio_pci_driver_ops ioat_ops;
 extern struct vfio_pci_driver_ops mlx5st_ops;
 extern struct vfio_pci_driver_ops nv_falcon_ops;
 extern struct vfio_pci_driver_ops igb_ops;
+extern struct vfio_pci_driver_ops nvme_ops;
 
 static struct vfio_pci_driver_ops *driver_ops[] = {
 #ifdef __x86_64__
@@ -18,6 +19,7 @@ static struct vfio_pci_driver_ops *driver_ops[] = {
 	&mlx5st_ops,
 	&nv_falcon_ops,
 	&igb_ops,
+	&nvme_ops,
 };
 
 void vfio_pci_driver_probe(struct vfio_pci_device *device)
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-04  1:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04  1:52 [PATCH v2] vfio: selftests: Add NVMe driver Josh Hilke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®