Skip to content

Commit d7bc6a9

Browse files
e1000: init rx
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent ad14a44 commit d7bc6a9

File tree

1 file changed

+108
-14
lines changed

1 file changed

+108
-14
lines changed

src/aero_kernel/src/drivers/e1000.rs

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ use alloc::sync::Arc;
2222
use crate::drivers::pci::*;
2323
use crate::mem::paging::*;
2424

25-
const TX_DESC_NUM: usize = 32;
26-
const TX_DESC_SIZE: usize = TX_DESC_NUM * core::mem::size_of::<TxDescriptor>();
25+
const TX_DESC_NUM: u32 = 32;
26+
const TX_DESC_SIZE: u32 = TX_DESC_NUM * core::mem::size_of::<TxDescriptor>() as u32;
2727

28-
const RX_QUEUE_SIZE: usize = 32;
28+
const RX_DESC_NUM: u32 = 32;
29+
const RX_DESC_SIZE: u32 = RX_DESC_NUM * core::mem::size_of::<RxDescriptor>() as u32;
2930

3031
#[derive(Copy, Clone, Debug)]
3132
enum Error {
@@ -41,6 +42,18 @@ enum Register {
4142
Control = 0x00,
4243
Eeprom = 0x14,
4344

45+
RCtrl = 0x0100,
46+
/// Lower bits of the 64 bit descriptor base address.
47+
RxDescLo = 0x2800,
48+
/// Upper 32 bits of the 64 bit descriptor base address.
49+
RxDescHi = 0x2804,
50+
/// Descriptor length and must be 128B aligned.
51+
RxDescLen = 0x2808,
52+
/// Head pointer for the receive descriptor buffer.
53+
RxDescHead = 0x2810,
54+
/// Tail pointer for the receive descriptor buffer.
55+
RxDescTail = 0x2818,
56+
4457
TCtrl = 0x400,
4558
/// Lower bits of the 64 bit descriptor base address.
4659
TxDesLo = 0x3800,
@@ -69,7 +82,7 @@ bitflags::bitflags! {
6982
}
7083

7184
bitflags::bitflags! {
72-
pub struct TStatus: u8 {
85+
struct TStatus: u8 {
7386
const DD = 1 << 0; // Descriptor Done
7487
const EC = 1 << 1; // Excess Collisions
7588
const LC = 1 << 2; // Late Collision
@@ -78,7 +91,7 @@ bitflags::bitflags! {
7891
}
7992

8093
bitflags::bitflags! {
81-
pub struct TCtl: u32 {
94+
struct TCtl: u32 {
8295
const EN = 1 << 1; // Transmit Enable
8396
const PSP = 1 << 3; // Pad Short Packets
8497
const SWXOFF = 1 << 22; // Software XOFF Transmission
@@ -89,19 +102,54 @@ bitflags::bitflags! {
89102
impl TCtl {
90103
/// Sets the number of attempts at retransmission prior to giving
91104
/// up on the packet (not including the first transmission attempt).
92-
pub fn set_collision_threshold(&mut self, value: u8) {
105+
fn set_collision_threshold(&mut self, value: u8) {
93106
self.bits |= (value as u32) << 4;
94107
}
95108

96109
/// Sets the minimum number of byte times which must elapse for
97110
/// proper CSMA/CD operation.
98-
pub fn set_collision_distance(&mut self, value: u8) {
111+
fn set_collision_distance(&mut self, value: u8) {
99112
self.bits |= (value as u32) << 12;
100113
}
101114
}
102115

116+
bitflags::bitflags! {
117+
struct RCtl: u32 {
118+
const EN = 1 << 1; // Receiver Enable
119+
const SBP = 1 << 2; // Store Bad Packets
120+
const UPE = 1 << 3; // Unicast Promiscuous Enabled
121+
const MPE = 1 << 4; // Multicast Promiscuous Enabled
122+
const LPE = 1 << 5; // Long Packet Reception Enable
123+
const LBM_NONE = 0 << 6; // No Loopback
124+
const LBM_PHY = 3 << 6; // PHY or external SerDesc loopback
125+
const RDMTS_HALF = 0 << 8; // Free Buffer Threshold is 1/2 of RDLEN
126+
const RDMTS_QUARTER = 1 << 8; // Free Buffer Threshold is 1/4 of RDLEN
127+
const RDMTS_EIGHTH = 2 << 8; // Free Buffer Threshold is 1/8 of RDLEN
128+
const MO_36 = 0 << 12; // Multicast Offset - bits 47:36
129+
const MO_35 = 1 << 12; // Multicast Offset - bits 46:35
130+
const MO_34 = 2 << 12; // Multicast Offset - bits 45:34
131+
const MO_32 = 3 << 12; // Multicast Offset - bits 43:32
132+
const BAM = 1 << 15; // Broadcast Accept Mode
133+
const VFE = 1 << 18; // VLAN Filter Enable
134+
const CFIEN = 1 << 19; // Canonical Form Indicator Enable
135+
const CFI = 1 << 20; // Canonical Form Indicator Bit Value
136+
const DPF = 1 << 22; // Discard Pause Frames
137+
const PMCF = 1 << 23; // Pass MAC Control Frames
138+
const SECRC = 1 << 26; // Strip Ethernet CRC
139+
140+
// Receive Buffer Size - bits 17:16
141+
const BSIZE_256 = 3 << 16;
142+
const BSIZE_512 = 2 << 16;
143+
const BSIZE_1024 = 1 << 16;
144+
const BSIZE_2048 = 0 << 16;
145+
const BSIZE_4096 = (3 << 16) | (1 << 25);
146+
const BSIZE_8192 = (2 << 16) | (1 << 25);
147+
const BSIZE_16384 = (1 << 16) | (1 << 25);
148+
}
149+
}
150+
103151
#[repr(C, packed)]
104-
pub struct TxDescriptor {
152+
struct TxDescriptor {
105153
pub addr: u64,
106154
pub length: u16,
107155
pub cso: u8,
@@ -111,6 +159,16 @@ pub struct TxDescriptor {
111159
pub special: u16,
112160
}
113161

162+
#[repr(C, packed)]
163+
struct RxDescriptor {
164+
pub addr: u64,
165+
pub length: u16,
166+
pub checksum: u16,
167+
pub status: u8,
168+
pub errors: u8,
169+
pub special: u16,
170+
}
171+
114172
struct Eeprom<'a> {
115173
e1000: &'a E1000,
116174
}
@@ -189,25 +247,25 @@ impl E1000 {
189247
}
190248

191249
fn init_tx(&self) -> Result<(), Error> {
192-
assert!(core::mem::size_of::<TxDescriptor>() * TX_DESC_NUM < Size4KiB::SIZE as usize);
250+
assert!(TX_DESC_SIZE < Size4KiB::SIZE as u32);
193251

194252
let frame: PhysFrame<Size4KiB> =
195253
FRAME_ALLOCATOR.allocate_frame().ok_or(Error::OutOfMemory)?;
196254

197-
let addr = frame.start_address().as_hhdm_virt();
255+
let phys = frame.start_address();
256+
let addr = phys.as_hhdm_virt();
257+
198258
let descriptors = addr
199-
.read_mut::<[TxDescriptor; TX_DESC_NUM]>()
259+
.read_mut::<[TxDescriptor; TX_DESC_NUM as usize]>()
200260
.ok_or(Error::NotSupported)?;
201261

202262
for desc in descriptors {
203263
desc.status = TStatus::DD;
204264
}
205265

206-
let phys = frame.start_address();
207-
208266
self.write(Register::TxDesLo, phys.as_u64() as _);
209267
self.write(Register::TxDesHi, (phys.as_u64() >> 32) as _);
210-
self.write(Register::TxDescLen, TX_DESC_SIZE as _);
268+
self.write(Register::TxDescLen, TX_DESC_SIZE);
211269
self.write(Register::TxDescHead, 0);
212270
self.write(Register::TxDescTail, 0);
213271

@@ -225,6 +283,42 @@ impl E1000 {
225283
}
226284

227285
fn init_rx(&self) -> Result<(), Error> {
286+
assert!(TX_DESC_SIZE < Size4KiB::SIZE as u32);
287+
288+
let frame: PhysFrame<Size4KiB> =
289+
FRAME_ALLOCATOR.allocate_frame().ok_or(Error::OutOfMemory)?;
290+
291+
let phys = frame.start_address();
292+
let addr = phys.as_hhdm_virt();
293+
294+
let descriptors = addr
295+
.read_mut::<[RxDescriptor; RX_DESC_NUM as usize]>()
296+
.ok_or(Error::NotSupported)?;
297+
298+
for desc in descriptors {
299+
let frame: PhysFrame<Size4KiB> =
300+
FRAME_ALLOCATOR.allocate_frame().ok_or(Error::OutOfMemory)?;
301+
302+
desc.addr = frame.start_address().as_u64();
303+
}
304+
305+
self.write(Register::RxDescLo, phys.as_u64() as _);
306+
self.write(Register::RxDescHi, (phys.as_u64() >> 32) as _);
307+
self.write(Register::RxDescLen, RX_DESC_SIZE);
308+
self.write(Register::RxDescHead, 0);
309+
self.write(Register::RxDescTail, RX_DESC_NUM - 1);
310+
311+
let flags = RCtl::EN
312+
| RCtl::UPE
313+
| RCtl::LPE
314+
| RCtl::MPE
315+
| RCtl::LBM_NONE
316+
| RCtl::RDMTS_EIGHTH
317+
| RCtl::BAM
318+
| RCtl::SECRC
319+
| RCtl::BSIZE_4096;
320+
321+
self.write(Register::RCtrl, flags.bits());
228322
Ok(())
229323
}
230324

0 commit comments

Comments
 (0)