linux-bk/drivers/net/starfire.c
<<
>>
Prefs
   1/* starfire.c: Linux device driver for the Adaptec Starfire network adapter. */
   2/*
   3        Written 1998-2000 by Donald Becker.
   4
   5        Current maintainer is Ion Badulescu <ionut@cs.columbia.edu>. Please
   6        send all bug reports to me, and not to Donald Becker, as this code
   7        has been heavily modified from Donald's original version.
   8
   9        This software may be used and distributed according to the terms of
  10        the GNU General Public License (GPL), incorporated herein by reference.
  11        Drivers based on or derived from this code fall under the GPL and must
  12        retain the authorship, copyright and license notice.  This file is not
  13        a complete program and may only be used when the entire operating
  14        system is licensed under the GPL.
  15
  16        The information below comes from Donald Becker's original driver:
  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        Support and updates available at
  24        http://www.scyld.com/network/starfire.html
  25
  26        -----------------------------------------------------------
  27
  28        Linux kernel-specific changes:
  29
  30        LK1.1.1 (jgarzik):
  31        - Use PCI driver interface
  32        - Fix MOD_xxx races
  33        - softnet fixups
  34
  35        LK1.1.2 (jgarzik):
  36        - Merge Becker version 0.15
  37
  38        LK1.1.3 (Andrew Morton)
  39        - Timer cleanups
  40
  41        LK1.1.4 (jgarzik):
  42        - Merge Becker version 1.03
  43
  44        LK1.2.1 (Ion Badulescu <ionut@cs.columbia.edu>)
  45        - Support hardware Rx/Tx checksumming
  46        - Use the GFP firmware taken from Adaptec's Netware driver
  47
  48        LK1.2.2 (Ion Badulescu)
  49        - Backported to 2.2.x
  50
  51        LK1.2.3 (Ion Badulescu)
  52        - Fix the flaky mdio interface
  53        - More compat clean-ups
  54
  55        LK1.2.4 (Ion Badulescu)
  56        - More 2.2.x initialization fixes
  57
  58        LK1.2.5 (Ion Badulescu)
  59        - Several fixes from Manfred Spraul
  60
  61        LK1.2.6 (Ion Badulescu)
  62        - Fixed ifup/ifdown/ifup problem in 2.4.x
  63
  64        LK1.2.7 (Ion Badulescu)
  65        - Removed unused code
  66        - Made more functions static and __init
  67
  68        LK1.2.8 (Ion Badulescu)
  69        - Quell bogus error messages, inform about the Tx threshold
  70        - Removed #ifdef CONFIG_PCI, this driver is PCI only
  71
  72        LK1.2.9 (Ion Badulescu)
  73        - Merged Jeff Garzik's changes from 2.4.4-pre5
  74        - Added 2.2.x compatibility stuff required by the above changes
  75
  76        LK1.2.9a (Ion Badulescu)
  77        - More updates from Jeff Garzik
  78
  79        LK1.3.0 (Ion Badulescu)
  80        - Merged zerocopy support
  81
  82        LK1.3.1 (Ion Badulescu)
  83        - Added ethtool support
  84        - Added GPIO (media change) interrupt support
  85
  86        LK1.3.2 (Ion Badulescu)
  87        - Fixed 2.2.x compatibility issues introduced in 1.3.1
  88        - Fixed ethtool ioctl returning uninitialized memory
  89
  90        LK1.3.3 (Ion Badulescu)
  91        - Initialize the TxMode register properly
  92        - Don't dereference dev->priv after freeing it
  93
  94        LK1.3.4 (Ion Badulescu)
  95        - Fixed initialization timing problems
  96        - Fixed interrupt mask definitions
  97
  98        LK1.3.5 (jgarzik)
  99        - ethtool NWAY_RST, GLINK, [GS]MSGLVL support
 100
 101        LK1.3.6:
 102        - Sparc64 support and fixes (Ion Badulescu)
 103        - Better stats and error handling (Ion Badulescu)
 104        - Use new pci_set_mwi() PCI API function (jgarzik)
 105
 106        LK1.3.7 (Ion Badulescu)
 107        - minimal implementation of tx_timeout()
 108        - correctly shutdown the Rx/Tx engines in netdev_close()
 109        - added calls to netif_carrier_on/off
 110        (patch from Stefan Rompf <srompf@isg.de>)
 111        - VLAN support
 112
 113        LK1.3.8 (Ion Badulescu)
 114        - adjust DMA burst size on sparc64
 115        - 64-bit support
 116        - reworked zerocopy support for 64-bit buffers
 117        - working and usable interrupt mitigation/latency
 118        - reduced Tx interrupt frequency for lower interrupt overhead
 119
 120        LK1.3.9 (Ion Badulescu)
 121        - bugfix for mcast filter
 122        - enable the right kind of Tx interrupts (TxDMADone, not TxDone)
 123
 124        LK1.4.0 (Ion Badulescu)
 125        - NAPI support
 126
 127        LK1.4.1 (Ion Badulescu)
 128        - flush PCI posting buffers after disabling Rx interrupts
 129        - put the chip to a D3 slumber on driver unload
 130        - added config option to enable/disable NAPI
 131
 132TODO:   bugfixes (no bugs known as of right now)
 133*/
 134
 135#define DRV_NAME        "starfire"
 136#define DRV_VERSION     "1.03+LK1.4.1"
 137#define DRV_RELDATE     "February 10, 2002"
 138
 139#include <linux/config.h>
 140#include <linux/version.h>
 141#include <linux/module.h>
 142#include <asm/io.h>
 143#include <linux/kernel.h>
 144#include <linux/pci.h>
 145#include <linux/netdevice.h>
 146#include <linux/etherdevice.h>
 147#include <linux/init.h>
 148#include <linux/delay.h>
 149#include <asm/processor.h>              /* Processor type for cache alignment. */
 150#include <asm/uaccess.h>
 151#include <asm/io.h>
 152
 153/*
 154 * Adaptec's license for their drivers (which is where I got the
 155 * firmware files) does not allow one to redistribute them. Thus, we can't
 156 * include the firmware with this driver.
 157 *
 158 * However, should a legal-to-distribute firmware become available,
 159 * the driver developer would need only to obtain the firmware in the
 160 * form of a C header file.
 161 * Once that's done, the #undef below must be changed into a #define
 162 * for this driver to really use the firmware. Note that Rx/Tx
 163 * hardware TCP checksumming is not possible without the firmware.
 164 *
 165 * WANTED: legal firmware to include with this GPL'd driver.
 166 */
 167#undef HAS_FIRMWARE
 168/*
 169 * The current frame processor firmware fails to checksum a fragment
 170 * of length 1. If and when this is fixed, the #define below can be removed.
 171 */
 172#define HAS_BROKEN_FIRMWARE
 173/*
 174 * Define this if using the driver with the zero-copy patch
 175 */
 176#if defined(HAS_FIRMWARE) && defined(MAX_SKB_FRAGS)
 177#define ZEROCOPY
 178#endif
 179
 180#ifdef HAS_FIRMWARE
 181#include "starfire_firmware.h"
 182#endif /* HAS_FIRMWARE */
 183
 184#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
 185#define VLAN_SUPPORT
 186#endif
 187
 188#ifndef CONFIG_ADAPTEC_STARFIRE_NAPI
 189#undef HAVE_NETDEV_POLL
 190#endif
 191
 192/* The user-configurable values.
 193   These may be modified when a driver module is loaded.*/
 194
 195/* Used for tuning interrupt latency vs. overhead. */
 196static int intr_latency;
 197static int small_frames;
 198
 199static int debug = 1;                   /* 1 normal messages, 0 quiet .. 7 verbose. */
 200static int max_interrupt_work = 20;
 201static int mtu;
 202/* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
 203   The Starfire has a 512 element hash table based on the Ethernet CRC. */
 204static int multicast_filter_limit = 512;
 205/* Whether to do TCP/UDP checksums in hardware */
 206#ifdef HAS_FIRMWARE
 207static int enable_hw_cksum = 1;
 208#else
 209static int enable_hw_cksum = 0;
 210#endif
 211
 212#define PKT_BUF_SZ      1536            /* Size of each temporary Rx buffer.*/
 213/*
 214 * Set the copy breakpoint for the copy-only-tiny-frames scheme.
 215 * Setting to > 1518 effectively disables this feature.
 216 *
 217 * NOTE:
 218 * The ia64 doesn't allow for unaligned loads even of integers being
 219 * misaligned on a 2 byte boundary. Thus always force copying of
 220 * packets as the starfire doesn't allow for misaligned DMAs ;-(
 221 * 23/10/2000 - Jes
 222 *
 223 * The Alpha and the Sparc don't like unaligned loads, either. On Sparc64,
 224 * at least, having unaligned frames leads to a rather serious performance
 225 * penalty. -Ion
 226 */
 227#if defined(__ia64__) || defined(__alpha__) || defined(__sparc__)
 228static int rx_copybreak = PKT_BUF_SZ;
 229#else
 230static int rx_copybreak /* = 0 */;
 231#endif
 232
 233/* PCI DMA burst size -- on sparc64 we want to force it to 64 bytes, on the others the default of 128 is fine. */
 234#ifdef __sparc__
 235#define DMA_BURST_SIZE 64
 236#else
 237#define DMA_BURST_SIZE 128
 238#endif
 239
 240/* Used to pass the media type, etc.
 241   Both 'options[]' and 'full_duplex[]' exist for driver interoperability.
 242   The media type is usually passed in 'options[]'.
 243   These variables are deprecated, use ethtool instead. -Ion
 244*/
 245#define MAX_UNITS 8             /* More are supported, limit only on options */
 246static int options[MAX_UNITS] = {0, };
 247static int full_duplex[MAX_UNITS] = {0, };
 248
 249/* Operational parameters that are set at compile time. */
 250
 251/* The "native" ring sizes are either 256 or 2048.
 252   However in some modes a descriptor may be marked to wrap the ring earlier.
 253*/
 254#define RX_RING_SIZE    256
 255#define TX_RING_SIZE    32
 256/* The completion queues are fixed at 1024 entries i.e. 4K or 8KB. */
 257#define DONE_Q_SIZE     1024
 258/* All queues must be aligned on a 256-byte boundary */
 259#define QUEUE_ALIGN     256
 260
 261#if RX_RING_SIZE > 256
 262#define RX_Q_ENTRIES Rx2048QEntries
 263#else
 264#define RX_Q_ENTRIES Rx256QEntries
 265#endif
 266
 267/* Operational parameters that usually are not changed. */
 268/* Time in jiffies before concluding the transmitter is hung. */
 269#define TX_TIMEOUT      (2 * HZ)
 270
 271/*
 272 * This SUCKS.
 273 * We need a much better method to determine if dma_addr_t is 64-bit.
 274 */
 275#if (defined(__i386__) && defined(CONFIG_HIGHMEM) && (LINUX_VERSION_CODE > 0x20500 || defined(CONFIG_HIGHMEM64G))) || defined(__x86_64__) || defined (__ia64__) || defined(__mips64__) || (defined(__mips__) && defined(CONFIG_HIGHMEM) && defined(CONFIG_64BIT_PHYS_ADDR))
 276/* 64-bit dma_addr_t */
 277#define ADDR_64BITS     /* This chip uses 64 bit addresses. */
 278#define cpu_to_dma(x) cpu_to_le64(x)
 279#define dma_to_cpu(x) le64_to_cpu(x)
 280#define RX_DESC_Q_ADDR_SIZE RxDescQAddr64bit
 281#define TX_DESC_Q_ADDR_SIZE TxDescQAddr64bit
 282#define RX_COMPL_Q_ADDR_SIZE RxComplQAddr64bit
 283#define TX_COMPL_Q_ADDR_SIZE TxComplQAddr64bit
 284#define RX_DESC_ADDR_SIZE RxDescAddr64bit
 285#else  /* 32-bit dma_addr_t */
 286#define cpu_to_dma(x) cpu_to_le32(x)
 287#define dma_to_cpu(x) le32_to_cpu(x)
 288#define RX_DESC_Q_ADDR_SIZE RxDescQAddr32bit
 289#define TX_DESC_Q_ADDR_SIZE TxDescQAddr32bit
 290#define RX_COMPL_Q_ADDR_SIZE RxComplQAddr32bit
 291#define TX_COMPL_Q_ADDR_SIZE TxComplQAddr32bit
 292#define RX_DESC_ADDR_SIZE RxDescAddr32bit
 293#endif
 294
 295#ifdef MAX_SKB_FRAGS
 296#define skb_first_frag_len(skb) skb_headlen(skb)
 297#define skb_num_frags(skb) (skb_shinfo(skb)->nr_frags + 1)
 298#else  /* not MAX_SKB_FRAGS */
 299#define skb_first_frag_len(skb) (skb->len)
 300#define skb_num_frags(skb) 1
 301#endif /* not MAX_SKB_FRAGS */
 302
 303/* 2.2.x compatibility code */
 304#if LINUX_VERSION_CODE < 0x20300
 305
 306#include "starfire-kcomp22.h"
 307
 308#else  /* LINUX_VERSION_CODE > 0x20300 */
 309
 310#include <linux/crc32.h>
 311#include <linux/ethtool.h>
 312#include <linux/mii.h>
 313
 314#include <linux/if_vlan.h>
 315
 316#define COMPAT_MOD_INC_USE_COUNT
 317#define COMPAT_MOD_DEC_USE_COUNT
 318
 319#define init_tx_timer(dev, func, timeout) \
 320        dev->tx_timeout = func; \
 321        dev->watchdog_timeo = timeout;
 322#define kick_tx_timer(dev, func, timeout)
 323
 324#define netif_start_if(dev)
 325#define netif_stop_if(dev)
 326
 327#define PCI_SLOT_NAME(pci_dev)  pci_name(pci_dev)
 328
 329#endif /* LINUX_VERSION_CODE > 0x20300 */
 330
 331#ifdef HAVE_NETDEV_POLL
 332#define init_poll(dev) \
 333        dev->poll = &netdev_poll; \
 334        dev->weight = max_interrupt_work;
 335#define netdev_rx(dev, ioaddr) \
 336do { \
 337        u32 intr_enable; \
 338        if (netif_rx_schedule_prep(dev)) { \
 339                __netif_rx_schedule(dev); \
 340                intr_enable = readl(ioaddr + IntrEnable); \
 341                intr_enable &= ~(IntrRxDone | IntrRxEmpty); \
 342                writel(intr_enable, ioaddr + IntrEnable); \
 343                readl(ioaddr + IntrEnable); /* flush PCI posting buffers */ \
 344        } else { \
 345                /* Paranoia check */ \
 346                intr_enable = readl(ioaddr + IntrEnable); \
 347                if (intr_enable & (IntrRxDone | IntrRxEmpty)) { \
 348                        printk("%s: interrupt while in polling mode!\n", dev->name); \
 349                        intr_enable &= ~(IntrRxDone | IntrRxEmpty); \
 350                        writel(intr_enable, ioaddr + IntrEnable); \
 351                } \
 352        } \
 353} while (0)
 354#define netdev_receive_skb(skb) netif_receive_skb(skb)
 355#define vlan_netdev_receive_skb(skb, vlgrp, vlid) vlan_hwaccel_receive_skb(skb, vlgrp, vlid)
 356static int      netdev_poll(struct net_device *dev, int *budget);
 357#else  /* not HAVE_NETDEV_POLL */
 358#define init_poll(dev)
 359#define netdev_receive_skb(skb) netif_rx(skb)
 360#define vlan_netdev_receive_skb(skb, vlgrp, vlid) vlan_hwaccel_rx(skb, vlgrp, vlid)
 361#define netdev_rx(dev, ioaddr) \
 362do { \
 363        int quota = np->dirty_rx + RX_RING_SIZE - np->cur_rx; \
 364        __netdev_rx(dev, &quota);\
 365} while (0)
 366#endif /* not HAVE_NETDEV_POLL */
 367/* end of compatibility code */
 368
 369
 370/* These identify the driver base version and may not be removed. */
 371static char version[] __devinitdata =
 372KERN_INFO "starfire.c:v1.03 7/26/2000  Written by Donald Becker <becker@scyld.com>\n"
 373KERN_INFO " (unofficial 2.2/2.4 kernel port, version " DRV_VERSION ", " DRV_RELDATE ")\n";
 374
 375MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
 376MODULE_DESCRIPTION("Adaptec Starfire Ethernet driver");
 377MODULE_LICENSE("GPL");
 378
 379MODULE_PARM(max_interrupt_work, "i");
 380MODULE_PARM(mtu, "i");
 381MODULE_PARM(debug, "i");
 382MODULE_PARM(rx_copybreak, "i");
 383MODULE_PARM(intr_latency, "i");
 384MODULE_PARM(small_frames, "i");
 385MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
 386MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
 387MODULE_PARM(enable_hw_cksum, "i");
 388MODULE_PARM_DESC(max_interrupt_work, "Maximum events handled per interrupt");
 389MODULE_PARM_DESC(mtu, "MTU (all boards)");
 390MODULE_PARM_DESC(debug, "Debug level (0-6)");
 391MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
 392MODULE_PARM_DESC(intr_latency, "Maximum interrupt latency, in microseconds");
 393MODULE_PARM_DESC(small_frames, "Maximum size of receive frames that bypass interrupt latency (0,64,128,256,512)");
 394MODULE_PARM_DESC(options, "Deprecated: Bits 0-3: media type, bit 17: full duplex");
 395MODULE_PARM_DESC(full_duplex, "Deprecated: Forced full-duplex setting (0/1)");
 396MODULE_PARM_DESC(enable_hw_cksum, "Enable/disable hardware cksum support (0/1)");
 397
 398/*
 399                                Theory of Operation
 400
 401I. Board Compatibility
 402
 403This driver is for the Adaptec 6915 "Starfire" 64 bit PCI Ethernet adapter.
 404
 405II. Board-specific settings
 406
 407III. Driver operation
 408
 409IIIa. Ring buffers
 410
 411The Starfire hardware uses multiple fixed-size descriptor queues/rings.  The
 412ring sizes are set fixed by the hardware, but may optionally be wrapped
 413earlier by the END bit in the descriptor.
 414This driver uses that hardware queue size for the Rx ring, where a large
 415number of entries has no ill effect beyond increases the potential backlog.
 416The Tx ring is wrapped with the END bit, since a large hardware Tx queue
 417disables the queue layer priority ordering and we have no mechanism to
 418utilize the hardware two-level priority queue.  When modifying the
 419RX/TX_RING_SIZE pay close attention to page sizes and the ring-empty warning
 420levels.
 421
 422IIIb/c. Transmit/Receive Structure
 423
 424See the Adaptec manual for the many possible structures, and options for
 425each structure.  There are far too many to document all of them here.
 426
 427For transmit this driver uses type 0/1 transmit descriptors (depending
 428on the 32/64 bitness of the architecture), and relies on automatic
 429minimum-length padding.  It does not use the completion queue
 430consumer index, but instead checks for non-zero status entries.
 431
 432For receive this driver uses type 0/1/2/3 receive descriptors.  The driver
 433allocates full frame size skbuffs for the Rx ring buffers, so all frames
 434should fit in a single descriptor.  The driver does not use the completion
 435queue consumer index, but instead checks for non-zero status entries.
 436
 437When an incoming frame is less than RX_COPYBREAK bytes long, a fresh skbuff
 438is allocated and the frame is copied to the new skbuff.  When the incoming
 439frame is larger, the skbuff is passed directly up the protocol stack.
 440Buffers consumed this way are replaced by newly allocated skbuffs in a later
 441phase of receive.
 442
 443A notable aspect of operation is that unaligned buffers are not permitted by
 444the Starfire hardware.  Thus the IP header at offset 14 in an ethernet frame
 445isn't longword aligned, which may cause problems on some machine
 446e.g. Alphas and IA64. For these architectures, the driver is forced to copy
 447the frame into a new skbuff unconditionally. Copied frames are put into the
 448skbuff at an offset of "+2", thus 16-byte aligning the IP header.
 449
 450IIId. Synchronization
 451
 452The driver runs as two independent, single-threaded flows of control.  One
 453is the send-packet routine, which enforces single-threaded use by the
 454dev->tbusy flag.  The other thread is the interrupt handler, which is single
 455threaded by the hardware and interrupt handling software.
 456
 457The send packet thread has partial control over the Tx ring and the netif_queue
 458status. If the number of free Tx slots in the ring falls below a certain number
 459(currently hardcoded to 4), it signals the upper layer to stop the queue.
 460
 461The interrupt handler has exclusive control over the Rx ring and records stats
 462from the Tx ring.  After reaping the stats, it marks the Tx queue entry as
 463empty by incrementing the dirty_tx mark. Iff the netif_queue is stopped and the
 464number of free Tx slow is above the threshold, it signals the upper layer to
 465restart the queue.
 466
 467IV. Notes
 468
 469IVb. References
 470
 471The Adaptec Starfire manuals, available only from Adaptec.
 472http://www.scyld.com/expert/100mbps.html
 473http://www.scyld.com/expert/NWay.html
 474
 475IVc. Errata
 476
 477- StopOnPerr is broken, don't enable
 478- Hardware ethernet padding exposes random data, perform software padding
 479  instead (unverified -- works correctly for all the hardware I have)
 480
 481*/
 482
 483
 484
 485enum chip_capability_flags {CanHaveMII=1, };
 486
 487enum chipset {
 488        CH_6915 = 0,
 489};
 490
 491static struct pci_device_id starfire_pci_tbl[] = {
 492        { 0x9004, 0x6915, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_6915 },
 493        { 0, }
 494};
 495MODULE_DEVICE_TABLE(pci, starfire_pci_tbl);
 496
 497/* A chip capabilities table, matching the CH_xxx entries in xxx_pci_tbl[] above. */
 498static struct chip_info {
 499        const char *name;
 500        int drv_flags;
 501} netdrv_tbl[] __devinitdata = {
 502        { "Adaptec Starfire 6915", CanHaveMII },
 503};
 504
 505
 506/* Offsets to the device registers.
 507   Unlike software-only systems, device drivers interact with complex hardware.
 508   It's not useful to define symbolic names for every register bit in the
 509   device.  The name can only partially document the semantics and make
 510   the driver longer and more difficult to read.
 511   In general, only the important configuration values or bits changed
 512   multiple times should be defined symbolically.
 513*/
 514enum register_offsets {
 515        PCIDeviceConfig=0x50040, GenCtrl=0x50070, IntrTimerCtrl=0x50074,
 516        IntrClear=0x50080, IntrStatus=0x50084, IntrEnable=0x50088,
 517        MIICtrl=0x52000, TxStationAddr=0x50120, EEPROMCtrl=0x51000,
 518        GPIOCtrl=0x5008C, TxDescCtrl=0x50090,
 519        TxRingPtr=0x50098, HiPriTxRingPtr=0x50094, /* Low and High priority. */
 520        TxRingHiAddr=0x5009C,           /* 64 bit address extension. */
 521        TxProducerIdx=0x500A0, TxConsumerIdx=0x500A4,
 522        TxThreshold=0x500B0,
 523        CompletionHiAddr=0x500B4, TxCompletionAddr=0x500B8,
 524        RxCompletionAddr=0x500BC, RxCompletionQ2Addr=0x500C0,
 525        CompletionQConsumerIdx=0x500C4, RxDMACtrl=0x500D0,
 526        RxDescQCtrl=0x500D4, RxDescQHiAddr=0x500DC, RxDescQAddr=0x500E0,
 527        RxDescQIdx=0x500E8, RxDMAStatus=0x500F0, RxFilterMode=0x500F4,
 528        TxMode=0x55000, VlanType=0x55064,
 529        PerfFilterTable=0x56000, HashTable=0x56100,
 530        TxGfpMem=0x58000, RxGfpMem=0x5a000,
 531};
 532
 533/*
 534 * Bits in the interrupt status/mask registers.
 535 * Warning: setting Intr[Ab]NormalSummary in the IntrEnable register
 536 * enables all the interrupt sources that are or'ed into those status bits.
 537 */
 538enum intr_status_bits {
 539        IntrLinkChange=0xf0000000, IntrStatsMax=0x08000000,
 540        IntrAbnormalSummary=0x02000000, IntrGeneralTimer=0x01000000,
 541        IntrSoftware=0x800000, IntrRxComplQ1Low=0x400000,
 542        IntrTxComplQLow=0x200000, IntrPCI=0x100000,
 543        IntrDMAErr=0x080000, IntrTxDataLow=0x040000,
 544        IntrRxComplQ2Low=0x020000, IntrRxDescQ1Low=0x010000,
 545        IntrNormalSummary=0x8000, IntrTxDone=0x4000,
 546        IntrTxDMADone=0x2000, IntrTxEmpty=0x1000,
 547        IntrEarlyRxQ2=0x0800, IntrEarlyRxQ1=0x0400,
 548        IntrRxQ2Done=0x0200, IntrRxQ1Done=0x0100,
 549        IntrRxGFPDead=0x80, IntrRxDescQ2Low=0x40,
 550        IntrNoTxCsum=0x20, IntrTxBadID=0x10,
 551        IntrHiPriTxBadID=0x08, IntrRxGfp=0x04,
 552        IntrTxGfp=0x02, IntrPCIPad=0x01,
 553        /* not quite bits */
 554        IntrRxDone=IntrRxQ2Done | IntrRxQ1Done,
 555        IntrRxEmpty=IntrRxDescQ1Low | IntrRxDescQ2Low,
 556        IntrNormalMask=0xff00, IntrAbnormalMask=0x3ff00fe,
 557};
 558
 559/* Bits in the RxFilterMode register. */
 560enum rx_mode_bits {
 561        AcceptBroadcast=0x04, AcceptAllMulticast=0x02, AcceptAll=0x01,
 562        AcceptMulticast=0x10, PerfectFilter=0x40, HashFilter=0x30,
 563        PerfectFilterVlan=0x80, MinVLANPrio=0xE000, VlanMode=0x0200,
 564        WakeupOnGFP=0x0800,
 565};
 566
 567/* Bits in the TxMode register */
 568enum tx_mode_bits {
 569        MiiSoftReset=0x8000, MIILoopback=0x4000,
 570        TxFlowEnable=0x0800, RxFlowEnable=0x0400,
 571        PadEnable=0x04, FullDuplex=0x02, HugeFrame=0x01,
 572};
 573
 574/* Bits in the TxDescCtrl register. */
 575enum tx_ctrl_bits {
 576        TxDescSpaceUnlim=0x00, TxDescSpace32=0x10, TxDescSpace64=0x20,
 577        TxDescSpace128=0x30, TxDescSpace256=0x40,
 578        TxDescType0=0x00, TxDescType1=0x01, TxDescType2=0x02,
 579        TxDescType3=0x03, TxDescType4=0x04,
 580        TxNoDMACompletion=0x08,
 581        TxDescQAddr64bit=0x80, TxDescQAddr32bit=0,
 582        TxHiPriFIFOThreshShift=24, TxPadLenShift=16,
 583        TxDMABurstSizeShift=8,
 584};
 585
 586/* Bits in the RxDescQCtrl register. */
 587enum rx_ctrl_bits {
 588        RxBufferLenShift=16, RxMinDescrThreshShift=0,
 589        RxPrefetchMode=0x8000, RxVariableQ=0x2000,
 590        Rx2048QEntries=0x4000, Rx256QEntries=0,
 591        RxDescAddr64bit=0x1000, RxDescAddr32bit=0,
 592        RxDescQAddr64bit=0x0100, RxDescQAddr32bit=0,
 593        RxDescSpace4=0x000, RxDescSpace8=0x100,
 594        RxDescSpace16=0x200, RxDescSpace32=0x300,
 595        RxDescSpace64=0x400, RxDescSpace128=0x500,
 596        RxConsumerWrEn=0x80,
 597};
 598
 599/* Bits in the RxDMACtrl register. */
 600enum rx_dmactrl_bits {
 601        RxReportBadFrames=0x80000000, RxDMAShortFrames=0x40000000,
 602        RxDMABadFrames=0x20000000, RxDMACrcErrorFrames=0x10000000,
 603        RxDMAControlFrame=0x08000000, RxDMAPauseFrame=0x04000000,
 604        RxChecksumIgnore=0, RxChecksumRejectTCPUDP=0x02000000,
 605        RxChecksumRejectTCPOnly=0x01000000,
 606        RxCompletionQ2Enable=0x800000,
 607        RxDMAQ2Disable=0, RxDMAQ2FPOnly=0x100000,
 608        RxDMAQ2SmallPkt=0x200000, RxDMAQ2HighPrio=0x300000,
 609        RxDMAQ2NonIP=0x400000,
 610        RxUseBackupQueue=0x080000, RxDMACRC=0x040000,
 611        RxEarlyIntThreshShift=12, RxHighPrioThreshShift=8,
 612        RxBurstSizeShift=0,
 613};
 614
 615/* Bits in the RxCompletionAddr register */
 616enum rx_compl_bits {
 617        RxComplQAddr64bit=0x80, RxComplQAddr32bit=0,
 618        RxComplProducerWrEn=0x40,
 619        RxComplType0=0x00, RxComplType1=0x10,
 620        RxComplType2=0x20, RxComplType3=0x30,
 621        RxComplThreshShift=0,
 622};
 623
 624/* Bits in the TxCompletionAddr register */
 625enum tx_compl_bits {
 626        TxComplQAddr64bit=0x80, TxComplQAddr32bit=0,
 627        TxComplProducerWrEn=0x40,
 628        TxComplIntrStatus=0x20,
 629        CommonQueueMode=0x10,
 630        TxComplThreshShift=0,
 631};
 632
 633/* Bits in the GenCtrl register */
 634enum gen_ctrl_bits {
 635        RxEnable=0x05, TxEnable=0x0a,
 636        RxGFPEnable=0x10, TxGFPEnable=0x20,
 637};
 638
 639/* Bits in the IntrTimerCtrl register */
 640enum intr_ctrl_bits {
 641        Timer10X=0x800, EnableIntrMasking=0x60, SmallFrameBypass=0x100,
 642        SmallFrame64=0, SmallFrame128=0x200, SmallFrame256=0x400, SmallFrame512=0x600,
 643        IntrLatencyMask=0x1f,
 644};
 645
 646/* The Rx and Tx buffer descriptors. */
 647struct starfire_rx_desc {
 648        dma_addr_t rxaddr;
 649};
 650enum rx_desc_bits {
 651        RxDescValid=1, RxDescEndRing=2,
 652};
 653
 654/* Completion queue entry. */
 655struct short_rx_done_desc {
 656        u32 status;                     /* Low 16 bits is length. */
 657};
 658struct basic_rx_done_desc {
 659        u32 status;                     /* Low 16 bits is length. */
 660        u16 vlanid;
 661        u16 status2;
 662};
 663struct csum_rx_done_desc {
 664        u32 status;                     /* Low 16 bits is length. */
 665        u16 csum;                       /* Partial checksum */
 666        u16 status2;
 667};
 668struct full_rx_done_desc {
 669        u32 status;                     /* Low 16 bits is length. */
 670        u16 status3;
 671        u16 status2;
 672        u16 vlanid;
 673        u16 csum;                       /* partial checksum */
 674        u32 timestamp;
 675};
 676/* XXX: this is ugly and I'm not sure it's worth the trouble -Ion */
 677#ifdef HAS_FIRMWARE
 678#ifdef VLAN_SUPPORT
 679typedef struct full_rx_done_desc rx_done_desc;
 680#define RxComplType RxComplType3
 681#else  /* not VLAN_SUPPORT */
 682typedef struct csum_rx_done_desc rx_done_desc;
 683#define RxComplType RxComplType2
 684#endif /* not VLAN_SUPPORT */
 685#else  /* not HAS_FIRMWARE */
 686#ifdef VLAN_SUPPORT
 687typedef struct basic_rx_done_desc rx_done_desc;
 688#define RxComplType RxComplType1
 689#else  /* not VLAN_SUPPORT */
 690typedef struct short_rx_done_desc rx_done_desc;
 691#define RxComplType RxComplType0
 692#endif /* not VLAN_SUPPORT */
 693#endif /* not HAS_FIRMWARE */
 694
 695enum rx_done_bits {
 696        RxOK=0x20000000, RxFIFOErr=0x10000000, RxBufQ2=0x08000000,
 697};
 698
 699/* Type 1 Tx descriptor. */
 700struct starfire_tx_desc_1 {
 701        u32 status;                     /* Upper bits are status, lower 16 length. */
 702        u32 addr;
 703};
 704
 705/* Type 2 Tx descriptor. */
 706struct starfire_tx_desc_2 {
 707        u32 status;                     /* Upper bits are status, lower 16 length. */
 708        u32 reserved;
 709        u64 addr;
 710};
 711
 712#ifdef ADDR_64BITS
 713typedef struct starfire_tx_desc_2 starfire_tx_desc;
 714#define TX_DESC_TYPE TxDescType2
 715#else  /* not ADDR_64BITS */
 716typedef struct starfire_tx_desc_1 starfire_tx_desc;
 717#define TX_DESC_TYPE TxDescType1
 718#endif /* not ADDR_64BITS */
 719#define TX_DESC_SPACING TxDescSpaceUnlim
 720
 721enum tx_desc_bits {
 722        TxDescID=0xB0000000,
 723        TxCRCEn=0x01000000, TxDescIntr=0x08000000,
 724        TxRingWrap=0x04000000, TxCalTCP=0x02000000,
 725};
 726struct tx_done_desc {
 727        u32 status;                     /* timestamp, index. */
 728#if 0
 729        u32 intrstatus;                 /* interrupt status */
 730#endif
 731};
 732
 733struct rx_ring_info {
 734        struct sk_buff *skb;
 735        dma_addr_t mapping;
 736};
 737struct tx_ring_info {
 738        struct sk_buff *skb;
 739        dma_addr_t mapping;
 740        unsigned int used_slots;
 741};
 742
 743#define PHY_CNT         2
 744struct netdev_private {
 745        /* Descriptor rings first for alignment. */
 746        struct starfire_rx_desc *rx_ring;
 747        starfire_tx_desc *tx_ring;
 748        dma_addr_t rx_ring_dma;
 749        dma_addr_t tx_ring_dma;
 750        /* The addresses of rx/tx-in-place skbuffs. */
 751        struct rx_ring_info rx_info[RX_RING_SIZE];
 752        struct tx_ring_info tx_info[TX_RING_SIZE];
 753        /* Pointers to completion queues (full pages). */
 754        rx_done_desc *rx_done_q;
 755        dma_addr_t rx_done_q_dma;
 756        unsigned int rx_done;
 757        struct tx_done_desc *tx_done_q;
 758        dma_addr_t tx_done_q_dma;
 759        unsigned int tx_done;
 760        struct net_device_stats stats;
 761        struct pci_dev *pci_dev;
 762#ifdef VLAN_SUPPORT
 763        struct vlan_group *vlgrp;
 764#endif
 765        void *queue_mem;
 766        dma_addr_t queue_mem_dma;
 767        size_t queue_mem_size;
 768
 769        /* Frequently used values: keep some adjacent for cache effect. */
 770        spinlock_t lock;
 771        unsigned int cur_rx, dirty_rx;  /* Producer/consumer ring indices */
 772        unsigned int cur_tx, dirty_tx, reap_tx;
 773        unsigned int rx_buf_sz;         /* Based on MTU+slack. */
 774        /* These values keep track of the transceiver/media in use. */
 775        int speed100;                   /* Set if speed == 100MBit. */
 776        u32 tx_mode;
 777        u32 intr_timer_ctrl;
 778        u8 tx_threshold;
 779        /* MII transceiver section. */
 780        struct mii_if_info mii_if;              /* MII lib hooks/info */
 781        int phy_cnt;                    /* MII device addresses. */
 782        unsigned char phys[PHY_CNT];    /* MII device addresses. */
 783};
 784
 785
 786static int      mdio_read(struct net_device *dev, int phy_id, int location);
 787static void     mdio_write(struct net_device *dev, int phy_id, int location, int value);
 788static int      netdev_open(struct net_device *dev);
 789static void     check_duplex(struct net_device *dev);
 790static void     tx_timeout(struct net_device *dev);
 791static void     init_ring(struct net_device *dev);
 792static int      start_tx(struct sk_buff *skb, struct net_device *dev);
 793static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
 794static void     netdev_error(struct net_device *dev, int intr_status);
 795static int      __netdev_rx(struct net_device *dev, int *quota);
 796static void     refill_rx_ring(struct net_device *dev);
 797static void     netdev_error(struct net_device *dev, int intr_status);
 798static void     set_rx_mode(struct net_device *dev);
 799static struct net_device_stats *get_stats(struct net_device *dev);
 800static int      netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 801static int      netdev_close(struct net_device *dev);
 802static void     netdev_media_change(struct net_device *dev);
 803
 804
 805#ifdef VLAN_SUPPORT
 806static void netdev_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
 807{
 808        struct netdev_private *np = dev->priv;
 809
 810        spin_lock(&np->lock);
 811        if (debug > 2)
 812                printk("%s: Setting vlgrp to %p\n", dev->name, grp);
 813        np->vlgrp = grp;
 814        set_rx_mode(dev);
 815        spin_unlock(&np->lock);
 816}
 817
 818static void netdev_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
 819{
 820        struct netdev_private *np = dev->priv;
 821
 822        spin_lock(&np->lock);
 823        if (debug > 1)
 824                printk("%s: Adding vlanid %d to vlan filter\n", dev->name, vid);
 825        set_rx_mode(dev);
 826        spin_unlock(&np->lock);
 827}
 828
 829static void netdev_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
 830{
 831        struct netdev_private *np = dev->priv;
 832
 833        spin_lock(&np->lock);
 834        if (debug > 1)
 835                printk("%s: removing vlanid %d from vlan filter\n", dev->name, vid);
 836        if (np->vlgrp)
 837                np->vlgrp->vlan_devices[vid] = NULL;
 838        set_rx_mode(dev);
 839        spin_unlock(&np->lock);
 840}
 841#endif /* VLAN_SUPPORT */
 842
 843
 844static int __devinit starfire_init_one(struct pci_dev *pdev,
 845                                       const struct pci_device_id *ent)
 846{
 847        struct netdev_private *np;
 848        int i, irq, option, chip_idx = ent->driver_data;
 849        struct net_device *dev;
 850        static int card_idx = -1;
 851        long ioaddr;
 852        int drv_flags, io_size;
 853        int boguscnt;
 854
 855/* when built into the kernel, we only print version if device is found */
 856#ifndef MODULE
 857        static int printed_version;
 858        if (!printed_version++)
 859                printk(version);
 860#endif
 861
 862        card_idx++;
 863
 864        if (pci_enable_device (pdev))
 865                return -EIO;
 866
 867        ioaddr = pci_resource_start(pdev, 0);
 868        io_size = pci_resource_len(pdev, 0);
 869        if (!ioaddr || ((pci_resource_flags(pdev, 0) & IORESOURCE_MEM) == 0)) {
 870                printk(KERN_ERR DRV_NAME " %d: no PCI MEM resources, aborting\n", card_idx);
 871                return -ENODEV;
 872        }
 873
 874        dev = alloc_etherdev(sizeof(*np));
 875        if (!dev) {
 876                printk(KERN_ERR DRV_NAME " %d: cannot alloc etherdev, aborting\n", card_idx);
 877                return -ENOMEM;
 878        }
 879        SET_MODULE_OWNER(dev);
 880        SET_NETDEV_DEV(dev, &pdev->dev);
 881
 882        irq = pdev->irq;
 883
 884        if (pci_request_regions (pdev, dev->name)) {
 885                printk(KERN_ERR DRV_NAME " %d: cannot reserve PCI resources, aborting\n", card_idx);
 886                goto err_out_free_netdev;
 887        }
 888
 889        /* ioremap is borken in Linux-2.2.x/sparc64 */
 890#if !defined(CONFIG_SPARC64) || LINUX_VERSION_CODE > 0x20300
 891        ioaddr = (long) ioremap(ioaddr, io_size);
 892        if (!ioaddr) {
 893                printk(KERN_ERR DRV_NAME " %d: cannot remap %#x @ %#lx, aborting\n",
 894                        card_idx, io_size, ioaddr);
 895                goto err_out_free_res;
 896        }
 897#endif /* !CONFIG_SPARC64 || Linux 2.3.0+ */
 898
 899        pci_set_master(pdev);
 900
 901        /* enable MWI -- it vastly improves Rx performance on sparc64 */
 902        pci_set_mwi(pdev);
 903
 904#ifdef MAX_SKB_FRAGS
 905        dev->features |= NETIF_F_SG;
 906#endif /* MAX_SKB_FRAGS */
 907#ifdef ZEROCOPY
 908        /* Starfire can do TCP/UDP checksumming */
 909        if (enable_hw_cksum)
 910                dev->features |= NETIF_F_IP_CSUM;
 911#endif /* ZEROCOPY */
 912#ifdef VLAN_SUPPORT
 913        dev->features |= NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
 914        dev->vlan_rx_register = netdev_vlan_rx_register;
 915        dev->vlan_rx_add_vid = netdev_vlan_rx_add_vid;
 916        dev->vlan_rx_kill_vid = netdev_vlan_rx_kill_vid;
 917#endif /* VLAN_RX_KILL_VID */
 918#ifdef ADDR_64BITS
 919        dev->features |= NETIF_F_HIGHDMA;
 920#endif /* ADDR_64BITS */
 921
 922        /* Serial EEPROM reads are hidden by the hardware. */
 923        for (i = 0; i < 6; i++)
 924                dev->dev_addr[i] = readb(ioaddr + EEPROMCtrl + 20 - i);
 925
 926#if ! defined(final_version) /* Dump the EEPROM contents during development. */
 927        if (debug > 4)
 928                for (i = 0; i < 0x20; i++)
 929                        printk("%2.2x%s",
 930                               (unsigned int)readb(ioaddr + EEPROMCtrl + i),
 931                               i % 16 != 15 ? " " : "\n");
 932#endif
 933
 934        /* Issue soft reset */
 935        writel(MiiSoftReset, ioaddr + TxMode);
 936        udelay(1000);
 937        writel(0, ioaddr + TxMode);
 938
 939        /* Reset the chip to erase previous misconfiguration. */
 940        writel(1, ioaddr + PCIDeviceConfig);
 941        boguscnt = 1000;
 942        while (--boguscnt > 0) {
 943                udelay(10);
 944                if ((readl(ioaddr + PCIDeviceConfig) & 1) == 0)
 945                        break;
 946        }
 947        if (boguscnt == 0)
 948                printk("%s: chipset reset never completed!\n", dev->name);
 949        /* wait a little longer */
 950        udelay(1000);
 951
 952        dev->base_addr = ioaddr;
 953        dev->irq = irq;
 954
 955        np = dev->priv;
 956        spin_lock_init(&np->lock);
 957        pci_set_drvdata(pdev, dev);
 958
 959        np->pci_dev = pdev;
 960
 961        np->mii_if.dev = dev;
 962        np->mii_if.mdio_read = mdio_read;
 963        np->mii_if.mdio_write = mdio_write;
 964        np->mii_if.phy_id_mask = 0x1f;
 965        np->mii_if.reg_num_mask = 0x1f;
 966
 967        drv_flags = netdrv_tbl[chip_idx].drv_flags;
 968
 969        option = card_idx < MAX_UNITS ? options[card_idx] : 0;
 970        if (dev->mem_start)
 971                option = dev->mem_start;
 972
 973        /* The lower four bits are the media type. */
 974        if (option & 0x200)
 975                np->mii_if.full_duplex = 1;
 976
 977        if (card_idx < MAX_UNITS && full_duplex[card_idx] > 0)
 978                np->mii_if.full_duplex = 1;
 979
 980        if (np->mii_if.full_duplex)
 981                np->mii_if.force_media = 1;
 982        else
 983                np->mii_if.force_media = 0;
 984        np->speed100 = 1;
 985
 986        /* timer resolution is 128 * 0.8us */
 987        np->intr_timer_ctrl = (((intr_latency * 10) / 1024) & IntrLatencyMask) |
 988                Timer10X | EnableIntrMasking;
 989
 990        if (small_frames > 0) {
 991                np->intr_timer_ctrl |= SmallFrameBypass;
 992                switch (small_frames) {
 993                case 1 ... 64:
 994                        np->intr_timer_ctrl |= SmallFrame64;
 995                        break;
 996                case 65 ... 128:
 997                        np->intr_timer_ctrl |= SmallFrame128;
 998                        break;
 999                case 129 ... 256:
1000                        np->intr_timer_ctrl |= SmallFrame256;
1001                        break;
1002                default:
1003                        np->intr_timer_ctrl |= SmallFrame512;
1004                        if (small_frames > 512)
1005                                printk("Adjusting small_frames down to 512\n");
1006                        break;
1007                }
1008        }
1009
1010        /* The chip-specific entries in the device structure. */
1011        dev->open = &netdev_open;
1012        dev->hard_start_xmit = &start_tx;
1013        init_tx_timer(dev, tx_timeout, TX_TIMEOUT);
1014        init_poll(dev);
1015        dev->stop = &netdev_close;
1016        dev->get_stats = &get_stats;
1017        dev->set_multicast_list = &set_rx_mode;
1018        dev->do_ioctl = &netdev_ioctl;
1019
1020        if (mtu)
1021                dev->mtu = mtu;
1022
1023        if (register_netdev(dev))
1024                goto err_out_cleardev;
1025
1026        printk(KERN_INFO "%s: %s at %#lx, ",
1027                   dev->name, netdrv_tbl[chip_idx].name, ioaddr);
1028        for (i = 0; i < 5; i++)
1029                printk("%2.2x:", dev->dev_addr[i]);
1030        printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
1031
1032        if (drv_flags & CanHaveMII) {
1033                int phy, phy_idx = 0;
1034                int mii_status;
1035                for (phy = 0; phy < 32 && phy_idx < PHY_CNT; phy++) {
1036                        mdio_write(dev, phy, MII_BMCR, BMCR_RESET);
1037                        mdelay(100);
1038                        boguscnt = 1000;
1039                        while (--boguscnt > 0)
1040                                if ((mdio_read(dev, phy, MII_BMCR) & BMCR_RESET) == 0)
1041                                        break;
1042                        if (boguscnt == 0) {
1043                                printk("%s: PHY reset never completed!\n", dev->name);
1044                                continue;
1045                        }
1046                        mii_status = mdio_read(dev, phy, MII_BMSR);
1047                        if (mii_status != 0) {
1048                                np->phys[phy_idx++] = phy;
1049                                np->mii_if.advertising = mdio_read(dev, phy, MII_ADVERTISE);
1050                                printk(KERN_INFO "%s: MII PHY found at address %d, status "
1051                                           "%#4.4x advertising %#4.4x.\n",
1052                                           dev->name, phy, mii_status, np->mii_if.advertising);
1053                                /* there can be only one PHY on-board */
1054                                break;
1055                        }
1056                }
1057                np->phy_cnt = phy_idx;
1058                if (np->phy_cnt > 0)
1059                        np->mii_if.phy_id = np->phys[0];
1060                else
1061                        memset(&np->mii_if, 0, sizeof(np->mii_if));
1062        }
1063
1064        printk(KERN_INFO "%s: scatter-gather and hardware TCP cksumming %s.\n",
1065               dev->name, enable_hw_cksum ? "enabled" : "disabled");
1066        return 0;
1067
1068err_out_cleardev:
1069        pci_set_drvdata(pdev, NULL);
1070        iounmap((void *)ioaddr);
1071err_out_free_res:
1072        pci_release_regions (pdev);
1073err_out_free_netdev:
1074        free_netdev(dev);
1075        return -ENODEV;
1076}
1077
1078
1079/* Read the MII Management Data I/O (MDIO) interfaces. */
1080static int mdio_read(struct net_device *dev, int phy_id, int location)
1081{
1082        long mdio_addr = dev->base_addr + MIICtrl + (phy_id<<7) + (location<<2);
1083        int result, boguscnt=1000;
1084        /* ??? Should we add a busy-wait here? */
1085        do
1086                result = readl(mdio_addr);
1087        while ((result & 0xC0000000) != 0x80000000 && --boguscnt > 0);
1088        if (boguscnt == 0)
1089                return 0;
1090        if ((result & 0xffff) == 0xffff)
1091                return 0;
1092        return result & 0xffff;
1093}
1094
1095
1096static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
1097{
1098        long mdio_addr = dev->base_addr + MIICtrl + (phy_id<<7) + (location<<2);
1099        writel(value, mdio_addr);
1100        /* The busy-wait will occur before a read. */
1101}
1102
1103
1104static int netdev_open(struct net_device *dev)
1105{
1106        struct netdev_private *np = dev->priv;
1107        long ioaddr = dev->base_addr;
1108        int i, retval;
1109        size_t tx_done_q_size, rx_done_q_size, tx_ring_size, rx_ring_size;
1110
1111        /* Do we ever need to reset the chip??? */
1112
1113        COMPAT_MOD_INC_USE_COUNT;
1114
1115        retval = request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev);
1116        if (retval) {
1117                COMPAT_MOD_DEC_USE_COUNT;
1118                return retval;
1119        }
1120
1121        /* Disable the Rx and Tx, and reset the chip. */
1122        writel(0, ioaddr + GenCtrl);
1123        writel(1, ioaddr + PCIDeviceConfig);
1124        if (debug > 1)
1125                printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
1126                       dev->name, dev->irq);
1127
1128        /* Allocate the various queues. */
1129        if (np->queue_mem == 0) {
1130                tx_done_q_size = ((sizeof(struct tx_done_desc) * DONE_Q_SIZE + QUEUE_ALIGN - 1) / QUEUE_ALIGN) * QUEUE_ALIGN;
1131                rx_done_q_size = ((sizeof(rx_done_desc) * DONE_Q_SIZE + QUEUE_ALIGN - 1) / QUEUE_ALIGN) * QUEUE_ALIGN;
1132                tx_ring_size = ((sizeof(starfire_tx_desc) * TX_RING_SIZE + QUEUE_ALIGN - 1) / QUEUE_ALIGN) * QUEUE_ALIGN;
1133                rx_ring_size = sizeof(struct starfire_rx_desc) * RX_RING_SIZE;
1134                np->queue_mem_size = tx_done_q_size + rx_done_q_size + tx_ring_size + rx_ring_size;
1135                np->queue_mem = pci_alloc_consistent(np->pci_dev, np->queue_mem_size, &np->queue_mem_dma);
1136                if (np->queue_mem == 0) {
1137                        COMPAT_MOD_DEC_USE_COUNT;
1138                        return -ENOMEM;
1139                }
1140
1141                np->tx_done_q     = np->queue_mem;
1142                np->tx_done_q_dma = np->queue_mem_dma;
1143                np->rx_done_q     = (void *) np->tx_done_q + tx_done_q_size;
1144                np->rx_done_q_dma = np->tx_done_q_dma + tx_done_q_size;
1145                np->tx_ring       = (void *) np->rx_done_q + rx_done_q_size;
1146                np->tx_ring_dma   = np->rx_done_q_dma + rx_done_q_size;
1147                np->rx_ring       = (void *) np->tx_ring + tx_ring_size;
1148                np->rx_ring_dma   = np->tx_ring_dma + tx_ring_size;
1149        }
1150
1151        /* Start with no carrier, it gets adjusted later */
1152        netif_carrier_off(dev);
1153        init_ring(dev);
1154        /* Set the size of the Rx buffers. */
1155        writel((np->rx_buf_sz << RxBufferLenShift) |
1156               (0 << RxMinDescrThreshShift) |
1157               RxPrefetchMode | RxVariableQ |
1158               RX_Q_ENTRIES |
1159               RX_DESC_Q_ADDR_SIZE | RX_DESC_ADDR_SIZE |
1160               RxDescSpace4,
1161               ioaddr + RxDescQCtrl);
1162
1163        /* Set up the Rx DMA controller. */
1164        writel(RxChecksumIgnore |
1165               (0 << RxEarlyIntThreshShift) |
1166               (6 << RxHighPrioThreshShift) |
1167               ((DMA_BURST_SIZE / 32) << RxBurstSizeShift),
1168               ioaddr + RxDMACtrl);
1169
1170        /* Set Tx descriptor */
1171        writel((2 << TxHiPriFIFOThreshShift) |
1172               (0 << TxPadLenShift) |
1173               ((DMA_BURST_SIZE / 32) << TxDMABurstSizeShift) |
1174               TX_DESC_Q_ADDR_SIZE |
1175               TX_DESC_SPACING | TX_DESC_TYPE,
1176               ioaddr + TxDescCtrl);
1177
1178        writel( (np->queue_mem_dma >> 16) >> 16, ioaddr + RxDescQHiAddr);
1179        writel( (np->queue_mem_dma >> 16) >> 16, ioaddr + TxRingHiAddr);
1180        writel( (np->queue_mem_dma >> 16) >> 16, ioaddr + CompletionHiAddr);
1181        writel(np->rx_ring_dma, ioaddr + RxDescQAddr);
1182        writel(np->tx_ring_dma, ioaddr + TxRingPtr);
1183
1184        writel(np->tx_done_q_dma, ioaddr + TxCompletionAddr);
1185        writel(np->rx_done_q_dma |
1186               RxComplType |
1187               (0 << RxComplThreshShift),
1188               ioaddr + RxCompletionAddr);
1189
1190        if (debug > 1)
1191                printk(KERN_DEBUG "%s: Filling in the station address.\n", dev->name);
1192
1193        /* Fill both the Tx SA register and the Rx perfect filter. */
1194        for (i = 0; i < 6; i++)
1195                writeb(dev->dev_addr[i], ioaddr + TxStationAddr + 5 - i);
1196        /* The first entry is special because it bypasses the VLAN filter.
1197           Don't use it. */
1198        writew(0, ioaddr + PerfFilterTable);
1199        writew(0, ioaddr + PerfFilterTable + 4);
1200        writew(0, ioaddr + PerfFilterTable + 8);
1201        for (i = 1; i < 16; i++) {
1202                u16 *eaddrs = (u16 *)dev->dev_addr;
1203                long setup_frm = ioaddr + PerfFilterTable + i * 16;
1204                writew(cpu_to_be16(eaddrs[2]), setup_frm); setup_frm += 4;
1205                writew(cpu_to_be16(eaddrs[1]), setup_frm); setup_frm += 4;
1206                writew(cpu_to_be16(eaddrs[0]), setup_frm); setup_frm += 8;
1207        }
1208
1209        /* Initialize other registers. */
1210        /* Configure the PCI bus bursts and FIFO thresholds. */
1211        np->tx_mode = TxFlowEnable|RxFlowEnable|PadEnable;      /* modified when link is up. */
1212        writel(MiiSoftReset | np->tx_mode, ioaddr + TxMode);
1213        udelay(1000);
1214        writel(np->tx_mode, ioaddr + TxMode);
1215        np->tx_threshold = 4;
1216        writel(np->tx_threshold, ioaddr + TxThreshold);
1217
1218        writel(np->intr_timer_ctrl, ioaddr + IntrTimerCtrl);
1219
1220        netif_start_if(dev);
1221        netif_start_queue(dev);
1222
1223        if (debug > 1)
1224                printk(KERN_DEBUG "%s: Setting the Rx and Tx modes.\n", dev->name);
1225        set_rx_mode(dev);
1226
1227        np->mii_if.advertising = mdio_read(dev, np->phys[0], MII_ADVERTISE);
1228        check_duplex(dev);
1229
1230        /* Enable GPIO interrupts on link change */
1231        writel(0x0f00ff00, ioaddr + GPIOCtrl);
1232
1233        /* Set the interrupt mask */
1234        writel(IntrRxDone | IntrRxEmpty | IntrDMAErr |
1235               IntrTxDMADone | IntrStatsMax | IntrLinkChange |
1236               IntrRxGFPDead | IntrNoTxCsum | IntrTxBadID,
1237               ioaddr + IntrEnable);
1238        /* Enable PCI interrupts. */
1239        writel(0x00800000 | readl(ioaddr + PCIDeviceConfig),
1240               ioaddr + PCIDeviceConfig);
1241
1242#ifdef VLAN_SUPPORT
1243        /* Set VLAN type to 802.1q */
1244        writel(ETH_P_8021Q, ioaddr + VlanType);
1245#endif /* VLAN_SUPPORT */
1246
1247#ifdef HAS_FIRMWARE
1248        /* Load Rx/Tx firmware into the frame processors */
1249        for (i = 0; i < FIRMWARE_RX_SIZE * 2; i++)
1250                writel(firmware_rx[i], ioaddr + RxGfpMem + i * 4);
1251        for (i = 0; i < FIRMWARE_TX_SIZE * 2; i++)
1252                writel(firmware_tx[i], ioaddr + TxGfpMem + i * 4);
1253#endif /* HAS_FIRMWARE */
1254        if (enable_hw_cksum)
1255                /* Enable the Rx and Tx units, and the Rx/Tx frame processors. */
1256                writel(TxEnable|TxGFPEnable|RxEnable|RxGFPEnable, ioaddr + GenCtrl);
1257        else
1258                /* Enable the Rx and Tx units only. */
1259                writel(TxEnable|RxEnable, ioaddr + GenCtrl);
1260
1261        if (debug > 1)
1262                printk(KERN_DEBUG "%s: Done netdev_open().\n",
1263                       dev->name);
1264
1265        return 0;
1266}
1267
1268
1269static void check_duplex(struct net_device *dev)
1270{
1271        struct netdev_private *np = dev->priv;
1272        u16 reg0;
1273        int silly_count = 1000;
1274
1275        mdio_write(dev, np->phys[0], MII_ADVERTISE, np->mii_if.advertising);
1276        mdio_write(dev, np->phys[0], MII_BMCR, BMCR_RESET);
1277        udelay(500);
1278        while (--silly_count && mdio_read(dev, np->phys[0], MII_BMCR) & BMCR_RESET)
1279                /* do nothing */;
1280        if (!silly_count) {
1281                printk("%s: MII reset failed!\n", dev->name);
1282                return;
1283        }
1284
1285        reg0 = mdio_read(dev, np->phys[0], MII_BMCR);
1286
1287        if (!np->mii_if.force_media) {
1288                reg0 |= BMCR_ANENABLE | BMCR_ANRESTART;
1289        } else {
1290                reg0 &= ~(BMCR_ANENABLE | BMCR_ANRESTART);
1291                if (np->speed100)
1292                        reg0 |= BMCR_SPEED100;
1293                if (np->mii_if.full_duplex)
1294                        reg0 |= BMCR_FULLDPLX;
1295                printk(KERN_DEBUG "%s: Link forced to %sMbit %s-duplex\n",
1296                       dev->name,
1297                       np->speed100 ? "100" : "10",
1298                       np->mii_if.full_duplex ? "full" : "half");
1299        }
1300        mdio_write(dev, np->phys[0], MII_BMCR, reg0);
1301}
1302
1303
1304static void tx_timeout(struct net_device *dev)
1305{
1306        struct netdev_private *np = dev->priv;
1307        long ioaddr = dev->base_addr;
1308        int old_debug;
1309
1310        printk(KERN_WARNING "%s: Transmit timed out, status %#8.8x, "
1311               "resetting...\n", dev->name, (int) readl(ioaddr + IntrStatus));
1312
1313        /* Perhaps we should reinitialize the hardware here. */
1314
1315        /*
1316         * Stop and restart the interface.
1317         * Cheat and increase the debug level temporarily.
1318         */
1319        old_debug = debug;
1320        debug = 2;
1321        netdev_close(dev);
1322        netdev_open(dev);
1323        debug = old_debug;
1324
1325        /* Trigger an immediate transmit demand. */
1326
1327        dev->trans_start = jiffies;
1328        np->stats.tx_errors++;
1329        netif_wake_queue(dev);
1330}
1331
1332
1333/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1334static void init_ring(struct net_device *dev)
1335{
1336        struct netdev_private *np = dev->priv;
1337        int i;
1338
1339        np->cur_rx = np->cur_tx = np->reap_tx = 0;
1340        np->dirty_rx = np->dirty_tx = np->rx_done = np->tx_done = 0;
1341
1342        np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
1343
1344        /* Fill in the Rx buffers.  Handle allocation failure gracefully. */
1345        for (i = 0; i < RX_RING_SIZE; i++) {
1346                struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
1347                np->rx_info[i].skb = skb;
1348                if (skb == NULL)
1349                        break;
1350                np->rx_info[i].mapping = pci_map_single(np->pci_dev, skb->tail, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1351                skb->dev = dev;                 /* Mark as being used by this device. */
1352                /* Grrr, we cannot offset to correctly align the IP header. */
1353                np->rx_ring[i].rxaddr = cpu_to_dma(np->rx_info[i].mapping | RxDescValid);
1354        }
1355        writew(i - 1, dev->base_addr + RxDescQIdx);
1356        np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
1357
1358        /* Clear the remainder of the Rx buffer ring. */
1359        for (  ; i < RX_RING_SIZE; i++) {
1360                np->rx_ring[i].rxaddr = 0;
1361                np->rx_info[i].skb = NULL;
1362                np->rx_info[i].mapping = 0;
1363        }
1364        /* Mark the last entry as wrapping the ring. */
1365        np->rx_ring[RX_RING_SIZE - 1].rxaddr |= cpu_to_dma(RxDescEndRing);
1366
1367        /* Clear the completion rings. */
1368        for (i = 0; i < DONE_Q_SIZE; i++) {
1369                np->rx_done_q[i].status = 0;
1370                np->tx_done_q[i].status = 0;
1371        }
1372
1373        for (i = 0; i < TX_RING_SIZE; i++)
1374                memset(&np->tx_info[i], 0, sizeof(np->tx_info[i]));
1375
1376        return;
1377}
1378
1379
1380static int start_tx(struct sk_buff *skb, struct net_device *dev)
1381{
1382        struct netdev_private *np = dev->priv;
1383        unsigned int entry;
1384        u32 status;
1385        int i;
1386
1387        kick_tx_timer(dev, tx_timeout, TX_TIMEOUT);
1388
1389        /*
1390         * be cautious here, wrapping the queue has weird semantics
1391         * and we may not have enough slots even when it seems we do.
1392         */
1393        if ((np->cur_tx - np->dirty_tx) + skb_num_frags(skb) * 2 > TX_RING_SIZE) {
1394                netif_stop_queue(dev);
1395                return 1;
1396        }
1397
1398#if defined(ZEROCOPY) && defined(HAS_BROKEN_FIRMWARE)
1399        {
1400                int has_bad_length = 0;
1401
1402                if (skb_first_frag_len(skb) == 1)
1403                        has_bad_length = 1;
1404                else {
1405                        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1406                                if (skb_shinfo(skb)->frags[i].size == 1) {
1407                                        has_bad_length = 1;
1408                                        break;
1409                                }
1410                }
1411
1412                if (has_bad_length)
1413                        skb_checksum_help(skb);
1414        }
1415#endif /* ZEROCOPY && HAS_BROKEN_FIRMWARE */
1416
1417        entry = np->cur_tx % TX_RING_SIZE;
1418        for (i = 0; i < skb_num_frags(skb); i++) {
1419                int wrap_ring = 0;
1420                status = TxDescID;
1421
1422                if (i == 0) {
1423                        np->tx_info[entry].skb = skb;
1424                        status |= TxCRCEn;
1425                        if (entry >= TX_RING_SIZE - skb_num_frags(skb)) {
1426                                status |= TxRingWrap;
1427                                wrap_ring = 1;
1428                        }
1429                        if (np->reap_tx) {
1430                                status |= TxDescIntr;
1431                                np->reap_tx = 0;
1432                        }
1433                        if (skb->ip_summed == CHECKSUM_HW) {
1434                                status |= TxCalTCP;
1435                                np->stats.tx_compressed++;
1436                        }
1437                        status |= skb_first_frag_len(skb) | (skb_num_frags(skb) << 16);
1438
1439                        np->tx_info[entry].mapping =
1440                                pci_map_single(np->pci_dev, skb->data, skb_first_frag_len(skb), PCI_DMA_TODEVICE);
1441                } else {
1442#ifdef MAX_SKB_FRAGS
1443                        skb_frag_t *this_frag = &skb_shinfo(skb)->frags[i - 1];
1444                        status |= this_frag->size;
1445                        np->tx_info[entry].mapping =
1446                                pci_map_single(np->pci_dev, page_address(this_frag->page) + this_frag->page_offset, this_frag->size, PCI_DMA_TODEVICE);
1447#endif /* MAX_SKB_FRAGS */
1448                }
1449
1450                np->tx_ring[entry].addr = cpu_to_dma(np->tx_info[entry].mapping);
1451                np->tx_ring[entry].status = cpu_to_le32(status);
1452                if (debug > 3)
1453                        printk(KERN_DEBUG "%s: Tx #%d/#%d slot %d status %#8.8x.\n",
1454                               dev->name, np->cur_tx, np->dirty_tx,
1455                               entry, status);
1456                if (wrap_ring) {
1457                        np->tx_info[entry].used_slots = TX_RING_SIZE - entry;
1458                        np->cur_tx += np->tx_info[entry].used_slots;
1459                        entry = 0;
1460                } else {
1461                        np->tx_info[entry].used_slots = 1;
1462                        np->cur_tx += np->tx_info[entry].used_slots;
1463                        entry++;
1464                }
1465                /* scavenge the tx descriptors twice per TX_RING_SIZE */
1466                if (np->cur_tx % (TX_RING_SIZE / 2) == 0)
1467                        np->reap_tx = 1;
1468        }
1469
1470        /* Non-x86: explicitly flush descriptor cache lines here. */
1471        /* Ensure all descriptors are written back before the transmit is
1472           initiated. - Jes */
1473        wmb();
1474
1475        /* Update the producer index. */
1476        writel(entry * (sizeof(starfire_tx_desc) / 8), dev->base_addr + TxProducerIdx);
1477
1478        /* 4 is arbitrary, but should be ok */
1479        if ((np->cur_tx - np->dirty_tx) + 4 > TX_RING_SIZE)
1480                netif_stop_queue(dev);
1481
1482        dev->trans_start = jiffies;
1483
1484        return 0;
1485}
1486
1487
1488/* The interrupt handler does all of the Rx thread work and cleans up
1489   after the Tx thread. */
1490static irqreturn_t intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
1491{
1492        struct net_device *dev = dev_instance;
1493        struct netdev_private *np;
1494        long ioaddr;
1495        int boguscnt = max_interrupt_work;
1496        int consumer;
1497        int tx_status;
1498        int handled = 0;
1499
1500        ioaddr = dev->base_addr;
1501        np = dev->priv;
1502
1503        do {
1504                u32 intr_status = readl(ioaddr + IntrClear);
1505
1506                if (debug > 4)
1507                        printk(KERN_DEBUG "%s: Interrupt status %#8.8x.\n",
1508                               dev->name, intr_status);
1509
1510                if (intr_status == 0 || intr_status == (u32) -1)
1511                        break;
1512
1513                handled = 1;
1514
1515                if (intr_status & (IntrRxDone | IntrRxEmpty))
1516                        netdev_rx(dev, ioaddr);
1517
1518                /* Scavenge the skbuff list based on the Tx-done queue.
1519                   There are redundant checks here that may be cleaned up
1520                   after the driver has proven to be reliable. */
1521                consumer = readl(ioaddr + TxConsumerIdx);
1522                if (debug > 3)
1523                        printk(KERN_DEBUG "%s: Tx Consumer index is %d.\n",
1524                               dev->name, consumer);
1525
1526                while ((tx_status = le32_to_cpu(np->tx_done_q[np->tx_done].status)) != 0) {
1527                        if (debug > 3)
1528                                printk(KERN_DEBUG "%s: Tx completion #%d entry %d is %#8.8x.\n",
1529                                       dev->name, np->dirty_tx, np->tx_done, tx_status);
1530                        if ((tx_status & 0xe0000000) == 0xa0000000) {
1531                                np->stats.tx_packets++;
1532                        } else if ((tx_status & 0xe0000000) == 0x80000000) {
1533                                u16 entry = (tx_status & 0x7fff) / sizeof(starfire_tx_desc);
1534                                struct sk_buff *skb = np->tx_info[entry].skb;
1535                                np->tx_info[entry].skb = NULL;
1536                                pci_unmap_single(np->pci_dev,
1537                                                 np->tx_info[entry].mapping,
1538                                                 skb_first_frag_len(skb),
1539                                                 PCI_DMA_TODEVICE);
1540                                np->tx_info[entry].mapping = 0;
1541                                np->dirty_tx += np->tx_info[entry].used_slots;
1542                                entry = (entry + np->tx_info[entry].used_slots) % TX_RING_SIZE;
1543#ifdef MAX_SKB_FRAGS
1544                                {
1545                                        int i;
1546                                        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1547                                                pci_unmap_single(np->pci_dev,
1548                                                                 np->tx_info[entry].mapping,
1549                                                                 skb_shinfo(skb)->frags[i].size,
1550                                                                 PCI_DMA_TODEVICE);
1551                                                np->dirty_tx++;
1552                                                entry++;
1553                                        }
1554                                }
1555#endif /* MAX_SKB_FRAGS */
1556                                dev_kfree_skb_irq(skb);
1557                        }
1558                        np->tx_done_q[np->tx_done].status = 0;
1559                        np->tx_done = (np->tx_done + 1) % DONE_Q_SIZE;
1560                }
1561                writew(np->tx_done, ioaddr + CompletionQConsumerIdx + 2);
1562
1563                if (netif_queue_stopped(dev) &&
1564                    (np->cur_tx - np->dirty_tx + 4 < TX_RING_SIZE)) {
1565                        /* The ring is no longer full, wake the queue. */
1566                        netif_wake_queue(dev);
1567                }
1568
1569                /* Stats overflow */
1570                if (intr_status & IntrStatsMax)
1571                        get_stats(dev);
1572
1573                /* Media change interrupt. */
1574                if (intr_status & IntrLinkChange)
1575                        netdev_media_change(dev);
1576
1577                /* Abnormal error summary/uncommon events handlers. */
1578                if (intr_status & IntrAbnormalSummary)
1579                        netdev_error(dev, intr_status);
1580
1581                if (--boguscnt < 0) {
1582                        if (debug > 1)
1583                                printk(KERN_WARNING "%s: Too much work at interrupt, "
1584                                       "status=%#8.8x.\n",
1585                                       dev->name, intr_status);
1586                        break;
1587                }
1588        } while (1);
1589
1590        if (debug > 4)
1591                printk(KERN_DEBUG "%s: exiting interrupt, status=%#8.8x.\n",
1592                       dev->name, (int) readl(ioaddr + IntrStatus));
1593        return IRQ_RETVAL(handled);
1594}
1595
1596
1597/* This routine is logically part of the interrupt/poll handler, but separated
1598   for clarity, code sharing between NAPI/non-NAPI, and better register allocation. */
1599static int __netdev_rx(struct net_device *dev, int *quota)
1600{
1601        struct netdev_private *np = dev->priv;
1602        u32 desc_status;
1603        int retcode = 0;
1604
1605        /* If EOP is set on the next entry, it's a new packet. Send it up. */
1606        while ((desc_status = le32_to_cpu(np->rx_done_q[np->rx_done].status)) != 0) {
1607                struct sk_buff *skb;
1608                u16 pkt_len;
1609                int entry;
1610                rx_done_desc *desc = &np->rx_done_q[np->rx_done];
1611
1612                if (debug > 4)
1613                        printk(KERN_DEBUG "  netdev_rx() status of %d was %#8.8x.\n", np->rx_done, desc_status);
1614                if (!(desc_status & RxOK)) {
1615                        /* There was a error. */
1616                        if (debug > 2)
1617                                printk(KERN_DEBUG "  netdev_rx() Rx error was %#8.8x.\n", desc_status);
1618                        np->stats.rx_errors++;
1619                        if (desc_status & RxFIFOErr)
1620                                np->stats.rx_fifo_errors++;
1621                        goto next_rx;
1622                }
1623
1624                if (*quota <= 0) {      /* out of rx quota */
1625                        retcode = 1;
1626                        goto out;
1627                }
1628                (*quota)--;
1629
1630                pkt_len = desc_status;  /* Implicitly Truncate */
1631                entry = (desc_status >> 16) & 0x7ff;
1632
1633                if (debug > 4)
1634                        printk(KERN_DEBUG "  netdev_rx() normal Rx pkt length %d, quota %d.\n", pkt_len, *quota);
1635                /* Check if the packet is long enough to accept without copying
1636                   to a minimally-sized skbuff. */
1637                if (pkt_len < rx_copybreak
1638                    && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1639                        skb->dev = dev;
1640                        skb_reserve(skb, 2);    /* 16 byte align the IP header */
1641                        pci_dma_sync_single(np->pci_dev,
1642                                            np->rx_info[entry].mapping,
1643                                            pkt_len, PCI_DMA_FROMDEVICE);
1644                        eth_copy_and_sum(skb, np->rx_info[entry].skb->tail, pkt_len, 0);
1645                        skb_put(skb, pkt_len);
1646                } else {
1647                        pci_unmap_single(np->pci_dev, np->rx_info[entry].mapping, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1648                        skb = np->rx_info[entry].skb;
1649                        skb_put(skb, pkt_len);
1650                        np->rx_info[entry].skb = NULL;
1651                        np->rx_info[entry].mapping = 0;
1652                }
1653#ifndef final_version                   /* Remove after testing. */
1654                /* You will want this info for the initial debug. */
1655                if (debug > 5)
1656                        printk(KERN_DEBUG "  Rx data %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:"
1657                               "%2.2x %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x %2.2x%2.2x.\n",
1658                               skb->data[0], skb->data[1], skb->data[2], skb->data[3],
1659                               skb->data[4], skb->data[5], skb->data[6], skb->data[7],
1660                               skb->data[8], skb->data[9], skb->data[10],
1661                               skb->data[11], skb->data[12], skb->data[13]);
1662#endif
1663
1664                skb->protocol = eth_type_trans(skb, dev);
1665#if defined(HAS_FIRMWARE) || defined(VLAN_SUPPORT)
1666                if (debug > 4)
1667                        printk(KERN_DEBUG "  netdev_rx() status2 of %d was %#4.4x.\n", np->rx_done, le16_to_cpu(desc->status2));
1668#endif
1669#ifdef HAS_FIRMWARE
1670                if (le16_to_cpu(desc->status2) & 0x0100) {
1671                        skb->ip_summed = CHECKSUM_UNNECESSARY;
1672                        np->stats.rx_compressed++;
1673                }
1674                /*
1675                 * This feature doesn't seem to be working, at least
1676                 * with the two firmware versions I have. If the GFP sees
1677                 * an IP fragment, it either ignores it completely, or reports
1678                 * "bad checksum" on it.
1679                 *
1680                 * Maybe I missed something -- corrections are welcome.
1681                 * Until then, the printk stays. :-) -Ion
1682                 */
1683                else if (le16_to_cpu(desc->status2) & 0x0040) {
1684                        skb->ip_summed = CHECKSUM_HW;
1685                        skb->csum = le16_to_cpu(desc->csum);
1686                        printk(KERN_DEBUG "%s: checksum_hw, status2 = %#x\n", dev->name, le16_to_cpu(desc->status2));
1687                }
1688#endif /* HAS_FIRMWARE */
1689#ifdef VLAN_SUPPORT
1690                if (np->vlgrp && le16_to_cpu(desc->status2) & 0x0200) {
1691                        if (debug > 4)
1692                                printk(KERN_DEBUG "  netdev_rx() vlanid = %d\n", le16_to_cpu(desc->vlanid));
1693                        /* vlan_netdev_receive_skb() expects a packet with the VLAN tag stripped out */
1694                        vlan_netdev_receive_skb(skb, np->vlgrp, le16_to_cpu(desc->vlanid) & VLAN_VID_MASK);
1695                } else
1696#endif /* VLAN_SUPPORT */
1697                        netdev_receive_skb(skb);
1698                dev->last_rx = jiffies;
1699                np->stats.rx_packets++;
1700
1701        next_rx:
1702                np->cur_rx++;
1703                desc->status = 0;
1704                np->rx_done = (np->rx_done + 1) % DONE_Q_SIZE;
1705        }
1706        writew(np->rx_done, dev->base_addr + CompletionQConsumerIdx);
1707
1708 out:
1709        refill_rx_ring(dev);
1710        if (debug > 5)
1711                printk(KERN_DEBUG "  exiting netdev_rx(): %d, status of %d was %#8.8x.\n",
1712                       retcode, np->rx_done, desc_status);
1713        return retcode;
1714}
1715
1716
1717#ifdef HAVE_NETDEV_POLL
1718static int netdev_poll(struct net_device *dev, int *budget)
1719{
1720        u32 intr_status;
1721        long ioaddr = dev->base_addr;
1722        int retcode = 0, quota = dev->quota;
1723
1724        do {
1725                writel(IntrRxDone | IntrRxEmpty, ioaddr + IntrClear);
1726
1727                retcode = __netdev_rx(dev, &quota);
1728                *budget -= (dev->quota - quota);
1729                dev->quota = quota;
1730                if (retcode)
1731                        goto out;
1732
1733                intr_status = readl(ioaddr + IntrStatus);
1734        } while (intr_status & (IntrRxDone | IntrRxEmpty));
1735
1736        netif_rx_complete(dev);
1737        intr_status = readl(ioaddr + IntrEnable);
1738        intr_status |= IntrRxDone | IntrRxEmpty;
1739        writel(intr_status, ioaddr + IntrEnable);
1740
1741 out:
1742        if (debug > 5)
1743                printk(KERN_DEBUG "  exiting netdev_poll(): %d.\n", retcode);
1744
1745        /* Restart Rx engine if stopped. */
1746        return retcode;
1747}
1748#endif /* HAVE_NETDEV_POLL */
1749
1750
1751static void refill_rx_ring(struct net_device *dev)
1752{
1753        struct netdev_private *np = dev->priv;
1754        struct sk_buff *skb;
1755        int entry = -1;
1756
1757        /* Refill the Rx ring buffers. */
1758        for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1759                entry = np->dirty_rx % RX_RING_SIZE;
1760                if (np->rx_info[entry].skb == NULL) {
1761                        skb = dev_alloc_skb(np->rx_buf_sz);
1762                        np->rx_info[entry].skb = skb;
1763                        if (skb == NULL)
1764                                break;  /* Better luck next round. */
1765                        np->rx_info[entry].mapping =
1766                                pci_map_single(np->pci_dev, skb->tail, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1767                        skb->dev = dev; /* Mark as being used by this device. */
1768                        np->rx_ring[entry].rxaddr =
1769                                cpu_to_dma(np->rx_info[entry].mapping | RxDescValid);
1770                }
1771                if (entry == RX_RING_SIZE - 1)
1772                        np->rx_ring[entry].rxaddr |= cpu_to_dma(RxDescEndRing);
1773        }
1774        if (entry >= 0)
1775                writew(entry, dev->base_addr + RxDescQIdx);
1776}
1777
1778
1779static void netdev_media_change(struct net_device *dev)
1780{
1781        struct netdev_private *np = dev->priv;
1782        long ioaddr = dev->base_addr;
1783        u16 reg0, reg1, reg4, reg5;
1784        u32 new_tx_mode;
1785        u32 new_intr_timer_ctrl;
1786
1787        /* reset status first */
1788        mdio_read(dev, np->phys[0], MII_BMCR);
1789        mdio_read(dev, np->phys[0], MII_BMSR);
1790
1791        reg0 = mdio_read(dev, np->phys[0], MII_BMCR);
1792        reg1 = mdio_read(dev, np->phys[0], MII_BMSR);
1793
1794        if (reg1 & BMSR_LSTATUS) {
1795                /* link is up */
1796                if (reg0 & BMCR_ANENABLE) {
1797                        /* autonegotiation is enabled */
1798                        reg4 = mdio_read(dev, np->phys[0], MII_ADVERTISE);
1799                        reg5 = mdio_read(dev, np->phys[0], MII_LPA);
1800                        if (reg4 & ADVERTISE_100FULL && reg5 & LPA_100FULL) {
1801                                np->speed100 = 1;
1802                                np->mii_if.full_duplex = 1;
1803                        } else if (reg4 & ADVERTISE_100HALF && reg5 & LPA_100HALF) {
1804                                np->speed100 = 1;
1805                                np->mii_if.full_duplex = 0;
1806                        } else if (reg4 & ADVERTISE_10FULL && reg5 & LPA_10FULL) {
1807                                np->speed100 = 0;
1808                                np->mii_if.full_duplex = 1;
1809                        } else {
1810                                np->speed100 = 0;
1811                                np->mii_if.full_duplex = 0;
1812                        }
1813                } else {
1814                        /* autonegotiation is disabled */
1815                        if (reg0 & BMCR_SPEED100)
1816                                np->speed100 = 1;
1817                        else
1818                                np->speed100 = 0;
1819                        if (reg0 & BMCR_FULLDPLX)
1820                                np->mii_if.full_duplex = 1;
1821                        else
1822                                np->mii_if.full_duplex = 0;
1823                }
1824                netif_carrier_on(dev);
1825                printk(KERN_DEBUG "%s: Link is up, running at %sMbit %s-duplex\n",
1826                       dev->name,
1827                       np->speed100 ? "100" : "10",
1828                       np->mii_if.full_duplex ? "full" : "half");
1829
1830                new_tx_mode = np->tx_mode & ~FullDuplex;        /* duplex setting */
1831                if (np->mii_if.full_duplex)
1832                        new_tx_mode |= FullDuplex;
1833                if (np->tx_mode != new_tx_mode) {
1834                        np->tx_mode = new_tx_mode;
1835                        writel(np->tx_mode | MiiSoftReset, ioaddr + TxMode);
1836                        udelay(1000);
1837                        writel(np->tx_mode, ioaddr + TxMode);
1838                }
1839
1840                new_intr_timer_ctrl = np->intr_timer_ctrl & ~Timer10X;
1841                if (np->speed100)
1842                        new_intr_timer_ctrl |= Timer10X;
1843                if (np->intr_timer_ctrl != new_intr_timer_ctrl) {
1844                        np->intr_timer_ctrl = new_intr_timer_ctrl;
1845                        writel(new_intr_timer_ctrl, ioaddr + IntrTimerCtrl);
1846                }
1847        } else {
1848                netif_carrier_off(dev);
1849                printk(KERN_DEBUG "%s: Link is down\n", dev->name);
1850        }
1851}
1852
1853
1854static void netdev_error(struct net_device *dev, int intr_status)
1855{
1856        struct netdev_private *np = dev->priv;
1857
1858        /* Came close to underrunning the Tx FIFO, increase threshold. */
1859        if (intr_status & IntrTxDataLow) {
1860                if (np->tx_threshold <= PKT_BUF_SZ / 16) {
1861                        writel(++np->tx_threshold, dev->base_addr + TxThreshold);
1862                        printk(KERN_NOTICE "%s: PCI bus congestion, increasing Tx FIFO threshold to %d bytes\n",
1863                               dev->name, np->tx_threshold * 16);
1864                } else
1865                        printk(KERN_WARNING "%s: PCI Tx underflow -- adapter is probably malfunctioning\n", dev->name);
1866        }
1867        if (intr_status & IntrRxGFPDead) {
1868                np->stats.rx_fifo_errors++;
1869                np->stats.rx_errors++;
1870        }
1871        if (intr_status & (IntrNoTxCsum | IntrDMAErr)) {
1872                np->stats.tx_fifo_errors++;
1873                np->stats.tx_errors++;
1874        }
1875        if ((intr_status & ~(IntrNormalMask | IntrAbnormalSummary | IntrLinkChange | IntrStatsMax | IntrTxDataLow | IntrRxGFPDead | IntrNoTxCsum | IntrPCIPad)) && debug)
1876                printk(KERN_ERR "%s: Something Wicked happened! %#8.8x.\n",
1877                       dev->name, intr_status);
1878}
1879
1880
1881static struct net_device_stats *get_stats(struct net_device *dev)
1882{
1883        long ioaddr = dev->base_addr;
1884        struct netdev_private *np = dev->priv;
1885
1886        /* This adapter architecture needs no SMP locks. */
1887        np->stats.tx_bytes = readl(ioaddr + 0x57010);
1888        np->stats.rx_bytes = readl(ioaddr + 0x57044);
1889        np->stats.tx_packets = readl(ioaddr + 0x57000);
1890        np->stats.tx_aborted_errors =
1891                readl(ioaddr + 0x57024) + readl(ioaddr + 0x57028);
1892        np->stats.tx_window_errors = readl(ioaddr + 0x57018);
1893        np->stats.collisions =
1894                readl(ioaddr + 0x57004) + readl(ioaddr + 0x57008);
1895
1896        /* The chip only need report frame silently dropped. */
1897        np->stats.rx_dropped += readw(ioaddr + RxDMAStatus);
1898        writew(0, ioaddr + RxDMAStatus);
1899        np->stats.rx_crc_errors = readl(ioaddr + 0x5703C);
1900        np->stats.rx_frame_errors = readl(ioaddr + 0x57040);
1901        np->stats.rx_length_errors = readl(ioaddr + 0x57058);
1902        np->stats.rx_missed_errors = readl(ioaddr + 0x5707C);
1903
1904        return &np->stats;
1905}
1906
1907
1908/* Chips may use the upper or lower CRC bits, and may reverse and/or invert
1909   them.  Select the endian-ness that results in minimal calculations.
1910*/
1911static void set_rx_mode(struct net_device *dev)
1912{
1913        long ioaddr = dev->base_addr;
1914        u32 rx_mode = MinVLANPrio;
1915        struct dev_mc_list *mclist;
1916        int i;
1917#ifdef VLAN_SUPPORT
1918        struct netdev_private *np = dev->priv;
1919
1920        rx_mode |= VlanMode;
1921        if (np->vlgrp) {
1922                int vlan_count = 0;
1923                long filter_addr = ioaddr + HashTable + 8;
1924                for (i = 0; i < VLAN_VID_MASK; i++) {
1925                        if (np->vlgrp->vlan_devices[i]) {
1926                                if (vlan_count >= 32)
1927                                        break;
1928                                writew(cpu_to_be16(i), filter_addr);
1929                                filter_addr += 16;
1930                                vlan_count++;
1931                        }
1932                }
1933                if (i == VLAN_VID_MASK) {
1934                        rx_mode |= PerfectFilterVlan;
1935                        while (vlan_count < 32) {
1936                                writew(0, filter_addr);
1937                                filter_addr += 16;
1938                                vlan_count++;
1939                        }
1940                }
1941        }
1942#endif /* VLAN_SUPPORT */
1943
1944        if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1945                rx_mode |= AcceptAll;
1946        } else if ((dev->mc_count > multicast_filter_limit)
1947                   || (dev->flags & IFF_ALLMULTI)) {
1948                /* Too many to match, or accept all multicasts. */
1949                rx_mode |= AcceptBroadcast|AcceptAllMulticast|PerfectFilter;
1950        } else if (dev->mc_count <= 14) {
1951                /* Use the 16 element perfect filter, skip first two entries. */
1952                long filter_addr = ioaddr + PerfFilterTable + 2 * 16;
1953                u16 *eaddrs;
1954                for (i = 2, mclist = dev->mc_list; mclist && i < dev->mc_count + 2;
1955                     i++, mclist = mclist->next) {
1956                        eaddrs = (u16 *)mclist->dmi_addr;
1957                        writew(cpu_to_be16(eaddrs[2]), filter_addr); filter_addr += 4;
1958                        writew(cpu_to_be16(eaddrs[1]), filter_addr); filter_addr += 4;
1959                        writew(cpu_to_be16(eaddrs[0]), filter_addr); filter_addr += 8;
1960                }
1961                eaddrs = (u16 *)dev->dev_addr;
1962                while (i++ < 16) {
1963                        writew(cpu_to_be16(eaddrs[0]), filter_addr); filter_addr += 4;
1964                        writew(cpu_to_be16(eaddrs[1]), filter_addr); filter_addr += 4;
1965                        writew(cpu_to_be16(eaddrs[2]), filter_addr); filter_addr += 8;
1966                }
1967                rx_mode |= AcceptBroadcast|PerfectFilter;
1968        } else {
1969                /* Must use a multicast hash table. */
1970                long filter_addr;
1971                u16 *eaddrs;
1972                u16 mc_filter[32] __attribute__ ((aligned(sizeof(long))));      /* Multicast hash filter */
1973
1974                memset(mc_filter, 0, sizeof(mc_filter));
1975                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1976                     i++, mclist = mclist->next) {
1977                        int bit_nr = ether_crc_le(ETH_ALEN, mclist->dmi_addr) >> 23;
1978                        __u32 *fptr = (__u32 *) &mc_filter[(bit_nr >> 4) & ~1];
1979
1980                        *fptr |= cpu_to_le32(1 << (bit_nr & 31));
1981                }
1982                /* Clear the perfect filter list, skip first two entries. */
1983                filter_addr = ioaddr + PerfFilterTable + 2 * 16;
1984                eaddrs = (u16 *)dev->dev_addr;
1985                for (i = 2; i < 16; i++) {
1986                        writew(cpu_to_be16(eaddrs[0]), filter_addr); filter_addr += 4;
1987                        writew(cpu_to_be16(eaddrs[1]), filter_addr); filter_addr += 4;
1988                        writew(cpu_to_be16(eaddrs[2]), filter_addr); filter_addr += 8;
1989                }
1990                for (filter_addr = ioaddr + HashTable, i = 0; i < 32; filter_addr+= 16, i++)
1991                        writew(mc_filter[i], filter_addr);
1992                rx_mode |= AcceptBroadcast|PerfectFilter|HashFilter;
1993        }
1994        writel(rx_mode, ioaddr + RxFilterMode);
1995}
1996
1997
1998static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1999{
2000        struct ethtool_cmd ecmd;
2001        struct netdev_private *np = dev->priv;
2002
2003        if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
2004                return -EFAULT;
2005
2006        switch (ecmd.cmd) {
2007        case ETHTOOL_GDRVINFO: {
2008                struct ethtool_drvinfo info;
2009                memset(&info, 0, sizeof(info));
2010                info.cmd = ecmd.cmd;
2011                strcpy(info.driver, DRV_NAME);
2012                strcpy(info.version, DRV_VERSION);
2013                *info.fw_version = 0;
2014                strcpy(info.bus_info, PCI_SLOT_NAME(np->pci_dev));
2015                if (copy_to_user(useraddr, &info, sizeof(info)))
2016                       return -EFAULT;
2017                return 0;
2018        }
2019
2020        /* get settings */
2021        case ETHTOOL_GSET: {
2022                struct ethtool_cmd ecmd = { ETHTOOL_GSET };
2023                spin_lock_irq(&np->lock);
2024                mii_ethtool_gset(&np->mii_if, &ecmd);
2025                spin_unlock_irq(&np->lock);
2026                if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
2027                        return -EFAULT;
2028                return 0;
2029        }
2030        /* set settings */
2031        case ETHTOOL_SSET: {
2032                int r;
2033                struct ethtool_cmd ecmd;
2034                if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
2035                        return -EFAULT;
2036                spin_lock_irq(&np->lock);
2037                r = mii_ethtool_sset(&np->mii_if, &ecmd);
2038                spin_unlock_irq(&np->lock);
2039                check_duplex(dev);
2040                return r;
2041        }
2042        /* restart autonegotiation */
2043        case ETHTOOL_NWAY_RST: {
2044                return mii_nway_restart(&np->mii_if);
2045        }
2046        /* get link status */
2047        case ETHTOOL_GLINK: {
2048                struct ethtool_value edata = {ETHTOOL_GLINK};
2049                edata.data = mii_link_ok(&np->mii_if);
2050                if (copy_to_user(useraddr, &edata, sizeof(edata)))
2051                        return -EFAULT;
2052                return 0;
2053        }
2054
2055        /* get message-level */
2056        case ETHTOOL_GMSGLVL: {
2057                struct ethtool_value edata = {ETHTOOL_GMSGLVL};
2058                edata.data = debug;
2059                if (copy_to_user(useraddr, &edata, sizeof(edata)))
2060                        return -EFAULT;
2061                return 0;
2062        }
2063        /* set message-level */
2064        case ETHTOOL_SMSGLVL: {
2065                struct ethtool_value edata;
2066                if (copy_from_user(&edata, useraddr, sizeof(edata)))
2067                        return -EFAULT;
2068                debug = edata.data;
2069                return 0;
2070        }
2071        default:
2072                return -EOPNOTSUPP;
2073        }
2074}
2075
2076
2077static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2078{
2079        struct netdev_private *np = dev->priv;
2080        struct mii_ioctl_data *data = (struct mii_ioctl_data *) & rq->ifr_data;
2081        int rc;
2082
2083        if (!netif_running(dev))
2084                return -EINVAL;
2085
2086        if (cmd == SIOCETHTOOL)
2087                rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
2088
2089        else {
2090                spin_lock_irq(&np->lock);
2091                rc = generic_mii_ioctl(&np->mii_if, data, cmd, NULL);
2092                spin_unlock_irq(&np->lock);
2093
2094                if ((cmd == SIOCSMIIREG) && (data->phy_id == np->phys[0]))
2095                        check_duplex(dev);
2096        }
2097
2098        return rc;
2099}
2100
2101static int netdev_close(struct net_device *dev)
2102{
2103        long ioaddr = dev->base_addr;
2104        struct netdev_private *np = dev->priv;
2105        int i;
2106
2107        netif_stop_queue(dev);
2108        netif_stop_if(dev);
2109
2110        if (debug > 1) {
2111                printk(KERN_DEBUG "%s: Shutting down ethercard, Intr status %#8.8x.\n",
2112                           dev->name, (int) readl(ioaddr + IntrStatus));
2113                printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n",
2114                       dev->name, np->cur_tx, np->dirty_tx,
2115                       np->cur_rx, np->dirty_rx);
2116        }
2117
2118        /* Disable interrupts by clearing the interrupt mask. */
2119        writel(0, ioaddr + IntrEnable);
2120
2121        /* Stop the chip's Tx and Rx processes. */
2122        writel(0, ioaddr + GenCtrl);
2123        readl(ioaddr + GenCtrl);
2124
2125        if (debug > 5) {
2126                printk(KERN_DEBUG"  Tx ring at %#llx:\n",
2127                       (long long) np->tx_ring_dma);
2128                for (i = 0; i < 8 /* TX_RING_SIZE is huge! */; i++)
2129                        printk(KERN_DEBUG " #%d desc. %#8.8x %#llx -> %#8.8x.\n",
2130                               i, le32_to_cpu(np->tx_ring[i].status),
2131                               (long long) dma_to_cpu(np->tx_ring[i].addr),
2132                               le32_to_cpu(np->tx_done_q[i].status));
2133                printk(KERN_DEBUG "  Rx ring at %#llx -> %p:\n",
2134                       (long long) np->rx_ring_dma, np->rx_done_q);
2135                if (np->rx_done_q)
2136                        for (i = 0; i < 8 /* RX_RING_SIZE */; i++) {
2137                                printk(KERN_DEBUG " #%d desc. %#llx -> %#8.8x\n",
2138                                       i, (long long) dma_to_cpu(np->rx_ring[i].rxaddr), le32_to_cpu(np->rx_done_q[i].status));
2139                }
2140        }
2141
2142        free_irq(dev->irq, dev);
2143
2144        /* Free all the skbuffs in the Rx queue. */
2145        for (i = 0; i < RX_RING_SIZE; i++) {
2146                np->rx_ring[i].rxaddr = cpu_to_dma(0xBADF00D0); /* An invalid address. */
2147                if (np->rx_info[i].skb != NULL) {
2148                        pci_unmap_single(np->pci_dev, np->rx_info[i].mapping, np->rx_buf_sz, PCI_DMA_FROMDEVICE);
2149                        dev_kfree_skb(np->rx_info[i].skb);
2150                }
2151                np->rx_info[i].skb = NULL;
2152                np->rx_info[i].mapping = 0;
2153        }
2154        for (i = 0; i < TX_RING_SIZE; i++) {
2155                struct sk_buff *skb = np->tx_info[i].skb;
2156                if (skb == NULL)
2157                        continue;
2158                pci_unmap_single(np->pci_dev,
2159                                 np->tx_info[i].mapping,
2160                                 skb_first_frag_len(skb), PCI_DMA_TODEVICE);
2161                np->tx_info[i].mapping = 0;
2162                dev_kfree_skb(skb);
2163                np->tx_info[i].skb = NULL;
2164        }
2165
2166        COMPAT_MOD_DEC_USE_COUNT;
2167
2168        return 0;
2169}
2170
2171
2172static void __devexit starfire_remove_one (struct pci_dev *pdev)
2173{
2174        struct net_device *dev = pci_get_drvdata(pdev);
2175        struct netdev_private *np;
2176
2177        if (!dev)
2178                BUG();
2179
2180        np = dev->priv;
2181        if (np->queue_mem)
2182                pci_free_consistent(pdev, np->queue_mem_size, np->queue_mem, np->queue_mem_dma);
2183
2184        unregister_netdev(dev);
2185
2186        /* XXX: add wakeup code -- requires firmware for MagicPacket */
2187        pci_set_power_state(pdev, 3);   /* go to sleep in D3 mode */
2188        pci_disable_device(pdev);
2189
2190        iounmap((char *)dev->base_addr);
2191        pci_release_regions(pdev);
2192
2193        pci_set_drvdata(pdev, NULL);
2194        free_netdev(dev);                       /* Will also free np!! */
2195}
2196
2197
2198static struct pci_driver starfire_driver = {
2199        .name           = DRV_NAME,
2200        .probe          = starfire_init_one,
2201        .remove         = __devexit_p(starfire_remove_one),
2202        .id_table       = starfire_pci_tbl,
2203};
2204
2205
2206static int __init starfire_init (void)
2207{
2208/* when a module, this is printed whether or not devices are found in probe */
2209#ifdef MODULE
2210        printk(version);
2211#endif
2212#ifndef ADDR_64BITS
2213        /* we can do this test only at run-time... sigh */
2214        if (sizeof(dma_addr_t) == sizeof(u64)) {
2215                printk("This driver has not been ported to this 64-bit architecture yet\n");
2216                return -ENODEV;
2217        }
2218#endif /* not ADDR_64BITS */
2219#ifndef HAS_FIRMWARE
2220        /* unconditionally disable hw cksums if firmware is not present */
2221        enable_hw_cksum = 0;
2222#endif /* not HAS_FIRMWARE */
2223        return pci_module_init (&starfire_driver);
2224}
2225
2226
2227static void __exit starfire_cleanup (void)
2228{
2229        pci_unregister_driver (&starfire_driver);
2230}
2231
2232
2233module_init(starfire_init);
2234module_exit(starfire_cleanup);
2235
2236
2237/*
2238 * Local variables:
2239 *  c-basic-offset: 8
2240 *  tab-width: 8
2241 * End:
2242 */
2243
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.