Subversion Repositories shark

Rev

Rev 442 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
442 giacomo 1
/* drivers/net/eepro100.c: An Intel i82557 Ethernet driver for Linux. */
2
/*
3
   NOTICE: this version tested with kernels 1.3.72 and later only!
4
        Written 1996-1998 by Donald Becker.
5
 
6
        This software may be used and distributed according to the terms
7
        of the GNU Public License, incorporated herein by reference.
8
 
9
        This driver is for the Intel EtherExpress Pro 100B boards.
10
        It should work with other i82557 and i82558 boards.
11
        To use a built-in driver, install as drivers/net/eepro100.c.
12
        To use as a module, use the compile-command at the end of the file.
13
 
14
        The author may be reached as becker@CESDIS.usra.edu, or C/O
15
        Center of Excellence in Space Data and Information Sciences
16
           Code 930.5, NASA Goddard Space Flight Center, Greenbelt MD 20771
17
        For updates see
18
                http://cesdis.gsfc.nasa.gov/linux/drivers/eepro100.html
19
        There is also a mailing list based at
20
                linux-eepro100@cesdis.gsfc.nasa.gov
21
*/
22
 
23
static const char *version =
24
"eepro100.c:v1.05 10/16/98 Donald Becker http://cesdis.gsfc.nasa.gov/linux/drivers/eepro100.html\n";
25
 
26
/* A few user-configurable values that apply to all boards.
27
   First set are undocumented and spelled per Intel recommendations. */
28
 
29
static int congenb = 0;         /* Enable congestion control in the DP83840. */
30
static int txfifo = 8;          /* Tx FIFO threshold in 4 byte units, 0-15 */
31
static int rxfifo = 8;          /* Rx FIFO threshold, default 32 bytes. */
32
/* Tx/Rx DMA burst length, 0-127, 0 == no preemption, tx==128 -> disabled. */
33
static int txdmacount = 128;
34
static int rxdmacount = 0;
35
 
36
/* Set the copy breakpoint for the copy-only-tiny-buffer Rx method.
37
   Lower values use more memory, but are faster. */
38
static int rx_copybreak = 200;
39
 
40
/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
41
static int max_interrupt_work = 20;
42
 
43
/* Maximum number of multicast addresses to filter (vs. rx-all-multicast) */
44
static int multicast_filter_limit = 64;
45
 
46
#include <linux/config.h>
47
#ifdef MODULE
48
#ifdef MODVERSIONS
49
#include <linux/modversions.h>
50
#endif
51
#include <linux/module.h>
52
#else
53
#define MOD_INC_USE_COUNT
54
#define MOD_DEC_USE_COUNT
55
#endif
56
 
57
#include <linux/version.h>
58
#include <linux/kernel.h>
59
#include <linux/string.h>
60
#include <linux/timer.h>
61
#include <linux/errno.h>
62
#include <linux/ioport.h>
63
#include <linux/malloc.h>
64
#include <linux/interrupt.h>
65
#include <linux/pci.h>
66
#if LINUX_VERSION_CODE < 0x20155
67
#include <linux/bios32.h>               /* Ignore the bogus warning in 2.1.100+ */
68
#endif
69
#include <asm/bitops.h>
70
#include <asm/io.h>
71
 
72
#include <linux/netdevice.h>
73
#include <linux/etherdevice.h>
74
#include <linux/skbuff.h>
75
#include <linux/delay.h>
76
 
77
/* Unused in the 2.0.* version, but retained for documentation. */
78
#if LINUX_VERSION_CODE > 0x20118  &&  defined(MODULE)
79
MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");
80
MODULE_DESCRIPTION("Intel i82557/i82558 PCI EtherExpressPro driver");
81
MODULE_PARM(debug, "i");
82
MODULE_PARM(options, "1-" __MODULE_STRING(8) "i");
83
MODULE_PARM(full_duplex, "1-" __MODULE_STRING(8) "i");
84
MODULE_PARM(congenb, "i");
85
MODULE_PARM(txfifo, "i");
86
MODULE_PARM(rxfifo, "i");
87
MODULE_PARM(txdmacount, "i");
88
MODULE_PARM(rxdmacount, "i");
89
MODULE_PARM(rx_copybreak, "i");
90
MODULE_PARM(max_interrupt_work, "i");
91
MODULE_PARM(multicast_filter_limit, "i");
92
#endif
93
 
94
#define RUN_AT(x) (jiffies + (x))
95
 
96
#if (LINUX_VERSION_CODE < 0x20123)
97
#define test_and_set_bit(val, addr) set_bit(val, addr)
98
#endif
99
#if LINUX_VERSION_CODE < 0x20159
100
#define dev_free_skb(skb) dev_kfree_skb(skb, FREE_WRITE);
101
#else
102
#define dev_free_skb(skb) dev_kfree_skb(skb);
103
#endif
104
 
105
/* The total I/O port extent of the board.
106
   The registers beyond 0x18 only exist on the i82558. */
107
#define SPEEDO3_TOTAL_SIZE 0x20
108
 
109
int speedo_debug = 1;
110
 
111
/*
112
                                Theory of Operation
113
 
114
I. Board Compatibility
115
 
116
This device driver is designed for the Intel i82557 "Speedo3" chip, Intel's
117
single-chip fast Ethernet controller for PCI, as used on the Intel
118
EtherExpress Pro 100 adapter.
119
 
120
II. Board-specific settings
121
 
122
PCI bus devices are configured by the system at boot time, so no jumpers
123
need to be set on the board.  The system BIOS should be set to assign the
124
PCI INTA signal to an otherwise unused system IRQ line.  While it's
125
possible to share PCI interrupt lines, it negatively impacts performance and
126
only recent kernels support it.
127
 
128
III. Driver operation
129
 
130
IIIA. General
131
The Speedo3 is very similar to other Intel network chips, that is to say
132
"apparently designed on a different planet".  This chips retains the complex
133
Rx and Tx descriptors and multiple buffers pointers as previous chips, but
134
also has simplified Tx and Rx buffer modes.  This driver uses the "flexible"
135
Tx mode, but in a simplified lower-overhead manner: it associates only a
136
single buffer descriptor with each frame descriptor.
137
 
138
Despite the extra space overhead in each receive skbuff, the driver must use
139
the simplified Rx buffer mode to assure that only a single data buffer is
140
associated with each RxFD. The driver implements this by reserving space
141
for the Rx descriptor at the head of each Rx skbuff.
142
 
143
The Speedo-3 has receive and command unit base addresses that are added to
144
almost all descriptor pointers.  The driver sets these to zero, so that all
145
pointer fields are absolute addresses.
146
 
147
The System Control Block (SCB) of some previous Intel chips exists on the
148
chip in both PCI I/O and memory space.  This driver uses the I/O space
149
registers, but might switch to memory mapped mode to better support non-x86
150
processors.
151
 
152
IIIB. Transmit structure
153
 
154
The driver must use the complex Tx command+descriptor mode in order to
155
have a indirect pointer to the skbuff data section.  Each Tx command block
156
(TxCB) is associated with two immediately appended Tx Buffer Descriptor
157
(TxBD).  A fixed ring of these TxCB+TxBD pairs are kept as part of the
158
speedo_private data structure for each adapter instance.
159
 
160
The newer i82558 explicitly supports this structure, and can read the two
161
TxBDs in the same PCI burst as the TxCB.
162
 
163
This ring structure is used for all normal transmit packets, but the
164
transmit packet descriptors aren't long enough for most non-Tx commands such
165
as CmdConfigure.  This is complicated by the possibility that the chip has
166
already loaded the link address in the previous descriptor.  So for these
167
commands we convert the next free descriptor on the ring to a NoOp, and point
168
that descriptor's link to the complex command.
169
 
170
An additional complexity of these non-transmit commands are that they may be
171
added asynchronous to the normal transmit queue, so we disable interrupts
172
whenever the Tx descriptor ring is manipulated.
173
 
174
A notable aspect of these special configure commands is that they do
175
work with the normal Tx ring entry scavenge method.  The Tx ring scavenge
176
is done at interrupt time using the 'dirty_tx' index, and checking for the
177
command-complete bit.  While the setup frames may have the NoOp command on the
178
Tx ring marked as complete, but not have completed the setup command, this
179
is not a problem.  The tx_ring entry can be still safely reused, as the
180
tx_skbuff[] entry is always empty for config_cmd and mc_setup frames.
181
 
182
Commands may have bits set e.g. CmdSuspend in the command word to either
183
suspend or stop the transmit/command unit.  This driver always flags the last
184
command with CmdSuspend, erases the CmdSuspend in the previous command, and
185
then issues a CU_RESUME.
186
Note: Watch out for the potential race condition here: imagine
187
        erasing the previous suspend
188
                the chip processes the previous command
189
                the chip processes the final command, and suspends
190
        doing the CU_RESUME
191
                the chip processes the next-yet-valid post-final-command.
192
So blindly sending a CU_RESUME is only safe if we do it immediately after
193
after erasing the previous CmdSuspend, without the possibility of an
194
intervening delay.  Thus the resume command is always within the
195
interrupts-disabled region.  This is a timing dependence, but handling this
196
condition in a timing-independent way would considerably complicate the code.
197
 
198
Note: In previous generation Intel chips, restarting the command unit was a
199
notoriously slow process.  This is presumably no longer true.
200
 
201
IIIC. Receive structure
202
 
203
Because of the bus-master support on the Speedo3 this driver uses the new
204
SKBUFF_RX_COPYBREAK scheme, rather than a fixed intermediate receive buffer.
205
This scheme allocates full-sized skbuffs as receive buffers.  The value
206
SKBUFF_RX_COPYBREAK is used as the copying breakpoint: it is chosen to
207
trade-off the memory wasted by passing the full-sized skbuff to the queue
208
layer for all frames vs. the copying cost of copying a frame to a
209
correctly-sized skbuff.
210
 
211
For small frames the copying cost is negligible (esp. considering that we
212
are pre-loading the cache with immediately useful header information), so we
213
allocate a new, minimally-sized skbuff.  For large frames the copying cost
214
is non-trivial, and the larger copy might flush the cache of useful data, so
215
we pass up the skbuff the packet was received into.
216
 
217
IIID. Synchronization
218
The driver runs as two independent, single-threaded flows of control.  One
219
is the send-packet routine, which enforces single-threaded use by the
220
dev->tbusy flag.  The other thread is the interrupt handler, which is single
221
threaded by the hardware and other software.
222
 
223
The send packet thread has partial control over the Tx ring and 'dev->tbusy'
224
flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
225
queue slot is empty, it clears the tbusy flag when finished otherwise it sets
226
the 'sp->tx_full' flag.
227
 
228
The interrupt handler has exclusive control over the Rx ring and records stats
229
from the Tx ring.  (The Tx-done interrupt can't be selectively turned off, so
230
we can't avoid the interrupt overhead by having the Tx routine reap the Tx
231
stats.)  After reaping the stats, it marks the queue entry as empty by setting
232
the 'base' to zero.      Iff the 'sp->tx_full' flag is set, it clears both the
233
tx_full and tbusy flags.
234
 
235
IV. Notes
236
 
237
Thanks to Steve Williams of Intel for arranging the non-disclosure agreement
238
that stated that I could disclose the information.  But I still resent
239
having to sign an Intel NDA when I'm helping Intel sell their own product!
240
 
241
*/
242
 
