OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [peripheral/] [eth.c] - Blame information for rev 1308

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 696 ivang
/* ethernet.c -- Simulation of Ethernet MAC
2
   Copyright (C) 2001 by Erez Volk, erez@opencores.org
3
                         Ivan Guzvinec, ivang@opencores.org
4
 
5
   This file is part of OpenRISC 1000 Architectural Simulator.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 2 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
*/
21
 
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <sys/types.h>
26
#include <sys/stat.h>   
27
#include <fcntl.h>      
28
#include <sys/poll.h>   
29
#include <sys/time.h>   
30
#include <unistd.h>     
31
#include <errno.h>
32 1308 phoenix
#include <netinet/in.h>
33 696 ivang
 
34 867 markom
#include "config.h"
35 696 ivang
#include "abstract.h"
36
#include "ethernet_i.h"
37
#include "dma.h"
38
#include "sim-config.h"
39
#include "fields.h"
40
#include "crc32.h"
41 889 ivang
#include "vapi.h"
42 1308 phoenix
#include "pic.h"
43
#include "debug.h"
44 696 ivang
 
45
static struct eth_device eths[MAX_ETHERNETS];
46
 
47 702 ivang
/* simulator interface */
48
static void eth_reset_controller( struct eth_device *eth);
49 889 ivang
static void eth_vapi_read( unsigned long id, unsigned long data);
50 696 ivang
/* register interface */
51
static void eth_write32( unsigned long addr, unsigned long value );
52
static unsigned long eth_read32( unsigned long addr );
53
/* clock */
54
static void eth_controller_tx_clock( struct eth_device * );
55
static void eth_controller_rx_clock( struct eth_device * );
56
/* utility functions */
57
static int eth_find_controller( unsigned long addr, struct eth_device **eth, unsigned long *reladdr );
58 1244 hpanther
struct eth_device *eth_find_vapi_device (unsigned long id, unsigned long *which);
59 702 ivang
static ssize_t eth_read_rx_file( struct eth_device *, void *, size_t );
60
static void eth_skip_rx_file( struct eth_device *, off_t );
61
static void eth_rewind_rx_file( struct eth_device *, off_t );
62
static void eth_rx_next_packet( struct eth_device * );
63
static void eth_write_tx_bd_num( struct eth_device *, unsigned long value );
64 696 ivang
/* ========================================================================= */
65 702 ivang
/*  TX LOGIC                                                                 */
66 696 ivang
/*---------------------------------------------------------------------------*/
67
 
68
/*
69
 * TX clock
70
 * Responsible for starting and finishing TX
71
 */
