blob: 47e8cce10d1e3f2900e2de52f6cb2bbf90c400fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* sundance.c: A Linux device driver for the Sundance ST201 "Alta". */
2/*
3 Written 1999-2000 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/sundance.html
Jeff Garzik03a8c662006-06-27 07:57:22 -040019 [link no longer provides useful info -jgarzik]
Philippe De Muytere714d992006-08-03 18:42:15 +020020 Archives of the mailing list are still available at
21 http://www.beowulf.org/pipermail/netdrivers/
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023*/
24
25#define DRV_NAME "sundance"
Andy Gospodarekd5b20692006-09-11 17:39:18 -040026#define DRV_VERSION "1.2"
27#define DRV_RELDATE "11-Sep-2006"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29
30/* The user-configurable values.
31 These may be modified when a driver module is loaded.*/
32static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
33/* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
34 Typical is a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050035static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Set the copy breakpoint for the copy-only-tiny-frames scheme.
38 Setting to > 1518 effectively disables this feature.
39 This chip can receive into offset buffers, so the Alpha does not
40 need a copy-align. */
41static int rx_copybreak;
42static int flowctrl=1;
43
44/* media[] specifies the media type the NIC operates at.
45 autosense Autosensing active media.
46 10mbps_hd 10Mbps half duplex.
47 10mbps_fd 10Mbps full duplex.
48 100mbps_hd 100Mbps half duplex.
49 100mbps_fd 100Mbps full duplex.
50 0 Autosensing active media.
51 1 10Mbps half duplex.
52 2 10Mbps full duplex.
53 3 100Mbps half duplex.
54 4 100Mbps full duplex.
55*/
56#define MAX_UNITS 8
57static char *media[MAX_UNITS];
58
59
60/* Operational parameters that are set at compile time. */
61
62/* Keep the ring sizes a power of two for compile efficiency.
63 The compiler will convert <unsigned>'%'<2^N> into a bit mask.
64 Making the Tx ring too large decreases the effectiveness of channel
65 bonding and packet priority, and more than 128 requires modifying the
66 Tx error recovery.
67 Large receive rings merely waste memory. */
68#define TX_RING_SIZE 32
69#define TX_QUEUE_LEN (TX_RING_SIZE - 1) /* Limit ring entries actually used. */
70#define RX_RING_SIZE 64
71#define RX_BUDGET 32
72#define TX_TOTAL_SIZE TX_RING_SIZE*sizeof(struct netdev_desc)
73#define RX_TOTAL_SIZE RX_RING_SIZE*sizeof(struct netdev_desc)
74
75/* Operational parameters that usually are not changed. */
76/* Time in jiffies before concluding the transmitter is hung. */
77#define TX_TIMEOUT (4*HZ)
78#define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
79
80/* Include files, designed to support most kernel versions 2.0.0 and later. */
81#include <linux/module.h>
82#include <linux/kernel.h>
83#include <linux/string.h>
84#include <linux/timer.h>
85#include <linux/errno.h>
86#include <linux/ioport.h>
87#include <linux/slab.h>
88#include <linux/interrupt.h>
89#include <linux/pci.h>
90#include <linux/netdevice.h>
91#include <linux/etherdevice.h>
92#include <linux/skbuff.h>
93#include <linux/init.h>
94#include <linux/bitops.h>
95#include <asm/uaccess.h>
96#include <asm/processor.h> /* Processor type for cache alignment. */
97#include <asm/io.h>
98#include <linux/delay.h>
99#include <linux/spinlock.h>
100#ifndef _COMPAT_WITH_OLD_KERNEL
101#include <linux/crc32.h>
102#include <linux/ethtool.h>
103#include <linux/mii.h>
104#else
105#include "crc32.h"
106#include "ethtool.h"
107#include "mii.h"
108#include "compat.h"
109#endif
110
111/* These identify the driver base version and may not be removed. */
Stephen Hemminger3af0fe32009-02-26 10:19:33 +0000112static const char version[] __devinitconst =
113 KERN_INFO DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE
114 " Written by Donald Becker\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
117MODULE_DESCRIPTION("Sundance Alta Ethernet driver");
118MODULE_LICENSE("GPL");
119
120module_param(debug, int, 0);
121module_param(rx_copybreak, int, 0);
122module_param_array(media, charp, NULL, 0);
123module_param(flowctrl, int, 0);
124MODULE_PARM_DESC(debug, "Sundance Alta debug level (0-5)");
125MODULE_PARM_DESC(rx_copybreak, "Sundance Alta copy breakpoint for copy-only-tiny-frames");
126MODULE_PARM_DESC(flowctrl, "Sundance Alta flow control [0|1]");
127
128/*
129 Theory of Operation
130
131I. Board Compatibility
132
133This driver is designed for the Sundance Technologies "Alta" ST201 chip.
134
135II. Board-specific settings
136
137III. Driver operation
138
139IIIa. Ring buffers
140
141This driver uses two statically allocated fixed-size descriptor lists
142formed into rings by a branch from the final descriptor to the beginning of
143the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
144Some chips explicitly use only 2^N sized rings, while others use a
145'next descriptor' pointer that the driver forms into rings.
146
147IIIb/c. Transmit/Receive Structure
148
149This driver uses a zero-copy receive and transmit scheme.
150The driver allocates full frame size skbuffs for the Rx ring buffers at
151open() time and passes the skb->data field to the chip as receive data
152buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
153a fresh skbuff is allocated and the frame is copied to the new skbuff.
154When the incoming frame is larger, the skbuff is passed directly up the
155protocol stack. Buffers consumed this way are replaced by newly allocated
156skbuffs in a later phase of receives.
157
158The RX_COPYBREAK value is chosen to trade-off the memory wasted by
159using a full-sized skbuff for small frames vs. the copying costs of larger
160frames. New boards are typically used in generously configured machines
161and the underfilled buffers have negligible impact compared to the benefit of
162a single allocation size, so the default value of zero results in never
163copying packets. When copying is done, the cost is usually mitigated by using
164a combined copy/checksum routine. Copying also preloads the cache, which is
165most useful with small frames.
166
167A subtle aspect of the operation is that the IP header at offset 14 in an
168ethernet frame isn't longword aligned for further processing.
169Unaligned buffers are permitted by the Sundance hardware, so
170frames are received into the skbuff at an offset of "+2", 16-byte aligning
171the IP header.
172
173IIId. Synchronization
174
175The driver runs as two independent, single-threaded flows of control. One
176is the send-packet routine, which enforces single-threaded use by the
177dev->tbusy flag. The other thread is the interrupt handler, which is single
178threaded by the hardware and interrupt handling software.
179
180The send packet thread has partial control over the Tx ring and 'dev->tbusy'
181flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
182queue slot is empty, it clears the tbusy flag when finished otherwise it sets
183the 'lp->tx_full' flag.
184
185The interrupt handler has exclusive control over the Rx ring and records stats
186from the Tx ring. After reaping the stats, it marks the Tx queue entry as
187empty by incrementing the dirty_tx mark. Iff the 'lp->tx_full' flag is set, it
188clears both the tx_full and tbusy flags.
189
190IV. Notes
191
192IVb. References
193
194The Sundance ST201 datasheet, preliminary version.
Philippe De Muyterb71b95e2005-10-28 12:23:47 +0200195The Kendin KS8723 datasheet, preliminary version.
196The ICplus IP100 datasheet, preliminary version.
197http://www.scyld.com/expert/100mbps.html
198http://www.scyld.com/expert/NWay.html
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200IVc. Errata
201
202*/
203
204/* Work-around for Kendin chip bugs. */
205#ifndef CONFIG_SUNDANCE_MMIO
206#define USE_IO_OPS 1
207#endif
208
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000209static DEFINE_PCI_DEVICE_TABLE(sundance_pci_tbl) = {
Jeff Garzik46009c82006-06-27 09:12:38 -0400210 { 0x1186, 0x1002, 0x1186, 0x1002, 0, 0, 0 },
211 { 0x1186, 0x1002, 0x1186, 0x1003, 0, 0, 1 },
212 { 0x1186, 0x1002, 0x1186, 0x1012, 0, 0, 2 },
213 { 0x1186, 0x1002, 0x1186, 0x1040, 0, 0, 3 },
214 { 0x1186, 0x1002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
215 { 0x13F0, 0x0201, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
216 { 0x13F0, 0x0200, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6 },
217 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219MODULE_DEVICE_TABLE(pci, sundance_pci_tbl);
220
221enum {
222 netdev_io_size = 128
223};
224
225struct pci_id_info {
226 const char *name;
227};
Jeff Garzik46009c82006-06-27 09:12:38 -0400228static const struct pci_id_info pci_id_tbl[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 {"D-Link DFE-550TX FAST Ethernet Adapter"},
230 {"D-Link DFE-550FX 100Mbps Fiber-optics Adapter"},
231 {"D-Link DFE-580TX 4 port Server Adapter"},
232 {"D-Link DFE-530TXS FAST Ethernet Adapter"},
233 {"D-Link DL10050-based FAST Ethernet Adapter"},
234 {"Sundance Technology Alta"},
Pedro Alejandro López-Valencia1668b192006-06-15 22:46:44 +0200235 {"IC Plus Corporation IP100A FAST Ethernet Adapter"},
Jeff Garzik46009c82006-06-27 09:12:38 -0400236 { } /* terminate list. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
239/* This driver was written to use PCI memory space, however x86-oriented
240 hardware often uses I/O space accesses. */
241
242/* Offsets to the device registers.
243 Unlike software-only systems, device drivers interact with complex hardware.
244 It's not useful to define symbolic names for every register bit in the
245 device. The name can only partially document the semantics and make
246 the driver longer and more difficult to read.
247 In general, only the important configuration values or bits changed
248 multiple times should be defined symbolically.
249*/
250enum alta_offsets {
251 DMACtrl = 0x00,
252 TxListPtr = 0x04,
253 TxDMABurstThresh = 0x08,
254 TxDMAUrgentThresh = 0x09,
255 TxDMAPollPeriod = 0x0a,
256 RxDMAStatus = 0x0c,
257 RxListPtr = 0x10,
258 DebugCtrl0 = 0x1a,
259 DebugCtrl1 = 0x1c,
260 RxDMABurstThresh = 0x14,
261 RxDMAUrgentThresh = 0x15,
262 RxDMAPollPeriod = 0x16,
263 LEDCtrl = 0x1a,
264 ASICCtrl = 0x30,
265 EEData = 0x34,
266 EECtrl = 0x36,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 FlashAddr = 0x40,
268 FlashData = 0x44,
269 TxStatus = 0x46,
270 TxFrameId = 0x47,
271 DownCounter = 0x18,
272 IntrClear = 0x4a,
273 IntrEnable = 0x4c,
274 IntrStatus = 0x4e,
275 MACCtrl0 = 0x50,
276 MACCtrl1 = 0x52,
277 StationAddr = 0x54,
278 MaxFrameSize = 0x5A,
279 RxMode = 0x5c,
280 MIICtrl = 0x5e,
281 MulticastFilter0 = 0x60,
282 MulticastFilter1 = 0x64,
283 RxOctetsLow = 0x68,
284 RxOctetsHigh = 0x6a,
285 TxOctetsLow = 0x6c,
286 TxOctetsHigh = 0x6e,
287 TxFramesOK = 0x70,
288 RxFramesOK = 0x72,
289 StatsCarrierError = 0x74,
290 StatsLateColl = 0x75,
291 StatsMultiColl = 0x76,
292 StatsOneColl = 0x77,
293 StatsTxDefer = 0x78,
294 RxMissed = 0x79,
295 StatsTxXSDefer = 0x7a,
296 StatsTxAbort = 0x7b,
297 StatsBcastTx = 0x7c,
298 StatsBcastRx = 0x7d,
299 StatsMcastTx = 0x7e,
300 StatsMcastRx = 0x7f,
301 /* Aliased and bogus values! */
302 RxStatus = 0x0c,
303};
304enum ASICCtrl_HiWord_bit {
305 GlobalReset = 0x0001,
306 RxReset = 0x0002,
307 TxReset = 0x0004,
308 DMAReset = 0x0008,
309 FIFOReset = 0x0010,
310 NetworkReset = 0x0020,
311 HostReset = 0x0040,
312 ResetBusy = 0x0400,
313};
314
315/* Bits in the interrupt status/mask registers. */
316enum intr_status_bits {
317 IntrSummary=0x0001, IntrPCIErr=0x0002, IntrMACCtrl=0x0008,
318 IntrTxDone=0x0004, IntrRxDone=0x0010, IntrRxStart=0x0020,
319 IntrDrvRqst=0x0040,
320 StatsMax=0x0080, LinkChange=0x0100,
321 IntrTxDMADone=0x0200, IntrRxDMADone=0x0400,
322};
323
324/* Bits in the RxMode register. */
325enum rx_mode_bits {
326 AcceptAllIPMulti=0x20, AcceptMultiHash=0x10, AcceptAll=0x08,
327 AcceptBroadcast=0x04, AcceptMulticast=0x02, AcceptMyPhys=0x01,
328};
329/* Bits in MACCtrl. */
330enum mac_ctrl0_bits {
331 EnbFullDuplex=0x20, EnbRcvLargeFrame=0x40,
332 EnbFlowCtrl=0x100, EnbPassRxCRC=0x200,
333};
334enum mac_ctrl1_bits {
335 StatsEnable=0x0020, StatsDisable=0x0040, StatsEnabled=0x0080,
336 TxEnable=0x0100, TxDisable=0x0200, TxEnabled=0x0400,
337 RxEnable=0x0800, RxDisable=0x1000, RxEnabled=0x2000,
338};
339
340/* The Rx and Tx buffer descriptors. */
341/* Note that using only 32 bit fields simplifies conversion to big-endian
342 architectures. */
343struct netdev_desc {
Al Viro14c9d9b2007-12-09 16:50:47 +0000344 __le32 next_desc;
345 __le32 status;
346 struct desc_frag { __le32 addr, length; } frag[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
349/* Bits in netdev_desc.status */
350enum desc_status_bits {
351 DescOwn=0x8000,
352 DescEndPacket=0x4000,
353 DescEndRing=0x2000,
354 LastFrag=0x80000000,
355 DescIntrOnTx=0x8000,
356 DescIntrOnDMADone=0x80000000,
357 DisableAlign = 0x00000001,
358};
359
360#define PRIV_ALIGN 15 /* Required alignment mask */
361/* Use __attribute__((aligned (L1_CACHE_BYTES))) to maintain alignment
362 within the structure. */
363#define MII_CNT 4
364struct netdev_private {
365 /* Descriptor rings first for alignment. */
366 struct netdev_desc *rx_ring;
367 struct netdev_desc *tx_ring;
368 struct sk_buff* rx_skbuff[RX_RING_SIZE];
369 struct sk_buff* tx_skbuff[TX_RING_SIZE];
370 dma_addr_t tx_ring_dma;
371 dma_addr_t rx_ring_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct timer_list timer; /* Media monitoring timer. */
373 /* Frequently used values: keep some adjacent for cache effect. */
374 spinlock_t lock;
375 spinlock_t rx_lock; /* Group with Tx control cache line. */
376 int msg_enable;
377 int chip_id;
378 unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
379 unsigned int rx_buf_sz; /* Based on MTU+slack. */
380 struct netdev_desc *last_tx; /* Last Tx descriptor used. */
381 unsigned int cur_tx, dirty_tx;
382 /* These values are keep track of the transceiver/media in use. */
383 unsigned int flowctrl:1;
384 unsigned int default_port:4; /* Last dev->if_port value. */
385 unsigned int an_enable:1;
386 unsigned int speed;
387 struct tasklet_struct rx_tasklet;
388 struct tasklet_struct tx_tasklet;
389 int budget;
390 int cur_task;
391 /* Multicast and receive mode. */
392 spinlock_t mcastlock; /* SMP lock multicast updates. */
393 u16 mcast_filter[4];
394 /* MII transceiver section. */
395 struct mii_if_info mii_if;
396 int mii_preamble_required;
397 unsigned char phys[MII_CNT]; /* MII device addresses, only first one used. */
398 struct pci_dev *pci_dev;
399 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
402/* The station address location in the EEPROM. */
403#define EEPROM_SA_OFFSET 0x10
404#define DEFAULT_INTR (IntrRxDMADone | IntrPCIErr | \
405 IntrDrvRqst | IntrTxDone | StatsMax | \
406 LinkChange)
407
408static int change_mtu(struct net_device *dev, int new_mtu);
409static int eeprom_read(void __iomem *ioaddr, int location);
410static int mdio_read(struct net_device *dev, int phy_id, int location);
411static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
Dan Nicholson50500152008-08-20 16:51:59 -0700412static int mdio_wait_link(struct net_device *dev, int wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static int netdev_open(struct net_device *dev);
414static void check_duplex(struct net_device *dev);
415static void netdev_timer(unsigned long data);
416static void tx_timeout(struct net_device *dev);
417static void init_ring(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000418static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static int reset_tx (struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100420static irqreturn_t intr_handler(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void rx_poll(unsigned long data);
422static void tx_poll(unsigned long data);
423static void refill_rx (struct net_device *dev);
424static void netdev_error(struct net_device *dev, int intr_status);
425static void netdev_error(struct net_device *dev, int intr_status);
426static void set_rx_mode(struct net_device *dev);
427static int __set_mac_addr(struct net_device *dev);
428static struct net_device_stats *get_stats(struct net_device *dev);
429static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
430static int netdev_close(struct net_device *dev);
Jeff Garzik7282d492006-09-13 14:30:00 -0400431static const struct ethtool_ops ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Philippe De Muyterb71b95e2005-10-28 12:23:47 +0200433static void sundance_reset(struct net_device *dev, unsigned long reset_cmd)
434{
435 struct netdev_private *np = netdev_priv(dev);
436 void __iomem *ioaddr = np->base + ASICCtrl;
437 int countdown;
438
439 /* ST201 documentation states ASICCtrl is a 32bit register */
440 iowrite32 (reset_cmd | ioread32 (ioaddr), ioaddr);
441 /* ST201 documentation states reset can take up to 1 ms */
442 countdown = 10 + 1;
443 while (ioread32 (ioaddr) & (ResetBusy << 16)) {
444 if (--countdown == 0) {
445 printk(KERN_WARNING "%s : reset not completed !!\n", dev->name);
446 break;
447 }
448 udelay(100);
449 }
450}
451
Stephen Hemminger633a2772009-01-07 17:58:43 -0800452static const struct net_device_ops netdev_ops = {
453 .ndo_open = netdev_open,
454 .ndo_stop = netdev_close,
455 .ndo_start_xmit = start_tx,
456 .ndo_get_stats = get_stats,
457 .ndo_set_multicast_list = set_rx_mode,
458 .ndo_do_ioctl = netdev_ioctl,
459 .ndo_tx_timeout = tx_timeout,
460 .ndo_change_mtu = change_mtu,
461 .ndo_set_mac_address = eth_mac_addr,
462 .ndo_validate_addr = eth_validate_addr,
463};
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static int __devinit sundance_probe1 (struct pci_dev *pdev,
466 const struct pci_device_id *ent)
467{
468 struct net_device *dev;
469 struct netdev_private *np;
470 static int card_idx;
471 int chip_idx = ent->driver_data;
472 int irq;
473 int i;
474 void __iomem *ioaddr;
475 u16 mii_ctl;
476 void *ring_space;
477 dma_addr_t ring_dma;
478#ifdef USE_IO_OPS
479 int bar = 0;
480#else
481 int bar = 1;
482#endif
Jeff Garzikac1d49f2007-09-29 01:10:14 -0400483 int phy, phy_end, phy_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485/* when built into the kernel, we only print version if device is found */
486#ifndef MODULE
487 static int printed_version;
488 if (!printed_version++)
489 printk(version);
490#endif
491
492 if (pci_enable_device(pdev))
493 return -EIO;
494 pci_set_master(pdev);
495
496 irq = pdev->irq;
497
498 dev = alloc_etherdev(sizeof(*np));
499 if (!dev)
500 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 SET_NETDEV_DEV(dev, &pdev->dev);
502
503 if (pci_request_regions(pdev, DRV_NAME))
504 goto err_out_netdev;
505
506 ioaddr = pci_iomap(pdev, bar, netdev_io_size);
507 if (!ioaddr)
508 goto err_out_res;
509
510 for (i = 0; i < 3; i++)
Al Viro14c9d9b2007-12-09 16:50:47 +0000511 ((__le16 *)dev->dev_addr)[i] =
512 cpu_to_le16(eeprom_read(ioaddr, i + EEPROM_SA_OFFSET));
John W. Linville30d60a82005-09-12 10:48:58 -0400513 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 dev->base_addr = (unsigned long)ioaddr;
516 dev->irq = irq;
517
518 np = netdev_priv(dev);
519 np->base = ioaddr;
520 np->pci_dev = pdev;
521 np->chip_id = chip_idx;
522 np->msg_enable = (1 << debug) - 1;
523 spin_lock_init(&np->lock);
524 tasklet_init(&np->rx_tasklet, rx_poll, (unsigned long)dev);
525 tasklet_init(&np->tx_tasklet, tx_poll, (unsigned long)dev);
526
527 ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
528 if (!ring_space)
529 goto err_out_cleardev;
530 np->tx_ring = (struct netdev_desc *)ring_space;
531 np->tx_ring_dma = ring_dma;
532
533 ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
534 if (!ring_space)
535 goto err_out_unmap_tx;
536 np->rx_ring = (struct netdev_desc *)ring_space;
537 np->rx_ring_dma = ring_dma;
538
539 np->mii_if.dev = dev;
540 np->mii_if.mdio_read = mdio_read;
541 np->mii_if.mdio_write = mdio_write;
542 np->mii_if.phy_id_mask = 0x1f;
543 np->mii_if.reg_num_mask = 0x1f;
544
545 /* The chip-specific entries in the device structure. */
Stephen Hemminger633a2772009-01-07 17:58:43 -0800546 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 SET_ETHTOOL_OPS(dev, &ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemminger633a2772009-01-07 17:58:43 -0800549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 pci_set_drvdata(pdev, dev);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 i = register_netdev(dev);
553 if (i)
554 goto err_out_unmap_rx;
555
Johannes Berge1749612008-10-27 15:59:26 -0700556 printk(KERN_INFO "%s: %s at %p, %pM, IRQ %d.\n",
Joe Perches0795af52007-10-03 17:59:30 -0700557 dev->name, pci_id_tbl[chip_idx].name, ioaddr,
Johannes Berge1749612008-10-27 15:59:26 -0700558 dev->dev_addr, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
John W. Linville67ec2f82005-10-18 21:31:01 -0400560 np->phys[0] = 1; /* Default setting */
561 np->mii_preamble_required++;
Jeff Garzikac1d49f2007-09-29 01:10:14 -0400562
Arnaldo Carvalho de Melo0d615ec2006-01-26 22:01:38 -0500563 /*
564 * It seems some phys doesn't deal well with address 0 being accessed
Jeff Garzikac1d49f2007-09-29 01:10:14 -0400565 * first
Arnaldo Carvalho de Melo0d615ec2006-01-26 22:01:38 -0500566 */
Jeff Garzikac1d49f2007-09-29 01:10:14 -0400567 if (sundance_pci_tbl[np->chip_id].device == 0x0200) {
568 phy = 0;
569 phy_end = 31;
570 } else {
571 phy = 1;
572 phy_end = 32; /* wraps to zero, due to 'phy & 0x1f' */
573 }
574 for (; phy <= phy_end && phy_idx < MII_CNT; phy++) {
John W. Linvilleb06c0932005-10-19 08:07:34 -0400575 int phyx = phy & 0x1f;
Arnaldo Carvalho de Melo0d615ec2006-01-26 22:01:38 -0500576 int mii_status = mdio_read(dev, phyx, MII_BMSR);
John W. Linville67ec2f82005-10-18 21:31:01 -0400577 if (mii_status != 0xffff && mii_status != 0x0000) {
John W. Linvilleb06c0932005-10-19 08:07:34 -0400578 np->phys[phy_idx++] = phyx;
579 np->mii_if.advertising = mdio_read(dev, phyx, MII_ADVERTISE);
John W. Linville67ec2f82005-10-18 21:31:01 -0400580 if ((mii_status & 0x0040) == 0)
581 np->mii_preamble_required++;
582 printk(KERN_INFO "%s: MII PHY found at address %d, status "
583 "0x%4.4x advertising %4.4x.\n",
John W. Linvilleb06c0932005-10-19 08:07:34 -0400584 dev->name, phyx, mii_status, np->mii_if.advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
John W. Linville67ec2f82005-10-18 21:31:01 -0400587 np->mii_preamble_required--;
588
589 if (phy_idx == 0) {
590 printk(KERN_INFO "%s: No MII transceiver found, aborting. ASIC status %x\n",
591 dev->name, ioread32(ioaddr + ASICCtrl));
592 goto err_out_unregister;
593 }
594
595 np->mii_if.phy_id = np->phys[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Parse override configuration */
598 np->an_enable = 1;
599 if (card_idx < MAX_UNITS) {
600 if (media[card_idx] != NULL) {
601 np->an_enable = 0;
602 if (strcmp (media[card_idx], "100mbps_fd") == 0 ||
603 strcmp (media[card_idx], "4") == 0) {
604 np->speed = 100;
605 np->mii_if.full_duplex = 1;
Joe Perches8e95a202009-12-03 07:58:21 +0000606 } else if (strcmp (media[card_idx], "100mbps_hd") == 0 ||
607 strcmp (media[card_idx], "3") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 np->speed = 100;
609 np->mii_if.full_duplex = 0;
610 } else if (strcmp (media[card_idx], "10mbps_fd") == 0 ||
611 strcmp (media[card_idx], "2") == 0) {
612 np->speed = 10;
613 np->mii_if.full_duplex = 1;
614 } else if (strcmp (media[card_idx], "10mbps_hd") == 0 ||
615 strcmp (media[card_idx], "1") == 0) {
616 np->speed = 10;
617 np->mii_if.full_duplex = 0;
618 } else {
619 np->an_enable = 1;
620 }
621 }
622 if (flowctrl == 1)
623 np->flowctrl = 1;
624 }
625
626 /* Fibre PHY? */
627 if (ioread32 (ioaddr + ASICCtrl) & 0x80) {
628 /* Default 100Mbps Full */
629 if (np->an_enable) {
630 np->speed = 100;
631 np->mii_if.full_duplex = 1;
632 np->an_enable = 0;
633 }
634 }
635 /* Reset PHY */
636 mdio_write (dev, np->phys[0], MII_BMCR, BMCR_RESET);
637 mdelay (300);
638 /* If flow control enabled, we need to advertise it.*/
639 if (np->flowctrl)
640 mdio_write (dev, np->phys[0], MII_ADVERTISE, np->mii_if.advertising | 0x0400);
641 mdio_write (dev, np->phys[0], MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART);
642 /* Force media type */
643 if (!np->an_enable) {
644 mii_ctl = 0;
645 mii_ctl |= (np->speed == 100) ? BMCR_SPEED100 : 0;
646 mii_ctl |= (np->mii_if.full_duplex) ? BMCR_FULLDPLX : 0;
647 mdio_write (dev, np->phys[0], MII_BMCR, mii_ctl);
648 printk (KERN_INFO "Override speed=%d, %s duplex\n",
649 np->speed, np->mii_if.full_duplex ? "Full" : "Half");
650
651 }
652
653 /* Perhaps move the reset here? */
654 /* Reset the chip to erase previous misconfiguration. */
655 if (netif_msg_hw(np))
656 printk("ASIC Control is %x.\n", ioread32(ioaddr + ASICCtrl));
Philippe De Muytere714d992006-08-03 18:42:15 +0200657 sundance_reset(dev, 0x00ff << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (netif_msg_hw(np))
659 printk("ASIC Control is now %x.\n", ioread32(ioaddr + ASICCtrl));
660
661 card_idx++;
662 return 0;
663
664err_out_unregister:
665 unregister_netdev(dev);
666err_out_unmap_rx:
667 pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma);
668err_out_unmap_tx:
669 pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma);
670err_out_cleardev:
671 pci_set_drvdata(pdev, NULL);
672 pci_iounmap(pdev, ioaddr);
673err_out_res:
674 pci_release_regions(pdev);
675err_out_netdev:
676 free_netdev (dev);
677 return -ENODEV;
678}
679
680static int change_mtu(struct net_device *dev, int new_mtu)
681{
682 if ((new_mtu < 68) || (new_mtu > 8191)) /* Set by RxDMAFrameLen */
683 return -EINVAL;
684 if (netif_running(dev))
685 return -EBUSY;
686 dev->mtu = new_mtu;
687 return 0;
688}
689
690#define eeprom_delay(ee_addr) ioread32(ee_addr)
691/* Read the EEPROM and MII Management Data I/O (MDIO) interfaces. */
692static int __devinit eeprom_read(void __iomem *ioaddr, int location)
693{
694 int boguscnt = 10000; /* Typical 1900 ticks. */
695 iowrite16(0x0200 | (location & 0xff), ioaddr + EECtrl);
696 do {
697 eeprom_delay(ioaddr + EECtrl);
698 if (! (ioread16(ioaddr + EECtrl) & 0x8000)) {
699 return ioread16(ioaddr + EEData);
700 }
701 } while (--boguscnt > 0);
702 return 0;
703}
704
705/* MII transceiver control section.
706 Read and write the MII registers using software-generated serial
707 MDIO protocol. See the MII specifications or DP83840A data sheet
708 for details.
709
710 The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
711 met by back-to-back 33Mhz PCI cycles. */
712#define mdio_delay() ioread8(mdio_addr)
713
714enum mii_reg_bits {
715 MDIO_ShiftClk=0x0001, MDIO_Data=0x0002, MDIO_EnbOutput=0x0004,
716};
717#define MDIO_EnbIn (0)
718#define MDIO_WRITE0 (MDIO_EnbOutput)
719#define MDIO_WRITE1 (MDIO_Data | MDIO_EnbOutput)
720
721/* Generate the preamble required for initial synchronization and
722 a few older transceivers. */
723static void mdio_sync(void __iomem *mdio_addr)
724{
725 int bits = 32;
726
727 /* Establish sync by sending at least 32 logic ones. */
728 while (--bits >= 0) {
729 iowrite8(MDIO_WRITE1, mdio_addr);
730 mdio_delay();
731 iowrite8(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
732 mdio_delay();
733 }
734}
735
736static int mdio_read(struct net_device *dev, int phy_id, int location)
737{
738 struct netdev_private *np = netdev_priv(dev);
739 void __iomem *mdio_addr = np->base + MIICtrl;
740 int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
741 int i, retval = 0;
742
743 if (np->mii_preamble_required)
744 mdio_sync(mdio_addr);
745
746 /* Shift the read command bits out. */
747 for (i = 15; i >= 0; i--) {
748 int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
749
750 iowrite8(dataval, mdio_addr);
751 mdio_delay();
752 iowrite8(dataval | MDIO_ShiftClk, mdio_addr);
753 mdio_delay();
754 }
755 /* Read the two transition, 16 data, and wire-idle bits. */
756 for (i = 19; i > 0; i--) {
757 iowrite8(MDIO_EnbIn, mdio_addr);
758 mdio_delay();
759 retval = (retval << 1) | ((ioread8(mdio_addr) & MDIO_Data) ? 1 : 0);
760 iowrite8(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
761 mdio_delay();
762 }
763 return (retval>>1) & 0xffff;
764}
765
766static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
767{
768 struct netdev_private *np = netdev_priv(dev);
769 void __iomem *mdio_addr = np->base + MIICtrl;
770 int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
771 int i;
772
773 if (np->mii_preamble_required)
774 mdio_sync(mdio_addr);
775
776 /* Shift the command bits out. */
777 for (i = 31; i >= 0; i--) {
778 int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
779
780 iowrite8(dataval, mdio_addr);
781 mdio_delay();
782 iowrite8(dataval | MDIO_ShiftClk, mdio_addr);
783 mdio_delay();
784 }
785 /* Clear out extra bits. */
786 for (i = 2; i > 0; i--) {
787 iowrite8(MDIO_EnbIn, mdio_addr);
788 mdio_delay();
789 iowrite8(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
790 mdio_delay();
791 }
792 return;
793}
794
Dan Nicholson50500152008-08-20 16:51:59 -0700795static int mdio_wait_link(struct net_device *dev, int wait)
796{
797 int bmsr;
798 int phy_id;
799 struct netdev_private *np;
800
801 np = netdev_priv(dev);
802 phy_id = np->phys[0];
803
804 do {
805 bmsr = mdio_read(dev, phy_id, MII_BMSR);
806 if (bmsr & 0x0004)
807 return 0;
808 mdelay(1);
809 } while (--wait > 0);
810 return -1;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static int netdev_open(struct net_device *dev)
814{
815 struct netdev_private *np = netdev_priv(dev);
816 void __iomem *ioaddr = np->base;
Jesse Huangacd70c22006-10-20 14:42:13 -0700817 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int i;
819
820 /* Do we need to reset the chip??? */
821
Joe Perchesa0607fd2009-11-18 23:29:17 -0800822 i = request_irq(dev->irq, intr_handler, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (i)
824 return i;
825
826 if (netif_msg_ifup(np))
827 printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
828 dev->name, dev->irq);
829 init_ring(dev);
830
831 iowrite32(np->rx_ring_dma, ioaddr + RxListPtr);
832 /* The Tx list pointer is written as packets are queued. */
833
834 /* Initialize other registers. */
835 __set_mac_addr(dev);
836#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
837 iowrite16(dev->mtu + 18, ioaddr + MaxFrameSize);
838#else
839 iowrite16(dev->mtu + 14, ioaddr + MaxFrameSize);
840#endif
841 if (dev->mtu > 2047)
842 iowrite32(ioread32(ioaddr + ASICCtrl) | 0x0C, ioaddr + ASICCtrl);
843
844 /* Configure the PCI bus bursts and FIFO thresholds. */
845
846 if (dev->if_port == 0)
847 dev->if_port = np->default_port;
848
849 spin_lock_init(&np->mcastlock);
850
851 set_rx_mode(dev);
852 iowrite16(0, ioaddr + IntrEnable);
853 iowrite16(0, ioaddr + DownCounter);
854 /* Set the chip to poll every N*320nsec. */
855 iowrite8(100, ioaddr + RxDMAPollPeriod);
856 iowrite8(127, ioaddr + TxDMAPollPeriod);
857 /* Fix DFE-580TX packet drop issue */
Auke Kok44c10132007-06-08 15:46:36 -0700858 if (np->pci_dev->revision >= 0x14)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 iowrite8(0x01, ioaddr + DebugCtrl1);
860 netif_start_queue(dev);
861
Jesse Huangacd70c22006-10-20 14:42:13 -0700862 spin_lock_irqsave(&np->lock, flags);
863 reset_tx(dev);
864 spin_unlock_irqrestore(&np->lock, flags);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 iowrite16 (StatsEnable | RxEnable | TxEnable, ioaddr + MACCtrl1);
867
868 if (netif_msg_ifup(np))
869 printk(KERN_DEBUG "%s: Done netdev_open(), status: Rx %x Tx %x "
870 "MAC Control %x, %4.4x %4.4x.\n",
871 dev->name, ioread32(ioaddr + RxStatus), ioread8(ioaddr + TxStatus),
872 ioread32(ioaddr + MACCtrl0),
873 ioread16(ioaddr + MACCtrl1), ioread16(ioaddr + MACCtrl0));
874
875 /* Set the timer to check for link beat. */
876 init_timer(&np->timer);
877 np->timer.expires = jiffies + 3*HZ;
878 np->timer.data = (unsigned long)dev;
879 np->timer.function = &netdev_timer; /* timer handler */
880 add_timer(&np->timer);
881
882 /* Enable interrupts by setting the interrupt mask. */
883 iowrite16(DEFAULT_INTR, ioaddr + IntrEnable);
884
885 return 0;
886}
887
888static void check_duplex(struct net_device *dev)
889{
890 struct netdev_private *np = netdev_priv(dev);
891 void __iomem *ioaddr = np->base;
892 int mii_lpa = mdio_read(dev, np->phys[0], MII_LPA);
893 int negotiated = mii_lpa & np->mii_if.advertising;
894 int duplex;
895
896 /* Force media */
897 if (!np->an_enable || mii_lpa == 0xffff) {
898 if (np->mii_if.full_duplex)
899 iowrite16 (ioread16 (ioaddr + MACCtrl0) | EnbFullDuplex,
900 ioaddr + MACCtrl0);
901 return;
902 }
903
904 /* Autonegotiation */
905 duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
906 if (np->mii_if.full_duplex != duplex) {
907 np->mii_if.full_duplex = duplex;
908 if (netif_msg_link(np))
909 printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d "
910 "negotiated capability %4.4x.\n", dev->name,
911 duplex ? "full" : "half", np->phys[0], negotiated);
Roel Kluin62660e22009-02-18 10:19:50 +0100912 iowrite16(ioread16(ioaddr + MACCtrl0) | (duplex ? 0x20 : 0), ioaddr + MACCtrl0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914}
915
916static void netdev_timer(unsigned long data)
917{
918 struct net_device *dev = (struct net_device *)data;
919 struct netdev_private *np = netdev_priv(dev);
920 void __iomem *ioaddr = np->base;
921 int next_tick = 10*HZ;
922
923 if (netif_msg_timer(np)) {
924 printk(KERN_DEBUG "%s: Media selection timer tick, intr status %4.4x, "
925 "Tx %x Rx %x.\n",
926 dev->name, ioread16(ioaddr + IntrEnable),
927 ioread8(ioaddr + TxStatus), ioread32(ioaddr + RxStatus));
928 }
929 check_duplex(dev);
930 np->timer.expires = jiffies + next_tick;
931 add_timer(&np->timer);
932}
933
934static void tx_timeout(struct net_device *dev)
935{
936 struct netdev_private *np = netdev_priv(dev);
937 void __iomem *ioaddr = np->base;
938 unsigned long flag;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 netif_stop_queue(dev);
941 tasklet_disable(&np->tx_tasklet);
942 iowrite16(0, ioaddr + IntrEnable);
943 printk(KERN_WARNING "%s: Transmit timed out, TxStatus %2.2x "
944 "TxFrameId %2.2x,"
945 " resetting...\n", dev->name, ioread8(ioaddr + TxStatus),
946 ioread8(ioaddr + TxFrameId));
947
948 {
949 int i;
950 for (i=0; i<TX_RING_SIZE; i++) {
951 printk(KERN_DEBUG "%02x %08llx %08x %08x(%02x) %08x %08x\n", i,
952 (unsigned long long)(np->tx_ring_dma + i*sizeof(*np->tx_ring)),
953 le32_to_cpu(np->tx_ring[i].next_desc),
954 le32_to_cpu(np->tx_ring[i].status),
955 (le32_to_cpu(np->tx_ring[i].status) >> 2) & 0xff,
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400956 le32_to_cpu(np->tx_ring[i].frag[0].addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 le32_to_cpu(np->tx_ring[i].frag[0].length));
958 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400959 printk(KERN_DEBUG "TxListPtr=%08x netif_queue_stopped=%d\n",
960 ioread32(np->base + TxListPtr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 netif_queue_stopped(dev));
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400962 printk(KERN_DEBUG "cur_tx=%d(%02x) dirty_tx=%d(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 np->cur_tx, np->cur_tx % TX_RING_SIZE,
964 np->dirty_tx, np->dirty_tx % TX_RING_SIZE);
965 printk(KERN_DEBUG "cur_rx=%d dirty_rx=%d\n", np->cur_rx, np->dirty_rx);
966 printk(KERN_DEBUG "cur_task=%d\n", np->cur_task);
967 }
968 spin_lock_irqsave(&np->lock, flag);
969
970 /* Stop and restart the chip's Tx processes . */
971 reset_tx(dev);
972 spin_unlock_irqrestore(&np->lock, flag);
973
974 dev->if_port = 0;
975
976 dev->trans_start = jiffies;
Eric Dumazet553e2332009-05-27 10:34:50 +0000977 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
979 netif_wake_queue(dev);
980 }
981 iowrite16(DEFAULT_INTR, ioaddr + IntrEnable);
982 tasklet_enable(&np->tx_tasklet);
983}
984
985
986/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
987static void init_ring(struct net_device *dev)
988{
989 struct netdev_private *np = netdev_priv(dev);
990 int i;
991
992 np->cur_rx = np->cur_tx = 0;
993 np->dirty_rx = np->dirty_tx = 0;
994 np->cur_task = 0;
995
996 np->rx_buf_sz = (dev->mtu <= 1520 ? PKT_BUF_SZ : dev->mtu + 16);
997
998 /* Initialize all Rx descriptors. */
999 for (i = 0; i < RX_RING_SIZE; i++) {
1000 np->rx_ring[i].next_desc = cpu_to_le32(np->rx_ring_dma +
1001 ((i+1)%RX_RING_SIZE)*sizeof(*np->rx_ring));
1002 np->rx_ring[i].status = 0;
1003 np->rx_ring[i].frag[0].length = 0;
1004 np->rx_skbuff[i] = NULL;
1005 }
1006
1007 /* Fill in the Rx buffers. Handle allocation failure gracefully. */
1008 for (i = 0; i < RX_RING_SIZE; i++) {
1009 struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
1010 np->rx_skbuff[i] = skb;
1011 if (skb == NULL)
1012 break;
1013 skb->dev = dev; /* Mark as being used by this device. */
1014 skb_reserve(skb, 2); /* 16 byte align the IP header. */
1015 np->rx_ring[i].frag[0].addr = cpu_to_le32(
David S. Miller689be432005-06-28 15:25:31 -07001016 pci_map_single(np->pci_dev, skb->data, np->rx_buf_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 PCI_DMA_FROMDEVICE));
1018 np->rx_ring[i].frag[0].length = cpu_to_le32(np->rx_buf_sz | LastFrag);
1019 }
1020 np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
1021
1022 for (i = 0; i < TX_RING_SIZE; i++) {
1023 np->tx_skbuff[i] = NULL;
1024 np->tx_ring[i].status = 0;
1025 }
1026 return;
1027}
1028
1029static void tx_poll (unsigned long data)
1030{
1031 struct net_device *dev = (struct net_device *)data;
1032 struct netdev_private *np = netdev_priv(dev);
1033 unsigned head = np->cur_task % TX_RING_SIZE;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001034 struct netdev_desc *txdesc =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 &np->tx_ring[(np->cur_tx - 1) % TX_RING_SIZE];
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* Chain the next pointer */
1038 for (; np->cur_tx - np->cur_task > 0; np->cur_task++) {
1039 int entry = np->cur_task % TX_RING_SIZE;
1040 txdesc = &np->tx_ring[entry];
1041 if (np->last_tx) {
1042 np->last_tx->next_desc = cpu_to_le32(np->tx_ring_dma +
1043 entry*sizeof(struct netdev_desc));
1044 }
1045 np->last_tx = txdesc;
1046 }
1047 /* Indicate the latest descriptor of tx ring */
1048 txdesc->status |= cpu_to_le32(DescIntrOnTx);
1049
1050 if (ioread32 (np->base + TxListPtr) == 0)
1051 iowrite32 (np->tx_ring_dma + head * sizeof(struct netdev_desc),
1052 np->base + TxListPtr);
1053 return;
1054}
1055
Stephen Hemminger613573252009-08-31 19:50:58 +00001056static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057start_tx (struct sk_buff *skb, struct net_device *dev)
1058{
1059 struct netdev_private *np = netdev_priv(dev);
1060 struct netdev_desc *txdesc;
1061 unsigned entry;
1062
1063 /* Calculate the next Tx descriptor entry. */
1064 entry = np->cur_tx % TX_RING_SIZE;
1065 np->tx_skbuff[entry] = skb;
1066 txdesc = &np->tx_ring[entry];
1067
1068 txdesc->next_desc = 0;
1069 txdesc->status = cpu_to_le32 ((entry << 2) | DisableAlign);
1070 txdesc->frag[0].addr = cpu_to_le32 (pci_map_single (np->pci_dev, skb->data,
1071 skb->len,
1072 PCI_DMA_TODEVICE));
1073 txdesc->frag[0].length = cpu_to_le32 (skb->len | LastFrag);
1074
1075 /* Increment cur_tx before tasklet_schedule() */
1076 np->cur_tx++;
1077 mb();
1078 /* Schedule a tx_poll() task */
1079 tasklet_schedule(&np->tx_tasklet);
1080
1081 /* On some architectures: explicitly flush cache lines here. */
Joe Perches8e95a202009-12-03 07:58:21 +00001082 if (np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 1 &&
1083 !netif_queue_stopped(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* do nothing */
1085 } else {
1086 netif_stop_queue (dev);
1087 }
1088 dev->trans_start = jiffies;
1089 if (netif_msg_tx_queued(np)) {
1090 printk (KERN_DEBUG
1091 "%s: Transmit frame #%d queued in slot %d.\n",
1092 dev->name, np->cur_tx, entry);
1093 }
Patrick McHardy6ed10652009-06-23 06:03:08 +00001094 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097/* Reset hardware tx and free all of tx buffers */
1098static int
1099reset_tx (struct net_device *dev)
1100{
1101 struct netdev_private *np = netdev_priv(dev);
1102 void __iomem *ioaddr = np->base;
1103 struct sk_buff *skb;
1104 int i;
1105 int irq = in_interrupt();
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* Reset tx logic, TxListPtr will be cleaned */
1108 iowrite16 (TxDisable, ioaddr + MACCtrl1);
Philippe De Muytere714d992006-08-03 18:42:15 +02001109 sundance_reset(dev, (NetworkReset|FIFOReset|DMAReset|TxReset) << 16);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* free all tx skbuff */
1112 for (i = 0; i < TX_RING_SIZE; i++) {
Jesse Huang2109f892006-10-20 14:42:11 -07001113 np->tx_ring[i].next_desc = 0;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 skb = np->tx_skbuff[i];
1116 if (skb) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001117 pci_unmap_single(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001118 le32_to_cpu(np->tx_ring[i].frag[0].addr),
1119 skb->len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (irq)
1121 dev_kfree_skb_irq (skb);
1122 else
1123 dev_kfree_skb (skb);
1124 np->tx_skbuff[i] = NULL;
Eric Dumazet553e2332009-05-27 10:34:50 +00001125 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127 }
1128 np->cur_tx = np->dirty_tx = 0;
1129 np->cur_task = 0;
Jesse Huang2109f892006-10-20 14:42:11 -07001130
Randy Dunlapbca79eb2006-11-29 13:15:17 -08001131 np->last_tx = NULL;
Jesse Huang2109f892006-10-20 14:42:11 -07001132 iowrite8(127, ioaddr + TxDMAPollPeriod);
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 iowrite16 (StatsEnable | RxEnable | TxEnable, ioaddr + MACCtrl1);
1135 return 0;
1136}
1137
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001138/* The interrupt handler cleans up after the Tx thread,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 and schedule a Rx thread work */
David Howells7d12e782006-10-05 14:55:46 +01001140static irqreturn_t intr_handler(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 struct net_device *dev = (struct net_device *)dev_instance;
1143 struct netdev_private *np = netdev_priv(dev);
1144 void __iomem *ioaddr = np->base;
1145 int hw_frame_id;
1146 int tx_cnt;
1147 int tx_status;
1148 int handled = 0;
Jesse Huange2420402006-10-20 14:42:05 -07001149 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151
1152 do {
1153 int intr_status = ioread16(ioaddr + IntrStatus);
1154 iowrite16(intr_status, ioaddr + IntrStatus);
1155
1156 if (netif_msg_intr(np))
1157 printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
1158 dev->name, intr_status);
1159
1160 if (!(intr_status & DEFAULT_INTR))
1161 break;
1162
1163 handled = 1;
1164
1165 if (intr_status & (IntrRxDMADone)) {
1166 iowrite16(DEFAULT_INTR & ~(IntrRxDone|IntrRxDMADone),
1167 ioaddr + IntrEnable);
1168 if (np->budget < 0)
1169 np->budget = RX_BUDGET;
1170 tasklet_schedule(&np->rx_tasklet);
1171 }
1172 if (intr_status & (IntrTxDone | IntrDrvRqst)) {
1173 tx_status = ioread16 (ioaddr + TxStatus);
1174 for (tx_cnt=32; tx_status & 0x80; --tx_cnt) {
1175 if (netif_msg_tx_done(np))
1176 printk
1177 ("%s: Transmit status is %2.2x.\n",
1178 dev->name, tx_status);
1179 if (tx_status & 0x1e) {
Philippe De Muyterb71b95e2005-10-28 12:23:47 +02001180 if (netif_msg_tx_err(np))
1181 printk("%s: Transmit error status %4.4x.\n",
1182 dev->name, tx_status);
Eric Dumazet553e2332009-05-27 10:34:50 +00001183 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (tx_status & 0x10)
Eric Dumazet553e2332009-05-27 10:34:50 +00001185 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (tx_status & 0x08)
Eric Dumazet553e2332009-05-27 10:34:50 +00001187 dev->stats.collisions++;
Philippe De Muyterb71b95e2005-10-28 12:23:47 +02001188 if (tx_status & 0x04)
Eric Dumazet553e2332009-05-27 10:34:50 +00001189 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (tx_status & 0x02)
Eric Dumazet553e2332009-05-27 10:34:50 +00001191 dev->stats.tx_window_errors++;
Jesse Huange2420402006-10-20 14:42:05 -07001192
Philippe De Muyterb71b95e2005-10-28 12:23:47 +02001193 /*
1194 ** This reset has been verified on
1195 ** DFE-580TX boards ! phdm@macqel.be.
1196 */
1197 if (tx_status & 0x10) { /* TxUnderrun */
Philippe De Muyterb71b95e2005-10-28 12:23:47 +02001198 /* Restart Tx FIFO and transmitter */
1199 sundance_reset(dev, (NetworkReset|FIFOReset|TxReset) << 16);
Philippe De Muyterb71b95e2005-10-28 12:23:47 +02001200 /* No need to reset the Tx pointer here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Jesse Huang2109f892006-10-20 14:42:11 -07001202 /* Restart the Tx. Need to make sure tx enabled */
1203 i = 10;
1204 do {
1205 iowrite16(ioread16(ioaddr + MACCtrl1) | TxEnable, ioaddr + MACCtrl1);
1206 if (ioread16(ioaddr + MACCtrl1) & TxEnabled)
1207 break;
1208 mdelay(1);
1209 } while (--i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 /* Yup, this is a documentation bug. It cost me *hours*. */
1212 iowrite16 (0, ioaddr + TxStatus);
1213 if (tx_cnt < 0) {
1214 iowrite32(5000, ioaddr + DownCounter);
1215 break;
1216 }
1217 tx_status = ioread16 (ioaddr + TxStatus);
1218 }
1219 hw_frame_id = (tx_status >> 8) & 0xff;
1220 } else {
1221 hw_frame_id = ioread8(ioaddr + TxFrameId);
1222 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001223
Auke Kok44c10132007-06-08 15:46:36 -07001224 if (np->pci_dev->revision >= 0x14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 spin_lock(&np->lock);
1226 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1227 int entry = np->dirty_tx % TX_RING_SIZE;
1228 struct sk_buff *skb;
1229 int sw_frame_id;
1230 sw_frame_id = (le32_to_cpu(
1231 np->tx_ring[entry].status) >> 2) & 0xff;
1232 if (sw_frame_id == hw_frame_id &&
1233 !(le32_to_cpu(np->tx_ring[entry].status)
1234 & 0x00010000))
1235 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001236 if (sw_frame_id == (hw_frame_id + 1) %
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 TX_RING_SIZE)
1238 break;
1239 skb = np->tx_skbuff[entry];
1240 /* Free the original skb. */
1241 pci_unmap_single(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001242 le32_to_cpu(np->tx_ring[entry].frag[0].addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 skb->len, PCI_DMA_TODEVICE);
1244 dev_kfree_skb_irq (np->tx_skbuff[entry]);
1245 np->tx_skbuff[entry] = NULL;
1246 np->tx_ring[entry].frag[0].addr = 0;
1247 np->tx_ring[entry].frag[0].length = 0;
1248 }
1249 spin_unlock(&np->lock);
1250 } else {
1251 spin_lock(&np->lock);
1252 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1253 int entry = np->dirty_tx % TX_RING_SIZE;
1254 struct sk_buff *skb;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001255 if (!(le32_to_cpu(np->tx_ring[entry].status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 & 0x00010000))
1257 break;
1258 skb = np->tx_skbuff[entry];
1259 /* Free the original skb. */
1260 pci_unmap_single(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001261 le32_to_cpu(np->tx_ring[entry].frag[0].addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 skb->len, PCI_DMA_TODEVICE);
1263 dev_kfree_skb_irq (np->tx_skbuff[entry]);
1264 np->tx_skbuff[entry] = NULL;
1265 np->tx_ring[entry].frag[0].addr = 0;
1266 np->tx_ring[entry].frag[0].length = 0;
1267 }
1268 spin_unlock(&np->lock);
1269 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (netif_queue_stopped(dev) &&
1272 np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
1273 /* The ring is no longer full, clear busy flag. */
1274 netif_wake_queue (dev);
1275 }
1276 /* Abnormal error summary/uncommon events handlers. */
1277 if (intr_status & (IntrPCIErr | LinkChange | StatsMax))
1278 netdev_error(dev, intr_status);
1279 } while (0);
1280 if (netif_msg_intr(np))
1281 printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1282 dev->name, ioread16(ioaddr + IntrStatus));
1283 return IRQ_RETVAL(handled);
1284}
1285
1286static void rx_poll(unsigned long data)
1287{
1288 struct net_device *dev = (struct net_device *)data;
1289 struct netdev_private *np = netdev_priv(dev);
1290 int entry = np->cur_rx % RX_RING_SIZE;
1291 int boguscnt = np->budget;
1292 void __iomem *ioaddr = np->base;
1293 int received = 0;
1294
1295 /* If EOP is set on the next entry, it's a new packet. Send it up. */
1296 while (1) {
1297 struct netdev_desc *desc = &(np->rx_ring[entry]);
1298 u32 frame_status = le32_to_cpu(desc->status);
1299 int pkt_len;
1300
1301 if (--boguscnt < 0) {
1302 goto not_done;
1303 }
1304 if (!(frame_status & DescOwn))
1305 break;
1306 pkt_len = frame_status & 0x1fff; /* Chip omits the CRC. */
1307 if (netif_msg_rx_status(np))
1308 printk(KERN_DEBUG " netdev_rx() status was %8.8x.\n",
1309 frame_status);
1310 if (frame_status & 0x001f4000) {
1311 /* There was a error. */
1312 if (netif_msg_rx_err(np))
1313 printk(KERN_DEBUG " netdev_rx() Rx error was %8.8x.\n",
1314 frame_status);
Eric Dumazet553e2332009-05-27 10:34:50 +00001315 dev->stats.rx_errors++;
1316 if (frame_status & 0x00100000)
1317 dev->stats.rx_length_errors++;
1318 if (frame_status & 0x00010000)
1319 dev->stats.rx_fifo_errors++;
1320 if (frame_status & 0x00060000)
1321 dev->stats.rx_frame_errors++;
1322 if (frame_status & 0x00080000)
1323 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (frame_status & 0x00100000) {
1325 printk(KERN_WARNING "%s: Oversized Ethernet frame,"
1326 " status %8.8x.\n",
1327 dev->name, frame_status);
1328 }
1329 } else {
1330 struct sk_buff *skb;
1331#ifndef final_version
1332 if (netif_msg_rx_status(np))
1333 printk(KERN_DEBUG " netdev_rx() normal Rx pkt length %d"
1334 ", bogus_cnt %d.\n",
1335 pkt_len, boguscnt);
1336#endif
1337 /* Check if the packet is long enough to accept without copying
1338 to a minimally-sized skbuff. */
Joe Perches8e95a202009-12-03 07:58:21 +00001339 if (pkt_len < rx_copybreak &&
1340 (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 skb_reserve(skb, 2); /* 16 byte align the IP header */
1342 pci_dma_sync_single_for_cpu(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001343 le32_to_cpu(desc->frag[0].addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 np->rx_buf_sz,
1345 PCI_DMA_FROMDEVICE);
1346
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001347 skb_copy_to_linear_data(skb, np->rx_skbuff[entry]->data, pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 pci_dma_sync_single_for_device(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001349 le32_to_cpu(desc->frag[0].addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 np->rx_buf_sz,
1351 PCI_DMA_FROMDEVICE);
1352 skb_put(skb, pkt_len);
1353 } else {
1354 pci_unmap_single(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001355 le32_to_cpu(desc->frag[0].addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 np->rx_buf_sz,
1357 PCI_DMA_FROMDEVICE);
1358 skb_put(skb = np->rx_skbuff[entry], pkt_len);
1359 np->rx_skbuff[entry] = NULL;
1360 }
1361 skb->protocol = eth_type_trans(skb, dev);
1362 /* Note: checksum -> skb->ip_summed = CHECKSUM_UNNECESSARY; */
1363 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 entry = (entry + 1) % RX_RING_SIZE;
1366 received++;
1367 }
1368 np->cur_rx = entry;
1369 refill_rx (dev);
1370 np->budget -= received;
1371 iowrite16(DEFAULT_INTR, ioaddr + IntrEnable);
1372 return;
1373
1374not_done:
1375 np->cur_rx = entry;
1376 refill_rx (dev);
1377 if (!received)
1378 received = 1;
1379 np->budget -= received;
1380 if (np->budget <= 0)
1381 np->budget = RX_BUDGET;
1382 tasklet_schedule(&np->rx_tasklet);
1383 return;
1384}
1385
1386static void refill_rx (struct net_device *dev)
1387{
1388 struct netdev_private *np = netdev_priv(dev);
1389 int entry;
1390 int cnt = 0;
1391
1392 /* Refill the Rx ring buffers. */
1393 for (;(np->cur_rx - np->dirty_rx + RX_RING_SIZE) % RX_RING_SIZE > 0;
1394 np->dirty_rx = (np->dirty_rx + 1) % RX_RING_SIZE) {
1395 struct sk_buff *skb;
1396 entry = np->dirty_rx % RX_RING_SIZE;
1397 if (np->rx_skbuff[entry] == NULL) {
1398 skb = dev_alloc_skb(np->rx_buf_sz);
1399 np->rx_skbuff[entry] = skb;
1400 if (skb == NULL)
1401 break; /* Better luck next round. */
1402 skb->dev = dev; /* Mark as being used by this device. */
1403 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
1404 np->rx_ring[entry].frag[0].addr = cpu_to_le32(
David S. Miller689be432005-06-28 15:25:31 -07001405 pci_map_single(np->pci_dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 np->rx_buf_sz, PCI_DMA_FROMDEVICE));
1407 }
1408 /* Perhaps we need not reset this field. */
1409 np->rx_ring[entry].frag[0].length =
1410 cpu_to_le32(np->rx_buf_sz | LastFrag);
1411 np->rx_ring[entry].status = 0;
1412 cnt++;
1413 }
1414 return;
1415}
1416static void netdev_error(struct net_device *dev, int intr_status)
1417{
1418 struct netdev_private *np = netdev_priv(dev);
1419 void __iomem *ioaddr = np->base;
1420 u16 mii_ctl, mii_advertise, mii_lpa;
1421 int speed;
1422
1423 if (intr_status & LinkChange) {
Dan Nicholson50500152008-08-20 16:51:59 -07001424 if (mdio_wait_link(dev, 10) == 0) {
1425 printk(KERN_INFO "%s: Link up\n", dev->name);
1426 if (np->an_enable) {
1427 mii_advertise = mdio_read(dev, np->phys[0],
1428 MII_ADVERTISE);
1429 mii_lpa = mdio_read(dev, np->phys[0], MII_LPA);
1430 mii_advertise &= mii_lpa;
1431 printk(KERN_INFO "%s: Link changed: ",
1432 dev->name);
1433 if (mii_advertise & ADVERTISE_100FULL) {
1434 np->speed = 100;
1435 printk("100Mbps, full duplex\n");
1436 } else if (mii_advertise & ADVERTISE_100HALF) {
1437 np->speed = 100;
1438 printk("100Mbps, half duplex\n");
1439 } else if (mii_advertise & ADVERTISE_10FULL) {
1440 np->speed = 10;
1441 printk("10Mbps, full duplex\n");
1442 } else if (mii_advertise & ADVERTISE_10HALF) {
1443 np->speed = 10;
1444 printk("10Mbps, half duplex\n");
1445 } else
1446 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Dan Nicholson50500152008-08-20 16:51:59 -07001448 } else {
1449 mii_ctl = mdio_read(dev, np->phys[0], MII_BMCR);
1450 speed = (mii_ctl & BMCR_SPEED100) ? 100 : 10;
1451 np->speed = speed;
1452 printk(KERN_INFO "%s: Link changed: %dMbps ,",
1453 dev->name, speed);
1454 printk("%s duplex.\n",
1455 (mii_ctl & BMCR_FULLDPLX) ?
1456 "full" : "half");
1457 }
1458 check_duplex(dev);
1459 if (np->flowctrl && np->mii_if.full_duplex) {
1460 iowrite16(ioread16(ioaddr + MulticastFilter1+2) | 0x0200,
1461 ioaddr + MulticastFilter1+2);
1462 iowrite16(ioread16(ioaddr + MACCtrl0) | EnbFlowCtrl,
1463 ioaddr + MACCtrl0);
1464 }
1465 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 } else {
Dan Nicholson50500152008-08-20 16:51:59 -07001467 printk(KERN_INFO "%s: Link down\n", dev->name);
1468 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 }
1471 if (intr_status & StatsMax) {
1472 get_stats(dev);
1473 }
1474 if (intr_status & IntrPCIErr) {
1475 printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",
1476 dev->name, intr_status);
1477 /* We must do a global reset of DMA to continue. */
1478 }
1479}
1480
1481static struct net_device_stats *get_stats(struct net_device *dev)
1482{
1483 struct netdev_private *np = netdev_priv(dev);
1484 void __iomem *ioaddr = np->base;
1485 int i;
1486
1487 /* We should lock this segment of code for SMP eventually, although
1488 the vulnerability window is very small and statistics are
1489 non-critical. */
1490 /* The chip only need report frame silently dropped. */
Eric Dumazet553e2332009-05-27 10:34:50 +00001491 dev->stats.rx_missed_errors += ioread8(ioaddr + RxMissed);
1492 dev->stats.tx_packets += ioread16(ioaddr + TxFramesOK);
1493 dev->stats.rx_packets += ioread16(ioaddr + RxFramesOK);
1494 dev->stats.collisions += ioread8(ioaddr + StatsLateColl);
1495 dev->stats.collisions += ioread8(ioaddr + StatsMultiColl);
1496 dev->stats.collisions += ioread8(ioaddr + StatsOneColl);
1497 dev->stats.tx_carrier_errors += ioread8(ioaddr + StatsCarrierError);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ioread8(ioaddr + StatsTxDefer);
1499 for (i = StatsTxDefer; i <= StatsMcastRx; i++)
1500 ioread8(ioaddr + i);
Eric Dumazet553e2332009-05-27 10:34:50 +00001501 dev->stats.tx_bytes += ioread16(ioaddr + TxOctetsLow);
1502 dev->stats.tx_bytes += ioread16(ioaddr + TxOctetsHigh) << 16;
1503 dev->stats.rx_bytes += ioread16(ioaddr + RxOctetsLow);
1504 dev->stats.rx_bytes += ioread16(ioaddr + RxOctetsHigh) << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Eric Dumazet553e2332009-05-27 10:34:50 +00001506 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
1509static void set_rx_mode(struct net_device *dev)
1510{
1511 struct netdev_private *np = netdev_priv(dev);
1512 void __iomem *ioaddr = np->base;
1513 u16 mc_filter[4]; /* Multicast hash filter */
1514 u32 rx_mode;
1515 int i;
1516
1517 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 memset(mc_filter, 0xff, sizeof(mc_filter));
1519 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptAll | AcceptMyPhys;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001520 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00001521 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /* Too many to match, or accept all multicasts. */
1523 memset(mc_filter, 0xff, sizeof(mc_filter));
1524 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001525 } else if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001526 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int bit;
1528 int index;
1529 int crc;
1530 memset (mc_filter, 0, sizeof (mc_filter));
Jiri Pirko22bedad2010-04-01 21:22:57 +00001531 netdev_for_each_mc_addr(ha, dev) {
1532 crc = ether_crc_le(ETH_ALEN, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 for (index=0, bit=0; bit < 6; bit++, crc <<= 1)
1534 if (crc & 0x80000000) index |= 1 << bit;
1535 mc_filter[index/16] |= (1 << (index % 16));
1536 }
1537 rx_mode = AcceptBroadcast | AcceptMultiHash | AcceptMyPhys;
1538 } else {
1539 iowrite8(AcceptBroadcast | AcceptMyPhys, ioaddr + RxMode);
1540 return;
1541 }
1542 if (np->mii_if.full_duplex && np->flowctrl)
1543 mc_filter[3] |= 0x0200;
1544
1545 for (i = 0; i < 4; i++)
1546 iowrite16(mc_filter[i], ioaddr + MulticastFilter0 + i*2);
1547 iowrite8(rx_mode, ioaddr + RxMode);
1548}
1549
1550static int __set_mac_addr(struct net_device *dev)
1551{
1552 struct netdev_private *np = netdev_priv(dev);
1553 u16 addr16;
1554
1555 addr16 = (dev->dev_addr[0] | (dev->dev_addr[1] << 8));
1556 iowrite16(addr16, np->base + StationAddr);
1557 addr16 = (dev->dev_addr[2] | (dev->dev_addr[3] << 8));
1558 iowrite16(addr16, np->base + StationAddr+2);
1559 addr16 = (dev->dev_addr[4] | (dev->dev_addr[5] << 8));
1560 iowrite16(addr16, np->base + StationAddr+4);
1561 return 0;
1562}
1563
1564static int check_if_running(struct net_device *dev)
1565{
1566 if (!netif_running(dev))
1567 return -EINVAL;
1568 return 0;
1569}
1570
1571static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1572{
1573 struct netdev_private *np = netdev_priv(dev);
1574 strcpy(info->driver, DRV_NAME);
1575 strcpy(info->version, DRV_VERSION);
1576 strcpy(info->bus_info, pci_name(np->pci_dev));
1577}
1578
1579static int get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1580{
1581 struct netdev_private *np = netdev_priv(dev);
1582 spin_lock_irq(&np->lock);
1583 mii_ethtool_gset(&np->mii_if, ecmd);
1584 spin_unlock_irq(&np->lock);
1585 return 0;
1586}
1587
1588static int set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1589{
1590 struct netdev_private *np = netdev_priv(dev);
1591 int res;
1592 spin_lock_irq(&np->lock);
1593 res = mii_ethtool_sset(&np->mii_if, ecmd);
1594 spin_unlock_irq(&np->lock);
1595 return res;
1596}
1597
1598static int nway_reset(struct net_device *dev)
1599{
1600 struct netdev_private *np = netdev_priv(dev);
1601 return mii_nway_restart(&np->mii_if);
1602}
1603
1604static u32 get_link(struct net_device *dev)
1605{
1606 struct netdev_private *np = netdev_priv(dev);
1607 return mii_link_ok(&np->mii_if);
1608}
1609
1610static u32 get_msglevel(struct net_device *dev)
1611{
1612 struct netdev_private *np = netdev_priv(dev);
1613 return np->msg_enable;
1614}
1615
1616static void set_msglevel(struct net_device *dev, u32 val)
1617{
1618 struct netdev_private *np = netdev_priv(dev);
1619 np->msg_enable = val;
1620}
1621
Jeff Garzik7282d492006-09-13 14:30:00 -04001622static const struct ethtool_ops ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 .begin = check_if_running,
1624 .get_drvinfo = get_drvinfo,
1625 .get_settings = get_settings,
1626 .set_settings = set_settings,
1627 .nway_reset = nway_reset,
1628 .get_link = get_link,
1629 .get_msglevel = get_msglevel,
1630 .set_msglevel = set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631};
1632
1633static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1634{
1635 struct netdev_private *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 if (!netif_running(dev))
1639 return -EINVAL;
1640
1641 spin_lock_irq(&np->lock);
1642 rc = generic_mii_ioctl(&np->mii_if, if_mii(rq), cmd, NULL);
1643 spin_unlock_irq(&np->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 return rc;
1646}
1647
1648static int netdev_close(struct net_device *dev)
1649{
1650 struct netdev_private *np = netdev_priv(dev);
1651 void __iomem *ioaddr = np->base;
1652 struct sk_buff *skb;
1653 int i;
1654
Jesse Huang31f817e2006-11-08 19:49:12 -08001655 /* Wait and kill tasklet */
1656 tasklet_kill(&np->rx_tasklet);
1657 tasklet_kill(&np->tx_tasklet);
1658 np->cur_tx = 0;
1659 np->dirty_tx = 0;
1660 np->cur_task = 0;
Randy Dunlapbca79eb2006-11-29 13:15:17 -08001661 np->last_tx = NULL;
Jesse Huang31f817e2006-11-08 19:49:12 -08001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 netif_stop_queue(dev);
1664
1665 if (netif_msg_ifdown(np)) {
1666 printk(KERN_DEBUG "%s: Shutting down ethercard, status was Tx %2.2x "
1667 "Rx %4.4x Int %2.2x.\n",
1668 dev->name, ioread8(ioaddr + TxStatus),
1669 ioread32(ioaddr + RxStatus), ioread16(ioaddr + IntrStatus));
1670 printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
1671 dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx);
1672 }
1673
1674 /* Disable interrupts by clearing the interrupt mask. */
1675 iowrite16(0x0000, ioaddr + IntrEnable);
1676
Jesse Huangacd70c22006-10-20 14:42:13 -07001677 /* Disable Rx and Tx DMA for safely release resource */
1678 iowrite32(0x500, ioaddr + DMACtrl);
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* Stop the chip's Tx and Rx processes. */
1681 iowrite16(TxDisable | RxDisable | StatsDisable, ioaddr + MACCtrl1);
1682
Jesse Huang31f817e2006-11-08 19:49:12 -08001683 for (i = 2000; i > 0; i--) {
1684 if ((ioread32(ioaddr + DMACtrl) & 0xc000) == 0)
1685 break;
1686 mdelay(1);
1687 }
1688
1689 iowrite16(GlobalReset | DMAReset | FIFOReset | NetworkReset,
1690 ioaddr +ASICCtrl + 2);
1691
1692 for (i = 2000; i > 0; i--) {
1693 if ((ioread16(ioaddr + ASICCtrl +2) & ResetBusy) == 0)
1694 break;
1695 mdelay(1);
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698#ifdef __i386__
1699 if (netif_msg_hw(np)) {
Joe Perchesad361c92009-07-06 13:05:40 -07001700 printk(KERN_DEBUG " Tx ring at %8.8x:\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 (int)(np->tx_ring_dma));
1702 for (i = 0; i < TX_RING_SIZE; i++)
Joe Perchesad361c92009-07-06 13:05:40 -07001703 printk(KERN_DEBUG " #%d desc. %4.4x %8.8x %8.8x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 i, np->tx_ring[i].status, np->tx_ring[i].frag[0].addr,
1705 np->tx_ring[i].frag[0].length);
Joe Perchesad361c92009-07-06 13:05:40 -07001706 printk(KERN_DEBUG " Rx ring %8.8x:\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 (int)(np->rx_ring_dma));
1708 for (i = 0; i < /*RX_RING_SIZE*/4 ; i++) {
1709 printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x\n",
1710 i, np->rx_ring[i].status, np->rx_ring[i].frag[0].addr,
1711 np->rx_ring[i].frag[0].length);
1712 }
1713 }
1714#endif /* __i386__ debugging only */
1715
1716 free_irq(dev->irq, dev);
1717
1718 del_timer_sync(&np->timer);
1719
1720 /* Free all the skbuffs in the Rx queue. */
1721 for (i = 0; i < RX_RING_SIZE; i++) {
1722 np->rx_ring[i].status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 skb = np->rx_skbuff[i];
1724 if (skb) {
1725 pci_unmap_single(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001726 le32_to_cpu(np->rx_ring[i].frag[0].addr),
1727 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 dev_kfree_skb(skb);
1729 np->rx_skbuff[i] = NULL;
1730 }
Al Viro14c9d9b2007-12-09 16:50:47 +00001731 np->rx_ring[i].frag[0].addr = cpu_to_le32(0xBADF00D0); /* poison */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733 for (i = 0; i < TX_RING_SIZE; i++) {
Jesse Huang31f817e2006-11-08 19:49:12 -08001734 np->tx_ring[i].next_desc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 skb = np->tx_skbuff[i];
1736 if (skb) {
1737 pci_unmap_single(np->pci_dev,
Al Viro14c9d9b2007-12-09 16:50:47 +00001738 le32_to_cpu(np->tx_ring[i].frag[0].addr),
1739 skb->len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 dev_kfree_skb(skb);
1741 np->tx_skbuff[i] = NULL;
1742 }
1743 }
1744
1745 return 0;
1746}
1747
1748static void __devexit sundance_remove1 (struct pci_dev *pdev)
1749{
1750 struct net_device *dev = pci_get_drvdata(pdev);
1751
1752 if (dev) {
1753 struct netdev_private *np = netdev_priv(dev);
1754
1755 unregister_netdev(dev);
1756 pci_free_consistent(pdev, RX_TOTAL_SIZE, np->rx_ring,
1757 np->rx_ring_dma);
1758 pci_free_consistent(pdev, TX_TOTAL_SIZE, np->tx_ring,
1759 np->tx_ring_dma);
1760 pci_iounmap(pdev, np->base);
1761 pci_release_regions(pdev);
1762 free_netdev(dev);
1763 pci_set_drvdata(pdev, NULL);
1764 }
1765}
1766
1767static struct pci_driver sundance_driver = {
1768 .name = DRV_NAME,
1769 .id_table = sundance_pci_tbl,
1770 .probe = sundance_probe1,
1771 .remove = __devexit_p(sundance_remove1),
1772};
1773
1774static int __init sundance_init(void)
1775{
1776/* when a module, this is printed whether or not devices are found in probe */
1777#ifdef MODULE
1778 printk(version);
1779#endif
Jeff Garzik29917622006-08-19 17:48:59 -04001780 return pci_register_driver(&sundance_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
1783static void __exit sundance_exit(void)
1784{
1785 pci_unregister_driver(&sundance_driver);
1786}
1787
1788module_init(sundance_init);
1789module_exit(sundance_exit);
1790
1791