linux-bk/drivers/net/via-rhine.c
<<
>>
Prefs
   1/* via-rhine.c: A Linux Ethernet device driver for VIA Rhine family chips. */
   2/*
   3        Written 1998-2001 by Donald Becker.
   4
   5        Current Maintainer: Roger Luethi <rl@hellgate.ch>
   6
   7        This software may be used and distributed according to the terms of
   8        the GNU General Public License (GPL), incorporated herein by reference.
   9        Drivers based on or derived from this code fall under the GPL and must
  10        retain the authorship, copyright and license notice.  This file is not
  11        a complete program and may only be used when the entire operating
  12        system is licensed under the GPL.
  13
  14        This driver is designed for the VIA VT86C100A Rhine-I.
  15        It also works with the Rhine-II (6102) and Rhine-III (6105/6105L/6105LOM
  16        and management NIC 6105M).
  17
  18        The author may be reached as becker@scyld.com, or C/O
  19        Scyld Computing Corporation
  20        410 Severn Ave., Suite 210
  21        Annapolis MD 21403
  22
  23
  24        This driver contains some changes from the original Donald Becker
  25        version. He may or may not be interested in bug reports on this
  26        code. You can find his versions at:
  27        http://www.scyld.com/network/via-rhine.html
  28
  29
  30        Linux kernel version history:
  31        
  32        LK1.1.0:
  33        - Jeff Garzik: softnet 'n stuff
  34        
  35        LK1.1.1:
  36        - Justin Guyett: softnet and locking fixes
  37        - Jeff Garzik: use PCI interface
  38
  39        LK1.1.2:
  40        - Urban Widmark: minor cleanups, merges from Becker 1.03a/1.04 versions
  41
  42        LK1.1.3:
  43        - Urban Widmark: use PCI DMA interface (with thanks to the eepro100.c
  44                         code) update "Theory of Operation" with
  45                         softnet/locking changes
  46        - Dave Miller: PCI DMA and endian fixups
  47        - Jeff Garzik: MOD_xxx race fixes, updated PCI resource allocation
  48
  49        LK1.1.4:
  50        - Urban Widmark: fix gcc 2.95.2 problem and
  51                         remove writel's to fixed address 0x7c
  52
  53        LK1.1.5:
  54        - Urban Widmark: mdio locking, bounce buffer changes
  55                         merges from Beckers 1.05 version
  56                         added netif_running_on/off support
  57
  58        LK1.1.6:
  59        - Urban Widmark: merges from Beckers 1.08b version (VT6102 + mdio)
  60                         set netif_running_on/off on startup, del_timer_sync
  61        
  62        LK1.1.7:
  63        - Manfred Spraul: added reset into tx_timeout
  64
  65        LK1.1.9:
  66        - Urban Widmark: merges from Beckers 1.10 version
  67                         (media selection + eeprom reload)
  68        - David Vrabel:  merges from D-Link "1.11" version
  69                         (disable WOL and PME on startup)
  70
  71        LK1.1.10:
  72        - Manfred Spraul: use "singlecopy" for unaligned buffers
  73                          don't allocate bounce buffers for !ReqTxAlign cards
  74
  75        LK1.1.11:
  76        - David Woodhouse: Set dev->base_addr before the first time we call
  77                                           wait_for_reset(). It's a lot happier that way.
  78                                           Free np->tx_bufs only if we actually allocated it.
  79
  80        LK1.1.12:
  81        - Martin Eriksson: Allow Memory-Mapped IO to be enabled.
  82
  83        LK1.1.13 (jgarzik):
  84        - Add ethtool support
  85        - Replace some MII-related magic numbers with constants
  86        
  87        LK1.1.14 (Ivan G.):
  88        - fixes comments for Rhine-III
  89        - removes W_MAX_TIMEOUT (unused)
  90        - adds HasDavicomPhy for Rhine-I (basis: linuxfet driver; my card
  91          is R-I and has Davicom chip, flag is referenced in kernel driver)
  92        - sends chip_id as a parameter to wait_for_reset since np is not
  93          initialized on first call
  94        - changes mmio "else if (chip_id==VT6102)" to "else" so it will work
  95          for Rhine-III's (documentation says same bit is correct)              
  96        - transmit frame queue message is off by one - fixed
  97        - adds IntrNormalSummary to "Something Wicked" exclusion list
  98          so normal interrupts will not trigger the message (src: Donald Becker)
  99        (Roger Luethi)
 100        - show confused chip where to continue after Tx error
 101        - location of collision counter is chip specific
 102        - allow selecting backoff algorithm (module parameter)
 103
 104        LK1.1.15 (jgarzik):
 105        - Use new MII lib helper generic_mii_ioctl
 106
 107        LK1.1.16 (Roger Luethi)
 108        - Etherleak fix
 109        - Handle Tx buffer underrun
 110        - Fix bugs in full duplex handling
 111        - New reset code uses "force reset" cmd on Rhine-II
 112        - Various clean ups
 113
 114        LK1.1.17 (Roger Luethi)
 115        - Fix race in via_rhine_start_tx()
 116        - On errors, wait for Tx engine to turn off before scavenging
 117        - Handle Tx descriptor write-back race on Rhine-II
 118        - Force flushing for PCI posted writes
 119        - More reset code changes
 120
 121        LK1.1.18 (Roger Luethi)
 122        - No filtering multicast in promisc mode (Edward Peng)
 123        - Fix for Rhine-I Tx timeouts
 124
 125        LK1.1.19 (Roger Luethi)
 126        - Increase Tx threshold for unspecified errors
 127
 128*/
 129
 130#define DRV_NAME        "via-rhine"
 131#define DRV_VERSION     "1.1.19-2.5"
 132#define DRV_RELDATE     "July-12-2003"
 133
 134
 135/* A few user-configurable values.
 136   These may be modified when a driver module is loaded. */
 137
 138static int debug = 1;                   /* 1 normal messages, 0 quiet .. 7 verbose. */
 139static int max_interrupt_work = 20;
 140
 141/* Set the copy breakpoint for the copy-only-tiny-frames scheme.
 142   Setting to > 1518 effectively disables this feature. */
 143static int rx_copybreak;
 144
 145/* Select a backoff algorithm (Ethernet capture effect) */
 146static int backoff;
 147
 148/* Used to pass the media type, etc.
 149   Both 'options[]' and 'full_duplex[]' should exist for driver
 150   interoperability.
 151   The media type is usually passed in 'options[]'.
 152   The default is autonegotiation for speed and duplex.
 153     This should rarely be overridden.
 154   Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps.
 155   Use option values 0x10 and 0x100 for forcing half duplex fixed speed.
 156   Use option values 0x20 and 0x200 for forcing full duplex operation.
 157*/
 158#define MAX_UNITS 8             /* More are supported, limit only on options */
 159static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
 160static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
 161
 162/* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
 163   The Rhine has a 64 element 8390-like hash table.  */
 164static const int multicast_filter_limit = 32;
 165
 166
 167/* Operational parameters that are set at compile time. */
 168
 169/* Keep the ring sizes a power of two for compile efficiency.
 170   The compiler will convert <unsigned>'%'<2^N> into a bit mask.
 171   Making the Tx ring too large decreases the effectiveness of channel
 172   bonding and packet priority.
 173   There are no ill effects from too-large receive rings. */
 174#define TX_RING_SIZE    16
 175#define TX_QUEUE_LEN    10              /* Limit ring entries actually used.  */
 176#define RX_RING_SIZE    16
 177
 178
 179/* Operational parameters that usually are not changed. */
 180
 181/* Time in jiffies before concluding the transmitter is hung. */
 182#define TX_TIMEOUT  (2*HZ)
 183
 184#define PKT_BUF_SZ              1536                    /* Size of each temporary Rx buffer.*/
 185
 186#if !defined(__OPTIMIZE__)  ||  !defined(__KERNEL__)
 187#warning  You must compile this file with the correct options!
 188#warning  See the last lines of the source file.
 189#error  You must compile this driver with "-O".
 190#endif
 191
 192#include <linux/module.h>
 193#include <linux/kernel.h>
 194#include <linux/string.h>
 195#include <linux/timer.h>
 196#include <linux/errno.h>
 197#include <linux/ioport.h>
 198#include <linux/slab.h>
 199#include <linux/interrupt.h>
 200#include <linux/pci.h>
 201#include <linux/netdevice.h>
 202#include <linux/etherdevice.h>
 203#include <linux/skbuff.h>
 204#include <linux/init.h>
 205#include <linux/delay.h>
 206#include <linux/mii.h>
 207#include <linux/ethtool.h>
 208#include <linux/crc32.h>
 209#include <asm/processor.h>              /* Processor type for cache alignment. */
 210#include <asm/bitops.h>
 211#include <asm/io.h>
 212#include <asm/irq.h>
 213#include <asm/uaccess.h>
 214
 215/* These identify the driver base version and may not be removed. */
 216static char version[] __devinitdata =
 217KERN_INFO DRV_NAME ".c:v1.10-LK" DRV_VERSION "  " DRV_RELDATE "  Written by Donald Becker\n"
 218KERN_INFO "  http://www.scyld.com/network/via-rhine.html\n";
 219
 220static char shortname[] = DRV_NAME;
 221
 222
 223/* This driver was written to use PCI memory space, however most versions
 224   of the Rhine only work correctly with I/O space accesses. */
 225#ifdef CONFIG_VIA_RHINE_MMIO
 226#define USE_MEM
 227#else
 228#define USE_IO
 229#undef readb
 230#undef readw
 231#undef readl
 232#undef writeb
 233#undef writew
 234#undef writel
 235#define readb inb
 236#define readw inw
 237#define readl inl
 238#define writeb outb
 239#define writew outw
 240#define writel outl
 241#endif
 242
 243MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
 244MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
 245MODULE_LICENSE("GPL");
 246
 247MODULE_PARM(max_interrupt_work, "i");
 248MODULE_PARM(debug, "i");
 249MODULE_PARM(rx_copybreak, "i");
 250MODULE_PARM(backoff, "i");
 251MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
 252MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
 253MODULE_PARM_DESC(max_interrupt_work, "VIA Rhine maximum events handled per interrupt");
 254MODULE_PARM_DESC(debug, "VIA Rhine debug level (0-7)");
 255MODULE_PARM_DESC(rx_copybreak, "VIA Rhine copy breakpoint for copy-only-tiny-frames");
 256MODULE_PARM_DESC(backoff, "VIA Rhine: Bits 0-3: backoff algorithm");
 257MODULE_PARM_DESC(options, "VIA Rhine: Bits 0-3: media type, bit 17: full duplex");
 258MODULE_PARM_DESC(full_duplex, "VIA Rhine full duplex setting(s) (1)");
 259
 260/*
 261                                Theory of Operation
 262
 263I. Board Compatibility
 264
 265This driver is designed for the VIA 86c100A Rhine-II PCI Fast Ethernet
 266controller.
 267
 268II. Board-specific settings
 269
 270Boards with this chip are functional only in a bus-master PCI slot.
 271
 272Many operational settings are loaded from the EEPROM to the Config word at
 273offset 0x78. For most of these settings, this driver assumes that they are
 274correct.
 275If this driver is compiled to use PCI memory space operations the EEPROM
 276must be configured to enable memory ops.
 277
 278III. Driver operation
 279
 280IIIa. Ring buffers
 281
 282This driver uses two statically allocated fixed-size descriptor lists
 283formed into rings by a branch from the final descriptor to the beginning of
 284the list.  The ring sizes are set at compile time by RX/TX_RING_SIZE.
 285
 286IIIb/c. Transmit/Receive Structure
 287
 288This driver attempts to use a zero-copy receive and transmit scheme.
 289
 290Alas, all data buffers are required to start on a 32 bit boundary, so
 291the driver must often copy transmit packets into bounce buffers.
 292
 293The driver allocates full frame size skbuffs for the Rx ring buffers at
 294open() time and passes the skb->data field to the chip as receive data
 295buffers.  When an incoming frame is less than RX_COPYBREAK bytes long,
 296a fresh skbuff is allocated and the frame is copied to the new skbuff.
 297When the incoming frame is larger, the skbuff is passed directly up the
 298protocol stack.  Buffers consumed this way are replaced by newly allocated
 299skbuffs in the last phase of via_rhine_rx().
 300
 301The RX_COPYBREAK value is chosen to trade-off the memory wasted by
 302using a full-sized skbuff for small frames vs. the copying costs of larger
 303frames.  New boards are typically used in generously configured machines
 304and the underfilled buffers have negligible impact compared to the benefit of
 305a single allocation size, so the default value of zero results in never
 306copying packets.  When copying is done, the cost is usually mitigated by using
 307a combined copy/checksum routine.  Copying also preloads the cache, which is
 308most useful with small frames.
 309
 310Since the VIA chips are only able to transfer data to buffers on 32 bit
 311boundaries, the IP header at offset 14 in an ethernet frame isn't
 312longword aligned for further processing.  Copying these unaligned buffers
 313has the beneficial effect of 16-byte aligning the IP header.
 314
 315IIId. Synchronization
 316
 317The driver runs as two independent, single-threaded flows of control.  One
 318is the send-packet routine, which enforces single-threaded use by the
 319dev->priv->lock spinlock. The other thread is the interrupt handler, which 
 320is single threaded by the hardware and interrupt handling software.
 321
 322The send packet thread has partial control over the Tx ring. It locks the 
 323dev->priv->lock whenever it's queuing a Tx packet. If the next slot in the ring
 324is not available it stops the transmit queue by calling netif_stop_queue.
 325
 326The interrupt handler has exclusive control over the Rx ring and records stats
 327from the Tx ring.  After reaping the stats, it marks the Tx queue entry as
 328empty by incrementing the dirty_tx mark. If at least half of the entries in
 329the Rx ring are available the transmit queue is woken up if it was stopped.
 330
 331IV. Notes
 332
 333IVb. References
 334
 335Preliminary VT86C100A manual from http://www.via.com.tw/
 336http://www.scyld.com/expert/100mbps.html
 337http://www.scyld.com/expert/NWay.html
 338ftp://ftp.via.com.tw/public/lan/Products/NIC/VT86C100A/Datasheet/VT86C100A03.pdf
 339ftp://ftp.via.com.tw/public/lan/Products/NIC/VT6102/Datasheet/VT6102_021.PDF
 340
 341
 342IVc. Errata
 343
 344The VT86C100A manual is not reliable information.
 345The 3043 chip does not handle unaligned transmit or receive buffers, resulting
 346in significant performance degradation for bounce buffer copies on transmit
 347and unaligned IP headers on receive.
 348The chip does not pad to minimum transmit length.
 349
 350*/
 351
 352
 353/* This table drives the PCI probe routines.  It's mostly boilerplate in all
 354   of the drivers, and will likely be provided by some future kernel.
 355   Note the matching code -- the first table entry matchs all 56** cards but
 356   second only the 1234 card.
 357*/
 358
 359enum pci_flags_bit {
 360        PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
 361        PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
 362};
 363
 364enum via_rhine_chips {
 365        VT86C100A = 0,
 366        VT6102,
 367        VT6105,
 368        VT6105M
 369};
 370
 371struct via_rhine_chip_info {
 372        const char *name;
 373        u16 pci_flags;
 374        int io_size;
 375        int drv_flags;
 376};
 377
 378
 379enum chip_capability_flags {
 380        CanHaveMII=1, HasESIPhy=2, HasDavicomPhy=4,
 381        ReqTxAlign=0x10, HasWOL=0x20, };
 382
 383#ifdef USE_MEM
 384#define RHINE_IOTYPE (PCI_USES_MEM | PCI_USES_MASTER | PCI_ADDR1)
 385#else
 386#define RHINE_IOTYPE (PCI_USES_IO  | PCI_USES_MASTER | PCI_ADDR0)
 387#endif
 388/* Beware of PCI posted writes */
 389#define IOSYNC  do { readb(dev->base_addr + StationAddr); } while (0)
 390
 391/* directly indexed by enum via_rhine_chips, above */
 392static struct via_rhine_chip_info via_rhine_chip_info[] __devinitdata =
 393{
 394        { "VIA VT86C100A Rhine", RHINE_IOTYPE, 128,
 395          CanHaveMII | ReqTxAlign | HasDavicomPhy },
 396        { "VIA VT6102 Rhine-II", RHINE_IOTYPE, 256,
 397          CanHaveMII | HasWOL },
 398        { "VIA VT6105 Rhine-III", RHINE_IOTYPE, 256,
 399          CanHaveMII | HasWOL },
 400        { "VIA VT6105M Rhine-III", RHINE_IOTYPE, 256,
 401          CanHaveMII | HasWOL },
 402};
 403
 404static struct pci_device_id via_rhine_pci_tbl[] =
 405{
 406        {0x1106, 0x3043, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT86C100A},
 407        {0x1106, 0x3065, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT6102},
 408        {0x1106, 0x3106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT6105}, /* 6105{,L,LOM} */
 409        {0x1106, 0x3053, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VT6105M},
 410        {0,}                    /* terminate list */
 411};
 412MODULE_DEVICE_TABLE(pci, via_rhine_pci_tbl);
 413
 414
 415/* Offsets to the device registers. */
 416enum register_offsets {
 417        StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
 418        IntrStatus=0x0C, IntrEnable=0x0E,
 419        MulticastFilter0=0x10, MulticastFilter1=0x14,
 420        RxRingPtr=0x18, TxRingPtr=0x1C, GFIFOTest=0x54,
 421        MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E,
 422        MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72, MACRegEEcsr=0x74,
 423        ConfigA=0x78, ConfigB=0x79, ConfigC=0x7A, ConfigD=0x7B,
 424        RxMissed=0x7C, RxCRCErrs=0x7E, MiscCmd=0x81,
 425        StickyHW=0x83, IntrStatus2=0x84, WOLcrClr=0xA4, WOLcgClr=0xA7,
 426        PwrcsrClr=0xAC,
 427};
 428
 429/* Bits in ConfigD */
 430enum backoff_bits {
 431        BackOptional=0x01, BackModify=0x02,
 432        BackCaptureEffect=0x04, BackRandom=0x08
 433};
 434
 435#ifdef USE_MEM
 436/* Registers we check that mmio and reg are the same. */
 437int mmio_verify_registers[] = {
 438        RxConfig, TxConfig, IntrEnable, ConfigA, ConfigB, ConfigC, ConfigD,
 439        0
 440};
 441#endif
 442
 443/* Bits in the interrupt status/mask registers. */
 444enum intr_status_bits {
 445        IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
 446        IntrTxDone=0x0002, IntrTxError=0x0008, IntrTxUnderrun=0x0210,
 447        IntrPCIErr=0x0040,
 448        IntrStatsMax=0x0080, IntrRxEarly=0x0100,
 449        IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
 450        IntrTxAborted=0x2000, IntrLinkChange=0x4000,
 451        IntrRxWakeUp=0x8000,
 452        IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
 453        IntrTxDescRace=0x080000,        /* mapped from IntrStatus2 */
 454        IntrTxErrSummary=0x082218,
 455};
 456
 457/* The Rx and Tx buffer descriptors. */
 458struct rx_desc {
 459        s32 rx_status;
 460        u32 desc_length; /* Chain flag, Buffer/frame length */
 461        u32 addr;
 462        u32 next_desc;
 463};
 464struct tx_desc {
 465        s32 tx_status;
 466        u32 desc_length; /* Chain flag, Tx Config, Frame length */
 467        u32 addr;
 468        u32 next_desc;
 469};
 470
 471/* Initial value for tx_desc.desc_length, Buffer size goes to bits 0-10 */
 472#define TXDESC 0x00e08000
 473
 474enum rx_status_bits {
 475        RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F
 476};
 477
 478/* Bits in *_desc.*_status */
 479enum desc_status_bits {
 480        DescOwn=0x80000000
 481};
 482
 483/* Bits in ChipCmd. */
 484enum chip_cmd_bits {
 485        CmdInit=0x0001, CmdStart=0x0002, CmdStop=0x0004, CmdRxOn=0x0008,
 486        CmdTxOn=0x0010, CmdTxDemand=0x0020, CmdRxDemand=0x0040,
 487        CmdEarlyRx=0x0100, CmdEarlyTx=0x0200, CmdFDuplex=0x0400,
 488        CmdNoTxPoll=0x0800, CmdReset=0x8000,
 489};
 490
 491#define MAX_MII_CNT     4
 492struct netdev_private {
 493        /* Descriptor rings */
 494        struct rx_desc *rx_ring;
 495        struct tx_desc *tx_ring;
 496        dma_addr_t rx_ring_dma;
 497        dma_addr_t tx_ring_dma;
 498
 499        /* The addresses of receive-in-place skbuffs. */
 500        struct sk_buff *rx_skbuff[RX_RING_SIZE];
 501        dma_addr_t rx_skbuff_dma[RX_RING_SIZE];
 502
 503        /* The saved address of a sent-in-place packet/buffer, for later free(). */
 504        struct sk_buff *tx_skbuff[TX_RING_SIZE];
 505        dma_addr_t tx_skbuff_dma[TX_RING_SIZE];
 506
 507        /* Tx bounce buffers */
 508        unsigned char *tx_buf[TX_RING_SIZE];
 509        unsigned char *tx_bufs;
 510        dma_addr_t tx_bufs_dma;
 511
 512        struct pci_dev *pdev;
 513        struct net_device_stats stats;
 514        struct timer_list timer;        /* Media monitoring timer. */
 515        spinlock_t lock;
 516
 517        /* Frequently used values: keep some adjacent for cache effect. */
 518        int chip_id, drv_flags;
 519        struct rx_desc *rx_head_desc;
 520        unsigned int cur_rx, dirty_rx;          /* Producer/consumer ring indices */
 521        unsigned int cur_tx, dirty_tx;
 522        unsigned int rx_buf_sz;                         /* Based on MTU+slack. */
 523        u16 chip_cmd;                                           /* Current setting for ChipCmd */
 524
 525        /* These values are keep track of the transceiver/media in use. */
 526        unsigned int default_port:4;            /* Last dev->if_port value. */
 527        u8 tx_thresh, rx_thresh;
 528
 529        /* MII transceiver section. */
 530        unsigned char phys[MAX_MII_CNT];                        /* MII device addresses. */
 531        unsigned int mii_cnt;                   /* number of MIIs found, but only the first one is used */
 532        u16 mii_status;                                         /* last read MII status */
 533        struct mii_if_info mii_if;
 534};
 535
 536static int  mdio_read(struct net_device *dev, int phy_id, int location);
 537static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
 538static int  via_rhine_open(struct net_device *dev);
 539static void via_rhine_check_duplex(struct net_device *dev);
 540static void via_rhine_timer(unsigned long data);
 541static void via_rhine_tx_timeout(struct net_device *dev);
 542static int  via_rhine_start_tx(struct sk_buff *skb, struct net_device *dev);
 543static irqreturn_t via_rhine_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
 544static void via_rhine_tx(struct net_device *dev);
 545static void via_rhine_rx(struct net_device *dev);
 546static void via_rhine_error(struct net_device *dev, int intr_status);
 547static void via_rhine_set_rx_mode(struct net_device *dev);
 548static struct net_device_stats *via_rhine_get_stats(struct net_device *dev);
 549static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 550static struct ethtool_ops netdev_ethtool_ops;
 551static int  via_rhine_close(struct net_device *dev);
 552
 553static inline u32 get_intr_status(struct net_device *dev)
 554{
 555        long ioaddr = dev->base_addr;
 556        struct netdev_private *np = dev->priv;
 557        u32 intr_status;
 558
 559        intr_status = readw(ioaddr + IntrStatus);
 560        /* On Rhine-II, Bit 3 indicates Tx descriptor write-back race. */
 561        if (np->chip_id == VT6102)
 562                intr_status |= readb(ioaddr + IntrStatus2) << 16;
 563        return intr_status;
 564}
 565
 566static void wait_for_reset(struct net_device *dev, int chip_id, char *name)
 567{
 568        long ioaddr = dev->base_addr;
 569        int boguscnt = 20;
 570
 571        IOSYNC;
 572
 573        if (readw(ioaddr + ChipCmd) & CmdReset) {
 574                printk(KERN_INFO "%s: Reset not complete yet. "
 575                        "Trying harder.\n", name);
 576
 577                /* Rhine-II needs to be forced sometimes */
 578                if (chip_id == VT6102)
 579                        writeb(0x40, ioaddr + MiscCmd);
 580
 581                /* VT86C100A may need long delay after reset (dlink) */
 582                /* Seen on Rhine-II as well (rl) */
 583                while ((readw(ioaddr + ChipCmd) & CmdReset) && --boguscnt)
 584                        udelay(5);
 585
 586        }
 587
 588        if (debug > 1)
 589                printk(KERN_INFO "%s: Reset %s.\n", name,
 590                        boguscnt ? "succeeded" : "failed");
 591}
 592
 593#ifdef USE_MEM
 594static void __devinit enable_mmio(long ioaddr, int chip_id)
 595{
 596        int n;
 597        if (chip_id == VT86C100A) {
 598                /* More recent docs say that this bit is reserved ... */
 599                n = inb(ioaddr + ConfigA) | 0x20;
 600                outb(n, ioaddr + ConfigA);
 601        } else {
 602                n = inb(ioaddr + ConfigD) | 0x80;
 603                outb(n, ioaddr + ConfigD);
 604        }
 605}
 606#endif
 607
 608static void __devinit reload_eeprom(long ioaddr)
 609{
 610        int i;
 611        outb(0x20, ioaddr + MACRegEEcsr);
 612        /* Typically 2 cycles to reload. */
 613        for (i = 0; i < 150; i++)
 614                if (! (inb(ioaddr + MACRegEEcsr) & 0x20))
 615                        break;
 616}
 617
 618static int __devinit via_rhine_init_one (struct pci_dev *pdev,
 619                                         const struct pci_device_id *ent)
 620{
 621        struct net_device *dev;
 622        struct netdev_private *np;
 623        int i, option;
 624        int chip_id = (int) ent->driver_data;
 625        static int card_idx = -1;
 626        long ioaddr;
 627        long memaddr;
 628        int io_size;
 629        int pci_flags;
 630#ifdef USE_MEM
 631        long ioaddr0;
 632#endif
 633        
 634/* when built into the kernel, we only print version if device is found */
 635#ifndef MODULE
 636        static int printed_version;
 637        if (!printed_version++)
 638                printk(version);
 639#endif
 640
 641        card_idx++;
 642        option = card_idx < MAX_UNITS ? options[card_idx] : 0;
 643        io_size = via_rhine_chip_info[chip_id].io_size;
 644        pci_flags = via_rhine_chip_info[chip_id].pci_flags;
 645
 646        if (pci_enable_device (pdev))
 647                goto err_out;
 648
 649        /* this should always be supported */
 650        if (pci_set_dma_mask(pdev, 0xffffffff)) {
 651                printk(KERN_ERR "32-bit PCI DMA addresses not supported by the card!?\n");
 652                goto err_out;
 653        }
 654        
 655        /* sanity check */
 656        if ((pci_resource_len (pdev, 0) < io_size) ||
 657            (pci_resource_len (pdev, 1) < io_size)) {
 658                printk (KERN_ERR "Insufficient PCI resources, aborting\n");
 659                goto err_out;
 660        }
 661
 662        ioaddr = pci_resource_start (pdev, 0);
 663        memaddr = pci_resource_start (pdev, 1);
 664
 665        if (pci_flags & PCI_USES_MASTER)
 666                pci_set_master (pdev);
 667
 668        dev = alloc_etherdev(sizeof(*np));
 669        if (dev == NULL) {
 670                printk (KERN_ERR "init_ethernet failed for card #%d\n", card_idx);
 671                goto err_out;
 672        }
 673        SET_MODULE_OWNER(dev);
 674        SET_NETDEV_DEV(dev, &pdev->dev);
 675        
 676        if (pci_request_regions(pdev, shortname))
 677                goto err_out_free_netdev;
 678
 679#ifdef USE_MEM
 680        ioaddr0 = ioaddr;
 681        enable_mmio(ioaddr0, chip_id);
 682
 683        ioaddr = (long) ioremap (memaddr, io_size);
 684        if (!ioaddr) {
 685                printk (KERN_ERR "ioremap failed for device %s, region 0x%X @ 0x%lX\n",
 686                                pci_name(pdev), io_size, memaddr);
 687                goto err_out_free_res;
 688        }
 689
 690        /* Check that selected MMIO registers match the PIO ones */
 691        i = 0;
 692        while (mmio_verify_registers[i]) {
 693                int reg = mmio_verify_registers[i++];
 694                unsigned char a = inb(ioaddr0+reg);
 695                unsigned char b = readb(ioaddr+reg);
 696                if (a != b) {
 697                        printk (KERN_ERR "MMIO do not match PIO [%02x] (%02x != %02x)\n",
 698                                        reg, a, b);
 699                        goto err_out_unmap;
 700                }
 701        }
 702#endif
 703
 704        /* D-Link provided reset code (with comment additions) */
 705        if (via_rhine_chip_info[chip_id].drv_flags & HasWOL) {
 706                unsigned char byOrgValue;
 707
 708                /* clear sticky bit before reset & read ethernet address */
 709                byOrgValue = readb(ioaddr + StickyHW);
 710                byOrgValue = byOrgValue & 0xFC;
 711                writeb(byOrgValue, ioaddr + StickyHW);
 712
 713                /* (bits written are cleared?) */
 714                /* disable force PME-enable */
 715                writeb(0x80, ioaddr + WOLcgClr);
 716                /* disable power-event config bit */
 717                writeb(0xFF, ioaddr + WOLcrClr);
 718                /* clear power status (undocumented in vt6102 docs?) */
 719                writeb(0xFF, ioaddr + PwrcsrClr);
 720        }
 721
 722        /* Reset the chip to erase previous misconfiguration. */
 723        writew(CmdReset, ioaddr + ChipCmd);
 724
 725        dev->base_addr = ioaddr;
 726        wait_for_reset(dev, chip_id, shortname);
 727
 728        /* Reload the station address from the EEPROM. */
 729#ifdef USE_IO
 730        reload_eeprom(ioaddr);
 731#else
 732        reload_eeprom(ioaddr0);
 733        /* Reloading from eeprom overwrites cfgA-D, so we must re-enable MMIO.
 734           If reload_eeprom() was done first this could be avoided, but it is
 735           not known if that still works with the "win98-reboot" problem. */
 736        enable_mmio(ioaddr0, chip_id);
 737#endif
 738
 739        for (i = 0; i < 6; i++)
 740                dev->dev_addr[i] = readb(ioaddr + StationAddr + i);
 741
 742        if (!is_valid_ether_addr(dev->dev_addr)) {
 743                printk(KERN_ERR "Invalid MAC address for card #%d\n", card_idx);
 744                goto err_out_unmap;
 745        }
 746
 747        if (chip_id == VT6102) {
 748                /*
 749                 * for 3065D, EEPROM reloaded will cause bit 0 in MAC_REG_CFGA
 750                 * turned on.  it makes MAC receive magic packet
 751                 * automatically. So, we turn it off. (D-Link)
 752                 */
 753                writeb(readb(ioaddr + ConfigA) & 0xFE, ioaddr + ConfigA);
 754        }
 755
 756        /* Select backoff algorithm */
 757        if (backoff)
 758                writeb(readb(ioaddr + ConfigD) & (0xF0 | backoff),
 759                        ioaddr + ConfigD);
 760
 761        dev->irq = pdev->irq;
 762
 763        np = dev->priv;
 764        spin_lock_init (&np->lock);
 765        np->chip_id = chip_id;
 766        np->drv_flags = via_rhine_chip_info[chip_id].drv_flags;
 767        np->pdev = pdev;
 768        np->mii_if.dev = dev;
 769        np->mii_if.mdio_read = mdio_read;
 770        np->mii_if.mdio_write = mdio_write;
 771        np->mii_if.phy_id_mask = 0x1f;
 772        np->mii_if.reg_num_mask = 0x1f;
 773
 774        if (dev->mem_start)
 775                option = dev->mem_start;
 776
 777        /* The chip-specific entries in the device structure. */
 778        dev->open = via_rhine_open;
 779        dev->hard_start_xmit = via_rhine_start_tx;
 780        dev->stop = via_rhine_close;
 781        dev->get_stats = via_rhine_get_stats;
 782        dev->set_multicast_list = via_rhine_set_rx_mode;
 783        dev->do_ioctl = netdev_ioctl;
 784        dev->ethtool_ops = &netdev_ethtool_ops;
 785        dev->tx_timeout = via_rhine_tx_timeout;
 786        dev->watchdog_timeo = TX_TIMEOUT;
 787        if (np->drv_flags & ReqTxAlign)
 788                dev->features |= NETIF_F_SG|NETIF_F_HW_CSUM;
 789
 790        /* dev->name not defined before register_netdev()! */
 791        i = register_netdev(dev);
 792        if (i)
 793                goto err_out_unmap;
 794
 795        /* The lower four bits are the media type. */
 796        if (option > 0) {
 797                if (option & 0x220)
 798                        np->mii_if.full_duplex = 1;
 799                np->default_port = option & 15;
 800        }
 801        if (card_idx < MAX_UNITS  &&  full_duplex[card_idx] > 0)
 802                np->mii_if.full_duplex = 1;
 803
 804        if (np->mii_if.full_duplex) {
 805                printk(KERN_INFO "%s: Set to forced full duplex, autonegotiation"
 806                           " disabled.\n", dev->name);
 807                np->mii_if.force_media = 1;
 808        }
 809
 810        printk(KERN_INFO "%s: %s at 0x%lx, ",
 811                   dev->name, via_rhine_chip_info[chip_id].name,
 812                   (pci_flags & PCI_USES_IO) ? ioaddr : memaddr);
 813
 814        for (i = 0; i < 5; i++)
 815                        printk("%2.2x:", dev->dev_addr[i]);
 816        printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], pdev->irq);
 817
 818        pci_set_drvdata(pdev, dev);
 819
 820        if (np->drv_flags & CanHaveMII) {
 821                int phy, phy_idx = 0;
 822                np->phys[0] = 1;                /* Standard for this chip. */
 823                for (phy = 1; phy < 32 && phy_idx < MAX_MII_CNT; phy++) {
 824                        int mii_status = mdio_read(dev, phy, 1);
 825                        if (mii_status != 0xffff  &&  mii_status != 0x0000) {
 826                                np->phys[phy_idx++] = phy;
 827                                np->mii_if.advertising = mdio_read(dev, phy, 4);
 828                                printk(KERN_INFO "%s: MII PHY found at address %d, status "
 829                                           "0x%4.4x advertising %4.4x Link %4.4x.\n",
 830                                           dev->name, phy, mii_status, np->mii_if.advertising,
 831                                           mdio_read(dev, phy, 5));
 832
 833                                /* set IFF_RUNNING */
 834                                if (mii_status & BMSR_LSTATUS)
 835                                        netif_carrier_on(dev);
 836                                else
 837                                        netif_carrier_off(dev);
 838                        }
 839                }
 840                np->mii_cnt = phy_idx;
 841                np->mii_if.phy_id = np->phys[0];
 842        }
 843
 844        /* Allow forcing the media type. */
 845        if (option > 0) {
 846                if (option & 0x220)
 847                        np->mii_if.full_duplex = 1;
 848                np->default_port = option & 0x3ff;
 849                if (np->default_port & 0x330) {
 850                        /* FIXME: shouldn't someone check this variable? */
 851                        /* np->medialock = 1; */
 852                        printk(KERN_INFO "  Forcing %dMbs %s-duplex operation.\n",
 853                                   (option & 0x300 ? 100 : 10),
 854                                   (option & 0x220 ? "full" : "half"));
 855                        if (np->mii_cnt)
 856                                mdio_write(dev, np->phys[0], MII_BMCR,
 857                                                   ((option & 0x300) ? 0x2000 : 0) |  /* 100mbps? */
 858                                                   ((option & 0x220) ? 0x0100 : 0));  /* Full duplex? */
 859                }
 860        }
 861
 862        return 0;
 863
 864err_out_unmap:
 865#ifdef USE_MEM
 866        iounmap((void *)ioaddr);
 867err_out_free_res:
 868#endif
 869        pci_release_regions(pdev);
 870err_out_free_netdev:
 871        free_netdev (dev);
 872err_out:
 873        return -ENODEV;
 874}
 875
 876static int alloc_ring(struct net_device* dev)
 877{
 878        struct netdev_private *np = dev->priv;
 879        void *ring;
 880        dma_addr_t ring_dma;
 881
 882        ring = pci_alloc_consistent(np->pdev, 
 883                                    RX_RING_SIZE * sizeof(struct rx_desc) +
 884                                    TX_RING_SIZE * sizeof(struct tx_desc),
 885                                    &ring_dma);
 886        if (!ring) {
 887                printk(KERN_ERR "Could not allocate DMA memory.\n");
 888                return -ENOMEM;
 889        }
 890        if (np->drv_flags & ReqTxAlign) {
 891                np->tx_bufs = pci_alloc_consistent(np->pdev, PKT_BUF_SZ * TX_RING_SIZE,
 892                                                                   &np->tx_bufs_dma);
 893                if (np->tx_bufs == NULL) {
 894                        pci_free_consistent(np->pdev, 
 895                                    RX_RING_SIZE * sizeof(struct rx_desc) +
 896                                    TX_RING_SIZE * sizeof(struct tx_desc),
 897                                    ring, ring_dma);
 898                        return -ENOMEM;
 899                }
 900        }
 901
 902        np->rx_ring = ring;
 903        np->tx_ring = ring + RX_RING_SIZE * sizeof(struct rx_desc);
 904        np->rx_ring_dma = ring_dma;
 905        np->tx_ring_dma = ring_dma + RX_RING_SIZE * sizeof(struct rx_desc);
 906
 907        return 0;
 908}
 909
 910void free_ring(struct net_device* dev)
 911{
 912        struct netdev_private *np = dev->priv;
 913
 914        pci_free_consistent(np->pdev, 
 915                            RX_RING_SIZE * sizeof(struct rx_desc) +
 916                            TX_RING_SIZE * sizeof(struct tx_desc),
 917                            np->rx_ring, np->rx_ring_dma);
 918        np->tx_ring = NULL;
 919
 920        if (np->tx_bufs)
 921                pci_free_consistent(np->pdev, PKT_BUF_SZ * TX_RING_SIZE,
 922                                                        np->tx_bufs, np->tx_bufs_dma);
 923
 924        np->tx_bufs = NULL;
 925
 926}
 927
 928static void alloc_rbufs(struct net_device *dev)
 929{
 930        struct netdev_private *np = dev->priv;
 931        dma_addr_t next;
 932        int i;
 933
 934        np->dirty_rx = np->cur_rx = 0;
 935
 936        np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
 937        np->rx_head_desc = &np->rx_ring[0];
 938        next = np->rx_ring_dma;
 939        
 940        /* Init the ring entries */
 941        for (i = 0; i < RX_RING_SIZE; i++) {
 942                np->rx_ring[i].rx_status = 0;
 943                np->rx_ring[i].desc_length = cpu_to_le32(np->rx_buf_sz);
 944                next += sizeof(struct rx_desc);
 945                np->rx_ring[i].next_desc = cpu_to_le32(next);
 946                np->rx_skbuff[i] = 0;
 947        }
 948        /* Mark the last entry as wrapping the ring. */
 949        np->rx_ring[i-1].next_desc = cpu_to_le32(np->rx_ring_dma);
 950
 951        /* Fill in the Rx buffers.  Handle allocation failure gracefully. */
 952        for (i = 0; i < RX_RING_SIZE; i++) {
 953                struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
 954                np->rx_skbuff[i] = skb;
 955                if (skb == NULL)
 956                        break;
 957                skb->dev = dev;                 /* Mark as being used by this device. */
 958
 959                np->rx_skbuff_dma[i] =
 960                        pci_map_single(np->pdev, skb->tail, np->rx_buf_sz,
 961                                                   PCI_DMA_FROMDEVICE);
 962
 963                np->rx_ring[i].addr = cpu_to_le32(np->rx_skbuff_dma[i]);
 964                np->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
 965        }
 966        np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
 967}
 968
 969static void free_rbufs(struct net_device* dev)
 970{
 971        struct netdev_private *np = dev->priv;
 972        int i;
 973
 974        /* Free all the skbuffs in the Rx queue. */
 975        for (i = 0; i < RX_RING_SIZE; i++) {
 976                np->rx_ring[i].rx_status = 0;
 977                np->rx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
 978                if (np->rx_skbuff[i]) {
 979                        pci_unmap_single(np->pdev,
 980                                                         np->rx_skbuff_dma[i],
 981                                                         np->rx_buf_sz, PCI_DMA_FROMDEVICE);
 982                        dev_kfree_skb(np->rx_skbuff[i]);
 983                }
 984                np->rx_skbuff[i] = 0;
 985        }
 986}
 987
 988static void alloc_tbufs(struct net_device* dev)
 989{
 990        struct netdev_private *np = dev->priv;
 991        dma_addr_t next;
 992        int i;
 993
 994        np->dirty_tx = np->cur_tx = 0;
 995        next = np->tx_ring_dma;
 996        for (i = 0; i < TX_RING_SIZE; i++) {
 997                np->tx_skbuff[i] = 0;
 998                np->tx_ring[i].tx_status = 0;
 999                np->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1000                next += sizeof(struct tx_desc);
1001                np->tx_ring[i].next_desc = cpu_to_le32(next);
1002                np->tx_buf[i] = &np->tx_bufs[i * PKT_BUF_SZ];
1003        }
1004        np->tx_ring[i-1].next_desc = cpu_to_le32(np->tx_ring_dma);
1005
1006}
1007
1008static void free_tbufs(struct net_device* dev)
1009{
1010        struct netdev_private *np = dev->priv;
1011        int i;
1012
1013        for (i = 0; i < TX_RING_SIZE; i++) {
1014                np->tx_ring[i].tx_status = 0;
1015                np->tx_ring[i].desc_length = cpu_to_le32(TXDESC);
1016                np->tx_ring[i].addr = cpu_to_le32(0xBADF00D0); /* An invalid address. */
1017                if (np->tx_skbuff[i]) {
1018                        if (np->tx_skbuff_dma[i]) {
1019                                pci_unmap_single(np->pdev,
1020                                                                 np->tx_skbuff_dma[i],
1021                                                                 np->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
1022                        }
1023                        dev_kfree_skb(np->tx_skbuff[i]);
1024                }
1025                np->tx_skbuff[i] = 0;
1026                np->tx_buf[i] = 0;
1027        }
1028}
1029
1030static void init_registers(struct net_device *dev)
1031{
1032        struct netdev_private *np = dev->priv;
1033        long ioaddr = dev->base_addr;
1034        int i;
1035
1036        for (i = 0; i < 6; i++)
1037                writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
1038
1039        /* Initialize other registers. */
1040        writew(0x0006, ioaddr + PCIBusConfig);  /* Tune configuration??? */
1041        /* Configure initial FIFO thresholds. */
1042        writeb(0x20, ioaddr + TxConfig);
1043        np->tx_thresh = 0x20;
1044        np->rx_thresh = 0x60;                   /* Written in via_rhine_set_rx_mode(). */
1045        np->mii_if.full_duplex = 0;
1046
1047        if (dev->if_port == 0)
1048                dev->if_port = np->default_port;
1049
1050        writel(np->rx_ring_dma, ioaddr + RxRingPtr);
1051        writel(np->tx_ring_dma, ioaddr + TxRingPtr);
1052
1053        via_rhine_set_rx_mode(dev);
1054
1055        /* Enable interrupts by setting the interrupt mask. */
1056        writew(IntrRxDone | IntrRxErr | IntrRxEmpty| IntrRxOverflow |
1057                   IntrRxDropped | IntrRxNoBuf | IntrTxAborted |
1058                   IntrTxDone | IntrTxError | IntrTxUnderrun |
1059                   IntrPCIErr | IntrStatsMax | IntrLinkChange,
1060                   ioaddr + IntrEnable);
1061
1062        np->chip_cmd = CmdStart|CmdTxOn|CmdRxOn|CmdNoTxPoll;
1063        if (np->mii_if.force_media)
1064                np->chip_cmd |= CmdFDuplex;
1065        writew(np->chip_cmd, ioaddr + ChipCmd);
1066
1067        via_rhine_check_duplex(dev);
1068
1069        /* The LED outputs of various MII xcvrs should be configured.  */
1070        /* For NS or Mison phys, turn on bit 1 in register 0x17 */
1071        /* For ESI phys, turn on bit 7 in register 0x17. */
1072        mdio_write(dev, np->phys[0], 0x17, mdio_read(dev, np->phys[0], 0x17) |
1073                           (np->drv_flags & HasESIPhy) ? 0x0080 : 0x0001);
1074}
1075/* Read and write over the MII Management Data I/O (MDIO) interface. */
1076
1077static int mdio_read(struct net_device *dev, int phy_id, int regnum)
1078{
1079        long ioaddr = dev->base_addr;
1080        int boguscnt = 1024;
1081
1082        /* Wait for a previous command to complete. */
1083        while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
1084                ;
1085        writeb(0x00, ioaddr + MIICmd);
1086        writeb(phy_id, ioaddr + MIIPhyAddr);
1087        writeb(regnum, ioaddr + MIIRegAddr);
1088        writeb(0x40, ioaddr + MIICmd);                  /* Trigger read */
1089        boguscnt = 1024;
1090        while ((readb(ioaddr + MIICmd) & 0x40) && --boguscnt > 0)
1091                ;
1092        return readw(ioaddr + MIIData);
1093}
1094
1095static void mdio_write(struct net_device *dev, int phy_id, int regnum, int value)
1096{
1097        struct netdev_private *np = dev->priv;
1098        long ioaddr = dev->base_addr;
1099        int boguscnt = 1024;
1100
1101        if (phy_id == np->phys[0]) {
1102                switch (regnum) {
1103                case MII_BMCR:                                  /* Is user forcing speed/duplex? */
1104                        if (value & 0x9000)                     /* Autonegotiation. */
1105                                np->mii_if.force_media = 0;
1106                        else
1107                                np->mii_if.full_duplex = (value & 0x0100) ? 1 : 0;
1108                        break;
1109                case MII_ADVERTISE:
1110                        np->mii_if.advertising = value;
1111                        break;
1112                }
1113        }
1114
1115        /* Wait for a previous command to complete. */
1116        while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
1117                ;
1118        writeb(0x00, ioaddr + MIICmd);
1119        writeb(phy_id, ioaddr + MIIPhyAddr);
1120        writeb(regnum, ioaddr + MIIRegAddr);
1121        writew(value, ioaddr + MIIData);
1122        writeb(0x20, ioaddr + MIICmd);                  /* Trigger write. */
1123}
1124
1125
1126static int via_rhine_open(struct net_device *dev)
1127{
1128        struct netdev_private *np = dev->priv;
1129        long ioaddr = dev->base_addr;
1130        int i;
1131
1132        /* Reset the chip. */
1133        writew(CmdReset, ioaddr + ChipCmd);
1134
1135        i = request_irq(np->pdev->irq, &via_rhine_interrupt, SA_SHIRQ, dev->name, dev);
1136        if (i)
1137                return i;
1138
1139        if (debug > 1)
1140                printk(KERN_DEBUG "%s: via_rhine_open() irq %d.\n",
1141                           dev->name, np->pdev->irq);
1142        
1143        i = alloc_ring(dev);
1144        if (i)
1145                return i;
1146        alloc_rbufs(dev);
1147        alloc_tbufs(dev);
1148        wait_for_reset(dev, np->chip_id, dev->name);
1149        init_registers(dev);
1150        if (debug > 2)
1151                printk(KERN_DEBUG "%s: Done via_rhine_open(), status %4.4x "
1152                           "MII status: %4.4x.\n",
1153                           dev->name, readw(ioaddr + ChipCmd),
1154                           mdio_read(dev, np->phys[0], MII_BMSR));
1155
1156        netif_start_queue(dev);
1157
1158        /* Set the timer to check for link beat. */
1159        init_timer(&np->timer);
1160        np->timer.expires = jiffies + 2 * HZ/100;
1161        np->timer.data = (unsigned long)dev;
1162        np->timer.function = &via_rhine_timer;                          /* timer handler */
1163        add_timer(&np->timer);
1164
1165        return 0;
1166}
1167
1168static void via_rhine_check_duplex(struct net_device *dev)
1169{
1170        struct netdev_private *np = dev->priv;
1171        long ioaddr = dev->base_addr;
1172        int mii_lpa = mdio_read(dev, np->phys[0], MII_LPA);
1173        int negotiated = mii_lpa & np->mii_if.advertising;
1174        int duplex;
1175
1176        if (np->mii_if.force_media  ||  mii_lpa == 0xffff)
1177                return;
1178        duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
1179        if (np->mii_if.full_duplex != duplex) {
1180                np->mii_if.full_duplex = duplex;
1181                if (debug)
1182                        printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
1183                                   " partner capability of %4.4x.\n", dev->name,
1184                                   duplex ? "full" : "half", np->phys[0], mii_lpa);
1185                if (duplex)
1186                        np->chip_cmd |= CmdFDuplex;
1187                else
1188                        np->chip_cmd &= ~CmdFDuplex;
1189                writew(np->chip_cmd, ioaddr + ChipCmd);
1190        }
1191}
1192
1193
1194static void via_rhine_timer(unsigned long data)
1195{
1196        struct net_device *dev = (struct net_device *)data;
1197        struct netdev_private *np = dev->priv;
1198        long ioaddr = dev->base_addr;
1199        int next_tick = 10*HZ;
1200        int mii_status;
1201
1202        if (debug > 3) {
1203                printk(KERN_DEBUG "%s: VIA Rhine monitor tick, status %4.4x.\n",
1204                           dev->name, readw(ioaddr + IntrStatus));
1205        }
1206
1207        spin_lock_irq (&np->lock);
1208
1209        via_rhine_check_duplex(dev);
1210
1211        /* make IFF_RUNNING follow the MII status bit "Link established" */
1212        mii_status = mdio_read(dev, np->phys[0], MII_BMSR);
1213        if ( (mii_status & BMSR_LSTATUS) != (np->mii_status & BMSR_LSTATUS) ) {
1214                if (mii_status & BMSR_LSTATUS)
1215                        netif_carrier_on(dev);
1216                else
1217                        netif_carrier_off(dev);
1218        }
1219        np->mii_status = mii_status;
1220
1221        spin_unlock_irq (&np->lock);
1222
1223        np->timer.expires = jiffies + next_tick;
1224        add_timer(&np->timer);
1225}
1226
1227
1228static void via_rhine_tx_timeout (struct net_device *dev)
1229{
1230        struct netdev_private *np = dev->priv;
1231        long ioaddr = dev->base_addr;
1232
1233        printk (KERN_WARNING "%s: Transmit timed out, status %4.4x, PHY status "
1234                "%4.4x, resetting...\n",
1235                dev->name, readw (ioaddr + IntrStatus),
1236                mdio_read (dev, np->phys[0], MII_BMSR));
1237
1238        dev->if_port = 0;
1239
1240        /* protect against concurrent rx interrupts */
1241        disable_irq(np->pdev->irq);
1242
1243        spin_lock(&np->lock);
1244
1245        /* Reset the chip. */
1246        writew(CmdReset, ioaddr + ChipCmd);
1247
1248        /* clear all descriptors */
1249        free_tbufs(dev);
1250        free_rbufs(dev);
1251        alloc_tbufs(dev);
1252        alloc_rbufs(dev);
1253
1254        /* Reinitialize the hardware. */
1255        wait_for_reset(dev, np->chip_id, dev->name);
1256        init_registers(dev);
1257        
1258        spin_unlock(&np->lock);
1259        enable_irq(np->pdev->irq);
1260
1261        dev->trans_start = jiffies;
1262        np->stats.tx_errors++;
1263        netif_wake_queue(dev);
1264}
1265
1266static int via_rhine_start_tx(struct sk_buff *skb, struct net_device *dev)
1267{
1268        struct netdev_private *np = dev->priv;
1269        unsigned entry;
1270        u32 intr_status;
1271
1272        /* Caution: the write order is important here, set the field
1273           with the "ownership" bits last. */
1274
1275        /* Calculate the next Tx descriptor entry. */
1276        entry = np->cur_tx % TX_RING_SIZE;
1277
1278        if (skb->len < ETH_ZLEN) {
1279                skb = skb_padto(skb, ETH_ZLEN);
1280                if (skb == NULL)
1281                        return 0;
1282        }
1283
1284        np->tx_skbuff[entry] = skb;
1285
1286        if ((np->drv_flags & ReqTxAlign) &&
1287                (((long)skb->data & 3) || skb_shinfo(skb)->nr_frags != 0 || skb->ip_summed == CHECKSUM_HW)
1288                ) {
1289                /* Must use alignment buffer. */
1290                if (skb->len > PKT_BUF_SZ) {
1291                        /* packet too long, drop it */
1292                        dev_kfree_skb(skb);
1293                        np->tx_skbuff[entry] = NULL;
1294                        np->stats.tx_dropped++;
1295                        return 0;
1296                }
1297                skb_copy_and_csum_dev(skb, np->tx_buf[entry]);
1298                np->tx_skbuff_dma[entry] = 0;
1299                np->tx_ring[entry].addr = cpu_to_le32(np->tx_bufs_dma +
1300                                                                                  (np->tx_buf[entry] - np->tx_bufs));
1301        } else {
1302                np->tx_skbuff_dma[entry] =
1303                        pci_map_single(np->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1304                np->tx_ring[entry].addr = cpu_to_le32(np->tx_skbuff_dma[entry]);
1305        }
1306
1307        np->tx_ring[entry].desc_length = 
1308                cpu_to_le32(TXDESC | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
1309
1310        /* lock eth irq */
1311        spin_lock_irq (&np->lock);
1312        wmb();
1313        np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1314        wmb();
1315
1316        np->cur_tx++;
1317
1318        /* Non-x86 Todo: explicitly flush cache lines here. */
1319
1320        /*
1321         * Wake the potentially-idle transmit channel unless errors are
1322         * pending (the ISR must sort them out first).
1323         */
1324        intr_status = get_intr_status(dev);
1325        if ((intr_status & IntrTxErrSummary) == 0) {
1326                writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
1327        }
1328        IOSYNC;
1329
1330        if (np->cur_tx == np->dirty_tx + TX_QUEUE_LEN)
1331                netif_stop_queue(dev);
1332
1333        dev->trans_start = jiffies;
1334
1335        spin_unlock_irq (&np->lock);
1336
1337        if (debug > 4) {
1338                printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
1339                           dev->name, np->cur_tx-1, entry);
1340        }
1341        return 0;
1342}
1343
1344/* The interrupt handler does all of the Rx thread work and cleans up
1345   after the Tx thread. */
1346static irqreturn_t via_rhine_interrupt(int irq, void *dev_instance, struct pt_regs *rgs)
1347{
1348        struct net_device *dev = dev_instance;
1349        long ioaddr;
1350        u32 intr_status;
1351        int boguscnt = max_interrupt_work;
1352        int handled = 0;
1353
1354        ioaddr = dev->base_addr;
1355        
1356        while ((intr_status = get_intr_status(dev))) {
1357                handled = 1;
1358
1359                /* Acknowledge all of the current interrupt sources ASAP. */
1360                if (intr_status & IntrTxDescRace)
1361                        writeb(0x08, ioaddr + IntrStatus2);
1362                writew(intr_status & 0xffff, ioaddr + IntrStatus);
1363                IOSYNC;
1364
1365                if (debug > 4)
1366                        printk(KERN_DEBUG "%s: Interrupt, status %8.8x.\n",
1367                                   dev->name, intr_status);
1368
1369                if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
1370                                                   IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf))
1371                        via_rhine_rx(dev);
1372
1373                if (intr_status & (IntrTxErrSummary | IntrTxDone)) {
1374                        if (intr_status & IntrTxErrSummary) {
1375                                int cnt = 20;
1376                                /* Avoid scavenging before Tx engine turned off */
1377                                while ((readw(ioaddr+ChipCmd) & CmdTxOn) && --cnt)
1378                                        udelay(5);
1379                                if (debug > 2 && !cnt)
1380                                        printk(KERN_WARNING "%s: via_rhine_interrupt() "
1381                                                   "Tx engine still on.\n",
1382                                                   dev->name);
1383                        }
1384                        via_rhine_tx(dev);
1385                }
1386
1387                /* Abnormal error summary/uncommon events handlers. */
1388                if (intr_status & (IntrPCIErr | IntrLinkChange |
1389                                   IntrStatsMax | IntrTxError | IntrTxAborted |
1390                                   IntrTxUnderrun | IntrTxDescRace))
1391                        via_rhine_error(dev, intr_status);
1392
1393                if (--boguscnt < 0) {
1394                        printk(KERN_WARNING "%s: Too much work at interrupt, "
1395                                   "status=%#8.8x.\n",
1396                                   dev->name, intr_status);
1397                        break;
1398                }
1399        }
1400
1401        if (debug > 3)
1402                printk(KERN_DEBUG "%s: exiting interrupt, status=%8.8x.\n",
1403                           dev->name, readw(ioaddr + IntrStatus));
1404        return IRQ_RETVAL(handled);
1405}
1406
1407/* This routine is logically part of the interrupt handler, but isolated
1408   for clarity. */
1409static void via_rhine_tx(struct net_device *dev)
1410{
1411        struct netdev_private *np = dev->priv;
1412        int txstatus = 0, entry = np->dirty_tx % TX_RING_SIZE;
1413
1414        spin_lock (&np->lock);
1415
1416        /* find and cleanup dirty tx descriptors */
1417        while (np->dirty_tx != np->cur_tx) {
1418                txstatus = le32_to_cpu(np->tx_ring[entry].tx_status);
1419                if (debug > 6)
1420                        printk(KERN_DEBUG " Tx scavenge %d status %8.8x.\n",
1421                                   entry, txstatus);
1422                if (txstatus & DescOwn)
1423                        break;
1424                if (txstatus & 0x8000) {
1425                        if (debug > 1)
1426                                printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
1427                                           dev->name, txstatus);
1428                        np->stats.tx_errors++;
1429                        if (txstatus & 0x0400) np->stats.tx_carrier_errors++;
1430                        if (txstatus & 0x0200) np->stats.tx_window_errors++;
1431                        if (txstatus & 0x0100) np->stats.tx_aborted_errors++;
1432                        if (txstatus & 0x0080) np->stats.tx_heartbeat_errors++;
1433                        if (((np->chip_id == VT86C100A) && txstatus & 0x0002) ||
1434                                (txstatus & 0x0800) || (txstatus & 0x1000)) {
1435                                np->stats.tx_fifo_errors++;
1436                                np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1437                                break; /* Keep the skb - we try again */
1438                        }
1439                        /* Transmitter restarted in 'abnormal' handler. */
1440                } else {
1441                        if (np->chip_id == VT86C100A)
1442                                np->stats.collisions += (txstatus >> 3) & 0x0F;
1443                        else
1444                                np->stats.collisions += txstatus & 0x0F;
1445                        if (debug > 6)
1446                                printk(KERN_DEBUG "collisions: %1.1x:%1.1x\n",
1447                                        (txstatus >> 3) & 0xF,
1448                                        txstatus & 0xF);
1449                        np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1450                        np->stats.tx_packets++;
1451                }
1452                /* Free the original skb. */
1453                if (np->tx_skbuff_dma[entry]) {
1454                        pci_unmap_single(np->pdev,
1455                                                         np->tx_skbuff_dma[entry],
1456                                                         np->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
1457                }
1458                dev_kfree_skb_irq(np->tx_skbuff[entry]);
1459                np->tx_skbuff[entry] = NULL;
1460                entry = (++np->dirty_tx) % TX_RING_SIZE;
1461        }
1462        if ((np->cur_tx - np->dirty_tx) < TX_QUEUE_LEN - 4)
1463                netif_wake_queue (dev);
1464
1465        spin_unlock (&np->lock);
1466}
1467
1468/* This routine is logically part of the interrupt handler, but isolated
1469   for clarity and better register allocation. */
1470static void via_rhine_rx(struct net_device *dev)
1471{
1472        struct netdev_private *np = dev->priv;
1473        int entry = np->cur_rx % RX_RING_SIZE;
1474        int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1475
1476        if (debug > 4) {
1477                printk(KERN_DEBUG "%s: via_rhine_rx(), entry %d status %8.8x.\n",
1478                           dev->name, entry, le32_to_cpu(np->rx_head_desc->rx_status));
1479        }
1480
1481        /* If EOP is set on the next entry, it's a new packet. Send it up. */
1482        while ( ! (np->rx_head_desc->rx_status & cpu_to_le32(DescOwn))) {
1483                struct rx_desc *desc = np->rx_head_desc;
1484                u32 desc_status = le32_to_cpu(desc->rx_status);
1485                int data_size = desc_status >> 16;
1486
1487                if (debug > 4)
1488                        printk(KERN_DEBUG "  via_rhine_rx() status is %8.8x.\n",
1489                                   desc_status);
1490                if (--boguscnt < 0)
1491                        break;
1492                if ( (desc_status & (RxWholePkt | RxErr)) !=  RxWholePkt) {
1493                        if ((desc_status & RxWholePkt) !=  RxWholePkt) {
1494                                printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
1495                                           "multiple buffers, entry %#x length %d status %8.8x!\n",
1496                                           dev->name, entry, data_size, desc_status);
1497                                printk(KERN_WARNING "%s: Oversized Ethernet frame %p vs %p.\n",
1498                                           dev->name, np->rx_head_desc, &np->rx_ring[entry]);
1499                                np->stats.rx_length_errors++;
1500                        } else if (desc_status & RxErr) {
1501                                /* There was a error. */
1502                                if (debug > 2)
1503                                        printk(KERN_DEBUG "  via_rhine_rx() Rx error was %8.8x.\n",
1504                                                   desc_status);
1505                                np->stats.rx_errors++;
1506                                if (desc_status & 0x0030) np->stats.rx_length_errors++;
1507                                if (desc_status & 0x0048) np->stats.rx_fifo_errors++;
1508                                if (desc_status & 0x0004) np->stats.rx_frame_errors++;
1509                                if (desc_status & 0x0002) {
1510                                        /* this can also be updated outside the interrupt handler */
1511                                        spin_lock (&np->lock);
1512                                        np->stats.rx_crc_errors++;
1513                                        spin_unlock (&np->lock);
1514                                }
1515                        }
1516                } else {
1517                        struct sk_buff *skb;
1518                        /* Length should omit the CRC */
1519                        int pkt_len = data_size - 4;
1520
1521                        /* Check if the packet is long enough to accept without copying
1522                           to a minimally-sized skbuff. */
1523                        if (pkt_len < rx_copybreak &&
1524                                (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1525                                skb->dev = dev;
1526                                skb_reserve(skb, 2);    /* 16 byte align the IP header */
1527                                pci_dma_sync_single(np->pdev, np->rx_skbuff_dma[entry],
1528                                                    np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1529
1530                                /* *_IP_COPYSUM isn't defined anywhere and eth_copy_and_sum
1531                                   is memcpy for all archs so this is kind of pointless right
1532                                   now ... or? */
1533#if HAS_IP_COPYSUM                     /* Call copy + cksum if available. */
1534                                eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0);
1535                                skb_put(skb, pkt_len);
1536#else
1537                                memcpy(skb_put(skb, pkt_len), np->rx_skbuff[entry]->tail,
1538                                           pkt_len);
1539#endif
1540                        } else {
1541                                skb = np->rx_skbuff[entry];
1542                                if (skb == NULL) {
1543                                        printk(KERN_ERR "%s: Inconsistent Rx descriptor chain.\n",
1544                                                   dev->name);
1545                                        break;
1546                                }
1547                                np->rx_skbuff[entry] = NULL;
1548                                skb_put(skb, pkt_len);
1549                                pci_unmap_single(np->pdev, np->rx_skbuff_dma[entry],
1550                                                                 np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1551                        }
1552                        skb->protocol = eth_type_trans(skb, dev);
1553                        netif_rx(skb);
1554                        dev->last_rx = jiffies;
1555                        np->stats.rx_bytes += pkt_len;
1556                        np->stats.rx_packets++;
1557                }
1558                entry = (++np->cur_rx) % RX_RING_SIZE;
1559                np->rx_head_desc = &np->rx_ring[entry];
1560        }
1561
1562        /* Refill the Rx ring buffers. */
1563        for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1564                struct sk_buff *skb;
1565                entry = np->dirty_rx % RX_RING_SIZE;
1566                if (np->rx_skbuff[entry] == NULL) {
1567                        skb = dev_alloc_skb(np->rx_buf_sz);
1568                        np->rx_skbuff[entry] = skb;
1569                        if (skb == NULL)
1570                                break;                  /* Better luck next round. */
1571                        skb->dev = dev;                 /* Mark as being used by this device. */
1572                        np->rx_skbuff_dma[entry] =
1573                                pci_map_single(np->pdev, skb->tail, np->rx_buf_sz, 
1574                                                           PCI_DMA_FROMDEVICE);
1575                        np->rx_ring[entry].addr = cpu_to_le32(np->rx_skbuff_dma[entry]);
1576                }
1577                np->rx_ring[entry].rx_status = cpu_to_le32(DescOwn);
1578        }
1579
1580        /* Pre-emptively restart Rx engine. */
1581        writew(readw(dev->base_addr + ChipCmd) | CmdRxOn | CmdRxDemand,
1582                   dev->base_addr + ChipCmd);
1583}
1584
1585/* Clears the "tally counters" for CRC errors and missed frames(?).
1586   It has been reported that some chips need a write of 0 to clear
1587   these, for others the counters are set to 1 when written to and
1588   instead cleared when read. So we clear them both ways ... */
1589static inline void clear_tally_counters(const long ioaddr)
1590{
1591        writel(0, ioaddr + RxMissed);
1592        readw(ioaddr + RxCRCErrs);
1593        readw(ioaddr + RxMissed);
1594}
1595
1596static void via_rhine_restart_tx(struct net_device *dev) {
1597        struct netdev_private *np = dev->priv;
1598        long ioaddr = dev->base_addr;
1599        int entry = np->dirty_tx % TX_RING_SIZE;
1600        u32 intr_status;
1601
1602        /*
1603         * If new errors occured, we need to sort them out before doing Tx.
1604         * In that case the ISR will be back here RSN anyway.
1605         */
1606        intr_status = get_intr_status(dev);
1607
1608        if ((intr_status & IntrTxErrSummary) == 0) {
1609
1610                /* We know better than the chip where it should continue. */
1611                writel(np->tx_ring_dma + entry * sizeof(struct tx_desc),
1612                           ioaddr + TxRingPtr);
1613
1614                writew(CmdTxDemand | np->chip_cmd, ioaddr + ChipCmd);
1615                IOSYNC;
1616        }
1617        else {
1618                /* This should never happen */
1619                if (debug > 1)
1620                        printk(KERN_WARNING "%s: via_rhine_restart_tx() "
1621                                   "Another error occured %8.8x.\n",
1622                                   dev->name, intr_status);
1623        }
1624
1625}
1626
1627static void via_rhine_error(struct net_device *dev, int intr_status)
1628{
1629        struct netdev_private *np = dev->priv;
1630        long ioaddr = dev->base_addr;
1631
1632        spin_lock (&np->lock);
1633
1634        if (intr_status & (IntrLinkChange)) {
1635                if (readb(ioaddr + MIIStatus) & 0x02) {
1636                        /* Link failed, restart autonegotiation. */
1637                        if (np->drv_flags & HasDavicomPhy)
1638                                mdio_write(dev, np->phys[0], MII_BMCR, 0x3300);
1639                } else
1640                        via_rhine_check_duplex(dev);
1641                if (debug)
1642                        printk(KERN_ERR "%s: MII status changed: Autonegotiation "
1643                                   "advertising %4.4x  partner %4.4x.\n", dev->name,
1644                           mdio_read(dev, np->phys[0], MII_ADVERTISE),
1645                           mdio_read(dev, np->phys[0], MII_LPA));
1646        }
1647        if (intr_status & IntrStatsMax) {
1648                np->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
1649                np->stats.rx_missed_errors      += readw(ioaddr + RxMissed);
1650                clear_tally_counters(ioaddr);
1651        }
1652        if (intr_status & IntrTxAborted) {
1653                if (debug > 1)
1654                        printk(KERN_INFO "%s: Abort %8.8x, frame dropped.\n",
1655                                   dev->name, intr_status);
1656        }
1657        if (intr_status & IntrTxUnderrun) {
1658                if (np->tx_thresh < 0xE0)
1659                        writeb(np->tx_thresh += 0x20, ioaddr + TxConfig);
1660                if (debug > 1)
1661                        printk(KERN_INFO "%s: Transmitter underrun, Tx "
1662                                   "threshold now %2.2x.\n",
1663                                   dev->name, np->tx_thresh);
1664        }
1665        if (intr_status & IntrTxDescRace) {
1666                if (debug > 2)
1667                        printk(KERN_INFO "%s: Tx descriptor write-back race.\n",
1668                                   dev->name);
1669        }
1670        if ((intr_status & IntrTxError) && ~( IntrTxAborted | IntrTxUnderrun |
1671                                                                                   IntrTxDescRace )) {
1672                if (np->tx_thresh < 0xE0) {
1673                        writeb(np->tx_thresh += 0x20, ioaddr + TxConfig);
1674                }
1675                if (debug > 1)
1676                        printk(KERN_INFO "%s: Unspecified error. Tx "
1677                                   "threshold now %2.2x.\n",
1678                                   dev->name, np->tx_thresh);
1679        }
1680        if (intr_status & ( IntrTxAborted | IntrTxUnderrun | IntrTxDescRace |
1681                                                IntrTxError ))
1682                via_rhine_restart_tx(dev);
1683
1684        if (intr_status & ~( IntrLinkChange | IntrStatsMax | IntrTxUnderrun |
1685                                                 IntrTxError | IntrTxAborted | IntrNormalSummary |
1686                                                 IntrTxDescRace )) {
1687                if (debug > 1)
1688                        printk(KERN_ERR "%s: Something Wicked happened! %8.8x.\n",
1689                                   dev->name, intr_status);
1690        }
1691
1692        spin_unlock (&np->lock);
1693}
1694
1695static struct net_device_stats *via_rhine_get_stats(struct net_device *dev)
1696{
1697        struct netdev_private *np = dev->priv;
1698        long ioaddr = dev->base_addr;
1699        unsigned long flags;
1700
1701        spin_lock_irqsave(&np->lock, flags);
1702        np->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
1703        np->stats.rx_missed_errors      += readw(ioaddr + RxMissed);
1704        clear_tally_counters(ioaddr);
1705        spin_unlock_irqrestore(&np->lock, flags);
1706
1707        return &np->stats;
1708}
1709
1710static void via_rhine_set_rx_mode(struct net_device *dev)
1711{
1712        struct netdev_private *np = dev->priv;
1713        long ioaddr = dev->base_addr;
1714        u32 mc_filter[2];                       /* Multicast hash filter */
1715        u8 rx_mode;                                     /* Note: 0x02=accept runt, 0x01=accept errs */
1716
1717        if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
1718                /* Unconditionally log net taps. */
1719                printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1720                rx_mode = 0x1C;
1721                writel(0xffffffff, ioaddr + MulticastFilter0);
1722                writel(0xffffffff, ioaddr + MulticastFilter1);
1723        } else if ((dev->mc_count > multicast_filter_limit)
1724                           ||  (dev->flags & IFF_ALLMULTI)) {
1725                /* Too many to match, or accept all multicasts. */
1726                writel(0xffffffff, ioaddr + MulticastFilter0);
1727                writel(0xffffffff, ioaddr + MulticastFilter1);
1728                rx_mode = 0x0C;
1729        } else {
1730                struct dev_mc_list *mclist;
1731                int i;
1732                memset(mc_filter, 0, sizeof(mc_filter));
1733                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1734                         i++, mclist = mclist->next) {
1735                        int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
1736
1737                        mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
1738                }
1739                writel(mc_filter[0], ioaddr + MulticastFilter0);
1740                writel(mc_filter[1], ioaddr + MulticastFilter1);
1741                rx_mode = 0x0C;
1742        }
1743        writeb(np->rx_thresh | rx_mode, ioaddr + RxConfig);
1744}
1745
1746static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1747{
1748        struct netdev_private *np = dev->priv;
1749
1750        strcpy (info->driver, DRV_NAME);
1751        strcpy (info->version, DRV_VERSION);
1752        strcpy (info->bus_info, pci_name(np->pdev));
1753}
1754
1755static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1756{
1757        struct netdev_private *np = dev->priv;
1758        int rc;
1759
1760        if (!(np->drv_flags & CanHaveMII))
1761                return -EINVAL;
1762
1763        spin_lock_irq(&np->lock);
1764        rc = mii_ethtool_gset(&np->mii_if, cmd);
1765        spin_unlock_irq(&np->lock);
1766
1767        return rc;
1768}
1769
1770static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1771{
1772        struct netdev_private *np = dev->priv;
1773        int rc;
1774
1775        if (!(np->drv_flags & CanHaveMII))
1776                return -EINVAL;
1777
1778        spin_lock_irq(&np->lock);
1779        rc = mii_ethtool_sset(&np->mii_if, cmd);
1780        spin_unlock_irq(&np->lock);
1781
1782        return rc;
1783}
1784
1785static int netdev_nway_reset(struct net_device *dev)
1786{
1787        struct netdev_private *np = dev->priv;
1788
1789        if (!(np->drv_flags & CanHaveMII))
1790                return -EINVAL;
1791
1792        return mii_nway_restart(&np->mii_if);
1793}
1794
1795static u32 netdev_get_link(struct net_device *dev)
1796{
1797        struct netdev_private *np = dev->priv;
1798
1799        if (!(np->drv_flags & CanHaveMII))
1800                return 0;       /* -EINVAL */
1801
1802        return mii_link_ok(&np->mii_if);
1803}
1804
1805static u32 netdev_get_msglevel(struct net_device *dev)
1806{
1807        return debug;
1808}
1809
1810static void netdev_set_msglevel(struct net_device *dev, u32 value)
1811{
1812        debug = value;
1813}
1814
1815static struct ethtool_ops netdev_ethtool_ops = {
1816        .get_drvinfo            = netdev_get_drvinfo,
1817        .get_settings           = netdev_get_settings,
1818        .set_settings           = netdev_set_settings,
1819        .nway_reset             = netdev_nway_reset,
1820        .get_link               = netdev_get_link,
1821        .get_msglevel           = netdev_get_msglevel,
1822        .set_msglevel           = netdev_set_msglevel,
1823        .get_sg                 = ethtool_op_get_sg,
1824        .get_tx_csum            = ethtool_op_get_tx_csum,
1825};
1826
1827static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1828{
1829        struct netdev_private *np = dev->priv;
1830        struct mii_ioctl_data *data = (struct mii_ioctl_data *) & rq->ifr_data;
1831        int rc;
1832
1833        if (!netif_running(dev))
1834                return -EINVAL;
1835
1836        spin_lock_irq(&np->lock);
1837        rc = generic_mii_ioctl(&np->mii_if, data, cmd, NULL);
1838        spin_unlock_irq(&np->lock);
1839
1840        return rc;
1841}
1842
1843static int via_rhine_close(struct net_device *dev)
1844{
1845        long ioaddr = dev->base_addr;
1846        struct netdev_private *np = dev->priv;
1847
1848        del_timer_sync(&np->timer);
1849
1850        spin_lock_irq(&np->lock);
1851
1852        netif_stop_queue(dev);
1853
1854        if (debug > 1)
1855                printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.\n",
1856                           dev->name, readw(ioaddr + ChipCmd));
1857
1858        /* Switch to loopback mode to avoid hardware races. */
1859        writeb(np->tx_thresh | 0x02, ioaddr + TxConfig);
1860
1861        /* Disable interrupts by clearing the interrupt mask. */
1862        writew(0x0000, ioaddr + IntrEnable);
1863
1864        /* Stop the chip's Tx and Rx processes. */
1865        writew(CmdStop, ioaddr + ChipCmd);
1866
1867        spin_unlock_irq(&np->lock);
1868
1869        free_irq(np->pdev->irq, dev);
1870        free_rbufs(dev);
1871        free_tbufs(dev);
1872        free_ring(dev);
1873
1874        return 0;
1875}
1876
1877
1878static void __devexit via_rhine_remove_one (struct pci_dev *pdev)
1879{
1880        struct net_device *dev = pci_get_drvdata(pdev);
1881        
1882        unregister_netdev(dev);
1883
1884        pci_release_regions(pdev);
1885
1886#ifdef USE_MEM
1887        iounmap((char *)(dev->base_addr));
1888#endif
1889
1890        free_netdev(dev);
1891        pci_disable_device(pdev);
1892        pci_set_drvdata(pdev, NULL);
1893}
1894
1895
1896static struct pci_driver via_rhine_driver = {
1897        .name           = "via-rhine",
1898        .id_table       = via_rhine_pci_tbl,
1899        .probe          = via_rhine_init_one,
1900        .remove         = __devexit_p(via_rhine_remove_one),
1901};
1902
1903
1904static int __init via_rhine_init (void)
1905{
1906/* when a module, this is printed whether or not devices are found in probe */
1907#ifdef MODULE
1908        printk(version);
1909#endif
1910        return pci_module_init (&via_rhine_driver);
1911}
1912
1913
1914static void __exit via_rhine_cleanup (void)
1915{
1916        pci_unregister_driver (&via_rhine_driver);
1917}
1918
1919
1920module_init(via_rhine_init);
1921module_exit(via_rhine_cleanup);
1922
1923
1924/*
1925 * Local variables:
1926 *  compile-command: "gcc -DMODULE -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -c via-rhine.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
1927 *  c-indent-level: 4
1928 *  c-basic-offset: 4
1929 *  tab-width: 4
1930 * End:
1931 */
1932
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.