72
void eth_controller_tx_clock( struct eth_device *eth )
73
{
74 702 ivang
    int breakpoint = 0;
75
    int bAdvance   = 1;
76 867 markom
#if HAVE_ETH_PHY
77 702 ivang
    struct sockaddr_ll sll;
78 849 markom
#endif /* HAVE_ETH_PHY */
79 702 ivang
    long nwritten;
80
    unsigned long read_word;
81 696 ivang
 
82
    switch (eth->tx.state) {
83 702 ivang
        case ETH_TXSTATE_IDLE:
84
        if ( TEST_FLAG( eth->regs.moder, ETH_MODER, TXEN ) ) {
85
 
86
            /* wait for TxBuffer to be ready */
87 705 ivang
                debug (3, "TX - entering state WAIT4BD (%d)\n", eth->tx.bd_index);
88 702 ivang
            eth->tx.state = ETH_TXSTATE_WAIT4BD;
89
        }
90
        break;
91 696 ivang
    case ETH_TXSTATE_WAIT4BD:
92 702 ivang
        /* Read buffer descriptor */
93
        eth->tx.bd = eth->regs.bd_ram[eth->tx.bd_index];
94
        eth->tx.bd_addr = eth->regs.bd_ram[eth->tx.bd_index + 1];
95
 
96
        if ( TEST_FLAG( eth->tx.bd, ETH_TX_BD, READY ) ) {
97
            /*****************/
98
            /* initialize TX */
99
            eth->tx.bytes_left = eth->tx.packet_length = GET_FIELD( eth->tx.bd, ETH_TX_BD, LENGTH );
100
            eth->tx.bytes_sent = 0;
101
 
102
            /*   Initialize error status bits */
103
            CLEAR_FLAG( eth->tx.bd, ETH_TX_BD, DEFER );
104
            CLEAR_FLAG( eth->tx.bd, ETH_TX_BD, COLLISION );
105
            CLEAR_FLAG( eth->tx.bd, ETH_TX_BD, RETRANSMIT );
106
            CLEAR_FLAG( eth->tx.bd, ETH_TX_BD, UNDERRUN );
107
            CLEAR_FLAG( eth->tx.bd, ETH_TX_BD, NO_CARRIER );
108
            SET_FIELD ( eth->tx.bd, ETH_TX_BD, RETRY, 0 );
109
 
110
            /* Find out minimum length */
111
            if ( TEST_FLAG( eth->tx.bd, ETH_TX_BD, PAD ) ||
112
                 TEST_FLAG( eth->regs.moder, ETH_MODER, PAD ) )
113
                eth->tx.minimum_length = GET_FIELD( eth->regs.packetlen, ETH_PACKETLEN, MINFL );
114
            else
115
                eth->tx.minimum_length = eth->tx.packet_length;
116
 
117
            /* Find out maximum length */
118
            if ( TEST_FLAG( eth->regs.moder, ETH_MODER, HUGEN ) )
119
                eth->tx.maximum_length = eth->tx.packet_length;
120
            else
121
                eth->tx.maximum_length = GET_FIELD( eth->regs.packetlen, ETH_PACKETLEN, MAXFL );
122
 
123
            /* Do we need CRC on this packet? */
124
            if ( TEST_FLAG( eth->regs.moder, ETH_MODER, CRCEN ) ||
125
                 (TEST_FLAG( eth->tx.bd, ETH_TX_BD, CRC) &&
126
                  TEST_FLAG( eth->tx.bd, ETH_TX_BD, LAST)) )
127
                eth->tx.add_crc = 1;
128
            else
129
                eth->tx.add_crc = 0;
130
 
131
            if ( TEST_FLAG( eth->regs.moder, ETH_MODER, DLYCRCEN ) )
132
                eth->tx.crc_dly = 1;
133
            else
134
                eth->tx.crc_dly = 0;
135
            /* XXX - For now we skip CRC calculation */
136
 
137
            debug( 3, "Ethernet: Starting TX of %u bytes (min. %u, max. %u)\n", eth->tx.packet_length,
138
                   eth->tx.minimum_length, eth->tx.maximum_length );
139
 
140
            if (eth->rtx_type == ETH_RTX_FILE) {
141
                /* write packet length to file */
142
                nwritten = write( eth->txfd, &(eth->tx.packet_length), sizeof(eth->tx.packet_length) );
143
            }
144
 
145
            /************************************************/
146
            /* start transmit with reading packet into FIFO */
147
                debug (3, "TX - entering state READFIFO\n");
148
            eth->tx.state = ETH_TXSTATE_READFIFO;
149
        }
150
        else if ( !TEST_FLAG( eth->regs.moder, ETH_MODER, TXEN ) ) {
151
            /* stop TX logic */
152
                debug (3, "TX - entering state IDLE\n");
153
            eth->tx.state = ETH_TXSTATE_IDLE;
154
        }
155
 
156
        /* stay in this state if (TXEN && !READY) */
157
        break;
158 696 ivang
    case ETH_TXSTATE_READFIFO:
159 744 simons
#if 1
160 702 ivang
        if ( eth->tx.bytes_sent < eth->tx.packet_length ) {
161 1241 phoenix
            read_word = eval_direct32(eth->tx.bytes_sent + eth->tx.bd_addr, &breakpoint, 0, 0);
162 702 ivang
            eth->tx_buff[eth->tx.bytes_sent]   = (unsigned char)(read_word >> 24);
163
            eth->tx_buff[eth->tx.bytes_sent+1] = (unsigned char)(read_word >> 16);
164
            eth->tx_buff[eth->tx.bytes_sent+2] = (unsigned char)(read_word >> 8);
165
            eth->tx_buff[eth->tx.bytes_sent+3] = (unsigned char)(read_word);
166
            eth->tx.bytes_sent += 4;
167
        }
168 744 simons
#else
169
        if ( eth->tx.bytes_sent < eth->tx.packet_length ) {
170 1241 phoenix
            eth->tx_buff[eth->tx.bytes_sent] = eval_direct8(eth->tx.bytes_sent + eth->tx.bd_addr, &breakpoint, 0, 0);
171 744 simons
            eth->tx.bytes_sent += 1;
172
        }
173
#endif
174 702 ivang
        else {
175
            debug (3, "TX - entering state TRANSMIT\n");
176
            eth->tx.state = ETH_TXSTATE_TRANSMIT;
177
        }
178
        break;
179 696 ivang
    case ETH_TXSTATE_TRANSMIT:
180 702 ivang
        /* send packet */
181
        switch (eth->rtx_type) {
182
        case ETH_RTX_FILE:
183
            nwritten = write( eth->txfd, eth->tx_buff, eth->tx.packet_length );
184
            break;
185 867 markom
#if HAVE_ETH_PHY
186 702 ivang
        case ETH_RTX_SOCK:
187
            memset(&sll, 0, sizeof(sll));
188 705 ivang
            sll.sll_ifindex = eth->ifr.ifr_ifindex;
189
            nwritten = sendto(eth->rtx_sock, eth->tx_buff, eth->tx.packet_length, 0, (struct sockaddr *)&sll, sizeof(sll));
190 849 markom
#endif /* HAVE_ETH_PHY */
191 702 ivang
        }
192
 
193
        /* set BD status */
194
        if (nwritten == eth->tx.packet_length) {
195
            CLEAR_FLAG (eth->tx.bd, ETH_TX_BD, READY);
196
            SET_FLAG (eth->regs.int_source, ETH_INT_SOURCE, TXB);
197 836 ivang
            debug (4, "ETH_INT_SOURCE = %0x\n", eth->regs.int_source);
198 702 ivang
 
199 705 ivang
            debug (3, "TX - entering state IDLE\n");
200 702 ivang
            eth->tx.state = ETH_TXSTATE_IDLE;
201
            debug (3, "send (%d)bytes OK\n", nwritten);
202
        }
203
        else {
204
            /* XXX - implement retry mechanism here! */
205
            CLEAR_FLAG (eth->tx.bd, ETH_TX_BD, READY);
206
            CLEAR_FLAG (eth->tx.bd, ETH_TX_BD, COLLISION);
207
            SET_FLAG (eth->regs.int_source, ETH_INT_SOURCE, TXE);
208 889 ivang
            debug (4, "ETH_INT_SOURCE = %0x\n", eth->regs.int_source);
209 702 ivang
 
210 889 ivang
            debug (3, "TX - entering state IDLE\n");
211 702 ivang
            eth->tx.state = ETH_TXSTATE_IDLE;
212
            debug (3, "send FAILED!\n");
213
        }
214
 
215
        eth->regs.bd_ram[eth->tx.bd_index] = eth->tx.bd;
216
 
217 889 ivang
        /* generate OK interrupt */
218
        if ( TEST_FLAG(eth->regs.int_mask, ETH_INT_MASK, TXE_M) ||
219
             TEST_FLAG(eth->regs.int_mask, ETH_INT_MASK, TXB_M) )
220
        {
221
            if ( TEST_FLAG( eth->tx.bd, ETH_TX_BD, IRQ ) )
222
                report_interrupt( eth->mac_int );
223
        }
224
 
225 702 ivang
        /* advance to next BD */
226
        if (bAdvance) {
227
            if ( TEST_FLAG( eth->tx.bd, ETH_TX_BD, WRAP ) ||
228
                            eth->tx.bd_index >= ETH_BD_COUNT )
229
                eth->tx.bd_index = 0;
230
            else
231
                eth->tx.bd_index += 2;
232
        }
233
 
234
        break;
235 696 ivang
    }
236
}
237
/* ========================================================================= */
238
 
