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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [eth/] [v2_0/] [src/] [net/] [eth_drv.c] - Blame information for rev 27

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/net/eth_drv.c
4
//
5
//      Hardware independent ethernet driver
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
// Copyright (C) 2002 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    gthomas
45
// Contributors: gthomas
46
// Date:         2000-01-10
47
// Purpose:      Hardware independent ethernet driver
48
// Description:  
49
//              
50
//
51
//####DESCRIPTIONEND####
52
//
53
//==========================================================================
54
 
55
// High-level ethernet driver
56
 
57
#include <sys/param.h>
58
#include <sys/errno.h>
59
#include <sys/ioctl.h>
60
#include <sys/mbuf.h>
61
#include <sys/socket.h>
62
 
63
#include <net/if.h>
64
#include <net/if_dl.h>
65
#include <net/if_types.h>
66
#include <net/netisr.h>
67
 
68
#ifdef INET
69
#include <netinet/in.h>
70
#include <netinet/in_systm.h>
71
#include <netinet/in_var.h>
72
#include <netinet/ip.h>
73
#include <netinet/if_ether.h>
74
#endif
75
 
76
#if NBPFILTER > 0
77
#include <net/bpf.h>
78
#include <net/bpfdesc.h>
79
#endif
80
 
81
#include <cyg/infra/cyg_ass.h>
82
#include <cyg/hal/drv_api.h>
83
#include <pkgconf/hal.h>
84
#include <cyg/hal/hal_if.h>
85
#include <pkgconf/io_eth_drivers.h> // module configury; SIMULATED_FAILURES
86
#include <pkgconf/net.h>            // CYGPKG_NET_FAST_THREAD_TICKLE_DEVS?
87
 
88
#include <cyg/io/eth/eth_drv.h>
89
#include <cyg/io/eth/netdev.h>
90
 
91
#ifndef min
92
#define min( _x_, _y_ ) ((_x_) < (_y_) ? (_x_) : (_y_))
93
#endif
94
 
95
// ------------------------------------------------------------------------
96
#ifdef CYGPKG_IO_ETH_DRIVERS_SIMULATED_FAILURES
97
 
98
#define noLOG_RANDOM 32 // so you can tell this is really being random
99
#ifdef LOG_RANDOM
100
static struct {
101
    unsigned int *which;
102
    unsigned int random;
103
    unsigned int r100;
104
} random_log[LOG_RANDOM];
105
 
106
static int random_index = 0;
107
#endif
108
 
109
static unsigned int
110
randomize( unsigned int *p )
111
{
112
    unsigned int r100;
113
    HAL_CLOCK_READ( &r100 );
114
    r100 ^= *p;
115
    *p = (r100 * 1103515245) + 12345;
116
    r100 &= 127;
117
    if ( r100 >= 100 ) // spread the overflow around evenly
118
        r100 = 4 * (r100 - 100);
119
    if ( r100 >= 100 ) // and again - (125,126,127=>100,104,108)
120
        r100 = 12 * (r100 - 100); // =>(0,48,96)
121
#ifdef LOG_RANDOM
122
    random_log[random_index].which  = p;
123
    random_log[random_index].random = *p;
124
    random_log[random_index].r100   = r100;
125
    random_index++;
126
    random_index &= (LOG_RANDOM-1);
127
#endif
128
    return r100;
129
}
130
 
131
#define SIMULATE_FAIL_SEND     1
132
#define SIMULATE_FAIL_RECV     2
133
#define SIMULATE_FAIL_CORRUPT  3
134
 
135
static struct simulated_failure_state {
136
    struct eth_drv_sc *sc;
137
    unsigned int r_tx_fail;
138
    unsigned int r_rx_fail;
139
    unsigned int r_rx_corrupt;
140
    cyg_tick_count_t droptime;
141
    cyg_tick_count_t passtime;
142
} simulated_failure_states[2] = {{0},{0}};
143
 
