crates

non_volatile_memory_express

check proofs license minimum Rust version

The queue mechanics of an NVMe (Non-Volatile Memory Express) driver, as pure logic. It covers register field decode, command building, submission and completion ring arithmetic with the phase tag, doorbell addressing, Physical Region Page (PRP) construction and Identify namespace and controller parsing. It follows revision 1.4 of the NVMe base specification. Every section number in this crate's documentation is that revision's. The crate is no_std, needs no allocator and performs no I/O. You supply the volatile memory-mapped input/output (MMIO) accessors, the direct memory access (DMA) memory and the timing (reset, enable, timeouts). The crate computes every value you write and checks every value the controller hands back.

A PRP entry is a physical address of one page of a transfer's data. A command carries two of them, PRP1 and PRP2. A longer transfer points PRP2 at a list of more entries, which physical_region_page_list builds. A logical block address (LBA) numbers the fixed-size blocks of a namespace, counting from 0. A block is the unit of storage a controller exposes.

EXAMPLES

To build and test, install Rust 1.85 or newer (for example with rustup), then run cargo build and cargo test from the crate's directory. The test run also compiles the example below as a documentation test.

use non_volatile_memory_express::{
    Command, Completion, CompletionQueue, Doorbell, SubmissionQueue, admin_queue_attributes,
    doorbell, physical_region_page_pair,
};

// Bring-up writes the admin ring sizes to the Admin Queue Attributes register before it enables
// the controller; the driver's own admin rings use the same counts.
assert_eq!(admin_queue_attributes(32, 32), 0x001f_001f);

let mut submission_queue = SubmissionQueue::new(64);
let mut completion_queue = CompletionQueue::new(64);

// Read eight 512-byte blocks from LBA 0 of namespace 1 into a buffer that straddles a page.
let pages = physical_region_page_pair(0x4000_0800, 4096, 4096).expect("two pages suffice");
// Command identifier 1, namespace 1, starting logical block 0, eight blocks.
let read = Command::read(1, 1, 0, 8, pages);
// Write `read.0` into submission slot `slot`, issue a write barrier, then ring the tail:
let slot = submission_queue.push().expect("the ring has room");
// The third argument is the raw `CAP.DSTRD` field, `Capabilities::doorbell_stride_exponent`, not bytes.
assert_eq!(doorbell(1, Doorbell::SubmissionTail, 0), 0x1008); // write the tail here
assert_eq!((slot, submission_queue.tail()), (0, 1));
assert_eq!(read.0[1], 1);

// Poll dword 3 of completion slot `completion_queue.head()` until its phase bit (bit 16) carries
// the expected phase, issue a read barrier, then read dwords 0 to 2. Here the controller reports
// submission queue head 1 (it consumed the command in slot 0) for submission queue 1, and echoes
// command identifier 1 with status 0.
let dword_three = 1 | (1 << 16);
assert_eq!(Completion::is_phase_tag_set(dword_three), completion_queue.expects_phase_tag_set());
let done = Completion::from_dwords([0, 0, 1 | (1 << 16), dword_three]);
assert!(completion_queue.is_owned(&done)); // the phase tag only
// Ownership says the entry is fresh, not that it answers a command this driver issued: match the
// identifiers against the commands in flight.
assert!(done.submission_queue_id == 1 && done.command_id == 1 && done.is_success());
// The head comes from the controller, so it is checked: an impossible one is an error to handle
// (here, by giving up), never a panic.
submission_queue
    .record_head(done.submission_queue_head)
    .expect("the controller consumed the command it was given");
// Write the new head to doorbell(1, Doorbell::CompletionHead, stride).
assert_eq!(completion_queue.pop(), 1);

examples/software_controller.rs runs the whole loop against a controller modeled over two Vecs: it drives both rings through three laps, checks each identifier with CommandIdentifierSet and each reported head, and refuses one impossible head at the end. cargo test runs it, and cargo run --example software_controller prints the heads. Copy its SoftwareController as a test double.

examples/qemu_driver.rs runs the same loop against QEMU's emulated NVMe controller, from Linux user space: bring-up, Identify, one I/O queue pair, a read, a write, a flush and a clean shutdown. It refuses any controller but QEMU's. docs/stranger-test.md gives the commands that build it, boot it under qemu-system-x86_64 and check the disk image afterwards.

What is proven

To run the proofs, install Kani as its installation guide says: cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup once (it downloads a pinned nightly toolchain). Run cargo kani from the crate's directory. The harnesses pass on Kani 0.68.0; all eighteen take about 75 seconds in total, compile included, after the setup. To run one, add --harness <name>. Eighteen harnesses live in the verification module:

Each harness has a falsification record at falsifications/<module.path>.<harness>.patch. Apply one with patch -p1 < falsifications/<module.path>.<harness>.patch from the crate root, then run cargo kani --harness <harness>. The patch makes that harness fail; its header names the assertion it expects to break. Kani may report a message it cannot print (it shows a formatted assertion message as a placeholder), or an additional overflow check alongside the named assertion. Undo the patch with patch -R -p1 < <the same patch>. The failure shows that the proof checks something.

Memory ordering

The crate performs no memory access, so the caller owns the ordering both rings need, and on a weakly ordered host (Arm, RISC-V) it needs barriers. On the submission side, issue a write barrier between the stores of the 64-byte entry and the write of the tail doorbell, or the controller may fetch a stale entry. On the completion side, read dword 3 of the entry first and test its phase bit (bit 16) against the phase CompletionQueue::is_owned expects. Once it matches, issue a read barrier, then read dwords 0 to 2, and decode all four with Completion::from_dwords. Reading all four dwords before the phase test lets the loads of dwords 0 to 2 run ahead of the controller's write while dword 3 follows it, and the fresh phase then carries a stale head and command identifier.

Bring-up and shutdown

The crate computes the register values, and the caller writes them in the order the specification sets (section 7.6.1). The offsets are in the registers module.

  1. Clear the enable bit (write 0 to Controller Configuration), then wait for Controller Status to clear CONTROLLER_STATUS_READY, for at most Capabilities::timeout_in_500_millisecond_units times 500 milliseconds.
  2. Write admin_queue_attributes to the Admin Queue Attributes register, then the physical addresses of the admin submission and completion rings to their two base address registers.
  3. Write enabled_controller_configuration for a page size between Capabilities::minimum_page_size_bytes and Capabilities::maximum_page_size_bytes, then wait for CONTROLLER_STATUS_READY, giving up on CONTROLLER_STATUS_FATAL.
  4. Identify the controller and the namespace, then create each I/O completion queue before the submission queue that completes into it.
  5. To shut down, write controller_configuration_with_shutdown_notification of the value written in step 3, and wait for CONTROLLER_STATUS_SHUTDOWN_COMPLETE before removing power.

examples/qemu_driver.rs follows these steps against a real controller.

BUGS

The public structs that a later release may widen (Completion, IdentifyController, IdentifyNamespace and Namespace) are #[non_exhaustive]. Build a Namespace from the Identify answer with Namespace::from_identify, or with Namespace::new when there is no answer to read, and read the other three from the controller's answers. Before Identify has answered, a driver holds Option<IdentifyNamespace>; no placeholder value of the type stands for a namespace nobody has read.

How it differs from Linux and the Rust drivers

The Linux driver (drivers/nvme/host) owns the whole data path: a tag set per queue, PRP lists and scatter-gather lists, many queue pairs, interrupts, reset, timeouts, shutdown and the admin command set. The polled user-space driver of the Storage Performance Development Kit (SPDK) does the same outside the kernel. Rust driver crates such as nvme-driver own their direct memory access and issue the commands themselves, and Redox nvmed is a driver service of its own. This crate is none of those. It builds one command at a time and walks one ring pair, as pure logic with no I/O, no allocator, no unsafe and no dependency, so it compiles for any target and the model checker reaches all of it. It checks what the controller hands back instead of trusting it: a submission queue head the controller cannot have reached is an error, not a recorded value. So is a completion for a command identifier CommandIdentifierSet does not hold, which stands in for the tag set.

What that costs you: you write the volatile register accessors, the direct memory access allocation, the admin bring-up sequence and every wait, and you acquire and release each command identifier yourself, on push and on its completion. A PRP list reaches as far as one list page, since the crate does not chain list pages. The crate gives the shutdown values (controller_configuration_with_shutdown_notification and the two CONTROLLER_STATUS_SHUTDOWN_* bits), and the driver writes the one and waits for the other before it removes power.

Minimum Rust version

Rust 1.85, the rust-version in Cargo.toml. To check it, run rustup toolchain install 1.85 --profile minimal, then cargo +1.85 test. For the bare-metal build, add the target with rustup target add --toolchain 1.85 aarch64-unknown-none-softfloat and run cargo +1.85 build --target aarch64-unknown-none-softfloat.

License

Licensed under either of MIT (LICENSE-MIT) or Apache-2.0 (LICENSE-APACHE), at your option.