Skip to content

Commit

Permalink
refactor(virtio/pci): extract select_queue
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Apr 22, 2024
1 parent efb82df commit cd2dd3a
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,19 @@ pub struct VqCfgHandler<'a> {
}

impl<'a> VqCfgHandler<'a> {
// TODO: Create type for queue selected invariant to get rid of `self.select_queue()` everywhere.
fn select_queue(&mut self) {
let raw = self.raw.as_mut_ptr();
map_field!(raw.queue_select).write(self.vq_index);
}

/// Sets the size of a given virtqueue. In case the provided size exceeds the maximum allowed
/// size, the size is set to this maximum instead. Else size is set to the provided value.
///
/// Returns the set size in form of a `u16`.
pub fn set_vq_size(&mut self, size: u16) -> u16 {
self.select_queue();
let raw = self.raw.as_mut_ptr();
map_field!(raw.queue_select).write(self.vq_index);
let queue_size = map_field!(raw.queue_size);

if queue_size.read() >= size {
Expand All @@ -423,32 +429,32 @@ impl<'a> VqCfgHandler<'a> {
}

pub fn set_ring_addr(&mut self, addr: PhysAddr) {
self.select_queue();
let raw = self.raw.as_mut_ptr();
map_field!(raw.queue_select).write(self.vq_index);
map_field!(raw.queue_desc).write(addr.as_u64());
}

pub fn set_drv_ctrl_addr(&mut self, addr: PhysAddr) {
self.select_queue();
let raw = self.raw.as_mut_ptr();
map_field!(raw.queue_select).write(self.vq_index);
map_field!(raw.queue_driver).write(addr.as_u64());
}

pub fn set_dev_ctrl_addr(&mut self, addr: PhysAddr) {
self.select_queue();
let raw = self.raw.as_mut_ptr();
map_field!(raw.queue_select).write(self.vq_index);
map_field!(raw.queue_device).write(addr.as_u64());
}

pub fn notif_off(&mut self) -> u16 {
self.select_queue();
let raw = self.raw.as_mut_ptr();
map_field!(raw.queue_select).write(self.vq_index);
map_field!(raw.queue_notify_off).read()
}

pub fn enable_queue(&mut self) {
self.select_queue();
let raw = self.raw.as_mut_ptr();
map_field!(raw.queue_select).write(self.vq_index);
map_field!(raw.queue_enable).write(1);
}
}
Expand Down

0 comments on commit cd2dd3a

Please sign in to comment.