linux-bk/drivers/net/8139too.c
<<
>>
Prefs
   1/*
   2
   3        8139too.c: A RealTek RTL-8139 Fast Ethernet driver for Linux.
   4
   5        Maintained by Jeff Garzik <jgarzik@pobox.com>
   6        Copyright 2000-2002 Jeff Garzik
   7
   8        Much code comes from Donald Becker's rtl8139.c driver,
   9        versions 1.13 and older.  This driver was originally based
  10        on rtl8139.c version 1.07.  Header of rtl8139.c version 1.13:
  11
  12        -----<snip>-----
  13
  14                Written 1997-2001 by Donald Becker.
  15                This software may be used and distributed according to the
  16                terms of the GNU General Public License (GPL), incorporated
  17                herein by reference.  Drivers based on or derived from this
  18                code fall under the GPL and must retain the authorship,
  19                copyright and license notice.  This file is not a complete
  20                program and may only be used when the entire operating
  21                system is licensed under the GPL.
  22
  23                This driver is for boards based on the RTL8129 and RTL8139
  24                PCI ethernet chips.
  25
  26                The author may be reached as becker@scyld.com, or C/O Scyld
  27                Computing Corporation 410 Severn Ave., Suite 210 Annapolis
  28                MD 21403
  29
  30                Support and updates available at
  31                http://www.scyld.com/network/rtl8139.html
  32
  33                Twister-tuning table provided by Kinston
  34                <shangh@realtek.com.tw>.
  35
  36        -----<snip>-----
  37
  38        This software may be used and distributed according to the terms
  39        of the GNU General Public License, incorporated herein by reference.
  40
  41        Contributors:
  42
  43                Donald Becker - he wrote the original driver, kudos to him!
  44                (but please don't e-mail him for support, this isn't his driver)
  45
  46                Tigran Aivazian - bug fixes, skbuff free cleanup
  47
  48                Martin Mares - suggestions for PCI cleanup
  49
  50                David S. Miller - PCI DMA and softnet updates
  51
  52                Ernst Gill - fixes ported from BSD driver
  53
  54                Daniel Kobras - identified specific locations of
  55                        posted MMIO write bugginess
  56
  57                Gerard Sharp - bug fix, testing and feedback
  58
  59                David Ford - Rx ring wrap fix
  60
  61                Dan DeMaggio - swapped RTL8139 cards with me, and allowed me
  62                to find and fix a crucial bug on older chipsets.
  63
  64                Donald Becker/Chris Butterworth/Marcus Westergren -
  65                Noticed various Rx packet size-related buglets.
  66
  67                Santiago Garcia Mantinan - testing and feedback
  68
  69                Jens David - 2.2.x kernel backports
  70
  71                Martin Dennett - incredibly helpful insight on undocumented
  72                features of the 8139 chips
  73
  74                Jean-Jacques Michel - bug fix
  75
  76                Tobias Ringström - Rx interrupt status checking suggestion
  77
  78                Andrew Morton - Clear blocked signals, avoid
  79                buffer overrun setting current->comm.
  80
  81                Kalle Olavi Niemitalo - Wake-on-LAN ioctls
  82
  83                Robert Kuebel - Save kernel thread from dying on any signal.
  84
  85        Submitting bug reports:
  86
  87                "rtl8139-diag -mmmaaavvveefN" output
  88                enable RTL8139_DEBUG below, and look at 'dmesg' or kernel log
  89
  90                See 8139too.txt for more details.
  91
  92*/
  93
  94#define DRV_NAME        "8139too"
  95#define DRV_VERSION     "0.9.26"
  96
  97
  98#include <linux/config.h>
  99#include <linux/module.h>
 100#include <linux/kernel.h>
 101#include <linux/compiler.h>
 102#include <linux/pci.h>
 103#include <linux/init.h>
 104#include <linux/ioport.h>
 105#include <linux/netdevice.h>
 106#include <linux/etherdevice.h>
 107#include <linux/rtnetlink.h>
 108#include <linux/delay.h>
 109#include <linux/ethtool.h>
 110#include <linux/mii.h>
 111#include <linux/completion.h>
 112#include <linux/crc32.h>
 113#include <linux/suspend.h>
 114#include <asm/io.h>
 115#include <asm/uaccess.h>
 116
 117#define RTL8139_DRIVER_NAME   DRV_NAME " Fast Ethernet driver " DRV_VERSION
 118#define PFX DRV_NAME ": "
 119
 120
 121/* enable PIO instead of MMIO, if CONFIG_8139TOO_PIO is selected */
 122#ifdef CONFIG_8139TOO_PIO
 123#define USE_IO_OPS 1
 124#endif
 125
 126/* use a 16K rx ring buffer instead of the default 32K */
 127#ifdef CONFIG_SH_DREAMCAST
 128#define USE_BUF16K 1
 129#endif
 130
 131/* define to 1 to enable copious debugging info */
 132#undef RTL8139_DEBUG
 133
 134/* define to 1 to disable lightweight runtime debugging checks */
 135#undef RTL8139_NDEBUG
 136
 137
 138#ifdef RTL8139_DEBUG
 139/* note: prints function name for you */
 140#  define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
 141#else
 142#  define DPRINTK(fmt, args...)
 143#endif
 144
 145#ifdef RTL8139_NDEBUG
 146#  define assert(expr) do {} while (0)
 147#else
 148#  define assert(expr) \
 149        if(!(expr)) {                                   \
 150        printk( "Assertion failed! %s,%s,%s,line=%d\n", \
 151        #expr,__FILE__,__FUNCTION__,__LINE__);          \
 152        }
 153#endif
 154
 155
 156/* A few user-configurable values. */
 157/* media options */
 158#define MAX_UNITS 8
 159static int media[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
 160static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
 161
 162/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
 163static int max_interrupt_work = 20;
 164
 165/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
 166   The RTL chips use a 64 element hash table based on the Ethernet CRC.  */
 167static int multicast_filter_limit = 32;
 168
 169/* bitmapped message enable number */
 170static int debug = -1;
 171
 172/* Size of the in-memory receive ring. */
 173#ifdef USE_BUF16K
 174#define RX_BUF_LEN_IDX  1       /* 0==8K, 1==16K, 2==32K, 3==64K */
 175#else
 176#define RX_BUF_LEN_IDX  2       /* 0==8K, 1==16K, 2==32K, 3==64K */
 177#endif
 178#define RX_BUF_LEN      (8192 << RX_BUF_LEN_IDX)
 179#define RX_BUF_PAD      16
 180#define RX_BUF_WRAP_PAD 2048 /* spare padding to handle lack of packet wrap */
 181#define RX_BUF_TOT_LEN  (RX_BUF_LEN + RX_BUF_PAD + RX_BUF_WRAP_PAD)
 182
 183/* Number of Tx descriptor registers. */
 184#define NUM_TX_DESC     4
 185
 186/* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/
 187#define MAX_ETH_FRAME_SIZE      1536
 188
 189/* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */
 190#define TX_BUF_SIZE     MAX_ETH_FRAME_SIZE
 191#define TX_BUF_TOT_LEN  (TX_BUF_SIZE * NUM_TX_DESC)
 192
 193/* PCI Tuning Parameters
 194   Threshold is bytes transferred to chip before transmission starts. */
 195#define TX_FIFO_THRESH 256      /* In bytes, rounded down to 32 byte units. */
 196
 197/* The following settings are log_2(bytes)-4:  0 == 16 bytes .. 6==1024, 7==end of packet. */
 198#define RX_FIFO_THRESH  7       /* Rx buffer level before first PCI xfer.  */
 199#define RX_DMA_BURST    7       /* Maximum PCI burst, '6' is 1024 */
 200#define TX_DMA_BURST    6       /* Maximum PCI burst, '6' is 1024 */
 201#define TX_RETRY        8       /* 0-15.  retries = 16 + (TX_RETRY * 16) */
 202
 203/* Operational parameters that usually are not changed. */
 204/* Time in jiffies before concluding the transmitter is hung. */
 205#define TX_TIMEOUT  (6*HZ)
 206
 207
 208enum {
 209        HAS_MII_XCVR = 0x010000,
 210        HAS_CHIP_XCVR = 0x020000,
 211        HAS_LNK_CHNG = 0x040000,
 212};
 213
 214#define RTL_NUM_STATS 4         /* number of ETHTOOL_GSTATS u64's */
 215#define RTL_REGS_VER 1          /* version of reg. data in ETHTOOL_GREGS */
 216#define RTL_MIN_IO_SIZE 0x80
 217#define RTL8139B_IO_SIZE 256
 218
 219#define RTL8129_CAPS    HAS_MII_XCVR
 220#define RTL8139_CAPS    HAS_CHIP_XCVR|HAS_LNK_CHNG
 221
 222typedef enum {
 223        RTL8139 = 0,
 224        RTL8129,
 225} board_t;
 226
 227
 228/* indexed by board_t, above */
 229static struct {
 230        const char *name;
 231        u32 hw_flags;
 232} board_info[] __devinitdata = {
 233        { "RealTek RTL8139", RTL8139_CAPS },
 234        { "RealTek RTL8129", RTL8129_CAPS },
 235};
 236
 237
 238static struct pci_device_id rtl8139_pci_tbl[] = {
 239        {0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 240        {0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 241        {0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 242        {0x1500, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 243        {0x4033, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 244        {0x1186, 0x1300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 245        {0x1186, 0x1340, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 246        {0x13d1, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 247        {0x1259, 0xa117, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 248        {0x1259, 0xa11e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 249        {0x14ea, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 250        {0x14ea, 0xab07, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 251        {0x11db, 0x1234, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 252        {0x1432, 0x9130, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 253        {0x02ac, 0x1012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 254
 255#ifdef CONFIG_SH_SECUREEDGE5410
 256        /* Bogus 8139 silicon reports 8129 without external PROM :-( */
 257        {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
 258#endif
 259#ifdef CONFIG_8139TOO_8129
 260        {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8129 },
 261#endif
 262
 263        /* some crazy cards report invalid vendor ids like
 264         * 0x0001 here.  The other ids are valid and constant,
 265         * so we simply don't match on the main vendor id.
 266         */
 267        {PCI_ANY_ID, 0x8139, 0x10ec, 0x8139, 0, 0, RTL8139 },
 268        {PCI_ANY_ID, 0x8139, 0x1186, 0x1300, 0, 0, RTL8139 },
 269        {PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },
 270
 271        {0,}
 272};
 273MODULE_DEVICE_TABLE (pci, rtl8139_pci_tbl);
 274
 275static struct {
 276        const char str[ETH_GSTRING_LEN];
 277} ethtool_stats_keys[] = {
 278        { "early_rx" },
 279        { "tx_buf_mapped" },
 280        { "tx_timeouts" },
 281        { "rx_lost_in_ring" },
 282};
 283
 284/* The rest of these values should never change. */
 285
 286/* Symbolic offsets to registers. */
 287enum RTL8139_registers {
 288        MAC0 = 0,               /* Ethernet hardware address. */
 289        MAR0 = 8,               /* Multicast filter. */
 290        TxStatus0 = 0x10,       /* Transmit status (Four 32bit registers). */
 291        TxAddr0 = 0x20,         /* Tx descriptors (also four 32bit). */
 292        RxBuf = 0x30,
 293        ChipCmd = 0x37,
 294        RxBufPtr = 0x38,
 295        RxBufAddr = 0x3A,
 296        IntrMask = 0x3C,
 297        IntrStatus = 0x3E,
 298        TxConfig = 0x40,
 299        RxConfig = 0x44,
 300        Timer = 0x48,           /* A general-purpose counter. */
 301        RxMissed = 0x4C,        /* 24 bits valid, write clears. */
 302        Cfg9346 = 0x50,
 303        Config0 = 0x51,
 304        Config1 = 0x52,
 305        FlashReg = 0x54,
 306        MediaStatus = 0x58,
 307        Config3 = 0x59,
 308        Config4 = 0x5A,         /* absent on RTL-8139A */
 309        HltClk = 0x5B,
 310        MultiIntr = 0x5C,
 311        TxSummary = 0x60,
 312        BasicModeCtrl = 0x62,
 313        BasicModeStatus = 0x64,
 314        NWayAdvert = 0x66,
 315        NWayLPAR = 0x68,
 316        NWayExpansion = 0x6A,
 317        /* Undocumented registers, but required for proper operation. */
 318        FIFOTMS = 0x70,         /* FIFO Control and test. */
 319        CSCR = 0x74,            /* Chip Status and Configuration Register. */
 320        PARA78 = 0x78,
 321        PARA7c = 0x7c,          /* Magic transceiver parameter register. */
 322        Config5 = 0xD8,         /* absent on RTL-8139A */
 323};
 324
 325enum ClearBitMasks {
 326        MultiIntrClear = 0xF000,
 327        ChipCmdClear = 0xE2,
 328        Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
 329};
 330
 331enum ChipCmdBits {
 332        CmdReset = 0x10,
 333        CmdRxEnb = 0x08,
 334        CmdTxEnb = 0x04,
 335        RxBufEmpty = 0x01,
 336};
 337
 338/* Interrupt register bits, using my own meaningful names. */
 339enum IntrStatusBits {
 340        PCIErr = 0x8000,
 341        PCSTimeout = 0x4000,
 342        RxFIFOOver = 0x40,
 343        RxUnderrun = 0x20,
 344        RxOverflow = 0x10,
 345        TxErr = 0x08,
 346        TxOK = 0x04,
 347        RxErr = 0x02,
 348        RxOK = 0x01,
 349
 350        RxAckBits = RxFIFOOver | RxOverflow | RxOK,
 351};
 352
 353enum TxStatusBits {
 354        TxHostOwns = 0x2000,
 355        TxUnderrun = 0x4000,
 356        TxStatOK = 0x8000,
 357        TxOutOfWindow = 0x20000000,
 358        TxAborted = 0x40000000,
 359        TxCarrierLost = 0x80000000,
 360};
 361enum RxStatusBits {
 362        RxMulticast = 0x8000,
 363        RxPhysical = 0x4000,
 364        RxBroadcast = 0x2000,
 365        RxBadSymbol = 0x0020,
 366        RxRunt = 0x0010,
 367        RxTooLong = 0x0008,
 368        RxCRCErr = 0x0004,
 369        RxBadAlign = 0x0002,
 370        RxStatusOK = 0x0001,
 371};
 372
 373/* Bits in RxConfig. */
 374enum rx_mode_bits {
 375        AcceptErr = 0x20,
 376        AcceptRunt = 0x10,
 377        AcceptBroadcast = 0x08,
 378        AcceptMulticast = 0x04,
 379        AcceptMyPhys = 0x02,
 380        AcceptAllPhys = 0x01,
 381};
 382
 383/* Bits in TxConfig. */
 384enum tx_config_bits {
 385        TxIFG1 = (1 << 25),     /* Interframe Gap Time */
 386        TxIFG0 = (1 << 24),     /* Enabling these bits violates IEEE 802.3 */
 387        TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
 388        TxCRC = (1 << 16),      /* DISABLE appending CRC to end of Tx packets */
 389        TxClearAbt = (1 << 0),  /* Clear abort (WO) */
 390        TxDMAShift = 8,         /* DMA burst value (0-7) is shifted this many bits */
 391        TxRetryShift = 4,       /* TXRR value (0-15) is shifted this many bits */
 392
 393        TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */
 394};
 395
 396/* Bits in Config1 */
 397enum Config1Bits {
 398        Cfg1_PM_Enable = 0x01,
 399        Cfg1_VPD_Enable = 0x02,
 400        Cfg1_PIO = 0x04,
 401        Cfg1_MMIO = 0x08,
 402        LWAKE = 0x10,           /* not on 8139, 8139A */
 403        Cfg1_Driver_Load = 0x20,
 404        Cfg1_LED0 = 0x40,
 405        Cfg1_LED1 = 0x80,
 406        SLEEP = (1 << 1),       /* only on 8139, 8139A */
 407        PWRDN = (1 << 0),       /* only on 8139, 8139A */
 408};
 409
 410/* Bits in Config3 */
 411enum Config3Bits {
 412        Cfg3_FBtBEn    = (1 << 0), /* 1 = Fast Back to Back */
 413        Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */
 414        Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */
 415        Cfg3_CardB_En  = (1 << 3), /* 1 = enable CardBus registers */
 416        Cfg3_LinkUp    = (1 << 4), /* 1 = wake up on link up */
 417        Cfg3_Magic     = (1 << 5), /* 1 = wake up on Magic Packet (tm) */
 418        Cfg3_PARM_En   = (1 << 6), /* 0 = software can set twister parameters */
 419        Cfg3_GNTSel    = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */
 420};
 421
 422/* Bits in Config4 */
 423enum Config4Bits {
 424        LWPTN = (1 << 2),       /* not on 8139, 8139A */
 425};
 426
 427/* Bits in Config5 */
 428enum Config5Bits {
 429        Cfg5_PME_STS     = (1 << 0), /* 1 = PCI reset resets PME_Status */
 430        Cfg5_LANWake     = (1 << 1), /* 1 = enable LANWake signal */
 431        Cfg5_LDPS        = (1 << 2), /* 0 = save power when link is down */
 432        Cfg5_FIFOAddrPtr = (1 << 3), /* Realtek internal SRAM testing */
 433        Cfg5_UWF         = (1 << 4), /* 1 = accept unicast wakeup frame */
 434        Cfg5_MWF         = (1 << 5), /* 1 = accept multicast wakeup frame */
 435        Cfg5_BWF         = (1 << 6), /* 1 = accept broadcast wakeup frame */
 436};
 437
 438enum RxConfigBits {
 439        /* rx fifo threshold */
 440        RxCfgFIFOShift = 13,
 441        RxCfgFIFONone = (7 << RxCfgFIFOShift),
 442
 443        /* Max DMA burst */
 444        RxCfgDMAShift = 8,
 445        RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
 446
 447        /* rx ring buffer length */
 448        RxCfgRcv8K = 0,
 449        RxCfgRcv16K = (1 << 11),
 450        RxCfgRcv32K = (1 << 12),
 451        RxCfgRcv64K = (1 << 11) | (1 << 12),
 452
 453        /* Disable packet wrap at end of Rx buffer */
 454        RxNoWrap = (1 << 7),
 455};
 456
 457/* Twister tuning parameters from RealTek.
 458   Completely undocumented, but required to tune bad links on some boards. */
 459enum CSCRBits {
 460        CSCR_LinkOKBit = 0x0400,
 461        CSCR_LinkChangeBit = 0x0800,
 462        CSCR_LinkStatusBits = 0x0f000,
 463        CSCR_LinkDownOffCmd = 0x003c0,
 464        CSCR_LinkDownCmd = 0x0f3c0,
 465};
 466
 467enum Cfg9346Bits {
 468        Cfg9346_Lock = 0x00,
 469        Cfg9346_Unlock = 0xC0,
 470};
 471
 472typedef enum {
 473        CH_8139 = 0,
 474        CH_8139_K,
 475        CH_8139A,
 476        CH_8139A_G,
 477        CH_8139B,
 478        CH_8130,
 479        CH_8139C,
 480        CH_8100,
 481        CH_8100B_8139D,
 482        CH_8101,
 483} chip_t;
 484
 485enum chip_flags {
 486        HasHltClk = (1 << 0),
 487        HasLWake = (1 << 1),
 488};
 489
 490#define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
 491        (b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
 492#define HW_REVID_MASK   HW_REVID(1, 1, 1, 1, 1, 1, 1)
 493
 494/* directly indexed by chip_t, above */
 495const static struct {
 496        const char *name;
 497        u32 version; /* from RTL8139C/RTL8139D docs */
 498        u32 flags;
 499} rtl_chip_info[] = {
 500        { "RTL-8139",
 501          HW_REVID(1, 0, 0, 0, 0, 0, 0),
 502          HasHltClk,
 503        },
 504
 505        { "RTL-8139 rev K",
 506          HW_REVID(1, 1, 0, 0, 0, 0, 0),
 507          HasHltClk,
 508        },
 509
 510        { "RTL-8139A",
 511          HW_REVID(1, 1, 1, 0, 0, 0, 0),
 512          HasHltClk, /* XXX undocumented? */
 513        },
 514
 515        { "RTL-8139A rev G",
 516          HW_REVID(1, 1, 1, 0, 0, 1, 0),
 517          HasHltClk, /* XXX undocumented? */
 518        },
 519
 520        { "RTL-8139B",
 521          HW_REVID(1, 1, 1, 1, 0, 0, 0),
 522          HasLWake,
 523        },
 524
 525        { "RTL-8130",
 526          HW_REVID(1, 1, 1, 1, 1, 0, 0),
 527          HasLWake,
 528        },
 529
 530        { "RTL-8139C",
 531          HW_REVID(1, 1, 1, 0, 1, 0, 0),
 532          HasLWake,
 533        },
 534
 535        { "RTL-8100",
 536          HW_REVID(1, 1, 1, 1, 0, 1, 0),
 537          HasLWake,
 538        },
 539
 540        { "RTL-8100B/8139D",
 541          HW_REVID(1, 1, 1, 0, 1, 0, 1),
 542          HasLWake,
 543        },
 544
 545        { "RTL-8101",
 546          HW_REVID(1, 1, 1, 0, 1, 1, 1),
 547          HasLWake,
 548        },
 549};
 550
 551struct rtl_extra_stats {
 552        unsigned long early_rx;
 553        unsigned long tx_buf_mapped;
 554        unsigned long tx_timeouts;
 555        unsigned long rx_lost_in_ring;
 556};
 557
 558struct rtl8139_private {
 559        void *mmio_addr;
 560        int drv_flags;
 561        struct pci_dev *pci_dev;
 562        u32 pci_state[16];
 563        struct net_device_stats stats;
 564        unsigned char *rx_ring;
 565        unsigned int cur_rx;    /* Index into the Rx buffer of next Rx pkt. */
 566        unsigned int tx_flag;
 567        unsigned long cur_tx;
 568        unsigned long dirty_tx;
 569        unsigned char *tx_buf[NUM_TX_DESC];     /* Tx bounce buffers */
 570        unsigned char *tx_bufs; /* Tx bounce buffer region. */
 571        dma_addr_t rx_ring_dma;
 572        dma_addr_t tx_bufs_dma;
 573        signed char phys[4];            /* MII device addresses. */
 574        char twistie, twist_row, twist_col;     /* Twister tune state. */
 575        unsigned int default_port:4;    /* Last dev->if_port value. */
 576        spinlock_t lock;
 577        chip_t chipset;
 578        pid_t thr_pid;
 579        wait_queue_head_t thr_wait;
 580        struct completion thr_exited;
 581        u32 rx_config;
 582        struct rtl_extra_stats xstats;
 583        int time_to_die;
 584        struct mii_if_info mii;
 585        unsigned int regs_len;
 586};
 587
 588MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
 589MODULE_DESCRIPTION ("RealTek RTL-8139 Fast Ethernet driver");
 590MODULE_LICENSE("GPL");
 591
 592MODULE_PARM (multicast_filter_limit, "i");
 593MODULE_PARM (max_interrupt_work, "i");
 594MODULE_PARM (media, "1-" __MODULE_STRING(MAX_UNITS) "i");
 595MODULE_PARM (full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
 596MODULE_PARM (debug, "i");
 597MODULE_PARM_DESC (debug, "8139too bitmapped message enable number");
 598MODULE_PARM_DESC (multicast_filter_limit, "8139too maximum number of filtered multicast addresses");
 599MODULE_PARM_DESC (max_interrupt_work, "8139too maximum events handled per interrupt");
 600MODULE_PARM_DESC (media, "8139too: Bits 4+9: force full duplex, bit 5: 100Mbps");
 601MODULE_PARM_DESC (full_duplex, "8139too: Force full duplex for board(s) (1)");
 602
 603static int read_eeprom (void *ioaddr, int location, int addr_len);
 604static int rtl8139_open (struct net_device *dev);
 605static int mdio_read (struct net_device *dev, int phy_id, int location);
 606static void mdio_write (struct net_device *dev, int phy_id, int location,
 607                        int val);
 608static inline void rtl8139_start_thread(struct net_device *dev);
 609static void rtl8139_tx_timeout (struct net_device *dev);
 610static void rtl8139_init_ring (struct net_device *dev);
 611static int rtl8139_start_xmit (struct sk_buff *skb,
 612                               struct net_device *dev);
 613static irqreturn_t rtl8139_interrupt (int irq, void *dev_instance,
 614                               struct pt_regs *regs);
 615static int rtl8139_close (struct net_device *dev);
 616static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
 617static struct net_device_stats *rtl8139_get_stats (struct net_device *dev);
 618static void rtl8139_set_rx_mode (struct net_device *dev);
 619static void __set_rx_mode (struct net_device *dev);
 620static void rtl8139_hw_start (struct net_device *dev);
 621static struct ethtool_ops rtl8139_ethtool_ops;
 622
 623#ifdef USE_IO_OPS
 624
 625#define RTL_R8(reg)             inb (((unsigned long)ioaddr) + (reg))
 626#define RTL_R16(reg)            inw (((unsigned long)ioaddr) + (reg))
 627#define RTL_R32(reg)            ((unsigned long) inl (((unsigned long)ioaddr) + (reg)))
 628#define RTL_W8(reg, val8)       outb ((val8), ((unsigned long)ioaddr) + (reg))
 629#define RTL_W16(reg, val16)     outw ((val16), ((unsigned long)ioaddr) + (reg))
 630#define RTL_W32(reg, val32)     outl ((val32), ((unsigned long)ioaddr) + (reg))
 631#define RTL_W8_F                RTL_W8
 632#define RTL_W16_F               RTL_W16
 633#define RTL_W32_F               RTL_W32
 634#undef readb
 635#undef readw
 636#undef readl
 637#undef writeb
 638#undef writew
 639#undef writel
 640#define readb(addr) inb((unsigned long)(addr))
 641#define readw(addr) inw((unsigned long)(addr))
 642#define readl(addr) inl((unsigned long)(addr))
 643#define writeb(val,addr) outb((val),(unsigned long)(addr))
 644#define writew(val,addr) outw((val),(unsigned long)(addr))
 645#define writel(val,addr) outl((val),(unsigned long)(addr))
 646
 647#else
 648
 649/* write MMIO register, with flush */
 650/* Flush avoids rtl8139 bug w/ posted MMIO writes */
 651#define RTL_W8_F(reg, val8)     do { writeb ((val8), ioaddr + (reg)); readb (ioaddr + (reg)); } while (0)
 652#define RTL_W16_F(reg, val16)   do { writew ((val16), ioaddr + (reg)); readw (ioaddr + (reg)); } while (0)
 653#define RTL_W32_F(reg, val32)   do { writel ((val32), ioaddr + (reg)); readl (ioaddr + (reg)); } while (0)
 654
 655
 656#define MMIO_FLUSH_AUDIT_COMPLETE 1
 657#if MMIO_FLUSH_AUDIT_COMPLETE
 658
 659/* write MMIO register */
 660#define RTL_W8(reg, val8)       writeb ((val8), ioaddr + (reg))
 661#define RTL_W16(reg, val16)     writew ((val16), ioaddr + (reg))
 662#define RTL_W32(reg, val32)     writel ((val32), ioaddr + (reg))
 663
 664#else
 665
 666/* write MMIO register, then flush */
 667#define RTL_W8          RTL_W8_F
 668#define RTL_W16         RTL_W16_F
 669#define RTL_W32         RTL_W32_F
 670
 671#endif /* MMIO_FLUSH_AUDIT_COMPLETE */
 672
 673/* read MMIO register */
 674#define RTL_R8(reg)             readb (ioaddr + (reg))
 675#define RTL_R16(reg)            readw (ioaddr + (reg))
 676#define RTL_R32(reg)            ((unsigned long) readl (ioaddr + (reg)))
 677
 678#endif /* USE_IO_OPS */
 679
 680
 681static const u16 rtl8139_intr_mask =
 682        PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver |
 683        TxErr | TxOK | RxErr | RxOK;
 684
 685#ifdef USE_BUF16K 
 686static const unsigned int rtl8139_rx_config =
 687        RxCfgRcv16K | RxNoWrap |
 688        (RX_FIFO_THRESH << RxCfgFIFOShift) |
 689        (RX_DMA_BURST << RxCfgDMAShift);
 690#else
 691static const unsigned int rtl8139_rx_config =
 692        RxCfgRcv32K | RxNoWrap |
 693        (RX_FIFO_THRESH << RxCfgFIFOShift) |
 694        (RX_DMA_BURST << RxCfgDMAShift);
 695#endif
 696
 697static const unsigned int rtl8139_tx_config =
 698        (TX_DMA_BURST << TxDMAShift) | (TX_RETRY << TxRetryShift);
 699
 700static void __rtl8139_cleanup_dev (struct net_device *dev)
 701{
 702        struct rtl8139_private *tp;
 703        struct pci_dev *pdev;
 704
 705        assert (dev != NULL);
 706        assert (dev->priv != NULL);
 707
 708        tp = dev->priv;
 709        assert (tp->pci_dev != NULL);
 710        pdev = tp->pci_dev;
 711
 712#ifndef USE_IO_OPS
 713        if (tp->mmio_addr)
 714                iounmap (tp->mmio_addr);
 715#endif /* !USE_IO_OPS */
 716
 717        /* it's ok to call this even if we have no regions to free */
 718        pci_release_regions (pdev);
 719
 720        free_netdev(dev);
 721
 722        pci_set_drvdata (pdev, NULL);
 723}
 724
 725
 726static void rtl8139_chip_reset (void *ioaddr)
 727{
 728        int i;
 729
 730        /* Soft reset the chip. */
 731        RTL_W8 (ChipCmd, CmdReset);
 732
 733        /* Check that the chip has finished the reset. */
 734        for (i = 1000; i > 0; i--) {
 735                barrier();
 736                if ((RTL_R8 (ChipCmd) & CmdReset) == 0)
 737                        break;
 738                udelay (10);
 739        }
 740}
 741
 742
 743static int __devinit rtl8139_init_board (struct pci_dev *pdev,
 744                                         struct net_device **dev_out)
 745{
 746        void *ioaddr;
 747        struct net_device *dev;
 748        struct rtl8139_private *tp;
 749        u8 tmp8;
 750        int rc;
 751        unsigned int i;
 752        u32 pio_start, pio_end, pio_flags, pio_len;
 753        unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
 754        u32 version;
 755
 756        assert (pdev != NULL);
 757
 758        *dev_out = NULL;
 759
 760        /* dev and dev->priv zeroed in alloc_etherdev */
 761        dev = alloc_etherdev (sizeof (*tp));
 762        if (dev == NULL) {
 763                printk (KERN_ERR PFX "%s: Unable to alloc new net device\n", pci_name(pdev));
 764                return -ENOMEM;
 765        }
 766        SET_MODULE_OWNER(dev);
 767        SET_NETDEV_DEV(dev, &pdev->dev);
 768
 769        tp = dev->priv;
 770        tp->pci_dev = pdev;
 771
 772        /* enable device (incl. PCI PM wakeup and hotplug setup) */
 773        rc = pci_enable_device (pdev);
 774        if (rc)
 775                goto err_out;
 776
 777        pio_start = pci_resource_start (pdev, 0);
 778        pio_end = pci_resource_end (pdev, 0);
 779        pio_flags = pci_resource_flags (pdev, 0);
 780        pio_len = pci_resource_len (pdev, 0);
 781
 782        mmio_start = pci_resource_start (pdev, 1);
 783        mmio_end = pci_resource_end (pdev, 1);
 784        mmio_flags = pci_resource_flags (pdev, 1);
 785        mmio_len = pci_resource_len (pdev, 1);
 786
 787        /* set this immediately, we need to know before
 788         * we talk to the chip directly */
 789        DPRINTK("PIO region size == 0x%02X\n", pio_len);
 790        DPRINTK("MMIO region size == 0x%02lX\n", mmio_len);
 791
 792#ifdef USE_IO_OPS
 793        /* make sure PCI base addr 0 is PIO */
 794        if (!(pio_flags & IORESOURCE_IO)) {
 795                printk (KERN_ERR PFX "%s: region #0 not a PIO resource, aborting\n", pci_name(pdev));
 796                rc = -ENODEV;
 797                goto err_out;
 798        }
 799        /* check for weird/broken PCI region reporting */
 800        if (pio_len < RTL_MIN_IO_SIZE) {
 801                printk (KERN_ERR PFX "%s: Invalid PCI I/O region size(s), aborting\n", pci_name(pdev));
 802                rc = -ENODEV;
 803                goto err_out;
 804        }
 805#else
 806        /* make sure PCI base addr 1 is MMIO */
 807        if (!(mmio_flags & IORESOURCE_MEM)) {
 808                printk (KERN_ERR PFX "%s: region #1 not an MMIO resource, aborting\n", pci_name(pdev));
 809                rc = -ENODEV;
 810                goto err_out;
 811        }
 812        if (mmio_len < RTL_MIN_IO_SIZE) {
 813                printk (KERN_ERR PFX "%s: Invalid PCI mem region size(s), aborting\n", pci_name(pdev));
 814                rc = -ENODEV;
 815                goto err_out;
 816        }
 817#endif
 818
 819        rc = pci_request_regions (pdev, "8139too");
 820        if (rc)
 821                goto err_out;
 822
 823        /* enable PCI bus-mastering */
 824        pci_set_master (pdev);
 825
 826#ifdef USE_IO_OPS
 827        ioaddr = (void *) pio_start;
 828        dev->base_addr = pio_start;
 829        tp->mmio_addr = ioaddr;
 830        tp->regs_len = pio_len;
 831#else
 832        /* ioremap MMIO region */
 833        ioaddr = ioremap (mmio_start, mmio_len);
 834        if (ioaddr == NULL) {
 835                printk (KERN_ERR PFX "%s: cannot remap MMIO, aborting\n", pci_name(pdev));
 836                rc = -EIO;
 837                goto err_out;
 838        }
 839        dev->base_addr = (long) ioaddr;
 840        tp->mmio_addr = ioaddr;
 841        tp->regs_len = mmio_len;
 842#endif /* USE_IO_OPS */
 843
 844        /* Bring old chips out of low-power mode. */
 845        RTL_W8 (HltClk, 'R');
 846
 847        /* check for missing/broken hardware */
 848        if (RTL_R32 (TxConfig) == 0xFFFFFFFF) {
 849                printk (KERN_ERR PFX "%s: Chip not responding, ignoring board\n",
 850                        pci_name(pdev));
 851                rc = -EIO;
 852                goto err_out;
 853        }
 854
 855        /* identify chip attached to board */
 856        version = RTL_R32 (TxConfig) & HW_REVID_MASK;
 857        for (i = 0; i < ARRAY_SIZE (rtl_chip_info); i++)
 858                if (version == rtl_chip_info[i].version) {
 859                        tp->chipset = i;
 860                        goto match;
 861                }
 862
 863        /* if unknown chip, assume array element #0, original RTL-8139 in this case */
 864        printk (KERN_DEBUG PFX "%s: unknown chip version, assuming RTL-8139\n",
 865                pci_name(pdev));
 866        printk (KERN_DEBUG PFX "%s: TxConfig = 0x%lx\n", pci_name(pdev), RTL_R32 (TxConfig));
 867        tp->chipset = 0;
 868
 869match:
 870        DPRINTK ("chipset id (%d) == index %d, '%s'\n",
 871                tmp,
 872                tp->chipset,
 873                rtl_chip_info[tp->chipset].name);
 874
 875        if (tp->chipset >= CH_8139B) {
 876                u8 new_tmp8 = tmp8 = RTL_R8 (Config1);
 877                DPRINTK("PCI PM wakeup\n");
 878                if ((rtl_chip_info[tp->chipset].flags & HasLWake) &&
 879                    (tmp8 & LWAKE))
 880                        new_tmp8 &= ~LWAKE;
 881                new_tmp8 |= Cfg1_PM_Enable;
 882                if (new_tmp8 != tmp8) {
 883                        RTL_W8 (Cfg9346, Cfg9346_Unlock);
 884                        RTL_W8 (Config1, tmp8);
 885                        RTL_W8 (Cfg9346, Cfg9346_Lock);
 886                }
 887                if (rtl_chip_info[tp->chipset].flags & HasLWake) {
 888                        tmp8 = RTL_R8 (Config4);
 889                        if (tmp8 & LWPTN) {
 890                                RTL_W8 (Cfg9346, Cfg9346_Unlock);
 891                                RTL_W8 (Config4, tmp8 & ~LWPTN);
 892                                RTL_W8 (Cfg9346, Cfg9346_Lock);
 893                        }
 894                }
 895        } else {
 896                DPRINTK("Old chip wakeup\n");
 897                tmp8 = RTL_R8 (Config1);
 898                tmp8 &= ~(SLEEP | PWRDN);
 899                RTL_W8 (Config1, tmp8);
 900        }
 901
 902        rtl8139_chip_reset (ioaddr);
 903
 904        *dev_out = dev;
 905        return 0;
 906
 907err_out:
 908        __rtl8139_cleanup_dev (dev);
 909        return rc;
 910}
 911
 912
 913static int __devinit rtl8139_init_one (struct pci_dev *pdev,
 914                                       const struct pci_device_id *ent)
 915{
 916        struct net_device *dev = NULL;
 917        struct rtl8139_private *tp;
 918        int i, addr_len, option;
 919        void *ioaddr;
 920        static int board_idx = -1;
 921        u8 pci_rev;
 922
 923        assert (pdev != NULL);
 924        assert (ent != NULL);
 925
 926        board_idx++;
 927
 928        /* when we're built into the kernel, the driver version message
 929         * is only printed if at least one 8139 board has been found
 930         */
 931#ifndef MODULE
 932        {
 933                static int printed_version;
 934                if (!printed_version++)
 935                        printk (KERN_INFO RTL8139_DRIVER_NAME "\n");
 936        }
 937#endif
 938
 939        pci_read_config_byte(pdev, PCI_REVISION_ID, &pci_rev);
 940
 941        if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
 942            pdev->device == PCI_DEVICE_ID_REALTEK_8139 && pci_rev >= 0x20) {
 943                printk(KERN_INFO PFX "pci dev %s (id %04x:%04x rev %02x) is an enhanced 8139C+ chip\n",
 944                       pci_name(pdev), pdev->vendor, pdev->device, pci_rev);
 945                printk(KERN_INFO PFX "Use the \"8139cp\" driver for improved performance and stability.\n");
 946        }
 947
 948        i = rtl8139_init_board (pdev, &dev);
 949        if (i < 0)
 950                return i;
 951
 952        tp = dev->priv;
 953        ioaddr = tp->mmio_addr;
 954
 955        assert (ioaddr != NULL);
 956        assert (dev != NULL);
 957        assert (tp != NULL);
 958
 959        addr_len = read_eeprom (ioaddr, 0, 8) == 0x8129 ? 8 : 6;
 960        for (i = 0; i < 3; i++)
 961                ((u16 *) (dev->dev_addr))[i] =
 962                    le16_to_cpu (read_eeprom (ioaddr, i + 7, addr_len));
 963
 964        /* The Rtl8139-specific entries in the device structure. */
 965        dev->open = rtl8139_open;
 966        dev->hard_start_xmit = rtl8139_start_xmit;
 967        dev->stop = rtl8139_close;
 968        dev->get_stats = rtl8139_get_stats;
 969        dev->set_multicast_list = rtl8139_set_rx_mode;
 970        dev->do_ioctl = netdev_ioctl;
 971        dev->ethtool_ops = &rtl8139_ethtool_ops;
 972        dev->tx_timeout = rtl8139_tx_timeout;
 973        dev->watchdog_timeo = TX_TIMEOUT;
 974
 975        /* note: the hardware is not capable of sg/csum/highdma, however
 976         * through the use of skb_copy_and_csum_dev we enable these
 977         * features
 978         */
 979        dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA;
 980
 981        dev->irq = pdev->irq;
 982
 983        /* dev->priv/tp zeroed and aligned in alloc_etherdev */
 984        tp = dev->priv;
 985
 986        /* note: tp->chipset set in rtl8139_init_board */
 987        tp->drv_flags = board_info[ent->driver_data].hw_flags;
 988        tp->mmio_addr = ioaddr;
 989        spin_lock_init (&tp->lock);
 990        init_waitqueue_head (&tp->thr_wait);
 991        init_completion (&tp->thr_exited);
 992        tp->mii.dev = dev;
 993        tp->mii.mdio_read = mdio_read;
 994        tp->mii.mdio_write = mdio_write;
 995        tp->mii.phy_id_mask = 0x3f;
 996        tp->mii.reg_num_mask = 0x1f;
 997
 998        /* dev is fully set up and ready to use now */
 999        DPRINTK("about to register device named %s (%p)...\n", dev->name, dev);
1000        i = register_netdev (dev);
1001        if (i) goto err_out;
1002
1003        pci_set_drvdata (pdev, dev);
1004
1005        printk (KERN_INFO "%s: %s at 0x%lx, "
1006                "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
1007                "IRQ %d\n",
1008                dev->name,
1009                board_info[ent->driver_data].name,
1010                dev->base_addr,
1011                dev->dev_addr[0], dev->dev_addr[1],
1012                dev->dev_addr[2], dev->dev_addr[3],
1013                dev->dev_addr[4], dev->dev_addr[5],
1014                dev->irq);
1015
1016        printk (KERN_DEBUG "%s:  Identified 8139 chip type '%s'\n",
1017                dev->name, rtl_chip_info[tp->chipset].name);
1018
1019        /* Find the connected MII xcvrs.
1020           Doing this in open() would allow detecting external xcvrs later, but
1021           takes too much time. */
1022#ifdef CONFIG_8139TOO_8129
1023        if (tp->drv_flags & HAS_MII_XCVR) {
1024                int phy, phy_idx = 0;
1025                for (phy = 0; phy < 32 && phy_idx < sizeof(tp->phys); phy++) {
1026                        int mii_status = mdio_read(dev, phy, 1);
1027                        if (mii_status != 0xffff  &&  mii_status != 0x0000) {
1028                                u16 advertising = mdio_read(dev, phy, 4);
1029                                tp->phys[phy_idx++] = phy;
1030                                printk(KERN_INFO "%s: MII transceiver %d status 0x%4.4x "
1031                                           "advertising %4.4x.\n",
1032                                           dev->name, phy, mii_status, advertising);
1033                        }
1034                }
1035                if (phy_idx == 0) {
1036                        printk(KERN_INFO "%s: No MII transceivers found!  Assuming SYM "
1037                                   "transceiver.\n",
1038                                   dev->name);
1039                        tp->phys[0] = 32;
1040                }
1041        } else
1042#endif
1043                tp->phys[0] = 32;
1044        tp->mii.phy_id = tp->phys[0];
1045
1046        /* The lower four bits are the media type. */
1047        option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
1048        if (option > 0) {
1049                tp->mii.full_duplex = (option & 0x210) ? 1 : 0;
1050                tp->default_port = option & 0xFF;
1051                if (tp->default_port)
1052                        tp->mii.force_media = 1;
1053        }
1054        if (board_idx < MAX_UNITS  &&  full_duplex[board_idx] > 0)
1055                tp->mii.full_duplex = full_duplex[board_idx];
1056        if (tp->mii.full_duplex) {
1057                printk(KERN_INFO "%s: Media type forced to Full Duplex.\n", dev->name);
1058                /* Changing the MII-advertised media because might prevent
1059                   re-connection. */
1060                tp->mii.force_media = 1;
1061        }
1062        if (tp->default_port) {
1063                printk(KERN_INFO "  Forcing %dMbps %s-duplex operation.\n",
1064                           (option & 0x20 ? 100 : 10),
1065                           (option & 0x10 ? "full" : "half"));
1066                mdio_write(dev, tp->phys[0], 0,
1067                                   ((option & 0x20) ? 0x2000 : 0) |     /* 100Mbps? */
1068                                   ((option & 0x10) ? 0x0100 : 0)); /* Full duplex? */
1069        }
1070
1071        /* Put the chip into low-power mode. */
1072        if (rtl_chip_info[tp->chipset].flags & HasHltClk)
1073                RTL_W8 (HltClk, 'H');   /* 'R' would leave the clock running. */
1074
1075        return 0;
1076
1077err_out:
1078        __rtl8139_cleanup_dev (dev);
1079        return i;
1080}
1081
1082
1083static void __devexit rtl8139_remove_one (struct pci_dev *pdev)
1084{
1085        struct net_device *dev = pci_get_drvdata (pdev);
1086        struct rtl8139_private *np;
1087
1088        assert (dev != NULL);
1089        np = dev->priv;
1090        assert (np != NULL);
1091
1092        unregister_netdev (dev);
1093
1094        __rtl8139_cleanup_dev (dev);
1095}
1096
1097
1098/* Serial EEPROM section. */
1099
1100/*  EEPROM_Ctrl bits. */
1101#define EE_SHIFT_CLK    0x04    /* EEPROM shift clock. */
1102#define EE_CS                   0x08    /* EEPROM chip select. */
1103#define EE_DATA_WRITE   0x02    /* EEPROM chip data in. */
1104#define EE_WRITE_0              0x00
1105#define EE_WRITE_1              0x02
1106#define EE_DATA_READ    0x01    /* EEPROM chip data out. */
1107#define EE_ENB                  (0x80 | EE_CS)
1108
1109/* Delay between EEPROM clock transitions.
1110   No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
1111 */
1112
1113#define eeprom_delay()  readl(ee_addr)
1114
1115/* The EEPROM commands include the alway-set leading bit. */
1116#define EE_WRITE_CMD    (5)
1117#define EE_READ_CMD             (6)
1118#define EE_ERASE_CMD    (7)
1119
1120static int __devinit read_eeprom (void *ioaddr, int location, int addr_len)
1121{
1122        int i;
1123        unsigned retval = 0;
1124        void *ee_addr = ioaddr + Cfg9346;
1125        int read_cmd = location | (EE_READ_CMD << addr_len);
1126
1127        writeb (EE_ENB & ~EE_CS, ee_addr);
1128        writeb (EE_ENB, ee_addr);
1129        eeprom_delay ();
1130
1131        /* Shift the read command bits out. */
1132        for (i = 4 + addr_len; i >= 0; i--) {
1133                int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1134                writeb (EE_ENB | dataval, ee_addr);
1135                eeprom_delay ();
1136                writeb (EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1137                eeprom_delay ();
1138        }
1139        writeb (EE_ENB, ee_addr);
1140        eeprom_delay ();
1141
1142        for (i = 16; i > 0; i--) {
1143                writeb (EE_ENB | EE_SHIFT_CLK, ee_addr);
1144                eeprom_delay ();
1145                retval =
1146                    (retval << 1) | ((readb (ee_addr) & EE_DATA_READ) ? 1 :
1147                                     0);
1148                writeb (EE_ENB, ee_addr);
1149                eeprom_delay ();
1150        }
1151
1152        /* Terminate the EEPROM access. */
1153        writeb (~EE_CS, ee_addr);
1154        eeprom_delay ();
1155
1156        return retval;
1157}
1158
1159/* MII serial management: mostly bogus for now. */
1160/* Read and write the MII management registers using software-generated
1161   serial MDIO protocol.
1162   The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
1163   met by back-to-back PCI I/O cycles, but we insert a delay to avoid
1164   "overclocking" issues. */
1165#define MDIO_DIR                0x80
1166#define MDIO_DATA_OUT   0x04
1167#define MDIO_DATA_IN    0x02
1168#define MDIO_CLK                0x01
1169#define MDIO_WRITE0 (MDIO_DIR)
1170#define MDIO_WRITE1 (MDIO_DIR | MDIO_DATA_OUT)
1171
1172#define mdio_delay(mdio_addr)   readb(mdio_addr)
1173
1174
1175static char mii_2_8139_map[8] = {
1176        BasicModeCtrl,
1177        BasicModeStatus,
1178        0,
1179        0,
1180        NWayAdvert,
1181        NWayLPAR,
1182        NWayExpansion,
1183        0
1184};
1185
1186
1187#ifdef CONFIG_8139TOO_8129
1188/* Syncronize the MII management interface by shifting 32 one bits out. */
1189static void mdio_sync (void *mdio_addr)
1190{
1191        int i;
1192
1193        for (i = 32; i >= 0; i--) {
1194                writeb (MDIO_WRITE1, mdio_addr);
1195                mdio_delay (mdio_addr);
1196                writeb (MDIO_WRITE1 | MDIO_CLK, mdio_addr);
1197                mdio_delay (mdio_addr);
1198        }
1199}
1200#endif
1201
1202static int mdio_read (struct net_device *dev, int phy_id, int location)
1203{
1204        struct rtl8139_private *tp = dev->priv;
1205        int retval = 0;
1206#ifdef CONFIG_8139TOO_8129
1207        void *mdio_addr = tp->mmio_addr + Config4;
1208        int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
1209        int i;
1210#endif
1211
1212        if (phy_id > 31) {      /* Really a 8139.  Use internal registers. */
1213                return location < 8 && mii_2_8139_map[location] ?
1214                    readw (tp->mmio_addr + mii_2_8139_map[location]) : 0;
1215        }
1216
1217#ifdef CONFIG_8139TOO_8129
1218        mdio_sync (mdio_addr);
1219        /* Shift the read command bits out. */
1220        for (i = 15; i >= 0; i--) {
1221                int dataval = (mii_cmd & (1 << i)) ? MDIO_DATA_OUT : 0;
1222
1223                writeb (MDIO_DIR | dataval, mdio_addr);
1224                mdio_delay (mdio_addr);
1225                writeb (MDIO_DIR | dataval | MDIO_CLK, mdio_addr);
1226                mdio_delay (mdio_addr);
1227        }
1228
1229        /* Read the two transition, 16 data, and wire-idle bits. */
1230        for (i = 19; i > 0; i--) {
1231                writeb (0, mdio_addr);
1232                mdio_delay (mdio_addr);
1233                retval = (retval << 1) | ((readb (mdio_addr) & MDIO_DATA_IN) ? 1 : 0);
1234                writeb (MDIO_CLK, mdio_addr);
1235                mdio_delay (mdio_addr);
1236        }
1237#endif
1238
1239        return (retval >> 1) & 0xffff;
1240}
1241
1242
1243static void mdio_write (struct net_device *dev, int phy_id, int location,
1244                        int value)
1245{
1246        struct rtl8139_private *tp = dev->priv;
1247#ifdef CONFIG_8139TOO_8129
1248        void *mdio_addr = tp->mmio_addr + Config4;
1249        int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
1250        int i;
1251#endif
1252
1253        if (phy_id > 31) {      /* Really a 8139.  Use internal registers. */
1254                void *ioaddr = tp->mmio_addr;
1255                if (location == 0) {
1256                        RTL_W8 (Cfg9346, Cfg9346_Unlock);
1257                        RTL_W16 (BasicModeCtrl, value);
1258                        RTL_W8 (Cfg9346, Cfg9346_Lock);
1259                } else if (location < 8 && mii_2_8139_map[location])
1260                        RTL_W16 (mii_2_8139_map[location], value);
1261                return;
1262        }
1263
1264#ifdef CONFIG_8139TOO_8129
1265        mdio_sync (mdio_addr);
1266
1267        /* Shift the command bits out. */
1268        for (i = 31; i >= 0; i--) {
1269                int dataval =
1270                    (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
1271                writeb (dataval, mdio_addr);
1272                mdio_delay (mdio_addr);
1273                writeb (dataval | MDIO_CLK, mdio_addr);
1274                mdio_delay (mdio_addr);
1275        }
1276        /* Clear out extra bits. */
1277        for (i = 2; i > 0; i--) {
1278                writeb (0, mdio_addr);
1279                mdio_delay (mdio_addr);
1280                writeb (MDIO_CLK, mdio_addr);
1281                mdio_delay (mdio_addr);
1282        }
1283#endif
1284}
1285
1286
1287static int rtl8139_open (struct net_device *dev)
1288{
1289        struct rtl8139_private *tp = dev->priv;
1290        int retval;
1291#ifdef RTL8139_DEBUG
1292        void *ioaddr = tp->mmio_addr;
1293#endif
1294
1295        retval = request_irq (dev->irq, rtl8139_interrupt, SA_SHIRQ, dev->name, dev);
1296        if (retval)
1297                return retval;
1298
1299        tp->tx_bufs = pci_alloc_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
1300                                           &tp->tx_bufs_dma);
1301        tp->rx_ring = pci_alloc_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
1302                                           &tp->rx_ring_dma);
1303        if (tp->tx_bufs == NULL || tp->rx_ring == NULL) {
1304                free_irq(dev->irq, dev);
1305
1306                if (tp->tx_bufs)
1307                        pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
1308                                            tp->tx_bufs, tp->tx_bufs_dma);
1309                if (tp->rx_ring)
1310                        pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
1311                                            tp->rx_ring, tp->rx_ring_dma);
1312
1313                return -ENOMEM;
1314
1315        }
1316
1317        tp->mii.full_duplex = tp->mii.force_media;
1318        tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000;
1319
1320        rtl8139_init_ring (dev);
1321        rtl8139_hw_start (dev);
1322
1323        DPRINTK ("%s: rtl8139_open() ioaddr %#lx IRQ %d"
1324                        " GP Pins %2.2x %s-duplex.\n",
1325                        dev->name, pci_resource_start (tp->pci_dev, 1),
1326                        dev->irq, RTL_R8 (MediaStatus),
1327                        tp->mii.full_duplex ? "full" : "half");
1328
1329        rtl8139_start_thread(dev);
1330
1331        return 0;
1332}
1333
1334
1335static void rtl_check_media (struct net_device *dev, unsigned int init_media)
1336{
1337        struct rtl8139_private *tp = dev->priv;
1338
1339        if (tp->phys[0] >= 0) {
1340                mii_check_media(&tp->mii, 1, init_media);
1341        }
1342}
1343
1344/* Start the hardware at open or resume. */
1345static void rtl8139_hw_start (struct net_device *dev)
1346{
1347        struct rtl8139_private *tp = dev->priv;
1348        void *ioaddr = tp->mmio_addr;
1349        u32 i;
1350        u8 tmp;
1351
1352        /* Bring old chips out of low-power mode. */
1353        if (rtl_chip_info[tp->chipset].flags & HasHltClk)
1354                RTL_W8 (HltClk, 'R');
1355
1356        rtl8139_chip_reset (ioaddr);
1357
1358        /* unlock Config[01234] and BMCR register writes */
1359        RTL_W8_F (Cfg9346, Cfg9346_Unlock);
1360        /* Restore our idea of the MAC address. */
1361        RTL_W32_F (MAC0 + 0, cpu_to_le32 (*(u32 *) (dev->dev_addr + 0)));
1362        RTL_W32_F (MAC0 + 4, cpu_to_le32 (*(u32 *) (dev->dev_addr + 4)));
1363
1364        /* Must enable Tx/Rx before setting transfer thresholds! */
1365        RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
1366
1367        tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
1368        RTL_W32 (RxConfig, tp->rx_config);
1369
1370        /* Check this value: the documentation for IFG contradicts ifself. */
1371        RTL_W32 (TxConfig, rtl8139_tx_config);
1372
1373        tp->cur_rx = 0;
1374
1375        rtl_check_media (dev, 1);
1376
1377        if (tp->chipset >= CH_8139B) {
1378                /* Disable magic packet scanning, which is enabled
1379                 * when PM is enabled in Config1.  It can be reenabled
1380                 * via ETHTOOL_SWOL if desired.  */
1381                RTL_W8 (Config3, RTL_R8 (Config3) & ~Cfg3_Magic);
1382        }
1383
1384        DPRINTK("init buffer addresses\n");
1385
1386        /* Lock Config[01234] and BMCR register writes */
1387        RTL_W8 (Cfg9346, Cfg9346_Lock);
1388
1389        /* init Rx ring buffer DMA address */
1390        RTL_W32_F (RxBuf, tp->rx_ring_dma);
1391
1392        /* init Tx buffer DMA addresses */
1393        for (i = 0; i < NUM_TX_DESC; i++)
1394                RTL_W32_F (TxAddr0 + (i * 4), tp->tx_bufs_dma + (tp->tx_buf[i] - tp->tx_bufs));
1395
1396        RTL_W32 (RxMissed, 0);
1397
1398        rtl8139_set_rx_mode (dev);
1399
1400        /* no early-rx interrupts */
1401        RTL_W16 (MultiIntr, RTL_R16 (MultiIntr) & MultiIntrClear);
1402
1403        /* make sure RxTx has started */
1404        tmp = RTL_R8 (ChipCmd);
1405        if ((!(tmp & CmdRxEnb)) || (!(tmp & CmdTxEnb)))
1406                RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
1407
1408        /* Enable all known interrupts by setting the interrupt mask. */
1409        RTL_W16 (IntrMask, rtl8139_intr_mask);
1410
1411        netif_start_queue (dev);
1412}
1413
1414
1415/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1416static void rtl8139_init_ring (struct net_device *dev)
1417{
1418        struct rtl8139_private *tp = dev->priv;
1419        int i;
1420
1421        tp->cur_rx = 0;
1422        tp->cur_tx = 0;
1423        tp->dirty_tx = 0;
1424
1425        for (i = 0; i < NUM_TX_DESC; i++)
1426                tp->tx_buf[i] = &tp->tx_bufs[i * TX_BUF_SIZE];
1427}
1428
1429
1430/* This must be global for CONFIG_8139TOO_TUNE_TWISTER case */
1431static int next_tick = 3 * HZ;
1432
1433#ifndef CONFIG_8139TOO_TUNE_TWISTER
1434static inline void rtl8139_tune_twister (struct net_device *dev,
1435                                  struct rtl8139_private *tp) {}
1436#else
1437enum TwisterParamVals {
1438        PARA78_default  = 0x78fa8388,
1439        PARA7c_default  = 0xcb38de43,   /* param[0][3] */
1440        PARA7c_xxx      = 0xcb38de43,
1441};
1442
1443static const unsigned long param[4][4] = {
1444        {0xcb39de43, 0xcb39ce43, 0xfb38de03, 0xcb38de43},
1445        {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
1446        {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
1447        {0xbb39de43, 0xbb39ce43, 0xbb39ce83, 0xbb39ce83}
1448};
1449
1450static void rtl8139_tune_twister (struct net_device *dev,
1451                                  struct rtl8139_private *tp)
1452{
1453        int linkcase;
1454        void *ioaddr = tp->mmio_addr;
1455
1456        /* This is a complicated state machine to configure the "twister" for
1457           impedance/echos based on the cable length.
1458           All of this is magic and undocumented.
1459         */
1460        switch (tp->twistie) {
1461        case 1:
1462                if (RTL_R16 (CSCR) & CSCR_LinkOKBit) {
1463                        /* We have link beat, let us tune the twister. */
1464                        RTL_W16 (CSCR, CSCR_LinkDownOffCmd);
1465                        tp->twistie = 2;        /* Change to state 2. */
1466                        next_tick = HZ / 10;
1467                } else {
1468                        /* Just put in some reasonable defaults for when beat returns. */
1469                        RTL_W16 (CSCR, CSCR_LinkDownCmd);
1470                        RTL_W32 (FIFOTMS, 0x20);        /* Turn on cable test mode. */
1471                        RTL_W32 (PARA78, PARA78_default);
1472                        RTL_W32 (PARA7c, PARA7c_default);
1473                        tp->twistie = 0;        /* Bail from future actions. */
1474                }
1475                break;
1476        case 2:
1477                /* Read how long it took to hear the echo. */
1478                linkcase = RTL_R16 (CSCR) & CSCR_LinkStatusBits;
1479                if (linkcase == 0x7000)
1480                        tp->twist_row = 3;
1481                else if (linkcase == 0x3000)
1482                        tp->twist_row = 2;
1483                else if (linkcase == 0x1000)
1484                        tp->twist_row = 1;
1485                else
1486                        tp->twist_row = 0;
1487                tp->twist_col = 0;
1488                tp->twistie = 3;        /* Change to state 2. */
1489                next_tick = HZ / 10;
1490                break;
1491        case 3:
1492                /* Put out four tuning parameters, one per 100msec. */
1493                if (tp->twist_col == 0)
1494                        RTL_W16 (FIFOTMS, 0);
1495                RTL_W32 (PARA7c, param[(int) tp->twist_row]
1496                         [(int) tp->twist_col]);
1497                next_tick = HZ / 10;
1498                if (++tp->twist_col >= 4) {
1499                        /* For short cables we are done.
1500                           For long cables (row == 3) check for mistune. */
1501                        tp->twistie =
1502                            (tp->twist_row == 3) ? 4 : 0;
1503                }
1504                break;
1505        case 4:
1506                /* Special case for long cables: check for mistune. */
1507                if ((RTL_R16 (CSCR) &
1508                     CSCR_LinkStatusBits) == 0x7000) {
1509                        tp->twistie = 0;
1510                        break;
1511                } else {
1512                        RTL_W32 (PARA7c, 0xfb38de03);
1513                        tp->twistie = 5;
1514                        next_tick = HZ / 10;
1515                }
1516                break;
1517        case 5:
1518                /* Retune for shorter cable (column 2). */
1519                RTL_W32 (FIFOTMS, 0x20);
1520                RTL_W32 (PARA78, PARA78_default);
1521                RTL_W32 (PARA7c, PARA7c_default);
1522                RTL_W32 (FIFOTMS, 0x00);
1523                tp->twist_row = 2;
1524                tp->twist_col = 0;
1525                tp->twistie = 3;
1526                next_tick = HZ / 10;
1527                break;
1528
1529        default:
1530                /* do nothing */
1531                break;
1532        }
1533}
1534#endif /* CONFIG_8139TOO_TUNE_TWISTER */
1535
1536static inline void rtl8139_thread_iter (struct net_device *dev,
1537                                 struct rtl8139_private *tp,
1538                                 void *ioaddr)
1539{
1540        int mii_lpa;
1541
1542        mii_lpa = mdio_read (dev, tp->phys[0], MII_LPA);
1543
1544        if (!tp->mii.force_media && mii_lpa != 0xffff) {
1545                int duplex = (mii_lpa & LPA_100FULL)
1546                    || (mii_lpa & 0x01C0) == 0x0040;
1547                if (tp->mii.full_duplex != duplex) {
1548                        tp->mii.full_duplex = duplex;
1549
1550                        if (mii_lpa) {
1551                                printk (KERN_INFO
1552                                        "%s: Setting %s-duplex based on MII #%d link"
1553                                        " partner ability of %4.4x.\n",
1554                                        dev->name,
1555                                        tp->mii.full_duplex ? "full" : "half",
1556                                        tp->phys[0], mii_lpa);
1557                        } else {
1558                                printk(KERN_INFO"%s: media is unconnected, link down, or incompatible connection\n",
1559                                       dev->name);
1560                        }
1561#if 0
1562                        RTL_W8 (Cfg9346, Cfg9346_Unlock);
1563                        RTL_W8 (Config1, tp->mii.full_duplex ? 0x60 : 0x20);
1564                        RTL_W8 (Cfg9346, Cfg9346_Lock);
1565#endif
1566                }
1567        }
1568
1569        next_tick = HZ * 60;
1570
1571        rtl8139_tune_twister (dev, tp);
1572
1573        DPRINTK ("%s: Media selection tick, Link partner %4.4x.\n",
1574                 dev->name, RTL_R16 (NWayLPAR));
1575        DPRINTK ("%s:  Other registers are IntMask %4.4x IntStatus %4.4x\n",
1576                 dev->name, RTL_R16 (IntrMask), RTL_R16 (IntrStatus));
1577        DPRINTK ("%s:  Chip config %2.2x %2.2x.\n",
1578                 dev->name, RTL_R8 (Config0),
1579                 RTL_R8 (Config1));
1580}
1581
1582static int rtl8139_thread (void *data)
1583{
1584        struct net_device *dev = data;
1585        struct rtl8139_private *tp = dev->priv;
1586        unsigned long timeout;
1587
1588        daemonize("%s", dev->name);
1589        allow_signal(SIGTERM);
1590
1591        while (1) {
1592                timeout = next_tick;
1593                do {
1594                        timeout = interruptible_sleep_on_timeout (&tp->thr_wait, timeout);
1595                        /* make swsusp happy with our thread */
1596                        if (current->flags & PF_FREEZE)
1597                                refrigerator(PF_IOTHREAD);
1598                } while (!signal_pending (current) && (timeout > 0));
1599
1600                if (signal_pending (current)) {
1601                        flush_signals(current);
1602                }
1603
1604                if (tp->time_to_die)
1605                        break;
1606
1607                rtnl_lock ();
1608                rtl8139_thread_iter (dev, tp, tp->mmio_addr);
1609                rtnl_unlock ();
1610        }
1611
1612        complete_and_exit (&tp->thr_exited, 0);
1613}
1614
1615static inline void rtl8139_start_thread(struct net_device *dev)
1616{
1617        struct rtl8139_private *tp = dev->priv;
1618
1619        tp->thr_pid = -1;
1620        tp->twistie = 0;
1621        tp->time_to_die = 0;
1622        if (tp->chipset == CH_8139_K)
1623                tp->twistie = 1;
1624        else if (tp->drv_flags & HAS_LNK_CHNG)
1625                return;
1626
1627        tp->thr_pid = kernel_thread(rtl8139_thread, dev, CLONE_FS|CLONE_FILES);
1628        if (tp->thr_pid < 0) {
1629                printk (KERN_WARNING "%s: unable to start kernel thread\n",
1630                        dev->name);
1631        }
1632}
1633
1634static void rtl8139_tx_clear (struct rtl8139_private *tp)
1635{
1636        tp->cur_tx = 0;
1637        tp->dirty_tx = 0;
1638
1639        /* XXX account for unsent Tx packets in tp->stats.tx_dropped */
1640}
1641
1642
1643static void rtl8139_tx_timeout (struct net_device *dev)
1644{
1645        struct rtl8139_private *tp = dev->priv;
1646        void *ioaddr = tp->mmio_addr;
1647        int i;
1648        u8 tmp8;
1649        unsigned long flags;
1650
1651        DPRINTK ("%s: Transmit timeout, status %2.2x %4.4x "
1652                 "media %2.2x.\n", dev->name,
1653                 RTL_R8 (ChipCmd),
1654                 RTL_R16 (IntrStatus),
1655                 RTL_R8 (MediaStatus));
1656
1657        tp->xstats.tx_timeouts++;
1658
1659        /* disable Tx ASAP, if not already */
1660        tmp8 = RTL_R8 (ChipCmd);
1661        if (tmp8 & CmdTxEnb)
1662                RTL_W8 (ChipCmd, CmdRxEnb);
1663
1664        /* Disable interrupts by clearing the interrupt mask. */
1665        RTL_W16 (IntrMask, 0x0000);
1666
1667        /* Emit info to figure out what went wrong. */
1668        printk (KERN_DEBUG "%s: Tx queue start entry %ld  dirty entry %ld.\n",
1669                dev->name, tp->cur_tx, tp->dirty_tx);
1670        for (i = 0; i < NUM_TX_DESC; i++)
1671                printk (KERN_DEBUG "%s:  Tx descriptor %d is %8.8lx.%s\n",
1672                        dev->name, i, RTL_R32 (TxStatus0 + (i * 4)),
1673                        i == tp->dirty_tx % NUM_TX_DESC ?
1674                                " (queue head)" : "");
1675
1676        /* Stop a shared interrupt from scavenging while we are. */
1677        spin_lock_irqsave (&tp->lock, flags);
1678        rtl8139_tx_clear (tp);
1679        spin_unlock_irqrestore (&tp->lock, flags);
1680
1681        /* ...and finally, reset everything */
1682        rtl8139_hw_start (dev);
1683
1684        netif_wake_queue (dev);
1685}
1686
1687
1688static int rtl8139_start_xmit (struct sk_buff *skb, struct net_device *dev)
1689{
1690        struct rtl8139_private *tp = dev->priv;
1691        void *ioaddr = tp->mmio_addr;
1692        unsigned int entry;
1693        unsigned int len = skb->len;
1694
1695        /* Calculate the next Tx descriptor entry. */
1696        entry = tp->cur_tx % NUM_TX_DESC;
1697
1698        if (likely(len < TX_BUF_SIZE)) {
1699                if (len < ETH_ZLEN)
1700                        memset(tp->tx_buf[entry], 0, ETH_ZLEN);
1701                skb_copy_and_csum_dev(skb, tp->tx_buf[entry]);
1702                dev_kfree_skb(skb);
1703        } else {
1704                dev_kfree_skb(skb);
1705                tp->stats.tx_dropped++;
1706                return 0;
1707        }
1708
1709        /* Note: the chip doesn't have auto-pad! */
1710        spin_lock_irq(&tp->lock);
1711        RTL_W32_F (TxStatus0 + (entry * sizeof (u32)),
1712                   tp->tx_flag | max(len, (unsigned int)ETH_ZLEN));
1713
1714        dev->trans_start = jiffies;
1715
1716        tp->cur_tx++;
1717        wmb();
1718
1719        if ((tp->cur_tx - NUM_TX_DESC) == tp->dirty_tx)
1720                netif_stop_queue (dev);
1721        spin_unlock_irq(&tp->lock);
1722
1723        DPRINTK ("%s: Queued Tx packet size %u to slot %d.\n",
1724                 dev->name, len, entry);
1725
1726        return 0;
1727}
1728
1729
1730static void rtl8139_tx_interrupt (struct net_device *dev,
1731                                  struct rtl8139_private *tp,
1732                                  void *ioaddr)
1733{
1734        unsigned long dirty_tx, tx_left;
1735
1736        assert (dev != NULL);
1737        assert (tp != NULL);
1738        assert (ioaddr != NULL);
1739
1740        dirty_tx = tp->dirty_tx;
1741        tx_left = tp->cur_tx - dirty_tx;
1742        while (tx_left > 0) {
1743                int entry = dirty_tx % NUM_TX_DESC;
1744                int txstatus;
1745
1746                txstatus = RTL_R32 (TxStatus0 + (entry * sizeof (u32)));
1747
1748                if (!(txstatus & (TxStatOK | TxUnderrun | TxAborted)))
1749                        break;  /* It still hasn't been Txed */
1750
1751                /* Note: TxCarrierLost is always asserted at 100mbps. */
1752                if (txstatus & (TxOutOfWindow | TxAborted)) {
1753                        /* There was an major error, log it. */
1754                        DPRINTK ("%s: Transmit error, Tx status %8.8x.\n",
1755                                 dev->name, txstatus);
1756                        tp->stats.tx_errors++;
1757                        if (txstatus & TxAborted) {
1758                                tp->stats.tx_aborted_errors++;
1759                                RTL_W32 (TxConfig, TxClearAbt);
1760                                RTL_W16 (IntrStatus, TxErr);
1761                                wmb();
1762                        }
1763                        if (txstatus & TxCarrierLost)
1764                                tp->stats.tx_carrier_errors++;
1765                        if (txstatus & TxOutOfWindow)
1766                                tp->stats.tx_window_errors++;
1767                } else {
1768                        if (txstatus & TxUnderrun) {
1769                                /* Add 64 to the Tx FIFO threshold. */
1770                                if (tp->tx_flag < 0x00300000)
1771                                        tp->tx_flag += 0x00020000;
1772                                tp->stats.tx_fifo_errors++;
1773                        }
1774                        tp->stats.collisions += (txstatus >> 24) & 15;
1775                        tp->stats.tx_bytes += txstatus & 0x7ff;
1776                        tp->stats.tx_packets++;
1777                }
1778
1779                dirty_tx++;
1780                tx_left--;
1781        }
1782
1783#ifndef RTL8139_NDEBUG
1784        if (tp->cur_tx - dirty_tx > NUM_TX_DESC) {
1785                printk (KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
1786                        dev->name, dirty_tx, tp->cur_tx);
1787                dirty_tx += NUM_TX_DESC;
1788        }
1789#endif /* RTL8139_NDEBUG */
1790
1791        /* only wake the queue if we did work, and the queue is stopped */
1792        if (tp->dirty_tx != dirty_tx) {
1793                tp->dirty_tx = dirty_tx;
1794                mb();
1795                if (netif_queue_stopped (dev))
1796                        netif_wake_queue (dev);
1797        }
1798}
1799
1800
1801/* TODO: clean this up!  Rx reset need not be this intensive */
1802static void rtl8139_rx_err (u32 rx_status, struct net_device *dev,
1803                            struct rtl8139_private *tp, void *ioaddr)
1804{
1805        u8 tmp8;
1806#ifdef CONFIG_8139_OLD_RX_RESET
1807        int tmp_work;
1808#endif
1809
1810        DPRINTK ("%s: Ethernet frame had errors, status %8.8x.\n",
1811                 dev->name, rx_status);
1812        tp->stats.rx_errors++;
1813        if (!(rx_status & RxStatusOK)) {
1814                if (rx_status & RxTooLong) {
1815                        DPRINTK ("%s: Oversized Ethernet frame, status %4.4x!\n",
1816                                dev->name, rx_status);
1817                        /* A.C.: The chip hangs here. */
1818                }
1819                if (rx_status & (RxBadSymbol | RxBadAlign))
1820                        tp->stats.rx_frame_errors++;
1821                if (rx_status & (RxRunt | RxTooLong))
1822                        tp->stats.rx_length_errors++;
1823                if (rx_status & RxCRCErr)
1824                        tp->stats.rx_crc_errors++;
1825        } else {
1826                tp->xstats.rx_lost_in_ring++;
1827        }
1828
1829#ifndef CONFIG_8139_OLD_RX_RESET
1830        tmp8 = RTL_R8 (ChipCmd);
1831        RTL_W8 (ChipCmd, tmp8 & ~CmdRxEnb);
1832        RTL_W8 (ChipCmd, tmp8);
1833        RTL_W32 (RxConfig, tp->rx_config);
1834        tp->cur_rx = 0;
1835#else
1836        /* Reset the receiver, based on RealTek recommendation. (Bug?) */
1837
1838        /* disable receive */
1839        RTL_W8_F (ChipCmd, CmdTxEnb);
1840        tmp_work = 200;
1841        while (--tmp_work > 0) {
1842                udelay(1);
1843                tmp8 = RTL_R8 (ChipCmd);
1844                if (!(tmp8 & CmdRxEnb))
1845                        break;
1846        }
1847        if (tmp_work <= 0)
1848                printk (KERN_WARNING PFX "rx stop wait too long\n");
1849        /* restart receive */
1850        tmp_work = 200;
1851        while (--tmp_work > 0) {
1852                RTL_W8_F (ChipCmd, CmdRxEnb | CmdTxEnb);
1853                udelay(1);
1854                tmp8 = RTL_R8 (ChipCmd);
1855                if ((tmp8 & CmdRxEnb) && (tmp8 & CmdTxEnb))
1856                        break;
1857        }
1858        if (tmp_work <= 0)
1859                printk (KERN_WARNING PFX "tx/rx enable wait too long\n");
1860
1861        /* and reinitialize all rx related registers */
1862        RTL_W8_F (Cfg9346, Cfg9346_Unlock);
1863        /* Must enable Tx/Rx before setting transfer thresholds! */
1864        RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
1865
1866        tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
1867        RTL_W32 (RxConfig, tp->rx_config);
1868        tp->cur_rx = 0;
1869
1870        DPRINTK("init buffer addresses\n");
1871
1872        /* Lock Config[01234] and BMCR register writes */
1873        RTL_W8 (Cfg9346, Cfg9346_Lock);
1874
1875        /* init Rx ring buffer DMA address */
1876        RTL_W32_F (RxBuf, tp->rx_ring_dma);
1877
1878        /* A.C.: Reset the multicast list. */
1879        __set_rx_mode (dev);
1880#endif
1881}
1882
1883static void rtl8139_rx_interrupt (struct net_device *dev,
1884                                  struct rtl8139_private *tp, void *ioaddr)
1885{
1886        unsigned char *rx_ring;
1887        u16 cur_rx;
1888
1889        assert (dev != NULL);
1890        assert (tp != NULL);
1891        assert (ioaddr != NULL);
1892
1893        rx_ring = tp->rx_ring;
1894        cur_rx = tp->cur_rx;
1895
1896        DPRINTK ("%s: In rtl8139_rx(), current %4.4x BufAddr %4.4x,"
1897                 " free to %4.4x, Cmd %2.2x.\n", dev->name, cur_rx,
1898                 RTL_R16 (RxBufAddr),
1899                 RTL_R16 (RxBufPtr), RTL_R8 (ChipCmd));
1900
1901        while ((RTL_R8 (ChipCmd) & RxBufEmpty) == 0) {
1902                int ring_offset = cur_rx % RX_BUF_LEN;
1903                u32 rx_status;
1904                unsigned int rx_size;
1905                unsigned int pkt_size;
1906                struct sk_buff *skb;
1907
1908                rmb();
1909
1910                /* read size+status of next frame from DMA ring buffer */
1911                rx_status = le32_to_cpu (*(u32 *) (rx_ring + ring_offset));
1912                rx_size = rx_status >> 16;
1913                pkt_size = rx_size - 4;
1914
1915                DPRINTK ("%s:  rtl8139_rx() status %4.4x, size %4.4x,"
1916                         " cur %4.4x.\n", dev->name, rx_status,
1917                         rx_size, cur_rx);
1918#if RTL8139_DEBUG > 2
1919                {
1920                        int i;
1921                        DPRINTK ("%s: Frame contents ", dev->name);
1922                        for (i = 0; i < 70; i++)
1923                                printk (" %2.2x",
1924                                        rx_ring[ring_offset + i]);
1925                        printk (".\n");
1926                }
1927#endif
1928
1929                /* Packet copy from FIFO still in progress.
1930                 * Theoretically, this should never happen
1931                 * since EarlyRx is disabled.
1932                 */
1933                if (rx_size == 0xfff0) {
1934                        tp->xstats.early_rx++;
1935                        break;
1936                }
1937
1938                /* If Rx err or invalid rx_size/rx_status received
1939                 * (which happens if we get lost in the ring),
1940                 * Rx process gets reset, so we abort any further
1941                 * Rx processing.
1942                 */
1943                if ((rx_size > (MAX_ETH_FRAME_SIZE+4)) ||
1944                    (rx_size < 8) ||
1945                    (!(rx_status & RxStatusOK))) {
1946                        rtl8139_rx_err (rx_status, dev, tp, ioaddr);
1947                        return;
1948                }
1949
1950                /* Malloc up new buffer, compatible with net-2e. */
1951                /* Omit the four octet CRC from the length. */
1952
1953                /* TODO: consider allocating skb's outside of
1954                 * interrupt context, both to speed interrupt processing,
1955                 * and also to reduce the chances of having to
1956                 * drop packets here under memory pressure.
1957                 */
1958
1959                skb = dev_alloc_skb (pkt_size + 2);
1960                if (skb) {
1961                        skb->dev = dev;
1962                        skb_reserve (skb, 2);   /* 16 byte align the IP fields. */
1963
1964                        eth_copy_and_sum (skb, &rx_ring[ring_offset + 4], pkt_size, 0);
1965                        skb_put (skb, pkt_size);
1966
1967                        skb->protocol = eth_type_trans (skb, dev);
1968                        netif_rx (skb);
1969                        dev->last_rx = jiffies;
1970                        tp->stats.rx_bytes += pkt_size;
1971                        tp->stats.rx_packets++;
1972                } else {
1973                        printk (KERN_WARNING
1974                                "%s: Memory squeeze, dropping packet.\n",
1975                                dev->name);
1976                        tp->stats.rx_dropped++;
1977                }
1978
1979                cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
1980                RTL_W16 (RxBufPtr, cur_rx - 16);
1981
1982                if (RTL_R16 (IntrStatus) & RxAckBits)
1983                        RTL_W16_F (IntrStatus, RxAckBits);
1984        }
1985
1986        DPRINTK ("%s: Done rtl8139_rx(), current %4.4x BufAddr %4.4x,"
1987                 " free to %4.4x, Cmd %2.2x.\n", dev->name, cur_rx,
1988                 RTL_R16 (RxBufAddr),
1989                 RTL_R16 (RxBufPtr), RTL_R8 (ChipCmd));
1990
1991        tp->cur_rx = cur_rx;
1992}
1993
1994
1995static void rtl8139_weird_interrupt (struct net_device *dev,
1996                                     struct rtl8139_private *tp,
1997                                     void *ioaddr,
1998                                     int status, int link_changed)
1999{
2000        DPRINTK ("%s: Abnormal interrupt, status %8.8x.\n",
2001                 dev->name, status);
2002
2003        assert (dev != NULL);
2004        assert (tp != NULL);
2005        assert (ioaddr != NULL);
2006
2007        /* Update the error count. */
2008        tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
2009        RTL_W32 (RxMissed, 0);
2010
2011        if ((status & RxUnderrun) && link_changed &&
2012            (tp->drv_flags & HAS_LNK_CHNG)) {
2013                rtl_check_media(dev, 0);
2014                status &= ~RxUnderrun;
2015        }
2016
2017        /* XXX along with rtl8139_rx_err, are we double-counting errors? */
2018        if (status &
2019            (RxUnderrun | RxOverflow | RxErr | RxFIFOOver))
2020                tp->stats.rx_errors++;
2021
2022        if (status & PCSTimeout)
2023                tp->stats.rx_length_errors++;
2024        if (status & (RxUnderrun | RxFIFOOver))
2025                tp->stats.rx_fifo_errors++;
2026        if (status & PCIErr) {
2027                u16 pci_cmd_status;
2028                pci_read_config_word (tp->pci_dev, PCI_STATUS, &pci_cmd_status);
2029                pci_write_config_word (tp->pci_dev, PCI_STATUS, pci_cmd_status);
2030
2031                printk (KERN_ERR "%s: PCI Bus error %4.4x.\n",
2032                        dev->name, pci_cmd_status);
2033        }
2034}
2035
2036
2037/* The interrupt handler does all of the Rx thread work and cleans up
2038   after the Tx thread. */
2039static irqreturn_t rtl8139_interrupt (int irq, void *dev_instance,
2040                               struct pt_regs *regs)
2041{
2042        struct net_device *dev = (struct net_device *) dev_instance;
2043        struct rtl8139_private *tp = dev->priv;
2044        int boguscnt = max_interrupt_work;
2045        void *ioaddr = tp->mmio_addr;
2046        int ackstat, status;
2047        int link_changed = 0; /* avoid bogus "uninit" warning */
2048        int handled = 0;
2049
2050        spin_lock (&tp->lock);
2051
2052        do {
2053                status = RTL_R16 (IntrStatus);
2054
2055                /* h/w no longer present (hotplug?) or major error, bail */
2056                if (status == 0xFFFF)
2057                        break;
2058
2059                if ((status &
2060                     (PCIErr | PCSTimeout | RxUnderrun | RxOverflow |
2061                      RxFIFOOver | TxErr | TxOK | RxErr | RxOK)) == 0)
2062                        break;
2063
2064                handled = 1;
2065
2066                /* Acknowledge all of the current interrupt sources ASAP, but
2067                   an first get an additional status bit from CSCR. */
2068                if (status & RxUnderrun)
2069                        link_changed = RTL_R16 (CSCR) & CSCR_LinkChangeBit;
2070
2071                /* The chip takes special action when we clear RxAckBits,
2072                 * so we clear them later in rtl8139_rx_interrupt
2073                 */
2074                ackstat = status & ~(RxAckBits | TxErr);
2075                RTL_W16 (IntrStatus, ackstat);
2076
2077                DPRINTK ("%s: interrupt  status=%#4.4x ackstat=%#4.4x new intstat=%#4.4x.\n",
2078                         dev->name, status, ackstat, RTL_R16 (IntrStatus));
2079
2080                if (netif_running (dev) && (status & RxAckBits))
2081                        rtl8139_rx_interrupt (dev, tp, ioaddr);
2082
2083                /* Check uncommon events with one test. */
2084                if (status & (PCIErr | PCSTimeout | RxUnderrun | RxOverflow |
2085                              RxFIFOOver | RxErr))
2086                        rtl8139_weird_interrupt (dev, tp, ioaddr,
2087                                                 status, link_changed);
2088
2089                if (netif_running (dev) && (status & (TxOK | TxErr))) {
2090                        rtl8139_tx_interrupt (dev, tp, ioaddr);
2091                        if (status & TxErr)
2092                                RTL_W16 (IntrStatus, TxErr);
2093                }
2094
2095                boguscnt--;
2096        } while (boguscnt > 0);
2097
2098        if (boguscnt <= 0) {
2099                printk (KERN_WARNING "%s: Too much work at interrupt, "
2100                        "IntrStatus=0x%4.4x.\n", dev->name, status);
2101
2102                /* Clear all interrupt sources. */
2103                RTL_W16 (IntrStatus, 0xffff);
2104        }
2105
2106        spin_unlock (&tp->lock);
2107
2108        DPRINTK ("%s: exiting interrupt, intr_status=%#4.4x.\n",
2109                 dev->name, RTL_R16 (IntrStatus));
2110        return IRQ_RETVAL(handled);
2111}
2112
2113
2114static int rtl8139_close (struct net_device *dev)
2115{
2116        struct rtl8139_private *tp = dev->priv;
2117        void *ioaddr = tp->mmio_addr;
2118        int ret = 0;
2119        unsigned long flags;
2120
2121        netif_stop_queue (dev);
2122
2123        if (tp->thr_pid >= 0) {
2124                tp->time_to_die = 1;
2125                wmb();
2126                ret = kill_proc (tp->thr_pid, SIGTERM, 1);
2127                if (ret) {
2128                        printk (KERN_ERR "%s: unable to signal thread\n", dev->name);
2129                        return ret;
2130                }
2131                wait_for_completion (&tp->thr_exited);
2132        }
2133
2134        DPRINTK ("%s: Shutting down ethercard, status was 0x%4.4x.\n",
2135                        dev->name, RTL_R16 (IntrStatus));
2136
2137        spin_lock_irqsave (&tp->lock, flags);
2138
2139        /* Stop the chip's Tx and Rx DMA processes. */
2140        RTL_W8 (ChipCmd, 0);
2141
2142        /* Disable interrupts by clearing the interrupt mask. */
2143        RTL_W16 (IntrMask, 0);
2144
2145        /* Update the error counts. */
2146        tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
2147        RTL_W32 (RxMissed, 0);
2148
2149        spin_unlock_irqrestore (&tp->lock, flags);
2150
2151        synchronize_irq (dev->irq);     /* racy, but that's ok here */
2152        free_irq (dev->irq, dev);
2153
2154        rtl8139_tx_clear (tp);
2155
2156        pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
2157                            tp->rx_ring, tp->rx_ring_dma);
2158        pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
2159                            tp->tx_bufs, tp->tx_bufs_dma);
2160        tp->rx_ring = NULL;
2161        tp->tx_bufs = NULL;
2162
2163        /* Green! Put the chip in low-power mode. */
2164        RTL_W8 (Cfg9346, Cfg9346_Unlock);
2165
2166        if (rtl_chip_info[tp->chipset].flags & HasHltClk)
2167                RTL_W8 (HltClk, 'H');   /* 'R' would leave the clock running. */
2168
2169        return 0;
2170}
2171
2172
2173/* Get the ethtool Wake-on-LAN settings.  Assumes that wol points to
2174   kernel memory, *wol has been initialized as {ETHTOOL_GWOL}, and
2175   other threads or interrupts aren't messing with the 8139.  */
2176static void rtl8139_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2177{
2178        struct rtl8139_private *np = dev->priv;
2179        void *ioaddr = np->mmio_addr;
2180
2181        spin_lock_irq(&np->lock);
2182        if (rtl_chip_info[np->chipset].flags & HasLWake) {
2183                u8 cfg3 = RTL_R8 (Config3);
2184                u8 cfg5 = RTL_R8 (Config5);
2185
2186                wol->supported = WAKE_PHY | WAKE_MAGIC
2187                        | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;
2188
2189                wol->wolopts = 0;
2190                if (cfg3 & Cfg3_LinkUp)
2191                        wol->wolopts |= WAKE_PHY;
2192                if (cfg3 & Cfg3_Magic)
2193                        wol->wolopts |= WAKE_MAGIC;
2194                /* (KON)FIXME: See how netdev_set_wol() handles the
2195                   following constants.  */
2196                if (cfg5 & Cfg5_UWF)
2197                        wol->wolopts |= WAKE_UCAST;
2198                if (cfg5 & Cfg5_MWF)
2199                        wol->wolopts |= WAKE_MCAST;
2200                if (cfg5 & Cfg5_BWF)
2201                        wol->wolopts |= WAKE_BCAST;
2202        }
2203        spin_unlock_irq(&np->lock);
2204}
2205
2206
2207/* Set the ethtool Wake-on-LAN settings.  Return 0 or -errno.  Assumes
2208   that wol points to kernel memory and other threads or interrupts
2209   aren't messing with the 8139.  */
2210static int rtl8139_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2211{
2212        struct rtl8139_private *np = dev->priv;
2213        void *ioaddr = np->mmio_addr;
2214        u32 support;
2215        u8 cfg3, cfg5;
2216
2217        support = ((rtl_chip_info[np->chipset].flags & HasLWake)
2218                   ? (WAKE_PHY | WAKE_MAGIC
2219                      | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST)
2220                   : 0);
2221        if (wol->wolopts & ~support)
2222                return -EINVAL;
2223
2224        spin_lock_irq(&np->lock);
2225        cfg3 = RTL_R8 (Config3) & ~(Cfg3_LinkUp | Cfg3_Magic);
2226        if (wol->wolopts & WAKE_PHY)
2227                cfg3 |= Cfg3_LinkUp;
2228        if (wol->wolopts & WAKE_MAGIC)
2229                cfg3 |= Cfg3_Magic;
2230        RTL_W8 (Cfg9346, Cfg9346_Unlock);
2231        RTL_W8 (Config3, cfg3);
2232        RTL_W8 (Cfg9346, Cfg9346_Lock);
2233
2234        cfg5 = RTL_R8 (Config5) & ~(Cfg5_UWF | Cfg5_MWF | Cfg5_BWF);
2235        /* (KON)FIXME: These are untested.  We may have to set the
2236           CRC0, Wakeup0 and LSBCRC0 registers too, but I have no
2237           documentation.  */
2238        if (wol->wolopts & WAKE_UCAST)
2239                cfg5 |= Cfg5_UWF;
2240        if (wol->wolopts & WAKE_MCAST)
2241                cfg5 |= Cfg5_MWF;
2242        if (wol->wolopts & WAKE_BCAST)
2243                cfg5 |= Cfg5_BWF;
2244        RTL_W8 (Config5, cfg5); /* need not unlock via Cfg9346 */
2245        spin_unlock_irq(&np->lock);
2246
2247        return 0;
2248}
2249
2250static void rtl8139_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2251{
2252        struct rtl8139_private *np = dev->priv;
2253        strcpy(info->driver, DRV_NAME);
2254        strcpy(info->version, DRV_VERSION);
2255        strcpy(info->bus_info, pci_name(np->pci_dev));
2256        info->regdump_len = np->regs_len;
2257}
2258
2259static int rtl8139_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2260{
2261        struct rtl8139_private *np = dev->priv;
2262        spin_lock_irq(&np->lock);
2263        mii_ethtool_gset(&np->mii, cmd);
2264        spin_unlock_irq(&np->lock);
2265        return 0;
2266}
2267
2268static int rtl8139_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2269{
2270        struct rtl8139_private *np = dev->priv;
2271        int rc;
2272        spin_lock_irq(&np->lock);
2273        rc = mii_ethtool_sset(&np->mii, cmd);
2274        spin_unlock_irq(&np->lock);
2275        return rc;
2276}
2277
2278static int rtl8139_nway_reset(struct net_device *dev)
2279{
2280        struct rtl8139_private *np = dev->priv;
2281        return mii_nway_restart(&np->mii);
2282}
2283
2284static u32 rtl8139_get_link(struct net_device *dev)
2285{
2286        struct rtl8139_private *np = dev->priv;
2287        return mii_link_ok(&np->mii);
2288}
2289
2290static u32 rtl8139_get_msglevel(struct net_device *dev)
2291{
2292        return debug;
2293}
2294
2295static void rtl8139_set_msglevel(struct net_device *dev, u32 datum)
2296{
2297        debug = datum;
2298}
2299
2300/* TODO: we are too slack to do reg dumping for pio, for now */
2301#ifdef CONFIG_8139TOO_PIO
2302#define rtl8139_get_regs_len    NULL
2303#define rtl8139_get_regs        NULL
2304#else
2305static int rtl8139_get_regs_len(struct net_device *dev)
2306{
2307        struct rtl8139_private *np = dev->priv;
2308        return np->regs_len;
2309}
2310
2311static void rtl8139_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
2312{
2313        struct rtl8139_private *np = dev->priv;
2314
2315        regs->version = RTL_REGS_VER;
2316
2317        spin_lock_irq(&np->lock);
2318        memcpy_fromio(regbuf, np->mmio_addr, regs->len);
2319        spin_unlock_irq(&np->lock);
2320}
2321#endif /* CONFIG_8139TOO_MMIO */
2322
2323static int rtl8139_get_stats_count(struct net_device *dev)
2324{
2325        return RTL_NUM_STATS;
2326}
2327
2328static void rtl8139_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2329{
2330        struct rtl8139_private *np = dev->priv;
2331
2332        data[0] = np->xstats.early_rx;
2333        data[1] = np->xstats.tx_buf_mapped;
2334        data[2] = np->xstats.tx_timeouts;
2335        data[3] = np->xstats.rx_lost_in_ring;
2336}
2337
2338static void rtl8139_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2339{
2340        memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2341}
2342
2343static struct ethtool_ops rtl8139_ethtool_ops = {
2344        .get_drvinfo            = rtl8139_get_drvinfo,
2345        .get_settings           = rtl8139_get_settings,
2346        .set_settings           = rtl8139_set_settings,
2347        .get_regs_len           = rtl8139_get_regs_len,
2348        .get_regs               = rtl8139_get_regs,
2349        .nway_reset             = rtl8139_nway_reset,
2350        .get_link               = rtl8139_get_link,
2351        .get_msglevel           = rtl8139_get_msglevel,
2352        .set_msglevel           = rtl8139_set_msglevel,
2353        .get_wol                = rtl8139_get_wol,
2354        .set_wol                = rtl8139_set_wol,
2355        .get_strings            = rtl8139_get_strings,
2356        .get_stats_count        = rtl8139_get_stats_count,
2357        .get_ethtool_stats      = rtl8139_get_ethtool_stats,
2358};
2359
2360static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2361{
2362        struct rtl8139_private *np = dev->priv;
2363        struct mii_ioctl_data *data = (struct mii_ioctl_data *) & rq->ifr_data;
2364        int rc;
2365
2366        if (!netif_running(dev))
2367                return -EINVAL;
2368
2369        spin_lock_irq(&np->lock);
2370        rc = generic_mii_ioctl(&np->mii, data, cmd, NULL);
2371        spin_unlock_irq(&np->lock);
2372
2373        return rc;
2374}
2375
2376
2377static struct net_device_stats *rtl8139_get_stats (struct net_device *dev)
2378{
2379        struct rtl8139_private *tp = dev->priv;
2380        void *ioaddr = tp->mmio_addr;
2381        unsigned long flags;
2382
2383        if (netif_running(dev)) {
2384                spin_lock_irqsave (&tp->lock, flags);
2385                tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
2386                RTL_W32 (RxMissed, 0);
2387                spin_unlock_irqrestore (&tp->lock, flags);
2388        }
2389
2390        return &tp->stats;
2391}
2392
2393/* Set or clear the multicast filter for this adaptor.
2394   This routine is not state sensitive and need not be SMP locked. */
2395
2396static void __set_rx_mode (struct net_device *dev)
2397{
2398        struct rtl8139_private *tp = dev->priv;
2399        void *ioaddr = tp->mmio_addr;
2400        u32 mc_filter[2];       /* Multicast hash filter */
2401        int i, rx_mode;
2402        u32 tmp;
2403
2404        DPRINTK ("%s:   rtl8139_set_rx_mode(%4.4x) done -- Rx config %8.8lx.\n",
2405                        dev->name, dev->flags, RTL_R32 (RxConfig));
2406
2407        /* Note: do not reorder, GCC is clever about common statements. */
2408        if (dev->flags & IFF_PROMISC) {
2409                /* Unconditionally log net taps. */
2410                printk (KERN_NOTICE "%s: Promiscuous mode enabled.\n",
2411                        dev->name);
2412                rx_mode =
2413                    AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
2414                    AcceptAllPhys;
2415                mc_filter[1] = mc_filter[0] = 0xffffffff;
2416        } else if ((dev->mc_count > multicast_filter_limit)
2417                   || (dev->flags & IFF_ALLMULTI)) {
2418                /* Too many to filter perfectly -- accept all multicasts. */
2419                rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
2420                mc_filter[1] = mc_filter[0] = 0xffffffff;
2421        } else {
2422                struct dev_mc_list *mclist;
2423                rx_mode = AcceptBroadcast | AcceptMyPhys;
2424                mc_filter[1] = mc_filter[0] = 0;
2425                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2426                     i++, mclist = mclist->next) {
2427                        int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
2428
2429                        mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2430                        rx_mode |= AcceptMulticast;
2431                }
2432        }
2433
2434        /* We can safely update without stopping the chip. */
2435        tmp = rtl8139_rx_config | rx_mode;
2436        if (tp->rx_config != tmp) {
2437                RTL_W32_F (RxConfig, tmp);
2438                tp->rx_config = tmp;
2439        }
2440        RTL_W32_F (MAR0 + 0, mc_filter[0]);
2441        RTL_W32_F (MAR0 + 4, mc_filter[1]);
2442}
2443
2444static void rtl8139_set_rx_mode (struct net_device *dev)
2445{
2446        unsigned long flags;
2447        struct rtl8139_private *tp = dev->priv;
2448
2449        spin_lock_irqsave (&tp->lock, flags);
2450        __set_rx_mode(dev);
2451        spin_unlock_irqrestore (&tp->lock, flags);
2452}
2453
2454#ifdef CONFIG_PM
2455
2456static int rtl8139_suspend (struct pci_dev *pdev, u32 state)
2457{
2458        struct net_device *dev = pci_get_drvdata (pdev);
2459        struct rtl8139_private *tp = dev->priv;
2460        void *ioaddr = tp->mmio_addr;
2461        unsigned long flags;
2462
2463        if (!netif_running (dev))
2464                return 0;
2465
2466        netif_device_detach (dev);
2467
2468        spin_lock_irqsave (&tp->lock, flags);
2469
2470        /* Disable interrupts, stop Tx and Rx. */
2471        RTL_W16 (IntrMask, 0);
2472        RTL_W8 (ChipCmd, 0);
2473
2474        /* Update the error counts. */
2475        tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
2476        RTL_W32 (RxMissed, 0);
2477
2478        pci_set_power_state (pdev, 3);
2479        pci_save_state (pdev, tp->pci_state);
2480
2481        spin_unlock_irqrestore (&tp->lock, flags);
2482        return 0;
2483}
2484
2485
2486static int rtl8139_resume (struct pci_dev *pdev)
2487{
2488        struct net_device *dev = pci_get_drvdata (pdev);
2489        struct rtl8139_private *tp = dev->priv;
2490
2491        if (!netif_running (dev))
2492                return 0;
2493        pci_restore_state (pdev, tp->pci_state);
2494        pci_set_power_state (pdev, 0);
2495        rtl8139_init_ring (dev);
2496        rtl8139_hw_start (dev);
2497        netif_device_attach (dev);
2498        return 0;
2499}
2500
2501#endif /* CONFIG_PM */
2502
2503
2504static struct pci_driver rtl8139_pci_driver = {
2505        .name           = DRV_NAME,
2506        .id_table       = rtl8139_pci_tbl,
2507        .probe          = rtl8139_init_one,
2508        .remove         = __devexit_p(rtl8139_remove_one),
2509#ifdef CONFIG_PM
2510        .suspend        = rtl8139_suspend,
2511        .resume         = rtl8139_resume,
2512#endif /* CONFIG_PM */
2513};
2514
2515
2516static int __init rtl8139_init_module (void)
2517{
2518        /* when we're a module, we always print a version message,
2519         * even if no 8139 board is found.
2520         */
2521#ifdef MODULE
2522        printk (KERN_INFO RTL8139_DRIVER_NAME "\n");
2523#endif
2524
2525        return pci_module_init (&rtl8139_pci_driver);
2526}
2527
2528
2529static void __exit rtl8139_cleanup_module (void)
2530{
2531        pci_unregister_driver (&rtl8139_pci_driver);
2532}
2533
2534
2535module_init(rtl8139_init_module);
2536module_exit(rtl8139_cleanup_module);
2537
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.