ia64/linux-2.6.18-xen.hg

annotate drivers/net/lance.c @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

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