Subversion Repositories shark

Rev

Rev 34 | Rev 333 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 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
 ------------
205 pj 23
 CVS :        $Id: eth.c,v 1.4 2003-07-10 07:52:04 pj Exp $
2 pj 24
 
25
 File:        $File$
205 pj 26
 Revision:    $Revision: 1.4 $
27
 Last update: $Date: 2003-07-10 07:52:04 $
2 pj 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
 
17 pj 77
//#define DEBUG_ETH
78
 
2 pj 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
{
34 pj 136
        printk(KERN_WARNING "Warning!!!! dev_tint called!!! (Why?)\n");
2 pj 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) {
17 pj 150
                printk(KERN_CRIT "Net receives packets, but the driver doesn't exist!!!\n");
2 pj 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
                port_receive(NetPort,&skb,BLOCK);  
172
                pkt = skb.data;
173
                len = skb.len;
174
 
175
                ((struct eth_header *)pkt)->type = ntohs(((struct eth_header *)pkt)->type);
176
                count = 0;
177
                /* Search for the frame protocol...*/
178
                for (i = 0; i < definedprotocols; i++) {
179
/* debug... */
180
netlev = 10 + i;
181
                        if (eth_table[i].type == (((struct eth_header *)pkt)->type)) {
182
                                count++;
183
                                /*...and call the protocol CallBack!!! */
184
                                eth_table[i].rfun(pkt);
185
                        }
186
                }
187
 
188
/* debug... */
189
                netlev = 20;
190
                //cprintf("ETH: releasing %p\n", pkt);
205 pj 191
 
192
                // NOTE changed by PJ because skb.data not always point to the
193
                // buffer start!!!... it is skb.head that always points there!
194
                netbuff_release(&rxbuff, skb.head);
2 pj 195
/* debug... */
196
netlev = 30;
197
        }
198
}
199
 
200
/*
201
   --------------------
202
   Interface functions
203
   --------------------
204
*/
205
/* formatted print of an ethernet header */
206
void eth_printHeader(struct eth_header *p)
207
{
17 pj 208
        cprintf("Dest   : %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",p->dest.ad[0],
2 pj 209
                                                        p->dest.ad[1],
210
                                                        p->dest.ad[2],
211
                                                        p->dest.ad[3],
212
                                                        p->dest.ad[4],
213
                                                        p->dest.ad[5]);
214
        cprintf("Source : %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",p->source.ad[0],
215
                                                        p->source.ad[1],
216
                                                        p->source.ad[2],
217
                                                        p->source.ad[3],
218
                                                        p->source.ad[4],
219
                                                        p->source.ad[5]);
220
        cprintf("Type : %x\n",p->type);
221
}
222
 
223
void eth_showinfo(struct eth_device *d)
224
{
225
        cprintf("IntLine                : %d\n",d->IntLine);
226
        cprintf("BaseAddress    : %lx\n",d->BaseAddress);
227
        cprintf("Address                : %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
228
                                        d->addr.ad[0],d->addr.ad[1],d->addr.ad[2],
229
                                        d->addr.ad[3],d->addr.ad[4],d->addr.ad[5]);
230
}
231
 
232
/* formatted print of an ethernet frame*/
233
void eth_printPkt(char *pkt,int len)
234
{
235
        int i,j,offset;
236
 
237
        eth_printHeader((struct eth_header *)pkt);
238
        offset = sizeof(struct eth_header);
239
        len -= offset;
240
        for (i = 0; i < len; i += 10) {
241
        for (j = 0; j < 10; j++)
242
                cprintf("%2.2x  ",pkt[offset+i+j]);
243
        for (j = 0; j < 10; j++)
244
                cputc(pkt[offset+i+j]);
245
                cprintf("\n");
246
        }
247
        cprintf("\n");
248
}
249
 
250
#if 0                                                                   
251
/*-------------------- User Interface -----------------------------*/
252
unsigned short htons(unsigned short host)
253
{
254
        return ((host & 0xff00) >> 8) + ((host & 0x00ff) << 8);
255
}
256
 
257
unsigned short ntohs(unsigned short host)
258
{
259
        return ((host & 0xff00) >> 8) + ((host & 0x00ff) << 8);
260
}
261
#endif
262
 
263
/*
264
   Translate an ethernet address from a text string to an eth_addr
265
   structure
266
*/
267
void eth_str2Addr(char *add, struct eth_addr *ds)
268
{
269
        int ad[6];
270
        int i,j;
271
        char c;
272
 
273
        i = 0;
274
        for(j = 0; j < 6; j++) {
275
        ad[j] = 0;
276
        while((add[i] != ':') && (add[i] != 0)) {
277
                c = add[i++];
278
                if (c <= '9') c = c - '0';
279
                else c = c - 'A' + 10;
280
                ad[j] = ad[j] * 16 + c;
281
        }
282
        i++;
283
        }
284
        for (i=0; i<6; i++) ds->ad[i] = ad[i];
285
}
286
 
