1731bc2b3SLoGin use core::{any::Any, fmt::Debug}; 2731bc2b3SLoGin 3731bc2b3SLoGin use alloc::{ 4*28fe4ad2S黄铭涛 collections::LinkedList, 5731bc2b3SLoGin string::{String, ToString}, 6731bc2b3SLoGin sync::{Arc, Weak}, 7731bc2b3SLoGin vec::Vec, 8731bc2b3SLoGin }; 99fa0e95eSLoGin use bitmap::traits::BitMapOps; 109fa0e95eSLoGin use log::error; 11731bc2b3SLoGin use system_error::SystemError; 12731bc2b3SLoGin use unified_init::macros::unified_init; 139fa0e95eSLoGin use virtio_drivers::device::blk::{VirtIOBlk, SECTOR_SIZE}; 14731bc2b3SLoGin 15731bc2b3SLoGin use crate::{ 16731bc2b3SLoGin driver::{ 17731bc2b3SLoGin base::{ 18731bc2b3SLoGin block::{ 199fa0e95eSLoGin block_device::{BlockDevName, BlockDevice, BlockId, GeneralBlockRange, LBA_SIZE}, 20731bc2b3SLoGin disk_info::Partition, 219fa0e95eSLoGin manager::{block_dev_manager, BlockDevMeta}, 22731bc2b3SLoGin }, 23731bc2b3SLoGin class::Class, 24731bc2b3SLoGin device::{ 25731bc2b3SLoGin bus::Bus, 26731bc2b3SLoGin driver::{Driver, DriverCommonData}, 27731bc2b3SLoGin Device, DeviceCommonData, DeviceId, DeviceType, IdTable, 28731bc2b3SLoGin }, 29731bc2b3SLoGin kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState}, 30731bc2b3SLoGin kset::KSet, 31731bc2b3SLoGin }, 32731bc2b3SLoGin virtio::{ 33731bc2b3SLoGin sysfs::{virtio_bus, virtio_device_manager, virtio_driver_manager}, 34731bc2b3SLoGin transport::VirtIOTransport, 35731bc2b3SLoGin virtio_impl::HalImpl, 36*28fe4ad2S黄铭涛 VirtIODevice, VirtIODeviceIndex, VirtIODriver, VirtIODriverCommonData, VirtioDeviceId, 37*28fe4ad2S黄铭涛 VIRTIO_VENDOR_ID, 38731bc2b3SLoGin }, 39731bc2b3SLoGin }, 400102d69fSLoGin exception::{irqdesc::IrqReturn, IrqNumber}, 41731bc2b3SLoGin filesystem::{kernfs::KernFSInode, mbr::MbrDiskPartionTable}, 42731bc2b3SLoGin init::initcall::INITCALL_POSTCORE, 43731bc2b3SLoGin libs::{ 44731bc2b3SLoGin rwlock::{RwLockReadGuard, RwLockWriteGuard}, 45731bc2b3SLoGin spinlock::{SpinLock, SpinLockGuard}, 46731bc2b3SLoGin }, 47731bc2b3SLoGin }; 48731bc2b3SLoGin 49731bc2b3SLoGin const VIRTIO_BLK_BASENAME: &str = "virtio_blk"; 50731bc2b3SLoGin 51731bc2b3SLoGin static mut VIRTIO_BLK_DRIVER: Option<Arc<VirtIOBlkDriver>> = None; 52731bc2b3SLoGin 53731bc2b3SLoGin #[inline(always)] 54*28fe4ad2S黄铭涛 #[allow(dead_code)] 55731bc2b3SLoGin fn virtio_blk_driver() -> Arc<VirtIOBlkDriver> { 56731bc2b3SLoGin unsafe { VIRTIO_BLK_DRIVER.as_ref().unwrap().clone() } 57731bc2b3SLoGin } 58731bc2b3SLoGin 59731bc2b3SLoGin /// Get the first virtio block device 60731bc2b3SLoGin #[allow(dead_code)] 61731bc2b3SLoGin pub fn virtio_blk_0() -> Option<Arc<VirtIOBlkDevice>> { 62731bc2b3SLoGin virtio_blk_driver() 63731bc2b3SLoGin .devices() 64731bc2b3SLoGin .first() 65731bc2b3SLoGin .cloned() 66731bc2b3SLoGin .map(|dev| dev.arc_any().downcast().unwrap()) 67731bc2b3SLoGin } 68731bc2b3SLoGin 69*28fe4ad2S黄铭涛 pub fn virtio_blk( 70*28fe4ad2S黄铭涛 transport: VirtIOTransport, 71*28fe4ad2S黄铭涛 dev_id: Arc<DeviceId>, 72*28fe4ad2S黄铭涛 dev_parent: Option<Arc<dyn Device>>, 73*28fe4ad2S黄铭涛 ) { 74731bc2b3SLoGin let device = VirtIOBlkDevice::new(transport, dev_id); 75731bc2b3SLoGin if let Some(device) = device { 76*28fe4ad2S黄铭涛 if let Some(dev_parent) = dev_parent { 77*28fe4ad2S黄铭涛 device.set_dev_parent(Some(Arc::downgrade(&dev_parent))); 78*28fe4ad2S黄铭涛 } 79731bc2b3SLoGin virtio_device_manager() 80731bc2b3SLoGin .device_add(device.clone() as Arc<dyn VirtIODevice>) 81731bc2b3SLoGin .expect("Add virtio blk failed"); 82731bc2b3SLoGin } 83731bc2b3SLoGin } 84731bc2b3SLoGin 859fa0e95eSLoGin static mut VIRTIOBLK_MANAGER: Option<VirtIOBlkManager> = None; 869fa0e95eSLoGin 879fa0e95eSLoGin #[inline] 889fa0e95eSLoGin fn virtioblk_manager() -> &'static VirtIOBlkManager { 899fa0e95eSLoGin unsafe { VIRTIOBLK_MANAGER.as_ref().unwrap() } 909fa0e95eSLoGin } 919fa0e95eSLoGin 929fa0e95eSLoGin #[unified_init(INITCALL_POSTCORE)] 939fa0e95eSLoGin fn virtioblk_manager_init() -> Result<(), SystemError> { 949fa0e95eSLoGin unsafe { 959fa0e95eSLoGin VIRTIOBLK_MANAGER = Some(VirtIOBlkManager::new()); 969fa0e95eSLoGin } 979fa0e95eSLoGin Ok(()) 989fa0e95eSLoGin } 999fa0e95eSLoGin 1009fa0e95eSLoGin pub struct VirtIOBlkManager { 1019fa0e95eSLoGin inner: SpinLock<InnerVirtIOBlkManager>, 1029fa0e95eSLoGin } 1039fa0e95eSLoGin 1049fa0e95eSLoGin struct InnerVirtIOBlkManager { 1059fa0e95eSLoGin id_bmp: bitmap::StaticBitmap<{ VirtIOBlkManager::MAX_DEVICES }>, 1069fa0e95eSLoGin devname: [Option<BlockDevName>; VirtIOBlkManager::MAX_DEVICES], 1079fa0e95eSLoGin } 1089fa0e95eSLoGin 1099fa0e95eSLoGin impl VirtIOBlkManager { 1109fa0e95eSLoGin pub const MAX_DEVICES: usize = 25; 1119fa0e95eSLoGin 1129fa0e95eSLoGin pub fn new() -> Self { 1139fa0e95eSLoGin Self { 1149fa0e95eSLoGin inner: SpinLock::new(InnerVirtIOBlkManager { 1159fa0e95eSLoGin id_bmp: bitmap::StaticBitmap::new(), 1169fa0e95eSLoGin devname: [const { None }; Self::MAX_DEVICES], 1179fa0e95eSLoGin }), 1189fa0e95eSLoGin } 1199fa0e95eSLoGin } 1209fa0e95eSLoGin 1219fa0e95eSLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkManager> { 1229fa0e95eSLoGin self.inner.lock() 1239fa0e95eSLoGin } 1249fa0e95eSLoGin 1259fa0e95eSLoGin pub fn alloc_id(&self) -> Option<BlockDevName> { 1269fa0e95eSLoGin let mut inner = self.inner(); 1279fa0e95eSLoGin let idx = inner.id_bmp.first_false_index()?; 1289fa0e95eSLoGin inner.id_bmp.set(idx, true); 1299fa0e95eSLoGin let name = Self::format_name(idx); 1309fa0e95eSLoGin inner.devname[idx] = Some(name.clone()); 1319fa0e95eSLoGin Some(name) 1329fa0e95eSLoGin } 1339fa0e95eSLoGin 1349fa0e95eSLoGin /// Generate a new block device name like 'vda', 'vdb', etc. 1359fa0e95eSLoGin fn format_name(id: usize) -> BlockDevName { 1369fa0e95eSLoGin let x = (b'a' + id as u8) as char; 1379fa0e95eSLoGin BlockDevName::new(format!("vd{}", x), id) 1389fa0e95eSLoGin } 1399fa0e95eSLoGin 1409fa0e95eSLoGin pub fn free_id(&self, id: usize) { 1419fa0e95eSLoGin if id >= Self::MAX_DEVICES { 1429fa0e95eSLoGin return; 1439fa0e95eSLoGin } 1449fa0e95eSLoGin self.inner().id_bmp.set(id, false); 1459fa0e95eSLoGin self.inner().devname[id] = None; 1469fa0e95eSLoGin } 1479fa0e95eSLoGin } 1489fa0e95eSLoGin 149731bc2b3SLoGin /// virtio block device 150731bc2b3SLoGin #[derive(Debug)] 151731bc2b3SLoGin #[cast_to([sync] VirtIODevice)] 152731bc2b3SLoGin #[cast_to([sync] Device)] 153731bc2b3SLoGin pub struct VirtIOBlkDevice { 1549fa0e95eSLoGin blkdev_meta: BlockDevMeta, 155731bc2b3SLoGin dev_id: Arc<DeviceId>, 156731bc2b3SLoGin inner: SpinLock<InnerVirtIOBlkDevice>, 157731bc2b3SLoGin locked_kobj_state: LockedKObjectState, 158731bc2b3SLoGin self_ref: Weak<Self>, 159731bc2b3SLoGin } 160731bc2b3SLoGin 161731bc2b3SLoGin unsafe impl Send for VirtIOBlkDevice {} 162731bc2b3SLoGin unsafe impl Sync for VirtIOBlkDevice {} 163731bc2b3SLoGin 164731bc2b3SLoGin impl VirtIOBlkDevice { 165731bc2b3SLoGin pub fn new(transport: VirtIOTransport, dev_id: Arc<DeviceId>) -> Option<Arc<Self>> { 1669fa0e95eSLoGin let devname = virtioblk_manager().alloc_id()?; 1670102d69fSLoGin let irq = transport.irq().map(|irq| IrqNumber::new(irq.data())); 168731bc2b3SLoGin let device_inner = VirtIOBlk::<HalImpl, VirtIOTransport>::new(transport); 169731bc2b3SLoGin if let Err(e) = device_inner { 1702eab6dd7S曾俊 error!("VirtIOBlkDevice '{dev_id:?}' create failed: {:?}", e); 171731bc2b3SLoGin return None; 172731bc2b3SLoGin } 173731bc2b3SLoGin 1740102d69fSLoGin let mut device_inner: VirtIOBlk<HalImpl, VirtIOTransport> = device_inner.unwrap(); 1750102d69fSLoGin device_inner.enable_interrupts(); 176731bc2b3SLoGin let dev = Arc::new_cyclic(|self_ref| Self { 1779fa0e95eSLoGin blkdev_meta: BlockDevMeta::new(devname), 178731bc2b3SLoGin self_ref: self_ref.clone(), 179731bc2b3SLoGin dev_id, 180731bc2b3SLoGin locked_kobj_state: LockedKObjectState::default(), 181731bc2b3SLoGin inner: SpinLock::new(InnerVirtIOBlkDevice { 182731bc2b3SLoGin device_inner, 183731bc2b3SLoGin name: None, 184731bc2b3SLoGin virtio_index: None, 185731bc2b3SLoGin device_common: DeviceCommonData::default(), 186731bc2b3SLoGin kobject_common: KObjectCommonData::default(), 1870102d69fSLoGin irq, 188731bc2b3SLoGin }), 189731bc2b3SLoGin }); 190731bc2b3SLoGin 191731bc2b3SLoGin Some(dev) 192731bc2b3SLoGin } 193731bc2b3SLoGin 194731bc2b3SLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDevice> { 195731bc2b3SLoGin self.inner.lock() 196731bc2b3SLoGin } 197731bc2b3SLoGin } 198731bc2b3SLoGin 199731bc2b3SLoGin impl BlockDevice for VirtIOBlkDevice { 2009fa0e95eSLoGin fn dev_name(&self) -> &BlockDevName { 2019fa0e95eSLoGin &self.blkdev_meta.devname 2029fa0e95eSLoGin } 2039fa0e95eSLoGin 2049fa0e95eSLoGin fn blkdev_meta(&self) -> &BlockDevMeta { 2059fa0e95eSLoGin &self.blkdev_meta 2069fa0e95eSLoGin } 2079fa0e95eSLoGin 2089fa0e95eSLoGin fn disk_range(&self) -> GeneralBlockRange { 2099fa0e95eSLoGin let inner = self.inner(); 2109fa0e95eSLoGin let blocks = inner.device_inner.capacity() as usize * SECTOR_SIZE / LBA_SIZE; 2119fa0e95eSLoGin drop(inner); 2129fa0e95eSLoGin log::debug!( 2139fa0e95eSLoGin "VirtIOBlkDevice '{:?}' disk_range: 0..{}", 2149fa0e95eSLoGin self.dev_name(), 2159fa0e95eSLoGin blocks 2169fa0e95eSLoGin ); 2179fa0e95eSLoGin GeneralBlockRange::new(0, blocks).unwrap() 2189fa0e95eSLoGin } 2199fa0e95eSLoGin 220731bc2b3SLoGin fn read_at_sync( 221731bc2b3SLoGin &self, 222731bc2b3SLoGin lba_id_start: BlockId, 223731bc2b3SLoGin count: usize, 224731bc2b3SLoGin buf: &mut [u8], 225731bc2b3SLoGin ) -> Result<usize, SystemError> { 226731bc2b3SLoGin let mut inner = self.inner(); 227731bc2b3SLoGin 228731bc2b3SLoGin inner 229731bc2b3SLoGin .device_inner 230731bc2b3SLoGin .read_blocks(lba_id_start, &mut buf[..count * LBA_SIZE]) 231731bc2b3SLoGin .map_err(|e| { 2322eab6dd7S曾俊 error!( 233731bc2b3SLoGin "VirtIOBlkDevice '{:?}' read_at_sync failed: {:?}", 2342eab6dd7S曾俊 self.dev_id, e 235731bc2b3SLoGin ); 236731bc2b3SLoGin SystemError::EIO 237731bc2b3SLoGin })?; 238731bc2b3SLoGin 239731bc2b3SLoGin Ok(count) 240731bc2b3SLoGin } 241731bc2b3SLoGin 242731bc2b3SLoGin fn write_at_sync( 243731bc2b3SLoGin &self, 244731bc2b3SLoGin lba_id_start: BlockId, 245731bc2b3SLoGin count: usize, 246731bc2b3SLoGin buf: &[u8], 247731bc2b3SLoGin ) -> Result<usize, SystemError> { 248731bc2b3SLoGin self.inner() 249731bc2b3SLoGin .device_inner 250731bc2b3SLoGin .write_blocks(lba_id_start, &buf[..count * LBA_SIZE]) 251731bc2b3SLoGin .map_err(|_| SystemError::EIO)?; 252731bc2b3SLoGin Ok(count) 253731bc2b3SLoGin } 254731bc2b3SLoGin 255731bc2b3SLoGin fn sync(&self) -> Result<(), SystemError> { 256731bc2b3SLoGin Ok(()) 257731bc2b3SLoGin } 258731bc2b3SLoGin 259731bc2b3SLoGin fn blk_size_log2(&self) -> u8 { 260731bc2b3SLoGin 9 261731bc2b3SLoGin } 262731bc2b3SLoGin 263731bc2b3SLoGin fn as_any_ref(&self) -> &dyn Any { 264731bc2b3SLoGin self 265731bc2b3SLoGin } 266731bc2b3SLoGin 267731bc2b3SLoGin fn device(&self) -> Arc<dyn Device> { 268731bc2b3SLoGin self.self_ref.upgrade().unwrap() 269731bc2b3SLoGin } 270731bc2b3SLoGin 271731bc2b3SLoGin fn block_size(&self) -> usize { 272731bc2b3SLoGin todo!() 273731bc2b3SLoGin } 274731bc2b3SLoGin 275731bc2b3SLoGin fn partitions(&self) -> Vec<Arc<Partition>> { 276731bc2b3SLoGin let device = self.self_ref.upgrade().unwrap() as Arc<dyn BlockDevice>; 277731bc2b3SLoGin let mbr_table = MbrDiskPartionTable::from_disk(device.clone()) 278731bc2b3SLoGin .expect("Failed to get MBR partition table"); 279731bc2b3SLoGin mbr_table.partitions(Arc::downgrade(&device)) 280731bc2b3SLoGin } 281731bc2b3SLoGin } 282731bc2b3SLoGin 283731bc2b3SLoGin struct InnerVirtIOBlkDevice { 284731bc2b3SLoGin device_inner: VirtIOBlk<HalImpl, VirtIOTransport>, 285731bc2b3SLoGin name: Option<String>, 286731bc2b3SLoGin virtio_index: Option<VirtIODeviceIndex>, 287731bc2b3SLoGin device_common: DeviceCommonData, 288731bc2b3SLoGin kobject_common: KObjectCommonData, 2890102d69fSLoGin irq: Option<IrqNumber>, 290731bc2b3SLoGin } 291731bc2b3SLoGin 292731bc2b3SLoGin impl Debug for InnerVirtIOBlkDevice { 293731bc2b3SLoGin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 294731bc2b3SLoGin f.debug_struct("InnerVirtIOBlkDevice").finish() 295731bc2b3SLoGin } 296731bc2b3SLoGin } 297731bc2b3SLoGin 298731bc2b3SLoGin impl VirtIODevice for VirtIOBlkDevice { 2990102d69fSLoGin fn irq(&self) -> Option<IrqNumber> { 3000102d69fSLoGin self.inner().irq 3010102d69fSLoGin } 3020102d69fSLoGin 303731bc2b3SLoGin fn handle_irq( 304731bc2b3SLoGin &self, 305731bc2b3SLoGin _irq: crate::exception::IrqNumber, 3060102d69fSLoGin ) -> Result<IrqReturn, system_error::SystemError> { 3070102d69fSLoGin // todo: handle virtio blk irq 3080102d69fSLoGin Ok(crate::exception::irqdesc::IrqReturn::Handled) 309731bc2b3SLoGin } 310731bc2b3SLoGin 311731bc2b3SLoGin fn dev_id(&self) -> &Arc<DeviceId> { 312731bc2b3SLoGin &self.dev_id 313731bc2b3SLoGin } 314731bc2b3SLoGin 315731bc2b3SLoGin fn set_device_name(&self, name: String) { 316731bc2b3SLoGin self.inner().name = Some(name); 317731bc2b3SLoGin } 318731bc2b3SLoGin 319731bc2b3SLoGin fn device_name(&self) -> String { 320731bc2b3SLoGin self.inner() 321731bc2b3SLoGin .name 322731bc2b3SLoGin .clone() 323731bc2b3SLoGin .unwrap_or_else(|| VIRTIO_BLK_BASENAME.to_string()) 324731bc2b3SLoGin } 325731bc2b3SLoGin 326731bc2b3SLoGin fn set_virtio_device_index(&self, index: VirtIODeviceIndex) { 327731bc2b3SLoGin self.inner().virtio_index = Some(index); 328731bc2b3SLoGin } 329731bc2b3SLoGin 330731bc2b3SLoGin fn virtio_device_index(&self) -> Option<VirtIODeviceIndex> { 331731bc2b3SLoGin self.inner().virtio_index 332731bc2b3SLoGin } 333731bc2b3SLoGin 334731bc2b3SLoGin fn device_type_id(&self) -> u32 { 335731bc2b3SLoGin virtio_drivers::transport::DeviceType::Block as u32 336731bc2b3SLoGin } 337731bc2b3SLoGin 338731bc2b3SLoGin fn vendor(&self) -> u32 { 339731bc2b3SLoGin VIRTIO_VENDOR_ID.into() 340731bc2b3SLoGin } 341731bc2b3SLoGin } 342731bc2b3SLoGin 343731bc2b3SLoGin impl Device for VirtIOBlkDevice { 344731bc2b3SLoGin fn dev_type(&self) -> DeviceType { 345731bc2b3SLoGin DeviceType::Net 346731bc2b3SLoGin } 347731bc2b3SLoGin 348731bc2b3SLoGin fn id_table(&self) -> IdTable { 349731bc2b3SLoGin IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None) 350731bc2b3SLoGin } 351731bc2b3SLoGin 352731bc2b3SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 353731bc2b3SLoGin self.inner().device_common.bus.clone() 354731bc2b3SLoGin } 355731bc2b3SLoGin 356731bc2b3SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 357731bc2b3SLoGin self.inner().device_common.bus = bus; 358731bc2b3SLoGin } 359731bc2b3SLoGin 360731bc2b3SLoGin fn class(&self) -> Option<Arc<dyn Class>> { 361731bc2b3SLoGin let mut guard = self.inner(); 362731bc2b3SLoGin let r = guard.device_common.class.clone()?.upgrade(); 363731bc2b3SLoGin if r.is_none() { 364731bc2b3SLoGin guard.device_common.class = None; 365731bc2b3SLoGin } 366731bc2b3SLoGin 367731bc2b3SLoGin return r; 368731bc2b3SLoGin } 369731bc2b3SLoGin 370731bc2b3SLoGin fn set_class(&self, class: Option<Weak<dyn Class>>) { 371731bc2b3SLoGin self.inner().device_common.class = class; 372731bc2b3SLoGin } 373731bc2b3SLoGin 374731bc2b3SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 375731bc2b3SLoGin let r = self.inner().device_common.driver.clone()?.upgrade(); 376731bc2b3SLoGin if r.is_none() { 377731bc2b3SLoGin self.inner().device_common.driver = None; 378731bc2b3SLoGin } 379731bc2b3SLoGin 380731bc2b3SLoGin return r; 381731bc2b3SLoGin } 382731bc2b3SLoGin 383731bc2b3SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 384731bc2b3SLoGin self.inner().device_common.driver = driver; 385731bc2b3SLoGin } 386731bc2b3SLoGin 387731bc2b3SLoGin fn is_dead(&self) -> bool { 388731bc2b3SLoGin false 389731bc2b3SLoGin } 390731bc2b3SLoGin 391731bc2b3SLoGin fn can_match(&self) -> bool { 392731bc2b3SLoGin self.inner().device_common.can_match 393731bc2b3SLoGin } 394731bc2b3SLoGin 395731bc2b3SLoGin fn set_can_match(&self, can_match: bool) { 396731bc2b3SLoGin self.inner().device_common.can_match = can_match; 397731bc2b3SLoGin } 398731bc2b3SLoGin 399731bc2b3SLoGin fn state_synced(&self) -> bool { 400731bc2b3SLoGin true 401731bc2b3SLoGin } 402*28fe4ad2S黄铭涛 403*28fe4ad2S黄铭涛 fn dev_parent(&self) -> Option<Weak<dyn Device>> { 404*28fe4ad2S黄铭涛 self.inner().device_common.get_parent_weak_or_clear() 405*28fe4ad2S黄铭涛 } 406*28fe4ad2S黄铭涛 407*28fe4ad2S黄铭涛 fn set_dev_parent(&self, parent: Option<Weak<dyn Device>>) { 408*28fe4ad2S黄铭涛 self.inner().device_common.parent = parent; 409*28fe4ad2S黄铭涛 } 410731bc2b3SLoGin } 411731bc2b3SLoGin 412731bc2b3SLoGin impl KObject for VirtIOBlkDevice { 413731bc2b3SLoGin fn as_any_ref(&self) -> &dyn Any { 414731bc2b3SLoGin self 415731bc2b3SLoGin } 416731bc2b3SLoGin 417731bc2b3SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 418731bc2b3SLoGin self.inner().kobject_common.kern_inode = inode; 419731bc2b3SLoGin } 420731bc2b3SLoGin 421731bc2b3SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 422731bc2b3SLoGin self.inner().kobject_common.kern_inode.clone() 423731bc2b3SLoGin } 424731bc2b3SLoGin 425731bc2b3SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 426731bc2b3SLoGin self.inner().kobject_common.parent.clone() 427731bc2b3SLoGin } 428731bc2b3SLoGin 429731bc2b3SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 430731bc2b3SLoGin self.inner().kobject_common.parent = parent; 431731bc2b3SLoGin } 432731bc2b3SLoGin 433731bc2b3SLoGin fn kset(&self) -> Option<Arc<KSet>> { 434731bc2b3SLoGin self.inner().kobject_common.kset.clone() 435731bc2b3SLoGin } 436731bc2b3SLoGin 437731bc2b3SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 438731bc2b3SLoGin self.inner().kobject_common.kset = kset; 439731bc2b3SLoGin } 440731bc2b3SLoGin 441731bc2b3SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 442731bc2b3SLoGin self.inner().kobject_common.kobj_type 443731bc2b3SLoGin } 444731bc2b3SLoGin 445731bc2b3SLoGin fn name(&self) -> String { 446731bc2b3SLoGin self.device_name() 447731bc2b3SLoGin } 448731bc2b3SLoGin 449731bc2b3SLoGin fn set_name(&self, _name: String) { 450731bc2b3SLoGin // do nothing 451731bc2b3SLoGin } 452731bc2b3SLoGin 453731bc2b3SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 454731bc2b3SLoGin self.locked_kobj_state.read() 455731bc2b3SLoGin } 456731bc2b3SLoGin 457731bc2b3SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 458731bc2b3SLoGin self.locked_kobj_state.write() 459731bc2b3SLoGin } 460731bc2b3SLoGin 461731bc2b3SLoGin fn set_kobj_state(&self, state: KObjectState) { 462731bc2b3SLoGin *self.locked_kobj_state.write() = state; 463731bc2b3SLoGin } 464731bc2b3SLoGin 465731bc2b3SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 466731bc2b3SLoGin self.inner().kobject_common.kobj_type = ktype; 467731bc2b3SLoGin } 468731bc2b3SLoGin } 469731bc2b3SLoGin 470731bc2b3SLoGin #[unified_init(INITCALL_POSTCORE)] 471731bc2b3SLoGin fn virtio_blk_driver_init() -> Result<(), SystemError> { 472731bc2b3SLoGin let driver = VirtIOBlkDriver::new(); 473731bc2b3SLoGin virtio_driver_manager() 474731bc2b3SLoGin .register(driver.clone() as Arc<dyn VirtIODriver>) 475731bc2b3SLoGin .expect("Add virtio net driver failed"); 476731bc2b3SLoGin unsafe { 477731bc2b3SLoGin VIRTIO_BLK_DRIVER = Some(driver); 478731bc2b3SLoGin } 479731bc2b3SLoGin 480731bc2b3SLoGin return Ok(()); 481731bc2b3SLoGin } 482731bc2b3SLoGin 483731bc2b3SLoGin #[derive(Debug)] 484731bc2b3SLoGin #[cast_to([sync] VirtIODriver)] 485731bc2b3SLoGin #[cast_to([sync] Driver)] 486731bc2b3SLoGin struct VirtIOBlkDriver { 487731bc2b3SLoGin inner: SpinLock<InnerVirtIOBlkDriver>, 488731bc2b3SLoGin kobj_state: LockedKObjectState, 489731bc2b3SLoGin } 490731bc2b3SLoGin 491731bc2b3SLoGin impl VirtIOBlkDriver { 492731bc2b3SLoGin pub fn new() -> Arc<Self> { 493731bc2b3SLoGin let inner = InnerVirtIOBlkDriver { 494*28fe4ad2S黄铭涛 virtio_driver_common: VirtIODriverCommonData::default(), 495731bc2b3SLoGin driver_common: DriverCommonData::default(), 496731bc2b3SLoGin kobj_common: KObjectCommonData::default(), 497731bc2b3SLoGin }; 498*28fe4ad2S黄铭涛 499*28fe4ad2S黄铭涛 let id_table = VirtioDeviceId::new( 500*28fe4ad2S黄铭涛 virtio_drivers::transport::DeviceType::Block as u32, 501*28fe4ad2S黄铭涛 VIRTIO_VENDOR_ID.into(), 502*28fe4ad2S黄铭涛 ); 503*28fe4ad2S黄铭涛 let result = VirtIOBlkDriver { 504731bc2b3SLoGin inner: SpinLock::new(inner), 505731bc2b3SLoGin kobj_state: LockedKObjectState::default(), 506*28fe4ad2S黄铭涛 }; 507*28fe4ad2S黄铭涛 result.add_virtio_id(id_table); 508*28fe4ad2S黄铭涛 509*28fe4ad2S黄铭涛 return Arc::new(result); 510731bc2b3SLoGin } 511731bc2b3SLoGin 512731bc2b3SLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDriver> { 513731bc2b3SLoGin return self.inner.lock(); 514731bc2b3SLoGin } 515731bc2b3SLoGin } 516731bc2b3SLoGin 517731bc2b3SLoGin #[derive(Debug)] 518731bc2b3SLoGin struct InnerVirtIOBlkDriver { 519*28fe4ad2S黄铭涛 virtio_driver_common: VirtIODriverCommonData, 520731bc2b3SLoGin driver_common: DriverCommonData, 521731bc2b3SLoGin kobj_common: KObjectCommonData, 522731bc2b3SLoGin } 523731bc2b3SLoGin 524731bc2b3SLoGin impl VirtIODriver for VirtIOBlkDriver { 525731bc2b3SLoGin fn probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError> { 5269fa0e95eSLoGin let dev = device 527731bc2b3SLoGin .clone() 528731bc2b3SLoGin .arc_any() 529731bc2b3SLoGin .downcast::<VirtIOBlkDevice>() 530731bc2b3SLoGin .map_err(|_| { 5312eab6dd7S曾俊 error!( 532731bc2b3SLoGin "VirtIOBlkDriver::probe() failed: device is not a VirtIO block device. Device: '{:?}'", 533731bc2b3SLoGin device.name() 534731bc2b3SLoGin ); 535731bc2b3SLoGin SystemError::EINVAL 536731bc2b3SLoGin })?; 537731bc2b3SLoGin 5389fa0e95eSLoGin block_dev_manager().register(dev as Arc<dyn BlockDevice>)?; 539731bc2b3SLoGin return Ok(()); 540731bc2b3SLoGin } 541*28fe4ad2S黄铭涛 542*28fe4ad2S黄铭涛 fn virtio_id_table(&self) -> LinkedList<crate::driver::virtio::VirtioDeviceId> { 543*28fe4ad2S黄铭涛 self.inner().virtio_driver_common.id_table.clone() 544*28fe4ad2S黄铭涛 } 545*28fe4ad2S黄铭涛 546*28fe4ad2S黄铭涛 fn add_virtio_id(&self, id: VirtioDeviceId) { 547*28fe4ad2S黄铭涛 self.inner().virtio_driver_common.id_table.push_back(id); 548*28fe4ad2S黄铭涛 } 549731bc2b3SLoGin } 550731bc2b3SLoGin 551731bc2b3SLoGin impl Driver for VirtIOBlkDriver { 552731bc2b3SLoGin fn id_table(&self) -> Option<IdTable> { 553731bc2b3SLoGin Some(IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None)) 554731bc2b3SLoGin } 555731bc2b3SLoGin 556731bc2b3SLoGin fn add_device(&self, device: Arc<dyn Device>) { 557731bc2b3SLoGin let iface = device 558731bc2b3SLoGin .arc_any() 559731bc2b3SLoGin .downcast::<VirtIOBlkDevice>() 560731bc2b3SLoGin .expect("VirtIOBlkDriver::add_device() failed: device is not a VirtIOBlkDevice"); 561731bc2b3SLoGin 562731bc2b3SLoGin self.inner() 563731bc2b3SLoGin .driver_common 564731bc2b3SLoGin .devices 565731bc2b3SLoGin .push(iface as Arc<dyn Device>); 566731bc2b3SLoGin } 567731bc2b3SLoGin 568731bc2b3SLoGin fn delete_device(&self, device: &Arc<dyn Device>) { 569731bc2b3SLoGin let _iface = device 570731bc2b3SLoGin .clone() 571731bc2b3SLoGin .arc_any() 572731bc2b3SLoGin .downcast::<VirtIOBlkDevice>() 573731bc2b3SLoGin .expect("VirtIOBlkDriver::delete_device() failed: device is not a VirtIOBlkDevice"); 574731bc2b3SLoGin 575731bc2b3SLoGin let mut guard = self.inner(); 576731bc2b3SLoGin let index = guard 577731bc2b3SLoGin .driver_common 578731bc2b3SLoGin .devices 579731bc2b3SLoGin .iter() 580731bc2b3SLoGin .position(|dev| Arc::ptr_eq(device, dev)) 581731bc2b3SLoGin .expect("VirtIOBlkDriver::delete_device() failed: device not found"); 582731bc2b3SLoGin 583731bc2b3SLoGin guard.driver_common.devices.remove(index); 584731bc2b3SLoGin } 585731bc2b3SLoGin 586731bc2b3SLoGin fn devices(&self) -> Vec<Arc<dyn Device>> { 587731bc2b3SLoGin self.inner().driver_common.devices.clone() 588731bc2b3SLoGin } 589731bc2b3SLoGin 590731bc2b3SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 591731bc2b3SLoGin Some(Arc::downgrade(&virtio_bus()) as Weak<dyn Bus>) 592731bc2b3SLoGin } 593731bc2b3SLoGin 594731bc2b3SLoGin fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) { 595731bc2b3SLoGin // do nothing 596731bc2b3SLoGin } 597731bc2b3SLoGin } 598731bc2b3SLoGin 599731bc2b3SLoGin impl KObject for VirtIOBlkDriver { 600731bc2b3SLoGin fn as_any_ref(&self) -> &dyn Any { 601731bc2b3SLoGin self 602731bc2b3SLoGin } 603731bc2b3SLoGin 604731bc2b3SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 605731bc2b3SLoGin self.inner().kobj_common.kern_inode = inode; 606731bc2b3SLoGin } 607731bc2b3SLoGin 608731bc2b3SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 609731bc2b3SLoGin self.inner().kobj_common.kern_inode.clone() 610731bc2b3SLoGin } 611731bc2b3SLoGin 612731bc2b3SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 613731bc2b3SLoGin self.inner().kobj_common.parent.clone() 614731bc2b3SLoGin } 615731bc2b3SLoGin 616731bc2b3SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 617731bc2b3SLoGin self.inner().kobj_common.parent = parent; 618731bc2b3SLoGin } 619731bc2b3SLoGin 620731bc2b3SLoGin fn kset(&self) -> Option<Arc<KSet>> { 621731bc2b3SLoGin self.inner().kobj_common.kset.clone() 622731bc2b3SLoGin } 623731bc2b3SLoGin 624731bc2b3SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 625731bc2b3SLoGin self.inner().kobj_common.kset = kset; 626731bc2b3SLoGin } 627731bc2b3SLoGin 628731bc2b3SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 629731bc2b3SLoGin self.inner().kobj_common.kobj_type 630731bc2b3SLoGin } 631731bc2b3SLoGin 632731bc2b3SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 633731bc2b3SLoGin self.inner().kobj_common.kobj_type = ktype; 634731bc2b3SLoGin } 635731bc2b3SLoGin 636731bc2b3SLoGin fn name(&self) -> String { 637731bc2b3SLoGin VIRTIO_BLK_BASENAME.to_string() 638731bc2b3SLoGin } 639731bc2b3SLoGin 640731bc2b3SLoGin fn set_name(&self, _name: String) { 641731bc2b3SLoGin // do nothing 642731bc2b3SLoGin } 643731bc2b3SLoGin 644731bc2b3SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 645731bc2b3SLoGin self.kobj_state.read() 646731bc2b3SLoGin } 647731bc2b3SLoGin 648731bc2b3SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 649731bc2b3SLoGin self.kobj_state.write() 650731bc2b3SLoGin } 651731bc2b3SLoGin 652731bc2b3SLoGin fn set_kobj_state(&self, state: KObjectState) { 653731bc2b3SLoGin *self.kobj_state.write() = state; 654731bc2b3SLoGin } 655731bc2b3SLoGin } 656