linux-bk/drivers/net/8139cp.c
<<
>>
Prefs
   1/* 8139cp.c: A Linux PCI Ethernet driver for the RealTek 8139C+ chips. */
   2/*
   3        Copyright 2001,2002 Jeff Garzik <jgarzik@pobox.com>
   4
   5        Copyright (C) 2001, 2002 David S. Miller (davem@redhat.com) [tg3.c]
   6        Copyright (C) 2000, 2001 David S. Miller (davem@redhat.com) [sungem.c]
   7        Copyright 2001 Manfred Spraul                               [natsemi.c]
   8        Copyright 1999-2001 by Donald Becker.                       [natsemi.c]
   9        Written 1997-2001 by Donald Becker.                         [8139too.c]
  10        Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>. [acenic.c]
  11
  12        This software may be used and distributed according to the terms of
  13        the GNU General Public License (GPL), incorporated herein by reference.
  14        Drivers based on or derived from this code fall under the GPL and must
  15        retain the authorship, copyright and license notice.  This file is not
  16        a complete program and may only be used when the entire operating
  17        system is licensed under the GPL.
  18
  19        See the file COPYING in this distribution for more information.
  20
  21        Contributors:
  22        
  23                Wake-on-LAN support - Felipe Damasio <felipewd@terra.com.br>
  24                PCI suspend/resume  - Felipe Damasio <felipewd@terra.com.br>
  25                LinkChg interrupt   - Felipe Damasio <felipewd@terra.com.br>
  26                        
  27        TODO:
  28        * Test Tx checksumming thoroughly
  29        * Implement dev->tx_timeout
  30
  31        Low priority TODO:
  32        * Complete reset on PciErr
  33        * Consider Rx interrupt mitigation using TimerIntr
  34        * Investigate using skb->priority with h/w VLAN priority
  35        * Investigate using High Priority Tx Queue with skb->priority
  36        * Adjust Rx FIFO threshold and Max Rx DMA burst on Rx FIFO error
  37        * Adjust Tx FIFO threshold and Max Tx DMA burst on Tx FIFO error
  38        * Implement Tx software interrupt mitigation via
  39          Tx descriptor bit
  40        * The real minimum of CP_MIN_MTU is 4 bytes.  However,
  41          for this to be supported, one must(?) turn on packet padding.
  42        * Support external MII transceivers (patch available)
  43
  44        NOTES:
  45        * TX checksumming is considered experimental.  It is off by
  46          default, use ethtool to turn it on.
  47
  48 */
  49
  50#define DRV_NAME                "8139cp"
  51#define DRV_VERSION             "1.1"
  52#define DRV_RELDATE             "Aug 30, 2003"
  53
  54
  55#include <linux/config.h>
  56#include <linux/module.h>
  57#include <linux/kernel.h>
  58#include <linux/compiler.h>
  59#include <linux/netdevice.h>
  60#include <linux/etherdevice.h>
  61#include <linux/init.h>
  62#include <linux/pci.h>
  63#include <linux/delay.h>
  64#include <linux/ethtool.h>
  65#include <linux/mii.h>
  66#include <linux/if_vlan.h>
  67#include <linux/crc32.h>
  68#include <linux/in.h>
  69#include <linux/ip.h>
  70#include <linux/tcp.h>
  71#include <linux/udp.h>
  72#include <asm/io.h>
  73#include <asm/uaccess.h>
  74
  75/* VLAN tagging feature enable/disable */
  76#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  77#define CP_VLAN_TAG_USED 1
  78#define CP_VLAN_TX_TAG(tx_desc,vlan_tag_value) \
  79        do { (tx_desc)->opts2 = (vlan_tag_value); } while (0)
  80#else
  81#define CP_VLAN_TAG_USED 0
  82#define CP_VLAN_TX_TAG(tx_desc,vlan_tag_value) \
  83        do { (tx_desc)->opts2 = 0; } while (0)
  84#endif
  85
  86/* These identify the driver base version and may not be removed. */
  87static char version[] =
  88KERN_INFO DRV_NAME ": 10/100 PCI Ethernet driver v" DRV_VERSION " (" DRV_RELDATE ")\n";
  89
  90MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
  91MODULE_DESCRIPTION("RealTek RTL-8139C+ series 10/100 PCI Ethernet driver");
  92MODULE_LICENSE("GPL");
  93
  94static int debug = -1;
  95MODULE_PARM (debug, "i");
  96MODULE_PARM_DESC (debug, "8139cp: bitmapped message enable number");
  97
  98/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  99   The RTL chips use a 64 element hash table based on the Ethernet CRC.  */
 100static int multicast_filter_limit = 32;
 101MODULE_PARM (multicast_filter_limit, "i");
 102MODULE_PARM_DESC (multicast_filter_limit, "8139cp: maximum number of filtered multicast addresses");
 103
 104#define PFX                     DRV_NAME ": "
 105
 106#ifndef TRUE
 107#define FALSE 0
 108#define TRUE (!FALSE)
 109#endif
 110
 111#define CP_DEF_MSG_ENABLE       (NETIF_MSG_DRV          | \
 112                                 NETIF_MSG_PROBE        | \
 113                                 NETIF_MSG_LINK)
 114#define CP_NUM_STATS            14      /* struct cp_dma_stats, plus one */
 115#define CP_STATS_SIZE           64      /* size in bytes of DMA stats block */
 116#define CP_REGS_SIZE            (0xff + 1)
 117#define CP_REGS_VER             1               /* version 1 */
 118#define CP_RX_RING_SIZE         64
 119#define CP_TX_RING_SIZE         64
 120#define CP_RING_BYTES           \
 121                ((sizeof(struct cp_desc) * CP_RX_RING_SIZE) +   \
 122                 (sizeof(struct cp_desc) * CP_TX_RING_SIZE) +   \
 123                 CP_STATS_SIZE)
 124#define NEXT_TX(N)              (((N) + 1) & (CP_TX_RING_SIZE - 1))
 125#define NEXT_RX(N)              (((N) + 1) & (CP_RX_RING_SIZE - 1))
 126#define TX_BUFFS_AVAIL(CP)                                      \
 127        (((CP)->tx_tail <= (CP)->tx_head) ?                     \
 128          (CP)->tx_tail + (CP_TX_RING_SIZE - 1) - (CP)->tx_head :       \
 129          (CP)->tx_tail - (CP)->tx_head - 1)
 130
 131#define PKT_BUF_SZ              1536    /* Size of each temporary Rx buffer.*/
 132#define RX_OFFSET               2
 133#define CP_INTERNAL_PHY         32
 134
 135/* The following settings are log_2(bytes)-4:  0 == 16 bytes .. 6==1024, 7==end of packet. */
 136#define RX_FIFO_THRESH          5       /* Rx buffer level before first PCI xfer.  */
 137#define RX_DMA_BURST            4       /* Maximum PCI burst, '4' is 256 */
 138#define TX_DMA_BURST            6       /* Maximum PCI burst, '6' is 1024 */
 139#define TX_EARLY_THRESH         256     /* Early Tx threshold, in bytes */
 140
 141/* Time in jiffies before concluding the transmitter is hung. */
 142#define TX_TIMEOUT              (6*HZ)
 143
 144/* hardware minimum and maximum for a single frame's data payload */
 145#define CP_MIN_MTU              60      /* TODO: allow lower, but pad */
 146#define CP_MAX_MTU              4096
 147
 148enum {
 149        /* NIC register offsets */
 150        MAC0            = 0x00, /* Ethernet hardware address. */
 151        MAR0            = 0x08, /* Multicast filter. */
 152        StatsAddr       = 0x10, /* 64-bit start addr of 64-byte DMA stats blk */
 153        TxRingAddr      = 0x20, /* 64-bit start addr of Tx ring */
 154        HiTxRingAddr    = 0x28, /* 64-bit start addr of high priority Tx ring */
 155        Cmd             = 0x37, /* Command register */
 156        IntrMask        = 0x3C, /* Interrupt mask */
 157        IntrStatus      = 0x3E, /* Interrupt status */
 158        TxConfig        = 0x40, /* Tx configuration */
 159        ChipVersion     = 0x43, /* 8-bit chip version, inside TxConfig */
 160        RxConfig        = 0x44, /* Rx configuration */
 161        RxMissed        = 0x4C, /* 24 bits valid, write clears */
 162        Cfg9346         = 0x50, /* EEPROM select/control; Cfg reg [un]lock */
 163        Config1         = 0x52, /* Config1 */
 164        Config3         = 0x59, /* Config3 */
 165        Config4         = 0x5A, /* Config4 */
 166        MultiIntr       = 0x5C, /* Multiple interrupt select */
 167        BasicModeCtrl   = 0x62, /* MII BMCR */
 168        BasicModeStatus = 0x64, /* MII BMSR */
 169        NWayAdvert      = 0x66, /* MII ADVERTISE */
 170        NWayLPAR        = 0x68, /* MII LPA */
 171        NWayExpansion   = 0x6A, /* MII Expansion */
 172        Config5         = 0xD8, /* Config5 */
 173        TxPoll          = 0xD9, /* Tell chip to check Tx descriptors for work */
 174        RxMaxSize       = 0xDA, /* Max size of an Rx packet (8169 only) */
 175        CpCmd           = 0xE0, /* C+ Command register (C+ mode only) */
 176        IntrMitigate    = 0xE2, /* rx/tx interrupt mitigation control */
 177        RxRingAddr      = 0xE4, /* 64-bit start addr of Rx ring */
 178        TxThresh        = 0xEC, /* Early Tx threshold */
 179        OldRxBufAddr    = 0x30, /* DMA address of Rx ring buffer (C mode) */
 180        OldTSD0         = 0x10, /* DMA address of first Tx desc (C mode) */
 181
 182        /* Tx and Rx status descriptors */
 183        DescOwn         = (1 << 31), /* Descriptor is owned by NIC */
 184        RingEnd         = (1 << 30), /* End of descriptor ring */
 185        FirstFrag       = (1 << 29), /* First segment of a packet */
 186        LastFrag        = (1 << 28), /* Final segment of a packet */
 187        TxError         = (1 << 23), /* Tx error summary */
 188        RxError         = (1 << 20), /* Rx error summary */
 189        IPCS            = (1 << 18), /* Calculate IP checksum */
 190        UDPCS           = (1 << 17), /* Calculate UDP/IP checksum */
 191        TCPCS           = (1 << 16), /* Calculate TCP/IP checksum */
 192        TxVlanTag       = (1 << 17), /* Add VLAN tag */
 193        RxVlanTagged    = (1 << 16), /* Rx VLAN tag available */
 194        IPFail          = (1 << 15), /* IP checksum failed */
 195        UDPFail         = (1 << 14), /* UDP/IP checksum failed */
 196        TCPFail         = (1 << 13), /* TCP/IP checksum failed */
 197        NormalTxPoll    = (1 << 6),  /* One or more normal Tx packets to send */
 198        PID1            = (1 << 17), /* 2 protocol id bits:  0==non-IP, */
 199        PID0            = (1 << 16), /* 1==UDP/IP, 2==TCP/IP, 3==IP */
 200        RxProtoTCP      = 1,
 201        RxProtoUDP      = 2,
 202        RxProtoIP       = 3,
 203        TxFIFOUnder     = (1 << 25), /* Tx FIFO underrun */
 204        TxOWC           = (1 << 22), /* Tx Out-of-window collision */
 205        TxLinkFail      = (1 << 21), /* Link failed during Tx of packet */
 206        TxMaxCol        = (1 << 20), /* Tx aborted due to excessive collisions */
 207        TxColCntShift   = 16,        /* Shift, to get 4-bit Tx collision cnt */
 208        TxColCntMask    = 0x01 | 0x02 | 0x04 | 0x08, /* 4-bit collision count */
 209        RxErrFrame      = (1 << 27), /* Rx frame alignment error */
 210        RxMcast         = (1 << 26), /* Rx multicast packet rcv'd */
 211        RxErrCRC        = (1 << 18), /* Rx CRC error */
 212        RxErrRunt       = (1 << 19), /* Rx error, packet < 64 bytes */
 213        RxErrLong       = (1 << 21), /* Rx error, packet > 4096 bytes */
 214        RxErrFIFO       = (1 << 22), /* Rx error, FIFO overflowed, pkt bad */
 215
 216        /* StatsAddr register */
 217        DumpStats       = (1 << 3),  /* Begin stats dump */
 218
 219        /* RxConfig register */
 220        RxCfgFIFOShift  = 13,        /* Shift, to get Rx FIFO thresh value */
 221        RxCfgDMAShift   = 8,         /* Shift, to get Rx Max DMA value */
 222        AcceptErr       = 0x20,      /* Accept packets with CRC errors */
 223        AcceptRunt      = 0x10,      /* Accept runt (<64 bytes) packets */
 224        AcceptBroadcast = 0x08,      /* Accept broadcast packets */
 225        AcceptMulticast = 0x04,      /* Accept multicast packets */
 226        AcceptMyPhys    = 0x02,      /* Accept pkts with our MAC as dest */
 227        AcceptAllPhys   = 0x01,      /* Accept all pkts w/ physical dest */
 228
 229        /* IntrMask / IntrStatus registers */
 230        PciErr          = (1 << 15), /* System error on the PCI bus */
 231        TimerIntr       = (1 << 14), /* Asserted when TCTR reaches TimerInt value */
 232        LenChg          = (1 << 13), /* Cable length change */
 233        SWInt           = (1 << 8),  /* Software-requested interrupt */
 234        TxEmpty         = (1 << 7),  /* No Tx descriptors available */
 235        RxFIFOOvr       = (1 << 6),  /* Rx FIFO Overflow */
 236        LinkChg         = (1 << 5),  /* Packet underrun, or link change */
 237        RxEmpty         = (1 << 4),  /* No Rx descriptors available */
 238        TxErr           = (1 << 3),  /* Tx error */
 239        TxOK            = (1 << 2),  /* Tx packet sent */
 240        RxErr           = (1 << 1),  /* Rx error */
 241        RxOK            = (1 << 0),  /* Rx packet received */
 242        IntrResvd       = (1 << 10), /* reserved, according to RealTek engineers,
 243                                        but hardware likes to raise it */
 244
 245        IntrAll         = PciErr | TimerIntr | LenChg | SWInt | TxEmpty |
 246                          RxFIFOOvr | LinkChg | RxEmpty | TxErr | TxOK |
 247                          RxErr | RxOK | IntrResvd,
 248
 249        /* C mode command register */
 250        CmdReset        = (1 << 4),  /* Enable to reset; self-clearing */
 251        RxOn            = (1 << 3),  /* Rx mode enable */
 252        TxOn            = (1 << 2),  /* Tx mode enable */
 253
 254        /* C+ mode command register */
 255        RxVlanOn        = (1 << 6),  /* Rx VLAN de-tagging enable */
 256        RxChkSum        = (1 << 5),  /* Rx checksum offload enable */
 257        PCIDAC          = (1 << 4),  /* PCI Dual Address Cycle (64-bit PCI) */
 258        PCIMulRW        = (1 << 3),  /* Enable PCI read/write multiple */
 259        CpRxOn          = (1 << 1),  /* Rx mode enable */
 260        CpTxOn          = (1 << 0),  /* Tx mode enable */
 261
 262        /* Cfg9436 EEPROM control register */
 263        Cfg9346_Lock    = 0x00,      /* Lock ConfigX/MII register access */
 264        Cfg9346_Unlock  = 0xC0,      /* Unlock ConfigX/MII register access */
 265
 266        /* TxConfig register */
 267        IFG             = (1 << 25) | (1 << 24), /* standard IEEE interframe gap */
 268        TxDMAShift      = 8,         /* DMA burst value (0-7) is shift this many bits */
 269
 270        /* Early Tx Threshold register */
 271        TxThreshMask    = 0x3f,      /* Mask bits 5-0 */
 272        TxThreshMax     = 2048,      /* Max early Tx threshold */
 273
 274        /* Config1 register */
 275        DriverLoaded    = (1 << 5),  /* Software marker, driver is loaded */
 276        LWACT           = (1 << 4),  /* LWAKE active mode */
 277        PMEnable        = (1 << 0),  /* Enable various PM features of chip */
 278
 279        /* Config3 register */
 280        PARMEnable      = (1 << 6),  /* Enable auto-loading of PHY parms */
 281        MagicPacket     = (1 << 5),  /* Wake up when receives a Magic Packet */
 282        LinkUp          = (1 << 4),  /* Wake up when the cable connection is re-established */
 283
 284        /* Config4 register */
 285        LWPTN           = (1 << 1),  /* LWAKE Pattern */
 286        LWPME           = (1 << 4),  /* LANWAKE vs PMEB */
 287
 288        /* Config5 register */
 289        BWF             = (1 << 6),  /* Accept Broadcast wakeup frame */
 290        MWF             = (1 << 5),  /* Accept Multicast wakeup frame */
 291        UWF             = (1 << 4),  /* Accept Unicast wakeup frame */
 292        LANWake         = (1 << 1),  /* Enable LANWake signal */
 293        PMEStatus       = (1 << 0),  /* PME status can be reset by PCI RST# */
 294
 295        cp_norx_intr_mask = PciErr | LinkChg | TxOK | TxErr | TxEmpty,
 296        cp_rx_intr_mask = RxOK | RxErr | RxEmpty | RxFIFOOvr,
 297        cp_intr_mask = cp_rx_intr_mask | cp_norx_intr_mask,
 298};
 299
 300static const unsigned int cp_rx_config =
 301          (RX_FIFO_THRESH << RxCfgFIFOShift) |
 302          (RX_DMA_BURST << RxCfgDMAShift);
 303
 304struct cp_desc {
 305        u32             opts1;
 306        u32             opts2;
 307        u64             addr;
 308};
 309
 310struct ring_info {
 311        struct sk_buff          *skb;
 312        dma_addr_t              mapping;
 313        unsigned                frag;
 314};
 315
 316struct cp_dma_stats {
 317        u64                     tx_ok;
 318        u64                     rx_ok;
 319        u64                     tx_err;
 320        u32                     rx_err;
 321        u16                     rx_fifo;
 322        u16                     frame_align;
 323        u32                     tx_ok_1col;
 324        u32                     tx_ok_mcol;
 325        u64                     rx_ok_phys;
 326        u64                     rx_ok_bcast;
 327        u32                     rx_ok_mcast;
 328        u16                     tx_abort;
 329        u16                     tx_underrun;
 330} __attribute__((packed));
 331
 332struct cp_extra_stats {
 333        unsigned long           rx_frags;
 334};
 335
 336struct cp_private {
 337        unsigned                tx_head;
 338        unsigned                tx_tail;
 339        unsigned                rx_tail;
 340
 341        void                    *regs;
 342        struct net_device       *dev;
 343        spinlock_t              lock;
 344
 345        struct cp_desc          *rx_ring;
 346        struct cp_desc          *tx_ring;
 347        struct ring_info        tx_skb[CP_TX_RING_SIZE];
 348        struct ring_info        rx_skb[CP_RX_RING_SIZE];
 349        unsigned                rx_buf_sz;
 350        dma_addr_t              ring_dma;
 351
 352#if CP_VLAN_TAG_USED
 353        struct vlan_group       *vlgrp;
 354#endif
 355
 356        u32                     msg_enable;
 357
 358        struct net_device_stats net_stats;
 359        struct cp_extra_stats   cp_stats;
 360        struct cp_dma_stats     *nic_stats;
 361        dma_addr_t              nic_stats_dma;
 362
 363        struct pci_dev          *pdev;
 364        u32                     rx_config;
 365        u16                     cpcmd;
 366
 367        unsigned int            wol_enabled : 1; /* Is Wake-on-LAN enabled? */
 368        u32                     power_state[16];
 369
 370        struct mii_if_info      mii_if;
 371};
 372
 373#define cpr8(reg)       readb(cp->regs + (reg))
 374#define cpr16(reg)      readw(cp->regs + (reg))
 375#define cpr32(reg)      readl(cp->regs + (reg))
 376#define cpw8(reg,val)   writeb((val), cp->regs + (reg))
 377#define cpw16(reg,val)  writew((val), cp->regs + (reg))
 378#define cpw32(reg,val)  writel((val), cp->regs + (reg))
 379#define cpw8_f(reg,val) do {                    \
 380        writeb((val), cp->regs + (reg));        \
 381        readb(cp->regs + (reg));                \
 382        } while (0)
 383#define cpw16_f(reg,val) do {                   \
 384        writew((val), cp->regs + (reg));        \
 385        readw(cp->regs + (reg));                \
 386        } while (0)
 387#define cpw32_f(reg,val) do {                   \
 388        writel((val), cp->regs + (reg));        \
 389        readl(cp->regs + (reg));                \
 390        } while (0)
 391
 392
 393static void __cp_set_rx_mode (struct net_device *dev);
 394static void cp_tx (struct cp_private *cp);
 395static void cp_clean_rings (struct cp_private *cp);
 396
 397static struct pci_device_id cp_pci_tbl[] = {
 398        { PCI_VENDOR_ID_REALTEK, PCI_DEVICE_ID_REALTEK_8139,
 399          PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
 400        { },
 401};
 402MODULE_DEVICE_TABLE(pci, cp_pci_tbl);
 403
 404static struct {
 405        const char str[ETH_GSTRING_LEN];
 406} ethtool_stats_keys[] = {
 407        { "tx_ok" },
 408        { "rx_ok" },
 409        { "tx_err" },
 410        { "rx_err" },
 411        { "rx_fifo" },
 412        { "frame_align" },
 413        { "tx_ok_1col" },
 414        { "tx_ok_mcol" },
 415        { "rx_ok_phys" },
 416        { "rx_ok_bcast" },
 417        { "rx_ok_mcast" },
 418        { "tx_abort" },
 419        { "tx_underrun" },
 420        { "rx_frags" },
 421};
 422
 423
 424#if CP_VLAN_TAG_USED
 425static void cp_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
 426{
 427        struct cp_private *cp = dev->priv;
 428
 429        spin_lock_irq(&cp->lock);
 430        cp->vlgrp = grp;
 431        cp->cpcmd |= RxVlanOn;
 432        cpw16(CpCmd, cp->cpcmd);
 433        spin_unlock_irq(&cp->lock);
 434}
 435
 436static void cp_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
 437{
 438        struct cp_private *cp = dev->priv;
 439
 440        spin_lock_irq(&cp->lock);
 441        cp->cpcmd &= ~RxVlanOn;
 442        cpw16(CpCmd, cp->cpcmd);
 443        if (cp->vlgrp)
 444                cp->vlgrp->vlan_devices[vid] = NULL;
 445        spin_unlock_irq(&cp->lock);
 446}
 447#endif /* CP_VLAN_TAG_USED */
 448
 449static inline void cp_set_rxbufsize (struct cp_private *cp)
 450{
 451        unsigned int mtu = cp->dev->mtu;
 452        
 453        if (mtu > ETH_DATA_LEN)
 454                /* MTU + ethernet header + FCS + optional VLAN tag */
 455                cp->rx_buf_sz = mtu + ETH_HLEN + 8;
 456        else
 457                cp->rx_buf_sz = PKT_BUF_SZ;
 458}
 459
 460static inline void cp_rx_skb (struct cp_private *cp, struct sk_buff *skb,
 461                              struct cp_desc *desc)
 462{
 463        skb->protocol = eth_type_trans (skb, cp->dev);
 464
 465        cp->net_stats.rx_packets++;
 466        cp->net_stats.rx_bytes += skb->len;
 467        cp->dev->last_rx = jiffies;
 468
 469#if CP_VLAN_TAG_USED
 470        if (cp->vlgrp && (desc->opts2 & RxVlanTagged)) {
 471                vlan_hwaccel_receive_skb(skb, cp->vlgrp,
 472                                         be16_to_cpu(desc->opts2 & 0xffff));
 473        } else
 474#endif
 475                netif_receive_skb(skb);
 476}
 477
 478static void cp_rx_err_acct (struct cp_private *cp, unsigned rx_tail,
 479                            u32 status, u32 len)
 480{
 481        if (netif_msg_rx_err (cp))
 482                printk (KERN_DEBUG
 483                        "%s: rx err, slot %d status 0x%x len %d\n",
 484                        cp->dev->name, rx_tail, status, len);
 485        cp->net_stats.rx_errors++;
 486        if (status & RxErrFrame)
 487                cp->net_stats.rx_frame_errors++;
 488        if (status & RxErrCRC)
 489                cp->net_stats.rx_crc_errors++;
 490        if ((status & RxErrRunt) || (status & RxErrLong))
 491                cp->net_stats.rx_length_errors++;
 492        if ((status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag))
 493                cp->net_stats.rx_length_errors++;
 494        if (status & RxErrFIFO)
 495                cp->net_stats.rx_fifo_errors++;
 496}
 497
 498static inline unsigned int cp_rx_csum_ok (u32 status)
 499{
 500        unsigned int protocol = (status >> 16) & 0x3;
 501        
 502        if (likely((protocol == RxProtoTCP) && (!(status & TCPFail))))
 503                return 1;
 504        else if ((protocol == RxProtoUDP) && (!(status & UDPFail)))
 505                return 1;
 506        else if ((protocol == RxProtoIP) && (!(status & IPFail)))
 507                return 1;
 508        return 0;
 509}
 510
 511static int cp_rx_poll (struct net_device *dev, int *budget)
 512{
 513        struct cp_private *cp = dev->priv;
 514        unsigned rx_tail = cp->rx_tail;
 515        unsigned rx_work = dev->quota;
 516        unsigned rx;
 517
 518rx_status_loop:
 519        rx = 0;
 520        cpw16(IntrStatus, cp_rx_intr_mask);
 521
 522        while (1) {
 523                u32 status, len;
 524                dma_addr_t mapping;
 525                struct sk_buff *skb, *new_skb;
 526                struct cp_desc *desc;
 527                unsigned buflen;
 528
 529                skb = cp->rx_skb[rx_tail].skb;
 530                if (!skb)
 531                        BUG();
 532
 533                desc = &cp->rx_ring[rx_tail];
 534                status = le32_to_cpu(desc->opts1);
 535                if (status & DescOwn)
 536                        break;
 537
 538                len = (status & 0x1fff) - 4;
 539                mapping = cp->rx_skb[rx_tail].mapping;
 540
 541                if ((status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag)) {
 542                        /* we don't support incoming fragmented frames.
 543                         * instead, we attempt to ensure that the
 544                         * pre-allocated RX skbs are properly sized such
 545                         * that RX fragments are never encountered
 546                         */
 547                        cp_rx_err_acct(cp, rx_tail, status, len);
 548                        cp->net_stats.rx_dropped++;
 549                        cp->cp_stats.rx_frags++;
 550                        goto rx_next;
 551                }
 552
 553                if (status & (RxError | RxErrFIFO)) {
 554                        cp_rx_err_acct(cp, rx_tail, status, len);
 555                        goto rx_next;
 556                }
 557
 558                if (netif_msg_rx_status(cp))
 559                        printk(KERN_DEBUG "%s: rx slot %d status 0x%x len %d\n",
 560                               cp->dev->name, rx_tail, status, len);
 561
 562                buflen = cp->rx_buf_sz + RX_OFFSET;
 563                new_skb = dev_alloc_skb (buflen);
 564                if (!new_skb) {
 565                        cp->net_stats.rx_dropped++;
 566                        goto rx_next;
 567                }
 568
 569                skb_reserve(new_skb, RX_OFFSET);
 570                new_skb->dev = cp->dev;
 571
 572                pci_unmap_single(cp->pdev, mapping,
 573                                 buflen, PCI_DMA_FROMDEVICE);
 574
 575                /* Handle checksum offloading for incoming packets. */
 576                if (cp_rx_csum_ok(status))
 577                        skb->ip_summed = CHECKSUM_UNNECESSARY;
 578                else
 579                        skb->ip_summed = CHECKSUM_NONE;
 580
 581                skb_put(skb, len);
 582
 583                mapping =
 584                cp->rx_skb[rx_tail].mapping =
 585                        pci_map_single(cp->pdev, new_skb->tail,
 586                                       buflen, PCI_DMA_FROMDEVICE);
 587                cp->rx_skb[rx_tail].skb = new_skb;
 588
 589                cp_rx_skb(cp, skb, desc);
 590                rx++;
 591
 592rx_next:
 593                cp->rx_ring[rx_tail].opts2 = 0;
 594                cp->rx_ring[rx_tail].addr = cpu_to_le64(mapping);
 595                if (rx_tail == (CP_RX_RING_SIZE - 1))
 596                        desc->opts1 = cpu_to_le32(DescOwn | RingEnd |
 597                                                  cp->rx_buf_sz);
 598                else
 599                        desc->opts1 = cpu_to_le32(DescOwn | cp->rx_buf_sz);
 600                rx_tail = NEXT_RX(rx_tail);
 601
 602                if (!rx_work--)
 603                        break;
 604        }
 605
 606        cp->rx_tail = rx_tail;
 607
 608        dev->quota -= rx;
 609        *budget -= rx;
 610
 611        /* if we did not reach work limit, then we're done with
 612         * this round of polling
 613         */
 614        if (rx_work) {
 615                if (cpr16(IntrStatus) & cp_rx_intr_mask)
 616                        goto rx_status_loop;
 617
 618                netif_rx_complete(dev);
 619                cpw16_f(IntrMask, cp_intr_mask);
 620
 621                return 0;       /* done */
 622        }
 623
 624        return 1;               /* not done */
 625}
 626
 627static irqreturn_t
 628cp_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
 629{
 630        struct net_device *dev = dev_instance;
 631        struct cp_private *cp = dev->priv;
 632        u16 status;
 633
 634        status = cpr16(IntrStatus);
 635        if (!status || (status == 0xFFFF))
 636                return IRQ_NONE;
 637
 638        if (netif_msg_intr(cp))
 639                printk(KERN_DEBUG "%s: intr, status %04x cmd %02x cpcmd %04x\n",
 640                        dev->name, status, cpr8(Cmd), cpr16(CpCmd));
 641
 642        cpw16(IntrStatus, status & ~cp_rx_intr_mask);
 643
 644        spin_lock(&cp->lock);
 645
 646        if (status & (RxOK | RxErr | RxEmpty | RxFIFOOvr)) {
 647                if (netif_rx_schedule_prep(dev)) {
 648                        cpw16_f(IntrMask, cp_norx_intr_mask);
 649                        __netif_rx_schedule(dev);
 650                }
 651        }
 652        if (status & (TxOK | TxErr | TxEmpty | SWInt))
 653                cp_tx(cp);
 654        if (status & LinkChg)
 655                mii_check_media(&cp->mii_if, netif_msg_link(cp), FALSE);
 656
 657        if (status & PciErr) {
 658                u16 pci_status;
 659
 660                pci_read_config_word(cp->pdev, PCI_STATUS, &pci_status);
 661                pci_write_config_word(cp->pdev, PCI_STATUS, pci_status);
 662                printk(KERN_ERR "%s: PCI bus error, status=%04x, PCI status=%04x\n",
 663                       dev->name, status, pci_status);
 664
 665                /* TODO: reset hardware */
 666        }
 667
 668        spin_unlock(&cp->lock);
 669        return IRQ_HANDLED;
 670}
 671
 672static void cp_tx (struct cp_private *cp)
 673{
 674        unsigned tx_head = cp->tx_head;
 675        unsigned tx_tail = cp->tx_tail;
 676
 677        while (tx_tail != tx_head) {
 678                struct sk_buff *skb;
 679                u32 status;
 680
 681                rmb();
 682                status = le32_to_cpu(cp->tx_ring[tx_tail].opts1);
 683                if (status & DescOwn)
 684                        break;
 685
 686                skb = cp->tx_skb[tx_tail].skb;
 687                if (!skb)
 688                        BUG();
 689
 690                pci_unmap_single(cp->pdev, cp->tx_skb[tx_tail].mapping,
 691                                        skb->len, PCI_DMA_TODEVICE);
 692
 693                if (status & LastFrag) {
 694                        if (status & (TxError | TxFIFOUnder)) {
 695                                if (netif_msg_tx_err(cp))
 696                                        printk(KERN_DEBUG "%s: tx err, status 0x%x\n",
 697                                               cp->dev->name, status);
 698                                cp->net_stats.tx_errors++;
 699                                if (status & TxOWC)
 700                                        cp->net_stats.tx_window_errors++;
 701                                if (status & TxMaxCol)
 702                                        cp->net_stats.tx_aborted_errors++;
 703                                if (status & TxLinkFail)
 704                                        cp->net_stats.tx_carrier_errors++;
 705                                if (status & TxFIFOUnder)
 706                                        cp->net_stats.tx_fifo_errors++;
 707                        } else {
 708                                cp->net_stats.collisions +=
 709                                        ((status >> TxColCntShift) & TxColCntMask);
 710                                cp->net_stats.tx_packets++;
 711                                cp->net_stats.tx_bytes += skb->len;
 712                                if (netif_msg_tx_done(cp))
 713                                        printk(KERN_DEBUG "%s: tx done, slot %d\n", cp->dev->name, tx_tail);
 714                        }
 715                        dev_kfree_skb_irq(skb);
 716                }
 717
 718                cp->tx_skb[tx_tail].skb = NULL;
 719
 720                tx_tail = NEXT_TX(tx_tail);
 721        }
 722
 723        cp->tx_tail = tx_tail;
 724
 725        if (TX_BUFFS_AVAIL(cp) > (MAX_SKB_FRAGS + 1))
 726                netif_wake_queue(cp->dev);
 727}
 728
 729static int cp_start_xmit (struct sk_buff *skb, struct net_device *dev)
 730{
 731        struct cp_private *cp = dev->priv;
 732        unsigned entry;
 733        u32 eor;
 734#if CP_VLAN_TAG_USED
 735        u32 vlan_tag = 0;
 736#endif
 737
 738        spin_lock_irq(&cp->lock);
 739
 740        /* This is a hard error, log it. */
 741        if (TX_BUFFS_AVAIL(cp) <= (skb_shinfo(skb)->nr_frags + 1)) {
 742                netif_stop_queue(dev);
 743                spin_unlock_irq(&cp->lock);
 744                printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
 745                       dev->name);
 746                return 1;
 747        }
 748
 749#if CP_VLAN_TAG_USED
 750        if (cp->vlgrp && vlan_tx_tag_present(skb))
 751                vlan_tag = TxVlanTag | cpu_to_be16(vlan_tx_tag_get(skb));
 752#endif
 753
 754        entry = cp->tx_head;
 755        eor = (entry == (CP_TX_RING_SIZE - 1)) ? RingEnd : 0;
 756        if (skb_shinfo(skb)->nr_frags == 0) {
 757                struct cp_desc *txd = &cp->tx_ring[entry];
 758                u32 len;
 759                dma_addr_t mapping;
 760
 761                len = skb->len;
 762                mapping = pci_map_single(cp->pdev, skb->data, len, PCI_DMA_TODEVICE);
 763                CP_VLAN_TX_TAG(txd, vlan_tag);
 764                txd->addr = cpu_to_le64(mapping);
 765                wmb();
 766
 767                if (skb->ip_summed == CHECKSUM_HW) {
 768                        const struct iphdr *ip = skb->nh.iph;
 769                        if (ip->protocol == IPPROTO_TCP)
 770                                txd->opts1 = cpu_to_le32(eor | len | DescOwn |
 771                                                         FirstFrag | LastFrag |
 772                                                         IPCS | TCPCS);
 773                        else if (ip->protocol == IPPROTO_UDP)
 774                                txd->opts1 = cpu_to_le32(eor | len | DescOwn |
 775                                                         FirstFrag | LastFrag |
 776                                                         IPCS | UDPCS);
 777                        else
 778                                BUG();
 779                } else
 780                        txd->opts1 = cpu_to_le32(eor | len | DescOwn |
 781                                                 FirstFrag | LastFrag);
 782                wmb();
 783
 784                cp->tx_skb[entry].skb = skb;
 785                cp->tx_skb[entry].mapping = mapping;
 786                cp->tx_skb[entry].frag = 0;
 787                entry = NEXT_TX(entry);
 788        } else {
 789                struct cp_desc *txd;
 790                u32 first_len, first_eor;
 791                dma_addr_t first_mapping;
 792                int frag, first_entry = entry;
 793                const struct iphdr *ip = skb->nh.iph;
 794
 795                /* We must give this initial chunk to the device last.
 796                 * Otherwise we could race with the device.
 797                 */
 798                first_eor = eor;
 799                first_len = skb_headlen(skb);
 800                first_mapping = pci_map_single(cp->pdev, skb->data,
 801                                               first_len, PCI_DMA_TODEVICE);
 802                cp->tx_skb[entry].skb = skb;
 803                cp->tx_skb[entry].mapping = first_mapping;
 804                cp->tx_skb[entry].frag = 1;
 805                entry = NEXT_TX(entry);
 806
 807                for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
 808                        skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
 809                        u32 len;
 810                        u32 ctrl;
 811                        dma_addr_t mapping;
 812
 813                        len = this_frag->size;
 814                        mapping = pci_map_single(cp->pdev,
 815                                                 ((void *) page_address(this_frag->page) +
 816                                                  this_frag->page_offset),
 817                                                 len, PCI_DMA_TODEVICE);
 818                        eor = (entry == (CP_TX_RING_SIZE - 1)) ? RingEnd : 0;
 819
 820                        if (skb->ip_summed == CHECKSUM_HW) {
 821                                ctrl = eor | len | DescOwn | IPCS;
 822                                if (ip->protocol == IPPROTO_TCP)
 823                                        ctrl |= TCPCS;
 824                                else if (ip->protocol == IPPROTO_UDP)
 825                                        ctrl |= UDPCS;
 826                                else
 827                                        BUG();
 828                        } else
 829                                ctrl = eor | len | DescOwn;
 830
 831                        if (frag == skb_shinfo(skb)->nr_frags - 1)
 832                                ctrl |= LastFrag;
 833
 834                        txd = &cp->tx_ring[entry];
 835                        CP_VLAN_TX_TAG(txd, vlan_tag);
 836                        txd->addr = cpu_to_le64(mapping);
 837                        wmb();
 838
 839                        txd->opts1 = cpu_to_le32(ctrl);
 840                        wmb();
 841
 842                        cp->tx_skb[entry].skb = skb;
 843                        cp->tx_skb[entry].mapping = mapping;
 844                        cp->tx_skb[entry].frag = frag + 2;
 845                        entry = NEXT_TX(entry);
 846                }
 847
 848                txd = &cp->tx_ring[first_entry];
 849                CP_VLAN_TX_TAG(txd, vlan_tag);
 850                txd->addr = cpu_to_le64(first_mapping);
 851                wmb();
 852
 853                if (skb->ip_summed == CHECKSUM_HW) {
 854                        if (ip->protocol == IPPROTO_TCP)
 855                                txd->opts1 = cpu_to_le32(first_eor | first_len |
 856                                                         FirstFrag | DescOwn |
 857                                                         IPCS | TCPCS);
 858                        else if (ip->protocol == IPPROTO_UDP)
 859                                txd->opts1 = cpu_to_le32(first_eor | first_len |
 860                                                         FirstFrag | DescOwn |
 861                                                         IPCS | UDPCS);
 862                        else
 863                                BUG();
 864                } else
 865                        txd->opts1 = cpu_to_le32(first_eor | first_len |
 866                                                 FirstFrag | DescOwn);
 867                wmb();
 868        }
 869        cp->tx_head = entry;
 870        if (netif_msg_tx_queued(cp))
 871                printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
 872                       dev->name, entry, skb->len);
 873        if (TX_BUFFS_AVAIL(cp) <= (MAX_SKB_FRAGS + 1))
 874                netif_stop_queue(dev);
 875
 876        spin_unlock_irq(&cp->lock);
 877
 878        cpw8(TxPoll, NormalTxPoll);
 879        dev->trans_start = jiffies;
 880
 881        return 0;
 882}
 883
 884/* Set or clear the multicast filter for this adaptor.
 885   This routine is not state sensitive and need not be SMP locked. */
 886
 887static void __cp_set_rx_mode (struct net_device *dev)
 888{
 889        struct cp_private *cp = dev->priv;
 890        u32 mc_filter[2];       /* Multicast hash filter */
 891        int i, rx_mode;
 892        u32 tmp;
 893
 894        /* Note: do not reorder, GCC is clever about common statements. */
 895        if (dev->flags & IFF_PROMISC) {
 896                /* Unconditionally log net taps. */
 897                printk (KERN_NOTICE "%s: Promiscuous mode enabled.\n",
 898                        dev->name);
 899                rx_mode =
 900                    AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
 901                    AcceptAllPhys;
 902                mc_filter[1] = mc_filter[0] = 0xffffffff;
 903        } else if ((dev->mc_count > multicast_filter_limit)
 904                   || (dev->flags & IFF_ALLMULTI)) {
 905                /* Too many to filter perfectly -- accept all multicasts. */
 906                rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
 907                mc_filter[1] = mc_filter[0] = 0xffffffff;
 908        } else {
 909                struct dev_mc_list *mclist;
 910                rx_mode = AcceptBroadcast | AcceptMyPhys;
 911                mc_filter[1] = mc_filter[0] = 0;
 912                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
 913                     i++, mclist = mclist->next) {
 914                        int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
 915
 916                        mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
 917                        rx_mode |= AcceptMulticast;
 918                }
 919        }
 920
 921        /* We can safely update without stopping the chip. */
 922        tmp = cp_rx_config | rx_mode;
 923        if (cp->rx_config != tmp) {
 924                cpw32_f (RxConfig, tmp);
 925                cp->rx_config = tmp;
 926        }
 927        cpw32_f (MAR0 + 0, mc_filter[0]);
 928        cpw32_f (MAR0 + 4, mc_filter[1]);
 929}
 930
 931static void cp_set_rx_mode (struct net_device *dev)
 932{
 933        unsigned long flags;
 934        struct cp_private *cp = dev->priv;
 935
 936        spin_lock_irqsave (&cp->lock, flags);
 937        __cp_set_rx_mode(dev);
 938        spin_unlock_irqrestore (&cp->lock, flags);
 939}
 940
 941static void __cp_get_stats(struct cp_private *cp)
 942{
 943        /* only lower 24 bits valid; write any value to clear */
 944        cp->net_stats.rx_missed_errors += (cpr32 (RxMissed) & 0xffffff);
 945        cpw32 (RxMissed, 0);
 946}
 947
 948static struct net_device_stats *cp_get_stats(struct net_device *dev)
 949{
 950        struct cp_private *cp = dev->priv;
 951
 952        /* The chip only need report frame silently dropped. */
 953        spin_lock_irq(&cp->lock);
 954        if (netif_running(dev) && netif_device_present(dev))
 955                __cp_get_stats(cp);
 956        spin_unlock_irq(&cp->lock);
 957
 958        return &cp->net_stats;
 959}
 960
 961static void cp_stop_hw (struct cp_private *cp)
 962{
 963        struct net_device *dev = cp->dev;
 964
 965        cpw16(IntrStatus, ~(cpr16(IntrStatus)));
 966        cpw16_f(IntrMask, 0);
 967        cpw8(Cmd, 0);
 968        cpw16_f(CpCmd, 0);
 969        cpw16(IntrStatus, ~(cpr16(IntrStatus)));
 970        synchronize_irq(dev->irq);
 971        udelay(10);
 972
 973        cp->rx_tail = 0;
 974        cp->tx_head = cp->tx_tail = 0;
 975
 976        (void) dev; /* avoid compiler warning when synchronize_irq()
 977                     * disappears during !CONFIG_SMP
 978                     */
 979}
 980
 981static void cp_reset_hw (struct cp_private *cp)
 982{
 983        unsigned work = 1000;
 984
 985        cpw8(Cmd, CmdReset);
 986
 987        while (work--) {
 988                if (!(cpr8(Cmd) & CmdReset))
 989                        return;
 990
 991                set_current_state(TASK_UNINTERRUPTIBLE);
 992                schedule_timeout(10);
 993        }
 994
 995        printk(KERN_ERR "%s: hardware reset timeout\n", cp->dev->name);
 996}
 997
 998static inline void cp_start_hw (struct cp_private *cp)
 999{
1000        cpw16(CpCmd, cp->cpcmd);
1001        cpw8(Cmd, RxOn | TxOn);
1002}
1003
1004static void cp_init_hw (struct cp_private *cp)
1005{
1006        struct net_device *dev = cp->dev;
1007
1008        cp_reset_hw(cp);
1009
1010        cpw8_f (Cfg9346, Cfg9346_Unlock);
1011
1012        /* Restore our idea of the MAC address. */
1013        cpw32_f (MAC0 + 0, cpu_to_le32 (*(u32 *) (dev->dev_addr + 0)));
1014        cpw32_f (MAC0 + 4, cpu_to_le32 (*(u32 *) (dev->dev_addr + 4)));
1015
1016        cp_start_hw(cp);
1017        cpw8(TxThresh, 0x06); /* XXX convert magic num to a constant */
1018
1019        __cp_set_rx_mode(dev);
1020        cpw32_f (TxConfig, IFG | (TX_DMA_BURST << TxDMAShift));
1021
1022        cpw8(Config1, cpr8(Config1) | DriverLoaded | PMEnable);
1023        /* Disable Wake-on-LAN. Can be turned on with ETHTOOL_SWOL */
1024        cpw8(Config3, PARMEnable);
1025        cp->wol_enabled = 0;
1026
1027        cpw8(Config5, cpr8(Config5) & PMEStatus); 
1028
1029        cpw32_f(HiTxRingAddr, 0);
1030        cpw32_f(HiTxRingAddr + 4, 0);
1031
1032        cpw32_f(RxRingAddr, cp->ring_dma);
1033        cpw32_f(RxRingAddr + 4, 0);             /* FIXME: 64-bit PCI */
1034        cpw32_f(TxRingAddr, cp->ring_dma + (sizeof(struct cp_desc) * CP_RX_RING_SIZE));
1035        cpw32_f(TxRingAddr + 4, 0);             /* FIXME: 64-bit PCI */
1036
1037        cpw16(MultiIntr, 0);
1038
1039        cpw16_f(IntrMask, cp_intr_mask);
1040
1041        cpw8_f(Cfg9346, Cfg9346_Lock);
1042}
1043
1044static int cp_refill_rx (struct cp_private *cp)
1045{
1046        unsigned i;
1047
1048        for (i = 0; i < CP_RX_RING_SIZE; i++) {
1049                struct sk_buff *skb;
1050
1051                skb = dev_alloc_skb(cp->rx_buf_sz + RX_OFFSET);
1052                if (!skb)
1053                        goto err_out;
1054
1055                skb->dev = cp->dev;
1056                skb_reserve(skb, RX_OFFSET);
1057
1058                cp->rx_skb[i].mapping = pci_map_single(cp->pdev,
1059                        skb->tail, cp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1060                cp->rx_skb[i].skb = skb;
1061                cp->rx_skb[i].frag = 0;
1062
1063                cp->rx_ring[i].opts2 = 0;
1064                cp->rx_ring[i].addr = cpu_to_le64(cp->rx_skb[i].mapping);
1065                if (i == (CP_RX_RING_SIZE - 1))
1066                        cp->rx_ring[i].opts1 =
1067                                cpu_to_le32(DescOwn | RingEnd | cp->rx_buf_sz);
1068                else
1069                        cp->rx_ring[i].opts1 =
1070                                cpu_to_le32(DescOwn | cp->rx_buf_sz);
1071        }
1072
1073        return 0;
1074
1075err_out:
1076        cp_clean_rings(cp);
1077        return -ENOMEM;
1078}
1079
1080static int cp_init_rings (struct cp_private *cp)
1081{
1082        memset(cp->tx_ring, 0, sizeof(struct cp_desc) * CP_TX_RING_SIZE);
1083        cp->tx_ring[CP_TX_RING_SIZE - 1].opts1 = cpu_to_le32(RingEnd);
1084
1085        cp->rx_tail = 0;
1086        cp->tx_head = cp->tx_tail = 0;
1087
1088        return cp_refill_rx (cp);
1089}
1090
1091static int cp_alloc_rings (struct cp_private *cp)
1092{
1093        void *mem;
1094
1095        mem = pci_alloc_consistent(cp->pdev, CP_RING_BYTES, &cp->ring_dma);
1096        if (!mem)
1097                return -ENOMEM;
1098
1099        cp->rx_ring = mem;
1100        cp->tx_ring = &cp->rx_ring[CP_RX_RING_SIZE];
1101
1102        mem += (CP_RING_BYTES - CP_STATS_SIZE);
1103        cp->nic_stats = mem;
1104        cp->nic_stats_dma = cp->ring_dma + (CP_RING_BYTES - CP_STATS_SIZE);
1105
1106        return cp_init_rings(cp);
1107}
1108
1109static void cp_clean_rings (struct cp_private *cp)
1110{
1111        unsigned i;
1112
1113        memset(cp->rx_ring, 0, sizeof(struct cp_desc) * CP_RX_RING_SIZE);
1114        memset(cp->tx_ring, 0, sizeof(struct cp_desc) * CP_TX_RING_SIZE);
1115
1116        for (i = 0; i < CP_RX_RING_SIZE; i++) {
1117                if (cp->rx_skb[i].skb) {
1118                        pci_unmap_single(cp->pdev, cp->rx_skb[i].mapping,
1119                                         cp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1120                        dev_kfree_skb(cp->rx_skb[i].skb);
1121                }
1122        }
1123
1124        for (i = 0; i < CP_TX_RING_SIZE; i++) {
1125                if (cp->tx_skb[i].skb) {
1126                        struct sk_buff *skb = cp->tx_skb[i].skb;
1127                        pci_unmap_single(cp->pdev, cp->tx_skb[i].mapping,
1128                                         skb->len, PCI_DMA_TODEVICE);
1129                        dev_kfree_skb(skb);
1130                        cp->net_stats.tx_dropped++;
1131                }
1132        }
1133
1134        memset(&cp->rx_skb, 0, sizeof(struct ring_info) * CP_RX_RING_SIZE);
1135        memset(&cp->tx_skb, 0, sizeof(struct ring_info) * CP_TX_RING_SIZE);
1136}
1137
1138static void cp_free_rings (struct cp_private *cp)
1139{
1140        cp_clean_rings(cp);
1141        pci_free_consistent(cp->pdev, CP_RING_BYTES, cp->rx_ring, cp->ring_dma);
1142        cp->rx_ring = NULL;
1143        cp->tx_ring = NULL;
1144        cp->nic_stats = NULL;
1145}
1146
1147static int cp_open (struct net_device *dev)
1148{
1149        struct cp_private *cp = dev->priv;
1150        int rc;
1151
1152        if (netif_msg_ifup(cp))
1153                printk(KERN_DEBUG "%s: enabling interface\n", dev->name);
1154
1155        rc = cp_alloc_rings(cp);
1156        if (rc)
1157                return rc;
1158
1159        cp_init_hw(cp);
1160
1161        rc = request_irq(dev->irq, cp_interrupt, SA_SHIRQ, dev->name, dev);
1162        if (rc)
1163                goto err_out_hw;
1164
1165        netif_carrier_off(dev);
1166        mii_check_media(&cp->mii_if, netif_msg_link(cp), TRUE);
1167        netif_start_queue(dev);
1168
1169        return 0;
1170
1171err_out_hw:
1172        cp_stop_hw(cp);
1173        cp_free_rings(cp);
1174        return rc;
1175}
1176
1177static int cp_close (struct net_device *dev)
1178{
1179        struct cp_private *cp = dev->priv;
1180
1181        if (netif_msg_ifdown(cp))
1182                printk(KERN_DEBUG "%s: disabling interface\n", dev->name);
1183
1184        netif_stop_queue(dev);
1185        netif_carrier_off(dev);
1186
1187        spin_lock_irq(&cp->lock);
1188        cp_stop_hw(cp);
1189        spin_unlock_irq(&cp->lock);
1190
1191        free_irq(dev->irq, dev);
1192        cp_free_rings(cp);
1193        return 0;
1194}
1195
1196#ifdef BROKEN
1197static int cp_change_mtu(struct net_device *dev, int new_mtu)
1198{
1199        struct cp_private *cp = dev->priv;
1200        int rc;
1201
1202        /* check for invalid MTU, according to hardware limits */
1203        if (new_mtu < CP_MIN_MTU || new_mtu > CP_MAX_MTU)
1204                return -EINVAL;
1205
1206        /* if network interface not up, no need for complexity */
1207        if (!netif_running(dev)) {
1208                dev->mtu = new_mtu;
1209                cp_set_rxbufsize(cp);   /* set new rx buf size */
1210                return 0;
1211        }
1212
1213        spin_lock_irq(&cp->lock);
1214
1215        cp_stop_hw(cp);                 /* stop h/w and free rings */
1216        cp_clean_rings(cp);
1217
1218        dev->mtu = new_mtu;
1219        cp_set_rxbufsize(cp);           /* set new rx buf size */
1220
1221        rc = cp_init_rings(cp);         /* realloc and restart h/w */
1222        cp_start_hw(cp);
1223
1224        spin_unlock_irq(&cp->lock);
1225
1226        return rc;
1227}
1228#endif /* BROKEN */
1229
1230static char mii_2_8139_map[8] = {
1231        BasicModeCtrl,
1232        BasicModeStatus,
1233        0,
1234        0,
1235        NWayAdvert,
1236        NWayLPAR,
1237        NWayExpansion,
1238        0
1239};
1240
1241static int mdio_read(struct net_device *dev, int phy_id, int location)
1242{
1243        struct cp_private *cp = dev->priv;
1244
1245        return location < 8 && mii_2_8139_map[location] ?
1246               readw(cp->regs + mii_2_8139_map[location]) : 0;
1247}
1248
1249
1250static void mdio_write(struct net_device *dev, int phy_id, int location,
1251                       int value)
1252{
1253        struct cp_private *cp = dev->priv;
1254
1255        if (location == 0) {
1256                cpw8(Cfg9346, Cfg9346_Unlock);
1257                cpw16(BasicModeCtrl, value);
1258                cpw8(Cfg9346, Cfg9346_Lock);
1259        } else if (location < 8 && mii_2_8139_map[location])
1260                cpw16(mii_2_8139_map[location], value);
1261}
1262
1263/* Set the ethtool Wake-on-LAN settings */
1264static int netdev_set_wol (struct cp_private *cp,
1265                           const struct ethtool_wolinfo *wol)
1266{
1267        u8 options;
1268
1269        options = cpr8 (Config3) & ~(LinkUp | MagicPacket);
1270        /* If WOL is being disabled, no need for complexity */
1271        if (wol->wolopts) {
1272                if (wol->wolopts & WAKE_PHY)    options |= LinkUp;
1273                if (wol->wolopts & WAKE_MAGIC)  options |= MagicPacket;
1274        }
1275
1276        cpw8 (Cfg9346, Cfg9346_Unlock);
1277        cpw8 (Config3, options);
1278        cpw8 (Cfg9346, Cfg9346_Lock);
1279
1280        options = 0; /* Paranoia setting */
1281        options = cpr8 (Config5) & ~(UWF | MWF | BWF);
1282        /* If WOL is being disabled, no need for complexity */
1283        if (wol->wolopts) {
1284                if (wol->wolopts & WAKE_UCAST)  options |= UWF;
1285                if (wol->wolopts & WAKE_BCAST)  options |= BWF;
1286                if (wol->wolopts & WAKE_MCAST)  options |= MWF;
1287        }
1288
1289        cpw8 (Config5, options);
1290
1291        cp->wol_enabled = (wol->wolopts) ? 1 : 0;
1292
1293        return 0;
1294}
1295
1296/* Get the ethtool Wake-on-LAN settings */
1297static void netdev_get_wol (struct cp_private *cp,
1298                     struct ethtool_wolinfo *wol)
1299{
1300        u8 options;
1301
1302        wol->wolopts   = 0; /* Start from scratch */
1303        wol->supported = WAKE_PHY   | WAKE_BCAST | WAKE_MAGIC |
1304                         WAKE_MCAST | WAKE_UCAST;
1305        /* We don't need to go on if WOL is disabled */
1306        if (!cp->wol_enabled) return;
1307        
1308        options        = cpr8 (Config3);
1309        if (options & LinkUp)        wol->wolopts |= WAKE_PHY;
1310        if (options & MagicPacket)   wol->wolopts |= WAKE_MAGIC;
1311
1312        options        = 0; /* Paranoia setting */
1313        options        = cpr8 (Config5);
1314        if (options & UWF)           wol->wolopts |= WAKE_UCAST;
1315        if (options & BWF)           wol->wolopts |= WAKE_BCAST;
1316        if (options & MWF)           wol->wolopts |= WAKE_MCAST;
1317}
1318
1319static void cp_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1320{
1321        struct cp_private *cp = dev->priv;
1322
1323        strcpy (info->driver, DRV_NAME);
1324        strcpy (info->version, DRV_VERSION);
1325        strcpy (info->bus_info, pci_name(cp->pdev));
1326}
1327
1328static int cp_get_regs_len(struct net_device *dev)
1329{
1330        return CP_REGS_SIZE;
1331}
1332
1333static int cp_get_stats_count (struct net_device *dev)
1334{
1335        return CP_NUM_STATS;
1336}
1337
1338static int cp_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1339{
1340        struct cp_private *cp = dev->priv;
1341        int rc;
1342
1343        spin_lock_irq(&cp->lock);
1344        rc = mii_ethtool_gset(&cp->mii_if, cmd);
1345        spin_unlock_irq(&cp->lock);
1346
1347        return rc;
1348}
1349
1350static int cp_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1351{
1352        struct cp_private *cp = dev->priv;
1353        int rc;
1354
1355        spin_lock_irq(&cp->lock);
1356        rc = mii_ethtool_sset(&cp->mii_if, cmd);
1357        spin_unlock_irq(&cp->lock);
1358
1359        return rc;
1360}
1361
1362static int cp_nway_reset(struct net_device *dev)
1363{
1364        struct cp_private *cp = dev->priv;
1365        return mii_nway_restart(&cp->mii_if);
1366}
1367
1368static u32 cp_get_msglevel(struct net_device *dev)
1369{
1370        struct cp_private *cp = dev->priv;
1371        return cp->msg_enable;
1372}
1373
1374static void cp_set_msglevel(struct net_device *dev, u32 value)
1375{
1376        struct cp_private *cp = dev->priv;
1377        cp->msg_enable = value;
1378}
1379
1380static u32 cp_get_rx_csum(struct net_device *dev)
1381{
1382        struct cp_private *cp = dev->priv;
1383        return (cpr16(CpCmd) & RxChkSum) ? 1 : 0;
1384}
1385
1386static int cp_set_rx_csum(struct net_device *dev, u32 data)
1387{
1388        struct cp_private *cp = dev->priv;
1389        u16 cmd = cp->cpcmd, newcmd;
1390
1391        newcmd = cmd;
1392
1393        if (data)
1394                newcmd |= RxChkSum;
1395        else
1396                newcmd &= ~RxChkSum;
1397
1398        if (newcmd != cmd) {
1399                spin_lock_irq(&cp->lock);
1400                cp->cpcmd = newcmd;
1401                cpw16_f(CpCmd, newcmd);
1402                spin_unlock_irq(&cp->lock);
1403        }
1404
1405        return 0;
1406}
1407
1408static void cp_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1409                        void *p)
1410{
1411        struct cp_private *cp = dev->priv;
1412
1413        if (regs->len < CP_REGS_SIZE)
1414                return /* -EINVAL */;
1415
1416        regs->version = CP_REGS_VER;
1417
1418        spin_lock_irq(&cp->lock);
1419        memcpy_fromio(p, cp->regs, CP_REGS_SIZE);
1420        spin_unlock_irq(&cp->lock);
1421}
1422
1423static void cp_get_wol (struct net_device *dev, struct ethtool_wolinfo *wol)
1424{
1425        struct cp_private *cp = dev->priv;
1426
1427        spin_lock_irq (&cp->lock);
1428        netdev_get_wol (cp, wol);
1429        spin_unlock_irq (&cp->lock);
1430}
1431
1432static int cp_set_wol (struct net_device *dev, struct ethtool_wolinfo *wol)
1433{
1434        struct cp_private *cp = dev->priv;
1435        int rc;
1436
1437        spin_lock_irq (&cp->lock);
1438        rc = netdev_set_wol (cp, wol);
1439        spin_unlock_irq (&cp->lock);
1440
1441        return rc;
1442}
1443
1444static void cp_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
1445{
1446        switch (stringset) {
1447        case ETH_SS_STATS:
1448                memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
1449                break;
1450        default:
1451                BUG();
1452                break;
1453        }
1454}
1455
1456static void cp_get_ethtool_stats (struct net_device *dev,
1457                                  struct ethtool_stats *estats, u64 *tmp_stats)
1458{
1459        struct cp_private *cp = dev->priv;
1460        unsigned int work = 100;
1461        int i;
1462
1463        /* begin NIC statistics dump */
1464        cpw32(StatsAddr + 4, 0); /* FIXME: 64-bit PCI */
1465        cpw32(StatsAddr, cp->nic_stats_dma | DumpStats);
1466        cpr32(StatsAddr);
1467
1468        while (work-- > 0) {
1469                if ((cpr32(StatsAddr) & DumpStats) == 0)
1470                        break;
1471                cpu_relax();
1472        }
1473
1474        if (cpr32(StatsAddr) & DumpStats)
1475                return /* -EIO */;
1476
1477        i = 0;
1478        tmp_stats[i++] = le64_to_cpu(cp->nic_stats->tx_ok);
1479        tmp_stats[i++] = le64_to_cpu(cp->nic_stats->rx_ok);
1480        tmp_stats[i++] = le64_to_cpu(cp->nic_stats->tx_err);
1481        tmp_stats[i++] = le32_to_cpu(cp->nic_stats->rx_err);
1482        tmp_stats[i++] = le16_to_cpu(cp->nic_stats->rx_fifo);
1483        tmp_stats[i++] = le16_to_cpu(cp->nic_stats->frame_align);
1484        tmp_stats[i++] = le32_to_cpu(cp->nic_stats->tx_ok_1col);
1485        tmp_stats[i++] = le32_to_cpu(cp->nic_stats->tx_ok_mcol);
1486        tmp_stats[i++] = le64_to_cpu(cp->nic_stats->rx_ok_phys);
1487        tmp_stats[i++] = le64_to_cpu(cp->nic_stats->rx_ok_bcast);
1488        tmp_stats[i++] = le32_to_cpu(cp->nic_stats->rx_ok_mcast);
1489        tmp_stats[i++] = le16_to_cpu(cp->nic_stats->tx_abort);
1490        tmp_stats[i++] = le16_to_cpu(cp->nic_stats->tx_underrun);
1491        tmp_stats[i++] = cp->cp_stats.rx_frags;
1492        if (i != CP_NUM_STATS)
1493                BUG();
1494}
1495
1496static struct ethtool_ops cp_ethtool_ops = {
1497        .get_drvinfo            = cp_get_drvinfo,
1498        .get_regs_len           = cp_get_regs_len,
1499        .get_stats_count        = cp_get_stats_count,
1500        .get_settings           = cp_get_settings,
1501        .set_settings           = cp_set_settings,
1502        .nway_reset             = cp_nway_reset,
1503        .get_link               = ethtool_op_get_link,
1504        .get_msglevel           = cp_get_msglevel,
1505        .set_msglevel           = cp_set_msglevel,
1506        .get_rx_csum            = cp_get_rx_csum,
1507        .set_rx_csum            = cp_set_rx_csum,
1508        .get_tx_csum            = ethtool_op_get_tx_csum,
1509        .set_tx_csum            = ethtool_op_set_tx_csum, /* local! */
1510        .get_sg                 = ethtool_op_get_sg,
1511        .set_sg                 = ethtool_op_set_sg,
1512        .get_regs               = cp_get_regs,
1513        .get_wol                = cp_get_wol,
1514        .set_wol                = cp_set_wol,
1515        .get_strings            = cp_get_strings,
1516        .get_ethtool_stats      = cp_get_ethtool_stats,
1517};
1518
1519static int cp_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1520{
1521        struct cp_private *cp = dev->priv;
1522        struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &rq->ifr_data;
1523        int rc;
1524
1525        if (!netif_running(dev))
1526                return -EINVAL;
1527
1528        spin_lock_irq(&cp->lock);
1529        rc = generic_mii_ioctl(&cp->mii_if, mii, cmd, NULL);
1530        spin_unlock_irq(&cp->lock);
1531        return rc;
1532}
1533
1534/* Serial EEPROM section. */
1535
1536/*  EEPROM_Ctrl bits. */
1537#define EE_SHIFT_CLK    0x04    /* EEPROM shift clock. */
1538#define EE_CS                   0x08    /* EEPROM chip select. */
1539#define EE_DATA_WRITE   0x02    /* EEPROM chip data in. */
1540#define EE_WRITE_0              0x00
1541#define EE_WRITE_1              0x02
1542#define EE_DATA_READ    0x01    /* EEPROM chip data out. */
1543#define EE_ENB                  (0x80 | EE_CS)
1544
1545/* Delay between EEPROM clock transitions.
1546   No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
1547 */
1548
1549#define eeprom_delay()  readl(ee_addr)
1550
1551/* The EEPROM commands include the alway-set leading bit. */
1552#define EE_WRITE_CMD    (5)
1553#define EE_READ_CMD             (6)
1554#define EE_ERASE_CMD    (7)
1555
1556static int read_eeprom (void *ioaddr, int location, int addr_len)
1557{
1558        int i;
1559        unsigned retval = 0;
1560        void *ee_addr = ioaddr + Cfg9346;
1561        int read_cmd = location | (EE_READ_CMD << addr_len);
1562
1563        writeb (EE_ENB & ~EE_CS, ee_addr);
1564        writeb (EE_ENB, ee_addr);
1565        eeprom_delay ();
1566
1567        /* Shift the read command bits out. */
1568        for (i = 4 + addr_len; i >= 0; i--) {
1569                int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1570                writeb (EE_ENB | dataval, ee_addr);
1571                eeprom_delay ();
1572                writeb (EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1573                eeprom_delay ();
1574        }
1575        writeb (EE_ENB, ee_addr);
1576        eeprom_delay ();
1577
1578        for (i = 16; i > 0; i--) {
1579                writeb (EE_ENB | EE_SHIFT_CLK, ee_addr);
1580                eeprom_delay ();
1581                retval =
1582                    (retval << 1) | ((readb (ee_addr) & EE_DATA_READ) ? 1 :
1583                                     0);
1584                writeb (EE_ENB, ee_addr);
1585                eeprom_delay ();
1586        }
1587
1588        /* Terminate the EEPROM access. */
1589        writeb (~EE_CS, ee_addr);
1590        eeprom_delay ();
1591
1592        return retval;
1593}
1594
1595/* Put the board into D3cold state and wait for WakeUp signal */
1596static void cp_set_d3_state (struct cp_private *cp)
1597{
1598        pci_enable_wake (cp->pdev, 0, 1); /* Enable PME# generation */
1599        pci_set_power_state (cp->pdev, 3);
1600}
1601
1602static int cp_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1603{
1604        struct net_device *dev;
1605        struct cp_private *cp;
1606        int rc;
1607        void *regs;
1608        long pciaddr;
1609        unsigned int addr_len, i, pci_using_dac;
1610        u8 pci_rev;
1611
1612#ifndef MODULE
1613        static int version_printed;
1614        if (version_printed++ == 0)
1615                printk("%s", version);
1616#endif
1617
1618        pci_read_config_byte(pdev, PCI_REVISION_ID, &pci_rev);
1619
1620        if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
1621            pdev->device == PCI_DEVICE_ID_REALTEK_8139 && pci_rev < 0x20) {
1622                printk(KERN_ERR PFX "pci dev %s (id %04x:%04x rev %02x) is not an 8139C+ compatible chip\n",
1623                       pci_name(pdev), pdev->vendor, pdev->device, pci_rev);
1624                printk(KERN_ERR PFX "Try the \"8139too\" driver instead.\n");
1625                return -ENODEV;
1626        }
1627
1628        dev = alloc_etherdev(sizeof(struct cp_private));
1629        if (!dev)
1630                return -ENOMEM;
1631        SET_MODULE_OWNER(dev);
1632        SET_NETDEV_DEV(dev, &pdev->dev);
1633
1634        cp = dev->priv;
1635        cp->pdev = pdev;
1636        cp->dev = dev;
1637        cp->msg_enable = (debug < 0 ? CP_DEF_MSG_ENABLE : debug);
1638        spin_lock_init (&cp->lock);
1639        cp->mii_if.dev = dev;
1640        cp->mii_if.mdio_read = mdio_read;
1641        cp->mii_if.mdio_write = mdio_write;
1642        cp->mii_if.phy_id = CP_INTERNAL_PHY;
1643        cp->mii_if.phy_id_mask = 0x1f;
1644        cp->mii_if.reg_num_mask = 0x1f;
1645        cp_set_rxbufsize(cp);
1646
1647        rc = pci_enable_device(pdev);
1648        if (rc)
1649                goto err_out_free;
1650
1651        rc = pci_set_mwi(pdev);
1652        if (rc)
1653                goto err_out_disable;
1654
1655        rc = pci_request_regions(pdev, DRV_NAME);
1656        if (rc)
1657                goto err_out_mwi;
1658
1659        if (pdev->irq < 2) {
1660                rc = -EIO;
1661                printk(KERN_ERR PFX "invalid irq (%d) for pci dev %s\n",
1662                       pdev->irq, pci_name(pdev));
1663                goto err_out_res;
1664        }
1665        pciaddr = pci_resource_start(pdev, 1);
1666        if (!pciaddr) {
1667                rc = -EIO;
1668                printk(KERN_ERR PFX "no MMIO resource for pci dev %s\n",
1669                       pci_name(pdev));
1670                goto err_out_res;
1671        }
1672        if (pci_resource_len(pdev, 1) < CP_REGS_SIZE) {
1673                rc = -EIO;
1674                printk(KERN_ERR PFX "MMIO resource (%lx) too small on pci dev %s\n",
1675                       pci_resource_len(pdev, 1), pci_name(pdev));
1676                goto err_out_res;
1677        }
1678
1679        /* Configure DMA attributes. */
1680        if ((sizeof(dma_addr_t) > 32) &&
1681            !pci_set_dma_mask(pdev, 0xffffffffffffffffULL)) {
1682                pci_using_dac = 1;
1683        } else {
1684                rc = pci_set_dma_mask(pdev, 0xffffffffULL);
1685                if (rc) {
1686                        printk(KERN_ERR PFX "No usable DMA configuration, "
1687                               "aborting.\n");
1688                        goto err_out_res;
1689                }
1690                pci_using_dac = 0;
1691        }
1692
1693        cp->cpcmd = (pci_using_dac ? PCIDAC : 0) |
1694                    PCIMulRW | RxChkSum | CpRxOn | CpTxOn;
1695
1696        regs = ioremap_nocache(pciaddr, CP_REGS_SIZE);
1697        if (!regs) {
1698                rc = -EIO;
1699                printk(KERN_ERR PFX "Cannot map PCI MMIO (%lx@%lx) on pci dev %s\n",
1700                       pci_resource_len(pdev, 1), pciaddr, pci_name(pdev));
1701                goto err_out_res;
1702        }
1703        dev->base_addr = (unsigned long) regs;
1704        cp->regs = regs;
1705
1706        cp_stop_hw(cp);
1707
1708        /* read MAC address from EEPROM */
1709        addr_len = read_eeprom (regs, 0, 8) == 0x8129 ? 8 : 6;
1710        for (i = 0; i < 3; i++)
1711                ((u16 *) (dev->dev_addr))[i] =
1712                    le16_to_cpu (read_eeprom (regs, i + 7, addr_len));
1713
1714        dev->open = cp_open;
1715        dev->stop = cp_close;
1716        dev->set_multicast_list = cp_set_rx_mode;
1717        dev->hard_start_xmit = cp_start_xmit;
1718        dev->get_stats = cp_get_stats;
1719        dev->do_ioctl = cp_ioctl;
1720        dev->poll = cp_rx_poll;
1721        dev->weight = 16;       /* arbitrary? from NAPI_HOWTO.txt. */
1722#ifdef BROKEN
1723        dev->change_mtu = cp_change_mtu;
1724#endif
1725        dev->ethtool_ops = &cp_ethtool_ops;
1726#if 0
1727        dev->tx_timeout = cp_tx_timeout;
1728        dev->watchdog_timeo = TX_TIMEOUT;
1729#endif
1730
1731#if CP_VLAN_TAG_USED
1732        dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1733        dev->vlan_rx_register = cp_vlan_rx_register;
1734        dev->vlan_rx_kill_vid = cp_vlan_rx_kill_vid;
1735#endif
1736
1737        dev->irq = pdev->irq;
1738
1739        rc = register_netdev(dev);
1740        if (rc)
1741                goto err_out_iomap;
1742
1743        printk (KERN_INFO "%s: RTL-8139C+ at 0x%lx, "
1744                "%02x:%02x:%02x:%02x:%02x:%02x, "
1745                "IRQ %d\n",
1746                dev->name,
1747                dev->base_addr,
1748                dev->dev_addr[0], dev->dev_addr[1],
1749                dev->dev_addr[2], dev->dev_addr[3],
1750                dev->dev_addr[4], dev->dev_addr[5],
1751                dev->irq);
1752
1753        pci_set_drvdata(pdev, dev);
1754
1755        /* enable busmastering and memory-write-invalidate */
1756        pci_set_master(pdev);
1757
1758        if (cp->wol_enabled) cp_set_d3_state (cp);
1759
1760        return 0;
1761
1762err_out_iomap:
1763        iounmap(regs);
1764err_out_res:
1765        pci_release_regions(pdev);
1766err_out_mwi:
1767        pci_clear_mwi(pdev);
1768err_out_disable:
1769        pci_disable_device(pdev);
1770err_out_free:
1771        free_netdev(dev);
1772        return rc;
1773}
1774
1775static void cp_remove_one (struct pci_dev *pdev)
1776{
1777        struct net_device *dev = pci_get_drvdata(pdev);
1778        struct cp_private *cp = dev->priv;
1779
1780        if (!dev)
1781                BUG();
1782        unregister_netdev(dev);
1783        iounmap(cp->regs);
1784        if (cp->wol_enabled) pci_set_power_state (pdev, 0);
1785        pci_release_regions(pdev);
1786        pci_clear_mwi(pdev);
1787        pci_disable_device(pdev);
1788        pci_set_drvdata(pdev, NULL);
1789        free_netdev(dev);
1790}
1791
1792#ifdef CONFIG_PM
1793static int cp_suspend (struct pci_dev *pdev, u32 state)
1794{
1795        struct net_device *dev;
1796        struct cp_private *cp;
1797        unsigned long flags;
1798
1799        dev = pci_get_drvdata (pdev);
1800        cp  = dev->priv;
1801
1802        if (!dev || !netif_running (dev)) return 0;
1803
1804        netif_device_detach (dev);
1805        netif_stop_queue (dev);
1806
1807        spin_lock_irqsave (&cp->lock, flags);
1808
1809        /* Disable Rx and Tx */
1810        cpw16 (IntrMask, 0);
1811        cpw8  (Cmd, cpr8 (Cmd) & (~RxOn | ~TxOn));
1812
1813        spin_unlock_irqrestore (&cp->lock, flags);
1814
1815        if (cp->pdev && cp->wol_enabled) {
1816                pci_save_state (cp->pdev, cp->power_state);
1817                cp_set_d3_state (cp);
1818        }
1819
1820        return 0;
1821}
1822
1823static int cp_resume (struct pci_dev *pdev)
1824{
1825        struct net_device *dev;
1826        struct cp_private *cp;
1827
1828        dev = pci_get_drvdata (pdev);
1829        cp  = dev->priv;
1830
1831        netif_device_attach (dev);
1832        
1833        if (cp->pdev && cp->wol_enabled) {
1834                pci_set_power_state (cp->pdev, 0);
1835                pci_restore_state (cp->pdev, cp->power_state);
1836        }
1837        
1838        cp_init_hw (cp);
1839        netif_start_queue (dev);
1840        
1841        return 0;
1842}
1843#endif /* CONFIG_PM */
1844
1845static struct pci_driver cp_driver = {
1846        .name         = DRV_NAME,
1847        .id_table     = cp_pci_tbl,
1848        .probe        = cp_init_one,
1849        .remove       = cp_remove_one,
1850#ifdef CONFIG_PM
1851        .resume       = cp_resume,
1852        .suspend      = cp_suspend,
1853#endif
1854};
1855
1856static int __init cp_init (void)
1857{
1858#ifdef MODULE
1859        printk("%s", version);
1860#endif
1861        return pci_module_init (&cp_driver);
1862}
1863
1864static void __exit cp_exit (void)
1865{
1866        pci_unregister_driver (&cp_driver);
1867}
1868
1869module_init(cp_init);
1870module_exit(cp_exit);
1871
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.