blob: 3ea42ff176577900c734b5e9d7bbd67973a1d324 [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.
Alan Cox113aa832008-10-13 19:01:08 -070020 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
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.
Alan Cox113aa832008-10-13 19:01:08 -070032 19950402 Alan Cox <alan@lxorguk.ukuu.org.uk>
Jeff Garzik6aa20a22006-09-13 13:24:59 -040033
34 Cleaned up for 2.3.x because we broke SMP now.
Alan Cox113aa832008-10-13 19:01:08 -070035 20000208 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 Check up pass for 2.5. Nothing significant changed
Alan Cox113aa832008-10-13 19:01:08 -070038 20021009 Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jeff Garzik6aa20a22006-09-13 13:24:59 -040040 Fixed zero fill corner case
Alan Cox113aa832008-10-13 19:01:08 -070041 20030104 Alan Cox <alan@lxorguk.ukuu.org.uk>
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[] =
Alan Cox113aa832008-10-13 19:01:08 -0700107 DRV_NAME ".c: " DRV_VERSION " Alan Cox (alan@lxorguk.ukuu.org.uk).\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <linux/string.h>
121#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/spinlock.h>
123#include <linux/ethtool.h>
124#include <linux/delay.h>
125#include <linux/bitops.h>
126
127#include <asm/uaccess.h>
128#include <asm/io.h>
129
130#include <linux/netdevice.h>
131#include <linux/etherdevice.h>
132#include <linux/skbuff.h>
133#include <linux/init.h>
134
135#include "3c501.h"
136
137/*
138 * The boilerplate probe code.
139 */
140
Alan Coxa35f5de2007-11-19 15:02:32 +0000141static int io = 0x280;
142static int irq = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int mem_start;
144
145/**
146 * el1_probe: - probe for a 3c501
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400147 * @dev: The device structure passed in to probe.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
149 * This can be called from two places. The network layer will probe using
150 * a device structure passed in with the probe information completed. For a
151 * modular driver we use #init_module to fill in our own structure and probe
152 * for it.
153 *
154 * Returns 0 on success. ENXIO if asked not to probe and ENODEV if asked to
155 * probe and failing to find anything.
156 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158struct net_device * __init el1_probe(int unit)
159{
160 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
161 static unsigned ports[] = { 0x280, 0x300, 0};
162 unsigned *port;
163 int err = 0;
164
165 if (!dev)
166 return ERR_PTR(-ENOMEM);
167
168 if (unit >= 0) {
169 sprintf(dev->name, "eth%d", unit);
170 netdev_boot_setup_check(dev);
171 io = dev->base_addr;
172 irq = dev->irq;
173 mem_start = dev->mem_start & 7;
174 }
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (io > 0x1ff) { /* Check a single specified location. */
177 err = el1_probe1(dev, io);
178 } else if (io != 0) {
179 err = -ENXIO; /* Don't probe at all. */
180 } else {
181 for (port = ports; *port && el1_probe1(dev, *port); port++)
182 ;
183 if (!*port)
184 err = -ENODEV;
185 }
186 if (err)
187 goto out;
188 err = register_netdev(dev);
189 if (err)
190 goto out1;
191 return dev;
192out1:
193 release_region(dev->base_addr, EL1_IO_EXTENT);
194out:
195 free_netdev(dev);
196 return ERR_PTR(err);
197}
198
Stephen Hemminger878f6482009-01-09 13:01:11 +0000199static const struct net_device_ops el_netdev_ops = {
200 .ndo_open = el_open,
201 .ndo_stop = el1_close,
202 .ndo_start_xmit = el_start_xmit,
203 .ndo_tx_timeout = el_timeout,
204 .ndo_set_multicast_list = set_multicast_list,
205 .ndo_change_mtu = eth_change_mtu,
206 .ndo_set_mac_address = eth_mac_addr,
207 .ndo_validate_addr = eth_validate_addr,
208};
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/**
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400211 * el1_probe1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * @dev: The device structure to use
213 * @ioaddr: An I/O address to probe at.
214 *
215 * The actual probe. This is iterated over by #el1_probe in order to
216 * check all the applicable device locations.
217 *
218 * Returns 0 for a success, in which case the device is activated,
219 * EAGAIN if the IRQ is in use by another driver, and ENODEV if the
220 * board cannot be found.
221 */
222
223static int __init el1_probe1(struct net_device *dev, int ioaddr)
224{
225 struct net_local *lp;
226 const char *mname; /* Vendor name */
227 unsigned char station_addr[6];
228 int autoirq = 0;
229 int i;
230
231 /*
232 * Reserve I/O resource for exclusive use by this driver
233 */
234
235 if (!request_region(ioaddr, EL1_IO_EXTENT, DRV_NAME))
236 return -ENODEV;
237
238 /*
239 * Read the station address PROM data from the special port.
240 */
241
Alan Coxa35f5de2007-11-19 15:02:32 +0000242 for (i = 0; i < 6; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 outw(i, ioaddr + EL1_DATAPTR);
244 station_addr[i] = inb(ioaddr + EL1_SAPROM);
245 }
246 /*
247 * Check the first three octets of the S.A. for 3Com's prefix, or
248 * for the Sager NP943 prefix.
249 */
250
Joe Perches8e95a202009-12-03 07:58:21 +0000251 if (station_addr[0] == 0x02 && station_addr[1] == 0x60 &&
252 station_addr[2] == 0x8c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 mname = "3c501";
Joe Perches8e95a202009-12-03 07:58:21 +0000254 else if (station_addr[0] == 0x00 && station_addr[1] == 0x80 &&
255 station_addr[2] == 0xC8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 mname = "NP943";
Alan Coxa35f5de2007-11-19 15:02:32 +0000257 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 release_region(ioaddr, EL1_IO_EXTENT);
259 return -ENODEV;
260 }
261
262 /*
Alan Coxa35f5de2007-11-19 15:02:32 +0000263 * We auto-IRQ by shutting off the interrupt line and letting it
264 * float high.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266
267 dev->irq = irq;
268
Alan Coxa35f5de2007-11-19 15:02:32 +0000269 if (dev->irq < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned long irq_mask;
271
272 irq_mask = probe_irq_on();
273 inb(RX_STATUS); /* Clear pending interrupts. */
274 inb(TX_STATUS);
275 outb(AX_LOOP + 1, AX_CMD);
276
277 outb(0x00, AX_CMD);
278
279 mdelay(20);
280 autoirq = probe_irq_off(irq_mask);
281
Alan Coxa35f5de2007-11-19 15:02:32 +0000282 if (autoirq == 0) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000283 pr_warning("%s probe at %#x failed to detect IRQ line.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 mname, ioaddr);
285 release_region(ioaddr, EL1_IO_EXTENT);
286 return -EAGAIN;
287 }
288 }
289
290 outb(AX_RESET+AX_LOOP, AX_CMD); /* Loopback mode. */
291 dev->base_addr = ioaddr;
292 memcpy(dev->dev_addr, station_addr, ETH_ALEN);
293
294 if (mem_start & 0xf)
295 el_debug = mem_start & 0x7;
296 if (autoirq)
297 dev->irq = autoirq;
298
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000299 pr_info("%s: %s EtherLink at %#lx, using %sIRQ %d.\n",
Alan Coxa35f5de2007-11-19 15:02:32 +0000300 dev->name, mname, dev->base_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 autoirq ? "auto":"assigned ", dev->irq);
302
303#ifdef CONFIG_IP_MULTICAST
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000304 pr_warning("WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif
306
307 if (el_debug)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000308 pr_debug("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 lp = netdev_priv(dev);
Wang Chen454d7c92008-11-12 23:37:49 -0800311 memset(lp, 0, sizeof(struct net_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_lock_init(&lp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /*
315 * The EL1-specific entries in the device structure.
316 */
317
Stephen Hemminger878f6482009-01-09 13:01:11 +0000318 dev->netdev_ops = &el_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 dev->ethtool_ops = &netdev_ethtool_ops;
321 return 0;
322}
323
324/**
325 * el1_open:
326 * @dev: device that is being opened
327 *
328 * When an ifconfig is issued which changes the device flags to include
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400329 * IFF_UP this function is called. It is only called when the change
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * occurs, not when the interface remains up. #el1_close will be called
331 * when it goes down.
332 *
333 * Returns 0 for a successful open, or -EAGAIN if someone has run off
334 * with our interrupt line.
335 */
336
337static int el_open(struct net_device *dev)
338{
339 int retval;
340 int ioaddr = dev->base_addr;
341 struct net_local *lp = netdev_priv(dev);
342 unsigned long flags;
343
344 if (el_debug > 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000345 pr_debug("%s: Doing el_open()...\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Joe Perchesa0607fd2009-11-18 23:29:17 -0800347 retval = request_irq(dev->irq, el_interrupt, 0, dev->name, dev);
Alan Coxa35f5de2007-11-19 15:02:32 +0000348 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return retval;
350
351 spin_lock_irqsave(&lp->lock, flags);
352 el_reset(dev);
353 spin_unlock_irqrestore(&lp->lock, flags);
354
355 lp->txing = 0; /* Board in RX mode */
356 outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
357 netif_start_queue(dev);
358 return 0;
359}
360
361/**
362 * el_timeout:
363 * @dev: The 3c501 card that has timed out
364 *
365 * Attempt to restart the board. This is basically a mixture of extreme
366 * violence and prayer
367 *
368 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static void el_timeout(struct net_device *dev)
371{
372 struct net_local *lp = netdev_priv(dev);
373 int ioaddr = dev->base_addr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (el_debug)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000376 pr_debug("%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
Alan Coxa35f5de2007-11-19 15:02:32 +0000377 dev->name, inb(TX_STATUS),
378 inb(AX_STATUS), inb(RX_STATUS));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700379 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 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
Stephen Hemminger27a1de92009-08-31 19:50:54 +0000411static netdev_tx_t el_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
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
Alan Coxa35f5de2007-11-19 15:02:32 +0000431 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int len = skb->len;
433 int pad = 0;
434 int gp_start;
435 unsigned char *buf = skb->data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (len < ETH_ZLEN)
438 pad = ETH_ZLEN - len;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400439
Alan Coxa35f5de2007-11-19 15:02:32 +0000440 gp_start = 0x800 - (len + pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 lp->tx_pkt_start = gp_start;
Alan Coxa35f5de2007-11-19 15:02:32 +0000443 lp->collisions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700445 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * Command mode with status cleared should [in theory]
449 * mean no more interrupts can be pending on the card.
450 */
451
452 outb_p(AX_SYS, AX_CMD);
453 inb_p(RX_STATUS);
454 inb_p(TX_STATUS);
455
456 lp->loading = 1;
457 lp->txing = 1;
458
459 /*
Alan Coxa35f5de2007-11-19 15:02:32 +0000460 * Turn interrupts back on while we spend a pleasant
461 * afternoon loading bytes into the board
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
463
464 spin_unlock_irqrestore(&lp->lock, flags);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400465
Alan Coxa35f5de2007-11-19 15:02:32 +0000466 /* Set rx packet area to 0. */
467 outw(0x00, RX_BUF_CLR);
468 /* aim - packet will be loaded into buffer start */
469 outw(gp_start, GP_LOW);
470 /* load buffer (usual thing each byte increments the pointer) */
471 outsb(DATAPORT, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (pad) {
Alan Coxa35f5de2007-11-19 15:02:32 +0000473 while (pad--) /* Zero fill buffer tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 outb(0, DATAPORT);
475 }
Alan Coxa35f5de2007-11-19 15:02:32 +0000476 /* the board reuses the same register */
477 outw(gp_start, GP_LOW);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400478
Alan Coxa35f5de2007-11-19 15:02:32 +0000479 if (lp->loading != 2) {
480 /* fire ... Trigger xmit. */
481 outb(AX_XMIT, AX_CMD);
482 lp->loading = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 dev->trans_start = jiffies;
484 if (el_debug > 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000485 pr_debug(" queued xmit.\n");
Alan Coxa35f5de2007-11-19 15:02:32 +0000486 dev_kfree_skb(skb);
Patrick McHardyec634fe2009-07-05 19:23:38 -0700487 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 /* A receive upset our load, despite our best efforts */
Alan Coxa35f5de2007-11-19 15:02:32 +0000490 if (el_debug > 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000491 pr_debug("%s: burped during tx load.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 spin_lock_irqsave(&lp->lock, flags);
Alan Coxad390d2d562008-03-10 21:57:20 +0000493 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/**
497 * el_interrupt:
498 * @irq: Interrupt number
499 * @dev_id: The 3c501 that burped
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400501 * Handle the ether interface interrupts. The 3c501 needs a lot more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * hand holding than most cards. In particular we get a transmit interrupt
503 * with a collision error because the board firmware isnt capable of rewinding
504 * its own transmit buffer pointers. It can however count to 16 for us.
505 *
506 * On the receive side the card is also very dumb. It has no buffering to
507 * speak of. We simply pull the packet out of its PIO buffer (which is slow)
508 * and queue it for the kernel. Then we reset the card for the next packet.
509 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200510 * We sometimes get surprise interrupts late both because the SMP IRQ delivery
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * is message passing and because the card sometimes seems to deliver late. I
512 * think if it is part way through a receive and the mode is changed it carries
513 * on receiving and sends us an interrupt. We have to band aid all these cases
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200514 * to get a sensible 150kBytes/second performance. Even then you want a small
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * TCP window.
516 */
517
David Howells7d12e782006-10-05 14:55:46 +0100518static irqreturn_t el_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct net_device *dev = dev_id;
521 struct net_local *lp;
522 int ioaddr;
523 int axsr; /* Aux. status reg. */
524
525 ioaddr = dev->base_addr;
526 lp = netdev_priv(dev);
527
528 spin_lock(&lp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * What happened ?
532 */
533
534 axsr = inb(AX_STATUS);
535
536 /*
537 * Log it
538 */
539
540 if (el_debug > 3)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000541 pr_debug("%s: el_interrupt() aux=%#02x\n", dev->name, axsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Alan Coxa35f5de2007-11-19 15:02:32 +0000543 if (lp->loading == 1 && !lp->txing)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000544 pr_warning("%s: Inconsistent state loading while not in tx\n",
Alan Coxa35f5de2007-11-19 15:02:32 +0000545 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Alan Coxa35f5de2007-11-19 15:02:32 +0000547 if (lp->txing) {
548 /*
549 * Board in transmit mode. May be loading. If we are
550 * loading we shouldn't have got this.
551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int txsr = inb(TX_STATUS);
553
Alan Coxa35f5de2007-11-19 15:02:32 +0000554 if (lp->loading == 1) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000555 if (el_debug > 2)
556 pr_debug("%s: Interrupt while loading [txsr=%02x gp=%04x rp=%04x]\n",
557 dev->name, txsr, inw(GP_LOW), inw(RX_LOW));
558
Alan Coxa35f5de2007-11-19 15:02:32 +0000559 /* Force a reload */
560 lp->loading = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_unlock(&lp->lock);
562 goto out;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (el_debug > 6)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000565 pr_debug("%s: txsr=%02x gp=%04x rp=%04x\n", dev->name,
Alan Coxa35f5de2007-11-19 15:02:32 +0000566 txsr, inw(GP_LOW), inw(RX_LOW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Alan Coxa35f5de2007-11-19 15:02:32 +0000568 if ((axsr & 0x80) && (txsr & TX_READY) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
Alan Coxa35f5de2007-11-19 15:02:32 +0000570 * FIXME: is there a logic to whether to keep
571 * on trying or reset immediately ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Alan Coxa35f5de2007-11-19 15:02:32 +0000573 if (el_debug > 1)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000574 pr_debug("%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x gp=%03x rp=%03x.\n",
Alan Coxa35f5de2007-11-19 15:02:32 +0000575 dev->name, txsr, axsr,
576 inw(ioaddr + EL1_DATAPTR),
577 inw(ioaddr + EL1_RXPTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 lp->txing = 0;
579 netif_wake_queue(dev);
Alan Coxa35f5de2007-11-19 15:02:32 +0000580 } else if (txsr & TX_16COLLISIONS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * Timed out
583 */
584 if (el_debug)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000585 pr_debug("%s: Transmit failed 16 times, Ethernet jammed?\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 outb(AX_SYS, AX_CMD);
587 lp->txing = 0;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700588 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 netif_wake_queue(dev);
Alan Coxa35f5de2007-11-19 15:02:32 +0000590 } else if (txsr & TX_COLLISION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * Retrigger xmit.
593 */
594
595 if (el_debug > 6)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000596 pr_debug("%s: retransmitting after a collision.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
Alan Coxa35f5de2007-11-19 15:02:32 +0000598 * Poor little chip can't reset its own start
599 * pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601
602 outb(AX_SYS, AX_CMD);
603 outw(lp->tx_pkt_start, GP_LOW);
604 outb(AX_XMIT, AX_CMD);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700605 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_unlock(&lp->lock);
607 goto out;
Alan Coxa35f5de2007-11-19 15:02:32 +0000608 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * It worked.. we will now fall through and receive
611 */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700612 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (el_debug > 6)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000614 pr_debug("%s: Tx succeeded %s\n", dev->name,
615 (txsr & TX_RDY) ? "." : "but tx is busy!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * This is safe the interrupt is atomic WRT itself.
618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 lp->txing = 0;
Alan Coxa35f5de2007-11-19 15:02:32 +0000620 /* In case more to transmit */
621 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Alan Coxa35f5de2007-11-19 15:02:32 +0000623 } else {
624 /*
625 * In receive mode.
626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 int rxsr = inb(RX_STATUS);
629 if (el_debug > 5)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000630 pr_debug("%s: rxsr=%02x txsr=%02x rp=%04x\n",
631 dev->name, rxsr, inb(TX_STATUS), inw(RX_LOW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Just reading rx_status fixes most errors.
634 */
635 if (rxsr & RX_MISSED)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700636 dev->stats.rx_missed_errors++;
Alan Coxa35f5de2007-11-19 15:02:32 +0000637 else if (rxsr & RX_RUNT) {
638 /* Handled to avoid board lock-up. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700639 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (el_debug > 5)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000641 pr_debug("%s: runt.\n", dev->name);
Alan Coxa35f5de2007-11-19 15:02:32 +0000642 } else if (rxsr & RX_GOOD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * Receive worked.
645 */
646 el_receive(dev);
Alan Coxa35f5de2007-11-19 15:02:32 +0000647 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /*
649 * Nothing? Something is broken!
650 */
651 if (el_debug > 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000652 pr_debug("%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 dev->name, rxsr);
654 el_reset(dev);
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658 /*
659 * Move into receive mode
660 */
661
662 outb(AX_RX, AX_CMD);
663 outw(0x00, RX_BUF_CLR);
664 inb(RX_STATUS); /* Be certain that interrupts are cleared. */
665 inb(TX_STATUS);
666 spin_unlock(&lp->lock);
667out:
668 return IRQ_HANDLED;
669}
670
671
672/**
673 * el_receive:
674 * @dev: Device to pull the packets from
675 *
676 * We have a good packet. Well, not really "good", just mostly not broken.
677 * We must check everything to see if it is good. In particular we occasionally
678 * get wild packet sizes from the card. If the packet seems sane we PIO it
679 * off the card and queue it for the protocol layers.
680 */
681
682static void el_receive(struct net_device *dev)
683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int ioaddr = dev->base_addr;
685 int pkt_len;
686 struct sk_buff *skb;
687
688 pkt_len = inw(RX_LOW);
689
690 if (el_debug > 4)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000691 pr_debug(" el_receive %d.\n", pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Alan Coxa35f5de2007-11-19 15:02:32 +0000693 if (pkt_len < 60 || pkt_len > 1536) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (el_debug)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000695 pr_debug("%s: bogus packet, length=%d\n",
Alan Coxad390d2d562008-03-10 21:57:20 +0000696 dev->name, pkt_len);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700697 dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return;
699 }
700
701 /*
702 * Command mode so we can empty the buffer
703 */
704
705 outb(AX_SYS, AX_CMD);
706 skb = dev_alloc_skb(pkt_len+2);
707
708 /*
709 * Start of frame
710 */
711
712 outw(0x00, GP_LOW);
Alan Coxa35f5de2007-11-19 15:02:32 +0000713 if (skb == NULL) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000714 pr_info("%s: Memory squeeze, dropping packet.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700715 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return;
Alan Coxa35f5de2007-11-19 15:02:32 +0000717 } else {
718 skb_reserve(skb, 2); /* Force 16 byte alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
720 * The read increments through the bytes. The interrupt
721 * handler will fix the pointer when it returns to
722 * receive mode.
723 */
Alan Coxa35f5de2007-11-19 15:02:32 +0000724 insb(DATAPORT, skb_put(skb, pkt_len), pkt_len);
725 skb->protocol = eth_type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700727 dev->stats.rx_packets++;
Alan Coxa35f5de2007-11-19 15:02:32 +0000728 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 return;
731}
732
733/**
734 * el_reset: Reset a 3c501 card
735 * @dev: The 3c501 card about to get zapped
736 *
737 * Even resetting a 3c501 isnt simple. When you activate reset it loses all
738 * its configuration. You must hold the lock when doing this. The function
739 * cannot take the lock itself as it is callable from the irq handler.
740 */
741
742static void el_reset(struct net_device *dev)
743{
744 struct net_local *lp = netdev_priv(dev);
745 int ioaddr = dev->base_addr;
746
Alan Coxa35f5de2007-11-19 15:02:32 +0000747 if (el_debug > 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000748 pr_info("3c501 reset...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 outb(AX_RESET, AX_CMD); /* Reset the chip */
Alan Coxad390d2d562008-03-10 21:57:20 +0000750 /* Aux control, irq and loopback enabled */
751 outb(AX_LOOP, AX_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 {
753 int i;
754 for (i = 0; i < 6; i++) /* Set the station address. */
755 outb(dev->dev_addr[i], ioaddr + i);
756 }
757
758 outw(0, RX_BUF_CLR); /* Set rx packet area to 0. */
759 outb(TX_NORM, TX_CMD); /* tx irq on done, collision */
760 outb(RX_NORM, RX_CMD); /* Set Rx commands. */
761 inb(RX_STATUS); /* Clear status. */
762 inb(TX_STATUS);
763 lp->txing = 0;
764}
765
766/**
767 * el1_close:
768 * @dev: 3c501 card to shut down
769 *
770 * Close a 3c501 card. The IFF_UP flag has been cleared by the user via
771 * the SIOCSIFFLAGS ioctl. We stop any further transmissions being queued,
772 * and then disable the interrupts. Finally we reset the chip. The effects
773 * of the rest will be cleaned up by #el1_open. Always returns 0 indicating
774 * a success.
775 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static int el1_close(struct net_device *dev)
778{
779 int ioaddr = dev->base_addr;
780
781 if (el_debug > 2)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000782 pr_info("%s: Shutting down Ethernet card at %#x.\n",
Alan Coxa35f5de2007-11-19 15:02:32 +0000783 dev->name, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 netif_stop_queue(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /*
788 * Free and disable the IRQ.
789 */
790
791 free_irq(dev->irq, dev);
792 outb(AX_RESET, AX_CMD); /* Reset the chip */
793
794 return 0;
795}
796
797/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * set_multicast_list:
799 * @dev: The device to adjust
800 *
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400801 * Set or clear the multicast filter for this adaptor to use the best-effort
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * filtering supported. The 3c501 supports only three modes of filtering.
803 * It always receives broadcasts and packets for itself. You can choose to
804 * optionally receive all packets, or all multicast packets on top of this.
805 */
806
807static void set_multicast_list(struct net_device *dev)
808{
809 int ioaddr = dev->base_addr;
810
Alan Coxa35f5de2007-11-19 15:02:32 +0000811 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 outb(RX_PROM, RX_CMD);
813 inb(RX_STATUS);
Jiri Pirko59ce25d2010-02-17 23:12:15 +0000814 } else if (!netdev_mc_empty(dev) || dev->flags & IFF_ALLMULTI) {
Alan Coxa35f5de2007-11-19 15:02:32 +0000815 /* Multicast or all multicast is the same */
816 outb(RX_MULT, RX_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 inb(RX_STATUS); /* Clear status. */
Alan Coxa35f5de2007-11-19 15:02:32 +0000818 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 outb(RX_NORM, RX_CMD);
820 inb(RX_STATUS);
821 }
822}
823
824
825static void netdev_get_drvinfo(struct net_device *dev,
826 struct ethtool_drvinfo *info)
827{
828 strcpy(info->driver, DRV_NAME);
829 strcpy(info->version, DRV_VERSION);
830 sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
831}
832
833static u32 netdev_get_msglevel(struct net_device *dev)
834{
835 return debug;
836}
837
838static void netdev_set_msglevel(struct net_device *dev, u32 level)
839{
840 debug = level;
841}
842
Jeff Garzik7282d492006-09-13 14:30:00 -0400843static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 .get_drvinfo = netdev_get_drvinfo,
845 .get_msglevel = netdev_get_msglevel,
846 .set_msglevel = netdev_set_msglevel,
847};
848
849#ifdef MODULE
850
851static struct net_device *dev_3c501;
852
853module_param(io, int, 0);
854module_param(irq, int, 0);
855MODULE_PARM_DESC(io, "EtherLink I/O base address");
856MODULE_PARM_DESC(irq, "EtherLink IRQ number");
857
858/**
859 * init_module:
860 *
861 * When the driver is loaded as a module this function is called. We fake up
862 * a device structure with the base I/O and interrupt set as if it were being
863 * called from Space.c. This minimises the extra code that would otherwise
864 * be required.
865 *
866 * Returns 0 for success or -EIO if a card is not found. Returning an error
867 * here also causes the module to be unloaded
868 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400869
Randy Dunlap96e672c2006-06-10 13:33:48 -0700870int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 dev_3c501 = el1_probe(-1);
873 if (IS_ERR(dev_3c501))
874 return PTR_ERR(dev_3c501);
875 return 0;
876}
877
878/**
879 * cleanup_module:
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * The module is being unloaded. We unhook our network device from the system
882 * and then free up the resources we took when the card was found.
883 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400884
Al Viroafc8eb42006-06-14 18:50:53 -0400885void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct net_device *dev = dev_3c501;
888 unregister_netdev(dev);
889 release_region(dev->base_addr, EL1_IO_EXTENT);
890 free_netdev(dev);
891}
892
893#endif /* MODULE */
894
895MODULE_AUTHOR("Donald Becker, Alan Cox");
896MODULE_DESCRIPTION("Support for the ancient 3Com 3c501 ethernet card");
897MODULE_LICENSE("GPL");
898