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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [eth/] [synth/] [ecosynth/] [current/] [src/] [syntheth.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      syntheth.c
4
//
5
//      Network device driver for the synthetic target
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 2002, 2003 Free Software Foundation, Inc.                  
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY 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        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    bartv
43
// Contributors: bartv
44
// Date:         2002-08-07
45
//
46
//####DESCRIPTIONEND####
47
//==========================================================================
48
 
49
#include <pkgconf/devs_eth_ecosynth.h>
50
 
51
#include <cyg/infra/cyg_type.h>
52
#include <cyg/infra/cyg_ass.h>
53
#include <cyg/hal/hal_arch.h>
54
#include <cyg/infra/diag.h>
55
#include <cyg/hal/drv_api.h>
56
#include <errno.h>
57
#include <string.h>
58
 
59
#define __ECOS 1
60
#include <sys/types.h>
61
#include <cyg/io/eth/netdev.h>
62
#include <cyg/io/eth/eth_drv.h>
63
#include <cyg/io/eth/eth_drv_stats.h>
64
 
65
#ifdef CYGPKG_NET
66
# include <net/if.h>
67
#else
68
# define IFF_PROMISC 0
69
#endif
70
 
71
#include <cyg/hal/hal_io.h>
72
#include "protocol.h"
73
 
74
// ----------------------------------------------------------------------------
75
// Device instances. The synthetic target ethernet package can support
76
// up to four ethernet devices, eth0 to eth3. A synth_eth structure
77
// holds the data that is specific to a given device. Each device
78
// needs an instance of this structure, followed by ETH_DRV_SC and
79
// NETDEVTAB_ENTRY macros. Another macro SYNTH_ETH_INSTANCE takes
80
// care of all that, to avoid unnecessary duplication of code here.
81
//
82
// NOTE: unfortunately this involves duplicating the eth_hwr_funs
83
// structure. This could be eliminated but it would require bypassing
84
// the ETH_DRV_SC macro.
85
 
86
#define ETHERNET_MINTU 14
87
#define ETHERNET_MAXTU 1514
88
 
89
 
90
typedef struct synth_eth {
91
    int             synth_id;           // Device id within the auxiliary
92
    int             up;                 // Has there been a call to start()?
93
    int             in_send;            // Detect recursive calls
94
    int             tx_done;
95
    unsigned long   tx_key;             // Allow mbuf's to be freed
96
    volatile int    rx_pending;         // There is pending data.
97
    int             rx_len;             // Length of buffered data.
98
    unsigned char   MAC[6];             // Obtained from the underlying ethernet device
99
    cyg_vector_t    interrupt;          // Interrupt number allocated by the auxiliary
100
    int             multi_supported;    // Does the driver support multicasting?
101
    cyg_handle_t    interrupt_handle;   // Allow the ISR and DSR to be installed.
102
    cyg_interrupt   interrupt_data;
103
    unsigned char   tx_data[ETHERNET_MAXTU];
104
    unsigned char   rx_data[ETHERNET_MAXTU];
105
} synth_eth;
106
 
107
#define SYNTH_ETH_INSTANCE( _number_)           \
108
static synth_eth synth_eth##_number_ = {        \
109
    synth_id:   -1,                             \
110
    up:          1,                             \
111
    in_send:     0,                             \
112
    tx_done:     0,                             \
113
    tx_key:      0L,                            \
114
    rx_pending:  0,                             \
115
    rx_len:      0,                             \
116
    MAC:         { 0, 0, 0, 0, 0, 0 },          \
117
    interrupt:   0,                             \
118
    interrupt_handle: 0                         \
119
};                                              \
120
ETH_DRV_SC(synth_eth_sc##_number_,              \
121
           (void*) &synth_eth##_number_,        \
122
           "eth" #_number_,                     \
123
           synth_eth_start,                     \
124
           synth_eth_stop,                      \
125
           synth_eth_ioctl,                     \
126
           synth_eth_can_send,                  \
127
           synth_eth_send,                      \
128
           synth_eth_recv,                      \
129
           synth_eth_deliver,                   \
130
           synth_eth_poll,                      \
131
           synth_eth_intvector);                \
