Skip to content

Commit

Permalink
refactor(virtio): move notif_data into virtio-spec
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Jun 7, 2024
1 parent 1348e59 commit 03b924a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 95 deletions.
30 changes: 12 additions & 18 deletions src/drivers/virtio/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
#![allow(dead_code)]

use core::mem;
use core::sync::atomic::{fence, Ordering};

use virtio_spec::mmio::{
DeviceRegisterVolatileFieldAccess, DeviceRegisterVolatileWideFieldAccess, DeviceRegisters,
InterruptStatus,
InterruptStatus, NotificationData,
};
use virtio_spec::{le32, DeviceStatus};
use volatile::VolatileRef;
Expand Down Expand Up @@ -301,26 +300,21 @@ impl NotifCtrl {
self.f_notif_data = true;
}

pub fn notify_dev(&self, notif_data: &[u8]) {
fence(Ordering::Acquire);
pub fn notify_dev(&self, vqn: u16, next_off: u16, next_wrap: u8) {
let notification_data = NotificationData::new()
.with_vqn(vqn)
.with_next_off(next_off)
.with_next_wrap(next_wrap);

if self.f_notif_data {
let ptr = self.notif_addr as *mut [u8; 4];

unsafe {
ptr.write_volatile(notif_data[0..4].try_into().unwrap());
}
let notification_data = if self.f_notif_data {
notification_data.into_bits()
} else {
let mut data: [u8; 4] = [0, 0, 0, 0];
data[0..2].copy_from_slice(&notif_data[0..2]);
let ptr = self.notif_addr as *mut [u8; 4];
u32::from(notification_data.vqn()).into()
};

unsafe {
ptr.write_volatile(data[0..4].try_into().unwrap());
}
unsafe {
self.notif_addr.write_volatile(notification_data);
}

fence(Ordering::Release);
}
}

Expand Down
25 changes: 12 additions & 13 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

use alloc::vec::Vec;
use core::ptr::NonNull;
use core::sync::atomic::{fence, Ordering};
use core::{mem, ptr};

use pci_types::capability::PciCapability;
use virtio_spec::pci::{
CommonCfg, CommonCfgVolatileFieldAccess, CommonCfgVolatileWideFieldAccess,
IsrStatus as IsrStatusRaw,
IsrStatus as IsrStatusRaw, NotificationData,
};
use virtio_spec::{le32, DeviceStatus};
use virtio_spec::{le16, le32, DeviceStatus};
use volatile::VolatileRef;

#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))]
Expand Down Expand Up @@ -648,28 +647,28 @@ impl NotifCtrl {
self.f_notif_data = true;
}

pub fn notify_dev(&self, notif_data: &[u8]) {
pub fn notify_dev(&self, vqn: u16, next_off: u16, next_wrap: u8) {
// See Virtio specification v.1.1. - 4.1.5.2
// Depending in the feature negotiation, we write either only the
// virtqueue index or the index and the next position inside the queue.

fence(Ordering::Acquire);
let notification_data = NotificationData::new()
.with_vqn(vqn)
.with_next_off(next_off)
.with_next_wrap(next_wrap);

if self.f_notif_data {
let ptr = self.notif_addr as *mut [u8; 4];

unsafe {
ptr.write_volatile(notif_data[0..4].try_into().unwrap());
self.notif_addr
.write_volatile(notification_data.into_bits());
}
} else {
let ptr = self.notif_addr as *mut [u8; 2];

unsafe {
ptr.write_volatile(notif_data[0..2].try_into().unwrap());
self.notif_addr
.cast::<le16>()
.write_volatile(notification_data.vqn().into());
}
}

fence(Ordering::Release);
}
}

Expand Down
54 changes: 6 additions & 48 deletions src/drivers/virtio/virtqueue/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,22 +971,8 @@ impl Virtq for PackedVq {
}

if self.dev_event.is_notif() | self.dev_event.is_notif_specfic(next_off, next_wrap) {
let index = self.index.0.to_le_bytes();
let mut index = index.iter();
let det_notif_data: u16 = next_off & !(1 << 15);
let flags = (det_notif_data | (u16::from(next_wrap) << 15)).to_le_bytes();
let mut flags = flags.iter();
let mut notif_data: [u8; 4] = [0, 0, 0, 0];

for (i, byte) in notif_data.iter_mut().enumerate() {
if i < 2 {
*byte = *index.next().unwrap();
} else {
*byte = *flags.next().unwrap();
}
}

self.notif_ctrl.notify_dev(&notif_data)
self.notif_ctrl
.notify_dev(self.index.0, next_off, next_wrap);
}
}

Expand All @@ -1013,22 +999,8 @@ impl Virtq for PackedVq {
}

if self.dev_event.is_notif() {
let index = self.index.0.to_le_bytes();
let mut index = index.iter();
let det_notif_data: u16 = next_off & !(1 << 15);
let flags = (det_notif_data | (u16::from(next_wrap) << 15)).to_le_bytes();
let mut flags = flags.iter();
let mut notif_data: [u8; 4] = [0, 0, 0, 0];

for (i, byte) in notif_data.iter_mut().enumerate() {
if i < 2 {
*byte = *index.next().unwrap();
} else {
*byte = *flags.next().unwrap();
}
}

self.notif_ctrl.notify_dev(&notif_data)
self.notif_ctrl
.notify_dev(self.index.0, next_off, next_wrap);
}
}

Expand All @@ -1042,22 +1014,8 @@ impl Virtq for PackedVq {
}

if self.dev_event.is_notif() {
let index = self.index.0.to_le_bytes();
let mut index = index.iter();
let det_notif_data: u16 = next_off & !(1 << 15);
let flags = (det_notif_data | (u16::from(next_wrap) << 15)).to_le_bytes();
let mut flags = flags.iter();
let mut notif_data: [u8; 4] = [0, 0, 0, 0];

for (i, byte) in notif_data.iter_mut().enumerate() {
if i < 2 {
*byte = *index.next().unwrap();
} else {
*byte = *flags.next().unwrap();
}
}

self.notif_ctrl.notify_dev(&notif_data)
self.notif_ctrl
.notify_dev(self.index.0, next_off, next_wrap);
}
}

Expand Down
18 changes: 2 additions & 16 deletions src/drivers/virtio/virtqueue/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,22 +378,8 @@ impl Virtq for SplitVq {
}

if self.ring.borrow().dev_is_notif() {
let index = self.index.0.to_le_bytes();
let mut index = index.iter();
let det_notif_data: u16 = next_off & !(1 << 15);
let flags = (det_notif_data | (u16::from(next_wrap) << 15)).to_le_bytes();
let mut flags = flags.iter();
let mut notif_data: [u8; 4] = [0, 0, 0, 0];

for (i, byte) in notif_data.iter_mut().enumerate() {
if i < 2 {
*byte = *index.next().unwrap();
} else {
*byte = *flags.next().unwrap();
}
}

self.notif_ctrl.notify_dev(&notif_data)
self.notif_ctrl
.notify_dev(self.index.0, next_off, next_wrap);
}
}

Expand Down

0 comments on commit 03b924a

Please sign in to comment.