[netdrvr] natsemi: Separate out media initialization code
[deliverable/linux.git] / drivers / net / natsemi.c
CommitLineData
1da177e4
LT
1/* 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)
b27a16b7 6 Portions copyright 2004 Harald Welte <laforge@gnumonks.org>
1da177e4
LT
7
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
1da177e4
LT
139*/
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>
b27a16b7 162#include <linux/prefetch.h>
1da177e4
LT
163#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
1da177e4
LT
186static 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. */
f71e1309 190static const int multicast_filter_limit = 100;
1da177e4
LT
191
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))
a8b4cf42 229#define NATSEMI_DEF_EEPROM_SIZE 24 /* 12 16-bit values */
1da177e4
LT
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. */
e19360f2 241static const char version[] __devinitdata =
1da177e4
LT
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
1da177e4
LT
252module_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);
1da177e4
LT
257MODULE_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
932ff279 321The rx process usually runs under the netif_tx_lock. If np->intr_tx_reap
1da177e4
LT
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);
932ff279 326 netif_tx_lock_bh(dev);
1da177e4
LT
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 */
f71e1309 372static const struct {
1da177e4
LT
373 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;
b27a16b7
MB
689 /* Interrupt status */
690 u32 intr_status;
1da177e4
LT
691 /* 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;
a8b4cf42
MB
717 /* EEPROM data */
718 int eeprom_size;
1da177e4
LT
719};
720
721static void move_int_phy(struct net_device *dev, int addr);
722static int eeprom_read(void __iomem *ioaddr, int location);
723static int mdio_read(struct net_device *dev, int reg);
724static void mdio_write(struct net_device *dev, int reg, u16 data);
725static void init_phy_fixup(struct net_device *dev);
726static int miiport_read(struct net_device *dev, int phy_id, int reg);
727static void miiport_write(struct net_device *dev, int phy_id, int reg, u16 data);
728static int find_mii(struct net_device *dev);
729static void natsemi_reset(struct net_device *dev);
730static void natsemi_reload_eeprom(struct net_device *dev);
731static void natsemi_stop_rxtx(struct net_device *dev);
732static int netdev_open(struct net_device *dev);
733static void do_cable_magic(struct net_device *dev);
734static void undo_cable_magic(struct net_device *dev);
735static void check_link(struct net_device *dev);
736static void netdev_timer(unsigned long data);
737static void dump_ring(struct net_device *dev);
738static void tx_timeout(struct net_device *dev);
739static int alloc_ring(struct net_device *dev);
740static void refill_rx(struct net_device *dev);
741static void init_ring(struct net_device *dev);
742static void drain_tx(struct net_device *dev);
743static void drain_ring(struct net_device *dev);
744static void free_ring(struct net_device *dev);
745static void reinit_ring(struct net_device *dev);
746static void init_registers(struct net_device *dev);
747static int start_tx(struct sk_buff *skb, struct net_device *dev);
748static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
749static void netdev_error(struct net_device *dev, int intr_status);
b27a16b7
MB
750static int natsemi_poll(struct net_device *dev, int *budget);
751static void netdev_rx(struct net_device *dev, int *work_done, int work_to_do);
1da177e4
LT
752static void netdev_tx_done(struct net_device *dev);
753static int natsemi_change_mtu(struct net_device *dev, int new_mtu);
754#ifdef CONFIG_NET_POLL_CONTROLLER
755static void natsemi_poll_controller(struct net_device *dev);
756#endif
757static void __set_rx_mode(struct net_device *dev);
758static void set_rx_mode(struct net_device *dev);
759static void __get_stats(struct net_device *dev);
760static struct net_device_stats *get_stats(struct net_device *dev);
761static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
762static int netdev_set_wol(struct net_device *dev, u32 newval);
763static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur);
764static int netdev_set_sopass(struct net_device *dev, u8 *newval);
765static int netdev_get_sopass(struct net_device *dev, u8 *data);
766static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
767static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
768static void enable_wol_mode(struct net_device *dev, int enable_intr);
769static int netdev_close(struct net_device *dev);
770static int netdev_get_regs(struct net_device *dev, u8 *buf);
771static int netdev_get_eeprom(struct net_device *dev, u8 *buf);
772static struct ethtool_ops ethtool_ops;
773
774static inline void __iomem *ns_ioaddr(struct net_device *dev)
775{
776 return (void __iomem *) dev->base_addr;
777}
778
b27a16b7
MB
779static inline void natsemi_irq_enable(struct net_device *dev)
780{
781 writel(1, ns_ioaddr(dev) + IntrEnable);
782 readl(ns_ioaddr(dev) + IntrEnable);
783}
784
785static inline void natsemi_irq_disable(struct net_device *dev)
786{
787 writel(0, ns_ioaddr(dev) + IntrEnable);
788 readl(ns_ioaddr(dev) + IntrEnable);
789}
790
1da177e4
LT
791static void move_int_phy(struct net_device *dev, int addr)
792{
793 struct netdev_private *np = netdev_priv(dev);
794 void __iomem *ioaddr = ns_ioaddr(dev);
795 int target = 31;
796
797 /*
798 * The internal phy is visible on the external mii bus. Therefore we must
799 * move it away before we can send commands to an external phy.
800 * There are two addresses we must avoid:
801 * - the address on the external phy that is used for transmission.
802 * - the address that we want to access. User space can access phys
803 * on the mii bus with SIOCGMIIREG/SIOCSMIIREG, independant from the
804 * phy that is used for transmission.
805 */
806
807 if (target == addr)
808 target--;
809 if (target == np->phy_addr_external)
810 target--;
811 writew(target, ioaddr + PhyCtrl);
812 readw(ioaddr + PhyCtrl);
813 udelay(1);
814}
815
5a40f09b
JG
816static void __devinit natsemi_init_media (struct net_device *dev)
817{
818 struct netdev_private *np = netdev_priv(dev);
819 u32 tmp;
820
821 netif_carrier_off(dev);
822
823 /* get the initial settings from hardware */
824 tmp = mdio_read(dev, MII_BMCR);
825 np->speed = (tmp & BMCR_SPEED100)? SPEED_100 : SPEED_10;
826 np->duplex = (tmp & BMCR_FULLDPLX)? DUPLEX_FULL : DUPLEX_HALF;
827 np->autoneg = (tmp & BMCR_ANENABLE)? AUTONEG_ENABLE: AUTONEG_DISABLE;
828 np->advertising= mdio_read(dev, MII_ADVERTISE);
829
830 if ((np->advertising & ADVERTISE_ALL) != ADVERTISE_ALL
831 && netif_msg_probe(np)) {
832 printk(KERN_INFO "natsemi %s: Transceiver default autonegotiation %s "
833 "10%s %s duplex.\n",
834 pci_name(np->pci_dev),
835 (mdio_read(dev, MII_BMCR) & BMCR_ANENABLE)?
836 "enabled, advertise" : "disabled, force",
837 (np->advertising &
838 (ADVERTISE_100FULL|ADVERTISE_100HALF))?
839 "0" : "",
840 (np->advertising &
841 (ADVERTISE_100FULL|ADVERTISE_10FULL))?
842 "full" : "half");
843 }
844 if (netif_msg_probe(np))
845 printk(KERN_INFO
846 "natsemi %s: Transceiver status %#04x advertising %#04x.\n",
847 pci_name(np->pci_dev), mdio_read(dev, MII_BMSR),
848 np->advertising);
849
850}
851
1da177e4
LT
852static int __devinit natsemi_probe1 (struct pci_dev *pdev,
853 const struct pci_device_id *ent)
854{
855 struct net_device *dev;
856 struct netdev_private *np;
857 int i, option, irq, chip_idx = ent->driver_data;
858 static int find_cnt = -1;
859 unsigned long iostart, iosize;
860 void __iomem *ioaddr;
861 const int pcibar = 1; /* PCI base address register */
862 int prev_eedata;
863 u32 tmp;
864
865/* when built into the kernel, we only print version if device is found */
866#ifndef MODULE
867 static int printed_version;
868 if (!printed_version++)
869 printk(version);
870#endif
871
872 i = pci_enable_device(pdev);
873 if (i) return i;
874
875 /* natsemi has a non-standard PM control register
876 * in PCI config space. Some boards apparently need
877 * to be brought to D0 in this manner.
878 */
879 pci_read_config_dword(pdev, PCIPM, &tmp);
880 if (tmp & PCI_PM_CTRL_STATE_MASK) {
881 /* D0 state, disable PME assertion */
882 u32 newtmp = tmp & ~PCI_PM_CTRL_STATE_MASK;
883 pci_write_config_dword(pdev, PCIPM, newtmp);
884 }
885
886 find_cnt++;
887 iostart = pci_resource_start(pdev, pcibar);
888 iosize = pci_resource_len(pdev, pcibar);
889 irq = pdev->irq;
890
891 if (natsemi_pci_info[chip_idx].flags & PCI_USES_MASTER)
892 pci_set_master(pdev);
893
894 dev = alloc_etherdev(sizeof (struct netdev_private));
895 if (!dev)
896 return -ENOMEM;
897 SET_MODULE_OWNER(dev);
898 SET_NETDEV_DEV(dev, &pdev->dev);
899
900 i = pci_request_regions(pdev, DRV_NAME);
901 if (i)
902 goto err_pci_request_regions;
903
904 ioaddr = ioremap(iostart, iosize);
905 if (!ioaddr) {
906 i = -ENOMEM;
907 goto err_ioremap;
908 }
909
910 /* Work around the dropped serial bit. */
911 prev_eedata = eeprom_read(ioaddr, 6);
912 for (i = 0; i < 3; i++) {
913 int eedata = eeprom_read(ioaddr, i + 7);
914 dev->dev_addr[i*2] = (eedata << 1) + (prev_eedata >> 15);
915 dev->dev_addr[i*2+1] = eedata >> 7;
916 prev_eedata = eedata;
917 }
918
919 dev->base_addr = (unsigned long __force) ioaddr;
920 dev->irq = irq;
921
922 np = netdev_priv(dev);
923
924 np->pci_dev = pdev;
925 pci_set_drvdata(pdev, dev);
926 np->iosize = iosize;
927 spin_lock_init(&np->lock);
928 np->msg_enable = (debug >= 0) ? (1<<debug)-1 : NATSEMI_DEF_MSG;
929 np->hands_off = 0;
b27a16b7 930 np->intr_status = 0;
a8b4cf42 931 np->eeprom_size = NATSEMI_DEF_EEPROM_SIZE;
1da177e4
LT
932
933 /* Initial port:
934 * - If the nic was configured to use an external phy and if find_mii
935 * finds a phy: use external port, first phy that replies.
936 * - Otherwise: internal port.
937 * Note that the phy address for the internal phy doesn't matter:
938 * The address would be used to access a phy over the mii bus, but
939 * the internal phy is accessed through mapped registers.
940 */
941 if (readl(ioaddr + ChipConfig) & CfgExtPhy)
942 dev->if_port = PORT_MII;
943 else
944 dev->if_port = PORT_TP;
945 /* Reset the chip to erase previous misconfiguration. */
946 natsemi_reload_eeprom(dev);
947 natsemi_reset(dev);
948
949 if (dev->if_port != PORT_TP) {
950 np->phy_addr_external = find_mii(dev);
951 if (np->phy_addr_external == PHY_ADDR_NONE) {
952 dev->if_port = PORT_TP;
953 np->phy_addr_external = PHY_ADDR_INTERNAL;
954 }
955 } else {
956 np->phy_addr_external = PHY_ADDR_INTERNAL;
957 }
958
959 option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
960 if (dev->mem_start)
961 option = dev->mem_start;
962
963 /* The lower four bits are the media type. */
964 if (option) {
965 if (option & 0x200)
966 np->full_duplex = 1;
967 if (option & 15)
968 printk(KERN_INFO
969 "natsemi %s: ignoring user supplied media type %d",
970 pci_name(np->pci_dev), option & 15);
971 }
972 if (find_cnt < MAX_UNITS && full_duplex[find_cnt])
973 np->full_duplex = 1;
974
975 /* The chip-specific entries in the device structure. */
976 dev->open = &netdev_open;
977 dev->hard_start_xmit = &start_tx;
978 dev->stop = &netdev_close;
979 dev->get_stats = &get_stats;
980 dev->set_multicast_list = &set_rx_mode;
981 dev->change_mtu = &natsemi_change_mtu;
982 dev->do_ioctl = &netdev_ioctl;
983 dev->tx_timeout = &tx_timeout;
984 dev->watchdog_timeo = TX_TIMEOUT;
b27a16b7
MB
985 dev->poll = natsemi_poll;
986 dev->weight = 64;
987
1da177e4
LT
988#ifdef CONFIG_NET_POLL_CONTROLLER
989 dev->poll_controller = &natsemi_poll_controller;
990#endif
991 SET_ETHTOOL_OPS(dev, &ethtool_ops);
992
993 if (mtu)
994 dev->mtu = mtu;
995
5a40f09b 996 natsemi_init_media(dev);
1da177e4
LT
997
998 /* save the silicon revision for later querying */
999 np->srr = readl(ioaddr + SiliconRev);
1000 if (netif_msg_hw(np))
1001 printk(KERN_INFO "natsemi %s: silicon revision %#04x.\n",
1002 pci_name(np->pci_dev), np->srr);
1003
1004 i = register_netdev(dev);
1005 if (i)
1006 goto err_register_netdev;
1007
1008 if (netif_msg_drv(np)) {
1009 printk(KERN_INFO "natsemi %s: %s at %#08lx (%s), ",
1010 dev->name, natsemi_pci_info[chip_idx].name, iostart,
1011 pci_name(np->pci_dev));
1012 for (i = 0; i < ETH_ALEN-1; i++)
1013 printk("%02x:", dev->dev_addr[i]);
1014 printk("%02x, IRQ %d", dev->dev_addr[i], irq);
1015 if (dev->if_port == PORT_TP)
1016 printk(", port TP.\n");
1017 else
1018 printk(", port MII, phy ad %d.\n", np->phy_addr_external);
1019 }
1020 return 0;
1021
1022 err_register_netdev:
1023 iounmap(ioaddr);
1024
1025 err_ioremap:
1026 pci_release_regions(pdev);
1027 pci_set_drvdata(pdev, NULL);
1028
1029 err_pci_request_regions:
1030 free_netdev(dev);
1031 return i;
1032}
1033
1034
1035/* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
1036 The EEPROM code is for the common 93c06/46 EEPROMs with 6 bit addresses. */
1037
1038/* Delay between EEPROM clock transitions.
1039 No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
1040 a delay. Note that pre-2.0.34 kernels had a cache-alignment bug that
1041 made udelay() unreliable.
1042 The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
1043 depricated.
1044*/
1045#define eeprom_delay(ee_addr) readl(ee_addr)
1046
1047#define EE_Write0 (EE_ChipSelect)
1048#define EE_Write1 (EE_ChipSelect | EE_DataIn)
1049
1050/* The EEPROM commands include the alway-set leading bit. */
1051enum EEPROM_Cmds {
1052 EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
1053};
1054
1055static int eeprom_read(void __iomem *addr, int location)
1056{
1057 int i;
1058 int retval = 0;
1059 void __iomem *ee_addr = addr + EECtrl;
1060 int read_cmd = location | EE_ReadCmd;
1061
1062 writel(EE_Write0, ee_addr);
1063
1064 /* Shift the read command bits out. */
1065 for (i = 10; i >= 0; i--) {
1066 short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
1067 writel(dataval, ee_addr);
1068 eeprom_delay(ee_addr);
1069 writel(dataval | EE_ShiftClk, ee_addr);
1070 eeprom_delay(ee_addr);
1071 }
1072 writel(EE_ChipSelect, ee_addr);
1073 eeprom_delay(ee_addr);
1074
1075 for (i = 0; i < 16; i++) {
1076 writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
1077 eeprom_delay(ee_addr);
1078 retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0;
1079 writel(EE_ChipSelect, ee_addr);
1080 eeprom_delay(ee_addr);
1081 }
1082
1083 /* Terminate the EEPROM access. */
1084 writel(EE_Write0, ee_addr);
1085 writel(0, ee_addr);
1086 return retval;
1087}
1088
1089/* MII transceiver control section.
1090 * The 83815 series has an internal transceiver, and we present the
1091 * internal management registers as if they were MII connected.
1092 * External Phy registers are referenced through the MII interface.
1093 */
1094
1095/* clock transitions >= 20ns (25MHz)
1096 * One readl should be good to PCI @ 100MHz
1097 */
1098#define mii_delay(ioaddr) readl(ioaddr + EECtrl)
1099
1100static int mii_getbit (struct net_device *dev)
1101{
1102 int data;
1103 void __iomem *ioaddr = ns_ioaddr(dev);
1104
1105 writel(MII_ShiftClk, ioaddr + EECtrl);
1106 data = readl(ioaddr + EECtrl);
1107 writel(0, ioaddr + EECtrl);
1108 mii_delay(ioaddr);
1109 return (data & MII_Data)? 1 : 0;
1110}
1111
1112static void mii_send_bits (struct net_device *dev, u32 data, int len)
1113{
1114 u32 i;
1115 void __iomem *ioaddr = ns_ioaddr(dev);
1116
1117 for (i = (1 << (len-1)); i; i >>= 1)
1118 {
1119 u32 mdio_val = MII_Write | ((data & i)? MII_Data : 0);
1120 writel(mdio_val, ioaddr + EECtrl);
1121 mii_delay(ioaddr);
1122 writel(mdio_val | MII_ShiftClk, ioaddr + EECtrl);
1123 mii_delay(ioaddr);
1124 }
1125 writel(0, ioaddr + EECtrl);
1126 mii_delay(ioaddr);
1127}
1128
1129static int miiport_read(struct net_device *dev, int phy_id, int reg)
1130{
1131 u32 cmd;
1132 int i;
1133 u32 retval = 0;
1134
1135 /* Ensure sync */
1136 mii_send_bits (dev, 0xffffffff, 32);
1137 /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
1138 /* ST,OP = 0110'b for read operation */
1139 cmd = (0x06 << 10) | (phy_id << 5) | reg;
1140 mii_send_bits (dev, cmd, 14);
1141 /* Turnaround */
1142 if (mii_getbit (dev))
1143 return 0;
1144 /* Read data */
1145 for (i = 0; i < 16; i++) {
1146 retval <<= 1;
1147 retval |= mii_getbit (dev);
1148 }
1149 /* End cycle */
1150 mii_getbit (dev);
1151 return retval;
1152}
1153
1154static void miiport_write(struct net_device *dev, int phy_id, int reg, u16 data)
1155{
1156 u32 cmd;
1157
1158 /* Ensure sync */
1159 mii_send_bits (dev, 0xffffffff, 32);
1160 /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
1161 /* ST,OP,AAAAA,RRRRR,TA = 0101xxxxxxxxxx10'b = 0x5002 for write */
1162 cmd = (0x5002 << 16) | (phy_id << 23) | (reg << 18) | data;
1163 mii_send_bits (dev, cmd, 32);
1164 /* End cycle */
1165 mii_getbit (dev);
1166}
1167
1168static int mdio_read(struct net_device *dev, int reg)
1169{
1170 struct netdev_private *np = netdev_priv(dev);
1171 void __iomem *ioaddr = ns_ioaddr(dev);
1172
1173 /* The 83815 series has two ports:
1174 * - an internal transceiver
1175 * - an external mii bus
1176 */
1177 if (dev->if_port == PORT_TP)
1178 return readw(ioaddr+BasicControl+(reg<<2));
1179 else
1180 return miiport_read(dev, np->phy_addr_external, reg);
1181}
1182
1183static void mdio_write(struct net_device *dev, int reg, u16 data)
1184{
1185 struct netdev_private *np = netdev_priv(dev);
1186 void __iomem *ioaddr = ns_ioaddr(dev);
1187
1188 /* The 83815 series has an internal transceiver; handle separately */
1189 if (dev->if_port == PORT_TP)
1190 writew(data, ioaddr+BasicControl+(reg<<2));
1191 else
1192 miiport_write(dev, np->phy_addr_external, reg, data);
1193}
1194
1195static void init_phy_fixup(struct net_device *dev)
1196{
1197 struct netdev_private *np = netdev_priv(dev);
1198 void __iomem *ioaddr = ns_ioaddr(dev);
1199 int i;
1200 u32 cfg;
1201 u16 tmp;
1202
1203 /* restore stuff lost when power was out */
1204 tmp = mdio_read(dev, MII_BMCR);
1205 if (np->autoneg == AUTONEG_ENABLE) {
1206 /* renegotiate if something changed */
1207 if ((tmp & BMCR_ANENABLE) == 0
1208 || np->advertising != mdio_read(dev, MII_ADVERTISE))
1209 {
1210 /* turn on autonegotiation and force negotiation */
1211 tmp |= (BMCR_ANENABLE | BMCR_ANRESTART);
1212 mdio_write(dev, MII_ADVERTISE, np->advertising);
1213 }
1214 } else {
1215 /* turn off auto negotiation, set speed and duplexity */
1216 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
1217 if (np->speed == SPEED_100)
1218 tmp |= BMCR_SPEED100;
1219 if (np->duplex == DUPLEX_FULL)
1220 tmp |= BMCR_FULLDPLX;
1221 /*
1222 * Note: there is no good way to inform the link partner
1223 * that our capabilities changed. The user has to unplug
1224 * and replug the network cable after some changes, e.g.
1225 * after switching from 10HD, autoneg off to 100 HD,
1226 * autoneg off.
1227 */
1228 }
1229 mdio_write(dev, MII_BMCR, tmp);
1230 readl(ioaddr + ChipConfig);
1231 udelay(1);
1232
1233 /* find out what phy this is */
1234 np->mii = (mdio_read(dev, MII_PHYSID1) << 16)
1235 + mdio_read(dev, MII_PHYSID2);
1236
1237 /* handle external phys here */
1238 switch (np->mii) {
1239 case PHYID_AM79C874:
1240 /* phy specific configuration for fibre/tp operation */
1241 tmp = mdio_read(dev, MII_MCTRL);
1242 tmp &= ~(MII_FX_SEL | MII_EN_SCRM);
1243 if (dev->if_port == PORT_FIBRE)
1244 tmp |= MII_FX_SEL;
1245 else
1246 tmp |= MII_EN_SCRM;
1247 mdio_write(dev, MII_MCTRL, tmp);
1248 break;
1249 default:
1250 break;
1251 }
1252 cfg = readl(ioaddr + ChipConfig);
1253 if (cfg & CfgExtPhy)
1254 return;
1255
1256 /* On page 78 of the spec, they recommend some settings for "optimum
1257 performance" to be done in sequence. These settings optimize some
1258 of the 100Mbit autodetection circuitry. They say we only want to
1259 do this for rev C of the chip, but engineers at NSC (Bradley
1260 Kennedy) recommends always setting them. If you don't, you get
1261 errors on some autonegotiations that make the device unusable.
1262
1263 It seems that the DSP needs a few usec to reinitialize after
1264 the start of the phy. Just retry writing these values until they
1265 stick.
1266 */
1267 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1268
1269 int dspcfg;
1270 writew(1, ioaddr + PGSEL);
1271 writew(PMDCSR_VAL, ioaddr + PMDCSR);
1272 writew(TSTDAT_VAL, ioaddr + TSTDAT);
1273 np->dspcfg = (np->srr <= SRR_DP83815_C)?
1274 DSPCFG_VAL : (DSPCFG_COEF | readw(ioaddr + DSPCFG));
1275 writew(np->dspcfg, ioaddr + DSPCFG);
1276 writew(SDCFG_VAL, ioaddr + SDCFG);
1277 writew(0, ioaddr + PGSEL);
1278 readl(ioaddr + ChipConfig);
1279 udelay(10);
1280
1281 writew(1, ioaddr + PGSEL);
1282 dspcfg = readw(ioaddr + DSPCFG);
1283 writew(0, ioaddr + PGSEL);
1284 if (np->dspcfg == dspcfg)
1285 break;
1286 }
1287
1288 if (netif_msg_link(np)) {
1289 if (i==NATSEMI_HW_TIMEOUT) {
1290 printk(KERN_INFO
1291 "%s: DSPCFG mismatch after retrying for %d usec.\n",
1292 dev->name, i*10);
1293 } else {
1294 printk(KERN_INFO
1295 "%s: DSPCFG accepted after %d usec.\n",
1296 dev->name, i*10);
1297 }
1298 }
1299 /*
1300 * Enable PHY Specific event based interrupts. Link state change
1301 * and Auto-Negotiation Completion are among the affected.
1302 * Read the intr status to clear it (needed for wake events).
1303 */
1304 readw(ioaddr + MIntrStatus);
1305 writew(MICRIntEn, ioaddr + MIntrCtrl);
1306}
1307
1308static int switch_port_external(struct net_device *dev)
1309{
1310 struct netdev_private *np = netdev_priv(dev);
1311 void __iomem *ioaddr = ns_ioaddr(dev);
1312 u32 cfg;
1313
1314 cfg = readl(ioaddr + ChipConfig);
1315 if (cfg & CfgExtPhy)
1316 return 0;
1317
1318 if (netif_msg_link(np)) {
1319 printk(KERN_INFO "%s: switching to external transceiver.\n",
1320 dev->name);
1321 }
1322
1323 /* 1) switch back to external phy */
1324 writel(cfg | (CfgExtPhy | CfgPhyDis), ioaddr + ChipConfig);
1325 readl(ioaddr + ChipConfig);
1326 udelay(1);
1327
1328 /* 2) reset the external phy: */
1329 /* resetting the external PHY has been known to cause a hub supplying
1330 * power over Ethernet to kill the power. We don't want to kill
1331 * power to this computer, so we avoid resetting the phy.
1332 */
1333
1334 /* 3) reinit the phy fixup, it got lost during power down. */
1335 move_int_phy(dev, np->phy_addr_external);
1336 init_phy_fixup(dev);
1337
1338 return 1;
1339}
1340
1341static int switch_port_internal(struct net_device *dev)
1342{
1343 struct netdev_private *np = netdev_priv(dev);
1344 void __iomem *ioaddr = ns_ioaddr(dev);
1345 int i;
1346 u32 cfg;
1347 u16 bmcr;
1348
1349 cfg = readl(ioaddr + ChipConfig);
1350 if (!(cfg &CfgExtPhy))
1351 return 0;
1352
1353 if (netif_msg_link(np)) {
1354 printk(KERN_INFO "%s: switching to internal transceiver.\n",
1355 dev->name);
1356 }
1357 /* 1) switch back to internal phy: */
1358 cfg = cfg & ~(CfgExtPhy | CfgPhyDis);
1359 writel(cfg, ioaddr + ChipConfig);
1360 readl(ioaddr + ChipConfig);
1361 udelay(1);
1362
1363 /* 2) reset the internal phy: */
1364 bmcr = readw(ioaddr+BasicControl+(MII_BMCR<<2));
1365 writel(bmcr | BMCR_RESET, ioaddr+BasicControl+(MII_BMCR<<2));
1366 readl(ioaddr + ChipConfig);
1367 udelay(10);
1368 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1369 bmcr = readw(ioaddr+BasicControl+(MII_BMCR<<2));
1370 if (!(bmcr & BMCR_RESET))
1371 break;
1372 udelay(10);
1373 }
1374 if (i==NATSEMI_HW_TIMEOUT && netif_msg_link(np)) {
1375 printk(KERN_INFO
1376 "%s: phy reset did not complete in %d usec.\n",
1377 dev->name, i*10);
1378 }
1379 /* 3) reinit the phy fixup, it got lost during power down. */
1380 init_phy_fixup(dev);
1381
1382 return 1;
1383}
1384
1385/* Scan for a PHY on the external mii bus.
1386 * There are two tricky points:
1387 * - Do not scan while the internal phy is enabled. The internal phy will
1388 * crash: e.g. reads from the DSPCFG register will return odd values and
1389 * the nasty random phy reset code will reset the nic every few seconds.
1390 * - The internal phy must be moved around, an external phy could
1391 * have the same address as the internal phy.
1392 */
1393static int find_mii(struct net_device *dev)
1394{
1395 struct netdev_private *np = netdev_priv(dev);
1396 int tmp;
1397 int i;
1398 int did_switch;
1399
1400 /* Switch to external phy */
1401 did_switch = switch_port_external(dev);
1402
1403 /* Scan the possible phy addresses:
1404 *
1405 * PHY address 0 means that the phy is in isolate mode. Not yet
1406 * supported due to lack of test hardware. User space should
1407 * handle it through ethtool.
1408 */
1409 for (i = 1; i <= 31; i++) {
1410 move_int_phy(dev, i);
1411 tmp = miiport_read(dev, i, MII_BMSR);
1412 if (tmp != 0xffff && tmp != 0x0000) {
1413 /* found something! */
1414 np->mii = (mdio_read(dev, MII_PHYSID1) << 16)
1415 + mdio_read(dev, MII_PHYSID2);
1416 if (netif_msg_probe(np)) {
1417 printk(KERN_INFO "natsemi %s: found external phy %08x at address %d.\n",
1418 pci_name(np->pci_dev), np->mii, i);
1419 }
1420 break;
1421 }
1422 }
1423 /* And switch back to internal phy: */
1424 if (did_switch)
1425 switch_port_internal(dev);
1426 return i;
1427}
1428
1429/* CFG bits [13:16] [18:23] */
1430#define CFG_RESET_SAVE 0xfde000
1431/* WCSR bits [0:4] [9:10] */
1432#define WCSR_RESET_SAVE 0x61f
1433/* RFCR bits [20] [22] [27:31] */
1434#define RFCR_RESET_SAVE 0xf8500000;
1435
1436static void natsemi_reset(struct net_device *dev)
1437{
1438 int i;
1439 u32 cfg;
1440 u32 wcsr;
1441 u32 rfcr;
1442 u16 pmatch[3];
1443 u16 sopass[3];
1444 struct netdev_private *np = netdev_priv(dev);
1445 void __iomem *ioaddr = ns_ioaddr(dev);
1446
1447 /*
1448 * Resetting the chip causes some registers to be lost.
1449 * Natsemi suggests NOT reloading the EEPROM while live, so instead
1450 * we save the state that would have been loaded from EEPROM
1451 * on a normal power-up (see the spec EEPROM map). This assumes
1452 * whoever calls this will follow up with init_registers() eventually.
1453 */
1454
1455 /* CFG */
1456 cfg = readl(ioaddr + ChipConfig) & CFG_RESET_SAVE;
1457 /* WCSR */
1458 wcsr = readl(ioaddr + WOLCmd) & WCSR_RESET_SAVE;
1459 /* RFCR */
1460 rfcr = readl(ioaddr + RxFilterAddr) & RFCR_RESET_SAVE;
1461 /* PMATCH */
1462 for (i = 0; i < 3; i++) {
1463 writel(i*2, ioaddr + RxFilterAddr);
1464 pmatch[i] = readw(ioaddr + RxFilterData);
1465 }
1466 /* SOPAS */
1467 for (i = 0; i < 3; i++) {
1468 writel(0xa+(i*2), ioaddr + RxFilterAddr);
1469 sopass[i] = readw(ioaddr + RxFilterData);
1470 }
1471
1472 /* now whack the chip */
1473 writel(ChipReset, ioaddr + ChipCmd);
1474 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1475 if (!(readl(ioaddr + ChipCmd) & ChipReset))
1476 break;
1477 udelay(5);
1478 }
1479 if (i==NATSEMI_HW_TIMEOUT) {
1480 printk(KERN_WARNING "%s: reset did not complete in %d usec.\n",
1481 dev->name, i*5);
1482 } else if (netif_msg_hw(np)) {
1483 printk(KERN_DEBUG "%s: reset completed in %d usec.\n",
1484 dev->name, i*5);
1485 }
1486
1487 /* restore CFG */
1488 cfg |= readl(ioaddr + ChipConfig) & ~CFG_RESET_SAVE;
1489 /* turn on external phy if it was selected */
1490 if (dev->if_port == PORT_TP)
1491 cfg &= ~(CfgExtPhy | CfgPhyDis);
1492 else
1493 cfg |= (CfgExtPhy | CfgPhyDis);
1494 writel(cfg, ioaddr + ChipConfig);
1495 /* restore WCSR */
1496 wcsr |= readl(ioaddr + WOLCmd) & ~WCSR_RESET_SAVE;
1497 writel(wcsr, ioaddr + WOLCmd);
1498 /* read RFCR */
1499 rfcr |= readl(ioaddr + RxFilterAddr) & ~RFCR_RESET_SAVE;
1500 /* restore PMATCH */
1501 for (i = 0; i < 3; i++) {
1502 writel(i*2, ioaddr + RxFilterAddr);
1503 writew(pmatch[i], ioaddr + RxFilterData);
1504 }
1505 for (i = 0; i < 3; i++) {
1506 writel(0xa+(i*2), ioaddr + RxFilterAddr);
1507 writew(sopass[i], ioaddr + RxFilterData);
1508 }
1509 /* restore RFCR */
1510 writel(rfcr, ioaddr + RxFilterAddr);
1511}
1512
e72fd96e
MB
1513static void reset_rx(struct net_device *dev)
1514{
1515 int i;
1516 struct netdev_private *np = netdev_priv(dev);
1517 void __iomem *ioaddr = ns_ioaddr(dev);
1518
1519 np->intr_status &= ~RxResetDone;
1520
1521 writel(RxReset, ioaddr + ChipCmd);
1522
1523 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1524 np->intr_status |= readl(ioaddr + IntrStatus);
1525 if (np->intr_status & RxResetDone)
1526 break;
1527 udelay(15);
1528 }
1529 if (i==NATSEMI_HW_TIMEOUT) {
1530 printk(KERN_WARNING "%s: RX reset did not complete in %d usec.\n",
1531 dev->name, i*15);
1532 } else if (netif_msg_hw(np)) {
1533 printk(KERN_WARNING "%s: RX reset took %d usec.\n",
1534 dev->name, i*15);
1535 }
1536}
1537
1da177e4
LT
1538static void natsemi_reload_eeprom(struct net_device *dev)
1539{
1540 struct netdev_private *np = netdev_priv(dev);
1541 void __iomem *ioaddr = ns_ioaddr(dev);
1542 int i;
1543
1544 writel(EepromReload, ioaddr + PCIBusCfg);
1545 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1546 udelay(50);
1547 if (!(readl(ioaddr + PCIBusCfg) & EepromReload))
1548 break;
1549 }
1550 if (i==NATSEMI_HW_TIMEOUT) {
1551 printk(KERN_WARNING "natsemi %s: EEPROM did not reload in %d usec.\n",
1552 pci_name(np->pci_dev), i*50);
1553 } else if (netif_msg_hw(np)) {
1554 printk(KERN_DEBUG "natsemi %s: EEPROM reloaded in %d usec.\n",
1555 pci_name(np->pci_dev), i*50);
1556 }
1557}
1558
1559static void natsemi_stop_rxtx(struct net_device *dev)
1560{
1561 void __iomem * ioaddr = ns_ioaddr(dev);
1562 struct netdev_private *np = netdev_priv(dev);
1563 int i;
1564
1565 writel(RxOff | TxOff, ioaddr + ChipCmd);
1566 for(i=0;i< NATSEMI_HW_TIMEOUT;i++) {
1567 if ((readl(ioaddr + ChipCmd) & (TxOn|RxOn)) == 0)
1568 break;
1569 udelay(5);
1570 }
1571 if (i==NATSEMI_HW_TIMEOUT) {
1572 printk(KERN_WARNING "%s: Tx/Rx process did not stop in %d usec.\n",
1573 dev->name, i*5);
1574 } else if (netif_msg_hw(np)) {
1575 printk(KERN_DEBUG "%s: Tx/Rx process stopped in %d usec.\n",
1576 dev->name, i*5);
1577 }
1578}
1579
1580static int netdev_open(struct net_device *dev)
1581{
1582 struct netdev_private *np = netdev_priv(dev);
1583 void __iomem * ioaddr = ns_ioaddr(dev);
1584 int i;
1585
1586 /* Reset the chip, just in case. */
1587 natsemi_reset(dev);
1588
1589 i = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
1590 if (i) return i;
1591
1592 if (netif_msg_ifup(np))
1593 printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
1594 dev->name, dev->irq);
1595 i = alloc_ring(dev);
1596 if (i < 0) {
1597 free_irq(dev->irq, dev);
1598 return i;
1599 }
1600 init_ring(dev);
1601 spin_lock_irq(&np->lock);
1602 init_registers(dev);
1603 /* now set the MAC address according to dev->dev_addr */
1604 for (i = 0; i < 3; i++) {
1605 u16 mac = (dev->dev_addr[2*i+1]<<8) + dev->dev_addr[2*i];
1606
1607 writel(i*2, ioaddr + RxFilterAddr);
1608 writew(mac, ioaddr + RxFilterData);
1609 }
1610 writel(np->cur_rx_mode, ioaddr + RxFilterAddr);
1611 spin_unlock_irq(&np->lock);
1612
1613 netif_start_queue(dev);
1614
1615 if (netif_msg_ifup(np))
1616 printk(KERN_DEBUG "%s: Done netdev_open(), status: %#08x.\n",
1617 dev->name, (int)readl(ioaddr + ChipCmd));
1618
1619 /* Set the timer to check for link beat. */
1620 init_timer(&np->timer);
1621 np->timer.expires = jiffies + NATSEMI_TIMER_FREQ;
1622 np->timer.data = (unsigned long)dev;
1623 np->timer.function = &netdev_timer; /* timer handler */
1624 add_timer(&np->timer);
1625
1626 return 0;
1627}
1628
1629static void do_cable_magic(struct net_device *dev)
1630{
1631 struct netdev_private *np = netdev_priv(dev);
1632 void __iomem *ioaddr = ns_ioaddr(dev);
1633
1634 if (dev->if_port != PORT_TP)
1635 return;
1636
1637 if (np->srr >= SRR_DP83816_A5)
1638 return;
1639
1640 /*
1641 * 100 MBit links with short cables can trip an issue with the chip.
1642 * The problem manifests as lots of CRC errors and/or flickering
1643 * activity LED while idle. This process is based on instructions
1644 * from engineers at National.
1645 */
1646 if (readl(ioaddr + ChipConfig) & CfgSpeed100) {
1647 u16 data;
1648
1649 writew(1, ioaddr + PGSEL);
1650 /*
1651 * coefficient visibility should already be enabled via
1652 * DSPCFG | 0x1000
1653 */
1654 data = readw(ioaddr + TSTDAT) & 0xff;
1655 /*
1656 * the value must be negative, and within certain values
1657 * (these values all come from National)
1658 */
1659 if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
1660 struct netdev_private *np = netdev_priv(dev);
1661
1662 /* the bug has been triggered - fix the coefficient */
1663 writew(TSTDAT_FIXED, ioaddr + TSTDAT);
1664 /* lock the value */
1665 data = readw(ioaddr + DSPCFG);
1666 np->dspcfg = data | DSPCFG_LOCK;
1667 writew(np->dspcfg, ioaddr + DSPCFG);
1668 }
1669 writew(0, ioaddr + PGSEL);
1670 }
1671}
1672
1673static void undo_cable_magic(struct net_device *dev)
1674{
1675 u16 data;
1676 struct netdev_private *np = netdev_priv(dev);
1677 void __iomem * ioaddr = ns_ioaddr(dev);
1678
1679 if (dev->if_port != PORT_TP)
1680 return;
1681
1682 if (np->srr >= SRR_DP83816_A5)
1683 return;
1684
1685 writew(1, ioaddr + PGSEL);
1686 /* make sure the lock bit is clear */
1687 data = readw(ioaddr + DSPCFG);
1688 np->dspcfg = data & ~DSPCFG_LOCK;
1689 writew(np->dspcfg, ioaddr + DSPCFG);
1690 writew(0, ioaddr + PGSEL);
1691}
1692
1693static void check_link(struct net_device *dev)
1694{
1695 struct netdev_private *np = netdev_priv(dev);
1696 void __iomem * ioaddr = ns_ioaddr(dev);
1697 int duplex;
1698 u16 bmsr;
1699
1700 /* The link status field is latched: it remains low after a temporary
1701 * link failure until it's read. We need the current link status,
1702 * thus read twice.
1703 */
1704 mdio_read(dev, MII_BMSR);
1705 bmsr = mdio_read(dev, MII_BMSR);
1706
1707 if (!(bmsr & BMSR_LSTATUS)) {
1708 if (netif_carrier_ok(dev)) {
1709 if (netif_msg_link(np))
1710 printk(KERN_NOTICE "%s: link down.\n",
1711 dev->name);
1712 netif_carrier_off(dev);
1713 undo_cable_magic(dev);
1714 }
1715 return;
1716 }
1717 if (!netif_carrier_ok(dev)) {
1718 if (netif_msg_link(np))
1719 printk(KERN_NOTICE "%s: link up.\n", dev->name);
1720 netif_carrier_on(dev);
1721 do_cable_magic(dev);
1722 }
1723
1724 duplex = np->full_duplex;
1725 if (!duplex) {
1726 if (bmsr & BMSR_ANEGCOMPLETE) {
1727 int tmp = mii_nway_result(
1728 np->advertising & mdio_read(dev, MII_LPA));
1729 if (tmp == LPA_100FULL || tmp == LPA_10FULL)
1730 duplex = 1;
1731 } else if (mdio_read(dev, MII_BMCR) & BMCR_FULLDPLX)
1732 duplex = 1;
1733 }
1734
1735 /* if duplex is set then bit 28 must be set, too */
1736 if (duplex ^ !!(np->rx_config & RxAcceptTx)) {
1737 if (netif_msg_link(np))
1738 printk(KERN_INFO
1739 "%s: Setting %s-duplex based on negotiated "
1740 "link capability.\n", dev->name,
1741 duplex ? "full" : "half");
1742 if (duplex) {
1743 np->rx_config |= RxAcceptTx;
1744 np->tx_config |= TxCarrierIgn | TxHeartIgn;
1745 } else {
1746 np->rx_config &= ~RxAcceptTx;
1747 np->tx_config &= ~(TxCarrierIgn | TxHeartIgn);
1748 }
1749 writel(np->tx_config, ioaddr + TxConfig);
1750 writel(np->rx_config, ioaddr + RxConfig);
1751 }
1752}
1753
1754static void init_registers(struct net_device *dev)
1755{
1756 struct netdev_private *np = netdev_priv(dev);
1757 void __iomem * ioaddr = ns_ioaddr(dev);
1758
1759 init_phy_fixup(dev);
1760
1761 /* clear any interrupts that are pending, such as wake events */
1762 readl(ioaddr + IntrStatus);
1763
1764 writel(np->ring_dma, ioaddr + RxRingPtr);
1765 writel(np->ring_dma + RX_RING_SIZE * sizeof(struct netdev_desc),
1766 ioaddr + TxRingPtr);
1767
1768 /* Initialize other registers.
1769 * Configure the PCI bus bursts and FIFO thresholds.
1770 * Configure for standard, in-spec Ethernet.
1771 * Start with half-duplex. check_link will update
1772 * to the correct settings.
1773 */
1774
1775 /* DRTH: 2: start tx if 64 bytes are in the fifo
1776 * FLTH: 0x10: refill with next packet if 512 bytes are free
1777 * MXDMA: 0: up to 256 byte bursts.
1778 * MXDMA must be <= FLTH
1779 * ECRETRY=1
1780 * ATP=1
1781 */
1782 np->tx_config = TxAutoPad | TxCollRetry | TxMxdma_256 |
1783 TX_FLTH_VAL | TX_DRTH_VAL_START;
1784 writel(np->tx_config, ioaddr + TxConfig);
1785
1786 /* DRTH 0x10: start copying to memory if 128 bytes are in the fifo
1787 * MXDMA 0: up to 256 byte bursts
1788 */
1789 np->rx_config = RxMxdma_256 | RX_DRTH_VAL;
1790 /* if receive ring now has bigger buffers than normal, enable jumbo */
1791 if (np->rx_buf_sz > NATSEMI_LONGPKT)
1792 np->rx_config |= RxAcceptLong;
1793
1794 writel(np->rx_config, ioaddr + RxConfig);
1795
1796 /* Disable PME:
1797 * The PME bit is initialized from the EEPROM contents.
1798 * PCI cards probably have PME disabled, but motherboard
1799 * implementations may have PME set to enable WakeOnLan.
1800 * With PME set the chip will scan incoming packets but
1801 * nothing will be written to memory. */
1802 np->SavedClkRun = readl(ioaddr + ClkRun);
1803 writel(np->SavedClkRun & ~PMEEnable, ioaddr + ClkRun);
1804 if (np->SavedClkRun & PMEStatus && netif_msg_wol(np)) {
1805 printk(KERN_NOTICE "%s: Wake-up event %#08x\n",
1806 dev->name, readl(ioaddr + WOLCmd));
1807 }
1808
1809 check_link(dev);
1810 __set_rx_mode(dev);
1811
1812 /* Enable interrupts by setting the interrupt mask. */
1813 writel(DEFAULT_INTR, ioaddr + IntrMask);
1814 writel(1, ioaddr + IntrEnable);
1815
1816 writel(RxOn | TxOn, ioaddr + ChipCmd);
1817 writel(StatsClear, ioaddr + StatsCtrl); /* Clear Stats */
1818}
1819
1820/*
1821 * netdev_timer:
1822 * Purpose:
1823 * 1) check for link changes. Usually they are handled by the MII interrupt
1824 * but it doesn't hurt to check twice.
1825 * 2) check for sudden death of the NIC:
1826 * It seems that a reference set for this chip went out with incorrect info,
1827 * and there exist boards that aren't quite right. An unexpected voltage
1828 * drop can cause the PHY to get itself in a weird state (basically reset).
1829 * NOTE: this only seems to affect revC chips.
1830 * 3) check of death of the RX path due to OOM
1831 */
1832static void netdev_timer(unsigned long data)
1833{
1834 struct net_device *dev = (struct net_device *)data;
1835 struct netdev_private *np = netdev_priv(dev);
1836 void __iomem * ioaddr = ns_ioaddr(dev);
1837 int next_tick = 5*HZ;
1838
1839 if (netif_msg_timer(np)) {
1840 /* DO NOT read the IntrStatus register,
1841 * a read clears any pending interrupts.
1842 */
1843 printk(KERN_DEBUG "%s: Media selection timer tick.\n",
1844 dev->name);
1845 }
1846
1847 if (dev->if_port == PORT_TP) {
1848 u16 dspcfg;
1849
1850 spin_lock_irq(&np->lock);
1851 /* check for a nasty random phy-reset - use dspcfg as a flag */
1852 writew(1, ioaddr+PGSEL);
1853 dspcfg = readw(ioaddr+DSPCFG);
1854 writew(0, ioaddr+PGSEL);
1855 if (dspcfg != np->dspcfg) {
1856 if (!netif_queue_stopped(dev)) {
1857 spin_unlock_irq(&np->lock);
1858 if (netif_msg_hw(np))
1859 printk(KERN_NOTICE "%s: possible phy reset: "
1860 "re-initializing\n", dev->name);
1861 disable_irq(dev->irq);
1862 spin_lock_irq(&np->lock);
1863 natsemi_stop_rxtx(dev);
1864 dump_ring(dev);
1865 reinit_ring(dev);
1866 init_registers(dev);
1867 spin_unlock_irq(&np->lock);
1868 enable_irq(dev->irq);
1869 } else {
1870 /* hurry back */
1871 next_tick = HZ;
1872 spin_unlock_irq(&np->lock);
1873 }
1874 } else {
1875 /* init_registers() calls check_link() for the above case */
1876 check_link(dev);
1877 spin_unlock_irq(&np->lock);
1878 }
1879 } else {
1880 spin_lock_irq(&np->lock);
1881 check_link(dev);
1882 spin_unlock_irq(&np->lock);
1883 }
1884 if (np->oom) {
1885 disable_irq(dev->irq);
1886 np->oom = 0;
1887 refill_rx(dev);
1888 enable_irq(dev->irq);
1889 if (!np->oom) {
1890 writel(RxOn, ioaddr + ChipCmd);
1891 } else {
1892 next_tick = 1;
1893 }
1894 }
1895 mod_timer(&np->timer, jiffies + next_tick);
1896}
1897
1898static void dump_ring(struct net_device *dev)
1899{
1900 struct netdev_private *np = netdev_priv(dev);
1901
1902 if (netif_msg_pktdata(np)) {
1903 int i;
1904 printk(KERN_DEBUG " Tx ring at %p:\n", np->tx_ring);
1905 for (i = 0; i < TX_RING_SIZE; i++) {
1906 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1907 i, np->tx_ring[i].next_desc,
1908 np->tx_ring[i].cmd_status,
1909 np->tx_ring[i].addr);
1910 }
1911 printk(KERN_DEBUG " Rx ring %p:\n", np->rx_ring);
1912 for (i = 0; i < RX_RING_SIZE; i++) {
1913 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1914 i, np->rx_ring[i].next_desc,
1915 np->rx_ring[i].cmd_status,
1916 np->rx_ring[i].addr);
1917 }
1918 }
1919}
1920
1921static void tx_timeout(struct net_device *dev)
1922{
1923 struct netdev_private *np = netdev_priv(dev);
1924 void __iomem * ioaddr = ns_ioaddr(dev);
1925
1926 disable_irq(dev->irq);
1927 spin_lock_irq(&np->lock);
1928 if (!np->hands_off) {
1929 if (netif_msg_tx_err(np))
1930 printk(KERN_WARNING
1931 "%s: Transmit timed out, status %#08x,"
1932 " resetting...\n",
1933 dev->name, readl(ioaddr + IntrStatus));
1934 dump_ring(dev);
1935
1936 natsemi_reset(dev);
1937 reinit_ring(dev);
1938 init_registers(dev);
1939 } else {
1940 printk(KERN_WARNING
1941 "%s: tx_timeout while in hands_off state?\n",
1942 dev->name);
1943 }
1944 spin_unlock_irq(&np->lock);
1945 enable_irq(dev->irq);
1946
1947 dev->trans_start = jiffies;
1948 np->stats.tx_errors++;
1949 netif_wake_queue(dev);
1950}
1951
1952static int alloc_ring(struct net_device *dev)
1953{
1954 struct netdev_private *np = netdev_priv(dev);
1955 np->rx_ring = pci_alloc_consistent(np->pci_dev,
1956 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
1957 &np->ring_dma);
1958 if (!np->rx_ring)
1959 return -ENOMEM;
1960 np->tx_ring = &np->rx_ring[RX_RING_SIZE];
1961 return 0;
1962}
1963
1964static void refill_rx(struct net_device *dev)
1965{
1966 struct netdev_private *np = netdev_priv(dev);
1967
1968 /* Refill the Rx ring buffers. */
1969 for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1970 struct sk_buff *skb;
1971 int entry = np->dirty_rx % RX_RING_SIZE;
1972 if (np->rx_skbuff[entry] == NULL) {
1973 unsigned int buflen = np->rx_buf_sz+NATSEMI_PADDING;
1974 skb = dev_alloc_skb(buflen);
1975 np->rx_skbuff[entry] = skb;
1976 if (skb == NULL)
1977 break; /* Better luck next round. */
1978 skb->dev = dev; /* Mark as being used by this device. */
1979 np->rx_dma[entry] = pci_map_single(np->pci_dev,
689be439 1980 skb->data, buflen, PCI_DMA_FROMDEVICE);
1da177e4
LT
1981 np->rx_ring[entry].addr = cpu_to_le32(np->rx_dma[entry]);
1982 }
1983 np->rx_ring[entry].cmd_status = cpu_to_le32(np->rx_buf_sz);
1984 }
1985 if (np->cur_rx - np->dirty_rx == RX_RING_SIZE) {
1986 if (netif_msg_rx_err(np))
1987 printk(KERN_WARNING "%s: going OOM.\n", dev->name);
1988 np->oom = 1;
1989 }
1990}
1991
1992static void set_bufsize(struct net_device *dev)
1993{
1994 struct netdev_private *np = netdev_priv(dev);
1995 if (dev->mtu <= ETH_DATA_LEN)
1996 np->rx_buf_sz = ETH_DATA_LEN + NATSEMI_HEADERS;
1997 else
1998 np->rx_buf_sz = dev->mtu + NATSEMI_HEADERS;
1999}
2000
2001/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
2002static void init_ring(struct net_device *dev)
2003{
2004 struct netdev_private *np = netdev_priv(dev);
2005 int i;
2006
2007 /* 1) TX ring */
2008 np->dirty_tx = np->cur_tx = 0;
2009 for (i = 0; i < TX_RING_SIZE; i++) {
2010 np->tx_skbuff[i] = NULL;
2011 np->tx_ring[i].next_desc = cpu_to_le32(np->ring_dma
2012 +sizeof(struct netdev_desc)
2013 *((i+1)%TX_RING_SIZE+RX_RING_SIZE));
2014 np->tx_ring[i].cmd_status = 0;
2015 }
2016
2017 /* 2) RX ring */
2018 np->dirty_rx = 0;
2019 np->cur_rx = RX_RING_SIZE;
2020 np->oom = 0;
2021 set_bufsize(dev);
2022
2023 np->rx_head_desc = &np->rx_ring[0];
2024
2025 /* Please be carefull before changing this loop - at least gcc-2.95.1
2026 * miscompiles it otherwise.
2027 */
2028 /* Initialize all Rx descriptors. */
2029 for (i = 0; i < RX_RING_SIZE; i++) {
2030 np->rx_ring[i].next_desc = cpu_to_le32(np->ring_dma
2031 +sizeof(struct netdev_desc)
2032 *((i+1)%RX_RING_SIZE));
2033 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
2034 np->rx_skbuff[i] = NULL;
2035 }
2036 refill_rx(dev);
2037 dump_ring(dev);
2038}
2039
2040static void drain_tx(struct net_device *dev)
2041{
2042 struct netdev_private *np = netdev_priv(dev);
2043 int i;
2044
2045 for (i = 0; i < TX_RING_SIZE; i++) {
2046 if (np->tx_skbuff[i]) {
2047 pci_unmap_single(np->pci_dev,
2048 np->tx_dma[i], np->tx_skbuff[i]->len,
2049 PCI_DMA_TODEVICE);
2050 dev_kfree_skb(np->tx_skbuff[i]);
2051 np->stats.tx_dropped++;
2052 }
2053 np->tx_skbuff[i] = NULL;
2054 }
2055}
2056
2057static void drain_rx(struct net_device *dev)
2058{
2059 struct netdev_private *np = netdev_priv(dev);
2060 unsigned int buflen = np->rx_buf_sz;
2061 int i;
2062
2063 /* Free all the skbuffs in the Rx queue. */
2064 for (i = 0; i < RX_RING_SIZE; i++) {
2065 np->rx_ring[i].cmd_status = 0;
2066 np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */
2067 if (np->rx_skbuff[i]) {
2068 pci_unmap_single(np->pci_dev,
2069 np->rx_dma[i], buflen,
2070 PCI_DMA_FROMDEVICE);
2071 dev_kfree_skb(np->rx_skbuff[i]);
2072 }
2073 np->rx_skbuff[i] = NULL;
2074 }
2075}
2076
2077static void drain_ring(struct net_device *dev)
2078{
2079 drain_rx(dev);
2080 drain_tx(dev);
2081}
2082
2083static void free_ring(struct net_device *dev)
2084{
2085 struct netdev_private *np = netdev_priv(dev);
2086 pci_free_consistent(np->pci_dev,
2087 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
2088 np->rx_ring, np->ring_dma);
2089}
2090
2091static void reinit_rx(struct net_device *dev)
2092{
2093 struct netdev_private *np = netdev_priv(dev);
2094 int i;
2095
2096 /* RX Ring */
2097 np->dirty_rx = 0;
2098 np->cur_rx = RX_RING_SIZE;
2099 np->rx_head_desc = &np->rx_ring[0];
2100 /* Initialize all Rx descriptors. */
2101 for (i = 0; i < RX_RING_SIZE; i++)
2102 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
2103
2104 refill_rx(dev);
2105}
2106
2107static void reinit_ring(struct net_device *dev)
2108{
2109 struct netdev_private *np = netdev_priv(dev);
2110 int i;
2111
2112 /* drain TX ring */
2113 drain_tx(dev);
2114 np->dirty_tx = np->cur_tx = 0;
2115 for (i=0;i<TX_RING_SIZE;i++)
2116 np->tx_ring[i].cmd_status = 0;
2117
2118 reinit_rx(dev);
2119}
2120
2121static int start_tx(struct sk_buff *skb, struct net_device *dev)
2122{
2123 struct netdev_private *np = netdev_priv(dev);
2124 void __iomem * ioaddr = ns_ioaddr(dev);
2125 unsigned entry;
2126
2127 /* Note: Ordering is important here, set the field with the
2128 "ownership" bit last, and only then increment cur_tx. */
2129
2130 /* Calculate the next Tx descriptor entry. */
2131 entry = np->cur_tx % TX_RING_SIZE;
2132
2133 np->tx_skbuff[entry] = skb;
2134 np->tx_dma[entry] = pci_map_single(np->pci_dev,
2135 skb->data,skb->len, PCI_DMA_TODEVICE);
2136
2137 np->tx_ring[entry].addr = cpu_to_le32(np->tx_dma[entry]);
2138
2139 spin_lock_irq(&np->lock);
2140
2141 if (!np->hands_off) {
2142 np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn | skb->len);
2143 /* StrongARM: Explicitly cache flush np->tx_ring and
2144 * skb->data,skb->len. */
2145 wmb();
2146 np->cur_tx++;
2147 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
2148 netdev_tx_done(dev);
2149 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1)
2150 netif_stop_queue(dev);
2151 }
2152 /* Wake the potentially-idle transmit channel. */
2153 writel(TxOn, ioaddr + ChipCmd);
2154 } else {
2155 dev_kfree_skb_irq(skb);
2156 np->stats.tx_dropped++;
2157 }
2158 spin_unlock_irq(&np->lock);
2159
2160 dev->trans_start = jiffies;
2161
2162 if (netif_msg_tx_queued(np)) {
2163 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
2164 dev->name, np->cur_tx, entry);
2165 }
2166 return 0;
2167}
2168
2169static void netdev_tx_done(struct net_device *dev)
2170{
2171 struct netdev_private *np = netdev_priv(dev);
2172
2173 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
2174 int entry = np->dirty_tx % TX_RING_SIZE;
2175 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescOwn))
2176 break;
2177 if (netif_msg_tx_done(np))
2178 printk(KERN_DEBUG
2179 "%s: tx frame #%d finished, status %#08x.\n",
2180 dev->name, np->dirty_tx,
2181 le32_to_cpu(np->tx_ring[entry].cmd_status));
2182 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescPktOK)) {
2183 np->stats.tx_packets++;
2184 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
2185 } else { /* Various Tx errors */
2186 int tx_status =
2187 le32_to_cpu(np->tx_ring[entry].cmd_status);
2188 if (tx_status & (DescTxAbort|DescTxExcColl))
2189 np->stats.tx_aborted_errors++;
2190 if (tx_status & DescTxFIFO)
2191 np->stats.tx_fifo_errors++;
2192 if (tx_status & DescTxCarrier)
2193 np->stats.tx_carrier_errors++;
2194 if (tx_status & DescTxOOWCol)
2195 np->stats.tx_window_errors++;
2196 np->stats.tx_errors++;
2197 }
2198 pci_unmap_single(np->pci_dev,np->tx_dma[entry],
2199 np->tx_skbuff[entry]->len,
2200 PCI_DMA_TODEVICE);
2201 /* Free the original skb. */
2202 dev_kfree_skb_irq(np->tx_skbuff[entry]);
2203 np->tx_skbuff[entry] = NULL;
2204 }
2205 if (netif_queue_stopped(dev)
2206 && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
2207 /* The ring is no longer full, wake queue. */
2208 netif_wake_queue(dev);
2209 }
2210}
2211
b27a16b7
MB
2212/* The interrupt handler doesn't actually handle interrupts itself, it
2213 * schedules a NAPI poll if there is anything to do. */
1da177e4
LT
2214static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
2215{
2216 struct net_device *dev = dev_instance;
2217 struct netdev_private *np = netdev_priv(dev);
2218 void __iomem * ioaddr = ns_ioaddr(dev);
1da177e4
LT
2219
2220 if (np->hands_off)
2221 return IRQ_NONE;
b27a16b7
MB
2222
2223 /* Reading automatically acknowledges. */
2224 np->intr_status = readl(ioaddr + IntrStatus);
1da177e4 2225
b27a16b7
MB
2226 if (netif_msg_intr(np))
2227 printk(KERN_DEBUG
2228 "%s: Interrupt, status %#08x, mask %#08x.\n",
2229 dev->name, np->intr_status,
2230 readl(ioaddr + IntrMask));
1da177e4 2231
b27a16b7
MB
2232 if (!np->intr_status)
2233 return IRQ_NONE;
1da177e4 2234
b27a16b7
MB
2235 prefetch(&np->rx_skbuff[np->cur_rx % RX_RING_SIZE]);
2236
2237 if (netif_rx_schedule_prep(dev)) {
2238 /* Disable interrupts and register for poll */
2239 natsemi_irq_disable(dev);
2240 __netif_rx_schedule(dev);
2241 }
2242 return IRQ_HANDLED;
2243}
2244
2245/* This is the NAPI poll routine. As well as the standard RX handling
2246 * it also handles all other interrupts that the chip might raise.
2247 */
2248static int natsemi_poll(struct net_device *dev, int *budget)
2249{
2250 struct netdev_private *np = netdev_priv(dev);
2251 void __iomem * ioaddr = ns_ioaddr(dev);
1da177e4 2252
b27a16b7
MB
2253 int work_to_do = min(*budget, dev->quota);
2254 int work_done = 0;
2255
2256 do {
2257 if (np->intr_status &
2258 (IntrTxDone | IntrTxIntr | IntrTxIdle | IntrTxErr)) {
1da177e4
LT
2259 spin_lock(&np->lock);
2260 netdev_tx_done(dev);
2261 spin_unlock(&np->lock);
2262 }
2263
2264 /* Abnormal error summary/uncommon events handlers. */
b27a16b7
MB
2265 if (np->intr_status & IntrAbnormalSummary)
2266 netdev_error(dev, np->intr_status);
2267
2268 if (np->intr_status &
2269 (IntrRxDone | IntrRxIntr | RxStatusFIFOOver |
2270 IntrRxErr | IntrRxOverrun)) {
2271 netdev_rx(dev, &work_done, work_to_do);
1da177e4 2272 }
b27a16b7
MB
2273
2274 *budget -= work_done;
2275 dev->quota -= work_done;
1da177e4 2276
b27a16b7
MB
2277 if (work_done >= work_to_do)
2278 return 1;
2279
2280 np->intr_status = readl(ioaddr + IntrStatus);
2281 } while (np->intr_status);
1da177e4 2282
b27a16b7
MB
2283 netif_rx_complete(dev);
2284
2285 /* Reenable interrupts providing nothing is trying to shut
2286 * the chip down. */
2287 spin_lock(&np->lock);
2288 if (!np->hands_off && netif_running(dev))
2289 natsemi_irq_enable(dev);
2290 spin_unlock(&np->lock);
2291
2292 return 0;
1da177e4
LT
2293}
2294
2295/* This routine is logically part of the interrupt handler, but separated
2296 for clarity and better register allocation. */
b27a16b7 2297static void netdev_rx(struct net_device *dev, int *work_done, int work_to_do)
1da177e4
LT
2298{
2299 struct netdev_private *np = netdev_priv(dev);
2300 int entry = np->cur_rx % RX_RING_SIZE;
2301 int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
2302 s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
2303 unsigned int buflen = np->rx_buf_sz;
2304 void __iomem * ioaddr = ns_ioaddr(dev);
2305
2306 /* If the driver owns the next entry it's a new packet. Send it up. */
2307 while (desc_status < 0) { /* e.g. & DescOwn */
2308 int pkt_len;
2309 if (netif_msg_rx_status(np))
2310 printk(KERN_DEBUG
2311 " netdev_rx() entry %d status was %#08x.\n",
2312 entry, desc_status);
2313 if (--boguscnt < 0)
2314 break;
b27a16b7
MB
2315
2316 if (*work_done >= work_to_do)
2317 break;
2318
2319 (*work_done)++;
2320
1da177e4
LT
2321 pkt_len = (desc_status & DescSizeMask) - 4;
2322 if ((desc_status&(DescMore|DescPktOK|DescRxLong)) != DescPktOK){
2323 if (desc_status & DescMore) {
2324 if (netif_msg_rx_err(np))
2325 printk(KERN_WARNING
2326 "%s: Oversized(?) Ethernet "
2327 "frame spanned multiple "
2328 "buffers, entry %#08x "
2329 "status %#08x.\n", dev->name,
2330 np->cur_rx, desc_status);
2331 np->stats.rx_length_errors++;
e72fd96e
MB
2332
2333 /* The RX state machine has probably
2334 * locked up beneath us. Follow the
2335 * reset procedure documented in
2336 * AN-1287. */
2337
2338 spin_lock_irq(&np->lock);
2339 reset_rx(dev);
2340 reinit_rx(dev);
2341 writel(np->ring_dma, ioaddr + RxRingPtr);
2342 check_link(dev);
2343 spin_unlock_irq(&np->lock);
2344
2345 /* We'll enable RX on exit from this
2346 * function. */
2347 break;
2348
1da177e4
LT
2349 } else {
2350 /* There was an error. */
2351 np->stats.rx_errors++;
2352 if (desc_status & (DescRxAbort|DescRxOver))
2353 np->stats.rx_over_errors++;
2354 if (desc_status & (DescRxLong|DescRxRunt))
2355 np->stats.rx_length_errors++;
2356 if (desc_status & (DescRxInvalid|DescRxAlign))
2357 np->stats.rx_frame_errors++;
2358 if (desc_status & DescRxCRC)
2359 np->stats.rx_crc_errors++;
2360 }
2361 } else if (pkt_len > np->rx_buf_sz) {
2362 /* if this is the tail of a double buffer
2363 * packet, we've already counted the error
2364 * on the first part. Ignore the second half.
2365 */
2366 } else {
2367 struct sk_buff *skb;
2368 /* Omit CRC size. */
2369 /* Check if the packet is long enough to accept
2370 * without copying to a minimally-sized skbuff. */
2371 if (pkt_len < rx_copybreak
2372 && (skb = dev_alloc_skb(pkt_len + RX_OFFSET)) != NULL) {
2373 skb->dev = dev;
2374 /* 16 byte align the IP header */
2375 skb_reserve(skb, RX_OFFSET);
2376 pci_dma_sync_single_for_cpu(np->pci_dev,
2377 np->rx_dma[entry],
2378 buflen,
2379 PCI_DMA_FROMDEVICE);
2380 eth_copy_and_sum(skb,
689be439 2381 np->rx_skbuff[entry]->data, pkt_len, 0);
1da177e4
LT
2382 skb_put(skb, pkt_len);
2383 pci_dma_sync_single_for_device(np->pci_dev,
2384 np->rx_dma[entry],
2385 buflen,
2386 PCI_DMA_FROMDEVICE);
2387 } else {
2388 pci_unmap_single(np->pci_dev, np->rx_dma[entry],
2389 buflen, PCI_DMA_FROMDEVICE);
2390 skb_put(skb = np->rx_skbuff[entry], pkt_len);
2391 np->rx_skbuff[entry] = NULL;
2392 }
2393 skb->protocol = eth_type_trans(skb, dev);
b27a16b7 2394 netif_receive_skb(skb);
1da177e4
LT
2395 dev->last_rx = jiffies;
2396 np->stats.rx_packets++;
2397 np->stats.rx_bytes += pkt_len;
2398 }
2399 entry = (++np->cur_rx) % RX_RING_SIZE;
2400 np->rx_head_desc = &np->rx_ring[entry];
2401 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
2402 }
2403 refill_rx(dev);
2404
2405 /* Restart Rx engine if stopped. */
2406 if (np->oom)
2407 mod_timer(&np->timer, jiffies + 1);
2408 else
2409 writel(RxOn, ioaddr + ChipCmd);
2410}
2411
2412static void netdev_error(struct net_device *dev, int intr_status)
2413{
2414 struct netdev_private *np = netdev_priv(dev);
2415 void __iomem * ioaddr = ns_ioaddr(dev);
2416
2417 spin_lock(&np->lock);
2418 if (intr_status & LinkChange) {
2419 u16 lpa = mdio_read(dev, MII_LPA);
2420 if (mdio_read(dev, MII_BMCR) & BMCR_ANENABLE
2421 && netif_msg_link(np)) {
2422 printk(KERN_INFO
2423 "%s: Autonegotiation advertising"
2424 " %#04x partner %#04x.\n", dev->name,
2425 np->advertising, lpa);
2426 }
2427
2428 /* read MII int status to clear the flag */
2429 readw(ioaddr + MIntrStatus);
2430 check_link(dev);
2431 }
2432 if (intr_status & StatsMax) {
2433 __get_stats(dev);
2434 }
2435 if (intr_status & IntrTxUnderrun) {
2436 if ((np->tx_config & TxDrthMask) < TX_DRTH_VAL_LIMIT) {
2437 np->tx_config += TX_DRTH_VAL_INC;
2438 if (netif_msg_tx_err(np))
2439 printk(KERN_NOTICE
2440 "%s: increased tx threshold, txcfg %#08x.\n",
2441 dev->name, np->tx_config);
2442 } else {
2443 if (netif_msg_tx_err(np))
2444 printk(KERN_NOTICE
2445 "%s: tx underrun with maximum tx threshold, txcfg %#08x.\n",
2446 dev->name, np->tx_config);
2447 }
2448 writel(np->tx_config, ioaddr + TxConfig);
2449 }
2450 if (intr_status & WOLPkt && netif_msg_wol(np)) {
2451 int wol_status = readl(ioaddr + WOLCmd);
2452 printk(KERN_NOTICE "%s: Link wake-up event %#08x\n",
2453 dev->name, wol_status);
2454 }
2455 if (intr_status & RxStatusFIFOOver) {
2456 if (netif_msg_rx_err(np) && netif_msg_intr(np)) {
2457 printk(KERN_NOTICE "%s: Rx status FIFO overrun\n",
2458 dev->name);
2459 }
2460 np->stats.rx_fifo_errors++;
2461 }
2462 /* Hmmmmm, it's not clear how to recover from PCI faults. */
2463 if (intr_status & IntrPCIErr) {
2464 printk(KERN_NOTICE "%s: PCI error %#08x\n", dev->name,
2465 intr_status & IntrPCIErr);
2466 np->stats.tx_fifo_errors++;
2467 np->stats.rx_fifo_errors++;
2468 }
2469 spin_unlock(&np->lock);
2470}
2471
2472static void __get_stats(struct net_device *dev)
2473{
2474 void __iomem * ioaddr = ns_ioaddr(dev);
2475 struct netdev_private *np = netdev_priv(dev);
2476
2477 /* The chip only need report frame silently dropped. */
2478 np->stats.rx_crc_errors += readl(ioaddr + RxCRCErrs);
2479 np->stats.rx_missed_errors += readl(ioaddr + RxMissed);
2480}
2481
2482static struct net_device_stats *get_stats(struct net_device *dev)
2483{
2484 struct netdev_private *np = netdev_priv(dev);
2485
2486 /* The chip only need report frame silently dropped. */
2487 spin_lock_irq(&np->lock);
2488 if (netif_running(dev) && !np->hands_off)
2489 __get_stats(dev);
2490 spin_unlock_irq(&np->lock);
2491
2492 return &np->stats;
2493}
2494
2495#ifdef CONFIG_NET_POLL_CONTROLLER
2496static void natsemi_poll_controller(struct net_device *dev)
2497{
2498 disable_irq(dev->irq);
2499 intr_handler(dev->irq, dev, NULL);
2500 enable_irq(dev->irq);
2501}
2502#endif
2503
2504#define HASH_TABLE 0x200
2505static void __set_rx_mode(struct net_device *dev)
2506{
2507 void __iomem * ioaddr = ns_ioaddr(dev);
2508 struct netdev_private *np = netdev_priv(dev);
2509 u8 mc_filter[64]; /* Multicast hash filter */
2510 u32 rx_mode;
2511
2512 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2513 /* Unconditionally log net taps. */
2514 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
2515 dev->name);
2516 rx_mode = RxFilterEnable | AcceptBroadcast
2517 | AcceptAllMulticast | AcceptAllPhys | AcceptMyPhys;
2518 } else if ((dev->mc_count > multicast_filter_limit)
2519 || (dev->flags & IFF_ALLMULTI)) {
2520 rx_mode = RxFilterEnable | AcceptBroadcast
2521 | AcceptAllMulticast | AcceptMyPhys;
2522 } else {
2523 struct dev_mc_list *mclist;
2524 int i;
2525 memset(mc_filter, 0, sizeof(mc_filter));
2526 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2527 i++, mclist = mclist->next) {
2528 int i = (ether_crc(ETH_ALEN, mclist->dmi_addr) >> 23) & 0x1ff;
2529 mc_filter[i/8] |= (1 << (i & 0x07));
2530 }
2531 rx_mode = RxFilterEnable | AcceptBroadcast
2532 | AcceptMulticast | AcceptMyPhys;
2533 for (i = 0; i < 64; i += 2) {
760f86d7
HX
2534 writel(HASH_TABLE + i, ioaddr + RxFilterAddr);
2535 writel((mc_filter[i + 1] << 8) + mc_filter[i],
2536 ioaddr + RxFilterData);
1da177e4
LT
2537 }
2538 }
2539 writel(rx_mode, ioaddr + RxFilterAddr);
2540 np->cur_rx_mode = rx_mode;
2541}
2542
2543static int natsemi_change_mtu(struct net_device *dev, int new_mtu)
2544{
2545 if (new_mtu < 64 || new_mtu > NATSEMI_RX_LIMIT-NATSEMI_HEADERS)
2546 return -EINVAL;
2547
2548 dev->mtu = new_mtu;
2549
2550 /* synchronized against open : rtnl_lock() held by caller */
2551 if (netif_running(dev)) {
2552 struct netdev_private *np = netdev_priv(dev);
2553 void __iomem * ioaddr = ns_ioaddr(dev);
2554
2555 disable_irq(dev->irq);
2556 spin_lock(&np->lock);
2557 /* stop engines */
2558 natsemi_stop_rxtx(dev);
2559 /* drain rx queue */
2560 drain_rx(dev);
2561 /* change buffers */
2562 set_bufsize(dev);
2563 reinit_rx(dev);
2564 writel(np->ring_dma, ioaddr + RxRingPtr);
2565 /* restart engines */
2566 writel(RxOn | TxOn, ioaddr + ChipCmd);
2567 spin_unlock(&np->lock);
2568 enable_irq(dev->irq);
2569 }
2570 return 0;
2571}
2572
2573static void set_rx_mode(struct net_device *dev)
2574{
2575 struct netdev_private *np = netdev_priv(dev);
2576 spin_lock_irq(&np->lock);
2577 if (!np->hands_off)
2578 __set_rx_mode(dev);
2579 spin_unlock_irq(&np->lock);
2580}
2581
2582static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2583{
2584 struct netdev_private *np = netdev_priv(dev);
2585 strncpy(info->driver, DRV_NAME, ETHTOOL_BUSINFO_LEN);
2586 strncpy(info->version, DRV_VERSION, ETHTOOL_BUSINFO_LEN);
2587 strncpy(info->bus_info, pci_name(np->pci_dev), ETHTOOL_BUSINFO_LEN);
2588}
2589
2590static int get_regs_len(struct net_device *dev)
2591{
2592 return NATSEMI_REGS_SIZE;
2593}
2594
2595static int get_eeprom_len(struct net_device *dev)
2596{
a8b4cf42
MB
2597 struct netdev_private *np = netdev_priv(dev);
2598 return np->eeprom_size;
1da177e4
LT
2599}
2600
2601static int get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2602{
2603 struct netdev_private *np = netdev_priv(dev);
2604 spin_lock_irq(&np->lock);
2605 netdev_get_ecmd(dev, ecmd);
2606 spin_unlock_irq(&np->lock);
2607 return 0;
2608}
2609
2610static int set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2611{
2612 struct netdev_private *np = netdev_priv(dev);
2613 int res;
2614 spin_lock_irq(&np->lock);
2615 res = netdev_set_ecmd(dev, ecmd);
2616 spin_unlock_irq(&np->lock);
2617 return res;
2618}
2619
2620static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2621{
2622 struct netdev_private *np = netdev_priv(dev);
2623 spin_lock_irq(&np->lock);
2624 netdev_get_wol(dev, &wol->supported, &wol->wolopts);
2625 netdev_get_sopass(dev, wol->sopass);
2626 spin_unlock_irq(&np->lock);
2627}
2628
2629static int set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2630{
2631 struct netdev_private *np = netdev_priv(dev);
2632 int res;
2633 spin_lock_irq(&np->lock);
2634 netdev_set_wol(dev, wol->wolopts);
2635 res = netdev_set_sopass(dev, wol->sopass);
2636 spin_unlock_irq(&np->lock);
2637 return res;
2638}
2639
2640static void get_regs(struct net_device *dev, struct ethtool_regs *regs, void *buf)
2641{
2642 struct netdev_private *np = netdev_priv(dev);
2643 regs->version = NATSEMI_REGS_VER;
2644 spin_lock_irq(&np->lock);
2645 netdev_get_regs(dev, buf);
2646 spin_unlock_irq(&np->lock);
2647}
2648
2649static u32 get_msglevel(struct net_device *dev)
2650{
2651 struct netdev_private *np = netdev_priv(dev);
2652 return np->msg_enable;
2653}
2654
2655static void set_msglevel(struct net_device *dev, u32 val)
2656{
2657 struct netdev_private *np = netdev_priv(dev);
2658 np->msg_enable = val;
2659}
2660
2661static int nway_reset(struct net_device *dev)
2662{
2663 int tmp;
2664 int r = -EINVAL;
2665 /* if autoneg is off, it's an error */
2666 tmp = mdio_read(dev, MII_BMCR);
2667 if (tmp & BMCR_ANENABLE) {
2668 tmp |= (BMCR_ANRESTART);
2669 mdio_write(dev, MII_BMCR, tmp);
2670 r = 0;
2671 }
2672 return r;
2673}
2674
2675static u32 get_link(struct net_device *dev)
2676{
2677 /* LSTATUS is latched low until a read - so read twice */
2678 mdio_read(dev, MII_BMSR);
2679 return (mdio_read(dev, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
2680}
2681
2682static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
2683{
2684 struct netdev_private *np = netdev_priv(dev);
a8b4cf42 2685 u8 *eebuf;
1da177e4
LT
2686 int res;
2687
a8b4cf42
MB
2688 eebuf = kmalloc(np->eeprom_size, GFP_KERNEL);
2689 if (!eebuf)
2690 return -ENOMEM;
2691
1da177e4
LT
2692 eeprom->magic = PCI_VENDOR_ID_NS | (PCI_DEVICE_ID_NS_83815<<16);
2693 spin_lock_irq(&np->lock);
2694 res = netdev_get_eeprom(dev, eebuf);
2695 spin_unlock_irq(&np->lock);
2696 if (!res)
2697 memcpy(data, eebuf+eeprom->offset, eeprom->len);
a8b4cf42 2698 kfree(eebuf);
1da177e4
LT
2699 return res;
2700}
2701
2702static struct ethtool_ops ethtool_ops = {
2703 .get_drvinfo = get_drvinfo,
2704 .get_regs_len = get_regs_len,
2705 .get_eeprom_len = get_eeprom_len,
2706 .get_settings = get_settings,
2707 .set_settings = set_settings,
2708 .get_wol = get_wol,
2709 .set_wol = set_wol,
2710 .get_regs = get_regs,
2711 .get_msglevel = get_msglevel,
2712 .set_msglevel = set_msglevel,
2713 .nway_reset = nway_reset,
2714 .get_link = get_link,
2715 .get_eeprom = get_eeprom,
2716};
2717
2718static int netdev_set_wol(struct net_device *dev, u32 newval)
2719{
2720 struct netdev_private *np = netdev_priv(dev);
2721 void __iomem * ioaddr = ns_ioaddr(dev);
2722 u32 data = readl(ioaddr + WOLCmd) & ~WakeOptsSummary;
2723
2724 /* translate to bitmasks this chip understands */
2725 if (newval & WAKE_PHY)
2726 data |= WakePhy;
2727 if (newval & WAKE_UCAST)
2728 data |= WakeUnicast;
2729 if (newval & WAKE_MCAST)
2730 data |= WakeMulticast;
2731 if (newval & WAKE_BCAST)
2732 data |= WakeBroadcast;
2733 if (newval & WAKE_ARP)
2734 data |= WakeArp;
2735 if (newval & WAKE_MAGIC)
2736 data |= WakeMagic;
2737 if (np->srr >= SRR_DP83815_D) {
2738 if (newval & WAKE_MAGICSECURE) {
2739 data |= WakeMagicSecure;
2740 }
2741 }
2742
2743 writel(data, ioaddr + WOLCmd);
2744
2745 return 0;
2746}
2747
2748static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur)
2749{
2750 struct netdev_private *np = netdev_priv(dev);
2751 void __iomem * ioaddr = ns_ioaddr(dev);
2752 u32 regval = readl(ioaddr + WOLCmd);
2753
2754 *supported = (WAKE_PHY | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST
2755 | WAKE_ARP | WAKE_MAGIC);
2756
2757 if (np->srr >= SRR_DP83815_D) {
2758 /* SOPASS works on revD and higher */
2759 *supported |= WAKE_MAGICSECURE;
2760 }
2761 *cur = 0;
2762
2763 /* translate from chip bitmasks */
2764 if (regval & WakePhy)
2765 *cur |= WAKE_PHY;
2766 if (regval & WakeUnicast)
2767 *cur |= WAKE_UCAST;
2768 if (regval & WakeMulticast)
2769 *cur |= WAKE_MCAST;
2770 if (regval & WakeBroadcast)
2771 *cur |= WAKE_BCAST;
2772 if (regval & WakeArp)
2773 *cur |= WAKE_ARP;
2774 if (regval & WakeMagic)
2775 *cur |= WAKE_MAGIC;
2776 if (regval & WakeMagicSecure) {
2777 /* this can be on in revC, but it's broken */
2778 *cur |= WAKE_MAGICSECURE;
2779 }
2780
2781 return 0;
2782}
2783
2784static int netdev_set_sopass(struct net_device *dev, u8 *newval)
2785{
2786 struct netdev_private *np = netdev_priv(dev);
2787 void __iomem * ioaddr = ns_ioaddr(dev);
2788 u16 *sval = (u16 *)newval;
2789 u32 addr;
2790
2791 if (np->srr < SRR_DP83815_D) {
2792 return 0;
2793 }
2794
2795 /* enable writing to these registers by disabling the RX filter */
2796 addr = readl(ioaddr + RxFilterAddr) & ~RFCRAddressMask;
2797 addr &= ~RxFilterEnable;
2798 writel(addr, ioaddr + RxFilterAddr);
2799
2800 /* write the three words to (undocumented) RFCR vals 0xa, 0xc, 0xe */
2801 writel(addr | 0xa, ioaddr + RxFilterAddr);
2802 writew(sval[0], ioaddr + RxFilterData);
2803
2804 writel(addr | 0xc, ioaddr + RxFilterAddr);
2805 writew(sval[1], ioaddr + RxFilterData);
2806
2807 writel(addr | 0xe, ioaddr + RxFilterAddr);
2808 writew(sval[2], ioaddr + RxFilterData);
2809
2810 /* re-enable the RX filter */
2811 writel(addr | RxFilterEnable, ioaddr + RxFilterAddr);
2812
2813 return 0;
2814}
2815
2816static int netdev_get_sopass(struct net_device *dev, u8 *data)
2817{
2818 struct netdev_private *np = netdev_priv(dev);
2819 void __iomem * ioaddr = ns_ioaddr(dev);
2820 u16 *sval = (u16 *)data;
2821 u32 addr;
2822
2823 if (np->srr < SRR_DP83815_D) {
2824 sval[0] = sval[1] = sval[2] = 0;
2825 return 0;
2826 }
2827
2828 /* read the three words from (undocumented) RFCR vals 0xa, 0xc, 0xe */
2829 addr = readl(ioaddr + RxFilterAddr) & ~RFCRAddressMask;
2830
2831 writel(addr | 0xa, ioaddr + RxFilterAddr);
2832 sval[0] = readw(ioaddr + RxFilterData);
2833
2834 writel(addr | 0xc, ioaddr + RxFilterAddr);
2835 sval[1] = readw(ioaddr + RxFilterData);
2836
2837 writel(addr | 0xe, ioaddr + RxFilterAddr);
2838 sval[2] = readw(ioaddr + RxFilterData);
2839
2840 writel(addr, ioaddr + RxFilterAddr);
2841
2842 return 0;
2843}
2844
2845static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2846{
2847 struct netdev_private *np = netdev_priv(dev);
2848 u32 tmp;
2849
2850 ecmd->port = dev->if_port;
2851 ecmd->speed = np->speed;
2852 ecmd->duplex = np->duplex;
2853 ecmd->autoneg = np->autoneg;
2854 ecmd->advertising = 0;
2855 if (np->advertising & ADVERTISE_10HALF)
2856 ecmd->advertising |= ADVERTISED_10baseT_Half;
2857 if (np->advertising & ADVERTISE_10FULL)
2858 ecmd->advertising |= ADVERTISED_10baseT_Full;
2859 if (np->advertising & ADVERTISE_100HALF)
2860 ecmd->advertising |= ADVERTISED_100baseT_Half;
2861 if (np->advertising & ADVERTISE_100FULL)
2862 ecmd->advertising |= ADVERTISED_100baseT_Full;
2863 ecmd->supported = (SUPPORTED_Autoneg |
2864 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2865 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2866 SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE);
2867 ecmd->phy_address = np->phy_addr_external;
2868 /*
2869 * We intentionally report the phy address of the external
2870 * phy, even if the internal phy is used. This is necessary
2871 * to work around a deficiency of the ethtool interface:
2872 * It's only possible to query the settings of the active
2873 * port. Therefore
2874 * # ethtool -s ethX port mii
2875 * actually sends an ioctl to switch to port mii with the
2876 * settings that are used for the current active port.
2877 * If we would report a different phy address in this
2878 * command, then
2879 * # ethtool -s ethX port tp;ethtool -s ethX port mii
2880 * would unintentionally change the phy address.
2881 *
2882 * Fortunately the phy address doesn't matter with the
2883 * internal phy...
2884 */
2885
2886 /* set information based on active port type */
2887 switch (ecmd->port) {
2888 default:
2889 case PORT_TP:
2890 ecmd->advertising |= ADVERTISED_TP;
2891 ecmd->transceiver = XCVR_INTERNAL;
2892 break;
2893 case PORT_MII:
2894 ecmd->advertising |= ADVERTISED_MII;
2895 ecmd->transceiver = XCVR_EXTERNAL;
2896 break;
2897 case PORT_FIBRE:
2898 ecmd->advertising |= ADVERTISED_FIBRE;
2899 ecmd->transceiver = XCVR_EXTERNAL;
2900 break;
2901 }
2902
2903 /* if autonegotiation is on, try to return the active speed/duplex */
2904 if (ecmd->autoneg == AUTONEG_ENABLE) {
2905 ecmd->advertising |= ADVERTISED_Autoneg;
2906 tmp = mii_nway_result(
2907 np->advertising & mdio_read(dev, MII_LPA));
2908 if (tmp == LPA_100FULL || tmp == LPA_100HALF)
2909 ecmd->speed = SPEED_100;
2910 else
2911 ecmd->speed = SPEED_10;
2912 if (tmp == LPA_100FULL || tmp == LPA_10FULL)
2913 ecmd->duplex = DUPLEX_FULL;
2914 else
2915 ecmd->duplex = DUPLEX_HALF;
2916 }
2917
2918 /* ignore maxtxpkt, maxrxpkt for now */
2919
2920 return 0;
2921}
2922
2923static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2924{
2925 struct netdev_private *np = netdev_priv(dev);
2926
2927 if (ecmd->port != PORT_TP && ecmd->port != PORT_MII && ecmd->port != PORT_FIBRE)
2928 return -EINVAL;
2929 if (ecmd->transceiver != XCVR_INTERNAL && ecmd->transceiver != XCVR_EXTERNAL)
2930 return -EINVAL;
2931 if (ecmd->autoneg == AUTONEG_ENABLE) {
2932 if ((ecmd->advertising & (ADVERTISED_10baseT_Half |
2933 ADVERTISED_10baseT_Full |
2934 ADVERTISED_100baseT_Half |
2935 ADVERTISED_100baseT_Full)) == 0) {
2936 return -EINVAL;
2937 }
2938 } else if (ecmd->autoneg == AUTONEG_DISABLE) {
2939 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
2940 return -EINVAL;
2941 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
2942 return -EINVAL;
2943 } else {
2944 return -EINVAL;
2945 }
2946
2947 /*
2948 * maxtxpkt, maxrxpkt: ignored for now.
2949 *
2950 * transceiver:
2951 * PORT_TP is always XCVR_INTERNAL, PORT_MII and PORT_FIBRE are always
2952 * XCVR_EXTERNAL. The implementation thus ignores ecmd->transceiver and
2953 * selects based on ecmd->port.
2954 *
2955 * Actually PORT_FIBRE is nearly identical to PORT_MII: it's for fibre
2956 * phys that are connected to the mii bus. It's used to apply fibre
2957 * specific updates.
2958 */
2959
2960 /* WHEW! now lets bang some bits */
2961
2962 /* save the parms */
2963 dev->if_port = ecmd->port;
2964 np->autoneg = ecmd->autoneg;
2965 np->phy_addr_external = ecmd->phy_address & PhyAddrMask;
2966 if (np->autoneg == AUTONEG_ENABLE) {
2967 /* advertise only what has been requested */
2968 np->advertising &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
2969 if (ecmd->advertising & ADVERTISED_10baseT_Half)
2970 np->advertising |= ADVERTISE_10HALF;
2971 if (ecmd->advertising & ADVERTISED_10baseT_Full)
2972 np->advertising |= ADVERTISE_10FULL;
2973 if (ecmd->advertising & ADVERTISED_100baseT_Half)
2974 np->advertising |= ADVERTISE_100HALF;
2975 if (ecmd->advertising & ADVERTISED_100baseT_Full)
2976 np->advertising |= ADVERTISE_100FULL;
2977 } else {
2978 np->speed = ecmd->speed;
2979 np->duplex = ecmd->duplex;
2980 /* user overriding the initial full duplex parm? */
2981 if (np->duplex == DUPLEX_HALF)
2982 np->full_duplex = 0;
2983 }
2984
2985 /* get the right phy enabled */
2986 if (ecmd->port == PORT_TP)
2987 switch_port_internal(dev);
2988 else
2989 switch_port_external(dev);
2990
2991 /* set parms and see how this affected our link status */
2992 init_phy_fixup(dev);
2993 check_link(dev);
2994 return 0;
2995}
2996
2997static int netdev_get_regs(struct net_device *dev, u8 *buf)
2998{
2999 int i;
3000 int j;
3001 u32 rfcr;
3002 u32 *rbuf = (u32 *)buf;
3003 void __iomem * ioaddr = ns_ioaddr(dev);
3004
3005 /* read non-mii page 0 of registers */
3006 for (i = 0; i < NATSEMI_PG0_NREGS/2; i++) {
3007 rbuf[i] = readl(ioaddr + i*4);
3008 }
3009
3010 /* read current mii registers */
3011 for (i = NATSEMI_PG0_NREGS/2; i < NATSEMI_PG0_NREGS; i++)
3012 rbuf[i] = mdio_read(dev, i & 0x1f);
3013
3014 /* read only the 'magic' registers from page 1 */
3015 writew(1, ioaddr + PGSEL);
3016 rbuf[i++] = readw(ioaddr + PMDCSR);
3017 rbuf[i++] = readw(ioaddr + TSTDAT);
3018 rbuf[i++] = readw(ioaddr + DSPCFG);
3019 rbuf[i++] = readw(ioaddr + SDCFG);
3020 writew(0, ioaddr + PGSEL);
3021
3022 /* read RFCR indexed registers */
3023 rfcr = readl(ioaddr + RxFilterAddr);
3024 for (j = 0; j < NATSEMI_RFDR_NREGS; j++) {
3025 writel(j*2, ioaddr + RxFilterAddr);
3026 rbuf[i++] = readw(ioaddr + RxFilterData);
3027 }
3028 writel(rfcr, ioaddr + RxFilterAddr);
3029
3030 /* the interrupt status is clear-on-read - see if we missed any */
3031 if (rbuf[4] & rbuf[5]) {
3032 printk(KERN_WARNING
3033 "%s: shoot, we dropped an interrupt (%#08x)\n",
3034 dev->name, rbuf[4] & rbuf[5]);
3035 }
3036
3037 return 0;
3038}
3039
3040#define SWAP_BITS(x) ( (((x) & 0x0001) << 15) | (((x) & 0x0002) << 13) \
3041 | (((x) & 0x0004) << 11) | (((x) & 0x0008) << 9) \
3042 | (((x) & 0x0010) << 7) | (((x) & 0x0020) << 5) \
3043 | (((x) & 0x0040) << 3) | (((x) & 0x0080) << 1) \
3044 | (((x) & 0x0100) >> 1) | (((x) & 0x0200) >> 3) \
3045 | (((x) & 0x0400) >> 5) | (((x) & 0x0800) >> 7) \
3046 | (((x) & 0x1000) >> 9) | (((x) & 0x2000) >> 11) \
3047 | (((x) & 0x4000) >> 13) | (((x) & 0x8000) >> 15) )
3048
3049static int netdev_get_eeprom(struct net_device *dev, u8 *buf)
3050{
3051 int i;
3052 u16 *ebuf = (u16 *)buf;
3053 void __iomem * ioaddr = ns_ioaddr(dev);
a8b4cf42 3054 struct netdev_private *np = netdev_priv(dev);
1da177e4
LT
3055
3056 /* eeprom_read reads 16 bits, and indexes by 16 bits */
a8b4cf42 3057 for (i = 0; i < np->eeprom_size/2; i++) {
1da177e4
LT
3058 ebuf[i] = eeprom_read(ioaddr, i);
3059 /* The EEPROM itself stores data bit-swapped, but eeprom_read
3060 * reads it back "sanely". So we swap it back here in order to
3061 * present it to userland as it is stored. */
3062 ebuf[i] = SWAP_BITS(ebuf[i]);
3063 }
3064 return 0;
3065}
3066
3067static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3068{
3069 struct mii_ioctl_data *data = if_mii(rq);
3070 struct netdev_private *np = netdev_priv(dev);
3071
3072 switch(cmd) {
3073 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
3074 case SIOCDEVPRIVATE: /* for binary compat, remove in 2.5 */
3075 data->phy_id = np->phy_addr_external;
3076 /* Fall Through */
3077
3078 case SIOCGMIIREG: /* Read MII PHY register. */
3079 case SIOCDEVPRIVATE+1: /* for binary compat, remove in 2.5 */
3080 /* The phy_id is not enough to uniquely identify
3081 * the intended target. Therefore the command is sent to
3082 * the given mii on the current port.
3083 */
3084 if (dev->if_port == PORT_TP) {
3085 if ((data->phy_id & 0x1f) == np->phy_addr_external)
3086 data->val_out = mdio_read(dev,
3087 data->reg_num & 0x1f);
3088 else
3089 data->val_out = 0;
3090 } else {
3091 move_int_phy(dev, data->phy_id & 0x1f);
3092 data->val_out = miiport_read(dev, data->phy_id & 0x1f,
3093 data->reg_num & 0x1f);
3094 }
3095 return 0;
3096
3097 case SIOCSMIIREG: /* Write MII PHY register. */
3098 case SIOCDEVPRIVATE+2: /* for binary compat, remove in 2.5 */
3099 if (!capable(CAP_NET_ADMIN))
3100 return -EPERM;
3101 if (dev->if_port == PORT_TP) {
3102 if ((data->phy_id & 0x1f) == np->phy_addr_external) {
3103 if ((data->reg_num & 0x1f) == MII_ADVERTISE)
3104 np->advertising = data->val_in;
3105 mdio_write(dev, data->reg_num & 0x1f,
3106 data->val_in);
3107 }
3108 } else {
3109 if ((data->phy_id & 0x1f) == np->phy_addr_external) {
3110 if ((data->reg_num & 0x1f) == MII_ADVERTISE)
3111 np->advertising = data->val_in;
3112 }
3113 move_int_phy(dev, data->phy_id & 0x1f);
3114 miiport_write(dev, data->phy_id & 0x1f,
3115 data->reg_num & 0x1f,
3116 data->val_in);
3117 }
3118 return 0;
3119 default:
3120 return -EOPNOTSUPP;
3121 }
3122}
3123
3124static void enable_wol_mode(struct net_device *dev, int enable_intr)
3125{
3126 void __iomem * ioaddr = ns_ioaddr(dev);
3127 struct netdev_private *np = netdev_priv(dev);
3128
3129 if (netif_msg_wol(np))
3130 printk(KERN_INFO "%s: remaining active for wake-on-lan\n",
3131 dev->name);
3132
3133 /* For WOL we must restart the rx process in silent mode.
3134 * Write NULL to the RxRingPtr. Only possible if
3135 * rx process is stopped
3136 */
3137 writel(0, ioaddr + RxRingPtr);
3138
3139 /* read WoL status to clear */
3140 readl(ioaddr + WOLCmd);
3141
3142 /* PME on, clear status */
3143 writel(np->SavedClkRun | PMEEnable | PMEStatus, ioaddr + ClkRun);
3144
3145 /* and restart the rx process */
3146 writel(RxOn, ioaddr + ChipCmd);
3147
3148 if (enable_intr) {
3149 /* enable the WOL interrupt.
3150 * Could be used to send a netlink message.
3151 */
3152 writel(WOLPkt | LinkChange, ioaddr + IntrMask);
3153 writel(1, ioaddr + IntrEnable);
3154 }
3155}
3156
3157static int netdev_close(struct net_device *dev)
3158{
3159 void __iomem * ioaddr = ns_ioaddr(dev);
3160 struct netdev_private *np = netdev_priv(dev);
3161
3162 if (netif_msg_ifdown(np))
3163 printk(KERN_DEBUG
3164 "%s: Shutting down ethercard, status was %#04x.\n",
3165 dev->name, (int)readl(ioaddr + ChipCmd));
3166 if (netif_msg_pktdata(np))
3167 printk(KERN_DEBUG
3168 "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
3169 dev->name, np->cur_tx, np->dirty_tx,
3170 np->cur_rx, np->dirty_rx);
3171
3172 /*
3173 * FIXME: what if someone tries to close a device
3174 * that is suspended?
3175 * Should we reenable the nic to switch to
3176 * the final WOL settings?
3177 */
3178
3179 del_timer_sync(&np->timer);
3180 disable_irq(dev->irq);
3181 spin_lock_irq(&np->lock);
b27a16b7 3182 natsemi_irq_disable(dev);
1da177e4
LT
3183 np->hands_off = 1;
3184 spin_unlock_irq(&np->lock);
3185 enable_irq(dev->irq);
3186
3187 free_irq(dev->irq, dev);
3188
3189 /* Interrupt disabled, interrupt handler released,
3190 * queue stopped, timer deleted, rtnl_lock held
3191 * All async codepaths that access the driver are disabled.
3192 */
3193 spin_lock_irq(&np->lock);
3194 np->hands_off = 0;
3195 readl(ioaddr + IntrMask);
3196 readw(ioaddr + MIntrStatus);
3197
3198 /* Freeze Stats */
3199 writel(StatsFreeze, ioaddr + StatsCtrl);
3200
3201 /* Stop the chip's Tx and Rx processes. */
3202 natsemi_stop_rxtx(dev);
3203
3204 __get_stats(dev);
3205 spin_unlock_irq(&np->lock);
3206
3207 /* clear the carrier last - an interrupt could reenable it otherwise */
3208 netif_carrier_off(dev);
3209 netif_stop_queue(dev);
3210
3211 dump_ring(dev);
3212 drain_ring(dev);
3213 free_ring(dev);
3214
3215 {
3216 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
3217 if (wol) {
3218 /* restart the NIC in WOL mode.
3219 * The nic must be stopped for this.
3220 */
3221 enable_wol_mode(dev, 0);
3222 } else {
3223 /* Restore PME enable bit unmolested */
3224 writel(np->SavedClkRun, ioaddr + ClkRun);
3225 }
3226 }
3227 return 0;
3228}
3229
3230
3231static void __devexit natsemi_remove1 (struct pci_dev *pdev)
3232{
3233 struct net_device *dev = pci_get_drvdata(pdev);
3234 void __iomem * ioaddr = ns_ioaddr(dev);
3235
3236 unregister_netdev (dev);
3237 pci_release_regions (pdev);
3238 iounmap(ioaddr);
3239 free_netdev (dev);
3240 pci_set_drvdata(pdev, NULL);
3241}
3242
3243#ifdef CONFIG_PM
3244
3245/*
3246 * The ns83815 chip doesn't have explicit RxStop bits.
3247 * Kicking the Rx or Tx process for a new packet reenables the Rx process
3248 * of the nic, thus this function must be very careful:
3249 *
3250 * suspend/resume synchronization:
3251 * entry points:
3252 * netdev_open, netdev_close, netdev_ioctl, set_rx_mode, intr_handler,
3253 * start_tx, tx_timeout
3254 *
3255 * No function accesses the hardware without checking np->hands_off.
3256 * the check occurs under spin_lock_irq(&np->lock);
3257 * exceptions:
3258 * * netdev_ioctl: noncritical access.
3259 * * netdev_open: cannot happen due to the device_detach
3260 * * netdev_close: doesn't hurt.
3261 * * netdev_timer: timer stopped by natsemi_suspend.
3262 * * intr_handler: doesn't acquire the spinlock. suspend calls
3263 * disable_irq() to enforce synchronization.
b27a16b7
MB
3264 * * natsemi_poll: checks before reenabling interrupts. suspend
3265 * sets hands_off, disables interrupts and then waits with
3266 * netif_poll_disable().
1da177e4
LT
3267 *
3268 * Interrupts must be disabled, otherwise hands_off can cause irq storms.
3269 */
3270
3271static int natsemi_suspend (struct pci_dev *pdev, pm_message_t state)
3272{
3273 struct net_device *dev = pci_get_drvdata (pdev);
3274 struct netdev_private *np = netdev_priv(dev);
3275 void __iomem * ioaddr = ns_ioaddr(dev);
3276
3277 rtnl_lock();
3278 if (netif_running (dev)) {
3279 del_timer_sync(&np->timer);
3280
3281 disable_irq(dev->irq);
3282 spin_lock_irq(&np->lock);
3283
3284 writel(0, ioaddr + IntrEnable);
3285 np->hands_off = 1;
3286 natsemi_stop_rxtx(dev);
3287 netif_stop_queue(dev);
3288
3289 spin_unlock_irq(&np->lock);
3290 enable_irq(dev->irq);
3291
b27a16b7
MB
3292 netif_poll_disable(dev);
3293
1da177e4
LT
3294 /* Update the error counts. */
3295 __get_stats(dev);
3296
3297 /* pci_power_off(pdev, -1); */
3298 drain_ring(dev);
3299 {
3300 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
3301 /* Restore PME enable bit */
3302 if (wol) {
3303 /* restart the NIC in WOL mode.
3304 * The nic must be stopped for this.
3305 * FIXME: use the WOL interrupt
3306 */
3307 enable_wol_mode(dev, 0);
3308 } else {
3309 /* Restore PME enable bit unmolested */
3310 writel(np->SavedClkRun, ioaddr + ClkRun);
3311 }
3312 }
3313 }
3314 netif_device_detach(dev);
3315 rtnl_unlock();
3316 return 0;
3317}
3318
3319
3320static int natsemi_resume (struct pci_dev *pdev)
3321{
3322 struct net_device *dev = pci_get_drvdata (pdev);
3323 struct netdev_private *np = netdev_priv(dev);
3324
3325 rtnl_lock();
3326 if (netif_device_present(dev))
3327 goto out;
3328 if (netif_running(dev)) {
3329 BUG_ON(!np->hands_off);
3330 pci_enable_device(pdev);
3331 /* pci_power_on(pdev); */
3332
3333 natsemi_reset(dev);
3334 init_ring(dev);
3335 disable_irq(dev->irq);
3336 spin_lock_irq(&np->lock);
3337 np->hands_off = 0;
3338 init_registers(dev);
3339 netif_device_attach(dev);
3340 spin_unlock_irq(&np->lock);
3341 enable_irq(dev->irq);
3342
3343 mod_timer(&np->timer, jiffies + 1*HZ);
3344 }
3345 netif_device_attach(dev);
b27a16b7 3346 netif_poll_enable(dev);
1da177e4
LT
3347out:
3348 rtnl_unlock();
3349 return 0;
3350}
3351
3352#endif /* CONFIG_PM */
3353
3354static struct pci_driver natsemi_driver = {
3355 .name = DRV_NAME,
3356 .id_table = natsemi_pci_tbl,
3357 .probe = natsemi_probe1,
3358 .remove = __devexit_p(natsemi_remove1),
3359#ifdef CONFIG_PM
3360 .suspend = natsemi_suspend,
3361 .resume = natsemi_resume,
3362#endif
3363};
3364
3365static int __init natsemi_init_mod (void)
3366{
3367/* when a module, this is printed whether or not devices are found in probe */
3368#ifdef MODULE
3369 printk(version);
3370#endif
3371
3372 return pci_module_init (&natsemi_driver);
3373}
3374
3375static void __exit natsemi_exit_mod (void)
3376{
3377 pci_unregister_driver (&natsemi_driver);
3378}
3379
3380module_init(natsemi_init_mod);
3381module_exit(natsemi_exit_mod);
3382
This page took 0.280476 seconds and 5 git commands to generate.