239
 
240
/* ========================================================================= */
241 702 ivang
/*  RX LOGIC                                                                 */
242 696 ivang
/*---------------------------------------------------------------------------*/
243
 
244
/*
245
 * RX clock
246
 * Responsible for starting and finishing RX
247
 */
248
void eth_controller_rx_clock( struct eth_device *eth )
249
{
250 702 ivang
    int breakpoint = 0;
251
    long nread;
252
    unsigned long send_word;
253
 
254
 
255 696 ivang
    switch (eth->rx.state) {
256
    case ETH_RXSTATE_IDLE:
257 702 ivang
        if ( TEST_FLAG( eth->regs.moder, ETH_MODER, RXEN) ) {
258 705 ivang
                debug (3, "RX - entering state WAIT4BD (%d)\n", eth->rx.bd_index);
259 702 ivang
            eth->rx.state = ETH_RXSTATE_WAIT4BD;
260
        }
261
        break;
262
 
263 696 ivang
    case ETH_RXSTATE_WAIT4BD:
264 702 ivang
        eth->rx.bd = eth->regs.bd_ram[eth->rx.bd_index];
265
        eth->rx.bd_addr = eth->regs.bd_ram[eth->rx.bd_index + 1];
266
 
267
        if ( TEST_FLAG( eth->rx.bd, ETH_RX_BD, READY ) ) {
268
            /*****************/
269
            /* Initialize RX */
270
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, MISS );
271
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, INVALID );
272
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, DRIBBLE );
273
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, UVERRUN );
274
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, COLLISION );
275
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, TOOBIG );
276
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, TOOSHORT );
277
 
278
            debug( 3,  "Ethernet: Starting RX\n" );
279
 
280
            /* Setup file to read from */
281
            if ( TEST_FLAG( eth->regs.moder, ETH_MODER, LOOPBCK ) ) {
282
                eth->rx.fd = eth->txfd;
283
                eth->rx.offset = &(eth->loopback_offset);
284
            } else {
285
                eth->rx.fd = eth->rxfd;
286
                eth->rx.offset = 0;
287
            }
288 889 ivang
            debug (3, "RX - entering state RECV\n");
289 702 ivang
            eth->rx.state = ETH_RXSTATE_RECV;
290
        }
291 705 ivang
        else if (!TEST_FLAG( eth->regs.moder, ETH_MODER, RXEN)) {
292
          debug (3, "RX - entering state IDLE\n");
293
          eth->rx.state = ETH_RXSTATE_IDLE;
294
        }
295
        else {
296 744 simons
            nread = recv(eth->rtx_sock, eth->rx_buff, ETH_MAXPL, /*MSG_PEEK | */MSG_DONTWAIT);
297 705 ivang
            if (nread > 0) {
298 702 ivang
                SET_FLAG (eth->regs.int_source, ETH_INT_SOURCE, BUSY);
299 723 ivang
                if ( TEST_FLAG(eth->regs.int_mask, ETH_INT_MASK, BUSY_M) )
300
                  report_interrupt(eth->mac_int);
301 702 ivang
            }
302
        }
303
        break;
304
 
305 696 ivang
    case ETH_RXSTATE_RECV:
