linux-bk/drivers/net/pcnet32.c
<<
>>
Prefs
   1/* pcnet32.c: An AMD PCnet32 ethernet driver for linux. */
   2/*
   3 *      Copyright 1996-1999 Thomas Bogendoerfer
   4 * 
   5 *      Derived from the lance driver written 1993,1994,1995 by Donald Becker.
   6 * 
   7 *      Copyright 1993 United States Government as represented by the
   8 *      Director, National Security Agency.
   9 * 
  10 *      This software may be used and distributed according to the terms
  11 *      of the GNU General Public License, incorporated herein by reference.
  12 *
  13 *      This driver is for PCnet32 and PCnetPCI based ethercards
  14 */
  15/**************************************************************************
  16 *  23 Oct, 2000.
  17 *  Fixed a few bugs, related to running the controller in 32bit mode.
  18 *
  19 *  Carsten Langgaard, carstenl@mips.com
  20 *  Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
  21 *
  22 *************************************************************************/
  23
  24#define DRV_NAME        "pcnet32"
  25#define DRV_VERSION     "1.27b"
  26#define DRV_RELDATE     "01.10.2002"
  27#define PFX             DRV_NAME ": "
  28
  29static const char *version =
  30DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " tsbogend@alpha.franken.de\n";
  31
  32#include <linux/module.h>
  33#include <linux/kernel.h>
  34#include <linux/string.h>
  35#include <linux/errno.h>
  36#include <linux/ioport.h>
  37#include <linux/slab.h>
  38#include <linux/interrupt.h>
  39#include <linux/pci.h>
  40#include <linux/delay.h>
  41#include <linux/init.h>
  42#include <linux/ethtool.h>
  43#include <linux/mii.h>
  44#include <linux/crc32.h>
  45#include <linux/netdevice.h>
  46#include <linux/etherdevice.h>
  47#include <linux/skbuff.h>
  48#include <linux/spinlock.h>
  49
  50#include <asm/bitops.h>
  51#include <asm/dma.h>
  52#include <asm/io.h>
  53#include <asm/uaccess.h>
  54
  55/*
  56 * PCI device identifiers for "new style" Linux PCI Device Drivers
  57 */
  58static struct pci_device_id pcnet32_pci_tbl[] = {
  59    { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE_HOME, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  60    { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  61    { 0, }
  62};
  63
  64MODULE_DEVICE_TABLE (pci, pcnet32_pci_tbl);
  65
  66int cards_found __initdata;
  67
  68/* 
  69 * VLB I/O addresses 
  70 */
  71static unsigned int pcnet32_portlist[] __initdata = 
  72        { 0x300, 0x320, 0x340, 0x360, 0 };
  73
  74
  75
  76static int pcnet32_debug = 1;
  77static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
  78static int pcnet32vlb;   /* check for VLB cards ? */
  79
  80static struct net_device *pcnet32_dev;
  81
  82static int max_interrupt_work = 80;
  83static int rx_copybreak = 200;
  84
  85#define PCNET32_PORT_AUI      0x00
  86#define PCNET32_PORT_10BT     0x01
  87#define PCNET32_PORT_GPSI     0x02
  88#define PCNET32_PORT_MII      0x03
  89
  90#define PCNET32_PORT_PORTSEL  0x03
  91#define PCNET32_PORT_ASEL     0x04
  92#define PCNET32_PORT_100      0x40
  93#define PCNET32_PORT_FD       0x80
  94
  95#define PCNET32_DMA_MASK 0xffffffff
  96
  97#define PCNET32_WATCHDOG_TIMEOUT (jiffies + (2 * HZ))
  98
  99/*
 100 * table to translate option values from tulip
 101 * to internal options
 102 */
 103static unsigned char options_mapping[] = {
 104    PCNET32_PORT_ASEL,                     /*  0 Auto-select      */
 105    PCNET32_PORT_AUI,                      /*  1 BNC/AUI          */
 106    PCNET32_PORT_AUI,                      /*  2 AUI/BNC          */ 
 107    PCNET32_PORT_ASEL,                     /*  3 not supported    */
 108    PCNET32_PORT_10BT | PCNET32_PORT_FD,   /*  4 10baseT-FD       */
 109    PCNET32_PORT_ASEL,                     /*  5 not supported    */
 110    PCNET32_PORT_ASEL,                     /*  6 not supported    */
 111    PCNET32_PORT_ASEL,                     /*  7 not supported    */
 112    PCNET32_PORT_ASEL,                     /*  8 not supported    */
 113    PCNET32_PORT_MII,                      /*  9 MII 10baseT      */
 114    PCNET32_PORT_MII | PCNET32_PORT_FD,    /* 10 MII 10baseT-FD   */
 115    PCNET32_PORT_MII,                      /* 11 MII (autosel)    */
 116    PCNET32_PORT_10BT,                     /* 12 10BaseT          */
 117    PCNET32_PORT_MII | PCNET32_PORT_100,   /* 13 MII 100BaseTx    */
 118    PCNET32_PORT_MII | PCNET32_PORT_100 | PCNET32_PORT_FD, /* 14 MII 100BaseTx-FD */
 119    PCNET32_PORT_ASEL                      /* 15 not supported    */
 120};
 121
 122#define MAX_UNITS 8     /* More are supported, limit only on options */
 123static int options[MAX_UNITS];
 124static int full_duplex[MAX_UNITS];
 125
 126/*
 127 *                              Theory of Operation
 128 * 
 129 * This driver uses the same software structure as the normal lance
 130 * driver. So look for a verbose description in lance.c. The differences
 131 * to the normal lance driver is the use of the 32bit mode of PCnet32
 132 * and PCnetPCI chips. Because these chips are 32bit chips, there is no
 133 * 16MB limitation and we don't need bounce buffers.
 134 */
 135 
 136/*
 137 * History:
 138 * v0.01:  Initial version
 139 *         only tested on Alpha Noname Board
 140 * v0.02:  changed IRQ handling for new interrupt scheme (dev_id)
 141 *         tested on a ASUS SP3G
 142 * v0.10:  fixed an odd problem with the 79C974 in a Compaq Deskpro XL
 143 *         looks like the 974 doesn't like stopping and restarting in a
 144 *         short period of time; now we do a reinit of the lance; the
 145 *         bug was triggered by doing ifconfig eth0 <ip> broadcast <addr>
 146 *         and hangs the machine (thanks to Klaus Liedl for debugging)
 147 * v0.12:  by suggestion from Donald Becker: Renamed driver to pcnet32,
 148 *         made it standalone (no need for lance.c)
 149 * v0.13:  added additional PCI detecting for special PCI devices (Compaq)
 150 * v0.14:  stripped down additional PCI probe (thanks to David C Niemi
 151 *         and sveneric@xs4all.nl for testing this on their Compaq boxes)
 152 * v0.15:  added 79C965 (VLB) probe
 153 *         added interrupt sharing for PCI chips
 154 * v0.16:  fixed set_multicast_list on Alpha machines
 155 * v0.17:  removed hack from dev.c; now pcnet32 uses ethif_probe in Space.c
 156 * v0.19:  changed setting of autoselect bit
 157 * v0.20:  removed additional Compaq PCI probe; there is now a working one
 158 *         in arch/i386/bios32.c
 159 * v0.21:  added endian conversion for ppc, from work by cort@cs.nmt.edu
 160 * v0.22:  added printing of status to ring dump
 161 * v0.23:  changed enet_statistics to net_devive_stats
 162 * v0.90:  added multicast filter
 163 *         added module support
 164 *         changed irq probe to new style
 165 *         added PCnetFast chip id
 166 *         added fix for receive stalls with Intel saturn chipsets
 167 *         added in-place rx skbs like in the tulip driver
 168 *         minor cleanups
 169 * v0.91:  added PCnetFast+ chip id
 170 *         back port to 2.0.x
 171 * v1.00:  added some stuff from Donald Becker's 2.0.34 version
 172 *         added support for byte counters in net_dev_stats
 173 * v1.01:  do ring dumps, only when debugging the driver
 174 *         increased the transmit timeout
 175 * v1.02:  fixed memory leak in pcnet32_init_ring()
 176 * v1.10:  workaround for stopped transmitter
 177 *         added port selection for modules
 178 *         detect special T1/E1 WAN card and setup port selection
 179 * v1.11:  fixed wrong checking of Tx errors
 180 * v1.20:  added check of return value kmalloc (cpeterso@cs.washington.edu)
 181 *         added save original kmalloc addr for freeing (mcr@solidum.com)
 182 *         added support for PCnetHome chip (joe@MIT.EDU)
 183 *         rewritten PCI card detection
 184 *         added dwio mode to get driver working on some PPC machines
 185 * v1.21:  added mii selection and mii ioctl
 186 * v1.22:  changed pci scanning code to make PPC people happy
 187 *         fixed switching to 32bit mode in pcnet32_open() (thanks
 188 *         to Michael Richard <mcr@solidum.com> for noticing this one)
 189 *         added sub vendor/device id matching (thanks again to 
 190 *         Michael Richard <mcr@solidum.com>)
 191 *         added chip id for 79c973/975 (thanks to Zach Brown <zab@zabbo.net>)
 192 * v1.23   fixed small bug, when manual selecting MII speed/duplex
 193 * v1.24   Applied Thomas' patch to use TxStartPoint and thus decrease TxFIFO
 194 *         underflows.  Added tx_start_pt module parameter. Increased
 195 *         TX_RING_SIZE from 16 to 32.  Added #ifdef'd code to use DXSUFLO
 196 *         for FAST[+] chipsets. <kaf@fc.hp.com>
 197 * v1.24ac Added SMP spinlocking - Alan Cox <alan@redhat.com>
 198 * v1.25kf Added No Interrupt on successful Tx for some Tx's <kaf@fc.hp.com>
 199 * v1.26   Converted to pci_alloc_consistent, Jamey Hicks / George France
 200 *                                           <jamey@crl.dec.com>
 201 * -       Fixed a few bugs, related to running the controller in 32bit mode.
 202 *         23 Oct, 2000.  Carsten Langgaard, carstenl@mips.com
 203 *         Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
 204 * v1.26p  Fix oops on rmmod+insmod; plug i/o resource leak - Paul Gortmaker
 205 * v1.27   improved CSR/PROM address detection, lots of cleanups,
 206 *         new pcnet32vlb module option, HP-PARISC support,
 207 *         added module parameter descriptions, 
 208 *         initial ethtool support - Helge Deller <deller@gmx.de>
 209 * v1.27a  Sun Feb 10 2002 Go Taniguchi <go@turbolinux.co.jp>
 210 *         use alloc_etherdev and register_netdev
 211 *         fix pci probe not increment cards_found
 212 *         FD auto negotiate error workaround for xSeries250
 213 *         clean up and using new mii module
 214 * v1.27b  Sep 30 2002 Kent Yoder <yoder1@us.ibm.com>
 215 *         Added timer for cable connection state changes.
 216 */
 217
 218
 219/*
 220 * Set the number of Tx and Rx buffers, using Log_2(# buffers).
 221 * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
 222 * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
 223 */
 224#ifndef PCNET32_LOG_TX_BUFFERS
 225#define PCNET32_LOG_TX_BUFFERS 4
 226#define PCNET32_LOG_RX_BUFFERS 5
 227#endif
 228
 229#define TX_RING_SIZE            (1 << (PCNET32_LOG_TX_BUFFERS))
 230#define TX_RING_MOD_MASK        (TX_RING_SIZE - 1)
 231#define TX_RING_LEN_BITS        ((PCNET32_LOG_TX_BUFFERS) << 12)
 232
 233#define RX_RING_SIZE            (1 << (PCNET32_LOG_RX_BUFFERS))
 234#define RX_RING_MOD_MASK        (RX_RING_SIZE - 1)
 235#define RX_RING_LEN_BITS        ((PCNET32_LOG_RX_BUFFERS) << 4)
 236
 237#define PKT_BUF_SZ              1544
 238
 239/* Offsets from base I/O address. */
 240#define PCNET32_WIO_RDP         0x10
 241#define PCNET32_WIO_RAP         0x12
 242#define PCNET32_WIO_RESET       0x14
 243#define PCNET32_WIO_BDP         0x16
 244
 245#define PCNET32_DWIO_RDP        0x10
 246#define PCNET32_DWIO_RAP        0x14
 247#define PCNET32_DWIO_RESET      0x18
 248#define PCNET32_DWIO_BDP        0x1C
 249
 250#define PCNET32_TOTAL_SIZE      0x20
 251
 252/* The PCNET32 Rx and Tx ring descriptors. */
 253struct pcnet32_rx_head {
 254    u32 base;
 255    s16 buf_length;
 256    s16 status;    
 257    u32 msg_length;
 258    u32 reserved;
 259};
 260        
 261struct pcnet32_tx_head {
 262    u32 base;
 263    s16 length;
 264    s16 status;
 265    u32 misc;
 266    u32 reserved;
 267};
 268
 269/* The PCNET32 32-Bit initialization block, described in databook. */
 270struct pcnet32_init_block {
 271    u16 mode;
 272    u16 tlen_rlen;
 273    u8  phys_addr[6];
 274    u16 reserved;
 275    u32 filter[2];
 276    /* Receive and transmit ring base, along with extra bits. */    
 277    u32 rx_ring;
 278    u32 tx_ring;
 279};
 280
 281/* PCnet32 access functions */
 282struct pcnet32_access {
 283    u16 (*read_csr)(unsigned long, int);
 284    void (*write_csr)(unsigned long, int, u16);
 285    u16 (*read_bcr)(unsigned long, int);
 286    void (*write_bcr)(unsigned long, int, u16);
 287    u16 (*read_rap)(unsigned long);
 288    void (*write_rap)(unsigned long, u16);
 289    void (*reset)(unsigned long);
 290};
 291
 292/*
 293 * The first three fields of pcnet32_private are read by the ethernet device 
 294 * so we allocate the structure should be allocated by pci_alloc_consistent().
 295 */
 296struct pcnet32_private {
 297    /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
 298    struct pcnet32_rx_head    rx_ring[RX_RING_SIZE];
 299    struct pcnet32_tx_head    tx_ring[TX_RING_SIZE];
 300    struct pcnet32_init_block init_block;
 301    dma_addr_t          dma_addr;       /* DMA address of beginning of this object, 
 302                                           returned by pci_alloc_consistent */
 303    struct pci_dev      *pci_dev;       /* Pointer to the associated pci device structure */
 304    const char          *name;
 305    /* The saved address of a sent-in-place packet/buffer, for skfree(). */
 306    struct sk_buff      *tx_skbuff[TX_RING_SIZE];
 307    struct sk_buff      *rx_skbuff[RX_RING_SIZE];
 308    dma_addr_t          tx_dma_addr[TX_RING_SIZE];
 309    dma_addr_t          rx_dma_addr[RX_RING_SIZE];
 310    struct pcnet32_access a;
 311    spinlock_t          lock;           /* Guard lock */
 312    unsigned int        cur_rx, cur_tx; /* The next free ring entry */
 313    unsigned int        dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
 314    struct net_device_stats stats;
 315    char                tx_full;
 316    int                 options;
 317    int shared_irq:1,                   /* shared irq possible */
 318        ltint:1,                        /* enable TxDone-intr inhibitor */
 319        dxsuflo:1,                      /* disable transmit stop on uflo */
 320        mii:1;                          /* mii port available */
 321    struct net_device   *next;
 322    struct mii_if_info mii_if;
 323    struct timer_list   watchdog_timer;
 324};
 325
 326static void pcnet32_probe_vlbus(void);
 327static int  pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
 328static int  pcnet32_probe1(unsigned long, unsigned int, int, struct pci_dev *);
 329static int  pcnet32_open(struct net_device *);
 330static int  pcnet32_init_ring(struct net_device *);
 331static int  pcnet32_start_xmit(struct sk_buff *, struct net_device *);
 332static int  pcnet32_rx(struct net_device *);
 333static void pcnet32_tx_timeout (struct net_device *dev);
 334static irqreturn_t pcnet32_interrupt(int, void *, struct pt_regs *);
 335static int  pcnet32_close(struct net_device *);
 336static struct net_device_stats *pcnet32_get_stats(struct net_device *);
 337static void pcnet32_set_multicast_list(struct net_device *);
 338static int  pcnet32_ioctl(struct net_device *, struct ifreq *, int);
 339static void pcnet32_watchdog(struct net_device *);
 340static int mdio_read(struct net_device *dev, int phy_id, int reg_num);
 341static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val);
 342
 343enum pci_flags_bit {
 344    PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
 345    PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
 346};
 347
 348
 349static u16 pcnet32_wio_read_csr (unsigned long addr, int index)
 350{
 351    outw (index, addr+PCNET32_WIO_RAP);
 352    return inw (addr+PCNET32_WIO_RDP);
 353}
 354
 355static void pcnet32_wio_write_csr (unsigned long addr, int index, u16 val)
 356{
 357    outw (index, addr+PCNET32_WIO_RAP);
 358    outw (val, addr+PCNET32_WIO_RDP);
 359}
 360
 361static u16 pcnet32_wio_read_bcr (unsigned long addr, int index)
 362{
 363    outw (index, addr+PCNET32_WIO_RAP);
 364    return inw (addr+PCNET32_WIO_BDP);
 365}
 366
 367static void pcnet32_wio_write_bcr (unsigned long addr, int index, u16 val)
 368{
 369    outw (index, addr+PCNET32_WIO_RAP);
 370    outw (val, addr+PCNET32_WIO_BDP);
 371}
 372
 373static u16 pcnet32_wio_read_rap (unsigned long addr)
 374{
 375    return inw (addr+PCNET32_WIO_RAP);
 376}
 377
 378static void pcnet32_wio_write_rap (unsigned long addr, u16 val)
 379{
 380    outw (val, addr+PCNET32_WIO_RAP);
 381}
 382
 383static void pcnet32_wio_reset (unsigned long addr)
 384{
 385    inw (addr+PCNET32_WIO_RESET);
 386}
 387
 388static int pcnet32_wio_check (unsigned long addr)
 389{
 390    outw (88, addr+PCNET32_WIO_RAP);
 391    return (inw (addr+PCNET32_WIO_RAP) == 88);
 392}
 393
 394static struct pcnet32_access pcnet32_wio = {
 395    .read_csr   = pcnet32_wio_read_csr,
 396    .write_csr  = pcnet32_wio_write_csr,
 397    .read_bcr   = pcnet32_wio_read_bcr,
 398    .write_bcr  = pcnet32_wio_write_bcr,
 399    .read_rap   = pcnet32_wio_read_rap,
 400    .write_rap  = pcnet32_wio_write_rap,
 401    .reset      = pcnet32_wio_reset
 402};
 403
 404static u16 pcnet32_dwio_read_csr (unsigned long addr, int index)
 405{
 406    outl (index, addr+PCNET32_DWIO_RAP);
 407    return (inl (addr+PCNET32_DWIO_RDP) & 0xffff);
 408}
 409
 410static void pcnet32_dwio_write_csr (unsigned long addr, int index, u16 val)
 411{
 412    outl (index, addr+PCNET32_DWIO_RAP);
 413    outl (val, addr+PCNET32_DWIO_RDP);
 414}
 415
 416static u16 pcnet32_dwio_read_bcr (unsigned long addr, int index)
 417{
 418    outl (index, addr+PCNET32_DWIO_RAP);
 419    return (inl (addr+PCNET32_DWIO_BDP) & 0xffff);
 420}
 421
 422static void pcnet32_dwio_write_bcr (unsigned long addr, int index, u16 val)
 423{
 424    outl (index, addr+PCNET32_DWIO_RAP);
 425    outl (val, addr+PCNET32_DWIO_BDP);
 426}
 427
 428static u16 pcnet32_dwio_read_rap (unsigned long addr)
 429{
 430    return (inl (addr+PCNET32_DWIO_RAP) & 0xffff);
 431}
 432
 433static void pcnet32_dwio_write_rap (unsigned long addr, u16 val)
 434{
 435    outl (val, addr+PCNET32_DWIO_RAP);
 436}
 437
 438static void pcnet32_dwio_reset (unsigned long addr)
 439{
 440    inl (addr+PCNET32_DWIO_RESET);
 441}
 442
 443static int pcnet32_dwio_check (unsigned long addr)
 444{
 445    outl (88, addr+PCNET32_DWIO_RAP);
 446    return ((inl (addr+PCNET32_DWIO_RAP) & 0xffff) == 88);
 447}
 448
 449static struct pcnet32_access pcnet32_dwio = {
 450    .read_csr   = pcnet32_dwio_read_csr,
 451    .write_csr  = pcnet32_dwio_write_csr,
 452    .read_bcr   = pcnet32_dwio_read_bcr,
 453    .write_bcr  = pcnet32_dwio_write_bcr,
 454    .read_rap   = pcnet32_dwio_read_rap,
 455    .write_rap  = pcnet32_dwio_write_rap,
 456    .reset      = pcnet32_dwio_reset
 457};
 458
 459
 460
 461/* only probes for non-PCI devices, the rest are handled by 
 462 * pci_register_driver via pcnet32_probe_pci */
 463
 464static void __devinit
 465pcnet32_probe_vlbus(void)
 466{
 467    unsigned int *port, ioaddr;
 468    
 469    /* search for PCnet32 VLB cards at known addresses */
 470    for (port = pcnet32_portlist; (ioaddr = *port); port++) {
 471        if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_vlbus")) {
 472                /* check if there is really a pcnet chip on that ioaddr */
 473                if ((inb(ioaddr + 14) == 0x57) && (inb(ioaddr + 15) == 0x57)) {
 474                        pcnet32_probe1(ioaddr, 0, 0, NULL);
 475                } else {
 476                        release_region(ioaddr, PCNET32_TOTAL_SIZE);
 477                }
 478        }
 479    }
 480}
 481
 482
 483static int __devinit
 484pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
 485{
 486    unsigned long ioaddr;
 487    int err;
 488
 489    err = pci_enable_device(pdev);
 490    if (err < 0) {
 491        printk(KERN_ERR PFX "failed to enable device -- err=%d\n", err);
 492        return err;
 493    }
 494    pci_set_master(pdev);
 495
 496    ioaddr = pci_resource_start (pdev, 0);
 497    if (!ioaddr) {
 498        printk (KERN_ERR PFX "card has no PCI IO resources, aborting\n");
 499        return -ENODEV;
 500    }
 501    
 502    if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
 503        printk(KERN_ERR PFX "architecture does not support 32bit PCI busmaster DMA\n");
 504        return -ENODEV;
 505    }
 506    if (request_region(ioaddr, PCNET32_TOTAL_SIZE, "pcnet32_probe_pci") == NULL) {
 507            printk(KERN_ERR PFX "io address range already allocated\n");
 508            return -EBUSY;
 509    }
 510
 511    return pcnet32_probe1(ioaddr, pdev->irq, 1, pdev);
 512}
 513
 514
 515/* pcnet32_probe1 
 516 *  Called from both pcnet32_probe_vlbus and pcnet_probe_pci.  
 517 *  pdev will be NULL when called from pcnet32_probe_vlbus.
 518 */
 519static int __devinit
 520pcnet32_probe1(unsigned long ioaddr, unsigned int irq_line, int shared,
 521                struct pci_dev *pdev)
 522{
 523    struct pcnet32_private *lp;
 524    dma_addr_t lp_dma_addr;
 525    int i, media;
 526    int fdx, mii, fset, dxsuflo, ltint;
 527    int chip_version;
 528    char *chipname;
 529    struct net_device *dev;
 530    struct pcnet32_access *a = NULL;
 531    u8 promaddr[6];
 532    int ret = -ENODEV;
 533
 534    /* reset the chip */
 535    pcnet32_wio_reset(ioaddr);
 536
 537    /* NOTE: 16-bit check is first, otherwise some older PCnet chips fail */
 538    if (pcnet32_wio_read_csr(ioaddr, 0) == 4 && pcnet32_wio_check(ioaddr)) {
 539        a = &pcnet32_wio;
 540    } else {
 541        pcnet32_dwio_reset(ioaddr);
 542        if (pcnet32_dwio_read_csr(ioaddr, 0) == 4 && pcnet32_dwio_check(ioaddr)) {
 543            a = &pcnet32_dwio;
 544        } else
 545                goto err_release_region;
 546    }
 547
 548    chip_version = a->read_csr(ioaddr, 88) | (a->read_csr(ioaddr,89) << 16);
 549    if (pcnet32_debug > 2)
 550        printk(KERN_INFO "  PCnet chip version is %#x.\n", chip_version);
 551    if ((chip_version & 0xfff) != 0x003)
 552            goto err_release_region;
 553    
 554    /* initialize variables */
 555    fdx = mii = fset = dxsuflo = ltint = 0;
 556    chip_version = (chip_version >> 12) & 0xffff;
 557
 558    switch (chip_version) {
 559    case 0x2420:
 560        chipname = "PCnet/PCI 79C970"; /* PCI */
 561        break;
 562    case 0x2430:
 563        if (shared)
 564            chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
 565        else
 566            chipname = "PCnet/32 79C965"; /* 486/VL bus */
 567        break;
 568    case 0x2621:
 569        chipname = "PCnet/PCI II 79C970A"; /* PCI */
 570        fdx = 1;
 571        break;
 572    case 0x2623:
 573        chipname = "PCnet/FAST 79C971"; /* PCI */
 574        fdx = 1; mii = 1; fset = 1;
 575        ltint = 1;
 576        break;
 577    case 0x2624:
 578        chipname = "PCnet/FAST+ 79C972"; /* PCI */
 579        fdx = 1; mii = 1; fset = 1;
 580        break;
 581    case 0x2625:
 582        chipname = "PCnet/FAST III 79C973"; /* PCI */
 583        fdx = 1; mii = 1;
 584        break;
 585    case 0x2626:
 586        chipname = "PCnet/Home 79C978"; /* PCI */
 587        fdx = 1;
 588        /* 
 589         * This is based on specs published at www.amd.com.  This section
 590         * assumes that a card with a 79C978 wants to go into 1Mb HomePNA
 591         * mode.  The 79C978 can also go into standard ethernet, and there
 592         * probably should be some sort of module option to select the
 593         * mode by which the card should operate
 594         */
 595        /* switch to home wiring mode */
 596        media = a->read_bcr(ioaddr, 49);
 597#if 0
 598        if (pcnet32_debug > 2)
 599            printk(KERN_DEBUG PFX "media value %#x.\n",  media);
 600        media &= ~3;
 601        media |= 1;
 602#endif
 603        if (pcnet32_debug > 2)
 604            printk(KERN_DEBUG PFX "media reset to %#x.\n",  media);
 605        a->write_bcr(ioaddr, 49, media);
 606        break;
 607    case 0x2627:
 608        chipname = "PCnet/FAST III 79C975"; /* PCI */
 609        fdx = 1; mii = 1;
 610        break;
 611    default:
 612        printk(KERN_INFO PFX "PCnet version %#x, no PCnet32 chip.\n",
 613                        chip_version);
 614        goto err_release_region;
 615    }
 616
 617    /*
 618     *  On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
 619     *  starting until the packet is loaded. Strike one for reliability, lose
 620     *  one for latency - although on PCI this isnt a big loss. Older chips 
 621     *  have FIFO's smaller than a packet, so you can't do this.
 622     */
 623         
 624    if(fset)
 625    {
 626        a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0800));
 627        a->write_csr(ioaddr, 80, (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
 628        dxsuflo = 1;
 629        ltint = 1;
 630    }
 631    
 632    dev = alloc_etherdev(0);
 633    if(!dev) {
 634            ret = -ENOMEM;
 635            goto err_release_region;
 636    }
 637    SET_NETDEV_DEV(dev, &pdev->dev);
 638
 639    printk(KERN_INFO PFX "%s at %#3lx,", chipname, ioaddr);
 640
 641    /* In most chips, after a chip reset, the ethernet address is read from the
 642     * station address PROM at the base address and programmed into the
 643     * "Physical Address Registers" CSR12-14.
 644     * As a precautionary measure, we read the PROM values and complain if
 645     * they disagree with the CSRs.  Either way, we use the CSR values, and
 646     * double check that they are valid.
 647     */
 648    for (i = 0; i < 3; i++) {
 649        unsigned int val;
 650        val = a->read_csr(ioaddr, i+12) & 0x0ffff;
 651        /* There may be endianness issues here. */
 652        dev->dev_addr[2*i] = val & 0x0ff;
 653        dev->dev_addr[2*i+1] = (val >> 8) & 0x0ff;
 654    }
 655
 656    /* read PROM address and compare with CSR address */
 657    for (i = 0; i < 6; i++)
 658        promaddr[i] = inb(ioaddr + i);
 659    
 660    if( memcmp( promaddr, dev->dev_addr, 6)
 661        || !is_valid_ether_addr(dev->dev_addr) ) {
 662#ifndef __powerpc__
 663        if( is_valid_ether_addr(promaddr) ){
 664#else
 665        if( !is_valid_ether_addr(dev->dev_addr)
 666            && is_valid_ether_addr(promaddr)) {
 667#endif
 668            printk(" warning: CSR address invalid,\n");
 669            printk(KERN_INFO "    using instead PROM address of");
 670            memcpy(dev->dev_addr, promaddr, 6);
 671        }
 672    }
 673
 674    /* if the ethernet address is not valid, force to 00:00:00:00:00:00 */
 675    if( !is_valid_ether_addr(dev->dev_addr) )
 676        memset(dev->dev_addr, 0, sizeof(dev->dev_addr));
 677
 678    for (i = 0; i < 6; i++)
 679        printk(" %2.2x", dev->dev_addr[i] );
 680
 681    if (((chip_version + 1) & 0xfffe) == 0x2624) { /* Version 0x2623 or 0x2624 */
 682        i = a->read_csr(ioaddr, 80) & 0x0C00;  /* Check tx_start_pt */
 683        printk("\n" KERN_INFO "    tx_start_pt(0x%04x):",i);
 684        switch(i>>10) {
 685            case 0: printk("  20 bytes,"); break;
 686            case 1: printk("  64 bytes,"); break;
 687            case 2: printk(" 128 bytes,"); break;
 688            case 3: printk("~220 bytes,"); break;
 689        }
 690        i = a->read_bcr(ioaddr, 18);  /* Check Burst/Bus control */
 691        printk(" BCR18(%x):",i&0xffff);
 692        if (i & (1<<5)) printk("BurstWrEn ");
 693        if (i & (1<<6)) printk("BurstRdEn ");
 694        if (i & (1<<7)) printk("DWordIO ");
 695        if (i & (1<<11)) printk("NoUFlow ");
 696        i = a->read_bcr(ioaddr, 25);
 697        printk("\n" KERN_INFO "    SRAMSIZE=0x%04x,",i<<8);
 698        i = a->read_bcr(ioaddr, 26);
 699        printk(" SRAM_BND=0x%04x,",i<<8);
 700        i = a->read_bcr(ioaddr, 27);
 701        if (i & (1<<14)) printk("LowLatRx");
 702    }
 703
 704    dev->base_addr = ioaddr;
 705    /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
 706    if ((lp = pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL) {
 707        ret = -ENOMEM;
 708        goto err_free_netdev;
 709    }
 710
 711    memset(lp, 0, sizeof(*lp));
 712    lp->dma_addr = lp_dma_addr;
 713    lp->pci_dev = pdev;
 714
 715    spin_lock_init(&lp->lock);
 716    
 717    SET_MODULE_OWNER(dev);
 718    SET_NETDEV_DEV(dev, &pdev->dev);
 719    dev->priv = lp;
 720    lp->name = chipname;
 721    lp->shared_irq = shared;
 722    lp->mii_if.full_duplex = fdx;
 723    lp->dxsuflo = dxsuflo;
 724    lp->ltint = ltint;
 725    lp->mii = mii;
 726    if ((cards_found >= MAX_UNITS) || (options[cards_found] > sizeof(options_mapping)))
 727        lp->options = PCNET32_PORT_ASEL;
 728    else
 729        lp->options = options_mapping[options[cards_found]];
 730    lp->mii_if.dev = dev;
 731    lp->mii_if.mdio_read = mdio_read;
 732    lp->mii_if.mdio_write = mdio_write;
 733    
 734    if (fdx && !(lp->options & PCNET32_PORT_ASEL) && 
 735                ((cards_found>=MAX_UNITS) || full_duplex[cards_found]))
 736        lp->options |= PCNET32_PORT_FD;
 737    
 738    if (!a) {
 739      printk(KERN_ERR PFX "No access methods\n");
 740      ret = -ENODEV;
 741      goto err_free_consistent;
 742    }
 743    lp->a = *a;
 744    
 745    /* detect special T1/E1 WAN card by checking for MAC address */
 746    if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0 && dev->dev_addr[2] == 0x75)
 747        lp->options = PCNET32_PORT_FD | PCNET32_PORT_GPSI;
 748
 749    lp->init_block.mode = le16_to_cpu(0x0003);  /* Disable Rx and Tx. */
 750    lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS); 
 751    for (i = 0; i < 6; i++)
 752        lp->init_block.phys_addr[i] = dev->dev_addr[i];
 753    lp->init_block.filter[0] = 0x00000000;
 754    lp->init_block.filter[1] = 0x00000000;
 755    lp->init_block.rx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, rx_ring));
 756    lp->init_block.tx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, tx_ring));
 757    
 758    /* switch pcnet32 to 32bit mode */
 759    a->write_bcr (ioaddr, 20, 2);
 760
 761    a->write_csr (ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) & 0xffff);
 762    a->write_csr (ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) >> 16);
 763    
 764    if (irq_line) {
 765        dev->irq = irq_line;
 766    }
 767    
 768    if (dev->irq >= 2)
 769        printk(" assigned IRQ %d.\n", dev->irq);
 770    else {
 771        unsigned long irq_mask = probe_irq_on();
 772        
 773        /*
 774         * To auto-IRQ we enable the initialization-done and DMA error
 775         * interrupts. For ISA boards we get a DMA error, but VLB and PCI
 776         * boards will work.
 777         */
 778        /* Trigger an initialization just for the interrupt. */
 779        a->write_csr (ioaddr, 0, 0x41);
 780        mdelay (1);
 781        
 782        dev->irq = probe_irq_off (irq_mask);
 783        if (!dev->irq) {
 784            printk(", failed to detect IRQ line.\n");
 785            ret = -ENODEV;
 786            goto err_free_consistent;
 787        }
 788        printk(", probed IRQ %d.\n", dev->irq);
 789    }
 790
 791    /* Set the mii phy_id so that we can query the link state */
 792    if (lp->mii)
 793        lp->mii_if.phy_id = ((lp->a.read_bcr (ioaddr, 33)) >> 5) & 0x1f;
 794
 795    init_timer (&lp->watchdog_timer);
 796    lp->watchdog_timer.data = (unsigned long) dev;
 797    lp->watchdog_timer.function = (void *) &pcnet32_watchdog;
 798    
 799    /* The PCNET32-specific entries in the device structure. */
 800    dev->open = &pcnet32_open;
 801    dev->hard_start_xmit = &pcnet32_start_xmit;
 802    dev->stop = &pcnet32_close;
 803    dev->get_stats = &pcnet32_get_stats;
 804    dev->set_multicast_list = &pcnet32_set_multicast_list;
 805    dev->do_ioctl = &pcnet32_ioctl;
 806    dev->tx_timeout = pcnet32_tx_timeout;
 807    dev->watchdog_timeo = (5*HZ);
 808
 809    lp->next = pcnet32_dev;
 810    pcnet32_dev = dev;
 811
 812    /* Fill in the generic fields of the device structure. */
 813    register_netdev(dev);
 814    printk(KERN_INFO "%s: registered as %s\n",dev->name, lp->name);
 815    cards_found++;
 816    return 0;
 817
 818err_free_consistent:
 819    pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
 820err_free_netdev:
 821    free_netdev(dev);
 822err_release_region:
 823    release_region(ioaddr, PCNET32_TOTAL_SIZE);
 824    return ret;
 825}
 826
 827
 828static int
 829pcnet32_open(struct net_device *dev)
 830{
 831    struct pcnet32_private *lp = dev->priv;
 832    unsigned long ioaddr = dev->base_addr;
 833    u16 val;
 834    int i;
 835
 836    if (dev->irq == 0 ||
 837        request_irq(dev->irq, &pcnet32_interrupt,
 838                    lp->shared_irq ? SA_SHIRQ : 0, lp->name, (void *)dev)) {
 839        return -EAGAIN;
 840    }
 841
 842    /* Check for a valid station address */
 843    if( !is_valid_ether_addr(dev->dev_addr) )
 844        return -EINVAL;
 845
 846    /* Reset the PCNET32 */
 847    lp->a.reset (ioaddr);
 848
 849    /* switch pcnet32 to 32bit mode */
 850    lp->a.write_bcr (ioaddr, 20, 2);
 851
 852    if (pcnet32_debug > 1)
 853        printk(KERN_DEBUG "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
 854               dev->name, dev->irq,
 855               (u32) (lp->dma_addr + offsetof(struct pcnet32_private, tx_ring)),
 856               (u32) (lp->dma_addr + offsetof(struct pcnet32_private, rx_ring)),
 857               (u32) (lp->dma_addr + offsetof(struct pcnet32_private, init_block)));
 858    
 859    /* set/reset autoselect bit */
 860    val = lp->a.read_bcr (ioaddr, 2) & ~2;
 861    if (lp->options & PCNET32_PORT_ASEL)
 862        val |= 2;
 863    lp->a.write_bcr (ioaddr, 2, val);
 864    
 865    /* handle full duplex setting */
 866    if (lp->mii_if.full_duplex) {
 867        val = lp->a.read_bcr (ioaddr, 9) & ~3;
 868        if (lp->options & PCNET32_PORT_FD) {
 869            val |= 1;
 870            if (lp->options == (PCNET32_PORT_FD | PCNET32_PORT_AUI))
 871                val |= 2;
 872        } else if (lp->options & PCNET32_PORT_ASEL) {
 873        /* workaround of xSeries250, turn on for 79C975 only */
 874            i = ((lp->a.read_csr(ioaddr, 88) | (lp->a.read_csr(ioaddr,89) << 16)) >> 12) & 0xffff;
 875            if (i == 0x2627) val |= 3;
 876        }
 877        lp->a.write_bcr (ioaddr, 9, val);
 878    }
 879    
 880    /* set/reset GPSI bit in test register */
 881    val = lp->a.read_csr (ioaddr, 124) & ~0x10;
 882    if ((lp->options & PCNET32_PORT_PORTSEL) == PCNET32_PORT_GPSI)
 883        val |= 0x10;
 884    lp->a.write_csr (ioaddr, 124, val);
 885    
 886    if (lp->mii && !(lp->options & PCNET32_PORT_ASEL)) {
 887        val = lp->a.read_bcr (ioaddr, 32) & ~0x38; /* disable Auto Negotiation, set 10Mpbs, HD */
 888        if (lp->options & PCNET32_PORT_FD)
 889            val |= 0x10;
 890        if (lp->options & PCNET32_PORT_100)
 891            val |= 0x08;
 892        lp->a.write_bcr (ioaddr, 32, val);
 893    } else {
 894        if (lp->options & PCNET32_PORT_ASEL) {  /* enable auto negotiate, setup, disable fd */
 895                val = lp->a.read_bcr(ioaddr, 32) & ~0x98;
 896                val |= 0x20;
 897                lp->a.write_bcr(ioaddr, 32, val);
 898        }
 899    }
 900
 901#ifdef DO_DXSUFLO 
 902    if (lp->dxsuflo) { /* Disable transmit stop on underflow */
 903        val = lp->a.read_csr (ioaddr, 3);
 904        val |= 0x40;
 905        lp->a.write_csr (ioaddr, 3, val);
 906    }
 907#endif
 908
 909    if (lp->ltint) { /* Enable TxDone-intr inhibitor */
 910        val = lp->a.read_csr (ioaddr, 5);
 911        val |= (1<<14);
 912        lp->a.write_csr (ioaddr, 5, val);
 913    }
 914   
 915    lp->init_block.mode = le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
 916    lp->init_block.filter[0] = 0x00000000;
 917    lp->init_block.filter[1] = 0x00000000;
 918    if (pcnet32_init_ring(dev))
 919        return -ENOMEM;
 920    
 921    /* Re-initialize the PCNET32, and start it when done. */
 922    lp->a.write_csr (ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) &0xffff);
 923    lp->a.write_csr (ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) >> 16);
 924
 925    lp->a.write_csr (ioaddr, 4, 0x0915);
 926    lp->a.write_csr (ioaddr, 0, 0x0001);
 927
 928    netif_start_queue(dev);
 929
 930    /* If we have mii, print the link status and start the watchdog */
 931    if (lp->mii) {
 932        mii_check_media (&lp->mii_if, 1, 1);
 933        mod_timer (&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
 934    }
 935    
 936    i = 0;
 937    while (i++ < 100)
 938        if (lp->a.read_csr (ioaddr, 0) & 0x0100)
 939            break;
 940    /* 
 941     * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
 942     * reports that doing so triggers a bug in the '974.
 943     */
 944    lp->a.write_csr (ioaddr, 0, 0x0042);
 945
 946    if (pcnet32_debug > 2)
 947        printk(KERN_DEBUG "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
 948               dev->name, i, (u32) (lp->dma_addr + offsetof(struct pcnet32_private, init_block)),
 949               lp->a.read_csr(ioaddr, 0));
 950
 951
 952    return 0;   /* Always succeed */
 953}
 954
 955/*
 956 * The LANCE has been halted for one reason or another (busmaster memory
 957 * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
 958 * etc.).  Modern LANCE variants always reload their ring-buffer
 959 * configuration when restarted, so we must reinitialize our ring
 960 * context before restarting.  As part of this reinitialization,
 961 * find all packets still on the Tx ring and pretend that they had been
 962 * sent (in effect, drop the packets on the floor) - the higher-level
 963 * protocols will time out and retransmit.  It'd be better to shuffle
 964 * these skbs to a temp list and then actually re-Tx them after
 965 * restarting the chip, but I'm too lazy to do so right now.  dplatt@3do.com
 966 */
 967
 968static void 
 969pcnet32_purge_tx_ring(struct net_device *dev)
 970{
 971    struct pcnet32_private *lp = dev->priv;
 972    int i;
 973
 974    for (i = 0; i < TX_RING_SIZE; i++) {
 975        if (lp->tx_skbuff[i]) {
 976            pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i], lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
 977            dev_kfree_skb_any(lp->tx_skbuff[i]); 
 978            lp->tx_skbuff[i] = NULL;
 979            lp->tx_dma_addr[i] = 0;
 980        }
 981    }
 982}
 983
 984
 985/* Initialize the PCNET32 Rx and Tx rings. */
 986static int
 987pcnet32_init_ring(struct net_device *dev)
 988{
 989    struct pcnet32_private *lp = dev->priv;
 990    int i;
 991
 992    lp->tx_full = 0;
 993    lp->cur_rx = lp->cur_tx = 0;
 994    lp->dirty_rx = lp->dirty_tx = 0;
 995
 996    for (i = 0; i < RX_RING_SIZE; i++) {
 997        struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
 998        if (rx_skbuff == NULL) {
 999            if (!(rx_skbuff = lp->rx_skbuff[i] = dev_alloc_skb (PKT_BUF_SZ))) {
1000                /* there is not much, we can do at this point */
1001                printk(KERN_ERR "%s: pcnet32_init_ring dev_alloc_skb failed.\n",dev->name);
1002                return -1;
1003            }
1004            skb_reserve (rx_skbuff, 2);
1005        }
1006
1007        if (lp->rx_dma_addr[i] == 0)
1008                lp->rx_dma_addr[i] = pci_map_single(lp->pci_dev, rx_skbuff->tail, rx_skbuff->len, PCI_DMA_FROMDEVICE);
1009        lp->rx_ring[i].base = (u32)le32_to_cpu(lp->rx_dma_addr[i]);
1010        lp->rx_ring[i].buf_length = le16_to_cpu(-PKT_BUF_SZ);
1011        lp->rx_ring[i].status = le16_to_cpu(0x8000);
1012    }
1013    /* The Tx buffer address is filled in as needed, but we do need to clear
1014       the upper ownership bit. */
1015    for (i = 0; i < TX_RING_SIZE; i++) {
1016        lp->tx_ring[i].base = 0;
1017        lp->tx_ring[i].status = 0;
1018        lp->tx_dma_addr[i] = 0;
1019    }
1020
1021    lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
1022    for (i = 0; i < 6; i++)
1023        lp->init_block.phys_addr[i] = dev->dev_addr[i];
1024    lp->init_block.rx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, rx_ring));
1025    lp->init_block.tx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, tx_ring));
1026    return 0;
1027}
1028
1029static void
1030pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
1031{
1032    struct pcnet32_private *lp = dev->priv;
1033    unsigned long ioaddr = dev->base_addr;
1034    int i;
1035    
1036    pcnet32_purge_tx_ring(dev);
1037    if (pcnet32_init_ring(dev))
1038        return;
1039    
1040    /* ReInit Ring */
1041    lp->a.write_csr (ioaddr, 0, 1);
1042    i = 0;
1043    while (i++ < 1000)
1044        if (lp->a.read_csr (ioaddr, 0) & 0x0100)
1045            break;
1046
1047    lp->a.write_csr (ioaddr, 0, csr0_bits);
1048}
1049
1050
1051static void
1052pcnet32_tx_timeout (struct net_device *dev)
1053{
1054    struct pcnet32_private *lp = dev->priv;
1055    unsigned long ioaddr = dev->base_addr, flags;
1056
1057    spin_lock_irqsave(&lp->lock, flags);
1058    /* Transmitter timeout, serious problems. */
1059        printk(KERN_ERR "%s: transmit timed out, status %4.4x, resetting.\n",
1060               dev->name, lp->a.read_csr(ioaddr, 0));
1061        lp->a.write_csr (ioaddr, 0, 0x0004);
1062        lp->stats.tx_errors++;
1063        if (pcnet32_debug > 2) {
1064            int i;
1065            printk(KERN_DEBUG " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
1066               lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
1067               lp->cur_rx);
1068            for (i = 0 ; i < RX_RING_SIZE; i++)
1069            printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1070                   lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
1071                   lp->rx_ring[i].msg_length, (unsigned)lp->rx_ring[i].status);
1072            for (i = 0 ; i < TX_RING_SIZE; i++)
1073            printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1074                   lp->tx_ring[i].base, -lp->tx_ring[i].length,
1075                   lp->tx_ring[i].misc, (unsigned)lp->tx_ring[i].status);
1076            printk("\n");
1077        }
1078        pcnet32_restart(dev, 0x0042);
1079
1080        dev->trans_start = jiffies;
1081        netif_start_queue(dev);
1082
1083        spin_unlock_irqrestore(&lp->lock, flags);
1084}
1085
1086
1087static int
1088pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
1089{
1090    struct pcnet32_private *lp = dev->priv;
1091    unsigned long ioaddr = dev->base_addr;
1092    u16 status;
1093    int entry;
1094    unsigned long flags;
1095
1096    if (pcnet32_debug > 3) {
1097        printk(KERN_DEBUG "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
1098               dev->name, lp->a.read_csr(ioaddr, 0));
1099    }
1100
1101    spin_lock_irqsave(&lp->lock, flags);
1102
1103    /* Default status -- will not enable Successful-TxDone
1104     * interrupt when that option is available to us.
1105     */
1106    status = 0x8300;
1107    if ((lp->ltint) &&
1108        ((lp->cur_tx - lp->dirty_tx == TX_RING_SIZE/2) ||
1109         (lp->cur_tx - lp->dirty_tx >= TX_RING_SIZE-2)))
1110    {
1111        /* Enable Successful-TxDone interrupt if we have
1112         * 1/2 of, or nearly all of, our ring buffer Tx'd
1113         * but not yet cleaned up.  Thus, most of the time,
1114         * we will not enable Successful-TxDone interrupts.
1115         */
1116        status = 0x9300;
1117    }
1118  
1119    /* Fill in a Tx ring entry */
1120  
1121    /* Mask to ring buffer boundary. */
1122    entry = lp->cur_tx & TX_RING_MOD_MASK;
1123  
1124    /* Caution: the write order is important here, set the base address
1125       with the "ownership" bits last. */
1126
1127    lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
1128
1129    lp->tx_ring[entry].misc = 0x00000000;
1130
1131    lp->tx_skbuff[entry] = skb;
1132    lp->tx_dma_addr[entry] = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
1133    lp->tx_ring[entry].base = (u32)le32_to_cpu(lp->tx_dma_addr[entry]);
1134    wmb(); /* Make sure owner changes after all others are visible */
1135    lp->tx_ring[entry].status = le16_to_cpu(status);
1136
1137    lp->cur_tx++;
1138    lp->stats.tx_bytes += skb->len;
1139
1140    /* Trigger an immediate send poll. */
1141    lp->a.write_csr (ioaddr, 0, 0x0048);
1142
1143    dev->trans_start = jiffies;
1144
1145    if (lp->tx_ring[(entry+1) & TX_RING_MOD_MASK].base == 0)
1146        netif_start_queue(dev);
1147    else {
1148        lp->tx_full = 1;
1149        netif_stop_queue(dev);
1150    }
1151    spin_unlock_irqrestore(&lp->lock, flags);
1152    return 0;
1153}
1154
1155/* The PCNET32 interrupt handler. */
1156static irqreturn_t
1157pcnet32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1158{
1159    struct net_device *dev = dev_id;
1160    struct pcnet32_private *lp;
1161    unsigned long ioaddr;
1162    u16 csr0,rap;
1163    int boguscnt =  max_interrupt_work;
1164    int must_restart;
1165
1166    if (!dev) {
1167        printk (KERN_DEBUG "%s(): irq %d for unknown device\n",
1168                __FUNCTION__, irq);
1169        return IRQ_NONE;
1170    }
1171
1172    ioaddr = dev->base_addr;
1173    lp = dev->priv;
1174    
1175    spin_lock(&lp->lock);
1176    
1177    rap = lp->a.read_rap(ioaddr);
1178    while ((csr0 = lp->a.read_csr (ioaddr, 0)) & 0x8600 && --boguscnt >= 0) {
1179        /* Acknowledge all of the current interrupt sources ASAP. */
1180        lp->a.write_csr (ioaddr, 0, csr0 & ~0x004f);
1181
1182        must_restart = 0;
1183
1184        if (pcnet32_debug > 5)
1185            printk(KERN_DEBUG "%s: interrupt  csr0=%#2.2x new csr=%#2.2x.\n",
1186                   dev->name, csr0, lp->a.read_csr (ioaddr, 0));
1187
1188        if (csr0 & 0x0400)              /* Rx interrupt */
1189            pcnet32_rx(dev);
1190
1191        if (csr0 & 0x0200) {            /* Tx-done interrupt */
1192            unsigned int dirty_tx = lp->dirty_tx;
1193
1194            while (dirty_tx < lp->cur_tx) {
1195                int entry = dirty_tx & TX_RING_MOD_MASK;
1196                int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
1197                        
1198                if (status < 0)
1199                    break;              /* It still hasn't been Txed */
1200
1201                lp->tx_ring[entry].base = 0;
1202
1203                if (status & 0x4000) {
1204                    /* There was an major error, log it. */
1205                    int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
1206                    lp->stats.tx_errors++;
1207                    if (err_status & 0x04000000) lp->stats.tx_aborted_errors++;
1208                    if (err_status & 0x08000000) lp->stats.tx_carrier_errors++;
1209                    if (err_status & 0x10000000) lp->stats.tx_window_errors++;
1210#ifndef DO_DXSUFLO
1211                    if (err_status & 0x40000000) {
1212                        lp->stats.tx_fifo_errors++;
1213                        /* Ackk!  On FIFO errors the Tx unit is turned off! */
1214                        /* Remove this verbosity later! */
1215                        printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
1216                               dev->name, csr0);
1217                        must_restart = 1;
1218                    }
1219#else
1220                    if (err_status & 0x40000000) {
1221                        lp->stats.tx_fifo_errors++;
1222                        if (! lp->dxsuflo) {  /* If controller doesn't recover ... */
1223                            /* Ackk!  On FIFO errors the Tx unit is turned off! */
1224                            /* Remove this verbosity later! */
1225                            printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
1226                                   dev->name, csr0);
1227                            must_restart = 1;
1228                        }
1229                    }
1230#endif
1231                } else {
1232                    if (status & 0x1800)
1233                        lp->stats.collisions++;
1234                    lp->stats.tx_packets++;
1235                }
1236
1237                /* We must free the original skb */
1238                if (lp->tx_skbuff[entry]) {
1239                    pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[entry],
1240                        lp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
1241                    dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1242                    lp->tx_skbuff[entry] = 0;
1243                    lp->tx_dma_addr[entry] = 0;
1244                }
1245                dirty_tx++;
1246            }
1247
1248            if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1249                printk(KERN_ERR "%s: out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1250                        dev->name, dirty_tx, lp->cur_tx, lp->tx_full);
1251                dirty_tx += TX_RING_SIZE;
1252            }
1253
1254            if (lp->tx_full &&
1255                netif_queue_stopped(dev) &&
1256                dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
1257                /* The ring is no longer full, clear tbusy. */
1258                lp->tx_full = 0;
1259                netif_wake_queue (dev);
1260            }
1261            lp->dirty_tx = dirty_tx;
1262        }
1263
1264        /* Log misc errors. */
1265        if (csr0 & 0x4000) lp->stats.tx_errors++; /* Tx babble. */
1266        if (csr0 & 0x1000) {
1267            /*
1268             * this happens when our receive ring is full. This shouldn't
1269             * be a problem as we will see normal rx interrupts for the frames
1270             * in the receive ring. But there are some PCI chipsets (I can reproduce
1271             * this on SP3G with Intel saturn chipset) which have sometimes problems
1272             * and will fill up the receive ring with error descriptors. In this
1273             * situation we don't get a rx interrupt, but a missed frame interrupt sooner
1274             * or later. So we try to clean up our receive ring here.
1275             */
1276            pcnet32_rx(dev);
1277            lp->stats.rx_errors++; /* Missed a Rx frame. */
1278        }
1279        if (csr0 & 0x0800) {
1280            printk(KERN_ERR "%s: Bus master arbitration failure, status %4.4x.\n",
1281                   dev->name, csr0);
1282            /* unlike for the lance, there is no restart needed */
1283        }
1284
1285        if (must_restart) {
1286            /* stop the chip to clear the error condition, then restart */
1287            lp->a.write_csr (ioaddr, 0, 0x0004);
1288            pcnet32_restart(dev, 0x0002);
1289        }
1290    }
1291
1292    /* Clear any other interrupt, and set interrupt enable. */
1293    lp->a.write_csr (ioaddr, 0, 0x7940);
1294    lp->a.write_rap (ioaddr,rap);
1295    
1296    if (pcnet32_debug > 4)
1297        printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
1298               dev->name, lp->a.read_csr (ioaddr, 0));
1299
1300    spin_unlock(&lp->lock);
1301
1302    return IRQ_HANDLED;
1303}
1304
1305static int
1306pcnet32_rx(struct net_device *dev)
1307{
1308    struct pcnet32_private *lp = dev->priv;
1309    int entry = lp->cur_rx & RX_RING_MOD_MASK;
1310
1311    /* If we own the next entry, it's a new packet. Send it up. */
1312    while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
1313        int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
1314
1315        if (status != 0x03) {                   /* There was an error. */
1316            /* 
1317             * There is a tricky error noted by John Murphy,
1318             * <murf@perftech.com> to Russ Nelson: Even with full-sized
1319             * buffers it's possible for a jabber packet to use two
1320             * buffers, with only the last correctly noting the error.
1321             */
1322            if (status & 0x01)  /* Only count a general error at the */
1323                lp->stats.rx_errors++; /* end of a packet.*/
1324            if (status & 0x20) lp->stats.rx_frame_errors++;
1325            if (status & 0x10) lp->stats.rx_over_errors++;
1326            if (status & 0x08) lp->stats.rx_crc_errors++;
1327            if (status & 0x04) lp->stats.rx_fifo_errors++;
1328            lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
1329        } else {
1330            /* Malloc up new buffer, compatible with net-2e. */
1331            short pkt_len = (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)-4;
1332            struct sk_buff *skb;
1333                        
1334            if(pkt_len < 60) {
1335                printk(KERN_ERR "%s: Runt packet!\n",dev->name);
1336                lp->stats.rx_errors++;
1337            } else {
1338                int rx_in_place = 0;
1339
1340                if (pkt_len > rx_copybreak) {
1341                    struct sk_buff *newskb;
1342                                
1343                    if ((newskb = dev_alloc_skb (PKT_BUF_SZ))) {
1344                        skb_reserve (newskb, 2);
1345                        skb = lp->rx_skbuff[entry];
1346                        pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[entry], skb->len, PCI_DMA_FROMDEVICE);
1347                        skb_put (skb, pkt_len);
1348                        lp->rx_skbuff[entry] = newskb;
1349                        newskb->dev = dev;
1350                        lp->rx_dma_addr[entry] = 
1351                                pci_map_single(lp->pci_dev, newskb->tail,
1352                                        newskb->len, PCI_DMA_FROMDEVICE);
1353                        lp->rx_ring[entry].base = le32_to_cpu(lp->rx_dma_addr[entry]);
1354                        rx_in_place = 1;
1355                    } else
1356                        skb = NULL;
1357                } else {
1358                    skb = dev_alloc_skb(pkt_len+2);
1359                }
1360                            
1361                if (skb == NULL) {
1362                    int i;
1363                    printk(KERN_ERR "%s: Memory squeeze, deferring packet.\n", dev->name);
1364                    for (i = 0; i < RX_RING_SIZE; i++)
1365                        if ((short)le16_to_cpu(lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].status) < 0)
1366                            break;
1367
1368                    if (i > RX_RING_SIZE -2) {
1369                        lp->stats.rx_dropped++;
1370                        lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
1371                        lp->cur_rx++;
1372                    }
1373                    break;
1374                }
1375                skb->dev = dev;
1376                if (!rx_in_place) {
1377                    skb_reserve(skb,2); /* 16 byte align */
1378                    skb_put(skb,pkt_len);       /* Make room */
1379                    pci_dma_sync_single(lp->pci_dev,
1380                                        lp->rx_dma_addr[entry],
1381                                        PKT_BUF_SZ,
1382                                        PCI_DMA_FROMDEVICE);
1383                    eth_copy_and_sum(skb,
1384                                     (unsigned char *)(lp->rx_skbuff[entry]->tail),
1385                                     pkt_len,0);
1386                }
1387                lp->stats.rx_bytes += skb->len;
1388                skb->protocol=eth_type_trans(skb,dev);
1389                netif_rx(skb);
1390                dev->last_rx = jiffies;
1391                lp->stats.rx_packets++;
1392            }
1393        }
1394        /*
1395         * The docs say that the buffer length isn't touched, but Andrew Boyd
1396         * of QNX reports that some revs of the 79C965 clear it.
1397         */
1398        lp->rx_ring[entry].buf_length = le16_to_cpu(-PKT_BUF_SZ);
1399        lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
1400        entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1401    }
1402
1403    return 0;
1404}
1405
1406static int
1407pcnet32_close(struct net_device *dev)
1408{
1409    unsigned long ioaddr = dev->base_addr;
1410    struct pcnet32_private *lp = dev->priv;
1411    int i;
1412
1413    del_timer_sync(&lp->watchdog_timer);
1414
1415    netif_stop_queue(dev);
1416
1417    lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
1418
1419    if (pcnet32_debug > 1)
1420        printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
1421               dev->name, lp->a.read_csr (ioaddr, 0));
1422
1423    /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
1424    lp->a.write_csr (ioaddr, 0, 0x0004);
1425
1426    /*
1427     * Switch back to 16bit mode to avoid problems with dumb 
1428     * DOS packet driver after a warm reboot
1429     */
1430    lp->a.write_bcr (ioaddr, 20, 4);
1431
1432    free_irq(dev->irq, dev);
1433    
1434    /* free all allocated skbuffs */
1435    for (i = 0; i < RX_RING_SIZE; i++) {
1436        lp->rx_ring[i].status = 0;                          
1437        if (lp->rx_skbuff[i]) {
1438            pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i], lp->rx_skbuff[i]->len, PCI_DMA_FROMDEVICE);
1439            dev_kfree_skb(lp->rx_skbuff[i]);
1440        }
1441        lp->rx_skbuff[i] = NULL;
1442        lp->rx_dma_addr[i] = 0;
1443    }
1444    
1445    for (i = 0; i < TX_RING_SIZE; i++) {
1446        if (lp->tx_skbuff[i]) {
1447            pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i], lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
1448            dev_kfree_skb(lp->tx_skbuff[i]);
1449        }
1450        lp->tx_skbuff[i] = NULL;
1451        lp->tx_dma_addr[i] = 0;
1452    }
1453    
1454    return 0;
1455}
1456
1457static struct net_device_stats *
1458pcnet32_get_stats(struct net_device *dev)
1459{
1460    struct pcnet32_private *lp = dev->priv;
1461    unsigned long ioaddr = dev->base_addr;
1462    u16 saved_addr;
1463    unsigned long flags;
1464
1465    spin_lock_irqsave(&lp->lock, flags);
1466    saved_addr = lp->a.read_rap(ioaddr);
1467    lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
1468    lp->a.write_rap(ioaddr, saved_addr);
1469    spin_unlock_irqrestore(&lp->lock, flags);
1470
1471    return &lp->stats;
1472}
1473
1474/* taken from the sunlance driver, which it took from the depca driver */
1475static void pcnet32_load_multicast (struct net_device *dev)
1476{
1477    struct pcnet32_private *lp = dev->priv;
1478    volatile struct pcnet32_init_block *ib = &lp->init_block;
1479    volatile u16 *mcast_table = (u16 *)&ib->filter;
1480    struct dev_mc_list *dmi=dev->mc_list;
1481    char *addrs;
1482    int i;
1483    u32 crc;
1484        
1485    /* set all multicast bits */
1486    if (dev->flags & IFF_ALLMULTI){ 
1487        ib->filter[0] = 0xffffffff;
1488        ib->filter[1] = 0xffffffff;
1489        return;
1490    }
1491    /* clear the multicast filter */
1492    ib->filter[0] = 0;
1493    ib->filter[1] = 0;
1494
1495    /* Add addresses */
1496    for (i = 0; i < dev->mc_count; i++){
1497        addrs = dmi->dmi_addr;
1498        dmi   = dmi->next;
1499        
1500        /* multicast address? */
1501        if (!(*addrs & 1))
1502            continue;
1503        
1504        crc = ether_crc_le(6, addrs);
1505        crc = crc >> 26;
1506        mcast_table [crc >> 4] = le16_to_cpu(
1507                le16_to_cpu(mcast_table [crc >> 4]) | (1 << (crc & 0xf))
1508        );
1509    }
1510    return;
1511}
1512
1513
1514/*
1515 * Set or clear the multicast filter for this adaptor.
1516 */
1517static void pcnet32_set_multicast_list(struct net_device *dev)
1518{
1519    unsigned long ioaddr = dev->base_addr, flags;
1520    struct pcnet32_private *lp = dev->priv;      
1521
1522    spin_lock_irqsave(&lp->lock, flags);
1523    if (dev->flags&IFF_PROMISC) {
1524        /* Log any net taps. */
1525        printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1526        lp->init_block.mode = le16_to_cpu(0x8000 | (lp->options & PCNET32_PORT_PORTSEL) << 7);
1527    } else {
1528        lp->init_block.mode = le16_to_cpu((lp->options & PCNET32_PORT_PORTSEL) << 7);
1529        pcnet32_load_multicast (dev);
1530    }
1531    
1532    lp->a.write_csr (ioaddr, 0, 0x0004); /* Temporarily stop the lance. */
1533
1534    pcnet32_restart(dev, 0x0042); /*  Resume normal operation */
1535    spin_unlock_irqrestore(&lp->lock, flags);
1536}
1537
1538static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
1539{
1540        struct pcnet32_private *lp = dev->priv;
1541        unsigned long ioaddr = dev->base_addr;
1542        u16 val_out;
1543        int phyaddr;
1544
1545        if (!lp->mii)
1546                return 0;
1547                
1548        phyaddr = lp->a.read_bcr(ioaddr, 33);
1549
1550        lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
1551        val_out = lp->a.read_bcr(ioaddr, 34);
1552        lp->a.write_bcr(ioaddr, 33, phyaddr);
1553        
1554        return val_out;
1555}
1556
1557static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
1558{
1559        struct pcnet32_private *lp = dev->priv;
1560        unsigned long ioaddr = dev->base_addr;
1561        int phyaddr;
1562
1563        if (!lp->mii)
1564                return;
1565                
1566        phyaddr = lp->a.read_bcr(ioaddr, 33);
1567
1568        lp->a.write_bcr(ioaddr, 33, ((phy_id & 0x1f) << 5) | (reg_num & 0x1f));
1569        lp->a.write_bcr(ioaddr, 34, val);
1570        lp->a.write_bcr(ioaddr, 33, phyaddr);
1571}
1572
1573static int pcnet32_ethtool_ioctl (struct net_device *dev, void *useraddr)
1574{
1575        struct pcnet32_private *lp = dev->priv;
1576        u32 ethcmd;
1577        int phyaddr = 0;
1578        int phy_id = 0;
1579        unsigned long ioaddr = dev->base_addr;
1580
1581        if (lp->mii) {
1582                phyaddr = lp->a.read_bcr (ioaddr, 33);
1583                phy_id = (phyaddr >> 5) & 0x1f;
1584                lp->mii_if.phy_id = phy_id;
1585        }
1586
1587        if (copy_from_user (&ethcmd, useraddr, sizeof (ethcmd)))
1588                return -EFAULT;
1589
1590        switch (ethcmd) {
1591        case ETHTOOL_GDRVINFO: {
1592                struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1593                strcpy (info.driver, DRV_NAME);
1594                strcpy (info.version, DRV_VERSION);
1595                if (lp->pci_dev)
1596                        strcpy (info.bus_info, pci_name(lp->pci_dev));
1597                else
1598                        sprintf(info.bus_info, "VLB 0x%lx", dev->base_addr);
1599                if (copy_to_user (useraddr, &info, sizeof (info)))
1600                        return -EFAULT;
1601                return 0;
1602        }
1603
1604        /* get settings */
1605        case ETHTOOL_GSET: {
1606                struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1607                spin_lock_irq(&lp->lock);
1608                mii_ethtool_gset(&lp->mii_if, &ecmd);
1609                spin_unlock_irq(&lp->lock);
1610                if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1611                        return -EFAULT;
1612                return 0;
1613        }
1614        /* set settings */
1615        case ETHTOOL_SSET: {
1616                int r;
1617                struct ethtool_cmd ecmd;
1618                if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1619                        return -EFAULT;
1620                spin_lock_irq(&lp->lock);
1621                r = mii_ethtool_sset(&lp->mii_if, &ecmd);
1622                spin_unlock_irq(&lp->lock);
1623                return r;
1624        }
1625        /* restart autonegotiation */
1626        case ETHTOOL_NWAY_RST: {
1627                return mii_nway_restart(&lp->mii_if);
1628        }
1629        /* get link status */
1630        case ETHTOOL_GLINK: {
1631                struct ethtool_value edata = {ETHTOOL_GLINK};
1632                edata.data = mii_link_ok(&lp->mii_if);
1633                if (copy_to_user(useraddr, &edata, sizeof(edata)))
1634                        return -EFAULT;
1635                return 0;
1636        }
1637
1638        /* get message-level */
1639        case ETHTOOL_GMSGLVL: {
1640                struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1641                edata.data = pcnet32_debug;
1642                if (copy_to_user(useraddr, &edata, sizeof(edata)))
1643                        return -EFAULT;
1644                return 0;
1645        }
1646        /* set message-level */
1647        case ETHTOOL_SMSGLVL: {
1648                struct ethtool_value edata;
1649                if (copy_from_user(&edata, useraddr, sizeof(edata)))
1650                        return -EFAULT;
1651                pcnet32_debug = edata.data;
1652                return 0;
1653        }
1654        default:
1655                break;
1656        }
1657
1658        return -EOPNOTSUPP;
1659}
1660
1661static int pcnet32_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1662{
1663    unsigned long ioaddr = dev->base_addr;
1664    struct pcnet32_private *lp = dev->priv;      
1665    struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
1666    int phyaddr = lp->a.read_bcr (ioaddr, 33);
1667
1668    if (cmd == SIOCETHTOOL)
1669        return pcnet32_ethtool_ioctl(dev, (void *) rq->ifr_data);
1670
1671    if (lp->mii) {
1672        switch(cmd) {
1673        case SIOCGMIIPHY:               /* Get address of MII PHY in use. */
1674            data->phy_id = (phyaddr >> 5) & 0x1f;
1675            /* Fall Through */
1676        case SIOCGMIIREG:               /* Read MII PHY register. */
1677            lp->a.write_bcr (ioaddr, 33, ((data->phy_id & 0x1f) << 5) | (data->reg_num & 0x1f));
1678            data->val_out = lp->a.read_bcr (ioaddr, 34);
1679            lp->a.write_bcr (ioaddr, 33, phyaddr);
1680            return 0;
1681        case SIOCSMIIREG:               /* Write MII PHY register. */
1682            if (!capable(CAP_NET_ADMIN))
1683                return -EPERM;
1684            lp->a.write_bcr (ioaddr, 33, ((data->phy_id & 0x1f) << 5) | (data->reg_num & 0x1f));
1685            lp->a.write_bcr (ioaddr, 34, data->val_in);
1686            lp->a.write_bcr (ioaddr, 33, phyaddr);
1687            return 0;
1688        default:
1689            return -EOPNOTSUPP;
1690        }
1691    }
1692    return -EOPNOTSUPP;
1693}
1694
1695static void pcnet32_watchdog(struct net_device *dev)
1696{
1697    struct pcnet32_private *lp = dev->priv;
1698
1699    /* Print the link status if it has changed */
1700    if (lp->mii)
1701        mii_check_media (&lp->mii_if, 1, 0);
1702
1703    mod_timer (&(lp->watchdog_timer), PCNET32_WATCHDOG_TIMEOUT);
1704}
1705
1706static struct pci_driver pcnet32_driver = {
1707    .name       = DRV_NAME,
1708    .probe      = pcnet32_probe_pci,
1709    .id_table   = pcnet32_pci_tbl,
1710};
1711
1712MODULE_PARM(debug, "i");
1713MODULE_PARM_DESC(debug, DRV_NAME " debug level (0-6)");
1714MODULE_PARM(max_interrupt_work, "i");
1715MODULE_PARM_DESC(max_interrupt_work, DRV_NAME " maximum events handled per interrupt");  
1716MODULE_PARM(rx_copybreak, "i");
1717MODULE_PARM_DESC(rx_copybreak, DRV_NAME " copy breakpoint for copy-only-tiny-frames"); 
1718MODULE_PARM(tx_start_pt, "i");
1719MODULE_PARM_DESC(tx_start_pt, DRV_NAME " transmit start point (0-3)"); 
1720MODULE_PARM(pcnet32vlb, "i");
1721MODULE_PARM_DESC(pcnet32vlb, DRV_NAME " Vesa local bus (VLB) support (0/1)"); 
1722MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
1723MODULE_PARM_DESC(options, DRV_NAME " initial option setting(s) (0-15)"); 
1724MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
1725MODULE_PARM_DESC(full_duplex, DRV_NAME " full duplex setting(s) (1)");
1726
1727MODULE_AUTHOR("Thomas Bogendoerfer");
1728MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
1729MODULE_LICENSE("GPL");
1730
1731/* An additional parameter that may be passed in... */
1732static int debug = -1;
1733static int tx_start_pt = -1;
1734static int pcnet32_have_pci;
1735
1736static int __init pcnet32_init_module(void)
1737{
1738    printk(KERN_INFO "%s", version);
1739
1740    if (debug > 0)
1741        pcnet32_debug = debug;
1742
1743    if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
1744        tx_start = tx_start_pt;
1745
1746    /* find the PCI devices */
1747    if (!pci_module_init(&pcnet32_driver))
1748        pcnet32_have_pci = 1;
1749
1750    /* should we find any remaining VLbus devices ? */
1751    if (pcnet32vlb)
1752        pcnet32_probe_vlbus();
1753
1754    if (cards_found)
1755        printk(KERN_INFO PFX "%d cards_found.\n", cards_found);
1756    
1757    return (pcnet32_have_pci + cards_found) ? 0 : -ENODEV;
1758}
1759
1760static void __exit pcnet32_cleanup_module(void)
1761{
1762    struct net_device *next_dev;
1763
1764    while (pcnet32_dev) {
1765        struct pcnet32_private *lp = pcnet32_dev->priv;
1766        next_dev = lp->next;
1767        unregister_netdev(pcnet32_dev);
1768        release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
1769        pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
1770        free_netdev(pcnet32_dev);
1771        pcnet32_dev = next_dev;
1772    }
1773
1774    if (pcnet32_have_pci)
1775        pci_unregister_driver(&pcnet32_driver);
1776}
1777
1778module_init(pcnet32_init_module);
1779module_exit(pcnet32_cleanup_module);
1780
1781/*
1782 * Local variables:
1783 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c pcnet32.c"
1784 *  c-indent-level: 4
1785 *  tab-width: 8
1786 * End:
1787 */
1788
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.