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