Import 2.2.0pre6
[davej-history.git] / drivers / net / 3c523.c
blobdd0b3826783a13d63ff85d67e8db9f5596f80504
1 /*
2 net-3-driver for the 3c523 Etherlink/MC card (i82586 Ethernet chip)
5 This is an extension to the Linux operating system, and is covered by the
6 same Gnu Public License that covers that work.
8 Copyright 1995, 1996 by Chris Beauregard (cpbeaure@undergrad.math.uwaterloo.ca)
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.
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.
18 sources:
19 ni52.c
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.
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/">
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.
34 The original device probe code was stolen from ps2esdi.c
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.
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?
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.
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.
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.
65 This driver should support multiple ethernet cards, but I can't test
66 that. If someone would I'd greatly appreciate it.
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).
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
80 $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 $
83 #ifdef MODULE
84 #include <linux/module.h>
85 #endif
87 #include <linux/kernel.h>
88 #include <linux/sched.h>
89 #include <linux/string.h>
90 #include <linux/errno.h>
91 #include <linux/ioport.h>
92 #include <linux/malloc.h>
93 #include <linux/interrupt.h>
94 #include <linux/delay.h>
95 #include <linux/mca.h>
96 #include <asm/processor.h>
97 #include <asm/bitops.h>
98 #include <asm/io.h>
100 #include <linux/netdevice.h>
101 #include <linux/etherdevice.h>
102 #include <linux/skbuff.h>
103 #include <linux/init.h>
105 #include "3c523.h"
107 /*************************************************************************/
108 #define DEBUG /* debug on */
109 #define SYSBUSVAL 0 /* 1 = 8 Bit, 0 = 16 bit - 3c523 only does 16 bit */
111 #define make32(ptr16) (p->memtop + (short) (ptr16) )
112 #define make24(ptr32) ((char *) (ptr32) - p->base)
113 #define make16(ptr32) ((unsigned short) ((unsigned long) (ptr32) - (unsigned long) p->memtop ))
115 /*************************************************************************/
117 Tables to which we can map values in the configuration registers.
119 static int irq_table[] __initdata = {
120 12, 7, 3, 9
123 static int csr_table[] __initdata = {
124 0x300, 0x1300, 0x2300, 0x3300
127 static int shm_table[] __initdata = {
128 0x0c0000, 0x0c8000, 0x0d0000, 0x0d8000
131 /******************* how to calculate the buffers *****************************
134 * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
135 * --------------- in a different (more stable?) mode. Only in this mode it's
136 * possible to configure the driver with 'NO_NOPCOMMANDS'
138 sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
139 sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
140 sizeof(rfd) = 24; sizeof(rbd) = 12;
141 sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
142 sizeof(nop_cmd) = 8;
144 * if you don't know the driver, better do not change this values: */
146 #define RECV_BUFF_SIZE 1524 /* slightly oversized */
147 #define XMIT_BUFF_SIZE 1524 /* slightly oversized */
148 #define NUM_XMIT_BUFFS 4 /* config for both, 8K and 16K shmem */
149 #define NUM_RECV_BUFFS_8 1 /* config for 8K shared mem */
150 #define NUM_RECV_BUFFS_16 6 /* config for 16K shared mem */
152 #if (NUM_XMIT_BUFFS == 1)
153 #define NO_NOPCOMMANDS /* only possible with NUM_XMIT_BUFFS=1 */
154 #endif
156 /**************************************************************************/
158 #define DELAY(x) {int i=jiffies; \
159 if(loops_per_sec == 1) \
160 while(time_after(i+(x), jiffies)); \
161 else \
162 __delay((loops_per_sec>>5)*x); \
165 /* a much shorter delay: */
166 #define DELAY_16(); { __delay( (loops_per_sec>>16)+1 ); }
168 /* wait for command with timeout: */
169 #define WAIT_4_SCB_CMD() { int i; \
170 for(i=0;i<1024;i++) { \
171 if(!p->scb->cmd) break; \
172 DELAY_16(); \
173 if(i == 1023) { \
174 printk("%s:%d: scb_cmd timed out .. resetting i82586\n",\
175 dev->name,__LINE__); \
176 elmc_id_reset586(); } } }
178 static void elmc_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr);
179 static int elmc_open(struct device *dev);
180 static int elmc_close(struct device *dev);
181 static int elmc_send_packet(struct sk_buff *, struct device *);
182 static struct net_device_stats *elmc_get_stats(struct device *dev);
183 static void set_multicast_list(struct device *dev);
185 /* helper-functions */
186 static int init586(struct device *dev);
187 static int check586(struct device *dev, char *where, unsigned size);
188 static void alloc586(struct device *dev);
189 static void startrecv586(struct device *dev);
190 static void *alloc_rfa(struct device *dev, void *ptr);
191 static void elmc_rcv_int(struct device *dev);
192 static void elmc_xmt_int(struct device *dev);
193 static void elmc_rnr_int(struct device *dev);
195 struct priv {
196 struct net_device_stats stats;
197 unsigned long base;
198 char *memtop;
199 volatile struct rfd_struct *rfd_last, *rfd_top, *rfd_first;
200 volatile struct scp_struct *scp; /* volatile is important */
201 volatile struct iscp_struct *iscp; /* volatile is important */
202 volatile struct scb_struct *scb; /* volatile is important */
203 volatile struct tbd_struct *xmit_buffs[NUM_XMIT_BUFFS];
204 volatile struct transmit_cmd_struct *xmit_cmds[NUM_XMIT_BUFFS];
205 #if (NUM_XMIT_BUFFS == 1)
206 volatile struct nop_cmd_struct *nop_cmds[2];
207 #else
208 volatile struct nop_cmd_struct *nop_cmds[NUM_XMIT_BUFFS];
209 #endif
210 volatile int nop_point, num_recv_buffs;
211 volatile char *xmit_cbuffs[NUM_XMIT_BUFFS];
212 volatile int xmit_count, xmit_last;
213 volatile int slot;
216 #define elmc_attn586() {elmc_do_attn586(dev->base_addr,ELMC_CTRL_INTE);}
217 #define elmc_reset586() {elmc_do_reset586(dev->base_addr,ELMC_CTRL_INTE);}
219 /* with interrupts disabled - this will clear the interrupt bit in the
220 3c523 control register, and won't put it back. This effectively
221 disables interrupts on the card. */
222 #define elmc_id_attn586() {elmc_do_attn586(dev->base_addr,0);}
223 #define elmc_id_reset586() {elmc_do_reset586(dev->base_addr,0);}
225 /*************************************************************************/
227 Do a Channel Attention on the 3c523. This is extremely board dependent.
229 static void elmc_do_attn586(int ioaddr, int ints)
231 /* the 3c523 requires a minimum of 500 ns. The delays here might be
232 a little too large, and hence they may cut the performance of the
233 card slightly. If someone who knows a little more about Linux
234 timing would care to play with these, I'd appreciate it. */
236 /* this bit masking stuff is crap. I'd rather have separate
237 registers with strobe triggers for each of these functions. <sigh>
238 Ya take what ya got. */
240 outb(ELMC_CTRL_RST | 0x3 | ELMC_CTRL_CA | ints, ioaddr + ELMC_CTRL);
241 DELAY_16(); /* > 500 ns */
242 outb(ELMC_CTRL_RST | 0x3 | ints, ioaddr + ELMC_CTRL);
245 /*************************************************************************/
247 Reset the 82586 on the 3c523. Also very board dependent.
249 static void elmc_do_reset586(int ioaddr, int ints)
251 /* toggle the RST bit low then high */
252 outb(0x3 | ELMC_CTRL_LBK, ioaddr + ELMC_CTRL);
253 DELAY_16(); /* > 500 ns */
254 outb(ELMC_CTRL_RST | ELMC_CTRL_LBK | 0x3, ioaddr + ELMC_CTRL);
256 elmc_do_attn586(ioaddr, ints);
259 /**********************************************
260 * close device
263 static int elmc_close(struct device *dev)
265 elmc_id_reset586(); /* the hard way to stop the receiver */
267 free_irq(dev->irq, dev);
269 dev->start = 0;
270 dev->tbusy = 0;
272 #ifdef MODULE
273 MOD_DEC_USE_COUNT;
274 #endif
276 return 0;
279 /**********************************************
280 * open device
283 static int elmc_open(struct device *dev)
286 elmc_id_attn586(); /* disable interrupts */
288 if (request_irq(dev->irq, &elmc_interrupt, SA_SHIRQ | SA_SAMPLE_RANDOM,
289 "3c523", dev)
291 printk("%s: couldn't get irq %d\n", dev->name, dev->irq);
292 elmc_id_reset586();
293 return -EAGAIN;
295 alloc586(dev);
296 init586(dev);
297 startrecv586(dev);
299 dev->interrupt = 0;
300 dev->tbusy = 0;
301 dev->start = 1;
303 #ifdef MODULE
304 MOD_INC_USE_COUNT;
305 #endif
307 return 0; /* most done by init */
310 /**********************************************
311 * Check to see if there's an 82586 out there.
314 __initfunc(static int check586(struct device *dev, char *where, unsigned size))
316 struct priv *p = (struct priv *) dev->priv;
317 char *iscp_addrs[2];
318 int i = 0;
320 p->base = (unsigned long) where + size - 0x01000000;
321 p->memtop = where + size;
322 p->scp = (struct scp_struct *) (p->base + SCP_DEFAULT_ADDRESS);
323 memset((char *) p->scp, 0, sizeof(struct scp_struct));
324 p->scp->sysbus = SYSBUSVAL; /* 1 = 8Bit-Bus, 0 = 16 Bit */
326 iscp_addrs[0] = where;
327 iscp_addrs[1] = (char *) p->scp - sizeof(struct iscp_struct);
329 for (i = 0; i < 2; i++) {
330 p->iscp = (struct iscp_struct *) iscp_addrs[i];
331 memset((char *) p->iscp, 0, sizeof(struct iscp_struct));
333 p->scp->iscp = make24(p->iscp);
334 p->iscp->busy = 1;
336 elmc_id_reset586();
338 /* reset586 does an implicit CA */
340 /* apparently, you sometimes have to kick the 82586 twice... */
341 elmc_id_attn586();
343 if (p->iscp->busy) { /* i82586 clears 'busy' after successful init */
344 return 0;
347 return 1;
350 /******************************************************************
351 * set iscp at the right place, called by elmc_probe and open586.
354 void alloc586(struct device *dev)
356 struct priv *p = (struct priv *) dev->priv;
358 elmc_id_reset586();
359 DELAY(2);
361 p->scp = (struct scp_struct *) (p->base + SCP_DEFAULT_ADDRESS);
362 p->scb = (struct scb_struct *) phys_to_virt(dev->mem_start);
363 p->iscp = (struct iscp_struct *) ((char *) p->scp - sizeof(struct iscp_struct));
365 memset((char *) p->iscp, 0, sizeof(struct iscp_struct));
366 memset((char *) p->scp, 0, sizeof(struct scp_struct));
368 p->scp->iscp = make24(p->iscp);
369 p->scp->sysbus = SYSBUSVAL;
370 p->iscp->scb_offset = make16(p->scb);
372 p->iscp->busy = 1;
373 elmc_id_reset586();
374 elmc_id_attn586();
376 DELAY(2);
378 if (p->iscp->busy) {
379 printk("%s: Init-Problems (alloc).\n", dev->name);
381 memset((char *) p->scb, 0, sizeof(struct scb_struct));
384 /*****************************************************************/
386 static int elmc_getinfo(char *buf, int slot, void *d)
388 int len = 0;
389 struct device *dev = (struct device *) d;
390 int i;
392 if (dev == NULL)
393 return len;
395 len += sprintf(buf + len, "Revision: 0x%x\n",
396 inb(dev->base_addr + ELMC_REVISION) & 0xf);
397 len += sprintf(buf + len, "IRQ: %d\n", dev->irq);
398 len += sprintf(buf + len, "IO Address: %#lx-%#lx\n", dev->base_addr,
399 dev->base_addr + ELMC_IO_EXTENT);
400 len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
401 dev->mem_end - 1);
402 len += sprintf(buf + len, "Transceiver: %s\n", dev->if_port ?
403 "External" : "Internal");
404 len += sprintf(buf + len, "Device: %s\n", dev->name);
405 len += sprintf(buf + len, "Hardware Address:");
406 for (i = 0; i < 6; i++) {
407 len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
409 buf[len++] = '\n';
410 buf[len] = 0;
412 return len;
413 } /* elmc_getinfo() */
415 /*****************************************************************/
417 __initfunc(int elmc_probe(struct device *dev))
419 static int slot = 0;
420 int base_addr = dev ? dev->base_addr : 0;
421 int irq = dev ? dev->irq : 0;
422 u_char status = 0;
423 u_char revision = 0;
424 int i = 0;
425 unsigned int size = 0;
427 if (MCA_bus == 0) {
428 return ENODEV;
430 /* search through the slots for the 3c523. */
431 slot = mca_find_adapter(ELMC_MCA_ID, 0);
432 while (slot != -1) {
433 status = mca_read_stored_pos(slot, 2);
436 If we're trying to match a specified irq or IO address,
437 we'll reject a match unless it's what we're looking for.
439 if (base_addr || irq) {
440 /* we're looking for a card at a particular place */
442 if (irq && irq != irq_table[(status & ELMC_STATUS_IRQ_SELECT) >> 6]) {
443 slot = mca_find_adapter(ELMC_MCA_ID, slot + 1);
444 continue;
446 if (base_addr && base_addr != csr_table[(status & ELMC_STATUS_CSR_SELECT) >> 1]) {
447 slot = mca_find_adapter(ELMC_MCA_ID, slot + 1);
448 continue;
451 /* found what we're looking for... */
452 break;
455 /* we didn't find any 3c523 in the slots we checked for */
456 if (slot == MCA_NOTFOUND) {
457 return ((base_addr || irq) ? ENXIO : ENODEV);
459 mca_set_adapter_name(slot, "3Com 3c523 Etherlink/MC");
460 mca_set_adapter_procfn(slot, (MCA_ProcFn) elmc_getinfo, dev);
462 /* if we get this far, adapter has been found - carry on */
463 printk("%s: 3c523 adapter found in slot %d\n", dev->name, slot + 1);
465 /* Now we extract configuration info from the card.
466 The 3c523 provides information in two of the POS registers, but
467 the second one is only needed if we want to tell the card what IRQ
468 to use. I suspect that whoever sets the thing up initially would
469 prefer we don't screw with those things.
471 Note that we read the status info when we found the card...
473 See 3c523.h for more details.
476 /* revision is stored in the first 4 bits of the revision register */
477 revision = inb(dev->base_addr + ELMC_REVISION) & 0xf;
479 /* figure out our irq */
480 dev->irq = irq_table[(status & ELMC_STATUS_IRQ_SELECT) >> 6];
482 /* according to docs, we read the interrupt and write it back to
483 the IRQ select register, since the POST might not configure the IRQ
484 properly. */
485 switch (dev->irq) {
486 case 3:
487 mca_write_pos(slot, 3, 0x04);
488 break;
489 case 7:
490 mca_write_pos(slot, 3, 0x02);
491 break;
492 case 9:
493 mca_write_pos(slot, 3, 0x08);
494 break;
495 case 12:
496 mca_write_pos(slot, 3, 0x01);
497 break;
500 /* Our IO address? */
501 dev->base_addr = csr_table[(status & ELMC_STATUS_CSR_SELECT) >> 1];
503 request_region(dev->base_addr, ELMC_IO_EXTENT, "3c523");
505 dev->priv = (void *) kmalloc(sizeof(struct priv), GFP_KERNEL);
506 if (dev->priv == NULL) {
507 return -ENOMEM;
509 memset((char *) dev->priv, 0, sizeof(struct priv));
511 ((struct priv *) (dev->priv))->slot = slot;
513 printk("%s: 3Com 3c523 Rev 0x%x at %#lx\n", dev->name, (int) revision,
514 dev->base_addr);
516 /* Determine if we're using the on-board transceiver (i.e. coax) or
517 an external one. The information is pretty much useless, but I
518 guess it's worth brownie points. */
519 dev->if_port = (status & ELMC_STATUS_DISABLE_THIN);
521 /* The 3c523 has a 24K chunk of memory. The first 16K is the
522 shared memory, while the last 8K is for the EtherStart BIOS ROM.
523 Which we don't care much about here. We'll just tell Linux that
524 we're using 16K. MCA won't permit address space conflicts caused
525 by not mapping the other 8K. */
526 dev->mem_start = shm_table[(status & ELMC_STATUS_MEMORY_SELECT) >> 3];
528 /* We're using MCA, so it's a given that the information about memory
529 size is correct. The Crynwr drivers do something like this. */
531 elmc_id_reset586(); /* seems like a good idea before checking it... */
533 size = 0x4000; /* check for 16K mem */
534 if (!check586(dev, (char *) phys_to_virt(dev->mem_start), size)) {
535 printk("%s: memprobe, Can't find memory at 0x%lx!\n", dev->name,
536 dev->mem_start);
537 release_region(dev->base_addr, ELMC_IO_EXTENT);
538 return ENODEV;
540 dev->mem_end = dev->mem_start + size; /* set mem_end showed by 'ifconfig' */
542 ((struct priv *) (dev->priv))->base = (unsigned long)phys_to_virt(dev->mem_start + size - 0x01000000);
543 alloc586(dev);
545 elmc_id_reset586(); /* make sure it doesn't generate spurious ints */
547 /* set number of receive-buffs according to memsize */
548 ((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_16;
550 /* dump all the assorted information */
551 printk("%s: IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->name,
552 dev->irq, dev->if_port ? "ex" : "in",
553 dev->mem_start, dev->mem_end - 1);
555 /* The hardware address for the 3c523 is stored in the first six
556 bytes of the IO address. */
557 printk("%s: hardware address ", dev->name);
558 for (i = 0; i < 6; i++) {
559 dev->dev_addr[i] = inb(dev->base_addr + i);
560 printk(" %02x", dev->dev_addr[i]);
562 printk("\n");
564 dev->open = &elmc_open;
565 dev->stop = &elmc_close;
566 dev->get_stats = &elmc_get_stats;
567 dev->hard_start_xmit = &elmc_send_packet;
568 dev->set_multicast_list = &set_multicast_list;
570 ether_setup(dev);
572 dev->tbusy = 0;
573 dev->interrupt = 0;
574 dev->start = 0;
576 /* note that we haven't actually requested the IRQ from the kernel.
577 That gets done in elmc_open(). I'm not sure that's such a good idea,
578 but it works, so I'll go with it. */
580 return 0;
583 /**********************************************
584 * init the chip (elmc-interrupt should be disabled?!)
585 * needs a correct 'allocated' memory
588 static int init586(struct device *dev)
590 void *ptr;
591 unsigned long s;
592 int i, result = 0;
593 struct priv *p = (struct priv *) dev->priv;
594 volatile struct configure_cmd_struct *cfg_cmd;
595 volatile struct iasetup_cmd_struct *ias_cmd;
596 volatile struct tdr_cmd_struct *tdr_cmd;
597 volatile struct mcsetup_cmd_struct *mc_cmd;
598 struct dev_mc_list *dmi = dev->mc_list;
599 int num_addrs = dev->mc_count;
601 ptr = (void *) ((char *) p->scb + sizeof(struct scb_struct));
603 cfg_cmd = (struct configure_cmd_struct *) ptr; /* configure-command */
604 cfg_cmd->cmd_status = 0;
605 cfg_cmd->cmd_cmd = CMD_CONFIGURE | CMD_LAST;
606 cfg_cmd->cmd_link = 0xffff;
608 cfg_cmd->byte_cnt = 0x0a; /* number of cfg bytes */
609 cfg_cmd->fifo = 0x08; /* fifo-limit (8=tx:32/rx:64) */
610 cfg_cmd->sav_bf = 0x40; /* hold or discard bad recv frames (bit 7) */
611 cfg_cmd->adr_len = 0x2e; /* addr_len |!src_insert |pre-len |loopback */
612 cfg_cmd->priority = 0x00;
613 cfg_cmd->ifs = 0x60;
614 cfg_cmd->time_low = 0x00;
615 cfg_cmd->time_high = 0xf2;
616 cfg_cmd->promisc = 0;
617 if (dev->flags & (IFF_ALLMULTI | IFF_PROMISC)) {
618 cfg_cmd->promisc = 1;
619 dev->flags |= IFF_PROMISC;
621 cfg_cmd->carr_coll = 0x00;
623 p->scb->cbl_offset = make16(cfg_cmd);
625 p->scb->cmd = CUC_START; /* cmd.-unit start */
626 elmc_id_attn586();
628 s = jiffies; /* warning: only active with interrupts on !! */
629 while (!(cfg_cmd->cmd_status & STAT_COMPL)) {
630 if (jiffies - s > 30*HZ/100)
631 break;
634 if ((cfg_cmd->cmd_status & (STAT_OK | STAT_COMPL)) != (STAT_COMPL | STAT_OK)) {
635 printk("%s (elmc): configure command failed: %x\n", dev->name, cfg_cmd->cmd_status);
636 return 1;
639 * individual address setup
641 ias_cmd = (struct iasetup_cmd_struct *) ptr;
643 ias_cmd->cmd_status = 0;
644 ias_cmd->cmd_cmd = CMD_IASETUP | CMD_LAST;
645 ias_cmd->cmd_link = 0xffff;
647 memcpy((char *) &ias_cmd->iaddr, (char *) dev->dev_addr, ETH_ALEN);
649 p->scb->cbl_offset = make16(ias_cmd);
651 p->scb->cmd = CUC_START; /* cmd.-unit start */
652 elmc_id_attn586();
654 s = jiffies;
655 while (!(ias_cmd->cmd_status & STAT_COMPL)) {
656 if (jiffies - s > 30*HZ/100)
657 break;
660 if ((ias_cmd->cmd_status & (STAT_OK | STAT_COMPL)) != (STAT_OK | STAT_COMPL)) {
661 printk("%s (elmc): individual address setup command failed: %04x\n", dev->name, ias_cmd->cmd_status);
662 return 1;
665 * TDR, wire check .. e.g. no resistor e.t.c
667 tdr_cmd = (struct tdr_cmd_struct *) ptr;
669 tdr_cmd->cmd_status = 0;
670 tdr_cmd->cmd_cmd = CMD_TDR | CMD_LAST;
671 tdr_cmd->cmd_link = 0xffff;
672 tdr_cmd->status = 0;
674 p->scb->cbl_offset = make16(tdr_cmd);
676 p->scb->cmd = CUC_START; /* cmd.-unit start */
677 elmc_attn586();
679 s = jiffies;
680 while (!(tdr_cmd->cmd_status & STAT_COMPL)) {
681 if (jiffies - s > 30*HZ/100) {
682 printk("%s: %d Problems while running the TDR.\n", dev->name, __LINE__);
683 result = 1;
684 break;
688 if (!result) {
689 DELAY(2); /* wait for result */
690 result = tdr_cmd->status;
692 p->scb->cmd = p->scb->status & STAT_MASK;
693 elmc_id_attn586(); /* ack the interrupts */
695 if (result & TDR_LNK_OK) {
696 /* empty */
697 } else if (result & TDR_XCVR_PRB) {
698 printk("%s: TDR: Transceiver problem!\n", dev->name);
699 } else if (result & TDR_ET_OPN) {
700 printk("%s: TDR: No correct termination %d clocks away.\n", dev->name, result & TDR_TIMEMASK);
701 } else if (result & TDR_ET_SRT) {
702 if (result & TDR_TIMEMASK) /* time == 0 -> strange :-) */
703 printk("%s: TDR: Detected a short circuit %d clocks away.\n", dev->name, result & TDR_TIMEMASK);
704 } else {
705 printk("%s: TDR: Unknown status %04x\n", dev->name, result);
709 * ack interrupts
711 p->scb->cmd = p->scb->status & STAT_MASK;
712 elmc_id_attn586();
715 * alloc nop/xmit-cmds
717 #if (NUM_XMIT_BUFFS == 1)
718 for (i = 0; i < 2; i++) {
719 p->nop_cmds[i] = (struct nop_cmd_struct *) ptr;
720 p->nop_cmds[i]->cmd_cmd = CMD_NOP;
721 p->nop_cmds[i]->cmd_status = 0;
722 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
723 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
725 p->xmit_cmds[0] = (struct transmit_cmd_struct *) ptr; /* transmit cmd/buff 0 */
726 ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
727 #else
728 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
729 p->nop_cmds[i] = (struct nop_cmd_struct *) ptr;
730 p->nop_cmds[i]->cmd_cmd = CMD_NOP;
731 p->nop_cmds[i]->cmd_status = 0;
732 p->nop_cmds[i]->cmd_link = make16((p->nop_cmds[i]));
733 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
734 p->xmit_cmds[i] = (struct transmit_cmd_struct *) ptr; /*transmit cmd/buff 0 */
735 ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
737 #endif
739 ptr = alloc_rfa(dev, (void *) ptr); /* init receive-frame-area */
742 * Multicast setup
745 if (dev->mc_count) {
746 /* I don't understand this: do we really need memory after the init? */
747 int len = ((char *) p->iscp - (char *) ptr - 8) / 6;
748 if (len <= 0) {
749 printk("%s: Ooooops, no memory for MC-Setup!\n", dev->name);
750 } else {
751 if (len < num_addrs) {
752 num_addrs = len;
753 printk("%s: Sorry, can only apply %d MC-Address(es).\n",
754 dev->name, num_addrs);
756 mc_cmd = (struct mcsetup_cmd_struct *) ptr;
757 mc_cmd->cmd_status = 0;
758 mc_cmd->cmd_cmd = CMD_MCSETUP | CMD_LAST;
759 mc_cmd->cmd_link = 0xffff;
760 mc_cmd->mc_cnt = num_addrs * 6;
761 for (i = 0; i < num_addrs; i++) {
762 memcpy((char *) mc_cmd->mc_list[i], dmi->dmi_addr, 6);
763 dmi = dmi->next;
765 p->scb->cbl_offset = make16(mc_cmd);
766 p->scb->cmd = CUC_START;
767 elmc_id_attn586();
768 s = jiffies;
769 while (!(mc_cmd->cmd_status & STAT_COMPL)) {
770 if (jiffies - s > 30*HZ/100)
771 break;
773 if (!(mc_cmd->cmd_status & STAT_COMPL)) {
774 printk("%s: Can't apply multicast-address-list.\n", dev->name);
779 * alloc xmit-buffs / init xmit_cmds
781 for (i = 0; i < NUM_XMIT_BUFFS; i++) {
782 p->xmit_cbuffs[i] = (char *) ptr; /* char-buffs */
783 ptr = (char *) ptr + XMIT_BUFF_SIZE;
784 p->xmit_buffs[i] = (struct tbd_struct *) ptr; /* TBD */
785 ptr = (char *) ptr + sizeof(struct tbd_struct);
786 if ((void *) ptr > (void *) p->iscp) {
787 printk("%s: not enough shared-mem for your configuration!\n", dev->name);
788 return 1;
790 memset((char *) (p->xmit_cmds[i]), 0, sizeof(struct transmit_cmd_struct));
791 memset((char *) (p->xmit_buffs[i]), 0, sizeof(struct tbd_struct));
792 p->xmit_cmds[i]->cmd_status = STAT_COMPL;
793 p->xmit_cmds[i]->cmd_cmd = CMD_XMIT | CMD_INT;
794 p->xmit_cmds[i]->tbd_offset = make16((p->xmit_buffs[i]));
795 p->xmit_buffs[i]->next = 0xffff;
796 p->xmit_buffs[i]->buffer = make24((p->xmit_cbuffs[i]));
799 p->xmit_count = 0;
800 p->xmit_last = 0;
801 #ifndef NO_NOPCOMMANDS
802 p->nop_point = 0;
803 #endif
806 * 'start transmitter' (nop-loop)
808 #ifndef NO_NOPCOMMANDS
809 p->scb->cbl_offset = make16(p->nop_cmds[0]);
810 p->scb->cmd = CUC_START;
811 elmc_id_attn586();
812 WAIT_4_SCB_CMD();
813 #else
814 p->xmit_cmds[0]->cmd_link = 0xffff;
815 p->xmit_cmds[0]->cmd_cmd = CMD_XMIT | CMD_LAST | CMD_INT;
816 #endif
818 return 0;
821 /******************************************************
822 * This is a helper routine for elmc_rnr_int() and init586().
823 * It sets up the Receive Frame Area (RFA).
826 static void *alloc_rfa(struct device *dev, void *ptr)
828 volatile struct rfd_struct *rfd = (struct rfd_struct *) ptr;
829 volatile struct rbd_struct *rbd;
830 int i;
831 struct priv *p = (struct priv *) dev->priv;
833 memset((char *) rfd, 0, sizeof(struct rfd_struct) * p->num_recv_buffs);
834 p->rfd_first = rfd;
836 for (i = 0; i < p->num_recv_buffs; i++) {
837 rfd[i].next = make16(rfd + (i + 1) % p->num_recv_buffs);
839 rfd[p->num_recv_buffs - 1].last = RFD_SUSP; /* RU suspend */
841 ptr = (void *) (rfd + p->num_recv_buffs);
843 rbd = (struct rbd_struct *) ptr;
844 ptr = (void *) (rbd + p->num_recv_buffs);
846 /* clr descriptors */
847 memset((char *) rbd, 0, sizeof(struct rbd_struct) * p->num_recv_buffs);
849 for (i = 0; i < p->num_recv_buffs; i++) {
850 rbd[i].next = make16((rbd + (i + 1) % p->num_recv_buffs));
851 rbd[i].size = RECV_BUFF_SIZE;
852 rbd[i].buffer = make24(ptr);
853 ptr = (char *) ptr + RECV_BUFF_SIZE;
856 p->rfd_top = p->rfd_first;
857 p->rfd_last = p->rfd_first + p->num_recv_buffs - 1;
859 p->scb->rfa_offset = make16(p->rfd_first);
860 p->rfd_first->rbd_offset = make16(rbd);
862 return ptr;
866 /**************************************************
867 * Interrupt Handler ...
870 static void elmc_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
872 struct device *dev = (struct device *) dev_id;
873 unsigned short stat;
874 struct priv *p;
876 if (dev == NULL) {
877 printk("elmc-interrupt: irq %d for unknown device.\n", (int) -(((struct pt_regs *) reg_ptr)->orig_eax + 2));
878 return;
879 } else if (!dev->start) {
880 /* The 3c523 has this habit of generating interrupts during the
881 reset. I'm not sure if the ni52 has this same problem, but it's
882 really annoying if we haven't finished initializing it. I was
883 hoping all the elmc_id_* commands would disable this, but I
884 might have missed a few. */
886 elmc_id_attn586(); /* ack inter. and disable any more */
887 return;
888 } else if (!(ELMC_CTRL_INT & inb(dev->base_addr + ELMC_CTRL))) {
889 /* wasn't this device */
890 return;
892 /* reading ELMC_CTRL also clears the INT bit. */
894 p = (struct priv *) dev->priv;
896 dev->interrupt = 1;
898 while ((stat = p->scb->status & STAT_MASK))
900 p->scb->cmd = stat;
901 elmc_attn586(); /* ack inter. */
903 if (stat & STAT_CX) {
904 /* command with I-bit set complete */
905 elmc_xmt_int(dev);
907 if (stat & STAT_FR) {
908 /* received a frame */
909 elmc_rcv_int(dev);
911 #ifndef NO_NOPCOMMANDS
912 if (stat & STAT_CNA) {
913 /* CU went 'not ready' */
914 if (dev->start) {
915 printk("%s: oops! CU has left active state. stat: %04x/%04x.\n", dev->name, (int) stat, (int) p->scb->status);
918 #endif
920 if (stat & STAT_RNR) {
921 /* RU went 'not ready' */
923 if (p->scb->status & RU_SUSPEND) {
924 /* special case: RU_SUSPEND */
926 WAIT_4_SCB_CMD();
927 p->scb->cmd = RUC_RESUME;
928 elmc_attn586();
929 } else {
930 printk("%s: Receiver-Unit went 'NOT READY': %04x/%04x.\n", dev->name, (int) stat, (int) p->scb->status);
931 elmc_rnr_int(dev);
934 WAIT_4_SCB_CMD(); /* wait for ack. (elmc_xmt_int can be faster than ack!!) */
935 if (p->scb->cmd) { /* timed out? */
936 break;
940 dev->interrupt = 0;
943 /*******************************************************
944 * receive-interrupt
947 static void elmc_rcv_int(struct device *dev)
949 int status;
950 unsigned short totlen;
951 struct sk_buff *skb;
952 struct rbd_struct *rbd;
953 struct priv *p = (struct priv *) dev->priv;
955 for (; (status = p->rfd_top->status) & STAT_COMPL;) {
956 rbd = (struct rbd_struct *) make32(p->rfd_top->rbd_offset);
958 if (status & STAT_OK) { /* frame received without error? */
959 if ((totlen = rbd->status) & RBD_LAST) { /* the first and the last buffer? */
960 totlen &= RBD_MASK; /* length of this frame */
961 rbd->status = 0;
962 skb = (struct sk_buff *) dev_alloc_skb(totlen + 2);
963 if (skb != NULL) {
964 skb->dev = dev;
965 skb_reserve(skb, 2); /* 16 byte alignment */
966 memcpy(skb_put(skb, totlen), (char *) p->base + (unsigned long) rbd->buffer, totlen);
967 skb->protocol = eth_type_trans(skb, dev);
968 netif_rx(skb);
969 p->stats.rx_packets++;
970 p->stats.rx_bytes += totlen;
971 } else {
972 p->stats.rx_dropped++;
974 } else {
975 printk("%s: received oversized frame.\n", dev->name);
976 p->stats.rx_dropped++;
978 } else { /* frame !(ok), only with 'save-bad-frames' */
979 printk("%s: oops! rfd-error-status: %04x\n", dev->name, status);
980 p->stats.rx_errors++;
982 p->rfd_top->status = 0;
983 p->rfd_top->last = RFD_SUSP;
984 p->rfd_last->last = 0; /* delete RU_SUSP */
985 p->rfd_last = p->rfd_top;
986 p->rfd_top = (struct rfd_struct *) make32(p->rfd_top->next); /* step to next RFD */
990 /**********************************************************
991 * handle 'Receiver went not ready'.
994 static void elmc_rnr_int(struct device *dev)
996 struct priv *p = (struct priv *) dev->priv;
998 p->stats.rx_errors++;
1000 WAIT_4_SCB_CMD(); /* wait for the last cmd */
1001 p->scb->cmd = RUC_ABORT; /* usually the RU is in the 'no resource'-state .. abort it now. */
1002 elmc_attn586();
1003 WAIT_4_SCB_CMD(); /* wait for accept cmd. */
1005 alloc_rfa(dev, (char *) p->rfd_first);
1006 startrecv586(dev); /* restart RU */
1008 printk("%s: Receive-Unit restarted. Status: %04x\n", dev->name, p->scb->status);
1012 /**********************************************************
1013 * handle xmit - interrupt
1016 static void elmc_xmt_int(struct device *dev)
1018 int status;
1019 struct priv *p = (struct priv *) dev->priv;
1021 status = p->xmit_cmds[p->xmit_last]->cmd_status;
1022 if (!(status & STAT_COMPL)) {
1023 printk("%s: strange .. xmit-int without a 'COMPLETE'\n", dev->name);
1025 if (status & STAT_OK) {
1026 p->stats.tx_packets++;
1027 p->stats.collisions += (status & TCMD_MAXCOLLMASK);
1028 } else {
1029 p->stats.tx_errors++;
1030 if (status & TCMD_LATECOLL) {
1031 printk("%s: late collision detected.\n", dev->name);
1032 p->stats.collisions++;
1033 } else if (status & TCMD_NOCARRIER) {
1034 p->stats.tx_carrier_errors++;
1035 printk("%s: no carrier detected.\n", dev->name);
1036 } else if (status & TCMD_LOSTCTS) {
1037 printk("%s: loss of CTS detected.\n", dev->name);
1038 } else if (status & TCMD_UNDERRUN) {
1039 p->stats.tx_fifo_errors++;
1040 printk("%s: DMA underrun detected.\n", dev->name);
1041 } else if (status & TCMD_MAXCOLL) {
1042 printk("%s: Max. collisions exceeded.\n", dev->name);
1043 p->stats.collisions += 16;
1047 #if (NUM_XMIT_BUFFS != 1)
1048 if ((++p->xmit_last) == NUM_XMIT_BUFFS) {
1049 p->xmit_last = 0;
1051 #endif
1053 dev->tbusy = 0;
1054 mark_bh(NET_BH);
1057 /***********************************************************
1058 * (re)start the receiver
1061 static void startrecv586(struct device *dev)
1063 struct priv *p = (struct priv *) dev->priv;
1065 p->scb->rfa_offset = make16(p->rfd_first);
1066 p->scb->cmd = RUC_START;
1067 elmc_attn586(); /* start cmd. */
1068 WAIT_4_SCB_CMD(); /* wait for accept cmd. (no timeout!!) */
1071 /******************************************************
1072 * send frame
1075 static int elmc_send_packet(struct sk_buff *skb, struct device *dev)
1077 int len;
1078 #ifndef NO_NOPCOMMANDS
1079 int next_nop;
1080 #endif
1081 struct priv *p = (struct priv *) dev->priv;
1083 if (dev->tbusy) {
1084 int tickssofar = jiffies - dev->trans_start;
1085 if (tickssofar < 5) {
1086 return 1;
1088 /* COMMAND-UNIT active? */
1089 if (p->scb->status & CU_ACTIVE) {
1090 dev->tbusy = 0;
1091 #ifdef DEBUG
1092 printk("%s: strange ... timeout with CU active?!?\n", dev->name);
1093 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);
1094 #endif
1095 p->scb->cmd = CUC_ABORT;
1096 elmc_attn586();
1097 WAIT_4_SCB_CMD();
1098 p->scb->cbl_offset = make16(p->nop_cmds[p->nop_point]);
1099 p->scb->cmd = CUC_START;
1100 elmc_attn586();
1101 WAIT_4_SCB_CMD();
1102 dev->trans_start = jiffies;
1103 return 0;
1104 } else {
1105 #ifdef DEBUG
1106 printk("%s: xmitter timed out, try to restart! stat: %04x\n", dev->name, p->scb->status);
1107 printk("%s: command-stats: %04x %04x\n", dev->name, p->xmit_cmds[0]->cmd_status, p->xmit_cmds[1]->cmd_status);
1108 #endif
1109 elmc_close(dev);
1110 elmc_open(dev);
1112 dev->trans_start = jiffies;
1113 return 0;
1115 if (test_and_set_bit(0, (void *) &dev->tbusy) != 0) {
1116 printk("%s: Transmitter access conflict.\n", dev->name);
1117 } else {
1118 memcpy((char *) p->xmit_cbuffs[p->xmit_count], (char *) (skb->data), skb->len);
1119 len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
1121 #if (NUM_XMIT_BUFFS == 1)
1122 #ifdef NO_NOPCOMMANDS
1123 p->xmit_buffs[0]->size = TBD_LAST | len;
1124 for (i = 0; i < 16; i++) {
1125 p->scb->cbl_offset = make16(p->xmit_cmds[0]);
1126 p->scb->cmd = CUC_START;
1127 p->xmit_cmds[0]->cmd_status = 0;
1129 elmc_attn586();
1130 dev->trans_start = jiffies;
1131 if (!i) {
1132 dev_kfree_skb(skb);
1134 WAIT_4_SCB_CMD();
1135 if ((p->scb->status & CU_ACTIVE)) { /* test it, because CU sometimes doesn't start immediately */
1136 break;
1138 if (p->xmit_cmds[0]->cmd_status) {
1139 break;
1141 if (i == 15) {
1142 printk("%s: Can't start transmit-command.\n", dev->name);
1145 #else
1146 next_nop = (p->nop_point + 1) & 0x1;
1147 p->xmit_buffs[0]->size = TBD_LAST | len;
1149 p->xmit_cmds[0]->cmd_link = p->nop_cmds[next_nop]->cmd_link
1150 = make16((p->nop_cmds[next_nop]));
1151 p->xmit_cmds[0]->cmd_status = p->nop_cmds[next_nop]->cmd_status = 0;
1153 p->nop_cmds[p->nop_point]->cmd_link = make16((p->xmit_cmds[0]));
1154 dev->trans_start = jiffies;
1155 p->nop_point = next_nop;
1156 dev_kfree_skb(skb);
1157 #endif
1158 #else
1159 p->xmit_buffs[p->xmit_count]->size = TBD_LAST | len;
1160 if ((next_nop = p->xmit_count + 1) == NUM_XMIT_BUFFS) {
1161 next_nop = 0;
1163 p->xmit_cmds[p->xmit_count]->cmd_status = 0;
1164 p->xmit_cmds[p->xmit_count]->cmd_link = p->nop_cmds[next_nop]->cmd_link
1165 = make16((p->nop_cmds[next_nop]));
1166 p->nop_cmds[next_nop]->cmd_status = 0;
1168 p->nop_cmds[p->xmit_count]->cmd_link = make16((p->xmit_cmds[p->xmit_count]));
1169 dev->trans_start = jiffies;
1170 p->xmit_count = next_nop;
1172 cli();
1173 if (p->xmit_count != p->xmit_last) {
1174 dev->tbusy = 0;
1176 sti();
1177 dev_kfree_skb(skb);
1178 #endif
1180 return 0;
1183 /*******************************************
1184 * Someone wanna have the statistics
1187 static struct net_device_stats *elmc_get_stats(struct device *dev)
1189 struct priv *p = (struct priv *) dev->priv;
1190 unsigned short crc, aln, rsc, ovrn;
1192 crc = p->scb->crc_errs; /* get error-statistic from the ni82586 */
1193 p->scb->crc_errs -= crc;
1194 aln = p->scb->aln_errs;
1195 p->scb->aln_errs -= aln;
1196 rsc = p->scb->rsc_errs;
1197 p->scb->rsc_errs -= rsc;
1198 ovrn = p->scb->ovrn_errs;
1199 p->scb->ovrn_errs -= ovrn;
1201 p->stats.rx_crc_errors += crc;
1202 p->stats.rx_fifo_errors += ovrn;
1203 p->stats.rx_frame_errors += aln;
1204 p->stats.rx_dropped += rsc;
1206 return &p->stats;
1209 /********************************************************
1210 * Set MC list ..
1213 static void set_multicast_list(struct device *dev)
1215 if (!dev->start) {
1216 /* without a running interface, promiscuous doesn't work */
1217 return;
1219 dev->start = 0;
1220 alloc586(dev);
1221 init586(dev);
1222 startrecv586(dev);
1223 dev->start = 1;
1226 /*************************************************************************/
1228 #ifdef MODULE
1230 static char devicename[9] = {0,};
1232 static struct device dev_elmc =
1234 devicename /*"3c523" */ , 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, elmc_probe
1237 static int irq = 0;
1238 static int io = 0;
1239 MODULE_PARM(irq, "i");
1240 MODULE_PARM(io, "i");
1242 int init_module(void)
1244 struct device *dev = &dev_elmc;
1246 dev->base_addr = io;
1247 dev->irq = irq;
1248 if (register_netdev(dev) != 0) {
1249 return -EIO;
1251 return 0;
1254 void cleanup_module(void)
1256 struct device *dev = &dev_elmc;
1258 /* shutdown interrupts on the card */
1259 elmc_id_reset586();
1260 if (dev->irq != 0) {
1261 /* this should be done by close, but if we failed to
1262 initialize properly something may have gotten hosed. */
1263 free_irq(dev->irq, dev);
1264 dev->irq = 0;
1266 if (dev->base_addr != 0) {
1267 release_region(dev->base_addr, ELMC_IO_EXTENT);
1268 dev->base_addr = 0;
1270 irq = 0;
1271 io = 0;
1272 unregister_netdev(dev);
1274 mca_set_adapter_procfn(((struct priv *) (dev->priv))->slot,
1275 NULL, NULL);
1277 kfree_s(dev->priv, sizeof(struct priv));
1278 dev->priv = NULL;
1281 #endif /* MODULE */