VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / ewrk3.c
1 /*  ewrk3.c: A DIGITAL EtherWORKS 3 ethernet driver for Linux.
2
3    Written 1994 by David C. Davies.
4
5    Copyright 1994 Digital Equipment Corporation.
6
7    This software may be used and distributed according to the terms of
8    the GNU General Public License, incorporated herein by reference.
9
10    This driver is written for the Digital Equipment Corporation series
11    of EtherWORKS ethernet cards:
12
13    DE203 Turbo (BNC)
14    DE204 Turbo (TP)
15    DE205 Turbo (TP BNC)
16
17    The driver has been tested on a relatively busy  network using the DE205
18    card and benchmarked with 'ttcp': it transferred 16M  of data at 975kB/s
19    (7.8Mb/s) to a DECstation 5000/200.
20
21    The author may be reached at davies@maniac.ultranet.com.
22
23    =========================================================================
24    This driver has been written  substantially  from scratch, although  its
25    inheritance of style and stack interface from 'depca.c' and in turn from
26    Donald Becker's 'lance.c' should be obvious.
27
28    The  DE203/4/5 boards  all  use a new proprietary   chip in place of the
29    LANCE chip used in prior cards  (DEPCA, DE100, DE200/1/2, DE210, DE422).
30    Use the depca.c driver in the standard distribution  for the LANCE based
31    cards from DIGITAL; this driver will not work with them.
32
33    The DE203/4/5 cards have 2  main modes: shared memory  and I/O only. I/O
34    only makes  all the card accesses through  I/O transactions and  no high
35    (shared)  memory is used. This  mode provides a >48% performance penalty
36    and  is deprecated in this  driver,  although allowed to provide initial
37    setup when hardstrapped.
38
39    The shared memory mode comes in 3 flavours: 2kB, 32kB and 64kB. There is
40    no point in using any mode other than the 2kB  mode - their performances
41    are virtually identical, although the driver has  been tested in the 2kB
42    and 32kB modes. I would suggest you uncomment the line:
43
44    FORCE_2K_MODE;
45
46    to allow the driver to configure the card as a  2kB card at your current
47    base  address, thus leaving more  room to clutter  your  system box with
48    other memory hungry boards.
49
50    As many ISA  and EISA cards  can be supported  under this driver  as you
51    wish, limited primarily  by the available IRQ lines,  rather than by the
52    available I/O addresses  (24 ISA,  16 EISA).   I have  checked different
53    configurations of  multiple  depca cards and  ewrk3 cards  and have  not
54    found a problem yet (provided you have at least depca.c v0.38) ...
55
56    The board IRQ setting   must be at  an unused  IRQ which is  auto-probed
57    using  Donald  Becker's autoprobe  routines.   All  these cards   are at
58    {5,10,11,15}.
59
60    No 16MB memory  limitation should exist with this  driver as DMA is  not
61    used and the common memory area is in low memory on the network card (my
62    current system has 20MB and I've not had problems yet).
63
64    The ability to load  this driver as a  loadable module has been included
65    and used  extensively during the  driver development (to save those long
66    reboot sequences). To utilise this ability, you have to do 8 things:
67
68    0) have a copy of the loadable modules code installed on your system.
69    1) copy ewrk3.c from the  /linux/drivers/net directory to your favourite
70    temporary directory.
71    2) edit the  source code near  line 1898 to reflect  the I/O address and
72    IRQ you're using.
73    3) compile  ewrk3.c, but include -DMODULE in  the command line to ensure
74    that the correct bits are compiled (see end of source code).
75    4) if you are wanting to add a new  card, goto 5. Otherwise, recompile a
76    kernel with the ewrk3 configuration turned off and reboot.
77    5) insmod ewrk3.o
78    [Alan Cox: Changed this so you can insmod ewrk3.o irq=x io=y]
79    [Adam Kropelin: now accepts irq=x1,x2 io=y1,y2 for multiple cards]
80    6) run the net startup bits for your new eth?? interface manually
81    (usually /etc/rc.inet[12] at boot time).
82    7) enjoy!
83
84    Note that autoprobing is not allowed in loadable modules - the system is
85    already up and running and you're messing with interrupts.
86
87    To unload a module, turn off the associated interface
88    'ifconfig eth?? down' then 'rmmod ewrk3'.
89
90    Promiscuous   mode has been  turned  off  in this driver,   but  all the
91    multicast  address bits  have been   turned on. This  improved the  send
92    performance on a busy network by about 13%.
93
94    Ioctl's have now been provided (primarily because  I wanted to grab some
95    packet size statistics). They  are patterned after 'plipconfig.c' from a
96    suggestion by Alan Cox.  Using these  ioctls, you can enable promiscuous
97    mode, add/delete multicast  addresses, change the hardware address,  get
98    packet size distribution statistics and muck around with the control and
99    status register. I'll add others if and when the need arises.
100
101    TO DO:
102    ------
103
104
105    Revision History
106    ----------------
107
108    Version   Date        Description
109
110    0.1     26-aug-94   Initial writing. ALPHA code release.
111    0.11    31-aug-94   Fixed: 2k mode memory base calc.,
112    LeMAC version calc.,
113    IRQ vector assignments during autoprobe.
114    0.12    31-aug-94   Tested working on LeMAC2 (DE20[345]-AC) card.
115    Fixed up MCA hash table algorithm.
116    0.20     4-sep-94   Added IOCTL functionality.
117    0.21    14-sep-94   Added I/O mode.
118    0.21axp 15-sep-94   Special version for ALPHA AXP Linux V1.0.
119    0.22    16-sep-94   Added more IOCTLs & tidied up.
120    0.23    21-sep-94   Added transmit cut through.
121    0.24    31-oct-94   Added uid checks in some ioctls.
122    0.30     1-nov-94   BETA code release.
123    0.31     5-dec-94   Added check/allocate region code.
124    0.32    16-jan-95   Broadcast packet fix.
125    0.33    10-Feb-95   Fix recognition bug reported by <bkm@star.rl.ac.uk>.
126    0.40    27-Dec-95   Rationalise MODULE and autoprobe code.
127    Rewrite for portability & updated.
128    ALPHA support from <jestabro@amt.tay1.dec.com>
129    Added verify_area() calls in ewrk3_ioctl() from
130    suggestion by <heiko@colossus.escape.de>.
131    Add new multicasting code.
132    0.41    20-Jan-96   Fix IRQ set up problem reported by
133    <kenneth@bbs.sas.ntu.ac.sg>.
134    0.42    22-Apr-96   Fix alloc_device() bug <jari@markkus2.fimr.fi>
135    0.43    16-Aug-96   Update alloc_device() to conform to de4x5.c
136    0.44    08-Nov-01   use library crc32 functions <Matt_Domsch@dell.com>
137    0.45    19-Jul-02   fix unaligned access on alpha <martin@bruli.net>
138    0.46    10-Oct-02   Multiple NIC support when module <akropel1@rochester.rr.com>
139    0.47    18-Oct-02   ethtool support <akropel1@rochester.rr.com>
140    0.48    18-Oct-02   cli/sti removal for 2.5 <vda@port.imtp.ilyichevsk.odessa.ua>
141    ioctl locking, signature search cleanup <akropel1@rochester.rr.com>
142
143    =========================================================================
144  */
145
146 #include <linux/module.h>
147 #include <linux/kernel.h>
148 #include <linux/string.h>
149 #include <linux/errno.h>
150 #include <linux/ioport.h>
151 #include <linux/slab.h>
152 #include <linux/interrupt.h>
153 #include <linux/delay.h>
154 #include <linux/init.h>
155 #include <linux/crc32.h>
156 #include <linux/netdevice.h>
157 #include <linux/etherdevice.h>
158 #include <linux/skbuff.h>
159 #include <linux/ethtool.h>
160 #include <linux/time.h>
161 #include <linux/types.h>
162 #include <linux/unistd.h>
163 #include <linux/ctype.h>
164
165 #include <asm/bitops.h>
166 #include <asm/io.h>
167 #include <asm/dma.h>
168 #include <asm/uaccess.h>
169
170 #include "ewrk3.h"
171
172 #define DRV_NAME        "ewrk3"
173 #define DRV_VERSION     "0.48"
174
175 static char version[] __initdata =
176 DRV_NAME ":v" DRV_VERSION " 2002/10/18 davies@maniac.ultranet.com\n";
177
178 #ifdef EWRK3_DEBUG
179 static int ewrk3_debug = EWRK3_DEBUG;
180 #else
181 static int ewrk3_debug = 1;
182 #endif
183
184 #define EWRK3_NDA 0xffe0        /* No Device Address */
185
186 #define PROBE_LENGTH    32
187 #define ETH_PROM_SIG    0xAA5500FFUL
188
189 #ifndef EWRK3_SIGNATURE
190 #define EWRK3_SIGNATURE {"DE203","DE204","DE205",""}
191 #define EWRK3_STRLEN 8
192 #endif
193
194 #ifndef EWRK3_RAM_BASE_ADDRESSES
195 #define EWRK3_RAM_BASE_ADDRESSES {0xc0000,0xd0000,0x00000}
196 #endif
197
198 /*
199    ** Sets up the I/O area for the autoprobe.
200  */
201 #define EWRK3_IO_BASE 0x100     /* Start address for probe search */
202 #define EWRK3_IOP_INC 0x20      /* I/O address increment */
203 #define EWRK3_TOTAL_SIZE 0x20   /* required I/O address length */
204
205 /* If you change this, remember to also change MODULE_PARM array limits */
206 #ifndef MAX_NUM_EWRK3S
207 #define MAX_NUM_EWRK3S 21
208 #endif
209
210 #ifndef EWRK3_EISA_IO_PORTS
211 #define EWRK3_EISA_IO_PORTS 0x0c00      /* I/O port base address, slot 0 */
212 #endif
213
214 #ifndef MAX_EISA_SLOTS
215 #define MAX_EISA_SLOTS 16
216 #define EISA_SLOT_INC 0x1000
217 #endif
218
219 #define QUEUE_PKT_TIMEOUT (1*HZ)        /* Jiffies */
220
221 /*
222    ** EtherWORKS 3 shared memory window sizes
223  */
224 #define IO_ONLY         0x00
225 #define SHMEM_2K        0x800
226 #define SHMEM_32K       0x8000
227 #define SHMEM_64K       0x10000
228
229 /*
230    ** EtherWORKS 3 IRQ ENABLE/DISABLE
231  */
232 #define ENABLE_IRQs { \
233   icr |= lp->irq_mask;\
234   outb(icr, EWRK3_ICR);                     /* Enable the IRQs */\
235 }
236
237 #define DISABLE_IRQs { \
238   icr = inb(EWRK3_ICR);\
239   icr &= ~lp->irq_mask;\
240   outb(icr, EWRK3_ICR);                     /* Disable the IRQs */\
241 }
242
243 /*
244    ** EtherWORKS 3 START/STOP
245  */
246 #define START_EWRK3 { \
247   csr = inb(EWRK3_CSR);\
248   csr &= ~(CSR_TXD|CSR_RXD);\
249   outb(csr, EWRK3_CSR);                     /* Enable the TX and/or RX */\
250 }
251
252 #define STOP_EWRK3 { \
253   csr = (CSR_TXD|CSR_RXD);\
254   outb(csr, EWRK3_CSR);                     /* Disable the TX and/or RX */\
255 }
256
257 /*
258    ** The EtherWORKS 3 private structure
259  */
260 #define EWRK3_PKT_STAT_SZ 16
261 #define EWRK3_PKT_BIN_SZ  128   /* Should be >=100 unless you
262                                    increase EWRK3_PKT_STAT_SZ */
263
264 struct ewrk3_stats {
265         u32 bins[EWRK3_PKT_STAT_SZ];
266         u32 unicast;
267         u32 multicast;
268         u32 broadcast;
269         u32 excessive_collisions;
270         u32 tx_underruns;
271         u32 excessive_underruns;
272 };
273
274 struct ewrk3_private {
275         char adapter_name[80];  /* Name exported to /proc/ioports */
276         u_long shmem_base;      /* Shared memory start address */
277         u_long shmem_length;    /* Shared memory window length */
278         struct net_device_stats stats;  /* Public stats */
279         struct ewrk3_stats pktStats; /* Private stats counters */
280         u_char irq_mask;        /* Adapter IRQ mask bits */
281         u_char mPage;           /* Maximum 2kB Page number */
282         u_char lemac;           /* Chip rev. level */
283         u_char hard_strapped;   /* Don't allow a full open */
284         u_char txc;             /* Transmit cut through */
285         u_char *mctbl;          /* Pointer to the multicast table */
286         u_char led_mask;        /* Used to reserve LED access for ethtool */
287         spinlock_t hw_lock;
288 };
289
290 /*
291    ** Force the EtherWORKS 3 card to be in 2kB MODE
292  */
293 #define FORCE_2K_MODE { \
294   shmem_length = SHMEM_2K;\
295   outb(((mem_start - 0x80000) >> 11), EWRK3_MBR);\
296 }
297
298 /*
299    ** Public Functions
300  */
301 static int ewrk3_open(struct net_device *dev);
302 static int ewrk3_queue_pkt(struct sk_buff *skb, struct net_device *dev);
303 static irqreturn_t ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
304 static int ewrk3_close(struct net_device *dev);
305 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev);
306 static void set_multicast_list(struct net_device *dev);
307 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
308
309 /*
310    ** Private functions
311  */
312 static int ewrk3_hw_init(struct net_device *dev, u_long iobase);
313 static void ewrk3_init(struct net_device *dev);
314 static int ewrk3_rx(struct net_device *dev);
315 static int ewrk3_tx(struct net_device *dev);
316 static void ewrk3_timeout(struct net_device *dev);
317
318 static void EthwrkSignature(char *name, char *eeprom_image);
319 static int DevicePresent(u_long iobase);
320 static void SetMulticastFilter(struct net_device *dev);
321 static int EISA_signature(char *name, s32 eisa_id);
322
323 static int Read_EEPROM(u_long iobase, u_char eaddr);
324 static int Write_EEPROM(short data, u_long iobase, u_char eaddr);
325 static u_char get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType);
326
327 static int ewrk3_probe1(struct net_device *dev, u_long iobase, int irq);
328 static int isa_probe(struct net_device *dev, u_long iobase);
329 static int eisa_probe(struct net_device *dev, u_long iobase);
330
331 static u_char irq[MAX_NUM_EWRK3S+1] = {5, 0, 10, 3, 11, 9, 15, 12};
332
333 static char name[EWRK3_STRLEN + 1];
334 static int num_ewrks3s;
335
336 /*
337    ** Miscellaneous defines...
338  */
339 #define INIT_EWRK3 {\
340     outb(EEPROM_INIT, EWRK3_IOPR);\
341     mdelay(1);\
342 }
343
344 #ifndef MODULE
345 struct net_device * __init ewrk3_probe(int unit)
346 {
347         struct net_device *dev = alloc_etherdev(sizeof(struct ewrk3_private));
348         int err;
349
350         if (!dev)
351                 return ERR_PTR(-ENOMEM);
352
353         if (unit >= 0) {
354                 sprintf(dev->name, "eth%d", unit);
355                 netdev_boot_setup_check(dev);
356         }
357         SET_MODULE_OWNER(dev);
358
359         err = ewrk3_probe1(dev, dev->base_addr, dev->irq);
360         if (err) 
361                 goto out;
362         return dev;
363 out:
364         free_netdev(dev);
365         return ERR_PTR(err);
366         
367 }
368 #endif
369
370 static int __init ewrk3_probe1(struct net_device *dev, u_long iobase, int irq)
371 {
372         int err;
373
374         dev->base_addr = iobase;
375         dev->irq = irq;
376
377         /* Address PROM pattern */
378         err = isa_probe(dev, iobase);
379         if (err != 0) 
380                 err = eisa_probe(dev, iobase);
381
382         if (err)
383                 return err;
384
385         err = register_netdev(dev);
386         if (err)
387                 release_region(dev->base_addr, EWRK3_TOTAL_SIZE);
388
389         return err;
390 }
391
392 static int __init 
393 ewrk3_hw_init(struct net_device *dev, u_long iobase)
394 {
395         struct ewrk3_private *lp;
396         int i, status = 0;
397         u_long mem_start, shmem_length;
398         u_char cr, cmr, icr, nicsr, lemac, hard_strapped = 0;
399         u_char eeprom_image[EEPROM_MAX], chksum, eisa_cr = 0;
400
401         /*
402         ** Stop the EWRK3. Enable the DBR ROM. Disable interrupts and remote boot.
403         ** This also disables the EISA_ENABLE bit in the EISA Control Register.
404          */
405         if (iobase > 0x400)
406                 eisa_cr = inb(EISA_CR);
407         INIT_EWRK3;
408
409         nicsr = inb(EWRK3_CSR);
410
411         icr = inb(EWRK3_ICR);
412         icr &= 0x70;
413         outb(icr, EWRK3_ICR);   /* Disable all the IRQs */
414
415         if (nicsr == (CSR_TXD | CSR_RXD))
416                 return -ENXIO;
417
418
419         /* Check that the EEPROM is alive and well and not living on Pluto... */
420         for (chksum = 0, i = 0; i < EEPROM_MAX; i += 2) {
421                 union {
422                         short val;
423                         char c[2];
424                 } tmp;
425
426                 tmp.val = (short) Read_EEPROM(iobase, (i >> 1));
427                 eeprom_image[i] = tmp.c[0];
428                 eeprom_image[i + 1] = tmp.c[1];
429                 chksum += eeprom_image[i] + eeprom_image[i + 1];
430         }
431
432         if (chksum != 0) {      /* Bad EEPROM Data! */
433                 printk("%s: Device has a bad on-board EEPROM.\n", dev->name);
434                 return -ENXIO;
435         }
436         
437         EthwrkSignature(name, eeprom_image);
438         if (*name == '\0') 
439                 return -ENXIO;
440
441         dev->base_addr = iobase;
442                                 
443         if (iobase > 0x400) {
444                 outb(eisa_cr, EISA_CR);         /* Rewrite the EISA CR */
445         }
446         lemac = eeprom_image[EEPROM_CHIPVER];
447         cmr = inb(EWRK3_CMR);
448         
449         if (((lemac == LeMAC) && ((cmr & CMR_NO_EEPROM) != CMR_NO_EEPROM)) ||
450             ((lemac == LeMAC2) && !(cmr & CMR_HS))) {
451                 printk("%s: %s at %#4lx", dev->name, name, iobase);
452                 hard_strapped = 1;
453         } else if ((iobase & 0x0fff) == EWRK3_EISA_IO_PORTS) {
454                 /* EISA slot address */
455                 printk("%s: %s at %#4lx (EISA slot %ld)",
456                        dev->name, name, iobase, ((iobase >> 12) & 0x0f));
457         } else {        /* ISA port address */
458                 printk("%s: %s at %#4lx", dev->name, name, iobase);
459         }
460
461         printk(", h/w address ");
462         if (lemac != LeMAC2)
463                 DevicePresent(iobase);  /* need after EWRK3_INIT */
464         status = get_hw_addr(dev, eeprom_image, lemac);
465         for (i = 0; i < ETH_ALEN - 1; i++) {    /* get the ethernet addr. */
466                 printk("%2.2x:", dev->dev_addr[i]);
467         }
468         printk("%2.2x,\n", dev->dev_addr[i]);
469         
470         if (status) {
471                 printk("      which has an EEPROM CRC error.\n");
472                 return -ENXIO;
473         }
474
475         if (lemac == LeMAC2) {  /* Special LeMAC2 CMR things */
476                 cmr &= ~(CMR_RA | CMR_WB | CMR_LINK | CMR_POLARITY | CMR_0WS);
477                 if (eeprom_image[EEPROM_MISC0] & READ_AHEAD)
478                         cmr |= CMR_RA;
479                 if (eeprom_image[EEPROM_MISC0] & WRITE_BEHIND)
480                         cmr |= CMR_WB;
481                 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_POL)
482                         cmr |= CMR_POLARITY;
483                 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_LINK)
484                         cmr |= CMR_LINK;
485                 if (eeprom_image[EEPROM_MISC0] & _0WS_ENA)
486                         cmr |= CMR_0WS;
487         }
488         if (eeprom_image[EEPROM_SETUP] & SETUP_DRAM)
489                 cmr |= CMR_DRAM;
490         outb(cmr, EWRK3_CMR);
491         
492         cr = inb(EWRK3_CR);     /* Set up the Control Register */
493         cr |= eeprom_image[EEPROM_SETUP] & SETUP_APD;
494         if (cr & SETUP_APD)
495                 cr |= eeprom_image[EEPROM_SETUP] & SETUP_PS;
496         cr |= eeprom_image[EEPROM_MISC0] & FAST_BUS;
497         cr |= eeprom_image[EEPROM_MISC0] & ENA_16;
498         outb(cr, EWRK3_CR);
499
500         /*
501         ** Determine the base address and window length for the EWRK3
502         ** RAM from the memory base register.
503         */
504         mem_start = inb(EWRK3_MBR);
505         shmem_length = 0;
506         if (mem_start != 0) {
507                 if ((mem_start >= 0x0a) && (mem_start <= 0x0f)) {
508                         mem_start *= SHMEM_64K;
509                         shmem_length = SHMEM_64K;
510                 } else if ((mem_start >= 0x14) && (mem_start <= 0x1f)) {
511                         mem_start *= SHMEM_32K;
512                         shmem_length = SHMEM_32K;
513                 } else if ((mem_start >= 0x40) && (mem_start <= 0xff)) {
514                         mem_start = mem_start * SHMEM_2K + 0x80000;
515                         shmem_length = SHMEM_2K;
516                 } else {
517                         return -ENXIO;
518                 }
519         }
520         /*
521         ** See the top of this source code for comments about
522         ** uncommenting this line.
523         */
524 /*          FORCE_2K_MODE; */
525         
526         if (hard_strapped) {
527                 printk("      is hard strapped.\n");
528         } else if (mem_start) {
529                 printk("      has a %dk RAM window", (int) (shmem_length >> 10));
530                 printk(" at 0x%.5lx", mem_start);
531         } else {
532                 printk("      is in I/O only mode");
533         }
534
535         lp = (struct ewrk3_private *) dev->priv;
536         lp->shmem_base = mem_start;
537         lp->shmem_length = shmem_length;
538         lp->lemac = lemac;
539         lp->hard_strapped = hard_strapped;
540         lp->led_mask = CR_LED;
541         spin_lock_init(&lp->hw_lock);
542         
543         lp->mPage = 64;
544         if (cmr & CMR_DRAM)
545                 lp->mPage <<= 1;        /* 2 DRAMS on module */
546         
547         sprintf(lp->adapter_name, "%s (%s)", name, dev->name);
548         
549         lp->irq_mask = ICR_TNEM | ICR_TXDM | ICR_RNEM | ICR_RXDM;
550         
551         if (!hard_strapped) {
552                 /*
553                 ** Enable EWRK3 board interrupts for autoprobing
554                 */
555                 icr |= ICR_IE;  /* Enable interrupts */
556                 outb(icr, EWRK3_ICR);
557                 
558                 /* The DMA channel may be passed in on this parameter. */
559                 dev->dma = 0;
560                 
561                 /* To auto-IRQ we enable the initialization-done and DMA err,
562                    interrupts. For now we will always get a DMA error. */
563                 if (dev->irq < 2) {
564 #ifndef MODULE
565                         u_char irqnum;
566                         unsigned long irq_mask;
567                         
568
569                         irq_mask = probe_irq_on();
570                         
571                         /*
572                         ** Trigger a TNE interrupt.
573                         */
574                         icr |= ICR_TNEM;
575                         outb(1, EWRK3_TDQ);     /* Write to the TX done queue */
576                         outb(icr, EWRK3_ICR);   /* Unmask the TXD interrupt */
577                         
578                         irqnum = irq[((icr & IRQ_SEL) >> 4)];
579                         
580                         mdelay(20);
581                         dev->irq = probe_irq_off(irq_mask);
582                         if ((dev->irq) && (irqnum == dev->irq)) {
583                                 printk(" and uses IRQ%d.\n", dev->irq);
584                         } else {
585                                 if (!dev->irq) {
586                                         printk(" and failed to detect IRQ line.\n");
587                                 } else if ((irqnum == 1) && (lemac == LeMAC2)) {
588                                         printk(" and an illegal IRQ line detected.\n");
589                                 } else {
590                                         printk(", but incorrect IRQ line detected.\n");
591                                 }
592                                 return -ENXIO;
593                         }
594
595                         DISABLE_IRQs;   /* Mask all interrupts */
596
597 #endif                          /* MODULE */
598                 } else {
599                         printk(" and requires IRQ%d.\n", dev->irq);
600                 }
601         }
602
603         if (ewrk3_debug > 1) {
604                 printk(version);
605         }
606         /* The EWRK3-specific entries in the device structure. */
607         dev->open = ewrk3_open;
608         dev->hard_start_xmit = ewrk3_queue_pkt;
609         dev->stop = ewrk3_close;
610         dev->get_stats = ewrk3_get_stats;
611         dev->set_multicast_list = set_multicast_list;
612         dev->do_ioctl = ewrk3_ioctl;
613         dev->tx_timeout = ewrk3_timeout;
614         dev->watchdog_timeo = QUEUE_PKT_TIMEOUT;
615         
616         dev->mem_start = 0;
617
618         return 0;
619 }
620 \f
621
622 static int ewrk3_open(struct net_device *dev)
623 {
624         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
625         u_long iobase = dev->base_addr;
626         int i, status = 0;
627         u_char icr, csr;
628
629         /*
630            ** Stop the TX and RX...
631          */
632         STOP_EWRK3;
633
634         if (!lp->hard_strapped) {
635                 if (request_irq(dev->irq, (void *) ewrk3_interrupt, 0, "ewrk3", dev)) {
636                         printk("ewrk3_open(): Requested IRQ%d is busy\n", dev->irq);
637                         status = -EAGAIN;
638                 } else {
639
640                         /*
641                            ** Re-initialize the EWRK3...
642                          */
643                         ewrk3_init(dev);
644
645                         if (ewrk3_debug > 1) {
646                                 printk("%s: ewrk3 open with irq %d\n", dev->name, dev->irq);
647                                 printk("  physical address: ");
648                                 for (i = 0; i < 5; i++) {
649                                         printk("%2.2x:", (u_char) dev->dev_addr[i]);
650                                 }
651                                 printk("%2.2x\n", (u_char) dev->dev_addr[i]);
652                                 if (lp->shmem_length == 0) {
653                                         printk("  no shared memory, I/O only mode\n");
654                                 } else {
655                                         printk("  start of shared memory: 0x%08lx\n", lp->shmem_base);
656                                         printk("  window length: 0x%04lx\n", lp->shmem_length);
657                                 }
658                                 printk("  # of DRAMS: %d\n", ((inb(EWRK3_CMR) & 0x02) ? 2 : 1));
659                                 printk("  csr:  0x%02x\n", inb(EWRK3_CSR));
660                                 printk("  cr:   0x%02x\n", inb(EWRK3_CR));
661                                 printk("  icr:  0x%02x\n", inb(EWRK3_ICR));
662                                 printk("  cmr:  0x%02x\n", inb(EWRK3_CMR));
663                                 printk("  fmqc: 0x%02x\n", inb(EWRK3_FMQC));
664                         }
665                         netif_start_queue(dev);
666                         /*
667                            ** Unmask EWRK3 board interrupts
668                          */
669                         icr = inb(EWRK3_ICR);
670                         ENABLE_IRQs;
671
672                 }
673         } else {
674                 printk(KERN_ERR "%s: ewrk3 available for hard strapped set up only.\n", dev->name);
675                 printk(KERN_ERR "      Run the 'ewrk3setup' utility or remove the hard straps.\n");
676                 return -EINVAL;
677         }
678
679         return status;
680 }
681
682 /*
683    ** Initialize the EtherWORKS 3 operating conditions
684  */
685 static void ewrk3_init(struct net_device *dev)
686 {
687         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
688         u_char csr, page;
689         u_long iobase = dev->base_addr;
690         int i;
691
692         /*
693            ** Enable any multicasts
694          */
695         set_multicast_list(dev);
696
697         /*
698         ** Set hardware MAC address. Address is initialized from the EEPROM
699         ** during startup but may have since been changed by the user.
700         */
701         for (i=0; i<ETH_ALEN; i++)
702                 outb(dev->dev_addr[i], EWRK3_PAR0 + i);
703
704         /*
705            ** Clean out any remaining entries in all the queues here
706          */
707         while (inb(EWRK3_TQ));
708         while (inb(EWRK3_TDQ));
709         while (inb(EWRK3_RQ));
710         while (inb(EWRK3_FMQ));
711
712         /*
713            ** Write a clean free memory queue
714          */
715         for (page = 1; page < lp->mPage; page++) {      /* Write the free page numbers */
716                 outb(page, EWRK3_FMQ);  /* to the Free Memory Queue */
717         }
718
719         START_EWRK3;            /* Enable the TX and/or RX */
720 }
721
722 /*
723  *  Transmit timeout
724  */
725  
726 static void ewrk3_timeout(struct net_device *dev)
727 {
728         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
729         u_char icr, csr;
730         u_long iobase = dev->base_addr;
731         
732         if (!lp->hard_strapped) 
733         {
734                 printk(KERN_WARNING"%s: transmit timed/locked out, status %04x, resetting.\n",
735                        dev->name, inb(EWRK3_CSR));
736
737                 /*
738                    ** Mask all board interrupts
739                  */
740                 DISABLE_IRQs;
741
742                 /*
743                    ** Stop the TX and RX...
744                  */
745                 STOP_EWRK3;
746
747                 ewrk3_init(dev);
748
749                 /*
750                    ** Unmask EWRK3 board interrupts
751                  */
752                 ENABLE_IRQs;
753
754                 dev->trans_start = jiffies;
755                 netif_wake_queue(dev);
756         }
757 }
758
759 /*
760    ** Writes a socket buffer to the free page queue
761  */
762 static int ewrk3_queue_pkt (struct sk_buff *skb, struct net_device *dev)
763 {
764         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
765         u_long iobase = dev->base_addr;
766         u_long buf = 0;
767         u_char icr;
768         u_char page;
769
770         spin_lock_irq (&lp->hw_lock);
771         DISABLE_IRQs;
772
773         /* if no resources available, exit, request packet be queued */
774         if (inb (EWRK3_FMQC) == 0) {
775                 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): No free resources...\n",
776                         dev->name);
777                 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): CSR: %02x ICR: %02x FMQC: %02x\n",
778                         dev->name, inb (EWRK3_CSR), inb (EWRK3_ICR),
779                         inb (EWRK3_FMQC));
780                 goto err_out;
781         }
782
783         /*
784          ** Get a free page from the FMQ
785          */
786         if ((page = inb (EWRK3_FMQ)) >= lp->mPage) {
787                 printk ("ewrk3_queue_pkt(): Invalid free memory page (%d).\n",
788                      (u_char) page);
789                 goto err_out;
790         }
791
792
793         /*
794          ** Set up shared memory window and pointer into the window
795          */
796         if (lp->shmem_length == IO_ONLY) {
797                 outb (page, EWRK3_IOPR);
798         } else if (lp->shmem_length == SHMEM_2K) {
799                 buf = lp->shmem_base;
800                 outb (page, EWRK3_MPR);
801         } else if (lp->shmem_length == SHMEM_32K) {
802                 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
803                 outb ((page >> 4), EWRK3_MPR);
804         } else if (lp->shmem_length == SHMEM_64K) {
805                 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
806                 outb ((page >> 5), EWRK3_MPR);
807         } else {
808                 printk (KERN_ERR "%s: Oops - your private data area is hosed!\n",
809                         dev->name);
810                 BUG ();
811         }
812
813         /*
814          ** Set up the buffer control structures and copy the data from
815          ** the socket buffer to the shared memory .
816          */
817         if (lp->shmem_length == IO_ONLY) {
818                 int i;
819                 u_char *p = skb->data;
820                 outb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), EWRK3_DATA);
821                 outb ((char) (skb->len & 0xff), EWRK3_DATA);
822                 outb ((char) ((skb->len >> 8) & 0xff), EWRK3_DATA);
823                 outb ((char) 0x04, EWRK3_DATA);
824                 for (i = 0; i < skb->len; i++) {
825                         outb (*p++, EWRK3_DATA);
826                 }
827                 outb (page, EWRK3_TQ);  /* Start sending pkt */
828         } else {
829                 isa_writeb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), buf);       /* ctrl byte */
830                 buf += 1;
831                 isa_writeb ((char) (skb->len & 0xff), buf);     /* length (16 bit xfer) */
832                 buf += 1;
833                 if (lp->txc) {
834                         isa_writeb ((char)
835                                     (((skb->len >> 8) & 0xff) | XCT), buf);
836                         buf += 1;
837                         isa_writeb (0x04, buf); /* index byte */
838                         buf += 1;
839                         isa_writeb (0x00, (buf + skb->len));    /* Write the XCT flag */
840                         isa_memcpy_toio (buf, skb->data, PRELOAD);      /* Write PRELOAD bytes */
841                         outb (page, EWRK3_TQ);  /* Start sending pkt */
842                         isa_memcpy_toio (buf + PRELOAD,
843                                          skb->data + PRELOAD,
844                                          skb->len - PRELOAD);
845                         isa_writeb (0xff, (buf + skb->len));    /* Write the XCT flag */
846                 } else {
847                         isa_writeb ((char)
848                                     ((skb->len >> 8) & 0xff), buf);
849                         buf += 1;
850                         isa_writeb (0x04, buf); /* index byte */
851                         buf += 1;
852                         isa_memcpy_toio (buf, skb->data, skb->len);     /* Write data bytes */
853                         outb (page, EWRK3_TQ);  /* Start sending pkt */
854                 }
855         }
856
857         ENABLE_IRQs;
858         spin_unlock_irq (&lp->hw_lock);
859
860         lp->stats.tx_bytes += skb->len;
861         dev->trans_start = jiffies;
862         dev_kfree_skb (skb);
863
864         /* Check for free resources: stop Tx queue if there are none */
865         if (inb (EWRK3_FMQC) == 0)
866                 netif_stop_queue (dev);
867
868         return 0;
869
870 err_out:
871         ENABLE_IRQs;
872         spin_unlock_irq (&lp->hw_lock);
873         return 1;
874 }
875
876 /*
877    ** The EWRK3 interrupt handler.
878  */
879 static irqreturn_t ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
880 {
881         struct net_device *dev = dev_id;
882         struct ewrk3_private *lp;
883         u_long iobase;
884         u_char icr, cr, csr;
885
886         lp = (struct ewrk3_private *) dev->priv;
887         iobase = dev->base_addr;
888
889         /* get the interrupt information */
890         csr = inb(EWRK3_CSR);
891
892         /*
893          ** Mask the EWRK3 board interrupts and turn on the LED
894          */
895         spin_lock(&lp->hw_lock);
896         DISABLE_IRQs;
897
898         cr = inb(EWRK3_CR);
899         cr |= lp->led_mask;
900         outb(cr, EWRK3_CR);
901
902         if (csr & CSR_RNE)      /* Rx interrupt (packet[s] arrived) */
903                 ewrk3_rx(dev);
904
905         if (csr & CSR_TNE)      /* Tx interrupt (packet sent) */
906                 ewrk3_tx(dev);
907
908         /*
909          ** Now deal with the TX/RX disable flags. These are set when there
910          ** are no more resources. If resources free up then enable these
911          ** interrupts, otherwise mask them - failure to do this will result
912          ** in the system hanging in an interrupt loop.
913          */
914         if (inb(EWRK3_FMQC)) {  /* any resources available? */
915                 lp->irq_mask |= ICR_TXDM | ICR_RXDM;    /* enable the interrupt source */
916                 csr &= ~(CSR_TXD | CSR_RXD);    /* ensure restart of a stalled TX or RX */
917                 outb(csr, EWRK3_CSR);
918                 netif_wake_queue(dev);
919         } else {
920                 lp->irq_mask &= ~(ICR_TXDM | ICR_RXDM);         /* disable the interrupt source */
921         }
922
923         /* Unmask the EWRK3 board interrupts and turn off the LED */
924         cr &= ~(lp->led_mask);
925         outb(cr, EWRK3_CR);
926         ENABLE_IRQs;
927         spin_unlock(&lp->hw_lock);
928         return IRQ_HANDLED;
929 }
930
931 /* Called with lp->hw_lock held */
932 static int ewrk3_rx(struct net_device *dev)
933 {
934         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
935         u_long iobase = dev->base_addr;
936         int i, status = 0;
937         u_char page;
938         u_long buf = 0;
939
940         while (inb(EWRK3_RQC) && !status) {     /* Whilst there's incoming data */
941                 if ((page = inb(EWRK3_RQ)) < lp->mPage) {       /* Get next entry's buffer page */
942                         /*
943                            ** Set up shared memory window and pointer into the window
944                          */
945                         if (lp->shmem_length == IO_ONLY) {
946                                 outb(page, EWRK3_IOPR);
947                         } else if (lp->shmem_length == SHMEM_2K) {
948                                 buf = lp->shmem_base;
949                                 outb(page, EWRK3_MPR);
950                         } else if (lp->shmem_length == SHMEM_32K) {
951                                 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
952                                 outb((page >> 4), EWRK3_MPR);
953                         } else if (lp->shmem_length == SHMEM_64K) {
954                                 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
955                                 outb((page >> 5), EWRK3_MPR);
956                         } else {
957                                 status = -1;
958                                 printk("%s: Oops - your private data area is hosed!\n", dev->name);
959                         }
960
961                         if (!status) {
962                                 char rx_status;
963                                 int pkt_len;
964
965                                 if (lp->shmem_length == IO_ONLY) {
966                                         rx_status = inb(EWRK3_DATA);
967                                         pkt_len = inb(EWRK3_DATA);
968                                         pkt_len |= ((u_short) inb(EWRK3_DATA) << 8);
969                                 } else {
970                                         rx_status = isa_readb(buf);
971                                         buf += 1;
972                                         pkt_len = isa_readw(buf);
973                                         buf += 3;
974                                 }
975
976                                 if (!(rx_status & R_ROK)) {     /* There was an error. */
977                                         lp->stats.rx_errors++;  /* Update the error stats. */
978                                         if (rx_status & R_DBE)
979                                                 lp->stats.rx_frame_errors++;
980                                         if (rx_status & R_CRC)
981                                                 lp->stats.rx_crc_errors++;
982                                         if (rx_status & R_PLL)
983                                                 lp->stats.rx_fifo_errors++;
984                                 } else {
985                                         struct sk_buff *skb;
986
987                                         if ((skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
988                                                 unsigned char *p;
989                                                 skb->dev = dev;
990                                                 skb_reserve(skb, 2);    /* Align to 16 bytes */
991                                                 p = skb_put(skb, pkt_len);
992
993                                                 if (lp->shmem_length == IO_ONLY) {
994                                                         *p = inb(EWRK3_DATA);   /* dummy read */
995                                                         for (i = 0; i < pkt_len; i++) {
996                                                                 *p++ = inb(EWRK3_DATA);
997                                                         }
998                                                 } else {
999                                                         isa_memcpy_fromio(p, buf, pkt_len);
1000                                                 }
1001
1002                                                 for (i = 1; i < EWRK3_PKT_STAT_SZ - 1; i++) {
1003                                                         if (pkt_len < i * EWRK3_PKT_BIN_SZ) {
1004                                                                 lp->pktStats.bins[i]++;
1005                                                                 i = EWRK3_PKT_STAT_SZ;
1006                                                         }
1007                                                 }
1008                                                 p = skb->data;  /* Look at the dest addr */
1009                                                 if (p[0] & 0x01) {      /* Multicast/Broadcast */
1010                                                         if ((*(s16 *) & p[0] == -1) && (*(s16 *) & p[2] == -1) && (*(s16 *) & p[4] == -1)) {
1011                                                                 lp->pktStats.broadcast++;
1012                                                         } else {
1013                                                                 lp->pktStats.multicast++;
1014                                                         }
1015                                                 } else if ((*(s16 *) & p[0] == *(s16 *) & dev->dev_addr[0]) &&
1016                                                            (*(s16 *) & p[2] == *(s16 *) & dev->dev_addr[2]) &&
1017                                                            (*(s16 *) & p[4] == *(s16 *) & dev->dev_addr[4])) {
1018                                                         lp->pktStats.unicast++;
1019                                                 }
1020                                                 lp->pktStats.bins[0]++;         /* Duplicates stats.rx_packets */
1021                                                 if (lp->pktStats.bins[0] == 0) {        /* Reset counters */
1022                                                         memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1023                                                 }
1024                                                 /*
1025                                                    ** Notify the upper protocol layers that there is another
1026                                                    ** packet to handle
1027                                                  */
1028                                                 skb->protocol = eth_type_trans(skb, dev);
1029                                                 netif_rx(skb);
1030
1031                                                 /*
1032                                                    ** Update stats
1033                                                  */
1034                                                 dev->last_rx = jiffies;
1035                                                 lp->stats.rx_packets++;
1036                                                 lp->stats.rx_bytes += pkt_len;
1037                                         } else {
1038                                                 printk("%s: Insufficient memory; nuking packet.\n", dev->name);
1039                                                 lp->stats.rx_dropped++;         /* Really, deferred. */
1040                                                 break;
1041                                         }
1042                                 }
1043                         }
1044                         /*
1045                            ** Return the received buffer to the free memory queue
1046                          */
1047                         outb(page, EWRK3_FMQ);
1048                 } else {
1049                         printk("ewrk3_rx(): Illegal page number, page %d\n", page);
1050                         printk("ewrk3_rx(): CSR: %02x ICR: %02x FMQC: %02x\n", inb(EWRK3_CSR), inb(EWRK3_ICR), inb(EWRK3_FMQC));
1051                 }
1052         }
1053         return status;
1054 }
1055
1056 /*
1057 ** Buffer sent - check for TX buffer errors.
1058 ** Called with lp->hw_lock held
1059 */
1060 static int ewrk3_tx(struct net_device *dev)
1061 {
1062         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1063         u_long iobase = dev->base_addr;
1064         u_char tx_status;
1065
1066         while ((tx_status = inb(EWRK3_TDQ)) > 0) {      /* Whilst there's old buffers */
1067                 if (tx_status & T_VSTS) {       /* The status is valid */
1068                         if (tx_status & T_TXE) {
1069                                 lp->stats.tx_errors++;
1070                                 if (tx_status & T_NCL)
1071                                         lp->stats.tx_carrier_errors++;
1072                                 if (tx_status & T_LCL)
1073                                         lp->stats.tx_window_errors++;
1074                                 if (tx_status & T_CTU) {
1075                                         if ((tx_status & T_COLL) ^ T_XUR) {
1076                                                 lp->pktStats.tx_underruns++;
1077                                         } else {
1078                                                 lp->pktStats.excessive_underruns++;
1079                                         }
1080                                 } else if (tx_status & T_COLL) {
1081                                         if ((tx_status & T_COLL) ^ T_XCOLL) {
1082                                                 lp->stats.collisions++;
1083                                         } else {
1084                                                 lp->pktStats.excessive_collisions++;
1085                                         }
1086                                 }
1087                         } else {
1088                                 lp->stats.tx_packets++;
1089                         }
1090                 }
1091         }
1092
1093         return 0;
1094 }
1095
1096 static int ewrk3_close(struct net_device *dev)
1097 {
1098         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1099         u_long iobase = dev->base_addr;
1100         u_char icr, csr;
1101
1102         netif_stop_queue(dev);
1103         
1104         if (ewrk3_debug > 1) {
1105                 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1106                        dev->name, inb(EWRK3_CSR));
1107         }
1108         /*
1109            ** We stop the EWRK3 here... mask interrupts and stop TX & RX
1110          */
1111         DISABLE_IRQs;
1112
1113         STOP_EWRK3;
1114
1115         /*
1116            ** Clean out the TX and RX queues here (note that one entry
1117            ** may get added to either the TXD or RX queues if the TX or RX
1118            ** just starts processing a packet before the STOP_EWRK3 command
1119            ** is received. This will be flushed in the ewrk3_open() call).
1120          */
1121         while (inb(EWRK3_TQ));
1122         while (inb(EWRK3_TDQ));
1123         while (inb(EWRK3_RQ));
1124
1125         if (!lp->hard_strapped) {
1126                 free_irq(dev->irq, dev);
1127         }
1128         return 0;
1129 }
1130
1131 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev)
1132 {
1133         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1134
1135         /* Null body since there is no framing error counter */
1136         return &lp->stats;
1137 }
1138
1139 /*
1140    ** Set or clear the multicast filter for this adapter.
1141  */
1142 static void set_multicast_list(struct net_device *dev)
1143 {
1144         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1145         u_long iobase = dev->base_addr;
1146         u_char csr;
1147
1148         csr = inb(EWRK3_CSR);
1149
1150         if (lp->shmem_length == IO_ONLY) {
1151                 lp->mctbl = (char *) PAGE0_HTE;
1152         } else {
1153                 lp->mctbl = (char *) (lp->shmem_base + PAGE0_HTE);
1154         }
1155
1156         csr &= ~(CSR_PME | CSR_MCE);
1157         if (dev->flags & IFF_PROMISC) {         /* set promiscuous mode */
1158                 csr |= CSR_PME;
1159                 outb(csr, EWRK3_CSR);
1160         } else {
1161                 SetMulticastFilter(dev);
1162                 csr |= CSR_MCE;
1163                 outb(csr, EWRK3_CSR);
1164         }
1165 }
1166
1167 /*
1168    ** Calculate the hash code and update the logical address filter
1169    ** from a list of ethernet multicast addresses.
1170    ** Little endian crc one liner from Matt Thomas, DEC.
1171    **
1172    ** Note that when clearing the table, the broadcast bit must remain asserted
1173    ** to receive broadcast messages.
1174  */
1175 static void SetMulticastFilter(struct net_device *dev)
1176 {
1177         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1178         struct dev_mc_list *dmi = dev->mc_list;
1179         u_long iobase = dev->base_addr;
1180         int i;
1181         char *addrs, bit, byte;
1182         short *p = (short *) lp->mctbl;
1183         u16 hashcode;
1184         u32 crc;
1185
1186         spin_lock_irq(&lp->hw_lock);
1187
1188         if (lp->shmem_length == IO_ONLY) {
1189                 outb(0, EWRK3_IOPR);
1190                 outw(EEPROM_OFFSET(lp->mctbl), EWRK3_PIR1);
1191         } else {
1192                 outb(0, EWRK3_MPR);
1193         }
1194
1195         if (dev->flags & IFF_ALLMULTI) {
1196                 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1197                         if (lp->shmem_length == IO_ONLY) {
1198                                 outb(0xff, EWRK3_DATA);
1199                         } else {        /* memset didn't work here */
1200                                 isa_writew(0xffff, (int) p);
1201                                 p++;
1202                                 i++;
1203                         }
1204                 }
1205         } else {
1206                 /* Clear table except for broadcast bit */
1207                 if (lp->shmem_length == IO_ONLY) {
1208                         for (i = 0; i < (HASH_TABLE_LEN >> 4) - 1; i++) {
1209                                 outb(0x00, EWRK3_DATA);
1210                         }
1211                         outb(0x80, EWRK3_DATA);
1212                         i++;    /* insert the broadcast bit */
1213                         for (; i < (HASH_TABLE_LEN >> 3); i++) {
1214                                 outb(0x00, EWRK3_DATA);
1215                         }
1216                 } else {
1217                         isa_memset_io((int) lp->mctbl, 0, (HASH_TABLE_LEN >> 3));
1218                         isa_writeb(0x80, (int) (lp->mctbl + (HASH_TABLE_LEN >> 4) - 1));
1219                 }
1220
1221                 /* Update table */
1222                 for (i = 0; i < dev->mc_count; i++) {   /* for each address in the list */
1223                         addrs = dmi->dmi_addr;
1224                         dmi = dmi->next;
1225                         if ((*addrs & 0x01) == 1) {     /* multicast address? */
1226                                 crc = ether_crc_le(ETH_ALEN, addrs);
1227                                 hashcode = crc & ((1 << 9) - 1);        /* hashcode is 9 LSb of CRC */
1228
1229                                 byte = hashcode >> 3;   /* bit[3-8] -> byte in filter */
1230                                 bit = 1 << (hashcode & 0x07);   /* bit[0-2] -> bit in byte */
1231
1232                                 if (lp->shmem_length == IO_ONLY) {
1233                                         u_char tmp;
1234
1235                                         outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1236                                         tmp = inb(EWRK3_DATA);
1237                                         tmp |= bit;
1238                                         outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1239                                         outb(tmp, EWRK3_DATA);
1240                                 } else {
1241                                         isa_writeb(isa_readb((int)(lp->mctbl + byte)) | bit, (int)(lp->mctbl + byte));
1242                                 }
1243                         }
1244                 }
1245         }
1246
1247         spin_unlock_irq(&lp->hw_lock);
1248 }
1249
1250 /*
1251    ** ISA bus I/O device probe
1252  */
1253 static int __init isa_probe(struct net_device *dev, u_long ioaddr)
1254 {
1255         int i = num_ewrks3s, maxSlots;
1256         int ret = -ENODEV;
1257
1258         u_long iobase;
1259
1260         if (ioaddr >= 0x400)
1261                 goto out;
1262
1263         if (ioaddr == 0) {      /* Autoprobing */
1264                 iobase = EWRK3_IO_BASE;         /* Get the first slot address */
1265                 maxSlots = 24;
1266         } else {                /* Probe a specific location */
1267                 iobase = ioaddr;
1268                 maxSlots = i + 1;
1269         }
1270
1271         for (; (i < maxSlots) && (dev != NULL);
1272              iobase += EWRK3_IOP_INC, i++)
1273         {
1274                 if (request_region(iobase, EWRK3_TOTAL_SIZE, DRV_NAME)) {
1275                         if (DevicePresent(iobase) == 0) {
1276                                 int irq = dev->irq;
1277                                 ret = ewrk3_hw_init(dev, iobase);
1278                                 if (!ret)
1279                                         break;
1280                                 dev->irq = irq;
1281                         }
1282                         release_region(iobase, EWRK3_TOTAL_SIZE);
1283                 }
1284         }
1285  out:
1286
1287         return ret;
1288 }
1289
1290 /*
1291    ** EISA bus I/O device probe. Probe from slot 1 since slot 0 is usually
1292    ** the motherboard.
1293  */
1294 static int __init eisa_probe(struct net_device *dev, u_long ioaddr)
1295 {
1296         int i, maxSlots;
1297         u_long iobase;
1298         int ret = -ENODEV;
1299
1300         if (ioaddr < 0x1000)
1301                 goto out;
1302
1303         if (ioaddr == 0) {      /* Autoprobing */
1304                 iobase = EISA_SLOT_INC;         /* Get the first slot address */
1305                 i = 1;
1306                 maxSlots = MAX_EISA_SLOTS;
1307         } else {                /* Probe a specific location */
1308                 iobase = ioaddr;
1309                 i = (ioaddr >> 12);
1310                 maxSlots = i + 1;
1311         }
1312
1313         for (i = 1; (i < maxSlots) && (dev != NULL); i++, iobase += EISA_SLOT_INC) {
1314                 if (EISA_signature(name, EISA_ID) == 0) {
1315                         if (request_region(iobase, EWRK3_TOTAL_SIZE, DRV_NAME) &&
1316                             DevicePresent(iobase) == 0) {
1317                                 int irq = dev->irq;
1318                                 ret = ewrk3_hw_init(dev, iobase);
1319                                 if (!ret)
1320                                         break;
1321                                 dev->irq = irq;
1322                         }
1323                         release_region(iobase, EWRK3_TOTAL_SIZE);
1324                 }
1325         }
1326
1327  out:
1328         return ret;
1329 }
1330
1331
1332 /*
1333    ** Read the EWRK3 EEPROM using this routine
1334  */
1335 static int Read_EEPROM(u_long iobase, u_char eaddr)
1336 {
1337         int i;
1338
1339         outb((eaddr & 0x3f), EWRK3_PIR1);       /* set up 6 bits of address info */
1340         outb(EEPROM_RD, EWRK3_IOPR);    /* issue read command */
1341         for (i = 0; i < 5000; i++)
1342                 inb(EWRK3_CSR); /* wait 1msec */
1343
1344         return inw(EWRK3_EPROM1);       /* 16 bits data return */
1345 }
1346
1347 /*
1348    ** Write the EWRK3 EEPROM using this routine
1349  */
1350 static int Write_EEPROM(short data, u_long iobase, u_char eaddr)
1351 {
1352         int i;
1353
1354         outb(EEPROM_WR_EN, EWRK3_IOPR);         /* issue write enable command */
1355         for (i = 0; i < 5000; i++)
1356                 inb(EWRK3_CSR); /* wait 1msec */
1357         outw(data, EWRK3_EPROM1);       /* write data to register */
1358         outb((eaddr & 0x3f), EWRK3_PIR1);       /* set up 6 bits of address info */
1359         outb(EEPROM_WR, EWRK3_IOPR);    /* issue write command */
1360         for (i = 0; i < 75000; i++)
1361                 inb(EWRK3_CSR); /* wait 15msec */
1362         outb(EEPROM_WR_DIS, EWRK3_IOPR);        /* issue write disable command */
1363         for (i = 0; i < 5000; i++)
1364                 inb(EWRK3_CSR); /* wait 1msec */
1365
1366         return 0;
1367 }
1368
1369 /*
1370    ** Look for a particular board name in the on-board EEPROM.
1371  */
1372 static void __init EthwrkSignature(char *name, char *eeprom_image)
1373 {
1374         int i;
1375         char *signatures[] = EWRK3_SIGNATURE;
1376
1377         for (i=0; *signatures[i] != '\0'; i++)
1378                 if( !strncmp(eeprom_image+EEPROM_PNAME7, signatures[i], strlen(signatures[i])) )
1379                         break;
1380
1381         if (*signatures[i] != '\0') {
1382                 memcpy(name, eeprom_image+EEPROM_PNAME7, EWRK3_STRLEN);
1383                 name[EWRK3_STRLEN] = '\0';
1384         } else
1385                 name[0] = '\0';
1386
1387         return;
1388 }
1389
1390 /*
1391    ** Look for a special sequence in the Ethernet station address PROM that
1392    ** is common across all EWRK3 products.
1393    **
1394    ** Search the Ethernet address ROM for the signature. Since the ROM address
1395    ** counter can start at an arbitrary point, the search must include the entire
1396    ** probe sequence length plus the (length_of_the_signature - 1).
1397    ** Stop the search IMMEDIATELY after the signature is found so that the
1398    ** PROM address counter is correctly positioned at the start of the
1399    ** ethernet address for later read out.
1400  */
1401
1402 static int __init DevicePresent(u_long iobase)
1403 {
1404         union {
1405                 struct {
1406                         u32 a;
1407                         u32 b;
1408                 } llsig;
1409                 char Sig[sizeof(u32) << 1];
1410         }
1411         dev;
1412         short sigLength;
1413         char data;
1414         int i, j, status = 0;
1415
1416         dev.llsig.a = ETH_PROM_SIG;
1417         dev.llsig.b = ETH_PROM_SIG;
1418         sigLength = sizeof(u32) << 1;
1419
1420         for (i = 0, j = 0; j < sigLength && i < PROBE_LENGTH + sigLength - 1; i++) {
1421                 data = inb(EWRK3_APROM);
1422                 if (dev.Sig[j] == data) {       /* track signature */
1423                         j++;
1424                 } else {        /* lost signature; begin search again */
1425                         if (data == dev.Sig[0]) {
1426                                 j = 1;
1427                         } else {
1428                                 j = 0;
1429                         }
1430                 }
1431         }
1432
1433         if (j != sigLength) {
1434                 status = -ENODEV;       /* search failed */
1435         }
1436         return status;
1437 }
1438
1439 static u_char __init get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType)
1440 {
1441         int i, j, k;
1442         u_short chksum;
1443         u_char crc, lfsr, sd, status = 0;
1444         u_long iobase = dev->base_addr;
1445         u16 tmp;
1446
1447         if (chipType == LeMAC2) {
1448                 for (crc = 0x6a, j = 0; j < ETH_ALEN; j++) {
1449                         sd = dev->dev_addr[j] = eeprom_image[EEPROM_PADDR0 + j];
1450                         outb(dev->dev_addr[j], EWRK3_PAR0 + j);
1451                         for (k = 0; k < 8; k++, sd >>= 1) {
1452                                 lfsr = ((((crc & 0x02) >> 1) ^ (crc & 0x01)) ^ (sd & 0x01)) << 7;
1453                                 crc = (crc >> 1) + lfsr;
1454                         }
1455                 }
1456                 if (crc != eeprom_image[EEPROM_PA_CRC])
1457                         status = -1;
1458         } else {
1459                 for (i = 0, k = 0; i < ETH_ALEN;) {
1460                         k <<= 1;
1461                         if (k > 0xffff)
1462                                 k -= 0xffff;
1463
1464                         k += (u_char) (tmp = inb(EWRK3_APROM));
1465                         dev->dev_addr[i] = (u_char) tmp;
1466                         outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1467                         i++;
1468                         k += (u_short) ((tmp = inb(EWRK3_APROM)) << 8);
1469                         dev->dev_addr[i] = (u_char) tmp;
1470                         outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1471                         i++;
1472
1473                         if (k > 0xffff)
1474                                 k -= 0xffff;
1475                 }
1476                 if (k == 0xffff)
1477                         k = 0;
1478                 chksum = inb(EWRK3_APROM);
1479                 chksum |= (inb(EWRK3_APROM) << 8);
1480                 if (k != chksum)
1481                         status = -1;
1482         }
1483
1484         return status;
1485 }
1486
1487 /*
1488    ** Look for a particular board name in the EISA configuration space
1489  */
1490 static int __init EISA_signature(char *name, s32 eisa_id)
1491 {
1492         u_long i;
1493         char *signatures[] = EWRK3_SIGNATURE;
1494         char ManCode[EWRK3_STRLEN];
1495         union {
1496                 s32 ID;
1497                 char Id[4];
1498         } Eisa;
1499         int status = 0;
1500
1501         *name = '\0';
1502         for (i = 0; i < 4; i++) {
1503                 Eisa.Id[i] = inb(eisa_id + i);
1504         }
1505
1506         ManCode[0] = (((Eisa.Id[0] >> 2) & 0x1f) + 0x40);
1507         ManCode[1] = (((Eisa.Id[1] & 0xe0) >> 5) + ((Eisa.Id[0] & 0x03) << 3) + 0x40);
1508         ManCode[2] = (((Eisa.Id[2] >> 4) & 0x0f) + 0x30);
1509         ManCode[3] = ((Eisa.Id[2] & 0x0f) + 0x30);
1510         ManCode[4] = (((Eisa.Id[3] >> 4) & 0x0f) + 0x30);
1511         ManCode[5] = '\0';
1512
1513         for (i = 0; (*signatures[i] != '\0') && (*name == '\0'); i++) {
1514                 if (strstr(ManCode, signatures[i]) != NULL) {
1515                         strcpy(name, ManCode);
1516                         status = 1;
1517                 }
1518         }
1519
1520         return status;          /* return the device name string */
1521 }
1522
1523 static int ewrk3_ethtool_ioctl(struct net_device *dev, void __user *useraddr)
1524 {
1525         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1526         u_long iobase = dev->base_addr;
1527         u32 ethcmd;
1528
1529         if (get_user(ethcmd, (u32 __user *)useraddr))
1530                 return -EFAULT;
1531
1532         switch (ethcmd) {
1533
1534         /* Get driver info */
1535         case ETHTOOL_GDRVINFO: {
1536                 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1537                 int fwrev = Read_EEPROM(dev->base_addr, EEPROM_REVLVL);
1538
1539                 strcpy(info.driver, DRV_NAME);
1540                 strcpy(info.version, DRV_VERSION);
1541                 sprintf(info.fw_version, "%d", fwrev);
1542                 strcpy(info.bus_info, "N/A");
1543                 info.eedump_len = EEPROM_MAX;
1544                 if (copy_to_user(useraddr, &info, sizeof(info)))
1545                         return -EFAULT;
1546                 return 0;
1547         }
1548
1549         /* Get settings */
1550         case ETHTOOL_GSET: {
1551                 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1552                 u_char cr = inb(EWRK3_CR);
1553
1554                 switch (lp->adapter_name[4]) {
1555                 case '3': /* DE203 */
1556                         ecmd.supported = SUPPORTED_BNC;
1557                         ecmd.port = PORT_BNC;
1558                         break;
1559
1560                 case '4': /* DE204 */
1561                         ecmd.supported = SUPPORTED_TP;
1562                         ecmd.port = PORT_TP;
1563                         break;
1564
1565                 case '5': /* DE205 */
1566                         ecmd.supported = SUPPORTED_TP | SUPPORTED_BNC | SUPPORTED_AUI;
1567                         ecmd.autoneg = !(cr & CR_APD);
1568                         /*
1569                         ** Port is only valid if autoneg is disabled
1570                         ** and even then we don't know if AUI is jumpered.
1571                         */
1572                         if (!ecmd.autoneg)
1573                                 ecmd.port = (cr & CR_PSEL) ? PORT_BNC : PORT_TP;
1574                         break;
1575                 }
1576
1577                 ecmd.supported |= SUPPORTED_10baseT_Half;
1578                 ecmd.speed = SPEED_10;
1579                 ecmd.duplex = DUPLEX_HALF;
1580
1581                 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1582                         return -EFAULT;
1583                 return 0;
1584         }
1585
1586         /* Set settings */
1587         case ETHTOOL_SSET: {
1588                 struct ethtool_cmd ecmd;
1589                 u_char cr;
1590                 u_long flags;
1591
1592                 /* DE205 is the only card with anything to set */
1593                 if (lp->adapter_name[4] != '5')
1594                         return -EOPNOTSUPP;
1595
1596                 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1597                         return -EFAULT;
1598
1599                 /* Sanity-check parameters */
1600                 if (ecmd.speed != SPEED_10)
1601                         return -EINVAL;
1602                 if (ecmd.port != PORT_TP && ecmd.port != PORT_BNC)
1603                         return -EINVAL; /* AUI is not software-selectable */
1604                 if (ecmd.transceiver != XCVR_INTERNAL)
1605                         return -EINVAL;
1606                 if (ecmd.duplex != DUPLEX_HALF)
1607                         return -EINVAL;
1608                 if (ecmd.phy_address != 0)
1609                         return -EINVAL;
1610
1611                 spin_lock_irqsave(&lp->hw_lock, flags);
1612                 cr = inb(EWRK3_CR);
1613
1614                 /* If Autoneg is set, change to Auto Port mode */
1615                 /* Otherwise, disable Auto Port and set port explicitly */
1616                 if (ecmd.autoneg) {
1617                         cr &= ~CR_APD;
1618                 } else {
1619                         cr |= CR_APD;
1620                         if (ecmd.port == PORT_TP)
1621                                 cr &= ~CR_PSEL;         /* Force TP */
1622                         else
1623                                 cr |= CR_PSEL;          /* Force BNC */
1624                 }
1625
1626                 /* Commit the changes */
1627                 outb(cr, EWRK3_CR);
1628
1629                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1630                 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1631                         return -EFAULT;
1632                 return 0;
1633         }
1634
1635         /* Get link status */
1636         case ETHTOOL_GLINK: {
1637                 struct ethtool_value edata = { ETHTOOL_GLINK };
1638                 u_char cmr = inb(EWRK3_CMR);
1639
1640                 /* DE203 has BNC only and link status does not apply */
1641                 if (lp->adapter_name[4] == '3')
1642                         return -EOPNOTSUPP;
1643
1644                 /* On DE204 this is always valid since TP is the only port. */
1645                 /* On DE205 this reflects TP status even if BNC or AUI is selected. */
1646                 edata.data = !(cmr & CMR_LINK);
1647
1648                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1649                         return -EFAULT;
1650                 return 0;
1651         }
1652
1653         /* Blink LED for identification */
1654         case ETHTOOL_PHYS_ID: {
1655                 struct ethtool_value edata;
1656                 u_long flags;
1657                 u_char cr;
1658                 int count;
1659
1660                 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1661                         return -EFAULT;
1662
1663                 /* Toggle LED 4x per second */
1664                 count = edata.data << 2;
1665
1666                 spin_lock_irqsave(&lp->hw_lock, flags);
1667
1668                 /* Bail if a PHYS_ID is already in progress */
1669                 if (lp->led_mask == 0) {
1670                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1671                         return -EBUSY;
1672                 }
1673
1674                 /* Prevent ISR from twiddling the LED */
1675                 lp->led_mask = 0;
1676
1677                 while (count--) {
1678                         /* Toggle the LED */
1679                         cr = inb(EWRK3_CR);
1680                         outb(cr ^ CR_LED, EWRK3_CR);
1681
1682                         /* Wait a little while */
1683                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1684                         set_current_state(TASK_UNINTERRUPTIBLE);
1685                         schedule_timeout(HZ>>2);
1686                         spin_lock_irqsave(&lp->hw_lock, flags);
1687
1688                         /* Exit if we got a signal */
1689                         if (signal_pending(current))
1690                                 break;
1691                 }
1692
1693                 lp->led_mask = CR_LED;
1694                 cr = inb(EWRK3_CR);
1695                 outb(cr & ~CR_LED, EWRK3_CR);
1696                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1697                 return signal_pending(current) ? -ERESTARTSYS : 0;
1698         }
1699
1700         }
1701
1702         return -EOPNOTSUPP;
1703 }
1704
1705 /*
1706    ** Perform IOCTL call functions here. Some are privileged operations and the
1707    ** effective uid is checked in those cases.
1708  */
1709 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1710 {
1711         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1712         struct ewrk3_ioctl *ioc = (struct ewrk3_ioctl *) &rq->ifr_ifru;
1713         u_long iobase = dev->base_addr;
1714         int i, j, status = 0;
1715         u_char csr;
1716         unsigned long flags;
1717         union ewrk3_addr {
1718                 u_char addr[HASH_TABLE_LEN * ETH_ALEN];
1719                 u_short val[(HASH_TABLE_LEN * ETH_ALEN) >> 1];
1720         };
1721         
1722         union ewrk3_addr *tmp;
1723
1724         /* ethtool IOCTLs are handled elsewhere */
1725         if (cmd == SIOCETHTOOL)
1726                 return ewrk3_ethtool_ioctl(dev, rq->ifr_data);
1727
1728         /* Other than ethtool, all we handle are private IOCTLs */
1729         if (cmd != EWRK3IOCTL)
1730                 return -EOPNOTSUPP;
1731
1732         tmp = kmalloc(sizeof(union ewrk3_addr), GFP_KERNEL);
1733         if(tmp==NULL)
1734                 return -ENOMEM;
1735
1736         switch (ioc->cmd) {
1737         case EWRK3_GET_HWADDR:  /* Get the hardware address */
1738                 for (i = 0; i < ETH_ALEN; i++) {
1739                         tmp->addr[i] = dev->dev_addr[i];
1740                 }
1741                 ioc->len = ETH_ALEN;
1742                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1743                         status = -EFAULT;
1744                 break;
1745                 
1746         case EWRK3_SET_HWADDR:  /* Set the hardware address */
1747                 if (capable(CAP_NET_ADMIN)) {
1748                         spin_lock_irqsave(&lp->hw_lock, flags);
1749                         csr = inb(EWRK3_CSR);
1750                         csr |= (CSR_TXD | CSR_RXD);
1751                         outb(csr, EWRK3_CSR);   /* Disable the TX and RX */
1752                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1753
1754                         if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN)) {
1755                                 status = -EFAULT;
1756                                 break;
1757                         }
1758                         spin_lock_irqsave(&lp->hw_lock, flags);
1759                         for (i = 0; i < ETH_ALEN; i++) {
1760                                 dev->dev_addr[i] = tmp->addr[i];
1761                                 outb(tmp->addr[i], EWRK3_PAR0 + i);
1762                         }
1763
1764                         csr = inb(EWRK3_CSR);
1765                         csr &= ~(CSR_TXD | CSR_RXD);    /* Enable the TX and RX */
1766                         outb(csr, EWRK3_CSR);
1767                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1768                 } else {
1769                         status = -EPERM;
1770                 }
1771
1772                 break;
1773         case EWRK3_SET_PROM:    /* Set Promiscuous Mode */
1774                 if (capable(CAP_NET_ADMIN)) {
1775                         spin_lock_irqsave(&lp->hw_lock, flags);
1776                         csr = inb(EWRK3_CSR);
1777                         csr |= CSR_PME;
1778                         csr &= ~CSR_MCE;
1779                         outb(csr, EWRK3_CSR);
1780                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1781                 } else {
1782                         status = -EPERM;
1783                 }
1784
1785                 break;
1786         case EWRK3_CLR_PROM:    /* Clear Promiscuous Mode */
1787                 if (capable(CAP_NET_ADMIN)) {
1788                         spin_lock_irqsave(&lp->hw_lock, flags);
1789                         csr = inb(EWRK3_CSR);
1790                         csr &= ~CSR_PME;
1791                         outb(csr, EWRK3_CSR);
1792                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1793                 } else {
1794                         status = -EPERM;
1795                 }
1796
1797                 break;
1798         case EWRK3_GET_MCA:     /* Get the multicast address table */
1799                 spin_lock_irqsave(&lp->hw_lock, flags);
1800                 if (lp->shmem_length == IO_ONLY) {
1801                         outb(0, EWRK3_IOPR);
1802                         outw(PAGE0_HTE, EWRK3_PIR1);
1803                         for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1804                                 tmp->addr[i] = inb(EWRK3_DATA);
1805                         }
1806                 } else {
1807                         outb(0, EWRK3_MPR);
1808                         isa_memcpy_fromio(tmp->addr, lp->shmem_base + PAGE0_HTE, (HASH_TABLE_LEN >> 3));
1809                 }
1810                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1811
1812                 ioc->len = (HASH_TABLE_LEN >> 3);
1813                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1814                         status = -EFAULT;
1815
1816                 break;
1817         case EWRK3_SET_MCA:     /* Set a multicast address */
1818                 if (capable(CAP_NET_ADMIN)) {
1819                         if (ioc->len > 1024)
1820                         {
1821                                 status = -EINVAL;
1822                                 break;
1823                         }
1824                         if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN * ioc->len)) {
1825                                 status = -EFAULT;
1826                                 break;
1827                         }
1828                         set_multicast_list(dev);
1829                 } else {
1830                         status = -EPERM;
1831                 }
1832
1833                 break;
1834         case EWRK3_CLR_MCA:     /* Clear all multicast addresses */
1835                 if (capable(CAP_NET_ADMIN)) {
1836                         set_multicast_list(dev);
1837                 } else {
1838                         status = -EPERM;
1839                 }
1840
1841                 break;
1842         case EWRK3_MCA_EN:      /* Enable multicast addressing */
1843                 if (capable(CAP_NET_ADMIN)) {
1844                         spin_lock_irqsave(&lp->hw_lock, flags);
1845                         csr = inb(EWRK3_CSR);
1846                         csr |= CSR_MCE;
1847                         csr &= ~CSR_PME;
1848                         outb(csr, EWRK3_CSR);
1849                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1850                 } else {
1851                         status = -EPERM;
1852                 }
1853
1854                 break;
1855         case EWRK3_GET_STATS: { /* Get the driver statistics */
1856                 struct ewrk3_stats *tmp_stats =
1857                         kmalloc(sizeof(lp->pktStats), GFP_KERNEL);
1858                 if (!tmp_stats) {
1859                         status = -ENOMEM;
1860                         break;
1861                 }
1862
1863                 spin_lock_irqsave(&lp->hw_lock, flags);
1864                 memcpy(tmp_stats, &lp->pktStats, sizeof(lp->pktStats));
1865                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1866
1867                 ioc->len = sizeof(lp->pktStats);
1868                 if (copy_to_user(ioc->data, tmp_stats, sizeof(lp->pktStats)))
1869                         status = -EFAULT;
1870                 kfree(tmp_stats);
1871                 break;
1872         }
1873         case EWRK3_CLR_STATS:   /* Zero out the driver statistics */
1874                 if (capable(CAP_NET_ADMIN)) {
1875                         spin_lock_irqsave(&lp->hw_lock, flags);
1876                         memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1877                         spin_unlock_irqrestore(&lp->hw_lock,flags);
1878                 } else {
1879                         status = -EPERM;
1880                 }
1881
1882                 break;
1883         case EWRK3_GET_CSR:     /* Get the CSR Register contents */
1884                 tmp->addr[0] = inb(EWRK3_CSR);
1885                 ioc->len = 1;
1886                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1887                         status = -EFAULT;
1888                 break;
1889         case EWRK3_SET_CSR:     /* Set the CSR Register contents */
1890                 if (capable(CAP_NET_ADMIN)) {
1891                         if (copy_from_user(tmp->addr, ioc->data, 1)) {
1892                                 status = -EFAULT;
1893                                 break;
1894                         }
1895                         outb(tmp->addr[0], EWRK3_CSR);
1896                 } else {
1897                         status = -EPERM;
1898                 }
1899
1900                 break;
1901         case EWRK3_GET_EEPROM:  /* Get the EEPROM contents */
1902                 if (capable(CAP_NET_ADMIN)) {
1903                         for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1904                                 tmp->val[i] = (short) Read_EEPROM(iobase, i);
1905                         }
1906                         i = EEPROM_MAX;
1907                         tmp->addr[i++] = inb(EWRK3_CMR);                /* Config/Management Reg. */
1908                         for (j = 0; j < ETH_ALEN; j++) {
1909                                 tmp->addr[i++] = inb(EWRK3_PAR0 + j);
1910                         }
1911                         ioc->len = EEPROM_MAX + 1 + ETH_ALEN;
1912                         if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1913                                 status = -EFAULT;
1914                 } else {
1915                         status = -EPERM;
1916                 }
1917
1918                 break;
1919         case EWRK3_SET_EEPROM:  /* Set the EEPROM contents */
1920                 if (capable(CAP_NET_ADMIN)) {
1921                         if (copy_from_user(tmp->addr, ioc->data, EEPROM_MAX)) {
1922                                 status = -EFAULT;
1923                                 break;
1924                         }
1925                         for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1926                                 Write_EEPROM(tmp->val[i], iobase, i);
1927                         }
1928                 } else {
1929                         status = -EPERM;
1930                 }
1931
1932                 break;
1933         case EWRK3_GET_CMR:     /* Get the CMR Register contents */
1934                 tmp->addr[0] = inb(EWRK3_CMR);
1935                 ioc->len = 1;
1936                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1937                         status = -EFAULT;
1938                 break;
1939         case EWRK3_SET_TX_CUT_THRU:     /* Set TX cut through mode */
1940                 if (capable(CAP_NET_ADMIN)) {
1941                         lp->txc = 1;
1942                 } else {
1943                         status = -EPERM;
1944                 }
1945
1946                 break;
1947         case EWRK3_CLR_TX_CUT_THRU:     /* Clear TX cut through mode */
1948                 if (capable(CAP_NET_ADMIN)) {
1949                         lp->txc = 0;
1950                 } else {
1951                         status = -EPERM;
1952                 }
1953
1954                 break;
1955         default:
1956                 status = -EOPNOTSUPP;
1957         }
1958         kfree(tmp);
1959         return status;
1960 }
1961
1962 #ifdef MODULE
1963 static struct net_device *ewrk3_devs[MAX_NUM_EWRK3S];
1964 static int ndevs;
1965 static int io[MAX_NUM_EWRK3S+1] = { 0x300, 0, };
1966
1967 /* '21' below should really be 'MAX_NUM_EWRK3S' */
1968 MODULE_PARM(io, "0-21i");
1969 MODULE_PARM(irq, "0-21i");
1970 MODULE_PARM_DESC(io, "EtherWORKS 3 I/O base address(es)");
1971 MODULE_PARM_DESC(irq, "EtherWORKS 3 IRQ number(s)");
1972
1973 static __exit void ewrk3_exit_module(void)
1974 {
1975         int i;
1976
1977         for( i=0; i<ndevs; i++ ) {
1978                 unregister_netdev(ewrk3_devs[i]);
1979                 release_region(ewrk3_devs[i]->base_addr, EWRK3_TOTAL_SIZE);
1980                 free_netdev(ewrk3_devs[i]);
1981                 ewrk3_devs[i] = NULL;
1982         }
1983 }
1984
1985 static __init int ewrk3_init_module(void)
1986 {
1987         int i=0;
1988
1989         while( io[i] && irq[i] ) {
1990                 struct net_device *dev
1991                         = alloc_etherdev(sizeof(struct ewrk3_private));
1992
1993                 if (!dev)
1994                         break;
1995
1996                 if (ewrk3_probe1(dev, io[i], irq[i]) != 0) {
1997                         free_netdev(dev);
1998                         break;
1999                 }
2000
2001                 ewrk3_devs[ndevs++] = dev;
2002                 i++;
2003         }
2004
2005         return ndevs ? 0 : -EIO;
2006 }
2007
2008
2009 /* Hack for breakage in new module stuff */
2010 module_exit(ewrk3_exit_module);
2011 module_init(ewrk3_init_module);
2012 #endif                          /* MODULE */
2013 MODULE_LICENSE("GPL");
2014
2015 \f
2016
2017 /*
2018  * Local variables:
2019  *  compile-command: "gcc -D__KERNEL__ -I/linux/include -Wall -Wstrict-prototypes -fomit-frame-pointer -fno-strength-reduce -malign-loops=2 -malign-jumps=2 -malign-functions=2 -O2 -m486 -c ewrk3.c"
2020  *
2021  *  compile-command: "gcc -D__KERNEL__ -DMODULE -I/linux/include -Wall -Wstrict-prototypes -fomit-frame-pointer -fno-strength-reduce -malign-loops=2 -malign-jumps=2 -malign-functions=2 -O2 -m486 -c ewrk3.c"
2022  * End:
2023  */