File: /usr/src/linux/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 6) run the net startup bits for your new eth?? interface manually
80 (usually /etc/rc.inet[12] at boot time).
81 7) enjoy!
82
83 Note that autoprobing is not allowed in loadable modules - the system is
84 already up and running and you're messing with interrupts.
85
86 To unload a module, turn off the associated interface
87 'ifconfig eth?? down' then 'rmmod ewrk3'.
88
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%.
92
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.
99
100 TO DO:
101 ------
102
103
104 Revision History
105 ----------------
106
107 Version Date Description
108
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
135
136 =========================================================================
137 */
138
139 #include <linux/module.h>
140
141 #include <linux/kernel.h>
142 #include <linux/sched.h>
143 #include <linux/string.h>
144 #include <linux/ptrace.h>
145 #include <linux/errno.h>
146 #include <linux/ioport.h>
147 #include <linux/slab.h>
148 #include <linux/interrupt.h>
149 #include <linux/delay.h>
150 #include <linux/init.h>
151 #include <asm/bitops.h>
152 #include <asm/io.h>
153 #include <asm/dma.h>
154 #include <asm/uaccess.h>
155
156 #include <linux/netdevice.h>
157 #include <linux/etherdevice.h>
158 #include <linux/skbuff.h>
159
160 #include <linux/time.h>
161 #include <linux/types.h>
162 #include <linux/unistd.h>
163 #include <linux/ctype.h>
164
165 #include "ewrk3.h"
166
167 static char version[] __initdata =
168 "ewrk3.c:v0.43a 2001/02/04 davies@maniac.ultranet.com\n";
169
170 #ifdef EWRK3_DEBUG
171 static int ewrk3_debug = EWRK3_DEBUG;
172 #else
173 static int ewrk3_debug = 1;
174 #endif
175
176 #define EWRK3_NDA 0xffe0 /* No Device Address */
177
178 #define PROBE_LENGTH 32
179 #define ETH_PROM_SIG 0xAA5500FFUL
180
181 #ifndef EWRK3_SIGNATURE
182 #define EWRK3_SIGNATURE {"DE203","DE204","DE205",""}
183 #define EWRK3_STRLEN 8
184 #endif
185
186 #ifndef EWRK3_RAM_BASE_ADDRESSES
187 #define EWRK3_RAM_BASE_ADDRESSES {0xc0000,0xd0000,0x00000}
188 #endif
189
190 /*
191 ** Sets up the I/O area for the autoprobe.
192 */
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 */
196
197 #ifndef MAX_NUM_EWRK3S
198 #define MAX_NUM_EWRK3S 21
199 #endif
200
201 #ifndef EWRK3_EISA_IO_PORTS
202 #define EWRK3_EISA_IO_PORTS 0x0c00 /* I/O port base address, slot 0 */
203 #endif
204
205 #ifndef MAX_EISA_SLOTS
206 #define MAX_EISA_SLOTS 16
207 #define EISA_SLOT_INC 0x1000
208 #endif
209
210 #define CRC_POLYNOMIAL_BE 0x04c11db7UL /* Ethernet CRC, big endian */
211 #define CRC_POLYNOMIAL_LE 0xedb88320UL /* Ethernet CRC, little endian */
212
213 #define QUEUE_PKT_TIMEOUT (1*HZ) /* Jiffies */
214
215 /*
216 ** EtherWORKS 3 shared memory window sizes
217 */
218 #define IO_ONLY 0x00
219 #define SHMEM_2K 0x800
220 #define SHMEM_32K 0x8000
221 #define SHMEM_64K 0x10000
222
223 /*
224 ** EtherWORKS 3 IRQ ENABLE/DISABLE
225 */
226 #define ENABLE_IRQs { \
227 icr |= lp->irq_mask;\
228 outb(icr, EWRK3_ICR); /* Enable the IRQs */\
229 }
230
231 #define DISABLE_IRQs { \
232 icr = inb(EWRK3_ICR);\
233 icr &= ~lp->irq_mask;\
234 outb(icr, EWRK3_ICR); /* Disable the IRQs */\
235 }
236
237 /*
238 ** EtherWORKS 3 START/STOP
239 */
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 */\
244 }
245
246 #define STOP_EWRK3 { \
247 csr = (CSR_TXD|CSR_RXD);\
248 outb(csr, EWRK3_CSR); /* Disable the TX and/or RX */\
249 }
250
251 /*
252 ** The EtherWORKS 3 private structure
253 */
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 */
257
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 txc; /* Transmit cut through */
277 u_char *mctbl; /* Pointer to the multicast table */
278 spinlock_t hw_lock;
279 };
280
281 /*
282 ** Force the EtherWORKS 3 card to be in 2kB MODE
283 */
284 #define FORCE_2K_MODE { \
285 shmem_length = SHMEM_2K;\
286 outb(((mem_start - 0x80000) >> 11), EWRK3_MBR);\
287 }
288
289 /*
290 ** Public Functions
291 */
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);
299
300 /*
301 ** Private functions
302 */
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);
308
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);
313
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);
317
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 *));
323
324
325 #ifdef MODULE
326 int init_module(void);
327 void cleanup_module(void);
328 static int autoprobed = 1, loading_module = 1;
329
330 #else
331 static u_char irq[] =
332 {5, 0, 10, 3, 11, 9, 15, 12};
333 static int autoprobed, loading_module;
334
335 #endif /* MODULE */
336
337 static char name[EWRK3_STRLEN + 1];
338 static int num_ewrk3s, num_eth;
339
340 /*
341 ** Miscellaneous defines...
342 */
343 #define INIT_EWRK3 {\
344 outb(EEPROM_INIT, EWRK3_IOPR);\
345 mdelay(1);\
346 }
347
348 int __init ewrk3_probe(struct net_device *dev)
349 {
350 int tmp = num_ewrk3s, status = -ENODEV;
351 u_long iobase = dev->base_addr;
352
353 SET_MODULE_OWNER(dev);
354
355 if ((iobase == 0) && loading_module) {
356 printk("Autoprobing is not supported when loading a module based driver.\n");
357 status = -EIO;
358 } else { /* First probe for the Ethernet */
359 /* Address PROM pattern */
360 isa_probe(dev, iobase);
361 eisa_probe(dev, iobase);
362
363 if ((tmp == num_ewrk3s) && (iobase != 0) && loading_module) {
364 printk("%s: ewrk3_probe() cannot find device at 0x%04lx.\n", dev->name,
365 iobase);
366 }
367 /*
368 ** Walk the device list to check that at least one device
369 ** initialised OK
370 */
371 for (; (dev->priv == NULL) && (dev->next != NULL); dev = dev->next);
372
373 if (dev->priv)
374 status = 0;
375 if (iobase == 0)
376 autoprobed = 1;
377 }
378
379 return status;
380 }
381
382 static int __init
383 ewrk3_hw_init(struct net_device *dev, u_long iobase)
384 {
385 struct ewrk3_private *lp;
386 int i, status = 0;
387 u_long mem_start, shmem_length;
388 u_char cr, cmr, icr, nicsr, lemac, hard_strapped = 0;
389 u_char eeprom_image[EEPROM_MAX], chksum, eisa_cr = 0;
390
391 /*
392 ** Stop the EWRK3. Enable the DBR ROM. Disable interrupts and remote boot.
393 ** This also disables the EISA_ENABLE bit in the EISA Control Register.
394 */
395 if (iobase > 0x400)
396 eisa_cr = inb(EISA_CR);
397 INIT_EWRK3;
398
399 nicsr = inb(EWRK3_CSR);
400
401 icr = inb(EWRK3_ICR);
402 icr &= 0x70;
403 outb(icr, EWRK3_ICR); /* Disable all the IRQs */
404
405 if (nicsr == (CSR_TXD | CSR_RXD)) {
406
407 /* Check that the EEPROM is alive and well and not living on Pluto... */
408 for (chksum = 0, i = 0; i < EEPROM_MAX; i += 2) {
409 union {
410 short val;
411 char c[2];
412 } tmp;
413
414 tmp.val = (short) Read_EEPROM(iobase, (i >> 1));
415 eeprom_image[i] = tmp.c[0];
416 eeprom_image[i + 1] = tmp.c[1];
417 chksum += eeprom_image[i] + eeprom_image[i + 1];
418 }
419
420 if (chksum != 0) { /* Bad EEPROM Data! */
421 printk("%s: Device has a bad on-board EEPROM.\n", dev->name);
422 status = -ENXIO;
423 } else {
424 EthwrkSignature(name, eeprom_image);
425 if (*name != '\0') { /* found a EWRK3 device */
426 dev->base_addr = iobase;
427
428 if (iobase > 0x400) {
429 outb(eisa_cr, EISA_CR); /* Rewrite the EISA CR */
430 }
431 lemac = eeprom_image[EEPROM_CHIPVER];
432 cmr = inb(EWRK3_CMR);
433
434 if (((lemac == LeMAC) && ((cmr & CMR_NO_EEPROM) != CMR_NO_EEPROM)) ||
435 ((lemac == LeMAC2) && !(cmr & CMR_HS))) {
436 printk("%s: %s at %#4lx", dev->name, name, iobase);
437 hard_strapped = 1;
438 } else if ((iobase & 0x0fff) == EWRK3_EISA_IO_PORTS) {
439 /* EISA slot address */
440 printk("%s: %s at %#4lx (EISA slot %ld)",
441 dev->name, name, iobase, ((iobase >> 12) & 0x0f));
442 } else { /* ISA port address */
443 printk("%s: %s at %#4lx", dev->name, name, iobase);
444 }
445
446 if (!status) {
447 printk(", h/w address ");
448 if (lemac != LeMAC2)
449 DevicePresent(iobase); /* need after EWRK3_INIT */
450 status = get_hw_addr(dev, eeprom_image, lemac);
451 for (i = 0; i < ETH_ALEN - 1; i++) { /* get the ethernet addr. */
452 printk("%2.2x:", dev->dev_addr[i]);
453 }
454 printk("%2.2x,\n", dev->dev_addr[i]);
455
456 if (status) {
457 printk(" which has an EEPROM CRC error.\n");
458 status = -ENXIO;
459 } else {
460 if (lemac == LeMAC2) { /* Special LeMAC2 CMR things */
461 cmr &= ~(CMR_RA | CMR_WB | CMR_LINK | CMR_POLARITY | CMR_0WS);
462 if (eeprom_image[EEPROM_MISC0] & READ_AHEAD)
463 cmr |= CMR_RA;
464 if (eeprom_image[EEPROM_MISC0] & WRITE_BEHIND)
465 cmr |= CMR_WB;
466 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_POL)
467 cmr |= CMR_POLARITY;
468 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_LINK)
469 cmr |= CMR_LINK;
470 if (eeprom_image[EEPROM_MISC0] & _0WS_ENA)
471 cmr |= CMR_0WS;
472 }
473 if (eeprom_image[EEPROM_SETUP] & SETUP_DRAM)
474 cmr |= CMR_DRAM;
475 outb(cmr, EWRK3_CMR);
476
477 cr = inb(EWRK3_CR); /* Set up the Control Register */
478 cr |= eeprom_image[EEPROM_SETUP] & SETUP_APD;
479 if (cr & SETUP_APD)
480 cr |= eeprom_image[EEPROM_SETUP] & SETUP_PS;
481 cr |= eeprom_image[EEPROM_MISC0] & FAST_BUS;
482 cr |= eeprom_image[EEPROM_MISC0] & ENA_16;
483 outb(cr, EWRK3_CR);
484
485 /*
486 ** Determine the base address and window length for the EWRK3
487 ** RAM from the memory base register.
488 */
489 mem_start = inb(EWRK3_MBR);
490 shmem_length = 0;
491 if (mem_start != 0) {
492 if ((mem_start >= 0x0a) && (mem_start <= 0x0f)) {
493 mem_start *= SHMEM_64K;
494 shmem_length = SHMEM_64K;
495 } else if ((mem_start >= 0x14) && (mem_start <= 0x1f)) {
496 mem_start *= SHMEM_32K;
497 shmem_length = SHMEM_32K;
498 } else if ((mem_start >= 0x40) && (mem_start <= 0xff)) {
499 mem_start = mem_start * SHMEM_2K + 0x80000;
500 shmem_length = SHMEM_2K;
501 } else {
502 status = -ENXIO;
503 }
504 }
505 /*
506 ** See the top of this source code for comments about
507 ** uncommenting this line.
508 */
509 /* FORCE_2K_MODE; */
510
511 if (!status) {
512 if (hard_strapped) {
513 printk(" is hard strapped.\n");
514 } else if (mem_start) {
515 printk(" has a %dk RAM window", (int) (shmem_length >> 10));
516 printk(" at 0x%.5lx", mem_start);
517 } else {
518 printk(" is in I/O only mode");
519 }
520
521 /* private area & initialise */
522 dev->priv = (void *) kmalloc(sizeof(struct ewrk3_private),
523 GFP_KERNEL);
524 if (dev->priv == NULL) {
525 return -ENOMEM;
526 }
527 lp = (struct ewrk3_private *) dev->priv;
528 memset(dev->priv, 0, sizeof(struct ewrk3_private));
529 lp->shmem_base = mem_start;
530 lp->shmem_length = shmem_length;
531 lp->lemac = lemac;
532 lp->hard_strapped = hard_strapped;
533 spin_lock_init(&lp->hw_lock);
534
535 lp->mPage = 64;
536 if (cmr & CMR_DRAM)
537 lp->mPage <<= 1; /* 2 DRAMS on module */
538
539 sprintf(lp->adapter_name, "%s (%s)", name, dev->name);
540 request_region(iobase, EWRK3_TOTAL_SIZE, lp->adapter_name);
541
542 lp->irq_mask = ICR_TNEM | ICR_TXDM | ICR_RNEM | ICR_RXDM;
543
544 if (!hard_strapped) {
545 /*
546 ** Enable EWRK3 board interrupts for autoprobing
547 */
548 icr |= ICR_IE; /* Enable interrupts */
549 outb(icr, EWRK3_ICR);
550
551 /* The DMA channel may be passed in on this parameter. */
552 dev->dma = 0;
553
554 /* To auto-IRQ we enable the initialization-done and DMA err,
555 interrupts. For now we will always get a DMA error. */
556 if (dev->irq < 2) {
557 #ifndef MODULE
558 u_char irqnum;
559
560 autoirq_setup(0);
561
562 /*
563 ** Trigger a TNE interrupt.
564 */
565 icr |= ICR_TNEM;
566 outb(1, EWRK3_TDQ); /* Write to the TX done queue */
567 outb(icr, EWRK3_ICR); /* Unmask the TXD interrupt */
568
569 irqnum = irq[((icr & IRQ_SEL) >> 4)];
570
571 dev->irq = autoirq_report(1);
572 if ((dev->irq) && (irqnum == dev->irq)) {
573 printk(" and uses IRQ%d.\n", dev->irq);
574 } else {
575 if (!dev->irq) {
576 printk(" and failed to detect IRQ line.\n");
577 } else if ((irqnum == 1) && (lemac == LeMAC2)) {
578 printk(" and an illegal IRQ line detected.\n");
579 } else {
580 printk(", but incorrect IRQ line detected.\n");
581 }
582 status = -ENXIO;
583 }
584
585 DISABLE_IRQs; /* Mask all interrupts */
586
587 #endif /* MODULE */
588 } else {
589 printk(" and requires IRQ%d.\n", dev->irq);
590 }
591 }
592 if (status)
593 release_region(iobase, EWRK3_TOTAL_SIZE);
594 } else {
595 status = -ENXIO;
596 }
597 }
598 }
599 } else {
600 status = -ENXIO;
601 }
602 }
603
604 if (!status) {
605 if (ewrk3_debug > 1) {
606 printk(version);
607 }
608 /* The EWRK3-specific entries in the device structure. */
609 dev->open = ewrk3_open;
610 dev->hard_start_xmit = ewrk3_queue_pkt;
611 dev->stop = ewrk3_close;
612 dev->get_stats = ewrk3_get_stats;
613 dev->set_multicast_list = set_multicast_list;
614 dev->do_ioctl = ewrk3_ioctl;
615 dev->tx_timeout = ewrk3_timeout;
616 dev->watchdog_timeo = QUEUE_PKT_TIMEOUT;
617
618 dev->mem_start = 0;
619
620 /* Fill in the generic field of the device structure. */
621 ether_setup(dev);
622 }
623 } else {
624 status = -ENXIO;
625 }
626 return status;
627 }
628
629
630 static int ewrk3_open(struct net_device *dev)
631 {
632 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
633 u_long iobase = dev->base_addr;
634 int i, status = 0;
635 u_char icr, csr;
636
637 /*
638 ** Stop the TX and RX...
639 */
640 STOP_EWRK3;
641
642 if (!lp->hard_strapped) {
643 if (request_irq(dev->irq, (void *) ewrk3_interrupt, 0, "ewrk3", dev)) {
644 printk("ewrk3_open(): Requested IRQ%d is busy\n", dev->irq);
645 status = -EAGAIN;
646 } else {
647
648 /*
649 ** Re-initialize the EWRK3...
650 */
651 ewrk3_init(dev);
652
653 if (ewrk3_debug > 1) {
654 printk("%s: ewrk3 open with irq %d\n", dev->name, dev->irq);
655 printk(" physical address: ");
656 for (i = 0; i < 5; i++) {
657 printk("%2.2x:", (u_char) dev->dev_addr[i]);
658 }
659 printk("%2.2x\n", (u_char) dev->dev_addr[i]);
660 if (lp->shmem_length == 0) {
661 printk(" no shared memory, I/O only mode\n");
662 } else {
663 printk(" start of shared memory: 0x%08lx\n", lp->shmem_base);
664 printk(" window length: 0x%04lx\n", lp->shmem_length);
665 }
666 printk(" # of DRAMS: %d\n", ((inb(EWRK3_CMR) & 0x02) ? 2 : 1));
667 printk(" csr: 0x%02x\n", inb(EWRK3_CSR));
668 printk(" cr: 0x%02x\n", inb(EWRK3_CR));
669 printk(" icr: 0x%02x\n", inb(EWRK3_ICR));
670 printk(" cmr: 0x%02x\n", inb(EWRK3_CMR));
671 printk(" fmqc: 0x%02x\n", inb(EWRK3_FMQC));
672 }
673 netif_start_queue(dev);
674 /*
675 ** Unmask EWRK3 board interrupts
676 */
677 icr = inb(EWRK3_ICR);
678 ENABLE_IRQs;
679
680 }
681 } else {
682 printk(KERN_ERR "%s: ewrk3 available for hard strapped set up only.\n", dev->name);
683 printk(KERN_ERR " Run the 'ewrk3setup' utility or remove the hard straps.\n");
684 return -EINVAL;
685 }
686
687 return status;
688 }
689
690 /*
691 ** Initialize the EtherWORKS 3 operating conditions
692 */
693 static void ewrk3_init(struct net_device *dev)
694 {
695 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
696 u_char csr, page;
697 u_long iobase = dev->base_addr;
698
699 /*
700 ** Enable any multicasts
701 */
702 set_multicast_list(dev);
703
704 /*
705 ** Clean out any remaining entries in all the queues here
706 */
707 while (inb(EWRK3_TQ));
708 while (inb(EWRK3_TDQ));
709 while (inb(EWRK3_RQ));
710 while (inb(EWRK3_FMQ));
711
712 /*
713 ** Write a clean free memory queue
714 */
715 for (page = 1; page < lp->mPage; page++) { /* Write the free page numbers */
716 outb(page, EWRK3_FMQ); /* to the Free Memory Queue */
717 }
718
719 START_EWRK3; /* Enable the TX and/or RX */
720 }
721
722 /*
723 * Transmit timeout
724 */
725
726 static void ewrk3_timeout(struct net_device *dev)
727 {
728 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
729 u_char icr, csr;
730 u_long iobase = dev->base_addr;
731
732 if (!lp->hard_strapped)
733 {
734 printk(KERN_WARNING"%s: transmit timed/locked out, status %04x, resetting.\n",
735 dev->name, inb(EWRK3_CSR));
736
737 /*
738 ** Mask all board interrupts
739 */
740 DISABLE_IRQs;
741
742 /*
743 ** Stop the TX and RX...
744 */
745 STOP_EWRK3;
746
747 ewrk3_init(dev);
748
749 /*
750 ** Unmask EWRK3 board interrupts
751 */
752 ENABLE_IRQs;
753
754 dev->trans_start = jiffies;
755 netif_wake_queue(dev);
756 }
757 }
758
759 /*
760 ** Writes a socket buffer to the free page queue
761 */
762 static int ewrk3_queue_pkt (struct sk_buff *skb, struct net_device *dev)
763 {
764 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
765 u_long iobase = dev->base_addr;
766 u_long buf = 0;
767 u_char icr;
768 u_char page;
769
770 spin_lock_irq (&lp->hw_lock);
771 DISABLE_IRQs;
772
773 /* if no resources available, exit, request packet be queued */
774 if (inb (EWRK3_FMQC) == 0) {
775 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): No free resources...\n",
776 dev->name);
777 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): CSR: %02x ICR: %02x FMQC: %02x\n",
778 dev->name, inb (EWRK3_CSR), inb (EWRK3_ICR),
779 inb (EWRK3_FMQC));
780 goto err_out;
781 }
782
783 /*
784 ** Get a free page from the FMQ
785 */
786 if ((page = inb (EWRK3_FMQ)) >= lp->mPage) {
787 printk ("ewrk3_queue_pkt(): Invalid free memory page (%d).\n",
788 (u_char) page);
789 goto err_out;
790 }
791
792
793 /*
794 ** Set up shared memory window and pointer into the window
795 */
796 if (lp->shmem_length == IO_ONLY) {
797 outb (page, EWRK3_IOPR);
798 } else if (lp->shmem_length == SHMEM_2K) {
799 buf = lp->shmem_base;
800 outb (page, EWRK3_MPR);
801 } else if (lp->shmem_length == SHMEM_32K) {
802 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
803 outb ((page >> 4), EWRK3_MPR);
804 } else if (lp->shmem_length == SHMEM_64K) {
805 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
806 outb ((page >> 5), EWRK3_MPR);
807 } else {
808 printk (KERN_ERR "%s: Oops - your private data area is hosed!\n",
809 dev->name);
810 BUG ();
811 }
812
813 /*
814 ** Set up the buffer control structures and copy the data from
815 ** the socket buffer to the shared memory .
816 */
817 if (lp->shmem_length == IO_ONLY) {
818 int i;
819 u_char *p = skb->data;
820 outb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), EWRK3_DATA);
821 outb ((char) (skb->len & 0xff), EWRK3_DATA);
822 outb ((char) ((skb->len >> 8) & 0xff), EWRK3_DATA);
823 outb ((char) 0x04, EWRK3_DATA);
824 for (i = 0; i < skb->len; i++) {
825 outb (*p++, EWRK3_DATA);
826 }
827 outb (page, EWRK3_TQ); /* Start sending pkt */
828 } else {
829 isa_writeb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), buf); /* ctrl byte */
830 buf += 1;
831 isa_writeb ((char) (skb->len & 0xff), buf); /* length (16 bit xfer) */
832 buf += 1;
833 if (lp->txc) {
834 isa_writeb ((char)
835 (((skb->len >> 8) & 0xff) | XCT), buf);
836 buf += 1;
837 isa_writeb (0x04, buf); /* index byte */
838 buf += 1;
839 isa_writeb (0x00, (buf + skb->len)); /* Write the XCT flag */
840 isa_memcpy_toio (buf, skb->data, PRELOAD); /* Write PRELOAD bytes */
841 outb (page, EWRK3_TQ); /* Start sending pkt */
842 isa_memcpy_toio (buf + PRELOAD,
843 skb->data + PRELOAD,
844 skb->len - PRELOAD);
845 isa_writeb (0xff, (buf + skb->len)); /* Write the XCT flag */
846 } else {
847 isa_writeb ((char)
848 ((skb->len >> 8) & 0xff), buf);
849 buf += 1;
850 isa_writeb (0x04, buf); /* index byte */
851 buf += 1;
852 isa_memcpy_toio (buf, skb->data, skb->len); /* Write data bytes */
853 outb (page, EWRK3_TQ); /* Start sending pkt */
854 }
855 }
856
857 ENABLE_IRQs;
858 spin_unlock_irq (&lp->hw_lock);
859
860 lp->stats.tx_bytes += skb->len;
861 dev->trans_start = jiffies;
862 dev_kfree_skb (skb);
863
864 /* Check for free resources: stop Tx queue if there are none */
865 if (inb (EWRK3_FMQC) == 0)
866 netif_stop_queue (dev);
867
868 return 0;
869
870 err_out:
871 ENABLE_IRQs;
872 spin_unlock_irq (&lp->hw_lock);
873 return 1;
874 }
875
876 /*
877 ** The EWRK3 interrupt handler.
878 */
879 static void ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
880 {
881 struct net_device *dev = dev_id;
882 struct ewrk3_private *lp;
883 u_long iobase;
884 u_char icr, cr, csr;
885
886 lp = (struct ewrk3_private *) dev->priv;
887 iobase = dev->base_addr;
888
889 /* get the interrupt information */
890 csr = inb(EWRK3_CSR);
891
892 /*
893 ** Mask the EWRK3 board interrupts and turn on the LED
894 */
895 spin_lock(&lp->hw_lock);
896 DISABLE_IRQs;
897
898 cr = inb(EWRK3_CR);
899 cr |= CR_LED;
900 outb(cr, EWRK3_CR);
901
902 if (csr & CSR_RNE) /* Rx interrupt (packet[s] arrived) */
903 ewrk3_rx(dev);
904
905 if (csr & CSR_TNE) /* Tx interrupt (packet sent) */
906 ewrk3_tx(dev);
907
908 /*
909 ** Now deal with the TX/RX disable flags. These are set when there
910 ** are no more resources. If resources free up then enable these
911 ** interrupts, otherwise mask them - failure to do this will result
912 ** in the system hanging in an interrupt loop.
913 */
914 if (inb(EWRK3_FMQC)) { /* any resources available? */
915 lp->irq_mask |= ICR_TXDM | ICR_RXDM; /* enable the interrupt source */
916 csr &= ~(CSR_TXD | CSR_RXD); /* ensure restart of a stalled TX or RX */
917 outb(csr, EWRK3_CSR);
918 netif_wake_queue(dev);
919 } else {
920 lp->irq_mask &= ~(ICR_TXDM | ICR_RXDM); /* disable the interrupt source */
921 }
922
923 /* Unmask the EWRK3 board interrupts and turn off the LED */
924 cr &= ~CR_LED;
925 outb(cr, EWRK3_CR);
926 ENABLE_IRQs;
927 spin_unlock(&lp->hw_lock);
928 }
929
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 ((*(s32 *) & p[0] == -1) && (*(s16 *) & p[4] == -1)) {
1009 lp->pktStats.broadcast++;
1010 } else {
1011 lp->pktStats.multicast++;
1012 }
1013 } else if ((*(s32 *) & p[0] == *(s32 *) & dev->dev_addr[0]) &&
1014 (*(s16 *) & p[4] == *(s16 *) & dev->dev_addr[4])) {
1015 lp->pktStats.unicast++;
1016 }
1017 lp->pktStats.bins[0]++; /* Duplicates stats.rx_packets */
1018 if (lp->pktStats.bins[0] == 0) { /* Reset counters */
1019 memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1020 }
1021 /*
1022 ** Notify the upper protocol layers that there is another
1023 ** packet to handle
1024 */
1025 skb->protocol = eth_type_trans(skb, dev);
1026 netif_rx(skb);
1027
1028 /*
1029 ** Update stats
1030 */
1031 dev->last_rx = jiffies;
1032 lp->stats.rx_packets++;
1033 lp->stats.rx_bytes += pkt_len;
1034 } else {
1035 printk("%s: Insufficient memory; nuking packet.\n", dev->name);
1036 lp->stats.rx_dropped++; /* Really, deferred. */
1037 break;
1038 }
1039 }
1040 }
1041 /*
1042 ** Return the received buffer to the free memory queue
1043 */
1044 outb(page, EWRK3_FMQ);
1045 } else {
1046 printk("ewrk3_rx(): Illegal page number, page %d\n", page);
1047 printk("ewrk3_rx(): CSR: %02x ICR: %02x FMQC: %02x\n", inb(EWRK3_CSR), inb(EWRK3_ICR), inb(EWRK3_FMQC));
1048 }
1049 }
1050 return status;
1051 }
1052
1053 /*
1054 ** Buffer sent - check for TX buffer errors.
1055 */
1056 static int ewrk3_tx(struct net_device *dev)
1057 {
1058 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1059 u_long iobase = dev->base_addr;
1060 u_char tx_status;
1061
1062 while ((tx_status = inb(EWRK3_TDQ)) > 0) { /* Whilst there's old buffers */
1063 if (tx_status & T_VSTS) { /* The status is valid */
1064 if (tx_status & T_TXE) {
1065 lp->stats.tx_errors++;
1066 if (tx_status & T_NCL)
1067 lp->stats.tx_carrier_errors++;
1068 if (tx_status & T_LCL)
1069 lp->stats.tx_window_errors++;
1070 if (tx_status & T_CTU) {
1071 if ((tx_status & T_COLL) ^ T_XUR) {
1072 lp->pktStats.tx_underruns++;
1073 } else {
1074 lp->pktStats.excessive_underruns++;
1075 }
1076 } else if (tx_status & T_COLL) {
1077 if ((tx_status & T_COLL) ^ T_XCOLL) {
1078 lp->stats.collisions++;
1079 } else {
1080 lp->pktStats.excessive_collisions++;
1081 }
1082 }
1083 } else {
1084 lp->stats.tx_packets++;
1085 }
1086 }
1087 }
1088
1089 return 0;
1090 }
1091
1092 static int ewrk3_close(struct net_device *dev)
1093 {
1094 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1095 u_long iobase = dev->base_addr;
1096 u_char icr, csr;
1097
1098 netif_stop_queue(dev);
1099
1100 if (ewrk3_debug > 1) {
1101 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1102 dev->name, inb(EWRK3_CSR));
1103 }
1104 /*
1105 ** We stop the EWRK3 here... mask interrupts and stop TX & RX
1106 */
1107 DISABLE_IRQs;
1108
1109 STOP_EWRK3;
1110
1111 /*
1112 ** Clean out the TX and RX queues here (note that one entry
1113 ** may get added to either the TXD or RX queues if the TX or RX
1114 ** just starts processing a packet before the STOP_EWRK3 command
1115 ** is received. This will be flushed in the ewrk3_open() call).
1116 */
1117 while (inb(EWRK3_TQ));
1118 while (inb(EWRK3_TDQ));
1119 while (inb(EWRK3_RQ));
1120
1121 if (!lp->hard_strapped) {
1122 free_irq(dev->irq, dev);
1123 }
1124 return 0;
1125 }
1126
1127 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev)
1128 {
1129 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1130
1131 /* Null body since there is no framing error counter */
1132 return &lp->stats;
1133 }
1134
1135 /*
1136 ** Set or clear the multicast filter for this adapter.
1137 */
1138 static void set_multicast_list(struct net_device *dev)
1139 {
1140 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1141 u_long iobase = dev->base_addr;
1142 u_char csr;
1143
1144 csr = inb(EWRK3_CSR);
1145
1146 if (lp->shmem_length == IO_ONLY) {
1147 lp->mctbl = (char *) PAGE0_HTE;
1148 } else {
1149 lp->mctbl = (char *) (lp->shmem_base + PAGE0_HTE);
1150 }
1151
1152 csr &= ~(CSR_PME | CSR_MCE);
1153 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
1154 csr |= CSR_PME;
1155 outb(csr, EWRK3_CSR);
1156 } else {
1157 SetMulticastFilter(dev);
1158 csr |= CSR_MCE;
1159 outb(csr, EWRK3_CSR);
1160 }
1161 }
1162
1163 /*
1164 ** Calculate the hash code and update the logical address filter
1165 ** from a list of ethernet multicast addresses.
1166 ** Little endian crc one liner from Matt Thomas, DEC.
1167 **
1168 ** Note that when clearing the table, the broadcast bit must remain asserted
1169 ** to receive broadcast messages.
1170 */
1171 static void SetMulticastFilter(struct net_device *dev)
1172 {
1173 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1174 struct dev_mc_list *dmi = dev->mc_list;
1175 u_long iobase = dev->base_addr;
1176 int i;
1177 char *addrs, j, bit, byte;
1178 short *p = (short *) lp->mctbl;
1179 u16 hashcode;
1180 s32 crc, poly = CRC_POLYNOMIAL_LE;
1181
1182 spin_lock_irq(&lp->hw_lock);
1183
1184 if (lp->shmem_length == IO_ONLY) {
1185 outb(0, EWRK3_IOPR);
1186 outw(EEPROM_OFFSET(lp->mctbl), EWRK3_PIR1);
1187 } else {
1188 outb(0, EWRK3_MPR);
1189 }
1190
1191 if (dev->flags & IFF_ALLMULTI) {
1192 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1193 if (lp->shmem_length == IO_ONLY) {
1194 outb(0xff, EWRK3_DATA);
1195 } else { /* memset didn't work here */
1196 isa_writew(0xffff, (int) p);
1197 p++;
1198 i++;
1199 }
1200 }
1201 } else {
1202 /* Clear table except for broadcast bit */
1203 if (lp->shmem_length == IO_ONLY) {
1204 for (i = 0; i < (HASH_TABLE_LEN >> 4) - 1; i++) {
1205 outb(0x00, EWRK3_DATA);
1206 }
1207 outb(0x80, EWRK3_DATA);
1208 i++; /* insert the broadcast bit */
1209 for (; i < (HASH_TABLE_LEN >> 3); i++) {
1210 outb(0x00, EWRK3_DATA);
1211 }
1212 } else {
1213 isa_memset_io((int) lp->mctbl, 0, (HASH_TABLE_LEN >> 3));
1214 isa_writeb(0x80, (int) (lp->mctbl + (HASH_TABLE_LEN >> 4) - 1));
1215 }
1216
1217 /* Update table */
1218 for (i = 0; i < dev->mc_count; i++) { /* for each address in the list */
1219 addrs = dmi->dmi_addr;
1220 dmi = dmi->next;
1221 if ((*addrs & 0x01) == 1) { /* multicast address? */
1222 crc = 0xffffffff; /* init CRC for each address */
1223 for (byte = 0; byte < ETH_ALEN; byte++) { /* for each address byte */
1224 /* process each address bit */
1225 for (bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
1226 crc = (crc >> 1) ^ (((crc ^ bit) & 0x01) ? poly : 0);
1227 }
1228 }
1229 hashcode = crc & ((1 << 9) - 1); /* hashcode is 9 LSb of CRC */
1230
1231 byte = hashcode >> 3; /* bit[3-8] -> byte in filter */
1232 bit = 1 << (hashcode & 0x07); /* bit[0-2] -> bit in byte */
1233
1234 if (lp->shmem_length == IO_ONLY) {
1235 u_char tmp;
1236
1237 outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1238 tmp = inb(EWRK3_DATA);
1239 tmp |= bit;
1240 outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1241 outb(tmp, EWRK3_DATA);
1242 } else {
1243 isa_writeb(isa_readb((int)(lp->mctbl + byte)) | bit, (int)(lp->mctbl + byte));
1244 }
1245 }
1246 }
1247 }
1248
1249 spin_unlock_irq(&lp->hw_lock);
1250 }
1251
1252 /*
1253 ** ISA bus I/O device probe
1254 */
1255 static void __init isa_probe(struct net_device *dev, u_long ioaddr)
1256 {
1257 int i = num_ewrk3s, maxSlots;
1258 u_long iobase;
1259
1260 if (!ioaddr && autoprobed)
1261 return; /* Been here before ! */
1262 if (ioaddr >= 0x400)
1263 return; /* Not ISA */
1264
1265 if (ioaddr == 0) { /* Autoprobing */
1266 iobase = EWRK3_IO_BASE; /* Get the first slot address */
1267 maxSlots = 24;
1268 } else { /* Probe a specific location */
1269 iobase = ioaddr;
1270 maxSlots = i + 1;
1271 }
1272
1273 for (; (i < maxSlots) && (dev != NULL); iobase += EWRK3_IOP_INC, i++) {
1274 if (!check_region(iobase, EWRK3_TOTAL_SIZE)) {
1275 if (DevicePresent(iobase) == 0) {
1276 if ((dev = alloc_device(dev, iobase)) != NULL) {
1277 if (ewrk3_hw_init(dev, iobase) == 0) {
1278 num_ewrk3s++;
1279 }
1280 num_eth++;
1281 }
1282 }
1283 } else if (autoprobed) {
1284 printk("%s: region already allocated at 0x%04lx.\n", dev->name, iobase);
1285 }
1286 }
1287
1288 return;
1289 }
1290
1291 /*
1292 ** EISA bus I/O device probe. Probe from slot 1 since slot 0 is usually
1293 ** the motherboard.
1294 */
1295 static void __init eisa_probe(struct net_device *dev, u_long ioaddr)
1296 {
1297 int i, maxSlots;
1298 u_long iobase;
1299 char name[EWRK3_STRLEN];
1300
1301 if (!ioaddr && autoprobed)
1302 return; /* Been here before ! */
1303 if (ioaddr < 0x1000)
1304 return; /* Not EISA */
1305
1306 if (ioaddr == 0) { /* Autoprobing */
1307 iobase = EISA_SLOT_INC; /* Get the first slot address */
1308 i = 1;
1309 maxSlots = MAX_EISA_SLOTS;
1310 } else { /* Probe a specific location */
1311 iobase = ioaddr;
1312 i = (ioaddr >> 12);
1313 maxSlots = i + 1;
1314 }
1315
1316 for (i = 1; (i < maxSlots) && (dev != NULL); i++, iobase += EISA_SLOT_INC) {
1317 if (EISA_signature(name, EISA_ID) == 0) {
1318 if (!check_region(iobase, EWRK3_TOTAL_SIZE)) {
1319 if (DevicePresent(iobase) == 0) {
1320 if ((dev = alloc_device(dev, iobase)) != NULL) {
1321 if (ewrk3_hw_init(dev, iobase) == 0) {
1322 num_ewrk3s++;
1323 }
1324 num_eth++;
1325 }
1326 }
1327 } else if (autoprobed) {
1328 printk("%s: region already allocated at 0x%04lx.\n", dev->name, iobase);
1329 }
1330 }
1331 }
1332
1333 return;
1334 }
1335
1336 /*
1337 ** Search the entire 'eth' device list for a fixed probe. If a match isn't
1338 ** found then check for an autoprobe or unused device location. If they
1339 ** are not available then insert a new device structure at the end of
1340 ** the current list.
1341 */
1342 static struct net_device * __init alloc_device(struct net_device *dev, u_long iobase)
1343 {
1344 struct net_device *adev = NULL;
1345 int fixed = 0, new_dev = 0;
1346
1347 num_eth = ewrk3_dev_index(dev->name);
1348 if (loading_module)
1349 return dev;
1350
1351 while (1) {
1352 if (((dev->base_addr == EWRK3_NDA) || (dev->base_addr == 0)) && !adev) {
1353 adev = dev;
1354 } else if ((dev->priv == NULL) && (dev->base_addr == iobase)) {
1355 fixed = 1;
1356 } else {
1357 if (dev->next == NULL) {
1358 new_dev = 1;
1359 } else if (strncmp(dev->next->name, "eth", 3) != 0) {
1360 new_dev = 1;
1361 }
1362 }
1363 if ((dev->next == NULL) || new_dev || fixed)
1364 break;
1365 dev = dev->next;
1366 num_eth++;
1367 }
1368 if (adev && !fixed) {
1369 dev = adev;
1370 num_eth = ewrk3_dev_index(dev->name);
1371 new_dev = 0;
1372 }
1373 if (((dev->next == NULL) &&
1374 ((dev->base_addr != EWRK3_NDA) && (dev->base_addr != 0)) && !fixed) ||
1375 new_dev) {
1376 num_eth++; /* New device */
1377 dev = insert_device(dev, iobase, ewrk3_probe);
1378 }
1379 return dev;
1380 }
1381
1382 /*
1383 ** If at end of eth device list and can't use current entry, malloc
1384 ** one up. If memory could not be allocated, print an error message.
1385 */
1386 static struct net_device * __init
1387 insert_device(struct net_device *dev, u_long iobase, int (*init) (struct net_device *))
1388 {
1389 struct net_device *new;
1390
1391 new = (struct net_device *) kmalloc(sizeof(struct net_device) + 8, GFP_KERNEL);
1392 if (new == NULL) {
1393 printk("eth%d: Device not initialised, insufficient memory\n", num_eth);
1394 return NULL;
1395 } else {
1396 new->next = dev->next;
1397 dev->next = new;
1398 dev = dev->next; /* point to the new device */
1399 if (num_eth > 9999) {
1400 sprintf(dev->name, "eth????"); /* New device name */
1401 } else {
1402 sprintf(dev->name, "eth%d", num_eth); /* New device name */
1403 }
1404 dev->base_addr = iobase; /* assign the io address */
1405 dev->init = init; /* initialisation routine */
1406 }
1407
1408 return dev;
1409 }
1410
1411 static int __init
1412 ewrk3_dev_index(char *s)
1413 {
1414 int i = 0, j = 0;
1415
1416 for (; *s; s++) {
1417 if (isdigit(*s)) {
1418 j = 1;
1419 i = (i * 10) + (*s - '0');
1420 } else if (j)
1421 break;
1422 }
1423
1424 return i;
1425 }
1426
1427 /*
1428 ** Read the EWRK3 EEPROM using this routine
1429 */
1430 static int Read_EEPROM(u_long iobase, u_char eaddr)
1431 {
1432 int i;
1433
1434 outb((eaddr & 0x3f), EWRK3_PIR1); /* set up 6 bits of address info */
1435 outb(EEPROM_RD, EWRK3_IOPR); /* issue read command */
1436 for (i = 0; i < 5000; i++)
1437 inb(EWRK3_CSR); /* wait 1msec */
1438
1439 return inw(EWRK3_EPROM1); /* 16 bits data return */
1440 }
1441
1442 /*
1443 ** Write the EWRK3 EEPROM using this routine
1444 */
1445 static int Write_EEPROM(short data, u_long iobase, u_char eaddr)
1446 {
1447 int i;
1448
1449 outb(EEPROM_WR_EN, EWRK3_IOPR); /* issue write enable command */
1450 for (i = 0; i < 5000; i++)
1451 inb(EWRK3_CSR); /* wait 1msec */
1452 outw(data, EWRK3_EPROM1); /* write data to register */
1453 outb((eaddr & 0x3f), EWRK3_PIR1); /* set up 6 bits of address info */
1454 outb(EEPROM_WR, EWRK3_IOPR); /* issue write command */
1455 for (i = 0; i < 75000; i++)
1456 inb(EWRK3_CSR); /* wait 15msec */
1457 outb(EEPROM_WR_DIS, EWRK3_IOPR); /* issue write disable command */
1458 for (i = 0; i < 5000; i++)
1459 inb(EWRK3_CSR); /* wait 1msec */
1460
1461 return 0;
1462 }
1463
1464 /*
1465 ** Look for a particular board name in the on-board EEPROM.
1466 */
1467 static void __init EthwrkSignature(char *name, char *eeprom_image)
1468 {
1469 u_long i, j, k;
1470 char *signatures[] = EWRK3_SIGNATURE;
1471
1472 strcpy(name, "");
1473 for (i = 0; *signatures[i] != '\0' && *name == '\0'; i++) {
1474 for (j = EEPROM_PNAME7, k = 0; j <= EEPROM_PNAME0 && k < strlen(signatures[i]); j++) {
1475 if (signatures[i][k] == eeprom_image[j]) { /* track signature */
1476 k++;
1477 } else { /* lost signature; begin search again */
1478 k = 0;
1479 }
1480 }
1481 if (k == strlen(signatures[i])) {
1482 for (k = 0; k < EWRK3_STRLEN; k++) {
1483 name[k] = eeprom_image[EEPROM_PNAME7 + k];
1484 name[EWRK3_STRLEN] = '\0';
1485 }
1486 }
1487 }
1488
1489 return; /* return the device name string */
1490 }
1491
1492 /*
1493 ** Look for a special sequence in the Ethernet station address PROM that
1494 ** is common across all EWRK3 products.
1495 **
1496 ** Search the Ethernet address ROM for the signature. Since the ROM address
1497 ** counter can start at an arbitrary point, the search must include the entire
1498 ** probe sequence length plus the (length_of_the_signature - 1).
1499 ** Stop the search IMMEDIATELY after the signature is found so that the
1500 ** PROM address counter is correctly positioned at the start of the
1501 ** ethernet address for later read out.
1502 */
1503
1504 static int __init DevicePresent(u_long iobase)
1505 {
1506 union {
1507 struct {
1508 u32 a;
1509 u32 b;
1510 } llsig;
1511 char Sig[sizeof(u32) << 1];
1512 }
1513 dev;
1514 short sigLength;
1515 char data;
1516 int i, j, status = 0;
1517
1518 dev.llsig.a = ETH_PROM_SIG;
1519 dev.llsig.b = ETH_PROM_SIG;
1520 sigLength = sizeof(u32) << 1;
1521
1522 for (i = 0, j = 0; j < sigLength && i < PROBE_LENGTH + sigLength - 1; i++) {
1523 data = inb(EWRK3_APROM);
1524 if (dev.Sig[j] == data) { /* track signature */
1525 j++;
1526 } else { /* lost signature; begin search again */
1527 if (data == dev.Sig[0]) {
1528 j = 1;
1529 } else {
1530 j = 0;
1531 }
1532 }
1533 }
1534
1535 if (j != sigLength) {
1536 status = -ENODEV; /* search failed */
1537 }
1538 return status;
1539 }
1540
1541 static u_char __init get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType)
1542 {
1543 int i, j, k;
1544 u_short chksum;
1545 u_char crc, lfsr, sd, status = 0;
1546 u_long iobase = dev->base_addr;
1547 u16 tmp;
1548
1549 if (chipType == LeMAC2) {
1550 for (crc = 0x6a, j = 0; j < ETH_ALEN; j++) {
1551 sd = dev->dev_addr[j] = eeprom_image[EEPROM_PADDR0 + j];
1552 outb(dev->dev_addr[j], EWRK3_PAR0 + j);
1553 for (k = 0; k < 8; k++, sd >>= 1) {
1554 lfsr = ((((crc & 0x02) >> 1) ^ (crc & 0x01)) ^ (sd & 0x01)) << 7;
1555 crc = (crc >> 1) + lfsr;
1556 }
1557 }
1558 if (crc != eeprom_image[EEPROM_PA_CRC])
1559 status = -1;
1560 } else {
1561 for (i = 0, k = 0; i < ETH_ALEN;) {
1562 k <<= 1;
1563 if (k > 0xffff)
1564 k -= 0xffff;
1565
1566 k += (u_char) (tmp = inb(EWRK3_APROM));
1567 dev->dev_addr[i] = (u_char) tmp;
1568 outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1569 i++;
1570 k += (u_short) ((tmp = inb(EWRK3_APROM)) << 8);
1571 dev->dev_addr[i] = (u_char) tmp;
1572 outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1573 i++;
1574
1575 if (k > 0xffff)
1576 k -= 0xffff;
1577 }
1578 if (k == 0xffff)
1579 k = 0;
1580 chksum = inb(EWRK3_APROM);
1581 chksum |= (inb(EWRK3_APROM) << 8);
1582 if (k != chksum)
1583 status = -1;
1584 }
1585
1586 return status;
1587 }
1588
1589 /*
1590 ** Look for a particular board name in the EISA configuration space
1591 */
1592 static int __init EISA_signature(char *name, s32 eisa_id)
1593 {
1594 u_long i;
1595 char *signatures[] = EWRK3_SIGNATURE;
1596 char ManCode[EWRK3_STRLEN];
1597 union {
1598 s32 ID;
1599 char Id[4];
1600 } Eisa;
1601 int status = 0;
1602
1603 *name = '\0';
1604 for (i = 0; i < 4; i++) {
1605 Eisa.Id[i] = inb(eisa_id + i);
1606 }
1607
1608 ManCode[0] = (((Eisa.Id[0] >> 2) & 0x1f) + 0x40);
1609 ManCode[1] = (((Eisa.Id[1] & 0xe0) >> 5) + ((Eisa.Id[0] & 0x03) << 3) + 0x40);
1610 ManCode[2] = (((Eisa.Id[2] >> 4) & 0x0f) + 0x30);
1611 ManCode[3] = ((Eisa.Id[2] & 0x0f) + 0x30);
1612 ManCode[4] = (((Eisa.Id[3] >> 4) & 0x0f) + 0x30);
1613 ManCode[5] = '\0';
1614
1615 for (i = 0; (*signatures[i] != '\0') && (*name == '\0'); i++) {
1616 if (strstr(ManCode, signatures[i]) != NULL) {
1617 strcpy(name, ManCode);
1618 status = 1;
1619 }
1620 }
1621
1622 return status; /* return the device name string */
1623 }
1624
1625 /*
1626 ** Perform IOCTL call functions here. Some are privileged operations and the
1627 ** effective uid is checked in those cases.
1628 */
1629 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1630 {
1631 struct ewrk3_private *lp = (struct ewrk3_private *) dev->priv;
1632 struct ewrk3_ioctl *ioc = (struct ewrk3_ioctl *) &rq->ifr_data;
1633 u_long iobase = dev->base_addr;
1634 int i, j, status = 0;
1635 u_char csr;
1636 union ewrk3_addr {
1637 u_char addr[HASH_TABLE_LEN * ETH_ALEN];
1638 u_short val[(HASH_TABLE_LEN * ETH_ALEN) >> 1];
1639 };
1640
1641 union ewrk3_addr *tmp;
1642
1643 tmp = kmalloc(sizeof(union ewrk3_addr), GFP_KERNEL);
1644 if(tmp==NULL)
1645 return -ENOMEM;
1646
1647 switch (ioc->cmd) {
1648 case EWRK3_GET_HWADDR: /* Get the hardware address */
1649 for (i = 0; i < ETH_ALEN; i++) {
1650 tmp->addr[i] = dev->dev_addr[i];
1651 }
1652 ioc->len = ETH_ALEN;
1653 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1654 status = -EFAULT;
1655 break;
1656
1657 case EWRK3_SET_HWADDR: /* Set the hardware address */
1658 if (capable(CAP_NET_ADMIN)) {
1659 csr = inb(EWRK3_CSR);
1660 csr |= (CSR_TXD | CSR_RXD);
1661 outb(csr, EWRK3_CSR); /* Disable the TX and RX */
1662
1663 if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN)) {
1664 status = -EFAULT;
1665 break;
1666 }
1667 for (i = 0; i < ETH_ALEN; i++) {
1668 dev->dev_addr[i] = tmp->addr[i];
1669 outb(tmp->addr[i], EWRK3_PAR0 + i);
1670 }
1671
1672 csr &= ~(CSR_TXD | CSR_RXD); /* Enable the TX and RX */
1673 outb(csr, EWRK3_CSR);
1674 } else {
1675 status = -EPERM;
1676 }
1677
1678 break;
1679 case EWRK3_SET_PROM: /* Set Promiscuous Mode */
1680 if (capable(CAP_NET_ADMIN)) {
1681 csr = inb(EWRK3_CSR);
1682 csr |= CSR_PME;
1683 csr &= ~CSR_MCE;
1684 outb(csr, EWRK3_CSR);
1685 } else {
1686 status = -EPERM;
1687 }
1688
1689 break;
1690 case EWRK3_CLR_PROM: /* Clear Promiscuous Mode */
1691 if (capable(CAP_NET_ADMIN)) {
1692 csr = inb(EWRK3_CSR);
1693 csr &= ~CSR_PME;
1694 outb(csr, EWRK3_CSR);
1695 } else {
1696 status = -EPERM;
1697 }
1698
1699 break;
1700 case EWRK3_GET_MCA: /* Get the multicast address table */
1701 spin_lock_irq(&lp->hw_lock);
1702 if (lp->shmem_length == IO_ONLY) {
1703 outb(0, EWRK3_IOPR);
1704 outw(PAGE0_HTE, EWRK3_PIR1);
1705 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1706 tmp->addr[i] = inb(EWRK3_DATA);
1707 }
1708 } else {
1709 outb(0, EWRK3_MPR);
1710 isa_memcpy_fromio(tmp->addr, lp->shmem_base + PAGE0_HTE, (HASH_TABLE_LEN >> 3));
1711 }
1712 spin_unlock_irq(&lp->hw_lock);
1713
1714 ioc->len = (HASH_TABLE_LEN >> 3);
1715 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1716 status = -EFAULT;
1717
1718 break;
1719 case EWRK3_SET_MCA: /* Set a multicast address */
1720 if (capable(CAP_NET_ADMIN)) {
1721 if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN * ioc->len)) {
1722 status = -EFAULT;
1723 break;
1724 }
1725 set_multicast_list(dev);
1726 } else {
1727 status = -EPERM;
1728 }
1729
1730 break;
1731 case EWRK3_CLR_MCA: /* Clear all multicast addresses */
1732 if (capable(CAP_NET_ADMIN)) {
1733 set_multicast_list(dev);
1734 } else {
1735 status = -EPERM;
1736 }
1737
1738 break;
1739 case EWRK3_MCA_EN: /* Enable multicast addressing */
1740 if (capable(CAP_NET_ADMIN)) {
1741 csr = inb(EWRK3_CSR);
1742 csr |= CSR_MCE;
1743 csr &= ~CSR_PME;
1744 outb(csr, EWRK3_CSR);
1745 } else {
1746 status = -EPERM;
1747 }
1748
1749 break;
1750 case EWRK3_GET_STATS: /* Get the driver statistics */
1751 cli();
1752 ioc->len = sizeof(lp->pktStats);
1753 if (copy_to_user(ioc->data, &lp->pktStats, ioc->len))
1754 status = -EFAULT;
1755 sti();
1756
1757 break;
1758 case EWRK3_CLR_STATS: /* Zero out the driver statistics */
1759 if (capable(CAP_NET_ADMIN)) {
1760 cli();
1761 memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1762 sti();
1763 } else {
1764 status = -EPERM;
1765 }
1766
1767 break;
1768 case EWRK3_GET_CSR: /* Get the CSR Register contents */
1769 tmp->addr[0] = inb(EWRK3_CSR);
1770 ioc->len = 1;
1771 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1772 status = -EFAULT;
1773 break;
1774 case EWRK3_SET_CSR: /* Set the CSR Register contents */
1775 if (capable(CAP_NET_ADMIN)) {
1776 if (copy_from_user(tmp->addr, ioc->data, 1)) {
1777 status = -EFAULT;
1778 break;
1779 }
1780 outb(tmp->addr[0], EWRK3_CSR);
1781 } else {
1782 status = -EPERM;
1783 }
1784
1785 break;
1786 case EWRK3_GET_EEPROM: /* Get the EEPROM contents */
1787 if (capable(CAP_NET_ADMIN)) {
1788 for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1789 tmp->val[i] = (short) Read_EEPROM(iobase, i);
1790 }
1791 i = EEPROM_MAX;
1792 tmp->addr[i++] = inb(EWRK3_CMR); /* Config/Management Reg. */
1793 for (j = 0; j < ETH_ALEN; j++) {
1794 tmp->addr[i++] = inb(EWRK3_PAR0 + j);
1795 }
1796 ioc->len = EEPROM_MAX + 1 + ETH_ALEN;
1797 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1798 status = -EFAULT;
1799 } else {
1800 status = -EPERM;
1801 }
1802
1803 break;
1804 case EWRK3_SET_EEPROM: /* Set the EEPROM contents */
1805 if (capable(CAP_NET_ADMIN)) {
1806 if (copy_from_user(tmp->addr, ioc->data, EEPROM_MAX)) {
1807 status = -EFAULT;
1808 break;
1809 }
1810 for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1811 Write_EEPROM(tmp->val[i], iobase, i);
1812 }
1813 } else {
1814 status = -EPERM;
1815 }
1816
1817 break;
1818 case EWRK3_GET_CMR: /* Get the CMR Register contents */
1819 tmp->addr[0] = inb(EWRK3_CMR);
1820 ioc->len = 1;
1821 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1822 status = -EFAULT;
1823 break;
1824 case EWRK3_SET_TX_CUT_THRU: /* Set TX cut through mode */
1825 if (capable(CAP_NET_ADMIN)) {
1826 lp->txc = 1;
1827 } else {
1828 status = -EPERM;
1829 }
1830
1831 break;
1832 case EWRK3_CLR_TX_CUT_THRU: /* Clear TX cut through mode */
1833 if (capable(CAP_NET_ADMIN)) {
1834 lp->txc = 0;
1835 } else {
1836 status = -EPERM;
1837 }
1838
1839 break;
1840 default:
1841 status = -EOPNOTSUPP;
1842 }
1843 kfree(tmp);
1844 return status;
1845 }
1846
1847 #ifdef MODULE
1848 static struct net_device thisEthwrk;
1849 static int io = 0x300; /* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
1850 static int irq = 5; /* or use the insmod io= irq= options */
1851
1852 MODULE_PARM(io, "i");
1853 MODULE_PARM(irq, "i");
1854 MODULE_PARM_DESC(io, "EtherWORKS 3 I/O base address");
1855 MODULE_PARM_DESC(irq, "EtherWORKS 3 IRQ number");
1856
1857 int init_module(void)
1858 {
1859 thisEthwrk.base_addr = io;
1860 thisEthwrk.irq = irq;
1861 thisEthwrk.init = ewrk3_probe;
1862 if (register_netdev(&thisEthwrk) != 0)
1863 return -EIO;
1864 return 0;
1865 }
1866
1867 void cleanup_module(void)
1868 {
1869 unregister_netdev(&thisEthwrk);
1870 if (thisEthwrk.priv) {
1871 kfree(thisEthwrk.priv);
1872 thisEthwrk.priv = NULL;
1873 }
1874 thisEthwrk.irq = 0;
1875
1876 release_region(thisEthwrk.base_addr, EWRK3_TOTAL_SIZE);
1877 }
1878 #endif /* MODULE */
1879
1880
1881 /*
1882 * Local variables:
1883 * 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"
1884 *
1885 * 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"
1886 * End:
1887 */
1888