10
10
11
11
#define PAGE_SIZE 4096
12
12
13
- #define TX_RING_SIZE 32
14
- #define RX_RING_SIZE 32
13
+ #define TX_DESC_COUNT 80
14
+ #define RX_DESC_COUNT 80
15
15
#define TX_BUFFER_SIZE 2048
16
16
#define RX_BUFFER_SIZE 2048
17
17
#define TX_RING_PAGES \
18
- ((TX_RING_SIZE * sizeof(struct e1000_tx_desc) + PAGE_SIZE - 1) / PAGE_SIZE)
18
+ ((TX_DESC_COUNT * sizeof(struct e1000_tx_desc) + PAGE_SIZE - 1) / PAGE_SIZE)
19
19
#define RX_RING_PAGES \
20
- ((RX_RING_SIZE * sizeof(struct e1000_rx_desc) + PAGE_SIZE - 1) / PAGE_SIZE)
20
+ ((RX_DESC_COUNT * sizeof(struct e1000_rx_desc) + PAGE_SIZE - 1) / PAGE_SIZE)
21
21
#define TX_BUFFER_PAGES \
22
- ((TX_RING_SIZE * TX_BUFFER_SIZE + PAGE_SIZE - 1) / PAGE_SIZE)
22
+ ((TX_DESC_COUNT * TX_BUFFER_SIZE + PAGE_SIZE - 1) / PAGE_SIZE)
23
23
#define RX_BUFFER_PAGES \
24
- ((TX_RING_SIZE * RX_BUFFER_SIZE + PAGE_SIZE - 1) / PAGE_SIZE)
24
+ ((TX_DESC_COUNT * RX_BUFFER_SIZE + PAGE_SIZE - 1) / PAGE_SIZE)
25
25
26
26
#define r32 (reg ) (*(volatile uint32_t *)(e->mmio_base + reg))
27
27
#define w32 (reg , val ) (*(volatile uint32_t *)(e->mmio_base + reg) = val)
@@ -47,8 +47,8 @@ struct e1000_rx_desc {
47
47
48
48
static_assert (sizeof (struct e1000_tx_desc ) == 16 );
49
49
static_assert (sizeof (struct e1000_rx_desc ) == 16 );
50
- static_assert (TX_RING_SIZE * sizeof (struct e1000_tx_desc ) % 128 == 0 );
51
- static_assert (RX_RING_SIZE * sizeof (struct e1000_rx_desc ) % 128 == 0 );
50
+ static_assert (TX_DESC_COUNT * sizeof (struct e1000_tx_desc ) % 128 == 0 );
51
+ static_assert (RX_DESC_COUNT * sizeof (struct e1000_rx_desc ) % 128 == 0 );
52
52
53
53
struct e1000 {
54
54
pci_address_t addr ;
@@ -78,6 +78,8 @@ enum e1000_reg {
78
78
STATUS = 0x00008 ,
79
79
EECD = 0x00010 ,
80
80
EERD = 0x00014 ,
81
+ FCT = 0x00030 ,
82
+ VET = 0x00038 ,
81
83
ICR = 0x000C0 ,
82
84
ITR = 0x000C4 ,
83
85
ICS = 0x000C8 ,
@@ -198,9 +200,9 @@ static void e1000_enable_interrupts(struct e1000 *e, uint32_t mask) {
198
200
w32 (IMS , mask );
199
201
}
200
202
201
- static void e1000_trigger_interrupt (struct e1000 * e , uint32_t mask ) {
202
- w32 (ICS , mask );
203
- }
203
+ // static void e1000_trigger_interrupt(struct e1000 *e, uint32_t mask) {
204
+ // w32(ICS, mask);
205
+ // }
204
206
205
207
static uint32_t e1000_read_interrupt_cause (struct e1000 * e ) { return r32 (ICR ); }
206
208
@@ -214,15 +216,15 @@ static void e1000_set_link_state(struct e1000 *e) {
214
216
static void e1000_enable_rx (struct e1000 * e ) {
215
217
w32 (RDBAL , e -> rx_ring_phy );
216
218
w32 (RDBAH , 0 );
217
- w32 (RDLEN , RX_RING_SIZE * sizeof (struct e1000_rx_desc ));
219
+ w32 (RDLEN , RX_DESC_COUNT * sizeof (struct e1000_rx_desc ));
218
220
w32 (RDH , 0 );
219
- w32 (RDT , RX_RING_SIZE - 1 );
221
+ w32 (RDT , RX_DESC_COUNT - 1 );
220
222
221
223
w32 (RDTR , 0 );
222
224
w32 (RADV , 0 );
223
225
w32 (RSRPD , 0 );
224
226
225
- w32 (ITR , 1'000'000 / 10'000 * 4 );
227
+ w32 (ITR , 651 ); // 1'000'000 / 10'000 * 4);
226
228
w32 (FCRTL , 0 );
227
229
w32 (FCRTH , 0 );
228
230
@@ -232,7 +234,7 @@ static void e1000_enable_rx(struct e1000 *e) {
232
234
static void e1000_enable_tx (struct e1000 * e ) {
233
235
w32 (TDBAL , e -> tx_ring_phy );
234
236
w32 (TDBAH , 0 );
235
- w32 (TDLEN , TX_RING_SIZE * sizeof (struct e1000_tx_desc ));
237
+ w32 (TDLEN , TX_DESC_COUNT * sizeof (struct e1000_tx_desc ));
236
238
w32 (TDH , 0 );
237
239
w32 (TDT , 0 );
238
240
@@ -259,7 +261,7 @@ static void e1000_send(struct e1000 *e, void *data, size_t len) {
259
261
desc -> length = len ;
260
262
desc -> cmd = CMD_EOP | CMD_IFCS | CMD_RS | CMD_RPS ;
261
263
desc -> status = 0 ;
262
- tail = (tail + 1 ) % TX_RING_SIZE ;
264
+ tail = (tail + 1 ) % TX_DESC_COUNT ;
263
265
264
266
printf ("e1000: sending %zu bytes, tail is now %u\n" , len , tail );
265
267
@@ -277,11 +279,16 @@ static void e1000_receive(struct e1000 *e) {
277
279
if (!(desc -> status & 1 ))
278
280
break ;
279
281
282
+ void * data = e -> rx_buffer + e -> rx * RX_BUFFER_SIZE ;
283
+
280
284
printf ("e1000: received %u bytes\n" , desc -> length );
281
- hexdump (e -> rx_buffer + e -> rx * RX_BUFFER_SIZE , desc -> length , 0 );
285
+ hexdump (data , desc -> length , 0 );
286
+
287
+ void net_debug (int , void * , size_t );
288
+ net_debug (0 , data , desc -> length );
282
289
283
290
desc -> status = 0 ;
284
- e -> rx = (e -> rx + 1 ) % RX_RING_SIZE ;
291
+ e -> rx = (e -> rx + 1 ) % RX_DESC_COUNT ;
285
292
w32 (RDT , e -> rx );
286
293
}
287
294
}
@@ -307,6 +314,11 @@ static void e1000_init(struct e1000 *e) {
307
314
e1000_set_rx_mac (e );
308
315
e1000_read_irq (e );
309
316
317
+ // 13.4.10, "this register should be programmed with 88_08h"
318
+ w32 (FCT , 0x8808 );
319
+ // 13.4.11, "this register should be programmed with 8100h"
320
+ w32 (VET , 0x8100 );
321
+
310
322
e -> tx_ring_phy = pm_alloc_contiguous (TX_RING_PAGES );
311
323
e -> tx_descs = (struct e1000_tx_desc * )(e -> tx_ring_phy | limine_hhdm ());
312
324
e -> rx_ring_phy = pm_alloc_contiguous (RX_RING_PAGES );
@@ -316,19 +328,19 @@ static void e1000_init(struct e1000 *e) {
316
328
e -> rx_buffer_phy = pm_alloc_contiguous (RX_BUFFER_PAGES );
317
329
e -> rx_buffer = (void * )(e -> rx_buffer_phy | limine_hhdm ());
318
330
319
- memset (e -> tx_descs , 0 , TX_RING_SIZE * sizeof (struct e1000_tx_desc ));
320
- memset (e -> rx_descs , 0 , RX_RING_SIZE * sizeof (struct e1000_rx_desc ));
321
- memset (e -> tx_buffer , 0 , TX_RING_SIZE * TX_BUFFER_SIZE );
322
- memset (e -> rx_buffer , 0 , RX_RING_SIZE * RX_BUFFER_SIZE );
331
+ memset (e -> tx_descs , 0 , TX_DESC_COUNT * sizeof (struct e1000_tx_desc ));
332
+ memset (e -> rx_descs , 0 , RX_DESC_COUNT * sizeof (struct e1000_rx_desc ));
333
+ memset (e -> tx_buffer , 0 , TX_DESC_COUNT * TX_BUFFER_SIZE );
334
+ memset (e -> rx_buffer , 0 , RX_DESC_COUNT * RX_BUFFER_SIZE );
323
335
324
336
// Initialize rx ring
325
- for (int i = 0 ; i < RX_RING_SIZE ; i ++ ) {
337
+ for (int i = 0 ; i < RX_DESC_COUNT ; i ++ ) {
326
338
e -> rx_descs [i ].addr = e -> rx_buffer_phy + i * RX_BUFFER_SIZE ;
327
339
e -> rx_descs [i ].status = 0 ;
328
340
}
329
341
330
342
// Initialize tx ring
331
- for (int i = 0 ; i < TX_RING_SIZE ; i ++ ) {
343
+ for (int i = 0 ; i < TX_DESC_COUNT ; i ++ ) {
332
344
e -> tx_descs [i ].addr = e -> tx_buffer_phy + i * TX_BUFFER_SIZE ;
333
345
e -> tx_descs [i ].status = 0 ;
334
346
}
@@ -378,6 +390,11 @@ void e1000_test(pci_address_t addr) {
378
390
0x56 , 0x08 , 0x06 , 0x00 , 0x01 , 0x08 , 0x00 , 0x06 , 0x04 , 0x00 , 0x01 ,
379
391
0x52 , 0x54 , 0x00 , 0x12 , 0x34 , 0x56 , 0x0a , 0x00 , 0x02 , 0x0f , 0x00 ,
380
392
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x0a , 0x00 , 0x02 , 0x02 };
393
+ printf ("sending frame:\n" );
394
+
395
+ void net_debug (int , void * , size_t );
396
+ net_debug (0 , ethernet_frame , sizeof (ethernet_frame ));
397
+
381
398
e1000_send (e , ethernet_frame , sizeof (ethernet_frame ));
382
399
383
400
printf ("%i -> %i\n" , r32 (TDH ), r32 (TDT ));
0 commit comments