243
/* A few values that may be tweaked. */
244
/* The ring sizes should be a power of two for efficiency. */
245
#define TX_RING_SIZE    16              /* Effectively 2 entries fewer. */
246
#define RX_RING_SIZE    16
247
/* Size of an pre-allocated Rx buffer: <Ethernet MTU> + slack.*/
248
#define PKT_BUF_SZ              1536
249
 
250
/* Time in jiffies before concluding the transmitter is hung. */
251
#define TX_TIMEOUT  ((800*HZ)/1000)
252
 
253
/* How to wait for the command unit to accept a command.
254
   Typically this takes 0 ticks. */
255
static inline void wait_for_cmd_done(long cmd_ioaddr)
256
{
257
        int wait = 100;
258
        do   ;
259
        while(inb(cmd_ioaddr) && --wait >= 0);
260
}
261
 
262
/* Operational parameter that usually are not changed. */
263
 
264
/* The rest of these values should never change. */
265
 
266
/* Offsets to the various registers.
267
   All accesses need not be longword aligned. */
268
enum speedo_offsets {
269
        SCBStatus = 0, SCBCmd = 2,      /* Rx/Command Unit command and status. */
270
        SCBPointer = 4,                         /* General purpose pointer. */
271
        SCBPort = 8,                            /* Misc. commands and operands.  */
272
        SCBflash = 12, SCBeeprom = 14, /* EEPROM and flash memory control. */
273
        SCBCtrlMDI = 16,                        /* MDI interface control. */
274
        SCBEarlyRx = 20,                        /* Early receive byte count. */
275
};
276
/* Commands that can be put in a command list entry. */
277
enum commands {
278
        CmdNOp = 0, CmdIASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
279
        CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7,
280
        CmdSuspend = 0x4000,            /* Suspend after completion. */
281
        CmdIntr = 0x2000,                       /* Interrupt after completion. */
282
        CmdTxFlex = 0x0008,                     /* Use "Flexible mode" for CmdTx command. */
283
};
284
 
285
/* The SCB accepts the following controls for the Tx and Rx units: */
286
#define  CU_START               0x0010
287
#define  CU_RESUME              0x0020
288
#define  CU_STATSADDR   0x0040
289
#define  CU_SHOWSTATS   0x0050  /* Dump statistics counters. */
290
#define  CU_CMD_BASE    0x0060  /* Base address to add to add CU commands. */
291
#define  CU_DUMPSTATS   0x0070  /* Dump then reset stats counters. */
292
 
293
#define  RX_START       0x0001
294
#define  RX_RESUME      0x0002
295
#define  RX_ABORT       0x0004
296
#define  RX_ADDR_LOAD   0x0006
297
#define  RX_RESUMENR    0x0007
298
#define INT_MASK        0x0100
299
#define DRVR_INT        0x0200          /* Driver generated interrupt. */
300
 
301
/* The Speedo3 Rx and Tx frame/buffer descriptors. */
302
struct descriptor_net {                 /* A generic descriptor. */
303
        s16 status;             /* Offset 0. */
304
        s16 command;            /* Offset 2. */
305
        u32 link;                                       /* struct descriptor *  */
306
        unsigned char params[0];
307
};
308
 
309
/* The Speedo3 Rx and Tx buffer descriptors. */
310
struct RxFD {                                   /* Receive frame descriptor. */
311
        s32 status;
312
        u32 link;                                       /* struct RxFD * */
313
        u32 rx_buf_addr;                        /* void * */
314
        u16 count;
315
        u16 size;
316
};
317
 
318
/* Selected elements of the Tx/RxFD.status word. */
319
enum RxFD_bits {
320
        RxComplete=0x8000, RxOK=0x2000,
321
        RxErrCRC=0x0800, RxErrAlign=0x0400, RxErrTooBig=0x0200, RxErrSymbol=0x0010,
322
        RxEth2Type=0x0020, RxNoMatch=0x0004, RxNoIAMatch=0x0002,
323
        StatusComplete=0x8000,
324
};
325
 
326
struct TxFD {                                   /* Transmit frame descriptor set. */
327
        s32 status;
328
        u32 link;                                       /* void * */
329
        u32 tx_desc_addr;                       /* Always points to the tx_buf_addr element. */
330
        s32 count;                                      /* # of TBD (=1), Tx start thresh., etc. */
331
        /* This constitutes two "TBD" entries -- we only use one. */
332
        u32 tx_buf_addr0;                       /* void *, frame to be transmitted.  */
333
        s32 tx_buf_size0;                       /* Length of Tx frame. */
334
        u32 tx_buf_addr1;                       /* void *, frame to be transmitted.  */
335
        s32 tx_buf_size1;                       /* Length of Tx frame. */
336
};
337
 
338
/* Elements of the dump_statistics block. This block must be lword aligned. */
339
struct speedo_stats {
340
        u32 tx_good_frames;
341
        u32 tx_coll16_errs;
342
        u32 tx_late_colls;
343
        u32 tx_underruns;
344
        u32 tx_lost_carrier;
345
        u32 tx_deferred;
346
        u32 tx_one_colls;
347
        u32 tx_multi_colls;
348
        u32 tx_total_colls;
349
        u32 rx_good_frames;
350
        u32 rx_crc_errs;
351
        u32 rx_align_errs;
352
        u32 rx_resource_errs;
353
        u32 rx_overrun_errs;
354
        u32 rx_colls_errs;
355
        u32 rx_runt_errs;
356
        u32 done_marker;
357
};
358
 
359
struct speedo_private {
360
        char devname[8];                        /* Used only for kernel debugging. */
361
        const char *product_name;
362
        struct device *next_module;
363
        struct TxFD     tx_ring[TX_RING_SIZE];  /* Commands (usually CmdTxPacket). */
364
        /* The saved address of a sent-in-place packet/buffer, for skfree(). */
365
        struct sk_buff* tx_skbuff[TX_RING_SIZE];
366
        struct descriptor_net  *last_cmd;       /* Last command sent. */
367
        /* Rx descriptor ring & addresses of receive-in-place skbuffs. */
368
        struct RxFD *rx_ringp[RX_RING_SIZE];
369
        struct sk_buff* rx_skbuff[RX_RING_SIZE];
370
        struct RxFD *last_rxf;  /* Last command sent. */
371
        struct enet_statistics stats;
372
        struct speedo_stats lstats;
373
        struct timer_list timer;        /* Media selection timer. */
374
        long last_rx_time;                      /* Last Rx, in jiffies, to handle Rx hang. */
375
        unsigned int cur_rx, cur_tx;            /* The next free ring entry */
376
        unsigned int dirty_rx, dirty_tx;        /* The ring entries to be free()ed. */
377
        int mc_setup_frm_len;                           /* The length of an allocated.. */
378
        struct descriptor_net *mc_setup_frm;    /* ..multicast setup frame. */
379
        int mc_setup_busy;                                      /* Avoid double-use of setup frame. */
380
        int in_interrupt;                                       /* Word-aligned dev->interrupt */
381
        char rx_mode;                                           /* Current PROMISC/ALLMULTI setting. */
382
        unsigned int tx_full:1;                         /* The Tx queue is full. */
383
        unsigned int full_duplex:1;                     /* Full-duplex operation requested. */
384
        unsigned int default_port:1;            /* Last dev->if_port value. */
385
        unsigned int rx_bug:1;                          /* Work around receiver hang errata. */
386
        unsigned int rx_bug10:1;                        /* Receiver might hang at 10mbps. */
387
        unsigned int rx_bug100:1;                       /* Receiver might hang at 100mbps. */
388
        unsigned short phy[2];                          /* PHY media interfaces available. */
389
};
390
 
391
/* The parameters for a CmdConfigure operation.
392
   There are so many options that it would be difficult to document each bit.
393
   We mostly use the default or recommended settings. */
394
const char i82557_config_cmd[22] = {
395
        22, 0x08, 0, 0,  0, 0x80, 0x32, 0x03,  1, /* 1=Use MII  0=Use AUI */
396
        0, 0x2E, 0,  0x60, 0,
397
        0xf2, 0x48,   0, 0x40, 0xf2, 0x80,              /* 0x40=Force full-duplex */
398
        0x3f, 0x05, };
399
const char i82558_config_cmd[22] = {
400
        22, 0x08, 0, 1,  0, 0x80, 0x22, 0x03,  1, /* 1=Use MII  0=Use AUI */
401
        0, 0x2E, 0,  0x60, 0x08, 0x88,
402
        0x68, 0, 0x40, 0xf2, 0xBD,              /* 0xBD->0xFD=Force full-duplex */
403
        0x31, 0x05, };
404
 
405
/* PHY media interface chips. */
406
static const char *phys[] = {
407
        "None", "i82553-A/B", "i82553-C", "i82503",
408
        "DP83840", "80c240", "80c24", "i82555",
409
        "unknown-8", "unknown-9", "DP83840A", "unknown-11",
410
        "unknown-12", "unknown-13", "unknown-14", "unknown-15", };
411
enum phy_chips { NonSuchPhy=0, I82553AB, I82553C, I82503, DP83840, S80C240,
412
                                         S80C24, I82555, DP83840A=10, };
413
static const char is_mii[] = { 0, 1, 1, 0, 1, 1, 0, 1 };
414
 
415
static void speedo_found1(struct device *dev, long ioaddr, int irq,
416
                                                  int card_idx);
417
 
418
static int read_eeprom(long ioaddr, int location, int addr_len);
419
static int mdio_read(long ioaddr, int phy_id, int location);
420
static int mdio_write(long ioaddr, int phy_id, int location, int value);
421
static int speedo_open(struct device *dev);
422
static void speedo_timer(unsigned long data);
423
static void speedo_init_rx_ring(struct device *dev);
424
static int speedo_start_xmit(struct sk_buff *skb, struct device *dev);
425
static int speedo_rx(struct device *dev);
426
static void speedo_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
427
static int speedo_close(struct device *dev);
428
static struct enet_statistics *speedo_get_stats(struct device *dev);
429
static int speedo_ioctl(struct device *dev, struct ifreq *rq, int cmd);
430
static void set_rx_mode(struct device *dev);
431
 
432
 
433
 
434
/* The parameters that may be passed in... */
435
/* 'options' is used to pass a transceiver override or full-duplex flag
436
   e.g. "options=16" for FD, "options=32" for 100mbps-only. */
437
static int full_duplex[] = {-1, -1, -1, -1, -1, -1, -1, -1};
438
static int options[] = {-1, -1, -1, -1, -1, -1, -1, -1};
439
#ifdef MODULE
440
static int debug = -1;                  /* The debug level */
441
#endif
442
 
443
#ifdef honor_default_port
444
/* Optional driver feature to allow forcing the transceiver setting.
445
   Not recommended. */
446
static int mii_ctrl[8] = { 0x3300, 0x3100, 0x0000, 0x0100,
447
                                                   0x2000, 0x2100, 0x0400, 0x3100};
448
#endif
449
 
450
/* A list of all installed Speedo devices, for removing the driver module. */
451
static struct device *root_speedo_dev = NULL;
452
 
453
int eepro100_init(struct device *dev)
454
{
455
        int cards_found = 0;
456
        static int pci_index = 0;
457
 
458
        if (! pcibios_present())
459
                return cards_found;
460
 
461
        for (; pci_index < 8; pci_index++) {
462
                unsigned char pci_bus, pci_device_fn, pci_latency;
446 giacomo 463
                unsigned short int vendor,device;
464
                long ioaddr;
442 giacomo 465
                int irq;
466
 
467
                u16 pci_command, new_command;
468
 
469
                printk("Finding Device\n");
470
 
471
                if (pcibios_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pci_index,
472
                                                                &pci_bus, &pci_device_fn)
473
                        != PCIBIOS_SUCCESSFUL)
474
                        break;
475
 
446 giacomo 476
                pcibios_read_config_word(pci_bus, pci_device_fn,
477
                                                                 PCI_VENDOR_ID, &vendor);
478
                pcibios_read_config_word(pci_bus, pci_device_fn,
479
                                                                 PCI_DEVICE_ID, &device);
442 giacomo 480
 
446 giacomo 481
                printk("Device = %x Vendor = %x\n",(int)device,(int)vendor);
482
 
483
                if (vendor != PCI_VENDOR_ID_INTEL)
484
                        break;
485
 
486
 
442 giacomo 487
#if defined(PCI_SUPPORT_VER2)                   
488
                {
489
                        struct pci_dev *pdev = pci_find_slot(pci_bus, pci_device_fn);
490
                        ioaddr = pdev->base_address[1];         /* Use [0] to mem-map */
491
                        irq = pdev->irq;
492
                        printk("Device 1 %d %d\n",ioaddr,irq);
493
                }
494
#else
495
                {
496
                        u32 pci_ioaddr;
497
                        u8 pci_irq_line;
498
                        pcibios_read_config_byte(pci_bus, pci_device_fn,
499
                                                                         PCI_INTERRUPT_LINE, &pci_irq_line);
500
                        /* Note: BASE_ADDRESS_0 is for memory-mapping the registers. */
501
                        pcibios_read_config_dword(pci_bus, pci_device_fn,
502
                                                                          PCI_BASE_ADDRESS_1, &pci_ioaddr);
503
                        ioaddr = pci_ioaddr;
504
                        irq = pci_irq_line;
505
                        printk("Device 2 %d %d\n",ioaddr,irq);
506
                }
507
#endif
508
                /* Remove I/O space marker in bit 0. */
509
                ioaddr &= ~3;
510
                if (speedo_debug > 2)
511
                        printk("Found Intel i82557 PCI Speedo at I/O %#lx, IRQ %d.\n",
512
                                   ioaddr, irq);
513
 
514
                /* Get and check the bus-master and latency values. */
515
                pcibios_read_config_word(pci_bus, pci_device_fn,
516
                                                                 PCI_COMMAND, &pci_command);
517
                new_command = pci_command | PCI_COMMAND_MASTER|PCI_COMMAND_IO;
518
                if (pci_command != new_command) {
519
                        printk(KERN_INFO "  The PCI BIOS has not enabled this"
520
                                   " device!  Updating PCI command %4.4x->%4.4x.\n",
521
                                   pci_command, new_command);
522
                        pcibios_write_config_word(pci_bus, pci_device_fn,
523
                                                                          PCI_COMMAND, new_command);
524
                }
525
                pcibios_read_config_byte(pci_bus, pci_device_fn,
526
                                                                 PCI_LATENCY_TIMER, &pci_latency);
527
                if (pci_latency < 32) {
528
                        printk("  PCI latency timer (CFLT) is unreasonably low at %d."
529
                                   "  Setting to 32 clocks.\n", pci_latency);
530
                        pcibios_write_config_byte(pci_bus, pci_device_fn,
531
                                                                          PCI_LATENCY_TIMER, 32);
532
                } else if (speedo_debug > 1)
533
                        printk("  PCI latency timer (CFLT) is %#x.\n", pci_latency);
534
 
535
                speedo_found1(dev, ioaddr, irq, cards_found);
536
                dev = NULL;
537
                cards_found++;
538
        }
539
 
540
        return cards_found;
541
}
542
 
543
static void speedo_found1(struct device *dev, long ioaddr, int irq,
544
                                                  int card_idx)