132
NETDEVTAB_ENTRY(synth_eth_netdev##_number_,     \
133
                "synth_eth" #_number_,          \
134
                synth_eth_init,                 \
135
                &synth_eth_sc##_number_);
136
 
137
#ifdef CYGVAR_DEVS_ETH_ECOSYNTH_ETH0
138
SYNTH_ETH_INSTANCE(0);
139
#endif
140
#ifdef CYGVAR_DEVS_ETH_ECOSYNTH_ETH1
141
SYNTH_ETH_INSTANCE(1);
142
#endif
143
#ifdef CYGVAR_DEVS_ETH_ECOSYNTH_ETH2
144
SYNTH_ETH_INSTANCE(2);
145
#endif
146
#ifdef CYGVAR_DEVS_ETH_ECOSYNTH_ETH3
147
SYNTH_ETH_INSTANCE(3);
148
#endif
149
 
150
// ----------------------------------------------------------------------------
151
// Data transmits.
152
//
153
// The eCos application will just send the data to the auxiliary,
154
// which will in turn pass it on to the rawether utility. There is no
155
// need for any response. Flow control is implicit: if the eCos
156
// application tries to send ethernet packets too quickly then those
157
// get passed on to the auxiliary, which in turn will pass them on to
158
// the rawether process. If rawether is still busy with the previous
159
// packet then the auxiliary will block on a pipe write, and in turn
160
// the eCos application will block. As long as rawether manages to
161
// complete its operations reasonably quickly these blocks should not
162
// be noticeable to the user.
163
//
164
// So can_send() should always return true for an interface that is up
165
// and running. The send operation needs to take the sg list, turn it
166
// into a single buffer, and transmit it to the auxiliary. At that
167
// point the transmission is already complete so eth_drv_dsr() should
168
// be called to call deliver() and release the buffer.
169
//
170
// However there are some complications. The first is polled operation,
171
// where eth_drv_dsr() is a no-op and should not really be called at
172
// all because there are no interrupts going off. The second is that
173
// calling eth_drv_dsr() directly will cause recursive operation:
174
// send() -> dsr -> can_send()/send() -> ...
175
// This is a bad idea, so can_send() has to check that we are not
176
// already inside a send(). Data transmission will proceed merrily
177
// once the send has returned.
178
 
179
static int
180
synth_eth_can_send(struct eth_drv_sc* sc)
181
{
182
    synth_eth*  eth = (synth_eth*)(sc->driver_private);
183
    return synth_auxiliary_running && eth->up && !eth->in_send && !eth->tx_done;
184
}
185
 
186
static void
187
synth_eth_send(struct eth_drv_sc* sc,
188
               struct eth_drv_sg* sg_list, int sg_len, int total_len,
189
               unsigned long key)
190
{
191
    synth_eth*  eth = (synth_eth*)(sc->driver_private);
192
 
193
    CYG_PRECONDITION((total_len >= ETHERNET_MINTU) && (total_len <= ETHERNET_MAXTU), "Only normal-sized ethernet packets are supported");
194
    CYG_PRECONDITION(!eth->in_send && !eth->tx_done, "Ethernet device must not still be in use for transmits");
195
 
196
    eth->in_send = 1;
197
    eth->tx_key  = key;
198
    if (synth_auxiliary_running && eth->up) {
199
        int i;
200
        unsigned char* buf = eth->tx_data;
201
        for (i = 0; i < sg_len; i++) {
202
            memcpy(buf, (void*) sg_list[i].buf, sg_list[i].len);
203
            buf += sg_list[i].len;
204
            CYG_LOOP_INVARIANT(buf <= &(eth->tx_data[ETHERNET_MAXTU]), "sg list must not exceed ethernet MTU");
205
        }
206
        CYG_POSTCONDITION(buf == &(eth->tx_data[total_len]), "sg list lengths should match total_len");
207
 
208
        synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_TX, 0, 0, eth->tx_data, total_len,
209
                                (void*) 0, (unsigned char*)0, (int*)0, 0);
210
    }
211
 
212
    // The transfer has now completed, one way or another, so inform
213
    // the higher-level code immediately.
214
    eth->tx_done = 1;
215
    eth_drv_dsr(eth->interrupt, 0, (cyg_addrword_t) sc);
216
    eth->in_send = 0;
217
}
218
 
219
// ----------------------------------------------------------------------------
220
// Receives.
221
//
222
// These are rather more complicated because there are real interrupts
223
// involved, and polling needs to be supported as well. The actual
224
// transfer of data from auxiliary to eCos happens inside deliver(),
225
// and the data is buffered up in the synth_eth structure. All that
226
// needs to be done here is scatter the existing data into the
227
// sg_list. If higher-level code has run out of space then the
228
// sg_list may contain null pointers.
229
 
