aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / tests / nvme_write_completion.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! External integration tests for the native NVMe write + flush completion lane. |
| 17 | //! |
| 18 | //! Target: `crates/aienos-kernel/tests/nvme_write_completion.rs`. |
| 19 | //! |
| 20 | //! These tests drive `aienos_kernel::nvme::driver::NvmeController` only through |
| 21 | //! its public API plus local `Registers`, `DmaMemory`, and `Delay` fakes. The |
| 22 | //! fake device is programmable: it records every submitted I/O command and can |
| 23 | //! inject crafted 16-byte completions (wrong status, wrong command id, wrong |
| 24 | //! phase, all-zero, or none at all). |
| 25 | //! |
| 26 | //! Reachability notes for the negative paths: |
| 27 | //! * `NvmeController::identify_namespace(nsid)` is public and takes an arbitrary |
| 28 | //! `nsid`, so the invalid-namespace error path is reachable and tested here. |
| 29 | //! * `BlockDevice::write_blocks` and `BlockDevice::flush` both funnel through the |
| 30 | //! private `transfer`/`submit_io` path, which hard-codes namespace id `1` when |
| 31 | //! it builds `Submission::write(1, ...)`. There is no public method that takes |
| 32 | //! an NSID for I/O, so a *write-side* invalid-namespace test is NOT reachable |
| 33 | //! today. It would require a new public method such as |
| 34 | //! `write_blocks_ns(nsid, lba, buffer)`; we deliberately do not add it here. |
| 35 | |
| 36 | use std::cell::RefCell; |
| 37 | use std::collections::VecDeque; |
| 38 | use std::rc::Rc; |
| 39 | |
| 40 | use aienos_kernel::block::{BlockDevice, BlockError}; |
| 41 | use aienos_kernel::nvme::driver::{ |
| 42 | Delay, DmaMemory, DmaRegion, NvmeController, NvmeError, ADMIN_DEPTH, |
| 43 | }; |
| 44 | use aienos_kernel::nvme::{ |
| 45 | doorbell_offset, Registers, Submission, CSTS_RDY, REG_ACQ, REG_ASQ, REG_CC, REG_CSTS, |
| 46 | }; |
| 47 | |
| 48 | const PAGE: usize = 4096; |
| 49 | /// CAP.DSTRD = 0 -> 4-byte doorbell stride; CAP.TO = 1 -> 500 ms ready budget; |
| 50 | /// CAP.MQES + 1 = 1024. |
| 51 | const CAP_LOW: u32 = 0x0100_03ff; |
| 52 | const STRIDE: u32 = 4; |
| 53 | const ADMIN_DOORBELL: u32 = doorbell_offset(0, false, STRIDE); |
| 54 | const IO_SQ_DOORBELL: u32 = doorbell_offset(1, false, STRIDE); |
| 55 | const NS_BLOCKS: u64 = 12_345; |
| 56 | /// The driver's I/O poll budget is `for _ in 0..=1000`, i.e. 1001 iterations. |
| 57 | const POLLS: u64 = 1_001; |
| 58 | const IO_DEPTH: u16 = 4; |
| 59 | |
| 60 | type Shared = Rc<RefCell<Device>>; |
| 61 | |
| 62 | /// One submitted I/O command, decoded from the SQ slot the driver filled. |
| 63 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 64 | struct IoCommand { |
| 65 | opcode: u32, |
| 66 | cid: u16, |
| 67 | nsid: u32, |
| 68 | lba: u64, |
| 69 | blocks: u32, |
| 70 | } |
| 71 | |
| 72 | #[derive(Clone, Copy, Default)] |
| 73 | struct Behavior { |
| 74 | /// Raw SC/SCT/DNR bits delivered on I/O completions (phase bit added later). |
| 75 | io_raw_status: Option<u16>, |
| 76 | /// Added to the echoed command id; nonzero forces a command-id mismatch. |
| 77 | io_cid_delta: i32, |
| 78 | /// Emit this many wrong-phase (zeroed) completions before the real one. |
| 79 | io_stale_wrong_phase: u32, |
| 80 | /// If true, never place an I/O completion (driver must bound its poll). |
| 81 | io_never_complete: bool, |
| 82 | /// Reject `Identify` for any namespace other than NSID 1. |
| 83 | reject_bad_nsid: bool, |
| 84 | } |
| 85 | |
| 86 | struct Region { |
| 87 | base: u64, |
| 88 | len: usize, |
| 89 | bytes: Vec<u8>, |
| 90 | } |
| 91 | |
| 92 | struct Device { |
| 93 | regions: Vec<Region>, |
| 94 | next_phys: u64, |
| 95 | cc: u32, |
| 96 | csts: u32, |
| 97 | asq: u64, |
| 98 | acq: u64, |
| 99 | admin_cq_base: Option<u64>, |
| 100 | admin_cq_len: usize, |
| 101 | io_sq_base: Option<u64>, |
| 102 | io_cq_base: Option<u64>, |
| 103 | io_depth: usize, |
| 104 | admin_sq_tail: usize, |
| 105 | admin_cq_tail: usize, |
| 106 | admin_phase: bool, |
| 107 | io_sq_tail: usize, |
| 108 | io_cq_tail: usize, |
| 109 | io_phase: bool, |
| 110 | admin_queue: VecDeque<[u8; 16]>, |
| 111 | io_queue: VecDeque<[u8; 16]>, |
| 112 | io_commands: u64, |
| 113 | io_submissions: Vec<IoCommand>, |
| 114 | behavior: Behavior, |
| 115 | } |
| 116 | |
| 117 | impl Device { |
| 118 | fn new() -> Self { |
| 119 | Self { |
| 120 | regions: Vec::new(), |
| 121 | next_phys: 0x1000, |
| 122 | cc: 0, |
| 123 | csts: 0, |
| 124 | asq: 0, |
| 125 | acq: 0, |
| 126 | admin_cq_base: None, |
| 127 | admin_cq_len: 0, |
| 128 | io_sq_base: None, |
| 129 | io_cq_base: None, |
| 130 | io_depth: 0, |
| 131 | admin_sq_tail: 0, |
| 132 | admin_cq_tail: 0, |
| 133 | admin_phase: true, |
| 134 | io_sq_tail: 0, |
| 135 | io_cq_tail: 0, |
| 136 | io_phase: true, |
| 137 | admin_queue: VecDeque::new(), |
| 138 | io_queue: VecDeque::new(), |
| 139 | io_commands: 0, |
| 140 | io_submissions: Vec::new(), |
| 141 | behavior: Behavior::default(), |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn alloc(&mut self, size: usize, alignment: usize) -> u64 { |
| 146 | let align = alignment.max(1) as u64; |
| 147 | let base = (self.next_phys + align - 1) & !(align - 1); |
| 148 | self.next_phys = base + size as u64; |
| 149 | self.regions.push(Region { |
| 150 | base, |
| 151 | len: size, |
| 152 | bytes: vec![0; size], |
| 153 | }); |
| 154 | base |
| 155 | } |
| 156 | |
| 157 | fn find_region(&self, physical: u64, len: usize) -> Option<usize> { |
| 158 | self.regions |
| 159 | .iter() |
| 160 | .position(|r| physical >= r.base && physical + len as u64 <= r.base + r.len as u64) |
| 161 | } |
| 162 | |
| 163 | /// Completion-queue reads are served from FIFO queues so a wrong-phase entry |
| 164 | /// can be parked ahead of the real one regardless of the exact slot address. |
| 165 | fn read_mem(&mut self, physical: u64, output: &mut [u8]) -> Result<(), NvmeError> { |
| 166 | if output.len() == 16 { |
| 167 | if let Some(base) = self.admin_cq_base { |
| 168 | if physical >= base && physical + 16 <= base + self.admin_cq_len as u64 { |
| 169 | match self.admin_queue.pop_front() { |
| 170 | Some(cqe) => output.copy_from_slice(&cqe), |
| 171 | None => output.fill(0), |
| 172 | } |
| 173 | return Ok(()); |
| 174 | } |
| 175 | } |
| 176 | if let Some(base) = self.io_cq_base { |
| 177 | let len = (self.io_depth * 16) as u64; |
| 178 | if physical >= base && physical + 16 <= base + len { |
| 179 | match self.io_queue.pop_front() { |
| 180 | Some(cqe) => output.copy_from_slice(&cqe), |
| 181 | None => output.fill(0), |
| 182 | } |
| 183 | return Ok(()); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | match self.find_region(physical, output.len()) { |
| 188 | Some(i) => { |
| 189 | output.copy_from_slice(&self.regions[i].bytes[..output.len()]); |
| 190 | Ok(()) |
| 191 | } |
| 192 | None => Err(NvmeError::Dma), |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | fn write_mem(&mut self, physical: u64, input: &[u8]) -> Result<(), NvmeError> { |
| 197 | match self.find_region(physical, input.len()) { |
| 198 | Some(i) => { |
| 199 | self.regions[i].bytes[..input.len()].copy_from_slice(input); |
| 200 | Ok(()) |
| 201 | } |
| 202 | None => Err(NvmeError::Dma), |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | fn peek_command(&self, base: u64, slot: usize) -> [u8; 64] { |
| 207 | let addr = base + (slot * 64) as u64; |
| 208 | let mut out = [0u8; 64]; |
| 209 | if let Some(i) = self.find_region(addr, 64) { |
| 210 | out.copy_from_slice(&self.regions[i].bytes[..64]); |
| 211 | } |
| 212 | out |
| 213 | } |
| 214 | |
| 215 | fn fill_identify(&mut self, prp: u64, cns: u32) { |
| 216 | if let Some(i) = self.find_region(prp, PAGE) { |
| 217 | let bytes = &mut self.regions[i].bytes; |
| 218 | if cns == 1 { |
| 219 | bytes[77] = 0; // MDTS = 0 -> 128 KiB default chunk limit. |
| 220 | } else { |
| 221 | bytes[0..8].copy_from_slice(&NS_BLOCKS.to_le_bytes()); |
| 222 | bytes[26] = 0; // FLBAS -> format 0 |
| 223 | bytes[128 + 2] = 9; // LBADS -> 512-byte blocks |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn push_admin(&mut self, phase: bool, cid: u16, raw: u16) { |
| 229 | let mut cqe = [0u8; 16]; |
| 230 | cqe[12..14].copy_from_slice(&cid.to_le_bytes()); |
| 231 | cqe[14..16].copy_from_slice(&(raw | u16::from(phase)).to_le_bytes()); |
| 232 | self.admin_queue.push_back(cqe); |
| 233 | } |
| 234 | |
| 235 | fn push_io(&mut self, phase: bool, cid: u16, raw: u16) { |
| 236 | let mut cqe = [0u8; 16]; |
| 237 | cqe[12..14].copy_from_slice(&cid.to_le_bytes()); |
| 238 | cqe[14..16].copy_from_slice(&(raw | u16::from(phase)).to_le_bytes()); |
| 239 | self.io_queue.push_back(cqe); |
| 240 | } |
| 241 | |
| 242 | /// A malformed, all-zero-otherwise CQE tagged with the *wrong* phase bit. |
| 243 | fn push_io_wrong_phase(&mut self) { |
| 244 | let mut cqe = [0u8; 16]; |
| 245 | cqe[14..16].copy_from_slice(&u16::from(!self.io_phase).to_le_bytes()); |
| 246 | self.io_queue.push_back(cqe); |
| 247 | } |
| 248 | |
| 249 | fn on_admin_doorbell(&mut self) { |
| 250 | let slot = self.admin_sq_tail; |
| 251 | let cmd = self.peek_command(self.asq, slot); |
| 252 | let opcode = u32::from_le_bytes(cmd[0..4].try_into().unwrap()) & 0xff; // low byte; high half is the CID |
| 253 | let cid = u16::from_le_bytes(cmd[2..4].try_into().unwrap()); |
| 254 | let nsid = u32::from_le_bytes(cmd[4..8].try_into().unwrap()); |
| 255 | let p1 = u64::from_le_bytes(cmd[24..32].try_into().unwrap()); |
| 256 | let cdw10 = u32::from_le_bytes(cmd[40..44].try_into().unwrap()); |
| 257 | let mut raw = 0u16; |
| 258 | match opcode { |
| 259 | 0x06 => { |
| 260 | if cdw10 == 0 && self.behavior.reject_bad_nsid && nsid != 1 { |
| 261 | raw |= 0x0002; // SC 0x01: invalid namespace, SCT 0. |
| 262 | } else { |
| 263 | self.fill_identify(p1, cdw10); |
| 264 | } |
| 265 | } |
| 266 | 0x05 => { |
| 267 | self.io_cq_base = Some(p1); |
| 268 | self.io_depth = ((cdw10 >> 16) & 0xffff) as usize + 1; |
| 269 | } |
| 270 | 0x01 => self.io_sq_base = Some(p1), |
| 271 | _ => {} |
| 272 | } |
| 273 | let echo = if self.behavior.io_cid_delta == 0 { |
| 274 | cid |
| 275 | } else { |
| 276 | cid.wrapping_add(self.behavior.io_cid_delta as u16) |
| 277 | }; |
| 278 | self.push_admin(self.admin_phase, echo, raw); |
| 279 | self.admin_sq_tail = (slot + 1) % ADMIN_DEPTH; |
| 280 | self.admin_cq_tail = (self.admin_cq_tail + 1) % ADMIN_DEPTH; |
| 281 | if self.admin_cq_tail == 0 { |
| 282 | self.admin_phase = !self.admin_phase; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | fn on_io_doorbell(&mut self) { |
| 287 | let Some(sq) = self.io_sq_base else { return }; |
| 288 | if self.io_depth == 0 { |
| 289 | return; |
| 290 | } |
| 291 | let slot = self.io_sq_tail; |
| 292 | let cmd = self.peek_command(sq, slot); |
| 293 | let opcode = u32::from_le_bytes(cmd[0..4].try_into().unwrap()) & 0xff; // low byte; high half is the CID |
| 294 | let cid = u16::from_le_bytes(cmd[2..4].try_into().unwrap()); |
| 295 | let nsid = u32::from_le_bytes(cmd[4..8].try_into().unwrap()); |
| 296 | let lba = u64::from(u32::from_le_bytes(cmd[40..44].try_into().unwrap())) |
| 297 | | (u64::from(u32::from_le_bytes(cmd[44..48].try_into().unwrap())) << 32); |
| 298 | let blocks = u32::from_le_bytes(cmd[48..52].try_into().unwrap()) + 1; |
| 299 | self.io_submissions.push(IoCommand { |
| 300 | opcode, |
| 301 | cid, |
| 302 | nsid, |
| 303 | lba, |
| 304 | blocks, |
| 305 | }); |
| 306 | self.io_commands += 1; |
| 307 | if !self.behavior.io_never_complete { |
| 308 | let mut stale = self.behavior.io_stale_wrong_phase; |
| 309 | while stale > 0 { |
| 310 | self.push_io_wrong_phase(); |
| 311 | stale -= 1; |
| 312 | } |
| 313 | let echo = if self.behavior.io_cid_delta == 0 { |
| 314 | cid |
| 315 | } else { |
| 316 | cid.wrapping_add(self.behavior.io_cid_delta as u16) |
| 317 | }; |
| 318 | let raw = self.behavior.io_raw_status.unwrap_or(0); |
| 319 | self.push_io(self.io_phase, echo, raw); |
| 320 | } |
| 321 | self.io_sq_tail = (slot + 1) % self.io_depth; |
| 322 | self.io_cq_tail = (self.io_cq_tail + 1) % self.io_depth; |
| 323 | if self.io_cq_tail == 0 { |
| 324 | self.io_phase = !self.io_phase; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | fn reg_read(&mut self, offset: u32) -> u32 { |
| 329 | match offset { |
| 330 | 0 => CAP_LOW, |
| 331 | 4 => 0, |
| 332 | REG_CC => self.cc, |
| 333 | REG_CSTS => self.csts, |
| 334 | _ => 0, |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | fn reg_write(&mut self, offset: u32, value: u32) { |
| 339 | match offset { |
| 340 | REG_CC => { |
| 341 | self.cc = value; |
| 342 | self.csts = if value & 1 == 0 { 0 } else { CSTS_RDY }; |
| 343 | } |
| 344 | REG_ASQ => self.asq = (self.asq & 0xffff_ffff_0000_0000) | u64::from(value), |
| 345 | x if x == REG_ASQ + 4 => self.asq = (self.asq & 0xffff_ffff) | (u64::from(value) << 32), |
| 346 | REG_ACQ => { |
| 347 | self.acq = (self.acq & 0xffff_ffff_0000_0000) | u64::from(value); |
| 348 | self.admin_cq_base = Some(self.acq); |
| 349 | self.admin_cq_len = PAGE; |
| 350 | } |
| 351 | x if x == REG_ACQ + 4 => { |
| 352 | self.acq = (self.acq & 0xffff_ffff) | (u64::from(value) << 32); |
| 353 | self.admin_cq_base = Some(self.acq); |
| 354 | self.admin_cq_len = PAGE; |
| 355 | } |
| 356 | ADMIN_DOORBELL => self.on_admin_doorbell(), |
| 357 | IO_SQ_DOORBELL => self.on_io_doorbell(), |
| 358 | _ => {} |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // --------------------------------------------------------------------------- |
| 364 | // Fakes implementing the public driver traits |
| 365 | // --------------------------------------------------------------------------- |
| 366 | |
| 367 | struct FakeRegs { |
| 368 | shared: Shared, |
| 369 | } |
| 370 | |
| 371 | impl Registers for FakeRegs { |
| 372 | fn read32(&mut self, offset: u32) -> u32 { |
| 373 | self.shared.borrow_mut().reg_read(offset) |
| 374 | } |
| 375 | fn write32(&mut self, offset: u32, value: u32) { |
| 376 | self.shared.borrow_mut().reg_write(offset, value); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | struct FakeDma { |
| 381 | shared: Shared, |
| 382 | } |
| 383 | |
| 384 | impl DmaMemory for FakeDma { |
| 385 | fn allocate(&mut self, size: usize, alignment: usize) -> Result<DmaRegion, NvmeError> { |
| 386 | let physical = self.shared.borrow_mut().alloc(size, alignment); |
| 387 | Ok(DmaRegion { |
| 388 | physical, |
| 389 | bytes: vec![0; size], |
| 390 | }) |
| 391 | } |
| 392 | fn read(&self, physical: u64, output: &mut [u8]) -> Result<(), NvmeError> { |
| 393 | self.shared.borrow_mut().read_mem(physical, output) |
| 394 | } |
| 395 | fn write(&mut self, physical: u64, input: &[u8]) -> Result<(), NvmeError> { |
| 396 | self.shared.borrow_mut().write_mem(physical, input) |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | #[derive(Default)] |
| 401 | struct CountingDelay { |
| 402 | waited_us: Rc<RefCell<u64>>, |
| 403 | } |
| 404 | |
| 405 | impl Delay for CountingDelay { |
| 406 | fn delay_us(&mut self, us: u32) { |
| 407 | *self.waited_us.borrow_mut() += u64::from(us); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | type Controller = NvmeController<FakeRegs, FakeDma, CountingDelay>; |
| 412 | |
| 413 | /// Build a controller that completes its initialization normally. |
| 414 | fn make_controller() -> (Controller, Shared, Rc<RefCell<u64>>) { |
| 415 | let shared: Shared = Rc::new(RefCell::new(Device::new())); |
| 416 | let regs = FakeRegs { |
| 417 | shared: shared.clone(), |
| 418 | }; |
| 419 | let dma = FakeDma { |
| 420 | shared: shared.clone(), |
| 421 | }; |
| 422 | let delay = CountingDelay::default(); |
| 423 | let waited = delay.waited_us.clone(); |
| 424 | let controller = NvmeController::init(regs, dma, delay).expect("init should succeed"); |
| 425 | (controller, shared, waited) |
| 426 | } |
| 427 | |
| 428 | // --------------------------------------------------------------------------- |
| 429 | // Tests |
| 430 | // --------------------------------------------------------------------------- |
| 431 | |
| 432 | #[test] |
| 433 | fn failed_write_completion_maps_to_device_error() { |
| 434 | let (mut controller, shared, _) = make_controller(); |
| 435 | controller.create_io_queues(IO_DEPTH).unwrap(); |
| 436 | shared.borrow_mut().behavior.io_raw_status = Some(0x0002); |
| 437 | assert_eq!( |
| 438 | controller.write_blocks(0, &[0xAB; 512]), |
| 439 | Err(BlockError::DeviceError) |
| 440 | ); |
| 441 | let device = shared.borrow(); |
| 442 | assert_eq!(device.io_submissions.len(), 1); |
| 443 | let command = device.io_submissions[0]; |
| 444 | assert_eq!(command.opcode, 0x01, "write opcode"); |
| 445 | assert_eq!(command.nsid, 1, "writes are hard-coded to NSID 1"); |
| 446 | assert_eq!(command.lba, 0); |
| 447 | assert_eq!(command.blocks, 1); |
| 448 | // A failing completion is still consumed; the driver's CQ head advances. |
| 449 | assert_eq!(controller.io_queue().unwrap().completion_head, 1); |
| 450 | } |
| 451 | |
| 452 | #[test] |
| 453 | fn failed_flush_completion_maps_to_device_error() { |
| 454 | let (mut controller, shared, _) = make_controller(); |
| 455 | controller.create_io_queues(IO_DEPTH).unwrap(); |
| 456 | shared.borrow_mut().behavior.io_raw_status = Some(0x0084); |
| 457 | assert_eq!(controller.flush(), Err(BlockError::DeviceError)); |
| 458 | let device = shared.borrow(); |
| 459 | assert_eq!(device.io_submissions.len(), 1); |
| 460 | assert_eq!(device.io_submissions[0].opcode, 0x00, "flush opcode"); |
| 461 | assert_eq!(device.io_submissions[0].nsid, 1); |
| 462 | } |
| 463 | |
| 464 | #[test] |
| 465 | fn write_timeout_is_bounded_no_infinite_loop() { |
| 466 | let (mut controller, shared, waited) = make_controller(); |
| 467 | controller.create_io_queues(IO_DEPTH).unwrap(); |
| 468 | shared.borrow_mut().behavior.io_never_complete = true; |
| 469 | let before = *waited.borrow(); |
| 470 | assert_eq!( |
| 471 | controller.write_blocks(0, &[0; 512]), |
| 472 | Err(BlockError::Timeout) |
| 473 | ); |
| 474 | assert_eq!(*waited.borrow() - before, POLLS * 1_000); |
| 475 | // Nothing was consumed because nothing ever completed. |
| 476 | assert_eq!(controller.io_queue().unwrap().completion_head, 0); |
| 477 | } |
| 478 | |
| 479 | #[test] |
| 480 | fn flush_timeout_is_bounded_no_infinite_loop() { |
| 481 | let (mut controller, shared, waited) = make_controller(); |
| 482 | controller.create_io_queues(IO_DEPTH).unwrap(); |
| 483 | shared.borrow_mut().behavior.io_never_complete = true; |
| 484 | let before = *waited.borrow(); |
| 485 | assert_eq!(controller.flush(), Err(BlockError::Timeout)); |
| 486 | assert_eq!(*waited.borrow() - before, POLLS * 1_000); |
| 487 | } |
| 488 | |
| 489 | #[test] |
| 490 | fn write_batch_cq_wraparound_flips_phase_and_consumes_in_order() { |
| 491 | let (mut controller, shared, _) = make_controller(); |
| 492 | controller.create_io_queues(IO_DEPTH).unwrap(); |
| 493 | for lba in 0..4u64 { |
| 494 | controller.write_blocks(lba, &[(lba as u8); 512]).unwrap(); |
| 495 | } |
| 496 | { |
| 497 | let device = shared.borrow(); |
| 498 | assert_eq!(device.io_cq_tail, 0, "producer tail wraps at depth 4"); |
| 499 | assert!(!device.io_phase, "producer phase flips after the wrap"); |
| 500 | assert_eq!(device.io_commands, 4); |
| 501 | let lbas: Vec<u64> = device.io_submissions.iter().map(|c| c.lba).collect(); |
| 502 | assert_eq!(lbas, vec![0, 1, 2, 3], "commands recorded in submit order"); |
| 503 | } |
| 504 | { |
| 505 | let q = controller.io_queue().unwrap(); |
| 506 | assert_eq!(q.completion_head, 0, "consumer head wraps at depth 4"); |
| 507 | assert!(!q.phase, "consumer flips in lockstep with the producer"); |
| 508 | } |
| 509 | // The fifth write only completes if the driver flipped in lockstep. |
| 510 | controller.write_blocks(4, &[0x44; 512]).unwrap(); |
| 511 | let q = controller.io_queue().unwrap(); |
| 512 | assert_eq!(q.completion_head, 1); |
| 513 | assert!(!q.phase, "phase stays flipped past the wrap"); |
| 514 | assert!(shared.borrow().io_queue.is_empty()); |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn invalid_namespace_identify_surfaces_completion_status() { |
| 519 | let (mut controller, shared, _) = make_controller(); |
| 520 | shared.borrow_mut().behavior.reject_bad_nsid = true; |
| 521 | // NSID 1 stays reachable and valid. |
| 522 | assert_eq!( |
| 523 | controller.identify_namespace(1).unwrap().block_count, |
| 524 | NS_BLOCKS |
| 525 | ); |
| 526 | // NSID 2 is rejected by the controller with a non-zero status. |
| 527 | assert_eq!( |
| 528 | controller.identify_namespace(2), |
| 529 | Err(NvmeError::CompletionStatus { sct: 0, sc: 1 }) |
| 530 | ); |
| 531 | } |
| 532 | |
| 533 | #[test] |
| 534 | fn write_command_id_mismatch_maps_to_device_error() { |
| 535 | let (mut controller, shared, _) = make_controller(); |
| 536 | controller.create_io_queues(IO_DEPTH).unwrap(); |
| 537 | shared.borrow_mut().behavior.io_cid_delta = 1; |
| 538 | assert_eq!( |
| 539 | controller.write_blocks(0, &[0; 512]), |
| 540 | Err(BlockError::DeviceError) |
| 541 | ); |
| 542 | // A command-id mismatch is rejected before the head advances. |
| 543 | assert_eq!(controller.io_queue().unwrap().completion_head, 0); |
| 544 | } |
| 545 | |
| 546 | #[test] |
| 547 | fn wrong_phase_zero_completion_is_ignored_not_consumed() { |
| 548 | let (mut controller, shared, waited) = make_controller(); |
| 549 | controller.create_io_queues(IO_DEPTH).unwrap(); |
| 550 | // An all-zero CQE (phase bit 0) is parked ahead of the real completion. |
| 551 | shared.borrow_mut().behavior.io_stale_wrong_phase = 1; |
| 552 | let before = *waited.borrow(); |
| 553 | controller.write_blocks(0, &[0x5A; 512]).unwrap(); |
| 554 | // Exactly one backoff: the mismatched entry was read and skipped, never |
| 555 | // consumed. The CQ head advanced once, only for the real completion. |
| 556 | assert_eq!(*waited.borrow() - before, 1_000); |
| 557 | let q = controller.io_queue().unwrap(); |
| 558 | assert_eq!(q.completion_head, 1); |
| 559 | assert!(q.phase); |
| 560 | } |
| 561 | |
| 562 | // Keep `Submission` in scope and document the command layout the fake parses. |
| 563 | #[allow(dead_code)] |
| 564 | fn io_command_layout_probe() -> Submission { |
| 565 | Submission::write(1, 0, 1, 0, 0) |
| 566 | } |