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::bus::Bus; 5 use crate::driver::base::device::{Device, DeviceId}; 6 use crate::driver::block::virtio_blk::virtio_blk; 7 use crate::driver::net::virtio_net::virtio_net; 8 use crate::driver::pci::pci::{ 9 get_pci_device_structures_mut_by_vendor_id, PciDeviceStructureGeneralDevice, 10 PCI_DEVICE_LINKEDLIST, 11 }; 12 use crate::driver::pci::subsys::pci_bus; 13 use crate::driver::virtio::transport::VirtIOTransport; 14 15 use alloc::string::String; 16 use alloc::sync::Arc; 17 use alloc::vec::Vec; 18 use log::{debug, error, warn}; 19 use virtio_drivers::transport::{DeviceType, Transport}; 20 21 ///@brief 寻找并加载所有virtio设备的驱动(目前只有virtio-net,但其他virtio设备也可添加) 22 pub fn virtio_probe() { 23 #[cfg(not(target_arch = "riscv64"))] 24 virtio_probe_pci(); 25 virtio_probe_mmio(); 26 } 27 28 #[allow(dead_code)] 29 fn virtio_probe_pci() { 30 let virtio_list = virtio_device_search(); 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.clone(), 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 // 这里暂时通过设备名称在sysfs中查找设备,但是我感觉用设备ID更好 43 let bus = pci_bus() as Arc<dyn Bus>; 44 let name: String = virtio_device.common_header.bus_device_function.into(); 45 let pci_raw_device = bus.find_device_by_name(name.as_str()); 46 virtio_device_init(transport, dev_id, pci_raw_device); 47 } 48 Err(err) => { 49 error!("Pci transport create failed because of error: {}", err); 50 } 51 } 52 } 53 } 54 55 ///@brief 为virtio设备寻找对应的驱动进行初始化 56 pub(super) fn virtio_device_init( 57 transport: VirtIOTransport, 58 dev_id: Arc<DeviceId>, 59 dev_parent: Option<Arc<dyn Device>>, 60 ) { 61 match transport.device_type() { 62 DeviceType::Block => virtio_blk(transport, dev_id, dev_parent), 63 DeviceType::GPU => { 64 warn!("Not support virtio_gpu device for now"); 65 } 66 DeviceType::Input => { 67 warn!("Not support virtio_input device for now"); 68 } 69 DeviceType::Network => virtio_net(transport, dev_id, dev_parent), 70 t => { 71 warn!("Unrecognized virtio device: {:?}", t); 72 } 73 } 74 } 75 76 /// # virtio_device_search - 在给定的PCI设备列表中搜索符合特定标准的virtio设备 77 /// 78 /// 该函数搜索一个PCI设备列表,找到所有由特定厂商ID(0x1AF4)和设备ID范围(0x1000至0x103F)定义的virtio设备。 79 /// 80 /// ## 参数 81 /// 82 /// - list: &'a mut RwLockWriteGuard<'_, LinkedList<Box<dyn PciDeviceStructure>>> - 一个可写的PCI设备结构列表的互斥锁。 83 /// 84 /// ## 返回值 85 /// 86 /// 返回一个包含所有找到的virtio设备的数组 87 fn virtio_device_search() -> Vec<Arc<PciDeviceStructureGeneralDevice>> { 88 let list = &*PCI_DEVICE_LINKEDLIST; 89 let mut virtio_list = Vec::new(); 90 let result = get_pci_device_structures_mut_by_vendor_id(list, 0x1AF4); 91 92 for device in result { 93 let standard_device = device.as_standard_device().unwrap(); 94 let header = &standard_device.common_header; 95 if header.device_id >= 0x1000 && header.device_id <= 0x103F { 96 virtio_list.push(standard_device); 97 } 98 } 99 return virtio_list; 100 } 101