ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / 3c515.c
1 /*
2         Written 1997-1998 by Donald Becker.
3
4         This software may be used and distributed according to the terms
5         of the GNU General Public License, incorporated herein by reference.
6
7         This driver is for the 3Com ISA EtherLink XL "Corkscrew" 3c515 ethercard.
8
9         The author may be reached as becker@scyld.com, or C/O
10         Scyld Computing Corporation
11         410 Severn Ave., Suite 210
12         Annapolis MD 21403
13
14
15         2000/2/2- Added support for kernel-level ISAPnP 
16                 by Stephen Frost <sfrost@snowman.net> and Alessandro Zummo
17         Cleaned up for 2.3.x/softnet by Jeff Garzik and Alan Cox.
18         
19         2001/11/17 - Added ethtool support (jgarzik)
20         
21         2002/10/28 - Locking updates for 2.5 (alan@redhat.com)
22
23 */
24
25 #define DRV_NAME                "3c515"
26 #define DRV_VERSION             "0.99t-ac"
27 #define DRV_RELDATE             "28-Oct-2002"
28
29 static char *version =
30 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " becker@scyld.com and others\n";
31
32 #define CORKSCREW 1
33
34 /* "Knobs" that adjust features and parameters. */
35 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
36    Setting to > 1512 effectively disables this feature. */
37 static const int rx_copybreak = 200;
38
39 /* Allow setting MTU to a larger size, bypassing the normal ethernet setup. */
40 static const int mtu = 1500;
41
42 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
43 static int max_interrupt_work = 20;
44
45 /* Enable the automatic media selection code -- usually set. */
46 #define AUTOMEDIA 1
47
48 /* Allow the use of fragment bus master transfers instead of only
49    programmed-I/O for Vortex cards.  Full-bus-master transfers are always
50    enabled by default on Boomerang cards.  If VORTEX_BUS_MASTER is defined,
51    the feature may be turned on using 'options'. */
52 #define VORTEX_BUS_MASTER
53
54 /* A few values that may be tweaked. */
55 /* Keep the ring sizes a power of two for efficiency. */
56 #define TX_RING_SIZE    16
57 #define RX_RING_SIZE    16
58 #define PKT_BUF_SZ              1536    /* Size of each temporary Rx buffer. */
59
60 #include <linux/config.h>
61 #include <linux/module.h>
62 #include <linux/isapnp.h>
63 #include <linux/kernel.h>
64 #include <linux/netdevice.h>
65 #include <linux/string.h>
66 #include <linux/errno.h>
67 #include <linux/in.h>
68 #include <linux/ioport.h>
69 #include <linux/slab.h>
70 #include <linux/skbuff.h>
71 #include <linux/etherdevice.h>
72 #include <linux/interrupt.h>
73 #include <linux/timer.h>
74 #include <linux/ethtool.h>
75
76 #include <asm/uaccess.h>
77 #include <asm/bitops.h>
78 #include <asm/io.h>
79 #include <asm/dma.h>
80
81 #define NEW_MULTICAST
82 #include <linux/delay.h>
83
84 #define MAX_UNITS 8
85
86 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
87 MODULE_DESCRIPTION("3Com 3c515 Corkscrew driver");
88 MODULE_LICENSE("GPL");
89
90 MODULE_PARM(debug, "i");
91 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
92 MODULE_PARM(rx_copybreak, "i");
93 MODULE_PARM(max_interrupt_work, "i");
94 MODULE_PARM_DESC(debug, "3c515 debug level (0-6)");
95 MODULE_PARM_DESC(options, "3c515: Bits 0-2: media type, bit 3: full duplex, bit 4: bus mastering");
96 MODULE_PARM_DESC(rx_copybreak, "3c515 copy breakpoint for copy-only-tiny-frames");
97 MODULE_PARM_DESC(max_interrupt_work, "3c515 maximum events handled per interrupt");
98
99 /* "Knobs" for adjusting internal parameters. */
100 /* Put out somewhat more debugging messages. (0 - no msg, 1 minimal msgs). */
101 #define DRIVER_DEBUG 1
102 /* Some values here only for performance evaluation and path-coverage
103    debugging. */
104 static int rx_nocopy, rx_copy, queued_packet;
105
106 /* Number of times to check to see if the Tx FIFO has space, used in some
107    limited cases. */
108 #define WAIT_TX_AVAIL 200
109
110 /* Operational parameter that usually are not changed. */
111 #define TX_TIMEOUT  40          /* Time in jiffies before concluding Tx hung */
112
113 /* The size here is somewhat misleading: the Corkscrew also uses the ISA
114    aliased registers at <base>+0x400.
115    */
116 #define CORKSCREW_TOTAL_SIZE 0x20
117
118 #ifdef DRIVER_DEBUG
119 static int corkscrew_debug = DRIVER_DEBUG;
120 #else
121 static int corkscrew_debug = 1;
122 #endif
123
124 #define CORKSCREW_ID 10
125
126 /*
127                                 Theory of Operation
128
129 I. Board Compatibility
130
131 This device driver is designed for the 3Com 3c515 ISA Fast EtherLink XL,
132 3Com's ISA bus adapter for Fast Ethernet.  Due to the unique I/O port layout,
133 it's not practical to integrate this driver with the other EtherLink drivers.
134
135 II. Board-specific settings
136
137 The Corkscrew has an EEPROM for configuration, but no special settings are
138 needed for Linux.
139
140 III. Driver operation
141
142 The 3c515 series use an interface that's very similar to the 3c900 "Boomerang"
143 PCI cards, with the bus master interface extensively modified to work with
144 the ISA bus.
145
146 The card is capable of full-bus-master transfers with separate
147 lists of transmit and receive descriptors, similar to the AMD LANCE/PCnet,
148 DEC Tulip and Intel Speedo3.
149
150 This driver uses a "RX_COPYBREAK" scheme rather than a fixed intermediate
151 receive buffer.  This scheme allocates full-sized skbuffs as receive
152 buffers.  The value RX_COPYBREAK is used as the copying breakpoint: it is
153 chosen to trade-off the memory wasted by passing the full-sized skbuff to
154 the queue layer for all frames vs. the copying cost of copying a frame to a
155 correctly-sized skbuff.
156
157
158 IIIC. Synchronization
159 The driver runs as two independent, single-threaded flows of control.  One
160 is the send-packet routine, which enforces single-threaded use by the netif
161 layer.  The other thread is the interrupt handler, which is single
162 threaded by the hardware and other software.
163
164 IV. Notes
165
166 Thanks to Terry Murphy of 3Com for providing documentation and a development
167 board.
168
169 The names "Vortex", "Boomerang" and "Corkscrew" are the internal 3Com
170 project names.  I use these names to eliminate confusion -- 3Com product
171 numbers and names are very similar and often confused.
172
173 The new chips support both ethernet (1.5K) and FDDI (4.5K) frame sizes!
174 This driver only supports ethernet frames because of the recent MTU limit
175 of 1.5K, but the changes to support 4.5K are minimal.
176 */
177
178 /* Operational definitions.
179    These are not used by other compilation units and thus are not
180    exported in a ".h" file.
181
182    First the windows.  There are eight register windows, with the command
183    and status registers available in each.
184    */
185 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
186 #define EL3_CMD 0x0e
187 #define EL3_STATUS 0x0e
188
189 /* The top five bits written to EL3_CMD are a command, the lower
190    11 bits are the parameter, if applicable.
191    Note that 11 parameters bits was fine for ethernet, but the new chips
192    can handle FDDI length frames (~4500 octets) and now parameters count
193    32-bit 'Dwords' rather than octets. */
194
195 enum corkscrew_cmd {
196         TotalReset = 0 << 11, SelectWindow = 1 << 11, StartCoax = 2 << 11,
197         RxDisable = 3 << 11, RxEnable = 4 << 11, RxReset = 5 << 11,
198         UpStall = 6 << 11, UpUnstall = (6 << 11) + 1, DownStall = (6 << 11) + 2,
199         DownUnstall = (6 << 11) + 3, RxDiscard = 8 << 11, TxEnable = 9 << 11, 
200         TxDisable = 10 << 11, TxReset = 11 << 11, FakeIntr = 12 << 11, 
201         AckIntr = 13 << 11, SetIntrEnb = 14 << 11, SetStatusEnb = 15 << 11, 
202         SetRxFilter = 16 << 11, SetRxThreshold = 17 << 11,
203         SetTxThreshold = 18 << 11, SetTxStart = 19 << 11, StartDMAUp = 20 << 11,
204         StartDMADown = (20 << 11) + 1, StatsEnable = 21 << 11,
205         StatsDisable = 22 << 11, StopCoax = 23 << 11,
206 };
207
208 /* The SetRxFilter command accepts the following classes: */
209 enum RxFilter {
210         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
211 };
212
213 /* Bits in the general status register. */
214 enum corkscrew_status {
215         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
216         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
217         IntReq = 0x0040, StatsFull = 0x0080,
218         DMADone = 1 << 8, DownComplete = 1 << 9, UpComplete = 1 << 10,
219         DMAInProgress = 1 << 11,        /* DMA controller is still busy. */
220         CmdInProgress = 1 << 12,        /* EL3_CMD is still busy. */
221 };
222
223 /* Register window 1 offsets, the window used in normal operation.
224    On the Corkscrew this window is always mapped at offsets 0x10-0x1f. */
225 enum Window1 {
226         TX_FIFO = 0x10, RX_FIFO = 0x10, RxErrors = 0x14,
227         RxStatus = 0x18, Timer = 0x1A, TxStatus = 0x1B,
228         TxFree = 0x1C,          /* Remaining free bytes in Tx buffer. */
229 };
230 enum Window0 {
231         Wn0IRQ = 0x08,
232 #if defined(CORKSCREW)
233         Wn0EepromCmd = 0x200A,  /* Corkscrew EEPROM command register. */
234         Wn0EepromData = 0x200C, /* Corkscrew EEPROM results register. */
235 #else
236         Wn0EepromCmd = 10,      /* Window 0: EEPROM command register. */
237         Wn0EepromData = 12,     /* Window 0: EEPROM results register. */
238 #endif
239 };
240 enum Win0_EEPROM_bits {
241         EEPROM_Read = 0x80, EEPROM_WRITE = 0x40, EEPROM_ERASE = 0xC0,
242         EEPROM_EWENB = 0x30,    /* Enable erasing/writing for 10 msec. */
243         EEPROM_EWDIS = 0x00,    /* Disable EWENB before 10 msec timeout. */
244 };
245
246 /* EEPROM locations. */
247 enum eeprom_offset {
248         PhysAddr01 = 0, PhysAddr23 = 1, PhysAddr45 = 2, ModelID = 3,
249         EtherLink3ID = 7,
250 };
251
252 enum Window3 {                  /* Window 3: MAC/config bits. */
253         Wn3_Config = 0, Wn3_MAC_Ctrl = 6, Wn3_Options = 8,
254 };
255 union wn3_config {
256         int i;
257         struct w3_config_fields {
258                 unsigned int ram_size:3, ram_width:1, ram_speed:2, rom_size:2;
259                 int pad8:8;
260                 unsigned int ram_split:2, pad18:2, xcvr:3, pad21:1, autoselect:1;
261                 int pad24:7;
262         } u;
263 };
264
265 enum Window4 {
266         Wn4_NetDiag = 6, Wn4_Media = 10,        /* Window 4: Xcvr/media bits. */
267 };
268 enum Win4_Media_bits {
269         Media_SQE = 0x0008,     /* Enable SQE error counting for AUI. */
270         Media_10TP = 0x00C0,    /* Enable link beat and jabber for 10baseT. */
271         Media_Lnk = 0x0080,     /* Enable just link beat for 100TX/100FX. */
272         Media_LnkBeat = 0x0800,
273 };
274 enum Window7 {                  /* Window 7: Bus Master control. */
275         Wn7_MasterAddr = 0, Wn7_MasterLen = 6, Wn7_MasterStatus = 12,
276 };
277
278 /* Boomerang-style bus master control registers.  Note ISA aliases! */
279 enum MasterCtrl {
280         PktStatus = 0x400, DownListPtr = 0x404, FragAddr = 0x408, FragLen =
281             0x40c,
282         TxFreeThreshold = 0x40f, UpPktStatus = 0x410, UpListPtr = 0x418,
283 };
284
285 /* The Rx and Tx descriptor lists.
286    Caution Alpha hackers: these types are 32 bits!  Note also the 8 byte
287    alignment contraint on tx_ring[] and rx_ring[]. */
288 struct boom_rx_desc {
289         u32 next;
290         s32 status;
291         u32 addr;
292         s32 length;
293 };
294
295 /* Values for the Rx status entry. */
296 enum rx_desc_status {
297         RxDComplete = 0x00008000, RxDError = 0x4000,
298         /* See boomerang_rx() for actual error bits */
299 };
300
301 struct boom_tx_desc {
302         u32 next;
303         s32 status;
304         u32 addr;
305         s32 length;
306 };
307
308 struct corkscrew_private {
309         const char *product_name;
310         struct list_head list;
311         struct net_device *our_dev;
312         /* The Rx and Tx rings are here to keep them quad-word-aligned. */
313         struct boom_rx_desc rx_ring[RX_RING_SIZE];
314         struct boom_tx_desc tx_ring[TX_RING_SIZE];
315         /* The addresses of transmit- and receive-in-place skbuffs. */
316         struct sk_buff *rx_skbuff[RX_RING_SIZE];
317         struct sk_buff *tx_skbuff[TX_RING_SIZE];
318         unsigned int cur_rx, cur_tx;    /* The next free ring entry */
319         unsigned int dirty_rx, dirty_tx;/* The ring entries to be free()ed. */
320         struct net_device_stats stats;
321         struct sk_buff *tx_skb; /* Packet being eaten by bus master ctrl.  */
322         struct timer_list timer;        /* Media selection timer. */
323         int capabilities        ;       /* Adapter capabilities word. */
324         int options;                    /* User-settable misc. driver options. */
325         int last_rx_packets;            /* For media autoselection. */
326         unsigned int available_media:8, /* From Wn3_Options */
327                 media_override:3,       /* Passed-in media type. */
328                 default_media:3,        /* Read from the EEPROM. */
329                 full_duplex:1, autoselect:1, bus_master:1,      /* Vortex can only do a fragment bus-m. */
330                 full_bus_master_tx:1, full_bus_master_rx:1,     /* Boomerang  */
331                 tx_full:1;
332         spinlock_t lock;
333         struct device *dev;
334 };
335
336 /* The action to take with a media selection timer tick.
337    Note that we deviate from the 3Com order by checking 10base2 before AUI.
338  */
339 enum xcvr_types {
340         XCVR_10baseT = 0, XCVR_AUI, XCVR_10baseTOnly, XCVR_10base2, XCVR_100baseTx,
341         XCVR_100baseFx, XCVR_MII = 6, XCVR_Default = 8,
342 };
343
344 static struct media_table {
345         char *name;
346         unsigned int media_bits:16,     /* Bits to set in Wn4_Media register. */
347                 mask:8,                 /* The transceiver-present bit in Wn3_Config. */
348                 next:8;                 /* The media type to try next. */
349         short wait;                     /* Time before we check media status. */
350 } media_tbl[] = {       
351         { "10baseT", Media_10TP, 0x08, XCVR_10base2, (14 * HZ) / 10 }, 
352         { "10Mbs AUI", Media_SQE, 0x20, XCVR_Default, (1 * HZ) / 10}, 
353         { "undefined", 0, 0x80, XCVR_10baseT, 10000}, 
354         { "10base2", 0, 0x10, XCVR_AUI, (1 * HZ) / 10}, 
355         { "100baseTX", Media_Lnk, 0x02, XCVR_100baseFx, (14 * HZ) / 10}, 
356         { "100baseFX", Media_Lnk, 0x04, XCVR_MII, (14 * HZ) / 10}, 
357         { "MII", 0, 0x40, XCVR_10baseT, 3 * HZ}, 
358         { "undefined", 0, 0x01, XCVR_10baseT, 10000}, 
359         { "Default", 0, 0xFF, XCVR_10baseT, 10000},
360 };
361
362 #ifdef __ISAPNP__
363 static struct isapnp_device_id corkscrew_isapnp_adapters[] = {
364         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
365                 ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5051),
366                 (long) "3Com Fast EtherLink ISA" },
367         { }     /* terminate list */
368 };
369
370 MODULE_DEVICE_TABLE(isapnp, corkscrew_isapnp_adapters);
371
372 static int nopnp;
373 #endif /* __ISAPNP__ */
374
375 static struct net_device *corkscrew_scan(int unit);
376 static void corkscrew_setup(struct net_device *dev, int ioaddr,
377                             struct pnp_dev *idev, int card_number);
378 static int corkscrew_open(struct net_device *dev);
379 static void corkscrew_timer(unsigned long arg);
380 static int corkscrew_start_xmit(struct sk_buff *skb,
381                                 struct net_device *dev);
382 static int corkscrew_rx(struct net_device *dev);
383 static void corkscrew_timeout(struct net_device *dev);
384 static int boomerang_rx(struct net_device *dev);
385 static irqreturn_t corkscrew_interrupt(int irq, void *dev_id,
386                                     struct pt_regs *regs);
387 static int corkscrew_close(struct net_device *dev);
388 static void update_stats(int addr, struct net_device *dev);
389 static struct net_device_stats *corkscrew_get_stats(struct net_device *dev);
390 static void set_rx_mode(struct net_device *dev);
391 static struct ethtool_ops netdev_ethtool_ops;
392 \f
393
394 /* 
395    Unfortunately maximizing the shared code between the integrated and
396    module version of the driver results in a complicated set of initialization
397    procedures.
398    init_module() -- modules /  tc59x_init()  -- built-in
399                 The wrappers for corkscrew_scan()
400    corkscrew_scan()              The common routine that scans for PCI and EISA cards
401    corkscrew_found_device() Allocate a device structure when we find a card.
402                                         Different versions exist for modules and built-in.
403    corkscrew_probe1()           Fill in the device structure -- this is separated
404                                         so that the modules code can put it in dev->init.
405 */
406 /* This driver uses 'options' to pass the media type, full-duplex flag, etc. */
407 /* Note: this is the only limit on the number of cards supported!! */
408 static int options[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1, };
409
410 #ifdef MODULE
411 static int debug = -1;
412 /* A list of all installed Vortex devices, for removing the driver module. */
413 /* we will need locking (and refcounting) if we ever use it for more */
414 static LIST_HEAD(root_corkscrew_dev);
415
416 int init_module(void)
417 {
418         int found = 0;
419         if (debug >= 0)
420                 corkscrew_debug = debug;
421         if (corkscrew_debug)
422                 printk(version);
423         while (corkscrew_scan(-1))
424                 found++;
425         return found ? 0 : -ENODEV;
426 }
427
428 #else
429 struct net_device *tc515_probe(int unit)
430 {
431         struct net_device *dev = corkscrew_scan(unit);
432         static int printed;
433
434         if (!dev)
435                 return ERR_PTR(-ENODEV);
436
437         if (corkscrew_debug > 0 && !printed) {
438                 printed = 1;
439                 printk(version);
440         }
441
442         return dev;
443 }
444 #endif                          /* not MODULE */
445
446 static int check_device(unsigned ioaddr)
447 {
448         int timer;
449
450         if (!request_region(ioaddr, CORKSCREW_TOTAL_SIZE, "3c515"))
451                 return 0;
452         /* Check the resource configuration for a matching ioaddr. */
453         if ((inw(ioaddr + 0x2002) & 0x1f0) != (ioaddr & 0x1f0)) {
454                 release_region(ioaddr, CORKSCREW_TOTAL_SIZE);
455                 return 0;
456         }
457         /* Verify by reading the device ID from the EEPROM. */
458         outw(EEPROM_Read + 7, ioaddr + Wn0EepromCmd);
459         /* Pause for at least 162 us. for the read to take place. */
460         for (timer = 4; timer >= 0; timer--) {
461                 udelay(162);
462                 if ((inw(ioaddr + Wn0EepromCmd) & 0x0200) == 0)
463                         break;
464         }
465         if (inw(ioaddr + Wn0EepromData) != 0x6d50) {
466                 release_region(ioaddr, CORKSCREW_TOTAL_SIZE);
467                 return 0;
468         }
469         return 1;
470 }
471
472 static void cleanup_card(struct net_device *dev)
473 {
474         struct corkscrew_private *vp = (struct corkscrew_private *) dev->priv;
475         list_del_init(&vp->list);
476         if (dev->dma)
477                 free_dma(dev->dma);
478         outw(TotalReset, dev->base_addr + EL3_CMD);
479         release_region(dev->base_addr, CORKSCREW_TOTAL_SIZE);
480         if (vp->dev)
481                 pnp_device_detach(to_pnp_dev(vp->dev));
482 }
483
484 static struct net_device *corkscrew_scan(int unit)
485 {
486         struct net_device *dev;
487         static int cards_found = 0;
488         static int ioaddr;
489         int err;
490 #ifdef __ISAPNP__
491         short i;
492         static int pnp_cards;
493 #endif
494
495         dev = alloc_etherdev(sizeof(struct corkscrew_private));
496         if (!dev)
497                 return ERR_PTR(-ENOMEM);
498
499         if (unit >= 0) {
500                 sprintf(dev->name, "eth%d", unit);
501                 netdev_boot_setup_check(dev);
502         }
503
504         SET_MODULE_OWNER(dev);
505
506 #ifdef __ISAPNP__
507         if(nopnp == 1)
508                 goto no_pnp;
509         for(i=0; corkscrew_isapnp_adapters[i].vendor != 0; i++) {
510                 struct pnp_dev *idev = NULL;
511                 int irq;
512                 while((idev = pnp_find_dev(NULL,
513                                            corkscrew_isapnp_adapters[i].vendor,
514                                            corkscrew_isapnp_adapters[i].function,
515                                            idev))) {
516
517                         if (pnp_device_attach(idev) < 0)
518                                 continue;
519                         if (pnp_activate_dev(idev) < 0) {
520                                 printk("pnp activate failed (out of resources?)\n");
521                                 pnp_device_detach(idev);
522                                 continue;
523                         }
524                         if (!pnp_port_valid(idev, 0) || !pnp_irq_valid(idev, 0)) {
525                                 pnp_device_detach(idev);
526                                 continue;
527                         }
528                         ioaddr = pnp_port_start(idev, 0);
529                         irq = pnp_irq(idev, 0);
530                         if (!check_device(ioaddr)) {
531                                 pnp_device_detach(idev);
532                                 continue;
533                         }
534                         if(corkscrew_debug)
535                                 printk ("ISAPNP reports %s at i/o 0x%x, irq %d\n",
536                                         (char*) corkscrew_isapnp_adapters[i].driver_data, ioaddr, irq);
537                         printk(KERN_INFO "3c515 Resource configuration register %#4.4x, DCR %4.4x.\n",
538                                 inl(ioaddr + 0x2002), inw(ioaddr + 0x2000));
539                         /* irq = inw(ioaddr + 0x2002) & 15; */ /* Use the irq from isapnp */
540                         corkscrew_setup(dev, ioaddr, idev, cards_found++);
541                         SET_NETDEV_DEV(dev, &idev->dev);
542                         pnp_cards++;
543                         err = register_netdev(dev);
544                         if (!err)
545                                 return dev;
546                         cleanup_card(dev);
547                 }
548         }
549 no_pnp:
550 #endif /* __ISAPNP__ */
551
552         /* Check all locations on the ISA bus -- evil! */
553         for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x20) {
554                 if (!check_device(ioaddr))
555                         continue;
556
557                 printk(KERN_INFO "3c515 Resource configuration register %#4.4x, DCR %4.4x.\n",
558                      inl(ioaddr + 0x2002), inw(ioaddr + 0x2000));
559                 corkscrew_setup(dev, ioaddr, NULL, cards_found++);
560                 err = register_netdev(dev);
561                 if (!err)
562                         return dev;
563                 cleanup_card(dev);
564         }
565         free_netdev(dev);
566         return NULL;
567 }
568
569 static void corkscrew_setup(struct net_device *dev, int ioaddr,
570                             struct pnp_dev *idev, int card_number)
571 {
572         struct corkscrew_private *vp = (struct corkscrew_private *) dev->priv;
573         unsigned int eeprom[0x40], checksum = 0;        /* EEPROM contents */
574         int i;
575         int irq;
576
577         if (idev) {
578                 irq = pnp_irq(idev, 0);
579                 vp->dev = &idev->dev;
580         } else {
581                 irq = inw(ioaddr + 0x2002) & 15;
582         }
583
584         dev->base_addr = ioaddr;
585         dev->irq = irq;
586         dev->dma = inw(ioaddr + 0x2000) & 7;
587         vp->product_name = "3c515";
588         vp->options = dev->mem_start;
589         vp->our_dev = dev;
590
591         if (!vp->options) {
592                  if (card_number >= MAX_UNITS)
593                         vp->options = -1;
594                 else
595                         vp->options = options[card_number];
596         }
597
598         if (vp->options >= 0) {
599                 vp->media_override = vp->options & 7;
600                 if (vp->media_override == 2)
601                         vp->media_override = 0;
602                 vp->full_duplex = (vp->options & 8) ? 1 : 0;
603                 vp->bus_master = (vp->options & 16) ? 1 : 0;
604         } else {
605                 vp->media_override = 7;
606                 vp->full_duplex = 0;
607                 vp->bus_master = 0;
608         }
609 #ifdef MODULE
610         list_add(&vp->list, &root_corkscrew_dev);
611 #endif
612
613         printk(KERN_INFO "%s: 3Com %s at %#3x,", dev->name, vp->product_name, ioaddr);
614
615         spin_lock_init(&vp->lock);
616         
617         /* Read the station address from the EEPROM. */
618         EL3WINDOW(0);
619         for (i = 0; i < 0x18; i++) {
620                 short *phys_addr = (short *) dev->dev_addr;
621                 int timer;
622                 outw(EEPROM_Read + i, ioaddr + Wn0EepromCmd);
623                 /* Pause for at least 162 us. for the read to take place. */
624                 for (timer = 4; timer >= 0; timer--) {
625                         udelay(162);
626                         if ((inw(ioaddr + Wn0EepromCmd) & 0x0200) == 0)
627                                 break;
628                 }
629                 eeprom[i] = inw(ioaddr + Wn0EepromData);
630                 checksum ^= eeprom[i];
631                 if (i < 3)
632                         phys_addr[i] = htons(eeprom[i]);
633         }
634         checksum = (checksum ^ (checksum >> 8)) & 0xff;
635         if (checksum != 0x00)
636                 printk(" ***INVALID CHECKSUM %4.4x*** ", checksum);
637         for (i = 0; i < 6; i++)
638                 printk("%c%2.2x", i ? ':' : ' ', dev->dev_addr[i]);
639         if (eeprom[16] == 0x11c7) {     /* Corkscrew */
640                 if (request_dma(dev->dma, "3c515")) {
641                         printk(", DMA %d allocation failed", dev->dma);
642                         dev->dma = 0;
643                 } else
644                         printk(", DMA %d", dev->dma);
645         }
646         printk(", IRQ %d\n", dev->irq);
647         /* Tell them about an invalid IRQ. */
648         if (corkscrew_debug && (dev->irq <= 0 || dev->irq > 15))
649                 printk(KERN_WARNING " *** Warning: this IRQ is unlikely to work! ***\n");
650
651         {
652                 char *ram_split[] = { "5:3", "3:1", "1:1", "3:5" };
653                 union wn3_config config;
654                 EL3WINDOW(3);
655                 vp->available_media = inw(ioaddr + Wn3_Options);
656                 config.i = inl(ioaddr + Wn3_Config);
657                 if (corkscrew_debug > 1)
658                         printk(KERN_INFO "  Internal config register is %4.4x, transceivers %#x.\n",
659                                 config.i, inw(ioaddr + Wn3_Options));
660                 printk(KERN_INFO "  %dK %s-wide RAM %s Rx:Tx split, %s%s interface.\n",
661                         8 << config.u.ram_size,
662                         config.u.ram_width ? "word" : "byte",
663                         ram_split[config.u.ram_split],
664                         config.u.autoselect ? "autoselect/" : "",
665                         media_tbl[config.u.xcvr].name);
666                 dev->if_port = config.u.xcvr;
667                 vp->default_media = config.u.xcvr;
668                 vp->autoselect = config.u.autoselect;
669         }
670         if (vp->media_override != 7) {
671                 printk(KERN_INFO "  Media override to transceiver type %d (%s).\n",
672                        vp->media_override,
673                        media_tbl[vp->media_override].name);
674                 dev->if_port = vp->media_override;
675         }
676
677         vp->capabilities = eeprom[16];
678         vp->full_bus_master_tx = (vp->capabilities & 0x20) ? 1 : 0;
679         /* Rx is broken at 10mbps, so we always disable it. */
680         /* vp->full_bus_master_rx = 0; */
681         vp->full_bus_master_rx = (vp->capabilities & 0x20) ? 1 : 0;
682
683         /* The 3c51x-specific entries in the device structure. */
684         dev->open = &corkscrew_open;
685         dev->hard_start_xmit = &corkscrew_start_xmit;
686         dev->tx_timeout = &corkscrew_timeout;
687         dev->watchdog_timeo = (400 * HZ) / 1000;
688         dev->stop = &corkscrew_close;
689         dev->get_stats = &corkscrew_get_stats;
690         dev->set_multicast_list = &set_rx_mode;
691         dev->ethtool_ops = &netdev_ethtool_ops;
692 }
693 \f
694
695 static int corkscrew_open(struct net_device *dev)
696 {
697         int ioaddr = dev->base_addr;
698         struct corkscrew_private *vp =
699             (struct corkscrew_private *) dev->priv;
700         union wn3_config config;
701         int i;
702
703         /* Before initializing select the active media port. */
704         EL3WINDOW(3);
705         if (vp->full_duplex)
706                 outb(0x20, ioaddr + Wn3_MAC_Ctrl);      /* Set the full-duplex bit. */
707         config.i = inl(ioaddr + Wn3_Config);
708
709         if (vp->media_override != 7) {
710                 if (corkscrew_debug > 1)
711                         printk(KERN_INFO "%s: Media override to transceiver %d (%s).\n",
712                                 dev->name, vp->media_override,
713                                 media_tbl[vp->media_override].name);
714                 dev->if_port = vp->media_override;
715         } else if (vp->autoselect) {
716                 /* Find first available media type, starting with 100baseTx. */
717                 dev->if_port = 4;
718                 while (!(vp->available_media & media_tbl[dev->if_port].mask)) 
719                         dev->if_port = media_tbl[dev->if_port].next;
720
721                 if (corkscrew_debug > 1)
722                         printk("%s: Initial media type %s.\n",
723                                dev->name, media_tbl[dev->if_port].name);
724
725                 init_timer(&vp->timer);
726                 vp->timer.expires = jiffies + media_tbl[dev->if_port].wait;
727                 vp->timer.data = (unsigned long) dev;
728                 vp->timer.function = &corkscrew_timer;  /* timer handler */
729                 add_timer(&vp->timer);
730         } else
731                 dev->if_port = vp->default_media;
732
733         config.u.xcvr = dev->if_port;
734         outl(config.i, ioaddr + Wn3_Config);
735
736         if (corkscrew_debug > 1) {
737                 printk("%s: corkscrew_open() InternalConfig %8.8x.\n",
738                        dev->name, config.i);
739         }
740
741         outw(TxReset, ioaddr + EL3_CMD);
742         for (i = 20; i >= 0; i--)
743                 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
744                         break;
745
746         outw(RxReset, ioaddr + EL3_CMD);
747         /* Wait a few ticks for the RxReset command to complete. */
748         for (i = 20; i >= 0; i--)
749                 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
750                         break;
751
752         outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
753
754         /* Use the now-standard shared IRQ implementation. */
755         if (vp->capabilities == 0x11c7) {
756                 /* Corkscrew: Cannot share ISA resources. */
757                 if (dev->irq == 0
758                     || dev->dma == 0
759                     || request_irq(dev->irq, &corkscrew_interrupt, 0,
760                                    vp->product_name, dev)) return -EAGAIN;
761                 enable_dma(dev->dma);
762                 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
763         } else if (request_irq(dev->irq, &corkscrew_interrupt, SA_SHIRQ,
764                                vp->product_name, dev)) {
765                 return -EAGAIN;
766         }
767
768         if (corkscrew_debug > 1) {
769                 EL3WINDOW(4);
770                 printk("%s: corkscrew_open() irq %d media status %4.4x.\n",
771                        dev->name, dev->irq, inw(ioaddr + Wn4_Media));
772         }
773
774         /* Set the station address and mask in window 2 each time opened. */
775         EL3WINDOW(2);
776         for (i = 0; i < 6; i++)
777                 outb(dev->dev_addr[i], ioaddr + i);
778         for (; i < 12; i += 2)
779                 outw(0, ioaddr + i);
780
781         if (dev->if_port == 3)
782                 /* Start the thinnet transceiver. We should really wait 50ms... */
783                 outw(StartCoax, ioaddr + EL3_CMD);
784         EL3WINDOW(4);
785         outw((inw(ioaddr + Wn4_Media) & ~(Media_10TP | Media_SQE)) |
786              media_tbl[dev->if_port].media_bits, ioaddr + Wn4_Media);
787
788         /* Switch to the stats window, and clear all stats by reading. */
789         outw(StatsDisable, ioaddr + EL3_CMD);
790         EL3WINDOW(6);
791         for (i = 0; i < 10; i++)
792                 inb(ioaddr + i);
793         inw(ioaddr + 10);
794         inw(ioaddr + 12);
795         /* New: On the Vortex we must also clear the BadSSD counter. */
796         EL3WINDOW(4);
797         inb(ioaddr + 12);
798         /* ..and on the Boomerang we enable the extra statistics bits. */
799         outw(0x0040, ioaddr + Wn4_NetDiag);
800
801         /* Switch to register set 7 for normal use. */
802         EL3WINDOW(7);
803
804         if (vp->full_bus_master_rx) {   /* Boomerang bus master. */
805                 vp->cur_rx = vp->dirty_rx = 0;
806                 if (corkscrew_debug > 2)
807                         printk("%s:  Filling in the Rx ring.\n",
808                                dev->name);
809                 for (i = 0; i < RX_RING_SIZE; i++) {
810                         struct sk_buff *skb;
811                         if (i < (RX_RING_SIZE - 1))
812                                 vp->rx_ring[i].next =
813                                     isa_virt_to_bus(&vp->rx_ring[i + 1]);
814                         else
815                                 vp->rx_ring[i].next = 0;
816                         vp->rx_ring[i].status = 0;      /* Clear complete bit. */
817                         vp->rx_ring[i].length = PKT_BUF_SZ | 0x80000000;
818                         skb = dev_alloc_skb(PKT_BUF_SZ);
819                         vp->rx_skbuff[i] = skb;
820                         if (skb == NULL)
821                                 break;  /* Bad news!  */
822                         skb->dev = dev; /* Mark as being used by this device. */
823                         skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
824                         vp->rx_ring[i].addr = isa_virt_to_bus(skb->tail);
825                 }
826                 vp->rx_ring[i - 1].next = isa_virt_to_bus(&vp->rx_ring[0]);     /* Wrap the ring. */
827                 outl(isa_virt_to_bus(&vp->rx_ring[0]), ioaddr + UpListPtr);
828         }
829         if (vp->full_bus_master_tx) {   /* Boomerang bus master Tx. */
830                 vp->cur_tx = vp->dirty_tx = 0;
831                 outb(PKT_BUF_SZ >> 8, ioaddr + TxFreeThreshold);        /* Room for a packet. */
832                 /* Clear the Tx ring. */
833                 for (i = 0; i < TX_RING_SIZE; i++)
834                         vp->tx_skbuff[i] = 0;
835                 outl(0, ioaddr + DownListPtr);
836         }
837         /* Set receiver mode: presumably accept b-case and phys addr only. */
838         set_rx_mode(dev);
839         outw(StatsEnable, ioaddr + EL3_CMD);    /* Turn on statistics. */
840
841         netif_start_queue(dev);
842
843         outw(RxEnable, ioaddr + EL3_CMD);       /* Enable the receiver. */
844         outw(TxEnable, ioaddr + EL3_CMD);       /* Enable transmitter. */
845         /* Allow status bits to be seen. */
846         outw(SetStatusEnb | AdapterFailure | IntReq | StatsFull |
847              (vp->full_bus_master_tx ? DownComplete : TxAvailable) |
848              (vp->full_bus_master_rx ? UpComplete : RxComplete) |
849              (vp->bus_master ? DMADone : 0), ioaddr + EL3_CMD);
850         /* Ack all pending events, and set active indicator mask. */
851         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
852              ioaddr + EL3_CMD);
853         outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
854              | (vp->bus_master ? DMADone : 0) | UpComplete | DownComplete,
855              ioaddr + EL3_CMD);
856
857         return 0;
858 }
859
860 static void corkscrew_timer(unsigned long data)
861 {
862 #ifdef AUTOMEDIA
863         struct net_device *dev = (struct net_device *) data;
864         struct corkscrew_private *vp = (struct corkscrew_private *) dev->priv;
865         int ioaddr = dev->base_addr;
866         unsigned long flags;
867         int ok = 0;
868
869         if (corkscrew_debug > 1)
870                 printk("%s: Media selection timer tick happened, %s.\n",
871                        dev->name, media_tbl[dev->if_port].name);
872
873         spin_lock_irqsave(&vp->lock, flags);
874         
875         {
876                 int old_window = inw(ioaddr + EL3_CMD) >> 13;
877                 int media_status;
878                 EL3WINDOW(4);
879                 media_status = inw(ioaddr + Wn4_Media);
880                 switch (dev->if_port) {
881                 case 0:
882                 case 4:
883                 case 5: /* 10baseT, 100baseTX, 100baseFX  */
884                         if (media_status & Media_LnkBeat) {
885                                 ok = 1;
886                                 if (corkscrew_debug > 1)
887                                         printk("%s: Media %s has link beat, %x.\n",
888                                                 dev->name,
889                                                 media_tbl[dev->if_port].name,
890                                                 media_status);
891                         } else if (corkscrew_debug > 1)
892                                 printk("%s: Media %s is has no link beat, %x.\n",
893                                         dev->name,
894                                         media_tbl[dev->if_port].name,
895                                         media_status);
896
897                         break;
898                 default:        /* Other media types handled by Tx timeouts. */
899                         if (corkscrew_debug > 1)
900                                 printk("%s: Media %s is has no indication, %x.\n",
901                                         dev->name,
902                                         media_tbl[dev->if_port].name,
903                                         media_status);
904                         ok = 1;
905                 }
906                 if (!ok) {
907                         union wn3_config config;
908
909                         do {
910                                 dev->if_port =
911                                     media_tbl[dev->if_port].next;
912                         }
913                         while (!(vp->available_media & media_tbl[dev->if_port].mask));
914                         
915                         if (dev->if_port == 8) {        /* Go back to default. */
916                                 dev->if_port = vp->default_media;
917                                 if (corkscrew_debug > 1)
918                                         printk("%s: Media selection failing, using default %s port.\n",
919                                                 dev->name,
920                                                 media_tbl[dev->if_port].name);
921                         } else {
922                                 if (corkscrew_debug > 1)
923                                         printk("%s: Media selection failed, now trying %s port.\n",
924                                                 dev->name,
925                                                 media_tbl[dev->if_port].name);
926                                 vp->timer.expires = jiffies + media_tbl[dev->if_port].wait;
927                                 add_timer(&vp->timer);
928                         }
929                         outw((media_status & ~(Media_10TP | Media_SQE)) |
930                              media_tbl[dev->if_port].media_bits,
931                              ioaddr + Wn4_Media);
932
933                         EL3WINDOW(3);
934                         config.i = inl(ioaddr + Wn3_Config);
935                         config.u.xcvr = dev->if_port;
936                         outl(config.i, ioaddr + Wn3_Config);
937
938                         outw(dev->if_port == 3 ? StartCoax : StopCoax,
939                              ioaddr + EL3_CMD);
940                 }
941                 EL3WINDOW(old_window);
942         }
943         
944         spin_unlock_irqrestore(&vp->lock, flags);
945         if (corkscrew_debug > 1)
946                 printk("%s: Media selection timer finished, %s.\n",
947                        dev->name, media_tbl[dev->if_port].name);
948
949 #endif                          /* AUTOMEDIA */
950         return;
951 }
952
953 static void corkscrew_timeout(struct net_device *dev)
954 {
955         int i;
956         struct corkscrew_private *vp =
957             (struct corkscrew_private *) dev->priv;
958         int ioaddr = dev->base_addr;
959
960         printk(KERN_WARNING
961                "%s: transmit timed out, tx_status %2.2x status %4.4x.\n",
962                dev->name, inb(ioaddr + TxStatus),
963                inw(ioaddr + EL3_STATUS));
964         /* Slight code bloat to be user friendly. */
965         if ((inb(ioaddr + TxStatus) & 0x88) == 0x88)
966                 printk(KERN_WARNING
967                        "%s: Transmitter encountered 16 collisions -- network"
968                        " network cable problem?\n", dev->name);
969 #ifndef final_version
970         printk("  Flags; bus-master %d, full %d; dirty %d current %d.\n",
971                vp->full_bus_master_tx, vp->tx_full, vp->dirty_tx,
972                vp->cur_tx);
973         printk("  Down list %8.8x vs. %p.\n", inl(ioaddr + DownListPtr),
974                &vp->tx_ring[0]);
975         for (i = 0; i < TX_RING_SIZE; i++) {
976                 printk("  %d: %p  length %8.8x status %8.8x\n", i,
977                        &vp->tx_ring[i],
978                        vp->tx_ring[i].length, vp->tx_ring[i].status);
979         }
980 #endif
981         /* Issue TX_RESET and TX_START commands. */
982         outw(TxReset, ioaddr + EL3_CMD);
983         for (i = 20; i >= 0; i--)
984                 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
985                         break;
986         outw(TxEnable, ioaddr + EL3_CMD);
987         dev->trans_start = jiffies;
988         vp->stats.tx_errors++;
989         vp->stats.tx_dropped++;
990         netif_wake_queue(dev);
991 }
992
993 static int corkscrew_start_xmit(struct sk_buff *skb,
994                                 struct net_device *dev)
995 {
996         struct corkscrew_private *vp =
997             (struct corkscrew_private *) dev->priv;
998         int ioaddr = dev->base_addr;
999
1000         /* Block a timer-based transmit from overlapping. */
1001
1002         netif_stop_queue(dev);
1003
1004         if (vp->full_bus_master_tx) {   /* BOOMERANG bus-master */
1005                 /* Calculate the next Tx descriptor entry. */
1006                 int entry = vp->cur_tx % TX_RING_SIZE;
1007                 struct boom_tx_desc *prev_entry;
1008                 unsigned long flags, i;
1009
1010                 if (vp->tx_full)        /* No room to transmit with */
1011                         return 1;
1012                 if (vp->cur_tx != 0)
1013                         prev_entry = &vp->tx_ring[(vp->cur_tx - 1) % TX_RING_SIZE];
1014                 else
1015                         prev_entry = NULL;
1016                 if (corkscrew_debug > 3)
1017                         printk("%s: Trying to send a packet, Tx index %d.\n",
1018                                 dev->name, vp->cur_tx);
1019                 /* vp->tx_full = 1; */
1020                 vp->tx_skbuff[entry] = skb;
1021                 vp->tx_ring[entry].next = 0;
1022                 vp->tx_ring[entry].addr = isa_virt_to_bus(skb->data);
1023                 vp->tx_ring[entry].length = skb->len | 0x80000000;
1024                 vp->tx_ring[entry].status = skb->len | 0x80000000;
1025
1026                 spin_lock_irqsave(&vp->lock, flags);
1027                 outw(DownStall, ioaddr + EL3_CMD);
1028                 /* Wait for the stall to complete. */
1029                 for (i = 20; i >= 0; i--)
1030                         if ((inw(ioaddr + EL3_STATUS) & CmdInProgress) == 0) 
1031                                 break;
1032                 if (prev_entry)
1033                         prev_entry->next = isa_virt_to_bus(&vp->tx_ring[entry]);
1034                 if (inl(ioaddr + DownListPtr) == 0) {
1035                         outl(isa_virt_to_bus(&vp->tx_ring[entry]),
1036                              ioaddr + DownListPtr);
1037                         queued_packet++;
1038                 }
1039                 outw(DownUnstall, ioaddr + EL3_CMD);
1040                 spin_unlock_irqrestore(&vp->lock, flags);
1041
1042                 vp->cur_tx++;
1043                 if (vp->cur_tx - vp->dirty_tx > TX_RING_SIZE - 1)
1044                         vp->tx_full = 1;
1045                 else {          /* Clear previous interrupt enable. */
1046                         if (prev_entry)
1047                                 prev_entry->status &= ~0x80000000;
1048                         netif_wake_queue(dev);
1049                 }
1050                 dev->trans_start = jiffies;
1051                 return 0;
1052         }
1053         /* Put out the doubleword header... */
1054         outl(skb->len, ioaddr + TX_FIFO);
1055         vp->stats.tx_bytes += skb->len;
1056 #ifdef VORTEX_BUS_MASTER
1057         if (vp->bus_master) {
1058                 /* Set the bus-master controller to transfer the packet. */
1059                 outl((int) (skb->data), ioaddr + Wn7_MasterAddr);
1060                 outw((skb->len + 3) & ~3, ioaddr + Wn7_MasterLen);
1061                 vp->tx_skb = skb;
1062                 outw(StartDMADown, ioaddr + EL3_CMD);
1063                 /* queue will be woken at the DMADone interrupt. */
1064         } else {
1065                 /* ... and the packet rounded to a doubleword. */
1066                 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
1067                 dev_kfree_skb(skb);
1068                 if (inw(ioaddr + TxFree) > 1536) {
1069                         netif_wake_queue(dev);
1070                 } else
1071                         /* Interrupt us when the FIFO has room for max-sized packet. */
1072                         outw(SetTxThreshold + (1536 >> 2),
1073                              ioaddr + EL3_CMD);
1074         }
1075 #else
1076         /* ... and the packet rounded to a doubleword. */
1077         outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
1078         dev_kfree_skb(skb);
1079         if (inw(ioaddr + TxFree) > 1536) {
1080                 netif_wake_queue(dev);
1081         } else
1082                 /* Interrupt us when the FIFO has room for max-sized packet. */
1083                 outw(SetTxThreshold + (1536 >> 2), ioaddr + EL3_CMD);
1084 #endif                          /* bus master */
1085
1086         dev->trans_start = jiffies;
1087
1088         /* Clear the Tx status stack. */
1089         {
1090                 short tx_status;
1091                 int i = 4;
1092
1093                 while (--i > 0 && (tx_status = inb(ioaddr + TxStatus)) > 0) {
1094                         if (tx_status & 0x3C) { /* A Tx-disabling error occurred.  */
1095                                 if (corkscrew_debug > 2)
1096                                         printk("%s: Tx error, status %2.2x.\n",
1097                                                 dev->name, tx_status);
1098                                 if (tx_status & 0x04)
1099                                         vp->stats.tx_fifo_errors++;
1100                                 if (tx_status & 0x38)
1101                                         vp->stats.tx_aborted_errors++;
1102                                 if (tx_status & 0x30) {
1103                                         int j;
1104                                         outw(TxReset, ioaddr + EL3_CMD);
1105                                         for (j = 20; j >= 0; j--)
1106                                                 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress)) 
1107                                                         break;
1108                                 }
1109                                 outw(TxEnable, ioaddr + EL3_CMD);
1110                         }
1111                         outb(0x00, ioaddr + TxStatus);  /* Pop the status stack. */
1112                 }
1113         }
1114         return 0;
1115 }
1116
1117 /* The interrupt handler does all of the Rx thread work and cleans up
1118    after the Tx thread. */
1119
1120 static irqreturn_t corkscrew_interrupt(int irq, void *dev_id,
1121                                     struct pt_regs *regs)
1122 {
1123         /* Use the now-standard shared IRQ implementation. */
1124         struct net_device *dev = dev_id;
1125         struct corkscrew_private *lp;
1126         int ioaddr, status;
1127         int latency;
1128         int i = max_interrupt_work;
1129
1130         ioaddr = dev->base_addr;
1131         latency = inb(ioaddr + Timer);
1132         lp = (struct corkscrew_private *) dev->priv;
1133
1134         spin_lock(&lp->lock);
1135         
1136         status = inw(ioaddr + EL3_STATUS);
1137
1138         if (corkscrew_debug > 4)
1139                 printk("%s: interrupt, status %4.4x, timer %d.\n",
1140                         dev->name, status, latency);
1141         if ((status & 0xE000) != 0xE000) {
1142                 static int donedidthis;
1143                 /* Some interrupt controllers store a bogus interrupt from boot-time.
1144                    Ignore a single early interrupt, but don't hang the machine for
1145                    other interrupt problems. */
1146                 if (donedidthis++ > 100) {
1147                         printk(KERN_ERR "%s: Bogus interrupt, bailing. Status %4.4x, start=%d.\n",
1148                                    dev->name, status, netif_running(dev));
1149                         free_irq(dev->irq, dev);
1150                         dev->irq = -1;
1151                 }
1152         }
1153
1154         do {
1155                 if (corkscrew_debug > 5)
1156                         printk("%s: In interrupt loop, status %4.4x.\n",
1157                                dev->name, status);
1158                 if (status & RxComplete)
1159                         corkscrew_rx(dev);
1160
1161                 if (status & TxAvailable) {
1162                         if (corkscrew_debug > 5)
1163                                 printk("        TX room bit was handled.\n");
1164                         /* There's room in the FIFO for a full-sized packet. */
1165                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
1166                         netif_wake_queue(dev);
1167                 }
1168                 if (status & DownComplete) {
1169                         unsigned int dirty_tx = lp->dirty_tx;
1170
1171                         while (lp->cur_tx - dirty_tx > 0) {
1172                                 int entry = dirty_tx % TX_RING_SIZE;
1173                                 if (inl(ioaddr + DownListPtr) == isa_virt_to_bus(&lp->tx_ring[entry]))
1174                                         break;  /* It still hasn't been processed. */
1175                                 if (lp->tx_skbuff[entry]) {
1176                                         dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1177                                         lp->tx_skbuff[entry] = 0;
1178                                 }
1179                                 dirty_tx++;
1180                         }
1181                         lp->dirty_tx = dirty_tx;
1182                         outw(AckIntr | DownComplete, ioaddr + EL3_CMD);
1183                         if (lp->tx_full && (lp->cur_tx - dirty_tx <= TX_RING_SIZE - 1)) {
1184                                 lp->tx_full = 0;
1185                                 netif_wake_queue(dev);
1186                         }
1187                 }
1188 #ifdef VORTEX_BUS_MASTER
1189                 if (status & DMADone) {
1190                         outw(0x1000, ioaddr + Wn7_MasterStatus);        /* Ack the event. */
1191                         dev_kfree_skb_irq(lp->tx_skb);  /* Release the transferred buffer */
1192                         netif_wake_queue(dev);
1193                 }
1194 #endif
1195                 if (status & UpComplete) {
1196                         boomerang_rx(dev);
1197                         outw(AckIntr | UpComplete, ioaddr + EL3_CMD);
1198                 }
1199                 if (status & (AdapterFailure | RxEarly | StatsFull)) {
1200                         /* Handle all uncommon interrupts at once. */
1201                         if (status & RxEarly) { /* Rx early is unused. */
1202                                 corkscrew_rx(dev);
1203                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
1204                         }
1205                         if (status & StatsFull) {       /* Empty statistics. */
1206                                 static int DoneDidThat;
1207                                 if (corkscrew_debug > 4)
1208                                         printk("%s: Updating stats.\n", dev->name);
1209                                 update_stats(ioaddr, dev);
1210                                 /* DEBUG HACK: Disable statistics as an interrupt source. */
1211                                 /* This occurs when we have the wrong media type! */
1212                                 if (DoneDidThat == 0 && inw(ioaddr + EL3_STATUS) & StatsFull) {
1213                                         int win, reg;
1214                                         printk("%s: Updating stats failed, disabling stats as an"
1215                                              " interrupt source.\n", dev->name);
1216                                         for (win = 0; win < 8; win++) {
1217                                                 EL3WINDOW(win);
1218                                                 printk("\n Vortex window %d:", win);
1219                                                 for (reg = 0; reg < 16; reg++)
1220                                                         printk(" %2.2x", inb(ioaddr + reg));
1221                                         }
1222                                         EL3WINDOW(7);
1223                                         outw(SetIntrEnb | TxAvailable |
1224                                              RxComplete | AdapterFailure |
1225                                              UpComplete | DownComplete |
1226                                              TxComplete, ioaddr + EL3_CMD);
1227                                         DoneDidThat++;
1228                                 }
1229                         }
1230                         if (status & AdapterFailure) {
1231                                 /* Adapter failure requires Rx reset and reinit. */
1232                                 outw(RxReset, ioaddr + EL3_CMD);
1233                                 /* Set the Rx filter to the current state. */
1234                                 set_rx_mode(dev);
1235                                 outw(RxEnable, ioaddr + EL3_CMD);       /* Re-enable the receiver. */
1236                                 outw(AckIntr | AdapterFailure,
1237                                      ioaddr + EL3_CMD);
1238                         }
1239                 }
1240
1241                 if (--i < 0) {
1242                         printk(KERN_ERR "%s: Too much work in interrupt, status %4.4x.  "
1243                              "Disabling functions (%4.4x).\n", dev->name,
1244                              status, SetStatusEnb | ((~status) & 0x7FE));
1245                         /* Disable all pending interrupts. */
1246                         outw(SetStatusEnb | ((~status) & 0x7FE), ioaddr + EL3_CMD);
1247                         outw(AckIntr | 0x7FF, ioaddr + EL3_CMD);
1248                         break;
1249                 }
1250                 /* Acknowledge the IRQ. */
1251                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
1252
1253         } while ((status = inw(ioaddr + EL3_STATUS)) & (IntLatch | RxComplete));
1254         
1255         spin_unlock(&lp->lock);
1256
1257         if (corkscrew_debug > 4)
1258                 printk("%s: exiting interrupt, status %4.4x.\n", dev->name, status);
1259         return IRQ_HANDLED;
1260 }
1261
1262 static int corkscrew_rx(struct net_device *dev)
1263 {
1264         struct corkscrew_private *vp = (struct corkscrew_private *) dev->priv;
1265         int ioaddr = dev->base_addr;
1266         int i;
1267         short rx_status;
1268
1269         if (corkscrew_debug > 5)
1270                 printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
1271                      inw(ioaddr + EL3_STATUS), inw(ioaddr + RxStatus));
1272         while ((rx_status = inw(ioaddr + RxStatus)) > 0) {
1273                 if (rx_status & 0x4000) {       /* Error, update stats. */
1274                         unsigned char rx_error = inb(ioaddr + RxErrors);
1275                         if (corkscrew_debug > 2)
1276                                 printk(" Rx error: status %2.2x.\n",
1277                                        rx_error);
1278                         vp->stats.rx_errors++;
1279                         if (rx_error & 0x01)
1280                                 vp->stats.rx_over_errors++;
1281                         if (rx_error & 0x02)
1282                                 vp->stats.rx_length_errors++;
1283                         if (rx_error & 0x04)
1284                                 vp->stats.rx_frame_errors++;
1285                         if (rx_error & 0x08)
1286                                 vp->stats.rx_crc_errors++;
1287                         if (rx_error & 0x10)
1288                                 vp->stats.rx_length_errors++;
1289                 } else {
1290                         /* The packet length: up to 4.5K!. */
1291                         short pkt_len = rx_status & 0x1fff;
1292                         struct sk_buff *skb;
1293
1294                         skb = dev_alloc_skb(pkt_len + 5 + 2);
1295                         if (corkscrew_debug > 4)
1296                                 printk("Receiving packet size %d status %4.4x.\n",
1297                                      pkt_len, rx_status);
1298                         if (skb != NULL) {
1299                                 skb->dev = dev;
1300                                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
1301                                 /* 'skb_put()' points to the start of sk_buff data area. */
1302                                 insl(ioaddr + RX_FIFO,
1303                                      skb_put(skb, pkt_len),
1304                                      (pkt_len + 3) >> 2);
1305                                 outw(RxDiscard, ioaddr + EL3_CMD);      /* Pop top Rx packet. */
1306                                 skb->protocol = eth_type_trans(skb, dev);
1307                                 netif_rx(skb);
1308                                 dev->last_rx = jiffies;
1309                                 vp->stats.rx_packets++;
1310                                 vp->stats.rx_bytes += pkt_len;
1311                                 /* Wait a limited time to go to next packet. */
1312                                 for (i = 200; i >= 0; i--)
1313                                         if (! (inw(ioaddr + EL3_STATUS) & CmdInProgress)) 
1314                                                 break;
1315                                 continue;
1316                         } else if (corkscrew_debug)
1317                                 printk("%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, pkt_len);
1318                 }
1319                 outw(RxDiscard, ioaddr + EL3_CMD);
1320                 vp->stats.rx_dropped++;
1321                 /* Wait a limited time to skip this packet. */
1322                 for (i = 200; i >= 0; i--)
1323                         if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
1324                                 break;
1325         }
1326         return 0;
1327 }
1328
1329 static int boomerang_rx(struct net_device *dev)
1330 {
1331         struct corkscrew_private *vp =
1332             (struct corkscrew_private *) dev->priv;
1333         int entry = vp->cur_rx % RX_RING_SIZE;
1334         int ioaddr = dev->base_addr;
1335         int rx_status;
1336
1337         if (corkscrew_debug > 5)
1338                 printk("   In boomerang_rx(), status %4.4x, rx_status %4.4x.\n",
1339                         inw(ioaddr + EL3_STATUS), inw(ioaddr + RxStatus));
1340         while ((rx_status = vp->rx_ring[entry].status) & RxDComplete) {
1341                 if (rx_status & RxDError) {     /* Error, update stats. */
1342                         unsigned char rx_error = rx_status >> 16;
1343                         if (corkscrew_debug > 2)
1344                                 printk(" Rx error: status %2.2x.\n",
1345                                        rx_error);
1346                         vp->stats.rx_errors++;
1347                         if (rx_error & 0x01)
1348                                 vp->stats.rx_over_errors++;
1349                         if (rx_error & 0x02)
1350                                 vp->stats.rx_length_errors++;
1351                         if (rx_error & 0x04)
1352                                 vp->stats.rx_frame_errors++;
1353                         if (rx_error & 0x08)
1354                                 vp->stats.rx_crc_errors++;
1355                         if (rx_error & 0x10)
1356                                 vp->stats.rx_length_errors++;
1357                 } else {
1358                         /* The packet length: up to 4.5K!. */
1359                         short pkt_len = rx_status & 0x1fff;
1360                         struct sk_buff *skb;
1361
1362                         vp->stats.rx_bytes += pkt_len;
1363                         if (corkscrew_debug > 4)
1364                                 printk("Receiving packet size %d status %4.4x.\n",
1365                                      pkt_len, rx_status);
1366
1367                         /* Check if the packet is long enough to just accept without
1368                            copying to a properly sized skbuff. */
1369                         if (pkt_len < rx_copybreak
1370                             && (skb = dev_alloc_skb(pkt_len + 4)) != 0) {
1371                                 skb->dev = dev;
1372                                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
1373                                 /* 'skb_put()' points to the start of sk_buff data area. */
1374                                 memcpy(skb_put(skb, pkt_len),
1375                                        isa_bus_to_virt(vp->rx_ring[entry].
1376                                                    addr), pkt_len);
1377                                 rx_copy++;
1378                         } else {
1379                                 void *temp;
1380                                 /* Pass up the skbuff already on the Rx ring. */
1381                                 skb = vp->rx_skbuff[entry];
1382                                 vp->rx_skbuff[entry] = NULL;
1383                                 temp = skb_put(skb, pkt_len);
1384                                 /* Remove this checking code for final release. */
1385                                 if (isa_bus_to_virt(vp->rx_ring[entry].addr) != temp)
1386                                             printk("%s: Warning -- the skbuff addresses do not match"
1387                                              " in boomerang_rx: %p vs. %p / %p.\n",
1388                                              dev->name,
1389                                              isa_bus_to_virt(vp->
1390                                                          rx_ring[entry].
1391                                                          addr), skb->head,
1392                                              temp);
1393                                 rx_nocopy++;
1394                         }
1395                         skb->protocol = eth_type_trans(skb, dev);
1396                         netif_rx(skb);
1397                         dev->last_rx = jiffies;
1398                         vp->stats.rx_packets++;
1399                 }
1400                 entry = (++vp->cur_rx) % RX_RING_SIZE;
1401         }
1402         /* Refill the Rx ring buffers. */
1403         for (; vp->cur_rx - vp->dirty_rx > 0; vp->dirty_rx++) {
1404                 struct sk_buff *skb;
1405                 entry = vp->dirty_rx % RX_RING_SIZE;
1406                 if (vp->rx_skbuff[entry] == NULL) {
1407                         skb = dev_alloc_skb(PKT_BUF_SZ);
1408                         if (skb == NULL)
1409                                 break;  /* Bad news!  */
1410                         skb->dev = dev; /* Mark as being used by this device. */
1411                         skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
1412                         vp->rx_ring[entry].addr = isa_virt_to_bus(skb->tail);
1413                         vp->rx_skbuff[entry] = skb;
1414                 }
1415                 vp->rx_ring[entry].status = 0;  /* Clear complete bit. */
1416         }
1417         return 0;
1418 }
1419
1420 static int corkscrew_close(struct net_device *dev)
1421 {
1422         struct corkscrew_private *vp =
1423             (struct corkscrew_private *) dev->priv;
1424         int ioaddr = dev->base_addr;
1425         int i;
1426
1427         netif_stop_queue(dev);
1428
1429         if (corkscrew_debug > 1) {
1430                 printk("%s: corkscrew_close() status %4.4x, Tx status %2.2x.\n",
1431                      dev->name, inw(ioaddr + EL3_STATUS),
1432                      inb(ioaddr + TxStatus));
1433                 printk("%s: corkscrew close stats: rx_nocopy %d rx_copy %d"
1434                        " tx_queued %d.\n", dev->name, rx_nocopy, rx_copy,
1435                        queued_packet);
1436         }
1437
1438         del_timer(&vp->timer);
1439
1440         /* Turn off statistics ASAP.  We update lp->stats below. */
1441         outw(StatsDisable, ioaddr + EL3_CMD);
1442
1443         /* Disable the receiver and transmitter. */
1444         outw(RxDisable, ioaddr + EL3_CMD);
1445         outw(TxDisable, ioaddr + EL3_CMD);
1446
1447         if (dev->if_port == XCVR_10base2)
1448                 /* Turn off thinnet power.  Green! */
1449                 outw(StopCoax, ioaddr + EL3_CMD);
1450
1451         free_irq(dev->irq, dev);
1452
1453         outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1454
1455         update_stats(ioaddr, dev);
1456         if (vp->full_bus_master_rx) {   /* Free Boomerang bus master Rx buffers. */
1457                 outl(0, ioaddr + UpListPtr);
1458                 for (i = 0; i < RX_RING_SIZE; i++)
1459                         if (vp->rx_skbuff[i]) {
1460                                 dev_kfree_skb(vp->rx_skbuff[i]);
1461                                 vp->rx_skbuff[i] = 0;
1462                         }
1463         }
1464         if (vp->full_bus_master_tx) {   /* Free Boomerang bus master Tx buffers. */
1465                 outl(0, ioaddr + DownListPtr);
1466                 for (i = 0; i < TX_RING_SIZE; i++)
1467                         if (vp->tx_skbuff[i]) {
1468                                 dev_kfree_skb(vp->tx_skbuff[i]);
1469                                 vp->tx_skbuff[i] = 0;
1470                         }
1471         }
1472
1473         return 0;
1474 }
1475
1476 static struct net_device_stats *corkscrew_get_stats(struct net_device *dev)
1477 {
1478         struct corkscrew_private *vp = (struct corkscrew_private *) dev->priv;
1479         unsigned long flags;
1480
1481         if (netif_running(dev)) {
1482                 spin_lock_irqsave(&vp->lock, flags);
1483                 update_stats(dev->base_addr, dev);
1484                 spin_unlock_irqrestore(&vp->lock, flags);
1485         }
1486         return &vp->stats;
1487 }
1488
1489 /*  Update statistics.
1490         Unlike with the EL3 we need not worry about interrupts changing
1491         the window setting from underneath us, but we must still guard
1492         against a race condition with a StatsUpdate interrupt updating the
1493         table.  This is done by checking that the ASM (!) code generated uses
1494         atomic updates with '+='.
1495         */
1496 static void update_stats(int ioaddr, struct net_device *dev)
1497 {
1498         struct corkscrew_private *vp =
1499             (struct corkscrew_private *) dev->priv;
1500
1501         /* Unlike the 3c5x9 we need not turn off stats updates while reading. */
1502         /* Switch to the stats window, and read everything. */
1503         EL3WINDOW(6);
1504         vp->stats.tx_carrier_errors += inb(ioaddr + 0);
1505         vp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
1506         /* Multiple collisions. */ inb(ioaddr + 2);
1507         vp->stats.collisions += inb(ioaddr + 3);
1508         vp->stats.tx_window_errors += inb(ioaddr + 4);
1509         vp->stats.rx_fifo_errors += inb(ioaddr + 5);
1510         vp->stats.tx_packets += inb(ioaddr + 6);
1511         vp->stats.tx_packets += (inb(ioaddr + 9) & 0x30) << 4;
1512                                                 /* Rx packets   */ inb(ioaddr + 7);
1513                                                 /* Must read to clear */
1514         /* Tx deferrals */ inb(ioaddr + 8);
1515         /* Don't bother with register 9, an extension of registers 6&7.
1516            If we do use the 6&7 values the atomic update assumption above
1517            is invalid. */
1518         inw(ioaddr + 10);       /* Total Rx and Tx octets. */
1519         inw(ioaddr + 12);
1520         /* New: On the Vortex we must also clear the BadSSD counter. */
1521         EL3WINDOW(4);
1522         inb(ioaddr + 12);
1523
1524         /* We change back to window 7 (not 1) with the Vortex. */
1525         EL3WINDOW(7);
1526         return;
1527 }
1528
1529 /* This new version of set_rx_mode() supports v1.4 kernels.
1530    The Vortex chip has no documented multicast filter, so the only
1531    multicast setting is to receive all multicast frames.  At least
1532    the chip has a very clean way to set the mode, unlike many others. */
1533 static void set_rx_mode(struct net_device *dev)
1534 {
1535         int ioaddr = dev->base_addr;
1536         short new_mode;
1537
1538         if (dev->flags & IFF_PROMISC) {
1539                 if (corkscrew_debug > 3)
1540                         printk("%s: Setting promiscuous mode.\n",
1541                                dev->name);
1542                 new_mode = SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm;
1543         } else if ((dev->mc_list) || (dev->flags & IFF_ALLMULTI)) {
1544                 new_mode = SetRxFilter | RxStation | RxMulticast | RxBroadcast;
1545         } else
1546                 new_mode = SetRxFilter | RxStation | RxBroadcast;
1547
1548         outw(new_mode, ioaddr + EL3_CMD);
1549 }
1550
1551 static void netdev_get_drvinfo(struct net_device *dev,
1552                                struct ethtool_drvinfo *info)
1553 {
1554         strcpy(info->driver, DRV_NAME);
1555         strcpy(info->version, DRV_VERSION);
1556         sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
1557 }
1558
1559 static u32 netdev_get_msglevel(struct net_device *dev)
1560 {
1561         return corkscrew_debug;
1562 }
1563
1564 static void netdev_set_msglevel(struct net_device *dev, u32 level)
1565 {
1566         corkscrew_debug = level;
1567 }
1568
1569 static struct ethtool_ops netdev_ethtool_ops = {
1570         .get_drvinfo            = netdev_get_drvinfo,
1571         .get_msglevel           = netdev_get_msglevel,
1572         .set_msglevel           = netdev_set_msglevel,
1573 };
1574
1575 \f
1576 #ifdef MODULE
1577 void cleanup_module(void)
1578 {
1579         while (!list_empty(&root_corkscrew_dev)) {
1580                 struct net_device *dev;
1581                 struct corkscrew_private *vp;
1582
1583                 vp = list_entry(root_corkscrew_dev.next,
1584                                 struct corkscrew_private, list);
1585                 dev = vp->our_dev;
1586                 unregister_netdev(dev);
1587                 cleanup_card(dev);
1588                 free_netdev(dev);
1589         }
1590 }
1591 #endif                          /* MODULE */
1592 \f
1593 /*
1594  * Local variables:
1595  *  compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c515.c"
1596  *  c-indent-level: 4
1597  *  tab-width: 4
1598  * End:
1599  */