linux-bk/drivers/net/epic100.c
<<
>>
Prefs
   1/* epic100.c: A SMC 83c170 EPIC/100 Fast Ethernet driver for Linux. */
   2/*
   3        Written/copyright 1997-2001 by Donald Becker.
   4
   5        This software may be used and distributed according to the terms of
   6        the GNU General Public License (GPL), incorporated herein by reference.
   7        Drivers based on or derived from this code fall under the GPL and must
   8        retain the authorship, copyright and license notice.  This file is not
   9        a complete program and may only be used when the entire operating
  10        system is licensed under the GPL.
  11
  12        This driver is for the SMC83c170/175 "EPIC" series, as used on the
  13        SMC EtherPower II 9432 PCI adapter, and several CardBus cards.
  14
  15        The author may be reached as becker@scyld.com, or C/O
  16        Scyld Computing Corporation
  17        410 Severn Ave., Suite 210
  18        Annapolis MD 21403
  19
  20        Information and updates available at
  21        http://www.scyld.com/network/epic100.html
  22
  23        ---------------------------------------------------------------------
  24        
  25        Linux kernel-specific changes:
  26        
  27        LK1.1.2 (jgarzik):
  28        * Merge becker version 1.09 (4/08/2000)
  29
  30        LK1.1.3:
  31        * Major bugfix to 1.09 driver (Francis Romieu)
  32        
  33        LK1.1.4 (jgarzik):
  34        * Merge becker test version 1.09 (5/29/2000)
  35
  36        LK1.1.5:
  37        * Fix locking (jgarzik)
  38        * Limit 83c175 probe to ethernet-class PCI devices (rgooch)
  39
  40        LK1.1.6:
  41        * Merge becker version 1.11
  42        * Move pci_enable_device before any PCI BAR len checks
  43
  44        LK1.1.7:
  45        * { fill me in }
  46
  47        LK1.1.8:
  48        * ethtool driver info support (jgarzik)
  49
  50        LK1.1.9:
  51        * ethtool media get/set support (jgarzik)
  52
  53        LK1.1.10:
  54        * revert MII transceiver init change (jgarzik)
  55
  56        LK1.1.11:
  57        * implement ETHTOOL_[GS]SET, _NWAY_RST, _[GS]MSGLVL, _GLINK (jgarzik)
  58        * replace some MII-related magic numbers with constants
  59
  60        LK1.1.12:
  61        * fix power-up sequence
  62
  63        LK1.1.13:
  64        * revert version 1.1.12, power-up sequence "fix"
  65
  66        LK1.1.14 (Kryzsztof Halasa):
  67        * fix spurious bad initializations
  68        * pound phy a la SMSC's app note on the subject
  69
  70*/
  71
  72#define DRV_NAME        "epic100"
  73#define DRV_VERSION     "1.11+LK1.1.14"
  74#define DRV_RELDATE     "Aug 4, 2002"
  75
  76/* The user-configurable values.
  77   These may be modified when a driver module is loaded.*/
  78
  79static int debug = 1;                   /* 1 normal messages, 0 quiet .. 7 verbose. */
  80/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  81static int max_interrupt_work = 32;
  82
  83/* Used to pass the full-duplex flag, etc. */
  84#define MAX_UNITS 8             /* More are supported, limit only on options */
  85static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  86static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  87
  88/* Set the copy breakpoint for the copy-only-tiny-frames scheme.
  89   Setting to > 1518 effectively disables this feature. */
  90static int rx_copybreak;
  91
  92/* Operational parameters that are set at compile time. */
  93
  94/* Keep the ring sizes a power of two for operational efficiency.
  95   The compiler will convert <unsigned>'%'<2^N> into a bit mask.
  96   Making the Tx ring too large decreases the effectiveness of channel
  97   bonding and packet priority.
  98   There are no ill effects from too-large receive rings. */
  99#define TX_RING_SIZE    16
 100#define TX_QUEUE_LEN    10              /* Limit ring entries actually used.  */
 101#define RX_RING_SIZE    32
 102#define TX_TOTAL_SIZE   TX_RING_SIZE*sizeof(struct epic_tx_desc)
 103#define RX_TOTAL_SIZE   RX_RING_SIZE*sizeof(struct epic_rx_desc)
 104
 105/* Operational parameters that usually are not changed. */
 106/* Time in jiffies before concluding the transmitter is hung. */
 107#define TX_TIMEOUT  (2*HZ)
 108
 109#define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
 110
 111/* Bytes transferred to chip before transmission starts. */
 112/* Initial threshold, increased on underflow, rounded down to 4 byte units. */
 113#define TX_FIFO_THRESH 256
 114#define RX_FIFO_THRESH 1                /* 0-3, 0==32, 64,96, or 3==128 bytes  */
 115
 116#if !defined(__OPTIMIZE__)
 117#warning  You must compile this file with the correct options!
 118#warning  See the last lines of the source file.
 119#error You must compile this driver with "-O".
 120#endif
 121
 122#include <linux/config.h>
 123#include <linux/module.h>
 124#include <linux/kernel.h>
 125#include <linux/string.h>
 126#include <linux/timer.h>
 127#include <linux/errno.h>
 128#include <linux/ioport.h>
 129#include <linux/slab.h>
 130#include <linux/interrupt.h>
 131#include <linux/pci.h>
 132#include <linux/delay.h>
 133#include <linux/netdevice.h>
 134#include <linux/etherdevice.h>
 135#include <linux/skbuff.h>
 136#include <linux/init.h>
 137#include <linux/spinlock.h>
 138#include <linux/ethtool.h>
 139#include <linux/mii.h>
 140#include <linux/crc32.h>
 141#include <asm/bitops.h>
 142#include <asm/io.h>
 143#include <asm/uaccess.h>
 144
 145/* These identify the driver base version and may not be removed. */
 146static char version[] __devinitdata =
 147DRV_NAME ".c:v1.11 1/7/2001 Written by Donald Becker <becker@scyld.com>\n";
 148static char version2[] __devinitdata =
 149"  http://www.scyld.com/network/epic100.html\n";
 150static char version3[] __devinitdata =
 151"  (unofficial 2.4.x kernel port, version " DRV_VERSION ", " DRV_RELDATE ")\n";
 152
 153MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
 154MODULE_DESCRIPTION("SMC 83c170 EPIC series Ethernet driver");
 155MODULE_LICENSE("GPL");
 156
 157MODULE_PARM(debug, "i");
 158MODULE_PARM(max_interrupt_work, "i");
 159MODULE_PARM(rx_copybreak, "i");
 160MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
 161MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
 162MODULE_PARM_DESC(debug, "EPIC/100 debug level (0-5)");
 163MODULE_PARM_DESC(max_interrupt_work, "EPIC/100 maximum events handled per interrupt");
 164MODULE_PARM_DESC(options, "EPIC/100: Bits 0-3: media type, bit 4: full duplex");
 165MODULE_PARM_DESC(rx_copybreak, "EPIC/100 copy breakpoint for copy-only-tiny-frames");
 166MODULE_PARM_DESC(full_duplex, "EPIC/100 full duplex setting(s) (1)");
 167
 168/*
 169                                Theory of Operation
 170
 171I. Board Compatibility
 172
 173This device driver is designed for the SMC "EPIC/100", the SMC
 174single-chip Ethernet controllers for PCI.  This chip is used on
 175the SMC EtherPower II boards.
 176
 177II. Board-specific settings
 178
 179PCI bus devices are configured by the system at boot time, so no jumpers
 180need to be set on the board.  The system BIOS will assign the
 181PCI INTA signal to a (preferably otherwise unused) system IRQ line.
 182Note: Kernel versions earlier than 1.3.73 do not support shared PCI
 183interrupt lines.
 184
 185III. Driver operation
 186
 187IIIa. Ring buffers
 188
 189IVb. References
 190
 191http://www.smsc.com/main/datasheets/83c171.pdf
 192http://www.smsc.com/main/datasheets/83c175.pdf
 193http://scyld.com/expert/NWay.html
 194http://www.national.com/pf/DP/DP83840A.html
 195
 196IVc. Errata
 197
 198*/
 199
 200
 201enum pci_id_flags_bits {
 202        /* Set PCI command register bits before calling probe1(). */
 203        PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
 204        /* Read and map the single following PCI BAR. */
 205        PCI_ADDR0=0<<4, PCI_ADDR1=1<<4, PCI_ADDR2=2<<4, PCI_ADDR3=3<<4,
 206        PCI_ADDR_64BITS=0x100, PCI_NO_ACPI_WAKE=0x200, PCI_NO_MIN_LATENCY=0x400,
 207};
 208
 209enum chip_capability_flags { MII_PWRDWN=1, TYPE2_INTR=2, NO_MII=4 };
 210
 211#define EPIC_TOTAL_SIZE 0x100
 212#define USE_IO_OPS 1
 213#ifdef USE_IO_OPS
 214#define EPIC_IOTYPE PCI_USES_MASTER|PCI_USES_IO|PCI_ADDR0
 215#else
 216#define EPIC_IOTYPE PCI_USES_MASTER|PCI_USES_MEM|PCI_ADDR1
 217#endif
 218
 219typedef enum {
 220        SMSC_83C170_0,
 221        SMSC_83C170,
 222        SMSC_83C175,
 223} chip_t;
 224
 225
 226struct epic_chip_info {
 227        const char *name;
 228        enum pci_id_flags_bits pci_flags;
 229        int io_size;                            /* Needed for I/O region check or ioremap(). */
 230        int drv_flags;                          /* Driver use, intended as capability flags. */
 231};
 232
 233
 234/* indexed by chip_t */
 235static struct epic_chip_info pci_id_tbl[] = {
 236        { "SMSC EPIC/100 83c170",
 237         EPIC_IOTYPE, EPIC_TOTAL_SIZE, TYPE2_INTR | NO_MII | MII_PWRDWN },
 238        { "SMSC EPIC/100 83c170",
 239         EPIC_IOTYPE, EPIC_TOTAL_SIZE, TYPE2_INTR },
 240        { "SMSC EPIC/C 83c175",
 241         EPIC_IOTYPE, EPIC_TOTAL_SIZE, TYPE2_INTR | MII_PWRDWN },
 242};
 243
 244
 245static struct pci_device_id epic_pci_tbl[] = {
 246        { 0x10B8, 0x0005, 0x1092, 0x0AB4, 0, 0, SMSC_83C170_0 },
 247        { 0x10B8, 0x0005, PCI_ANY_ID, PCI_ANY_ID, 0, 0, SMSC_83C170 },
 248        { 0x10B8, 0x0006, PCI_ANY_ID, PCI_ANY_ID,
 249          PCI_CLASS_NETWORK_ETHERNET << 8, 0xffff00, SMSC_83C175 },
 250        { 0,}
 251};
 252MODULE_DEVICE_TABLE (pci, epic_pci_tbl);
 253
 254        
 255#ifndef USE_IO_OPS
 256#undef inb
 257#undef inw
 258#undef inl
 259#undef outb
 260#undef outw
 261#undef outl
 262#define inb readb
 263#define inw readw
 264#define inl readl
 265#define outb writeb
 266#define outw writew
 267#define outl writel
 268#endif
 269
 270/* Offsets to registers, using the (ugh) SMC names. */
 271enum epic_registers {
 272  COMMAND=0, INTSTAT=4, INTMASK=8, GENCTL=0x0C, NVCTL=0x10, EECTL=0x14,
 273  PCIBurstCnt=0x18,
 274  TEST1=0x1C, CRCCNT=0x20, ALICNT=0x24, MPCNT=0x28,     /* Rx error counters. */
 275  MIICtrl=0x30, MIIData=0x34, MIICfg=0x38,
 276  LAN0=64,                                              /* MAC address. */
 277  MC0=80,                                               /* Multicast filter table. */
 278  RxCtrl=96, TxCtrl=112, TxSTAT=0x74,
 279  PRxCDAR=0x84, RxSTAT=0xA4, EarlyRx=0xB0, PTxCDAR=0xC4, TxThresh=0xDC,
 280};
 281
 282/* Interrupt register bits, using my own meaningful names. */
 283enum IntrStatus {
 284        TxIdle=0x40000, RxIdle=0x20000, IntrSummary=0x010000,
 285        PCIBusErr170=0x7000, PCIBusErr175=0x1000, PhyEvent175=0x8000,
 286        RxStarted=0x0800, RxEarlyWarn=0x0400, CntFull=0x0200, TxUnderrun=0x0100,
 287        TxEmpty=0x0080, TxDone=0x0020, RxError=0x0010,
 288        RxOverflow=0x0008, RxFull=0x0004, RxHeader=0x0002, RxDone=0x0001,
 289};
 290enum CommandBits {
 291        StopRx=1, StartRx=2, TxQueued=4, RxQueued=8,
 292        StopTxDMA=0x20, StopRxDMA=0x40, RestartTx=0x80,
 293};
 294
 295static u16 media2miictl[16] = {
 296        0, 0x0C00, 0x0C00, 0x2000,  0x0100, 0x2100, 0, 0,
 297        0, 0, 0, 0,  0, 0, 0, 0 };
 298
 299/* The EPIC100 Rx and Tx buffer descriptors. */
 300
 301struct epic_tx_desc {
 302        u32 txstatus;
 303        u32 bufaddr;
 304        u32 buflength;
 305        u32 next;
 306};
 307
 308struct epic_rx_desc {
 309        u32 rxstatus;
 310        u32 bufaddr;
 311        u32 buflength;
 312        u32 next;
 313};
 314
 315enum desc_status_bits {
 316        DescOwn=0x8000,
 317};
 318
 319#define PRIV_ALIGN      15      /* Required alignment mask */
 320struct epic_private {
 321        struct epic_rx_desc *rx_ring;
 322        struct epic_tx_desc *tx_ring;
 323        /* The saved address of a sent-in-place packet/buffer, for skfree(). */
 324        struct sk_buff* tx_skbuff[TX_RING_SIZE];
 325        /* The addresses of receive-in-place skbuffs. */
 326        struct sk_buff* rx_skbuff[RX_RING_SIZE];
 327
 328        dma_addr_t tx_ring_dma;
 329        dma_addr_t rx_ring_dma;
 330
 331        /* Ring pointers. */
 332        spinlock_t lock;                                /* Group with Tx control cache line. */
 333        unsigned int cur_tx, dirty_tx;
 334
 335        unsigned int cur_rx, dirty_rx;
 336        unsigned int rx_buf_sz;                         /* Based on MTU+slack. */
 337
 338        struct pci_dev *pci_dev;                        /* PCI bus location. */
 339        int chip_id, chip_flags;
 340
 341        struct net_device_stats stats;
 342        struct timer_list timer;                        /* Media selection timer. */
 343        int tx_threshold;
 344        unsigned char mc_filter[8];
 345        signed char phys[4];                            /* MII device addresses. */
 346        u16 advertising;                                        /* NWay media advertisement */
 347        int mii_phy_cnt;
 348        struct mii_if_info mii;
 349        unsigned int tx_full:1;                         /* The Tx queue is full. */
 350        unsigned int default_port:4;            /* Last dev->if_port value. */
 351};
 352
 353static int epic_open(struct net_device *dev);
 354static int read_eeprom(long ioaddr, int location);
 355static int mdio_read(struct net_device *dev, int phy_id, int location);
 356static void mdio_write(struct net_device *dev, int phy_id, int loc, int val);
 357static void epic_restart(struct net_device *dev);
 358static void epic_timer(unsigned long data);
 359static void epic_tx_timeout(struct net_device *dev);
 360static void epic_init_ring(struct net_device *dev);
 361static int epic_start_xmit(struct sk_buff *skb, struct net_device *dev);
 362static int epic_rx(struct net_device *dev);
 363static irqreturn_t epic_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
 364static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 365static struct ethtool_ops netdev_ethtool_ops;
 366static int epic_close(struct net_device *dev);
 367static struct net_device_stats *epic_get_stats(struct net_device *dev);
 368static void set_rx_mode(struct net_device *dev);
 369
 370
 371
 372static int __devinit epic_init_one (struct pci_dev *pdev,
 373                                    const struct pci_device_id *ent)
 374{
 375        static int card_idx = -1;
 376        long ioaddr;
 377        int chip_idx = (int) ent->driver_data;
 378        int irq;
 379        struct net_device *dev;
 380        struct epic_private *ep;
 381        int i, option = 0, duplex = 0;
 382        void *ring_space;
 383        dma_addr_t ring_dma;
 384
 385/* when built into the kernel, we only print version if device is found */
 386#ifndef MODULE
 387        static int printed_version;
 388        if (!printed_version++)
 389                printk (KERN_INFO "%s" KERN_INFO "%s" KERN_INFO "%s",
 390                        version, version2, version3);
 391#endif
 392        
 393        card_idx++;
 394        
 395        i = pci_enable_device(pdev);
 396        if (i)
 397                return i;
 398        irq = pdev->irq;
 399
 400        if (pci_resource_len(pdev, 0) < pci_id_tbl[chip_idx].io_size) {
 401                printk (KERN_ERR "card %d: no PCI region space\n", card_idx);
 402                return -ENODEV;
 403        }
 404        
 405        pci_set_master(pdev);
 406
 407        dev = alloc_etherdev(sizeof (*ep));
 408        if (!dev) {
 409                printk (KERN_ERR "card %d: no memory for eth device\n", card_idx);
 410                return -ENOMEM;
 411        }
 412        SET_MODULE_OWNER(dev);
 413        SET_NETDEV_DEV(dev, &pdev->dev);
 414
 415        if (pci_request_regions(pdev, DRV_NAME))
 416                goto err_out_free_netdev;
 417
 418#ifdef USE_IO_OPS
 419        ioaddr = pci_resource_start (pdev, 0);
 420#else
 421        ioaddr = pci_resource_start (pdev, 1);
 422        ioaddr = (long) ioremap (ioaddr, pci_resource_len (pdev, 1));
 423        if (!ioaddr) {
 424                printk (KERN_ERR DRV_NAME " %d: ioremap failed\n", card_idx);
 425                goto err_out_free_res;
 426        }
 427#endif
 428
 429        pci_set_drvdata(pdev, dev);
 430        ep = dev->priv;
 431        ep->mii.dev = dev;
 432        ep->mii.mdio_read = mdio_read;
 433        ep->mii.mdio_write = mdio_write;
 434        ep->mii.phy_id_mask = 0x1f;
 435        ep->mii.reg_num_mask = 0x1f;
 436
 437        ring_space = pci_alloc_consistent(pdev, TX_TOTAL_SIZE, &ring_dma);
 438        if (!ring_space)
 439                goto err_out_iounmap;
 440        ep->tx_ring = (struct epic_tx_desc *)ring_space;
 441        ep->tx_ring_dma = ring_dma;
 442
 443        ring_space = pci_alloc_consistent(pdev, RX_TOTAL_SIZE, &ring_dma);
 444        if (!ring_space)
 445                goto err_out_unmap_tx;
 446        ep->rx_ring = (struct epic_rx_desc *)ring_space;
 447        ep->rx_ring_dma = ring_dma;
 448
 449        if (dev->mem_start) {
 450                option = dev->mem_start;
 451                duplex = (dev->mem_start & 16) ? 1 : 0;
 452        } else if (card_idx >= 0  &&  card_idx < MAX_UNITS) {
 453                if (options[card_idx] >= 0)
 454                        option = options[card_idx];
 455                if (full_duplex[card_idx] >= 0)
 456                        duplex = full_duplex[card_idx];
 457        }
 458
 459        dev->base_addr = ioaddr;
 460        dev->irq = irq;
 461
 462        spin_lock_init (&ep->lock);
 463
 464        /* Bring the chip out of low-power mode. */
 465        outl(0x4200, ioaddr + GENCTL);
 466        /* Magic?!  If we don't set this bit the MII interface won't work. */
 467        /* This magic is documented in SMSC app note 7.15 */
 468        for (i = 16; i > 0; i--)
 469                outl(0x0008, ioaddr + TEST1);
 470
 471        /* Turn on the MII transceiver. */
 472        outl(0x12, ioaddr + MIICfg);
 473        if (chip_idx == 1)
 474                outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
 475        outl(0x0200, ioaddr + GENCTL);
 476
 477        /* Note: the '175 does not have a serial EEPROM. */
 478        for (i = 0; i < 3; i++)
 479                ((u16 *)dev->dev_addr)[i] = le16_to_cpu(inw(ioaddr + LAN0 + i*4));
 480
 481        if (debug > 2) {
 482                printk(KERN_DEBUG DRV_NAME "(%s): EEPROM contents\n",
 483                       pci_name(pdev));
 484                for (i = 0; i < 64; i++)
 485                        printk(" %4.4x%s", read_eeprom(ioaddr, i),
 486                                   i % 16 == 15 ? "\n" : "");
 487        }
 488
 489        ep->pci_dev = pdev;
 490        ep->chip_id = chip_idx;
 491        ep->chip_flags = pci_id_tbl[chip_idx].drv_flags;
 492
 493        /* Find the connected MII xcvrs.
 494           Doing this in open() would allow detecting external xcvrs later, but
 495           takes much time and no cards have external MII. */
 496        {
 497                int phy, phy_idx = 0;
 498                for (phy = 1; phy < 32 && phy_idx < sizeof(ep->phys); phy++) {
 499                        int mii_status = mdio_read(dev, phy, MII_BMSR);
 500                        if (mii_status != 0xffff  &&  mii_status != 0x0000) {
 501                                ep->phys[phy_idx++] = phy;
 502                                printk(KERN_INFO DRV_NAME "(%s): MII transceiver #%d control "
 503                                           "%4.4x status %4.4x.\n",
 504                                           pci_name(pdev), phy, mdio_read(dev, phy, 0), mii_status);
 505                        }
 506                }
 507                ep->mii_phy_cnt = phy_idx;
 508                if (phy_idx != 0) {
 509                        phy = ep->phys[0];
 510                        ep->mii.advertising = mdio_read(dev, phy, MII_ADVERTISE);
 511                        printk(KERN_INFO DRV_NAME "(%s): Autonegotiation advertising %4.4x link "
 512                                   "partner %4.4x.\n",
 513                                   pci_name(pdev), ep->mii.advertising, mdio_read(dev, phy, 5));
 514                } else if ( ! (ep->chip_flags & NO_MII)) {
 515                        printk(KERN_WARNING DRV_NAME "(%s): ***WARNING***: No MII transceiver found!\n",
 516                               pci_name(pdev));
 517                        /* Use the known PHY address of the EPII. */
 518                        ep->phys[0] = 3;
 519                }
 520                ep->mii.phy_id = ep->phys[0];
 521        }
 522
 523        /* Turn off the MII xcvr (175 only!), leave the chip in low-power mode. */
 524        if (ep->chip_flags & MII_PWRDWN)
 525                outl(inl(ioaddr + NVCTL) & ~0x483C, ioaddr + NVCTL);
 526        outl(0x0008, ioaddr + GENCTL);
 527
 528        /* The lower four bits are the media type. */
 529        if (duplex) {
 530                ep->mii.force_media = ep->mii.full_duplex = 1;
 531                printk(KERN_INFO DRV_NAME "(%s):  Forced full duplex operation requested.\n",
 532                       pci_name(pdev));
 533        }
 534        dev->if_port = ep->default_port = option;
 535
 536        /* The Epic-specific entries in the device structure. */
 537        dev->open = &epic_open;
 538        dev->hard_start_xmit = &epic_start_xmit;
 539        dev->stop = &epic_close;
 540        dev->get_stats = &epic_get_stats;
 541        dev->set_multicast_list = &set_rx_mode;
 542        dev->do_ioctl = &netdev_ioctl;
 543        dev->ethtool_ops = &netdev_ethtool_ops;
 544        dev->watchdog_timeo = TX_TIMEOUT;
 545        dev->tx_timeout = &epic_tx_timeout;
 546
 547        i = register_netdev(dev);
 548        if (i)
 549                goto err_out_unmap_tx;
 550
 551        printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ",
 552                   dev->name, pci_id_tbl[chip_idx].name, ioaddr, dev->irq);
 553        for (i = 0; i < 5; i++)
 554                printk("%2.2x:", dev->dev_addr[i]);
 555        printk("%2.2x.\n", dev->dev_addr[i]);
 556
 557        return 0;
 558
 559err_out_unmap_tx:
 560        pci_free_consistent(pdev, TX_TOTAL_SIZE, ep->tx_ring, ep->tx_ring_dma);
 561err_out_iounmap:
 562#ifndef USE_IO_OPS
 563        iounmap(ioaddr);
 564err_out_free_res:
 565#endif
 566        pci_release_regions(pdev);
 567err_out_free_netdev:
 568        free_netdev(dev);
 569        return -ENODEV;
 570}
 571
 572/* Serial EEPROM section. */
 573
 574/*  EEPROM_Ctrl bits. */
 575#define EE_SHIFT_CLK    0x04    /* EEPROM shift clock. */
 576#define EE_CS                   0x02    /* EEPROM chip select. */
 577#define EE_DATA_WRITE   0x08    /* EEPROM chip data in. */
 578#define EE_WRITE_0              0x01
 579#define EE_WRITE_1              0x09
 580#define EE_DATA_READ    0x10    /* EEPROM chip data out. */
 581#define EE_ENB                  (0x0001 | EE_CS)
 582
 583/* Delay between EEPROM clock transitions.
 584   This serves to flush the operation to the PCI bus.
 585 */
 586
 587#define eeprom_delay()  inl(ee_addr)
 588
 589/* The EEPROM commands include the alway-set leading bit. */
 590#define EE_WRITE_CMD    (5 << 6)
 591#define EE_READ64_CMD   (6 << 6)
 592#define EE_READ256_CMD  (6 << 8)
 593#define EE_ERASE_CMD    (7 << 6)
 594
 595static int __devinit read_eeprom(long ioaddr, int location)
 596{
 597        int i;
 598        int retval = 0;
 599        long ee_addr = ioaddr + EECTL;
 600        int read_cmd = location |
 601                (inl(ee_addr) & 0x40 ? EE_READ64_CMD : EE_READ256_CMD);
 602
 603        outl(EE_ENB & ~EE_CS, ee_addr);
 604        outl(EE_ENB, ee_addr);
 605
 606        /* Shift the read command bits out. */
 607        for (i = 12; i >= 0; i--) {
 608                short dataval = (read_cmd & (1 << i)) ? EE_WRITE_1 : EE_WRITE_0;
 609                outl(EE_ENB | dataval, ee_addr);
 610                eeprom_delay();
 611                outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
 612                eeprom_delay();
 613        }
 614        outl(EE_ENB, ee_addr);
 615
 616        for (i = 16; i > 0; i--) {
 617                outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
 618                eeprom_delay();
 619                retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
 620                outl(EE_ENB, ee_addr);
 621                eeprom_delay();
 622        }
 623
 624        /* Terminate the EEPROM access. */
 625        outl(EE_ENB & ~EE_CS, ee_addr);
 626        return retval;
 627}
 628
 629#define MII_READOP              1
 630#define MII_WRITEOP             2
 631static int mdio_read(struct net_device *dev, int phy_id, int location)
 632{
 633        long ioaddr = dev->base_addr;
 634        int read_cmd = (phy_id << 9) | (location << 4) | MII_READOP;
 635        int i;
 636
 637        outl(read_cmd, ioaddr + MIICtrl);
 638        /* Typical operation takes 25 loops. */
 639        for (i = 400; i > 0; i--) {
 640                barrier();
 641                if ((inl(ioaddr + MIICtrl) & MII_READOP) == 0) {
 642                        /* Work around read failure bug. */
 643                        if (phy_id == 1 && location < 6
 644                                && inw(ioaddr + MIIData) == 0xffff) {
 645                                outl(read_cmd, ioaddr + MIICtrl);
 646                                continue;
 647                        }
 648                        return inw(ioaddr + MIIData);
 649                }
 650        }
 651        return 0xffff;
 652}
 653
 654static void mdio_write(struct net_device *dev, int phy_id, int loc, int value)
 655{
 656        long ioaddr = dev->base_addr;
 657        int i;
 658
 659        outw(value, ioaddr + MIIData);
 660        outl((phy_id << 9) | (loc << 4) | MII_WRITEOP, ioaddr + MIICtrl);
 661        for (i = 10000; i > 0; i--) { 
 662                barrier();
 663                if ((inl(ioaddr + MIICtrl) & MII_WRITEOP) == 0)
 664                        break;
 665        }
 666        return;
 667}
 668
 669
 670static int epic_open(struct net_device *dev)
 671{
 672        struct epic_private *ep = dev->priv;
 673        long ioaddr = dev->base_addr;
 674        int i;
 675        int retval;
 676
 677        /* Soft reset the chip. */
 678        outl(0x4001, ioaddr + GENCTL);
 679
 680        if ((retval = request_irq(dev->irq, &epic_interrupt, SA_SHIRQ, dev->name, dev)))
 681                return retval;
 682
 683        epic_init_ring(dev);
 684
 685        outl(0x4000, ioaddr + GENCTL);
 686        /* This magic is documented in SMSC app note 7.15 */
 687        for (i = 16; i > 0; i--)
 688                outl(0x0008, ioaddr + TEST1);
 689
 690        /* Pull the chip out of low-power mode, enable interrupts, and set for
 691           PCI read multiple.  The MIIcfg setting and strange write order are
 692           required by the details of which bits are reset and the transceiver
 693           wiring on the Ositech CardBus card.
 694        */
 695#if 0
 696        outl(dev->if_port == 1 ? 0x13 : 0x12, ioaddr + MIICfg);
 697#endif
 698        if (ep->chip_flags & MII_PWRDWN)
 699                outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
 700
 701#if defined(__powerpc__) || defined(__sparc__)          /* Big endian */
 702        outl(0x4432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
 703        inl(ioaddr + GENCTL);
 704        outl(0x0432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
 705#else
 706        outl(0x4412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
 707        inl(ioaddr + GENCTL);
 708        outl(0x0412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
 709#endif
 710
 711        udelay(20); /* Looks like EPII needs that if you want reliable RX init. FIXME: pci posting bug? */
 712        
 713        for (i = 0; i < 3; i++)
 714                outl(cpu_to_le16(((u16*)dev->dev_addr)[i]), ioaddr + LAN0 + i*4);
 715
 716        ep->tx_threshold = TX_FIFO_THRESH;
 717        outl(ep->tx_threshold, ioaddr + TxThresh);
 718
 719        if (media2miictl[dev->if_port & 15]) {
 720                if (ep->mii_phy_cnt)
 721                        mdio_write(dev, ep->phys[0], MII_BMCR, media2miictl[dev->if_port&15]);
 722                if (dev->if_port == 1) {
 723                        if (debug > 1)
 724                                printk(KERN_INFO "%s: Using the 10base2 transceiver, MII "
 725                                           "status %4.4x.\n",
 726                                           dev->name, mdio_read(dev, ep->phys[0], MII_BMSR));
 727                }
 728        } else {
 729                int mii_lpa = mdio_read(dev, ep->phys[0], MII_LPA);
 730                if (mii_lpa != 0xffff) {
 731                        if ((mii_lpa & LPA_100FULL) || (mii_lpa & 0x01C0) == LPA_10FULL)
 732                                ep->mii.full_duplex = 1;
 733                        else if (! (mii_lpa & LPA_LPACK))
 734                                mdio_write(dev, ep->phys[0], MII_BMCR, BMCR_ANENABLE|BMCR_ANRESTART);
 735                        if (debug > 1)
 736                                printk(KERN_INFO "%s: Setting %s-duplex based on MII xcvr %d"
 737                                           " register read of %4.4x.\n", dev->name,
 738                                           ep->mii.full_duplex ? "full" : "half",
 739                                           ep->phys[0], mii_lpa);
 740                }
 741        }
 742
 743        outl(ep->mii.full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
 744        outl(ep->rx_ring_dma, ioaddr + PRxCDAR);
 745        outl(ep->tx_ring_dma, ioaddr + PTxCDAR);
 746
 747        /* Start the chip's Rx process. */
 748        set_rx_mode(dev);
 749        outl(StartRx | RxQueued, ioaddr + COMMAND);
 750
 751        netif_start_queue(dev);
 752
 753        /* Enable interrupts by setting the interrupt mask. */
 754        outl((ep->chip_flags & TYPE2_INTR ? PCIBusErr175 : PCIBusErr170)
 755                 | CntFull | TxUnderrun | TxDone | TxEmpty
 756                 | RxError | RxOverflow | RxFull | RxHeader | RxDone,
 757                 ioaddr + INTMASK);
 758
 759        if (debug > 1)
 760                printk(KERN_DEBUG "%s: epic_open() ioaddr %lx IRQ %d status %4.4x "
 761                           "%s-duplex.\n",
 762                           dev->name, ioaddr, dev->irq, (int)inl(ioaddr + GENCTL),
 763                           ep->mii.full_duplex ? "full" : "half");
 764
 765        /* Set the timer to switch to check for link beat and perhaps switch
 766           to an alternate media type. */
 767        init_timer(&ep->timer);
 768        ep->timer.expires = jiffies + 3*HZ;
 769        ep->timer.data = (unsigned long)dev;
 770        ep->timer.function = &epic_timer;                               /* timer handler */
 771        add_timer(&ep->timer);
 772
 773        return 0;
 774}
 775
 776/* Reset the chip to recover from a PCI transaction error.
 777   This may occur at interrupt time. */
 778static void epic_pause(struct net_device *dev)
 779{
 780        long ioaddr = dev->base_addr;
 781        struct epic_private *ep = dev->priv;
 782
 783        netif_stop_queue (dev);
 784        
 785        /* Disable interrupts by clearing the interrupt mask. */
 786        outl(0x00000000, ioaddr + INTMASK);
 787        /* Stop the chip's Tx and Rx DMA processes. */
 788        outw(StopRx | StopTxDMA | StopRxDMA, ioaddr + COMMAND);
 789
 790        /* Update the error counts. */
 791        if (inw(ioaddr + COMMAND) != 0xffff) {
 792                ep->stats.rx_missed_errors += inb(ioaddr + MPCNT);
 793                ep->stats.rx_frame_errors += inb(ioaddr + ALICNT);
 794                ep->stats.rx_crc_errors += inb(ioaddr + CRCCNT);
 795        }
 796
 797        /* Remove the packets on the Rx queue. */
 798        epic_rx(dev);
 799}
 800
 801static void epic_restart(struct net_device *dev)
 802{
 803        long ioaddr = dev->base_addr;
 804        struct epic_private *ep = dev->priv;
 805        int i;
 806
 807        /* Soft reset the chip. */
 808        outl(0x4001, ioaddr + GENCTL);
 809
 810        printk(KERN_DEBUG "%s: Restarting the EPIC chip, Rx %d/%d Tx %d/%d.\n",
 811                   dev->name, ep->cur_rx, ep->dirty_rx, ep->dirty_tx, ep->cur_tx);
 812        udelay(1);
 813
 814        /* This magic is documented in SMSC app note 7.15 */
 815        for (i = 16; i > 0; i--)
 816                outl(0x0008, ioaddr + TEST1);
 817
 818#if defined(__powerpc__) || defined(__sparc__)          /* Big endian */
 819        outl(0x0432 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
 820#else
 821        outl(0x0412 | (RX_FIFO_THRESH<<8), ioaddr + GENCTL);
 822#endif
 823        outl(dev->if_port == 1 ? 0x13 : 0x12, ioaddr + MIICfg);
 824        if (ep->chip_flags & MII_PWRDWN)
 825                outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
 826
 827        for (i = 0; i < 3; i++)
 828                outl(cpu_to_le16(((u16*)dev->dev_addr)[i]), ioaddr + LAN0 + i*4);
 829
 830        ep->tx_threshold = TX_FIFO_THRESH;
 831        outl(ep->tx_threshold, ioaddr + TxThresh);
 832        outl(ep->mii.full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
 833        outl(ep->rx_ring_dma + (ep->cur_rx%RX_RING_SIZE)*
 834                sizeof(struct epic_rx_desc), ioaddr + PRxCDAR);
 835        outl(ep->tx_ring_dma + (ep->dirty_tx%TX_RING_SIZE)*
 836                 sizeof(struct epic_tx_desc), ioaddr + PTxCDAR);
 837
 838        /* Start the chip's Rx process. */
 839        set_rx_mode(dev);
 840        outl(StartRx | RxQueued, ioaddr + COMMAND);
 841
 842        /* Enable interrupts by setting the interrupt mask. */
 843        outl((ep->chip_flags & TYPE2_INTR ? PCIBusErr175 : PCIBusErr170)
 844                 | CntFull | TxUnderrun | TxDone | TxEmpty
 845                 | RxError | RxOverflow | RxFull | RxHeader | RxDone,
 846                 ioaddr + INTMASK);
 847        printk(KERN_DEBUG "%s: epic_restart() done, cmd status %4.4x, ctl %4.4x"
 848                   " interrupt %4.4x.\n",
 849                   dev->name, (int)inl(ioaddr + COMMAND), (int)inl(ioaddr + GENCTL),
 850                   (int)inl(ioaddr + INTSTAT));
 851        return;
 852}
 853
 854static void check_media(struct net_device *dev)
 855{
 856        struct epic_private *ep = dev->priv;
 857        long ioaddr = dev->base_addr;
 858        int mii_lpa = ep->mii_phy_cnt ? mdio_read(dev, ep->phys[0], MII_LPA) : 0;
 859        int negotiated = mii_lpa & ep->mii.advertising;
 860        int duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
 861
 862        if (ep->mii.force_media)
 863                return;
 864        if (mii_lpa == 0xffff)          /* Bogus read */
 865                return;
 866        if (ep->mii.full_duplex != duplex) {
 867                ep->mii.full_duplex = duplex;
 868                printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
 869                           " partner capability of %4.4x.\n", dev->name,
 870                           ep->mii.full_duplex ? "full" : "half", ep->phys[0], mii_lpa);
 871                outl(ep->mii.full_duplex ? 0x7F : 0x79, ioaddr + TxCtrl);
 872        }
 873}
 874
 875static void epic_timer(unsigned long data)
 876{
 877        struct net_device *dev = (struct net_device *)data;
 878        struct epic_private *ep = dev->priv;
 879        long ioaddr = dev->base_addr;
 880        int next_tick = 5*HZ;
 881
 882        if (debug > 3) {
 883                printk(KERN_DEBUG "%s: Media monitor tick, Tx status %8.8x.\n",
 884                           dev->name, (int)inl(ioaddr + TxSTAT));
 885                printk(KERN_DEBUG "%s: Other registers are IntMask %4.4x "
 886                           "IntStatus %4.4x RxStatus %4.4x.\n",
 887                           dev->name, (int)inl(ioaddr + INTMASK),
 888                           (int)inl(ioaddr + INTSTAT), (int)inl(ioaddr + RxSTAT));
 889        }
 890
 891        check_media(dev);
 892
 893        ep->timer.expires = jiffies + next_tick;
 894        add_timer(&ep->timer);
 895}
 896
 897static void epic_tx_timeout(struct net_device *dev)
 898{
 899        struct epic_private *ep = dev->priv;
 900        long ioaddr = dev->base_addr;
 901
 902        if (debug > 0) {
 903                printk(KERN_WARNING "%s: Transmit timeout using MII device, "
 904                           "Tx status %4.4x.\n",
 905                           dev->name, (int)inw(ioaddr + TxSTAT));
 906                if (debug > 1) {
 907                        printk(KERN_DEBUG "%s: Tx indices: dirty_tx %d, cur_tx %d.\n",
 908                                   dev->name, ep->dirty_tx, ep->cur_tx);
 909                }
 910        }
 911        if (inw(ioaddr + TxSTAT) & 0x10) {              /* Tx FIFO underflow. */
 912                ep->stats.tx_fifo_errors++;
 913                outl(RestartTx, ioaddr + COMMAND);
 914        } else {
 915                epic_restart(dev);
 916                outl(TxQueued, dev->base_addr + COMMAND);
 917        }
 918
 919        dev->trans_start = jiffies;
 920        ep->stats.tx_errors++;
 921        if (!ep->tx_full)
 922                netif_wake_queue(dev);
 923}
 924
 925/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
 926static void epic_init_ring(struct net_device *dev)
 927{
 928        struct epic_private *ep = dev->priv;
 929        int i;
 930
 931        ep->tx_full = 0;
 932        ep->lock = (spinlock_t) SPIN_LOCK_UNLOCKED;
 933        ep->dirty_tx = ep->cur_tx = 0;
 934        ep->cur_rx = ep->dirty_rx = 0;
 935        ep->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
 936
 937        /* Initialize all Rx descriptors. */
 938        for (i = 0; i < RX_RING_SIZE; i++) {
 939                ep->rx_ring[i].rxstatus = 0;
 940                ep->rx_ring[i].buflength = cpu_to_le32(ep->rx_buf_sz);
 941                ep->rx_ring[i].next = ep->rx_ring_dma + 
 942                                      (i+1)*sizeof(struct epic_rx_desc);
 943                ep->rx_skbuff[i] = 0;
 944        }
 945        /* Mark the last entry as wrapping the ring. */
 946        ep->rx_ring[i-1].next = ep->rx_ring_dma;
 947
 948        /* Fill in the Rx buffers.  Handle allocation failure gracefully. */
 949        for (i = 0; i < RX_RING_SIZE; i++) {
 950                struct sk_buff *skb = dev_alloc_skb(ep->rx_buf_sz);
 951                ep->rx_skbuff[i] = skb;
 952                if (skb == NULL)
 953                        break;
 954                skb->dev = dev;                 /* Mark as being used by this device. */
 955                skb_reserve(skb, 2);    /* 16 byte align the IP header. */
 956                ep->rx_ring[i].bufaddr = pci_map_single(ep->pci_dev, 
 957                        skb->tail, ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
 958                ep->rx_ring[i].rxstatus = cpu_to_le32(DescOwn);
 959        }
 960        ep->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
 961
 962        /* The Tx buffer descriptor is filled in as needed, but we
 963           do need to clear the ownership bit. */
 964        for (i = 0; i < TX_RING_SIZE; i++) {
 965                ep->tx_skbuff[i] = 0;
 966                ep->tx_ring[i].txstatus = 0x0000;
 967                ep->tx_ring[i].next = ep->tx_ring_dma + 
 968                        (i+1)*sizeof(struct epic_tx_desc);
 969        }
 970        ep->tx_ring[i-1].next = ep->tx_ring_dma;
 971        return;
 972}
 973
 974static int epic_start_xmit(struct sk_buff *skb, struct net_device *dev)
 975{
 976        struct epic_private *ep = dev->priv;
 977        int entry, free_count;
 978        u32 ctrl_word;
 979        unsigned long flags;
 980        
 981        if (skb->len < ETH_ZLEN) {
 982                skb = skb_padto(skb, ETH_ZLEN);
 983                if (skb == NULL)
 984                        return 0;
 985        }
 986
 987        /* Caution: the write order is important here, set the field with the
 988           "ownership" bit last. */
 989
 990        /* Calculate the next Tx descriptor entry. */
 991        spin_lock_irqsave(&ep->lock, flags);
 992        free_count = ep->cur_tx - ep->dirty_tx;
 993        entry = ep->cur_tx % TX_RING_SIZE;
 994
 995        ep->tx_skbuff[entry] = skb;
 996        ep->tx_ring[entry].bufaddr = pci_map_single(ep->pci_dev, skb->data, 
 997                                                    skb->len, PCI_DMA_TODEVICE);
 998        if (free_count < TX_QUEUE_LEN/2) {/* Typical path */
 999                ctrl_word = cpu_to_le32(0x100000); /* No interrupt */
1000        } else if (free_count == TX_QUEUE_LEN/2) {
1001                ctrl_word = cpu_to_le32(0x140000); /* Tx-done intr. */
1002        } else if (free_count < TX_QUEUE_LEN - 1) {
1003                ctrl_word = cpu_to_le32(0x100000); /* No Tx-done intr. */
1004        } else {
1005                /* Leave room for an additional entry. */
1006                ctrl_word = cpu_to_le32(0x140000); /* Tx-done intr. */
1007                ep->tx_full = 1;
1008        }
1009        ep->tx_ring[entry].buflength = ctrl_word | cpu_to_le32(skb->len);
1010        ep->tx_ring[entry].txstatus =
1011                ((skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN) << 16)
1012                | cpu_to_le32(DescOwn);
1013
1014        ep->cur_tx++;
1015        if (ep->tx_full)
1016                netif_stop_queue(dev);
1017
1018        spin_unlock_irqrestore(&ep->lock, flags);
1019        /* Trigger an immediate transmit demand. */
1020        outl(TxQueued, dev->base_addr + COMMAND);
1021
1022        dev->trans_start = jiffies;
1023        if (debug > 4)
1024                printk(KERN_DEBUG "%s: Queued Tx packet size %d to slot %d, "
1025                           "flag %2.2x Tx status %8.8x.\n",
1026                           dev->name, (int)skb->len, entry, ctrl_word,
1027                           (int)inl(dev->base_addr + TxSTAT));
1028
1029        return 0;
1030}
1031
1032/* The interrupt handler does all of the Rx thread work and cleans up
1033   after the Tx thread. */
1034static irqreturn_t epic_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1035{
1036        struct net_device *dev = dev_instance;
1037        struct epic_private *ep = dev->priv;
1038        long ioaddr = dev->base_addr;
1039        int status, boguscnt = max_interrupt_work;
1040        unsigned int handled = 0;
1041
1042        do {
1043                status = inl(ioaddr + INTSTAT);
1044                /* Acknowledge all of the current interrupt sources ASAP. */
1045                outl(status & 0x00007fff, ioaddr + INTSTAT);
1046
1047                if (debug > 4)
1048                        printk(KERN_DEBUG "%s: Interrupt, status=%#8.8x new "
1049                                   "intstat=%#8.8x.\n",
1050                                   dev->name, status, (int)inl(ioaddr + INTSTAT));
1051
1052                if ((status & IntrSummary) == 0)
1053                        break;
1054                handled = 1;
1055
1056                if (status & (RxDone | RxStarted | RxEarlyWarn | RxOverflow))
1057                        epic_rx(dev);
1058
1059                if (status & (TxEmpty | TxDone)) {
1060                        unsigned int dirty_tx, cur_tx;
1061
1062                        /* Note: if this lock becomes a problem we can narrow the locked
1063                           region at the cost of occasionally grabbing the lock more
1064                           times. */
1065                        spin_lock(&ep->lock);
1066                        cur_tx = ep->cur_tx;
1067                        dirty_tx = ep->dirty_tx;
1068                        for (; cur_tx - dirty_tx > 0; dirty_tx++) {
1069                                struct sk_buff *skb;
1070                                int entry = dirty_tx % TX_RING_SIZE;
1071                                int txstatus = le32_to_cpu(ep->tx_ring[entry].txstatus);
1072
1073                                if (txstatus & DescOwn)
1074                                        break;                  /* It still hasn't been Txed */
1075
1076                                if ( ! (txstatus & 0x0001)) {
1077                                        /* There was an major error, log it. */
1078#ifndef final_version
1079                                        if (debug > 1)
1080                                                printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
1081                                                           dev->name, txstatus);
1082#endif
1083                                        ep->stats.tx_errors++;
1084                                        if (txstatus & 0x1050) ep->stats.tx_aborted_errors++;
1085                                        if (txstatus & 0x0008) ep->stats.tx_carrier_errors++;
1086                                        if (txstatus & 0x0040) ep->stats.tx_window_errors++;
1087                                        if (txstatus & 0x0010) ep->stats.tx_fifo_errors++;
1088                                } else {
1089                                        ep->stats.collisions += (txstatus >> 8) & 15;
1090                                        ep->stats.tx_packets++;
1091                                        ep->stats.tx_bytes += ep->tx_skbuff[entry]->len;
1092                                }
1093
1094                                /* Free the original skb. */
1095                                skb = ep->tx_skbuff[entry];
1096                                pci_unmap_single(ep->pci_dev, ep->tx_ring[entry].bufaddr, 
1097                                                 skb->len, PCI_DMA_TODEVICE);
1098                                dev_kfree_skb_irq(skb);
1099                                ep->tx_skbuff[entry] = 0;
1100                        }
1101
1102#ifndef final_version
1103                        if (cur_tx - dirty_tx > TX_RING_SIZE) {
1104                                printk(KERN_WARNING "%s: Out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1105                                           dev->name, dirty_tx, cur_tx, ep->tx_full);
1106                                dirty_tx += TX_RING_SIZE;
1107                        }
1108#endif
1109                        ep->dirty_tx = dirty_tx;
1110                        if (ep->tx_full
1111                                && cur_tx - dirty_tx < TX_QUEUE_LEN - 4) {
1112                                /* The ring is no longer full, allow new TX entries. */
1113                                ep->tx_full = 0;
1114                                spin_unlock(&ep->lock);
1115                                netif_wake_queue(dev);
1116                        } else
1117                                spin_unlock(&ep->lock);
1118                }
1119
1120                /* Check uncommon events all at once. */
1121                if (status & (CntFull | TxUnderrun | RxOverflow | RxFull |
1122                                          PCIBusErr170 | PCIBusErr175)) {
1123                        if (status == 0xffffffff) /* Chip failed or removed (CardBus). */
1124                                break;
1125                        /* Always update the error counts to avoid overhead later. */
1126                        ep->stats.rx_missed_errors += inb(ioaddr + MPCNT);
1127                        ep->stats.rx_frame_errors += inb(ioaddr + ALICNT);
1128                        ep->stats.rx_crc_errors += inb(ioaddr + CRCCNT);
1129
1130                        if (status & TxUnderrun) { /* Tx FIFO underflow. */
1131                                ep->stats.tx_fifo_errors++;
1132                                outl(ep->tx_threshold += 128, ioaddr + TxThresh);
1133                                /* Restart the transmit process. */
1134                                outl(RestartTx, ioaddr + COMMAND);
1135                        }
1136                        if (status & RxOverflow) {              /* Missed a Rx frame. */
1137                                ep->stats.rx_errors++;
1138                        }
1139                        if (status & (RxOverflow | RxFull))
1140                                outw(RxQueued, ioaddr + COMMAND);
1141                        if (status & PCIBusErr170) {
1142                                printk(KERN_ERR "%s: PCI Bus Error!  EPIC status %4.4x.\n",
1143                                           dev->name, status);
1144                                epic_pause(dev);
1145                                epic_restart(dev);
1146                        }
1147                        /* Clear all error sources. */
1148                        outl(status & 0x7f18, ioaddr + INTSTAT);
1149                }
1150                if (--boguscnt < 0) {
1151                        printk(KERN_ERR "%s: Too much work at interrupt, "
1152                                   "IntrStatus=0x%8.8x.\n",
1153                                   dev->name, status);
1154                        /* Clear all interrupt sources. */
1155                        outl(0x0001ffff, ioaddr + INTSTAT);
1156                        break;
1157                }
1158        } while (1);
1159
1160        if (debug > 3)
1161                printk(KERN_DEBUG "%s: exiting interrupt, intr_status=%#4.4x.\n",
1162                           dev->name, status);
1163
1164        return IRQ_RETVAL(handled);
1165}
1166
1167static int epic_rx(struct net_device *dev)
1168{
1169        struct epic_private *ep = dev->priv;
1170        int entry = ep->cur_rx % RX_RING_SIZE;
1171        int rx_work_limit = ep->dirty_rx + RX_RING_SIZE - ep->cur_rx;
1172        int work_done = 0;
1173
1174        if (debug > 4)
1175                printk(KERN_DEBUG " In epic_rx(), entry %d %8.8x.\n", entry,
1176                           ep->rx_ring[entry].rxstatus);
1177        /* If we own the next entry, it's a new packet. Send it up. */
1178        while ((ep->rx_ring[entry].rxstatus & cpu_to_le32(DescOwn)) == 0) {
1179                int status = le32_to_cpu(ep->rx_ring[entry].rxstatus);
1180
1181                if (debug > 4)
1182                        printk(KERN_DEBUG "  epic_rx() status was %8.8x.\n", status);
1183                if (--rx_work_limit < 0)
1184                        break;
1185                if (status & 0x2006) {
1186                        if (debug > 2)
1187                                printk(KERN_DEBUG "%s: epic_rx() error status was %8.8x.\n",
1188                                           dev->name, status);
1189                        if (status & 0x2000) {
1190                                printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
1191                                           "multiple buffers, status %4.4x!\n", dev->name, status);
1192                                ep->stats.rx_length_errors++;
1193                        } else if (status & 0x0006)
1194                                /* Rx Frame errors are counted in hardware. */
1195                                ep->stats.rx_errors++;
1196                } else {
1197                        /* Malloc up new buffer, compatible with net-2e. */
1198                        /* Omit the four octet CRC from the length. */
1199                        short pkt_len = (status >> 16) - 4;
1200                        struct sk_buff *skb;
1201
1202                        pci_dma_sync_single(ep->pci_dev, ep->rx_ring[entry].bufaddr, 
1203                                            ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
1204                        if (pkt_len > PKT_BUF_SZ - 4) {
1205                                printk(KERN_ERR "%s: Oversized Ethernet frame, status %x "
1206                                           "%d bytes.\n",
1207                                           dev->name, status, pkt_len);
1208                                pkt_len = 1514;
1209                        }
1210                        /* Check if the packet is long enough to accept without copying
1211                           to a minimally-sized skbuff. */
1212                        if (pkt_len < rx_copybreak
1213                                && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1214                                skb->dev = dev;
1215                                skb_reserve(skb, 2);    /* 16 byte align the IP header */
1216#if 1 /* HAS_IP_COPYSUM */
1217                                eth_copy_and_sum(skb, ep->rx_skbuff[entry]->tail, pkt_len, 0);
1218                                skb_put(skb, pkt_len);
1219#else
1220                                memcpy(skb_put(skb, pkt_len), ep->rx_skbuff[entry]->tail,
1221                                           pkt_len);
1222#endif
1223                        } else {
1224                                pci_unmap_single(ep->pci_dev, 
1225                                        ep->rx_ring[entry].bufaddr, 
1226                                        ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
1227                                skb_put(skb = ep->rx_skbuff[entry], pkt_len);
1228                                ep->rx_skbuff[entry] = NULL;
1229                        }
1230                        skb->protocol = eth_type_trans(skb, dev);
1231                        netif_rx(skb);
1232                        dev->last_rx = jiffies;
1233                        ep->stats.rx_packets++;
1234                        ep->stats.rx_bytes += pkt_len;
1235                }
1236                work_done++;
1237                entry = (++ep->cur_rx) % RX_RING_SIZE;
1238        }
1239
1240        /* Refill the Rx ring buffers. */
1241        for (; ep->cur_rx - ep->dirty_rx > 0; ep->dirty_rx++) {
1242                entry = ep->dirty_rx % RX_RING_SIZE;
1243                if (ep->rx_skbuff[entry] == NULL) {
1244                        struct sk_buff *skb;
1245                        skb = ep->rx_skbuff[entry] = dev_alloc_skb(ep->rx_buf_sz);
1246                        if (skb == NULL)
1247                                break;
1248                        skb->dev = dev;                 /* Mark as being used by this device. */
1249                        skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
1250                        ep->rx_ring[entry].bufaddr = pci_map_single(ep->pci_dev, 
1251                                skb->tail, ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
1252                        work_done++;
1253                }
1254                ep->rx_ring[entry].rxstatus = cpu_to_le32(DescOwn);
1255        }
1256        return work_done;
1257}
1258
1259static int epic_close(struct net_device *dev)
1260{
1261        long ioaddr = dev->base_addr;
1262        struct epic_private *ep = dev->priv;
1263        struct sk_buff *skb;
1264        int i;
1265
1266        netif_stop_queue(dev);
1267
1268        if (debug > 1)
1269                printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
1270                           dev->name, (int)inl(ioaddr + INTSTAT));
1271
1272        del_timer_sync(&ep->timer);
1273        epic_pause(dev);
1274        free_irq(dev->irq, dev);
1275
1276        /* Free all the skbuffs in the Rx queue. */
1277        for (i = 0; i < RX_RING_SIZE; i++) {
1278                skb = ep->rx_skbuff[i];
1279                ep->rx_skbuff[i] = 0;
1280                ep->rx_ring[i].rxstatus = 0;            /* Not owned by Epic chip. */
1281                ep->rx_ring[i].buflength = 0;
1282                if (skb) {
1283                        pci_unmap_single(ep->pci_dev, ep->rx_ring[i].bufaddr, 
1284                                         ep->rx_buf_sz, PCI_DMA_FROMDEVICE);
1285                        dev_kfree_skb(skb);
1286                }
1287                ep->rx_ring[i].bufaddr = 0xBADF00D0; /* An invalid address. */
1288        }
1289        for (i = 0; i < TX_RING_SIZE; i++) {
1290                skb = ep->tx_skbuff[i];
1291                ep->tx_skbuff[i] = 0;
1292                if (!skb)
1293                        continue;
1294                pci_unmap_single(ep->pci_dev, ep->tx_ring[i].bufaddr, 
1295                                 skb->len, PCI_DMA_TODEVICE);
1296                dev_kfree_skb(skb);
1297        }
1298
1299        /* Green! Leave the chip in low-power mode. */
1300        outl(0x0008, ioaddr + GENCTL);
1301
1302        return 0;
1303}
1304
1305static struct net_device_stats *epic_get_stats(struct net_device *dev)
1306{
1307        struct epic_private *ep = dev->priv;
1308        long ioaddr = dev->base_addr;
1309
1310        if (netif_running(dev)) {
1311                /* Update the error counts. */
1312                ep->stats.rx_missed_errors += inb(ioaddr + MPCNT);
1313                ep->stats.rx_frame_errors += inb(ioaddr + ALICNT);
1314                ep->stats.rx_crc_errors += inb(ioaddr + CRCCNT);
1315        }
1316
1317        return &ep->stats;
1318}
1319
1320/* Set or clear the multicast filter for this adaptor.
1321   Note that we only use exclusion around actually queueing the
1322   new frame, not around filling ep->setup_frame.  This is non-deterministic
1323   when re-entered but still correct. */
1324
1325static void set_rx_mode(struct net_device *dev)
1326{
1327        long ioaddr = dev->base_addr;
1328        struct epic_private *ep = dev->priv;
1329        unsigned char mc_filter[8];              /* Multicast hash filter */
1330        int i;
1331
1332        if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
1333                outl(0x002C, ioaddr + RxCtrl);
1334                /* Unconditionally log net taps. */
1335                printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1336                memset(mc_filter, 0xff, sizeof(mc_filter));
1337        } else if ((dev->mc_count > 0)  ||  (dev->flags & IFF_ALLMULTI)) {
1338                /* There is apparently a chip bug, so the multicast filter
1339                   is never enabled. */
1340                /* Too many to filter perfectly -- accept all multicasts. */
1341                memset(mc_filter, 0xff, sizeof(mc_filter));
1342                outl(0x000C, ioaddr + RxCtrl);
1343        } else if (dev->mc_count == 0) {
1344                outl(0x0004, ioaddr + RxCtrl);
1345                return;
1346        } else {                                        /* Never executed, for now. */
1347                struct dev_mc_list *mclist;
1348
1349                memset(mc_filter, 0, sizeof(mc_filter));
1350                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1351                         i++, mclist = mclist->next) {
1352                        unsigned int bit_nr =
1353                                ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f;
1354                        mc_filter[bit_nr >> 3] |= (1 << bit_nr);
1355                }
1356        }
1357        /* ToDo: perhaps we need to stop the Tx and Rx process here? */
1358        if (memcmp(mc_filter, ep->mc_filter, sizeof(mc_filter))) {
1359                for (i = 0; i < 4; i++)
1360                        outw(((u16 *)mc_filter)[i], ioaddr + MC0 + i*4);
1361                memcpy(ep->mc_filter, mc_filter, sizeof(mc_filter));
1362        }
1363        return;
1364}
1365
1366static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1367{
1368        struct epic_private *np = dev->priv;
1369
1370        strcpy (info->driver, DRV_NAME);
1371        strcpy (info->version, DRV_VERSION);
1372        strcpy (info->bus_info, pci_name(np->pci_dev));
1373}
1374
1375static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1376{
1377        struct epic_private *np = dev->priv;
1378        int rc;
1379
1380        spin_lock_irq(&np->lock);
1381        rc = mii_ethtool_gset(&np->mii, cmd);
1382        spin_unlock_irq(&np->lock);
1383
1384        return rc;
1385}
1386
1387static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1388{
1389        struct epic_private *np = dev->priv;
1390        int rc;
1391
1392        spin_lock_irq(&np->lock);
1393        rc = mii_ethtool_sset(&np->mii, cmd);
1394        spin_unlock_irq(&np->lock);
1395
1396        return rc;
1397}
1398
1399static int netdev_nway_reset(struct net_device *dev)
1400{
1401        struct epic_private *np = dev->priv;
1402        return mii_nway_restart(&np->mii);
1403}
1404
1405static u32 netdev_get_link(struct net_device *dev)
1406{
1407        struct epic_private *np = dev->priv;
1408        return mii_link_ok(&np->mii);
1409}
1410
1411static u32 netdev_get_msglevel(struct net_device *dev)
1412{
1413        return debug;
1414}
1415
1416static void netdev_set_msglevel(struct net_device *dev, u32 value)
1417{
1418        debug = value;
1419}
1420
1421static struct ethtool_ops netdev_ethtool_ops = {
1422        .get_drvinfo            = netdev_get_drvinfo,
1423        .get_settings           = netdev_get_settings,
1424        .set_settings           = netdev_set_settings,
1425        .nway_reset             = netdev_nway_reset,
1426        .get_link               = netdev_get_link,
1427        .get_msglevel           = netdev_get_msglevel,
1428        .set_msglevel           = netdev_set_msglevel,
1429        .get_sg                 = ethtool_op_get_sg,
1430        .get_tx_csum            = ethtool_op_get_tx_csum,
1431};
1432
1433static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1434{
1435        struct epic_private *np = dev->priv;
1436        long ioaddr = dev->base_addr;
1437        struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
1438        int rc;
1439
1440        /* power-up, if interface is down */
1441        if (! netif_running(dev)) {
1442                outl(0x0200, ioaddr + GENCTL);
1443                outl((inl(ioaddr + NVCTL) & ~0x003C) | 0x4800, ioaddr + NVCTL);
1444        }
1445
1446        /* all non-ethtool ioctls (the SIOC[GS]MIIxxx ioctls) */
1447        spin_lock_irq(&np->lock);
1448        rc = generic_mii_ioctl(&np->mii, data, cmd, NULL);
1449        spin_unlock_irq(&np->lock);
1450
1451        /* power-down, if interface is down */
1452        if (! netif_running(dev)) {
1453                outl(0x0008, ioaddr + GENCTL);
1454                outl((inl(ioaddr + NVCTL) & ~0x483C) | 0x0000, ioaddr + NVCTL);
1455        }
1456        return rc;
1457}
1458
1459
1460static void __devexit epic_remove_one (struct pci_dev *pdev)
1461{
1462        struct net_device *dev = pci_get_drvdata(pdev);
1463        struct epic_private *ep = dev->priv;
1464        
1465        pci_free_consistent(pdev, TX_TOTAL_SIZE, ep->tx_ring, ep->tx_ring_dma);
1466        pci_free_consistent(pdev, RX_TOTAL_SIZE, ep->rx_ring, ep->rx_ring_dma);
1467        unregister_netdev(dev);
1468#ifndef USE_IO_OPS
1469        iounmap((void*) dev->base_addr);
1470#endif
1471        pci_release_regions(pdev);
1472        free_netdev(dev);
1473        pci_set_drvdata(pdev, NULL);
1474        /* pci_power_off(pdev, -1); */
1475}
1476
1477
1478#ifdef CONFIG_PM
1479
1480static int epic_suspend (struct pci_dev *pdev, u32 state)
1481{
1482        struct net_device *dev = pci_get_drvdata(pdev);
1483        long ioaddr = dev->base_addr;
1484
1485        if (!netif_running(dev))
1486                return 0;
1487        epic_pause(dev);
1488        /* Put the chip into low-power mode. */
1489        outl(0x0008, ioaddr + GENCTL);
1490        /* pci_power_off(pdev, -1); */
1491        return 0;
1492}
1493
1494
1495static int epic_resume (struct pci_dev *pdev)
1496{
1497        struct net_device *dev = pci_get_drvdata(pdev);
1498
1499        if (!netif_running(dev))
1500                return 0;
1501        epic_restart(dev);
1502        /* pci_power_on(pdev); */
1503        return 0;
1504}
1505
1506#endif /* CONFIG_PM */
1507
1508
1509static struct pci_driver epic_driver = {
1510        .name           = DRV_NAME,
1511        .id_table       = epic_pci_tbl,
1512        .probe          = epic_init_one,
1513        .remove         = __devexit_p(epic_remove_one),
1514#ifdef CONFIG_PM
1515        .suspend        = epic_suspend,
1516        .resume         = epic_resume,
1517#endif /* CONFIG_PM */
1518};
1519
1520
1521static int __init epic_init (void)
1522{
1523/* when a module, this is printed whether or not devices are found in probe */
1524#ifdef MODULE
1525        printk (KERN_INFO "%s" KERN_INFO "%s" KERN_INFO "%s",
1526                version, version2, version3);
1527#endif
1528
1529        return pci_module_init (&epic_driver);
1530}
1531
1532
1533static void __exit epic_cleanup (void)
1534{
1535        pci_unregister_driver (&epic_driver);
1536}
1537
1538
1539module_init(epic_init);
1540module_exit(epic_cleanup);
1541
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.