1731bc2b3SLoGin use core::{any::Any, fmt::Debug}; 2731bc2b3SLoGin 3731bc2b3SLoGin use alloc::{ 4731bc2b3SLoGin string::{String, ToString}, 5731bc2b3SLoGin sync::{Arc, Weak}, 6731bc2b3SLoGin vec::Vec, 7731bc2b3SLoGin }; 8*9fa0e95eSLoGin use bitmap::traits::BitMapOps; 9*9fa0e95eSLoGin use log::error; 10731bc2b3SLoGin use system_error::SystemError; 11731bc2b3SLoGin use unified_init::macros::unified_init; 12*9fa0e95eSLoGin use virtio_drivers::device::blk::{VirtIOBlk, SECTOR_SIZE}; 13731bc2b3SLoGin 14731bc2b3SLoGin use crate::{ 15731bc2b3SLoGin driver::{ 16731bc2b3SLoGin base::{ 17731bc2b3SLoGin block::{ 18*9fa0e95eSLoGin block_device::{BlockDevName, BlockDevice, BlockId, GeneralBlockRange, LBA_SIZE}, 19731bc2b3SLoGin disk_info::Partition, 20*9fa0e95eSLoGin manager::{block_dev_manager, BlockDevMeta}, 21731bc2b3SLoGin }, 22731bc2b3SLoGin class::Class, 23731bc2b3SLoGin device::{ 24731bc2b3SLoGin bus::Bus, 25731bc2b3SLoGin driver::{Driver, DriverCommonData}, 26731bc2b3SLoGin Device, DeviceCommonData, DeviceId, DeviceType, IdTable, 27731bc2b3SLoGin }, 28731bc2b3SLoGin kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState}, 29731bc2b3SLoGin kset::KSet, 30731bc2b3SLoGin }, 31731bc2b3SLoGin virtio::{ 32731bc2b3SLoGin sysfs::{virtio_bus, virtio_device_manager, virtio_driver_manager}, 33731bc2b3SLoGin transport::VirtIOTransport, 34731bc2b3SLoGin virtio_impl::HalImpl, 35731bc2b3SLoGin VirtIODevice, VirtIODeviceIndex, VirtIODriver, VIRTIO_VENDOR_ID, 36731bc2b3SLoGin }, 37731bc2b3SLoGin }, 380102d69fSLoGin exception::{irqdesc::IrqReturn, IrqNumber}, 39731bc2b3SLoGin filesystem::{kernfs::KernFSInode, mbr::MbrDiskPartionTable}, 40731bc2b3SLoGin init::initcall::INITCALL_POSTCORE, 41731bc2b3SLoGin libs::{ 42731bc2b3SLoGin rwlock::{RwLockReadGuard, RwLockWriteGuard}, 43731bc2b3SLoGin spinlock::{SpinLock, SpinLockGuard}, 44731bc2b3SLoGin }, 45731bc2b3SLoGin }; 46731bc2b3SLoGin 47731bc2b3SLoGin const VIRTIO_BLK_BASENAME: &str = "virtio_blk"; 48731bc2b3SLoGin 49731bc2b3SLoGin static mut VIRTIO_BLK_DRIVER: Option<Arc<VirtIOBlkDriver>> = None; 50731bc2b3SLoGin 51731bc2b3SLoGin #[inline(always)] 52731bc2b3SLoGin fn virtio_blk_driver() -> Arc<VirtIOBlkDriver> { 53731bc2b3SLoGin unsafe { VIRTIO_BLK_DRIVER.as_ref().unwrap().clone() } 54731bc2b3SLoGin } 55731bc2b3SLoGin 56731bc2b3SLoGin /// Get the first virtio block device 57731bc2b3SLoGin #[allow(dead_code)] 58731bc2b3SLoGin pub fn virtio_blk_0() -> Option<Arc<VirtIOBlkDevice>> { 59731bc2b3SLoGin virtio_blk_driver() 60731bc2b3SLoGin .devices() 61731bc2b3SLoGin .first() 62731bc2b3SLoGin .cloned() 63731bc2b3SLoGin .map(|dev| dev.arc_any().downcast().unwrap()) 64731bc2b3SLoGin } 65731bc2b3SLoGin 66731bc2b3SLoGin pub fn virtio_blk(transport: VirtIOTransport, dev_id: Arc<DeviceId>) { 67731bc2b3SLoGin let device = VirtIOBlkDevice::new(transport, dev_id); 68731bc2b3SLoGin if let Some(device) = device { 69731bc2b3SLoGin virtio_device_manager() 70731bc2b3SLoGin .device_add(device.clone() as Arc<dyn VirtIODevice>) 71731bc2b3SLoGin .expect("Add virtio blk failed"); 72731bc2b3SLoGin } 73731bc2b3SLoGin } 74731bc2b3SLoGin 75*9fa0e95eSLoGin static mut VIRTIOBLK_MANAGER: Option<VirtIOBlkManager> = None; 76*9fa0e95eSLoGin 77*9fa0e95eSLoGin #[inline] 78*9fa0e95eSLoGin fn virtioblk_manager() -> &'static VirtIOBlkManager { 79*9fa0e95eSLoGin unsafe { VIRTIOBLK_MANAGER.as_ref().unwrap() } 80*9fa0e95eSLoGin } 81*9fa0e95eSLoGin 82*9fa0e95eSLoGin #[unified_init(INITCALL_POSTCORE)] 83*9fa0e95eSLoGin fn virtioblk_manager_init() -> Result<(), SystemError> { 84*9fa0e95eSLoGin unsafe { 85*9fa0e95eSLoGin VIRTIOBLK_MANAGER = Some(VirtIOBlkManager::new()); 86*9fa0e95eSLoGin } 87*9fa0e95eSLoGin Ok(()) 88*9fa0e95eSLoGin } 89*9fa0e95eSLoGin 90*9fa0e95eSLoGin pub struct VirtIOBlkManager { 91*9fa0e95eSLoGin inner: SpinLock<InnerVirtIOBlkManager>, 92*9fa0e95eSLoGin } 93*9fa0e95eSLoGin 94*9fa0e95eSLoGin struct InnerVirtIOBlkManager { 95*9fa0e95eSLoGin id_bmp: bitmap::StaticBitmap<{ VirtIOBlkManager::MAX_DEVICES }>, 96*9fa0e95eSLoGin devname: [Option<BlockDevName>; VirtIOBlkManager::MAX_DEVICES], 97*9fa0e95eSLoGin } 98*9fa0e95eSLoGin 99*9fa0e95eSLoGin impl VirtIOBlkManager { 100*9fa0e95eSLoGin pub const MAX_DEVICES: usize = 25; 101*9fa0e95eSLoGin 102*9fa0e95eSLoGin pub fn new() -> Self { 103*9fa0e95eSLoGin Self { 104*9fa0e95eSLoGin inner: SpinLock::new(InnerVirtIOBlkManager { 105*9fa0e95eSLoGin id_bmp: bitmap::StaticBitmap::new(), 106*9fa0e95eSLoGin devname: [const { None }; Self::MAX_DEVICES], 107*9fa0e95eSLoGin }), 108*9fa0e95eSLoGin } 109*9fa0e95eSLoGin } 110*9fa0e95eSLoGin 111*9fa0e95eSLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkManager> { 112*9fa0e95eSLoGin self.inner.lock() 113*9fa0e95eSLoGin } 114*9fa0e95eSLoGin 115*9fa0e95eSLoGin pub fn alloc_id(&self) -> Option<BlockDevName> { 116*9fa0e95eSLoGin let mut inner = self.inner(); 117*9fa0e95eSLoGin let idx = inner.id_bmp.first_false_index()?; 118*9fa0e95eSLoGin inner.id_bmp.set(idx, true); 119*9fa0e95eSLoGin let name = Self::format_name(idx); 120*9fa0e95eSLoGin inner.devname[idx] = Some(name.clone()); 121*9fa0e95eSLoGin Some(name) 122*9fa0e95eSLoGin } 123*9fa0e95eSLoGin 124*9fa0e95eSLoGin /// Generate a new block device name like 'vda', 'vdb', etc. 125*9fa0e95eSLoGin fn format_name(id: usize) -> BlockDevName { 126*9fa0e95eSLoGin let x = (b'a' + id as u8) as char; 127*9fa0e95eSLoGin BlockDevName::new(format!("vd{}", x), id) 128*9fa0e95eSLoGin } 129*9fa0e95eSLoGin 130*9fa0e95eSLoGin pub fn free_id(&self, id: usize) { 131*9fa0e95eSLoGin if id >= Self::MAX_DEVICES { 132*9fa0e95eSLoGin return; 133*9fa0e95eSLoGin } 134*9fa0e95eSLoGin self.inner().id_bmp.set(id, false); 135*9fa0e95eSLoGin self.inner().devname[id] = None; 136*9fa0e95eSLoGin } 137*9fa0e95eSLoGin } 138*9fa0e95eSLoGin 139731bc2b3SLoGin /// virtio block device 140731bc2b3SLoGin #[derive(Debug)] 141731bc2b3SLoGin #[cast_to([sync] VirtIODevice)] 142731bc2b3SLoGin #[cast_to([sync] Device)] 143731bc2b3SLoGin pub struct VirtIOBlkDevice { 144*9fa0e95eSLoGin blkdev_meta: BlockDevMeta, 145731bc2b3SLoGin dev_id: Arc<DeviceId>, 146731bc2b3SLoGin inner: SpinLock<InnerVirtIOBlkDevice>, 147731bc2b3SLoGin locked_kobj_state: LockedKObjectState, 148731bc2b3SLoGin self_ref: Weak<Self>, 149731bc2b3SLoGin } 150731bc2b3SLoGin 151731bc2b3SLoGin unsafe impl Send for VirtIOBlkDevice {} 152731bc2b3SLoGin unsafe impl Sync for VirtIOBlkDevice {} 153731bc2b3SLoGin 154731bc2b3SLoGin impl VirtIOBlkDevice { 155731bc2b3SLoGin pub fn new(transport: VirtIOTransport, dev_id: Arc<DeviceId>) -> Option<Arc<Self>> { 156*9fa0e95eSLoGin let devname = virtioblk_manager().alloc_id()?; 1570102d69fSLoGin let irq = transport.irq().map(|irq| IrqNumber::new(irq.data())); 158731bc2b3SLoGin let device_inner = VirtIOBlk::<HalImpl, VirtIOTransport>::new(transport); 159731bc2b3SLoGin if let Err(e) = device_inner { 1602eab6dd7S曾俊 error!("VirtIOBlkDevice '{dev_id:?}' create failed: {:?}", e); 161731bc2b3SLoGin return None; 162731bc2b3SLoGin } 163731bc2b3SLoGin 1640102d69fSLoGin let mut device_inner: VirtIOBlk<HalImpl, VirtIOTransport> = device_inner.unwrap(); 1650102d69fSLoGin device_inner.enable_interrupts(); 166731bc2b3SLoGin let dev = Arc::new_cyclic(|self_ref| Self { 167*9fa0e95eSLoGin blkdev_meta: BlockDevMeta::new(devname), 168731bc2b3SLoGin self_ref: self_ref.clone(), 169731bc2b3SLoGin dev_id, 170731bc2b3SLoGin locked_kobj_state: LockedKObjectState::default(), 171731bc2b3SLoGin inner: SpinLock::new(InnerVirtIOBlkDevice { 172731bc2b3SLoGin device_inner, 173731bc2b3SLoGin name: None, 174731bc2b3SLoGin virtio_index: None, 175731bc2b3SLoGin device_common: DeviceCommonData::default(), 176731bc2b3SLoGin kobject_common: KObjectCommonData::default(), 1770102d69fSLoGin irq, 178731bc2b3SLoGin }), 179731bc2b3SLoGin }); 180731bc2b3SLoGin 181731bc2b3SLoGin dev.set_driver(Some(Arc::downgrade( 182731bc2b3SLoGin &(virtio_blk_driver() as Arc<dyn Driver>), 183731bc2b3SLoGin ))); 184731bc2b3SLoGin 185731bc2b3SLoGin Some(dev) 186731bc2b3SLoGin } 187731bc2b3SLoGin 188731bc2b3SLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDevice> { 189731bc2b3SLoGin self.inner.lock() 190731bc2b3SLoGin } 191731bc2b3SLoGin } 192731bc2b3SLoGin 193731bc2b3SLoGin impl BlockDevice for VirtIOBlkDevice { 194*9fa0e95eSLoGin fn dev_name(&self) -> &BlockDevName { 195*9fa0e95eSLoGin &self.blkdev_meta.devname 196*9fa0e95eSLoGin } 197*9fa0e95eSLoGin 198*9fa0e95eSLoGin fn blkdev_meta(&self) -> &BlockDevMeta { 199*9fa0e95eSLoGin &self.blkdev_meta 200*9fa0e95eSLoGin } 201*9fa0e95eSLoGin 202*9fa0e95eSLoGin fn disk_range(&self) -> GeneralBlockRange { 203*9fa0e95eSLoGin let inner = self.inner(); 204*9fa0e95eSLoGin let blocks = inner.device_inner.capacity() as usize * SECTOR_SIZE / LBA_SIZE; 205*9fa0e95eSLoGin drop(inner); 206*9fa0e95eSLoGin log::debug!( 207*9fa0e95eSLoGin "VirtIOBlkDevice '{:?}' disk_range: 0..{}", 208*9fa0e95eSLoGin self.dev_name(), 209*9fa0e95eSLoGin blocks 210*9fa0e95eSLoGin ); 211*9fa0e95eSLoGin GeneralBlockRange::new(0, blocks).unwrap() 212*9fa0e95eSLoGin } 213*9fa0e95eSLoGin 214731bc2b3SLoGin fn read_at_sync( 215731bc2b3SLoGin &self, 216731bc2b3SLoGin lba_id_start: BlockId, 217731bc2b3SLoGin count: usize, 218731bc2b3SLoGin buf: &mut [u8], 219731bc2b3SLoGin ) -> Result<usize, SystemError> { 220731bc2b3SLoGin let mut inner = self.inner(); 221731bc2b3SLoGin 222731bc2b3SLoGin inner 223731bc2b3SLoGin .device_inner 224731bc2b3SLoGin .read_blocks(lba_id_start, &mut buf[..count * LBA_SIZE]) 225731bc2b3SLoGin .map_err(|e| { 2262eab6dd7S曾俊 error!( 227731bc2b3SLoGin "VirtIOBlkDevice '{:?}' read_at_sync failed: {:?}", 2282eab6dd7S曾俊 self.dev_id, e 229731bc2b3SLoGin ); 230731bc2b3SLoGin SystemError::EIO 231731bc2b3SLoGin })?; 232731bc2b3SLoGin 233731bc2b3SLoGin Ok(count) 234731bc2b3SLoGin } 235731bc2b3SLoGin 236731bc2b3SLoGin fn write_at_sync( 237731bc2b3SLoGin &self, 238731bc2b3SLoGin lba_id_start: BlockId, 239731bc2b3SLoGin count: usize, 240731bc2b3SLoGin buf: &[u8], 241731bc2b3SLoGin ) -> Result<usize, SystemError> { 242731bc2b3SLoGin self.inner() 243731bc2b3SLoGin .device_inner 244731bc2b3SLoGin .write_blocks(lba_id_start, &buf[..count * LBA_SIZE]) 245731bc2b3SLoGin .map_err(|_| SystemError::EIO)?; 246731bc2b3SLoGin Ok(count) 247731bc2b3SLoGin } 248731bc2b3SLoGin 249731bc2b3SLoGin fn sync(&self) -> Result<(), SystemError> { 250731bc2b3SLoGin Ok(()) 251731bc2b3SLoGin } 252731bc2b3SLoGin 253731bc2b3SLoGin fn blk_size_log2(&self) -> u8 { 254731bc2b3SLoGin 9 255731bc2b3SLoGin } 256731bc2b3SLoGin 257731bc2b3SLoGin fn as_any_ref(&self) -> &dyn Any { 258731bc2b3SLoGin self 259731bc2b3SLoGin } 260731bc2b3SLoGin 261731bc2b3SLoGin fn device(&self) -> Arc<dyn Device> { 262731bc2b3SLoGin self.self_ref.upgrade().unwrap() 263731bc2b3SLoGin } 264731bc2b3SLoGin 265731bc2b3SLoGin fn block_size(&self) -> usize { 266731bc2b3SLoGin todo!() 267731bc2b3SLoGin } 268731bc2b3SLoGin 269731bc2b3SLoGin fn partitions(&self) -> Vec<Arc<Partition>> { 270731bc2b3SLoGin let device = self.self_ref.upgrade().unwrap() as Arc<dyn BlockDevice>; 271731bc2b3SLoGin let mbr_table = MbrDiskPartionTable::from_disk(device.clone()) 272731bc2b3SLoGin .expect("Failed to get MBR partition table"); 273731bc2b3SLoGin mbr_table.partitions(Arc::downgrade(&device)) 274731bc2b3SLoGin } 275731bc2b3SLoGin } 276731bc2b3SLoGin 277731bc2b3SLoGin struct InnerVirtIOBlkDevice { 278731bc2b3SLoGin device_inner: VirtIOBlk<HalImpl, VirtIOTransport>, 279731bc2b3SLoGin name: Option<String>, 280731bc2b3SLoGin virtio_index: Option<VirtIODeviceIndex>, 281731bc2b3SLoGin device_common: DeviceCommonData, 282731bc2b3SLoGin kobject_common: KObjectCommonData, 2830102d69fSLoGin irq: Option<IrqNumber>, 284731bc2b3SLoGin } 285731bc2b3SLoGin 286731bc2b3SLoGin impl Debug for InnerVirtIOBlkDevice { 287731bc2b3SLoGin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 288731bc2b3SLoGin f.debug_struct("InnerVirtIOBlkDevice").finish() 289731bc2b3SLoGin } 290731bc2b3SLoGin } 291731bc2b3SLoGin 292731bc2b3SLoGin impl VirtIODevice for VirtIOBlkDevice { 2930102d69fSLoGin fn irq(&self) -> Option<IrqNumber> { 2940102d69fSLoGin self.inner().irq 2950102d69fSLoGin } 2960102d69fSLoGin 297731bc2b3SLoGin fn handle_irq( 298731bc2b3SLoGin &self, 299731bc2b3SLoGin _irq: crate::exception::IrqNumber, 3000102d69fSLoGin ) -> Result<IrqReturn, system_error::SystemError> { 3010102d69fSLoGin // todo: handle virtio blk irq 3020102d69fSLoGin Ok(crate::exception::irqdesc::IrqReturn::Handled) 303731bc2b3SLoGin } 304731bc2b3SLoGin 305731bc2b3SLoGin fn dev_id(&self) -> &Arc<DeviceId> { 306731bc2b3SLoGin &self.dev_id 307731bc2b3SLoGin } 308731bc2b3SLoGin 309731bc2b3SLoGin fn set_device_name(&self, name: String) { 310731bc2b3SLoGin self.inner().name = Some(name); 311731bc2b3SLoGin } 312731bc2b3SLoGin 313731bc2b3SLoGin fn device_name(&self) -> String { 314731bc2b3SLoGin self.inner() 315731bc2b3SLoGin .name 316731bc2b3SLoGin .clone() 317731bc2b3SLoGin .unwrap_or_else(|| VIRTIO_BLK_BASENAME.to_string()) 318731bc2b3SLoGin } 319731bc2b3SLoGin 320731bc2b3SLoGin fn set_virtio_device_index(&self, index: VirtIODeviceIndex) { 321731bc2b3SLoGin self.inner().virtio_index = Some(index); 322731bc2b3SLoGin } 323731bc2b3SLoGin 324731bc2b3SLoGin fn virtio_device_index(&self) -> Option<VirtIODeviceIndex> { 325731bc2b3SLoGin self.inner().virtio_index 326731bc2b3SLoGin } 327731bc2b3SLoGin 328731bc2b3SLoGin fn device_type_id(&self) -> u32 { 329731bc2b3SLoGin virtio_drivers::transport::DeviceType::Block as u32 330731bc2b3SLoGin } 331731bc2b3SLoGin 332731bc2b3SLoGin fn vendor(&self) -> u32 { 333731bc2b3SLoGin VIRTIO_VENDOR_ID.into() 334731bc2b3SLoGin } 335731bc2b3SLoGin } 336731bc2b3SLoGin 337731bc2b3SLoGin impl Device for VirtIOBlkDevice { 338731bc2b3SLoGin fn dev_type(&self) -> DeviceType { 339731bc2b3SLoGin DeviceType::Net 340731bc2b3SLoGin } 341731bc2b3SLoGin 342731bc2b3SLoGin fn id_table(&self) -> IdTable { 343731bc2b3SLoGin IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None) 344731bc2b3SLoGin } 345731bc2b3SLoGin 346731bc2b3SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 347731bc2b3SLoGin self.inner().device_common.bus.clone() 348731bc2b3SLoGin } 349731bc2b3SLoGin 350731bc2b3SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 351731bc2b3SLoGin self.inner().device_common.bus = bus; 352731bc2b3SLoGin } 353731bc2b3SLoGin 354731bc2b3SLoGin fn class(&self) -> Option<Arc<dyn Class>> { 355731bc2b3SLoGin let mut guard = self.inner(); 356731bc2b3SLoGin let r = guard.device_common.class.clone()?.upgrade(); 357731bc2b3SLoGin if r.is_none() { 358731bc2b3SLoGin guard.device_common.class = None; 359731bc2b3SLoGin } 360731bc2b3SLoGin 361731bc2b3SLoGin return r; 362731bc2b3SLoGin } 363731bc2b3SLoGin 364731bc2b3SLoGin fn set_class(&self, class: Option<Weak<dyn Class>>) { 365731bc2b3SLoGin self.inner().device_common.class = class; 366731bc2b3SLoGin } 367731bc2b3SLoGin 368731bc2b3SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 369731bc2b3SLoGin let r = self.inner().device_common.driver.clone()?.upgrade(); 370731bc2b3SLoGin if r.is_none() { 371731bc2b3SLoGin self.inner().device_common.driver = None; 372731bc2b3SLoGin } 373731bc2b3SLoGin 374731bc2b3SLoGin return r; 375731bc2b3SLoGin } 376731bc2b3SLoGin 377731bc2b3SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 378731bc2b3SLoGin self.inner().device_common.driver = driver; 379731bc2b3SLoGin } 380731bc2b3SLoGin 381731bc2b3SLoGin fn is_dead(&self) -> bool { 382731bc2b3SLoGin false 383731bc2b3SLoGin } 384731bc2b3SLoGin 385731bc2b3SLoGin fn can_match(&self) -> bool { 386731bc2b3SLoGin self.inner().device_common.can_match 387731bc2b3SLoGin } 388731bc2b3SLoGin 389731bc2b3SLoGin fn set_can_match(&self, can_match: bool) { 390731bc2b3SLoGin self.inner().device_common.can_match = can_match; 391731bc2b3SLoGin } 392731bc2b3SLoGin 393731bc2b3SLoGin fn state_synced(&self) -> bool { 394731bc2b3SLoGin true 395731bc2b3SLoGin } 396731bc2b3SLoGin } 397731bc2b3SLoGin 398731bc2b3SLoGin impl KObject for VirtIOBlkDevice { 399731bc2b3SLoGin fn as_any_ref(&self) -> &dyn Any { 400731bc2b3SLoGin self 401731bc2b3SLoGin } 402731bc2b3SLoGin 403731bc2b3SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 404731bc2b3SLoGin self.inner().kobject_common.kern_inode = inode; 405731bc2b3SLoGin } 406731bc2b3SLoGin 407731bc2b3SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 408731bc2b3SLoGin self.inner().kobject_common.kern_inode.clone() 409731bc2b3SLoGin } 410731bc2b3SLoGin 411731bc2b3SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 412731bc2b3SLoGin self.inner().kobject_common.parent.clone() 413731bc2b3SLoGin } 414731bc2b3SLoGin 415731bc2b3SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 416731bc2b3SLoGin self.inner().kobject_common.parent = parent; 417731bc2b3SLoGin } 418731bc2b3SLoGin 419731bc2b3SLoGin fn kset(&self) -> Option<Arc<KSet>> { 420731bc2b3SLoGin self.inner().kobject_common.kset.clone() 421731bc2b3SLoGin } 422731bc2b3SLoGin 423731bc2b3SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 424731bc2b3SLoGin self.inner().kobject_common.kset = kset; 425731bc2b3SLoGin } 426731bc2b3SLoGin 427731bc2b3SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 428731bc2b3SLoGin self.inner().kobject_common.kobj_type 429731bc2b3SLoGin } 430731bc2b3SLoGin 431731bc2b3SLoGin fn name(&self) -> String { 432731bc2b3SLoGin self.device_name() 433731bc2b3SLoGin } 434731bc2b3SLoGin 435731bc2b3SLoGin fn set_name(&self, _name: String) { 436731bc2b3SLoGin // do nothing 437731bc2b3SLoGin } 438731bc2b3SLoGin 439731bc2b3SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 440731bc2b3SLoGin self.locked_kobj_state.read() 441731bc2b3SLoGin } 442731bc2b3SLoGin 443731bc2b3SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 444731bc2b3SLoGin self.locked_kobj_state.write() 445731bc2b3SLoGin } 446731bc2b3SLoGin 447731bc2b3SLoGin fn set_kobj_state(&self, state: KObjectState) { 448731bc2b3SLoGin *self.locked_kobj_state.write() = state; 449731bc2b3SLoGin } 450731bc2b3SLoGin 451731bc2b3SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 452731bc2b3SLoGin self.inner().kobject_common.kobj_type = ktype; 453731bc2b3SLoGin } 454731bc2b3SLoGin } 455731bc2b3SLoGin 456731bc2b3SLoGin #[unified_init(INITCALL_POSTCORE)] 457731bc2b3SLoGin fn virtio_blk_driver_init() -> Result<(), SystemError> { 458731bc2b3SLoGin let driver = VirtIOBlkDriver::new(); 459731bc2b3SLoGin virtio_driver_manager() 460731bc2b3SLoGin .register(driver.clone() as Arc<dyn VirtIODriver>) 461731bc2b3SLoGin .expect("Add virtio net driver failed"); 462731bc2b3SLoGin unsafe { 463731bc2b3SLoGin VIRTIO_BLK_DRIVER = Some(driver); 464731bc2b3SLoGin } 465731bc2b3SLoGin 466731bc2b3SLoGin return Ok(()); 467731bc2b3SLoGin } 468731bc2b3SLoGin 469731bc2b3SLoGin #[derive(Debug)] 470731bc2b3SLoGin #[cast_to([sync] VirtIODriver)] 471731bc2b3SLoGin #[cast_to([sync] Driver)] 472731bc2b3SLoGin struct VirtIOBlkDriver { 473731bc2b3SLoGin inner: SpinLock<InnerVirtIOBlkDriver>, 474731bc2b3SLoGin kobj_state: LockedKObjectState, 475731bc2b3SLoGin } 476731bc2b3SLoGin 477731bc2b3SLoGin impl VirtIOBlkDriver { 478731bc2b3SLoGin pub fn new() -> Arc<Self> { 479731bc2b3SLoGin let inner = InnerVirtIOBlkDriver { 480731bc2b3SLoGin driver_common: DriverCommonData::default(), 481731bc2b3SLoGin kobj_common: KObjectCommonData::default(), 482731bc2b3SLoGin }; 483731bc2b3SLoGin Arc::new(VirtIOBlkDriver { 484731bc2b3SLoGin inner: SpinLock::new(inner), 485731bc2b3SLoGin kobj_state: LockedKObjectState::default(), 486731bc2b3SLoGin }) 487731bc2b3SLoGin } 488731bc2b3SLoGin 489731bc2b3SLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDriver> { 490731bc2b3SLoGin return self.inner.lock(); 491731bc2b3SLoGin } 492731bc2b3SLoGin } 493731bc2b3SLoGin 494731bc2b3SLoGin #[derive(Debug)] 495731bc2b3SLoGin struct InnerVirtIOBlkDriver { 496731bc2b3SLoGin driver_common: DriverCommonData, 497731bc2b3SLoGin kobj_common: KObjectCommonData, 498731bc2b3SLoGin } 499731bc2b3SLoGin 500731bc2b3SLoGin impl VirtIODriver for VirtIOBlkDriver { 501731bc2b3SLoGin fn probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError> { 502*9fa0e95eSLoGin let dev = device 503731bc2b3SLoGin .clone() 504731bc2b3SLoGin .arc_any() 505731bc2b3SLoGin .downcast::<VirtIOBlkDevice>() 506731bc2b3SLoGin .map_err(|_| { 5072eab6dd7S曾俊 error!( 508731bc2b3SLoGin "VirtIOBlkDriver::probe() failed: device is not a VirtIO block device. Device: '{:?}'", 509731bc2b3SLoGin device.name() 510731bc2b3SLoGin ); 511731bc2b3SLoGin SystemError::EINVAL 512731bc2b3SLoGin })?; 513731bc2b3SLoGin 514*9fa0e95eSLoGin block_dev_manager().register(dev as Arc<dyn BlockDevice>)?; 515731bc2b3SLoGin return Ok(()); 516731bc2b3SLoGin } 517731bc2b3SLoGin } 518731bc2b3SLoGin 519731bc2b3SLoGin impl Driver for VirtIOBlkDriver { 520731bc2b3SLoGin fn id_table(&self) -> Option<IdTable> { 521731bc2b3SLoGin Some(IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None)) 522731bc2b3SLoGin } 523731bc2b3SLoGin 524731bc2b3SLoGin fn add_device(&self, device: Arc<dyn Device>) { 525731bc2b3SLoGin let iface = device 526731bc2b3SLoGin .arc_any() 527731bc2b3SLoGin .downcast::<VirtIOBlkDevice>() 528731bc2b3SLoGin .expect("VirtIOBlkDriver::add_device() failed: device is not a VirtIOBlkDevice"); 529731bc2b3SLoGin 530731bc2b3SLoGin self.inner() 531731bc2b3SLoGin .driver_common 532731bc2b3SLoGin .devices 533731bc2b3SLoGin .push(iface as Arc<dyn Device>); 534731bc2b3SLoGin } 535731bc2b3SLoGin 536731bc2b3SLoGin fn delete_device(&self, device: &Arc<dyn Device>) { 537731bc2b3SLoGin let _iface = device 538731bc2b3SLoGin .clone() 539731bc2b3SLoGin .arc_any() 540731bc2b3SLoGin .downcast::<VirtIOBlkDevice>() 541731bc2b3SLoGin .expect("VirtIOBlkDriver::delete_device() failed: device is not a VirtIOBlkDevice"); 542731bc2b3SLoGin 543731bc2b3SLoGin let mut guard = self.inner(); 544731bc2b3SLoGin let index = guard 545731bc2b3SLoGin .driver_common 546731bc2b3SLoGin .devices 547731bc2b3SLoGin .iter() 548731bc2b3SLoGin .position(|dev| Arc::ptr_eq(device, dev)) 549731bc2b3SLoGin .expect("VirtIOBlkDriver::delete_device() failed: device not found"); 550731bc2b3SLoGin 551731bc2b3SLoGin guard.driver_common.devices.remove(index); 552731bc2b3SLoGin } 553731bc2b3SLoGin 554731bc2b3SLoGin fn devices(&self) -> Vec<Arc<dyn Device>> { 555731bc2b3SLoGin self.inner().driver_common.devices.clone() 556731bc2b3SLoGin } 557731bc2b3SLoGin 558731bc2b3SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 559731bc2b3SLoGin Some(Arc::downgrade(&virtio_bus()) as Weak<dyn Bus>) 560731bc2b3SLoGin } 561731bc2b3SLoGin 562731bc2b3SLoGin fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) { 563731bc2b3SLoGin // do nothing 564731bc2b3SLoGin } 565731bc2b3SLoGin } 566731bc2b3SLoGin 567731bc2b3SLoGin impl KObject for VirtIOBlkDriver { 568731bc2b3SLoGin fn as_any_ref(&self) -> &dyn Any { 569731bc2b3SLoGin self 570731bc2b3SLoGin } 571731bc2b3SLoGin 572731bc2b3SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 573731bc2b3SLoGin self.inner().kobj_common.kern_inode = inode; 574731bc2b3SLoGin } 575731bc2b3SLoGin 576731bc2b3SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 577731bc2b3SLoGin self.inner().kobj_common.kern_inode.clone() 578731bc2b3SLoGin } 579731bc2b3SLoGin 580731bc2b3SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 581731bc2b3SLoGin self.inner().kobj_common.parent.clone() 582731bc2b3SLoGin } 583731bc2b3SLoGin 584731bc2b3SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 585731bc2b3SLoGin self.inner().kobj_common.parent = parent; 586731bc2b3SLoGin } 587731bc2b3SLoGin 588731bc2b3SLoGin fn kset(&self) -> Option<Arc<KSet>> { 589731bc2b3SLoGin self.inner().kobj_common.kset.clone() 590731bc2b3SLoGin } 591731bc2b3SLoGin 592731bc2b3SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 593731bc2b3SLoGin self.inner().kobj_common.kset = kset; 594731bc2b3SLoGin } 595731bc2b3SLoGin 596731bc2b3SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 597731bc2b3SLoGin self.inner().kobj_common.kobj_type 598731bc2b3SLoGin } 599731bc2b3SLoGin 600731bc2b3SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 601731bc2b3SLoGin self.inner().kobj_common.kobj_type = ktype; 602731bc2b3SLoGin } 603731bc2b3SLoGin 604731bc2b3SLoGin fn name(&self) -> String { 605731bc2b3SLoGin VIRTIO_BLK_BASENAME.to_string() 606731bc2b3SLoGin } 607731bc2b3SLoGin 608731bc2b3SLoGin fn set_name(&self, _name: String) { 609731bc2b3SLoGin // do nothing 610731bc2b3SLoGin } 611731bc2b3SLoGin 612731bc2b3SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 613731bc2b3SLoGin self.kobj_state.read() 614731bc2b3SLoGin } 615731bc2b3SLoGin 616731bc2b3SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 617731bc2b3SLoGin self.kobj_state.write() 618731bc2b3SLoGin } 619731bc2b3SLoGin 620731bc2b3SLoGin fn set_kobj_state(&self, state: KObjectState) { 621731bc2b3SLoGin *self.kobj_state.write() = state; 622731bc2b3SLoGin } 623731bc2b3SLoGin } 624