230
static void
231
synth_eth_recv(struct eth_drv_sc* sc, struct eth_drv_sg* sg_list, int sg_len)
232
{
233
    synth_eth*      eth     = (synth_eth*)(sc->driver_private);
234
    unsigned char*  buf     = eth->rx_data;
235
    int             len     = eth->rx_len;
236
    int             i;
237
 
238
    for (i = 0; i < sg_len; i++) {
239
        if (0 == sg_list[i].buf) {
240
            break;
241
        } else if (len <= sg_list[i].len) {
242
            memcpy((void*)sg_list[i].buf, buf, len);
243
            break;
244
        } else {
245
            memcpy((void*)sg_list[i].buf, buf, sg_list[i].len);
246
            buf += sg_list[i].len;
247
            len -= sg_list[i].len;
248
        }
249
    }
250
}
251
 
252
// The ISR does not have to do anything, the DSR does the real work.
253
static cyg_uint32
254
synth_eth_isr(cyg_vector_t vector, cyg_addrword_t data)
255
{
256
    cyg_drv_interrupt_acknowledge(vector);
257
    return CYG_ISR_CALL_DSR;
258
}
259
 
260
// The DSR also does not have to do very much. The data argument is
261
// actually the eth_drv_sc structure, which must match the vector.
262
// Interrupts only go off when there are pending receives, so set the
263
// rx_pending flag and call the generic DSR to do the real work.
264
static void
265
synth_eth_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
266
{
267
    struct eth_drv_sc* sc = (struct eth_drv_sc*) data;
268
    synth_eth* eth        = (synth_eth*)(sc->driver_private);
269
    CYG_ASSERT(eth->interrupt == vector, "Interrupt vectors cannot change during a run");
270
 
271
    eth->rx_pending = 1;
272
    eth_drv_dsr(vector, count, data);
273
}
274
 
275
// ----------------------------------------------------------------------------
276
// Delivery. This is invoked by a thread inside the TCP/IP stack, or by
277
// the poll function.
278
static void
279
synth_eth_deliver(struct eth_drv_sc* sc)
280
{
281
    synth_eth* eth = (synth_eth*)(sc->driver_private);
282
 
283
    if (eth->tx_done) {
284
        eth->tx_done = 0;
285
        (*sc->funs->eth_drv->tx_done)(sc, eth->tx_key, 1);
286
    }
287
    while (eth->rx_pending) {
288
        int more = 1;
289
        eth->rx_pending = 0;
290
 
291
        while (more && eth->up && synth_auxiliary_running) {
292
            synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_RX, 0, 0, (void*) 0, 0,
293
                                    &more, eth->rx_data, &(eth->rx_len), ETHERNET_MAXTU);
294
            CYG_LOOP_INVARIANT(!more || (0 != eth->rx_len), "Auxiliary must send at least one packet if several are available");
295
            if (eth->rx_len > 0) {
296
                CYG_ASSERT((eth->rx_len >= ETHERNET_MINTU) && (eth->rx_len <= ETHERNET_MAXTU), "Only normal-sized ethernet packets are supported");
297
                // Inform higher-level code that data is available.
298
                // This should result in a call to recv() with a
299
                // suitable sg_list. If out of memory, recv()
300
                // will see a null pointer.
301
                (*sc->funs->eth_drv->recv)(sc, eth->rx_len);
302
            }
303
        };
304
    }
305
}
306
 
307
 
308
// ----------------------------------------------------------------------------
309
// Polling support. Transmits are relatively straightforward because
310
// all the hard work is handled by send(). Receives are rather more
311
// complicated because interrupts are disabled so we never know when
312
// there is really pending data. However deliver() will do the right
313
// thing even if there is no data, so simply faking up an interrupt
314
// is enough. This does mean extra traffic between application and
315
// auxiliary, but polling does rather imply that.
316
static void
317
synth_eth_poll(struct eth_drv_sc* sc)
318
{
319
    synth_eth* eth = (synth_eth*)(sc->driver_private);
320
    if (synth_auxiliary_running && eth->up) {
321
        eth->rx_pending = 1;
322
    }
323
    synth_eth_deliver(sc);
324
}
325
 
326
static int
327
synth_eth_intvector(struct eth_drv_sc* sc)
328
{
329
    synth_eth* eth = (synth_eth*)(sc->driver_private);
330
    return eth->interrupt;
331
}
332
 
