blob: 4bee99ba7dbb7a6a35f62929e995d5c75c53e047 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 original 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 Fixed (again!) the missing interrupt locking on TX/RX shifting.
20 Alan Cox <Alan.Cox@linux.org>
21
22 Removed calls to init_etherdev since they are no longer needed, and
23 cleaned up modularization just a bit. The driver still allows only
24 the default address for cards when loaded as a module, but that's
25 really less braindead than anyone using a 3c501 board. :)
26 19950208 (invid@msen.com)
27
28 Added traps for interrupts hitting the window as we clear and TX load
29 the board. Now getting 150K/second FTP with a 3c501 card. Still playing
30 with a TX-TX optimisation to see if we can touch 180-200K/second as seems
31 theoretically maximum.
32 19950402 Alan Cox <Alan.Cox@linux.org>
Jeff Garzik6aa20a22006-09-13 13:24:59 -040033
34 Cleaned up for 2.3.x because we broke SMP now.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 20000208 Alan Cox <alan@redhat.com>
36
37 Check up pass for 2.5. Nothing significant changed
38 20021009 Alan Cox <alan@redhat.com>
39
Jeff Garzik6aa20a22006-09-13 13:24:59 -040040 Fixed zero fill corner case
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 20030104 Alan Cox <alan@redhat.com>
Jeff Garzik6aa20a22006-09-13 13:24:59 -040042
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 For the avoidance of doubt the "preferred form" of this code is one which
45 is in an open non patent encumbered format. Where cryptographic key signing
46 forms part of the process of creating an executable the information
47 including keys needed to generate an equivalently functional executable
48 are deemed to be part of the source code.
49
50*/
51
52
53/**
54 * DOC: 3c501 Card Notes
55 *
56 * Some notes on this thing if you have to hack it. [Alan]
57 *
58 * Some documentation is available from 3Com. Due to the boards age
59 * standard responses when you ask for this will range from 'be serious'
60 * to 'give it to a museum'. The documentation is incomplete and mostly
Jeff Garzik6aa20a22006-09-13 13:24:59 -040061 * of historical interest anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 * The basic system is a single buffer which can be used to receive or
64 * transmit a packet. A third command mode exists when you are setting
65 * things up.
66 *
67 * If it's transmitting it's not receiving and vice versa. In fact the
68 * time to get the board back into useful state after an operation is
69 * quite large.
70 *
71 * The driver works by keeping the board in receive mode waiting for a
72 * packet to arrive. When one arrives it is copied out of the buffer
73 * and delivered to the kernel. The card is reloaded and off we go.
74 *
75 * When transmitting lp->txing is set and the card is reset (from
76 * receive mode) [possibly losing a packet just received] to command
77 * mode. A packet is loaded and transmit mode triggered. The interrupt
78 * handler runs different code for transmit interrupts and can handle
79 * returning to receive mode or retransmissions (yes you have to help
80 * out with those too).
81 *
82 * DOC: Problems
Jeff Garzik6aa20a22006-09-13 13:24:59 -040083 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * There are a wide variety of undocumented error returns from the card
85 * and you basically have to kick the board and pray if they turn up. Most
86 * only occur under extreme load or if you do something the board doesn't
87 * like (eg touching a register at the wrong time).
88 *
89 * The driver is less efficient than it could be. It switches through
90 * receive mode even if more transmits are queued. If this worries you buy
91 * a real Ethernet card.
92 *
93 * The combination of slow receive restart and no real multicast
94 * filter makes the board unusable with a kernel compiled for IP
95 * multicasting in a real multicast environment. That's down to the board,
96 * but even with no multicast programs running a multicast IP kernel is
97 * in group 224.0.0.1 and you will therefore be listening to all multicasts.
98 * One nv conference running over that Ethernet and you can give up.
99 *
100 */
101
102#define DRV_NAME "3c501"
103#define DRV_VERSION "2002/10/09"
104
105
106static const char version[] =
107 DRV_NAME ".c: " DRV_VERSION " Alan Cox (alan@redhat.com).\n";
108
109/*
110 * Braindamage remaining:
111 * The 3c501 board.
112 */
113
114#include <linux/module.h>
115
116#include <linux/kernel.h>
117#include <linux/fcntl.h>
118#include <linux/ioport.h>
119#include <linux/interrupt.h>
120#include <linux/slab.h>
121#include <linux/string.h>
122#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#include <linux/spinlock.h>
124#include <linux/ethtool.h>
125#include <linux/delay.h>
126#include <linux/bitops.h>
127
128#include <asm/uaccess.h>
129#include <asm/io.h>
130
131#include <linux/netdevice.h>
132#include <linux/etherdevice.h>
133#include <linux/skbuff.h>
134#include <linux/init.h>
135
136#include "3c501.h"
137
138/*
139 * The boilerplate probe code.
140 */
141
142static int io=0x280;
143static int irq=5;
144static int mem_start;
145
146/**
147 * el1_probe: - probe for a 3c501
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400148 * @dev: The device structure passed in to probe.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
150 * This can be called from two places. The network layer will probe using
151 * a device structure passed in with the probe information completed. For a
152 * modular driver we use #init_module to fill in our own structure and probe
153 * for it.
154 *
155 * Returns 0 on success. ENXIO if asked not to probe and ENODEV if asked to
156 * probe and failing to find anything.
157 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159struct net_device * __init el1_probe(int unit)
160{
161 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
162 static unsigned ports[] = { 0x280, 0x300, 0};
163 unsigned *port;
164 int err = 0;
165
166 if (!dev)
167 return ERR_PTR(-ENOMEM);
168
169 if (unit >= 0) {
170 sprintf(dev->name, "eth%d", unit);
171 netdev_boot_setup_check(dev);
172 io = dev->base_addr;
173 irq = dev->irq;
174 mem_start = dev->mem_start & 7;
175 }
176
177 SET_MODULE_OWNER(dev);
178
179 if (io > 0x1ff) { /* Check a single specified location. */
180 err = el1_probe1(dev, io);
181 } else if (io != 0) {
182 err = -ENXIO; /* Don't probe at all. */
183 } else {
184 for (port = ports; *port && el1_probe1(dev, *port); port++)
185 ;
186 if (!*port)
187 err = -ENODEV;
188 }
189 if (err)
190 goto out;
191 err = register_netdev(dev);
192 if (err)
193 goto out1;
194 return dev;
195out1:
196 release_region(dev->base_addr, EL1_IO_EXTENT);
197out:
198 free_netdev(dev);
199 return ERR_PTR(err);
200}
201
202/**
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400203 * el1_probe1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * @dev: The device structure to use
205 * @ioaddr: An I/O address to probe at.
206 *
207 * The actual probe. This is iterated over by #el1_probe in order to
208 * check all the applicable device locations.
209 *
210 * Returns 0 for a success, in which case the device is activated,
211 * EAGAIN if the IRQ is in use by another driver, and ENODEV if the
212 * board cannot be found.
213 */
214
215static int __init el1_probe1(struct net_device *dev, int ioaddr)
216{
217 struct net_local *lp;
218 const char *mname; /* Vendor name */
219 unsigned char station_addr[6];
220 int autoirq = 0;
221 int i;
222
223 /*
224 * Reserve I/O resource for exclusive use by this driver
225 */
226
227 if (!request_region(ioaddr, EL1_IO_EXTENT, DRV_NAME))
228 return -ENODEV;
229
230 /*
231 * Read the station address PROM data from the special port.
232 */
233
234 for (i = 0; i < 6; i++)
235 {
236 outw(i, ioaddr + EL1_DATAPTR);
237 station_addr[i] = inb(ioaddr + EL1_SAPROM);
238 }
239 /*
240 * Check the first three octets of the S.A. for 3Com's prefix, or
241 * for the Sager NP943 prefix.
242 */
243
244 if (station_addr[0] == 0x02 && station_addr[1] == 0x60
245 && station_addr[2] == 0x8c)
246 {
247 mname = "3c501";
248 } else if (station_addr[0] == 0x00 && station_addr[1] == 0x80
249 && station_addr[2] == 0xC8)
250 {
251 mname = "NP943";
252 }
253 else {
254 release_region(ioaddr, EL1_IO_EXTENT);
255 return -ENODEV;
256 }
257
258 /*
259 * We auto-IRQ by shutting off the interrupt line and letting it float
260 * high.
261 */
262
263 dev->irq = irq;
264
265 if (dev->irq < 2)
266 {
267 unsigned long irq_mask;
268
269 irq_mask = probe_irq_on();
270 inb(RX_STATUS); /* Clear pending interrupts. */
271 inb(TX_STATUS);
272 outb(AX_LOOP + 1, AX_CMD);
273
274 outb(0x00, AX_CMD);
275
276 mdelay(20);
277 autoirq = probe_irq_off(irq_mask);
278
279 if (autoirq == 0)
280 {
281 printk(KERN_WARNING "%s probe at %#x failed to detect IRQ line.\n",
282 mname, ioaddr);
283 release_region(ioaddr, EL1_IO_EXTENT);
284 return -EAGAIN;
285 }
286 }
287
288 outb(AX_RESET+AX_LOOP, AX_CMD); /* Loopback mode. */
289 dev->base_addr = ioaddr;
290 memcpy(dev->dev_addr, station_addr, ETH_ALEN);
291
292 if (mem_start & 0xf)
293 el_debug = mem_start & 0x7;
294 if (autoirq)
295 dev->irq = autoirq;
296
297 printk(KERN_INFO "%s: %s EtherLink at %#lx, using %sIRQ %d.\n", dev->name, mname, dev->base_addr,
298 autoirq ? "auto":"assigned ", dev->irq);
299
300#ifdef CONFIG_IP_MULTICAST
301 printk(KERN_WARNING "WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
302#endif
303
304 if (el_debug)
305 printk(KERN_DEBUG "%s", version);
306
307 memset(dev->priv, 0, sizeof(struct net_local));
308 lp = netdev_priv(dev);
309 spin_lock_init(&lp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
312 * The EL1-specific entries in the device structure.
313 */
314
315 dev->open = &el_open;
316 dev->hard_start_xmit = &el_start_xmit;
317 dev->tx_timeout = &el_timeout;
318 dev->watchdog_timeo = HZ;
319 dev->stop = &el1_close;
320 dev->get_stats = &el1_get_stats;
321 dev->set_multicast_list = &set_multicast_list;
322 dev->ethtool_ops = &netdev_ethtool_ops;
323 return 0;
324}
325
326/**
327 * el1_open:
328 * @dev: device that is being opened
329 *
330 * When an ifconfig is issued which changes the device flags to include
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400331 * IFF_UP this function is called. It is only called when the change
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * occurs, not when the interface remains up. #el1_close will be called
333 * when it goes down.
334 *
335 * Returns 0 for a successful open, or -EAGAIN if someone has run off
336 * with our interrupt line.
337 */
338
339static int el_open(struct net_device *dev)
340{
341 int retval;
342 int ioaddr = dev->base_addr;
343 struct net_local *lp = netdev_priv(dev);
344 unsigned long flags;
345
346 if (el_debug > 2)
347 printk(KERN_DEBUG "%s: Doing el_open()...", dev->name);
348
349 if ((retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev)))
350 return retval;
351
352 spin_lock_irqsave(&lp->lock, flags);
353 el_reset(dev);
354 spin_unlock_irqrestore(&lp->lock, flags);
355
356 lp->txing = 0; /* Board in RX mode */
357 outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
358 netif_start_queue(dev);
359 return 0;
360}
361
362/**
363 * el_timeout:
364 * @dev: The 3c501 card that has timed out
365 *
366 * Attempt to restart the board. This is basically a mixture of extreme
367 * violence and prayer
368 *
369 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void el_timeout(struct net_device *dev)
372{
373 struct net_local *lp = netdev_priv(dev);
374 int ioaddr = dev->base_addr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (el_debug)
377 printk (KERN_DEBUG "%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
378 dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
379 lp->stats.tx_errors++;
380 outb(TX_NORM, TX_CMD);
381 outb(RX_NORM, RX_CMD);
382 outb(AX_OFF, AX_CMD); /* Just trigger a false interrupt. */
383 outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
384 lp->txing = 0; /* Ripped back in to RX */
385 netif_wake_queue(dev);
386}
387
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/**
390 * el_start_xmit:
391 * @skb: The packet that is queued to be sent
392 * @dev: The 3c501 card we want to throw it down
393 *
394 * Attempt to send a packet to a 3c501 card. There are some interesting
395 * catches here because the 3c501 is an extremely old and therefore
396 * stupid piece of technology.
397 *
398 * If we are handling an interrupt on the other CPU we cannot load a packet
399 * as we may still be attempting to retrieve the last RX packet buffer.
400 *
401 * When a transmit times out we dump the card into control mode and just
402 * start again. It happens enough that it isnt worth logging.
403 *
404 * We avoid holding the spin locks when doing the packet load to the board.
405 * The device is very slow, and its DMA mode is even slower. If we held the
406 * lock while loading 1500 bytes onto the controller we would drop a lot of
407 * serial port characters. This requires we do extra locking, but we have
408 * no real choice.
409 */
410
411static int el_start_xmit(struct sk_buff *skb, struct net_device *dev)
412{
413 struct net_local *lp = netdev_priv(dev);
414 int ioaddr = dev->base_addr;
415 unsigned long flags;
416
417 /*
418 * Avoid incoming interrupts between us flipping txing and flipping
419 * mode as the driver assumes txing is a faithful indicator of card
420 * state
421 */
422
423 spin_lock_irqsave(&lp->lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
426 * Avoid timer-based retransmission conflicts.
427 */
428
429 netif_stop_queue(dev);
430
431 do
432 {
433 int len = skb->len;
434 int pad = 0;
435 int gp_start;
436 unsigned char *buf = skb->data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (len < ETH_ZLEN)
439 pad = ETH_ZLEN - len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 gp_start = 0x800 - ( len + pad );
442
443 lp->tx_pkt_start = gp_start;
444 lp->collisions = 0;
445
446 lp->stats.tx_bytes += skb->len;
447
448 /*
449 * Command mode with status cleared should [in theory]
450 * mean no more interrupts can be pending on the card.
451 */
452
453 outb_p(AX_SYS, AX_CMD);
454 inb_p(RX_STATUS);
455 inb_p(TX_STATUS);
456
457 lp->loading = 1;
458 lp->txing = 1;
459
460 /*
461 * Turn interrupts back on while we spend a pleasant afternoon
462 * loading bytes into the board
463 */
464
465 spin_unlock_irqrestore(&lp->lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 outw(0x00, RX_BUF_CLR); /* Set rx packet area to 0. */
468 outw(gp_start, GP_LOW); /* aim - packet will be loaded into buffer start */
469 outsb(DATAPORT,buf,len); /* load buffer (usual thing each byte increments the pointer) */
470 if (pad) {
471 while(pad--) /* Zero fill buffer tail */
472 outb(0, DATAPORT);
473 }
474 outw(gp_start, GP_LOW); /* the board reuses the same register */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if(lp->loading != 2)
477 {
478 outb(AX_XMIT, AX_CMD); /* fire ... Trigger xmit. */
479 lp->loading=0;
480 dev->trans_start = jiffies;
481 if (el_debug > 2)
482 printk(KERN_DEBUG " queued xmit.\n");
483 dev_kfree_skb (skb);
484 return 0;
485 }
486 /* A receive upset our load, despite our best efforts */
487 if(el_debug>2)
488 printk(KERN_DEBUG "%s: burped during tx load.\n", dev->name);
489 spin_lock_irqsave(&lp->lock, flags);
490 }
491 while(1);
492
493}
494
495/**
496 * el_interrupt:
497 * @irq: Interrupt number
498 * @dev_id: The 3c501 that burped
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400500 * Handle the ether interface interrupts. The 3c501 needs a lot more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * hand holding than most cards. In particular we get a transmit interrupt
502 * with a collision error because the board firmware isnt capable of rewinding
503 * its own transmit buffer pointers. It can however count to 16 for us.
504 *
505 * On the receive side the card is also very dumb. It has no buffering to
506 * speak of. We simply pull the packet out of its PIO buffer (which is slow)
507 * and queue it for the kernel. Then we reset the card for the next packet.
508 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200509 * We sometimes get surprise interrupts late both because the SMP IRQ delivery
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * is message passing and because the card sometimes seems to deliver late. I
511 * think if it is part way through a receive and the mode is changed it carries
512 * on receiving and sends us an interrupt. We have to band aid all these cases
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200513 * to get a sensible 150kBytes/second performance. Even then you want a small
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * TCP window.
515 */
516
David Howells7d12e782006-10-05 14:55:46 +0100517static irqreturn_t el_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct net_device *dev = dev_id;
520 struct net_local *lp;
521 int ioaddr;
522 int axsr; /* Aux. status reg. */
523
524 ioaddr = dev->base_addr;
525 lp = netdev_priv(dev);
526
527 spin_lock(&lp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * What happened ?
531 */
532
533 axsr = inb(AX_STATUS);
534
535 /*
536 * Log it
537 */
538
539 if (el_debug > 3)
540 printk(KERN_DEBUG "%s: el_interrupt() aux=%#02x", dev->name, axsr);
541
542 if(lp->loading==1 && !lp->txing)
543 printk(KERN_WARNING "%s: Inconsistent state loading while not in tx\n",
544 dev->name);
545
546 if (lp->txing)
547 {
548
549 /*
550 * Board in transmit mode. May be loading. If we are
551 * loading we shouldn't have got this.
552 */
553
554 int txsr = inb(TX_STATUS);
555
556 if(lp->loading==1)
557 {
558 if(el_debug > 2)
559 {
560 printk(KERN_DEBUG "%s: Interrupt while loading [", dev->name);
561 printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x]\n", txsr, inw(GP_LOW),inw(RX_LOW));
562 }
563 lp->loading=2; /* Force a reload */
564 spin_unlock(&lp->lock);
565 goto out;
566 }
567
568 if (el_debug > 6)
569 printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),inw(RX_LOW));
570
571 if ((axsr & 0x80) && (txsr & TX_READY) == 0)
572 {
573 /*
574 * FIXME: is there a logic to whether to keep on trying or
575 * reset immediately ?
576 */
577 if(el_debug>1)
578 printk(KERN_DEBUG "%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
579 " gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
580 inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
581 lp->txing = 0;
582 netif_wake_queue(dev);
583 }
584 else if (txsr & TX_16COLLISIONS)
585 {
586 /*
587 * Timed out
588 */
589 if (el_debug)
590 printk (KERN_DEBUG "%s: Transmit failed 16 times, Ethernet jammed?\n",dev->name);
591 outb(AX_SYS, AX_CMD);
592 lp->txing = 0;
593 lp->stats.tx_aborted_errors++;
594 netif_wake_queue(dev);
595 }
596 else if (txsr & TX_COLLISION)
597 {
598 /*
599 * Retrigger xmit.
600 */
601
602 if (el_debug > 6)
603 printk(KERN_DEBUG " retransmitting after a collision.\n");
604 /*
605 * Poor little chip can't reset its own start pointer
606 */
607
608 outb(AX_SYS, AX_CMD);
609 outw(lp->tx_pkt_start, GP_LOW);
610 outb(AX_XMIT, AX_CMD);
611 lp->stats.collisions++;
612 spin_unlock(&lp->lock);
613 goto out;
614 }
615 else
616 {
617 /*
618 * It worked.. we will now fall through and receive
619 */
620 lp->stats.tx_packets++;
621 if (el_debug > 6)
622 printk(KERN_DEBUG " Tx succeeded %s\n",
623 (txsr & TX_RDY) ? "." : "but tx is busy!");
624 /*
625 * This is safe the interrupt is atomic WRT itself.
626 */
627
628 lp->txing = 0;
629 netif_wake_queue(dev); /* In case more to transmit */
630 }
631 }
632 else
633 {
634 /*
635 * In receive mode.
636 */
637
638 int rxsr = inb(RX_STATUS);
639 if (el_debug > 5)
640 printk(KERN_DEBUG " rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),inw(RX_LOW));
641 /*
642 * Just reading rx_status fixes most errors.
643 */
644 if (rxsr & RX_MISSED)
645 lp->stats.rx_missed_errors++;
646 else if (rxsr & RX_RUNT)
647 { /* Handled to avoid board lock-up. */
648 lp->stats.rx_length_errors++;
649 if (el_debug > 5)
650 printk(KERN_DEBUG " runt.\n");
651 }
652 else if (rxsr & RX_GOOD)
653 {
654 /*
655 * Receive worked.
656 */
657 el_receive(dev);
658 }
659 else
660 {
661 /*
662 * Nothing? Something is broken!
663 */
664 if (el_debug > 2)
665 printk(KERN_DEBUG "%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
666 dev->name, rxsr);
667 el_reset(dev);
668 }
669 if (el_debug > 3)
670 printk(KERN_DEBUG ".\n");
671 }
672
673 /*
674 * Move into receive mode
675 */
676
677 outb(AX_RX, AX_CMD);
678 outw(0x00, RX_BUF_CLR);
679 inb(RX_STATUS); /* Be certain that interrupts are cleared. */
680 inb(TX_STATUS);
681 spin_unlock(&lp->lock);
682out:
683 return IRQ_HANDLED;
684}
685
686
687/**
688 * el_receive:
689 * @dev: Device to pull the packets from
690 *
691 * We have a good packet. Well, not really "good", just mostly not broken.
692 * We must check everything to see if it is good. In particular we occasionally
693 * get wild packet sizes from the card. If the packet seems sane we PIO it
694 * off the card and queue it for the protocol layers.
695 */
696
697static void el_receive(struct net_device *dev)
698{
699 struct net_local *lp = netdev_priv(dev);
700 int ioaddr = dev->base_addr;
701 int pkt_len;
702 struct sk_buff *skb;
703
704 pkt_len = inw(RX_LOW);
705
706 if (el_debug > 4)
707 printk(KERN_DEBUG " el_receive %d.\n", pkt_len);
708
709 if ((pkt_len < 60) || (pkt_len > 1536))
710 {
711 if (el_debug)
712 printk(KERN_DEBUG "%s: bogus packet, length=%d\n", dev->name, pkt_len);
713 lp->stats.rx_over_errors++;
714 return;
715 }
716
717 /*
718 * Command mode so we can empty the buffer
719 */
720
721 outb(AX_SYS, AX_CMD);
722 skb = dev_alloc_skb(pkt_len+2);
723
724 /*
725 * Start of frame
726 */
727
728 outw(0x00, GP_LOW);
729 if (skb == NULL)
730 {
731 printk(KERN_INFO "%s: Memory squeeze, dropping packet.\n", dev->name);
732 lp->stats.rx_dropped++;
733 return;
734 }
735 else
736 {
737 skb_reserve(skb,2); /* Force 16 byte alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
739 * The read increments through the bytes. The interrupt
740 * handler will fix the pointer when it returns to
741 * receive mode.
742 */
743 insb(DATAPORT, skb_put(skb,pkt_len), pkt_len);
744 skb->protocol=eth_type_trans(skb,dev);
745 netif_rx(skb);
746 dev->last_rx = jiffies;
747 lp->stats.rx_packets++;
748 lp->stats.rx_bytes+=pkt_len;
749 }
750 return;
751}
752
753/**
754 * el_reset: Reset a 3c501 card
755 * @dev: The 3c501 card about to get zapped
756 *
757 * Even resetting a 3c501 isnt simple. When you activate reset it loses all
758 * its configuration. You must hold the lock when doing this. The function
759 * cannot take the lock itself as it is callable from the irq handler.
760 */
761
762static void el_reset(struct net_device *dev)
763{
764 struct net_local *lp = netdev_priv(dev);
765 int ioaddr = dev->base_addr;
766
767 if (el_debug> 2)
768 printk(KERN_INFO "3c501 reset...");
769 outb(AX_RESET, AX_CMD); /* Reset the chip */
770 outb(AX_LOOP, AX_CMD); /* Aux control, irq and loopback enabled */
771 {
772 int i;
773 for (i = 0; i < 6; i++) /* Set the station address. */
774 outb(dev->dev_addr[i], ioaddr + i);
775 }
776
777 outw(0, RX_BUF_CLR); /* Set rx packet area to 0. */
778 outb(TX_NORM, TX_CMD); /* tx irq on done, collision */
779 outb(RX_NORM, RX_CMD); /* Set Rx commands. */
780 inb(RX_STATUS); /* Clear status. */
781 inb(TX_STATUS);
782 lp->txing = 0;
783}
784
785/**
786 * el1_close:
787 * @dev: 3c501 card to shut down
788 *
789 * Close a 3c501 card. The IFF_UP flag has been cleared by the user via
790 * the SIOCSIFFLAGS ioctl. We stop any further transmissions being queued,
791 * and then disable the interrupts. Finally we reset the chip. The effects
792 * of the rest will be cleaned up by #el1_open. Always returns 0 indicating
793 * a success.
794 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static int el1_close(struct net_device *dev)
797{
798 int ioaddr = dev->base_addr;
799
800 if (el_debug > 2)
801 printk(KERN_INFO "%s: Shutting down Ethernet card at %#x.\n", dev->name, ioaddr);
802
803 netif_stop_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
806 * Free and disable the IRQ.
807 */
808
809 free_irq(dev->irq, dev);
810 outb(AX_RESET, AX_CMD); /* Reset the chip */
811
812 return 0;
813}
814
815/**
816 * el1_get_stats:
817 * @dev: The card to get the statistics for
818 *
819 * In smarter devices this function is needed to pull statistics off the
820 * board itself. The 3c501 has no hardware statistics. We maintain them all
821 * so they are by definition always up to date.
822 *
823 * Returns the statistics for the card from the card private data
824 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static struct net_device_stats *el1_get_stats(struct net_device *dev)
827{
828 struct net_local *lp = netdev_priv(dev);
829 return &lp->stats;
830}
831
832/**
833 * set_multicast_list:
834 * @dev: The device to adjust
835 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400836 * Set or clear the multicast filter for this adaptor to use the best-effort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 * filtering supported. The 3c501 supports only three modes of filtering.
838 * It always receives broadcasts and packets for itself. You can choose to
839 * optionally receive all packets, or all multicast packets on top of this.
840 */
841
842static void set_multicast_list(struct net_device *dev)
843{
844 int ioaddr = dev->base_addr;
845
846 if(dev->flags&IFF_PROMISC)
847 {
848 outb(RX_PROM, RX_CMD);
849 inb(RX_STATUS);
850 }
851 else if (dev->mc_list || dev->flags&IFF_ALLMULTI)
852 {
853 outb(RX_MULT, RX_CMD); /* Multicast or all multicast is the same */
854 inb(RX_STATUS); /* Clear status. */
855 }
856 else
857 {
858 outb(RX_NORM, RX_CMD);
859 inb(RX_STATUS);
860 }
861}
862
863
864static void netdev_get_drvinfo(struct net_device *dev,
865 struct ethtool_drvinfo *info)
866{
867 strcpy(info->driver, DRV_NAME);
868 strcpy(info->version, DRV_VERSION);
869 sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
870}
871
872static u32 netdev_get_msglevel(struct net_device *dev)
873{
874 return debug;
875}
876
877static void netdev_set_msglevel(struct net_device *dev, u32 level)
878{
879 debug = level;
880}
881
Jeff Garzik7282d492006-09-13 14:30:00 -0400882static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .get_drvinfo = netdev_get_drvinfo,
884 .get_msglevel = netdev_get_msglevel,
885 .set_msglevel = netdev_set_msglevel,
886};
887
888#ifdef MODULE
889
890static struct net_device *dev_3c501;
891
892module_param(io, int, 0);
893module_param(irq, int, 0);
894MODULE_PARM_DESC(io, "EtherLink I/O base address");
895MODULE_PARM_DESC(irq, "EtherLink IRQ number");
896
897/**
898 * init_module:
899 *
900 * When the driver is loaded as a module this function is called. We fake up
901 * a device structure with the base I/O and interrupt set as if it were being
902 * called from Space.c. This minimises the extra code that would otherwise
903 * be required.
904 *
905 * Returns 0 for success or -EIO if a card is not found. Returning an error
906 * here also causes the module to be unloaded
907 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400908
Randy Dunlap96e672c2006-06-10 13:33:48 -0700909int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 dev_3c501 = el1_probe(-1);
912 if (IS_ERR(dev_3c501))
913 return PTR_ERR(dev_3c501);
914 return 0;
915}
916
917/**
918 * cleanup_module:
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400919 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * The module is being unloaded. We unhook our network device from the system
921 * and then free up the resources we took when the card was found.
922 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400923
Al Viroafc8eb42006-06-14 18:50:53 -0400924void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 struct net_device *dev = dev_3c501;
927 unregister_netdev(dev);
928 release_region(dev->base_addr, EL1_IO_EXTENT);
929 free_netdev(dev);
930}
931
932#endif /* MODULE */
933
934MODULE_AUTHOR("Donald Becker, Alan Cox");
935MODULE_DESCRIPTION("Support for the ancient 3Com 3c501 ethernet card");
936MODULE_LICENSE("GPL");
937