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