1 /* winbond-840.c: A Linux PCI network adapter device driver. */
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 The author may be reached as becker@scyld.com, or C/O
13 Scyld Computing Corporation
14 410 Severn Ave., Suite 210
17 Support and updates available at
18 http://www.scyld.com/network/drivers.html
20 Do not remove the copyright information.
21 Do not change the version information unless an improvement has been made.
22 Merely removing my name, as Compex has done in the past, does not count
28 * spin lock update, memory barriers, new style dma mappings
29 limit each tx buffer to < 1024 bytes
30 remove DescIntr from Rx descriptors (that's an Tx flag)
31 remove next pointer from Tx descriptors
32 synchronize tx_q_bytes
33 software reset in tx_timeout
34 Copyright (C) 2000 Manfred Spraul
37 support for big endian descriptors
38 Copyright (C) 2001 Manfred Spraul
39 * ethtool support (jgarzik)
40 * Replace some MII-related magic numbers with constants (jgarzik)
43 * enable pci_power_off
47 #define DRV_NAME "winbond-840"
48 #define DRV_VERSION "1.01-d"
49 #define DRV_RELDATE "Nov-17-2001"
52 /* Automatically extracted configuration info:
53 probe-func: winbond840_probe
54 config-in: tristate 'Winbond W89c840 Ethernet support' CONFIG_WINBOND_840
56 c-help-name: Winbond W89c840 PCI Ethernet support
57 c-help-symbol: CONFIG_WINBOND_840
58 c-help: This driver is for the Winbond W89c840 chip. It also works with
59 c-help: the TX9882 chip on the Compex RL100-ATX board.
60 c-help: More specific information and updates are available from
61 c-help: http://www.scyld.com/network/drivers.html
64 /* The user-configurable values.
65 These may be modified when a driver module is loaded.*/
67 static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
68 static int max_interrupt_work = 20;
69 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
70 The '840 uses a 64 element hash table based on the Ethernet CRC. */
71 static int multicast_filter_limit = 32;
73 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
74 Setting to > 1518 effectively disables this feature. */
75 static int rx_copybreak;
77 /* Used to pass the media type, etc.
78 Both 'options[]' and 'full_duplex[]' should exist for driver
80 The media type is usually passed in 'options[]'.
82 #define MAX_UNITS 8 /* More are supported, limit only on options */
83 static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
84 static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
86 /* Operational parameters that are set at compile time. */
88 /* Keep the ring sizes a power of two for compile efficiency.
89 The compiler will convert <unsigned>'%'<2^N> into a bit mask.
90 Making the Tx ring too large decreases the effectiveness of channel
91 bonding and packet priority.
92 There are no ill effects from too-large receive rings. */
93 #define TX_RING_SIZE 16
94 #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
95 #define TX_QUEUE_LEN_RESTART 5
96 #define RX_RING_SIZE 32
98 #define TX_BUFLIMIT (1024-128)
100 /* The presumed FIFO size for working around the Tx-FIFO-overflow bug.
101 To avoid overflowing we don't queue again until we have room for a
104 #define TX_FIFO_SIZE (2048)
105 #define TX_BUG_FIFO_LIMIT (TX_FIFO_SIZE-1514-16)
108 /* Operational parameters that usually are not changed. */
109 /* Time in jiffies before concluding the transmitter is hung. */
110 #define TX_TIMEOUT (2*HZ)
112 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
114 /* Include files, designed to support most kernel versions 2.0.0 and later. */
115 #include <linux/module.h>
116 #include <linux/kernel.h>
117 #include <linux/string.h>
118 #include <linux/timer.h>
119 #include <linux/errno.h>
120 #include <linux/ioport.h>
121 #include <linux/slab.h>
122 #include <linux/interrupt.h>
123 #include <linux/pci.h>
124 #include <linux/dma-mapping.h>
125 #include <linux/netdevice.h>
126 #include <linux/etherdevice.h>
127 #include <linux/skbuff.h>
128 #include <linux/init.h>
129 #include <linux/delay.h>
130 #include <linux/ethtool.h>
131 #include <linux/mii.h>
132 #include <linux/rtnetlink.h>
133 #include <linux/crc32.h>
134 #include <linux/bitops.h>
135 #include <asm/uaccess.h>
136 #include <asm/processor.h> /* Processor type for cache alignment. */
140 /* These identify the driver base version and may not be removed. */
141 static char version[] __devinitdata =
142 KERN_INFO DRV_NAME ".c:v" DRV_VERSION " (2.4 port) " DRV_RELDATE " Donald Becker <becker@scyld.com>\n"
143 KERN_INFO " http://www.scyld.com/network/drivers.html\n";
145 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
146 MODULE_DESCRIPTION("Winbond W89c840 Ethernet driver");
147 MODULE_LICENSE("GPL");
148 MODULE_VERSION(DRV_VERSION);
150 module_param(max_interrupt_work, int, 0);
151 module_param(debug, int, 0);
152 module_param(rx_copybreak, int, 0);
153 module_param(multicast_filter_limit, int, 0);
154 module_param_array(options, int, NULL, 0);
155 module_param_array(full_duplex, int, NULL, 0);
156 MODULE_PARM_DESC(max_interrupt_work, "winbond-840 maximum events handled per interrupt");
157 MODULE_PARM_DESC(debug, "winbond-840 debug level (0-6)");
158 MODULE_PARM_DESC(rx_copybreak, "winbond-840 copy breakpoint for copy-only-tiny-frames");
159 MODULE_PARM_DESC(multicast_filter_limit, "winbond-840 maximum number of filtered multicast addresses");
160 MODULE_PARM_DESC(options, "winbond-840: Bits 0-3: media type, bit 17: full duplex");
161 MODULE_PARM_DESC(full_duplex, "winbond-840 full duplex setting(s) (1)");
166 I. Board Compatibility
168 This driver is for the Winbond w89c840 chip.
170 II. Board-specific settings
174 III. Driver operation
176 This chip is very similar to the Digital 21*4* "Tulip" family. The first
177 twelve registers and the descriptor format are nearly identical. Read a
178 Tulip manual for operational details.
180 A significant difference is that the multicast filter and station address are
181 stored in registers rather than loaded through a pseudo-transmit packet.
183 Unlike the Tulip, transmit buffers are limited to 1KB. To transmit a
184 full-sized packet we must use both data buffers in a descriptor. Thus the
185 driver uses ring mode where descriptors are implicitly sequential in memory,
186 rather than using the second descriptor address as a chain pointer to
187 subsequent descriptors.
191 If you are going to almost clone a Tulip, why not go all the way and avoid
192 the need for a new driver?
196 http://www.scyld.com/expert/100mbps.html
197 http://www.scyld.com/expert/NWay.html
198 http://www.winbond.com.tw/
202 A horrible bug exists in the transmit FIFO. Apparently the chip doesn't
203 correctly detect a full FIFO, and queuing more than 2048 bytes may result in
204 silent data corruption.
206 Test with 'ping -s 10000' on a fast computer.
215 enum chip_capability_flags {
216 CanHaveMII=1, HasBrokenTx=2, AlwaysFDX=4, FDXOnNoMII=8,
219 static const struct pci_device_id w840_pci_tbl[] = {
220 { 0x1050, 0x0840, PCI_ANY_ID, 0x8153, 0, 0, 0 },
221 { 0x1050, 0x0840, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
222 { 0x11f6, 0x2011, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
225 MODULE_DEVICE_TABLE(pci, w840_pci_tbl);
228 netdev_res_size = 128, /* size of PCI BAR resource */
233 int drv_flags; /* Driver use, intended as capability flags. */
236 static const struct pci_id_info pci_id_tbl[] __devinitdata = {
237 { /* Sometime a Level-One switch card. */
238 "Winbond W89c840", CanHaveMII | HasBrokenTx | FDXOnNoMII},
239 { "Winbond W89c840", CanHaveMII | HasBrokenTx},
240 { "Compex RL100-ATX", CanHaveMII | HasBrokenTx},
241 { } /* terminate list. */
244 /* This driver was written to use PCI memory space, however some x86 systems
245 work only with I/O space accesses. Pass -DUSE_IO_OPS to use PCI I/O space
246 accesses instead of memory space. */
248 /* Offsets to the Command and Status Registers, "CSRs".
249 While similar to the Tulip, these registers are longword aligned.
250 Note: It's not useful to define symbolic names for every register bit in
251 the device. The name can only partially document the semantics and make
252 the driver longer and more difficult to read.
255 PCIBusCfg=0x00, TxStartDemand=0x04, RxStartDemand=0x08,
256 RxRingPtr=0x0C, TxRingPtr=0x10,
257 IntrStatus=0x14, NetworkConfig=0x18, IntrEnable=0x1C,
258 RxMissed=0x20, EECtrl=0x24, MIICtrl=0x24, BootRom=0x28, GPTimer=0x2C,
259 CurRxDescAddr=0x30, CurRxBufAddr=0x34, /* Debug use */
260 MulticastFilter0=0x38, MulticastFilter1=0x3C, StationAddr=0x40,
261 CurTxDescAddr=0x4C, CurTxBufAddr=0x50,
264 /* Bits in the interrupt status/enable registers. */
265 /* The bits in the Intr Status/Enable registers, mostly interrupt sources. */
266 enum intr_status_bits {
267 NormalIntr=0x10000, AbnormalIntr=0x8000,
268 IntrPCIErr=0x2000, TimerInt=0x800,
269 IntrRxDied=0x100, RxNoBuf=0x80, IntrRxDone=0x40,
270 TxFIFOUnderflow=0x20, RxErrIntr=0x10,
271 TxIdle=0x04, IntrTxStopped=0x02, IntrTxDone=0x01,
274 /* Bits in the NetworkConfig register. */
276 AcceptErr=0x80, AcceptRunt=0x40,
277 AcceptBroadcast=0x20, AcceptMulticast=0x10,
278 AcceptAllPhys=0x08, AcceptMyPhys=0x02,
282 MDIO_ShiftClk=0x10000, MDIO_DataIn=0x80000, MDIO_DataOut=0x20000,
283 MDIO_EnbOutput=0x40000, MDIO_EnbIn = 0x00000,
286 /* The Tulip Rx and Tx buffer descriptors. */
287 struct w840_rx_desc {
294 struct w840_tx_desc {
297 u32 buffer1, buffer2;
300 /* Bits in network_desc.status */
301 enum desc_status_bits {
302 DescOwn=0x80000000, DescEndRing=0x02000000, DescUseLink=0x01000000,
303 DescWholePkt=0x60000000, DescStartPkt=0x20000000, DescEndPkt=0x40000000,
307 #define MII_CNT 1 /* winbond only supports one MII */
308 struct netdev_private {
309 struct w840_rx_desc *rx_ring;
310 dma_addr_t rx_addr[RX_RING_SIZE];
311 struct w840_tx_desc *tx_ring;
312 dma_addr_t tx_addr[TX_RING_SIZE];
313 dma_addr_t ring_dma_addr;
314 /* The addresses of receive-in-place skbuffs. */
315 struct sk_buff* rx_skbuff[RX_RING_SIZE];
316 /* The saved address of a sent-in-place packet/buffer, for later free(). */
317 struct sk_buff* tx_skbuff[TX_RING_SIZE];
318 struct net_device_stats stats;
319 struct timer_list timer; /* Media monitoring timer. */
320 /* Frequently used values: keep some adjacent for cache effect. */
322 int chip_id, drv_flags;
323 struct pci_dev *pci_dev;
325 struct w840_rx_desc *rx_head_desc;
326 unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
327 unsigned int rx_buf_sz; /* Based on MTU+slack. */
328 unsigned int cur_tx, dirty_tx;
329 unsigned int tx_q_bytes;
330 unsigned int tx_full; /* The Tx queue is full. */
331 /* MII transceiver section. */
332 int mii_cnt; /* MII device addresses. */
333 unsigned char phys[MII_CNT]; /* MII device addresses, but only the first is used */
335 struct mii_if_info mii_if;
336 void __iomem *base_addr;
339 static int eeprom_read(void __iomem *ioaddr, int location);
340 static int mdio_read(struct net_device *dev, int phy_id, int location);
341 static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
342 static int netdev_open(struct net_device *dev);
343 static int update_link(struct net_device *dev);
344 static void netdev_timer(unsigned long data);
345 static void init_rxtx_rings(struct net_device *dev);
346 static void free_rxtx_rings(struct netdev_private *np);
347 static void init_registers(struct net_device *dev);
348 static void tx_timeout(struct net_device *dev);
349 static int alloc_ringdesc(struct net_device *dev);
350 static void free_ringdesc(struct netdev_private *np);
351 static int start_tx(struct sk_buff *skb, struct net_device *dev);
352 static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
353 static void netdev_error(struct net_device *dev, int intr_status);
354 static int netdev_rx(struct net_device *dev);
355 static u32 __set_rx_mode(struct net_device *dev);
356 static void set_rx_mode(struct net_device *dev);
357 static struct net_device_stats *get_stats(struct net_device *dev);
358 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
359 static struct ethtool_ops netdev_ethtool_ops;
360 static int netdev_close(struct net_device *dev);
364 static int __devinit w840_probe1 (struct pci_dev *pdev,
365 const struct pci_device_id *ent)
367 struct net_device *dev;
368 struct netdev_private *np;
370 int chip_idx = ent->driver_data;
372 int i, option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
373 void __iomem *ioaddr;
376 i = pci_enable_device(pdev);
379 pci_set_master(pdev);
383 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
384 printk(KERN_WARNING "Winbond-840: Device %s disabled due to DMA limitations.\n",
388 dev = alloc_etherdev(sizeof(*np));
391 SET_MODULE_OWNER(dev);
392 SET_NETDEV_DEV(dev, &pdev->dev);
394 if (pci_request_regions(pdev, DRV_NAME))
399 ioaddr = pci_iomap(pdev, bar, netdev_res_size);
401 goto err_out_free_res;
403 for (i = 0; i < 3; i++)
404 ((u16 *)dev->dev_addr)[i] = le16_to_cpu(eeprom_read(ioaddr, i));
406 /* Reset the chip to erase previous misconfiguration.
407 No hold time required! */
408 iowrite32(0x00000001, ioaddr + PCIBusCfg);
410 dev->base_addr = (unsigned long)ioaddr;
413 np = netdev_priv(dev);
415 np->chip_id = chip_idx;
416 np->drv_flags = pci_id_tbl[chip_idx].drv_flags;
417 spin_lock_init(&np->lock);
418 np->mii_if.dev = dev;
419 np->mii_if.mdio_read = mdio_read;
420 np->mii_if.mdio_write = mdio_write;
421 np->base_addr = ioaddr;
423 pci_set_drvdata(pdev, dev);
426 option = dev->mem_start;
428 /* The lower four bits are the media type. */
431 np->mii_if.full_duplex = 1;
433 printk(KERN_INFO "%s: ignoring user supplied media type %d",
434 dev->name, option & 15);
436 if (find_cnt < MAX_UNITS && full_duplex[find_cnt] > 0)
437 np->mii_if.full_duplex = 1;
439 if (np->mii_if.full_duplex)
440 np->mii_if.force_media = 1;
442 /* The chip-specific entries in the device structure. */
443 dev->open = &netdev_open;
444 dev->hard_start_xmit = &start_tx;
445 dev->stop = &netdev_close;
446 dev->get_stats = &get_stats;
447 dev->set_multicast_list = &set_rx_mode;
448 dev->do_ioctl = &netdev_ioctl;
449 dev->ethtool_ops = &netdev_ethtool_ops;
450 dev->tx_timeout = &tx_timeout;
451 dev->watchdog_timeo = TX_TIMEOUT;
453 i = register_netdev(dev);
455 goto err_out_cleardev;
457 printk(KERN_INFO "%s: %s at %p, ",
458 dev->name, pci_id_tbl[chip_idx].name, ioaddr);
459 for (i = 0; i < 5; i++)
460 printk("%2.2x:", dev->dev_addr[i]);
461 printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
463 if (np->drv_flags & CanHaveMII) {
464 int phy, phy_idx = 0;
465 for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
466 int mii_status = mdio_read(dev, phy, MII_BMSR);
467 if (mii_status != 0xffff && mii_status != 0x0000) {
468 np->phys[phy_idx++] = phy;
469 np->mii_if.advertising = mdio_read(dev, phy, MII_ADVERTISE);
470 np->mii = (mdio_read(dev, phy, MII_PHYSID1) << 16)+
471 mdio_read(dev, phy, MII_PHYSID2);
472 printk(KERN_INFO "%s: MII PHY %8.8xh found at address %d, status "
473 "0x%4.4x advertising %4.4x.\n",
474 dev->name, np->mii, phy, mii_status, np->mii_if.advertising);
477 np->mii_cnt = phy_idx;
478 np->mii_if.phy_id = np->phys[0];
480 printk(KERN_WARNING "%s: MII PHY not found -- this device may "
481 "not operate correctly.\n", dev->name);
489 pci_set_drvdata(pdev, NULL);
490 pci_iounmap(pdev, ioaddr);
492 pci_release_regions(pdev);
499 /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. These are
500 often serial bit streams generated by the host processor.
501 The example below is for the common 93c46 EEPROM, 64 16 bit words. */
503 /* Delay between EEPROM clock transitions.
504 No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
505 a delay. Note that pre-2.0.34 kernels had a cache-alignment bug that
506 made udelay() unreliable.
507 The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
510 #define eeprom_delay(ee_addr) ioread32(ee_addr)
512 enum EEPROM_Ctrl_Bits {
513 EE_ShiftClk=0x02, EE_Write0=0x801, EE_Write1=0x805,
514 EE_ChipSelect=0x801, EE_DataIn=0x08,
517 /* The EEPROM commands include the alway-set leading bit. */
519 EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
522 static int eeprom_read(void __iomem *addr, int location)
526 void __iomem *ee_addr = addr + EECtrl;
527 int read_cmd = location | EE_ReadCmd;
528 iowrite32(EE_ChipSelect, ee_addr);
530 /* Shift the read command bits out. */
531 for (i = 10; i >= 0; i--) {
532 short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
533 iowrite32(dataval, ee_addr);
534 eeprom_delay(ee_addr);
535 iowrite32(dataval | EE_ShiftClk, ee_addr);
536 eeprom_delay(ee_addr);
538 iowrite32(EE_ChipSelect, ee_addr);
539 eeprom_delay(ee_addr);
541 for (i = 16; i > 0; i--) {
542 iowrite32(EE_ChipSelect | EE_ShiftClk, ee_addr);
543 eeprom_delay(ee_addr);
544 retval = (retval << 1) | ((ioread32(ee_addr) & EE_DataIn) ? 1 : 0);
545 iowrite32(EE_ChipSelect, ee_addr);
546 eeprom_delay(ee_addr);
549 /* Terminate the EEPROM access. */
550 iowrite32(0, ee_addr);
554 /* MII transceiver control section.
555 Read and write the MII registers using software-generated serial
556 MDIO protocol. See the MII specifications or DP83840A data sheet
559 The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
560 met by back-to-back 33Mhz PCI cycles. */
561 #define mdio_delay(mdio_addr) ioread32(mdio_addr)
563 /* Set iff a MII transceiver on any interface requires mdio preamble.
564 This only set with older transceivers, so the extra
565 code size of a per-interface flag is not worthwhile. */
566 static char mii_preamble_required = 1;
568 #define MDIO_WRITE0 (MDIO_EnbOutput)
569 #define MDIO_WRITE1 (MDIO_DataOut | MDIO_EnbOutput)
571 /* Generate the preamble required for initial synchronization and
572 a few older transceivers. */
573 static void mdio_sync(void __iomem *mdio_addr)
577 /* Establish sync by sending at least 32 logic ones. */
578 while (--bits >= 0) {
579 iowrite32(MDIO_WRITE1, mdio_addr);
580 mdio_delay(mdio_addr);
581 iowrite32(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
582 mdio_delay(mdio_addr);
586 static int mdio_read(struct net_device *dev, int phy_id, int location)
588 struct netdev_private *np = netdev_priv(dev);
589 void __iomem *mdio_addr = np->base_addr + MIICtrl;
590 int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
593 if (mii_preamble_required)
594 mdio_sync(mdio_addr);
596 /* Shift the read command bits out. */
597 for (i = 15; i >= 0; i--) {
598 int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
600 iowrite32(dataval, mdio_addr);
601 mdio_delay(mdio_addr);
602 iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
603 mdio_delay(mdio_addr);
605 /* Read the two transition, 16 data, and wire-idle bits. */
606 for (i = 20; i > 0; i--) {
607 iowrite32(MDIO_EnbIn, mdio_addr);
608 mdio_delay(mdio_addr);
609 retval = (retval << 1) | ((ioread32(mdio_addr) & MDIO_DataIn) ? 1 : 0);
610 iowrite32(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
611 mdio_delay(mdio_addr);
613 return (retval>>1) & 0xffff;
616 static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
618 struct netdev_private *np = netdev_priv(dev);
619 void __iomem *mdio_addr = np->base_addr + MIICtrl;
620 int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
623 if (location == 4 && phy_id == np->phys[0])
624 np->mii_if.advertising = value;
626 if (mii_preamble_required)
627 mdio_sync(mdio_addr);
629 /* Shift the command bits out. */
630 for (i = 31; i >= 0; i--) {
631 int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
633 iowrite32(dataval, mdio_addr);
634 mdio_delay(mdio_addr);
635 iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
636 mdio_delay(mdio_addr);
638 /* Clear out extra bits. */
639 for (i = 2; i > 0; i--) {
640 iowrite32(MDIO_EnbIn, mdio_addr);
641 mdio_delay(mdio_addr);
642 iowrite32(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
643 mdio_delay(mdio_addr);
649 static int netdev_open(struct net_device *dev)
651 struct netdev_private *np = netdev_priv(dev);
652 void __iomem *ioaddr = np->base_addr;
655 iowrite32(0x00000001, ioaddr + PCIBusCfg); /* Reset */
657 netif_device_detach(dev);
658 i = request_irq(dev->irq, &intr_handler, IRQF_SHARED, dev->name, dev);
663 printk(KERN_DEBUG "%s: w89c840_open() irq %d.\n",
664 dev->name, dev->irq);
666 if((i=alloc_ringdesc(dev)))
669 spin_lock_irq(&np->lock);
670 netif_device_attach(dev);
672 spin_unlock_irq(&np->lock);
674 netif_start_queue(dev);
676 printk(KERN_DEBUG "%s: Done netdev_open().\n", dev->name);
678 /* Set the timer to check for link beat. */
679 init_timer(&np->timer);
680 np->timer.expires = jiffies + 1*HZ;
681 np->timer.data = (unsigned long)dev;
682 np->timer.function = &netdev_timer; /* timer handler */
683 add_timer(&np->timer);
686 netif_device_attach(dev);
690 #define MII_DAVICOM_DM9101 0x0181b800
692 static int update_link(struct net_device *dev)
694 struct netdev_private *np = netdev_priv(dev);
695 int duplex, fasteth, result, mii_reg;
698 mii_reg = mdio_read(dev, np->phys[0], MII_BMSR);
700 if (mii_reg == 0xffff)
702 /* reread: the link status bit is sticky */
703 mii_reg = mdio_read(dev, np->phys[0], MII_BMSR);
704 if (!(mii_reg & 0x4)) {
705 if (netif_carrier_ok(dev)) {
707 printk(KERN_INFO "%s: MII #%d reports no link. Disabling watchdog.\n",
708 dev->name, np->phys[0]);
709 netif_carrier_off(dev);
713 if (!netif_carrier_ok(dev)) {
715 printk(KERN_INFO "%s: MII #%d link is back. Enabling watchdog.\n",
716 dev->name, np->phys[0]);
717 netif_carrier_on(dev);
720 if ((np->mii & ~0xf) == MII_DAVICOM_DM9101) {
721 /* If the link partner doesn't support autonegotiation
722 * the MII detects it's abilities with the "parallel detection".
723 * Some MIIs update the LPA register to the result of the parallel
724 * detection, some don't.
725 * The Davicom PHY [at least 0181b800] doesn't.
726 * Instead bit 9 and 13 of the BMCR are updated to the result
727 * of the negotiation..
729 mii_reg = mdio_read(dev, np->phys[0], MII_BMCR);
730 duplex = mii_reg & BMCR_FULLDPLX;
731 fasteth = mii_reg & BMCR_SPEED100;
734 mii_reg = mdio_read(dev, np->phys[0], MII_LPA);
735 negotiated = mii_reg & np->mii_if.advertising;
737 duplex = (negotiated & LPA_100FULL) || ((negotiated & 0x02C0) == LPA_10FULL);
738 fasteth = negotiated & 0x380;
740 duplex |= np->mii_if.force_media;
741 /* remove fastether and fullduplex */
742 result = np->csr6 & ~0x20000200;
746 result |= 0x20000000;
747 if (result != np->csr6 && debug)
748 printk(KERN_INFO "%s: Setting %dMBit-%s-duplex based on MII#%d\n",
749 dev->name, fasteth ? 100 : 10,
750 duplex ? "full" : "half", np->phys[0]);
754 #define RXTX_TIMEOUT 2000
755 static inline void update_csr6(struct net_device *dev, int new)
757 struct netdev_private *np = netdev_priv(dev);
758 void __iomem *ioaddr = np->base_addr;
759 int limit = RXTX_TIMEOUT;
761 if (!netif_device_present(dev))
765 /* stop both Tx and Rx processes */
766 iowrite32(np->csr6 & ~0x2002, ioaddr + NetworkConfig);
767 /* wait until they have really stopped */
769 int csr5 = ioread32(ioaddr + IntrStatus);
772 t = (csr5 >> 17) & 0x07;
775 t = (csr5 >> 20) & 0x07;
782 printk(KERN_INFO "%s: couldn't stop rxtx, IntrStatus %xh.\n",
789 /* and restart them with the new configuration */
790 iowrite32(np->csr6, ioaddr + NetworkConfig);
792 np->mii_if.full_duplex = 1;
795 static void netdev_timer(unsigned long data)
797 struct net_device *dev = (struct net_device *)data;
798 struct netdev_private *np = netdev_priv(dev);
799 void __iomem *ioaddr = np->base_addr;
802 printk(KERN_DEBUG "%s: Media selection timer tick, status %8.8x "
804 dev->name, ioread32(ioaddr + IntrStatus),
805 ioread32(ioaddr + NetworkConfig));
806 spin_lock_irq(&np->lock);
807 update_csr6(dev, update_link(dev));
808 spin_unlock_irq(&np->lock);
809 np->timer.expires = jiffies + 10*HZ;
810 add_timer(&np->timer);
813 static void init_rxtx_rings(struct net_device *dev)
815 struct netdev_private *np = netdev_priv(dev);
818 np->rx_head_desc = &np->rx_ring[0];
819 np->tx_ring = (struct w840_tx_desc*)&np->rx_ring[RX_RING_SIZE];
821 /* Initial all Rx descriptors. */
822 for (i = 0; i < RX_RING_SIZE; i++) {
823 np->rx_ring[i].length = np->rx_buf_sz;
824 np->rx_ring[i].status = 0;
825 np->rx_skbuff[i] = NULL;
827 /* Mark the last entry as wrapping the ring. */
828 np->rx_ring[i-1].length |= DescEndRing;
830 /* Fill in the Rx buffers. Handle allocation failure gracefully. */
831 for (i = 0; i < RX_RING_SIZE; i++) {
832 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
833 np->rx_skbuff[i] = skb;
836 skb->dev = dev; /* Mark as being used by this device. */
837 np->rx_addr[i] = pci_map_single(np->pci_dev,skb->data,
838 np->rx_buf_sz,PCI_DMA_FROMDEVICE);
840 np->rx_ring[i].buffer1 = np->rx_addr[i];
841 np->rx_ring[i].status = DescOwn;
845 np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
847 /* Initialize the Tx descriptors */
848 for (i = 0; i < TX_RING_SIZE; i++) {
849 np->tx_skbuff[i] = NULL;
850 np->tx_ring[i].status = 0;
853 np->tx_q_bytes = np->dirty_tx = np->cur_tx = 0;
855 iowrite32(np->ring_dma_addr, np->base_addr + RxRingPtr);
856 iowrite32(np->ring_dma_addr+sizeof(struct w840_rx_desc)*RX_RING_SIZE,
857 np->base_addr + TxRingPtr);
861 static void free_rxtx_rings(struct netdev_private* np)
864 /* Free all the skbuffs in the Rx queue. */
865 for (i = 0; i < RX_RING_SIZE; i++) {
866 np->rx_ring[i].status = 0;
867 if (np->rx_skbuff[i]) {
868 pci_unmap_single(np->pci_dev,
870 np->rx_skbuff[i]->len,
872 dev_kfree_skb(np->rx_skbuff[i]);
874 np->rx_skbuff[i] = NULL;
876 for (i = 0; i < TX_RING_SIZE; i++) {
877 if (np->tx_skbuff[i]) {
878 pci_unmap_single(np->pci_dev,
880 np->tx_skbuff[i]->len,
882 dev_kfree_skb(np->tx_skbuff[i]);
884 np->tx_skbuff[i] = NULL;
888 static void init_registers(struct net_device *dev)
890 struct netdev_private *np = netdev_priv(dev);
891 void __iomem *ioaddr = np->base_addr;
894 for (i = 0; i < 6; i++)
895 iowrite8(dev->dev_addr[i], ioaddr + StationAddr + i);
897 /* Initialize other registers. */
899 i = (1<<20); /* Big-endian descriptors */
903 i |= (0x04<<2); /* skip length 4 u32 */
904 i |= 0x02; /* give Rx priority */
906 /* Configure the PCI bus bursts and FIFO thresholds.
907 486: Set 8 longword cache alignment, 8 longword burst.
908 586: Set 16 longword cache alignment, no burst limit.
909 Cache alignment bits 15:14 Burst length 13:8
910 0000 <not allowed> 0000 align to cache 0800 8 longwords
911 4000 8 longwords 0100 1 longword 1000 16 longwords
912 8000 16 longwords 0200 2 longwords 2000 32 longwords
913 C000 32 longwords 0400 4 longwords */
915 #if defined (__i386__) && !defined(MODULE)
916 /* When not a module we can work around broken '486 PCI boards. */
917 if (boot_cpu_data.x86 <= 4) {
919 printk(KERN_INFO "%s: This is a 386/486 PCI system, setting cache "
920 "alignment to 8 longwords.\n", dev->name);
924 #elif defined(__powerpc__) || defined(__i386__) || defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
926 #elif defined(__sparc__)
929 #warning Processor architecture undefined
932 iowrite32(i, ioaddr + PCIBusCfg);
935 /* 128 byte Tx threshold;
936 Transmit on; Receive on; */
937 update_csr6(dev, 0x00022002 | update_link(dev) | __set_rx_mode(dev));
939 /* Clear and Enable interrupts by setting the interrupt mask. */
940 iowrite32(0x1A0F5, ioaddr + IntrStatus);
941 iowrite32(0x1A0F5, ioaddr + IntrEnable);
943 iowrite32(0, ioaddr + RxStartDemand);
946 static void tx_timeout(struct net_device *dev)
948 struct netdev_private *np = netdev_priv(dev);
949 void __iomem *ioaddr = np->base_addr;
951 printk(KERN_WARNING "%s: Transmit timed out, status %8.8x,"
952 " resetting...\n", dev->name, ioread32(ioaddr + IntrStatus));
956 printk(KERN_DEBUG " Rx ring %p: ", np->rx_ring);
957 for (i = 0; i < RX_RING_SIZE; i++)
958 printk(" %8.8x", (unsigned int)np->rx_ring[i].status);
959 printk("\n"KERN_DEBUG" Tx ring %p: ", np->tx_ring);
960 for (i = 0; i < TX_RING_SIZE; i++)
961 printk(" %8.8x", np->tx_ring[i].status);
964 printk(KERN_DEBUG "Tx cur %d Tx dirty %d Tx Full %d, q bytes %d.\n",
965 np->cur_tx, np->dirty_tx, np->tx_full, np->tx_q_bytes);
966 printk(KERN_DEBUG "Tx Descriptor addr %xh.\n",ioread32(ioaddr+0x4C));
968 disable_irq(dev->irq);
969 spin_lock_irq(&np->lock);
971 * Under high load dirty_tx and the internal tx descriptor pointer
972 * come out of sync, thus perform a software reset and reinitialize
976 iowrite32(1, np->base_addr+PCIBusCfg);
980 init_rxtx_rings(dev);
982 spin_unlock_irq(&np->lock);
983 enable_irq(dev->irq);
985 netif_wake_queue(dev);
986 dev->trans_start = jiffies;
987 np->stats.tx_errors++;
991 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
992 static int alloc_ringdesc(struct net_device *dev)
994 struct netdev_private *np = netdev_priv(dev);
996 np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
998 np->rx_ring = pci_alloc_consistent(np->pci_dev,
999 sizeof(struct w840_rx_desc)*RX_RING_SIZE +
1000 sizeof(struct w840_tx_desc)*TX_RING_SIZE,
1001 &np->ring_dma_addr);
1004 init_rxtx_rings(dev);
1008 static void free_ringdesc(struct netdev_private *np)
1010 pci_free_consistent(np->pci_dev,
1011 sizeof(struct w840_rx_desc)*RX_RING_SIZE +
1012 sizeof(struct w840_tx_desc)*TX_RING_SIZE,
1013 np->rx_ring, np->ring_dma_addr);
1017 static int start_tx(struct sk_buff *skb, struct net_device *dev)
1019 struct netdev_private *np = netdev_priv(dev);
1022 /* Caution: the write order is important here, set the field
1023 with the "ownership" bits last. */
1025 /* Calculate the next Tx descriptor entry. */
1026 entry = np->cur_tx % TX_RING_SIZE;
1028 np->tx_addr[entry] = pci_map_single(np->pci_dev,
1029 skb->data,skb->len, PCI_DMA_TODEVICE);
1030 np->tx_skbuff[entry] = skb;
1032 np->tx_ring[entry].buffer1 = np->tx_addr[entry];
1033 if (skb->len < TX_BUFLIMIT) {
1034 np->tx_ring[entry].length = DescWholePkt | skb->len;
1036 int len = skb->len - TX_BUFLIMIT;
1038 np->tx_ring[entry].buffer2 = np->tx_addr[entry]+TX_BUFLIMIT;
1039 np->tx_ring[entry].length = DescWholePkt | (len << 11) | TX_BUFLIMIT;
1041 if(entry == TX_RING_SIZE-1)
1042 np->tx_ring[entry].length |= DescEndRing;
1044 /* Now acquire the irq spinlock.
1045 * The difficult race is the the ordering between
1046 * increasing np->cur_tx and setting DescOwn:
1047 * - if np->cur_tx is increased first the interrupt
1048 * handler could consider the packet as transmitted
1049 * since DescOwn is cleared.
1050 * - If DescOwn is set first the NIC could report the
1051 * packet as sent, but the interrupt handler would ignore it
1052 * since the np->cur_tx was not yet increased.
1054 spin_lock_irq(&np->lock);
1057 wmb(); /* flush length, buffer1, buffer2 */
1058 np->tx_ring[entry].status = DescOwn;
1059 wmb(); /* flush status and kick the hardware */
1060 iowrite32(0, np->base_addr + TxStartDemand);
1061 np->tx_q_bytes += skb->len;
1062 /* Work around horrible bug in the chip by marking the queue as full
1063 when we do not have FIFO room for a maximum sized packet. */
1064 if (np->cur_tx - np->dirty_tx > TX_QUEUE_LEN ||
1065 ((np->drv_flags & HasBrokenTx) && np->tx_q_bytes > TX_BUG_FIFO_LIMIT)) {
1066 netif_stop_queue(dev);
1070 spin_unlock_irq(&np->lock);
1072 dev->trans_start = jiffies;
1075 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1076 dev->name, np->cur_tx, entry);
1081 static void netdev_tx_done(struct net_device *dev)
1083 struct netdev_private *np = netdev_priv(dev);
1084 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1085 int entry = np->dirty_tx % TX_RING_SIZE;
1086 int tx_status = np->tx_ring[entry].status;
1090 if (tx_status & 0x8000) { /* There was an error, log it. */
1091 #ifndef final_version
1093 printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
1094 dev->name, tx_status);
1096 np->stats.tx_errors++;
1097 if (tx_status & 0x0104) np->stats.tx_aborted_errors++;
1098 if (tx_status & 0x0C80) np->stats.tx_carrier_errors++;
1099 if (tx_status & 0x0200) np->stats.tx_window_errors++;
1100 if (tx_status & 0x0002) np->stats.tx_fifo_errors++;
1101 if ((tx_status & 0x0080) && np->mii_if.full_duplex == 0)
1102 np->stats.tx_heartbeat_errors++;
1104 #ifndef final_version
1106 printk(KERN_DEBUG "%s: Transmit slot %d ok, Tx status %8.8x.\n",
1107 dev->name, entry, tx_status);
1109 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1110 np->stats.collisions += (tx_status >> 3) & 15;
1111 np->stats.tx_packets++;
1113 /* Free the original skb. */
1114 pci_unmap_single(np->pci_dev,np->tx_addr[entry],
1115 np->tx_skbuff[entry]->len,
1117 np->tx_q_bytes -= np->tx_skbuff[entry]->len;
1118 dev_kfree_skb_irq(np->tx_skbuff[entry]);
1119 np->tx_skbuff[entry] = NULL;
1122 np->cur_tx - np->dirty_tx < TX_QUEUE_LEN_RESTART &&
1123 np->tx_q_bytes < TX_BUG_FIFO_LIMIT) {
1124 /* The ring is no longer full, clear tbusy. */
1127 netif_wake_queue(dev);
1131 /* The interrupt handler does all of the Rx thread work and cleans up
1132 after the Tx thread. */
1133 static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
1135 struct net_device *dev = (struct net_device *)dev_instance;
1136 struct netdev_private *np = netdev_priv(dev);
1137 void __iomem *ioaddr = np->base_addr;
1138 int work_limit = max_interrupt_work;
1141 if (!netif_device_present(dev))
1144 u32 intr_status = ioread32(ioaddr + IntrStatus);
1146 /* Acknowledge all of the current interrupt sources ASAP. */
1147 iowrite32(intr_status & 0x001ffff, ioaddr + IntrStatus);
1150 printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
1151 dev->name, intr_status);
1153 if ((intr_status & (NormalIntr|AbnormalIntr)) == 0)
1158 if (intr_status & (IntrRxDone | RxNoBuf))
1160 if (intr_status & RxNoBuf)
1161 iowrite32(0, ioaddr + RxStartDemand);
1163 if (intr_status & (TxIdle | IntrTxDone) &&
1164 np->cur_tx != np->dirty_tx) {
1165 spin_lock(&np->lock);
1166 netdev_tx_done(dev);
1167 spin_unlock(&np->lock);
1170 /* Abnormal error summary/uncommon events handlers. */
1171 if (intr_status & (AbnormalIntr | TxFIFOUnderflow | IntrPCIErr |
1172 TimerInt | IntrTxStopped))
1173 netdev_error(dev, intr_status);
1175 if (--work_limit < 0) {
1176 printk(KERN_WARNING "%s: Too much work at interrupt, "
1177 "status=0x%4.4x.\n", dev->name, intr_status);
1178 /* Set the timer to re-enable the other interrupts after
1180 spin_lock(&np->lock);
1181 if (netif_device_present(dev)) {
1182 iowrite32(AbnormalIntr | TimerInt, ioaddr + IntrEnable);
1183 iowrite32(10, ioaddr + GPTimer);
1185 spin_unlock(&np->lock);
1191 printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1192 dev->name, ioread32(ioaddr + IntrStatus));
1193 return IRQ_RETVAL(handled);
1196 /* This routine is logically part of the interrupt handler, but separated
1197 for clarity and better register allocation. */
1198 static int netdev_rx(struct net_device *dev)
1200 struct netdev_private *np = netdev_priv(dev);
1201 int entry = np->cur_rx % RX_RING_SIZE;
1202 int work_limit = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1205 printk(KERN_DEBUG " In netdev_rx(), entry %d status %4.4x.\n",
1206 entry, np->rx_ring[entry].status);
1209 /* If EOP is set on the next entry, it's a new packet. Send it up. */
1210 while (--work_limit >= 0) {
1211 struct w840_rx_desc *desc = np->rx_head_desc;
1212 s32 status = desc->status;
1215 printk(KERN_DEBUG " netdev_rx() status was %8.8x.\n",
1219 if ((status & 0x38008300) != 0x0300) {
1220 if ((status & 0x38000300) != 0x0300) {
1221 /* Ingore earlier buffers. */
1222 if ((status & 0xffff) != 0x7fff) {
1223 printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
1224 "multiple buffers, entry %#x status %4.4x!\n",
1225 dev->name, np->cur_rx, status);
1226 np->stats.rx_length_errors++;
1228 } else if (status & 0x8000) {
1229 /* There was a fatal error. */
1231 printk(KERN_DEBUG "%s: Receive error, Rx status %8.8x.\n",
1233 np->stats.rx_errors++; /* end of a packet.*/
1234 if (status & 0x0890) np->stats.rx_length_errors++;
1235 if (status & 0x004C) np->stats.rx_frame_errors++;
1236 if (status & 0x0002) np->stats.rx_crc_errors++;
1239 struct sk_buff *skb;
1240 /* Omit the four octet CRC from the length. */
1241 int pkt_len = ((status >> 16) & 0x7ff) - 4;
1243 #ifndef final_version
1245 printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d"
1246 " status %x.\n", pkt_len, status);
1248 /* Check if the packet is long enough to accept without copying
1249 to a minimally-sized skbuff. */
1250 if (pkt_len < rx_copybreak
1251 && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1253 skb_reserve(skb, 2); /* 16 byte align the IP header */
1254 pci_dma_sync_single_for_cpu(np->pci_dev,np->rx_addr[entry],
1255 np->rx_skbuff[entry]->len,
1256 PCI_DMA_FROMDEVICE);
1257 eth_copy_and_sum(skb, np->rx_skbuff[entry]->data, pkt_len, 0);
1258 skb_put(skb, pkt_len);
1259 pci_dma_sync_single_for_device(np->pci_dev,np->rx_addr[entry],
1260 np->rx_skbuff[entry]->len,
1261 PCI_DMA_FROMDEVICE);
1263 pci_unmap_single(np->pci_dev,np->rx_addr[entry],
1264 np->rx_skbuff[entry]->len,
1265 PCI_DMA_FROMDEVICE);
1266 skb_put(skb = np->rx_skbuff[entry], pkt_len);
1267 np->rx_skbuff[entry] = NULL;
1269 #ifndef final_version /* Remove after testing. */
1270 /* You will want this info for the initial debug. */
1272 printk(KERN_DEBUG " Rx data %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:"
1273 "%2.2x %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x %2.2x%2.2x "
1275 skb->data[0], skb->data[1], skb->data[2], skb->data[3],
1276 skb->data[4], skb->data[5], skb->data[6], skb->data[7],
1277 skb->data[8], skb->data[9], skb->data[10],
1278 skb->data[11], skb->data[12], skb->data[13],
1279 skb->data[14], skb->data[15], skb->data[16],
1282 skb->protocol = eth_type_trans(skb, dev);
1284 dev->last_rx = jiffies;
1285 np->stats.rx_packets++;
1286 np->stats.rx_bytes += pkt_len;
1288 entry = (++np->cur_rx) % RX_RING_SIZE;
1289 np->rx_head_desc = &np->rx_ring[entry];
1292 /* Refill the Rx ring buffers. */
1293 for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1294 struct sk_buff *skb;
1295 entry = np->dirty_rx % RX_RING_SIZE;
1296 if (np->rx_skbuff[entry] == NULL) {
1297 skb = dev_alloc_skb(np->rx_buf_sz);
1298 np->rx_skbuff[entry] = skb;
1300 break; /* Better luck next round. */
1301 skb->dev = dev; /* Mark as being used by this device. */
1302 np->rx_addr[entry] = pci_map_single(np->pci_dev,
1304 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1305 np->rx_ring[entry].buffer1 = np->rx_addr[entry];
1308 np->rx_ring[entry].status = DescOwn;
1314 static void netdev_error(struct net_device *dev, int intr_status)
1316 struct netdev_private *np = netdev_priv(dev);
1317 void __iomem *ioaddr = np->base_addr;
1320 printk(KERN_DEBUG "%s: Abnormal event, %8.8x.\n",
1321 dev->name, intr_status);
1322 if (intr_status == 0xffffffff)
1324 spin_lock(&np->lock);
1325 if (intr_status & TxFIFOUnderflow) {
1327 /* Bump up the Tx threshold */
1329 /* This causes lots of dropped packets,
1330 * and under high load even tx_timeouts
1332 new = np->csr6 + 0x4000;
1334 new = (np->csr6 >> 14)&0x7f;
1338 new = 127; /* load full packet before starting */
1339 new = (np->csr6 & ~(0x7F << 14)) | (new<<14);
1341 printk(KERN_DEBUG "%s: Tx underflow, new csr6 %8.8x.\n",
1343 update_csr6(dev, new);
1345 if (intr_status & IntrRxDied) { /* Missed a Rx frame. */
1346 np->stats.rx_errors++;
1348 if (intr_status & TimerInt) {
1349 /* Re-enable other interrupts. */
1350 if (netif_device_present(dev))
1351 iowrite32(0x1A0F5, ioaddr + IntrEnable);
1353 np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1354 iowrite32(0, ioaddr + RxStartDemand);
1355 spin_unlock(&np->lock);
1358 static struct net_device_stats *get_stats(struct net_device *dev)
1360 struct netdev_private *np = netdev_priv(dev);
1361 void __iomem *ioaddr = np->base_addr;
1363 /* The chip only need report frame silently dropped. */
1364 spin_lock_irq(&np->lock);
1365 if (netif_running(dev) && netif_device_present(dev))
1366 np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1367 spin_unlock_irq(&np->lock);
1373 static u32 __set_rx_mode(struct net_device *dev)
1375 struct netdev_private *np = netdev_priv(dev);
1376 void __iomem *ioaddr = np->base_addr;
1377 u32 mc_filter[2]; /* Multicast hash filter */
1380 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1381 /* Unconditionally log net taps. */
1382 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1383 memset(mc_filter, 0xff, sizeof(mc_filter));
1384 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptAllPhys
1386 } else if ((dev->mc_count > multicast_filter_limit)
1387 || (dev->flags & IFF_ALLMULTI)) {
1388 /* Too many to match, or accept all multicasts. */
1389 memset(mc_filter, 0xff, sizeof(mc_filter));
1390 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1392 struct dev_mc_list *mclist;
1394 memset(mc_filter, 0, sizeof(mc_filter));
1395 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1396 i++, mclist = mclist->next) {
1397 int filterbit = (ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26) ^ 0x3F;
1399 mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
1401 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1403 iowrite32(mc_filter[0], ioaddr + MulticastFilter0);
1404 iowrite32(mc_filter[1], ioaddr + MulticastFilter1);
1408 static void set_rx_mode(struct net_device *dev)
1410 struct netdev_private *np = netdev_priv(dev);
1411 u32 rx_mode = __set_rx_mode(dev);
1412 spin_lock_irq(&np->lock);
1413 update_csr6(dev, (np->csr6 & ~0x00F8) | rx_mode);
1414 spin_unlock_irq(&np->lock);
1417 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1419 struct netdev_private *np = netdev_priv(dev);
1421 strcpy (info->driver, DRV_NAME);
1422 strcpy (info->version, DRV_VERSION);
1423 strcpy (info->bus_info, pci_name(np->pci_dev));
1426 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1428 struct netdev_private *np = netdev_priv(dev);
1431 spin_lock_irq(&np->lock);
1432 rc = mii_ethtool_gset(&np->mii_if, cmd);
1433 spin_unlock_irq(&np->lock);
1438 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1440 struct netdev_private *np = netdev_priv(dev);
1443 spin_lock_irq(&np->lock);
1444 rc = mii_ethtool_sset(&np->mii_if, cmd);
1445 spin_unlock_irq(&np->lock);
1450 static int netdev_nway_reset(struct net_device *dev)
1452 struct netdev_private *np = netdev_priv(dev);
1453 return mii_nway_restart(&np->mii_if);
1456 static u32 netdev_get_link(struct net_device *dev)
1458 struct netdev_private *np = netdev_priv(dev);
1459 return mii_link_ok(&np->mii_if);
1462 static u32 netdev_get_msglevel(struct net_device *dev)
1467 static void netdev_set_msglevel(struct net_device *dev, u32 value)
1472 static struct ethtool_ops netdev_ethtool_ops = {
1473 .get_drvinfo = netdev_get_drvinfo,
1474 .get_settings = netdev_get_settings,
1475 .set_settings = netdev_set_settings,
1476 .nway_reset = netdev_nway_reset,
1477 .get_link = netdev_get_link,
1478 .get_msglevel = netdev_get_msglevel,
1479 .set_msglevel = netdev_set_msglevel,
1480 .get_sg = ethtool_op_get_sg,
1481 .get_tx_csum = ethtool_op_get_tx_csum,
1484 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1486 struct mii_ioctl_data *data = if_mii(rq);
1487 struct netdev_private *np = netdev_priv(dev);
1490 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1491 data->phy_id = ((struct netdev_private *)netdev_priv(dev))->phys[0] & 0x1f;
1494 case SIOCGMIIREG: /* Read MII PHY register. */
1495 spin_lock_irq(&np->lock);
1496 data->val_out = mdio_read(dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
1497 spin_unlock_irq(&np->lock);
1500 case SIOCSMIIREG: /* Write MII PHY register. */
1501 if (!capable(CAP_NET_ADMIN))
1503 spin_lock_irq(&np->lock);
1504 mdio_write(dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
1505 spin_unlock_irq(&np->lock);
1512 static int netdev_close(struct net_device *dev)
1514 struct netdev_private *np = netdev_priv(dev);
1515 void __iomem *ioaddr = np->base_addr;
1517 netif_stop_queue(dev);
1520 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %8.8x "
1521 "Config %8.8x.\n", dev->name, ioread32(ioaddr + IntrStatus),
1522 ioread32(ioaddr + NetworkConfig));
1523 printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
1524 dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx);
1527 /* Stop the chip's Tx and Rx processes. */
1528 spin_lock_irq(&np->lock);
1529 netif_device_detach(dev);
1530 update_csr6(dev, 0);
1531 iowrite32(0x0000, ioaddr + IntrEnable);
1532 spin_unlock_irq(&np->lock);
1534 free_irq(dev->irq, dev);
1536 netif_device_attach(dev);
1538 if (ioread32(ioaddr + NetworkConfig) != 0xffffffff)
1539 np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1545 printk(KERN_DEBUG" Tx ring at %8.8x:\n",
1547 for (i = 0; i < TX_RING_SIZE; i++)
1548 printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x.\n",
1549 i, np->tx_ring[i].length,
1550 np->tx_ring[i].status, np->tx_ring[i].buffer1);
1551 printk("\n"KERN_DEBUG " Rx ring %8.8x:\n",
1553 for (i = 0; i < RX_RING_SIZE; i++) {
1554 printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x\n",
1555 i, np->rx_ring[i].length,
1556 np->rx_ring[i].status, np->rx_ring[i].buffer1);
1559 #endif /* __i386__ debugging only */
1561 del_timer_sync(&np->timer);
1563 free_rxtx_rings(np);
1569 static void __devexit w840_remove1 (struct pci_dev *pdev)
1571 struct net_device *dev = pci_get_drvdata(pdev);
1574 struct netdev_private *np = netdev_priv(dev);
1575 unregister_netdev(dev);
1576 pci_release_regions(pdev);
1577 pci_iounmap(pdev, np->base_addr);
1581 pci_set_drvdata(pdev, NULL);
1587 * suspend/resume synchronization:
1588 * - open, close, do_ioctl:
1589 * rtnl_lock, & netif_device_detach after the rtnl_unlock.
1591 * spin_lock_irq(np->lock), doesn't touch hw if not present
1592 * - hard_start_xmit:
1593 * synchronize_irq + netif_tx_disable;
1595 * netif_device_detach + netif_tx_disable;
1596 * - set_multicast_list
1597 * netif_device_detach + netif_tx_disable;
1598 * - interrupt handler
1599 * doesn't touch hw if not present, synchronize_irq waits for
1600 * running instances of the interrupt handler.
1602 * Disabling hw requires clearing csr6 & IntrEnable.
1603 * update_csr6 & all function that write IntrEnable check netif_device_present
1604 * before settings any bits.
1606 * Detach must occur under spin_unlock_irq(), interrupts from a detached
1607 * device would cause an irq storm.
1609 static int w840_suspend (struct pci_dev *pdev, pm_message_t state)
1611 struct net_device *dev = pci_get_drvdata (pdev);
1612 struct netdev_private *np = netdev_priv(dev);
1613 void __iomem *ioaddr = np->base_addr;
1616 if (netif_running (dev)) {
1617 del_timer_sync(&np->timer);
1619 spin_lock_irq(&np->lock);
1620 netif_device_detach(dev);
1621 update_csr6(dev, 0);
1622 iowrite32(0, ioaddr + IntrEnable);
1623 spin_unlock_irq(&np->lock);
1625 synchronize_irq(dev->irq);
1626 netif_tx_disable(dev);
1628 np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1630 /* no more hardware accesses behind this line. */
1633 if (ioread32(ioaddr + IntrEnable)) BUG();
1635 /* pci_power_off(pdev, -1); */
1637 free_rxtx_rings(np);
1639 netif_device_detach(dev);
1645 static int w840_resume (struct pci_dev *pdev)
1647 struct net_device *dev = pci_get_drvdata (pdev);
1648 struct netdev_private *np = netdev_priv(dev);
1651 if (netif_device_present(dev))
1652 goto out; /* device not suspended */
1653 if (netif_running(dev)) {
1654 pci_enable_device(pdev);
1655 /* pci_power_on(pdev); */
1657 spin_lock_irq(&np->lock);
1658 iowrite32(1, np->base_addr+PCIBusCfg);
1659 ioread32(np->base_addr+PCIBusCfg);
1661 netif_device_attach(dev);
1662 init_rxtx_rings(dev);
1663 init_registers(dev);
1664 spin_unlock_irq(&np->lock);
1666 netif_wake_queue(dev);
1668 mod_timer(&np->timer, jiffies + 1*HZ);
1670 netif_device_attach(dev);
1678 static struct pci_driver w840_driver = {
1680 .id_table = w840_pci_tbl,
1681 .probe = w840_probe1,
1682 .remove = __devexit_p(w840_remove1),
1684 .suspend = w840_suspend,
1685 .resume = w840_resume,
1689 static int __init w840_init(void)
1692 return pci_module_init(&w840_driver);
1695 static void __exit w840_exit(void)
1697 pci_unregister_driver(&w840_driver);
1700 module_init(w840_init);
1701 module_exit(w840_exit);