]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/tulip/winbond-840.c
Merge git://git.linux-nfs.org/pub/linux/nfs-2.6
[mirror_ubuntu-artful-kernel.git] / drivers / net / tulip / winbond-840.c
1 /* winbond-840.c: A Linux PCI network adapter device driver. */
2 /*
3 Written 1998-2001 by Donald Becker.
4
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.
11
12 The author may be reached as becker@scyld.com, or C/O
13 Scyld Computing Corporation
14 410 Severn Ave., Suite 210
15 Annapolis MD 21403
16
17 Support and updates available at
18 http://www.scyld.com/network/drivers.html
19
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
23 as an improvement.
24
25 Changelog:
26 * ported to 2.4
27 ???
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
35 * further cleanups
36 power management.
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)
41
42 TODO:
43 * enable pci_power_off
44 * Wake-On-LAN
45 */
46
47 #define DRV_NAME "winbond-840"
48 #define DRV_VERSION "1.01-d"
49 #define DRV_RELDATE "Nov-17-2001"
50
51
52 /* Automatically extracted configuration info:
53 probe-func: winbond840_probe
54 config-in: tristate 'Winbond W89c840 Ethernet support' CONFIG_WINBOND_840
55
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
62 */
63
64 /* The user-configurable values.
65 These may be modified when a driver module is loaded.*/
66
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;
72
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;
76
77 /* Used to pass the media type, etc.
78 Both 'options[]' and 'full_duplex[]' should exist for driver
79 interoperability.
80 The media type is usually passed in 'options[]'.
81 */
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};
85
86 /* Operational parameters that are set at compile time. */
87
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
97
98 #define TX_BUFLIMIT (1024-128)
99
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
102 full-size packet.
103 */
104 #define TX_FIFO_SIZE (2048)
105 #define TX_BUG_FIFO_LIMIT (TX_FIFO_SIZE-1514-16)
106
107
108 /* Operational parameters that usually are not changed. */
109 /* Time in jiffies before concluding the transmitter is hung. */
110 #define TX_TIMEOUT (2*HZ)
111
112 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
113
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. */
137 #include <asm/io.h>
138 #include <asm/irq.h>
139
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";
144
145 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
146 MODULE_DESCRIPTION("Winbond W89c840 Ethernet driver");
147 MODULE_LICENSE("GPL");
148 MODULE_VERSION(DRV_VERSION);
149
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)");
162
163 /*
164 Theory of Operation
165
166 I. Board Compatibility
167
168 This driver is for the Winbond w89c840 chip.
169
170 II. Board-specific settings
171
172 None.
173
174 III. Driver operation
175
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.
179
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.
182
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.
188
189 IV. Notes
190
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?
193
194 IVb. References
195
196 http://www.scyld.com/expert/100mbps.html
197 http://www.scyld.com/expert/NWay.html
198 http://www.winbond.com.tw/
199
200 IVc. Errata
201
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.
205
206 Test with 'ping -s 10000' on a fast computer.
207
208 */
209
210
211
212 /*
213 PCI probe table.
214 */
215 enum chip_capability_flags {
216 CanHaveMII=1, HasBrokenTx=2, AlwaysFDX=4, FDXOnNoMII=8,
217 };
218
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 },
223 { }
224 };
225 MODULE_DEVICE_TABLE(pci, w840_pci_tbl);
226
227 enum {
228 netdev_res_size = 128, /* size of PCI BAR resource */
229 };
230
231 struct pci_id_info {
232 const char *name;
233 int drv_flags; /* Driver use, intended as capability flags. */
234 };
235
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. */
242 };
243
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. */
247
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.
253 */
254 enum w840_offsets {
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,
262 };
263
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,
272 };
273
274 /* Bits in the NetworkConfig register. */
275 enum rx_mode_bits {
276 AcceptErr=0x80, AcceptRunt=0x40,
277 AcceptBroadcast=0x20, AcceptMulticast=0x10,
278 AcceptAllPhys=0x08, AcceptMyPhys=0x02,
279 };
280
281 enum mii_reg_bits {
282 MDIO_ShiftClk=0x10000, MDIO_DataIn=0x80000, MDIO_DataOut=0x20000,
283 MDIO_EnbOutput=0x40000, MDIO_EnbIn = 0x00000,
284 };
285
286 /* The Tulip Rx and Tx buffer descriptors. */
287 struct w840_rx_desc {
288 s32 status;
289 s32 length;
290 u32 buffer1;
291 u32 buffer2;
292 };
293
294 struct w840_tx_desc {
295 s32 status;
296 s32 length;
297 u32 buffer1, buffer2;
298 };
299
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,
304 DescIntr=0x80000000,
305 };
306
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. */
321 spinlock_t lock;
322 int chip_id, drv_flags;
323 struct pci_dev *pci_dev;
324 int csr6;
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 */
334 u32 mii;
335 struct mii_if_info mii_if;
336 void __iomem *base_addr;
337 };
338
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);
361
362
363
364 static int __devinit w840_probe1 (struct pci_dev *pdev,
365 const struct pci_device_id *ent)
366 {
367 struct net_device *dev;
368 struct netdev_private *np;
369 static int find_cnt;
370 int chip_idx = ent->driver_data;
371 int irq;
372 int i, option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
373 void __iomem *ioaddr;
374 int bar = 1;
375
376 i = pci_enable_device(pdev);
377 if (i) return i;
378
379 pci_set_master(pdev);
380
381 irq = pdev->irq;
382
383 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
384 printk(KERN_WARNING "Winbond-840: Device %s disabled due to DMA limitations.\n",
385 pci_name(pdev));
386 return -EIO;
387 }
388 dev = alloc_etherdev(sizeof(*np));
389 if (!dev)
390 return -ENOMEM;
391 SET_MODULE_OWNER(dev);
392 SET_NETDEV_DEV(dev, &pdev->dev);
393
394 if (pci_request_regions(pdev, DRV_NAME))
395 goto err_out_netdev;
396 #ifdef USE_IO_OPS
397 bar = 0;
398 #endif
399 ioaddr = pci_iomap(pdev, bar, netdev_res_size);
400 if (!ioaddr)
401 goto err_out_free_res;
402
403 for (i = 0; i < 3; i++)
404 ((u16 *)dev->dev_addr)[i] = le16_to_cpu(eeprom_read(ioaddr, i));
405
406 /* Reset the chip to erase previous misconfiguration.
407 No hold time required! */
408 iowrite32(0x00000001, ioaddr + PCIBusCfg);
409
410 dev->base_addr = (unsigned long)ioaddr;
411 dev->irq = irq;
412
413 np = netdev_priv(dev);
414 np->pci_dev = pdev;
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;
422
423 pci_set_drvdata(pdev, dev);
424
425 if (dev->mem_start)
426 option = dev->mem_start;
427
428 /* The lower four bits are the media type. */
429 if (option > 0) {
430 if (option & 0x200)
431 np->mii_if.full_duplex = 1;
432 if (option & 15)
433 printk(KERN_INFO "%s: ignoring user supplied media type %d",
434 dev->name, option & 15);
435 }
436 if (find_cnt < MAX_UNITS && full_duplex[find_cnt] > 0)
437 np->mii_if.full_duplex = 1;
438
439 if (np->mii_if.full_duplex)
440 np->mii_if.force_media = 1;
441
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;
452
453 i = register_netdev(dev);
454 if (i)
455 goto err_out_cleardev;
456
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);
462
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);
475 }
476 }
477 np->mii_cnt = phy_idx;
478 np->mii_if.phy_id = np->phys[0];
479 if (phy_idx == 0) {
480 printk(KERN_WARNING "%s: MII PHY not found -- this device may "
481 "not operate correctly.\n", dev->name);
482 }
483 }
484
485 find_cnt++;
486 return 0;
487
488 err_out_cleardev:
489 pci_set_drvdata(pdev, NULL);
490 pci_iounmap(pdev, ioaddr);
491 err_out_free_res:
492 pci_release_regions(pdev);
493 err_out_netdev:
494 free_netdev (dev);
495 return -ENODEV;
496 }
497
498
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. */
502
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
508 depricated.
509 */
510 #define eeprom_delay(ee_addr) ioread32(ee_addr)
511
512 enum EEPROM_Ctrl_Bits {
513 EE_ShiftClk=0x02, EE_Write0=0x801, EE_Write1=0x805,
514 EE_ChipSelect=0x801, EE_DataIn=0x08,
515 };
516
517 /* The EEPROM commands include the alway-set leading bit. */
518 enum EEPROM_Cmds {
519 EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
520 };
521
522 static int eeprom_read(void __iomem *addr, int location)
523 {
524 int i;
525 int retval = 0;
526 void __iomem *ee_addr = addr + EECtrl;
527 int read_cmd = location | EE_ReadCmd;
528 iowrite32(EE_ChipSelect, ee_addr);
529
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);
537 }
538 iowrite32(EE_ChipSelect, ee_addr);
539 eeprom_delay(ee_addr);
540
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);
547 }
548
549 /* Terminate the EEPROM access. */
550 iowrite32(0, ee_addr);
551 return retval;
552 }
553
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
557 for details.
558
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)
562
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;
567
568 #define MDIO_WRITE0 (MDIO_EnbOutput)
569 #define MDIO_WRITE1 (MDIO_DataOut | MDIO_EnbOutput)
570
571 /* Generate the preamble required for initial synchronization and
572 a few older transceivers. */
573 static void mdio_sync(void __iomem *mdio_addr)
574 {
575 int bits = 32;
576
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);
583 }
584 }
585
586 static int mdio_read(struct net_device *dev, int phy_id, int location)
587 {
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;
591 int i, retval = 0;
592
593 if (mii_preamble_required)
594 mdio_sync(mdio_addr);
595
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;
599
600 iowrite32(dataval, mdio_addr);
601 mdio_delay(mdio_addr);
602 iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
603 mdio_delay(mdio_addr);
604 }
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);
612 }
613 return (retval>>1) & 0xffff;
614 }
615
616 static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
617 {
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;
621 int i;
622
623 if (location == 4 && phy_id == np->phys[0])
624 np->mii_if.advertising = value;
625
626 if (mii_preamble_required)
627 mdio_sync(mdio_addr);
628
629 /* Shift the command bits out. */
630 for (i = 31; i >= 0; i--) {
631 int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
632
633 iowrite32(dataval, mdio_addr);
634 mdio_delay(mdio_addr);
635 iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
636 mdio_delay(mdio_addr);
637 }
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);
644 }
645 return;
646 }
647
648
649 static int netdev_open(struct net_device *dev)
650 {
651 struct netdev_private *np = netdev_priv(dev);
652 void __iomem *ioaddr = np->base_addr;
653 int i;
654
655 iowrite32(0x00000001, ioaddr + PCIBusCfg); /* Reset */
656
657 netif_device_detach(dev);
658 i = request_irq(dev->irq, &intr_handler, IRQF_SHARED, dev->name, dev);
659 if (i)
660 goto out_err;
661
662 if (debug > 1)
663 printk(KERN_DEBUG "%s: w89c840_open() irq %d.\n",
664 dev->name, dev->irq);
665
666 if((i=alloc_ringdesc(dev)))
667 goto out_err;
668
669 spin_lock_irq(&np->lock);
670 netif_device_attach(dev);
671 init_registers(dev);
672 spin_unlock_irq(&np->lock);
673
674 netif_start_queue(dev);
675 if (debug > 2)
676 printk(KERN_DEBUG "%s: Done netdev_open().\n", dev->name);
677
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);
684 return 0;
685 out_err:
686 netif_device_attach(dev);
687 return i;
688 }
689
690 #define MII_DAVICOM_DM9101 0x0181b800
691
692 static int update_link(struct net_device *dev)
693 {
694 struct netdev_private *np = netdev_priv(dev);
695 int duplex, fasteth, result, mii_reg;
696
697 /* BSMR */
698 mii_reg = mdio_read(dev, np->phys[0], MII_BMSR);
699
700 if (mii_reg == 0xffff)
701 return np->csr6;
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)) {
706 if (debug)
707 printk(KERN_INFO "%s: MII #%d reports no link. Disabling watchdog.\n",
708 dev->name, np->phys[0]);
709 netif_carrier_off(dev);
710 }
711 return np->csr6;
712 }
713 if (!netif_carrier_ok(dev)) {
714 if (debug)
715 printk(KERN_INFO "%s: MII #%d link is back. Enabling watchdog.\n",
716 dev->name, np->phys[0]);
717 netif_carrier_on(dev);
718 }
719
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..
728 */
729 mii_reg = mdio_read(dev, np->phys[0], MII_BMCR);
730 duplex = mii_reg & BMCR_FULLDPLX;
731 fasteth = mii_reg & BMCR_SPEED100;
732 } else {
733 int negotiated;
734 mii_reg = mdio_read(dev, np->phys[0], MII_LPA);
735 negotiated = mii_reg & np->mii_if.advertising;
736
737 duplex = (negotiated & LPA_100FULL) || ((negotiated & 0x02C0) == LPA_10FULL);
738 fasteth = negotiated & 0x380;
739 }
740 duplex |= np->mii_if.force_media;
741 /* remove fastether and fullduplex */
742 result = np->csr6 & ~0x20000200;
743 if (duplex)
744 result |= 0x200;
745 if (fasteth)
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]);
751 return result;
752 }
753
754 #define RXTX_TIMEOUT 2000
755 static inline void update_csr6(struct net_device *dev, int new)
756 {
757 struct netdev_private *np = netdev_priv(dev);
758 void __iomem *ioaddr = np->base_addr;
759 int limit = RXTX_TIMEOUT;
760
761 if (!netif_device_present(dev))
762 new = 0;
763 if (new==np->csr6)
764 return;
765 /* stop both Tx and Rx processes */
766 iowrite32(np->csr6 & ~0x2002, ioaddr + NetworkConfig);
767 /* wait until they have really stopped */
768 for (;;) {
769 int csr5 = ioread32(ioaddr + IntrStatus);
770 int t;
771
772 t = (csr5 >> 17) & 0x07;
773 if (t==0||t==1) {
774 /* rx stopped */
775 t = (csr5 >> 20) & 0x07;
776 if (t==0||t==1)
777 break;
778 }
779
780 limit--;
781 if(!limit) {
782 printk(KERN_INFO "%s: couldn't stop rxtx, IntrStatus %xh.\n",
783 dev->name, csr5);
784 break;
785 }
786 udelay(1);
787 }
788 np->csr6 = new;
789 /* and restart them with the new configuration */
790 iowrite32(np->csr6, ioaddr + NetworkConfig);
791 if (new & 0x200)
792 np->mii_if.full_duplex = 1;
793 }
794
795 static void netdev_timer(unsigned long data)
796 {
797 struct net_device *dev = (struct net_device *)data;
798 struct netdev_private *np = netdev_priv(dev);
799 void __iomem *ioaddr = np->base_addr;
800
801 if (debug > 2)
802 printk(KERN_DEBUG "%s: Media selection timer tick, status %8.8x "
803 "config %8.8x.\n",
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);
811 }
812
813 static void init_rxtx_rings(struct net_device *dev)
814 {
815 struct netdev_private *np = netdev_priv(dev);
816 int i;
817
818 np->rx_head_desc = &np->rx_ring[0];
819 np->tx_ring = (struct w840_tx_desc*)&np->rx_ring[RX_RING_SIZE];
820
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;
826 }
827 /* Mark the last entry as wrapping the ring. */
828 np->rx_ring[i-1].length |= DescEndRing;
829
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;
834 if (skb == NULL)
835 break;
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);
839
840 np->rx_ring[i].buffer1 = np->rx_addr[i];
841 np->rx_ring[i].status = DescOwn;
842 }
843
844 np->cur_rx = 0;
845 np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
846
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;
851 }
852 np->tx_full = 0;
853 np->tx_q_bytes = np->dirty_tx = np->cur_tx = 0;
854
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);
858
859 }
860
861 static void free_rxtx_rings(struct netdev_private* np)
862 {
863 int i;
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,
869 np->rx_addr[i],
870 np->rx_skbuff[i]->len,
871 PCI_DMA_FROMDEVICE);
872 dev_kfree_skb(np->rx_skbuff[i]);
873 }
874 np->rx_skbuff[i] = NULL;
875 }
876 for (i = 0; i < TX_RING_SIZE; i++) {
877 if (np->tx_skbuff[i]) {
878 pci_unmap_single(np->pci_dev,
879 np->tx_addr[i],
880 np->tx_skbuff[i]->len,
881 PCI_DMA_TODEVICE);
882 dev_kfree_skb(np->tx_skbuff[i]);
883 }
884 np->tx_skbuff[i] = NULL;
885 }
886 }
887
888 static void init_registers(struct net_device *dev)
889 {
890 struct netdev_private *np = netdev_priv(dev);
891 void __iomem *ioaddr = np->base_addr;
892 int i;
893
894 for (i = 0; i < 6; i++)
895 iowrite8(dev->dev_addr[i], ioaddr + StationAddr + i);
896
897 /* Initialize other registers. */
898 #ifdef __BIG_ENDIAN
899 i = (1<<20); /* Big-endian descriptors */
900 #else
901 i = 0;
902 #endif
903 i |= (0x04<<2); /* skip length 4 u32 */
904 i |= 0x02; /* give Rx priority */
905
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 */
914
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) {
918 i |= 0x4800;
919 printk(KERN_INFO "%s: This is a 386/486 PCI system, setting cache "
920 "alignment to 8 longwords.\n", dev->name);
921 } else {
922 i |= 0xE000;
923 }
924 #elif defined(__powerpc__) || defined(__i386__) || defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
925 i |= 0xE000;
926 #elif defined(__sparc__)
927 i |= 0x4800;
928 #else
929 #warning Processor architecture undefined
930 i |= 0x4800;
931 #endif
932 iowrite32(i, ioaddr + PCIBusCfg);
933
934 np->csr6 = 0;
935 /* 128 byte Tx threshold;
936 Transmit on; Receive on; */
937 update_csr6(dev, 0x00022002 | update_link(dev) | __set_rx_mode(dev));
938
939 /* Clear and Enable interrupts by setting the interrupt mask. */
940 iowrite32(0x1A0F5, ioaddr + IntrStatus);
941 iowrite32(0x1A0F5, ioaddr + IntrEnable);
942
943 iowrite32(0, ioaddr + RxStartDemand);
944 }
945
946 static void tx_timeout(struct net_device *dev)
947 {
948 struct netdev_private *np = netdev_priv(dev);
949 void __iomem *ioaddr = np->base_addr;
950
951 printk(KERN_WARNING "%s: Transmit timed out, status %8.8x,"
952 " resetting...\n", dev->name, ioread32(ioaddr + IntrStatus));
953
954 {
955 int i;
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);
962 printk("\n");
963 }
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));
967
968 disable_irq(dev->irq);
969 spin_lock_irq(&np->lock);
970 /*
971 * Under high load dirty_tx and the internal tx descriptor pointer
972 * come out of sync, thus perform a software reset and reinitialize
973 * everything.
974 */
975
976 iowrite32(1, np->base_addr+PCIBusCfg);
977 udelay(1);
978
979 free_rxtx_rings(np);
980 init_rxtx_rings(dev);
981 init_registers(dev);
982 spin_unlock_irq(&np->lock);
983 enable_irq(dev->irq);
984
985 netif_wake_queue(dev);
986 dev->trans_start = jiffies;
987 np->stats.tx_errors++;
988 return;
989 }
990
991 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
992 static int alloc_ringdesc(struct net_device *dev)
993 {
994 struct netdev_private *np = netdev_priv(dev);
995
996 np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
997
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);
1002 if(!np->rx_ring)
1003 return -ENOMEM;
1004 init_rxtx_rings(dev);
1005 return 0;
1006 }
1007
1008 static void free_ringdesc(struct netdev_private *np)
1009 {
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);
1014
1015 }
1016
1017 static int start_tx(struct sk_buff *skb, struct net_device *dev)
1018 {
1019 struct netdev_private *np = netdev_priv(dev);
1020 unsigned entry;
1021
1022 /* Caution: the write order is important here, set the field
1023 with the "ownership" bits last. */
1024
1025 /* Calculate the next Tx descriptor entry. */
1026 entry = np->cur_tx % TX_RING_SIZE;
1027
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;
1031
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;
1035 } else {
1036 int len = skb->len - TX_BUFLIMIT;
1037
1038 np->tx_ring[entry].buffer2 = np->tx_addr[entry]+TX_BUFLIMIT;
1039 np->tx_ring[entry].length = DescWholePkt | (len << 11) | TX_BUFLIMIT;
1040 }
1041 if(entry == TX_RING_SIZE-1)
1042 np->tx_ring[entry].length |= DescEndRing;
1043
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.
1053 */
1054 spin_lock_irq(&np->lock);
1055 np->cur_tx++;
1056
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);
1067 wmb();
1068 np->tx_full = 1;
1069 }
1070 spin_unlock_irq(&np->lock);
1071
1072 dev->trans_start = jiffies;
1073
1074 if (debug > 4) {
1075 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1076 dev->name, np->cur_tx, entry);
1077 }
1078 return 0;
1079 }
1080
1081 static void netdev_tx_done(struct net_device *dev)
1082 {
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;
1087
1088 if (tx_status < 0)
1089 break;
1090 if (tx_status & 0x8000) { /* There was an error, log it. */
1091 #ifndef final_version
1092 if (debug > 1)
1093 printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
1094 dev->name, tx_status);
1095 #endif
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++;
1103 } else {
1104 #ifndef final_version
1105 if (debug > 3)
1106 printk(KERN_DEBUG "%s: Transmit slot %d ok, Tx status %8.8x.\n",
1107 dev->name, entry, tx_status);
1108 #endif
1109 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1110 np->stats.collisions += (tx_status >> 3) & 15;
1111 np->stats.tx_packets++;
1112 }
1113 /* Free the original skb. */
1114 pci_unmap_single(np->pci_dev,np->tx_addr[entry],
1115 np->tx_skbuff[entry]->len,
1116 PCI_DMA_TODEVICE);
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;
1120 }
1121 if (np->tx_full &&
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. */
1125 np->tx_full = 0;
1126 wmb();
1127 netif_wake_queue(dev);
1128 }
1129 }
1130
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)
1134 {
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;
1139 int handled = 0;
1140
1141 if (!netif_device_present(dev))
1142 return IRQ_NONE;
1143 do {
1144 u32 intr_status = ioread32(ioaddr + IntrStatus);
1145
1146 /* Acknowledge all of the current interrupt sources ASAP. */
1147 iowrite32(intr_status & 0x001ffff, ioaddr + IntrStatus);
1148
1149 if (debug > 4)
1150 printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
1151 dev->name, intr_status);
1152
1153 if ((intr_status & (NormalIntr|AbnormalIntr)) == 0)
1154 break;
1155
1156 handled = 1;
1157
1158 if (intr_status & (IntrRxDone | RxNoBuf))
1159 netdev_rx(dev);
1160 if (intr_status & RxNoBuf)
1161 iowrite32(0, ioaddr + RxStartDemand);
1162
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);
1168 }
1169
1170 /* Abnormal error summary/uncommon events handlers. */
1171 if (intr_status & (AbnormalIntr | TxFIFOUnderflow | IntrPCIErr |
1172 TimerInt | IntrTxStopped))
1173 netdev_error(dev, intr_status);
1174
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
1179 10*82usec ticks. */
1180 spin_lock(&np->lock);
1181 if (netif_device_present(dev)) {
1182 iowrite32(AbnormalIntr | TimerInt, ioaddr + IntrEnable);
1183 iowrite32(10, ioaddr + GPTimer);
1184 }
1185 spin_unlock(&np->lock);
1186 break;
1187 }
1188 } while (1);
1189
1190 if (debug > 3)
1191 printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1192 dev->name, ioread32(ioaddr + IntrStatus));
1193 return IRQ_RETVAL(handled);
1194 }
1195
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)
1199 {
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;
1203
1204 if (debug > 4) {
1205 printk(KERN_DEBUG " In netdev_rx(), entry %d status %4.4x.\n",
1206 entry, np->rx_ring[entry].status);
1207 }
1208
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;
1213
1214 if (debug > 4)
1215 printk(KERN_DEBUG " netdev_rx() status was %8.8x.\n",
1216 status);
1217 if (status < 0)
1218 break;
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++;
1227 }
1228 } else if (status & 0x8000) {
1229 /* There was a fatal error. */
1230 if (debug > 2)
1231 printk(KERN_DEBUG "%s: Receive error, Rx status %8.8x.\n",
1232 dev->name, status);
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++;
1237 }
1238 } else {
1239 struct sk_buff *skb;
1240 /* Omit the four octet CRC from the length. */
1241 int pkt_len = ((status >> 16) & 0x7ff) - 4;
1242
1243 #ifndef final_version
1244 if (debug > 4)
1245 printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d"
1246 " status %x.\n", pkt_len, status);
1247 #endif
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) {
1252 skb->dev = dev;
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);
1262 } else {
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;
1268 }
1269 #ifndef final_version /* Remove after testing. */
1270 /* You will want this info for the initial debug. */
1271 if (debug > 5)
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 "
1274 "%d.%d.%d.%d.\n",
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],
1280 skb->data[17]);
1281 #endif
1282 skb->protocol = eth_type_trans(skb, dev);
1283 netif_rx(skb);
1284 dev->last_rx = jiffies;
1285 np->stats.rx_packets++;
1286 np->stats.rx_bytes += pkt_len;
1287 }
1288 entry = (++np->cur_rx) % RX_RING_SIZE;
1289 np->rx_head_desc = &np->rx_ring[entry];
1290 }
1291
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;
1299 if (skb == NULL)
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,
1303 skb->data,
1304 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1305 np->rx_ring[entry].buffer1 = np->rx_addr[entry];
1306 }
1307 wmb();
1308 np->rx_ring[entry].status = DescOwn;
1309 }
1310
1311 return 0;
1312 }
1313
1314 static void netdev_error(struct net_device *dev, int intr_status)
1315 {
1316 struct netdev_private *np = netdev_priv(dev);
1317 void __iomem *ioaddr = np->base_addr;
1318
1319 if (debug > 2)
1320 printk(KERN_DEBUG "%s: Abnormal event, %8.8x.\n",
1321 dev->name, intr_status);
1322 if (intr_status == 0xffffffff)
1323 return;
1324 spin_lock(&np->lock);
1325 if (intr_status & TxFIFOUnderflow) {
1326 int new;
1327 /* Bump up the Tx threshold */
1328 #if 0
1329 /* This causes lots of dropped packets,
1330 * and under high load even tx_timeouts
1331 */
1332 new = np->csr6 + 0x4000;
1333 #else
1334 new = (np->csr6 >> 14)&0x7f;
1335 if (new < 64)
1336 new *= 2;
1337 else
1338 new = 127; /* load full packet before starting */
1339 new = (np->csr6 & ~(0x7F << 14)) | (new<<14);
1340 #endif
1341 printk(KERN_DEBUG "%s: Tx underflow, new csr6 %8.8x.\n",
1342 dev->name, new);
1343 update_csr6(dev, new);
1344 }
1345 if (intr_status & IntrRxDied) { /* Missed a Rx frame. */
1346 np->stats.rx_errors++;
1347 }
1348 if (intr_status & TimerInt) {
1349 /* Re-enable other interrupts. */
1350 if (netif_device_present(dev))
1351 iowrite32(0x1A0F5, ioaddr + IntrEnable);
1352 }
1353 np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1354 iowrite32(0, ioaddr + RxStartDemand);
1355 spin_unlock(&np->lock);
1356 }
1357
1358 static struct net_device_stats *get_stats(struct net_device *dev)
1359 {
1360 struct netdev_private *np = netdev_priv(dev);
1361 void __iomem *ioaddr = np->base_addr;
1362
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);
1368
1369 return &np->stats;
1370 }
1371
1372
1373 static u32 __set_rx_mode(struct net_device *dev)
1374 {
1375 struct netdev_private *np = netdev_priv(dev);
1376 void __iomem *ioaddr = np->base_addr;
1377 u32 mc_filter[2]; /* Multicast hash filter */
1378 u32 rx_mode;
1379
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
1385 | AcceptMyPhys;
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;
1391 } else {
1392 struct dev_mc_list *mclist;
1393 int i;
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;
1398 filterbit &= 0x3f;
1399 mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
1400 }
1401 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1402 }
1403 iowrite32(mc_filter[0], ioaddr + MulticastFilter0);
1404 iowrite32(mc_filter[1], ioaddr + MulticastFilter1);
1405 return rx_mode;
1406 }
1407
1408 static void set_rx_mode(struct net_device *dev)
1409 {
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);
1415 }
1416
1417 static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1418 {
1419 struct netdev_private *np = netdev_priv(dev);
1420
1421 strcpy (info->driver, DRV_NAME);
1422 strcpy (info->version, DRV_VERSION);
1423 strcpy (info->bus_info, pci_name(np->pci_dev));
1424 }
1425
1426 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1427 {
1428 struct netdev_private *np = netdev_priv(dev);
1429 int rc;
1430
1431 spin_lock_irq(&np->lock);
1432 rc = mii_ethtool_gset(&np->mii_if, cmd);
1433 spin_unlock_irq(&np->lock);
1434
1435 return rc;
1436 }
1437
1438 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1439 {
1440 struct netdev_private *np = netdev_priv(dev);
1441 int rc;
1442
1443 spin_lock_irq(&np->lock);
1444 rc = mii_ethtool_sset(&np->mii_if, cmd);
1445 spin_unlock_irq(&np->lock);
1446
1447 return rc;
1448 }
1449
1450 static int netdev_nway_reset(struct net_device *dev)
1451 {
1452 struct netdev_private *np = netdev_priv(dev);
1453 return mii_nway_restart(&np->mii_if);
1454 }
1455
1456 static u32 netdev_get_link(struct net_device *dev)
1457 {
1458 struct netdev_private *np = netdev_priv(dev);
1459 return mii_link_ok(&np->mii_if);
1460 }
1461
1462 static u32 netdev_get_msglevel(struct net_device *dev)
1463 {
1464 return debug;
1465 }
1466
1467 static void netdev_set_msglevel(struct net_device *dev, u32 value)
1468 {
1469 debug = value;
1470 }
1471
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,
1482 };
1483
1484 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1485 {
1486 struct mii_ioctl_data *data = if_mii(rq);
1487 struct netdev_private *np = netdev_priv(dev);
1488
1489 switch(cmd) {
1490 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1491 data->phy_id = ((struct netdev_private *)netdev_priv(dev))->phys[0] & 0x1f;
1492 /* Fall Through */
1493
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);
1498 return 0;
1499
1500 case SIOCSMIIREG: /* Write MII PHY register. */
1501 if (!capable(CAP_NET_ADMIN))
1502 return -EPERM;
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);
1506 return 0;
1507 default:
1508 return -EOPNOTSUPP;
1509 }
1510 }
1511
1512 static int netdev_close(struct net_device *dev)
1513 {
1514 struct netdev_private *np = netdev_priv(dev);
1515 void __iomem *ioaddr = np->base_addr;
1516
1517 netif_stop_queue(dev);
1518
1519 if (debug > 1) {
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);
1525 }
1526
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);
1533
1534 free_irq(dev->irq, dev);
1535 wmb();
1536 netif_device_attach(dev);
1537
1538 if (ioread32(ioaddr + NetworkConfig) != 0xffffffff)
1539 np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1540
1541 #ifdef __i386__
1542 if (debug > 2) {
1543 int i;
1544
1545 printk(KERN_DEBUG" Tx ring at %8.8x:\n",
1546 (int)np->tx_ring);
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",
1552 (int)np->rx_ring);
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);
1557 }
1558 }
1559 #endif /* __i386__ debugging only */
1560
1561 del_timer_sync(&np->timer);
1562
1563 free_rxtx_rings(np);
1564 free_ringdesc(np);
1565
1566 return 0;
1567 }
1568
1569 static void __devexit w840_remove1 (struct pci_dev *pdev)
1570 {
1571 struct net_device *dev = pci_get_drvdata(pdev);
1572
1573 if (dev) {
1574 struct netdev_private *np = netdev_priv(dev);
1575 unregister_netdev(dev);
1576 pci_release_regions(pdev);
1577 pci_iounmap(pdev, np->base_addr);
1578 free_netdev(dev);
1579 }
1580
1581 pci_set_drvdata(pdev, NULL);
1582 }
1583
1584 #ifdef CONFIG_PM
1585
1586 /*
1587 * suspend/resume synchronization:
1588 * - open, close, do_ioctl:
1589 * rtnl_lock, & netif_device_detach after the rtnl_unlock.
1590 * - get_stats:
1591 * spin_lock_irq(np->lock), doesn't touch hw if not present
1592 * - hard_start_xmit:
1593 * synchronize_irq + netif_tx_disable;
1594 * - tx_timeout:
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.
1601 *
1602 * Disabling hw requires clearing csr6 & IntrEnable.
1603 * update_csr6 & all function that write IntrEnable check netif_device_present
1604 * before settings any bits.
1605 *
1606 * Detach must occur under spin_unlock_irq(), interrupts from a detached
1607 * device would cause an irq storm.
1608 */
1609 static int w840_suspend (struct pci_dev *pdev, pm_message_t state)
1610 {
1611 struct net_device *dev = pci_get_drvdata (pdev);
1612 struct netdev_private *np = netdev_priv(dev);
1613 void __iomem *ioaddr = np->base_addr;
1614
1615 rtnl_lock();
1616 if (netif_running (dev)) {
1617 del_timer_sync(&np->timer);
1618
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);
1624
1625 synchronize_irq(dev->irq);
1626 netif_tx_disable(dev);
1627
1628 np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1629
1630 /* no more hardware accesses behind this line. */
1631
1632 BUG_ON(np->csr6);
1633 if (ioread32(ioaddr + IntrEnable)) BUG();
1634
1635 /* pci_power_off(pdev, -1); */
1636
1637 free_rxtx_rings(np);
1638 } else {
1639 netif_device_detach(dev);
1640 }
1641 rtnl_unlock();
1642 return 0;
1643 }
1644
1645 static int w840_resume (struct pci_dev *pdev)
1646 {
1647 struct net_device *dev = pci_get_drvdata (pdev);
1648 struct netdev_private *np = netdev_priv(dev);
1649
1650 rtnl_lock();
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); */
1656
1657 spin_lock_irq(&np->lock);
1658 iowrite32(1, np->base_addr+PCIBusCfg);
1659 ioread32(np->base_addr+PCIBusCfg);
1660 udelay(1);
1661 netif_device_attach(dev);
1662 init_rxtx_rings(dev);
1663 init_registers(dev);
1664 spin_unlock_irq(&np->lock);
1665
1666 netif_wake_queue(dev);
1667
1668 mod_timer(&np->timer, jiffies + 1*HZ);
1669 } else {
1670 netif_device_attach(dev);
1671 }
1672 out:
1673 rtnl_unlock();
1674 return 0;
1675 }
1676 #endif
1677
1678 static struct pci_driver w840_driver = {
1679 .name = DRV_NAME,
1680 .id_table = w840_pci_tbl,
1681 .probe = w840_probe1,
1682 .remove = __devexit_p(w840_remove1),
1683 #ifdef CONFIG_PM
1684 .suspend = w840_suspend,
1685 .resume = w840_resume,
1686 #endif
1687 };
1688
1689 static int __init w840_init(void)
1690 {
1691 printk(version);
1692 return pci_module_init(&w840_driver);
1693 }
1694
1695 static void __exit w840_exit(void)
1696 {
1697 pci_unregister_driver(&w840_driver);
1698 }
1699
1700 module_init(w840_init);
1701 module_exit(w840_exit);