1 /* via-rhine.c: A Linux Ethernet device driver for VIA Rhine family chips. */
3 Written 1998-2001 by Donald Becker.
5 This software may be used and distributed according to the terms of
6 the GNU General Public License (GPL), incorporated herein by reference.
7 Drivers based on or derived from this code fall under the GPL and must
8 retain the authorship, copyright and license notice. This file is not
9 a complete program and may only be used when the entire operating
10 system is licensed under the GPL.
12 This driver is designed for the VIA VT86C100A Rhine-I.
13 It also works with the 6102 Rhine-II, and 6105/6105M Rhine-III.
15 The author may be reached as becker@scyld.com, or C/O
16 Scyld Computing Corporation
17 410 Severn Ave., Suite 210
21 This driver contains some changes from the original Donald Becker
22 version. He may or may not be interested in bug reports on this
23 code. You can find his versions at:
24 http://www.scyld.com/network/via-rhine.html
27 Linux kernel version history:
30 - Jeff Garzik: softnet 'n stuff
33 - Justin Guyett: softnet and locking fixes
34 - Jeff Garzik: use PCI interface
37 - Urban Widmark: minor cleanups, merges from Becker 1.03a/1.04 versions
40 - Urban Widmark: use PCI DMA interface (with thanks to the eepro100.c
41 code) update "Theory of Operation" with
42 softnet/locking changes
43 - Dave Miller: PCI DMA and endian fixups
44 - Jeff Garzik: MOD_xxx race fixes, updated PCI resource allocation
47 - Urban Widmark: fix gcc 2.95.2 problem and
48 remove writel's to fixed address 0x7c
51 - Urban Widmark: mdio locking, bounce buffer changes
52 merges from Beckers 1.05 version
53 added netif_running_on/off support
56 - Urban Widmark: merges from Beckers 1.08b version (VT6102 + mdio)
57 set netif_running_on/off on startup, del_timer_sync
60 - Manfred Spraul: added reset into tx_timeout
63 - Urban Widmark: merges from Beckers 1.10 version
64 (media selection + eeprom reload)
65 - David Vrabel: merges from D-Link "1.11" version
66 (disable WOL and PME on startup)
69 - Manfred Spraul: use "singlecopy" for unaligned buffers
70 don't allocate bounce buffers for !ReqTxAlign cards
73 - David Woodhouse: Set dev->base_addr before the first time we call
74 wait_for_reset(). It's a lot happier that way.
75 Free np->tx_bufs only if we actually allocated it.
78 - Martin Eriksson: Allow Memory-Mapped IO to be enabled.
82 - Replace some MII-related magic numbers with constants
85 - fixes comments for Rhine-III
86 - removes W_MAX_TIMEOUT (unused)
87 - adds HasDavicomPhy for Rhine-I (basis: linuxfet driver; my card
88 is R-I and has Davicom chip, flag is referenced in kernel driver)
89 - sends chip_id as a parameter to wait_for_reset since np is not
90 initialized on first call
91 - changes mmio "else if (chip_id==VT6102)" to "else" so it will work
92 for Rhine-III's (documentation says same bit is correct)
93 - transmit frame queue message is off by one - fixed
94 - adds IntrNormalSummary to "Something Wicked" exclusion list
95 so normal interrupts will not trigger the message (src: Donald Becker)
97 - show confused chip where to continue after Tx error
98 - location of collision counter is chip specific
99 - allow selecting backoff algorithm (module parameter)
102 - Use new MII lib helper generic_mii_ioctl
104 LK1.1.16 (Roger Luethi)
106 - Handle Tx buffer underrun
107 - Fix bugs in full duplex handling
108 - New reset code uses "force reset" cmd on Rhine-II
111 LK1.1.17 (Roger Luethi)
112 - Fix race in via_rhine_start_tx()
113 - On errors, wait for Tx engine to turn off before scavenging
114 - Handle Tx descriptor write-back race on Rhine-II
115 - Force flushing for PCI posted writes
116 - More reset code changes
120 #define DRV_NAME "via-rhine"
121 #define DRV_VERSION "1.1.17"
122 #define DRV_RELDATE "March-1-2003"
125 /* A few user-configurable values.
126 These may be modified when a driver module is loaded. */
128 static int debug
= 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
129 static int max_interrupt_work
= 20;
131 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
132 Setting to > 1518 effectively disables this feature. */
133 static int rx_copybreak
;
135 /* Select a backoff algorithm (Ethernet capture effect) */
138 /* Used to pass the media type, etc.
139 Both 'options[]' and 'full_duplex[]' should exist for driver
141 The media type is usually passed in 'options[]'.
142 The default is autonegotiation for speed and duplex.
143 This should rarely be overridden.
144 Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps.
145 Use option values 0x10 and 0x100 for forcing half duplex fixed speed.
146 Use option values 0x20 and 0x200 for forcing full duplex operation.
148 #define MAX_UNITS 8 /* More are supported, limit only on options */
149 static int options
[MAX_UNITS
] = {-1, -1, -1, -1, -1, -1, -1, -1};
150 static int full_duplex
[MAX_UNITS
] = {-1, -1, -1, -1, -1, -1, -1, -1};
152 /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
153 The Rhine has a 64 element 8390-like hash table. */
154 static const int multicast_filter_limit
= 32;
157 /* Operational parameters that are set at compile time. */
159 /* Keep the ring sizes a power of two for compile efficiency.
160 The compiler will convert <unsigned>'%'<2^N> into a bit mask.
161 Making the Tx ring too large decreases the effectiveness of channel
162 bonding and packet priority.
163 There are no ill effects from too-large receive rings. */
164 #define TX_RING_SIZE 16
165 #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
166 #define RX_RING_SIZE 16
169 /* Operational parameters that usually are not changed. */
171 /* Time in jiffies before concluding the transmitter is hung. */
172 #define TX_TIMEOUT (2*HZ)
174 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
176 #if !defined(__OPTIMIZE__) || !defined(__KERNEL__)
177 #warning You must compile this file with the correct options!
178 #warning See the last lines of the source file.
179 #error You must compile this driver with "-O".
182 #include <linux/module.h>
183 #include <linux/kernel.h>
184 #include <linux/string.h>
185 #include <linux/timer.h>
186 #include <linux/errno.h>
187 #include <linux/ioport.h>
188 #include <linux/slab.h>
189 #include <linux/interrupt.h>
190 #include <linux/pci.h>
191 #include <linux/netdevice.h>
192 #include <linux/etherdevice.h>
193 #include <linux/skbuff.h>
194 #include <linux/init.h>
195 #include <linux/delay.h>
196 #include <linux/mii.h>
197 #include <linux/ethtool.h>
198 #include <linux/crc32.h>
199 #include <asm/processor.h> /* Processor type for cache alignment. */
200 #include <asm/bitops.h>
203 #include <asm/uaccess.h>
205 /* These identify the driver base version and may not be removed. */
206 static char version
[] __devinitdata
=
207 KERN_INFO DRV_NAME
".c:v1.10-LK" DRV_VERSION
" " DRV_RELDATE
" Written by Donald Becker\n"
208 KERN_INFO
" http://www.scyld.com/network/via-rhine.html\n";
210 static char shortname
[] = DRV_NAME
;
213 /* This driver was written to use PCI memory space, however most versions
214 of the Rhine only work correctly with I/O space accesses. */
215 #ifdef CONFIG_VIA_RHINE_MMIO
233 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
234 MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
235 MODULE_LICENSE("GPL");
237 MODULE_PARM(max_interrupt_work
, "i");
238 MODULE_PARM(debug
, "i");
239 MODULE_PARM(rx_copybreak
, "i");
240 MODULE_PARM(backoff
, "i");
241 MODULE_PARM(options
, "1-" __MODULE_STRING(MAX_UNITS
) "i");
242 MODULE_PARM(full_duplex
, "1-" __MODULE_STRING(MAX_UNITS
) "i");
243 MODULE_PARM_DESC(max_interrupt_work
, "VIA Rhine maximum events handled per interrupt");
244 MODULE_PARM_DESC(debug
, "VIA Rhine debug level (0-7)");
245 MODULE_PARM_DESC(rx_copybreak
, "VIA Rhine copy breakpoint for copy-only-tiny-frames");
246 MODULE_PARM_DESC(backoff
, "VIA Rhine: Bits 0-3: backoff algorithm");
247 MODULE_PARM_DESC(options
, "VIA Rhine: Bits 0-3: media type, bit 17: full duplex");
248 MODULE_PARM_DESC(full_duplex
, "VIA Rhine full duplex setting(s) (1)");
253 I. Board Compatibility
255 This driver is designed for the VIA 86c100A Rhine-II PCI Fast Ethernet
258 II. Board-specific settings
260 Boards with this chip are functional only in a bus-master PCI slot.
262 Many operational settings are loaded from the EEPROM to the Config word at
263 offset 0x78. For most of these settings, this driver assumes that they are
265 If this driver is compiled to use PCI memory space operations the EEPROM
266 must be configured to enable memory ops.
268 III. Driver operation
272 This driver uses two statically allocated fixed-size descriptor lists
273 formed into rings by a branch from the final descriptor to the beginning of
274 the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
276 IIIb/c. Transmit/Receive Structure
278 This driver attempts to use a zero-copy receive and transmit scheme.
280 Alas, all data buffers are required to start on a 32 bit boundary, so
281 the driver must often copy transmit packets into bounce buffers.
283 The driver allocates full frame size skbuffs for the Rx ring buffers at
284 open() time and passes the skb->data field to the chip as receive data
285 buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
286 a fresh skbuff is allocated and the frame is copied to the new skbuff.
287 When the incoming frame is larger, the skbuff is passed directly up the
288 protocol stack. Buffers consumed this way are replaced by newly allocated
289 skbuffs in the last phase of via_rhine_rx().
291 The RX_COPYBREAK value is chosen to trade-off the memory wasted by
292 using a full-sized skbuff for small frames vs. the copying costs of larger
293 frames. New boards are typically used in generously configured machines
294 and the underfilled buffers have negligible impact compared to the benefit of
295 a single allocation size, so the default value of zero results in never
296 copying packets. When copying is done, the cost is usually mitigated by using
297 a combined copy/checksum routine. Copying also preloads the cache, which is
298 most useful with small frames.
300 Since the VIA chips are only able to transfer data to buffers on 32 bit
301 boundaries, the IP header at offset 14 in an ethernet frame isn't
302 longword aligned for further processing. Copying these unaligned buffers
303 has the beneficial effect of 16-byte aligning the IP header.
305 IIId. Synchronization
307 The driver runs as two independent, single-threaded flows of control. One
308 is the send-packet routine, which enforces single-threaded use by the
309 dev->priv->lock spinlock. The other thread is the interrupt handler, which
310 is single threaded by the hardware and interrupt handling software.
312 The send packet thread has partial control over the Tx ring. It locks the
313 dev->priv->lock whenever it's queuing a Tx packet. If the next slot in the ring
314 is not available it stops the transmit queue by calling netif_stop_queue.
316 The interrupt handler has exclusive control over the Rx ring and records stats
317 from the Tx ring. After reaping the stats, it marks the Tx queue entry as
318 empty by incrementing the dirty_tx mark. If at least half of the entries in
319 the Rx ring are available the transmit queue is woken up if it was stopped.
325 Preliminary VT86C100A manual from http://www.via.com.tw/
326 http://www.scyld.com/expert/100mbps.html
327 http://www.scyld.com/expert/NWay.html
328 ftp://ftp.via.com.tw/public/lan/Products/NIC/VT86C100A/Datasheet/VT86C100A03.pdf
329 ftp://ftp.via.com.tw/public/lan/Products/NIC/VT6102/Datasheet/VT6102_021.PDF
334 The VT86C100A manual is not reliable information.
335 The 3043 chip does not handle unaligned transmit or receive buffers, resulting
336 in significant performance degradation for bounce buffer copies on transmit
337 and unaligned IP headers on receive.
338 The chip does not pad to minimum transmit length.
343 /* This table drives the PCI probe routines. It's mostly boilerplate in all
344 of the drivers, and will likely be provided by some future kernel.
345 Note the matching code -- the first table entry matchs all 56** cards but
346 second only the 1234 card.
350 PCI_USES_IO
=1, PCI_USES_MEM
=2, PCI_USES_MASTER
=4,
351 PCI_ADDR0
=0x10<<0, PCI_ADDR1
=0x10<<1, PCI_ADDR2
=0x10<<2, PCI_ADDR3
=0x10<<3,
354 enum via_rhine_chips
{
361 struct via_rhine_chip_info
{
369 enum chip_capability_flags
{
370 CanHaveMII
=1, HasESIPhy
=2, HasDavicomPhy
=4,
371 ReqTxAlign
=0x10, HasWOL
=0x20, };
374 #define RHINE_IOTYPE (PCI_USES_MEM | PCI_USES_MASTER | PCI_ADDR1)
376 #define RHINE_IOTYPE (PCI_USES_IO | PCI_USES_MASTER | PCI_ADDR0)
378 /* Beware of PCI posted writes */
379 #define IOSYNC do { readb(dev->base_addr + StationAddr); } while (0)
381 /* directly indexed by enum via_rhine_chips, above */
382 static struct via_rhine_chip_info via_rhine_chip_info
[] __devinitdata
=
384 { "VIA VT86C100A Rhine", RHINE_IOTYPE
, 128,
385 CanHaveMII
| ReqTxAlign
| HasDavicomPhy
},
386 { "VIA VT6102 Rhine-II", RHINE_IOTYPE
, 256,
387 CanHaveMII
| HasWOL
},
388 { "VIA VT6105 Rhine-III", RHINE_IOTYPE
, 256,
389 CanHaveMII
| HasWOL
},
390 { "VIA VT6105M Rhine-III", RHINE_IOTYPE
, 256,
391 CanHaveMII
| HasWOL
},
394 static struct pci_device_id via_rhine_pci_tbl
[] __devinitdata
=
396 {0x1106, 0x3043, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, VT86C100A
},
397 {0x1106, 0x3065, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, VT6102
},
398 {0x1106, 0x3106, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, VT6105
},
399 {0x1106, 0x3053, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, VT6105M
},
400 {0,} /* terminate list */
402 MODULE_DEVICE_TABLE(pci
, via_rhine_pci_tbl
);
405 /* Offsets to the device registers. */
406 enum register_offsets
{
407 StationAddr
=0x00, RxConfig
=0x06, TxConfig
=0x07, ChipCmd
=0x08,
408 IntrStatus
=0x0C, IntrEnable
=0x0E,
409 MulticastFilter0
=0x10, MulticastFilter1
=0x14,
410 RxRingPtr
=0x18, TxRingPtr
=0x1C, GFIFOTest
=0x54,
411 MIIPhyAddr
=0x6C, MIIStatus
=0x6D, PCIBusConfig
=0x6E,
412 MIICmd
=0x70, MIIRegAddr
=0x71, MIIData
=0x72, MACRegEEcsr
=0x74,
413 ConfigA
=0x78, ConfigB
=0x79, ConfigC
=0x7A, ConfigD
=0x7B,
414 RxMissed
=0x7C, RxCRCErrs
=0x7E, MiscCmd
=0x81,
415 StickyHW
=0x83, IntrStatus2
=0x84, WOLcrClr
=0xA4, WOLcgClr
=0xA7,
419 /* Bits in ConfigD */
421 BackOptional
=0x01, BackModify
=0x02,
422 BackCaptureEffect
=0x04, BackRandom
=0x08
426 /* Registers we check that mmio and reg are the same. */
427 int mmio_verify_registers
[] = {
428 RxConfig
, TxConfig
, IntrEnable
, ConfigA
, ConfigB
, ConfigC
, ConfigD
,
433 /* Bits in the interrupt status/mask registers. */
434 enum intr_status_bits
{
435 IntrRxDone
=0x0001, IntrRxErr
=0x0004, IntrRxEmpty
=0x0020,
436 IntrTxDone
=0x0002, IntrTxError
=0x0008, IntrTxUnderrun
=0x0210,
438 IntrStatsMax
=0x0080, IntrRxEarly
=0x0100,
439 IntrRxOverflow
=0x0400, IntrRxDropped
=0x0800, IntrRxNoBuf
=0x1000,
440 IntrTxAborted
=0x2000, IntrLinkChange
=0x4000,
442 IntrNormalSummary
=0x0003, IntrAbnormalSummary
=0xC260,
443 IntrTxDescRace
=0x080000, /* mapped from IntrStatus2 */
444 IntrTxErrSummary
=0x082210,
447 /* The Rx and Tx buffer descriptors. */
450 u32 desc_length
; /* Chain flag, Buffer/frame length */
456 u32 desc_length
; /* Chain flag, Tx Config, Frame length */
461 /* Initial value for tx_desc.desc_length, Buffer size goes to bits 0-10 */
462 #define TXDESC 0x00e08000
464 enum rx_status_bits
{
465 RxOK
=0x8000, RxWholePkt
=0x0300, RxErr
=0x008F
468 /* Bits in *_desc.*_status */
469 enum desc_status_bits
{
473 /* Bits in ChipCmd. */
475 CmdInit
=0x0001, CmdStart
=0x0002, CmdStop
=0x0004, CmdRxOn
=0x0008,
476 CmdTxOn
=0x0010, CmdTxDemand
=0x0020, CmdRxDemand
=0x0040,
477 CmdEarlyRx
=0x0100, CmdEarlyTx
=0x0200, CmdFDuplex
=0x0400,
478 CmdNoTxPoll
=0x0800, CmdReset
=0x8000,
481 #define MAX_MII_CNT 4
482 struct netdev_private
{
483 /* Descriptor rings */
484 struct rx_desc
*rx_ring
;
485 struct tx_desc
*tx_ring
;
486 dma_addr_t rx_ring_dma
;
487 dma_addr_t tx_ring_dma
;
489 /* The addresses of receive-in-place skbuffs. */
490 struct sk_buff
*rx_skbuff
[RX_RING_SIZE
];
491 dma_addr_t rx_skbuff_dma
[RX_RING_SIZE
];
493 /* The saved address of a sent-in-place packet/buffer, for later free(). */
494 struct sk_buff
*tx_skbuff
[TX_RING_SIZE
];
495 dma_addr_t tx_skbuff_dma
[TX_RING_SIZE
];
497 /* Tx bounce buffers */
498 unsigned char *tx_buf
[TX_RING_SIZE
];
499 unsigned char *tx_bufs
;
500 dma_addr_t tx_bufs_dma
;
502 struct pci_dev
*pdev
;
503 struct net_device_stats stats
;
504 struct timer_list timer
; /* Media monitoring timer. */
507 /* Frequently used values: keep some adjacent for cache effect. */
508 int chip_id
, drv_flags
;
509 struct rx_desc
*rx_head_desc
;
510 unsigned int cur_rx
, dirty_rx
; /* Producer/consumer ring indices */
511 unsigned int cur_tx
, dirty_tx
;
512 unsigned int rx_buf_sz
; /* Based on MTU+slack. */
513 u16 chip_cmd
; /* Current setting for ChipCmd */
515 /* These values are keep track of the transceiver/media in use. */
516 unsigned int default_port
:4; /* Last dev->if_port value. */
517 u8 tx_thresh
, rx_thresh
;
519 /* MII transceiver section. */
520 unsigned char phys
[MAX_MII_CNT
]; /* MII device addresses. */
521 unsigned int mii_cnt
; /* number of MIIs found, but only the first one is used */
522 u16 mii_status
; /* last read MII status */
523 struct mii_if_info mii_if
;
526 static int mdio_read(struct net_device
*dev
, int phy_id
, int location
);
527 static void mdio_write(struct net_device
*dev
, int phy_id
, int location
, int value
);
528 static int via_rhine_open(struct net_device
*dev
);
529 static void via_rhine_check_duplex(struct net_device
*dev
);
530 static void via_rhine_timer(unsigned long data
);
531 static void via_rhine_tx_timeout(struct net_device
*dev
);
532 static int via_rhine_start_tx(struct sk_buff
*skb
, struct net_device
*dev
);
533 static irqreturn_t
via_rhine_interrupt(int irq
, void *dev_instance
, struct pt_regs
*regs
);
534 static void via_rhine_tx(struct net_device
*dev
);
535 static void via_rhine_rx(struct net_device
*dev
);
536 static void via_rhine_error(struct net_device
*dev
, int intr_status
);
537 static void via_rhine_set_rx_mode(struct net_device
*dev
);
538 static struct net_device_stats
*via_rhine_get_stats(struct net_device
*dev
);
539 static int netdev_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
);
540 static int via_rhine_close(struct net_device
*dev
);
542 static inline u32
get_intr_status(struct net_device
*dev
)
544 long ioaddr
= dev
->base_addr
;
545 struct netdev_private
*np
= dev
->priv
;
548 intr_status
= readw(ioaddr
+ IntrStatus
);
549 /* On Rhine-II, Bit 3 indicates Tx descriptor write-back race. */
550 if (np
->chip_id
== VT6102
)
551 intr_status
|= readb(ioaddr
+ IntrStatus2
) << 16;
555 static void wait_for_reset(struct net_device
*dev
, int chip_id
, char *name
)
557 long ioaddr
= dev
->base_addr
;
562 if (readw(ioaddr
+ ChipCmd
) & CmdReset
) {
563 printk(KERN_INFO
"%s: Reset not complete yet. "
564 "Trying harder.\n", name
);
566 /* Rhine-II needs to be forced sometimes */
567 if (chip_id
== VT6102
)
568 writeb(0x40, ioaddr
+ MiscCmd
);
570 /* VT86C100A may need long delay after reset (dlink) */
571 /* Seen on Rhine-II as well (rl) */
572 while ((readw(ioaddr
+ ChipCmd
) & CmdReset
) && --boguscnt
)
578 printk(KERN_INFO
"%s: Reset %s.\n", name
,
579 boguscnt
? "succeeded" : "failed");
583 static void __devinit
enable_mmio(long ioaddr
, int chip_id
)
586 if (chip_id
== VT86C100A
) {
587 /* More recent docs say that this bit is reserved ... */
588 n
= inb(ioaddr
+ ConfigA
) | 0x20;
589 outb(n
, ioaddr
+ ConfigA
);
591 n
= inb(ioaddr
+ ConfigD
) | 0x80;
592 outb(n
, ioaddr
+ ConfigD
);
597 static void __devinit
reload_eeprom(long ioaddr
)
600 outb(0x20, ioaddr
+ MACRegEEcsr
);
601 /* Typically 2 cycles to reload. */
602 for (i
= 0; i
< 150; i
++)
603 if (! (inb(ioaddr
+ MACRegEEcsr
) & 0x20))
607 static int __devinit
via_rhine_init_one (struct pci_dev
*pdev
,
608 const struct pci_device_id
*ent
)
610 struct net_device
*dev
;
611 struct netdev_private
*np
;
613 int chip_id
= (int) ent
->driver_data
;
614 static int card_idx
= -1;
623 /* when built into the kernel, we only print version if device is found */
625 static int printed_version
;
626 if (!printed_version
++)
631 option
= card_idx
< MAX_UNITS
? options
[card_idx
] : 0;
632 io_size
= via_rhine_chip_info
[chip_id
].io_size
;
633 pci_flags
= via_rhine_chip_info
[chip_id
].pci_flags
;
635 if (pci_enable_device (pdev
))
638 /* this should always be supported */
639 if (pci_set_dma_mask(pdev
, 0xffffffff)) {
640 printk(KERN_ERR
"32-bit PCI DMA addresses not supported by the card!?\n");
645 if ((pci_resource_len (pdev
, 0) < io_size
) ||
646 (pci_resource_len (pdev
, 1) < io_size
)) {
647 printk (KERN_ERR
"Insufficient PCI resources, aborting\n");
651 ioaddr
= pci_resource_start (pdev
, 0);
652 memaddr
= pci_resource_start (pdev
, 1);
654 if (pci_flags
& PCI_USES_MASTER
)
655 pci_set_master (pdev
);
657 dev
= alloc_etherdev(sizeof(*np
));
659 printk (KERN_ERR
"init_ethernet failed for card #%d\n", card_idx
);
662 SET_MODULE_OWNER(dev
);
663 SET_NETDEV_DEV(dev
, &pdev
->dev
);
665 if (pci_request_regions(pdev
, shortname
))
666 goto err_out_free_netdev
;
670 enable_mmio(ioaddr0
, chip_id
);
672 ioaddr
= (long) ioremap (memaddr
, io_size
);
674 printk (KERN_ERR
"ioremap failed for device %s, region 0x%X @ 0x%lX\n",
675 pdev
->slot_name
, io_size
, memaddr
);
676 goto err_out_free_res
;
679 /* Check that selected MMIO registers match the PIO ones */
681 while (mmio_verify_registers
[i
]) {
682 int reg
= mmio_verify_registers
[i
++];
683 unsigned char a
= inb(ioaddr0
+reg
);
684 unsigned char b
= readb(ioaddr
+reg
);
686 printk (KERN_ERR
"MMIO do not match PIO [%02x] (%02x != %02x)\n",
693 /* D-Link provided reset code (with comment additions) */
694 if (via_rhine_chip_info
[chip_id
].drv_flags
& HasWOL
) {
695 unsigned char byOrgValue
;
697 /* clear sticky bit before reset & read ethernet address */
698 byOrgValue
= readb(ioaddr
+ StickyHW
);
699 byOrgValue
= byOrgValue
& 0xFC;
700 writeb(byOrgValue
, ioaddr
+ StickyHW
);
702 /* (bits written are cleared?) */
703 /* disable force PME-enable */
704 writeb(0x80, ioaddr
+ WOLcgClr
);
705 /* disable power-event config bit */
706 writeb(0xFF, ioaddr
+ WOLcrClr
);
707 /* clear power status (undocumented in vt6102 docs?) */
708 writeb(0xFF, ioaddr
+ PwrcsrClr
);
711 /* Reset the chip to erase previous misconfiguration. */
712 writew(CmdReset
, ioaddr
+ ChipCmd
);
714 dev
->base_addr
= ioaddr
;
715 wait_for_reset(dev
, chip_id
, shortname
);
717 /* Reload the station address from the EEPROM. */
719 reload_eeprom(ioaddr
);
721 reload_eeprom(ioaddr0
);
722 /* Reloading from eeprom overwrites cfgA-D, so we must re-enable MMIO.
723 If reload_eeprom() was done first this could be avoided, but it is
724 not known if that still works with the "win98-reboot" problem. */
725 enable_mmio(ioaddr0
, chip_id
);
728 for (i
= 0; i
< 6; i
++)
729 dev
->dev_addr
[i
] = readb(ioaddr
+ StationAddr
+ i
);
731 if (!is_valid_ether_addr(dev
->dev_addr
)) {
732 printk(KERN_ERR
"Invalid MAC address for card #%d\n", card_idx
);
736 if (chip_id
== VT6102
) {
738 * for 3065D, EEPROM reloaded will cause bit 0 in MAC_REG_CFGA
739 * turned on. it makes MAC receive magic packet
740 * automatically. So, we turn it off. (D-Link)
742 writeb(readb(ioaddr
+ ConfigA
) & 0xFE, ioaddr
+ ConfigA
);
745 /* Select backoff algorithm */
747 writeb(readb(ioaddr
+ ConfigD
) & (0xF0 | backoff
),
750 dev
->irq
= pdev
->irq
;
753 spin_lock_init (&np
->lock
);
754 np
->chip_id
= chip_id
;
755 np
->drv_flags
= via_rhine_chip_info
[chip_id
].drv_flags
;
757 np
->mii_if
.dev
= dev
;
758 np
->mii_if
.mdio_read
= mdio_read
;
759 np
->mii_if
.mdio_write
= mdio_write
;
760 np
->mii_if
.phy_id_mask
= 0x1f;
761 np
->mii_if
.reg_num_mask
= 0x1f;
764 option
= dev
->mem_start
;
766 /* The chip-specific entries in the device structure. */
767 dev
->open
= via_rhine_open
;
768 dev
->hard_start_xmit
= via_rhine_start_tx
;
769 dev
->stop
= via_rhine_close
;
770 dev
->get_stats
= via_rhine_get_stats
;
771 dev
->set_multicast_list
= via_rhine_set_rx_mode
;
772 dev
->do_ioctl
= netdev_ioctl
;
773 dev
->tx_timeout
= via_rhine_tx_timeout
;
774 dev
->watchdog_timeo
= TX_TIMEOUT
;
775 if (np
->drv_flags
& ReqTxAlign
)
776 dev
->features
|= NETIF_F_SG
|NETIF_F_HW_CSUM
;
778 /* dev->name not defined before register_netdev()! */
779 i
= register_netdev(dev
);
783 /* The lower four bits are the media type. */
786 np
->mii_if
.full_duplex
= 1;
787 np
->default_port
= option
& 15;
789 if (card_idx
< MAX_UNITS
&& full_duplex
[card_idx
] > 0)
790 np
->mii_if
.full_duplex
= 1;
792 if (np
->mii_if
.full_duplex
) {
793 printk(KERN_INFO
"%s: Set to forced full duplex, autonegotiation"
794 " disabled.\n", dev
->name
);
795 np
->mii_if
.force_media
= 1;
798 printk(KERN_INFO
"%s: %s at 0x%lx, ",
799 dev
->name
, via_rhine_chip_info
[chip_id
].name
,
800 (pci_flags
& PCI_USES_IO
) ? ioaddr
: memaddr
);
802 for (i
= 0; i
< 5; i
++)
803 printk("%2.2x:", dev
->dev_addr
[i
]);
804 printk("%2.2x, IRQ %d.\n", dev
->dev_addr
[i
], pdev
->irq
);
806 pci_set_drvdata(pdev
, dev
);
808 if (np
->drv_flags
& CanHaveMII
) {
809 int phy
, phy_idx
= 0;
810 np
->phys
[0] = 1; /* Standard for this chip. */
811 for (phy
= 1; phy
< 32 && phy_idx
< MAX_MII_CNT
; phy
++) {
812 int mii_status
= mdio_read(dev
, phy
, 1);
813 if (mii_status
!= 0xffff && mii_status
!= 0x0000) {
814 np
->phys
[phy_idx
++] = phy
;
815 np
->mii_if
.advertising
= mdio_read(dev
, phy
, 4);
816 printk(KERN_INFO
"%s: MII PHY found at address %d, status "
817 "0x%4.4x advertising %4.4x Link %4.4x.\n",
818 dev
->name
, phy
, mii_status
, np
->mii_if
.advertising
,
819 mdio_read(dev
, phy
, 5));
821 /* set IFF_RUNNING */
822 if (mii_status
& BMSR_LSTATUS
)
823 netif_carrier_on(dev
);
825 netif_carrier_off(dev
);
828 np
->mii_cnt
= phy_idx
;
829 np
->mii_if
.phy_id
= np
->phys
[0];
832 /* Allow forcing the media type. */
835 np
->mii_if
.full_duplex
= 1;
836 np
->default_port
= option
& 0x3ff;
837 if (np
->default_port
& 0x330) {
838 /* FIXME: shouldn't someone check this variable? */
839 /* np->medialock = 1; */
840 printk(KERN_INFO
" Forcing %dMbs %s-duplex operation.\n",
841 (option
& 0x300 ? 100 : 10),
842 (option
& 0x220 ? "full" : "half"));
844 mdio_write(dev
, np
->phys
[0], MII_BMCR
,
845 ((option
& 0x300) ? 0x2000 : 0) | /* 100mbps? */
846 ((option
& 0x220) ? 0x0100 : 0)); /* Full duplex? */
854 iounmap((void *)ioaddr
);
857 pci_release_regions(pdev
);
864 static int alloc_ring(struct net_device
* dev
)
866 struct netdev_private
*np
= dev
->priv
;
870 ring
= pci_alloc_consistent(np
->pdev
,
871 RX_RING_SIZE
* sizeof(struct rx_desc
) +
872 TX_RING_SIZE
* sizeof(struct tx_desc
),
875 printk(KERN_ERR
"Could not allocate DMA memory.\n");
878 if (np
->drv_flags
& ReqTxAlign
) {
879 np
->tx_bufs
= pci_alloc_consistent(np
->pdev
, PKT_BUF_SZ
* TX_RING_SIZE
,
881 if (np
->tx_bufs
== NULL
) {
882 pci_free_consistent(np
->pdev
,
883 RX_RING_SIZE
* sizeof(struct rx_desc
) +
884 TX_RING_SIZE
* sizeof(struct tx_desc
),
891 np
->tx_ring
= ring
+ RX_RING_SIZE
* sizeof(struct rx_desc
);
892 np
->rx_ring_dma
= ring_dma
;
893 np
->tx_ring_dma
= ring_dma
+ RX_RING_SIZE
* sizeof(struct rx_desc
);
898 void free_ring(struct net_device
* dev
)
900 struct netdev_private
*np
= dev
->priv
;
902 pci_free_consistent(np
->pdev
,
903 RX_RING_SIZE
* sizeof(struct rx_desc
) +
904 TX_RING_SIZE
* sizeof(struct tx_desc
),
905 np
->rx_ring
, np
->rx_ring_dma
);
909 pci_free_consistent(np
->pdev
, PKT_BUF_SZ
* TX_RING_SIZE
,
910 np
->tx_bufs
, np
->tx_bufs_dma
);
916 static void alloc_rbufs(struct net_device
*dev
)
918 struct netdev_private
*np
= dev
->priv
;
922 np
->dirty_rx
= np
->cur_rx
= 0;
924 np
->rx_buf_sz
= (dev
->mtu
<= 1500 ? PKT_BUF_SZ
: dev
->mtu
+ 32);
925 np
->rx_head_desc
= &np
->rx_ring
[0];
926 next
= np
->rx_ring_dma
;
928 /* Init the ring entries */
929 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
930 np
->rx_ring
[i
].rx_status
= 0;
931 np
->rx_ring
[i
].desc_length
= cpu_to_le32(np
->rx_buf_sz
);
932 next
+= sizeof(struct rx_desc
);
933 np
->rx_ring
[i
].next_desc
= cpu_to_le32(next
);
934 np
->rx_skbuff
[i
] = 0;
936 /* Mark the last entry as wrapping the ring. */
937 np
->rx_ring
[i
-1].next_desc
= cpu_to_le32(np
->rx_ring_dma
);
939 /* Fill in the Rx buffers. Handle allocation failure gracefully. */
940 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
941 struct sk_buff
*skb
= dev_alloc_skb(np
->rx_buf_sz
);
942 np
->rx_skbuff
[i
] = skb
;
945 skb
->dev
= dev
; /* Mark as being used by this device. */
947 np
->rx_skbuff_dma
[i
] =
948 pci_map_single(np
->pdev
, skb
->tail
, np
->rx_buf_sz
,
951 np
->rx_ring
[i
].addr
= cpu_to_le32(np
->rx_skbuff_dma
[i
]);
952 np
->rx_ring
[i
].rx_status
= cpu_to_le32(DescOwn
);
954 np
->dirty_rx
= (unsigned int)(i
- RX_RING_SIZE
);
957 static void free_rbufs(struct net_device
* dev
)
959 struct netdev_private
*np
= dev
->priv
;
962 /* Free all the skbuffs in the Rx queue. */
963 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
964 np
->rx_ring
[i
].rx_status
= 0;
965 np
->rx_ring
[i
].addr
= cpu_to_le32(0xBADF00D0); /* An invalid address. */
966 if (np
->rx_skbuff
[i
]) {
967 pci_unmap_single(np
->pdev
,
968 np
->rx_skbuff_dma
[i
],
969 np
->rx_buf_sz
, PCI_DMA_FROMDEVICE
);
970 dev_kfree_skb(np
->rx_skbuff
[i
]);
972 np
->rx_skbuff
[i
] = 0;
976 static void alloc_tbufs(struct net_device
* dev
)
978 struct netdev_private
*np
= dev
->priv
;
982 np
->dirty_tx
= np
->cur_tx
= 0;
983 next
= np
->tx_ring_dma
;
984 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
985 np
->tx_skbuff
[i
] = 0;
986 np
->tx_ring
[i
].tx_status
= 0;
987 np
->tx_ring
[i
].desc_length
= cpu_to_le32(TXDESC
);
988 next
+= sizeof(struct tx_desc
);
989 np
->tx_ring
[i
].next_desc
= cpu_to_le32(next
);
990 np
->tx_buf
[i
] = &np
->tx_bufs
[i
* PKT_BUF_SZ
];
992 np
->tx_ring
[i
-1].next_desc
= cpu_to_le32(np
->tx_ring_dma
);
996 static void free_tbufs(struct net_device
* dev
)
998 struct netdev_private
*np
= dev
->priv
;
1001 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1002 np
->tx_ring
[i
].tx_status
= 0;
1003 np
->tx_ring
[i
].desc_length
= cpu_to_le32(TXDESC
);
1004 np
->tx_ring
[i
].addr
= cpu_to_le32(0xBADF00D0); /* An invalid address. */
1005 if (np
->tx_skbuff
[i
]) {
1006 if (np
->tx_skbuff_dma
[i
]) {
1007 pci_unmap_single(np
->pdev
,
1008 np
->tx_skbuff_dma
[i
],
1009 np
->tx_skbuff
[i
]->len
, PCI_DMA_TODEVICE
);
1011 dev_kfree_skb(np
->tx_skbuff
[i
]);
1013 np
->tx_skbuff
[i
] = 0;
1018 static void init_registers(struct net_device
*dev
)
1020 struct netdev_private
*np
= dev
->priv
;
1021 long ioaddr
= dev
->base_addr
;
1024 for (i
= 0; i
< 6; i
++)
1025 writeb(dev
->dev_addr
[i
], ioaddr
+ StationAddr
+ i
);
1027 /* Initialize other registers. */
1028 writew(0x0006, ioaddr
+ PCIBusConfig
); /* Tune configuration??? */
1029 /* Configure initial FIFO thresholds. */
1030 writeb(0x20, ioaddr
+ TxConfig
);
1031 np
->tx_thresh
= 0x20;
1032 np
->rx_thresh
= 0x60; /* Written in via_rhine_set_rx_mode(). */
1033 np
->mii_if
.full_duplex
= 0;
1035 if (dev
->if_port
== 0)
1036 dev
->if_port
= np
->default_port
;
1038 writel(np
->rx_ring_dma
, ioaddr
+ RxRingPtr
);
1039 writel(np
->tx_ring_dma
, ioaddr
+ TxRingPtr
);
1041 via_rhine_set_rx_mode(dev
);
1043 /* Enable interrupts by setting the interrupt mask. */
1044 writew(IntrRxDone
| IntrRxErr
| IntrRxEmpty
| IntrRxOverflow
|
1045 IntrRxDropped
| IntrRxNoBuf
| IntrTxAborted
|
1046 IntrTxDone
| IntrTxError
| IntrTxUnderrun
|
1047 IntrPCIErr
| IntrStatsMax
| IntrLinkChange
,
1048 ioaddr
+ IntrEnable
);
1050 np
->chip_cmd
= CmdStart
|CmdTxOn
|CmdRxOn
|CmdNoTxPoll
;
1051 if (np
->mii_if
.force_media
)
1052 np
->chip_cmd
|= CmdFDuplex
;
1053 writew(np
->chip_cmd
, ioaddr
+ ChipCmd
);
1055 via_rhine_check_duplex(dev
);
1057 /* The LED outputs of various MII xcvrs should be configured. */
1058 /* For NS or Mison phys, turn on bit 1 in register 0x17 */
1059 /* For ESI phys, turn on bit 7 in register 0x17. */
1060 mdio_write(dev
, np
->phys
[0], 0x17, mdio_read(dev
, np
->phys
[0], 0x17) |
1061 (np
->drv_flags
& HasESIPhy
) ? 0x0080 : 0x0001);
1063 /* Read and write over the MII Management Data I/O (MDIO) interface. */
1065 static int mdio_read(struct net_device
*dev
, int phy_id
, int regnum
)
1067 long ioaddr
= dev
->base_addr
;
1068 int boguscnt
= 1024;
1070 /* Wait for a previous command to complete. */
1071 while ((readb(ioaddr
+ MIICmd
) & 0x60) && --boguscnt
> 0)
1073 writeb(0x00, ioaddr
+ MIICmd
);
1074 writeb(phy_id
, ioaddr
+ MIIPhyAddr
);
1075 writeb(regnum
, ioaddr
+ MIIRegAddr
);
1076 writeb(0x40, ioaddr
+ MIICmd
); /* Trigger read */
1078 while ((readb(ioaddr
+ MIICmd
) & 0x40) && --boguscnt
> 0)
1080 return readw(ioaddr
+ MIIData
);
1083 static void mdio_write(struct net_device
*dev
, int phy_id
, int regnum
, int value
)
1085 struct netdev_private
*np
= dev
->priv
;
1086 long ioaddr
= dev
->base_addr
;
1087 int boguscnt
= 1024;
1089 if (phy_id
== np
->phys
[0]) {
1091 case MII_BMCR
: /* Is user forcing speed/duplex? */
1092 if (value
& 0x9000) /* Autonegotiation. */
1093 np
->mii_if
.force_media
= 0;
1095 np
->mii_if
.full_duplex
= (value
& 0x0100) ? 1 : 0;
1098 np
->mii_if
.advertising
= value
;
1103 /* Wait for a previous command to complete. */
1104 while ((readb(ioaddr
+ MIICmd
) & 0x60) && --boguscnt
> 0)
1106 writeb(0x00, ioaddr
+ MIICmd
);
1107 writeb(phy_id
, ioaddr
+ MIIPhyAddr
);
1108 writeb(regnum
, ioaddr
+ MIIRegAddr
);
1109 writew(value
, ioaddr
+ MIIData
);
1110 writeb(0x20, ioaddr
+ MIICmd
); /* Trigger write. */
1114 static int via_rhine_open(struct net_device
*dev
)
1116 struct netdev_private
*np
= dev
->priv
;
1117 long ioaddr
= dev
->base_addr
;
1120 /* Reset the chip. */
1121 writew(CmdReset
, ioaddr
+ ChipCmd
);
1123 i
= request_irq(np
->pdev
->irq
, &via_rhine_interrupt
, SA_SHIRQ
, dev
->name
, dev
);
1128 printk(KERN_DEBUG
"%s: via_rhine_open() irq %d.\n",
1129 dev
->name
, np
->pdev
->irq
);
1131 i
= alloc_ring(dev
);
1136 wait_for_reset(dev
, np
->chip_id
, dev
->name
);
1137 init_registers(dev
);
1139 printk(KERN_DEBUG
"%s: Done via_rhine_open(), status %4.4x "
1140 "MII status: %4.4x.\n",
1141 dev
->name
, readw(ioaddr
+ ChipCmd
),
1142 mdio_read(dev
, np
->phys
[0], MII_BMSR
));
1144 netif_start_queue(dev
);
1146 /* Set the timer to check for link beat. */
1147 init_timer(&np
->timer
);
1148 np
->timer
.expires
= jiffies
+ 2;
1149 np
->timer
.data
= (unsigned long)dev
;
1150 np
->timer
.function
= &via_rhine_timer
; /* timer handler */
1151 add_timer(&np
->timer
);
1156 static void via_rhine_check_duplex(struct net_device
*dev
)
1158 struct netdev_private
*np
= dev
->priv
;
1159 long ioaddr
= dev
->base_addr
;
1160 int mii_lpa
= mdio_read(dev
, np
->phys
[0], MII_LPA
);
1161 int negotiated
= mii_lpa
& np
->mii_if
.advertising
;
1164 if (np
->mii_if
.force_media
|| mii_lpa
== 0xffff)
1166 duplex
= (negotiated
& 0x0100) || (negotiated
& 0x01C0) == 0x0040;
1167 if (np
->mii_if
.full_duplex
!= duplex
) {
1168 np
->mii_if
.full_duplex
= duplex
;
1170 printk(KERN_INFO
"%s: Setting %s-duplex based on MII #%d link"
1171 " partner capability of %4.4x.\n", dev
->name
,
1172 duplex
? "full" : "half", np
->phys
[0], mii_lpa
);
1174 np
->chip_cmd
|= CmdFDuplex
;
1176 np
->chip_cmd
&= ~CmdFDuplex
;
1177 writew(np
->chip_cmd
, ioaddr
+ ChipCmd
);
1182 static void via_rhine_timer(unsigned long data
)
1184 struct net_device
*dev
= (struct net_device
*)data
;
1185 struct netdev_private
*np
= dev
->priv
;
1186 long ioaddr
= dev
->base_addr
;
1187 int next_tick
= 10*HZ
;
1191 printk(KERN_DEBUG
"%s: VIA Rhine monitor tick, status %4.4x.\n",
1192 dev
->name
, readw(ioaddr
+ IntrStatus
));
1195 spin_lock_irq (&np
->lock
);
1197 via_rhine_check_duplex(dev
);
1199 /* make IFF_RUNNING follow the MII status bit "Link established" */
1200 mii_status
= mdio_read(dev
, np
->phys
[0], MII_BMSR
);
1201 if ( (mii_status
& BMSR_LSTATUS
) != (np
->mii_status
& BMSR_LSTATUS
) ) {
1202 if (mii_status
& BMSR_LSTATUS
)
1203 netif_carrier_on(dev
);
1205 netif_carrier_off(dev
);
1207 np
->mii_status
= mii_status
;
1209 spin_unlock_irq (&np
->lock
);
1211 np
->timer
.expires
= jiffies
+ next_tick
;
1212 add_timer(&np
->timer
);
1216 static void via_rhine_tx_timeout (struct net_device
*dev
)
1218 struct netdev_private
*np
= dev
->priv
;
1219 long ioaddr
= dev
->base_addr
;
1221 printk (KERN_WARNING
"%s: Transmit timed out, status %4.4x, PHY status "
1222 "%4.4x, resetting...\n",
1223 dev
->name
, readw (ioaddr
+ IntrStatus
),
1224 mdio_read (dev
, np
->phys
[0], MII_BMSR
));
1228 /* protect against concurrent rx interrupts */
1229 disable_irq(np
->pdev
->irq
);
1231 spin_lock(&np
->lock
);
1233 /* Reset the chip. */
1234 writew(CmdReset
, ioaddr
+ ChipCmd
);
1236 /* clear all descriptors */
1242 /* Reinitialize the hardware. */
1243 wait_for_reset(dev
, np
->chip_id
, dev
->name
);
1244 init_registers(dev
);
1246 spin_unlock(&np
->lock
);
1247 enable_irq(np
->pdev
->irq
);
1249 dev
->trans_start
= jiffies
;
1250 np
->stats
.tx_errors
++;
1251 netif_wake_queue(dev
);
1254 static int via_rhine_start_tx(struct sk_buff
*skb
, struct net_device
*dev
)
1256 struct netdev_private
*np
= dev
->priv
;
1260 /* Caution: the write order is important here, set the field
1261 with the "ownership" bits last. */
1263 /* Calculate the next Tx descriptor entry. */
1264 entry
= np
->cur_tx
% TX_RING_SIZE
;
1266 if (skb
->len
< ETH_ZLEN
) {
1267 skb
= skb_padto(skb
, ETH_ZLEN
);
1272 np
->tx_skbuff
[entry
] = skb
;
1274 if ((np
->drv_flags
& ReqTxAlign
) &&
1275 (((long)skb
->data
& 3) || skb_shinfo(skb
)->nr_frags
!= 0 || skb
->ip_summed
== CHECKSUM_HW
)
1277 /* Must use alignment buffer. */
1278 if (skb
->len
> PKT_BUF_SZ
) {
1279 /* packet too long, drop it */
1281 np
->tx_skbuff
[entry
] = NULL
;
1282 np
->stats
.tx_dropped
++;
1285 skb_copy_and_csum_dev(skb
, np
->tx_buf
[entry
]);
1286 np
->tx_skbuff_dma
[entry
] = 0;
1287 np
->tx_ring
[entry
].addr
= cpu_to_le32(np
->tx_bufs_dma
+
1288 (np
->tx_buf
[entry
] - np
->tx_bufs
));
1290 np
->tx_skbuff_dma
[entry
] =
1291 pci_map_single(np
->pdev
, skb
->data
, skb
->len
, PCI_DMA_TODEVICE
);
1292 np
->tx_ring
[entry
].addr
= cpu_to_le32(np
->tx_skbuff_dma
[entry
]);
1295 np
->tx_ring
[entry
].desc_length
=
1296 cpu_to_le32(TXDESC
| (skb
->len
>= ETH_ZLEN
? skb
->len
: ETH_ZLEN
));
1299 spin_lock_irq (&np
->lock
);
1301 np
->tx_ring
[entry
].tx_status
= cpu_to_le32(DescOwn
);
1306 /* Non-x86 Todo: explicitly flush cache lines here. */
1309 * Wake the potentially-idle transmit channel unless errors are
1310 * pending (the ISR must sort them out first).
1312 intr_status
= get_intr_status(dev
);
1313 if ((intr_status
& IntrTxErrSummary
) == 0) {
1314 writew(CmdTxDemand
| np
->chip_cmd
, dev
->base_addr
+ ChipCmd
);
1318 if (np
->cur_tx
== np
->dirty_tx
+ TX_QUEUE_LEN
)
1319 netif_stop_queue(dev
);
1321 dev
->trans_start
= jiffies
;
1323 spin_unlock_irq (&np
->lock
);
1326 printk(KERN_DEBUG
"%s: Transmit frame #%d queued in slot %d.\n",
1327 dev
->name
, np
->cur_tx
-1, entry
);
1332 /* The interrupt handler does all of the Rx thread work and cleans up
1333 after the Tx thread. */
1334 static irqreturn_t
via_rhine_interrupt(int irq
, void *dev_instance
, struct pt_regs
*rgs
)
1336 struct net_device
*dev
= dev_instance
;
1339 int boguscnt
= max_interrupt_work
;
1342 ioaddr
= dev
->base_addr
;
1344 while ((intr_status
= get_intr_status(dev
))) {
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
);
1354 printk(KERN_DEBUG
"%s: Interrupt, status %8.8x.\n",
1355 dev
->name
, intr_status
);
1357 if (intr_status
& (IntrRxDone
| IntrRxErr
| IntrRxDropped
|
1358 IntrRxWakeUp
| IntrRxEmpty
| IntrRxNoBuf
))
1361 if (intr_status
& (IntrTxErrSummary
| IntrTxDone
)) {
1362 if (intr_status
& IntrTxErrSummary
) {
1364 /* Avoid scavenging before Tx engine turned off */
1365 while ((readw(ioaddr
+ChipCmd
) & CmdTxOn
) && --cnt
)
1367 if (debug
> 2 && !cnt
)
1368 printk(KERN_WARNING
"%s: via_rhine_interrupt() "
1369 "Tx engine still on.\n",
1375 /* Abnormal error summary/uncommon events handlers. */
1376 if (intr_status
& (IntrPCIErr
| IntrLinkChange
|
1377 IntrStatsMax
| IntrTxError
| IntrTxAborted
|
1378 IntrTxUnderrun
| IntrTxDescRace
))
1379 via_rhine_error(dev
, intr_status
);
1381 if (--boguscnt
< 0) {
1382 printk(KERN_WARNING
"%s: Too much work at interrupt, "
1384 dev
->name
, intr_status
);
1390 printk(KERN_DEBUG
"%s: exiting interrupt, status=%8.8x.\n",
1391 dev
->name
, readw(ioaddr
+ IntrStatus
));
1392 return IRQ_RETVAL(handled
);
1395 /* This routine is logically part of the interrupt handler, but isolated
1397 static void via_rhine_tx(struct net_device
*dev
)
1399 struct netdev_private
*np
= dev
->priv
;
1400 int txstatus
= 0, entry
= np
->dirty_tx
% TX_RING_SIZE
;
1402 spin_lock (&np
->lock
);
1404 /* find and cleanup dirty tx descriptors */
1405 while (np
->dirty_tx
!= np
->cur_tx
) {
1406 txstatus
= le32_to_cpu(np
->tx_ring
[entry
].tx_status
);
1408 printk(KERN_DEBUG
" Tx scavenge %d status %8.8x.\n",
1410 if (txstatus
& DescOwn
)
1412 if (txstatus
& 0x8000) {
1414 printk(KERN_DEBUG
"%s: Transmit error, Tx status %8.8x.\n",
1415 dev
->name
, txstatus
);
1416 np
->stats
.tx_errors
++;
1417 if (txstatus
& 0x0400) np
->stats
.tx_carrier_errors
++;
1418 if (txstatus
& 0x0200) np
->stats
.tx_window_errors
++;
1419 if (txstatus
& 0x0100) np
->stats
.tx_aborted_errors
++;
1420 if (txstatus
& 0x0080) np
->stats
.tx_heartbeat_errors
++;
1421 if (((np
->chip_id
== VT86C100A
) && txstatus
& 0x0002) ||
1422 (txstatus
& 0x0800) || (txstatus
& 0x1000)) {
1423 np
->stats
.tx_fifo_errors
++;
1424 np
->tx_ring
[entry
].tx_status
= cpu_to_le32(DescOwn
);
1425 break; /* Keep the skb - we try again */
1427 /* Transmitter restarted in 'abnormal' handler. */
1429 if (np
->chip_id
== VT86C100A
)
1430 np
->stats
.collisions
+= (txstatus
>> 3) & 0x0F;
1432 np
->stats
.collisions
+= txstatus
& 0x0F;
1434 printk(KERN_DEBUG
"collisions: %1.1x:%1.1x\n",
1435 (txstatus
>> 3) & 0xF,
1437 np
->stats
.tx_bytes
+= np
->tx_skbuff
[entry
]->len
;
1438 np
->stats
.tx_packets
++;
1440 /* Free the original skb. */
1441 if (np
->tx_skbuff_dma
[entry
]) {
1442 pci_unmap_single(np
->pdev
,
1443 np
->tx_skbuff_dma
[entry
],
1444 np
->tx_skbuff
[entry
]->len
, PCI_DMA_TODEVICE
);
1446 dev_kfree_skb_irq(np
->tx_skbuff
[entry
]);
1447 np
->tx_skbuff
[entry
] = NULL
;
1448 entry
= (++np
->dirty_tx
) % TX_RING_SIZE
;
1450 if ((np
->cur_tx
- np
->dirty_tx
) < TX_QUEUE_LEN
- 4)
1451 netif_wake_queue (dev
);
1453 spin_unlock (&np
->lock
);
1456 /* This routine is logically part of the interrupt handler, but isolated
1457 for clarity and better register allocation. */
1458 static void via_rhine_rx(struct net_device
*dev
)
1460 struct netdev_private
*np
= dev
->priv
;
1461 int entry
= np
->cur_rx
% RX_RING_SIZE
;
1462 int boguscnt
= np
->dirty_rx
+ RX_RING_SIZE
- np
->cur_rx
;
1465 printk(KERN_DEBUG
"%s: via_rhine_rx(), entry %d status %8.8x.\n",
1466 dev
->name
, entry
, le32_to_cpu(np
->rx_head_desc
->rx_status
));
1469 /* If EOP is set on the next entry, it's a new packet. Send it up. */
1470 while ( ! (np
->rx_head_desc
->rx_status
& cpu_to_le32(DescOwn
))) {
1471 struct rx_desc
*desc
= np
->rx_head_desc
;
1472 u32 desc_status
= le32_to_cpu(desc
->rx_status
);
1473 int data_size
= desc_status
>> 16;
1476 printk(KERN_DEBUG
" via_rhine_rx() status is %8.8x.\n",
1480 if ( (desc_status
& (RxWholePkt
| RxErr
)) != RxWholePkt
) {
1481 if ((desc_status
& RxWholePkt
) != RxWholePkt
) {
1482 printk(KERN_WARNING
"%s: Oversized Ethernet frame spanned "
1483 "multiple buffers, entry %#x length %d status %8.8x!\n",
1484 dev
->name
, entry
, data_size
, desc_status
);
1485 printk(KERN_WARNING
"%s: Oversized Ethernet frame %p vs %p.\n",
1486 dev
->name
, np
->rx_head_desc
, &np
->rx_ring
[entry
]);
1487 np
->stats
.rx_length_errors
++;
1488 } else if (desc_status
& RxErr
) {
1489 /* There was a error. */
1491 printk(KERN_DEBUG
" via_rhine_rx() Rx error was %8.8x.\n",
1493 np
->stats
.rx_errors
++;
1494 if (desc_status
& 0x0030) np
->stats
.rx_length_errors
++;
1495 if (desc_status
& 0x0048) np
->stats
.rx_fifo_errors
++;
1496 if (desc_status
& 0x0004) np
->stats
.rx_frame_errors
++;
1497 if (desc_status
& 0x0002) {
1498 /* this can also be updated outside the interrupt handler */
1499 spin_lock (&np
->lock
);
1500 np
->stats
.rx_crc_errors
++;
1501 spin_unlock (&np
->lock
);
1505 struct sk_buff
*skb
;
1506 /* Length should omit the CRC */
1507 int pkt_len
= data_size
- 4;
1509 /* Check if the packet is long enough to accept without copying
1510 to a minimally-sized skbuff. */
1511 if (pkt_len
< rx_copybreak
&&
1512 (skb
= dev_alloc_skb(pkt_len
+ 2)) != NULL
) {
1514 skb_reserve(skb
, 2); /* 16 byte align the IP header */
1515 pci_dma_sync_single(np
->pdev
, np
->rx_skbuff_dma
[entry
],
1516 np
->rx_buf_sz
, PCI_DMA_FROMDEVICE
);
1518 /* *_IP_COPYSUM isn't defined anywhere and eth_copy_and_sum
1519 is memcpy for all archs so this is kind of pointless right
1521 #if HAS_IP_COPYSUM /* Call copy + cksum if available. */
1522 eth_copy_and_sum(skb
, np
->rx_skbuff
[entry
]->tail
, pkt_len
, 0);
1523 skb_put(skb
, pkt_len
);
1525 memcpy(skb_put(skb
, pkt_len
), np
->rx_skbuff
[entry
]->tail
,
1529 skb
= np
->rx_skbuff
[entry
];
1531 printk(KERN_ERR
"%s: Inconsistent Rx descriptor chain.\n",
1535 np
->rx_skbuff
[entry
] = NULL
;
1536 skb_put(skb
, pkt_len
);
1537 pci_unmap_single(np
->pdev
, np
->rx_skbuff_dma
[entry
],
1538 np
->rx_buf_sz
, PCI_DMA_FROMDEVICE
);
1540 skb
->protocol
= eth_type_trans(skb
, dev
);
1542 dev
->last_rx
= jiffies
;
1543 np
->stats
.rx_bytes
+= pkt_len
;
1544 np
->stats
.rx_packets
++;
1546 entry
= (++np
->cur_rx
) % RX_RING_SIZE
;
1547 np
->rx_head_desc
= &np
->rx_ring
[entry
];
1550 /* Refill the Rx ring buffers. */
1551 for (; np
->cur_rx
- np
->dirty_rx
> 0; np
->dirty_rx
++) {
1552 struct sk_buff
*skb
;
1553 entry
= np
->dirty_rx
% RX_RING_SIZE
;
1554 if (np
->rx_skbuff
[entry
] == NULL
) {
1555 skb
= dev_alloc_skb(np
->rx_buf_sz
);
1556 np
->rx_skbuff
[entry
] = skb
;
1558 break; /* Better luck next round. */
1559 skb
->dev
= dev
; /* Mark as being used by this device. */
1560 np
->rx_skbuff_dma
[entry
] =
1561 pci_map_single(np
->pdev
, skb
->tail
, np
->rx_buf_sz
,
1562 PCI_DMA_FROMDEVICE
);
1563 np
->rx_ring
[entry
].addr
= cpu_to_le32(np
->rx_skbuff_dma
[entry
]);
1565 np
->rx_ring
[entry
].rx_status
= cpu_to_le32(DescOwn
);
1568 /* Pre-emptively restart Rx engine. */
1569 writew(readw(dev
->base_addr
+ ChipCmd
) | CmdRxOn
| CmdRxDemand
,
1570 dev
->base_addr
+ ChipCmd
);
1573 /* Clears the "tally counters" for CRC errors and missed frames(?).
1574 It has been reported that some chips need a write of 0 to clear
1575 these, for others the counters are set to 1 when written to and
1576 instead cleared when read. So we clear them both ways ... */
1577 static inline void clear_tally_counters(const long ioaddr
)
1579 writel(0, ioaddr
+ RxMissed
);
1580 readw(ioaddr
+ RxCRCErrs
);
1581 readw(ioaddr
+ RxMissed
);
1584 static void via_rhine_restart_tx(struct net_device
*dev
) {
1585 struct netdev_private
*np
= dev
->priv
;
1586 long ioaddr
= dev
->base_addr
;
1587 int entry
= np
->dirty_tx
% TX_RING_SIZE
;
1591 * If new errors occured, we need to sort them out before doing Tx.
1592 * In that case the ISR will be back here RSN anyway.
1594 intr_status
= get_intr_status(dev
);
1596 if ((intr_status
& IntrTxErrSummary
) == 0) {
1598 /* We know better than the chip where it should continue. */
1599 writel(np
->tx_ring_dma
+ entry
* sizeof(struct tx_desc
),
1600 ioaddr
+ TxRingPtr
);
1602 writew(CmdTxDemand
| np
->chip_cmd
, ioaddr
+ ChipCmd
);
1606 /* This should never happen */
1608 printk(KERN_WARNING
"%s: via_rhine_restart_tx() "
1609 "Another error occured %8.8x.\n",
1610 dev
->name
, intr_status
);
1615 static void via_rhine_error(struct net_device
*dev
, int intr_status
)
1617 struct netdev_private
*np
= dev
->priv
;
1618 long ioaddr
= dev
->base_addr
;
1620 spin_lock (&np
->lock
);
1622 if (intr_status
& (IntrLinkChange
)) {
1623 if (readb(ioaddr
+ MIIStatus
) & 0x02) {
1624 /* Link failed, restart autonegotiation. */
1625 if (np
->drv_flags
& HasDavicomPhy
)
1626 mdio_write(dev
, np
->phys
[0], MII_BMCR
, 0x3300);
1628 via_rhine_check_duplex(dev
);
1630 printk(KERN_ERR
"%s: MII status changed: Autonegotiation "
1631 "advertising %4.4x partner %4.4x.\n", dev
->name
,
1632 mdio_read(dev
, np
->phys
[0], MII_ADVERTISE
),
1633 mdio_read(dev
, np
->phys
[0], MII_LPA
));
1635 if (intr_status
& IntrStatsMax
) {
1636 np
->stats
.rx_crc_errors
+= readw(ioaddr
+ RxCRCErrs
);
1637 np
->stats
.rx_missed_errors
+= readw(ioaddr
+ RxMissed
);
1638 clear_tally_counters(ioaddr
);
1640 if (intr_status
& IntrTxAborted
) {
1642 printk(KERN_INFO
"%s: Abort %8.8x, frame dropped.\n",
1643 dev
->name
, intr_status
);
1645 if (intr_status
& IntrTxUnderrun
) {
1646 if (np
->tx_thresh
< 0xE0)
1647 writeb(np
->tx_thresh
+= 0x20, ioaddr
+ TxConfig
);
1649 printk(KERN_INFO
"%s: Transmitter underrun, Tx "
1650 "threshold now %2.2x.\n",
1651 dev
->name
, np
->tx_thresh
);
1653 if (intr_status
& IntrTxDescRace
) {
1655 printk(KERN_INFO
"%s: Tx descriptor write-back race.\n",
1658 if (intr_status
& ( IntrTxAborted
| IntrTxUnderrun
| IntrTxDescRace
))
1659 via_rhine_restart_tx(dev
);
1661 if (intr_status
& ~( IntrLinkChange
| IntrStatsMax
| IntrTxUnderrun
|
1662 IntrTxError
| IntrTxAborted
| IntrNormalSummary
|
1665 printk(KERN_ERR
"%s: Something Wicked happened! %8.8x.\n",
1666 dev
->name
, intr_status
);
1669 spin_unlock (&np
->lock
);
1672 static struct net_device_stats
*via_rhine_get_stats(struct net_device
*dev
)
1674 struct netdev_private
*np
= dev
->priv
;
1675 long ioaddr
= dev
->base_addr
;
1676 unsigned long flags
;
1678 spin_lock_irqsave(&np
->lock
, flags
);
1679 np
->stats
.rx_crc_errors
+= readw(ioaddr
+ RxCRCErrs
);
1680 np
->stats
.rx_missed_errors
+= readw(ioaddr
+ RxMissed
);
1681 clear_tally_counters(ioaddr
);
1682 spin_unlock_irqrestore(&np
->lock
, flags
);
1687 static void via_rhine_set_rx_mode(struct net_device
*dev
)
1689 struct netdev_private
*np
= dev
->priv
;
1690 long ioaddr
= dev
->base_addr
;
1691 u32 mc_filter
[2]; /* Multicast hash filter */
1692 u8 rx_mode
; /* Note: 0x02=accept runt, 0x01=accept errs */
1694 if (dev
->flags
& IFF_PROMISC
) { /* Set promiscuous. */
1695 /* Unconditionally log net taps. */
1696 printk(KERN_NOTICE
"%s: Promiscuous mode enabled.\n", dev
->name
);
1698 writel(0xffffffff, ioaddr
+ MulticastFilter0
);
1699 writel(0xffffffff, ioaddr
+ MulticastFilter1
);
1700 } else if ((dev
->mc_count
> multicast_filter_limit
)
1701 || (dev
->flags
& IFF_ALLMULTI
)) {
1702 /* Too many to match, or accept all multicasts. */
1703 writel(0xffffffff, ioaddr
+ MulticastFilter0
);
1704 writel(0xffffffff, ioaddr
+ MulticastFilter1
);
1707 struct dev_mc_list
*mclist
;
1709 memset(mc_filter
, 0, sizeof(mc_filter
));
1710 for (i
= 0, mclist
= dev
->mc_list
; mclist
&& i
< dev
->mc_count
;
1711 i
++, mclist
= mclist
->next
) {
1712 int bit_nr
= ether_crc(ETH_ALEN
, mclist
->dmi_addr
) >> 26;
1714 mc_filter
[bit_nr
>> 5] |= cpu_to_le32(1 << (bit_nr
& 31));
1716 writel(mc_filter
[0], ioaddr
+ MulticastFilter0
);
1717 writel(mc_filter
[1], ioaddr
+ MulticastFilter1
);
1720 writeb(np
->rx_thresh
| rx_mode
, ioaddr
+ RxConfig
);
1723 static int netdev_ethtool_ioctl (struct net_device
*dev
, void *useraddr
)
1725 struct netdev_private
*np
= dev
->priv
;
1728 if (get_user(ethcmd
, (u32
*)useraddr
))
1732 case ETHTOOL_GDRVINFO
: {
1733 struct ethtool_drvinfo info
= { ETHTOOL_GDRVINFO
};
1734 strcpy (info
.driver
, DRV_NAME
);
1735 strcpy (info
.version
, DRV_VERSION
);
1736 strcpy (info
.bus_info
, np
->pdev
->slot_name
);
1737 if (copy_to_user (useraddr
, &info
, sizeof (info
)))
1743 case ETHTOOL_GSET
: {
1744 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1745 if (!(np
->drv_flags
& CanHaveMII
))
1747 spin_lock_irq(&np
->lock
);
1748 mii_ethtool_gset(&np
->mii_if
, &ecmd
);
1749 spin_unlock_irq(&np
->lock
);
1750 if (copy_to_user(useraddr
, &ecmd
, sizeof(ecmd
)))
1755 case ETHTOOL_SSET
: {
1757 struct ethtool_cmd ecmd
;
1758 if (!(np
->drv_flags
& CanHaveMII
))
1760 if (copy_from_user(&ecmd
, useraddr
, sizeof(ecmd
)))
1762 spin_lock_irq(&np
->lock
);
1763 r
= mii_ethtool_sset(&np
->mii_if
, &ecmd
);
1764 spin_unlock_irq(&np
->lock
);
1767 /* restart autonegotiation */
1768 case ETHTOOL_NWAY_RST
: {
1769 if (!(np
->drv_flags
& CanHaveMII
))
1771 return mii_nway_restart(&np
->mii_if
);
1773 /* get link status */
1774 case ETHTOOL_GLINK
: {
1775 struct ethtool_value edata
= {ETHTOOL_GLINK
};
1776 if (!(np
->drv_flags
& CanHaveMII
))
1778 edata
.data
= mii_link_ok(&np
->mii_if
);
1779 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1784 /* get message-level */
1785 case ETHTOOL_GMSGLVL
: {
1786 struct ethtool_value edata
= {ETHTOOL_GMSGLVL
};
1788 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1792 /* set message-level */
1793 case ETHTOOL_SMSGLVL
: {
1794 struct ethtool_value edata
;
1795 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1807 static int netdev_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
1809 struct netdev_private
*np
= dev
->priv
;
1810 struct mii_ioctl_data
*data
= (struct mii_ioctl_data
*) & rq
->ifr_data
;
1813 if (!netif_running(dev
))
1816 if (cmd
== SIOCETHTOOL
)
1817 rc
= netdev_ethtool_ioctl(dev
, (void *) rq
->ifr_data
);
1820 spin_lock_irq(&np
->lock
);
1821 rc
= generic_mii_ioctl(&np
->mii_if
, data
, cmd
, NULL
);
1822 spin_unlock_irq(&np
->lock
);
1828 static int via_rhine_close(struct net_device
*dev
)
1830 long ioaddr
= dev
->base_addr
;
1831 struct netdev_private
*np
= dev
->priv
;
1833 del_timer_sync(&np
->timer
);
1835 spin_lock_irq(&np
->lock
);
1837 netif_stop_queue(dev
);
1840 printk(KERN_DEBUG
"%s: Shutting down ethercard, status was %4.4x.\n",
1841 dev
->name
, readw(ioaddr
+ ChipCmd
));
1843 /* Switch to loopback mode to avoid hardware races. */
1844 writeb(np
->tx_thresh
| 0x02, ioaddr
+ TxConfig
);
1846 /* Disable interrupts by clearing the interrupt mask. */
1847 writew(0x0000, ioaddr
+ IntrEnable
);
1849 /* Stop the chip's Tx and Rx processes. */
1850 writew(CmdStop
, ioaddr
+ ChipCmd
);
1852 spin_unlock_irq(&np
->lock
);
1854 free_irq(np
->pdev
->irq
, dev
);
1863 static void __devexit
via_rhine_remove_one (struct pci_dev
*pdev
)
1865 struct net_device
*dev
= pci_get_drvdata(pdev
);
1867 unregister_netdev(dev
);
1869 pci_release_regions(pdev
);
1872 iounmap((char *)(dev
->base_addr
));
1876 pci_disable_device(pdev
);
1877 pci_set_drvdata(pdev
, NULL
);
1881 static struct pci_driver via_rhine_driver
= {
1882 .name
= "via-rhine",
1883 .id_table
= via_rhine_pci_tbl
,
1884 .probe
= via_rhine_init_one
,
1885 .remove
= __devexit_p(via_rhine_remove_one
),
1889 static int __init
via_rhine_init (void)
1891 /* when a module, this is printed whether or not devices are found in probe */
1895 return pci_module_init (&via_rhine_driver
);
1899 static void __exit
via_rhine_cleanup (void)
1901 pci_unregister_driver (&via_rhine_driver
);
1905 module_init(via_rhine_init
);
1906 module_exit(via_rhine_cleanup
);
1911 * compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c via-rhine.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"