1 /* lance.c: An AMD LANCE ethernet driver for linux. */ 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? ;-) 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> 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
); 60 struct netdev_entry lance_drv
= 61 {"lance", lance_probe1
, LANCE_TOTAL_SIZE
, lance_portlist
}; 65 int lance_debug
= LANCE_DEBUG
; 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 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)) 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 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
{ 198 s16 buf_length
;/* This length is 2s complement (negative)! */ 199 s16 msg_length
;/* This length is "normal". */ 202 struct lance_tx_head
{ 204 s16 length
;/* Length is 2s complement (negative)! */ 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 */ 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
; 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. */ 232 struct enet_statistics stats
; 233 unsigned char chip_version
;/* See lance_chip_type. */ 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
{ 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 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 305 if(high_memory
<=16*1024*1024) 306 lance_need_isa_bounce_buffers
=0; 308 #if defined(CONFIG_PCI) 309 if(pcibios_present()) { 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) 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. */ 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
); 345 #endif/* defined(CONFIG_PCI) */ 347 /* On the Alpha don't look for PCnet chips on the ISA bus */ 349 for(port
= lance_portlist
; *port
; 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
); 367 voidlance_probe1(int ioaddr
) 370 struct lance_private
*lp
; 371 short dma_channels
;/* Mark spuriously-busy DMA channels */ 372 int i
, reset_val
, lance_version
; 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. */ 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); 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. */ 404 outw(reset_val
, ioaddr
+LANCE_RESET
); 406 outw(0x0000, ioaddr
+LANCE_ADDR
);/* Switch to window 0 */ 407 if(inw(ioaddr
+LANCE_DATA
) !=0x0004) 410 /* Get the version of the chip. */ 411 outw(88, ioaddr
+LANCE_ADDR
); 412 if(inw(ioaddr
+LANCE_ADDR
) !=88) { 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; 419 printk(" LANCE chip version is %#x.\n", chip_version
); 420 if((chip_version
&0xfff) !=0x003) 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
) 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. */ 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
); 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
)); 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. */ 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
); 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
); 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. */ 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; 505 /* The DMA channel may be passed in PARAM1. */ 506 if(dev
->mem_start
&0x07) 507 dev
->dma
= dev
->mem_start
&0x07; 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); 517 printk(" assigned IRQ %d", dev
->irq
); 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 524 /* Trigger an initialization just for the interrupt. */ 525 outw(0x0041, ioaddr
+LANCE_DATA
); 527 dev
->irq
=autoirq_report(1); 529 printk(", probed IRQ %d", dev
->irq
); 531 printk(", failed to detect IRQ line.\n"); 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) 542 printk(", no DMA needed.\n"); 544 if(request_dma(dev
->dma
, chipname
)) { 545 printk("DMA %d allocation failed.\n", dev
->dma
); 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}; 555 /* Don't enable a permanently busy DMA channel, or the machine 557 if(test_bit(dma
, &dma_channels
)) 559 outw(0x7f04, ioaddr
+LANCE_DATA
);/* Clear the memory error bits. */ 560 if(request_dma(dma
, chipname
)) 562 set_dma_mode(dma
, DMA_MODE_CASCADE
); 565 /* Trigger an initialization. */ 566 outw(0x0001, ioaddr
+LANCE_DATA
); 567 for(boguscnt
=100; boguscnt
>0; --boguscnt
) 568 if(inw(ioaddr
+LANCE_DATA
) &0x0900) 570 if(inw(ioaddr
+LANCE_DATA
) &0x0100) { 572 printk(", DMA %d.\n", dev
->dma
); 579 if(i
==4) {/* Failure: bail. */ 580 printk("DMA detection failed.\n"); 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) 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
; 607 lance_open(struct device
*dev
) 609 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
; 610 int ioaddr
= dev
->base_addr
; 614 request_irq(dev
->irq
, &lance_interrupt
,0, lp
->name
, NULL
)) { 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". */ 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
); 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
); 667 if(inw(ioaddr
+LANCE_DATA
) &0x0100) 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
); 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 695 lance_purge_tx_ring(struct device
*dev
) 697 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
; 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. */ 711 lance_init_ring(struct device
*dev
) 713 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
; 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; 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
; 740 lance_restart(struct device
*dev
,unsigned int csr0_bits
,int must_reinit
) 742 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
; 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
); 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
; 761 /* Transmitter timeout, serious problems. */ 763 int tickssofar
= jiffies
- dev
->trans_start
; 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 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)":"", 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
); 788 lance_restart(dev
,0x0043,1); 791 dev
->trans_start
= jiffies
; 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
); 818 if(set_bit(0, (void*)&lp
->lock
) !=0) { 820 printk("%s: tx queue lock!.\n", dev
->name
); 821 /* don't clear dev->tbusy flag. */ 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
); 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 844 if((u32
)virt_to_bus(skb
->data
) + skb
->len
>0x01000000) { 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
); 853 lp
->tx_skbuff
[entry
] = skb
; 854 lp
->tx_ring
[entry
].base
= ((u32
)virt_to_bus(skb
->data
) &0xffffff) |0x83000000; 858 /* Trigger an immediate send poll. */ 859 outw(0x0000, ioaddr
+LANCE_ADDR
); 860 outw(0x0048, ioaddr
+LANCE_DATA
); 862 dev
->trans_start
= jiffies
; 867 if(lp
->tx_ring
[(entry
+1) & TX_RING_MOD_MASK
].base
==0) 871 restore_flags(flags
); 876 /* The LANCE interrupt handler. */ 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; 886 printk("lance_interrupt(): irq %d for unknown device.\n", irq
); 890 ioaddr
= dev
->base_addr
; 891 lp
= (struct lance_private
*)dev
->priv
; 893 printk("%s: Re-entering the interrupt handler.\n", dev
->name
); 897 outw(0x00, dev
->base_addr
+ LANCE_ADDR
); 898 while((csr0
=inw(dev
->base_addr
+ LANCE_DATA
)) &0x8600 900 /* Acknowledge all of the current interrupt sources ASAP. */ 901 outw(csr0
& ~0x004f, dev
->base_addr
+ LANCE_DATA
); 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 */ 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
; 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", 937 /* Restart the chip. */ 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; 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
; 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. */ 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. */ 978 printk("%s: Bus master arbitration failure, status %4.4x.\n", 980 /* Restart the chip. */ 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
); 997 printk("%s: exiting interrupt, csr%d=%#4.4x.\n", 998 dev
->name
,inw(ioaddr
+ LANCE_ADDR
), 999 inw(dev
->base_addr
+ LANCE_DATA
)); 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
; 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; 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
; 1037 printk("%s: Runt packet!\n",dev
->name
); 1038 lp
->stats
.rx_errors
++; 1042 skb
=dev_alloc_skb(pkt_len
+2); 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) 1050 if(i
> RX_RING_SIZE
-2) 1052 lp
->stats
.rx_dropped
++; 1053 lp
->rx_ring
[entry
].base
|=0x80000000; 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)), 1064 skb
->protocol
=eth_type_trans(skb
,dev
); 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++. */ 1083 lance_close(struct device
*dev
) 1085 int ioaddr
= dev
->base_addr
; 1086 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
; 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
); 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
); 1106 disable_dma(dev
->dma
); 1108 free_irq(dev
->irq
, NULL
); 1110 irq2dev_map
[dev
->irq
] =0; 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
; 1121 unsigned long flags
; 1123 if(chip_table
[lp
->chip_version
].flags
& LANCE_HAS_MISSED_FRAME
) { 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
); 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 */ 1152 short multicast_table
[4]; 1154 int num_addrs
=dev
->mc_count
; 1155 if(dev
->flags
&IFF_ALLMULTI
) 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 */ 1174 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c lance.c"