|
| 1 | +#![no_std] |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + device::{Geometry, VirtioBlkDevice}, |
| 5 | + queue::RequestQueue, |
| 6 | +}; |
| 7 | +use core::sync::atomic::{AtomicUsize, Ordering}; |
| 8 | +use virtio::VirtioDevice; |
| 9 | +use zinnia::{ |
| 10 | + alloc::{boxed::Box, format, sync::Arc}, |
| 11 | + device::pci::{DeviceView, Driver, PciVariant}, |
| 12 | + error, |
| 13 | + irq::{IrqHandler, Status}, |
| 14 | + log, |
| 15 | + posix::errno::{EResult, Errno}, |
| 16 | +}; |
| 17 | + |
| 18 | +mod device; |
| 19 | +mod error; |
| 20 | +mod queue; |
| 21 | +mod spec; |
| 22 | + |
| 23 | +const MSIX_VECTOR: u16 = 0; |
| 24 | +const MIN_QUEUE_SIZE: u16 = 4; |
| 25 | + |
| 26 | +static BLK_COUNTER: AtomicUsize = AtomicUsize::new(0); |
| 27 | + |
| 28 | +struct VirtioBlkIrqHandler { |
| 29 | + queue: Arc<RequestQueue>, |
| 30 | +} |
| 31 | + |
| 32 | +impl IrqHandler for VirtioBlkIrqHandler { |
| 33 | + fn raise(&mut self) -> Status { |
| 34 | + self.queue.drain(); |
| 35 | + Status::Handled |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +fn enable_bus_mastering(view: &mut DeviceView<'static>) { |
| 40 | + let command = view.access().read32(view.address(), 0x04) as u16; |
| 41 | + let updated = command | (1 << 1) | (1 << 2); |
| 42 | + view.access().write32(view.address(), 0x04, updated as u32); |
| 43 | +} |
| 44 | + |
| 45 | +fn bind_interrupts(virtio: &mut VirtioDevice) -> EResult<bool> { |
| 46 | + if virtio.set_queue_msix_vector(spec::REQUEST_QUEUE, MSIX_VECTOR)? != MSIX_VECTOR { |
| 47 | + return Ok(false); |
| 48 | + } |
| 49 | + virtio.set_config_msix_vector(0xFFFF)?; |
| 50 | + Ok(true) |
| 51 | +} |
| 52 | + |
| 53 | +fn probe(_: &PciVariant, mut view: DeviceView<'static>) -> EResult<()> { |
| 54 | + log!("Probing VirtIO block device on {}", view.address()); |
| 55 | + |
| 56 | + enable_bus_mastering(&mut view); |
| 57 | + let irq_line = view.setup_irq().ok(); |
| 58 | + |
| 59 | + let mut virtio = VirtioDevice::new_pci(view)?; |
| 60 | + |
| 61 | + let device_lo = virtio.get_device_features(0)?; |
| 62 | + let device_hi = virtio.get_device_features(1)?; |
| 63 | + if device_hi & spec::VIRTIO_F_VERSION_1_LO == 0 { |
| 64 | + error!("VirtIO block device does not implement the 1.0 interface"); |
| 65 | + return Err(Errno::ENOTSUP); |
| 66 | + } |
| 67 | + |
| 68 | + let driver_lo = device_lo & spec::SUPPORTED_FEATURES; |
| 69 | + virtio.set_driver_features(0, driver_lo)?; |
| 70 | + virtio.set_driver_features(1, spec::VIRTIO_F_VERSION_1_LO)?; |
| 71 | + virtio.finalize_features()?; |
| 72 | + log!("Negotiated features: {driver_lo:#010x}"); |
| 73 | + |
| 74 | + if virtio.num_queues()? == 0 { |
| 75 | + error!("VirtIO block device exposes no request queue"); |
| 76 | + return Err(Errno::ENODEV); |
| 77 | + } |
| 78 | + |
| 79 | + let queue = virtio.setup_queue(spec::REQUEST_QUEUE)?; |
| 80 | + let queue_size = queue.queue_size(); |
| 81 | + if queue_size < MIN_QUEUE_SIZE { |
| 82 | + error!("Request queue is too small ({queue_size} descriptors)"); |
| 83 | + return Err(Errno::ENODEV); |
| 84 | + } |
| 85 | + |
| 86 | + let geometry = Geometry::read(&virtio, driver_lo, queue_size)?; |
| 87 | + |
| 88 | + let irq_line = match irq_line { |
| 89 | + Some(line) => bind_interrupts(&mut virtio)?.then_some(line), |
| 90 | + None => None, |
| 91 | + }; |
| 92 | + if irq_line.is_none() { |
| 93 | + log!("Falling back to polled completions"); |
| 94 | + } |
| 95 | + |
| 96 | + virtio.set_driver_ok()?; |
| 97 | + |
| 98 | + let requests = Arc::new(RequestQueue::new(virtio, queue, irq_line.is_none())?); |
| 99 | + |
| 100 | + if let Some(line) = irq_line.as_ref() { |
| 101 | + line.attach(Box::new(VirtioBlkIrqHandler { |
| 102 | + queue: requests.clone(), |
| 103 | + })); |
| 104 | + line.unmask(); |
| 105 | + } |
| 106 | + |
| 107 | + let index = BLK_COUNTER.fetch_add(1, Ordering::SeqCst); |
| 108 | + let blk = Arc::new(VirtioBlkDevice::new(requests, geometry, index as u32)); |
| 109 | + |
| 110 | + zinnia::device::block::register_block_device(&format!("virtblk{index}"), blk) |
| 111 | +} |
| 112 | + |
| 113 | +const BASE_VARIANT: PciVariant = PciVariant::new().vendor(0x1AF4); |
| 114 | + |
| 115 | +static DRIVER: Driver = Driver { |
| 116 | + name: "virtio_blk", |
| 117 | + probe, |
| 118 | + variants: &[BASE_VARIANT.device(0x1001), BASE_VARIANT.device(0x1042)], |
| 119 | +}; |
| 120 | + |
| 121 | +zinnia::module!("VirtIO block driver", "Marvin Friedrich", main); |
| 122 | + |
| 123 | +pub fn main(_cmdline: &str) { |
| 124 | + match DRIVER.register() { |
| 125 | + Ok(_) => (), |
| 126 | + Err(e) => error!("Unable to load VirtIO block driver: {:?}", e), |
| 127 | + } |
| 128 | +} |
0 commit comments