545
{
546
        static int did_version = 0;                     /* Already printed version info. */
547
        struct speedo_private *sp;
548
        char *product;
549
        int i, option;
550
        u16 eeprom[0x40];
551
 
552
        if (speedo_debug > 0  &&  did_version++ == 0)
553
                printk(version);
554
 
555
        dev = init_etherdev(dev, sizeof(struct speedo_private));
556
 
557
        if (dev->mem_start > 0)
558
                option = dev->mem_start;
559
        else if (card_idx >= 0  &&  options[card_idx] >= 0)
560
                option = options[card_idx];
561
        else
562
                option = 0;
563
 
564
        /* Read the station address EEPROM before doing the reset.
565
           Perhaps this should even be done before accepting the device,
566
           then we wouldn't have a device name with which to report the error. */
567
        {
568
                u16 sum = 0;
569
                int j;
570
                int addr_len = read_eeprom(ioaddr, 0, 6) == 0xffff ? 8 : 6;
571
 
572
                for (j = 0, i = 0; i < 0x40; i++) {
573
                        u16 value = read_eeprom(ioaddr, i, addr_len);
574
                        eeprom[i] = value;
575
                        sum += value;
576
                        if (i < 3) {
577
                                dev->dev_addr[j++] = value;
578
                                dev->dev_addr[j++] = value >> 8;
579
                        }
580
                }
581
                if (sum != 0xBABA)
582
                        printk(KERN_WARNING "%s: Invalid EEPROM checksum %#4.4x, "
583
                                   "check settings before activating this device!\n",
584
                                   dev->name, sum);
585
                /* Don't  unregister_netdev(dev);  as the EEPro may actually be
586
                   usable, especially if the MAC address is set later. */
587
        }
588
 
589
        /* Reset the chip: stop Tx and Rx processes and clear counters.
590
           This takes less than 10usec and will easily finish before the next
591
           action. */
592
        outl(0, ioaddr + SCBPort);
593
 
594
        if (eeprom[3] & 0x0100)
595
                product = "OEM i82557/i82558 10/100 Ethernet";
596
        else
597
                product = "Intel EtherExpress Pro 10/100";
598
 
599
        printk(KERN_INFO "%s: %s at %#3lx, ", dev->name, product, ioaddr);
600
 
601
        for (i = 0; i < 5; i++)
446 giacomo 602
                printk("%2x:", dev->dev_addr[i]);
603
        printk("%2x, IRQ %d.\n", dev->dev_addr[i], irq);
442 giacomo 604
 
605
#ifndef kernel_bloat
606
        /* OK, this is pure kernel bloat.  I don't like it when other drivers
607
           waste non-pageable kernel space to emit similar messages, but I need
608
           them for bug reports. */
609
        {
610
                const char *connectors[] = {" RJ45", " BNC", " AUI", " MII"};
611
                /* The self-test results must be paragraph aligned. */
612
                s32 str[6], *volatile self_test_results;
613
                int boguscnt = 16000;   /* Timeout for set-test. */
614
                if (eeprom[3] & 0x03)
615
                        printk(KERN_INFO "  Receiver lock-up bug exists -- enabling"
616
                                   " work-around.\n");
617
                printk(KERN_INFO "  Board assembly %4.4x%2.2x-%3.3d, Physical"
618
                           " connectors present:",
619
                           eeprom[8], eeprom[9]>>8, eeprom[9] & 0xff);
620
                for (i = 0; i < 4; i++)
621
                        if (eeprom[5] & (1<<i))
622
                                printk(connectors[i]);
623
                printk("\n"KERN_INFO"  Primary interface chip %s PHY #%d.\n",
624
                           phys[(eeprom[6]>>8)&15], eeprom[6] & 0x1f);
625
                if (eeprom[7] & 0x0700)
626
                        printk(KERN_INFO "    Secondary interface chip %s.\n",
627
                                   phys[(eeprom[7]>>8)&7]);
628
                if (((eeprom[6]>>8) & 0x3f) == DP83840
629
                        ||  ((eeprom[6]>>8) & 0x3f) == DP83840A) {
630
                        int mdi_reg23 = mdio_read(ioaddr, eeprom[6] & 0x1f, 23) | 0x0422;
631
                        if (congenb)
632
                          mdi_reg23 |= 0x0100;
633
                        printk(KERN_INFO"  DP83840 specific setup, setting register 23 to %4.4x.\n",
634
                                   mdi_reg23);
635
                        mdio_write(ioaddr, eeprom[6] & 0x1f, 23, mdi_reg23);
636
                }
637
                if ((option >= 0) && (option & 0x70)) {
638
                        printk(KERN_INFO "  Forcing %dMbs %s-duplex operation.\n",
639
                                   (option & 0x20 ? 100 : 10),
640
                                   (option & 0x10 ? "full" : "half"));
641
                        mdio_write(ioaddr, eeprom[6] & 0x1f, 0,
642
                                           ((option & 0x20) ? 0x2000 : 0) |     /* 100mbps? */
643
                                           ((option & 0x10) ? 0x0100 : 0)); /* Full duplex? */
644
                }
645
 
646
                /* Perform a system self-test. */
647
                self_test_results = (s32*) ((((long) str) + 15) & ~0xf);
648
                self_test_results[0] = 0;
649
                self_test_results[1] = -1;
650
                outl(virt_to_bus(self_test_results) | 1, ioaddr + SCBPort);
651
                do {
652
                        udelay(10);
653
                } while (self_test_results[1] == -1  &&  --boguscnt >= 0);
654
 
655
                if (boguscnt < 0) {             /* Test optimized out. */
656
                        printk(KERN_ERR "Self test failed, status %8.8x:\n"
657
                                   KERN_ERR " Failure to initialize the i82557.\n"
658
                                   KERN_ERR " Verify that the card is a bus-master"
659
                                   " capable slot.\n",
660
                                   self_test_results[1]);
661
                } else
662
                        printk(KERN_INFO "  General self-test: %s.\n"
663
                                   KERN_INFO "  Serial sub-system self-test: %s.\n"
664
                                   KERN_INFO "  Internal registers self-test: %s.\n"
665
                                   KERN_INFO "  ROM checksum self-test: %s (%#8.8x).\n",
666
                                   self_test_results[1] & 0x1000 ? "failed" : "passed",
667
                                   self_test_results[1] & 0x0020 ? "failed" : "passed",
668
                                   self_test_results[1] & 0x0008 ? "failed" : "passed",
669
                                   self_test_results[1] & 0x0004 ? "failed" : "passed",
670
                                   self_test_results[0]);
671
        }
672
#endif  /* kernel_bloat */
673
 
674
        outl(0, ioaddr + SCBPort);
675
 
676
        /* We do a request_region() only to register /proc/ioports info. */
677
        request_region(ioaddr, SPEEDO3_TOTAL_SIZE, "Intel Speedo3 Ethernet");
678
 
679
        dev->base_addr = ioaddr;
680
        dev->irq = irq;
681
 
682
        if (dev->priv == NULL)
683
                dev->priv = kmalloc(sizeof(*sp), GFP_KERNEL);
684
        sp = dev->priv;
685
        memset(sp, 0, sizeof(*sp));
686
        sp->next_module = root_speedo_dev;
687
        root_speedo_dev = dev;
688
 
689
        sp->full_duplex = option >= 0 && (option & 0x10) ? 1 : 0;
690
        if (card_idx >= 0) {
691
                if (full_duplex[card_idx] >= 0)
692
                        sp->full_duplex = full_duplex[card_idx];
693
        }
694
        sp->default_port = option >= 0 ? (option & 0x0f) : 0;
695
 
696
        sp->phy[0] = eeprom[6];
697
        sp->phy[1] = eeprom[7];
698
        sp->rx_bug = (eeprom[3] & 0x03) == 3 ? 0 : 1;
699
 
700
        if (sp->rx_bug)
701
                printk(KERN_INFO "  Receiver lock-up workaround activated.\n");
702
 
703
        /* The Speedo-specific entries in the device structure. */
704
        dev->open = &speedo_open;
705
        dev->hard_start_xmit = &speedo_start_xmit;
706
        dev->stop = &speedo_close;
707
        dev->get_stats = &speedo_get_stats;
708
        dev->set_multicast_list = &set_rx_mode;
709
        dev->do_ioctl = &speedo_ioctl;
710
 
711
        return;
712
}
713
 
714
/* Serial EEPROM section.
715
   A "bit" grungy, but we work our way through bit-by-bit :->. */
716
/*  EEPROM_Ctrl bits. */
717
#define EE_SHIFT_CLK    0x01    /* EEPROM shift clock. */
718
#define EE_CS                   0x02    /* EEPROM chip select. */
719
#define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
720
#define EE_WRITE_0              0x01
721
#define EE_WRITE_1              0x05
722
#define EE_DATA_READ    0x08    /* EEPROM chip data out. */
723
#define EE_ENB                  (0x4800 | EE_CS)
724
 
725
/* Delay between EEPROM clock transitions.
726
   This will actually work with no delay on 33Mhz PCI.  */
727
#define eeprom_delay(nanosec)           udelay(1);
728
 
729
/* The EEPROM commands include the alway-set leading bit. */
730
#define EE_WRITE_CMD    (5 << addr_len)
731
#define EE_READ_CMD             (6 << addr_len)
732
#define EE_ERASE_CMD    (7 << addr_len)
733
 
734
static int read_eeprom(long ioaddr, int location, int addr_len)
735
{
736
        unsigned short retval = 0;
737
        int ee_addr = ioaddr + SCBeeprom;
738
        int read_cmd = location | EE_READ_CMD;
739
        int i;
740
 
741
        outw(EE_ENB & ~EE_CS, ee_addr);
742
        outw(EE_ENB, ee_addr);
743
 
744
        /* Shift the read command bits out. */
745
        for (i = 12; i >= 0; i--) {
746
                short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
747
                outw(EE_ENB | dataval, ee_addr);
748
                eeprom_delay(100);
749
                outw(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
750
                eeprom_delay(150);
751
        }
752
        outw(EE_ENB, ee_addr);
753
 
754
        for (i = 15; i >= 0; i--) {
755
                outw(EE_ENB | EE_SHIFT_CLK, ee_addr);
756
                eeprom_delay(100);
757
                retval = (retval << 1) | ((inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
758
                outw(EE_ENB, ee_addr);
759
                eeprom_delay(100);
760
        }
761
 
762
        /* Terminate the EEPROM access. */
763
        outw(EE_ENB & ~EE_CS, ee_addr);
764
        return retval;
765
}
766
 
767
static int mdio_read(long ioaddr, int phy_id, int location)
768
{
769
        int val, boguscnt = 64*10;              /* <64 usec. to complete, typ 27 ticks */
770
        outl(0x08000000 | (location<<16) | (phy_id<<21), ioaddr + SCBCtrlMDI);
771
        do {
772
                val = inl(ioaddr + SCBCtrlMDI);
773
                if (--boguscnt < 0) {
774
                        printk(KERN_ERR " mdio_read() timed out with val = %8.8x.\n", val);
775
                }
776
        } while (! (val & 0x10000000));
777
        return val & 0xffff;
778
}
779
 
780
static int mdio_write(long ioaddr, int phy_id, int location, int value)
781
{
782
        int val, boguscnt = 64*10;              /* <64 usec. to complete, typ 27 ticks */
783
        outl(0x04000000 | (location<<16) | (phy_id<<21) | value,
784
                 ioaddr + SCBCtrlMDI);
785
        do {
786
                val = inl(ioaddr + SCBCtrlMDI);
787
                if (--boguscnt < 0) {
788
                        printk(KERN_ERR" mdio_write() timed out with val = %8.8x.\n", val);
789
                }
790
        } while (! (val & 0x10000000));
791
        return val & 0xffff;
792
}
793
 
794
 
795
static int
796
speedo_open(struct device *dev)
797
{
798
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
799
        long ioaddr = dev->base_addr;
800
 
801
#ifdef notdef
802
        /* We could reset the chip, but should not need to. */
803
        outl(0, ioaddr + SCBPort);
804
        udelay(10);
805
#endif
806
 
807
        if (request_irq(dev->irq, &speedo_interrupt, SA_SHIRQ,
808
                                        "Intel EtherExpress Pro 10/100 Ethernet", dev)) {
809
                return -EAGAIN;
810
        }
811
        if (speedo_debug > 1)
812
                printk(KERN_DEBUG "%s: speedo_open() irq %d.\n", dev->name, dev->irq);
813
 
814
        MOD_INC_USE_COUNT;
815
 
816
        /* Retrigger negotiation to reset previous errors. */
817
        if ((sp->phy[0] & 0x8000) == 0) {
818
                int phy_addr = sp->phy[0] & 0x1f ;
819
                /* Use 0x3300 for restarting NWay, other values to force xcvr:
820
                   0x0000 10-HD
821
                   0x0100 10-FD
822
                   0x2000 100-HD
823
                   0x2100 100-FD
824
                */
825
#ifdef honor_default_port
826
                mdio_write(ioaddr, phy_addr, 0, mii_ctrl[dev->default_port & 7]);
827
#else
828
                mdio_write(ioaddr, phy_addr, 0, 0x3300);
829
#endif
830
        }
831
 
832
        /* Load the statistics block address. */
833
        wait_for_cmd_done(ioaddr + SCBCmd);
834
        outl(virt_to_bus(&sp->lstats), ioaddr + SCBPointer);
835
        outw(INT_MASK | CU_STATSADDR, ioaddr + SCBCmd);
836
        sp->lstats.done_marker = 0;
837
 
838
        speedo_init_rx_ring(dev);
839
        wait_for_cmd_done(ioaddr + SCBCmd);
840
        outl(0, ioaddr + SCBPointer);
841
        outw(INT_MASK | RX_ADDR_LOAD, ioaddr + SCBCmd);
842
 
843
        /* Todo: verify that we must wait for previous command completion. */
844
        wait_for_cmd_done(ioaddr + SCBCmd);
845
        outl(virt_to_bus(sp->rx_ringp[0]), ioaddr + SCBPointer);
846
        outw(INT_MASK | RX_START, ioaddr + SCBCmd);
847
 
848
        /* Fill the first command with our physical address. */
849
        {
850
                u16 *eaddrs = (u16 *)dev->dev_addr;
851
                u16 *setup_frm = (u16 *)&(sp->tx_ring[0].tx_desc_addr);
852
 
853
                /* Avoid a bug(?!) here by marking the command already completed. */
854
                sp->tx_ring[0].status = ((CmdSuspend | CmdIASetup) << 16) | 0xa000;
855
                sp->tx_ring[0].link = virt_to_bus(&(sp->tx_ring[1]));
856
                *setup_frm++ = eaddrs[0];
857
                *setup_frm++ = eaddrs[1];
858
                *setup_frm++ = eaddrs[2];
859
        }
860
        sp->last_cmd = (struct descriptor_net *)&sp->tx_ring[0];
861
        sp->cur_tx = 1;
862
        sp->dirty_tx = 0;
863
        sp->tx_full = 0;
864
 
865
        wait_for_cmd_done(ioaddr + SCBCmd);
866
        outl(0, ioaddr + SCBPointer);
867
        outw(INT_MASK | CU_CMD_BASE, ioaddr + SCBCmd);
868
 
869
        dev->if_port = sp->default_port;
870
 
871
        sp->in_interrupt = 0;
872
        dev->tbusy = 0;
873
        dev->interrupt = 0;
874
        dev->start = 1;
875
 
876
        /* Start the chip's Tx process and unmask interrupts. */
877
        /* Todo: verify that we must wait for previous command completion. */
878
        wait_for_cmd_done(ioaddr + SCBCmd);
879
        outl(virt_to_bus(&sp->tx_ring[0]), ioaddr + SCBPointer);
880
        outw(CU_START, ioaddr + SCBCmd);
881
 
882
        /* Setup the chip and configure the multicast list. */
883
        sp->mc_setup_frm = NULL;
884
        sp->mc_setup_frm_len = 0;
885
        sp->mc_setup_busy = 0;
886
        sp->rx_mode = -1;                       /* Invalid -> always reset the mode. */
887
        set_rx_mode(dev);
888
 
889
        if (speedo_debug > 2) {
890
                printk(KERN_DEBUG "%s: Done speedo_open(), status %8.8x.\n",
891
                           dev->name, inw(ioaddr + SCBStatus));
892
        }
893
        /* Set the timer.  The timer serves a dual purpose:
894
           1) to monitor the media interface (e.g. link beat) and perhaps switch
895
           to an alternate media type
896
           2) to monitor Rx activity, and restart the Rx process if the receiver
897
           hangs. */
898
        init_timer(&sp->timer);
899
        sp->timer.expires = RUN_AT((24*HZ)/10);                         /* 2.4 sec. */
900
        sp->timer.data = (unsigned long)dev;
901
        sp->timer.function = &speedo_timer;                                     /* timer handler */
902
        add_timer(&sp->timer);
903
 
904
        wait_for_cmd_done(ioaddr + SCBCmd);
905
        outw(CU_DUMPSTATS, ioaddr + SCBCmd);
906
        /* No need to wait for the command unit to accept here. */
907
        if ((sp->phy[0] & 0x8000) == 0)
908
                mdio_read(ioaddr, sp->phy[0] & 0x1f, 0);
909
        return 0;
910
}
911
 
912
/* Media monitoring and control. */
913
static void speedo_timer(unsigned long data)
914
{
915
        struct device *dev = (struct device *)data;
916
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
917
 
918
        if (speedo_debug > 3) {
919
                long ioaddr = dev->base_addr;
920
                printk(KERN_DEBUG "%s: Media control tick, status %4.4x.\n",
921
                           dev->name, inw(ioaddr + SCBStatus));
922
        }
923
        if (sp->rx_mode < 0  ||
924
                (sp->rx_bug  && jiffies - sp->last_rx_time > 2*HZ)) {
925
                /* We haven't received a packet in a Long Time.  We might have been
926
                   bitten by the receiver hang bug.  This can be cleared by sending
927
                   a set multicast list command. */
928
                set_rx_mode(dev);
929
        }
930
        /* We must continue to monitor the media. */
931
        sp->timer.expires = RUN_AT(2*HZ);                       /* 2.0 sec. */
932
        add_timer(&sp->timer);
933
}
934
 
935
/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
936
static void
937
speedo_init_rx_ring(struct device *dev)
938
{
939
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
940
        struct RxFD *rxf, *last_rxf = NULL;
941
        int i;
942
 
943
        sp->cur_rx = 0;
944
 
945
        for (i = 0; i < RX_RING_SIZE; i++) {
946
                struct sk_buff *skb;
947
                skb = dev_alloc_skb(PKT_BUF_SZ + sizeof(struct RxFD));
948
                sp->rx_skbuff[i] = skb;
949
                if (skb == NULL)
950
                        break;                  /* OK.  Just initially short of Rx bufs. */
951
                skb->dev = dev;                 /* Mark as being used by this device. */
952
                rxf = (struct RxFD *)skb->tail;
953
                sp->rx_ringp[i] = rxf;
954
                skb_reserve(skb, sizeof(struct RxFD));
955
                if (last_rxf)
956
                        last_rxf->link = virt_to_bus(rxf);
957
                last_rxf = rxf;
958
                rxf->status = 0x00000001;                       /* '1' is flag value only. */
959
                rxf->link = 0;                                          /* None yet. */
960
                /* This field unused by i82557, we use it as a consistency check. */
961
#ifdef final_version
962
                rxf->rx_buf_addr = 0xffffffff;
963
#else
964
                rxf->rx_buf_addr = virt_to_bus(skb->tail);
965
#endif
966
                rxf->count = 0;
967
                rxf->size = PKT_BUF_SZ;
968
        }
969
        sp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
970
        /* Mark the last entry as end-of-list. */
971
        last_rxf->status = 0xC0000002;                  /* '2' is flag value only. */
972
        sp->last_rxf = last_rxf;
973
}
974
 
975
static void speedo_tx_timeout(struct device *dev)
976
{
977
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
978
        long ioaddr = dev->base_addr;
979
 
980
        printk(KERN_WARNING "%s: Transmit timed out: status %4.4x "
981
                   " %4.4x at %d/%d command %8.8x.\n",
982
                   dev->name, inw(ioaddr + SCBStatus), inw(ioaddr + SCBCmd),
983
                   sp->dirty_tx, sp->cur_tx,
984
                   sp->tx_ring[sp->dirty_tx % TX_RING_SIZE].status);
985
        if ((inw(ioaddr + SCBStatus) & 0x00C0) != 0x0080) {
986
                printk(KERN_WARNING "%s: Trying to restart the transmitter...\n",
987
                           dev->name);
988
                outl(virt_to_bus(&sp->tx_ring[sp->dirty_tx % TX_RING_SIZE]),
989
                         ioaddr + SCBPointer);
990
                outw(CU_START, ioaddr + SCBCmd);
991
        } else {
992
                outw(DRVR_INT, ioaddr + SCBCmd);
993
        }
994
        /* Reset the MII transceiver, suggested by Fred Young @ scalable.com. */
995
        if ((sp->phy[0] & 0x8000) == 0) {
996
                int phy_addr = sp->phy[0] & 0x1f;
997
                mdio_write(ioaddr, phy_addr, 0, 0x0400);
998
                mdio_write(ioaddr, phy_addr, 1, 0x0000);
999
                mdio_write(ioaddr, phy_addr, 4, 0x0000);
1000
                mdio_write(ioaddr, phy_addr, 0, 0x8000);
1001
#ifdef honor_default_port
1002
                mdio_write(ioaddr, phy_addr, 0, mii_ctrl[dev->default_port & 7]);
1003
#endif
1004
        }
1005
        sp->stats.tx_errors++;
1006
        dev->trans_start = jiffies;
1007
        return;
1008
}
1009
 
1010
static int
1011
speedo_start_xmit(struct sk_buff *skb, struct device *dev)
1012
{
1013
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
1014
        long ioaddr = dev->base_addr;
1015
        int entry;
1016
 
1017
        /* Block a timer-based transmit from overlapping.  This could better be
1018
           done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
1019
           If this ever occurs the queue layer is doing something evil! */
1020
        if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
1021
                int tickssofar = jiffies - dev->trans_start;
1022
                if (tickssofar < TX_TIMEOUT - 2)
1023
                        return 1;
1024
                if (tickssofar < TX_TIMEOUT) {
1025
                        /* Reap sent packets from the full Tx queue. */
1026
                        outw(DRVR_INT, ioaddr + SCBCmd);
1027
                        return 1;
1028
                }
1029
                speedo_tx_timeout(dev);
1030
                return 1;
1031
        }
1032
 
1033
        /* Caution: the write order is important here, set the base address
1034
           with the "ownership" bits last. */
1035
 
1036
        {       /* Prevent interrupts from changing the Tx ring from underneath us. */
1037
                unsigned long flags;
1038
 
1039
                save_flags(flags);
1040
                cli();
1041
                /* Calculate the Tx descriptor entry. */
1042
                entry = sp->cur_tx++ % TX_RING_SIZE;
1043
 
1044
                sp->tx_skbuff[entry] = skb;
1045
                /* Todo: be a little more clever about setting the interrupt bit. */
1046
                sp->tx_ring[entry].status =
1047
                        (CmdSuspend | CmdTx | CmdTxFlex) << 16;
1048
                sp->tx_ring[entry].link =
1049
                  virt_to_bus(&sp->tx_ring[sp->cur_tx % TX_RING_SIZE]);
1050
                sp->tx_ring[entry].tx_desc_addr =
1051
                  virt_to_bus(&sp->tx_ring[entry].tx_buf_addr0);
1052
                /* The data region is always in one buffer descriptor, Tx FIFO
1053
                   threshold of 256. */
1054
                sp->tx_ring[entry].count = 0x01208000;
1055
                sp->tx_ring[entry].tx_buf_addr0 = virt_to_bus(skb->data);
1056
                sp->tx_ring[entry].tx_buf_size0 = skb->len;
1057
                /* Todo: perhaps leave the interrupt bit set if the Tx queue is more
1058
                   than half full.  Argument against: we should be receiving packets
1059
                   and scavenging the queue.  Argument for: if so, it shouldn't
1060
                   matter. */
1061
                sp->last_cmd->command &= ~(CmdSuspend | CmdIntr);
1062
                sp->last_cmd = (struct descriptor_net *)&sp->tx_ring[entry];
1063
                restore_flags(flags);
1064
                /* Trigger the command unit resume. */
1065
                wait_for_cmd_done(ioaddr + SCBCmd);
1066
                outw(CU_RESUME, ioaddr + SCBCmd);
1067
        }
1068
 
1069
        /* Leave room for set_rx_mode() to fill two entries. */
1070
        if (sp->cur_tx - sp->dirty_tx > TX_RING_SIZE - 3)
1071
                sp->tx_full = 1;
1072
        else
1073
                clear_bit(0, (void*)&dev->tbusy);
1074
 
1075
        dev->trans_start = jiffies;
1076
 
1077
        return 0;
1078
}
1079
 
1080
/* The interrupt handler does all of the Rx thread work and cleans up
1081
   after the Tx thread. */
1082
static void speedo_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1083
{
1084
        struct device *dev = (struct device *)dev_instance;
1085
        struct speedo_private *sp;
1086
        long ioaddr, boguscnt = max_interrupt_work;
1087
        unsigned short status;
1088
 
1089
#ifndef final_version
1090
        if (dev == NULL) {
1091
                printk(KERN_ERR "speedo_interrupt(): irq %d for unknown device.\n", irq);
1092
                return;
1093
        }
1094
#endif
1095
 
1096
        ioaddr = dev->base_addr;
1097
        sp = (struct speedo_private *)dev->priv;
1098
#ifndef final_version
1099
        /* A lock to prevent simultaneous entry on SMP machines. */
1100
        if (test_and_set_bit(0, (void*)&sp->in_interrupt)) {
1101
                printk(KERN_ERR"%s: SMP simultaneous entry of an interrupt handler.\n",
1102
                           dev->name);
1103
                sp->in_interrupt = 0;   /* Avoid halting machine. */
1104
                return;
1105
        }
1106
        dev->interrupt = 1;
1107
#endif
1108
 
1109
        do {
1110
                status = inw(ioaddr + SCBStatus);
1111
                /* Acknowledge all of the current interrupt sources ASAP. */
1112
                outw(status & 0xfc00, ioaddr + SCBStatus);
1113
 
1114
                if (speedo_debug > 4)
1115
                        printk(KERN_DEBUG "%s: interrupt  status=%#4.4x.\n",
1116
                                   dev->name, status);
1117
 
1118
                if ((status & 0xfc00) == 0)
1119
                        break;
1120
 
1121
                if (status & 0x4000)     /* Packet received. */
1122
                        speedo_rx(dev);
1123
 
1124
                if (status & 0x1000) {
1125
                  if ((status & 0x003c) == 0x0028) /* No more Rx buffers. */
1126
                        outw(RX_RESUMENR, ioaddr + SCBCmd);
1127
                  else if ((status & 0x003c) == 0x0008) { /* No resources (why?!) */
1128
                        /* No idea of what went wrong.  Restart the receiver. */
1129
                        outl(virt_to_bus(sp->rx_ringp[sp->cur_rx % RX_RING_SIZE]),
1130
                                 ioaddr + SCBPointer);
1131
                        outw(RX_START, ioaddr + SCBCmd);
1132
                  }
1133
                  sp->stats.rx_errors++;
1134
                }
1135
 
1136
                /* User interrupt, Command/Tx unit interrupt or CU not active. */
1137
                if (status & 0xA400) {
1138
                        unsigned int dirty_tx = sp->dirty_tx;
1139
 
1140
                        while (sp->cur_tx - dirty_tx > 0) {
1141
                                int entry = dirty_tx % TX_RING_SIZE;
1142
                                int status = sp->tx_ring[entry].status;
1143
 
1144
                                if (speedo_debug > 5)
1145
                                        printk(KERN_DEBUG " scavenge candidate %d status %4.4x.\n",
1146
                                                   entry, status);
1147
                                if ((status & StatusComplete) == 0)
1148
                                        break;                  /* It still hasn't been processed. */
1149
                                /* Free the original skb. */
1150
                                if (sp->tx_skbuff[entry]) {
1151
                                        sp->stats.tx_packets++; /* Count only user packets. */
1152
                                        dev_free_skb(sp->tx_skbuff[entry]);
1153
                                        sp->tx_skbuff[entry] = 0;
1154
                                } else if ((sp->tx_ring[entry].status&0x70000) == CmdNOp << 16)
1155
                                        sp->mc_setup_busy = 0;
1156
                                dirty_tx++;
1157
                        }
1158
 
1159
#ifndef final_version
1160
                        if (sp->cur_tx - dirty_tx > TX_RING_SIZE) {
1161
                                printk(KERN_ERR "out-of-sync dirty pointer, %d vs. %d,"
1162
                                           " full=%d.\n",
1163
                                           dirty_tx, sp->cur_tx, sp->tx_full);
1164
                                dirty_tx += TX_RING_SIZE;
1165
                        }
1166
#endif
1167
 
1168
                        if (sp->tx_full && dev->tbusy
1169
                                && dirty_tx > sp->cur_tx - TX_RING_SIZE + 2) {
1170
                                /* The ring is no longer full, clear tbusy. */
1171
                                sp->tx_full = 0;
1172
                                clear_bit(0, (void*)&dev->tbusy);
1173
                                mark_bh(NET_BH);
1174
                        }
1175
 
1176
                        sp->dirty_tx = dirty_tx;
1177
                }
1178
 
1179
                if (--boguscnt < 0) {
1180
                        printk(KERN_ERR "%s: Too much work at interrupt, status=0x%4.4x.\n",
1181
                                   dev->name, status);
1182
                        /* Clear all interrupt sources. */
1183
                        outl(0xfc00, ioaddr + SCBStatus);
1184
                        break;
1185
                }
1186
        } while (1);
1187
 
1188
        if (speedo_debug > 3)
1189
                printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1190
                           dev->name, inw(ioaddr + SCBStatus));
1191
 
1192
        dev->interrupt = 0;
1193
        clear_bit(0, (void*)&sp->in_interrupt);
1194
        return;
1195
}
1196
 
1197
static int
1198
speedo_rx(struct device *dev)
1199
{
1200
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
1201
        int entry = sp->cur_rx % RX_RING_SIZE;
1202
        int status;
1203
        int rx_work_limit = sp->dirty_rx + RX_RING_SIZE - sp->cur_rx;
1204
 
1205
        if (speedo_debug > 4)
1206
                printk(KERN_DEBUG " In speedo_rx().\n");
1207
        /* If we own the next entry, it's a new packet. Send it up. */
1208
        while (sp->rx_ringp[entry] != NULL &&
1209
                   (status = sp->rx_ringp[entry]->status) & RxComplete) {
1210
 
1211
                if (--rx_work_limit < 0)
1212
                        break;
1213
                if (speedo_debug > 4)
1214
                        printk(KERN_DEBUG "  speedo_rx() status %8.8x len %d.\n", status,
1215
                                   sp->rx_ringp[entry]->count & 0x3fff);
1216
                if ((status & (RxErrTooBig|RxOK)) != RxOK) {
1217
                        if (status & RxErrTooBig)
1218
                                printk(KERN_ERR "%s: Ethernet frame overran the Rx buffer, "
1219
                                           "status %8.8x!\n", dev->name, status);
1220
                        else if ( ! (status & 0x2000)) {
1221
                                /* There was a fatal error.  This *should* be impossible. */
1222
                                sp->stats.rx_errors++;
1223
                                printk(KERN_ERR "%s: Anomalous event in speedo_rx(), "
1224
                                           "status %8.8x.\n",
1225
                                           dev->name, status);
1226
                        }
1227
                } else {
1228
                        int pkt_len = sp->rx_ringp[entry]->count & 0x3fff;
1229
                        struct sk_buff *skb;
1230
 
1231
                        /* Check if the packet is long enough to just accept without
1232
                           copying to a properly sized skbuff. */
1233
                        if (pkt_len < rx_copybreak
1234
                                && (skb = dev_alloc_skb(pkt_len + 2)) != 0) {
1235
                                skb->dev = dev;
1236
                                skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
1237
                                /* 'skb_put()' points to the start of sk_buff data area. */
446 giacomo 1238
#if 0
442 giacomo 1239
                                /* Packet is in one chunk -- we can copy + cksum. */
1240
                                eth_copy_and_sum(skb,
1241
                                                                 bus_to_virt(sp->rx_ringp[entry]->rx_buf_addr),
1242
                                                                 pkt_len, 0);
1243
                                skb_put(skb, pkt_len);
1244
#else
1245
                                memcpy(skb_put(skb, pkt_len),
1246
                                           bus_to_virt(sp->rx_ringp[entry]->rx_buf_addr), pkt_len);
1247
#endif
1248
                        } else {
1249
                                void *temp;
1250
                                /* Pass up the already-filled skbuff. */
1251
                                skb = sp->rx_skbuff[entry];
1252
                                if (skb == NULL) {
1253
                                        printk(KERN_ERR "%s: Inconsistent Rx descriptor chain.\n",
1254
                                                   dev->name);
1255
                                        break;
1256
                                }
1257
                                sp->rx_skbuff[entry] = NULL;
1258
                                temp = skb_put(skb, pkt_len);
1259
                                if (bus_to_virt(sp->rx_ringp[entry]->rx_buf_addr) != temp)
1260
                                        printk(KERN_ERR "%s: Rx consistency error -- the skbuff "
1261
                                                   "addresses do not match in speedo_rx: %p vs. %p "
1262
                                                   "/ %p.\n", dev->name,
1263
                                                   bus_to_virt(sp->rx_ringp[entry]->rx_buf_addr),
1264
                                                   skb->head, temp);
1265
                                sp->rx_ringp[entry] = NULL;
1266
                        }
1267
                        skb->protocol = eth_type_trans(skb, dev);
1268
                        netif_rx(skb);
1269
                        sp->stats.rx_packets++;
1270
                }
1271
                entry = (++sp->cur_rx) % RX_RING_SIZE;
1272
        }
1273
 
1274
        /* Refill the Rx ring buffers. */
1275
        for (; sp->dirty_rx < sp->cur_rx; sp->dirty_rx++) {
1276
                struct RxFD *rxf;
1277
                entry = sp->dirty_rx % RX_RING_SIZE;
1278
                if (sp->rx_skbuff[entry] == NULL) {
1279
                        struct sk_buff *skb;
1280
                        /* Get a fresh skbuff to replace the consumed one. */
1281
                        skb = dev_alloc_skb(PKT_BUF_SZ + sizeof(struct RxFD));
1282
                        sp->rx_skbuff[entry] = skb;
1283
                        if (skb == NULL) {
1284
                                sp->rx_ringp[entry] = NULL;
1285
                                break;                  /* Better luck next time!  */
1286
                        }
1287
                        rxf = sp->rx_ringp[entry] = (struct RxFD *)skb->tail;
1288
                        skb->dev = dev;
1289
                        skb_reserve(skb, sizeof(struct RxFD));
1290
                        rxf->rx_buf_addr = virt_to_bus(skb->tail);
1291
                } else {
1292
                        rxf = sp->rx_ringp[entry];
1293
                }
1294
                rxf->status = 0xC0000001;       /* '1' for driver use only. */
1295
                rxf->link = 0;                  /* None yet. */
1296
                rxf->count = 0;
1297
                rxf->size = PKT_BUF_SZ;
1298
                sp->last_rxf->link = virt_to_bus(rxf);
1299
                sp->last_rxf->status &= ~0xC0000000;
1300
                sp->last_rxf = rxf;
1301
        }
1302
 
1303
        sp->last_rx_time = jiffies;
1304
        return 0;
1305
}
1306
 
1307
static int
1308
speedo_close(struct device *dev)
1309
{
1310
        long ioaddr = dev->base_addr;
1311
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
1312
        int i;
1313
 
1314
        dev->start = 0;
1315
        dev->tbusy = 1;
1316
 
1317
        if (speedo_debug > 1)
1318
                printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.\n",
1319
                           dev->name, inw(ioaddr + SCBStatus));
1320
 
1321
        /* Shut off the media monitoring timer. */
1322
        del_timer(&sp->timer);
1323
 
1324
        /* Disable interrupts, and stop the chip's Rx process. */
1325
        outw(INT_MASK, ioaddr + SCBCmd);
1326
        outw(INT_MASK | RX_ABORT, ioaddr + SCBCmd);
1327
 
1328
        free_irq(dev->irq, dev);
1329
 
1330
        /* Free all the skbuffs in the Rx and Tx queues. */
1331
        for (i = 0; i < RX_RING_SIZE; i++) {
1332
                struct sk_buff *skb = sp->rx_skbuff[i];
1333
                sp->rx_skbuff[i] = 0;
1334
                /* Clear the Rx descriptors. */
1335
                if (skb)
1336
                        dev_free_skb(skb);
1337
        }
1338
 
1339
        for (i = 0; i < TX_RING_SIZE; i++) {
1340
                struct sk_buff *skb = sp->tx_skbuff[i];
1341
                sp->tx_skbuff[i] = 0;
1342
                /* Clear the Tx descriptors. */
1343
                if (skb)
1344
                        dev_free_skb(skb);
1345
        }
1346
        if (sp->mc_setup_frm) {
1347
                kfree(sp->mc_setup_frm);
1348
                sp->mc_setup_frm_len = 0;
1349
        }
1350
 
1351
        /* Print a few items for debugging. */
1352
        if (speedo_debug > 3) {
1353
                int phy_num = sp->phy[0] & 0x1f;
1354
                printk(KERN_DEBUG "%s:Printing Rx ring (next to receive into %d).\n",
1355
                           dev->name, sp->cur_rx);
1356
 
1357
                for (i = 0; i < RX_RING_SIZE; i++)
1358
                        printk(KERN_DEBUG "  Rx ring entry %d  %8.8x.\n",
1359
                                   i, (int)sp->rx_ringp[i]->status);
1360
 
1361
                for (i = 0; i < 5; i++)
1362
                        printk(KERN_DEBUG "  PHY index %d register %d is %4.4x.\n",
1363
                                   phy_num, i, mdio_read(ioaddr, phy_num, i));
1364
                for (i = 21; i < 26; i++)
1365
                        printk(KERN_DEBUG "  PHY index %d register %d is %4.4x.\n",
1366
                                   phy_num, i, mdio_read(ioaddr, phy_num, i));
1367
        }
1368
        MOD_DEC_USE_COUNT;
1369
 
1370
        return 0;
1371
}
1372
 
