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#define DRV_NAME "3c509"
55#define DRV_VERSION "1.18c"
56#define DRV_RELDATE "1Mar2002"
57
58
59
60
61#define TX_TIMEOUT (400*HZ/1000)
62
63static int max_interrupt_work = 10;
64
65#include <linux/config.h>
66#include <linux/module.h>
67
68#include <linux/mca.h>
69#include <linux/isapnp.h>
70#include <linux/sched.h>
71#include <linux/string.h>
72#include <linux/interrupt.h>
73#include <linux/errno.h>
74#include <linux/in.h>
75#include <linux/slab.h>
76#include <linux/ioport.h>
77#include <linux/init.h>
78#include <linux/netdevice.h>
79#include <linux/etherdevice.h>
80#include <linux/skbuff.h>
81#include <linux/delay.h>
82#include <linux/spinlock.h>
83#include <linux/ethtool.h>
84
85#include <asm/uaccess.h>
86#include <asm/bitops.h>
87#include <asm/io.h>
88#include <asm/irq.h>
89#include <linux/pm.h>
90
91static char versionA[] __initdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
92static char versionB[] __initdata = "http://www.scyld.com/network/3c509.html\n";
93
94#ifdef EL3_DEBUG
95static int el3_debug = EL3_DEBUG;
96#else
97static int el3_debug = 2;
98#endif
99
100
101
102
103
104
105#define EL3_DATA 0x00
106#define EL3_CMD 0x0e
107#define EL3_STATUS 0x0e
108#define EEPROM_READ 0x80
109
110#define EL3_IO_EXTENT 16
111
112#define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
113
114
115
116
117enum c509cmd {
118 TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
119 RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
120 TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
121 FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
122 SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
123 SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
124 StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
125 PowerDown = 28<<11, PowerAuto = 29<<11};
126
127enum c509status {
128 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
129 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
130 IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
131
132
133enum RxFilter {
134 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
135
136
137#define TX_FIFO 0x00
138#define RX_FIFO 0x00
139#define RX_STATUS 0x08
140#define TX_STATUS 0x0B
141#define TX_FREE 0x0C
142
143#define WN0_IRQ 0x08
144#define WN4_MEDIA 0x0A
145#define MEDIA_TP 0x00C0
146#define WN4_NETDIAG 0x06
147#define FD_ENABLE 0x8000
148
149
150
151
152
153#define SKB_QUEUE_SIZE 64
154
155struct el3_private {
156 struct net_device_stats stats;
157 struct net_device *next_dev;
158 spinlock_t lock;
159
160 int head, size;
161 struct sk_buff *queue[SKB_QUEUE_SIZE];
162 char mca_slot;
163#ifdef CONFIG_PM
164 struct pm_dev *pmdev;
165#endif
166};
167static int id_port __initdata = 0x110;
168static struct net_device *el3_root_dev;
169
170static ushort id_read_eeprom(int index);
171static ushort read_eeprom(int ioaddr, int index);
172static int el3_open(struct net_device *dev);
173static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
174static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
175static void update_stats(struct net_device *dev);
176static struct net_device_stats *el3_get_stats(struct net_device *dev);
177static int el3_rx(struct net_device *dev);
178static int el3_close(struct net_device *dev);
179static void set_multicast_list(struct net_device *dev);
180static void el3_tx_timeout (struct net_device *dev);
181static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
182static void el3_down(struct net_device *dev);
183static void el3_up(struct net_device *dev);
184#ifdef CONFIG_PM
185static int el3_suspend(struct pm_dev *pdev);
186static int el3_resume(struct pm_dev *pdev);
187static int el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data);
188#endif
189
190#ifdef CONFIG_MCA
191struct el3_mca_adapters_struct {
192 char* name;
193 int id;
194};
195
196static struct el3_mca_adapters_struct el3_mca_adapters[] __initdata = {
197 { "3Com 3c529 EtherLink III (10base2)", 0x627c },
198 { "3Com 3c529 EtherLink III (10baseT)", 0x627d },
199 { "3Com 3c529 EtherLink III (test mode)", 0x62db },
200 { "3Com 3c529 EtherLink III (TP or coax)", 0x62f6 },
201 { "3Com 3c529 EtherLink III (TP)", 0x62f7 },
202 { NULL, 0 },
203};
204#endif
205
206#ifdef __ISAPNP__
207static struct isapnp_device_id el3_isapnp_adapters[] __initdata = {
208 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
209 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090),
210 (long) "3Com Etherlink III (TP)" },
211 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
212 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091),
213 (long) "3Com Etherlink III" },
214 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
215 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094),
216 (long) "3Com Etherlink III (combo)" },
217 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
218 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095),
219 (long) "3Com Etherlink III (TPO)" },
220 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
221 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098),
222 (long) "3Com Etherlink III (TPC)" },
223 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
224 ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f7),
225 (long) "3Com Etherlink III compatible" },
226 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
227 ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8),
228 (long) "3Com Etherlink III compatible" },
229 { }
230};
231
232static u16 el3_isapnp_phys_addr[8][3];
233#endif
234static int nopnp;
235
236int __init el3_probe(struct net_device *dev, int card_idx)
237{
238 struct el3_private *lp;
239 short lrs_state = 0xff, i;
240 int ioaddr, irq, if_port;
241 u16 phys_addr[3];
242 static int current_tag;
243 int mca_slot = -1;
244#ifdef __ISAPNP__
245 static int pnp_cards;
246#endif
247
248 if (dev) SET_MODULE_OWNER(dev);
249
250
251
252 if (EISA_bus) {
253 static int eisa_addr = 0x1000;
254 while (eisa_addr < 0x9000) {
255 int device_id;
256
257 ioaddr = eisa_addr;
258 eisa_addr += 0x1000;
259
260
261 if (inw(ioaddr + 0xC80) != 0x6d50)
262 continue;
263
264
265 device_id = (inb(ioaddr + 0xC82)<<8) + inb(ioaddr + 0xC83);
266 if ((device_id & 0xFF00) == 0x5900) {
267 continue;
268 }
269
270
271 outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
272
273 irq = inw(ioaddr + WN0_IRQ) >> 12;
274 if_port = inw(ioaddr + 6)>>14;
275 for (i = 0; i < 3; i++)
276 phys_addr[i] = htons(read_eeprom(ioaddr, i));
277
278
279 read_eeprom(ioaddr, 3);
280
281
282 goto found;
283 }
284 }
285
286#ifdef CONFIG_MCA
287
288
289
290
291
292
293
294
295 if( MCA_bus ) {
296 int slot, j;
297 u_char pos4, pos5;
298
299 for( j = 0; el3_mca_adapters[j].name != NULL; j ++ ) {
300 slot = 0;
301 while( slot != MCA_NOTFOUND ) {
302 slot = mca_find_unused_adapter(
303 el3_mca_adapters[j].id, slot );
304 if( slot == MCA_NOTFOUND ) break;
305
306
307
308
309
310 pos4 = mca_read_stored_pos( slot, 4 );
311 pos5 = mca_read_stored_pos( slot, 5 );
312
313 ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
314 irq = pos5 & 0x0f;
315
316
317 if(dev && ((dev->irq >= 1 && dev->irq != irq) ||
318 (dev->base_addr >= 1 && dev->base_addr != ioaddr))) {
319 slot++;
320 continue;
321 }
322
323 printk("3c509: found %s at slot %d\n",
324 el3_mca_adapters[j].name, slot + 1 );
325
326
327 mca_set_adapter_name(slot, el3_mca_adapters[j].name);
328 mca_set_adapter_procfn(slot, NULL, NULL);
329 mca_mark_as_used(slot);
330
331 if_port = pos4 & 0x03;
332 if (el3_debug > 2) {
333 printk("3c529: irq %d ioaddr 0x%x ifport %d\n", irq, ioaddr, if_port);
334 }
335 EL3WINDOW(0);
336 for (i = 0; i < 3; i++) {
337 phys_addr[i] = htons(read_eeprom(ioaddr, i));
338 }
339
340 mca_slot = slot;
341
342 goto found;
343 }
344 }
345
346 return -ENODEV;
347 }
348#endif
349
350#ifdef __ISAPNP__
351 if (nopnp == 1)
352 goto no_pnp;
353
354 for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
355 struct pci_dev *idev = NULL;
356 int j;
357 while ((idev = isapnp_find_dev(NULL,
358 el3_isapnp_adapters[i].vendor,
359 el3_isapnp_adapters[i].function,
360 idev))) {
361 idev->prepare(idev);
362
363
364 idev->deactivate(idev);
365 idev->activate(idev);
366 if (!idev->resource[0].start || check_region(idev->resource[0].start, EL3_IO_EXTENT))
367 continue;
368 ioaddr = idev->resource[0].start;
369 if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP"))
370 return -EBUSY;
371 irq = idev->irq_resource[0].start;
372 if (el3_debug > 3)
373 printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
374 (char*) el3_isapnp_adapters[i].driver_data, ioaddr, irq);
375 EL3WINDOW(0);
376 for (j = 0; j < 3; j++)
377 el3_isapnp_phys_addr[pnp_cards][j] =
378 phys_addr[j] =
379 htons(read_eeprom(ioaddr, j));
380 if_port = read_eeprom(ioaddr, 8) >> 14;
381 pnp_cards++;
382 goto found;
383 }
384 }
385no_pnp:
386#endif
387
388
389 for ( ; id_port < 0x200; id_port += 0x10) {
390 if (check_region(id_port, 1))
391 continue;
392 outb(0x00, id_port);
393 outb(0xff, id_port);
394 if (inb(id_port) & 0x01)
395 break;
396 }
397 if (id_port >= 0x200) {
398
399 printk(" WARNING: No I/O port available for 3c509 activation.\n");
400 return -ENODEV;
401 }
402
403
404
405
406
407 outb(0x00, id_port);
408 outb(0x00, id_port);
409 for(i = 0; i < 255; i++) {
410 outb(lrs_state, id_port);
411 lrs_state <<= 1;
412 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
413 }
414
415
416 if (current_tag == 0)
417 outb(0xd0, id_port);
418 else
419 outb(0xd8, id_port);
420
421 if (id_read_eeprom(7) != 0x6d50) {
422 return -ENODEV;
423 }
424
425
426
427
428 for (i = 0; i < 3; i++) {
429 phys_addr[i] = htons(id_read_eeprom(i));
430 }
431
432#ifdef __ISAPNP__
433 if (nopnp == 0) {
434
435
436 for (i = 0; i < pnp_cards; i++) {
437 if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
438 phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
439 phys_addr[2] == el3_isapnp_phys_addr[i][2])
440 {
441 if (el3_debug > 3)
442 printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
443 phys_addr[0] & 0xff, phys_addr[0] >> 8,
444 phys_addr[1] & 0xff, phys_addr[1] >> 8,
445 phys_addr[2] & 0xff, phys_addr[2] >> 8);
446
447 outb(0xd0 + ++current_tag, id_port);
448 goto no_pnp;
449 }
450 }
451 }
452#endif
453
454 {
455 unsigned int iobase = id_read_eeprom(8);
456 if_port = iobase >> 14;
457 ioaddr = 0x200 + ((iobase & 0x1f) << 4);
458 }
459 irq = id_read_eeprom(9) >> 12;
460
461 if (dev) {
462 if (dev->irq > 1 && dev->irq < 16)
463 irq = dev->irq;
464
465 if (dev->base_addr) {
466 if (dev->mem_end == 0x3c509
467 && dev->base_addr >= 0x200 && dev->base_addr <= 0x3e0)
468 ioaddr = dev->base_addr & 0x3f0;
469 else if (dev->base_addr != ioaddr)
470 return -ENODEV;
471 }
472 }
473
474 if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
475 return -EBUSY;
476
477
478 outb(0xd0 + ++current_tag, id_port);
479
480
481 outb((ioaddr >> 4) | 0xe0, id_port);
482
483 EL3WINDOW(0);
484 if (inw(ioaddr) != 0x6d50) {
485 release_region(ioaddr, EL3_IO_EXTENT);
486 return -ENODEV;
487 }
488
489
490 outw(0x0f00, ioaddr + WN0_IRQ);
491 found:
492 if (dev == NULL) {
493 dev = init_etherdev(dev, sizeof(struct el3_private));
494 if (dev == NULL) {
495 release_region(ioaddr, EL3_IO_EXTENT);
496 return -ENOMEM;
497 }
498 SET_MODULE_OWNER(dev);
499 }
500 memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
501 dev->base_addr = ioaddr;
502 dev->irq = irq;
503
504 if (dev->mem_start & 0x05) {
505 dev->if_port = (dev->mem_start & 0x0f);
506 } else {
507
508 dev->if_port = (if_port | (dev->mem_start & 0x08) );
509 }
510
511 {
512 const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
513 printk("%s: 3c5x9 at %#3.3lx, %s port, address ",
514 dev->name, dev->base_addr, if_names[(dev->if_port & 0x03)]);
515 }
516
517
518 for (i = 0; i < 6; i++)
519 printk(" %2.2x", dev->dev_addr[i]);
520 printk(", IRQ %d.\n", dev->irq);
521
522
523 if (dev->priv == NULL)
524 dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL);
525 if (dev->priv == NULL)
526 return -ENOMEM;
527 memset(dev->priv, 0, sizeof(struct el3_private));
528
529 lp = dev->priv;
530 lp->mca_slot = mca_slot;
531 lp->next_dev = el3_root_dev;
532 spin_lock_init(&lp->lock);
533 el3_root_dev = dev;
534
535 if (el3_debug > 0)
536 printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
537
538
539 dev->open = &el3_open;
540 dev->hard_start_xmit = &el3_start_xmit;
541 dev->stop = &el3_close;
542 dev->get_stats = &el3_get_stats;
543 dev->set_multicast_list = &set_multicast_list;
544 dev->tx_timeout = el3_tx_timeout;
545 dev->watchdog_timeo = TX_TIMEOUT;
546 dev->do_ioctl = netdev_ioctl;
547
548#ifdef CONFIG_PM
549
550 lp->pmdev = pm_register(PM_ISA_DEV, card_idx, el3_pm_callback);
551 if (lp->pmdev) {
552 struct pm_dev *p;
553 p = lp->pmdev;
554 p->data = (struct net_device *)dev;
555 }
556#endif
557
558
559 ether_setup(dev);
560 return 0;
561}
562
563
564
565
566static ushort read_eeprom(int ioaddr, int index)
567{
568 outw(EEPROM_READ + index, ioaddr + 10);
569
570 udelay (500);
571 return inw(ioaddr + 12);
572}
573
574
575static ushort __init id_read_eeprom(int index)
576{
577 int bit, word = 0;
578
579
580
581 outb(EEPROM_READ + index, id_port);
582
583
584 udelay (500);
585
586 for (bit = 15; bit >= 0; bit--)
587 word = (word << 1) + (inb(id_port) & 0x01);
588
589 if (el3_debug > 3)
590 printk(" 3c509 EEPROM word %d %#4.4x.\n", index, word);
591
592 return word;
593}
594
595
596static int
597el3_open(struct net_device *dev)
598{
599 int ioaddr = dev->base_addr;
600 int i;
601
602 outw(TxReset, ioaddr + EL3_CMD);
603 outw(RxReset, ioaddr + EL3_CMD);
604 outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
605
606 i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
607 if (i) return i;
608
609 EL3WINDOW(0);
610 if (el3_debug > 3)
611 printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev->name,
612 dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
613
614 el3_up(dev);
615
616 if (el3_debug > 3)
617 printk("%s: Opened 3c509 IRQ %d status %4.4x.\n",
618 dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
619
620 return 0;
621}
622
623static void
624el3_tx_timeout (struct net_device *dev)
625{
626 struct el3_private *lp = (struct el3_private *)dev->priv;
627 int ioaddr = dev->base_addr;
628
629
630 printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
631 "Tx FIFO room %d.\n",
632 dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
633 inw(ioaddr + TX_FREE));
634 lp->stats.tx_errors++;
635 dev->trans_start = jiffies;
636
637 outw(TxReset, ioaddr + EL3_CMD);
638 outw(TxEnable, ioaddr + EL3_CMD);
639 netif_wake_queue(dev);
640}
641
642
643static int
644el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
645{
646 struct el3_private *lp = (struct el3_private *)dev->priv;
647 int ioaddr = dev->base_addr;
648 unsigned long flags;
649
650 netif_stop_queue (dev);
651
652 lp->stats.tx_bytes += skb->len;
653
654 if (el3_debug > 4) {
655 printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
656 dev->name, skb->len, inw(ioaddr + EL3_STATUS));
657 }
658#if 0
659#ifndef final_version
660 {
661 ushort status = inw(ioaddr + EL3_STATUS);
662 if (status & 0x0001
663 && inw(ioaddr + EL3_STATUS) & 1) {
664 printk("%s: Missed interrupt, status then %04x now %04x"
665 " Tx %2.2x Rx %4.4x.\n", dev->name, status,
666 inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
667 inw(ioaddr + RX_STATUS));
668
669 outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
670 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
671 ioaddr + EL3_CMD);
672 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
673 }
674 }
675#endif
676#endif
677
678
679
680
681
682
683
684
685
686
687
688 spin_lock_irqsave(&lp->lock, flags);
689
690
691 outw(skb->len, ioaddr + TX_FIFO);
692 outw(0x00, ioaddr + TX_FIFO);
693
694#ifdef __powerpc__
695 outsl_ns(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
696#else
697 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
698#endif
699
700 dev->trans_start = jiffies;
701 if (inw(ioaddr + TX_FREE) > 1536)
702 netif_start_queue(dev);
703 else
704
705 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
706
707 spin_unlock_irqrestore(&lp->lock, flags);
708
709 dev_kfree_skb (skb);
710
711
712 {
713 short tx_status;
714 int i = 4;
715
716 while (--i > 0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
717 if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
718 if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
719 if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
720 outb(0x00, ioaddr + TX_STATUS);
721 }
722 }
723 return 0;
724}
725
726
727static void
728el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
729{
730 struct net_device *dev = (struct net_device *)dev_id;
731 struct el3_private *lp;
732 int ioaddr, status;
733 int i = max_interrupt_work;
734
735 if (dev == NULL) {
736 printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
737 return;
738 }
739
740 lp = (struct el3_private *)dev->priv;
741 spin_lock(&lp->lock);
742
743 ioaddr = dev->base_addr;
744
745 if (el3_debug > 4) {
746 status = inw(ioaddr + EL3_STATUS);
747 printk("%s: interrupt, status %4.4x.\n", dev->name, status);
748 }
749
750 while ((status = inw(ioaddr + EL3_STATUS)) &
751 (IntLatch | RxComplete | StatsFull)) {
752
753 if (status & RxComplete)
754 el3_rx(dev);
755
756 if (status & TxAvailable) {
757 if (el3_debug > 5)
758 printk(" TX room bit was handled.\n");
759
760 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
761 netif_wake_queue (dev);
762 }
763 if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
764
765 if (status & StatsFull)
766 update_stats(dev);
767 if (status & RxEarly) {
768 el3_rx(dev);
769 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
770 }
771 if (status & TxComplete) {
772 struct el3_private *lp = (struct el3_private *)dev->priv;
773 short tx_status;
774 int i = 4;
775
776 while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
777 if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
778 if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
779 if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
780 outb(0x00, ioaddr + TX_STATUS);
781 }
782 }
783 if (status & AdapterFailure) {
784
785 outw(RxReset, ioaddr + EL3_CMD);
786
787 outw(SetRxFilter | RxStation | RxBroadcast
788 | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
789 | (dev->flags & IFF_PROMISC ? RxProm : 0),
790 ioaddr + EL3_CMD);
791 outw(RxEnable, ioaddr + EL3_CMD);
792 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
793 }
794 }
795
796 if (--i < 0) {
797 printk("%s: Infinite loop in interrupt, status %4.4x.\n",
798 dev->name, status);
799
800 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
801 break;
802 }
803
804 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
805 }
806
807 if (el3_debug > 4) {
808 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
809 inw(ioaddr + EL3_STATUS));
810 }
811 spin_unlock(&lp->lock);
812 return;
813}
814
815
816static struct net_device_stats *
817el3_get_stats(struct net_device *dev)
818{
819 struct el3_private *lp = (struct el3_private *)dev->priv;
820 unsigned long flags;
821
822
823
824
825
826
827 spin_lock_irqsave(&lp->lock, flags);
828 update_stats(dev);
829 spin_unlock_irqrestore(&lp->lock, flags);
830 return &lp->stats;
831}
832
833
834
835
836
837
838static void update_stats(struct net_device *dev)
839{
840 struct el3_private *lp = (struct el3_private *)dev->priv;
841 int ioaddr = dev->base_addr;
842
843 if (el3_debug > 5)
844 printk(" Updating the statistics.\n");
845
846 outw(StatsDisable, ioaddr + EL3_CMD);
847
848 EL3WINDOW(6);
849 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
850 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
851 inb(ioaddr + 2);
852 lp->stats.collisions += inb(ioaddr + 3);
853 lp->stats.tx_window_errors += inb(ioaddr + 4);
854 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
855 lp->stats.tx_packets += inb(ioaddr + 6);
856 inb(ioaddr + 7);
857 inb(ioaddr + 8);
858 inw(ioaddr + 10);
859 inw(ioaddr + 12);
860
861
862 EL3WINDOW(1);
863 outw(StatsEnable, ioaddr + EL3_CMD);
864 return;
865}
866
867static int
868el3_rx(struct net_device *dev)
869{
870 struct el3_private *lp = (struct el3_private *)dev->priv;
871 int ioaddr = dev->base_addr;
872 short rx_status;
873
874 if (el3_debug > 5)
875 printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n",
876 inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
877 while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
878 if (rx_status & 0x4000) {
879 short error = rx_status & 0x3800;
880
881 outw(RxDiscard, ioaddr + EL3_CMD);
882 lp->stats.rx_errors++;
883 switch (error) {
884 case 0x0000: lp->stats.rx_over_errors++; break;
885 case 0x0800: lp->stats.rx_length_errors++; break;
886 case 0x1000: lp->stats.rx_frame_errors++; break;
887 case 0x1800: lp->stats.rx_length_errors++; break;
888 case 0x2000: lp->stats.rx_frame_errors++; break;
889 case 0x2800: lp->stats.rx_crc_errors++; break;
890 }
891 } else {
892 short pkt_len = rx_status & 0x7ff;
893 struct sk_buff *skb;
894
895 skb = dev_alloc_skb(pkt_len+5);
896 lp->stats.rx_bytes += pkt_len;
897 if (el3_debug > 4)
898 printk("Receiving packet size %d status %4.4x.\n",
899 pkt_len, rx_status);
900 if (skb != NULL) {
901 skb->dev = dev;
902 skb_reserve(skb, 2);
903
904
905#ifdef __powerpc__
906 insl_ns(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
907 (pkt_len + 3) >> 2);
908#else
909 insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
910 (pkt_len + 3) >> 2);
911#endif
912
913 outw(RxDiscard, ioaddr + EL3_CMD);
914 skb->protocol = eth_type_trans(skb,dev);
915 netif_rx(skb);
916 dev->last_rx = jiffies;
917 lp->stats.rx_packets++;
918 continue;
919 }
920 outw(RxDiscard, ioaddr + EL3_CMD);
921 lp->stats.rx_dropped++;
922 if (el3_debug)
923 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
924 dev->name, pkt_len);
925 }
926 inw(ioaddr + EL3_STATUS);
927 while (inw(ioaddr + EL3_STATUS) & 0x1000)
928 printk(KERN_DEBUG " Waiting for 3c509 to discard packet, status %x.\n",
929 inw(ioaddr + EL3_STATUS) );
930 }
931
932 return 0;
933}
934
935
936
937
938static void
939set_multicast_list(struct net_device *dev)
940{
941 unsigned long flags;
942 struct el3_private *lp = (struct el3_private *)dev->priv;
943 int ioaddr = dev->base_addr;
944
945 if (el3_debug > 1) {
946 static int old;
947 if (old != dev->mc_count) {
948 old = dev->mc_count;
949 printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
950 }
951 }
952 spin_lock_irqsave(&lp->lock, flags);
953 if (dev->flags&IFF_PROMISC) {
954 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
955 ioaddr + EL3_CMD);
956 }
957 else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
958 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
959 }
960 else
961 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
962 spin_unlock_irqrestore(&lp->lock, flags);
963}
964
965static int
966el3_close(struct net_device *dev)
967{
968 int ioaddr = dev->base_addr;
969
970 if (el3_debug > 2)
971 printk("%s: Shutting down ethercard.\n", dev->name);
972
973 el3_down(dev);
974
975 free_irq(dev->irq, dev);
976
977 EL3WINDOW(0);
978
979 outw(0x0f00, ioaddr + WN0_IRQ);
980
981 return 0;
982}
983
984
985
986
987
988
989
990
991
992static int netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
993{
994 u32 ethcmd;
995
996
997
998
999 if (get_user(ethcmd, (u32 *)useraddr))
1000 return -EFAULT;
1001
1002 switch (ethcmd) {
1003
1004 case ETHTOOL_GDRVINFO: {
1005 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1006 strcpy (info.driver, DRV_NAME);
1007 strcpy (info.version, DRV_VERSION);
1008 if (copy_to_user (useraddr, &info, sizeof (info)))
1009 return -EFAULT;
1010 return 0;
1011 }
1012
1013
1014 case ETHTOOL_GMSGLVL: {
1015 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1016 edata.data = el3_debug;
1017 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1018 return -EFAULT;
1019 return 0;
1020 }
1021
1022 case ETHTOOL_SMSGLVL: {
1023 struct ethtool_value edata;
1024 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1025 return -EFAULT;
1026 el3_debug = edata.data;
1027 return 0;
1028 }
1029
1030 default:
1031 break;
1032 }
1033
1034 return -EOPNOTSUPP;
1035}
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1047{
1048 int rc = 0;
1049
1050 switch (cmd) {
1051 case SIOCETHTOOL:
1052 rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
1053 break;
1054
1055 default:
1056 rc = -EOPNOTSUPP;
1057 break;
1058 }
1059
1060 return rc;
1061}
1062
1063static void el3_down(struct net_device *dev)
1064{
1065 int ioaddr = dev->base_addr;
1066
1067 netif_stop_queue(dev);
1068
1069
1070 outw(StatsDisable, ioaddr + EL3_CMD);
1071
1072
1073 outw(RxDisable, ioaddr + EL3_CMD);
1074 outw(TxDisable, ioaddr + EL3_CMD);
1075
1076 if (dev->if_port == 3)
1077
1078 outw(StopCoax, ioaddr + EL3_CMD);
1079 else if (dev->if_port == 0) {
1080
1081 EL3WINDOW(4);
1082 outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
1083 }
1084
1085 outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1086
1087 update_stats(dev);
1088}
1089
1090static void el3_up(struct net_device *dev)
1091{
1092 int i, sw_info, net_diag;
1093 int ioaddr = dev->base_addr;
1094
1095
1096 outw(0x0001, ioaddr + 4);
1097
1098
1099 outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
1100
1101
1102 EL3WINDOW(2);
1103
1104 for (i = 0; i < 6; i++)
1105 outb(dev->dev_addr[i], ioaddr + i);
1106
1107 if ((dev->if_port & 0x03) == 3)
1108
1109 outw(StartCoax, ioaddr + EL3_CMD);
1110 else if ((dev->if_port & 0x03) == 0) {
1111
1112
1113 EL3WINDOW(0);
1114 sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) |
1115 (read_eeprom(ioaddr, 0x0d) & 0xBff0);
1116
1117 EL3WINDOW(4);
1118 net_diag = inw(ioaddr + WN4_NETDIAG);
1119 net_diag = (net_diag | FD_ENABLE);
1120 printk("%s: ", dev->name);
1121 switch (dev->if_port & 0x0c) {
1122 case 12:
1123
1124 if (sw_info & 0x000f) {
1125 printk("Forcing 3c5x9b full-duplex mode");
1126 break;
1127 }
1128 case 8:
1129
1130 if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
1131 printk("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
1132 break;
1133 }
1134 default:
1135
1136 printk("Setting 3c5x9/3c5x9B half-duplex mode");
1137 net_diag = (net_diag & ~FD_ENABLE);
1138 }
1139
1140 outw(net_diag, ioaddr + WN4_NETDIAG);
1141 printk(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
1142 if (el3_debug > 3)
1143 printk("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
1144
1145 outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
1146 }
1147
1148
1149 outw(StatsDisable, ioaddr + EL3_CMD);
1150 EL3WINDOW(6);
1151 for (i = 0; i < 9; i++)
1152 inb(ioaddr + i);
1153 inw(ioaddr + 10);
1154 inw(ioaddr + 12);
1155
1156
1157 EL3WINDOW(1);
1158
1159
1160 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1161 outw(StatsEnable, ioaddr + EL3_CMD);
1162
1163 outw(RxEnable, ioaddr + EL3_CMD);
1164 outw(TxEnable, ioaddr + EL3_CMD);
1165
1166 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
1167
1168 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
1169 ioaddr + EL3_CMD);
1170 outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
1171 ioaddr + EL3_CMD);
1172
1173 netif_start_queue(dev);
1174}
1175
1176
1177#ifdef CONFIG_PM
1178
1179static int el3_suspend(struct pm_dev *pdev)
1180{
1181 unsigned long flags;
1182 struct net_device *dev;
1183 struct el3_private *lp;
1184 int ioaddr;
1185
1186 if (!pdev && !pdev->data)
1187 return -EINVAL;
1188
1189 dev = (struct net_device *)pdev->data;
1190 lp = (struct el3_private *)dev->priv;
1191 ioaddr = dev->base_addr;
1192
1193 spin_lock_irqsave(&lp->lock, flags);
1194
1195 if (netif_running(dev))
1196 netif_device_detach(dev);
1197
1198 el3_down(dev);
1199 outw(PowerDown, ioaddr + EL3_CMD);
1200
1201 spin_unlock_irqrestore(&lp->lock, flags);
1202 return 0;
1203}
1204
1205static int el3_resume(struct pm_dev *pdev)
1206{
1207 unsigned long flags;
1208 struct net_device *dev;
1209 struct el3_private *lp;
1210 int ioaddr;
1211
1212 if (!pdev && !pdev->data)
1213 return -EINVAL;
1214
1215 dev = (struct net_device *)pdev->data;
1216 lp = (struct el3_private *)dev->priv;
1217 ioaddr = dev->base_addr;
1218
1219 spin_lock_irqsave(&lp->lock, flags);
1220
1221 outw(PowerUp, ioaddr + EL3_CMD);
1222 el3_up(dev);
1223
1224 if (netif_running(dev))
1225 netif_device_attach(dev);
1226
1227 spin_unlock_irqrestore(&lp->lock, flags);
1228 return 0;
1229}
1230
1231static int el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
1232{
1233 switch (rqst) {
1234 case PM_SUSPEND:
1235 return el3_suspend(pdev);
1236
1237 case PM_RESUME:
1238 return el3_resume(pdev);
1239 }
1240 return 0;
1241}
1242
1243#endif
1244
1245#ifdef MODULE
1246
1247static int debug = -1;
1248static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
1249static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1250
1251MODULE_PARM(debug,"i");
1252MODULE_PARM(irq,"1-8i");
1253MODULE_PARM(xcvr,"1-12i");
1254MODULE_PARM(max_interrupt_work, "i");
1255MODULE_PARM_DESC(debug, "debug level (0-6)");
1256MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
1257MODULE_PARM_DESC(xcvr,"tranceiver(s) (0=internal, 1=external)");
1258MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
1259#ifdef __ISAPNP__
1260MODULE_PARM(nopnp, "i");
1261MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
1262MODULE_DEVICE_TABLE(isapnp, el3_isapnp_adapters);
1263#endif
1264MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B) ISA/PnP ethernet driver");
1265MODULE_LICENSE("GPL");
1266
1267int
1268init_module(void)
1269{
1270 int el3_cards = 0;
1271
1272 if (debug >= 0)
1273 el3_debug = debug;
1274
1275 el3_root_dev = NULL;
1276 while (el3_probe(0, el3_cards) == 0) {
1277 if (irq[el3_cards] > 1)
1278 el3_root_dev->irq = irq[el3_cards];
1279 if (xcvr[el3_cards] >= 0)
1280 el3_root_dev->if_port = xcvr[el3_cards];
1281 el3_cards++;
1282 }
1283
1284 return el3_cards ? 0 : -ENODEV;
1285}
1286
1287void
1288cleanup_module(void)
1289{
1290 struct net_device *next_dev;
1291
1292
1293 while (el3_root_dev) {
1294 struct el3_private *lp = (struct el3_private *)el3_root_dev->priv;
1295#ifdef CONFIG_MCA
1296 if(lp->mca_slot!=-1)
1297 mca_mark_as_unused(lp->mca_slot);
1298#endif
1299
1300#ifdef CONFIG_PM
1301 if (lp->pmdev)
1302 pm_unregister(lp->pmdev);
1303#endif
1304 next_dev = lp->next_dev;
1305 unregister_netdev(el3_root_dev);
1306 release_region(el3_root_dev->base_addr, EL3_IO_EXTENT);
1307 kfree(el3_root_dev);
1308 el3_root_dev = next_dev;
1309 }
1310}
1311#endif
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321