287
/* Set a higher level protocol's CallBack */
288
int eth_setProtocol(WORD type, void (*recv)(void *pkt))
289
{
290
        int i;
291
 
292
        if (definedprotocols == ETH_MAX_PROTOCOLS) return FALSE;
293
        for(i = 0; i < definedprotocols; i++) {
294
        if (eth_table[i].type == type) return FALSE;
295
        }
296
        eth_table[definedprotocols].type = type;
297
        eth_table[definedprotocols++].rfun = recv;
298
        return TRUE;
299
}
300
 
301
/* Fill an ethernet frame's header and return a pointer to the frame's body */
302
void *eth_setHeader(void *b,struct eth_addr dest, WORD type)
303
{
304
        setEthAddr(((struct eth_header *)b)->dest,dest);
305
        setEthAddr(((struct eth_header *)b)->source,eth_dev.addr);
306
        /* the type field is in big-endian format */
307
        ((struct eth_header *) b)->type = htons(type);
308
        return((BYTE *)b + sizeof(struct eth_header));
309
}
310
 
311
/* getFirstDataByte : Return a pointer to the body of an ethernet frame */
312
void *eth_getFDB(void *p)
313
{
314
        return ((void *)((BYTE *)p + sizeof(struct eth_header)));
315
}
316
 
317
/* eth_getAddress : return the local ethernet address */
318
void eth_getAddress(struct eth_addr *eth)
319
{
320
        memcpy(eth->ad,&(device0.dev_addr),sizeof(struct eth_addr));
321
}
322
 
323
/* Send an ethernet frame */
324
int eth_sendPkt(void *p, int len)
325
{
326
        int i;
327
        int l;
328
        struct sk_buff buff;
329
 
330
        l = len + sizeof(struct eth_header);
331
        if (l < 60) {
332
                for (i = l; i <= 60; i++) *((BYTE *)p + i) = 0;
333
                l = 60;
334
        }
335
        buff.len = l;
336
        buff.data = p;
337
        device0.hard_start_xmit(&buff, &device0);
338
/*    lowlev_send(eth_dev.BaseAddress, p, l);*/
339
        return TRUE;
340
}
341
 
342
int eth_exc(int err)
343
{
344
        int p;
345
 
17 pj 346
         if (err != ETH_BUFFERS_FULL) printk(KERN_ERR "Ethernet : ");
2 pj 347
        switch (err) {
348
        case ETH_DRIVER_NOT_FOUND :
17 pj 349
                printk(KERN_ERR "NET PANIC --> Etherlink not found.\n");
2 pj 350
                return 0;
351
        case ETH_RXERROR :
17 pj 352
                printk(KERN_ERR "Receive error (vero dramma!!!).\n");
2 pj 353
                return 0;
354
        case ETH_TXERROR :
17 pj 355
                printk(KERN_ERR "Transimit error: N. Max Retry.\n");
2 pj 356
                return 0;
357
        case ETH_PROTOCOL_ERROR :
17 pj 358
                printk(KERN_ERR "Too much protocols.\n");
2 pj 359
                return 0;
360
        case ETH_BUFFERS_FULL:
17 pj 361
                printk(KERN_ERR "Buffers full: frame lost!\n");
2 pj 362
                return 1;
363
        case ETH_NULLPROTOCOL_EXC:
17 pj 364
                printk(KERN_ERR "Null protocol called!!!\n");
2 pj 365
                for (p = 0; p < ETH_MAX_PROTOCOLS; p++) {
17 pj 366
                printk(KERN_ERR "%d:   %d\n", p, eth_table[p].type);
2 pj 367
                }
368
                return 0;
369
        default :
370
                return 1;
371
        }
372
}
373
 
