patch-2_6_7-vs1_9_1_12
[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 struct net_device * __init ewrk3_probe(int unit)
345 {
346         struct net_device *dev = alloc_etherdev(sizeof(struct ewrk3_private));
347         int err;
348
349         if (!dev)
350                 return ERR_PTR(-ENOMEM);
351
352         if (unit >= 0) {
353                 sprintf(dev->name, "eth%d", unit);
354                 netdev_boot_setup_check(dev);
355         }
356         SET_MODULE_OWNER(dev);
357
358         err = ewrk3_probe1(dev, dev->base_addr, dev->irq);
359         if (err) 
360                 goto out;
361         return dev;
362 out:
363         free_netdev(dev);
364         return ERR_PTR(err);
365         
366 }
367
368 static int __init ewrk3_probe1(struct net_device *dev, u_long iobase, int irq)
369 {
370         int err;
371
372         dev->base_addr = iobase;
373         dev->irq = irq;
374
375         /* Address PROM pattern */
376         err = isa_probe(dev, iobase);
377         if (err != 0) 
378                 err = eisa_probe(dev, iobase);
379
380         if (err)
381                 return err;
382
383         err = register_netdev(dev);
384         if (err)
385                 release_region(dev->base_addr, EWRK3_TOTAL_SIZE);
386
387         return err;
388 }
389
390 static int __init 
391 ewrk3_hw_init(struct net_device *dev, u_long iobase)
392 {
393         struct ewrk3_private *lp;
394         int i, status = 0;
395         u_long mem_start, shmem_length;
396         u_char cr, cmr, icr, nicsr, lemac, hard_strapped = 0;
397         u_char eeprom_image[EEPROM_MAX], chksum, eisa_cr = 0;
398
399         /*
400         ** Stop the EWRK3. Enable the DBR ROM. Disable interrupts and remote boot.
401         ** This also disables the EISA_ENABLE bit in the EISA Control Register.
402          */
403         if (iobase > 0x400)
404                 eisa_cr = inb(EISA_CR);
405         INIT_EWRK3;
406
407         nicsr = inb(EWRK3_CSR);
408
409         icr = inb(EWRK3_ICR);
410         icr &= 0x70;
411         outb(icr, EWRK3_ICR);   /* Disable all the IRQs */
412
413         if (nicsr == (CSR_TXD | CSR_RXD))
414                 return -ENXIO;
415
416
417         /* Check that the EEPROM is alive and well and not living on Pluto... */
418         for (chksum = 0, i = 0; i < EEPROM_MAX; i += 2) {
419                 union {
420                         short val;
421                         char c[2];
422                 } tmp;
423
424                 tmp.val = (short) Read_EEPROM(iobase, (i >> 1));
425                 eeprom_image[i] = tmp.c[0];
426                 eeprom_image[i + 1] = tmp.c[1];
427                 chksum += eeprom_image[i] + eeprom_image[i + 1];
428         }
429
430         if (chksum != 0) {      /* Bad EEPROM Data! */
431                 printk("%s: Device has a bad on-board EEPROM.\n", dev->name);
432                 return -ENXIO;
433         }
434         
435         EthwrkSignature(name, eeprom_image);
436         if (*name == '\0') 
437                 return -ENXIO;
438
439         dev->base_addr = iobase;
440                                 
441         if (iobase > 0x400) {
442                 outb(eisa_cr, EISA_CR);         /* Rewrite the EISA CR */
443         }
444         lemac = eeprom_image[EEPROM_CHIPVER];
445         cmr = inb(EWRK3_CMR);
446         
447         if (((lemac == LeMAC) && ((cmr & CMR_NO_EEPROM) != CMR_NO_EEPROM)) ||
448             ((lemac == LeMAC2) && !(cmr & CMR_HS))) {
449                 printk("%s: %s at %#4lx", dev->name, name, iobase);
450                 hard_strapped = 1;
451         } else if ((iobase & 0x0fff) == EWRK3_EISA_IO_PORTS) {
452                 /* EISA slot address */
453                 printk("%s: %s at %#4lx (EISA slot %ld)",
454                        dev->name, name, iobase, ((iobase >> 12) & 0x0f));
455         } else {        /* ISA port address */
456                 printk("%s: %s at %#4lx", dev->name, name, iobase);
457         }
458
459         printk(", h/w address ");
460         if (lemac != LeMAC2)
461                 DevicePresent(iobase);  /* need after EWRK3_INIT */
462         status = get_hw_addr(dev, eeprom_image, lemac);
463         for (i = 0; i < ETH_ALEN - 1; i++) {    /* get the ethernet addr. */
464                 printk("%2.2x:", dev->dev_addr[i]);
465         }
466         printk("%2.2x,\n", dev->dev_addr[i]);
467         
468         if (status) {
469                 printk("      which has an EEPROM CRC error.\n");
470                 return -ENXIO;
471         }
472
473         if (lemac == LeMAC2) {  /* Special LeMAC2 CMR things */
474                 cmr &= ~(CMR_RA | CMR_WB | CMR_LINK | CMR_POLARITY | CMR_0WS);
475                 if (eeprom_image[EEPROM_MISC0] & READ_AHEAD)
476                         cmr |= CMR_RA;
477                 if (eeprom_image[EEPROM_MISC0] & WRITE_BEHIND)
478                         cmr |= CMR_WB;
479                 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_POL)
480                         cmr |= CMR_POLARITY;
481                 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_LINK)
482                         cmr |= CMR_LINK;
483                 if (eeprom_image[EEPROM_MISC0] & _0WS_ENA)
484                         cmr |= CMR_0WS;
485         }
486         if (eeprom_image[EEPROM_SETUP] & SETUP_DRAM)
487                 cmr |= CMR_DRAM;
488         outb(cmr, EWRK3_CMR);
489         
490         cr = inb(EWRK3_CR);     /* Set up the Control Register */
491         cr |= eeprom_image[EEPROM_SETUP] & SETUP_APD;
492         if (cr & SETUP_APD)
493                 cr |= eeprom_image[EEPROM_SETUP] & SETUP_PS;
494         cr |= eeprom_image[EEPROM_MISC0] & FAST_BUS;
495         cr |= eeprom_image[EEPROM_MISC0] & ENA_16;
496         outb(cr, EWRK3_CR);
497
498         /*
499         ** Determine the base address and window length for the EWRK3
500         ** RAM from the memory base register.
501         */
502         mem_start = inb(EWRK3_MBR);
503         shmem_length = 0;
504         if (mem_start != 0) {
505                 if ((mem_start >= 0x0a) && (mem_start <= 0x0f)) {
506                         mem_start *= SHMEM_64K;
507                         shmem_length = SHMEM_64K;
508                 } else if ((mem_start >= 0x14) && (mem_start <= 0x1f)) {
509                         mem_start *= SHMEM_32K;
510                         shmem_length = SHMEM_32K;
511                 } else if ((mem_start >= 0x40) && (mem_start <= 0xff)) {
512                         mem_start = mem_start * SHMEM_2K + 0x80000;
513                         shmem_length = SHMEM_2K;
514                 } else {
515                         return -ENXIO;
516                 }
517         }
518         /*
519         ** See the top of this source code for comments about
520         ** uncommenting this line.
521         */
522 /*          FORCE_2K_MODE; */
523         
524         if (hard_strapped) {
525                 printk("      is hard strapped.\n");
526         } else if (mem_start) {
527                 printk("      has a %dk RAM window", (int) (shmem_length >> 10));
528                 printk(" at 0x%.5lx", mem_start);
529         } else {
530                 printk("      is in I/O only mode");
531         }
532
533         lp = (struct ewrk3_private *) dev->priv;
534         lp->shmem_base = mem_start;
535         lp->shmem_length = shmem_length;
536         lp->lemac = lemac;
537         lp->hard_strapped = hard_strapped;
538         lp->led_mask = CR_LED;
539         spin_lock_init(&lp->hw_lock);
540         
541         lp->mPage = 64;
542         if (cmr & CMR_DRAM)
543                 lp->mPage <<= 1;        /* 2 DRAMS on module */
544         
545         sprintf(lp->adapter_name, "%s (%s)", name, dev->name);
546         
547         lp->irq_mask = ICR_TNEM | ICR_TXDM | ICR_RNEM | ICR_RXDM;
548         
549         if (!hard_strapped) {
550                 /*
551                 ** Enable EWRK3 board interrupts for autoprobing
552                 */
553                 icr |= ICR_IE;  /* Enable interrupts */
554                 outb(icr, EWRK3_ICR);
555                 
556                 /* The DMA channel may be passed in on this parameter. */
557                 dev->dma = 0;
558                 
559                 /* To auto-IRQ we enable the initialization-done and DMA err,
560                    interrupts. For now we will always get a DMA error. */
561                 if (dev->irq < 2) {
562 #ifndef MODULE
563                         u_char irqnum;
564                         unsigned long irq_mask;
565                         
566
567                         irq_mask = probe_irq_on();
568                         
569                         /*
570                         ** Trigger a TNE interrupt.
571                         */
572                         icr |= ICR_TNEM;
573                         outb(1, EWRK3_TDQ);     /* Write to the TX done queue */
574                         outb(icr, EWRK3_ICR);   /* Unmask the TXD interrupt */
575                         
576                         irqnum = irq[((icr & IRQ_SEL) >> 4)];
577                         
578                         mdelay(20);
579                         dev->irq = probe_irq_off(irq_mask);
580                         if ((dev->irq) && (irqnum == dev->irq)) {
581                                 printk(" and uses IRQ%d.\n", dev->irq);
582                         } else {
583                                 if (!dev->irq) {
584                                         printk(" and failed to detect IRQ line.\n");
585                                 } else if ((irqnum == 1) && (lemac == LeMAC2)) {
586                                         printk(" and an illegal IRQ line detected.\n");
587                                 } else {
588                                         printk(", but incorrect IRQ line detected.\n");
589                                 }
590                                 return -ENXIO;
591                         }
592
593                         DISABLE_IRQs;   /* Mask all interrupts */
594
595 #endif                          /* MODULE */
596                 } else {
597                         printk(" and requires IRQ%d.\n", dev->irq);
598                 }
599         }
600
601         if (ewrk3_debug > 1) {
602                 printk(version);
603         }
604         /* The EWRK3-specific entries in the device structure. */
605         dev->open = ewrk3_open;
606         dev->hard_start_xmit = ewrk3_queue_pkt;
607         dev->stop = ewrk3_close;
608         dev->get_stats = ewrk3_get_stats;
609         dev->set_multicast_list = set_multicast_list;
610         dev->do_ioctl = ewrk3_ioctl;
611         dev->tx_timeout = ewrk3_timeout;
612         dev->watchdog_timeo = QUEUE_PKT_TIMEOUT;
613         
614         dev->mem_start = 0;
615
616         return 0;
617 }
618 \f
619
620 static int ewrk3_open(struct net_device *dev)
621 {
622         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
623         u_long iobase = dev->base_addr;
624         int i, status = 0;
625         u_char icr, csr;
626
627         /*
628            ** Stop the TX and RX...
629          */
630         STOP_EWRK3;
631
632         if (!lp->hard_strapped) {
633                 if (request_irq(dev->irq, (void *) ewrk3_interrupt, 0, "ewrk3", dev)) {
634                         printk("ewrk3_open(): Requested IRQ%d is busy\n", dev->irq);
635                         status = -EAGAIN;
636                 } else {
637
638                         /*
639                            ** Re-initialize the EWRK3...
640                          */
641                         ewrk3_init(dev);
642
643                         if (ewrk3_debug > 1) {
644                                 printk("%s: ewrk3 open with irq %d\n", dev->name, dev->irq);
645                                 printk("  physical address: ");
646                                 for (i = 0; i < 5; i++) {
647                                         printk("%2.2x:", (u_char) dev->dev_addr[i]);
648                                 }
649                                 printk("%2.2x\n", (u_char) dev->dev_addr[i]);
650                                 if (lp->shmem_length == 0) {
651                                         printk("  no shared memory, I/O only mode\n");
652                                 } else {
653                                         printk("  start of shared memory: 0x%08lx\n", lp->shmem_base);
654                                         printk("  window length: 0x%04lx\n", lp->shmem_length);
655                                 }
656                                 printk("  # of DRAMS: %d\n", ((inb(EWRK3_CMR) & 0x02) ? 2 : 1));
657                                 printk("  csr:  0x%02x\n", inb(EWRK3_CSR));
658                                 printk("  cr:   0x%02x\n", inb(EWRK3_CR));
659                                 printk("  icr:  0x%02x\n", inb(EWRK3_ICR));
660                                 printk("  cmr:  0x%02x\n", inb(EWRK3_CMR));
661                                 printk("  fmqc: 0x%02x\n", inb(EWRK3_FMQC));
662                         }
663                         netif_start_queue(dev);
664                         /*
665                            ** Unmask EWRK3 board interrupts
666                          */
667                         icr = inb(EWRK3_ICR);
668                         ENABLE_IRQs;
669
670                 }
671         } else {
672                 printk(KERN_ERR "%s: ewrk3 available for hard strapped set up only.\n", dev->name);
673                 printk(KERN_ERR "      Run the 'ewrk3setup' utility or remove the hard straps.\n");
674                 return -EINVAL;
675         }
676
677         return status;
678 }
679
680 /*
681    ** Initialize the EtherWORKS 3 operating conditions
682  */
683 static void ewrk3_init(struct net_device *dev)
684 {
685         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
686         u_char csr, page;
687         u_long iobase = dev->base_addr;
688         int i;
689
690         /*
691            ** Enable any multicasts
692          */
693         set_multicast_list(dev);
694
695         /*
696         ** Set hardware MAC address. Address is initialized from the EEPROM
697         ** during startup but may have since been changed by the user.
698         */
699         for (i=0; i<ETH_ALEN; i++)
700                 outb(dev->dev_addr[i], EWRK3_PAR0 + i);
701
702         /*
703            ** Clean out any remaining entries in all the queues here
704          */
705         while (inb(EWRK3_TQ));
706         while (inb(EWRK3_TDQ));
707         while (inb(EWRK3_RQ));
708         while (inb(EWRK3_FMQ));
709
710         /*
711            ** Write a clean free memory queue
712          */
713         for (page = 1; page < lp->mPage; page++) {      /* Write the free page numbers */
714                 outb(page, EWRK3_FMQ);  /* to the Free Memory Queue */
715         }
716
717         START_EWRK3;            /* Enable the TX and/or RX */
718 }
719
720 /*
721  *  Transmit timeout
722  */
723  
724 static void ewrk3_timeout(struct net_device *dev)
725 {
726         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
727         u_char icr, csr;
728         u_long iobase = dev->base_addr;
729         
730         if (!lp->hard_strapped) 
731         {
732                 printk(KERN_WARNING"%s: transmit timed/locked out, status %04x, resetting.\n",
733                        dev->name, inb(EWRK3_CSR));
734
735                 /*
736                    ** Mask all board interrupts
737                  */
738                 DISABLE_IRQs;
739
740                 /*
741                    ** Stop the TX and RX...
742                  */
743                 STOP_EWRK3;
744
745                 ewrk3_init(dev);
746
747                 /*
748                    ** Unmask EWRK3 board interrupts
749                  */
750                 ENABLE_IRQs;
751
752                 dev->trans_start = jiffies;
753                 netif_wake_queue(dev);
754         }
755 }
756
757 /*
758    ** Writes a socket buffer to the free page queue
759  */
760 static int ewrk3_queue_pkt (struct sk_buff *skb, struct net_device *dev)
761 {
762         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
763         u_long iobase = dev->base_addr;
764         u_long buf = 0;
765         u_char icr;
766         u_char page;
767
768         spin_lock_irq (&lp->hw_lock);
769         DISABLE_IRQs;
770
771         /* if no resources available, exit, request packet be queued */
772         if (inb (EWRK3_FMQC) == 0) {
773                 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): No free resources...\n",
774                         dev->name);
775                 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): CSR: %02x ICR: %02x FMQC: %02x\n",
776                         dev->name, inb (EWRK3_CSR), inb (EWRK3_ICR),
777                         inb (EWRK3_FMQC));
778                 goto err_out;
779         }
780
781         /*
782          ** Get a free page from the FMQ
783          */
784         if ((page = inb (EWRK3_FMQ)) >= lp->mPage) {
785                 printk ("ewrk3_queue_pkt(): Invalid free memory page (%d).\n",
786                      (u_char) page);
787                 goto err_out;
788         }
789
790
791         /*
792          ** Set up shared memory window and pointer into the window
793          */
794         if (lp->shmem_length == IO_ONLY) {
795                 outb (page, EWRK3_IOPR);
796         } else if (lp->shmem_length == SHMEM_2K) {
797                 buf = lp->shmem_base;
798                 outb (page, EWRK3_MPR);
799         } else if (lp->shmem_length == SHMEM_32K) {
800                 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
801                 outb ((page >> 4), EWRK3_MPR);
802         } else if (lp->shmem_length == SHMEM_64K) {
803                 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
804                 outb ((page >> 5), EWRK3_MPR);
805         } else {
806                 printk (KERN_ERR "%s: Oops - your private data area is hosed!\n",
807                         dev->name);
808                 BUG ();
809         }
810
811         /*
812          ** Set up the buffer control structures and copy the data from
813          ** the socket buffer to the shared memory .
814          */
815         if (lp->shmem_length == IO_ONLY) {
816                 int i;
817                 u_char *p = skb->data;
818                 outb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), EWRK3_DATA);
819                 outb ((char) (skb->len & 0xff), EWRK3_DATA);
820                 outb ((char) ((skb->len >> 8) & 0xff), EWRK3_DATA);
821                 outb ((char) 0x04, EWRK3_DATA);
822                 for (i = 0; i < skb->len; i++) {
823                         outb (*p++, EWRK3_DATA);
824                 }
825                 outb (page, EWRK3_TQ);  /* Start sending pkt */
826         } else {
827                 isa_writeb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), buf);       /* ctrl byte */
828                 buf += 1;
829                 isa_writeb ((char) (skb->len & 0xff), buf);     /* length (16 bit xfer) */
830                 buf += 1;
831                 if (lp->txc) {
832                         isa_writeb ((char)
833                                     (((skb->len >> 8) & 0xff) | XCT), buf);
834                         buf += 1;
835                         isa_writeb (0x04, buf); /* index byte */
836                         buf += 1;
837                         isa_writeb (0x00, (buf + skb->len));    /* Write the XCT flag */
838                         isa_memcpy_toio (buf, skb->data, PRELOAD);      /* Write PRELOAD bytes */
839                         outb (page, EWRK3_TQ);  /* Start sending pkt */
840                         isa_memcpy_toio (buf + PRELOAD,
841                                          skb->data + PRELOAD,
842                                          skb->len - PRELOAD);
843                         isa_writeb (0xff, (buf + skb->len));    /* Write the XCT flag */
844                 } else {
845                         isa_writeb ((char)
846                                     ((skb->len >> 8) & 0xff), buf);
847                         buf += 1;
848                         isa_writeb (0x04, buf); /* index byte */
849                         buf += 1;
850                         isa_memcpy_toio (buf, skb->data, skb->len);     /* Write data bytes */
851                         outb (page, EWRK3_TQ);  /* Start sending pkt */
852                 }
853         }
854
855         ENABLE_IRQs;
856         spin_unlock_irq (&lp->hw_lock);
857
858         lp->stats.tx_bytes += skb->len;
859         dev->trans_start = jiffies;
860         dev_kfree_skb (skb);
861
862         /* Check for free resources: stop Tx queue if there are none */
863         if (inb (EWRK3_FMQC) == 0)
864                 netif_stop_queue (dev);
865
866         return 0;
867
868 err_out:
869         ENABLE_IRQs;
870         spin_unlock_irq (&lp->hw_lock);
871         return 1;
872 }
873
874 /*
875    ** The EWRK3 interrupt handler.
876  */
877 static irqreturn_t ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
878 {
879         struct net_device *dev = dev_id;
880         struct ewrk3_private *lp;
881         u_long iobase;
882         u_char icr, cr, csr;
883
884         lp = (struct ewrk3_private *) dev->priv;
885         iobase = dev->base_addr;
886
887         /* get the interrupt information */
888         csr = inb(EWRK3_CSR);
889
890         /*
891          ** Mask the EWRK3 board interrupts and turn on the LED
892          */
893         spin_lock(&lp->hw_lock);
894         DISABLE_IRQs;
895
896         cr = inb(EWRK3_CR);
897         cr |= lp->led_mask;
898         outb(cr, EWRK3_CR);
899
900         if (csr & CSR_RNE)      /* Rx interrupt (packet[s] arrived) */
901                 ewrk3_rx(dev);
902
903         if (csr & CSR_TNE)      /* Tx interrupt (packet sent) */
904                 ewrk3_tx(dev);
905
906         /*
907          ** Now deal with the TX/RX disable flags. These are set when there
908          ** are no more resources. If resources free up then enable these
909          ** interrupts, otherwise mask them - failure to do this will result
910          ** in the system hanging in an interrupt loop.
911          */
912         if (inb(EWRK3_FMQC)) {  /* any resources available? */
913                 lp->irq_mask |= ICR_TXDM | ICR_RXDM;    /* enable the interrupt source */
914                 csr &= ~(CSR_TXD | CSR_RXD);    /* ensure restart of a stalled TX or RX */
915                 outb(csr, EWRK3_CSR);
916                 netif_wake_queue(dev);
917         } else {
918                 lp->irq_mask &= ~(ICR_TXDM | ICR_RXDM);         /* disable the interrupt source */
919         }
920
921         /* Unmask the EWRK3 board interrupts and turn off the LED */
922         cr &= ~(lp->led_mask);
923         outb(cr, EWRK3_CR);
924         ENABLE_IRQs;
925         spin_unlock(&lp->hw_lock);
926         return IRQ_HANDLED;
927 }
928
929 /* Called with lp->hw_lock held */
930 static int ewrk3_rx(struct net_device *dev)
931 {
932         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
933         u_long iobase = dev->base_addr;
934         int i, status = 0;
935         u_char page;
936         u_long buf = 0;
937
938         while (inb(EWRK3_RQC) && !status) {     /* Whilst there's incoming data */
939                 if ((page = inb(EWRK3_RQ)) < lp->mPage) {       /* Get next entry's buffer page */
940                         /*
941                            ** Set up shared memory window and pointer into the window
942                          */
943                         if (lp->shmem_length == IO_ONLY) {
944                                 outb(page, EWRK3_IOPR);
945                         } else if (lp->shmem_length == SHMEM_2K) {
946                                 buf = lp->shmem_base;
947                                 outb(page, EWRK3_MPR);
948                         } else if (lp->shmem_length == SHMEM_32K) {
949                                 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
950                                 outb((page >> 4), EWRK3_MPR);
951                         } else if (lp->shmem_length == SHMEM_64K) {
952                                 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
953                                 outb((page >> 5), EWRK3_MPR);
954                         } else {
955                                 status = -1;
956                                 printk("%s: Oops - your private data area is hosed!\n", dev->name);
957                         }
958
959                         if (!status) {
960                                 char rx_status;
961                                 int pkt_len;
962
963                                 if (lp->shmem_length == IO_ONLY) {
964                                         rx_status = inb(EWRK3_DATA);
965                                         pkt_len = inb(EWRK3_DATA);
966                                         pkt_len |= ((u_short) inb(EWRK3_DATA) << 8);
967                                 } else {
968                                         rx_status = isa_readb(buf);
969                                         buf += 1;
970                                         pkt_len = isa_readw(buf);
971                                         buf += 3;
972                                 }
973
974                                 if (!(rx_status & R_ROK)) {     /* There was an error. */
975                                         lp->stats.rx_errors++;  /* Update the error stats. */
976                                         if (rx_status & R_DBE)
977                                                 lp->stats.rx_frame_errors++;
978                                         if (rx_status & R_CRC)
979                                                 lp->stats.rx_crc_errors++;
980                                         if (rx_status & R_PLL)
981                                                 lp->stats.rx_fifo_errors++;
982                                 } else {
983                                         struct sk_buff *skb;
984
985                                         if ((skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
986                                                 unsigned char *p;
987                                                 skb->dev = dev;
988                                                 skb_reserve(skb, 2);    /* Align to 16 bytes */
989                                                 p = skb_put(skb, pkt_len);
990
991                                                 if (lp->shmem_length == IO_ONLY) {
992                                                         *p = inb(EWRK3_DATA);   /* dummy read */
993                                                         for (i = 0; i < pkt_len; i++) {
994                                                                 *p++ = inb(EWRK3_DATA);
995                                                         }
996                                                 } else {
997                                                         isa_memcpy_fromio(p, buf, pkt_len);
998                                                 }
999
1000                                                 for (i = 1; i < EWRK3_PKT_STAT_SZ - 1; i++) {
1001                                                         if (pkt_len < i * EWRK3_PKT_BIN_SZ) {
1002                                                                 lp->pktStats.bins[i]++;
1003                                                                 i = EWRK3_PKT_STAT_SZ;
1004                                                         }
1005                                                 }
1006                                                 p = skb->data;  /* Look at the dest addr */
1007                                                 if (p[0] & 0x01) {      /* Multicast/Broadcast */
1008                                                         if ((*(s16 *) & p[0] == -1) && (*(s16 *) & p[2] == -1) && (*(s16 *) & p[4] == -1)) {
1009                                                                 lp->pktStats.broadcast++;
1010                                                         } else {
1011                                                                 lp->pktStats.multicast++;
1012                                                         }
1013                                                 } else if ((*(s16 *) & p[0] == *(s16 *) & dev->dev_addr[0]) &&
1014                                                            (*(s16 *) & p[2] == *(s16 *) & dev->dev_addr[2]) &&
1015                                                            (*(s16 *) & p[4] == *(s16 *) & dev->dev_addr[4])) {
1016                                                         lp->pktStats.unicast++;
1017                                                 }
1018                                                 lp->pktStats.bins[0]++;         /* Duplicates stats.rx_packets */
1019                                                 if (lp->pktStats.bins[0] == 0) {        /* Reset counters */
1020                                                         memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1021                                                 }
1022                                                 /*
1023                                                    ** Notify the upper protocol layers that there is another
1024                                                    ** packet to handle
1025                                                  */
1026                                                 skb->protocol = eth_type_trans(skb, dev);
1027                                                 netif_rx(skb);
1028
1029                                                 /*
1030                                                    ** Update stats
1031                                                  */
1032                                                 dev->last_rx = jiffies;
1033                                                 lp->stats.rx_packets++;
1034                                                 lp->stats.rx_bytes += pkt_len;
1035                                         } else {
1036                                                 printk("%s: Insufficient memory; nuking packet.\n", dev->name);
1037                                                 lp->stats.rx_dropped++;         /* Really, deferred. */
1038                                                 break;
1039                                         }
1040                                 }
1041                         }
1042                         /*
1043                            ** Return the received buffer to the free memory queue
1044                          */
1045                         outb(page, EWRK3_FMQ);
1046                 } else {
1047                         printk("ewrk3_rx(): Illegal page number, page %d\n", page);
1048                         printk("ewrk3_rx(): CSR: %02x ICR: %02x FMQC: %02x\n", inb(EWRK3_CSR), inb(EWRK3_ICR), inb(EWRK3_FMQC));
1049                 }
1050         }
1051         return status;
1052 }
1053
1054 /*
1055 ** Buffer sent - check for TX buffer errors.
1056 ** Called with lp->hw_lock held
1057 */
1058 static int ewrk3_tx(struct net_device *dev)
1059 {
1060         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1061         u_long iobase = dev->base_addr;
1062         u_char tx_status;
1063
1064         while ((tx_status = inb(EWRK3_TDQ)) > 0) {      /* Whilst there's old buffers */
1065                 if (tx_status & T_VSTS) {       /* The status is valid */
1066                         if (tx_status & T_TXE) {
1067                                 lp->stats.tx_errors++;
1068                                 if (tx_status & T_NCL)
1069                                         lp->stats.tx_carrier_errors++;
1070                                 if (tx_status & T_LCL)
1071                                         lp->stats.tx_window_errors++;
1072                                 if (tx_status & T_CTU) {
1073                                         if ((tx_status & T_COLL) ^ T_XUR) {
1074                                                 lp->pktStats.tx_underruns++;
1075                                         } else {
1076                                                 lp->pktStats.excessive_underruns++;
1077                                         }
1078                                 } else if (tx_status & T_COLL) {
1079                                         if ((tx_status & T_COLL) ^ T_XCOLL) {
1080                                                 lp->stats.collisions++;
1081                                         } else {
1082                                                 lp->pktStats.excessive_collisions++;
1083                                         }
1084                                 }
1085                         } else {
1086                                 lp->stats.tx_packets++;
1087                         }
1088                 }
1089         }
1090
1091         return 0;
1092 }
1093
1094 static int ewrk3_close(struct net_device *dev)
1095 {
1096         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1097         u_long iobase = dev->base_addr;
1098         u_char icr, csr;
1099
1100         netif_stop_queue(dev);
1101         
1102         if (ewrk3_debug > 1) {
1103                 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1104                        dev->name, inb(EWRK3_CSR));
1105         }
1106         /*
1107            ** We stop the EWRK3 here... mask interrupts and stop TX & RX
1108          */
1109         DISABLE_IRQs;
1110
1111         STOP_EWRK3;
1112
1113         /*
1114            ** Clean out the TX and RX queues here (note that one entry
1115            ** may get added to either the TXD or RX queues if the TX or RX
1116            ** just starts processing a packet before the STOP_EWRK3 command
1117            ** is received. This will be flushed in the ewrk3_open() call).
1118          */
1119         while (inb(EWRK3_TQ));
1120         while (inb(EWRK3_TDQ));
1121         while (inb(EWRK3_RQ));
1122
1123         if (!lp->hard_strapped) {
1124                 free_irq(dev->irq, dev);
1125         }
1126         return 0;
1127 }
1128
1129 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev)
1130 {
1131         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1132
1133         /* Null body since there is no framing error counter */
1134         return &lp->stats;
1135 }
1136
1137 /*
1138    ** Set or clear the multicast filter for this adapter.
1139  */
1140 static void set_multicast_list(struct net_device *dev)
1141 {
1142         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1143         u_long iobase = dev->base_addr;
1144         u_char csr;
1145
1146         csr = inb(EWRK3_CSR);
1147
1148         if (lp->shmem_length == IO_ONLY) {
1149                 lp->mctbl = (char *) PAGE0_HTE;
1150         } else {
1151                 lp->mctbl = (char *) (lp->shmem_base + PAGE0_HTE);
1152         }
1153
1154         csr &= ~(CSR_PME | CSR_MCE);
1155         if (dev->flags & IFF_PROMISC) {         /* set promiscuous mode */
1156                 csr |= CSR_PME;
1157                 outb(csr, EWRK3_CSR);
1158         } else {
1159                 SetMulticastFilter(dev);
1160                 csr |= CSR_MCE;
1161                 outb(csr, EWRK3_CSR);
1162         }
1163 }
1164
1165 /*
1166    ** Calculate the hash code and update the logical address filter
1167    ** from a list of ethernet multicast addresses.
1168    ** Little endian crc one liner from Matt Thomas, DEC.
1169    **
1170    ** Note that when clearing the table, the broadcast bit must remain asserted
1171    ** to receive broadcast messages.
1172  */
1173 static void SetMulticastFilter(struct net_device *dev)
1174 {
1175         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1176         struct dev_mc_list *dmi = dev->mc_list;
1177         u_long iobase = dev->base_addr;
1178         int i;
1179         char *addrs, bit, byte;
1180         short *p = (short *) lp->mctbl;
1181         u16 hashcode;
1182         u32 crc;
1183
1184         spin_lock_irq(&lp->hw_lock);
1185
1186         if (lp->shmem_length == IO_ONLY) {
1187                 outb(0, EWRK3_IOPR);
1188                 outw(EEPROM_OFFSET(lp->mctbl), EWRK3_PIR1);
1189         } else {
1190                 outb(0, EWRK3_MPR);
1191         }
1192
1193         if (dev->flags & IFF_ALLMULTI) {
1194                 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1195                         if (lp->shmem_length == IO_ONLY) {
1196                                 outb(0xff, EWRK3_DATA);
1197                         } else {        /* memset didn't work here */
1198                                 isa_writew(0xffff, (int) p);
1199                                 p++;
1200                                 i++;
1201                         }
1202                 }
1203         } else {
1204                 /* Clear table except for broadcast bit */
1205                 if (lp->shmem_length == IO_ONLY) {
1206                         for (i = 0; i < (HASH_TABLE_LEN >> 4) - 1; i++) {
1207                                 outb(0x00, EWRK3_DATA);
1208                         }
1209                         outb(0x80, EWRK3_DATA);
1210                         i++;    /* insert the broadcast bit */
1211                         for (; i < (HASH_TABLE_LEN >> 3); i++) {
1212                                 outb(0x00, EWRK3_DATA);
1213                         }
1214                 } else {
1215                         isa_memset_io((int) lp->mctbl, 0, (HASH_TABLE_LEN >> 3));
1216                         isa_writeb(0x80, (int) (lp->mctbl + (HASH_TABLE_LEN >> 4) - 1));
1217                 }
1218
1219                 /* Update table */
1220                 for (i = 0; i < dev->mc_count; i++) {   /* for each address in the list */
1221                         addrs = dmi->dmi_addr;
1222                         dmi = dmi->next;
1223                         if ((*addrs & 0x01) == 1) {     /* multicast address? */
1224                                 crc = ether_crc_le(ETH_ALEN, addrs);
1225                                 hashcode = crc & ((1 << 9) - 1);        /* hashcode is 9 LSb of CRC */
1226
1227                                 byte = hashcode >> 3;   /* bit[3-8] -> byte in filter */
1228                                 bit = 1 << (hashcode & 0x07);   /* bit[0-2] -> bit in byte */
1229
1230                                 if (lp->shmem_length == IO_ONLY) {
1231                                         u_char tmp;
1232
1233                                         outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1234                                         tmp = inb(EWRK3_DATA);
1235                                         tmp |= bit;
1236                                         outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1237                                         outb(tmp, EWRK3_DATA);
1238                                 } else {
1239                                         isa_writeb(isa_readb((int)(lp->mctbl + byte)) | bit, (int)(lp->mctbl + byte));
1240                                 }
1241                         }
1242                 }
1243         }
1244
1245         spin_unlock_irq(&lp->hw_lock);
1246 }
1247
1248 /*
1249    ** ISA bus I/O device probe
1250  */
1251 static int __init isa_probe(struct net_device *dev, u_long ioaddr)
1252 {
1253         int i = num_ewrks3s, maxSlots;
1254         int ret = -ENODEV;
1255
1256         u_long iobase;
1257
1258         if (ioaddr >= 0x400)
1259                 goto out;
1260
1261         if (ioaddr == 0) {      /* Autoprobing */
1262                 iobase = EWRK3_IO_BASE;         /* Get the first slot address */
1263                 maxSlots = 24;
1264         } else {                /* Probe a specific location */
1265                 iobase = ioaddr;
1266                 maxSlots = i + 1;
1267         }
1268
1269         for (; (i < maxSlots) && (dev != NULL);
1270              iobase += EWRK3_IOP_INC, i++)
1271         {
1272                 if (request_region(iobase, EWRK3_TOTAL_SIZE, dev->name)) {
1273                         if (DevicePresent(iobase) == 0) {
1274                                 int irq = dev->irq;
1275                                 ret = ewrk3_hw_init(dev, iobase);
1276                                 if (!ret)
1277                                         break;
1278                                 dev->irq = irq;
1279                         }
1280                         release_region(iobase, EWRK3_TOTAL_SIZE);
1281                 }
1282         }
1283  out:
1284
1285         return ret;
1286 }
1287
1288 /*
1289    ** EISA bus I/O device probe. Probe from slot 1 since slot 0 is usually
1290    ** the motherboard.
1291  */
1292 static int __init eisa_probe(struct net_device *dev, u_long ioaddr)
1293 {
1294         int i, maxSlots;
1295         u_long iobase;
1296         int ret = -ENODEV;
1297
1298         if (ioaddr < 0x1000)
1299                 goto out;
1300
1301         if (ioaddr == 0) {      /* Autoprobing */
1302                 iobase = EISA_SLOT_INC;         /* Get the first slot address */
1303                 i = 1;
1304                 maxSlots = MAX_EISA_SLOTS;
1305         } else {                /* Probe a specific location */
1306                 iobase = ioaddr;
1307                 i = (ioaddr >> 12);
1308                 maxSlots = i + 1;
1309         }
1310
1311         for (i = 1; (i < maxSlots) && (dev != NULL); i++, iobase += EISA_SLOT_INC) {
1312                 if (EISA_signature(name, EISA_ID) == 0) {
1313                         if (request_region(iobase, EWRK3_TOTAL_SIZE, dev->name) &&
1314                             DevicePresent(iobase) == 0) {
1315                                 int irq = dev->irq;
1316                                 ret = ewrk3_hw_init(dev, iobase);
1317                                 if (!ret)
1318                                         break;
1319                                 dev->irq = irq;
1320                         }
1321                         release_region(iobase, EWRK3_TOTAL_SIZE);
1322                 }
1323         }
1324
1325  out:
1326         return ret;
1327 }
1328
1329
1330 /*
1331    ** Read the EWRK3 EEPROM using this routine
1332  */
1333 static int Read_EEPROM(u_long iobase, u_char eaddr)
1334 {
1335         int i;
1336
1337         outb((eaddr & 0x3f), EWRK3_PIR1);       /* set up 6 bits of address info */
1338         outb(EEPROM_RD, EWRK3_IOPR);    /* issue read command */
1339         for (i = 0; i < 5000; i++)
1340                 inb(EWRK3_CSR); /* wait 1msec */
1341
1342         return inw(EWRK3_EPROM1);       /* 16 bits data return */
1343 }
1344
1345 /*
1346    ** Write the EWRK3 EEPROM using this routine
1347  */
1348 static int Write_EEPROM(short data, u_long iobase, u_char eaddr)
1349 {
1350         int i;
1351
1352         outb(EEPROM_WR_EN, EWRK3_IOPR);         /* issue write enable command */
1353         for (i = 0; i < 5000; i++)
1354                 inb(EWRK3_CSR); /* wait 1msec */
1355         outw(data, EWRK3_EPROM1);       /* write data to register */
1356         outb((eaddr & 0x3f), EWRK3_PIR1);       /* set up 6 bits of address info */
1357         outb(EEPROM_WR, EWRK3_IOPR);    /* issue write command */
1358         for (i = 0; i < 75000; i++)
1359                 inb(EWRK3_CSR); /* wait 15msec */
1360         outb(EEPROM_WR_DIS, EWRK3_IOPR);        /* issue write disable command */
1361         for (i = 0; i < 5000; i++)
1362                 inb(EWRK3_CSR); /* wait 1msec */
1363
1364         return 0;
1365 }
1366
1367 /*
1368    ** Look for a particular board name in the on-board EEPROM.
1369  */
1370 static void __init EthwrkSignature(char *name, char *eeprom_image)
1371 {
1372         int i;
1373         char *signatures[] = EWRK3_SIGNATURE;
1374
1375         for (i=0; *signatures[i] != '\0'; i++)
1376                 if( !strncmp(eeprom_image+EEPROM_PNAME7, signatures[i], strlen(signatures[i])) )
1377                         break;
1378
1379         if (*signatures[i] != '\0') {
1380                 memcpy(name, eeprom_image+EEPROM_PNAME7, EWRK3_STRLEN);
1381                 name[EWRK3_STRLEN] = '\0';
1382         } else
1383                 name[0] = '\0';
1384
1385         return;
1386 }
1387
1388 /*
1389    ** Look for a special sequence in the Ethernet station address PROM that
1390    ** is common across all EWRK3 products.
1391    **
1392    ** Search the Ethernet address ROM for the signature. Since the ROM address
1393    ** counter can start at an arbitrary point, the search must include the entire
1394    ** probe sequence length plus the (length_of_the_signature - 1).
1395    ** Stop the search IMMEDIATELY after the signature is found so that the
1396    ** PROM address counter is correctly positioned at the start of the
1397    ** ethernet address for later read out.
1398  */
1399
1400 static int __init DevicePresent(u_long iobase)
1401 {
1402         union {
1403                 struct {
1404                         u32 a;
1405                         u32 b;
1406                 } llsig;
1407                 char Sig[sizeof(u32) << 1];
1408         }
1409         dev;
1410         short sigLength;
1411         char data;
1412         int i, j, status = 0;
1413
1414         dev.llsig.a = ETH_PROM_SIG;
1415         dev.llsig.b = ETH_PROM_SIG;
1416         sigLength = sizeof(u32) << 1;
1417
1418         for (i = 0, j = 0; j < sigLength && i < PROBE_LENGTH + sigLength - 1; i++) {
1419                 data = inb(EWRK3_APROM);
1420                 if (dev.Sig[j] == data) {       /* track signature */
1421                         j++;
1422                 } else {        /* lost signature; begin search again */
1423                         if (data == dev.Sig[0]) {
1424                                 j = 1;
1425                         } else {
1426                                 j = 0;
1427                         }
1428                 }
1429         }
1430
1431         if (j != sigLength) {
1432                 status = -ENODEV;       /* search failed */
1433         }
1434         return status;
1435 }
1436
1437 static u_char __init get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType)
1438 {
1439         int i, j, k;
1440         u_short chksum;
1441         u_char crc, lfsr, sd, status = 0;
1442         u_long iobase = dev->base_addr;
1443         u16 tmp;
1444
1445         if (chipType == LeMAC2) {
1446                 for (crc = 0x6a, j = 0; j < ETH_ALEN; j++) {
1447                         sd = dev->dev_addr[j] = eeprom_image[EEPROM_PADDR0 + j];
1448                         outb(dev->dev_addr[j], EWRK3_PAR0 + j);
1449                         for (k = 0; k < 8; k++, sd >>= 1) {
1450                                 lfsr = ((((crc & 0x02) >> 1) ^ (crc & 0x01)) ^ (sd & 0x01)) << 7;
1451                                 crc = (crc >> 1) + lfsr;
1452                         }
1453                 }
1454                 if (crc != eeprom_image[EEPROM_PA_CRC])
1455                         status = -1;
1456         } else {
1457                 for (i = 0, k = 0; i < ETH_ALEN;) {
1458                         k <<= 1;
1459                         if (k > 0xffff)
1460                                 k -= 0xffff;
1461
1462                         k += (u_char) (tmp = inb(EWRK3_APROM));
1463                         dev->dev_addr[i] = (u_char) tmp;
1464                         outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1465                         i++;
1466                         k += (u_short) ((tmp = inb(EWRK3_APROM)) << 8);
1467                         dev->dev_addr[i] = (u_char) tmp;
1468                         outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1469                         i++;
1470
1471                         if (k > 0xffff)
1472                                 k -= 0xffff;
1473                 }
1474                 if (k == 0xffff)
1475                         k = 0;
1476                 chksum = inb(EWRK3_APROM);
1477                 chksum |= (inb(EWRK3_APROM) << 8);
1478                 if (k != chksum)
1479                         status = -1;
1480         }
1481
1482         return status;
1483 }
1484
1485 /*
1486    ** Look for a particular board name in the EISA configuration space
1487  */
1488 static int __init EISA_signature(char *name, s32 eisa_id)
1489 {
1490         u_long i;
1491         char *signatures[] = EWRK3_SIGNATURE;
1492         char ManCode[EWRK3_STRLEN];
1493         union {
1494                 s32 ID;
1495                 char Id[4];
1496         } Eisa;
1497         int status = 0;
1498
1499         *name = '\0';
1500         for (i = 0; i < 4; i++) {
1501                 Eisa.Id[i] = inb(eisa_id + i);
1502         }
1503
1504         ManCode[0] = (((Eisa.Id[0] >> 2) & 0x1f) + 0x40);
1505         ManCode[1] = (((Eisa.Id[1] & 0xe0) >> 5) + ((Eisa.Id[0] & 0x03) << 3) + 0x40);
1506         ManCode[2] = (((Eisa.Id[2] >> 4) & 0x0f) + 0x30);
1507         ManCode[3] = ((Eisa.Id[2] & 0x0f) + 0x30);
1508         ManCode[4] = (((Eisa.Id[3] >> 4) & 0x0f) + 0x30);
1509         ManCode[5] = '\0';
1510
1511         for (i = 0; (*signatures[i] != '\0') && (*name == '\0'); i++) {
1512                 if (strstr(ManCode, signatures[i]) != NULL) {
1513                         strcpy(name, ManCode);
1514                         status = 1;
1515                 }
1516         }
1517
1518         return status;          /* return the device name string */
1519 }
1520
1521 static int ewrk3_ethtool_ioctl(struct net_device *dev, void __user *useraddr)
1522 {
1523         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1524         u_long iobase = dev->base_addr;
1525         u32 ethcmd;
1526
1527         if (get_user(ethcmd, (u32 __user *)useraddr))
1528                 return -EFAULT;
1529
1530         switch (ethcmd) {
1531
1532         /* Get driver info */
1533         case ETHTOOL_GDRVINFO: {
1534                 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1535                 int fwrev = Read_EEPROM(dev->base_addr, EEPROM_REVLVL);
1536
1537                 strcpy(info.driver, DRV_NAME);
1538                 strcpy(info.version, DRV_VERSION);
1539                 sprintf(info.fw_version, "%d", fwrev);
1540                 strcpy(info.bus_info, "N/A");
1541                 info.eedump_len = EEPROM_MAX;
1542                 if (copy_to_user(useraddr, &info, sizeof(info)))
1543                         return -EFAULT;
1544                 return 0;
1545         }
1546
1547         /* Get settings */
1548         case ETHTOOL_GSET: {
1549                 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1550                 u_char cr = inb(EWRK3_CR);
1551
1552                 switch (lp->adapter_name[4]) {
1553                 case '3': /* DE203 */
1554                         ecmd.supported = SUPPORTED_BNC;
1555                         ecmd.port = PORT_BNC;
1556                         break;
1557
1558                 case '4': /* DE204 */
1559                         ecmd.supported = SUPPORTED_TP;
1560                         ecmd.port = PORT_TP;
1561                         break;
1562
1563                 case '5': /* DE205 */
1564                         ecmd.supported = SUPPORTED_TP | SUPPORTED_BNC | SUPPORTED_AUI;
1565                         ecmd.autoneg = !(cr & CR_APD);
1566                         /*
1567                         ** Port is only valid if autoneg is disabled
1568                         ** and even then we don't know if AUI is jumpered.
1569                         */
1570                         if (!ecmd.autoneg)
1571                                 ecmd.port = (cr & CR_PSEL) ? PORT_BNC : PORT_TP;
1572                         break;
1573                 }
1574
1575                 ecmd.supported |= SUPPORTED_10baseT_Half;
1576                 ecmd.speed = SPEED_10;
1577                 ecmd.duplex = DUPLEX_HALF;
1578
1579                 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1580                         return -EFAULT;
1581                 return 0;
1582         }
1583
1584         /* Set settings */
1585         case ETHTOOL_SSET: {
1586                 struct ethtool_cmd ecmd;
1587                 u_char cr;
1588                 u_long flags;
1589
1590                 /* DE205 is the only card with anything to set */
1591                 if (lp->adapter_name[4] != '5')
1592                         return -EOPNOTSUPP;
1593
1594                 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1595                         return -EFAULT;
1596
1597                 /* Sanity-check parameters */
1598                 if (ecmd.speed != SPEED_10)
1599                         return -EINVAL;
1600                 if (ecmd.port != PORT_TP && ecmd.port != PORT_BNC)
1601                         return -EINVAL; /* AUI is not software-selectable */
1602                 if (ecmd.transceiver != XCVR_INTERNAL)
1603                         return -EINVAL;
1604                 if (ecmd.duplex != DUPLEX_HALF)
1605                         return -EINVAL;
1606                 if (ecmd.phy_address != 0)
1607                         return -EINVAL;
1608
1609                 spin_lock_irqsave(&lp->hw_lock, flags);
1610                 cr = inb(EWRK3_CR);
1611
1612                 /* If Autoneg is set, change to Auto Port mode */
1613                 /* Otherwise, disable Auto Port and set port explicitly */
1614                 if (ecmd.autoneg) {
1615                         cr &= ~CR_APD;
1616                 } else {
1617                         cr |= CR_APD;
1618                         if (ecmd.port == PORT_TP)
1619                                 cr &= ~CR_PSEL;         /* Force TP */
1620                         else
1621                                 cr |= CR_PSEL;          /* Force BNC */
1622                 }
1623
1624                 /* Commit the changes */
1625                 outb(cr, EWRK3_CR);
1626
1627                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1628                 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1629                         return -EFAULT;
1630                 return 0;
1631         }
1632
1633         /* Get link status */
1634         case ETHTOOL_GLINK: {
1635                 struct ethtool_value edata = { ETHTOOL_GLINK };
1636                 u_char cmr = inb(EWRK3_CMR);
1637
1638                 /* DE203 has BNC only and link status does not apply */
1639                 if (lp->adapter_name[4] == '3')
1640                         return -EOPNOTSUPP;
1641
1642                 /* On DE204 this is always valid since TP is the only port. */
1643                 /* On DE205 this reflects TP status even if BNC or AUI is selected. */
1644                 edata.data = !(cmr & CMR_LINK);
1645
1646                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1647                         return -EFAULT;
1648                 return 0;
1649         }
1650
1651         /* Blink LED for identification */
1652         case ETHTOOL_PHYS_ID: {
1653                 struct ethtool_value edata;
1654                 u_long flags;
1655                 u_char cr;
1656                 int count;
1657
1658                 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1659                         return -EFAULT;
1660
1661                 /* Toggle LED 4x per second */
1662                 count = edata.data << 2;
1663
1664                 spin_lock_irqsave(&lp->hw_lock, flags);
1665
1666                 /* Bail if a PHYS_ID is already in progress */
1667                 if (lp->led_mask == 0) {
1668                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1669                         return -EBUSY;
1670                 }
1671
1672                 /* Prevent ISR from twiddling the LED */
1673                 lp->led_mask = 0;
1674
1675                 while (count--) {
1676                         /* Toggle the LED */
1677                         cr = inb(EWRK3_CR);
1678                         outb(cr ^ CR_LED, EWRK3_CR);
1679
1680                         /* Wait a little while */
1681                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1682                         set_current_state(TASK_UNINTERRUPTIBLE);
1683                         schedule_timeout(HZ>>2);
1684                         spin_lock_irqsave(&lp->hw_lock, flags);
1685
1686                         /* Exit if we got a signal */
1687                         if (signal_pending(current))
1688                                 break;
1689                 }
1690
1691                 lp->led_mask = CR_LED;
1692                 cr = inb(EWRK3_CR);
1693                 outb(cr & ~CR_LED, EWRK3_CR);
1694                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1695                 return signal_pending(current) ? -ERESTARTSYS : 0;
1696         }
1697
1698         }
1699
1700         return -EOPNOTSUPP;
1701 }
1702
1703 /*
1704    ** Perform IOCTL call functions here. Some are privileged operations and the
1705    ** effective uid is checked in those cases.
1706  */
1707 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1708 {
1709         struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1710         struct ewrk3_ioctl *ioc = (struct ewrk3_ioctl *) &rq->ifr_ifru;
1711         u_long iobase = dev->base_addr;
1712         int i, j, status = 0;
1713         u_char csr;
1714         unsigned long flags;
1715         union ewrk3_addr {
1716                 u_char addr[HASH_TABLE_LEN * ETH_ALEN];
1717                 u_short val[(HASH_TABLE_LEN * ETH_ALEN) >> 1];
1718         };
1719         
1720         union ewrk3_addr *tmp;
1721
1722         /* ethtool IOCTLs are handled elsewhere */
1723         if (cmd == SIOCETHTOOL)
1724                 return ewrk3_ethtool_ioctl(dev, rq->ifr_data);
1725
1726         /* Other than ethtool, all we handle are private IOCTLs */
1727         if (cmd != EWRK3IOCTL)
1728                 return -EOPNOTSUPP;
1729
1730         tmp = kmalloc(sizeof(union ewrk3_addr), GFP_KERNEL);
1731         if(tmp==NULL)
1732                 return -ENOMEM;
1733
1734         switch (ioc->cmd) {
1735         case EWRK3_GET_HWADDR:  /* Get the hardware address */
1736                 for (i = 0; i < ETH_ALEN; i++) {
1737                         tmp->addr[i] = dev->dev_addr[i];
1738                 }
1739                 ioc->len = ETH_ALEN;
1740                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1741                         status = -EFAULT;
1742                 break;
1743                 
1744         case EWRK3_SET_HWADDR:  /* Set the hardware address */
1745                 if (capable(CAP_NET_ADMIN)) {
1746                         spin_lock_irqsave(&lp->hw_lock, flags);
1747                         csr = inb(EWRK3_CSR);
1748                         csr |= (CSR_TXD | CSR_RXD);
1749                         outb(csr, EWRK3_CSR);   /* Disable the TX and RX */
1750                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1751
1752                         if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN)) {
1753                                 status = -EFAULT;
1754                                 break;
1755                         }
1756                         spin_lock_irqsave(&lp->hw_lock, flags);
1757                         for (i = 0; i < ETH_ALEN; i++) {
1758                                 dev->dev_addr[i] = tmp->addr[i];
1759                                 outb(tmp->addr[i], EWRK3_PAR0 + i);
1760                         }
1761
1762                         csr = inb(EWRK3_CSR);
1763                         csr &= ~(CSR_TXD | CSR_RXD);    /* Enable the TX and RX */
1764                         outb(csr, EWRK3_CSR);
1765                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1766                 } else {
1767                         status = -EPERM;
1768                 }
1769
1770                 break;
1771         case EWRK3_SET_PROM:    /* Set Promiscuous Mode */
1772                 if (capable(CAP_NET_ADMIN)) {
1773                         spin_lock_irqsave(&lp->hw_lock, flags);
1774                         csr = inb(EWRK3_CSR);
1775                         csr |= CSR_PME;
1776                         csr &= ~CSR_MCE;
1777                         outb(csr, EWRK3_CSR);
1778                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1779                 } else {
1780                         status = -EPERM;
1781                 }
1782
1783                 break;
1784         case EWRK3_CLR_PROM:    /* Clear Promiscuous Mode */
1785                 if (capable(CAP_NET_ADMIN)) {
1786                         spin_lock_irqsave(&lp->hw_lock, flags);
1787                         csr = inb(EWRK3_CSR);
1788                         csr &= ~CSR_PME;
1789                         outb(csr, EWRK3_CSR);
1790                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1791                 } else {
1792                         status = -EPERM;
1793                 }
1794
1795                 break;
1796         case EWRK3_GET_MCA:     /* Get the multicast address table */
1797                 spin_lock_irqsave(&lp->hw_lock, flags);
1798                 if (lp->shmem_length == IO_ONLY) {
1799                         outb(0, EWRK3_IOPR);
1800                         outw(PAGE0_HTE, EWRK3_PIR1);
1801                         for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1802                                 tmp->addr[i] = inb(EWRK3_DATA);
1803                         }
1804                 } else {
1805                         outb(0, EWRK3_MPR);
1806                         isa_memcpy_fromio(tmp->addr, lp->shmem_base + PAGE0_HTE, (HASH_TABLE_LEN >> 3));
1807                 }
1808                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1809
1810                 ioc->len = (HASH_TABLE_LEN >> 3);
1811                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1812                         status = -EFAULT;
1813
1814                 break;
1815         case EWRK3_SET_MCA:     /* Set a multicast address */
1816                 if (capable(CAP_NET_ADMIN)) {
1817                         if (ioc->len > 1024)
1818                         {
1819                                 status = -EINVAL;
1820                                 break;
1821                         }
1822                         if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN * ioc->len)) {
1823                                 status = -EFAULT;
1824                                 break;
1825                         }
1826                         set_multicast_list(dev);
1827                 } else {
1828                         status = -EPERM;
1829                 }
1830
1831                 break;
1832         case EWRK3_CLR_MCA:     /* Clear all multicast addresses */
1833                 if (capable(CAP_NET_ADMIN)) {
1834                         set_multicast_list(dev);
1835                 } else {
1836                         status = -EPERM;
1837                 }
1838
1839                 break;
1840         case EWRK3_MCA_EN:      /* Enable multicast addressing */
1841                 if (capable(CAP_NET_ADMIN)) {
1842                         spin_lock_irqsave(&lp->hw_lock, flags);
1843                         csr = inb(EWRK3_CSR);
1844                         csr |= CSR_MCE;
1845                         csr &= ~CSR_PME;
1846                         outb(csr, EWRK3_CSR);
1847                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1848                 } else {
1849                         status = -EPERM;
1850                 }
1851
1852                 break;
1853         case EWRK3_GET_STATS: { /* Get the driver statistics */
1854                 struct ewrk3_stats *tmp_stats =
1855                         kmalloc(sizeof(lp->pktStats), GFP_KERNEL);
1856                 if (!tmp_stats) {
1857                         status = -ENOMEM;
1858                         break;
1859                 }
1860
1861                 spin_lock_irqsave(&lp->hw_lock, flags);
1862                 memcpy(tmp_stats, &lp->pktStats, sizeof(lp->pktStats));
1863                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1864
1865                 ioc->len = sizeof(lp->pktStats);
1866                 if (copy_to_user(ioc->data, tmp_stats, sizeof(lp->pktStats)))
1867                         status = -EFAULT;
1868                 kfree(tmp_stats);
1869                 break;
1870         }
1871         case EWRK3_CLR_STATS:   /* Zero out the driver statistics */
1872                 if (capable(CAP_NET_ADMIN)) {
1873                         spin_lock_irqsave(&lp->hw_lock, flags);
1874                         memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1875                         spin_unlock_irqrestore(&lp->hw_lock,flags);
1876                 } else {
1877                         status = -EPERM;
1878                 }
1879
1880                 break;
1881         case EWRK3_GET_CSR:     /* Get the CSR Register contents */
1882                 tmp->addr[0] = inb(EWRK3_CSR);
1883                 ioc->len = 1;
1884                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1885                         status = -EFAULT;
1886                 break;
1887         case EWRK3_SET_CSR:     /* Set the CSR Register contents */
1888                 if (capable(CAP_NET_ADMIN)) {
1889                         if (copy_from_user(tmp->addr, ioc->data, 1)) {
1890                                 status = -EFAULT;
1891                                 break;
1892                         }
1893                         outb(tmp->addr[0], EWRK3_CSR);
1894                 } else {
1895                         status = -EPERM;
1896                 }
1897
1898                 break;
1899         case EWRK3_GET_EEPROM:  /* Get the EEPROM contents */
1900                 if (capable(CAP_NET_ADMIN)) {
1901                         for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1902                                 tmp->val[i] = (short) Read_EEPROM(iobase, i);
1903                         }
1904                         i = EEPROM_MAX;
1905                         tmp->addr[i++] = inb(EWRK3_CMR);                /* Config/Management Reg. */
1906                         for (j = 0; j < ETH_ALEN; j++) {
1907                                 tmp->addr[i++] = inb(EWRK3_PAR0 + j);
1908                         }
1909                         ioc->len = EEPROM_MAX + 1 + ETH_ALEN;
1910                         if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1911                                 status = -EFAULT;
1912                 } else {
1913                         status = -EPERM;
1914                 }
1915
1916                 break;
1917         case EWRK3_SET_EEPROM:  /* Set the EEPROM contents */
1918                 if (capable(CAP_NET_ADMIN)) {
1919                         if (copy_from_user(tmp->addr, ioc->data, EEPROM_MAX)) {
1920                                 status = -EFAULT;
1921                                 break;
1922                         }
1923                         for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1924                                 Write_EEPROM(tmp->val[i], iobase, i);
1925                         }
1926                 } else {
1927                         status = -EPERM;
1928                 }
1929
1930                 break;
1931         case EWRK3_GET_CMR:     /* Get the CMR Register contents */
1932                 tmp->addr[0] = inb(EWRK3_CMR);
1933                 ioc->len = 1;
1934                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1935                         status = -EFAULT;
1936                 break;
1937         case EWRK3_SET_TX_CUT_THRU:     /* Set TX cut through mode */
1938                 if (capable(CAP_NET_ADMIN)) {
1939                         lp->txc = 1;
1940                 } else {
1941                         status = -EPERM;
1942                 }
1943
1944                 break;
1945         case EWRK3_CLR_TX_CUT_THRU:     /* Clear TX cut through mode */
1946                 if (capable(CAP_NET_ADMIN)) {
1947                         lp->txc = 0;
1948                 } else {
1949                         status = -EPERM;
1950                 }
1951
1952                 break;
1953         default:
1954                 status = -EOPNOTSUPP;
1955         }
1956         kfree(tmp);
1957         return status;
1958 }
1959
1960 #ifdef MODULE
1961 static struct net_device *ewrk3_devs[MAX_NUM_EWRK3S];
1962 static int ndevs;
1963 static int io[MAX_NUM_EWRK3S+1] = { 0x300, 0, };
1964
1965 /* '21' below should really be 'MAX_NUM_EWRK3S' */
1966 MODULE_PARM(io, "0-21i");
1967 MODULE_PARM(irq, "0-21i");
1968 MODULE_PARM_DESC(io, "EtherWORKS 3 I/O base address(es)");
1969 MODULE_PARM_DESC(irq, "EtherWORKS 3 IRQ number(s)");
1970
1971 static __exit void ewrk3_exit_module(void)
1972 {
1973         int i;
1974
1975         for( i=0; i<ndevs; i++ ) {
1976                 unregister_netdev(ewrk3_devs[i]);
1977                 release_region(ewrk3_devs[i]->base_addr, EWRK3_TOTAL_SIZE);
1978                 free_netdev(ewrk3_devs[i]);
1979                 ewrk3_devs[i] = NULL;
1980         }
1981 }
1982
1983 static __init int ewrk3_init_module(void)
1984 {
1985         int i=0;
1986
1987         while( io[i] && irq[i] ) {
1988                 struct net_device *dev
1989                         = alloc_etherdev(sizeof(struct ewrk3_private));
1990
1991                 if (!dev)
1992                         break;
1993
1994                 if (ewrk3_probe1(dev, io[i], irq[i]) != 0) {
1995                         free_netdev(dev);
1996                         break;
1997                 }
1998
1999                 ewrk3_devs[ndevs++] = dev;
2000                 i++;
2001         }
2002
2003         return ndevs ? 0 : -EIO;
2004 }
2005
2006
2007 /* Hack for breakage in new module stuff */
2008 module_exit(ewrk3_exit_module);
2009 module_init(ewrk3_init_module);
2010 #endif                          /* MODULE */
2011 MODULE_LICENSE("GPL");
2012
2013 \f
2014
2015 /*
2016  * Local variables:
2017  *  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"
2018  *
2019  *  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"
2020  * End:
2021  */