Skip to content

Commit

Permalink
feat(driver): 把virtio添加到sysfs (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
fslongjin authored Apr 22, 2024
1 parent a17651b commit e32effb
Show file tree
Hide file tree
Showing 21 changed files with 1,117 additions and 265 deletions.
6 changes: 3 additions & 3 deletions kernel/src/arch/x86_64/driver/apic/x2apic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use x86::msr::{
IA32_X2APIC_VERSION,
};

use crate::{kdebug, kinfo};
use crate::kinfo;

use super::{hw_irq::ApicId, LVTRegister, LocalAPIC, LVT};

Expand Down Expand Up @@ -55,9 +55,9 @@ impl LocalAPIC for X2Apic {
kinfo!("x2APIC EOI broadcast suppression enabled.");
}
}
kdebug!("x2apic: to mask all lvt");
// kdebug!("x2apic: to mask all lvt");
self.mask_all_lvt();
kdebug!("x2apic: all lvt masked");
// kdebug!("x2apic: all lvt masked");
}
true
}
Expand Down
6 changes: 3 additions & 3 deletions kernel/src/driver/base/device/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ pub trait Bus: Debug + Send + Sync {

fn resume(&self, device: &Arc<dyn Device>) -> Result<(), SystemError>;

/// match platform device to platform driver.
/// match device to driver.
///
/// ## 参数
///
/// * `device` - platform device
/// * `driver` - platform driver
/// * `device` - device
/// * `driver` - driver
///
/// ## 返回
///
Expand Down
9 changes: 7 additions & 2 deletions kernel/src/driver/base/device/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl DeviceManager {
///
/// 如果传递的设备已成功完成对驱动程序的探测,则返回true,否则返回false。
pub fn device_is_bound(&self, dev: &Arc<dyn Device>) -> bool {
return dev.driver().is_some();
return driver_manager().driver_is_bound(dev);
}

/// 把一个驱动绑定到设备上
Expand All @@ -199,6 +199,11 @@ impl DeviceManager {
pub fn device_bind_driver(&self, dev: &Arc<dyn Device>) -> Result<(), SystemError> {
let r = driver_manager().driver_sysfs_add(dev);
if let Err(e) = r {
kerror!(
"device_bind_driver: driver_sysfs_add failed, dev: '{}', err: {:?}",
dev.name(),
e
);
self.device_links_force_bind(dev);
driver_manager().driver_bound(dev);
return Err(e);
Expand Down Expand Up @@ -606,7 +611,7 @@ impl Attribute for DeviceAttrStateSynced {
}

#[derive(Debug)]
struct DeviceAttrCoredump;
pub(super) struct DeviceAttrCoredump;

impl Attribute for DeviceAttrCoredump {
fn name(&self) -> &str {
Expand Down
40 changes: 37 additions & 3 deletions kernel/src/driver/base/device/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ use super::{
Device, DeviceMatchName, DeviceMatcher, IdTable,
};
use crate::{
driver::base::kobject::KObject,
driver::base::{
device::{bus::BusNotifyEvent, dd::DeviceAttrCoredump, device_manager},
kobject::KObject,
},
filesystem::sysfs::{sysfs_instance, Attribute, AttributeGroup},
};
use alloc::{
string::ToString,
sync::{Arc, Weak},
vec::Vec,
};
Expand Down Expand Up @@ -226,8 +230,38 @@ impl DriverManager {
}

/// 参考: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c#434
pub fn driver_sysfs_add(&self, _dev: &Arc<dyn Device>) -> Result<(), SystemError> {
todo!("DriverManager::driver_sysfs_add()");
pub fn driver_sysfs_add(&self, dev: &Arc<dyn Device>) -> Result<(), SystemError> {
if let Some(bus) = dev.bus().and_then(|bus| bus.upgrade()) {
bus.subsystem()
.bus_notifier()
.call_chain(BusNotifyEvent::BindDriver, Some(dev), None);
}
let driver_kobj = dev.driver().unwrap() as Arc<dyn KObject>;
let device_kobj = dev.clone() as Arc<dyn KObject>;

let err_remove_device = |e| {
sysfs_instance().remove_link(&driver_kobj, dev.name());
Err(e)
};

let err_remove_driver = |e| {
sysfs_instance().remove_link(&device_kobj, "driver".to_string());
err_remove_device(e)
};

sysfs_instance().create_link(Some(&driver_kobj), &device_kobj, device_kobj.name())?;

if let Err(e) =
sysfs_instance().create_link(Some(&device_kobj), &driver_kobj, "driver".to_string())
{
return err_remove_device(e);
}

if let Err(e) = device_manager().create_file(dev, &DeviceAttrCoredump) {
return err_remove_driver(e);
}

return Ok(());
}

pub fn add_groups(
Expand Down
5 changes: 5 additions & 0 deletions kernel/src/driver/base/subsys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::{
driver::Driver,
Device,
},
kobject::KObject,
kset::KSet,
};

Expand Down Expand Up @@ -82,6 +83,10 @@ impl SubSysPrivate {
};
}

pub fn name(&self) -> String {
return self.subsys.name();
}

pub fn subsys(&self) -> Arc<KSet> {
return self.subsys.clone();
}
Expand Down
41 changes: 29 additions & 12 deletions kernel/src/driver/net/e1000e/e1000e_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use crate::{
arch::rand::rand,
driver::{
base::{
device::{bus::Bus, driver::Driver, Device, IdTable},
class::Class,
device::{bus::Bus, driver::Driver, Device, DeviceType, IdTable},
kobject::{KObjType, KObject, KObjectState},
},
net::NetDriver,
net::NetDevice,
},
kinfo,
libs::spinlock::SpinLock,
net::{generate_iface_id, NET_DRIVERS},
net::{generate_iface_id, NET_DEVICES},
time::Instant,
};
use alloc::{
Expand Down Expand Up @@ -205,33 +206,49 @@ impl Debug for E1000EInterface {
}
}

impl Driver for E1000EInterface {
fn id_table(&self) -> Option<IdTable> {
impl Device for E1000EInterface {
fn dev_type(&self) -> DeviceType {
todo!()
}

fn add_device(&self, _device: Arc<dyn Device>) {
fn id_table(&self) -> IdTable {
todo!()
}

fn delete_device(&self, _device: &Arc<dyn Device>) {
fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) {
todo!()
}

fn devices(&self) -> alloc::vec::Vec<Arc<dyn Device>> {
fn set_class(&self, _class: Option<Weak<dyn Class>>) {
todo!()
}

fn bus(&self) -> Option<Weak<dyn Bus>> {
fn driver(&self) -> Option<Arc<dyn Driver>> {
todo!()
}

fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) {
fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) {
todo!()
}

fn is_dead(&self) -> bool {
todo!()
}

fn can_match(&self) -> bool {
todo!()
}

fn set_can_match(&self, _can_match: bool) {
todo!()
}

fn state_synced(&self) -> bool {
todo!()
}
}

impl NetDriver for E1000EInterface {
impl NetDevice for E1000EInterface {
fn mac(&self) -> smoltcp::wire::EthernetAddress {
let mac = self.driver.inner.lock().mac_address();
return smoltcp::wire::EthernetAddress::from_bytes(&mac);
Expand Down Expand Up @@ -347,7 +364,7 @@ pub fn e1000e_driver_init(device: E1000EDevice) {
let driver = E1000EDriver::new(device);
let iface = E1000EInterface::new(driver);
// 将网卡的接口信息注册到全局的网卡接口信息表中
NET_DRIVERS
NET_DEVICES
.write_irqsave()
.insert(iface.nic_id(), iface.clone());
kinfo!("e1000e driver init successfully!\tMAC: [{}]", mac);
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/driver/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use smoltcp::{
wire::{self, EthernetAddress},
};

use super::base::device::driver::Driver;
use super::base::device::Device;
use crate::libs::spinlock::SpinLock;
use system_error::SystemError;

Expand All @@ -13,7 +13,7 @@ pub mod e1000e;
pub mod irq_handle;
pub mod virtio_net;

pub trait NetDriver: Driver {
pub trait NetDevice: Device {
/// @brief 获取网卡的MAC地址
fn mac(&self) -> EthernetAddress;

Expand Down
Loading

0 comments on commit e32effb

Please sign in to comment.