xref: /DragonOS/kernel/src/driver/virtio/mod.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
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 {
25     fn handle_irq(&self, _irq: IrqNumber) -> Result<IrqReturn, SystemError>;
26 
27     fn dev_id(&self) -> &Arc<DeviceId>;
28 
29     fn set_device_name(&self, name: String);
30 
31     fn device_name(&self) -> String;
32 
33     fn set_virtio_device_index(&self, index: VirtIODeviceIndex);
34 
35     fn virtio_device_index(&self) -> Option<VirtIODeviceIndex>;
36 
37     /// virtio 设备类型
38     fn device_type_id(&self) -> u32;
39 
40     /// virtio 设备厂商
41     fn vendor(&self) -> u32;
42 
43     /// virtio设备的中断号
44     fn irq(&self) -> Option<IrqNumber>;
45 
46     fn set_irq_number(&self, _irq: IrqNumber) -> Result<(), SystemError> {
47         Err(SystemError::ENOSYS)
48     }
49 }
50 
51 pub trait VirtIODriver: Driver {
52     fn probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError>;
53 
54     fn virtio_id_table(&self) -> LinkedList<VirtioDeviceId>;
55 
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 {
74     pub fn new(device: u32, vendor: u32) -> Self {
75         Self { device, vendor }
76     }
77 }
78