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