linux-bk/drivers/net/natsemi.c
<<
>>
Prefs
   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)
   6
   7        This software may be used and distributed according to the terms of
   8        the GNU General Public License (GPL), incorporated herein by reference.
   9        Drivers based on or derived from this code fall under the GPL and must
  10        retain the authorship, copyright and license notice.  This file is not
  11        a complete program and may only be used when the entire operating
  12        system is licensed under the GPL.  License for under other terms may be
  13        available.  Contact the original author for details.
  14
  15        The original author may be reached as becker@scyld.com, or at
  16        Scyld Computing Corporation
  17        410 Severn Ave., Suite 210
  18        Annapolis MD 21403
  19
  20        Support information and updates available at
  21        http://www.scyld.com/network/netsemi.html
  22
  23
  24        Linux kernel modifications:
  25
  26        Version 1.0.1:
  27                - Spinlock fixes
  28                - Bug fixes and better intr performance (Tjeerd)
  29        Version 1.0.2:
  30                - Now reads correct MAC address from eeprom
  31        Version 1.0.3:
  32                - Eliminate redundant priv->tx_full flag
  33                - Call netif_start_queue from dev->tx_timeout
  34                - wmb() in start_tx() to flush data
  35                - Update Tx locking
  36                - Clean up PCI enable (davej)
  37        Version 1.0.4:
  38                - Merge Donald Becker's natsemi.c version 1.07
  39        Version 1.0.5:
  40                - { fill me in }
  41        Version 1.0.6:
  42                * ethtool support (jgarzik)
  43                * Proper initialization of the card (which sometimes
  44                fails to occur and leaves the card in a non-functional
  45                state). (uzi)
  46
  47                * Some documented register settings to optimize some
  48                of the 100Mbit autodetection circuitry in rev C cards. (uzi)
  49
  50                * Polling of the PHY intr for stuff like link state
  51                change and auto- negotiation to finally work properly. (uzi)
  52
  53                * One-liner removal of a duplicate declaration of
  54                netdev_error(). (uzi)
  55
  56        Version 1.0.7: (Manfred Spraul)
  57                * pci dma
  58                * SMP locking update
  59                * full reset added into tx_timeout
  60                * correct multicast hash generation (both big and little endian)
  61                        [copied from a natsemi driver version
  62                         from Myrio Corporation, Greg Smith]
  63                * suspend/resume
  64
  65        version 1.0.8 (Tim Hockin <thockin@sun.com>)
  66                * ETHTOOL_* support
  67                * Wake on lan support (Erik Gilling)
  68                * MXDMA fixes for serverworks
  69                * EEPROM reload
  70
  71        version 1.0.9 (Manfred Spraul)
  72                * Main change: fix lack of synchronize
  73                netif_close/netif_suspend against a last interrupt
  74                or packet.
  75                * do not enable superflous interrupts (e.g. the
  76                drivers relies on TxDone - TxIntr not needed)
  77                * wait that the hardware has really stopped in close
  78                and suspend.
  79                * workaround for the (at least) gcc-2.95.1 compiler
  80                problem. Also simplifies the code a bit.
  81                * disable_irq() in tx_timeout - needed to protect
  82                against rx interrupts.
  83                * stop the nic before switching into silent rx mode
  84                for wol (required according to docu).
  85
  86        version 1.0.10:
  87                * use long for ee_addr (various)
  88                * print pointers properly (DaveM)
  89                * include asm/irq.h (?)
  90
  91        version 1.0.11:
  92                * check and reset if PHY errors appear (Adrian Sun)
  93                * WoL cleanup (Tim Hockin)
  94                * Magic number cleanup (Tim Hockin)
  95                * Don't reload EEPROM on every reset (Tim Hockin)
  96                * Save and restore EEPROM state across reset (Tim Hockin)
  97                * MDIO Cleanup (Tim Hockin)
  98                * Reformat register offsets/bits (jgarzik)
  99
 100        version 1.0.12:
 101                * ETHTOOL_* further support (Tim Hockin)
 102
 103        version 1.0.13:
 104                * ETHTOOL_[G]EEPROM support (Tim Hockin)
 105
 106        version 1.0.13:
 107                * crc cleanup (Matt Domsch <Matt_Domsch@dell.com>)
 108
 109        version 1.0.14:
 110                * Cleanup some messages and autoneg in ethtool (Tim Hockin)
 111
 112        version 1.0.15:
 113                * Get rid of cable_magic flag
 114                * use new (National provided) solution for cable magic issue
 115
 116        version 1.0.16:
 117                * call netdev_rx() for RxErrors (Manfred Spraul)
 118                * formatting and cleanups
 119                * change options and full_duplex arrays to be zero
 120                  initialized
 121                * enable only the WoL and PHY interrupts in wol mode
 122
 123        version 1.0.17:
 124                * only do cable_magic on 83815 and early 83816 (Tim Hockin)
 125                * create a function for rx refill (Manfred Spraul)
 126                * combine drain_ring and init_ring (Manfred Spraul)
 127                * oom handling (Manfred Spraul)
 128                * hands_off instead of playing with netif_device_{de,a}ttach
 129                  (Manfred Spraul)
 130                * be sure to write the MAC back to the chip (Manfred Spraul)
 131                * lengthen EEPROM timeout, and always warn about timeouts
 132                  (Manfred Spraul)
 133                * comments update (Manfred)
 134                * do the right thing on a phy-reset (Manfred and Tim)
 135
 136        TODO:
 137        * big endian support with CFG:BEM instead of cpu_to_le32
 138        * support for an external PHY
 139        * NAPI
 140*/
 141
 142#if !defined(__OPTIMIZE__)
 143#warning  You must compile this file with the correct options!
 144#warning  See the last lines of the source file.
 145#error You must compile this driver with "-O".
 146#endif
 147
 148#include <linux/config.h>
 149#include <linux/module.h>
 150#include <linux/kernel.h>
 151#include <linux/string.h>
 152#include <linux/timer.h>
 153#include <linux/errno.h>
 154#include <linux/ioport.h>
 155#include <linux/slab.h>
 156#include <linux/interrupt.h>
 157#include <linux/pci.h>
 158#include <linux/netdevice.h>
 159#include <linux/etherdevice.h>
 160#include <linux/skbuff.h>
 161#include <linux/init.h>
 162#include <linux/spinlock.h>
 163#include <linux/ethtool.h>
 164#include <linux/delay.h>
 165#include <linux/rtnetlink.h>
 166#include <linux/mii.h>
 167#include <linux/crc32.h>
 168#include <asm/processor.h>      /* Processor type for cache alignment. */
 169#include <asm/bitops.h>
 170#include <asm/io.h>
 171#include <asm/irq.h>
 172#include <asm/uaccess.h>
 173
 174#define DRV_NAME        "natsemi"
 175#define DRV_VERSION     "1.07+LK1.0.17"
 176#define DRV_RELDATE     "Sep 27, 2002"
 177
 178/* Updated to recommendations in pci-skeleton v2.03. */
 179
 180/* The user-configurable values.
 181   These may be modified when a driver module is loaded.*/
 182
 183#define NATSEMI_DEF_MSG         (NETIF_MSG_DRV          | \
 184                                 NETIF_MSG_LINK         | \
 185                                 NETIF_MSG_WOL          | \
 186                                 NETIF_MSG_RX_ERR       | \
 187                                 NETIF_MSG_TX_ERR)
 188static int debug = -1;
 189
 190/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
 191static int max_interrupt_work = 20;
 192static int mtu;
 193
 194/* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
 195   This chip uses a 512 element hash table based on the Ethernet CRC.  */
 196static int multicast_filter_limit = 100;
 197
 198/* Set the copy breakpoint for the copy-only-tiny-frames scheme.
 199   Setting to > 1518 effectively disables this feature. */
 200static int rx_copybreak;
 201
 202/* Used to pass the media type, etc.
 203   Both 'options[]' and 'full_duplex[]' should exist for driver
 204   interoperability.
 205   The media type is usually passed in 'options[]'.
 206*/
 207#define MAX_UNITS 8             /* More are supported, limit only on options */
 208static int options[MAX_UNITS];
 209static int full_duplex[MAX_UNITS];
 210
 211/* Operational parameters that are set at compile time. */
 212
 213/* Keep the ring sizes a power of two for compile efficiency.
 214   The compiler will convert <unsigned>'%'<2^N> into a bit mask.
 215   Making the Tx ring too large decreases the effectiveness of channel
 216   bonding and packet priority.
 217   There are no ill effects from too-large receive rings. */
 218#define TX_RING_SIZE    16
 219#define TX_QUEUE_LEN    10 /* Limit ring entries actually used, min 4. */
 220#define RX_RING_SIZE    32
 221
 222/* Operational parameters that usually are not changed. */
 223/* Time in jiffies before concluding the transmitter is hung. */
 224#define TX_TIMEOUT  (2*HZ)
 225
 226#define NATSEMI_HW_TIMEOUT      400
 227#define NATSEMI_TIMER_FREQ      3*HZ
 228#define NATSEMI_PG0_NREGS       64
 229#define NATSEMI_RFDR_NREGS      8
 230#define NATSEMI_PG1_NREGS       4
 231#define NATSEMI_NREGS           (NATSEMI_PG0_NREGS + NATSEMI_RFDR_NREGS + \
 232                                 NATSEMI_PG1_NREGS)
 233#define NATSEMI_REGS_VER        1 /* v1 added RFDR registers */
 234#define NATSEMI_REGS_SIZE       (NATSEMI_NREGS * sizeof(u32))
 235#define NATSEMI_EEPROM_SIZE     24 /* 12 16-bit values */
 236
 237#define PKT_BUF_SZ              1536 /* Size of each temporary Rx buffer. */
 238
 239/* These identify the driver base version and may not be removed. */
 240static 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";
 246
 247MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
 248MODULE_DESCRIPTION("National Semiconductor DP8381x series PCI Ethernet driver");
 249MODULE_LICENSE("GPL");
 250
 251MODULE_PARM(max_interrupt_work, "i");
 252MODULE_PARM(mtu, "i");
 253MODULE_PARM(debug, "i");
 254MODULE_PARM(rx_copybreak, "i");
 255MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
 256MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
 257MODULE_PARM_DESC(max_interrupt_work, 
 258        "DP8381x maximum events handled per interrupt");
 259MODULE_PARM_DESC(mtu, "DP8381x MTU (all boards)");
 260MODULE_PARM_DESC(debug, "DP8381x default debug level");
 261MODULE_PARM_DESC(rx_copybreak, 
 262        "DP8381x copy breakpoint for copy-only-tiny-frames");
 263MODULE_PARM_DESC(options, 
 264        "DP8381x: Bits 0-3: media type, bit 17: full duplex");
 265MODULE_PARM_DESC(full_duplex, "DP8381x full duplex setting(s) (1)");
 266
 267/*
 268                                Theory of Operation
 269
 270I. Board Compatibility
 271
 272This driver is designed for National Semiconductor DP83815 PCI Ethernet NIC.
 273It also works with other chips in in the DP83810 series.
 274
 275II. Board-specific settings
 276
 277This driver requires the PCI interrupt line to be valid.
 278It honors the EEPROM-set values.
 279
 280III. Driver operation
 281
 282IIIa. Ring buffers
 283
 284This driver uses two statically allocated fixed-size descriptor lists
 285formed into rings by a branch from the final descriptor to the beginning of
 286the list.  The ring sizes are set at compile time by RX/TX_RING_SIZE.
 287The NatSemi design uses a 'next descriptor' pointer that the driver forms
 288into a list.
 289
 290IIIb/c. Transmit/Receive Structure
 291
 292This driver uses a zero-copy receive and transmit scheme.
 293The driver allocates full frame size skbuffs for the Rx ring buffers at
 294open() time and passes the skb->data field to the chip as receive data
 295buffers.  When an incoming frame is less than RX_COPYBREAK bytes long,
 296a fresh skbuff is allocated and the frame is copied to the new skbuff.
 297When the incoming frame is larger, the skbuff is passed directly up the
 298protocol stack.  Buffers consumed this way are replaced by newly allocated
 299skbuffs in a later phase of receives.
 300
 301The RX_COPYBREAK value is chosen to trade-off the memory wasted by
 302using a full-sized skbuff for small frames vs. the copying costs of larger
 303frames.  New boards are typically used in generously configured machines
 304and the underfilled buffers have negligible impact compared to the benefit of
 305a single allocation size, so the default value of zero results in never
 306copying packets.  When copying is done, the cost is usually mitigated by using
 307a combined copy/checksum routine.  Copying also preloads the cache, which is
 308most useful with small frames.
 309
 310A subtle aspect of the operation is that unaligned buffers are not permitted
 311by the hardware.  Thus the IP header at offset 14 in an ethernet frame isn't
 312longword aligned for further processing.  On copies frames are put into the
 313skbuff at an offset of "+2", 16-byte aligning the IP header.
 314
 315IIId. Synchronization
 316
 317Most operations are synchronized on the np->lock irq spinlock, except the
 318performance critical codepaths:
 319
 320The rx process only runs in the interrupt handler. Access from outside
 321the interrupt handler is only permitted after disable_irq().
 322
 323The rx process usually runs under the dev->xmit_lock. If np->intr_tx_reap
 324is set, then access is permitted under spin_lock_irq(&np->lock).
 325
 326Thus configuration functions that want to access everything must call
 327        disable_irq(dev->irq);
 328        spin_lock_bh(dev->xmit_lock);
 329        spin_lock_irq(&np->lock);
 330
 331IV. Notes
 332
 333NatSemi PCI network controllers are very uncommon.
 334
 335IVb. References
 336
 337http://www.scyld.com/expert/100mbps.html
 338http://www.scyld.com/expert/NWay.html
 339Datasheet is available from:
 340http://www.national.com/pf/DP/DP83815.html
 341
 342IVc. Errata
 343
 344None characterised.
 345*/
 346
 347
 348
 349enum pcistuff {
 350        PCI_USES_IO = 0x01,
 351        PCI_USES_MEM = 0x02,
 352        PCI_USES_MASTER = 0x04,
 353        PCI_ADDR0 = 0x08,
 354        PCI_ADDR1 = 0x10,
 355};
 356
 357/* MMIO operations required */
 358#define PCI_IOTYPE (PCI_USES_MASTER | PCI_USES_MEM | PCI_ADDR1)
 359
 360
 361/* array of board data directly indexed by pci_tbl[x].driver_data */
 362static struct {
 363        const char *name;
 364        unsigned long flags;
 365} natsemi_pci_info[] __devinitdata = {
 366        { "NatSemi DP8381[56]", PCI_IOTYPE },
 367};
 368
 369static struct pci_device_id natsemi_pci_tbl[] = {
 370        { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_83815, PCI_ANY_ID, PCI_ANY_ID, },
 371        { 0, },
 372};
 373MODULE_DEVICE_TABLE(pci, natsemi_pci_tbl);
 374
 375/* Offsets to the device registers.
 376   Unlike software-only systems, device drivers interact with complex hardware.
 377   It's not useful to define symbolic names for every register bit in the
 378   device.
 379*/
 380enum register_offsets {
 381        ChipCmd                 = 0x00,
 382        ChipConfig              = 0x04,
 383        EECtrl                  = 0x08,
 384        PCIBusCfg               = 0x0C,
 385        IntrStatus              = 0x10,
 386        IntrMask                = 0x14,
 387        IntrEnable              = 0x18,
 388        IntrHoldoff             = 0x16, /* DP83816 only */
 389        TxRingPtr               = 0x20,
 390        TxConfig                = 0x24,
 391        RxRingPtr               = 0x30,
 392        RxConfig                = 0x34,
 393        ClkRun                  = 0x3C,
 394        WOLCmd                  = 0x40,
 395        PauseCmd                = 0x44,
 396        RxFilterAddr            = 0x48,
 397        RxFilterData            = 0x4C,
 398        BootRomAddr             = 0x50,
 399        BootRomData             = 0x54,
 400        SiliconRev              = 0x58,
 401        StatsCtrl               = 0x5C,
 402        StatsData               = 0x60,
 403        RxPktErrs               = 0x60,
 404        RxMissed                = 0x68,
 405        RxCRCErrs               = 0x64,
 406        BasicControl            = 0x80,
 407        BasicStatus             = 0x84,
 408        AnegAdv                 = 0x90,
 409        AnegPeer                = 0x94,
 410        PhyStatus               = 0xC0,
 411        MIntrCtrl               = 0xC4,
 412        MIntrStatus             = 0xC8,
 413        PhyCtrl                 = 0xE4,
 414
 415        /* These are from the spec, around page 78... on a separate table.
 416         * The meaning of these registers depend on the value of PGSEL. */
 417        PGSEL                   = 0xCC,
 418        PMDCSR                  = 0xE4,
 419        TSTDAT                  = 0xFC,
 420        DSPCFG                  = 0xF4,
 421        SDCFG                   = 0xF8
 422};
 423/* the values for the 'magic' registers above (PGSEL=1) */
 424#define PMDCSR_VAL      0x189c  /* enable preferred adaptation circuitry */
 425#define TSTDAT_VAL      0x0
 426#define DSPCFG_VAL      0x5040
 427#define SDCFG_VAL       0x008c  /* set voltage thresholds for Signal Detect */
 428#define DSPCFG_LOCK     0x20    /* coefficient lock bit in DSPCFG */
 429#define TSTDAT_FIXED    0xe8    /* magic number for bad coefficients */
 430
 431/* misc PCI space registers */
 432enum pci_register_offsets {
 433        PCIPM                   = 0x44,
 434};
 435
 436enum ChipCmd_bits {
 437        ChipReset               = 0x100,
 438        RxReset                 = 0x20,
 439        TxReset                 = 0x10,
 440        RxOff                   = 0x08,
 441        RxOn                    = 0x04,
 442        TxOff                   = 0x02,
 443        TxOn                    = 0x01,
 444};
 445
 446enum ChipConfig_bits {
 447        CfgPhyDis               = 0x200,
 448        CfgPhyRst               = 0x400,
 449        CfgExtPhy               = 0x1000,
 450        CfgAnegEnable           = 0x2000,
 451        CfgAneg100              = 0x4000,
 452        CfgAnegFull             = 0x8000,
 453        CfgAnegDone             = 0x8000000,
 454        CfgFullDuplex           = 0x20000000,
 455        CfgSpeed100             = 0x40000000,
 456        CfgLink                 = 0x80000000,
 457};
 458
 459enum EECtrl_bits {
 460        EE_ShiftClk             = 0x04,
 461        EE_DataIn               = 0x01,
 462        EE_ChipSelect           = 0x08,
 463        EE_DataOut              = 0x02,
 464};
 465
 466enum PCIBusCfg_bits {
 467        EepromReload            = 0x4,
 468};
 469
 470/* Bits in the interrupt status/mask registers. */
 471enum IntrStatus_bits {
 472        IntrRxDone              = 0x0001,
 473        IntrRxIntr              = 0x0002,
 474        IntrRxErr               = 0x0004,
 475        IntrRxEarly             = 0x0008,
 476        IntrRxIdle              = 0x0010,
 477        IntrRxOverrun           = 0x0020,
 478        IntrTxDone              = 0x0040,
 479        IntrTxIntr              = 0x0080,
 480        IntrTxErr               = 0x0100,
 481        IntrTxIdle              = 0x0200,
 482        IntrTxUnderrun          = 0x0400,
 483        StatsMax                = 0x0800,
 484        SWInt                   = 0x1000,
 485        WOLPkt                  = 0x2000,
 486        LinkChange              = 0x4000,
 487        IntrHighBits            = 0x8000,
 488        RxStatusFIFOOver        = 0x10000,
 489        IntrPCIErr              = 0xf00000,
 490        RxResetDone             = 0x1000000,
 491        TxResetDone             = 0x2000000,
 492        IntrAbnormalSummary     = 0xCD20,
 493};
 494
 495/*
 496 * Default Interrupts:
 497 * Rx OK, Rx Packet Error, Rx Overrun,
 498 * Tx OK, Tx Packet Error, Tx Underrun,
 499 * MIB Service, Phy Interrupt, High Bits,
 500 * Rx Status FIFO overrun,
 501 * Received Target Abort, Received Master Abort,
 502 * Signalled System Error, Received Parity Error
 503 */
 504#define DEFAULT_INTR 0x00f1cd65
 505
 506enum TxConfig_bits {
 507        TxDrthMask              = 0x3f,
 508        TxFlthMask              = 0x3f00,
 509        TxMxdmaMask             = 0x700000,
 510        TxMxdma_512             = 0x0,
 511        TxMxdma_4               = 0x100000,
 512        TxMxdma_8               = 0x200000,
 513        TxMxdma_16              = 0x300000,
 514        TxMxdma_32              = 0x400000,
 515        TxMxdma_64              = 0x500000,
 516        TxMxdma_128             = 0x600000,
 517        TxMxdma_256             = 0x700000,
 518        TxCollRetry             = 0x800000,
 519        TxAutoPad               = 0x10000000,
 520        TxMacLoop               = 0x20000000,
 521        TxHeartIgn              = 0x40000000,
 522        TxCarrierIgn            = 0x80000000
 523};
 524
 525enum RxConfig_bits {
 526        RxDrthMask              = 0x3e,
 527        RxMxdmaMask             = 0x700000,
 528        RxMxdma_512             = 0x0,
 529        RxMxdma_4               = 0x100000,
 530        RxMxdma_8               = 0x200000,
 531        RxMxdma_16              = 0x300000,
 532        RxMxdma_32              = 0x400000,
 533        RxMxdma_64              = 0x500000,
 534        RxMxdma_128             = 0x600000,
 535        RxMxdma_256             = 0x700000,
 536        RxAcceptLong            = 0x8000000,
 537        RxAcceptTx              = 0x10000000,
 538        RxAcceptRunt            = 0x40000000,
 539        RxAcceptErr             = 0x80000000
 540};
 541
 542enum ClkRun_bits {
 543        PMEEnable               = 0x100,
 544        PMEStatus               = 0x8000,
 545};
 546
 547enum WolCmd_bits {
 548        WakePhy                 = 0x1,
 549        WakeUnicast             = 0x2,
 550        WakeMulticast           = 0x4,
 551        WakeBroadcast           = 0x8,
 552        WakeArp                 = 0x10,
 553        WakePMatch0             = 0x20,
 554        WakePMatch1             = 0x40,
 555        WakePMatch2             = 0x80,
 556        WakePMatch3             = 0x100,
 557        WakeMagic               = 0x200,
 558        WakeMagicSecure         = 0x400,
 559        SecureHack              = 0x100000,
 560        WokePhy                 = 0x400000,
 561        WokeUnicast             = 0x800000,
 562        WokeMulticast           = 0x1000000,
 563        WokeBroadcast           = 0x2000000,
 564        WokeArp                 = 0x4000000,
 565        WokePMatch0             = 0x8000000,
 566        WokePMatch1             = 0x10000000,
 567        WokePMatch2             = 0x20000000,
 568        WokePMatch3             = 0x40000000,
 569        WokeMagic               = 0x80000000,
 570        WakeOptsSummary         = 0x7ff
 571};
 572
 573enum RxFilterAddr_bits {
 574        RFCRAddressMask         = 0x3ff,
 575        AcceptMulticast         = 0x00200000,
 576        AcceptMyPhys            = 0x08000000,
 577        AcceptAllPhys           = 0x10000000,
 578        AcceptAllMulticast      = 0x20000000,
 579        AcceptBroadcast         = 0x40000000,
 580        RxFilterEnable          = 0x80000000
 581};
 582
 583enum StatsCtrl_bits {
 584        StatsWarn               = 0x1,
 585        StatsFreeze             = 0x2,
 586        StatsClear              = 0x4,
 587        StatsStrobe             = 0x8,
 588};
 589
 590enum MIntrCtrl_bits {
 591        MICRIntEn               = 0x2,
 592};
 593
 594enum PhyCtrl_bits {
 595        PhyAddrMask             = 0xf,
 596};
 597
 598/* values we might find in the silicon revision register */
 599#define SRR_DP83815_C   0x0302
 600#define SRR_DP83815_D   0x0403
 601#define SRR_DP83816_A4  0x0504
 602#define SRR_DP83816_A5  0x0505
 603
 604/* The Rx and Tx buffer descriptors. */
 605/* Note that using only 32 bit fields simplifies conversion to big-endian
 606   architectures. */
 607struct netdev_desc {
 608        u32 next_desc;
 609        s32 cmd_status;
 610        u32 addr;
 611        u32 software_use;
 612};
 613
 614/* Bits in network_desc.status */
 615enum desc_status_bits {
 616        DescOwn=0x80000000, DescMore=0x40000000, DescIntr=0x20000000,
 617        DescNoCRC=0x10000000, DescPktOK=0x08000000,
 618        DescSizeMask=0xfff,
 619
 620        DescTxAbort=0x04000000, DescTxFIFO=0x02000000,
 621        DescTxCarrier=0x01000000, DescTxDefer=0x00800000,
 622        DescTxExcDefer=0x00400000, DescTxOOWCol=0x00200000,
 623        DescTxExcColl=0x00100000, DescTxCollCount=0x000f0000,
 624
 625        DescRxAbort=0x04000000, DescRxOver=0x02000000,
 626        DescRxDest=0x01800000, DescRxLong=0x00400000,
 627        DescRxRunt=0x00200000, DescRxInvalid=0x00100000,
 628        DescRxCRC=0x00080000, DescRxAlign=0x00040000,
 629        DescRxLoop=0x00020000, DesRxColl=0x00010000,
 630};
 631
 632struct netdev_private {
 633        /* Descriptor rings first for alignment */
 634        dma_addr_t ring_dma;
 635        struct netdev_desc *rx_ring;
 636        struct netdev_desc *tx_ring;
 637        /* The addresses of receive-in-place skbuffs */
 638        struct sk_buff *rx_skbuff[RX_RING_SIZE];
 639        dma_addr_t rx_dma[RX_RING_SIZE];
 640        /* address of a sent-in-place packet/buffer, for later free() */
 641        struct sk_buff *tx_skbuff[TX_RING_SIZE];
 642        dma_addr_t tx_dma[TX_RING_SIZE];
 643        struct net_device_stats stats;
 644        /* Media monitoring timer */
 645        struct timer_list timer;
 646        /* Frequently used values: keep some adjacent for cache effect */
 647        struct pci_dev *pci_dev;
 648        struct netdev_desc *rx_head_desc;
 649        /* Producer/consumer ring indices */
 650        unsigned int cur_rx, dirty_rx;
 651        unsigned int cur_tx, dirty_tx;
 652        /* Based on MTU+slack. */
 653        unsigned int rx_buf_sz;
 654        int oom;
 655        /* Do not touch the nic registers */
 656        int hands_off;
 657        /* These values are keep track of the transceiver/media in use */
 658        unsigned int full_duplex;
 659        /* Rx filter */
 660        u32 cur_rx_mode;
 661        u32 rx_filter[16];
 662        /* FIFO and PCI burst thresholds */
 663        u32 tx_config, rx_config;
 664        /* original contents of ClkRun register */
 665        u32 SavedClkRun;
 666        /* silicon revision */
 667        u32 srr;
 668        /* expected DSPCFG value */
 669        u16 dspcfg;
 670        /* MII transceiver section */
 671        u16 advertising;
 672        unsigned int iosize;
 673        spinlock_t lock;
 674        u32 msg_enable;
 675};
 676
 677static int eeprom_read(long ioaddr, int location);
 678static int mdio_read(struct net_device *dev, int phy_id, int reg);
 679static void mdio_write(struct net_device *dev, int phy_id, int reg, u16 data);
 680static void natsemi_reset(struct net_device *dev);
 681static void natsemi_reload_eeprom(struct net_device *dev);
 682static void natsemi_stop_rxtx(struct net_device *dev);
 683static int netdev_open(struct net_device *dev);
 684static void do_cable_magic(struct net_device *dev);
 685static void undo_cable_magic(struct net_device *dev);
 686static void check_link(struct net_device *dev);
 687static void netdev_timer(unsigned long data);
 688static void dump_ring(struct net_device *dev);
 689static void tx_timeout(struct net_device *dev);
 690static int alloc_ring(struct net_device *dev);
 691static void refill_rx(struct net_device *dev);
 692static void init_ring(struct net_device *dev);
 693static void drain_tx(struct net_device *dev);
 694static void drain_ring(struct net_device *dev);
 695static void free_ring(struct net_device *dev);
 696static void reinit_ring(struct net_device *dev);
 697static void init_registers(struct net_device *dev);
 698static int start_tx(struct sk_buff *skb, struct net_device *dev);
 699static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
 700static void netdev_error(struct net_device *dev, int intr_status);
 701static void netdev_rx(struct net_device *dev);
 702static void netdev_tx_done(struct net_device *dev);
 703static void __set_rx_mode(struct net_device *dev);
 704static void set_rx_mode(struct net_device *dev);
 705static void __get_stats(struct net_device *dev);
 706static struct net_device_stats *get_stats(struct net_device *dev);
 707static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 708static int netdev_set_wol(struct net_device *dev, u32 newval);
 709static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur);
 710static int netdev_set_sopass(struct net_device *dev, u8 *newval);
 711static int netdev_get_sopass(struct net_device *dev, u8 *data);
 712static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
 713static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd);
 714static void enable_wol_mode(struct net_device *dev, int enable_intr);
 715static int netdev_close(struct net_device *dev);
 716static int netdev_get_regs(struct net_device *dev, u8 *buf);
 717static int netdev_get_eeprom(struct net_device *dev, u8 *buf);
 718
 719
 720static int __devinit natsemi_probe1 (struct pci_dev *pdev,
 721        const struct pci_device_id *ent)
 722{
 723        struct net_device *dev;
 724        struct netdev_private *np;
 725        int i, option, irq, chip_idx = ent->driver_data;
 726        static int find_cnt = -1;
 727        unsigned long ioaddr, iosize;
 728        const int pcibar = 1; /* PCI base address register */
 729        int prev_eedata;
 730        u32 tmp;
 731
 732/* when built into the kernel, we only print version if device is found */
 733#ifndef MODULE
 734        static int printed_version;
 735        if (!printed_version++)
 736                printk(version);
 737#endif
 738
 739        i = pci_enable_device(pdev);
 740        if (i) return i;
 741
 742        /* natsemi has a non-standard PM control register
 743         * in PCI config space.  Some boards apparently need
 744         * to be brought to D0 in this manner.
 745         */
 746        pci_read_config_dword(pdev, PCIPM, &tmp);
 747        if (tmp & PCI_PM_CTRL_STATE_MASK) {
 748                /* D0 state, disable PME assertion */
 749                u32 newtmp = tmp & ~PCI_PM_CTRL_STATE_MASK;
 750                pci_write_config_dword(pdev, PCIPM, newtmp);
 751        }
 752
 753        find_cnt++;
 754        ioaddr = pci_resource_start(pdev, pcibar);
 755        iosize = pci_resource_len(pdev, pcibar);
 756        irq = pdev->irq;
 757
 758        if (natsemi_pci_info[chip_idx].flags & PCI_USES_MASTER)
 759                pci_set_master(pdev);
 760
 761        dev = alloc_etherdev(sizeof (struct netdev_private));
 762        if (!dev)
 763                return -ENOMEM;
 764        SET_MODULE_OWNER(dev);
 765        SET_NETDEV_DEV(dev, &pdev->dev);
 766
 767        i = pci_request_regions(pdev, dev->name);
 768        if (i) {
 769                free_netdev(dev);
 770                return i;
 771        }
 772
 773        {
 774                void *mmio = ioremap (ioaddr, iosize);
 775                if (!mmio) {
 776                        pci_release_regions(pdev);
 777                        free_netdev(dev);
 778                        return -ENOMEM;
 779                }
 780                ioaddr = (unsigned long) mmio;
 781        }
 782
 783        /* Work around the dropped serial bit. */
 784        prev_eedata = eeprom_read(ioaddr, 6);
 785        for (i = 0; i < 3; i++) {
 786                int eedata = eeprom_read(ioaddr, i + 7);
 787                dev->dev_addr[i*2] = (eedata << 1) + (prev_eedata >> 15);
 788                dev->dev_addr[i*2+1] = eedata >> 7;
 789                prev_eedata = eedata;
 790        }
 791
 792        dev->base_addr = ioaddr;
 793        dev->irq = irq;
 794
 795        np = dev->priv;
 796
 797        np->pci_dev = pdev;
 798        pci_set_drvdata(pdev, dev);
 799        np->iosize = iosize;
 800        spin_lock_init(&np->lock);
 801        np->msg_enable = (debug >= 0) ? (1<<debug)-1 : NATSEMI_DEF_MSG;
 802        np->hands_off = 0;
 803
 804        /* Reset the chip to erase previous misconfiguration. */
 805        natsemi_reload_eeprom(dev);
 806        natsemi_reset(dev);
 807
 808        option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
 809        if (dev->mem_start)
 810                option = dev->mem_start;
 811
 812        /* The lower four bits are the media type. */
 813        if (option) {
 814                if (option & 0x200)
 815                        np->full_duplex = 1;
 816                if (option & 15)
 817                        printk(KERN_INFO
 818                                "%s: ignoring user supplied media type %d",
 819                                dev->name, option & 15);
 820        }
 821        if (find_cnt < MAX_UNITS  &&  full_duplex[find_cnt])
 822                np->full_duplex = 1;
 823
 824        /* The chip-specific entries in the device structure. */
 825        dev->open = &netdev_open;
 826        dev->hard_start_xmit = &start_tx;
 827        dev->stop = &netdev_close;
 828        dev->get_stats = &get_stats;
 829        dev->set_multicast_list = &set_rx_mode;
 830        dev->do_ioctl = &netdev_ioctl;
 831        dev->tx_timeout = &tx_timeout;
 832        dev->watchdog_timeo = TX_TIMEOUT;
 833
 834        if (mtu)
 835                dev->mtu = mtu;
 836
 837        i = register_netdev(dev);
 838        if (i) {
 839                pci_release_regions(pdev);
 840                unregister_netdev(dev);
 841                free_netdev(dev);
 842                pci_set_drvdata(pdev, NULL);
 843                return i;
 844        }
 845        netif_carrier_off(dev);
 846
 847        if (netif_msg_drv(np)) {
 848                printk(KERN_INFO "%s: %s at %#08lx, ",
 849                        dev->name, natsemi_pci_info[chip_idx].name, ioaddr);
 850                for (i = 0; i < ETH_ALEN-1; i++)
 851                                printk("%02x:", dev->dev_addr[i]);
 852                printk("%02x, IRQ %d.\n", dev->dev_addr[i], irq);
 853        }
 854
 855        np->advertising = mdio_read(dev, 1, MII_ADVERTISE);
 856        if ((readl(ioaddr + ChipConfig) & 0xe000) != 0xe000
 857         && netif_msg_probe(np)) {
 858                u32 chip_config = readl(ioaddr + ChipConfig);
 859                printk(KERN_INFO "%s: Transceiver default autonegotiation %s "
 860                        "10%s %s duplex.\n",
 861                        dev->name,
 862                        chip_config & CfgAnegEnable ?
 863                          "enabled, advertise" : "disabled, force",
 864                        chip_config & CfgAneg100 ? "0" : "",
 865                        chip_config & CfgAnegFull ? "full" : "half");
 866        }
 867        if (netif_msg_probe(np))
 868                printk(KERN_INFO
 869                        "%s: Transceiver status %#04x advertising %#04x.\n",
 870                        dev->name, mdio_read(dev, 1, MII_BMSR),
 871                        np->advertising);
 872
 873        /* save the silicon revision for later querying */
 874        np->srr = readl(ioaddr + SiliconRev);
 875        if (netif_msg_hw(np))
 876                printk(KERN_INFO "%s: silicon revision %#04x.\n",
 877                                dev->name, np->srr);
 878
 879
 880        return 0;
 881}
 882
 883
 884/* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
 885   The EEPROM code is for the common 93c06/46 EEPROMs with 6 bit addresses. */
 886
 887/* Delay between EEPROM clock transitions.
 888   No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
 889   a delay.  Note that pre-2.0.34 kernels had a cache-alignment bug that
 890   made udelay() unreliable.
 891   The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
 892   depricated.
 893*/
 894#define eeprom_delay(ee_addr)   readl(ee_addr)
 895
 896#define EE_Write0 (EE_ChipSelect)
 897#define EE_Write1 (EE_ChipSelect | EE_DataIn)
 898
 899/* The EEPROM commands include the alway-set leading bit. */
 900enum EEPROM_Cmds {
 901        EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
 902};
 903
 904static int eeprom_read(long addr, int location)
 905{
 906        int i;
 907        int retval = 0;
 908        long ee_addr = addr + EECtrl;
 909        int read_cmd = location | EE_ReadCmd;
 910        writel(EE_Write0, ee_addr);
 911
 912        /* Shift the read command bits out. */
 913        for (i = 10; i >= 0; i--) {
 914                short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
 915                writel(dataval, ee_addr);
 916                eeprom_delay(ee_addr);
 917                writel(dataval | EE_ShiftClk, ee_addr);
 918                eeprom_delay(ee_addr);
 919        }
 920        writel(EE_ChipSelect, ee_addr);
 921        eeprom_delay(ee_addr);
 922
 923        for (i = 0; i < 16; i++) {
 924                writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
 925                eeprom_delay(ee_addr);
 926                retval |= (readl(ee_addr) & EE_DataOut) ? 1 << i : 0;
 927                writel(EE_ChipSelect, ee_addr);
 928                eeprom_delay(ee_addr);
 929        }
 930
 931        /* Terminate the EEPROM access. */
 932        writel(EE_Write0, ee_addr);
 933        writel(0, ee_addr);
 934        return retval;
 935}
 936
 937/* MII transceiver control section.
 938 * The 83815 series has an internal transceiver, and we present the
 939 * management registers as if they were MII connected. */
 940
 941static int mdio_read(struct net_device *dev, int phy_id, int reg)
 942{
 943        if (phy_id == 1 && reg < 32)
 944                return readl(dev->base_addr+BasicControl+(reg<<2))&0xffff;
 945        else
 946                return 0xffff;
 947}
 948
 949static void mdio_write(struct net_device *dev, int phy_id, int reg, u16 data)
 950{
 951        struct netdev_private *np = dev->priv;
 952        if (phy_id == 1 && reg < 32) {
 953                writew(data, dev->base_addr+BasicControl+(reg<<2));
 954                switch (reg) {
 955                        case MII_ADVERTISE: np->advertising = data; break;
 956                }
 957        }
 958}
 959
 960/* CFG bits [13:16] [18:23] */
 961#define CFG_RESET_SAVE 0xfde000
 962/* WCSR bits [0:4] [9:10] */
 963#define WCSR_RESET_SAVE 0x61f
 964/* RFCR bits [20] [22] [27:31] */
 965#define RFCR_RESET_SAVE 0xf8500000;
 966
 967static void natsemi_reset(struct net_device *dev)
 968{
 969        int i;
 970        u32 cfg;
 971        u32 wcsr;
 972        u32 rfcr;
 973        u16 pmatch[3];
 974        u16 sopass[3];
 975        struct netdev_private *np = dev->priv;
 976
 977        /*
 978         * Resetting the chip causes some registers to be lost.
 979         * Natsemi suggests NOT reloading the EEPROM while live, so instead
 980         * we save the state that would have been loaded from EEPROM
 981         * on a normal power-up (see the spec EEPROM map).  This assumes
 982         * whoever calls this will follow up with init_registers() eventually.
 983         */
 984
 985        /* CFG */
 986        cfg = readl(dev->base_addr + ChipConfig) & CFG_RESET_SAVE;
 987        /* WCSR */
 988        wcsr = readl(dev->base_addr + WOLCmd) & WCSR_RESET_SAVE;
 989        /* RFCR */
 990        rfcr = readl(dev->base_addr + RxFilterAddr) & RFCR_RESET_SAVE;
 991        /* PMATCH */
 992        for (i = 0; i < 3; i++) {
 993                writel(i*2, dev->base_addr + RxFilterAddr);
 994                pmatch[i] = readw(dev->base_addr + RxFilterData);
 995        }
 996        /* SOPAS */
 997        for (i = 0; i < 3; i++) {
 998                writel(0xa+(i*2), dev->base_addr + RxFilterAddr);
 999                sopass[i] = readw(dev->base_addr + RxFilterData);
1000        }
1001
1002        /* now whack the chip */
1003        writel(ChipReset, dev->base_addr + ChipCmd);
1004        for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1005                if (!(readl(dev->base_addr + ChipCmd) & ChipReset))
1006                        break;
1007                udelay(5);
1008        }
1009        if (i==NATSEMI_HW_TIMEOUT) {
1010                printk(KERN_WARNING "%s: reset did not complete in %d usec.\n",
1011                        dev->name, i*5);
1012        } else if (netif_msg_hw(np)) {
1013                printk(KERN_DEBUG "%s: reset completed in %d usec.\n",
1014                        dev->name, i*5);
1015        }
1016
1017        /* restore CFG */
1018        cfg |= readl(dev->base_addr + ChipConfig) & ~CFG_RESET_SAVE;
1019        writel(cfg, dev->base_addr + ChipConfig);
1020        /* restore WCSR */
1021        wcsr |= readl(dev->base_addr + WOLCmd) & ~WCSR_RESET_SAVE;
1022        writel(wcsr, dev->base_addr + WOLCmd);
1023        /* read RFCR */
1024        rfcr |= readl(dev->base_addr + RxFilterAddr) & ~RFCR_RESET_SAVE;
1025        /* restore PMATCH */
1026        for (i = 0; i < 3; i++) {
1027                writel(i*2, dev->base_addr + RxFilterAddr);
1028                writew(pmatch[i], dev->base_addr + RxFilterData);
1029        }
1030        for (i = 0; i < 3; i++) {
1031                writel(0xa+(i*2), dev->base_addr + RxFilterAddr);
1032                writew(sopass[i], dev->base_addr + RxFilterData);
1033        }
1034        /* restore RFCR */
1035        writel(rfcr, dev->base_addr + RxFilterAddr);
1036}
1037
1038static void natsemi_reload_eeprom(struct net_device *dev)
1039{
1040        struct netdev_private *np = dev->priv;
1041        int i;
1042
1043        writel(EepromReload, dev->base_addr + PCIBusCfg);
1044        for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1045                udelay(50);
1046                if (!(readl(dev->base_addr + PCIBusCfg) & EepromReload))
1047                        break;
1048        }
1049        if (i==NATSEMI_HW_TIMEOUT) {
1050                printk(KERN_WARNING "%s: EEPROM did not reload in %d usec.\n",
1051                        dev->name, i*50);
1052        } else if (netif_msg_hw(np)) {
1053                printk(KERN_DEBUG "%s: EEPROM reloaded in %d usec.\n",
1054                        dev->name, i*50);
1055        }
1056}
1057
1058static void natsemi_stop_rxtx(struct net_device *dev)
1059{
1060        long ioaddr = dev->base_addr;
1061        struct netdev_private *np = dev->priv;
1062        int i;
1063
1064        writel(RxOff | TxOff, ioaddr + ChipCmd);
1065        for(i=0;i< NATSEMI_HW_TIMEOUT;i++) {
1066                if ((readl(ioaddr + ChipCmd) & (TxOn|RxOn)) == 0)
1067                        break;
1068                udelay(5);
1069        }
1070        if (i==NATSEMI_HW_TIMEOUT) {
1071                printk(KERN_WARNING "%s: Tx/Rx process did not stop in %d usec.\n",
1072                        dev->name, i*5);
1073        } else if (netif_msg_hw(np)) {
1074                printk(KERN_DEBUG "%s: Tx/Rx process stopped in %d usec.\n",
1075                        dev->name, i*5);
1076        }
1077}
1078
1079static int netdev_open(struct net_device *dev)
1080{
1081        struct netdev_private *np = dev->priv;
1082        long ioaddr = dev->base_addr;
1083        int i;
1084
1085        /* Reset the chip, just in case. */
1086        natsemi_reset(dev);
1087
1088        i = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
1089        if (i) return i;
1090
1091        if (netif_msg_ifup(np))
1092                printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
1093                        dev->name, dev->irq);
1094        i = alloc_ring(dev);
1095        if (i < 0) {
1096                free_irq(dev->irq, dev);
1097                return i;
1098        }
1099        init_ring(dev);
1100        spin_lock_irq(&np->lock);
1101        init_registers(dev);
1102        /* now set the MAC address according to dev->dev_addr */
1103        for (i = 0; i < 3; i++) {
1104                u16 mac = (dev->dev_addr[2*i+1]<<8) + dev->dev_addr[2*i];
1105
1106                writel(i*2, ioaddr + RxFilterAddr);
1107                writew(mac, ioaddr + RxFilterData);
1108        }
1109        writel(np->cur_rx_mode, ioaddr + RxFilterAddr);
1110        spin_unlock_irq(&np->lock);
1111
1112        netif_start_queue(dev);
1113
1114        if (netif_msg_ifup(np))
1115                printk(KERN_DEBUG "%s: Done netdev_open(), status: %#08x.\n",
1116                        dev->name, (int)readl(ioaddr + ChipCmd));
1117
1118        /* Set the timer to check for link beat. */
1119        init_timer(&np->timer);
1120        np->timer.expires = jiffies + NATSEMI_TIMER_FREQ;
1121        np->timer.data = (unsigned long)dev;
1122        np->timer.function = &netdev_timer; /* timer handler */
1123        add_timer(&np->timer);
1124
1125        return 0;
1126}
1127
1128static void do_cable_magic(struct net_device *dev)
1129{
1130        struct netdev_private *np = dev->priv;
1131
1132        if (np->srr >= SRR_DP83816_A5)
1133                return;
1134
1135        /*
1136         * 100 MBit links with short cables can trip an issue with the chip.
1137         * The problem manifests as lots of CRC errors and/or flickering
1138         * activity LED while idle.  This process is based on instructions
1139         * from engineers at National.
1140         */
1141        if (readl(dev->base_addr + ChipConfig) & CfgSpeed100) {
1142                u16 data;
1143
1144                writew(1, dev->base_addr + PGSEL);
1145                /*
1146                 * coefficient visibility should already be enabled via
1147                 * DSPCFG | 0x1000
1148                 */
1149                data = readw(dev->base_addr + TSTDAT) & 0xff;
1150                /*
1151                 * the value must be negative, and within certain values
1152                 * (these values all come from National)
1153                 */
1154                if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
1155                        struct netdev_private *np = dev->priv;
1156
1157                        /* the bug has been triggered - fix the coefficient */
1158                        writew(TSTDAT_FIXED, dev->base_addr + TSTDAT);
1159                        /* lock the value */
1160                        data = readw(dev->base_addr + DSPCFG);
1161                        np->dspcfg = data | DSPCFG_LOCK;
1162                        writew(np->dspcfg, dev->base_addr + DSPCFG);
1163                }
1164                writew(0, dev->base_addr + PGSEL);
1165        }
1166}
1167
1168static void undo_cable_magic(struct net_device *dev)
1169{
1170        u16 data;
1171        struct netdev_private *np = dev->priv;
1172
1173        if (np->srr >= SRR_DP83816_A5)
1174                return;
1175
1176        writew(1, dev->base_addr + PGSEL);
1177        /* make sure the lock bit is clear */
1178        data = readw(dev->base_addr + DSPCFG);
1179        np->dspcfg = data & ~DSPCFG_LOCK;
1180        writew(np->dspcfg, dev->base_addr + DSPCFG);
1181        writew(0, dev->base_addr + PGSEL);
1182}
1183
1184static void check_link(struct net_device *dev)
1185{
1186        struct netdev_private *np = dev->priv;
1187        long ioaddr = dev->base_addr;
1188        int duplex;
1189        int chipcfg = readl(ioaddr + ChipConfig);
1190
1191        if (!(chipcfg & CfgLink)) {
1192                if (netif_carrier_ok(dev)) {
1193                        if (netif_msg_link(np))
1194                                printk(KERN_NOTICE "%s: link down.\n",
1195                                        dev->name);
1196                        netif_carrier_off(dev);
1197                        undo_cable_magic(dev);
1198                }
1199                return;
1200        }
1201        if (!netif_carrier_ok(dev)) {
1202                if (netif_msg_link(np))
1203                        printk(KERN_NOTICE "%s: link up.\n", dev->name);
1204                netif_carrier_on(dev);
1205                do_cable_magic(dev);
1206        }
1207
1208        duplex = np->full_duplex || (chipcfg & CfgFullDuplex ? 1 : 0);
1209
1210        /* if duplex is set then bit 28 must be set, too */
1211        if (duplex ^ !!(np->rx_config & RxAcceptTx)) {
1212                if (netif_msg_link(np))
1213                        printk(KERN_INFO
1214                                "%s: Setting %s-duplex based on negotiated "
1215                                "link capability.\n", dev->name,
1216                                duplex ? "full" : "half");
1217                if (duplex) {
1218                        np->rx_config |= RxAcceptTx;
1219                        np->tx_config |= TxCarrierIgn | TxHeartIgn;
1220                } else {
1221                        np->rx_config &= ~RxAcceptTx;
1222                        np->tx_config &= ~(TxCarrierIgn | TxHeartIgn);
1223                }
1224                writel(np->tx_config, ioaddr + TxConfig);
1225                writel(np->rx_config, ioaddr + RxConfig);
1226        }
1227}
1228
1229static void init_registers(struct net_device *dev)
1230{
1231        struct netdev_private *np = dev->priv;
1232        long ioaddr = dev->base_addr;
1233        int i;
1234
1235        for (i=0;i<NATSEMI_HW_TIMEOUT;i++) {
1236                if (readl(dev->base_addr + ChipConfig) & CfgAnegDone)
1237                        break;
1238                udelay(10);
1239        }
1240        if (i==NATSEMI_HW_TIMEOUT && netif_msg_link(np)) {
1241                printk(KERN_INFO
1242                        "%s: autonegotiation did not complete in %d usec.\n",
1243                        dev->name, i*10);
1244        }
1245
1246        /* On page 78 of the spec, they recommend some settings for "optimum
1247           performance" to be done in sequence.  These settings optimize some
1248           of the 100Mbit autodetection circuitry.  They say we only want to
1249           do this for rev C of the chip, but engineers at NSC (Bradley
1250           Kennedy) recommends always setting them.  If you don't, you get
1251           errors on some autonegotiations that make the device unusable.
1252        */
1253        writew(1, ioaddr + PGSEL);
1254        writew(PMDCSR_VAL, ioaddr + PMDCSR);
1255        writew(TSTDAT_VAL, ioaddr + TSTDAT);
1256        writew(DSPCFG_VAL, ioaddr + DSPCFG);
1257        writew(SDCFG_VAL, ioaddr + SDCFG);
1258        writew(0, ioaddr + PGSEL);
1259        np->dspcfg = DSPCFG_VAL;
1260
1261        /* Enable PHY Specific event based interrupts.  Link state change
1262           and Auto-Negotiation Completion are among the affected.
1263           Read the intr status to clear it (needed for wake events).
1264        */
1265        readw(ioaddr + MIntrStatus);
1266        writew(MICRIntEn, ioaddr + MIntrCtrl);
1267
1268        /* clear any interrupts that are pending, such as wake events */
1269        readl(ioaddr + IntrStatus);
1270
1271        writel(np->ring_dma, ioaddr + RxRingPtr);
1272        writel(np->ring_dma + RX_RING_SIZE * sizeof(struct netdev_desc),
1273                ioaddr + TxRingPtr);
1274
1275        /* Initialize other registers.
1276         * Configure the PCI bus bursts and FIFO thresholds.
1277         * Configure for standard, in-spec Ethernet.
1278         * Start with half-duplex. check_link will update
1279         * to the correct settings.
1280         */
1281
1282        /* DRTH: 2: start tx if 64 bytes are in the fifo
1283         * FLTH: 0x10: refill with next packet if 512 bytes are free
1284         * MXDMA: 0: up to 256 byte bursts.
1285         *      MXDMA must be <= FLTH
1286         * ECRETRY=1
1287         * ATP=1
1288         */
1289        np->tx_config = TxAutoPad | TxCollRetry | TxMxdma_256 | (0x1002);
1290        writel(np->tx_config, ioaddr + TxConfig);
1291
1292        /* DRTH 0x10: start copying to memory if 128 bytes are in the fifo
1293         * MXDMA 0: up to 256 byte bursts
1294         */
1295        np->rx_config = RxMxdma_256 | 0x20;
1296        writel(np->rx_config, ioaddr + RxConfig);
1297
1298        /* Disable PME:
1299         * The PME bit is initialized from the EEPROM contents.
1300         * PCI cards probably have PME disabled, but motherboard
1301         * implementations may have PME set to enable WakeOnLan.
1302         * With PME set the chip will scan incoming packets but
1303         * nothing will be written to memory. */
1304        np->SavedClkRun = readl(ioaddr + ClkRun);
1305        writel(np->SavedClkRun & ~PMEEnable, ioaddr + ClkRun);
1306        if (np->SavedClkRun & PMEStatus && netif_msg_wol(np)) {
1307                printk(KERN_NOTICE "%s: Wake-up event %#08x\n",
1308                        dev->name, readl(ioaddr + WOLCmd));
1309        }
1310
1311        check_link(dev);
1312        __set_rx_mode(dev);
1313
1314        /* Enable interrupts by setting the interrupt mask. */
1315        writel(DEFAULT_INTR, ioaddr + IntrMask);
1316        writel(1, ioaddr + IntrEnable);
1317
1318        writel(RxOn | TxOn, ioaddr + ChipCmd);
1319        writel(StatsClear, ioaddr + StatsCtrl); /* Clear Stats */
1320}
1321
1322/*
1323 * netdev_timer:
1324 * Purpose:
1325 * 1) check for link changes. Usually they are handled by the MII interrupt
1326 *    but it doesn't hurt to check twice.
1327 * 2) check for sudden death of the NIC:
1328 *    It seems that a reference set for this chip went out with incorrect info,
1329 *    and there exist boards that aren't quite right.  An unexpected voltage
1330 *    drop can cause the PHY to get itself in a weird state (basically reset).
1331 *    NOTE: this only seems to affect revC chips.
1332 * 3) check of death of the RX path due to OOM
1333 */
1334static void netdev_timer(unsigned long data)
1335{
1336        struct net_device *dev = (struct net_device *)data;
1337        struct netdev_private *np = dev->priv;
1338        int next_tick = 5*HZ;
1339        long ioaddr = dev->base_addr;
1340        u16 dspcfg;
1341
1342        if (netif_msg_timer(np)) {
1343                /* DO NOT read the IntrStatus register,
1344                 * a read clears any pending interrupts.
1345                 */
1346                printk(KERN_DEBUG "%s: Media selection timer tick.\n",
1347                        dev->name);
1348        }
1349
1350        spin_lock_irq(&np->lock);
1351
1352        /* check for a nasty random phy-reset - use dspcfg as a flag */
1353        writew(1, ioaddr+PGSEL);
1354        dspcfg = readw(ioaddr+DSPCFG);
1355        writew(0, ioaddr+PGSEL);
1356        if (dspcfg != np->dspcfg) {
1357                if (!netif_queue_stopped(dev)) {
1358                        spin_unlock_irq(&np->lock);
1359                        if (netif_msg_hw(np))
1360                                printk(KERN_NOTICE "%s: possible phy reset: "
1361                                        "re-initializing\n", dev->name);
1362                        disable_irq(dev->irq);
1363                        spin_lock_irq(&np->lock);
1364                        natsemi_stop_rxtx(dev);
1365                        dump_ring(dev);
1366                        reinit_ring(dev);
1367                        init_registers(dev);
1368                        spin_unlock_irq(&np->lock);
1369                        enable_irq(dev->irq);
1370                } else {
1371                        /* hurry back */
1372                        next_tick = HZ;
1373                        spin_unlock_irq(&np->lock);
1374                }
1375        } else {
1376                /* init_registers() calls check_link() for the above case */
1377                check_link(dev);
1378                spin_unlock_irq(&np->lock);
1379        }
1380        if (np->oom) {
1381                disable_irq(dev->irq);
1382                np->oom = 0;
1383                refill_rx(dev);
1384                enable_irq(dev->irq);
1385                if (!np->oom) {
1386                        writel(RxOn, dev->base_addr + ChipCmd);
1387                } else {
1388                        next_tick = 1;
1389                }
1390        }
1391        mod_timer(&np->timer, jiffies + next_tick);
1392}
1393
1394static void dump_ring(struct net_device *dev)
1395{
1396        struct netdev_private *np = dev->priv;
1397
1398        if (netif_msg_pktdata(np)) {
1399                int i;
1400                printk(KERN_DEBUG "  Tx ring at %p:\n", np->tx_ring);
1401                for (i = 0; i < TX_RING_SIZE; i++) {
1402                        printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1403                                i, np->tx_ring[i].next_desc,
1404                                np->tx_ring[i].cmd_status,
1405                                np->tx_ring[i].addr);
1406                }
1407                printk(KERN_DEBUG "  Rx ring %p:\n", np->rx_ring);
1408                for (i = 0; i < RX_RING_SIZE; i++) {
1409                        printk(KERN_DEBUG " #%d desc. %#08x %#08x %#08x.\n",
1410                                i, np->rx_ring[i].next_desc,
1411                                np->rx_ring[i].cmd_status,
1412                                np->rx_ring[i].addr);
1413                }
1414        }
1415}
1416
1417static void tx_timeout(struct net_device *dev)
1418{
1419        struct netdev_private *np = dev->priv;
1420        long ioaddr = dev->base_addr;
1421
1422        disable_irq(dev->irq);
1423        spin_lock_irq(&np->lock);
1424        if (!np->hands_off) {
1425                if (netif_msg_tx_err(np))
1426                        printk(KERN_WARNING
1427                                "%s: Transmit timed out, status %#08x,"
1428                                " resetting...\n",
1429                                dev->name, readl(ioaddr + IntrStatus));
1430                dump_ring(dev);
1431
1432                natsemi_reset(dev);
1433                reinit_ring(dev);
1434                init_registers(dev);
1435        } else {
1436                printk(KERN_WARNING
1437                        "%s: tx_timeout while in hands_off state?\n",
1438                        dev->name);
1439        }
1440        spin_unlock_irq(&np->lock);
1441        enable_irq(dev->irq);
1442
1443        dev->trans_start = jiffies;
1444        np->stats.tx_errors++;
1445        netif_wake_queue(dev);
1446}
1447
1448static int alloc_ring(struct net_device *dev)
1449{
1450        struct netdev_private *np = dev->priv;
1451        np->rx_ring = pci_alloc_consistent(np->pci_dev,
1452                sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
1453                &np->ring_dma);
1454        if (!np->rx_ring)
1455                return -ENOMEM;
1456        np->tx_ring = &np->rx_ring[RX_RING_SIZE];
1457        return 0;
1458}
1459
1460static void refill_rx(struct net_device *dev)
1461{
1462        struct netdev_private *np = dev->priv;
1463
1464        /* Refill the Rx ring buffers. */
1465        for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1466                struct sk_buff *skb;
1467                int entry = np->dirty_rx % RX_RING_SIZE;
1468                if (np->rx_skbuff[entry] == NULL) {
1469                        skb = dev_alloc_skb(np->rx_buf_sz);
1470                        np->rx_skbuff[entry] = skb;
1471                        if (skb == NULL)
1472                                break; /* Better luck next round. */
1473                        skb->dev = dev; /* Mark as being used by this device. */
1474                        np->rx_dma[entry] = pci_map_single(np->pci_dev,
1475                                skb->data, skb->len, PCI_DMA_FROMDEVICE);
1476                        np->rx_ring[entry].addr = cpu_to_le32(np->rx_dma[entry]);
1477                }
1478                np->rx_ring[entry].cmd_status = cpu_to_le32(np->rx_buf_sz);
1479        }
1480        if (np->cur_rx - np->dirty_rx == RX_RING_SIZE) {
1481                if (netif_msg_rx_err(np))
1482                        printk(KERN_WARNING "%s: going OOM.\n", dev->name);
1483                np->oom = 1;
1484        }
1485}
1486
1487/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1488static void init_ring(struct net_device *dev)
1489{
1490        struct netdev_private *np = dev->priv;
1491        int i;
1492
1493        /* 1) TX ring */
1494        np->dirty_tx = np->cur_tx = 0;
1495        for (i = 0; i < TX_RING_SIZE; i++) {
1496                np->tx_skbuff[i] = NULL;
1497                np->tx_ring[i].next_desc = cpu_to_le32(np->ring_dma
1498                        +sizeof(struct netdev_desc)
1499                        *((i+1)%TX_RING_SIZE+RX_RING_SIZE));
1500                np->tx_ring[i].cmd_status = 0;
1501        }
1502
1503        /* 2) RX ring */
1504        np->dirty_rx = 0;
1505        np->cur_rx = RX_RING_SIZE;
1506        np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1507        np->oom = 0;
1508        np->rx_head_desc = &np->rx_ring[0];
1509
1510        /* Please be carefull before changing this loop - at least gcc-2.95.1
1511         * miscompiles it otherwise.
1512         */
1513        /* Initialize all Rx descriptors. */
1514        for (i = 0; i < RX_RING_SIZE; i++) {
1515                np->rx_ring[i].next_desc = cpu_to_le32(np->ring_dma
1516                                +sizeof(struct netdev_desc)
1517                                *((i+1)%RX_RING_SIZE));
1518                np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
1519                np->rx_skbuff[i] = NULL;
1520        }
1521        refill_rx(dev);
1522        dump_ring(dev);
1523}
1524
1525static void drain_tx(struct net_device *dev)
1526{
1527        struct netdev_private *np = dev->priv;
1528        int i;
1529
1530        for (i = 0; i < TX_RING_SIZE; i++) {
1531                if (np->tx_skbuff[i]) {
1532                        pci_unmap_single(np->pci_dev,
1533                                np->tx_dma[i], np->tx_skbuff[i]->len,
1534                                PCI_DMA_TODEVICE);
1535                        dev_kfree_skb(np->tx_skbuff[i]);
1536                        np->stats.tx_dropped++;
1537                }
1538                np->tx_skbuff[i] = NULL;
1539        }
1540}
1541
1542static void drain_ring(struct net_device *dev)
1543{
1544        struct netdev_private *np = dev->priv;
1545        int i;
1546
1547        /* Free all the skbuffs in the Rx queue. */
1548        for (i = 0; i < RX_RING_SIZE; i++) {
1549                np->rx_ring[i].cmd_status = 0;
1550                np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */
1551                if (np->rx_skbuff[i]) {
1552                        pci_unmap_single(np->pci_dev,
1553                                np->rx_dma[i], np->rx_skbuff[i]->len,
1554                                PCI_DMA_FROMDEVICE);
1555                        dev_kfree_skb(np->rx_skbuff[i]);
1556                }
1557                np->rx_skbuff[i] = NULL;
1558        }
1559        drain_tx(dev);
1560}
1561
1562static void free_ring(struct net_device *dev)
1563{
1564        struct netdev_private *np = dev->priv;
1565        pci_free_consistent(np->pci_dev,
1566                sizeof(struct netdev_desc) * (RX_RING_SIZE+TX_RING_SIZE),
1567                np->rx_ring, np->ring_dma);
1568}
1569
1570static void reinit_ring(struct net_device *dev)
1571{
1572        struct netdev_private *np = dev->priv;
1573        int i;
1574
1575        /* drain TX ring */
1576        drain_tx(dev);
1577        np->dirty_tx = np->cur_tx = 0;
1578        for (i=0;i<TX_RING_SIZE;i++)
1579                np->tx_ring[i].cmd_status = 0;
1580
1581        /* RX Ring */
1582        np->dirty_rx = 0;
1583        np->cur_rx = RX_RING_SIZE;
1584        np->rx_head_desc = &np->rx_ring[0];
1585        /* Initialize all Rx descriptors. */
1586        for (i = 0; i < RX_RING_SIZE; i++)
1587                np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn);
1588
1589        refill_rx(dev);
1590}
1591
1592static int start_tx(struct sk_buff *skb, struct net_device *dev)
1593{
1594        struct netdev_private *np = dev->priv;
1595        unsigned entry;
1596
1597        /* Note: Ordering is important here, set the field with the
1598           "ownership" bit last, and only then increment cur_tx. */
1599
1600        /* Calculate the next Tx descriptor entry. */
1601        entry = np->cur_tx % TX_RING_SIZE;
1602
1603        np->tx_skbuff[entry] = skb;
1604        np->tx_dma[entry] = pci_map_single(np->pci_dev,
1605                                skb->data,skb->len, PCI_DMA_TODEVICE);
1606
1607        np->tx_ring[entry].addr = cpu_to_le32(np->tx_dma[entry]);
1608
1609        spin_lock_irq(&np->lock);
1610
1611        if (!np->hands_off) {
1612                np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn | skb->len);
1613                /* StrongARM: Explicitly cache flush np->tx_ring and
1614                 * skb->data,skb->len. */
1615                wmb();
1616                np->cur_tx++;
1617                if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
1618                        netdev_tx_done(dev);
1619                        if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1)
1620                                netif_stop_queue(dev);
1621                }
1622                /* Wake the potentially-idle transmit channel. */
1623                writel(TxOn, dev->base_addr + ChipCmd);
1624        } else {
1625                dev_kfree_skb_irq(skb);
1626                np->stats.tx_dropped++;
1627        }
1628        spin_unlock_irq(&np->lock);
1629
1630        dev->trans_start = jiffies;
1631
1632        if (netif_msg_tx_queued(np)) {
1633                printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1634                        dev->name, np->cur_tx, entry);
1635        }
1636        return 0;
1637}
1638
1639static void netdev_tx_done(struct net_device *dev)
1640{
1641        struct netdev_private *np = dev->priv;
1642
1643        for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1644                int entry = np->dirty_tx % TX_RING_SIZE;
1645                if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescOwn))
1646                        break;
1647                if (netif_msg_tx_done(np))
1648                        printk(KERN_DEBUG
1649                                "%s: tx frame #%d finished, status %#08x.\n",
1650                                        dev->name, np->dirty_tx,
1651                                        le32_to_cpu(np->tx_ring[entry].cmd_status));
1652                if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescPktOK)) {
1653                        np->stats.tx_packets++;
1654                        np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1655                } else { /* Various Tx errors */
1656                        int tx_status =
1657                                le32_to_cpu(np->tx_ring[entry].cmd_status);
1658                        if (tx_status & (DescTxAbort|DescTxExcColl))
1659                                np->stats.tx_aborted_errors++;
1660                        if (tx_status & DescTxFIFO)
1661                                np->stats.tx_fifo_errors++;
1662                        if (tx_status & DescTxCarrier)
1663                                np->stats.tx_carrier_errors++;
1664                        if (tx_status & DescTxOOWCol)
1665                                np->stats.tx_window_errors++;
1666                        np->stats.tx_errors++;
1667                }
1668                pci_unmap_single(np->pci_dev,np->tx_dma[entry],
1669                                        np->tx_skbuff[entry]->len,
1670                                        PCI_DMA_TODEVICE);
1671                /* Free the original skb. */
1672                dev_kfree_skb_irq(np->tx_skbuff[entry]);
1673                np->tx_skbuff[entry] = NULL;
1674        }
1675        if (netif_queue_stopped(dev)
1676                && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
1677                /* The ring is no longer full, wake queue. */
1678                netif_wake_queue(dev);
1679        }
1680}
1681
1682/* The interrupt handler does all of the Rx thread work and cleans up
1683   after the Tx thread. */
1684static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
1685{
1686        struct net_device *dev = dev_instance;
1687        struct netdev_private *np = dev->priv;
1688        long ioaddr = dev->base_addr;
1689        int boguscnt = max_interrupt_work;
1690        unsigned int handled = 0;
1691
1692        if (np->hands_off)
1693                return IRQ_NONE;
1694        do {
1695                /* Reading automatically acknowledges all int sources. */
1696                u32 intr_status = readl(ioaddr + IntrStatus);
1697
1698                if (netif_msg_intr(np))
1699                        printk(KERN_DEBUG
1700                                "%s: Interrupt, status %#08x, mask %#08x.\n",
1701                                dev->name, intr_status,
1702                                readl(ioaddr + IntrMask));
1703
1704                if (intr_status == 0)
1705                        break;
1706                handled = 1;
1707
1708                if (intr_status &
1709                   (IntrRxDone | IntrRxIntr | RxStatusFIFOOver |
1710                    IntrRxErr | IntrRxOverrun)) {
1711                        netdev_rx(dev);
1712                }
1713
1714                if (intr_status &
1715                   (IntrTxDone | IntrTxIntr | IntrTxIdle | IntrTxErr)) {
1716                        spin_lock(&np->lock);
1717                        netdev_tx_done(dev);
1718                        spin_unlock(&np->lock);
1719                }
1720
1721                /* Abnormal error summary/uncommon events handlers. */
1722                if (intr_status & IntrAbnormalSummary)
1723                        netdev_error(dev, intr_status);
1724
1725                if (--boguscnt < 0) {
1726                        if (netif_msg_intr(np))
1727                                printk(KERN_WARNING
1728                                        "%s: Too much work at interrupt, "
1729                                        "status=%#08x.\n",
1730                                        dev->name, intr_status);
1731                        break;
1732                }
1733        } while (1);
1734
1735        if (netif_msg_intr(np))
1736                printk(KERN_DEBUG "%s: exiting interrupt.\n", dev->name);
1737
1738        return IRQ_RETVAL(handled);
1739}
1740
1741/* This routine is logically part of the interrupt handler, but separated
1742   for clarity and better register allocation. */
1743static void netdev_rx(struct net_device *dev)
1744{
1745        struct netdev_private *np = dev->priv;
1746        int entry = np->cur_rx % RX_RING_SIZE;
1747        int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1748        s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
1749
1750        /* If the driver owns the next entry it's a new packet. Send it up. */
1751        while (desc_status < 0) { /* e.g. & DescOwn */
1752                if (netif_msg_rx_status(np))
1753                        printk(KERN_DEBUG
1754                                "  netdev_rx() entry %d status was %#08x.\n",
1755                                entry, desc_status);
1756                if (--boguscnt < 0)
1757                        break;
1758                if ((desc_status&(DescMore|DescPktOK|DescRxLong)) != DescPktOK){
1759                        if (desc_status & DescMore) {
1760                                if (netif_msg_rx_err(np))
1761                                        printk(KERN_WARNING
1762                                                "%s: Oversized(?) Ethernet "
1763                                                "frame spanned multiple "
1764                                                "buffers, entry %#08x "
1765                                                "status %#08x.\n", dev->name,
1766                                                np->cur_rx, desc_status);
1767                                np->stats.rx_length_errors++;
1768                        } else {
1769                                /* There was an error. */
1770                                np->stats.rx_errors++;
1771                                if (desc_status & (DescRxAbort|DescRxOver))
1772                                        np->stats.rx_over_errors++;
1773                                if (desc_status & (DescRxLong|DescRxRunt))
1774                                        np->stats.rx_length_errors++;
1775                                if (desc_status & (DescRxInvalid|DescRxAlign))
1776                                        np->stats.rx_frame_errors++;
1777                                if (desc_status & DescRxCRC)
1778                                        np->stats.rx_crc_errors++;
1779                        }
1780                } else {
1781                        struct sk_buff *skb;
1782                        /* Omit CRC size. */
1783                        int pkt_len = (desc_status & DescSizeMask) - 4;
1784                        /* Check if the packet is long enough to accept
1785                         * without copying to a minimally-sized skbuff. */
1786                        if (pkt_len < rx_copybreak
1787                            && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1788                                skb->dev = dev;
1789                                /* 16 byte align the IP header */
1790                                skb_reserve(skb, 2);
1791                                pci_dma_sync_single(np->pci_dev,
1792                                        np->rx_dma[entry],
1793                                        np->rx_skbuff[entry]->len,
1794                                        PCI_DMA_FROMDEVICE);
1795#if HAS_IP_COPYSUM
1796                                eth_copy_and_sum(skb,
1797                                        np->rx_skbuff[entry]->tail, pkt_len, 0);
1798                                skb_put(skb, pkt_len);
1799#else
1800                                memcpy(skb_put(skb, pkt_len),
1801                                        np->rx_skbuff[entry]->tail, pkt_len);
1802#endif
1803                        } else {
1804                                pci_unmap_single(np->pci_dev, np->rx_dma[entry],
1805                                        np->rx_skbuff[entry]->len,
1806                                        PCI_DMA_FROMDEVICE);
1807                                skb_put(skb = np->rx_skbuff[entry], pkt_len);
1808                                np->rx_skbuff[entry] = NULL;
1809                        }
1810                        skb->protocol = eth_type_trans(skb, dev);
1811                        netif_rx(skb);
1812                        dev->last_rx = jiffies;
1813                        np->stats.rx_packets++;
1814                        np->stats.rx_bytes += pkt_len;
1815                }
1816                entry = (++np->cur_rx) % RX_RING_SIZE;
1817                np->rx_head_desc = &np->rx_ring[entry];
1818                desc_status = le32_to_cpu(np->rx_head_desc->cmd_status);
1819        }
1820        refill_rx(dev);
1821
1822        /* Restart Rx engine if stopped. */
1823        if (np->oom)
1824                mod_timer(&np->timer, jiffies + 1);
1825        else
1826                writel(RxOn, dev->base_addr + ChipCmd);
1827}
1828
1829static void netdev_error(struct net_device *dev, int intr_status)
1830{
1831        struct netdev_private *np = dev->priv;
1832        long ioaddr = dev->base_addr;
1833
1834        spin_lock(&np->lock);
1835        if (intr_status & LinkChange) {
1836                u16 adv = mdio_read(dev, 1, MII_ADVERTISE);
1837                u16 lpa = mdio_read(dev, 1, MII_LPA);
1838                if (mdio_read(dev, 1, MII_BMCR) & BMCR_ANENABLE
1839                 && netif_msg_link(np)) {
1840                        printk(KERN_INFO
1841                                "%s: Autonegotiation advertising"
1842                                " %#04x  partner %#04x.\n", dev->name,
1843                                adv, lpa);
1844                }
1845
1846                /* read MII int status to clear the flag */
1847                readw(ioaddr + MIntrStatus);
1848                check_link(dev);
1849        }
1850        if (intr_status & StatsMax) {
1851                __get_stats(dev);
1852        }
1853        if (intr_status & IntrTxUnderrun) {
1854                if ((np->tx_config & TxDrthMask) < 62)
1855                        np->tx_config += 2;
1856                if (netif_msg_tx_err(np))
1857                        printk(KERN_NOTICE
1858                                "%s: increased Tx threshold, txcfg %#08x.\n",
1859                                dev->name, np->tx_config);
1860                writel(np->tx_config, ioaddr + TxConfig);
1861        }
1862        if (intr_status & WOLPkt && netif_msg_wol(np)) {
1863                int wol_status = readl(ioaddr + WOLCmd);
1864                printk(KERN_NOTICE "%s: Link wake-up event %#08x\n",
1865                        dev->name, wol_status);
1866        }
1867        if (intr_status & RxStatusFIFOOver) {
1868                if (netif_msg_rx_err(np) && netif_msg_intr(np)) {
1869                        printk(KERN_NOTICE "%s: Rx status FIFO overrun\n",
1870                                dev->name);
1871                }
1872                np->stats.rx_fifo_errors++;
1873        }
1874        /* Hmmmmm, it's not clear how to recover from PCI faults. */
1875        if (intr_status & IntrPCIErr) {
1876                printk(KERN_NOTICE "%s: PCI error %#08x\n", dev->name,
1877                        intr_status & IntrPCIErr);
1878                np->stats.tx_fifo_errors++;
1879                np->stats.rx_fifo_errors++;
1880        }
1881        spin_unlock(&np->lock);
1882}
1883
1884static void __get_stats(struct net_device *dev)
1885{
1886        long ioaddr = dev->base_addr;
1887        struct netdev_private *np = dev->priv;
1888
1889        /* The chip only need report frame silently dropped. */
1890        np->stats.rx_crc_errors += readl(ioaddr + RxCRCErrs);
1891        np->stats.rx_missed_errors += readl(ioaddr + RxMissed);
1892}
1893
1894static struct net_device_stats *get_stats(struct net_device *dev)
1895{
1896        struct netdev_private *np = dev->priv;
1897
1898        /* The chip only need report frame silently dropped. */
1899        spin_lock_irq(&np->lock);
1900        if (netif_running(dev) && !np->hands_off)
1901                __get_stats(dev);
1902        spin_unlock_irq(&np->lock);
1903
1904        return &np->stats;
1905}
1906
1907#define HASH_TABLE      0x200
1908static void __set_rx_mode(struct net_device *dev)
1909{
1910        long ioaddr = dev->base_addr;
1911        struct netdev_private *np = dev->priv;
1912        u8 mc_filter[64]; /* Multicast hash filter */
1913        u32 rx_mode;
1914
1915        if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1916                /* Unconditionally log net taps. */
1917                printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
1918                        dev->name);
1919                rx_mode = RxFilterEnable | AcceptBroadcast
1920                        | AcceptAllMulticast | AcceptAllPhys | AcceptMyPhys;
1921        } else if ((dev->mc_count > multicast_filter_limit)
1922          || (dev->flags & IFF_ALLMULTI)) {
1923                rx_mode = RxFilterEnable | AcceptBroadcast
1924                        | AcceptAllMulticast | AcceptMyPhys;
1925        } else {
1926                struct dev_mc_list *mclist;
1927                int i;
1928                memset(mc_filter, 0, sizeof(mc_filter));
1929                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1930                         i++, mclist = mclist->next) {
1931                        int i = (ether_crc(ETH_ALEN, mclist->dmi_addr) >> 23) & 0x1ff;
1932                        mc_filter[i/8] |= (1 << (i & 0x07));
1933                }
1934                rx_mode = RxFilterEnable | AcceptBroadcast
1935                        | AcceptMulticast | AcceptMyPhys;
1936                for (i = 0; i < 64; i += 2) {
1937                        writew(HASH_TABLE + i, ioaddr + RxFilterAddr);
1938                        writew((mc_filter[i+1]<<8) + mc_filter[i],
1939                                ioaddr + RxFilterData);
1940                }
1941        }
1942        writel(rx_mode, ioaddr + RxFilterAddr);
1943        np->cur_rx_mode = rx_mode;
1944}
1945
1946static void set_rx_mode(struct net_device *dev)
1947{
1948        struct netdev_private *np = dev->priv;
1949        spin_lock_irq(&np->lock);
1950        if (!np->hands_off)
1951                __set_rx_mode(dev);
1952        spin_unlock_irq(&np->lock);
1953}
1954
1955static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1956{
1957        struct netdev_private *np = dev->priv;
1958        u32 cmd;
1959
1960        if (get_user(cmd, (u32 *)useraddr))
1961                return -EFAULT;
1962
1963        switch (cmd) {
1964        /* get driver info */
1965        case ETHTOOL_GDRVINFO: {
1966                struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1967                strncpy(info.driver, DRV_NAME, ETHTOOL_BUSINFO_LEN);
1968                strncpy(info.version, DRV_VERSION, ETHTOOL_BUSINFO_LEN);
1969                info.fw_version[0] = '\0';
1970                strncpy(info.bus_info, pci_name(np->pci_dev),
1971                        ETHTOOL_BUSINFO_LEN);
1972                info.eedump_len = NATSEMI_EEPROM_SIZE;
1973                info.regdump_len = NATSEMI_REGS_SIZE;
1974                if (copy_to_user(useraddr, &info, sizeof(info)))
1975                        return -EFAULT;
1976                return 0;
1977        }
1978        /* get settings */
1979        case ETHTOOL_GSET: {
1980                struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1981                spin_lock_irq(&np->lock);
1982                netdev_get_ecmd(dev, &ecmd);
1983                spin_unlock_irq(&np->lock);
1984                if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1985                        return -EFAULT;
1986                return 0;
1987        }
1988        /* set settings */
1989        case ETHTOOL_SSET: {
1990                struct ethtool_cmd ecmd;
1991                int r;
1992                if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1993                        return -EFAULT;
1994                spin_lock_irq(&np->lock);
1995                r = netdev_set_ecmd(dev, &ecmd);
1996                spin_unlock_irq(&np->lock);
1997                return r;
1998        }
1999        /* get wake-on-lan */
2000        case ETHTOOL_GWOL: {
2001                struct ethtool_wolinfo wol = {ETHTOOL_GWOL};
2002                spin_lock_irq(&np->lock);
2003                netdev_get_wol(dev, &wol.supported, &wol.wolopts);
2004                netdev_get_sopass(dev, wol.sopass);
2005                spin_unlock_irq(&np->lock);
2006                if (copy_to_user(useraddr, &wol, sizeof(wol)))
2007                        return -EFAULT;
2008                return 0;
2009        }
2010        /* set wake-on-lan */
2011        case ETHTOOL_SWOL: {
2012                struct ethtool_wolinfo wol;
2013                int r;
2014                if (copy_from_user(&wol, useraddr, sizeof(wol)))
2015                        return -EFAULT;
2016                spin_lock_irq(&np->lock);
2017                netdev_set_wol(dev, wol.wolopts);
2018                r = netdev_set_sopass(dev, wol.sopass);
2019                spin_unlock_irq(&np->lock);
2020                return r;
2021        }
2022        /* get registers */
2023        case ETHTOOL_GREGS: {
2024                struct ethtool_regs regs;
2025                u8 regbuf[NATSEMI_REGS_SIZE];
2026                int r;
2027
2028                if (copy_from_user(&regs, useraddr, sizeof(regs)))
2029                        return -EFAULT;
2030
2031                if (regs.len > NATSEMI_REGS_SIZE) {
2032                        regs.len = NATSEMI_REGS_SIZE;
2033                }
2034                regs.version = NATSEMI_REGS_VER;
2035                if (copy_to_user(useraddr, &regs, sizeof(regs)))
2036                        return -EFAULT;
2037
2038                useraddr += offsetof(struct ethtool_regs, data);
2039
2040                spin_lock_irq(&np->lock);
2041                r = netdev_get_regs(dev, regbuf);
2042                spin_unlock_irq(&np->lock);
2043
2044                if (r)
2045                        return r;
2046                if (copy_to_user(useraddr, regbuf, regs.len))
2047                        return -EFAULT;
2048                return 0;
2049        }
2050        /* get message-level */
2051        case ETHTOOL_GMSGLVL: {
2052                struct ethtool_value edata = {ETHTOOL_GMSGLVL};
2053                edata.data = np->msg_enable;
2054                if (copy_to_user(useraddr, &edata, sizeof(edata)))
2055                        return -EFAULT;
2056                return 0;
2057        }
2058        /* set message-level */
2059        case ETHTOOL_SMSGLVL: {
2060                struct ethtool_value edata;
2061                if (copy_from_user(&edata, useraddr, sizeof(edata)))
2062                        return -EFAULT;
2063                np->msg_enable = edata.data;
2064                return 0;
2065        }
2066        /* restart autonegotiation */
2067        case ETHTOOL_NWAY_RST: {
2068                int tmp;
2069                int r = -EINVAL;
2070                /* if autoneg is off, it's an error */
2071                tmp = mdio_read(dev, 1, MII_BMCR);
2072                if (tmp & BMCR_ANENABLE) {
2073                        tmp |= (BMCR_ANRESTART);
2074                        mdio_write(dev, 1, MII_BMCR, tmp);
2075                        r = 0;
2076                }
2077                return r;
2078        }
2079        /* get link status */
2080        case ETHTOOL_GLINK: {
2081                struct ethtool_value edata = {ETHTOOL_GLINK};
2082                /* LSTATUS is latched low until a read - so read twice */
2083                mdio_read(dev, 1, MII_BMSR);
2084                edata.data = (mdio_read(dev, 1, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
2085                if (copy_to_user(useraddr, &edata, sizeof(edata)))
2086                        return -EFAULT;
2087                return 0;
2088        }
2089        /* get EEPROM */
2090        case ETHTOOL_GEEPROM: {
2091                struct ethtool_eeprom eeprom;
2092                u8 eebuf[NATSEMI_EEPROM_SIZE];
2093                int r;
2094
2095                if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
2096                        return -EFAULT;
2097
2098                if (eeprom.offset > eeprom.offset+eeprom.len)
2099                        return -EINVAL;
2100
2101                if ((eeprom.offset+eeprom.len) > NATSEMI_EEPROM_SIZE) {
2102                        eeprom.len = NATSEMI_EEPROM_SIZE-eeprom.offset;
2103                }
2104                eeprom.magic = PCI_VENDOR_ID_NS | (PCI_DEVICE_ID_NS_83815<<16);
2105                if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
2106                        return -EFAULT;
2107
2108                useraddr += offsetof(struct ethtool_eeprom, data);
2109
2110                spin_lock_irq(&np->lock);
2111                r = netdev_get_eeprom(dev, eebuf);
2112                spin_unlock_irq(&np->lock);
2113
2114                if (r)
2115                        return r;
2116                if (copy_to_user(useraddr, eebuf+eeprom.offset, eeprom.len))
2117                        return -EFAULT;
2118                return 0;
2119        }
2120
2121        }
2122
2123        return -EOPNOTSUPP;
2124}
2125
2126static int netdev_set_wol(struct net_device *dev, u32 newval)
2127{
2128        struct netdev_private *np = dev->priv;
2129        u32 data = readl(dev->base_addr + WOLCmd) & ~WakeOptsSummary;
2130
2131        /* translate to bitmasks this chip understands */
2132        if (newval & WAKE_PHY)
2133                data |= WakePhy;
2134        if (newval & WAKE_UCAST)
2135                data |= WakeUnicast;
2136        if (newval & WAKE_MCAST)
2137                data |= WakeMulticast;
2138        if (newval & WAKE_BCAST)
2139                data |= WakeBroadcast;
2140        if (newval & WAKE_ARP)
2141                data |= WakeArp;
2142        if (newval & WAKE_MAGIC)
2143                data |= WakeMagic;
2144        if (np->srr >= SRR_DP83815_D) {
2145                if (newval & WAKE_MAGICSECURE) {
2146                        data |= WakeMagicSecure;
2147                }
2148        }
2149
2150        writel(data, dev->base_addr + WOLCmd);
2151
2152        return 0;
2153}
2154
2155static int netdev_get_wol(struct net_device *dev, u32 *supported, u32 *cur)
2156{
2157        struct netdev_private *np = dev->priv;
2158        u32 regval = readl(dev->base_addr + WOLCmd);
2159
2160        *supported = (WAKE_PHY | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST
2161                        | WAKE_ARP | WAKE_MAGIC);
2162
2163        if (np->srr >= SRR_DP83815_D) {
2164                /* SOPASS works on revD and higher */
2165                *supported |= WAKE_MAGICSECURE;
2166        }
2167        *cur = 0;
2168
2169        /* translate from chip bitmasks */
2170        if (regval & WakePhy)
2171                *cur |= WAKE_PHY;
2172        if (regval & WakeUnicast)
2173                *cur |= WAKE_UCAST;
2174        if (regval & WakeMulticast)
2175                *cur |= WAKE_MCAST;
2176        if (regval & WakeBroadcast)
2177                *cur |= WAKE_BCAST;
2178        if (regval & WakeArp)
2179                *cur |= WAKE_ARP;
2180        if (regval & WakeMagic)
2181                *cur |= WAKE_MAGIC;
2182        if (regval & WakeMagicSecure) {
2183                /* this can be on in revC, but it's broken */
2184                *cur |= WAKE_MAGICSECURE;
2185        }
2186
2187        return 0;
2188}
2189
2190static int netdev_set_sopass(struct net_device *dev, u8 *newval)
2191{
2192        struct netdev_private *np = dev->priv;
2193        u16 *sval = (u16 *)newval;
2194        u32 addr;
2195
2196        if (np->srr < SRR_DP83815_D) {
2197                return 0;
2198        }
2199
2200        /* enable writing to these registers by disabling the RX filter */
2201        addr = readl(dev->base_addr + RxFilterAddr) & ~RFCRAddressMask;
2202        addr &= ~RxFilterEnable;
2203        writel(addr, dev->base_addr + RxFilterAddr);
2204
2205        /* write the three words to (undocumented) RFCR vals 0xa, 0xc, 0xe */
2206        writel(addr | 0xa, dev->base_addr + RxFilterAddr);
2207        writew(sval[0], dev->base_addr + RxFilterData);
2208
2209        writel(addr | 0xc, dev->base_addr + RxFilterAddr);
2210        writew(sval[1], dev->base_addr + RxFilterData);
2211
2212        writel(addr | 0xe, dev->base_addr + RxFilterAddr);
2213        writew(sval[2], dev->base_addr + RxFilterData);
2214
2215        /* re-enable the RX filter */
2216        writel(addr | RxFilterEnable, dev->base_addr + RxFilterAddr);
2217
2218        return 0;
2219}
2220
2221static int netdev_get_sopass(struct net_device *dev, u8 *data)
2222{
2223        struct netdev_private *np = dev->priv;
2224        u16 *sval = (u16 *)data;
2225        u32 addr;
2226
2227        if (np->srr < SRR_DP83815_D) {
2228                sval[0] = sval[1] = sval[2] = 0;
2229                return 0;
2230        }
2231
2232        /* read the three words from (undocumented) RFCR vals 0xa, 0xc, 0xe */
2233        addr = readl(dev->base_addr + RxFilterAddr) & ~RFCRAddressMask;
2234
2235        writel(addr | 0xa, dev->base_addr + RxFilterAddr);
2236        sval[0] = readw(dev->base_addr + RxFilterData);
2237
2238        writel(addr | 0xc, dev->base_addr + RxFilterAddr);
2239        sval[1] = readw(dev->base_addr + RxFilterData);
2240
2241        writel(addr | 0xe, dev->base_addr + RxFilterAddr);
2242        sval[2] = readw(dev->base_addr + RxFilterData);
2243
2244        writel(addr, dev->base_addr + RxFilterAddr);
2245
2246        return 0;
2247}
2248
2249static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2250{
2251        u32 tmp;
2252
2253        ecmd->supported =
2254                (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2255                SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2256                SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
2257
2258        /* only supports twisted-pair or MII */
2259        tmp = readl(dev->base_addr + ChipConfig);
2260        if (tmp & CfgExtPhy)
2261                ecmd->port = PORT_MII;
2262        else
2263                ecmd->port = PORT_TP;
2264
2265        /* only supports internal transceiver */
2266        ecmd->transceiver = XCVR_INTERNAL;
2267
2268        /* not sure what this is for */
2269        ecmd->phy_address = readw(dev->base_addr + PhyCtrl) & PhyAddrMask;
2270
2271        ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
2272        tmp = mdio_read(dev, 1, MII_ADVERTISE);
2273        if (tmp & ADVERTISE_10HALF)
2274                ecmd->advertising |= ADVERTISED_10baseT_Half;
2275        if (tmp & ADVERTISE_10FULL)
2276                ecmd->advertising |= ADVERTISED_10baseT_Full;
2277        if (tmp & ADVERTISE_100HALF)
2278                ecmd->advertising |= ADVERTISED_100baseT_Half;
2279        if (tmp & ADVERTISE_100FULL)
2280                ecmd->advertising |= ADVERTISED_100baseT_Full;
2281
2282        tmp = mdio_read(dev, 1, MII_BMCR);
2283        if (tmp & BMCR_ANENABLE) {
2284                ecmd->advertising |= ADVERTISED_Autoneg;
2285                ecmd->autoneg = AUTONEG_ENABLE;
2286        } else {
2287                ecmd->autoneg = AUTONEG_DISABLE;
2288        }
2289
2290        tmp = readl(dev->base_addr + ChipConfig);
2291        if (tmp & CfgSpeed100) {
2292                ecmd->speed = SPEED_100;
2293        } else {
2294                ecmd->speed = SPEED_10;
2295        }
2296
2297        if (tmp & CfgFullDuplex) {
2298                ecmd->duplex = DUPLEX_FULL;
2299        } else {
2300                ecmd->duplex = DUPLEX_HALF;
2301        }
2302
2303        /* ignore maxtxpkt, maxrxpkt for now */
2304
2305        return 0;
2306}
2307
2308static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
2309{
2310        struct netdev_private *np = dev->priv;
2311        u32 tmp;
2312
2313        if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
2314                return -EINVAL;
2315        if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
2316                return -EINVAL;
2317        if (ecmd->port != PORT_TP && ecmd->port != PORT_MII)
2318                return -EINVAL;
2319        if (ecmd->transceiver != XCVR_INTERNAL)
2320                return -EINVAL;
2321        if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
2322                return -EINVAL;
2323        /* ignore phy_address, maxtxpkt, maxrxpkt for now */
2324
2325        /* WHEW! now lets bang some bits */
2326
2327        tmp = mdio_read(dev, 1, MII_BMCR);
2328        if (ecmd->autoneg == AUTONEG_ENABLE) {
2329                /* turn on autonegotiation */
2330                tmp |= BMCR_ANENABLE;
2331                np->advertising = mdio_read(dev, 1, MII_ADVERTISE);
2332        } else {
2333                /* turn off auto negotiation, set speed and duplexity */
2334                tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
2335                if (ecmd->speed == SPEED_100)
2336                        tmp |= BMCR_SPEED100;
2337                if (ecmd->duplex == DUPLEX_FULL)
2338                        tmp |= BMCR_FULLDPLX;
2339                else
2340                        np->full_duplex = 0;
2341        }
2342        mdio_write(dev, 1, MII_BMCR, tmp);
2343        return 0;
2344}
2345
2346static int netdev_get_regs(struct net_device *dev, u8 *buf)
2347{
2348        int i;
2349        int j;
2350        u32 rfcr;
2351        u32 *rbuf = (u32 *)buf;
2352
2353        /* read all of page 0 of registers */
2354        for (i = 0; i < NATSEMI_PG0_NREGS; i++) {
2355                rbuf[i] = readl(dev->base_addr + i*4);
2356        }
2357
2358        /* read only the 'magic' registers from page 1 */
2359        writew(1, dev->base_addr + PGSEL);
2360        rbuf[i++] = readw(dev->base_addr + PMDCSR);
2361        rbuf[i++] = readw(dev->base_addr + TSTDAT);
2362        rbuf[i++] = readw(dev->base_addr + DSPCFG);
2363        rbuf[i++] = readw(dev->base_addr + SDCFG);
2364        writew(0, dev->base_addr + PGSEL);
2365
2366        /* read RFCR indexed registers */
2367        rfcr = readl(dev->base_addr + RxFilterAddr);
2368        for (j = 0; j < NATSEMI_RFDR_NREGS; j++) {
2369                writel(j*2, dev->base_addr + RxFilterAddr);
2370                rbuf[i++] = readw(dev->base_addr + RxFilterData);
2371        }
2372        writel(rfcr, dev->base_addr + RxFilterAddr);
2373
2374        /* the interrupt status is clear-on-read - see if we missed any */
2375        if (rbuf[4] & rbuf[5]) {
2376                printk(KERN_WARNING
2377                        "%s: shoot, we dropped an interrupt (%#08x)\n",
2378                        dev->name, rbuf[4] & rbuf[5]);
2379        }
2380
2381        return 0;
2382}
2383
2384#define SWAP_BITS(x)    ( (((x) & 0x0001) << 15) | (((x) & 0x0002) << 13) \
2385                        | (((x) & 0x0004) << 11) | (((x) & 0x0008) << 9)  \
2386                        | (((x) & 0x0010) << 7)  | (((x) & 0x0020) << 5)  \
2387                        | (((x) & 0x0040) << 3)  | (((x) & 0x0080) << 1)  \
2388                        | (((x) & 0x0100) >> 1)  | (((x) & 0x0200) >> 3)  \
2389                        | (((x) & 0x0400) >> 5)  | (((x) & 0x0800) >> 7)  \
2390                        | (((x) & 0x1000) >> 9)  | (((x) & 0x2000) >> 11) \
2391                        | (((x) & 0x4000) >> 13) | (((x) & 0x8000) >> 15) )
2392
2393static int netdev_get_eeprom(struct net_device *dev, u8 *buf)
2394{
2395        int i;
2396        u16 *ebuf = (u16 *)buf;
2397
2398        /* eeprom_read reads 16 bits, and indexes by 16 bits */
2399        for (i = 0; i < NATSEMI_EEPROM_SIZE/2; i++) {
2400                ebuf[i] = eeprom_read(dev->base_addr, i);
2401                /* The EEPROM itself stores data bit-swapped, but eeprom_read
2402                 * reads it back "sanely". So we swap it back here in order to
2403                 * present it to userland as it is stored. */
2404                ebuf[i] = SWAP_BITS(ebuf[i]);
2405        }
2406        return 0;
2407}
2408
2409static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2410{
2411        struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
2412
2413        switch(cmd) {
2414        case SIOCETHTOOL:
2415                return netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
2416        case SIOCGMIIPHY:               /* Get address of MII PHY in use. */
2417        case SIOCDEVPRIVATE:            /* for binary compat, remove in 2.5 */
2418                data->phy_id = 1;
2419                /* Fall Through */
2420
2421        case SIOCGMIIREG:               /* Read MII PHY register. */
2422        case SIOCDEVPRIVATE+1:          /* for binary compat, remove in 2.5 */
2423                data->val_out = mdio_read(dev, data->phy_id & 0x1f,
2424                        data->reg_num & 0x1f);
2425                return 0;
2426
2427        case SIOCSMIIREG:               /* Write MII PHY register. */
2428        case SIOCDEVPRIVATE+2:          /* for binary compat, remove in 2.5 */
2429                if (!capable(CAP_NET_ADMIN))
2430                        return -EPERM;
2431                mdio_write(dev, data->phy_id & 0x1f, data->reg_num & 0x1f,
2432                        data->val_in);
2433                return 0;
2434        default:
2435                return -EOPNOTSUPP;
2436        }
2437}
2438
2439static void enable_wol_mode(struct net_device *dev, int enable_intr)
2440{
2441        long ioaddr = dev->base_addr;
2442        struct netdev_private *np = dev->priv;
2443
2444        if (netif_msg_wol(np))
2445                printk(KERN_INFO "%s: remaining active for wake-on-lan\n",
2446                        dev->name);
2447
2448        /* For WOL we must restart the rx process in silent mode.
2449         * Write NULL to the RxRingPtr. Only possible if
2450         * rx process is stopped
2451         */
2452        writel(0, ioaddr + RxRingPtr);
2453
2454        /* read WoL status to clear */
2455        readl(ioaddr + WOLCmd);
2456
2457        /* PME on, clear status */
2458        writel(np->SavedClkRun | PMEEnable | PMEStatus, ioaddr + ClkRun);
2459
2460        /* and restart the rx process */
2461        writel(RxOn, ioaddr + ChipCmd);
2462
2463        if (enable_intr) {
2464                /* enable the WOL interrupt.
2465                 * Could be used to send a netlink message.
2466                 */
2467                writel(WOLPkt | LinkChange, ioaddr + IntrMask);
2468                writel(1, ioaddr + IntrEnable);
2469        }
2470}
2471
2472static int netdev_close(struct net_device *dev)
2473{
2474        long ioaddr = dev->base_addr;
2475        struct netdev_private *np = dev->priv;
2476
2477        if (netif_msg_ifdown(np))
2478                printk(KERN_DEBUG
2479                        "%s: Shutting down ethercard, status was %#04x.\n",
2480                        dev->name, (int)readl(ioaddr + ChipCmd));
2481        if (netif_msg_pktdata(np))
2482                printk(KERN_DEBUG
2483                        "%s: Queue pointers were Tx %d / %d,  Rx %d / %d.\n",
2484                        dev->name, np->cur_tx, np->dirty_tx,
2485                        np->cur_rx, np->dirty_rx);
2486
2487        /*
2488         * FIXME: what if someone tries to close a device
2489         * that is suspended?
2490         * Should we reenable the nic to switch to
2491         * the final WOL settings?
2492         */
2493
2494        del_timer_sync(&np->timer);
2495        disable_irq(dev->irq);
2496        spin_lock_irq(&np->lock);
2497        /* Disable interrupts, and flush posted writes */
2498        writel(0, ioaddr + IntrEnable);
2499        readl(ioaddr + IntrEnable);
2500        np->hands_off = 1;
2501        spin_unlock_irq(&np->lock);
2502        enable_irq(dev->irq);
2503
2504        free_irq(dev->irq, dev);
2505
2506        /* Interrupt disabled, interrupt handler released,
2507         * queue stopped, timer deleted, rtnl_lock held
2508         * All async codepaths that access the driver are disabled.
2509         */
2510        spin_lock_irq(&np->lock);
2511        np->hands_off = 0;
2512        readl(ioaddr + IntrMask);
2513        readw(ioaddr + MIntrStatus);
2514
2515        /* Freeze Stats */
2516        writel(StatsFreeze, ioaddr + StatsCtrl);
2517
2518        /* Stop the chip's Tx and Rx processes. */
2519        natsemi_stop_rxtx(dev);
2520
2521        __get_stats(dev);
2522        spin_unlock_irq(&np->lock);
2523
2524        /* clear the carrier last - an interrupt could reenable it otherwise */
2525        netif_carrier_off(dev);
2526        netif_stop_queue(dev);
2527
2528        dump_ring(dev);
2529        drain_ring(dev);
2530        free_ring(dev);
2531
2532        {
2533                u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
2534                if (wol) {
2535                        /* restart the NIC in WOL mode.
2536                         * The nic must be stopped for this.
2537                         */
2538                        enable_wol_mode(dev, 0);
2539                } else {
2540                        /* Restore PME enable bit unmolested */
2541                        writel(np->SavedClkRun, ioaddr + ClkRun);
2542                }
2543        }
2544        return 0;
2545}
2546
2547
2548static void __devexit natsemi_remove1 (struct pci_dev *pdev)
2549{
2550        struct net_device *dev = pci_get_drvdata(pdev);
2551
2552        unregister_netdev (dev);
2553        pci_release_regions (pdev);
2554        iounmap ((char *) dev->base_addr);
2555        free_netdev (dev);
2556        pci_set_drvdata(pdev, NULL);
2557}
2558
2559#ifdef CONFIG_PM
2560
2561/*
2562 * The ns83815 chip doesn't have explicit RxStop bits.
2563 * Kicking the Rx or Tx process for a new packet reenables the Rx process
2564 * of the nic, thus this function must be very careful:
2565 *
2566 * suspend/resume synchronization:
2567 * entry points:
2568 *   netdev_open, netdev_close, netdev_ioctl, set_rx_mode, intr_handler,
2569 *   start_tx, tx_timeout
2570 *
2571 * No function accesses the hardware without checking np->hands_off.
2572 *      the check occurs under spin_lock_irq(&np->lock);
2573 * exceptions:
2574 *      * netdev_ioctl: noncritical access.
2575 *      * netdev_open: cannot happen due to the device_detach
2576 *      * netdev_close: doesn't hurt.
2577 *      * netdev_timer: timer stopped by natsemi_suspend.
2578 *      * intr_handler: doesn't acquire the spinlock. suspend calls
2579 *              disable_irq() to enforce synchronization.
2580 *
2581 * Interrupts must be disabled, otherwise hands_off can cause irq storms.
2582 */
2583
2584static int natsemi_suspend (struct pci_dev *pdev, u32 state)
2585{
2586        struct net_device *dev = pci_get_drvdata (pdev);
2587        struct netdev_private *np = dev->priv;
2588        long ioaddr = dev->base_addr;
2589
2590        rtnl_lock();
2591        if (netif_running (dev)) {
2592                del_timer_sync(&np->timer);
2593
2594                disable_irq(dev->irq);
2595                spin_lock_irq(&np->lock);
2596
2597                writel(0, ioaddr + IntrEnable);
2598                np->hands_off = 1;
2599                natsemi_stop_rxtx(dev);
2600                netif_stop_queue(dev);
2601
2602                spin_unlock_irq(&np->lock);
2603                enable_irq(dev->irq);
2604
2605                /* Update the error counts. */
2606                __get_stats(dev);
2607
2608                /* pci_power_off(pdev, -1); */
2609                drain_ring(dev);
2610                {
2611                        u32 wol = readl(ioaddr + WOLCmd) & WakeOptsSummary;
2612                        /* Restore PME enable bit */
2613                        if (wol) {
2614                                /* restart the NIC in WOL mode.
2615                                 * The nic must be stopped for this.
2616                                 * FIXME: use the WOL interrupt
2617                                 */
2618                                enable_wol_mode(dev, 0);
2619                        } else {
2620                                /* Restore PME enable bit unmolested */
2621                                writel(np->SavedClkRun, ioaddr + ClkRun);
2622                        }
2623                }
2624        }
2625        netif_device_detach(dev);
2626        rtnl_unlock();
2627        return 0;
2628}
2629
2630
2631static int natsemi_resume (struct pci_dev *pdev)
2632{
2633        struct net_device *dev = pci_get_drvdata (pdev);
2634        struct netdev_private *np = dev->priv;
2635
2636        rtnl_lock();
2637        if (netif_device_present(dev))
2638                goto out;
2639        if (netif_running(dev)) {
2640                BUG_ON(!np->hands_off);
2641                pci_enable_device(pdev);
2642        /*      pci_power_on(pdev); */
2643
2644                natsemi_reset(dev);
2645                init_ring(dev);
2646                disable_irq(dev->irq);
2647                spin_lock_irq(&np->lock);
2648                np->hands_off = 0;
2649                init_registers(dev);
2650                netif_device_attach(dev);
2651                spin_unlock_irq(&np->lock);
2652                enable_irq(dev->irq);
2653
2654                mod_timer(&np->timer, jiffies + 1*HZ);
2655        }
2656        netif_device_attach(dev);
2657out:
2658        rtnl_unlock();
2659        return 0;
2660}
2661
2662#endif /* CONFIG_PM */
2663
2664static struct pci_driver natsemi_driver = {
2665        .name           = DRV_NAME,
2666        .id_table       = natsemi_pci_tbl,
2667        .probe          = natsemi_probe1,
2668        .remove         = __devexit_p(natsemi_remove1),
2669#ifdef CONFIG_PM
2670        .suspend        = natsemi_suspend,
2671        .resume         = natsemi_resume,
2672#endif
2673};
2674
2675static int __init natsemi_init_mod (void)
2676{
2677/* when a module, this is printed whether or not devices are found in probe */
2678#ifdef MODULE
2679        printk(version);
2680#endif
2681
2682        return pci_module_init (&natsemi_driver);
2683}
2684
2685static void __exit natsemi_exit_mod (void)
2686{
2687        pci_unregister_driver (&natsemi_driver);
2688}
2689
2690module_init(natsemi_init_mod);
2691module_exit(natsemi_exit_mod);
2692
2693
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.