Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #750 by maintaining the Rx tail pointer #1493

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions drv/stm32h7-eth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#![no_std]

use core::convert::TryFrom;
use core::sync::atomic::{self, Ordering};

#[cfg(feature = "h743")]
use stm32h7::stm32h743 as device;
Expand Down Expand Up @@ -157,8 +158,10 @@ impl Ethernet {
// above.
dma.dmactx_dtpr
.write(|w| unsafe { w.tdt().bits(tx_ring.tail_ptr() as u32 >> 2) });
dma.dmacrx_dtpr
.write(|w| unsafe { w.rdt().bits(rx_ring.tail_ptr() as u32 >> 2) });
atomic::fence(Ordering::Release);
dma.dmacrx_dtpr.write(|w| unsafe {
w.rdt().bits(rx_ring.first_tail_ptr() as u32 >> 2)
});
dancrossnyc marked this conversation as resolved.
Show resolved Hide resolved

// Central DMA config:

Expand Down Expand Up @@ -318,11 +321,11 @@ impl Ethernet {
fn rx_notify(&self) {
// We have dequeued a packet! The hardware might not realize there is
// room in the RX queue now. Poke it.
core::sync::atomic::fence(core::sync::atomic::Ordering::Release);
atomic::fence(Ordering::Release);
// Poke the tail pointer so the hardware knows to recheck (dropping two
// bottom bits because svd2rust)
// bottom bits because svd2rust).
self.dma.dmacrx_dtpr.write(|w| unsafe {
w.rdt().bits(self.rx_ring.tail_ptr() as u32 >> 2)
w.rdt().bits(self.rx_ring.next_tail_ptr() as u32 >> 2)
});
}

Expand Down
24 changes: 14 additions & 10 deletions drv/stm32h7-eth/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,18 @@ impl RxRing {
self.storage.as_ptr()
}

/// Returns a pointer to the byte just past the end of the `RxDesc` ring.
/// This too gets loaded into the DMA controller, so that it knows what
/// section of the ring is initialized and can be read. (The answer is "all
/// of it.")
pub fn tail_ptr(&self) -> *const RxDesc {
self.storage.as_ptr_range().end
/// Returns a pointer to the last valid descriptor in the ring.
/// We load this into the DMA controller when we first start operation so
/// that it knows what section of the ring is initialized and can be read.
/// The answer is "all of it," but we can't exactly specify that.
pub fn first_tail_ptr(&self) -> *const RxDesc {
self.base_ptr().wrapping_add(self.len() - 1)
}

/// Returns a pointer to the "next" descriptor in the ring. We load this
/// into the device so that the DMA engine knows what descriptors are free.
pub fn next_tail_ptr(&self) -> *const RxDesc {
self.base_ptr().wrapping_add(self.next.get())
}

/// Returns the count of entries in the descriptor ring / buffers in the
Expand All @@ -468,12 +474,10 @@ impl RxRing {
fn set_descriptor(d: &RxDesc, buffer: *mut [u8; BUFSZ]) {
d.rdes[0].store(buffer as u32, Ordering::Relaxed);
d.rdes[1].store(0, Ordering::Relaxed);
d.rdes[2].store(0, Ordering::Release);
// See hubris#750 for why we need Ordering::Release and this delay
cortex_m::asm::delay(16);
d.rdes[2].store(0, Ordering::Relaxed);
let rdes3 =
1 << RDES3_OWN_BIT | 1 << RDES3_IOC_BIT | 1 << RDES3_BUF1_VALID_BIT;
d.rdes[3].store(rdes3, Ordering::Release); // <-- release
d.rdes[3].store(rdes3, Ordering::Relaxed);
}
}

Expand Down