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