1373
/* The Speedo-3 has an especially awkward and unusable method of getting
1374
   statistics out of the chip.  It takes an unpredictable length of time
1375
   for the dump-stats command to complete.  To avoid a busy-wait loop we
1376
   update the stats with the previous dump results, and then trigger a
1377
   new dump.
1378
 
1379
   These problems are mitigated by the current /proc implementation, which
1380
   calls this routine first to judge the output length, and then to emit the
1381
   output.
1382
 
1383
   Oh, and incoming frames are dropped while executing dump-stats!
1384
   */
1385
static struct enet_statistics *
1386
speedo_get_stats(struct device *dev)
1387
{
1388
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
1389
        long ioaddr = dev->base_addr;
1390
 
1391
        if (sp->lstats.done_marker == 0xA007) { /* Previous dump finished */
1392
                sp->stats.tx_aborted_errors += sp->lstats.tx_coll16_errs;
1393
                sp->stats.tx_window_errors += sp->lstats.tx_late_colls;
1394
                sp->stats.tx_fifo_errors += sp->lstats.tx_underruns;
1395
                sp->stats.tx_fifo_errors += sp->lstats.tx_lost_carrier;
1396
                /*sp->stats.tx_deferred += sp->lstats.tx_deferred;*/
1397
                sp->stats.collisions += sp->lstats.tx_total_colls;
1398
                sp->stats.rx_crc_errors += sp->lstats.rx_crc_errs;
1399
                sp->stats.rx_frame_errors += sp->lstats.rx_align_errs;
1400
                sp->stats.rx_over_errors += sp->lstats.rx_resource_errs;
1401
                sp->stats.rx_fifo_errors += sp->lstats.rx_overrun_errs;
1402
                sp->stats.rx_length_errors += sp->lstats.rx_runt_errs;
1403
                sp->lstats.done_marker = 0x0000;
1404
                if (dev->start) {
1405
                        wait_for_cmd_done(ioaddr + SCBCmd);
1406
                        outw(CU_DUMPSTATS, ioaddr + SCBCmd);
1407
                }
1408
        }
1409
        return &sp->stats;
1410
}
1411
 
