xref: /DragonOS/kernel/src/driver/block/virtio_blk.rs (revision 731bc2b32d7b37298883d7a15b6dca659b436ee4)
1*731bc2b3SLoGin use core::{any::Any, fmt::Debug};
2*731bc2b3SLoGin 
3*731bc2b3SLoGin use alloc::{
4*731bc2b3SLoGin     string::{String, ToString},
5*731bc2b3SLoGin     sync::{Arc, Weak},
6*731bc2b3SLoGin     vec::Vec,
7*731bc2b3SLoGin };
8*731bc2b3SLoGin use system_error::SystemError;
9*731bc2b3SLoGin use unified_init::macros::unified_init;
10*731bc2b3SLoGin use virtio_drivers::device::blk::VirtIOBlk;
11*731bc2b3SLoGin 
12*731bc2b3SLoGin use crate::{
13*731bc2b3SLoGin     driver::{
14*731bc2b3SLoGin         base::{
15*731bc2b3SLoGin             block::{
16*731bc2b3SLoGin                 block_device::{BlockDevice, BlockId, LBA_SIZE},
17*731bc2b3SLoGin                 disk_info::Partition,
18*731bc2b3SLoGin             },
19*731bc2b3SLoGin             class::Class,
20*731bc2b3SLoGin             device::{
21*731bc2b3SLoGin                 bus::Bus,
22*731bc2b3SLoGin                 driver::{Driver, DriverCommonData},
23*731bc2b3SLoGin                 Device, DeviceCommonData, DeviceId, DeviceType, IdTable,
24*731bc2b3SLoGin             },
25*731bc2b3SLoGin             kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
26*731bc2b3SLoGin             kset::KSet,
27*731bc2b3SLoGin         },
28*731bc2b3SLoGin         virtio::{
29*731bc2b3SLoGin             sysfs::{virtio_bus, virtio_device_manager, virtio_driver_manager},
30*731bc2b3SLoGin             transport::VirtIOTransport,
31*731bc2b3SLoGin             virtio_impl::HalImpl,
32*731bc2b3SLoGin             VirtIODevice, VirtIODeviceIndex, VirtIODriver, VIRTIO_VENDOR_ID,
33*731bc2b3SLoGin         },
34*731bc2b3SLoGin     },
35*731bc2b3SLoGin     filesystem::{kernfs::KernFSInode, mbr::MbrDiskPartionTable},
36*731bc2b3SLoGin     init::initcall::INITCALL_POSTCORE,
37*731bc2b3SLoGin     libs::{
38*731bc2b3SLoGin         rwlock::{RwLockReadGuard, RwLockWriteGuard},
39*731bc2b3SLoGin         spinlock::{SpinLock, SpinLockGuard},
40*731bc2b3SLoGin     },
41*731bc2b3SLoGin };
42*731bc2b3SLoGin 
43*731bc2b3SLoGin const VIRTIO_BLK_BASENAME: &str = "virtio_blk";
44*731bc2b3SLoGin 
45*731bc2b3SLoGin static mut VIRTIO_BLK_DRIVER: Option<Arc<VirtIOBlkDriver>> = None;
46*731bc2b3SLoGin 
47*731bc2b3SLoGin #[inline(always)]
48*731bc2b3SLoGin fn virtio_blk_driver() -> Arc<VirtIOBlkDriver> {
49*731bc2b3SLoGin     unsafe { VIRTIO_BLK_DRIVER.as_ref().unwrap().clone() }
50*731bc2b3SLoGin }
51*731bc2b3SLoGin 
52*731bc2b3SLoGin /// Get the first virtio block device
53*731bc2b3SLoGin #[allow(dead_code)]
54*731bc2b3SLoGin pub fn virtio_blk_0() -> Option<Arc<VirtIOBlkDevice>> {
55*731bc2b3SLoGin     virtio_blk_driver()
56*731bc2b3SLoGin         .devices()
57*731bc2b3SLoGin         .first()
58*731bc2b3SLoGin         .cloned()
59*731bc2b3SLoGin         .map(|dev| dev.arc_any().downcast().unwrap())
60*731bc2b3SLoGin }
61*731bc2b3SLoGin 
62*731bc2b3SLoGin pub fn virtio_blk(transport: VirtIOTransport, dev_id: Arc<DeviceId>) {
63*731bc2b3SLoGin     let device = VirtIOBlkDevice::new(transport, dev_id);
64*731bc2b3SLoGin     if let Some(device) = device {
65*731bc2b3SLoGin         kdebug!("VirtIOBlkDevice '{:?}' created", device.dev_id);
66*731bc2b3SLoGin         virtio_device_manager()
67*731bc2b3SLoGin             .device_add(device.clone() as Arc<dyn VirtIODevice>)
68*731bc2b3SLoGin             .expect("Add virtio blk failed");
69*731bc2b3SLoGin     }
70*731bc2b3SLoGin }
71*731bc2b3SLoGin 
72*731bc2b3SLoGin /// virtio block device
73*731bc2b3SLoGin #[derive(Debug)]
74*731bc2b3SLoGin #[cast_to([sync] VirtIODevice)]
75*731bc2b3SLoGin #[cast_to([sync] Device)]
76*731bc2b3SLoGin pub struct VirtIOBlkDevice {
77*731bc2b3SLoGin     dev_id: Arc<DeviceId>,
78*731bc2b3SLoGin     inner: SpinLock<InnerVirtIOBlkDevice>,
79*731bc2b3SLoGin     locked_kobj_state: LockedKObjectState,
80*731bc2b3SLoGin     self_ref: Weak<Self>,
81*731bc2b3SLoGin }
82*731bc2b3SLoGin 
83*731bc2b3SLoGin unsafe impl Send for VirtIOBlkDevice {}
84*731bc2b3SLoGin unsafe impl Sync for VirtIOBlkDevice {}
85*731bc2b3SLoGin 
86*731bc2b3SLoGin impl VirtIOBlkDevice {
87*731bc2b3SLoGin     pub fn new(transport: VirtIOTransport, dev_id: Arc<DeviceId>) -> Option<Arc<Self>> {
88*731bc2b3SLoGin         let device_inner = VirtIOBlk::<HalImpl, VirtIOTransport>::new(transport);
89*731bc2b3SLoGin         if let Err(e) = device_inner {
90*731bc2b3SLoGin             kerror!("VirtIOBlkDevice '{dev_id:?}' create failed: {:?}", e);
91*731bc2b3SLoGin             return None;
92*731bc2b3SLoGin         }
93*731bc2b3SLoGin         // !!!! 在这里临时测试virtio-blk的读写功能,后续需要删除 !!!!
94*731bc2b3SLoGin         // 目前read会报错 `NotReady`
95*731bc2b3SLoGin         let device_inner: VirtIOBlk<HalImpl, VirtIOTransport> = device_inner.unwrap();
96*731bc2b3SLoGin 
97*731bc2b3SLoGin         let dev = Arc::new_cyclic(|self_ref| Self {
98*731bc2b3SLoGin             self_ref: self_ref.clone(),
99*731bc2b3SLoGin             dev_id,
100*731bc2b3SLoGin             locked_kobj_state: LockedKObjectState::default(),
101*731bc2b3SLoGin             inner: SpinLock::new(InnerVirtIOBlkDevice {
102*731bc2b3SLoGin                 device_inner,
103*731bc2b3SLoGin                 name: None,
104*731bc2b3SLoGin                 virtio_index: None,
105*731bc2b3SLoGin                 device_common: DeviceCommonData::default(),
106*731bc2b3SLoGin                 kobject_common: KObjectCommonData::default(),
107*731bc2b3SLoGin             }),
108*731bc2b3SLoGin         });
109*731bc2b3SLoGin 
110*731bc2b3SLoGin         dev.set_driver(Some(Arc::downgrade(
111*731bc2b3SLoGin             &(virtio_blk_driver() as Arc<dyn Driver>),
112*731bc2b3SLoGin         )));
113*731bc2b3SLoGin 
114*731bc2b3SLoGin         Some(dev)
115*731bc2b3SLoGin     }
116*731bc2b3SLoGin 
117*731bc2b3SLoGin     fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDevice> {
118*731bc2b3SLoGin         self.inner.lock()
119*731bc2b3SLoGin     }
120*731bc2b3SLoGin }
121*731bc2b3SLoGin 
122*731bc2b3SLoGin impl BlockDevice for VirtIOBlkDevice {
123*731bc2b3SLoGin     fn read_at_sync(
124*731bc2b3SLoGin         &self,
125*731bc2b3SLoGin         lba_id_start: BlockId,
126*731bc2b3SLoGin         count: usize,
127*731bc2b3SLoGin         buf: &mut [u8],
128*731bc2b3SLoGin     ) -> Result<usize, SystemError> {
129*731bc2b3SLoGin         let mut inner = self.inner();
130*731bc2b3SLoGin 
131*731bc2b3SLoGin         inner
132*731bc2b3SLoGin             .device_inner
133*731bc2b3SLoGin             .read_blocks(lba_id_start, &mut buf[..count * LBA_SIZE])
134*731bc2b3SLoGin             .map_err(|e| {
135*731bc2b3SLoGin                 kerror!(
136*731bc2b3SLoGin                     "VirtIOBlkDevice '{:?}' read_at_sync failed: {:?}",
137*731bc2b3SLoGin                     self.dev_id,
138*731bc2b3SLoGin                     e
139*731bc2b3SLoGin                 );
140*731bc2b3SLoGin                 SystemError::EIO
141*731bc2b3SLoGin             })?;
142*731bc2b3SLoGin 
143*731bc2b3SLoGin         Ok(count)
144*731bc2b3SLoGin     }
145*731bc2b3SLoGin 
146*731bc2b3SLoGin     fn write_at_sync(
147*731bc2b3SLoGin         &self,
148*731bc2b3SLoGin         lba_id_start: BlockId,
149*731bc2b3SLoGin         count: usize,
150*731bc2b3SLoGin         buf: &[u8],
151*731bc2b3SLoGin     ) -> Result<usize, SystemError> {
152*731bc2b3SLoGin         self.inner()
153*731bc2b3SLoGin             .device_inner
154*731bc2b3SLoGin             .write_blocks(lba_id_start, &buf[..count * LBA_SIZE])
155*731bc2b3SLoGin             .map_err(|_| SystemError::EIO)?;
156*731bc2b3SLoGin         Ok(count)
157*731bc2b3SLoGin     }
158*731bc2b3SLoGin 
159*731bc2b3SLoGin     fn sync(&self) -> Result<(), SystemError> {
160*731bc2b3SLoGin         Ok(())
161*731bc2b3SLoGin     }
162*731bc2b3SLoGin 
163*731bc2b3SLoGin     fn blk_size_log2(&self) -> u8 {
164*731bc2b3SLoGin         9
165*731bc2b3SLoGin     }
166*731bc2b3SLoGin 
167*731bc2b3SLoGin     fn as_any_ref(&self) -> &dyn Any {
168*731bc2b3SLoGin         self
169*731bc2b3SLoGin     }
170*731bc2b3SLoGin 
171*731bc2b3SLoGin     fn device(&self) -> Arc<dyn Device> {
172*731bc2b3SLoGin         self.self_ref.upgrade().unwrap()
173*731bc2b3SLoGin     }
174*731bc2b3SLoGin 
175*731bc2b3SLoGin     fn block_size(&self) -> usize {
176*731bc2b3SLoGin         todo!()
177*731bc2b3SLoGin     }
178*731bc2b3SLoGin 
179*731bc2b3SLoGin     fn partitions(&self) -> Vec<Arc<Partition>> {
180*731bc2b3SLoGin         let device = self.self_ref.upgrade().unwrap() as Arc<dyn BlockDevice>;
181*731bc2b3SLoGin         let mbr_table = MbrDiskPartionTable::from_disk(device.clone())
182*731bc2b3SLoGin             .expect("Failed to get MBR partition table");
183*731bc2b3SLoGin         mbr_table.partitions(Arc::downgrade(&device))
184*731bc2b3SLoGin     }
185*731bc2b3SLoGin }
186*731bc2b3SLoGin 
187*731bc2b3SLoGin struct InnerVirtIOBlkDevice {
188*731bc2b3SLoGin     device_inner: VirtIOBlk<HalImpl, VirtIOTransport>,
189*731bc2b3SLoGin     name: Option<String>,
190*731bc2b3SLoGin     virtio_index: Option<VirtIODeviceIndex>,
191*731bc2b3SLoGin     device_common: DeviceCommonData,
192*731bc2b3SLoGin     kobject_common: KObjectCommonData,
193*731bc2b3SLoGin }
194*731bc2b3SLoGin 
195*731bc2b3SLoGin impl Debug for InnerVirtIOBlkDevice {
196*731bc2b3SLoGin     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
197*731bc2b3SLoGin         f.debug_struct("InnerVirtIOBlkDevice").finish()
198*731bc2b3SLoGin     }
199*731bc2b3SLoGin }
200*731bc2b3SLoGin 
201*731bc2b3SLoGin impl VirtIODevice for VirtIOBlkDevice {
202*731bc2b3SLoGin     fn handle_irq(
203*731bc2b3SLoGin         &self,
204*731bc2b3SLoGin         _irq: crate::exception::IrqNumber,
205*731bc2b3SLoGin     ) -> Result<crate::exception::irqdesc::IrqReturn, system_error::SystemError> {
206*731bc2b3SLoGin         todo!("VirtIOBlkDevice::handle_irq")
207*731bc2b3SLoGin     }
208*731bc2b3SLoGin 
209*731bc2b3SLoGin     fn dev_id(&self) -> &Arc<DeviceId> {
210*731bc2b3SLoGin         &self.dev_id
211*731bc2b3SLoGin     }
212*731bc2b3SLoGin 
213*731bc2b3SLoGin     fn set_device_name(&self, name: String) {
214*731bc2b3SLoGin         self.inner().name = Some(name);
215*731bc2b3SLoGin     }
216*731bc2b3SLoGin 
217*731bc2b3SLoGin     fn device_name(&self) -> String {
218*731bc2b3SLoGin         self.inner()
219*731bc2b3SLoGin             .name
220*731bc2b3SLoGin             .clone()
221*731bc2b3SLoGin             .unwrap_or_else(|| VIRTIO_BLK_BASENAME.to_string())
222*731bc2b3SLoGin     }
223*731bc2b3SLoGin 
224*731bc2b3SLoGin     fn set_virtio_device_index(&self, index: VirtIODeviceIndex) {
225*731bc2b3SLoGin         self.inner().virtio_index = Some(index);
226*731bc2b3SLoGin     }
227*731bc2b3SLoGin 
228*731bc2b3SLoGin     fn virtio_device_index(&self) -> Option<VirtIODeviceIndex> {
229*731bc2b3SLoGin         self.inner().virtio_index
230*731bc2b3SLoGin     }
231*731bc2b3SLoGin 
232*731bc2b3SLoGin     fn device_type_id(&self) -> u32 {
233*731bc2b3SLoGin         virtio_drivers::transport::DeviceType::Block as u32
234*731bc2b3SLoGin     }
235*731bc2b3SLoGin 
236*731bc2b3SLoGin     fn vendor(&self) -> u32 {
237*731bc2b3SLoGin         VIRTIO_VENDOR_ID.into()
238*731bc2b3SLoGin     }
239*731bc2b3SLoGin }
240*731bc2b3SLoGin 
241*731bc2b3SLoGin impl Device for VirtIOBlkDevice {
242*731bc2b3SLoGin     fn dev_type(&self) -> DeviceType {
243*731bc2b3SLoGin         DeviceType::Net
244*731bc2b3SLoGin     }
245*731bc2b3SLoGin 
246*731bc2b3SLoGin     fn id_table(&self) -> IdTable {
247*731bc2b3SLoGin         IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None)
248*731bc2b3SLoGin     }
249*731bc2b3SLoGin 
250*731bc2b3SLoGin     fn bus(&self) -> Option<Weak<dyn Bus>> {
251*731bc2b3SLoGin         self.inner().device_common.bus.clone()
252*731bc2b3SLoGin     }
253*731bc2b3SLoGin 
254*731bc2b3SLoGin     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
255*731bc2b3SLoGin         self.inner().device_common.bus = bus;
256*731bc2b3SLoGin     }
257*731bc2b3SLoGin 
258*731bc2b3SLoGin     fn class(&self) -> Option<Arc<dyn Class>> {
259*731bc2b3SLoGin         let mut guard = self.inner();
260*731bc2b3SLoGin         let r = guard.device_common.class.clone()?.upgrade();
261*731bc2b3SLoGin         if r.is_none() {
262*731bc2b3SLoGin             guard.device_common.class = None;
263*731bc2b3SLoGin         }
264*731bc2b3SLoGin 
265*731bc2b3SLoGin         return r;
266*731bc2b3SLoGin     }
267*731bc2b3SLoGin 
268*731bc2b3SLoGin     fn set_class(&self, class: Option<Weak<dyn Class>>) {
269*731bc2b3SLoGin         self.inner().device_common.class = class;
270*731bc2b3SLoGin     }
271*731bc2b3SLoGin 
272*731bc2b3SLoGin     fn driver(&self) -> Option<Arc<dyn Driver>> {
273*731bc2b3SLoGin         let r = self.inner().device_common.driver.clone()?.upgrade();
274*731bc2b3SLoGin         if r.is_none() {
275*731bc2b3SLoGin             self.inner().device_common.driver = None;
276*731bc2b3SLoGin         }
277*731bc2b3SLoGin 
278*731bc2b3SLoGin         return r;
279*731bc2b3SLoGin     }
280*731bc2b3SLoGin 
281*731bc2b3SLoGin     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
282*731bc2b3SLoGin         self.inner().device_common.driver = driver;
283*731bc2b3SLoGin     }
284*731bc2b3SLoGin 
285*731bc2b3SLoGin     fn is_dead(&self) -> bool {
286*731bc2b3SLoGin         false
287*731bc2b3SLoGin     }
288*731bc2b3SLoGin 
289*731bc2b3SLoGin     fn can_match(&self) -> bool {
290*731bc2b3SLoGin         self.inner().device_common.can_match
291*731bc2b3SLoGin     }
292*731bc2b3SLoGin 
293*731bc2b3SLoGin     fn set_can_match(&self, can_match: bool) {
294*731bc2b3SLoGin         self.inner().device_common.can_match = can_match;
295*731bc2b3SLoGin     }
296*731bc2b3SLoGin 
297*731bc2b3SLoGin     fn state_synced(&self) -> bool {
298*731bc2b3SLoGin         true
299*731bc2b3SLoGin     }
300*731bc2b3SLoGin }
301*731bc2b3SLoGin 
302*731bc2b3SLoGin impl KObject for VirtIOBlkDevice {
303*731bc2b3SLoGin     fn as_any_ref(&self) -> &dyn Any {
304*731bc2b3SLoGin         self
305*731bc2b3SLoGin     }
306*731bc2b3SLoGin 
307*731bc2b3SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
308*731bc2b3SLoGin         self.inner().kobject_common.kern_inode = inode;
309*731bc2b3SLoGin     }
310*731bc2b3SLoGin 
311*731bc2b3SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>> {
312*731bc2b3SLoGin         self.inner().kobject_common.kern_inode.clone()
313*731bc2b3SLoGin     }
314*731bc2b3SLoGin 
315*731bc2b3SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>> {
316*731bc2b3SLoGin         self.inner().kobject_common.parent.clone()
317*731bc2b3SLoGin     }
318*731bc2b3SLoGin 
319*731bc2b3SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
320*731bc2b3SLoGin         self.inner().kobject_common.parent = parent;
321*731bc2b3SLoGin     }
322*731bc2b3SLoGin 
323*731bc2b3SLoGin     fn kset(&self) -> Option<Arc<KSet>> {
324*731bc2b3SLoGin         self.inner().kobject_common.kset.clone()
325*731bc2b3SLoGin     }
326*731bc2b3SLoGin 
327*731bc2b3SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>) {
328*731bc2b3SLoGin         self.inner().kobject_common.kset = kset;
329*731bc2b3SLoGin     }
330*731bc2b3SLoGin 
331*731bc2b3SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
332*731bc2b3SLoGin         self.inner().kobject_common.kobj_type
333*731bc2b3SLoGin     }
334*731bc2b3SLoGin 
335*731bc2b3SLoGin     fn name(&self) -> String {
336*731bc2b3SLoGin         self.device_name()
337*731bc2b3SLoGin     }
338*731bc2b3SLoGin 
339*731bc2b3SLoGin     fn set_name(&self, _name: String) {
340*731bc2b3SLoGin         // do nothing
341*731bc2b3SLoGin     }
342*731bc2b3SLoGin 
343*731bc2b3SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
344*731bc2b3SLoGin         self.locked_kobj_state.read()
345*731bc2b3SLoGin     }
346*731bc2b3SLoGin 
347*731bc2b3SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
348*731bc2b3SLoGin         self.locked_kobj_state.write()
349*731bc2b3SLoGin     }
350*731bc2b3SLoGin 
351*731bc2b3SLoGin     fn set_kobj_state(&self, state: KObjectState) {
352*731bc2b3SLoGin         *self.locked_kobj_state.write() = state;
353*731bc2b3SLoGin     }
354*731bc2b3SLoGin 
355*731bc2b3SLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
356*731bc2b3SLoGin         self.inner().kobject_common.kobj_type = ktype;
357*731bc2b3SLoGin     }
358*731bc2b3SLoGin }
359*731bc2b3SLoGin 
360*731bc2b3SLoGin #[unified_init(INITCALL_POSTCORE)]
361*731bc2b3SLoGin fn virtio_blk_driver_init() -> Result<(), SystemError> {
362*731bc2b3SLoGin     let driver = VirtIOBlkDriver::new();
363*731bc2b3SLoGin     virtio_driver_manager()
364*731bc2b3SLoGin         .register(driver.clone() as Arc<dyn VirtIODriver>)
365*731bc2b3SLoGin         .expect("Add virtio net driver failed");
366*731bc2b3SLoGin     unsafe {
367*731bc2b3SLoGin         VIRTIO_BLK_DRIVER = Some(driver);
368*731bc2b3SLoGin     }
369*731bc2b3SLoGin 
370*731bc2b3SLoGin     return Ok(());
371*731bc2b3SLoGin }
372*731bc2b3SLoGin 
373*731bc2b3SLoGin #[derive(Debug)]
374*731bc2b3SLoGin #[cast_to([sync] VirtIODriver)]
375*731bc2b3SLoGin #[cast_to([sync] Driver)]
376*731bc2b3SLoGin struct VirtIOBlkDriver {
377*731bc2b3SLoGin     inner: SpinLock<InnerVirtIOBlkDriver>,
378*731bc2b3SLoGin     kobj_state: LockedKObjectState,
379*731bc2b3SLoGin }
380*731bc2b3SLoGin 
381*731bc2b3SLoGin impl VirtIOBlkDriver {
382*731bc2b3SLoGin     pub fn new() -> Arc<Self> {
383*731bc2b3SLoGin         let inner = InnerVirtIOBlkDriver {
384*731bc2b3SLoGin             driver_common: DriverCommonData::default(),
385*731bc2b3SLoGin             kobj_common: KObjectCommonData::default(),
386*731bc2b3SLoGin         };
387*731bc2b3SLoGin         Arc::new(VirtIOBlkDriver {
388*731bc2b3SLoGin             inner: SpinLock::new(inner),
389*731bc2b3SLoGin             kobj_state: LockedKObjectState::default(),
390*731bc2b3SLoGin         })
391*731bc2b3SLoGin     }
392*731bc2b3SLoGin 
393*731bc2b3SLoGin     fn inner(&self) -> SpinLockGuard<InnerVirtIOBlkDriver> {
394*731bc2b3SLoGin         return self.inner.lock();
395*731bc2b3SLoGin     }
396*731bc2b3SLoGin }
397*731bc2b3SLoGin 
398*731bc2b3SLoGin #[derive(Debug)]
399*731bc2b3SLoGin struct InnerVirtIOBlkDriver {
400*731bc2b3SLoGin     driver_common: DriverCommonData,
401*731bc2b3SLoGin     kobj_common: KObjectCommonData,
402*731bc2b3SLoGin }
403*731bc2b3SLoGin 
404*731bc2b3SLoGin impl VirtIODriver for VirtIOBlkDriver {
405*731bc2b3SLoGin     fn probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError> {
406*731bc2b3SLoGin         let _dev = device
407*731bc2b3SLoGin             .clone()
408*731bc2b3SLoGin             .arc_any()
409*731bc2b3SLoGin             .downcast::<VirtIOBlkDevice>()
410*731bc2b3SLoGin             .map_err(|_| {
411*731bc2b3SLoGin                 kerror!(
412*731bc2b3SLoGin                 "VirtIOBlkDriver::probe() failed: device is not a VirtIO block device. Device: '{:?}'",
413*731bc2b3SLoGin                 device.name()
414*731bc2b3SLoGin             );
415*731bc2b3SLoGin                 SystemError::EINVAL
416*731bc2b3SLoGin             })?;
417*731bc2b3SLoGin 
418*731bc2b3SLoGin         return Ok(());
419*731bc2b3SLoGin     }
420*731bc2b3SLoGin }
421*731bc2b3SLoGin 
422*731bc2b3SLoGin impl Driver for VirtIOBlkDriver {
423*731bc2b3SLoGin     fn id_table(&self) -> Option<IdTable> {
424*731bc2b3SLoGin         Some(IdTable::new(VIRTIO_BLK_BASENAME.to_string(), None))
425*731bc2b3SLoGin     }
426*731bc2b3SLoGin 
427*731bc2b3SLoGin     fn add_device(&self, device: Arc<dyn Device>) {
428*731bc2b3SLoGin         let iface = device
429*731bc2b3SLoGin             .arc_any()
430*731bc2b3SLoGin             .downcast::<VirtIOBlkDevice>()
431*731bc2b3SLoGin             .expect("VirtIOBlkDriver::add_device() failed: device is not a VirtIOBlkDevice");
432*731bc2b3SLoGin 
433*731bc2b3SLoGin         self.inner()
434*731bc2b3SLoGin             .driver_common
435*731bc2b3SLoGin             .devices
436*731bc2b3SLoGin             .push(iface as Arc<dyn Device>);
437*731bc2b3SLoGin     }
438*731bc2b3SLoGin 
439*731bc2b3SLoGin     fn delete_device(&self, device: &Arc<dyn Device>) {
440*731bc2b3SLoGin         let _iface = device
441*731bc2b3SLoGin             .clone()
442*731bc2b3SLoGin             .arc_any()
443*731bc2b3SLoGin             .downcast::<VirtIOBlkDevice>()
444*731bc2b3SLoGin             .expect("VirtIOBlkDriver::delete_device() failed: device is not a VirtIOBlkDevice");
445*731bc2b3SLoGin 
446*731bc2b3SLoGin         let mut guard = self.inner();
447*731bc2b3SLoGin         let index = guard
448*731bc2b3SLoGin             .driver_common
449*731bc2b3SLoGin             .devices
450*731bc2b3SLoGin             .iter()
451*731bc2b3SLoGin             .position(|dev| Arc::ptr_eq(device, dev))
452*731bc2b3SLoGin             .expect("VirtIOBlkDriver::delete_device() failed: device not found");
453*731bc2b3SLoGin 
454*731bc2b3SLoGin         guard.driver_common.devices.remove(index);
455*731bc2b3SLoGin     }
456*731bc2b3SLoGin 
457*731bc2b3SLoGin     fn devices(&self) -> Vec<Arc<dyn Device>> {
458*731bc2b3SLoGin         self.inner().driver_common.devices.clone()
459*731bc2b3SLoGin     }
460*731bc2b3SLoGin 
461*731bc2b3SLoGin     fn bus(&self) -> Option<Weak<dyn Bus>> {
462*731bc2b3SLoGin         Some(Arc::downgrade(&virtio_bus()) as Weak<dyn Bus>)
463*731bc2b3SLoGin     }
464*731bc2b3SLoGin 
465*731bc2b3SLoGin     fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) {
466*731bc2b3SLoGin         // do nothing
467*731bc2b3SLoGin     }
468*731bc2b3SLoGin }
469*731bc2b3SLoGin 
470*731bc2b3SLoGin impl KObject for VirtIOBlkDriver {
471*731bc2b3SLoGin     fn as_any_ref(&self) -> &dyn Any {
472*731bc2b3SLoGin         self
473*731bc2b3SLoGin     }
474*731bc2b3SLoGin 
475*731bc2b3SLoGin     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
476*731bc2b3SLoGin         self.inner().kobj_common.kern_inode = inode;
477*731bc2b3SLoGin     }
478*731bc2b3SLoGin 
479*731bc2b3SLoGin     fn inode(&self) -> Option<Arc<KernFSInode>> {
480*731bc2b3SLoGin         self.inner().kobj_common.kern_inode.clone()
481*731bc2b3SLoGin     }
482*731bc2b3SLoGin 
483*731bc2b3SLoGin     fn parent(&self) -> Option<Weak<dyn KObject>> {
484*731bc2b3SLoGin         self.inner().kobj_common.parent.clone()
485*731bc2b3SLoGin     }
486*731bc2b3SLoGin 
487*731bc2b3SLoGin     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
488*731bc2b3SLoGin         self.inner().kobj_common.parent = parent;
489*731bc2b3SLoGin     }
490*731bc2b3SLoGin 
491*731bc2b3SLoGin     fn kset(&self) -> Option<Arc<KSet>> {
492*731bc2b3SLoGin         self.inner().kobj_common.kset.clone()
493*731bc2b3SLoGin     }
494*731bc2b3SLoGin 
495*731bc2b3SLoGin     fn set_kset(&self, kset: Option<Arc<KSet>>) {
496*731bc2b3SLoGin         self.inner().kobj_common.kset = kset;
497*731bc2b3SLoGin     }
498*731bc2b3SLoGin 
499*731bc2b3SLoGin     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
500*731bc2b3SLoGin         self.inner().kobj_common.kobj_type
501*731bc2b3SLoGin     }
502*731bc2b3SLoGin 
503*731bc2b3SLoGin     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
504*731bc2b3SLoGin         self.inner().kobj_common.kobj_type = ktype;
505*731bc2b3SLoGin     }
506*731bc2b3SLoGin 
507*731bc2b3SLoGin     fn name(&self) -> String {
508*731bc2b3SLoGin         VIRTIO_BLK_BASENAME.to_string()
509*731bc2b3SLoGin     }
510*731bc2b3SLoGin 
511*731bc2b3SLoGin     fn set_name(&self, _name: String) {
512*731bc2b3SLoGin         // do nothing
513*731bc2b3SLoGin     }
514*731bc2b3SLoGin 
515*731bc2b3SLoGin     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
516*731bc2b3SLoGin         self.kobj_state.read()
517*731bc2b3SLoGin     }
518*731bc2b3SLoGin 
519*731bc2b3SLoGin     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
520*731bc2b3SLoGin         self.kobj_state.write()
521*731bc2b3SLoGin     }
522*731bc2b3SLoGin 
523*731bc2b3SLoGin     fn set_kobj_state(&self, state: KObjectState) {
524*731bc2b3SLoGin         *self.kobj_state.write() = state;
525*731bc2b3SLoGin     }
526*731bc2b3SLoGin }
527