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

1     /* 3c501.c: A 3Com 3c501 Ethernet driver for Linux. */
2     /*
3         Written 1992,1993,1994  Donald Becker
4     
5         Copyright 1993 United States Government as represented by the
6         Director, National Security Agency.  This software may be used and
7         distributed according to the terms of the GNU General Public License,
8         incorporated herein by reference.
9     
10         This is a device driver for the 3Com Etherlink 3c501.
11         Do not purchase this card, even as a joke.  It's performance is horrible,
12         and it breaks in many ways.
13     
14         The author may be reached as becker@scyld.com, or C/O
15     	Scyld Computing Corporation
16     	410 Severn Ave., Suite 210
17     	Annapolis MD 21403
18     
19     
20         Fixed (again!) the missing interrupt locking on TX/RX shifting.
21         		Alan Cox <Alan.Cox@linux.org>
22     
23         Removed calls to init_etherdev since they are no longer needed, and
24         cleaned up modularization just a bit. The driver still allows only
25         the default address for cards when loaded as a module, but that's
26         really less braindead than anyone using a 3c501 board. :)
27     		    19950208 (invid@msen.com)
28     
29         Added traps for interrupts hitting the window as we clear and TX load
30         the board. Now getting 150K/second FTP with a 3c501 card. Still playing
31         with a TX-TX optimisation to see if we can touch 180-200K/second as seems
32         theoretically maximum.
33         		19950402 Alan Cox <Alan.Cox@linux.org>
34         		
35         Cleaned up for 2.3.x because we broke SMP now. 
36         		20000208 Alan Cox <alan@redhat.com>
37         		
38     */
39     
40     
41     /**
42      * DOC: 3c501 Card Notes
43      *
44      *  Some notes on this thing if you have to hack it.  [Alan]
45      *
46      *  Some documentation is available from 3Com. Due to the boards age
47      *  standard responses when you ask for this will range from 'be serious'
48      *  to 'give it to a museum'. The documentation is incomplete and mostly
49      *  of historical interest anyway. 
50      *
51      *  The basic system is a single buffer which can be used to receive or
52      *  transmit a packet. A third command mode exists when you are setting
53      *  things up.
54      *
55      *  If it's transmitting it's not receiving and vice versa. In fact the
56      *  time to get the board back into useful state after an operation is
57      *  quite large.
58      *
59      *  The driver works by keeping the board in receive mode waiting for a
60      *  packet to arrive. When one arrives it is copied out of the buffer
61      *  and delivered to the kernel. The card is reloaded and off we go.
62      *
63      *  When transmitting lp->txing is set and the card is reset (from
64      *  receive mode) [possibly losing a packet just received] to command
65      *  mode. A packet is loaded and transmit mode triggered. The interrupt
66      *  handler runs different code for transmit interrupts and can handle
67      *  returning to receive mode or retransmissions (yes you have to help
68      *  out with those too).
69      *
70      * DOC: Problems
71      *  
72      *  There are a wide variety of undocumented error returns from the card
73      *  and you basically have to kick the board and pray if they turn up. Most
74      *  only occur under extreme load or if you do something the board doesn't
75      *  like (eg touching a register at the wrong time).
76      *
77      *  The driver is less efficient than it could be. It switches through
78      *  receive mode even if more transmits are queued. If this worries you buy
79      *  a real Ethernet card.
80      *
81      *  The combination of slow receive restart and no real multicast
82      *  filter makes the board unusable with a kernel compiled for IP
83      *  multicasting in a real multicast environment. That's down to the board,
84      *  but even with no multicast programs running a multicast IP kernel is
85      *  in group 224.0.0.1 and you will therefore be listening to all multicasts.
86      *  One nv conference running over that Ethernet and you can give up.
87      *
88      */
89     
90     static const char version[] =
91         "3c501.c: 2000/02/08 Alan Cox (alan@redhat.com).\n";
92     
93     /*
94      *	Braindamage remaining:
95      *	The 3c501 board.
96      */
97     
98     #include <linux/module.h>
99     
100     #include <linux/kernel.h>
101     #include <linux/sched.h>
102     #include <linux/ptrace.h>
103     #include <linux/fcntl.h>
104     #include <linux/ioport.h>
105     #include <linux/interrupt.h>
106     #include <linux/slab.h>
107     #include <linux/string.h>
108     #include <linux/errno.h>
109     #include <linux/config.h>	/* for CONFIG_IP_MULTICAST */
110     #include <linux/spinlock.h>
111     
112     #include <asm/bitops.h>
113     #include <asm/io.h>
114     
115     #include <linux/netdevice.h>
116     #include <linux/etherdevice.h>
117     #include <linux/skbuff.h>
118     #include <linux/init.h>
119     
120     /* A zero-terminated list of I/O addresses to be probed.
121        The 3c501 can be at many locations, but here are the popular ones. */
122     static unsigned int netcard_portlist[] __initdata = { 
123     	0x280, 0x300, 0
124     };
125     
126     
127     /*
128      *	Index to functions.
129      */
130     
131     int el1_probe(struct net_device *dev);
132     static int  el1_probe1(struct net_device *dev, int ioaddr);
133     static int  el_open(struct net_device *dev);
134     static void el_timeout(struct net_device *dev);
135     static int  el_start_xmit(struct sk_buff *skb, struct net_device *dev);
136     static void el_interrupt(int irq, void *dev_id, struct pt_regs *regs);
137     static void el_receive(struct net_device *dev);
138     static void el_reset(struct net_device *dev);
139     static int  el1_close(struct net_device *dev);
140     static struct net_device_stats *el1_get_stats(struct net_device *dev);
141     static void set_multicast_list(struct net_device *dev);
142     
143     #define EL1_IO_EXTENT	16
144     
145     #ifndef EL_DEBUG
146     #define EL_DEBUG  0	/* use 0 for production, 1 for devel., >2 for debug */
147     #endif			/* Anything above 5 is wordy death! */
148     static int el_debug = EL_DEBUG;
149     
150     /*
151      *	Board-specific info in dev->priv.
152      */
153     
154     struct net_local
155     {
156     	struct net_device_stats stats;
157     	int		tx_pkt_start;	/* The length of the current Tx packet. */
158     	int		collisions;	/* Tx collisions this packet */
159     	int		loading;	/* Spot buffer load collisions */
160     	int		txing;		/* True if card is in TX mode */
161     	spinlock_t	lock;		/* Serializing lock */
162     };
163     
164     
165     #define RX_STATUS (ioaddr + 0x06)
166     #define RX_CMD	  RX_STATUS
167     #define TX_STATUS (ioaddr + 0x07)
168     #define TX_CMD	  TX_STATUS
169     #define GP_LOW 	  (ioaddr + 0x08)
170     #define GP_HIGH   (ioaddr + 0x09)
171     #define RX_BUF_CLR (ioaddr + 0x0A)
172     #define RX_LOW	  (ioaddr + 0x0A)
173     #define RX_HIGH   (ioaddr + 0x0B)
174     #define SAPROM	  (ioaddr + 0x0C)
175     #define AX_STATUS (ioaddr + 0x0E)
176     #define AX_CMD	  AX_STATUS
177     #define DATAPORT  (ioaddr + 0x0F)
178     #define TX_RDY 0x08		/* In TX_STATUS */
179     
180     #define EL1_DATAPTR	0x08
181     #define EL1_RXPTR	0x0A
182     #define EL1_SAPROM	0x0C
183     #define EL1_DATAPORT 	0x0f
184     
185     /*
186      *	Writes to the ax command register.
187      */
188     
189     #define AX_OFF	0x00			/* Irq off, buffer access on */
190     #define AX_SYS  0x40			/* Load the buffer */
191     #define AX_XMIT 0x44			/* Transmit a packet */
192     #define AX_RX	0x48			/* Receive a packet */
193     #define AX_LOOP	0x0C			/* Loopback mode */
194     #define AX_RESET 0x80
195     
196     /*
197      *	Normal receive mode written to RX_STATUS.  We must intr on short packets
198      *	to avoid bogus rx lockups.
199      */
200     
201     #define RX_NORM 0xA8		/* 0x68 == all addrs, 0xA8 only to me. */
202     #define RX_PROM 0x68		/* Senior Prom, uhmm promiscuous mode. */
203     #define RX_MULT 0xE8		/* Accept multicast packets. */
204     #define TX_NORM 0x0A		/* Interrupt on everything that might hang the chip */
205     
206     /*
207      *	TX_STATUS register.
208      */
209     
210     #define TX_COLLISION 0x02
211     #define TX_16COLLISIONS 0x04
212     #define TX_READY 0x08
213     
214     #define RX_RUNT 0x08
215     #define RX_MISSED 0x01		/* Missed a packet due to 3c501 braindamage. */
216     #define RX_GOOD	0x30		/* Good packet 0x20, or simple overflow 0x10. */
217     
218     
219     /*
220      *	The boilerplate probe code.
221      */
222     
223     /**
224      * el1_probe:
225      * @dev: The device structure passed in to probe. 
226      *
227      * This can be called from two places. The network layer will probe using
228      * a device structure passed in with the probe information completed. For a
229      * modular driver we use #init_module to fill in our own structure and probe
230      * for it.
231      *
232      * Returns 0 on success. ENXIO if asked not to probe and ENODEV if asked to
233      * probe and failing to find anything.
234      */
235      
236     int __init el1_probe(struct net_device *dev)
237     {
238     	int i;
239     	int base_addr = dev->base_addr;
240     
241     	SET_MODULE_OWNER(dev);
242     
243     	if (base_addr > 0x1ff)	/* Check a single specified location. */
244     		return el1_probe1(dev, base_addr);
245     	else if (base_addr != 0)	/* Don't probe at all. */
246     		return -ENXIO;
247     
248     	for (i = 0; netcard_portlist[i]; i++)
249     		if (el1_probe1(dev, netcard_portlist[i]) == 0)
250     			return 0;
251     
252     	return -ENODEV;
253     }
254     
255     /**
256      *	el1_probe1: 
257      *	@dev: The device structure to use
258      *	@ioaddr: An I/O address to probe at.
259      *
260      *	The actual probe. This is iterated over by #el1_probe in order to
261      *	check all the applicable device locations.
262      *
263      *	Returns 0 for a success, in which case the device is activated,
264      *	EAGAIN if the IRQ is in use by another driver, and ENODEV if the
265      *	board cannot be found.
266      */
267     
268     static int __init el1_probe1(struct net_device *dev, int ioaddr)
269     {
270     	struct net_local *lp;
271     	const char *mname;		/* Vendor name */
272     	unsigned char station_addr[6];
273     	int autoirq = 0;
274     	int i;
275     
276     	/*
277     	 *	Reserve I/O resource for exclusive use by this driver
278     	 */
279     
280     	if (!request_region(ioaddr, EL1_IO_EXTENT, dev->name))
281     		return -ENODEV;
282     
283     	/*
284     	 *	Read the station address PROM data from the special port.
285     	 */
286     
287     	for (i = 0; i < 6; i++)
288     	{
289     		outw(i, ioaddr + EL1_DATAPTR);
290     		station_addr[i] = inb(ioaddr + EL1_SAPROM);
291     	}
292     	/*
293     	 *	Check the first three octets of the S.A. for 3Com's prefix, or
294     	 *	for the Sager NP943 prefix.
295     	 */
296     
297     	if (station_addr[0] == 0x02  &&  station_addr[1] == 0x60
298     		&& station_addr[2] == 0x8c)
299     	{
300     		mname = "3c501";
301     	} else if (station_addr[0] == 0x00  &&  station_addr[1] == 0x80
302     	&& station_addr[2] == 0xC8)
303     	{
304     		mname = "NP943";
305         	}
306         	else {
307     		release_region(ioaddr, EL1_IO_EXTENT);
308     		return -ENODEV;
309     	}
310     
311     	/*
312     	 *	We auto-IRQ by shutting off the interrupt line and letting it float
313     	 *	high.
314     	 */
315     
316     	if (dev->irq < 2)
317     	{
318     		autoirq_setup(2);
319     		inb(RX_STATUS);		/* Clear pending interrupts. */
320     		inb(TX_STATUS);
321     		outb(AX_LOOP + 1, AX_CMD);
322     
323     		outb(0x00, AX_CMD);
324     
325     		autoirq = autoirq_report(1);
326     
327     		if (autoirq == 0)
328     		{
329     			printk("%s probe at %#x failed to detect IRQ line.\n",
330     				mname, ioaddr);
331     			release_region(ioaddr, EL1_IO_EXTENT);
332     			return -EAGAIN;
333     		}
334     	}
335     
336     	outb(AX_RESET+AX_LOOP, AX_CMD);			/* Loopback mode. */
337     	dev->base_addr = ioaddr;
338     	memcpy(dev->dev_addr, station_addr, ETH_ALEN);
339     
340     	if (dev->mem_start & 0xf)
341     		el_debug = dev->mem_start & 0x7;
342     	if (autoirq)
343     		dev->irq = autoirq;
344     
345     	printk(KERN_INFO "%s: %s EtherLink at %#lx, using %sIRQ %d.\n", dev->name, mname, dev->base_addr,
346     			autoirq ? "auto":"assigned ", dev->irq);
347     
348     #ifdef CONFIG_IP_MULTICAST
349     	printk(KERN_WARNING "WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
350     #endif
351     
352     	if (el_debug)
353     		printk("%s", version);
354     
355     	/*
356     	 *	Initialize the device structure.
357     	 */
358     
359     	dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
360     	if (dev->priv == NULL) {
361     		release_region(ioaddr, EL1_IO_EXTENT);
362     		return -ENOMEM;
363     	}
364     	memset(dev->priv, 0, sizeof(struct net_local));
365     
366     	lp=dev->priv;
367     	spin_lock_init(&lp->lock);
368     	
369     	/*
370     	 *	The EL1-specific entries in the device structure.
371     	 */
372     
373     	dev->open = &el_open;
374     	dev->hard_start_xmit = &el_start_xmit;
375     	dev->tx_timeout = &el_timeout;
376     	dev->watchdog_timeo = HZ;
377     	dev->stop = &el1_close;
378     	dev->get_stats = &el1_get_stats;
379     	dev->set_multicast_list = &set_multicast_list;
380     
381     	/*
382     	 *	Setup the generic properties
383     	 */
384     
385     	ether_setup(dev);
386     
387     	return 0;
388     }
389     
390     /**
391      *	el1_open:
392      *	@dev: device that is being opened
393      *
394      *	When an ifconfig is issued which changes the device flags to include
395      *	IFF_UP this function is called. It is only called when the change 
396      *	occurs, not when the interface remains up. #el1_close will be called
397      *	when it goes down.
398      *
399      *	Returns 0 for a successful open, or -EAGAIN if someone has run off
400      *	with our interrupt line.
401      */
402     
403     static int el_open(struct net_device *dev)
404     {
405     	int retval;
406     	int ioaddr = dev->base_addr;
407     	struct net_local *lp = (struct net_local *)dev->priv;
408     	unsigned long flags;
409     
410     	if (el_debug > 2)
411     		printk("%s: Doing el_open()...", dev->name);
412     
413     	if ((retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev)))
414     		return retval;
415     
416     	spin_lock_irqsave(&lp->lock, flags);
417     	el_reset(dev);
418     	spin_unlock_irqrestore(&lp->lock, flags);
419     
420     	lp->txing = 0;		/* Board in RX mode */
421     	outb(AX_RX, AX_CMD);	/* Aux control, irq and receive enabled */
422     	netif_start_queue(dev);
423     	return 0;
424     }
425     
426     /**
427      * el_timeout:
428      * @dev: The 3c501 card that has timed out
429      *
430      * Attempt to restart the board. This is basically a mixture of extreme
431      * violence and prayer
432      *
433      */
434      
435     static void el_timeout(struct net_device *dev)
436     {
437     	struct net_local *lp = (struct net_local *)dev->priv;
438     	int ioaddr = dev->base_addr;
439      
440     	if (el_debug)
441     		printk (KERN_DEBUG "%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
442     			dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
443     	lp->stats.tx_errors++;
444     	outb(TX_NORM, TX_CMD);
445     	outb(RX_NORM, RX_CMD);
446     	outb(AX_OFF, AX_CMD);	/* Just trigger a false interrupt. */
447     	outb(AX_RX, AX_CMD);	/* Aux control, irq and receive enabled */
448     	lp->txing = 0;		/* Ripped back in to RX */
449     	netif_wake_queue(dev);
450     }
451     
452      
453     /**
454      * el_start_xmit:
455      * @skb: The packet that is queued to be sent
456      * @dev: The 3c501 card we want to throw it down
457      *
458      * Attempt to send a packet to a 3c501 card. There are some interesting
459      * catches here because the 3c501 is an extremely old and therefore
460      * stupid piece of technology.
461      *
462      * If we are handling an interrupt on the other CPU we cannot load a packet
463      * as we may still be attempting to retrieve the last RX packet buffer.
464      *
465      * When a transmit times out we dump the card into control mode and just
466      * start again. It happens enough that it isnt worth logging.
467      *
468      * We avoid holding the spin locks when doing the packet load to the board.
469      * The device is very slow, and its DMA mode is even slower. If we held the
470      * lock while loading 1500 bytes onto the controller we would drop a lot of
471      * serial port characters. This requires we do extra locking, but we have
472      * no real choice.
473      */
474     
475     static int el_start_xmit(struct sk_buff *skb, struct net_device *dev)
476     {
477     	struct net_local *lp = (struct net_local *)dev->priv;
478     	int ioaddr = dev->base_addr;
479     	unsigned long flags;
480     
481     	/*
482     	 *	Avoid incoming interrupts between us flipping txing and flipping
483     	 *	mode as the driver assumes txing is a faithful indicator of card
484     	 *	state
485     	 */
486     
487     	spin_lock_irqsave(&lp->lock, flags);
488     	
489     	/*
490     	 *	Avoid timer-based retransmission conflicts.
491     	 */
492     
493     	netif_stop_queue(dev);
494     
495     	do
496     	{
497     		int gp_start = 0x800 - (ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN);
498     		unsigned char *buf = skb->data;
499     
500     		lp->tx_pkt_start = gp_start;
501         		lp->collisions = 0;
502     
503         		lp->stats.tx_bytes += skb->len;
504     
505     		/*
506     		 *	Command mode with status cleared should [in theory]
507     		 *	mean no more interrupts can be pending on the card.
508     		 */
509     
510     		outb_p(AX_SYS, AX_CMD);
511     		inb_p(RX_STATUS);
512     		inb_p(TX_STATUS);
513     
514     		lp->loading = 1;
515     		lp->txing = 1;
516     
517     		/*
518     		 *	Turn interrupts back on while we spend a pleasant afternoon
519     		 *	loading bytes into the board
520     		 */
521     
522     		spin_unlock_irqrestore(&lp->lock, flags);
523     		
524     		outw(0x00, RX_BUF_CLR);		/* Set rx packet area to 0. */
525     		outw(gp_start, GP_LOW);		/* aim - packet will be loaded into buffer start */
526     		outsb(DATAPORT,buf,skb->len);	/* load buffer (usual thing each byte increments the pointer) */
527     		outw(gp_start, GP_LOW);		/* the board reuses the same register */
528     	
529     		if(lp->loading != 2)
530     		{
531     			outb(AX_XMIT, AX_CMD);		/* fire ... Trigger xmit.  */
532     			lp->loading=0;
533     			dev->trans_start = jiffies;
534     			if (el_debug > 2)
535     				printk(" queued xmit.\n");
536     			dev_kfree_skb (skb);
537     			return 0;
538     		}
539     		/* A receive upset our load, despite our best efforts */
540     		if(el_debug>2)
541     			printk("%s: burped during tx load.\n", dev->name);
542     		spin_lock_irqsave(&lp->lock, flags);
543     	}
544     	while(1);
545     
546     }
547     
548     
549     /**
550      * el_interrupt:
551      * @irq: Interrupt number
552      * @dev_id: The 3c501 that burped
553      * @regs: Register data (surplus to our requirements)
554      *
555      * Handle the ether interface interrupts. The 3c501 needs a lot more 
556      * hand holding than most cards. In paticular we get a transmit interrupt
557      * with a collision error because the board firmware isnt capable of rewinding
558      * its own transmit buffer pointers. It can however count to 16 for us.
559      *
560      * On the receive side the card is also very dumb. It has no buffering to
561      * speak of. We simply pull the packet out of its PIO buffer (which is slow)
562      * and queue it for the kernel. Then we reset the card for the next packet.
563      *
564      * We sometimes get suprise interrupts late both because the SMP IRQ delivery
565      * is message passing and because the card sometimes seems to deliver late. I
566      * think if it is part way through a receive and the mode is changed it carries
567      * on receiving and sends us an interrupt. We have to band aid all these cases
568      * to get a sensible 150kbytes/second performance. Even then you want a small
569      * TCP window.
570      */
571     
572     static void el_interrupt(int irq, void *dev_id, struct pt_regs *regs)
573     {
574     	struct net_device *dev = dev_id;
575     	struct net_local *lp;
576     	int ioaddr;
577     	int axsr;			/* Aux. status reg. */
578     
579     	ioaddr = dev->base_addr;
580     	lp = (struct net_local *)dev->priv;
581     
582     	spin_lock(&lp->lock);
583     	
584     	/*
585     	 *	What happened ?
586     	 */
587     
588     	axsr = inb(AX_STATUS);
589     
590     	/*
591     	 *	Log it
592     	 */
593     
594     	if (el_debug > 3)
595     		printk(KERN_DEBUG "%s: el_interrupt() aux=%#02x", dev->name, axsr);
596     
597             if(lp->loading==1 && !lp->txing)
598             	printk(KERN_WARNING "%s: Inconsistent state loading while not in tx\n",
599             		dev->name);
600     
601     	if (lp->txing)
602     	{
603     
604         		/*
605         		 *	Board in transmit mode. May be loading. If we are
606         		 *	loading we shouldn't have got this.
607         		 */
608     
609     		int txsr = inb(TX_STATUS);
610     
611     		if(lp->loading==1)
612     		{
613     			if(el_debug > 2)
614     			{
615     				printk(KERN_DEBUG "%s: Interrupt while loading [", dev->name);
616     				printk(" txsr=%02x gp=%04x rp=%04x]\n", txsr, inw(GP_LOW),inw(RX_LOW));
617     			}
618     			lp->loading=2;		/* Force a reload */
619     			spin_unlock(&lp->lock);
620     			return;
621     		}
622     
623     		if (el_debug > 6)
624     			printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),inw(RX_LOW));
625     
626     		if ((axsr & 0x80) && (txsr & TX_READY) == 0)
627     		{
628     			/*
629     			 *	FIXME: is there a logic to whether to keep on trying or
630     			 *	reset immediately ?
631     			 */
632     			if(el_debug>1)
633     				printk("%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
634     			  		" gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
635     			inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
636     			lp->txing = 0;
637     			netif_wake_queue(dev);
638     		}
639     		else if (txsr & TX_16COLLISIONS)
640     		{
641     			/*
642     			 *	Timed out
643     			 */
644     			if (el_debug)
645     				printk("%s: Transmit failed 16 times, Ethernet jammed?\n",dev->name);
646     			outb(AX_SYS, AX_CMD);
647     			lp->txing = 0;
648     			lp->stats.tx_aborted_errors++;
649     			netif_wake_queue(dev);
650     		}
651     		else if (txsr & TX_COLLISION)
652     		{
653     			/*
654     			 *	Retrigger xmit.
655     			 */
656     
657     			if (el_debug > 6)
658     				printk(" retransmitting after a collision.\n");
659     			/*
660     			 *	Poor little chip can't reset its own start pointer
661     			 */
662     
663     			outb(AX_SYS, AX_CMD);
664     			outw(lp->tx_pkt_start, GP_LOW);
665     			outb(AX_XMIT, AX_CMD);
666     			lp->stats.collisions++;
667     			spin_unlock(&lp->lock);
668     			return;
669     		}
670     		else
671     		{
672     			/*
673     			 *	It worked.. we will now fall through and receive
674     			 */
675     			lp->stats.tx_packets++;
676     			if (el_debug > 6)
677     				printk(" Tx succeeded %s\n",
678     		       			(txsr & TX_RDY) ? "." : "but tx is busy!");
679     			/*
680     			 *	This is safe the interrupt is atomic WRT itself.
681     			 */
682     
683     			lp->txing = 0;
684     			netif_wake_queue(dev);	/* In case more to transmit */
685     		}
686     	}
687     	else
688     	{
689         		/*
690         		 *	In receive mode.
691         		 */
692     
693     		int rxsr = inb(RX_STATUS);
694     		if (el_debug > 5)
695     			printk(" rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),inw(RX_LOW));
696     		/*
697     		 *	Just reading rx_status fixes most errors.
698     		 */
699     		if (rxsr & RX_MISSED)
700     			lp->stats.rx_missed_errors++;
701     		else if (rxsr & RX_RUNT)
702     		{	/* Handled to avoid board lock-up. */
703     			lp->stats.rx_length_errors++;
704     			if (el_debug > 5)
705     				printk(" runt.\n");
706     		}
707     		else if (rxsr & RX_GOOD)
708     		{
709     			/*
710     			 *	Receive worked.
711     			 */
712     			el_receive(dev);
713     		}
714     		else
715     		{
716     			/*
717     			 *	Nothing?  Something is broken!
718     			 */
719     			if (el_debug > 2)
720     				printk("%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
721     					dev->name, rxsr);
722     			el_reset(dev);
723     		}
724     		if (el_debug > 3)
725     			printk(".\n");
726     	}
727     
728     	/*
729     	 *	Move into receive mode
730     	 */
731     
732     	outb(AX_RX, AX_CMD);
733     	outw(0x00, RX_BUF_CLR);
734     	inb(RX_STATUS);		/* Be certain that interrupts are cleared. */
735     	inb(TX_STATUS);
736     	spin_unlock(&lp->lock);
737     	return;
738     }
739     
740     
741     /**
742      * el_receive:
743      * @dev: Device to pull the packets from
744      *
745      * We have a good packet. Well, not really "good", just mostly not broken.
746      * We must check everything to see if it is good. In paticular we occasionally
747      * get wild packet sizes from the card. If the packet seems sane we PIO it
748      * off the card and queue it for the protocol layers.
749      */
750     
751     static void el_receive(struct net_device *dev)
752     {
753     	struct net_local *lp = (struct net_local *)dev->priv;
754     	int ioaddr = dev->base_addr;
755     	int pkt_len;
756     	struct sk_buff *skb;
757     
758     	pkt_len = inw(RX_LOW);
759     
760     	if (el_debug > 4)
761     		printk(" el_receive %d.\n", pkt_len);
762     
763     	if ((pkt_len < 60)  ||  (pkt_len > 1536))
764     	{
765     		if (el_debug)
766     			printk("%s: bogus packet, length=%d\n", dev->name, pkt_len);
767     		lp->stats.rx_over_errors++;
768     		return;
769     	}
770     
771     	/*
772     	 *	Command mode so we can empty the buffer
773     	 */
774     
775     	outb(AX_SYS, AX_CMD);
776     	skb = dev_alloc_skb(pkt_len+2);
777     
778     	/*
779     	 *	Start of frame
780     	 */
781     
782     	outw(0x00, GP_LOW);
783     	if (skb == NULL)
784     	{
785     		printk("%s: Memory squeeze, dropping packet.\n", dev->name);
786     		lp->stats.rx_dropped++;
787     		return;
788     	}
789     	else
790     	{
791         		skb_reserve(skb,2);	/* Force 16 byte alignment */
792     		skb->dev = dev;
793     		/*
794     		 *	The read increments through the bytes. The interrupt
795     		 *	handler will fix the pointer when it returns to
796     		 *	receive mode.
797     		 */
798     		insb(DATAPORT, skb_put(skb,pkt_len), pkt_len);
799     		skb->protocol=eth_type_trans(skb,dev);
800     		netif_rx(skb);
801     		dev->last_rx = jiffies;
802     		lp->stats.rx_packets++;
803     		lp->stats.rx_bytes+=pkt_len;
804     	}
805     	return;
806     }
807     
808     /**
809      * el_reset: Reset a 3c501 card
810      * @dev: The 3c501 card about to get zapped
811      *
812      * Even resetting a 3c501 isnt simple. When you activate reset it loses all
813      * its configuration. You must hold the lock when doing this. The function
814      * cannot take the lock itself as it is callable from the irq handler.
815      */
816     
817     static void  el_reset(struct net_device *dev)
818     {
819     	struct net_local *lp = (struct net_local *)dev->priv;
820     	int ioaddr = dev->base_addr;
821     
822     	if (el_debug> 2)
823     		printk("3c501 reset...");
824     	outb(AX_RESET, AX_CMD);		/* Reset the chip */
825     	outb(AX_LOOP, AX_CMD);		/* Aux control, irq and loopback enabled */
826     	{
827     		int i;
828     		for (i = 0; i < 6; i++)	/* Set the station address. */
829     			outb(dev->dev_addr[i], ioaddr + i);
830     	}
831     
832     	outw(0, RX_BUF_CLR);		/* Set rx packet area to 0. */
833     	outb(TX_NORM, TX_CMD);		/* tx irq on done, collision */
834     	outb(RX_NORM, RX_CMD);		/* Set Rx commands. */
835     	inb(RX_STATUS);			/* Clear status. */
836     	inb(TX_STATUS);
837     	lp->txing = 0;
838     }
839     
840     /**
841      * el1_close:
842      * @dev: 3c501 card to shut down
843      *
844      * Close a 3c501 card. The IFF_UP flag has been cleared by the user via
845      * the SIOCSIFFLAGS ioctl. We stop any further transmissions being queued,
846      * and then disable the interrupts. Finally we reset the chip. The effects
847      * of the rest will be cleaned up by #el1_open. Always returns 0 indicating
848      * a success.
849      */
850      
851     static int el1_close(struct net_device *dev)
852     {
853     	int ioaddr = dev->base_addr;
854     
855     	if (el_debug > 2)
856     		printk("%s: Shutting down Ethernet card at %#x.\n", dev->name, ioaddr);
857     
858     	netif_stop_queue(dev);
859     	
860     	/*
861     	 *	Free and disable the IRQ.
862     	 */
863     
864     	free_irq(dev->irq, dev);
865     	outb(AX_RESET, AX_CMD);		/* Reset the chip */
866     
867     	return 0;
868     }
869     
870     /**
871      * el1_get_stats:
872      * @dev: The card to get the statistics for
873      *
874      * In smarter devices this function is needed to pull statistics off the
875      * board itself. The 3c501 has no hardware statistics. We maintain them all
876      * so they are by definition always up to date.
877      *
878      * Returns the statistics for the card from the card private data
879      */
880      
881     static struct net_device_stats *el1_get_stats(struct net_device *dev)
882     {
883     	struct net_local *lp = (struct net_local *)dev->priv;
884     	return &lp->stats;
885     }
886     
887     /**
888      * set_multicast_list:
889      * @dev: The device to adjust
890      *
891      * Set or clear the multicast filter for this adaptor to use the best-effort 
892      * filtering supported. The 3c501 supports only three modes of filtering.
893      * It always receives broadcasts and packets for itself. You can choose to
894      * optionally receive all packets, or all multicast packets on top of this.
895      */
896     
897     static void set_multicast_list(struct net_device *dev)
898     {
899     	int ioaddr = dev->base_addr;
900     
901     	if(dev->flags&IFF_PROMISC)
902     	{
903     		outb(RX_PROM, RX_CMD);
904     		inb(RX_STATUS);
905     	}
906     	else if (dev->mc_list || dev->flags&IFF_ALLMULTI)
907     	{
908     		outb(RX_MULT, RX_CMD);	/* Multicast or all multicast is the same */
909     		inb(RX_STATUS);		/* Clear status. */
910     	}
911     	else
912     	{
913     		outb(RX_NORM, RX_CMD);
914     		inb(RX_STATUS);
915     	}
916     }
917     
918     #ifdef MODULE
919     
920     static struct net_device dev_3c501 = {
921     	init:		el1_probe,
922     	base_addr:	0x280,
923     	irq:		5,
924     };
925     
926     static int io=0x280;
927     static int irq=5;
928     MODULE_PARM(io, "i");
929     MODULE_PARM(irq, "i");
930     MODULE_PARM_DESC(io, "EtherLink I/O base address");
931     MODULE_PARM_DESC(irq, "EtherLink IRQ number");
932     
933     /**
934      * init_module:
935      *
936      * When the driver is loaded as a module this function is called. We fake up
937      * a device structure with the base I/O and interrupt set as if it was being
938      * called from Space.c. This minimises the extra code that would otherwise
939      * be required.
940      *
941      * Returns 0 for success or -EIO if a card is not found. Returning an error
942      * here also causes the module to be unloaded
943      */
944      
945     int init_module(void)
946     {
947     	dev_3c501.irq=irq;
948     	dev_3c501.base_addr=io;
949     	if (register_netdev(&dev_3c501) != 0)
950     		return -EIO;
951     	return 0;
952     }
953     
954     /**
955      * cleanup_module:
956      * 
957      * The module is being unloaded. We unhook our network device from the system
958      * and then free up the resources we took when the card was found.
959      */
960      
961     void cleanup_module(void)
962     {
963     	/*
964     	 *	No need to check MOD_IN_USE, as sys_delete_module() checks.
965     	 */
966     
967     	unregister_netdev(&dev_3c501);
968     
969     	/*
970     	 *	Free up the private structure, or leak memory :-)
971     	 */
972     
973     	kfree(dev_3c501.priv);
974     	dev_3c501.priv = NULL;	/* gets re-allocated by el1_probe1 */
975     
976     	/*
977     	 *	If we don't do this, we can't re-insmod it later.
978     	 */
979     	release_region(dev_3c501.base_addr, EL1_IO_EXTENT);
980     }
981     
982     #endif /* MODULE */
983     
984     /*
985      * Local variables:
986      *  compile-command: "gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer  -m486 -c -o 3c501.o 3c501.c"
987      *  kept-new-versions: 5
988      * End:
989      */
990