blob: a4e5fab12628f6b578cd11ac5327fa61b3949779 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* lance.c: An AMD LANCE/PCnet ethernet driver for Linux. */
2/*
3 Written/copyright 1993-1998 by Donald Becker.
4
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.
9
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.
12
13 The author may be reached as becker@scyld.com, or C/O
14 Scyld Computing Corporation
15 410 Severn Ave., Suite 210
16 Annapolis MD 21403
17
18 Andrey V. Savochkin:
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
Jeff Garzik6aa20a22006-09-13 13:24:59 -040022 it worked only for the PCI chip.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 - 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>
Jeff Garzik6aa20a22006-09-13 13:24:59 -040034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 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
37
38 Get rid of check_region, check kmalloc return in lance_probe1
39 Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 11/01/2001
40
41 Reworked detection, added support for Racal InterLan EtherBlaster cards
42 Vesselin Kostadinov <vesok at yahoo dot com > - 22/4/2004
43*/
44
Andy Gospodarekd5b20692006-09-11 17:39:18 -040045static const char version[] = "lance.c:v1.16 2006/11/09 dplatt@3do.com, becker@cesdis.gsfc.nasa.gov\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/string.h>
50#include <linux/delay.h>
51#include <linux/errno.h>
52#include <linux/ioport.h>
53#include <linux/slab.h>
54#include <linux/interrupt.h>
55#include <linux/pci.h>
56#include <linux/init.h>
57#include <linux/netdevice.h>
58#include <linux/etherdevice.h>
59#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050060#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/bitops.h>
62
63#include <asm/io.h>
64#include <asm/dma.h>
65
66static unsigned int lance_portlist[] __initdata = { 0x300, 0x320, 0x340, 0x360, 0};
67static int lance_probe1(struct net_device *dev, int ioaddr, int irq, int options);
68static int __init do_lance_probe(struct net_device *dev);
69
70
71static struct card {
72 char id_offset14;
73 char id_offset15;
74} cards[] = {
75 { //"normal"
76 .id_offset14 = 0x57,
77 .id_offset15 = 0x57,
78 },
79 { //NI6510EB
80 .id_offset14 = 0x52,
81 .id_offset15 = 0x44,
82 },
83 { //Racal InterLan EtherBlaster
84 .id_offset14 = 0x52,
85 .id_offset15 = 0x49,
86 },
87};
88#define NUM_CARDS 3
89
90#ifdef LANCE_DEBUG
91static int lance_debug = LANCE_DEBUG;
92#else
93static int lance_debug = 1;
94#endif
95
96/*
97 Theory of Operation
98
99I. Board Compatibility
100
101This device driver is designed for the AMD 79C960, the "PCnet-ISA
102single-chip ethernet controller for ISA". This chip is used in a wide
103variety of boards from vendors such as Allied Telesis, HP, Kingston,
104and Boca. This driver is also intended to work with older AMD 7990
105designs, such as the NE1500 and NE2100, and newer 79C961. For convenience,
106I use the name LANCE to refer to all of the AMD chips, even though it properly
107refers only to the original 7990.
108
109II. Board-specific settings
110
111The driver is designed to work the boards that use the faster
112bus-master mode, rather than in shared memory mode. (Only older designs
113have on-board buffer memory needed to support the slower shared memory mode.)
114
115Most ISA boards have jumpered settings for the I/O base, IRQ line, and DMA
116channel. This driver probes the likely base addresses:
117{0x300, 0x320, 0x340, 0x360}.
118After the board is found it generates a DMA-timeout interrupt and uses
119autoIRQ to find the IRQ line. The DMA channel can be set with the low bits
120of the otherwise-unused dev->mem_start value (aka PARAM1). If unset it is
121probed for by enabling each free DMA channel in turn and checking if
122initialization succeeds.
123
124The HP-J2405A board is an exception: with this board it is easy to read the
125EEPROM-set values for the base, IRQ, and DMA. (Of course you must already
126_know_ the base address -- that field is for writing the EEPROM.)
127
128III. Driver operation
129
130IIIa. Ring buffers
131The LANCE uses ring buffers of Tx and Rx descriptors. Each entry describes
132the base and length of the data buffer, along with status bits. The length
133of these buffers is set by LANCE_LOG_{RX,TX}_BUFFERS, which is log_2() of
134the buffer length (rather than being directly the buffer length) for
135implementation ease. The current values are 2 (Tx) and 4 (Rx), which leads to
136ring sizes of 4 (Tx) and 16 (Rx). Increasing the number of ring entries
137needlessly uses extra space and reduces the chance that an upper layer will
138be able to reorder queued Tx packets based on priority. Decreasing the number
139of entries makes it more difficult to achieve back-to-back packet transmission
140and increases the chance that Rx ring will overflow. (Consider the worst case
141of receiving back-to-back minimum-sized packets.)
142
143The LANCE has the capability to "chain" both Rx and Tx buffers, but this driver
144statically allocates full-sized (slightly oversized -- PKT_BUF_SZ) buffers to
145avoid the administrative overhead. For the Rx side this avoids dynamically
146allocating full-sized buffers "just in case", at the expense of a
147memory-to-memory data copy for each packet received. For most systems this
148is a good tradeoff: the Rx buffer will always be in low memory, the copy
149is inexpensive, and it primes the cache for later packet processing. For Tx
150the buffers are only used when needed as low-memory bounce buffers.
151
152IIIB. 16M memory limitations.
153For the ISA bus master mode all structures used directly by the LANCE,
154the initialization block, Rx and Tx rings, and data buffers, must be
155accessible from the ISA bus, i.e. in the lower 16M of real memory.
156This is a problem for current Linux kernels on >16M machines. The network
157devices are initialized after memory initialization, and the kernel doles out
158memory from the top of memory downward. The current solution is to have a
159special network initialization routine that's called before memory
160initialization; this will eventually be generalized for all network devices.
161As mentioned before, low-memory "bounce-buffers" are used when needed.
162
163IIIC. Synchronization
164The driver runs as two independent, single-threaded flows of control. One
165is the send-packet routine, which enforces single-threaded use by the
166dev->tbusy flag. The other thread is the interrupt handler, which is single
167threaded by the hardware and other software.
168
169The send packet thread has partial control over the Tx ring and 'dev->tbusy'
170flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
171queue slot is empty, it clears the tbusy flag when finished otherwise it sets
172the 'lp->tx_full' flag.
173
174The interrupt handler has exclusive control over the Rx ring and records stats
175from the Tx ring. (The Tx-done interrupt can't be selectively turned off, so
176we can't avoid the interrupt overhead by having the Tx routine reap the Tx
177stats.) After reaping the stats, it marks the queue entry as empty by setting
178the 'base' to zero. Iff the 'lp->tx_full' flag is set, it clears both the
179tx_full and tbusy flags.
180
181*/
182
183/* Set the number of Tx and Rx buffers, using Log_2(# buffers).
184 Reasonable default values are 16 Tx buffers, and 16 Rx buffers.
185 That translates to 4 and 4 (16 == 2^^4).
186 This is a compile-time option for efficiency.
187 */
188#ifndef LANCE_LOG_TX_BUFFERS
189#define LANCE_LOG_TX_BUFFERS 4
190#define LANCE_LOG_RX_BUFFERS 4
191#endif
192
193#define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))
194#define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
195#define TX_RING_LEN_BITS ((LANCE_LOG_TX_BUFFERS) << 29)
196
197#define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))
198#define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
199#define RX_RING_LEN_BITS ((LANCE_LOG_RX_BUFFERS) << 29)
200
201#define PKT_BUF_SZ 1544
202
203/* Offsets from base I/O address. */
204#define LANCE_DATA 0x10
205#define LANCE_ADDR 0x12
206#define LANCE_RESET 0x14
207#define LANCE_BUS_IF 0x16
208#define LANCE_TOTAL_SIZE 0x18
209
210#define TX_TIMEOUT 20
211
212/* The LANCE Rx and Tx ring descriptors. */
213struct lance_rx_head {
214 s32 base;
215 s16 buf_length; /* This length is 2s complement (negative)! */
216 s16 msg_length; /* This length is "normal". */
217};
218
219struct lance_tx_head {
220 s32 base;
221 s16 length; /* Length is 2s complement (negative)! */
222 s16 misc;
223};
224
225/* The LANCE initialization block, described in databook. */
226struct lance_init_block {
227 u16 mode; /* Pre-set mode (reg. 15) */
228 u8 phys_addr[6]; /* Physical ethernet address */
229 u32 filter[2]; /* Multicast filter (unused). */
230 /* Receive and transmit ring base, along with extra bits. */
231 u32 rx_ring; /* Tx and Rx ring base pointers */
232 u32 tx_ring;
233};
234
235struct lance_private {
236 /* The Tx and Rx ring entries must be aligned on 8-byte boundaries. */
237 struct lance_rx_head rx_ring[RX_RING_SIZE];
238 struct lance_tx_head tx_ring[TX_RING_SIZE];
239 struct lance_init_block init_block;
240 const char *name;
241 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
242 struct sk_buff* tx_skbuff[TX_RING_SIZE];
243 /* The addresses of receive-in-place skbuffs. */
244 struct sk_buff* rx_skbuff[RX_RING_SIZE];
245 unsigned long rx_buffs; /* Address of Rx and Tx buffers. */
246 /* Tx low-memory "bounce buffer" address. */
247 char (*tx_bounce_buffs)[PKT_BUF_SZ];
248 int cur_rx, cur_tx; /* The next free ring entry */
249 int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
250 int dma;
251 struct net_device_stats stats;
252 unsigned char chip_version; /* See lance_chip_type. */
253 spinlock_t devlock;
254};
255
256#define LANCE_MUST_PAD 0x00000001
257#define LANCE_ENABLE_AUTOSELECT 0x00000002
258#define LANCE_MUST_REINIT_RING 0x00000004
259#define LANCE_MUST_UNRESET 0x00000008
260#define LANCE_HAS_MISSED_FRAME 0x00000010
261
262/* A mapping from the chip ID number to the part number and features.
263 These are from the datasheets -- in real life the '970 version
264 reportedly has the same ID as the '965. */
265static struct lance_chip_type {
266 int id_number;
267 const char *name;
268 int flags;
269} chip_table[] = {
270 {0x0000, "LANCE 7990", /* Ancient lance chip. */
271 LANCE_MUST_PAD + LANCE_MUST_UNRESET},
272 {0x0003, "PCnet/ISA 79C960", /* 79C960 PCnet/ISA. */
273 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
274 LANCE_HAS_MISSED_FRAME},
275 {0x2260, "PCnet/ISA+ 79C961", /* 79C961 PCnet/ISA+, Plug-n-Play. */
276 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
277 LANCE_HAS_MISSED_FRAME},
278 {0x2420, "PCnet/PCI 79C970", /* 79C970 or 79C974 PCnet-SCSI, PCI. */
279 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
280 LANCE_HAS_MISSED_FRAME},
281 /* Bug: the PCnet/PCI actually uses the PCnet/VLB ID number, so just call
282 it the PCnet32. */
283 {0x2430, "PCnet32", /* 79C965 PCnet for VL bus. */
284 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
285 LANCE_HAS_MISSED_FRAME},
286 {0x2621, "PCnet/PCI-II 79C970A", /* 79C970A PCInetPCI II. */
287 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
288 LANCE_HAS_MISSED_FRAME},
289 {0x0, "PCnet (unknown)",
290 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
291 LANCE_HAS_MISSED_FRAME},
292};
293
294enum {OLD_LANCE = 0, PCNET_ISA=1, PCNET_ISAP=2, PCNET_PCI=3, PCNET_VLB=4, PCNET_PCI_II=5, LANCE_UNKNOWN=6};
295
296
297/* Non-zero if lance_probe1() needs to allocate low-memory bounce buffers.
298 Assume yes until we know the memory size. */
299static unsigned char lance_need_isa_bounce_buffers = 1;
300
301static int lance_open(struct net_device *dev);
Al Viro9e249742005-10-21 03:22:29 -0400302static void lance_init_ring(struct net_device *dev, gfp_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev);
304static int lance_rx(struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100305static irqreturn_t lance_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int lance_close(struct net_device *dev);
307static struct net_device_stats *lance_get_stats(struct net_device *dev);
308static void set_multicast_list(struct net_device *dev);
309static void lance_tx_timeout (struct net_device *dev);
310
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#ifdef MODULE
314#define MAX_CARDS 8 /* Max number of interfaces (cards) per module */
315
316static struct net_device *dev_lance[MAX_CARDS];
317static int io[MAX_CARDS];
318static int dma[MAX_CARDS];
319static int irq[MAX_CARDS];
320
321module_param_array(io, int, NULL, 0);
322module_param_array(dma, int, NULL, 0);
323module_param_array(irq, int, NULL, 0);
324module_param(lance_debug, int, 0);
325MODULE_PARM_DESC(io, "LANCE/PCnet I/O base address(es),required");
326MODULE_PARM_DESC(dma, "LANCE/PCnet ISA DMA channel (ignored for some devices)");
327MODULE_PARM_DESC(irq, "LANCE/PCnet IRQ number (ignored for some devices)");
328MODULE_PARM_DESC(lance_debug, "LANCE/PCnet debug level (0-7)");
329
Andrew Morton3805f0e2006-08-14 23:00:03 -0700330int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct net_device *dev;
333 int this_dev, found = 0;
334
335 for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) {
336 if (io[this_dev] == 0) {
337 if (this_dev != 0) /* only complain once */
338 break;
339 printk(KERN_NOTICE "lance.c: Module autoprobing not allowed. Append \"io=0xNNN\" value(s).\n");
340 return -EPERM;
341 }
342 dev = alloc_etherdev(0);
343 if (!dev)
344 break;
345 dev->irq = irq[this_dev];
346 dev->base_addr = io[this_dev];
347 dev->dma = dma[this_dev];
348 if (do_lance_probe(dev) == 0) {
b1fc5502005-05-12 20:11:55 -0400349 dev_lance[found++] = dev;
350 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 free_netdev(dev);
353 break;
354 }
355 if (found != 0)
356 return 0;
357 return -ENXIO;
358}
359
Denis Vlasenko64916f12006-01-05 22:45:47 -0800360static void cleanup_card(struct net_device *dev)
361{
362 struct lance_private *lp = dev->priv;
363 if (dev->dma != 4)
364 free_dma(dev->dma);
365 release_region(dev->base_addr, LANCE_TOTAL_SIZE);
366 kfree(lp->tx_bounce_buffs);
367 kfree((void*)lp->rx_buffs);
368 kfree(lp);
369}
370
Al Viroafc8eb42006-06-14 18:50:53 -0400371void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 int this_dev;
374
375 for (this_dev = 0; this_dev < MAX_CARDS; this_dev++) {
376 struct net_device *dev = dev_lance[this_dev];
377 if (dev) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400378 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 cleanup_card(dev);
380 free_netdev(dev);
381 }
382 }
383}
384#endif /* MODULE */
385MODULE_LICENSE("GPL");
386
387
388/* Starting in v2.1.*, the LANCE/PCnet probe is now similar to the other
389 board probes now that kmalloc() can allocate ISA DMA-able regions.
390 This also allows the LANCE driver to be used as a module.
391 */
392static int __init do_lance_probe(struct net_device *dev)
393{
394 int *port, result;
395
396 if (high_memory <= phys_to_virt(16*1024*1024))
397 lance_need_isa_bounce_buffers = 0;
398
399 for (port = lance_portlist; *port; port++) {
400 int ioaddr = *port;
401 struct resource *r = request_region(ioaddr, LANCE_TOTAL_SIZE,
402 "lance-probe");
403
404 if (r) {
405 /* Detect the card with minimal I/O reads */
406 char offset14 = inb(ioaddr + 14);
407 int card;
408 for (card = 0; card < NUM_CARDS; ++card)
409 if (cards[card].id_offset14 == offset14)
410 break;
411 if (card < NUM_CARDS) {/*yes, the first byte matches*/
412 char offset15 = inb(ioaddr + 15);
413 for (card = 0; card < NUM_CARDS; ++card)
414 if ((cards[card].id_offset14 == offset14) &&
415 (cards[card].id_offset15 == offset15))
416 break;
417 }
418 if (card < NUM_CARDS) { /*Signature OK*/
419 result = lance_probe1(dev, ioaddr, 0, 0);
420 if (!result) {
421 struct lance_private *lp = dev->priv;
422 int ver = lp->chip_version;
423
424 r->name = chip_table[ver].name;
425 return 0;
426 }
427 }
428 release_region(ioaddr, LANCE_TOTAL_SIZE);
429 }
430 }
431 return -ENODEV;
432}
433
434#ifndef MODULE
435struct net_device * __init lance_probe(int unit)
436{
437 struct net_device *dev = alloc_etherdev(0);
438 int err;
439
440 if (!dev)
441 return ERR_PTR(-ENODEV);
442
443 sprintf(dev->name, "eth%d", unit);
444 netdev_boot_setup_check(dev);
445
446 err = do_lance_probe(dev);
447 if (err)
448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450out:
451 free_netdev(dev);
452 return ERR_PTR(err);
453}
454#endif
455
456static int __init lance_probe1(struct net_device *dev, int ioaddr, int irq, int options)
457{
458 struct lance_private *lp;
459 long dma_channels; /* Mark spuriously-busy DMA channels */
460 int i, reset_val, lance_version;
461 const char *chipname;
462 /* Flags for specific chips or boards. */
463 unsigned char hpJ2405A = 0; /* HP ISA adaptor */
464 int hp_builtin = 0; /* HP on-board ethernet. */
465 static int did_version; /* Already printed version info. */
466 unsigned long flags;
467 int err = -ENOMEM;
Al Viroc44fec12006-03-24 03:15:41 -0800468 void __iomem *bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* First we look for special cases.
471 Check for HP's on-board ethernet by looking for 'HP' in the BIOS.
472 There are two HP versions, check the BIOS for the configuration port.
473 This method provided by L. Julliard, Laurent_Julliard@grenoble.hp.com.
474 */
Al Viroc44fec12006-03-24 03:15:41 -0800475 bios = ioremap(0xf00f0, 0x14);
476 if (!bios)
477 return -ENOMEM;
478 if (readw(bios + 0x12) == 0x5048) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 static const short ioaddr_table[] = { 0x300, 0x320, 0x340, 0x360};
Al Viroc44fec12006-03-24 03:15:41 -0800480 int hp_port = (readl(bios + 1) & 1) ? 0x499 : 0x99;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* We can have boards other than the built-in! Verify this is on-board. */
482 if ((inb(hp_port) & 0xc0) == 0x80
483 && ioaddr_table[inb(hp_port) & 3] == ioaddr)
484 hp_builtin = hp_port;
485 }
Al Viroc44fec12006-03-24 03:15:41 -0800486 iounmap(bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* We also recognize the HP Vectra on-board here, but check below. */
488 hpJ2405A = (inb(ioaddr) == 0x08 && inb(ioaddr+1) == 0x00
489 && inb(ioaddr+2) == 0x09);
490
491 /* Reset the LANCE. */
492 reset_val = inw(ioaddr+LANCE_RESET); /* Reset the LANCE */
493
494 /* The Un-Reset needed is only needed for the real NE2100, and will
495 confuse the HP board. */
496 if (!hpJ2405A)
497 outw(reset_val, ioaddr+LANCE_RESET);
498
499 outw(0x0000, ioaddr+LANCE_ADDR); /* Switch to window 0 */
500 if (inw(ioaddr+LANCE_DATA) != 0x0004)
501 return -ENODEV;
502
503 /* Get the version of the chip. */
504 outw(88, ioaddr+LANCE_ADDR);
505 if (inw(ioaddr+LANCE_ADDR) != 88) {
506 lance_version = 0;
507 } else { /* Good, it's a newer chip. */
508 int chip_version = inw(ioaddr+LANCE_DATA);
509 outw(89, ioaddr+LANCE_ADDR);
510 chip_version |= inw(ioaddr+LANCE_DATA) << 16;
511 if (lance_debug > 2)
512 printk(" LANCE chip version is %#x.\n", chip_version);
513 if ((chip_version & 0xfff) != 0x003)
514 return -ENODEV;
515 chip_version = (chip_version >> 12) & 0xffff;
516 for (lance_version = 1; chip_table[lance_version].id_number; lance_version++) {
517 if (chip_table[lance_version].id_number == chip_version)
518 break;
519 }
520 }
521
522 /* We can't allocate dev->priv from alloc_etherdev() because it must
523 a ISA DMA-able region. */
524 SET_MODULE_OWNER(dev);
525 chipname = chip_table[lance_version].name;
526 printk("%s: %s at %#3x,", dev->name, chipname, ioaddr);
527
528 /* There is a 16 byte station address PROM at the base address.
529 The first six bytes are the station address. */
530 for (i = 0; i < 6; i++)
531 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
532
533 dev->base_addr = ioaddr;
534 /* Make certain the data structures used by the LANCE are aligned and DMAble. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400535
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700536 lp = kzalloc(sizeof(*lp), GFP_DMA | GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if(lp==NULL)
538 return -ENODEV;
539 if (lance_debug > 6) printk(" (#0x%05lx)", (unsigned long)lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 dev->priv = lp;
541 lp->name = chipname;
542 lp->rx_buffs = (unsigned long)kmalloc(PKT_BUF_SZ*RX_RING_SIZE,
543 GFP_DMA | GFP_KERNEL);
544 if (!lp->rx_buffs)
545 goto out_lp;
546 if (lance_need_isa_bounce_buffers) {
547 lp->tx_bounce_buffs = kmalloc(PKT_BUF_SZ*TX_RING_SIZE,
548 GFP_DMA | GFP_KERNEL);
549 if (!lp->tx_bounce_buffs)
550 goto out_rx;
551 } else
552 lp->tx_bounce_buffs = NULL;
553
554 lp->chip_version = lance_version;
555 spin_lock_init(&lp->devlock);
556
557 lp->init_block.mode = 0x0003; /* Disable Rx and Tx. */
558 for (i = 0; i < 6; i++)
559 lp->init_block.phys_addr[i] = dev->dev_addr[i];
560 lp->init_block.filter[0] = 0x00000000;
561 lp->init_block.filter[1] = 0x00000000;
562 lp->init_block.rx_ring = ((u32)isa_virt_to_bus(lp->rx_ring) & 0xffffff) | RX_RING_LEN_BITS;
563 lp->init_block.tx_ring = ((u32)isa_virt_to_bus(lp->tx_ring) & 0xffffff) | TX_RING_LEN_BITS;
564
565 outw(0x0001, ioaddr+LANCE_ADDR);
566 inw(ioaddr+LANCE_ADDR);
567 outw((short) (u32) isa_virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
568 outw(0x0002, ioaddr+LANCE_ADDR);
569 inw(ioaddr+LANCE_ADDR);
570 outw(((u32)isa_virt_to_bus(&lp->init_block)) >> 16, ioaddr+LANCE_DATA);
571 outw(0x0000, ioaddr+LANCE_ADDR);
572 inw(ioaddr+LANCE_ADDR);
573
574 if (irq) { /* Set iff PCI card. */
575 dev->dma = 4; /* Native bus-master, no DMA channel needed. */
576 dev->irq = irq;
577 } else if (hp_builtin) {
578 static const char dma_tbl[4] = {3, 5, 6, 0};
579 static const char irq_tbl[4] = {3, 4, 5, 9};
580 unsigned char port_val = inb(hp_builtin);
581 dev->dma = dma_tbl[(port_val >> 4) & 3];
582 dev->irq = irq_tbl[(port_val >> 2) & 3];
583 printk(" HP Vectra IRQ %d DMA %d.\n", dev->irq, dev->dma);
584 } else if (hpJ2405A) {
585 static const char dma_tbl[4] = {3, 5, 6, 7};
586 static const char irq_tbl[8] = {3, 4, 5, 9, 10, 11, 12, 15};
587 short reset_val = inw(ioaddr+LANCE_RESET);
588 dev->dma = dma_tbl[(reset_val >> 2) & 3];
589 dev->irq = irq_tbl[(reset_val >> 4) & 7];
590 printk(" HP J2405A IRQ %d DMA %d.\n", dev->irq, dev->dma);
591 } else if (lance_version == PCNET_ISAP) { /* The plug-n-play version. */
592 short bus_info;
593 outw(8, ioaddr+LANCE_ADDR);
594 bus_info = inw(ioaddr+LANCE_BUS_IF);
595 dev->dma = bus_info & 0x07;
596 dev->irq = (bus_info >> 4) & 0x0F;
597 } else {
598 /* The DMA channel may be passed in PARAM1. */
599 if (dev->mem_start & 0x07)
600 dev->dma = dev->mem_start & 0x07;
601 }
602
603 if (dev->dma == 0) {
604 /* Read the DMA channel status register, so that we can avoid
605 stuck DMA channels in the DMA detection below. */
606 dma_channels = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
607 (inb(DMA2_STAT_REG) & 0xf0);
608 }
609 err = -ENODEV;
610 if (dev->irq >= 2)
611 printk(" assigned IRQ %d", dev->irq);
612 else if (lance_version != 0) { /* 7990 boards need DMA detection first. */
613 unsigned long irq_mask;
614
615 /* To auto-IRQ we enable the initialization-done and DMA error
616 interrupts. For ISA boards we get a DMA error, but VLB and PCI
617 boards will work. */
618 irq_mask = probe_irq_on();
619
620 /* Trigger an initialization just for the interrupt. */
621 outw(0x0041, ioaddr+LANCE_DATA);
622
623 mdelay(20);
624 dev->irq = probe_irq_off(irq_mask);
625 if (dev->irq)
626 printk(", probed IRQ %d", dev->irq);
627 else {
628 printk(", failed to detect IRQ line.\n");
629 goto out_tx;
630 }
631
632 /* Check for the initialization done bit, 0x0100, which means
633 that we don't need a DMA channel. */
634 if (inw(ioaddr+LANCE_DATA) & 0x0100)
635 dev->dma = 4;
636 }
637
638 if (dev->dma == 4) {
639 printk(", no DMA needed.\n");
640 } else if (dev->dma) {
641 if (request_dma(dev->dma, chipname)) {
642 printk("DMA %d allocation failed.\n", dev->dma);
643 goto out_tx;
644 } else
645 printk(", assigned DMA %d.\n", dev->dma);
646 } else { /* OK, we have to auto-DMA. */
647 for (i = 0; i < 4; i++) {
648 static const char dmas[] = { 5, 6, 7, 3 };
649 int dma = dmas[i];
650 int boguscnt;
651
652 /* Don't enable a permanently busy DMA channel, or the machine
653 will hang. */
654 if (test_bit(dma, &dma_channels))
655 continue;
656 outw(0x7f04, ioaddr+LANCE_DATA); /* Clear the memory error bits. */
657 if (request_dma(dma, chipname))
658 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 flags=claim_dma_lock();
661 set_dma_mode(dma, DMA_MODE_CASCADE);
662 enable_dma(dma);
663 release_dma_lock(flags);
664
665 /* Trigger an initialization. */
666 outw(0x0001, ioaddr+LANCE_DATA);
667 for (boguscnt = 100; boguscnt > 0; --boguscnt)
668 if (inw(ioaddr+LANCE_DATA) & 0x0900)
669 break;
670 if (inw(ioaddr+LANCE_DATA) & 0x0100) {
671 dev->dma = dma;
672 printk(", DMA %d.\n", dev->dma);
673 break;
674 } else {
675 flags=claim_dma_lock();
676 disable_dma(dma);
677 release_dma_lock(flags);
678 free_dma(dma);
679 }
680 }
681 if (i == 4) { /* Failure: bail. */
682 printk("DMA detection failed.\n");
683 goto out_tx;
684 }
685 }
686
687 if (lance_version == 0 && dev->irq == 0) {
688 /* We may auto-IRQ now that we have a DMA channel. */
689 /* Trigger an initialization just for the interrupt. */
690 unsigned long irq_mask;
691
692 irq_mask = probe_irq_on();
693 outw(0x0041, ioaddr+LANCE_DATA);
694
695 mdelay(40);
696 dev->irq = probe_irq_off(irq_mask);
697 if (dev->irq == 0) {
698 printk(" Failed to detect the 7990 IRQ line.\n");
699 goto out_dma;
700 }
701 printk(" Auto-IRQ detected IRQ%d.\n", dev->irq);
702 }
703
704 if (chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
705 /* Turn on auto-select of media (10baseT or BNC) so that the user
706 can watch the LEDs even if the board isn't opened. */
707 outw(0x0002, ioaddr+LANCE_ADDR);
708 /* Don't touch 10base2 power bit. */
709 outw(inw(ioaddr+LANCE_BUS_IF) | 0x0002, ioaddr+LANCE_BUS_IF);
710 }
711
712 if (lance_debug > 0 && did_version++ == 0)
713 printk(version);
714
715 /* The LANCE-specific entries in the device structure. */
716 dev->open = lance_open;
717 dev->hard_start_xmit = lance_start_xmit;
718 dev->stop = lance_close;
719 dev->get_stats = lance_get_stats;
720 dev->set_multicast_list = set_multicast_list;
721 dev->tx_timeout = lance_tx_timeout;
722 dev->watchdog_timeo = TX_TIMEOUT;
723
b1fc5502005-05-12 20:11:55 -0400724 err = register_netdev(dev);
725 if (err)
726 goto out_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
728out_dma:
729 if (dev->dma != 4)
730 free_dma(dev->dma);
731out_tx:
732 kfree(lp->tx_bounce_buffs);
733out_rx:
734 kfree((void*)lp->rx_buffs);
735out_lp:
736 kfree(lp);
737 return err;
738}
739
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static int
742lance_open(struct net_device *dev)
743{
744 struct lance_private *lp = dev->priv;
745 int ioaddr = dev->base_addr;
746 int i;
747
748 if (dev->irq == 0 ||
749 request_irq(dev->irq, &lance_interrupt, 0, lp->name, dev)) {
750 return -EAGAIN;
751 }
752
753 /* We used to allocate DMA here, but that was silly.
754 DMA lines can't be shared! We now permanently allocate them. */
755
756 /* Reset the LANCE */
757 inw(ioaddr+LANCE_RESET);
758
759 /* The DMA controller is used as a no-operation slave, "cascade mode". */
760 if (dev->dma != 4) {
761 unsigned long flags=claim_dma_lock();
762 enable_dma(dev->dma);
763 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
764 release_dma_lock(flags);
765 }
766
767 /* Un-Reset the LANCE, needed only for the NE2100. */
768 if (chip_table[lp->chip_version].flags & LANCE_MUST_UNRESET)
769 outw(0, ioaddr+LANCE_RESET);
770
771 if (chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
772 /* This is 79C960-specific: Turn on auto-select of media (AUI, BNC). */
773 outw(0x0002, ioaddr+LANCE_ADDR);
774 /* Only touch autoselect bit. */
775 outw(inw(ioaddr+LANCE_BUS_IF) | 0x0002, ioaddr+LANCE_BUS_IF);
776 }
777
778 if (lance_debug > 1)
779 printk("%s: lance_open() irq %d dma %d tx/rx rings %#x/%#x init %#x.\n",
780 dev->name, dev->irq, dev->dma,
781 (u32) isa_virt_to_bus(lp->tx_ring),
782 (u32) isa_virt_to_bus(lp->rx_ring),
783 (u32) isa_virt_to_bus(&lp->init_block));
784
785 lance_init_ring(dev, GFP_KERNEL);
786 /* Re-initialize the LANCE, and start it when done. */
787 outw(0x0001, ioaddr+LANCE_ADDR);
788 outw((short) (u32) isa_virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
789 outw(0x0002, ioaddr+LANCE_ADDR);
790 outw(((u32)isa_virt_to_bus(&lp->init_block)) >> 16, ioaddr+LANCE_DATA);
791
792 outw(0x0004, ioaddr+LANCE_ADDR);
793 outw(0x0915, ioaddr+LANCE_DATA);
794
795 outw(0x0000, ioaddr+LANCE_ADDR);
796 outw(0x0001, ioaddr+LANCE_DATA);
797
798 netif_start_queue (dev);
799
800 i = 0;
801 while (i++ < 100)
802 if (inw(ioaddr+LANCE_DATA) & 0x0100)
803 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400804 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
806 * reports that doing so triggers a bug in the '974.
807 */
808 outw(0x0042, ioaddr+LANCE_DATA);
809
810 if (lance_debug > 2)
811 printk("%s: LANCE open after %d ticks, init block %#x csr0 %4.4x.\n",
812 dev->name, i, (u32) isa_virt_to_bus(&lp->init_block), inw(ioaddr+LANCE_DATA));
813
814 return 0; /* Always succeed */
815}
816
817/* The LANCE has been halted for one reason or another (busmaster memory
818 arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
819 etc.). Modern LANCE variants always reload their ring-buffer
820 configuration when restarted, so we must reinitialize our ring
821 context before restarting. As part of this reinitialization,
822 find all packets still on the Tx ring and pretend that they had been
823 sent (in effect, drop the packets on the floor) - the higher-level
824 protocols will time out and retransmit. It'd be better to shuffle
825 these skbs to a temp list and then actually re-Tx them after
826 restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
827*/
828
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400829static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830lance_purge_ring(struct net_device *dev)
831{
832 struct lance_private *lp = dev->priv;
833 int i;
834
835 /* Free all the skbuffs in the Rx and Tx queues. */
836 for (i = 0; i < RX_RING_SIZE; i++) {
837 struct sk_buff *skb = lp->rx_skbuff[i];
838 lp->rx_skbuff[i] = NULL;
839 lp->rx_ring[i].base = 0; /* Not owned by LANCE chip. */
840 if (skb)
841 dev_kfree_skb_any(skb);
842 }
843 for (i = 0; i < TX_RING_SIZE; i++) {
844 if (lp->tx_skbuff[i]) {
845 dev_kfree_skb_any(lp->tx_skbuff[i]);
846 lp->tx_skbuff[i] = NULL;
847 }
848 }
849}
850
851
852/* Initialize the LANCE Rx and Tx rings. */
853static void
Al Viro9e249742005-10-21 03:22:29 -0400854lance_init_ring(struct net_device *dev, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct lance_private *lp = dev->priv;
857 int i;
858
859 lp->cur_rx = lp->cur_tx = 0;
860 lp->dirty_rx = lp->dirty_tx = 0;
861
862 for (i = 0; i < RX_RING_SIZE; i++) {
863 struct sk_buff *skb;
864 void *rx_buff;
865
866 skb = alloc_skb(PKT_BUF_SZ, GFP_DMA | gfp);
867 lp->rx_skbuff[i] = skb;
868 if (skb) {
869 skb->dev = dev;
David S. Miller689be432005-06-28 15:25:31 -0700870 rx_buff = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 } else
872 rx_buff = kmalloc(PKT_BUF_SZ, GFP_DMA | gfp);
873 if (rx_buff == NULL)
874 lp->rx_ring[i].base = 0;
875 else
876 lp->rx_ring[i].base = (u32)isa_virt_to_bus(rx_buff) | 0x80000000;
877 lp->rx_ring[i].buf_length = -PKT_BUF_SZ;
878 }
879 /* The Tx buffer address is filled in as needed, but we do need to clear
880 the upper ownership bit. */
881 for (i = 0; i < TX_RING_SIZE; i++) {
882 lp->tx_skbuff[i] = NULL;
883 lp->tx_ring[i].base = 0;
884 }
885
886 lp->init_block.mode = 0x0000;
887 for (i = 0; i < 6; i++)
888 lp->init_block.phys_addr[i] = dev->dev_addr[i];
889 lp->init_block.filter[0] = 0x00000000;
890 lp->init_block.filter[1] = 0x00000000;
891 lp->init_block.rx_ring = ((u32)isa_virt_to_bus(lp->rx_ring) & 0xffffff) | RX_RING_LEN_BITS;
892 lp->init_block.tx_ring = ((u32)isa_virt_to_bus(lp->tx_ring) & 0xffffff) | TX_RING_LEN_BITS;
893}
894
895static void
896lance_restart(struct net_device *dev, unsigned int csr0_bits, int must_reinit)
897{
898 struct lance_private *lp = dev->priv;
899
900 if (must_reinit ||
901 (chip_table[lp->chip_version].flags & LANCE_MUST_REINIT_RING)) {
902 lance_purge_ring(dev);
903 lance_init_ring(dev, GFP_ATOMIC);
904 }
905 outw(0x0000, dev->base_addr + LANCE_ADDR);
906 outw(csr0_bits, dev->base_addr + LANCE_DATA);
907}
908
909
910static void lance_tx_timeout (struct net_device *dev)
911{
912 struct lance_private *lp = (struct lance_private *) dev->priv;
913 int ioaddr = dev->base_addr;
914
915 outw (0, ioaddr + LANCE_ADDR);
916 printk ("%s: transmit timed out, status %4.4x, resetting.\n",
917 dev->name, inw (ioaddr + LANCE_DATA));
918 outw (0x0004, ioaddr + LANCE_DATA);
919 lp->stats.tx_errors++;
920#ifndef final_version
921 if (lance_debug > 3) {
922 int i;
923 printk (" Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
924 lp->dirty_tx, lp->cur_tx, netif_queue_stopped(dev) ? " (full)" : "",
925 lp->cur_rx);
926 for (i = 0; i < RX_RING_SIZE; i++)
927 printk ("%s %08x %04x %04x", i & 0x3 ? "" : "\n ",
928 lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
929 lp->rx_ring[i].msg_length);
930 for (i = 0; i < TX_RING_SIZE; i++)
931 printk ("%s %08x %04x %04x", i & 0x3 ? "" : "\n ",
932 lp->tx_ring[i].base, -lp->tx_ring[i].length,
933 lp->tx_ring[i].misc);
934 printk ("\n");
935 }
936#endif
937 lance_restart (dev, 0x0043, 1);
938
939 dev->trans_start = jiffies;
940 netif_wake_queue (dev);
941}
942
943
944static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
945{
946 struct lance_private *lp = dev->priv;
947 int ioaddr = dev->base_addr;
948 int entry;
949 unsigned long flags;
950
951 spin_lock_irqsave(&lp->devlock, flags);
952
953 if (lance_debug > 3) {
954 outw(0x0000, ioaddr+LANCE_ADDR);
955 printk("%s: lance_start_xmit() called, csr0 %4.4x.\n", dev->name,
956 inw(ioaddr+LANCE_DATA));
957 outw(0x0000, ioaddr+LANCE_DATA);
958 }
959
960 /* Fill in a Tx ring entry */
961
962 /* Mask to ring buffer boundary. */
963 entry = lp->cur_tx & TX_RING_MOD_MASK;
964
965 /* Caution: the write order is important here, set the base address
966 with the "ownership" bits last. */
967
968 /* The old LANCE chips doesn't automatically pad buffers to min. size. */
969 if (chip_table[lp->chip_version].flags & LANCE_MUST_PAD) {
970 if (skb->len < ETH_ZLEN) {
Herbert Xu5b057c62006-06-23 02:06:41 -0700971 if (skb_padto(skb, ETH_ZLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 goto out;
973 lp->tx_ring[entry].length = -ETH_ZLEN;
974 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400975 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 lp->tx_ring[entry].length = -skb->len;
977 } else
978 lp->tx_ring[entry].length = -skb->len;
979
980 lp->tx_ring[entry].misc = 0x0000;
981
982 lp->stats.tx_bytes += skb->len;
983
984 /* If any part of this buffer is >16M we must copy it to a low-memory
985 buffer. */
986 if ((u32)isa_virt_to_bus(skb->data) + skb->len > 0x01000000) {
987 if (lance_debug > 5)
988 printk("%s: bouncing a high-memory packet (%#x).\n",
989 dev->name, (u32)isa_virt_to_bus(skb->data));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300990 skb_copy_from_linear_data(skb, &lp->tx_bounce_buffs[entry], skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 lp->tx_ring[entry].base =
992 ((u32)isa_virt_to_bus((lp->tx_bounce_buffs + entry)) & 0xffffff) | 0x83000000;
993 dev_kfree_skb(skb);
994 } else {
995 lp->tx_skbuff[entry] = skb;
996 lp->tx_ring[entry].base = ((u32)isa_virt_to_bus(skb->data) & 0xffffff) | 0x83000000;
997 }
998 lp->cur_tx++;
999
1000 /* Trigger an immediate send poll. */
1001 outw(0x0000, ioaddr+LANCE_ADDR);
1002 outw(0x0048, ioaddr+LANCE_DATA);
1003
1004 dev->trans_start = jiffies;
1005
1006 if ((lp->cur_tx - lp->dirty_tx) >= TX_RING_SIZE)
1007 netif_stop_queue(dev);
1008
1009out:
1010 spin_unlock_irqrestore(&lp->devlock, flags);
1011 return 0;
1012}
1013
1014/* The LANCE interrupt handler. */
David Howells7d12e782006-10-05 14:55:46 +01001015static irqreturn_t lance_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 struct net_device *dev = dev_id;
1018 struct lance_private *lp;
1019 int csr0, ioaddr, boguscnt=10;
1020 int must_restart;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ioaddr = dev->base_addr;
1023 lp = dev->priv;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 spin_lock (&lp->devlock);
1026
1027 outw(0x00, dev->base_addr + LANCE_ADDR);
1028 while ((csr0 = inw(dev->base_addr + LANCE_DATA)) & 0x8600
1029 && --boguscnt >= 0) {
1030 /* Acknowledge all of the current interrupt sources ASAP. */
1031 outw(csr0 & ~0x004f, dev->base_addr + LANCE_DATA);
1032
1033 must_restart = 0;
1034
1035 if (lance_debug > 5)
1036 printk("%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
1037 dev->name, csr0, inw(dev->base_addr + LANCE_DATA));
1038
1039 if (csr0 & 0x0400) /* Rx interrupt */
1040 lance_rx(dev);
1041
1042 if (csr0 & 0x0200) { /* Tx-done interrupt */
1043 int dirty_tx = lp->dirty_tx;
1044
1045 while (dirty_tx < lp->cur_tx) {
1046 int entry = dirty_tx & TX_RING_MOD_MASK;
1047 int status = lp->tx_ring[entry].base;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (status < 0)
1050 break; /* It still hasn't been Txed */
1051
1052 lp->tx_ring[entry].base = 0;
1053
1054 if (status & 0x40000000) {
1055 /* There was an major error, log it. */
1056 int err_status = lp->tx_ring[entry].misc;
1057 lp->stats.tx_errors++;
1058 if (err_status & 0x0400) lp->stats.tx_aborted_errors++;
1059 if (err_status & 0x0800) lp->stats.tx_carrier_errors++;
1060 if (err_status & 0x1000) lp->stats.tx_window_errors++;
1061 if (err_status & 0x4000) {
1062 /* Ackk! On FIFO errors the Tx unit is turned off! */
1063 lp->stats.tx_fifo_errors++;
1064 /* Remove this verbosity later! */
1065 printk("%s: Tx FIFO error! Status %4.4x.\n",
1066 dev->name, csr0);
1067 /* Restart the chip. */
1068 must_restart = 1;
1069 }
1070 } else {
1071 if (status & 0x18000000)
1072 lp->stats.collisions++;
1073 lp->stats.tx_packets++;
1074 }
1075
1076 /* We must free the original skb if it's not a data-only copy
1077 in the bounce buffer. */
1078 if (lp->tx_skbuff[entry]) {
1079 dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1080 lp->tx_skbuff[entry] = NULL;
1081 }
1082 dirty_tx++;
1083 }
1084
1085#ifndef final_version
1086 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1087 printk("out-of-sync dirty pointer, %d vs. %d, full=%s.\n",
1088 dirty_tx, lp->cur_tx,
1089 netif_queue_stopped(dev) ? "yes" : "no");
1090 dirty_tx += TX_RING_SIZE;
1091 }
1092#endif
1093
1094 /* if the ring is no longer full, accept more packets */
1095 if (netif_queue_stopped(dev) &&
1096 dirty_tx > lp->cur_tx - TX_RING_SIZE + 2)
1097 netif_wake_queue (dev);
1098
1099 lp->dirty_tx = dirty_tx;
1100 }
1101
1102 /* Log misc errors. */
1103 if (csr0 & 0x4000) lp->stats.tx_errors++; /* Tx babble. */
1104 if (csr0 & 0x1000) lp->stats.rx_errors++; /* Missed a Rx frame. */
1105 if (csr0 & 0x0800) {
1106 printk("%s: Bus master arbitration failure, status %4.4x.\n",
1107 dev->name, csr0);
1108 /* Restart the chip. */
1109 must_restart = 1;
1110 }
1111
1112 if (must_restart) {
1113 /* stop the chip to clear the error condition, then restart */
1114 outw(0x0000, dev->base_addr + LANCE_ADDR);
1115 outw(0x0004, dev->base_addr + LANCE_DATA);
1116 lance_restart(dev, 0x0002, 0);
1117 }
1118 }
1119
1120 /* Clear any other interrupt, and set interrupt enable. */
1121 outw(0x0000, dev->base_addr + LANCE_ADDR);
1122 outw(0x7940, dev->base_addr + LANCE_DATA);
1123
1124 if (lance_debug > 4)
1125 printk("%s: exiting interrupt, csr%d=%#4.4x.\n",
1126 dev->name, inw(ioaddr + LANCE_ADDR),
1127 inw(dev->base_addr + LANCE_DATA));
1128
1129 spin_unlock (&lp->devlock);
1130 return IRQ_HANDLED;
1131}
1132
1133static int
1134lance_rx(struct net_device *dev)
1135{
1136 struct lance_private *lp = dev->priv;
1137 int entry = lp->cur_rx & RX_RING_MOD_MASK;
1138 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* If we own the next entry, it's a new packet. Send it up. */
1141 while (lp->rx_ring[entry].base >= 0) {
1142 int status = lp->rx_ring[entry].base >> 24;
1143
1144 if (status != 0x03) { /* There was an error. */
1145 /* There is a tricky error noted by John Murphy,
1146 <murf@perftech.com> to Russ Nelson: Even with full-sized
1147 buffers it's possible for a jabber packet to use two
1148 buffers, with only the last correctly noting the error. */
1149 if (status & 0x01) /* Only count a general error at the */
1150 lp->stats.rx_errors++; /* end of a packet.*/
1151 if (status & 0x20) lp->stats.rx_frame_errors++;
1152 if (status & 0x10) lp->stats.rx_over_errors++;
1153 if (status & 0x08) lp->stats.rx_crc_errors++;
1154 if (status & 0x04) lp->stats.rx_fifo_errors++;
1155 lp->rx_ring[entry].base &= 0x03ffffff;
1156 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001157 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 {
1159 /* Malloc up new buffer, compatible with net3. */
1160 short pkt_len = (lp->rx_ring[entry].msg_length & 0xfff)-4;
1161 struct sk_buff *skb;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if(pkt_len<60)
1164 {
1165 printk("%s: Runt packet!\n",dev->name);
1166 lp->stats.rx_errors++;
1167 }
1168 else
1169 {
1170 skb = dev_alloc_skb(pkt_len+2);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001171 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 {
1173 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1174 for (i=0; i < RX_RING_SIZE; i++)
1175 if (lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].base < 0)
1176 break;
1177
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001178 if (i > RX_RING_SIZE -2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 {
1180 lp->stats.rx_dropped++;
1181 lp->rx_ring[entry].base |= 0x80000000;
1182 lp->cur_rx++;
1183 }
1184 break;
1185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 skb_reserve(skb,2); /* 16 byte align */
1187 skb_put(skb,pkt_len); /* Make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001188 skb_copy_to_linear_data(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 (unsigned char *)isa_bus_to_virt((lp->rx_ring[entry].base & 0x00ffffff)),
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001190 pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 skb->protocol=eth_type_trans(skb,dev);
1192 netif_rx(skb);
1193 dev->last_rx = jiffies;
1194 lp->stats.rx_packets++;
1195 lp->stats.rx_bytes+=pkt_len;
1196 }
1197 }
1198 /* The docs say that the buffer length isn't touched, but Andrew Boyd
1199 of QNX reports that some revs of the 79C965 clear it. */
1200 lp->rx_ring[entry].buf_length = -PKT_BUF_SZ;
1201 lp->rx_ring[entry].base |= 0x80000000;
1202 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1203 }
1204
1205 /* We should check that at least two ring entries are free. If not,
1206 we should free one and mark stats->rx_dropped++. */
1207
1208 return 0;
1209}
1210
1211static int
1212lance_close(struct net_device *dev)
1213{
1214 int ioaddr = dev->base_addr;
1215 struct lance_private *lp = dev->priv;
1216
1217 netif_stop_queue (dev);
1218
1219 if (chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1220 outw(112, ioaddr+LANCE_ADDR);
1221 lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
1222 }
1223 outw(0, ioaddr+LANCE_ADDR);
1224
1225 if (lance_debug > 1)
1226 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1227 dev->name, inw(ioaddr+LANCE_DATA));
1228
1229 /* We stop the LANCE here -- it occasionally polls
1230 memory if we don't. */
1231 outw(0x0004, ioaddr+LANCE_DATA);
1232
1233 if (dev->dma != 4)
1234 {
1235 unsigned long flags=claim_dma_lock();
1236 disable_dma(dev->dma);
1237 release_dma_lock(flags);
1238 }
1239 free_irq(dev->irq, dev);
1240
1241 lance_purge_ring(dev);
1242
1243 return 0;
1244}
1245
1246static struct net_device_stats *lance_get_stats(struct net_device *dev)
1247{
1248 struct lance_private *lp = dev->priv;
1249
1250 if (chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1251 short ioaddr = dev->base_addr;
1252 short saved_addr;
1253 unsigned long flags;
1254
1255 spin_lock_irqsave(&lp->devlock, flags);
1256 saved_addr = inw(ioaddr+LANCE_ADDR);
1257 outw(112, ioaddr+LANCE_ADDR);
1258 lp->stats.rx_missed_errors = inw(ioaddr+LANCE_DATA);
1259 outw(saved_addr, ioaddr+LANCE_ADDR);
1260 spin_unlock_irqrestore(&lp->devlock, flags);
1261 }
1262
1263 return &lp->stats;
1264}
1265
1266/* Set or clear the multicast filter for this adaptor.
1267 */
1268
1269static void set_multicast_list(struct net_device *dev)
1270{
1271 short ioaddr = dev->base_addr;
1272
1273 outw(0, ioaddr+LANCE_ADDR);
1274 outw(0x0004, ioaddr+LANCE_DATA); /* Temporarily stop the lance. */
1275
1276 if (dev->flags&IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 outw(15, ioaddr+LANCE_ADDR);
1278 outw(0x8000, ioaddr+LANCE_DATA); /* Set promiscuous mode */
1279 } else {
1280 short multicast_table[4];
1281 int i;
1282 int num_addrs=dev->mc_count;
1283 if(dev->flags&IFF_ALLMULTI)
1284 num_addrs=1;
1285 /* FIXIT: We don't use the multicast table, but rely on upper-layer filtering. */
1286 memset(multicast_table, (num_addrs == 0) ? 0 : -1, sizeof(multicast_table));
1287 for (i = 0; i < 4; i++) {
1288 outw(8 + i, ioaddr+LANCE_ADDR);
1289 outw(multicast_table[i], ioaddr+LANCE_DATA);
1290 }
1291 outw(15, ioaddr+LANCE_ADDR);
1292 outw(0x0000, ioaddr+LANCE_DATA); /* Unset promiscuous mode */
1293 }
1294
1295 lance_restart(dev, 0x0142, 0); /* Resume normal operation */
1296
1297}
1298