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