mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-22 08:53:29 +00:00
Use Dma APIs for virtio-block driver
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
d8a841f88a
commit
c3c6b0c19d
@ -222,6 +222,73 @@ impl HasPaddr for DmaStream {
|
||||
}
|
||||
}
|
||||
|
||||
/// A slice of streaming DMA mapping.
|
||||
#[derive(Debug)]
|
||||
pub struct DmaStreamSlice<'a> {
|
||||
stream: &'a DmaStream,
|
||||
offset: usize,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<'a> DmaStreamSlice<'a> {
|
||||
/// Constructs a `DmaStreamSlice` from the `DmaStream`.
|
||||
///
|
||||
/// # Panic
|
||||
///
|
||||
/// If the `offset` is greater than or equal to the length of the stream,
|
||||
/// this method will panic.
|
||||
/// If the `offset + len` is greater than the length of the stream,
|
||||
/// this method will panic.
|
||||
pub fn new(stream: &'a DmaStream, offset: usize, len: usize) -> Self {
|
||||
assert!(offset < stream.nbytes());
|
||||
assert!(offset + len <= stream.nbytes());
|
||||
|
||||
Self {
|
||||
stream,
|
||||
offset,
|
||||
len,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of bytes.
|
||||
pub fn nbytes(&self) -> usize {
|
||||
self.len
|
||||
}
|
||||
|
||||
/// Synchronizes the slice of streaming DMA mapping with the device.
|
||||
pub fn sync(&self) -> Result<(), Error> {
|
||||
self.stream.sync(self.offset..self.offset + self.len)
|
||||
}
|
||||
}
|
||||
|
||||
impl VmIo for DmaStreamSlice<'_> {
|
||||
fn read_bytes(&self, offset: usize, buf: &mut [u8]) -> Result<(), Error> {
|
||||
if buf.len() + offset > self.len {
|
||||
return Err(Error::InvalidArgs);
|
||||
}
|
||||
self.stream.read_bytes(self.offset + offset, buf)
|
||||
}
|
||||
|
||||
fn write_bytes(&self, offset: usize, buf: &[u8]) -> Result<(), Error> {
|
||||
if buf.len() + offset > self.len {
|
||||
return Err(Error::InvalidArgs);
|
||||
}
|
||||
self.stream.write_bytes(self.offset + offset, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDaddr for DmaStreamSlice<'_> {
|
||||
fn daddr(&self) -> Daddr {
|
||||
self.stream.daddr() + self.offset
|
||||
}
|
||||
}
|
||||
|
||||
impl HasPaddr for DmaStreamSlice<'_> {
|
||||
fn paddr(&self) -> Paddr {
|
||||
self.stream.paddr() + self.offset
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(ktest)]
|
||||
mod test {
|
||||
use alloc::vec;
|
||||
|
@ -6,7 +6,7 @@ mod dma_stream;
|
||||
use alloc::collections::BTreeSet;
|
||||
|
||||
pub use dma_coherent::DmaCoherent;
|
||||
pub use dma_stream::{DmaDirection, DmaStream};
|
||||
pub use dma_stream::{DmaDirection, DmaStream, DmaStreamSlice};
|
||||
use spin::Once;
|
||||
|
||||
use super::Paddr;
|
||||
|
@ -25,7 +25,7 @@ use core::ops::Range;
|
||||
use spin::Once;
|
||||
|
||||
pub use self::{
|
||||
dma::{Daddr, DmaCoherent, DmaDirection, DmaStream, HasDaddr},
|
||||
dma::{Daddr, DmaCoherent, DmaDirection, DmaStream, DmaStreamSlice, HasDaddr},
|
||||
frame::{VmFrame, VmFrameVec, VmFrameVecIter, VmReader, VmSegment, VmWriter},
|
||||
io::VmIo,
|
||||
memory_set::{MapArea, MemorySet},
|
||||
|
Reference in New Issue
Block a user