Skip to content

Commit 34fe584

Browse files
committed
refactor(virtio): make devices generic over transport
Instead of relying on importing one of the PCI and MMIO transport modules that implement identically named structs to select the transport used by VIRTIO device drivers, use generics. This is in preparation for removing the mutual exclusivity of PCI and MMIO-based devices.
1 parent ce6a236 commit 34fe584

22 files changed

Lines changed: 507 additions & 366 deletions

File tree

src/arch/aarch64/kernel/mmio.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::drivers::virtio::transport::mmio as mmio_virtio;
3232
feature = "virtio-net",
3333
feature = "virtio-vsock",
3434
))]
35-
use crate::drivers::virtio::transport::mmio::VirtioDriver;
35+
use crate::drivers::virtio::transport::mmio::{Transport, VirtioDriver};
3636
#[cfg(feature = "virtio-vsock")]
3737
use crate::drivers::vsock::VirtioVsockDriver;
3838
#[cfg(feature = "virtio-net")]
@@ -45,16 +45,16 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
4545
#[allow(clippy::enum_variant_names)]
4646
pub(crate) enum MmioDriver {
4747
#[cfg(feature = "virtio-console")]
48-
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver>),
48+
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver<Transport>>),
4949
#[cfg(feature = "virtio-fs")]
50-
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
50+
VirtioFs(InterruptTicketMutex<VirtioFsDriver<Transport>>),
5151
#[cfg(feature = "virtio-vsock")]
52-
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
52+
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver<Transport>>),
5353
}
5454

5555
impl MmioDriver {
5656
#[cfg(feature = "virtio-console")]
57-
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver>> {
57+
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
5858
#[allow(unreachable_patterns)]
5959
match self {
6060
Self::VirtioConsole(drv) => Some(drv),
@@ -63,7 +63,7 @@ impl MmioDriver {
6363
}
6464

6565
#[cfg(feature = "virtio-fs")]
66-
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver>> {
66+
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver<Transport>>> {
6767
#[allow(unreachable_patterns)]
6868
match self {
6969
Self::VirtioFs(drv) => Some(drv),
@@ -72,7 +72,7 @@ impl MmioDriver {
7272
}
7373

7474
#[cfg(feature = "virtio-vsock")]
75-
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver>> {
75+
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
7676
#[allow(unreachable_patterns)]
7777
match self {
7878
Self::VirtioVsock(drv) => Some(drv),
@@ -81,32 +81,39 @@ impl MmioDriver {
8181
}
8282
}
8383

84-
#[cfg(any(feature = "virtio-console", feature = "virtio-fs", feature = "virtio-vsock"))]
84+
#[cfg(any(
85+
feature = "virtio-console",
86+
feature = "virtio-fs",
87+
feature = "virtio-vsock"
88+
))]
8589
pub(crate) fn register_driver(drv: MmioDriver) {
8690
MMIO_DRIVERS.with(|mmio_drivers| mmio_drivers.unwrap().push(drv));
8791
}
8892

8993
#[cfg(feature = "virtio-net")]
90-
pub(crate) type NetworkDevice = VirtioNetDriver;
94+
pub(crate) type NetworkDevice = VirtioNetDriver<Transport>;
9195

9296
#[cfg(feature = "virtio-console")]
93-
pub(crate) fn get_console_driver() -> Option<&'static InterruptTicketMutex<VirtioConsoleDriver>> {
97+
pub(crate) fn get_console_driver()
98+
-> Option<&'static InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
9499
MMIO_DRIVERS
95100
.get()?
96101
.iter()
97102
.find_map(|drv| drv.get_console_driver())
98103
}
99104

100105
#[cfg(feature = "virtio-fs")]
101-
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptTicketMutex<VirtioFsDriver>> {
106+
pub(crate) fn get_filesystem_driver()
107+
-> Option<&'static InterruptTicketMutex<VirtioFsDriver<Transport>>> {
102108
MMIO_DRIVERS
103109
.get()?
104110
.iter()
105111
.find_map(|drv| drv.get_filesystem_driver())
106112
}
107113

108114
#[cfg(feature = "virtio-vsock")]
109-
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptTicketMutex<VirtioVsockDriver>> {
115+
pub(crate) fn get_vsock_driver()
116+
-> Option<&'static InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
110117
MMIO_DRIVERS
111118
.get()?
112119
.iter()
@@ -209,25 +216,29 @@ pub fn init_drivers() {
209216
} else {
210217
panic!("Invalid interrupt type");
211218
};
212-
gic.set_interrupt_priority(virtio_irqid, Some(cpu_id), 0x00).unwrap();
219+
gic.set_interrupt_priority(virtio_irqid, Some(cpu_id), 0x00)
220+
.unwrap();
213221
if (irqflags & 0xf) == 4 || (irqflags & 0xf) == 8 {
214-
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Level).unwrap();
222+
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Level)
223+
.unwrap();
215224
} else if (irqflags & 0xf) == 2 || (irqflags & 0xf) == 1 {
216-
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Edge).unwrap();
225+
gic.set_trigger(virtio_irqid, Some(cpu_id), Trigger::Edge)
226+
.unwrap();
217227
} else {
218228
panic!("Invalid interrupt level!");
219229
}
220-
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true).unwrap();
230+
gic.enable_interrupt(virtio_irqid, Some(cpu_id), true)
231+
.unwrap();
221232