306 702 ivang
        switch (eth->rtx_type) {
307
        case ETH_RTX_FILE:
308
            /* Read packet length */
309
            if ( eth_read_rx_file( eth, &(eth->rx.packet_length), sizeof(eth->rx.packet_length) )
310
                     < sizeof(eth->rx.packet_length) ) {
311
                /* TODO: just do what real ethernet would do (some kind of error state) */
312 836 ivang
                debug (4, "eth_start_rx(): File does not have a packet ready for RX (len = %d)\n", eth->rx.packet_length );
313 884 markom
                runtime.sim.cont_run = 0;
314 702 ivang
                break;
315
            }
316
 
317
            /* Packet must be big enough to hold a header */
318 1244 hpanther
            if ( eth->rx.packet_length < ETHER_HDR_LEN ){
319 702 ivang
                debug( 3,  "eth_start_rx(): Packet too small\n" );
320
                eth_rx_next_packet( eth );
321
 
322 836 ivang
                debug (3, "RX - entering state IDLE\n");
323 702 ivang
                eth->rx.state = ETH_RXSTATE_IDLE;
324
                break;
325
            }
326
 
327
            eth->rx.bytes_read = 0;
328
            eth->rx.bytes_left = eth->rx.packet_length;
329
 
330
            /* for now Read entire packet into memory */
331
            nread = eth_read_rx_file( eth, eth->rx_buff, eth->rx.bytes_left );
332 844 ivang
            if ( nread < eth->rx.bytes_left ) {
333 702 ivang
                debug (3, "Read %d from %d. Error!\n", nread, eth->rx.bytes_left);
334 844 ivang
                eth->rx.error = 1;
335
                break;
336
            }
337
 
338
            eth->rx.packet_length = nread;
339
            eth->rx.bytes_left = nread;
340
            eth->rx.bytes_read = 0;
341
 
342
            debug (3, "RX - entering state WRITEFIFO\n");
343
            eth->rx.state = ETH_RXSTATE_WRITEFIFO;
344
 
345 702 ivang
            break;
346
 
347
        case ETH_RTX_SOCK:
348
            nread = recv(eth->rtx_sock, eth->rx_buff, ETH_MAXPL, MSG_DONTWAIT);
349 744 simons
 
350
            if (nread == 0)
351
                break;
352
            else if (nread < 0) {
353
                if ( errno != EAGAIN ) {
354 889 ivang
                    debug (3, "recv() FAILED!\n");
355
                    break;
356
                }
357
                else break;
358
            }
359 744 simons
            /* If not promiscouos mode, check the destination address */
360
            if (!TEST_FLAG(eth->regs.moder, ETH_MODER, PRO)) {
361
                if (TEST_FLAG(eth->regs.moder, ETH_MODER, IAM) && (eth->rx_buff[0] & 1)) {
362
                /* Nothing for now */
363
                }
364
 
365
                if (eth->mac_address[5] != eth->rx_buff[0] ||
366
                    eth->mac_address[4] != eth->rx_buff[1] ||
367
                    eth->mac_address[3] != eth->rx_buff[2] ||
368
                    eth->mac_address[2] != eth->rx_buff[3] ||
369
                    eth->mac_address[1] != eth->rx_buff[4] ||
370
                    eth->mac_address[0] != eth->rx_buff[5])
371 889 ivang
                    break;
372 744 simons
            }
373
 
374 841 simons
            eth->rx.packet_length = nread;
375
            eth->rx.bytes_left = nread;
376
            eth->rx.bytes_read = 0;
377
 
378
            debug (3, "RX - entering state WRITEFIFO\n");
379
            eth->rx.state = ETH_RXSTATE_WRITEFIFO;
380
 
381 702 ivang
            break;
382 889 ivang
        case ETH_RTX_VAPI:
383
 
384 702 ivang
        }
385 841 simons
        break;
386
 
387 696 ivang
    case ETH_RXSTATE_WRITEFIFO:
388 744 simons
#if 1
389 702 ivang
        send_word = ((unsigned long)eth->rx_buff[eth->rx.bytes_read]   << 24) |
390
                    ((unsigned long)eth->rx_buff[eth->rx.bytes_read+1] << 16) |
391
                    ((unsigned long)eth->rx_buff[eth->rx.bytes_read+2] << 8)  |
392
                    ((unsigned long)eth->rx_buff[eth->rx.bytes_read+3] );
393 1241 phoenix
        set_direct32( eth->rx.bd_addr + eth->rx.bytes_read, send_word, &breakpoint, 0, 0);
394 702 ivang
        /* update counters */
395
        debug (3, "Write %d, left %d - %08lXd\n", eth->rx.bytes_read, eth->rx.bytes_left, send_word);
396
        eth->rx.bytes_left -= 4;
397
        eth->rx.bytes_read += 4;
398 744 simons
#else
399 1241 phoenix
        set_direct8( eth->rx.bd_addr + eth->rx.bytes_read, eth->rx_buff[eth->rx.bytes_read], &breakpoint, 0, 0);
400 744 simons
        eth->rx.bytes_left -= 1;
401
        eth->rx.bytes_read += 1;
402
#endif
403
 
404 702 ivang
        if ( eth->rx.bytes_left <= 0 ) {
405
            /* Write result to bd */
406
            SET_FIELD( eth->rx.bd, ETH_RX_BD, LENGTH, eth->rx.packet_length );
407
            CLEAR_FLAG( eth->rx.bd, ETH_RX_BD, READY);
408 705 ivang
            SET_FLAG( eth->regs.int_source, ETH_INT_SOURCE, RXB);
409 836 ivang
            debug (4, "ETH_INT_SOURCE = %0x\n", eth->regs.int_source);
410 702 ivang
 
411 1068 simons
            if ( eth->rx.packet_length < (GET_FIELD( eth->regs.packetlen, ETH_PACKETLEN, MINFL ) - 4) )
412 744 simons
                SET_FLAG( eth->rx.bd, ETH_RX_BD, TOOSHORT);
413
            if ( eth->rx.packet_length > GET_FIELD( eth->regs.packetlen, ETH_PACKETLEN, MAXFL ) )
414 702 ivang
                SET_FLAG( eth->rx.bd, ETH_RX_BD, TOOBIG);
415
 
416
            eth->regs.bd_ram[eth->rx.bd_index] = eth->rx.bd;
417
 
418
            /* advance to next BD */
419
            if ( TEST_FLAG( eth->rx.bd, ETH_RX_BD, WRAP ) || eth->rx.bd_index >= ETH_BD_COUNT )
420 1018 simons
                eth->rx.bd_index = eth->regs.tx_bd_num << 1;
421 702 ivang
            else
422 705 ivang
                eth->rx.bd_index += 2;
423 702 ivang
 
424 889 ivang
            if ( ( TEST_FLAG( eth->regs.int_mask, ETH_INT_MASK, RXB_M ) ) &&
425
                 ( TEST_FLAG( eth->rx.bd, ETH_RX_BD, IRQ )              ) ) {
426 702 ivang
                report_interrupt( eth->mac_int );
427
            }
428
 
429
            /* ready to receive next packet */
430
                debug (3, "RX - entering state IDLE\n");
431
            eth->rx.state = ETH_RXSTATE_IDLE;
432
        }
