blob: bcdbdc72b558f332c0b8ab7a662ff28cbf158f68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* via-rhine.c: A Linux Ethernet device driver for VIA Rhine family chips. */
2/*
3 Written 1998-2001 by Donald Becker.
4
5 Current Maintainer: Roger Luethi <rl@hellgate.ch>
6
7 This software may be used and distributed according to the terms of
8 the GNU General Public License (GPL), incorporated herein by reference.
9 Drivers based on or derived from this code fall under the GPL and must
10 retain the authorship, copyright and license notice. This file is not
11 a complete program and may only be used when the entire operating
12 system is licensed under the GPL.
13
14 This driver is designed for the VIA VT86C100A Rhine-I.
15 It also works with the Rhine-II (6102) and Rhine-III (6105/6105L/6105LOM
16 and management NIC 6105M).
17
18 The author may be reached as becker@scyld.com, or C/O
19 Scyld Computing Corporation
20 410 Severn Ave., Suite 210
21 Annapolis MD 21403
22
23
24 This driver contains some changes from the original Donald Becker
25 version. He may or may not be interested in bug reports on this
26 code. You can find his versions at:
27 http://www.scyld.com/network/via-rhine.html
Jeff Garzik03a8c662006-06-27 07:57:22 -040028 [link no longer provides useful info -jgarzik]
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30*/
31
Joe Perchesdf4511f2011-04-16 14:15:25 +000032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define DRV_NAME "via-rhine"
Roger Luethi38f49e82010-12-06 00:59:40 +000035#define DRV_VERSION "1.5.0"
36#define DRV_RELDATE "2010-10-09"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
39/* A few user-configurable values.
40 These may be modified when a driver module is loaded. */
41
Joe Perchesdf4511f2011-04-16 14:15:25 +000042#define DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
44static int max_interrupt_work = 20;
45
46/* Set the copy breakpoint for the copy-only-tiny-frames scheme.
47 Setting to > 1518 effectively disables this feature. */
Joe Perches8e95a202009-12-03 07:58:21 +000048#if defined(__alpha__) || defined(__arm__) || defined(__hppa__) || \
49 defined(CONFIG_SPARC) || defined(__ia64__) || \
50 defined(__sh__) || defined(__mips__)
Dustin Marquessb47157f2007-08-10 14:05:15 -070051static int rx_copybreak = 1518;
52#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int rx_copybreak;
Dustin Marquessb47157f2007-08-10 14:05:15 -070054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Roger Luethib933b4d2006-08-14 23:00:21 -070056/* Work-around for broken BIOSes: they are unable to get the chip back out of
57 power state D3 so PXE booting fails. bootparam(7): via-rhine.avoid_D3=1 */
58static int avoid_D3;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * In case you are looking for 'options[]' or 'full_duplex[]', they
62 * are gone. Use ethtool(8) instead.
63 */
64
65/* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
66 The Rhine has a 64 element 8390-like hash table. */
67static const int multicast_filter_limit = 32;
68
69
70/* Operational parameters that are set at compile time. */
71
72/* Keep the ring sizes a power of two for compile efficiency.
73 The compiler will convert <unsigned>'%'<2^N> into a bit mask.
74 Making the Tx ring too large decreases the effectiveness of channel
75 bonding and packet priority.
76 There are no ill effects from too-large receive rings. */
77#define TX_RING_SIZE 16
78#define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
Roger Luethi633949a2006-08-14 23:00:17 -070079#define RX_RING_SIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* Operational parameters that usually are not changed. */
82
83/* Time in jiffies before concluding the transmitter is hung. */
84#define TX_TIMEOUT (2*HZ)
85
86#define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
87
88#include <linux/module.h>
89#include <linux/moduleparam.h>
90#include <linux/kernel.h>
91#include <linux/string.h>
92#include <linux/timer.h>
93#include <linux/errno.h>
94#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/interrupt.h>
96#include <linux/pci.h>
Domen Puncer1e7f0bd2005-06-26 18:22:14 -040097#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/netdevice.h>
99#include <linux/etherdevice.h>
100#include <linux/skbuff.h>
101#include <linux/init.h>
102#include <linux/delay.h>
103#include <linux/mii.h>
104#include <linux/ethtool.h>
105#include <linux/crc32.h>
Roger Luethi38f49e82010-12-06 00:59:40 +0000106#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#include <linux/bitops.h>
Jarek Poplawskic0d7a022009-12-23 21:54:29 -0800108#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#include <asm/processor.h> /* Processor type for cache alignment. */
110#include <asm/io.h>
111#include <asm/irq.h>
112#include <asm/uaccess.h>
Roger Luethie84df482007-03-06 19:57:37 +0100113#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/* These identify the driver base version and may not be removed. */
Stephen Hemmingerc8de1fc2009-02-26 10:19:31 +0000116static const char version[] __devinitconst =
Joe Perchesdf4511f2011-04-16 14:15:25 +0000117 "v1.10-LK" DRV_VERSION " " DRV_RELDATE " Written by Donald Becker";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/* This driver was written to use PCI memory space. Some early versions
120 of the Rhine may only work correctly with I/O space accesses. */
121#ifdef CONFIG_VIA_RHINE_MMIO
122#define USE_MMIO
123#else
124#endif
125
126MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
127MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
128MODULE_LICENSE("GPL");
129
130module_param(max_interrupt_work, int, 0);
131module_param(debug, int, 0);
132module_param(rx_copybreak, int, 0);
Roger Luethib933b4d2006-08-14 23:00:21 -0700133module_param(avoid_D3, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134MODULE_PARM_DESC(max_interrupt_work, "VIA Rhine maximum events handled per interrupt");
135MODULE_PARM_DESC(debug, "VIA Rhine debug level (0-7)");
136MODULE_PARM_DESC(rx_copybreak, "VIA Rhine copy breakpoint for copy-only-tiny-frames");
Roger Luethib933b4d2006-08-14 23:00:21 -0700137MODULE_PARM_DESC(avoid_D3, "Avoid power state D3 (work-around for broken BIOSes)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Roger Luethi38f49e82010-12-06 00:59:40 +0000139#define MCAM_SIZE 32
140#define VCAM_SIZE 32
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 Theory of Operation
144
145I. Board Compatibility
146
147This driver is designed for the VIA 86c100A Rhine-II PCI Fast Ethernet
148controller.
149
150II. Board-specific settings
151
152Boards with this chip are functional only in a bus-master PCI slot.
153
154Many operational settings are loaded from the EEPROM to the Config word at
155offset 0x78. For most of these settings, this driver assumes that they are
156correct.
157If this driver is compiled to use PCI memory space operations the EEPROM
158must be configured to enable memory ops.
159
160III. Driver operation
161
162IIIa. Ring buffers
163
164This driver uses two statically allocated fixed-size descriptor lists
165formed into rings by a branch from the final descriptor to the beginning of
166the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
167
168IIIb/c. Transmit/Receive Structure
169
170This driver attempts to use a zero-copy receive and transmit scheme.
171
172Alas, all data buffers are required to start on a 32 bit boundary, so
173the driver must often copy transmit packets into bounce buffers.
174
175The driver allocates full frame size skbuffs for the Rx ring buffers at
176open() time and passes the skb->data field to the chip as receive data
177buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
178a fresh skbuff is allocated and the frame is copied to the new skbuff.
179When the incoming frame is larger, the skbuff is passed directly up the
180protocol stack. Buffers consumed this way are replaced by newly allocated
181skbuffs in the last phase of rhine_rx().
182
183The RX_COPYBREAK value is chosen to trade-off the memory wasted by
184using a full-sized skbuff for small frames vs. the copying costs of larger
185frames. New boards are typically used in generously configured machines
186and the underfilled buffers have negligible impact compared to the benefit of
187a single allocation size, so the default value of zero results in never
188copying packets. When copying is done, the cost is usually mitigated by using
189a combined copy/checksum routine. Copying also preloads the cache, which is
190most useful with small frames.
191
192Since the VIA chips are only able to transfer data to buffers on 32 bit
193boundaries, the IP header at offset 14 in an ethernet frame isn't
194longword aligned for further processing. Copying these unaligned buffers
195has the beneficial effect of 16-byte aligning the IP header.
196
197IIId. Synchronization
198
199The driver runs as two independent, single-threaded flows of control. One
200is the send-packet routine, which enforces single-threaded use by the
Wang Chenb74ca3a2008-12-08 01:14:16 -0800201netdev_priv(dev)->lock spinlock. The other thread is the interrupt handler,
202which is single threaded by the hardware and interrupt handling software.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204The send packet thread has partial control over the Tx ring. It locks the
Wang Chenb74ca3a2008-12-08 01:14:16 -0800205netdev_priv(dev)->lock whenever it's queuing a Tx packet. If the next slot in
206the ring is not available it stops the transmit queue by
207calling netif_stop_queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209The interrupt handler has exclusive control over the Rx ring and records stats
210from the Tx ring. After reaping the stats, it marks the Tx queue entry as
211empty by incrementing the dirty_tx mark. If at least half of the entries in
212the Rx ring are available the transmit queue is woken up if it was stopped.
213
214IV. Notes
215
216IVb. References
217
218Preliminary VT86C100A manual from http://www.via.com.tw/
219http://www.scyld.com/expert/100mbps.html
220http://www.scyld.com/expert/NWay.html
221ftp://ftp.via.com.tw/public/lan/Products/NIC/VT86C100A/Datasheet/VT86C100A03.pdf
222ftp://ftp.via.com.tw/public/lan/Products/NIC/VT6102/Datasheet/VT6102_021.PDF
223
224
225IVc. Errata
226
227The VT86C100A manual is not reliable information.
228The 3043 chip does not handle unaligned transmit or receive buffers, resulting
229in significant performance degradation for bounce buffer copies on transmit
230and unaligned IP headers on receive.
231The chip does not pad to minimum transmit length.
232
233*/
234
235
236/* This table drives the PCI probe routines. It's mostly boilerplate in all
237 of the drivers, and will likely be provided by some future kernel.
238 Note the matching code -- the first table entry matchs all 56** cards but
239 second only the 1234 card.
240*/
241
242enum rhine_revs {
243 VT86C100A = 0x00,
244 VTunknown0 = 0x20,
245 VT6102 = 0x40,
246 VT8231 = 0x50, /* Integrated MAC */
247 VT8233 = 0x60, /* Integrated MAC */
248 VT8235 = 0x74, /* Integrated MAC */
249 VT8237 = 0x78, /* Integrated MAC */
250 VTunknown1 = 0x7C,
251 VT6105 = 0x80,
252 VT6105_B0 = 0x83,
253 VT6105L = 0x8A,
254 VT6107 = 0x8C,
255 VTunknown2 = 0x8E,
256 VT6105M = 0x90, /* Management adapter */
257};
258
259enum rhine_quirks {
260 rqWOL = 0x0001, /* Wake-On-LAN support */
261 rqForceReset = 0x0002,
262 rq6patterns = 0x0040, /* 6 instead of 4 patterns for WOL */
263 rqStatusWBRace = 0x0080, /* Tx Status Writeback Error possible */
264 rqRhineI = 0x0100, /* See comment below */
265};
266/*
267 * rqRhineI: VT86C100A (aka Rhine-I) uses different bits to enable
268 * MMIO as well as for the collision counter and the Tx FIFO underflow
269 * indicator. In addition, Tx and Rx buffers need to 4 byte aligned.
270 */
271
272/* Beware of PCI posted writes */
273#define IOSYNC do { ioread8(ioaddr + StationAddr); } while (0)
274
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000275static DEFINE_PCI_DEVICE_TABLE(rhine_pci_tbl) = {
Jeff Garzik46009c82006-06-27 09:12:38 -0400276 { 0x1106, 0x3043, PCI_ANY_ID, PCI_ANY_ID, }, /* VT86C100A */
277 { 0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, }, /* VT6102 */
278 { 0x1106, 0x3106, PCI_ANY_ID, PCI_ANY_ID, }, /* 6105{,L,LOM} */
279 { 0x1106, 0x3053, PCI_ANY_ID, PCI_ANY_ID, }, /* VT6105M */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 { } /* terminate list */
281};
282MODULE_DEVICE_TABLE(pci, rhine_pci_tbl);
283
284
285/* Offsets to the device registers. */
286enum register_offsets {
287 StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
Roger Luethi38f49e82010-12-06 00:59:40 +0000288 ChipCmd1=0x09, TQWake=0x0A,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 IntrStatus=0x0C, IntrEnable=0x0E,
290 MulticastFilter0=0x10, MulticastFilter1=0x14,
291 RxRingPtr=0x18, TxRingPtr=0x1C, GFIFOTest=0x54,
Roger Luethi38f49e82010-12-06 00:59:40 +0000292 MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E, PCIBusConfig1=0x6F,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72, MACRegEEcsr=0x74,
294 ConfigA=0x78, ConfigB=0x79, ConfigC=0x7A, ConfigD=0x7B,
295 RxMissed=0x7C, RxCRCErrs=0x7E, MiscCmd=0x81,
296 StickyHW=0x83, IntrStatus2=0x84,
Roger Luethi38f49e82010-12-06 00:59:40 +0000297 CamMask=0x88, CamCon=0x92, CamAddr=0x93,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 WOLcrSet=0xA0, PwcfgSet=0xA1, WOLcgSet=0xA3, WOLcrClr=0xA4,
299 WOLcrClr1=0xA6, WOLcgClr=0xA7,
300 PwrcsrSet=0xA8, PwrcsrSet1=0xA9, PwrcsrClr=0xAC, PwrcsrClr1=0xAD,
301};
302
303/* Bits in ConfigD */
304enum backoff_bits {
305 BackOptional=0x01, BackModify=0x02,
306 BackCaptureEffect=0x04, BackRandom=0x08
307};
308
Roger Luethi38f49e82010-12-06 00:59:40 +0000309/* Bits in the TxConfig (TCR) register */
310enum tcr_bits {
311 TCR_PQEN=0x01,
312 TCR_LB0=0x02, /* loopback[0] */
313 TCR_LB1=0x04, /* loopback[1] */
314 TCR_OFSET=0x08,
315 TCR_RTGOPT=0x10,
316 TCR_RTFT0=0x20,
317 TCR_RTFT1=0x40,
318 TCR_RTSF=0x80,
319};
320
321/* Bits in the CamCon (CAMC) register */
322enum camcon_bits {
323 CAMC_CAMEN=0x01,
324 CAMC_VCAMSL=0x02,
325 CAMC_CAMWR=0x04,
326 CAMC_CAMRD=0x08,
327};
328
329/* Bits in the PCIBusConfig1 (BCR1) register */
330enum bcr1_bits {
331 BCR1_POT0=0x01,
332 BCR1_POT1=0x02,
333 BCR1_POT2=0x04,
334 BCR1_CTFT0=0x08,
335 BCR1_CTFT1=0x10,
336 BCR1_CTSF=0x20,
337 BCR1_TXQNOBK=0x40, /* for VT6105 */
338 BCR1_VIDFR=0x80, /* for VT6105 */
339 BCR1_MED0=0x40, /* for VT6102 */
340 BCR1_MED1=0x80, /* for VT6102 */
341};
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#ifdef USE_MMIO
344/* Registers we check that mmio and reg are the same. */
345static const int mmio_verify_registers[] = {
346 RxConfig, TxConfig, IntrEnable, ConfigA, ConfigB, ConfigC, ConfigD,
347 0
348};
349#endif
350
351/* Bits in the interrupt status/mask registers. */
352enum intr_status_bits {
353 IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
354 IntrTxDone=0x0002, IntrTxError=0x0008, IntrTxUnderrun=0x0210,
355 IntrPCIErr=0x0040,
356 IntrStatsMax=0x0080, IntrRxEarly=0x0100,
357 IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
358 IntrTxAborted=0x2000, IntrLinkChange=0x4000,
359 IntrRxWakeUp=0x8000,
360 IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
361 IntrTxDescRace=0x080000, /* mapped from IntrStatus2 */
362 IntrTxErrSummary=0x082218,
363};
364
365/* Bits in WOLcrSet/WOLcrClr and PwrcsrSet/PwrcsrClr */
366enum wol_bits {
367 WOLucast = 0x10,
368 WOLmagic = 0x20,
369 WOLbmcast = 0x30,
370 WOLlnkon = 0x40,
371 WOLlnkoff = 0x80,
372};
373
374/* The Rx and Tx buffer descriptors. */
375struct rx_desc {
Al Viro53c03f52007-08-23 02:33:30 -0400376 __le32 rx_status;
377 __le32 desc_length; /* Chain flag, Buffer/frame length */
378 __le32 addr;
379 __le32 next_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380};
381struct tx_desc {
Al Viro53c03f52007-08-23 02:33:30 -0400382 __le32 tx_status;
383 __le32 desc_length; /* Chain flag, Tx Config, Frame length */
384 __le32 addr;
385 __le32 next_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386};
387
388/* Initial value for tx_desc.desc_length, Buffer size goes to bits 0-10 */
389#define TXDESC 0x00e08000
390
391enum rx_status_bits {
392 RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F
393};
394
395/* Bits in *_desc.*_status */
396enum desc_status_bits {
397 DescOwn=0x80000000
398};
399
Roger Luethi38f49e82010-12-06 00:59:40 +0000400/* Bits in *_desc.*_length */
401enum desc_length_bits {
402 DescTag=0x00010000
403};
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/* Bits in ChipCmd. */
406enum chip_cmd_bits {
407 CmdInit=0x01, CmdStart=0x02, CmdStop=0x04, CmdRxOn=0x08,
408 CmdTxOn=0x10, Cmd1TxDemand=0x20, CmdRxDemand=0x40,
409 Cmd1EarlyRx=0x01, Cmd1EarlyTx=0x02, Cmd1FDuplex=0x04,
410 Cmd1NoTxPoll=0x08, Cmd1Reset=0x80,
411};
412
413struct rhine_private {
Roger Luethi38f49e82010-12-06 00:59:40 +0000414 /* Bit mask for configured VLAN ids */
415 unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Descriptor rings */
418 struct rx_desc *rx_ring;
419 struct tx_desc *tx_ring;
420 dma_addr_t rx_ring_dma;
421 dma_addr_t tx_ring_dma;
422
423 /* The addresses of receive-in-place skbuffs. */
424 struct sk_buff *rx_skbuff[RX_RING_SIZE];
425 dma_addr_t rx_skbuff_dma[RX_RING_SIZE];
426
427 /* The saved address of a sent-in-place packet/buffer, for later free(). */
428 struct sk_buff *tx_skbuff[TX_RING_SIZE];
429 dma_addr_t tx_skbuff_dma[TX_RING_SIZE];
430
Roger Luethi4be5de22006-04-04 20:49:16 +0200431 /* Tx bounce buffers (Rhine-I only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 unsigned char *tx_buf[TX_RING_SIZE];
433 unsigned char *tx_bufs;
434 dma_addr_t tx_bufs_dma;
435
436 struct pci_dev *pdev;
437 long pioaddr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700438 struct net_device *dev;
439 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 spinlock_t lock;
Jarek Poplawskic0d7a022009-12-23 21:54:29 -0800441 struct work_struct reset_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* Frequently used values: keep some adjacent for cache effect. */
444 u32 quirks;
445 struct rx_desc *rx_head_desc;
446 unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */
447 unsigned int cur_tx, dirty_tx;
448 unsigned int rx_buf_sz; /* Based on MTU+slack. */
449 u8 wolopts;
450
451 u8 tx_thresh, rx_thresh;
452
453 struct mii_if_info mii_if;
454 void __iomem *base;
455};
456
Roger Luethi38f49e82010-12-06 00:59:40 +0000457#define BYTE_REG_BITS_ON(x, p) do { iowrite8((ioread8((p))|(x)), (p)); } while (0)
458#define WORD_REG_BITS_ON(x, p) do { iowrite16((ioread16((p))|(x)), (p)); } while (0)
459#define DWORD_REG_BITS_ON(x, p) do { iowrite32((ioread32((p))|(x)), (p)); } while (0)
460
461#define BYTE_REG_BITS_IS_ON(x, p) (ioread8((p)) & (x))
462#define WORD_REG_BITS_IS_ON(x, p) (ioread16((p)) & (x))
463#define DWORD_REG_BITS_IS_ON(x, p) (ioread32((p)) & (x))
464
465#define BYTE_REG_BITS_OFF(x, p) do { iowrite8(ioread8((p)) & (~(x)), (p)); } while (0)
466#define WORD_REG_BITS_OFF(x, p) do { iowrite16(ioread16((p)) & (~(x)), (p)); } while (0)
467#define DWORD_REG_BITS_OFF(x, p) do { iowrite32(ioread32((p)) & (~(x)), (p)); } while (0)
468
469#define BYTE_REG_BITS_SET(x, m, p) do { iowrite8((ioread8((p)) & (~(m)))|(x), (p)); } while (0)
470#define WORD_REG_BITS_SET(x, m, p) do { iowrite16((ioread16((p)) & (~(m)))|(x), (p)); } while (0)
471#define DWORD_REG_BITS_SET(x, m, p) do { iowrite32((ioread32((p)) & (~(m)))|(x), (p)); } while (0)
472
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static int mdio_read(struct net_device *dev, int phy_id, int location);
475static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
476static int rhine_open(struct net_device *dev);
Jarek Poplawskic0d7a022009-12-23 21:54:29 -0800477static void rhine_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static void rhine_tx_timeout(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000479static netdev_tx_t rhine_start_tx(struct sk_buff *skb,
480 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100481static irqreturn_t rhine_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static void rhine_tx(struct net_device *dev);
Roger Luethi633949a2006-08-14 23:00:17 -0700483static int rhine_rx(struct net_device *dev, int limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static void rhine_error(struct net_device *dev, int intr_status);
485static void rhine_set_rx_mode(struct net_device *dev);
486static struct net_device_stats *rhine_get_stats(struct net_device *dev);
487static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Jeff Garzik7282d492006-09-13 14:30:00 -0400488static const struct ethtool_ops netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static int rhine_close(struct net_device *dev);
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -0700490static void rhine_shutdown (struct pci_dev *pdev);
Jiri Pirko8e586132011-12-08 19:52:37 -0500491static int rhine_vlan_rx_add_vid(struct net_device *dev, unsigned short vid);
492static int rhine_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid);
Roger Luethi38f49e82010-12-06 00:59:40 +0000493static void rhine_set_cam(void __iomem *ioaddr, int idx, u8 *addr);
494static void rhine_set_vlan_cam(void __iomem *ioaddr, int idx, u8 *addr);
495static void rhine_set_cam_mask(void __iomem *ioaddr, u32 mask);
496static void rhine_set_vlan_cam_mask(void __iomem *ioaddr, u32 mask);
497static void rhine_init_cam_filter(struct net_device *dev);
498static void rhine_update_vcam(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Joe Perchesdf4511f2011-04-16 14:15:25 +0000500#define RHINE_WAIT_FOR(condition) \
501do { \
502 int i = 1024; \
503 while (!(condition) && --i) \
504 ; \
505 if (debug > 1 && i < 512) \
506 pr_info("%4d cycles used @ %s:%d\n", \
507 1024 - i, __func__, __LINE__); \
508} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510static inline u32 get_intr_status(struct net_device *dev)
511{
512 struct rhine_private *rp = netdev_priv(dev);
513 void __iomem *ioaddr = rp->base;
514 u32 intr_status;
515
516 intr_status = ioread16(ioaddr + IntrStatus);
517 /* On Rhine-II, Bit 3 indicates Tx descriptor write-back race. */
518 if (rp->quirks & rqStatusWBRace)
519 intr_status |= ioread8(ioaddr + IntrStatus2) << 16;
520 return intr_status;
521}
522
523/*
524 * Get power related registers into sane state.
525 * Notify user about past WOL event.
526 */
527static void rhine_power_init(struct net_device *dev)
528{
529 struct rhine_private *rp = netdev_priv(dev);
530 void __iomem *ioaddr = rp->base;
531 u16 wolstat;
532
533 if (rp->quirks & rqWOL) {
534 /* Make sure chip is in power state D0 */
535 iowrite8(ioread8(ioaddr + StickyHW) & 0xFC, ioaddr + StickyHW);
536
537 /* Disable "force PME-enable" */
538 iowrite8(0x80, ioaddr + WOLcgClr);
539
540 /* Clear power-event config bits (WOL) */
541 iowrite8(0xFF, ioaddr + WOLcrClr);
542 /* More recent cards can manage two additional patterns */
543 if (rp->quirks & rq6patterns)
544 iowrite8(0x03, ioaddr + WOLcrClr1);
545
546 /* Save power-event status bits */
547 wolstat = ioread8(ioaddr + PwrcsrSet);
548 if (rp->quirks & rq6patterns)
549 wolstat |= (ioread8(ioaddr + PwrcsrSet1) & 0x03) << 8;
550
551 /* Clear power-event status bits */
552 iowrite8(0xFF, ioaddr + PwrcsrClr);
553 if (rp->quirks & rq6patterns)
554 iowrite8(0x03, ioaddr + PwrcsrClr1);
555
556 if (wolstat) {
557 char *reason;
558 switch (wolstat) {
559 case WOLmagic:
560 reason = "Magic packet";
561 break;
562 case WOLlnkon:
563 reason = "Link went up";
564 break;
565 case WOLlnkoff:
566 reason = "Link went down";
567 break;
568 case WOLucast:
569 reason = "Unicast packet";
570 break;
571 case WOLbmcast:
572 reason = "Multicast/broadcast packet";
573 break;
574 default:
575 reason = "Unknown";
576 }
Joe Perchesdf4511f2011-04-16 14:15:25 +0000577 netdev_info(dev, "Woke system up. Reason: %s\n",
578 reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 }
581}
582
583static void rhine_chip_reset(struct net_device *dev)
584{
585 struct rhine_private *rp = netdev_priv(dev);
586 void __iomem *ioaddr = rp->base;
587
588 iowrite8(Cmd1Reset, ioaddr + ChipCmd1);
589 IOSYNC;
590
591 if (ioread8(ioaddr + ChipCmd1) & Cmd1Reset) {
Joe Perchesdf4511f2011-04-16 14:15:25 +0000592 netdev_info(dev, "Reset not complete yet. Trying harder.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Force reset */
595 if (rp->quirks & rqForceReset)
596 iowrite8(0x40, ioaddr + MiscCmd);
597
598 /* Reset can take somewhat longer (rare) */
599 RHINE_WAIT_FOR(!(ioread8(ioaddr + ChipCmd1) & Cmd1Reset));
600 }
601
602 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +0000603 netdev_info(dev, "Reset %s\n",
604 (ioread8(ioaddr + ChipCmd1) & Cmd1Reset) ?
605 "failed" : "succeeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608#ifdef USE_MMIO
609static void enable_mmio(long pioaddr, u32 quirks)
610{
611 int n;
612 if (quirks & rqRhineI) {
613 /* More recent docs say that this bit is reserved ... */
614 n = inb(pioaddr + ConfigA) | 0x20;
615 outb(n, pioaddr + ConfigA);
616 } else {
617 n = inb(pioaddr + ConfigD) | 0x80;
618 outb(n, pioaddr + ConfigD);
619 }
620}
621#endif
622
623/*
624 * Loads bytes 0x00-0x05, 0x6E-0x6F, 0x78-0x7B from EEPROM
625 * (plus 0x6C for Rhine-I/II)
626 */
627static void __devinit rhine_reload_eeprom(long pioaddr, struct net_device *dev)
628{
629 struct rhine_private *rp = netdev_priv(dev);
630 void __iomem *ioaddr = rp->base;
631
632 outb(0x20, pioaddr + MACRegEEcsr);
633 RHINE_WAIT_FOR(!(inb(pioaddr + MACRegEEcsr) & 0x20));
634
635#ifdef USE_MMIO
636 /*
637 * Reloading from EEPROM overwrites ConfigA-D, so we must re-enable
638 * MMIO. If reloading EEPROM was done first this could be avoided, but
639 * it is not known if that still works with the "win98-reboot" problem.
640 */
641 enable_mmio(pioaddr, rp->quirks);
642#endif
643
644 /* Turn off EEPROM-controlled wake-up (magic packet) */
645 if (rp->quirks & rqWOL)
646 iowrite8(ioread8(ioaddr + ConfigA) & 0xFC, ioaddr + ConfigA);
647
648}
649
650#ifdef CONFIG_NET_POLL_CONTROLLER
651static void rhine_poll(struct net_device *dev)
652{
653 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +0100654 rhine_interrupt(dev->irq, (void *)dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 enable_irq(dev->irq);
656}
657#endif
658
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700659static int rhine_napipoll(struct napi_struct *napi, int budget)
Roger Luethi633949a2006-08-14 23:00:17 -0700660{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700661 struct rhine_private *rp = container_of(napi, struct rhine_private, napi);
662 struct net_device *dev = rp->dev;
Roger Luethi633949a2006-08-14 23:00:17 -0700663 void __iomem *ioaddr = rp->base;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700664 int work_done;
Roger Luethi633949a2006-08-14 23:00:17 -0700665
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700666 work_done = rhine_rx(dev, budget);
Roger Luethi633949a2006-08-14 23:00:17 -0700667
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700668 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800669 napi_complete(napi);
Roger Luethi633949a2006-08-14 23:00:17 -0700670
671 iowrite16(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow |
672 IntrRxDropped | IntrRxNoBuf | IntrTxAborted |
673 IntrTxDone | IntrTxError | IntrTxUnderrun |
674 IntrPCIErr | IntrStatsMax | IntrLinkChange,
675 ioaddr + IntrEnable);
Roger Luethi633949a2006-08-14 23:00:17 -0700676 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700677 return work_done;
Roger Luethi633949a2006-08-14 23:00:17 -0700678}
Roger Luethi633949a2006-08-14 23:00:17 -0700679
Adrian Bunkde4e7c82008-01-30 22:02:05 +0200680static void __devinit rhine_hw_init(struct net_device *dev, long pioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct rhine_private *rp = netdev_priv(dev);
683
684 /* Reset the chip to erase previous misconfiguration. */
685 rhine_chip_reset(dev);
686
687 /* Rhine-I needs extra time to recuperate before EEPROM reload */
688 if (rp->quirks & rqRhineI)
689 msleep(5);
690
691 /* Reload EEPROM controlled bytes cleared by soft reset */
692 rhine_reload_eeprom(pioaddr, dev);
693}
694
Stephen Hemminger5d1d07d2008-11-21 17:30:11 -0800695static const struct net_device_ops rhine_netdev_ops = {
696 .ndo_open = rhine_open,
697 .ndo_stop = rhine_close,
698 .ndo_start_xmit = rhine_start_tx,
699 .ndo_get_stats = rhine_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000700 .ndo_set_rx_mode = rhine_set_rx_mode,
Ben Hutchings635ecaa2009-07-09 17:59:01 +0000701 .ndo_change_mtu = eth_change_mtu,
Stephen Hemminger5d1d07d2008-11-21 17:30:11 -0800702 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerfe96aaa2009-01-09 11:13:14 +0000703 .ndo_set_mac_address = eth_mac_addr,
Stephen Hemminger5d1d07d2008-11-21 17:30:11 -0800704 .ndo_do_ioctl = netdev_ioctl,
705 .ndo_tx_timeout = rhine_tx_timeout,
Roger Luethi38f49e82010-12-06 00:59:40 +0000706 .ndo_vlan_rx_add_vid = rhine_vlan_rx_add_vid,
707 .ndo_vlan_rx_kill_vid = rhine_vlan_rx_kill_vid,
Stephen Hemminger5d1d07d2008-11-21 17:30:11 -0800708#ifdef CONFIG_NET_POLL_CONTROLLER
709 .ndo_poll_controller = rhine_poll,
710#endif
711};
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static int __devinit rhine_init_one(struct pci_dev *pdev,
714 const struct pci_device_id *ent)
715{
716 struct net_device *dev;
717 struct rhine_private *rp;
718 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 u32 quirks;
720 long pioaddr;
721 long memaddr;
722 void __iomem *ioaddr;
723 int io_size, phy_id;
724 const char *name;
725#ifdef USE_MMIO
726 int bar = 1;
727#else
728 int bar = 0;
729#endif
730
731/* when built into the kernel, we only print version if device is found */
732#ifndef MODULE
Joe Perchesdf4511f2011-04-16 14:15:25 +0000733 pr_info_once("%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#endif
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 io_size = 256;
737 phy_id = 0;
738 quirks = 0;
739 name = "Rhine";
Auke Kok44c10132007-06-08 15:46:36 -0700740 if (pdev->revision < VTunknown0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 quirks = rqRhineI;
742 io_size = 128;
743 }
Auke Kok44c10132007-06-08 15:46:36 -0700744 else if (pdev->revision >= VT6102) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 quirks = rqWOL | rqForceReset;
Auke Kok44c10132007-06-08 15:46:36 -0700746 if (pdev->revision < VT6105) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 name = "Rhine II";
748 quirks |= rqStatusWBRace; /* Rhine-II exclusive */
749 }
750 else {
751 phy_id = 1; /* Integrated PHY, phy_id fixed to 1 */
Auke Kok44c10132007-06-08 15:46:36 -0700752 if (pdev->revision >= VT6105_B0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 quirks |= rq6patterns;
Auke Kok44c10132007-06-08 15:46:36 -0700754 if (pdev->revision < VT6105M)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 name = "Rhine III";
756 else
757 name = "Rhine III (Management Adapter)";
758 }
759 }
760
761 rc = pci_enable_device(pdev);
762 if (rc)
763 goto err_out;
764
765 /* this should always be supported */
Yang Hongyang284901a2009-04-06 19:01:15 -0700766 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (rc) {
Joe Perchesdf4511f2011-04-16 14:15:25 +0000768 dev_err(&pdev->dev,
769 "32-bit PCI DMA addresses not supported by the card!?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto err_out;
771 }
772
773 /* sanity check */
774 if ((pci_resource_len(pdev, 0) < io_size) ||
775 (pci_resource_len(pdev, 1) < io_size)) {
776 rc = -EIO;
Joe Perchesdf4511f2011-04-16 14:15:25 +0000777 dev_err(&pdev->dev, "Insufficient PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto err_out;
779 }
780
781 pioaddr = pci_resource_start(pdev, 0);
782 memaddr = pci_resource_start(pdev, 1);
783
784 pci_set_master(pdev);
785
786 dev = alloc_etherdev(sizeof(struct rhine_private));
787 if (!dev) {
788 rc = -ENOMEM;
Joe Perchesdf4511f2011-04-16 14:15:25 +0000789 dev_err(&pdev->dev, "alloc_etherdev failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto err_out;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 SET_NETDEV_DEV(dev, &pdev->dev);
793
794 rp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700795 rp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 rp->quirks = quirks;
797 rp->pioaddr = pioaddr;
798 rp->pdev = pdev;
799
800 rc = pci_request_regions(pdev, DRV_NAME);
801 if (rc)
802 goto err_out_free_netdev;
803
804 ioaddr = pci_iomap(pdev, bar, io_size);
805 if (!ioaddr) {
806 rc = -EIO;
Joe Perchesdf4511f2011-04-16 14:15:25 +0000807 dev_err(&pdev->dev,
808 "ioremap failed for device %s, region 0x%X @ 0x%lX\n",
809 pci_name(pdev), io_size, memaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto err_out_free_res;
811 }
812
813#ifdef USE_MMIO
814 enable_mmio(pioaddr, quirks);
815
816 /* Check that selected MMIO registers match the PIO ones */
817 i = 0;
818 while (mmio_verify_registers[i]) {
819 int reg = mmio_verify_registers[i++];
820 unsigned char a = inb(pioaddr+reg);
821 unsigned char b = readb(ioaddr+reg);
822 if (a != b) {
823 rc = -EIO;
Joe Perchesdf4511f2011-04-16 14:15:25 +0000824 dev_err(&pdev->dev,
825 "MMIO do not match PIO [%02x] (%02x != %02x)\n",
826 reg, a, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto err_out_unmap;
828 }
829 }
830#endif /* USE_MMIO */
831
832 dev->base_addr = (unsigned long)ioaddr;
833 rp->base = ioaddr;
834
835 /* Get chip registers into a sane state */
836 rhine_power_init(dev);
837 rhine_hw_init(dev, pioaddr);
838
839 for (i = 0; i < 6; i++)
840 dev->dev_addr[i] = ioread8(ioaddr + StationAddr + i);
841
Joe Perches482e3fe2011-04-16 14:15:26 +0000842 if (!is_valid_ether_addr(dev->dev_addr)) {
843 /* Report it and use a random ethernet address instead */
844 netdev_err(dev, "Invalid MAC address: %pM\n", dev->dev_addr);
845 random_ether_addr(dev->dev_addr);
846 netdev_info(dev, "Using random MAC address: %pM\n",
847 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Joe Perches482e3fe2011-04-16 14:15:26 +0000849 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* For Rhine-I/II, phy_id is loaded from EEPROM */
852 if (!phy_id)
853 phy_id = ioread8(ioaddr + 0x6C);
854
855 dev->irq = pdev->irq;
856
857 spin_lock_init(&rp->lock);
Jarek Poplawskic0d7a022009-12-23 21:54:29 -0800858 INIT_WORK(&rp->reset_task, rhine_reset_task);
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 rp->mii_if.dev = dev;
861 rp->mii_if.mdio_read = mdio_read;
862 rp->mii_if.mdio_write = mdio_write;
863 rp->mii_if.phy_id_mask = 0x1f;
864 rp->mii_if.reg_num_mask = 0x1f;
865
866 /* The chip-specific entries in the device structure. */
Stephen Hemminger5d1d07d2008-11-21 17:30:11 -0800867 dev->netdev_ops = &rhine_netdev_ops;
868 dev->ethtool_ops = &netdev_ethtool_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemminger5d1d07d2008-11-21 17:30:11 -0800870
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700871 netif_napi_add(dev, &rp->napi, rhine_napipoll, 64);
Francois Romieu32b0f532008-07-11 00:30:14 +0200872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (rp->quirks & rqRhineI)
874 dev->features |= NETIF_F_SG|NETIF_F_HW_CSUM;
875
Roger Luethi38f49e82010-12-06 00:59:40 +0000876 if (pdev->revision >= VT6105M)
877 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
878 NETIF_F_HW_VLAN_FILTER;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* dev->name not defined before register_netdev()! */
881 rc = register_netdev(dev);
882 if (rc)
883 goto err_out_unmap;
884
Joe Perchesdf4511f2011-04-16 14:15:25 +0000885 netdev_info(dev, "VIA %s at 0x%lx, %pM, IRQ %d\n",
886 name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#ifdef USE_MMIO
Joe Perchesdf4511f2011-04-16 14:15:25 +0000888 memaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#else
Joe Perchesdf4511f2011-04-16 14:15:25 +0000890 (long)ioaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#endif
Joe Perchesdf4511f2011-04-16 14:15:25 +0000892 dev->dev_addr, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 pci_set_drvdata(pdev, dev);
895
896 {
897 u16 mii_cmd;
898 int mii_status = mdio_read(dev, phy_id, 1);
899 mii_cmd = mdio_read(dev, phy_id, MII_BMCR) & ~BMCR_ISOLATE;
900 mdio_write(dev, phy_id, MII_BMCR, mii_cmd);
901 if (mii_status != 0xffff && mii_status != 0x0000) {
902 rp->mii_if.advertising = mdio_read(dev, phy_id, 4);
Joe Perchesdf4511f2011-04-16 14:15:25 +0000903 netdev_info(dev,
904 "MII PHY found at address %d, status 0x%04x advertising %04x Link %04x\n",
905 phy_id,
906 mii_status, rp->mii_if.advertising,
907 mdio_read(dev, phy_id, 5));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* set IFF_RUNNING */
910 if (mii_status & BMSR_LSTATUS)
911 netif_carrier_on(dev);
912 else
913 netif_carrier_off(dev);
914
915 }
916 }
917 rp->mii_if.phy_id = phy_id;
Roger Luethib933b4d2006-08-14 23:00:21 -0700918 if (debug > 1 && avoid_D3)
Joe Perchesdf4511f2011-04-16 14:15:25 +0000919 netdev_info(dev, "No D3 power state at shutdown\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return 0;
922
923err_out_unmap:
924 pci_iounmap(pdev, ioaddr);
925err_out_free_res:
926 pci_release_regions(pdev);
927err_out_free_netdev:
928 free_netdev(dev);
929err_out:
930 return rc;
931}
932
933static int alloc_ring(struct net_device* dev)
934{
935 struct rhine_private *rp = netdev_priv(dev);
936 void *ring;
937 dma_addr_t ring_dma;
938
939 ring = pci_alloc_consistent(rp->pdev,
940 RX_RING_SIZE * sizeof(struct rx_desc) +
941 TX_RING_SIZE * sizeof(struct tx_desc),
942 &ring_dma);
943 if (!ring) {
Joe Perchesdf4511f2011-04-16 14:15:25 +0000944 netdev_err(dev, "Could not allocate DMA memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -ENOMEM;
946 }
947 if (rp->quirks & rqRhineI) {
948 rp->tx_bufs = pci_alloc_consistent(rp->pdev,
949 PKT_BUF_SZ * TX_RING_SIZE,
950 &rp->tx_bufs_dma);
951 if (rp->tx_bufs == NULL) {
952 pci_free_consistent(rp->pdev,
953 RX_RING_SIZE * sizeof(struct rx_desc) +
954 TX_RING_SIZE * sizeof(struct tx_desc),
955 ring, ring_dma);
956 return -ENOMEM;
957 }
958 }
959
960 rp->rx_ring = ring;
961 rp->tx_ring = ring + RX_RING_SIZE * sizeof(struct rx_desc);
962 rp->rx_ring_dma = ring_dma;
963 rp->tx_ring_dma = ring_dma + RX_RING_SIZE * sizeof(struct rx_desc);
964
965 return 0;
966}
967
968static void free_ring(struct net_device* dev)
969{
970 struct rhine_private *rp = netdev_priv(dev);
971
972 pci_free_consistent(rp->pdev,
973 RX_RING_SIZE * sizeof(struct rx_desc) +
974 TX_RING_SIZE * sizeof(struct tx_desc),
975 rp->rx_ring, rp->rx_ring_dma);
976 rp->tx_ring = NULL;
977
978 if (rp->tx_bufs)
979 pci_free_consistent(rp->pdev, PKT_BUF_SZ * TX_RING_SIZE,
980 rp->tx_bufs, rp->tx_bufs_dma);
981
982 rp->tx_bufs = NULL;
983
984}
985
986static void alloc_rbufs(struct net_device *dev)
987{
988 struct rhine_private *rp = netdev_priv(dev);
989 dma_addr_t next;
990 int i;
991
992 rp->dirty_rx = rp->cur_rx = 0;
993
994 rp->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
995 rp->rx_head_desc = &rp->rx_ring[0];
996 next = rp->rx_ring_dma;
997
998 /* Init the ring entries */
999 for (i = 0; i < RX_RING_SIZE; i++) {
1000 rp->rx_ring[i].rx_status = 0;
1001 rp->rx_ring[i].desc_length = cpu_to_le32(rp->rx_buf_sz);
1002 next += sizeof(struct rx_desc);
1003 rp->rx_ring[i].next_desc = cpu_to_le32(next);
1004 rp->rx_skbuff[i] = NULL;
1005 }
1006 /* Mark the last entry as wrapping the ring. */
1007 rp->rx_ring[i-1].next_desc = cpu_to_le32(rp->rx_ring_dma);
1008
1009 /* Fill in the Rx buffers. Handle allocation failure gracefully. */
1010 for (i = 0; i < RX_RING_SIZE; i++) {
Kevin Lob26b5552008-08-27 11:35:09 +08001011 struct sk_buff *skb = netdev_alloc_skb(dev, rp->rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 rp->rx_skbuff[i] = skb;
1013 if (skb == NULL)
1014 break;
1015 skb->dev = dev; /* Mark as being used by this device. */
1016
1017 rp->rx_skbuff_dma[i] =
David S. Miller689be432005-06-28 15:25:31 -07001018 pci_map_single(rp->pdev, skb->data, rp->rx_buf_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 PCI_DMA_FROMDEVICE);
1020
1021 rp->rx_ring[i].addr = cpu_to_le32(rp->rx_skbuff_dma[i]);
1022 rp->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
1023 }
1024 rp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
1025}
1026
1027static void free_rbufs(struct net_device* dev)
1028{
1029 struct rhine_private *rp = netdev_priv(dev);
1030 int i;
1031
1032 /* Free all the skbuffs in the Rx queue. */
1033 for (i = 0; i < RX_RING_SIZE; i++) {
1034 rp->rx_ring[i].rx_status = 0;
1035 rp->rx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
1036 if (rp->rx_skbuff[i]) {
1037 pci_unmap_single(rp->pdev,
1038 rp->rx_skbuff_dma[i],
1039 rp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1040 dev_kfree_skb(rp->rx_skbuff[i]);
1041 }
1042 rp->rx_skbuff[i] = NULL;
1043 }
1044}
1045
1046static void alloc_tbufs(struct net_device* dev)
1047{
1048 struct rhine_private *rp = netdev_priv(dev);
1049 dma_addr_t next;
1050 int i;
1051
1052 rp->dirty_tx = rp->cur_tx = 0;
1053 next = rp->tx_ring_dma;
1054 for (i = 0; i < TX_RING_SIZE; i++) {
1055 rp->tx_skbuff[i] = NULL;
1056 rp->tx_ring[i].tx_status = 0;
1057 rp->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1058 next += sizeof(struct tx_desc);
1059 rp->tx_ring[i].next_desc = cpu_to_le32(next);
Roger Luethi4be5de22006-04-04 20:49:16 +02001060 if (rp->quirks & rqRhineI)
1061 rp->tx_buf[i] = &rp->tx_bufs[i * PKT_BUF_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 rp->tx_ring[i-1].next_desc = cpu_to_le32(rp->tx_ring_dma);
1064
1065}
1066
1067static void free_tbufs(struct net_device* dev)
1068{
1069 struct rhine_private *rp = netdev_priv(dev);
1070 int i;
1071
1072 for (i = 0; i < TX_RING_SIZE; i++) {
1073 rp->tx_ring[i].tx_status = 0;
1074 rp->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1075 rp->tx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
1076 if (rp->tx_skbuff[i]) {
1077 if (rp->tx_skbuff_dma[i]) {
1078 pci_unmap_single(rp->pdev,
1079 rp->tx_skbuff_dma[i],
1080 rp->tx_skbuff[i]->len,
1081 PCI_DMA_TODEVICE);
1082 }
1083 dev_kfree_skb(rp->tx_skbuff[i]);
1084 }
1085 rp->tx_skbuff[i] = NULL;
1086 rp->tx_buf[i] = NULL;
1087 }
1088}
1089
1090static void rhine_check_media(struct net_device *dev, unsigned int init_media)
1091{
1092 struct rhine_private *rp = netdev_priv(dev);
1093 void __iomem *ioaddr = rp->base;
1094
1095 mii_check_media(&rp->mii_if, debug, init_media);
1096
1097 if (rp->mii_if.full_duplex)
1098 iowrite8(ioread8(ioaddr + ChipCmd1) | Cmd1FDuplex,
1099 ioaddr + ChipCmd1);
1100 else
1101 iowrite8(ioread8(ioaddr + ChipCmd1) & ~Cmd1FDuplex,
1102 ioaddr + ChipCmd1);
Roger Luethi00b428c2006-03-28 20:53:56 +02001103 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001104 netdev_info(dev, "force_media %d, carrier %d\n",
1105 rp->mii_if.force_media, netif_carrier_ok(dev));
Roger Luethi00b428c2006-03-28 20:53:56 +02001106}
1107
1108/* Called after status of force_media possibly changed */
Adrian Bunk0761be42006-04-10 23:22:21 -07001109static void rhine_set_carrier(struct mii_if_info *mii)
Roger Luethi00b428c2006-03-28 20:53:56 +02001110{
1111 if (mii->force_media) {
1112 /* autoneg is off: Link is always assumed to be up */
1113 if (!netif_carrier_ok(mii->dev))
1114 netif_carrier_on(mii->dev);
1115 }
1116 else /* Let MMI library update carrier status */
1117 rhine_check_media(mii->dev, 0);
1118 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001119 netdev_info(mii->dev, "force_media %d, carrier %d\n",
1120 mii->force_media, netif_carrier_ok(mii->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Roger Luethi38f49e82010-12-06 00:59:40 +00001123/**
1124 * rhine_set_cam - set CAM multicast filters
1125 * @ioaddr: register block of this Rhine
1126 * @idx: multicast CAM index [0..MCAM_SIZE-1]
1127 * @addr: multicast address (6 bytes)
1128 *
1129 * Load addresses into multicast filters.
1130 */
1131static void rhine_set_cam(void __iomem *ioaddr, int idx, u8 *addr)
1132{
1133 int i;
1134
1135 iowrite8(CAMC_CAMEN, ioaddr + CamCon);
1136 wmb();
1137
1138 /* Paranoid -- idx out of range should never happen */
1139 idx &= (MCAM_SIZE - 1);
1140
1141 iowrite8((u8) idx, ioaddr + CamAddr);
1142
1143 for (i = 0; i < 6; i++, addr++)
1144 iowrite8(*addr, ioaddr + MulticastFilter0 + i);
1145 udelay(10);
1146 wmb();
1147
1148 iowrite8(CAMC_CAMWR | CAMC_CAMEN, ioaddr + CamCon);
1149 udelay(10);
1150
1151 iowrite8(0, ioaddr + CamCon);
1152}
1153
1154/**
1155 * rhine_set_vlan_cam - set CAM VLAN filters
1156 * @ioaddr: register block of this Rhine
1157 * @idx: VLAN CAM index [0..VCAM_SIZE-1]
1158 * @addr: VLAN ID (2 bytes)
1159 *
1160 * Load addresses into VLAN filters.
1161 */
1162static void rhine_set_vlan_cam(void __iomem *ioaddr, int idx, u8 *addr)
1163{
1164 iowrite8(CAMC_CAMEN | CAMC_VCAMSL, ioaddr + CamCon);
1165 wmb();
1166
1167 /* Paranoid -- idx out of range should never happen */
1168 idx &= (VCAM_SIZE - 1);
1169
1170 iowrite8((u8) idx, ioaddr + CamAddr);
1171
1172 iowrite16(*((u16 *) addr), ioaddr + MulticastFilter0 + 6);
1173 udelay(10);
1174 wmb();
1175
1176 iowrite8(CAMC_CAMWR | CAMC_CAMEN, ioaddr + CamCon);
1177 udelay(10);
1178
1179 iowrite8(0, ioaddr + CamCon);
1180}
1181
1182/**
1183 * rhine_set_cam_mask - set multicast CAM mask
1184 * @ioaddr: register block of this Rhine
1185 * @mask: multicast CAM mask
1186 *
1187 * Mask sets multicast filters active/inactive.
1188 */
1189static void rhine_set_cam_mask(void __iomem *ioaddr, u32 mask)
1190{
1191 iowrite8(CAMC_CAMEN, ioaddr + CamCon);
1192 wmb();
1193
1194 /* write mask */
1195 iowrite32(mask, ioaddr + CamMask);
1196
1197 /* disable CAMEN */
1198 iowrite8(0, ioaddr + CamCon);
1199}
1200
1201/**
1202 * rhine_set_vlan_cam_mask - set VLAN CAM mask
1203 * @ioaddr: register block of this Rhine
1204 * @mask: VLAN CAM mask
1205 *
1206 * Mask sets VLAN filters active/inactive.
1207 */
1208static void rhine_set_vlan_cam_mask(void __iomem *ioaddr, u32 mask)
1209{
1210 iowrite8(CAMC_CAMEN | CAMC_VCAMSL, ioaddr + CamCon);
1211 wmb();
1212
1213 /* write mask */
1214 iowrite32(mask, ioaddr + CamMask);
1215
1216 /* disable CAMEN */
1217 iowrite8(0, ioaddr + CamCon);
1218}
1219
1220/**
1221 * rhine_init_cam_filter - initialize CAM filters
1222 * @dev: network device
1223 *
1224 * Initialize (disable) hardware VLAN and multicast support on this
1225 * Rhine.
1226 */
1227static void rhine_init_cam_filter(struct net_device *dev)
1228{
1229 struct rhine_private *rp = netdev_priv(dev);
1230 void __iomem *ioaddr = rp->base;
1231
1232 /* Disable all CAMs */
1233 rhine_set_vlan_cam_mask(ioaddr, 0);
1234 rhine_set_cam_mask(ioaddr, 0);
1235
1236 /* disable hardware VLAN support */
1237 BYTE_REG_BITS_ON(TCR_PQEN, ioaddr + TxConfig);
1238 BYTE_REG_BITS_OFF(BCR1_VIDFR, ioaddr + PCIBusConfig1);
1239}
1240
1241/**
1242 * rhine_update_vcam - update VLAN CAM filters
1243 * @rp: rhine_private data of this Rhine
1244 *
1245 * Update VLAN CAM filters to match configuration change.
1246 */
1247static void rhine_update_vcam(struct net_device *dev)
1248{
1249 struct rhine_private *rp = netdev_priv(dev);
1250 void __iomem *ioaddr = rp->base;
1251 u16 vid;
1252 u32 vCAMmask = 0; /* 32 vCAMs (6105M and better) */
1253 unsigned int i = 0;
1254
1255 for_each_set_bit(vid, rp->active_vlans, VLAN_N_VID) {
1256 rhine_set_vlan_cam(ioaddr, i, (u8 *)&vid);
1257 vCAMmask |= 1 << i;
1258 if (++i >= VCAM_SIZE)
1259 break;
1260 }
1261 rhine_set_vlan_cam_mask(ioaddr, vCAMmask);
1262}
1263
Jiri Pirko8e586132011-12-08 19:52:37 -05001264static int rhine_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
Roger Luethi38f49e82010-12-06 00:59:40 +00001265{
1266 struct rhine_private *rp = netdev_priv(dev);
1267
1268 spin_lock_irq(&rp->lock);
1269 set_bit(vid, rp->active_vlans);
1270 rhine_update_vcam(dev);
1271 spin_unlock_irq(&rp->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001272 return 0;
Roger Luethi38f49e82010-12-06 00:59:40 +00001273}
1274
Jiri Pirko8e586132011-12-08 19:52:37 -05001275static int rhine_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
Roger Luethi38f49e82010-12-06 00:59:40 +00001276{
1277 struct rhine_private *rp = netdev_priv(dev);
1278
1279 spin_lock_irq(&rp->lock);
1280 clear_bit(vid, rp->active_vlans);
1281 rhine_update_vcam(dev);
1282 spin_unlock_irq(&rp->lock);
Jiri Pirko8e586132011-12-08 19:52:37 -05001283 return 0;
Roger Luethi38f49e82010-12-06 00:59:40 +00001284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286static void init_registers(struct net_device *dev)
1287{
1288 struct rhine_private *rp = netdev_priv(dev);
1289 void __iomem *ioaddr = rp->base;
1290 int i;
1291
1292 for (i = 0; i < 6; i++)
1293 iowrite8(dev->dev_addr[i], ioaddr + StationAddr + i);
1294
1295 /* Initialize other registers. */
1296 iowrite16(0x0006, ioaddr + PCIBusConfig); /* Tune configuration??? */
1297 /* Configure initial FIFO thresholds. */
1298 iowrite8(0x20, ioaddr + TxConfig);
1299 rp->tx_thresh = 0x20;
1300 rp->rx_thresh = 0x60; /* Written in rhine_set_rx_mode(). */
1301
1302 iowrite32(rp->rx_ring_dma, ioaddr + RxRingPtr);
1303 iowrite32(rp->tx_ring_dma, ioaddr + TxRingPtr);
1304
1305 rhine_set_rx_mode(dev);
1306
Roger Luethi38f49e82010-12-06 00:59:40 +00001307 if (rp->pdev->revision >= VT6105M)
1308 rhine_init_cam_filter(dev);
1309
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001310 napi_enable(&rp->napi);
Stephen Hemmingerab197662006-08-14 23:00:18 -07001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /* Enable interrupts by setting the interrupt mask. */
1313 iowrite16(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow |
1314 IntrRxDropped | IntrRxNoBuf | IntrTxAborted |
1315 IntrTxDone | IntrTxError | IntrTxUnderrun |
1316 IntrPCIErr | IntrStatsMax | IntrLinkChange,
1317 ioaddr + IntrEnable);
1318
1319 iowrite16(CmdStart | CmdTxOn | CmdRxOn | (Cmd1NoTxPoll << 8),
1320 ioaddr + ChipCmd);
1321 rhine_check_media(dev, 1);
1322}
1323
1324/* Enable MII link status auto-polling (required for IntrLinkChange) */
1325static void rhine_enable_linkmon(void __iomem *ioaddr)
1326{
1327 iowrite8(0, ioaddr + MIICmd);
1328 iowrite8(MII_BMSR, ioaddr + MIIRegAddr);
1329 iowrite8(0x80, ioaddr + MIICmd);
1330
1331 RHINE_WAIT_FOR((ioread8(ioaddr + MIIRegAddr) & 0x20));
1332
1333 iowrite8(MII_BMSR | 0x40, ioaddr + MIIRegAddr);
1334}
1335
1336/* Disable MII link status auto-polling (required for MDIO access) */
1337static void rhine_disable_linkmon(void __iomem *ioaddr, u32 quirks)
1338{
1339 iowrite8(0, ioaddr + MIICmd);
1340
1341 if (quirks & rqRhineI) {
1342 iowrite8(0x01, ioaddr + MIIRegAddr); // MII_BMSR
1343
John W. Linville38bb6b22006-05-19 10:51:21 -04001344 /* Can be called from ISR. Evil. */
1345 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /* 0x80 must be set immediately before turning it off */
1348 iowrite8(0x80, ioaddr + MIICmd);
1349
1350 RHINE_WAIT_FOR(ioread8(ioaddr + MIIRegAddr) & 0x20);
1351
1352 /* Heh. Now clear 0x80 again. */
1353 iowrite8(0, ioaddr + MIICmd);
1354 }
1355 else
1356 RHINE_WAIT_FOR(ioread8(ioaddr + MIIRegAddr) & 0x80);
1357}
1358
1359/* Read and write over the MII Management Data I/O (MDIO) interface. */
1360
1361static int mdio_read(struct net_device *dev, int phy_id, int regnum)
1362{
1363 struct rhine_private *rp = netdev_priv(dev);
1364 void __iomem *ioaddr = rp->base;
1365 int result;
1366
1367 rhine_disable_linkmon(ioaddr, rp->quirks);
1368
1369 /* rhine_disable_linkmon already cleared MIICmd */
1370 iowrite8(phy_id, ioaddr + MIIPhyAddr);
1371 iowrite8(regnum, ioaddr + MIIRegAddr);
1372 iowrite8(0x40, ioaddr + MIICmd); /* Trigger read */
1373 RHINE_WAIT_FOR(!(ioread8(ioaddr + MIICmd) & 0x40));
1374 result = ioread16(ioaddr + MIIData);
1375
1376 rhine_enable_linkmon(ioaddr);
1377 return result;
1378}
1379
1380static void mdio_write(struct net_device *dev, int phy_id, int regnum, int value)
1381{
1382 struct rhine_private *rp = netdev_priv(dev);
1383 void __iomem *ioaddr = rp->base;
1384
1385 rhine_disable_linkmon(ioaddr, rp->quirks);
1386
1387 /* rhine_disable_linkmon already cleared MIICmd */
1388 iowrite8(phy_id, ioaddr + MIIPhyAddr);
1389 iowrite8(regnum, ioaddr + MIIRegAddr);
1390 iowrite16(value, ioaddr + MIIData);
1391 iowrite8(0x20, ioaddr + MIICmd); /* Trigger write */
1392 RHINE_WAIT_FOR(!(ioread8(ioaddr + MIICmd) & 0x20));
1393
1394 rhine_enable_linkmon(ioaddr);
1395}
1396
1397static int rhine_open(struct net_device *dev)
1398{
1399 struct rhine_private *rp = netdev_priv(dev);
1400 void __iomem *ioaddr = rp->base;
1401 int rc;
1402
Julia Lawall76781382009-11-18 08:23:53 +00001403 rc = request_irq(rp->pdev->irq, rhine_interrupt, IRQF_SHARED, dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 dev);
1405 if (rc)
1406 return rc;
1407
1408 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001409 netdev_dbg(dev, "%s() irq %d\n", __func__, rp->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 rc = alloc_ring(dev);
1412 if (rc) {
1413 free_irq(rp->pdev->irq, dev);
1414 return rc;
1415 }
1416 alloc_rbufs(dev);
1417 alloc_tbufs(dev);
1418 rhine_chip_reset(dev);
1419 init_registers(dev);
1420 if (debug > 2)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001421 netdev_dbg(dev, "%s() Done - status %04x MII status: %04x\n",
1422 __func__, ioread16(ioaddr + ChipCmd),
1423 mdio_read(dev, rp->mii_if.phy_id, MII_BMSR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 netif_start_queue(dev);
1426
1427 return 0;
1428}
1429
Jarek Poplawskic0d7a022009-12-23 21:54:29 -08001430static void rhine_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Jarek Poplawskic0d7a022009-12-23 21:54:29 -08001432 struct rhine_private *rp = container_of(work, struct rhine_private,
1433 reset_task);
1434 struct net_device *dev = rp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 /* protect against concurrent rx interrupts */
1437 disable_irq(rp->pdev->irq);
1438
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001439 napi_disable(&rp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001440
Jarek Poplawskic0d7a022009-12-23 21:54:29 -08001441 spin_lock_bh(&rp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /* clear all descriptors */
1444 free_tbufs(dev);
1445 free_rbufs(dev);
1446 alloc_tbufs(dev);
1447 alloc_rbufs(dev);
1448
1449 /* Reinitialize the hardware. */
1450 rhine_chip_reset(dev);
1451 init_registers(dev);
1452
Jarek Poplawskic0d7a022009-12-23 21:54:29 -08001453 spin_unlock_bh(&rp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 enable_irq(rp->pdev->irq);
1455
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001456 dev->trans_start = jiffies; /* prevent tx timeout */
Eric Dumazet553e2332009-05-27 10:34:50 +00001457 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 netif_wake_queue(dev);
1459}
1460
Jarek Poplawskic0d7a022009-12-23 21:54:29 -08001461static void rhine_tx_timeout(struct net_device *dev)
1462{
1463 struct rhine_private *rp = netdev_priv(dev);
1464 void __iomem *ioaddr = rp->base;
1465
Joe Perchesdf4511f2011-04-16 14:15:25 +00001466 netdev_warn(dev, "Transmit timed out, status %04x, PHY status %04x, resetting...\n",
1467 ioread16(ioaddr + IntrStatus),
1468 mdio_read(dev, rp->mii_if.phy_id, MII_BMSR));
Jarek Poplawskic0d7a022009-12-23 21:54:29 -08001469
1470 schedule_work(&rp->reset_task);
1471}
1472
Stephen Hemminger613573252009-08-31 19:50:58 +00001473static netdev_tx_t rhine_start_tx(struct sk_buff *skb,
1474 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 struct rhine_private *rp = netdev_priv(dev);
1477 void __iomem *ioaddr = rp->base;
1478 unsigned entry;
Dongdong Deng22580f82009-08-13 19:12:31 +00001479 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 /* Caution: the write order is important here, set the field
1482 with the "ownership" bits last. */
1483
1484 /* Calculate the next Tx descriptor entry. */
1485 entry = rp->cur_tx % TX_RING_SIZE;
1486
Herbert Xu5b057c62006-06-23 02:06:41 -07001487 if (skb_padto(skb, ETH_ZLEN))
Patrick McHardy6ed10652009-06-23 06:03:08 +00001488 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 rp->tx_skbuff[entry] = skb;
1491
1492 if ((rp->quirks & rqRhineI) &&
Patrick McHardy84fa7932006-08-29 16:44:56 -07001493 (((unsigned long)skb->data & 3) || skb_shinfo(skb)->nr_frags != 0 || skb->ip_summed == CHECKSUM_PARTIAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 /* Must use alignment buffer. */
1495 if (skb->len > PKT_BUF_SZ) {
1496 /* packet too long, drop it */
1497 dev_kfree_skb(skb);
1498 rp->tx_skbuff[entry] = NULL;
Eric Dumazet553e2332009-05-27 10:34:50 +00001499 dev->stats.tx_dropped++;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001500 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Craig Brind3e0d1672006-04-27 02:30:46 -07001502
1503 /* Padding is not copied and so must be redone. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 skb_copy_and_csum_dev(skb, rp->tx_buf[entry]);
Craig Brind3e0d1672006-04-27 02:30:46 -07001505 if (skb->len < ETH_ZLEN)
1506 memset(rp->tx_buf[entry] + skb->len, 0,
1507 ETH_ZLEN - skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 rp->tx_skbuff_dma[entry] = 0;
1509 rp->tx_ring[entry].addr = cpu_to_le32(rp->tx_bufs_dma +
1510 (rp->tx_buf[entry] -
1511 rp->tx_bufs));
1512 } else {
1513 rp->tx_skbuff_dma[entry] =
1514 pci_map_single(rp->pdev, skb->data, skb->len,
1515 PCI_DMA_TODEVICE);
1516 rp->tx_ring[entry].addr = cpu_to_le32(rp->tx_skbuff_dma[entry]);
1517 }
1518
1519 rp->tx_ring[entry].desc_length =
1520 cpu_to_le32(TXDESC | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
1521
Roger Luethi38f49e82010-12-06 00:59:40 +00001522 if (unlikely(vlan_tx_tag_present(skb))) {
1523 rp->tx_ring[entry].tx_status = cpu_to_le32((vlan_tx_tag_get(skb)) << 16);
1524 /* request tagging */
1525 rp->tx_ring[entry].desc_length |= cpu_to_le32(0x020000);
1526 }
1527 else
1528 rp->tx_ring[entry].tx_status = 0;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* lock eth irq */
Dongdong Deng22580f82009-08-13 19:12:31 +00001531 spin_lock_irqsave(&rp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 wmb();
Roger Luethi38f49e82010-12-06 00:59:40 +00001533 rp->tx_ring[entry].tx_status |= cpu_to_le32(DescOwn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 wmb();
1535
1536 rp->cur_tx++;
1537
1538 /* Non-x86 Todo: explicitly flush cache lines here. */
1539
Roger Luethi38f49e82010-12-06 00:59:40 +00001540 if (vlan_tx_tag_present(skb))
1541 /* Tx queues are bits 7-0 (first Tx queue: bit 7) */
1542 BYTE_REG_BITS_ON(1 << 7, ioaddr + TQWake);
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* Wake the potentially-idle transmit channel */
1545 iowrite8(ioread8(ioaddr + ChipCmd1) | Cmd1TxDemand,
1546 ioaddr + ChipCmd1);
1547 IOSYNC;
1548
1549 if (rp->cur_tx == rp->dirty_tx + TX_QUEUE_LEN)
1550 netif_stop_queue(dev);
1551
Dongdong Deng22580f82009-08-13 19:12:31 +00001552 spin_unlock_irqrestore(&rp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 if (debug > 4) {
Joe Perchesdf4511f2011-04-16 14:15:25 +00001555 netdev_dbg(dev, "Transmit frame #%d queued in slot %d\n",
1556 rp->cur_tx-1, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Patrick McHardy6ed10652009-06-23 06:03:08 +00001558 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561/* The interrupt handler does all of the Rx thread work and cleans up
1562 after the Tx thread. */
David Howells7d12e782006-10-05 14:55:46 +01001563static irqreturn_t rhine_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 struct net_device *dev = dev_instance;
1566 struct rhine_private *rp = netdev_priv(dev);
1567 void __iomem *ioaddr = rp->base;
1568 u32 intr_status;
1569 int boguscnt = max_interrupt_work;
1570 int handled = 0;
1571
1572 while ((intr_status = get_intr_status(dev))) {
1573 handled = 1;
1574
1575 /* Acknowledge all of the current interrupt sources ASAP. */
1576 if (intr_status & IntrTxDescRace)
1577 iowrite8(0x08, ioaddr + IntrStatus2);
1578 iowrite16(intr_status & 0xffff, ioaddr + IntrStatus);
1579 IOSYNC;
1580
1581 if (debug > 4)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001582 netdev_dbg(dev, "Interrupt, status %08x\n",
1583 intr_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
Roger Luethi633949a2006-08-14 23:00:17 -07001586 IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf)) {
Roger Luethi633949a2006-08-14 23:00:17 -07001587 iowrite16(IntrTxAborted |
1588 IntrTxDone | IntrTxError | IntrTxUnderrun |
1589 IntrPCIErr | IntrStatsMax | IntrLinkChange,
1590 ioaddr + IntrEnable);
1591
Ben Hutchings288379f2009-01-19 16:43:59 -08001592 napi_schedule(&rp->napi);
Roger Luethi633949a2006-08-14 23:00:17 -07001593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (intr_status & (IntrTxErrSummary | IntrTxDone)) {
1596 if (intr_status & IntrTxErrSummary) {
1597 /* Avoid scavenging before Tx engine turned off */
1598 RHINE_WAIT_FOR(!(ioread8(ioaddr+ChipCmd) & CmdTxOn));
1599 if (debug > 2 &&
1600 ioread8(ioaddr+ChipCmd) & CmdTxOn)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001601 netdev_warn(dev,
1602 "%s: Tx engine still on\n",
1603 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 rhine_tx(dev);
1606 }
1607
1608 /* Abnormal error summary/uncommon events handlers. */
1609 if (intr_status & (IntrPCIErr | IntrLinkChange |
1610 IntrStatsMax | IntrTxError | IntrTxAborted |
1611 IntrTxUnderrun | IntrTxDescRace))
1612 rhine_error(dev, intr_status);
1613
1614 if (--boguscnt < 0) {
Joe Perchesdf4511f2011-04-16 14:15:25 +00001615 netdev_warn(dev, "Too much work at interrupt, status=%#08x\n",
1616 intr_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 break;
1618 }
1619 }
1620
1621 if (debug > 3)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001622 netdev_dbg(dev, "exiting interrupt, status=%08x\n",
1623 ioread16(ioaddr + IntrStatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return IRQ_RETVAL(handled);
1625}
1626
1627/* This routine is logically part of the interrupt handler, but isolated
1628 for clarity. */
1629static void rhine_tx(struct net_device *dev)
1630{
1631 struct rhine_private *rp = netdev_priv(dev);
1632 int txstatus = 0, entry = rp->dirty_tx % TX_RING_SIZE;
1633
1634 spin_lock(&rp->lock);
1635
1636 /* find and cleanup dirty tx descriptors */
1637 while (rp->dirty_tx != rp->cur_tx) {
1638 txstatus = le32_to_cpu(rp->tx_ring[entry].tx_status);
1639 if (debug > 6)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001640 netdev_dbg(dev, "Tx scavenge %d status %08x\n",
1641 entry, txstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (txstatus & DescOwn)
1643 break;
1644 if (txstatus & 0x8000) {
1645 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001646 netdev_dbg(dev, "Transmit error, Tx status %08x\n",
1647 txstatus);
Eric Dumazet553e2332009-05-27 10:34:50 +00001648 dev->stats.tx_errors++;
1649 if (txstatus & 0x0400)
1650 dev->stats.tx_carrier_errors++;
1651 if (txstatus & 0x0200)
1652 dev->stats.tx_window_errors++;
1653 if (txstatus & 0x0100)
1654 dev->stats.tx_aborted_errors++;
1655 if (txstatus & 0x0080)
1656 dev->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (((rp->quirks & rqRhineI) && txstatus & 0x0002) ||
1658 (txstatus & 0x0800) || (txstatus & 0x1000)) {
Eric Dumazet553e2332009-05-27 10:34:50 +00001659 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 rp->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1661 break; /* Keep the skb - we try again */
1662 }
1663 /* Transmitter restarted in 'abnormal' handler. */
1664 } else {
1665 if (rp->quirks & rqRhineI)
Eric Dumazet553e2332009-05-27 10:34:50 +00001666 dev->stats.collisions += (txstatus >> 3) & 0x0F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 else
Eric Dumazet553e2332009-05-27 10:34:50 +00001668 dev->stats.collisions += txstatus & 0x0F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (debug > 6)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001670 netdev_dbg(dev, "collisions: %1.1x:%1.1x\n",
1671 (txstatus >> 3) & 0xF,
1672 txstatus & 0xF);
Eric Dumazet553e2332009-05-27 10:34:50 +00001673 dev->stats.tx_bytes += rp->tx_skbuff[entry]->len;
1674 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 /* Free the original skb. */
1677 if (rp->tx_skbuff_dma[entry]) {
1678 pci_unmap_single(rp->pdev,
1679 rp->tx_skbuff_dma[entry],
1680 rp->tx_skbuff[entry]->len,
1681 PCI_DMA_TODEVICE);
1682 }
1683 dev_kfree_skb_irq(rp->tx_skbuff[entry]);
1684 rp->tx_skbuff[entry] = NULL;
1685 entry = (++rp->dirty_tx) % TX_RING_SIZE;
1686 }
1687 if ((rp->cur_tx - rp->dirty_tx) < TX_QUEUE_LEN - 4)
1688 netif_wake_queue(dev);
1689
1690 spin_unlock(&rp->lock);
1691}
1692
Roger Luethi38f49e82010-12-06 00:59:40 +00001693/**
1694 * rhine_get_vlan_tci - extract TCI from Rx data buffer
1695 * @skb: pointer to sk_buff
1696 * @data_size: used data area of the buffer including CRC
1697 *
1698 * If hardware VLAN tag extraction is enabled and the chip indicates a 802.1Q
1699 * packet, the extracted 802.1Q header (2 bytes TPID + 2 bytes TCI) is 4-byte
1700 * aligned following the CRC.
1701 */
1702static inline u16 rhine_get_vlan_tci(struct sk_buff *skb, int data_size)
1703{
1704 u8 *trailer = (u8 *)skb->data + ((data_size + 3) & ~3) + 2;
Harvey Harrison4562b2f2011-03-28 17:08:59 +00001705 return be16_to_cpup((__be16 *)trailer);
Roger Luethi38f49e82010-12-06 00:59:40 +00001706}
1707
Roger Luethi633949a2006-08-14 23:00:17 -07001708/* Process up to limit frames from receive ring */
1709static int rhine_rx(struct net_device *dev, int limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct rhine_private *rp = netdev_priv(dev);
Roger Luethi633949a2006-08-14 23:00:17 -07001712 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int entry = rp->cur_rx % RX_RING_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 if (debug > 4) {
Joe Perchesdf4511f2011-04-16 14:15:25 +00001716 netdev_dbg(dev, "%s(), entry %d status %08x\n",
1717 __func__, entry,
1718 le32_to_cpu(rp->rx_head_desc->rx_status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720
1721 /* If EOP is set on the next entry, it's a new packet. Send it up. */
Roger Luethi633949a2006-08-14 23:00:17 -07001722 for (count = 0; count < limit; ++count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 struct rx_desc *desc = rp->rx_head_desc;
1724 u32 desc_status = le32_to_cpu(desc->rx_status);
Roger Luethi38f49e82010-12-06 00:59:40 +00001725 u32 desc_length = le32_to_cpu(desc->desc_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int data_size = desc_status >> 16;
1727
Roger Luethi633949a2006-08-14 23:00:17 -07001728 if (desc_status & DescOwn)
1729 break;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (debug > 4)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001732 netdev_dbg(dev, "%s() status is %08x\n",
1733 __func__, desc_status);
Roger Luethi633949a2006-08-14 23:00:17 -07001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if ((desc_status & (RxWholePkt | RxErr)) != RxWholePkt) {
1736 if ((desc_status & RxWholePkt) != RxWholePkt) {
Joe Perchesdf4511f2011-04-16 14:15:25 +00001737 netdev_warn(dev,
1738 "Oversized Ethernet frame spanned multiple buffers, "
1739 "entry %#x length %d status %08x!\n",
1740 entry, data_size,
1741 desc_status);
1742 netdev_warn(dev,
1743 "Oversized Ethernet frame %p vs %p\n",
1744 rp->rx_head_desc,
1745 &rp->rx_ring[entry]);
Eric Dumazet553e2332009-05-27 10:34:50 +00001746 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 } else if (desc_status & RxErr) {
1748 /* There was a error. */
1749 if (debug > 2)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001750 netdev_dbg(dev, "%s() Rx error was %08x\n",
1751 __func__, desc_status);
Eric Dumazet553e2332009-05-27 10:34:50 +00001752 dev->stats.rx_errors++;
1753 if (desc_status & 0x0030)
1754 dev->stats.rx_length_errors++;
1755 if (desc_status & 0x0048)
1756 dev->stats.rx_fifo_errors++;
1757 if (desc_status & 0x0004)
1758 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (desc_status & 0x0002) {
1760 /* this can also be updated outside the interrupt handler */
1761 spin_lock(&rp->lock);
Eric Dumazet553e2332009-05-27 10:34:50 +00001762 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 spin_unlock(&rp->lock);
1764 }
1765 }
1766 } else {
Eric Dumazet89d71a62009-10-13 05:34:20 +00001767 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* Length should omit the CRC */
1769 int pkt_len = data_size - 4;
Roger Luethi38f49e82010-12-06 00:59:40 +00001770 u16 vlan_tci = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /* Check if the packet is long enough to accept without
1773 copying to a minimally-sized skbuff. */
Eric Dumazet89d71a62009-10-13 05:34:20 +00001774 if (pkt_len < rx_copybreak)
1775 skb = netdev_alloc_skb_ip_align(dev, pkt_len);
1776 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 pci_dma_sync_single_for_cpu(rp->pdev,
1778 rp->rx_skbuff_dma[entry],
1779 rp->rx_buf_sz,
1780 PCI_DMA_FROMDEVICE);
1781
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001782 skb_copy_to_linear_data(skb,
David S. Miller689be432005-06-28 15:25:31 -07001783 rp->rx_skbuff[entry]->data,
David S. Miller8c7b7fa2007-07-10 22:08:12 -07001784 pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 skb_put(skb, pkt_len);
1786 pci_dma_sync_single_for_device(rp->pdev,
1787 rp->rx_skbuff_dma[entry],
1788 rp->rx_buf_sz,
1789 PCI_DMA_FROMDEVICE);
1790 } else {
1791 skb = rp->rx_skbuff[entry];
1792 if (skb == NULL) {
Joe Perchesdf4511f2011-04-16 14:15:25 +00001793 netdev_err(dev, "Inconsistent Rx descriptor chain\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 break;
1795 }
1796 rp->rx_skbuff[entry] = NULL;
1797 skb_put(skb, pkt_len);
1798 pci_unmap_single(rp->pdev,
1799 rp->rx_skbuff_dma[entry],
1800 rp->rx_buf_sz,
1801 PCI_DMA_FROMDEVICE);
1802 }
Roger Luethi38f49e82010-12-06 00:59:40 +00001803
1804 if (unlikely(desc_length & DescTag))
1805 vlan_tci = rhine_get_vlan_tci(skb, data_size);
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 skb->protocol = eth_type_trans(skb, dev);
Roger Luethi38f49e82010-12-06 00:59:40 +00001808
1809 if (unlikely(desc_length & DescTag))
1810 __vlan_hwaccel_put_tag(skb, vlan_tci);
Roger Luethi633949a2006-08-14 23:00:17 -07001811 netif_receive_skb(skb);
Eric Dumazet553e2332009-05-27 10:34:50 +00001812 dev->stats.rx_bytes += pkt_len;
1813 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 entry = (++rp->cur_rx) % RX_RING_SIZE;
1816 rp->rx_head_desc = &rp->rx_ring[entry];
1817 }
1818
1819 /* Refill the Rx ring buffers. */
1820 for (; rp->cur_rx - rp->dirty_rx > 0; rp->dirty_rx++) {
1821 struct sk_buff *skb;
1822 entry = rp->dirty_rx % RX_RING_SIZE;
1823 if (rp->rx_skbuff[entry] == NULL) {
Kevin Lob26b5552008-08-27 11:35:09 +08001824 skb = netdev_alloc_skb(dev, rp->rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 rp->rx_skbuff[entry] = skb;
1826 if (skb == NULL)
1827 break; /* Better luck next round. */
1828 skb->dev = dev; /* Mark as being used by this device. */
1829 rp->rx_skbuff_dma[entry] =
David S. Miller689be432005-06-28 15:25:31 -07001830 pci_map_single(rp->pdev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 rp->rx_buf_sz,
1832 PCI_DMA_FROMDEVICE);
1833 rp->rx_ring[entry].addr = cpu_to_le32(rp->rx_skbuff_dma[entry]);
1834 }
1835 rp->rx_ring[entry].rx_status = cpu_to_le32(DescOwn);
1836 }
Roger Luethi633949a2006-08-14 23:00:17 -07001837
1838 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
1841/*
1842 * Clears the "tally counters" for CRC errors and missed frames(?).
1843 * It has been reported that some chips need a write of 0 to clear
1844 * these, for others the counters are set to 1 when written to and
1845 * instead cleared when read. So we clear them both ways ...
1846 */
1847static inline void clear_tally_counters(void __iomem *ioaddr)
1848{
1849 iowrite32(0, ioaddr + RxMissed);
1850 ioread16(ioaddr + RxCRCErrs);
1851 ioread16(ioaddr + RxMissed);
1852}
1853
1854static void rhine_restart_tx(struct net_device *dev) {
1855 struct rhine_private *rp = netdev_priv(dev);
1856 void __iomem *ioaddr = rp->base;
1857 int entry = rp->dirty_tx % TX_RING_SIZE;
1858 u32 intr_status;
1859
1860 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001861 * If new errors occurred, we need to sort them out before doing Tx.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * In that case the ISR will be back here RSN anyway.
1863 */
1864 intr_status = get_intr_status(dev);
1865
1866 if ((intr_status & IntrTxErrSummary) == 0) {
1867
1868 /* We know better than the chip where it should continue. */
1869 iowrite32(rp->tx_ring_dma + entry * sizeof(struct tx_desc),
1870 ioaddr + TxRingPtr);
1871
1872 iowrite8(ioread8(ioaddr + ChipCmd) | CmdTxOn,
1873 ioaddr + ChipCmd);
Roger Luethi38f49e82010-12-06 00:59:40 +00001874
1875 if (rp->tx_ring[entry].desc_length & cpu_to_le32(0x020000))
1876 /* Tx queues are bits 7-0 (first Tx queue: bit 7) */
1877 BYTE_REG_BITS_ON(1 << 7, ioaddr + TQWake);
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 iowrite8(ioread8(ioaddr + ChipCmd1) | Cmd1TxDemand,
1880 ioaddr + ChipCmd1);
1881 IOSYNC;
1882 }
1883 else {
1884 /* This should never happen */
1885 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001886 netdev_warn(dev, "%s() Another error occurred %08x\n",
1887 __func__, intr_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
1890}
1891
1892static void rhine_error(struct net_device *dev, int intr_status)
1893{
1894 struct rhine_private *rp = netdev_priv(dev);
1895 void __iomem *ioaddr = rp->base;
1896
1897 spin_lock(&rp->lock);
1898
1899 if (intr_status & IntrLinkChange)
John W. Linville38bb6b22006-05-19 10:51:21 -04001900 rhine_check_media(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (intr_status & IntrStatsMax) {
Eric Dumazet553e2332009-05-27 10:34:50 +00001902 dev->stats.rx_crc_errors += ioread16(ioaddr + RxCRCErrs);
1903 dev->stats.rx_missed_errors += ioread16(ioaddr + RxMissed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 clear_tally_counters(ioaddr);
1905 }
1906 if (intr_status & IntrTxAborted) {
1907 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001908 netdev_info(dev, "Abort %08x, frame dropped\n",
1909 intr_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 if (intr_status & IntrTxUnderrun) {
1912 if (rp->tx_thresh < 0xE0)
Roger Luethi38f49e82010-12-06 00:59:40 +00001913 BYTE_REG_BITS_SET((rp->tx_thresh += 0x20), 0x80, ioaddr + TxConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001915 netdev_info(dev, "Transmitter underrun, Tx threshold now %02x\n",
1916 rp->tx_thresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918 if (intr_status & IntrTxDescRace) {
1919 if (debug > 2)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001920 netdev_info(dev, "Tx descriptor write-back race\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922 if ((intr_status & IntrTxError) &&
1923 (intr_status & (IntrTxAborted |
1924 IntrTxUnderrun | IntrTxDescRace)) == 0) {
1925 if (rp->tx_thresh < 0xE0) {
Roger Luethi38f49e82010-12-06 00:59:40 +00001926 BYTE_REG_BITS_SET((rp->tx_thresh += 0x20), 0x80, ioaddr + TxConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001929 netdev_info(dev, "Unspecified error. Tx threshold now %02x\n",
1930 rp->tx_thresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932 if (intr_status & (IntrTxAborted | IntrTxUnderrun | IntrTxDescRace |
1933 IntrTxError))
1934 rhine_restart_tx(dev);
1935
1936 if (intr_status & ~(IntrLinkChange | IntrStatsMax | IntrTxUnderrun |
1937 IntrTxError | IntrTxAborted | IntrNormalSummary |
1938 IntrTxDescRace)) {
1939 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00001940 netdev_err(dev, "Something Wicked happened! %08x\n",
1941 intr_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943
1944 spin_unlock(&rp->lock);
1945}
1946
1947static struct net_device_stats *rhine_get_stats(struct net_device *dev)
1948{
1949 struct rhine_private *rp = netdev_priv(dev);
1950 void __iomem *ioaddr = rp->base;
1951 unsigned long flags;
1952
1953 spin_lock_irqsave(&rp->lock, flags);
Eric Dumazet553e2332009-05-27 10:34:50 +00001954 dev->stats.rx_crc_errors += ioread16(ioaddr + RxCRCErrs);
1955 dev->stats.rx_missed_errors += ioread16(ioaddr + RxMissed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 clear_tally_counters(ioaddr);
1957 spin_unlock_irqrestore(&rp->lock, flags);
1958
Eric Dumazet553e2332009-05-27 10:34:50 +00001959 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962static void rhine_set_rx_mode(struct net_device *dev)
1963{
1964 struct rhine_private *rp = netdev_priv(dev);
1965 void __iomem *ioaddr = rp->base;
1966 u32 mc_filter[2]; /* Multicast hash filter */
Roger Luethi38f49e82010-12-06 00:59:40 +00001967 u8 rx_mode = 0x0C; /* Note: 0x02=accept runt, 0x01=accept errs */
1968 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 rx_mode = 0x1C;
1972 iowrite32(0xffffffff, ioaddr + MulticastFilter0);
1973 iowrite32(0xffffffff, ioaddr + MulticastFilter1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001974 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00001975 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* Too many to match, or accept all multicasts. */
1977 iowrite32(0xffffffff, ioaddr + MulticastFilter0);
1978 iowrite32(0xffffffff, ioaddr + MulticastFilter1);
Roger Luethi38f49e82010-12-06 00:59:40 +00001979 } else if (rp->pdev->revision >= VT6105M) {
1980 int i = 0;
1981 u32 mCAMmask = 0; /* 32 mCAMs (6105M and better) */
1982 netdev_for_each_mc_addr(ha, dev) {
1983 if (i == MCAM_SIZE)
1984 break;
1985 rhine_set_cam(ioaddr, i, ha->addr);
1986 mCAMmask |= 1 << i;
1987 i++;
1988 }
1989 rhine_set_cam_mask(ioaddr, mCAMmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 memset(mc_filter, 0, sizeof(mc_filter));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001992 netdev_for_each_mc_addr(ha, dev) {
1993 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1996 }
1997 iowrite32(mc_filter[0], ioaddr + MulticastFilter0);
1998 iowrite32(mc_filter[1], ioaddr + MulticastFilter1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Roger Luethi38f49e82010-12-06 00:59:40 +00002000 /* enable/disable VLAN receive filtering */
2001 if (rp->pdev->revision >= VT6105M) {
2002 if (dev->flags & IFF_PROMISC)
2003 BYTE_REG_BITS_OFF(BCR1_VIDFR, ioaddr + PCIBusConfig1);
2004 else
2005 BYTE_REG_BITS_ON(BCR1_VIDFR, ioaddr + PCIBusConfig1);
2006 }
2007 BYTE_REG_BITS_ON(rx_mode, ioaddr + RxConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010static void netdev_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2011{
2012 struct rhine_private *rp = netdev_priv(dev);
2013
Rick Jones23020ab2011-11-09 09:58:07 +00002014 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2015 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
2016 strlcpy(info->bus_info, pci_name(rp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
2019static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2020{
2021 struct rhine_private *rp = netdev_priv(dev);
2022 int rc;
2023
2024 spin_lock_irq(&rp->lock);
2025 rc = mii_ethtool_gset(&rp->mii_if, cmd);
2026 spin_unlock_irq(&rp->lock);
2027
2028 return rc;
2029}
2030
2031static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2032{
2033 struct rhine_private *rp = netdev_priv(dev);
2034 int rc;
2035
2036 spin_lock_irq(&rp->lock);
2037 rc = mii_ethtool_sset(&rp->mii_if, cmd);
2038 spin_unlock_irq(&rp->lock);
Roger Luethi00b428c2006-03-28 20:53:56 +02002039 rhine_set_carrier(&rp->mii_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 return rc;
2042}
2043
2044static int netdev_nway_reset(struct net_device *dev)
2045{
2046 struct rhine_private *rp = netdev_priv(dev);
2047
2048 return mii_nway_restart(&rp->mii_if);
2049}
2050
2051static u32 netdev_get_link(struct net_device *dev)
2052{
2053 struct rhine_private *rp = netdev_priv(dev);
2054
2055 return mii_link_ok(&rp->mii_if);
2056}
2057
2058static u32 netdev_get_msglevel(struct net_device *dev)
2059{
2060 return debug;
2061}
2062
2063static void netdev_set_msglevel(struct net_device *dev, u32 value)
2064{
2065 debug = value;
2066}
2067
2068static void rhine_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2069{
2070 struct rhine_private *rp = netdev_priv(dev);
2071
2072 if (!(rp->quirks & rqWOL))
2073 return;
2074
2075 spin_lock_irq(&rp->lock);
2076 wol->supported = WAKE_PHY | WAKE_MAGIC |
2077 WAKE_UCAST | WAKE_MCAST | WAKE_BCAST; /* Untested */
2078 wol->wolopts = rp->wolopts;
2079 spin_unlock_irq(&rp->lock);
2080}
2081
2082static int rhine_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2083{
2084 struct rhine_private *rp = netdev_priv(dev);
2085 u32 support = WAKE_PHY | WAKE_MAGIC |
2086 WAKE_UCAST | WAKE_MCAST | WAKE_BCAST; /* Untested */
2087
2088 if (!(rp->quirks & rqWOL))
2089 return -EINVAL;
2090
2091 if (wol->wolopts & ~support)
2092 return -EINVAL;
2093
2094 spin_lock_irq(&rp->lock);
2095 rp->wolopts = wol->wolopts;
2096 spin_unlock_irq(&rp->lock);
2097
2098 return 0;
2099}
2100
Jeff Garzik7282d492006-09-13 14:30:00 -04002101static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 .get_drvinfo = netdev_get_drvinfo,
2103 .get_settings = netdev_get_settings,
2104 .set_settings = netdev_set_settings,
2105 .nway_reset = netdev_nway_reset,
2106 .get_link = netdev_get_link,
2107 .get_msglevel = netdev_get_msglevel,
2108 .set_msglevel = netdev_set_msglevel,
2109 .get_wol = rhine_get_wol,
2110 .set_wol = rhine_set_wol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111};
2112
2113static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2114{
2115 struct rhine_private *rp = netdev_priv(dev);
2116 int rc;
2117
2118 if (!netif_running(dev))
2119 return -EINVAL;
2120
2121 spin_lock_irq(&rp->lock);
2122 rc = generic_mii_ioctl(&rp->mii_if, if_mii(rq), cmd, NULL);
2123 spin_unlock_irq(&rp->lock);
Roger Luethi00b428c2006-03-28 20:53:56 +02002124 rhine_set_carrier(&rp->mii_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 return rc;
2127}
2128
2129static int rhine_close(struct net_device *dev)
2130{
2131 struct rhine_private *rp = netdev_priv(dev);
2132 void __iomem *ioaddr = rp->base;
2133
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002134 napi_disable(&rp->napi);
Jarek Poplawskic0d7a022009-12-23 21:54:29 -08002135 cancel_work_sync(&rp->reset_task);
2136 netif_stop_queue(dev);
2137
2138 spin_lock_irq(&rp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00002141 netdev_dbg(dev, "Shutting down ethercard, status was %04x\n",
2142 ioread16(ioaddr + ChipCmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 /* Switch to loopback mode to avoid hardware races. */
2145 iowrite8(rp->tx_thresh | 0x02, ioaddr + TxConfig);
2146
2147 /* Disable interrupts by clearing the interrupt mask. */
2148 iowrite16(0x0000, ioaddr + IntrEnable);
2149
2150 /* Stop the chip's Tx and Rx processes. */
2151 iowrite16(CmdStop, ioaddr + ChipCmd);
2152
2153 spin_unlock_irq(&rp->lock);
2154
2155 free_irq(rp->pdev->irq, dev);
2156 free_rbufs(dev);
2157 free_tbufs(dev);
2158 free_ring(dev);
2159
2160 return 0;
2161}
2162
2163
2164static void __devexit rhine_remove_one(struct pci_dev *pdev)
2165{
2166 struct net_device *dev = pci_get_drvdata(pdev);
2167 struct rhine_private *rp = netdev_priv(dev);
2168
2169 unregister_netdev(dev);
2170
2171 pci_iounmap(pdev, rp->base);
2172 pci_release_regions(pdev);
2173
2174 free_netdev(dev);
2175 pci_disable_device(pdev);
2176 pci_set_drvdata(pdev, NULL);
2177}
2178
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07002179static void rhine_shutdown (struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 struct net_device *dev = pci_get_drvdata(pdev);
2182 struct rhine_private *rp = netdev_priv(dev);
2183 void __iomem *ioaddr = rp->base;
2184
2185 if (!(rp->quirks & rqWOL))
2186 return; /* Nothing to do for non-WOL adapters */
2187
2188 rhine_power_init(dev);
2189
2190 /* Make sure we use pattern 0, 1 and not 4, 5 */
2191 if (rp->quirks & rq6patterns)
Laura Garciaf11cf252008-02-23 18:56:35 +01002192 iowrite8(0x04, ioaddr + WOLcgClr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 if (rp->wolopts & WAKE_MAGIC) {
2195 iowrite8(WOLmagic, ioaddr + WOLcrSet);
2196 /*
2197 * Turn EEPROM-controlled wake-up back on -- some hardware may
2198 * not cooperate otherwise.
2199 */
2200 iowrite8(ioread8(ioaddr + ConfigA) | 0x03, ioaddr + ConfigA);
2201 }
2202
2203 if (rp->wolopts & (WAKE_BCAST|WAKE_MCAST))
2204 iowrite8(WOLbmcast, ioaddr + WOLcgSet);
2205
2206 if (rp->wolopts & WAKE_PHY)
2207 iowrite8(WOLlnkon | WOLlnkoff, ioaddr + WOLcrSet);
2208
2209 if (rp->wolopts & WAKE_UCAST)
2210 iowrite8(WOLucast, ioaddr + WOLcrSet);
2211
2212 if (rp->wolopts) {
2213 /* Enable legacy WOL (for old motherboards) */
2214 iowrite8(0x01, ioaddr + PwcfgSet);
2215 iowrite8(ioread8(ioaddr + StickyHW) | 0x04, ioaddr + StickyHW);
2216 }
2217
2218 /* Hit power state D3 (sleep) */
Roger Luethib933b4d2006-08-14 23:00:21 -07002219 if (!avoid_D3)
2220 iowrite8(ioread8(ioaddr + StickyHW) | 0x03, ioaddr + StickyHW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 /* TODO: Check use of pci_enable_wake() */
2223
2224}
2225
2226#ifdef CONFIG_PM
2227static int rhine_suspend(struct pci_dev *pdev, pm_message_t state)
2228{
2229 struct net_device *dev = pci_get_drvdata(pdev);
2230 struct rhine_private *rp = netdev_priv(dev);
2231 unsigned long flags;
2232
2233 if (!netif_running(dev))
2234 return 0;
2235
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002236 napi_disable(&rp->napi);
Francois Romieu32b0f532008-07-11 00:30:14 +02002237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 netif_device_detach(dev);
2239 pci_save_state(pdev);
2240
2241 spin_lock_irqsave(&rp->lock, flags);
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07002242 rhine_shutdown(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 spin_unlock_irqrestore(&rp->lock, flags);
2244
2245 free_irq(dev->irq, dev);
2246 return 0;
2247}
2248
2249static int rhine_resume(struct pci_dev *pdev)
2250{
2251 struct net_device *dev = pci_get_drvdata(pdev);
2252 struct rhine_private *rp = netdev_priv(dev);
2253 unsigned long flags;
2254 int ret;
2255
2256 if (!netif_running(dev))
2257 return 0;
2258
Roger Luethi38f49e82010-12-06 00:59:40 +00002259 if (request_irq(dev->irq, rhine_interrupt, IRQF_SHARED, dev->name, dev))
Joe Perchesdf4511f2011-04-16 14:15:25 +00002260 netdev_err(dev, "request_irq failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 ret = pci_set_power_state(pdev, PCI_D0);
2263 if (debug > 1)
Joe Perchesdf4511f2011-04-16 14:15:25 +00002264 netdev_info(dev, "Entering power state D0 %s (%d)\n",
2265 ret ? "failed" : "succeeded", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 pci_restore_state(pdev);
2268
2269 spin_lock_irqsave(&rp->lock, flags);
2270#ifdef USE_MMIO
2271 enable_mmio(rp->pioaddr, rp->quirks);
2272#endif
2273 rhine_power_init(dev);
2274 free_tbufs(dev);
2275 free_rbufs(dev);
2276 alloc_tbufs(dev);
2277 alloc_rbufs(dev);
2278 init_registers(dev);
2279 spin_unlock_irqrestore(&rp->lock, flags);
2280
2281 netif_device_attach(dev);
2282
2283 return 0;
2284}
2285#endif /* CONFIG_PM */
2286
2287static struct pci_driver rhine_driver = {
2288 .name = DRV_NAME,
2289 .id_table = rhine_pci_tbl,
2290 .probe = rhine_init_one,
2291 .remove = __devexit_p(rhine_remove_one),
2292#ifdef CONFIG_PM
2293 .suspend = rhine_suspend,
2294 .resume = rhine_resume,
2295#endif /* CONFIG_PM */
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07002296 .shutdown = rhine_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297};
2298
Roger Luethie84df482007-03-06 19:57:37 +01002299static struct dmi_system_id __initdata rhine_dmi_table[] = {
2300 {
2301 .ident = "EPIA-M",
2302 .matches = {
2303 DMI_MATCH(DMI_BIOS_VENDOR, "Award Software International, Inc."),
2304 DMI_MATCH(DMI_BIOS_VERSION, "6.00 PG"),
2305 },
2306 },
2307 {
2308 .ident = "KV7",
2309 .matches = {
2310 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
2311 DMI_MATCH(DMI_BIOS_VERSION, "6.00 PG"),
2312 },
2313 },
2314 { NULL }
2315};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317static int __init rhine_init(void)
2318{
2319/* when a module, this is printed whether or not devices are found in probe */
2320#ifdef MODULE
Joe Perchesdf4511f2011-04-16 14:15:25 +00002321 pr_info("%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322#endif
Roger Luethie84df482007-03-06 19:57:37 +01002323 if (dmi_check_system(rhine_dmi_table)) {
2324 /* these BIOSes fail at PXE boot if chip is in D3 */
2325 avoid_D3 = 1;
Joe Perchesdf4511f2011-04-16 14:15:25 +00002326 pr_warn("Broken BIOS detected, avoid_D3 enabled\n");
Roger Luethie84df482007-03-06 19:57:37 +01002327 }
2328 else if (avoid_D3)
Joe Perchesdf4511f2011-04-16 14:15:25 +00002329 pr_info("avoid_D3 set\n");
Roger Luethie84df482007-03-06 19:57:37 +01002330
Jeff Garzik29917622006-08-19 17:48:59 -04002331 return pci_register_driver(&rhine_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334
2335static void __exit rhine_cleanup(void)
2336{
2337 pci_unregister_driver(&rhine_driver);
2338}
2339
2340
2341module_init(rhine_init);
2342module_exit(rhine_cleanup);