1 /* lance.c: An AMD LANCE/PCnet ethernet driver for Linux. */
3 Written/copyright 1993-1998 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
7 This software may be used and distributed according to the terms
8 of the GNU General Public License, incorporated herein by reference.
10 This driver is for the Allied Telesis AT1500 and HP J2405A, and should work
11 with most other LANCE-based bus-master (NE2100/NE2500) ethercards.
13 The author may be reached as becker@scyld.com, or C/O
14 Scyld Computing Corporation
15 410 Severn Ave., Suite 210
19 - alignment problem with 1.3.* kernel and some minor changes.
20 Thomas Bogendoerfer (tsbogend@bigbug.franken.de):
21 - added support for Linux/Alpha, but removed most of it, because
22 it worked only for the PCI chip.
23 - added hook for the 32bit lance driver
24 - added PCnetPCI II (79C970A) to chip table
25 Paul Gortmaker (gpg109@rsphy1.anu.edu.au):
26 - hopefully fix above so Linux/Alpha can use ISA cards too.
27 8/20/96 Fixed 7990 autoIRQ failure and reversed unneeded alignment -djb
28 v1.12 10/27/97 Module support -djb
29 v1.14 2/3/98 Module support modified, made PCI support optional -djb
30 v1.15 5/27/99 Fixed bug in the cleanup_module(). dev->priv was freed
31 before unregister_netdev() which caused NULL pointer
32 reference later in the chain (in rtnetlink_fill_ifinfo())
33 -- Mika Kuoppala <miku@iki.fi>
35 Forward ported v1.14 to 2.1.129, merged the PCI and misc changes from
36 the 2.1 version of the old driver - Alan Cox
38 Get rid of check_region, check kmalloc return in lance_probe1
39 Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 11/01/2001
42 static const char version[] = "lance.c:v1.15ac 1999/11/13 dplatt@3do.com, becker@cesdis.gsfc.nasa.gov\n";
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/string.h>
47 #include <linux/errno.h>
48 #include <linux/ioport.h>
49 #include <linux/slab.h>
50 #include <linux/interrupt.h>
51 #include <linux/pci.h>
52 #include <linux/init.h>
53 #include <linux/netdevice.h>
54 #include <linux/etherdevice.h>
55 #include <linux/skbuff.h>
57 #include <asm/bitops.h>
61 static unsigned int lance_portlist[] __initdata = { 0x300, 0x320, 0x340, 0x360, 0};
62 static int lance_probe1(struct net_device *dev, int ioaddr, int irq, int options);
63 static int __init do_lance_probe(struct net_device *dev);
66 static int lance_debug = LANCE_DEBUG;
68 static int lance_debug = 1;
74 I. Board Compatibility
76 This device driver is designed for the AMD 79C960, the "PCnet-ISA
77 single-chip ethernet controller for ISA". This chip is used in a wide
78 variety of boards from vendors such as Allied Telesis, HP, Kingston,
79 and Boca. This driver is also intended to work with older AMD 7990
80 designs, such as the NE1500 and NE2100, and newer 79C961. For convenience,
81 I use the name LANCE to refer to all of the AMD chips, even though it properly
82 refers only to the original 7990.
84 II. Board-specific settings
86 The driver is designed to work the boards that use the faster
87 bus-master mode, rather than in shared memory mode. (Only older designs
88 have on-board buffer memory needed to support the slower shared memory mode.)
90 Most ISA boards have jumpered settings for the I/O base, IRQ line, and DMA
91 channel. This driver probes the likely base addresses:
92 {0x300, 0x320, 0x340, 0x360}.
93 After the board is found it generates a DMA-timeout interrupt and uses
94 autoIRQ to find the IRQ line. The DMA channel can be set with the low bits
95 of the otherwise-unused dev->mem_start value (aka PARAM1). If unset it is
96 probed for by enabling each free DMA channel in turn and checking if
97 initialization succeeds.
99 The HP-J2405A board is an exception: with this board it is easy to read the
100 EEPROM-set values for the base, IRQ, and DMA. (Of course you must already
101 _know_ the base address -- that field is for writing the EEPROM.)
103 III. Driver operation
106 The LANCE uses ring buffers of Tx and Rx descriptors. Each entry describes
107 the base and length of the data buffer, along with status bits. The length
108 of these buffers is set by LANCE_LOG_{RX,TX}_BUFFERS, which is log_2() of
109 the buffer length (rather than being directly the buffer length) for
110 implementation ease. The current values are 2 (Tx) and 4 (Rx), which leads to
111 ring sizes of 4 (Tx) and 16 (Rx). Increasing the number of ring entries
112 needlessly uses extra space and reduces the chance that an upper layer will
113 be able to reorder queued Tx packets based on priority. Decreasing the number
114 of entries makes it more difficult to achieve back-to-back packet transmission
115 and increases the chance that Rx ring will overflow. (Consider the worst case
116 of receiving back-to-back minimum-sized packets.)
118 The LANCE has the capability to "chain" both Rx and Tx buffers, but this driver
119 statically allocates full-sized (slightly oversized -- PKT_BUF_SZ) buffers to
120 avoid the administrative overhead. For the Rx side this avoids dynamically
121 allocating full-sized buffers "just in case", at the expense of a
122 memory-to-memory data copy for each packet received. For most systems this
123 is a good tradeoff: the Rx buffer will always be in low memory, the copy
124 is inexpensive, and it primes the cache for later packet processing. For Tx
125 the buffers are only used when needed as low-memory bounce buffers.
127 IIIB. 16M memory limitations.
128 For the ISA bus master mode all structures used directly by the LANCE,
129 the initialization block, Rx and Tx rings, and data buffers, must be
130 accessible from the ISA bus, i.e. in the lower 16M of real memory.
131 This is a problem for current Linux kernels on >16M machines. The network
132 devices are initialized after memory initialization, and the kernel doles out
133 memory from the top of memory downward. The current solution is to have a
134 special network initialization routine that's called before memory
135 initialization; this will eventually be generalized for all network devices.
136 As mentioned before, low-memory "bounce-buffers" are used when needed.
138 IIIC. Synchronization
139 The driver runs as two independent, single-threaded flows of control. One
140 is the send-packet routine, which enforces single-threaded use by the
141 dev->tbusy flag. The other thread is the interrupt handler, which is single
142 threaded by the hardware and other software.
144 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
145 flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
146 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
147 the 'lp->tx_full' flag.
149 The interrupt handler has exclusive control over the Rx ring and records stats
150 from the Tx ring. (The Tx-done interrupt can't be selectively turned off, so
151 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
152 stats.) After reaping the stats, it marks the queue entry as empty by setting
153 the 'base' to zero. Iff the 'lp->tx_full' flag is set, it clears both the
154 tx_full and tbusy flags.
158 /* Set the number of Tx and Rx buffers, using Log_2(# buffers).
159 Reasonable default values are 16 Tx buffers, and 16 Rx buffers.
160 That translates to 4 and 4 (16 == 2^^4).
161 This is a compile-time option for efficiency.
163 #ifndef LANCE_LOG_TX_BUFFERS
164 #define LANCE_LOG_TX_BUFFERS 4
165 #define LANCE_LOG_RX_BUFFERS 4
168 #define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))
169 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
170 #define TX_RING_LEN_BITS ((LANCE_LOG_TX_BUFFERS) << 29)
172 #define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))
173 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
174 #define RX_RING_LEN_BITS ((LANCE_LOG_RX_BUFFERS) << 29)
176 #define PKT_BUF_SZ 1544
178 /* Offsets from base I/O address. */
179 #define LANCE_DATA 0x10
180 #define LANCE_ADDR 0x12
181 #define LANCE_RESET 0x14
182 #define LANCE_BUS_IF 0x16
183 #define LANCE_TOTAL_SIZE 0x18
185 #define TX_TIMEOUT 20
187 /* The LANCE Rx and Tx ring descriptors. */
188 struct lance_rx_head {
190 s16 buf_length; /* This length is 2s complement (negative)! */
191 s16 msg_length; /* This length is "normal". */
194 struct lance_tx_head {
196 s16 length; /* Length is 2s complement (negative)! */
200 /* The LANCE initialization block, described in databook. */
201 struct lance_init_block {
202 u16 mode; /* Pre-set mode (reg. 15) */
203 u8 phys_addr[6]; /* Physical ethernet address */
204 u32 filter[2]; /* Multicast filter (unused). */
205 /* Receive and transmit ring base, along with extra bits. */
206 u32 rx_ring; /* Tx and Rx ring base pointers */
210 struct lance_private {
211 /* The Tx and Rx ring entries must be aligned on 8-byte boundaries. */
212 struct lance_rx_head rx_ring[RX_RING_SIZE];
213 struct lance_tx_head tx_ring[TX_RING_SIZE];
214 struct lance_init_block init_block;
216 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
217 struct sk_buff* tx_skbuff[TX_RING_SIZE];
218 /* The addresses of receive-in-place skbuffs. */
219 struct sk_buff* rx_skbuff[RX_RING_SIZE];
220 unsigned long rx_buffs; /* Address of Rx and Tx buffers. */
221 /* Tx low-memory "bounce buffer" address. */
222 char (*tx_bounce_buffs)[PKT_BUF_SZ];
223 int cur_rx, cur_tx; /* The next free ring entry */
224 int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
226 struct net_device_stats stats;
227 unsigned char chip_version; /* See lance_chip_type. */
231 #define LANCE_MUST_PAD 0x00000001
232 #define LANCE_ENABLE_AUTOSELECT 0x00000002
233 #define LANCE_MUST_REINIT_RING 0x00000004
234 #define LANCE_MUST_UNRESET 0x00000008
235 #define LANCE_HAS_MISSED_FRAME 0x00000010
237 /* A mapping from the chip ID number to the part number and features.
238 These are from the datasheets -- in real life the '970 version
239 reportedly has the same ID as the '965. */
240 static struct lance_chip_type {
245 {0x0000, "LANCE 7990", /* Ancient lance chip. */
246 LANCE_MUST_PAD + LANCE_MUST_UNRESET},
247 {0x0003, "PCnet/ISA 79C960", /* 79C960 PCnet/ISA. */
248 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
249 LANCE_HAS_MISSED_FRAME},
250 {0x2260, "PCnet/ISA+ 79C961", /* 79C961 PCnet/ISA+, Plug-n-Play. */
251 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
252 LANCE_HAS_MISSED_FRAME},
253 {0x2420, "PCnet/PCI 79C970", /* 79C970 or 79C974 PCnet-SCSI, PCI. */
254 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
255 LANCE_HAS_MISSED_FRAME},
256 /* Bug: the PCnet/PCI actually uses the PCnet/VLB ID number, so just call
258 {0x2430, "PCnet32", /* 79C965 PCnet for VL bus. */
259 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
260 LANCE_HAS_MISSED_FRAME},
261 {0x2621, "PCnet/PCI-II 79C970A", /* 79C970A PCInetPCI II. */
262 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
263 LANCE_HAS_MISSED_FRAME},
264 {0x0, "PCnet (unknown)",
265 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
266 LANCE_HAS_MISSED_FRAME},
269 enum {OLD_LANCE = 0, PCNET_ISA=1, PCNET_ISAP=2, PCNET_PCI=3, PCNET_VLB=4, PCNET_PCI_II=5, LANCE_UNKNOWN=6};
272 /* Non-zero if lance_probe1() needs to allocate low-memory bounce buffers.
273 Assume yes until we know the memory size. */
274 static unsigned char lance_need_isa_bounce_buffers = 1;
276 static int lance_open(struct net_device *dev);
277 static void lance_init_ring(struct net_device *dev, int mode);
278 static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev);
279 static int lance_rx(struct net_device *dev);
280 static irqreturn_t lance_interrupt(int irq, void *dev_id, struct pt_regs *regs);
281 static int lance_close(struct net_device *dev);
282 static struct net_device_stats *lance_get_stats(struct net_device *dev);
283 static void set_multicast_list(struct net_device *dev);
284 static void lance_tx_timeout (struct net_device *dev);
288 static void cleanup_card(struct net_device *dev)
290 struct lance_private *lp = dev->priv;
293 release_region(dev->base_addr, LANCE_TOTAL_SIZE);
294 kfree(lp->tx_bounce_buffs);
295 kfree((void*)lp->rx_buffs);
300 #define MAX_CARDS 8 /* Max number of interfaces (cards) per module */
302 static struct net_device *dev_lance[MAX_CARDS];
303 static int io[MAX_CARDS];
304 static int dma[MAX_CARDS];
305 static int irq[MAX_CARDS];
307 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");
308 MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");
309 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
310 MODULE_PARM(lance_debug, "i");
311 MODULE_PARM_DESC(io, "LANCE/PCnet I/O base address(es),required");
312 MODULE_PARM_DESC(dma, "LANCE/PCnet ISA DMA channel (ignored for some devices)");
313 MODULE_PARM_DESC(irq, "LANCE/PCnet IRQ number (ignored for some devices)");
314 MODULE_PARM_DESC(lance_debug, "LANCE/PCnet debug level (0-7)");
316 int init_module(void)
318 struct net_device *dev;
319 int this_dev, found = 0;
321 for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) {
322 if (io[this_dev] == 0) {
323 if (this_dev != 0) /* only complain once */
325 printk(KERN_NOTICE "lance.c: Module autoprobing not allowed. Append \"io=0xNNN\" value(s).\n");
328 dev = alloc_etherdev(0);
331 dev->irq = irq[this_dev];
332 dev->base_addr = io[this_dev];
333 dev->dma = dma[this_dev];
334 if (do_lance_probe(dev) == 0) {
335 if (register_netdev(dev) == 0) {
336 dev_lance[found++] = dev;
349 void cleanup_module(void)
353 for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) {
354 struct net_device *dev = dev_lance[this_dev];
356 unregister_netdev(dev);
363 MODULE_LICENSE("GPL");
366 /* Starting in v2.1.*, the LANCE/PCnet probe is now similar to the other
367 board probes now that kmalloc() can allocate ISA DMA-able regions.
368 This also allows the LANCE driver to be used as a module.
370 static int __init do_lance_probe(struct net_device *dev)
374 if (high_memory <= phys_to_virt(16*1024*1024))
375 lance_need_isa_bounce_buffers = 0;
377 for (port = lance_portlist; *port; port++) {
379 struct resource *r = request_region(ioaddr, LANCE_TOTAL_SIZE,
383 /* Detect "normal" 0x57 0x57 and the NI6510EB 0x52 0x44
384 signatures w/ minimal I/O reads */
385 char offset15, offset14 = inb(ioaddr + 14);
387 if ((offset14 == 0x52 || offset14 == 0x57) &&
388 ((offset15 = inb(ioaddr + 15)) == 0x57 ||
390 result = lance_probe1(dev, ioaddr, 0, 0);
392 struct lance_private *lp = dev->priv;
393 int ver = lp->chip_version;
395 r->name = chip_table[ver].name;
399 release_region(ioaddr, LANCE_TOTAL_SIZE);
405 struct net_device * __init lance_probe(int unit)
407 struct net_device *dev = alloc_etherdev(0);
411 return ERR_PTR(-ENODEV);
413 sprintf(dev->name, "eth%d", unit);
414 netdev_boot_setup_check(dev);
416 err = do_lance_probe(dev);
419 err = register_netdev(dev);
430 static int __init lance_probe1(struct net_device *dev, int ioaddr, int irq, int options)
432 struct lance_private *lp;
433 long dma_channels; /* Mark spuriously-busy DMA channels */
434 int i, reset_val, lance_version;
435 const char *chipname;
436 /* Flags for specific chips or boards. */
437 unsigned char hpJ2405A = 0; /* HP ISA adaptor */
438 int hp_builtin = 0; /* HP on-board ethernet. */
439 static int did_version; /* Already printed version info. */
443 /* First we look for special cases.
444 Check for HP's on-board ethernet by looking for 'HP' in the BIOS.
445 There are two HP versions, check the BIOS for the configuration port.
446 This method provided by L. Julliard, Laurent_Julliard@grenoble.hp.com.
448 if (isa_readw(0x000f0102) == 0x5048) {
449 static const short ioaddr_table[] = { 0x300, 0x320, 0x340, 0x360};
450 int hp_port = (isa_readl(0x000f00f1) & 1) ? 0x499 : 0x99;
451 /* We can have boards other than the built-in! Verify this is on-board. */
452 if ((inb(hp_port) & 0xc0) == 0x80
453 && ioaddr_table[inb(hp_port) & 3] == ioaddr)
454 hp_builtin = hp_port;
456 /* We also recognize the HP Vectra on-board here, but check below. */
457 hpJ2405A = (inb(ioaddr) == 0x08 && inb(ioaddr+1) == 0x00
458 && inb(ioaddr+2) == 0x09);
460 /* Reset the LANCE. */
461 reset_val = inw(ioaddr+LANCE_RESET); /* Reset the LANCE */
463 /* The Un-Reset needed is only needed for the real NE2100, and will
464 confuse the HP board. */
466 outw(reset_val, ioaddr+LANCE_RESET);
468 outw(0x0000, ioaddr+LANCE_ADDR); /* Switch to window 0 */
469 if (inw(ioaddr+LANCE_DATA) != 0x0004)
472 /* Get the version of the chip. */
473 outw(88, ioaddr+LANCE_ADDR);
474 if (inw(ioaddr+LANCE_ADDR) != 88) {
476 } else { /* Good, it's a newer chip. */
477 int chip_version = inw(ioaddr+LANCE_DATA);
478 outw(89, ioaddr+LANCE_ADDR);
479 chip_version |= inw(ioaddr+LANCE_DATA) << 16;
481 printk(" LANCE chip version is %#x.\n", chip_version);
482 if ((chip_version & 0xfff) != 0x003)
484 chip_version = (chip_version >> 12) & 0xffff;
485 for (lance_version = 1; chip_table[lance_version].id_number; lance_version++) {
486 if (chip_table[lance_version].id_number == chip_version)
491 /* We can't allocate dev->priv from alloc_etherdev() because it must
492 a ISA DMA-able region. */
493 SET_MODULE_OWNER(dev);
494 chipname = chip_table[lance_version].name;
495 printk("%s: %s at %#3x,", dev->name, chipname, ioaddr);
497 /* There is a 16 byte station address PROM at the base address.
498 The first six bytes are the station address. */
499 for (i = 0; i < 6; i++)
500 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
502 dev->base_addr = ioaddr;
503 /* Make certain the data structures used by the LANCE are aligned and DMAble. */
505 lp = kmalloc(sizeof(*lp), GFP_DMA | GFP_KERNEL);
508 if (lance_debug > 6) printk(" (#0x%05lx)", (unsigned long)lp);
509 memset(lp, 0, sizeof(*lp));
512 lp->rx_buffs = (unsigned long)kmalloc(PKT_BUF_SZ*RX_RING_SIZE,
513 GFP_DMA | GFP_KERNEL);
516 if (lance_need_isa_bounce_buffers) {
517 lp->tx_bounce_buffs = kmalloc(PKT_BUF_SZ*TX_RING_SIZE,
518 GFP_DMA | GFP_KERNEL);
519 if (!lp->tx_bounce_buffs)
522 lp->tx_bounce_buffs = NULL;
524 lp->chip_version = lance_version;
525 spin_lock_init(&lp->devlock);
527 lp->init_block.mode = 0x0003; /* Disable Rx and Tx. */
528 for (i = 0; i < 6; i++)
529 lp->init_block.phys_addr[i] = dev->dev_addr[i];
530 lp->init_block.filter[0] = 0x00000000;
531 lp->init_block.filter[1] = 0x00000000;
532 lp->init_block.rx_ring = ((u32)isa_virt_to_bus(lp->rx_ring) & 0xffffff) | RX_RING_LEN_BITS;
533 lp->init_block.tx_ring = ((u32)isa_virt_to_bus(lp->tx_ring) & 0xffffff) | TX_RING_LEN_BITS;
535 outw(0x0001, ioaddr+LANCE_ADDR);
536 inw(ioaddr+LANCE_ADDR);
537 outw((short) (u32) isa_virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
538 outw(0x0002, ioaddr+LANCE_ADDR);
539 inw(ioaddr+LANCE_ADDR);
540 outw(((u32)isa_virt_to_bus(&lp->init_block)) >> 16, ioaddr+LANCE_DATA);
541 outw(0x0000, ioaddr+LANCE_ADDR);
542 inw(ioaddr+LANCE_ADDR);
544 if (irq) { /* Set iff PCI card. */
545 dev->dma = 4; /* Native bus-master, no DMA channel needed. */
547 } else if (hp_builtin) {
548 static const char dma_tbl[4] = {3, 5, 6, 0};
549 static const char irq_tbl[4] = {3, 4, 5, 9};
550 unsigned char port_val = inb(hp_builtin);
551 dev->dma = dma_tbl[(port_val >> 4) & 3];
552 dev->irq = irq_tbl[(port_val >> 2) & 3];
553 printk(" HP Vectra IRQ %d DMA %d.\n", dev->irq, dev->dma);
554 } else if (hpJ2405A) {
555 static const char dma_tbl[4] = {3, 5, 6, 7};
556 static const char irq_tbl[8] = {3, 4, 5, 9, 10, 11, 12, 15};
557 short reset_val = inw(ioaddr+LANCE_RESET);
558 dev->dma = dma_tbl[(reset_val >> 2) & 3];
559 dev->irq = irq_tbl[(reset_val >> 4) & 7];
560 printk(" HP J2405A IRQ %d DMA %d.\n", dev->irq, dev->dma);
561 } else if (lance_version == PCNET_ISAP) { /* The plug-n-play version. */
563 outw(8, ioaddr+LANCE_ADDR);
564 bus_info = inw(ioaddr+LANCE_BUS_IF);
565 dev->dma = bus_info & 0x07;
566 dev->irq = (bus_info >> 4) & 0x0F;
568 /* The DMA channel may be passed in PARAM1. */
569 if (dev->mem_start & 0x07)
570 dev->dma = dev->mem_start & 0x07;
574 /* Read the DMA channel status register, so that we can avoid
575 stuck DMA channels in the DMA detection below. */
576 dma_channels = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
577 (inb(DMA2_STAT_REG) & 0xf0);
581 printk(" assigned IRQ %d", dev->irq);
582 else if (lance_version != 0) { /* 7990 boards need DMA detection first. */
583 unsigned long irq_mask;
585 /* To auto-IRQ we enable the initialization-done and DMA error
586 interrupts. For ISA boards we get a DMA error, but VLB and PCI
588 irq_mask = probe_irq_on();
590 /* Trigger an initialization just for the interrupt. */
591 outw(0x0041, ioaddr+LANCE_DATA);
594 dev->irq = probe_irq_off(irq_mask);
596 printk(", probed IRQ %d", dev->irq);
598 printk(", failed to detect IRQ line.\n");
602 /* Check for the initialization done bit, 0x0100, which means
603 that we don't need a DMA channel. */
604 if (inw(ioaddr+LANCE_DATA) & 0x0100)
609 printk(", no DMA needed.\n");
610 } else if (dev->dma) {
611 if (request_dma(dev->dma, chipname)) {
612 printk("DMA %d allocation failed.\n", dev->dma);
615 printk(", assigned DMA %d.\n", dev->dma);
616 } else { /* OK, we have to auto-DMA. */
617 for (i = 0; i < 4; i++) {
618 static const char dmas[] = { 5, 6, 7, 3 };
622 /* Don't enable a permanently busy DMA channel, or the machine
624 if (test_bit(dma, &dma_channels))
626 outw(0x7f04, ioaddr+LANCE_DATA); /* Clear the memory error bits. */
627 if (request_dma(dma, chipname))
630 flags=claim_dma_lock();
631 set_dma_mode(dma, DMA_MODE_CASCADE);
633 release_dma_lock(flags);
635 /* Trigger an initialization. */
636 outw(0x0001, ioaddr+LANCE_DATA);
637 for (boguscnt = 100; boguscnt > 0; --boguscnt)
638 if (inw(ioaddr+LANCE_DATA) & 0x0900)
640 if (inw(ioaddr+LANCE_DATA) & 0x0100) {
642 printk(", DMA %d.\n", dev->dma);
645 flags=claim_dma_lock();
647 release_dma_lock(flags);
651 if (i == 4) { /* Failure: bail. */
652 printk("DMA detection failed.\n");
657 if (lance_version == 0 && dev->irq == 0) {
658 /* We may auto-IRQ now that we have a DMA channel. */
659 /* Trigger an initialization just for the interrupt. */
660 unsigned long irq_mask;
662 irq_mask = probe_irq_on();
663 outw(0x0041, ioaddr+LANCE_DATA);
666 dev->irq = probe_irq_off(irq_mask);
668 printk(" Failed to detect the 7990 IRQ line.\n");
671 printk(" Auto-IRQ detected IRQ%d.\n", dev->irq);
674 if (chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
675 /* Turn on auto-select of media (10baseT or BNC) so that the user
676 can watch the LEDs even if the board isn't opened. */
677 outw(0x0002, ioaddr+LANCE_ADDR);
678 /* Don't touch 10base2 power bit. */
679 outw(inw(ioaddr+LANCE_BUS_IF) | 0x0002, ioaddr+LANCE_BUS_IF);
682 if (lance_debug > 0 && did_version++ == 0)
685 /* The LANCE-specific entries in the device structure. */
686 dev->open = lance_open;
687 dev->hard_start_xmit = lance_start_xmit;
688 dev->stop = lance_close;
689 dev->get_stats = lance_get_stats;
690 dev->set_multicast_list = set_multicast_list;
691 dev->tx_timeout = lance_tx_timeout;
692 dev->watchdog_timeo = TX_TIMEOUT;
699 kfree(lp->tx_bounce_buffs);
701 kfree((void*)lp->rx_buffs);
709 lance_open(struct net_device *dev)
711 struct lance_private *lp = dev->priv;
712 int ioaddr = dev->base_addr;
716 request_irq(dev->irq, &lance_interrupt, 0, lp->name, dev)) {
720 /* We used to allocate DMA here, but that was silly.
721 DMA lines can't be shared! We now permanently allocate them. */
723 /* Reset the LANCE */
724 inw(ioaddr+LANCE_RESET);
726 /* The DMA controller is used as a no-operation slave, "cascade mode". */
728 unsigned long flags=claim_dma_lock();
729 enable_dma(dev->dma);
730 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
731 release_dma_lock(flags);
734 /* Un-Reset the LANCE, needed only for the NE2100. */
735 if (chip_table[lp->chip_version].flags & LANCE_MUST_UNRESET)
736 outw(0, ioaddr+LANCE_RESET);
738 if (chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
739 /* This is 79C960-specific: Turn on auto-select of media (AUI, BNC). */
740 outw(0x0002, ioaddr+LANCE_ADDR);
741 /* Only touch autoselect bit. */
742 outw(inw(ioaddr+LANCE_BUS_IF) | 0x0002, ioaddr+LANCE_BUS_IF);
746 printk("%s: lance_open() irq %d dma %d tx/rx rings %#x/%#x init %#x.\n",
747 dev->name, dev->irq, dev->dma,
748 (u32) isa_virt_to_bus(lp->tx_ring),
749 (u32) isa_virt_to_bus(lp->rx_ring),
750 (u32) isa_virt_to_bus(&lp->init_block));
752 lance_init_ring(dev, GFP_KERNEL);
753 /* Re-initialize the LANCE, and start it when done. */
754 outw(0x0001, ioaddr+LANCE_ADDR);
755 outw((short) (u32) isa_virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
756 outw(0x0002, ioaddr+LANCE_ADDR);
757 outw(((u32)isa_virt_to_bus(&lp->init_block)) >> 16, ioaddr+LANCE_DATA);
759 outw(0x0004, ioaddr+LANCE_ADDR);
760 outw(0x0915, ioaddr+LANCE_DATA);
762 outw(0x0000, ioaddr+LANCE_ADDR);
763 outw(0x0001, ioaddr+LANCE_DATA);
765 netif_start_queue (dev);
769 if (inw(ioaddr+LANCE_DATA) & 0x0100)
772 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
773 * reports that doing so triggers a bug in the '974.
775 outw(0x0042, ioaddr+LANCE_DATA);
778 printk("%s: LANCE open after %d ticks, init block %#x csr0 %4.4x.\n",
779 dev->name, i, (u32) isa_virt_to_bus(&lp->init_block), inw(ioaddr+LANCE_DATA));
781 return 0; /* Always succeed */
784 /* The LANCE has been halted for one reason or another (busmaster memory
785 arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
786 etc.). Modern LANCE variants always reload their ring-buffer
787 configuration when restarted, so we must reinitialize our ring
788 context before restarting. As part of this reinitialization,
789 find all packets still on the Tx ring and pretend that they had been
790 sent (in effect, drop the packets on the floor) - the higher-level
791 protocols will time out and retransmit. It'd be better to shuffle
792 these skbs to a temp list and then actually re-Tx them after
793 restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
797 lance_purge_ring(struct net_device *dev)
799 struct lance_private *lp = dev->priv;
802 /* Free all the skbuffs in the Rx and Tx queues. */
803 for (i = 0; i < RX_RING_SIZE; i++) {
804 struct sk_buff *skb = lp->rx_skbuff[i];
805 lp->rx_skbuff[i] = 0;
806 lp->rx_ring[i].base = 0; /* Not owned by LANCE chip. */
808 dev_kfree_skb_any(skb);
810 for (i = 0; i < TX_RING_SIZE; i++) {
811 if (lp->tx_skbuff[i]) {
812 dev_kfree_skb_any(lp->tx_skbuff[i]);
813 lp->tx_skbuff[i] = NULL;
819 /* Initialize the LANCE Rx and Tx rings. */
821 lance_init_ring(struct net_device *dev, int gfp)
823 struct lance_private *lp = dev->priv;
826 lp->cur_rx = lp->cur_tx = 0;
827 lp->dirty_rx = lp->dirty_tx = 0;
829 for (i = 0; i < RX_RING_SIZE; i++) {
833 skb = alloc_skb(PKT_BUF_SZ, GFP_DMA | gfp);
834 lp->rx_skbuff[i] = skb;
839 rx_buff = kmalloc(PKT_BUF_SZ, GFP_DMA | gfp);
841 lp->rx_ring[i].base = 0;
843 lp->rx_ring[i].base = (u32)isa_virt_to_bus(rx_buff) | 0x80000000;
844 lp->rx_ring[i].buf_length = -PKT_BUF_SZ;
846 /* The Tx buffer address is filled in as needed, but we do need to clear
847 the upper ownership bit. */
848 for (i = 0; i < TX_RING_SIZE; i++) {
849 lp->tx_skbuff[i] = 0;
850 lp->tx_ring[i].base = 0;
853 lp->init_block.mode = 0x0000;
854 for (i = 0; i < 6; i++)
855 lp->init_block.phys_addr[i] = dev->dev_addr[i];
856 lp->init_block.filter[0] = 0x00000000;
857 lp->init_block.filter[1] = 0x00000000;
858 lp->init_block.rx_ring = ((u32)isa_virt_to_bus(lp->rx_ring) & 0xffffff) | RX_RING_LEN_BITS;
859 lp->init_block.tx_ring = ((u32)isa_virt_to_bus(lp->tx_ring) & 0xffffff) | TX_RING_LEN_BITS;
863 lance_restart(struct net_device *dev, unsigned int csr0_bits, int must_reinit)
865 struct lance_private *lp = dev->priv;
868 (chip_table[lp->chip_version].flags & LANCE_MUST_REINIT_RING)) {
869 lance_purge_ring(dev);
870 lance_init_ring(dev, GFP_ATOMIC);
872 outw(0x0000, dev->base_addr + LANCE_ADDR);
873 outw(csr0_bits, dev->base_addr + LANCE_DATA);
877 static void lance_tx_timeout (struct net_device *dev)
879 struct lance_private *lp = (struct lance_private *) dev->priv;
880 int ioaddr = dev->base_addr;
882 outw (0, ioaddr + LANCE_ADDR);
883 printk ("%s: transmit timed out, status %4.4x, resetting.\n",
884 dev->name, inw (ioaddr + LANCE_DATA));
885 outw (0x0004, ioaddr + LANCE_DATA);
886 lp->stats.tx_errors++;
887 #ifndef final_version
888 if (lance_debug > 3) {
890 printk (" Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
891 lp->dirty_tx, lp->cur_tx, netif_queue_stopped(dev) ? " (full)" : "",
893 for (i = 0; i < RX_RING_SIZE; i++)
894 printk ("%s %08x %04x %04x", i & 0x3 ? "" : "\n ",
895 lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
896 lp->rx_ring[i].msg_length);
897 for (i = 0; i < TX_RING_SIZE; i++)
898 printk ("%s %08x %04x %04x", i & 0x3 ? "" : "\n ",
899 lp->tx_ring[i].base, -lp->tx_ring[i].length,
900 lp->tx_ring[i].misc);
904 lance_restart (dev, 0x0043, 1);
906 dev->trans_start = jiffies;
907 netif_wake_queue (dev);
911 static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
913 struct lance_private *lp = dev->priv;
914 int ioaddr = dev->base_addr;
918 spin_lock_irqsave(&lp->devlock, flags);
920 if (lance_debug > 3) {
921 outw(0x0000, ioaddr+LANCE_ADDR);
922 printk("%s: lance_start_xmit() called, csr0 %4.4x.\n", dev->name,
923 inw(ioaddr+LANCE_DATA));
924 outw(0x0000, ioaddr+LANCE_DATA);
927 /* Fill in a Tx ring entry */
929 /* Mask to ring buffer boundary. */
930 entry = lp->cur_tx & TX_RING_MOD_MASK;
932 /* Caution: the write order is important here, set the base address
933 with the "ownership" bits last. */
935 /* The old LANCE chips doesn't automatically pad buffers to min. size. */
936 if (chip_table[lp->chip_version].flags & LANCE_MUST_PAD) {
937 if (skb->len < ETH_ZLEN) {
938 skb = skb_padto(skb, ETH_ZLEN);
941 lp->tx_ring[entry].length = -ETH_ZLEN;
944 lp->tx_ring[entry].length = -skb->len;
946 lp->tx_ring[entry].length = -skb->len;
948 lp->tx_ring[entry].misc = 0x0000;
950 lp->stats.tx_bytes += skb->len;
952 /* If any part of this buffer is >16M we must copy it to a low-memory
954 if ((u32)isa_virt_to_bus(skb->data) + skb->len > 0x01000000) {
956 printk("%s: bouncing a high-memory packet (%#x).\n",
957 dev->name, (u32)isa_virt_to_bus(skb->data));
958 memcpy(&lp->tx_bounce_buffs[entry], skb->data, skb->len);
959 lp->tx_ring[entry].base =
960 ((u32)isa_virt_to_bus((lp->tx_bounce_buffs + entry)) & 0xffffff) | 0x83000000;
963 lp->tx_skbuff[entry] = skb;
964 lp->tx_ring[entry].base = ((u32)isa_virt_to_bus(skb->data) & 0xffffff) | 0x83000000;
968 /* Trigger an immediate send poll. */
969 outw(0x0000, ioaddr+LANCE_ADDR);
970 outw(0x0048, ioaddr+LANCE_DATA);
972 dev->trans_start = jiffies;
974 if ((lp->cur_tx - lp->dirty_tx) >= TX_RING_SIZE)
975 netif_stop_queue(dev);
978 spin_unlock_irqrestore(&lp->devlock, flags);
982 /* The LANCE interrupt handler. */
984 lance_interrupt(int irq, void *dev_id, struct pt_regs * regs)
986 struct net_device *dev = dev_id;
987 struct lance_private *lp;
988 int csr0, ioaddr, boguscnt=10;
992 printk ("lance_interrupt(): irq %d for unknown device.\n", irq);
996 ioaddr = dev->base_addr;
999 spin_lock (&lp->devlock);
1001 outw(0x00, dev->base_addr + LANCE_ADDR);
1002 while ((csr0 = inw(dev->base_addr + LANCE_DATA)) & 0x8600
1003 && --boguscnt >= 0) {
1004 /* Acknowledge all of the current interrupt sources ASAP. */
1005 outw(csr0 & ~0x004f, dev->base_addr + LANCE_DATA);
1009 if (lance_debug > 5)
1010 printk("%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
1011 dev->name, csr0, inw(dev->base_addr + LANCE_DATA));
1013 if (csr0 & 0x0400) /* Rx interrupt */
1016 if (csr0 & 0x0200) { /* Tx-done interrupt */
1017 int dirty_tx = lp->dirty_tx;
1019 while (dirty_tx < lp->cur_tx) {
1020 int entry = dirty_tx & TX_RING_MOD_MASK;
1021 int status = lp->tx_ring[entry].base;
1024 break; /* It still hasn't been Txed */
1026 lp->tx_ring[entry].base = 0;
1028 if (status & 0x40000000) {
1029 /* There was an major error, log it. */
1030 int err_status = lp->tx_ring[entry].misc;
1031 lp->stats.tx_errors++;
1032 if (err_status & 0x0400) lp->stats.tx_aborted_errors++;
1033 if (err_status & 0x0800) lp->stats.tx_carrier_errors++;
1034 if (err_status & 0x1000) lp->stats.tx_window_errors++;
1035 if (err_status & 0x4000) {
1036 /* Ackk! On FIFO errors the Tx unit is turned off! */
1037 lp->stats.tx_fifo_errors++;
1038 /* Remove this verbosity later! */
1039 printk("%s: Tx FIFO error! Status %4.4x.\n",
1041 /* Restart the chip. */
1045 if (status & 0x18000000)
1046 lp->stats.collisions++;
1047 lp->stats.tx_packets++;
1050 /* We must free the original skb if it's not a data-only copy
1051 in the bounce buffer. */
1052 if (lp->tx_skbuff[entry]) {
1053 dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1054 lp->tx_skbuff[entry] = 0;
1059 #ifndef final_version
1060 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1061 printk("out-of-sync dirty pointer, %d vs. %d, full=%s.\n",
1062 dirty_tx, lp->cur_tx,
1063 netif_queue_stopped(dev) ? "yes" : "no");
1064 dirty_tx += TX_RING_SIZE;
1068 /* if the ring is no longer full, accept more packets */
1069 if (netif_queue_stopped(dev) &&
1070 dirty_tx > lp->cur_tx - TX_RING_SIZE + 2)
1071 netif_wake_queue (dev);
1073 lp->dirty_tx = dirty_tx;
1076 /* Log misc errors. */
1077 if (csr0 & 0x4000) lp->stats.tx_errors++; /* Tx babble. */
1078 if (csr0 & 0x1000) lp->stats.rx_errors++; /* Missed a Rx frame. */
1079 if (csr0 & 0x0800) {
1080 printk("%s: Bus master arbitration failure, status %4.4x.\n",
1082 /* Restart the chip. */
1087 /* stop the chip to clear the error condition, then restart */
1088 outw(0x0000, dev->base_addr + LANCE_ADDR);
1089 outw(0x0004, dev->base_addr + LANCE_DATA);
1090 lance_restart(dev, 0x0002, 0);
1094 /* Clear any other interrupt, and set interrupt enable. */
1095 outw(0x0000, dev->base_addr + LANCE_ADDR);
1096 outw(0x7940, dev->base_addr + LANCE_DATA);
1098 if (lance_debug > 4)
1099 printk("%s: exiting interrupt, csr%d=%#4.4x.\n",
1100 dev->name, inw(ioaddr + LANCE_ADDR),
1101 inw(dev->base_addr + LANCE_DATA));
1103 spin_unlock (&lp->devlock);
1108 lance_rx(struct net_device *dev)
1110 struct lance_private *lp = dev->priv;
1111 int entry = lp->cur_rx & RX_RING_MOD_MASK;
1114 /* If we own the next entry, it's a new packet. Send it up. */
1115 while (lp->rx_ring[entry].base >= 0) {
1116 int status = lp->rx_ring[entry].base >> 24;
1118 if (status != 0x03) { /* There was an error. */
1119 /* There is a tricky error noted by John Murphy,
1120 <murf@perftech.com> to Russ Nelson: Even with full-sized
1121 buffers it's possible for a jabber packet to use two
1122 buffers, with only the last correctly noting the error. */
1123 if (status & 0x01) /* Only count a general error at the */
1124 lp->stats.rx_errors++; /* end of a packet.*/
1125 if (status & 0x20) lp->stats.rx_frame_errors++;
1126 if (status & 0x10) lp->stats.rx_over_errors++;
1127 if (status & 0x08) lp->stats.rx_crc_errors++;
1128 if (status & 0x04) lp->stats.rx_fifo_errors++;
1129 lp->rx_ring[entry].base &= 0x03ffffff;
1133 /* Malloc up new buffer, compatible with net3. */
1134 short pkt_len = (lp->rx_ring[entry].msg_length & 0xfff)-4;
1135 struct sk_buff *skb;
1139 printk("%s: Runt packet!\n",dev->name);
1140 lp->stats.rx_errors++;
1144 skb = dev_alloc_skb(pkt_len+2);
1147 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1148 for (i=0; i < RX_RING_SIZE; i++)
1149 if (lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].base < 0)
1152 if (i > RX_RING_SIZE -2)
1154 lp->stats.rx_dropped++;
1155 lp->rx_ring[entry].base |= 0x80000000;
1161 skb_reserve(skb,2); /* 16 byte align */
1162 skb_put(skb,pkt_len); /* Make room */
1163 eth_copy_and_sum(skb,
1164 (unsigned char *)isa_bus_to_virt((lp->rx_ring[entry].base & 0x00ffffff)),
1166 skb->protocol=eth_type_trans(skb,dev);
1168 dev->last_rx = jiffies;
1169 lp->stats.rx_packets++;
1170 lp->stats.rx_bytes+=pkt_len;
1173 /* The docs say that the buffer length isn't touched, but Andrew Boyd
1174 of QNX reports that some revs of the 79C965 clear it. */
1175 lp->rx_ring[entry].buf_length = -PKT_BUF_SZ;
1176 lp->rx_ring[entry].base |= 0x80000000;
1177 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1180 /* We should check that at least two ring entries are free. If not,
1181 we should free one and mark stats->rx_dropped++. */
1187 lance_close(struct net_device *dev)
1189 int ioaddr = dev->base_addr;
1190 struct lance_private *lp = dev->priv;
1192 netif_stop_queue (dev);
1194 if (chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1195 outw(112, ioaddr+LANCE_ADDR);
1196 lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
1198 outw(0, ioaddr+LANCE_ADDR);
1200 if (lance_debug > 1)
1201 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1202 dev->name, inw(ioaddr+LANCE_DATA));
1204 /* We stop the LANCE here -- it occasionally polls
1205 memory if we don't. */
1206 outw(0x0004, ioaddr+LANCE_DATA);
1210 unsigned long flags=claim_dma_lock();
1211 disable_dma(dev->dma);
1212 release_dma_lock(flags);
1214 free_irq(dev->irq, dev);
1216 lance_purge_ring(dev);
1221 static struct net_device_stats *lance_get_stats(struct net_device *dev)
1223 struct lance_private *lp = dev->priv;
1225 if (chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1226 short ioaddr = dev->base_addr;
1228 unsigned long flags;
1230 spin_lock_irqsave(&lp->devlock, flags);
1231 saved_addr = inw(ioaddr+LANCE_ADDR);
1232 outw(112, ioaddr+LANCE_ADDR);
1233 lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
1234 outw(saved_addr, ioaddr+LANCE_ADDR);
1235 spin_unlock_irqrestore(&lp->devlock, flags);
1241 /* Set or clear the multicast filter for this adaptor.
1244 static void set_multicast_list(struct net_device *dev)
1246 short ioaddr = dev->base_addr;
1248 outw(0, ioaddr+LANCE_ADDR);
1249 outw(0x0004, ioaddr+LANCE_DATA); /* Temporarily stop the lance. */
1251 if (dev->flags&IFF_PROMISC) {
1252 /* Log any net taps. */
1253 printk("%s: Promiscuous mode enabled.\n", dev->name);
1254 outw(15, ioaddr+LANCE_ADDR);
1255 outw(0x8000, ioaddr+LANCE_DATA); /* Set promiscuous mode */
1257 short multicast_table[4];
1259 int num_addrs=dev->mc_count;
1260 if(dev->flags&IFF_ALLMULTI)
1262 /* FIXIT: We don't use the multicast table, but rely on upper-layer filtering. */
1263 memset(multicast_table, (num_addrs == 0) ? 0 : -1, sizeof(multicast_table));
1264 for (i = 0; i < 4; i++) {
1265 outw(8 + i, ioaddr+LANCE_ADDR);
1266 outw(multicast_table[i], ioaddr+LANCE_DATA);
1268 outw(15, ioaddr+LANCE_ADDR);
1269 outw(0x0000, ioaddr+LANCE_DATA); /* Unset promiscuous mode */
1272 lance_restart(dev, 0x0142, 0); /* Resume normal operation */