433
        break;
434 696 ivang
    }
435
}
436 702 ivang
 
437 696 ivang
/* ========================================================================= */
438 702 ivang
/* Move to next RX BD */
439
void eth_rx_next_packet( struct eth_device *eth )
440
{
441
    /* Skip any possible leftovers */
442
    if ( eth->rx.bytes_left )
443
        eth_skip_rx_file( eth, eth->rx.bytes_left );
444
}
445
/* "Skip" bytes in RX file */
446
void eth_skip_rx_file( struct eth_device *eth, off_t count )
447
{
448
    eth->rx.offset += count;
449
}
450 696 ivang
 
451 702 ivang
/* Move RX file position back */
452
void eth_rewind_rx_file( struct eth_device *eth, off_t count )
453
{
454
    eth->rx.offset -= count;
455
}
456
/*
457
 * Utility function to read from the ethernet RX file
458
 * This function moves the file pointer to the current place in the packet before reading
459
 */
460
ssize_t eth_read_rx_file( struct eth_device *eth, void *buf, size_t count )
461
{
462
    ssize_t result;
463
 
464
    if ( eth->rx.fd <= 0 ) {
465
        debug( 3,  "Ethernet: No RX file\n" );
466
        return 0;
467
    }
468
 
469
    if ( eth->rx.offset )
470
        if ( lseek( eth->rx.fd, *(eth->rx.offset), SEEK_SET ) == (off_t)-1 ) {
471
            debug( 3,  "Ethernet: Error seeking RX file\n" );
472
            return 0;
473
        }
474 696 ivang
 
475 702 ivang
    result = read( eth->rx.fd, buf, count );
476 836 ivang
    debug (4, "Ethernet: read result = %d \n", result);
477 702 ivang
    if ( eth->rx.offset && result >= 0 )
478
        *(eth->rx.offset) += result;
479
 
480
    return result;
481
}
482
 
483
/* ========================================================================= */
484
 
485 696 ivang
/*
486 702 ivang
  Reset. Initializes all registers to default and places devices in
487
         memory address space.
488 696 ivang
*/
489
void eth_reset()
490
{
491
    static int first_time = 1;
492
    unsigned i;
493
 
494
    if (!config.nethernets)
495 702 ivang
        return;
496 696 ivang
 
497 841 simons
    if ( first_time )
498 702 ivang
        memset( eths, 0, sizeof(eths) );
499 696 ivang
 
500
    for ( i = 0; i < MAX_ETHERNETS; ++ i ) {
501 702 ivang
        struct eth_device *eth = &(eths[i]);
502
 
503 849 markom
        if (!HAVE_ETH_PHY && eth->rtx_type == ETH_RTX_SOCK) {
504
          fprintf (stderr, "Ethernet phy not enabled in this configuration.  Configure with --enable-ethphy.\n");
505
          exit (1);
506
        }
507 702 ivang
        eth->eth_number = i;
508
        eth_reset_controller( eth );
509 841 simons
        if ( eth->baseaddr && first_time )
510 970 simons
            register_memoryarea( eth->baseaddr, ETH_ADDR_SPACE, 4, 0, eth_read32, eth_write32 );
511 696 ivang
    }
512 841 simons
 
513
    if ( first_time )
514
        first_time = 0;
515 696 ivang
}
516 841 simons
 
517 696 ivang
/* ========================================================================= */
518
 
519
 
