Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
583 mauro 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/**
22
 ------------
23
 CVS :        $Id: eth.c,v 1.1 2004-04-23 14:30:32 mauro Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1 $
27
 Last update: $Date: 2004-04-23 14:30:32 $
28
 ------------
29
**/
30
 
31
/*
32
 * Copyright (C) 2000 Luca Abeni
33
 *
34
 * This program is free software; you can redistribute it and/or modify
35
 * it under the terms of the GNU General Public License as published by
36
 * the Free Software Foundation; either version 2 of the License, or
37
 * (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
47
 *
48
 */
49
 
50
 
51
/* Author:      Luca Abeni                  */
52
/* Date:        2/12/1997                                       */
53
 
54
/* File:    eth.C                       */
55
/* Revision:    2.0                     */
56
 
57
/*
58
   Ethernet layer: it is an inetrface between the low level driver (
59
   3Com 3c59xx, ...) and the high level protocols (IP, ARP,...).
60
*/
61
 
62
/* only 4 debug... */
63
int netlev;
64
 
65
#include <kernel/kern.h>
66
#include <modules/hartport.h>
67
 
68
#include <drivers/pci.h>
69
 
70
#include "eth_priv.h"
71
#include "netbuff.h"
72
 
73
#include <linux/netdevice.h>
74
/*#include "lowlev.h"
75
//#include "3com.h" */
76
 
77
#define DEBUG_ETH
78
 
79
#define ETH_PAGE        5
80
 
81
struct eth_service{
82
        WORD type;
83
        void (*rfun)(void *pkt);
84
} ETH_SERVICE;
85
 
86
int definedprotocols;
87
struct eth_service eth_table[ETH_MAX_PROTOCOLS];
88
 
89
#define ETH_RX_BUFFERS      4   
90
#define ETH_TX_BUFFERS      4
91
 
92
#ifndef ETH0_ADDR
93
# define ETH0_ADDR 0
94
#endif
95
#ifndef ETH0_IRQ
96
# define ETH0_IRQ 0
97
#endif
98
 
99
#define NONE 0
100
 
101
/*extern void net_handler(void);
102
//extern PID net_extern_driver(void);*/
103
 
104
PID nettask_pid = NIL;
105
static PORT NetRxPort;
106
 
107
/* void (*vortex_send)(DWORD BaseAddress, DWORD *txbuff, int len); */
108
 
109
int ethIsInstalled = FALSE;
110
 
111
/* device descriptor */
112
struct eth_device eth_dev;
113
struct pci_des pci_devs[5];
114
/* This is the Linux one!!! */
115
static struct device device0 = {
116
        "eth0", 0, 0, 0, 0, ETH0_ADDR, ETH0_IRQ, 0, 0, 0, NULL, NULL};
117
struct device *dev_base = &device0;
118
 
119
/* received frames buffers */
120
extern struct netbuff rxbuff; /* from skbuff.c */
121
 
122
/* buffers for the frames to send */
123
/* struct netbuff txbuff; */
124
 
125
/* Called if an unknown frames arrives */
126
void eth_nullfun(void *pkt)
127
{
128
        kern_raise(ETH_NULLPROTOCOL_EXC,NIL);
129
}
130
 
131
 
132
 
133
 
134
void dev_tint(struct device *dev)
135
{
136
        printk(KERN_WARNING "Warning!!!! dev_tint called!!! (Why?)\n");
137
        sys_abort(201);
138
}
139
 
140
/*
141
   -----------------------------------------------------------------------
142
   The extern process calls this function when a frame arrives
143
   -----------------------------------------------------------------------
144
*/
145
 
146
void netif_rx(struct sk_buff *skb)
147
{
148
  //cprintf("DENTRO netif_rx, skbuf=%p\n",skb->data);
149
        if (nettask_pid == NIL) {
150
                printk(KERN_CRIT "Net receives packets, but the driver doesn't exist.\n");
151
                sys_abort(300);
152
        }
153
 
154
        port_send(NetRxPort,skb,NON_BLOCK);
155
/*  task_activate(nettask_pid);*/
156
}
157
 