144
static int
145
simulate_fail( struct eth_drv_sc *sc, int which )
146
{
147
    struct simulated_failure_state *s;
148
 
149
    for ( s = &simulated_failure_states[0]; s < &simulated_failure_states[2];
150
          s++ ) {
151
        if ( 0 == s->sc ) {
152
            s->sc = sc;
153
            s->r_tx_fail    = (unsigned int)sc;
154
            s->r_rx_fail    = (unsigned int)sc ^ 0x01234567;
155
            s->r_rx_corrupt = (unsigned int)sc ^ 0xdeadbeef;
156
            s->droptime = 0;
157
            s->passtime = 0;
158
        }
159
        if ( sc == s->sc )
160
            break;
161
    }
162
    if ( &simulated_failure_states[2] == s ) {
163
        CYG_FAIL( "No free slot in simulated_failure_states[]" );
164
        return 1; // always fail
165
    }
166
 
167
#ifdef CYGPKG_IO_ETH_DRIVERS_SIMULATE_LINE_CUT
168
    // Regardless of the question, we say "yes" during the period of
169
    // unpluggedness...
170
    {
171
        cyg_tick_count_t now = cyg_current_time();
172
        if ( now > s->droptime && 0 == s->passtime ) { // [initial condition]
173
            s->droptime = 0; // go into a passing phase
174
            (void)randomize( &s->r_tx_fail );
175
            (void)randomize( &s->r_rx_fail );
176
            (void)randomize( &s->r_rx_corrupt );
177
            s->passtime = s->r_tx_fail + s->r_rx_fail + s->r_rx_corrupt;
178
            s->passtime &= 0x3fff; // 16k cS is up to 160S, about 2.5 minutes
179
            s->passtime += now;
180
        }
181
        else if ( now > s->passtime && 0 == s->droptime ) {
182
            s->passtime = 0; // go into a dropping phase
183
            (void)randomize( &s->r_tx_fail );
184
            (void)randomize( &s->r_rx_fail );
185
            (void)randomize( &s->r_rx_corrupt );
186
            s->droptime = s->r_tx_fail + s->r_rx_fail + s->r_rx_corrupt;
187
            s->droptime &= 0x0fff; // 4k cS is up to 40S, about 1/2 a minute
188
            s->droptime += now;
189
        }
190
 
191
        if ( now < s->droptime )
192
            return 1; // Say "no"
193
    }
194
#endif
195
 
196
    switch ( which ) {
197
#ifdef CYGPKG_IO_ETH_DRIVERS_SIMULATE_DROP_TX
198
    case SIMULATE_FAIL_SEND: {
199
        unsigned int z = randomize( &s->r_tx_fail );
200
        return z < CYGPKG_IO_ETH_DRIVERS_SIMULATE_DROP_TX;
201
    }
202
#endif
203
#ifdef CYGPKG_IO_ETH_DRIVERS_SIMULATE_DROP_RX
204
    case SIMULATE_FAIL_RECV: {
205
        unsigned int z = randomize( &s->r_rx_fail );
206
        return z < CYGPKG_IO_ETH_DRIVERS_SIMULATE_DROP_RX;
207
    }
208
#endif
209
#ifdef CYGPKG_IO_ETH_DRIVERS_SIMULATE_CORRUPT_RX
210
    case SIMULATE_FAIL_CORRUPT: {
211
        unsigned int z = randomize( &s->r_rx_corrupt );
212
        return z < CYGPKG_IO_ETH_DRIVERS_SIMULATE_CORRUPT_RX;
213
    }
214
#endif
215
    default:
216
        // do nothing - for when options above are not enabled.
217
    }
218
    return 0;
219
}
220
 
221
#define noLOG_CORRUPTION 32 // so you can tell this is really being random
222
#ifdef LOG_CORRUPTION
223
static struct {
224
    int len;
225
    int thislen;
226
    int off;
227
    unsigned char xor;
228
    unsigned char idx;
229
} corruption_log[LOG_CORRUPTION];
230
 
231
static int corruption_index = 0;
232
#endif
233
 
234
static void
235
simulate_fail_corrupt_sglist( struct eth_drv_sg *sg_list, int sg_len )
236
{
237
    unsigned int z, len, i, off;
238
    HAL_CLOCK_READ( &z );
239
    z += simulated_failure_states[0].r_rx_corrupt;
240
    z += simulated_failure_states[1].r_rx_corrupt;
241
 
242
    CYG_ASSERT( MAX_ETH_DRV_SG >= sg_len, "sg_len overflow in corrupt" );
243
 
244
    for ( i = 0, len = 0; i < sg_len && sg_list[i].buf && sg_list[i].len; i++ )
245
        len =+ sg_list[i].len;
246
 
247
    CYG_ASSERT( 1500 >= len, "sg...len > ether MTU" );
248
    if ( 14 >= len ) // normal ether header
249
        return;
250
 
251
    off = z & 2047; // next (2^N-1) > MTU
252
    while ( off > len )
253
        off -= len;
254
 
255
    for ( i = 0; i < sg_len && sg_list[i].buf && sg_list[i].len; i++ ) {
256
        if ( off < sg_list[i].len ) { // corrupt this one
257
            unsigned char *p = (unsigned char *)sg_list[i].buf;
258
            p[off] ^= (0xff & (z >> 11));
259
#ifdef LOG_CORRUPTION
260
            corruption_log[corruption_index].len = len;
261
            corruption_log[corruption_index].thislen = sg_list[i].len;
262
            corruption_log[corruption_index].off = off;
263
            corruption_log[corruption_index].xor = (0xff & (z >> 11));
264
            corruption_log[corruption_index].idx = i;
265
            corruption_index++;
266
            corruption_index &= (LOG_CORRUPTION-1);
267
#endif
268
            return;
269
        }
270
        off -= sg_list[i].len;
271
    }
272
    CYG_FAIL( "Didn't corrupt anything" );
273
}
274
 
275
#endif // CYGPKG_IO_ETH_DRIVERS_SIMULATED_FAILURES
276
// ------------------------------------------------------------------------
277
 
278
#ifdef CYGSEM_HAL_VIRTUAL_VECTOR_SUPPORT
279
 
280
#include <cyg/hal/hal_if.h>
281
 
282
// Use with care!  Local variable defined!
283
#define START_CONSOLE()                                                                 \
284
{   /* NEW BLOCK */                                                                     \
285
    int _cur_console =                                                                  \
286
        CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);      \
287
    {                                                                                   \
288
        int i;                                                                          \
289
        if ( CYGACC_CALL_IF_FLASH_CFG_OP( CYGNUM_CALL_IF_FLASH_CFG_GET,                 \
290
                                          "info_console_force", &i,                     \
291
                                          CYGNUM_FLASH_CFG_OP_CONFIG_BOOL ) ) {         \
292
            if ( i ) {                                                                  \
293
                if ( CYGACC_CALL_IF_FLASH_CFG_OP( CYGNUM_CALL_IF_FLASH_CFG_GET,         \
294
                                                  "info_console_number", &i,            \
295
                                                  CYGNUM_FLASH_CFG_OP_CONFIG_INT ) ) {  \
296
                    /* Then i is the console to force it to: */                         \
297
                    CYGACC_CALL_IF_SET_CONSOLE_COMM( i );                               \
298
                }                                                                       \
299
            }                                                                           \
300
        }                                                                               \
301
    }
302
 
303
#define END_CONSOLE()                                   \
304
    CYGACC_CALL_IF_SET_CONSOLE_COMM(_cur_console);      \
305
}   /* END BLOCK */
306
 
307
#else
308
#define START_CONSOLE()
309
#define END_CONSOLE()
310
#endif
311
// ------------------------------------------------------------------------
312
 
313
#ifdef CYGPKG_NET_FREEBSD_STACK
314
extern char *_ioctl_name(u_long cmd);
315
typedef void void_fun(void *);
316
#endif
317
 
318
static int  eth_drv_ioctl(struct ifnet *, u_long, caddr_t);
319
static void eth_drv_send(struct ifnet *);
320
static void eth_drv_start(struct eth_drv_sc *sc);
321
 
322
#ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG 
323
int cyg_io_eth_net_debug = CYGDBG_IO_ETH_DRIVERS_DEBUG_VERBOSITY;
324
#endif
325
 
326
// Interfaces exported to drivers
327
 
328
static void eth_drv_init(struct eth_drv_sc *sc, unsigned char *enaddr);
329
static void eth_drv_recv(struct eth_drv_sc *sc, int total_len);
330
static void eth_drv_tx_done(struct eth_drv_sc *sc, CYG_ADDRESS key, int status);
331
 
332
struct eth_drv_funs eth_drv_funs = {eth_drv_init, eth_drv_recv, eth_drv_tx_done};
333
 