520 702 ivang
static void eth_reset_controller(struct eth_device *eth)
521
{
522
    int i = eth->eth_number;
523 1308 phoenix
#if HAVE_ETH_PHY
524 702 ivang
    int j;
525
    struct sockaddr_ll sll;
526 849 markom
#endif /* HAVE_ETH_PHY */
527 702 ivang
 
528
    eth->baseaddr = config.ethernets[i].baseaddr;
529
 
530
    if ( eth->baseaddr != 0 ) {
531
        /* Mark which DMA controller and channels */
532
        eth->dma        = config.ethernets[i].dma;
533 725 ivang
        eth->mac_int    = config.ethernets[i].irq;
534 702 ivang
        eth->tx_channel = config.ethernets[i].tx_channel;
535
        eth->rx_channel = config.ethernets[i].rx_channel;
536 725 ivang
        eth->rtx_type   = config.ethernets[i].rtx_type;
537 702 ivang
 
538
        switch (eth->rtx_type) {
539
        case ETH_RTX_FILE:
540
            /* (Re-)open TX/RX files */
541
            eth->rxfile = config.ethernets[i].rxfile;
542
            eth->txfile = config.ethernets[i].txfile;
543
 
544
            if ( eth->rxfd > 0 )
545
                close( eth->rxfd );
546
            if ( eth->txfd > 0 )
547
                close( eth->txfd );
548
            eth->rxfd = eth->txfd = -1;
549
 
550
            if ( (eth->rxfd = open( eth->rxfile, O_RDONLY )) < 0 )
551
                fprintf( stderr, "Cannot open Ethernet RX file \"%s\"\n", eth->rxfile );
552
            if ( (eth->txfd = open( eth->txfile,
553 1244 hpanther
                                    O_RDWR | O_CREAT | O_APPEND
554
 
555
#if defined(O_SYNC)     /* BSD / Mac OS X manual doesn't know about O_SYNC */
556
                                                                        | O_SYNC
557
#endif
558
                                                                        ,
559 702 ivang
                                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH )) < 0 )
560
                fprintf( stderr, "Cannot open Ethernet TX file \"%s\"\n", eth->txfile );
561
            eth->loopback_offset = lseek( eth->txfd, 0, SEEK_END );
562
 
563
            break;
564 867 markom
#if HAVE_ETH_PHY
565 702 ivang
        case ETH_RTX_SOCK:
566
            /* (Re-)open TX/RX sockets */
567
            if (eth->rtx_sock != 0)
568
                break;
569
 
570
            debug (3, "RTX oppening socket...\n");
571
            eth->rtx_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
572
            if (eth->rtx_sock == -1) {
573
                fprintf( stderr, "Cannot open rtx_sock.\n");
574
                return;
575
            }
576
 
577
            /* get interface index number */
578
            debug (3, "RTX getting interface...\n");
579
            memset(&(eth->ifr), 0, sizeof(eth->ifr));
580 725 ivang
            strncpy(eth->ifr.ifr_name, config.ethernets[i].sockif, IFNAMSIZ);
581 702 ivang
            if (ioctl(eth->rtx_sock, SIOCGIFINDEX, &(eth->ifr)) == -1) {
582
                fprintf( stderr, "SIOCGIFINDEX failed!\n");
583
                return;
584
            }
585
            debug (3, "RTX Socket Interface : %d\n", eth->ifr.ifr_ifindex);
586
 
587
            /* Bind to interface... */
588
            debug (3, "Binding to the interface ifindex=%d\n", eth->ifr.ifr_ifindex);
589
            memset(&sll, 0xff, sizeof(sll));
590
            sll.sll_family = AF_PACKET;    /* allways AF_PACKET */
591
            sll.sll_protocol = htons(ETH_P_ALL);
592
            sll.sll_ifindex = eth->ifr.ifr_ifindex;
593
            if (bind(eth->rtx_sock, (struct sockaddr *)&sll, sizeof(sll)) == -1) {
594
                fprintf( stderr, "Error bind().\n");
595
                return;
596
            }
597
 
598
            /* first, flush all received packets. */
599
            debug (3, "Flush");
600
            do {
601
                fd_set fds;
602
                struct timeval t;
603
 
604
                debug( 3, ".");
605
                FD_ZERO(&fds);
606
                FD_SET(eth->rtx_sock, &fds);
607
                memset(&t, 0, sizeof(t));
608
                j = select(FD_SETSIZE, &fds, NULL, NULL, &t);
609
                if (j > 0)
610
                    recv(eth->rtx_sock, eth->rx_buff, j, 0);
611
            } while (j);
612
            debug (3, "\n");
613
 
614
            break;
615 849 markom
#endif /* HAVE_ETH_PHY */
616 702 ivang
        }
617
 
618
        /* Set registers to default values */
619
        memset( &(eth->regs), 0, sizeof(eth->regs) );
620
        eth->regs.moder = 0x0000A000;
621
        eth->regs.ipgt = 0x00000012;
622
        eth->regs.ipgr1 = 0x0000000C;
623
        eth->regs.ipgr2 = 0x00000012;
624
        eth->regs.packetlen = 0x003C0600;
625
        eth->regs.collconf = 0x000F003F;
626
        eth->regs.miimoder = 0x00000064;
627 1018 simons
        eth->regs.tx_bd_num = 0x00000040;
628 702 ivang
 
629
        /* Initialize TX/RX status */
630
        memset( &(eth->tx), 0, sizeof(eth->tx) );
631
        memset( &(eth->rx), 0, sizeof(eth->rx) );
632 1018 simons
        eth->rx.bd_index = eth->regs.tx_bd_num << 1;
633 889 ivang
 
634
        /* Initialize VAPI */
635
        if (config.ethernets[i].base_vapi_id) {
636
            eth->base_vapi_id = config.ethernets[i].base_vapi_id;
637
            vapi_install_multi_handler( eth->base_vapi_id, ETH_NUM_VAPI_IDS, eth_vapi_read );
638
        }
639 702 ivang
    }
640
}
641
/* ========================================================================= */
642
 
643
 
