xref: /DragonOS/kernel/src/driver/virtio/virtio.rs (revision f79998f626801329580c782fd05e36cb2027f474)
1 use super::mmio::virtio_probe_mmio;
2 use super::transport_pci::PciTransport;
3 use super::virtio_impl::HalImpl;
4 use crate::driver::base::device::DeviceId;
5 use crate::driver::block::virtio_blk::virtio_blk;
6 use crate::driver::net::virtio_net::virtio_net;
7 use crate::driver::pci::pci::{
8     get_pci_device_structures_mut_by_vendor_id, PciDeviceStructure,
9     PciDeviceStructureGeneralDevice, PCI_DEVICE_LINKEDLIST,
10 };
11 use crate::driver::virtio::transport::VirtIOTransport;
12 use crate::libs::rwlock::RwLockWriteGuard;
13 
14 use alloc::sync::Arc;
15 use alloc::vec::Vec;
16 use alloc::{boxed::Box, collections::LinkedList};
17 use log::{debug, error, warn};
18 use virtio_drivers::transport::{DeviceType, Transport};
19 
20 ///@brief 寻找并加载所有virtio设备的驱动(目前只有virtio-net,但其他virtio设备也可添加)
21 pub fn virtio_probe() {
22     #[cfg(not(target_arch = "riscv64"))]
23     virtio_probe_pci();
24     virtio_probe_mmio();
25 }
26 
27 #[allow(dead_code)]
28 fn virtio_probe_pci() {
29     let mut list = PCI_DEVICE_LINKEDLIST.write();
30     let virtio_list = virtio_device_search(&mut list);
31     for virtio_device in virtio_list {
32         let dev_id = virtio_device.common_header.device_id;
33         let dev_id = DeviceId::new(None, Some(format!("{dev_id}"))).unwrap();
34         match PciTransport::new::<HalImpl>(virtio_device, dev_id.clone()) {
35             Ok(mut transport) => {
36                 debug!(
37                     "Detected virtio PCI device with device type {:?}, features {:#018x}",
38                     transport.device_type(),
39                     transport.read_device_features(),
40                 );
41                 let transport = VirtIOTransport::Pci(transport);
42                 virtio_device_init(transport, dev_id);
43             }
44             Err(err) => {
45                 error!("Pci transport create failed because of error: {}", err);
46             }
47         }
48     }
49 }
50 
51 ///@brief 为virtio设备寻找对应的驱动进行初始化
52 pub(super) fn virtio_device_init(transport: VirtIOTransport, dev_id: Arc<DeviceId>) {
53     match transport.device_type() {
54         DeviceType::Block => virtio_blk(transport, dev_id),
55         DeviceType::GPU => {
56             warn!("Not support virtio_gpu device for now");
57         }
58         DeviceType::Input => {
59             warn!("Not support virtio_input device for now");
60         }
61         DeviceType::Network => virtio_net(transport, dev_id),
62         t => {
63             warn!("Unrecognized virtio device: {:?}", t);
64         }
65     }
66 }
67 
68 /// # virtio_device_search - 在给定的PCI设备列表中搜索符合特定标准的virtio设备
69 ///
70 /// 该函数搜索一个PCI设备列表,找到所有由特定厂商ID(0x1AF4)和设备ID范围(0x1000至0x103F)定义的virtio设备。
71 ///
72 /// ## 参数
73 ///
74 /// - list: &'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>> - 一个可写的PCI设备结构列表的互斥锁。
75 ///
76 /// ## 返回值
77 ///
78 /// 返回一个包含所有找到的virtio设备的数组
79 fn virtio_device_search<'a>(
80     list: &'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>>,
81 ) -> Vec<&'a mut PciDeviceStructureGeneralDevice> {
82     let mut virtio_list = Vec::new();
83     let result = get_pci_device_structures_mut_by_vendor_id(list, 0x1AF4);
84 
85     for device in result {
86         let standard_device = device.as_standard_device_mut().unwrap();
87         let header = &standard_device.common_header;
88         if header.device_id >= 0x1000 && header.device_id <= 0x103F {
89             virtio_list.push(standard_device);
90         }
91     }
92 
93     return virtio_list;
94 }
95