Import 1.3.87
[davej-history.git] / drivers / net / lance.c
blob155641da79656771e59ba8434f554246966dbf27
1 /* lance.c: An AMD LANCE ethernet driver for linux. */
2 /*
3 Written 1993,1994,1995 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
7 This software may be used and distributed according to the terms
8 of the GNU Public License, incorporated herein by reference.
10 This driver is for the Allied Telesis AT1500 and HP J2405A, and should work
11 with most other LANCE-based bus-master (NE2100 clone) ethercards.
13 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
14 Center of Excellence in Space Data and Information Sciences
15 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
18 Fixing alignment problem with 1.3.* kernel and some minor changes
19 by Andrey V. Savochkin, 1996.
21 Problems or questions may be send to Donald Becker (see above) or to
22 Andrey Savochkin -- saw@shade.msu.ru or
23 Laboratory of Computation Methods,
24 Department of Mathematics and Mechanics,
25 Moscow State University,
26 Leninskye Gory, Moscow 119899
28 But I should to inform you that I'm not an expert in the LANCE card
29 and it may occurs that you will receive no answer on your mail
30 to Donald Becker. I didn't receive any answer on all my letters
31 to him. Who knows why... But may be you are more lucky? ;-)
32 SAW
35 static const char*version ="lance.c:v1.08.02 Mar 17 1996 tsbogend@bigbug.franken.de\n";
37 #include <linux/config.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/string.h>
41 #include <linux/ptrace.h>
42 #include <linux/errno.h>
43 #include <linux/ioport.h>
44 #include <linux/malloc.h>
45 #include <linux/interrupt.h>
46 #include <linux/pci.h>
47 #include <linux/bios32.h>
48 #include <asm/bitops.h>
49 #include <asm/io.h>
50 #include <asm/dma.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/skbuff.h>
56 static unsigned int lance_portlist[] = {0x300,0x320,0x340,0x360,0};
57 voidlance_probe1(int ioaddr);
59 #ifdef HAVE_DEVLIST
60 struct netdev_entry lance_drv =
61 {"lance", lance_probe1, LANCE_TOTAL_SIZE, lance_portlist};
62 #endif
64 #ifdef LANCE_DEBUG
65 int lance_debug = LANCE_DEBUG;
66 #else
67 int lance_debug =1;
68 #endif
71 Theory of Operation
73 I. Board Compatibility
75 This device driver is designed for the AMD 79C960, the "PCnet-ISA
76 single-chip ethernet controller for ISA". This chip is used in a wide
77 variety of boards from vendors such as Allied Telesis, HP, Kingston,
78 and Boca. This driver is also intended to work with older AMD 7990
79 designs, such as the NE1500 and NE2100, and newer 79C961. For convenience,
80 I use the name LANCE to refer to all of the AMD chips, even though it properly
81 refers only to the original 7990.
83 II. Board-specific settings
85 The driver is designed to work the boards that use the faster
86 bus-master mode, rather than in shared memory mode. (Only older designs
87 have on-board buffer memory needed to support the slower shared memory mode.)
89 Most ISA boards have jumpered settings for the I/O base, IRQ line, and DMA
90 channel. This driver probes the likely base addresses:
91 {0x300, 0x320, 0x340, 0x360}.
92 After the board is found it generates a DMA-timeout interrupt and uses
93 autoIRQ to find the IRQ line. The DMA channel can be set with the low bits
94 of the otherwise-unused dev->mem_start value (aka PARAM1). If unset it is
95 probed for by enabling each free DMA channel in turn and checking if
96 initialization succeeds.
98 The HP-J2405A board is an exception: with this board it's easy to read the
99 EEPROM-set values for the base, IRQ, and DMA. (Of course you must already
100 _know_ the base address -- that field is for writing the EEPROM.)
102 III. Driver operation
104 IIIa. Ring buffers
105 The LANCE uses ring buffers of Tx and Rx descriptors. Each entry describes
106 the base and length of the data buffer, along with status bits. The length
107 of these buffers is set by LANCE_LOG_{RX,TX}_BUFFERS, which is log_2() of
108 the buffer length (rather than being directly the buffer length) for
109 implementation ease. The current values are 2 (Tx) and 4 (Rx), which leads to
110 ring sizes of 4 (Tx) and 16 (Rx). Increasing the number of ring entries
111 needlessly uses extra space and reduces the chance that an upper layer will
112 be able to reorder queued Tx packets based on priority. Decreasing the number
113 of entries makes it more difficult to achieve back-to-back packet transmission
114 and increases the chance that Rx ring will overflow. (Consider the worst case
115 of receiving back-to-back minimum-sized packets.)
117 The LANCE has the capability to "chain" both Rx and Tx buffers, but this driver
118 statically allocates full-sized (slightly oversized -- PKT_BUF_SZ) buffers to
119 avoid the administrative overhead. For the Rx side this avoids dynamically
120 allocating full-sized buffers "just in case", at the expense of a
121 memory-to-memory data copy for each packet received. For most systems this
122 is a good tradeoff: the Rx buffer will always be in low memory, the copy
123 is inexpensive, and it primes the cache for later packet processing. For Tx
124 the buffers are only used when needed as low-memory bounce buffers.
126 IIIB. 16M memory limitations.
127 For the ISA bus master mode all structures used directly by the LANCE,
128 the initialization block, Rx and Tx rings, and data buffers, must be
129 accessible from the ISA bus, i.e. in the lower 16M of real memory.
130 This is a problem for current Linux kernels on >16M machines. The network
131 devices are initialized after memory initialization, and the kernel doles out
132 memory from the top of memory downward. The current solution is to have a
133 special network initialization routine that's called before memory
134 initialization; this will eventually be generalized for all network devices.
135 As mentioned before, low-memory "bounce-buffers" are used when needed.
137 IIIC. Synchronization
138 The driver runs as two independent, single-threaded flows of control. One
139 is the send-packet routine, which enforces single-threaded use by the
140 dev->tbusy flag. The other thread is the interrupt handler, which is single
141 threaded by the hardware and other software.
143 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
144 flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
145 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
146 the 'lp->tx_full' flag.
148 The interrupt handler has exclusive control over the Rx ring and records stats
149 from the Tx ring. (The Tx-done interrupt can't be selectively turned off, so
150 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
151 stats.) After reaping the stats, it marks the queue entry as empty by setting
152 the 'base' to zero. Iff the 'lp->tx_full' flag is set, it clears both the
153 tx_full and tbusy flags.
157 /* Memory accessed from LANCE card must be aligned on 8-byte boundaries.
158 But we can't believe that kmalloc()'ed memory satisfies it. -- SAW */
159 #define LANCE_KMALLOC(x) \
160 ((void *) (((unsigned long)kmalloc((x)+7, GFP_DMA | GFP_KERNEL)+7) & ~7))
163 * Changes:
164 * Thomas Bogendoerfer (tsbogend@bigbug.franken.de):
165 * - added support for Linux/Alpha, but removed most of it, because
166 * it worked only for the PCI chip.
167 * - added hook for the 32bit lance driver
170 /* Set the number of Tx and Rx buffers, using Log_2(# buffers).
171 Reasonable default values are 16 Tx buffers, and 16 Rx buffers.
172 That translates to 4 and 4 (16 == 2^^4). */
173 #ifndef LANCE_LOG_TX_BUFFERS
174 #define LANCE_LOG_TX_BUFFERS 4
175 #define LANCE_LOG_RX_BUFFERS 4
176 #endif
178 #define TX_RING_SIZE (1 << (LANCE_LOG_TX_BUFFERS))
179 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
180 #define TX_RING_LEN_BITS ((LANCE_LOG_TX_BUFFERS) << 29)
182 #define RX_RING_SIZE (1 << (LANCE_LOG_RX_BUFFERS))
183 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
184 #define RX_RING_LEN_BITS ((LANCE_LOG_RX_BUFFERS) << 29)
186 #define PKT_BUF_SZ 1544
188 /* Offsets from base I/O address. */
189 #define LANCE_DATA 0x10
190 #define LANCE_ADDR 0x12
191 #define LANCE_RESET 0x14
192 #define LANCE_BUS_IF 0x16
193 #define LANCE_TOTAL_SIZE 0x18
195 /* The LANCE Rx and Tx ring descriptors. */
196 struct lance_rx_head {
197 s32 base;
198 s16 buf_length;/* This length is 2s complement (negative)! */
199 s16 msg_length;/* This length is "normal". */
202 struct lance_tx_head {
203 s32 base;
204 s16 length;/* Length is 2s complement (negative)! */
205 s16 misc;
208 /* The LANCE initialization block, described in databook. */
209 struct lance_init_block {
210 u16 mode;/* Pre-set mode (reg. 15) */
211 u8 phys_addr[6];/* Physical ethernet address */
212 u32 filter[2];/* Multicast filter (unused). */
213 /* Receive and transmit ring base, along with extra bits. */
214 u32 rx_ring;/* Tx and Rx ring base pointers */
215 u32 tx_ring;
218 struct lance_private {
219 /* The Tx and Rx ring entries must be aligned on 8-byte boundaries. */
220 struct lance_rx_head rx_ring[RX_RING_SIZE];
221 struct lance_tx_head tx_ring[TX_RING_SIZE];
222 struct lance_init_block init_block;
223 const char*name;
224 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
225 struct sk_buff* tx_skbuff[TX_RING_SIZE];
226 unsigned long rx_buffs;/* Address of Rx and Tx buffers. */
227 /* Tx low-memory "bounce buffer" address. */
228 char(*tx_bounce_buffs)[PKT_BUF_SZ];
229 int cur_rx, cur_tx;/* The next free ring entry */
230 int dirty_rx, dirty_tx;/* The ring entries to be free()ed. */
231 int dma;
232 struct enet_statistics stats;
233 unsigned char chip_version;/* See lance_chip_type. */
234 char tx_full;
235 unsigned long lock;
238 #define LANCE_MUST_PAD 0x00000001
239 #define LANCE_ENABLE_AUTOSELECT 0x00000002
240 #define LANCE_MUST_REINIT_RING 0x00000004
241 #define LANCE_MUST_UNRESET 0x00000008
242 #define LANCE_HAS_MISSED_FRAME 0x00000010
244 /* A mapping from the chip ID number to the part number and features.
245 These are from the datasheets -- in real life the '970 version
246 reportedly has the same ID as the '965. */
247 static struct lance_chip_type {
248 int id_number;
249 const char*name;
250 int flags;
251 } chip_table[] = {
252 {0x0000,"LANCE 7990",/* Ancient lance chip. */
253 LANCE_MUST_PAD + LANCE_MUST_UNRESET},
254 {0x0003,"PCnet/ISA 79C960",/* 79C960 PCnet/ISA. */
255 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
256 LANCE_HAS_MISSED_FRAME},
257 {0x2260,"PCnet/ISA+ 79C961",/* 79C961 PCnet/ISA+, Plug-n-Play. */
258 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
259 LANCE_HAS_MISSED_FRAME},
260 {0x2420,"PCnet/PCI 79C970",/* 79C970 or 79C974 PCnet-SCSI, PCI. */
261 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
262 LANCE_HAS_MISSED_FRAME},
263 /* Bug: the PCnet/PCI actually uses the PCnet/VLB ID number, so just call
264 it the PCnet32. */
265 {0x2430,"PCnet32",/* 79C965 PCnet for VL bus. */
266 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
267 LANCE_HAS_MISSED_FRAME},
268 {0x0,"PCnet (unknown)",
269 LANCE_ENABLE_AUTOSELECT + LANCE_MUST_REINIT_RING +
270 LANCE_HAS_MISSED_FRAME},
273 enum{OLD_LANCE =0, PCNET_ISA=1, PCNET_ISAP=2, PCNET_PCI=3, PCNET_VLB=4, LANCE_UNKNOWN=5};
275 /* Non-zero only if the current card is a PCI with BIOS-set IRQ. */
276 static unsigned char pci_irq_line =0;
278 /* Non-zero if lance_probe1() needs to allocate low-memory bounce buffers.
279 Assume yes until we know the memory size. */
280 static unsigned char lance_need_isa_bounce_buffers =1;
282 static intlance_open(struct device *dev);
283 static voidlance_init_ring(struct device *dev);
284 static intlance_start_xmit(struct sk_buff *skb,struct device *dev);
285 static intlance_rx(struct device *dev);
286 static voidlance_interrupt(int irq,void*dev_id,struct pt_regs *regs);
287 static intlance_close(struct device *dev);
288 static struct enet_statistics *lance_get_stats(struct device *dev);
289 static voidset_multicast_list(struct device *dev);
293 /* This lance probe is unlike the other board probes in 1.0.*. The LANCE may
294 have to allocate a contiguous low-memory region for bounce buffers.
295 This requirement is satisfied by having the lance initialization occur
296 before the memory management system is started, and thus well before the
297 other probes. */
299 intlance_init(void)
301 #ifndef __alpha__
302 int*port;
303 #endif
305 if(high_memory <=16*1024*1024)
306 lance_need_isa_bounce_buffers =0;
308 #if defined(CONFIG_PCI)
309 if(pcibios_present()) {
310 int pci_index;
311 printk("lance.c: PCI bios is present, checking for devices...\n");
312 for(pci_index =0; pci_index <8; pci_index++) {
313 unsigned char pci_bus, pci_device_fn;
314 unsigned int pci_ioaddr;
315 unsigned short pci_command;
317 if(pcibios_find_device(PCI_VENDOR_ID_AMD,
318 PCI_DEVICE_ID_AMD_LANCE, pci_index,
319 &pci_bus, &pci_device_fn) !=0)
320 break;
321 pcibios_read_config_byte(pci_bus, pci_device_fn,
322 PCI_INTERRUPT_LINE, &pci_irq_line);
323 pcibios_read_config_dword(pci_bus, pci_device_fn,
324 PCI_BASE_ADDRESS_0, &pci_ioaddr);
325 /* Remove I/O space marker in bit 0. */
326 pci_ioaddr &= ~3;
327 /* PCI Spec 2.1 states that it is either the driver or PCI card's
328 * responsibility to set the PCI Master Enable Bit if needed.
329 * (From Mark Stockton <marks@schooner.sys.hou.compaq.com>)
331 pcibios_read_config_word(pci_bus, pci_device_fn,
332 PCI_COMMAND, &pci_command);
333 if( ! (pci_command & PCI_COMMAND_MASTER)) {
334 printk("PCI Master Bit has not been set. Setting...\n");
335 pci_command |= PCI_COMMAND_MASTER;
336 pcibios_write_config_word(pci_bus, pci_device_fn,
337 PCI_COMMAND, pci_command);
339 printk("Found PCnet/PCI at %#x, irq %d.\n",
340 pci_ioaddr, pci_irq_line);
341 lance_probe1(pci_ioaddr);
342 pci_irq_line =0;
345 #endif/* defined(CONFIG_PCI) */
347 /* On the Alpha don't look for PCnet chips on the ISA bus */
348 #ifndef __alpha__
349 for(port = lance_portlist; *port; port++) {
350 int ioaddr = *port;
352 if(check_region(ioaddr, LANCE_TOTAL_SIZE) ==0) {
353 /* Detect "normal" 0x57 0x57 and the NI6510EB 0x52 0x44
354 signatures w/ minimal I/O reads */
355 char offset15, offset14 =inb(ioaddr +14);
357 if((offset14 ==0x52|| offset14 ==0x57) &&
358 ((offset15 =inb(ioaddr +15)) ==0x57|| offset15 ==0x44))
359 lance_probe1(ioaddr);
362 #endif
364 return0;
367 voidlance_probe1(int ioaddr)
369 struct device *dev;
370 struct lance_private *lp;
371 short dma_channels;/* Mark spuriously-busy DMA channels */
372 int i, reset_val, lance_version;
373 const char*chipname;
374 /* Flags for specific chips or boards. */
375 unsigned char hpJ2405A =0;/* HP ISA adaptor */
376 int hp_builtin =0;/* HP on-board ethernet. */
377 static int did_version =0;/* Already printed version info. */
379 #ifndef __alpha__
380 /* First we look for special cases.
381 Check for HP's on-board ethernet by looking for 'HP' in the BIOS.
382 There are two HP versions, check the BIOS for the configuration port.
383 This method provided by L. Julliard, Laurent_Julliard@grenoble.hp.com.
385 if( *((unsigned short*)0x000f0102) ==0x5048) {
386 static const short ioaddr_table[] = {0x300,0x320,0x340,0x360};
387 int hp_port = ( *((unsigned char*)0x000f00f1) &1) ?0x499:0x99;
388 /* We can have boards other than the built-in! Verify this is on-board. */
389 if((inb(hp_port) &0xc0) ==0x80
390 && ioaddr_table[inb(hp_port) &3] == ioaddr)
391 hp_builtin = hp_port;
393 /* We also recognize the HP Vectra on-board here, but check below. */
394 hpJ2405A = (inb(ioaddr) ==0x08&&inb(ioaddr+1) ==0x00
395 &&inb(ioaddr+2) ==0x09);
396 #endif
398 /* Reset the LANCE. */
399 reset_val =inw(ioaddr+LANCE_RESET);/* Reset the LANCE */
401 /* The Un-Reset needed is only needed for the real NE2100, and will
402 confuse the HP board. */
403 if(!hpJ2405A)
404 outw(reset_val, ioaddr+LANCE_RESET);
406 outw(0x0000, ioaddr+LANCE_ADDR);/* Switch to window 0 */
407 if(inw(ioaddr+LANCE_DATA) !=0x0004)
408 return;
410 /* Get the version of the chip. */
411 outw(88, ioaddr+LANCE_ADDR);
412 if(inw(ioaddr+LANCE_ADDR) !=88) {
413 lance_version =0;
414 }else{/* Good, it's a newer chip. */
415 int chip_version =inw(ioaddr+LANCE_DATA);
416 outw(89, ioaddr+LANCE_ADDR);
417 chip_version |=inw(ioaddr+LANCE_DATA) <<16;
418 if(lance_debug >2)
419 printk(" LANCE chip version is %#x.\n", chip_version);
420 if((chip_version &0xfff) !=0x003)
421 return;
422 chip_version = (chip_version >>12) &0xffff;
423 for(lance_version =1; chip_table[lance_version].id_number; lance_version++) {
424 if(chip_table[lance_version].id_number == chip_version)
425 break;
429 dev =init_etherdev(0,0);
430 chipname = chip_table[lance_version].name;
431 printk("%s: %s at %#3x,", dev->name, chipname, ioaddr);
433 /* There is a 16 byte station address PROM at the base address.
434 The first six bytes are the station address. */
435 for(i =0; i <6; i++)
436 printk(" %2.2x", dev->dev_addr[i] =inb(ioaddr + i));
438 dev->base_addr = ioaddr;
439 request_region(ioaddr, LANCE_TOTAL_SIZE, chip_table[lance_version].name);
441 #ifdef CONFIG_LANCE32
442 /* look if it's a PCI or VLB chip */
443 if(lance_version == PCNET_PCI || lance_version == PCNET_VLB) {
444 externvoidlance32_probe1(struct device *dev,const char*chipname,int pci_irq_line);
446 lance32_probe1(dev, chipname, pci_irq_line);
447 return;
449 #endif
450 /* Make certain the data structures used by the LANCE are aligned and DMAble. */
451 lp = (struct lance_private *)LANCE_KMALLOC(sizeof(*lp));
452 if(lance_debug >6)printk(" (#0x%05lx)", (unsigned long)lp);
453 memset(lp,0,sizeof(*lp));
454 dev->priv = lp;
455 lp->name = chipname;
456 /* I'm not sure that buffs also must be aligned but it's safer to do it -- SAW */
457 lp->rx_buffs = (unsigned long)LANCE_KMALLOC(PKT_BUF_SZ*RX_RING_SIZE);
458 lp->tx_bounce_buffs = NULL;
459 if(lance_need_isa_bounce_buffers)
460 lp->tx_bounce_buffs =LANCE_KMALLOC(PKT_BUF_SZ*TX_RING_SIZE);
462 lp->chip_version = lance_version;
464 lp->init_block.mode =0x0003;/* Disable Rx and Tx. */
465 for(i =0; i <6; i++)
466 lp->init_block.phys_addr[i] = dev->dev_addr[i];
467 lp->init_block.filter[0] =0x00000000;
468 lp->init_block.filter[1] =0x00000000;
469 lp->init_block.rx_ring = ((u32)virt_to_bus(lp->rx_ring) &0xffffff) | RX_RING_LEN_BITS;
470 lp->init_block.tx_ring = ((u32)virt_to_bus(lp->tx_ring) &0xffffff) | TX_RING_LEN_BITS;
472 outw(0x0001, ioaddr+LANCE_ADDR);
473 inw(ioaddr+LANCE_ADDR);
474 outw((short) (u32)virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
475 outw(0x0002, ioaddr+LANCE_ADDR);
476 inw(ioaddr+LANCE_ADDR);
477 outw(((u32)virt_to_bus(&lp->init_block)) >>16, ioaddr+LANCE_DATA);
478 outw(0x0000, ioaddr+LANCE_ADDR);
479 inw(ioaddr+LANCE_ADDR);
481 if(pci_irq_line) {
482 dev->dma =4;/* Native bus-master, no DMA channel needed. */
483 dev->irq = pci_irq_line;
484 }else if(hp_builtin) {
485 static const char dma_tbl[4] = {3,5,6,0};
486 static const char irq_tbl[4] = {3,4,5,9};
487 unsigned char port_val =inb(hp_builtin);
488 dev->dma = dma_tbl[(port_val >>4) &3];
489 dev->irq = irq_tbl[(port_val >>2) &3];
490 printk(" HP Vectra IRQ %d DMA %d.\n", dev->irq, dev->dma);
491 }else if(hpJ2405A) {
492 static const char dma_tbl[4] = {3,5,6,7};
493 static const char irq_tbl[8] = {3,4,5,9,10,11,12,15};
494 short reset_val =inw(ioaddr+LANCE_RESET);
495 dev->dma = dma_tbl[(reset_val >>2) &3];
496 dev->irq = irq_tbl[(reset_val >>4) &7];
497 printk(" HP J2405A IRQ %d DMA %d.\n", dev->irq, dev->dma);
498 }else if(lance_version == PCNET_ISAP) {/* The plug-n-play version. */
499 short bus_info;
500 outw(8, ioaddr+LANCE_ADDR);
501 bus_info =inw(ioaddr+LANCE_BUS_IF);
502 dev->dma = bus_info &0x07;
503 dev->irq = (bus_info >>4) &0x0F;
504 }else{
505 /* The DMA channel may be passed in PARAM1. */
506 if(dev->mem_start &0x07)
507 dev->dma = dev->mem_start &0x07;
510 if(dev->dma ==0) {
511 /* Read the DMA channel status register, so that we can avoid
512 stuck DMA channels in the DMA detection below. */
513 dma_channels = ((inb(DMA1_STAT_REG) >>4) &0x0f) |
514 (inb(DMA2_STAT_REG) &0xf0);
516 if(dev->irq >=2)
517 printk(" assigned IRQ %d", dev->irq);
518 else{
519 /* To auto-IRQ we enable the initialization-done and DMA error
520 interrupts. For ISA boards we get a DMA error, but VLB and PCI
521 boards will work. */
522 autoirq_setup(0);
524 /* Trigger an initialization just for the interrupt. */
525 outw(0x0041, ioaddr+LANCE_DATA);
527 dev->irq =autoirq_report(1);
528 if(dev->irq)
529 printk(", probed IRQ %d", dev->irq);
530 else{
531 printk(", failed to detect IRQ line.\n");
532 return;
535 /* Check for the initialization done bit, 0x0100, which means
536 that we don't need a DMA channel. */
537 if(inw(ioaddr+LANCE_DATA) &0x0100)
538 dev->dma =4;
541 if(dev->dma ==4) {
542 printk(", no DMA needed.\n");
543 }else if(dev->dma) {
544 if(request_dma(dev->dma, chipname)) {
545 printk("DMA %d allocation failed.\n", dev->dma);
546 return;
547 }else
548 printk(", assigned DMA %d.\n", dev->dma);
549 }else{/* OK, we have to auto-DMA. */
550 for(i =0; i <4; i++) {
551 static const char dmas[] = {5,6,7,3};
552 int dma = dmas[i];
553 int boguscnt;
555 /* Don't enable a permanently busy DMA channel, or the machine
556 will hang. */
557 if(test_bit(dma, &dma_channels))
558 continue;
559 outw(0x7f04, ioaddr+LANCE_DATA);/* Clear the memory error bits. */
560 if(request_dma(dma, chipname))
561 continue;
562 set_dma_mode(dma, DMA_MODE_CASCADE);
563 enable_dma(dma);
565 /* Trigger an initialization. */
566 outw(0x0001, ioaddr+LANCE_DATA);
567 for(boguscnt =100; boguscnt >0; --boguscnt)
568 if(inw(ioaddr+LANCE_DATA) &0x0900)
569 break;
570 if(inw(ioaddr+LANCE_DATA) &0x0100) {
571 dev->dma = dma;
572 printk(", DMA %d.\n", dev->dma);
573 break;
574 }else{
575 disable_dma(dma);
576 free_dma(dma);
579 if(i ==4) {/* Failure: bail. */
580 printk("DMA detection failed.\n");
581 return;
585 if(chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
586 /* Turn on auto-select of media (10baseT or BNC) so that the user
587 can watch the LEDs even if the board isn't opened. */
588 outw(0x0002, ioaddr+LANCE_ADDR);
589 outw(0x0002, ioaddr+LANCE_BUS_IF);
592 if(lance_debug >0&& did_version++ ==0)
593 printk(version);
595 /* The LANCE-specific entries in the device structure. */
596 dev->open = &lance_open;
597 dev->hard_start_xmit = &lance_start_xmit;
598 dev->stop = &lance_close;
599 dev->get_stats = &lance_get_stats;
600 dev->set_multicast_list = &set_multicast_list;
602 return;
606 static int
607 lance_open(struct device *dev)
609 struct lance_private *lp = (struct lance_private *)dev->priv;
610 int ioaddr = dev->base_addr;
611 int i;
613 if(dev->irq ==0||
614 request_irq(dev->irq, &lance_interrupt,0, lp->name, NULL)) {
615 return-EAGAIN;
618 /* We used to allocate DMA here, but that was silly.
619 DMA lines can't be shared! We now permanently allocate them. */
621 irq2dev_map[dev->irq] = dev;
623 /* Reset the LANCE */
624 inw(ioaddr+LANCE_RESET);
626 /* The DMA controller is used as a no-operation slave, "cascade mode". */
627 if(dev->dma !=4) {
628 enable_dma(dev->dma);
629 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
632 /* Un-Reset the LANCE, needed only for the NE2100. */
633 if(chip_table[lp->chip_version].flags & LANCE_MUST_UNRESET)
634 outw(0, ioaddr+LANCE_RESET);
636 if(chip_table[lp->chip_version].flags & LANCE_ENABLE_AUTOSELECT) {
637 /* This is 79C960-specific: Turn on auto-select of media (AUI, BNC). */
638 outw(0x0002, ioaddr+LANCE_ADDR);
639 outw(0x0002, ioaddr+LANCE_BUS_IF);
642 if(lance_debug >1)
643 printk("%s: lance_open() irq %d dma %d tx/rx rings %#x/%#x init %#x.\n",
644 dev->name, dev->irq, dev->dma,
645 (u32)virt_to_bus(lp->tx_ring),
646 (u32)virt_to_bus(lp->rx_ring),
647 (u32)virt_to_bus(&lp->init_block));
649 lance_init_ring(dev);
650 /* Re-initialize the LANCE, and start it when done. */
651 outw(0x0001, ioaddr+LANCE_ADDR);
652 outw((short) (u32)virt_to_bus(&lp->init_block), ioaddr+LANCE_DATA);
653 outw(0x0002, ioaddr+LANCE_ADDR);
654 outw(((u32)virt_to_bus(&lp->init_block)) >>16, ioaddr+LANCE_DATA);
656 outw(0x0004, ioaddr+LANCE_ADDR);
657 outw(0x0915, ioaddr+LANCE_DATA);
659 outw(0x0000, ioaddr+LANCE_ADDR);
660 outw(0x0001, ioaddr+LANCE_DATA);
662 dev->tbusy =0;
663 dev->interrupt =0;
664 dev->start =1;
665 i =0;
666 while(i++ <100)
667 if(inw(ioaddr+LANCE_DATA) &0x0100)
668 break;
670 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
671 * reports that doing so triggers a bug in the '974.
673 outw(0x0042, ioaddr+LANCE_DATA);
675 if(lance_debug >2)
676 printk("%s: LANCE open after %d ticks, init block %#x csr0 %4.4x.\n",
677 dev->name, i, (u32)virt_to_bus(&lp->init_block),inw(ioaddr+LANCE_DATA));
679 return0;/* Always succeed */
682 /* The LANCE has been halted for one reason or another (busmaster memory
683 arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
684 etc.). Modern LANCE variants always reload their ring-buffer
685 configuration when restarted, so we must reinitialize our ring
686 context before restarting. As part of this reinitialization,
687 find all packets still on the Tx ring and pretend that they had been
688 sent (in effect, drop the packets on the floor) - the higher-level
689 protocols will time out and retransmit. It'd be better to shuffle
690 these skbs to a temp list and then actually re-Tx them after
691 restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
694 static void
695 lance_purge_tx_ring(struct device *dev)
697 struct lance_private *lp = (struct lance_private *)dev->priv;
698 int i;
700 for(i =0; i < TX_RING_SIZE; i++) {
701 if(lp->tx_skbuff[i]) {
702 dev_kfree_skb(lp->tx_skbuff[i],FREE_WRITE);
703 lp->tx_skbuff[i] = NULL;
709 /* Initialize the LANCE Rx and Tx rings. */
710 static void
711 lance_init_ring(struct device *dev)
713 struct lance_private *lp = (struct lance_private *)dev->priv;
714 int i;
716 lp->lock =0, lp->tx_full =0;
717 lp->cur_rx = lp->cur_tx =0;
718 lp->dirty_rx = lp->dirty_tx =0;
720 for(i =0; i < RX_RING_SIZE; i++) {
721 lp->rx_ring[i].base = (u32)virt_to_bus((char*)lp->rx_buffs + i*PKT_BUF_SZ) |0x80000000;
722 lp->rx_ring[i].buf_length = -PKT_BUF_SZ;
724 /* The Tx buffer address is filled in as needed, but we do need to clear
725 the upper ownership bit. */
726 for(i =0; i < TX_RING_SIZE; i++) {
727 lp->tx_ring[i].base =0;
730 lp->init_block.mode =0x0000;
731 for(i =0; i <6; i++)
732 lp->init_block.phys_addr[i] = dev->dev_addr[i];
733 lp->init_block.filter[0] =0x00000000;
734 lp->init_block.filter[1] =0x00000000;
735 lp->init_block.rx_ring = ((u32)virt_to_bus(lp->rx_ring) &0xffffff) | RX_RING_LEN_BITS;
736 lp->init_block.tx_ring = ((u32)virt_to_bus(lp->tx_ring) &0xffffff) | TX_RING_LEN_BITS;
739 static void
740 lance_restart(struct device *dev,unsigned int csr0_bits,int must_reinit)
742 struct lance_private *lp = (struct lance_private *)dev->priv;
744 if(must_reinit ||
745 (chip_table[lp->chip_version].flags & LANCE_MUST_REINIT_RING)) {
746 lance_purge_tx_ring(dev);
747 lance_init_ring(dev);
749 outw(0x0000, dev->base_addr + LANCE_ADDR);
750 outw(csr0_bits, dev->base_addr + LANCE_DATA);
753 static int
754 lance_start_xmit(struct sk_buff *skb,struct device *dev)
756 struct lance_private *lp = (struct lance_private *)dev->priv;
757 int ioaddr = dev->base_addr;
758 int entry;
759 unsigned long flags;
761 /* Transmitter timeout, serious problems. */
762 if(dev->tbusy) {
763 int tickssofar = jiffies - dev->trans_start;
764 if(tickssofar <20)
765 return1;
766 outw(0, ioaddr+LANCE_ADDR);
767 printk("%s: transmit timed out, status %4.4x, resetting.\n",
768 dev->name,inw(ioaddr+LANCE_DATA));
769 outw(0x0004, ioaddr+LANCE_DATA);
770 lp->stats.tx_errors++;
771 #ifndef final_version
773 int i;
774 printk(" Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
775 lp->dirty_tx, lp->cur_tx, lp->tx_full ?" (full)":"",
776 lp->cur_rx);
777 for(i =0; i < RX_RING_SIZE; i++)
778 printk("%s %08x %04x %04x", i &0x3?"":"\n",
779 lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
780 lp->rx_ring[i].msg_length);
781 for(i =0; i < TX_RING_SIZE; i++)
782 printk("%s %08x %04x %04x", i &0x3?"":"\n",
783 lp->tx_ring[i].base, -lp->tx_ring[i].length,
784 lp->tx_ring[i].misc);
785 printk("\n");
787 #endif
788 lance_restart(dev,0x0043,1);
790 dev->tbusy=0;
791 dev->trans_start = jiffies;
793 return0;
796 if(skb == NULL) {
797 dev_tint(dev);
798 return0;
801 if(skb->len <=0)
802 return0;
804 if(lance_debug >3) {
805 outw(0x0000, ioaddr+LANCE_ADDR);
806 printk("%s: lance_start_xmit() called, csr0 %4.4x.\n", dev->name,
807 inw(ioaddr+LANCE_DATA));
808 outw(0x0000, ioaddr+LANCE_DATA);
811 /* Block a timer-based transmit from overlapping. This could better be
812 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
813 if(set_bit(0, (void*)&dev->tbusy) !=0) {
814 printk("%s: Transmitter access conflict.\n", dev->name);
815 return1;
818 if(set_bit(0, (void*)&lp->lock) !=0) {
819 if(lance_debug >0)
820 printk("%s: tx queue lock!.\n", dev->name);
821 /* don't clear dev->tbusy flag. */
822 return1;
825 /* Fill in a Tx ring entry */
827 /* Mask to ring buffer boundary. */
828 entry = lp->cur_tx & TX_RING_MOD_MASK;
830 /* Caution: the write order is important here, set the base address
831 with the "ownership" bits last. */
833 /* The old LANCE chips doesn't automatically pad buffers to min. size. */
834 if(chip_table[lp->chip_version].flags & LANCE_MUST_PAD) {
835 lp->tx_ring[entry].length =
836 -(ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN);
837 }else
838 lp->tx_ring[entry].length = -skb->len;
840 lp->tx_ring[entry].misc =0x0000;
842 /* If any part of this buffer is >16M we must copy it to a low-memory
843 buffer. */
844 if((u32)virt_to_bus(skb->data) + skb->len >0x01000000) {
845 if(lance_debug >5)
846 printk("%s: bouncing a high-memory packet (%#x).\n",
847 dev->name, (u32)virt_to_bus(skb->data));
848 memcpy(&lp->tx_bounce_buffs[entry], skb->data, skb->len);
849 lp->tx_ring[entry].base =
850 ((u32)virt_to_bus((lp->tx_bounce_buffs + entry)) &0xffffff) |0x83000000;
851 dev_kfree_skb(skb, FREE_WRITE);
852 }else{
853 lp->tx_skbuff[entry] = skb;
854 lp->tx_ring[entry].base = ((u32)virt_to_bus(skb->data) &0xffffff) |0x83000000;
856 lp->cur_tx++;
858 /* Trigger an immediate send poll. */
859 outw(0x0000, ioaddr+LANCE_ADDR);
860 outw(0x0048, ioaddr+LANCE_DATA);
862 dev->trans_start = jiffies;
864 save_flags(flags);
865 cli();
866 lp->lock =0;
867 if(lp->tx_ring[(entry+1) & TX_RING_MOD_MASK].base ==0)
868 dev->tbusy=0;
869 else
870 lp->tx_full =1;
871 restore_flags(flags);
873 return0;
876 /* The LANCE interrupt handler. */
877 static void
878 lance_interrupt(int irq,void*dev_id,struct pt_regs * regs)
880 struct device *dev = (struct device *)(irq2dev_map[irq]);
881 struct lance_private *lp;
882 int csr0, ioaddr, boguscnt=10;
883 int must_restart;
885 if(dev == NULL) {
886 printk("lance_interrupt(): irq %d for unknown device.\n", irq);
887 return;
890 ioaddr = dev->base_addr;
891 lp = (struct lance_private *)dev->priv;
892 if(dev->interrupt)
893 printk("%s: Re-entering the interrupt handler.\n", dev->name);
895 dev->interrupt =1;
897 outw(0x00, dev->base_addr + LANCE_ADDR);
898 while((csr0 =inw(dev->base_addr + LANCE_DATA)) &0x8600
899 && --boguscnt >=0) {
900 /* Acknowledge all of the current interrupt sources ASAP. */
901 outw(csr0 & ~0x004f, dev->base_addr + LANCE_DATA);
903 must_restart =0;
905 if(lance_debug >5)
906 printk("%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
907 dev->name, csr0,inw(dev->base_addr + LANCE_DATA));
909 if(csr0 &0x0400)/* Rx interrupt */
910 lance_rx(dev);
912 if(csr0 &0x0200) {/* Tx-done interrupt */
913 int dirty_tx = lp->dirty_tx;
915 while(dirty_tx < lp->cur_tx) {
916 int entry = dirty_tx & TX_RING_MOD_MASK;
917 int status = lp->tx_ring[entry].base;
919 if(status <0)
920 break;/* It still hasn't been Txed */
922 lp->tx_ring[entry].base =0;
924 if(status &0x40000000) {
925 /* There was an major error, log it. */
926 int err_status = lp->tx_ring[entry].misc;
927 lp->stats.tx_errors++;
928 if(err_status &0x0400) lp->stats.tx_aborted_errors++;
929 if(err_status &0x0800) lp->stats.tx_carrier_errors++;
930 if(err_status &0x1000) lp->stats.tx_window_errors++;
931 if(err_status &0x4000) {
932 /* Ackk! On FIFO errors the Tx unit is turned off! */
933 lp->stats.tx_fifo_errors++;
934 /* Remove this verbosity later! */
935 printk("%s: Tx FIFO error! Status %4.4x.\n",
936 dev->name, csr0);
937 /* Restart the chip. */
938 must_restart =1;
940 }else{
941 if(status &0x18000000)
942 lp->stats.collisions++;
943 lp->stats.tx_packets++;
946 /* We must free the original skb if it's not a data-only copy
947 in the bounce buffer. */
948 if(lp->tx_skbuff[entry]) {
949 dev_kfree_skb(lp->tx_skbuff[entry],FREE_WRITE);
950 lp->tx_skbuff[entry] =0;
952 dirty_tx++;
955 #ifndef final_version
956 if(lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
957 printk("out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
958 dirty_tx, lp->cur_tx, lp->tx_full);
959 dirty_tx += TX_RING_SIZE;
961 #endif
963 if(lp->tx_full && dev->tbusy
964 && dirty_tx > lp->cur_tx - TX_RING_SIZE +2) {
965 /* The ring is no longer full, clear tbusy. */
966 lp->tx_full =0;
967 dev->tbusy =0;
968 mark_bh(NET_BH);
971 lp->dirty_tx = dirty_tx;
974 /* Log misc errors. */
975 if(csr0 &0x4000) lp->stats.tx_errors++;/* Tx babble. */
976 if(csr0 &0x1000) lp->stats.rx_errors++;/* Missed a Rx frame. */
977 if(csr0 &0x0800) {
978 printk("%s: Bus master arbitration failure, status %4.4x.\n",
979 dev->name, csr0);
980 /* Restart the chip. */
981 must_restart =1;
984 if(must_restart) {
985 /* stop the chip to clear the error condition, then restart */
986 outw(0x0000, dev->base_addr + LANCE_ADDR);
987 outw(0x0004, dev->base_addr + LANCE_DATA);
988 lance_restart(dev,0x0002,0);
992 /* Clear any other interrupt, and set interrupt enable. */
993 outw(0x0000, dev->base_addr + LANCE_ADDR);
994 outw(0x7940, dev->base_addr + LANCE_DATA);
996 if(lance_debug >4)
997 printk("%s: exiting interrupt, csr%d=%#4.4x.\n",
998 dev->name,inw(ioaddr + LANCE_ADDR),
999 inw(dev->base_addr + LANCE_DATA));
1001 dev->interrupt =0;
1002 return;
1005 static int
1006 lance_rx(struct device *dev)
1008 struct lance_private *lp = (struct lance_private *)dev->priv;
1009 int entry = lp->cur_rx & RX_RING_MOD_MASK;
1010 int i;
1012 /* If we own the next entry, it's a new packet. Send it up. */
1013 while(lp->rx_ring[entry].base >=0) {
1014 int status = lp->rx_ring[entry].base >>24;
1016 if(status !=0x03) {/* There was an error. */
1017 /* There is a tricky error noted by John Murphy,
1018 <murf@perftech.com> to Russ Nelson: Even with full-sized
1019 buffers it's possible for a jabber packet to use two
1020 buffers, with only the last correctly noting the error. */
1021 if(status &0x01)/* Only count a general error at the */
1022 lp->stats.rx_errors++;/* end of a packet.*/
1023 if(status &0x20) lp->stats.rx_frame_errors++;
1024 if(status &0x10) lp->stats.rx_over_errors++;
1025 if(status &0x08) lp->stats.rx_crc_errors++;
1026 if(status &0x04) lp->stats.rx_fifo_errors++;
1027 lp->rx_ring[entry].base &=0x03ffffff;
1029 else
1031 /* Malloc up new buffer, compatible with net3. */
1032 short pkt_len = (lp->rx_ring[entry].msg_length &0xfff)-4;
1033 struct sk_buff *skb;
1035 if(pkt_len<60)
1037 printk("%s: Runt packet!\n",dev->name);
1038 lp->stats.rx_errors++;
1040 else
1042 skb =dev_alloc_skb(pkt_len+2);
1043 if(skb == NULL)
1045 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1046 for(i=0; i < RX_RING_SIZE; i++)
1047 if(lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].base <0)
1048 break;
1050 if(i > RX_RING_SIZE -2)
1052 lp->stats.rx_dropped++;
1053 lp->rx_ring[entry].base |=0x80000000;
1054 lp->cur_rx++;
1056 break;
1058 skb->dev = dev;
1059 skb_reserve(skb,2);/* 16 byte align */
1060 skb_put(skb,pkt_len);/* Make room */
1061 eth_copy_and_sum(skb,
1062 (unsigned char*)bus_to_virt((lp->rx_ring[entry].base &0x00ffffff)),
1063 pkt_len,0);
1064 skb->protocol=eth_type_trans(skb,dev);
1065 netif_rx(skb);
1066 lp->stats.rx_packets++;
1069 /* The docs say that the buffer length isn't touched, but Andrew Boyd
1070 of QNX reports that some revs of the 79C965 clear it. */
1071 lp->rx_ring[entry].buf_length = -PKT_BUF_SZ;
1072 lp->rx_ring[entry].base |=0x80000000;
1073 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1076 /* We should check that at least two ring entries are free. If not,
1077 we should free one and mark stats->rx_dropped++. */
1079 return0;
1082 static int
1083 lance_close(struct device *dev)
1085 int ioaddr = dev->base_addr;
1086 struct lance_private *lp = (struct lance_private *)dev->priv;
1088 dev->start =0;
1089 dev->tbusy =1;
1091 if(chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1092 outw(112, ioaddr+LANCE_ADDR);
1093 lp->stats.rx_missed_errors =inw(ioaddr+LANCE_DATA);
1095 outw(0, ioaddr+LANCE_ADDR);
1097 if(lance_debug >1)
1098 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1099 dev->name,inw(ioaddr+LANCE_DATA));
1101 /* We stop the LANCE here -- it occasionally polls
1102 memory if we don't. */
1103 outw(0x0004, ioaddr+LANCE_DATA);
1105 if(dev->dma !=4)
1106 disable_dma(dev->dma);
1108 free_irq(dev->irq, NULL);
1110 irq2dev_map[dev->irq] =0;
1112 return0;
1115 static struct enet_statistics *
1116 lance_get_stats(struct device *dev)
1118 struct lance_private *lp = (struct lance_private *)dev->priv;
1119 short ioaddr = dev->base_addr;
1120 short saved_addr;
1121 unsigned long flags;
1123 if(chip_table[lp->chip_version].flags & LANCE_HAS_MISSED_FRAME) {
1124 save_flags(flags);
1125 cli();
1126 saved_addr =inw(ioaddr+LANCE_ADDR);
1127 outw(112, ioaddr+LANCE_ADDR);
1128 lp->stats.rx_missed_errors =inw(ioaddr+LANCE_DATA);
1129 outw(saved_addr, ioaddr+LANCE_ADDR);
1130 restore_flags(flags);
1133 return&lp->stats;
1136 /* Set or clear the multicast filter for this adaptor.
1139 static voidset_multicast_list(struct device *dev)
1141 short ioaddr = dev->base_addr;
1143 outw(0, ioaddr+LANCE_ADDR);
1144 outw(0x0004, ioaddr+LANCE_DATA);/* Temporarily stop the lance. */
1146 if(dev->flags&IFF_PROMISC) {
1147 /* Log any net taps. */
1148 printk("%s: Promiscuous mode enabled.\n", dev->name);
1149 outw(15, ioaddr+LANCE_ADDR);
1150 outw(0x8000, ioaddr+LANCE_DATA);/* Set promiscuous mode */
1151 }else{
1152 short multicast_table[4];
1153 int i;
1154 int num_addrs=dev->mc_count;
1155 if(dev->flags&IFF_ALLMULTI)
1156 num_addrs=1;
1157 /* FIXIT: We don't use the multicast table, but rely on upper-layer filtering. */
1158 memset(multicast_table, (num_addrs ==0) ?0: -1,sizeof(multicast_table));
1159 for(i =0; i <4; i++) {
1160 outw(8+ i, ioaddr+LANCE_ADDR);
1161 outw(multicast_table[i], ioaddr+LANCE_DATA);
1163 outw(15, ioaddr+LANCE_ADDR);
1164 outw(0x0000, ioaddr+LANCE_DATA);/* Unset promiscuous mode */
1167 lance_restart(dev,0x0142,0);/* Resume normal operation */
1173 * Local variables:
1174 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c lance.c"
1175 * c-indent-level: 4
1176 * tab-width: 4
1177 * End:
close