1 /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
3 Written 1993-2000 by Donald Becker.
5 Copyright 1994-2000 by Donald Becker.
6 Copyright 1993 United States Government as represented by the
7 Director, National Security Agency. This software may be used and
8 distributed according to the terms of the GNU General Public License,
9 incorporated herein by reference.
11 This driver is for the 3Com EtherLinkIII series.
13 The author may be reached as becker@scyld.com, or C/O
14 Scyld Computing Corporation
15 410 Severn Ave., Suite 210
19 Because of the way 3c509 ISA detection works it's difficult to predict
20 a priori which of several ISA-mode cards will be detected first.
22 This driver does not use predictive interrupt mode, resulting in higher
23 packet latency but lower overhead. If interrupts are disabled for an
24 unusually long time it could also result in missed packets, but in
25 practice this rarely happens.
29 Alan Cox: Removed the 'Unexpected interrupt' bug.
30 Michael Meskes: Upgraded to Donald Becker's version 1.07.
31 Alan Cox: Increased the eeprom delay. Regardless of
32 what the docs say some people definitely
33 get problems with lower (but in card spec)
35 v1.10 4/21/97 Fixed module code so that multiple cards may be detected,
37 Andrea Arcangeli: Upgraded to Donald Becker's version 1.12.
38 Rick Payne: Fixed SMP race condition
39 v1.13 9/8/97 Made 'max_interrupt_work' an insmod-settable variable -djb
40 v1.14 10/15/97 Avoided waiting..discard message for fast machines -djb
41 v1.15 1/31/98 Faster recovery for Tx errors. -djb
42 v1.16 2/3/98 Different ID port handling to avoid sound cards. -djb
43 v1.18 12Mar2001 Andrew Morton <andrewm@uow.edu.au>
44 - Avoid bogus detect of 3c590's (Andrzej Krzysztofowicz)
45 - Reviewed against 1.18 from scyld.com
46 v1.18a 17Nov2001 Jeff Garzik <jgarzik@pobox.com>
48 v1.18b 1Mar2002 Zwane Mwaikambo <zwane@commfireservices.com>
49 - Power Management support
50 v1.18c 1Mar2002 David Ruggiero <jdr@farfalle.com>
52 v1.19 16Oct2002 Zwane Mwaikambo <zwane@linuxpower.ca>
53 - Additional ethtool features
56 #define DRV_NAME "3c509"
57 #define DRV_VERSION "1.19"
58 #define DRV_RELDATE "16Oct2002"
60 /* A few values that may be tweaked. */
62 /* Time in jiffies before concluding the transmitter is hung. */
63 #define TX_TIMEOUT (400*HZ/1000)
64 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
65 static int max_interrupt_work = 10;
67 #include <linux/config.h>
68 #include <linux/module.h>
70 #include <linux/mca.h>
71 #include <linux/isapnp.h>
72 #include <linux/sched.h>
73 #include <linux/string.h>
74 #include <linux/interrupt.h>
75 #include <linux/errno.h>
77 #include <linux/slab.h>
78 #include <linux/ioport.h>
79 #include <linux/init.h>
80 #include <linux/netdevice.h>
81 #include <linux/etherdevice.h>
82 #include <linux/skbuff.h>
83 #include <linux/delay.h> /* for udelay() */
84 #include <linux/spinlock.h>
85 #include <linux/ethtool.h>
87 #include <asm/uaccess.h>
88 #include <asm/bitops.h>
93 static char versionA[] __initdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
94 static char versionB[] __initdata = "http://www.scyld.com/network/3c509.html\n";
97 static int el3_debug = EL3_DEBUG;
99 static int el3_debug = 2;
103 /* To minimize the size of the driver source I only define operating
104 constants if they are used several times. You'll need the manual
105 anyway if you want to understand driver details. */
106 /* Offsets from base I/O address. */
107 #define EL3_DATA 0x00
109 #define EL3_STATUS 0x0e
110 #define EEPROM_READ 0x80
112 #define EL3_IO_EXTENT 16
114 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
117 /* The top five bits written to EL3_CMD are a command, the lower
118 11 bits are the parameter, if applicable. */
120 TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
121 RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
122 TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
123 FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
124 SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
125 SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
126 StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
127 PowerDown = 28<<11, PowerAuto = 29<<11};
130 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
131 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
132 IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
134 /* The SetRxFilter command accepts the following classes: */
136 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
138 /* Register window 1 offsets, the window used in normal operation. */
141 #define RX_STATUS 0x08
142 #define TX_STATUS 0x0B
143 #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
145 #define WN0_CONF_CTRL 0x04 /* Window 0: Configuration control register */
146 #define WN0_ADDR_CONF 0x06 /* Window 0: Address configuration register */
147 #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
148 #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
149 #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
150 #define WN4_NETDIAG 0x06 /* Window 4: Net diagnostic */
151 #define FD_ENABLE 0x8000 /* Enable full-duplex ("external loopback") */
154 * Must be a power of two (we use a binary and in the
157 #define SKB_QUEUE_SIZE 64
160 struct net_device_stats stats;
161 struct net_device *next_dev;
165 struct sk_buff *queue[SKB_QUEUE_SIZE];
168 struct pm_dev *pmdev;
171 static int id_port __initdata = 0x110; /* Start with 0x110 to avoid new sound cards.*/
172 static struct net_device *el3_root_dev;
174 static ushort id_read_eeprom(int index);
175 static ushort read_eeprom(int ioaddr, int index);
176 static int el3_open(struct net_device *dev);
177 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
178 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
179 static void update_stats(struct net_device *dev);
180 static struct net_device_stats *el3_get_stats(struct net_device *dev);
181 static int el3_rx(struct net_device *dev);
182 static int el3_close(struct net_device *dev);
183 static void set_multicast_list(struct net_device *dev);
184 static void el3_tx_timeout (struct net_device *dev);
185 static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
186 static void el3_down(struct net_device *dev);
187 static void el3_up(struct net_device *dev);
189 static int el3_suspend(struct pm_dev *pdev);
190 static int el3_resume(struct pm_dev *pdev);
191 static int el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data);
195 struct el3_mca_adapters_struct {
200 static struct el3_mca_adapters_struct el3_mca_adapters[] __initdata = {
201 { "3Com 3c529 EtherLink III (10base2)", 0x627c },
202 { "3Com 3c529 EtherLink III (10baseT)", 0x627d },
203 { "3Com 3c529 EtherLink III (test mode)", 0x62db },
204 { "3Com 3c529 EtherLink III (TP or coax)", 0x62f6 },
205 { "3Com 3c529 EtherLink III (TP)", 0x62f7 },
208 #endif /* CONFIG_MCA */
210 #if defined(CONFIG_ISAPNP) || defined(CONFIG_ISAPNP_MODULE)
211 static struct isapnp_device_id el3_isapnp_adapters[] __initdata = {
212 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
213 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090),
214 (long) "3Com Etherlink III (TP)" },
215 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
216 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091),
217 (long) "3Com Etherlink III" },
218 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
219 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094),
220 (long) "3Com Etherlink III (combo)" },
221 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
222 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095),
223 (long) "3Com Etherlink III (TPO)" },
224 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
225 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098),
226 (long) "3Com Etherlink III (TPC)" },
227 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
228 ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f7),
229 (long) "3Com Etherlink III compatible" },
230 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
231 ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8),
232 (long) "3Com Etherlink III compatible" },
233 { } /* terminate list */
236 MODULE_DEVICE_TABLE(isapnp, el3_isapnp_adapters);
238 static u16 el3_isapnp_phys_addr[8][3];
240 #endif /* CONFIG_ISAPNP || CONFIG_ISAPNP_MODULE */
242 int __init el3_probe(struct net_device *dev, int card_idx)
244 struct el3_private *lp;
245 short lrs_state = 0xff, i;
246 int ioaddr, irq, if_port;
248 static int current_tag;
250 #if defined(CONFIG_ISAPNP) || defined(CONFIG_ISAPNP_MODULE)
251 static int pnp_cards;
252 #endif /* CONFIG_ISAPNP || CONFIG_ISAPNP_MODULE */
254 if (dev) SET_MODULE_OWNER(dev);
256 /* First check all slots of the EISA bus. The next slot address to
257 probe is kept in 'eisa_addr' to support multiple probe() calls. */
259 static int eisa_addr = 0x1000;
260 while (eisa_addr < 0x9000) {
266 /* Check the standard EISA ID register for an encoded '3Com'. */
267 if (inw(ioaddr + 0xC80) != 0x6d50)
270 /* Avoid conflict with 3c590, 3c592, 3c597, etc */
271 device_id = (inb(ioaddr + 0xC82)<<8) + inb(ioaddr + 0xC83);
272 if ((device_id & 0xFF00) == 0x5900) {
276 /* Change the register set to the configuration window 0. */
277 outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
279 irq = inw(ioaddr + WN0_IRQ) >> 12;
280 if_port = inw(ioaddr + 6)>>14;
281 for (i = 0; i < 3; i++)
282 phys_addr[i] = htons(read_eeprom(ioaddr, i));
284 /* Restore the "Product ID" to the EEPROM read register. */
285 read_eeprom(ioaddr, 3);
287 /* Was the EISA code an add-on hack? Nahhhhh... */
293 /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch, heavily
294 * modified by Chris Beauregard (cpbeaure@csclub.uwaterloo.ca)
295 * to support standard MCA probing.
297 * redone for multi-card detection by ZP Gu (zpg@castle.net)
298 * now works as a module
305 for( j = 0; el3_mca_adapters[j].name != NULL; j ++ ) {
307 while( slot != MCA_NOTFOUND ) {
308 slot = mca_find_unused_adapter(
309 el3_mca_adapters[j].id, slot );
310 if( slot == MCA_NOTFOUND ) break;
312 /* if we get this far, an adapter has been
313 * detected and is enabled
316 pos4 = mca_read_stored_pos( slot, 4 );
317 pos5 = mca_read_stored_pos( slot, 5 );
319 ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
322 /* probing for a card at a particular IO/IRQ */
323 if(dev && ((dev->irq >= 1 && dev->irq != irq) ||
324 (dev->base_addr >= 1 && dev->base_addr != ioaddr))) {
325 slot++; /* probing next slot */
329 printk("3c509: found %s at slot %d\n",
330 el3_mca_adapters[j].name, slot + 1 );
333 mca_set_adapter_name(slot, el3_mca_adapters[j].name);
334 mca_set_adapter_procfn(slot, NULL, NULL);
335 mca_mark_as_used(slot);
337 if_port = pos4 & 0x03;
339 printk("3c529: irq %d ioaddr 0x%x ifport %d\n", irq, ioaddr, if_port);
342 for (i = 0; i < 3; i++) {
343 phys_addr[i] = htons(read_eeprom(ioaddr, i));
351 /* if we get here, we didn't find an MCA adapter */
354 #endif /* CONFIG_MCA */
356 #if defined(CONFIG_ISAPNP) || defined(CONFIG_ISAPNP_MODULE)
360 for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
361 struct pci_dev *idev = NULL;
363 while ((idev = isapnp_find_dev(NULL,
364 el3_isapnp_adapters[i].vendor,
365 el3_isapnp_adapters[i].function,
368 /* Deactivation is needed if the driver was called
369 with "nopnp=1" before, does not harm if not. */
370 idev->deactivate(idev);
371 idev->activate(idev);
372 if (!idev->resource[0].start || check_region(idev->resource[0].start, EL3_IO_EXTENT))
374 ioaddr = idev->resource[0].start;
375 if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP"))
377 irq = idev->irq_resource[0].start;
379 printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
380 (char*) el3_isapnp_adapters[i].driver_data, ioaddr, irq);
382 for (j = 0; j < 3; j++)
383 el3_isapnp_phys_addr[pnp_cards][j] =
385 htons(read_eeprom(ioaddr, j));
386 if_port = read_eeprom(ioaddr, 8) >> 14;
392 #endif /* CONFIG_ISAPNP || CONFIG_ISAPNP_MODULE */
394 /* Select an open I/O location at 0x1*0 to do contention select. */
395 for ( ; id_port < 0x200; id_port += 0x10) {
396 if (check_region(id_port, 1))
400 if (inb(id_port) & 0x01)
403 if (id_port >= 0x200) {
404 /* Rare -- do we really need a warning? */
405 printk(" WARNING: No I/O port available for 3c509 activation.\n");
408 /* Next check for all ISA bus boards by sending the ID sequence to the
409 ID_PORT. We find cards past the first by setting the 'current_tag'
410 on cards as they are found. Cards with their tag set will not
411 respond to subsequent ID sequences. */
415 for(i = 0; i < 255; i++) {
416 outb(lrs_state, id_port);
418 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
421 /* For the first probe, clear all board's tag registers. */
422 if (current_tag == 0)
424 else /* Otherwise kill off already-found boards. */
427 if (id_read_eeprom(7) != 0x6d50) {
431 /* Read in EEPROM data, which does contention-select.
432 Only the lowest address board will stay "on-line".
433 3Com got the byte order backwards. */
434 for (i = 0; i < 3; i++) {
435 phys_addr[i] = htons(id_read_eeprom(i));
438 #if defined(CONFIG_ISAPNP) || defined(CONFIG_ISAPNP_MODULE)
440 /* The ISA PnP 3c509 cards respond to the ID sequence.
441 This check is needed in order not to register them twice. */
442 for (i = 0; i < pnp_cards; i++) {
443 if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
444 phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
445 phys_addr[2] == el3_isapnp_phys_addr[i][2])
448 printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
449 phys_addr[0] & 0xff, phys_addr[0] >> 8,
450 phys_addr[1] & 0xff, phys_addr[1] >> 8,
451 phys_addr[2] & 0xff, phys_addr[2] >> 8);
452 /* Set the adaptor tag so that the next card can be found. */
453 outb(0xd0 + ++current_tag, id_port);
458 #endif /* CONFIG_ISAPNP || CONFIG_ISAPNP_MODULE */
461 unsigned int iobase = id_read_eeprom(8);
462 if_port = iobase >> 14;
463 ioaddr = 0x200 + ((iobase & 0x1f) << 4);
465 irq = id_read_eeprom(9) >> 12;
467 if (dev) { /* Set passed-in IRQ or I/O Addr. */
468 if (dev->irq > 1 && dev->irq < 16)
471 if (dev->base_addr) {
472 if (dev->mem_end == 0x3c509 /* Magic key */
473 && dev->base_addr >= 0x200 && dev->base_addr <= 0x3e0)
474 ioaddr = dev->base_addr & 0x3f0;
475 else if (dev->base_addr != ioaddr)
480 if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
483 /* Set the adaptor tag so that the next card can be found. */
484 outb(0xd0 + ++current_tag, id_port);
486 /* Activate the adaptor at the EEPROM location. */
487 outb((ioaddr >> 4) | 0xe0, id_port);
490 if (inw(ioaddr) != 0x6d50) {
491 release_region(ioaddr, EL3_IO_EXTENT);
495 /* Free the interrupt so that some other card can use it. */
496 outw(0x0f00, ioaddr + WN0_IRQ);
499 dev = init_etherdev(dev, sizeof(struct el3_private));
501 release_region(ioaddr, EL3_IO_EXTENT);
504 SET_MODULE_OWNER(dev);
506 memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
507 dev->base_addr = ioaddr;
510 if (dev->mem_start & 0x05) { /* xcvr codes 1/3/4/12 */
511 dev->if_port = (dev->mem_start & 0x0f);
512 } else { /* xcvr codes 0/8 */
513 /* use eeprom value, but save user's full-duplex selection */
514 dev->if_port = (if_port | (dev->mem_start & 0x08) );
518 const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
519 printk("%s: 3c5x9 at %#3.3lx, %s port, address ",
520 dev->name, dev->base_addr, if_names[(dev->if_port & 0x03)]);
523 /* Read in the station address. */
524 for (i = 0; i < 6; i++)
525 printk(" %2.2x", dev->dev_addr[i]);
526 printk(", IRQ %d.\n", dev->irq);
528 /* Make up a EL3-specific-data structure. */
529 if (dev->priv == NULL)
530 dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL);
531 if (dev->priv == NULL)
533 memset(dev->priv, 0, sizeof(struct el3_private));
536 lp->mca_slot = mca_slot;
537 lp->next_dev = el3_root_dev;
538 spin_lock_init(&lp->lock);
542 printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
544 /* The EL3-specific entries in the device structure. */
545 dev->open = &el3_open;
546 dev->hard_start_xmit = &el3_start_xmit;
547 dev->stop = &el3_close;
548 dev->get_stats = &el3_get_stats;
549 dev->set_multicast_list = &set_multicast_list;
550 dev->tx_timeout = el3_tx_timeout;
551 dev->watchdog_timeo = TX_TIMEOUT;
552 dev->do_ioctl = netdev_ioctl;
555 /* register power management */
556 lp->pmdev = pm_register(PM_ISA_DEV, card_idx, el3_pm_callback);
560 p->data = (struct net_device *)dev;
564 /* Fill in the generic fields of the device structure. */
569 /* Read a word from the EEPROM using the regular EEPROM access register.
570 Assume that we are in register window zero.
572 static ushort read_eeprom(int ioaddr, int index)
574 outw(EEPROM_READ + index, ioaddr + 10);
575 /* Pause for at least 162 us. for the read to take place. */
577 return inw(ioaddr + 12);
580 /* Read a word from the EEPROM when in the ISA ID probe state. */
581 static ushort __init id_read_eeprom(int index)
585 /* Issue read command, and pause for at least 162 us. for it to complete.
586 Assume extra-fast 16Mhz bus. */
587 outb(EEPROM_READ + index, id_port);
589 /* Pause for at least 162 us. for the read to take place. */
592 for (bit = 15; bit >= 0; bit--)
593 word = (word << 1) + (inb(id_port) & 0x01);
596 printk(" 3c509 EEPROM word %d %#4.4x.\n", index, word);
603 el3_open(struct net_device *dev)
605 int ioaddr = dev->base_addr;
608 outw(TxReset, ioaddr + EL3_CMD);
609 outw(RxReset, ioaddr + EL3_CMD);
610 outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
612 i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
617 printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev->name,
618 dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
623 printk("%s: Opened 3c509 IRQ %d status %4.4x.\n",
624 dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
630 el3_tx_timeout (struct net_device *dev)
632 struct el3_private *lp = (struct el3_private *)dev->priv;
633 int ioaddr = dev->base_addr;
635 /* Transmitter timeout, serious problems. */
636 printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
637 "Tx FIFO room %d.\n",
638 dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
639 inw(ioaddr + TX_FREE));
640 lp->stats.tx_errors++;
641 dev->trans_start = jiffies;
642 /* Issue TX_RESET and TX_START commands. */
643 outw(TxReset, ioaddr + EL3_CMD);
644 outw(TxEnable, ioaddr + EL3_CMD);
645 netif_wake_queue(dev);
650 el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
652 struct el3_private *lp = (struct el3_private *)dev->priv;
653 int ioaddr = dev->base_addr;
656 netif_stop_queue (dev);
658 lp->stats.tx_bytes += skb->len;
661 printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
662 dev->name, skb->len, inw(ioaddr + EL3_STATUS));
665 #ifndef final_version
666 { /* Error-checking code, delete someday. */
667 ushort status = inw(ioaddr + EL3_STATUS);
668 if (status & 0x0001 /* IRQ line active, missed one. */
669 && inw(ioaddr + EL3_STATUS) & 1) { /* Make sure. */
670 printk("%s: Missed interrupt, status then %04x now %04x"
671 " Tx %2.2x Rx %4.4x.\n", dev->name, status,
672 inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
673 inw(ioaddr + RX_STATUS));
674 /* Fake interrupt trigger by masking, acknowledge interrupts. */
675 outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
676 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
678 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
684 * We lock the driver against other processors. Note
685 * we don't need to lock versus the IRQ as we suspended
686 * that. This means that we lose the ability to take
687 * an RX during a TX upload. That sucks a bit with SMP
688 * on an original 3c509 (2K buffer)
690 * Using disable_irq stops us crapping on other
691 * time sensitive devices.
694 spin_lock_irqsave(&lp->lock, flags);
696 /* Put out the doubleword header... */
697 outw(skb->len, ioaddr + TX_FIFO);
698 outw(0x00, ioaddr + TX_FIFO);
699 /* ... and the packet rounded to a doubleword. */
701 outsl_ns(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
703 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
706 dev->trans_start = jiffies;
707 if (inw(ioaddr + TX_FREE) > 1536)
708 netif_start_queue(dev);
710 /* Interrupt us when the FIFO has room for max-sized packet. */
711 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
713 spin_unlock_irqrestore(&lp->lock, flags);
717 /* Clear the Tx status stack. */
722 while (--i > 0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
723 if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
724 if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
725 if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
726 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
732 /* The EL3 interrupt handler. */
734 el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
736 struct net_device *dev = (struct net_device *)dev_id;
737 struct el3_private *lp;
739 int i = max_interrupt_work;
742 printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
746 lp = (struct el3_private *)dev->priv;
747 spin_lock(&lp->lock);
749 ioaddr = dev->base_addr;
752 status = inw(ioaddr + EL3_STATUS);
753 printk("%s: interrupt, status %4.4x.\n", dev->name, status);
756 while ((status = inw(ioaddr + EL3_STATUS)) &
757 (IntLatch | RxComplete | StatsFull)) {
759 if (status & RxComplete)
762 if (status & TxAvailable) {
764 printk(" TX room bit was handled.\n");
765 /* There's room in the FIFO for a full-sized packet. */
766 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
767 netif_wake_queue (dev);
769 if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
770 /* Handle all uncommon interrupts. */
771 if (status & StatsFull) /* Empty statistics. */
773 if (status & RxEarly) { /* Rx early is unused. */
775 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
777 if (status & TxComplete) { /* Really Tx error. */
778 struct el3_private *lp = (struct el3_private *)dev->priv;
782 while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
783 if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
784 if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
785 if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
786 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
789 if (status & AdapterFailure) {
790 /* Adapter failure requires Rx reset and reinit. */
791 outw(RxReset, ioaddr + EL3_CMD);
792 /* Set the Rx filter to the current state. */
793 outw(SetRxFilter | RxStation | RxBroadcast
794 | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
795 | (dev->flags & IFF_PROMISC ? RxProm : 0),
797 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
798 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
803 printk("%s: Infinite loop in interrupt, status %4.4x.\n",
805 /* Clear all interrupts. */
806 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
809 /* Acknowledge the IRQ. */
810 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
814 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
815 inw(ioaddr + EL3_STATUS));
817 spin_unlock(&lp->lock);
822 static struct net_device_stats *
823 el3_get_stats(struct net_device *dev)
825 struct el3_private *lp = (struct el3_private *)dev->priv;
829 * This is fast enough not to bother with disable IRQ
833 spin_lock_irqsave(&lp->lock, flags);
835 spin_unlock_irqrestore(&lp->lock, flags);
839 /* Update statistics. We change to register window 6, so this should be run
840 single-threaded if the device is active. This is expected to be a rare
841 operation, and it's simpler for the rest of the driver to assume that
842 window 1 is always valid rather than use a special window-state variable.
844 static void update_stats(struct net_device *dev)
846 struct el3_private *lp = (struct el3_private *)dev->priv;
847 int ioaddr = dev->base_addr;
850 printk(" Updating the statistics.\n");
851 /* Turn off statistics updates while reading. */
852 outw(StatsDisable, ioaddr + EL3_CMD);
853 /* Switch to the stats window, and read everything. */
855 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
856 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
857 /* Multiple collisions. */ inb(ioaddr + 2);
858 lp->stats.collisions += inb(ioaddr + 3);
859 lp->stats.tx_window_errors += inb(ioaddr + 4);
860 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
861 lp->stats.tx_packets += inb(ioaddr + 6);
862 /* Rx packets */ inb(ioaddr + 7);
863 /* Tx deferrals */ inb(ioaddr + 8);
864 inw(ioaddr + 10); /* Total Rx and Tx octets. */
867 /* Back to window 1, and turn statistics back on. */
869 outw(StatsEnable, ioaddr + EL3_CMD);
874 el3_rx(struct net_device *dev)
876 struct el3_private *lp = (struct el3_private *)dev->priv;
877 int ioaddr = dev->base_addr;
881 printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n",
882 inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
883 while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
884 if (rx_status & 0x4000) { /* Error, update stats. */
885 short error = rx_status & 0x3800;
887 outw(RxDiscard, ioaddr + EL3_CMD);
888 lp->stats.rx_errors++;
890 case 0x0000: lp->stats.rx_over_errors++; break;
891 case 0x0800: lp->stats.rx_length_errors++; break;
892 case 0x1000: lp->stats.rx_frame_errors++; break;
893 case 0x1800: lp->stats.rx_length_errors++; break;
894 case 0x2000: lp->stats.rx_frame_errors++; break;
895 case 0x2800: lp->stats.rx_crc_errors++; break;
898 short pkt_len = rx_status & 0x7ff;
901 skb = dev_alloc_skb(pkt_len+5);
902 lp->stats.rx_bytes += pkt_len;
904 printk("Receiving packet size %d status %4.4x.\n",
908 skb_reserve(skb, 2); /* Align IP on 16 byte */
910 /* 'skb->data' points to the start of sk_buff data area. */
912 insl_ns(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
915 insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
919 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
920 skb->protocol = eth_type_trans(skb,dev);
922 dev->last_rx = jiffies;
923 lp->stats.rx_packets++;
926 outw(RxDiscard, ioaddr + EL3_CMD);
927 lp->stats.rx_dropped++;
929 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
932 inw(ioaddr + EL3_STATUS); /* Delay. */
933 while (inw(ioaddr + EL3_STATUS) & 0x1000)
934 printk(KERN_DEBUG " Waiting for 3c509 to discard packet, status %x.\n",
935 inw(ioaddr + EL3_STATUS) );
942 * Set or clear the multicast filter for this adaptor.
945 set_multicast_list(struct net_device *dev)
948 struct el3_private *lp = (struct el3_private *)dev->priv;
949 int ioaddr = dev->base_addr;
953 if (old != dev->mc_count) {
955 printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
958 spin_lock_irqsave(&lp->lock, flags);
959 if (dev->flags&IFF_PROMISC) {
960 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
963 else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
964 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
967 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
968 spin_unlock_irqrestore(&lp->lock, flags);
972 el3_close(struct net_device *dev)
974 int ioaddr = dev->base_addr;
977 printk("%s: Shutting down ethercard.\n", dev->name);
981 free_irq(dev->irq, dev);
982 /* Switching back to window 0 disables the IRQ. */
984 /* But we explicitly zero the IRQ line select anyway. */
985 outw(0x0f00, ioaddr + WN0_IRQ);
991 el3_link_ok(struct net_device *dev)
993 int ioaddr = dev->base_addr;
997 tmp = inw(ioaddr + WN4_MEDIA);
999 return tmp & (1<<11);
1003 el3_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1006 int ioaddr = dev->base_addr;
1009 /* obtain current tranceiver via WN4_MEDIA? */
1010 tmp = inw(ioaddr + WN0_ADDR_CONF);
1011 ecmd->transceiver = XCVR_INTERNAL;
1012 switch (tmp >> 14) {
1014 ecmd->port = PORT_TP;
1017 ecmd->port = PORT_AUI;
1018 ecmd->transceiver = XCVR_EXTERNAL;
1021 ecmd->port = PORT_BNC;
1026 ecmd->duplex = DUPLEX_HALF;
1027 ecmd->supported = 0;
1028 tmp = inw(ioaddr + WN0_CONF_CTRL);
1030 ecmd->supported |= SUPPORTED_AUI;
1032 ecmd->supported |= SUPPORTED_BNC;
1034 ecmd->supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half |
1035 SUPPORTED_10baseT_Full; /* hmm... */
1037 tmp = inw(ioaddr + WN4_NETDIAG);
1038 if (tmp & FD_ENABLE)
1039 ecmd->duplex = DUPLEX_FULL;
1042 ecmd->speed = SPEED_10;
1048 el3_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1051 int ioaddr = dev->base_addr;
1053 if (ecmd->speed != SPEED_10)
1055 if ((ecmd->duplex != DUPLEX_HALF) && (ecmd->duplex != DUPLEX_FULL))
1057 if ((ecmd->transceiver != XCVR_INTERNAL) && (ecmd->transceiver != XCVR_EXTERNAL))
1060 /* change XCVR type */
1062 tmp = inw(ioaddr + WN0_ADDR_CONF);
1063 switch (ecmd->port) {
1080 outw(tmp, ioaddr + WN0_ADDR_CONF);
1081 if (dev->if_port == 3) {
1082 /* fire up the DC-DC convertor if BNC gets enabled */
1083 tmp = inw(ioaddr + WN0_ADDR_CONF);
1084 if (tmp & (3 << 14)) {
1085 outw(StartCoax, ioaddr + EL3_CMD);
1092 tmp = inw(ioaddr + WN4_NETDIAG);
1093 if (ecmd->duplex == DUPLEX_FULL)
1097 outw(tmp, ioaddr + WN4_NETDIAG);
1104 * netdev_ethtool_ioctl: Handle network interface SIOCETHTOOL ioctls
1105 * @dev: network interface on which out-of-band action is to be performed
1106 * @useraddr: userspace address to which data is to be read and returned
1108 * Process the various commands of the SIOCETHTOOL interface.
1112 netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
1115 struct el3_private *lp = dev->priv;
1117 /* dev_ioctl() in ../../net/core/dev.c has already checked
1118 capable(CAP_NET_ADMIN), so don't bother with that here. */
1120 if (get_user(ethcmd, (u32 *)useraddr))
1125 case ETHTOOL_GDRVINFO: {
1126 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1127 strcpy (info.driver, DRV_NAME);
1128 strcpy (info.version, DRV_VERSION);
1129 if (copy_to_user (useraddr, &info, sizeof (info)))
1135 case ETHTOOL_GSET: {
1137 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1138 spin_lock_irq(&lp->lock);
1139 ret = el3_netdev_get_ecmd(dev, &ecmd);
1140 spin_unlock_irq(&lp->lock);
1141 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1147 case ETHTOOL_SSET: {
1149 struct ethtool_cmd ecmd;
1150 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1152 spin_lock_irq(&lp->lock);
1153 ret = el3_netdev_set_ecmd(dev, &ecmd);
1154 spin_unlock_irq(&lp->lock);
1158 /* get link status */
1159 case ETHTOOL_GLINK: {
1160 struct ethtool_value edata = { ETHTOOL_GLINK };
1161 spin_lock_irq(&lp->lock);
1162 edata.data = el3_link_ok(dev);
1163 spin_unlock_irq(&lp->lock);
1164 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1169 /* get message-level */
1170 case ETHTOOL_GMSGLVL: {
1171 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1172 edata.data = el3_debug;
1173 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1177 /* set message-level */
1178 case ETHTOOL_SMSGLVL: {
1179 struct ethtool_value edata;
1180 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1182 el3_debug = edata.data;
1194 * netdev_ioctl: Handle network interface ioctls
1195 * @dev: network interface on which out-of-band action is to be performed
1196 * @rq: user request data
1197 * @cmd: command issued by user
1199 * Process the various out-of-band ioctls passed to this driver.
1203 netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1209 rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
1221 el3_down(struct net_device *dev)
1223 int ioaddr = dev->base_addr;
1225 netif_stop_queue(dev);
1227 /* Turn off statistics ASAP. We update lp->stats below. */
1228 outw(StatsDisable, ioaddr + EL3_CMD);
1230 /* Disable the receiver and transmitter. */
1231 outw(RxDisable, ioaddr + EL3_CMD);
1232 outw(TxDisable, ioaddr + EL3_CMD);
1234 if (dev->if_port == 3)
1235 /* Turn off thinnet power. Green! */
1236 outw(StopCoax, ioaddr + EL3_CMD);
1237 else if (dev->if_port == 0) {
1238 /* Disable link beat and jabber, if_port may change here next open(). */
1240 outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
1243 outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1249 el3_up(struct net_device *dev)
1251 int i, sw_info, net_diag;
1252 int ioaddr = dev->base_addr;
1254 /* Activating the board required and does no harm otherwise */
1255 outw(0x0001, ioaddr + 4);
1257 /* Set the IRQ line. */
1258 outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
1260 /* Set the station address in window 2 each time opened. */
1263 for (i = 0; i < 6; i++)
1264 outb(dev->dev_addr[i], ioaddr + i);
1266 if ((dev->if_port & 0x03) == 3) /* BNC interface */
1267 /* Start the thinnet transceiver. We should really wait 50ms...*/
1268 outw(StartCoax, ioaddr + EL3_CMD);
1269 else if ((dev->if_port & 0x03) == 0) { /* 10baseT interface */
1270 /* Combine secondary sw_info word (the adapter level) and primary
1271 sw_info word (duplex setting plus other useless bits) */
1273 sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) |
1274 (read_eeprom(ioaddr, 0x0d) & 0xBff0);
1277 net_diag = inw(ioaddr + WN4_NETDIAG);
1278 net_diag = (net_diag | FD_ENABLE); /* temporarily assume full-duplex will be set */
1279 printk("%s: ", dev->name);
1280 switch (dev->if_port & 0x0c) {
1282 /* force full-duplex mode if 3c5x9b */
1283 if (sw_info & 0x000f) {
1284 printk("Forcing 3c5x9b full-duplex mode");
1288 /* set full-duplex mode based on eeprom config setting */
1289 if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
1290 printk("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
1294 /* xcvr=(0 || 4) OR user has an old 3c5x9 non "B" model */
1295 printk("Setting 3c5x9/3c5x9B half-duplex mode");
1296 net_diag = (net_diag & ~FD_ENABLE); /* disable full duplex */
1299 outw(net_diag, ioaddr + WN4_NETDIAG);
1300 printk(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
1302 printk("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
1303 /* Enable link beat and jabber check. */
1304 outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
1307 /* Switch to the stats window, and clear all stats by reading. */
1308 outw(StatsDisable, ioaddr + EL3_CMD);
1310 for (i = 0; i < 9; i++)
1315 /* Switch to register set 1 for normal use. */
1318 /* Accept b-case and phys addr only. */
1319 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1320 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
1322 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
1323 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
1324 /* Allow status bits to be seen. */
1325 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
1326 /* Ack all pending events, and set active indicator mask. */
1327 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
1329 outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
1332 netif_start_queue(dev);
1335 /* Power Management support functions */
1339 el3_suspend(struct pm_dev *pdev)
1341 unsigned long flags;
1342 struct net_device *dev;
1343 struct el3_private *lp;
1346 if (!pdev && !pdev->data)
1349 dev = (struct net_device *)pdev->data;
1350 lp = (struct el3_private *)dev->priv;
1351 ioaddr = dev->base_addr;
1353 spin_lock_irqsave(&lp->lock, flags);
1355 if (netif_running(dev))
1356 netif_device_detach(dev);
1359 outw(PowerDown, ioaddr + EL3_CMD);
1361 spin_unlock_irqrestore(&lp->lock, flags);
1366 el3_resume(struct pm_dev *pdev)
1368 unsigned long flags;
1369 struct net_device *dev;
1370 struct el3_private *lp;
1373 if (!pdev && !pdev->data)
1376 dev = (struct net_device *)pdev->data;
1377 lp = (struct el3_private *)dev->priv;
1378 ioaddr = dev->base_addr;
1380 spin_lock_irqsave(&lp->lock, flags);
1382 outw(PowerUp, ioaddr + EL3_CMD);
1385 if (netif_running(dev))
1386 netif_device_attach(dev);
1388 spin_unlock_irqrestore(&lp->lock, flags);
1393 el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
1397 return el3_suspend(pdev);
1400 return el3_resume(pdev);
1405 #endif /* CONFIG_PM */
1408 /* Parameters that may be passed into the module. */
1409 static int debug = -1;
1410 static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
1411 static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1413 MODULE_PARM(debug,"i");
1414 MODULE_PARM(irq,"1-8i");
1415 MODULE_PARM(xcvr,"1-12i");
1416 MODULE_PARM(max_interrupt_work, "i");
1417 MODULE_PARM_DESC(debug, "debug level (0-6)");
1418 MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
1419 MODULE_PARM_DESC(xcvr,"tranceiver(s) (0=internal, 1=external)");
1420 MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
1421 #ifdef CONFIG_ISAPNP
1422 MODULE_PARM(nopnp, "i");
1423 MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
1424 #endif /* CONFIG_ISAPNP */
1425 MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B) ISA/PnP ethernet driver");
1426 MODULE_LICENSE("GPL");
1436 el3_root_dev = NULL;
1437 while (el3_probe(0, el3_cards) == 0) {
1438 if (irq[el3_cards] > 1)
1439 el3_root_dev->irq = irq[el3_cards];
1440 if (xcvr[el3_cards] >= 0)
1441 el3_root_dev->if_port = xcvr[el3_cards];
1445 return el3_cards ? 0 : -ENODEV;
1449 cleanup_module(void)
1451 struct net_device *next_dev;
1453 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1454 while (el3_root_dev) {
1455 struct el3_private *lp = (struct el3_private *)el3_root_dev->priv;
1457 if(lp->mca_slot!=-1)
1458 mca_mark_as_unused(lp->mca_slot);
1463 pm_unregister(lp->pmdev);
1465 next_dev = lp->next_dev;
1466 unregister_netdev(el3_root_dev);
1467 release_region(el3_root_dev->base_addr, EL3_IO_EXTENT);
1468 kfree(el3_root_dev);
1469 el3_root_dev = next_dev;
1476 * compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c509.c"
1477 * version-control: t
1478 * kept-new-versions: 5