158
TASK net_extern_driver(void)
159
{
160
        static PORT NetPort;
161
        struct sk_buff skb;
162
        void *pkt;
163
        int len;
164
        BYTE count;
165
        int i;
166
 
167
        NetPort = port_connect("NetPort", sizeof(struct sk_buff), STREAM, READ);
168
        while (1) {
169
                /* debug... */
170
                netlev = 1;
171
 
172
                port_receive(NetPort,&skb,BLOCK);  
173
                pkt = skb.data;
174
                len = skb.len;
175
 
176
                ((struct eth_header *)pkt)->type = ntohs(((struct eth_header *)pkt)->type);
177
                count = 0;
178
                /* Search for the frame protocol...*/
179
                for (i = 0; i < definedprotocols; i++) {
180
                        /* debug... */
181
                        netlev = 10 + i;
182
 
183
                        if (eth_table[i].type == (((struct eth_header *)pkt)->type)) {
184
                                count++;
185
                                /*...and call the protocol CallBack!!! */
186
                                eth_table[i].rfun(pkt);
187
                        }
188
                }
189
 
190
                /* debug... */
191
                netlev = 20;
192
 
193
                //cprintf("ETH: releasing %p\n", pkt);
194
 
195
                // NOTE changed by PJ because skb.data not always point to the
196
                // buffer start!!!... it is skb.head that always points there!
197
                netbuff_release(&rxbuff, skb.head);
198
 
199
                /* debug... */
200
                netlev = 30;
201
        }
202
}
203
 
204
/*
205
   --------------------
206
   Interface functions
207
   --------------------
208
*/
209
/* formatted print of an ethernet header */
210
void eth_printHeader(struct eth_header *p)
211
{
212
        cprintf("Dest   : %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",p->dest.ad[0],
213
                                                        p->dest.ad[1],
214
                                                        p->dest.ad[2],
215
                                                        p->dest.ad[3],
216
                                                        p->dest.ad[4],
217
                                                        p->dest.ad[5]);
218
        cprintf("Source : %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",p->source.ad[0],
219
                                                        p->source.ad[1],
220
                                                        p->source.ad[2],
221
                                                        p->source.ad[3],
222
                                                        p->source.ad[4],
223
                                                        p->source.ad[5]);
224
        cprintf("Type : %x\n",p->type);
225
}
226
 
227
void eth_showinfo(struct eth_device *d)
228
{
229
        cprintf("IntLine                : %d\n",d->IntLine);
230
        cprintf("BaseAddress    : %lx\n",d->BaseAddress);
231
        cprintf("Address                : %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
232
                                        d->addr.ad[0],d->addr.ad[1],d->addr.ad[2],
233
                                        d->addr.ad[3],d->addr.ad[4],d->addr.ad[5]);
234
}
235
 
236
/* formatted print of an ethernet frame*/
237
void eth_printPkt(char *pkt,int len)
238
{
239
        int i,j,offset;
240
 
241
        eth_printHeader((struct eth_header *)pkt);
242
        offset = sizeof(struct eth_header);
243
        len -= offset;
244
        for (i = 0; i < len; i += 10) {
245
        for (j = 0; j < 10; j++)
246
                cprintf("%2.2x  ",pkt[offset+i+j]);
247
        for (j = 0; j < 10; j++)
248
                cputc(pkt[offset+i+j]);
249
                cprintf("\n");
250
        }
251
        cprintf("\n");
252
}
253
 
254
void eth_copy_and_sum(struct sk_buff *dest, unsigned char *src, int length, int base)
255
{
256
        memcpy(dest->data,src,length);
257
}
258
 
259
#if 0                                                                   
260
/*-------------------- User Interface -----------------------------*/
261
unsigned short htons(unsigned short host)
262
{
263
        return ((host & 0xff00) >> 8) + ((host & 0x00ff) << 8);
264
}
265
 
