|
| 1 | +use super::{mmio::MmioTransport, pci::PciTransport, DeviceStatus, DeviceType, Transport}; |
| 2 | +use crate::{PhysAddr, Result}; |
| 3 | +use core::ptr::NonNull; |
| 4 | + |
| 5 | +/// A wrapper for an arbitrary VirtIO transport, either MMIO or PCI. |
| 6 | +#[derive(Debug)] |
| 7 | +pub enum SomeTransport { |
| 8 | + /// An MMIO transport. |
| 9 | + Mmio(MmioTransport), |
| 10 | + /// A PCI transport. |
| 11 | + Pci(PciTransport), |
| 12 | +} |
| 13 | + |
| 14 | +impl From<MmioTransport> for SomeTransport { |
| 15 | + fn from(mmio: MmioTransport) -> Self { |
| 16 | + Self::Mmio(mmio) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl From<PciTransport> for SomeTransport { |
| 21 | + fn from(pci: PciTransport) -> Self { |
| 22 | + Self::Pci(pci) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl Transport for SomeTransport { |
| 27 | + fn device_type(&self) -> DeviceType { |
| 28 | + match self { |
| 29 | + Self::Mmio(mmio) => mmio.device_type(), |
| 30 | + Self::Pci(pci) => pci.device_type(), |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + fn read_device_features(&mut self) -> u64 { |
| 35 | + match self { |
| 36 | + Self::Mmio(mmio) => mmio.read_device_features(), |
| 37 | + Self::Pci(pci) => pci.read_device_features(), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fn write_driver_features(&mut self, driver_features: u64) { |
| 42 | + match self { |
| 43 | + Self::Mmio(mmio) => mmio.write_driver_features(driver_features), |
| 44 | + Self::Pci(pci) => pci.write_driver_features(driver_features), |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + fn max_queue_size(&mut self, queue: u16) -> u32 { |
| 49 | + match self { |
| 50 | + Self::Mmio(mmio) => mmio.max_queue_size(queue), |
| 51 | + Self::Pci(pci) => pci.max_queue_size(queue), |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + fn notify(&mut self, queue: u16) { |
| 56 | + match self { |
| 57 | + Self::Mmio(mmio) => mmio.notify(queue), |
| 58 | + Self::Pci(pci) => pci.notify(queue), |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + fn get_status(&self) -> DeviceStatus { |
| 63 | + match self { |
| 64 | + Self::Mmio(mmio) => mmio.get_status(), |
| 65 | + Self::Pci(pci) => pci.get_status(), |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + fn set_status(&mut self, status: DeviceStatus) { |
| 70 | + match self { |
| 71 | + Self::Mmio(mmio) => mmio.set_status(status), |
| 72 | + Self::Pci(pci) => pci.set_status(status), |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + fn set_guest_page_size(&mut self, guest_page_size: u32) { |
| 77 | + match self { |
| 78 | + Self::Mmio(mmio) => mmio.set_guest_page_size(guest_page_size), |
| 79 | + Self::Pci(pci) => pci.set_guest_page_size(guest_page_size), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn requires_legacy_layout(&self) -> bool { |
| 84 | + match self { |
| 85 | + Self::Mmio(mmio) => mmio.requires_legacy_layout(), |
| 86 | + Self::Pci(pci) => pci.requires_legacy_layout(), |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fn queue_set( |
| 91 | + &mut self, |
| 92 | + queue: u16, |
| 93 | + size: u32, |
| 94 | + descriptors: PhysAddr, |
| 95 | + driver_area: PhysAddr, |
| 96 | + device_area: PhysAddr, |
| 97 | + ) { |
| 98 | + match self { |
| 99 | + Self::Mmio(mmio) => mmio.queue_set(queue, size, descriptors, driver_area, device_area), |
| 100 | + Self::Pci(pci) => pci.queue_set(queue, size, descriptors, driver_area, device_area), |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fn queue_unset(&mut self, queue: u16) { |
| 105 | + match self { |
| 106 | + Self::Mmio(mmio) => mmio.queue_unset(queue), |
| 107 | + Self::Pci(pci) => pci.queue_unset(queue), |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + fn queue_used(&mut self, queue: u16) -> bool { |
| 112 | + match self { |
| 113 | + Self::Mmio(mmio) => mmio.queue_used(queue), |
| 114 | + Self::Pci(pci) => pci.queue_used(queue), |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fn ack_interrupt(&mut self) -> bool { |
| 119 | + match self { |
| 120 | + Self::Mmio(mmio) => mmio.ack_interrupt(), |
| 121 | + Self::Pci(pci) => pci.ack_interrupt(), |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + fn config_space<T: 'static>(&self) -> Result<NonNull<T>> { |
| 126 | + match self { |
| 127 | + Self::Mmio(mmio) => mmio.config_space(), |
| 128 | + Self::Pci(pci) => pci.config_space(), |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments