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