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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [redboot/] [current/] [src/] [net/] [enet.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      net/enet.c
4
//
5
//      Stand-alone ethernet [link-layer] support for RedBoot
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 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):    gthomas
43
// Contributors: gthomas
44
// Date:         2000-07-14
45
// Purpose:      
46
// Description:  
47
//              
48
// This code is part of RedBoot (tm).
49
//
50
//####DESCRIPTIONEND####
51
//
52
//==========================================================================
53
 
54
#include <redboot.h>
55
#include <net/net.h>
56
#include <cyg/io/eth/eth_drv.h>       // Logical driver interfaces
57
 
58
//#define ENET_STATS 1
59
 
60
#ifdef ENET_STATS
61
static int num_ip = 0;
62
static int num_arp = 0;
63
#ifdef NET_SUPPORT_RARP
64
static int num_rarp = 0;
65
#endif
66
static int num_received = 0;
67
static int num_transmitted = 0;
68
#endif
69
 
70
//
71
// Support for user handlers of additional ethernet packets (nonIP)
72
//
73
 
74
#define NUM_EXTRA_HANDLERS 4
75
static struct {
76
    int type;
77
    pkt_handler_t handler;
78
} eth_handlers[NUM_EXTRA_HANDLERS];
79
 
80
pkt_handler_t
81
__eth_install_listener(int eth_type, pkt_handler_t handler)
82
{
83
    int i, empty;
84
    pkt_handler_t old;
85
 
86
    if (eth_type > 0x800 || handler != (pkt_handler_t)0) {
87
        empty = -1;
88
        for (i = 0;  i < NUM_EXTRA_HANDLERS;  i++) {
89
            if (eth_handlers[i].type == eth_type) {
90
                // Replace existing handler
91
                old = eth_handlers[i].handler;
92
                eth_handlers[i].handler = handler;
93
                return old;
94
            }
95
            if (eth_handlers[i].type == 0) {
96
                empty = i;
97
            }
98
        }
99
        if (empty >= 0) {
100
            // Found a free slot
101
            eth_handlers[empty].type = eth_type;
102
            eth_handlers[empty].handler = handler;
103
            return (pkt_handler_t)0;
104
        }
105
    }
106
    diag_printf("** Warning: can't install listener for ethernet type 0x%02x\n", eth_type);
107
    return (pkt_handler_t)0;
108
}
109
 
110
void
111
__eth_remove_listener(int eth_type)
112
{
113
    int i;
114
 
115
    for (i = 0;  i < NUM_EXTRA_HANDLERS;  i++) {
116
        if (eth_handlers[i].type == eth_type) {
117
            eth_handlers[i].type = 0;
118
        }
119
    }
120
}
121
 
122
/*
123
 * Non-blocking poll of ethernet link. Process packets until no more
124
 * are available.
125
 */
126
void
127
__enet_poll(void)
128
{
129
    pktbuf_t *pkt;
130
    eth_header_t eth_hdr;
131
    int i, type;
132
#ifdef DEBUG_PKT_EXHAUSTION
133
    static bool was_exhausted = false;
134
#endif
135
 
136
    while (true) {
137
        /*
138
         * Try to get a free pktbuf and return if none
139
         * are available.
140
         */
141
        if ((pkt = __pktbuf_alloc(ETH_MAX_PKTLEN)) == NULL) {
142
#ifdef DEBUG_PKT_EXHAUSTION
143
            if (!was_exhausted) {
144
                int old = start_console();  // Force output to standard port
145
                diag_printf("__enet_poll: no more buffers\n");
146
                __pktbuf_dump();
147
                was_exhausted = true;
148
                end_console(old);
149
            }
150
#endif
151
            return;
152
        }
153
#ifdef DEBUG_PKT_EXHAUSTION
154
        was_exhausted = false;  // Report the next time we're out of buffers
155
#endif
156
 
157
        if ((pkt->pkt_bytes = eth_drv_read((char *)&eth_hdr, (char *)pkt->buf,
158
                                           ETH_MAX_PKTLEN)) > 0) {
159
#ifdef ENET_STATS
160
            ++num_received;
161
#endif
162
            switch (type = ntohs(eth_hdr.type)) {
163
 
164
            case ETH_TYPE_IP:
165
#ifdef ENET_STATS
166
                ++num_ip;
167
#endif
168
                pkt->ip_hdr = (ip_header_t *)pkt->buf;
169
                __ip_handler(pkt, &eth_hdr.source);
170
                break;
171
 
172
            case ETH_TYPE_ARP:
173
#ifdef ENET_STATS
174
                ++num_arp;
175
#endif
176
                pkt->arp_hdr = (arp_header_t *)pkt->buf;
177
                __arp_handler(pkt);
178
                break;
179
 
180
#ifdef NET_SUPPORT_RARP
181
            case ETH_TYPE_RARP:
182
#ifdef ENET_STATS
183
                ++num_rarp;
184
#endif
185
                pkt->arp_hdr = (arp_header_t *)pkt->buf;
186
                __rarp_handler(pkt);
187
                break;
188
#endif
189
 
190
            default:
191
                if (type > 0x800) {
192
                    for (i = 0;  i < NUM_EXTRA_HANDLERS;  i++) {
193
                        if (eth_handlers[i].type == type) {
194
                            (eth_handlers[i].handler)(pkt, &eth_hdr);
195
                        }
196
                    }
197
                }
198
                __pktbuf_free(pkt);
199
                break;
200
            }
201
        } else {
202
            __pktbuf_free(pkt);
203
            break;
204
        }
205
    }
206
}
207
 
208
 
209
 
210
/*
211
 * Send an ethernet packet.
212
 */
213
void
214
__enet_send(pktbuf_t *pkt, enet_addr_t *dest, int eth_type)
215
{
216
    eth_header_t eth_hdr;
217
 
218
    // Set up ethernet header
219
    memcpy(&eth_hdr.destination, dest, sizeof(enet_addr_t));
220
    memcpy(&eth_hdr.source, __local_enet_addr, sizeof(enet_addr_t));
221
    eth_hdr.type = htons(eth_type);
222
 
223
    eth_drv_write((char *)&eth_hdr, (char *)pkt->buf, pkt->pkt_bytes);
224
#ifdef ENET_STATS
225
    ++num_transmitted;
226
#endif
227
}
228
 
229
#ifdef __LITTLE_ENDIAN__
230
 
231
unsigned long
232
ntohl(unsigned long x)
233
{
234
    return (((x & 0x000000FF) << 24) |
235
            ((x & 0x0000FF00) <<  8) |
236
            ((x & 0x00FF0000) >>  8) |
237
            ((x & 0xFF000000) >> 24));
238
}
239
 
240
unsigned short
241
ntohs(unsigned short x)
242
{
243
    return (((x & 0x00FF) << 8) |
244
            ((x & 0xFF00) >> 8));
245
}
246
 
247
#endif

powered by: WebSVN 2.1.0

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