@@ -22,10 +22,11 @@ use alloc::sync::Arc;
22
22
use crate :: drivers:: pci:: * ;
23
23
use crate :: mem:: paging:: * ;
24
24
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 ;
27
27
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 ;
29
30
30
31
#[ derive( Copy , Clone , Debug ) ]
31
32
enum Error {
@@ -41,6 +42,18 @@ enum Register {
41
42
Control = 0x00 ,
42
43
Eeprom = 0x14 ,
43
44
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
+
44
57
TCtrl = 0x400 ,
45
58
/// Lower bits of the 64 bit descriptor base address.
46
59
TxDesLo = 0x3800 ,
@@ -69,7 +82,7 @@ bitflags::bitflags! {
69
82
}
70
83
71
84
bitflags:: bitflags! {
72
- pub struct TStatus : u8 {
85
+ struct TStatus : u8 {
73
86
const DD = 1 << 0 ; // Descriptor Done
74
87
const EC = 1 << 1 ; // Excess Collisions
75
88
const LC = 1 << 2 ; // Late Collision
@@ -78,7 +91,7 @@ bitflags::bitflags! {
78
91
}
79
92
80
93
bitflags:: bitflags! {
81
- pub struct TCtl : u32 {
94
+ struct TCtl : u32 {
82
95
const EN = 1 << 1 ; // Transmit Enable
83
96
const PSP = 1 << 3 ; // Pad Short Packets
84
97
const SWXOFF = 1 << 22 ; // Software XOFF Transmission
@@ -89,19 +102,54 @@ bitflags::bitflags! {
89
102
impl TCtl {
90
103
/// Sets the number of attempts at retransmission prior to giving
91
104
/// 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 ) {
93
106
self . bits |= ( value as u32 ) << 4 ;
94
107
}
95
108
96
109
/// Sets the minimum number of byte times which must elapse for
97
110
/// proper CSMA/CD operation.
98
- pub fn set_collision_distance ( & mut self , value : u8 ) {
111
+ fn set_collision_distance ( & mut self , value : u8 ) {
99
112
self . bits |= ( value as u32 ) << 12 ;
100
113
}
101
114
}
102
115
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
+
103
151
#[ repr( C , packed) ]
104
- pub struct TxDescriptor {
152
+ struct TxDescriptor {
105
153
pub addr : u64 ,
106
154
pub length : u16 ,
107
155
pub cso : u8 ,
@@ -111,6 +159,16 @@ pub struct TxDescriptor {
111
159
pub special : u16 ,
112
160
}
113
161
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
+
114
172
struct Eeprom < ' a > {
115
173
e1000 : & ' a E1000 ,
116
174
}
@@ -189,25 +247,25 @@ impl E1000 {
189
247
}
190
248
191
249
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 ) ;
193
251
194
252
let frame: PhysFrame < Size4KiB > =
195
253
FRAME_ALLOCATOR . allocate_frame ( ) . ok_or ( Error :: OutOfMemory ) ?;
196
254
197
- let addr = frame. start_address ( ) . as_hhdm_virt ( ) ;
255
+ let phys = frame. start_address ( ) ;
256
+ let addr = phys. as_hhdm_virt ( ) ;
257
+
198
258
let descriptors = addr
199
- . read_mut :: < [ TxDescriptor ; TX_DESC_NUM ] > ( )
259
+ . read_mut :: < [ TxDescriptor ; TX_DESC_NUM as usize ] > ( )
200
260
. ok_or ( Error :: NotSupported ) ?;
201
261
202
262
for desc in descriptors {
203
263
desc. status = TStatus :: DD ;
204
264
}
205
265
206
- let phys = frame. start_address ( ) ;
207
-
208
266
self . write ( Register :: TxDesLo , phys. as_u64 ( ) as _ ) ;
209
267
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 ) ;
211
269
self . write ( Register :: TxDescHead , 0 ) ;
212
270
self . write ( Register :: TxDescTail , 0 ) ;
213
271
@@ -225,6 +283,42 @@ impl E1000 {
225
283
}
226
284
227
285
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 ( ) ) ;
228
322
Ok ( ( ) )
229
323
}
230
324
0 commit comments