Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions axdriver_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ pub trait BaseDriverOps: Send + Sync {

/// The type of the device.
fn device_type(&self) -> DeviceType;

/// The IRQ number of the device, if applicable.
fn irq_num(&self) -> Option<usize> {
None
}
}
14 changes: 12 additions & 2 deletions axdriver_virtio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,22 @@ pub fn probe_pci_device<H: VirtIoHal>(
root: &mut PciRoot,
bdf: DeviceFunction,
dev_info: &DeviceFunctionInfo,
) -> Option<(DeviceType, PciTransport)> {
) -> Option<(DeviceType, PciTransport, usize)> {
use virtio_drivers::transport::pci::virtio_device_type;

#[cfg(target_arch = "x86_64")]
const PCI_IRQ_BASE: usize = 0x20;
#[cfg(target_arch = "riscv64")]
const PCI_IRQ_BASE: usize = 0x20;
#[cfg(target_arch = "loongarch64")]
const PCI_IRQ_BASE: usize = 0x10;
#[cfg(target_arch = "aarch64")]
const PCI_IRQ_BASE: usize = 0x23;

let dev_type = virtio_device_type(dev_info).and_then(as_dev_type)?;
let transport = PciTransport::new::<H>(root, bdf).ok()?;
Some((dev_type, transport))
let irq = PCI_IRQ_BASE + (bdf.device & 3) as usize;
Some((dev_type, transport, irq))
}

const fn as_dev_type(t: VirtIoDevType) -> Option<DeviceType> {
Expand Down
9 changes: 8 additions & 1 deletion axdriver_virtio/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct VirtIoNetDev<H: Hal, T: Transport, const QS: usize> {
free_tx_bufs: Vec<NetBufBox>,
buf_pool: Arc<NetBufPool>,
inner: InnerDev<H, T, QS>,
irq: Option<usize>,
}

unsafe impl<H: Hal, T: Transport, const QS: usize> Send for VirtIoNetDev<H, T, QS> {}
Expand All @@ -25,7 +26,7 @@ unsafe impl<H: Hal, T: Transport, const QS: usize> Sync for VirtIoNetDev<H, T, Q
impl<H: Hal, T: Transport, const QS: usize> VirtIoNetDev<H, T, QS> {
/// Creates a new driver instance and initializes the device, or returns
/// an error if any step fails.
pub fn try_new(transport: T) -> DevResult<Self> {
pub fn try_new(transport: T, irq: Option<usize>) -> DevResult<Self> {
// 0. Create a new driver instance.
const NONE_BUF: Option<NetBufBox> = None;
let inner = InnerDev::new(transport).map_err(as_dev_err)?;
Expand All @@ -40,6 +41,7 @@ impl<H: Hal, T: Transport, const QS: usize> VirtIoNetDev<H, T, QS> {
tx_buffers,
free_tx_bufs,
buf_pool,
irq,
};

// 1. Fill all rx buffers.
Expand Down Expand Up @@ -80,6 +82,10 @@ impl<H: Hal, T: Transport, const QS: usize> BaseDriverOps for VirtIoNetDev<H, T,
fn device_type(&self) -> DeviceType {
DeviceType::Net
}

fn irq_num(&self) -> Option<usize> {
self.irq
}
}

impl<H: Hal, T: Transport, const QS: usize> NetDriverOps for VirtIoNetDev<H, T, QS> {
Expand Down Expand Up @@ -156,6 +162,7 @@ impl<H: Hal, T: Transport, const QS: usize> NetDriverOps for VirtIoNetDev<H, T,
}

fn receive(&mut self) -> DevResult<NetBufPtr> {
self.inner.ack_interrupt();
if let Some(token) = self.inner.poll_receive() {
let mut rx_buf = self.rx_buffers[token as usize]
.take()
Expand Down