266
unsigned short ntohs(unsigned short host)
267
{
268
        return ((host & 0xff00) >> 8) + ((host & 0x00ff) << 8);
269
}
270
#endif
271
 
272
/*
273
   Translate an ethernet address from a text string to an eth_addr
274
   structure
275
*/
276
void eth_str2Addr(char *add, struct eth_addr *ds)
277
{
278
        int ad[6];
279
        int i,j;
280
        char c;
281
 
282
        i = 0;
283
        for(j = 0; j < 6; j++) {
284
                ad[j] = 0;
285
                        while((add[i] != ':') && (add[i] != 0)) {
286
                                c = add[i++];
287
                                if (c <= '9') c = c - '0';
288
                                else c = c - 'A' + 10;
289
                                ad[j] = ad[j] * 16 + c;
290
                        }
291
                i++;
292
        }
293
        for (i=0; i<6; i++) ds->ad[i] = ad[i];
294
}
295
 
296
/* Set a higher level protocol's CallBack */
297
int eth_setProtocol(WORD type, void (*recv)(void *pkt))
298
{
299
        int i;
300
 
301
        if (definedprotocols == ETH_MAX_PROTOCOLS) return FALSE;
302
        for(i = 0; i < definedprotocols; i++) {
303
                if (eth_table[i].type == type) return FALSE;
304
        }
305
        eth_table[definedprotocols].type = type;
306
        eth_table[definedprotocols++].rfun = recv;
307
 
308
        return TRUE;
309
}
310
 
311
/* Fill an ethernet frame's header and return a pointer to the frame's body */
312
void *eth_setHeader(void *b,struct eth_addr dest, WORD type)
313
{
314
        setEthAddr(((struct eth_header *)b)->dest,dest);
315
        setEthAddr(((struct eth_header *)b)->source,eth_dev.addr);
316
        /* the type field is in big-endian format */
317
        ((struct eth_header *) b)->type = htons(type);
318
 
319
        return((BYTE *)b + sizeof(struct eth_header));
320
}
321
 
322
/* getFirstDataByte : Return a pointer to the body of an ethernet frame */
323
void *eth_getFDB(void *p)
324
{
325
        return ((void *)((BYTE *)p + sizeof(struct eth_header)));
326
}
327
 
328
/* eth_getAddress : return the local ethernet address */
329
void eth_getAddress(struct eth_addr *eth)
330
{
331
        memcpy(eth->ad,&(device0.dev_addr),sizeof(struct eth_addr));
332
}
333
 
334
/* Send an ethernet frame */
335
int eth_sendPkt(void *p, int len)
336
{
337
        int i;
338
        int l;
339
        struct sk_buff buff;
340
 
341
        l = len + sizeof(struct eth_header);
342
        if (l < 60) {
343
                for (i = l; i <= 60; i++) *((BYTE *)p + i) = 0;
344
                l = 60;
345
        }
346
        buff.len = l;
347
        buff.data = p;
348
        device0.hard_start_xmit(&buff, &device0);
349
        /* lowlev_send(eth_dev.BaseAddress, p, l); */
350
 
351
        return TRUE;
352
}
353
 
354
int eth_exc(int err)
355
{
356
        int p;
357
 
358
         if (err != ETH_BUFFERS_FULL) printk(KERN_ERR "Ethernet : ");
359
        switch (err) {
360
        case ETH_DRIVER_NOT_FOUND :
361
                printk(KERN_ERR "NET PANIC --> Etherlink not found.\n");
362
                return 0;
363
        case ETH_RXERROR :
364
                printk(KERN_ERR "Receive error (vero dramma!!!).\n");
365
                return 0;
366
        case ETH_TXERROR :
367
                printk(KERN_ERR "Transimit error: N. Max Retry.\n");
368
                return 0;
369
        case ETH_PROTOCOL_ERROR :
370
                printk(KERN_ERR "Too much protocols.\n");
371
                return 0;
372
        case ETH_BUFFERS_FULL:
373
                printk(KERN_ERR "Buffers full: frame lost!\n");
374
                return 1;
375
        case ETH_NULLPROTOCOL_EXC:
376
                printk(KERN_ERR "Null protocol called!!!\n");
377
                for (p = 0; p < ETH_MAX_PROTOCOLS; p++) {
378
                printk(KERN_ERR "%d:   %d\n", p, eth_table[p].type);
379
                }
380
                return 0;
381
        default :
382
                return 1;
383
        }
384
}
385
 
