Skip to content

Commit efc56b2

Browse files
committed
Add SomeTransport, an enum wrapping either an MMIO or PCI transport.
1 parent 845a75f commit efc56b2

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

src/transport/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
pub mod fake;
55
pub mod mmio;
66
pub mod pci;
7+
mod some;
78

89
use crate::{PhysAddr, Result, PAGE_SIZE};
910
use bitflags::{bitflags, Flags};
1011
use core::{fmt::Debug, ops::BitAnd};
1112
use log::debug;
13+
pub use some::SomeTransport;
1214
use zerocopy::{FromBytes, IntoBytes};
1315

1416
/// A VirtIO transport layer.

src/transport/some.rs

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

0 commit comments

Comments
 (0)