xref: /DragonOS/kernel/src/driver/virtio/mod.rs (revision 28fe4ad2a0b0d8b5abf1f0cb402b1c3204b42242)
1 use alloc::{collections::LinkedList, string::String, sync::Arc};
2 use system_error::SystemError;
3 
4 use crate::exception::{irqdesc::IrqReturn, IrqNumber};
5 
6 use super::base::device::{driver::Driver, Device, DeviceId};
7 
8 pub(super) mod irq;
9 pub mod mmio;
10 pub mod sysfs;
11 pub mod transport;
12 pub mod transport_mmio;
13 pub mod transport_pci;
14 #[allow(clippy::module_inception)]
15 pub mod virtio;
16 pub mod virtio_impl;
17 
18 /// virtio 设备厂商ID
19 pub const VIRTIO_VENDOR_ID: u16 = 0x1af4;
20 // 参考:https://code.dragonos.org.cn/xref/linux-6.6.21/include/linux/mod_devicetable.h?fi=VIRTIO_DEV_ANY_ID#453
21 pub const VIRTIO_DEV_ANY_ID: u32 = 0xffffffff;
22 
23 #[allow(dead_code)]
24 pub trait VirtIODevice: Device {
handle_irq(&self, _irq: IrqNumber) -> Result<IrqReturn, SystemError>25     fn handle_irq(&self, _irq: IrqNumber) -> Result<IrqReturn, SystemError>;
26 
dev_id(&self) -> &Arc<DeviceId>27     fn dev_id(&self) -> &Arc<DeviceId>;
28 
set_device_name(&self, name: String)29     fn set_device_name(&self, name: String);
30 
device_name(&self) -> String31     fn device_name(&self) -> String;
32 
set_virtio_device_index(&self, index: VirtIODeviceIndex)33     fn set_virtio_device_index(&self, index: VirtIODeviceIndex);
34 
virtio_device_index(&self) -> Option<VirtIODeviceIndex>35     fn virtio_device_index(&self) -> Option<VirtIODeviceIndex>;
36 
37     /// virtio 设备类型
device_type_id(&self) -> u3238     fn device_type_id(&self) -> u32;
39 
40     /// virtio 设备厂商
vendor(&self) -> u3241     fn vendor(&self) -> u32;
42 
43     /// virtio设备的中断号
irq(&self) -> Option<IrqNumber>44     fn irq(&self) -> Option<IrqNumber>;
45 
set_irq_number(&self, _irq: IrqNumber) -> Result<(), SystemError>46     fn set_irq_number(&self, _irq: IrqNumber) -> Result<(), SystemError> {
47         Err(SystemError::ENOSYS)
48     }
49 }
50 
51 pub trait VirtIODriver: Driver {
probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError>52     fn probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError>;
53 
virtio_id_table(&self) -> LinkedList<VirtioDeviceId>54     fn virtio_id_table(&self) -> LinkedList<VirtioDeviceId>;
55 
add_virtio_id(&self, id: VirtioDeviceId)56     fn add_virtio_id(&self, id: VirtioDeviceId);
57 }
58 
59 int_like!(VirtIODeviceIndex, usize);
60 
61 #[derive(Debug, Default)]
62 pub struct VirtIODriverCommonData {
63     pub id_table: LinkedList<VirtioDeviceId>,
64 }
65 
66 /// 参考:https://code.dragonos.org.cn/xref/linux-6.6.21/include/linux/mod_devicetable.h#449
67 #[derive(Debug, Default, Clone)]
68 pub struct VirtioDeviceId {
69     pub device: u32,
70     pub vendor: u32,
71 }
72 
73 impl VirtioDeviceId {
new(device: u32, vendor: u32) -> Self74     pub fn new(device: u32, vendor: u32) -> Self {
75         Self { device, vendor }
76     }
77 }
78