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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102#define DRV_NAME "3c501"
103#define DRV_VERSION "2002/10/09"
104
105
106static const char version[] =
107 DRV_NAME ".c: " DRV_VERSION " Alan Cox (alan@lxorguk.ukuu.org.uk).\n";
108
109
110
111
112
113
114#include <linux/module.h>
115
116#include <linux/kernel.h>
117#include <linux/fcntl.h>
118#include <linux/ioport.h>
119#include <linux/interrupt.h>
120#include <linux/slab.h>
121#include <linux/string.h>
122#include <linux/errno.h>
123#include <linux/spinlock.h>
124#include <linux/ethtool.h>
125#include <linux/delay.h>
126#include <linux/bitops.h>
127
128#include <asm/uaccess.h>
129#include <asm/io.h>
130
131#include <linux/netdevice.h>
132#include <linux/etherdevice.h>
133#include <linux/skbuff.h>
134#include <linux/init.h>
135
136#include "3c501.h"
137
138
139
140
141
142static int io = 0x280;
143static int irq = 5;
144static int mem_start;
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159struct net_device * __init el1_probe(int unit)
160{
161 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
162 static unsigned ports[] = { 0x280, 0x300, 0};
163 unsigned *port;
164 int err = 0;
165
166 if (!dev)
167 return ERR_PTR(-ENOMEM);
168
169 if (unit >= 0) {
170 sprintf(dev->name, "eth%d", unit);
171 netdev_boot_setup_check(dev);
172 io = dev->base_addr;
173 irq = dev->irq;
174 mem_start = dev->mem_start & 7;
175 }
176
177 if (io > 0x1ff) {
178 err = el1_probe1(dev, io);
179 } else if (io != 0) {
180 err = -ENXIO;
181 } else {
182 for (port = ports; *port && el1_probe1(dev, *port); port++)
183 ;
184 if (!*port)
185 err = -ENODEV;
186 }
187 if (err)
188 goto out;
189 err = register_netdev(dev);
190 if (err)
191 goto out1;
192 return dev;
193out1:
194 release_region(dev->base_addr, EL1_IO_EXTENT);
195out:
196 free_netdev(dev);
197 return ERR_PTR(err);
198}
199
200static const struct net_device_ops el_netdev_ops = {
201 .ndo_open = el_open,
202 .ndo_stop = el1_close,
203 .ndo_start_xmit = el_start_xmit,
204 .ndo_tx_timeout = el_timeout,
205 .ndo_set_multicast_list = set_multicast_list,
206 .ndo_change_mtu = eth_change_mtu,
207 .ndo_set_mac_address = eth_mac_addr,
208 .ndo_validate_addr = eth_validate_addr,
209};
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224static int __init el1_probe1(struct net_device *dev, int ioaddr)
225{
226 struct net_local *lp;
227 const char *mname;
228 unsigned char station_addr[6];
229 int autoirq = 0;
230 int i;
231
232
233
234
235
236 if (!request_region(ioaddr, EL1_IO_EXTENT, DRV_NAME))
237 return -ENODEV;
238
239
240
241
242
243 for (i = 0; i < 6; i++) {
244 outw(i, ioaddr + EL1_DATAPTR);
245 station_addr[i] = inb(ioaddr + EL1_SAPROM);
246 }
247
248
249
250
251
252 if (station_addr[0] == 0x02 && station_addr[1] == 0x60
253 && station_addr[2] == 0x8c)
254 mname = "3c501";
255 else if (station_addr[0] == 0x00 && station_addr[1] == 0x80
256 && station_addr[2] == 0xC8)
257 mname = "NP943";
258 else {
259 release_region(ioaddr, EL1_IO_EXTENT);
260 return -ENODEV;
261 }
262
263
264
265
266
267
268 dev->irq = irq;
269
270 if (dev->irq < 2) {
271 unsigned long irq_mask;
272
273 irq_mask = probe_irq_on();
274 inb(RX_STATUS);
275 inb(TX_STATUS);
276 outb(AX_LOOP + 1, AX_CMD);
277
278 outb(0x00, AX_CMD);
279
280 mdelay(20);
281 autoirq = probe_irq_off(irq_mask);
282
283 if (autoirq == 0) {
284 pr_warning("%s probe at %#x failed to detect IRQ line.\n",
285 mname, ioaddr);
286 release_region(ioaddr, EL1_IO_EXTENT);
287 return -EAGAIN;
288 }
289 }
290
291 outb(AX_RESET+AX_LOOP, AX_CMD);
292 dev->base_addr = ioaddr;
293 memcpy(dev->dev_addr, station_addr, ETH_ALEN);
294
295 if (mem_start & 0xf)
296 el_debug = mem_start & 0x7;
297 if (autoirq)
298 dev->irq = autoirq;
299
300 pr_info("%s: %s EtherLink at %#lx, using %sIRQ %d.\n",
301 dev->name, mname, dev->base_addr,
302 autoirq ? "auto":"assigned ", dev->irq);
303
304#ifdef CONFIG_IP_MULTICAST
305 pr_warning("WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
306#endif
307
308 if (el_debug)
309 pr_debug("%s", version);
310
311 lp = netdev_priv(dev);
312 memset(lp, 0, sizeof(struct net_local));
313 spin_lock_init(&lp->lock);
314
315
316
317
318
319 dev->netdev_ops = &el_netdev_ops;
320 dev->watchdog_timeo = HZ;
321 dev->ethtool_ops = &netdev_ethtool_ops;
322 return 0;
323}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338static int el_open(struct net_device *dev)
339{
340 int retval;
341 int ioaddr = dev->base_addr;
342 struct net_local *lp = netdev_priv(dev);
343 unsigned long flags;
344
345 if (el_debug > 2)
346 pr_debug("%s: Doing el_open()...\n", dev->name);
347
348 retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev);
349 if (retval)
350 return retval;
351
352 spin_lock_irqsave(&lp->lock, flags);
353 el_reset(dev);
354 spin_unlock_irqrestore(&lp->lock, flags);
355
356 lp->txing = 0;
357 outb(AX_RX, AX_CMD);
358 netif_start_queue(dev);
359 return 0;
360}
361
362
363
364
365
366
367
368
369
370
371static void el_timeout(struct net_device *dev)
372{
373 struct net_local *lp = netdev_priv(dev);
374 int ioaddr = dev->base_addr;
375
376 if (el_debug)
377 pr_debug("%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
378 dev->name, inb(TX_STATUS),
379 inb(AX_STATUS), inb(RX_STATUS));
380 dev->stats.tx_errors++;
381 outb(TX_NORM, TX_CMD);
382 outb(RX_NORM, RX_CMD);
383 outb(AX_OFF, AX_CMD);
384 outb(AX_RX, AX_CMD);
385 lp->txing = 0;
386 netif_wake_queue(dev);
387}
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412static netdev_tx_t el_start_xmit(struct sk_buff *skb, struct net_device *dev)
413{
414 struct net_local *lp = netdev_priv(dev);
415 int ioaddr = dev->base_addr;
416 unsigned long flags;
417
418
419
420
421
422
423
424 spin_lock_irqsave(&lp->lock, flags);
425
426
427
428
429
430 netif_stop_queue(dev);
431
432 do {
433 int len = skb->len;
434 int pad = 0;
435 int gp_start;
436 unsigned char *buf = skb->data;
437
438 if (len < ETH_ZLEN)
439 pad = ETH_ZLEN - len;
440
441 gp_start = 0x800 - (len + pad);
442
443 lp->tx_pkt_start = gp_start;
444 lp->collisions = 0;
445
446 dev->stats.tx_bytes += skb->len;
447
448
449
450
451
452
453 outb_p(AX_SYS, AX_CMD);
454 inb_p(RX_STATUS);
455 inb_p(TX_STATUS);
456
457 lp->loading = 1;
458 lp->txing = 1;
459
460
461
462
463
464
465 spin_unlock_irqrestore(&lp->lock, flags);
466
467
468 outw(0x00, RX_BUF_CLR);
469
470 outw(gp_start, GP_LOW);
471
472 outsb(DATAPORT, buf, len);
473 if (pad) {
474 while (pad--)
475 outb(0, DATAPORT);
476 }
477
478 outw(gp_start, GP_LOW);
479
480 if (lp->loading != 2) {
481
482 outb(AX_XMIT, AX_CMD);
483 lp->loading = 0;
484 dev->trans_start = jiffies;
485 if (el_debug > 2)
486 pr_debug(" queued xmit.\n");
487 dev_kfree_skb(skb);
488 return NETDEV_TX_OK;
489 }
490
491 if (el_debug > 2)
492 pr_debug("%s: burped during tx load.\n", dev->name);
493 spin_lock_irqsave(&lp->lock, flags);
494 } while (1);
495}
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519static irqreturn_t el_interrupt(int irq, void *dev_id)
520{
521 struct net_device *dev = dev_id;
522 struct net_local *lp;
523 int ioaddr;
524 int axsr;
525
526 ioaddr = dev->base_addr;
527 lp = netdev_priv(dev);
528
529 spin_lock(&lp->lock);
530
531
532
533
534
535 axsr = inb(AX_STATUS);
536
537
538
539
540
541 if (el_debug > 3)
542 pr_debug("%s: el_interrupt() aux=%#02x\n", dev->name, axsr);
543
544 if (lp->loading == 1 && !lp->txing)
545 pr_warning("%s: Inconsistent state loading while not in tx\n",
546 dev->name);
547
548 if (lp->txing) {
549
550
551
552
553 int txsr = inb(TX_STATUS);
554
555 if (lp->loading == 1) {
556 if (el_debug > 2)
557 pr_debug("%s: Interrupt while loading [txsr=%02x gp=%04x rp=%04x]\n",
558 dev->name, txsr, inw(GP_LOW), inw(RX_LOW));
559
560
561 lp->loading = 2;
562 spin_unlock(&lp->lock);
563 goto out;
564 }
565 if (el_debug > 6)
566 pr_debug("%s: txsr=%02x gp=%04x rp=%04x\n", dev->name,
567 txsr, inw(GP_LOW), inw(RX_LOW));
568
569 if ((axsr & 0x80) && (txsr & TX_READY) == 0) {
570
571
572
573
574 if (el_debug > 1)
575 pr_debug("%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x gp=%03x rp=%03x.\n",
576 dev->name, txsr, axsr,
577 inw(ioaddr + EL1_DATAPTR),
578 inw(ioaddr + EL1_RXPTR));
579 lp->txing = 0;
580 netif_wake_queue(dev);
581 } else if (txsr & TX_16COLLISIONS) {
582
583
584
585 if (el_debug)
586 pr_debug("%s: Transmit failed 16 times, Ethernet jammed?\n", dev->name);
587 outb(AX_SYS, AX_CMD);
588 lp->txing = 0;
589 dev->stats.tx_aborted_errors++;
590 netif_wake_queue(dev);
591 } else if (txsr & TX_COLLISION) {
592
593
594
595
596 if (el_debug > 6)
597 pr_debug("%s: retransmitting after a collision.\n", dev->name);
598
599
600
601
602
603 outb(AX_SYS, AX_CMD);
604 outw(lp->tx_pkt_start, GP_LOW);
605 outb(AX_XMIT, AX_CMD);
606 dev->stats.collisions++;
607 spin_unlock(&lp->lock);
608 goto out;
609 } else {
610
611
612
613 dev->stats.tx_packets++;
614 if (el_debug > 6)
615 pr_debug("%s: Tx succeeded %s\n", dev->name,
616 (txsr & TX_RDY) ? "." : "but tx is busy!");
617
618
619
620 lp->txing = 0;
621
622 netif_wake_queue(dev);
623 }
624 } else {
625
626
627
628
629 int rxsr = inb(RX_STATUS);
630 if (el_debug > 5)
631 pr_debug("%s: rxsr=%02x txsr=%02x rp=%04x\n",
632 dev->name, rxsr, inb(TX_STATUS), inw(RX_LOW));
633
634
635
636 if (rxsr & RX_MISSED)
637 dev->stats.rx_missed_errors++;
638 else if (rxsr & RX_RUNT) {
639
640 dev->stats.rx_length_errors++;
641 if (el_debug > 5)
642 pr_debug("%s: runt.\n", dev->name);
643 } else if (rxsr & RX_GOOD) {
644
645
646
647 el_receive(dev);
648 } else {
649
650
651
652 if (el_debug > 2)
653 pr_debug("%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
654 dev->name, rxsr);
655 el_reset(dev);
656 }
657 }
658
659
660
661
662
663 outb(AX_RX, AX_CMD);
664 outw(0x00, RX_BUF_CLR);
665 inb(RX_STATUS);
666 inb(TX_STATUS);
667 spin_unlock(&lp->lock);
668out:
669 return IRQ_HANDLED;
670}
671
672
673
674
675
676
677
678
679
680
681
682
683static void el_receive(struct net_device *dev)
684{
685 int ioaddr = dev->base_addr;
686 int pkt_len;
687 struct sk_buff *skb;
688
689 pkt_len = inw(RX_LOW);
690
691 if (el_debug > 4)
692 pr_debug(" el_receive %d.\n", pkt_len);
693
694 if (pkt_len < 60 || pkt_len > 1536) {
695 if (el_debug)
696 pr_debug("%s: bogus packet, length=%d\n",
697 dev->name, pkt_len);
698 dev->stats.rx_over_errors++;
699 return;
700 }
701
702
703
704
705
706 outb(AX_SYS, AX_CMD);
707 skb = dev_alloc_skb(pkt_len+2);
708
709
710
711
712
713 outw(0x00, GP_LOW);
714 if (skb == NULL) {
715 pr_info("%s: Memory squeeze, dropping packet.\n", dev->name);
716 dev->stats.rx_dropped++;
717 return;
718 } else {
719 skb_reserve(skb, 2);
720
721
722
723
724
725 insb(DATAPORT, skb_put(skb, pkt_len), pkt_len);
726 skb->protocol = eth_type_trans(skb, dev);
727 netif_rx(skb);
728 dev->stats.rx_packets++;
729 dev->stats.rx_bytes += pkt_len;
730 }
731 return;
732}
733
734
735
736
737
738
739
740
741
742
743static void el_reset(struct net_device *dev)
744{
745 struct net_local *lp = netdev_priv(dev);
746 int ioaddr = dev->base_addr;
747
748 if (el_debug > 2)
749 pr_info("3c501 reset...\n");
750 outb(AX_RESET, AX_CMD);
751
752 outb(AX_LOOP, AX_CMD);
753 {
754 int i;
755 for (i = 0; i < 6; i++)
756 outb(dev->dev_addr[i], ioaddr + i);
757 }
758
759 outw(0, RX_BUF_CLR);
760 outb(TX_NORM, TX_CMD);
761 outb(RX_NORM, RX_CMD);
762 inb(RX_STATUS);
763 inb(TX_STATUS);
764 lp->txing = 0;
765}
766
767
768
769
770
771
772
773
774
775
776
777
778static int el1_close(struct net_device *dev)
779{
780 int ioaddr = dev->base_addr;
781
782 if (el_debug > 2)
783 pr_info("%s: Shutting down Ethernet card at %#x.\n",
784 dev->name, ioaddr);
785
786 netif_stop_queue(dev);
787
788
789
790
791
792 free_irq(dev->irq, dev);
793 outb(AX_RESET, AX_CMD);
794
795 return 0;
796}
797
798
799
800
801
802
803
804
805
806
807
808static void set_multicast_list(struct net_device *dev)
809{
810 int ioaddr = dev->base_addr;
811
812 if (dev->flags & IFF_PROMISC) {
813 outb(RX_PROM, RX_CMD);
814 inb(RX_STATUS);
815 } else if (dev->mc_list || dev->flags & IFF_ALLMULTI) {
816
817 outb(RX_MULT, RX_CMD);
818 inb(RX_STATUS);
819 } else {
820 outb(RX_NORM, RX_CMD);
821 inb(RX_STATUS);
822 }
823}
824
825
826static void netdev_get_drvinfo(struct net_device *dev,
827 struct ethtool_drvinfo *info)
828{
829 strcpy(info->driver, DRV_NAME);
830 strcpy(info->version, DRV_VERSION);
831 sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
832}
833
834static u32 netdev_get_msglevel(struct net_device *dev)
835{
836 return debug;
837}
838
839static void netdev_set_msglevel(struct net_device *dev, u32 level)
840{
841 debug = level;
842}
843
844static const struct ethtool_ops netdev_ethtool_ops = {
845 .get_drvinfo = netdev_get_drvinfo,
846 .get_msglevel = netdev_get_msglevel,
847 .set_msglevel = netdev_set_msglevel,
848};
849
850#ifdef MODULE
851
852static struct net_device *dev_3c501;
853
854module_param(io, int, 0);
855module_param(irq, int, 0);
856MODULE_PARM_DESC(io, "EtherLink I/O base address");
857MODULE_PARM_DESC(irq, "EtherLink IRQ number");
858
859
860
861
862
863
864
865
866
867
868
869
870
871int __init init_module(void)
872{
873 dev_3c501 = el1_probe(-1);
874 if (IS_ERR(dev_3c501))
875 return PTR_ERR(dev_3c501);
876 return 0;
877}
878
879
880
881
882
883
884
885
886void __exit cleanup_module(void)
887{
888 struct net_device *dev = dev_3c501;
889 unregister_netdev(dev);
890 release_region(dev->base_addr, EL1_IO_EXTENT);
891 free_netdev(dev);
892}
893
894#endif
895
896MODULE_AUTHOR("Donald Becker, Alan Cox");
897MODULE_DESCRIPTION("Support for the ancient 3Com 3c501 ethernet card");
898MODULE_LICENSE("GPL");
899
900