vserver 1.9.3
[linux-2.6.git] / drivers / net / via-rhine.c
1 /* via-rhine.c: A Linux Ethernet device driver for VIA Rhine family chips. */
2 /*
3         Written 1998-2001 by Donald Becker.
4
5         Current Maintainer: Roger Luethi <rl@hellgate.ch>
6
7         This software may be used and distributed according to the terms of
8         the GNU General Public License (GPL), incorporated herein by reference.
9         Drivers based on or derived from this code fall under the GPL and must
10         retain the authorship, copyright and license notice.  This file is not
11         a complete program and may only be used when the entire operating
12         system is licensed under the GPL.
13
14         This driver is designed for the VIA VT86C100A Rhine-I.
15         It also works with the Rhine-II (6102) and Rhine-III (6105/6105L/6105LOM
16         and management NIC 6105M).
17
18         The author may be reached as becker@scyld.com, or C/O
19         Scyld Computing Corporation
20         410 Severn Ave., Suite 210
21         Annapolis MD 21403
22
23
24         This driver contains some changes from the original Donald Becker
25         version. He may or may not be interested in bug reports on this
26         code. You can find his versions at:
27         http://www.scyld.com/network/via-rhine.html
28
29
30         Linux kernel version history:
31
32         LK1.1.0:
33         - Jeff Garzik: softnet 'n stuff
34
35         LK1.1.1:
36         - Justin Guyett: softnet and locking fixes
37         - Jeff Garzik: use PCI interface
38
39         LK1.1.2:
40         - Urban Widmark: minor cleanups, merges from Becker 1.03a/1.04 versions
41
42         LK1.1.3:
43         - Urban Widmark: use PCI DMA interface (with thanks to the eepro100.c
44                          code) update "Theory of Operation" with
45                          softnet/locking changes
46         - Dave Miller: PCI DMA and endian fixups
47         - Jeff Garzik: MOD_xxx race fixes, updated PCI resource allocation
48
49         LK1.1.4:
50         - Urban Widmark: fix gcc 2.95.2 problem and
51                          remove writel's to fixed address 0x7c
52
53         LK1.1.5:
54         - Urban Widmark: mdio locking, bounce buffer changes
55                          merges from Beckers 1.05 version
56                          added netif_running_on/off support
57
58         LK1.1.6:
59         - Urban Widmark: merges from Beckers 1.08b version (VT6102 + mdio)
60                          set netif_running_on/off on startup, del_timer_sync
61
62         LK1.1.7:
63         - Manfred Spraul: added reset into tx_timeout
64
65         LK1.1.9:
66         - Urban Widmark: merges from Beckers 1.10 version
67                          (media selection + eeprom reload)
68         - David Vrabel:  merges from D-Link "1.11" version
69                          (disable WOL and PME on startup)
70
71         LK1.1.10:
72         - Manfred Spraul: use "singlecopy" for unaligned buffers
73                           don't allocate bounce buffers for !ReqTxAlign cards
74
75         LK1.1.11:
76         - David Woodhouse: Set dev->base_addr before the first time we call
77                            wait_for_reset(). It's a lot happier that way.
78                            Free np->tx_bufs only if we actually allocated it.
79
80         LK1.1.12:
81         - Martin Eriksson: Allow Memory-Mapped IO to be enabled.
82
83         LK1.1.13 (jgarzik):
84         - Add ethtool support
85         - Replace some MII-related magic numbers with constants
86
87         LK1.1.14 (Ivan G.):
88         - fixes comments for Rhine-III
89         - removes W_MAX_TIMEOUT (unused)
90         - adds HasDavicomPhy for Rhine-I (basis: linuxfet driver; my card
91           is R-I and has Davicom chip, flag is referenced in kernel driver)
92         - sends chip_id as a parameter to wait_for_reset since np is not
93           initialized on first call
94         - changes mmio "else if (chip_id==VT6102)" to "else" so it will work
95           for Rhine-III's (documentation says same bit is correct)
96         - transmit frame queue message is off by one - fixed
97         - adds IntrNormalSummary to "Something Wicked" exclusion list
98           so normal interrupts will not trigger the message (src: Donald Becker)
99         (Roger Luethi)
100         - show confused chip where to continue after Tx error
101         - location of collision counter is chip specific
102         - allow selecting backoff algorithm (module parameter)
103
104         LK1.1.15 (jgarzik):
105         - Use new MII lib helper generic_mii_ioctl
106
107         LK1.1.16 (Roger Luethi)
108         - Etherleak fix
109         - Handle Tx buffer underrun
110         - Fix bugs in full duplex handling
111         - New reset code uses "force reset" cmd on Rhine-II
112         - Various clean ups
113
114         LK1.1.17 (Roger Luethi)
115         - Fix race in via_rhine_start_tx()
116         - On errors, wait for Tx engine to turn off before scavenging
117         - Handle Tx descriptor write-back race on Rhine-II
118         - Force flushing for PCI posted writes
119         - More reset code changes
120
121         LK1.1.18 (Roger Luethi)
122         - No filtering multicast in promisc mode (Edward Peng)
123         - Fix for Rhine-I Tx timeouts
124
125         LK1.1.19 (Roger Luethi)
126         - Increase Tx threshold for unspecified errors
127
128         LK1.2.0-2.6 (Roger Luethi)
129         - Massive clean-up
130         - Rewrite PHY, media handling (remove options, full_duplex, backoff)
131         - Fix Tx engine race for good
132
133 */
134
135 #define DRV_NAME        "via-rhine"
136 #define DRV_VERSION     "1.2.0-2.6"
137 #define DRV_RELDATE     "June-10-2004"
138
139
140 /* A few user-configurable values.
141    These may be modified when a driver module is loaded. */
142
143 static int debug = 1;   /* 1 normal messages, 0 quiet .. 7 verbose. */
144 static int max_interrupt_work = 20;
145
146 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
147    Setting to > 1518 effectively disables this feature. */
148 static int rx_copybreak;
149
150 /*
151  * In case you are looking for 'options[]' or 'full_duplex[]', they
152  * are gone. Use ethtool(8) instead.
153  */
154
155 /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
156    The Rhine has a 64 element 8390-like hash table. */
157 static const int multicast_filter_limit = 32;
158
159
160 /* Operational parameters that are set at compile time. */
161
162 /* Keep the ring sizes a power of two for compile efficiency.
163    The compiler will convert <unsigned>'%'<2^N> into a bit mask.
164    Making the Tx ring too large decreases the effectiveness of channel
165    bonding and packet priority.
166    There are no ill effects from too-large receive rings. */
167 #define TX_RING_SIZE    16
168 #define TX_QUEUE_LEN    10      /* Limit ring entries actually used. */
169 #define RX_RING_SIZE    16
170
171
172 /* Operational parameters that usually are not changed. */
173
174 /* Time in jiffies before concluding the transmitter is hung. */
175 #define TX_TIMEOUT      (2*HZ)
176
177 #define PKT_BUF_SZ      1536    /* Size of each temporary Rx buffer.*/
178
179 #include <linux/module.h>
180 #include <linux/kernel.h>
181 #include <linux/string.h>
182 #include <linux/timer.h>
183 #include <linux/errno.h>
184 #include <linux/ioport.h>
185 #include <linux/slab.h>
186 #include <linux/interrupt.h>
187 #include <linux/pci.h>
188 #include <linux/netdevice.h>
189 #include <linux/etherdevice.h>
190 #include <linux/skbuff.h>
191 #include <linux/init.h>
192 #include <linux/delay.h>
193 #include <linux/mii.h>
194 #include <linux/ethtool.h>
195 #include <linux/crc32.h>
196 #include <asm/processor.h>      /* Processor type for cache alignment. */
197 #include <asm/bitops.h>
198 #include <asm/io.h>
199 #include <asm/irq.h>
200 #include <asm/uaccess.h>
201
202 /* These identify the driver base version and may not be removed. */
203 static char version[] __devinitdata =
204 KERN_INFO DRV_NAME ".c:v1.10-LK" DRV_VERSION " " DRV_RELDATE " Written by Donald Becker\n";
205
206 /* This driver was written to use PCI memory space. Some early versions
207    of the Rhine may only work correctly with I/O space accesses. */
208 #ifdef CONFIG_VIA_RHINE_MMIO
209 #define USE_MMIO
210 #else
211 #undef readb
212 #undef readw
213 #undef readl
214 #undef writeb
215 #undef writew
216 #undef writel
217 #define readb inb
218 #define readw inw
219 #define readl inl
220 #define writeb outb
221 #define writew outw
222 #define writel outl
223 #endif
224
225 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
226 MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
227 MODULE_LICENSE("GPL");
228
229 MODULE_PARM(max_interrupt_work, "i");
230 MODULE_PARM(debug, "i");
231 MODULE_PARM(rx_copybreak, "i");
232 MODULE_PARM_DESC(max_interrupt_work, "VIA Rhine maximum events handled per interrupt");
233 MODULE_PARM_DESC(debug, "VIA Rhine debug level (0-7)");
234 MODULE_PARM_DESC(rx_copybreak, "VIA Rhine copy breakpoint for copy-only-tiny-frames");
235
236 /*
237                 Theory of Operation
238
239 I. Board Compatibility
240
241 This driver is designed for the VIA 86c100A Rhine-II PCI Fast Ethernet
242 controller.
243
244 II. Board-specific settings
245
246 Boards with this chip are functional only in a bus-master PCI slot.
247
248 Many operational settings are loaded from the EEPROM to the Config word at
249 offset 0x78. For most of these settings, this driver assumes that they are
250 correct.
251 If this driver is compiled to use PCI memory space operations the EEPROM
252 must be configured to enable memory ops.
253
254 III. Driver operation
255
256 IIIa. Ring buffers
257
258 This driver uses two statically allocated fixed-size descriptor lists
259 formed into rings by a branch from the final descriptor to the beginning of
260 the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
261
262 IIIb/c. Transmit/Receive Structure
263
264 This driver attempts to use a zero-copy receive and transmit scheme.
265
266 Alas, all data buffers are required to start on a 32 bit boundary, so
267 the driver must often copy transmit packets into bounce buffers.
268
269 The driver allocates full frame size skbuffs for the Rx ring buffers at
270 open() time and passes the skb->data field to the chip as receive data
271 buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
272 a fresh skbuff is allocated and the frame is copied to the new skbuff.
273 When the incoming frame is larger, the skbuff is passed directly up the
274 protocol stack. Buffers consumed this way are replaced by newly allocated
275 skbuffs in the last phase of rhine_rx().
276
277 The RX_COPYBREAK value is chosen to trade-off the memory wasted by
278 using a full-sized skbuff for small frames vs. the copying costs of larger
279 frames. New boards are typically used in generously configured machines
280 and the underfilled buffers have negligible impact compared to the benefit of
281 a single allocation size, so the default value of zero results in never
282 copying packets. When copying is done, the cost is usually mitigated by using
283 a combined copy/checksum routine. Copying also preloads the cache, which is
284 most useful with small frames.
285
286 Since the VIA chips are only able to transfer data to buffers on 32 bit
287 boundaries, the IP header at offset 14 in an ethernet frame isn't
288 longword aligned for further processing. Copying these unaligned buffers
289 has the beneficial effect of 16-byte aligning the IP header.
290
291 IIId. Synchronization
292
293 The driver runs as two independent, single-threaded flows of control. One
294 is the send-packet routine, which enforces single-threaded use by the
295 dev->priv->lock spinlock. The other thread is the interrupt handler, which
296 is single threaded by the hardware and interrupt handling software.
297
298 The send packet thread has partial control over the Tx ring. It locks the
299 dev->priv->lock whenever it's queuing a Tx packet. If the next slot in the ring
300 is not available it stops the transmit queue by calling netif_stop_queue.
301
302 The interrupt handler has exclusive control over the Rx ring and records stats
303 from the Tx ring. After reaping the stats, it marks the Tx queue entry as
304 empty by incrementing the dirty_tx mark. If at least half of the entries in
305 the Rx ring are available the transmit queue is woken up if it was stopped.
306
307 IV. Notes
308
309 IVb. References
310
311 Preliminary VT86C100A manual from http://www.via.com.tw/
312 http://www.scyld.com/expert/100mbps.html
313 http://www.scyld.com/expert/NWay.html
314 ftp://ftp.via.com.tw/public/lan/Products/NIC/VT86C100A/Datasheet/VT86C100A03.pdf
315 ftp://ftp.via.com.tw/public/lan/Products/NIC/VT6102/Datasheet/VT6102_021.PDF
316
317
318 IVc. Errata
319
320 The VT86C100A manual is not reliable information.
321 The 3043 chip does not handle unaligned transmit or receive buffers, resulting
322 in significant performance degradation for bounce buffer copies on transmit
323 and unaligned IP headers on receive.
324 The chip does not pad to minimum transmit length.
325
326 */
327
328
329 /* This table drives the PCI probe routines. It's mostly boilerplate in all
330    of the drivers, and will likely be provided by some future kernel.
331    Note the matching code -- the first table entry matchs all 56** cards but
332    second only the 1234 card.
333 */
334
335 enum rhine_revs {
336         VT86C100A       = 0x00,
337         VTunknown0      = 0x20,
338         VT6102          = 0x40,
339         VT8231          = 0x50, /* Integrated MAC */
340         VT8233          = 0x60, /* Integrated MAC */
341         VT8235          = 0x74, /* Integrated MAC */
342         VT8237          = 0x78, /* Integrated MAC */
343         VTunknown1      = 0x7C,
344         VT6105          = 0x80,
345         VT6105_B0       = 0x83,
346         VT6105L         = 0x8A,
347         VT6107          = 0x8C,
348         VTunknown2      = 0x8E,
349         VT6105M         = 0x90, /* Management adapter */
350 };
351
352 enum rhine_quirks {
353         rqWOL           = 0x0001,       /* Wake-On-LAN support */
354         rqForceReset    = 0x0002,
355         rq6patterns     = 0x0040,       /* 6 instead of 4 patterns for WOL */
356         rqStatusWBRace  = 0x0080,       /* Tx Status Writeback Error possible */
357         rqRhineI        = 0x0100,       /* See comment below */
358 };
359 /*
360  * rqRhineI: VT86C100A (aka Rhine-I) uses different bits to enable
361  * MMIO as well as for the collision counter and the Tx FIFO underflow
362  * indicator. In addition, Tx and Rx buffers need to 4 byte aligned.
363  */
364
365 /* Beware of PCI posted writes */
366 #define IOSYNC  do { readb(dev->base_addr + StationAddr); } while (0)
367
368 static struct pci_device_id rhine_pci_tbl[] =
369 {
370         {0x1106, 0x3043, PCI_ANY_ID, PCI_ANY_ID, 0, 0, }, /* VT86C100A */
371         {0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, }, /* VT6102 */
372         {0x1106, 0x3106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, }, /* 6105{,L,LOM} */
373         {0x1106, 0x3053, PCI_ANY_ID, PCI_ANY_ID, 0, 0, }, /* VT6105M */
374         { }     /* terminate list */
375 };
376 MODULE_DEVICE_TABLE(pci, rhine_pci_tbl);
377
378
379 /* Offsets to the device registers. */
380 enum register_offsets {
381         StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
382         ChipCmd1=0x09,
383         IntrStatus=0x0C, IntrEnable=0x0E,
384         MulticastFilter0=0x10, MulticastFilter1=0x14,
385         RxRingPtr=0x18, TxRingPtr=0x1C, GFIFOTest=0x54,
386         MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E,
387         MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72, MACRegEEcsr=0x74,
388         ConfigA=0x78, ConfigB=0x79, ConfigC=0x7A, ConfigD=0x7B,
389         RxMissed=0x7C, RxCRCErrs=0x7E, MiscCmd=0x81,
390         StickyHW=0x83, IntrStatus2=0x84,
391         WOLcrSet=0xA0, PwcfgSet=0xA1, WOLcgSet=0xA3, WOLcrClr=0xA4,
392         WOLcrClr1=0xA6, WOLcgClr=0xA7,
393         PwrcsrSet=0xA8, PwrcsrSet1=0xA9, PwrcsrClr=0xAC, PwrcsrClr1=0xAD,
394 };
395
396 /* Bits in ConfigD */
397 enum backoff_bits {
398         BackOptional=0x01, BackModify=0x02,
399         BackCaptureEffect=0x04, BackRandom=0x08
400 };
401
402 #ifdef USE_MMIO
403 /* Registers we check that mmio and reg are the same. */
404 int mmio_verify_registers[] = {
405         RxConfig, TxConfig, IntrEnable, ConfigA, ConfigB, ConfigC, ConfigD,
406         0
407 };
408 #endif
409
410 /* Bits in the interrupt status/mask registers. */
411 enum intr_status_bits {
412         IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
413         IntrTxDone=0x0002, IntrTxError=0x0008, IntrTxUnderrun=0x0210,
414         IntrPCIErr=0x0040,
415         IntrStatsMax=0x0080, IntrRxEarly=0x0100,
416         IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
417         IntrTxAborted=0x2000, IntrLinkChange=0x4000,
418         IntrRxWakeUp=0x8000,
419         IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
420         IntrTxDescRace=0x080000,        /* mapped from IntrStatus2 */
421         IntrTxErrSummary=0x082218,
422 };
423
424 /* Bits in WOLcrSet/WOLcrClr and PwrcsrSet/PwrcsrClr */
425 enum wol_bits {
426         WOLucast        = 0x10,
427         WOLmagic        = 0x20,
428         WOLbmcast       = 0x30,
429         WOLlnkon        = 0x40,
430         WOLlnkoff       = 0x80,
431 };
432
433 /* The Rx and Tx buffer descriptors. */
434 struct rx_desc {
435         s32 rx_status;
436         u32 desc_length; /* Chain flag, Buffer/frame length */
437         u32 addr;
438         u32 next_desc;
439 };
440 struct tx_desc {
441         s32 tx_status;
442         u32 desc_length; /* Chain flag, Tx Config, Frame length */
443         u32 addr;
444         u32 next_desc;
445 };
446
447 /* Initial value for tx_desc.desc_length, Buffer size goes to bits 0-10 */
448 #define TXDESC          0x00e08000
449
450 enum rx_status_bits {
451         RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F
452 };
453
454 /* Bits in *_desc.*_status */
455 enum desc_status_bits {
456         DescOwn=0x80000000
457 };
458
459 /* Bits in ChipCmd. */
460 enum chip_cmd_bits {
461         CmdInit=0x01, CmdStart=0x02, CmdStop=0x04, CmdRxOn=0x08,
462         CmdTxOn=0x10, Cmd1TxDemand=0x20, CmdRxDemand=0x40,
463         Cmd1EarlyRx=0x01, Cmd1EarlyTx=0x02, Cmd1FDuplex=0x04,
464         Cmd1NoTxPoll=0x08, Cmd1Reset=0x80,
465 };
466
467 struct rhine_private {
468         /* Descriptor rings */
469         struct rx_desc *rx_ring;
470         struct tx_desc *tx_ring;
471         dma_addr_t rx_ring_dma;
472         dma_addr_t tx_ring_dma;
473
474         /* The addresses of receive-in-place skbuffs. */
475         struct sk_buff *rx_skbuff[RX_RING_SIZE];
476         dma_addr_t rx_skbuff_dma[RX_RING_SIZE];
477
478         /* The saved address of a sent-in-place packet/buffer, for later free(). */
479         struct sk_buff *tx_skbuff[TX_RING_SIZE];
480         dma_addr_t tx_skbuff_dma[TX_RING_SIZE];
481
482         /* Tx bounce buffers */
483         unsigned char *tx_buf[TX_RING_SIZE];
484         unsigned char *tx_bufs;
485         dma_addr_t tx_bufs_dma;
486
487         struct pci_dev *pdev;
488         long pioaddr;
489         struct net_device_stats stats;
490         spinlock_t lock;
491
492         /* Frequently used values: keep some adjacent for cache effect. */
493         u32 quirks;
494         struct rx_desc *rx_head_desc;
495         unsigned int cur_rx, dirty_rx;  /* Producer/consumer ring indices */
496         unsigned int cur_tx, dirty_tx;
497         unsigned int rx_buf_sz;         /* Based on MTU+slack. */
498         u8 wolopts;
499
500         u8 tx_thresh, rx_thresh;
501
502         struct mii_if_info mii_if;
503 };
504
505 static int  mdio_read(struct net_device *dev, int phy_id, int location);
506 static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
507 static int  rhine_open(struct net_device *dev);
508 static void rhine_tx_timeout(struct net_device *dev);
509 static int  rhine_start_tx(struct sk_buff *skb, struct net_device *dev);
510 static irqreturn_t rhine_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
511 static void rhine_tx(struct net_device *dev);
512 static void rhine_rx(struct net_device *dev);
513 static void rhine_error(struct net_device *dev, int intr_status);
514 static void rhine_set_rx_mode(struct net_device *dev);
515 static struct net_device_stats *rhine_get_stats(struct net_device *dev);
516 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
517 static struct ethtool_ops netdev_ethtool_ops;
518 static int  rhine_close(struct net_device *dev);
519 static void rhine_shutdown (struct device *gdev);
520
521 #define RHINE_WAIT_FOR(condition) do {                                  \
522         int i=1024;                                                     \
523         while (!(condition) && --i)                                     \
524                 ;                                                       \
525         if (debug > 1 && i < 512)                                       \
526                 printk(KERN_INFO "%s: %4d cycles used @ %s:%d\n",       \
527                                 DRV_NAME, 1024-i, __func__, __LINE__);  \
528 } while(0)
529
530 static inline u32 get_intr_status(struct net_device *dev)
531 {
532         long ioaddr = dev->base_addr;
533         struct rhine_private *rp = netdev_priv(dev);
534         u32 intr_status;
535
536         intr_status = readw(ioaddr + IntrStatus);
537         /* On Rhine-II, Bit 3 indicates Tx descriptor write-back race. */
538         if (rp->quirks & rqStatusWBRace)
539                 intr_status |= readb(ioaddr + IntrStatus2) << 16;
540         return intr_status;
541 }
542
543 /*
544  * Get power related registers into sane state.
545  * Notify user about past WOL event.
546  */
547 static void rhine_power_init(struct net_device *dev)
548 {
549         long ioaddr = dev->base_addr;
550         struct rhine_private *rp = netdev_priv(dev);
551         u16 wolstat;
552
553         if (rp->quirks & rqWOL) {
554                 /* Make sure chip is in power state D0 */
555                 writeb(readb(ioaddr + StickyHW) & 0xFC, ioaddr + StickyHW);
556
557                 /* Disable "force PME-enable" */
558                 writeb(0x80, ioaddr + WOLcgClr);
559
560                 /* Clear power-event config bits (WOL) */
561                 writeb(0xFF, ioaddr + WOLcrClr);
562                 /* More recent cards can manage two additional patterns */
563                 if (rp->quirks & rq6patterns)
564                         writeb(0x03, ioaddr + WOLcrClr1);
565
566                 /* Save power-event status bits */
567                 wolstat = readb(ioaddr + PwrcsrSet);
568                 if (rp->quirks & rq6patterns)
569                         wolstat |= (readb(ioaddr + PwrcsrSet1) & 0x03) << 8;
570
571                 /* Clear power-event status bits */
572                 writeb(0xFF, ioaddr + PwrcsrClr);
573                 if (rp->quirks & rq6patterns)
574                         writeb(0x03, ioaddr + PwrcsrClr1);
575
576                 if (wolstat) {
577                         char *reason;
578                         switch (wolstat) {
579                         case WOLmagic:
580                                 reason = "Magic packet";
581                                 break;
582                         case WOLlnkon:
583                                 reason = "Link went up";
584                                 break;
585                         case WOLlnkoff:
586                                 reason = "Link went down";
587                                 break;
588                         case WOLucast:
589                                 reason = "Unicast packet";
590                                 break;
591                         case WOLbmcast:
592                                 reason = "Multicast/broadcast packet";
593                                 break;
594                         default:
595                                 reason = "Unknown";
596                         }
597                         printk(KERN_INFO "%s: Woke system up. Reason: %s.\n",
598                                DRV_NAME, reason);
599                 }
600         }
601 }
602
603 static void rhine_chip_reset(struct net_device *dev)
604 {
605         long ioaddr = dev->base_addr;
606         struct rhine_private *rp = netdev_priv(dev);
607
608         writeb(Cmd1Reset, ioaddr + ChipCmd1);
609         IOSYNC;
610
611         if (readb(ioaddr + ChipCmd1) & Cmd1Reset) {
612                 printk(KERN_INFO "%s: Reset not complete yet. "
613                         "Trying harder.\n", DRV_NAME);
614
615                 /* Force reset */
616                 if (rp->quirks & rqForceReset)
617                         writeb(0x40, ioaddr + MiscCmd);
618
619                 /* Reset can take somewhat longer (rare) */
620                 RHINE_WAIT_FOR(!(readb(ioaddr + ChipCmd1) & Cmd1Reset));
621         }
622
623         if (debug > 1)
624                 printk(KERN_INFO "%s: Reset %s.\n", dev->name,
625                         (readb(ioaddr + ChipCmd1) & Cmd1Reset) ?
626                         "failed" : "succeeded");
627 }
628
629 #ifdef USE_MMIO
630 static void __devinit enable_mmio(long pioaddr, u32 quirks)
631 {
632         int n;
633         if (quirks & rqRhineI) {
634                 /* More recent docs say that this bit is reserved ... */
635                 n = inb(pioaddr + ConfigA) | 0x20;
636                 outb(n, pioaddr + ConfigA);
637         } else {
638                 n = inb(pioaddr + ConfigD) | 0x80;
639                 outb(n, pioaddr + ConfigD);
640         }
641 }
642 #endif
643
644 /*
645  * Loads bytes 0x00-0x05, 0x6E-0x6F, 0x78-0x7B from EEPROM
646  * (plus 0x6C for Rhine-I/II)
647  */
648 static void __devinit rhine_reload_eeprom(long pioaddr, struct net_device *dev)
649 {
650         long ioaddr = dev->base_addr;
651         struct rhine_private *rp = netdev_priv(dev);
652
653         outb(0x20, pioaddr + MACRegEEcsr);
654         RHINE_WAIT_FOR(!(inb(pioaddr + MACRegEEcsr) & 0x20));
655
656 #ifdef USE_MMIO
657         /*
658          * Reloading from EEPROM overwrites ConfigA-D, so we must re-enable
659          * MMIO. If reloading EEPROM was done first this could be avoided, but
660          * it is not known if that still works with the "win98-reboot" problem.
661          */
662         enable_mmio(pioaddr, rp->quirks);
663 #endif
664
665         /* Turn off EEPROM-controlled wake-up (magic packet) */
666         if (rp->quirks & rqWOL)
667                 writeb(readb(ioaddr + ConfigA) & 0xFE, ioaddr + ConfigA);
668
669 }
670
671 #ifdef CONFIG_NET_POLL_CONTROLLER
672 static void rhine_poll(struct net_device *dev)
673 {
674         disable_irq(dev->irq);
675         rhine_interrupt(dev->irq, (void *)dev, NULL);
676         enable_irq(dev->irq);
677 }
678 #endif
679
680 static void rhine_hw_init(struct net_device *dev, long pioaddr)
681 {
682         struct rhine_private *rp = netdev_priv(dev);
683
684         /* Reset the chip to erase previous misconfiguration. */
685         rhine_chip_reset(dev);
686
687         /* Rhine-I needs extra time to recuperate before EEPROM reload */
688         if (rp->quirks & rqRhineI)
689                 msleep(5);
690
691         /* Reload EEPROM controlled bytes cleared by soft reset */
692         rhine_reload_eeprom(pioaddr, dev);
693 }
694
695 static int __devinit rhine_init_one(struct pci_dev *pdev,
696                                     const struct pci_device_id *ent)
697 {
698         struct net_device *dev;
699         struct rhine_private *rp;
700         int i, rc;
701         u8 pci_rev;
702         u32 quirks;
703         long pioaddr;
704         long memaddr;
705         long ioaddr;
706         int io_size, phy_id;
707         const char *name;
708
709 /* when built into the kernel, we only print version if device is found */
710 #ifndef MODULE
711         static int printed_version;
712         if (!printed_version++)
713                 printk(version);
714 #endif
715
716         pci_read_config_byte(pdev, PCI_REVISION_ID, &pci_rev);
717
718         io_size = 256;
719         phy_id = 0;
720         quirks = 0;
721         name = "Rhine";
722         if (pci_rev < VTunknown0) {
723                 quirks = rqRhineI;
724                 io_size = 128;
725         }
726         else if (pci_rev >= VT6102) {
727                 quirks = rqWOL | rqForceReset;
728                 if (pci_rev < VT6105) {
729                         name = "Rhine II";
730                         quirks |= rqStatusWBRace;       /* Rhine-II exclusive */
731                 }
732                 else {
733                         phy_id = 1;     /* Integrated PHY, phy_id fixed to 1 */
734                         if (pci_rev >= VT6105_B0)
735                                 quirks |= rq6patterns;
736                         if (pci_rev < VT6105M)
737                                 name = "Rhine III";
738                         else
739                                 name = "Rhine III (Management Adapter)";
740                 }
741         }
742
743         rc = pci_enable_device(pdev);
744         if (rc)
745                 goto err_out;
746
747         /* this should always be supported */
748         rc = pci_set_dma_mask(pdev, 0xffffffff);
749         if (rc) {
750                 printk(KERN_ERR "32-bit PCI DMA addresses not supported by "
751                        "the card!?\n");
752                 goto err_out;
753         }
754
755         /* sanity check */
756         if ((pci_resource_len(pdev, 0) < io_size) ||
757             (pci_resource_len(pdev, 1) < io_size)) {
758                 rc = -EIO;
759                 printk(KERN_ERR "Insufficient PCI resources, aborting\n");
760                 goto err_out;
761         }
762
763         pioaddr = pci_resource_start(pdev, 0);
764         memaddr = pci_resource_start(pdev, 1);
765
766         pci_set_master(pdev);
767
768         dev = alloc_etherdev(sizeof(struct rhine_private));
769         if (!dev) {
770                 rc = -ENOMEM;
771                 printk(KERN_ERR "alloc_etherdev failed\n");
772                 goto err_out;
773         }
774         SET_MODULE_OWNER(dev);
775         SET_NETDEV_DEV(dev, &pdev->dev);
776
777         rp = netdev_priv(dev);
778         rp->quirks = quirks;
779         rp->pioaddr = pioaddr;
780         rp->pdev = pdev;
781
782         rc = pci_request_regions(pdev, DRV_NAME);
783         if (rc)
784                 goto err_out_free_netdev;
785
786 #ifdef USE_MMIO
787         enable_mmio(pioaddr, quirks);
788
789         ioaddr = (long) ioremap(memaddr, io_size);
790         if (!ioaddr) {
791                 rc = -EIO;
792                 printk(KERN_ERR "ioremap failed for device %s, region 0x%X "
793                        "@ 0x%lX\n", pci_name(pdev), io_size, memaddr);
794                 goto err_out_free_res;
795         }
796
797         /* Check that selected MMIO registers match the PIO ones */
798         i = 0;
799         while (mmio_verify_registers[i]) {
800                 int reg = mmio_verify_registers[i++];
801                 unsigned char a = inb(pioaddr+reg);
802                 unsigned char b = readb(ioaddr+reg);
803                 if (a != b) {
804                         rc = -EIO;
805                         printk(KERN_ERR "MMIO do not match PIO [%02x] "
806                                "(%02x != %02x)\n", reg, a, b);
807                         goto err_out_unmap;
808                 }
809         }
810 #else
811         ioaddr = pioaddr;
812 #endif /* USE_MMIO */
813
814         dev->base_addr = ioaddr;
815
816         /* Get chip registers into a sane state */
817         rhine_power_init(dev);
818         rhine_hw_init(dev, pioaddr);
819
820         for (i = 0; i < 6; i++)
821                 dev->dev_addr[i] = readb(ioaddr + StationAddr + i);
822
823         if (!is_valid_ether_addr(dev->dev_addr)) {
824                 rc = -EIO;
825                 printk(KERN_ERR "Invalid MAC address\n");
826                 goto err_out_unmap;
827         }
828
829         /* For Rhine-I/II, phy_id is loaded from EEPROM */
830         if (!phy_id)
831                 phy_id = readb(ioaddr + 0x6C);
832
833         dev->irq = pdev->irq;
834
835         spin_lock_init(&rp->lock);
836         rp->mii_if.dev = dev;
837         rp->mii_if.mdio_read = mdio_read;
838         rp->mii_if.mdio_write = mdio_write;
839         rp->mii_if.phy_id_mask = 0x1f;
840         rp->mii_if.reg_num_mask = 0x1f;
841
842         /* The chip-specific entries in the device structure. */
843         dev->open = rhine_open;
844         dev->hard_start_xmit = rhine_start_tx;
845         dev->stop = rhine_close;
846         dev->get_stats = rhine_get_stats;
847         dev->set_multicast_list = rhine_set_rx_mode;
848         dev->do_ioctl = netdev_ioctl;
849         dev->ethtool_ops = &netdev_ethtool_ops;
850         dev->tx_timeout = rhine_tx_timeout;
851         dev->watchdog_timeo = TX_TIMEOUT;
852 #ifdef CONFIG_NET_POLL_CONTROLLER
853         dev->poll_controller = rhine_poll;
854 #endif
855         if (rp->quirks & rqRhineI)
856                 dev->features |= NETIF_F_SG|NETIF_F_HW_CSUM;
857
858         /* dev->name not defined before register_netdev()! */
859         rc = register_netdev(dev);
860         if (rc)
861                 goto err_out_unmap;
862
863         printk(KERN_INFO "%s: VIA %s at 0x%lx, ",
864                dev->name, name,
865 #ifdef USE_MMIO
866                 memaddr
867 #else
868                 ioaddr
869 #endif
870                  );
871
872         for (i = 0; i < 5; i++)
873                 printk("%2.2x:", dev->dev_addr[i]);
874         printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], pdev->irq);
875
876         pci_set_drvdata(pdev, dev);
877
878         {
879                 u16 mii_cmd;
880                 int mii_status = mdio_read(dev, phy_id, 1);
881                 mii_cmd = mdio_read(dev, phy_id, MII_BMCR) & ~BMCR_ISOLATE;
882                 mdio_write(dev, phy_id, MII_BMCR, mii_cmd);
883                 if (mii_status != 0xffff && mii_status != 0x0000) {
884                         rp->mii_if.advertising = mdio_read(dev, phy_id, 4);
885                         printk(KERN_INFO "%s: MII PHY found at address "
886                                "%d, status 0x%4.4x advertising %4.4x "
887                                "Link %4.4x.\n", dev->name, phy_id,
888                                mii_status, rp->mii_if.advertising,
889                                mdio_read(dev, phy_id, 5));
890
891                         /* set IFF_RUNNING */
892                         if (mii_status & BMSR_LSTATUS)
893                                 netif_carrier_on(dev);
894                         else
895                                 netif_carrier_off(dev);
896
897                 }
898         }
899         rp->mii_if.phy_id = phy_id;
900
901         return 0;
902
903 err_out_unmap:
904 #ifdef USE_MMIO
905         iounmap((void *)ioaddr);
906 err_out_free_res:
907 #endif
908         pci_release_regions(pdev);
909 err_out_free_netdev:
910         free_netdev(dev);
911 err_out:
912         return rc;
913 }
914
915 static int alloc_ring(struct net_device* dev)
916 {
917         struct rhine_private *rp = netdev_priv(dev);
918         void *ring;
919         dma_addr_t ring_dma;
920
921         ring = pci_alloc_consistent(rp->pdev,
922                                     RX_RING_SIZE * sizeof(struct rx_desc) +
923                                     TX_RING_SIZE * sizeof(struct tx_desc),
924                                     &ring_dma);
925         if (!ring) {
926                 printk(KERN_ERR "Could not allocate DMA memory.\n");
927                 return -ENOMEM;
928         }
929         if (rp->quirks & rqRhineI) {
930                 rp->tx_bufs = pci_alloc_consistent(rp->pdev,
931                                                    PKT_BUF_SZ * TX_RING_SIZE,
932                                                    &rp->tx_bufs_dma);
933                 if (rp->tx_bufs == NULL) {
934                         pci_free_consistent(rp->pdev,
935                                     RX_RING_SIZE * sizeof(struct rx_desc) +
936                                     TX_RING_SIZE * sizeof(struct tx_desc),
937                                     ring, ring_dma);
938                         return -ENOMEM;
939                 }
940         }
941
942         rp->rx_ring = ring;
943         rp->tx_ring = ring + RX_RING_SIZE * sizeof(struct rx_desc);
944         rp->rx_ring_dma = ring_dma;
945         rp->tx_ring_dma = ring_dma + RX_RING_SIZE * sizeof(struct rx_desc);
946
947         return 0;
948 }
949
950 void free_ring(struct net_device* dev)
951 {
952         struct rhine_private *rp = netdev_priv(dev);
953
954         pci_free_consistent(rp->pdev,
955                             RX_RING_SIZE * sizeof(struct rx_desc) +
956                             TX_RING_SIZE * sizeof(struct tx_desc),
957                             rp->rx_ring, rp->rx_ring_dma);
958         rp->tx_ring = NULL;
959
960         if (rp->tx_bufs)
961                 pci_free_consistent(rp->pdev, PKT_BUF_SZ * TX_RING_SIZE,
962                                     rp->tx_bufs, rp->tx_bufs_dma);
963
964         rp->tx_bufs = NULL;
965
966 }
967
968 static void alloc_rbufs(struct net_device *dev)
969 {
970         struct rhine_private *rp = netdev_priv(dev);
971         dma_addr_t next;
972         int i;
973
974         rp->dirty_rx = rp->cur_rx = 0;
975
976         rp->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
977         rp->rx_head_desc = &rp->rx_ring[0];
978         next = rp->rx_ring_dma;
979
980         /* Init the ring entries */
981         for (i = 0; i < RX_RING_SIZE; i++) {
982                 rp->rx_ring[i].rx_status = 0;
983                 rp->rx_ring[i].desc_length = cpu_to_le32(rp->rx_buf_sz);
984                 next += sizeof(struct rx_desc);
985                 rp->rx_ring[i].next_desc = cpu_to_le32(next);
986                 rp->rx_skbuff[i] = NULL;
987         }
988         /* Mark the last entry as wrapping the ring. */
989         rp->rx_ring[i-1].next_desc = cpu_to_le32(rp->rx_ring_dma);
990
991         /* Fill in the Rx buffers.  Handle allocation failure gracefully. */
992         for (i = 0; i < RX_RING_SIZE; i++) {
993                 struct sk_buff *skb = dev_alloc_skb(rp->rx_buf_sz);
994                 rp->rx_skbuff[i] = skb;
995                 if (skb == NULL)
996                         break;
997                 skb->dev = dev;                 /* Mark as being used by this device. */
998
999                 rp->rx_skbuff_dma[i] =
1000                         pci_map_single(rp->pdev, skb->tail, rp->rx_buf_sz,
1001                                        PCI_DMA_FROMDEVICE);
1002
1003                 rp->rx_ring[i].addr = cpu_to_le32(rp->rx_skbuff_dma[i]);
1004                 rp->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
1005         }
1006         rp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
1007 }
1008
1009 static void free_rbufs(struct net_device* dev)
1010 {
1011         struct rhine_private *rp = netdev_priv(dev);
1012         int i;
1013
1014         /* Free all the skbuffs in the Rx queue. */
1015         for (i = 0; i < RX_RING_SIZE; i++) {
1016                 rp->rx_ring[i].rx_status = 0;
1017                 rp->rx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
1018                 if (rp->rx_skbuff[i]) {
1019                         pci_unmap_single(rp->pdev,
1020                                          rp->rx_skbuff_dma[i],
1021                                          rp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1022                         dev_kfree_skb(rp->rx_skbuff[i]);
1023                 }
1024                 rp->rx_skbuff[i] = NULL;
1025         }
1026 }
1027
1028 static void alloc_tbufs(struct net_device* dev)
1029 {
1030         struct rhine_private *rp = netdev_priv(dev);
1031         dma_addr_t next;
1032         int i;
1033
1034         rp->dirty_tx = rp->cur_tx = 0;
1035         next = rp->tx_ring_dma;
1036         for (i = 0; i < TX_RING_SIZE; i++) {
1037                 rp->tx_skbuff[i] = NULL;
1038                 rp->tx_ring[i].tx_status = 0;
1039                 rp->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1040                 next += sizeof(struct tx_desc);
1041                 rp->tx_ring[i].next_desc = cpu_to_le32(next);
1042                 rp->tx_buf[i] = &rp->tx_bufs[i * PKT_BUF_SZ];
1043         }
1044         rp->tx_ring[i-1].next_desc = cpu_to_le32(rp->tx_ring_dma);
1045
1046 }
1047
1048 static void free_tbufs(struct net_device* dev)
1049 {
1050         struct rhine_private *rp = netdev_priv(dev);
1051         int i;
1052
1053         for (i = 0; i < TX_RING_SIZE; i++) {
1054                 rp->tx_ring[i].tx_status = 0;
1055                 rp->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1056                 rp->tx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
1057                 if (rp->tx_skbuff[i]) {
1058                         if (rp->tx_skbuff_dma[i]) {
1059                                 pci_unmap_single(rp->pdev,
1060                                                  rp->tx_skbuff_dma[i],
1061                                                  rp->tx_skbuff[i]->len,
1062                                                  PCI_DMA_TODEVICE);
1063                         }
1064                         dev_kfree_skb(rp->tx_skbuff[i]);
1065                 }
1066                 rp->tx_skbuff[i] = NULL;
1067                 rp->tx_buf[i] = NULL;
1068         }
1069 }
1070
1071 static void rhine_check_media(struct net_device *dev, unsigned int init_media)
1072 {
1073         struct rhine_private *rp = netdev_priv(dev);
1074         long ioaddr = dev->base_addr;
1075
1076         mii_check_media(&rp->mii_if, debug, init_media);
1077
1078         if (rp->mii_if.full_duplex)
1079             writeb(readb(ioaddr + ChipCmd1) | Cmd1FDuplex,
1080                    ioaddr + ChipCmd1);
1081         else
1082             writeb(readb(ioaddr + ChipCmd1) & ~Cmd1FDuplex,
1083                    ioaddr + ChipCmd1);
1084 }
1085
1086 static void init_registers(struct net_device *dev)
1087 {
1088         struct rhine_private *rp = netdev_priv(dev);
1089         long ioaddr = dev->base_addr;
1090         int i;
1091
1092         for (i = 0; i < 6; i++)
1093                 writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
1094
1095         /* Initialize other registers. */
1096         writew(0x0006, ioaddr + PCIBusConfig);  /* Tune configuration??? */
1097         /* Configure initial FIFO thresholds. */
1098         writeb(0x20, ioaddr + TxConfig);
1099         rp->tx_thresh = 0x20;
1100         rp->rx_thresh = 0x60;           /* Written in rhine_set_rx_mode(). */
1101
1102         writel(rp->rx_ring_dma, ioaddr + RxRingPtr);
1103         writel(rp->tx_ring_dma, ioaddr + TxRingPtr);
1104
1105         rhine_set_rx_mode(dev);
1106
1107         /* Enable interrupts by setting the interrupt mask. */
1108         writew(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow |
1109                IntrRxDropped | IntrRxNoBuf | IntrTxAborted |
1110                IntrTxDone | IntrTxError | IntrTxUnderrun |
1111                IntrPCIErr | IntrStatsMax | IntrLinkChange,
1112                ioaddr + IntrEnable);
1113
1114         writew(CmdStart | CmdTxOn | CmdRxOn | (Cmd1NoTxPoll << 8),
1115                ioaddr + ChipCmd);
1116         rhine_check_media(dev, 1);
1117 }
1118
1119 /* Enable MII link status auto-polling (required for IntrLinkChange) */
1120 static void rhine_enable_linkmon(long ioaddr)
1121 {
1122         writeb(0, ioaddr + MIICmd);
1123         writeb(MII_BMSR, ioaddr + MIIRegAddr);
1124         writeb(0x80, ioaddr + MIICmd);
1125
1126         RHINE_WAIT_FOR((readb(ioaddr + MIIRegAddr) & 0x20));
1127
1128         writeb(MII_BMSR | 0x40, ioaddr + MIIRegAddr);
1129 }
1130
1131 /* Disable MII link status auto-polling (required for MDIO access) */
1132 static void rhine_disable_linkmon(long ioaddr, u32 quirks)
1133 {
1134         writeb(0, ioaddr + MIICmd);
1135
1136         if (quirks & rqRhineI) {
1137                 writeb(0x01, ioaddr + MIIRegAddr);      // MII_BMSR
1138
1139                 /* Can be called from ISR. Evil. */
1140                 mdelay(1);
1141
1142                 /* 0x80 must be set immediately before turning it off */
1143                 writeb(0x80, ioaddr + MIICmd);
1144
1145                 RHINE_WAIT_FOR(readb(ioaddr + MIIRegAddr) & 0x20);
1146
1147                 /* Heh. Now clear 0x80 again. */
1148                 writeb(0, ioaddr + MIICmd);
1149         }
1150         else
1151                 RHINE_WAIT_FOR(readb(ioaddr + MIIRegAddr) & 0x80);
1152 }
1153
1154 /* Read and write over the MII Management Data I/O (MDIO) interface. */
1155
1156 static int mdio_read(struct net_device *dev, int phy_id, int regnum)
1157 {
1158         long ioaddr = dev->base_addr;
1159         struct rhine_private *rp = netdev_priv(dev);
1160         int result;
1161
1162         rhine_disable_linkmon(ioaddr, rp->quirks);
1163
1164         /* rhine_disable_linkmon already cleared MIICmd */
1165         writeb(phy_id, ioaddr + MIIPhyAddr);
1166         writeb(regnum, ioaddr + MIIRegAddr);
1167         writeb(0x40, ioaddr + MIICmd);          /* Trigger read */
1168         RHINE_WAIT_FOR(!(readb(ioaddr + MIICmd) & 0x40));
1169         result = readw(ioaddr + MIIData);
1170
1171         rhine_enable_linkmon(ioaddr);
1172         return result;
1173 }
1174
1175 static void mdio_write(struct net_device *dev, int phy_id, int regnum, int value)
1176 {
1177         struct rhine_private *rp = netdev_priv(dev);
1178         long ioaddr = dev->base_addr;
1179
1180         rhine_disable_linkmon(ioaddr, rp->quirks);
1181
1182         /* rhine_disable_linkmon already cleared MIICmd */
1183         writeb(phy_id, ioaddr + MIIPhyAddr);
1184         writeb(regnum, ioaddr + MIIRegAddr);
1185         writew(value, ioaddr + MIIData);
1186         writeb(0x20, ioaddr + MIICmd);          /* Trigger write */
1187         RHINE_WAIT_FOR(!(readb(ioaddr + MIICmd) & 0x20));
1188
1189         rhine_enable_linkmon(ioaddr);
1190 }
1191
1192 static int rhine_open(struct net_device *dev)
1193 {
1194         struct rhine_private *rp = netdev_priv(dev);
1195         long ioaddr = dev->base_addr;
1196         int rc;
1197
1198         rc = request_irq(rp->pdev->irq, &rhine_interrupt, SA_SHIRQ, dev->name,
1199                         dev);
1200         if (rc)
1201                 return rc;
1202
1203         if (debug > 1)
1204                 printk(KERN_DEBUG "%s: rhine_open() irq %d.\n",
1205                        dev->name, rp->pdev->irq);
1206
1207         rc = alloc_ring(dev);
1208         if (rc)
1209                 return rc;
1210         alloc_rbufs(dev);
1211         alloc_tbufs(dev);
1212         rhine_chip_reset(dev);
1213         init_registers(dev);
1214         if (debug > 2)
1215                 printk(KERN_DEBUG "%s: Done rhine_open(), status %4.4x "
1216                        "MII status: %4.4x.\n",
1217                        dev->name, readw(ioaddr + ChipCmd),
1218                        mdio_read(dev, rp->mii_if.phy_id, MII_BMSR));
1219
1220         netif_start_queue(dev);
1221
1222         return 0;
1223 }
1224
1225 static void rhine_tx_timeout(struct net_device *dev)
1226 {
1227         struct rhine_private *rp = netdev_priv(dev);
1228         long ioaddr = dev->base_addr;
1229
1230         printk(KERN_WARNING "%s: Transmit timed out, status %4.4x, PHY status "
1231                "%4.4x, resetting...\n",
1232                dev->name, readw(ioaddr + IntrStatus),
1233                mdio_read(dev, rp->mii_if.phy_id, MII_BMSR));
1234
1235         /* protect against concurrent rx interrupts */
1236         disable_irq(rp->pdev->irq);
1237
1238         spin_lock(&rp->lock);
1239
1240         /* clear all descriptors */
1241         free_tbufs(dev);
1242         free_rbufs(dev);
1243         alloc_tbufs(dev);
1244         alloc_rbufs(dev);
1245
1246         /* Reinitialize the hardware. */
1247         rhine_chip_reset(dev);
1248         init_registers(dev);
1249
1250         spin_unlock(&rp->lock);
1251         enable_irq(rp->pdev->irq);
1252
1253         dev->trans_start = jiffies;
1254         rp->stats.tx_errors++;
1255         netif_wake_queue(dev);
1256 }
1257
1258 static int rhine_start_tx(struct sk_buff *skb, struct net_device *dev)
1259 {
1260         struct rhine_private *rp = netdev_priv(dev);
1261         long ioaddr = dev->base_addr;
1262         unsigned entry;
1263
1264         /* Caution: the write order is important here, set the field
1265            with the "ownership" bits last. */
1266
1267         /* Calculate the next Tx descriptor entry. */
1268         entry = rp->cur_tx % TX_RING_SIZE;
1269
1270         if (skb->len < ETH_ZLEN) {
1271                 skb = skb_padto(skb, ETH_ZLEN);
1272                 if (skb == NULL)
1273                         return 0;
1274         }
1275
1276         rp->tx_skbuff[entry] = skb;
1277
1278         if ((rp->quirks & rqRhineI) &&
1279             (((long)skb->data & 3) || skb_shinfo(skb)->nr_frags != 0 || skb->ip_summed == CHECKSUM_HW)) {
1280                 /* Must use alignment buffer. */
1281                 if (skb->len > PKT_BUF_SZ) {
1282                         /* packet too long, drop it */
1283                         dev_kfree_skb(skb);
1284                         rp->tx_skbuff[entry] = NULL;
1285                         rp->stats.tx_dropped++;
1286                         return 0;
1287                 }
1288                 skb_copy_and_csum_dev(skb, rp->tx_buf[entry]);
1289                 rp->tx_skbuff_dma[entry] = 0;
1290                 rp->tx_ring[entry].addr = cpu_to_le32(rp->tx_bufs_dma +
1291                                                       (rp->tx_buf[entry] -
1292                                                        rp->tx_bufs));
1293         } else {
1294                 rp->tx_skbuff_dma[entry] =
1295                         pci_map_single(rp->pdev, skb->data, skb->len,
1296                                        PCI_DMA_TODEVICE);
1297                 rp->tx_ring[entry].addr = cpu_to_le32(rp->tx_skbuff_dma[entry]);
1298         }
1299
1300         rp->tx_ring[entry].desc_length =
1301                 cpu_to_le32(TXDESC | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
1302
1303         /* lock eth irq */
1304         spin_lock_irq(&rp->lock);
1305         wmb();
1306         rp->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1307         wmb();
1308
1309         rp->cur_tx++;
1310
1311         /* Non-x86 Todo: explicitly flush cache lines here. */
1312
1313         /* Wake the potentially-idle transmit channel */
1314         writeb(readb(ioaddr + ChipCmd1) | Cmd1TxDemand,
1315                ioaddr + ChipCmd1);
1316         IOSYNC;
1317
1318         if (rp->cur_tx == rp->dirty_tx + TX_QUEUE_LEN)
1319                 netif_stop_queue(dev);
1320
1321         dev->trans_start = jiffies;
1322
1323         spin_unlock_irq(&rp->lock);
1324
1325         if (debug > 4) {
1326                 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1327                        dev->name, rp->cur_tx-1, entry);
1328         }
1329         return 0;
1330 }
1331
1332 /* The interrupt handler does all of the Rx thread work and cleans up
1333    after the Tx thread. */
1334 static irqreturn_t rhine_interrupt(int irq, void *dev_instance, struct pt_regs *rgs)
1335 {
1336         struct net_device *dev = dev_instance;
1337         long ioaddr;
1338         u32 intr_status;
1339         int boguscnt = max_interrupt_work;
1340         int handled = 0;
1341
1342         ioaddr = dev->base_addr;
1343
1344         while ((intr_status = get_intr_status(dev))) {
1345                 handled = 1;
1346
1347                 /* Acknowledge all of the current interrupt sources ASAP. */
1348                 if (intr_status & IntrTxDescRace)
1349                         writeb(0x08, ioaddr + IntrStatus2);
1350                 writew(intr_status & 0xffff, ioaddr + IntrStatus);
1351                 IOSYNC;
1352
1353                 if (debug > 4)
1354                         printk(KERN_DEBUG "%s: Interrupt, status %8.8x.\n",
1355                                dev->name, intr_status);
1356
1357                 if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
1358                     IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf))
1359                         rhine_rx(dev);
1360
1361                 if (intr_status & (IntrTxErrSummary | IntrTxDone)) {
1362                         if (intr_status & IntrTxErrSummary) {
1363                                 /* Avoid scavenging before Tx engine turned off */
1364                                 RHINE_WAIT_FOR(!(readb(ioaddr+ChipCmd) & CmdTxOn));
1365                                 if (debug > 2 &&
1366                                     readb(ioaddr+ChipCmd) & CmdTxOn)
1367                                         printk(KERN_WARNING "%s: "
1368                                                "rhine_interrupt() Tx engine"
1369                                                "still on.\n", dev->name);
1370                         }
1371                         rhine_tx(dev);
1372                 }
1373
1374                 /* Abnormal error summary/uncommon events handlers. */
1375                 if (intr_status & (IntrPCIErr | IntrLinkChange |
1376                                    IntrStatsMax | IntrTxError | IntrTxAborted |
1377                                    IntrTxUnderrun | IntrTxDescRace))
1378                         rhine_error(dev, intr_status);
1379
1380                 if (--boguscnt < 0) {
1381                         printk(KERN_WARNING "%s: Too much work at interrupt, "
1382                                "status=%#8.8x.\n",
1383                                dev->name, intr_status);
1384                         break;
1385                 }
1386         }
1387
1388         if (debug > 3)
1389                 printk(KERN_DEBUG "%s: exiting interrupt, status=%8.8x.\n",
1390                        dev->name, readw(ioaddr + IntrStatus));
1391         return IRQ_RETVAL(handled);
1392 }
1393
1394 /* This routine is logically part of the interrupt handler, but isolated
1395    for clarity. */
1396 static void rhine_tx(struct net_device *dev)
1397 {
1398         struct rhine_private *rp = netdev_priv(dev);
1399         int txstatus = 0, entry = rp->dirty_tx % TX_RING_SIZE;
1400
1401         spin_lock(&rp->lock);
1402
1403         /* find and cleanup dirty tx descriptors */
1404         while (rp->dirty_tx != rp->cur_tx) {
1405                 txstatus = le32_to_cpu(rp->tx_ring[entry].tx_status);
1406                 if (debug > 6)
1407                         printk(KERN_DEBUG " Tx scavenge %d status %8.8x.\n",
1408                                entry, txstatus);
1409                 if (txstatus & DescOwn)
1410                         break;
1411                 if (txstatus & 0x8000) {
1412                         if (debug > 1)
1413                                 printk(KERN_DEBUG "%s: Transmit error, "
1414                                        "Tx status %8.8x.\n",
1415                                        dev->name, txstatus);
1416                         rp->stats.tx_errors++;
1417                         if (txstatus & 0x0400) rp->stats.tx_carrier_errors++;
1418                         if (txstatus & 0x0200) rp->stats.tx_window_errors++;
1419                         if (txstatus & 0x0100) rp->stats.tx_aborted_errors++;
1420                         if (txstatus & 0x0080) rp->stats.tx_heartbeat_errors++;
1421                         if (((rp->quirks & rqRhineI) && txstatus & 0x0002) ||
1422                             (txstatus & 0x0800) || (txstatus & 0x1000)) {
1423                                 rp->stats.tx_fifo_errors++;
1424                                 rp->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1425                                 break; /* Keep the skb - we try again */
1426                         }
1427                         /* Transmitter restarted in 'abnormal' handler. */
1428                 } else {
1429                         if (rp->quirks & rqRhineI)
1430                                 rp->stats.collisions += (txstatus >> 3) & 0x0F;
1431                         else
1432                                 rp->stats.collisions += txstatus & 0x0F;
1433                         if (debug > 6)
1434                                 printk(KERN_DEBUG "collisions: %1.1x:%1.1x\n",
1435                                        (txstatus >> 3) & 0xF,
1436                                        txstatus & 0xF);
1437                         rp->stats.tx_bytes += rp->tx_skbuff[entry]->len;
1438                         rp->stats.tx_packets++;
1439                 }
1440                 /* Free the original skb. */
1441                 if (rp->tx_skbuff_dma[entry]) {
1442                         pci_unmap_single(rp->pdev,
1443                                          rp->tx_skbuff_dma[entry],
1444                                          rp->tx_skbuff[entry]->len,
1445                                          PCI_DMA_TODEVICE);
1446                 }
1447                 dev_kfree_skb_irq(rp->tx_skbuff[entry]);
1448                 rp->tx_skbuff[entry] = NULL;
1449                 entry = (++rp->dirty_tx) % TX_RING_SIZE;
1450         }
1451         if ((rp->cur_tx - rp->dirty_tx) < TX_QUEUE_LEN - 4)
1452                 netif_wake_queue(dev);
1453
1454         spin_unlock(&rp->lock);
1455 }
1456
1457 /* This routine is logically part of the interrupt handler, but isolated
1458    for clarity and better register allocation. */
1459 static void rhine_rx(struct net_device *dev)
1460 {
1461         struct rhine_private *rp = netdev_priv(dev);
1462         int entry = rp->cur_rx % RX_RING_SIZE;
1463         int boguscnt = rp->dirty_rx + RX_RING_SIZE - rp->cur_rx;
1464
1465         if (debug > 4) {
1466                 printk(KERN_DEBUG "%s: rhine_rx(), entry %d status %8.8x.\n",
1467                        dev->name, entry,
1468                        le32_to_cpu(rp->rx_head_desc->rx_status));
1469         }
1470
1471         /* If EOP is set on the next entry, it's a new packet. Send it up. */
1472         while (!(rp->rx_head_desc->rx_status & cpu_to_le32(DescOwn))) {
1473                 struct rx_desc *desc = rp->rx_head_desc;
1474                 u32 desc_status = le32_to_cpu(desc->rx_status);
1475                 int data_size = desc_status >> 16;
1476
1477                 if (debug > 4)
1478                         printk(KERN_DEBUG " rhine_rx() status is %8.8x.\n",
1479                                desc_status);
1480                 if (--boguscnt < 0)
1481                         break;
1482                 if ((desc_status & (RxWholePkt | RxErr)) != RxWholePkt) {
1483                         if ((desc_status & RxWholePkt) != RxWholePkt) {
1484                                 printk(KERN_WARNING "%s: Oversized Ethernet "
1485                                        "frame spanned multiple buffers, entry "
1486                                        "%#x length %d status %8.8x!\n",
1487                                        dev->name, entry, data_size,
1488                                        desc_status);
1489                                 printk(KERN_WARNING "%s: Oversized Ethernet "
1490                                        "frame %p vs %p.\n", dev->name,
1491                                        rp->rx_head_desc, &rp->rx_ring[entry]);
1492                                 rp->stats.rx_length_errors++;
1493                         } else if (desc_status & RxErr) {
1494                                 /* There was a error. */
1495                                 if (debug > 2)
1496                                         printk(KERN_DEBUG " rhine_rx() Rx "
1497                                                "error was %8.8x.\n",
1498                                                desc_status);
1499                                 rp->stats.rx_errors++;
1500                                 if (desc_status & 0x0030) rp->stats.rx_length_errors++;
1501                                 if (desc_status & 0x0048) rp->stats.rx_fifo_errors++;
1502                                 if (desc_status & 0x0004) rp->stats.rx_frame_errors++;
1503                                 if (desc_status & 0x0002) {
1504                                         /* this can also be updated outside the interrupt handler */
1505                                         spin_lock(&rp->lock);
1506                                         rp->stats.rx_crc_errors++;
1507                                         spin_unlock(&rp->lock);
1508                                 }
1509                         }
1510                 } else {
1511                         struct sk_buff *skb;
1512                         /* Length should omit the CRC */
1513                         int pkt_len = data_size - 4;
1514
1515                         /* Check if the packet is long enough to accept without
1516                            copying to a minimally-sized skbuff. */
1517                         if (pkt_len < rx_copybreak &&
1518                                 (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1519                                 skb->dev = dev;
1520                                 skb_reserve(skb, 2);    /* 16 byte align the IP header */
1521                                 pci_dma_sync_single_for_cpu(rp->pdev,
1522                                                             rp->rx_skbuff_dma[entry],
1523                                                             rp->rx_buf_sz,
1524                                                             PCI_DMA_FROMDEVICE);
1525
1526                                 eth_copy_and_sum(skb,
1527                                                  rp->rx_skbuff[entry]->tail,
1528                                                  pkt_len, 0);
1529                                 skb_put(skb, pkt_len);
1530                                 pci_dma_sync_single_for_device(rp->pdev,
1531                                                                rp->rx_skbuff_dma[entry],
1532                                                                rp->rx_buf_sz,
1533                                                                PCI_DMA_FROMDEVICE);
1534                         } else {
1535                                 skb = rp->rx_skbuff[entry];
1536                                 if (skb == NULL) {
1537                                         printk(KERN_ERR "%s: Inconsistent Rx "
1538                                                "descriptor chain.\n",
1539                                                dev->name);
1540                                         break;
1541                                 }
1542                                 rp->rx_skbuff[entry] = NULL;
1543                                 skb_put(skb, pkt_len);
1544                                 pci_unmap_single(rp->pdev,
1545                                                  rp->rx_skbuff_dma[entry],
1546                                                  rp->rx_buf_sz,
1547                                                  PCI_DMA_FROMDEVICE);
1548                         }
1549                         skb->protocol = eth_type_trans(skb, dev);
1550                         netif_rx(skb);
1551                         dev->last_rx = jiffies;
1552                         rp->stats.rx_bytes += pkt_len;
1553                         rp->stats.rx_packets++;
1554                 }
1555                 entry = (++rp->cur_rx) % RX_RING_SIZE;
1556                 rp->rx_head_desc = &rp->rx_ring[entry];
1557         }
1558
1559         /* Refill the Rx ring buffers. */
1560         for (; rp->cur_rx - rp->dirty_rx > 0; rp->dirty_rx++) {
1561                 struct sk_buff *skb;
1562                 entry = rp->dirty_rx % RX_RING_SIZE;
1563                 if (rp->rx_skbuff[entry] == NULL) {
1564                         skb = dev_alloc_skb(rp->rx_buf_sz);
1565                         rp->rx_skbuff[entry] = skb;
1566                         if (skb == NULL)
1567                                 break;  /* Better luck next round. */
1568                         skb->dev = dev; /* Mark as being used by this device. */
1569                         rp->rx_skbuff_dma[entry] =
1570                                 pci_map_single(rp->pdev, skb->tail,
1571                                                rp->rx_buf_sz,
1572                                                PCI_DMA_FROMDEVICE);
1573                         rp->rx_ring[entry].addr = cpu_to_le32(rp->rx_skbuff_dma[entry]);
1574                 }
1575                 rp->rx_ring[entry].rx_status = cpu_to_le32(DescOwn);
1576         }
1577 }
1578
1579 /*
1580  * Clears the "tally counters" for CRC errors and missed frames(?).
1581  * It has been reported that some chips need a write of 0 to clear
1582  * these, for others the counters are set to 1 when written to and
1583  * instead cleared when read. So we clear them both ways ...
1584  */
1585 static inline void clear_tally_counters(const long ioaddr)
1586 {
1587         writel(0, ioaddr + RxMissed);
1588         readw(ioaddr + RxCRCErrs);
1589         readw(ioaddr + RxMissed);
1590 }
1591
1592 static void rhine_restart_tx(struct net_device *dev) {
1593         struct rhine_private *rp = netdev_priv(dev);
1594         long ioaddr = dev->base_addr;
1595         int entry = rp->dirty_tx % TX_RING_SIZE;
1596         u32 intr_status;
1597
1598         /*
1599          * If new errors occured, we need to sort them out before doing Tx.
1600          * In that case the ISR will be back here RSN anyway.
1601          */
1602         intr_status = get_intr_status(dev);
1603
1604         if ((intr_status & IntrTxErrSummary) == 0) {
1605
1606                 /* We know better than the chip where it should continue. */
1607                 writel(rp->tx_ring_dma + entry * sizeof(struct tx_desc),
1608                        ioaddr + TxRingPtr);
1609
1610                 writeb(readb(ioaddr + ChipCmd) | CmdTxOn,
1611                        ioaddr + ChipCmd);
1612                 writeb(readb(ioaddr + ChipCmd1) | Cmd1TxDemand,
1613                        ioaddr + ChipCmd1);
1614                 IOSYNC;
1615         }
1616         else {
1617                 /* This should never happen */
1618                 if (debug > 1)
1619                         printk(KERN_WARNING "%s: rhine_restart_tx() "
1620                                "Another error occured %8.8x.\n",
1621                                dev->name, intr_status);
1622         }
1623
1624 }
1625
1626 static void rhine_error(struct net_device *dev, int intr_status)
1627 {
1628         struct rhine_private *rp = netdev_priv(dev);
1629         long ioaddr = dev->base_addr;
1630
1631         spin_lock(&rp->lock);
1632
1633         if (intr_status & IntrLinkChange)
1634                 rhine_check_media(dev, 0);
1635         if (intr_status & IntrStatsMax) {
1636                 rp->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
1637                 rp->stats.rx_missed_errors += readw(ioaddr + RxMissed);
1638                 clear_tally_counters(ioaddr);
1639         }
1640         if (intr_status & IntrTxAborted) {
1641                 if (debug > 1)
1642                         printk(KERN_INFO "%s: Abort %8.8x, frame dropped.\n",
1643                                dev->name, intr_status);
1644         }
1645         if (intr_status & IntrTxUnderrun) {
1646                 if (rp->tx_thresh < 0xE0)
1647                         writeb(rp->tx_thresh += 0x20, ioaddr + TxConfig);
1648                 if (debug > 1)
1649                         printk(KERN_INFO "%s: Transmitter underrun, Tx "
1650                                "threshold now %2.2x.\n",
1651                                dev->name, rp->tx_thresh);
1652         }
1653         if (intr_status & IntrTxDescRace) {
1654                 if (debug > 2)
1655                         printk(KERN_INFO "%s: Tx descriptor write-back race.\n",
1656                                dev->name);
1657         }
1658         if ((intr_status & IntrTxError) &&
1659             (intr_status & (IntrTxAborted |
1660              IntrTxUnderrun | IntrTxDescRace)) == 0) {
1661                 if (rp->tx_thresh < 0xE0) {
1662                         writeb(rp->tx_thresh += 0x20, ioaddr + TxConfig);
1663                 }
1664                 if (debug > 1)
1665                         printk(KERN_INFO "%s: Unspecified error. Tx "
1666                                "threshold now %2.2x.\n",
1667                                dev->name, rp->tx_thresh);
1668         }
1669         if (intr_status & (IntrTxAborted | IntrTxUnderrun | IntrTxDescRace |
1670                            IntrTxError))
1671                 rhine_restart_tx(dev);
1672
1673         if (intr_status & ~(IntrLinkChange | IntrStatsMax | IntrTxUnderrun |
1674                             IntrTxError | IntrTxAborted | IntrNormalSummary |
1675                             IntrTxDescRace)) {
1676                 if (debug > 1)
1677                         printk(KERN_ERR "%s: Something Wicked happened! "
1678                                "%8.8x.\n", dev->name, intr_status);
1679         }
1680
1681         spin_unlock(&rp->lock);
1682 }
1683
1684 static struct net_device_stats *rhine_get_stats(struct net_device *dev)
1685 {
1686         struct rhine_private *rp = netdev_priv(dev);
1687         long ioaddr = dev->base_addr;
1688         unsigned long flags;
1689
1690         spin_lock_irqsave(&rp->lock, flags);
1691         rp->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
1692         rp->stats.rx_missed_errors += readw(ioaddr + RxMissed);
1693         clear_tally_counters(ioaddr);
1694         spin_unlock_irqrestore(&rp->lock, flags);
1695
1696         return &rp->stats;
1697 }
1698
1699 static void rhine_set_rx_mode(struct net_device *dev)
1700 {
1701         struct rhine_private *rp = netdev_priv(dev);
1702         long ioaddr = dev->base_addr;
1703         u32 mc_filter[2];       /* Multicast hash filter */
1704         u8 rx_mode;             /* Note: 0x02=accept runt, 0x01=accept errs */
1705
1706         if (dev->flags & IFF_PROMISC) {         /* Set promiscuous. */
1707                 /* Unconditionally log net taps. */
1708                 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
1709                        dev->name);
1710                 rx_mode = 0x1C;
1711                 writel(0xffffffff, ioaddr + MulticastFilter0);
1712                 writel(0xffffffff, ioaddr + MulticastFilter1);
1713         } else if ((dev->mc_count > multicast_filter_limit)
1714                    || (dev->flags & IFF_ALLMULTI)) {
1715                 /* Too many to match, or accept all multicasts. */
1716                 writel(0xffffffff, ioaddr + MulticastFilter0);
1717                 writel(0xffffffff, ioaddr + MulticastFilter1);
1718                 rx_mode = 0x0C;
1719         } else {
1720                 struct dev_mc_list *mclist;
1721                 int i;
1722                 memset(mc_filter, 0, sizeof(mc_filter));
1723                 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1724                      i++, mclist = mclist->next) {
1725                         int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
1726
1727                         mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1728                 }
1729                 writel(mc_filter[0], ioaddr + MulticastFilter0);
1730                 writel(mc_filter[1], ioaddr + MulticastFilter1);
1731                 rx_mode = 0x0C;
1732         }
1733         writeb(rp->rx_thresh | rx_mode, ioaddr + RxConfig);
1734 }
1735
1736 static void netdev_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1737 {
1738         struct rhine_private *rp = netdev_priv(dev);
1739
1740         strcpy(info->driver, DRV_NAME);
1741         strcpy(info->version, DRV_VERSION);
1742         strcpy(info->bus_info, pci_name(rp->pdev));
1743 }
1744
1745 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1746 {
1747         struct rhine_private *rp = netdev_priv(dev);
1748         int rc;
1749
1750         spin_lock_irq(&rp->lock);
1751         rc = mii_ethtool_gset(&rp->mii_if, cmd);
1752         spin_unlock_irq(&rp->lock);
1753
1754         return rc;
1755 }
1756
1757 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1758 {
1759         struct rhine_private *rp = netdev_priv(dev);
1760         int rc;
1761
1762         spin_lock_irq(&rp->lock);
1763         rc = mii_ethtool_sset(&rp->mii_if, cmd);
1764         spin_unlock_irq(&rp->lock);
1765
1766         return rc;
1767 }
1768
1769 static int netdev_nway_reset(struct net_device *dev)
1770 {
1771         struct rhine_private *rp = netdev_priv(dev);
1772
1773         return mii_nway_restart(&rp->mii_if);
1774 }
1775
1776 static u32 netdev_get_link(struct net_device *dev)
1777 {
1778         struct rhine_private *rp = netdev_priv(dev);
1779
1780         return mii_link_ok(&rp->mii_if);
1781 }
1782
1783 static u32 netdev_get_msglevel(struct net_device *dev)
1784 {
1785         return debug;
1786 }
1787
1788 static void netdev_set_msglevel(struct net_device *dev, u32 value)
1789 {
1790         debug = value;
1791 }
1792
1793 static void rhine_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1794 {
1795         struct rhine_private *rp = netdev_priv(dev);
1796
1797         if (!(rp->quirks & rqWOL))
1798                 return;
1799
1800         spin_lock_irq(&rp->lock);
1801         wol->supported = WAKE_PHY | WAKE_MAGIC |
1802                          WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;  /* Untested */
1803         wol->wolopts = rp->wolopts;
1804         spin_unlock_irq(&rp->lock);
1805 }
1806
1807 static int rhine_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1808 {
1809         struct rhine_private *rp = netdev_priv(dev);
1810         u32 support = WAKE_PHY | WAKE_MAGIC |
1811                       WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;     /* Untested */
1812
1813         if (!(rp->quirks & rqWOL))
1814                 return -EINVAL;
1815
1816         if (wol->wolopts & ~support)
1817                 return -EINVAL;
1818
1819         spin_lock_irq(&rp->lock);
1820         rp->wolopts = wol->wolopts;
1821         spin_unlock_irq(&rp->lock);
1822
1823         return 0;
1824 }
1825
1826 static struct ethtool_ops netdev_ethtool_ops = {
1827         .get_drvinfo            = netdev_get_drvinfo,
1828         .get_settings           = netdev_get_settings,
1829         .set_settings           = netdev_set_settings,
1830         .nway_reset             = netdev_nway_reset,
1831         .get_link               = netdev_get_link,
1832         .get_msglevel           = netdev_get_msglevel,
1833         .set_msglevel           = netdev_set_msglevel,
1834         .get_wol                = rhine_get_wol,
1835         .set_wol                = rhine_set_wol,
1836         .get_sg                 = ethtool_op_get_sg,
1837         .get_tx_csum            = ethtool_op_get_tx_csum,
1838 };
1839
1840 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1841 {
1842         struct rhine_private *rp = netdev_priv(dev);
1843         int rc;
1844
1845         if (!netif_running(dev))
1846                 return -EINVAL;
1847
1848         spin_lock_irq(&rp->lock);
1849         rc = generic_mii_ioctl(&rp->mii_if, if_mii(rq), cmd, NULL);
1850         spin_unlock_irq(&rp->lock);
1851
1852         return rc;
1853 }
1854
1855 static int rhine_close(struct net_device *dev)
1856 {
1857         long ioaddr = dev->base_addr;
1858         struct rhine_private *rp = netdev_priv(dev);
1859
1860         spin_lock_irq(&rp->lock);
1861
1862         netif_stop_queue(dev);
1863
1864         if (debug > 1)
1865                 printk(KERN_DEBUG "%s: Shutting down ethercard, "
1866                        "status was %4.4x.\n",
1867                        dev->name, readw(ioaddr + ChipCmd));
1868
1869         /* Switch to loopback mode to avoid hardware races. */
1870         writeb(rp->tx_thresh | 0x02, ioaddr + TxConfig);
1871
1872         /* Disable interrupts by clearing the interrupt mask. */
1873         writew(0x0000, ioaddr + IntrEnable);
1874
1875         /* Stop the chip's Tx and Rx processes. */
1876         writew(CmdStop, ioaddr + ChipCmd);
1877
1878         spin_unlock_irq(&rp->lock);
1879
1880         free_irq(rp->pdev->irq, dev);
1881         free_rbufs(dev);
1882         free_tbufs(dev);
1883         free_ring(dev);
1884
1885         return 0;
1886 }
1887
1888
1889 static void __devexit rhine_remove_one(struct pci_dev *pdev)
1890 {
1891         struct net_device *dev = pci_get_drvdata(pdev);
1892
1893         unregister_netdev(dev);
1894
1895         pci_release_regions(pdev);
1896
1897 #ifdef USE_MMIO
1898         iounmap((char *)(dev->base_addr));
1899 #endif
1900
1901         free_netdev(dev);
1902         pci_disable_device(pdev);
1903         pci_set_drvdata(pdev, NULL);
1904 }
1905
1906 static void rhine_shutdown (struct device *gendev)
1907 {
1908         struct pci_dev *pdev = to_pci_dev(gendev);
1909         struct net_device *dev = pci_get_drvdata(pdev);
1910         struct rhine_private *rp = netdev_priv(dev);
1911
1912         long ioaddr = dev->base_addr;
1913
1914         rhine_power_init(dev);
1915
1916         /* Make sure we use pattern 0, 1 and not 4, 5 */
1917         if (rp->quirks & rq6patterns)
1918                 writeb(0x04, ioaddr + 0xA7);
1919
1920         if (rp->wolopts & WAKE_MAGIC)
1921                 writeb(WOLmagic, ioaddr + WOLcrSet);
1922
1923         if (rp->wolopts & (WAKE_BCAST|WAKE_MCAST))
1924                 writeb(WOLbmcast, ioaddr + WOLcgSet);
1925
1926         if (rp->wolopts & WAKE_PHY)
1927                 writeb(WOLlnkon | WOLlnkoff, ioaddr + WOLcrSet);
1928
1929         if (rp->wolopts & WAKE_UCAST)
1930                 writeb(WOLucast, ioaddr + WOLcrSet);
1931
1932         /* Enable legacy WOL (for old motherboards) */
1933         writeb(0x01, ioaddr + PwcfgSet);
1934         writeb(readb(ioaddr + StickyHW) | 0x04, ioaddr + StickyHW);
1935
1936         /* Hit power state D3 (sleep) */
1937         writeb(readb(ioaddr + StickyHW) | 0x03, ioaddr + StickyHW);
1938
1939         /* TODO: Check use of pci_enable_wake() */
1940
1941 }
1942
1943 #ifdef CONFIG_PM
1944 static int rhine_suspend(struct pci_dev *pdev, u32 state)
1945 {
1946         struct net_device *dev = pci_get_drvdata(pdev);
1947         struct rhine_private *rp = netdev_priv(dev);
1948         unsigned long flags;
1949
1950         if (!netif_running(dev))
1951                 return 0;
1952
1953         netif_device_detach(dev);
1954         pci_save_state(pdev, pdev->saved_config_space);
1955
1956         spin_lock_irqsave(&rp->lock, flags);
1957         rhine_shutdown(&pdev->dev);
1958         spin_unlock_irqrestore(&rp->lock, flags);
1959
1960         return 0;
1961 }
1962
1963 static int rhine_resume(struct pci_dev *pdev)
1964 {
1965         struct net_device *dev = pci_get_drvdata(pdev);
1966         struct rhine_private *rp = netdev_priv(dev);
1967         unsigned long flags;
1968         int ret;
1969
1970         if (!netif_running(dev))
1971                 return 0;
1972
1973         ret = pci_set_power_state(pdev, 0);
1974         if (debug > 1)
1975                 printk(KERN_INFO "%s: Entering power state D0 %s (%d).\n",
1976                         dev->name, ret ? "failed" : "succeeded", ret);
1977
1978         pci_restore_state(pdev, pdev->saved_config_space);
1979
1980         spin_lock_irqsave(&rp->lock, flags);
1981 #ifdef USE_MMIO
1982         enable_mmio(rp->pioaddr, rp->quirks);
1983 #endif
1984         rhine_power_init(dev);
1985         free_tbufs(dev);
1986         free_rbufs(dev);
1987         alloc_tbufs(dev);
1988         alloc_rbufs(dev);
1989         init_registers(dev);
1990         spin_unlock_irqrestore(&rp->lock, flags);
1991
1992         netif_device_attach(dev);
1993
1994         return 0;
1995 }
1996 #endif /* CONFIG_PM */
1997
1998 static struct pci_driver rhine_driver = {
1999         .name           = DRV_NAME,
2000         .id_table       = rhine_pci_tbl,
2001         .probe          = rhine_init_one,
2002         .remove         = __devexit_p(rhine_remove_one),
2003 #ifdef CONFIG_PM
2004         .suspend        = rhine_suspend,
2005         .resume         = rhine_resume,
2006 #endif /* CONFIG_PM */
2007         .driver = {
2008                 .shutdown = rhine_shutdown,
2009         }
2010 };
2011
2012
2013 static int __init rhine_init(void)
2014 {
2015 /* when a module, this is printed whether or not devices are found in probe */
2016 #ifdef MODULE
2017         printk(version);
2018 #endif
2019         return pci_module_init(&rhine_driver);
2020 }
2021
2022
2023 static void __exit rhine_cleanup(void)
2024 {
2025         pci_unregister_driver(&rhine_driver);
2026 }
2027
2028
2029 module_init(rhine_init);
2030 module_exit(rhine_cleanup);