1412
static int speedo_ioctl(struct device *dev, struct ifreq *rq, int cmd)
1413
{
1414
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
1415
        long ioaddr = dev->base_addr;
1416
        u16 *data = (u16 *)&rq->ifr_data;
1417
        int phy = sp->phy[0] & 0x1f;
1418
 
1419
    switch(cmd) {
1420
        case SIOCDEVPRIVATE:            /* Get the address of the PHY in use. */
1421
                data[0] = phy;
1422
        case SIOCDEVPRIVATE+1:          /* Read the specified MII register. */
1423
                data[3] = mdio_read(ioaddr, data[0], data[1]);
1424
                return 0;
1425
        case SIOCDEVPRIVATE+2:          /* Write the specified MII register */
1426
                if (!suser())
1427
                        return -EPERM;
1428
                mdio_write(ioaddr, data[0], data[1], data[2]);
1429
                return 0;
1430
        default:
1431
                return -EOPNOTSUPP;
1432
        }
1433
}
1434
 
1435
/* Set or clear the multicast filter for this adaptor.
1436
   This is very ugly with Intel chips -- we usually have to execute an
1437
   entire configuration command, plus process a multicast command.
1438
   This is complicated.  We must put a large configuration command and
1439
   an arbitrarily-sized multicast command in the transmit list.
1440
   To minimize the disruption -- the previous command might have already
1441
   loaded the link -- we convert the current command block, normally a Tx
1442
   command, into a no-op and link it to the new command.
1443
*/
1444
static void
1445
set_rx_mode(struct device *dev)
1446
{
1447
        struct speedo_private *sp = (struct speedo_private *)dev->priv;
1448
        long ioaddr = dev->base_addr;
1449
        struct descriptor_net *last_cmd;
1450
        char new_rx_mode;
1451
        unsigned long flags;
1452
        int entry, i;
1453
 
1454
        if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
1455
                new_rx_mode = 3;
1456
        } else if ((dev->flags & IFF_ALLMULTI)  ||
1457
                           dev->mc_count > multicast_filter_limit) {
1458
                new_rx_mode = 1;
1459
        } else