334
//
335
// This function is called during system initialization to register a
336
// network interface with the system.
337
//
338
static void
339
eth_drv_init(struct eth_drv_sc *sc, unsigned char *enaddr)
340
{
341
    struct ifnet *ifp = &sc->sc_arpcom.ac_if;
342
#ifdef CYGPKG_NET_FREEBSD_STACK
343
    int unit;
344
    char *np, *xp;
345
#endif
346
 
347
    // Set up hardware address
348
    if (NULL != enaddr)
349
        bcopy(enaddr, &sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
350
 
351
    // Initialize ifnet structure
352
    ifp->if_softc = sc;
353
    ifp->if_start = eth_drv_send;
354
    ifp->if_ioctl = eth_drv_ioctl;
355
    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
356
#ifdef IFF_NOTRAILERS
357
    ifp->if_flags |= IFF_NOTRAILERS;
358
#endif
359
#ifdef CYGPKG_NET_FREEBSD_STACK
360
    ifp->if_name = xp = ifp->if_xname;
361
    np = (char *)sc->dev_name;
362
    unit = 0;
363
    while (*np && !((*np >= '0') && (*np <= '9'))) *xp++ = *np++;
364
    if (*np) {
365
        *xp = '\0';
366
        while (*np) {
367
            unit = (unit * 10) + (*np++ - '0');
368
        }
369
        ifp->if_unit = unit;
370
    }
371
    ifp->if_init = (void_fun *)eth_drv_start;
372
    ifp->if_output = ether_output;
373
#else
374
    bcopy((void *)sc->dev_name, ifp->if_xname, IFNAMSIZ);
375
#endif
376
    sc->state = 0;
377
 
378
    // Attach the interface
379
#ifdef CYGPKG_NET_FREEBSD_STACK
380
    ether_ifattach(ifp, 0);
381
#else
382
    if_attach(ifp);
383
    ether_ifattach(ifp);
384
#endif
385
 
386
#ifdef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
387
// Set up interfaces so debug environment can share this device
388
    {
389
        void *dbg = CYGACC_CALL_IF_DBG_DATA();
390
        if (!dbg) {
391
            CYGACC_CALL_IF_DBG_DATA_SET((void *)sc);
392
        }
393
    }
394
#endif
395
}
396
 
397
//
398
// This [internal] function will be called to stop activity on an interface.
399
//
400
static void
401
eth_drv_stop(struct eth_drv_sc *sc)
402
{
403
    (sc->funs->stop)(sc);
404
    sc->state &= ~ETH_DRV_STATE_ACTIVE;
405
}
406
 
407
//
408
// This [internal] function will be called to start activity on an interface.
409
//
410
static void
411
eth_drv_start(struct eth_drv_sc *sc)
412
{
413
    struct ifnet *ifp = &sc->sc_arpcom.ac_if;
414
 
415
    // Perform any hardware initialization
416
    (sc->funs->start)(sc, (unsigned char *)&sc->sc_arpcom.ac_enaddr, 0);
417
#ifdef CYGPKG_NET_FREEBSD_STACK
418
    // resend multicast addresses if present
419
    if(ifp->if_multiaddrs.lh_first && ifp->if_ioctl) {
420
        int s = splimp();
421
        ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
422
        splx(s);
423
    }
424
#endif
425
    // Set 'running' flag, and clear output active flag.
426
    ifp->if_flags |= IFF_RUNNING;
427
    ifp->if_flags &= ~IFF_OACTIVE;
428
    sc->state |= ETH_DRV_STATE_ACTIVE;
429
    eth_drv_send(ifp);  // Try and start up transmit
430
}
431
 
432
//
433
// This function supports "I/O control" operations on an interface.
434
//
435
static int
436
eth_drv_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
437
{
438
    struct eth_drv_sc *sc = ifp->if_softc;
439
#ifndef CYGPKG_NET_FREEBSD_STACK
440
    struct ifaddr *ifa = (struct ifaddr *) data;
441
#endif
442
    struct ifreq *ifr = (struct ifreq *)data;
443
    int     s, error = 0;
444
 
445
// DEBUG
446
#ifdef CYGPKG_NET_FREEBSD_STACK
447
    log(LOG_IOCTL, "%s: cmd: %s, data:\n", __FUNCTION__, _ioctl_name(cmd));
448
    log_dump(LOG_IOCTL, data, 32);
449
#endif
450
// DEBUG
451
 
452
    s = splnet();
453
 
454
#ifdef CYGPKG_NET_FREEBSD_STACK
455
    if ((error = ether_ioctl(ifp, cmd, data)) > 0) {
456
#else
457
    if ((error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data)) > 0) {
458
#endif
459
        splx(s);
460
        return error;
461
    }
462
 
463
    switch (cmd) {
464
 
465
    case SIOCSIFADDR:
466
#ifndef CYGPKG_NET_FREEBSD_STACK // Now in if_ethersubr.c
467
        ifp->if_flags |= IFF_UP;
468
 
469
        switch (ifa->ifa_addr->sa_family) {
470
#ifdef INET
471
        case AF_INET:
472
            eth_drv_start(sc);
473
            arp_ifinit(&sc->sc_arpcom, ifa);
474
            break;
475
#endif
476
        default:
477
            eth_drv_start(sc);
478
            break;
479
        }
480
#endif // CYGPKG_NET_FREEBSD_STACK
481
        break;
482
 
483
    case SIOCGIFHWADDR:
484
        // Get hardware (MAC) address
485
        ifr->ifr_hwaddr.sa_family = AF_INET;
486
        bcopy(&sc->sc_arpcom.ac_enaddr, &ifr->ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
487
        break;
488
 
489
    case SIOCSIFHWADDR:
490
        // Set hardware (MAC) address
491
        bcopy(&ifr->ifr_hwaddr.sa_data, &sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
492
        if ((sc->funs->control)(sc, ETH_DRV_SET_MAC_ADDRESS,
493
                                &sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN)) {
494
            error = EINVAL;
495
        }
496
        break;
497
 
498
#ifdef SIOCGIFSTATS
499
    case SIOCGIFSTATS:
500
#ifdef SIOCGIFSTATSUD
501
    case SIOCGIFSTATSUD:
502
#endif
503
        // Get interface statistics:
504
        if ((sc->funs->control)(sc, (cmd == SIOCGIFSTATS)
505
                                ? ETH_DRV_GET_IF_STATS
506
                                : ETH_DRV_GET_IF_STATS_UD,
507
                                data, 0 ) ) {
508
            error = EINVAL;
509
        }
510
        break;
511
#endif // SIOCGIFSTATS
512
 
513
    case SIOCSIFFLAGS:
514
        if ((ifp->if_flags & IFF_UP) == 0 &&
515
            (ifp->if_flags & IFF_RUNNING) != 0) {
516
            /*
517
             * If interface is marked down and it is running, then
518
             * stop it.
519
             */
520
            eth_drv_stop(sc);
521
            ifp->if_flags &= ~IFF_RUNNING;
522
        } else
523
            if ((ifp->if_flags & IFF_UP) != 0 &&
524
                (ifp->if_flags & IFF_RUNNING) == 0) {
525
                /*
526
                 * If interface is marked up and it is stopped, then
527
                 * start it.
528
                 */
529
                eth_drv_start(sc);
530
            } else {
531
                /*
532
                 * Reset the interface to pick up changes in any other
533
                 * flags that affect hardware registers.
534
                 */
535
                eth_drv_stop(sc);
536
                eth_drv_start(sc);
537
            }
538
        break;
539
 
540
#ifdef CYGPKG_NET_FREEBSD_STACK
541
    case SIOCADDMULTI:
542
    case SIOCDELMULTI:
543
    {
544
        struct ifmultiaddr *ifma;
545
        struct eth_drv_mc_list mc_list;
546
        int mode = (ifp->if_flags & IFF_ALLMULTI) ? ETH_DRV_SET_MC_ALL :
547
                                                    ETH_DRV_SET_MC_LIST;
548
 
549
#ifdef DEBUG
550
        log(LOG_ADDR, "%s Multi\n",(cmd == SIOCADDMULTI) ? "Add" : "Del");
551
#endif
552
        mc_list.len = 0;
553
        LIST_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
554
            if (ifma->ifma_addr->sa_family != AF_LINK) {
555
              continue;
556
            }
557
#ifdef DEBUG
558
            log_dump(LOG_ADDR, LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 6);
559
#endif
560
            if ((LLADDR((struct sockaddr_dl *)ifma->ifma_addr)[0] & 0x01) == 0) {
561
#ifdef DEBUG
562
                log(LOG_ADDR, "** Not a multicast address - ignored\n");
563
#endif
564
                continue;
565
            }
566
            if (mc_list.len < ETH_DRV_MAX_MC) {
567
                bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
568
                      mc_list.addrs[mc_list.len], ETHER_ADDR_LEN);
569
                mc_list.len++;
570
            } else {
571
                mode = ETH_DRV_SET_MC_ALL;
572
            }
573
        }
574
        // Note: drivers may behave like IFF_ALLMULTI if the list is 
575
        // more than their hardware can handle, e.g. some can only handle 1.
576
        if ((sc->funs->control)(sc, mode, &mc_list, sizeof(mc_list))) {
577
            diag_printf( "[%s] Warning: Driver can't set multi-cast mode\n",
578
                         __FUNCTION__ );
579
            error = EINVAL;
580
        }
581
        break;
582
    }
583
#endif
584
 
585
    default:
586
        error = EINVAL;
587
        break;
588
    }
589
 
590
    splx(s);
591
    return (error);
592
}
593
 
594
//
595
// Control whether any special locking needs to take place if we intend to
596
// cooperate with a ROM monitor (e.g. RedBoot) using this hardware.  
597
//
598
#if defined(CYGSEM_HAL_USE_ROM_MONITOR) && \
599
    defined(CYGSEM_HAL_VIRTUAL_VECTOR_DIAG) && \
600
   !defined(CYGSEM_HAL_VIRTUAL_VECTOR_CLAIM_COMMS)
601
 
602
// Indicate that special locking precautions are warranted.
603
#define _LOCK_WITH_ROM_MONITOR
604
 
605
// This defines the [well known] channel that RedBoot will use when it is
606
// using the network hardware for the debug channel.
607
#define RedBoot_TCP_CHANNEL CYGNUM_HAL_VIRTUAL_VECTOR_COMM_CHANNELS
608
 
609
// Define this if you ever need to call 'diag_printf()' from interrupt level
610
// code (ISR) and the debug channel might be using the network hardware. If
611
// this is not the case, then disabling interrupts here is over-kill.
612
//#define _LOCK_USING_INTERRUPTS
613
#endif
614
 
615
//
616
// This routine is called to start transmitting if there is data
617
// available.
618
//
619
static void
620
eth_drv_send(struct ifnet *ifp)
621
{
622
    struct eth_drv_sc *sc = ifp->if_softc;
623
#if MAX_ETH_DRV_SG > 64
624
    static  // Avoid large stack requirements
625
#endif
626
    struct eth_drv_sg sg_list[MAX_ETH_DRV_SG];
627
    int sg_len;
628
    struct mbuf *m0, *m;
629
    int len, total_len;
630
    unsigned char *data;
631
#ifdef _LOCK_WITH_ROM_MONITOR
632
#ifdef _LOCK_USING_INTERRUPTS
633
    cyg_uint32 ints;
634
#endif
635
    bool need_lock = false;
636
    int debug_chan;
637
#endif // _LOCK_WITH_ROM_MONITOR
638
 
639
    // This is now only called from network threads, so no guarding is
640
    // required; locking is in place via the splfoo() mechanism already.
641
 
642
    if ((ifp->if_flags & IFF_RUNNING) != IFF_RUNNING) {
643
         return;
644
    }
645
 
646
    while ((sc->funs->can_send)(sc) > 0) {
647
        IF_DEQUEUE(&ifp->if_snd, m0);
648
        if (m0 == 0) {
649
            break;
650
        }
651
 
652
#ifdef CYGPKG_IO_ETH_DRIVERS_SIMULATED_FAILURES
653
        if ( simulate_fail( sc, SIMULATE_FAIL_SEND ) ) {
654
            // must free the mbufs
655
            m_freem(m0);
656
            continue; // next packet to send
657
        }
658
#endif
659
 
660
#ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG
661
        if (cyg_io_eth_net_debug) {
662
            START_CONSOLE();
663
            diag_printf("Sending %d bytes\n", m0->m_pkthdr.len);
664
            END_CONSOLE();
665
        }
666
#endif
667
 
668
        /* We need to use m->m_pkthdr.len, so require the header */
669
        if ((m0->m_flags & M_PKTHDR) == 0)
670
            panic("eth_drv_send: no header mbuf");
671
 
672
#if NBPFILTER > 0
673
        /* Tap off here if there is a BPF listener. */
674
        if (ifp->if_bpf)
675
            bpf_mtap(ifp->if_bpf, m0);
676
#endif
677
 
678
        // Extract data pointers (don't actually move data here)
679
        sg_len = 0;  total_len = 0;
680
        for (m = m0; m ; m = m->m_next) {
681
            data = mtod(m, u_char *);
682
            len = m->m_len;
683
            total_len += len;
684
            sg_list[sg_len].buf = (CYG_ADDRESS)data;
685
            sg_list[sg_len].len = len;
686
            if ( len )
687
                sg_len++;
688
#ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG
689
            if (cyg_io_eth_net_debug) {
690
                START_CONSOLE();
691
                diag_printf("xmit %d bytes at %x sg[%d]\n", len, data, sg_len);
692
                if ( cyg_io_eth_net_debug > 1)
693
                    diag_dump_buf(data, len);
694
                END_CONSOLE();
695
            }
696
#endif
697
            if ( MAX_ETH_DRV_SG < sg_len ) {
698
#ifdef CYGPKG_IO_ETH_DRIVERS_WARN_NO_MBUFS
699
                int needed = 0;
700
                struct mbuf *m1;
701
                for (m1 = m0; m1 ; m1 = m1->m_next) needed++;
702
                START_CONSOLE();
703
                diag_printf("too many mbufs to tx, %d > %d, need %d\n",
704
                            sg_len, MAX_ETH_DRV_SG, needed );
705
                END_CONSOLE();
706
#endif
707
                sg_len = 0;
708
                break; // drop it on the floor
709
            }
710
        }
711
 
712
#ifdef _LOCK_WITH_ROM_MONITOR
713
        // Firm lock on this portion of the driver.  Since we are about to
714
        // start messing with the actual hardware, it is imperative that the
715
        // current thread not loose control of the CPU at this time.  Otherwise,
716
        // the hardware could be left in an unusable state.  This caution is
717
        // only warranted if there is a possibility of some other thread trying
718
        // to use the hardware simultaneously.  The network stack would prevent
719
        // this implicitly since all accesses are controlled by the "splX()"
720
        // locks, but if there is a ROM monitor, such as RedBoot, also using
721
        // the hardware, all bets are off.
722
 
723
        // Note: these operations can be avoided if it were well known that
724
        // RedBoot was not using the network hardware for diagnostic I/O.  This
725
        // can be inferred by checking which I/O channel RedBoot is currently
726
        // hooked to.
727
        debug_chan = CYGACC_CALL_IF_SET_DEBUG_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
728
        if (debug_chan == RedBoot_TCP_CHANNEL) {
729
            need_lock = true;
730
#ifdef _LOCK_USING_INTERRUPTS
731
            HAL_DISABLE_INTERRUPTS(ints);
732
#endif
733
            cyg_drv_dsr_lock();
734
        }
735
#endif // _LOCK_WITH_ROM_MONITOR
736
 
737
        // Tell hardware to send this packet
738
        if ( sg_len )
739
            (sc->funs->send)(sc, sg_list, sg_len, total_len, (unsigned long)m0);
740
 
741
#ifdef _LOCK_WITH_ROM_MONITOR
742
        // Unlock the driver & hardware.  It can once again be safely shared.
743
        if (need_lock) {
744
            cyg_drv_dsr_unlock();
745
#ifdef _LOCK_USING_INTERRUPTS
746
            HAL_RESTORE_INTERRUPTS(ints);
747
#endif
748
        }
749
#endif // _LOCK_WITH_ROM_MONITOR
750
#undef _LOCK_WITH_ROM_MONITOR
751
    }
752
}
753
 
754
//
755
// This function is called from the hardware driver when an output operation
756
// has completed - i.e. the packet has been sent.
757
//
758
static struct mbuf *mbuf_key;
759
 
760
static void
761
eth_drv_tx_done(struct eth_drv_sc *sc, CYG_ADDRESS key, int status)
762
{
763
    struct ifnet *ifp = &sc->sc_arpcom.ac_if;
764
    struct mbuf *m0 = (struct mbuf *)key;
765
    CYGARC_HAL_SAVE_GP();
766
 
767
    // Check for errors here (via 'status')
768
    ifp->if_opackets++;
769
    // Done with packet
770
 
771
    // Guard against a NULL return - can be caused by race conditions in
772
    // the driver, this is the neatest fixup:
773
    if (m0) {
774
        mbuf_key = m0;
775
        m_freem(m0);
776
    }
777
    // Start another if possible
778
    eth_drv_send(ifp);
779
    CYGARC_HAL_RESTORE_GP();
780
}
781
 
782
//
783
// This function is called from a hardware driver to indicate that an input
784
// packet has arrived.  The routine will set up appropriate network resources
785
// (mbuf's) to hold the data and call back into the driver to retrieve the data.
786
//
787
static void
788
eth_drv_recv(struct eth_drv_sc *sc, int total_len)
789
{
790
    struct ifnet *ifp = &sc->sc_arpcom.ac_if;
791
    struct ether_header _eh, *eh=&_eh;
792
    struct mbuf *top, **mp, *m;
793
    int mlen;
794
    unsigned char *data;
795
#if MAX_ETH_DRV_SG > 64
796
    static  // Avoid large stack requirements
797
#endif
798
    struct eth_drv_sg sg_list[MAX_ETH_DRV_SG];
799
    int sg_len;
800
 
801
    if ((ifp->if_flags & IFF_RUNNING) != IFF_RUNNING) {
802
        return;  // Interface not up, ignore this request
803
    }
804
 
805
    CYG_ASSERT( 0 != total_len, "total_len is zero!" );
806
    CYG_ASSERT( 0 <= total_len, "total_len is negative!" );
807
    CYG_ASSERT( sizeof( struct ether_header ) <= total_len,
808
                "No ether header here!" );
809
 
810
    if ( total_len < sizeof( struct ether_header ) )
811
        // Our arithmetic below would go wrong
812
        return;
813
 
814
    CYGARC_HAL_SAVE_GP();  // This is down here to make matching restore neat
815
 
816
    /* Pull packet off interface. */
817
    MGETHDR(m, M_DONTWAIT, MT_DATA);
818
    if (m == 0) {
819
#ifdef CYGPKG_IO_ETH_DRIVERS_WARN_NO_MBUFS
820
        START_CONSOLE();
821
        diag_printf("warning: eth_recv out of MBUFs\n");
822
        END_CONSOLE();
823
#endif
824
    }
825
 
826
    // Set up buffers
827
    // Unload ethernet header separately so IP/UDP/TCP headers are aligned
828
    sg_list[0].buf = (CYG_ADDRESS)eh;
829
    sg_list[0].len = sizeof(*eh);
830
    sg_len = 1;
831
 
832
    // Compute total length (minus ethernet header)
833
    total_len -= sizeof(*eh);
834
 
835
    top = 0;
836
    mlen = MHLEN;
837
    mp = &top;
838
 
839
    if (m) {
840
        m->m_pkthdr.rcvif = ifp;
841
        m->m_pkthdr.len = total_len;
842
    } else {
843
        sg_list[sg_len].buf = (CYG_ADDRESS)0;
844
        sg_list[sg_len].len = total_len;
845
        sg_len++;
846
        total_len = 0;
847
    }
848
 
849
    while (total_len > 0) {
850
        if (top) {
851
            MGET(m, M_DONTWAIT, MT_DATA);
852
            if (m == 0) {
853
                m_freem(top);
854
#ifdef CYGPKG_IO_ETH_DRIVERS_WARN_NO_MBUFS
855
                START_CONSOLE();
856
                diag_printf("out of MBUFs [2]");
857
                END_CONSOLE();
858
#endif
859
                sg_list[sg_len].buf = (CYG_ADDRESS)0;
860
                sg_list[sg_len].len = total_len;
861
                sg_len++;
862
                top = 0;
863
                break;
864
            }
865
            mlen = MLEN;
866
        }
867
        if (total_len >= MINCLSIZE) {
868
            MCLGET(m, M_DONTWAIT);
869
            if ((m->m_flags & M_EXT) == 0) {
870
                m_freem(top);
871
                m_free(m);
872
#ifdef CYGPKG_IO_ETH_DRIVERS_WARN_NO_MBUFS
873
                START_CONSOLE();
874
                diag_printf("warning: eth_recv out of MBUFs\n");
875
                END_CONSOLE();
876
#endif
877
                sg_list[sg_len].buf = (CYG_ADDRESS)0;
878
                sg_list[sg_len].len = total_len;
879
                sg_len++;
880
                top = 0;
881
                break;
882
            }
883
            mlen = MCLBYTES;
884
        }
885
        m->m_len = mlen = min(total_len, mlen);
886
        total_len -= mlen;
887
        data = mtod(m, caddr_t);
888
        sg_list[sg_len].buf = (CYG_ADDRESS)data;
889
        sg_list[sg_len].len = mlen;
890
        sg_len++;
891
        *mp = m;
892
        mp = &m->m_next;
893
    } // endwhile
894
 
895
    // Ask hardware to unload buffers
896
    (sc->funs->recv)(sc, sg_list, sg_len);
897
 
898
#ifdef CYGPKG_IO_ETH_DRIVERS_SIMULATED_FAILURES
899
    if ( simulate_fail( sc, SIMULATE_FAIL_RECV ) ) {
900
        // toss the packet - note that some hardware gets
901
        // fussy if the packet isn't "unloaded", thus we
902
        // have to wait until now to throw it away
903
        if (top) {
904
            m_free(top);
905
        }
906
        ifp->if_ierrors++;
907
        return;
908
    }
909
 
910
    if ( simulate_fail( sc, SIMULATE_FAIL_CORRUPT ) ) {
911
        // Corrupt the data
912
        simulate_fail_corrupt_sglist( sg_list, sg_len );
913
    }
914
#endif
915
 
916
#ifdef CYGDBG_IO_ETH_DRIVERS_DEBUG
917
    if (cyg_io_eth_net_debug) {
918
        int i;
919
        START_CONSOLE();
920
        for (i = 0;  i < sg_len;  i++) {
921
            if (sg_list[i].buf) {
922
                diag_printf("rx %d bytes at %x sg[%d]\n", sg_list[i].len, sg_list[i].buf, i);
923
                if ( cyg_io_eth_net_debug > 1 )
924
                    diag_dump_buf((void *)sg_list[i].buf, sg_list[i].len);
925
            }
926
        }
927
        END_CONSOLE();
928
    }
929
#endif
930
    m = top;
931
    if (m == 0) {
932
        ifp->if_ierrors++;
933
    }
934
    else {
935
        ifp->if_ipackets++;
936
 
937
#if NBPFILTER > 0
938
#error FIXME - Need mbuf with ethernet header attached
939
        /*
940
         * Check if there's a BPF listener on this interface.
941
         * If so, hand off the raw packet to bpf.
942
         */
943
        if (ifp->if_bpf)
944
            bpf_mtap(ifp->if_bpf, m);
945
#endif
946
 
947
        // Push data into protocol stacks
948
        ether_input(ifp, eh, m);
949
    }
950
    CYGARC_HAL_RESTORE_GP();
951
}
952
 
953
 
954
// ------------------------------------------------------------------------
955
// DSR to schedule network delivery thread
956
 
957
extern void ecos_synch_eth_drv_dsr(void); // from ecos/timeout.c in net stack
958
 
959
void
960
eth_drv_dsr(cyg_vector_t vector,
961
            cyg_ucount32 count,
962
            cyg_addrword_t data)
963
{
964
    struct eth_drv_sc *sc = (struct eth_drv_sc *)data;
965
 
966
#ifdef CYGDBG_USE_ASSERTS
967
    // then check that this really is a "sc"
968
    {
969
        cyg_netdevtab_entry_t *t;
970
        for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++)
971
            if ( ((struct eth_drv_sc *)t->device_instance) == sc )
972
                break; // found it
973
        CYG_ASSERT( t != &__NETDEVTAB_END__, "eth_drv_dsr: Failed to find sc in NETDEVTAB" );
974
    }
975
#endif // Checking code
976
 
977
    sc->state |= ETH_DRV_NEEDS_DELIVERY;
978
 
979
    ecos_synch_eth_drv_dsr(); // [request] run delivery function for this dev
980
}
981
 
