designware_ethernet
The logic of a driver for the Synopsys DesignWare Ethernet QoS controller (dwmac 4.x and 5.x
descriptor format) and its Motorcomm YT8531 physical layer device. It holds the register map, the descriptor formats, the direct memory access (DMA)
region's layout, the receive and transmit rings, and the controller and physical layer device bring-up sequences. It
also holds MDIO (management data input/output) and a probe that measures whether device DMA is
coherent with the CPU's caches. It knows no system-on-chip: the register window, the bus
configuration (network_interface_controller::BusConfiguration) and the physical layer device's
settings come from the caller in bring_up::Inputs, and Settings::from_mainline converts the
mainline device-tree spelling of the delays and drive currents. It has run on a StarFive
VisionFive 2. It is no_std and needs no allocator.
It dereferences no pointer and touches no hardware. You supply register access through the
Hardware trait, the DMA region through DirectMemoryAccessRegion, and the tail-pointer writes and
barrier through Tails; the tests run all of it against a simulated controller. It does not support
the older 3.x GMAC (gigabit media access controller).
EXAMPLES
DataPlane runs the rings over a DMA region and tail-pointer writes you supply. Here the region
is ordinary memory and the tails go nowhere, so nothing answers the frame:
use designware_ethernet::{DataPlane, DirectMemoryAccessRegion, Tails, layout};
struct Memory(Vec<u8>);
impl DirectMemoryAccessRegion for Memory {
fn read_u64(&self, offset: u64) -> u64 {
let mut bytes = [0; 8];
self.read_bytes(offset, &mut bytes);
u64::from_le_bytes(bytes)
}
fn write_u64(&mut self, offset: u64, value: u64) {
self.write_bytes(offset, &value.to_le_bytes());
}
fn read_bytes(&self, offset: u64, out: &mut [u8]) {
out.copy_from_slice(&self.0[offset as usize..][..out.len()]);
}
fn write_bytes(&mut self, offset: u64, source: &[u8]) {
self.0[offset as usize..][..source.len()].copy_from_slice(source);
}
}
struct NoDevice;
impl Tails for NoDevice {
fn set_receive_tail(&mut self, _tail_pointer_low_bits: u32) {}
fn set_transmit_tail(&mut self, _tail_pointer_low_bits: u32) {}
fn barrier(&mut self) {}
}
let mut region = Memory(vec![0; layout::DIRECT_MEMORY_ACCESS_REGION_BYTES as usize]);
let mut tails = NoDevice;
let mut data_plane = DataPlane::new(0x4000_0000); // the region's physical address
data_plane.start(&mut region, &mut tails); // posts the receive buffers
data_plane.transmit(&mut region, &mut tails, &[0xff; 60]).unwrap();
let mut frame_buffer = [0u8; 1514];
assert_eq!(data_plane.receive(&mut region, &mut tails, &mut frame_buffer), None);
What is proven
Six Kani harnesses, in src/proofs.rs:
a_delivered_frame_always_fits_the_buffer_and_the_room: for every value a device can write into word 3 of a receive descriptor and every caller buffer size, a frame thatreceive_verdictaccepts fits the caller's buffer and is at mostMAXIMUM_FRAME_BYTESbytes. With its CRC (cyclic redundancy check), it fits the receive buffer.every_ring_offset_is_inside_the_region: for every index, each descriptor and buffer offset lies inside its own part of the DMA region.a_link_is_decoded_only_from_what_the_registers_say: for every value of the six clause 22 registers the decode reads, it returns a link only when the status bit is up and the physical layer device is not isolated. With autonegotiation on, it returns one only when autonegotiation is complete, at a speed and duplex both ends advertise; with it off, at the forced duplex.a_transmit_descriptor_keeps_its_length_inside_its_fields: for every buffer address and length, the transmit descriptor carries the address untouched, the device owns it, it is the first and last segment, and the length fits its two fields without reaching a flag bit.a_management_data_io_command_word_keeps_each_bus_address_in_its_own_field: for every physical layer device and register address, the command word holds each in its own five-bit field, says read or write as asked and sets the busy bit.a_coherent_verdict_needs_every_account_to_agree: for every observation, a fatal bus error gives the bus error verdict, and the coherent verdict needs the receive descriptor handed back, the payload to match the probe frame and the delivered length to be the probe's.
Running the proofs
The proofs need Kani, which rustup does not install. Run cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup. The setup step downloads a model
checker and a pinned nightly Rust toolchain, so it needs network access. CI uses version 0.68.0,
and another version may verify differently.
Run them all from the crate root with cargo kani, or one of them with
cargo kani --harness <name>, where <name> is a harness name from the list above. On a
passing run, all six finish in about five seconds once the crate has compiled. The first
cargo install and cargo kani setup take minutes.
A passing run still prints alarming things. Around seventeen compiler warnings, "trailing
semicolon in macro used in expression position", come from the crate's compile-time assertions
and are harmless. The log lists each check, then ends with VERIFICATION:- SUCCESSFUL and
Complete - 6 successfully verified harnesses, 0 failures, 6 total. Anything else is a real
failure.
Running one falsification patch
Each harness has a falsification record at falsifications/<module.path>.<harness>.patch. Applying
it with patch -p1 < <file> from the crate root makes that harness fail at its own assertion, which
shows the proof checks something; the patch header names the assertion. The file name is the
module proofs, then the harness name, so the patch for every_ring_offset_is_inside_the_region
is falsifications/proofs.every_ring_offset_is_inside_the_region.patch. You need the patch
program. For example:
patch -p1 < falsifications/proofs.every_ring_offset_is_inside_the_region.patch
cargo kani --harness every_ring_offset_is_inside_the_region
patch -R -p1 < falsifications/proofs.every_ring_offset_is_inside_the_region.patch
git status --short # prints nothing once the patch is out
The middle command is meant to fail: it prints a Failed Checks: line naming the assertion,
VERIFICATION:- FAILED, and Complete - 0 successfully verified harnesses, 1 failures, 1 total.,
and exits with a non-zero status. A run that passes means the patch did not apply or the proof
checks nothing.
MDIO sequencing, the physical layer device configuration writes and controller bring-up have unit tests against the simulator but no proof.
Building and testing
cargo build and cargo test need no setup beyond a Rust toolchain at the minimum version below.
cargo test runs the unit tests against the simulated controller and the example above as a
documentation test. The crate is no_std, so cargo build --target riscv64imac-unknown-none-elf
builds it for the bare-metal board target once rustup target add riscv64imac-unknown-none-elf has
installed it. No feature flag is needed for any of these commands. The Kani proofs under "What is proven" are the only part that needs extra tools.
BUGS
- The tests run only against the simulator, not against a device.
- One receive and one transmit queue, polled: no interrupts, offloads, jumbo frames or multicast.
- The crate reads no device tree and names no system-on-chip. Finding the port's node, its bus configuration and its physical layer device's properties, and the clocks, resets and interface select around the controller, are the caller's.
- The API may still change until the driver built on it is complete.
Minimum Rust version
Rust 1.88. To check it, run rustup toolchain install 1.88 --profile minimal, then
cargo +1.88 build and cargo +1.88 test. Both pass.
License
You may use this crate under the MIT license (LICENSE-MIT) or the Apache License, version 2.0
(LICENSE-APACHE), at your option. Parts adapt OpenBSD's dwqe and ytphy drivers under the
Internet Systems Consortium (ISC) license, and the physical layer device's link decoding
(motorcomm_physical_layer_device) adapts ukphy_status (OpenBSD's sys/dev/mii/ukphy_subr.c)
under the NetBSD Foundation's BSD-2-Clause license. Both notices are in src/lib.rs.