Import 2.3.46pre3
[davej-history.git] / drivers / net / ewrk3.c
blob90c82b341ccef2b7979aaa418e6c21bc08692ff2
1 /* ewrk3.c: A DIGITAL EtherWORKS 3 ethernet driver for Linux.
3 Written 1994 by David C. Davies.
5 Copyright 1994 Digital Equipment Corporation.
7 This software may be used and distributed according to the terms of
8 the GNU Public License, incorporated herein by reference.
10 This driver is written for the Digital Equipment Corporation series
11 of EtherWORKS ethernet cards:
13 DE203 Turbo (BNC)
14 DE204 Turbo (TP)
15 DE205 Turbo (TP BNC)
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.
21 The author may be reached at davies@maniac.ultranet.com.
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.
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.
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.
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:
44 FORCE_2K_MODE;
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.
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) ...
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}.
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).
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:
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 6) run the net startup bits for your new eth?? interface manually
80 (usually /etc/rc.inet[12] at boot time).
81 7) enjoy!
83 Note that autoprobing is not allowed in loadable modules - the system is
84 already up and running and you're messing with interrupts.
86 To unload a module, turn off the associated interface
87 'ifconfig eth?? down' then 'rmmod ewrk3'.
89 Promiscuous mode has been turned off in this driver, but all the
90 multicast address bits have been turned on. This improved the send
91 performance on a busy network by about 13%.
93 Ioctl's have now been provided (primarily because I wanted to grab some
94 packet size statistics). They are patterned after 'plipconfig.c' from a
95 suggestion by Alan Cox. Using these ioctls, you can enable promiscuous
96 mode, add/delete multicast addresses, change the hardware address, get
97 packet size distribution statistics and muck around with the control and
98 status register. I'll add others if and when the need arises.
100 TO DO:
101 ------
104 Revision History
105 ----------------
107 Version Date Description
109 0.1 26-aug-94 Initial writing. ALPHA code release.
110 0.11 31-aug-94 Fixed: 2k mode memory base calc.,
111 LeMAC version calc.,
112 IRQ vector assignments during autoprobe.
113 0.12 31-aug-94 Tested working on LeMAC2 (DE20[345]-AC) card.
114 Fixed up MCA hash table algorithm.
115 0.20 4-sep-94 Added IOCTL functionality.
116 0.21 14-sep-94 Added I/O mode.
117 0.21axp 15-sep-94 Special version for ALPHA AXP Linux V1.0.
118 0.22 16-sep-94 Added more IOCTLs & tidied up.
119 0.23 21-sep-94 Added transmit cut through.
120 0.24 31-oct-94 Added uid checks in some ioctls.
121 0.30 1-nov-94 BETA code release.
122 0.31 5-dec-94 Added check/allocate region code.
123 0.32 16-jan-95 Broadcast packet fix.
124 0.33 10-Feb-95 Fix recognition bug reported by <bkm@star.rl.ac.uk>.
125 0.40 27-Dec-95 Rationalise MODULE and autoprobe code.
126 Rewrite for portability & updated.
127 ALPHA support from <jestabro@amt.tay1.dec.com>
128 Added verify_area() calls in ewrk3_ioctl() from
129 suggestion by <heiko@colossus.escape.de>.
130 Add new multicasting code.
131 0.41 20-Jan-96 Fix IRQ set up problem reported by
132 <kenneth@bbs.sas.ntu.ac.sg>.
133 0.42 22-Apr-96 Fix alloc_device() bug <jari@markkus2.fimr.fi>
134 0.43 16-Aug-96 Update alloc_device() to conform to de4x5.c
136 =========================================================================
139 static const char *version = "ewrk3.c:v0.43 96/8/16 davies@maniac.ultranet.com\n";
141 #include <linux/config.h>
142 #include <linux/module.h>
144 #include <linux/kernel.h>
145 #include <linux/sched.h>
146 #include <linux/string.h>
147 #include <linux/ptrace.h>
148 #include <linux/errno.h>
149 #include <linux/ioport.h>
150 #include <linux/malloc.h>
151 #include <linux/interrupt.h>
152 #include <linux/delay.h>
153 #include <linux/init.h>
154 #include <asm/bitops.h>
155 #include <asm/io.h>
156 #include <asm/dma.h>
157 #include <asm/uaccess.h>
159 #include <linux/netdevice.h>
160 #include <linux/etherdevice.h>
161 #include <linux/skbuff.h>
163 #include <linux/time.h>
164 #include <linux/types.h>
165 #include <linux/unistd.h>
166 #include <linux/ctype.h>
168 #include "ewrk3.h"
170 #ifdef EWRK3_DEBUG
171 static int ewrk3_debug = EWRK3_DEBUG;
172 #else
173 static int ewrk3_debug = 1;
174 #endif
176 #define EWRK3_NDA 0xffe0 /* No Device Address */
178 #define PROBE_LENGTH 32
179 #define ETH_PROM_SIG 0xAA5500FFUL
181 #ifndef EWRK3_SIGNATURE
182 #define EWRK3_SIGNATURE {"DE203","DE204","DE205",""}
183 #define EWRK3_STRLEN 8
184 #endif
186 #ifndef EWRK3_RAM_BASE_ADDRESSES
187 #define EWRK3_RAM_BASE_ADDRESSES {0xc0000,0xd0000,0x00000}
188 #endif
191 ** Sets up the I/O area for the autoprobe.
193 #define EWRK3_IO_BASE 0x100 /* Start address for probe search */
194 #define EWRK3_IOP_INC 0x20 /* I/O address increment */
195 #define EWRK3_TOTAL_SIZE 0x20 /* required I/O address length */
197 #ifndef MAX_NUM_EWRK3S
198 #define MAX_NUM_EWRK3S 21
199 #endif
201 #ifndef EWRK3_EISA_IO_PORTS
202 #define EWRK3_EISA_IO_PORTS 0x0c00 /* I/O port base address, slot 0 */
203 #endif
205 #ifndef MAX_EISA_SLOTS
206 #define MAX_EISA_SLOTS 16
207 #define EISA_SLOT_INC 0x1000
208 #endif
210 #define CRC_POLYNOMIAL_BE 0x04c11db7UL /* Ethernet CRC, big endian */
211 #define CRC_POLYNOMIAL_LE 0xedb88320UL /* Ethernet CRC, little endian */
213 #define QUEUE_PKT_TIMEOUT (1*HZ) /* Jiffies */
216 ** EtherWORKS 3 shared memory window sizes
218 #define IO_ONLY 0x00
219 #define SHMEM_2K 0x800
220 #define SHMEM_32K 0x8000
221 #define SHMEM_64K 0x10000
224 ** EtherWORKS 3 IRQ ENABLE/DISABLE
226 #define ENABLE_IRQs { \
227 icr |= lp->irq_mask;\
228 outb(icr, EWRK3_ICR); /* Enable the IRQs */\
231 #define DISABLE_IRQs { \
232 icr = inb(EWRK3_ICR);\
233 icr &= ~lp->irq_mask;\
234 outb(icr, EWRK3_ICR); /* Disable the IRQs */\
238 ** EtherWORKS 3 START/STOP
240 #define START_EWRK3 { \
241 csr = inb(EWRK3_CSR);\
242 csr &= ~(CSR_TXD|CSR_RXD);\
243 outb(csr, EWRK3_CSR); /* Enable the TX and/or RX */\
246 #define STOP_EWRK3 { \
247 csr = (CSR_TXD|CSR_RXD);\
248 outb(csr, EWRK3_CSR); /* Disable the TX and/or RX */\
252 ** The EtherWORKS 3 private structure
254 #define EWRK3_PKT_STAT_SZ 16
255 #define EWRK3_PKT_BIN_SZ 128 /* Should be >=100 unless you
256 increase EWRK3_PKT_STAT_SZ */
258 struct ewrk3_private {
259 char adapter_name[80]; /* Name exported to /proc/ioports */
260 u_long shmem_base; /* Shared memory start address */
261 u_long shmem_length; /* Shared memory window length */
262 struct net_device_stats stats; /* Public stats */
263 struct {
264 u32 bins[EWRK3_PKT_STAT_SZ]; /* Private stats counters */
265 u32 unicast;
266 u32 multicast;
267 u32 broadcast;
268 u32 excessive_collisions;
269 u32 tx_underruns;
270 u32 excessive_underruns;
271 } pktStats;
272 u_char irq_mask; /* Adapter IRQ mask bits */
273 u_char mPage; /* Maximum 2kB Page number */
274 u_char lemac; /* Chip rev. level */
275 u_char hard_strapped; /* Don't allow a full open */
276 u_char lock; /* Lock the page register */
277 u_char txc; /* Transmit cut through */
278 u_char *mctbl; /* Pointer to the multicast table */
282 ** Force the EtherWORKS 3 card to be in 2kB MODE
284 #define FORCE_2K_MODE { \
285 shmem_length = SHMEM_2K;\
286 outb(((mem_start - 0x80000) >> 11), EWRK3_MBR);\
290 ** Public Functions
292 static int ewrk3_open(struct net_device *dev);
293 static int ewrk3_queue_pkt(struct sk_buff *skb, struct net_device *dev);
294 static void ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
295 static int ewrk3_close(struct net_device *dev);
296 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev);
297 static void set_multicast_list(struct net_device *dev);
298 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
301 ** Private functions
303 static int ewrk3_hw_init(struct net_device *dev, u_long iobase);
304 static void ewrk3_init(struct net_device *dev);
305 static int ewrk3_rx(struct net_device *dev);
306 static int ewrk3_tx(struct net_device *dev);
307 static void ewrk3_timeout(struct net_device *dev);
309 static void EthwrkSignature(char *name, char *eeprom_image);
310 static int DevicePresent(u_long iobase);
311 static void SetMulticastFilter(struct net_device *dev);
312 static int EISA_signature(char *name, s32 eisa_id);
314 static int Read_EEPROM(u_long iobase, u_char eaddr);
315 static int Write_EEPROM(short data, u_long iobase, u_char eaddr);
316 static u_char get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType);
318 static void isa_probe(struct net_device *dev, u_long iobase);
319 static void eisa_probe(struct net_device *dev, u_long iobase);
320 static struct net_device *alloc_device(struct net_device *dev, u_long iobase);
321 static int ewrk3_dev_index(char *s);
322 static struct net_device *insert_device(struct net_device *dev, u_long iobase, int (*init) (struct net_device *));
325 #ifdef MODULE
326 int init_module(void);
327 void cleanup_module(void);
328 static int autoprobed = 1, loading_module = 1;
330 #else
331 static u_char irq[] =
332 {5, 0, 10, 3, 11, 9, 15, 12};
333 static int autoprobed = 0, loading_module = 0;
335 #endif /* MODULE */
337 static char name[EWRK3_STRLEN + 1];
338 static int num_ewrk3s = 0, num_eth = 0;
341 ** Miscellaneous defines...
343 #define INIT_EWRK3 {\
344 outb(EEPROM_INIT, EWRK3_IOPR);\
345 mdelay(1);\
348 int __init ewrk3_probe(struct net_device *dev)
350 int tmp = num_ewrk3s, status = -ENODEV;
351 u_long iobase = dev->base_addr;
353 if ((iobase == 0) && loading_module) {
354 printk("Autoprobing is not supported when loading a module based driver.\n");
355 status = -EIO;
356 } else { /* First probe for the Ethernet */
357 /* Address PROM pattern */
358 isa_probe(dev, iobase);
359 eisa_probe(dev, iobase);
361 if ((tmp == num_ewrk3s) && (iobase != 0) && loading_module) {
362 printk("%s: ewrk3_probe() cannot find device at 0x%04lx.\n", dev->name,
363 iobase);
366 ** Walk the device list to check that at least one device
367 ** initialised OK
369 for (; (dev->priv == NULL) && (dev->next != NULL); dev = dev->next);
371 if (dev->priv)
372 status = 0;
373 if (iobase == 0)
374 autoprobed = 1;
377 return status;
380 static int __init
381 ewrk3_hw_init(struct net_device *dev, u_long iobase)
383 struct ewrk3_private *lp;
384 int i, status = 0;
385 u_long mem_start, shmem_length;
386 u_char cr, cmr, icr, nicsr, lemac, hard_strapped = 0;
387 u_char eeprom_image[EEPROM_MAX], chksum, eisa_cr = 0;
390 ** Stop the EWRK3. Enable the DBR ROM. Disable interrupts and remote boot.
391 ** This also disables the EISA_ENABLE bit in the EISA Control Register.
393 if (iobase > 0x400)
394 eisa_cr = inb(EISA_CR);
395 INIT_EWRK3;
397 nicsr = inb(EWRK3_CSR);
399 icr = inb(EWRK3_ICR);
400 icr &= 0x70;
401 outb(icr, EWRK3_ICR); /* Disable all the IRQs */
403 if (nicsr == (CSR_TXD | CSR_RXD)) {
405 /* Check that the EEPROM is alive and well and not living on Pluto... */
406 for (chksum = 0, i = 0; i < EEPROM_MAX; i += 2) {
407 union {
408 short val;
409 char c[2];
410 } tmp;
412 tmp.val = (short) Read_EEPROM(iobase, (i >> 1));
413 eeprom_image[i] = tmp.c[0];
414 eeprom_image[i + 1] = tmp.c[1];
415 chksum += eeprom_image[i] + eeprom_image[i + 1];
418 if (chksum != 0) { /* Bad EEPROM Data! */
419 printk("%s: Device has a bad on-board EEPROM.\n", dev->name);
420 status = -ENXIO;
421 } else {
422 EthwrkSignature(name, eeprom_image);
423 if (*name != '\0') { /* found a EWRK3 device */
424 dev->base_addr = iobase;
426 if (iobase > 0x400) {
427 outb(eisa_cr, EISA_CR); /* Rewrite the EISA CR */
429 lemac = eeprom_image[EEPROM_CHIPVER];
430 cmr = inb(EWRK3_CMR);
432 if (((lemac == LeMAC) && ((cmr & CMR_NO_EEPROM) != CMR_NO_EEPROM)) ||
433 ((lemac == LeMAC2) && !(cmr & CMR_HS))) {
434 printk("%s: %s at %#4lx", dev->name, name, iobase);
435 hard_strapped = 1;
436 } else if ((iobase & 0x0fff) == EWRK3_EISA_IO_PORTS) {
437 /* EISA slot address */
438 printk("%s: %s at %#4lx (EISA slot %ld)",
439 dev->name, name, iobase, ((iobase >> 12) & 0x0f));
440 } else { /* ISA port address */
441 printk("%s: %s at %#4lx", dev->name, name, iobase);
444 if (!status) {
445 printk(", h/w address ");
446 if (lemac != LeMAC2)
447 DevicePresent(iobase); /* need after EWRK3_INIT */
448 status = get_hw_addr(dev, eeprom_image, lemac);
449 for (i = 0; i < ETH_ALEN - 1; i++) { /* get the ethernet addr. */
450 printk("%2.2x:", dev->dev_addr[i]);
452 printk("%2.2x,\n", dev->dev_addr[i]);
454 if (status) {
455 printk(" which has an EEPROM CRC error.\n");
456 status = -ENXIO;
457 } else {
458 if (lemac == LeMAC2) { /* Special LeMAC2 CMR things */
459 cmr &= ~(CMR_RA | CMR_WB | CMR_LINK | CMR_POLARITY | CMR_0WS);
460 if (eeprom_image[EEPROM_MISC0] & READ_AHEAD)
461 cmr |= CMR_RA;
462 if (eeprom_image[EEPROM_MISC0] & WRITE_BEHIND)
463 cmr |= CMR_WB;
464 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_POL)
465 cmr |= CMR_POLARITY;
466 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_LINK)
467 cmr |= CMR_LINK;
468 if (eeprom_image[EEPROM_MISC0] & _0WS_ENA)
469 cmr |= CMR_0WS;
471 if (eeprom_image[EEPROM_SETUP] & SETUP_DRAM)
472 cmr |= CMR_DRAM;
473 outb(cmr, EWRK3_CMR);
475 cr = inb(EWRK3_CR); /* Set up the Control Register */
476 cr |= eeprom_image[EEPROM_SETUP] & SETUP_APD;
477 if (cr & SETUP_APD)
478 cr |= eeprom_image[EEPROM_SETUP] & SETUP_PS;
479 cr |= eeprom_image[EEPROM_MISC0] & FAST_BUS;
480 cr |= eeprom_image[EEPROM_MISC0] & ENA_16;
481 outb(cr, EWRK3_CR);
484 ** Determine the base address and window length for the EWRK3
485 ** RAM from the memory base register.
487 mem_start = inb(EWRK3_MBR);
488 shmem_length = 0;
489 if (mem_start != 0) {
490 if ((mem_start >= 0x0a) && (mem_start <= 0x0f)) {
491 mem_start *= SHMEM_64K;
492 shmem_length = SHMEM_64K;
493 } else if ((mem_start >= 0x14) && (mem_start <= 0x1f)) {
494 mem_start *= SHMEM_32K;
495 shmem_length = SHMEM_32K;
496 } else if ((mem_start >= 0x40) && (mem_start <= 0xff)) {
497 mem_start = mem_start * SHMEM_2K + 0x80000;
498 shmem_length = SHMEM_2K;
499 } else {
500 status = -ENXIO;
504 ** See the top of this source code for comments about
505 ** uncommenting this line.
507 /* FORCE_2K_MODE; */
509 if (!status) {
510 if (hard_strapped) {
511 printk(" is hard strapped.\n");
512 } else if (mem_start) {
513 printk(" has a %dk RAM window", (int) (shmem_length >> 10));
514 printk(" at 0x%.5lx", mem_start);
515 } else {
516 printk(" is in I/O only mode");
519 /* private area & initialise */
520 dev->priv = (void *) kmalloc(sizeof(struct ewrk3_private),
521 GFP_KERNEL);
522 if (dev->priv == NULL) {
523 return -ENOMEM;
525 lp = (struct ewrk3_private *) dev->priv;
526 memset(dev->priv, 0, sizeof(struct ewrk3_private));
527 lp->shmem_base = mem_start;
528 lp->shmem_length = shmem_length;
529 lp->lemac = lemac;
530 lp->hard_strapped = hard_strapped;
532 lp->mPage = 64;
533 if (cmr & CMR_DRAM)
534 lp->mPage <<= 1; /* 2 DRAMS on module */
536 sprintf(lp->adapter_name, "%s (%s)", name, dev->name);
537 request_region(iobase, EWRK3_TOTAL_SIZE, lp->adapter_name);
539 lp->irq_mask = ICR_TNEM | ICR_TXDM | ICR_RNEM | ICR_RXDM;
541 if (!hard_strapped) {
543 ** Enable EWRK3 board interrupts for autoprobing
545 icr |= ICR_IE; /* Enable interrupts */
546 outb(icr, EWRK3_ICR);
548 /* The DMA channel may be passed in on this parameter. */
549 dev->dma = 0;
551 /* To auto-IRQ we enable the initialization-done and DMA err,
552 interrupts. For now we will always get a DMA error. */
553 if (dev->irq < 2) {
554 #ifndef MODULE
555 u_char irqnum;
557 autoirq_setup(0);
560 ** Trigger a TNE interrupt.
562 icr |= ICR_TNEM;
563 outb(1, EWRK3_TDQ); /* Write to the TX done queue */
564 outb(icr, EWRK3_ICR); /* Unmask the TXD interrupt */
566 irqnum = irq[((icr & IRQ_SEL) >> 4)];
568 dev->irq = autoirq_report(1);
569 if ((dev->irq) && (irqnum == dev->irq)) {
570 printk(" and uses IRQ%d.\n", dev->irq);
571 } else {
572 if (!dev->irq) {
573 printk(" and failed to detect IRQ line.\n");
574 } else if ((irqnum == 1) && (lemac == LeMAC2)) {
575 printk(" and an illegal IRQ line detected.\n");
576 } else {
577 printk(", but incorrect IRQ line detected.\n");
579 status = -ENXIO;
582 DISABLE_IRQs; /* Mask all interrupts */
584 #endif /* MODULE */
585 } else {
586 printk(" and requires IRQ%d.\n", dev->irq);
589 if (status)
590 release_region(iobase, EWRK3_TOTAL_SIZE);
591 } else {
592 status = -ENXIO;
596 } else {
597 status = -ENXIO;
601 if (!status) {
602 if (ewrk3_debug > 1) {
603 printk(version);
605 /* The EWRK3-specific entries in the device structure. */
606 dev->open = ewrk3_open;
607 dev->hard_start_xmit = ewrk3_queue_pkt;
608 dev->stop = ewrk3_close;
609 dev->get_stats = ewrk3_get_stats;
610 dev->set_multicast_list = set_multicast_list;
611 dev->do_ioctl = ewrk3_ioctl;
612 dev->tx_timeout = ewrk3_timeout;
613 dev->watchdog_timeo = QUEUE_PKT_TIMEOUT;
615 dev->mem_start = 0;
617 /* Fill in the generic field of the device structure. */
618 ether_setup(dev);
620 } else {
621 status = -ENXIO;
623 return status;
627 static int ewrk3_open(struct net_device *dev)
629 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
630 u_long iobase = dev->base_addr;
631 int i, status = 0;
632 u_char icr, csr;
635 ** Stop the TX and RX...
637 STOP_EWRK3;
639 if (!lp->hard_strapped) {
640 if (request_irq(dev->irq, (void *) ewrk3_interrupt, 0, "ewrk3", dev)) {
641 printk("ewrk3_open(): Requested IRQ%d is busy\n", dev->irq);
642 status = -EAGAIN;
643 } else {
646 ** Re-initialize the EWRK3...
648 ewrk3_init(dev);
650 if (ewrk3_debug > 1) {
651 printk("%s: ewrk3 open with irq %d\n", dev->name, dev->irq);
652 printk(" physical address: ");
653 for (i = 0; i < 5; i++) {
654 printk("%2.2x:", (u_char) dev->dev_addr[i]);
656 printk("%2.2x\n", (u_char) dev->dev_addr[i]);
657 if (lp->shmem_length == 0) {
658 printk(" no shared memory, I/O only mode\n");
659 } else {
660 printk(" start of shared memory: 0x%08lx\n", lp->shmem_base);
661 printk(" window length: 0x%04lx\n", lp->shmem_length);
663 printk(" # of DRAMS: %d\n", ((inb(EWRK3_CMR) & 0x02) ? 2 : 1));
664 printk(" csr: 0x%02x\n", inb(EWRK3_CSR));
665 printk(" cr: 0x%02x\n", inb(EWRK3_CR));
666 printk(" icr: 0x%02x\n", inb(EWRK3_ICR));
667 printk(" cmr: 0x%02x\n", inb(EWRK3_CMR));
668 printk(" fmqc: 0x%02x\n", inb(EWRK3_FMQC));
670 netif_start_queue(dev);
672 ** Unmask EWRK3 board interrupts
674 icr = inb(EWRK3_ICR);
675 ENABLE_IRQs;
678 } else {
679 printk(KERN_ERR "%s: ewrk3 available for hard strapped set up only.\n", dev->name);
680 printk(KERN_ERR " Run the 'ewrk3setup' utility or remove the hard straps.\n");
681 return -EINVAL;
684 MOD_INC_USE_COUNT;
686 return status;
690 ** Initialize the EtherWORKS 3 operating conditions
692 static void ewrk3_init(struct net_device *dev)
694 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
695 u_char csr, page;
696 u_long iobase = dev->base_addr;
699 ** Enable any multicasts
701 set_multicast_list(dev);
704 ** Clean out any remaining entries in all the queues here
706 while (inb(EWRK3_TQ));
707 while (inb(EWRK3_TDQ));
708 while (inb(EWRK3_RQ));
709 while (inb(EWRK3_FMQ));
712 ** Write a clean free memory queue
714 for (page = 1; page < lp->mPage; page++) { /* Write the free page numbers */
715 outb(page, EWRK3_FMQ); /* to the Free Memory Queue */
718 lp->lock = 0; /* Ensure there are no locks */
720 START_EWRK3; /* Enable the TX and/or RX */
724 * Transmit timeout
727 static void ewrk3_timeout(struct net_device *dev)
729 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
730 u_char icr, csr;
731 u_long iobase = dev->base_addr;
733 if (!lp->hard_strapped)
735 printk(KERN_WARNING"%s: transmit timed/locked out, status %04x, resetting.\n",
736 dev->name, inb(EWRK3_CSR));
739 ** Mask all board interrupts
741 DISABLE_IRQs;
744 ** Stop the TX and RX...
746 STOP_EWRK3;
748 ewrk3_init(dev);
751 ** Unmask EWRK3 board interrupts
753 ENABLE_IRQs;
755 dev->trans_start = jiffies;
756 netif_wake_queue(dev);
761 ** Writes a socket buffer to the free page queue
763 static int ewrk3_queue_pkt(struct sk_buff *skb, struct net_device *dev)
765 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
766 u_long iobase = dev->base_addr;
767 int status = 0;
768 u_char icr;
770 netif_stop_queue(dev);
771 #ifdef CONFIG_SMP
772 #error "This needs spinlocks"
773 #endif
774 DISABLE_IRQs; /* So that the page # remains correct */
777 ** Get a free page from the FMQ when resources are available
779 if (inb(EWRK3_FMQC) > 0)
781 u_long buf = 0;
782 u_char page;
784 if ((page = inb(EWRK3_FMQ)) < lp->mPage) {
786 ** Set up shared memory window and pointer into the window
788 while (test_and_set_bit(0, (void *) &lp->lock) != 0); /* Wait for lock to free */
789 if (lp->shmem_length == IO_ONLY) {
790 outb(page, EWRK3_IOPR);
791 } else if (lp->shmem_length == SHMEM_2K) {
792 buf = lp->shmem_base;
793 outb(page, EWRK3_MPR);
794 } else if (lp->shmem_length == SHMEM_32K) {
795 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
796 outb((page >> 4), EWRK3_MPR);
797 } else if (lp->shmem_length == SHMEM_64K) {
798 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
799 outb((page >> 5), EWRK3_MPR);
800 } else {
801 status = -1;
802 printk(KERN_ERR "%s: Oops - your private data area is hosed!\n", dev->name);
805 if (!status) {
807 ** Set up the buffer control structures and copy the data from
808 ** the socket buffer to the shared memory .
810 if (lp->shmem_length == IO_ONLY) {
811 int i;
812 u_char *p = skb->data;
813 outb((char) (TCR_QMODE | TCR_PAD | TCR_IFC), EWRK3_DATA);
814 outb((char) (skb->len & 0xff), EWRK3_DATA);
815 outb((char) ((skb->len >> 8) & 0xff), EWRK3_DATA);
816 outb((char) 0x04, EWRK3_DATA);
817 for (i = 0; i < skb->len; i++) {
818 outb(*p++, EWRK3_DATA);
820 outb(page, EWRK3_TQ); /* Start sending pkt */
821 } else {
822 writeb((char) (TCR_QMODE | TCR_PAD | TCR_IFC), (char *) buf); /* ctrl byte */
823 buf += 1;
824 writeb((char) (skb->len & 0xff), (char *) buf); /* length (16 bit xfer) */
825 buf += 1;
826 if (lp->txc) {
827 writeb((char) (((skb->len >> 8) & 0xff) | XCT), (char *) buf);
828 buf += 1;
829 writeb(0x04, (char *) buf); /* index byte */
830 buf += 1;
831 writeb(0x00, (char *) (buf + skb->len)); /* Write the XCT flag */
832 isa_memcpy_toio(buf, skb->data, PRELOAD); /* Write PRELOAD bytes */
833 outb(page, EWRK3_TQ); /* Start sending pkt */
834 isa_memcpy_toio(buf + PRELOAD, skb->data + PRELOAD, skb->len - PRELOAD);
835 writeb(0xff, (char *) (buf + skb->len)); /* Write the XCT flag */
836 } else {
837 writeb((char) ((skb->len >> 8) & 0xff), (char *) buf);
838 buf += 1;
839 writeb(0x04, (char *) buf); /* index byte */
840 buf += 1;
841 isa_memcpy_toio(buf, skb->data, skb->len); /* Write data bytes */
842 outb(page, EWRK3_TQ); /* Start sending pkt */
846 dev->trans_start = jiffies;
847 dev_kfree_skb(skb);
848 } else { /* return unused page to the free memory queue */
849 outb(page, EWRK3_FMQ);
851 lp->lock = 0; /* unlock the page register */
852 } else {
853 printk("ewrk3_queue_pkt(): Invalid free memory page (%d).\n",
854 (u_char) page);
856 } else {
857 printk(KERN_WARNING "%s: ewrk3_queue_pkt(): No free resources...\n", dev->name);
858 printk(KERN_WARNING "%s: ewrk3_queue_pkt(): CSR: %02x ICR: %02x FMQC: %02x\n", dev->name, inb(EWRK3_CSR), inb(EWRK3_ICR), inb(EWRK3_FMQC));
861 /* Check for free resources: clear 'tbusy' if there are some */
862 if (inb(EWRK3_FMQC) > 0) {
863 netif_wake_queue(dev);
865 ENABLE_IRQs;
866 return status;
870 ** The EWRK3 interrupt handler.
872 static void ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
874 struct net_device *dev = dev_id;
875 struct ewrk3_private *lp;
876 u_long iobase;
877 u_char icr, cr, csr;
879 lp = (struct ewrk3_private *) dev->priv;
880 iobase = dev->base_addr;
882 /* get the interrupt information */
883 csr = inb(EWRK3_CSR);
886 ** Mask the EWRK3 board interrupts and turn on the LED
888 DISABLE_IRQs;
890 cr = inb(EWRK3_CR);
891 cr |= CR_LED;
892 outb(cr, EWRK3_CR);
894 if (csr & CSR_RNE) /* Rx interrupt (packet[s] arrived) */
895 ewrk3_rx(dev);
897 if (csr & CSR_TNE) /* Tx interrupt (packet sent) */
898 ewrk3_tx(dev);
901 ** Now deal with the TX/RX disable flags. These are set when there
902 ** are no more resources. If resources free up then enable these
903 ** interrupts, otherwise mask them - failure to do this will result
904 ** in the system hanging in an interrupt loop.
906 if (inb(EWRK3_FMQC)) { /* any resources available? */
907 lp->irq_mask |= ICR_TXDM | ICR_RXDM; /* enable the interrupt source */
908 csr &= ~(CSR_TXD | CSR_RXD); /* ensure restart of a stalled TX or RX */
909 outb(csr, EWRK3_CSR);
910 netif_wake_queue(dev);
911 } else {
912 lp->irq_mask &= ~(ICR_TXDM | ICR_RXDM); /* disable the interrupt source */
915 /* Unmask the EWRK3 board interrupts and turn off the LED */
916 cr &= ~CR_LED;
917 outb(cr, EWRK3_CR);
918 ENABLE_IRQs;
921 static int ewrk3_rx(struct net_device *dev)
923 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
924 u_long iobase = dev->base_addr;
925 int i, status = 0;
926 u_char page, tmpPage = 0, tmpLock = 0;
927 u_long buf = 0;
929 while (inb(EWRK3_RQC) && !status) { /* Whilst there's incoming data */
930 if ((page = inb(EWRK3_RQ)) < lp->mPage) { /* Get next entry's buffer page */
932 ** Preempt any process using the current page register. Check for
933 ** an existing lock to reduce time taken in I/O transactions.
935 if ((tmpLock = test_and_set_bit(0, (void *) &lp->lock)) == 1) { /* Assert lock */
936 if (lp->shmem_length == IO_ONLY) { /* Get existing page */
937 tmpPage = inb(EWRK3_IOPR);
938 } else {
939 tmpPage = inb(EWRK3_MPR);
943 ** Set up shared memory window and pointer into the window
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);
961 if (!status) {
962 char rx_status;
963 int pkt_len;
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 = readb(buf);
971 buf += 1;
972 pkt_len = readw(buf);
973 buf += 3;
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;
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);
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);
998 } else {
999 isa_memcpy_fromio(p, buf, pkt_len);
1003 ** Notify the upper protocol layers that there is another
1004 ** packet to handle
1006 skb->protocol = eth_type_trans(skb, dev);
1007 netif_rx(skb);
1010 ** Update stats
1012 lp->stats.rx_packets++;
1013 for (i = 1; i < EWRK3_PKT_STAT_SZ - 1; i++) {
1014 if (pkt_len < i * EWRK3_PKT_BIN_SZ) {
1015 lp->pktStats.bins[i]++;
1016 i = EWRK3_PKT_STAT_SZ;
1019 p = skb->data; /* Look at the dest addr */
1020 if (p[0] & 0x01) { /* Multicast/Broadcast */
1021 if ((*(s32 *) & p[0] == -1) && (*(s16 *) & p[4] == -1)) {
1022 lp->pktStats.broadcast++;
1023 } else {
1024 lp->pktStats.multicast++;
1026 } else if ((*(s32 *) & p[0] == *(s32 *) & dev->dev_addr[0]) &&
1027 (*(s16 *) & p[4] == *(s16 *) & dev->dev_addr[4])) {
1028 lp->pktStats.unicast++;
1030 lp->pktStats.bins[0]++; /* Duplicates stats.rx_packets */
1031 if (lp->pktStats.bins[0] == 0) { /* Reset counters */
1032 memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1034 } else {
1035 printk("%s: Insufficient memory; nuking packet.\n", dev->name);
1036 lp->stats.rx_dropped++; /* Really, deferred. */
1037 break;
1042 ** Return the received buffer to the free memory queue
1044 outb(page, EWRK3_FMQ);
1046 if (tmpLock) { /* If a lock was preempted */
1047 if (lp->shmem_length == IO_ONLY) { /* Replace old page */
1048 outb(tmpPage, EWRK3_IOPR);
1049 } else {
1050 outb(tmpPage, EWRK3_MPR);
1053 lp->lock = 0; /* Unlock the page register */
1054 } else {
1055 printk("ewrk3_rx(): Illegal page number, page %d\n", page);
1056 printk("ewrk3_rx(): CSR: %02x ICR: %02x FMQC: %02x\n", inb(EWRK3_CSR), inb(EWRK3_ICR), inb(EWRK3_FMQC));
1059 return status;
1063 ** Buffer sent - check for TX buffer errors.
1065 static int ewrk3_tx(struct net_device *dev)
1067 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1068 u_long iobase = dev->base_addr;
1069 u_char tx_status;
1071 while ((tx_status = inb(EWRK3_TDQ)) > 0) { /* Whilst there's old buffers */
1072 if (tx_status & T_VSTS) { /* The status is valid */
1073 if (tx_status & T_TXE) {
1074 lp->stats.tx_errors++;
1075 if (tx_status & T_NCL)
1076 lp->stats.tx_carrier_errors++;
1077 if (tx_status & T_LCL)
1078 lp->stats.tx_window_errors++;
1079 if (tx_status & T_CTU) {
1080 if ((tx_status & T_COLL) ^ T_XUR) {
1081 lp->pktStats.tx_underruns++;
1082 } else {
1083 lp->pktStats.excessive_underruns++;
1085 } else if (tx_status & T_COLL) {
1086 if ((tx_status & T_COLL) ^ T_XCOLL) {
1087 lp->stats.collisions++;
1088 } else {
1089 lp->pktStats.excessive_collisions++;
1092 } else {
1093 lp->stats.tx_packets++;
1098 return 0;
1101 static int ewrk3_close(struct net_device *dev)
1103 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1104 u_long iobase = dev->base_addr;
1105 u_char icr, csr;
1107 netif_stop_queue(dev);
1109 if (ewrk3_debug > 1) {
1110 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1111 dev->name, inb(EWRK3_CSR));
1114 ** We stop the EWRK3 here... mask interrupts and stop TX & RX
1116 DISABLE_IRQs;
1118 STOP_EWRK3;
1121 ** Clean out the TX and RX queues here (note that one entry
1122 ** may get added to either the TXD or RX queues if the TX or RX
1123 ** just starts processing a packet before the STOP_EWRK3 command
1124 ** is received. This will be flushed in the ewrk3_open() call).
1126 while (inb(EWRK3_TQ));
1127 while (inb(EWRK3_TDQ));
1128 while (inb(EWRK3_RQ));
1130 if (!lp->hard_strapped) {
1131 free_irq(dev->irq, dev);
1133 MOD_DEC_USE_COUNT;
1135 return 0;
1138 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev)
1140 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1142 /* Null body since there is no framing error counter */
1143 return &lp->stats;
1147 ** Set or clear the multicast filter for this adapter.
1149 static void set_multicast_list(struct net_device *dev)
1151 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1152 u_long iobase = dev->base_addr;
1153 u_char csr;
1155 csr = inb(EWRK3_CSR);
1157 if (lp->shmem_length == IO_ONLY) {
1158 lp->mctbl = (char *) PAGE0_HTE;
1159 } else {
1160 lp->mctbl = (char *) (lp->shmem_base + PAGE0_HTE);
1163 csr &= ~(CSR_PME | CSR_MCE);
1164 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
1165 csr |= CSR_PME;
1166 outb(csr, EWRK3_CSR);
1167 } else {
1168 SetMulticastFilter(dev);
1169 csr |= CSR_MCE;
1170 outb(csr, EWRK3_CSR);
1175 ** Calculate the hash code and update the logical address filter
1176 ** from a list of ethernet multicast addresses.
1177 ** Little endian crc one liner from Matt Thomas, DEC.
1179 ** Note that when clearing the table, the broadcast bit must remain asserted
1180 ** to receive broadcast messages.
1182 static void SetMulticastFilter(struct net_device *dev)
1184 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1185 struct dev_mc_list *dmi = dev->mc_list;
1186 u_long iobase = dev->base_addr;
1187 int i;
1188 char *addrs, j, bit, byte;
1189 short *p = (short *) lp->mctbl;
1190 u16 hashcode;
1191 s32 crc, poly = CRC_POLYNOMIAL_LE;
1193 while (test_and_set_bit(0, (void *) &lp->lock) != 0); /* Wait for lock to free */
1195 if (lp->shmem_length == IO_ONLY) {
1196 outb(0, EWRK3_IOPR);
1197 outw(EEPROM_OFFSET(lp->mctbl), EWRK3_PIR1);
1198 } else {
1199 outb(0, EWRK3_MPR);
1202 if (dev->flags & IFF_ALLMULTI) {
1203 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1204 if (lp->shmem_length == IO_ONLY) {
1205 outb(0xff, EWRK3_DATA);
1206 } else { /* memset didn't work here */
1207 writew(0xffff, p);
1208 p++;
1209 i++;
1212 } else {
1213 /* Clear table except for broadcast bit */
1214 if (lp->shmem_length == IO_ONLY) {
1215 for (i = 0; i < (HASH_TABLE_LEN >> 4) - 1; i++) {
1216 outb(0x00, EWRK3_DATA);
1218 outb(0x80, EWRK3_DATA);
1219 i++; /* insert the broadcast bit */
1220 for (; i < (HASH_TABLE_LEN >> 3); i++) {
1221 outb(0x00, EWRK3_DATA);
1223 } else {
1224 memset_io(lp->mctbl, 0, (HASH_TABLE_LEN >> 3));
1225 writeb(0x80, (char *) (lp->mctbl + (HASH_TABLE_LEN >> 4) - 1));
1228 /* Update table */
1229 for (i = 0; i < dev->mc_count; i++) { /* for each address in the list */
1230 addrs = dmi->dmi_addr;
1231 dmi = dmi->next;
1232 if ((*addrs & 0x01) == 1) { /* multicast address? */
1233 crc = 0xffffffff; /* init CRC for each address */
1234 for (byte = 0; byte < ETH_ALEN; byte++) { /* for each address byte */
1235 /* process each address bit */
1236 for (bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
1237 crc = (crc >> 1) ^ (((crc ^ bit) & 0x01) ? poly : 0);
1240 hashcode = crc & ((1 << 9) - 1); /* hashcode is 9 LSb of CRC */
1242 byte = hashcode >> 3; /* bit[3-8] -> byte in filter */
1243 bit = 1 << (hashcode & 0x07); /* bit[0-2] -> bit in byte */
1245 if (lp->shmem_length == IO_ONLY) {
1246 u_char tmp;
1248 outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1249 tmp = inb(EWRK3_DATA);
1250 tmp |= bit;
1251 outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1252 outb(tmp, EWRK3_DATA);
1253 } else {
1254 writeb(readb(lp->mctbl + byte) | bit, lp->mctbl + byte);
1260 lp->lock = 0; /* Unlock the page register */
1262 return;
1266 ** ISA bus I/O device probe
1268 static void __init isa_probe(struct net_device *dev, u_long ioaddr)
1270 int i = num_ewrk3s, maxSlots;
1271 u_long iobase;
1273 if (!ioaddr && autoprobed)
1274 return; /* Been here before ! */
1275 if (ioaddr >= 0x400)
1276 return; /* Not ISA */
1278 if (ioaddr == 0) { /* Autoprobing */
1279 iobase = EWRK3_IO_BASE; /* Get the first slot address */
1280 maxSlots = 24;
1281 } else { /* Probe a specific location */
1282 iobase = ioaddr;
1283 maxSlots = i + 1;
1286 for (; (i < maxSlots) && (dev != NULL); iobase += EWRK3_IOP_INC, i++) {
1287 if (!check_region(iobase, EWRK3_TOTAL_SIZE)) {
1288 if (DevicePresent(iobase) == 0) {
1289 if ((dev = alloc_device(dev, iobase)) != NULL) {
1290 if (ewrk3_hw_init(dev, iobase) == 0) {
1291 num_ewrk3s++;
1293 num_eth++;
1296 } else if (autoprobed) {
1297 printk("%s: region already allocated at 0x%04lx.\n", dev->name, iobase);
1301 return;
1305 ** EISA bus I/O device probe. Probe from slot 1 since slot 0 is usually
1306 ** the motherboard.
1308 static void __init eisa_probe(struct net_device *dev, u_long ioaddr)
1310 int i, maxSlots;
1311 u_long iobase;
1312 char name[EWRK3_STRLEN];
1314 if (!ioaddr && autoprobed)
1315 return; /* Been here before ! */
1316 if (ioaddr < 0x1000)
1317 return; /* Not EISA */
1319 if (ioaddr == 0) { /* Autoprobing */
1320 iobase = EISA_SLOT_INC; /* Get the first slot address */
1321 i = 1;
1322 maxSlots = MAX_EISA_SLOTS;
1323 } else { /* Probe a specific location */
1324 iobase = ioaddr;
1325 i = (ioaddr >> 12);
1326 maxSlots = i + 1;
1329 for (i = 1; (i < maxSlots) && (dev != NULL); i++, iobase += EISA_SLOT_INC) {
1330 if (EISA_signature(name, EISA_ID) == 0) {
1331 if (!check_region(iobase, EWRK3_TOTAL_SIZE)) {
1332 if (DevicePresent(iobase) == 0) {
1333 if ((dev = alloc_device(dev, iobase)) != NULL) {
1334 if (ewrk3_hw_init(dev, iobase) == 0) {
1335 num_ewrk3s++;
1337 num_eth++;
1340 } else if (autoprobed) {
1341 printk("%s: region already allocated at 0x%04lx.\n", dev->name, iobase);
1346 return;
1350 ** Search the entire 'eth' device list for a fixed probe. If a match isn't
1351 ** found then check for an autoprobe or unused device location. If they
1352 ** are not available then insert a new device structure at the end of
1353 ** the current list.
1355 static struct net_device * __init alloc_device(struct net_device *dev, u_long iobase)
1357 struct net_device *adev = NULL;
1358 int fixed = 0, new_dev = 0;
1360 num_eth = ewrk3_dev_index(dev->name);
1361 if (loading_module)
1362 return dev;
1364 while (1) {
1365 if (((dev->base_addr == EWRK3_NDA) || (dev->base_addr == 0)) && !adev) {
1366 adev = dev;
1367 } else if ((dev->priv == NULL) && (dev->base_addr == iobase)) {
1368 fixed = 1;
1369 } else {
1370 if (dev->next == NULL) {
1371 new_dev = 1;
1372 } else if (strncmp(dev->next->name, "eth", 3) != 0) {
1373 new_dev = 1;
1376 if ((dev->next == NULL) || new_dev || fixed)
1377 break;
1378 dev = dev->next;
1379 num_eth++;
1381 if (adev && !fixed) {
1382 dev = adev;
1383 num_eth = ewrk3_dev_index(dev->name);
1384 new_dev = 0;
1386 if (((dev->next == NULL) &&
1387 ((dev->base_addr != EWRK3_NDA) && (dev->base_addr != 0)) && !fixed) ||
1388 new_dev) {
1389 num_eth++; /* New device */
1390 dev = insert_device(dev, iobase, ewrk3_probe);
1392 return dev;
1396 ** If at end of eth device list and can't use current entry, malloc
1397 ** one up. If memory could not be allocated, print an error message.
1399 static struct net_device * __init
1400 insert_device(struct net_device *dev, u_long iobase, int (*init) (struct net_device *))
1402 struct net_device *new;
1404 new = (struct net_device *) kmalloc(sizeof(struct net_device) + 8, GFP_KERNEL);
1405 if (new == NULL) {
1406 printk("eth%d: Device not initialised, insufficient memory\n", num_eth);
1407 return NULL;
1408 } else {
1409 new->next = dev->next;
1410 dev->next = new;
1411 dev = dev->next; /* point to the new device */
1412 dev->name = (char *) (dev + 1);
1413 if (num_eth > 9999) {
1414 sprintf(dev->name, "eth????"); /* New device name */
1415 } else {
1416 sprintf(dev->name, "eth%d", num_eth); /* New device name */
1418 dev->base_addr = iobase; /* assign the io address */
1419 dev->init = init; /* initialisation routine */
1422 return dev;
1425 static int __init
1426 ewrk3_dev_index(char *s)
1428 int i = 0, j = 0;
1430 for (; *s; s++) {
1431 if (isdigit(*s)) {
1432 j = 1;
1433 i = (i * 10) + (*s - '0');
1434 } else if (j)
1435 break;
1438 return i;
1442 ** Read the EWRK3 EEPROM using this routine
1444 static int Read_EEPROM(u_long iobase, u_char eaddr)
1446 int i;
1448 outb((eaddr & 0x3f), EWRK3_PIR1); /* set up 6 bits of address info */
1449 outb(EEPROM_RD, EWRK3_IOPR); /* issue read command */
1450 for (i = 0; i < 5000; i++)
1451 inb(EWRK3_CSR); /* wait 1msec */
1453 return inw(EWRK3_EPROM1); /* 16 bits data return */
1457 ** Write the EWRK3 EEPROM using this routine
1459 static int Write_EEPROM(short data, u_long iobase, u_char eaddr)
1461 int i;
1463 outb(EEPROM_WR_EN, EWRK3_IOPR); /* issue write enable command */
1464 for (i = 0; i < 5000; i++)
1465 inb(EWRK3_CSR); /* wait 1msec */
1466 outw(data, EWRK3_EPROM1); /* write data to register */
1467 outb((eaddr & 0x3f), EWRK3_PIR1); /* set up 6 bits of address info */
1468 outb(EEPROM_WR, EWRK3_IOPR); /* issue write command */
1469 for (i = 0; i < 75000; i++)
1470 inb(EWRK3_CSR); /* wait 15msec */
1471 outb(EEPROM_WR_DIS, EWRK3_IOPR); /* issue write disable command */
1472 for (i = 0; i < 5000; i++)
1473 inb(EWRK3_CSR); /* wait 1msec */
1475 return 0;
1479 ** Look for a particular board name in the on-board EEPROM.
1481 static void __init EthwrkSignature(char *name, char *eeprom_image)
1483 u_long i, j, k;
1484 char *signatures[] = EWRK3_SIGNATURE;
1486 strcpy(name, "");
1487 for (i = 0; *signatures[i] != '\0' && *name == '\0'; i++) {
1488 for (j = EEPROM_PNAME7, k = 0; j <= EEPROM_PNAME0 && k < strlen(signatures[i]); j++) {
1489 if (signatures[i][k] == eeprom_image[j]) { /* track signature */
1490 k++;
1491 } else { /* lost signature; begin search again */
1492 k = 0;
1495 if (k == strlen(signatures[i])) {
1496 for (k = 0; k < EWRK3_STRLEN; k++) {
1497 name[k] = eeprom_image[EEPROM_PNAME7 + k];
1498 name[EWRK3_STRLEN] = '\0';
1503 return; /* return the device name string */
1507 ** Look for a special sequence in the Ethernet station address PROM that
1508 ** is common across all EWRK3 products.
1510 ** Search the Ethernet address ROM for the signature. Since the ROM address
1511 ** counter can start at an arbitrary point, the search must include the entire
1512 ** probe sequence length plus the (length_of_the_signature - 1).
1513 ** Stop the search IMMEDIATELY after the signature is found so that the
1514 ** PROM address counter is correctly positioned at the start of the
1515 ** ethernet address for later read out.
1518 static int __init DevicePresent(u_long iobase)
1520 union {
1521 struct {
1522 u32 a;
1523 u32 b;
1524 } llsig;
1525 char Sig[sizeof(u32) << 1];
1527 dev;
1528 short sigLength;
1529 char data;
1530 int i, j, status = 0;
1532 dev.llsig.a = ETH_PROM_SIG;
1533 dev.llsig.b = ETH_PROM_SIG;
1534 sigLength = sizeof(u32) << 1;
1536 for (i = 0, j = 0; j < sigLength && i < PROBE_LENGTH + sigLength - 1; i++) {
1537 data = inb(EWRK3_APROM);
1538 if (dev.Sig[j] == data) { /* track signature */
1539 j++;
1540 } else { /* lost signature; begin search again */
1541 if (data == dev.Sig[0]) {
1542 j = 1;
1543 } else {
1544 j = 0;
1549 if (j != sigLength) {
1550 status = -ENODEV; /* search failed */
1552 return status;
1555 static u_char __init get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType)
1557 int i, j, k;
1558 u_short chksum;
1559 u_char crc, lfsr, sd, status = 0;
1560 u_long iobase = dev->base_addr;
1561 u16 tmp;
1563 if (chipType == LeMAC2) {
1564 for (crc = 0x6a, j = 0; j < ETH_ALEN; j++) {
1565 sd = dev->dev_addr[j] = eeprom_image[EEPROM_PADDR0 + j];
1566 outb(dev->dev_addr[j], EWRK3_PAR0 + j);
1567 for (k = 0; k < 8; k++, sd >>= 1) {
1568 lfsr = ((((crc & 0x02) >> 1) ^ (crc & 0x01)) ^ (sd & 0x01)) << 7;
1569 crc = (crc >> 1) + lfsr;
1572 if (crc != eeprom_image[EEPROM_PA_CRC])
1573 status = -1;
1574 } else {
1575 for (i = 0, k = 0; i < ETH_ALEN;) {
1576 k <<= 1;
1577 if (k > 0xffff)
1578 k -= 0xffff;
1580 k += (u_char) (tmp = inb(EWRK3_APROM));
1581 dev->dev_addr[i] = (u_char) tmp;
1582 outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1583 i++;
1584 k += (u_short) ((tmp = inb(EWRK3_APROM)) << 8);
1585 dev->dev_addr[i] = (u_char) tmp;
1586 outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1587 i++;
1589 if (k > 0xffff)
1590 k -= 0xffff;
1592 if (k == 0xffff)
1593 k = 0;
1594 chksum = inb(EWRK3_APROM);
1595 chksum |= (inb(EWRK3_APROM) << 8);
1596 if (k != chksum)
1597 status = -1;
1600 return status;
1604 ** Look for a particular board name in the EISA configuration space
1606 static int __init EISA_signature(char *name, s32 eisa_id)
1608 u_long i;
1609 char *signatures[] = EWRK3_SIGNATURE;
1610 char ManCode[EWRK3_STRLEN];
1611 union {
1612 s32 ID;
1613 char Id[4];
1614 } Eisa;
1615 int status = 0;
1617 *name = '\0';
1618 for (i = 0; i < 4; i++) {
1619 Eisa.Id[i] = inb(eisa_id + i);
1622 ManCode[0] = (((Eisa.Id[0] >> 2) & 0x1f) + 0x40);
1623 ManCode[1] = (((Eisa.Id[1] & 0xe0) >> 5) + ((Eisa.Id[0] & 0x03) << 3) + 0x40);
1624 ManCode[2] = (((Eisa.Id[2] >> 4) & 0x0f) + 0x30);
1625 ManCode[3] = ((Eisa.Id[2] & 0x0f) + 0x30);
1626 ManCode[4] = (((Eisa.Id[3] >> 4) & 0x0f) + 0x30);
1627 ManCode[5] = '\0';
1629 for (i = 0; (*signatures[i] != '\0') && (*name == '\0'); i++) {
1630 if (strstr(ManCode, signatures[i]) != NULL) {
1631 strcpy(name, ManCode);
1632 status = 1;
1636 return status; /* return the device name string */
1640 ** Perform IOCTL call functions here. Some are privileged operations and the
1641 ** effective uid is checked in those cases.
1643 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1645 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1646 struct ewrk3_ioctl *ioc = (struct ewrk3_ioctl *) &rq->ifr_data;
1647 u_long iobase = dev->base_addr;
1648 int i, j, status = 0;
1649 u_char csr;
1650 union {
1651 u_char addr[HASH_TABLE_LEN * ETH_ALEN];
1652 u_short val[(HASH_TABLE_LEN * ETH_ALEN) >> 1];
1653 } tmp;
1655 switch (ioc->cmd) {
1656 case EWRK3_GET_HWADDR: /* Get the hardware address */
1657 for (i = 0; i < ETH_ALEN; i++) {
1658 tmp.addr[i] = dev->dev_addr[i];
1660 ioc->len = ETH_ALEN;
1661 if (copy_to_user(ioc->data, tmp.addr, ioc->len)) {
1662 status = -EFAULT;
1663 break;
1665 case EWRK3_SET_HWADDR: /* Set the hardware address */
1666 if (capable(CAP_NET_ADMIN)) {
1667 csr = inb(EWRK3_CSR);
1668 csr |= (CSR_TXD | CSR_RXD);
1669 outb(csr, EWRK3_CSR); /* Disable the TX and RX */
1671 if (copy_from_user(tmp.addr, ioc->data, ETH_ALEN)) {
1672 status = -EFAULT;
1673 break;
1675 for (i = 0; i < ETH_ALEN; i++) {
1676 dev->dev_addr[i] = tmp.addr[i];
1677 outb(tmp.addr[i], EWRK3_PAR0 + i);
1680 csr &= ~(CSR_TXD | CSR_RXD); /* Enable the TX and RX */
1681 outb(csr, EWRK3_CSR);
1682 } else {
1683 status = -EPERM;
1686 break;
1687 case EWRK3_SET_PROM: /* Set Promiscuous Mode */
1688 if (capable(CAP_NET_ADMIN)) {
1689 csr = inb(EWRK3_CSR);
1690 csr |= CSR_PME;
1691 csr &= ~CSR_MCE;
1692 outb(csr, EWRK3_CSR);
1693 } else {
1694 status = -EPERM;
1697 break;
1698 case EWRK3_CLR_PROM: /* Clear Promiscuous Mode */
1699 if (capable(CAP_NET_ADMIN)) {
1700 csr = inb(EWRK3_CSR);
1701 csr &= ~CSR_PME;
1702 outb(csr, EWRK3_CSR);
1703 } else {
1704 status = -EPERM;
1707 break;
1708 case EWRK3_SAY_BOO: /* Say "Boo!" to the kernel log file */
1709 printk("%s: Boo!\n", dev->name);
1711 break;
1712 case EWRK3_GET_MCA: /* Get the multicast address table */
1713 while (test_and_set_bit(0, (void *) &lp->lock) != 0); /* Wait for lock to free */
1714 if (lp->shmem_length == IO_ONLY) {
1715 outb(0, EWRK3_IOPR);
1716 outw(PAGE0_HTE, EWRK3_PIR1);
1717 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1718 tmp.addr[i] = inb(EWRK3_DATA);
1720 } else {
1721 outb(0, EWRK3_MPR);
1722 isa_memcpy_fromio(tmp.addr, lp->shmem_base + PAGE0_HTE, (HASH_TABLE_LEN >> 3));
1724 ioc->len = (HASH_TABLE_LEN >> 3);
1725 if (copy_to_user(ioc->data, tmp.addr, ioc->len)) {
1726 status = -EFAULT;
1727 break;
1730 lp->lock = 0; /* Unlock the page register */
1732 break;
1733 case EWRK3_SET_MCA: /* Set a multicast address */
1734 if (capable(CAP_NET_ADMIN)) {
1735 if (copy_from_user(tmp.addr, ioc->data, ETH_ALEN * ioc->len)) {
1736 status = -EFAULT;
1737 break;
1739 set_multicast_list(dev);
1740 } else {
1741 status = -EPERM;
1744 break;
1745 case EWRK3_CLR_MCA: /* Clear all multicast addresses */
1746 if (capable(CAP_NET_ADMIN)) {
1747 set_multicast_list(dev);
1748 } else {
1749 status = -EPERM;
1752 break;
1753 case EWRK3_MCA_EN: /* Enable multicast addressing */
1754 if (capable(CAP_NET_ADMIN)) {
1755 csr = inb(EWRK3_CSR);
1756 csr |= CSR_MCE;
1757 csr &= ~CSR_PME;
1758 outb(csr, EWRK3_CSR);
1759 } else {
1760 status = -EPERM;
1763 break;
1764 case EWRK3_GET_STATS: /* Get the driver statistics */
1765 cli();
1766 ioc->len = sizeof(lp->pktStats);
1767 if (copy_to_user(ioc->data, &lp->pktStats, ioc->len))
1768 status = -EFAULT;
1769 sti();
1771 break;
1772 case EWRK3_CLR_STATS: /* Zero out the driver statistics */
1773 if (capable(CAP_NET_ADMIN)) {
1774 cli();
1775 memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1776 sti();
1777 } else {
1778 status = -EPERM;
1781 break;
1782 case EWRK3_GET_CSR: /* Get the CSR Register contents */
1783 tmp.addr[0] = inb(EWRK3_CSR);
1784 ioc->len = 1;
1785 if (copy_to_user(ioc->data, tmp.addr, ioc->len))
1786 status = -EFAULT;
1787 break;
1788 case EWRK3_SET_CSR: /* Set the CSR Register contents */
1789 if (capable(CAP_NET_ADMIN)) {
1790 if (copy_from_user(tmp.addr, ioc->data, 1)) {
1791 status = -EFAULT;
1792 break;
1794 outb(tmp.addr[0], EWRK3_CSR);
1795 } else {
1796 status = -EPERM;
1799 break;
1800 case EWRK3_GET_EEPROM: /* Get the EEPROM contents */
1801 if (capable(CAP_NET_ADMIN)) {
1802 for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1803 tmp.val[i] = (short) Read_EEPROM(iobase, i);
1805 i = EEPROM_MAX;
1806 tmp.addr[i++] = inb(EWRK3_CMR); /* Config/Management Reg. */
1807 for (j = 0; j < ETH_ALEN; j++) {
1808 tmp.addr[i++] = inb(EWRK3_PAR0 + j);
1810 ioc->len = EEPROM_MAX + 1 + ETH_ALEN;
1811 if (copy_to_user(ioc->data, tmp.addr, ioc->len))
1812 status = -EFAULT;
1813 } else {
1814 status = -EPERM;
1817 break;
1818 case EWRK3_SET_EEPROM: /* Set the EEPROM contents */
1819 if (capable(CAP_NET_ADMIN)) {
1820 if (copy_from_user(tmp.addr, ioc->data, EEPROM_MAX)) {
1821 status = -EFAULT;
1822 break;
1824 for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1825 Write_EEPROM(tmp.val[i], iobase, i);
1827 } else {
1828 status = -EPERM;
1831 break;
1832 case EWRK3_GET_CMR: /* Get the CMR Register contents */
1833 tmp.addr[0] = inb(EWRK3_CMR);
1834 ioc->len = 1;
1835 if (copy_to_user(ioc->data, tmp.addr, ioc->len))
1836 status = -EFAULT;
1837 break;
1838 case EWRK3_SET_TX_CUT_THRU: /* Set TX cut through mode */
1839 if (suser()) {
1840 lp->txc = 1;
1841 } else {
1842 status = -EPERM;
1845 break;
1846 case EWRK3_CLR_TX_CUT_THRU: /* Clear TX cut through mode */
1847 if (suser()) {
1848 lp->txc = 0;
1849 } else {
1850 status = -EPERM;
1853 break;
1854 default:
1855 status = -EOPNOTSUPP;
1858 return status;
1861 #ifdef MODULE
1862 static char devicename[9] =
1863 {0,};
1864 static struct net_device thisEthwrk =
1866 devicename, /* device name is inserted by /linux/drivers/net/net_init.c */
1867 0, 0, 0, 0,
1868 0x300, 5, /* I/O address, IRQ */
1869 0, 0, 0, NULL, ewrk3_probe};
1871 static int io = 0x300; /* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
1872 static int irq = 5; /* or use the insmod io= irq= options */
1874 MODULE_PARM(io, "i");
1875 MODULE_PARM(irq, "i");
1877 int init_module(void)
1879 thisEthwrk.base_addr = io;
1880 thisEthwrk.irq = irq;
1881 if (register_netdev(&thisEthwrk) != 0)
1882 return -EIO;
1883 return 0;
1886 void cleanup_module(void)
1888 unregister_netdev(&thisEthwrk);
1889 if (thisEthwrk.priv) {
1890 kfree(thisEthwrk.priv);
1891 thisEthwrk.priv = NULL;
1893 thisEthwrk.irq = 0;
1895 release_region(thisEthwrk.base_addr, EWRK3_TOTAL_SIZE);
1897 #endif /* MODULE */
1901 * Local variables:
1902 * 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"
1904 * 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"
1905 * End: