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