From ee66451873bb325f904fcf43288c020d5baff6f9 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Tue, 2 Apr 2019 10:24:18 +0800 Subject: [PATCH] Map e1000 to kernel space addr as well and rename its iface --- kernel/src/drivers/block/ahci.rs | 1 - kernel/src/drivers/bus/pci.rs | 12 ++++++-- kernel/src/drivers/net/e1000.rs | 53 ++++++++++---------------------- 3 files changed, 25 insertions(+), 41 deletions(-) diff --git a/kernel/src/drivers/block/ahci.rs b/kernel/src/drivers/block/ahci.rs index d05658c7..842259c3 100644 --- a/kernel/src/drivers/block/ahci.rs +++ b/kernel/src/drivers/block/ahci.rs @@ -457,7 +457,6 @@ pub fn ahci_init(irq: Option, header: usize, size: usize) -> Arc Option { } pub fn init_driver(dev: &PCIDevice) { + let name = format!("enp{}s{}f{}", dev.loc.bus, dev.loc.device, dev.loc.function); match (dev.id.vendor_id, dev.id.device_id) { (0x8086, 0x100e) | (0x8086, 0x100f) | (0x8086, 0x10d3) => { // 0x100e @@ -110,14 +111,19 @@ pub fn init_driver(dev: &PCIDevice) { // 0x10d3 // 82574L Gigabit Network Connection if let Some(BAR::Memory(addr, len, _, _)) = dev.bars[0] { - let _ = unsafe { enable(dev.loc) }; - e1000::e1000_init(addr as usize, len as usize); + let irq = unsafe { enable(dev.loc) }; + let vaddr = KERNEL_OFFSET + addr as usize; + let mut current_addr = addr as usize; + while current_addr < addr as usize + len as usize { + active_table().map_if_not_exists(KERNEL_OFFSET + current_addr, current_addr); + current_addr = current_addr + PAGE_SIZE; + } + e1000::e1000_init(name, irq, vaddr, len as usize); } } (0x8086, 0x10fb) => { // 82599ES 10-Gigabit SFI/SFP+ Network Connection if let Some(BAR::Memory(addr, len, _, _)) = dev.bars[0] { - let name = format!("enp{}s{}f{}", dev.loc.bus, dev.loc.device, dev.loc.function); let irq = unsafe { enable(dev.loc) }; PCI_DRIVERS.lock().insert( dev.loc, diff --git a/kernel/src/drivers/net/e1000.rs b/kernel/src/drivers/net/e1000.rs index 09172682..fb32d8f8 100644 --- a/kernel/src/drivers/net/e1000.rs +++ b/kernel/src/drivers/net/e1000.rs @@ -73,20 +73,19 @@ const E1000_RAH: usize = 0x5404 / 4; pub struct E1000Interface { iface: Mutex>, driver: E1000Driver, + name: String, + irq: Option, } impl Driver for E1000Interface { - fn try_handle_interrupt(&self, _irq: Option) -> bool { - let irq = { - let driver = self.driver.0.lock(); + fn try_handle_interrupt(&self, irq: Option) -> bool { + if irq.is_some() && self.irq.is_some() && irq != self.irq { + // not ours, skip it + return false; + } - if let None = active_table().get_entry(driver.header) { - let mut current_addr = driver.header; - while current_addr < driver.header + driver.size { - active_table().map_if_not_exists(current_addr, current_addr); - current_addr = current_addr + PAGE_SIZE; - } - } + let data = { + let driver = self.driver.0.lock(); let e1000 = unsafe { slice::from_raw_parts_mut(driver.header as *mut Volatile, driver.size / 4) @@ -102,7 +101,7 @@ impl Driver for E1000Interface { } }; - if irq { + if data { let timestamp = Instant::from_millis(crate::trap::uptime_msec() as i64); let mut sockets = SOCKETS.lock(); match self.iface.lock().poll(&mut sockets, timestamp) { @@ -115,7 +114,7 @@ impl Driver for E1000Interface { } } - return irq; + return data; } fn device_type(&self) -> DeviceType { @@ -131,7 +130,7 @@ impl Driver for E1000Interface { } fn get_ifname(&self) -> String { - format!("e1000") + self.name.clone() } fn ipv4_address(&self) -> Option { @@ -185,14 +184,6 @@ impl<'a> phy::Device<'a> for E1000Driver { fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { let driver = self.0.lock(); - if let None = active_table().get_entry(driver.header) { - let mut current_addr = driver.header; - while current_addr < driver.header + driver.size { - active_table().map_if_not_exists(current_addr, current_addr); - current_addr = current_addr + PAGE_SIZE; - } - } - let e1000 = unsafe { slice::from_raw_parts_mut(driver.header as *mut Volatile, driver.size / 4) }; @@ -238,14 +229,6 @@ impl<'a> phy::Device<'a> for E1000Driver { fn transmit(&'a mut self) -> Option { let driver = self.0.lock(); - if let None = active_table().get_entry(driver.header) { - let mut current_addr = driver.header; - while current_addr < driver.header + driver.size { - active_table().map_if_not_exists(current_addr, current_addr); - current_addr = current_addr + PAGE_SIZE; - } - } - let e1000 = unsafe { slice::from_raw_parts_mut(driver.header as *mut Volatile, driver.size / 4) }; @@ -354,8 +337,8 @@ bitflags! { } // JudgeDuck-OS/kern/e1000.c -pub fn e1000_init(header: usize, size: usize) { - info!("Probing e1000"); +pub fn e1000_init(name: String, irq: Option, header: usize, size: usize) { + info!("Probing e1000 {}", name); assert_eq!(size_of::(), 16); assert_eq!(size_of::(), 16); @@ -387,12 +370,6 @@ pub fn e1000_init(header: usize, size: usize) { first_trans: true, }; - let mut current_addr = header; - while current_addr < header + size { - active_table().map_if_not_exists(current_addr, current_addr); - current_addr = current_addr + PAGE_SIZE; - } - let e1000 = unsafe { slice::from_raw_parts_mut(header as *mut Volatile, size / 4) }; debug!( "status before setup: {:#?}", @@ -502,6 +479,8 @@ pub fn e1000_init(header: usize, size: usize) { let e1000_iface = E1000Interface { iface: Mutex::new(iface), driver: net_driver.clone(), + name, + irq, }; let driver = Arc::new(e1000_iface);