333
// ----------------------------------------------------------------------------
334
// ioctl()'s.
335
//
336
// SET_MAC_ADDRESS is not currently implemented, and probably should
337
// not be implemented because the underlying ethernet device may not
338
// support it.
339
//
340
// SET_MC_ALL is supported if the underlying hardware does. This is
341
// needed for IPV6 support. More selective multicasting via
342
// SET_MC_LIST is not supported, because it imposes too heavy a
343
// requirement on the underlying Linux device. SET_MC_LIST can be
344
// used to disable multicast support.
345
//
346
// GET_IF_STATS_UD and GET_IF_STATS are not currently implemented
347
static int
348
synth_eth_ioctl(struct eth_drv_sc* sc, unsigned long key, void* data, int data_length)
349
{
350
    synth_eth* eth = (synth_eth*)(sc->driver_private);
351
    int result = EINVAL;
352
 
353
    switch(key) {
354
      case ETH_DRV_SET_MC_ALL:
355
        {
356
            if (eth->multi_supported) {
357
                synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_MULTIALL, 1, 0, (unsigned char*) 0, 0,
358
                                       (void*)0, (unsigned char*)0, (int*)0, 0);
359
                result = 0;
360
            }
361
            break;
362
        }
363
      case ETH_DRV_SET_MC_LIST:
364
        {
365
            struct eth_drv_mc_list* mcl = (struct eth_drv_mc_list*) data;
366
            if (eth->multi_supported && (0 == mcl->len)) {
367
                synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_MULTIALL, 0, 0, (unsigned char*) 0, 0,
368
                                       (void*)0, (unsigned char*)0, (int*)0, 0);
369
                result = 0;
370
            }
371
            break;
372
        }
373
      default:
374
        break;
375
    }
376
 
377
    return result;
378
}
379
 
380
// ----------------------------------------------------------------------------
381
// Starting and stopping an interface. This includes restarting in
382
// promiscuous mode.
383
static void
384
synth_eth_start(struct eth_drv_sc* sc, unsigned char* enaddr, int flags)
385
{
386
    synth_eth* eth = (synth_eth*)(sc->driver_private);
387
 
388
    eth->up         = 0;
389
    eth->rx_pending = 0;
390
 
391
    if ((-1 != eth->synth_id) && synth_auxiliary_running) {
392
        synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_START, flags & IFF_PROMISC, 0, (void*) 0, 0,
393
                                (int*)0, (void*) 0, (int*) 0, 0);
394
    }
395
    eth->up = 1;
396
}
397
 
398
static void
399
synth_eth_stop(struct eth_drv_sc* sc)
400
{
401
    synth_eth* eth = (synth_eth*)(sc->driver_private);
402
 
403
    eth->up         = 0;
404
    if ((-1 != eth->synth_id) && synth_auxiliary_running) {
405
        synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_STOP, 0, 0, (void*) 0, 0,
406
                                (int*) 0, (void*) 0, (int*) 0, 0);
407
    }
408
    eth->rx_pending = 0;
409
}
410
 
411
 
412
// ----------------------------------------------------------------------------
413
// Initialization.
414
//
415
// This requires instantiating a device of class ethernet with a specific
416
// device name held in the eth_drv_sc structure. No additional device data
417
// is needed. If a device can be instantiated then the interrupt vector
418
// and the MAC address can be obtained, and an interrupt handler can be
419
// installed.
420
static bool
421
synth_eth_init(struct cyg_netdevtab_entry* ndp)
422
{
423
    bool result = false;
424
    struct eth_drv_sc* sc   = (struct eth_drv_sc*)(ndp->device_instance);
425
    struct synth_eth*  eth  = (struct synth_eth*)(sc->driver_private);
426
 
427
    if (synth_auxiliary_running) {
428
        eth->synth_id = synth_auxiliary_instantiate("devs/eth/synth/ecosynth", SYNTH_MAKESTRING(CYGPKG_DEVS_ETH_ECOSYNTH), "ethernet",
429
                                                    sc->dev_name, (const char*) 0);
430
 
431
        if (-1 != eth->synth_id) {
432
            unsigned char data[7];
433
            result = true;
434
            synth_auxiliary_xchgmsg(eth->synth_id, SYNTH_ETH_GETPARAMS, 0, 0, (const unsigned char*) 0, 0,
435
                                    (int *)&(eth->interrupt), data, 0, 7);
436
            memcpy(eth->MAC, data, 6);
437
            eth->multi_supported = data[6];
438
            cyg_drv_interrupt_create(eth->interrupt,
439
                                     0,
440
                                     (CYG_ADDRWORD) sc,
441
                                     &synth_eth_isr,
442
                                     &synth_eth_dsr,
443
                                     &(eth->interrupt_handle),
444
                                     &(eth->interrupt_data));
445
            cyg_drv_interrupt_attach(eth->interrupt_handle);
446
            cyg_drv_interrupt_unmask(eth->interrupt);
447
        }
448
    }
449
    (*sc->funs->eth_drv->init)(sc, eth->MAC);
450
 
451
#ifdef CYGPKG_NET
452
    if (eth->multi_supported) {
453
      sc->sc_arpcom.ac_if.if_flags |= IFF_ALLMULTI;
454
    }
455
#endif    
456
    return result;
457
}

powered by: WebSVN 2.1.0

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