374
int eth_init(int mode, TASK_MODEL *m)
375
{
376
        SOFT_TASK_MODEL m_soft;
377
        int i, ndev;
378
        WORD Class;
379
        struct pci_regs *r;
380
        PID p;
381
        BYTE cardtype;
382
        int linux_found = 0;
383
 
384
        if (!ethIsInstalled) {
17 pj 385
                printk(KERN_INFO "Hartik/Shark Net lib");
2 pj 386
                /* Scan the devices connected to the PCI bus */
387
                cardtype = NONE;
388
 
389
                skb_init();
390
                NetRxPort = port_create("NetPort",sizeof(struct sk_buff),50,STREAM,WRITE);
391
                if (!m) {
392
                  soft_task_default_model(m_soft);
393
                  soft_task_def_wcet(m_soft, 1000);
394
                  soft_task_def_period(m_soft,20000);
395
                  soft_task_def_met(m_soft, 1000);
396
                  soft_task_def_aperiodic(m_soft);
397
                  soft_task_def_system(m_soft);
398
                  soft_task_def_nokill(m_soft);
34 pj 399
                  m = (TASK_MODEL *)&m_soft;
2 pj 400
                }
401
 
402
                nettask_pid = task_create("rxProc", net_extern_driver, m, NULL);
403
                if (nettask_pid == NIL) {
17 pj 404
                  printk(KERN_ERR "Can't create extern driver!!!\n");
405
                  return 0;
2 pj 406
                }
407
                task_activate(nettask_pid);
408
                if (pci_init() == 1) {
409
                        linuxpci_init();
205 pj 410
                        pci_show();
17 pj 411
#ifdef DEBUG_ETH
412
                        printk(KERN_DEBUG "LF %d\n", linux_found);
413
#endif
2 pj 414
                        linux_found += (rtl8139_probe(&device0) == 0);
17 pj 415
#ifdef DEBUG_ETH
416
                        printk(KERN_DEBUG "LF %d\n", linux_found);
417
#endif
2 pj 418
 
419
                        linux_found += (tc59x_probe(&device0) == 0);
17 pj 420
#ifdef DEBUG_ETH
421
                        printk(KERN_DEBUG "LF %d\n", linux_found);
422
#endif
205 pj 423
 
2 pj 424
#if 0
425
                ndev = pci_scan_bus(pci_devs);
426
#ifdef __ETH_DBG__
427
pci_show(pci_devs, ndev);
428
#endif
429
                for (i = 0; i < ndev; i++) {
430
                r = (struct pci_regs *) pci_devs[i].mem;
431
                Class = r->ClassCode;
432
                /* Is there a network card? */
433
                if (Class == 0x0200) {
434
                        if (cardtype == NONE) {
435
                        cardtype = UNKNOWN;
436
                        }
437
                /* is it a 3COM card? */
438
                        if (r->VendorId == 0x10b7) {
439
                        /* YES!!!!!! */
440
                        lowlev_info = vortex_info;
441
                                lowlev_readregs = vortex_readregs;
442
                                lowlev_open = vortex_open;
443
                                lowlev_close = vortex_close;
444
                                if (mode == TXTASK) {
445
                                lowlev_send = vortex_send_msg;
446
                        } else {
447
                                lowlev_send = vortex_send_mem;
448
                        }
17 pj 449
                        printk(KERN_INFO "PCI Ethlink card found:\n");
2 pj 450
                        lowlev_info(r);
451
                        cardtype = VORTEX;
452
                        }
453
                }
454
                }
455
        }
456
        if ((cardtype == NONE) || (cardtype == UNKNOWN)) {
457
                exc_raise(ETH_DRIVER_NOT_FOUND);
458
        }
459
/*PUT HERE THE PFUN INIT!!!*/
460
        if (cardtype == VORTEX) {
461
        }
462
        /*
463
           Use it if you want to see the value of the internal
464
           registers of the card
465
        */
466
        /*vortex_readregs(eth_dev.BaseAddress);*/
467
 
468
        p = lowlev_open(eth_dev.BaseAddress, mode);
469
 
470
        /* Set the Fast Handler and the external process */
471
        handler_set(eth_dev.IntLine, net_handler, p);
472
#else
473
                }
474
                if (linux_found == 0) {
17 pj 475
                  linux_found += (el3_probe(&device0) == 0);
476
#ifdef DEBUG_ETH
477
                  printk(KERN_DEBUG "LF %d\n", linux_found);
478
#endif
479
                  linux_found += (ne_probe(&device0) == 0);
480
#ifdef DEBUG_ETH
481
                  printk(KERN_DEBUG "LF %d\n", linux_found);
482
#endif
2 pj 483
                }
484
 
485
/*
486
                if (mode & LOOPBACK) {
487
                        cprintf("Installing loopback device (forced)\n");
488
                        loopback_init(&device0);
489
                }
490
*/
491
                if (linux_found) {
492
                        device0.open(&device0);
17 pj 493
                        printk(KERN_INFO "Net card found!!!\n");
2 pj 494
                } else {
17 pj 495
                        printk(KERN_INFO "No card found... \n");
2 pj 496
/*          cprintf("No card found... Installing loopback device\n");
497
                        loopback_init(&device0);
498
                        device0.open(&device0);*/
499
                        return 0;
500
                }
501
#endif
502
 
503
//      netbuff_init(&rxbuff, ETH_RX_BUFFERS, ETH_MAX_LEN);
504
//      netbuff_init(&txbuff, ETH_TX_BUFFERS, ETH_MAX_LEN);
505
 
506
                definedprotocols = 0;
507
                for (i = 0; i < ETH_MAX_PROTOCOLS; i++) {
508
                        eth_table[i].type = 0;
509
                        eth_table[i].rfun = eth_nullfun;
510
                }
511
                ethIsInstalled = TRUE;
512
        /* Don't crash the system at the exit, please :) */
513
                sys_atrunlevel(eth_close,NULL,RUNLEVEL_BEFORE_EXIT);
514
 
515
        } else {
17 pj 516
                printk(KERN_INFO "Ethernet already installed!!!\n");
2 pj 517
                return 0;
518
        }
519
        return 1;
520
}
521
 
522
void eth_close(void *a)
523
{
17 pj 524
#ifdef DEBUG_ETH
525
  printk(KERN_DEBUG "CLOSE!!!!\n");
526
#endif
527
  if (ethIsInstalled == TRUE) {
528
    device0.stop(&device0);     /*This seems to break everithing...
529
                              //  lowlev_close(eth_dev.BaseAddress);*/
530
    ethIsInstalled = FALSE;
531
  }
2 pj 532
}