1460
                new_rx_mode = 0;
1461
 
1462
        if (sp->cur_tx - sp->dirty_tx >= TX_RING_SIZE - 1) {
1463
          /* The Tx ring is full -- don't add anything!  Presumably the new mode
1464
                 is in config_cmd_data and will be added anyway. */
1465
                sp->rx_mode = -1;
1466
                return;
1467
        }
1468
 
1469
        if (new_rx_mode != sp->rx_mode) {
1470
                u8 *config_cmd_data;
1471
 
1472
                save_flags(flags);              /* Lock to protect sp->cur_tx. */
1473
                cli();
1474
                entry = sp->cur_tx++ % TX_RING_SIZE;
1475
                last_cmd = sp->last_cmd;
1476
                sp->last_cmd = (struct descriptor_net *)&sp->tx_ring[entry];
1477
                restore_flags(flags);
1478
 
1479
                sp->tx_skbuff[entry] = 0;                       /* Redundant. */
1480
                sp->tx_ring[entry].status = (CmdSuspend | CmdConfigure) << 16;
1481
                sp->tx_ring[entry].link =
1482
                        virt_to_bus(&sp->tx_ring[(entry + 1) % TX_RING_SIZE]);
1483
                config_cmd_data = (void *)&sp->tx_ring[entry].tx_desc_addr;
1484
                /* Construct a full CmdConfig frame. */
1485
                memcpy(config_cmd_data, i82558_config_cmd, sizeof(i82558_config_cmd));
1486
                config_cmd_data[1] = (txfifo << 4) | rxfifo;
1487
                config_cmd_data[4] = rxdmacount;
1488
                config_cmd_data[5] = txdmacount + 0x80;
1489
                config_cmd_data[15] |= (new_rx_mode & 2) ? 1 : 0;
1490
                config_cmd_data[19] |= sp->full_duplex ? 0x40 : 0;
1491
                config_cmd_data[21] = (new_rx_mode & 1) ? 0x0D : 0x05;
1492
                if (sp->phy[0] & 0x8000) {                      /* Use the AUI port instead. */
1493
                        config_cmd_data[15] |= 0x80;
1494
                        config_cmd_data[8] = 0;
1495
                }
1496
                /* Trigger the command unit resume. */
1497
                last_cmd->command &= ~CmdSuspend;
1498
                wait_for_cmd_done(ioaddr + SCBCmd);
1499
                outw(CU_RESUME, ioaddr + SCBCmd);
1500
        }
1501
 
1502
        if (new_rx_mode == 0  &&  dev->mc_count < 4) {
1503
                /* The simple case of 0-3 multicast list entries occurs often, and
1504
                   fits within one tx_ring[] entry. */
1505
                struct dev_mc_list *mclist;
1506
                u16 *setup_params, *eaddrs;
1507
 
1508
                save_flags(flags);              /* Lock to protect sp->cur_tx. */
1509
                cli();
1510
                entry = sp->cur_tx++ % TX_RING_SIZE;
1511
                last_cmd = sp->last_cmd;
1512
                sp->last_cmd = (struct descriptor_net *)&sp->tx_ring[entry];
1513
                restore_flags(flags);
1514
 
1515
                sp->tx_skbuff[entry] = 0;
1516
                sp->tx_ring[entry].status = (CmdSuspend | CmdMulticastList) << 16;
1517
                sp->tx_ring[entry].link =
1518
                        virt_to_bus(&sp->tx_ring[(entry + 1) % TX_RING_SIZE]);
1519
                sp->tx_ring[entry].tx_desc_addr = 0; /* Really MC list count. */
1520
                setup_params = (u16 *)&sp->tx_ring[entry].tx_desc_addr;
1521
                *setup_params++ = dev->mc_count*6;
1522
                /* Fill in the multicast addresses. */
1523
                for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
1524
                         i++, mclist = mclist->next) {
1525
                        eaddrs = (u16 *)mclist->dmi_addr;
1526
                        *setup_params++ = *eaddrs++;
1527
                        *setup_params++ = *eaddrs++;
1528
                        *setup_params++ = *eaddrs++;
1529
                }
1530
 
1531
                last_cmd->command &= ~CmdSuspend;
1532
                /* Immediately trigger the command unit resume. */
1533
                wait_for_cmd_done(ioaddr + SCBCmd);
1534
                outw(CU_RESUME, ioaddr + SCBCmd);
1535
        } else if (new_rx_mode == 0) {
1536
                struct dev_mc_list *mclist;
1537
                u16 *setup_params, *eaddrs;
1538
                struct descriptor_net *mc_setup_frm = sp->mc_setup_frm;
1539
                int i;
1540
 
1541
                if (sp->mc_setup_frm_len < 10 + dev->mc_count*6
1542
                        || sp->mc_setup_frm == NULL) {
1543
                        /* Allocate a full setup frame, 10bytes + <max addrs>. */
1544
                        if (sp->mc_setup_frm)
1545
                                kfree(sp->mc_setup_frm);
1546
                        sp->mc_setup_busy = 0;
1547
                        sp->mc_setup_frm_len = 10 + multicast_filter_limit*6;
1548
                        sp->mc_setup_frm = kmalloc(sp->mc_setup_frm_len, GFP_ATOMIC);
1549
                        if (sp->mc_setup_frm == NULL) {
1550
                                printk(KERN_ERR "%s: Failed to allocate a setup frame.\n",
1551
                                           dev->name);
1552
                                sp->rx_mode = -1; /* We failed, try again. */
1553
                                return;
1554
                        }
1555
                }
1556
                /* If we are busy, someone might be quickly adding to the MC list.
1557
                   Try again later when the list changes stop. */
1558
                if (sp->mc_setup_busy) {
1559
                        sp->rx_mode = -1;
1560
                        return;
1561
                }
1562
                mc_setup_frm = sp->mc_setup_frm;
1563
                /* Fill the setup frame. */
1564
                if (speedo_debug > 1)
1565
                        printk(KERN_DEBUG "%s: Constructing a setup frame at %p, "
1566
                                   "%d bytes.\n",
1567
                                   dev->name, sp->mc_setup_frm, sp->mc_setup_frm_len);
1568
                mc_setup_frm->status = 0;
1569
                mc_setup_frm->command = CmdSuspend | CmdIntr | CmdMulticastList;
1570
                /* Link set below. */
1571
                setup_params = (u16 *)&mc_setup_frm->params;
1572
                *setup_params++ = dev->mc_count*6;
1573
                /* Fill in the multicast addresses. */
1574
                for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
1575
                         i++, mclist = mclist->next) {
1576
                        eaddrs = (u16 *)mclist->dmi_addr;
1577
                        *setup_params++ = *eaddrs++;
1578
                        *setup_params++ = *eaddrs++;
1579
                        *setup_params++ = *eaddrs++;
1580
                }
1581
 
1582
                /* Disable interrupts while playing with the Tx Cmd list. */
1583
                save_flags(flags);
1584
                cli();
1585
                entry = sp->cur_tx++ % TX_RING_SIZE;
1586
                last_cmd = sp->last_cmd;
1587
                sp->last_cmd = mc_setup_frm;
1588
                sp->mc_setup_busy++;
1589
                restore_flags(flags);
1590
 
1591
                /* Change the command to a NoOp, pointing to the CmdMulti command. */
1592
                sp->tx_skbuff[entry] = 0;
1593
                sp->tx_ring[entry].status = CmdNOp << 16;
1594
                sp->tx_ring[entry].link = virt_to_bus(mc_setup_frm);
1595
 
1596
                /* Set the link in the setup frame. */
1597
                mc_setup_frm->link =
1598
                        virt_to_bus(&(sp->tx_ring[(entry+1) % TX_RING_SIZE]));
1599
 
1600
                last_cmd->command &= ~CmdSuspend;
1601
                /* Immediately trigger the command unit resume. */
1602
                wait_for_cmd_done(ioaddr + SCBCmd);
1603
                outw(CU_RESUME, ioaddr + SCBCmd);
1604
                if (speedo_debug > 5)
1605
                        printk(" CmdMCSetup frame length %d in entry %d.\n",
1606
                                   dev->mc_count, entry);
1607
        }
1608
 
1609
        sp->rx_mode = new_rx_mode;
1610
}
1611
 
1612
#ifdef MODULE
1613
 
1614
int
1615
init_module(void)
1616
{
1617
        int cards_found;
1618
 
1619
        if (debug >= 0)
1620
                speedo_debug = debug;
1621
        if (speedo_debug)
1622
                printk(KERN_INFO "%s", version);
1623
 
1624
        root_speedo_dev = NULL;
1625
        cards_found = eepro100_init(NULL);
1626
        return cards_found ? 0 : -ENODEV;
1627
}
1628
 
1629
void
1630
cleanup_module(void)
1631
{
1632
        struct device *next_dev;
1633
 
1634
        /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1635
        while (root_speedo_dev) {
1636
                next_dev = ((struct speedo_private *)root_speedo_dev->priv)->next_module;
1637
                unregister_netdev(root_speedo_dev);
1638
                release_region(root_speedo_dev->base_addr, SPEEDO3_TOTAL_SIZE);
1639
                kfree(root_speedo_dev);
1640
                root_speedo_dev = next_dev;
1641
        }
1642
}
1643
#else   /* not MODULE */
1644
int eepro100_probe(struct device *dev)
1645
{
1646
        int cards_found = 0;
1647
 
1648
        cards_found = eepro100_init(dev);
1649
 
1650
        if (speedo_debug > 0  &&  cards_found)
1651
                printk(version);
1652
 
1653
        return cards_found ? 0 : -ENODEV;
1654
}
1655
#endif  /* MODULE */
1656
 
1657
/*
1658
 * Local variables:
1659
 *  compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c eepro100.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
1660
 *  SMP-compile-command: "gcc -D__SMP__ -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c eepro100.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
1661
 *  c-indent-level: 4
1662
 *  c-basic-offset: 4
1663
 *  tab-width: 4
1664
 * End:
1665
 */