File: /usr/src/linux/drivers/net/3c527.c

1     /* 3c527.c: 3Com Etherlink/MC32 driver for Linux 2.4
2      *
3      *	(c) Copyright 1998 Red Hat Software Inc
4      *	Written by Alan Cox. 
5      *	Further debugging by Carl Drougge.
6      *      Modified by Richard Procter (rnp@netlink.co.nz)
7      *
8      *	Based on skeleton.c written 1993-94 by Donald Becker and ne2.c
9      *	(for the MCA stuff) written by Wim Dumon.
10      *
11      *	Thanks to 3Com for making this possible by providing me with the
12      *	documentation.
13      *
14      *	This software may be used and distributed according to the terms
15      *	of the GNU General Public License, incorporated herein by reference.
16      *
17      */
18     
19     static const char *version =
20     	"3c527.c:v0.6 2001/03/03 Richard Proctor (rnp@netlink.co.nz)\n";
21     
22     /**
23      * DOC: Traps for the unwary
24      *
25      *	The diagram (Figure 1-1) and the POS summary disagree with the
26      *	"Interrupt Level" section in the manual.
27      *
28      *	The manual contradicts itself when describing the minimum number 
29      *	buffers in the 'configure lists' command. 
30      *	My card accepts a buffer config of 4/4. 
31      *
32      *	Setting the SAV BP bit does not save bad packets, but
33      *	only enables RX on-card stats collection. 
34      *
35      *	The documentation in places seems to miss things. In actual fact
36      *	I've always eventually found everything is documented, it just
37      *	requires careful study.
38      *
39      * DOC: Theory Of Operation
40      *
41      *	The 3com 3c527 is a 32bit MCA bus mastering adapter with a large
42      *	amount of on board intelligence that housekeeps a somewhat dumber
43      *	Intel NIC. For performance we want to keep the transmit queue deep
44      *	as the card can transmit packets while fetching others from main
45      *	memory by bus master DMA. Transmission and reception are driven by
46      *	circular buffer queues.
47      *
48      *	The mailboxes can be used for controlling how the card traverses
49      *	its buffer rings, but are used only for inital setup in this
50      *	implementation.  The exec mailbox allows a variety of commands to
51      *	be executed. Each command must complete before the next is
52      *	executed. Primarily we use the exec mailbox for controlling the
53      *	multicast lists.  We have to do a certain amount of interesting
54      *	hoop jumping as the multicast list changes can occur in interrupt
55      *	state when the card has an exec command pending. We defer such
56      *	events until the command completion interrupt.
57      *
58      *	A copy break scheme (taken from 3c59x.c) is employed whereby
59      *	received frames exceeding a configurable length are passed
60      *	directly to the higher networking layers without incuring a copy,
61      *	in what amounts to a time/space trade-off.
62      *	 
63      *	The card also keeps a large amount of statistical information
64      *	on-board. In a perfect world, these could be used safely at no
65      *	cost. However, lacking information to the contrary, processing
66      *	them without races would involve so much extra complexity as to
67      *	make it unworthwhile to do so. In the end, a hybrid SW/HW
68      *	implementation was made necessary --- see mc32_update_stats().  
69      *
70      * DOC: Notes
71      *	
72      *	It should be possible to use two or more cards, but at this stage
73      *	only by loading two copies of the same module.
74      *
75      *	The on-board 82586 NIC has trouble receiving multiple
76      *	back-to-back frames and so is likely to drop packets from fast
77      *	senders.
78     **/
79     
80     #include <linux/module.h>
81     
82     #include <linux/kernel.h>
83     #include <linux/sched.h>
84     #include <linux/types.h>
85     #include <linux/fcntl.h>
86     #include <linux/interrupt.h>
87     #include <linux/ptrace.h>
88     #include <linux/mca.h>
89     #include <linux/ioport.h>
90     #include <linux/in.h>
91     #include <linux/slab.h>
92     #include <linux/string.h>
93     #include <asm/system.h>
94     #include <asm/bitops.h>
95     #include <asm/io.h>
96     #include <asm/dma.h>
97     #include <linux/errno.h>
98     #include <linux/init.h>
99     
100     #include <linux/netdevice.h>
101     #include <linux/etherdevice.h>
102     #include <linux/skbuff.h>
103     #include <linux/if_ether.h>
104     
105     #include "3c527.h"
106     
107     /*
108      * The name of the card. Is used for messages and in the requests for
109      * io regions, irqs and dma channels
110      */
111     static const char* cardname = "3c527";
112     
113     /* use 0 for production, 1 for verification, >2 for debug */
114     #ifndef NET_DEBUG
115     #define NET_DEBUG 2
116     #endif
117     
118     #undef DEBUG_IRQ
119     
120     static unsigned int mc32_debug = NET_DEBUG;
121     
122     /* The number of low I/O ports used by the ethercard. */
123     #define MC32_IO_EXTENT	8
124     
125     /* As implemented, values must be a power-of-2 -- 4/8/16/32 */ 
126     #define TX_RING_LEN     32       /* Typically the card supports 37  */
127     #define RX_RING_LEN     8        /*     "       "        "          */
128     
129     /* Copy break point, see above for details. 
130      * Setting to > 1512 effectively disables this feature.	*/	    
131     #define RX_COPYBREAK    200      /* Value from 3c59x.c */
132     
133     /* Issue the 82586 workaround command - this is for "busy lans", but
134      * basically means for all lans now days - has a performance (latency) 
135      * cost, but best set. */ 
136     static const int WORKAROUND_82586=1;
137     
138     /* Pointers to buffers and their on-card records */
139     
140     struct mc32_ring_desc 
141     {
142     	volatile struct skb_header *p;                    
143     	struct sk_buff *skb;          
144     };
145     
146     
147     /* Information that needs to be kept for each board. */
148     struct mc32_local 
149     {
150     	struct net_device_stats net_stats;
151     	int slot;
152     	volatile struct mc32_mailbox *rx_box;
153     	volatile struct mc32_mailbox *tx_box;
154     	volatile struct mc32_mailbox *exec_box;
155             volatile struct mc32_stats *stats;    /* Start of on-card statistics */
156             u16 tx_chain;           /* Transmit list start offset */
157     	u16 rx_chain;           /* Receive list start offset */
158             u16 tx_len;             /* Transmit list count */ 
159             u16 rx_len;             /* Receive list count */
160     
161     	u32 base;
162     	u16 exec_pending;
163     	u16 mc_reload_wait;	/* a multicast load request is pending */
164     	u32 mc_list_valid;	/* True when the mclist is set */
165     	u16 xceiver_state;      /* Current transceiver state. bitmapped */ 
166     	u16 desired_state;      /* The state we want the transceiver to be in */ 
167     	atomic_t tx_count;	/* buffers left */
168     	wait_queue_head_t event;
169     
170     	struct mc32_ring_desc tx_ring[TX_RING_LEN];	/* Host Transmit ring */
171     	struct mc32_ring_desc rx_ring[RX_RING_LEN];	/* Host Receive ring */
172     
173     	u16 tx_ring_tail;       /* index to tx de-queue end */
174     	u16 tx_ring_head;       /* index to tx en-queue end */
175     
176     	u16 rx_ring_tail;       /* index to rx de-queue end */ 
177     };
178     
179     /* The station (ethernet) address prefix, used for a sanity check. */
180     #define SA_ADDR0 0x02
181     #define SA_ADDR1 0x60
182     #define SA_ADDR2 0xAC
183     
184     struct mca_adapters_t {
185     	unsigned int	id;
186     	char		*name;
187     };
188     
189     const struct mca_adapters_t mc32_adapters[] = {
190     	{ 0x0041, "3COM EtherLink MC/32" },
191     	{ 0x8EF5, "IBM High Performance Lan Adapter" },
192     	{ 0x0000, NULL }
193     };
194     
195     
196     /* Macros for ring index manipulations */ 
197     static inline u16 next_rx(u16 rx) { return (rx+1)&(RX_RING_LEN-1); };
198     static inline u16 prev_rx(u16 rx) { return (rx-1)&(RX_RING_LEN-1); };
199     
200     static inline u16 next_tx(u16 tx) { return (tx+1)&(TX_RING_LEN-1); };
201     
202     
203     /* Index to functions, as function prototypes. */
204     extern int mc32_probe(struct net_device *dev);
205     
206     static int	mc32_probe1(struct net_device *dev, int ioaddr);
207     static int      mc32_command(struct net_device *dev, u16 cmd, void *data, int len);
208     static int	mc32_open(struct net_device *dev);
209     static void	mc32_timeout(struct net_device *dev);
210     static int	mc32_send_packet(struct sk_buff *skb, struct net_device *dev);
211     static void	mc32_interrupt(int irq, void *dev_id, struct pt_regs *regs);
212     static int	mc32_close(struct net_device *dev);
213     static struct	net_device_stats *mc32_get_stats(struct net_device *dev);
214     static void	mc32_set_multicast_list(struct net_device *dev);
215     static void	mc32_reset_multicast_list(struct net_device *dev);
216     
217     /**
218      * mc32_probe 	-	Search for supported boards
219      * @dev: device to probe
220      *
221      * Because MCA bus is a real bus and we can scan for cards we could do a
222      * single scan for all boards here. Right now we use the passed in device
223      * structure and scan for only one board. This needs fixing for modules
224      * in paticular.
225      */
226     
227     int __init mc32_probe(struct net_device *dev)
228     {
229     	static int current_mca_slot = -1;
230     	int i;
231     	int adapter_found = 0;
232     
233     	SET_MODULE_OWNER(dev);
234     
235     	/* Do not check any supplied i/o locations. 
236     	   POS registers usually don't fail :) */
237     
238     	/* MCA cards have POS registers.  
239     	   Autodetecting MCA cards is extremely simple. 
240     	   Just search for the card. */
241     
242     	for(i = 0; (mc32_adapters[i].name != NULL) && !adapter_found; i++) {
243     		current_mca_slot = 
244     			mca_find_unused_adapter(mc32_adapters[i].id, 0);
245     
246     		if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
247     			if(!mc32_probe1(dev, current_mca_slot))
248     			{
249     				mca_set_adapter_name(current_mca_slot, 
250     						mc32_adapters[i].name);
251     				mca_mark_as_used(current_mca_slot);
252     				return 0;
253     			}
254     			
255     		}
256     	}
257     	return -ENODEV;
258     }
259     
260     /**
261      * mc32_probe1	-	Check a given slot for a board and test the card
262      * @dev:  Device structure to fill in
263      * @slot: The MCA bus slot being used by this card
264      *
265      * Decode the slot data and configure the card structures. Having done this we
266      * can reset the card and configure it. The card does a full self test cycle
267      * in firmware so we have to wait for it to return and post us either a 
268      * failure case or some addresses we use to find the board internals.
269      */
270     
271     static int __init mc32_probe1(struct net_device *dev, int slot)
272     {
273     	static unsigned version_printed;
274     	int i, err;
275     	u8 POS;
276     	u32 base;
277     	struct mc32_local *lp;
278     	static u16 mca_io_bases[]={
279     		0x7280,0x7290,
280     		0x7680,0x7690,
281     		0x7A80,0x7A90,
282     		0x7E80,0x7E90
283     	};
284     	static u32 mca_mem_bases[]={
285     		0x00C0000,
286     		0x00C4000,
287     		0x00C8000,
288     		0x00CC000,
289     		0x00D0000,
290     		0x00D4000,
291     		0x00D8000,
292     		0x00DC000
293     	};
294     	static char *failures[]={
295     		"Processor instruction",
296     		"Processor data bus",
297     		"Processor data bus",
298     		"Processor data bus",
299     		"Adapter bus",
300     		"ROM checksum",
301     		"Base RAM",
302     		"Extended RAM",
303     		"82586 internal loopback",
304     		"82586 initialisation failure",
305     		"Adapter list configuration error"
306     	};
307     
308     	/* Time to play MCA games */
309     
310     	if (mc32_debug  &&  version_printed++ == 0)
311     		printk(KERN_DEBUG "%s", version);
312     
313     	printk(KERN_INFO "%s: %s found in slot %d:", dev->name, cardname, slot);
314     
315     	POS = mca_read_stored_pos(slot, 2);
316     	
317     	if(!(POS&1))
318     	{
319     		printk(" disabled.\n");
320     		return -ENODEV;
321     	}
322     
323     	/* Fill in the 'dev' fields. */
324     	dev->base_addr = mca_io_bases[(POS>>1)&7];
325     	dev->mem_start = mca_mem_bases[(POS>>4)&7];
326     	
327     	POS = mca_read_stored_pos(slot, 4);
328     	if(!(POS&1))
329     	{
330     		printk("memory window disabled.\n");
331     		return -ENODEV;
332     	}
333     
334     	POS = mca_read_stored_pos(slot, 5);
335     	
336     	i=(POS>>4)&3;
337     	if(i==3)
338     	{
339     		printk("invalid memory window.\n");
340     		return -ENODEV;
341     	}
342     	
343     	i*=16384;
344     	i+=16384;
345     	
346     	dev->mem_end=dev->mem_start + i;
347     	
348     	dev->irq = ((POS>>2)&3)+9;
349     	
350     	if(!request_region(dev->base_addr, MC32_IO_EXTENT, cardname))
351     	{
352     		printk("io 0x%3lX, which is busy.\n", dev->base_addr);
353     		return -EBUSY;
354     	}
355     
356     	printk("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
357     		dev->base_addr, dev->irq, dev->mem_start, i/1024);
358     	
359     	
360     	/* We ought to set the cache line size here.. */
361     	
362     	
363     	/*
364     	 *	Go PROM browsing
365     	 */
366     	 
367     	printk("%s: Address ", dev->name);
368     	 
369     	/* Retrieve and print the ethernet address. */
370     	for (i = 0; i < 6; i++)
371     	{
372     		mca_write_pos(slot, 6, i+12);
373     		mca_write_pos(slot, 7, 0);
374     	
375     		printk(" %2.2x", dev->dev_addr[i] = mca_read_pos(slot,3));
376     	}
377     
378     	mca_write_pos(slot, 6, 0);
379     	mca_write_pos(slot, 7, 0);
380     
381     	POS = mca_read_stored_pos(slot, 4);
382     	
383     	if(POS&2)
384     		printk(" : BNC port selected.\n");
385     	else 
386     		printk(" : AUI port selected.\n");
387     		
388     	POS=inb(dev->base_addr+HOST_CTRL);
389     	POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
390     	POS&=~HOST_CTRL_INTE;
391     	outb(POS, dev->base_addr+HOST_CTRL);
392     	/* Reset adapter */
393     	udelay(100);
394     	/* Reset off */
395     	POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
396     	outb(POS, dev->base_addr+HOST_CTRL);
397     	
398     	udelay(300);
399     	
400     	/*
401     	 *	Grab the IRQ
402     	 */
403     
404     	i = request_irq(dev->irq, &mc32_interrupt, SA_SHIRQ, dev->name, dev);
405     	if (i) {
406     		release_region(dev->base_addr, MC32_IO_EXTENT);
407     		printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq);
408     		return i;
409     	}
410     
411     
412     	/* Initialize the device structure. */
413     	dev->priv = kmalloc(sizeof(struct mc32_local), GFP_KERNEL);
414     	if (dev->priv == NULL)
415     	{
416     		err = -ENOMEM;
417     		goto err_exit_irq; 
418     	}
419     
420     	memset(dev->priv, 0, sizeof(struct mc32_local));
421     	lp = dev->priv;
422     	lp->slot = slot;
423     
424     	i=0;
425     
426     	base = inb(dev->base_addr);
427     	
428     	while(base == 0xFF)
429     	{
430     		i++;
431     		if(i == 1000)
432     		{
433     			printk(KERN_ERR "%s: failed to boot adapter.\n", dev->name);
434     			err = -ENODEV; 
435     			goto err_exit_free;
436     		}
437     		udelay(1000);
438     		if(inb(dev->base_addr+2)&(1<<5))
439     			base = inb(dev->base_addr);
440     	}
441     
442     	if(base>0)
443     	{
444     		if(base < 0x0C)
445     			printk(KERN_ERR "%s: %s%s.\n", dev->name, failures[base-1],
446     				base<0x0A?" test failure":"");
447     		else
448     			printk(KERN_ERR "%s: unknown failure %d.\n", dev->name, base);
449     		err = -ENODEV; 
450     		goto err_exit_free;
451     	}
452     	
453     	base=0;
454     	for(i=0;i<4;i++)
455     	{
456     		int n=0;
457     	
458     		while(!(inb(dev->base_addr+2)&(1<<5)))
459     		{
460     			n++;
461     			udelay(50);
462     			if(n>100)
463     			{
464     				printk(KERN_ERR "%s: mailbox read fail (%d).\n", dev->name, i);
465     				err = -ENODEV;
466     				goto err_exit_free;
467     			}
468     		}
469     
470     		base|=(inb(dev->base_addr)<<(8*i));
471     	}
472     	
473     	lp->exec_box=bus_to_virt(dev->mem_start+base);
474     	
475     	base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];  
476     	
477     	lp->base = dev->mem_start+base;
478     	
479     	lp->rx_box=bus_to_virt(lp->base + lp->exec_box->data[2]); 
480     	lp->tx_box=bus_to_virt(lp->base + lp->exec_box->data[3]);
481     	
482     	lp->stats = bus_to_virt(lp->base + lp->exec_box->data[5]);
483     
484     	/*
485     	 *	Descriptor chains (card relative)
486     	 */
487     	 
488     	lp->tx_chain 		= lp->exec_box->data[8];   /* Transmit list start offset */
489     	lp->rx_chain 		= lp->exec_box->data[10];  /* Receive list start offset */
490     	lp->tx_len 		= lp->exec_box->data[9];   /* Transmit list count */ 
491     	lp->rx_len 		= lp->exec_box->data[11];  /* Receive list count */
492     
493     	init_waitqueue_head(&lp->event);
494     	
495     	printk("%s: Firmware Rev %d. %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
496     		dev->name, lp->exec_box->data[12], lp->rx_len, lp->tx_len, lp->base);
497     
498     	dev->open		= mc32_open;
499     	dev->stop		= mc32_close;
500     	dev->hard_start_xmit	= mc32_send_packet;
501     	dev->get_stats		= mc32_get_stats;
502     	dev->set_multicast_list = mc32_set_multicast_list;
503     	dev->tx_timeout		= mc32_timeout;
504     	dev->watchdog_timeo	= HZ*5;	/* Board does all the work */
505     
506     	
507     	lp->xceiver_state = HALTED; 
508     	
509     	lp->tx_ring_tail=lp->tx_ring_head=0;
510     
511     	/* Fill in the fields of the device structure with ethernet values. */
512     	ether_setup(dev);
513     	
514     	return 0;
515     
516     err_exit_free:
517     	kfree(dev->priv);
518     err_exit_irq:
519     	free_irq(dev->irq, dev);
520     	release_region(dev->base_addr, MC32_IO_EXTENT);
521     	return err;
522     }
523     
524     
525     /**
526      *	mc32_ready_poll		-	wait until we can feed it a command
527      *	@dev:	The device to wait for
528      *	
529      *	Wait until the card becomes ready to accept a command via the
530      *	command register. This tells us nothing about the completion
531      *	status of any pending commands and takes very little time at all.
532      */
533      
534     static void mc32_ready_poll(struct net_device *dev)
535     {
536     	int ioaddr = dev->base_addr;
537     	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
538     }
539     
540     
541     /**
542      *	mc32_command_nowait	-	send a command non blocking
543      *	@dev: The 3c527 to issue the command to
544      *	@cmd: The command word to write to the mailbox
545      *	@data: A data block if the command expects one
546      *	@len: Length of the data block
547      *
548      *	Send a command from interrupt state. If there is a command
549      *	currently being executed then we return an error of -1. It simply
550      *	isn't viable to wait around as commands may be slow. Providing we
551      *	get in, we busy wait for the board to become ready to accept the
552      *	command and issue it. We do not wait for the command to complete
553      *	--- the card will interrupt us when it's done.
554      */
555     
556     static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
557     {
558     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
559     	int ioaddr = dev->base_addr;
560     
561     	if(lp->exec_pending)
562     		return -1;
563     	
564     	lp->exec_pending=3;
565     	lp->exec_box->mbox=0;
566     	lp->exec_box->mbox=cmd;
567     	memcpy((void *)lp->exec_box->data, data, len);
568     	barrier();	/* the memcpy forgot the volatile so be sure */
569     
570     	/* Send the command */
571     	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
572     	outb(1<<6, ioaddr+HOST_CMD);	
573     	return 0;
574     }
575     
576     
577     /**
578      *	mc32_command	-	send a command and sleep until completion
579      *	@dev: The 3c527 card to issue the command to
580      *	@cmd: The command word to write to the mailbox
581      *	@data: A data block if the command expects one
582      *	@len: Length of the data block
583      *
584      *	Sends exec commands in a user context. This permits us to wait around
585      *	for the replies and also to wait for the command buffer to complete
586      *	from a previous command before we execute our command. After our 
587      *	command completes we will complete any pending multicast reload
588      *	we blocked off by hogging the exec buffer.
589      *
590      *	You feed the card a command, you wait, it interrupts you get a 
591      *	reply. All well and good. The complication arises because you use
592      *	commands for filter list changes which come in at bh level from things
593      *	like IPV6 group stuff.
594      *
595      *	We have a simple state machine
596      *
597      *	0	- nothing issued
598      *
599      *	1	- command issued, wait reply
600      *
601      *	2	- reply waiting - reader then goes to state 0
602      *
603      *	3	- command issued, trash reply. In which case the irq
604      *		  takes it back to state 0
605      *
606      */
607       
608     static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
609     {
610     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
611     	int ioaddr = dev->base_addr;
612     	unsigned long flags;
613     	int ret = 0;
614     	
615     	/*
616     	 *	Wait for a command
617     	 */
618     	 
619     	save_flags(flags);
620     	cli();
621     	 
622     	while(lp->exec_pending)
623     		sleep_on(&lp->event);
624     		
625     	/*
626     	 *	Issue mine
627     	 */
628     
629     	lp->exec_pending=1;
630     	
631     	restore_flags(flags);
632     	
633     	lp->exec_box->mbox=0;
634     	lp->exec_box->mbox=cmd;
635     	memcpy((void *)lp->exec_box->data, data, len);
636     	barrier();	/* the memcpy forgot the volatile so be sure */
637     
638     	/* Send the command */
639     	while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
640     	outb(1<<6, ioaddr+HOST_CMD);	
641     
642     	save_flags(flags);
643     	cli();
644     
645     	while(lp->exec_pending!=2)
646     		sleep_on(&lp->event);
647     	lp->exec_pending=0;
648     	restore_flags(flags);
649     	
650     	if(lp->exec_box->mbox&(1<<13))
651     		ret = -1;
652     
653     	/*
654     	 *	A multicast set got blocked - do it now
655     	 */
656     		
657     	if(lp->mc_reload_wait)
658     	{
659     		mc32_reset_multicast_list(dev);
660     	}
661     
662     	return ret;
663     }
664     
665     
666     /**
667      *	mc32_start_transceiver	-	tell board to restart tx/rx
668      *	@dev: The 3c527 card to issue the command to
669      *
670      *	This may be called from the interrupt state, where it is used
671      *	to restart the rx ring if the card runs out of rx buffers. 
672      *	
673      * 	First, we check if it's ok to start the transceiver. We then show
674      * 	the card where to start in the rx ring and issue the
675      * 	commands to start reception and transmission. We don't wait
676      * 	around for these to complete.
677      */ 
678     
679     static void mc32_start_transceiver(struct net_device *dev) {
680     
681     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
682     	int ioaddr = dev->base_addr;
683     
684     	/* Ignore RX overflow on device closure */ 
685     	if (lp->desired_state==HALTED)  
686     		return; 
687     
688     	mc32_ready_poll(dev); 
689     
690     	lp->tx_box->mbox=0;
691     	lp->rx_box->mbox=0;
692     
693     	/* Give the card the offset to the post-EOL-bit RX descriptor */ 
694     	lp->rx_box->data[0]=lp->rx_ring[prev_rx(lp->rx_ring_tail)].p->next; 
695     
696     	outb(HOST_CMD_START_RX, ioaddr+HOST_CMD);      
697     
698     	mc32_ready_poll(dev); 
699     	outb(HOST_CMD_RESTRT_TX, ioaddr+HOST_CMD);   /* card ignores this on RX restart */ 
700     	
701     	/* We are not interrupted on start completion */ 
702     	lp->xceiver_state=RUNNING; 
703     }
704     
705     
706     /**
707      *	mc32_halt_transceiver	-	tell board to stop tx/rx
708      *	@dev: The 3c527 card to issue the command to
709      *
710      *	We issue the commands to halt the card's transceiver. In fact,
711      *	after some experimenting we now simply tell the card to
712      *	suspend. When issuing aborts occasionally odd things happened.
713      *
714      *	We then sleep until the card has notified us that both rx and
715      *	tx have been suspended.
716      */ 
717     
718     static void mc32_halt_transceiver(struct net_device *dev) 
719     {
720     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
721     	int ioaddr = dev->base_addr;
722     	unsigned long flags;
723     
724     	mc32_ready_poll(dev);	
725     
726     	lp->tx_box->mbox=0;
727     	lp->rx_box->mbox=0;
728     
729     	outb(HOST_CMD_SUSPND_RX, ioaddr+HOST_CMD);			
730     	mc32_ready_poll(dev); 
731     	outb(HOST_CMD_SUSPND_TX, ioaddr+HOST_CMD);	
732     		
733     	save_flags(flags);
734     	cli();
735     		
736     	while(lp->xceiver_state!=HALTED) 
737     		sleep_on(&lp->event); 
738     		
739     	restore_flags(flags);	
740     } 
741     
742     
743     /**
744      *	mc32_load_rx_ring	-	load the ring of receive buffers
745      *	@dev: 3c527 to build the ring for
746      *
747      *	This initalises the on-card and driver datastructures to
748      *	the point where mc32_start_transceiver() can be called.
749      *
750      *	The card sets up the receive ring for us. We are required to use the
751      *	ring it provides although we can change the size of the ring.
752      *
753      * 	We allocate an sk_buff for each ring entry in turn and
754      * 	initalise its house-keeping info. At the same time, we read
755      * 	each 'next' pointer in our rx_ring array. This reduces slow
756      * 	shared-memory reads and makes it easy to access predecessor
757      * 	descriptors.
758      *
759      *	We then set the end-of-list bit for the last entry so that the
760      * 	card will know when it has run out of buffers.
761      */
762     	 
763     static int mc32_load_rx_ring(struct net_device *dev)
764     {
765     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
766     	int i;
767     	u16 rx_base;
768     	volatile struct skb_header *p;
769     	
770     	rx_base=lp->rx_chain;
771     
772     	for(i=0;i<RX_RING_LEN;i++)
773     	{
774     		lp->rx_ring[i].skb=alloc_skb(1532, GFP_KERNEL);
775     		skb_reserve(lp->rx_ring[i].skb, 18);  
776     
777     		if(lp->rx_ring[i].skb==NULL)
778     		{
779     			for(;i>=0;i--)
780     				kfree_skb(lp->rx_ring[i].skb);
781     			return -ENOBUFS;
782     		}
783     		
784     		p=bus_to_virt(lp->base+rx_base);
785     				
786     		p->control=0;
787     		p->data=virt_to_bus(lp->rx_ring[i].skb->data);
788     		p->status=0;
789     		p->length=1532;
790     	
791     		lp->rx_ring[i].p=p; 
792     		rx_base=p->next; 
793     	}
794     
795     	lp->rx_ring[i-1].p->control |= CONTROL_EOL;
796     
797     	lp->rx_ring_tail=0;
798     
799     	return 0;
800     }	
801     
802     
803     /**
804      *	mc32_flush_rx_ring	-	free the ring of receive buffers
805      *	@lp: Local data of 3c527 to flush the rx ring of
806      *
807      *	Free the buffer for each ring slot. This may be called 
808      *      before mc32_load_rx_ring(), eg. on error in mc32_open().
809      */
810     
811     static void mc32_flush_rx_ring(struct net_device *dev)
812     {
813     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
814     	
815     	struct sk_buff *skb;
816     	int i; 
817     
818     	for(i=0; i < RX_RING_LEN; i++) 
819     	{ 
820     		skb = lp->rx_ring[i].skb;
821     		if (skb!=NULL) {
822     			kfree_skb(skb);
823     			skb=NULL; 
824     		}
825     		lp->rx_ring[i].p=NULL; 
826     	} 
827     }
828     
829     
830     /**
831      *	mc32_load_tx_ring	-	load transmit ring
832      *	@dev: The 3c527 card to issue the command to
833      *
834      *	This sets up the host transmit data-structures. 
835      *
836      *	First, we obtain from the card it's current postion in the tx
837      *	ring, so that we will know where to begin transmitting
838      *	packets.
839      * 	
840      * 	Then, we read the 'next' pointers from the on-card tx ring into
841      *  	our tx_ring array to reduce slow shared-mem reads. Finally, we
842      * 	intitalise the tx house keeping variables.
843      * 
844      */ 
845     
846     static void mc32_load_tx_ring(struct net_device *dev)
847     { 
848     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
849     	volatile struct skb_header *p;
850     	int i; 
851     	u16 tx_base;
852     
853     	tx_base=lp->tx_box->data[0]; 
854     
855     	for(i=0;i<lp->tx_len;i++) 
856     	{
857     		p=bus_to_virt(lp->base+tx_base);
858     		lp->tx_ring[i].p=p; 
859     		lp->tx_ring[i].skb=NULL;
860     
861     		tx_base=p->next;
862     	}
863     
864     	/* -1 so that tx_ring_head cannot "lap" tx_ring_tail,           */
865     	/* which would be bad news for mc32_tx_ring as cur. implemented */ 
866     
867     	atomic_set(&lp->tx_count, TX_RING_LEN-1); 
868     	lp->tx_ring_head=lp->tx_ring_tail=0; 
869     } 
870     
871     
872     /**
873      *	mc32_flush_tx_ring 	-	free transmit ring
874      *	@lp: Local data of 3c527 to flush the tx ring of
875      *
876      *	We have to consider two cases here. We want to free the pending
877      *	buffers only. If the ring buffer head is past the start then the
878      *	ring segment we wish to free wraps through zero. The tx ring 
879      *	house-keeping variables are then reset.
880      */
881     
882     static void mc32_flush_tx_ring(struct net_device *dev)
883     {
884     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
885     	
886     	if(lp->tx_ring_tail!=lp->tx_ring_head)
887     	{
888     		int i;	
889     		if(lp->tx_ring_tail < lp->tx_ring_head)
890     		{
891     			for(i=lp->tx_ring_tail;i<lp->tx_ring_head;i++)
892     			{
893     				dev_kfree_skb(lp->tx_ring[i].skb);
894     				lp->tx_ring[i].skb=NULL;
895     				lp->tx_ring[i].p=NULL; 
896     			}
897     		}
898     		else
899     		{
900     			for(i=lp->tx_ring_tail; i<TX_RING_LEN; i++) 
901     			{
902     				dev_kfree_skb(lp->tx_ring[i].skb);
903     				lp->tx_ring[i].skb=NULL;
904     				lp->tx_ring[i].p=NULL; 
905     			}
906     			for(i=0; i<lp->tx_ring_head; i++) 
907     			{
908     				dev_kfree_skb(lp->tx_ring[i].skb);
909     				lp->tx_ring[i].skb=NULL;
910     				lp->tx_ring[i].p=NULL; 
911     			}
912     		}
913     	}
914     	
915     	atomic_set(&lp->tx_count, 0); 
916     	lp->tx_ring_tail=lp->tx_ring_head=0;
917     }
918      	
919     
920     /**
921      *	mc32_open	-	handle 'up' of card
922      *	@dev: device to open
923      *
924      *	The user is trying to bring the card into ready state. This requires
925      *	a brief dialogue with the card. Firstly we enable interrupts and then
926      *	'indications'. Without these enabled the card doesn't bother telling
927      *	us what it has done. This had me puzzled for a week.
928      *
929      *	We configure the number of card descriptors, then load the network
930      *	address and multicast filters. Turn on the workaround mode. This
931      *	works around a bug in the 82586 - it asks the firmware to do
932      *	so. It has a performance (latency) hit but is needed on busy
933      *	[read most] lans. We load the ring with buffers then we kick it
934      *	all off.
935      */
936     
937     static int mc32_open(struct net_device *dev)
938     {
939     	int ioaddr = dev->base_addr;
940     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
941     	u8 one=1;
942     	u8 regs;
943     	u16 descnumbuffs[2] = {TX_RING_LEN, RX_RING_LEN};
944     
945     	/*
946     	 *	Interrupts enabled
947     	 */
948     
949     	regs=inb(ioaddr+HOST_CTRL);
950     	regs|=HOST_CTRL_INTE;
951     	outb(regs, ioaddr+HOST_CTRL);
952     	
953     
954     	/*
955     	 *	Send the indications on command
956     	 */
957     
958     	mc32_command(dev, 4, &one, 2);
959     
960     	/*
961     	 *	Poke it to make sure it's really dead. 
962     	 */
963     
964     	mc32_halt_transceiver(dev); 
965     	mc32_flush_tx_ring(dev); 
966     
967     	/* 
968     	 *	Ask card to set up on-card descriptors to our spec 
969     	 */ 
970     
971     	if(mc32_command(dev, 8, descnumbuffs, 4)) { 
972     		printk("%s: %s rejected our buffer configuration!\n",
973     	 	       dev->name, cardname);
974     		mc32_close(dev); 
975     		return -ENOBUFS; 
976     	}
977     	
978     	/* Report new configuration */ 
979     	mc32_command(dev, 6, NULL, 0); 
980     
981     	lp->tx_chain 		= lp->exec_box->data[8];   /* Transmit list start offset */
982     	lp->rx_chain 		= lp->exec_box->data[10];  /* Receive list start offset */
983     	lp->tx_len 		= lp->exec_box->data[9];   /* Transmit list count */ 
984     	lp->rx_len 		= lp->exec_box->data[11];  /* Receive list count */
985      
986     	/* Set Network Address */
987     	mc32_command(dev, 1, dev->dev_addr, 6);
988     	
989     	/* Set the filters */
990     	mc32_set_multicast_list(dev);
991     		   
992     	if (WORKAROUND_82586) { 
993     		u16 zero_word=0;
994     		mc32_command(dev, 0x0D, &zero_word, 2);   /* 82586 bug workaround on  */
995     	}
996     
997     	mc32_load_tx_ring(dev);
998     	
999     	if(mc32_load_rx_ring(dev)) 
1000     	{
1001     		mc32_close(dev);
1002     		return -ENOBUFS;
1003     	}
1004     
1005     	lp->desired_state = RUNNING; 
1006     	
1007     	/* And finally, set the ball rolling... */
1008     	mc32_start_transceiver(dev);
1009     
1010     	netif_start_queue(dev);
1011     
1012     	return 0;
1013     }
1014     
1015     
1016     /**
1017      *	mc32_timeout	-	handle a timeout from the network layer
1018      *	@dev: 3c527 that timed out
1019      *
1020      *	Handle a timeout on transmit from the 3c527. This normally means
1021      *	bad things as the hardware handles cable timeouts and mess for
1022      *	us.
1023      *
1024      */
1025     
1026     static void mc32_timeout(struct net_device *dev)
1027     {
1028     	printk(KERN_WARNING "%s: transmit timed out?\n", dev->name);
1029     	/* Try to restart the adaptor. */
1030     	netif_wake_queue(dev);
1031     }
1032     
1033     
1034     /**
1035      *	mc32_send_packet	-	queue a frame for transmit
1036      *	@skb: buffer to transmit
1037      *	@dev: 3c527 to send it out of
1038      *
1039      *	Transmit a buffer. This normally means throwing the buffer onto
1040      *	the transmit queue as the queue is quite large. If the queue is
1041      *	full then we set tx_busy and return. Once the interrupt handler
1042      *	gets messages telling it to reclaim transmit queue entries we will
1043      *	clear tx_busy and the kernel will start calling this again.
1044      *
1045      *	We use cli rather than spinlocks. Since I have no access to an SMP
1046      *	MCA machine I don't plan to change it. It is probably the top 
1047      *	performance hit for this driver on SMP however.
1048      */
1049     
1050     static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev)
1051     {
1052     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
1053     	unsigned long flags;
1054     
1055     	volatile struct skb_header *p, *np;
1056     
1057     	netif_stop_queue(dev);
1058     
1059     	save_flags(flags);
1060     	cli();
1061     		
1062     	if(atomic_read(&lp->tx_count)==0)
1063     	{
1064     		restore_flags(flags);
1065     		return 1;
1066     	}
1067     
1068     	atomic_dec(&lp->tx_count); 
1069     
1070     	/* P is the last sending/sent buffer as a pointer */
1071     	p=lp->tx_ring[lp->tx_ring_head].p; 
1072     		
1073     	lp->tx_ring_head=next_tx(lp->tx_ring_head); 
1074     
1075     	/* NP is the buffer we will be loading */
1076     	np=lp->tx_ring[lp->tx_ring_head].p; 
1077     
1078     	/* We will need this to flush the buffer out */
1079     	lp->tx_ring[lp->tx_ring_head].skb=skb;
1080        	   
1081     	np->length = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len; 
1082     			
1083     	np->data	= virt_to_bus(skb->data);
1084     	np->status	= 0;
1085     	np->control     = CONTROL_EOP | CONTROL_EOL;     
1086     	wmb();
1087     		
1088     	p->control     &= ~CONTROL_EOL;     /* Clear EOL on p */ 
1089     	
1090     	restore_flags(flags);
1091     
1092     	netif_wake_queue(dev);
1093     	return 0;
1094     }
1095     
1096     
1097     /**
1098      *	mc32_update_stats	-	pull off the on board statistics
1099      *	@dev: 3c527 to service
1100      *
1101      * 
1102      *	Query and reset the on-card stats. There's the small possibility
1103      *	of a race here, which would result in an underestimation of
1104      *	actual errors. As such, we'd prefer to keep all our stats
1105      *	collection in software. As a rule, we do. However it can't be
1106      *	used for rx errors and collisions as, by default, the card discards
1107      *	bad rx packets. 
1108      *
1109      *	Setting the SAV BP in the rx filter command supposedly
1110      *	stops this behaviour. However, testing shows that it only seems to
1111      *	enable the collation of on-card rx statistics --- the driver
1112      *	never sees an RX descriptor with an error status set.
1113      *
1114      */
1115     
1116     static void mc32_update_stats(struct net_device *dev)
1117     {
1118     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
1119     	volatile struct mc32_stats *st = lp->stats; 
1120     
1121     	u32 rx_errors=0; 
1122           
1123     	rx_errors+=lp->net_stats.rx_crc_errors   +=st->rx_crc_errors;         
1124     	                                           st->rx_crc_errors=0;
1125     	rx_errors+=lp->net_stats.rx_fifo_errors  +=st->rx_overrun_errors;   
1126     	                                           st->rx_overrun_errors=0; 
1127     	rx_errors+=lp->net_stats.rx_frame_errors +=st->rx_alignment_errors; 
1128      	                                           st->rx_alignment_errors=0;
1129     	rx_errors+=lp->net_stats.rx_length_errors+=st->rx_tooshort_errors; 
1130     	                                           st->rx_tooshort_errors=0;
1131     	rx_errors+=lp->net_stats.rx_missed_errors+=st->rx_outofresource_errors;
1132     	                                           st->rx_outofresource_errors=0; 
1133             lp->net_stats.rx_errors=rx_errors; 
1134     						   
1135     	/* Number of packets which saw one collision */
1136     	lp->net_stats.collisions+=st->dataC[10];
1137     	st->dataC[10]=0; 
1138     
1139     	/* Number of packets which saw 2--15 collisions */ 
1140     	lp->net_stats.collisions+=st->dataC[11]; 
1141     	st->dataC[11]=0; 
1142     }	
1143     
1144     
1145     /**
1146      *	mc32_rx_ring	-	process the receive ring
1147      *	@dev: 3c527 that needs its receive ring processing
1148      *
1149      *
1150      *	We have received one or more indications from the card that a
1151      *	receive has completed. The buffer ring thus contains dirty
1152      *	entries. We walk the ring by iterating over the circular rx_ring
1153      *	array, starting at the next dirty buffer (which happens to be the
1154      *	one we finished up at last time around).
1155      *
1156      *	For each completed packet, we will either copy it and pass it up
1157      * 	the stack or, if the packet is near MTU sized, we allocate
1158      *	another buffer and flip the old one up the stack.
1159      * 
1160      *	We must succeed in keeping a buffer on the ring. If neccessary we
1161      *	will toss a received packet rather than lose a ring entry. Once
1162      *	the first uncompleted descriptor is found, we move the
1163      *	End-Of-List bit to include the buffers just processed.
1164      *
1165      */
1166     
1167     static void mc32_rx_ring(struct net_device *dev)
1168     {
1169     	struct mc32_local *lp=dev->priv;		
1170     	volatile struct skb_header *p;
1171     	u16 rx_ring_tail = lp->rx_ring_tail;
1172     	u16 rx_old_tail = rx_ring_tail; 
1173     
1174     	int x=0;
1175     	
1176     	do
1177     	{ 
1178     		p=lp->rx_ring[rx_ring_tail].p; 
1179     
1180     		if(!(p->status & (1<<7))) { /* Not COMPLETED */ 
1181     			break;
1182     		} 
1183     		if(p->status & (1<<6)) /* COMPLETED_OK */
1184     		{		        
1185     
1186     			u16 length=p->length;
1187     			struct sk_buff *skb; 
1188     			struct sk_buff *newskb; 
1189     
1190     			/* Try to save time by avoiding a copy on big frames */
1191     
1192     			if ((length > RX_COPYBREAK) 
1193     			    && ((newskb=dev_alloc_skb(1532)) != NULL)) 
1194     			{ 
1195     				skb=lp->rx_ring[rx_ring_tail].skb;
1196     				skb_put(skb, length);
1197     				
1198     				skb_reserve(newskb,18); 
1199     				lp->rx_ring[rx_ring_tail].skb=newskb;  
1200     				p->data=virt_to_bus(newskb->data);  
1201     			} 
1202     			else 
1203     			{
1204     				skb=dev_alloc_skb(length+2);  
1205     
1206     				if(skb==NULL) {
1207     					lp->net_stats.rx_dropped++; 
1208     					goto dropped; 
1209     				}
1210     
1211     				skb_reserve(skb,2);
1212     				memcpy(skb_put(skb, length),
1213     				       lp->rx_ring[rx_ring_tail].skb->data, length);
1214     			}
1215     			
1216     			skb->protocol=eth_type_trans(skb,dev); 
1217     			skb->dev=dev; 
1218     			dev->last_rx = jiffies;
1219      			lp->net_stats.rx_packets++; 
1220      			lp->net_stats.rx_bytes += length; 
1221     			netif_rx(skb);
1222     		}
1223     
1224     	dropped:
1225     		p->length = 1532; 
1226     		p->status = 0;
1227     		
1228     		rx_ring_tail=next_rx(rx_ring_tail); 
1229     	}
1230             while(x++<48);  
1231     
1232     	/* If there was actually a frame to be processed, place the EOL bit */ 
1233     	/* at the descriptor prior to the one to be filled next */ 
1234     
1235     	if (rx_ring_tail != rx_old_tail) 
1236     	{ 
1237     		lp->rx_ring[prev_rx(rx_ring_tail)].p->control |=  CONTROL_EOL; 
1238     		lp->rx_ring[prev_rx(rx_old_tail)].p->control  &= ~CONTROL_EOL; 
1239     
1240     		lp->rx_ring_tail=rx_ring_tail; 
1241     	}
1242     }
1243     
1244     
1245     /**
1246      *	mc32_tx_ring	-	process completed transmits
1247      *	@dev: 3c527 that needs its transmit ring processing
1248      *
1249      *
1250      *	This operates in a similar fashion to mc32_rx_ring. We iterate
1251      *	over the transmit ring. For each descriptor which has been
1252      *	processed by the card, we free its associated buffer and note
1253      *	any errors. This continues until the transmit ring is emptied
1254      *	or we reach a descriptor that hasn't yet been processed by the
1255      *	card.
1256      * 
1257      */
1258     
1259     static void mc32_tx_ring(struct net_device *dev) 
1260     {
1261     	struct mc32_local *lp=(struct mc32_local *)dev->priv;
1262     	volatile struct skb_header *np;
1263     
1264     	/* NB: lp->tx_count=TX_RING_LEN-1 so that tx_ring_head cannot "lap" tail here */
1265     
1266     	while (lp->tx_ring_tail != lp->tx_ring_head)  
1267     	{   
1268     		u16 t; 
1269     
1270     		t=next_tx(lp->tx_ring_tail); 
1271     		np=lp->tx_ring[t].p; 
1272     
1273     		if(!(np->status & (1<<7))) 
1274     		{
1275     			/* Not COMPLETED */ 
1276     			break; 
1277     		} 
1278     		lp->net_stats.tx_packets++;
1279     		if(!(np->status & (1<<6))) /* Not COMPLETED_OK */
1280     		{
1281     			lp->net_stats.tx_errors++;   
1282     
1283     			switch(np->status&0x0F)
1284     			{
1285     				case 1:
1286     					lp->net_stats.tx_aborted_errors++;
1287     					break; /* Max collisions */ 
1288     				case 2:
1289     					lp->net_stats.tx_fifo_errors++;
1290     					break;
1291     				case 3:
1292     					lp->net_stats.tx_carrier_errors++;
1293     					break;
1294     				case 4:
1295     					lp->net_stats.tx_window_errors++;
1296     					break;  /* CTS Lost */ 
1297     				case 5:
1298     					lp->net_stats.tx_aborted_errors++;
1299     					break; /* Transmit timeout */ 
1300     			}
1301     		}
1302     		/* Packets are sent in order - this is
1303     		    basically a FIFO queue of buffers matching
1304     		    the card ring */
1305     		lp->net_stats.tx_bytes+=lp->tx_ring[t].skb->len;
1306     		dev_kfree_skb_irq(lp->tx_ring[t].skb);
1307     		lp->tx_ring[t].skb=NULL;
1308     		atomic_inc(&lp->tx_count);
1309     		netif_wake_queue(dev);
1310     
1311     		lp->tx_ring_tail=t; 
1312     	}
1313     
1314     } 
1315     
1316     
1317     /**
1318      *	mc32_interrupt		-	handle an interrupt from a 3c527
1319      *	@irq: Interrupt number
1320      *	@dev_id: 3c527 that requires servicing
1321      *	@regs: Registers (unused)
1322      *
1323      *
1324      *	An interrupt is raised whenever the 3c527 writes to the command
1325      *	register. This register contains the message it wishes to send us
1326      *	packed into a single byte field. We keep reading status entries
1327      *	until we have processed all the control items, but simply count
1328      *	transmit and receive reports. When all reports are in we empty the
1329      *	transceiver rings as appropriate. This saves the overhead of
1330      *	multiple command requests.
1331      *
1332      *	Because MCA is level-triggered, we shouldn't miss indications.
1333      *	Therefore, we needn't ask the card to suspend interrupts within
1334      *	this handler. The card receives an implicit acknowledgment of the
1335      *	current interrupt when we read the command register.
1336      *
1337      */
1338     
1339     static void mc32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1340     {
1341     	struct net_device *dev = dev_id;
1342     	struct mc32_local *lp;
1343     	int ioaddr, status, boguscount = 0;
1344     	int rx_event = 0;
1345     	int tx_event = 0; 
1346     	
1347     	if (dev == NULL) {
1348     		printk(KERN_WARNING "%s: irq %d for unknown device.\n", cardname, irq);
1349     		return;
1350     	}
1351      
1352     	ioaddr = dev->base_addr;
1353     	lp = (struct mc32_local *)dev->priv;
1354     
1355     	/* See whats cooking */
1356     
1357     	while((inb(ioaddr+HOST_STATUS)&HOST_STATUS_CWR) && boguscount++<2000)
1358     	{
1359     		status=inb(ioaddr+HOST_CMD);
1360     
1361     #ifdef DEBUG_IRQ		
1362     		printk("Status TX%d RX%d EX%d OV%d BC%d\n",
1363     			(status&7), (status>>3)&7, (status>>6)&1,
1364     			(status>>7)&1, boguscount);
1365     #endif
1366     			
1367     		switch(status&7)
1368     		{
1369     			case 0:
1370     				break;
1371     			case 6: /* TX fail */
1372     			case 2:	/* TX ok */
1373     				tx_event = 1; 
1374     				break;
1375     			case 3: /* Halt */
1376     			case 4: /* Abort */
1377     				lp->xceiver_state |= TX_HALTED; 
1378     				wake_up(&lp->event);
1379     				break;
1380     			default:
1381     				printk("%s: strange tx ack %d\n", dev->name, status&7);
1382     		}
1383     		status>>=3;
1384     		switch(status&7)
1385     		{
1386     			case 0:
1387     				break;
1388     			case 2:	/* RX */
1389     				rx_event=1; 
1390     				break;
1391     			case 3: /* Halt */
1392     			case 4: /* Abort */
1393     				lp->xceiver_state |= RX_HALTED;
1394     				wake_up(&lp->event);
1395     				break;
1396     			case 6:
1397     				/* Out of RX buffers stat */
1398     				/* Must restart rx */
1399     				lp->net_stats.rx_dropped++;
1400     				mc32_rx_ring(dev); 
1401     				mc32_start_transceiver(dev); 
1402     				break;
1403     			default:
1404     				printk("%s: strange rx ack %d\n", 
1405     					dev->name, status&7);			
1406     		}
1407     		status>>=3;
1408     		if(status&1)
1409     		{
1410     
1411     			/* 0=no 1=yes 2=replied, get cmd, 3 = wait reply & dump it */
1412     			
1413     			if(lp->exec_pending!=3) {
1414     				lp->exec_pending=2;
1415     				wake_up(&lp->event);
1416     			}
1417     			else 
1418     			{				
1419     			  	lp->exec_pending=0;
1420     
1421     				/* A new multicast set may have been
1422     				   blocked while the old one was
1423     				   running. If so, do it now. */
1424     				   
1425     				if (lp->mc_reload_wait) 
1426     					mc32_reset_multicast_list(dev);
1427     				else 
1428     					wake_up(&lp->event);			       
1429     			}
1430     		}
1431     		if(status&2)
1432     		{
1433     			/*
1434     			 *	We get interrupted once per
1435     			 *	counter that is about to overflow. 
1436     			 */
1437     
1438     			mc32_update_stats(dev);			
1439     		}
1440     	}
1441     
1442     
1443     	/*
1444     	 *	Process the transmit and receive rings 
1445              */
1446     
1447     	if(tx_event) 
1448     		mc32_tx_ring(dev);
1449     	 
1450     	if(rx_event) 
1451     		mc32_rx_ring(dev);
1452     
1453     	return;
1454     }
1455     
1456     
1457     /**
1458      *	mc32_close	-	user configuring the 3c527 down
1459      *	@dev: 3c527 card to shut down
1460      *
1461      *	The 3c527 is a bus mastering device. We must be careful how we
1462      *	shut it down. It may also be running shared interrupt so we have
1463      *	to be sure to silence it properly
1464      *
1465      *	We indicate that the card is closing to the rest of the
1466      *	driver.  Otherwise, it is possible that the card may run out
1467      *	of receive buffers and restart the transceiver while we're
1468      *	trying to close it.
1469      * 
1470      *	We abort any receive and transmits going on and then wait until
1471      *	any pending exec commands have completed in other code threads.
1472      *	In theory we can't get here while that is true, in practice I am
1473      *	paranoid
1474      *
1475      *	We turn off the interrupt enable for the board to be sure it can't
1476      *	intefere with other devices.
1477      */
1478     
1479     static int mc32_close(struct net_device *dev)
1480     {
1481     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
1482     
1483     	int ioaddr = dev->base_addr;
1484     	u8 regs;
1485     	u16 one=1;
1486     	
1487     	lp->desired_state = HALTED;
1488     	netif_stop_queue(dev);
1489     
1490     	/*
1491     	 *	Send the indications on command (handy debug check)
1492     	 */
1493     
1494     	mc32_command(dev, 4, &one, 2);
1495     
1496     	/* Shut down the transceiver */
1497     
1498     	mc32_halt_transceiver(dev); 
1499     	
1500     	/* Catch any waiting commands */
1501     	
1502     	while(lp->exec_pending==1)
1503     		sleep_on(&lp->event);
1504     	       
1505     	/* Ok the card is now stopping */	
1506     	
1507     	regs=inb(ioaddr+HOST_CTRL);
1508     	regs&=~HOST_CTRL_INTE;
1509     	outb(regs, ioaddr+HOST_CTRL);
1510     
1511     	mc32_flush_rx_ring(dev);
1512     	mc32_flush_tx_ring(dev);
1513     		
1514     	mc32_update_stats(dev); 
1515     
1516     	return 0;
1517     }
1518     
1519     
1520     /**
1521      *	mc32_get_stats		-	hand back stats to network layer
1522      *	@dev: The 3c527 card to handle
1523      *
1524      *	We've collected all the stats we can in software already. Now
1525      *	it's time to update those kept on-card and return the lot. 
1526      * 
1527      */
1528     
1529     static struct net_device_stats *mc32_get_stats(struct net_device *dev)
1530     {
1531     	struct mc32_local *lp;
1532     	
1533     	mc32_update_stats(dev); 
1534     
1535     	lp = (struct mc32_local *)dev->priv;
1536     
1537     	return &lp->net_stats;
1538     }
1539     
1540     
1541     /**
1542      *	do_mc32_set_multicast_list	-	attempt to update multicasts
1543      *	@dev: 3c527 device to load the list on
1544      *	@retry: indicates this is not the first call. 
1545      *
1546      *
1547      * 	Actually set or clear the multicast filter for this adaptor. The
1548      *	locking issues are handled by this routine. We have to track
1549      *	state as it may take multiple calls to get the command sequence
1550      *	completed. We just keep trying to schedule the loads until we
1551      *	manage to process them all.
1552      * 
1553      *	num_addrs == -1	Promiscuous mode, receive all packets
1554      * 
1555      *	num_addrs == 0	Normal mode, clear multicast list
1556      * 
1557      *	num_addrs > 0	Multicast mode, receive normal and MC packets, 
1558      *			and do best-effort filtering. 
1559      *
1560      *	See mc32_update_stats() regards setting the SAV BP bit. 
1561      *
1562      */
1563     
1564     static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
1565     {
1566     	struct mc32_local *lp = (struct mc32_local *)dev->priv;
1567     	u16 filt = (1<<2); /* Save Bad Packets, for stats purposes */ 
1568     
1569     	if (dev->flags&IFF_PROMISC)
1570     		/* Enable promiscuous mode */
1571     		filt |= 1;
1572     	else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > 10)
1573     	{
1574     		dev->flags|=IFF_PROMISC;
1575     		filt |= 1;
1576     	}
1577     	else if(dev->mc_count)
1578     	{
1579     		unsigned char block[62];
1580     		unsigned char *bp;
1581     		struct dev_mc_list *dmc=dev->mc_list;
1582     		
1583     		int i;
1584     	       
1585     		if(retry==0)
1586     			lp->mc_list_valid = 0;
1587     		if(!lp->mc_list_valid)
1588     		{
1589     			block[1]=0;
1590     			block[0]=dev->mc_count;
1591     			bp=block+2;
1592     		
1593     			for(i=0;i<dev->mc_count;i++)
1594     			{
1595     				memcpy(bp, dmc->dmi_addr, 6);
1596     				bp+=6;
1597     				dmc=dmc->next;
1598     			}
1599     			if(mc32_command_nowait(dev, 2, block, 2+6*dev->mc_count)==-1)
1600     			{
1601     				lp->mc_reload_wait = 1;
1602     				return;
1603     			}
1604     			lp->mc_list_valid=1;
1605     		}
1606     	}
1607     	
1608     	if(mc32_command_nowait(dev, 0, &filt, 2)==-1) 
1609     	{
1610     		lp->mc_reload_wait = 1;
1611     	} 
1612     	else { 
1613     		lp->mc_reload_wait = 0;
1614     	}
1615     }
1616     
1617     
1618     /**
1619      *	mc32_set_multicast_list	-	queue multicast list update
1620      *	@dev: The 3c527 to use
1621      *
1622      *	Commence loading the multicast list. This is called when the kernel
1623      *	changes the lists. It will override any pending list we are trying to
1624      *	load.
1625      */
1626     
1627     static void mc32_set_multicast_list(struct net_device *dev)
1628     {
1629     	do_mc32_set_multicast_list(dev,0);
1630     }
1631     
1632     
1633     /**
1634      *	mc32_reset_multicast_list	-	reset multicast list
1635      *	@dev: The 3c527 to use
1636      *
1637      *	Attempt the next step in loading the multicast lists. If this attempt
1638      *	fails to complete then it will be scheduled and this function called
1639      *	again later from elsewhere.
1640      */
1641     
1642     static void mc32_reset_multicast_list(struct net_device *dev)
1643     {
1644     	do_mc32_set_multicast_list(dev,1);
1645     }
1646     
1647     #ifdef MODULE
1648     
1649     static struct net_device this_device;
1650     
1651     /**
1652      *	init_module		-	entry point
1653      *
1654      *	Probe and locate a 3c527 card. This really should probe and locate
1655      *	all the 3c527 cards in the machine not just one of them. Yes you can
1656      *	insmod multiple modules for now but it's a hack.
1657      */
1658     
1659     int init_module(void)
1660     {
1661     	int result;
1662     	
1663     	this_device.init = mc32_probe;
1664     	if ((result = register_netdev(&this_device)) != 0)
1665     		return result;
1666     
1667     	return 0;
1668     }
1669     
1670     /**
1671      *	cleanup_module	-	free resources for an unload
1672      *
1673      *	Unloading time. We release the MCA bus resources and the interrupt
1674      *	at which point everything is ready to unload. The card must be stopped
1675      *	at this point or we would not have been called. When we unload we
1676      *	leave the card stopped but not totally shut down. When the card is
1677      *	initialized it must be rebooted or the rings reloaded before any
1678      *	transmit operations are allowed to start scribbling into memory.
1679      */
1680     
1681     void cleanup_module(void)
1682     {
1683     	int slot;
1684     	
1685     	/* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1686     	unregister_netdev(&this_device);
1687     
1688     	/*
1689     	 * If we don't do this, we can't re-insmod it later.
1690     	 */
1691     	 
1692     	if (this_device.priv)
1693     	{
1694     		struct mc32_local *lp=this_device.priv;
1695     		slot = lp->slot;
1696     		mca_mark_as_unused(slot);
1697     		mca_set_adapter_name(slot, NULL);
1698     		kfree(this_device.priv);
1699     	}
1700     	free_irq(this_device.irq, &this_device);
1701     	release_region(this_device.base_addr, MC32_IO_EXTENT);
1702     }
1703     
1704     #endif /* MODULE */
1705