-
Notifications
You must be signed in to change notification settings - Fork 5
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
Support ethernet driver: fxmac for PhytiumPi #1
Open
elliott10
wants to merge
3
commits into
arceos-org:main
Choose a base branch
from
elliott10:phtpi-tag-v0.1.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use crate::{EthernetAddress, NetBufPtr, NetDriverOps}; | ||
use alloc::boxed::Box; | ||
use alloc::collections::VecDeque; | ||
use alloc::vec::Vec; | ||
use axdriver_base::{BaseDriverOps, DevError, DevResult, DeviceType}; | ||
use core::ptr::NonNull; | ||
|
||
use fxmac_rs::{self, xmac_init, FXmac, FXmacLwipPortTx, FXmacRecvHandler}; | ||
use log::*; | ||
|
||
extern crate alloc; | ||
|
||
const QS: usize = 64; | ||
//const NET_BUF_LEN: usize = 1526; | ||
|
||
/// fxmac driver device | ||
pub struct FXmacNic { | ||
inner: &'static mut FXmac, | ||
rx_buffer_queue: VecDeque<NetBufPtr>, | ||
} | ||
|
||
unsafe impl Sync for FXmacNic {} | ||
unsafe impl Send for FXmacNic {} | ||
|
||
impl FXmacNic { | ||
/// initialize fxmac driver | ||
pub fn init(mapped_regs: usize) -> DevResult<Self> { | ||
info!("FXmacNic init @ {:#x}", mapped_regs); | ||
let rx_buffer_queue = VecDeque::with_capacity(QS); | ||
let hwaddr: [u8; 6] = [0x98, 0x0e, 0x24, 0x00, 0x11, 0x0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid hardcoding MAC address? |
||
let inner = xmac_init(&hwaddr); | ||
|
||
let dev = Self { | ||
inner, | ||
rx_buffer_queue, | ||
}; | ||
Ok(dev) | ||
} | ||
} | ||
|
||
impl BaseDriverOps for FXmacNic { | ||
fn device_name(&self) -> &str { | ||
"cdns,phytium-gem-1.0" | ||
} | ||
|
||
fn device_type(&self) -> DeviceType { | ||
DeviceType::Net | ||
} | ||
} | ||
|
||
impl NetDriverOps for FXmacNic { | ||
fn mac_address(&self) -> EthernetAddress { | ||
EthernetAddress([0x98, 0x0e, 0x24, 0x00, 0x11, 0x0]) | ||
} | ||
|
||
fn rx_queue_size(&self) -> usize { | ||
QS | ||
} | ||
|
||
fn tx_queue_size(&self) -> usize { | ||
QS | ||
} | ||
|
||
fn can_receive(&self) -> bool { | ||
!self.rx_buffer_queue.is_empty() | ||
} | ||
|
||
fn can_transmit(&self) -> bool { | ||
//!self.free_tx_bufs.is_empty() | ||
true | ||
} | ||
|
||
fn recycle_rx_buffer(&mut self, rx_buf: NetBufPtr) -> DevResult { | ||
unsafe { | ||
drop(Box::from_raw(rx_buf.raw_ptr::<u8>())); | ||
} | ||
drop(rx_buf); | ||
Ok(()) | ||
} | ||
|
||
fn recycle_tx_buffers(&mut self) -> DevResult { | ||
// drop tx_buf | ||
Ok(()) | ||
} | ||
|
||
fn receive(&mut self) -> DevResult<NetBufPtr> { | ||
if !self.rx_buffer_queue.is_empty() { | ||
// RX buffer have received packets. | ||
Ok(self.rx_buffer_queue.pop_front().unwrap()) | ||
} else { | ||
match FXmacRecvHandler(self.inner) { | ||
None => Err(DevError::Again), | ||
Some(packets) => { | ||
for packet in packets { | ||
info!("received packet length {}", packet.len()); | ||
let mut buf = Box::new(packet); | ||
let buf_ptr = buf.as_mut_ptr() as *mut u8; | ||
let buf_len = buf.len(); | ||
let rx_buf = NetBufPtr::new( | ||
NonNull::new(Box::into_raw(buf) as *mut u8).unwrap(), | ||
NonNull::new(buf_ptr).unwrap(), | ||
buf_len, | ||
); | ||
|
||
self.rx_buffer_queue.push_back(rx_buf); | ||
} | ||
|
||
Ok(self.rx_buffer_queue.pop_front().unwrap()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn transmit(&mut self, tx_buf: NetBufPtr) -> DevResult { | ||
let mut tx_vec = Vec::new(); | ||
tx_vec.push(tx_buf.packet().to_vec()); | ||
let ret = FXmacLwipPortTx(self.inner, tx_vec); | ||
unsafe { | ||
drop(Box::from_raw(tx_buf.raw_ptr::<u8>())); | ||
} | ||
if ret < 0 { | ||
Err(DevError::Again) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn alloc_tx_buffer(&mut self, size: usize) -> DevResult<NetBufPtr> { | ||
let mut tx_buf = Box::new(alloc::vec![0; size]); | ||
let tx_buf_ptr = tx_buf.as_mut_ptr(); | ||
|
||
Ok(NetBufPtr::new( | ||
NonNull::new(Box::into_raw(tx_buf) as *mut u8).unwrap(), | ||
NonNull::new(tx_buf_ptr).unwrap(), | ||
size, | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Organize crates Import order: first
core
/alloc
, then external crates, thencrate::*
/super::*
.