xref: /DragonOS/kernel/src/driver/virtio/transport.rs (revision 0102d69fdd231e472d7bb3d609a41ae56a3799ee)
1e32effb1SLoGin use virtio_drivers::transport::Transport;
2e32effb1SLoGin 
3*0102d69fSLoGin use crate::exception::HardwareIrqNumber;
4*0102d69fSLoGin 
5731bc2b3SLoGin use super::{transport_mmio::VirtIOMmioTransport, transport_pci::PciTransport};
6e32effb1SLoGin 
7e32effb1SLoGin pub enum VirtIOTransport {
8e32effb1SLoGin     Pci(PciTransport),
9731bc2b3SLoGin     Mmio(VirtIOMmioTransport),
10e32effb1SLoGin }
11e32effb1SLoGin 
12*0102d69fSLoGin impl VirtIOTransport {
13*0102d69fSLoGin     pub fn irq(&self) -> Option<HardwareIrqNumber> {
14*0102d69fSLoGin         match self {
15*0102d69fSLoGin             VirtIOTransport::Mmio(transport) => Some(transport.irq()),
16*0102d69fSLoGin             _ => None,
17*0102d69fSLoGin         }
18*0102d69fSLoGin     }
19*0102d69fSLoGin }
20*0102d69fSLoGin 
21e32effb1SLoGin impl core::fmt::Debug for VirtIOTransport {
22e32effb1SLoGin     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23e32effb1SLoGin         match self {
24e32effb1SLoGin             VirtIOTransport::Pci(_) => write!(f, "VirtIOTransport::Pci"),
25731bc2b3SLoGin             VirtIOTransport::Mmio(_) => write!(f, "VirtIOTransport::Mmio"),
26e32effb1SLoGin         }
27e32effb1SLoGin     }
28e32effb1SLoGin }
29e32effb1SLoGin 
30e32effb1SLoGin impl Transport for VirtIOTransport {
31e32effb1SLoGin     #[inline(always)]
32e32effb1SLoGin     fn finish_init(&mut self) {
33e32effb1SLoGin         match self {
34e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.finish_init(),
35731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.finish_init(),
36e32effb1SLoGin         }
37e32effb1SLoGin     }
38e32effb1SLoGin 
39e32effb1SLoGin     #[inline(always)]
40e32effb1SLoGin     fn device_type(&self) -> virtio_drivers::transport::DeviceType {
41e32effb1SLoGin         match self {
42e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.device_type(),
43731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.device_type(),
44e32effb1SLoGin         }
45e32effb1SLoGin     }
46e32effb1SLoGin 
47e32effb1SLoGin     #[inline(always)]
48e32effb1SLoGin     fn read_device_features(&mut self) -> u64 {
49e32effb1SLoGin         match self {
50e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.read_device_features(),
51731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.read_device_features(),
52e32effb1SLoGin         }
53e32effb1SLoGin     }
54e32effb1SLoGin 
55e32effb1SLoGin     #[inline(always)]
56e32effb1SLoGin     fn write_driver_features(&mut self, driver_features: u64) {
57e32effb1SLoGin         match self {
58e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.write_driver_features(driver_features),
59731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.write_driver_features(driver_features),
60e32effb1SLoGin         }
61e32effb1SLoGin     }
62e32effb1SLoGin 
63e32effb1SLoGin     #[inline(always)]
64e32effb1SLoGin     fn max_queue_size(&mut self, queue: u16) -> u32 {
65e32effb1SLoGin         match self {
66e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.max_queue_size(queue),
67731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.max_queue_size(queue),
68e32effb1SLoGin         }
69e32effb1SLoGin     }
70e32effb1SLoGin 
71e32effb1SLoGin     #[inline(always)]
72e32effb1SLoGin     fn notify(&mut self, queue: u16) {
73e32effb1SLoGin         match self {
74e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.notify(queue),
75731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.notify(queue),
76e32effb1SLoGin         }
77e32effb1SLoGin     }
78e32effb1SLoGin 
79e32effb1SLoGin     #[inline(always)]
80e32effb1SLoGin     fn get_status(&self) -> virtio_drivers::transport::DeviceStatus {
81e32effb1SLoGin         match self {
82e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.get_status(),
83731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.get_status(),
84e32effb1SLoGin         }
85e32effb1SLoGin     }
86e32effb1SLoGin 
87e32effb1SLoGin     #[inline(always)]
88e32effb1SLoGin     fn set_status(&mut self, status: virtio_drivers::transport::DeviceStatus) {
89e32effb1SLoGin         match self {
90e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.set_status(status),
91731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.set_status(status),
92e32effb1SLoGin         }
93e32effb1SLoGin     }
94e32effb1SLoGin 
95e32effb1SLoGin     #[inline(always)]
96e32effb1SLoGin     fn set_guest_page_size(&mut self, guest_page_size: u32) {
97e32effb1SLoGin         match self {
98e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.set_guest_page_size(guest_page_size),
99731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.set_guest_page_size(guest_page_size),
100e32effb1SLoGin         }
101e32effb1SLoGin     }
102e32effb1SLoGin 
103e32effb1SLoGin     #[inline(always)]
104e32effb1SLoGin     fn requires_legacy_layout(&self) -> bool {
105e32effb1SLoGin         match self {
106e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.requires_legacy_layout(),
107731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.requires_legacy_layout(),
108e32effb1SLoGin         }
109e32effb1SLoGin     }
110e32effb1SLoGin 
111e32effb1SLoGin     #[inline(always)]
112e32effb1SLoGin     fn queue_set(
113e32effb1SLoGin         &mut self,
114e32effb1SLoGin         queue: u16,
115e32effb1SLoGin         size: u32,
116e32effb1SLoGin         descriptors: virtio_drivers::PhysAddr,
117e32effb1SLoGin         driver_area: virtio_drivers::PhysAddr,
118e32effb1SLoGin         device_area: virtio_drivers::PhysAddr,
119e32effb1SLoGin     ) {
120e32effb1SLoGin         match self {
121e32effb1SLoGin             VirtIOTransport::Pci(transport) => {
122e32effb1SLoGin                 transport.queue_set(queue, size, descriptors, driver_area, device_area)
123e32effb1SLoGin             }
124731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => {
125731bc2b3SLoGin                 transport.queue_set(queue, size, descriptors, driver_area, device_area)
126731bc2b3SLoGin             }
127e32effb1SLoGin         }
128e32effb1SLoGin     }
129e32effb1SLoGin 
130e32effb1SLoGin     #[inline(always)]
131e32effb1SLoGin     fn queue_unset(&mut self, queue: u16) {
132e32effb1SLoGin         match self {
133e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.queue_unset(queue),
134731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.queue_unset(queue),
135e32effb1SLoGin         }
136e32effb1SLoGin     }
137e32effb1SLoGin 
138e32effb1SLoGin     #[inline(always)]
139e32effb1SLoGin     fn queue_used(&mut self, queue: u16) -> bool {
140e32effb1SLoGin         match self {
141e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.queue_used(queue),
142731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.queue_used(queue),
143e32effb1SLoGin         }
144e32effb1SLoGin     }
145e32effb1SLoGin 
146e32effb1SLoGin     #[inline(always)]
147e32effb1SLoGin     fn ack_interrupt(&mut self) -> bool {
148e32effb1SLoGin         match self {
149e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.ack_interrupt(),
150731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.ack_interrupt(),
151e32effb1SLoGin         }
152e32effb1SLoGin     }
153e32effb1SLoGin 
154e32effb1SLoGin     #[inline(always)]
155e32effb1SLoGin     fn config_space<T: 'static>(&self) -> virtio_drivers::Result<core::ptr::NonNull<T>> {
156e32effb1SLoGin         match self {
157e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.config_space(),
158731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.config_space(),
159e32effb1SLoGin         }
160e32effb1SLoGin     }
161e32effb1SLoGin }
162