VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / 3c509.c
1 /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
2 /*
3         Written 1993-2000 by Donald Becker.
4
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.
10
11         This driver is for the 3Com EtherLinkIII series.
12
13         The author may be reached as becker@scyld.com, or C/O
14         Scyld Computing Corporation
15         410 Severn Ave., Suite 210
16         Annapolis MD 21403
17
18         Known limitations:
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.
21
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.
26
27
28         FIXES:
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)
34                                 delays
35                 v1.10 4/21/97 Fixed module code so that multiple cards may be detected,
36                                 other cleanups.  -djb
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>
47                         - ethtool support
48                 v1.18b 1Mar2002 Zwane Mwaikambo <zwane@commfireservices.com>
49                         - Power Management support
50                 v1.18c 1Mar2002 David Ruggiero <jdr@farfalle.com>
51                         - Full duplex support
52                 v1.19  16Oct2002 Zwane Mwaikambo <zwane@linuxpower.ca>
53                         - Additional ethtool features
54                 v1.19a 28Oct2002 Davud Ruggiero <jdr@farfalle.com>
55                         - Increase *read_eeprom udelay to workaround oops with 2 cards.
56                 v1.19b 08Nov2002 Marc Zyngier <maz@wild-wind.fr.eu.org>
57                     - Introduce driver model for EISA cards.
58 */
59
60 #define DRV_NAME        "3c509"
61 #define DRV_VERSION     "1.19b"
62 #define DRV_RELDATE     "08Nov2002"
63
64 /* A few values that may be tweaked. */
65
66 /* Time in jiffies before concluding the transmitter is hung. */
67 #define TX_TIMEOUT  (400*HZ/1000)
68 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
69 static int max_interrupt_work = 10;
70
71 #include <linux/config.h>
72 #include <linux/module.h>
73 #ifdef CONFIG_MCA
74 #include <linux/mca.h>
75 #endif
76 #include <linux/isapnp.h>
77 #include <linux/string.h>
78 #include <linux/interrupt.h>
79 #include <linux/errno.h>
80 #include <linux/in.h>
81 #include <linux/slab.h>
82 #include <linux/ioport.h>
83 #include <linux/init.h>
84 #include <linux/netdevice.h>
85 #include <linux/etherdevice.h>
86 #include <linux/pm.h>
87 #include <linux/skbuff.h>
88 #include <linux/delay.h>        /* for udelay() */
89 #include <linux/spinlock.h>
90 #include <linux/ethtool.h>
91 #include <linux/device.h>
92 #include <linux/eisa.h>
93
94 #include <asm/uaccess.h>
95 #include <asm/bitops.h>
96 #include <asm/io.h>
97 #include <asm/irq.h>
98
99 static char versionA[] __initdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
100 static char versionB[] __initdata = "http://www.scyld.com/network/3c509.html\n";
101
102 #ifdef EL3_DEBUG
103 static int el3_debug = EL3_DEBUG;
104 #else
105 static int el3_debug = 2;
106 #endif
107
108 /* Used to do a global count of all the cards in the system.  Must be
109  * a global variable so that the mca/eisa probe routines can increment
110  * it */
111 static int el3_cards = 0;
112
113 /* To minimize the size of the driver source I only define operating
114    constants if they are used several times.  You'll need the manual
115    anyway if you want to understand driver details. */
116 /* Offsets from base I/O address. */
117 #define EL3_DATA 0x00
118 #define EL3_CMD 0x0e
119 #define EL3_STATUS 0x0e
120 #define  EEPROM_READ 0x80
121
122 #define EL3_IO_EXTENT   16
123
124 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
125
126
127 /* The top five bits written to EL3_CMD are a command, the lower
128    11 bits are the parameter, if applicable. */
129 enum c509cmd {
130         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
131         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
132         TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
133         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
134         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
135         SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
136         StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
137         PowerDown = 28<<11, PowerAuto = 29<<11};
138
139 enum c509status {
140         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
141         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
142         IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
143
144 /* The SetRxFilter command accepts the following classes: */
145 enum RxFilter {
146         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
147
148 /* Register window 1 offsets, the window used in normal operation. */
149 #define TX_FIFO         0x00
150 #define RX_FIFO         0x00
151 #define RX_STATUS       0x08
152 #define TX_STATUS       0x0B
153 #define TX_FREE         0x0C            /* Remaining free bytes in Tx buffer. */
154
155 #define WN0_CONF_CTRL   0x04            /* Window 0: Configuration control register */
156 #define WN0_ADDR_CONF   0x06            /* Window 0: Address configuration register */
157 #define WN0_IRQ         0x08            /* Window 0: Set IRQ line in bits 12-15. */
158 #define WN4_MEDIA       0x0A            /* Window 4: Various transcvr/media bits. */
159 #define MEDIA_TP        0x00C0          /* Enable link beat and jabber for 10baseT. */
160 #define WN4_NETDIAG     0x06            /* Window 4: Net diagnostic */
161 #define FD_ENABLE       0x8000          /* Enable full-duplex ("external loopback") */  
162
163 /*
164  * Must be a power of two (we use a binary and in the
165  * circular queue)
166  */
167 #define SKB_QUEUE_SIZE  64
168
169 struct el3_private {
170         struct net_device_stats stats;
171         struct net_device *next_dev;
172         spinlock_t lock;
173         /* skb send-queue */
174         int head, size;
175         struct sk_buff *queue[SKB_QUEUE_SIZE];
176 #ifdef CONFIG_PM
177         struct pm_dev *pmdev;
178 #endif
179         enum {
180                 EL3_MCA,
181                 EL3_PNP,
182                 EL3_EISA,
183         } type;                                         /* type of device */
184         struct device *dev;
185 };
186 static int id_port __initdata = 0x110;  /* Start with 0x110 to avoid new sound cards.*/
187 static struct net_device *el3_root_dev;
188
189 static ushort id_read_eeprom(int index);
190 static ushort read_eeprom(int ioaddr, int index);
191 static int el3_open(struct net_device *dev);
192 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
193 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
194 static void update_stats(struct net_device *dev);
195 static struct net_device_stats *el3_get_stats(struct net_device *dev);
196 static int el3_rx(struct net_device *dev);
197 static int el3_close(struct net_device *dev);
198 static void set_multicast_list(struct net_device *dev);
199 static void el3_tx_timeout (struct net_device *dev);
200 static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
201 static void el3_down(struct net_device *dev);
202 static void el3_up(struct net_device *dev);
203 #ifdef CONFIG_PM
204 static int el3_suspend(struct pm_dev *pdev);
205 static int el3_resume(struct pm_dev *pdev);
206 static int el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data);
207 #endif
208 /* generic device remove for all device types */
209 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
210 static int el3_device_remove (struct device *device);
211 #endif
212
213 #ifdef CONFIG_EISA
214 struct eisa_device_id el3_eisa_ids[] = {
215                 { "TCM5092" },
216                 { "TCM5093" },
217                 { "" }
218 };
219
220 static int el3_eisa_probe (struct device *device);
221
222 struct eisa_driver el3_eisa_driver = {
223                 .id_table = el3_eisa_ids,
224                 .driver   = {
225                                 .name    = "3c509",
226                                 .probe   = el3_eisa_probe,
227                                 .remove  = __devexit_p (el3_device_remove)
228                 }
229 };
230 #endif
231
232 #ifdef CONFIG_MCA
233 static int el3_mca_probe(struct device *dev);
234
235 static short el3_mca_adapter_ids[] __initdata = {
236                 0x627c,
237                 0x627d,
238                 0x62db,
239                 0x62f6,
240                 0x62f7,
241                 0x0000
242 };
243
244 static char *el3_mca_adapter_names[] __initdata = {
245                 "3Com 3c529 EtherLink III (10base2)",
246                 "3Com 3c529 EtherLink III (10baseT)",
247                 "3Com 3c529 EtherLink III (test mode)",
248                 "3Com 3c529 EtherLink III (TP or coax)",
249                 "3Com 3c529 EtherLink III (TP)",
250                 NULL
251 };
252
253 static struct mca_driver el3_mca_driver = {
254                 .id_table = el3_mca_adapter_ids,
255                 .driver = {
256                                 .name = "3c529",
257                                 .bus = &mca_bus_type,
258                                 .probe = el3_mca_probe,
259                                 .remove = __devexit_p(el3_device_remove),
260                 },
261 };
262 #endif /* CONFIG_MCA */
263
264 #if defined(__ISAPNP__)
265 static struct isapnp_device_id el3_isapnp_adapters[] __initdata = {
266         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
267                 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090),
268                 (long) "3Com Etherlink III (TP)" },
269         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
270                 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091),
271                 (long) "3Com Etherlink III" },
272         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
273                 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094),
274                 (long) "3Com Etherlink III (combo)" },
275         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
276                 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095),
277                 (long) "3Com Etherlink III (TPO)" },
278         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
279                 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098),
280                 (long) "3Com Etherlink III (TPC)" },
281         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
282                 ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f7),
283                 (long) "3Com Etherlink III compatible" },
284         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
285                 ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8),
286                 (long) "3Com Etherlink III compatible" },
287         { }     /* terminate list */
288 };
289
290 static u16 el3_isapnp_phys_addr[8][3];
291 static int nopnp;
292 #endif /* __ISAPNP__ */
293
294 /* With the driver model introduction for EISA devices, both init
295  * and cleanup have been split :
296  * - EISA devices probe/remove starts in el3_eisa_probe/el3_device_remove
297  * - MCA/ISA still use el3_probe
298  *
299  * Both call el3_common_init/el3_common_remove. */
300
301 static int __init el3_common_init(struct net_device *dev)
302 {
303         struct el3_private *lp = netdev_priv(dev);
304         short i;
305         int err;
306
307         spin_lock_init(&lp->lock);
308
309         if (dev->mem_start & 0x05) { /* xcvr codes 1/3/4/12 */
310                 dev->if_port = (dev->mem_start & 0x0f);
311         } else { /* xcvr codes 0/8 */
312                 /* use eeprom value, but save user's full-duplex selection */
313                 dev->if_port |= (dev->mem_start & 0x08);
314         }
315
316         /* The EL3-specific entries in the device structure. */
317         dev->open = &el3_open;
318         dev->hard_start_xmit = &el3_start_xmit;
319         dev->stop = &el3_close;
320         dev->get_stats = &el3_get_stats;
321         dev->set_multicast_list = &set_multicast_list;
322         dev->tx_timeout = el3_tx_timeout;
323         dev->watchdog_timeo = TX_TIMEOUT;
324         dev->do_ioctl = netdev_ioctl;
325
326         err = register_netdev(dev);
327         if (err) {
328                 printk(KERN_ERR "Failed to register 3c5x9 at %#3.3lx, IRQ %d.\n",
329                         dev->base_addr, dev->irq);
330                 release_region(dev->base_addr, EL3_IO_EXTENT);
331                 return err;
332         }
333
334         {
335                 const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
336                 printk("%s: 3c5x9 found at %#3.3lx, %s port, address ",
337                         dev->name, dev->base_addr, 
338                         if_names[(dev->if_port & 0x03)]);
339         }
340
341         /* Read in the station address. */
342         for (i = 0; i < 6; i++)
343                 printk(" %2.2x", dev->dev_addr[i]);
344         printk(", IRQ %d.\n", dev->irq);
345
346         if (el3_debug > 0)
347                 printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
348         return 0;
349
350 }
351
352 static void el3_common_remove (struct net_device *dev)
353 {
354         struct el3_private *lp = netdev_priv(dev);
355
356         (void) lp;                              /* Keep gcc quiet... */
357 #ifdef CONFIG_PM
358         if (lp->pmdev)
359                 pm_unregister(lp->pmdev);
360 #endif
361 #if defined(__ISAPNP__)
362         if (lp->type == EL3_PNP)
363                 pnp_device_detach(to_pnp_dev(lp->dev));
364 #endif
365
366         unregister_netdev (dev);
367         release_region(dev->base_addr, EL3_IO_EXTENT);
368         free_netdev (dev);
369 }
370
371 static int __init el3_probe(int card_idx)
372 {
373         struct net_device *dev;
374         struct el3_private *lp;
375         short lrs_state = 0xff, i;
376         int ioaddr, irq, if_port;
377         u16 phys_addr[3];
378         static int current_tag;
379         int err = -ENODEV;
380 #if defined(__ISAPNP__)
381         static int pnp_cards;
382         struct pnp_dev *idev = NULL;
383
384         if (nopnp == 1)
385                 goto no_pnp;
386
387         for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
388                 int j;
389                 while ((idev = pnp_find_dev(NULL,
390                                             el3_isapnp_adapters[i].vendor,
391                                             el3_isapnp_adapters[i].function,
392                                             idev))) {
393                         if (pnp_device_attach(idev) < 0)
394                                 continue;
395                         if (pnp_activate_dev(idev) < 0) {
396 __again:
397                                 pnp_device_detach(idev);
398                                 continue;
399                         }
400                         if (!pnp_port_valid(idev, 0) || !pnp_irq_valid(idev, 0))
401                                 goto __again;
402                         ioaddr = pnp_port_start(idev, 0);
403                         if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP")) {
404                                 pnp_device_detach(idev);
405                                 return -EBUSY;
406                         }
407                         irq = pnp_irq(idev, 0);
408                         if (el3_debug > 3)
409                                 printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
410                                         (char*) el3_isapnp_adapters[i].driver_data, ioaddr, irq);
411                         EL3WINDOW(0);
412                         for (j = 0; j < 3; j++)
413                                 el3_isapnp_phys_addr[pnp_cards][j] =
414                                         phys_addr[j] =
415                                                 htons(read_eeprom(ioaddr, j));
416                         if_port = read_eeprom(ioaddr, 8) >> 14;
417                         dev = alloc_etherdev(sizeof (struct el3_private));
418                         if (!dev) {
419                                         release_region(ioaddr, EL3_IO_EXTENT);
420                                         pnp_device_detach(idev);
421                                         return -ENOMEM;
422                         }
423
424                         SET_MODULE_OWNER(dev);
425                         SET_NETDEV_DEV(dev, &idev->dev);
426                         pnp_cards++;
427
428                         netdev_boot_setup_check(dev);
429                         goto found;
430                 }
431         }
432 no_pnp:
433 #endif /* __ISAPNP__ */
434
435         /* Select an open I/O location at 0x1*0 to do contention select. */
436         for ( ; id_port < 0x200; id_port += 0x10) {
437                 if (!request_region(id_port, 1, "3c509"))
438                         continue;
439                 outb(0x00, id_port);
440                 outb(0xff, id_port);
441                 if (inb(id_port) & 0x01){
442                         release_region(id_port, 1);
443                         break;
444                 } else
445                         release_region(id_port, 1);
446         }
447         if (id_port >= 0x200) {
448                 /* Rare -- do we really need a warning? */
449                 printk(" WARNING: No I/O port available for 3c509 activation.\n");
450                 return -ENODEV;
451         }
452
453         /* Next check for all ISA bus boards by sending the ID sequence to the
454            ID_PORT.  We find cards past the first by setting the 'current_tag'
455            on cards as they are found.  Cards with their tag set will not
456            respond to subsequent ID sequences. */
457
458         outb(0x00, id_port);
459         outb(0x00, id_port);
460         for(i = 0; i < 255; i++) {
461                 outb(lrs_state, id_port);
462                 lrs_state <<= 1;
463                 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
464         }
465
466         /* For the first probe, clear all board's tag registers. */
467         if (current_tag == 0)
468                 outb(0xd0, id_port);
469         else                            /* Otherwise kill off already-found boards. */
470                 outb(0xd8, id_port);
471
472         if (id_read_eeprom(7) != 0x6d50) {
473                 return -ENODEV;
474         }
475
476         /* Read in EEPROM data, which does contention-select.
477            Only the lowest address board will stay "on-line".
478            3Com got the byte order backwards. */
479         for (i = 0; i < 3; i++) {
480                 phys_addr[i] = htons(id_read_eeprom(i));
481         }
482
483 #if defined(__ISAPNP__)
484         if (nopnp == 0) {
485                 /* The ISA PnP 3c509 cards respond to the ID sequence.
486                    This check is needed in order not to register them twice. */
487                 for (i = 0; i < pnp_cards; i++) {
488                         if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
489                             phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
490                             phys_addr[2] == el3_isapnp_phys_addr[i][2])
491                         {
492                                 if (el3_debug > 3)
493                                         printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
494                                                 phys_addr[0] & 0xff, phys_addr[0] >> 8,
495                                                 phys_addr[1] & 0xff, phys_addr[1] >> 8,
496                                                 phys_addr[2] & 0xff, phys_addr[2] >> 8);
497                                 /* Set the adaptor tag so that the next card can be found. */
498                                 outb(0xd0 + ++current_tag, id_port);
499                                 goto no_pnp;
500                         }
501                 }
502         }
503 #endif /* __ISAPNP__ */
504
505         {
506                 unsigned int iobase = id_read_eeprom(8);
507                 if_port = iobase >> 14;
508                 ioaddr = 0x200 + ((iobase & 0x1f) << 4);
509         }
510         irq = id_read_eeprom(9) >> 12;
511
512         dev = alloc_etherdev(sizeof (struct el3_private));
513         if (!dev)
514                 return -ENOMEM;
515
516         SET_MODULE_OWNER(dev);
517
518         netdev_boot_setup_check(dev);
519         
520         /* Set passed-in IRQ or I/O Addr. */
521         if (dev->irq > 1  &&  dev->irq < 16)
522                         irq = dev->irq;
523
524         if (dev->base_addr) {
525                 if (dev->mem_end == 0x3c509     /* Magic key */
526                     && dev->base_addr >= 0x200  &&  dev->base_addr <= 0x3e0)
527                         ioaddr = dev->base_addr & 0x3f0;
528                 else if (dev->base_addr != ioaddr)
529                         goto out;
530         }
531
532         if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509")) {
533                 err = -EBUSY;
534                 goto out;
535         }
536
537         /* Set the adaptor tag so that the next card can be found. */
538         outb(0xd0 + ++current_tag, id_port);
539
540         /* Activate the adaptor at the EEPROM location. */
541         outb((ioaddr >> 4) | 0xe0, id_port);
542
543         EL3WINDOW(0);
544         if (inw(ioaddr) != 0x6d50)
545                 goto out1;
546
547         /* Free the interrupt so that some other card can use it. */
548         outw(0x0f00, ioaddr + WN0_IRQ);
549
550 #if defined(__ISAPNP__)
551  found:                                                 /* PNP jumps here... */
552 #endif /* __ISAPNP__ */
553
554         memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
555         dev->base_addr = ioaddr;
556         dev->irq = irq;
557         dev->if_port = if_port;
558         lp = netdev_priv(dev);
559 #if defined(__ISAPNP__)
560         lp->dev = &idev->dev;
561 #endif
562         err = el3_common_init(dev);
563
564         if (err)
565                 goto out1;
566
567 #ifdef CONFIG_PM
568         /* register power management */
569         lp->pmdev = pm_register(PM_ISA_DEV, card_idx, el3_pm_callback);
570         if (lp->pmdev) {
571                 struct pm_dev *p;
572                 p = lp->pmdev;
573                 p->data = (struct net_device *)dev;
574         }
575 #endif
576
577         el3_cards++;
578         lp->next_dev = el3_root_dev;
579         el3_root_dev = dev;
580         return 0;
581
582 out1:
583 #if defined(__ISAPNP__)
584         if (idev)
585                 pnp_device_detach(idev);
586 #endif
587 out:
588         free_netdev(dev);
589         return err;
590 }
591
592 #ifdef CONFIG_MCA
593 static int __init el3_mca_probe(struct device *device)
594 {
595         /* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch,
596          * heavily modified by Chris Beauregard
597          * (cpbeaure@csclub.uwaterloo.ca) to support standard MCA
598          * probing.
599          *
600          * redone for multi-card detection by ZP Gu (zpg@castle.net)
601          * now works as a module */
602
603         struct el3_private *lp;
604         short i;
605         int ioaddr, irq, if_port;
606         u16 phys_addr[3];
607         struct net_device *dev = NULL;
608         u_char pos4, pos5;
609         struct mca_device *mdev = to_mca_device(device);
610         int slot = mdev->slot;
611         int err;
612
613         pos4 = mca_device_read_stored_pos(mdev, 4);
614         pos5 = mca_device_read_stored_pos(mdev, 5);
615
616         ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
617         irq = pos5 & 0x0f;
618
619
620         printk("3c529: found %s at slot %d\n",
621                    el3_mca_adapter_names[mdev->index], slot + 1);
622
623         /* claim the slot */
624         strncpy(mdev->name, el3_mca_adapter_names[mdev->index],
625                         sizeof(mdev->name));
626         mca_device_set_claim(mdev, 1);
627
628         if_port = pos4 & 0x03;
629
630         irq = mca_device_transform_irq(mdev, irq);
631         ioaddr = mca_device_transform_ioport(mdev, ioaddr); 
632         if (el3_debug > 2) {
633                         printk("3c529: irq %d  ioaddr 0x%x  ifport %d\n", irq, ioaddr, if_port);
634         }
635         EL3WINDOW(0);
636         for (i = 0; i < 3; i++) {
637                         phys_addr[i] = htons(read_eeprom(ioaddr, i));
638         }
639
640         dev = alloc_etherdev(sizeof (struct el3_private));
641         if (dev == NULL) {
642                         release_region(ioaddr, EL3_IO_EXTENT);
643                         return -ENOMEM;
644         }
645
646         SET_MODULE_OWNER(dev);
647         netdev_boot_setup_check(dev);
648
649         memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
650         dev->base_addr = ioaddr;
651         dev->irq = irq;
652         dev->if_port = if_port;
653         lp = netdev_priv(dev);
654         lp->dev = device;
655         lp->type = EL3_MCA;
656         device->driver_data = dev;
657         err = el3_common_init(dev);
658
659         if (err) {
660                 device->driver_data = NULL;
661                 free_netdev(dev);
662                 return -ENOMEM;
663         }
664
665         el3_cards++;
666         return 0;
667 }
668                 
669 #endif /* CONFIG_MCA */
670
671 #ifdef CONFIG_EISA
672 static int __init el3_eisa_probe (struct device *device)
673 {
674         struct el3_private *lp;
675         short i;
676         int ioaddr, irq, if_port;
677         u16 phys_addr[3];
678         struct net_device *dev = NULL;
679         struct eisa_device *edev;
680         int err;
681
682         /* Yeepee, The driver framework is calling us ! */
683         edev = to_eisa_device (device);
684         ioaddr = edev->base_addr;
685         
686         if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
687                 return -EBUSY;
688
689         /* Change the register set to the configuration window 0. */
690         outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
691
692         irq = inw(ioaddr + WN0_IRQ) >> 12;
693         if_port = inw(ioaddr + 6)>>14;
694         for (i = 0; i < 3; i++)
695                 phys_addr[i] = htons(read_eeprom(ioaddr, i));
696
697         /* Restore the "Product ID" to the EEPROM read register. */
698         read_eeprom(ioaddr, 3);
699
700         dev = alloc_etherdev(sizeof (struct el3_private));
701         if (dev == NULL) {
702                 release_region(ioaddr, EL3_IO_EXTENT);
703                 return -ENOMEM;
704         }
705
706         SET_MODULE_OWNER(dev);
707
708         netdev_boot_setup_check(dev);
709
710         memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
711         dev->base_addr = ioaddr;
712         dev->irq = irq;
713         dev->if_port = if_port;
714         lp = netdev_priv(dev);
715         lp->dev = device;
716         lp->type = EL3_EISA;
717         eisa_set_drvdata (edev, dev);
718         err = el3_common_init(dev);
719
720         if (err) {
721                 eisa_set_drvdata (edev, NULL);
722                 free_netdev(dev);
723                 return err;
724         }
725
726         el3_cards++;
727         return 0;
728 }
729 #endif
730
731 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
732 /* This remove works for all device types.
733  *
734  * The net dev must be stored in the driver_data field */
735 static int __devexit el3_device_remove (struct device *device)
736 {
737         struct net_device *dev;
738
739         dev  = device->driver_data;
740
741         el3_common_remove (dev);
742         return 0;
743 }
744 #endif
745
746 /* Read a word from the EEPROM using the regular EEPROM access register.
747    Assume that we are in register window zero.
748  */
749 static ushort read_eeprom(int ioaddr, int index)
750 {
751         outw(EEPROM_READ + index, ioaddr + 10);
752         /* Pause for at least 162 us. for the read to take place. 
753            Some chips seem to require much longer */
754         mdelay(2);
755         return inw(ioaddr + 12);
756 }
757
758 /* Read a word from the EEPROM when in the ISA ID probe state. */
759 static ushort __init id_read_eeprom(int index)
760 {
761         int bit, word = 0;
762
763         /* Issue read command, and pause for at least 162 us. for it to complete.
764            Assume extra-fast 16Mhz bus. */
765         outb(EEPROM_READ + index, id_port);
766
767         /* Pause for at least 162 us. for the read to take place. */
768         /* Some chips seem to require much longer */
769         mdelay(4);
770         
771         for (bit = 15; bit >= 0; bit--)
772                 word = (word << 1) + (inb(id_port) & 0x01);
773
774         if (el3_debug > 3)
775                 printk("  3c509 EEPROM word %d %#4.4x.\n", index, word);
776
777         return word;
778 }
779
780
781 static int
782 el3_open(struct net_device *dev)
783 {
784         int ioaddr = dev->base_addr;
785         int i;
786
787         outw(TxReset, ioaddr + EL3_CMD);
788         outw(RxReset, ioaddr + EL3_CMD);
789         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
790
791         i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
792         if (i)
793                 return i;
794
795         EL3WINDOW(0);
796         if (el3_debug > 3)
797                 printk("%s: Opening, IRQ %d      status@%x %4.4x.\n", dev->name,
798                            dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
799
800         el3_up(dev);
801
802         if (el3_debug > 3)
803                 printk("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
804                            dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
805
806         return 0;
807 }
808
809 static void
810 el3_tx_timeout (struct net_device *dev)
811 {
812         struct el3_private *lp = netdev_priv(dev);
813         int ioaddr = dev->base_addr;
814
815         /* Transmitter timeout, serious problems. */
816         printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
817                    "Tx FIFO room %d.\n",
818                    dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
819                    inw(ioaddr + TX_FREE));
820         lp->stats.tx_errors++;
821         dev->trans_start = jiffies;
822         /* Issue TX_RESET and TX_START commands. */
823         outw(TxReset, ioaddr + EL3_CMD);
824         outw(TxEnable, ioaddr + EL3_CMD);
825         netif_wake_queue(dev);
826 }
827
828
829 static int
830 el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
831 {
832         struct el3_private *lp = netdev_priv(dev);
833         int ioaddr = dev->base_addr;
834         unsigned long flags;
835
836         netif_stop_queue (dev);
837
838         lp->stats.tx_bytes += skb->len;
839         
840         if (el3_debug > 4) {
841                 printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
842                            dev->name, skb->len, inw(ioaddr + EL3_STATUS));
843         }
844 #if 0
845 #ifndef final_version
846         {       /* Error-checking code, delete someday. */
847                 ushort status = inw(ioaddr + EL3_STATUS);
848                 if (status & 0x0001             /* IRQ line active, missed one. */
849                         && inw(ioaddr + EL3_STATUS) & 1) {                      /* Make sure. */
850                         printk("%s: Missed interrupt, status then %04x now %04x"
851                                    "  Tx %2.2x Rx %4.4x.\n", dev->name, status,
852                                    inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
853                                    inw(ioaddr + RX_STATUS));
854                         /* Fake interrupt trigger by masking, acknowledge interrupts. */
855                         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
856                         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
857                                  ioaddr + EL3_CMD);
858                         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
859                 }
860         }
861 #endif
862 #endif
863         /*
864          *      We lock the driver against other processors. Note
865          *      we don't need to lock versus the IRQ as we suspended
866          *      that. This means that we lose the ability to take
867          *      an RX during a TX upload. That sucks a bit with SMP
868          *      on an original 3c509 (2K buffer)
869          *
870          *      Using disable_irq stops us crapping on other
871          *      time sensitive devices.
872          */
873
874         spin_lock_irqsave(&lp->lock, flags);
875
876         /* Put out the doubleword header... */
877         outw(skb->len, ioaddr + TX_FIFO);
878         outw(0x00, ioaddr + TX_FIFO);
879         /* ... and the packet rounded to a doubleword. */
880 #ifdef  __powerpc__
881         outsl_ns(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
882 #else
883         outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
884 #endif
885
886         dev->trans_start = jiffies;
887         if (inw(ioaddr + TX_FREE) > 1536)
888                 netif_start_queue(dev);
889         else
890                 /* Interrupt us when the FIFO has room for max-sized packet. */
891                 outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
892
893         spin_unlock_irqrestore(&lp->lock, flags);
894
895         dev_kfree_skb (skb);
896
897         /* Clear the Tx status stack. */
898         {
899                 short tx_status;
900                 int i = 4;
901
902                 while (--i > 0  &&      (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
903                         if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
904                         if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
905                         if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
906                         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
907                 }
908         }
909         return 0;
910 }
911
912 /* The EL3 interrupt handler. */
913 static irqreturn_t
914 el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
915 {
916         struct net_device *dev = (struct net_device *)dev_id;
917         struct el3_private *lp;
918         int ioaddr, status;
919         int i = max_interrupt_work;
920
921         if (dev == NULL) {
922                 printk ("el3_interrupt(): irq %d for unknown device.\n", irq);
923                 return IRQ_NONE;
924         }
925
926         lp = netdev_priv(dev);
927         spin_lock(&lp->lock);
928
929         ioaddr = dev->base_addr;
930
931         if (el3_debug > 4) {
932                 status = inw(ioaddr + EL3_STATUS);
933                 printk("%s: interrupt, status %4.4x.\n", dev->name, status);
934         }
935
936         while ((status = inw(ioaddr + EL3_STATUS)) &
937                    (IntLatch | RxComplete | StatsFull)) {
938
939                 if (status & RxComplete)
940                         el3_rx(dev);
941
942                 if (status & TxAvailable) {
943                         if (el3_debug > 5)
944                                 printk("        TX room bit was handled.\n");
945                         /* There's room in the FIFO for a full-sized packet. */
946                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
947                         netif_wake_queue (dev);
948                 }
949                 if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
950                         /* Handle all uncommon interrupts. */
951                         if (status & StatsFull)                         /* Empty statistics. */
952                                 update_stats(dev);
953                         if (status & RxEarly) {                         /* Rx early is unused. */
954                                 el3_rx(dev);
955                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
956                         }
957                         if (status & TxComplete) {                      /* Really Tx error. */
958                                 struct el3_private *lp = netdev_priv(dev);
959                                 short tx_status;
960                                 int i = 4;
961
962                                 while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
963                                         if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
964                                         if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
965                                         if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
966                                         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
967                                 }
968                         }
969                         if (status & AdapterFailure) {
970                                 /* Adapter failure requires Rx reset and reinit. */
971                                 outw(RxReset, ioaddr + EL3_CMD);
972                                 /* Set the Rx filter to the current state. */
973                                 outw(SetRxFilter | RxStation | RxBroadcast
974                                          | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
975                                          | (dev->flags & IFF_PROMISC ? RxProm : 0),
976                                          ioaddr + EL3_CMD);
977                                 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
978                                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
979                         }
980                 }
981
982                 if (--i < 0) {
983                         printk("%s: Infinite loop in interrupt, status %4.4x.\n",
984                                    dev->name, status);
985                         /* Clear all interrupts. */
986                         outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
987                         break;
988                 }
989                 /* Acknowledge the IRQ. */
990                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
991         }
992
993         if (el3_debug > 4) {
994                 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
995                            inw(ioaddr + EL3_STATUS));
996         }
997         spin_unlock(&lp->lock);
998         return IRQ_HANDLED;
999 }
1000
1001
1002 static struct net_device_stats *
1003 el3_get_stats(struct net_device *dev)
1004 {
1005         struct el3_private *lp = netdev_priv(dev);
1006         unsigned long flags;
1007
1008         /*
1009          *      This is fast enough not to bother with disable IRQ
1010          *      stuff.
1011          */
1012          
1013         spin_lock_irqsave(&lp->lock, flags);
1014         update_stats(dev);
1015         spin_unlock_irqrestore(&lp->lock, flags);
1016         return &lp->stats;
1017 }
1018
1019 /*  Update statistics.  We change to register window 6, so this should be run
1020         single-threaded if the device is active. This is expected to be a rare
1021         operation, and it's simpler for the rest of the driver to assume that
1022         window 1 is always valid rather than use a special window-state variable.
1023         */
1024 static void update_stats(struct net_device *dev)
1025 {
1026         struct el3_private *lp = netdev_priv(dev);
1027         int ioaddr = dev->base_addr;
1028
1029         if (el3_debug > 5)
1030                 printk("   Updating the statistics.\n");
1031         /* Turn off statistics updates while reading. */
1032         outw(StatsDisable, ioaddr + EL3_CMD);
1033         /* Switch to the stats window, and read everything. */
1034         EL3WINDOW(6);
1035         lp->stats.tx_carrier_errors     += inb(ioaddr + 0);
1036         lp->stats.tx_heartbeat_errors   += inb(ioaddr + 1);
1037         /* Multiple collisions. */         inb(ioaddr + 2);
1038         lp->stats.collisions            += inb(ioaddr + 3);
1039         lp->stats.tx_window_errors      += inb(ioaddr + 4);
1040         lp->stats.rx_fifo_errors        += inb(ioaddr + 5);
1041         lp->stats.tx_packets            += inb(ioaddr + 6);
1042         /* Rx packets   */                 inb(ioaddr + 7);
1043         /* Tx deferrals */                 inb(ioaddr + 8);
1044         inw(ioaddr + 10);       /* Total Rx and Tx octets. */
1045         inw(ioaddr + 12);
1046
1047         /* Back to window 1, and turn statistics back on. */
1048         EL3WINDOW(1);
1049         outw(StatsEnable, ioaddr + EL3_CMD);
1050         return;
1051 }
1052
1053 static int
1054 el3_rx(struct net_device *dev)
1055 {
1056         struct el3_private *lp = netdev_priv(dev);
1057         int ioaddr = dev->base_addr;
1058         short rx_status;
1059
1060         if (el3_debug > 5)
1061                 printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
1062                            inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
1063         while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
1064                 if (rx_status & 0x4000) { /* Error, update stats. */
1065                         short error = rx_status & 0x3800;
1066
1067                         outw(RxDiscard, ioaddr + EL3_CMD);
1068                         lp->stats.rx_errors++;
1069                         switch (error) {
1070                         case 0x0000:            lp->stats.rx_over_errors++; break;
1071                         case 0x0800:            lp->stats.rx_length_errors++; break;
1072                         case 0x1000:            lp->stats.rx_frame_errors++; break;
1073                         case 0x1800:            lp->stats.rx_length_errors++; break;
1074                         case 0x2000:            lp->stats.rx_frame_errors++; break;
1075                         case 0x2800:            lp->stats.rx_crc_errors++; break;
1076                         }
1077                 } else {
1078                         short pkt_len = rx_status & 0x7ff;
1079                         struct sk_buff *skb;
1080
1081                         skb = dev_alloc_skb(pkt_len+5);
1082                         lp->stats.rx_bytes += pkt_len;
1083                         if (el3_debug > 4)
1084                                 printk("Receiving packet size %d status %4.4x.\n",
1085                                            pkt_len, rx_status);
1086                         if (skb != NULL) {
1087                                 skb->dev = dev;
1088                                 skb_reserve(skb, 2);     /* Align IP on 16 byte */
1089
1090                                 /* 'skb->data' points to the start of sk_buff data area. */
1091 #ifdef  __powerpc__
1092                                 insl_ns(ioaddr+RX_FIFO, skb_put(skb,pkt_len),
1093                                                            (pkt_len + 3) >> 2);
1094 #else
1095                                 insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
1096                                          (pkt_len + 3) >> 2);
1097 #endif
1098
1099                                 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
1100                                 skb->protocol = eth_type_trans(skb,dev);
1101                                 netif_rx(skb);
1102                                 dev->last_rx = jiffies;
1103                                 lp->stats.rx_packets++;
1104                                 continue;
1105                         }
1106                         outw(RxDiscard, ioaddr + EL3_CMD);
1107                         lp->stats.rx_dropped++;
1108                         if (el3_debug)
1109                                 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
1110                                            dev->name, pkt_len);
1111                 }
1112                 inw(ioaddr + EL3_STATUS);                               /* Delay. */
1113                 while (inw(ioaddr + EL3_STATUS) & 0x1000)
1114                         printk(KERN_DEBUG "     Waiting for 3c509 to discard packet, status %x.\n",
1115                                    inw(ioaddr + EL3_STATUS) );
1116         }
1117
1118         return 0;
1119 }
1120
1121 /*
1122  *     Set or clear the multicast filter for this adaptor.
1123  */
1124 static void
1125 set_multicast_list(struct net_device *dev)
1126 {
1127         unsigned long flags;
1128         struct el3_private *lp = netdev_priv(dev);
1129         int ioaddr = dev->base_addr;
1130
1131         if (el3_debug > 1) {
1132                 static int old;
1133                 if (old != dev->mc_count) {
1134                         old = dev->mc_count;
1135                         printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
1136                 }
1137         }
1138         spin_lock_irqsave(&lp->lock, flags);
1139         if (dev->flags&IFF_PROMISC) {
1140                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1141                          ioaddr + EL3_CMD);
1142         }
1143         else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
1144                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
1145         }
1146         else
1147                 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1148         spin_unlock_irqrestore(&lp->lock, flags);
1149 }
1150
1151 static int
1152 el3_close(struct net_device *dev)
1153 {
1154         int ioaddr = dev->base_addr;
1155         struct el3_private *lp = netdev_priv(dev);
1156         
1157         if (el3_debug > 2)
1158                 printk("%s: Shutting down ethercard.\n", dev->name);
1159
1160         el3_down(dev);
1161
1162         free_irq(dev->irq, dev);
1163         /* Switching back to window 0 disables the IRQ. */
1164         EL3WINDOW(0);
1165         if (lp->type != EL3_EISA) {
1166                 /* But we explicitly zero the IRQ line select anyway. Don't do
1167                  * it on EISA cards, it prevents the module from getting an
1168                  * IRQ after unload+reload... */
1169                 outw(0x0f00, ioaddr + WN0_IRQ);
1170         }
1171
1172         return 0;
1173 }
1174
1175 static int 
1176 el3_link_ok(struct net_device *dev)
1177 {
1178         int ioaddr = dev->base_addr;
1179         u16 tmp;
1180
1181         EL3WINDOW(4);
1182         tmp = inw(ioaddr + WN4_MEDIA);
1183         EL3WINDOW(1);
1184         return tmp & (1<<11);
1185 }
1186
1187 static int
1188 el3_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1189 {
1190         u16 tmp;
1191         int ioaddr = dev->base_addr;
1192         
1193         EL3WINDOW(0);
1194         /* obtain current transceiver via WN4_MEDIA? */ 
1195         tmp = inw(ioaddr + WN0_ADDR_CONF);
1196         ecmd->transceiver = XCVR_INTERNAL;
1197         switch (tmp >> 14) {
1198         case 0:
1199                 ecmd->port = PORT_TP;
1200                 break;
1201         case 1:
1202                 ecmd->port = PORT_AUI;
1203                 ecmd->transceiver = XCVR_EXTERNAL;
1204                 break;
1205         case 3:
1206                 ecmd->port = PORT_BNC;
1207         default:
1208                 break;
1209         }
1210
1211         ecmd->duplex = DUPLEX_HALF;
1212         ecmd->supported = 0;
1213         tmp = inw(ioaddr + WN0_CONF_CTRL);
1214         if (tmp & (1<<13))
1215                 ecmd->supported |= SUPPORTED_AUI;
1216         if (tmp & (1<<12))
1217                 ecmd->supported |= SUPPORTED_BNC;
1218         if (tmp & (1<<9)) {
1219                 ecmd->supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half |
1220                                 SUPPORTED_10baseT_Full; /* hmm... */
1221                 EL3WINDOW(4);
1222                 tmp = inw(ioaddr + WN4_NETDIAG);
1223                 if (tmp & FD_ENABLE)
1224                         ecmd->duplex = DUPLEX_FULL;
1225         }
1226
1227         ecmd->speed = SPEED_10;
1228         EL3WINDOW(1);
1229         return 0;
1230 }
1231
1232 static int
1233 el3_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1234 {
1235         u16 tmp;
1236         int ioaddr = dev->base_addr;
1237
1238         if (ecmd->speed != SPEED_10)
1239                 return -EINVAL;
1240         if ((ecmd->duplex != DUPLEX_HALF) && (ecmd->duplex != DUPLEX_FULL))
1241                 return -EINVAL;
1242         if ((ecmd->transceiver != XCVR_INTERNAL) && (ecmd->transceiver != XCVR_EXTERNAL))
1243                 return -EINVAL;
1244
1245         /* change XCVR type */
1246         EL3WINDOW(0);
1247         tmp = inw(ioaddr + WN0_ADDR_CONF);
1248         switch (ecmd->port) {
1249         case PORT_TP:
1250                 tmp &= ~(3<<14);
1251                 dev->if_port = 0;
1252                 break;
1253         case PORT_AUI:
1254                 tmp |= (1<<14);
1255                 dev->if_port = 1;
1256                 break;
1257         case PORT_BNC:
1258                 tmp |= (3<<14);
1259                 dev->if_port = 3;
1260                 break;
1261         default:
1262                 return -EINVAL;
1263         }
1264
1265         outw(tmp, ioaddr + WN0_ADDR_CONF);
1266         if (dev->if_port == 3) {
1267                 /* fire up the DC-DC convertor if BNC gets enabled */
1268                 tmp = inw(ioaddr + WN0_ADDR_CONF);
1269                 if (tmp & (3 << 14)) {
1270                         outw(StartCoax, ioaddr + EL3_CMD);
1271                         udelay(800);
1272                 } else
1273                         return -EIO;
1274         }
1275
1276         EL3WINDOW(4);
1277         tmp = inw(ioaddr + WN4_NETDIAG);
1278         if (ecmd->duplex == DUPLEX_FULL)
1279                 tmp |= FD_ENABLE;
1280         else
1281                 tmp &= ~FD_ENABLE;
1282         outw(tmp, ioaddr + WN4_NETDIAG);
1283         EL3WINDOW(1);
1284
1285         return 0;
1286 }
1287
1288 /**
1289  * netdev_ethtool_ioctl: Handle network interface SIOCETHTOOL ioctls
1290  * @dev: network interface on which out-of-band action is to be performed
1291  * @useraddr: userspace address to which data is to be read and returned
1292  *
1293  * Process the various commands of the SIOCETHTOOL interface.
1294  */
1295
1296 static int
1297 netdev_ethtool_ioctl (struct net_device *dev, void __user *useraddr)
1298 {
1299         u32 ethcmd;
1300         struct el3_private *lp = netdev_priv(dev);
1301
1302         /* dev_ioctl() in ../../net/core/dev.c has already checked
1303            capable(CAP_NET_ADMIN), so don't bother with that here.  */
1304
1305         if (get_user(ethcmd, (u32 __user *)useraddr))
1306                 return -EFAULT;
1307
1308         switch (ethcmd) {
1309
1310         case ETHTOOL_GDRVINFO: {
1311                 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1312                 strcpy (info.driver, DRV_NAME);
1313                 strcpy (info.version, DRV_VERSION);
1314                 if (copy_to_user (useraddr, &info, sizeof (info)))
1315                         return -EFAULT;
1316                 return 0;
1317         }
1318
1319         /* get settings */
1320         case ETHTOOL_GSET: {
1321                 int ret;
1322                 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1323                 spin_lock_irq(&lp->lock);
1324                 ret = el3_netdev_get_ecmd(dev, &ecmd);
1325                 spin_unlock_irq(&lp->lock);
1326                 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1327                         return -EFAULT;
1328                 return ret;
1329         }
1330
1331         /* set settings */
1332         case ETHTOOL_SSET: {
1333                 int ret;
1334                 struct ethtool_cmd ecmd;
1335                 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1336                         return -EFAULT;
1337                 spin_lock_irq(&lp->lock);
1338                 ret = el3_netdev_set_ecmd(dev, &ecmd);
1339                 spin_unlock_irq(&lp->lock);
1340                 return ret;
1341         }
1342
1343         /* get link status */
1344         case ETHTOOL_GLINK: {
1345                 struct ethtool_value edata = { ETHTOOL_GLINK };
1346                 spin_lock_irq(&lp->lock);
1347                 edata.data = el3_link_ok(dev);
1348                 spin_unlock_irq(&lp->lock);
1349                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1350                         return -EFAULT;
1351                 return 0;
1352         }
1353
1354         /* get message-level */
1355         case ETHTOOL_GMSGLVL: {
1356                 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1357                 edata.data = el3_debug;
1358                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1359                         return -EFAULT;
1360                 return 0;
1361         }
1362         /* set message-level */
1363         case ETHTOOL_SMSGLVL: {
1364                 struct ethtool_value edata;
1365                 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1366                         return -EFAULT;
1367                 el3_debug = edata.data;
1368                 return 0;
1369         }
1370
1371         default:
1372                 break;
1373         }
1374
1375         return -EOPNOTSUPP;
1376 }
1377
1378 /**
1379  * netdev_ioctl: Handle network interface ioctls
1380  * @dev: network interface on which out-of-band action is to be performed
1381  * @rq: user request data
1382  * @cmd: command issued by user
1383  *
1384  * Process the various out-of-band ioctls passed to this driver.
1385  */
1386
1387 static int
1388 netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1389 {
1390         int rc = 0;
1391
1392         switch (cmd) {
1393         case SIOCETHTOOL:
1394                 rc = netdev_ethtool_ioctl(dev, rq->ifr_data);
1395                 break;
1396
1397         default:
1398                 rc = -EOPNOTSUPP;
1399                 break;
1400         }
1401
1402         return rc;
1403 }
1404
1405 static void
1406 el3_down(struct net_device *dev)
1407 {
1408         int ioaddr = dev->base_addr;
1409
1410         netif_stop_queue(dev);
1411
1412         /* Turn off statistics ASAP.  We update lp->stats below. */
1413         outw(StatsDisable, ioaddr + EL3_CMD);
1414
1415         /* Disable the receiver and transmitter. */
1416         outw(RxDisable, ioaddr + EL3_CMD);
1417         outw(TxDisable, ioaddr + EL3_CMD);
1418
1419         if (dev->if_port == 3)
1420                 /* Turn off thinnet power.  Green! */
1421                 outw(StopCoax, ioaddr + EL3_CMD);
1422         else if (dev->if_port == 0) {
1423                 /* Disable link beat and jabber, if_port may change here next open(). */
1424                 EL3WINDOW(4);
1425                 outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
1426         }
1427
1428         outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1429
1430         update_stats(dev);
1431 }
1432
1433 static void
1434 el3_up(struct net_device *dev)
1435 {
1436         int i, sw_info, net_diag;
1437         int ioaddr = dev->base_addr;
1438         
1439         /* Activating the board required and does no harm otherwise */
1440         outw(0x0001, ioaddr + 4);
1441
1442         /* Set the IRQ line. */
1443         outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
1444
1445         /* Set the station address in window 2 each time opened. */
1446         EL3WINDOW(2);
1447
1448         for (i = 0; i < 6; i++)
1449                 outb(dev->dev_addr[i], ioaddr + i);
1450
1451         if ((dev->if_port & 0x03) == 3) /* BNC interface */
1452                 /* Start the thinnet transceiver. We should really wait 50ms...*/
1453                 outw(StartCoax, ioaddr + EL3_CMD);
1454         else if ((dev->if_port & 0x03) == 0) { /* 10baseT interface */
1455                 /* Combine secondary sw_info word (the adapter level) and primary
1456                         sw_info word (duplex setting plus other useless bits) */
1457                 EL3WINDOW(0);
1458                 sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) | 
1459                         (read_eeprom(ioaddr, 0x0d) & 0xBff0);
1460
1461                 EL3WINDOW(4);
1462                 net_diag = inw(ioaddr + WN4_NETDIAG);
1463                 net_diag = (net_diag | FD_ENABLE); /* temporarily assume full-duplex will be set */
1464                 printk("%s: ", dev->name);
1465                 switch (dev->if_port & 0x0c) {
1466                         case 12:
1467                                 /* force full-duplex mode if 3c5x9b */
1468                                 if (sw_info & 0x000f) {
1469                                         printk("Forcing 3c5x9b full-duplex mode");
1470                                         break;
1471                                 }
1472                         case 8:
1473                                 /* set full-duplex mode based on eeprom config setting */
1474                                 if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
1475                                         printk("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
1476                                         break;
1477                                 }
1478                         default:
1479                                 /* xcvr=(0 || 4) OR user has an old 3c5x9 non "B" model */
1480                                 printk("Setting 3c5x9/3c5x9B half-duplex mode");
1481                                 net_diag = (net_diag & ~FD_ENABLE); /* disable full duplex */
1482                 }
1483
1484                 outw(net_diag, ioaddr + WN4_NETDIAG);
1485                 printk(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
1486                 if (el3_debug > 3)
1487                         printk("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
1488                 /* Enable link beat and jabber check. */
1489                 outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
1490         }
1491
1492         /* Switch to the stats window, and clear all stats by reading. */
1493         outw(StatsDisable, ioaddr + EL3_CMD);
1494         EL3WINDOW(6);
1495         for (i = 0; i < 9; i++)
1496                 inb(ioaddr + i);
1497         inw(ioaddr + 10);
1498         inw(ioaddr + 12);
1499
1500         /* Switch to register set 1 for normal use. */
1501         EL3WINDOW(1);
1502
1503         /* Accept b-case and phys addr only. */
1504         outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1505         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
1506
1507         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
1508         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
1509         /* Allow status bits to be seen. */
1510         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
1511         /* Ack all pending events, and set active indicator mask. */
1512         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
1513                  ioaddr + EL3_CMD);
1514         outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
1515                  ioaddr + EL3_CMD);
1516
1517         netif_start_queue(dev);
1518 }
1519
1520 /* Power Management support functions */
1521 #ifdef CONFIG_PM
1522
1523 static int
1524 el3_suspend(struct pm_dev *pdev)
1525 {
1526         unsigned long flags;
1527         struct net_device *dev;
1528         struct el3_private *lp;
1529         int ioaddr;
1530         
1531         if (!pdev && !pdev->data)
1532                 return -EINVAL;
1533
1534         dev = (struct net_device *)pdev->data;
1535         lp = netdev_priv(dev);
1536         ioaddr = dev->base_addr;
1537
1538         spin_lock_irqsave(&lp->lock, flags);
1539
1540         if (netif_running(dev))
1541                 netif_device_detach(dev);
1542
1543         el3_down(dev);
1544         outw(PowerDown, ioaddr + EL3_CMD);
1545
1546         spin_unlock_irqrestore(&lp->lock, flags);
1547         return 0;
1548 }
1549
1550 static int
1551 el3_resume(struct pm_dev *pdev)
1552 {
1553         unsigned long flags;
1554         struct net_device *dev;
1555         struct el3_private *lp;
1556         int ioaddr;
1557         
1558         if (!pdev && !pdev->data)
1559                 return -EINVAL;
1560
1561         dev = (struct net_device *)pdev->data;
1562         lp = netdev_priv(dev);
1563         ioaddr = dev->base_addr;
1564
1565         spin_lock_irqsave(&lp->lock, flags);
1566
1567         outw(PowerUp, ioaddr + EL3_CMD);
1568         el3_up(dev);
1569
1570         if (netif_running(dev))
1571                 netif_device_attach(dev);
1572                 
1573         spin_unlock_irqrestore(&lp->lock, flags);
1574         return 0;
1575 }
1576
1577 static int
1578 el3_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
1579 {
1580         switch (rqst) {
1581                 case PM_SUSPEND:
1582                         return el3_suspend(pdev);
1583
1584                 case PM_RESUME:
1585                         return el3_resume(pdev);
1586         }
1587         return 0;
1588 }
1589
1590 #endif /* CONFIG_PM */
1591
1592 /* Parameters that may be passed into the module. */
1593 static int debug = -1;
1594 static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
1595 static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1596
1597 MODULE_PARM(debug,"i");
1598 MODULE_PARM(irq,"1-8i");
1599 MODULE_PARM(xcvr,"1-12i");
1600 MODULE_PARM(max_interrupt_work, "i");
1601 MODULE_PARM_DESC(debug, "debug level (0-6)");
1602 MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
1603 MODULE_PARM_DESC(xcvr,"transceiver(s) (0=internal, 1=external)");
1604 MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
1605 #if defined(__ISAPNP__)
1606 MODULE_PARM(nopnp, "i");
1607 MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
1608 MODULE_DEVICE_TABLE(isapnp, el3_isapnp_adapters);
1609 #endif  /* __ISAPNP__ */
1610 MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B) ISA/PnP ethernet driver");
1611 MODULE_LICENSE("GPL");
1612
1613 static int __init el3_init_module(void)
1614 {
1615         el3_cards = 0;
1616
1617         if (debug >= 0)
1618                 el3_debug = debug;
1619
1620         el3_root_dev = NULL;
1621         while (el3_probe(el3_cards) == 0) {
1622                 if (irq[el3_cards] > 1)
1623                         el3_root_dev->irq = irq[el3_cards];
1624                 if (xcvr[el3_cards] >= 0)
1625                         el3_root_dev->if_port = xcvr[el3_cards];
1626                 el3_cards++;
1627         }
1628
1629 #ifdef CONFIG_EISA
1630         if (eisa_driver_register (&el3_eisa_driver) < 0) {
1631                 eisa_driver_unregister (&el3_eisa_driver);
1632         }
1633 #endif
1634 #ifdef CONFIG_MCA
1635         mca_register_driver(&el3_mca_driver);
1636 #endif
1637         return 0;
1638 }
1639
1640 static void __exit el3_cleanup_module(void)
1641 {
1642         struct net_device *next_dev;
1643
1644         while (el3_root_dev) {
1645                 struct el3_private *lp = netdev_priv(el3_root_dev);
1646
1647                 next_dev = lp->next_dev;
1648                 el3_common_remove (el3_root_dev);
1649                 el3_root_dev = next_dev;
1650         }
1651
1652 #ifdef CONFIG_EISA
1653         eisa_driver_unregister (&el3_eisa_driver);
1654 #endif
1655 #ifdef CONFIG_MCA
1656         mca_unregister_driver(&el3_mca_driver);
1657 #endif
1658 }
1659
1660 module_init (el3_init_module);
1661 module_exit (el3_cleanup_module);
1662