use crate::error::Result;
use std::time::Duration;
mod ioctl_transport;
pub use ioctl_transport::IoctlTransport;
#[cfg(feature = "emu")]
mod ffi_transport;
#[cfg(feature = "emu")]
pub use ffi_transport::FfiTransport;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DramInfo {
pub base: u64,
pub size: u64,
pub dma_max_elem_size: u32,
pub dma_max_elem_count: u16,
pub dma_alignment: u16,
}
impl DramInfo {
pub fn alignment(&self) -> u64 {
(self.dma_alignment as u64).max(1).next_power_of_two()
}
}
pub trait DmaHostBuffer {
fn as_mut_slice(&mut self) -> &mut [u8];
fn as_slice(&self) -> &[u8];
fn virt_addr(&self) -> u64;
fn phys_addr(&self) -> u64;
}
pub struct VecDmaBuffer {
buf: Vec<u8>,
}
impl VecDmaBuffer {
pub fn new(size: usize) -> Self {
VecDmaBuffer {
buf: vec![0u8; size],
}
}
}
impl DmaHostBuffer for VecDmaBuffer {
fn as_mut_slice(&mut self) -> &mut [u8] {
&mut self.buf
}
fn as_slice(&self) -> &[u8] {
&self.buf
}
fn virt_addr(&self) -> u64 {
self.buf.as_ptr() as u64
}
fn phys_addr(&self) -> u64 {
0
}
}
#[derive(Clone, Debug)]
pub struct PoppedResponse {
pub bytes: Vec<u8>,
pub cq_index: u16,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DeviceConfig {
pub shire_mask: u64,
pub cache_line: u32,
}
pub trait Transport {
fn dram_info(&self) -> Result<DramInfo>;
fn device_config(&self) -> Result<DeviceConfig> {
Ok(DeviceConfig {
shire_mask: 0x1,
cache_line: 64,
})
}
fn fw_update(&self, image: &[u8]) -> Result<()>;
fn sq_count(&self) -> Result<u16>;
fn sq_max_msg_size(&self) -> Result<u16>;
fn push_sq(&self, sq_index: u16, cmd: &[u8], flags: u8) -> Result<bool>;
fn pop_cq(&self) -> Result<Option<PoppedResponse>>;
fn extract_trace(&self, trace_type: u8) -> Result<Vec<u8>>;
fn wait_cq(&self, _timeout: Duration) -> Result<bool> {
Ok(true)
}
fn wait_sq(&self, _timeout: Duration) -> Result<bool> {
Ok(true)
}
fn dma_host_buffer(&self, size: usize) -> Result<Box<dyn DmaHostBuffer>> {
Ok(Box::new(VecDmaBuffer::new(size)))
}
}