2019-06-21 07:19:04 +08:00
|
|
|
use core::ops::Deref;
|
2019-12-18 06:35:58 +08:00
|
|
|
use libregister::*;
|
2020-03-29 06:30:39 +08:00
|
|
|
use super::Buffer;
|
2019-06-09 07:02:10 +08:00
|
|
|
|
2019-06-22 07:20:18 +08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
HrespNotOk,
|
|
|
|
RxOverrun,
|
|
|
|
BufferNotAvail,
|
|
|
|
Truncated,
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:02:10 +08:00
|
|
|
/// Descriptor entry
|
2019-09-29 07:39:12 +08:00
|
|
|
#[repr(C, align(0x08))]
|
2019-06-21 06:58:18 +08:00
|
|
|
pub struct DescEntry {
|
2019-06-09 07:02:10 +08:00
|
|
|
word0: DescWord0,
|
|
|
|
word1: DescWord1,
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:15:13 +08:00
|
|
|
impl DescEntry {
|
|
|
|
pub fn zeroed() -> Self {
|
|
|
|
DescEntry {
|
|
|
|
word0: DescWord0 { inner: VolatileCell::new(0) },
|
|
|
|
word1: DescWord1 { inner: VolatileCell::new(0) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
register!(desc_word0, DescWord0, VolatileCell, u32);
|
2019-08-11 06:55:27 +08:00
|
|
|
register_bit!(desc_word0,
|
|
|
|
/// true if owned by software, false if owned by hardware
|
|
|
|
used, 0);
|
|
|
|
register_bit!(desc_word0,
|
|
|
|
/// mark last desc in list
|
|
|
|
wrap, 1);
|
2019-06-09 07:02:10 +08:00
|
|
|
register_bits!(desc_word0, address, u32, 2, 31);
|
|
|
|
|
2019-10-31 10:15:13 +08:00
|
|
|
register!(desc_word1, DescWord1, VolatileCell, u32);
|
2019-06-09 07:02:10 +08:00
|
|
|
register_bits!(desc_word1, frame_length_lsbs, u16, 0, 12);
|
|
|
|
register_bit!(desc_word1, bad_fcs, 13);
|
|
|
|
register_bit!(desc_word1, start_of_frame, 14);
|
|
|
|
register_bit!(desc_word1, end_of_frame, 15);
|
|
|
|
register_bit!(desc_word1, cfi, 16);
|
|
|
|
register_bits!(desc_word1, vlan_priority, u8, 17, 19);
|
|
|
|
register_bit!(desc_word1, priority_tag, 20);
|
|
|
|
register_bit!(desc_word1, vlan_tag, 21);
|
|
|
|
register_bits!(desc_word1, bits_22_23, u8, 22, 23);
|
|
|
|
register_bit!(desc_word1, bit_24, 24);
|
|
|
|
register_bits!(desc_word1, spec_addr_which, u8, 25, 26);
|
|
|
|
register_bit!(desc_word1, spec_addr_match, 27);
|
|
|
|
register_bit!(desc_word1, uni_hash_match, 29);
|
|
|
|
register_bit!(desc_word1, multi_hash_match, 30);
|
|
|
|
register_bit!(desc_word1, global_broadcast, 31);
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct DescList<'a> {
|
2019-06-21 06:58:18 +08:00
|
|
|
list: &'a mut [DescEntry],
|
2020-03-29 06:30:39 +08:00
|
|
|
buffers: &'a mut [Buffer],
|
2019-06-10 08:44:29 +08:00
|
|
|
next: usize,
|
2019-06-09 07:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DescList<'a> {
|
2020-03-29 06:30:39 +08:00
|
|
|
pub fn new(list: &'a mut [DescEntry], buffers: &'a mut [Buffer]) -> Self {
|
2019-06-21 06:58:18 +08:00
|
|
|
let last = list.len().min(buffers.len()) - 1;
|
|
|
|
for (i, (entry, buffer)) in list.iter_mut().zip(buffers.iter_mut()).enumerate() {
|
|
|
|
let is_last = i == last;
|
2020-03-29 06:30:39 +08:00
|
|
|
let buffer_addr = &mut buffer.0[0] as *mut _ as u32;
|
2019-06-09 08:23:37 +08:00
|
|
|
assert!(buffer_addr & 0b11 == 0);
|
2019-06-21 06:58:18 +08:00
|
|
|
entry.word0.write(
|
2019-06-09 07:02:10 +08:00
|
|
|
DescWord0::zeroed()
|
|
|
|
.used(false)
|
2019-06-21 06:58:18 +08:00
|
|
|
.wrap(is_last)
|
2019-06-09 07:02:10 +08:00
|
|
|
.address(buffer_addr >> 2)
|
|
|
|
);
|
2019-06-21 06:58:18 +08:00
|
|
|
entry.word1.write(
|
2019-06-09 07:02:10 +08:00
|
|
|
DescWord1::zeroed()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-10 08:44:29 +08:00
|
|
|
DescList {
|
2019-10-18 06:03:17 +08:00
|
|
|
// Shorten the list of descriptors to the required number.
|
|
|
|
list: &mut list[0..=last],
|
2019-06-21 06:58:18 +08:00
|
|
|
buffers,
|
2019-06-10 08:44:29 +08:00
|
|
|
next: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 06:58:18 +08:00
|
|
|
pub fn list_addr(&self) -> u32 {
|
|
|
|
&self.list[0] as *const _ as u32
|
|
|
|
}
|
|
|
|
|
2019-06-22 07:20:18 +08:00
|
|
|
pub fn recv_next<'s: 'p, 'p>(&'s mut self) -> Result<Option<PktRef<'p>>, Error> {
|
2019-06-21 07:19:04 +08:00
|
|
|
let list_len = self.list.len();
|
|
|
|
let entry = &mut self.list[self.next];
|
|
|
|
if entry.word0.read().used() {
|
2019-06-22 07:20:18 +08:00
|
|
|
let word1 = entry.word1.read();
|
|
|
|
let len = word1.frame_length_lsbs().into();
|
2019-10-18 06:03:17 +08:00
|
|
|
let buffer = &mut self.buffers[self.next][0..len];
|
2019-06-21 07:19:04 +08:00
|
|
|
|
2019-06-10 08:44:29 +08:00
|
|
|
self.next += 1;
|
2019-06-21 07:19:04 +08:00
|
|
|
if self.next >= list_len {
|
2019-06-10 08:44:29 +08:00
|
|
|
self.next = 0;
|
|
|
|
}
|
2019-06-21 07:19:04 +08:00
|
|
|
|
2019-06-22 07:20:18 +08:00
|
|
|
let pkt = PktRef { entry, buffer };
|
|
|
|
if word1.start_of_frame() && word1.end_of_frame() {
|
|
|
|
Ok(Some(pkt))
|
|
|
|
} else {
|
|
|
|
Err(Error::Truncated)
|
|
|
|
}
|
2019-06-10 08:44:29 +08:00
|
|
|
} else {
|
2019-06-22 07:20:18 +08:00
|
|
|
Ok(None)
|
2019-06-10 08:44:29 +08:00
|
|
|
}
|
2019-06-09 07:02:10 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-21 07:19:04 +08:00
|
|
|
|
|
|
|
/// Releases a buffer back to the HW upon Drop
|
|
|
|
pub struct PktRef<'a> {
|
|
|
|
entry: &'a mut DescEntry,
|
2019-10-18 06:03:17 +08:00
|
|
|
buffer: &'a mut [u8],
|
2019-06-21 07:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for PktRef<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.entry.word0.modify(|_, w| w.used(false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Deref for PktRef<'a> {
|
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
self.buffer
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 05:29:16 +08:00
|
|
|
|
|
|
|
impl<'a> smoltcp::phy::RxToken for PktRef<'a> {
|
2019-09-29 09:01:24 +08:00
|
|
|
fn consume<R, F>(self, _timestamp: smoltcp::time::Instant, f: F) -> smoltcp::Result<R>
|
2019-07-03 05:29:16 +08:00
|
|
|
where
|
2019-11-11 07:28:46 +08:00
|
|
|
F: FnOnce(&mut [u8]) -> smoltcp::Result<R>
|
2019-07-03 05:29:16 +08:00
|
|
|
{
|
|
|
|
f(self.buffer)
|
|
|
|
}
|
|
|
|
}
|