386
void skb_init(void);
387
void linuxpci_init(void);
388
int rtl8139_probe(struct device *dev);
389
int tc59x_probe(struct device *dev);
390
int eepro100_probe(struct device *dev);
391
int el3_probe(struct device *dev);
392
int ne_probe(struct device *dev);
393
 
394
int eth_init(int mode, TASK_MODEL *m)
395
{
396
        SOFT_TASK_MODEL m_soft;
397
        int i;
398
#if 0
399
        ndev;
400
        WORD Class;
401
        struct pci_regs *r;
402
        PID p;
403
 
404
#endif
405
 
406
        BYTE cardtype;
407
        int linux_found = 0;
408
 
409
        if (!ethIsInstalled) {
410
                /* Scan the devices connected to the PCI bus */
411
                cardtype = NONE;
412
 
413
                skb_init();
414
                NetRxPort = port_create("NetPort",sizeof(struct sk_buff),50,STREAM,WRITE);
415
                if (!m) {
416
                        soft_task_default_model(m_soft);
417
                        soft_task_def_wcet(m_soft, 1000);
418
                        soft_task_def_period(m_soft,20000);
419
                        soft_task_def_met(m_soft, 1000);
420
                        soft_task_def_aperiodic(m_soft);
421
                        soft_task_def_system(m_soft);
422
                        soft_task_def_nokill(m_soft);
423
                        m = (TASK_MODEL *)&m_soft;
424
                }
425
 
426
                nettask_pid = task_create("rxProc", net_extern_driver, m, NULL);
427
                if (nettask_pid == NIL) {
428
                        printk(KERN_ERR "Can't create extern driver.\n");
429
                        return 0;
430
                }
431
                task_activate(nettask_pid);
432
 
433
                if (1) { //!!!if (pci_init() == 1) {
434
                        linuxpci_init();
435
#ifdef DEBUG_ETH
436
                        printk(KERN_DEBUG "LF %d\n", linux_found);
437
#endif
438
                        linux_found += (rtl8139_probe(&device0) == 0);
439
#ifdef DEBUG_ETH
440
                        printk(KERN_DEBUG "LF %d\n", linux_found);
441
#endif
442
                        linux_found += (tc59x_probe(&device0) == 0);
443
#ifdef DEBUG_ETH
444
                        printk(KERN_DEBUG "LF %d\n", linux_found);
445
#endif
446
                        linux_found += (eepro100_probe(&device0) == 0);
447
#ifdef DEBUG_ETH
448
                        printk(KERN_DEBUG "LF %d\n", linux_found);
449
#endif
450
 
451
 
452
#if 0
453
                ndev = pci_scan_bus(pci_devs);
454
#ifdef __ETH_DBG__
455
pci_show(pci_devs, ndev);
456
#endif
457
                for (i = 0; i < ndev; i++) {
458
                r = (struct pci_regs *) pci_devs[i].mem;
459
                Class = r->ClassCode;
460
                /* Is there a network card? */
461
                if (Class == 0x0200) {
462
                        if (cardtype == NONE) {
463
                        cardtype = UNKNOWN;
464
                        }
465
                /* is it a 3COM card? */
466
                        if (r->VendorId == 0x10b7) {
467
                        /* YES!!!!!! */
468
                        lowlev_info = vortex_info;
469
                                lowlev_readregs = vortex_readregs;
470
                                lowlev_open = vortex_open;
471
                                lowlev_close = vortex_close;
472
                                if (mode == TXTASK) {
473
                                lowlev_send = vortex_send_msg;
474
                        } else {
475
                                lowlev_send = vortex_send_mem;
476
                        }
477
                        printk(KERN_INFO "PCI Ethlink card found:\n");
478
                        lowlev_info(r);
479
                        cardtype = VORTEX;
480
                        }
481
                }
482
                }
483
        }
484
        if ((cardtype == NONE) || (cardtype == UNKNOWN)) {
485
                exc_raise(ETH_DRIVER_NOT_FOUND);
486
        }
487
/*PUT HERE THE PFUN INIT!!!*/
488
        if (cardtype == VORTEX) {
489
        }
490
        /*
491
           Use it if you want to see the value of the internal
492
           registers of the card
493
        */
494
        /*vortex_readregs(eth_dev.BaseAddress);*/
495
 
496
        p = lowlev_open(eth_dev.BaseAddress, mode);
497
 
498
        /* Set the Fast Handler and the external process */
499
        handler_set(eth_dev.IntLine, net_handler, p);
500
#else
501
                }
502
                if (linux_found == 0) {
503
                  linux_found += (el3_probe(&device0) == 0);
504
#ifdef DEBUG_ETH
505
                  printk(KERN_DEBUG "LF %d\n", linux_found);
506
#endif
507
                  linux_found += (ne_probe(&device0) == 0);
508
#ifdef DEBUG_ETH
509
                  printk(KERN_DEBUG "LF %d\n", linux_found);
510
#endif
511
                  linux_found += (NS8390_init(&device0, 1) == 0);
512
#ifdef DEBUG_ETH
513
                  printk(KERN_DEBUG "LF %d\n", linux_found);
514
#endif
515
                }
516
 
517
/*
518
                if (mode & LOOPBACK) {
519
                        cprintf("Installing loopback device (forced)\n");
520
                        loopback_init(&device0);
521
                }
522
*/
523
                if (linux_found) {
524
                        device0.open(&device0);
525
                        printk(KERN_INFO "Network card found.\n");
526
                } else {
527
                        printk(KERN_INFO "No network card found.\n");
528
/*                      cprintf("No card found... Installing loopback device\n");
529
                        loopback_init(&device0);
530
                        device0.open(&device0);*/
531
                        return 0;
532
                }
533
#endif
534
 
535
//              netbuff_init(&rxbuff, ETH_RX_BUFFERS, ETH_MAX_LEN);
536
//              netbuff_init(&txbuff, ETH_TX_BUFFERS, ETH_MAX_LEN);
537
 
538
                definedprotocols = 0;
539
                for (i = 0; i < ETH_MAX_PROTOCOLS; i++) {
540
                        eth_table[i].type = 0;
541
                        eth_table[i].rfun = eth_nullfun;
542
                }
543
                ethIsInstalled = TRUE;
544
                /* Don't crash the system at the exit, please :) */
545
                sys_atrunlevel(eth_close,NULL,RUNLEVEL_BEFORE_EXIT);
546
 
547
        } else {
548
                printk(KERN_INFO "Ethernet already installed.\n");
549
                return 0;
550
        }
551
        return 1;
552
}
553
 
554
void eth_close(void *a)
555
{
556
#ifdef DEBUG_ETH
557
        printk(KERN_DEBUG "Network Closing.\n");
558
#endif
559
        if (ethIsInstalled == TRUE) {
560
                device0.stop(&device0);
561
                /* This seems to break everithing... */
562
                // lowlev_close(eth_dev.BaseAddress);
563
                ethIsInstalled = FALSE;
564
        }
565
#ifdef DEBUG_ETH
566
        printk(KERN_DEBUG "Network Closed.\n");
567
#endif
568
}