ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / 3c523.c
1 /*
2    net-3-driver for the 3c523 Etherlink/MC card (i82586 Ethernet chip)
3
4
5    This is an extension to the Linux operating system, and is covered by the
6    same GNU General Public License that covers that work.
7
8    Copyright 1995, 1996 by Chris Beauregard (cpbeaure@undergrad.math.uwaterloo.ca)
9
10    This is basically Michael Hipp's ni52 driver, with a new probing
11    algorithm and some minor changes to the 82586 CA and reset routines.
12    Thanks a lot Michael for a really clean i82586 implementation!  Unless
13    otherwise documented in ni52.c, any bugs are mine.
14
15    Contrary to the Ethernet-HOWTO, this isn't based on the 3c507 driver in
16    any way.  The ni52 is a lot easier to modify.
17
18    sources:
19    ni52.c
20
21    Crynwr packet driver collection was a great reference for my first
22    attempt at this sucker.  The 3c507 driver also helped, until I noticed
23    that ni52.c was a lot nicer.
24
25    EtherLink/MC: Micro Channel Ethernet Adapter Technical Reference
26    Manual, courtesy of 3Com CardFacts, documents the 3c523-specific
27    stuff.  Information on CardFacts is found in the Ethernet HOWTO.
28    Also see <a href="http://www.3com.com/">
29
30    Microprocessor Communications Support Chips, T.J. Byers, ISBN
31    0-444-01224-9, has a section on the i82586.  It tells you just enough
32    to know that you really don't want to learn how to program the chip.
33
34    The original device probe code was stolen from ps2esdi.c
35
36    Known Problems:
37    Since most of the code was stolen from ni52.c, you'll run across the
38    same bugs in the 0.62 version of ni52.c, plus maybe a few because of
39    the 3c523 idiosynchacies.  The 3c523 has 16K of RAM though, so there
40    shouldn't be the overrun problem that the 8K ni52 has.
41
42    This driver is for a 16K adapter.  It should work fine on the 64K
43    adapters, but it will only use one of the 4 banks of RAM.  Modifying
44    this for the 64K version would require a lot of heinous bank
45    switching, which I'm sure not interested in doing.  If you try to
46    implement a bank switching version, you'll basically have to remember
47    what bank is enabled and do a switch everytime you access a memory
48    location that's not current.  You'll also have to remap pointers on
49    the driver side, because it only knows about 16K of the memory.
50    Anyone desperate or masochistic enough to try?
51
52    It seems to be stable now when multiple transmit buffers are used.  I
53    can't see any performance difference, but then I'm working on a 386SX.
54
55    Multicast doesn't work.  It doesn't even pretend to work.  Don't use
56    it.  Don't compile your kernel with multicast support.  I don't know
57    why.
58
59    Features:
60    This driver is useable as a loadable module.  If you try to specify an
61    IRQ or a IO address (via insmod 3c523.o irq=xx io=0xyyy), it will
62    search the MCA slots until it finds a 3c523 with the specified
63    parameters.
64
65    This driver does support multiple ethernet cards when used as a module
66    (up to MAX_3C523_CARDS, the default being 4)
67
68    This has been tested with both BNC and TP versions, internal and
69    external transceivers.  Haven't tested with the 64K version (that I
70    know of).
71
72    History:
73    Jan 1st, 1996
74    first public release
75    Feb 4th, 1996
76    update to 1.3.59, incorporated multicast diffs from ni52.c
77    Feb 15th, 1996
78    added shared irq support
79    Apr 1999
80    added support for multiple cards when used as a module
81    added option to disable multicast as is causes problems
82        Ganesh Sittampalam <ganesh.sittampalam@magdalen.oxford.ac.uk>
83        Stuart Adamson <stuart.adamson@compsoc.net>
84    Nov 2001
85    added support for ethtool (jgarzik)
86         
87    $Header: /fsys2/home/chrisb/linux-1.3.59-MCA/drivers/net/RCS/3c523.c,v 1.1 1996/02/05 01:53:46 chrisb Exp chrisb $
88  */
89
90 #define DRV_NAME                "3c523"
91 #define DRV_VERSION             "17-Nov-2001"
92
93 #include <linux/init.h>
94 #include <linux/netdevice.h>
95 #include <linux/etherdevice.h>
96 #include <linux/module.h>
97 #include <linux/kernel.h>
98 #include <linux/string.h>
99 #include <linux/errno.h>
100 #include <linux/ioport.h>
101 #include <linux/skbuff.h>
102 #include <linux/slab.h>
103 #include <linux/interrupt.h>
104 #include <linux/delay.h>
105 #include <linux/mca-legacy.h>
106 #include <linux/ethtool.h>
107
108 #include <asm/uaccess.h>
109 #include <asm/processor.h>
110 #include <asm/bitops.h>
111 #include <asm/io.h>
112
113 #include "3c523.h"
114
115 /*************************************************************************/
116 #define DEBUG                   /* debug on */
117 #define SYSBUSVAL 0             /* 1 = 8 Bit, 0 = 16 bit - 3c523 only does 16 bit */
118 #undef ELMC_MULTICAST           /* Disable multicast support as it is somewhat seriously broken at the moment */
119
120 #define make32(ptr16) (p->memtop + (short) (ptr16) )
121 #define make24(ptr32) ((char *) (ptr32) - p->base)
122 #define make16(ptr32) ((unsigned short) ((unsigned long) (ptr32) - (unsigned long) p->memtop ))
123
124 /*************************************************************************/
125 /*
126    Tables to which we can map values in the configuration registers.
127  */
128 static int irq_table[] __initdata = {
129         12, 7, 3, 9
130 };
131
132 static int csr_table[] __initdata = {
133         0x300, 0x1300, 0x2300, 0x3300
134 };
135
136 static int shm_table[] __initdata = {
137         0x0c0000, 0x0c8000, 0x0d0000, 0x0d8000
138 };
139
140 /******************* how to calculate the buffers *****************************
141
142
143   * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
144   * --------------- in a different (more stable?) mode. Only in this mode it's
145   *                 possible to configure the driver with 'NO_NOPCOMMANDS'
146
147 sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
148 sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
149 sizeof(rfd) = 24; sizeof(rbd) = 12;
150 sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
151 sizeof(nop_cmd) = 8;
152
153   * if you don't know the driver, better do not change this values: */
154
155 #define RECV_BUFF_SIZE 1524     /* slightly oversized */
156 #define XMIT_BUFF_SIZE 1524     /* slightly oversized */
157 #define NUM_XMIT_BUFFS 1        /* config for both, 8K and 16K shmem */
158 #define NUM_RECV_BUFFS_8  4     /* config for 8K shared mem */
159 #define NUM_RECV_BUFFS_16 9     /* config for 16K shared mem */
160
161 #if (NUM_XMIT_BUFFS == 1)
162 #define NO_NOPCOMMANDS          /* only possible with NUM_XMIT_BUFFS=1 */
163 #endif
164
165 /**************************************************************************/
166
167 #define DELAY(x) { mdelay(32 * x); }
168
169 /* a much shorter delay: */
170 #define DELAY_16(); { udelay(16) ; }
171
172 /* wait for command with timeout: */
173 #define WAIT_4_SCB_CMD() { int i; \
174   for(i=0;i<1024;i++) { \
175     if(!p->scb->cmd) break; \
176     DELAY_16(); \
177     if(i == 1023) { \
178       printk(KERN_WARNING "%s:%d: scb_cmd timed out .. resetting i82586\n",\
179         dev->name,__LINE__); \
180       elmc_id_reset586(); } } }
181
182 static irqreturn_t elmc_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr);
183 static int elmc_open(struct net_device *dev);
184 static int elmc_close(struct net_device *dev);
185 static int elmc_send_packet(struct sk_buff *, struct net_device *);
186 static struct net_device_stats *elmc_get_stats(struct net_device *dev);
187 static void elmc_timeout(struct net_device *dev);
188 #ifdef ELMC_MULTICAST
189 static void set_multicast_list(struct net_device *dev);
190 #endif
191 static struct ethtool_ops netdev_ethtool_ops;
192
193 /* helper-functions */
194 static int init586(struct net_device *dev);
195 static int check586(struct net_device *dev, unsigned long where, unsigned size);
196 static void alloc586(struct net_device *dev);
197 static void startrecv586(struct net_device *dev);
198 static void *alloc_rfa(struct net_device *dev, void *ptr);
199 static void elmc_rcv_int(struct net_device *dev);
200 static void elmc_xmt_int(struct net_device *dev);
201 static void elmc_rnr_int(struct net_device *dev);
202
203 struct priv {
204         struct net_device_stats stats;
205         unsigned long base;
206         char *memtop;
207         unsigned long mapped_start;             /* Start of ioremap */
208         volatile struct rfd_struct *rfd_last, *rfd_top, *rfd_first;
209         volatile struct scp_struct *scp;        /* volatile is important */
210         volatile struct iscp_struct *iscp;      /* volatile is important */
211         volatile struct scb_struct *scb;        /* volatile is important */
212         volatile struct tbd_struct *xmit_buffs[NUM_XMIT_BUFFS];
213 #if (NUM_XMIT_BUFFS == 1)
214         volatile struct transmit_cmd_struct *xmit_cmds[2];
215         volatile struct nop_cmd_struct *nop_cmds[2];
216 #else
217         volatile struct transmit_cmd_struct *xmit_cmds[NUM_XMIT_BUFFS];
218         volatile struct nop_cmd_struct *nop_cmds[NUM_XMIT_BUFFS];
219 #endif
220         volatile int nop_point, num_recv_buffs;
221         volatile char *xmit_cbuffs[NUM_XMIT_BUFFS];
222         volatile int xmit_count, xmit_last;
223         volatile int slot;
224 };
225
226 #define elmc_attn586()  {elmc_do_attn586(dev->base_addr,ELMC_CTRL_INTE);}
227 #define elmc_reset586() {elmc_do_reset586(dev->base_addr,ELMC_CTRL_INTE);}
228
229 /* with interrupts disabled - this will clear the interrupt bit in the
230    3c523 control register, and won't put it back.  This effectively
231    disables interrupts on the card. */
232 #define elmc_id_attn586()  {elmc_do_attn586(dev->base_addr,0);}
233 #define elmc_id_reset586() {elmc_do_reset586(dev->base_addr,0);}
234
235 /*************************************************************************/
236 /*
237    Do a Channel Attention on the 3c523.  This is extremely board dependent.
238  */
239 static void elmc_do_attn586(int ioaddr, int ints)
240 {
241         /* the 3c523 requires a minimum of 500 ns.  The delays here might be
242            a little too large, and hence they may cut the performance of the
243            card slightly.  If someone who knows a little more about Linux
244            timing would care to play with these, I'd appreciate it. */
245
246         /* this bit masking stuff is crap.  I'd rather have separate
247            registers with strobe triggers for each of these functions.  <sigh>
248            Ya take what ya got. */
249
250         outb(ELMC_CTRL_RST | 0x3 | ELMC_CTRL_CA | ints, ioaddr + ELMC_CTRL);
251         DELAY_16();             /* > 500 ns */
252         outb(ELMC_CTRL_RST | 0x3 | ints, ioaddr + ELMC_CTRL);
253 }
254
255 /*************************************************************************/
256 /*
257    Reset the 82586 on the 3c523.  Also very board dependent.
258  */
259 static void elmc_do_reset586(int ioaddr, int ints)
260 {
261         /* toggle the RST bit low then high */
262         outb(0x3 | ELMC_CTRL_LBK, ioaddr + ELMC_CTRL);
263         DELAY_16();             /* > 500 ns */
264         outb(ELMC_CTRL_RST | ELMC_CTRL_LBK | 0x3, ioaddr + ELMC_CTRL);
265
266         elmc_do_attn586(ioaddr, ints);
267 }
268
269 /**********************************************
270  * close device
271  */
272
273 static int elmc_close(struct net_device *dev)
274 {
275         netif_stop_queue(dev);
276         elmc_id_reset586();     /* the hard way to stop the receiver */
277         free_irq(dev->irq, dev);
278         return 0;
279 }
280
281 /**********************************************
282  * open device
283  */
284
285 static int elmc_open(struct net_device *dev)
286 {
287         int ret;
288
289         elmc_id_attn586();      /* disable interrupts */
290
291         ret = request_irq(dev->irq, &elmc_interrupt, SA_SHIRQ | SA_SAMPLE_RANDOM,
292                           dev->name, dev);
293         if (ret) {
294                 printk(KERN_ERR "%s: couldn't get irq %d\n", dev->name, dev->irq);
295                 elmc_id_reset586();
296                 return ret;
297         }
298         alloc586(dev);
299         init586(dev);
300         startrecv586(dev);
301         netif_start_queue(dev);
302         return 0;               /* most done by init */
303 }
304
305 /**********************************************
306  * Check to see if there's an 82586 out there.
307  */
308
309 static int __init check586(struct net_device *dev, unsigned long where, unsigned size)
310 {
311         struct priv *p = (struct priv *) dev->priv;
312         char *iscp_addrs[2];
313         int i = 0;
314
315         p->base = (unsigned long) isa_bus_to_virt((unsigned long)where) + size - 0x01000000;
316         p->memtop = isa_bus_to_virt((unsigned long)where) + size;
317         p->scp = (struct scp_struct *)(p->base + SCP_DEFAULT_ADDRESS);
318         memset((char *) p->scp, 0, sizeof(struct scp_struct));
319         p->scp->sysbus = SYSBUSVAL;     /* 1 = 8Bit-Bus, 0 = 16 Bit */
320
321         iscp_addrs[0] = isa_bus_to_virt((unsigned long)where);
322         iscp_addrs[1] = (char *) p->scp - sizeof(struct iscp_struct);
323
324         for (i = 0; i < 2; i++) {
325                 p->iscp = (struct iscp_struct *) iscp_addrs[i];
326                 memset((char *) p->iscp, 0, sizeof(struct iscp_struct));
327
328                 p->scp->iscp = make24(p->iscp);
329                 p->iscp->busy = 1;
330
331                 elmc_id_reset586();
332
333                 /* reset586 does an implicit CA */
334
335                 /* apparently, you sometimes have to kick the 82586 twice... */
336                 elmc_id_attn586();
337                 DELAY(1);
338
339                 if (p->iscp->busy) {    /* i82586 clears 'busy' after successful init */
340                         return 0;
341                 }
342         }
343         return 1;
344 }
345
346 /******************************************************************
347  * set iscp at the right place, called by elmc_probe and open586.
348  */
349
350 void alloc586(struct net_device *dev)
351 {
352         struct priv *p = (struct priv *) dev->priv;
353
354         elmc_id_reset586();
355         DELAY(2);
356
357         p->scp = (struct scp_struct *) (p->base + SCP_DEFAULT_ADDRESS);
358         p->scb = (struct scb_struct *) isa_bus_to_virt(dev->mem_start);
359         p->iscp = (struct iscp_struct *) ((char *) p->scp - sizeof(struct iscp_struct));
360
361         memset((char *) p->iscp, 0, sizeof(struct iscp_struct));
362         memset((char *) p->scp, 0, sizeof(struct scp_struct));
363
364         p->scp->iscp = make24(p->iscp);
365         p->scp->sysbus = SYSBUSVAL;
366         p->iscp->scb_offset = make16(p->scb);
367
368         p->iscp->busy = 1;
369         elmc_id_reset586();
370         elmc_id_attn586();
371
372         DELAY(2);
373
374         if (p->iscp->busy) {
375                 printk(KERN_ERR "%s: Init-Problems (alloc).\n", dev->name);
376         }
377         memset((char *) p->scb, 0, sizeof(struct scb_struct));
378 }
379
380 /*****************************************************************/
381
382 static int elmc_getinfo(char *buf, int slot, void *d)
383 {
384         int len = 0;
385         struct net_device *dev = (struct net_device *) d;
386         int i;
387
388         if (dev == NULL)
389                 return len;
390
391         len += sprintf(buf + len, "Revision: 0x%x\n",
392                        inb(dev->base_addr + ELMC_REVISION) & 0xf);
393         len += sprintf(buf + len, "IRQ: %d\n", dev->irq);
394         len += sprintf(buf + len, "IO Address: %#lx-%#lx\n", dev->base_addr,
395                        dev->base_addr + ELMC_IO_EXTENT);
396         len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
397                        dev->mem_end - 1);
398         len += sprintf(buf + len, "Transceiver: %s\n", dev->if_port ?
399                        "External" : "Internal");
400         len += sprintf(buf + len, "Device: %s\n", dev->name);
401         len += sprintf(buf + len, "Hardware Address:");
402         for (i = 0; i < 6; i++) {
403                 len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
404         }
405         buf[len++] = '\n';
406         buf[len] = 0;
407
408         return len;
409 }                               /* elmc_getinfo() */
410
411 /*****************************************************************/
412
413 static int __init do_elmc_probe(struct net_device *dev)
414 {
415         static int slot;
416         int base_addr = dev->base_addr;
417         int irq = dev->irq;
418         u_char status = 0;
419         u_char revision = 0;
420         int i = 0;
421         unsigned int size = 0;
422         int retval;
423         struct priv *pr = dev->priv;
424
425         SET_MODULE_OWNER(dev);
426         if (MCA_bus == 0) {
427                 return -ENODEV;
428         }
429         /* search through the slots for the 3c523. */
430         slot = mca_find_adapter(ELMC_MCA_ID, 0);
431         while (slot != -1) {
432                 status = mca_read_stored_pos(slot, 2);
433
434                 dev->irq=irq_table[(status & ELMC_STATUS_IRQ_SELECT) >> 6];
435                 dev->base_addr=csr_table[(status & ELMC_STATUS_CSR_SELECT) >> 1];
436                 
437                 /*
438                    If we're trying to match a specified irq or IO address,
439                    we'll reject a match unless it's what we're looking for.
440                    Also reject it if the card is already in use.
441                  */
442
443                 if ((irq && irq != dev->irq) || 
444                     (base_addr && base_addr != dev->base_addr)) {
445                         slot = mca_find_adapter(ELMC_MCA_ID, slot + 1);
446                         continue;
447                 }
448                 if (!request_region(dev->base_addr, ELMC_IO_EXTENT, dev->name)) {
449                         slot = mca_find_adapter(ELMC_MCA_ID, slot + 1);
450                         continue;
451                 }
452
453                 /* found what we're looking for... */
454                 break;
455         }
456
457         /* we didn't find any 3c523 in the slots we checked for */
458         if (slot == MCA_NOTFOUND)
459                 return ((base_addr || irq) ? -ENXIO : -ENODEV);
460
461         mca_set_adapter_name(slot, "3Com 3c523 Etherlink/MC");
462         mca_set_adapter_procfn(slot, (MCA_ProcFn) elmc_getinfo, dev);
463
464         /* if we get this far, adapter has been found - carry on */
465         printk(KERN_INFO "%s: 3c523 adapter found in slot %d\n", dev->name, slot + 1);
466
467         /* Now we extract configuration info from the card.
468            The 3c523 provides information in two of the POS registers, but
469            the second one is only needed if we want to tell the card what IRQ
470            to use.  I suspect that whoever sets the thing up initially would
471            prefer we don't screw with those things.
472
473            Note that we read the status info when we found the card...
474
475            See 3c523.h for more details.
476          */
477
478         /* revision is stored in the first 4 bits of the revision register */
479         revision = inb(dev->base_addr + ELMC_REVISION) & 0xf;
480
481         /* according to docs, we read the interrupt and write it back to
482            the IRQ select register, since the POST might not configure the IRQ
483            properly. */
484         switch (dev->irq) {
485         case 3:
486                 mca_write_pos(slot, 3, 0x04);
487                 break;
488         case 7:
489                 mca_write_pos(slot, 3, 0x02);
490                 break;
491         case 9:
492                 mca_write_pos(slot, 3, 0x08);
493                 break;
494         case 12:
495                 mca_write_pos(slot, 3, 0x01);
496                 break;
497         }
498
499         memset(pr, 0, sizeof(struct priv));
500         pr->slot = slot;
501
502         printk(KERN_INFO "%s: 3Com 3c523 Rev 0x%x at %#lx\n", dev->name, (int) revision,
503                dev->base_addr);
504
505         /* Determine if we're using the on-board transceiver (i.e. coax) or
506            an external one.  The information is pretty much useless, but I
507            guess it's worth brownie points. */
508         dev->if_port = (status & ELMC_STATUS_DISABLE_THIN);
509
510         /* The 3c523 has a 24K chunk of memory.  The first 16K is the
511            shared memory, while the last 8K is for the EtherStart BIOS ROM.
512            Which we don't care much about here.  We'll just tell Linux that
513            we're using 16K.  MCA won't permit address space conflicts caused
514            by not mapping the other 8K. */
515         dev->mem_start = shm_table[(status & ELMC_STATUS_MEMORY_SELECT) >> 3];
516
517         /* We're using MCA, so it's a given that the information about memory
518            size is correct.  The Crynwr drivers do something like this. */
519
520         elmc_id_reset586();     /* seems like a good idea before checking it... */
521
522         size = 0x4000;          /* check for 16K mem */
523         if (!check586(dev, dev->mem_start, size)) {
524                 printk(KERN_ERR "%s: memprobe, Can't find memory at 0x%lx!\n", dev->name,
525                        dev->mem_start);
526                 retval = -ENODEV;
527                 goto err_out;
528         }
529         dev->mem_end = dev->mem_start + size;   /* set mem_end showed by 'ifconfig' */
530
531         pr->memtop = isa_bus_to_virt(dev->mem_start) + size;
532         pr->base = (unsigned long) isa_bus_to_virt(dev->mem_start) + size - 0x01000000;
533         alloc586(dev);
534
535         elmc_id_reset586();     /* make sure it doesn't generate spurious ints */
536
537         /* set number of receive-buffs according to memsize */
538         pr->num_recv_buffs = NUM_RECV_BUFFS_16;
539
540         /* dump all the assorted information */
541         printk(KERN_INFO "%s: IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->name,
542                dev->irq, dev->if_port ? "ex" : "in", 
543                dev->mem_start, dev->mem_end - 1);
544
545         /* The hardware address for the 3c523 is stored in the first six
546            bytes of the IO address. */
547         printk(KERN_INFO "%s: hardware address ", dev->name);
548         for (i = 0; i < 6; i++) {
549                 dev->dev_addr[i] = inb(dev->base_addr + i);
550                 printk(" %02x", dev->dev_addr[i]);
551         }
552         printk("\n");
553
554         dev->open = &elmc_open;
555         dev->stop = &elmc_close;
556         dev->get_stats = &elmc_get_stats;
557         dev->hard_start_xmit = &elmc_send_packet;
558         dev->tx_timeout = &elmc_timeout;
559         dev->watchdog_timeo = HZ;
560 #ifdef ELMC_MULTICAST
561         dev->set_multicast_list = &set_multicast_list;
562 #else
563         dev->set_multicast_list = NULL;
564 #endif
565         dev->ethtool_ops = &netdev_ethtool_ops;
566         
567         /* note that we haven't actually requested the IRQ from the kernel.
568            That gets done in elmc_open().  I'm not sure that's such a good idea,
569            but it works, so I'll go with it. */
570
571 #ifndef ELMC_MULTICAST
572         dev->flags&=~IFF_MULTICAST;     /* Multicast doesn't work */
573 #endif
574
575         return 0;
576 err_out:
577         mca_set_adapter_procfn(slot, NULL, NULL);
578         release_region(dev->base_addr, ELMC_IO_EXTENT);
579         return retval;
580 }
581  
582 static void cleanup_card(struct net_device *dev)
583 {
584         mca_set_adapter_procfn(((struct priv *) (dev->priv))->slot, NULL, NULL);
585         release_region(dev->base_addr, ELMC_IO_EXTENT);
586 }
587
588 struct net_device * __init elmc_probe(int unit)
589 {
590         struct net_device *dev = alloc_etherdev(sizeof(struct priv));
591         int err;
592
593         if (!dev)
594                 return ERR_PTR(-ENOMEM);
595
596         sprintf(dev->name, "eth%d", unit);
597         netdev_boot_setup_check(dev);
598
599         err = do_elmc_probe(dev);
600         if (err)
601                 goto out;
602         err = register_netdev(dev);
603         if (err)
604                 goto out1;
605         return dev;
606 out1:
607         cleanup_card(dev);
608 out:
609         free_netdev(dev);
610         return ERR_PTR(err);
611 }
612
613 /**********************************************
614  * init the chip (elmc-interrupt should be disabled?!)
615  * needs a correct 'allocated' memory
616  */
617
618 static int init586(struct net_device *dev)
619 {
620         void *ptr;
621         unsigned long s;
622         int i, result = 0;
623         struct priv *p = (struct priv *) dev->priv;
624         volatile struct configure_cmd_struct *cfg_cmd;
625         volatile struct iasetup_cmd_struct *ias_cmd;
626         volatile struct tdr_cmd_struct *tdr_cmd;
627         volatile struct mcsetup_cmd_struct *mc_cmd;
628         struct dev_mc_list *dmi = dev->mc_list;
629         int num_addrs = dev->mc_count;
630
631         ptr = (void *) ((char *) p->scb + sizeof(struct scb_struct));
632
633         cfg_cmd = (struct configure_cmd_struct *) ptr;  /* configure-command */
634         cfg_cmd->cmd_status = 0;
635         cfg_cmd->cmd_cmd = CMD_CONFIGURE | CMD_LAST;
636         cfg_cmd->cmd_link = 0xffff;
637
638         cfg_cmd->byte_cnt = 0x0a;       /* number of cfg bytes */
639         cfg_cmd->fifo = 0x08;   /* fifo-limit (8=tx:32/rx:64) */
640         cfg_cmd->sav_bf = 0x40; /* hold or discard bad recv frames (bit 7) */
641         cfg_cmd->adr_len = 0x2e;        /* addr_len |!src_insert |pre-len |loopback */
642         cfg_cmd->priority = 0x00;
643         cfg_cmd->ifs = 0x60;
644         cfg_cmd->time_low = 0x00;
645         cfg_cmd->time_high = 0xf2;
646         cfg_cmd->promisc = 0;
647         if (dev->flags & (IFF_ALLMULTI | IFF_PROMISC)) {
648                 cfg_cmd->promisc = 1;
649                 dev->flags |= IFF_PROMISC;
650         }
651         cfg_cmd->carr_coll = 0x00;
652
653         p->scb->cbl_offset = make16(cfg_cmd);
654
655         p->scb->cmd = CUC_START;        /* cmd.-unit start */
656         elmc_id_attn586();
657
658         s = jiffies;            /* warning: only active with interrupts on !! */
659         while (!(cfg_cmd->cmd_status & STAT_COMPL)) {
660                 if (jiffies - s > 30*HZ/100)
661                         break;
662         }
663
664         if ((cfg_cmd->cmd_status & (STAT_OK | STAT_COMPL)) != (STAT_COMPL | STAT_OK)) {
665                 printk(KERN_WARNING "%s (elmc): configure command failed: %x\n", dev->name, cfg_cmd->cmd_status);
666                 return 1;
667         }
668         /*
669          * individual address setup
670          */
671         ias_cmd = (struct iasetup_cmd_struct *) ptr;
672
673         ias_cmd->cmd_status = 0;
674         ias_cmd->cmd_cmd = CMD_IASETUP | CMD_LAST;
675         ias_cmd->cmd_link = 0xffff;
676
677         memcpy((char *) &ias_cmd->iaddr, (char *) dev->dev_addr, ETH_ALEN);
678
679         p->scb->cbl_offset = make16(ias_cmd);
680
681         p->scb->cmd = CUC_START;        /* cmd.-unit start */
682         elmc_id_attn586();
683
684         s = jiffies;
685         while (!(ias_cmd->cmd_status & STAT_COMPL)) {
686                 if (jiffies - s > 30*HZ/100)
687                         break;
688         }
689
690         if ((ias_cmd->cmd_status & (STAT_OK | STAT_COMPL)) != (STAT_OK | STAT_COMPL)) {
691                 printk(KERN_WARNING "%s (elmc): individual address setup command failed: %04x\n", dev->name, ias_cmd->cmd_status);
692                 return 1;
693         }
694         /*
695          * TDR, wire check .. e.g. no resistor e.t.c
696          */
697         tdr_cmd = (struct tdr_cmd_struct *) ptr;
698
699         tdr_cmd->cmd_status = 0;
700         tdr_cmd->cmd_cmd = CMD_TDR | CMD_LAST;
701         tdr_cmd->cmd_link = 0xffff;
702         tdr_cmd->status = 0;
703
704         p->scb->cbl_offset = make16(tdr_cmd);
705
706         p->scb->cmd = CUC_START;        /* cmd.-unit start */
707         elmc_attn586();
708
709         s = jiffies;
710         while (!(tdr_cmd->cmd_status & STAT_COMPL)) {
711                 if (jiffies - s > 30*HZ/100) {
712                         printk(KERN_WARNING "%s: %d Problems while running the TDR.\n", dev->name, __LINE__);
713                         result = 1;
714                         break;
715                 }
716         }
717
718         if (!result) {
719                 DELAY(2);       /* wait for result */
720                 result = tdr_cmd->status;
721
722                 p->scb->cmd = p->scb->status & STAT_MASK;
723                 elmc_id_attn586();      /* ack the interrupts */
724
725                 if (result & TDR_LNK_OK) {
726                         /* empty */
727                 } else if (result & TDR_XCVR_PRB) {
728                         printk(KERN_WARNING "%s: TDR: Transceiver problem!\n", dev->name);
729                 } else if (result & TDR_ET_OPN) {
730                         printk(KERN_WARNING "%s: TDR: No correct termination %d clocks away.\n", dev->name, result & TDR_TIMEMASK);
731                 } else if (result & TDR_ET_SRT) {
732                         if (result & TDR_TIMEMASK)      /* time == 0 -> strange :-) */
733                                 printk(KERN_WARNING "%s: TDR: Detected a short circuit %d clocks away.\n", dev->name, result & TDR_TIMEMASK);
734                 } else {
735                         printk(KERN_WARNING "%s: TDR: Unknown status %04x\n", dev->name, result);
736                 }
737         }
738         /*
739          * ack interrupts
740          */
741         p->scb->cmd = p->scb->status & STAT_MASK;
742         elmc_id_attn586();
743
744         /*
745          * alloc nop/xmit-cmds
746          */
747 #if (NUM_XMIT_BUFFS == 1)
748         for (i = 0; i < 2; i++) {
749                 p->nop_cmds[i] = (struct nop_cmd_struct *) ptr;
750                 p->nop_cmds[i]->cmd_cmd = CMD_NOP;
751                 p->nop_cmds[i]->cmd_status = 0;
752                 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
753                 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
754         }
755         p->xmit_cmds[0] = (struct transmit_cmd_struct *) ptr;   /* transmit cmd/buff 0 */
756         ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
757 #else
758         for (i = 0; i < NUM_XMIT_BUFFS; i++) {
759                 p->nop_cmds[i] = (struct nop_cmd_struct *) ptr;
760                 p->nop_cmds[i]->cmd_cmd = CMD_NOP;
761                 p->nop_cmds[i]->cmd_status = 0;
762                 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
763                 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
764                 p->xmit_cmds[i] = (struct transmit_cmd_struct *) ptr;   /*transmit cmd/buff 0 */
765                 ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
766         }
767 #endif
768
769         ptr = alloc_rfa(dev, (void *) ptr);     /* init receive-frame-area */
770
771         /*
772          * Multicast setup
773          */
774
775         if (dev->mc_count) {
776                 /* I don't understand this: do we really need memory after the init? */
777                 int len = ((char *) p->iscp - (char *) ptr - 8) / 6;
778                 if (len <= 0) {
779                         printk(KERN_ERR "%s: Ooooops, no memory for MC-Setup!\n", dev->name);
780                 } else {
781                         if (len < num_addrs) {
782                                 num_addrs = len;
783                                 printk(KERN_WARNING "%s: Sorry, can only apply %d MC-Address(es).\n",
784                                        dev->name, num_addrs);
785                         }
786                         mc_cmd = (struct mcsetup_cmd_struct *) ptr;
787                         mc_cmd->cmd_status = 0;
788                         mc_cmd->cmd_cmd = CMD_MCSETUP | CMD_LAST;
789                         mc_cmd->cmd_link = 0xffff;
790                         mc_cmd->mc_cnt = num_addrs * 6;
791                         for (i = 0; i < num_addrs; i++) {
792                                 memcpy((char *) mc_cmd->mc_list[i], dmi->dmi_addr, 6);
793                                 dmi = dmi->next;
794                         }
795                         p->scb->cbl_offset = make16(mc_cmd);
796                         p->scb->cmd = CUC_START;
797                         elmc_id_attn586();
798                         s = jiffies;
799                         while (!(mc_cmd->cmd_status & STAT_COMPL)) {
800                                 if (jiffies - s > 30*HZ/100)
801                                         break;
802                         }
803                         if (!(mc_cmd->cmd_status & STAT_COMPL)) {
804                                 printk(KERN_WARNING "%s: Can't apply multicast-address-list.\n", dev->name);
805                         }
806                 }
807         }
808         /*
809          * alloc xmit-buffs / init xmit_cmds
810          */
811         for (i = 0; i < NUM_XMIT_BUFFS; i++) {
812                 p->xmit_cbuffs[i] = (char *) ptr;       /* char-buffs */
813                 ptr = (char *) ptr + XMIT_BUFF_SIZE;
814                 p->xmit_buffs[i] = (struct tbd_struct *) ptr;   /* TBD */
815                 ptr = (char *) ptr + sizeof(struct tbd_struct);
816                 if ((void *) ptr > (void *) p->iscp) {
817                         printk(KERN_ERR "%s: not enough shared-mem for your configuration!\n", dev->name);
818                         return 1;
819                 }
820                 memset((char *) (p->xmit_cmds[i]), 0, sizeof(struct transmit_cmd_struct));
821                 memset((char *) (p->xmit_buffs[i]), 0, sizeof(struct tbd_struct));
822                 p->xmit_cmds[i]->cmd_status = STAT_COMPL;
823                 p->xmit_cmds[i]->cmd_cmd = CMD_XMIT | CMD_INT;
824                 p->xmit_cmds[i]->tbd_offset = make16((p->xmit_buffs[i]));
825                 p->xmit_buffs[i]->next = 0xffff;
826                 p->xmit_buffs[i]->buffer = make24((p->xmit_cbuffs[i]));
827         }
828
829         p->xmit_count = 0;
830         p->xmit_last = 0;
831 #ifndef NO_NOPCOMMANDS
832         p->nop_point = 0;
833 #endif
834
835         /*
836          * 'start transmitter' (nop-loop)
837          */
838 #ifndef NO_NOPCOMMANDS
839         p->scb->cbl_offset = make16(p->nop_cmds[0]);
840         p->scb->cmd = CUC_START;
841         elmc_id_attn586();
842         WAIT_4_SCB_CMD();
843 #else
844         p->xmit_cmds[0]->cmd_link = 0xffff;
845         p->xmit_cmds[0]->cmd_cmd = CMD_XMIT | CMD_LAST | CMD_INT;
846 #endif
847
848         return 0;
849 }
850
851 /******************************************************
852  * This is a helper routine for elmc_rnr_int() and init586().
853  * It sets up the Receive Frame Area (RFA).
854  */
855
856 static void *alloc_rfa(struct net_device *dev, void *ptr)
857 {
858         volatile struct rfd_struct *rfd = (struct rfd_struct *) ptr;
859         volatile struct rbd_struct *rbd;
860         int i;
861         struct priv *p = (struct priv *) dev->priv;
862
863         memset((char *) rfd, 0, sizeof(struct rfd_struct) * p->num_recv_buffs);
864         p->rfd_first = rfd;
865
866         for (i = 0; i < p->num_recv_buffs; i++) {
867                 rfd[i].next = make16(rfd + (i + 1) % p->num_recv_buffs);
868         }
869         rfd[p->num_recv_buffs - 1].last = RFD_SUSP;     /* RU suspend */
870
871         ptr = (void *) (rfd + p->num_recv_buffs);
872
873         rbd = (struct rbd_struct *) ptr;
874         ptr = (void *) (rbd + p->num_recv_buffs);
875
876         /* clr descriptors */
877         memset((char *) rbd, 0, sizeof(struct rbd_struct) * p->num_recv_buffs);
878
879         for (i = 0; i < p->num_recv_buffs; i++) {
880                 rbd[i].next = make16((rbd + (i + 1) % p->num_recv_buffs));
881                 rbd[i].size = RECV_BUFF_SIZE;
882                 rbd[i].buffer = make24(ptr);
883                 ptr = (char *) ptr + RECV_BUFF_SIZE;
884         }
885
886         p->rfd_top = p->rfd_first;
887         p->rfd_last = p->rfd_first + p->num_recv_buffs - 1;
888
889         p->scb->rfa_offset = make16(p->rfd_first);
890         p->rfd_first->rbd_offset = make16(rbd);
891
892         return ptr;
893 }
894
895
896 /**************************************************
897  * Interrupt Handler ...
898  */
899
900 static irqreturn_t
901 elmc_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
902 {
903         struct net_device *dev = (struct net_device *) dev_id;
904         unsigned short stat;
905         struct priv *p;
906
907         if (dev == NULL) {
908                 printk(KERN_ERR "elmc-interrupt: irq %d for unknown device.\n", (int) -(((struct pt_regs *) reg_ptr)->orig_eax + 2));
909                 return IRQ_NONE;
910         } else if (!netif_running(dev)) {
911                 /* The 3c523 has this habit of generating interrupts during the
912                    reset.  I'm not sure if the ni52 has this same problem, but it's
913                    really annoying if we haven't finished initializing it.  I was
914                    hoping all the elmc_id_* commands would disable this, but I
915                    might have missed a few. */
916
917                 elmc_id_attn586();      /* ack inter. and disable any more */
918                 return IRQ_HANDLED;
919         } else if (!(ELMC_CTRL_INT & inb(dev->base_addr + ELMC_CTRL))) {
920                 /* wasn't this device */
921                 return IRQ_NONE;
922         }
923         /* reading ELMC_CTRL also clears the INT bit. */
924
925         p = (struct priv *) dev->priv;
926
927         while ((stat = p->scb->status & STAT_MASK)) 
928         {
929                 p->scb->cmd = stat;
930                 elmc_attn586(); /* ack inter. */
931
932                 if (stat & STAT_CX) {
933                         /* command with I-bit set complete */
934                         elmc_xmt_int(dev);
935                 }
936                 if (stat & STAT_FR) {
937                         /* received a frame */
938                         elmc_rcv_int(dev);
939                 }
940 #ifndef NO_NOPCOMMANDS
941                 if (stat & STAT_CNA) {
942                         /* CU went 'not ready' */
943                         if (netif_running(dev)) {
944                                 printk(KERN_WARNING "%s: oops! CU has left active state. stat: %04x/%04x.\n", dev->name, (int) stat, (int) p->scb->status);
945                         }
946                 }
947 #endif
948
949                 if (stat & STAT_RNR) {
950                         /* RU went 'not ready' */
951
952                         if (p->scb->status & RU_SUSPEND) {
953                                 /* special case: RU_SUSPEND */
954
955                                 WAIT_4_SCB_CMD();
956                                 p->scb->cmd = RUC_RESUME;
957                                 elmc_attn586();
958                         } else {
959                                 printk(KERN_WARNING "%s: Receiver-Unit went 'NOT READY': %04x/%04x.\n", dev->name, (int) stat, (int) p->scb->status);
960                                 elmc_rnr_int(dev);
961                         }
962                 }
963                 WAIT_4_SCB_CMD();       /* wait for ack. (elmc_xmt_int can be faster than ack!!) */
964                 if (p->scb->cmd) {      /* timed out? */
965                         break;
966                 }
967         }
968         return IRQ_HANDLED;
969 }
970
971 /*******************************************************
972  * receive-interrupt
973  */
974
975 static void elmc_rcv_int(struct net_device *dev)
976 {
977         int status;
978         unsigned short totlen;
979         struct sk_buff *skb;
980         struct rbd_struct *rbd;
981         struct priv *p = (struct priv *) dev->priv;
982
983         for (; (status = p->rfd_top->status) & STAT_COMPL;) {
984                 rbd = (struct rbd_struct *) make32(p->rfd_top->rbd_offset);
985
986                 if (status & STAT_OK) {         /* frame received without error? */
987                         if ((totlen = rbd->status) & RBD_LAST) {        /* the first and the last buffer? */
988                                 totlen &= RBD_MASK;     /* length of this frame */
989                                 rbd->status = 0;
990                                 skb = (struct sk_buff *) dev_alloc_skb(totlen + 2);
991                                 if (skb != NULL) {
992                                         skb->dev = dev;
993                                         skb_reserve(skb, 2);    /* 16 byte alignment */
994                                         skb_put(skb,totlen);
995                                         eth_copy_and_sum(skb, (char *) p->base+(unsigned long) rbd->buffer,totlen,0);
996                                         skb->protocol = eth_type_trans(skb, dev);
997                                         netif_rx(skb);
998                                         dev->last_rx = jiffies;
999                                         p->stats.rx_packets++;
1000                                         p->stats.rx_bytes += totlen;
1001                                 } else {
1002                                         p->stats.rx_dropped++;
1003                                 }
1004                         } else {
1005                                 printk(KERN_WARNING "%s: received oversized frame.\n", dev->name);
1006                                 p->stats.rx_dropped++;
1007                         }
1008                 } else {        /* frame !(ok), only with 'save-bad-frames' */
1009                         printk(KERN_WARNING "%s: oops! rfd-error-status: %04x\n", dev->name, status);
1010                         p->stats.rx_errors++;
1011                 }
1012                 p->rfd_top->status = 0;
1013                 p->rfd_top->last = RFD_SUSP;
1014                 p->rfd_last->last = 0;  /* delete RU_SUSP  */
1015                 p->rfd_last = p->rfd_top;
1016                 p->rfd_top = (struct rfd_struct *) make32(p->rfd_top->next);    /* step to next RFD */
1017         }
1018 }
1019
1020 /**********************************************************
1021  * handle 'Receiver went not ready'.
1022  */
1023
1024 static void elmc_rnr_int(struct net_device *dev)
1025 {
1026         struct priv *p = (struct priv *) dev->priv;
1027
1028         p->stats.rx_errors++;
1029
1030         WAIT_4_SCB_CMD();       /* wait for the last cmd */
1031         p->scb->cmd = RUC_ABORT;        /* usually the RU is in the 'no resource'-state .. abort it now. */
1032         elmc_attn586();
1033         WAIT_4_SCB_CMD();       /* wait for accept cmd. */
1034
1035         alloc_rfa(dev, (char *) p->rfd_first);
1036         startrecv586(dev);      /* restart RU */
1037
1038         printk(KERN_WARNING "%s: Receive-Unit restarted. Status: %04x\n", dev->name, p->scb->status);
1039
1040 }
1041
1042 /**********************************************************
1043  * handle xmit - interrupt
1044  */
1045
1046 static void elmc_xmt_int(struct net_device *dev)
1047 {
1048         int status;
1049         struct priv *p = (struct priv *) dev->priv;
1050
1051         status = p->xmit_cmds[p->xmit_last]->cmd_status;
1052         if (!(status & STAT_COMPL)) {
1053                 printk(KERN_WARNING "%s: strange .. xmit-int without a 'COMPLETE'\n", dev->name);
1054         }
1055         if (status & STAT_OK) {
1056                 p->stats.tx_packets++;
1057                 p->stats.collisions += (status & TCMD_MAXCOLLMASK);
1058         } else {
1059                 p->stats.tx_errors++;
1060                 if (status & TCMD_LATECOLL) {
1061                         printk(KERN_WARNING "%s: late collision detected.\n", dev->name);
1062                         p->stats.collisions++;
1063                 } else if (status & TCMD_NOCARRIER) {
1064                         p->stats.tx_carrier_errors++;
1065                         printk(KERN_WARNING "%s: no carrier detected.\n", dev->name);
1066                 } else if (status & TCMD_LOSTCTS) {
1067                         printk(KERN_WARNING "%s: loss of CTS detected.\n", dev->name);
1068                 } else if (status & TCMD_UNDERRUN) {
1069                         p->stats.tx_fifo_errors++;
1070                         printk(KERN_WARNING "%s: DMA underrun detected.\n", dev->name);
1071                 } else if (status & TCMD_MAXCOLL) {
1072                         printk(KERN_WARNING "%s: Max. collisions exceeded.\n", dev->name);
1073                         p->stats.collisions += 16;
1074                 }
1075         }
1076
1077 #if (NUM_XMIT_BUFFS != 1)
1078         if ((++p->xmit_last) == NUM_XMIT_BUFFS) {
1079                 p->xmit_last = 0;
1080         }
1081 #endif
1082
1083         netif_wake_queue(dev);
1084 }
1085
1086 /***********************************************************
1087  * (re)start the receiver
1088  */
1089
1090 static void startrecv586(struct net_device *dev)
1091 {
1092         struct priv *p = (struct priv *) dev->priv;
1093
1094         p->scb->rfa_offset = make16(p->rfd_first);
1095         p->scb->cmd = RUC_START;
1096         elmc_attn586();         /* start cmd. */
1097         WAIT_4_SCB_CMD();       /* wait for accept cmd. (no timeout!!) */
1098 }
1099
1100 /******************************************************
1101  * timeout
1102  */
1103  
1104 static void elmc_timeout(struct net_device *dev)
1105 {
1106         struct priv *p = (struct priv *) dev->priv;
1107         /* COMMAND-UNIT active? */
1108         if (p->scb->status & CU_ACTIVE) {
1109 #ifdef DEBUG
1110                 printk("%s: strange ... timeout with CU active?!?\n", dev->name);
1111                 printk("%s: X0: %04x N0: %04x N1: %04x %d\n", dev->name, (int) p->xmit_cmds[0]->cmd_status, (int) p->nop_cmds[0]->cmd_status, (int) p->nop_cmds[1]->cmd_status, (int) p->nop_point);
1112 #endif
1113                 p->scb->cmd = CUC_ABORT;
1114                 elmc_attn586();
1115                 WAIT_4_SCB_CMD();
1116                 p->scb->cbl_offset = make16(p->nop_cmds[p->nop_point]);
1117                 p->scb->cmd = CUC_START;
1118                 elmc_attn586();
1119                 WAIT_4_SCB_CMD();
1120                 netif_wake_queue(dev);
1121         } else {
1122 #ifdef DEBUG
1123                 printk("%s: xmitter timed out, try to restart! stat: %04x\n", dev->name, p->scb->status);
1124                 printk("%s: command-stats: %04x %04x\n", dev->name, p->xmit_cmds[0]->cmd_status, p->xmit_cmds[1]->cmd_status);
1125 #endif
1126                 elmc_close(dev);
1127                 elmc_open(dev);
1128         }
1129 }
1130  
1131 /******************************************************
1132  * send frame
1133  */
1134
1135 static int elmc_send_packet(struct sk_buff *skb, struct net_device *dev)
1136 {
1137         int len;
1138         int i;
1139 #ifndef NO_NOPCOMMANDS
1140         int next_nop;
1141 #endif
1142         struct priv *p = (struct priv *) dev->priv;
1143
1144         netif_stop_queue(dev);
1145
1146         len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
1147         
1148         if (len != skb->len)
1149                 memset((char *) p->xmit_cbuffs[p->xmit_count], 0, ETH_ZLEN);
1150         memcpy((char *) p->xmit_cbuffs[p->xmit_count], (char *) (skb->data), skb->len);
1151
1152 #if (NUM_XMIT_BUFFS == 1)
1153 #ifdef NO_NOPCOMMANDS
1154         p->xmit_buffs[0]->size = TBD_LAST | len;
1155         for (i = 0; i < 16; i++) {
1156                 p->scb->cbl_offset = make16(p->xmit_cmds[0]);
1157                 p->scb->cmd = CUC_START;
1158                 p->xmit_cmds[0]->cmd_status = 0;
1159                         elmc_attn586();
1160                 dev->trans_start = jiffies;
1161                 if (!i) {
1162                         dev_kfree_skb(skb);
1163                 }
1164                 WAIT_4_SCB_CMD();
1165                 if ((p->scb->status & CU_ACTIVE)) {     /* test it, because CU sometimes doesn't start immediately */
1166                         break;
1167                 }
1168                 if (p->xmit_cmds[0]->cmd_status) {
1169                         break;
1170                 }
1171                 if (i == 15) {
1172                         printk(KERN_WARNING "%s: Can't start transmit-command.\n", dev->name);
1173                 }
1174         }
1175 #else
1176         next_nop = (p->nop_point + 1) & 0x1;
1177         p->xmit_buffs[0]->size = TBD_LAST | len;
1178         
1179         p->xmit_cmds[0]->cmd_link = p->nop_cmds[next_nop]->cmd_link
1180             = make16((p->nop_cmds[next_nop]));
1181         p->xmit_cmds[0]->cmd_status = p->nop_cmds[next_nop]->cmd_status = 0;
1182
1183         p->nop_cmds[p->nop_point]->cmd_link = make16((p->xmit_cmds[0]));
1184         dev->trans_start = jiffies;
1185         p->nop_point = next_nop;
1186         dev_kfree_skb(skb);
1187 #endif
1188 #else
1189         p->xmit_buffs[p->xmit_count]->size = TBD_LAST | len;
1190         if ((next_nop = p->xmit_count + 1) == NUM_XMIT_BUFFS) {
1191                 next_nop = 0;
1192         }
1193         p->xmit_cmds[p->xmit_count]->cmd_status = 0;
1194         p->xmit_cmds[p->xmit_count]->cmd_link = p->nop_cmds[next_nop]->cmd_link
1195             = make16((p->nop_cmds[next_nop]));
1196         p->nop_cmds[next_nop]->cmd_status = 0;
1197                 p->nop_cmds[p->xmit_count]->cmd_link = make16((p->xmit_cmds[p->xmit_count]));
1198         dev->trans_start = jiffies;
1199         p->xmit_count = next_nop;
1200         if (p->xmit_count != p->xmit_last)
1201                 netif_wake_queue(dev);
1202         dev_kfree_skb(skb);
1203 #endif
1204         return 0;
1205 }
1206
1207 /*******************************************
1208  * Someone wanna have the statistics
1209  */
1210
1211 static struct net_device_stats *elmc_get_stats(struct net_device *dev)
1212 {
1213         struct priv *p = (struct priv *) dev->priv;
1214         unsigned short crc, aln, rsc, ovrn;
1215
1216         crc = p->scb->crc_errs; /* get error-statistic from the ni82586 */
1217         p->scb->crc_errs -= crc;
1218         aln = p->scb->aln_errs;
1219         p->scb->aln_errs -= aln;
1220         rsc = p->scb->rsc_errs;
1221         p->scb->rsc_errs -= rsc;
1222         ovrn = p->scb->ovrn_errs;
1223         p->scb->ovrn_errs -= ovrn;
1224
1225         p->stats.rx_crc_errors += crc;
1226         p->stats.rx_fifo_errors += ovrn;
1227         p->stats.rx_frame_errors += aln;
1228         p->stats.rx_dropped += rsc;
1229
1230         return &p->stats;
1231 }
1232
1233 /********************************************************
1234  * Set MC list ..
1235  */
1236
1237 #ifdef ELMC_MULTICAST
1238 static void set_multicast_list(struct net_device *dev)
1239 {
1240         if (!dev->start) {
1241                 /* without a running interface, promiscuous doesn't work */
1242                 return;
1243         }
1244         dev->start = 0;
1245         alloc586(dev);
1246         init586(dev);
1247         startrecv586(dev);
1248         dev->start = 1;
1249 }
1250 #endif
1251
1252 static void netdev_get_drvinfo(struct net_device *dev,
1253                                struct ethtool_drvinfo *info)
1254 {
1255         strcpy(info->driver, DRV_NAME);
1256         strcpy(info->version, DRV_VERSION);
1257         sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
1258 }
1259
1260 static struct ethtool_ops netdev_ethtool_ops = {
1261         .get_drvinfo            = netdev_get_drvinfo,
1262 };
1263
1264 #ifdef MODULE
1265
1266 /* Increase if needed ;) */
1267 #define MAX_3C523_CARDS 4
1268
1269 static struct net_device *dev_elmc[MAX_3C523_CARDS];
1270 static int irq[MAX_3C523_CARDS];
1271 static int io[MAX_3C523_CARDS];
1272 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_3C523_CARDS) "i");
1273 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_3C523_CARDS) "i");
1274 MODULE_PARM_DESC(io, "EtherLink/MC I/O base address(es)");
1275 MODULE_PARM_DESC(irq, "EtherLink/MC IRQ number(s)");
1276
1277 int init_module(void)
1278 {
1279         int this_dev,found = 0;
1280
1281         /* Loop until we either can't find any more cards, or we have MAX_3C523_CARDS */        
1282         for(this_dev=0; this_dev<MAX_3C523_CARDS; this_dev++) {
1283                 struct net_device *dev = alloc_etherdev(sizeof(struct priv));
1284                 if (!dev)
1285                         break;
1286                 dev->irq=irq[this_dev];
1287                 dev->base_addr=io[this_dev];
1288                 if (do_elmc_probe(dev) == 0) {
1289                         if (register_netdev(dev) == 0) {
1290                                 dev_elmc[this_dev] = dev;
1291                                 found++;
1292                                 continue;
1293                         }
1294                         cleanup_card(dev);
1295                 }
1296                 free_netdev(dev);
1297                 if (io[this_dev]==0)
1298                         break;
1299                 printk(KERN_WARNING "3c523.c: No 3c523 card found at io=%#x\n",io[this_dev]);
1300         }
1301
1302         if(found==0) {
1303                 if(io[0]==0) printk(KERN_NOTICE "3c523.c: No 3c523 cards found\n");
1304                 return -ENXIO;
1305         } else return 0;
1306 }
1307
1308 void cleanup_module(void)
1309 {
1310         int this_dev;
1311         for (this_dev=0; this_dev<MAX_3C523_CARDS; this_dev++) {
1312                 struct net_device *dev = dev_elmc[this_dev];
1313                 if (dev) {
1314                         unregister_netdev(dev);
1315                         cleanup_card(dev);
1316                         free_netdev(dev);
1317                 }
1318         }
1319 }
1320
1321 #endif                          /* MODULE */