644 696 ivang
/*
645
  Print register values on stdout
646
*/
647
void eth_status( void )
648
{
649
    unsigned i;
650
 
651
    for ( i = 0; i < MAX_ETHERNETS; ++ i ) {
652 702 ivang
        struct eth_device *eth = &(eths[i]);
653
 
654
        if ( eth->baseaddr == 0 )
655
            continue;
656
 
657 1308 phoenix
        PRINTF( "\nEthernet MAC %u at 0x%08lX:\n", i, eth->baseaddr );
658 997 markom
        PRINTF( "MODER        : 0x%08lX\n", eth->regs.moder );
659
        PRINTF( "INT_SOURCE   : 0x%08lX\n", eth->regs.int_source );
660
        PRINTF( "INT_MASK     : 0x%08lX\n", eth->regs.int_mask );
661
        PRINTF( "IPGT         : 0x%08lX\n", eth->regs.ipgt );
662
        PRINTF( "IPGR1        : 0x%08lX\n", eth->regs.ipgr1 );
663
        PRINTF( "IPGR2        : 0x%08lX\n", eth->regs.ipgr2 );
664
        PRINTF( "PACKETLEN    : 0x%08lX\n", eth->regs.packetlen );
665
        PRINTF( "COLLCONF     : 0x%08lX\n", eth->regs.collconf );
666
        PRINTF( "TX_BD_NUM    : 0x%08lX\n", eth->regs.tx_bd_num );
667
        PRINTF( "CTRLMODER    : 0x%08lX\n", eth->regs.controlmoder );
668
        PRINTF( "MIIMODER     : 0x%08lX\n", eth->regs.miimoder );
669
        PRINTF( "MIICOMMAND   : 0x%08lX\n", eth->regs.miicommand );
670
        PRINTF( "MIIADDRESS   : 0x%08lX\n", eth->regs.miiaddress );
671
        PRINTF( "MIITX_DATA   : 0x%08lX\n", eth->regs.miitx_data );
672
        PRINTF( "MIIRX_DATA   : 0x%08lX\n", eth->regs.miirx_data );
673
        PRINTF( "MIISTATUS    : 0x%08lX\n", eth->regs.miistatus );
674
        PRINTF( "MAC Address  : %02X:%02X:%02X:%02X:%02X:%02X\n",
675 702 ivang
                eth->mac_address[0], eth->mac_address[1], eth->mac_address[2],
676
                eth->mac_address[3], eth->mac_address[4], eth->mac_address[5] );
677 997 markom
        PRINTF( "HASH0        : 0x%08lX\n", eth->regs.hash0 );
678
        PRINTF( "HASH1        : 0x%08lX\n", eth->regs.hash1 );
679 696 ivang
    }
680
}
681
/* ========================================================================= */
682
 
683
 
684
/*
685
  Simulation hook. Must be called every clock cycle to simulate Ethernet MAC.
686
*/
687
void eth_clock()
688
{
689
    unsigned i;
690
 
691
    for ( i = 0; i < config.nethernets; ++ i ) {
692 702 ivang
        eth_controller_tx_clock( &(eths[i]) );
693
        eth_controller_rx_clock( &(eths[i]) );
694 696 ivang
    }
695
}
696
/* ========================================================================= */
697
 
698
 
699
/*
700
  Read a register
701
*/
702
unsigned long eth_read32( unsigned long addr )
703
{
704
    struct eth_device *eth;
705 702 ivang
    if ( !eth_find_controller( addr, &eth, &addr ) )    {
706 997 markom
        PRINTF( "eth_read32( 0x%08lX ): Not in registered range(s)\n", addr );
707 702 ivang
        return 0;
708 696 ivang
    }
709
 
710
    switch( addr ) {
711
    case ETH_MODER: return eth->regs.moder;
712
    case ETH_INT_SOURCE: return eth->regs.int_source;
713
    case ETH_INT_MASK: return eth->regs.int_mask;
714
    case ETH_IPGT: return eth->regs.ipgt;
715
    case ETH_IPGR1: return eth->regs.ipgr1;
716
    case ETH_IPGR2: return eth->regs.ipgr2;
717
    case ETH_PACKETLEN: return eth->regs.packetlen;
718
    case ETH_COLLCONF: return eth->regs.collconf;
719
    case ETH_TX_BD_NUM: return eth->regs.tx_bd_num;
720
    case ETH_CTRLMODER: return eth->regs.controlmoder;
721
    case ETH_MIIMODER: return eth->regs.miimoder;
722
    case ETH_MIICOMMAND: return eth->regs.miicommand;
723
    case ETH_MIIADDRESS: return eth->regs.miiaddress;
724
    case ETH_MIITX_DATA: return eth->regs.miitx_data;
725
    case ETH_MIIRX_DATA: return eth->regs.miirx_data;
726
    case ETH_MIISTATUS: return eth->regs.miistatus;
727
    case ETH_MAC_ADDR0: return (((unsigned long)eth->mac_address[3]) << 24) |
728 702 ivang
                               (((unsigned long)eth->mac_address[2]) << 16) |
729
                               (((unsigned long)eth->mac_address[1]) << 8) |
730
                                 (unsigned long)eth->mac_address[0];
731 696 ivang
    case ETH_MAC_ADDR1: return (((unsigned long)eth->mac_address[5]) << 8) |
732 702 ivang
                                 (unsigned long)eth->mac_address[4];
733 744 simons
    case ETH_HASH0: return eth->regs.hash0;
734
    case ETH_HASH1: return eth->regs.hash1;
735 702 ivang
    /*case ETH_DMA_RX_TX: return eth_rx( eth );*/
736 696 ivang
    }
737
 
738
    if ( (addr >= ETH_BD_BASE) && (addr < ETH_BD_BASE + ETH_BD_SPACE) )
739 702 ivang
        return eth->regs.bd_ram[(addr - ETH_BD_BASE) / 4];
740 696 ivang
 
741 997 markom
    PRINTF( "eth_read32( 0x%08lX ): Illegal address\n", addr + eth->baseaddr );
742 884 markom
    runtime.sim.cont_run = 0;
743 696 ivang
    return 0;
744
}
745
/* ========================================================================= */
746
 
747
 
748
/*
749
  Write a register
750
*/
751
void eth_write32( unsigned long addr, unsigned long value )
752
{
753
    struct eth_device *eth;
754 702 ivang
    if ( !eth_find_controller( addr, &eth, &addr ) )    {
755 997 markom
        PRINTF( "eth_write32( 0x%08lX ): Not in registered range(s)\n", addr );
756 702 ivang
    return;
757 696 ivang
    }
758
 
759
    switch( addr ) {
760 841 simons
    case ETH_MODER: eth->regs.moder = value; if (TEST_FLAG(value, ETH_MODER, RST)) eth_reset(); return;
761 744 simons
    case ETH_INT_SOURCE: eth->regs.int_source &= ~value; return;
762 696 ivang
    case ETH_INT_MASK: eth->regs.int_mask = value; return;
763
    case ETH_IPGT: eth->regs.ipgt = value; return;
764
    case ETH_IPGR1: eth->regs.ipgr1 = value; return;
765
    case ETH_IPGR2: eth->regs.ipgr2 = value; return;
766
    case ETH_PACKETLEN: eth->regs.packetlen = value; return;
767
    case ETH_COLLCONF: eth->regs.collconf = value; return;
768
    case ETH_TX_BD_NUM: eth_write_tx_bd_num( eth, value ); return;
769
    case ETH_CTRLMODER: eth->regs.controlmoder = value; return;
770
    case ETH_MIIMODER: eth->regs.miimoder = value; return;
771
    case ETH_MIICOMMAND: eth->regs.miicommand = value; return;
772
    case ETH_MIIADDRESS: eth->regs.miiaddress = value; return;
773
    case ETH_MIITX_DATA: eth->regs.miitx_data = value; return;
774
    case ETH_MIIRX_DATA: eth->regs.miirx_data = value; return;
775
    case ETH_MIISTATUS: eth->regs.miistatus = value; return;
776
    case ETH_MAC_ADDR0:
777 702 ivang
        eth->mac_address[0] = value & 0xFF;
778
        eth->mac_address[1] = (value >> 8) & 0xFF;
779
        eth->mac_address[2] = (value >> 16) & 0xFF;
780
        eth->mac_address[3] = (value >> 24) & 0xFF;
781
        return;
782 696 ivang
    case ETH_MAC_ADDR1:
783 702 ivang
        eth->mac_address[4] = value & 0xFF;
784
        eth->mac_address[5] = (value >> 8) & 0xFF;
785
        return;
786 744 simons
    case ETH_HASH0: eth->regs.hash0 = value; return;
787
    case ETH_HASH1: eth->regs.hash1 = value; return;
788 702 ivang
 
789
    /*case ETH_DMA_RX_TX: eth_tx( eth, value ); return;*/
790 696 ivang
    }
791
 
792
    if ( (addr >= ETH_BD_BASE) && (addr < ETH_BD_BASE + ETH_BD_SPACE) ) {
793 702 ivang
        eth->regs.bd_ram[(addr - ETH_BD_BASE) / 4] = value;
794
        return;
795 696 ivang
    }
796
 
797 997 markom
    PRINTF( "eth_write32( 0x%08lX ): Illegal address\n", addr + eth->baseaddr );
798 884 markom
    runtime.sim.cont_run = 0;
799 696 ivang
    return;
800
}
801
/* ========================================================================= */
802
 
803
 
804 889 ivang
/*
805
 *   VAPI connection to outside
806
 */
807
static void eth_vapi_read (unsigned long id, unsigned long data)
808
{
809
    unsigned long which;
810
    struct eth_device *eth = eth_find_vapi_device( id, &which );
811
 
812
    debug( 5, "ETH: id %08x, data %08x\n", id, data );
813
 
814
    if ( !eth ) {
815
        debug( 1, "ETH: VAPI ID %08x is not ours!\n", id );
816
        return;
817
    }
818
 
819
    switch( which ) {
820
    case ETH_VAPI_DATA:
821
        break;
822
    case ETH_VAPI_CTRL:
823
        break;
824
    }
825
}
826
/* ========================================================================= */
827
 
828
 
829 702 ivang
/* When TX_BD_NUM is written, also reset current RX BD index */
830
void eth_write_tx_bd_num( struct eth_device *eth, unsigned long value )
831
{
832 1018 simons
    eth->regs.tx_bd_num = value & 0xFF;
833
    eth->rx.bd_index = eth->regs.tx_bd_num << 1;
834 702 ivang
}
835
/* ========================================================================= */
836
 
837
 
838 696 ivang
/*
839
  Convert a memory address to a oontroller struct and relative address.
840
  Return nonzero on success
841
*/
842
int eth_find_controller( unsigned long addr, struct eth_device **eth, unsigned long *reladdr )
843
{
844
    unsigned i;
845
    *eth = NULL;
846
 
847
    for ( i = 0; i < MAX_ETHERNETS && *eth == NULL; ++ i ) {
848 702 ivang
        if ( (addr >= eths[i].baseaddr) && (addr < eths[i].baseaddr + ETH_ADDR_SPACE) )
849
            *eth = &(eths[i]);
850
        }
851 696 ivang
 
852
    /* verify we found a controller */
853
    if ( *eth == NULL )
854 702 ivang
        return 0;
855 696 ivang
 
856
    /* Verify legal address */
857
    if ( (addr - (*eth)->baseaddr) % 4 != 0 )
858 702 ivang
        return 0;
859 696 ivang
 
860
    *reladdr = addr - (*eth)->baseaddr;
861
    return 1;
862
}
863 889 ivang
 
864
/*
865
 * Convert VAPI id to controller struct and relative address.
866
 */
867 1244 hpanther
struct eth_device *eth_find_vapi_device( unsigned long id, unsigned long *which )
868 889 ivang
{
869
    unsigned i;
870
 
871
    for ( i=0; i<config.nethernets; i++) {
872
        if ( (id>=eths[i].base_vapi_id) && (id < eths[i].base_vapi_id + ETH_NUM_VAPI_IDS)) {
873
            *which = id - eths[i].base_vapi_id;
874
            return &(eths[i]);
875
        }
876
    }
877
 
878
    return NULL;
879
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.