Import 2.3.18pre1
[davej-history.git] / drivers / net / 3c509.c
blob60f9f3d091847db8d36424928bbd53774ca629f7
1 /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
2 /*
3 Written 1993-1998 by Donald Becker.
5 Copyright 1994-1998 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 Public License,
9 incorporated herein by reference.
11 This driver is for the 3Com EtherLinkIII series.
13 The author may be reached as becker@cesdis.gsfc.nasa.gov or
14 C/O Center of Excellence in Space Data and Information Sciences
15 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
17 Known limitations:
18 Because of the way 3c509 ISA detection works it's difficult to predict
19 a priori which of several ISA-mode cards will be detected first.
21 This driver does not use predictive interrupt mode, resulting in higher
22 packet latency but lower overhead. If interrupts are disabled for an
23 unusually long time it could also result in missed packets, but in
24 practice this rarely happens.
27 FIXES:
28 Alan Cox: Removed the 'Unexpected interrupt' bug.
29 Michael Meskes: Upgraded to Donald Becker's version 1.07.
30 Alan Cox: Increased the eeprom delay. Regardless of
31 what the docs say some people definitely
32 get problems with lower (but in card spec)
33 delays
34 v1.10 4/21/97 Fixed module code so that multiple cards may be detected,
35 other cleanups. -djb
36 Andrea Arcangeli: Upgraded to Donald Becker's version 1.12.
37 Rick Payne: Fixed SMP race condition
38 v1.13 9/8/97 Made 'max_interrupt_work' an insmod-settable variable -djb
39 v1.14 10/15/97 Avoided waiting..discard message for fast machines -djb
40 v1.15 1/31/98 Faster recovery for Tx errors. -djb
41 v1.16 2/3/98 Different ID port handling to avoid sound cards. -djb
44 static char *version = "3c509.c:1.16 (2.2) 2/3/98 becker@cesdis.gsfc.nasa.gov.\n";
45 /* A few values that may be tweaked. */
47 /* Time in jiffies before concluding the transmitter is hung. */
48 #define TX_TIMEOUT (400*HZ/1000)
49 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
50 static int max_interrupt_work = 10;
52 #include <linux/config.h>
53 #include <linux/module.h>
55 #include <linux/mca.h>
56 #include <linux/sched.h>
57 #include <linux/string.h>
58 #include <linux/interrupt.h>
59 #include <linux/errno.h>
60 #include <linux/in.h>
61 #include <linux/malloc.h>
62 #include <linux/ioport.h>
63 #include <linux/netdevice.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
66 #include <linux/delay.h> /* for udelay() */
67 #include <linux/spinlock.h>
69 #include <asm/bitops.h>
70 #include <asm/io.h>
71 #include <asm/irq.h>
73 #ifdef EL3_DEBUG
74 int el3_debug = EL3_DEBUG;
75 #else
76 int el3_debug = 2;
77 #endif
79 /* To minimize the size of the driver source I only define operating
80 constants if they are used several times. You'll need the manual
81 anyway if you want to understand driver details. */
82 /* Offsets from base I/O address. */
83 #define EL3_DATA 0x00
84 #define EL3_CMD 0x0e
85 #define EL3_STATUS 0x0e
86 #define EEPROM_READ 0x80
88 #define EL3_IO_EXTENT 16
90 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
93 /* The top five bits written to EL3_CMD are a command, the lower
94 11 bits are the parameter, if applicable. */
95 enum c509cmd {
96 TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
97 RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
98 TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
99 FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
100 SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
101 SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
102 StatsDisable = 22<<11, StopCoax = 23<<11,};
104 enum c509status {
105 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
106 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
107 IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
109 /* The SetRxFilter command accepts the following classes: */
110 enum RxFilter {
111 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
113 /* Register window 1 offsets, the window used in normal operation. */
114 #define TX_FIFO 0x00
115 #define RX_FIFO 0x00
116 #define RX_STATUS 0x08
117 #define TX_STATUS 0x0B
118 #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
120 #define WN0_IRQ 0x08 /* Window 0: Set IRQ line in bits 12-15. */
121 #define WN4_MEDIA 0x0A /* Window 4: Various transcvr/media bits. */
122 #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
125 * Must be a power of two (we use a binary and in the
126 * circular queue)
128 #define SKB_QUEUE_SIZE 64
130 struct el3_private {
131 struct enet_statistics stats;
132 struct net_device *next_dev;
133 spinlock_t lock;
134 /* skb send-queue */
135 int head, size;
136 struct sk_buff *queue[SKB_QUEUE_SIZE];
137 char mca_slot;
139 static int id_port = 0x110; /* Start with 0x110 to avoid new sound cards.*/
140 static struct net_device *el3_root_dev = NULL;
142 static ushort id_read_eeprom(int index);
143 static ushort read_eeprom(int ioaddr, int index);
144 static int el3_open(struct net_device *dev);
145 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
146 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
147 static void update_stats(struct net_device *dev);
148 static struct enet_statistics *el3_get_stats(struct net_device *dev);
149 static int el3_rx(struct net_device *dev);
150 static int el3_close(struct net_device *dev);
151 static void set_multicast_list(struct net_device *dev);
153 #ifdef CONFIG_MCA
154 struct el3_mca_adapters_struct {
155 char* name;
156 int id;
159 struct el3_mca_adapters_struct el3_mca_adapters[] = {
160 { "3Com 3c529 EtherLink III (10base2)", 0x627c },
161 { "3Com 3c529 EtherLink III (10baseT)", 0x627d },
162 { "3Com 3c529 EtherLink III (test mode)", 0x62db },
163 { "3Com 3c529 EtherLink III (TP or coax)", 0x62f6 },
164 { "3Com 3c529 EtherLink III (TP)", 0x62f7 },
165 { NULL, 0 },
167 #endif
169 int el3_probe(struct net_device *dev)
171 short lrs_state = 0xff, i;
172 int ioaddr, irq, if_port;
173 u16 phys_addr[3];
174 static int current_tag = 0;
175 int mca_slot = -1;
177 /* First check all slots of the EISA bus. The next slot address to
178 probe is kept in 'eisa_addr' to support multiple probe() calls. */
179 if (EISA_bus) {
180 static int eisa_addr = 0x1000;
181 while (eisa_addr < 0x9000) {
182 ioaddr = eisa_addr;
183 eisa_addr += 0x1000;
185 /* Check the standard EISA ID register for an encoded '3Com'. */
186 if (inw(ioaddr + 0xC80) != 0x6d50)
187 continue;
189 /* Change the register set to the configuration window 0. */
190 outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
192 irq = inw(ioaddr + WN0_IRQ) >> 12;
193 if_port = inw(ioaddr + 6)>>14;
194 for (i = 0; i < 3; i++)
195 phys_addr[i] = htons(read_eeprom(ioaddr, i));
197 /* Restore the "Product ID" to the EEPROM read register. */
198 read_eeprom(ioaddr, 3);
200 /* Was the EISA code an add-on hack? Nahhhhh... */
201 goto found;
205 #ifdef CONFIG_MCA
206 /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch, heavily
207 * modified by Chris Beauregard (cpbeaure@csclub.uwaterloo.ca)
208 * to support standard MCA probing.
210 * redone for multi-card detection by ZP Gu (zpg@castle.net)
211 * now works as a module
214 if( MCA_bus ) {
215 int slot, j;
216 u_char pos4, pos5;
218 for( j = 0; el3_mca_adapters[j].name != NULL; j ++ ) {
219 slot = 0;
220 while( slot != MCA_NOTFOUND ) {
221 slot = mca_find_unused_adapter(
222 el3_mca_adapters[j].id, slot );
223 if( slot == MCA_NOTFOUND ) break;
225 /* if we get this far, an adapter has been
226 * detected and is enabled
229 printk("3c509: found %s at slot %d\n",
230 el3_mca_adapters[j].name, slot + 1 );
232 pos4 = mca_read_stored_pos( slot, 4 );
233 pos5 = mca_read_stored_pos( slot, 5 );
235 ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
236 irq = pos5 & 0x0f;
238 /* probing for a card at a particular IO/IRQ */
239 if(dev && ((dev->irq >= 1 && dev->irq != irq) ||
240 (dev->base_addr >= 1 && dev->base_addr != ioaddr))) {
241 slot++; /* probing next slot */
242 continue;
245 /* claim the slot */
246 mca_set_adapter_name(slot, el3_mca_adapters[j].name);
247 mca_set_adapter_procfn(slot, NULL, NULL);
248 mca_mark_as_used(slot);
250 if_port = pos4 & 0x03;
251 if (el3_debug > 2) {
252 printk("3c529: irq %d ioaddr 0x%x ifport %d\n", irq, ioaddr, if_port);
254 for (i = 0; i < 3; i++) {
255 phys_addr[i] = htons(read_eeprom(ioaddr, i));
258 mca_slot = slot;
260 goto found;
263 /* if we get here, we didn't find an MCA adapter */
264 return -ENODEV;
266 #endif
267 /* Reset the ISA PnP mechanism on 3c509b. */
268 outb(0x02, 0x279); /* Select PnP config control register. */
269 outb(0x02, 0xA79); /* Return to WaitForKey state. */
270 /* Select an open I/O location at 0x1*0 to do contention select. */
271 for ( ; id_port < 0x200; id_port += 0x10) {
272 if (check_region(id_port, 1))
273 continue;
274 outb(0x00, id_port);
275 outb(0xff, id_port);
276 if (inb(id_port) & 0x01)
277 break;
279 if (id_port >= 0x200) {
280 /* Rare -- do we really need a warning? */
281 printk(" WARNING: No I/O port available for 3c509 activation.\n");
282 return -ENODEV;
284 /* Next check for all ISA bus boards by sending the ID sequence to the
285 ID_PORT. We find cards past the first by setting the 'current_tag'
286 on cards as they are found. Cards with their tag set will not
287 respond to subsequent ID sequences. */
289 outb(0x00, id_port);
290 outb(0x00, id_port);
291 for(i = 0; i < 255; i++) {
292 outb(lrs_state, id_port);
293 lrs_state <<= 1;
294 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
297 /* For the first probe, clear all board's tag registers. */
298 if (current_tag == 0)
299 outb(0xd0, id_port);
300 else /* Otherwise kill off already-found boards. */
301 outb(0xd8, id_port);
303 if (id_read_eeprom(7) != 0x6d50) {
304 return -ENODEV;
307 /* Read in EEPROM data, which does contention-select.
308 Only the lowest address board will stay "on-line".
309 3Com got the byte order backwards. */
310 for (i = 0; i < 3; i++) {
311 phys_addr[i] = htons(id_read_eeprom(i));
315 unsigned int iobase = id_read_eeprom(8);
316 if_port = iobase >> 14;
317 ioaddr = 0x200 + ((iobase & 0x1f) << 4);
319 irq = id_read_eeprom(9) >> 12;
321 if (dev) { /* Set passed-in IRQ or I/O Addr. */
322 if (dev->irq > 1 && dev->irq < 16)
323 irq = dev->irq;
325 if (dev->base_addr) {
326 if (dev->mem_end == 0x3c509 /* Magic key */
327 && dev->base_addr >= 0x200 && dev->base_addr <= 0x3e0)
328 ioaddr = dev->base_addr & 0x3f0;
329 else if (dev->base_addr != ioaddr)
330 return -ENODEV;
334 /* Set the adaptor tag so that the next card can be found. */
335 outb(0xd0 + ++current_tag, id_port);
337 /* Activate the adaptor at the EEPROM location. */
338 outb((ioaddr >> 4) | 0xe0, id_port);
340 EL3WINDOW(0);
341 if (inw(ioaddr) != 0x6d50)
342 return -ENODEV;
344 /* Free the interrupt so that some other card can use it. */
345 outw(0x0f00, ioaddr + WN0_IRQ);
346 found:
347 if (dev == NULL) {
348 dev = init_etherdev(dev, sizeof(struct el3_private));
350 memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
351 dev->base_addr = ioaddr;
352 dev->irq = irq;
353 dev->if_port = (dev->mem_start & 0x1f) ? dev->mem_start & 3 : if_port;
355 request_region(dev->base_addr, EL3_IO_EXTENT, "3c509");
358 const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
359 printk("%s: 3c509 at %#3.3lx tag %d, %s port, address ",
360 dev->name, dev->base_addr, current_tag, if_names[dev->if_port]);
363 /* Read in the station address. */
364 for (i = 0; i < 6; i++)
365 printk(" %2.2x", dev->dev_addr[i]);
366 printk(", IRQ %d.\n", dev->irq);
368 /* Make up a EL3-specific-data structure. */
369 if (dev->priv == NULL)
370 dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL);
371 if (dev->priv == NULL)
372 return -ENOMEM;
373 memset(dev->priv, 0, sizeof(struct el3_private));
375 ((struct el3_private *)dev->priv)->mca_slot = mca_slot;
376 ((struct el3_private *)dev->priv)->next_dev = el3_root_dev;
377 el3_root_dev = dev;
379 if (el3_debug > 0)
380 printk(version);
382 /* The EL3-specific entries in the device structure. */
383 dev->open = &el3_open;
384 dev->hard_start_xmit = &el3_start_xmit;
385 dev->stop = &el3_close;
386 dev->get_stats = &el3_get_stats;
387 dev->set_multicast_list = &set_multicast_list;
389 /* Fill in the generic fields of the device structure. */
390 ether_setup(dev);
391 return 0;
394 /* Read a word from the EEPROM using the regular EEPROM access register.
395 Assume that we are in register window zero.
397 static ushort read_eeprom(int ioaddr, int index)
399 outw(EEPROM_READ + index, ioaddr + 10);
400 /* Pause for at least 162 us. for the read to take place. */
401 udelay (500);
402 return inw(ioaddr + 12);
405 /* Read a word from the EEPROM when in the ISA ID probe state. */
406 static ushort id_read_eeprom(int index)
408 int bit, word = 0;
410 /* Issue read command, and pause for at least 162 us. for it to complete.
411 Assume extra-fast 16Mhz bus. */
412 outb(EEPROM_READ + index, id_port);
414 /* Pause for at least 162 us. for the read to take place. */
415 udelay (500);
417 for (bit = 15; bit >= 0; bit--)
418 word = (word << 1) + (inb(id_port) & 0x01);
420 if (el3_debug > 3)
421 printk(" 3c509 EEPROM word %d %#4.4x.\n", index, word);
423 return word;
427 static int
428 el3_open(struct net_device *dev)
430 int ioaddr = dev->base_addr;
431 int i;
433 outw(TxReset, ioaddr + EL3_CMD);
434 outw(RxReset, ioaddr + EL3_CMD);
435 outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
437 /* Set the spinlock before grabbing IRQ! */
438 ((struct el3_private *)dev->priv)->lock = (spinlock_t) SPIN_LOCK_UNLOCKED;
440 if (request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev)) {
441 return -EAGAIN;
444 EL3WINDOW(0);
445 if (el3_debug > 3)
446 printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev->name,
447 dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
449 /* Activate board: this is probably unnecessary. */
450 outw(0x0001, ioaddr + 4);
452 /* Set the IRQ line. */
453 outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
455 /* Set the station address in window 2 each time opened. */
456 EL3WINDOW(2);
458 for (i = 0; i < 6; i++)
459 outb(dev->dev_addr[i], ioaddr + i);
461 if (dev->if_port == 3)
462 /* Start the thinnet transceiver. We should really wait 50ms...*/
463 outw(StartCoax, ioaddr + EL3_CMD);
464 else if (dev->if_port == 0) {
465 /* 10baseT interface, enabled link beat and jabber check. */
466 EL3WINDOW(4);
467 outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
470 /* Switch to the stats window, and clear all stats by reading. */
471 outw(StatsDisable, ioaddr + EL3_CMD);
472 EL3WINDOW(6);
473 for (i = 0; i < 9; i++)
474 inb(ioaddr + i);
475 inw(ioaddr + 10);
476 inw(ioaddr + 12);
478 /* Switch to register set 1 for normal use. */
479 EL3WINDOW(1);
481 /* Accept b-case and phys addr only. */
482 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
483 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
485 dev->interrupt = 0;
486 dev->tbusy = 0;
487 dev->start = 1;
489 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
490 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
491 /* Allow status bits to be seen. */
492 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
493 /* Ack all pending events, and set active indicator mask. */
494 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
495 ioaddr + EL3_CMD);
496 outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
497 ioaddr + EL3_CMD);
499 if (el3_debug > 3)
500 printk("%s: Opened 3c509 IRQ %d status %4.4x.\n",
501 dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
503 MOD_INC_USE_COUNT;
504 return 0; /* Always succeed */
507 static int
508 el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
510 struct el3_private *lp = (struct el3_private *)dev->priv;
511 int ioaddr = dev->base_addr;
513 /* Transmitter timeout, serious problems. */
514 if (dev->tbusy) {
515 int tickssofar = jiffies - dev->trans_start;
516 if (tickssofar < TX_TIMEOUT)
517 return 1;
518 printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
519 "Tx FIFO room %d.\n",
520 dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
521 inw(ioaddr + TX_FREE));
522 lp->stats.tx_errors++;
523 dev->trans_start = jiffies;
524 /* Issue TX_RESET and TX_START commands. */
525 outw(TxReset, ioaddr + EL3_CMD);
526 outw(TxEnable, ioaddr + EL3_CMD);
527 dev->tbusy = 0;
530 lp->stats.tx_bytes += skb->len;
532 if (el3_debug > 4) {
533 printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
534 dev->name, skb->len, inw(ioaddr + EL3_STATUS));
536 #if 0
537 #ifndef final_version
538 { /* Error-checking code, delete someday. */
539 ushort status = inw(ioaddr + EL3_STATUS);
540 if (status & 0x0001 /* IRQ line active, missed one. */
541 && inw(ioaddr + EL3_STATUS) & 1) { /* Make sure. */
542 printk("%s: Missed interrupt, status then %04x now %04x"
543 " Tx %2.2x Rx %4.4x.\n", dev->name, status,
544 inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
545 inw(ioaddr + RX_STATUS));
546 /* Fake interrupt trigger by masking, acknowledge interrupts. */
547 outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
548 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
549 ioaddr + EL3_CMD);
550 outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
553 #endif
554 #endif
555 /* Avoid timer-based retransmission conflicts. */
556 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0)
557 printk("%s: Transmitter access conflict.\n", dev->name);
558 else {
560 * We lock the driver against other processors. Note
561 * we don't need to lock versus the IRQ as we suspended
562 * that. This means that we lose the ability to take
563 * an RX during a TX upload. That sucks a bit with SMP
564 * on an original 3c509 (2K buffer)
566 * Using disable_irq stops us crapping on other
567 * time sensitive devices.
570 #ifdef __SMP__
571 disable_irq_nosync(dev->irq);
572 spin_lock(&lp->lock);
573 #endif
575 /* Put out the doubleword header... */
576 outw(skb->len, ioaddr + TX_FIFO);
577 outw(0x00, ioaddr + TX_FIFO);
578 /* ... and the packet rounded to a doubleword. */
579 #ifdef __powerpc__
580 outsl_unswapped(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
581 #else
582 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
583 #endif
585 dev->trans_start = jiffies;
586 if (inw(ioaddr + TX_FREE) > 1536) {
587 dev->tbusy = 0;
588 } else
589 /* Interrupt us when the FIFO has room for max-sized packet. */
590 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
591 #ifdef __SMP__
592 spin_unlock(&lp->lock);
593 enable_irq(dev->irq);
594 #endif
597 dev_kfree_skb (skb);
599 /* Clear the Tx status stack. */
601 short tx_status;
602 int i = 4;
604 while (--i > 0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
605 if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
606 if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
607 if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
608 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
611 return 0;
614 /* The EL3 interrupt handler. */
615 static void
616 el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
618 struct net_device *dev = (struct net_device *)dev_id;
619 struct el3_private *lp;
620 int ioaddr, status;
621 int i = max_interrupt_work;
623 if (dev == NULL) {
624 printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
625 return;
628 lp = (struct el3_private *)dev->priv;
629 spin_lock(&lp->lock);
631 if (dev->interrupt)
632 printk("%s: Re-entering the interrupt handler.\n", dev->name);
633 dev->interrupt = 1;
635 ioaddr = dev->base_addr;
637 if (el3_debug > 4) {
638 status = inw(ioaddr + EL3_STATUS);
639 printk("%s: interrupt, status %4.4x.\n", dev->name, status);
642 while ((status = inw(ioaddr + EL3_STATUS)) &
643 (IntLatch | RxComplete | StatsFull)) {
645 if (status & RxComplete)
646 el3_rx(dev);
648 if (status & TxAvailable) {
649 if (el3_debug > 5)
650 printk(" TX room bit was handled.\n");
651 /* There's room in the FIFO for a full-sized packet. */
652 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
653 dev->tbusy = 0;
654 mark_bh(NET_BH);
656 if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
657 /* Handle all uncommon interrupts. */
658 if (status & StatsFull) /* Empty statistics. */
659 update_stats(dev);
660 if (status & RxEarly) { /* Rx early is unused. */
661 el3_rx(dev);
662 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
664 if (status & TxComplete) { /* Really Tx error. */
665 struct el3_private *lp = (struct el3_private *)dev->priv;
666 short tx_status;
667 int i = 4;
669 while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
670 if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
671 if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
672 if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
673 outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
676 if (status & AdapterFailure) {
677 /* Adapter failure requires Rx reset and reinit. */
678 outw(RxReset, ioaddr + EL3_CMD);
679 /* Set the Rx filter to the current state. */
680 outw(SetRxFilter | RxStation | RxBroadcast
681 | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
682 | (dev->flags & IFF_PROMISC ? RxProm : 0),
683 ioaddr + EL3_CMD);
684 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
685 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
689 if (--i < 0) {
690 printk("%s: Infinite loop in interrupt, status %4.4x.\n",
691 dev->name, status);
692 /* Clear all interrupts. */
693 outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
694 break;
696 /* Acknowledge the IRQ. */
697 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
700 if (el3_debug > 4) {
701 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
702 inw(ioaddr + EL3_STATUS));
704 spin_unlock(&lp->lock);
705 dev->interrupt = 0;
706 return;
710 static struct enet_statistics *
711 el3_get_stats(struct net_device *dev)
713 struct el3_private *lp = (struct el3_private *)dev->priv;
714 unsigned long flags;
717 * This is fast enough not to bother with disable IRQ
718 * stuff.
721 spin_lock_irqsave(&lp->lock, flags);
722 update_stats(dev);
723 spin_unlock_irqrestore(&lp->lock, flags);
724 return &lp->stats;
727 /* Update statistics. We change to register window 6, so this should be run
728 single-threaded if the device is active. This is expected to be a rare
729 operation, and it's simpler for the rest of the driver to assume that
730 window 1 is always valid rather than use a special window-state variable.
732 static void update_stats(struct net_device *dev)
734 struct el3_private *lp = (struct el3_private *)dev->priv;
735 int ioaddr = dev->base_addr;
737 if (el3_debug > 5)
738 printk(" Updating the statistics.\n");
739 /* Turn off statistics updates while reading. */
740 outw(StatsDisable, ioaddr + EL3_CMD);
741 /* Switch to the stats window, and read everything. */
742 EL3WINDOW(6);
743 lp->stats.tx_carrier_errors += inb(ioaddr + 0);
744 lp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
745 /* Multiple collisions. */ inb(ioaddr + 2);
746 lp->stats.collisions += inb(ioaddr + 3);
747 lp->stats.tx_window_errors += inb(ioaddr + 4);
748 lp->stats.rx_fifo_errors += inb(ioaddr + 5);
749 lp->stats.tx_packets += inb(ioaddr + 6);
750 /* Rx packets */ inb(ioaddr + 7);
751 /* Tx deferrals */ inb(ioaddr + 8);
752 inw(ioaddr + 10); /* Total Rx and Tx octets. */
753 inw(ioaddr + 12);
755 /* Back to window 1, and turn statistics back on. */
756 EL3WINDOW(1);
757 outw(StatsEnable, ioaddr + EL3_CMD);
758 return;
761 static int
762 el3_rx(struct net_device *dev)
764 struct el3_private *lp = (struct el3_private *)dev->priv;
765 int ioaddr = dev->base_addr;
766 short rx_status;
768 if (el3_debug > 5)
769 printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n",
770 inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
771 while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
772 if (rx_status & 0x4000) { /* Error, update stats. */
773 short error = rx_status & 0x3800;
775 outw(RxDiscard, ioaddr + EL3_CMD);
776 lp->stats.rx_errors++;
777 switch (error) {
778 case 0x0000: lp->stats.rx_over_errors++; break;
779 case 0x0800: lp->stats.rx_length_errors++; break;
780 case 0x1000: lp->stats.rx_frame_errors++; break;
781 case 0x1800: lp->stats.rx_length_errors++; break;
782 case 0x2000: lp->stats.rx_frame_errors++; break;
783 case 0x2800: lp->stats.rx_crc_errors++; break;
785 } else {
786 short pkt_len = rx_status & 0x7ff;
787 struct sk_buff *skb;
789 skb = dev_alloc_skb(pkt_len+5);
790 lp->stats.rx_bytes += pkt_len;
791 if (el3_debug > 4)
792 printk("Receiving packet size %d status %4.4x.\n",
793 pkt_len, rx_status);
794 if (skb != NULL) {
795 skb->dev = dev;
796 skb_reserve(skb, 2); /* Align IP on 16 byte */
798 /* 'skb->data' points to the start of sk_buff data area. */
799 #ifdef __powerpc__
800 insl_unswapped(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
801 (pkt_len + 3) >> 2);
802 #else
803 insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
804 (pkt_len + 3) >> 2);
805 #endif
807 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
808 skb->protocol = eth_type_trans(skb,dev);
809 netif_rx(skb);
810 lp->stats.rx_packets++;
811 continue;
813 outw(RxDiscard, ioaddr + EL3_CMD);
814 lp->stats.rx_dropped++;
815 if (el3_debug)
816 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
817 dev->name, pkt_len);
819 inw(ioaddr + EL3_STATUS); /* Delay. */
820 while (inw(ioaddr + EL3_STATUS) & 0x1000)
821 printk(KERN_DEBUG " Waiting for 3c509 to discard packet, status %x.\n",
822 inw(ioaddr + EL3_STATUS) );
825 return 0;
829 * Set or clear the multicast filter for this adaptor.
831 static void
832 set_multicast_list(struct net_device *dev)
834 unsigned long flags;
835 struct el3_private *lp = (struct el3_private *)dev->priv;
836 int ioaddr = dev->base_addr;
838 if (el3_debug > 1) {
839 static int old = 0;
840 if (old != dev->mc_count) {
841 old = dev->mc_count;
842 printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
845 spin_lock_irqsave(&lp->lock, flags);
846 if (dev->flags&IFF_PROMISC) {
847 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
848 ioaddr + EL3_CMD);
850 else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
851 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
853 else
854 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
855 spin_unlock_irqrestore(&lp->lock, flags);
858 static int
859 el3_close(struct net_device *dev)
861 int ioaddr = dev->base_addr;
863 if (el3_debug > 2)
864 printk("%s: Shutting down ethercard.\n", dev->name);
866 dev->tbusy = 1;
867 dev->start = 0;
869 /* Turn off statistics ASAP. We update lp->stats below. */
870 outw(StatsDisable, ioaddr + EL3_CMD);
872 /* Disable the receiver and transmitter. */
873 outw(RxDisable, ioaddr + EL3_CMD);
874 outw(TxDisable, ioaddr + EL3_CMD);
876 if (dev->if_port == 3)
877 /* Turn off thinnet power. Green! */
878 outw(StopCoax, ioaddr + EL3_CMD);
879 else if (dev->if_port == 0) {
880 /* Disable link beat and jabber, if_port may change ere next open(). */
881 EL3WINDOW(4);
882 outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
885 free_irq(dev->irq, dev);
886 /* Switching back to window 0 disables the IRQ. */
887 EL3WINDOW(0);
888 /* But we explicitly zero the IRQ line select anyway. */
889 outw(0x0f00, ioaddr + WN0_IRQ);
891 update_stats(dev);
892 MOD_DEC_USE_COUNT;
893 return 0;
896 #ifdef MODULE
897 /* Parameters that may be passed into the module. */
898 static int debug = -1;
899 static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
900 static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1};
902 MODULE_PARM(debug,"i");
903 MODULE_PARM(irq,"1-8i");
904 MODULE_PARM(xcvr,"1-8i");
907 init_module(void)
909 int el3_cards = 0;
911 if (debug >= 0)
912 el3_debug = debug;
914 el3_root_dev = NULL;
915 while (el3_probe(0) == 0) {
916 if (irq[el3_cards] > 1)
917 el3_root_dev->irq = irq[el3_cards];
918 if (xcvr[el3_cards] >= 0)
919 el3_root_dev->if_port = xcvr[el3_cards];
920 el3_cards++;
923 return el3_cards ? 0 : -ENODEV;
926 void
927 cleanup_module(void)
929 struct net_device *next_dev;
931 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
932 while (el3_root_dev) {
933 struct el3_private *lp = (struct el3_private *)el3_root_dev->priv;
934 #ifdef CONFIG_MCA
935 if(lp->mca_slot!=-1)
936 mca_mark_as_unused(lp->mca_slot);
937 #endif
938 next_dev = lp->next_dev;
939 unregister_netdev(el3_root_dev);
940 release_region(el3_root_dev->base_addr, EL3_IO_EXTENT);
941 kfree(el3_root_dev);
942 el3_root_dev = next_dev;
945 #endif /* MODULE */
948 * Local variables:
949 * compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c509.c"
950 * version-control: t
951 * kept-new-versions: 5
952 * tab-width: 4
953 * End: