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