This source file includes following definitions.
- el3_probe
- read_eeprom
- id_read_eeprom
- el3_open
- el3_start_xmit
- el3_interrupt
- el3_get_stats
- update_stats
- el3_rx
- set_multicast_list
- el3_close
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 static char *version = "3c509.c:pl13t 11/24/93 becker@super.org\n";
17
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/interrupt.h>
23 #include <linux/ptrace.h>
24 #include <linux/errno.h>
25 #include <linux/in.h>
26 #include <linux/malloc.h>
27 #include <linux/ioport.h>
28 #include <asm/bitops.h>
29 #include <asm/io.h>
30
31 #include "dev.h"
32 #include "eth.h"
33 #include "skbuff.h"
34 #include "arp.h"
35
36 #ifndef HAVE_ALLOC_SKB
37 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
38 #endif
39
40
41 #ifdef EL3_DEBUG
42 int el3_debug = EL3_DEBUG;
43 #else
44 int el3_debug = 2;
45 #endif
46
47
48
49
50
51 #define EL3_DATA 0x00
52 #define EL3_CMD 0x0e
53 #define EL3_STATUS 0x0e
54 #define ID_PORT 0x100
55 #define EEPROM_READ 0x80
56
57 #define EL3WINDOW(win_num) outw(0x0800+(win_num), ioaddr + EL3_CMD)
58
59
60 #define TX_FIFO 0x00
61 #define RX_FIFO 0x00
62 #define RX_STATUS 0x08
63 #define TX_STATUS 0x0B
64 #define TX_FREE 0x0C
65
66 #define WN4_MEDIA 0x0A
67 #define MEDIA_TP 0x00C0
68
69 struct el3_private {
70 struct enet_statistics stats;
71 };
72
73 static ushort id_read_eeprom(int index);
74 static ushort read_eeprom(short ioaddr, int index);
75 static int el3_open(struct device *dev);
76 static int el3_start_xmit(struct sk_buff *skb, struct device *dev);
77 static void el3_interrupt(int reg_ptr);
78 static void update_stats(int addr, struct device *dev);
79 static struct enet_statistics *el3_get_stats(struct device *dev);
80 static int el3_rx(struct device *dev);
81 static int el3_close(struct device *dev);
82 #ifdef HAVE_MULTICAST
83 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
84 #endif
85
86
87
88 int el3_probe(struct device *dev)
89 {
90 short lrs_state = 0xff, i;
91 ushort ioaddr, irq, if_port;
92 short *phys_addr = (short *)dev->dev_addr;
93 static int current_tag = 0;
94
95
96 if (EISA_bus) {
97 for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
98 if (inw(ioaddr) != 0x6d50)
99 continue;
100
101 irq = inw(ioaddr + 8) >> 12;
102 if_port = inw(ioaddr + 6)>>14;
103 for (i = 0; i < 3; i++)
104 phys_addr[i] = htons(read_eeprom(ioaddr, i));
105
106
107
108 read_eeprom(ioaddr, 7);
109
110
111 goto found;
112 }
113 }
114
115 #ifdef CONFIG_MCA
116 if (MCA_bus) {
117 mca_adaptor_select_mode(1);
118 for (i = 0; i < 8; i++)
119 if ((mca_adaptor_id(i) | 1) == 0x627c) {
120 ioaddr = mca_pos_base_addr(i);
121 irq = inw(ioaddr + 8) >> 12;
122 if_port = inw(ioaddr + 6)>>14;
123 for (i = 0; i < 3; i++)
124 phys_addr[i] = htons(read_eeprom(ioaddr, i));
125
126 mca_adaptor_select_mode(0);
127 goto found;
128 }
129 mca_adaptor_select_mode(0);
130
131 }
132 #endif
133
134
135 outb(0x00, ID_PORT);
136 outb(0x00, ID_PORT);
137 for(i = 0; i < 255; i++) {
138 outb(lrs_state, ID_PORT);
139 lrs_state <<= 1;
140 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
141 }
142
143
144 if (current_tag == 0)
145 outb(0xd0, ID_PORT);
146 else
147 outb(0xd8, ID_PORT);
148
149 if (id_read_eeprom(7) != 0x6d50) {
150 return -ENODEV;
151 }
152
153
154
155
156 for (i = 0; i < 3; i++) {
157 phys_addr[i] = htons(id_read_eeprom(i));
158 }
159
160 {
161 unsigned short iobase = id_read_eeprom(8);
162 if_port = iobase >> 14;
163 ioaddr = 0x200 + ((iobase & 0x1f) << 4);
164 }
165 irq = id_read_eeprom(9) >> 12;
166
167
168
169
170 if (dev->base_addr != 0
171 && dev->base_addr != (unsigned short)ioaddr) {
172 return -ENODEV;
173 }
174
175
176 outb(0xd0 + ++current_tag, ID_PORT);
177
178
179 outb(0xff, ID_PORT);
180
181 EL3WINDOW(0);
182 if (inw(ioaddr) != 0x6d50)
183 return -ENODEV;
184
185 found:
186 dev->base_addr = ioaddr;
187 dev->irq = irq;
188 dev->if_port = if_port;
189 snarf_region(dev->base_addr, 16);
190
191 {
192 char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
193 printk("%s: 3c509 at %#3.3x tag %d, %s port, address ",
194 dev->name, dev->base_addr, current_tag, if_names[dev->if_port]);
195 }
196
197
198 for (i = 0; i < 6; i++)
199 printk(" %2.2x", dev->dev_addr[i]);
200 printk(", IRQ %d.\n", dev->irq);
201
202
203 dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL);
204 memset(dev->priv, 0, sizeof(struct el3_private));
205
206 if (el3_debug > 0)
207 printk(version);
208
209
210 dev->open = &el3_open;
211 dev->hard_start_xmit = &el3_start_xmit;
212 dev->stop = &el3_close;
213 dev->get_stats = &el3_get_stats;
214 #ifdef HAVE_MULTICAST
215 dev->set_multicast_list = &set_multicast_list;
216 #endif
217
218
219 for (i = 0; i < DEV_NUMBUFFS; i++)
220 dev->buffs[i] = NULL;
221
222 dev->hard_header = eth_header;
223 dev->add_arp = eth_add_arp;
224 dev->queue_xmit = dev_queue_xmit;
225 dev->rebuild_header = eth_rebuild_header;
226 dev->type_trans = eth_type_trans;
227
228 dev->type = ARPHRD_ETHER;
229 dev->hard_header_len = ETH_HLEN;
230 dev->mtu = 1500;
231 dev->addr_len = ETH_ALEN;
232 for (i = 0; i < ETH_ALEN; i++) {
233 dev->broadcast[i]=0xff;
234 }
235
236
237 dev->flags = IFF_BROADCAST;
238 dev->family = AF_INET;
239 dev->pa_addr = 0;
240 dev->pa_brdaddr = 0;
241 dev->pa_mask = 0;
242 dev->pa_alen = sizeof(unsigned long);
243
244 return 0;
245 }
246
247
248
249
250 static ushort read_eeprom(short ioaddr, int index)
251 {
252 int timer;
253
254 outw(EEPROM_READ + index, ioaddr + 10);
255
256 for (timer = 0; timer < 162*4 + 400; timer++)
257 SLOW_DOWN_IO;
258 return inw(ioaddr + 12);
259 }
260
261
262 static ushort id_read_eeprom(int index)
263 {
264 int timer, bit, word = 0;
265
266
267
268 outb(EEPROM_READ + index, ID_PORT);
269
270
271 for (timer = 0; timer < 162*4 + 400; timer++)
272 SLOW_DOWN_IO;
273
274 for (bit = 15; bit >= 0; bit--)
275 word = (word << 1) + (inb(ID_PORT) & 0x01);
276
277 if (el3_debug > 3)
278 printk(" 3c509 EEPROM word %d %#4.4x.\n", index, word);
279
280 return word;
281 }
282
283
284
285 static int
286 el3_open(struct device *dev)
287 {
288 int ioaddr = dev->base_addr;
289 int i;
290
291 if (request_irq(dev->irq, &el3_interrupt)) {
292 return -EAGAIN;
293 }
294
295 EL3WINDOW(0);
296 if (el3_debug > 3)
297 printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev->name,
298 dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
299
300
301 outw(0x0001, ioaddr + 4);
302
303 irq2dev_map[dev->irq] = dev;
304
305
306 outw((dev->irq << 12) | 0x0f00, ioaddr + 8);
307
308
309 EL3WINDOW(2);
310
311 for (i = 0; i < 6; i++)
312 outb(dev->dev_addr[i], ioaddr + i);
313
314 if (dev->if_port == 3)
315
316 outw(0x1000, ioaddr + EL3_CMD);
317 else if (dev->if_port == 0) {
318
319 EL3WINDOW(4);
320 outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
321 }
322
323
324 EL3WINDOW(1);
325
326 outw(0x8005, ioaddr + EL3_CMD);
327 outw(0xA800, ioaddr + EL3_CMD);
328 outw(0x2000, ioaddr + EL3_CMD);
329 outw(0x4800, ioaddr + EL3_CMD);
330 outw(0x78ff, ioaddr + EL3_CMD);
331 dev->interrupt = 0;
332 dev->tbusy = 0;
333 dev->start = 1;
334 outw(0x7098, ioaddr + EL3_CMD);
335
336 if (el3_debug > 3)
337 printk("%s: Opened 3c509 IRQ %d status %4.4x.\n",
338 dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
339
340 return 0;
341 }
342
343 static int
344 el3_start_xmit(struct sk_buff *skb, struct device *dev)
345 {
346 struct el3_private *lp = (struct el3_private *)dev->priv;
347 int ioaddr = dev->base_addr;
348
349
350 if (dev->tbusy) {
351 int tickssofar = jiffies - dev->trans_start;
352 if (tickssofar < 10)
353 return 1;
354 printk("%s: transmit timed out, tx_status %2.2x status %4.4x.\n",
355 dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS));
356 dev->trans_start = jiffies;
357
358 outw(0x5800, ioaddr + EL3_CMD);
359 outw(0x4800, ioaddr + EL3_CMD);
360 dev->tbusy = 0;
361 }
362
363 if (skb == NULL) {
364 dev_tint(dev);
365 return 0;
366 }
367
368
369 if (!skb->arp && dev->rebuild_header(skb+1, dev)) {
370 skb->dev = dev;
371 arp_queue (skb);
372 return 0;
373 }
374 skb->arp=1;
375
376 if (skb->len <= 0)
377 return 0;
378
379 if (el3_debug > 4) {
380 printk("%s: el3_start_xmit(lenght = %d) called, status %4.4x.\n",
381 dev->name, skb->len, inw(ioaddr + EL3_STATUS));
382 }
383 #ifndef final_version
384 {
385 ushort status = inw(ioaddr + EL3_STATUS);
386 if (status & 0x0001
387 && inw(ioaddr + EL3_STATUS) & 1) {
388 printk("%s: Missed interrupt, status then %04x now %04x"
389 " Tx %2.2x Rx %4.4x.\n", dev->name, status,
390 inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
391 inw(ioaddr + RX_STATUS));
392 outw(0x7800, ioaddr + EL3_CMD);
393 outw(0x6899, ioaddr + EL3_CMD);
394 outw(0x78ff, ioaddr + EL3_CMD);
395 }
396 }
397 #endif
398
399
400 if (set_bit(0, (void*)&dev->tbusy) != 0)
401 printk("%s: Transmitter access conflict.\n", dev->name);
402 else {
403
404 outw(skb->len, ioaddr + TX_FIFO);
405 outw(0x00, ioaddr + TX_FIFO);
406
407 outsl(ioaddr + TX_FIFO, (void *)(skb+1), (skb->len + 3) >> 2);
408
409 dev->trans_start = jiffies;
410 if (inw(ioaddr + TX_FREE) > 1536) {
411 dev->tbusy=0;
412 } else
413
414 outw(0x9000 + 1536, ioaddr + EL3_CMD);
415 }
416
417 if (skb->free)
418 kfree_skb (skb, FREE_WRITE);
419
420
421 {
422 short tx_status;
423 int i = 4;
424
425 while (--i > 0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
426 if (el3_debug > 5)
427 printk(" Tx status %4.4x.\n", tx_status);
428 if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
429 if (tx_status & 0x30) outw(0x5800, ioaddr + EL3_CMD);
430 if (tx_status & 0x3C) outw(0x4800, ioaddr + EL3_CMD);
431 outb(0x00, ioaddr + TX_STATUS);
432 }
433 }
434 return 0;
435 }
436
437
438 static void
439 el3_interrupt(int reg_ptr)
440 {
441 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
442 struct device *dev = (struct device *)(irq2dev_map[irq]);
443 int ioaddr, status;
444 int i = 0;
445
446 if (dev == NULL) {
447 printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
448 return;
449 }
450
451 if (dev->interrupt)
452 printk("%s: Re-entering the interrupt handler.\n", dev->name);
453 dev->interrupt = 1;
454
455 ioaddr = dev->base_addr;
456 status = inw(ioaddr + EL3_STATUS);
457
458 if (el3_debug > 4)
459 printk("%s: interrupt, status %4.4x.\n", dev->name, status);
460
461 while ((status = inw(ioaddr + EL3_STATUS)) & 0x01) {
462
463 if (status & 0x10)
464 el3_rx(dev);
465
466 if (status & 0x08) {
467 if (el3_debug > 5)
468 printk(" TX room bit was handled.\n");
469
470 outw(0x6808, ioaddr + EL3_CMD);
471 dev->tbusy = 0;
472 mark_bh(INET_BH);
473 }
474 if (status & 0x80)
475 update_stats(ioaddr, dev);
476
477 if (++i > 10) {
478 printk("%s: Infinite loop in interrupt, status %4.4x.\n",
479 dev->name, status);
480 break;
481 }
482
483 outw(0x6899, ioaddr + EL3_CMD);
484 }
485
486 if (el3_debug > 4) {
487 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
488 inw(ioaddr + EL3_STATUS));
489 }
490
491 dev->interrupt = 0;
492 return;
493 }
494
495
496 static struct enet_statistics *
497 el3_get_stats(struct device *dev)
498 {
499 struct el3_private *lp = (struct el3_private *)dev->priv;
500
501 sti();
502 update_stats(dev->base_addr, dev);
503 cli();
504 return &lp->stats;
505 }
506
507
508
509
510
511
512 static void update_stats(int ioaddr, struct device *dev)
513 {
514 struct el3_private *lp = (struct el3_private *)dev->priv;
515
516 if (el3_debug > 5)
517 printk(" Updating the statistics.\n");
518
519 outw(0xB000, ioaddr + EL3_CMD);
520
521 EL3WINDOW(6);
522 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
523 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
524 inb(ioaddr + 2);
525 lp->stats.collisions += inb(ioaddr + 3);
526 lp->stats.tx_window_errors += inb(ioaddr + 4);
527 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
528 lp->stats.tx_packets += inb(ioaddr + 6);
529 lp->stats.rx_packets += inb(ioaddr + 7);
530 inb(ioaddr + 8);
531 inw(ioaddr + 10);
532 inw(ioaddr + 12);
533
534
535 EL3WINDOW(1);
536 outw(0xA800, ioaddr + EL3_CMD);
537 return;
538 }
539
540 static int
541 el3_rx(struct device *dev)
542 {
543 struct el3_private *lp = (struct el3_private *)dev->priv;
544 int ioaddr = dev->base_addr;
545 short rx_status;
546
547 if (el3_debug > 5)
548 printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n",
549 inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
550 while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
551 if (rx_status & 0x4000) {
552 short error = rx_status & 0x3C00;
553 lp->stats.rx_errors++;
554 switch (error) {
555 case 0x2000: lp->stats.rx_over_errors++; break;
556 case 0x2C00: lp->stats.rx_length_errors++; break;
557 case 0x3400: lp->stats.rx_crc_errors++; break;
558 case 0x2400: lp->stats.rx_length_errors++; break;
559 case 0x3000: lp->stats.rx_frame_errors++; break;
560 case 0x0800: lp->stats.rx_frame_errors++; break;
561 }
562 }
563 if ( (! (rx_status & 0x4000))
564 || ! (rx_status & 0x2000)) {
565 short pkt_len = rx_status & 0x7ff;
566 int sksize = sizeof(struct sk_buff) + pkt_len + 3;
567 struct sk_buff *skb;
568
569 skb = alloc_skb(sksize, GFP_ATOMIC);
570 if (el3_debug > 4)
571 printk(" Receiving packet size %d status %4.4x.\n",
572 pkt_len, rx_status);
573 if (skb != NULL) {
574 skb->mem_len = sksize;
575 skb->mem_addr = skb;
576 skb->len = pkt_len;
577 skb->dev = dev;
578
579
580 insl(ioaddr+RX_FIFO, (void *)(skb+1),
581 (pkt_len + 3) >> 2);
582
583 #ifdef HAVE_NETIF_RX
584 netif_rx(skb);
585 outw(0x4000, ioaddr + EL3_CMD);
586 continue;
587 #else
588 skb->lock = 0;
589 if (dev_rint((unsigned char *)skb, pkt_len,
590 IN_SKBUFF,dev)== 0){
591 if (el3_debug > 6)
592 printk(" dev_rint() happy, status %4.4x.\n",
593 inb(ioaddr + EL3_STATUS));
594 outw(0x4000, ioaddr + EL3_CMD);
595 while (inw(ioaddr + EL3_STATUS) & 0x1000)
596 printk(" Waiting for 3c509 to discard packet, status %x.\n",
597 inw(ioaddr + EL3_STATUS) );
598 if (el3_debug > 6)
599 printk(" discarded packet, status %4.4x.\n",
600 inb(ioaddr + EL3_STATUS));
601 continue;
602 } else {
603 printk("%s: receive buffers full.\n", dev->name);
604 kfree_s(skb, sksize);
605 }
606 #endif
607 } else if (el3_debug)
608 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
609 dev->name, sksize);
610 }
611 lp->stats.rx_dropped++;
612 outw(0x4000, ioaddr + EL3_CMD);
613 while (inw(ioaddr + EL3_STATUS) & 0x1000)
614 printk(" Waiting for 3c509 to discard packet, status %x.\n",
615 inw(ioaddr + EL3_STATUS) );
616 }
617
618 if (el3_debug > 5)
619 printk(" Exiting rx_packet(), status %4.4x, rx_status %4.4x.\n",
620 inw(ioaddr+EL3_STATUS), inw(ioaddr+8));
621
622 return 0;
623 }
624
625 #ifdef HAVE_MULTICAST
626
627
628
629
630
631
632 static void
633 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
634 {
635 short ioaddr = dev->base_addr;
636 if (num_addrs > 0) {
637 outw(0x8007, ioaddr + EL3_CMD);
638 } else if (num_addrs < 0) {
639 outw(0x8008, ioaddr + EL3_CMD);
640 } else
641 outw(0x8005, ioaddr + EL3_CMD);
642 }
643 #endif
644
645 static int
646 el3_close(struct device *dev)
647 {
648 int ioaddr = dev->base_addr;
649
650 if (el3_debug > 2)
651 printk("%s: Shutting down ethercard.\n", dev->name);
652
653 dev->tbusy = 1;
654 dev->start = 0;
655
656
657 outw(0xB000, ioaddr + EL3_CMD);
658
659
660 outw(0x1800, ioaddr + EL3_CMD);
661 outw(0x5000, ioaddr + EL3_CMD);
662
663 if (dev->if_port == 3)
664
665 outw(0xb800, ioaddr + EL3_CMD);
666 else if (dev->if_port == 0) {
667
668 EL3WINDOW(4);
669 outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
670 }
671
672 free_irq(dev->irq);
673
674 EL3WINDOW(0);
675
676 outw(0x0f00, ioaddr + 8);
677
678
679 irq2dev_map[dev->irq] = 0;
680
681 update_stats(ioaddr, dev);
682 return 0;
683 }
684
685
686
687
688
689
690
691
692