222233
match drv {
223234
#[cfg(feature = "virtio-console")]
224235
VirtioDriver::Console(drv) => register_driver(MmioDriver::VirtioConsole(
225236
InterruptTicketMutex::new(*drv),
226237
)),
227238
#[cfg(feature = "virtio-fs")]
228-
VirtioDriver::Fs(drv) => register_driver(MmioDriver::VirtioFs(
229-
InterruptTicketMutex::new(*drv),
230-
)),
239+
VirtioDriver::Fs(drv) => {
240+
register_driver(MmioDriver::VirtioFs(InterruptTicketMutex::new(*drv)))
241+
}
231242
#[cfg(feature = "virtio-net")]
232243
VirtioDriver::Net(drv) => *NETWORK_DEVICE.lock() = Some(*drv),
233244
#[cfg(feature = "virtio-vsock")]

src/arch/riscv64/kernel/mmio.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use crate::drivers::fs::VirtioFsDriver;
1515
use crate::drivers::net::gem::GEMDriver;
1616
#[cfg(all(not(feature = "gem-net"), feature = "virtio-net"))]
1717
use crate::drivers::net::virtio::VirtioNetDriver;
18+
#[cfg(feature = "virtio")]
19+
use crate::drivers::virtio::transport::mmio::Transport;
1820
#[cfg(feature = "virtio-vsock")]
1921
use crate::drivers::vsock::VirtioVsockDriver;
2022
use crate::init_cell::InitCell;
@@ -24,16 +26,16 @@ pub(crate) static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::n
2426
#[allow(clippy::enum_variant_names)]
2527
pub(crate) enum MmioDriver {
2628
#[cfg(feature = "virtio-console")]
27-
VirtioConsole(InterruptSpinMutex<VirtioConsoleDriver>),
29+
VirtioConsole(InterruptSpinMutex<VirtioConsoleDriver<Transport>>),
2830
#[cfg(feature = "virtio-fs")]
29-
VirtioFs(InterruptSpinMutex<VirtioFsDriver>),
31+
VirtioFs(InterruptSpinMutex<VirtioFsDriver<Transport>>),
3032
#[cfg(feature = "virtio-vsock")]
31-
VirtioVsock(InterruptSpinMutex<VirtioVsockDriver>),
33+
VirtioVsock(InterruptSpinMutex<VirtioVsockDriver<Transport>>),
3234
}
3335

3436
impl MmioDriver {
3537
#[cfg(feature = "virtio-console")]
36-
fn get_console_driver(&self) -> Option<&InterruptSpinMutex<VirtioConsoleDriver>> {
38+
fn get_console_driver(&self) -> Option<&InterruptSpinMutex<VirtioConsoleDriver<Transport>>> {
3739
#[allow(unreachable_patterns)]
3840
match self {
3941
Self::VirtioConsole(drv) => Some(drv),
@@ -42,7 +44,7 @@ impl MmioDriver {
4244
}
4345

4446
#[cfg(feature = "virtio-fs")]
45-
fn get_filesystem_driver(&self) -> Option<&InterruptSpinMutex<VirtioFsDriver>> {
47+
fn get_filesystem_driver(&self) -> Option<&InterruptSpinMutex<VirtioFsDriver<Transport>>> {
4648
#[allow(unreachable_patterns)]
4749
match self {
4850
Self::VirtioFs(drv) => Some(drv),
@@ -51,7 +53,7 @@ impl MmioDriver {
5153
}
5254

5355
#[cfg(feature = "virtio-vsock")]
54-
fn get_vsock_driver(&self) -> Option<&InterruptSpinMutex<VirtioVsockDriver>> {
56+
fn get_vsock_driver(&self) -> Option<&InterruptSpinMutex<VirtioVsockDriver<Transport>>> {
5557
#[allow(unreachable_patterns)]
5658
match self {
5759
Self::VirtioVsock(drv) => Some(drv),
@@ -73,26 +75,29 @@ pub(crate) fn register_driver(drv: MmioDriver) {
7375
pub(crate) type NetworkDevice = GEMDriver;
7476

7577
#[cfg(all(not(feature = "gem-net"), feature = "virtio-net"))]
76-
pub(crate) type NetworkDevice = VirtioNetDriver;
78+
pub(crate) type NetworkDevice = VirtioNetDriver<Transport>;
7779

7880
#[cfg(feature = "virtio-console")]
79-
pub(crate) fn get_console_driver() -> Option<&'static InterruptSpinMutex<VirtioConsoleDriver>> {
81+
pub(crate) fn get_console_driver()
82+
-> Option<&'static InterruptSpinMutex<VirtioConsoleDriver<Transport>>> {
8083
MMIO_DRIVERS
8184
.get()?
8285
.iter()
8386
.find_map(|drv| drv.get_console_driver())
8487
}
8588

8689
#[cfg(feature = "virtio-fs")]
87-
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptSpinMutex<VirtioFsDriver>> {
90+
pub(crate) fn get_filesystem_driver()
91+
-> Option<&'static InterruptSpinMutex<VirtioFsDriver<Transport>>> {
8892
MMIO_DRIVERS
8993
.get()?
9094
.iter()
9195
.find_map(|drv| drv.get_filesystem_driver())
9296
}
9397

9498
#[cfg(feature = "virtio-vsock")]
95-
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptSpinMutex<VirtioVsockDriver>> {
99+
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptSpinMutex<VirtioVsockDriver<Transport>>>
100+
{
96101
MMIO_DRIVERS
97102
.get()?
98103
.iter()

src/arch/x86_64/kernel/mmio.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::drivers::virtio::transport::mmio as mmio_virtio;
3333
feature = "virtio-net",
3434
feature = "virtio-vsock",
3535
))]
36-
use crate::drivers::virtio::transport::mmio::VirtioDriver;
36+
use crate::drivers::virtio::transport::mmio::{Transport, VirtioDriver};
3737
#[cfg(feature = "virtio-vsock")]
3838
use crate::drivers::vsock::VirtioVsockDriver;
3939
use crate::env;
@@ -49,16 +49,16 @@ static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::new());
4949
#[allow(clippy::enum_variant_names)]
5050
pub(crate) enum MmioDriver {
5151
#[cfg(feature = "virtio-console")]
52-
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver>),
52+
VirtioConsole(InterruptTicketMutex<VirtioConsoleDriver<Transport>>),
5353
#[cfg(feature = "virtio-fs")]
54-
VirtioFs(InterruptTicketMutex<VirtioFsDriver>),
54+
VirtioFs(InterruptTicketMutex<VirtioFsDriver<Transport>>),
5555
#[cfg(feature = "virtio-vsock")]
56-
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver>),
56+
VirtioVsock(InterruptTicketMutex<VirtioVsockDriver<Transport>>),
5757
}
5858

5959
impl MmioDriver {
6060
#[cfg(feature = "virtio-console")]
61-
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver>> {
61+
fn get_console_driver(&self) -> Option<&InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
6262
#[allow(unreachable_patterns)]
6363
match self {
6464
Self::VirtioConsole(drv) => Some(drv),
@@ -67,7 +67,7 @@ impl MmioDriver {
6767
}
6868

6969
#[cfg(feature = "virtio-fs")]
70-
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver>> {
70+
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver<Transport>>> {
7171
#[allow(unreachable_patterns)]
7272
match self {
7373
Self::VirtioFs(drv) => Some(drv),
@@ -76,7 +76,7 @@ impl MmioDriver {
7676
}
7777

7878
#[cfg(feature = "virtio-vsock")]
79-
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver>> {
79+
fn get_vsock_driver(&self) -> Option<&InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
8080
#[allow(unreachable_patterns)]
8181
match self {
8282
Self::VirtioVsock(drv) => Some(drv),
@@ -191,26 +191,29 @@ pub(crate) fn register_driver(drv: MmioDriver) {
191191
}
192192

193193
#[cfg(feature = "virtio-net")]
194-
pub(crate) type NetworkDevice = VirtioNetDriver;
194+
pub(crate) type NetworkDevice = VirtioNetDriver<Transport>;
195195

196196
#[cfg(feature = "virtio-console")]
197-
pub(crate) fn get_console_driver() -> Option<&'static InterruptTicketMutex<VirtioConsoleDriver>> {
197+
pub(crate) fn get_console_driver()
198+
-> Option<&'static InterruptTicketMutex<VirtioConsoleDriver<Transport>>> {
198199
MMIO_DRIVERS
199200
.get()?
200201
.iter()
201202
.find_map(|drv| drv.get_console_driver())
202203
}
203204

204205
#[cfg(feature = "virtio-fs")]
205-
pub(crate) fn get_filesystem_driver() -> Option<&'static InterruptTicketMutex<VirtioFsDriver>> {
206+
pub(crate) fn get_filesystem_driver()
207+
-> Option<&'static InterruptTicketMutex<VirtioFsDriver<Transport>>> {
206208
MMIO_DRIVERS
207209
.get()?
208210
.iter()
209211
.find_map(|drv| drv.get_filesystem_driver())
210212
}
211213

212214
#[cfg(feature = "virtio-vsock")]
213-
pub(crate) fn get_vsock_driver() -> Option<&'static InterruptTicketMutex<VirtioVsockDriver>> {
215+
pub(crate) fn get_vsock_driver()
216+
-> Option<&'static InterruptTicketMutex<VirtioVsockDriver<Transport>>> {
214217
MMIO_DRIVERS
215218
.get()?
216219
.iter()

src/drivers/console/mmio.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use volatile::VolatileRef;
55
use crate::drivers::InterruptLine;
66
use crate::drivers::console::{ConsoleDevCfg, RxQueue, TxQueue, VirtioConsoleDriver};
77
use crate::drivers::virtio::error::VirtioError;
8-
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
8+
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg, Transport};
99

1010
// Backend-dependent interface for Virtio console driver
11-
impl VirtioConsoleDriver {
11+
impl VirtioConsoleDriver<Transport> {
1212
pub fn new(
1313
dev_id: u16,
1414
mut registers: VolatileRef<'static, DeviceRegisters>,
1515
irq: InterruptLine,
16-
) -> Result<VirtioConsoleDriver, VirtioError> {
16+
) -> Result<Self, VirtioError> {
1717
let dev_cfg_raw: &'static Config = unsafe {
1818
&*registers
1919
.borrow_mut()
@@ -32,7 +32,7 @@ impl VirtioConsoleDriver {
3232
let isr_stat = IsrStatus::new(registers.borrow_mut());
3333
let notif_cfg = NotifCfg::new(registers.borrow_mut());
3434

35-
Ok(VirtioConsoleDriver {
35+
Ok(Self {
3636
dev_cfg,
3737
com_cfg: ComCfg::new(registers),
3838
isr_stat,
@@ -52,7 +52,7 @@ impl VirtioConsoleDriver {
5252
dev_id: u16,
5353
registers: VolatileRef<'static, DeviceRegisters>,
5454
irq: InterruptLine,
55-
) -> Result<VirtioConsoleDriver, VirtioError> {
55+
) -> Result<Self, VirtioError> {
5656
let mut drv = VirtioConsoleDriver::new(dev_id, registers, irq)?;
5757
drv.init_dev().map_err(VirtioError::ConsoleDriver)?;
5858
drv.com_cfg.print_information();

0 commit comments

Comments
 (0)