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