Skip to content

Commit

Permalink
refactor(fs/virtio): migrate FsDevCfgRaw to virtio-spec
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed Jun 13, 2024
1 parent 10ab630 commit a42446d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 44 deletions.
21 changes: 16 additions & 5 deletions src/drivers/fs/virtio_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::str;

use pci_types::InterruptLine;
use virtio::fs::ConfigVolatileFieldAccess;
use virtio::FeatureBits;
use volatile::access::ReadOnly;
use volatile::VolatileRef;

use crate::config::VIRTIO_MAX_QUEUE_SIZE;
#[cfg(feature = "pci")]
use crate::drivers::fs::virtio_pci::FsDevCfgRaw;
use crate::drivers::virtio::error::VirtioFsError;
#[cfg(not(feature = "pci"))]
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
Expand All @@ -23,7 +25,7 @@ use crate::fs::fuse::{self, FuseInterface};
/// Handling the right access to fields, as some are read-only
/// for the driver.
pub(crate) struct FsDevCfg {
pub raw: &'static FsDevCfgRaw,
pub raw: VolatileRef<'static, virtio::fs::Config, ReadOnly>,
pub dev_id: u16,
pub features: virtio::fs::F,
}
Expand Down Expand Up @@ -112,7 +114,13 @@ impl VirtioFsDriver {
}

// 1 highprio queue, and n normal request queues
let vqnum = self.dev_cfg.raw.get_num_queues() + 1;
let vqnum = self
.dev_cfg
.raw
.as_ptr()
.num_request_queues()
.read()
.to_ne() + 1;
if vqnum == 0 {
error!("0 request queues requested from device. Aborting!");
return Err(VirtioFsError::Unknown);
Expand Down Expand Up @@ -160,7 +168,10 @@ impl FuseInterface for VirtioFsDriver {
}

fn get_mount_point(&self) -> String {
self.dev_cfg.raw.get_tag().to_string()
let tag = self.dev_cfg.raw.as_ptr().tag().read();
let tag = str::from_utf8(&tag).unwrap();
let tag = tag.split('\0').next().unwrap();
tag.to_string()
}
}

Expand Down
44 changes: 5 additions & 39 deletions src/drivers/fs/virtio_pci.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,23 @@
use alloc::vec::Vec;

use volatile::VolatileRef;

use crate::arch::pci::PciConfigRegion;
use crate::drivers::fs::virtio_fs::{FsDevCfg, VirtioFsDriver};
use crate::drivers::pci::PciDevice;
use crate::drivers::virtio::error::{self, VirtioError};
use crate::drivers::virtio::transport::pci;
use crate::drivers::virtio::transport::pci::{PciCap, UniCapsColl};

/// Virtio's network device configuration structure.
/// See specification v1.1. - 5.11.4
///
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub(crate) struct FsDevCfgRaw {
/// Tag is the name associated with this file system.
/// The tag is encoded in UTF-8 and padded with NUL bytes if shorter than the available space.
/// This field is not NUL-terminated if the encoded bytes take up the entire field.
tag: [u8; 36],
/// num_queues is the total number of request virtqueues exposed by the device.
/// Each virtqueue offers identical functionality and there are no ordering guarantees between
/// requests made available on different queues. Use of multiple queues is intended to increase
/// performance.
num_queues: i32,
}

impl FsDevCfgRaw {
pub fn get_tag(&self) -> &str {
let mut i: usize = 0;
let len = loop {
if i >= self.tag.len() {
break self.tag.len();
}
if self.tag[i] == 0 {
break i;
}
i += 1;
};

core::str::from_utf8(&self.tag[..len]).unwrap_or_default()
}

pub fn get_num_queues(&self) -> i32 {
self.num_queues
}
}

impl VirtioFsDriver {
fn map_cfg(cap: &PciCap) -> Option<FsDevCfg> {
let dev_cfg: &'static FsDevCfgRaw = match pci::map_dev_cfg::<FsDevCfgRaw>(cap) {
let dev_cfg = match pci::map_dev_cfg::<virtio::fs::Config>(cap) {
Some(cfg) => cfg,
None => return None,
};

let dev_cfg = VolatileRef::from_ref(dev_cfg);

Some(FsDevCfg {
raw: dev_cfg,
dev_id: cap.dev_id(),
Expand Down

0 comments on commit a42446d

Please sign in to comment.