OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [net/] [lance.c] - Blame information for rev 1772

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.