hermit/drivers/virtio/
mod.rs

1//! A module containing virtios core infrastructure for hermit-rs.
2//!
3//! The module contains virtios transport mechanisms, virtqueues and virtio specific errors
4pub mod transport;
5pub mod virtqueue;
6
7pub mod error {
8	use core::fmt;
9
10	#[cfg(feature = "fuse")]
11	pub use crate::drivers::fs::virtio_fs::error::VirtioFsError;
12	#[cfg(all(
13		not(all(target_arch = "x86_64", feature = "rtl8139")),
14		any(feature = "tcp", feature = "udp")
15	))]
16	pub use crate::drivers::net::virtio::error::VirtioNetError;
17	#[cfg(feature = "pci")]
18	use crate::drivers::pci::error::PciError;
19	#[cfg(feature = "vsock")]
20	pub use crate::drivers::vsock::error::VirtioVsockError;
21
22	#[allow(dead_code)]
23	#[derive(Debug)]
24	pub enum VirtioError {
25		#[cfg(feature = "pci")]
26		FromPci(PciError),
27		#[cfg(feature = "pci")]
28		NoComCfg(u16),
29		#[cfg(feature = "pci")]
30		NoIsrCfg(u16),
31		#[cfg(feature = "pci")]
32		NoNotifCfg(u16),
33		DevNotSupported(u16),
34		#[cfg(all(
35			not(all(target_arch = "x86_64", feature = "rtl8139")),
36			any(feature = "tcp", feature = "udp")
37		))]
38		NetDriver(VirtioNetError),
39		#[cfg(feature = "fuse")]
40		FsDriver(VirtioFsError),
41		#[cfg(feature = "vsock")]
42		VsockDriver(VirtioVsockError),
43		#[cfg(not(feature = "pci"))]
44		Unknown,
45	}
46
47	impl fmt::Display for VirtioError {
48		fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49			match self {
50				#[cfg(not(feature = "pci"))]
51				VirtioError::Unknown => write!(f, "Driver failure"),
52				#[cfg(feature = "pci")]
53				VirtioError::FromPci(pci_error) => match pci_error {
54					PciError::General(id) => write!(
55						f,
56						"Driver failed to initialize device with id: {id:#x}. Due to unknown reasosn!"
57					),
58					PciError::NoBar(id) => write!(
59						f,
60						"Driver failed to initialize device with id: {id:#x}. Reason: No BAR's found."
61					),
62					PciError::NoCapPtr(id) => write!(
63						f,
64						"Driver failed to initialize device with id: {id:#x}. Reason: No Capabilities pointer found."
65					),
66					PciError::NoVirtioCaps(id) => write!(
67						f,
68						"Driver failed to initialize device with id: {id:#x}. Reason: No Virtio capabilities were found."
69					),
70				},
71				#[cfg(feature = "pci")]
72				VirtioError::NoComCfg(id) => write!(
73					f,
74					"Virtio driver failed, for device {id:x}, due to a missing or malformed common config!"
75				),
76				#[cfg(feature = "pci")]
77				VirtioError::NoIsrCfg(id) => write!(
78					f,
79					"Virtio driver failed, for device {id:x}, due to a missing or malformed ISR status config!"
80				),
81				#[cfg(feature = "pci")]
82				VirtioError::NoNotifCfg(id) => write!(
83					f,
84					"Virtio driver failed, for device {id:x}, due to a missing or malformed notification config!"
85				),
86				VirtioError::DevNotSupported(id) => {
87					write!(f, "Device with id {id:#x} not supported.")
88				}
89				#[cfg(all(
90					not(all(target_arch = "x86_64", feature = "rtl8139")),
91					any(feature = "tcp", feature = "udp")
92				))]
93				VirtioError::NetDriver(net_error) => match net_error {
94					#[cfg(feature = "pci")]
95					VirtioNetError::NoDevCfg(id) => write!(
96						f,
97						"Virtio network driver failed, for device {id:x}, due to a missing or malformed device config!"
98					),
99					VirtioNetError::FailFeatureNeg(id) => write!(
100						f,
101						"Virtio network driver failed, for device {id:x}, device did not acknowledge negotiated feature set!"
102					),
103					VirtioNetError::FeatureRequirementsNotMet(features) => write!(
104						f,
105						"Virtio network driver tried to set feature bit without setting dependency feature. Feat set: {features:?}"
106					),
107					VirtioNetError::IncompatibleFeatureSets(driver_features, device_features) => {
108						write!(
109							f,
110							"Feature set: {driver_features:?} , is incompatible with the device features: {device_features:?}"
111						)
112					}
113				},
114				#[cfg(feature = "fuse")]
115				VirtioError::FsDriver(fs_error) => match fs_error {
116					#[cfg(feature = "pci")]
117					VirtioFsError::NoDevCfg(id) => write!(
118						f,
119						"Virtio filesystem driver failed, for device {id:x}, due to a missing or malformed device config!"
120					),
121					VirtioFsError::FailFeatureNeg(id) => write!(
122						f,
123						"Virtio filesystem driver failed, for device {id:x}, device did not acknowledge negotiated feature set!"
124					),
125					VirtioFsError::FeatureRequirementsNotMet(features) => write!(
126						f,
127						"Virtio filesystem driver tried to set feature bit without setting dependency feature. Feat set: {features:?}"
128					),
129					VirtioFsError::IncompatibleFeatureSets(driver_features, device_features) => {
130						write!(
131							f,
132							"Feature set: {driver_features:?} , is incompatible with the device features: {device_features:?}",
133						)
134					}
135					VirtioFsError::Unknown => write!(
136						f,
137						"Virtio filesystem failed, driver failed due unknown reason!"
138					),
139				},
140				#[cfg(feature = "vsock")]
141				VirtioError::VsockDriver(vsock_error) => match vsock_error {
142					#[cfg(feature = "pci")]
143					VirtioVsockError::NoDevCfg(id) => write!(
144						f,
145						"Virtio socket device driver failed, for device {id:x}, due to a missing or malformed device config!"
146					),
147					#[cfg(feature = "pci")]
148					VirtioVsockError::NoComCfg(id) => write!(
149						f,
150						"Virtio socket device driver failed, for device {id:x}, due to a missing or malformed common config!"
151					),
152					#[cfg(feature = "pci")]
153					VirtioVsockError::NoIsrCfg(id) => write!(
154						f,
155						"Virtio socket device driver failed, for device {id:x}, due to a missing or malformed ISR status config!"
156					),
157					#[cfg(feature = "pci")]
158					VirtioVsockError::NoNotifCfg(id) => write!(
159						f,
160						"Virtio socket device driver failed, for device {id:x}, due to a missing or malformed notification config!"
161					),
162					VirtioVsockError::FailFeatureNeg(id) => write!(
163						f,
164						"Virtio socket device driver failed, for device {id:x}, device did not acknowledge negotiated feature set!"
165					),
166					VirtioVsockError::FeatureRequirementsNotMet(features) => write!(
167						f,
168						"Virtio socket driver tried to set feature bit without setting dependency feature. Feat set: {features:?}"
169					),
170					VirtioVsockError::IncompatibleFeatureSets(driver_features, device_features) => {
171						write!(
172							f,
173							"Feature set: {driver_features:?} , is incompatible with the device features: {device_features:?}"
174						)
175					}
176				},
177			}
178		}
179	}
180}