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