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