blob: 5657049c216041faea5c59f61e37898643f14447 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* natsemi.c: A Linux PCI Ethernet driver for the NatSemi DP8381x series. */
2/*
3 Written/copyright 1999-2001 by Donald Becker.
4 Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
5 Portions copyright 2001,2002 Manfred Spraul (manfred@colorfullife.com)
Mark Brownb27a16b2006-02-02 00:00:01 +00006 Portions copyright 2004 Harald Welte <laforge@gnumonks.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8 This software may be used and distributed according to the terms of
9 the GNU General Public License (GPL), incorporated herein by reference.
10 Drivers based on or derived from this code fall under the GPL and must
11 retain the authorship, copyright and license notice. This file is not
12 a complete program and may only be used when the entire operating
13 system is licensed under the GPL. License for under other terms may be
14 available. Contact the original author for details.
15
16 The original author may be reached as becker@scyld.com, or at
17 Scyld Computing Corporation
18 410 Severn Ave., Suite 210
19 Annapolis MD 21403
20
21 Support information and updates available at
22 http://www.scyld.com/network/netsemi.html
23
24
25 Linux kernel modifications:
26
27 Version 1.0.1:
28 - Spinlock fixes
29 - Bug fixes and better intr performance (Tjeerd)
30 Version 1.0.2:
31 - Now reads correct MAC address from eeprom
32 Version 1.0.3:
33 - Eliminate redundant priv->tx_full flag
34 - Call netif_start_queue from dev->tx_timeout
35 - wmb() in start_tx() to flush data
36 - Update Tx locking
37 - Clean up PCI enable (davej)
38 Version 1.0.4:
39 - Merge Donald Becker's natsemi.c version 1.07
40 Version 1.0.5:
41 - { fill me in }
42 Version 1.0.6:
43 * ethtool support (jgarzik)
44 * Proper initialization of the card (which sometimes
45 fails to occur and leaves the card in a non-functional
46 state). (uzi)
47
48 * Some documented register settings to optimize some
49 of the 100Mbit autodetection circuitry in rev C cards. (uzi)
50
51 * Polling of the PHY intr for stuff like link state
52 change and auto- negotiation to finally work properly. (uzi)
53
54 * One-liner removal of a duplicate declaration of
55 netdev_error(). (uzi)
56
57 Version 1.0.7: (Manfred Spraul)
58 * pci dma
59 * SMP locking update
60 * full reset added into tx_timeout
61 * correct multicast hash generation (both big and little endian)
62 [copied from a natsemi driver version
63 from Myrio Corporation, Greg Smith]
64 * suspend/resume
65
66 version 1.0.8 (Tim Hockin <thockin@sun.com>)
67 * ETHTOOL_* support
68 * Wake on lan support (Erik Gilling)
69 * MXDMA fixes for serverworks
70 * EEPROM reload
71
72 version 1.0.9 (Manfred Spraul)
73 * Main change: fix lack of synchronize
74 netif_close/netif_suspend against a last interrupt
75 or packet.
76 * do not enable superflous interrupts (e.g. the
77 drivers relies on TxDone - TxIntr not needed)
78 * wait that the hardware has really stopped in close
79 and suspend.
80 * workaround for the (at least) gcc-2.95.1 compiler
81 problem. Also simplifies the code a bit.
82 * disable_irq() in tx_timeout - needed to protect
83 against rx interrupts.
84 * stop the nic before switching into silent rx mode
85 for wol (required according to docu).
86
87 version 1.0.10:
88 * use long for ee_addr (various)
89 * print pointers properly (DaveM)
90 * include asm/irq.h (?)
91
92 version 1.0.11:
93 * check and reset if PHY errors appear (Adrian Sun)
94 * WoL cleanup (Tim Hockin)
95 * Magic number cleanup (Tim Hockin)
96 * Don't reload EEPROM on every reset (Tim Hockin)
97 * Save and restore EEPROM state across reset (Tim Hockin)
98 * MDIO Cleanup (Tim Hockin)
99 * Reformat register offsets/bits (jgarzik)
100
101 version 1.0.12:
102 * ETHTOOL_* further support (Tim Hockin)
103
104 version 1.0.13:
105 * ETHTOOL_[G]EEPROM support (Tim Hockin)
106
107 version 1.0.13:
108 * crc cleanup (Matt Domsch <Matt_Domsch@dell.com>)
109
110 version 1.0.14:
111 * Cleanup some messages and autoneg in ethtool (Tim Hockin)
112
113 version 1.0.15:
114 * Get rid of cable_magic flag
115 * use new (National provided) solution for cable magic issue
116
117 version 1.0.16:
118 * call netdev_rx() for RxErrors (Manfred Spraul)
119 * formatting and cleanups
120 * change options and full_duplex arrays to be zero
121 initialized
122 * enable only the WoL and PHY interrupts in wol mode
123
124 version 1.0.17:
125 * only do cable_magic on 83815 and early 83816 (Tim Hockin)
126 * create a function for rx refill (Manfred Spraul)
127 * combine drain_ring and init_ring (Manfred Spraul)
128 * oom handling (Manfred Spraul)
129 * hands_off instead of playing with netif_device_{de,a}ttach
130 (Manfred Spraul)
131 * be sure to write the MAC back to the chip (Manfred Spraul)
132 * lengthen EEPROM timeout, and always warn about timeouts
133 (Manfred Spraul)
134 * comments update (Manfred)
135 * do the right thing on a phy-reset (Manfred and Tim)
136
137 TODO:
138 * big endian support with CFG:BEM instead of cpu_to_le32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139*/
140
141#include <linux/config.h>
142#include <linux/module.h>
143#include <linux/kernel.h>
144#include <linux/string.h>
145#include <linux/timer.h>
146#include <linux/errno.h>
147#include <linux/ioport.h>
148#include <linux/slab.h>
149#include <linux/interrupt.h>
150#include <linux/pci.h>
151#include <linux/netdevice.h>
152#include <linux/etherdevice.h>
153#include <linux/skbuff.h>
154#include <linux/init.h>
155#include <linux/spinlock.h>
156#include <linux/ethtool.h>
157#include <linux/delay.h>
158#include <linux/rtnetlink.h>
159#include <linux/mii.h>
160#include <linux/crc32.h>
161#include <linux/bitops.h>
Mark Brownb27a16b2006-02-02 00:00:01 +0000162#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#include <asm/processor.h> /* Processor type for cache alignment. */
164#include <asm/io.h>
165#include <asm/irq.h>
166#include <asm/uaccess.h>
167
168#define DRV_NAME "natsemi"
169#define DRV_VERSION "1.07+LK1.0.17"
170#define DRV_RELDATE "Sep 27, 2002"
171
172#define RX_OFFSET 2
173
174/* Updated to recommendations in pci-skeleton v2.03. */
175
176/* The user-configurable values.
177 These may be modified when a driver module is loaded.*/
178
179#define NATSEMI_DEF_MSG (NETIF_MSG_DRV | \
180 NETIF_MSG_LINK | \
181 NETIF_MSG_WOL | \
182 NETIF_MSG_RX_ERR | \
183 NETIF_MSG_TX_ERR)
184static int debug = -1;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static int mtu;
187
188/* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
189 This chip uses a 512 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500190static const int multicast_filter_limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/* Set the copy breakpoint for the copy-only-tiny-frames scheme.
193 Setting to > 1518 effectively disables this feature. */
194static int rx_copybreak;
195
196/* Used to pass the media type, etc.
197 Both 'options[]' and 'full_duplex[]' should exist for driver
198 interoperability.
199 The media type is usually passed in 'options[]'.
200*/
201#define MAX_UNITS 8 /* More are supported, limit only on options */
202static int options[MAX_UNITS];
203static int full_duplex[MAX_UNITS];
204
205/* Operational parameters that are set at compile time. */
206
207/* Keep the ring sizes a power of two for compile efficiency.
208 The compiler will convert <unsigned>'%'<2^N> into a bit mask.
209 Making the Tx ring too large decreases the effectiveness of channel
210 bonding and packet priority.
211 There are no ill effects from too-large receive rings. */
212#define TX_RING_SIZE 16
213#define TX_QUEUE_LEN 10 /* Limit ring entries actually used, min 4. */
214#define RX_RING_SIZE 32
215
216/* Operational parameters that usually are not changed. */
217/* Time in jiffies before concluding the transmitter is hung. */
218#define TX_TIMEOUT (2*HZ)
219
220#define NATSEMI_HW_TIMEOUT 400
221#define NATSEMI_TIMER_FREQ 3*HZ
222#define NATSEMI_PG0_NREGS 64
223#define NATSEMI_RFDR_NREGS 8
224#define NATSEMI_PG1_NREGS 4
225#define NATSEMI_NREGS (NATSEMI_PG0_NREGS + NATSEMI_RFDR_NREGS + \
226 NATSEMI_PG1_NREGS)
227#define NATSEMI_REGS_VER 1 /* v1 added RFDR registers */
228#define NATSEMI_REGS_SIZE (NATSEMI_NREGS * sizeof(u32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230/* Buffer sizes:
231 * The nic writes 32-bit values, even if the upper bytes of
232 * a 32-bit value are beyond the end of the buffer.
233 */
234#define NATSEMI_HEADERS 22 /* 2*mac,type,vlan,crc */
235#define NATSEMI_PADDING 16 /* 2 bytes should be sufficient */
236#define NATSEMI_LONGPKT 1518 /* limit for normal packets */
237#define NATSEMI_RX_LIMIT 2046 /* maximum supported by hardware */
238
239/* These identify the driver base version and may not be removed. */
Randy Dunlape19360f2006-04-10 23:22:06 -0700240static const char version[] __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 KERN_INFO DRV_NAME " dp8381x driver, version "
242 DRV_VERSION ", " DRV_RELDATE "\n"
243 KERN_INFO " originally by Donald Becker <becker@scyld.com>\n"
244 KERN_INFO " http://www.scyld.com/network/natsemi.html\n"
245 KERN_INFO " 2.4.x kernel port by Jeff Garzik, Tjeerd Mulder\n";
246
247MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
248MODULE_DESCRIPTION("National Semiconductor DP8381x series PCI Ethernet driver");
249MODULE_LICENSE("GPL");
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251module_param(mtu, int, 0);
252module_param(debug, int, 0);
253module_param(rx_copybreak, int, 0);
254module_param_array(options, int, NULL, 0);
255module_param_array(full_duplex, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256MODULE_PARM_DESC(mtu, "DP8381x MTU (all boards)");
257MODULE_PARM_DESC(debug, "DP8381x default debug level");
258MODULE_PARM_DESC(rx_copybreak,
259 "DP8381x copy breakpoint for copy-only-tiny-frames");
260MODULE_PARM_DESC(options,
261 "DP8381x: Bits 0-3: media type, bit 17: full duplex");
262MODULE_PARM_DESC(full_duplex, "DP8381x full duplex setting(s) (1)");
263
264/*
265 Theory of Operation
266
267I. Board Compatibility
268
269This driver is designed for National Semiconductor DP83815 PCI Ethernet NIC.
270It also works with other chips in in the DP83810 series.
271
272II. Board-specific settings
273
274This driver requires the PCI interrupt line to be valid.
275It honors the EEPROM-set values.
276
277III. Driver operation
278
279IIIa. Ring buffers
280
281This driver uses two statically allocated fixed-size descriptor lists
282formed into rings by a branch from the final descriptor to the beginning of
283the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
284The NatSemi design uses a 'next descriptor' pointer that the driver forms
285into a list.
286
287IIIb/c. Transmit/Receive Structure
288
289This driver uses a zero-copy receive and transmit scheme.
290The driver allocates full frame size skbuffs for the Rx ring buffers at
291open() time and passes the skb->data field to the chip as receive data
292buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
293a fresh skbuff is allocated and the frame is copied to the new skbuff.
294When the incoming frame is larger, the skbuff is passed directly up the
295protocol stack. Buffers consumed this way are replaced by newly allocated
296skbuffs in a later phase of receives.
297
298The RX_COPYBREAK value is chosen to trade-off the memory wasted by
299using a full-sized skbuff for small frames vs. the copying costs of larger
300frames. New boards are typically used in generously configured machines
301and the underfilled buffers have negligible impact compared to the benefit of
302a single allocation size, so the default value of zero results in never
303copying packets. When copying is done, the cost is usually mitigated by using
304a combined copy/checksum routine. Copying also preloads the cache, which is
305most useful with small frames.
306
307A subtle aspect of the operation is that unaligned buffers are not permitted
308by the hardware. Thus the IP header at offset 14 in an ethernet frame isn't
309longword aligned for further processing. On copies frames are put into the
310skbuff at an offset of "+2", 16-byte aligning the IP header.
311
312IIId. Synchronization
313
314Most operations are synchronized on the np->lock irq spinlock, except the
315performance critical codepaths:
316
317The rx process only runs in the interrupt handler. Access from outside
318the interrupt handler is only permitted after disable_irq().
319
Herbert Xu932ff272006-06-09 12:20:56 -0700320The rx process usually runs under the netif_tx_lock. If np->intr_tx_reap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321is set, then access is permitted under spin_lock_irq(&np->lock).
322
323Thus configuration functions that want to access everything must call
324 disable_irq(dev->irq);
Herbert Xu932ff272006-06-09 12:20:56 -0700325 netif_tx_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 spin_lock_irq(&np->lock);
327
328IV. Notes
329
330NatSemi PCI network controllers are very uncommon.
331
332IVb. References
333
334http://www.scyld.com/expert/100mbps.html
335http://www.scyld.com/expert/NWay.html
336Datasheet is available from:
337http://www.national.com/pf/DP/DP83815.html
338
339IVc. Errata
340
341None characterised.
342*/
343
344
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/*
347 * Support for fibre connections on Am79C874:
348 * This phy needs a special setup when connected to a fibre cable.
349 * http://www.amd.com/files/connectivitysolutions/networking/archivednetworking/22235.pdf
350 */
351#define PHYID_AM79C874 0x0022561b
352
Jeff Garzika2b524b2006-06-26 22:48:38 -0400353enum {
354 MII_MCTRL = 0x15, /* mode control register */
355 MII_FX_SEL = 0x0001, /* 100BASE-FX (fiber) */
356 MII_EN_SCRM = 0x0004, /* enable scrambler (tp) */
357};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359
360/* array of board data directly indexed by pci_tbl[x].driver_data */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500361static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 const char *name;
363 unsigned long flags;
Jeff Garzika2b524b2006-06-26 22:48:38 -0400364 unsigned int eeprom_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365} natsemi_pci_info[] __devinitdata = {
Jeff Garzika2b524b2006-06-26 22:48:38 -0400366 { "NatSemi DP8381[56]", 0, 24 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367};
368
Jeff Garzika2b524b2006-06-26 22:48:38 -0400369static const struct pci_device_id natsemi_pci_tbl[] __devinitdata = {
370 { PCI_VENDOR_ID_NS, 0x0020, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
371 { } /* terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373MODULE_DEVICE_TABLE(pci, natsemi_pci_tbl);
374
375/* Offsets to the device registers.
376 Unlike software-only systems, device drivers interact with complex hardware.
377 It's not useful to define symbolic names for every register bit in the
378 device.
379*/
380enum register_offsets {
381 ChipCmd = 0x00,
382 ChipConfig = 0x04,
383 EECtrl = 0x08,
384 PCIBusCfg = 0x0C,
385 IntrStatus = 0x10,
386 IntrMask = 0x14,
387 IntrEnable = 0x18,
388 IntrHoldoff = 0x1C, /* DP83816 only */
389 TxRingPtr = 0x20,
390 TxConfig = 0x24,
391 RxRingPtr = 0x30,
392 RxConfig = 0x34,
393 ClkRun = 0x3C,
394 WOLCmd = 0x40,
395 PauseCmd = 0x44,
396 RxFilterAddr = 0x48,
397 RxFilterData = 0x4C,
398 BootRomAddr = 0x50,
399 BootRomData = 0x54,
400 SiliconRev = 0x58,
401 StatsCtrl = 0x5C,
402 StatsData = 0x60,
403 RxPktErrs = 0x60,
404 RxMissed = 0x68,
405 RxCRCErrs = 0x64,
406 BasicControl = 0x80,
407 BasicStatus = 0x84,
408 AnegAdv = 0x90,
409 AnegPeer = 0x94,
410 PhyStatus = 0xC0,
411 MIntrCtrl = 0xC4,
412 MIntrStatus = 0xC8,
413 PhyCtrl = 0xE4,
414
415 /* These are from the spec, around page 78... on a separate table.
416 * The meaning of these registers depend on the value of PGSEL. */
417 PGSEL = 0xCC,
418 PMDCSR = 0xE4,
419 TSTDAT = 0xFC,
420 DSPCFG = 0xF4,
421 SDCFG = 0xF8
422};
423/* the values for the 'magic' registers above (PGSEL=1) */
424#define PMDCSR_VAL 0x189c /* enable preferred adaptation circuitry */
425#define TSTDAT_VAL 0x0
426#define DSPCFG_VAL 0x5040
427#define SDCFG_VAL 0x008c /* set voltage thresholds for Signal Detect */
428#define DSPCFG_LOCK 0x20 /* coefficient lock bit in DSPCFG */
429#define DSPCFG_COEF 0x1000 /* see coefficient (in TSTDAT) bit in DSPCFG */
430#define TSTDAT_FIXED 0xe8 /* magic number for bad coefficients */
431
432/* misc PCI space registers */
433enum pci_register_offsets {
434 PCIPM = 0x44,
435};
436
437enum ChipCmd_bits {
438 ChipReset = 0x100,
439 RxReset = 0x20,
440 TxReset = 0x10,
441 RxOff = 0x08,
442 RxOn = 0x04,
443 TxOff = 0x02,
444 TxOn = 0x01,
445};
446
447enum ChipConfig_bits {
448 CfgPhyDis = 0x200,
449 CfgPhyRst = 0x400,
450 CfgExtPhy = 0x1000,
451 CfgAnegEnable = 0x2000,
452 CfgAneg100 = 0x4000,
453 CfgAnegFull = 0x8000,
454 CfgAnegDone = 0x8000000,
455 CfgFullDuplex = 0x20000000,
456 CfgSpeed100 = 0x40000000,
457 CfgLink = 0x80000000,
458};
459
460enum EECtrl_bits {
461 EE_ShiftClk = 0x04,
462 EE_DataIn = 0x01,
463 EE_ChipSelect = 0x08,
464 EE_DataOut = 0x02,
465 MII_Data = 0x10,
466 MII_Write = 0x20,
467 MII_ShiftClk = 0x40,
468};
469
470enum PCIBusCfg_bits {
471 EepromReload = 0x4,
472};
473
474/* Bits in the interrupt status/mask registers. */
475enum IntrStatus_bits {
476 IntrRxDone = 0x0001,
477 IntrRxIntr = 0x0002,
478 IntrRxErr = 0x0004,
479 IntrRxEarly = 0x0008,
480 IntrRxIdle = 0x0010,
481 IntrRxOverrun = 0x0020,
482 IntrTxDone = 0x0040,
483 IntrTxIntr = 0x0080,
484 IntrTxErr = 0x0100,
485 IntrTxIdle = 0x0200,
486 IntrTxUnderrun = 0x0400,
487 StatsMax = 0x0800,
488 SWInt = 0x1000,
489 WOLPkt = 0x2000,
490 LinkChange = 0x4000,
491 IntrHighBits = 0x8000,
492 RxStatusFIFOOver = 0x10000,
493 IntrPCIErr = 0xf00000,
494 RxResetDone = 0x1000000,
495 TxResetDone = 0x2000000,
496 IntrAbnormalSummary = 0xCD20,
497};
498
499/*
500 * Default Interrupts:
501 * Rx OK, Rx Packet Error, Rx Overrun,
502 * Tx OK, Tx Packet Error, Tx Underrun,
503 * MIB Service, Phy Interrupt, High Bits,
504 * Rx Status FIFO overrun,
505 * Received Target Abort, Received Master Abort,
506 * Signalled System Error, Received Parity Error
507 */
508#define DEFAULT_INTR 0x00f1cd65
509
510enum TxConfig_bits {
511 TxDrthMask = 0x3f,
512 TxFlthMask = 0x3f00,
513 TxMxdmaMask = 0x700000,
514 TxMxdma_512 = 0x0,
515 TxMxdma_4 = 0x100000,
516 TxMxdma_8 = 0x200000,
517 TxMxdma_16 = 0x300000,
518 TxMxdma_32 = 0x400000,
519 TxMxdma_64 = 0x500000,
520 TxMxdma_128 = 0x600000,
521 TxMxdma_256 = 0x700000,
522 TxCollRetry = 0x800000,
523 TxAutoPad = 0x10000000,
524 TxMacLoop = 0x20000000,
525 TxHeartIgn = 0x40000000,
526 TxCarrierIgn = 0x80000000
527};
528
529/*
530 * Tx Configuration:
531 * - 256 byte DMA burst length
532 * - fill threshold 512 bytes (i.e. restart DMA when 512 bytes are free)
533 * - 64 bytes initial drain threshold (i.e. begin actual transmission
534 * when 64 byte are in the fifo)
535 * - on tx underruns, increase drain threshold by 64.
536 * - at most use a drain threshold of 1472 bytes: The sum of the fill
537 * threshold and the drain threshold must be less than 2016 bytes.
538 *
539 */
540#define TX_FLTH_VAL ((512/32) << 8)
541#define TX_DRTH_VAL_START (64/32)
542#define TX_DRTH_VAL_INC 2
543#define TX_DRTH_VAL_LIMIT (1472/32)
544
545enum RxConfig_bits {
546 RxDrthMask = 0x3e,
547 RxMxdmaMask = 0x700000,
548 RxMxdma_512 = 0x0,
549 RxMxdma_4 = 0x100000,
550 RxMxdma_8 = 0x200000,
551 RxMxdma_16 = 0x300000,
552 RxMxdma_32 = 0x400000,
553 RxMxdma_64 = 0x500000,
554 RxMxdma_128 = 0x600000,
555 RxMxdma_256 = 0x700000,
556 RxAcceptLong = 0x8000000,
557 RxAcceptTx = 0x10000000,
558 RxAcceptRunt = 0x40000000,
559 RxAcceptErr = 0x80000000
560};
561#define RX_DRTH_VAL (128/8)
562
563enum ClkRun_bits {
564 PMEEnable = 0x100,
565 PMEStatus = 0x8000,
566};
567
568enum WolCmd_bits {
569 WakePhy = 0x1,
570 WakeUnicast = 0x2,
571 WakeMulticast = 0x4,
572 WakeBroadcast = 0x8,
573 WakeArp = 0x10,
574 WakePMatch0 = 0x20,
575 WakePMatch1 = 0x40,
576 WakePMatch2 = 0x80,
577 WakePMatch3 = 0x100,
578 WakeMagic = 0x200,
579 WakeMagicSecure = 0x400,
580 SecureHack = 0x100000,
581 WokePhy = 0x400000,
582 WokeUnicast = 0x800000,
583 WokeMulticast = 0x1000000,
584 WokeBroadcast = 0x2000000,
585 WokeArp = 0x4000000,
586 WokePMatch0 = 0x8000000,
587 WokePMatch1 = 0x10000000,
588 WokePMatch2 = 0x20000000,
589 WokePMatch3 = 0x40000000,
590 WokeMagic = 0x80000000,
591 WakeOptsSummary = 0x7ff
592};
593
594enum RxFilterAddr_bits {
595 RFCRAddressMask = 0x3ff,
596 AcceptMulticast = 0x00200000,
597 AcceptMyPhys = 0x08000000,
598 AcceptAllPhys = 0x10000000,
599 AcceptAllMulticast = 0x20000000,
600 AcceptBroadcast = 0x40000000,
601 RxFilterEnable = 0x80000000
602};
603
604enum StatsCtrl_bits {
605 StatsWarn = 0x1,
606 StatsFreeze = 0x2,
607 StatsClear = 0x4,
608 StatsStrobe = 0x8,
609};
610
611enum MIntrCtrl_bits {
612 MICRIntEn = 0x2,
613};
614
615enum PhyCtrl_bits {
616 PhyAddrMask = 0x1f,
617};
618
619#define PHY_ADDR_NONE 32
620#define PHY_ADDR_INTERNAL 1
621
622/* values we might find in the silicon revision register */
623#define SRR_DP83815_C 0x0302
624#define SRR_DP83815_D 0x0403
625#define SRR_DP83816_A4 0x0504
626#define SRR_DP83816_A5 0x0505
627
628/* The Rx and Tx buffer descriptors. */
629/* Note that using only 32 bit fields simplifies conversion to big-endian
630 architectures. */
631struct netdev_desc {
632 u32 next_desc;
633 s32 cmd_status;
634 u32 addr;
635 u32 software_use;
636};
637
638/* Bits in network_desc.status */
639enum desc_status_bits {
640 DescOwn=0x80000000, DescMore=0x40000000, DescIntr=0x20000000,
641 DescNoCRC=0x10000000, DescPktOK=0x08000000,
642 DescSizeMask=0xfff,
643
644 DescTxAbort=0x04000000, DescTxFIFO=0x02000000,
645 DescTxCarrier=0x01000000, DescTxDefer=0x00800000,
646 DescTxExcDefer=0x00400000, DescTxOOWCol=0x00200000,
647 DescTxExcColl=0x00100000, DescTxCollCount=0x000f0000,
648
649 DescRxAbort=0x04000000, DescRxOver=0x02000000,
650 DescRxDest=0x01800000, DescRxLong=0x00400000,
651 DescRxRunt=0x00200000, DescRxInvalid=0x00100000,
652 DescRxCRC=0x00080000, DescRxAlign=0x00040000,
653 DescRxLoop=0x00020000, DesRxColl=0x00010000,
654};
655
656struct netdev_private {
657 /* Descriptor rings first for alignment */
658 dma_addr_t ring_dma;
659 struct netdev_desc *rx_ring;
660 struct netdev_desc *tx_ring;
661 /* The addresses of receive-in-place skbuffs */
662 struct sk_buff *rx_skbuff[RX_RING_SIZE];
663 dma_addr_t rx_dma[RX_RING_SIZE];
664 /* address of a sent-in-place packet/buffer, for later free() */
665 struct sk_buff *tx_skbuff[TX_RING_SIZE];
666 dma_addr_t tx_dma[TX_RING_SIZE];
667 struct net_device_stats stats;
668 /* Media monitoring timer */
669 struct timer_list timer;
670 /* Frequently used values: keep some adjacent for cache effect */
671 struct pci_dev *pci_dev;
672 struct netdev_desc *rx_head_desc;
673 /* Producer/consumer ring indices */
674 unsigned int cur_rx, dirty_rx;
675 unsigned int cur_tx, dirty_tx;
676 /* Based on MTU+slack. */
677 unsigned int rx_buf_sz;
678 int oom;
Mark Brownb27a16b2006-02-02 00:00:01 +0000679 /* Interrupt status */
680 u32 intr_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Do not touch the nic registers */
682 int hands_off;
683 /* external phy that is used: only valid if dev->if_port != PORT_TP */
684 int mii;
685 int phy_addr_external;
686 unsigned int full_duplex;
687 /* Rx filter */
688 u32 cur_rx_mode;
689 u32 rx_filter[16];
690 /* FIFO and PCI burst thresholds */
691 u32 tx_config, rx_config;
692 /* original contents of ClkRun register */
693 u32 SavedClkRun;
694 /* silicon revision */
695 u32 srr;
696 /* expected DSPCFG value */
697 u16 dspcfg;
698 /* parms saved in ethtool format */
699 u16 speed; /* The forced speed, 10Mb, 100Mb, gigabit */
700 u8 duplex; /* Duplex, half or full */
701 u8 autoneg; /* Autonegotiation enabled */
702 /* MII transceiver section */
703 u16 advertising;
704 unsigned int iosize;
705 spinlock_t lock;
706 u32 msg_enable;
Mark Browna8b4cf42006-03-28 14:08:55 -0800707 /* EEPROM data */
708 int eeprom_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709};
710
711static void move_int_phy(struct net_device *dev, int addr);
712static int eeprom_read(void __iomem *ioaddr, int location);
713static int mdio_read(struct net_device *dev, int reg);
714static void mdio_write(struct net_device *dev, int reg, u16 data);
715static void init_phy_fixup(struct net_device *dev);
716static int miiport_read(struct net_device *dev, int phy_id, int reg);
717static void miiport_write(struct net_device *dev, int phy_id, int reg, u16 data);
718static int find_mii(struct net_device *dev);
719static void natsemi_reset(struct net_device *dev);
720static void natsemi_reload_eeprom(struct net_device *dev);
721static void natsemi_stop_rxtx(struct net_device *dev);
722static int netdev_open(struct net_device *dev);
723static void do_cable_magic(struct net_device *dev);
724static void undo_cable_magic(struct net_device *dev);
725static void check_link(struct net_device *dev);
726static void netdev_timer(unsigned long data);
727static void dump_ring(struct net_device *dev);
728static void tx_timeout(struct net_device *dev);
729static int alloc_ring(struct net_device *dev);
730static void refill_rx(struct net_device *dev);
731static void init_ring(struct net_device *dev);
732static void drain_tx(struct net_device *dev);
733static void drain_ring(struct net_device *dev);
734static void free_ring(struct net_device *dev);
735static void reinit_ring(struct net_device *dev);
736static void init_registers(struct net_device *dev);
737static int start_tx(struct sk_buff *skb, struct net_device *dev);
738static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
739static void netdev_error(struct net_device *dev, int intr_status);
Mark Brownb27a16b2006-02-02 00:00:01 +0000740static int natsemi_poll(struct net_device *dev, int *budget);
741static void netdev_rx(struct net_device *dev, int *work_done, int work_to_do);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742static void netdev_tx_done(struct net_device *dev);
743static int natsemi_change_mtu(struct net_device *dev, int new_mtu);
744#ifdef CONFIG_NET_POLL_CONTROLLER
745static void natsemi_poll_controller(struct net_device *dev);
746#endif
747static void __set_rx_mode(struct net_device *dev);
748static void set_rx_mode(struct net_device *dev);
749static void __get_stats(struct net_device *dev);
750static struct net_device_stats *get_stats(struct net_device *dev);
751static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
752static int netdev_set_wol(struct net_device *dev, u32 newval);
753static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur);
754static int netdev_set_sopass(struct net_device *dev, u8 *newval);
755static int netdev_get_sopass(struct net_device *dev, u8 *data);
756static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
757static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
758static void enable_wol_mode(struct net_device *dev, int enable_intr);
759static int netdev_close(struct net_device *dev);
760static int netdev_get_regs(struct net_device *dev, u8 *buf);
761static int netdev_get_eeprom(struct net_device *dev, u8 *buf);
762static struct ethtool_ops ethtool_ops;
763
764static inline void __iomem *ns_ioaddr(struct net_device *dev)
765{
766 return (void __iomem *) dev->base_addr;
767}
768
Mark Brownb27a16b2006-02-02 00:00:01 +0000769static inline void natsemi_irq_enable(struct net_device *dev)
770{
771 writel(1, ns_ioaddr(dev) + IntrEnable);
772 readl(ns_ioaddr(dev) + IntrEnable);
773}
774
775static inline void natsemi_irq_disable(struct net_device *dev)
776{
777 writel(0, ns_ioaddr(dev) + IntrEnable);
778 readl(ns_ioaddr(dev) + IntrEnable);
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static void move_int_phy(struct net_device *dev, int addr)
782{
783 struct netdev_private *np = netdev_priv(dev);
784 void __iomem *ioaddr = ns_ioaddr(dev);
785 int target = 31;
786
787 /*
788 * The internal phy is visible on the external mii bus. Therefore we must
789 * move it away before we can send commands to an external phy.
790 * There are two addresses we must avoid:
791 * - the address on the external phy that is used for transmission.
792 * - the address that we want to access. User space can access phys
793 * on the mii bus with SIOCGMIIREG/SIOCSMIIREG, independant from the
794 * phy that is used for transmission.
795 */
796
797 if (target == addr)
798 target--;
799 if (target == np->phy_addr_external)
800 target--;
801 writew(target, ioaddr + PhyCtrl);
802 readw(ioaddr + PhyCtrl);
803 udelay(1);
804}
805
Jeff Garzik5a40f092006-06-26 22:24:03 -0400806static void __devinit natsemi_init_media (struct net_device *dev)
807{
808 struct netdev_private *np = netdev_priv(dev);
809 u32 tmp;
810
811 netif_carrier_off(dev);
812
813 /* get the initial settings from hardware */
814 tmp = mdio_read(dev, MII_BMCR);
815 np->speed = (tmp & BMCR_SPEED100)? SPEED_100 : SPEED_10;
816 np->duplex = (tmp & BMCR_FULLDPLX)? DUPLEX_FULL : DUPLEX_HALF;
817 np->autoneg = (tmp & BMCR_ANENABLE)? AUTONEG_ENABLE: AUTONEG_DISABLE;
818 np->advertising= mdio_read(dev, MII_ADVERTISE);
819
820 if ((np->advertising & ADVERTISE_ALL) != ADVERTISE_ALL
821 && netif_msg_probe(np)) {
822 printk(KERN_INFO "natsemi %s: Transceiver default autonegotiation %s "
823 "10%s %s duplex.\n",
824 pci_name(np->pci_dev),
825 (mdio_read(dev, MII_BMCR) & BMCR_ANENABLE)?
826 "enabled, advertise" : "disabled, force",
827 (np->advertising &
828 (ADVERTISE_100FULL|ADVERTISE_100HALF))?
829 "0" : "",
830 (np->advertising &
831 (ADVERTISE_100FULL|ADVERTISE_10FULL))?
832 "full" : "half");
833 }
834 if (netif_msg_probe(np))
835 printk(KERN_INFO
836 "natsemi %s: Transceiver status %#04x advertising %#04x.\n",
837 pci_name(np->pci_dev), mdio_read(dev, MII_BMSR),
838 np->advertising);
839
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842static int __devinit natsemi_probe1 (struct pci_dev *pdev,
843 const struct pci_device_id *ent)
844{
845 struct net_device *dev;
846 struct netdev_private *np;
847 int i, option, irq, chip_idx = ent->driver_data;
848 static int find_cnt = -1;
849 unsigned long iostart, iosize;
850 void __iomem *ioaddr;
851 const int pcibar = 1; /* PCI base address register */
852 int prev_eedata;
853 u32 tmp;
854
855/* when built into the kernel, we only print version if device is found */
856#ifndef MODULE
857 static int printed_version;
858 if (!printed_version++)
859 printk(version);
860#endif
861
862 i = pci_enable_device(pdev);
863 if (i) return i;
864
865 /* natsemi has a non-standard PM control register
866 * in PCI config space. Some boards apparently need
867 * to be brought to D0 in this manner.
868 */
869 pci_read_config_dword(pdev, PCIPM, &tmp);
870 if (tmp & PCI_PM_CTRL_STATE_MASK) {
871 /* D0 state, disable PME assertion */
872 u32 newtmp = tmp & ~PCI_PM_CTRL_STATE_MASK;
873 pci_write_config_dword(pdev, PCIPM, newtmp);
874 }
875
876 find_cnt++;
877 iostart = pci_resource_start(pdev, pcibar);
878 iosize = pci_resource_len(pdev, pcibar);
879 irq = pdev->irq;
880
Jeff Garzika2b524b2006-06-26 22:48:38 -0400881 pci_set_master(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 dev = alloc_etherdev(sizeof (struct netdev_private));
884 if (!dev)
885 return -ENOMEM;
886 SET_MODULE_OWNER(dev);
887 SET_NETDEV_DEV(dev, &pdev->dev);
888
889 i = pci_request_regions(pdev, DRV_NAME);
890 if (i)
891 goto err_pci_request_regions;
892
893 ioaddr = ioremap(iostart, iosize);
894 if (!ioaddr) {
895 i = -ENOMEM;
896 goto err_ioremap;
897 }
898
899 /* Work around the dropped serial bit. */
900 prev_eedata = eeprom_read(ioaddr, 6);
901 for (i = 0; i < 3; i++) {
902 int eedata = eeprom_read(ioaddr, i + 7);
903 dev->dev_addr[i*2] = (eedata << 1) + (prev_eedata >> 15);
904 dev->dev_addr[i*2+1] = eedata >> 7;
905 prev_eedata = eedata;
906 }
907
908 dev->base_addr = (unsigned long __force) ioaddr;
909 dev->irq = irq;
910
911 np = netdev_priv(dev);
912
913 np->pci_dev = pdev;
914 pci_set_drvdata(pdev, dev);
915 np->iosize = iosize;
916 spin_lock_init(&np->lock);
917 np->msg_enable = (debug >= 0) ? (1<<debug)-1 : NATSEMI_DEF_MSG;
918 np->hands_off = 0;
Mark Brownb27a16b2006-02-02 00:00:01 +0000919 np->intr_status = 0;
Jeff Garzika2b524b2006-06-26 22:48:38 -0400920 np->eeprom_size = natsemi_pci_info[chip_idx].eeprom_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* Initial port:
923 * - If the nic was configured to use an external phy and if find_mii
924 * finds a phy: use external port, first phy that replies.
925 * - Otherwise: internal port.
926 * Note that the phy address for the internal phy doesn't matter:
927 * The address would be used to access a phy over the mii bus, but
928 * the internal phy is accessed through mapped registers.
929 */
930 if (readl(ioaddr + ChipConfig) & CfgExtPhy)
931 dev->if_port = PORT_MII;
932 else
933 dev->if_port = PORT_TP;
934 /* Reset the chip to erase previous misconfiguration. */
935 natsemi_reload_eeprom(dev);
936 natsemi_reset(dev);
937
938 if (dev->if_port != PORT_TP) {
939 np->phy_addr_external = find_mii(dev);
940 if (np->phy_addr_external == PHY_ADDR_NONE) {
941 dev->if_port = PORT_TP;
942 np->phy_addr_external = PHY_ADDR_INTERNAL;
943 }
944 } else {
945 np->phy_addr_external = PHY_ADDR_INTERNAL;
946 }
947
948 option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
949 if (dev->mem_start)
950 option = dev->mem_start;
951
952 /* The lower four bits are the media type. */
953 if (option) {
954 if (option & 0x200)
955 np->full_duplex = 1;
956 if (option & 15)
957 printk(KERN_INFO
958 "natsemi %s: ignoring user supplied media type %d",
959 pci_name(np->pci_dev), option & 15);
960 }
961 if (find_cnt < MAX_UNITS && full_duplex[find_cnt])
962 np->full_duplex = 1;
963
964 /* The chip-specific entries in the device structure. */
965 dev->open = &netdev_open;
966 dev->hard_start_xmit = &start_tx;
967 dev->stop = &netdev_close;
968 dev->get_stats = &get_stats;
969 dev->set_multicast_list = &set_rx_mode;
970 dev->change_mtu = &natsemi_change_mtu;
971 dev->do_ioctl = &netdev_ioctl;
972 dev->tx_timeout = &tx_timeout;
973 dev->watchdog_timeo = TX_TIMEOUT;
Mark Brownb27a16b2006-02-02 00:00:01 +0000974 dev->poll = natsemi_poll;
975 dev->weight = 64;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#ifdef CONFIG_NET_POLL_CONTROLLER
978 dev->poll_controller = &natsemi_poll_controller;
979#endif
980 SET_ETHTOOL_OPS(dev, &ethtool_ops);
981
982 if (mtu)
983 dev->mtu = mtu;
984
Jeff Garzik5a40f092006-06-26 22:24:03 -0400985 natsemi_init_media(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* save the silicon revision for later querying */
988 np->srr = readl(ioaddr + SiliconRev);
989 if (netif_msg_hw(np))
990 printk(KERN_INFO "natsemi %s: silicon revision %#04x.\n",
991 pci_name(np->pci_dev), np->srr);
992
993 i = register_netdev(dev);
994 if (i)
995 goto err_register_netdev;
996
997 if (netif_msg_drv(np)) {
998 printk(KERN_INFO "natsemi %s: %s at %#08lx (%s), ",
999 dev->name, natsemi_pci_info[chip_idx].name, iostart,
1000 pci_name(np->pci_dev));
1001 for (i = 0; i < ETH_ALEN-1; i++)
1002 printk("%02x:", dev->dev_addr[i]);
1003 printk("%02x, IRQ %d", dev->dev_addr[i], irq);
1004 if (dev->if_port == PORT_TP)
1005 printk(", port TP.\n");
1006 else
1007 printk(", port MII, phy ad %d.\n", np->phy_addr_external);
1008 }
1009 return 0;
1010
1011 err_register_netdev:
1012 iounmap(ioaddr);
1013
1014 err_ioremap:
1015 pci_release_regions(pdev);
1016 pci_set_drvdata(pdev, NULL);
1017
1018 err_pci_request_regions:
1019 free_netdev(dev);
1020 return i;
1021}
1022
1023
1024/* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
1025 The EEPROM code is for the common 93c06/46 EEPROMs with 6 bit addresses. */
1026
1027/* Delay between EEPROM clock transitions.
1028 No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
1029 a delay. Note that pre-2.0.34 kernels had a cache-alignment bug that
1030 made udelay() unreliable.
1031 The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
1032 depricated.
1033*/
1034#define eeprom_delay(ee_addr) readl(ee_addr)
1035
1036#define EE_Write0 (EE_ChipSelect)
1037#define EE_Write1 (EE_ChipSelect | EE_DataIn)
1038
1039/* The EEPROM commands include the alway-set leading bit. */
1040enum EEPROM_Cmds {
1041 EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
1042};
1043
1044static int eeprom_read(void __iomem *addr, int location)
1045{
1046 int i;
1047 int retval = 0;
1048 void __iomem *ee_addr = addr + EECtrl;
1049 int read_cmd = location | EE_ReadCmd;
1050
1051 writel(EE_Write0, ee_addr);
1052
1053 /* Shift the read command bits out. */
1054 for (i = 10; i >= 0; i--) {
1055 short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
1056 writel(dataval, ee_addr);
1057 eeprom_delay(ee_addr);
1058 writel(dataval | EE_ShiftClk, ee_addr);
1059 eeprom_delay(ee_addr);
1060 }
1061 writel(EE_ChipSelect, ee_addr);
1062 eeprom_delay(ee_addr);
1063
1064 for (i = 0; i < 16; i++) {
1065 writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
1066 eeprom_delay(ee_addr);
1067 retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0;
1068 writel(EE_ChipSelect, ee_addr);
1069 eeprom_delay(ee_addr);
1070 }
1071
1072 /* Terminate the EEPROM access. */
1073 writel(EE_Write0, ee_addr);
1074 writel(0, ee_addr);
1075 return retval;
1076}
1077
1078/* MII transceiver control section.
1079 * The 83815 series has an internal transceiver, and we present the
1080 * internal management registers as if they were MII connected.
1081 * External Phy registers are referenced through the MII interface.
1082 */
1083
1084/* clock transitions >= 20ns (25MHz)
1085 * One readl should be good to PCI @ 100MHz
1086 */
1087#define mii_delay(ioaddr) readl(ioaddr + EECtrl)
1088
1089static int mii_getbit (struct net_device *dev)
1090{
1091 int data;
1092 void __iomem *ioaddr = ns_ioaddr(dev);
1093
1094 writel(MII_ShiftClk, ioaddr + EECtrl);
1095 data = readl(ioaddr + EECtrl);
1096 writel(0, ioaddr + EECtrl);
1097 mii_delay(ioaddr);
1098 return (data & MII_Data)? 1 : 0;
1099}
1100
1101static void mii_send_bits (struct net_device *dev, u32 data, int len)
1102{
1103 u32 i;
1104 void __iomem *ioaddr = ns_ioaddr(dev);
1105
1106 for (i = (1 << (len-1)); i; i >>= 1)
1107 {
1108 u32 mdio_val = MII_Write | ((data & i)? MII_Data : 0);
1109 writel(mdio_val, ioaddr + EECtrl);
1110 mii_delay(ioaddr);
1111 writel(mdio_val | MII_ShiftClk, ioaddr + EECtrl);
1112 mii_delay(ioaddr);
1113 }
1114 writel(0, ioaddr + EECtrl);
1115 mii_delay(ioaddr);
1116}
1117
1118static int miiport_read(struct net_device *dev, int phy_id, int reg)
1119{
1120 u32 cmd;
1121 int i;
1122 u32 retval = 0;
1123
1124 /* Ensure sync */
1125 mii_send_bits (dev, 0xffffffff, 32);
1126 /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
1127 /* ST,OP = 0110'b for read operation */
1128 cmd = (0x06 << 10) | (phy_id << 5) | reg;
1129 mii_send_bits (dev, cmd, 14);
1130 /* Turnaround */
1131 if (mii_getbit (dev))
1132 return 0;
1133 /* Read data */
1134 for (i = 0; i < 16; i++) {
1135 retval <<= 1;
1136 retval |= mii_getbit (dev);
1137 }
1138 /* End cycle */
1139 mii_getbit (dev);
1140 return retval;
1141}
1142
1143static void miiport_write(struct net_device *dev, int phy_id, int reg, u16 data)
1144{
1145 u32 cmd;
1146
1147 /* Ensure sync */
1148 mii_send_bits (dev, 0xffffffff, 32);
1149 /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
1150 /* ST,OP,AAAAA,RRRRR,TA = 0101xxxxxxxxxx10'b = 0x5002 for write */
1151 cmd = (0x5002 << 16) | (phy_id << 23) | (reg << 18) | data;
1152 mii_send_bits (dev, cmd, 32);
1153 /* End cycle */
1154 mii_getbit (dev);
1155}
1156
1157static int mdio_read(struct net_device *dev, int reg)
1158{
1159 struct netdev_private *np = netdev_priv(dev);
1160 void __iomem *ioaddr = ns_ioaddr(dev);
1161
1162 /* The 83815 series has two ports:
1163 * - an internal transceiver
1164 * - an external mii bus
1165 */
1166 if (dev->if_port == PORT_TP)
1167 return readw(ioaddr+BasicControl+(reg<<2));
1168 else
1169 return miiport_read(dev, np->phy_addr_external, reg);
1170}
1171
1172static void mdio_write(struct net_device *dev, int reg, u16 data)
1173{
1174 struct netdev_private *np = netdev_priv(dev);
1175 void __iomem *ioaddr = ns_ioaddr(dev);
1176
1177 /* The 83815 series has an internal transceiver; handle separately */
1178 if (dev->if_port == PORT_TP)
1179 writew(data, ioaddr+BasicControl+(reg<<2));
1180 else
1181 miiport_write(dev, np->phy_addr_external, reg, data);
1182}
1183
1184static void init_phy_fixup(struct net_device *dev)
1185{
1186 struct netdev_private *np = netdev_priv(dev);
1187 void __iomem *ioaddr = ns_ioaddr(dev);
1188 int i;
1189 u32 cfg;
1190 u16 tmp;
1191
1192 /* restore stuff lost when power was out */
1193 tmp = mdio_read(dev, MII_BMCR);
1194 if (np->autoneg == AUTONEG_ENABLE) {
1195 /* renegotiate if something changed */
1196 if ((tmp & BMCR_ANENABLE) == 0
1197 || np->advertising != mdio_read(dev, MII_ADVERTISE))
1198 {
1199 /* turn on autonegotiation and force negotiation */
1200 tmp |= (BMCR_ANENABLE | BMCR_ANRESTART);
1201 mdio_write(dev, MII_ADVERTISE, np->advertising);
1202 }
1203 } else {
1204 /* turn off auto negotiation, set speed and duplexity */
1205 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
1206 if (np->speed == SPEED_100)
1207 tmp |= BMCR_SPEED100;
1208 if (np->duplex == DUPLEX_FULL)
1209 tmp |= BMCR_FULLDPLX;
1210 /*
1211 * Note: there is no good way to inform the link partner
1212 * that our capabilities changed. The user has to unplug
1213 * and replug the network cable after some changes, e.g.
1214 * after switching from 10HD, autoneg off to 100 HD,
1215 * autoneg off.
1216 */
1217 }
1218 mdio_write(dev, MII_BMCR, tmp);
1219 readl(ioaddr + ChipConfig);
1220 udelay(1);
1221
1222 /* find out what phy this is */
1223 np->mii = (mdio_read(dev, MII_PHYSID1) << 16)
1224 + mdio_read(dev, MII_PHYSID2);
1225
1226 /* handle external phys here */
1227 switch (np->mii) {
1228 case PHYID_AM79C874:
1229 /* phy specific configuration for fibre/tp operation */
1230 tmp = mdio_read(dev, MII_MCTRL);
1231 tmp &= ~(MII_FX_SEL | MII_EN_SCRM);
1232 if (dev->if_port == PORT_FIBRE)
1233 tmp |= MII_FX_SEL;
1234 else
1235 tmp |= MII_EN_SCRM;
1236 mdio_write(dev, MII_MCTRL, tmp);
1237 break;
1238 default:
1239 break;
1240 }
1241 cfg = readl(ioaddr + ChipConfig);
1242 if (cfg & CfgExtPhy)
1243 return;
1244
1245 /* On page 78 of the spec, they recommend some settings for "optimum
1246 performance" to be done in sequence. These settings optimize some
1247 of the 100Mbit autodetection circuitry. They say we only want to
1248 do this for rev C of the chip, but engineers at NSC (Bradley
1249 Kennedy) recommends always setting them. If you don't, you get
1250 errors on some autonegotiations that make the device unusable.
1251
1252 It seems that the DSP needs a few usec to reinitialize after
1253 the start of the phy. Just retry writing these values until they
1254 stick.
1255 */
1256 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1257
1258 int dspcfg;
1259 writew(1, ioaddr + PGSEL);
1260 writew(PMDCSR_VAL, ioaddr + PMDCSR);
1261 writew(TSTDAT_VAL, ioaddr + TSTDAT);
1262 np->dspcfg = (np->srr <= SRR_DP83815_C)?
1263 DSPCFG_VAL : (DSPCFG_COEF | readw(ioaddr + DSPCFG));
1264 writew(np->dspcfg, ioaddr + DSPCFG);
1265 writew(SDCFG_VAL, ioaddr + SDCFG);
1266 writew(0, ioaddr + PGSEL);
1267 readl(ioaddr + ChipConfig);
1268 udelay(10);
1269
1270 writew(1, ioaddr + PGSEL);
1271 dspcfg = readw(ioaddr + DSPCFG);
1272 writew(0, ioaddr + PGSEL);
1273 if (np->dspcfg == dspcfg)
1274 break;
1275 }
1276
1277 if (netif_msg_link(np)) {
1278 if (i==NATSEMI_HW_TIMEOUT) {
1279 printk(KERN_INFO
1280 "%s: DSPCFG mismatch after retrying for %d usec.\n",
1281 dev->name, i*10);
1282 } else {
1283 printk(KERN_INFO
1284 "%s: DSPCFG accepted after %d usec.\n",
1285 dev->name, i*10);
1286 }
1287 }
1288 /*
1289 * Enable PHY Specific event based interrupts. Link state change
1290 * and Auto-Negotiation Completion are among the affected.
1291 * Read the intr status to clear it (needed for wake events).
1292 */
1293 readw(ioaddr + MIntrStatus);
1294 writew(MICRIntEn, ioaddr + MIntrCtrl);
1295}
1296
1297static int switch_port_external(struct net_device *dev)
1298{
1299 struct netdev_private *np = netdev_priv(dev);
1300 void __iomem *ioaddr = ns_ioaddr(dev);
1301 u32 cfg;
1302
1303 cfg = readl(ioaddr + ChipConfig);
1304 if (cfg & CfgExtPhy)
1305 return 0;
1306
1307 if (netif_msg_link(np)) {
1308 printk(KERN_INFO "%s: switching to external transceiver.\n",
1309 dev->name);
1310 }
1311
1312 /* 1) switch back to external phy */
1313 writel(cfg | (CfgExtPhy | CfgPhyDis), ioaddr + ChipConfig);
1314 readl(ioaddr + ChipConfig);
1315 udelay(1);
1316
1317 /* 2) reset the external phy: */
1318 /* resetting the external PHY has been known to cause a hub supplying
1319 * power over Ethernet to kill the power. We don't want to kill
1320 * power to this computer, so we avoid resetting the phy.
1321 */
1322
1323 /* 3) reinit the phy fixup, it got lost during power down. */
1324 move_int_phy(dev, np->phy_addr_external);
1325 init_phy_fixup(dev);
1326
1327 return 1;
1328}
1329
1330static int switch_port_internal(struct net_device *dev)
1331{
1332 struct netdev_private *np = netdev_priv(dev);
1333 void __iomem *ioaddr = ns_ioaddr(dev);
1334 int i;
1335 u32 cfg;
1336 u16 bmcr;
1337
1338 cfg = readl(ioaddr + ChipConfig);
1339 if (!(cfg &CfgExtPhy))
1340 return 0;
1341
1342 if (netif_msg_link(np)) {
1343 printk(KERN_INFO "%s: switching to internal transceiver.\n",
1344 dev->name);
1345 }
1346 /* 1) switch back to internal phy: */
1347 cfg = cfg & ~(CfgExtPhy | CfgPhyDis);
1348 writel(cfg, ioaddr + ChipConfig);
1349 readl(ioaddr + ChipConfig);
1350 udelay(1);
1351
1352 /* 2) reset the internal phy: */
1353 bmcr = readw(ioaddr+BasicControl+(MII_BMCR<<2));
1354 writel(bmcr | BMCR_RESET, ioaddr+BasicControl+(MII_BMCR<<2));
1355 readl(ioaddr + ChipConfig);
1356 udelay(10);
1357 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1358 bmcr = readw(ioaddr+BasicControl+(MII_BMCR<<2));
1359 if (!(bmcr & BMCR_RESET))
1360 break;
1361 udelay(10);
1362 }
1363 if (i==NATSEMI_HW_TIMEOUT && netif_msg_link(np)) {
1364 printk(KERN_INFO
1365 "%s: phy reset did not complete in %d usec.\n",
1366 dev->name, i*10);
1367 }
1368 /* 3) reinit the phy fixup, it got lost during power down. */
1369 init_phy_fixup(dev);
1370
1371 return 1;
1372}
1373
1374/* Scan for a PHY on the external mii bus.
1375 * There are two tricky points:
1376 * - Do not scan while the internal phy is enabled. The internal phy will
1377 * crash: e.g. reads from the DSPCFG register will return odd values and
1378 * the nasty random phy reset code will reset the nic every few seconds.
1379 * - The internal phy must be moved around, an external phy could
1380 * have the same address as the internal phy.
1381 */
1382static int find_mii(struct net_device *dev)
1383{
1384 struct netdev_private *np = netdev_priv(dev);
1385 int tmp;
1386 int i;
1387 int did_switch;
1388
1389 /* Switch to external phy */
1390 did_switch = switch_port_external(dev);
1391
1392 /* Scan the possible phy addresses:
1393 *
1394 * PHY address 0 means that the phy is in isolate mode. Not yet
1395 * supported due to lack of test hardware. User space should
1396 * handle it through ethtool.
1397 */
1398 for (i = 1; i <= 31; i++) {
1399 move_int_phy(dev, i);
1400 tmp = miiport_read(dev, i, MII_BMSR);
1401 if (tmp != 0xffff && tmp != 0x0000) {
1402 /* found something! */
1403 np->mii = (mdio_read(dev, MII_PHYSID1) << 16)
1404 + mdio_read(dev, MII_PHYSID2);
1405 if (netif_msg_probe(np)) {
1406 printk(KERN_INFO "natsemi %s: found external phy %08x at address %d.\n",
1407 pci_name(np->pci_dev), np->mii, i);
1408 }
1409 break;
1410 }
1411 }
1412 /* And switch back to internal phy: */
1413 if (did_switch)
1414 switch_port_internal(dev);
1415 return i;
1416}
1417
1418/* CFG bits [13:16] [18:23] */
1419#define CFG_RESET_SAVE 0xfde000
1420/* WCSR bits [0:4] [9:10] */
1421#define WCSR_RESET_SAVE 0x61f
1422/* RFCR bits [20] [22] [27:31] */
1423#define RFCR_RESET_SAVE 0xf8500000;
1424
1425static void natsemi_reset(struct net_device *dev)
1426{
1427 int i;
1428 u32 cfg;
1429 u32 wcsr;
1430 u32 rfcr;
1431 u16 pmatch[3];
1432 u16 sopass[3];
1433 struct netdev_private *np = netdev_priv(dev);
1434 void __iomem *ioaddr = ns_ioaddr(dev);
1435
1436 /*
1437 * Resetting the chip causes some registers to be lost.
1438 * Natsemi suggests NOT reloading the EEPROM while live, so instead
1439 * we save the state that would have been loaded from EEPROM
1440 * on a normal power-up (see the spec EEPROM map). This assumes
1441 * whoever calls this will follow up with init_registers() eventually.
1442 */
1443
1444 /* CFG */
1445 cfg = readl(ioaddr + ChipConfig) & CFG_RESET_SAVE;
1446 /* WCSR */
1447 wcsr = readl(ioaddr + WOLCmd) & WCSR_RESET_SAVE;
1448 /* RFCR */
1449 rfcr = readl(ioaddr + RxFilterAddr) & RFCR_RESET_SAVE;
1450 /* PMATCH */
1451 for (i = 0; i < 3; i++) {
1452 writel(i*2, ioaddr + RxFilterAddr);
1453 pmatch[i] = readw(ioaddr + RxFilterData);
1454 }
1455 /* SOPAS */
1456 for (i = 0; i < 3; i++) {
1457 writel(0xa+(i*2), ioaddr + RxFilterAddr);
1458 sopass[i] = readw(ioaddr + RxFilterData);
1459 }
1460
1461 /* now whack the chip */
1462 writel(ChipReset, ioaddr + ChipCmd);
1463 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1464 if (!(readl(ioaddr + ChipCmd) & ChipReset))
1465 break;
1466 udelay(5);
1467 }
1468 if (i==NATSEMI_HW_TIMEOUT) {
1469 printk(KERN_WARNING "%s: reset did not complete in %d usec.\n",
1470 dev->name, i*5);
1471 } else if (netif_msg_hw(np)) {
1472 printk(KERN_DEBUG "%s: reset completed in %d usec.\n",
1473 dev->name, i*5);
1474 }
1475
1476 /* restore CFG */
1477 cfg |= readl(ioaddr + ChipConfig) & ~CFG_RESET_SAVE;
1478 /* turn on external phy if it was selected */
1479 if (dev->if_port == PORT_TP)
1480 cfg &= ~(CfgExtPhy | CfgPhyDis);
1481 else
1482 cfg |= (CfgExtPhy | CfgPhyDis);
1483 writel(cfg, ioaddr + ChipConfig);
1484 /* restore WCSR */
1485 wcsr |= readl(ioaddr + WOLCmd) & ~WCSR_RESET_SAVE;
1486 writel(wcsr, ioaddr + WOLCmd);
1487 /* read RFCR */
1488 rfcr |= readl(ioaddr + RxFilterAddr) & ~RFCR_RESET_SAVE;
1489 /* restore PMATCH */
1490 for (i = 0; i < 3; i++) {
1491 writel(i*2, ioaddr + RxFilterAddr);
1492 writew(pmatch[i], ioaddr + RxFilterData);
1493 }
1494 for (i = 0; i < 3; i++) {
1495 writel(0xa+(i*2), ioaddr + RxFilterAddr);
1496 writew(sopass[i], ioaddr + RxFilterData);
1497 }
1498 /* restore RFCR */
1499 writel(rfcr, ioaddr + RxFilterAddr);
1500}
1501
Mark Browne72fd962006-02-02 00:00:02 +00001502static void reset_rx(struct net_device *dev)
1503{
1504 int i;
1505 struct netdev_private *np = netdev_priv(dev);
1506 void __iomem *ioaddr = ns_ioaddr(dev);
1507
1508 np->intr_status &= ~RxResetDone;
1509
1510 writel(RxReset, ioaddr + ChipCmd);
1511
1512 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1513 np->intr_status |= readl(ioaddr + IntrStatus);
1514 if (np->intr_status & RxResetDone)
1515 break;
1516 udelay(15);
1517 }
1518 if (i==NATSEMI_HW_TIMEOUT) {
1519 printk(KERN_WARNING "%s: RX reset did not complete in %d usec.\n",
1520 dev->name, i*15);
1521 } else if (netif_msg_hw(np)) {
1522 printk(KERN_WARNING "%s: RX reset took %d usec.\n",
1523 dev->name, i*15);
1524 }
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527static void natsemi_reload_eeprom(struct net_device *dev)
1528{
1529 struct netdev_private *np = netdev_priv(dev);
1530 void __iomem *ioaddr = ns_ioaddr(dev);
1531 int i;
1532
1533 writel(EepromReload, ioaddr + PCIBusCfg);
1534 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1535 udelay(50);
1536 if (!(readl(ioaddr + PCIBusCfg) & EepromReload))
1537 break;
1538 }
1539 if (i==NATSEMI_HW_TIMEOUT) {
1540 printk(KERN_WARNING "natsemi %s: EEPROM did not reload in %d usec.\n",
1541 pci_name(np->pci_dev), i*50);
1542 } else if (netif_msg_hw(np)) {
1543 printk(KERN_DEBUG "natsemi %s: EEPROM reloaded in %d usec.\n",
1544 pci_name(np->pci_dev), i*50);
1545 }
1546}
1547
1548static void natsemi_stop_rxtx(struct net_device *dev)
1549{
1550 void __iomem * ioaddr = ns_ioaddr(dev);
1551 struct netdev_private *np = netdev_priv(dev);
1552 int i;
1553
1554 writel(RxOff | TxOff, ioaddr + ChipCmd);
1555 for(i=0;i< NATSEMI_HW_TIMEOUT;i++) {
1556 if ((readl(ioaddr + ChipCmd) & (TxOn|RxOn)) == 0)
1557 break;
1558 udelay(5);
1559 }
1560 if (i==NATSEMI_HW_TIMEOUT) {
1561 printk(KERN_WARNING "%s: Tx/Rx process did not stop in %d usec.\n",
1562 dev->name, i*5);
1563 } else if (netif_msg_hw(np)) {
1564 printk(KERN_DEBUG "%s: Tx/Rx process stopped in %d usec.\n",
1565 dev->name, i*5);
1566 }
1567}
1568
1569static int netdev_open(struct net_device *dev)
1570{
1571 struct netdev_private *np = netdev_priv(dev);
1572 void __iomem * ioaddr = ns_ioaddr(dev);
1573 int i;
1574
1575 /* Reset the chip, just in case. */
1576 natsemi_reset(dev);
1577
1578 i = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
1579 if (i) return i;
1580
1581 if (netif_msg_ifup(np))
1582 printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
1583 dev->name, dev->irq);
1584 i = alloc_ring(dev);
1585 if (i < 0) {
1586 free_irq(dev->irq, dev);
1587 return i;
1588 }
1589 init_ring(dev);
1590 spin_lock_irq(&np->lock);
1591 init_registers(dev);
1592 /* now set the MAC address according to dev->dev_addr */
1593 for (i = 0; i < 3; i++) {
1594 u16 mac = (dev->dev_addr[2*i+1]<<8) + dev->dev_addr[2*i];
1595
1596 writel(i*2, ioaddr + RxFilterAddr);
1597 writew(mac, ioaddr + RxFilterData);
1598 }
1599 writel(np->cur_rx_mode, ioaddr + RxFilterAddr);
1600 spin_unlock_irq(&np->lock);
1601
1602 netif_start_queue(dev);
1603
1604 if (netif_msg_ifup(np))
1605 printk(KERN_DEBUG "%s: Done netdev_open(), status: %#08x.\n",
1606 dev->name, (int)readl(ioaddr + ChipCmd));
1607
1608 /* Set the timer to check for link beat. */
1609 init_timer(&np->timer);
1610 np->timer.expires = jiffies + NATSEMI_TIMER_FREQ;
1611 np->timer.data = (unsigned long)dev;
1612 np->timer.function = &netdev_timer; /* timer handler */
1613 add_timer(&np->timer);
1614
1615 return 0;
1616}
1617
1618static void do_cable_magic(struct net_device *dev)
1619{
1620 struct netdev_private *np = netdev_priv(dev);
1621 void __iomem *ioaddr = ns_ioaddr(dev);
1622
1623 if (dev->if_port != PORT_TP)
1624 return;
1625
1626 if (np->srr >= SRR_DP83816_A5)
1627 return;
1628
1629 /*
1630 * 100 MBit links with short cables can trip an issue with the chip.
1631 * The problem manifests as lots of CRC errors and/or flickering
1632 * activity LED while idle. This process is based on instructions
1633 * from engineers at National.
1634 */
1635 if (readl(ioaddr + ChipConfig) & CfgSpeed100) {
1636 u16 data;
1637
1638 writew(1, ioaddr + PGSEL);
1639 /*
1640 * coefficient visibility should already be enabled via
1641 * DSPCFG | 0x1000
1642 */
1643 data = readw(ioaddr + TSTDAT) & 0xff;
1644 /*
1645 * the value must be negative, and within certain values
1646 * (these values all come from National)
1647 */
1648 if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
1649 struct netdev_private *np = netdev_priv(dev);
1650
1651 /* the bug has been triggered - fix the coefficient */
1652 writew(TSTDAT_FIXED, ioaddr + TSTDAT);
1653 /* lock the value */
1654 data = readw(ioaddr + DSPCFG);
1655 np->dspcfg = data | DSPCFG_LOCK;
1656 writew(np->dspcfg, ioaddr + DSPCFG);
1657 }
1658 writew(0, ioaddr + PGSEL);
1659 }
1660}
1661
1662static void undo_cable_magic(struct net_device *dev)
1663{
1664 u16 data;
1665 struct netdev_private *np = netdev_priv(dev);
1666 void __iomem * ioaddr = ns_ioaddr(dev);
1667
1668 if (dev->if_port != PORT_TP)
1669 return;
1670
1671 if (np->srr >= SRR_DP83816_A5)
1672 return;
1673
1674 writew(1, ioaddr + PGSEL);
1675 /* make sure the lock bit is clear */
1676 data = readw(ioaddr + DSPCFG);
1677 np->dspcfg = data & ~DSPCFG_LOCK;
1678 writew(np->dspcfg, ioaddr + DSPCFG);
1679 writew(0, ioaddr + PGSEL);
1680}
1681
1682static void check_link(struct net_device *dev)
1683{
1684 struct netdev_private *np = netdev_priv(dev);
1685 void __iomem * ioaddr = ns_ioaddr(dev);
1686 int duplex;
1687 u16 bmsr;
1688
1689 /* The link status field is latched: it remains low after a temporary
1690 * link failure until it's read. We need the current link status,
1691 * thus read twice.
1692 */
1693 mdio_read(dev, MII_BMSR);
1694 bmsr = mdio_read(dev, MII_BMSR);
1695
1696 if (!(bmsr & BMSR_LSTATUS)) {
1697 if (netif_carrier_ok(dev)) {
1698 if (netif_msg_link(np))
1699 printk(KERN_NOTICE "%s: link down.\n",
1700 dev->name);
1701 netif_carrier_off(dev);
1702 undo_cable_magic(dev);
1703 }
1704 return;
1705 }
1706 if (!netif_carrier_ok(dev)) {
1707 if (netif_msg_link(np))
1708 printk(KERN_NOTICE "%s: link up.\n", dev->name);
1709 netif_carrier_on(dev);
1710 do_cable_magic(dev);
1711 }
1712
1713 duplex = np->full_duplex;
1714 if (!duplex) {
1715 if (bmsr & BMSR_ANEGCOMPLETE) {
1716 int tmp = mii_nway_result(
1717 np->advertising & mdio_read(dev, MII_LPA));
1718 if (tmp == LPA_100FULL || tmp == LPA_10FULL)
1719 duplex = 1;
1720 } else if (mdio_read(dev, MII_BMCR) & BMCR_FULLDPLX)
1721 duplex = 1;
1722 }
1723
1724 /* if duplex is set then bit 28 must be set, too */
1725 if (duplex ^ !!(np->rx_config & RxAcceptTx)) {
1726 if (netif_msg_link(np))
1727 printk(KERN_INFO
1728 "%s: Setting %s-duplex based on negotiated "
1729 "link capability.\n", dev->name,
1730 duplex ? "full" : "half");
1731 if (duplex) {
1732 np->rx_config |= RxAcceptTx;
1733 np->tx_config |= TxCarrierIgn | TxHeartIgn;
1734 } else {
1735 np->rx_config &= ~RxAcceptTx;
1736 np->tx_config &= ~(TxCarrierIgn | TxHeartIgn);
1737 }
1738 writel(np->tx_config, ioaddr + TxConfig);
1739 writel(np->rx_config, ioaddr + RxConfig);
1740 }
1741}
1742
1743static void init_registers(struct net_device *dev)
1744{
1745 struct netdev_private *np = netdev_priv(dev);
1746 void __iomem * ioaddr = ns_ioaddr(dev);
1747
1748 init_phy_fixup(dev);
1749
1750 /* clear any interrupts that are pending, such as wake events */
1751 readl(ioaddr + IntrStatus);
1752
1753 writel(np->ring_dma, ioaddr + RxRingPtr);
1754 writel(np->ring_dma + RX_RING_SIZE * sizeof(struct netdev_desc),
1755 ioaddr + TxRingPtr);
1756
1757 /* Initialize other registers.
1758 * Configure the PCI bus bursts and FIFO thresholds.
1759 * Configure for standard, in-spec Ethernet.
1760 * Start with half-duplex. check_link will update
1761 * to the correct settings.
1762 */
1763
1764 /* DRTH: 2: start tx if 64 bytes are in the fifo
1765 * FLTH: 0x10: refill with next packet if 512 bytes are free
1766 * MXDMA: 0: up to 256 byte bursts.
1767 * MXDMA must be <= FLTH
1768 * ECRETRY=1
1769 * ATP=1
1770 */
1771 np->tx_config = TxAutoPad | TxCollRetry | TxMxdma_256 |
1772 TX_FLTH_VAL | TX_DRTH_VAL_START;
1773 writel(np->tx_config, ioaddr + TxConfig);
1774
1775 /* DRTH 0x10: start copying to memory if 128 bytes are in the fifo
1776 * MXDMA 0: up to 256 byte bursts
1777 */
1778 np->rx_config = RxMxdma_256 | RX_DRTH_VAL;
1779 /* if receive ring now has bigger buffers than normal, enable jumbo */
1780 if (np->rx_buf_sz > NATSEMI_LONGPKT)
1781 np->rx_config |= RxAcceptLong;
1782
1783 writel(np->rx_config, ioaddr + RxConfig);
1784
1785 /* Disable PME:
1786 * The PME bit is initialized from the EEPROM contents.
1787 * PCI cards probably have PME disabled, but motherboard
1788 * implementations may have PME set to enable WakeOnLan.
1789 * With PME set the chip will scan incoming packets but
1790 * nothing will be written to memory. */
1791 np->SavedClkRun = readl(ioaddr + ClkRun);
1792 writel(np->SavedClkRun & ~PMEEnable, ioaddr + ClkRun);
1793 if (np->SavedClkRun & PMEStatus && netif_msg_wol(np)) {
1794 printk(KERN_NOTICE "%s: Wake-up event %#08x\n",
1795 dev->name, readl(ioaddr + WOLCmd));
1796 }
1797
1798 check_link(dev);
1799 __set_rx_mode(dev);
1800
1801 /* Enable interrupts by setting the interrupt mask. */
1802 writel(DEFAULT_INTR, ioaddr + IntrMask);
1803 writel(1, ioaddr + IntrEnable);
1804
1805 writel(RxOn | TxOn, ioaddr + ChipCmd);
1806 writel(StatsClear, ioaddr + StatsCtrl); /* Clear Stats */
1807}
1808
1809/*
1810 * netdev_timer:
1811 * Purpose:
1812 * 1) check for link changes. Usually they are handled by the MII interrupt
1813 * but it doesn't hurt to check twice.
1814 * 2) check for sudden death of the NIC:
1815 * It seems that a reference set for this chip went out with incorrect info,
1816 * and there exist boards that aren't quite right. An unexpected voltage
1817 * drop can cause the PHY to get itself in a weird state (basically reset).
1818 * NOTE: this only seems to affect revC chips.
1819 * 3) check of death of the RX path due to OOM
1820 */
1821static void netdev_timer(unsigned long data)
1822{
1823 struct net_device *dev = (struct net_device *)data;
1824 struct netdev_private *np = netdev_priv(dev);
1825 void __iomem * ioaddr = ns_ioaddr(dev);
1826 int next_tick = 5*HZ;
1827
1828 if (netif_msg_timer(np)) {
1829 /* DO NOT read the IntrStatus register,
1830 * a read clears any pending interrupts.
1831 */
1832 printk(KERN_DEBUG "%s: Media selection timer tick.\n",
1833 dev->name);
1834 }
1835
1836 if (dev->if_port == PORT_TP) {
1837 u16 dspcfg;
1838
1839 spin_lock_irq(&np->lock);
1840 /* check for a nasty random phy-reset - use dspcfg as a flag */
1841 writew(1, ioaddr+PGSEL);
1842 dspcfg = readw(ioaddr+DSPCFG);
1843 writew(0, ioaddr+PGSEL);
1844 if (dspcfg != np->dspcfg) {
1845 if (!netif_queue_stopped(dev)) {
1846 spin_unlock_irq(&np->lock);
1847 if (netif_msg_hw(np))
1848 printk(KERN_NOTICE "%s: possible phy reset: "
1849 "re-initializing\n", dev->name);
1850 disable_irq(dev->irq);
1851 spin_lock_irq(&np->lock);
1852 natsemi_stop_rxtx(dev);
1853 dump_ring(dev);
1854 reinit_ring(dev);
1855 init_registers(dev);
1856 spin_unlock_irq(&np->lock);
1857 enable_irq(dev->irq);
1858 } else {
1859 /* hurry back */
1860 next_tick = HZ;
1861 spin_unlock_irq(&np->lock);
1862 }
1863 } else {
1864 /* init_registers() calls check_link() for the above case */
1865 check_link(dev);
1866 spin_unlock_irq(&np->lock);
1867 }
1868 } else {
1869 spin_lock_irq(&np->lock);
1870 check_link(dev);
1871 spin_unlock_irq(&np->lock);
1872 }
1873 if (np->oom) {
1874 disable_irq(dev->irq);
1875 np->oom = 0;
1876 refill_rx(dev);
1877 enable_irq(dev->irq);
1878 if (!np->oom) {
1879 writel(RxOn, ioaddr + ChipCmd);
1880 } else {
1881 next_tick = 1;
1882 }
1883 }
1884 mod_timer(&np->timer, jiffies + next_tick);
1885}
1886
1887static void dump_ring(struct net_device *dev)
1888{
1889 struct netdev_private *np = netdev_priv(dev);
1890
1891 if (netif_msg_pktdata(np)) {
1892 int i;
1893 printk(KERN_DEBUG " Tx ring at %p:\n", np->tx_ring);
1894 for (i = 0; i < TX_RING_SIZE; i++) {
1895 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1896 i, np->tx_ring[i].next_desc,
1897 np->tx_ring[i].cmd_status,
1898 np->tx_ring[i].addr);
1899 }
1900 printk(KERN_DEBUG " Rx ring %p:\n", np->rx_ring);
1901 for (i = 0; i < RX_RING_SIZE; i++) {
1902 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1903 i, np->rx_ring[i].next_desc,
1904 np->rx_ring[i].cmd_status,
1905 np->rx_ring[i].addr);
1906 }
1907 }
1908}
1909
1910static void tx_timeout(struct net_device *dev)
1911{
1912 struct netdev_private *np = netdev_priv(dev);
1913 void __iomem * ioaddr = ns_ioaddr(dev);
1914
1915 disable_irq(dev->irq);
1916 spin_lock_irq(&np->lock);
1917 if (!np->hands_off) {
1918 if (netif_msg_tx_err(np))
1919 printk(KERN_WARNING
1920 "%s: Transmit timed out, status %#08x,"
1921 " resetting...\n",
1922 dev->name, readl(ioaddr + IntrStatus));
1923 dump_ring(dev);
1924
1925 natsemi_reset(dev);
1926 reinit_ring(dev);
1927 init_registers(dev);
1928 } else {
1929 printk(KERN_WARNING
1930 "%s: tx_timeout while in hands_off state?\n",
1931 dev->name);
1932 }
1933 spin_unlock_irq(&np->lock);
1934 enable_irq(dev->irq);
1935
1936 dev->trans_start = jiffies;
1937 np->stats.tx_errors++;
1938 netif_wake_queue(dev);
1939}
1940
1941static int alloc_ring(struct net_device *dev)
1942{
1943 struct netdev_private *np = netdev_priv(dev);
1944 np->rx_ring = pci_alloc_consistent(np->pci_dev,
1945 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
1946 &np->ring_dma);
1947 if (!np->rx_ring)
1948 return -ENOMEM;
1949 np->tx_ring = &np->rx_ring[RX_RING_SIZE];
1950 return 0;
1951}
1952
1953static void refill_rx(struct net_device *dev)
1954{
1955 struct netdev_private *np = netdev_priv(dev);
1956
1957 /* Refill the Rx ring buffers. */
1958 for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1959 struct sk_buff *skb;
1960 int entry = np->dirty_rx % RX_RING_SIZE;
1961 if (np->rx_skbuff[entry] == NULL) {
1962 unsigned int buflen = np->rx_buf_sz+NATSEMI_PADDING;
1963 skb = dev_alloc_skb(buflen);
1964 np->rx_skbuff[entry] = skb;
1965 if (skb == NULL)
1966 break; /* Better luck next round. */
1967 skb->dev = dev; /* Mark as being used by this device. */
1968 np->rx_dma[entry] = pci_map_single(np->pci_dev,
David S. Miller689be432005-06-28 15:25:31 -07001969 skb->data, buflen, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 np->rx_ring[entry].addr = cpu_to_le32(np->rx_dma[entry]);
1971 }
1972 np->rx_ring[entry].cmd_status = cpu_to_le32(np->rx_buf_sz);
1973 }
1974 if (np->cur_rx - np->dirty_rx == RX_RING_SIZE) {
1975 if (netif_msg_rx_err(np))
1976 printk(KERN_WARNING "%s: going OOM.\n", dev->name);
1977 np->oom = 1;
1978 }
1979}
1980
1981static void set_bufsize(struct net_device *dev)
1982{
1983 struct netdev_private *np = netdev_priv(dev);
1984 if (dev->mtu <= ETH_DATA_LEN)
1985 np->rx_buf_sz = ETH_DATA_LEN + NATSEMI_HEADERS;
1986 else
1987 np->rx_buf_sz = dev->mtu + NATSEMI_HEADERS;
1988}
1989
1990/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1991static void init_ring(struct net_device *dev)
1992{
1993 struct netdev_private *np = netdev_priv(dev);
1994 int i;
1995
1996 /* 1) TX ring */
1997 np->dirty_tx = np->cur_tx = 0;
1998 for (i = 0; i < TX_RING_SIZE; i++) {
1999 np->tx_skbuff[i] = NULL;
2000 np->tx_ring[i].next_desc = cpu_to_le32(np->ring_dma
2001 +sizeof(struct netdev_desc)
2002 *((i+1)%TX_RING_SIZE+RX_RING_SIZE));
2003 np->tx_ring[i].cmd_status = 0;
2004 }
2005
2006 /* 2) RX ring */
2007 np->dirty_rx = 0;
2008 np->cur_rx = RX_RING_SIZE;
2009 np->oom = 0;
2010 set_bufsize(dev);
2011
2012 np->rx_head_desc = &np->rx_ring[0];
2013
2014 /* Please be carefull before changing this loop - at least gcc-2.95.1
2015 * miscompiles it otherwise.
2016 */
2017 /* Initialize all Rx descriptors. */
2018 for (i = 0; i < RX_RING_SIZE; i++) {
2019 np->rx_ring[i].next_desc = cpu_to_le32(np->ring_dma
2020 +sizeof(struct netdev_desc)
2021 *((i+1)%RX_RING_SIZE));
2022 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
2023 np->rx_skbuff[i] = NULL;
2024 }
2025 refill_rx(dev);
2026 dump_ring(dev);
2027}
2028
2029static void drain_tx(struct net_device *dev)
2030{
2031 struct netdev_private *np = netdev_priv(dev);
2032 int i;
2033
2034 for (i = 0; i < TX_RING_SIZE; i++) {
2035 if (np->tx_skbuff[i]) {
2036 pci_unmap_single(np->pci_dev,
2037 np->tx_dma[i], np->tx_skbuff[i]->len,
2038 PCI_DMA_TODEVICE);
2039 dev_kfree_skb(np->tx_skbuff[i]);
2040 np->stats.tx_dropped++;
2041 }
2042 np->tx_skbuff[i] = NULL;
2043 }
2044}
2045
2046static void drain_rx(struct net_device *dev)
2047{
2048 struct netdev_private *np = netdev_priv(dev);
2049 unsigned int buflen = np->rx_buf_sz;
2050 int i;
2051
2052 /* Free all the skbuffs in the Rx queue. */
2053 for (i = 0; i < RX_RING_SIZE; i++) {
2054 np->rx_ring[i].cmd_status = 0;
2055 np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */
2056 if (np->rx_skbuff[i]) {
2057 pci_unmap_single(np->pci_dev,
2058 np->rx_dma[i], buflen,
2059 PCI_DMA_FROMDEVICE);
2060 dev_kfree_skb(np->rx_skbuff[i]);
2061 }
2062 np->rx_skbuff[i] = NULL;
2063 }
2064}
2065
2066static void drain_ring(struct net_device *dev)
2067{
2068 drain_rx(dev);
2069 drain_tx(dev);
2070}
2071
2072static void free_ring(struct net_device *dev)
2073{
2074 struct netdev_private *np = netdev_priv(dev);
2075 pci_free_consistent(np->pci_dev,
2076 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
2077 np->rx_ring, np->ring_dma);
2078}
2079
2080static void reinit_rx(struct net_device *dev)
2081{
2082 struct netdev_private *np = netdev_priv(dev);
2083 int i;
2084
2085 /* RX Ring */
2086 np->dirty_rx = 0;
2087 np->cur_rx = RX_RING_SIZE;
2088 np->rx_head_desc = &np->rx_ring[0];
2089 /* Initialize all Rx descriptors. */
2090 for (i = 0; i < RX_RING_SIZE; i++)
2091 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
2092
2093 refill_rx(dev);
2094}
2095
2096static void reinit_ring(struct net_device *dev)
2097{
2098 struct netdev_private *np = netdev_priv(dev);
2099 int i;
2100
2101 /* drain TX ring */
2102 drain_tx(dev);
2103 np->dirty_tx = np->cur_tx = 0;
2104 for (i=0;i<TX_RING_SIZE;i++)
2105 np->tx_ring[i].cmd_status = 0;
2106
2107 reinit_rx(dev);
2108}
2109
2110static int start_tx(struct sk_buff *skb, struct net_device *dev)
2111{
2112 struct netdev_private *np = netdev_priv(dev);
2113 void __iomem * ioaddr = ns_ioaddr(dev);
2114 unsigned entry;
2115
2116 /* Note: Ordering is important here, set the field with the
2117 "ownership" bit last, and only then increment cur_tx. */
2118
2119 /* Calculate the next Tx descriptor entry. */
2120 entry = np->cur_tx % TX_RING_SIZE;
2121
2122 np->tx_skbuff[entry] = skb;
2123 np->tx_dma[entry] = pci_map_single(np->pci_dev,
2124 skb->data,skb->len, PCI_DMA_TODEVICE);
2125
2126 np->tx_ring[entry].addr = cpu_to_le32(np->tx_dma[entry]);
2127
2128 spin_lock_irq(&np->lock);
2129
2130 if (!np->hands_off) {
2131 np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn | skb->len);
2132 /* StrongARM: Explicitly cache flush np->tx_ring and
2133 * skb->data,skb->len. */
2134 wmb();
2135 np->cur_tx++;
2136 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
2137 netdev_tx_done(dev);
2138 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1)
2139 netif_stop_queue(dev);
2140 }
2141 /* Wake the potentially-idle transmit channel. */
2142 writel(TxOn, ioaddr + ChipCmd);
2143 } else {
2144 dev_kfree_skb_irq(skb);
2145 np->stats.tx_dropped++;
2146 }
2147 spin_unlock_irq(&np->lock);
2148
2149 dev->trans_start = jiffies;
2150
2151 if (netif_msg_tx_queued(np)) {
2152 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
2153 dev->name, np->cur_tx, entry);
2154 }
2155 return 0;
2156}
2157
2158static void netdev_tx_done(struct net_device *dev)
2159{
2160 struct netdev_private *np = netdev_priv(dev);
2161
2162 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
2163 int entry = np->dirty_tx % TX_RING_SIZE;
2164 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescOwn))
2165 break;
2166 if (netif_msg_tx_done(np))
2167 printk(KERN_DEBUG
2168 "%s: tx frame #%d finished, status %#08x.\n",
2169 dev->name, np->dirty_tx,
2170 le32_to_cpu(np->tx_ring[entry].cmd_status));
2171 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescPktOK)) {
2172 np->stats.tx_packets++;
2173 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
2174 } else { /* Various Tx errors */
2175 int tx_status =
2176 le32_to_cpu(np->tx_ring[entry].cmd_status);
2177 if (tx_status & (DescTxAbort|DescTxExcColl))
2178 np->stats.tx_aborted_errors++;
2179 if (tx_status & DescTxFIFO)
2180 np->stats.tx_fifo_errors++;
2181 if (tx_status & DescTxCarrier)
2182 np->stats.tx_carrier_errors++;
2183 if (tx_status & DescTxOOWCol)
2184 np->stats.tx_window_errors++;
2185 np->stats.tx_errors++;
2186 }
2187 pci_unmap_single(np->pci_dev,np->tx_dma[entry],
2188 np->tx_skbuff[entry]->len,
2189 PCI_DMA_TODEVICE);
2190 /* Free the original skb. */
2191 dev_kfree_skb_irq(np->tx_skbuff[entry]);
2192 np->tx_skbuff[entry] = NULL;
2193 }
2194 if (netif_queue_stopped(dev)
2195 && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
2196 /* The ring is no longer full, wake queue. */
2197 netif_wake_queue(dev);
2198 }
2199}
2200
Mark Brownb27a16b2006-02-02 00:00:01 +00002201/* The interrupt handler doesn't actually handle interrupts itself, it
2202 * schedules a NAPI poll if there is anything to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
2204{
2205 struct net_device *dev = dev_instance;
2206 struct netdev_private *np = netdev_priv(dev);
2207 void __iomem * ioaddr = ns_ioaddr(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 if (np->hands_off)
2210 return IRQ_NONE;
Mark Brownb27a16b2006-02-02 00:00:01 +00002211
2212 /* Reading automatically acknowledges. */
2213 np->intr_status = readl(ioaddr + IntrStatus);
2214
2215 if (netif_msg_intr(np))
2216 printk(KERN_DEBUG
2217 "%s: Interrupt, status %#08x, mask %#08x.\n",
2218 dev->name, np->intr_status,
2219 readl(ioaddr + IntrMask));
2220
2221 if (!np->intr_status)
2222 return IRQ_NONE;
2223
2224 prefetch(&np->rx_skbuff[np->cur_rx % RX_RING_SIZE]);
2225
2226 if (netif_rx_schedule_prep(dev)) {
2227 /* Disable interrupts and register for poll */
2228 natsemi_irq_disable(dev);
2229 __netif_rx_schedule(dev);
2230 }
2231 return IRQ_HANDLED;
2232}
2233
2234/* This is the NAPI poll routine. As well as the standard RX handling
2235 * it also handles all other interrupts that the chip might raise.
2236 */
2237static int natsemi_poll(struct net_device *dev, int *budget)
2238{
2239 struct netdev_private *np = netdev_priv(dev);
2240 void __iomem * ioaddr = ns_ioaddr(dev);
2241
2242 int work_to_do = min(*budget, dev->quota);
2243 int work_done = 0;
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 do {
Mark Brownb27a16b2006-02-02 00:00:01 +00002246 if (np->intr_status &
2247 (IntrTxDone | IntrTxIntr | IntrTxIdle | IntrTxErr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 spin_lock(&np->lock);
2249 netdev_tx_done(dev);
2250 spin_unlock(&np->lock);
2251 }
2252
2253 /* Abnormal error summary/uncommon events handlers. */
Mark Brownb27a16b2006-02-02 00:00:01 +00002254 if (np->intr_status & IntrAbnormalSummary)
2255 netdev_error(dev, np->intr_status);
2256
2257 if (np->intr_status &
2258 (IntrRxDone | IntrRxIntr | RxStatusFIFOOver |
2259 IntrRxErr | IntrRxOverrun)) {
2260 netdev_rx(dev, &work_done, work_to_do);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
Mark Brownb27a16b2006-02-02 00:00:01 +00002262
2263 *budget -= work_done;
2264 dev->quota -= work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Mark Brownb27a16b2006-02-02 00:00:01 +00002266 if (work_done >= work_to_do)
2267 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Mark Brownb27a16b2006-02-02 00:00:01 +00002269 np->intr_status = readl(ioaddr + IntrStatus);
2270 } while (np->intr_status);
2271
2272 netif_rx_complete(dev);
2273
2274 /* Reenable interrupts providing nothing is trying to shut
2275 * the chip down. */
2276 spin_lock(&np->lock);
2277 if (!np->hands_off && netif_running(dev))
2278 natsemi_irq_enable(dev);
2279 spin_unlock(&np->lock);
2280
2281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
2284/* This routine is logically part of the interrupt handler, but separated
2285 for clarity and better register allocation. */
Mark Brownb27a16b2006-02-02 00:00:01 +00002286static void netdev_rx(struct net_device *dev, int *work_done, int work_to_do)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 struct netdev_private *np = netdev_priv(dev);
2289 int entry = np->cur_rx % RX_RING_SIZE;
2290 int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
2291 s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
2292 unsigned int buflen = np->rx_buf_sz;
2293 void __iomem * ioaddr = ns_ioaddr(dev);
2294
2295 /* If the driver owns the next entry it's a new packet. Send it up. */
2296 while (desc_status < 0) { /* e.g. & DescOwn */
2297 int pkt_len;
2298 if (netif_msg_rx_status(np))
2299 printk(KERN_DEBUG
2300 " netdev_rx() entry %d status was %#08x.\n",
2301 entry, desc_status);
2302 if (--boguscnt < 0)
2303 break;
Mark Brownb27a16b2006-02-02 00:00:01 +00002304
2305 if (*work_done >= work_to_do)
2306 break;
2307
2308 (*work_done)++;
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 pkt_len = (desc_status & DescSizeMask) - 4;
2311 if ((desc_status&(DescMore|DescPktOK|DescRxLong)) != DescPktOK){
2312 if (desc_status & DescMore) {
2313 if (netif_msg_rx_err(np))
2314 printk(KERN_WARNING
2315 "%s: Oversized(?) Ethernet "
2316 "frame spanned multiple "
2317 "buffers, entry %#08x "
2318 "status %#08x.\n", dev->name,
2319 np->cur_rx, desc_status);
2320 np->stats.rx_length_errors++;
Mark Browne72fd962006-02-02 00:00:02 +00002321
2322 /* The RX state machine has probably
2323 * locked up beneath us. Follow the
2324 * reset procedure documented in
2325 * AN-1287. */
2326
2327 spin_lock_irq(&np->lock);
2328 reset_rx(dev);
2329 reinit_rx(dev);
2330 writel(np->ring_dma, ioaddr + RxRingPtr);
2331 check_link(dev);
2332 spin_unlock_irq(&np->lock);
2333
2334 /* We'll enable RX on exit from this
2335 * function. */
2336 break;
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 } else {
2339 /* There was an error. */
2340 np->stats.rx_errors++;
2341 if (desc_status & (DescRxAbort|DescRxOver))
2342 np->stats.rx_over_errors++;
2343 if (desc_status & (DescRxLong|DescRxRunt))
2344 np->stats.rx_length_errors++;
2345 if (desc_status & (DescRxInvalid|DescRxAlign))
2346 np->stats.rx_frame_errors++;
2347 if (desc_status & DescRxCRC)
2348 np->stats.rx_crc_errors++;
2349 }
2350 } else if (pkt_len > np->rx_buf_sz) {
2351 /* if this is the tail of a double buffer
2352 * packet, we've already counted the error
2353 * on the first part. Ignore the second half.
2354 */
2355 } else {
2356 struct sk_buff *skb;
2357 /* Omit CRC size. */
2358 /* Check if the packet is long enough to accept
2359 * without copying to a minimally-sized skbuff. */
2360 if (pkt_len < rx_copybreak
2361 && (skb = dev_alloc_skb(pkt_len + RX_OFFSET)) != NULL) {
2362 skb->dev = dev;
2363 /* 16 byte align the IP header */
2364 skb_reserve(skb, RX_OFFSET);
2365 pci_dma_sync_single_for_cpu(np->pci_dev,
2366 np->rx_dma[entry],
2367 buflen,
2368 PCI_DMA_FROMDEVICE);
2369 eth_copy_and_sum(skb,
David S. Miller689be432005-06-28 15:25:31 -07002370 np->rx_skbuff[entry]->data, pkt_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 skb_put(skb, pkt_len);
2372 pci_dma_sync_single_for_device(np->pci_dev,
2373 np->rx_dma[entry],
2374 buflen,
2375 PCI_DMA_FROMDEVICE);
2376 } else {
2377 pci_unmap_single(np->pci_dev, np->rx_dma[entry],
2378 buflen, PCI_DMA_FROMDEVICE);
2379 skb_put(skb = np->rx_skbuff[entry], pkt_len);
2380 np->rx_skbuff[entry] = NULL;
2381 }
2382 skb->protocol = eth_type_trans(skb, dev);
Mark Brownb27a16b2006-02-02 00:00:01 +00002383 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 dev->last_rx = jiffies;
2385 np->stats.rx_packets++;
2386 np->stats.rx_bytes += pkt_len;
2387 }
2388 entry = (++np->cur_rx) % RX_RING_SIZE;
2389 np->rx_head_desc = &np->rx_ring[entry];
2390 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
2391 }
2392 refill_rx(dev);
2393
2394 /* Restart Rx engine if stopped. */
2395 if (np->oom)
2396 mod_timer(&np->timer, jiffies + 1);
2397 else
2398 writel(RxOn, ioaddr + ChipCmd);
2399}
2400
2401static void netdev_error(struct net_device *dev, int intr_status)
2402{
2403 struct netdev_private *np = netdev_priv(dev);
2404 void __iomem * ioaddr = ns_ioaddr(dev);
2405
2406 spin_lock(&np->lock);
2407 if (intr_status & LinkChange) {
2408 u16 lpa = mdio_read(dev, MII_LPA);
2409 if (mdio_read(dev, MII_BMCR) & BMCR_ANENABLE
2410 && netif_msg_link(np)) {
2411 printk(KERN_INFO
2412 "%s: Autonegotiation advertising"
2413 " %#04x partner %#04x.\n", dev->name,
2414 np->advertising, lpa);
2415 }
2416
2417 /* read MII int status to clear the flag */
2418 readw(ioaddr + MIntrStatus);
2419 check_link(dev);
2420 }
2421 if (intr_status & StatsMax) {
2422 __get_stats(dev);
2423 }
2424 if (intr_status & IntrTxUnderrun) {
2425 if ((np->tx_config & TxDrthMask) < TX_DRTH_VAL_LIMIT) {
2426 np->tx_config += TX_DRTH_VAL_INC;
2427 if (netif_msg_tx_err(np))
2428 printk(KERN_NOTICE
2429 "%s: increased tx threshold, txcfg %#08x.\n",
2430 dev->name, np->tx_config);
2431 } else {
2432 if (netif_msg_tx_err(np))
2433 printk(KERN_NOTICE
2434 "%s: tx underrun with maximum tx threshold, txcfg %#08x.\n",
2435 dev->name, np->tx_config);
2436 }
2437 writel(np->tx_config, ioaddr + TxConfig);
2438 }
2439 if (intr_status & WOLPkt && netif_msg_wol(np)) {
2440 int wol_status = readl(ioaddr + WOLCmd);
2441 printk(KERN_NOTICE "%s: Link wake-up event %#08x\n",
2442 dev->name, wol_status);
2443 }
2444 if (intr_status & RxStatusFIFOOver) {
2445 if (netif_msg_rx_err(np) && netif_msg_intr(np)) {
2446 printk(KERN_NOTICE "%s: Rx status FIFO overrun\n",
2447 dev->name);
2448 }
2449 np->stats.rx_fifo_errors++;
2450 }
2451 /* Hmmmmm, it's not clear how to recover from PCI faults. */
2452 if (intr_status & IntrPCIErr) {
2453 printk(KERN_NOTICE "%s: PCI error %#08x\n", dev->name,
2454 intr_status & IntrPCIErr);
2455 np->stats.tx_fifo_errors++;
2456 np->stats.rx_fifo_errors++;
2457 }
2458 spin_unlock(&np->lock);
2459}
2460
2461static void __get_stats(struct net_device *dev)
2462{
2463 void __iomem * ioaddr = ns_ioaddr(dev);
2464 struct netdev_private *np = netdev_priv(dev);
2465
2466 /* The chip only need report frame silently dropped. */
2467 np->stats.rx_crc_errors += readl(ioaddr + RxCRCErrs);
2468 np->stats.rx_missed_errors += readl(ioaddr + RxMissed);
2469}
2470
2471static struct net_device_stats *get_stats(struct net_device *dev)
2472{
2473 struct netdev_private *np = netdev_priv(dev);
2474
2475 /* The chip only need report frame silently dropped. */
2476 spin_lock_irq(&np->lock);
2477 if (netif_running(dev) && !np->hands_off)
2478 __get_stats(dev);
2479 spin_unlock_irq(&np->lock);
2480
2481 return &np->stats;
2482}
2483
2484#ifdef CONFIG_NET_POLL_CONTROLLER
2485static void natsemi_poll_controller(struct net_device *dev)
2486{
2487 disable_irq(dev->irq);
2488 intr_handler(dev->irq, dev, NULL);
2489 enable_irq(dev->irq);
2490}
2491#endif
2492
2493#define HASH_TABLE 0x200
2494static void __set_rx_mode(struct net_device *dev)
2495{
2496 void __iomem * ioaddr = ns_ioaddr(dev);
2497 struct netdev_private *np = netdev_priv(dev);
2498 u8 mc_filter[64]; /* Multicast hash filter */
2499 u32 rx_mode;
2500
2501 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2502 /* Unconditionally log net taps. */
2503 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
2504 dev->name);
2505 rx_mode = RxFilterEnable | AcceptBroadcast
2506 | AcceptAllMulticast | AcceptAllPhys | AcceptMyPhys;
2507 } else if ((dev->mc_count > multicast_filter_limit)
2508 || (dev->flags & IFF_ALLMULTI)) {
2509 rx_mode = RxFilterEnable | AcceptBroadcast
2510 | AcceptAllMulticast | AcceptMyPhys;
2511 } else {
2512 struct dev_mc_list *mclist;
2513 int i;
2514 memset(mc_filter, 0, sizeof(mc_filter));
2515 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2516 i++, mclist = mclist->next) {
2517 int i = (ether_crc(ETH_ALEN, mclist->dmi_addr) >> 23) & 0x1ff;
2518 mc_filter[i/8] |= (1 << (i & 0x07));
2519 }
2520 rx_mode = RxFilterEnable | AcceptBroadcast
2521 | AcceptMulticast | AcceptMyPhys;
2522 for (i = 0; i < 64; i += 2) {
Herbert Xu760f86d2005-04-16 16:24:16 +10002523 writel(HASH_TABLE + i, ioaddr + RxFilterAddr);
2524 writel((mc_filter[i + 1] << 8) + mc_filter[i],
2525 ioaddr + RxFilterData);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
2527 }
2528 writel(rx_mode, ioaddr + RxFilterAddr);
2529 np->cur_rx_mode = rx_mode;
2530}
2531
2532static int natsemi_change_mtu(struct net_device *dev, int new_mtu)
2533{
2534 if (new_mtu < 64 || new_mtu > NATSEMI_RX_LIMIT-NATSEMI_HEADERS)
2535 return -EINVAL;
2536
2537 dev->mtu = new_mtu;
2538
2539 /* synchronized against open : rtnl_lock() held by caller */
2540 if (netif_running(dev)) {
2541 struct netdev_private *np = netdev_priv(dev);
2542 void __iomem * ioaddr = ns_ioaddr(dev);
2543
2544 disable_irq(dev->irq);
2545 spin_lock(&np->lock);
2546 /* stop engines */
2547 natsemi_stop_rxtx(dev);
2548 /* drain rx queue */
2549 drain_rx(dev);
2550 /* change buffers */
2551 set_bufsize(dev);
2552 reinit_rx(dev);
2553 writel(np->ring_dma, ioaddr + RxRingPtr);
2554 /* restart engines */
2555 writel(RxOn | TxOn, ioaddr + ChipCmd);
2556 spin_unlock(&np->lock);
2557 enable_irq(dev->irq);
2558 }
2559 return 0;
2560}
2561
2562static void set_rx_mode(struct net_device *dev)
2563{
2564 struct netdev_private *np = netdev_priv(dev);
2565 spin_lock_irq(&np->lock);
2566 if (!np->hands_off)
2567 __set_rx_mode(dev);
2568 spin_unlock_irq(&np->lock);
2569}
2570
2571static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2572{
2573 struct netdev_private *np = netdev_priv(dev);
2574 strncpy(info->driver, DRV_NAME, ETHTOOL_BUSINFO_LEN);
2575 strncpy(info->version, DRV_VERSION, ETHTOOL_BUSINFO_LEN);
2576 strncpy(info->bus_info, pci_name(np->pci_dev), ETHTOOL_BUSINFO_LEN);
2577}
2578
2579static int get_regs_len(struct net_device *dev)
2580{
2581 return NATSEMI_REGS_SIZE;
2582}
2583
2584static int get_eeprom_len(struct net_device *dev)
2585{
Mark Browna8b4cf42006-03-28 14:08:55 -08002586 struct netdev_private *np = netdev_priv(dev);
2587 return np->eeprom_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590static int get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2591{
2592 struct netdev_private *np = netdev_priv(dev);
2593 spin_lock_irq(&np->lock);
2594 netdev_get_ecmd(dev, ecmd);
2595 spin_unlock_irq(&np->lock);
2596 return 0;
2597}
2598
2599static int set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2600{
2601 struct netdev_private *np = netdev_priv(dev);
2602 int res;
2603 spin_lock_irq(&np->lock);
2604 res = netdev_set_ecmd(dev, ecmd);
2605 spin_unlock_irq(&np->lock);
2606 return res;
2607}
2608
2609static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2610{
2611 struct netdev_private *np = netdev_priv(dev);
2612 spin_lock_irq(&np->lock);
2613 netdev_get_wol(dev, &wol->supported, &wol->wolopts);
2614 netdev_get_sopass(dev, wol->sopass);
2615 spin_unlock_irq(&np->lock);
2616}
2617
2618static int set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2619{
2620 struct netdev_private *np = netdev_priv(dev);
2621 int res;
2622 spin_lock_irq(&np->lock);
2623 netdev_set_wol(dev, wol->wolopts);
2624 res = netdev_set_sopass(dev, wol->sopass);
2625 spin_unlock_irq(&np->lock);
2626 return res;
2627}
2628
2629static void get_regs(struct net_device *dev, struct ethtool_regs *regs, void *buf)
2630{
2631 struct netdev_private *np = netdev_priv(dev);
2632 regs->version = NATSEMI_REGS_VER;
2633 spin_lock_irq(&np->lock);
2634 netdev_get_regs(dev, buf);
2635 spin_unlock_irq(&np->lock);
2636}
2637
2638static u32 get_msglevel(struct net_device *dev)
2639{
2640 struct netdev_private *np = netdev_priv(dev);
2641 return np->msg_enable;
2642}
2643
2644static void set_msglevel(struct net_device *dev, u32 val)
2645{
2646 struct netdev_private *np = netdev_priv(dev);
2647 np->msg_enable = val;
2648}
2649
2650static int nway_reset(struct net_device *dev)
2651{
2652 int tmp;
2653 int r = -EINVAL;
2654 /* if autoneg is off, it's an error */
2655 tmp = mdio_read(dev, MII_BMCR);
2656 if (tmp & BMCR_ANENABLE) {
2657 tmp |= (BMCR_ANRESTART);
2658 mdio_write(dev, MII_BMCR, tmp);
2659 r = 0;
2660 }
2661 return r;
2662}
2663
2664static u32 get_link(struct net_device *dev)
2665{
2666 /* LSTATUS is latched low until a read - so read twice */
2667 mdio_read(dev, MII_BMSR);
2668 return (mdio_read(dev, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
2669}
2670
2671static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
2672{
2673 struct netdev_private *np = netdev_priv(dev);
Mark Browna8b4cf42006-03-28 14:08:55 -08002674 u8 *eebuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 int res;
2676
Mark Browna8b4cf42006-03-28 14:08:55 -08002677 eebuf = kmalloc(np->eeprom_size, GFP_KERNEL);
2678 if (!eebuf)
2679 return -ENOMEM;
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 eeprom->magic = PCI_VENDOR_ID_NS | (PCI_DEVICE_ID_NS_83815<<16);
2682 spin_lock_irq(&np->lock);
2683 res = netdev_get_eeprom(dev, eebuf);
2684 spin_unlock_irq(&np->lock);
2685 if (!res)
2686 memcpy(data, eebuf+eeprom->offset, eeprom->len);
Mark Browna8b4cf42006-03-28 14:08:55 -08002687 kfree(eebuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return res;
2689}
2690
2691static struct ethtool_ops ethtool_ops = {
2692 .get_drvinfo = get_drvinfo,
2693 .get_regs_len = get_regs_len,
2694 .get_eeprom_len = get_eeprom_len,
2695 .get_settings = get_settings,
2696 .set_settings = set_settings,
2697 .get_wol = get_wol,
2698 .set_wol = set_wol,
2699 .get_regs = get_regs,
2700 .get_msglevel = get_msglevel,
2701 .set_msglevel = set_msglevel,
2702 .nway_reset = nway_reset,
2703 .get_link = get_link,
2704 .get_eeprom = get_eeprom,
2705};
2706
2707static int netdev_set_wol(struct net_device *dev, u32 newval)
2708{
2709 struct netdev_private *np = netdev_priv(dev);
2710 void __iomem * ioaddr = ns_ioaddr(dev);
2711 u32 data = readl(ioaddr + WOLCmd) & ~WakeOptsSummary;
2712
2713 /* translate to bitmasks this chip understands */
2714 if (newval & WAKE_PHY)
2715 data |= WakePhy;
2716 if (newval & WAKE_UCAST)
2717 data |= WakeUnicast;
2718 if (newval & WAKE_MCAST)
2719 data |= WakeMulticast;
2720 if (newval & WAKE_BCAST)
2721 data |= WakeBroadcast;
2722 if (newval & WAKE_ARP)
2723 data |= WakeArp;
2724 if (newval & WAKE_MAGIC)
2725 data |= WakeMagic;
2726 if (np->srr >= SRR_DP83815_D) {
2727 if (newval & WAKE_MAGICSECURE) {
2728 data |= WakeMagicSecure;
2729 }
2730 }
2731
2732 writel(data, ioaddr + WOLCmd);
2733
2734 return 0;
2735}
2736
2737static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur)
2738{
2739 struct netdev_private *np = netdev_priv(dev);
2740 void __iomem * ioaddr = ns_ioaddr(dev);
2741 u32 regval = readl(ioaddr + WOLCmd);
2742
2743 *supported = (WAKE_PHY | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST
2744 | WAKE_ARP | WAKE_MAGIC);
2745
2746 if (np->srr >= SRR_DP83815_D) {
2747 /* SOPASS works on revD and higher */
2748 *supported |= WAKE_MAGICSECURE;
2749 }
2750 *cur = 0;
2751
2752 /* translate from chip bitmasks */
2753 if (regval & WakePhy)
2754 *cur |= WAKE_PHY;
2755 if (regval & WakeUnicast)
2756 *cur |= WAKE_UCAST;
2757 if (regval & WakeMulticast)
2758 *cur |= WAKE_MCAST;
2759 if (regval & WakeBroadcast)
2760 *cur |= WAKE_BCAST;
2761 if (regval & WakeArp)
2762 *cur |= WAKE_ARP;
2763 if (regval & WakeMagic)
2764 *cur |= WAKE_MAGIC;
2765 if (regval & WakeMagicSecure) {
2766 /* this can be on in revC, but it's broken */
2767 *cur |= WAKE_MAGICSECURE;
2768 }
2769
2770 return 0;
2771}
2772
2773static int netdev_set_sopass(struct net_device *dev, u8 *newval)
2774{
2775 struct netdev_private *np = netdev_priv(dev);
2776 void __iomem * ioaddr = ns_ioaddr(dev);
2777 u16 *sval = (u16 *)newval;
2778 u32 addr;
2779
2780 if (np->srr < SRR_DP83815_D) {
2781 return 0;
2782 }
2783
2784 /* enable writing to these registers by disabling the RX filter */
2785 addr = readl(ioaddr + RxFilterAddr) & ~RFCRAddressMask;
2786 addr &= ~RxFilterEnable;
2787 writel(addr, ioaddr + RxFilterAddr);
2788
2789 /* write the three words to (undocumented) RFCR vals 0xa, 0xc, 0xe */
2790 writel(addr | 0xa, ioaddr + RxFilterAddr);
2791 writew(sval[0], ioaddr + RxFilterData);
2792
2793 writel(addr | 0xc, ioaddr + RxFilterAddr);
2794 writew(sval[1], ioaddr + RxFilterData);
2795
2796 writel(addr | 0xe, ioaddr + RxFilterAddr);
2797 writew(sval[2], ioaddr + RxFilterData);
2798
2799 /* re-enable the RX filter */
2800 writel(addr | RxFilterEnable, ioaddr + RxFilterAddr);
2801
2802 return 0;
2803}
2804
2805static int netdev_get_sopass(struct net_device *dev, u8 *data)
2806{
2807 struct netdev_private *np = netdev_priv(dev);
2808 void __iomem * ioaddr = ns_ioaddr(dev);
2809 u16 *sval = (u16 *)data;
2810 u32 addr;
2811
2812 if (np->srr < SRR_DP83815_D) {
2813 sval[0] = sval[1] = sval[2] = 0;
2814 return 0;
2815 }
2816
2817 /* read the three words from (undocumented) RFCR vals 0xa, 0xc, 0xe */
2818 addr = readl(ioaddr + RxFilterAddr) & ~RFCRAddressMask;
2819
2820 writel(addr | 0xa, ioaddr + RxFilterAddr);
2821 sval[0] = readw(ioaddr + RxFilterData);
2822
2823 writel(addr | 0xc, ioaddr + RxFilterAddr);
2824 sval[1] = readw(ioaddr + RxFilterData);
2825
2826 writel(addr | 0xe, ioaddr + RxFilterAddr);
2827 sval[2] = readw(ioaddr + RxFilterData);
2828
2829 writel(addr, ioaddr + RxFilterAddr);
2830
2831 return 0;
2832}
2833
2834static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2835{
2836 struct netdev_private *np = netdev_priv(dev);
2837 u32 tmp;
2838
2839 ecmd->port = dev->if_port;
2840 ecmd->speed = np->speed;
2841 ecmd->duplex = np->duplex;
2842 ecmd->autoneg = np->autoneg;
2843 ecmd->advertising = 0;
2844 if (np->advertising & ADVERTISE_10HALF)
2845 ecmd->advertising |= ADVERTISED_10baseT_Half;
2846 if (np->advertising & ADVERTISE_10FULL)
2847 ecmd->advertising |= ADVERTISED_10baseT_Full;
2848 if (np->advertising & ADVERTISE_100HALF)
2849 ecmd->advertising |= ADVERTISED_100baseT_Half;
2850 if (np->advertising & ADVERTISE_100FULL)
2851 ecmd->advertising |= ADVERTISED_100baseT_Full;
2852 ecmd->supported = (SUPPORTED_Autoneg |
2853 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2854 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2855 SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE);
2856 ecmd->phy_address = np->phy_addr_external;
2857 /*
2858 * We intentionally report the phy address of the external
2859 * phy, even if the internal phy is used. This is necessary
2860 * to work around a deficiency of the ethtool interface:
2861 * It's only possible to query the settings of the active
2862 * port. Therefore
2863 * # ethtool -s ethX port mii
2864 * actually sends an ioctl to switch to port mii with the
2865 * settings that are used for the current active port.
2866 * If we would report a different phy address in this
2867 * command, then
2868 * # ethtool -s ethX port tp;ethtool -s ethX port mii
2869 * would unintentionally change the phy address.
2870 *
2871 * Fortunately the phy address doesn't matter with the
2872 * internal phy...
2873 */
2874
2875 /* set information based on active port type */
2876 switch (ecmd->port) {
2877 default:
2878 case PORT_TP:
2879 ecmd->advertising |= ADVERTISED_TP;
2880 ecmd->transceiver = XCVR_INTERNAL;
2881 break;
2882 case PORT_MII:
2883 ecmd->advertising |= ADVERTISED_MII;
2884 ecmd->transceiver = XCVR_EXTERNAL;
2885 break;
2886 case PORT_FIBRE:
2887 ecmd->advertising |= ADVERTISED_FIBRE;
2888 ecmd->transceiver = XCVR_EXTERNAL;
2889 break;
2890 }
2891
2892 /* if autonegotiation is on, try to return the active speed/duplex */
2893 if (ecmd->autoneg == AUTONEG_ENABLE) {
2894 ecmd->advertising |= ADVERTISED_Autoneg;
2895 tmp = mii_nway_result(
2896 np->advertising & mdio_read(dev, MII_LPA));
2897 if (tmp == LPA_100FULL || tmp == LPA_100HALF)
2898 ecmd->speed = SPEED_100;
2899 else
2900 ecmd->speed = SPEED_10;
2901 if (tmp == LPA_100FULL || tmp == LPA_10FULL)
2902 ecmd->duplex = DUPLEX_FULL;
2903 else
2904 ecmd->duplex = DUPLEX_HALF;
2905 }
2906
2907 /* ignore maxtxpkt, maxrxpkt for now */
2908
2909 return 0;
2910}
2911
2912static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2913{
2914 struct netdev_private *np = netdev_priv(dev);
2915
2916 if (ecmd->port != PORT_TP && ecmd->port != PORT_MII && ecmd->port != PORT_FIBRE)
2917 return -EINVAL;
2918 if (ecmd->transceiver != XCVR_INTERNAL && ecmd->transceiver != XCVR_EXTERNAL)
2919 return -EINVAL;
2920 if (ecmd->autoneg == AUTONEG_ENABLE) {
2921 if ((ecmd->advertising & (ADVERTISED_10baseT_Half |
2922 ADVERTISED_10baseT_Full |
2923 ADVERTISED_100baseT_Half |
2924 ADVERTISED_100baseT_Full)) == 0) {
2925 return -EINVAL;
2926 }
2927 } else if (ecmd->autoneg == AUTONEG_DISABLE) {
2928 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
2929 return -EINVAL;
2930 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
2931 return -EINVAL;
2932 } else {
2933 return -EINVAL;
2934 }
2935
2936 /*
2937 * maxtxpkt, maxrxpkt: ignored for now.
2938 *
2939 * transceiver:
2940 * PORT_TP is always XCVR_INTERNAL, PORT_MII and PORT_FIBRE are always
2941 * XCVR_EXTERNAL. The implementation thus ignores ecmd->transceiver and
2942 * selects based on ecmd->port.
2943 *
2944 * Actually PORT_FIBRE is nearly identical to PORT_MII: it's for fibre
2945 * phys that are connected to the mii bus. It's used to apply fibre
2946 * specific updates.
2947 */
2948
2949 /* WHEW! now lets bang some bits */
2950
2951 /* save the parms */
2952 dev->if_port = ecmd->port;
2953 np->autoneg = ecmd->autoneg;
2954 np->phy_addr_external = ecmd->phy_address & PhyAddrMask;
2955 if (np->autoneg == AUTONEG_ENABLE) {
2956 /* advertise only what has been requested */
2957 np->advertising &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
2958 if (ecmd->advertising & ADVERTISED_10baseT_Half)
2959 np->advertising |= ADVERTISE_10HALF;
2960 if (ecmd->advertising & ADVERTISED_10baseT_Full)
2961 np->advertising |= ADVERTISE_10FULL;
2962 if (ecmd->advertising & ADVERTISED_100baseT_Half)
2963 np->advertising |= ADVERTISE_100HALF;
2964 if (ecmd->advertising & ADVERTISED_100baseT_Full)
2965 np->advertising |= ADVERTISE_100FULL;
2966 } else {
2967 np->speed = ecmd->speed;
2968 np->duplex = ecmd->duplex;
2969 /* user overriding the initial full duplex parm? */
2970 if (np->duplex == DUPLEX_HALF)
2971 np->full_duplex = 0;
2972 }
2973
2974 /* get the right phy enabled */
2975 if (ecmd->port == PORT_TP)
2976 switch_port_internal(dev);
2977 else
2978 switch_port_external(dev);
2979
2980 /* set parms and see how this affected our link status */
2981 init_phy_fixup(dev);
2982 check_link(dev);
2983 return 0;
2984}
2985
2986static int netdev_get_regs(struct net_device *dev, u8 *buf)
2987{
2988 int i;
2989 int j;
2990 u32 rfcr;
2991 u32 *rbuf = (u32 *)buf;
2992 void __iomem * ioaddr = ns_ioaddr(dev);
2993
2994 /* read non-mii page 0 of registers */
2995 for (i = 0; i < NATSEMI_PG0_NREGS/2; i++) {
2996 rbuf[i] = readl(ioaddr + i*4);
2997 }
2998
2999 /* read current mii registers */
3000 for (i = NATSEMI_PG0_NREGS/2; i < NATSEMI_PG0_NREGS; i++)
3001 rbuf[i] = mdio_read(dev, i & 0x1f);
3002
3003 /* read only the 'magic' registers from page 1 */
3004 writew(1, ioaddr + PGSEL);
3005 rbuf[i++] = readw(ioaddr + PMDCSR);
3006 rbuf[i++] = readw(ioaddr + TSTDAT);
3007 rbuf[i++] = readw(ioaddr + DSPCFG);
3008 rbuf[i++] = readw(ioaddr + SDCFG);
3009 writew(0, ioaddr + PGSEL);
3010
3011 /* read RFCR indexed registers */
3012 rfcr = readl(ioaddr + RxFilterAddr);
3013 for (j = 0; j < NATSEMI_RFDR_NREGS; j++) {
3014 writel(j*2, ioaddr + RxFilterAddr);
3015 rbuf[i++] = readw(ioaddr + RxFilterData);
3016 }
3017 writel(rfcr, ioaddr + RxFilterAddr);
3018
3019 /* the interrupt status is clear-on-read - see if we missed any */
3020 if (rbuf[4] & rbuf[5]) {
3021 printk(KERN_WARNING
3022 "%s: shoot, we dropped an interrupt (%#08x)\n",
3023 dev->name, rbuf[4] & rbuf[5]);
3024 }
3025
3026 return 0;
3027}
3028
3029#define SWAP_BITS(x) ( (((x) & 0x0001) << 15) | (((x) & 0x0002) << 13) \
3030 | (((x) & 0x0004) << 11) | (((x) & 0x0008) << 9) \
3031 | (((x) & 0x0010) << 7) | (((x) & 0x0020) << 5) \
3032 | (((x) & 0x0040) << 3) | (((x) & 0x0080) << 1) \
3033 | (((x) & 0x0100) >> 1) | (((x) & 0x0200) >> 3) \
3034 | (((x) & 0x0400) >> 5) | (((x) & 0x0800) >> 7) \
3035 | (((x) & 0x1000) >> 9) | (((x) & 0x2000) >> 11) \
3036 | (((x) & 0x4000) >> 13) | (((x) & 0x8000) >> 15) )
3037
3038static int netdev_get_eeprom(struct net_device *dev, u8 *buf)
3039{
3040 int i;
3041 u16 *ebuf = (u16 *)buf;
3042 void __iomem * ioaddr = ns_ioaddr(dev);
Mark Browna8b4cf42006-03-28 14:08:55 -08003043 struct netdev_private *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 /* eeprom_read reads 16 bits, and indexes by 16 bits */
Mark Browna8b4cf42006-03-28 14:08:55 -08003046 for (i = 0; i < np->eeprom_size/2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 ebuf[i] = eeprom_read(ioaddr, i);
3048 /* The EEPROM itself stores data bit-swapped, but eeprom_read
3049 * reads it back "sanely". So we swap it back here in order to
3050 * present it to userland as it is stored. */
3051 ebuf[i] = SWAP_BITS(ebuf[i]);
3052 }
3053 return 0;
3054}
3055
3056static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3057{
3058 struct mii_ioctl_data *data = if_mii(rq);
3059 struct netdev_private *np = netdev_priv(dev);
3060
3061 switch(cmd) {
3062 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
3063 case SIOCDEVPRIVATE: /* for binary compat, remove in 2.5 */
3064 data->phy_id = np->phy_addr_external;
3065 /* Fall Through */
3066
3067 case SIOCGMIIREG: /* Read MII PHY register. */
3068 case SIOCDEVPRIVATE+1: /* for binary compat, remove in 2.5 */
3069 /* The phy_id is not enough to uniquely identify
3070 * the intended target. Therefore the command is sent to
3071 * the given mii on the current port.
3072 */
3073 if (dev->if_port == PORT_TP) {
3074 if ((data->phy_id & 0x1f) == np->phy_addr_external)
3075 data->val_out = mdio_read(dev,
3076 data->reg_num & 0x1f);
3077 else
3078 data->val_out = 0;
3079 } else {
3080 move_int_phy(dev, data->phy_id & 0x1f);
3081 data->val_out = miiport_read(dev, data->phy_id & 0x1f,
3082 data->reg_num & 0x1f);
3083 }
3084 return 0;
3085
3086 case SIOCSMIIREG: /* Write MII PHY register. */
3087 case SIOCDEVPRIVATE+2: /* for binary compat, remove in 2.5 */
3088 if (!capable(CAP_NET_ADMIN))
3089 return -EPERM;
3090 if (dev->if_port == PORT_TP) {
3091 if ((data->phy_id & 0x1f) == np->phy_addr_external) {
3092 if ((data->reg_num & 0x1f) == MII_ADVERTISE)
3093 np->advertising = data->val_in;
3094 mdio_write(dev, data->reg_num & 0x1f,
3095 data->val_in);
3096 }
3097 } else {
3098 if ((data->phy_id & 0x1f) == np->phy_addr_external) {
3099 if ((data->reg_num & 0x1f) == MII_ADVERTISE)
3100 np->advertising = data->val_in;
3101 }
3102 move_int_phy(dev, data->phy_id & 0x1f);
3103 miiport_write(dev, data->phy_id & 0x1f,
3104 data->reg_num & 0x1f,
3105 data->val_in);
3106 }
3107 return 0;
3108 default:
3109 return -EOPNOTSUPP;
3110 }
3111}
3112
3113static void enable_wol_mode(struct net_device *dev, int enable_intr)
3114{
3115 void __iomem * ioaddr = ns_ioaddr(dev);
3116 struct netdev_private *np = netdev_priv(dev);
3117
3118 if (netif_msg_wol(np))
3119 printk(KERN_INFO "%s: remaining active for wake-on-lan\n",
3120 dev->name);
3121
3122 /* For WOL we must restart the rx process in silent mode.
3123 * Write NULL to the RxRingPtr. Only possible if
3124 * rx process is stopped
3125 */
3126 writel(0, ioaddr + RxRingPtr);
3127
3128 /* read WoL status to clear */
3129 readl(ioaddr + WOLCmd);
3130
3131 /* PME on, clear status */
3132 writel(np->SavedClkRun | PMEEnable | PMEStatus, ioaddr + ClkRun);
3133
3134 /* and restart the rx process */
3135 writel(RxOn, ioaddr + ChipCmd);
3136
3137 if (enable_intr) {
3138 /* enable the WOL interrupt.
3139 * Could be used to send a netlink message.
3140 */
3141 writel(WOLPkt | LinkChange, ioaddr + IntrMask);
3142 writel(1, ioaddr + IntrEnable);
3143 }
3144}
3145
3146static int netdev_close(struct net_device *dev)
3147{
3148 void __iomem * ioaddr = ns_ioaddr(dev);
3149 struct netdev_private *np = netdev_priv(dev);
3150
3151 if (netif_msg_ifdown(np))
3152 printk(KERN_DEBUG
3153 "%s: Shutting down ethercard, status was %#04x.\n",
3154 dev->name, (int)readl(ioaddr + ChipCmd));
3155 if (netif_msg_pktdata(np))
3156 printk(KERN_DEBUG
3157 "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
3158 dev->name, np->cur_tx, np->dirty_tx,
3159 np->cur_rx, np->dirty_rx);
3160
3161 /*
3162 * FIXME: what if someone tries to close a device
3163 * that is suspended?
3164 * Should we reenable the nic to switch to
3165 * the final WOL settings?
3166 */
3167
3168 del_timer_sync(&np->timer);
3169 disable_irq(dev->irq);
3170 spin_lock_irq(&np->lock);
Mark Brownb27a16b2006-02-02 00:00:01 +00003171 natsemi_irq_disable(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 np->hands_off = 1;
3173 spin_unlock_irq(&np->lock);
3174 enable_irq(dev->irq);
3175
3176 free_irq(dev->irq, dev);
3177
3178 /* Interrupt disabled, interrupt handler released,
3179 * queue stopped, timer deleted, rtnl_lock held
3180 * All async codepaths that access the driver are disabled.
3181 */
3182 spin_lock_irq(&np->lock);
3183 np->hands_off = 0;
3184 readl(ioaddr + IntrMask);
3185 readw(ioaddr + MIntrStatus);
3186
3187 /* Freeze Stats */
3188 writel(StatsFreeze, ioaddr + StatsCtrl);
3189
3190 /* Stop the chip's Tx and Rx processes. */
3191 natsemi_stop_rxtx(dev);
3192
3193 __get_stats(dev);
3194 spin_unlock_irq(&np->lock);
3195
3196 /* clear the carrier last - an interrupt could reenable it otherwise */
3197 netif_carrier_off(dev);
3198 netif_stop_queue(dev);
3199
3200 dump_ring(dev);
3201 drain_ring(dev);
3202 free_ring(dev);
3203
3204 {
3205 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
3206 if (wol) {
3207 /* restart the NIC in WOL mode.
3208 * The nic must be stopped for this.
3209 */
3210 enable_wol_mode(dev, 0);
3211 } else {
3212 /* Restore PME enable bit unmolested */
3213 writel(np->SavedClkRun, ioaddr + ClkRun);
3214 }
3215 }
3216 return 0;
3217}
3218
3219
3220static void __devexit natsemi_remove1 (struct pci_dev *pdev)
3221{
3222 struct net_device *dev = pci_get_drvdata(pdev);
3223 void __iomem * ioaddr = ns_ioaddr(dev);
3224
3225 unregister_netdev (dev);
3226 pci_release_regions (pdev);
3227 iounmap(ioaddr);
3228 free_netdev (dev);
3229 pci_set_drvdata(pdev, NULL);
3230}
3231
3232#ifdef CONFIG_PM
3233
3234/*
3235 * The ns83815 chip doesn't have explicit RxStop bits.
3236 * Kicking the Rx or Tx process for a new packet reenables the Rx process
3237 * of the nic, thus this function must be very careful:
3238 *
3239 * suspend/resume synchronization:
3240 * entry points:
3241 * netdev_open, netdev_close, netdev_ioctl, set_rx_mode, intr_handler,
3242 * start_tx, tx_timeout
3243 *
3244 * No function accesses the hardware without checking np->hands_off.
3245 * the check occurs under spin_lock_irq(&np->lock);
3246 * exceptions:
3247 * * netdev_ioctl: noncritical access.
3248 * * netdev_open: cannot happen due to the device_detach
3249 * * netdev_close: doesn't hurt.
3250 * * netdev_timer: timer stopped by natsemi_suspend.
3251 * * intr_handler: doesn't acquire the spinlock. suspend calls
3252 * disable_irq() to enforce synchronization.
Mark Brownb27a16b2006-02-02 00:00:01 +00003253 * * natsemi_poll: checks before reenabling interrupts. suspend
3254 * sets hands_off, disables interrupts and then waits with
3255 * netif_poll_disable().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 *
3257 * Interrupts must be disabled, otherwise hands_off can cause irq storms.
3258 */
3259
3260static int natsemi_suspend (struct pci_dev *pdev, pm_message_t state)
3261{
3262 struct net_device *dev = pci_get_drvdata (pdev);
3263 struct netdev_private *np = netdev_priv(dev);
3264 void __iomem * ioaddr = ns_ioaddr(dev);
3265
3266 rtnl_lock();
3267 if (netif_running (dev)) {
3268 del_timer_sync(&np->timer);
3269
3270 disable_irq(dev->irq);
3271 spin_lock_irq(&np->lock);
3272
3273 writel(0, ioaddr + IntrEnable);
3274 np->hands_off = 1;
3275 natsemi_stop_rxtx(dev);
3276 netif_stop_queue(dev);
3277
3278 spin_unlock_irq(&np->lock);
3279 enable_irq(dev->irq);
3280
Mark Brownb27a16b2006-02-02 00:00:01 +00003281 netif_poll_disable(dev);
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* Update the error counts. */
3284 __get_stats(dev);
3285
3286 /* pci_power_off(pdev, -1); */
3287 drain_ring(dev);
3288 {
3289 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
3290 /* Restore PME enable bit */
3291 if (wol) {
3292 /* restart the NIC in WOL mode.
3293 * The nic must be stopped for this.
3294 * FIXME: use the WOL interrupt
3295 */
3296 enable_wol_mode(dev, 0);
3297 } else {
3298 /* Restore PME enable bit unmolested */
3299 writel(np->SavedClkRun, ioaddr + ClkRun);
3300 }
3301 }
3302 }
3303 netif_device_detach(dev);
3304 rtnl_unlock();
3305 return 0;
3306}
3307
3308
3309static int natsemi_resume (struct pci_dev *pdev)
3310{
3311 struct net_device *dev = pci_get_drvdata (pdev);
3312 struct netdev_private *np = netdev_priv(dev);
3313
3314 rtnl_lock();
3315 if (netif_device_present(dev))
3316 goto out;
3317 if (netif_running(dev)) {
3318 BUG_ON(!np->hands_off);
3319 pci_enable_device(pdev);
3320 /* pci_power_on(pdev); */
3321
3322 natsemi_reset(dev);
3323 init_ring(dev);
3324 disable_irq(dev->irq);
3325 spin_lock_irq(&np->lock);
3326 np->hands_off = 0;
3327 init_registers(dev);
3328 netif_device_attach(dev);
3329 spin_unlock_irq(&np->lock);
3330 enable_irq(dev->irq);
3331
3332 mod_timer(&np->timer, jiffies + 1*HZ);
3333 }
3334 netif_device_attach(dev);
Mark Brownb27a16b2006-02-02 00:00:01 +00003335 netif_poll_enable(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336out:
3337 rtnl_unlock();
3338 return 0;
3339}
3340
3341#endif /* CONFIG_PM */
3342
3343static struct pci_driver natsemi_driver = {
3344 .name = DRV_NAME,
3345 .id_table = natsemi_pci_tbl,
3346 .probe = natsemi_probe1,
3347 .remove = __devexit_p(natsemi_remove1),
3348#ifdef CONFIG_PM
3349 .suspend = natsemi_suspend,
3350 .resume = natsemi_resume,
3351#endif
3352};
3353
3354static int __init natsemi_init_mod (void)
3355{
3356/* when a module, this is printed whether or not devices are found in probe */
3357#ifdef MODULE
3358 printk(version);
3359#endif
3360
3361 return pci_module_init (&natsemi_driver);
3362}
3363
3364static void __exit natsemi_exit_mod (void)
3365{
3366 pci_unregister_driver (&natsemi_driver);
3367}
3368
3369module_init(natsemi_init_mod);
3370module_exit(natsemi_exit_mod);
3371