xref: /DragonOS/kernel/src/driver/virtio/transport.rs (revision 731bc2b32d7b37298883d7a15b6dca659b436ee4)
1e32effb1SLoGin use virtio_drivers::transport::Transport;
2e32effb1SLoGin 
3*731bc2b3SLoGin use super::{transport_mmio::VirtIOMmioTransport, transport_pci::PciTransport};
4e32effb1SLoGin 
5e32effb1SLoGin pub enum VirtIOTransport {
6e32effb1SLoGin     Pci(PciTransport),
7*731bc2b3SLoGin     Mmio(VirtIOMmioTransport),
8e32effb1SLoGin }
9e32effb1SLoGin 
10e32effb1SLoGin impl core::fmt::Debug for VirtIOTransport {
11e32effb1SLoGin     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
12e32effb1SLoGin         match self {
13e32effb1SLoGin             VirtIOTransport::Pci(_) => write!(f, "VirtIOTransport::Pci"),
14*731bc2b3SLoGin             VirtIOTransport::Mmio(_) => write!(f, "VirtIOTransport::Mmio"),
15e32effb1SLoGin         }
16e32effb1SLoGin     }
17e32effb1SLoGin }
18e32effb1SLoGin 
19e32effb1SLoGin impl Transport for VirtIOTransport {
20e32effb1SLoGin     #[inline(always)]
21e32effb1SLoGin     fn finish_init(&mut self) {
22e32effb1SLoGin         match self {
23e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.finish_init(),
24*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.finish_init(),
25e32effb1SLoGin         }
26e32effb1SLoGin     }
27e32effb1SLoGin 
28e32effb1SLoGin     #[inline(always)]
29e32effb1SLoGin     fn device_type(&self) -> virtio_drivers::transport::DeviceType {
30e32effb1SLoGin         match self {
31e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.device_type(),
32*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.device_type(),
33e32effb1SLoGin         }
34e32effb1SLoGin     }
35e32effb1SLoGin 
36e32effb1SLoGin     #[inline(always)]
37e32effb1SLoGin     fn read_device_features(&mut self) -> u64 {
38e32effb1SLoGin         match self {
39e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.read_device_features(),
40*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.read_device_features(),
41e32effb1SLoGin         }
42e32effb1SLoGin     }
43e32effb1SLoGin 
44e32effb1SLoGin     #[inline(always)]
45e32effb1SLoGin     fn write_driver_features(&mut self, driver_features: u64) {
46e32effb1SLoGin         match self {
47e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.write_driver_features(driver_features),
48*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.write_driver_features(driver_features),
49e32effb1SLoGin         }
50e32effb1SLoGin     }
51e32effb1SLoGin 
52e32effb1SLoGin     #[inline(always)]
53e32effb1SLoGin     fn max_queue_size(&mut self, queue: u16) -> u32 {
54e32effb1SLoGin         match self {
55e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.max_queue_size(queue),
56*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.max_queue_size(queue),
57e32effb1SLoGin         }
58e32effb1SLoGin     }
59e32effb1SLoGin 
60e32effb1SLoGin     #[inline(always)]
61e32effb1SLoGin     fn notify(&mut self, queue: u16) {
62e32effb1SLoGin         match self {
63e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.notify(queue),
64*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.notify(queue),
65e32effb1SLoGin         }
66e32effb1SLoGin     }
67e32effb1SLoGin 
68e32effb1SLoGin     #[inline(always)]
69e32effb1SLoGin     fn get_status(&self) -> virtio_drivers::transport::DeviceStatus {
70e32effb1SLoGin         match self {
71e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.get_status(),
72*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.get_status(),
73e32effb1SLoGin         }
74e32effb1SLoGin     }
75e32effb1SLoGin 
76e32effb1SLoGin     #[inline(always)]
77e32effb1SLoGin     fn set_status(&mut self, status: virtio_drivers::transport::DeviceStatus) {
78e32effb1SLoGin         match self {
79e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.set_status(status),
80*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.set_status(status),
81e32effb1SLoGin         }
82e32effb1SLoGin     }
83e32effb1SLoGin 
84e32effb1SLoGin     #[inline(always)]
85e32effb1SLoGin     fn set_guest_page_size(&mut self, guest_page_size: u32) {
86e32effb1SLoGin         match self {
87e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.set_guest_page_size(guest_page_size),
88*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.set_guest_page_size(guest_page_size),
89e32effb1SLoGin         }
90e32effb1SLoGin     }
91e32effb1SLoGin 
92e32effb1SLoGin     #[inline(always)]
93e32effb1SLoGin     fn requires_legacy_layout(&self) -> bool {
94e32effb1SLoGin         match self {
95e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.requires_legacy_layout(),
96*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.requires_legacy_layout(),
97e32effb1SLoGin         }
98e32effb1SLoGin     }
99e32effb1SLoGin 
100e32effb1SLoGin     #[inline(always)]
101e32effb1SLoGin     fn queue_set(
102e32effb1SLoGin         &mut self,
103e32effb1SLoGin         queue: u16,
104e32effb1SLoGin         size: u32,
105e32effb1SLoGin         descriptors: virtio_drivers::PhysAddr,
106e32effb1SLoGin         driver_area: virtio_drivers::PhysAddr,
107e32effb1SLoGin         device_area: virtio_drivers::PhysAddr,
108e32effb1SLoGin     ) {
109e32effb1SLoGin         match self {
110e32effb1SLoGin             VirtIOTransport::Pci(transport) => {
111e32effb1SLoGin                 transport.queue_set(queue, size, descriptors, driver_area, device_area)
112e32effb1SLoGin             }
113*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => {
114*731bc2b3SLoGin                 transport.queue_set(queue, size, descriptors, driver_area, device_area)
115*731bc2b3SLoGin             }
116e32effb1SLoGin         }
117e32effb1SLoGin     }
118e32effb1SLoGin 
119e32effb1SLoGin     #[inline(always)]
120e32effb1SLoGin     fn queue_unset(&mut self, queue: u16) {
121e32effb1SLoGin         match self {
122e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.queue_unset(queue),
123*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.queue_unset(queue),
124e32effb1SLoGin         }
125e32effb1SLoGin     }
126e32effb1SLoGin 
127e32effb1SLoGin     #[inline(always)]
128e32effb1SLoGin     fn queue_used(&mut self, queue: u16) -> bool {
129e32effb1SLoGin         match self {
130e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.queue_used(queue),
131*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.queue_used(queue),
132e32effb1SLoGin         }
133e32effb1SLoGin     }
134e32effb1SLoGin 
135e32effb1SLoGin     #[inline(always)]
136e32effb1SLoGin     fn ack_interrupt(&mut self) -> bool {
137e32effb1SLoGin         match self {
138e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.ack_interrupt(),
139*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.ack_interrupt(),
140e32effb1SLoGin         }
141e32effb1SLoGin     }
142e32effb1SLoGin 
143e32effb1SLoGin     #[inline(always)]
144e32effb1SLoGin     fn config_space<T: 'static>(&self) -> virtio_drivers::Result<core::ptr::NonNull<T>> {
145e32effb1SLoGin         match self {
146e32effb1SLoGin             VirtIOTransport::Pci(transport) => transport.config_space(),
147*731bc2b3SLoGin             VirtIOTransport::Mmio(transport) => transport.config_space(),
148e32effb1SLoGin         }
149e32effb1SLoGin     }
150e32effb1SLoGin }
151