Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / net / 3c509.c
1 /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
2 /*
3         Written 1993-1998 by Donald Becker.
4
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.
10
11         This driver is for the 3Com EtherLinkIII series.
12
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
16
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.
20
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.
25
26
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
42 */
43
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. */
46
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;
51
52 #include <linux/config.h>
53 #include <linux/module.h>
54
55 #include <linux/mca.h>
56 #include <linux/isapnp.h>
57 #include <linux/sched.h>
58 #include <linux/string.h>
59 #include <linux/interrupt.h>
60 #include <linux/errno.h>
61 #include <linux/in.h>
62 #include <linux/malloc.h>
63 #include <linux/ioport.h>
64 #include <linux/netdevice.h>
65 #include <linux/etherdevice.h>
66 #include <linux/skbuff.h>
67 #include <linux/delay.h>        /* for udelay() */
68 #include <linux/spinlock.h>
69
70 #include <asm/bitops.h>
71 #include <asm/io.h>
72 #include <asm/irq.h>
73
74 #ifdef EL3_DEBUG
75 int el3_debug = EL3_DEBUG;
76 #else
77 int el3_debug = 2;
78 #endif
79
80 /* To minimize the size of the driver source I only define operating
81    constants if they are used several times.  You'll need the manual
82    anyway if you want to understand driver details. */
83 /* Offsets from base I/O address. */
84 #define EL3_DATA 0x00
85 #define EL3_CMD 0x0e
86 #define EL3_STATUS 0x0e
87 #define  EEPROM_READ 0x80
88
89 #define EL3_IO_EXTENT   16
90
91 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
92
93
94 /* The top five bits written to EL3_CMD are a command, the lower
95    11 bits are the parameter, if applicable. */
96 enum c509cmd {
97         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
98         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
99         TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
100         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
101         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
102         SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
103         StatsDisable = 22<<11, StopCoax = 23<<11,};
104
105 enum c509status {
106         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
107         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
108         IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
109
110 /* The SetRxFilter command accepts the following classes: */
111 enum RxFilter {
112         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
113
114 /* Register window 1 offsets, the window used in normal operation. */
115 #define TX_FIFO         0x00
116 #define RX_FIFO         0x00
117 #define RX_STATUS       0x08
118 #define TX_STATUS       0x0B
119 #define TX_FREE         0x0C            /* Remaining free bytes in Tx buffer. */
120
121 #define WN0_IRQ         0x08            /* Window 0: Set IRQ line in bits 12-15. */
122 #define WN4_MEDIA       0x0A            /* Window 4: Various transcvr/media bits. */
123 #define  MEDIA_TP       0x00C0          /* Enable link beat and jabber for 10baseT. */
124
125 /*
126  * Must be a power of two (we use a binary and in the
127  * circular queue)
128  */
129 #define SKB_QUEUE_SIZE  64
130
131 struct el3_private {
132         struct net_device_stats stats;
133         struct net_device *next_dev;
134         spinlock_t lock;
135         /* skb send-queue */
136         int head, size;
137         struct sk_buff *queue[SKB_QUEUE_SIZE];
138         char mca_slot;
139 };
140 static int id_port = 0x110;             /* Start with 0x110 to avoid new sound cards.*/
141 static struct net_device *el3_root_dev = NULL;
142
143 static ushort id_read_eeprom(int index);
144 static ushort read_eeprom(int ioaddr, int index);
145 static int el3_open(struct net_device *dev);
146 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
147 static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
148 static void update_stats(struct net_device *dev);
149 static struct net_device_stats *el3_get_stats(struct net_device *dev);
150 static int el3_rx(struct net_device *dev);
151 static int el3_close(struct net_device *dev);
152 static void set_multicast_list(struct net_device *dev);
153 static void el3_tx_timeout (struct net_device *dev);
154
155 #ifdef CONFIG_MCA
156 struct el3_mca_adapters_struct {
157         char* name;
158         int id;
159 };
160
161 struct el3_mca_adapters_struct el3_mca_adapters[] = {
162         { "3Com 3c529 EtherLink III (10base2)", 0x627c },
163         { "3Com 3c529 EtherLink III (10baseT)", 0x627d },
164         { "3Com 3c529 EtherLink III (test mode)", 0x62db },
165         { "3Com 3c529 EtherLink III (TP or coax)", 0x62f6 },
166         { "3Com 3c529 EtherLink III (TP)", 0x62f7 },
167         { NULL, 0 },
168 };
169 #endif
170
171 #ifdef __ISAPNP__
172 struct el3_isapnp_adapters_struct {
173         unsigned short vendor, function;
174         char *name;
175 };
176 static struct el3_isapnp_adapters_struct el3_isapnp_adapters[] = {
177         {ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090), "3Com Etherlink III (TP)"},
178         {ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091), "3Com Etherlink III"},
179         {ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094), "3Com Etherlink III (combo)"},
180         {ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095), "3Com Etherlink III (TPO)"},
181         {ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098), "3Com Etherlink III (TPC)"},
182         {ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8), "3Com Etherlink III compatible"},
183         {0, }
184 };
185 static u16 el3_isapnp_phys_addr[8][3];
186 #endif /* CONFIG_ISAPNP */
187 #ifdef __ISAPNP__
188 static int nopnp;
189 #endif
190
191 int el3_probe(struct net_device *dev)
192 {
193         struct el3_private *lp;
194         short lrs_state = 0xff, i;
195         int ioaddr, irq, if_port;
196         u16 phys_addr[3];
197         static int current_tag = 0;
198         int mca_slot = -1;
199 #ifdef __ISAPNP__
200         static int pnp_cards = 0;
201 #endif /* __ISAPNP__ */
202
203         if (dev) SET_MODULE_OWNER(dev);
204
205         /* First check all slots of the EISA bus.  The next slot address to
206            probe is kept in 'eisa_addr' to support multiple probe() calls. */
207         if (EISA_bus) {
208                 static int eisa_addr = 0x1000;
209                 while (eisa_addr < 0x9000) {
210                         ioaddr = eisa_addr;
211                         eisa_addr += 0x1000;
212
213                         /* Check the standard EISA ID register for an encoded '3Com'. */
214                         if (inw(ioaddr + 0xC80) != 0x6d50)
215                                 continue;
216
217                         /* Change the register set to the configuration window 0. */
218                         outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
219
220                         irq = inw(ioaddr + WN0_IRQ) >> 12;
221                         if_port = inw(ioaddr + 6)>>14;
222                         for (i = 0; i < 3; i++)
223                                 phys_addr[i] = htons(read_eeprom(ioaddr, i));
224
225                         /* Restore the "Product ID" to the EEPROM read register. */
226                         read_eeprom(ioaddr, 3);
227
228                         /* Was the EISA code an add-on hack?  Nahhhhh... */
229                         goto found;
230                 }
231         }
232
233 #ifdef CONFIG_MCA
234         /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch, heavily
235          * modified by Chris Beauregard (cpbeaure@csclub.uwaterloo.ca)
236          * to support standard MCA probing.
237          *
238          * redone for multi-card detection by ZP Gu (zpg@castle.net)
239          * now works as a module
240          */
241
242         if( MCA_bus ) {
243                 int slot, j;
244                 u_char pos4, pos5;
245
246                 for( j = 0; el3_mca_adapters[j].name != NULL; j ++ ) {
247                         slot = 0;
248                         while( slot != MCA_NOTFOUND ) {
249                                 slot = mca_find_unused_adapter(
250                                         el3_mca_adapters[j].id, slot );
251                                 if( slot == MCA_NOTFOUND ) break;
252
253                                 /* if we get this far, an adapter has been
254                                  * detected and is enabled
255                                  */
256
257                                 pos4 = mca_read_stored_pos( slot, 4 );
258                                 pos5 = mca_read_stored_pos( slot, 5 );
259
260                                 ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
261                                 irq = pos5 & 0x0f;
262
263                                 /* probing for a card at a particular IO/IRQ */
264                                 if(dev && ((dev->irq >= 1 && dev->irq != irq) ||
265                                 (dev->base_addr >= 1 && dev->base_addr != ioaddr))) {
266                                         slot++;         /* probing next slot */
267                                         continue;
268                                 }
269
270                                 printk("3c509: found %s at slot %d\n",
271                                         el3_mca_adapters[j].name, slot + 1 );
272
273                                 /* claim the slot */
274                                 mca_set_adapter_name(slot, el3_mca_adapters[j].name);
275                                 mca_set_adapter_procfn(slot, NULL, NULL);
276                                 mca_mark_as_used(slot);
277
278                                 if_port = pos4 & 0x03;
279                                 if (el3_debug > 2) {
280                                         printk("3c529: irq %d  ioaddr 0x%x  ifport %d\n", irq, ioaddr, if_port);
281                                 }
282                                 EL3WINDOW(0);
283                                 for (i = 0; i < 3; i++) {
284                                         phys_addr[i] = htons(read_eeprom(ioaddr, i));
285                                 }
286                                 
287                                 mca_slot = slot;
288
289                                 goto found;
290                         }
291                 }
292                 /* if we get here, we didn't find an MCA adapter */
293                 return -ENODEV;
294         }
295 #endif /* CONFIG_MCA */
296
297 #ifdef __ISAPNP__
298         if (nopnp == 1)
299                 goto no_pnp;
300
301         for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
302                 struct pci_dev *idev = NULL;
303                 int j;
304                 while ((idev = isapnp_find_dev(NULL,
305                                                 el3_isapnp_adapters[i].vendor,
306                                                 el3_isapnp_adapters[i].function,
307                                                 idev))) {
308                         idev->prepare(idev);
309                         /* Deactivation is needed if the driver was called
310                            with "nopnp=1" before, does not harm if not. */
311                         idev->deactivate(idev);
312                         idev->activate(idev);
313                         if (!idev->resource[0].start || check_region(idev->resource[0].start, EL3_IO_EXTENT))
314                                 continue;
315                         ioaddr = idev->resource[0].start;
316                         if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP"))
317                                 return -EBUSY;
318                         irq = idev->irq_resource[0].start;
319                         if (el3_debug > 3)
320                                 printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
321                                         el3_isapnp_adapters[i].name, ioaddr, irq);
322                         EL3WINDOW(0);
323                         for (j = 0; j < 3; j++)
324                                 el3_isapnp_phys_addr[pnp_cards][j] =
325                                         phys_addr[j] =
326                                                 htons(read_eeprom(ioaddr, j));
327                         if_port = read_eeprom(ioaddr, 8) >> 14;
328                         pnp_cards++;
329                         goto found;
330                 }
331         }
332 no_pnp:
333 #endif /* __ISAPNP__ */
334
335         /* Select an open I/O location at 0x1*0 to do contention select. */
336         for ( ; id_port < 0x200; id_port += 0x10) {
337                 if (check_region(id_port, 1))
338                         continue;
339                 outb(0x00, id_port);
340                 outb(0xff, id_port);
341                 if (inb(id_port) & 0x01)
342                         break;
343         }
344         if (id_port >= 0x200) {
345                 /* Rare -- do we really need a warning? */
346                 printk(" WARNING: No I/O port available for 3c509 activation.\n");
347                 return -ENODEV;
348         }
349         /* Next check for all ISA bus boards by sending the ID sequence to the
350            ID_PORT.  We find cards past the first by setting the 'current_tag'
351            on cards as they are found.  Cards with their tag set will not
352            respond to subsequent ID sequences. */
353
354         outb(0x00, id_port);
355         outb(0x00, id_port);
356         for(i = 0; i < 255; i++) {
357                 outb(lrs_state, id_port);
358                 lrs_state <<= 1;
359                 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
360         }
361
362         /* For the first probe, clear all board's tag registers. */
363         if (current_tag == 0)
364                 outb(0xd0, id_port);
365         else                            /* Otherwise kill off already-found boards. */
366                 outb(0xd8, id_port);
367
368         if (id_read_eeprom(7) != 0x6d50) {
369                 return -ENODEV;
370         }
371
372         /* Read in EEPROM data, which does contention-select.
373            Only the lowest address board will stay "on-line".
374            3Com got the byte order backwards. */
375         for (i = 0; i < 3; i++) {
376                 phys_addr[i] = htons(id_read_eeprom(i));
377         }
378
379 #ifdef __ISAPNP__
380         if (nopnp == 0) {
381                 /* The ISA PnP 3c509 cards respond to the ID sequence.
382                    This check is needed in order not to register them twice. */
383                 for (i = 0; i < pnp_cards; i++) {
384                         if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
385                             phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
386                             phys_addr[2] == el3_isapnp_phys_addr[i][2])
387                         {
388                                 if (el3_debug > 3)
389                                         printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
390                                                 phys_addr[0] & 0xff, phys_addr[0] >> 8,
391                                                 phys_addr[1] & 0xff, phys_addr[1] >> 8,
392                                                 phys_addr[2] & 0xff, phys_addr[2] >> 8);
393                                 /* Set the adaptor tag so that the next card can be found. */
394                                 outb(0xd0 + ++current_tag, id_port);
395                                 goto no_pnp;
396                         }
397                 }
398         }
399 #endif /* __ISAPNP__ */
400
401         {
402                 unsigned int iobase = id_read_eeprom(8);
403                 if_port = iobase >> 14;
404                 ioaddr = 0x200 + ((iobase & 0x1f) << 4);
405         }
406         irq = id_read_eeprom(9) >> 12;
407
408         if (dev) {                                      /* Set passed-in IRQ or I/O Addr. */
409                 if (dev->irq > 1  &&  dev->irq < 16)
410                         irq = dev->irq;
411
412                 if (dev->base_addr) {
413                         if (dev->mem_end == 0x3c509                     /* Magic key */
414                                 && dev->base_addr >= 0x200  &&  dev->base_addr <= 0x3e0)
415                                 ioaddr = dev->base_addr & 0x3f0;
416                         else if (dev->base_addr != ioaddr)
417                                 return -ENODEV;
418                 }
419         }
420
421         if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
422                 return -EBUSY;
423
424         /* Set the adaptor tag so that the next card can be found. */
425         outb(0xd0 + ++current_tag, id_port);
426
427         /* Activate the adaptor at the EEPROM location. */
428         outb((ioaddr >> 4) | 0xe0, id_port);
429
430         EL3WINDOW(0);
431         if (inw(ioaddr) != 0x6d50) {
432                 release_region(ioaddr, EL3_IO_EXTENT);
433                 return -ENODEV;
434         }
435
436         /* Free the interrupt so that some other card can use it. */
437         outw(0x0f00, ioaddr + WN0_IRQ);
438  found:
439         if (dev == NULL) {
440                 dev = init_etherdev(dev, sizeof(struct el3_private));
441                 if (dev == NULL) {
442                         release_region(ioaddr, EL3_IO_EXTENT);
443                         return -ENOMEM;
444                 }
445                 SET_MODULE_OWNER(dev);
446         }
447         memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
448         dev->base_addr = ioaddr;
449         dev->irq = irq;
450         dev->if_port = (dev->mem_start & 0x1f) ? dev->mem_start & 3 : if_port;
451
452         {
453                 const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
454                 printk("%s: 3c509 at %#3.3lx, %s port, address ",
455                            dev->name, dev->base_addr, if_names[dev->if_port]);
456         }
457
458         /* Read in the station address. */
459         for (i = 0; i < 6; i++)
460                 printk(" %2.2x", dev->dev_addr[i]);
461         printk(", IRQ %d.\n", dev->irq);
462
463         /* Make up a EL3-specific-data structure. */
464         if (dev->priv == NULL)
465                 dev->priv = kmalloc(sizeof(struct el3_private), GFP_KERNEL);
466         if (dev->priv == NULL)
467                 return -ENOMEM;
468         memset(dev->priv, 0, sizeof(struct el3_private));
469         
470         lp = dev->priv;
471         lp->mca_slot = mca_slot;
472         lp->next_dev = el3_root_dev;
473         spin_lock_init(&lp->lock);
474         el3_root_dev = dev;
475
476         if (el3_debug > 0)
477                 printk(version);
478
479         /* The EL3-specific entries in the device structure. */
480         dev->open = &el3_open;
481         dev->hard_start_xmit = &el3_start_xmit;
482         dev->stop = &el3_close;
483         dev->get_stats = &el3_get_stats;
484         dev->set_multicast_list = &set_multicast_list;
485         dev->tx_timeout = el3_tx_timeout;
486         dev->watchdog_timeo = TX_TIMEOUT;
487
488         /* Fill in the generic fields of the device structure. */
489         ether_setup(dev);
490         return 0;
491 }
492
493 /* Read a word from the EEPROM using the regular EEPROM access register.
494    Assume that we are in register window zero.
495  */
496 static ushort read_eeprom(int ioaddr, int index)
497 {
498         outw(EEPROM_READ + index, ioaddr + 10);
499         /* Pause for at least 162 us. for the read to take place. */
500         udelay (500);
501         return inw(ioaddr + 12);
502 }
503
504 /* Read a word from the EEPROM when in the ISA ID probe state. */
505 static ushort id_read_eeprom(int index)
506 {
507         int bit, word = 0;
508
509         /* Issue read command, and pause for at least 162 us. for it to complete.
510            Assume extra-fast 16Mhz bus. */
511         outb(EEPROM_READ + index, id_port);
512
513         /* Pause for at least 162 us. for the read to take place. */
514         udelay (500);
515         
516         for (bit = 15; bit >= 0; bit--)
517                 word = (word << 1) + (inb(id_port) & 0x01);
518
519         if (el3_debug > 3)
520                 printk("  3c509 EEPROM word %d %#4.4x.\n", index, word);
521
522         return word;
523 }
524
525
526 static int
527 el3_open(struct net_device *dev)
528 {
529         int ioaddr = dev->base_addr;
530         int i;
531
532         outw(TxReset, ioaddr + EL3_CMD);
533         outw(RxReset, ioaddr + EL3_CMD);
534         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
535
536         i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
537         if (i) return i;
538
539         EL3WINDOW(0);
540         if (el3_debug > 3)
541                 printk("%s: Opening, IRQ %d      status@%x %4.4x.\n", dev->name,
542                            dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
543
544         /* Activate board: this is probably unnecessary. */
545         outw(0x0001, ioaddr + 4);
546
547         /* Set the IRQ line. */
548         outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
549
550         /* Set the station address in window 2 each time opened. */
551         EL3WINDOW(2);
552
553         for (i = 0; i < 6; i++)
554                 outb(dev->dev_addr[i], ioaddr + i);
555
556         if (dev->if_port == 3)
557                 /* Start the thinnet transceiver. We should really wait 50ms...*/
558                 outw(StartCoax, ioaddr + EL3_CMD);
559         else if (dev->if_port == 0) {
560                 /* 10baseT interface, enabled link beat and jabber check. */
561                 EL3WINDOW(4);
562                 outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
563         }
564
565         /* Switch to the stats window, and clear all stats by reading. */
566         outw(StatsDisable, ioaddr + EL3_CMD);
567         EL3WINDOW(6);
568         for (i = 0; i < 9; i++)
569                 inb(ioaddr + i);
570         inw(ioaddr + 10);
571         inw(ioaddr + 12);
572
573         /* Switch to register set 1 for normal use. */
574         EL3WINDOW(1);
575
576         /* Accept b-case and phys addr only. */
577         outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
578         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
579
580         netif_start_queue(dev);
581
582         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
583         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
584         /* Allow status bits to be seen. */
585         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
586         /* Ack all pending events, and set active indicator mask. */
587         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
588                  ioaddr + EL3_CMD);
589         outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
590                  ioaddr + EL3_CMD);
591
592         if (el3_debug > 3)
593                 printk("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
594                            dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
595
596         return 0;
597 }
598
599 static void
600 el3_tx_timeout (struct net_device *dev)
601 {
602         struct el3_private *lp = (struct el3_private *)dev->priv;
603         int ioaddr = dev->base_addr;
604
605         /* Transmitter timeout, serious problems. */
606         printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
607                    "Tx FIFO room %d.\n",
608                    dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
609                    inw(ioaddr + TX_FREE));
610         lp->stats.tx_errors++;
611         dev->trans_start = jiffies;
612         /* Issue TX_RESET and TX_START commands. */
613         outw(TxReset, ioaddr + EL3_CMD);
614         outw(TxEnable, ioaddr + EL3_CMD);
615         netif_wake_queue(dev);
616 }
617
618
619 static int
620 el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
621 {
622         struct el3_private *lp = (struct el3_private *)dev->priv;
623         int ioaddr = dev->base_addr;
624         unsigned long flags;
625
626         netif_stop_queue (dev);
627
628         lp->stats.tx_bytes += skb->len;
629         
630         if (el3_debug > 4) {
631                 printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
632                            dev->name, skb->len, inw(ioaddr + EL3_STATUS));
633         }
634 #if 0
635 #ifndef final_version
636         {       /* Error-checking code, delete someday. */
637                 ushort status = inw(ioaddr + EL3_STATUS);
638                 if (status & 0x0001             /* IRQ line active, missed one. */
639                         && inw(ioaddr + EL3_STATUS) & 1) {                      /* Make sure. */
640                         printk("%s: Missed interrupt, status then %04x now %04x"
641                                    "  Tx %2.2x Rx %4.4x.\n", dev->name, status,
642                                    inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
643                                    inw(ioaddr + RX_STATUS));
644                         /* Fake interrupt trigger by masking, acknowledge interrupts. */
645                         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
646                         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
647                                  ioaddr + EL3_CMD);
648                         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
649                 }
650         }
651 #endif
652 #endif
653         /*
654          *      We lock the driver against other processors. Note
655          *      we don't need to lock versus the IRQ as we suspended
656          *      that. This means that we lose the ability to take
657          *      an RX during a TX upload. That sucks a bit with SMP
658          *      on an original 3c509 (2K buffer)
659          *
660          *      Using disable_irq stops us crapping on other
661          *      time sensitive devices.
662          */
663
664         spin_lock_irqsave(&lp->lock, flags);
665             
666         /* Put out the doubleword header... */
667         outw(skb->len, ioaddr + TX_FIFO);
668         outw(0x00, ioaddr + TX_FIFO);
669         /* ... and the packet rounded to a doubleword. */
670 #ifdef  __powerpc__
671         outsl_unswapped(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
672 #else
673         outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
674 #endif
675
676         dev->trans_start = jiffies;
677         if (inw(ioaddr + TX_FREE) > 1536)
678                 netif_start_queue(dev);
679         else
680                 /* Interrupt us when the FIFO has room for max-sized packet. */
681                 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
682
683         spin_unlock_irqrestore(&lp->lock, flags);
684
685         dev_kfree_skb (skb);
686
687         /* Clear the Tx status stack. */
688         {
689                 short tx_status;
690                 int i = 4;
691
692                 while (--i > 0  &&      (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
693                         if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
694                         if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
695                         if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
696                         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
697                 }
698         }
699         return 0;
700 }
701
702 /* The EL3 interrupt handler. */
703 static void
704 el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
705 {
706         struct net_device *dev = (struct net_device *)dev_id;
707         struct el3_private *lp;
708         int ioaddr, status;
709         int i = max_interrupt_work;
710
711         if (dev == NULL) {
712                 printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
713                 return;
714         }
715
716         lp = (struct el3_private *)dev->priv;
717         spin_lock(&lp->lock);
718
719         ioaddr = dev->base_addr;
720
721         if (el3_debug > 4) {
722                 status = inw(ioaddr + EL3_STATUS);
723                 printk("%s: interrupt, status %4.4x.\n", dev->name, status);
724         }
725
726         while ((status = inw(ioaddr + EL3_STATUS)) &
727                    (IntLatch | RxComplete | StatsFull)) {
728
729                 if (status & RxComplete)
730                         el3_rx(dev);
731
732                 if (status & TxAvailable) {
733                         if (el3_debug > 5)
734                                 printk("        TX room bit was handled.\n");
735                         /* There's room in the FIFO for a full-sized packet. */
736                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
737                         netif_wake_queue (dev);
738                 }
739                 if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
740                         /* Handle all uncommon interrupts. */
741                         if (status & StatsFull)                         /* Empty statistics. */
742                                 update_stats(dev);
743                         if (status & RxEarly) {                         /* Rx early is unused. */
744                                 el3_rx(dev);
745                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
746                         }
747                         if (status & TxComplete) {                      /* Really Tx error. */
748                                 struct el3_private *lp = (struct el3_private *)dev->priv;
749                                 short tx_status;
750                                 int i = 4;
751
752                                 while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
753                                         if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
754                                         if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
755                                         if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
756                                         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
757                                 }
758                         }
759                         if (status & AdapterFailure) {
760                                 /* Adapter failure requires Rx reset and reinit. */
761                                 outw(RxReset, ioaddr + EL3_CMD);
762                                 /* Set the Rx filter to the current state. */
763                                 outw(SetRxFilter | RxStation | RxBroadcast
764                                          | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
765                                          | (dev->flags & IFF_PROMISC ? RxProm : 0),
766                                          ioaddr + EL3_CMD);
767                                 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
768                                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
769                         }
770                 }
771
772                 if (--i < 0) {
773                         printk("%s: Infinite loop in interrupt, status %4.4x.\n",
774                                    dev->name, status);
775                         /* Clear all interrupts. */
776                         outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
777                         break;
778                 }
779                 /* Acknowledge the IRQ. */
780                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
781         }
782
783         if (el3_debug > 4) {
784                 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
785                            inw(ioaddr + EL3_STATUS));
786         }
787         spin_unlock(&lp->lock);
788         return;
789 }
790
791
792 static struct net_device_stats *
793 el3_get_stats(struct net_device *dev)
794 {
795         struct el3_private *lp = (struct el3_private *)dev->priv;
796         unsigned long flags;
797
798         /*
799          *      This is fast enough not to bother with disable IRQ
800          *      stuff.
801          */
802          
803         spin_lock_irqsave(&lp->lock, flags);
804         update_stats(dev);
805         spin_unlock_irqrestore(&lp->lock, flags);
806         return &lp->stats;
807 }
808
809 /*  Update statistics.  We change to register window 6, so this should be run
810         single-threaded if the device is active. This is expected to be a rare
811         operation, and it's simpler for the rest of the driver to assume that
812         window 1 is always valid rather than use a special window-state variable.
813         */
814 static void update_stats(struct net_device *dev)
815 {
816         struct el3_private *lp = (struct el3_private *)dev->priv;
817         int ioaddr = dev->base_addr;
818
819         if (el3_debug > 5)
820                 printk("   Updating the statistics.\n");
821         /* Turn off statistics updates while reading. */
822         outw(StatsDisable, ioaddr + EL3_CMD);
823         /* Switch to the stats window, and read everything. */
824         EL3WINDOW(6);
825         lp->stats.tx_carrier_errors     += inb(ioaddr + 0);
826         lp->stats.tx_heartbeat_errors   += inb(ioaddr + 1);
827         /* Multiple collisions. */         inb(ioaddr + 2);
828         lp->stats.collisions            += inb(ioaddr + 3);
829         lp->stats.tx_window_errors      += inb(ioaddr + 4);
830         lp->stats.rx_fifo_errors        += inb(ioaddr + 5);
831         lp->stats.tx_packets            += inb(ioaddr + 6);
832         /* Rx packets   */                 inb(ioaddr + 7);
833         /* Tx deferrals */                 inb(ioaddr + 8);
834         inw(ioaddr + 10);       /* Total Rx and Tx octets. */
835         inw(ioaddr + 12);
836
837         /* Back to window 1, and turn statistics back on. */
838         EL3WINDOW(1);
839         outw(StatsEnable, ioaddr + EL3_CMD);
840         return;
841 }
842
843 static int
844 el3_rx(struct net_device *dev)
845 {
846         struct el3_private *lp = (struct el3_private *)dev->priv;
847         int ioaddr = dev->base_addr;
848         short rx_status;
849
850         if (el3_debug > 5)
851                 printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
852                            inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
853         while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
854                 if (rx_status & 0x4000) { /* Error, update stats. */
855                         short error = rx_status & 0x3800;
856
857                         outw(RxDiscard, ioaddr + EL3_CMD);
858                         lp->stats.rx_errors++;
859                         switch (error) {
860                         case 0x0000:            lp->stats.rx_over_errors++; break;
861                         case 0x0800:            lp->stats.rx_length_errors++; break;
862                         case 0x1000:            lp->stats.rx_frame_errors++; break;
863                         case 0x1800:            lp->stats.rx_length_errors++; break;
864                         case 0x2000:            lp->stats.rx_frame_errors++; break;
865                         case 0x2800:            lp->stats.rx_crc_errors++; break;
866                         }
867                 } else {
868                         short pkt_len = rx_status & 0x7ff;
869                         struct sk_buff *skb;
870
871                         skb = dev_alloc_skb(pkt_len+5);
872                         lp->stats.rx_bytes += pkt_len;
873                         if (el3_debug > 4)
874                                 printk("Receiving packet size %d status %4.4x.\n",
875                                            pkt_len, rx_status);
876                         if (skb != NULL) {
877                                 skb->dev = dev;
878                                 skb_reserve(skb, 2);     /* Align IP on 16 byte */
879
880                                 /* 'skb->data' points to the start of sk_buff data area. */
881 #ifdef  __powerpc__
882                                 insl_unswapped(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
883                                                            (pkt_len + 3) >> 2);
884 #else
885                                 insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
886                                          (pkt_len + 3) >> 2);
887 #endif
888
889                                 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
890                                 skb->protocol = eth_type_trans(skb,dev);
891                                 netif_rx(skb);
892                                 lp->stats.rx_packets++;
893                                 continue;
894                         }
895                         outw(RxDiscard, ioaddr + EL3_CMD);
896                         lp->stats.rx_dropped++;
897                         if (el3_debug)
898                                 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
899                                            dev->name, pkt_len);
900                 }
901                 inw(ioaddr + EL3_STATUS);                               /* Delay. */
902                 while (inw(ioaddr + EL3_STATUS) & 0x1000)
903                         printk(KERN_DEBUG "     Waiting for 3c509 to discard packet, status %x.\n",
904                                    inw(ioaddr + EL3_STATUS) );
905         }
906
907         return 0;
908 }
909
910 /*
911  *     Set or clear the multicast filter for this adaptor.
912  */
913 static void
914 set_multicast_list(struct net_device *dev)
915 {
916         unsigned long flags;
917         struct el3_private *lp = (struct el3_private *)dev->priv;
918         int ioaddr = dev->base_addr;
919
920         if (el3_debug > 1) {
921                 static int old = 0;
922                 if (old != dev->mc_count) {
923                         old = dev->mc_count;
924                         printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
925                 }
926         }
927         spin_lock_irqsave(&lp->lock, flags);
928         if (dev->flags&IFF_PROMISC) {
929                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
930                          ioaddr + EL3_CMD);
931         }
932         else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
933                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
934         }
935         else
936                 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
937         spin_unlock_irqrestore(&lp->lock, flags);
938 }
939
940 static int
941 el3_close(struct net_device *dev)
942 {
943         int ioaddr = dev->base_addr;
944
945         if (el3_debug > 2)
946                 printk("%s: Shutting down ethercard.\n", dev->name);
947
948         netif_stop_queue(dev);
949
950         /* Turn off statistics ASAP.  We update lp->stats below. */
951         outw(StatsDisable, ioaddr + EL3_CMD);
952
953         /* Disable the receiver and transmitter. */
954         outw(RxDisable, ioaddr + EL3_CMD);
955         outw(TxDisable, ioaddr + EL3_CMD);
956
957         if (dev->if_port == 3)
958                 /* Turn off thinnet power.  Green! */
959                 outw(StopCoax, ioaddr + EL3_CMD);
960         else if (dev->if_port == 0) {
961                 /* Disable link beat and jabber, if_port may change ere next open(). */
962                 EL3WINDOW(4);
963                 outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
964         }
965
966         free_irq(dev->irq, dev);
967         /* Switching back to window 0 disables the IRQ. */
968         EL3WINDOW(0);
969         /* But we explicitly zero the IRQ line select anyway. */
970         outw(0x0f00, ioaddr + WN0_IRQ);
971
972         update_stats(dev);
973         return 0;
974 }
975
976 #ifdef MODULE
977 /* Parameters that may be passed into the module. */
978 static int debug = -1;
979 static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
980 static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1};
981
982 MODULE_PARM(debug,"i");
983 MODULE_PARM(irq,"1-8i");
984 MODULE_PARM(xcvr,"1-8i");
985 MODULE_PARM(max_interrupt_work, "i");
986 #ifdef __ISAPNP__
987 MODULE_PARM(nopnp, "i");
988 #endif
989
990 int
991 init_module(void)
992 {
993         int el3_cards = 0;
994
995         if (debug >= 0)
996                 el3_debug = debug;
997
998         el3_root_dev = NULL;
999         while (el3_probe(0) == 0) {
1000                 if (irq[el3_cards] > 1)
1001                         el3_root_dev->irq = irq[el3_cards];
1002                 if (xcvr[el3_cards] >= 0)
1003                         el3_root_dev->if_port = xcvr[el3_cards];
1004                 el3_cards++;
1005         }
1006
1007         return el3_cards ? 0 : -ENODEV;
1008 }
1009
1010 void
1011 cleanup_module(void)
1012 {
1013         struct net_device *next_dev;
1014
1015         /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1016         while (el3_root_dev) {
1017                 struct el3_private *lp = (struct el3_private *)el3_root_dev->priv;
1018 #ifdef CONFIG_MCA               
1019                 if(lp->mca_slot!=-1)
1020                         mca_mark_as_unused(lp->mca_slot);
1021 #endif                  
1022                 next_dev = lp->next_dev;
1023                 unregister_netdev(el3_root_dev);
1024                 release_region(el3_root_dev->base_addr, EL3_IO_EXTENT);
1025                 kfree(el3_root_dev);
1026                 el3_root_dev = next_dev;
1027         }
1028 }
1029 #endif /* MODULE */
1030
1031 /*
1032  * Local variables:
1033  *  compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c509.c"
1034  *  version-control: t
1035  *  kept-new-versions: 5
1036  *  tab-width: 4
1037  * End:
1038  */