From 46af38906e71de765ab75404fcc4c72c03344f99 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 28 Mar 2020 23:30:39 +0100 Subject: [PATCH] libboard_zynq: wrap eth Buffer for alignment --- libboard_zynq/src/eth/mod.rs | 28 ++++++++++++++++++++++++++-- libboard_zynq/src/eth/rx.rs | 8 ++++---- libboard_zynq/src/eth/tx.rs | 8 ++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/libboard_zynq/src/eth/mod.rs b/libboard_zynq/src/eth/mod.rs index 4a74f7d..be05378 100644 --- a/libboard_zynq/src/eth/mod.rs +++ b/libboard_zynq/src/eth/mod.rs @@ -1,3 +1,4 @@ +use core::ops::{Deref, DerefMut}; use libregister::*; use crate::println; use super::slcr; @@ -18,6 +19,29 @@ const TX_100: u32 = 25_000_000; /// Clock for GbE const TX_1000: u32 = 125_000_000; +#[derive(Clone)] +#[repr(C, align(0x08))] +pub struct Buffer(pub [u8; MTU]); + +impl Buffer { + pub fn new() -> Self { + Buffer([0; MTU]) + } +} + +impl Deref for Buffer { + type Target = [u8]; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Buffer { + fn deref_mut(&mut self) -> &mut ::Target { + &mut self.0 + } +} + pub struct Eth<'r, RX, TX> { rx: RX, tx: TX, @@ -239,7 +263,7 @@ impl<'r, RX, TX> Eth<'r, RX, TX> { }); } - pub fn start_rx<'rx>(self, rx_list: &'rx mut [rx::DescEntry], rx_buffers: &'rx mut [[u8; MTU]]) -> Eth<'r, rx::DescList<'rx>, TX> { + pub fn start_rx<'rx>(self, rx_list: &'rx mut [rx::DescEntry], rx_buffers: &'rx mut [Buffer]) -> Eth<'r, rx::DescList<'rx>, TX> { let new_self = Eth { rx: rx::DescList::new(rx_list, rx_buffers), tx: self.tx, @@ -258,7 +282,7 @@ impl<'r, RX, TX> Eth<'r, RX, TX> { new_self } - pub fn start_tx<'tx>(self, tx_list: &'tx mut [tx::DescEntry], tx_buffers: &'tx mut [[u8; MTU]]) -> Eth<'r, RX, tx::DescList<'tx>> { + pub fn start_tx<'tx>(self, tx_list: &'tx mut [tx::DescEntry], tx_buffers: &'tx mut [Buffer]) -> Eth<'r, RX, tx::DescList<'tx>> { let new_self = Eth { rx: self.rx, tx: tx::DescList::new(tx_list, tx_buffers), diff --git a/libboard_zynq/src/eth/rx.rs b/libboard_zynq/src/eth/rx.rs index 61cf996..2a583cb 100644 --- a/libboard_zynq/src/eth/rx.rs +++ b/libboard_zynq/src/eth/rx.rs @@ -1,6 +1,6 @@ use core::ops::Deref; use libregister::*; -use super::MTU; +use super::Buffer; #[derive(Debug)] pub enum Error { @@ -55,16 +55,16 @@ register_bit!(desc_word1, global_broadcast, 31); #[repr(C)] pub struct DescList<'a> { list: &'a mut [DescEntry], - buffers: &'a mut [[u8; MTU]], + buffers: &'a mut [Buffer], next: usize, } impl<'a> DescList<'a> { - pub fn new(list: &'a mut [DescEntry], buffers: &'a mut [[u8; MTU]]) -> Self { + pub fn new(list: &'a mut [DescEntry], buffers: &'a mut [Buffer]) -> Self { 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; - let buffer_addr = &mut buffer[0] as *mut _ as u32; + let buffer_addr = &mut buffer.0[0] as *mut _ as u32; assert!(buffer_addr & 0b11 == 0); entry.word0.write( DescWord0::zeroed() diff --git a/libboard_zynq/src/eth/tx.rs b/libboard_zynq/src/eth/tx.rs index a9ae46f..665a3c2 100644 --- a/libboard_zynq/src/eth/tx.rs +++ b/libboard_zynq/src/eth/tx.rs @@ -1,6 +1,6 @@ use core::ops::{Deref, DerefMut}; use libregister::*; -use super::{MTU, regs}; +use super::{Buffer, regs}; /// Descriptor entry #[repr(C, align(0x08))] @@ -42,12 +42,12 @@ pub const DESCS: usize = 8; #[repr(C)] pub struct DescList<'a> { list: &'a mut [DescEntry], - buffers: &'a mut [[u8; MTU]], + buffers: &'a mut [Buffer], next: usize, } impl<'a> DescList<'a> { - pub fn new(list: &'a mut [DescEntry], buffers: &'a mut [[u8; MTU]]) -> Self { + pub fn new(list: &'a mut [DescEntry], buffers: &'a mut [Buffer]) -> Self { let last = list.len().min(buffers.len()) - 1; // Sending seems to not work properly with only one packet // buffer (two duplicates get send with every packet), so @@ -57,7 +57,7 @@ impl<'a> DescList<'a> { for (i, (entry, buffer)) in list.iter_mut().zip(buffers.iter_mut()).enumerate() { let is_last = i == last; - let buffer_addr = &mut buffer[0] as *mut _ as u32; + let buffer_addr = &mut buffer.0[0] as *mut _ as u32; assert!(buffer_addr & 0b11 == 0); entry.word0.write( DescWord0::zeroed()