git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/drivers/e1000.rs
//! Intel's Ethernet on a PC's own board: the e1000e family, from the
//! 82574L (what QEMU offers as `e1000e`) to the I217, I218 and I219 every
//! Intel laptop with a cable port has had since 2013 (the chipset's LAN,
//! with a PHY of its own). One receive and one transmit ring of legacy
//! descriptors, which the whole family shares; its interrupt by MSI.
//!
//! Written from Intel's datasheets and tried in QEMU's 82574L only. Not
//! here: the I219's PHY waking from its low-power state (ULP) when the
//! firmware left it asleep, which Linux's driver does on some ThinkPads,
//! and Wake-on-LAN, checksum offload, more than one queue.
use crate::mm::pages;
use crate::mmu::physical;
use core::sync::atomic::{Ordering, fence};
const CTRL: usize = 0x0000;
const STATUS: usize = 0x0008;
const EERD: usize = 0x0014;
const ICR: usize = 0x00c0;
const IMS: usize = 0x00d0;
const IMC: usize = 0x00d8;
const RCTL: usize = 0x0100;
const TCTL: usize = 0x0400;
const TIPG: usize = 0x0410;
const RDBAL: usize = 0x2800;
const RDBAH: usize = 0x2804;
const RDLEN: usize = 0x2808;
const RDH: usize = 0x2810;
const RDT: usize = 0x2818;
const TDBAL: usize = 0x3800;
const TDBAH: usize = 0x3804;
const TDLEN: usize = 0x3808;
const TDH: usize = 0x3810;
const TDT: usize = 0x3818;
const MTA: usize = 0x5200;
const RAL: usize = 0x5400;
const RAH: usize = 0x5404;
const CTRL_SLU: u32 = 1 << 6;
const CTRL_RST: u32 = 1 << 26;
const STATUS_LINK_UP: u32 = 1 << 1;
/// Receive: on, every multicast (IPv6's router and neighbour
/// discovery come so), broadcasts, CRC stripped, 2048-byte buffers (size 00).
const RCTL_EN: u32 = 1 << 1;
const RCTL_MPE: u32 = 1 << 4;
const RCTL_BAM: u32 = 1 << 15;
const RCTL_SECRC: u32 = 1 << 26;
/// Transmit: on, short packets padded, collision threshold and distance.
const TCTL_EN: u32 = 1 << 1;
const TCTL_PSP: u32 = 1 << 3;
const TCTL_CT: u32 = 0x0f << 4;
const TCTL_COLD: u32 = 0x3f << 12;
/// Interrupt causes: link changed, receive ring low, overrun, received.
const INT_LSC: u32 = 1 << 2;
const INT_RXDMT0: u32 = 1 << 4;
const INT_RXO: u32 = 1 << 6;
const INT_RXT0: u32 = 1 << 7;
/// A descriptor's status: done; and a transmit's command: end of packet,
/// insert the CRC, report status.
const DD: u8 = 1;
const CMD_EOP: u8 = 1;
const CMD_IFCS: u8 = 1 << 1;
const CMD_RS: u8 = 1 << 3;
const RX: usize = 32;
const TX: usize = 32;
/// A buffer: a full frame, two to a page.
const BUFFER: usize = 2048;
/// The family's device IDs: the 82574L, the 82579, then the chipsets' LAN
/// (I217, I218, I219 in its many steppings).
const IDS: &[u16] = &[
0x10d3, 0x1502, 0x1503, 0x153a, 0x153b, 0x155a, 0x1559, 0x15a0, 0x15a1, 0x15a2, 0x15a3, 0x156f, 0x1570,
0x15b7, 0x15b8, 0x15b9, 0x15bb, 0x15bc, 0x15bd, 0x15be, 0x15d6, 0x15d7, 0x15d8, 0x15df, 0x15e0, 0x15e1,
0x15e2, 0x15e3, 0x15f4, 0x15f5, 0x15f9, 0x15fa, 0x15fb, 0x15fc, 0x0d4c, 0x0d4d, 0x0d4e, 0x0d4f, 0x0d53,
0x0d55, 0x1a1c, 0x1a1d, 0x1a1e, 0x1a1f, 0x0dc5, 0x0dc6, 0x0dc7, 0x0dc8, 0x550a, 0x550b, 0x550c, 0x550d,
0x550e, 0x550f, 0x5510, 0x5511, 0x57a0, 0x57a1, 0x57b3, 0x57b4, 0x57b5, 0x57b6, 0x57b7, 0x57b8, 0x57b9,
0x57ba,
];
pub struct Nic {
regs: usize,
pub intid: u32,
/// The rings (a page each) and their buffers.
rx_ring: usize,
tx_ring: usize,
rx_pages: [usize; RX / 2],
tx_pages: [usize; TX / 2],
/// The next receive descriptor the device fills, and the next
/// transmit descriptor to use.
rx_next: usize,
tx_next: usize,
/// Its model, for the boot log.
pub id: u16,
}
impl Nic {
fn read(&self, register: usize) -> u32 {
// SAFETY: the device's registers, in its BAR 0.
unsafe { ((self.regs + register) as *const u32).read_volatile() }
}
fn write(&self, register: usize, value: u32) {
// SAFETY: as in `read`.
unsafe { ((self.regs + register) as *mut u32).write_volatile(value) }
}
fn rx_buffer(&self, i: usize) -> usize {
self.rx_pages[i / 2] + (i % 2) * BUFFER
}
fn tx_buffer(&self, i: usize) -> usize {
self.tx_pages[i / 2] + (i % 2) * BUFFER
}
fn descriptor(ring: usize, i: usize) -> *mut u8 {
(ring + i * 16) as *mut u8
}
/// Whether the cable is in and the link up.
pub fn link(&self) -> bool {
self.read(STATUS) & STATUS_LINK_UP != 0
}
/// Puts `frame` on the wire, if a descriptor is free; else drops it,
/// as a full queue does.
pub fn send(&mut self, frame: &[u8]) {
let i = self.tx_next;
let d = Self::descriptor(self.tx_ring, i);
// SAFETY: the transmit descriptor `i`, in our page; the device has
// finished with it when its DD is set, or it was never used (its
// command still 0).
unsafe {
let (status, command) = (d.add(12).read_volatile(), d.add(11).read_volatile());
if command != 0 && status & DD == 0 {
return;
}
let len = frame.len().min(BUFFER);
let at = self.tx_buffer(i);
core::ptr::copy_nonoverlapping(frame.as_ptr(), at as *mut u8, len);
(d as *mut u64).write_volatile(physical(at) as u64);
(d.add(8) as *mut u16).write_volatile(len as u16);
d.add(12).write_volatile(0);
d.add(11).write_volatile(CMD_EOP | CMD_IFCS | CMD_RS);
}
self.tx_next = (i + 1) % TX;
fence(Ordering::SeqCst);
self.write(TDT, self.tx_next as u32);
}
/// The interrupt: its causes read (which clears them), and every frame
/// received handed to `each`, its buffer given back.
pub fn receive(&mut self, mut each: impl FnMut(&[u8])) {
self.read(ICR);
loop {
let i = self.rx_next;
let d = Self::descriptor(self.rx_ring, i);
// SAFETY: receive descriptor `i`, in our page, which the device
// wrote whole before setting DD.
let (status, len) = unsafe {
(
d.add(12).read_volatile(),
(d.add(8) as *const u16).read_volatile(),
)
};
if status & DD == 0 {
break;
}
fence(Ordering::SeqCst);
let at = self.rx_buffer(i);
// SAFETY: the device wrote `len` bytes into the buffer.
each(unsafe { core::slice::from_raw_parts(at as *const u8, usize::from(len).min(BUFFER)) });
// SAFETY: as above; given back with its status cleared.
unsafe { d.add(12).write_volatile(0) };
self.rx_next = (i + 1) % RX;
fence(Ordering::SeqCst);
self.write(RDT, i as u32);
}
}
}
/// A 16-bit word of the device's NVM, through EERD: where the MAC address
/// is when RAL does not have it.
fn nvm_word(nic: &Nic, word: u32) -> Option<u16> {
const START: u32 = 1;
const DONE: u32 = 1 << 1;
nic.write(EERD, word << 2 | START);
let deadline = crate::timer::now_ms() + 10;
loop {
let v = nic.read(EERD);
if v & DONE != 0 {
return Some((v >> 16) as u16);
}
if crate::timer::now_ms() > deadline {
return None;
}
}
}
/// Sets up the first of the family on PCI: reset, its rings, receiving
/// and sending on. The device and its MAC address.
pub fn init() -> Option<(Nic, [u8; 6])> {
let f = crate::pci::find(0x8086, 0..=0xffff)
.into_iter()
.find(|f| f.class >> 8 == 0x0200 && IDS.contains(&f.id))?;
let regs = f.bar_address(0, 0)?;
// Its interrupt by MSI, which every one of them has (the 82574's
// MSI-X would route its causes through IVAR first).
let (address, data, intid) = crate::gic::message_interrupt()?;
f.enable(false);
if !f.msi(address, data) {
return None;
}
crate::pci::claim(&f, "e1000e", crate::device::Class::Network);
let alloc_zeroed = || {
let p = pages::alloc(0)?;
// SAFETY: a fresh page, ours.
unsafe { core::ptr::write_bytes(p as *mut u8, 0, crate::mm::PAGE) };
Some(p)
};
let mut nic = Nic {
regs,
intid,
rx_ring: alloc_zeroed()?,
tx_ring: alloc_zeroed()?,
rx_pages: [0; RX / 2],
tx_pages: [0; TX / 2],
rx_next: 0,
tx_next: 0,
id: f.id,
};
for p in &mut nic.rx_pages {
*p = pages::alloc(0)?;
}
for p in &mut nic.tx_pages {
*p = pages::alloc(0)?;
}
// Reset, with its interrupts masked before and after.
nic.write(IMC, u32::MAX);
nic.write(CTRL, nic.read(CTRL) | CTRL_RST);
let deadline = crate::timer::now_ms() + 100;
while nic.read(CTRL) & CTRL_RST != 0 && crate::timer::now_ms() < deadline {
core::hint::spin_loop();
}
let settle = crate::timer::now_ms() + 10;
while crate::timer::now_ms() < settle {
core::hint::spin_loop();
}
nic.write(IMC, u32::MAX);
nic.read(ICR);
nic.write(CTRL, nic.read(CTRL) | CTRL_SLU);
// Its address: as the NVM loaded it into RAL/RAH at reset, else read.
let (low, high) = (nic.read(RAL), nic.read(RAH));
let mut mac = [
low as u8,
(low >> 8) as u8,
(low >> 16) as u8,
(low >> 24) as u8,
high as u8,
(high >> 8) as u8,
];
if mac == [0; 6] {
for i in 0..3 {
let word = nvm_word(&nic, i as u32)?;
mac[2 * i] = word as u8;
mac[2 * i + 1] = (word >> 8) as u8;
}
nic.write(RAL, u32::from_le_bytes([mac[0], mac[1], mac[2], mac[3]]));
nic.write(RAH, u32::from(mac[4]) | u32::from(mac[5]) << 8 | 1 << 31);
}
for i in 0..128 {
nic.write(MTA + 4 * i, 0);
}
for i in 0..RX {
let d = Nic::descriptor(nic.rx_ring, i);
// SAFETY: receive descriptor `i`, in our zeroed page.
unsafe { (d as *mut u64).write_volatile(physical(nic.rx_buffer(i)) as u64) };
}
let (rx, tx) = (physical(nic.rx_ring) as u64, physical(nic.tx_ring) as u64);
nic.write(RDBAL, rx as u32);
nic.write(RDBAH, (rx >> 32) as u32);
nic.write(RDLEN, (RX * 16) as u32);
nic.write(RDH, 0);
nic.write(RDT, (RX - 1) as u32);
nic.write(TDBAL, tx as u32);
nic.write(TDBAH, (tx >> 32) as u32);
nic.write(TDLEN, (TX * 16) as u32);
nic.write(TDH, 0);
nic.write(TDT, 0);
nic.write(RCTL, RCTL_EN | RCTL_MPE | RCTL_BAM | RCTL_SECRC);
nic.write(TCTL, TCTL_EN | TCTL_PSP | TCTL_CT | TCTL_COLD);
// The inter-packet gap IEEE 802.3 has (10, 8, 6).
nic.write(TIPG, 10 | 8 << 10 | 6 << 20);
nic.write(IMS, INT_RXT0 | INT_RXO | INT_RXDMT0 | INT_LSC);
Some((nic, mac))
}