* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / net / 3c527.c
blobddb6d8bf8c02f7d098faec41df958579390d3a91
2 /* 3c527.c: 3Com Etherlink/MC32 driver for Linux
4 * (c) Copyright 1998 Red Hat Software Inc
5 * Written by Alan Cox.
7 * Based on skeleton.c written 1993-94 by Donald Becker and ne2.c
8 * (for the MCA stuff) written by Wim Dumon.
10 * Thanks to 3Com for making this possible by providing me with the
11 * documentation.
13 * This software may be used and distributed according to the terms
14 * of the GNU Public License, incorporated herein by reference.
18 static const char *version =
19 "3c527.c:v0.05 1999/09/06 Alan Cox (alan@redhat.com)\n";
22 * Things you need
23 * o The databook.
25 * Traps for the unwary
27 * The diagram (Figure 1-1) and the POS summary disagree with the
28 * "Interrupt Level" section in the manual.
30 * The documentation in places seems to miss things. In actual fact
31 * I've always eventually found everything is documented, it just
32 * requires careful study.
35 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/mca.h>
44 #include <linux/ioport.h>
45 #include <linux/in.h>
46 #include <linux/malloc.h>
47 #include <linux/string.h>
48 #include <asm/system.h>
49 #include <asm/bitops.h>
50 #include <asm/io.h>
51 #include <asm/dma.h>
52 #include <linux/errno.h>
53 #include <linux/init.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/skbuff.h>
59 #include "3c527.h"
62 * The name of the card. Is used for messages and in the requests for
63 * io regions, irqs and dma channels
65 static const char* cardname = "3c527";
67 /* use 0 for production, 1 for verification, >2 for debug */
68 #ifndef NET_DEBUG
69 #define NET_DEBUG 2
70 #endif
71 static unsigned int mc32_debug = NET_DEBUG;
73 /* The number of low I/O ports used by the ethercard. */
74 #define NETCARD_IO_EXTENT 8
77 struct mc32_mailbox
79 u16 mbox __attribute((packed));
80 u16 data[1] __attribute((packed));
83 /* Information that need to be kept for each board. */
85 #define TX_RING_MAX 16 /* Typically the card supports 37 */
86 #define RX_RING_MAX 32 /* " " " */
88 struct mc32_local
90 struct net_device_stats net_stats;
91 int slot;
92 volatile struct mc32_mailbox *rx_box;
93 volatile struct mc32_mailbox *tx_box;
94 volatile struct mc32_mailbox *exec_box;
95 volatile u16 *stats;
96 u16 tx_chain;
97 u16 rx_chain;
98 u16 tx_len;
99 u16 rx_len;
100 u32 base;
101 u16 rx_halted;
102 u16 tx_halted;
103 u16 exec_pending;
104 u16 mc_reload_wait; /* a multicast load request is pending */
105 atomic_t tx_count; /* buffers left */
106 wait_queue_head_t event;
107 struct sk_buff *tx_skb[TX_RING_MAX]; /* Transmit ring */
108 u16 tx_skb_top;
109 u16 tx_skb_end;
110 struct sk_buff *rx_skb[RX_RING_MAX]; /* Receive ring */
111 void *rx_ptr[RX_RING_MAX]; /* Data pointers */
112 u32 mc_list_valid; /* True when the mclist is set */
115 /* The station (ethernet) address prefix, used for a sanity check. */
116 #define SA_ADDR0 0x02
117 #define SA_ADDR1 0x60
118 #define SA_ADDR2 0xAC
120 struct mca_adapters_t {
121 unsigned int id;
122 char *name;
125 const struct mca_adapters_t mc32_adapters[] = {
126 { 0x0041, "3COM EtherLink MC/32" },
127 { 0x8EF5, "IBM High Performance Lan Adapter" },
128 { 0x0000, NULL }
132 /* Index to functions, as function prototypes. */
134 extern int mc32_probe(struct net_device *dev);
136 static int mc32_probe1(struct net_device *dev, int ioaddr);
137 static int mc32_open(struct net_device *dev);
138 static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev);
139 static void mc32_interrupt(int irq, void *dev_id, struct pt_regs *regs);
140 static int mc32_close(struct net_device *dev);
141 static struct net_device_stats *mc32_get_stats(struct net_device *dev);
142 static void mc32_set_multicast_list(struct net_device *dev);
143 static void mc32_reset_multicast_list(struct net_device *dev);
147 * Check for a network adaptor of this type, and return '0' iff one exists.
148 * If dev->base_addr == 0, probe all likely locations.
149 * If dev->base_addr == 1, always return failure.
150 * If dev->base_addr == 2, allocate space for the device and return success
151 * (detachable devices only).
154 int __init mc32_probe(struct net_device *dev)
156 static int current_mca_slot = -1;
157 int i;
158 int adapter_found = 0;
160 /* Do not check any supplied i/o locations.
161 POS registers usually don't fail :) */
163 /* MCA cards have POS registers.
164 Autodetecting MCA cards is extremely simple.
165 Just search for the card. */
167 for(i = 0; (mc32_adapters[i].name != NULL) && !adapter_found; i++) {
168 current_mca_slot =
169 mca_find_unused_adapter(mc32_adapters[i].id, 0);
171 if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
172 if(!mc32_probe1(dev, current_mca_slot))
174 mca_set_adapter_name(current_mca_slot,
175 mc32_adapters[i].name);
176 mca_mark_as_used(current_mca_slot);
177 return 0;
182 return -ENODEV;
186 * This is the real probe routine. Linux has a history of friendly device
187 * probes on the ISA bus. A good device probes avoids doing writes, and
188 * verifies that the correct device exists and functions.
190 static int __init mc32_probe1(struct net_device *dev, int slot)
192 static unsigned version_printed = 0;
193 int i;
194 u8 POS;
195 u32 base;
196 struct mc32_local *lp;
197 static u16 mca_io_bases[]={
198 0x7280,0x7290,
199 0x7680,0x7690,
200 0x7A80,0x7A90,
201 0x7E80,0x7E90
203 static u32 mca_mem_bases[]={
204 0x00C0000,
205 0x00C4000,
206 0x00C8000,
207 0x00CC000,
208 0x00D0000,
209 0x00D4000,
210 0x00D8000,
211 0x00DC000
213 static char *failures[]={
214 "Processor instruction",
215 "Processor data bus",
216 "Processor data bus",
217 "Processor data bus",
218 "Adapter bus",
219 "ROM checksum",
220 "Base RAM",
221 "Extended RAM",
222 "82586 internal loopback",
223 "82586 initialisation failure",
224 "Adapter list configuration error"
227 /* Time to play MCA games */
229 if (mc32_debug && version_printed++ == 0)
230 printk(KERN_DEBUG "%s", version);
232 printk(KERN_INFO "%s: %s found in slot %d:", dev->name, cardname, slot);
234 POS = mca_read_stored_pos(slot, 2);
236 if(!(POS&1))
238 printk(" disabled.\n");
239 return -ENODEV;
242 /* Allocate a new 'dev' if needed. */
243 if (dev == NULL) {
245 * Don't allocate the private data here, it is done later
246 * This makes it easier to free the memory when this driver
247 * is used as a module.
249 dev = init_etherdev(0, 0);
250 if (dev == NULL)
251 return -ENOMEM;
254 /* Fill in the 'dev' fields. */
255 dev->base_addr = mca_io_bases[(POS>>1)&7];
256 dev->mem_start = mca_mem_bases[(POS>>4)&7];
258 POS = mca_read_stored_pos(slot, 4);
259 if(!(POS&1))
261 printk("memory window disabled.\n");
262 return -ENODEV;
265 POS = mca_read_stored_pos(slot, 5);
267 i=(POS>>4)&3;
268 if(i==3)
270 printk("invalid memory window.\n");
271 return -ENODEV;
274 i*=16384;
275 i+=16384;
277 dev->mem_end=dev->mem_start + i;
279 dev->irq = ((POS>>2)&3)+9;
281 printk("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
282 dev->base_addr, dev->irq, dev->mem_start, i/1024);
285 /* We ought to set the cache line size here.. */
289 * Go PROM browsing
292 printk("%s: Address ", dev->name);
294 /* Retrieve and print the ethernet address. */
295 for (i = 0; i < 6; i++)
297 mca_write_pos(slot, 6, i+12);
298 mca_write_pos(slot, 7, 0);
300 printk(" %2.2x", dev->dev_addr[i] = mca_read_pos(slot,3));
303 mca_write_pos(slot, 6, 0);
304 mca_write_pos(slot, 7, 0);
306 POS = mca_read_stored_pos(slot, 4);
308 if(POS&2)
309 printk(" : BNC port selected.\n");
310 else
311 printk(" : AUI port selected.\n");
313 POS=inb(dev->base_addr+HOST_CTRL);
314 POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
315 POS&=~HOST_CTRL_INTE;
316 outb(POS, dev->base_addr+HOST_CTRL);
317 /* Reset adapter */
318 udelay(100);
319 /* Reset off */
320 POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
321 outb(POS, dev->base_addr+HOST_CTRL);
323 udelay(300);
326 * Grab the IRQ
329 if(request_irq(dev->irq, &mc32_interrupt, 0, cardname, dev))
331 printk("%s: unable to get IRQ %d.\n",
332 dev->name, dev->irq);
333 return -EAGAIN;
336 /* Initialize the device structure. */
337 if (dev->priv == NULL) {
338 dev->priv = kmalloc(sizeof(struct mc32_local), GFP_KERNEL);
339 if (dev->priv == NULL)
341 free_irq(dev->irq, dev);
342 return -ENOMEM;
346 memset(dev->priv, 0, sizeof(struct mc32_local));
347 lp = (struct mc32_local *)dev->priv;
348 lp->slot = slot;
350 i=0;
352 base = inb(dev->base_addr);
354 while(base==0xFF)
356 i++;
357 if(i==1000)
359 printk("%s: failed to boot adapter.\n", dev->name);
360 free_irq(dev->irq, dev);
361 return -ENODEV;
363 udelay(1000);
364 if(inb(dev->base_addr+2)&(1<<5))
365 base = inb(dev->base_addr);
368 if(base>0)
370 if(base < 0x0C)
371 printk("%s: %s%s.\n", dev->name, failures[base-1],
372 base<0x0A?" test failure":"");
373 else
374 printk("%s: unknown failure %d.\n", dev->name, base);
375 free_irq(dev->irq, dev);
376 return -ENODEV;
379 base=0;
380 for(i=0;i<4;i++)
382 int n=0;
384 while(!(inb(dev->base_addr+2)&(1<<5)))
386 n++;
387 udelay(50);
388 if(n>100)
390 printk(KERN_ERR "%s: mailbox read fail (%d).\n", dev->name, i);
391 free_irq(dev->irq, dev);
392 return -ENODEV;
396 base|=(inb(dev->base_addr)<<(8*i));
399 lp->exec_box=bus_to_virt(dev->mem_start+base);
401 base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];
403 lp->base = dev->mem_start+base;
405 lp->rx_box=bus_to_virt(lp->base + lp->exec_box->data[2]);
406 lp->tx_box=bus_to_virt(lp->base + lp->exec_box->data[3]);
408 lp->stats = bus_to_virt(lp->base + lp->exec_box->data[5]);
411 * Descriptor chains (card relative)
414 lp->tx_chain = lp->exec_box->data[8];
415 lp->rx_chain = lp->exec_box->data[10];
416 lp->tx_len = lp->exec_box->data[9];
417 lp->rx_len = lp->exec_box->data[11];
418 init_waitqueue_head(&lp->event);
420 printk("%s: %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
421 dev->name, lp->rx_len, lp->tx_len, lp->base);
423 dev->open = mc32_open;
424 dev->stop = mc32_close;
425 dev->hard_start_xmit = mc32_send_packet;
426 dev->get_stats = mc32_get_stats;
427 dev->set_multicast_list = mc32_set_multicast_list;
429 lp->rx_halted = 1;
430 lp->tx_halted = 1;
432 /* Fill in the fields of the device structure with ethernet values. */
433 ether_setup(dev);
434 return 0;
439 * Polled command stuff
442 static void mc32_ring_poll(struct net_device *dev)
444 int ioaddr = dev->base_addr;
445 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
450 * Send exec commands
455 * Send command from interrupt state
458 static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
460 struct mc32_local *lp = (struct mc32_local *)dev->priv;
461 int ioaddr = dev->base_addr;
463 if(lp->exec_pending)
464 return -1;
466 lp->exec_pending=1;
467 lp->exec_box->mbox=0;
468 lp->exec_box->mbox=cmd;
469 memcpy((void *)lp->exec_box->data, data, len);
470 barrier(); /* the memcpy forgot the volatile so be sure */
472 /* Send the command */
473 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
474 outb(1<<6, ioaddr+HOST_CMD);
475 return 0;
480 * Send command and block for results. On completion spot and reissue
481 * multicasts
484 static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
486 struct mc32_local *lp = (struct mc32_local *)dev->priv;
487 int ioaddr = dev->base_addr;
488 unsigned long flags;
489 int ret = 0;
492 * Wait for a command
495 while(lp->exec_pending)
496 sleep_on(&lp->event);
499 * Issue mine
502 lp->exec_pending=1;
503 lp->exec_box->mbox=0;
504 lp->exec_box->mbox=cmd;
505 memcpy((void *)lp->exec_box->data, data, len);
506 barrier(); /* the memcpy forgot the volatile so be sure */
508 /* Send the command */
509 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
510 outb(1<<6, ioaddr+HOST_CMD);
512 save_flags(flags);
513 cli();
514 while(lp->exec_pending!=2)
515 sleep_on(&lp->event);
516 lp->exec_pending=0;
517 restore_flags(flags);
520 if(lp->exec_box->data[0]&(1<<13))
521 ret = -1;
523 * A multicast set got blocked - do it now
526 if(lp->mc_reload_wait)
527 mc32_reset_multicast_list(dev);
529 return ret;
534 * RX abort
537 static void mc32_rx_abort(struct net_device *dev)
539 struct mc32_local *lp = (struct mc32_local *)dev->priv;
540 int ioaddr = dev->base_addr;
542 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
544 lp->rx_box->mbox=0;
545 outb(3<<3, ioaddr+HOST_CMD); /* Suspend reception */
550 * RX enable
553 static void mc32_rx_begin(struct net_device *dev)
555 struct mc32_local *lp = (struct mc32_local *)dev->priv;
556 int ioaddr = dev->base_addr;
558 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
560 lp->rx_box->mbox=0;
561 outb(1<<3, ioaddr+HOST_CMD); /* GO */
562 mc32_ring_poll(dev);
564 lp->rx_halted=0;
567 static void mc32_tx_abort(struct net_device *dev)
569 struct mc32_local *lp = (struct mc32_local *)dev->priv;
570 int ioaddr = dev->base_addr;
572 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
574 lp->tx_box->mbox=0;
575 outb(3, ioaddr+HOST_CMD); /* Suspend */
577 /* Ring empty */
579 atomic_set(&lp->tx_count, lp->tx_len);
581 /* Flush */
582 if(lp->tx_skb_top!=lp->tx_skb_end)
584 int i;
585 if(lp->tx_skb_top<=lp->tx_skb_end)
587 for(i=lp->tx_skb_top;i<lp->tx_skb_end;i++)
589 dev_kfree_skb(lp->tx_skb[i]);
590 lp->tx_skb[i]=NULL;
593 else
595 for(i=lp->tx_skb_end;i<TX_RING_MAX;i++)
597 dev_kfree_skb(lp->tx_skb[i]);
598 lp->tx_skb[i]=NULL;
600 for(i=0;i<lp->tx_skb_top;i++)
602 dev_kfree_skb(lp->tx_skb[i]);
603 lp->tx_skb[i]=NULL;
607 lp->tx_skb_top=lp->tx_skb_end=0;
611 * TX enable
614 static void mc32_tx_begin(struct net_device *dev)
616 struct mc32_local *lp = (struct mc32_local *)dev->priv;
617 int ioaddr = dev->base_addr;
619 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
621 lp->tx_box->mbox=0;
622 #if 0
623 outb(5, ioaddr+HOST_CMD); /* GO */
624 printk("TX=>5\n");
625 mc32_ring_poll(dev);
626 if(lp->tx_box->mbox&(1<<13))
627 printk("TX begin error!\n");
628 #endif
629 lp->tx_halted=0;
634 * Load the rx ring
637 static int mc32_load_rx_ring(struct net_device *dev)
639 struct mc32_local *lp = (struct mc32_local *)dev->priv;
640 int i;
641 u16 base;
642 volatile struct skb_header *p;
644 base = lp->rx_box->data[0];
646 /* Fix me - should use card size - also fix flush ! */
648 for(i=0;i<RX_RING_MAX;i++)
650 lp->rx_skb[i]=alloc_skb(1532, GFP_KERNEL);
651 if(lp->rx_skb[i]==NULL)
653 for(;i>=0;i--)
654 kfree_skb(lp->rx_skb[i]);
655 return -ENOBUFS;
657 lp->rx_ptr[i]=lp->rx_skb[i]->data+18;
659 p=bus_to_virt(lp->base+base);
660 p->control=0;
661 p->data = virt_to_bus(lp->rx_ptr[i]);
662 p->status=0;
663 p->length = 1532;
664 base = p->next;
666 p->control = (1<<6);
667 lp->rx_box->mbox = 0;
668 return 0;
671 static void mc32_flush_rx_ring(struct mc32_local *lp)
673 int i;
674 for(i=0;i<RX_RING_MAX;i++)
675 kfree_skb(lp->rx_skb[i]);
678 static void mc32_flush_tx_ring(struct mc32_local *lp)
680 int i;
682 if(lp->tx_skb_top <= lp->tx_skb_end)
684 for(i=lp->tx_skb_top;i<lp->tx_skb_end;i++)
685 dev_kfree_skb(lp->tx_skb[i]);
687 else
689 for(i=0;i<lp->tx_skb_end;i++)
690 dev_kfree_skb(lp->tx_skb[i]);
691 for(i=lp->tx_skb_top;i<TX_RING_MAX;i++)
692 dev_kfree_skb(lp->tx_skb[i]);
697 * Open/initialize the board. This is called (in the current kernel)
698 * sometime after booting when the 'ifconfig' program is run.
701 static int mc32_open(struct net_device *dev)
703 int ioaddr = dev->base_addr;
704 u16 zero_word=0;
705 u8 one=1;
706 u8 regs;
708 dev->tbusy = 0;
709 dev->interrupt = 0;
710 dev->start = 1;
713 * Interrupts enabled
716 regs=inb(ioaddr+HOST_CTRL);
717 regs|=HOST_CTRL_INTE;
718 outb(regs, ioaddr+HOST_CTRL);
722 * Send the indications on command
725 mc32_command(dev, 4, &one, 2);
729 * Send the command sequence "abort, resume" for RX and TX.
730 * The abort cleans up the buffer chains if needed.
733 mc32_rx_abort(dev);
734 mc32_tx_abort(dev);
736 /* Set Network Address */
737 mc32_command(dev, 1, dev->dev_addr, 6);
739 /* Set the filters */
740 mc32_set_multicast_list(dev);
742 /* Issue the 82586 workaround command - this is for "busy lans",
743 but basically means for all lans now days - has a performance
744 cost but best set */
746 mc32_command(dev, 0x0D, &zero_word, 2); /* 82586 bug workaround on */
748 /* Load the ring we just initialised */
750 if(mc32_load_rx_ring(dev))
752 mc32_close(dev);
753 return -ENOBUFS;
756 /* And the resume command goes last */
758 mc32_rx_begin(dev);
759 mc32_tx_begin(dev);
761 MOD_INC_USE_COUNT;
763 return 0;
766 static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev)
768 struct mc32_local *lp = (struct mc32_local *)dev->priv;
770 if (dev->tbusy) {
772 * If we get here, some higher level has decided we are broken.
773 * There should really be a "kick me" function call instead.
775 int tickssofar = jiffies - dev->trans_start;
776 if (tickssofar < 5)
777 return 1;
778 printk(KERN_WARNING "%s: transmit timed out?\n", dev->name);
779 /* Try to restart the adaptor. */
780 dev->tbusy=0;
781 dev->trans_start = jiffies;
785 * Block a timer-based transmit from overlapping. This could better be
786 * done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
788 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0)
790 printk(KERN_WARNING "%s: Transmitter access conflict.\n", dev->name);
791 dev_kfree_skb(skb);
793 else
795 unsigned long flags;
797 u16 tx_head;
798 volatile struct skb_header *p, *np;
800 save_flags(flags);
801 cli();
803 if(atomic_read(&lp->tx_count)==0)
805 dev->tbusy=1;
806 restore_flags(flags);
807 return 1;
810 tx_head = lp->tx_box->data[0];
811 atomic_dec(&lp->tx_count);
813 /* We will need this to flush the buffer out */
815 lp->tx_skb[lp->tx_skb_end] = skb;
816 lp->tx_skb_end++;
817 lp->tx_skb_end&=(TX_RING_MAX-1);
819 /* P is the last sending/sent buffer as a pointer */
820 p=(struct skb_header *)bus_to_virt(lp->base+tx_head);
822 /* NP is the buffer we will be loading */
823 np=(struct skb_header *)bus_to_virt(lp->base+p->next);
825 np->control |= (1<<6); /* EOL */
826 wmb();
828 np->length = skb->len;
829 np->data = virt_to_bus(skb->data);
830 np->status = 0;
831 np->control = (1<<7)|(1<<6); /* EOP EOL */
832 wmb();
834 p->status = 0;
835 p->control &= ~(1<<6);
837 dev->tbusy = 0; /* Keep feeding me */
839 lp->tx_box->mbox=0;
840 restore_flags(flags);
842 return 0;
845 static void mc32_update_stats(struct net_device *dev)
850 static void mc32_rx_ring(struct net_device *dev)
852 struct mc32_local *lp=dev->priv;
853 int ioaddr = dev->base_addr;
854 int x=0;
855 volatile struct skb_header *p;
856 u16 base;
857 u16 top;
859 top = base = lp->rx_box->data[0];
862 p=(struct skb_header *)bus_to_virt(base+lp->base);
863 if(!(p->status & (1<<7)))
864 break;
865 if(p->status & (1<<6))
867 u16 length = p->length;
868 struct sk_buff *skb=dev_alloc_skb(length+2);
869 if(skb!=NULL)
871 skb_reserve(skb,2);
872 /*printk("Frame at %p\n", bus_to_virt(p->data)); */
873 memcpy(skb_put(skb, length),
874 bus_to_virt(p->data), length);
875 skb->protocol=eth_type_trans(skb,dev);
876 skb->dev=dev;
877 lp->net_stats.rx_packets++;
878 lp->net_stats.rx_bytes+=skb->len;
879 netif_rx(skb);
881 else
882 lp->net_stats.rx_dropped++;
884 else
886 lp->net_stats.rx_errors++;
887 switch(p->status&0x0F)
889 case 1:
890 lp->net_stats.rx_crc_errors++;break;
891 case 2:
892 lp->net_stats.rx_fifo_errors++;break;
893 case 3:
894 lp->net_stats.rx_frame_errors++;break;
895 case 4:
896 lp->net_stats.rx_missed_errors++;break;
897 case 5:
898 lp->net_stats.rx_length_errors++;break;
901 p->length = 1532;
902 p->control &= ~(1<<6);
903 p->status = 0;
904 base = p->next;
906 while(x++<48);
909 * This is curious. It seems the receive stop and receive continue
910 * commands race against each other, even though we poll for
911 * command ready to be issued. The delay is hackish but is a workaround
912 * while I investigate in depth
915 while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
916 lp->rx_box->mbox=0;
917 lp->rx_box->data[0] = top;
918 outb(1<<3, ioaddr+HOST_CMD);
923 * The typical workload of the driver:
924 * Handle the network interface interrupts.
926 static void mc32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
928 struct net_device *dev = dev_id;
929 struct mc32_local *lp;
930 int ioaddr, status, boguscount = 0;
931 int rx_event = 0;
933 if (dev == NULL) {
934 printk(KERN_WARNING "%s: irq %d for unknown device.\n", cardname, irq);
935 return;
937 dev->interrupt = 1;
939 ioaddr = dev->base_addr;
940 lp = (struct mc32_local *)dev->priv;
942 /* See whats cooking */
944 while((inb(ioaddr+2)&(1<<5)) && boguscount++<2000)
946 status=inb(ioaddr+HOST_CMD);
948 #ifdef DEBUG_IRQ
949 printk("Status TX%d RX%d EX%d OV%d\n",
950 (status&7), (status>>3)&7, (status>>6)&1,
951 (status>>7)&1);
952 #endif
954 switch(status&7)
956 case 0:
957 break;
958 case 6: /* TX fail */
959 lp->net_stats.tx_errors++;
960 case 2: /* TX ok */
961 lp->net_stats.tx_packets++;
962 /* Packets are sent in order - this is
963 basically a FIFO queue of buffers matching
964 the card ring */
965 lp->net_stats.tx_bytes+=lp->tx_skb[lp->tx_skb_top]->len;
966 dev_kfree_skb(lp->tx_skb[lp->tx_skb_top]);
967 lp->tx_skb[lp->tx_skb_top]=NULL;
968 lp->tx_skb_top++;
969 lp->tx_skb_top&=(TX_RING_MAX-1);
970 atomic_inc(&lp->tx_count);
971 dev->tbusy=0;
972 mark_bh(NET_BH);
973 break;
974 case 3: /* Halt */
975 case 4: /* Abort */
976 lp->tx_halted=1;
977 wake_up(&lp->event);
978 break;
979 case 5:
980 lp->tx_halted=0;
981 wake_up(&lp->event);
982 break;
983 default:
984 printk("%s: strange tx ack %d\n",
985 dev->name, status&7);
987 status>>=3;
988 switch(status&7)
990 case 0:
991 break;
992 case 2: /* RX */
993 rx_event=1;
994 break;
995 case 3:
996 case 4:
997 lp->rx_halted=1;
998 wake_up(&lp->event);
999 break;
1000 case 5:
1001 lp->rx_halted=0;
1002 wake_up(&lp->event);
1003 break;
1004 case 6:
1005 /* Out of RX buffers stat */
1006 /* Must restart */
1007 lp->net_stats.rx_dropped++;
1008 rx_event = 1; /* To restart */
1009 break;
1010 default:
1011 printk("%s: strange rx ack %d\n",
1012 dev->name, status&7);
1015 status>>=3;
1016 if(status&1)
1018 /* 0=no 1=yes 2=reply clearing */
1019 lp->exec_pending=2;
1020 wake_up(&lp->event);
1022 if(status&2)
1025 * Update the stats as soon as
1026 * we have it flagged and can
1027 * send an immediate reply (CRR set)
1030 if(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR)
1032 mc32_update_stats(dev);
1033 outb(0, ioaddr+HOST_CMD);
1039 * Process and restart the receive ring.
1042 if(rx_event)
1043 mc32_rx_ring(dev);
1044 dev->interrupt = 0;
1045 return;
1049 /* The inverse routine to mc32_open(). */
1051 static int mc32_close(struct net_device *dev)
1053 struct mc32_local *lp = (struct mc32_local *)dev->priv;
1054 int ioaddr = dev->base_addr;
1055 u8 regs;
1056 u16 one=1;
1059 * Send the indications on command (handy debug check)
1062 mc32_command(dev, 4, &one, 2);
1064 /* Abort RX and Abort TX */
1066 mc32_rx_abort(dev);
1067 mc32_tx_abort(dev);
1069 /* Catch any waiting commands */
1071 while(lp->exec_pending==1)
1072 sleep_on(&lp->event);
1074 /* Ok the card is now stopping */
1076 regs=inb(ioaddr+HOST_CTRL);
1077 regs&=~HOST_CTRL_INTE;
1078 outb(regs, ioaddr+HOST_CTRL);
1080 mc32_flush_rx_ring(lp);
1081 mc32_flush_tx_ring(lp);
1083 dev->tbusy = 1;
1084 dev->start = 0;
1086 /* Update the statistics here. */
1088 MOD_DEC_USE_COUNT;
1090 return 0;
1095 * Get the current statistics.
1096 * This may be called with the card open or closed.
1099 static struct net_device_stats *mc32_get_stats(struct net_device *dev)
1101 struct mc32_local *lp = (struct mc32_local *)dev->priv;
1102 return &lp->net_stats;
1106 * Set or clear the multicast filter for this adaptor.
1107 * num_addrs == -1 Promiscuous mode, receive all packets
1108 * num_addrs == 0 Normal mode, clear multicast list
1109 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1110 * and do best-effort filtering.
1112 static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
1114 struct mc32_local *lp = (struct mc32_local *)dev->priv;
1115 u16 filt;
1117 if (dev->flags&IFF_PROMISC)
1118 /* Enable promiscuous mode */
1119 filt = 1;
1120 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > 10)
1122 dev->flags|=IFF_PROMISC;
1123 filt = 1;
1125 else if(dev->mc_count)
1127 unsigned char block[62];
1128 unsigned char *bp;
1129 struct dev_mc_list *dmc=dev->mc_list;
1131 int i;
1133 filt = 0;
1135 if(retry==0)
1136 lp->mc_list_valid = 0;
1137 if(!lp->mc_list_valid)
1139 block[1]=0;
1140 block[0]=dev->mc_count;
1141 bp=block+2;
1143 for(i=0;i<dev->mc_count;i++)
1145 memcpy(bp, dmc->dmi_addr, 6);
1146 bp+=6;
1147 dmc=dmc->next;
1149 if(mc32_command_nowait(dev, 2, block, 2+6*dev->mc_count)==-1)
1151 lp->mc_reload_wait = 1;
1152 return;
1154 lp->mc_list_valid=1;
1157 else
1159 filt = 0;
1161 if(mc32_command_nowait(dev, 0, &filt, 2)==-1)
1163 lp->mc_reload_wait = 1;
1167 static void mc32_set_multicast_list(struct net_device *dev)
1169 do_mc32_set_multicast_list(dev,0);
1172 static void mc32_reset_multicast_list(struct net_device *dev)
1174 do_mc32_set_multicast_list(dev,1);
1177 #ifdef MODULE
1179 static char devicename[9] = { 0, };
1180 static struct net_device this_device = {
1181 devicename, /* will be inserted by linux/drivers/net/mc32_init.c */
1182 0, 0, 0, 0,
1183 0, 0, /* I/O address, IRQ */
1184 0, 0, 0, NULL, mc32_probe };
1186 int init_module(void)
1188 int result;
1190 if ((result = register_netdev(&this_device)) != 0)
1191 return result;
1193 return 0;
1196 void cleanup_module(void)
1198 int slot;
1200 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1201 unregister_netdev(&this_device);
1204 * If we don't do this, we can't re-insmod it later.
1205 * Release irq/dma here, when you have jumpered versions and
1206 * allocate them in mc32_probe1().
1209 if (this_device.priv)
1211 struct mc32_local *lp=this_device.priv;
1212 slot = lp->slot;
1213 mca_mark_as_unused(slot);
1214 mca_set_adapter_name(slot, NULL);
1215 kfree_s(this_device.priv, sizeof(struct mc32_local));
1217 free_irq(this_device.irq, &this_device);
1220 #endif /* MODULE */