diff --git a/crates/stela-driver-net/src/realtek.rs b/crates/stela-driver-net/src/realtek.rs index a1c055aa42..896248413a 100644 --- a/crates/stela-driver-net/src/realtek.rs +++ b/crates/stela-driver-net/src/realtek.rs @@ -306,9 +306,14 @@ impl Realtek { let device = map_registers(handoff, bar, 0, 256)?; function.enable_polled()?; let txconfig = device.read32(TX_CONFIG); + // A window panics on an access past its length, so each read goes + // through one that covers it: the extended identity word, then + // every register the detected revision's start and runtime reach. + let device = widen(handoff, bar, device, identity_aperture(txconfig))?; let revision = detect_revision(&device, txconfig, |bytes| { map_registers(handoff, bar, 0, bytes).map(|_| ()) })?; + let device = widen(handoff, bar, device, register_aperture(revision))?; admit_revision(function.device, pcie_link_control, revision)?; if tbi_mode(revision, device.read8(PHY_STATUS)) { return Err(BlockError::Unsupported("Realtek TBI/fiber mode").into()); @@ -536,6 +541,31 @@ fn admit_revision(device: u16, pcie_control: Option, revision: Revision) -> Ok(()) } +/// The registers the identity reads span: the first 256 bytes, and through +/// the extended word where TxConfig says the chip has one. +fn identity_aperture(txconfig: u32) -> u64 { + if revision::needs_extended(txconfig) { + EXTENDED_REGISTER_BYTES + } else { + 256 + } +} + +/// `device`, or a window of `bytes` over the same BAR when it is shorter. +/// The BAR must cover and map the wider range. +fn widen( + handoff: &Handoff, + bar: Option, + device: Mmio, + bytes: u64, +) -> Result { + if bytes <= device.len() as u64 { + return Ok(device); + } + map_registers(handoff, bar, 0, bytes) +} + +/// The registers a revision's start and runtime span. fn register_aperture(revision: Revision) -> u64 { if revision.nbase() { NBASE_REGISTER_BYTES diff --git a/crates/stela-driver-net/src/realtek/tests/nbase_admission.rs b/crates/stela-driver-net/src/realtek/tests/nbase_admission.rs index 10d69bf723..1e96b2c4ae 100644 --- a/crates/stela-driver-net/src/realtek/tests/nbase_admission.rs +++ b/crates/stela-driver-net/src/realtek/tests/nbase_admission.rs @@ -139,3 +139,37 @@ fn nbase_polled_carrier_preserves_latch_low_an_restart_and_cache_expiry() { assert_eq!(model.state.borrow().writes.len(), writes); } } + +/// The constructor reads the identity through 256 bytes, or through the +/// extended word where TxConfig names one, then maps the detected chip's +/// whole aperture. An RTL8125 or RTL8126 start writes far past 256 bytes +/// (0xa00 to 0xb00, 0x1880), and a window panics past its length, so +/// every write of the reset and MAC start must lie inside the aperture. +#[test] +fn nbase_start_stays_inside_the_window_the_constructor_maps() { + assert_eq!(identity_aperture(0x641 << 20), 256); + assert_eq!(identity_aperture(0x7c8 << 20), EXTENDED_REGISTER_BYTES); + assert_eq!(identity_aperture(u32::MAX), 256); + for version in [61, 63, 64, 65, 66, 70] { + let time = Time::default(); + let model = Model::nbase(&time); + let mut hw = Hardware::new(&model, &time, revision(version), 0); + hw.initialize_modern().unwrap(); + hw.reset().unwrap(); + hw.configure_mac(None).unwrap(); + kick_transmit(&model, revision(version)); + let aperture = register_aperture(revision(version)) as usize; + let s = model.state.borrow(); + let furthest = s + .writes + .iter() + .map(|(offset, _, width)| offset + usize::from(*width)) + .max() + .unwrap(); + assert!(furthest > 256, "version {version} writes past 256 bytes"); + assert!( + furthest <= aperture, + "version {version} reaches {furthest:#x}" + ); + } +}