Merge master.kernel.org:/pub/scm/linux/kernel/git/kyle/parisc-2.6
[pandora-kernel.git] / drivers / net / 3c59x.c
1 /* EtherLinkXL.c: A 3Com EtherLink PCI III/XL ethernet driver for linux. */
2 /*
3         Written 1996-1999 by Donald Becker.
4
5         This software may be used and distributed according to the terms
6         of the GNU General Public License, incorporated herein by reference.
7
8         This driver is for the 3Com "Vortex" and "Boomerang" series ethercards.
9         Members of the series include Fast EtherLink 3c590/3c592/3c595/3c597
10         and the EtherLink XL 3c900 and 3c905 cards.
11
12         Problem reports and questions should be directed to
13         vortex@scyld.com
14
15         The author may be reached as becker@scyld.com, or C/O
16         Scyld Computing Corporation
17         410 Severn Ave., Suite 210
18         Annapolis MD 21403
19
20         Linux Kernel Additions:
21         
22         0.99H+lk0.9 - David S. Miller - softnet, PCI DMA updates
23         0.99H+lk1.0 - Jeff Garzik <jgarzik@pobox.com>
24                 Remove compatibility defines for kernel versions < 2.2.x.
25                 Update for new 2.3.x module interface
26         LK1.1.2 (March 19, 2000)
27         * New PCI interface (jgarzik)
28
29     LK1.1.3 25 April 2000, Andrew Morton <andrewm@uow.edu.au>
30     - Merged with 3c575_cb.c
31     - Don't set RxComplete in boomerang interrupt enable reg
32     - spinlock in vortex_timer to protect mdio functions
33     - disable local interrupts around call to vortex_interrupt in
34       vortex_tx_timeout() (So vortex_interrupt can use spin_lock())
35     - Select window 3 in vortex_timer()'s write to Wn3_MAC_Ctrl
36     - In vortex_start_xmit(), move the lock to _after_ we've altered
37       vp->cur_tx and vp->tx_full.  This defeats the race between
38       vortex_start_xmit() and vortex_interrupt which was identified
39       by Bogdan Costescu.
40     - Merged back support for six new cards from various sources
41     - Set vortex_have_pci if pci_module_init returns zero (fixes cardbus
42       insertion oops)
43     - Tell it that 3c905C has NWAY for 100bT autoneg
44     - Fix handling of SetStatusEnd in 'Too much work..' code, as
45       per 2.3.99's 3c575_cb (Dave Hinds).
46     - Split ISR into two for vortex & boomerang
47     - Fix MOD_INC/DEC races
48     - Handle resource allocation failures.
49     - Fix 3CCFE575CT LED polarity
50     - Make tx_interrupt_mitigation the default
51
52     LK1.1.4 25 April 2000, Andrew Morton <andrewm@uow.edu.au>    
53     - Add extra TxReset to vortex_up() to fix 575_cb hotplug initialisation probs.
54     - Put vortex_info_tbl into __devinitdata
55     - In the vortex_error StatsFull HACK, disable stats in vp->intr_enable as well
56       as in the hardware.
57     - Increased the loop counter in issue_and_wait from 2,000 to 4,000.
58
59     LK1.1.5 28 April 2000, andrewm
60     - Added powerpc defines (John Daniel <jdaniel@etresoft.com> said these work...)
61     - Some extra diagnostics
62     - In vortex_error(), reset the Tx on maxCollisions.  Otherwise most
63       chips usually get a Tx timeout.
64     - Added extra_reset module parm
65     - Replaced some inline timer manip with mod_timer
66       (Franois romieu <Francois.Romieu@nic.fr>)
67     - In vortex_up(), don't make Wn3_config initialisation dependent upon has_nway
68       (this came across from 3c575_cb).
69
70     LK1.1.6 06 Jun 2000, andrewm
71     - Backed out the PPC defines.
72     - Use del_timer_sync(), mod_timer().
73     - Fix wrapped ulong comparison in boomerang_rx()
74     - Add IS_TORNADO, use it to suppress 3c905C checksum error msg
75       (Donald Becker, I Lee Hetherington <ilh@sls.lcs.mit.edu>)
76     - Replace union wn3_config with BFINS/BFEXT manipulation for
77       sparc64 (Pete Zaitcev, Peter Jones)
78     - In vortex_error, do_tx_reset and vortex_tx_timeout(Vortex):
79       do a netif_wake_queue() to better recover from errors. (Anders Pedersen,
80       Donald Becker)
81     - Print a warning on out-of-memory (rate limited to 1 per 10 secs)
82     - Added two more Cardbus 575 NICs: 5b57 and 6564 (Paul Wagland)
83
84     LK1.1.7 2 Jul 2000 andrewm
85     - Better handling of shared IRQs
86     - Reset the transmitter on a Tx reclaim error
87     - Fixed crash under OOM during vortex_open() (Mark Hemment)
88     - Fix Rx cessation problem during OOM (help from Mark Hemment)
89     - The spinlocks around the mdio access were blocking interrupts for 300uS.
90       Fix all this to use spin_lock_bh() within mdio_read/write
91     - Only write to TxFreeThreshold if it's a boomerang - other NICs don't
92       have one.
93     - Added 802.3x MAC-layer flow control support
94
95    LK1.1.8 13 Aug 2000 andrewm
96     - Ignore request_region() return value - already reserved if Cardbus.
97     - Merged some additional Cardbus flags from Don's 0.99Qk
98     - Some fixes for 3c556 (Fred Maciel)
99     - Fix for EISA initialisation (Jan Rekorajski)
100     - Renamed MII_XCVR_PWR and EEPROM_230 to align with 3c575_cb and D. Becker's drivers
101     - Fixed MII_XCVR_PWR for 3CCFE575CT
102     - Added INVERT_LED_PWR, used it.
103     - Backed out the extra_reset stuff
104
105    LK1.1.9 12 Sep 2000 andrewm
106     - Backed out the tx_reset_resume flags.  It was a no-op.
107     - In vortex_error, don't reset the Tx on txReclaim errors
108     - In vortex_error, don't reset the Tx on maxCollisions errors.
109       Hence backed out all the DownListPtr logic here.
110     - In vortex_error, give Tornado cards a partial TxReset on
111       maxCollisions (David Hinds).  Defined MAX_COLLISION_RESET for this.
112     - Redid some driver flags and device names based on pcmcia_cs-3.1.20.
113     - Fixed a bug where, if vp->tx_full is set when the interface
114       is downed, it remains set when the interface is upped.  Bad
115       things happen.
116
117    LK1.1.10 17 Sep 2000 andrewm
118     - Added EEPROM_8BIT for 3c555 (Fred Maciel)
119     - Added experimental support for the 3c556B Laptop Hurricane (Louis Gerbarg)
120     - Add HAS_NWAY to "3c900 Cyclone 10Mbps TPO"
121
122    LK1.1.11 13 Nov 2000 andrewm
123     - Dump MOD_INC/DEC_USE_COUNT, use SET_MODULE_OWNER
124
125    LK1.1.12 1 Jan 2001 andrewm (2.4.0-pre1)
126     - Call pci_enable_device before we request our IRQ (Tobias Ringstrom)
127     - Add 3c590 PCI latency timer hack to vortex_probe1 (from 0.99Ra)
128     - Added extended issue_and_wait for the 3c905CX.
129     - Look for an MII on PHY index 24 first (3c905CX oddity).
130     - Add HAS_NWAY to 3cSOHO100-TX (Brett Frankenberger)
131     - Don't free skbs we don't own on oom path in vortex_open().
132
133    LK1.1.13 27 Jan 2001
134     - Added explicit `medialock' flag so we can truly
135       lock the media type down with `options'.
136     - "check ioremap return and some tidbits" (Arnaldo Carvalho de Melo <acme@conectiva.com.br>)
137     - Added and used EEPROM_NORESET for 3c556B PM resumes.
138     - Fixed leakage of vp->rx_ring.
139     - Break out separate HAS_HWCKSM device capability flag.
140     - Kill vp->tx_full (ANK)
141     - Merge zerocopy fragment handling (ANK?)
142
143    LK1.1.14 15 Feb 2001
144     - Enable WOL.  Can be turned on with `enable_wol' module option.
145     - EISA and PCI initialisation fixes (jgarzik, Manfred Spraul)
146     - If a device's internalconfig register reports it has NWAY,
147       use it, even if autoselect is enabled.
148
149    LK1.1.15 6 June 2001 akpm
150     - Prevent double counting of received bytes (Lars Christensen)
151     - Add ethtool support (jgarzik)
152     - Add module parm descriptions (Andrzej M. Krzysztofowicz)
153     - Implemented alloc_etherdev() API
154     - Special-case the 'Tx error 82' message.
155
156    LK1.1.16 18 July 2001 akpm
157     - Make NETIF_F_SG dependent upon nr_free_highpages(), not on CONFIG_HIGHMEM
158     - Lessen verbosity of bootup messages
159     - Fix WOL - use new PM API functions.
160     - Use netif_running() instead of vp->open in suspend/resume.
161     - Don't reset the interface logic on open/close/rmmod.  It upsets
162       autonegotiation, and hence DHCP (from 0.99T).
163     - Back out EEPROM_NORESET flag because of the above (we do it for all
164       NICs).
165     - Correct 3c982 identification string
166     - Rename wait_for_completion() to issue_and_wait() to avoid completion.h
167       clash.
168
169    LK1.1.17 18Dec01 akpm
170     - PCI ID 9805 is a Python-T, not a dual-port Cyclone.  Apparently.
171       And it has NWAY.
172     - Mask our advertised modes (vp->advertising) with our capabilities
173           (MII reg5) when deciding which duplex mode to use.
174     - Add `global_options' as default for options[].  Ditto global_enable_wol,
175       global_full_duplex.
176
177    LK1.1.18 01Jul02 akpm
178     - Fix for undocumented transceiver power-up bit on some 3c566B's
179       (Donald Becker, Rahul Karnik)
180
181     - See http://www.zip.com.au/~akpm/linux/#3c59x-2.3 for more details.
182     - Also see Documentation/networking/vortex.txt
183
184    LK1.1.19 10Nov02 Marc Zyngier <maz@wild-wind.fr.eu.org>
185     - EISA sysfs integration.
186 */
187
188 /*
189  * FIXME: This driver _could_ support MTU changing, but doesn't.  See Don's hamachi.c implementation
190  * as well as other drivers
191  *
192  * NOTE: If you make 'vortex_debug' a constant (#define vortex_debug 0) the driver shrinks by 2k
193  * due to dead code elimination.  There will be some performance benefits from this due to
194  * elimination of all the tests and reduced cache footprint.
195  */
196
197
198 #define DRV_NAME        "3c59x"
199
200
201
202 /* A few values that may be tweaked. */
203 /* Keep the ring sizes a power of two for efficiency. */
204 #define TX_RING_SIZE    16
205 #define RX_RING_SIZE    32
206 #define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
207
208 /* "Knobs" that adjust features and parameters. */
209 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
210    Setting to > 1512 effectively disables this feature. */
211 #ifndef __arm__
212 static int rx_copybreak = 200;
213 #else
214 /* ARM systems perform better by disregarding the bus-master
215    transfer capability of these cards. -- rmk */
216 static int rx_copybreak = 1513;
217 #endif
218 /* Allow setting MTU to a larger size, bypassing the normal ethernet setup. */
219 static const int mtu = 1500;
220 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
221 static int max_interrupt_work = 32;
222 /* Tx timeout interval (millisecs) */
223 static int watchdog = 5000;
224
225 /* Allow aggregation of Tx interrupts.  Saves CPU load at the cost
226  * of possible Tx stalls if the system is blocking interrupts
227  * somewhere else.  Undefine this to disable.
228  */
229 #define tx_interrupt_mitigation 1
230
231 /* Put out somewhat more debugging messages. (0: no msg, 1 minimal .. 6). */
232 #define vortex_debug debug
233 #ifdef VORTEX_DEBUG
234 static int vortex_debug = VORTEX_DEBUG;
235 #else
236 static int vortex_debug = 1;
237 #endif
238
239 #include <linux/config.h>
240 #include <linux/module.h>
241 #include <linux/kernel.h>
242 #include <linux/string.h>
243 #include <linux/timer.h>
244 #include <linux/errno.h>
245 #include <linux/in.h>
246 #include <linux/ioport.h>
247 #include <linux/slab.h>
248 #include <linux/interrupt.h>
249 #include <linux/pci.h>
250 #include <linux/mii.h>
251 #include <linux/init.h>
252 #include <linux/netdevice.h>
253 #include <linux/etherdevice.h>
254 #include <linux/skbuff.h>
255 #include <linux/ethtool.h>
256 #include <linux/highmem.h>
257 #include <linux/eisa.h>
258 #include <linux/bitops.h>
259 #include <linux/jiffies.h>
260 #include <asm/irq.h>                    /* For NR_IRQS only. */
261 #include <asm/io.h>
262 #include <asm/uaccess.h>
263
264 /* Kernel compatibility defines, some common to David Hinds' PCMCIA package.
265    This is only in the support-all-kernels source code. */
266
267 #define RUN_AT(x) (jiffies + (x))
268
269 #include <linux/delay.h>
270
271
272 static char version[] __devinitdata =
273 DRV_NAME ": Donald Becker and others. www.scyld.com/network/vortex.html\n";
274
275 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
276 MODULE_DESCRIPTION("3Com 3c59x/3c9xx ethernet driver ");
277 MODULE_LICENSE("GPL");
278
279
280 /* Operational parameter that usually are not changed. */
281
282 /* The Vortex size is twice that of the original EtherLinkIII series: the
283    runtime register window, window 1, is now always mapped in.
284    The Boomerang size is twice as large as the Vortex -- it has additional
285    bus master control registers. */
286 #define VORTEX_TOTAL_SIZE 0x20
287 #define BOOMERANG_TOTAL_SIZE 0x40
288
289 /* Set iff a MII transceiver on any interface requires mdio preamble.
290    This only set with the original DP83840 on older 3c905 boards, so the extra
291    code size of a per-interface flag is not worthwhile. */
292 static char mii_preamble_required;
293
294 #define PFX DRV_NAME ": "
295
296
297
298 /*
299                                 Theory of Operation
300
301 I. Board Compatibility
302
303 This device driver is designed for the 3Com FastEtherLink and FastEtherLink
304 XL, 3Com's PCI to 10/100baseT adapters.  It also works with the 10Mbs
305 versions of the FastEtherLink cards.  The supported product IDs are
306   3c590, 3c592, 3c595, 3c597, 3c900, 3c905
307
308 The related ISA 3c515 is supported with a separate driver, 3c515.c, included
309 with the kernel source or available from
310     cesdis.gsfc.nasa.gov:/pub/linux/drivers/3c515.html
311
312 II. Board-specific settings
313
314 PCI bus devices are configured by the system at boot time, so no jumpers
315 need to be set on the board.  The system BIOS should be set to assign the
316 PCI INTA signal to an otherwise unused system IRQ line.
317
318 The EEPROM settings for media type and forced-full-duplex are observed.
319 The EEPROM media type should be left at the default "autoselect" unless using
320 10base2 or AUI connections which cannot be reliably detected.
321
322 III. Driver operation
323
324 The 3c59x series use an interface that's very similar to the previous 3c5x9
325 series.  The primary interface is two programmed-I/O FIFOs, with an
326 alternate single-contiguous-region bus-master transfer (see next).
327
328 The 3c900 "Boomerang" series uses a full-bus-master interface with separate
329 lists of transmit and receive descriptors, similar to the AMD LANCE/PCnet,
330 DEC Tulip and Intel Speedo3.  The first chip version retains a compatible
331 programmed-I/O interface that has been removed in 'B' and subsequent board
332 revisions.
333
334 One extension that is advertised in a very large font is that the adapters
335 are capable of being bus masters.  On the Vortex chip this capability was
336 only for a single contiguous region making it far less useful than the full
337 bus master capability.  There is a significant performance impact of taking
338 an extra interrupt or polling for the completion of each transfer, as well
339 as difficulty sharing the single transfer engine between the transmit and
340 receive threads.  Using DMA transfers is a win only with large blocks or
341 with the flawed versions of the Intel Orion motherboard PCI controller.
342
343 The Boomerang chip's full-bus-master interface is useful, and has the
344 currently-unused advantages over other similar chips that queued transmit
345 packets may be reordered and receive buffer groups are associated with a
346 single frame.
347
348 With full-bus-master support, this driver uses a "RX_COPYBREAK" scheme.
349 Rather than a fixed intermediate receive buffer, this scheme allocates
350 full-sized skbuffs as receive buffers.  The value RX_COPYBREAK is used as
351 the copying breakpoint: it is chosen to trade-off the memory wasted by
352 passing the full-sized skbuff to the queue layer for all frames vs. the
353 copying cost of copying a frame to a correctly-sized skbuff.
354
355 IIIC. Synchronization
356 The driver runs as two independent, single-threaded flows of control.  One
357 is the send-packet routine, which enforces single-threaded use by the
358 dev->tbusy flag.  The other thread is the interrupt handler, which is single
359 threaded by the hardware and other software.
360
361 IV. Notes
362
363 Thanks to Cameron Spitzer and Terry Murphy of 3Com for providing development
364 3c590, 3c595, and 3c900 boards.
365 The name "Vortex" is the internal 3Com project name for the PCI ASIC, and
366 the EISA version is called "Demon".  According to Terry these names come
367 from rides at the local amusement park.
368
369 The new chips support both ethernet (1.5K) and FDDI (4.5K) packet sizes!
370 This driver only supports ethernet packets because of the skbuff allocation
371 limit of 4K.
372 */
373
374 /* This table drives the PCI probe routines.  It's mostly boilerplate in all
375    of the drivers, and will likely be provided by some future kernel.
376 */
377 enum pci_flags_bit {
378         PCI_USES_MASTER=4,
379 };
380
381 enum {  IS_VORTEX=1, IS_BOOMERANG=2, IS_CYCLONE=4, IS_TORNADO=8,
382         EEPROM_8BIT=0x10,       /* AKPM: Uses 0x230 as the base bitmaps for EEPROM reads */
383         HAS_PWR_CTRL=0x20, HAS_MII=0x40, HAS_NWAY=0x80, HAS_CB_FNS=0x100,
384         INVERT_MII_PWR=0x200, INVERT_LED_PWR=0x400, MAX_COLLISION_RESET=0x800,
385         EEPROM_OFFSET=0x1000, HAS_HWCKSM=0x2000, WNO_XCVR_PWR=0x4000,
386         EXTRA_PREAMBLE=0x8000, EEPROM_RESET=0x10000, };
387
388 enum vortex_chips {
389         CH_3C590 = 0,
390         CH_3C592,
391         CH_3C597,
392         CH_3C595_1,
393         CH_3C595_2,
394
395         CH_3C595_3,
396         CH_3C900_1,
397         CH_3C900_2,
398         CH_3C900_3,
399         CH_3C900_4,
400
401         CH_3C900_5,
402         CH_3C900B_FL,
403         CH_3C905_1,
404         CH_3C905_2,
405         CH_3C905B_1,
406
407         CH_3C905B_2,
408         CH_3C905B_FX,
409         CH_3C905C,
410         CH_3C9202,
411         CH_3C980,
412         CH_3C9805,
413
414         CH_3CSOHO100_TX,
415         CH_3C555,
416         CH_3C556,
417         CH_3C556B,
418         CH_3C575,
419
420         CH_3C575_1,
421         CH_3CCFE575,
422         CH_3CCFE575CT,
423         CH_3CCFE656,
424         CH_3CCFEM656,
425
426         CH_3CCFEM656_1,
427         CH_3C450,
428         CH_3C920,
429         CH_3C982A,
430         CH_3C982B,
431
432         CH_905BT4,
433         CH_920B_EMB_WNM,
434 };
435
436
437 /* note: this array directly indexed by above enums, and MUST
438  * be kept in sync with both the enums above, and the PCI device
439  * table below
440  */
441 static struct vortex_chip_info {
442         const char *name;
443         int flags;
444         int drv_flags;
445         int io_size;
446 } vortex_info_tbl[] __devinitdata = {
447         {"3c590 Vortex 10Mbps",
448          PCI_USES_MASTER, IS_VORTEX, 32, },
449         {"3c592 EISA 10Mbps Demon/Vortex",                                      /* AKPM: from Don's 3c59x_cb.c 0.49H */
450          PCI_USES_MASTER, IS_VORTEX, 32, },
451         {"3c597 EISA Fast Demon/Vortex",                                        /* AKPM: from Don's 3c59x_cb.c 0.49H */
452          PCI_USES_MASTER, IS_VORTEX, 32, },
453         {"3c595 Vortex 100baseTx",
454          PCI_USES_MASTER, IS_VORTEX, 32, },
455         {"3c595 Vortex 100baseT4",
456          PCI_USES_MASTER, IS_VORTEX, 32, },
457
458         {"3c595 Vortex 100base-MII",
459          PCI_USES_MASTER, IS_VORTEX, 32, },
460         {"3c900 Boomerang 10baseT",
461          PCI_USES_MASTER, IS_BOOMERANG|EEPROM_RESET, 64, },
462         {"3c900 Boomerang 10Mbps Combo",
463          PCI_USES_MASTER, IS_BOOMERANG|EEPROM_RESET, 64, },
464         {"3c900 Cyclone 10Mbps TPO",                                            /* AKPM: from Don's 0.99M */
465          PCI_USES_MASTER, IS_CYCLONE|HAS_HWCKSM, 128, },
466         {"3c900 Cyclone 10Mbps Combo",
467          PCI_USES_MASTER, IS_CYCLONE|HAS_HWCKSM, 128, },
468
469         {"3c900 Cyclone 10Mbps TPC",                                            /* AKPM: from Don's 0.99M */
470          PCI_USES_MASTER, IS_CYCLONE|HAS_HWCKSM, 128, },
471         {"3c900B-FL Cyclone 10base-FL",
472          PCI_USES_MASTER, IS_CYCLONE|HAS_HWCKSM, 128, },
473         {"3c905 Boomerang 100baseTx",
474          PCI_USES_MASTER, IS_BOOMERANG|HAS_MII|EEPROM_RESET, 64, },
475         {"3c905 Boomerang 100baseT4",
476          PCI_USES_MASTER, IS_BOOMERANG|HAS_MII|EEPROM_RESET, 64, },
477         {"3c905B Cyclone 100baseTx",
478          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_HWCKSM|EXTRA_PREAMBLE, 128, },
479
480         {"3c905B Cyclone 10/100/BNC",
481          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_HWCKSM, 128, },
482         {"3c905B-FX Cyclone 100baseFx",
483          PCI_USES_MASTER, IS_CYCLONE|HAS_HWCKSM, 128, },
484         {"3c905C Tornado",
485         PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|HAS_HWCKSM|EXTRA_PREAMBLE, 128, },
486         {"3c920B-EMB-WNM (ATI Radeon 9100 IGP)",
487          PCI_USES_MASTER, IS_TORNADO|HAS_MII|HAS_HWCKSM, 128, },
488         {"3c980 Cyclone",
489          PCI_USES_MASTER, IS_CYCLONE|HAS_HWCKSM, 128, },
490
491         {"3c980C Python-T",
492          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_HWCKSM, 128, },
493         {"3cSOHO100-TX Hurricane",
494          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_HWCKSM, 128, },
495         {"3c555 Laptop Hurricane",
496          PCI_USES_MASTER, IS_CYCLONE|EEPROM_8BIT|HAS_HWCKSM, 128, },
497         {"3c556 Laptop Tornado",
498          PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|EEPROM_8BIT|HAS_CB_FNS|INVERT_MII_PWR|
499                                                                         HAS_HWCKSM, 128, },
500         {"3c556B Laptop Hurricane",
501          PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|EEPROM_OFFSET|HAS_CB_FNS|INVERT_MII_PWR|
502                                         WNO_XCVR_PWR|HAS_HWCKSM, 128, },
503
504         {"3c575 [Megahertz] 10/100 LAN  CardBus",
505         PCI_USES_MASTER, IS_BOOMERANG|HAS_MII|EEPROM_8BIT, 128, },
506         {"3c575 Boomerang CardBus",
507          PCI_USES_MASTER, IS_BOOMERANG|HAS_MII|EEPROM_8BIT, 128, },
508         {"3CCFE575BT Cyclone CardBus",
509          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_CB_FNS|EEPROM_8BIT|
510                                                                         INVERT_LED_PWR|HAS_HWCKSM, 128, },
511         {"3CCFE575CT Tornado CardBus",
512          PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|HAS_CB_FNS|EEPROM_8BIT|INVERT_MII_PWR|
513                                                                         MAX_COLLISION_RESET|HAS_HWCKSM, 128, },
514         {"3CCFE656 Cyclone CardBus",
515          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_CB_FNS|EEPROM_8BIT|INVERT_MII_PWR|
516                                                                         INVERT_LED_PWR|HAS_HWCKSM, 128, },
517
518         {"3CCFEM656B Cyclone+Winmodem CardBus",
519          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_CB_FNS|EEPROM_8BIT|INVERT_MII_PWR|
520                                                                         INVERT_LED_PWR|HAS_HWCKSM, 128, },
521         {"3CXFEM656C Tornado+Winmodem CardBus",                 /* From pcmcia-cs-3.1.5 */
522          PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|HAS_CB_FNS|EEPROM_8BIT|INVERT_MII_PWR|
523                                                                         MAX_COLLISION_RESET|HAS_HWCKSM, 128, },
524         {"3c450 HomePNA Tornado",                                               /* AKPM: from Don's 0.99Q */
525          PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|HAS_HWCKSM, 128, },
526         {"3c920 Tornado",
527          PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|HAS_HWCKSM, 128, },
528         {"3c982 Hydra Dual Port A",
529          PCI_USES_MASTER, IS_TORNADO|HAS_HWCKSM|HAS_NWAY, 128, },
530
531         {"3c982 Hydra Dual Port B",
532          PCI_USES_MASTER, IS_TORNADO|HAS_HWCKSM|HAS_NWAY, 128, },
533         {"3c905B-T4",
534          PCI_USES_MASTER, IS_CYCLONE|HAS_NWAY|HAS_HWCKSM|EXTRA_PREAMBLE, 128, },
535         {"3c920B-EMB-WNM Tornado",
536          PCI_USES_MASTER, IS_TORNADO|HAS_NWAY|HAS_HWCKSM, 128, },
537
538         {NULL,}, /* NULL terminated list. */
539 };
540
541
542 static struct pci_device_id vortex_pci_tbl[] = {
543         { 0x10B7, 0x5900, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C590 },
544         { 0x10B7, 0x5920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C592 },
545         { 0x10B7, 0x5970, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C597 },
546         { 0x10B7, 0x5950, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C595_1 },
547         { 0x10B7, 0x5951, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C595_2 },
548
549         { 0x10B7, 0x5952, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C595_3 },
550         { 0x10B7, 0x9000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C900_1 },
551         { 0x10B7, 0x9001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C900_2 },
552         { 0x10B7, 0x9004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C900_3 },
553         { 0x10B7, 0x9005, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C900_4 },
554
555         { 0x10B7, 0x9006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C900_5 },
556         { 0x10B7, 0x900A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C900B_FL },
557         { 0x10B7, 0x9050, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C905_1 },
558         { 0x10B7, 0x9051, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C905_2 },
559         { 0x10B7, 0x9055, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C905B_1 },
560
561         { 0x10B7, 0x9058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C905B_2 },
562         { 0x10B7, 0x905A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C905B_FX },
563         { 0x10B7, 0x9200, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C905C },
564         { 0x10B7, 0x9202, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C9202 },
565         { 0x10B7, 0x9800, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C980 },
566         { 0x10B7, 0x9805, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C9805 },
567
568         { 0x10B7, 0x7646, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3CSOHO100_TX },
569         { 0x10B7, 0x5055, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C555 },
570         { 0x10B7, 0x6055, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C556 },
571         { 0x10B7, 0x6056, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C556B },
572         { 0x10B7, 0x5b57, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C575 },
573
574         { 0x10B7, 0x5057, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C575_1 },
575         { 0x10B7, 0x5157, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3CCFE575 },
576         { 0x10B7, 0x5257, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3CCFE575CT },
577         { 0x10B7, 0x6560, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3CCFE656 },
578         { 0x10B7, 0x6562, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3CCFEM656 },
579
580         { 0x10B7, 0x6564, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3CCFEM656_1 },
581         { 0x10B7, 0x4500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C450 },
582         { 0x10B7, 0x9201, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C920 },
583         { 0x10B7, 0x1201, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C982A },
584         { 0x10B7, 0x1202, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_3C982B },
585
586         { 0x10B7, 0x9056, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_905BT4 },
587         { 0x10B7, 0x9210, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_920B_EMB_WNM },
588
589         {0,}                                            /* 0 terminated list. */
590 };
591 MODULE_DEVICE_TABLE(pci, vortex_pci_tbl);
592
593
594 /* Operational definitions.
595    These are not used by other compilation units and thus are not
596    exported in a ".h" file.
597
598    First the windows.  There are eight register windows, with the command
599    and status registers available in each.
600    */
601 #define EL3WINDOW(win_num) iowrite16(SelectWindow + (win_num), ioaddr + EL3_CMD)
602 #define EL3_CMD 0x0e
603 #define EL3_STATUS 0x0e
604
605 /* The top five bits written to EL3_CMD are a command, the lower
606    11 bits are the parameter, if applicable.
607    Note that 11 parameters bits was fine for ethernet, but the new chip
608    can handle FDDI length frames (~4500 octets) and now parameters count
609    32-bit 'Dwords' rather than octets. */
610
611 enum vortex_cmd {
612         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
613         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11,
614         UpStall = 6<<11, UpUnstall = (6<<11)+1,
615         DownStall = (6<<11)+2, DownUnstall = (6<<11)+3,
616         RxDiscard = 8<<11, TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
617         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
618         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
619         SetTxThreshold = 18<<11, SetTxStart = 19<<11,
620         StartDMAUp = 20<<11, StartDMADown = (20<<11)+1, StatsEnable = 21<<11,
621         StatsDisable = 22<<11, StopCoax = 23<<11, SetFilterBit = 25<<11,};
622
623 /* The SetRxFilter command accepts the following classes: */
624 enum RxFilter {
625         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
626
627 /* Bits in the general status register. */
628 enum vortex_status {
629         IntLatch = 0x0001, HostError = 0x0002, TxComplete = 0x0004,
630         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
631         IntReq = 0x0040, StatsFull = 0x0080,
632         DMADone = 1<<8, DownComplete = 1<<9, UpComplete = 1<<10,
633         DMAInProgress = 1<<11,                  /* DMA controller is still busy.*/
634         CmdInProgress = 1<<12,                  /* EL3_CMD is still busy.*/
635 };
636
637 /* Register window 1 offsets, the window used in normal operation.
638    On the Vortex this window is always mapped at offsets 0x10-0x1f. */
639 enum Window1 {
640         TX_FIFO = 0x10,  RX_FIFO = 0x10,  RxErrors = 0x14,
641         RxStatus = 0x18,  Timer=0x1A, TxStatus = 0x1B,
642         TxFree = 0x1C, /* Remaining free bytes in Tx buffer. */
643 };
644 enum Window0 {
645         Wn0EepromCmd = 10,              /* Window 0: EEPROM command register. */
646         Wn0EepromData = 12,             /* Window 0: EEPROM results register. */
647         IntrStatus=0x0E,                /* Valid in all windows. */
648 };
649 enum Win0_EEPROM_bits {
650         EEPROM_Read = 0x80, EEPROM_WRITE = 0x40, EEPROM_ERASE = 0xC0,
651         EEPROM_EWENB = 0x30,            /* Enable erasing/writing for 10 msec. */
652         EEPROM_EWDIS = 0x00,            /* Disable EWENB before 10 msec timeout. */
653 };
654 /* EEPROM locations. */
655 enum eeprom_offset {
656         PhysAddr01=0, PhysAddr23=1, PhysAddr45=2, ModelID=3,
657         EtherLink3ID=7, IFXcvrIO=8, IRQLine=9,
658         NodeAddr01=10, NodeAddr23=11, NodeAddr45=12,
659         DriverTune=13, Checksum=15};
660
661 enum Window2 {                  /* Window 2. */
662         Wn2_ResetOptions=12,
663 };
664 enum Window3 {                  /* Window 3: MAC/config bits. */
665         Wn3_Config=0, Wn3_MaxPktSize=4, Wn3_MAC_Ctrl=6, Wn3_Options=8,
666 };
667
668 #define BFEXT(value, offset, bitcount)  \
669     ((((unsigned long)(value)) >> (offset)) & ((1 << (bitcount)) - 1))
670
671 #define BFINS(lhs, rhs, offset, bitcount)                                       \
672         (((lhs) & ~((((1 << (bitcount)) - 1)) << (offset))) |   \
673         (((rhs) & ((1 << (bitcount)) - 1)) << (offset)))
674
675 #define RAM_SIZE(v)             BFEXT(v, 0, 3)
676 #define RAM_WIDTH(v)    BFEXT(v, 3, 1)
677 #define RAM_SPEED(v)    BFEXT(v, 4, 2)
678 #define ROM_SIZE(v)             BFEXT(v, 6, 2)
679 #define RAM_SPLIT(v)    BFEXT(v, 16, 2)
680 #define XCVR(v)                 BFEXT(v, 20, 4)
681 #define AUTOSELECT(v)   BFEXT(v, 24, 1)
682
683 enum Window4 {          /* Window 4: Xcvr/media bits. */
684         Wn4_FIFODiag = 4, Wn4_NetDiag = 6, Wn4_PhysicalMgmt=8, Wn4_Media = 10,
685 };
686 enum Win4_Media_bits {
687         Media_SQE = 0x0008,             /* Enable SQE error counting for AUI. */
688         Media_10TP = 0x00C0,    /* Enable link beat and jabber for 10baseT. */
689         Media_Lnk = 0x0080,             /* Enable just link beat for 100TX/100FX. */
690         Media_LnkBeat = 0x0800,
691 };
692 enum Window7 {                                  /* Window 7: Bus Master control. */
693         Wn7_MasterAddr = 0, Wn7_VlanEtherType=4, Wn7_MasterLen = 6,
694         Wn7_MasterStatus = 12,
695 };
696 /* Boomerang bus master control registers. */
697 enum MasterCtrl {
698         PktStatus = 0x20, DownListPtr = 0x24, FragAddr = 0x28, FragLen = 0x2c,
699         TxFreeThreshold = 0x2f, UpPktStatus = 0x30, UpListPtr = 0x38,
700 };
701
702 /* The Rx and Tx descriptor lists.
703    Caution Alpha hackers: these types are 32 bits!  Note also the 8 byte
704    alignment contraint on tx_ring[] and rx_ring[]. */
705 #define LAST_FRAG       0x80000000                      /* Last Addr/Len pair in descriptor. */
706 #define DN_COMPLETE     0x00010000                      /* This packet has been downloaded */
707 struct boom_rx_desc {
708         u32 next;                                       /* Last entry points to 0.   */
709         s32 status;
710         u32 addr;                                       /* Up to 63 addr/len pairs possible. */
711         s32 length;                                     /* Set LAST_FRAG to indicate last pair. */
712 };
713 /* Values for the Rx status entry. */
714 enum rx_desc_status {
715         RxDComplete=0x00008000, RxDError=0x4000,
716         /* See boomerang_rx() for actual error bits */
717         IPChksumErr=1<<25, TCPChksumErr=1<<26, UDPChksumErr=1<<27,
718         IPChksumValid=1<<29, TCPChksumValid=1<<30, UDPChksumValid=1<<31,
719 };
720
721 #ifdef MAX_SKB_FRAGS
722 #define DO_ZEROCOPY 1
723 #else
724 #define DO_ZEROCOPY 0
725 #endif
726
727 struct boom_tx_desc {
728         u32 next;                                       /* Last entry points to 0.   */
729         s32 status;                                     /* bits 0:12 length, others see below.  */
730 #if DO_ZEROCOPY
731         struct {
732                 u32 addr;
733                 s32 length;
734         } frag[1+MAX_SKB_FRAGS];
735 #else
736                 u32 addr;
737                 s32 length;
738 #endif
739 };
740
741 /* Values for the Tx status entry. */
742 enum tx_desc_status {
743         CRCDisable=0x2000, TxDComplete=0x8000,
744         AddIPChksum=0x02000000, AddTCPChksum=0x04000000, AddUDPChksum=0x08000000,
745         TxIntrUploaded=0x80000000,              /* IRQ when in FIFO, but maybe not sent. */
746 };
747
748 /* Chip features we care about in vp->capabilities, read from the EEPROM. */
749 enum ChipCaps { CapBusMaster=0x20, CapPwrMgmt=0x2000 };
750
751 struct vortex_extra_stats {
752         unsigned long tx_deferred;
753         unsigned long tx_max_collisions;
754         unsigned long tx_multiple_collisions;
755         unsigned long tx_single_collisions;
756         unsigned long rx_bad_ssd;
757 };
758
759 struct vortex_private {
760         /* The Rx and Tx rings should be quad-word-aligned. */
761         struct boom_rx_desc* rx_ring;
762         struct boom_tx_desc* tx_ring;
763         dma_addr_t rx_ring_dma;
764         dma_addr_t tx_ring_dma;
765         /* The addresses of transmit- and receive-in-place skbuffs. */
766         struct sk_buff* rx_skbuff[RX_RING_SIZE];
767         struct sk_buff* tx_skbuff[TX_RING_SIZE];
768         unsigned int cur_rx, cur_tx;            /* The next free ring entry */
769         unsigned int dirty_rx, dirty_tx;        /* The ring entries to be free()ed. */
770         struct net_device_stats stats;          /* Generic stats */
771         struct vortex_extra_stats xstats;       /* NIC-specific extra stats */
772         struct sk_buff *tx_skb;                         /* Packet being eaten by bus master ctrl.  */
773         dma_addr_t tx_skb_dma;                          /* Allocated DMA address for bus master ctrl DMA.   */
774
775         /* PCI configuration space information. */
776         struct device *gendev;
777         void __iomem *ioaddr;                   /* IO address space */
778         void __iomem *cb_fn_base;               /* CardBus function status addr space. */
779
780         /* Some values here only for performance evaluation and path-coverage */
781         int rx_nocopy, rx_copy, queued_packet, rx_csumhits;
782         int card_idx;
783
784         /* The remainder are related to chip state, mostly media selection. */
785         struct timer_list timer;                        /* Media selection timer. */
786         struct timer_list rx_oom_timer;         /* Rx skb allocation retry timer */
787         int options;                                            /* User-settable misc. driver options. */
788         unsigned int media_override:4,          /* Passed-in media type. */
789                 default_media:4,                                /* Read from the EEPROM/Wn3_Config. */
790                 full_duplex:1, autoselect:1,
791                 bus_master:1,                                   /* Vortex can only do a fragment bus-m. */
792                 full_bus_master_tx:1, full_bus_master_rx:2, /* Boomerang  */
793                 flow_ctrl:1,                                    /* Use 802.3x flow control (PAUSE only) */
794                 partner_flow_ctrl:1,                    /* Partner supports flow control */
795                 has_nway:1,
796                 enable_wol:1,                                   /* Wake-on-LAN is enabled */
797                 pm_state_valid:1,                               /* pci_dev->saved_config_space has sane contents */
798                 open:1,
799                 medialock:1,
800                 must_free_region:1,                             /* Flag: if zero, Cardbus owns the I/O region */
801                 large_frames:1;                 /* accept large frames */
802         int drv_flags;
803         u16 status_enable;
804         u16 intr_enable;
805         u16 available_media;                            /* From Wn3_Options. */
806         u16 capabilities, info1, info2;         /* Various, from EEPROM. */
807         u16 advertising;                                        /* NWay media advertisement */
808         unsigned char phys[2];                          /* MII device addresses. */
809         u16 deferred;                                           /* Resend these interrupts when we
810                                                                                  * bale from the ISR */
811         u16 io_size;                                            /* Size of PCI region (for release_region) */
812         spinlock_t lock;                                        /* Serialise access to device & its vortex_private */
813         struct mii_if_info mii;                         /* MII lib hooks/info */
814 };
815
816 #ifdef CONFIG_PCI
817 #define DEVICE_PCI(dev) (((dev)->bus == &pci_bus_type) ? to_pci_dev((dev)) : NULL)
818 #else
819 #define DEVICE_PCI(dev) NULL
820 #endif
821
822 #define VORTEX_PCI(vp) (((vp)->gendev) ? DEVICE_PCI((vp)->gendev) : NULL)
823
824 #ifdef CONFIG_EISA
825 #define DEVICE_EISA(dev) (((dev)->bus == &eisa_bus_type) ? to_eisa_device((dev)) : NULL)
826 #else
827 #define DEVICE_EISA(dev) NULL
828 #endif
829
830 #define VORTEX_EISA(vp) (((vp)->gendev) ? DEVICE_EISA((vp)->gendev) : NULL)
831
832 /* The action to take with a media selection timer tick.
833    Note that we deviate from the 3Com order by checking 10base2 before AUI.
834  */
835 enum xcvr_types {
836         XCVR_10baseT=0, XCVR_AUI, XCVR_10baseTOnly, XCVR_10base2, XCVR_100baseTx,
837         XCVR_100baseFx, XCVR_MII=6, XCVR_NWAY=8, XCVR_ExtMII=9, XCVR_Default=10,
838 };
839
840 static const struct media_table {
841         char *name;
842         unsigned int media_bits:16,             /* Bits to set in Wn4_Media register. */
843                 mask:8,                                         /* The transceiver-present bit in Wn3_Config.*/
844                 next:8;                                         /* The media type to try next. */
845         int wait;                                               /* Time before we check media status. */
846 } media_tbl[] = {
847   {     "10baseT",   Media_10TP,0x08, XCVR_10base2, (14*HZ)/10},
848   { "10Mbs AUI", Media_SQE, 0x20, XCVR_Default, (1*HZ)/10},
849   { "undefined", 0,                     0x80, XCVR_10baseT, 10000},
850   { "10base2",   0,                     0x10, XCVR_AUI,         (1*HZ)/10},
851   { "100baseTX", Media_Lnk, 0x02, XCVR_100baseFx, (14*HZ)/10},
852   { "100baseFX", Media_Lnk, 0x04, XCVR_MII,             (14*HZ)/10},
853   { "MII",               0,                     0x41, XCVR_10baseT, 3*HZ },
854   { "undefined", 0,                     0x01, XCVR_10baseT, 10000},
855   { "Autonegotiate", 0,         0x41, XCVR_10baseT, 3*HZ},
856   { "MII-External",      0,             0x41, XCVR_10baseT, 3*HZ },
857   { "Default",   0,                     0xFF, XCVR_10baseT, 10000},
858 };
859
860 static struct {
861         const char str[ETH_GSTRING_LEN];
862 } ethtool_stats_keys[] = {
863         { "tx_deferred" },
864         { "tx_max_collisions" },
865         { "tx_multiple_collisions" },
866         { "tx_single_collisions" },
867         { "rx_bad_ssd" },
868 };
869
870 /* number of ETHTOOL_GSTATS u64's */
871 #define VORTEX_NUM_STATS    5
872
873 static int vortex_probe1(struct device *gendev, void __iomem *ioaddr, int irq,
874                                    int chip_idx, int card_idx);
875 static void vortex_up(struct net_device *dev);
876 static void vortex_down(struct net_device *dev, int final);
877 static int vortex_open(struct net_device *dev);
878 static void mdio_sync(void __iomem *ioaddr, int bits);
879 static int mdio_read(struct net_device *dev, int phy_id, int location);
880 static void mdio_write(struct net_device *vp, int phy_id, int location, int value);
881 static void vortex_timer(unsigned long arg);
882 static void rx_oom_timer(unsigned long arg);
883 static int vortex_start_xmit(struct sk_buff *skb, struct net_device *dev);
884 static int boomerang_start_xmit(struct sk_buff *skb, struct net_device *dev);
885 static int vortex_rx(struct net_device *dev);
886 static int boomerang_rx(struct net_device *dev);
887 static irqreturn_t vortex_interrupt(int irq, void *dev_id, struct pt_regs *regs);
888 static irqreturn_t boomerang_interrupt(int irq, void *dev_id, struct pt_regs *regs);
889 static int vortex_close(struct net_device *dev);
890 static void dump_tx_ring(struct net_device *dev);
891 static void update_stats(void __iomem *ioaddr, struct net_device *dev);
892 static struct net_device_stats *vortex_get_stats(struct net_device *dev);
893 static void set_rx_mode(struct net_device *dev);
894 #ifdef CONFIG_PCI
895 static int vortex_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
896 #endif
897 static void vortex_tx_timeout(struct net_device *dev);
898 static void acpi_set_WOL(struct net_device *dev);
899 static struct ethtool_ops vortex_ethtool_ops;
900 static void set_8021q_mode(struct net_device *dev, int enable);
901
902 /* This driver uses 'options' to pass the media type, full-duplex flag, etc. */
903 /* Option count limit only -- unlimited interfaces are supported. */
904 #define MAX_UNITS 8
905 static int options[MAX_UNITS] = { [0 ... MAX_UNITS-1] = -1 };
906 static int full_duplex[MAX_UNITS] = {[0 ... MAX_UNITS-1] = -1 };
907 static int hw_checksums[MAX_UNITS] = {[0 ... MAX_UNITS-1] = -1 };
908 static int flow_ctrl[MAX_UNITS] = {[0 ... MAX_UNITS-1] = -1 };
909 static int enable_wol[MAX_UNITS] = {[0 ... MAX_UNITS-1] = -1 };
910 static int use_mmio[MAX_UNITS] = {[0 ... MAX_UNITS-1] = -1 };
911 static int global_options = -1;
912 static int global_full_duplex = -1;
913 static int global_enable_wol = -1;
914 static int global_use_mmio = -1;
915
916 /* Variables to work-around the Compaq PCI BIOS32 problem. */
917 static int compaq_ioaddr, compaq_irq, compaq_device_id = 0x5900;
918 static struct net_device *compaq_net_device;
919
920 static int vortex_cards_found;
921
922 module_param(debug, int, 0);
923 module_param(global_options, int, 0);
924 module_param_array(options, int, NULL, 0);
925 module_param(global_full_duplex, int, 0);
926 module_param_array(full_duplex, int, NULL, 0);
927 module_param_array(hw_checksums, int, NULL, 0);
928 module_param_array(flow_ctrl, int, NULL, 0);
929 module_param(global_enable_wol, int, 0);
930 module_param_array(enable_wol, int, NULL, 0);
931 module_param(rx_copybreak, int, 0);
932 module_param(max_interrupt_work, int, 0);
933 module_param(compaq_ioaddr, int, 0);
934 module_param(compaq_irq, int, 0);
935 module_param(compaq_device_id, int, 0);
936 module_param(watchdog, int, 0);
937 module_param(global_use_mmio, int, 0);
938 module_param_array(use_mmio, int, NULL, 0);
939 MODULE_PARM_DESC(debug, "3c59x debug level (0-6)");
940 MODULE_PARM_DESC(options, "3c59x: Bits 0-3: media type, bit 4: bus mastering, bit 9: full duplex");
941 MODULE_PARM_DESC(global_options, "3c59x: same as options, but applies to all NICs if options is unset");
942 MODULE_PARM_DESC(full_duplex, "3c59x full duplex setting(s) (1)");
943 MODULE_PARM_DESC(global_full_duplex, "3c59x: same as full_duplex, but applies to all NICs if full_duplex is unset");
944 MODULE_PARM_DESC(hw_checksums, "3c59x Hardware checksum checking by adapter(s) (0-1)");
945 MODULE_PARM_DESC(flow_ctrl, "3c59x 802.3x flow control usage (PAUSE only) (0-1)");
946 MODULE_PARM_DESC(enable_wol, "3c59x: Turn on Wake-on-LAN for adapter(s) (0-1)");
947 MODULE_PARM_DESC(global_enable_wol, "3c59x: same as enable_wol, but applies to all NICs if enable_wol is unset");
948 MODULE_PARM_DESC(rx_copybreak, "3c59x copy breakpoint for copy-only-tiny-frames");
949 MODULE_PARM_DESC(max_interrupt_work, "3c59x maximum events handled per interrupt");
950 MODULE_PARM_DESC(compaq_ioaddr, "3c59x PCI I/O base address (Compaq BIOS problem workaround)");
951 MODULE_PARM_DESC(compaq_irq, "3c59x PCI IRQ number (Compaq BIOS problem workaround)");
952 MODULE_PARM_DESC(compaq_device_id, "3c59x PCI device ID (Compaq BIOS problem workaround)");
953 MODULE_PARM_DESC(watchdog, "3c59x transmit timeout in milliseconds");
954 MODULE_PARM_DESC(global_use_mmio, "3c59x: same as use_mmio, but applies to all NICs if options is unset");
955 MODULE_PARM_DESC(use_mmio, "3c59x: use memory-mapped PCI I/O resource (0-1)");
956
957 #ifdef CONFIG_NET_POLL_CONTROLLER
958 static void poll_vortex(struct net_device *dev)
959 {
960         struct vortex_private *vp = netdev_priv(dev);
961         unsigned long flags;
962         local_save_flags(flags);
963         local_irq_disable();
964         (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev,NULL);
965         local_irq_restore(flags);
966
967 #endif
968
969 #ifdef CONFIG_PM
970
971 static int vortex_suspend(struct pci_dev *pdev, pm_message_t state)
972 {
973         struct net_device *dev = pci_get_drvdata(pdev);
974
975         if (dev && dev->priv) {
976                 if (netif_running(dev)) {
977                         netif_device_detach(dev);
978                         vortex_down(dev, 1);
979                 }
980                 pci_save_state(pdev);
981                 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
982                 free_irq(dev->irq, dev);
983                 pci_disable_device(pdev);
984                 pci_set_power_state(pdev, pci_choose_state(pdev, state));
985         }
986         return 0;
987 }
988
989 static int vortex_resume(struct pci_dev *pdev)
990 {
991         struct net_device *dev = pci_get_drvdata(pdev);
992         struct vortex_private *vp = netdev_priv(dev);
993
994         if (dev && vp) {
995                 pci_set_power_state(pdev, PCI_D0);
996                 pci_restore_state(pdev);
997                 pci_enable_device(pdev);
998                 pci_set_master(pdev);
999                 if (request_irq(dev->irq, vp->full_bus_master_rx ?
1000                                 &boomerang_interrupt : &vortex_interrupt, SA_SHIRQ, dev->name, dev)) {
1001                         printk(KERN_WARNING "%s: Could not reserve IRQ %d\n", dev->name, dev->irq);
1002                         pci_disable_device(pdev);
1003                         return -EBUSY;
1004                 }
1005                 if (netif_running(dev)) {
1006                         vortex_up(dev);
1007                         netif_device_attach(dev);
1008                 }
1009         }
1010         return 0;
1011 }
1012
1013 #endif /* CONFIG_PM */
1014
1015 #ifdef CONFIG_EISA
1016 static struct eisa_device_id vortex_eisa_ids[] = {
1017         { "TCM5920", CH_3C592 },
1018         { "TCM5970", CH_3C597 },
1019         { "" }
1020 };
1021
1022 static int vortex_eisa_probe(struct device *device);
1023 static int vortex_eisa_remove(struct device *device);
1024
1025 static struct eisa_driver vortex_eisa_driver = {
1026         .id_table = vortex_eisa_ids,
1027         .driver   = {
1028                 .name    = "3c59x",
1029                 .probe   = vortex_eisa_probe,
1030                 .remove  = vortex_eisa_remove
1031         }
1032 };
1033
1034 static int vortex_eisa_probe(struct device *device)
1035 {
1036         void __iomem *ioaddr;
1037         struct eisa_device *edev;
1038
1039         edev = to_eisa_device(device);
1040
1041         if (!request_region(edev->base_addr, VORTEX_TOTAL_SIZE, DRV_NAME))
1042                 return -EBUSY;
1043
1044         ioaddr = ioport_map(edev->base_addr, VORTEX_TOTAL_SIZE);
1045
1046         if (vortex_probe1(device, ioaddr, ioread16(ioaddr + 0xC88) >> 12,
1047                                           edev->id.driver_data, vortex_cards_found)) {
1048                 release_region(edev->base_addr, VORTEX_TOTAL_SIZE);
1049                 return -ENODEV;
1050         }
1051
1052         vortex_cards_found++;
1053
1054         return 0;
1055 }
1056
1057 static int vortex_eisa_remove(struct device *device)
1058 {
1059         struct eisa_device *edev;
1060         struct net_device *dev;
1061         struct vortex_private *vp;
1062         void __iomem *ioaddr;
1063
1064         edev = to_eisa_device(device);
1065         dev = eisa_get_drvdata(edev);
1066
1067         if (!dev) {
1068                 printk("vortex_eisa_remove called for Compaq device!\n");
1069                 BUG();
1070         }
1071
1072         vp = netdev_priv(dev);
1073         ioaddr = vp->ioaddr;
1074         
1075         unregister_netdev(dev);
1076         iowrite16(TotalReset|0x14, ioaddr + EL3_CMD);
1077         release_region(dev->base_addr, VORTEX_TOTAL_SIZE);
1078
1079         free_netdev(dev);
1080         return 0;
1081 }
1082 #endif
1083
1084 /* returns count found (>= 0), or negative on error */
1085 static int __init vortex_eisa_init(void)
1086 {
1087         int eisa_found = 0;
1088         int orig_cards_found = vortex_cards_found;
1089
1090 #ifdef CONFIG_EISA
1091         int err;
1092
1093         err = eisa_driver_register (&vortex_eisa_driver);
1094         if (!err) {
1095                 /*
1096                  * Because of the way EISA bus is probed, we cannot assume
1097                  * any device have been found when we exit from
1098                  * eisa_driver_register (the bus root driver may not be
1099                  * initialized yet). So we blindly assume something was
1100                  * found, and let the sysfs magic happend...
1101                  */
1102                 eisa_found = 1;
1103         }
1104 #endif
1105         
1106         /* Special code to work-around the Compaq PCI BIOS32 problem. */
1107         if (compaq_ioaddr) {
1108                 vortex_probe1(NULL, ioport_map(compaq_ioaddr, VORTEX_TOTAL_SIZE),
1109                               compaq_irq, compaq_device_id, vortex_cards_found++);
1110         }
1111
1112         return vortex_cards_found - orig_cards_found + eisa_found;
1113 }
1114
1115 /* returns count (>= 0), or negative on error */
1116 static int __devinit vortex_init_one(struct pci_dev *pdev,
1117                                       const struct pci_device_id *ent)
1118 {
1119         int rc, unit, pci_bar;
1120         struct vortex_chip_info *vci;
1121         void __iomem *ioaddr;
1122
1123         /* wake up and enable device */         
1124         rc = pci_enable_device(pdev);
1125         if (rc < 0)
1126                 goto out;
1127
1128         unit = vortex_cards_found;
1129
1130         if (global_use_mmio < 0 && (unit >= MAX_UNITS || use_mmio[unit] < 0)) {
1131                 /* Determine the default if the user didn't override us */
1132                 vci = &vortex_info_tbl[ent->driver_data];
1133                 pci_bar = vci->drv_flags & (IS_CYCLONE | IS_TORNADO) ? 1 : 0;
1134         } else if (unit < MAX_UNITS && use_mmio[unit] >= 0)
1135                 pci_bar = use_mmio[unit] ? 1 : 0;
1136         else
1137                 pci_bar = global_use_mmio ? 1 : 0;
1138
1139         ioaddr = pci_iomap(pdev, pci_bar, 0);
1140         if (!ioaddr) /* If mapping fails, fall-back to BAR 0... */
1141                 ioaddr = pci_iomap(pdev, 0, 0);
1142
1143         rc = vortex_probe1(&pdev->dev, ioaddr, pdev->irq,
1144                            ent->driver_data, unit);
1145         if (rc < 0) {
1146                 pci_disable_device(pdev);
1147                 goto out;
1148         }
1149
1150         vortex_cards_found++;
1151
1152 out:
1153         return rc;
1154 }
1155
1156 /*
1157  * Start up the PCI/EISA device which is described by *gendev.
1158  * Return 0 on success.
1159  *
1160  * NOTE: pdev can be NULL, for the case of a Compaq device
1161  */
1162 static int __devinit vortex_probe1(struct device *gendev,
1163                                    void __iomem *ioaddr, int irq,
1164                                    int chip_idx, int card_idx)
1165 {
1166         struct vortex_private *vp;
1167         int option;
1168         unsigned int eeprom[0x40], checksum = 0;                /* EEPROM contents */
1169         int i, step;
1170         struct net_device *dev;
1171         static int printed_version;
1172         int retval, print_info;
1173         struct vortex_chip_info * const vci = &vortex_info_tbl[chip_idx];
1174         char *print_name = "3c59x";
1175         struct pci_dev *pdev = NULL;
1176         struct eisa_device *edev = NULL;
1177
1178         if (!printed_version) {
1179                 printk (version);
1180                 printed_version = 1;
1181         }
1182
1183         if (gendev) {
1184                 if ((pdev = DEVICE_PCI(gendev))) {
1185                         print_name = pci_name(pdev);
1186                 }
1187
1188                 if ((edev = DEVICE_EISA(gendev))) {
1189                         print_name = edev->dev.bus_id;
1190                 }
1191         }
1192
1193         dev = alloc_etherdev(sizeof(*vp));
1194         retval = -ENOMEM;
1195         if (!dev) {
1196                 printk (KERN_ERR PFX "unable to allocate etherdev, aborting\n");
1197                 goto out;
1198         }
1199         SET_MODULE_OWNER(dev);
1200         SET_NETDEV_DEV(dev, gendev);
1201         vp = netdev_priv(dev);
1202
1203         option = global_options;
1204
1205         /* The lower four bits are the media type. */
1206         if (dev->mem_start) {
1207                 /*
1208                  * The 'options' param is passed in as the third arg to the
1209                  * LILO 'ether=' argument for non-modular use
1210                  */
1211                 option = dev->mem_start;
1212         }
1213         else if (card_idx < MAX_UNITS) {
1214                 if (options[card_idx] >= 0)
1215                         option = options[card_idx];
1216         }
1217
1218         if (option > 0) {
1219                 if (option & 0x8000)
1220                         vortex_debug = 7;
1221                 if (option & 0x4000)
1222                         vortex_debug = 2;
1223                 if (option & 0x0400)
1224                         vp->enable_wol = 1;
1225         }
1226
1227         print_info = (vortex_debug > 1);
1228         if (print_info)
1229                 printk (KERN_INFO "See Documentation/networking/vortex.txt\n");
1230
1231         printk(KERN_INFO "%s: 3Com %s %s at %p.\n",
1232                print_name,
1233                pdev ? "PCI" : "EISA",
1234                vci->name,
1235                ioaddr);
1236
1237         dev->base_addr = (unsigned long)ioaddr;
1238         dev->irq = irq;
1239         dev->mtu = mtu;
1240         vp->ioaddr = ioaddr;
1241         vp->large_frames = mtu > 1500;
1242         vp->drv_flags = vci->drv_flags;
1243         vp->has_nway = (vci->drv_flags & HAS_NWAY) ? 1 : 0;
1244         vp->io_size = vci->io_size;
1245         vp->card_idx = card_idx;
1246
1247         /* module list only for Compaq device */
1248         if (gendev == NULL) {
1249                 compaq_net_device = dev;
1250         }
1251
1252         /* PCI-only startup logic */
1253         if (pdev) {
1254                 /* EISA resources already marked, so only PCI needs to do this here */
1255                 /* Ignore return value, because Cardbus drivers already allocate for us */
1256                 if (request_region(dev->base_addr, vci->io_size, print_name) != NULL)
1257                         vp->must_free_region = 1;
1258
1259                 /* enable bus-mastering if necessary */         
1260                 if (vci->flags & PCI_USES_MASTER)
1261                         pci_set_master(pdev);
1262
1263                 if (vci->drv_flags & IS_VORTEX) {
1264                         u8 pci_latency;
1265                         u8 new_latency = 248;
1266
1267                         /* Check the PCI latency value.  On the 3c590 series the latency timer
1268                            must be set to the maximum value to avoid data corruption that occurs
1269                            when the timer expires during a transfer.  This bug exists the Vortex
1270                            chip only. */
1271                         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &pci_latency);
1272                         if (pci_latency < new_latency) {
1273                                 printk(KERN_INFO "%s: Overriding PCI latency"
1274                                         " timer (CFLT) setting of %d, new value is %d.\n",
1275                                         print_name, pci_latency, new_latency);
1276                                         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, new_latency);
1277                         }
1278                 }
1279         }
1280
1281         spin_lock_init(&vp->lock);
1282         vp->gendev = gendev;
1283         vp->mii.dev = dev;
1284         vp->mii.mdio_read = mdio_read;
1285         vp->mii.mdio_write = mdio_write;
1286         vp->mii.phy_id_mask = 0x1f;
1287         vp->mii.reg_num_mask = 0x1f;
1288
1289         /* Makes sure rings are at least 16 byte aligned. */
1290         vp->rx_ring = pci_alloc_consistent(pdev, sizeof(struct boom_rx_desc) * RX_RING_SIZE
1291                                            + sizeof(struct boom_tx_desc) * TX_RING_SIZE,
1292                                            &vp->rx_ring_dma);
1293         retval = -ENOMEM;
1294         if (vp->rx_ring == 0)
1295                 goto free_region;
1296
1297         vp->tx_ring = (struct boom_tx_desc *)(vp->rx_ring + RX_RING_SIZE);
1298         vp->tx_ring_dma = vp->rx_ring_dma + sizeof(struct boom_rx_desc) * RX_RING_SIZE;
1299
1300         /* if we are a PCI driver, we store info in pdev->driver_data
1301          * instead of a module list */  
1302         if (pdev)
1303                 pci_set_drvdata(pdev, dev);
1304         if (edev)
1305                 eisa_set_drvdata(edev, dev);
1306
1307         vp->media_override = 7;
1308         if (option >= 0) {
1309                 vp->media_override = ((option & 7) == 2)  ?  0  :  option & 15;
1310                 if (vp->media_override != 7)
1311                         vp->medialock = 1;
1312                 vp->full_duplex = (option & 0x200) ? 1 : 0;
1313                 vp->bus_master = (option & 16) ? 1 : 0;
1314         }
1315
1316         if (global_full_duplex > 0)
1317                 vp->full_duplex = 1;
1318         if (global_enable_wol > 0)
1319                 vp->enable_wol = 1;
1320
1321         if (card_idx < MAX_UNITS) {
1322                 if (full_duplex[card_idx] > 0)
1323                         vp->full_duplex = 1;
1324                 if (flow_ctrl[card_idx] > 0)
1325                         vp->flow_ctrl = 1;
1326                 if (enable_wol[card_idx] > 0)
1327                         vp->enable_wol = 1;
1328         }
1329
1330         vp->mii.force_media = vp->full_duplex;
1331         vp->options = option;
1332         /* Read the station address from the EEPROM. */
1333         EL3WINDOW(0);
1334         {
1335                 int base;
1336
1337                 if (vci->drv_flags & EEPROM_8BIT)
1338                         base = 0x230;
1339                 else if (vci->drv_flags & EEPROM_OFFSET)
1340                         base = EEPROM_Read + 0x30;
1341                 else
1342                         base = EEPROM_Read;
1343
1344                 for (i = 0; i < 0x40; i++) {
1345                         int timer;
1346                         iowrite16(base + i, ioaddr + Wn0EepromCmd);
1347                         /* Pause for at least 162 us. for the read to take place. */
1348                         for (timer = 10; timer >= 0; timer--) {
1349                                 udelay(162);
1350                                 if ((ioread16(ioaddr + Wn0EepromCmd) & 0x8000) == 0)
1351                                         break;
1352                         }
1353                         eeprom[i] = ioread16(ioaddr + Wn0EepromData);
1354                 }
1355         }
1356         for (i = 0; i < 0x18; i++)
1357                 checksum ^= eeprom[i];
1358         checksum = (checksum ^ (checksum >> 8)) & 0xff;
1359         if (checksum != 0x00) {         /* Grrr, needless incompatible change 3Com. */
1360                 while (i < 0x21)
1361                         checksum ^= eeprom[i++];
1362                 checksum = (checksum ^ (checksum >> 8)) & 0xff;
1363         }
1364         if ((checksum != 0x00) && !(vci->drv_flags & IS_TORNADO))
1365                 printk(" ***INVALID CHECKSUM %4.4x*** ", checksum);
1366         for (i = 0; i < 3; i++)
1367                 ((u16 *)dev->dev_addr)[i] = htons(eeprom[i + 10]);
1368         memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
1369         if (print_info) {
1370                 for (i = 0; i < 6; i++)
1371                         printk("%c%2.2x", i ? ':' : ' ', dev->dev_addr[i]);
1372         }
1373         /* Unfortunately an all zero eeprom passes the checksum and this
1374            gets found in the wild in failure cases. Crypto is hard 8) */
1375         if (!is_valid_ether_addr(dev->dev_addr)) {
1376                 retval = -EINVAL;
1377                 printk(KERN_ERR "*** EEPROM MAC address is invalid.\n");
1378                 goto free_ring; /* With every pack */
1379         }
1380         EL3WINDOW(2);
1381         for (i = 0; i < 6; i++)
1382                 iowrite8(dev->dev_addr[i], ioaddr + i);
1383
1384         if (print_info)
1385                 printk(", IRQ %d\n", dev->irq);
1386         /* Tell them about an invalid IRQ. */
1387         if (dev->irq <= 0 || dev->irq >= NR_IRQS)
1388                 printk(KERN_WARNING " *** Warning: IRQ %d is unlikely to work! ***\n",
1389                            dev->irq);
1390
1391         EL3WINDOW(4);
1392         step = (ioread8(ioaddr + Wn4_NetDiag) & 0x1e) >> 1;
1393         if (print_info) {
1394                 printk(KERN_INFO "  product code %02x%02x rev %02x.%d date %02d-"
1395                         "%02d-%02d\n", eeprom[6]&0xff, eeprom[6]>>8, eeprom[0x14],
1396                         step, (eeprom[4]>>5) & 15, eeprom[4] & 31, eeprom[4]>>9);
1397         }
1398
1399
1400         if (pdev && vci->drv_flags & HAS_CB_FNS) {
1401                 unsigned short n;
1402
1403                 vp->cb_fn_base = pci_iomap(pdev, 2, 0);
1404                 if (!vp->cb_fn_base) {
1405                         retval = -ENOMEM;
1406                         goto free_ring;
1407                 }
1408
1409                 if (print_info) {
1410                         printk(KERN_INFO "%s: CardBus functions mapped "
1411                                 "%16.16llx->%p\n",
1412                                 print_name,
1413                                 (unsigned long long)pci_resource_start(pdev, 2),
1414                                 vp->cb_fn_base);
1415                 }
1416                 EL3WINDOW(2);
1417
1418                 n = ioread16(ioaddr + Wn2_ResetOptions) & ~0x4010;
1419                 if (vp->drv_flags & INVERT_LED_PWR)
1420                         n |= 0x10;
1421                 if (vp->drv_flags & INVERT_MII_PWR)
1422                         n |= 0x4000;
1423                 iowrite16(n, ioaddr + Wn2_ResetOptions);
1424                 if (vp->drv_flags & WNO_XCVR_PWR) {
1425                         EL3WINDOW(0);
1426                         iowrite16(0x0800, ioaddr);
1427                 }
1428         }
1429
1430         /* Extract our information from the EEPROM data. */
1431         vp->info1 = eeprom[13];
1432         vp->info2 = eeprom[15];
1433         vp->capabilities = eeprom[16];
1434
1435         if (vp->info1 & 0x8000) {
1436                 vp->full_duplex = 1;
1437                 if (print_info)
1438                         printk(KERN_INFO "Full duplex capable\n");
1439         }
1440
1441         {
1442                 static const char * const ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
1443                 unsigned int config;
1444                 EL3WINDOW(3);
1445                 vp->available_media = ioread16(ioaddr + Wn3_Options);
1446                 if ((vp->available_media & 0xff) == 0)          /* Broken 3c916 */
1447                         vp->available_media = 0x40;
1448                 config = ioread32(ioaddr + Wn3_Config);
1449                 if (print_info) {
1450                         printk(KERN_DEBUG "  Internal config register is %4.4x, "
1451                                    "transceivers %#x.\n", config, ioread16(ioaddr + Wn3_Options));
1452                         printk(KERN_INFO "  %dK %s-wide RAM %s Rx:Tx split, %s%s interface.\n",
1453                                    8 << RAM_SIZE(config),
1454                                    RAM_WIDTH(config) ? "word" : "byte",
1455                                    ram_split[RAM_SPLIT(config)],
1456                                    AUTOSELECT(config) ? "autoselect/" : "",
1457                                    XCVR(config) > XCVR_ExtMII ? "<invalid transceiver>" :
1458                                    media_tbl[XCVR(config)].name);
1459                 }
1460                 vp->default_media = XCVR(config);
1461                 if (vp->default_media == XCVR_NWAY)
1462                         vp->has_nway = 1;
1463                 vp->autoselect = AUTOSELECT(config);
1464         }
1465
1466         if (vp->media_override != 7) {
1467                 printk(KERN_INFO "%s:  Media override to transceiver type %d (%s).\n",
1468                                 print_name, vp->media_override,
1469                                 media_tbl[vp->media_override].name);
1470                 dev->if_port = vp->media_override;
1471         } else
1472                 dev->if_port = vp->default_media;
1473
1474         if ((vp->available_media & 0x40) || (vci->drv_flags & HAS_NWAY) ||
1475                 dev->if_port == XCVR_MII || dev->if_port == XCVR_NWAY) {
1476                 int phy, phy_idx = 0;
1477                 EL3WINDOW(4);
1478                 mii_preamble_required++;
1479                 if (vp->drv_flags & EXTRA_PREAMBLE)
1480                         mii_preamble_required++;
1481                 mdio_sync(ioaddr, 32);
1482                 mdio_read(dev, 24, MII_BMSR);
1483                 for (phy = 0; phy < 32 && phy_idx < 1; phy++) {
1484                         int mii_status, phyx;
1485
1486                         /*
1487                          * For the 3c905CX we look at index 24 first, because it bogusly
1488                          * reports an external PHY at all indices
1489                          */
1490                         if (phy == 0)
1491                                 phyx = 24;
1492                         else if (phy <= 24)
1493                                 phyx = phy - 1;
1494                         else
1495                                 phyx = phy;
1496                         mii_status = mdio_read(dev, phyx, MII_BMSR);
1497                         if (mii_status  &&  mii_status != 0xffff) {
1498                                 vp->phys[phy_idx++] = phyx;
1499                                 if (print_info) {
1500                                         printk(KERN_INFO "  MII transceiver found at address %d,"
1501                                                 " status %4x.\n", phyx, mii_status);
1502                                 }
1503                                 if ((mii_status & 0x0040) == 0)
1504                                         mii_preamble_required++;
1505                         }
1506                 }
1507                 mii_preamble_required--;
1508                 if (phy_idx == 0) {
1509                         printk(KERN_WARNING"  ***WARNING*** No MII transceivers found!\n");
1510                         vp->phys[0] = 24;
1511                 } else {
1512                         vp->advertising = mdio_read(dev, vp->phys[0], MII_ADVERTISE);
1513                         if (vp->full_duplex) {
1514                                 /* Only advertise the FD media types. */
1515                                 vp->advertising &= ~0x02A0;
1516                                 mdio_write(dev, vp->phys[0], 4, vp->advertising);
1517                         }
1518                 }
1519                 vp->mii.phy_id = vp->phys[0];
1520         }
1521
1522         if (vp->capabilities & CapBusMaster) {
1523                 vp->full_bus_master_tx = 1;
1524                 if (print_info) {
1525                         printk(KERN_INFO "  Enabling bus-master transmits and %s receives.\n",
1526                         (vp->info2 & 1) ? "early" : "whole-frame" );
1527                 }
1528                 vp->full_bus_master_rx = (vp->info2 & 1) ? 1 : 2;
1529                 vp->bus_master = 0;             /* AKPM: vortex only */
1530         }
1531
1532         /* The 3c59x-specific entries in the device structure. */
1533         dev->open = vortex_open;
1534         if (vp->full_bus_master_tx) {
1535                 dev->hard_start_xmit = boomerang_start_xmit;
1536                 /* Actually, it still should work with iommu. */
1537                 if (card_idx < MAX_UNITS &&
1538                     ((hw_checksums[card_idx] == -1 && (vp->drv_flags & HAS_HWCKSM)) ||
1539                                 hw_checksums[card_idx] == 1)) {
1540                         dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
1541                 }
1542         } else {
1543                 dev->hard_start_xmit = vortex_start_xmit;
1544         }
1545
1546         if (print_info) {
1547                 printk(KERN_INFO "%s: scatter/gather %sabled. h/w checksums %sabled\n",
1548                                 print_name,
1549                                 (dev->features & NETIF_F_SG) ? "en":"dis",
1550                                 (dev->features & NETIF_F_IP_CSUM) ? "en":"dis");
1551         }
1552
1553         dev->stop = vortex_close;
1554         dev->get_stats = vortex_get_stats;
1555 #ifdef CONFIG_PCI
1556         dev->do_ioctl = vortex_ioctl;
1557 #endif
1558         dev->ethtool_ops = &vortex_ethtool_ops;
1559         dev->set_multicast_list = set_rx_mode;
1560         dev->tx_timeout = vortex_tx_timeout;
1561         dev->watchdog_timeo = (watchdog * HZ) / 1000;
1562 #ifdef CONFIG_NET_POLL_CONTROLLER
1563         dev->poll_controller = poll_vortex; 
1564 #endif
1565         if (pdev) {
1566                 vp->pm_state_valid = 1;
1567                 pci_save_state(VORTEX_PCI(vp));
1568                 acpi_set_WOL(dev);
1569         }
1570         retval = register_netdev(dev);
1571         if (retval == 0)
1572                 return 0;
1573
1574 free_ring:
1575         pci_free_consistent(pdev,
1576                                                 sizeof(struct boom_rx_desc) * RX_RING_SIZE
1577                                                         + sizeof(struct boom_tx_desc) * TX_RING_SIZE,
1578                                                 vp->rx_ring,
1579                                                 vp->rx_ring_dma);
1580 free_region:
1581         if (vp->must_free_region)
1582                 release_region(dev->base_addr, vci->io_size);
1583         free_netdev(dev);
1584         printk(KERN_ERR PFX "vortex_probe1 fails.  Returns %d\n", retval);
1585 out:
1586         return retval;
1587 }
1588
1589 static void
1590 issue_and_wait(struct net_device *dev, int cmd)
1591 {
1592         struct vortex_private *vp = netdev_priv(dev);
1593         void __iomem *ioaddr = vp->ioaddr;
1594         int i;
1595
1596         iowrite16(cmd, ioaddr + EL3_CMD);
1597         for (i = 0; i < 2000; i++) {
1598                 if (!(ioread16(ioaddr + EL3_STATUS) & CmdInProgress))
1599                         return;
1600         }
1601
1602         /* OK, that didn't work.  Do it the slow way.  One second */
1603         for (i = 0; i < 100000; i++) {
1604                 if (!(ioread16(ioaddr + EL3_STATUS) & CmdInProgress)) {
1605                         if (vortex_debug > 1)
1606                                 printk(KERN_INFO "%s: command 0x%04x took %d usecs\n",
1607                                            dev->name, cmd, i * 10);
1608                         return;
1609                 }
1610                 udelay(10);
1611         }
1612         printk(KERN_ERR "%s: command 0x%04x did not complete! Status=0x%x\n",
1613                            dev->name, cmd, ioread16(ioaddr + EL3_STATUS));
1614 }
1615
1616 static void
1617 vortex_set_duplex(struct net_device *dev)
1618 {
1619         struct vortex_private *vp = netdev_priv(dev);
1620         void __iomem *ioaddr = vp->ioaddr;
1621
1622         printk(KERN_INFO "%s:  setting %s-duplex.\n",
1623                 dev->name, (vp->full_duplex) ? "full" : "half");
1624
1625         EL3WINDOW(3);
1626         /* Set the full-duplex bit. */
1627         iowrite16(((vp->info1 & 0x8000) || vp->full_duplex ? 0x20 : 0) |
1628                         (vp->large_frames ? 0x40 : 0) |
1629                         ((vp->full_duplex && vp->flow_ctrl && vp->partner_flow_ctrl) ?
1630                                         0x100 : 0),
1631                         ioaddr + Wn3_MAC_Ctrl);
1632 }
1633
1634 static void vortex_check_media(struct net_device *dev, unsigned int init)
1635 {
1636         struct vortex_private *vp = netdev_priv(dev);
1637         unsigned int ok_to_print = 0;
1638
1639         if (vortex_debug > 3)
1640                 ok_to_print = 1;
1641
1642         if (mii_check_media(&vp->mii, ok_to_print, init)) {
1643                 vp->full_duplex = vp->mii.full_duplex;
1644                 vortex_set_duplex(dev);
1645         } else if (init) {
1646                 vortex_set_duplex(dev);
1647         }
1648 }
1649
1650 static void
1651 vortex_up(struct net_device *dev)
1652 {
1653         struct vortex_private *vp = netdev_priv(dev);
1654         void __iomem *ioaddr = vp->ioaddr;
1655         unsigned int config;
1656         int i, mii_reg1, mii_reg5;
1657
1658         if (VORTEX_PCI(vp)) {
1659                 pci_set_power_state(VORTEX_PCI(vp), PCI_D0);    /* Go active */
1660                 if (vp->pm_state_valid)
1661                         pci_restore_state(VORTEX_PCI(vp));
1662                 pci_enable_device(VORTEX_PCI(vp));
1663         }
1664
1665         /* Before initializing select the active media port. */
1666         EL3WINDOW(3);
1667         config = ioread32(ioaddr + Wn3_Config);
1668
1669         if (vp->media_override != 7) {
1670                 printk(KERN_INFO "%s: Media override to transceiver %d (%s).\n",
1671                            dev->name, vp->media_override,
1672                            media_tbl[vp->media_override].name);
1673                 dev->if_port = vp->media_override;
1674         } else if (vp->autoselect) {
1675                 if (vp->has_nway) {
1676                         if (vortex_debug > 1)
1677                                 printk(KERN_INFO "%s: using NWAY device table, not %d\n",
1678                                                                 dev->name, dev->if_port);
1679                         dev->if_port = XCVR_NWAY;
1680                 } else {
1681                         /* Find first available media type, starting with 100baseTx. */
1682                         dev->if_port = XCVR_100baseTx;
1683                         while (! (vp->available_media & media_tbl[dev->if_port].mask))
1684                                 dev->if_port = media_tbl[dev->if_port].next;
1685                         if (vortex_debug > 1)
1686                                 printk(KERN_INFO "%s: first available media type: %s\n",
1687                                         dev->name, media_tbl[dev->if_port].name);
1688                 }
1689         } else {
1690                 dev->if_port = vp->default_media;
1691                 if (vortex_debug > 1)
1692                         printk(KERN_INFO "%s: using default media %s\n",
1693                                 dev->name, media_tbl[dev->if_port].name);
1694         }
1695
1696         init_timer(&vp->timer);
1697         vp->timer.expires = RUN_AT(media_tbl[dev->if_port].wait);
1698         vp->timer.data = (unsigned long)dev;
1699         vp->timer.function = vortex_timer;              /* timer handler */
1700         add_timer(&vp->timer);
1701
1702         init_timer(&vp->rx_oom_timer);
1703         vp->rx_oom_timer.data = (unsigned long)dev;
1704         vp->rx_oom_timer.function = rx_oom_timer;
1705
1706         if (vortex_debug > 1)
1707                 printk(KERN_DEBUG "%s: Initial media type %s.\n",
1708                            dev->name, media_tbl[dev->if_port].name);
1709
1710         vp->full_duplex = vp->mii.force_media;
1711         config = BFINS(config, dev->if_port, 20, 4);
1712         if (vortex_debug > 6)
1713                 printk(KERN_DEBUG "vortex_up(): writing 0x%x to InternalConfig\n", config);
1714         iowrite32(config, ioaddr + Wn3_Config);
1715
1716         if (dev->if_port == XCVR_MII || dev->if_port == XCVR_NWAY) {
1717                 EL3WINDOW(4);
1718                 mii_reg1 = mdio_read(dev, vp->phys[0], MII_BMSR);
1719                 mii_reg5 = mdio_read(dev, vp->phys[0], MII_LPA);
1720                 vp->partner_flow_ctrl = ((mii_reg5 & 0x0400) != 0);
1721
1722                 vortex_check_media(dev, 1);
1723         }
1724         else
1725                 vortex_set_duplex(dev);
1726
1727         issue_and_wait(dev, TxReset);
1728         /*
1729          * Don't reset the PHY - that upsets autonegotiation during DHCP operations.
1730          */
1731         issue_and_wait(dev, RxReset|0x04);
1732
1733
1734         iowrite16(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
1735
1736         if (vortex_debug > 1) {
1737                 EL3WINDOW(4);
1738                 printk(KERN_DEBUG "%s: vortex_up() irq %d media status %4.4x.\n",
1739                            dev->name, dev->irq, ioread16(ioaddr + Wn4_Media));
1740         }
1741
1742         /* Set the station address and mask in window 2 each time opened. */
1743         EL3WINDOW(2);
1744         for (i = 0; i < 6; i++)
1745                 iowrite8(dev->dev_addr[i], ioaddr + i);
1746         for (; i < 12; i+=2)
1747                 iowrite16(0, ioaddr + i);
1748
1749         if (vp->cb_fn_base) {
1750                 unsigned short n = ioread16(ioaddr + Wn2_ResetOptions) & ~0x4010;
1751                 if (vp->drv_flags & INVERT_LED_PWR)
1752                         n |= 0x10;
1753                 if (vp->drv_flags & INVERT_MII_PWR)
1754                         n |= 0x4000;
1755                 iowrite16(n, ioaddr + Wn2_ResetOptions);
1756         }
1757
1758         if (dev->if_port == XCVR_10base2)
1759                 /* Start the thinnet transceiver. We should really wait 50ms...*/
1760                 iowrite16(StartCoax, ioaddr + EL3_CMD);
1761         if (dev->if_port != XCVR_NWAY) {
1762                 EL3WINDOW(4);
1763                 iowrite16((ioread16(ioaddr + Wn4_Media) & ~(Media_10TP|Media_SQE)) |
1764                          media_tbl[dev->if_port].media_bits, ioaddr + Wn4_Media);
1765         }
1766
1767         /* Switch to the stats window, and clear all stats by reading. */
1768         iowrite16(StatsDisable, ioaddr + EL3_CMD);
1769         EL3WINDOW(6);
1770         for (i = 0; i < 10; i++)
1771                 ioread8(ioaddr + i);
1772         ioread16(ioaddr + 10);
1773         ioread16(ioaddr + 12);
1774         /* New: On the Vortex we must also clear the BadSSD counter. */
1775         EL3WINDOW(4);
1776         ioread8(ioaddr + 12);
1777         /* ..and on the Boomerang we enable the extra statistics bits. */
1778         iowrite16(0x0040, ioaddr + Wn4_NetDiag);
1779
1780         /* Switch to register set 7 for normal use. */
1781         EL3WINDOW(7);
1782
1783         if (vp->full_bus_master_rx) { /* Boomerang bus master. */
1784                 vp->cur_rx = vp->dirty_rx = 0;
1785                 /* Initialize the RxEarly register as recommended. */
1786                 iowrite16(SetRxThreshold + (1536>>2), ioaddr + EL3_CMD);
1787                 iowrite32(0x0020, ioaddr + PktStatus);
1788                 iowrite32(vp->rx_ring_dma, ioaddr + UpListPtr);
1789         }
1790         if (vp->full_bus_master_tx) {           /* Boomerang bus master Tx. */
1791                 vp->cur_tx = vp->dirty_tx = 0;
1792                 if (vp->drv_flags & IS_BOOMERANG)
1793                         iowrite8(PKT_BUF_SZ>>8, ioaddr + TxFreeThreshold); /* Room for a packet. */
1794                 /* Clear the Rx, Tx rings. */
1795                 for (i = 0; i < RX_RING_SIZE; i++)      /* AKPM: this is done in vortex_open, too */
1796                         vp->rx_ring[i].status = 0;
1797                 for (i = 0; i < TX_RING_SIZE; i++)
1798                         vp->tx_skbuff[i] = NULL;
1799                 iowrite32(0, ioaddr + DownListPtr);
1800         }
1801         /* Set receiver mode: presumably accept b-case and phys addr only. */
1802         set_rx_mode(dev);
1803         /* enable 802.1q tagged frames */
1804         set_8021q_mode(dev, 1);
1805         iowrite16(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
1806
1807         iowrite16(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
1808         iowrite16(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
1809         /* Allow status bits to be seen. */
1810         vp->status_enable = SetStatusEnb | HostError|IntReq|StatsFull|TxComplete|
1811                 (vp->full_bus_master_tx ? DownComplete : TxAvailable) |
1812                 (vp->full_bus_master_rx ? UpComplete : RxComplete) |
1813                 (vp->bus_master ? DMADone : 0);
1814         vp->intr_enable = SetIntrEnb | IntLatch | TxAvailable |
1815                 (vp->full_bus_master_rx ? 0 : RxComplete) |
1816                 StatsFull | HostError | TxComplete | IntReq
1817                 | (vp->bus_master ? DMADone : 0) | UpComplete | DownComplete;
1818         iowrite16(vp->status_enable, ioaddr + EL3_CMD);
1819         /* Ack all pending events, and set active indicator mask. */
1820         iowrite16(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
1821                  ioaddr + EL3_CMD);
1822         iowrite16(vp->intr_enable, ioaddr + EL3_CMD);
1823         if (vp->cb_fn_base)                     /* The PCMCIA people are idiots.  */
1824                 iowrite32(0x8000, vp->cb_fn_base + 4);
1825         netif_start_queue (dev);
1826 }
1827
1828 static int
1829 vortex_open(struct net_device *dev)
1830 {
1831         struct vortex_private *vp = netdev_priv(dev);
1832         int i;
1833         int retval;
1834
1835         /* Use the now-standard shared IRQ implementation. */
1836         if ((retval = request_irq(dev->irq, vp->full_bus_master_rx ?
1837                                 &boomerang_interrupt : &vortex_interrupt, SA_SHIRQ, dev->name, dev))) {
1838                 printk(KERN_ERR "%s: Could not reserve IRQ %d\n", dev->name, dev->irq);
1839                 goto out;
1840         }
1841
1842         if (vp->full_bus_master_rx) { /* Boomerang bus master. */
1843                 if (vortex_debug > 2)
1844                         printk(KERN_DEBUG "%s:  Filling in the Rx ring.\n", dev->name);
1845                 for (i = 0; i < RX_RING_SIZE; i++) {
1846                         struct sk_buff *skb;
1847                         vp->rx_ring[i].next = cpu_to_le32(vp->rx_ring_dma + sizeof(struct boom_rx_desc) * (i+1));
1848                         vp->rx_ring[i].status = 0;      /* Clear complete bit. */
1849                         vp->rx_ring[i].length = cpu_to_le32(PKT_BUF_SZ | LAST_FRAG);
1850                         skb = dev_alloc_skb(PKT_BUF_SZ);
1851                         vp->rx_skbuff[i] = skb;
1852                         if (skb == NULL)
1853                                 break;                  /* Bad news!  */
1854                         skb->dev = dev;                 /* Mark as being used by this device. */
1855                         skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
1856                         vp->rx_ring[i].addr = cpu_to_le32(pci_map_single(VORTEX_PCI(vp), skb->data, PKT_BUF_SZ, PCI_DMA_FROMDEVICE));
1857                 }
1858                 if (i != RX_RING_SIZE) {
1859                         int j;
1860                         printk(KERN_EMERG "%s: no memory for rx ring\n", dev->name);
1861                         for (j = 0; j < i; j++) {
1862                                 if (vp->rx_skbuff[j]) {
1863                                         dev_kfree_skb(vp->rx_skbuff[j]);
1864                                         vp->rx_skbuff[j] = NULL;
1865                                 }
1866                         }
1867                         retval = -ENOMEM;
1868                         goto out_free_irq;
1869                 }
1870                 /* Wrap the ring. */
1871                 vp->rx_ring[i-1].next = cpu_to_le32(vp->rx_ring_dma);
1872         }
1873
1874         vortex_up(dev);
1875         return 0;
1876
1877 out_free_irq:
1878         free_irq(dev->irq, dev);
1879 out:
1880         if (vortex_debug > 1)
1881                 printk(KERN_ERR "%s: vortex_open() fails: returning %d\n", dev->name, retval);
1882         return retval;
1883 }
1884
1885 static void
1886 vortex_timer(unsigned long data)
1887 {
1888         struct net_device *dev = (struct net_device *)data;
1889         struct vortex_private *vp = netdev_priv(dev);
1890         void __iomem *ioaddr = vp->ioaddr;
1891         int next_tick = 60*HZ;
1892         int ok = 0;
1893         int media_status, old_window;
1894
1895         if (vortex_debug > 2) {
1896                 printk(KERN_DEBUG "%s: Media selection timer tick happened, %s.\n",
1897                            dev->name, media_tbl[dev->if_port].name);
1898                 printk(KERN_DEBUG "dev->watchdog_timeo=%d\n", dev->watchdog_timeo);
1899         }
1900
1901         disable_irq(dev->irq);
1902         old_window = ioread16(ioaddr + EL3_CMD) >> 13;
1903         EL3WINDOW(4);
1904         media_status = ioread16(ioaddr + Wn4_Media);
1905         switch (dev->if_port) {
1906         case XCVR_10baseT:  case XCVR_100baseTx:  case XCVR_100baseFx:
1907                 if (media_status & Media_LnkBeat) {
1908                         netif_carrier_on(dev);
1909                         ok = 1;
1910                         if (vortex_debug > 1)
1911                                 printk(KERN_DEBUG "%s: Media %s has link beat, %x.\n",
1912                                            dev->name, media_tbl[dev->if_port].name, media_status);
1913                 } else {
1914                         netif_carrier_off(dev);
1915                         if (vortex_debug > 1) {
1916                                 printk(KERN_DEBUG "%s: Media %s has no link beat, %x.\n",
1917                                            dev->name, media_tbl[dev->if_port].name, media_status);
1918                         }
1919                 }
1920                 break;
1921         case XCVR_MII: case XCVR_NWAY:
1922                 {
1923                         ok = 1;
1924                         spin_lock_bh(&vp->lock);
1925                         vortex_check_media(dev, 0);
1926                         spin_unlock_bh(&vp->lock);
1927                 }
1928                 break;
1929           default:                                      /* Other media types handled by Tx timeouts. */
1930                 if (vortex_debug > 1)
1931                   printk(KERN_DEBUG "%s: Media %s has no indication, %x.\n",
1932                                  dev->name, media_tbl[dev->if_port].name, media_status);
1933                 ok = 1;
1934         }
1935
1936         if (!netif_carrier_ok(dev))
1937                 next_tick = 5*HZ;
1938
1939         if (vp->medialock)
1940                 goto leave_media_alone;
1941
1942         if (!ok) {
1943                 unsigned int config;
1944
1945                 do {
1946                         dev->if_port = media_tbl[dev->if_port].next;
1947                 } while ( ! (vp->available_media & media_tbl[dev->if_port].mask));
1948                 if (dev->if_port == XCVR_Default) { /* Go back to default. */
1949                   dev->if_port = vp->default_media;
1950                   if (vortex_debug > 1)
1951                         printk(KERN_DEBUG "%s: Media selection failing, using default "
1952                                    "%s port.\n",
1953                                    dev->name, media_tbl[dev->if_port].name);
1954                 } else {
1955                         if (vortex_debug > 1)
1956                                 printk(KERN_DEBUG "%s: Media selection failed, now trying "
1957                                            "%s port.\n",
1958                                            dev->name, media_tbl[dev->if_port].name);
1959                         next_tick = media_tbl[dev->if_port].wait;
1960                 }
1961                 iowrite16((media_status & ~(Media_10TP|Media_SQE)) |
1962                          media_tbl[dev->if_port].media_bits, ioaddr + Wn4_Media);
1963
1964                 EL3WINDOW(3);
1965                 config = ioread32(ioaddr + Wn3_Config);
1966                 config = BFINS(config, dev->if_port, 20, 4);
1967                 iowrite32(config, ioaddr + Wn3_Config);
1968
1969                 iowrite16(dev->if_port == XCVR_10base2 ? StartCoax : StopCoax,
1970                          ioaddr + EL3_CMD);
1971                 if (vortex_debug > 1)
1972                         printk(KERN_DEBUG "wrote 0x%08x to Wn3_Config\n", config);
1973                 /* AKPM: FIXME: Should reset Rx & Tx here.  P60 of 3c90xc.pdf */
1974         }
1975
1976 leave_media_alone:
1977         if (vortex_debug > 2)
1978           printk(KERN_DEBUG "%s: Media selection timer finished, %s.\n",
1979                          dev->name, media_tbl[dev->if_port].name);
1980
1981         EL3WINDOW(old_window);
1982         enable_irq(dev->irq);
1983         mod_timer(&vp->timer, RUN_AT(next_tick));
1984         if (vp->deferred)
1985                 iowrite16(FakeIntr, ioaddr + EL3_CMD);
1986         return;
1987 }
1988
1989 static void vortex_tx_timeout(struct net_device *dev)
1990 {
1991         struct vortex_private *vp = netdev_priv(dev);
1992         void __iomem *ioaddr = vp->ioaddr;
1993
1994         printk(KERN_ERR "%s: transmit timed out, tx_status %2.2x status %4.4x.\n",
1995                    dev->name, ioread8(ioaddr + TxStatus),
1996                    ioread16(ioaddr + EL3_STATUS));
1997         EL3WINDOW(4);
1998         printk(KERN_ERR "  diagnostics: net %04x media %04x dma %08x fifo %04x\n",
1999                         ioread16(ioaddr + Wn4_NetDiag),
2000                         ioread16(ioaddr + Wn4_Media),
2001                         ioread32(ioaddr + PktStatus),
2002                         ioread16(ioaddr + Wn4_FIFODiag));
2003         /* Slight code bloat to be user friendly. */
2004         if ((ioread8(ioaddr + TxStatus) & 0x88) == 0x88)
2005                 printk(KERN_ERR "%s: Transmitter encountered 16 collisions --"
2006                            " network cable problem?\n", dev->name);
2007         if (ioread16(ioaddr + EL3_STATUS) & IntLatch) {
2008                 printk(KERN_ERR "%s: Interrupt posted but not delivered --"
2009                            " IRQ blocked by another device?\n", dev->name);
2010                 /* Bad idea here.. but we might as well handle a few events. */
2011                 {
2012                         /*
2013                          * Block interrupts because vortex_interrupt does a bare spin_lock()
2014                          */
2015                         unsigned long flags;
2016                         local_irq_save(flags);
2017                         if (vp->full_bus_master_tx)
2018                                 boomerang_interrupt(dev->irq, dev, NULL);
2019                         else
2020                                 vortex_interrupt(dev->irq, dev, NULL);
2021                         local_irq_restore(flags);
2022                 }
2023         }
2024
2025         if (vortex_debug > 0)
2026                 dump_tx_ring(dev);
2027
2028         issue_and_wait(dev, TxReset);
2029
2030         vp->stats.tx_errors++;
2031         if (vp->full_bus_master_tx) {
2032                 printk(KERN_DEBUG "%s: Resetting the Tx ring pointer.\n", dev->name);
2033                 if (vp->cur_tx - vp->dirty_tx > 0  &&  ioread32(ioaddr + DownListPtr) == 0)
2034                         iowrite32(vp->tx_ring_dma + (vp->dirty_tx % TX_RING_SIZE) * sizeof(struct boom_tx_desc),
2035                                  ioaddr + DownListPtr);
2036                 if (vp->cur_tx - vp->dirty_tx < TX_RING_SIZE)
2037                         netif_wake_queue (dev);
2038                 if (vp->drv_flags & IS_BOOMERANG)
2039                         iowrite8(PKT_BUF_SZ>>8, ioaddr + TxFreeThreshold);
2040                 iowrite16(DownUnstall, ioaddr + EL3_CMD);
2041         } else {
2042                 vp->stats.tx_dropped++;
2043                 netif_wake_queue(dev);
2044         }
2045         
2046         /* Issue Tx Enable */
2047         iowrite16(TxEnable, ioaddr + EL3_CMD);
2048         dev->trans_start = jiffies;
2049         
2050         /* Switch to register set 7 for normal use. */
2051         EL3WINDOW(7);
2052 }
2053
2054 /*
2055  * Handle uncommon interrupt sources.  This is a separate routine to minimize
2056  * the cache impact.
2057  */
2058 static void
2059 vortex_error(struct net_device *dev, int status)
2060 {
2061         struct vortex_private *vp = netdev_priv(dev);
2062         void __iomem *ioaddr = vp->ioaddr;
2063         int do_tx_reset = 0, reset_mask = 0;
2064         unsigned char tx_status = 0;
2065
2066         if (vortex_debug > 2) {
2067                 printk(KERN_ERR "%s: vortex_error(), status=0x%x\n", dev->name, status);
2068         }
2069
2070         if (status & TxComplete) {                      /* Really "TxError" for us. */
2071                 tx_status = ioread8(ioaddr + TxStatus);
2072                 /* Presumably a tx-timeout. We must merely re-enable. */
2073                 if (vortex_debug > 2
2074                         || (tx_status != 0x88 && vortex_debug > 0)) {
2075                         printk(KERN_ERR "%s: Transmit error, Tx status register %2.2x.\n",
2076                                    dev->name, tx_status);
2077                         if (tx_status == 0x82) {
2078                                 printk(KERN_ERR "Probably a duplex mismatch.  See "
2079                                                 "Documentation/networking/vortex.txt\n");
2080                         }
2081                         dump_tx_ring(dev);
2082                 }
2083                 if (tx_status & 0x14)  vp->stats.tx_fifo_errors++;
2084                 if (tx_status & 0x38)  vp->stats.tx_aborted_errors++;
2085                 if (tx_status & 0x08)  vp->xstats.tx_max_collisions++;
2086                 iowrite8(0, ioaddr + TxStatus);
2087                 if (tx_status & 0x30) {                 /* txJabber or txUnderrun */
2088                         do_tx_reset = 1;
2089                 } else if ((tx_status & 0x08) && (vp->drv_flags & MAX_COLLISION_RESET))  {      /* maxCollisions */
2090                         do_tx_reset = 1;
2091                         reset_mask = 0x0108;            /* Reset interface logic, but not download logic */
2092                 } else {                                /* Merely re-enable the transmitter. */
2093                         iowrite16(TxEnable, ioaddr + EL3_CMD);
2094                 }
2095         }
2096
2097         if (status & RxEarly) {                         /* Rx early is unused. */
2098                 vortex_rx(dev);
2099                 iowrite16(AckIntr | RxEarly, ioaddr + EL3_CMD);
2100         }
2101         if (status & StatsFull) {                       /* Empty statistics. */
2102                 static int DoneDidThat;
2103                 if (vortex_debug > 4)
2104                         printk(KERN_DEBUG "%s: Updating stats.\n", dev->name);
2105                 update_stats(ioaddr, dev);
2106                 /* HACK: Disable statistics as an interrupt source. */
2107                 /* This occurs when we have the wrong media type! */
2108                 if (DoneDidThat == 0  &&
2109                         ioread16(ioaddr + EL3_STATUS) & StatsFull) {
2110                         printk(KERN_WARNING "%s: Updating statistics failed, disabling "
2111                                    "stats as an interrupt source.\n", dev->name);
2112                         EL3WINDOW(5);
2113                         iowrite16(SetIntrEnb | (ioread16(ioaddr + 10) & ~StatsFull), ioaddr + EL3_CMD);
2114                         vp->intr_enable &= ~StatsFull;
2115                         EL3WINDOW(7);
2116                         DoneDidThat++;
2117                 }
2118         }
2119         if (status & IntReq) {          /* Restore all interrupt sources.  */
2120                 iowrite16(vp->status_enable, ioaddr + EL3_CMD);
2121                 iowrite16(vp->intr_enable, ioaddr + EL3_CMD);
2122         }
2123         if (status & HostError) {
2124                 u16 fifo_diag;
2125                 EL3WINDOW(4);
2126                 fifo_diag = ioread16(ioaddr + Wn4_FIFODiag);
2127                 printk(KERN_ERR "%s: Host error, FIFO diagnostic register %4.4x.\n",
2128                            dev->name, fifo_diag);
2129                 /* Adapter failure requires Tx/Rx reset and reinit. */
2130                 if (vp->full_bus_master_tx) {
2131                         int bus_status = ioread32(ioaddr + PktStatus);
2132                         /* 0x80000000 PCI master abort. */
2133                         /* 0x40000000 PCI target abort. */
2134                         if (vortex_debug)
2135                                 printk(KERN_ERR "%s: PCI bus error, bus status %8.8x\n", dev->name, bus_status);
2136
2137                         /* In this case, blow the card away */
2138                         /* Must not enter D3 or we can't legally issue the reset! */
2139                         vortex_down(dev, 0);
2140                         issue_and_wait(dev, TotalReset | 0xff);
2141                         vortex_up(dev);         /* AKPM: bug.  vortex_up() assumes that the rx ring is full. It may not be. */
2142                 } else if (fifo_diag & 0x0400)
2143                         do_tx_reset = 1;
2144                 if (fifo_diag & 0x3000) {
2145                         /* Reset Rx fifo and upload logic */
2146                         issue_and_wait(dev, RxReset|0x07);
2147                         /* Set the Rx filter to the current state. */
2148                         set_rx_mode(dev);
2149                         /* enable 802.1q VLAN tagged frames */
2150                         set_8021q_mode(dev, 1);
2151                         iowrite16(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
2152                         iowrite16(AckIntr | HostError, ioaddr + EL3_CMD);
2153                 }
2154         }
2155
2156         if (do_tx_reset) {
2157                 issue_and_wait(dev, TxReset|reset_mask);
2158                 iowrite16(TxEnable, ioaddr + EL3_CMD);
2159                 if (!vp->full_bus_master_tx)
2160                         netif_wake_queue(dev);
2161         }
2162 }
2163
2164 static int
2165 vortex_start_xmit(struct sk_buff *skb, struct net_device *dev)
2166 {
2167         struct vortex_private *vp = netdev_priv(dev);
2168         void __iomem *ioaddr = vp->ioaddr;
2169
2170         /* Put out the doubleword header... */
2171         iowrite32(skb->len, ioaddr + TX_FIFO);
2172         if (vp->bus_master) {
2173                 /* Set the bus-master controller to transfer the packet. */
2174                 int len = (skb->len + 3) & ~3;
2175                 iowrite32(vp->tx_skb_dma = pci_map_single(VORTEX_PCI(vp), skb->data, len, PCI_DMA_TODEVICE),
2176                                 ioaddr + Wn7_MasterAddr);
2177                 iowrite16(len, ioaddr + Wn7_MasterLen);
2178                 vp->tx_skb = skb;
2179                 iowrite16(StartDMADown, ioaddr + EL3_CMD);
2180                 /* netif_wake_queue() will be called at the DMADone interrupt. */
2181         } else {
2182                 /* ... and the packet rounded to a doubleword. */
2183                 iowrite32_rep(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
2184                 dev_kfree_skb (skb);
2185                 if (ioread16(ioaddr + TxFree) > 1536) {
2186                         netif_start_queue (dev);        /* AKPM: redundant? */
2187                 } else {
2188                         /* Interrupt us when the FIFO has room for max-sized packet. */
2189                         netif_stop_queue(dev);
2190                         iowrite16(SetTxThreshold + (1536>>2), ioaddr + EL3_CMD);
2191                 }
2192         }
2193
2194         dev->trans_start = jiffies;
2195
2196         /* Clear the Tx status stack. */
2197         {
2198                 int tx_status;
2199                 int i = 32;
2200
2201                 while (--i > 0  &&      (tx_status = ioread8(ioaddr + TxStatus)) > 0) {
2202                         if (tx_status & 0x3C) {         /* A Tx-disabling error occurred.  */
2203                                 if (vortex_debug > 2)
2204                                   printk(KERN_DEBUG "%s: Tx error, status %2.2x.\n",
2205                                                  dev->name, tx_status);
2206                                 if (tx_status & 0x04) vp->stats.tx_fifo_errors++;
2207                                 if (tx_status & 0x38) vp->stats.tx_aborted_errors++;
2208                                 if (tx_status & 0x30) {
2209                                         issue_and_wait(dev, TxReset);
2210                                 }
2211                                 iowrite16(TxEnable, ioaddr + EL3_CMD);
2212                         }
2213                         iowrite8(0x00, ioaddr + TxStatus); /* Pop the status stack. */
2214                 }
2215         }
2216         return 0;
2217 }
2218
2219 static int
2220 boomerang_start_xmit(struct sk_buff *skb, struct net_device *dev)
2221 {
2222         struct vortex_private *vp = netdev_priv(dev);
2223         void __iomem *ioaddr = vp->ioaddr;
2224         /* Calculate the next Tx descriptor entry. */
2225         int entry = vp->cur_tx % TX_RING_SIZE;
2226         struct boom_tx_desc *prev_entry = &vp->tx_ring[(vp->cur_tx-1) % TX_RING_SIZE];
2227         unsigned long flags;
2228
2229         if (vortex_debug > 6) {
2230                 printk(KERN_DEBUG "boomerang_start_xmit()\n");
2231                 printk(KERN_DEBUG "%s: Trying to send a packet, Tx index %d.\n",
2232                            dev->name, vp->cur_tx);
2233         }
2234
2235         if (vp->cur_tx - vp->dirty_tx >= TX_RING_SIZE) {
2236                 if (vortex_debug > 0)
2237                         printk(KERN_WARNING "%s: BUG! Tx Ring full, refusing to send buffer.\n",
2238                                    dev->name);
2239                 netif_stop_queue(dev);
2240                 return 1;
2241         }
2242
2243         vp->tx_skbuff[entry] = skb;
2244
2245         vp->tx_ring[entry].next = 0;
2246 #if DO_ZEROCOPY
2247         if (skb->ip_summed != CHECKSUM_HW)
2248                         vp->tx_ring[entry].status = cpu_to_le32(skb->len | TxIntrUploaded);
2249         else
2250                         vp->tx_ring[entry].status = cpu_to_le32(skb->len | TxIntrUploaded | AddTCPChksum | AddUDPChksum);
2251
2252         if (!skb_shinfo(skb)->nr_frags) {
2253                 vp->tx_ring[entry].frag[0].addr = cpu_to_le32(pci_map_single(VORTEX_PCI(vp), skb->data,
2254                                                                                 skb->len, PCI_DMA_TODEVICE));
2255                 vp->tx_ring[entry].frag[0].length = cpu_to_le32(skb->len | LAST_FRAG);
2256         } else {
2257                 int i;
2258
2259                 vp->tx_ring[entry].frag[0].addr = cpu_to_le32(pci_map_single(VORTEX_PCI(vp), skb->data,
2260                                                                                 skb->len-skb->data_len, PCI_DMA_TODEVICE));
2261                 vp->tx_ring[entry].frag[0].length = cpu_to_le32(skb->len-skb->data_len);
2262
2263                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2264                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2265
2266                         vp->tx_ring[entry].frag[i+1].addr =
2267                                         cpu_to_le32(pci_map_single(VORTEX_PCI(vp),
2268                                                                                            (void*)page_address(frag->page) + frag->page_offset,
2269                                                                                            frag->size, PCI_DMA_TODEVICE));
2270
2271                         if (i == skb_shinfo(skb)->nr_frags-1)
2272                                         vp->tx_ring[entry].frag[i+1].length = cpu_to_le32(frag->size|LAST_FRAG);
2273                         else
2274                                         vp->tx_ring[entry].frag[i+1].length = cpu_to_le32(frag->size);
2275                 }
2276         }
2277 #else
2278         vp->tx_ring[entry].addr = cpu_to_le32(pci_map_single(VORTEX_PCI(vp), skb->data, skb->len, PCI_DMA_TODEVICE));
2279         vp->tx_ring[entry].length = cpu_to_le32(skb->len | LAST_FRAG);
2280         vp->tx_ring[entry].status = cpu_to_le32(skb->len | TxIntrUploaded);
2281 #endif
2282
2283         spin_lock_irqsave(&vp->lock, flags);
2284         /* Wait for the stall to complete. */
2285         issue_and_wait(dev, DownStall);
2286         prev_entry->next = cpu_to_le32(vp->tx_ring_dma + entry * sizeof(struct boom_tx_desc));
2287         if (ioread32(ioaddr + DownListPtr) == 0) {
2288                 iowrite32(vp->tx_ring_dma + entry * sizeof(struct boom_tx_desc), ioaddr + DownListPtr);
2289                 vp->queued_packet++;
2290         }
2291
2292         vp->cur_tx++;
2293         if (vp->cur_tx - vp->dirty_tx > TX_RING_SIZE - 1) {
2294                 netif_stop_queue (dev);
2295         } else {                                        /* Clear previous interrupt enable. */
2296 #if defined(tx_interrupt_mitigation)
2297                 /* Dubious. If in boomeang_interrupt "faster" cyclone ifdef
2298                  * were selected, this would corrupt DN_COMPLETE. No?
2299                  */
2300                 prev_entry->status &= cpu_to_le32(~TxIntrUploaded);
2301 #endif
2302         }
2303         iowrite16(DownUnstall, ioaddr + EL3_CMD);
2304         spin_unlock_irqrestore(&vp->lock, flags);
2305         dev->trans_start = jiffies;
2306         return 0;
2307 }
2308
2309 /* The interrupt handler does all of the Rx thread work and cleans up
2310    after the Tx thread. */
2311
2312 /*
2313  * This is the ISR for the vortex series chips.
2314  * full_bus_master_tx == 0 && full_bus_master_rx == 0
2315  */
2316
2317 static irqreturn_t
2318 vortex_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2319 {
2320         struct net_device *dev = dev_id;
2321         struct vortex_private *vp = netdev_priv(dev);
2322         void __iomem *ioaddr;
2323         int status;
2324         int work_done = max_interrupt_work;
2325         int handled = 0;
2326
2327         ioaddr = vp->ioaddr;
2328         spin_lock(&vp->lock);
2329
2330         status = ioread16(ioaddr + EL3_STATUS);
2331
2332         if (vortex_debug > 6)
2333                 printk("vortex_interrupt(). status=0x%4x\n", status);
2334
2335         if ((status & IntLatch) == 0)
2336                 goto handler_exit;              /* No interrupt: shared IRQs cause this */
2337         handled = 1;
2338
2339         if (status & IntReq) {
2340                 status |= vp->deferred;
2341                 vp->deferred = 0;
2342         }
2343
2344         if (status == 0xffff)           /* h/w no longer present (hotplug)? */
2345                 goto handler_exit;
2346
2347         if (vortex_debug > 4)
2348                 printk(KERN_DEBUG "%s: interrupt, status %4.4x, latency %d ticks.\n",
2349                            dev->name, status, ioread8(ioaddr + Timer));
2350
2351         do {
2352                 if (vortex_debug > 5)
2353                                 printk(KERN_DEBUG "%s: In interrupt loop, status %4.4x.\n",
2354                                            dev->name, status);
2355                 if (status & RxComplete)
2356                         vortex_rx(dev);
2357
2358                 if (status & TxAvailable) {
2359                         if (vortex_debug > 5)
2360                                 printk(KERN_DEBUG "     TX room bit was handled.\n");
2361                         /* There's room in the FIFO for a full-sized packet. */
2362                         iowrite16(AckIntr | TxAvailable, ioaddr + EL3_CMD);
2363                         netif_wake_queue (dev);
2364                 }
2365
2366                 if (status & DMADone) {
2367                         if (ioread16(ioaddr + Wn7_MasterStatus) & 0x1000) {
2368                                 iowrite16(0x1000, ioaddr + Wn7_MasterStatus); /* Ack the event. */
2369                                 pci_unmap_single(VORTEX_PCI(vp), vp->tx_skb_dma, (vp->tx_skb->len + 3) & ~3, PCI_DMA_TODEVICE);
2370                                 dev_kfree_skb_irq(vp->tx_skb); /* Release the transferred buffer */
2371                                 if (ioread16(ioaddr + TxFree) > 1536) {
2372                                         /*
2373                                          * AKPM: FIXME: I don't think we need this.  If the queue was stopped due to
2374                                          * insufficient FIFO room, the TxAvailable test will succeed and call
2375                                          * netif_wake_queue()
2376                                          */
2377                                         netif_wake_queue(dev);
2378                                 } else { /* Interrupt when FIFO has room for max-sized packet. */
2379                                         iowrite16(SetTxThreshold + (1536>>2), ioaddr + EL3_CMD);
2380                                         netif_stop_queue(dev);
2381                                 }
2382                         }
2383                 }
2384                 /* Check for all uncommon interrupts at once. */
2385                 if (status & (HostError | RxEarly | StatsFull | TxComplete | IntReq)) {
2386                         if (status == 0xffff)
2387                                 break;
2388                         vortex_error(dev, status);
2389                 }
2390
2391                 if (--work_done < 0) {
2392                         printk(KERN_WARNING "%s: Too much work in interrupt, status "
2393                                    "%4.4x.\n", dev->name, status);
2394                         /* Disable all pending interrupts. */
2395                         do {
2396                                 vp->deferred |= status;
2397                                 iowrite16(SetStatusEnb | (~vp->deferred & vp->status_enable),
2398                                          ioaddr + EL3_CMD);
2399                                 iowrite16(AckIntr | (vp->deferred & 0x7ff), ioaddr + EL3_CMD);
2400                         } while ((status = ioread16(ioaddr + EL3_CMD)) & IntLatch);
2401                         /* The timer will reenable interrupts. */
2402                         mod_timer(&vp->timer, jiffies + 1*HZ);
2403                         break;
2404                 }
2405                 /* Acknowledge the IRQ. */
2406                 iowrite16(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
2407         } while ((status = ioread16(ioaddr + EL3_STATUS)) & (IntLatch | RxComplete));
2408
2409         if (vortex_debug > 4)
2410                 printk(KERN_DEBUG "%s: exiting interrupt, status %4.4x.\n",
2411                            dev->name, status);
2412 handler_exit:
2413         spin_unlock(&vp->lock);
2414         return IRQ_RETVAL(handled);
2415 }
2416
2417 /*
2418  * This is the ISR for the boomerang series chips.
2419  * full_bus_master_tx == 1 && full_bus_master_rx == 1
2420  */
2421
2422 static irqreturn_t
2423 boomerang_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2424 {
2425         struct net_device *dev = dev_id;
2426         struct vortex_private *vp = netdev_priv(dev);
2427         void __iomem *ioaddr;
2428         int status;
2429         int work_done = max_interrupt_work;
2430
2431         ioaddr = vp->ioaddr;
2432
2433         /*
2434          * It seems dopey to put the spinlock this early, but we could race against vortex_tx_timeout
2435          * and boomerang_start_xmit
2436          */
2437         spin_lock(&vp->lock);
2438
2439         status = ioread16(ioaddr + EL3_STATUS);
2440
2441         if (vortex_debug > 6)
2442                 printk(KERN_DEBUG "boomerang_interrupt. status=0x%4x\n", status);
2443
2444         if ((status & IntLatch) == 0)
2445                 goto handler_exit;              /* No interrupt: shared IRQs can cause this */
2446
2447         if (status == 0xffff) {         /* h/w no longer present (hotplug)? */
2448                 if (vortex_debug > 1)
2449                         printk(KERN_DEBUG "boomerang_interrupt(1): status = 0xffff\n");
2450                 goto handler_exit;
2451         }
2452
2453         if (status & IntReq) {
2454                 status |= vp->deferred;
2455                 vp->deferred = 0;
2456         }
2457
2458         if (vortex_debug > 4)
2459                 printk(KERN_DEBUG "%s: interrupt, status %4.4x, latency %d ticks.\n",
2460                            dev->name, status, ioread8(ioaddr + Timer));
2461         do {
2462                 if (vortex_debug > 5)
2463                                 printk(KERN_DEBUG "%s: In interrupt loop, status %4.4x.\n",
2464                                            dev->name, status);
2465                 if (status & UpComplete) {
2466                         iowrite16(AckIntr | UpComplete, ioaddr + EL3_CMD);
2467                         if (vortex_debug > 5)
2468                                 printk(KERN_DEBUG "boomerang_interrupt->boomerang_rx\n");
2469                         boomerang_rx(dev);
2470                 }
2471
2472                 if (status & DownComplete) {
2473                         unsigned int dirty_tx = vp->dirty_tx;
2474
2475                         iowrite16(AckIntr | DownComplete, ioaddr + EL3_CMD);
2476                         while (vp->cur_tx - dirty_tx > 0) {
2477                                 int entry = dirty_tx % TX_RING_SIZE;
2478 #if 1   /* AKPM: the latter is faster, but cyclone-only */
2479                                 if (ioread32(ioaddr + DownListPtr) ==
2480                                         vp->tx_ring_dma + entry * sizeof(struct boom_tx_desc))
2481                                         break;                  /* It still hasn't been processed. */
2482 #else
2483                                 if ((vp->tx_ring[entry].status & DN_COMPLETE) == 0)
2484                                         break;                  /* It still hasn't been processed. */
2485 #endif
2486                                         
2487                                 if (vp->tx_skbuff[entry]) {
2488                                         struct sk_buff *skb = vp->tx_skbuff[entry];
2489 #if DO_ZEROCOPY                                 
2490                                         int i;
2491                                         for (i=0; i<=skb_shinfo(skb)->nr_frags; i++)
2492                                                         pci_unmap_single(VORTEX_PCI(vp),
2493                                                                                          le32_to_cpu(vp->tx_ring[entry].frag[i].addr),
2494                                                                                          le32_to_cpu(vp->tx_ring[entry].frag[i].length)&0xFFF,
2495                                                                                          PCI_DMA_TODEVICE);
2496 #else
2497                                         pci_unmap_single(VORTEX_PCI(vp),
2498                                                 le32_to_cpu(vp->tx_ring[entry].addr), skb->len, PCI_DMA_TODEVICE);
2499 #endif
2500                                         dev_kfree_skb_irq(skb);
2501                                         vp->tx_skbuff[entry] = NULL;
2502                                 } else {
2503                                         printk(KERN_DEBUG "boomerang_interrupt: no skb!\n");
2504                                 }
2505                                 /* vp->stats.tx_packets++;  Counted below. */
2506                                 dirty_tx++;
2507                         }
2508                         vp->dirty_tx = dirty_tx;
2509                         if (vp->cur_tx - dirty_tx <= TX_RING_SIZE - 1) {
2510                                 if (vortex_debug > 6)
2511                                         printk(KERN_DEBUG "boomerang_interrupt: wake queue\n");
2512                                 netif_wake_queue (dev);
2513                         }
2514                 }
2515
2516                 /* Check for all uncommon interrupts at once. */
2517                 if (status & (HostError | RxEarly | StatsFull | TxComplete | IntReq))
2518                         vortex_error(dev, status);
2519
2520                 if (--work_done < 0) {
2521                         printk(KERN_WARNING "%s: Too much work in interrupt, status "
2522                                    "%4.4x.\n", dev->name, status);
2523                         /* Disable all pending interrupts. */
2524                         do {
2525                                 vp->deferred |= status;
2526                                 iowrite16(SetStatusEnb | (~vp->deferred & vp->status_enable),
2527                                          ioaddr + EL3_CMD);
2528                                 iowrite16(AckIntr | (vp->deferred & 0x7ff), ioaddr + EL3_CMD);
2529                         } while ((status = ioread16(ioaddr + EL3_CMD)) & IntLatch);
2530                         /* The timer will reenable interrupts. */
2531                         mod_timer(&vp->timer, jiffies + 1*HZ);
2532                         break;
2533                 }
2534                 /* Acknowledge the IRQ. */
2535                 iowrite16(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
2536                 if (vp->cb_fn_base)                     /* The PCMCIA people are idiots.  */
2537                         iowrite32(0x8000, vp->cb_fn_base + 4);
2538
2539         } while ((status = ioread16(ioaddr + EL3_STATUS)) & IntLatch);
2540
2541         if (vortex_debug > 4)
2542                 printk(KERN_DEBUG "%s: exiting interrupt, status %4.4x.\n",
2543                            dev->name, status);
2544 handler_exit:
2545         spin_unlock(&vp->lock);
2546         return IRQ_HANDLED;
2547 }
2548
2549 static int vortex_rx(struct net_device *dev)
2550 {
2551         struct vortex_private *vp = netdev_priv(dev);
2552         void __iomem *ioaddr = vp->ioaddr;
2553         int i;
2554         short rx_status;
2555
2556         if (vortex_debug > 5)
2557                 printk(KERN_DEBUG "vortex_rx(): status %4.4x, rx_status %4.4x.\n",
2558                            ioread16(ioaddr+EL3_STATUS), ioread16(ioaddr+RxStatus));
2559         while ((rx_status = ioread16(ioaddr + RxStatus)) > 0) {
2560                 if (rx_status & 0x4000) { /* Error, update stats. */
2561                         unsigned char rx_error = ioread8(ioaddr + RxErrors);
2562                         if (vortex_debug > 2)
2563                                 printk(KERN_DEBUG " Rx error: status %2.2x.\n", rx_error);
2564                         vp->stats.rx_errors++;
2565                         if (rx_error & 0x01)  vp->stats.rx_over_errors++;
2566                         if (rx_error & 0x02)  vp->stats.rx_length_errors++;
2567                         if (rx_error & 0x04)  vp->stats.rx_frame_errors++;
2568                         if (rx_error & 0x08)  vp->stats.rx_crc_errors++;
2569                         if (rx_error & 0x10)  vp->stats.rx_length_errors++;
2570                 } else {
2571                         /* The packet length: up to 4.5K!. */
2572                         int pkt_len = rx_status & 0x1fff;
2573                         struct sk_buff *skb;
2574
2575                         skb = dev_alloc_skb(pkt_len + 5);
2576                         if (vortex_debug > 4)
2577                                 printk(KERN_DEBUG "Receiving packet size %d status %4.4x.\n",
2578                                            pkt_len, rx_status);
2579                         if (skb != NULL) {
2580                                 skb->dev = dev;
2581                                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
2582                                 /* 'skb_put()' points to the start of sk_buff data area. */
2583                                 if (vp->bus_master &&
2584                                         ! (ioread16(ioaddr + Wn7_MasterStatus) & 0x8000)) {
2585                                         dma_addr_t dma = pci_map_single(VORTEX_PCI(vp), skb_put(skb, pkt_len),
2586                                                                            pkt_len, PCI_DMA_FROMDEVICE);
2587                                         iowrite32(dma, ioaddr + Wn7_MasterAddr);
2588                                         iowrite16((skb->len + 3) & ~3, ioaddr + Wn7_MasterLen);
2589                                         iowrite16(StartDMAUp, ioaddr + EL3_CMD);
2590                                         while (ioread16(ioaddr + Wn7_MasterStatus) & 0x8000)
2591                                                 ;
2592                                         pci_unmap_single(VORTEX_PCI(vp), dma, pkt_len, PCI_DMA_FROMDEVICE);
2593                                 } else {
2594                                         ioread32_rep(ioaddr + RX_FIFO,
2595                                                      skb_put(skb, pkt_len),
2596                                                      (pkt_len + 3) >> 2);
2597                                 }
2598                                 iowrite16(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
2599                                 skb->protocol = eth_type_trans(skb, dev);
2600                                 netif_rx(skb);
2601                                 dev->last_rx = jiffies;
2602                                 vp->stats.rx_packets++;
2603                                 /* Wait a limited time to go to next packet. */
2604                                 for (i = 200; i >= 0; i--)
2605                                         if ( ! (ioread16(ioaddr + EL3_STATUS) & CmdInProgress))
2606                                                 break;
2607                                 continue;
2608                         } else if (vortex_debug > 0)
2609                                 printk(KERN_NOTICE "%s: No memory to allocate a sk_buff of "
2610                                            "size %d.\n", dev->name, pkt_len);
2611                         vp->stats.rx_dropped++;
2612                 }
2613                 issue_and_wait(dev, RxDiscard);
2614         }
2615
2616         return 0;
2617 }
2618
2619 static int
2620 boomerang_rx(struct net_device *dev)
2621 {
2622         struct vortex_private *vp = netdev_priv(dev);
2623         int entry = vp->cur_rx % RX_RING_SIZE;
2624         void __iomem *ioaddr = vp->ioaddr;
2625         int rx_status;
2626         int rx_work_limit = vp->dirty_rx + RX_RING_SIZE - vp->cur_rx;
2627
2628         if (vortex_debug > 5)
2629                 printk(KERN_DEBUG "boomerang_rx(): status %4.4x\n", ioread16(ioaddr+EL3_STATUS));
2630
2631         while ((rx_status = le32_to_cpu(vp->rx_ring[entry].status)) & RxDComplete){
2632                 if (--rx_work_limit < 0)
2633                         break;
2634                 if (rx_status & RxDError) { /* Error, update stats. */
2635                         unsigned char rx_error = rx_status >> 16;
2636                         if (vortex_debug > 2)
2637                                 printk(KERN_DEBUG " Rx error: status %2.2x.\n", rx_error);
2638                         vp->stats.rx_errors++;
2639                         if (rx_error & 0x01)  vp->stats.rx_over_errors++;
2640                         if (rx_error & 0x02)  vp->stats.rx_length_errors++;
2641                         if (rx_error & 0x04)  vp->stats.rx_frame_errors++;
2642                         if (rx_error & 0x08)  vp->stats.rx_crc_errors++;
2643                         if (rx_error & 0x10)  vp->stats.rx_length_errors++;
2644                 } else {
2645                         /* The packet length: up to 4.5K!. */
2646                         int pkt_len = rx_status & 0x1fff;
2647                         struct sk_buff *skb;
2648                         dma_addr_t dma = le32_to_cpu(vp->rx_ring[entry].addr);
2649
2650                         if (vortex_debug > 4)
2651                                 printk(KERN_DEBUG "Receiving packet size %d status %4.4x.\n",
2652                                            pkt_len, rx_status);
2653
2654                         /* Check if the packet is long enough to just accept without
2655                            copying to a properly sized skbuff. */
2656                         if (pkt_len < rx_copybreak && (skb = dev_alloc_skb(pkt_len + 2)) != 0) {
2657                                 skb->dev = dev;
2658                                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
2659                                 pci_dma_sync_single_for_cpu(VORTEX_PCI(vp), dma, PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
2660                                 /* 'skb_put()' points to the start of sk_buff data area. */
2661                                 memcpy(skb_put(skb, pkt_len),
2662                                            vp->rx_skbuff[entry]->data,
2663                                            pkt_len);
2664                                 pci_dma_sync_single_for_device(VORTEX_PCI(vp), dma, PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
2665                                 vp->rx_copy++;
2666                         } else {
2667                                 /* Pass up the skbuff already on the Rx ring. */
2668                                 skb = vp->rx_skbuff[entry];
2669                                 vp->rx_skbuff[entry] = NULL;
2670                                 skb_put(skb, pkt_len);
2671                                 pci_unmap_single(VORTEX_PCI(vp), dma, PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
2672                                 vp->rx_nocopy++;
2673                         }
2674                         skb->protocol = eth_type_trans(skb, dev);
2675                         {                                       /* Use hardware checksum info. */
2676                                 int csum_bits = rx_status & 0xee000000;
2677                                 if (csum_bits &&
2678                                         (csum_bits == (IPChksumValid | TCPChksumValid) ||
2679                                          csum_bits == (IPChksumValid | UDPChksumValid))) {
2680                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
2681                                         vp->rx_csumhits++;
2682                                 }
2683                         }
2684                         netif_rx(skb);
2685                         dev->last_rx = jiffies;
2686                         vp->stats.rx_packets++;
2687                 }
2688                 entry = (++vp->cur_rx) % RX_RING_SIZE;
2689         }
2690         /* Refill the Rx ring buffers. */
2691         for (; vp->cur_rx - vp->dirty_rx > 0; vp->dirty_rx++) {
2692                 struct sk_buff *skb;
2693                 entry = vp->dirty_rx % RX_RING_SIZE;
2694                 if (vp->rx_skbuff[entry] == NULL) {
2695                         skb = dev_alloc_skb(PKT_BUF_SZ);
2696                         if (skb == NULL) {
2697                                 static unsigned long last_jif;
2698                                 if (time_after(jiffies, last_jif + 10 * HZ)) {
2699                                         printk(KERN_WARNING "%s: memory shortage\n", dev->name);
2700                                         last_jif = jiffies;
2701                                 }
2702                                 if ((vp->cur_rx - vp->dirty_rx) == RX_RING_SIZE)
2703                                         mod_timer(&vp->rx_oom_timer, RUN_AT(HZ * 1));
2704                                 break;                  /* Bad news!  */
2705                         }
2706                         skb->dev = dev;                 /* Mark as being used by this device. */
2707                         skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
2708                         vp->rx_ring[entry].addr = cpu_to_le32(pci_map_single(VORTEX_PCI(vp), skb->data, PKT_BUF_SZ, PCI_DMA_FROMDEVICE));
2709                         vp->rx_skbuff[entry] = skb;
2710                 }
2711                 vp->rx_ring[entry].status = 0;  /* Clear complete bit. */
2712                 iowrite16(UpUnstall, ioaddr + EL3_CMD);
2713         }
2714         return 0;
2715 }
2716
2717 /*
2718  * If we've hit a total OOM refilling the Rx ring we poll once a second
2719  * for some memory.  Otherwise there is no way to restart the rx process.
2720  */
2721 static void
2722 rx_oom_timer(unsigned long arg)
2723 {
2724         struct net_device *dev = (struct net_device *)arg;
2725         struct vortex_private *vp = netdev_priv(dev);
2726
2727         spin_lock_irq(&vp->lock);
2728         if ((vp->cur_rx - vp->dirty_rx) == RX_RING_SIZE)        /* This test is redundant, but makes me feel good */
2729                 boomerang_rx(dev);
2730         if (vortex_debug > 1) {
2731                 printk(KERN_DEBUG "%s: rx_oom_timer %s\n", dev->name,
2732                         ((vp->cur_rx - vp->dirty_rx) != RX_RING_SIZE) ? "succeeded" : "retrying");
2733         }
2734         spin_unlock_irq(&vp->lock);
2735 }
2736
2737 static void
2738 vortex_down(struct net_device *dev, int final_down)
2739 {
2740         struct vortex_private *vp = netdev_priv(dev);
2741         void __iomem *ioaddr = vp->ioaddr;
2742
2743         netif_stop_queue (dev);
2744
2745         del_timer_sync(&vp->rx_oom_timer);
2746         del_timer_sync(&vp->timer);
2747
2748         /* Turn off statistics ASAP.  We update vp->stats below. */
2749         iowrite16(StatsDisable, ioaddr + EL3_CMD);
2750
2751         /* Disable the receiver and transmitter. */
2752         iowrite16(RxDisable, ioaddr + EL3_CMD);
2753         iowrite16(TxDisable, ioaddr + EL3_CMD);
2754
2755         /* Disable receiving 802.1q tagged frames */
2756         set_8021q_mode(dev, 0);
2757
2758         if (dev->if_port == XCVR_10base2)
2759                 /* Turn off thinnet power.  Green! */
2760                 iowrite16(StopCoax, ioaddr + EL3_CMD);
2761
2762         iowrite16(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
2763
2764         update_stats(ioaddr, dev);
2765         if (vp->full_bus_master_rx)
2766                 iowrite32(0, ioaddr + UpListPtr);
2767         if (vp->full_bus_master_tx)
2768                 iowrite32(0, ioaddr + DownListPtr);
2769
2770         if (final_down && VORTEX_PCI(vp)) {
2771                 vp->pm_state_valid = 1;
2772                 pci_save_state(VORTEX_PCI(vp));
2773                 acpi_set_WOL(dev);
2774         }
2775 }
2776
2777 static int
2778 vortex_close(struct net_device *dev)
2779 {
2780         struct vortex_private *vp = netdev_priv(dev);
2781         void __iomem *ioaddr = vp->ioaddr;
2782         int i;
2783
2784         if (netif_device_present(dev))
2785                 vortex_down(dev, 1);
2786
2787         if (vortex_debug > 1) {
2788                 printk(KERN_DEBUG"%s: vortex_close() status %4.4x, Tx status %2.2x.\n",
2789                            dev->name, ioread16(ioaddr + EL3_STATUS), ioread8(ioaddr + TxStatus));
2790                 printk(KERN_DEBUG "%s: vortex close stats: rx_nocopy %d rx_copy %d"
2791                            " tx_queued %d Rx pre-checksummed %d.\n",
2792                            dev->name, vp->rx_nocopy, vp->rx_copy, vp->queued_packet, vp->rx_csumhits);
2793         }
2794
2795 #if DO_ZEROCOPY
2796         if (vp->rx_csumhits &&
2797             (vp->drv_flags & HAS_HWCKSM) == 0 &&
2798             (vp->card_idx >= MAX_UNITS || hw_checksums[vp->card_idx] == -1)) {
2799                         printk(KERN_WARNING "%s supports hardware checksums, and we're "
2800                                                 "not using them!\n", dev->name);
2801         }
2802 #endif
2803                 
2804         free_irq(dev->irq, dev);
2805
2806         if (vp->full_bus_master_rx) { /* Free Boomerang bus master Rx buffers. */
2807                 for (i = 0; i < RX_RING_SIZE; i++)
2808                         if (vp->rx_skbuff[i]) {
2809                                 pci_unmap_single(       VORTEX_PCI(vp), le32_to_cpu(vp->rx_ring[i].addr),
2810                                                                         PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
2811                                 dev_kfree_skb(vp->rx_skbuff[i]);
2812                                 vp->rx_skbuff[i] = NULL;
2813                         }
2814         }
2815         if (vp->full_bus_master_tx) { /* Free Boomerang bus master Tx buffers. */
2816                 for (i = 0; i < TX_RING_SIZE; i++) {
2817                         if (vp->tx_skbuff[i]) {
2818                                 struct sk_buff *skb = vp->tx_skbuff[i];
2819 #if DO_ZEROCOPY
2820                                 int k;
2821
2822                                 for (k=0; k<=skb_shinfo(skb)->nr_frags; k++)
2823                                                 pci_unmap_single(VORTEX_PCI(vp),
2824                                                                                  le32_to_cpu(vp->tx_ring[i].frag[k].addr),
2825                                                                                  le32_to_cpu(vp->tx_ring[i].frag[k].length)&0xFFF,
2826                                                                                  PCI_DMA_TODEVICE);
2827 #else
2828                                 pci_unmap_single(VORTEX_PCI(vp), le32_to_cpu(vp->tx_ring[i].addr), skb->len, PCI_DMA_TODEVICE);
2829 #endif
2830                                 dev_kfree_skb(skb);
2831                                 vp->tx_skbuff[i] = NULL;
2832                         }
2833                 }
2834         }
2835
2836         return 0;
2837 }
2838
2839 static void
2840 dump_tx_ring(struct net_device *dev)
2841 {
2842         if (vortex_debug > 0) {
2843         struct vortex_private *vp = netdev_priv(dev);
2844                 void __iomem *ioaddr = vp->ioaddr;
2845                 
2846                 if (vp->full_bus_master_tx) {
2847                         int i;
2848                         int stalled = ioread32(ioaddr + PktStatus) & 0x04;      /* Possible racy. But it's only debug stuff */
2849
2850                         printk(KERN_ERR "  Flags; bus-master %d, dirty %d(%d) current %d(%d)\n",
2851                                         vp->full_bus_master_tx,
2852                                         vp->dirty_tx, vp->dirty_tx % TX_RING_SIZE,
2853                                         vp->cur_tx, vp->cur_tx % TX_RING_SIZE);
2854                         printk(KERN_ERR "  Transmit list %8.8x vs. %p.\n",
2855                                    ioread32(ioaddr + DownListPtr),
2856                                    &vp->tx_ring[vp->dirty_tx % TX_RING_SIZE]);
2857                         issue_and_wait(dev, DownStall);
2858                         for (i = 0; i < TX_RING_SIZE; i++) {
2859                                 printk(KERN_ERR "  %d: @%p  length %8.8x status %8.8x\n", i,
2860                                            &vp->tx_ring[i],
2861 #if DO_ZEROCOPY
2862                                            le32_to_cpu(vp->tx_ring[i].frag[0].length),
2863 #else
2864                                            le32_to_cpu(vp->tx_ring[i].length),
2865 #endif
2866                                            le32_to_cpu(vp->tx_ring[i].status));
2867                         }
2868                         if (!stalled)
2869                                 iowrite16(DownUnstall, ioaddr + EL3_CMD);
2870                 }
2871         }
2872 }
2873
2874 static struct net_device_stats *vortex_get_stats(struct net_device *dev)
2875 {
2876         struct vortex_private *vp = netdev_priv(dev);
2877         void __iomem *ioaddr = vp->ioaddr;
2878         unsigned long flags;
2879
2880         if (netif_device_present(dev)) {        /* AKPM: Used to be netif_running */
2881                 spin_lock_irqsave (&vp->lock, flags);
2882                 update_stats(ioaddr, dev);
2883                 spin_unlock_irqrestore (&vp->lock, flags);
2884         }
2885         return &vp->stats;
2886 }
2887
2888 /*  Update statistics.
2889         Unlike with the EL3 we need not worry about interrupts changing
2890         the window setting from underneath us, but we must still guard
2891         against a race condition with a StatsUpdate interrupt updating the
2892         table.  This is done by checking that the ASM (!) code generated uses
2893         atomic updates with '+='.
2894         */
2895 static void update_stats(void __iomem *ioaddr, struct net_device *dev)
2896 {
2897         struct vortex_private *vp = netdev_priv(dev);
2898         int old_window = ioread16(ioaddr + EL3_CMD);
2899
2900         if (old_window == 0xffff)       /* Chip suspended or ejected. */
2901                 return;
2902         /* Unlike the 3c5x9 we need not turn off stats updates while reading. */
2903         /* Switch to the stats window, and read everything. */
2904         EL3WINDOW(6);
2905         vp->stats.tx_carrier_errors             += ioread8(ioaddr + 0);
2906         vp->stats.tx_heartbeat_errors           += ioread8(ioaddr + 1);
2907         vp->stats.tx_window_errors              += ioread8(ioaddr + 4);
2908         vp->stats.rx_fifo_errors                += ioread8(ioaddr + 5);
2909         vp->stats.tx_packets                    += ioread8(ioaddr + 6);
2910         vp->stats.tx_packets                    += (ioread8(ioaddr + 9)&0x30) << 4;
2911         /* Rx packets   */                      ioread8(ioaddr + 7);   /* Must read to clear */
2912         /* Don't bother with register 9, an extension of registers 6&7.
2913            If we do use the 6&7 values the atomic update assumption above
2914            is invalid. */
2915         vp->stats.rx_bytes                      += ioread16(ioaddr + 10);
2916         vp->stats.tx_bytes                      += ioread16(ioaddr + 12);
2917         /* Extra stats for get_ethtool_stats() */
2918         vp->xstats.tx_multiple_collisions       += ioread8(ioaddr + 2);
2919         vp->xstats.tx_single_collisions         += ioread8(ioaddr + 3);
2920         vp->xstats.tx_deferred                  += ioread8(ioaddr + 8);
2921         EL3WINDOW(4);
2922         vp->xstats.rx_bad_ssd                   += ioread8(ioaddr + 12);
2923
2924         vp->stats.collisions = vp->xstats.tx_multiple_collisions
2925                 + vp->xstats.tx_single_collisions
2926                 + vp->xstats.tx_max_collisions;
2927
2928         {
2929                 u8 up = ioread8(ioaddr + 13);
2930                 vp->stats.rx_bytes += (up & 0x0f) << 16;
2931                 vp->stats.tx_bytes += (up & 0xf0) << 12;
2932         }
2933
2934         EL3WINDOW(old_window >> 13);
2935         return;
2936 }
2937
2938 static int vortex_nway_reset(struct net_device *dev)
2939 {
2940         struct vortex_private *vp = netdev_priv(dev);
2941         void __iomem *ioaddr = vp->ioaddr;
2942         unsigned long flags;
2943         int rc;
2944
2945         spin_lock_irqsave(&vp->lock, flags);
2946         EL3WINDOW(4);
2947         rc = mii_nway_restart(&vp->mii);
2948         spin_unlock_irqrestore(&vp->lock, flags);
2949         return rc;
2950 }
2951
2952 static int vortex_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2953 {
2954         struct vortex_private *vp = netdev_priv(dev);
2955         void __iomem *ioaddr = vp->ioaddr;
2956         unsigned long flags;
2957         int rc;
2958
2959         spin_lock_irqsave(&vp->lock, flags);
2960         EL3WINDOW(4);
2961         rc = mii_ethtool_gset(&vp->mii, cmd);
2962         spin_unlock_irqrestore(&vp->lock, flags);
2963         return rc;
2964 }
2965
2966 static int vortex_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2967 {
2968         struct vortex_private *vp = netdev_priv(dev);
2969         void __iomem *ioaddr = vp->ioaddr;
2970         unsigned long flags;
2971         int rc;
2972
2973         spin_lock_irqsave(&vp->lock, flags);
2974         EL3WINDOW(4);
2975         rc = mii_ethtool_sset(&vp->mii, cmd);
2976         spin_unlock_irqrestore(&vp->lock, flags);
2977         return rc;
2978 }
2979
2980 static u32 vortex_get_msglevel(struct net_device *dev)
2981 {
2982         return vortex_debug;
2983 }
2984
2985 static void vortex_set_msglevel(struct net_device *dev, u32 dbg)
2986 {
2987         vortex_debug = dbg;
2988 }
2989
2990 static int vortex_get_stats_count(struct net_device *dev)
2991 {
2992         return VORTEX_NUM_STATS;
2993 }
2994
2995 static void vortex_get_ethtool_stats(struct net_device *dev,
2996         struct ethtool_stats *stats, u64 *data)
2997 {
2998         struct vortex_private *vp = netdev_priv(dev);
2999         void __iomem *ioaddr = vp->ioaddr;
3000         unsigned long flags;
3001
3002         spin_lock_irqsave(&vp->lock, flags);
3003         update_stats(ioaddr, dev);
3004         spin_unlock_irqrestore(&vp->lock, flags);
3005
3006         data[0] = vp->xstats.tx_deferred;
3007         data[1] = vp->xstats.tx_max_collisions;
3008         data[2] = vp->xstats.tx_multiple_collisions;
3009         data[3] = vp->xstats.tx_single_collisions;
3010         data[4] = vp->xstats.rx_bad_ssd;
3011 }
3012
3013
3014 static void vortex_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3015 {
3016         switch (stringset) {
3017         case ETH_SS_STATS:
3018                 memcpy(data, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
3019                 break;
3020         default:
3021                 WARN_ON(1);
3022                 break;
3023         }
3024 }
3025
3026 static void vortex_get_drvinfo(struct net_device *dev,
3027                                         struct ethtool_drvinfo *info)
3028 {
3029         struct vortex_private *vp = netdev_priv(dev);
3030
3031         strcpy(info->driver, DRV_NAME);
3032         if (VORTEX_PCI(vp)) {
3033                 strcpy(info->bus_info, pci_name(VORTEX_PCI(vp)));
3034         } else {
3035                 if (VORTEX_EISA(vp))
3036                         sprintf(info->bus_info, vp->gendev->bus_id);
3037                 else
3038                         sprintf(info->bus_info, "EISA 0x%lx %d",
3039                                         dev->base_addr, dev->irq);
3040         }
3041 }
3042
3043 static struct ethtool_ops vortex_ethtool_ops = {
3044         .get_drvinfo            = vortex_get_drvinfo,
3045         .get_strings            = vortex_get_strings,
3046         .get_msglevel           = vortex_get_msglevel,
3047         .set_msglevel           = vortex_set_msglevel,
3048         .get_ethtool_stats      = vortex_get_ethtool_stats,
3049         .get_stats_count        = vortex_get_stats_count,
3050         .get_settings           = vortex_get_settings,
3051         .set_settings           = vortex_set_settings,
3052         .get_link               = ethtool_op_get_link,
3053         .nway_reset             = vortex_nway_reset,
3054         .get_perm_addr          = ethtool_op_get_perm_addr,
3055 };
3056
3057 #ifdef CONFIG_PCI
3058 /*
3059  *      Must power the device up to do MDIO operations
3060  */
3061 static int vortex_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3062 {
3063         int err;
3064         struct vortex_private *vp = netdev_priv(dev);
3065         void __iomem *ioaddr = vp->ioaddr;
3066         unsigned long flags;
3067         int state = 0;
3068
3069         if(VORTEX_PCI(vp))
3070                 state = VORTEX_PCI(vp)->current_state;
3071
3072         /* The kernel core really should have pci_get_power_state() */
3073
3074         if(state != 0)
3075                 pci_set_power_state(VORTEX_PCI(vp), PCI_D0);
3076         spin_lock_irqsave(&vp->lock, flags);
3077         EL3WINDOW(4);
3078         err = generic_mii_ioctl(&vp->mii, if_mii(rq), cmd, NULL);
3079         spin_unlock_irqrestore(&vp->lock, flags);
3080         if(state != 0)
3081                 pci_set_power_state(VORTEX_PCI(vp), state);
3082
3083         return err;
3084 }
3085 #endif
3086
3087
3088 /* Pre-Cyclone chips have no documented multicast filter, so the only
3089    multicast setting is to receive all multicast frames.  At least
3090    the chip has a very clean way to set the mode, unlike many others. */
3091 static void set_rx_mode(struct net_device *dev)
3092 {
3093         struct vortex_private *vp = netdev_priv(dev);
3094         void __iomem *ioaddr = vp->ioaddr;
3095         int new_mode;
3096
3097         if (dev->flags & IFF_PROMISC) {
3098                 if (vortex_debug > 0)
3099                         printk(KERN_NOTICE "%s: Setting promiscuous mode.\n", dev->name);
3100                 new_mode = SetRxFilter|RxStation|RxMulticast|RxBroadcast|RxProm;
3101         } else  if ((dev->mc_list)  ||  (dev->flags & IFF_ALLMULTI)) {
3102                 new_mode = SetRxFilter|RxStation|RxMulticast|RxBroadcast;
3103         } else
3104                 new_mode = SetRxFilter | RxStation | RxBroadcast;
3105
3106         iowrite16(new_mode, ioaddr + EL3_CMD);
3107 }
3108
3109 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
3110 /* Setup the card so that it can receive frames with an 802.1q VLAN tag.
3111    Note that this must be done after each RxReset due to some backwards
3112    compatibility logic in the Cyclone and Tornado ASICs */
3113
3114 /* The Ethernet Type used for 802.1q tagged frames */
3115 #define VLAN_ETHER_TYPE 0x8100
3116
3117 static void set_8021q_mode(struct net_device *dev, int enable)
3118 {
3119         struct vortex_private *vp = netdev_priv(dev);
3120         void __iomem *ioaddr = vp->ioaddr;
3121         int old_window = ioread16(ioaddr + EL3_CMD);
3122         int mac_ctrl;
3123
3124         if ((vp->drv_flags&IS_CYCLONE) || (vp->drv_flags&IS_TORNADO)) {
3125                 /* cyclone and tornado chipsets can recognize 802.1q
3126                  * tagged frames and treat them correctly */
3127
3128                 int max_pkt_size = dev->mtu+14; /* MTU+Ethernet header */
3129                 if (enable)
3130                         max_pkt_size += 4;      /* 802.1Q VLAN tag */
3131
3132                 EL3WINDOW(3);
3133                 iowrite16(max_pkt_size, ioaddr+Wn3_MaxPktSize);
3134
3135                 /* set VlanEtherType to let the hardware checksumming
3136                    treat tagged frames correctly */
3137                 EL3WINDOW(7);
3138                 iowrite16(VLAN_ETHER_TYPE, ioaddr+Wn7_VlanEtherType);
3139         } else {
3140                 /* on older cards we have to enable large frames */
3141
3142                 vp->large_frames = dev->mtu > 1500 || enable;
3143
3144                 EL3WINDOW(3);
3145                 mac_ctrl = ioread16(ioaddr+Wn3_MAC_Ctrl);
3146                 if (vp->large_frames)
3147                         mac_ctrl |= 0x40;
3148                 else
3149                         mac_ctrl &= ~0x40;
3150                 iowrite16(mac_ctrl, ioaddr+Wn3_MAC_Ctrl);
3151         }
3152
3153         EL3WINDOW(old_window);
3154 }
3155 #else
3156
3157 static void set_8021q_mode(struct net_device *dev, int enable)
3158 {
3159 }
3160
3161
3162 #endif
3163
3164 /* MII transceiver control section.
3165    Read and write the MII registers using software-generated serial
3166    MDIO protocol.  See the MII specifications or DP83840A data sheet
3167    for details. */
3168
3169 /* The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
3170    met by back-to-back PCI I/O cycles, but we insert a delay to avoid
3171    "overclocking" issues. */
3172 #define mdio_delay() ioread32(mdio_addr)
3173
3174 #define MDIO_SHIFT_CLK  0x01
3175 #define MDIO_DIR_WRITE  0x04
3176 #define MDIO_DATA_WRITE0 (0x00 | MDIO_DIR_WRITE)
3177 #define MDIO_DATA_WRITE1 (0x02 | MDIO_DIR_WRITE)
3178 #define MDIO_DATA_READ  0x02
3179 #define MDIO_ENB_IN             0x00
3180
3181 /* Generate the preamble required for initial synchronization and
3182    a few older transceivers. */
3183 static void mdio_sync(void __iomem *ioaddr, int bits)
3184 {
3185         void __iomem *mdio_addr = ioaddr + Wn4_PhysicalMgmt;
3186
3187         /* Establish sync by sending at least 32 logic ones. */
3188         while (-- bits >= 0) {
3189                 iowrite16(MDIO_DATA_WRITE1, mdio_addr);
3190                 mdio_delay();
3191                 iowrite16(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
3192                 mdio_delay();
3193         }
3194 }
3195
3196 static int mdio_read(struct net_device *dev, int phy_id, int location)
3197 {
3198         int i;
3199         struct vortex_private *vp = netdev_priv(dev);
3200         void __iomem *ioaddr = vp->ioaddr;
3201         int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
3202         unsigned int retval = 0;
3203         void __iomem *mdio_addr = ioaddr + Wn4_PhysicalMgmt;
3204
3205         if (mii_preamble_required)
3206                 mdio_sync(ioaddr, 32);
3207
3208         /* Shift the read command bits out. */
3209         for (i = 14; i >= 0; i--) {
3210                 int dataval = (read_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
3211                 iowrite16(dataval, mdio_addr);
3212                 mdio_delay();
3213                 iowrite16(dataval | MDIO_SHIFT_CLK, mdio_addr);
3214                 mdio_delay();
3215         }
3216         /* Read the two transition, 16 data, and wire-idle bits. */
3217         for (i = 19; i > 0; i--) {
3218                 iowrite16(MDIO_ENB_IN, mdio_addr);
3219                 mdio_delay();
3220                 retval = (retval << 1) | ((ioread16(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
3221                 iowrite16(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
3222                 mdio_delay();
3223         }
3224         return retval & 0x20000 ? 0xffff : retval>>1 & 0xffff;
3225 }
3226
3227 static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
3228 {
3229         struct vortex_private *vp = netdev_priv(dev);
3230         void __iomem *ioaddr = vp->ioaddr;
3231         int write_cmd = 0x50020000 | (phy_id << 23) | (location << 18) | value;
3232         void __iomem *mdio_addr = ioaddr + Wn4_PhysicalMgmt;
3233         int i;
3234
3235         if (mii_preamble_required)
3236                 mdio_sync(ioaddr, 32);
3237
3238         /* Shift the command bits out. */
3239         for (i = 31; i >= 0; i--) {
3240                 int dataval = (write_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
3241                 iowrite16(dataval, mdio_addr);
3242                 mdio_delay();
3243                 iowrite16(dataval | MDIO_SHIFT_CLK, mdio_addr);
3244                 mdio_delay();
3245         }
3246         /* Leave the interface idle. */
3247         for (i = 1; i >= 0; i--) {
3248                 iowrite16(MDIO_ENB_IN, mdio_addr);
3249                 mdio_delay();
3250                 iowrite16(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
3251                 mdio_delay();
3252         }
3253         return;
3254 }
3255
3256 /* ACPI: Advanced Configuration and Power Interface. */
3257 /* Set Wake-On-LAN mode and put the board into D3 (power-down) state. */
3258 static void acpi_set_WOL(struct net_device *dev)
3259 {
3260         struct vortex_private *vp = netdev_priv(dev);
3261         void __iomem *ioaddr = vp->ioaddr;
3262
3263         if (vp->enable_wol) {
3264                 /* Power up on: 1==Downloaded Filter, 2==Magic Packets, 4==Link Status. */
3265                 EL3WINDOW(7);
3266                 iowrite16(2, ioaddr + 0x0c);
3267                 /* The RxFilter must accept the WOL frames. */
3268                 iowrite16(SetRxFilter|RxStation|RxMulticast|RxBroadcast, ioaddr + EL3_CMD);
3269                 iowrite16(RxEnable, ioaddr + EL3_CMD);
3270
3271                 pci_enable_wake(VORTEX_PCI(vp), 0, 1);
3272
3273                 /* Change the power state to D3; RxEnable doesn't take effect. */
3274                 pci_set_power_state(VORTEX_PCI(vp), PCI_D3hot);
3275         }
3276 }
3277
3278
3279 static void __devexit vortex_remove_one(struct pci_dev *pdev)
3280 {
3281         struct net_device *dev = pci_get_drvdata(pdev);
3282         struct vortex_private *vp;
3283
3284         if (!dev) {
3285                 printk("vortex_remove_one called for Compaq device!\n");
3286                 BUG();
3287         }
3288
3289         vp = netdev_priv(dev);
3290
3291         if (vp->cb_fn_base)
3292                 pci_iounmap(VORTEX_PCI(vp), vp->cb_fn_base);
3293
3294         unregister_netdev(dev);
3295
3296         if (VORTEX_PCI(vp)) {
3297                 pci_set_power_state(VORTEX_PCI(vp), PCI_D0);    /* Go active */
3298                 if (vp->pm_state_valid)
3299                         pci_restore_state(VORTEX_PCI(vp));
3300                 pci_disable_device(VORTEX_PCI(vp));
3301         }
3302         /* Should really use issue_and_wait() here */
3303         iowrite16(TotalReset | ((vp->drv_flags & EEPROM_RESET) ? 0x04 : 0x14),
3304              vp->ioaddr + EL3_CMD);
3305
3306         pci_iounmap(VORTEX_PCI(vp), vp->ioaddr);
3307
3308         pci_free_consistent(pdev,
3309                                                 sizeof(struct boom_rx_desc) * RX_RING_SIZE
3310                                                         + sizeof(struct boom_tx_desc) * TX_RING_SIZE,
3311                                                 vp->rx_ring,
3312                                                 vp->rx_ring_dma);
3313         if (vp->must_free_region)
3314                 release_region(dev->base_addr, vp->io_size);
3315         free_netdev(dev);
3316 }
3317
3318
3319 static struct pci_driver vortex_driver = {
3320         .name           = "3c59x",
3321         .probe          = vortex_init_one,
3322         .remove         = __devexit_p(vortex_remove_one),
3323         .id_table       = vortex_pci_tbl,
3324 #ifdef CONFIG_PM
3325         .suspend        = vortex_suspend,
3326         .resume         = vortex_resume,
3327 #endif
3328 };
3329
3330
3331 static int vortex_have_pci;
3332 static int vortex_have_eisa;
3333
3334
3335 static int __init vortex_init(void)
3336 {
3337         int pci_rc, eisa_rc;
3338
3339         pci_rc = pci_module_init(&vortex_driver);
3340         eisa_rc = vortex_eisa_init();
3341
3342         if (pci_rc == 0)
3343                 vortex_have_pci = 1;
3344         if (eisa_rc > 0)
3345                 vortex_have_eisa = 1;
3346
3347         return (vortex_have_pci + vortex_have_eisa) ? 0 : -ENODEV;
3348 }
3349
3350
3351 static void __exit vortex_eisa_cleanup(void)
3352 {
3353         struct vortex_private *vp;
3354         void __iomem *ioaddr;
3355
3356 #ifdef CONFIG_EISA
3357         /* Take care of the EISA devices */
3358         eisa_driver_unregister(&vortex_eisa_driver);
3359 #endif
3360         
3361         if (compaq_net_device) {
3362                 vp = compaq_net_device->priv;
3363                 ioaddr = ioport_map(compaq_net_device->base_addr,
3364                                     VORTEX_TOTAL_SIZE);
3365
3366                 unregister_netdev(compaq_net_device);
3367                 iowrite16(TotalReset, ioaddr + EL3_CMD);
3368                 release_region(compaq_net_device->base_addr,
3369                                VORTEX_TOTAL_SIZE);
3370
3371                 free_netdev(compaq_net_device);
3372         }
3373 }
3374
3375
3376 static void __exit vortex_cleanup(void)
3377 {
3378         if (vortex_have_pci)
3379                 pci_unregister_driver(&vortex_driver);
3380         if (vortex_have_eisa)
3381                 vortex_eisa_cleanup();
3382 }
3383
3384
3385 module_init(vortex_init);
3386 module_exit(vortex_cleanup);