982
// This is called from the delivery thread, to do just that:
983
void eth_drv_run_deliveries( void )
984
{
985
    cyg_netdevtab_entry_t *t;
986
    for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
987
        struct eth_drv_sc *sc = (struct eth_drv_sc *)t->device_instance;
988
        if ( ETH_DRV_NEEDS_DELIVERY & sc->state ) {
989
#if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
990
            cyg_bool was_ctrlc_int;
991
#endif
992
            sc->state &=~ETH_DRV_NEEDS_DELIVERY;
993
#if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
994
            was_ctrlc_int = HAL_CTRLC_CHECK((*sc->funs->int_vector)(sc), (int)sc);
995
            if (!was_ctrlc_int) // Fall through and run normal code
996
#endif
997
            (*sc->funs->deliver)(sc);
998
        }
999
    }
1000
}
1001
 
1002
// This is called from the delivery thread, to unstick devices if there is
1003
// no network activity.
1004
#ifdef CYGPKG_NET_FAST_THREAD_TICKLE_DEVS
1005
void eth_drv_tickle_devices( void )
1006
{
1007
    cyg_netdevtab_entry_t *t;
1008
    for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
1009
        struct eth_drv_sc *sc = (struct eth_drv_sc *)t->device_instance;
1010
        if ( ETH_DRV_STATE_ACTIVE & sc->state ) {
1011
            struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1012
            // Try to dequeue a packet for this interface, if we can.  This
1013
            // will call can_send() for active interfaces.  It is calls to
1014
            // this function from tx_done() which normally provide
1015
            // continuous transmissions; otherwise we do not get control.
1016
            // This call fixes that.
1017
            eth_drv_send(ifp);
1018
        }
1019
    }
1020
}
1021
#endif // CYGPKG_NET_FAST_THREAD_TICKLE_DEVS
1022
 
1023
// ------------------------------------------------------------------------
1024
 
1025
#ifdef CYGPKG_IO_PCMCIA
1026
// Lookup a 'netdev' entry, assuming that it is an ethernet device.
1027
cyg_netdevtab_entry_t *
1028
eth_drv_netdev(char *name)
1029
{
1030
    cyg_netdevtab_entry_t *t;
1031
    struct eth_drv_sc *sc;
1032
    for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
1033
        sc = (struct eth_drv_sc *)t->device_instance;
1034
        if (strcmp(sc->dev_name, name) == 0) {
1035
            return t;
1036
        }
1037
    }
1038
    return (cyg_netdevtab_entry_t *)NULL;
1039
}
1040
#endif // CYGPKG_IO_PCMCIA
1041
 
1042
// EOF src/net/eth_drv.c

powered by: WebSVN 2.1.0

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