vfio: selftests: Add NVMe driver
Subsystems (5)
PATCH 1/1 vfio: selftests: Add NVMe driver
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 the logical blocks at the start of the first active namespace. memcpy_start() writes @src to the logical blocks from LBA 0, waits for the write, and then queues @count reads of the same blocks into @dst. The driver must wait for the write because the controller can run the commands of a queue in any order. All @count reads are in flight when memcpy_start() returns. The driver spreads the reads over up to 16 such 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 overwrites the first max_memcpy_size bytes of the namespace. Do not use a device that holds data you want to keep.
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 MSI-X, 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"
---
Testing:
- Built VFIO and KVM selftests on x86_64 (dynamic and static) and VFIO selftests on arm64 (static).
- QEMU nvme (x86_64): vfio_pci_driver_test 35/35 passed (default and nsid=2); msix_qsize=1 and max_ioqpairs=1 failed at their expected assertions.
- Samsung PM175x VF (x86_64): vfio_pci_device_test 5/5, vfio_pci_driver_test 35/35, and KVM irq_test passed.
- Samsung PM174X VF (arm64): vfio_pci_device_test 5/5 and vfio_pci_driver_test 35/35 passed.
Documentation/driver-api/vfio-selftests.rst | 4 + .../selftests/vfio/lib/drivers/nvme/nvme.c | 745 ++++++++++++++++++ .../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, 753 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
Documentation/driver-api/vfio-selftests.rst +4 -0
@@ -39,10 +39,14 @@ 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 overwrites the start of the first active namespace. +Do not use an NVMe device that holds data you want to keep. + Setting up Devices ------------------
tools/testing/selftests/vfio/lib/drivers/nvme/nvme.c new file +745 -0
745 lines not on this page · raw message
tools/testing/selftests/vfio/lib/drivers/nvme/nvme.h new file +1 -0
@@ -0,0 +1 @@ +../../../../../../../include/linux/nvme.h
tools/testing/selftests/vfio/lib/libvfio.mk +1 -0
@@ -18,6 +18,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
tools/testing/selftests/vfio/lib/vfio_pci_driver.c +2 -0
@@ -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)
Signature · 1 line
2.56.1.831.g720f3f5937-goog