1 /* natsemi.c: A Linux PCI Ethernet driver for the NatSemi DP8381x series. */
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)
6 Portions copyright 2004 Harald Welte <laforge@gnumonks.org>
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.
16 The original author may be reached as becker@scyld.com, or at
17 Scyld Computing Corporation
18 410 Severn Ave., Suite 210
21 Support information and updates available at
22 http://www.scyld.com/network/netsemi.html
25 Linux kernel modifications:
29 - Bug fixes and better intr performance (Tjeerd)
31 - Now reads correct MAC address from eeprom
33 - Eliminate redundant priv->tx_full flag
34 - Call netif_start_queue from dev->tx_timeout
35 - wmb() in start_tx() to flush data
37 - Clean up PCI enable (davej)
39 - Merge Donald Becker's natsemi.c version 1.07
43 * ethtool support (jgarzik)
44 * Proper initialization of the card (which sometimes
45 fails to occur and leaves the card in a non-functional
48 * Some documented register settings to optimize some
49 of the 100Mbit autodetection circuitry in rev C cards. (uzi)
51 * Polling of the PHY intr for stuff like link state
52 change and auto- negotiation to finally work properly. (uzi)
54 * One-liner removal of a duplicate declaration of
57 Version 1.0.7: (Manfred Spraul)
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]
66 version 1.0.8 (Tim Hockin <thockin@sun.com>)
68 * Wake on lan support (Erik Gilling)
69 * MXDMA fixes for serverworks
72 version 1.0.9 (Manfred Spraul)
73 * Main change: fix lack of synchronize
74 netif_close/netif_suspend against a last interrupt
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
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).
88 * use long for ee_addr (various)
89 * print pointers properly (DaveM)
90 * include asm/irq.h (?)
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)
102 * ETHTOOL_* further support (Tim Hockin)
105 * ETHTOOL_[G]EEPROM support (Tim Hockin)
108 * crc cleanup (Matt Domsch <Matt_Domsch@dell.com>)
111 * Cleanup some messages and autoneg in ethtool (Tim Hockin)
114 * Get rid of cable_magic flag
115 * use new (National provided) solution for cable magic issue
118 * call netdev_rx() for RxErrors (Manfred Spraul)
119 * formatting and cleanups
120 * change options and full_duplex arrays to be zero
122 * enable only the WoL and PHY interrupts in wol mode
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
131 * be sure to write the MAC back to the chip (Manfred Spraul)
132 * lengthen EEPROM timeout, and always warn about timeouts
134 * comments update (Manfred)
135 * do the right thing on a phy-reset (Manfred and Tim)
138 * big endian support with CFG:BEM instead of cpu_to_le32
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>
162 #include <linux/prefetch.h>
163 #include <asm/processor.h> /* Processor type for cache alignment. */
166 #include <asm/uaccess.h>
168 #define DRV_NAME "natsemi"
169 #define DRV_VERSION "1.07+LK1.0.17"
170 #define DRV_RELDATE "Sep 27, 2002"
174 /* Updated to recommendations in pci-skeleton v2.03. */
176 /* The user-configurable values.
177 These may be modified when a driver module is loaded.*/
179 #define NATSEMI_DEF_MSG (NETIF_MSG_DRV | \
184 static int debug = -1;
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. */
190 static const int multicast_filter_limit = 100;
192 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
193 Setting to > 1518 effectively disables this feature. */
194 static int rx_copybreak;
196 /* Used to pass the media type, etc.
197 Both 'options[]' and 'full_duplex[]' should exist for driver
199 The media type is usually passed in 'options[]'.
201 #define MAX_UNITS 8 /* More are supported, limit only on options */
202 static int options[MAX_UNITS];
203 static int full_duplex[MAX_UNITS];
205 /* Operational parameters that are set at compile time. */
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
216 /* Operational parameters that usually are not changed. */
217 /* Time in jiffies before concluding the transmitter is hung. */
218 #define TX_TIMEOUT (2*HZ)
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 + \
227 #define NATSEMI_REGS_VER 1 /* v1 added RFDR registers */
228 #define NATSEMI_REGS_SIZE (NATSEMI_NREGS * sizeof(u32))
231 * The nic writes 32-bit values, even if the upper bytes of
232 * a 32-bit value are beyond the end of the buffer.
234 #define NATSEMI_HEADERS 22 /* 2*mac,type,vlan,crc */
235 #define NATSEMI_PADDING 16 /* 2 bytes should be sufficient */
236 #define NATSEMI_LONGPKT 1518 /* limit for normal packets */
237 #define NATSEMI_RX_LIMIT 2046 /* maximum supported by hardware */
239 /* These identify the driver base version and may not be removed. */
240 static const char version[] __devinitdata =
241 KERN_INFO DRV_NAME " dp8381x driver, version "
242 DRV_VERSION ", " DRV_RELDATE "\n"
243 KERN_INFO " originally by Donald Becker <becker@scyld.com>\n"
244 KERN_INFO " http://www.scyld.com/network/natsemi.html\n"
245 KERN_INFO " 2.4.x kernel port by Jeff Garzik, Tjeerd Mulder\n";
247 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
248 MODULE_DESCRIPTION("National Semiconductor DP8381x series PCI Ethernet driver");
249 MODULE_LICENSE("GPL");
251 module_param(mtu, int, 0);
252 module_param(debug, int, 0);
253 module_param(rx_copybreak, int, 0);
254 module_param_array(options, int, NULL, 0);
255 module_param_array(full_duplex, int, NULL, 0);
256 MODULE_PARM_DESC(mtu, "DP8381x MTU (all boards)");
257 MODULE_PARM_DESC(debug, "DP8381x default debug level");
258 MODULE_PARM_DESC(rx_copybreak,
259 "DP8381x copy breakpoint for copy-only-tiny-frames");
260 MODULE_PARM_DESC(options,
261 "DP8381x: Bits 0-3: media type, bit 17: full duplex");
262 MODULE_PARM_DESC(full_duplex, "DP8381x full duplex setting(s) (1)");
267 I. Board Compatibility
269 This driver is designed for National Semiconductor DP83815 PCI Ethernet NIC.
270 It also works with other chips in in the DP83810 series.
272 II. Board-specific settings
274 This driver requires the PCI interrupt line to be valid.
275 It honors the EEPROM-set values.
277 III. Driver operation
281 This driver uses two statically allocated fixed-size descriptor lists
282 formed into rings by a branch from the final descriptor to the beginning of
283 the list. The ring sizes are set at compile time by RX/TX_RING_SIZE.
284 The NatSemi design uses a 'next descriptor' pointer that the driver forms
287 IIIb/c. Transmit/Receive Structure
289 This driver uses a zero-copy receive and transmit scheme.
290 The driver allocates full frame size skbuffs for the Rx ring buffers at
291 open() time and passes the skb->data field to the chip as receive data
292 buffers. When an incoming frame is less than RX_COPYBREAK bytes long,
293 a fresh skbuff is allocated and the frame is copied to the new skbuff.
294 When the incoming frame is larger, the skbuff is passed directly up the
295 protocol stack. Buffers consumed this way are replaced by newly allocated
296 skbuffs in a later phase of receives.
298 The RX_COPYBREAK value is chosen to trade-off the memory wasted by
299 using a full-sized skbuff for small frames vs. the copying costs of larger
300 frames. New boards are typically used in generously configured machines
301 and the underfilled buffers have negligible impact compared to the benefit of
302 a single allocation size, so the default value of zero results in never
303 copying packets. When copying is done, the cost is usually mitigated by using
304 a combined copy/checksum routine. Copying also preloads the cache, which is
305 most useful with small frames.
307 A subtle aspect of the operation is that unaligned buffers are not permitted
308 by the hardware. Thus the IP header at offset 14 in an ethernet frame isn't
309 longword aligned for further processing. On copies frames are put into the
310 skbuff at an offset of "+2", 16-byte aligning the IP header.
312 IIId. Synchronization
314 Most operations are synchronized on the np->lock irq spinlock, except the
315 performance critical codepaths:
317 The rx process only runs in the interrupt handler. Access from outside
318 the interrupt handler is only permitted after disable_irq().
320 The rx process usually runs under the netif_tx_lock. If np->intr_tx_reap
321 is set, then access is permitted under spin_lock_irq(&np->lock).
323 Thus configuration functions that want to access everything must call
324 disable_irq(dev->irq);
325 netif_tx_lock_bh(dev);
326 spin_lock_irq(&np->lock);
330 NatSemi PCI network controllers are very uncommon.
334 http://www.scyld.com/expert/100mbps.html
335 http://www.scyld.com/expert/NWay.html
336 Datasheet is available from:
337 http://www.national.com/pf/DP/DP83815.html
347 * Support for fibre connections on Am79C874:
348 * This phy needs a special setup when connected to a fibre cable.
349 * http://www.amd.com/files/connectivitysolutions/networking/archivednetworking/22235.pdf
351 #define PHYID_AM79C874 0x0022561b
354 MII_MCTRL = 0x15, /* mode control register */
355 MII_FX_SEL = 0x0001, /* 100BASE-FX (fiber) */
356 MII_EN_SCRM = 0x0004, /* enable scrambler (tp) */
360 /* array of board data directly indexed by pci_tbl[x].driver_data */
361 static const struct {
364 unsigned int eeprom_size;
365 } natsemi_pci_info[] __devinitdata = {
366 { "NatSemi DP8381[56]", 0, 24 },
369 static const struct pci_device_id natsemi_pci_tbl[] __devinitdata = {
370 { PCI_VENDOR_ID_NS, 0x0020, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
371 { } /* terminate list */
373 MODULE_DEVICE_TABLE(pci, natsemi_pci_tbl);
375 /* Offsets to the device registers.
376 Unlike software-only systems, device drivers interact with complex hardware.
377 It's not useful to define symbolic names for every register bit in the
380 enum register_offsets {
388 IntrHoldoff = 0x1C, /* DP83816 only */
415 /* These are from the spec, around page 78... on a separate table.
416 * The meaning of these registers depend on the value of PGSEL. */
423 /* the values for the 'magic' registers above (PGSEL=1) */
424 #define PMDCSR_VAL 0x189c /* enable preferred adaptation circuitry */
425 #define TSTDAT_VAL 0x0
426 #define DSPCFG_VAL 0x5040
427 #define SDCFG_VAL 0x008c /* set voltage thresholds for Signal Detect */
428 #define DSPCFG_LOCK 0x20 /* coefficient lock bit in DSPCFG */
429 #define DSPCFG_COEF 0x1000 /* see coefficient (in TSTDAT) bit in DSPCFG */
430 #define TSTDAT_FIXED 0xe8 /* magic number for bad coefficients */
432 /* misc PCI space registers */
433 enum pci_register_offsets {
447 enum ChipConfig_bits {
451 CfgAnegEnable = 0x2000,
453 CfgAnegFull = 0x8000,
454 CfgAnegDone = 0x8000000,
455 CfgFullDuplex = 0x20000000,
456 CfgSpeed100 = 0x40000000,
457 CfgLink = 0x80000000,
463 EE_ChipSelect = 0x08,
470 enum PCIBusCfg_bits {
474 /* Bits in the interrupt status/mask registers. */
475 enum IntrStatus_bits {
479 IntrRxEarly = 0x0008,
481 IntrRxOverrun = 0x0020,
486 IntrTxUnderrun = 0x0400,
491 IntrHighBits = 0x8000,
492 RxStatusFIFOOver = 0x10000,
493 IntrPCIErr = 0xf00000,
494 RxResetDone = 0x1000000,
495 TxResetDone = 0x2000000,
496 IntrAbnormalSummary = 0xCD20,
500 * Default Interrupts:
501 * Rx OK, Rx Packet Error, Rx Overrun,
502 * Tx OK, Tx Packet Error, Tx Underrun,
503 * MIB Service, Phy Interrupt, High Bits,
504 * Rx Status FIFO overrun,
505 * Received Target Abort, Received Master Abort,
506 * Signalled System Error, Received Parity Error
508 #define DEFAULT_INTR 0x00f1cd65
513 TxMxdmaMask = 0x700000,
515 TxMxdma_4 = 0x100000,
516 TxMxdma_8 = 0x200000,
517 TxMxdma_16 = 0x300000,
518 TxMxdma_32 = 0x400000,
519 TxMxdma_64 = 0x500000,
520 TxMxdma_128 = 0x600000,
521 TxMxdma_256 = 0x700000,
522 TxCollRetry = 0x800000,
523 TxAutoPad = 0x10000000,
524 TxMacLoop = 0x20000000,
525 TxHeartIgn = 0x40000000,
526 TxCarrierIgn = 0x80000000
531 * - 256 byte DMA burst length
532 * - fill threshold 512 bytes (i.e. restart DMA when 512 bytes are free)
533 * - 64 bytes initial drain threshold (i.e. begin actual transmission
534 * when 64 byte are in the fifo)
535 * - on tx underruns, increase drain threshold by 64.
536 * - at most use a drain threshold of 1472 bytes: The sum of the fill
537 * threshold and the drain threshold must be less than 2016 bytes.
540 #define TX_FLTH_VAL ((512/32) << 8)
541 #define TX_DRTH_VAL_START (64/32)
542 #define TX_DRTH_VAL_INC 2
543 #define TX_DRTH_VAL_LIMIT (1472/32)
547 RxMxdmaMask = 0x700000,
549 RxMxdma_4 = 0x100000,
550 RxMxdma_8 = 0x200000,
551 RxMxdma_16 = 0x300000,
552 RxMxdma_32 = 0x400000,
553 RxMxdma_64 = 0x500000,
554 RxMxdma_128 = 0x600000,
555 RxMxdma_256 = 0x700000,
556 RxAcceptLong = 0x8000000,
557 RxAcceptTx = 0x10000000,
558 RxAcceptRunt = 0x40000000,
559 RxAcceptErr = 0x80000000
561 #define RX_DRTH_VAL (128/8)
579 WakeMagicSecure = 0x400,
580 SecureHack = 0x100000,
582 WokeUnicast = 0x800000,
583 WokeMulticast = 0x1000000,
584 WokeBroadcast = 0x2000000,
586 WokePMatch0 = 0x8000000,
587 WokePMatch1 = 0x10000000,
588 WokePMatch2 = 0x20000000,
589 WokePMatch3 = 0x40000000,
590 WokeMagic = 0x80000000,
591 WakeOptsSummary = 0x7ff
594 enum RxFilterAddr_bits {
595 RFCRAddressMask = 0x3ff,
596 AcceptMulticast = 0x00200000,
597 AcceptMyPhys = 0x08000000,
598 AcceptAllPhys = 0x10000000,
599 AcceptAllMulticast = 0x20000000,
600 AcceptBroadcast = 0x40000000,
601 RxFilterEnable = 0x80000000
604 enum StatsCtrl_bits {
611 enum MIntrCtrl_bits {
619 #define PHY_ADDR_NONE 32
620 #define PHY_ADDR_INTERNAL 1
622 /* values we might find in the silicon revision register */
623 #define SRR_DP83815_C 0x0302
624 #define SRR_DP83815_D 0x0403
625 #define SRR_DP83816_A4 0x0504
626 #define SRR_DP83816_A5 0x0505
628 /* The Rx and Tx buffer descriptors. */
629 /* Note that using only 32 bit fields simplifies conversion to big-endian
638 /* Bits in network_desc.status */
639 enum desc_status_bits {
640 DescOwn=0x80000000, DescMore=0x40000000, DescIntr=0x20000000,
641 DescNoCRC=0x10000000, DescPktOK=0x08000000,
644 DescTxAbort=0x04000000, DescTxFIFO=0x02000000,
645 DescTxCarrier=0x01000000, DescTxDefer=0x00800000,
646 DescTxExcDefer=0x00400000, DescTxOOWCol=0x00200000,
647 DescTxExcColl=0x00100000, DescTxCollCount=0x000f0000,
649 DescRxAbort=0x04000000, DescRxOver=0x02000000,
650 DescRxDest=0x01800000, DescRxLong=0x00400000,
651 DescRxRunt=0x00200000, DescRxInvalid=0x00100000,
652 DescRxCRC=0x00080000, DescRxAlign=0x00040000,
653 DescRxLoop=0x00020000, DesRxColl=0x00010000,
656 struct netdev_private {
657 /* Descriptor rings first for alignment */
659 struct netdev_desc *rx_ring;
660 struct netdev_desc *tx_ring;
661 /* The addresses of receive-in-place skbuffs */
662 struct sk_buff *rx_skbuff[RX_RING_SIZE];
663 dma_addr_t rx_dma[RX_RING_SIZE];
664 /* address of a sent-in-place packet/buffer, for later free() */
665 struct sk_buff *tx_skbuff[TX_RING_SIZE];
666 dma_addr_t tx_dma[TX_RING_SIZE];
667 struct net_device_stats stats;
668 /* Media monitoring timer */
669 struct timer_list timer;
670 /* Frequently used values: keep some adjacent for cache effect */
671 struct pci_dev *pci_dev;
672 struct netdev_desc *rx_head_desc;
673 /* Producer/consumer ring indices */
674 unsigned int cur_rx, dirty_rx;
675 unsigned int cur_tx, dirty_tx;
676 /* Based on MTU+slack. */
677 unsigned int rx_buf_sz;
679 /* Interrupt status */
681 /* Do not touch the nic registers */
683 /* external phy that is used: only valid if dev->if_port != PORT_TP */
685 int phy_addr_external;
686 unsigned int full_duplex;
690 /* FIFO and PCI burst thresholds */
691 u32 tx_config, rx_config;
692 /* original contents of ClkRun register */
694 /* silicon revision */
696 /* expected DSPCFG value */
698 /* parms saved in ethtool format */
699 u16 speed; /* The forced speed, 10Mb, 100Mb, gigabit */
700 u8 duplex; /* Duplex, half or full */
701 u8 autoneg; /* Autonegotiation enabled */
702 /* MII transceiver section */
711 static void move_int_phy(struct net_device *dev, int addr);
712 static int eeprom_read(void __iomem *ioaddr, int location);
713 static int mdio_read(struct net_device *dev, int reg);
714 static void mdio_write(struct net_device *dev, int reg, u16 data);
715 static void init_phy_fixup(struct net_device *dev);
716 static int miiport_read(struct net_device *dev, int phy_id, int reg);
717 static void miiport_write(struct net_device *dev, int phy_id, int reg, u16 data);
718 static int find_mii(struct net_device *dev);
719 static void natsemi_reset(struct net_device *dev);
720 static void natsemi_reload_eeprom(struct net_device *dev);
721 static void natsemi_stop_rxtx(struct net_device *dev);
722 static int netdev_open(struct net_device *dev);
723 static void do_cable_magic(struct net_device *dev);
724 static void undo_cable_magic(struct net_device *dev);
725 static void check_link(struct net_device *dev);
726 static void netdev_timer(unsigned long data);
727 static void dump_ring(struct net_device *dev);
728 static void tx_timeout(struct net_device *dev);
729 static int alloc_ring(struct net_device *dev);
730 static void refill_rx(struct net_device *dev);
731 static void init_ring(struct net_device *dev);
732 static void drain_tx(struct net_device *dev);
733 static void drain_ring(struct net_device *dev);
734 static void free_ring(struct net_device *dev);
735 static void reinit_ring(struct net_device *dev);
736 static void init_registers(struct net_device *dev);
737 static int start_tx(struct sk_buff *skb, struct net_device *dev);
738 static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
739 static void netdev_error(struct net_device *dev, int intr_status);
740 static int natsemi_poll(struct net_device *dev, int *budget);
741 static void netdev_rx(struct net_device *dev, int *work_done, int work_to_do);
742 static void netdev_tx_done(struct net_device *dev);
743 static int natsemi_change_mtu(struct net_device *dev, int new_mtu);
744 #ifdef CONFIG_NET_POLL_CONTROLLER
745 static void natsemi_poll_controller(struct net_device *dev);
747 static void __set_rx_mode(struct net_device *dev);
748 static void set_rx_mode(struct net_device *dev);
749 static void __get_stats(struct net_device *dev);
750 static struct net_device_stats *get_stats(struct net_device *dev);
751 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
752 static int netdev_set_wol(struct net_device *dev, u32 newval);
753 static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur);
754 static int netdev_set_sopass(struct net_device *dev, u8 *newval);
755 static int netdev_get_sopass(struct net_device *dev, u8 *data);
756 static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
757 static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
758 static void enable_wol_mode(struct net_device *dev, int enable_intr);
759 static int netdev_close(struct net_device *dev);
760 static int netdev_get_regs(struct net_device *dev, u8 *buf);
761 static int netdev_get_eeprom(struct net_device *dev, u8 *buf);
762 static struct ethtool_ops ethtool_ops;
764 static inline void __iomem *ns_ioaddr(struct net_device *dev)
766 return (void __iomem *) dev->base_addr;
769 static inline void natsemi_irq_enable(struct net_device *dev)
771 writel(1, ns_ioaddr(dev) + IntrEnable);
772 readl(ns_ioaddr(dev) + IntrEnable);
775 static inline void natsemi_irq_disable(struct net_device *dev)
777 writel(0, ns_ioaddr(dev) + IntrEnable);
778 readl(ns_ioaddr(dev) + IntrEnable);
781 static void move_int_phy(struct net_device *dev, int addr)
783 struct netdev_private *np = netdev_priv(dev);
784 void __iomem *ioaddr = ns_ioaddr(dev);
788 * The internal phy is visible on the external mii bus. Therefore we must
789 * move it away before we can send commands to an external phy.
790 * There are two addresses we must avoid:
791 * - the address on the external phy that is used for transmission.
792 * - the address that we want to access. User space can access phys
793 * on the mii bus with SIOCGMIIREG/SIOCSMIIREG, independant from the
794 * phy that is used for transmission.
799 if (target == np->phy_addr_external)
801 writew(target, ioaddr + PhyCtrl);
802 readw(ioaddr + PhyCtrl);
806 static void __devinit natsemi_init_media (struct net_device *dev)
808 struct netdev_private *np = netdev_priv(dev);
811 netif_carrier_off(dev);
813 /* get the initial settings from hardware */
814 tmp = mdio_read(dev, MII_BMCR);
815 np->speed = (tmp & BMCR_SPEED100)? SPEED_100 : SPEED_10;
816 np->duplex = (tmp & BMCR_FULLDPLX)? DUPLEX_FULL : DUPLEX_HALF;
817 np->autoneg = (tmp & BMCR_ANENABLE)? AUTONEG_ENABLE: AUTONEG_DISABLE;
818 np->advertising= mdio_read(dev, MII_ADVERTISE);
820 if ((np->advertising & ADVERTISE_ALL) != ADVERTISE_ALL
821 && netif_msg_probe(np)) {
822 printk(KERN_INFO "natsemi %s: Transceiver default autonegotiation %s "
824 pci_name(np->pci_dev),
825 (mdio_read(dev, MII_BMCR) & BMCR_ANENABLE)?
826 "enabled, advertise" : "disabled, force",
828 (ADVERTISE_100FULL|ADVERTISE_100HALF))?
831 (ADVERTISE_100FULL|ADVERTISE_10FULL))?
834 if (netif_msg_probe(np))
836 "natsemi %s: Transceiver status %#04x advertising %#04x.\n",
837 pci_name(np->pci_dev), mdio_read(dev, MII_BMSR),
842 static int __devinit natsemi_probe1 (struct pci_dev *pdev,
843 const struct pci_device_id *ent)
845 struct net_device *dev;
846 struct netdev_private *np;
847 int i, option, irq, chip_idx = ent->driver_data;
848 static int find_cnt = -1;
849 unsigned long iostart, iosize;
850 void __iomem *ioaddr;
851 const int pcibar = 1; /* PCI base address register */
855 /* when built into the kernel, we only print version if device is found */
857 static int printed_version;
858 if (!printed_version++)
862 i = pci_enable_device(pdev);
865 /* natsemi has a non-standard PM control register
866 * in PCI config space. Some boards apparently need
867 * to be brought to D0 in this manner.
869 pci_read_config_dword(pdev, PCIPM, &tmp);
870 if (tmp & PCI_PM_CTRL_STATE_MASK) {
871 /* D0 state, disable PME assertion */
872 u32 newtmp = tmp & ~PCI_PM_CTRL_STATE_MASK;
873 pci_write_config_dword(pdev, PCIPM, newtmp);
877 iostart = pci_resource_start(pdev, pcibar);
878 iosize = pci_resource_len(pdev, pcibar);
881 pci_set_master(pdev);
883 dev = alloc_etherdev(sizeof (struct netdev_private));
886 SET_MODULE_OWNER(dev);
887 SET_NETDEV_DEV(dev, &pdev->dev);
889 i = pci_request_regions(pdev, DRV_NAME);
891 goto err_pci_request_regions;
893 ioaddr = ioremap(iostart, iosize);
899 /* Work around the dropped serial bit. */
900 prev_eedata = eeprom_read(ioaddr, 6);
901 for (i = 0; i < 3; i++) {
902 int eedata = eeprom_read(ioaddr, i + 7);
903 dev->dev_addr[i*2] = (eedata << 1) + (prev_eedata >> 15);
904 dev->dev_addr[i*2+1] = eedata >> 7;
905 prev_eedata = eedata;
908 dev->base_addr = (unsigned long __force) ioaddr;
911 np = netdev_priv(dev);
914 pci_set_drvdata(pdev, dev);
916 spin_lock_init(&np->lock);
917 np->msg_enable = (debug >= 0) ? (1<<debug)-1 : NATSEMI_DEF_MSG;
920 np->eeprom_size = natsemi_pci_info[chip_idx].eeprom_size;
923 * - If the nic was configured to use an external phy and if find_mii
924 * finds a phy: use external port, first phy that replies.
925 * - Otherwise: internal port.
926 * Note that the phy address for the internal phy doesn't matter:
927 * The address would be used to access a phy over the mii bus, but
928 * the internal phy is accessed through mapped registers.
930 if (readl(ioaddr + ChipConfig) & CfgExtPhy)
931 dev->if_port = PORT_MII;
933 dev->if_port = PORT_TP;
934 /* Reset the chip to erase previous misconfiguration. */
935 natsemi_reload_eeprom(dev);
938 if (dev->if_port != PORT_TP) {
939 np->phy_addr_external = find_mii(dev);
940 if (np->phy_addr_external == PHY_ADDR_NONE) {
941 dev->if_port = PORT_TP;
942 np->phy_addr_external = PHY_ADDR_INTERNAL;
945 np->phy_addr_external = PHY_ADDR_INTERNAL;
948 option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
950 option = dev->mem_start;
952 /* The lower four bits are the media type. */
958 "natsemi %s: ignoring user supplied media type %d",
959 pci_name(np->pci_dev), option & 15);
961 if (find_cnt < MAX_UNITS && full_duplex[find_cnt])
964 /* The chip-specific entries in the device structure. */
965 dev->open = &netdev_open;
966 dev->hard_start_xmit = &start_tx;
967 dev->stop = &netdev_close;
968 dev->get_stats = &get_stats;
969 dev->set_multicast_list = &set_rx_mode;
970 dev->change_mtu = &natsemi_change_mtu;
971 dev->do_ioctl = &netdev_ioctl;
972 dev->tx_timeout = &tx_timeout;
973 dev->watchdog_timeo = TX_TIMEOUT;
974 dev->poll = natsemi_poll;
977 #ifdef CONFIG_NET_POLL_CONTROLLER
978 dev->poll_controller = &natsemi_poll_controller;
980 SET_ETHTOOL_OPS(dev, ðtool_ops);
985 natsemi_init_media(dev);
987 /* save the silicon revision for later querying */
988 np->srr = readl(ioaddr + SiliconRev);
989 if (netif_msg_hw(np))
990 printk(KERN_INFO "natsemi %s: silicon revision %#04x.\n",
991 pci_name(np->pci_dev), np->srr);
993 i = register_netdev(dev);
995 goto err_register_netdev;
997 if (netif_msg_drv(np)) {
998 printk(KERN_INFO "natsemi %s: %s at %#08lx (%s), ",
999 dev->name, natsemi_pci_info[chip_idx].name, iostart,
1000 pci_name(np->pci_dev));
1001 for (i = 0; i < ETH_ALEN-1; i++)
1002 printk("%02x:", dev->dev_addr[i]);
1003 printk("%02x, IRQ %d", dev->dev_addr[i], irq);
1004 if (dev->if_port == PORT_TP)
1005 printk(", port TP.\n");
1007 printk(", port MII, phy ad %d.\n", np->phy_addr_external);
1011 err_register_netdev:
1015 pci_release_regions(pdev);
1016 pci_set_drvdata(pdev, NULL);
1018 err_pci_request_regions:
1024 /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
1025 The EEPROM code is for the common 93c06/46 EEPROMs with 6 bit addresses. */
1027 /* Delay between EEPROM clock transitions.
1028 No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
1029 a delay. Note that pre-2.0.34 kernels had a cache-alignment bug that
1030 made udelay() unreliable.
1031 The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
1034 #define eeprom_delay(ee_addr) readl(ee_addr)
1036 #define EE_Write0 (EE_ChipSelect)
1037 #define EE_Write1 (EE_ChipSelect | EE_DataIn)
1039 /* The EEPROM commands include the alway-set leading bit. */
1041 EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
1044 static int eeprom_read(void __iomem *addr, int location)
1048 void __iomem *ee_addr = addr + EECtrl;
1049 int read_cmd = location | EE_ReadCmd;
1051 writel(EE_Write0, ee_addr);
1053 /* Shift the read command bits out. */
1054 for (i = 10; i >= 0; i--) {
1055 short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
1056 writel(dataval, ee_addr);
1057 eeprom_delay(ee_addr);
1058 writel(dataval | EE_ShiftClk, ee_addr);
1059 eeprom_delay(ee_addr);
1061 writel(EE_ChipSelect, ee_addr);
1062 eeprom_delay(ee_addr);
1064 for (i = 0; i < 16; i++) {
1065 writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
1066 eeprom_delay(ee_addr);
1067 retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0;
1068 writel(EE_ChipSelect, ee_addr);
1069 eeprom_delay(ee_addr);
1072 /* Terminate the EEPROM access. */
1073 writel(EE_Write0, ee_addr);
1078 /* MII transceiver control section.
1079 * The 83815 series has an internal transceiver, and we present the
1080 * internal management registers as if they were MII connected.
1081 * External Phy registers are referenced through the MII interface.
1084 /* clock transitions >= 20ns (25MHz)
1085 * One readl should be good to PCI @ 100MHz
1087 #define mii_delay(ioaddr) readl(ioaddr + EECtrl)
1089 static int mii_getbit (struct net_device *dev)
1092 void __iomem *ioaddr = ns_ioaddr(dev);
1094 writel(MII_ShiftClk, ioaddr + EECtrl);
1095 data = readl(ioaddr + EECtrl);
1096 writel(0, ioaddr + EECtrl);
1098 return (data & MII_Data)? 1 : 0;
1101 static void mii_send_bits (struct net_device *dev, u32 data, int len)
1104 void __iomem *ioaddr = ns_ioaddr(dev);
1106 for (i = (1 << (len-1)); i; i >>= 1)
1108 u32 mdio_val = MII_Write | ((data & i)? MII_Data : 0);
1109 writel(mdio_val, ioaddr + EECtrl);
1111 writel(mdio_val | MII_ShiftClk, ioaddr + EECtrl);
1114 writel(0, ioaddr + EECtrl);
1118 static int miiport_read(struct net_device *dev, int phy_id, int reg)
1125 mii_send_bits (dev, 0xffffffff, 32);
1126 /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
1127 /* ST,OP = 0110'b for read operation */
1128 cmd = (0x06 << 10) | (phy_id << 5) | reg;
1129 mii_send_bits (dev, cmd, 14);
1131 if (mii_getbit (dev))
1134 for (i = 0; i < 16; i++) {
1136 retval |= mii_getbit (dev);
1143 static void miiport_write(struct net_device *dev, int phy_id, int reg, u16 data)
1148 mii_send_bits (dev, 0xffffffff, 32);
1149 /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
1150 /* ST,OP,AAAAA,RRRRR,TA = 0101xxxxxxxxxx10'b = 0x5002 for write */
1151 cmd = (0x5002 << 16) | (phy_id << 23) | (reg << 18) | data;
1152 mii_send_bits (dev, cmd, 32);
1157 static int mdio_read(struct net_device *dev, int reg)
1159 struct netdev_private *np = netdev_priv(dev);
1160 void __iomem *ioaddr = ns_ioaddr(dev);
1162 /* The 83815 series has two ports:
1163 * - an internal transceiver
1164 * - an external mii bus
1166 if (dev->if_port == PORT_TP)
1167 return readw(ioaddr+BasicControl+(reg<<2));
1169 return miiport_read(dev, np->phy_addr_external, reg);
1172 static void mdio_write(struct net_device *dev, int reg, u16 data)
1174 struct netdev_private *np = netdev_priv(dev);
1175 void __iomem *ioaddr = ns_ioaddr(dev);
1177 /* The 83815 series has an internal transceiver; handle separately */
1178 if (dev->if_port == PORT_TP)
1179 writew(data, ioaddr+BasicControl+(reg<<2));
1181 miiport_write(dev, np->phy_addr_external, reg, data);
1184 static void init_phy_fixup(struct net_device *dev)
1186 struct netdev_private *np = netdev_priv(dev);
1187 void __iomem *ioaddr = ns_ioaddr(dev);
1192 /* restore stuff lost when power was out */
1193 tmp = mdio_read(dev, MII_BMCR);
1194 if (np->autoneg == AUTONEG_ENABLE) {
1195 /* renegotiate if something changed */
1196 if ((tmp & BMCR_ANENABLE) == 0
1197 || np->advertising != mdio_read(dev, MII_ADVERTISE))
1199 /* turn on autonegotiation and force negotiation */
1200 tmp |= (BMCR_ANENABLE | BMCR_ANRESTART);
1201 mdio_write(dev, MII_ADVERTISE, np->advertising);
1204 /* turn off auto negotiation, set speed and duplexity */
1205 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
1206 if (np->speed == SPEED_100)
1207 tmp |= BMCR_SPEED100;
1208 if (np->duplex == DUPLEX_FULL)
1209 tmp |= BMCR_FULLDPLX;
1211 * Note: there is no good way to inform the link partner
1212 * that our capabilities changed. The user has to unplug
1213 * and replug the network cable after some changes, e.g.
1214 * after switching from 10HD, autoneg off to 100 HD,
1218 mdio_write(dev, MII_BMCR, tmp);
1219 readl(ioaddr + ChipConfig);
1222 /* find out what phy this is */
1223 np->mii = (mdio_read(dev, MII_PHYSID1) << 16)
1224 + mdio_read(dev, MII_PHYSID2);
1226 /* handle external phys here */
1228 case PHYID_AM79C874:
1229 /* phy specific configuration for fibre/tp operation */
1230 tmp = mdio_read(dev, MII_MCTRL);
1231 tmp &= ~(MII_FX_SEL | MII_EN_SCRM);
1232 if (dev->if_port == PORT_FIBRE)
1236 mdio_write(dev, MII_MCTRL, tmp);
1241 cfg = readl(ioaddr + ChipConfig);
1242 if (cfg & CfgExtPhy)
1245 /* On page 78 of the spec, they recommend some settings for "optimum
1246 performance" to be done in sequence. These settings optimize some
1247 of the 100Mbit autodetection circuitry. They say we only want to
1248 do this for rev C of the chip, but engineers at NSC (Bradley
1249 Kennedy) recommends always setting them. If you don't, you get
1250 errors on some autonegotiations that make the device unusable.
1252 It seems that the DSP needs a few usec to reinitialize after
1253 the start of the phy. Just retry writing these values until they
1256 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1259 writew(1, ioaddr + PGSEL);
1260 writew(PMDCSR_VAL, ioaddr + PMDCSR);
1261 writew(TSTDAT_VAL, ioaddr + TSTDAT);
1262 np->dspcfg = (np->srr <= SRR_DP83815_C)?
1263 DSPCFG_VAL : (DSPCFG_COEF | readw(ioaddr + DSPCFG));
1264 writew(np->dspcfg, ioaddr + DSPCFG);
1265 writew(SDCFG_VAL, ioaddr + SDCFG);
1266 writew(0, ioaddr + PGSEL);
1267 readl(ioaddr + ChipConfig);
1270 writew(1, ioaddr + PGSEL);
1271 dspcfg = readw(ioaddr + DSPCFG);
1272 writew(0, ioaddr + PGSEL);
1273 if (np->dspcfg == dspcfg)
1277 if (netif_msg_link(np)) {
1278 if (i==NATSEMI_HW_TIMEOUT) {
1280 "%s: DSPCFG mismatch after retrying for %d usec.\n",
1284 "%s: DSPCFG accepted after %d usec.\n",
1289 * Enable PHY Specific event based interrupts. Link state change
1290 * and Auto-Negotiation Completion are among the affected.
1291 * Read the intr status to clear it (needed for wake events).
1293 readw(ioaddr + MIntrStatus);
1294 writew(MICRIntEn, ioaddr + MIntrCtrl);
1297 static int switch_port_external(struct net_device *dev)
1299 struct netdev_private *np = netdev_priv(dev);
1300 void __iomem *ioaddr = ns_ioaddr(dev);
1303 cfg = readl(ioaddr + ChipConfig);
1304 if (cfg & CfgExtPhy)
1307 if (netif_msg_link(np)) {
1308 printk(KERN_INFO "%s: switching to external transceiver.\n",
1312 /* 1) switch back to external phy */
1313 writel(cfg | (CfgExtPhy | CfgPhyDis), ioaddr + ChipConfig);
1314 readl(ioaddr + ChipConfig);
1317 /* 2) reset the external phy: */
1318 /* resetting the external PHY has been known to cause a hub supplying
1319 * power over Ethernet to kill the power. We don't want to kill
1320 * power to this computer, so we avoid resetting the phy.
1323 /* 3) reinit the phy fixup, it got lost during power down. */
1324 move_int_phy(dev, np->phy_addr_external);
1325 init_phy_fixup(dev);
1330 static int switch_port_internal(struct net_device *dev)
1332 struct netdev_private *np = netdev_priv(dev);
1333 void __iomem *ioaddr = ns_ioaddr(dev);
1338 cfg = readl(ioaddr + ChipConfig);
1339 if (!(cfg &CfgExtPhy))
1342 if (netif_msg_link(np)) {
1343 printk(KERN_INFO "%s: switching to internal transceiver.\n",
1346 /* 1) switch back to internal phy: */
1347 cfg = cfg & ~(CfgExtPhy | CfgPhyDis);
1348 writel(cfg, ioaddr + ChipConfig);
1349 readl(ioaddr + ChipConfig);
1352 /* 2) reset the internal phy: */
1353 bmcr = readw(ioaddr+BasicControl+(MII_BMCR<<2));
1354 writel(bmcr | BMCR_RESET, ioaddr+BasicControl+(MII_BMCR<<2));
1355 readl(ioaddr + ChipConfig);
1357 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1358 bmcr = readw(ioaddr+BasicControl+(MII_BMCR<<2));
1359 if (!(bmcr & BMCR_RESET))
1363 if (i==NATSEMI_HW_TIMEOUT && netif_msg_link(np)) {
1365 "%s: phy reset did not complete in %d usec.\n",
1368 /* 3) reinit the phy fixup, it got lost during power down. */
1369 init_phy_fixup(dev);
1374 /* Scan for a PHY on the external mii bus.
1375 * There are two tricky points:
1376 * - Do not scan while the internal phy is enabled. The internal phy will
1377 * crash: e.g. reads from the DSPCFG register will return odd values and
1378 * the nasty random phy reset code will reset the nic every few seconds.
1379 * - The internal phy must be moved around, an external phy could
1380 * have the same address as the internal phy.
1382 static int find_mii(struct net_device *dev)
1384 struct netdev_private *np = netdev_priv(dev);
1389 /* Switch to external phy */
1390 did_switch = switch_port_external(dev);
1392 /* Scan the possible phy addresses:
1394 * PHY address 0 means that the phy is in isolate mode. Not yet
1395 * supported due to lack of test hardware. User space should
1396 * handle it through ethtool.
1398 for (i = 1; i <= 31; i++) {
1399 move_int_phy(dev, i);
1400 tmp = miiport_read(dev, i, MII_BMSR);
1401 if (tmp != 0xffff && tmp != 0x0000) {
1402 /* found something! */
1403 np->mii = (mdio_read(dev, MII_PHYSID1) << 16)
1404 + mdio_read(dev, MII_PHYSID2);
1405 if (netif_msg_probe(np)) {
1406 printk(KERN_INFO "natsemi %s: found external phy %08x at address %d.\n",
1407 pci_name(np->pci_dev), np->mii, i);
1412 /* And switch back to internal phy: */
1414 switch_port_internal(dev);
1418 /* CFG bits [13:16] [18:23] */
1419 #define CFG_RESET_SAVE 0xfde000
1420 /* WCSR bits [0:4] [9:10] */
1421 #define WCSR_RESET_SAVE 0x61f
1422 /* RFCR bits [20] [22] [27:31] */
1423 #define RFCR_RESET_SAVE 0xf8500000;
1425 static void natsemi_reset(struct net_device *dev)
1433 struct netdev_private *np = netdev_priv(dev);
1434 void __iomem *ioaddr = ns_ioaddr(dev);
1437 * Resetting the chip causes some registers to be lost.
1438 * Natsemi suggests NOT reloading the EEPROM while live, so instead
1439 * we save the state that would have been loaded from EEPROM
1440 * on a normal power-up (see the spec EEPROM map). This assumes
1441 * whoever calls this will follow up with init_registers() eventually.
1445 cfg = readl(ioaddr + ChipConfig) & CFG_RESET_SAVE;
1447 wcsr = readl(ioaddr + WOLCmd) & WCSR_RESET_SAVE;
1449 rfcr = readl(ioaddr + RxFilterAddr) & RFCR_RESET_SAVE;
1451 for (i = 0; i < 3; i++) {
1452 writel(i*2, ioaddr + RxFilterAddr);
1453 pmatch[i] = readw(ioaddr + RxFilterData);
1456 for (i = 0; i < 3; i++) {
1457 writel(0xa+(i*2), ioaddr + RxFilterAddr);
1458 sopass[i] = readw(ioaddr + RxFilterData);
1461 /* now whack the chip */
1462 writel(ChipReset, ioaddr + ChipCmd);
1463 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1464 if (!(readl(ioaddr + ChipCmd) & ChipReset))
1468 if (i==NATSEMI_HW_TIMEOUT) {
1469 printk(KERN_WARNING "%s: reset did not complete in %d usec.\n",
1471 } else if (netif_msg_hw(np)) {
1472 printk(KERN_DEBUG "%s: reset completed in %d usec.\n",
1477 cfg |= readl(ioaddr + ChipConfig) & ~CFG_RESET_SAVE;
1478 /* turn on external phy if it was selected */
1479 if (dev->if_port == PORT_TP)
1480 cfg &= ~(CfgExtPhy | CfgPhyDis);
1482 cfg |= (CfgExtPhy | CfgPhyDis);
1483 writel(cfg, ioaddr + ChipConfig);
1485 wcsr |= readl(ioaddr + WOLCmd) & ~WCSR_RESET_SAVE;
1486 writel(wcsr, ioaddr + WOLCmd);
1488 rfcr |= readl(ioaddr + RxFilterAddr) & ~RFCR_RESET_SAVE;
1489 /* restore PMATCH */
1490 for (i = 0; i < 3; i++) {
1491 writel(i*2, ioaddr + RxFilterAddr);
1492 writew(pmatch[i], ioaddr + RxFilterData);
1494 for (i = 0; i < 3; i++) {
1495 writel(0xa+(i*2), ioaddr + RxFilterAddr);
1496 writew(sopass[i], ioaddr + RxFilterData);
1499 writel(rfcr, ioaddr + RxFilterAddr);
1502 static void reset_rx(struct net_device *dev)
1505 struct netdev_private *np = netdev_priv(dev);
1506 void __iomem *ioaddr = ns_ioaddr(dev);
1508 np->intr_status &= ~RxResetDone;
1510 writel(RxReset, ioaddr + ChipCmd);
1512 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1513 np->intr_status |= readl(ioaddr + IntrStatus);
1514 if (np->intr_status & RxResetDone)
1518 if (i==NATSEMI_HW_TIMEOUT) {
1519 printk(KERN_WARNING "%s: RX reset did not complete in %d usec.\n",
1521 } else if (netif_msg_hw(np)) {
1522 printk(KERN_WARNING "%s: RX reset took %d usec.\n",
1527 static void natsemi_reload_eeprom(struct net_device *dev)
1529 struct netdev_private *np = netdev_priv(dev);
1530 void __iomem *ioaddr = ns_ioaddr(dev);
1533 writel(EepromReload, ioaddr + PCIBusCfg);
1534 for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1536 if (!(readl(ioaddr + PCIBusCfg) & EepromReload))
1539 if (i==NATSEMI_HW_TIMEOUT) {
1540 printk(KERN_WARNING "natsemi %s: EEPROM did not reload in %d usec.\n",
1541 pci_name(np->pci_dev), i*50);
1542 } else if (netif_msg_hw(np)) {
1543 printk(KERN_DEBUG "natsemi %s: EEPROM reloaded in %d usec.\n",
1544 pci_name(np->pci_dev), i*50);
1548 static void natsemi_stop_rxtx(struct net_device *dev)
1550 void __iomem * ioaddr = ns_ioaddr(dev);
1551 struct netdev_private *np = netdev_priv(dev);
1554 writel(RxOff | TxOff, ioaddr + ChipCmd);
1555 for(i=0;i< NATSEMI_HW_TIMEOUT;i++) {
1556 if ((readl(ioaddr + ChipCmd) & (TxOn|RxOn)) == 0)
1560 if (i==NATSEMI_HW_TIMEOUT) {
1561 printk(KERN_WARNING "%s: Tx/Rx process did not stop in %d usec.\n",
1563 } else if (netif_msg_hw(np)) {
1564 printk(KERN_DEBUG "%s: Tx/Rx process stopped in %d usec.\n",
1569 static int netdev_open(struct net_device *dev)
1571 struct netdev_private *np = netdev_priv(dev);
1572 void __iomem * ioaddr = ns_ioaddr(dev);
1575 /* Reset the chip, just in case. */
1578 i = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
1581 if (netif_msg_ifup(np))
1582 printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
1583 dev->name, dev->irq);
1584 i = alloc_ring(dev);
1586 free_irq(dev->irq, dev);
1590 spin_lock_irq(&np->lock);
1591 init_registers(dev);
1592 /* now set the MAC address according to dev->dev_addr */
1593 for (i = 0; i < 3; i++) {
1594 u16 mac = (dev->dev_addr[2*i+1]<<8) + dev->dev_addr[2*i];
1596 writel(i*2, ioaddr + RxFilterAddr);
1597 writew(mac, ioaddr + RxFilterData);
1599 writel(np->cur_rx_mode, ioaddr + RxFilterAddr);
1600 spin_unlock_irq(&np->lock);
1602 netif_start_queue(dev);
1604 if (netif_msg_ifup(np))
1605 printk(KERN_DEBUG "%s: Done netdev_open(), status: %#08x.\n",
1606 dev->name, (int)readl(ioaddr + ChipCmd));
1608 /* Set the timer to check for link beat. */
1609 init_timer(&np->timer);
1610 np->timer.expires = jiffies + NATSEMI_TIMER_FREQ;
1611 np->timer.data = (unsigned long)dev;
1612 np->timer.function = &netdev_timer; /* timer handler */
1613 add_timer(&np->timer);
1618 static void do_cable_magic(struct net_device *dev)
1620 struct netdev_private *np = netdev_priv(dev);
1621 void __iomem *ioaddr = ns_ioaddr(dev);
1623 if (dev->if_port != PORT_TP)
1626 if (np->srr >= SRR_DP83816_A5)
1630 * 100 MBit links with short cables can trip an issue with the chip.
1631 * The problem manifests as lots of CRC errors and/or flickering
1632 * activity LED while idle. This process is based on instructions
1633 * from engineers at National.
1635 if (readl(ioaddr + ChipConfig) & CfgSpeed100) {
1638 writew(1, ioaddr + PGSEL);
1640 * coefficient visibility should already be enabled via
1643 data = readw(ioaddr + TSTDAT) & 0xff;
1645 * the value must be negative, and within certain values
1646 * (these values all come from National)
1648 if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
1649 struct netdev_private *np = netdev_priv(dev);
1651 /* the bug has been triggered - fix the coefficient */
1652 writew(TSTDAT_FIXED, ioaddr + TSTDAT);
1653 /* lock the value */
1654 data = readw(ioaddr + DSPCFG);
1655 np->dspcfg = data | DSPCFG_LOCK;
1656 writew(np->dspcfg, ioaddr + DSPCFG);
1658 writew(0, ioaddr + PGSEL);
1662 static void undo_cable_magic(struct net_device *dev)
1665 struct netdev_private *np = netdev_priv(dev);
1666 void __iomem * ioaddr = ns_ioaddr(dev);
1668 if (dev->if_port != PORT_TP)
1671 if (np->srr >= SRR_DP83816_A5)
1674 writew(1, ioaddr + PGSEL);
1675 /* make sure the lock bit is clear */
1676 data = readw(ioaddr + DSPCFG);
1677 np->dspcfg = data & ~DSPCFG_LOCK;
1678 writew(np->dspcfg, ioaddr + DSPCFG);
1679 writew(0, ioaddr + PGSEL);
1682 static void check_link(struct net_device *dev)
1684 struct netdev_private *np = netdev_priv(dev);
1685 void __iomem * ioaddr = ns_ioaddr(dev);
1689 /* The link status field is latched: it remains low after a temporary
1690 * link failure until it's read. We need the current link status,
1693 mdio_read(dev, MII_BMSR);
1694 bmsr = mdio_read(dev, MII_BMSR);
1696 if (!(bmsr & BMSR_LSTATUS)) {
1697 if (netif_carrier_ok(dev)) {
1698 if (netif_msg_link(np))
1699 printk(KERN_NOTICE "%s: link down.\n",
1701 netif_carrier_off(dev);
1702 undo_cable_magic(dev);
1706 if (!netif_carrier_ok(dev)) {
1707 if (netif_msg_link(np))
1708 printk(KERN_NOTICE "%s: link up.\n", dev->name);
1709 netif_carrier_on(dev);
1710 do_cable_magic(dev);
1713 duplex = np->full_duplex;
1715 if (bmsr & BMSR_ANEGCOMPLETE) {
1716 int tmp = mii_nway_result(
1717 np->advertising & mdio_read(dev, MII_LPA));
1718 if (tmp == LPA_100FULL || tmp == LPA_10FULL)
1720 } else if (mdio_read(dev, MII_BMCR) & BMCR_FULLDPLX)
1724 /* if duplex is set then bit 28 must be set, too */
1725 if (duplex ^ !!(np->rx_config & RxAcceptTx)) {
1726 if (netif_msg_link(np))
1728 "%s: Setting %s-duplex based on negotiated "
1729 "link capability.\n", dev->name,
1730 duplex ? "full" : "half");
1732 np->rx_config |= RxAcceptTx;
1733 np->tx_config |= TxCarrierIgn | TxHeartIgn;
1735 np->rx_config &= ~RxAcceptTx;
1736 np->tx_config &= ~(TxCarrierIgn | TxHeartIgn);
1738 writel(np->tx_config, ioaddr + TxConfig);
1739 writel(np->rx_config, ioaddr + RxConfig);
1743 static void init_registers(struct net_device *dev)
1745 struct netdev_private *np = netdev_priv(dev);
1746 void __iomem * ioaddr = ns_ioaddr(dev);
1748 init_phy_fixup(dev);
1750 /* clear any interrupts that are pending, such as wake events */
1751 readl(ioaddr + IntrStatus);
1753 writel(np->ring_dma, ioaddr + RxRingPtr);
1754 writel(np->ring_dma + RX_RING_SIZE * sizeof(struct netdev_desc),
1755 ioaddr + TxRingPtr);
1757 /* Initialize other registers.
1758 * Configure the PCI bus bursts and FIFO thresholds.
1759 * Configure for standard, in-spec Ethernet.
1760 * Start with half-duplex. check_link will update
1761 * to the correct settings.
1764 /* DRTH: 2: start tx if 64 bytes are in the fifo
1765 * FLTH: 0x10: refill with next packet if 512 bytes are free
1766 * MXDMA: 0: up to 256 byte bursts.
1767 * MXDMA must be <= FLTH
1771 np->tx_config = TxAutoPad | TxCollRetry | TxMxdma_256 |
1772 TX_FLTH_VAL | TX_DRTH_VAL_START;
1773 writel(np->tx_config, ioaddr + TxConfig);
1775 /* DRTH 0x10: start copying to memory if 128 bytes are in the fifo
1776 * MXDMA 0: up to 256 byte bursts
1778 np->rx_config = RxMxdma_256 | RX_DRTH_VAL;
1779 /* if receive ring now has bigger buffers than normal, enable jumbo */
1780 if (np->rx_buf_sz > NATSEMI_LONGPKT)
1781 np->rx_config |= RxAcceptLong;
1783 writel(np->rx_config, ioaddr + RxConfig);
1786 * The PME bit is initialized from the EEPROM contents.
1787 * PCI cards probably have PME disabled, but motherboard
1788 * implementations may have PME set to enable WakeOnLan.
1789 * With PME set the chip will scan incoming packets but
1790 * nothing will be written to memory. */
1791 np->SavedClkRun = readl(ioaddr + ClkRun);
1792 writel(np->SavedClkRun & ~PMEEnable, ioaddr + ClkRun);
1793 if (np->SavedClkRun & PMEStatus && netif_msg_wol(np)) {
1794 printk(KERN_NOTICE "%s: Wake-up event %#08x\n",
1795 dev->name, readl(ioaddr + WOLCmd));
1801 /* Enable interrupts by setting the interrupt mask. */
1802 writel(DEFAULT_INTR, ioaddr + IntrMask);
1803 writel(1, ioaddr + IntrEnable);
1805 writel(RxOn | TxOn, ioaddr + ChipCmd);
1806 writel(StatsClear, ioaddr + StatsCtrl); /* Clear Stats */
1812 * 1) check for link changes. Usually they are handled by the MII interrupt
1813 * but it doesn't hurt to check twice.
1814 * 2) check for sudden death of the NIC:
1815 * It seems that a reference set for this chip went out with incorrect info,
1816 * and there exist boards that aren't quite right. An unexpected voltage
1817 * drop can cause the PHY to get itself in a weird state (basically reset).
1818 * NOTE: this only seems to affect revC chips.
1819 * 3) check of death of the RX path due to OOM
1821 static void netdev_timer(unsigned long data)
1823 struct net_device *dev = (struct net_device *)data;
1824 struct netdev_private *np = netdev_priv(dev);
1825 void __iomem * ioaddr = ns_ioaddr(dev);
1826 int next_tick = 5*HZ;
1828 if (netif_msg_timer(np)) {
1829 /* DO NOT read the IntrStatus register,
1830 * a read clears any pending interrupts.
1832 printk(KERN_DEBUG "%s: Media selection timer tick.\n",
1836 if (dev->if_port == PORT_TP) {
1839 spin_lock_irq(&np->lock);
1840 /* check for a nasty random phy-reset - use dspcfg as a flag */
1841 writew(1, ioaddr+PGSEL);
1842 dspcfg = readw(ioaddr+DSPCFG);
1843 writew(0, ioaddr+PGSEL);
1844 if (dspcfg != np->dspcfg) {
1845 if (!netif_queue_stopped(dev)) {
1846 spin_unlock_irq(&np->lock);
1847 if (netif_msg_hw(np))
1848 printk(KERN_NOTICE "%s: possible phy reset: "
1849 "re-initializing\n", dev->name);
1850 disable_irq(dev->irq);
1851 spin_lock_irq(&np->lock);
1852 natsemi_stop_rxtx(dev);
1855 init_registers(dev);
1856 spin_unlock_irq(&np->lock);
1857 enable_irq(dev->irq);
1861 spin_unlock_irq(&np->lock);
1864 /* init_registers() calls check_link() for the above case */
1866 spin_unlock_irq(&np->lock);
1869 spin_lock_irq(&np->lock);
1871 spin_unlock_irq(&np->lock);
1874 disable_irq(dev->irq);
1877 enable_irq(dev->irq);
1879 writel(RxOn, ioaddr + ChipCmd);
1884 mod_timer(&np->timer, jiffies + next_tick);
1887 static void dump_ring(struct net_device *dev)
1889 struct netdev_private *np = netdev_priv(dev);
1891 if (netif_msg_pktdata(np)) {
1893 printk(KERN_DEBUG " Tx ring at %p:\n", np->tx_ring);
1894 for (i = 0; i < TX_RING_SIZE; i++) {
1895 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1896 i, np->tx_ring[i].next_desc,
1897 np->tx_ring[i].cmd_status,
1898 np->tx_ring[i].addr);
1900 printk(KERN_DEBUG " Rx ring %p:\n", np->rx_ring);
1901 for (i = 0; i < RX_RING_SIZE; i++) {
1902 printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1903 i, np->rx_ring[i].next_desc,
1904 np->rx_ring[i].cmd_status,
1905 np->rx_ring[i].addr);
1910 static void tx_timeout(struct net_device *dev)
1912 struct netdev_private *np = netdev_priv(dev);
1913 void __iomem * ioaddr = ns_ioaddr(dev);
1915 disable_irq(dev->irq);
1916 spin_lock_irq(&np->lock);
1917 if (!np->hands_off) {
1918 if (netif_msg_tx_err(np))
1920 "%s: Transmit timed out, status %#08x,"
1922 dev->name, readl(ioaddr + IntrStatus));
1927 init_registers(dev);
1930 "%s: tx_timeout while in hands_off state?\n",
1933 spin_unlock_irq(&np->lock);
1934 enable_irq(dev->irq);
1936 dev->trans_start = jiffies;
1937 np->stats.tx_errors++;
1938 netif_wake_queue(dev);
1941 static int alloc_ring(struct net_device *dev)
1943 struct netdev_private *np = netdev_priv(dev);
1944 np->rx_ring = pci_alloc_consistent(np->pci_dev,
1945 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
1949 np->tx_ring = &np->rx_ring[RX_RING_SIZE];
1953 static void refill_rx(struct net_device *dev)
1955 struct netdev_private *np = netdev_priv(dev);
1957 /* Refill the Rx ring buffers. */
1958 for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1959 struct sk_buff *skb;
1960 int entry = np->dirty_rx % RX_RING_SIZE;
1961 if (np->rx_skbuff[entry] == NULL) {
1962 unsigned int buflen = np->rx_buf_sz+NATSEMI_PADDING;
1963 skb = dev_alloc_skb(buflen);
1964 np->rx_skbuff[entry] = skb;
1966 break; /* Better luck next round. */
1967 skb->dev = dev; /* Mark as being used by this device. */
1968 np->rx_dma[entry] = pci_map_single(np->pci_dev,
1969 skb->data, buflen, PCI_DMA_FROMDEVICE);
1970 np->rx_ring[entry].addr = cpu_to_le32(np->rx_dma[entry]);
1972 np->rx_ring[entry].cmd_status = cpu_to_le32(np->rx_buf_sz);
1974 if (np->cur_rx - np->dirty_rx == RX_RING_SIZE) {
1975 if (netif_msg_rx_err(np))
1976 printk(KERN_WARNING "%s: going OOM.\n", dev->name);
1981 static void set_bufsize(struct net_device *dev)
1983 struct netdev_private *np = netdev_priv(dev);
1984 if (dev->mtu <= ETH_DATA_LEN)
1985 np->rx_buf_sz = ETH_DATA_LEN + NATSEMI_HEADERS;
1987 np->rx_buf_sz = dev->mtu + NATSEMI_HEADERS;
1990 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1991 static void init_ring(struct net_device *dev)
1993 struct netdev_private *np = netdev_priv(dev);
1997 np->dirty_tx = np->cur_tx = 0;
1998 for (i = 0; i < TX_RING_SIZE; i++) {
1999 np->tx_skbuff[i] = NULL;
2000 np->tx_ring[i].next_desc = cpu_to_le32(np->ring_dma
2001 +sizeof(struct netdev_desc)
2002 *((i+1)%TX_RING_SIZE+RX_RING_SIZE));
2003 np->tx_ring[i].cmd_status = 0;
2008 np->cur_rx = RX_RING_SIZE;
2012 np->rx_head_desc = &np->rx_ring[0];
2014 /* Please be carefull before changing this loop - at least gcc-2.95.1
2015 * miscompiles it otherwise.
2017 /* Initialize all Rx descriptors. */
2018 for (i = 0; i < RX_RING_SIZE; i++) {
2019 np->rx_ring[i].next_desc = cpu_to_le32(np->ring_dma
2020 +sizeof(struct netdev_desc)
2021 *((i+1)%RX_RING_SIZE));
2022 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
2023 np->rx_skbuff[i] = NULL;
2029 static void drain_tx(struct net_device *dev)
2031 struct netdev_private *np = netdev_priv(dev);
2034 for (i = 0; i < TX_RING_SIZE; i++) {
2035 if (np->tx_skbuff[i]) {
2036 pci_unmap_single(np->pci_dev,
2037 np->tx_dma[i], np->tx_skbuff[i]->len,
2039 dev_kfree_skb(np->tx_skbuff[i]);
2040 np->stats.tx_dropped++;
2042 np->tx_skbuff[i] = NULL;
2046 static void drain_rx(struct net_device *dev)
2048 struct netdev_private *np = netdev_priv(dev);
2049 unsigned int buflen = np->rx_buf_sz;
2052 /* Free all the skbuffs in the Rx queue. */
2053 for (i = 0; i < RX_RING_SIZE; i++) {
2054 np->rx_ring[i].cmd_status = 0;
2055 np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */
2056 if (np->rx_skbuff[i]) {
2057 pci_unmap_single(np->pci_dev,
2058 np->rx_dma[i], buflen,
2059 PCI_DMA_FROMDEVICE);
2060 dev_kfree_skb(np->rx_skbuff[i]);
2062 np->rx_skbuff[i] = NULL;
2066 static void drain_ring(struct net_device *dev)
2072 static void free_ring(struct net_device *dev)
2074 struct netdev_private *np = netdev_priv(dev);
2075 pci_free_consistent(np->pci_dev,
2076 sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
2077 np->rx_ring, np->ring_dma);
2080 static void reinit_rx(struct net_device *dev)
2082 struct netdev_private *np = netdev_priv(dev);
2087 np->cur_rx = RX_RING_SIZE;
2088 np->rx_head_desc = &np->rx_ring[0];
2089 /* Initialize all Rx descriptors. */
2090 for (i = 0; i < RX_RING_SIZE; i++)
2091 np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
2096 static void reinit_ring(struct net_device *dev)
2098 struct netdev_private *np = netdev_priv(dev);
2103 np->dirty_tx = np->cur_tx = 0;
2104 for (i=0;i<TX_RING_SIZE;i++)
2105 np->tx_ring[i].cmd_status = 0;
2110 static int start_tx(struct sk_buff *skb, struct net_device *dev)
2112 struct netdev_private *np = netdev_priv(dev);
2113 void __iomem * ioaddr = ns_ioaddr(dev);
2116 /* Note: Ordering is important here, set the field with the
2117 "ownership" bit last, and only then increment cur_tx. */
2119 /* Calculate the next Tx descriptor entry. */
2120 entry = np->cur_tx % TX_RING_SIZE;
2122 np->tx_skbuff[entry] = skb;
2123 np->tx_dma[entry] = pci_map_single(np->pci_dev,
2124 skb->data,skb->len, PCI_DMA_TODEVICE);
2126 np->tx_ring[entry].addr = cpu_to_le32(np->tx_dma[entry]);
2128 spin_lock_irq(&np->lock);
2130 if (!np->hands_off) {
2131 np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn | skb->len);
2132 /* StrongARM: Explicitly cache flush np->tx_ring and
2133 * skb->data,skb->len. */
2136 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
2137 netdev_tx_done(dev);
2138 if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1)
2139 netif_stop_queue(dev);
2141 /* Wake the potentially-idle transmit channel. */
2142 writel(TxOn, ioaddr + ChipCmd);
2144 dev_kfree_skb_irq(skb);
2145 np->stats.tx_dropped++;
2147 spin_unlock_irq(&np->lock);
2149 dev->trans_start = jiffies;
2151 if (netif_msg_tx_queued(np)) {
2152 printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
2153 dev->name, np->cur_tx, entry);
2158 static void netdev_tx_done(struct net_device *dev)
2160 struct netdev_private *np = netdev_priv(dev);
2162 for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
2163 int entry = np->dirty_tx % TX_RING_SIZE;
2164 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescOwn))
2166 if (netif_msg_tx_done(np))
2168 "%s: tx frame #%d finished, status %#08x.\n",
2169 dev->name, np->dirty_tx,
2170 le32_to_cpu(np->tx_ring[entry].cmd_status));
2171 if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescPktOK)) {
2172 np->stats.tx_packets++;
2173 np->stats.tx_bytes += np->tx_skbuff[entry]->len;
2174 } else { /* Various Tx errors */
2176 le32_to_cpu(np->tx_ring[entry].cmd_status);
2177 if (tx_status & (DescTxAbort|DescTxExcColl))
2178 np->stats.tx_aborted_errors++;
2179 if (tx_status & DescTxFIFO)
2180 np->stats.tx_fifo_errors++;
2181 if (tx_status & DescTxCarrier)
2182 np->stats.tx_carrier_errors++;
2183 if (tx_status & DescTxOOWCol)
2184 np->stats.tx_window_errors++;
2185 np->stats.tx_errors++;
2187 pci_unmap_single(np->pci_dev,np->tx_dma[entry],
2188 np->tx_skbuff[entry]->len,
2190 /* Free the original skb. */
2191 dev_kfree_skb_irq(np->tx_skbuff[entry]);
2192 np->tx_skbuff[entry] = NULL;
2194 if (netif_queue_stopped(dev)
2195 && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
2196 /* The ring is no longer full, wake queue. */
2197 netif_wake_queue(dev);
2201 /* The interrupt handler doesn't actually handle interrupts itself, it
2202 * schedules a NAPI poll if there is anything to do. */
2203 static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
2205 struct net_device *dev = dev_instance;
2206 struct netdev_private *np = netdev_priv(dev);
2207 void __iomem * ioaddr = ns_ioaddr(dev);
2212 /* Reading automatically acknowledges. */
2213 np->intr_status = readl(ioaddr + IntrStatus);
2215 if (netif_msg_intr(np))
2217 "%s: Interrupt, status %#08x, mask %#08x.\n",
2218 dev->name, np->intr_status,
2219 readl(ioaddr + IntrMask));
2221 if (!np->intr_status)
2224 prefetch(&np->rx_skbuff[np->cur_rx % RX_RING_SIZE]);
2226 if (netif_rx_schedule_prep(dev)) {
2227 /* Disable interrupts and register for poll */
2228 natsemi_irq_disable(dev);
2229 __netif_rx_schedule(dev);
2234 /* This is the NAPI poll routine. As well as the standard RX handling
2235 * it also handles all other interrupts that the chip might raise.
2237 static int natsemi_poll(struct net_device *dev, int *budget)
2239 struct netdev_private *np = netdev_priv(dev);
2240 void __iomem * ioaddr = ns_ioaddr(dev);
2242 int work_to_do = min(*budget, dev->quota);
2246 if (np->intr_status &
2247 (IntrTxDone | IntrTxIntr | IntrTxIdle | IntrTxErr)) {
2248 spin_lock(&np->lock);
2249 netdev_tx_done(dev);
2250 spin_unlock(&np->lock);
2253 /* Abnormal error summary/uncommon events handlers. */
2254 if (np->intr_status & IntrAbnormalSummary)
2255 netdev_error(dev, np->intr_status);
2257 if (np->intr_status &
2258 (IntrRxDone | IntrRxIntr | RxStatusFIFOOver |
2259 IntrRxErr | IntrRxOverrun)) {
2260 netdev_rx(dev, &work_done, work_to_do);
2263 *budget -= work_done;
2264 dev->quota -= work_done;
2266 if (work_done >= work_to_do)
2269 np->intr_status = readl(ioaddr + IntrStatus);
2270 } while (np->intr_status);
2272 netif_rx_complete(dev);
2274 /* Reenable interrupts providing nothing is trying to shut
2276 spin_lock(&np->lock);
2277 if (!np->hands_off && netif_running(dev))
2278 natsemi_irq_enable(dev);
2279 spin_unlock(&np->lock);
2284 /* This routine is logically part of the interrupt handler, but separated
2285 for clarity and better register allocation. */
2286 static void netdev_rx(struct net_device *dev, int *work_done, int work_to_do)
2288 struct netdev_private *np = netdev_priv(dev);
2289 int entry = np->cur_rx % RX_RING_SIZE;
2290 int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
2291 s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
2292 unsigned int buflen = np->rx_buf_sz;
2293 void __iomem * ioaddr = ns_ioaddr(dev);
2295 /* If the driver owns the next entry it's a new packet. Send it up. */
2296 while (desc_status < 0) { /* e.g. & DescOwn */
2298 if (netif_msg_rx_status(np))
2300 " netdev_rx() entry %d status was %#08x.\n",
2301 entry, desc_status);
2305 if (*work_done >= work_to_do)
2310 pkt_len = (desc_status & DescSizeMask) - 4;
2311 if ((desc_status&(DescMore|DescPktOK|DescRxLong)) != DescPktOK){
2312 if (desc_status & DescMore) {
2313 if (netif_msg_rx_err(np))
2315 "%s: Oversized(?) Ethernet "
2316 "frame spanned multiple "
2317 "buffers, entry %#08x "
2318 "status %#08x.\n", dev->name,
2319 np->cur_rx, desc_status);
2320 np->stats.rx_length_errors++;
2322 /* The RX state machine has probably
2323 * locked up beneath us. Follow the
2324 * reset procedure documented in
2327 spin_lock_irq(&np->lock);
2330 writel(np->ring_dma, ioaddr + RxRingPtr);
2332 spin_unlock_irq(&np->lock);
2334 /* We'll enable RX on exit from this
2339 /* There was an error. */
2340 np->stats.rx_errors++;
2341 if (desc_status & (DescRxAbort|DescRxOver))
2342 np->stats.rx_over_errors++;
2343 if (desc_status & (DescRxLong|DescRxRunt))
2344 np->stats.rx_length_errors++;
2345 if (desc_status & (DescRxInvalid|DescRxAlign))
2346 np->stats.rx_frame_errors++;
2347 if (desc_status & DescRxCRC)
2348 np->stats.rx_crc_errors++;
2350 } else if (pkt_len > np->rx_buf_sz) {
2351 /* if this is the tail of a double buffer
2352 * packet, we've already counted the error
2353 * on the first part. Ignore the second half.
2356 struct sk_buff *skb;
2357 /* Omit CRC size. */
2358 /* Check if the packet is long enough to accept
2359 * without copying to a minimally-sized skbuff. */
2360 if (pkt_len < rx_copybreak
2361 && (skb = dev_alloc_skb(pkt_len + RX_OFFSET)) != NULL) {
2363 /* 16 byte align the IP header */
2364 skb_reserve(skb, RX_OFFSET);
2365 pci_dma_sync_single_for_cpu(np->pci_dev,
2368 PCI_DMA_FROMDEVICE);
2369 eth_copy_and_sum(skb,
2370 np->rx_skbuff[entry]->data, pkt_len, 0);
2371 skb_put(skb, pkt_len);
2372 pci_dma_sync_single_for_device(np->pci_dev,
2375 PCI_DMA_FROMDEVICE);
2377 pci_unmap_single(np->pci_dev, np->rx_dma[entry],
2378 buflen, PCI_DMA_FROMDEVICE);
2379 skb_put(skb = np->rx_skbuff[entry], pkt_len);
2380 np->rx_skbuff[entry] = NULL;
2382 skb->protocol = eth_type_trans(skb, dev);
2383 netif_receive_skb(skb);
2384 dev->last_rx = jiffies;
2385 np->stats.rx_packets++;
2386 np->stats.rx_bytes += pkt_len;
2388 entry = (++np->cur_rx) % RX_RING_SIZE;
2389 np->rx_head_desc = &np->rx_ring[entry];
2390 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
2394 /* Restart Rx engine if stopped. */
2396 mod_timer(&np->timer, jiffies + 1);
2398 writel(RxOn, ioaddr + ChipCmd);
2401 static void netdev_error(struct net_device *dev, int intr_status)
2403 struct netdev_private *np = netdev_priv(dev);
2404 void __iomem * ioaddr = ns_ioaddr(dev);
2406 spin_lock(&np->lock);
2407 if (intr_status & LinkChange) {
2408 u16 lpa = mdio_read(dev, MII_LPA);
2409 if (mdio_read(dev, MII_BMCR) & BMCR_ANENABLE
2410 && netif_msg_link(np)) {
2412 "%s: Autonegotiation advertising"
2413 " %#04x partner %#04x.\n", dev->name,
2414 np->advertising, lpa);
2417 /* read MII int status to clear the flag */
2418 readw(ioaddr + MIntrStatus);
2421 if (intr_status & StatsMax) {
2424 if (intr_status & IntrTxUnderrun) {
2425 if ((np->tx_config & TxDrthMask) < TX_DRTH_VAL_LIMIT) {
2426 np->tx_config += TX_DRTH_VAL_INC;
2427 if (netif_msg_tx_err(np))
2429 "%s: increased tx threshold, txcfg %#08x.\n",
2430 dev->name, np->tx_config);
2432 if (netif_msg_tx_err(np))
2434 "%s: tx underrun with maximum tx threshold, txcfg %#08x.\n",
2435 dev->name, np->tx_config);
2437 writel(np->tx_config, ioaddr + TxConfig);
2439 if (intr_status & WOLPkt && netif_msg_wol(np)) {
2440 int wol_status = readl(ioaddr + WOLCmd);
2441 printk(KERN_NOTICE "%s: Link wake-up event %#08x\n",
2442 dev->name, wol_status);
2444 if (intr_status & RxStatusFIFOOver) {
2445 if (netif_msg_rx_err(np) && netif_msg_intr(np)) {
2446 printk(KERN_NOTICE "%s: Rx status FIFO overrun\n",
2449 np->stats.rx_fifo_errors++;
2451 /* Hmmmmm, it's not clear how to recover from PCI faults. */
2452 if (intr_status & IntrPCIErr) {
2453 printk(KERN_NOTICE "%s: PCI error %#08x\n", dev->name,
2454 intr_status & IntrPCIErr);
2455 np->stats.tx_fifo_errors++;
2456 np->stats.rx_fifo_errors++;
2458 spin_unlock(&np->lock);
2461 static void __get_stats(struct net_device *dev)
2463 void __iomem * ioaddr = ns_ioaddr(dev);
2464 struct netdev_private *np = netdev_priv(dev);
2466 /* The chip only need report frame silently dropped. */
2467 np->stats.rx_crc_errors += readl(ioaddr + RxCRCErrs);
2468 np->stats.rx_missed_errors += readl(ioaddr + RxMissed);
2471 static struct net_device_stats *get_stats(struct net_device *dev)
2473 struct netdev_private *np = netdev_priv(dev);
2475 /* The chip only need report frame silently dropped. */
2476 spin_lock_irq(&np->lock);
2477 if (netif_running(dev) && !np->hands_off)
2479 spin_unlock_irq(&np->lock);
2484 #ifdef CONFIG_NET_POLL_CONTROLLER
2485 static void natsemi_poll_controller(struct net_device *dev)
2487 disable_irq(dev->irq);
2488 intr_handler(dev->irq, dev, NULL);
2489 enable_irq(dev->irq);
2493 #define HASH_TABLE 0x200
2494 static void __set_rx_mode(struct net_device *dev)
2496 void __iomem * ioaddr = ns_ioaddr(dev);
2497 struct netdev_private *np = netdev_priv(dev);
2498 u8 mc_filter[64]; /* Multicast hash filter */
2501 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2502 /* Unconditionally log net taps. */
2503 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
2505 rx_mode = RxFilterEnable | AcceptBroadcast
2506 | AcceptAllMulticast | AcceptAllPhys | AcceptMyPhys;
2507 } else if ((dev->mc_count > multicast_filter_limit)
2508 || (dev->flags & IFF_ALLMULTI)) {
2509 rx_mode = RxFilterEnable | AcceptBroadcast
2510 | AcceptAllMulticast | AcceptMyPhys;
2512 struct dev_mc_list *mclist;
2514 memset(mc_filter, 0, sizeof(mc_filter));
2515 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2516 i++, mclist = mclist->next) {
2517 int i = (ether_crc(ETH_ALEN, mclist->dmi_addr) >> 23) & 0x1ff;
2518 mc_filter[i/8] |= (1 << (i & 0x07));
2520 rx_mode = RxFilterEnable | AcceptBroadcast
2521 | AcceptMulticast | AcceptMyPhys;
2522 for (i = 0; i < 64; i += 2) {
2523 writel(HASH_TABLE + i, ioaddr + RxFilterAddr);
2524 writel((mc_filter[i + 1] << 8) + mc_filter[i],
2525 ioaddr + RxFilterData);
2528 writel(rx_mode, ioaddr + RxFilterAddr);
2529 np->cur_rx_mode = rx_mode;
2532 static int natsemi_change_mtu(struct net_device *dev, int new_mtu)
2534 if (new_mtu < 64 || new_mtu > NATSEMI_RX_LIMIT-NATSEMI_HEADERS)
2539 /* synchronized against open : rtnl_lock() held by caller */
2540 if (netif_running(dev)) {
2541 struct netdev_private *np = netdev_priv(dev);
2542 void __iomem * ioaddr = ns_ioaddr(dev);
2544 disable_irq(dev->irq);
2545 spin_lock(&np->lock);
2547 natsemi_stop_rxtx(dev);
2548 /* drain rx queue */
2550 /* change buffers */
2553 writel(np->ring_dma, ioaddr + RxRingPtr);
2554 /* restart engines */
2555 writel(RxOn | TxOn, ioaddr + ChipCmd);
2556 spin_unlock(&np->lock);
2557 enable_irq(dev->irq);
2562 static void set_rx_mode(struct net_device *dev)
2564 struct netdev_private *np = netdev_priv(dev);
2565 spin_lock_irq(&np->lock);
2568 spin_unlock_irq(&np->lock);
2571 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2573 struct netdev_private *np = netdev_priv(dev);
2574 strncpy(info->driver, DRV_NAME, ETHTOOL_BUSINFO_LEN);
2575 strncpy(info->version, DRV_VERSION, ETHTOOL_BUSINFO_LEN);
2576 strncpy(info->bus_info, pci_name(np->pci_dev), ETHTOOL_BUSINFO_LEN);
2579 static int get_regs_len(struct net_device *dev)
2581 return NATSEMI_REGS_SIZE;
2584 static int get_eeprom_len(struct net_device *dev)
2586 struct netdev_private *np = netdev_priv(dev);
2587 return np->eeprom_size;
2590 static int get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2592 struct netdev_private *np = netdev_priv(dev);
2593 spin_lock_irq(&np->lock);
2594 netdev_get_ecmd(dev, ecmd);
2595 spin_unlock_irq(&np->lock);
2599 static int set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2601 struct netdev_private *np = netdev_priv(dev);
2603 spin_lock_irq(&np->lock);
2604 res = netdev_set_ecmd(dev, ecmd);
2605 spin_unlock_irq(&np->lock);
2609 static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2611 struct netdev_private *np = netdev_priv(dev);
2612 spin_lock_irq(&np->lock);
2613 netdev_get_wol(dev, &wol->supported, &wol->wolopts);
2614 netdev_get_sopass(dev, wol->sopass);
2615 spin_unlock_irq(&np->lock);
2618 static int set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2620 struct netdev_private *np = netdev_priv(dev);
2622 spin_lock_irq(&np->lock);
2623 netdev_set_wol(dev, wol->wolopts);
2624 res = netdev_set_sopass(dev, wol->sopass);
2625 spin_unlock_irq(&np->lock);
2629 static void get_regs(struct net_device *dev, struct ethtool_regs *regs, void *buf)
2631 struct netdev_private *np = netdev_priv(dev);
2632 regs->version = NATSEMI_REGS_VER;
2633 spin_lock_irq(&np->lock);
2634 netdev_get_regs(dev, buf);
2635 spin_unlock_irq(&np->lock);
2638 static u32 get_msglevel(struct net_device *dev)
2640 struct netdev_private *np = netdev_priv(dev);
2641 return np->msg_enable;
2644 static void set_msglevel(struct net_device *dev, u32 val)
2646 struct netdev_private *np = netdev_priv(dev);
2647 np->msg_enable = val;
2650 static int nway_reset(struct net_device *dev)
2654 /* if autoneg is off, it's an error */
2655 tmp = mdio_read(dev, MII_BMCR);
2656 if (tmp & BMCR_ANENABLE) {
2657 tmp |= (BMCR_ANRESTART);
2658 mdio_write(dev, MII_BMCR, tmp);
2664 static u32 get_link(struct net_device *dev)
2666 /* LSTATUS is latched low until a read - so read twice */
2667 mdio_read(dev, MII_BMSR);
2668 return (mdio_read(dev, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
2671 static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
2673 struct netdev_private *np = netdev_priv(dev);
2677 eebuf = kmalloc(np->eeprom_size, GFP_KERNEL);
2681 eeprom->magic = PCI_VENDOR_ID_NS | (PCI_DEVICE_ID_NS_83815<<16);
2682 spin_lock_irq(&np->lock);
2683 res = netdev_get_eeprom(dev, eebuf);
2684 spin_unlock_irq(&np->lock);
2686 memcpy(data, eebuf+eeprom->offset, eeprom->len);
2691 static struct ethtool_ops ethtool_ops = {
2692 .get_drvinfo = get_drvinfo,
2693 .get_regs_len = get_regs_len,
2694 .get_eeprom_len = get_eeprom_len,
2695 .get_settings = get_settings,
2696 .set_settings = set_settings,
2699 .get_regs = get_regs,
2700 .get_msglevel = get_msglevel,
2701 .set_msglevel = set_msglevel,
2702 .nway_reset = nway_reset,
2703 .get_link = get_link,
2704 .get_eeprom = get_eeprom,
2707 static int netdev_set_wol(struct net_device *dev, u32 newval)
2709 struct netdev_private *np = netdev_priv(dev);
2710 void __iomem * ioaddr = ns_ioaddr(dev);
2711 u32 data = readl(ioaddr + WOLCmd) & ~WakeOptsSummary;
2713 /* translate to bitmasks this chip understands */
2714 if (newval & WAKE_PHY)
2716 if (newval & WAKE_UCAST)
2717 data |= WakeUnicast;
2718 if (newval & WAKE_MCAST)
2719 data |= WakeMulticast;
2720 if (newval & WAKE_BCAST)
2721 data |= WakeBroadcast;
2722 if (newval & WAKE_ARP)
2724 if (newval & WAKE_MAGIC)
2726 if (np->srr >= SRR_DP83815_D) {
2727 if (newval & WAKE_MAGICSECURE) {
2728 data |= WakeMagicSecure;
2732 writel(data, ioaddr + WOLCmd);
2737 static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur)
2739 struct netdev_private *np = netdev_priv(dev);
2740 void __iomem * ioaddr = ns_ioaddr(dev);
2741 u32 regval = readl(ioaddr + WOLCmd);
2743 *supported = (WAKE_PHY | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST
2744 | WAKE_ARP | WAKE_MAGIC);
2746 if (np->srr >= SRR_DP83815_D) {
2747 /* SOPASS works on revD and higher */
2748 *supported |= WAKE_MAGICSECURE;
2752 /* translate from chip bitmasks */
2753 if (regval & WakePhy)
2755 if (regval & WakeUnicast)
2757 if (regval & WakeMulticast)
2759 if (regval & WakeBroadcast)
2761 if (regval & WakeArp)
2763 if (regval & WakeMagic)
2765 if (regval & WakeMagicSecure) {
2766 /* this can be on in revC, but it's broken */
2767 *cur |= WAKE_MAGICSECURE;
2773 static int netdev_set_sopass(struct net_device *dev, u8 *newval)
2775 struct netdev_private *np = netdev_priv(dev);
2776 void __iomem * ioaddr = ns_ioaddr(dev);
2777 u16 *sval = (u16 *)newval;
2780 if (np->srr < SRR_DP83815_D) {
2784 /* enable writing to these registers by disabling the RX filter */
2785 addr = readl(ioaddr + RxFilterAddr) & ~RFCRAddressMask;
2786 addr &= ~RxFilterEnable;
2787 writel(addr, ioaddr + RxFilterAddr);
2789 /* write the three words to (undocumented) RFCR vals 0xa, 0xc, 0xe */
2790 writel(addr | 0xa, ioaddr + RxFilterAddr);
2791 writew(sval[0], ioaddr + RxFilterData);
2793 writel(addr | 0xc, ioaddr + RxFilterAddr);
2794 writew(sval[1], ioaddr + RxFilterData);
2796 writel(addr | 0xe, ioaddr + RxFilterAddr);
2797 writew(sval[2], ioaddr + RxFilterData);
2799 /* re-enable the RX filter */
2800 writel(addr | RxFilterEnable, ioaddr + RxFilterAddr);
2805 static int netdev_get_sopass(struct net_device *dev, u8 *data)
2807 struct netdev_private *np = netdev_priv(dev);
2808 void __iomem * ioaddr = ns_ioaddr(dev);
2809 u16 *sval = (u16 *)data;
2812 if (np->srr < SRR_DP83815_D) {
2813 sval[0] = sval[1] = sval[2] = 0;
2817 /* read the three words from (undocumented) RFCR vals 0xa, 0xc, 0xe */
2818 addr = readl(ioaddr + RxFilterAddr) & ~RFCRAddressMask;
2820 writel(addr | 0xa, ioaddr + RxFilterAddr);
2821 sval[0] = readw(ioaddr + RxFilterData);
2823 writel(addr | 0xc, ioaddr + RxFilterAddr);
2824 sval[1] = readw(ioaddr + RxFilterData);
2826 writel(addr | 0xe, ioaddr + RxFilterAddr);
2827 sval[2] = readw(ioaddr + RxFilterData);
2829 writel(addr, ioaddr + RxFilterAddr);
2834 static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2836 struct netdev_private *np = netdev_priv(dev);
2839 ecmd->port = dev->if_port;
2840 ecmd->speed = np->speed;
2841 ecmd->duplex = np->duplex;
2842 ecmd->autoneg = np->autoneg;
2843 ecmd->advertising = 0;
2844 if (np->advertising & ADVERTISE_10HALF)
2845 ecmd->advertising |= ADVERTISED_10baseT_Half;
2846 if (np->advertising & ADVERTISE_10FULL)
2847 ecmd->advertising |= ADVERTISED_10baseT_Full;
2848 if (np->advertising & ADVERTISE_100HALF)
2849 ecmd->advertising |= ADVERTISED_100baseT_Half;
2850 if (np->advertising & ADVERTISE_100FULL)
2851 ecmd->advertising |= ADVERTISED_100baseT_Full;
2852 ecmd->supported = (SUPPORTED_Autoneg |
2853 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2854 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2855 SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE);
2856 ecmd->phy_address = np->phy_addr_external;
2858 * We intentionally report the phy address of the external
2859 * phy, even if the internal phy is used. This is necessary
2860 * to work around a deficiency of the ethtool interface:
2861 * It's only possible to query the settings of the active
2863 * # ethtool -s ethX port mii
2864 * actually sends an ioctl to switch to port mii with the
2865 * settings that are used for the current active port.
2866 * If we would report a different phy address in this
2868 * # ethtool -s ethX port tp;ethtool -s ethX port mii
2869 * would unintentionally change the phy address.
2871 * Fortunately the phy address doesn't matter with the
2875 /* set information based on active port type */
2876 switch (ecmd->port) {
2879 ecmd->advertising |= ADVERTISED_TP;
2880 ecmd->transceiver = XCVR_INTERNAL;
2883 ecmd->advertising |= ADVERTISED_MII;
2884 ecmd->transceiver = XCVR_EXTERNAL;
2887 ecmd->advertising |= ADVERTISED_FIBRE;
2888 ecmd->transceiver = XCVR_EXTERNAL;
2892 /* if autonegotiation is on, try to return the active speed/duplex */
2893 if (ecmd->autoneg == AUTONEG_ENABLE) {
2894 ecmd->advertising |= ADVERTISED_Autoneg;
2895 tmp = mii_nway_result(
2896 np->advertising & mdio_read(dev, MII_LPA));
2897 if (tmp == LPA_100FULL || tmp == LPA_100HALF)
2898 ecmd->speed = SPEED_100;
2900 ecmd->speed = SPEED_10;
2901 if (tmp == LPA_100FULL || tmp == LPA_10FULL)
2902 ecmd->duplex = DUPLEX_FULL;
2904 ecmd->duplex = DUPLEX_HALF;
2907 /* ignore maxtxpkt, maxrxpkt for now */
2912 static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2914 struct netdev_private *np = netdev_priv(dev);
2916 if (ecmd->port != PORT_TP && ecmd->port != PORT_MII && ecmd->port != PORT_FIBRE)
2918 if (ecmd->transceiver != XCVR_INTERNAL && ecmd->transceiver != XCVR_EXTERNAL)
2920 if (ecmd->autoneg == AUTONEG_ENABLE) {
2921 if ((ecmd->advertising & (ADVERTISED_10baseT_Half |
2922 ADVERTISED_10baseT_Full |
2923 ADVERTISED_100baseT_Half |
2924 ADVERTISED_100baseT_Full)) == 0) {
2927 } else if (ecmd->autoneg == AUTONEG_DISABLE) {
2928 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
2930 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
2937 * maxtxpkt, maxrxpkt: ignored for now.
2940 * PORT_TP is always XCVR_INTERNAL, PORT_MII and PORT_FIBRE are always
2941 * XCVR_EXTERNAL. The implementation thus ignores ecmd->transceiver and
2942 * selects based on ecmd->port.
2944 * Actually PORT_FIBRE is nearly identical to PORT_MII: it's for fibre
2945 * phys that are connected to the mii bus. It's used to apply fibre
2949 /* WHEW! now lets bang some bits */
2951 /* save the parms */
2952 dev->if_port = ecmd->port;
2953 np->autoneg = ecmd->autoneg;
2954 np->phy_addr_external = ecmd->phy_address & PhyAddrMask;
2955 if (np->autoneg == AUTONEG_ENABLE) {
2956 /* advertise only what has been requested */
2957 np->advertising &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
2958 if (ecmd->advertising & ADVERTISED_10baseT_Half)
2959 np->advertising |= ADVERTISE_10HALF;
2960 if (ecmd->advertising & ADVERTISED_10baseT_Full)
2961 np->advertising |= ADVERTISE_10FULL;
2962 if (ecmd->advertising & ADVERTISED_100baseT_Half)
2963 np->advertising |= ADVERTISE_100HALF;
2964 if (ecmd->advertising & ADVERTISED_100baseT_Full)
2965 np->advertising |= ADVERTISE_100FULL;
2967 np->speed = ecmd->speed;
2968 np->duplex = ecmd->duplex;
2969 /* user overriding the initial full duplex parm? */
2970 if (np->duplex == DUPLEX_HALF)
2971 np->full_duplex = 0;
2974 /* get the right phy enabled */
2975 if (ecmd->port == PORT_TP)
2976 switch_port_internal(dev);
2978 switch_port_external(dev);
2980 /* set parms and see how this affected our link status */
2981 init_phy_fixup(dev);
2986 static int netdev_get_regs(struct net_device *dev, u8 *buf)
2991 u32 *rbuf = (u32 *)buf;
2992 void __iomem * ioaddr = ns_ioaddr(dev);
2994 /* read non-mii page 0 of registers */
2995 for (i = 0; i < NATSEMI_PG0_NREGS/2; i++) {
2996 rbuf[i] = readl(ioaddr + i*4);
2999 /* read current mii registers */
3000 for (i = NATSEMI_PG0_NREGS/2; i < NATSEMI_PG0_NREGS; i++)
3001 rbuf[i] = mdio_read(dev, i & 0x1f);
3003 /* read only the 'magic' registers from page 1 */
3004 writew(1, ioaddr + PGSEL);
3005 rbuf[i++] = readw(ioaddr + PMDCSR);
3006 rbuf[i++] = readw(ioaddr + TSTDAT);
3007 rbuf[i++] = readw(ioaddr + DSPCFG);
3008 rbuf[i++] = readw(ioaddr + SDCFG);
3009 writew(0, ioaddr + PGSEL);
3011 /* read RFCR indexed registers */
3012 rfcr = readl(ioaddr + RxFilterAddr);
3013 for (j = 0; j < NATSEMI_RFDR_NREGS; j++) {
3014 writel(j*2, ioaddr + RxFilterAddr);
3015 rbuf[i++] = readw(ioaddr + RxFilterData);
3017 writel(rfcr, ioaddr + RxFilterAddr);
3019 /* the interrupt status is clear-on-read - see if we missed any */
3020 if (rbuf[4] & rbuf[5]) {
3022 "%s: shoot, we dropped an interrupt (%#08x)\n",
3023 dev->name, rbuf[4] & rbuf[5]);
3029 #define SWAP_BITS(x) ( (((x) & 0x0001) << 15) | (((x) & 0x0002) << 13) \
3030 | (((x) & 0x0004) << 11) | (((x) & 0x0008) << 9) \
3031 | (((x) & 0x0010) << 7) | (((x) & 0x0020) << 5) \
3032 | (((x) & 0x0040) << 3) | (((x) & 0x0080) << 1) \
3033 | (((x) & 0x0100) >> 1) | (((x) & 0x0200) >> 3) \
3034 | (((x) & 0x0400) >> 5) | (((x) & 0x0800) >> 7) \
3035 | (((x) & 0x1000) >> 9) | (((x) & 0x2000) >> 11) \
3036 | (((x) & 0x4000) >> 13) | (((x) & 0x8000) >> 15) )
3038 static int netdev_get_eeprom(struct net_device *dev, u8 *buf)
3041 u16 *ebuf = (u16 *)buf;
3042 void __iomem * ioaddr = ns_ioaddr(dev);
3043 struct netdev_private *np = netdev_priv(dev);
3045 /* eeprom_read reads 16 bits, and indexes by 16 bits */
3046 for (i = 0; i < np->eeprom_size/2; i++) {
3047 ebuf[i] = eeprom_read(ioaddr, i);
3048 /* The EEPROM itself stores data bit-swapped, but eeprom_read
3049 * reads it back "sanely". So we swap it back here in order to
3050 * present it to userland as it is stored. */
3051 ebuf[i] = SWAP_BITS(ebuf[i]);
3056 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3058 struct mii_ioctl_data *data = if_mii(rq);
3059 struct netdev_private *np = netdev_priv(dev);
3062 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
3063 case SIOCDEVPRIVATE: /* for binary compat, remove in 2.5 */
3064 data->phy_id = np->phy_addr_external;
3067 case SIOCGMIIREG: /* Read MII PHY register. */
3068 case SIOCDEVPRIVATE+1: /* for binary compat, remove in 2.5 */
3069 /* The phy_id is not enough to uniquely identify
3070 * the intended target. Therefore the command is sent to
3071 * the given mii on the current port.
3073 if (dev->if_port == PORT_TP) {
3074 if ((data->phy_id & 0x1f) == np->phy_addr_external)
3075 data->val_out = mdio_read(dev,
3076 data->reg_num & 0x1f);
3080 move_int_phy(dev, data->phy_id & 0x1f);
3081 data->val_out = miiport_read(dev, data->phy_id & 0x1f,
3082 data->reg_num & 0x1f);
3086 case SIOCSMIIREG: /* Write MII PHY register. */
3087 case SIOCDEVPRIVATE+2: /* for binary compat, remove in 2.5 */
3088 if (!capable(CAP_NET_ADMIN))
3090 if (dev->if_port == PORT_TP) {
3091 if ((data->phy_id & 0x1f) == np->phy_addr_external) {
3092 if ((data->reg_num & 0x1f) == MII_ADVERTISE)
3093 np->advertising = data->val_in;
3094 mdio_write(dev, data->reg_num & 0x1f,
3098 if ((data->phy_id & 0x1f) == np->phy_addr_external) {
3099 if ((data->reg_num & 0x1f) == MII_ADVERTISE)
3100 np->advertising = data->val_in;
3102 move_int_phy(dev, data->phy_id & 0x1f);
3103 miiport_write(dev, data->phy_id & 0x1f,
3104 data->reg_num & 0x1f,
3113 static void enable_wol_mode(struct net_device *dev, int enable_intr)
3115 void __iomem * ioaddr = ns_ioaddr(dev);
3116 struct netdev_private *np = netdev_priv(dev);
3118 if (netif_msg_wol(np))
3119 printk(KERN_INFO "%s: remaining active for wake-on-lan\n",
3122 /* For WOL we must restart the rx process in silent mode.
3123 * Write NULL to the RxRingPtr. Only possible if
3124 * rx process is stopped
3126 writel(0, ioaddr + RxRingPtr);
3128 /* read WoL status to clear */
3129 readl(ioaddr + WOLCmd);
3131 /* PME on, clear status */
3132 writel(np->SavedClkRun | PMEEnable | PMEStatus, ioaddr + ClkRun);
3134 /* and restart the rx process */
3135 writel(RxOn, ioaddr + ChipCmd);
3138 /* enable the WOL interrupt.
3139 * Could be used to send a netlink message.
3141 writel(WOLPkt | LinkChange, ioaddr + IntrMask);
3142 writel(1, ioaddr + IntrEnable);
3146 static int netdev_close(struct net_device *dev)
3148 void __iomem * ioaddr = ns_ioaddr(dev);
3149 struct netdev_private *np = netdev_priv(dev);
3151 if (netif_msg_ifdown(np))
3153 "%s: Shutting down ethercard, status was %#04x.\n",
3154 dev->name, (int)readl(ioaddr + ChipCmd));
3155 if (netif_msg_pktdata(np))
3157 "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
3158 dev->name, np->cur_tx, np->dirty_tx,
3159 np->cur_rx, np->dirty_rx);
3162 * FIXME: what if someone tries to close a device
3163 * that is suspended?
3164 * Should we reenable the nic to switch to
3165 * the final WOL settings?
3168 del_timer_sync(&np->timer);
3169 disable_irq(dev->irq);
3170 spin_lock_irq(&np->lock);
3171 natsemi_irq_disable(dev);
3173 spin_unlock_irq(&np->lock);
3174 enable_irq(dev->irq);
3176 free_irq(dev->irq, dev);
3178 /* Interrupt disabled, interrupt handler released,
3179 * queue stopped, timer deleted, rtnl_lock held
3180 * All async codepaths that access the driver are disabled.
3182 spin_lock_irq(&np->lock);
3184 readl(ioaddr + IntrMask);
3185 readw(ioaddr + MIntrStatus);
3188 writel(StatsFreeze, ioaddr + StatsCtrl);
3190 /* Stop the chip's Tx and Rx processes. */
3191 natsemi_stop_rxtx(dev);
3194 spin_unlock_irq(&np->lock);
3196 /* clear the carrier last - an interrupt could reenable it otherwise */
3197 netif_carrier_off(dev);
3198 netif_stop_queue(dev);
3205 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
3207 /* restart the NIC in WOL mode.
3208 * The nic must be stopped for this.
3210 enable_wol_mode(dev, 0);
3212 /* Restore PME enable bit unmolested */
3213 writel(np->SavedClkRun, ioaddr + ClkRun);
3220 static void __devexit natsemi_remove1 (struct pci_dev *pdev)
3222 struct net_device *dev = pci_get_drvdata(pdev);
3223 void __iomem * ioaddr = ns_ioaddr(dev);
3225 unregister_netdev (dev);
3226 pci_release_regions (pdev);
3229 pci_set_drvdata(pdev, NULL);
3235 * The ns83815 chip doesn't have explicit RxStop bits.
3236 * Kicking the Rx or Tx process for a new packet reenables the Rx process
3237 * of the nic, thus this function must be very careful:
3239 * suspend/resume synchronization:
3241 * netdev_open, netdev_close, netdev_ioctl, set_rx_mode, intr_handler,
3242 * start_tx, tx_timeout
3244 * No function accesses the hardware without checking np->hands_off.
3245 * the check occurs under spin_lock_irq(&np->lock);
3247 * * netdev_ioctl: noncritical access.
3248 * * netdev_open: cannot happen due to the device_detach
3249 * * netdev_close: doesn't hurt.
3250 * * netdev_timer: timer stopped by natsemi_suspend.
3251 * * intr_handler: doesn't acquire the spinlock. suspend calls
3252 * disable_irq() to enforce synchronization.
3253 * * natsemi_poll: checks before reenabling interrupts. suspend
3254 * sets hands_off, disables interrupts and then waits with
3255 * netif_poll_disable().
3257 * Interrupts must be disabled, otherwise hands_off can cause irq storms.
3260 static int natsemi_suspend (struct pci_dev *pdev, pm_message_t state)
3262 struct net_device *dev = pci_get_drvdata (pdev);
3263 struct netdev_private *np = netdev_priv(dev);
3264 void __iomem * ioaddr = ns_ioaddr(dev);
3267 if (netif_running (dev)) {
3268 del_timer_sync(&np->timer);
3270 disable_irq(dev->irq);
3271 spin_lock_irq(&np->lock);
3273 writel(0, ioaddr + IntrEnable);
3275 natsemi_stop_rxtx(dev);
3276 netif_stop_queue(dev);
3278 spin_unlock_irq(&np->lock);
3279 enable_irq(dev->irq);
3281 netif_poll_disable(dev);
3283 /* Update the error counts. */
3286 /* pci_power_off(pdev, -1); */
3289 u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
3290 /* Restore PME enable bit */
3292 /* restart the NIC in WOL mode.
3293 * The nic must be stopped for this.
3294 * FIXME: use the WOL interrupt
3296 enable_wol_mode(dev, 0);
3298 /* Restore PME enable bit unmolested */
3299 writel(np->SavedClkRun, ioaddr + ClkRun);
3303 netif_device_detach(dev);
3309 static int natsemi_resume (struct pci_dev *pdev)
3311 struct net_device *dev = pci_get_drvdata (pdev);
3312 struct netdev_private *np = netdev_priv(dev);
3315 if (netif_device_present(dev))
3317 if (netif_running(dev)) {
3318 BUG_ON(!np->hands_off);
3319 pci_enable_device(pdev);
3320 /* pci_power_on(pdev); */
3324 disable_irq(dev->irq);
3325 spin_lock_irq(&np->lock);
3327 init_registers(dev);
3328 netif_device_attach(dev);
3329 spin_unlock_irq(&np->lock);
3330 enable_irq(dev->irq);
3332 mod_timer(&np->timer, jiffies + 1*HZ);
3334 netif_device_attach(dev);
3335 netif_poll_enable(dev);
3341 #endif /* CONFIG_PM */
3343 static struct pci_driver natsemi_driver = {
3345 .id_table = natsemi_pci_tbl,
3346 .probe = natsemi_probe1,
3347 .remove = __devexit_p(natsemi_remove1),
3349 .suspend = natsemi_suspend,
3350 .resume = natsemi_resume,
3354 static int __init natsemi_init_mod (void)
3356 /* when a module, this is printed whether or not devices are found in probe */
3361 return pci_module_init (&natsemi_driver);
3364 static void __exit natsemi_exit_mod (void)
3366 pci_unregister_driver (&natsemi_driver);
3369 module_init(natsemi_init_mod);
3370 module_exit(natsemi_exit_mod);