Annotation of Gnu-Mach/linux/src/drivers/net/via-rhine.c, revision 1.1.1.2

1.1       root        1: /* via-rhine.c: A Linux Ethernet device driver for VIA Rhine family chips. */
                      2: /*
1.1.1.2 ! root        3:        Written 1998-2003 by Donald Becker.
1.1       root        4: 
1.1.1.2 ! root        5:        This software may be used and distributed according to the terms of
        !             6:        the GNU General Public License (GPL), incorporated herein by reference.
        !             7:        Drivers based on or derived from this code fall under the GPL and must
        !             8:        retain the authorship, copyright and license notice.  This file is not
        !             9:        a complete program and may only be used when the entire operating
        !            10:        system is licensed under the GPL.
1.1       root       11: 
                     12:        This driver is designed for the VIA VT86c100A Rhine-II PCI Fast Ethernet
                     13:        controller.  It also works with the older 3043 Rhine-I chip.
                     14: 
1.1.1.2 ! root       15:        The author may be reached as [email protected], or C/O
        !            16:        Scyld Computing Corporation
        !            17:        914 Bay Ridge Road, Suite 220
1.1       root       18:        Annapolis MD 21403
                     19: 
1.1.1.2 ! root       20:        Support information and updates available at
        !            21:                http://www.scyld.com/network/via-rhine.html
        !            22:        The information and support mailing lists are based at
        !            23:                http://www.scyld.com/mailman/listinfo/
1.1       root       24: */
                     25: 
1.1.1.2 ! root       26: /* These identify the driver base version and may not be removed. */
        !            27: static const char version1[] =
        !            28: "via-rhine.c:v1.16 7/22/2003  Written by Donald Becker <[email protected]>\n";
        !            29: static const char version2[] =
        !            30: "  http://www.scyld.com/network/via-rhine.html\n";
        !            31: 
        !            32: /* Automatically extracted configuration info:
        !            33: probe-func: via_rhine_probe
        !            34: config-in: tristate 'VIA "Rhine" vt86c100, vt3043, and vt3065 series PCI Ethernet support' CONFIG_VIA_RHINE
        !            35: 
        !            36: c-help-name: VIA Rhine series PCI Ethernet support
        !            37: c-help-symbol: CONFIG_VIA_RHINE
        !            38: c-help: This driver is for the VIA Rhine (v3043) and Rhine-II
        !            39: c-help: (vt3065 AKA vt86c100) network adapter chip series.
        !            40: c-help: More specific information and updates are available from 
        !            41: c-help: http://www.scyld.com/network/via-rhine.html
        !            42: */
        !            43: 
        !            44: /* The user-configurable values.
        !            45:    These may be modified when a driver module is loaded.*/
1.1       root       46: 
1.1.1.2 ! root       47: /* Message enable level: 0..31 = no..all messages.  See NETIF_MSG docs. */
        !            48: static int debug = 2;
1.1       root       49: 
1.1.1.2 ! root       50: /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
1.1       root       51: static int max_interrupt_work = 20;
                     52: 
                     53: /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
                     54:    Setting to > 1518 effectively disables this feature. */
                     55: static int rx_copybreak = 0;
                     56: 
                     57: /* Used to pass the media type, etc.
                     58:    Both 'options[]' and 'full_duplex[]' should exist for driver
                     59:    interoperability.
                     60:    The media type is usually passed in 'options[]'.
1.1.1.2 ! root       61:     The default is autonegotation for speed and duplex.
        !            62:        This should rarely be overridden.
        !            63:     Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps.
        !            64:     Use option values 0x10 and 0x100 for forcing half duplex fixed speed.
        !            65:     Use option values 0x20 and 0x200 for forcing full duplex operation.
1.1       root       66: */
                     67: #define MAX_UNITS 8            /* More are supported, limit only on options */
                     68: static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
                     69: static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
                     70: 
                     71: /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
                     72:    The Rhine has a 64 element 8390-like hash table.  */
                     73: static const int multicast_filter_limit = 32;
                     74: 
                     75: /* Operational parameters that are set at compile time. */
                     76: 
1.1.1.2 ! root       77: /* Making the Tx ring too large decreases the effectiveness of channel
1.1       root       78:    bonding and packet priority.
                     79:    There are no ill effects from too-large receive rings. */
1.1.1.2 ! root       80: #define TX_RING_SIZE   16
        !            81: #define TX_QUEUE_LEN   10              /* Limit ring entries actually used.  */
        !            82: #define RX_RING_SIZE   32
1.1       root       83: 
                     84: /* Operational parameters that usually are not changed. */
                     85: /* Time in jiffies before concluding the transmitter is hung. */
1.1.1.2 ! root       86: #define TX_TIMEOUT  (6*HZ)
1.1       root       87: 
1.1.1.2 ! root       88: /* Allocation size of Rx buffers with normal sized Ethernet frames.
        !            89:    Do not change this value without good reason.  This is not a limit,
        !            90:    but a way to keep a consistent allocation size among drivers.
        !            91:  */
        !            92: #define PKT_BUF_SZ             1536
        !            93: 
        !            94: #ifndef __KERNEL__
        !            95: #define __KERNEL__
        !            96: #endif
        !            97: #if !defined(__OPTIMIZE__)
        !            98: #warning  You must compile this file with the correct options!
        !            99: #warning  See the last lines of the source file.
        !           100: #error You must compile this driver with "-O".
        !           101: #endif
1.1       root      102: 
                    103: /* Include files, designed to support most kernel versions 2.0.0 and later. */
                    104: #include <linux/config.h>
1.1.1.2 ! root      105: #if defined(CONFIG_SMP) && ! defined(__SMP__)
        !           106: #define __SMP__
        !           107: #endif
        !           108: #if defined(MODULE) && defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
        !           109: #define MODVERSIONS
        !           110: #endif
        !           111: 
1.1       root      112: #include <linux/version.h>
1.1.1.2 ! root      113: #if defined(MODVERSIONS)
1.1       root      114: #include <linux/modversions.h>
                    115: #endif
                    116: #include <linux/module.h>
                    117: 
                    118: #include <linux/kernel.h>
                    119: #include <linux/string.h>
                    120: #include <linux/timer.h>
                    121: #include <linux/errno.h>
                    122: #include <linux/ioport.h>
1.1.1.2 ! root      123: #if LINUX_VERSION_CODE >= 0x20400
        !           124: #include <linux/slab.h>
        !           125: #else
1.1       root      126: #include <linux/malloc.h>
1.1.1.2 ! root      127: #endif
1.1       root      128: #include <linux/interrupt.h>
                    129: #include <linux/pci.h>
                    130: #include <linux/netdevice.h>
                    131: #include <linux/etherdevice.h>
                    132: #include <linux/skbuff.h>
                    133: #include <asm/processor.h>             /* Processor type for cache alignment. */
                    134: #include <asm/bitops.h>
                    135: #include <asm/io.h>
                    136: 
1.1.1.2 ! root      137: #ifdef INLINE_PCISCAN
        !           138: #include "k_compat.h"
        !           139: #else
        !           140: #include "pci-scan.h"
        !           141: #include "kern_compat.h"
        !           142: #endif
        !           143: 
        !           144: /* Condensed bus+endian portability operations. */
        !           145: #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
        !           146: #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
        !           147: 
        !           148: /* This driver was written to use PCI memory space, however most versions
        !           149:    of the Rhine only work correctly with I/O space accesses. */
        !           150: #if defined(VIA_USE_MEMORY)
        !           151: #warning Many adapters using the VIA Rhine chip are not configured to work
        !           152: #warning with PCI memory space accesses.
        !           153: #else
        !           154: #define USE_IO_OPS
1.1       root      155: #undef readb
                    156: #undef readw
                    157: #undef readl
                    158: #undef writeb
                    159: #undef writew
                    160: #undef writel
                    161: #define readb inb
                    162: #define readw inw
                    163: #define readl inl
                    164: #define writeb outb
                    165: #define writew outw
                    166: #define writel outl
                    167: #endif
                    168: 
1.1.1.2 ! root      169: #if (LINUX_VERSION_CODE >= 0x20100)  &&  defined(MODULE)
1.1       root      170: char kernel_version[] = UTS_RELEASE;
                    171: #endif
1.1.1.2 ! root      172: 
        !           173: MODULE_AUTHOR("Donald Becker <[email protected]>");
1.1       root      174: MODULE_DESCRIPTION("VIA Rhine PCI Fast Ethernet driver");
1.1.1.2 ! root      175: MODULE_LICENSE("GPL");
1.1       root      176: MODULE_PARM(max_interrupt_work, "i");
                    177: MODULE_PARM(debug, "i");
                    178: MODULE_PARM(rx_copybreak, "i");
                    179: MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
                    180: MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
1.1.1.2 ! root      181: MODULE_PARM(multicast_filter_limit, "i");
        !           182: MODULE_PARM_DESC(debug, "Driver message level (0-31)");
        !           183: MODULE_PARM_DESC(options, "Force transceiver type or fixed speed+duplex");
        !           184: MODULE_PARM_DESC(max_interrupt_work,
        !           185:                                 "Driver maximum events handled per interrupt");
        !           186: MODULE_PARM_DESC(full_duplex, "Non-zero to set forced full duplex "
        !           187:                                 "(deprecated, use options[] instead).");
        !           188: MODULE_PARM_DESC(rx_copybreak,
        !           189:                                 "Breakpoint in bytes for copy-only-tiny-frames");
        !           190: MODULE_PARM_DESC(multicast_filter_limit,
        !           191:                                 "Multicast addresses before switching to Rx-all-multicast");
1.1       root      192: 
                    193: /*
                    194:                                Theory of Operation
                    195: 
                    196: I. Board Compatibility
                    197: 
                    198: This driver is designed for the VIA 86c100A Rhine-II PCI Fast Ethernet
                    199: controller.
                    200: 
                    201: II. Board-specific settings
                    202: 
                    203: Boards with this chip are functional only in a bus-master PCI slot.
                    204: 
                    205: Many operational settings are loaded from the EEPROM to the Config word at
                    206: offset 0x78.  This driver assumes that they are correct.
                    207: If this driver is compiled to use PCI memory space operations the EEPROM
                    208: must be configured to enable memory ops.
                    209: 
                    210: III. Driver operation
                    211: 
                    212: IIIa. Ring buffers
                    213: 
                    214: This driver uses two statically allocated fixed-size descriptor lists
                    215: formed into rings by a branch from the final descriptor to the beginning of
                    216: the list.  The ring sizes are set at compile time by RX/TX_RING_SIZE.
                    217: 
                    218: IIIb/c. Transmit/Receive Structure
                    219: 
                    220: This driver attempts to use a zero-copy receive and transmit scheme.
                    221: 
                    222: Alas, all data buffers are required to start on a 32 bit boundary, so
                    223: the driver must often copy transmit packets into bounce buffers.
                    224: 
                    225: The driver allocates full frame size skbuffs for the Rx ring buffers at
                    226: open() time and passes the skb->data field to the chip as receive data
                    227: buffers.  When an incoming frame is less than RX_COPYBREAK bytes long,
                    228: a fresh skbuff is allocated and the frame is copied to the new skbuff.
                    229: When the incoming frame is larger, the skbuff is passed directly up the
                    230: protocol stack.  Buffers consumed this way are replaced by newly allocated
                    231: skbuffs in the last phase of netdev_rx().
                    232: 
                    233: The RX_COPYBREAK value is chosen to trade-off the memory wasted by
                    234: using a full-sized skbuff for small frames vs. the copying costs of larger
                    235: frames.  New boards are typically used in generously configured machines
                    236: and the underfilled buffers have negligible impact compared to the benefit of
                    237: a single allocation size, so the default value of zero results in never
                    238: copying packets.  When copying is done, the cost is usually mitigated by using
                    239: a combined copy/checksum routine.  Copying also preloads the cache, which is
                    240: most useful with small frames.
                    241: 
                    242: Since the VIA chips are only able to transfer data to buffers on 32 bit
                    243: boundaries, the the IP header at offset 14 in an ethernet frame isn't
                    244: longword aligned for further processing.  Copying these unaligned buffers
                    245: has the beneficial effect of 16-byte aligning the IP header.
                    246: 
                    247: IIId. Synchronization
                    248: 
                    249: The driver runs as two independent, single-threaded flows of control.  One
                    250: is the send-packet routine, which enforces single-threaded use by the
                    251: dev->tbusy flag.  The other thread is the interrupt handler, which is single
                    252: threaded by the hardware and interrupt handling software.
                    253: 
                    254: The send packet thread has partial control over the Tx ring and 'dev->tbusy'
                    255: flag.  It sets the tbusy flag whenever it's queuing a Tx packet. If the next
                    256: queue slot is empty, it clears the tbusy flag when finished otherwise it sets
                    257: the 'lp->tx_full' flag.
                    258: 
                    259: The interrupt handler has exclusive control over the Rx ring and records stats
                    260: from the Tx ring.  After reaping the stats, it marks the Tx queue entry as
                    261: empty by incrementing the dirty_tx mark. Iff the 'lp->tx_full' flag is set, it
                    262: clears both the tx_full and tbusy flags.
                    263: 
                    264: IV. Notes
                    265: 
                    266: IVb. References
                    267: 
1.1.1.2 ! root      268: This driver was originally written using a preliminary VT86C100A manual
        !           269: from
        !           270:   http://www.via.com.tw/ 
        !           271: The usual background material was used:
        !           272:   http://www.scyld.com/expert/100mbps.html
        !           273:   http://scyld.com/expert/NWay.html
        !           274: 
        !           275: Additional information is now available, especially for the newer chips.
        !           276:    http://www.via.com.tw/en/Networking/DS6105LOM100.pdf
1.1       root      277: 
                    278: IVc. Errata
                    279: 
                    280: The VT86C100A manual is not reliable information.
1.1.1.2 ! root      281: The 3043 chip does not handle unaligned transmit or receive buffers,
        !           282: resulting in significant performance degradation for bounce buffer
        !           283: copies on transmit and unaligned IP headers on receive.
1.1       root      284: The chip does not pad to minimum transmit length.
                    285: 
1.1.1.2 ! root      286: There is a bug with the transmit descriptor pointer handling when the
        !           287: chip encounters a transmit error.
        !           288: 
1.1       root      289: */
                    290: 
                    291: 
                    292: 
1.1.1.2 ! root      293: static void *via_probe1(struct pci_dev *pdev, void *init_dev,
        !           294:                                                long ioaddr, int irq, int chip_idx, int find_cnt);
        !           295: static int via_pwr_event(void *dev_instance, int event);
        !           296: enum chip_capability_flags {
        !           297:        CanHaveMII=1, HasESIPhy=2, HasDavicomPhy=4, HasV1TxStat=8,
        !           298:        ReqTxAlign=0x10, HasWOL=0x20, HasIPChecksum=0x40, HasVLAN=0x80,
        !           299:        
1.1       root      300: };
                    301: 
1.1.1.2 ! root      302: #if defined(VIA_USE_MEMORY)
        !           303: #define RHINE_IOTYPE (PCI_USES_MEM | PCI_USES_MASTER | PCI_ADDR1)
        !           304: #define RHINE_I_IOSIZE 128
        !           305: #define RHINEII_IOSIZE 4096
        !           306: #else
        !           307: #define RHINE_IOTYPE (PCI_USES_IO  | PCI_USES_MASTER | PCI_ADDR0)
        !           308: #define RHINE_I_IOSIZE 128
        !           309: #define RHINEII_IOSIZE 256
        !           310: #endif
1.1       root      311: 
                    312: static struct pci_id_info pci_tbl[] = {
1.1.1.2 ! root      313:        { "VIA VT3043 Rhine", { 0x30431106, 0xffffffff,},
        !           314:          RHINE_IOTYPE, RHINE_I_IOSIZE, CanHaveMII | ReqTxAlign | HasV1TxStat },
        !           315:        { "VIA VT86C100A Rhine", { 0x61001106, 0xffffffff,},
        !           316:          RHINE_IOTYPE, RHINE_I_IOSIZE, CanHaveMII | ReqTxAlign | HasV1TxStat },
        !           317:        { "VIA VT6102 Rhine-II", { 0x30651106, 0xffffffff,},
        !           318:          RHINE_IOTYPE, RHINEII_IOSIZE, CanHaveMII | HasWOL },
        !           319:        { "VIA VT6105LOM Rhine-III (3106)", { 0x31061106, 0xffffffff,},
        !           320:          RHINE_IOTYPE, RHINEII_IOSIZE, CanHaveMII | HasWOL },
        !           321:        /* Duplicate entry, with 'M' features enabled. */
        !           322:        { "VIA VT6105M Rhine-III (3106)", { 0x31061106, 0xffffffff,},
        !           323:          RHINE_IOTYPE, RHINEII_IOSIZE, CanHaveMII|HasWOL|HasIPChecksum|HasVLAN},
        !           324:        { "VIA VT6105M Rhine-III (3053 prototype)", { 0x30531106, 0xffffffff,},
        !           325:          RHINE_IOTYPE, RHINEII_IOSIZE, CanHaveMII | HasWOL },
1.1       root      326:        {0,},                                           /* 0 terminated list. */
                    327: };
                    328: 
1.1.1.2 ! root      329: struct drv_id_info via_rhine_drv_id = {
        !           330:        "via-rhine", PCI_HOTSWAP, PCI_CLASS_NETWORK_ETHERNET<<8, pci_tbl,
        !           331:        via_probe1, via_pwr_event
1.1       root      332: };
                    333: 
                    334: /* Offsets to the device registers.
                    335: */
                    336: enum register_offsets {
                    337:        StationAddr=0x00, RxConfig=0x06, TxConfig=0x07, ChipCmd=0x08,
                    338:        IntrStatus=0x0C, IntrEnable=0x0E,
                    339:        MulticastFilter0=0x10, MulticastFilter1=0x14,
                    340:        RxRingPtr=0x18, TxRingPtr=0x1C,
1.1.1.2 ! root      341:        MIIPhyAddr=0x6C, MIIStatus=0x6D, PCIBusConfig=0x6E,
        !           342:        MIICmd=0x70, MIIRegAddr=0x71, MIIData=0x72, MACRegEEcsr=0x74,
        !           343:        Config=0x78, ConfigA=0x7A, RxMissed=0x7C, RxCRCErrs=0x7E,
        !           344:        StickyHW=0x83, WOLcrClr=0xA4, WOLcgClr=0xA7, PwrcsrClr=0xAC,
1.1       root      345: };
                    346: 
                    347: /* Bits in the interrupt status/mask registers. */
                    348: enum intr_status_bits {
                    349:        IntrRxDone=0x0001, IntrRxErr=0x0004, IntrRxEmpty=0x0020,
                    350:        IntrTxDone=0x0002, IntrTxAbort=0x0008, IntrTxUnderrun=0x0010,
                    351:        IntrPCIErr=0x0040,
                    352:        IntrStatsMax=0x0080, IntrRxEarly=0x0100, IntrMIIChange=0x0200,
                    353:        IntrRxOverflow=0x0400, IntrRxDropped=0x0800, IntrRxNoBuf=0x1000,
                    354:        IntrTxAborted=0x2000, IntrLinkChange=0x4000,
                    355:        IntrRxWakeUp=0x8000,
1.1.1.2 ! root      356:        IntrNormalSummary=0x0003, IntrAbnormalSummary=0xC260,
1.1       root      357: };
                    358: 
                    359: /* The Rx and Tx buffer descriptors. */
                    360: struct rx_desc {
1.1.1.2 ! root      361:        s32 rx_status;
1.1       root      362:        u32 desc_length;
                    363:        u32 addr;
                    364:        u32 next_desc;
                    365: };
                    366: struct tx_desc {
1.1.1.2 ! root      367:        s32 tx_status;
1.1       root      368:        u32 desc_length;
                    369:        u32 addr;
                    370:        u32 next_desc;
                    371: };
                    372: 
                    373: /* Bits in *_desc.status */
                    374: enum rx_status_bits {
1.1.1.2 ! root      375:        RxOK=0x8000, RxWholePkt=0x0300, RxErr=0x008F};
1.1       root      376: enum desc_status_bits {
1.1.1.2 ! root      377:        DescOwn=0x80000000, DescEndPacket=0x4000, DescIntr=0x1000,
        !           378: };
        !           379: 
        !           380: /* Bits in rx.desc_length for extended status. */
        !           381: enum rx_info_bits {
        !           382:        RxTypeTag=0x00010000,
        !           383:        RxTypeUDP=0x00020000, RxTypeTCP=0x00040000, RxTypeIP=0x00080000,
        !           384:        RxTypeUTChksumOK=0x00100000, RxTypeIPChksumOK=0x00200000,
        !           385:        /* Summarized. */
        !           386:        RxTypeCsumMask=0x003E0000,
        !           387:        RxTypeUDPSumOK=0x003A0000, RxTypeTCPSumOK=0x003C0000, 
1.1       root      388: };
                    389: 
                    390: /* Bits in ChipCmd. */
                    391: enum chip_cmd_bits {
                    392:        CmdInit=0x0001, CmdStart=0x0002, CmdStop=0x0004, CmdRxOn=0x0008,
                    393:        CmdTxOn=0x0010, CmdTxDemand=0x0020, CmdRxDemand=0x0040,
                    394:        CmdEarlyRx=0x0100, CmdEarlyTx=0x0200, CmdFDuplex=0x0400,
                    395:        CmdNoTxPoll=0x0800, CmdReset=0x8000,
                    396: };
                    397: 
1.1.1.2 ! root      398: #define PRIV_ALIGN     15      /* Required alignment mask */
        !           399: /* Use  __attribute__((aligned (L1_CACHE_BYTES)))  to maintain alignment
        !           400:    within the structure. */
1.1       root      401: struct netdev_private {
                    402:        /* Descriptor rings first for alignment. */
                    403:        struct rx_desc rx_ring[RX_RING_SIZE];
                    404:        struct tx_desc tx_ring[TX_RING_SIZE];
                    405:        /* The addresses of receive-in-place skbuffs. */
                    406:        struct sk_buff* rx_skbuff[RX_RING_SIZE];
                    407:        /* The saved address of a sent-in-place packet/buffer, for later free(). */
                    408:        struct sk_buff* tx_skbuff[TX_RING_SIZE];
                    409:        unsigned char *tx_buf[TX_RING_SIZE];    /* Tx bounce buffers */
                    410:        unsigned char *tx_bufs;                         /* Tx bounce buffer region. */
1.1.1.2 ! root      411:        struct net_device *next_module;         /* Link for devices of this type. */
        !           412:        void *priv_addr;                                        /* Unaligned address for kfree */
1.1       root      413:        struct net_device_stats stats;
                    414:        struct timer_list timer;        /* Media monitoring timer. */
1.1.1.2 ! root      415:        int msg_level;
        !           416:        int max_interrupt_work;
        !           417:        int intr_enable;
        !           418:        int chip_id, drv_flags;
        !           419:        struct pci_dev *pci_dev;
        !           420: 
1.1       root      421:        /* Frequently used values: keep some adjacent for cache effect. */
1.1.1.2 ! root      422: 
1.1       root      423:        struct rx_desc *rx_head_desc;
                    424:        unsigned int cur_rx, dirty_rx;          /* Producer/consumer ring indices */
                    425:        unsigned int rx_buf_sz;                         /* Based on MTU+slack. */
1.1.1.2 ! root      426:        int rx_copybreak;
        !           427: 
        !           428:        unsigned int cur_tx, dirty_tx;
1.1       root      429:        u16 chip_cmd;                                           /* Current setting for ChipCmd */
1.1.1.2 ! root      430:        int multicast_filter_limit;
        !           431:        u32 mc_filter[2];
        !           432:        int rx_mode;
1.1       root      433:        unsigned int tx_full:1;                         /* The Tx queue is full. */
                    434:        /* These values are keep track of the transceiver/media in use. */
                    435:        unsigned int full_duplex:1;                     /* Full-duplex operation requested. */
                    436:        unsigned int duplex_lock:1;
                    437:        unsigned int medialock:1;                       /* Do not sense media. */
1.1.1.2 ! root      438:        unsigned int default_port;                      /* Last dev->if_port value. */
1.1       root      439:        u8 tx_thresh, rx_thresh;
                    440:        /* MII transceiver section. */
                    441:        int mii_cnt;                                            /* MII device addresses. */
                    442:        u16 advertising;                                        /* NWay media advertisement */
                    443:        unsigned char phys[2];                          /* MII device addresses. */
                    444: };
                    445: 
1.1.1.2 ! root      446: static int  mdio_read(struct net_device *dev, int phy_id, int location);
        !           447: static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
        !           448: static int  netdev_open(struct net_device *dev);
        !           449: static void check_duplex(struct net_device *dev);
1.1       root      450: static void netdev_timer(unsigned long data);
1.1.1.2 ! root      451: static void tx_timeout(struct net_device *dev);
        !           452: static void init_ring(struct net_device *dev);
        !           453: static int  start_tx(struct sk_buff *skb, struct net_device *dev);
1.1       root      454: static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
1.1.1.2 ! root      455: static int  netdev_rx(struct net_device *dev);
        !           456: static void netdev_error(struct net_device *dev, int intr_status);
        !           457: static void set_rx_mode(struct net_device *dev);
        !           458: static struct net_device_stats *get_stats(struct net_device *dev);
        !           459: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
        !           460: static int  netdev_close(struct net_device *dev);
1.1       root      461: 
                    462: 
                    463: 
                    464: /* A list of our installed devices, for removing the driver module. */
1.1.1.2 ! root      465: static struct net_device *root_net_dev = NULL;
1.1       root      466: 
                    467: #ifndef MODULE
1.1.1.2 ! root      468: int via_rhine_probe(struct net_device *dev)
1.1       root      469: {
1.1.1.2 ! root      470:        printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
        !           471:        return pci_drv_register(&via_rhine_drv_id, dev);
1.1       root      472: }
                    473: #endif
                    474: 
1.1.1.2 ! root      475: static void *via_probe1(struct pci_dev *pdev, void *init_dev,
        !           476:                                                long ioaddr, int irq, int chip_idx, int card_idx)
1.1       root      477: {
1.1.1.2 ! root      478:        struct net_device *dev;
1.1       root      479:        struct netdev_private *np;
1.1.1.2 ! root      480:        void *priv_mem;
1.1       root      481:        int i, option = card_idx < MAX_UNITS ? options[card_idx] : 0;
                    482: 
1.1.1.2 ! root      483:        dev = init_etherdev(init_dev, 0);
        !           484:        if (!dev)
        !           485:                return NULL;
1.1       root      486: 
                    487:        printk(KERN_INFO "%s: %s at 0x%lx, ",
1.1.1.2 ! root      488:                   dev->name, pci_tbl[chip_idx].name, ioaddr);
1.1       root      489: 
1.1.1.2 ! root      490:        /* We would prefer to directly read the EEPROM but access may be locked. */
        !           491:        for (i = 0; i < 6; i++)
1.1       root      492:                dev->dev_addr[i] = readb(ioaddr + StationAddr + i);
1.1.1.2 ! root      493:        if (memcmp(dev->dev_addr, "\0\0\0\0\0", 6) == 0) {
        !           494:                /* Reload the station address from the EEPROM. */
        !           495:                writeb(0x20, ioaddr + MACRegEEcsr);
        !           496:                /* Typically 2 cycles to reload. */
        !           497:                for (i = 0; i < 150; i++)
        !           498:                        if (! (readb(ioaddr + MACRegEEcsr) & 0x20))
        !           499:                                break;
        !           500:                for (i = 0; i < 6; i++)
        !           501:                        dev->dev_addr[i] = readb(ioaddr + StationAddr + i);
        !           502:                if (memcmp(dev->dev_addr, "\0\0\0\0\0", 6) == 0) {
        !           503:                        printk(" (MISSING EEPROM ADDRESS)");
        !           504:                        /* Fill a temp addr with the "locally administered" bit set. */
        !           505:                        memcpy(dev->dev_addr, ">Linux", 6);
        !           506:                }
        !           507:        }
        !           508: 
1.1       root      509:        for (i = 0; i < 5; i++)
1.1.1.2 ! root      510:                printk("%2.2x:", dev->dev_addr[i]);
1.1       root      511:        printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
                    512: 
1.1.1.2 ! root      513:        /* Make certain the descriptor lists are cache-aligned. */
        !           514:        priv_mem = kmalloc(sizeof(*np) + PRIV_ALIGN, GFP_KERNEL);
        !           515:        /* Check for the very unlikely case of no memory. */
        !           516:        if (priv_mem == NULL)
        !           517:                return NULL;
        !           518: 
        !           519: #ifdef USE_IO_OPS
        !           520:        request_region(ioaddr, pci_tbl[chip_idx].io_size, dev->name);
1.1       root      521: #endif
                    522: 
                    523:        /* Reset the chip to erase previous misconfiguration. */
                    524:        writew(CmdReset, ioaddr + ChipCmd);
                    525: 
                    526:        dev->base_addr = ioaddr;
                    527:        dev->irq = irq;
                    528: 
1.1.1.2 ! root      529:        dev->priv = np = (void *)(((long)priv_mem + PRIV_ALIGN) & ~PRIV_ALIGN);
1.1       root      530:        memset(np, 0, sizeof(*np));
1.1.1.2 ! root      531:        np->priv_addr = priv_mem;
1.1       root      532: 
                    533:        np->next_module = root_net_dev;
                    534:        root_net_dev = dev;
                    535: 
1.1.1.2 ! root      536:        np->pci_dev = pdev;
        !           537:        np->chip_id = chip_idx;
        !           538:        np->drv_flags = pci_tbl[chip_idx].drv_flags;
        !           539:        np->msg_level = (1 << debug) - 1;
        !           540:        np->rx_copybreak = rx_copybreak;
        !           541:        np->max_interrupt_work = max_interrupt_work;
        !           542:        np->multicast_filter_limit = multicast_filter_limit;
1.1       root      543: 
                    544:        if (dev->mem_start)
                    545:                option = dev->mem_start;
                    546: 
                    547:        /* The lower four bits are the media type. */
                    548:        if (option > 0) {
1.1.1.2 ! root      549:                if (option & 0x220)
1.1       root      550:                        np->full_duplex = 1;
                    551:                np->default_port = option & 15;
                    552:                if (np->default_port)
                    553:                        np->medialock = 1;
                    554:        }
                    555:        if (card_idx < MAX_UNITS  &&  full_duplex[card_idx] > 0)
                    556:                np->full_duplex = 1;
                    557: 
1.1.1.2 ! root      558:        if (np->full_duplex) {
        !           559:                printk(KERN_INFO "%s: Set to forced full duplex, autonegotiation"
        !           560:                           " disabled.\n", dev->name);
1.1       root      561:                np->duplex_lock = 1;
1.1.1.2 ! root      562:        }
1.1       root      563: 
                    564:        /* The chip-specific entries in the device structure. */
                    565:        dev->open = &netdev_open;
                    566:        dev->hard_start_xmit = &start_tx;
                    567:        dev->stop = &netdev_close;
                    568:        dev->get_stats = &get_stats;
                    569:        dev->set_multicast_list = &set_rx_mode;
                    570:        dev->do_ioctl = &mii_ioctl;
                    571: 
1.1.1.2 ! root      572:        if (np->drv_flags & CanHaveMII) {
1.1       root      573:                int phy, phy_idx = 0;
                    574:                np->phys[0] = 1;                /* Standard for this chip. */
                    575:                for (phy = 1; phy < 32 && phy_idx < 4; phy++) {
                    576:                        int mii_status = mdio_read(dev, phy, 1);
                    577:                        if (mii_status != 0xffff  &&  mii_status != 0x0000) {
                    578:                                np->phys[phy_idx++] = phy;
                    579:                                np->advertising = mdio_read(dev, phy, 4);
                    580:                                printk(KERN_INFO "%s: MII PHY found at address %d, status "
                    581:                                           "0x%4.4x advertising %4.4x Link %4.4x.\n",
                    582:                                           dev->name, phy, mii_status, np->advertising,
                    583:                                           mdio_read(dev, phy, 5));
                    584:                        }
                    585:                }
                    586:                np->mii_cnt = phy_idx;
                    587:        }
                    588: 
1.1.1.2 ! root      589:        /* Allow forcing the media type. */
        !           590:        if (option > 0) {
        !           591:                if (option & 0x220)
        !           592:                        np->full_duplex = 1;
        !           593:                np->default_port = option & 0x3ff;
        !           594:                if (np->default_port & 0x330) {
        !           595:                        np->medialock = 1;
        !           596:                        printk(KERN_INFO "  Forcing %dMbs %s-duplex operation.\n",
        !           597:                                   (option & 0x300 ? 100 : 10),
        !           598:                                   (np->full_duplex ? "full" : "half"));
        !           599:                        if (np->mii_cnt)
        !           600:                                mdio_write(dev, np->phys[0], 0,
        !           601:                                                   ((option & 0x300) ? 0x2000 : 0) |    /* 100mbps? */
        !           602:                                                   (np->full_duplex ? 0x0100 : 0)); /* Full duplex? */
        !           603:                }
        !           604:        }
        !           605: 
1.1       root      606:        return dev;
                    607: }
                    608: 
                    609: 
                    610: /* Read and write over the MII Management Data I/O (MDIO) interface. */
                    611: 
1.1.1.2 ! root      612: static int mdio_read(struct net_device *dev, int phy_id, int regnum)
1.1       root      613: {
                    614:        long ioaddr = dev->base_addr;
                    615:        int boguscnt = 1024;
                    616: 
                    617:        /* Wait for a previous command to complete. */
                    618:        while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
                    619:                ;
                    620:        writeb(0x00, ioaddr + MIICmd);
                    621:        writeb(phy_id, ioaddr + MIIPhyAddr);
                    622:        writeb(regnum, ioaddr + MIIRegAddr);
                    623:        writeb(0x40, ioaddr + MIICmd);                  /* Trigger read */
                    624:        boguscnt = 1024;
                    625:        while ((readb(ioaddr + MIICmd) & 0x40) && --boguscnt > 0)
                    626:                ;
                    627:        return readw(ioaddr + MIIData);
                    628: }
                    629: 
1.1.1.2 ! root      630: static void mdio_write(struct net_device *dev, int phy_id, int regnum, int value)
1.1       root      631: {
1.1.1.2 ! root      632:        struct netdev_private *np = (struct netdev_private *)dev->priv;
1.1       root      633:        long ioaddr = dev->base_addr;
                    634:        int boguscnt = 1024;
                    635: 
1.1.1.2 ! root      636:        if (phy_id == np->phys[0]) {
        !           637:                switch (regnum) {
        !           638:                case 0:                                 /* Is user forcing speed/duplex? */
        !           639:                        if (value & 0x9000)     /* Autonegotiation. */
        !           640:                                np->duplex_lock = 0;
        !           641:                        else
        !           642:                                np->full_duplex = (value & 0x0100) ? 1 : 0;
        !           643:                        break;
        !           644:                case 4: np->advertising = value; break;
        !           645:                }
        !           646:        }
1.1       root      647:        /* Wait for a previous command to complete. */
                    648:        while ((readb(ioaddr + MIICmd) & 0x60) && --boguscnt > 0)
                    649:                ;
                    650:        writeb(0x00, ioaddr + MIICmd);
                    651:        writeb(phy_id, ioaddr + MIIPhyAddr);
                    652:        writeb(regnum, ioaddr + MIIRegAddr);
                    653:        writew(value, ioaddr + MIIData);
                    654:        writeb(0x20, ioaddr + MIICmd);                  /* Trigger write. */
                    655:        return;
                    656: }
                    657: 
                    658: 
1.1.1.2 ! root      659: static int netdev_open(struct net_device *dev)
1.1       root      660: {
                    661:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                    662:        long ioaddr = dev->base_addr;
                    663:        int i;
                    664: 
                    665:        /* Reset the chip. */
                    666:        writew(CmdReset, ioaddr + ChipCmd);
                    667: 
1.1.1.2 ! root      668:        MOD_INC_USE_COUNT;
        !           669: 
        !           670:        if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev)) {
        !           671:                MOD_DEC_USE_COUNT;
1.1       root      672:                return -EAGAIN;
1.1.1.2 ! root      673:        }
1.1       root      674: 
1.1.1.2 ! root      675:        if (np->msg_level & NETIF_MSG_IFUP)
1.1       root      676:                printk(KERN_DEBUG "%s: netdev_open() irq %d.\n",
                    677:                           dev->name, dev->irq);
                    678: 
                    679:        init_ring(dev);
                    680: 
                    681:        writel(virt_to_bus(np->rx_ring), ioaddr + RxRingPtr);
                    682:        writel(virt_to_bus(np->tx_ring), ioaddr + TxRingPtr);
                    683: 
                    684:        for (i = 0; i < 6; i++)
                    685:                writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
                    686: 
                    687:        /* Initialize other registers. */
1.1.1.2 ! root      688:        writew(0x0006, ioaddr + PCIBusConfig);  /* Tune configuration??? */
1.1       root      689:        /* Configure the FIFO thresholds. */
                    690:        writeb(0x20, ioaddr + TxConfig);        /* Initial threshold 32 bytes */
                    691:        np->tx_thresh = 0x20;
                    692:        np->rx_thresh = 0x60;                           /* Written in set_rx_mode(). */
                    693: 
                    694:        if (dev->if_port == 0)
                    695:                dev->if_port = np->default_port;
                    696: 
                    697:        set_rx_mode(dev);
1.1.1.2 ! root      698:        netif_start_tx_queue(dev);
1.1       root      699: 
1.1.1.2 ! root      700:        np->intr_enable = IntrRxDone | IntrRxErr | IntrRxEmpty |
        !           701:                IntrRxOverflow| IntrRxDropped| IntrTxDone | IntrTxAbort |
        !           702:                IntrTxUnderrun | IntrPCIErr | IntrStatsMax | IntrLinkChange |
        !           703:                IntrMIIChange;
1.1       root      704:        /* Enable interrupts by setting the interrupt mask. */
1.1.1.2 ! root      705:        writew(np->intr_enable, ioaddr + IntrEnable);
1.1       root      706: 
                    707:        np->chip_cmd = CmdStart|CmdTxOn|CmdRxOn|CmdNoTxPoll;
1.1.1.2 ! root      708:        if (np->duplex_lock)
        !           709:                np->chip_cmd |= CmdFDuplex;
1.1       root      710:        writew(np->chip_cmd, ioaddr + ChipCmd);
                    711: 
                    712:        check_duplex(dev);
1.1.1.2 ! root      713:        /* The LED outputs of various MII xcvrs should be configured.  */
        !           714:        /* For NS or Mison phys, turn on bit 1 in register 0x17 */
        !           715:        /* For ESI phys, turn on bit 7 in register 0x17. */
        !           716:        mdio_write(dev, np->phys[0], 0x17, mdio_read(dev, np->phys[0], 0x17) |
        !           717:                           (np->drv_flags & HasESIPhy) ? 0x0080 : 0x0001);
1.1       root      718: 
1.1.1.2 ! root      719:        if (np->msg_level & NETIF_MSG_IFUP)
1.1       root      720:                printk(KERN_DEBUG "%s: Done netdev_open(), status %4.4x "
                    721:                           "MII status: %4.4x.\n",
                    722:                           dev->name, readw(ioaddr + ChipCmd),
                    723:                           mdio_read(dev, np->phys[0], 1));
                    724: 
                    725:        /* Set the timer to check for link beat. */
                    726:        init_timer(&np->timer);
1.1.1.2 ! root      727:        np->timer.expires = jiffies + 2;
1.1       root      728:        np->timer.data = (unsigned long)dev;
                    729:        np->timer.function = &netdev_timer;                             /* timer handler */
                    730:        add_timer(&np->timer);
                    731: 
                    732:        return 0;
                    733: }
                    734: 
1.1.1.2 ! root      735: static void check_duplex(struct net_device *dev)
1.1       root      736: {
                    737:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                    738:        long ioaddr = dev->base_addr;
                    739:        int mii_reg5 = mdio_read(dev, np->phys[0], 5);
1.1.1.2 ! root      740:        int negotiated = mii_reg5 & np->advertising;
1.1       root      741:        int duplex;
                    742: 
                    743:        if (np->duplex_lock  ||  mii_reg5 == 0xffff)
                    744:                return;
1.1.1.2 ! root      745:        duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
1.1       root      746:        if (np->full_duplex != duplex) {
                    747:                np->full_duplex = duplex;
1.1.1.2 ! root      748:                if (np->msg_level & NETIF_MSG_LINK)
1.1       root      749:                        printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d link"
                    750:                                   " partner capability of %4.4x.\n", dev->name,
                    751:                                   duplex ? "full" : "half", np->phys[0], mii_reg5);
                    752:                if (duplex)
                    753:                        np->chip_cmd |= CmdFDuplex;
                    754:                else
                    755:                        np->chip_cmd &= ~CmdFDuplex;
                    756:                writew(np->chip_cmd, ioaddr + ChipCmd);
                    757:        }
                    758: }
                    759: 
                    760: static void netdev_timer(unsigned long data)
                    761: {
1.1.1.2 ! root      762:        struct net_device *dev = (struct net_device *)data;
1.1       root      763:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                    764:        long ioaddr = dev->base_addr;
                    765:        int next_tick = 10*HZ;
                    766: 
1.1.1.2 ! root      767:        if (np->msg_level & NETIF_MSG_TIMER) {
1.1       root      768:                printk(KERN_DEBUG "%s: VIA Rhine monitor tick, status %4.4x.\n",
                    769:                           dev->name, readw(ioaddr + IntrStatus));
                    770:        }
1.1.1.2 ! root      771:        if (netif_queue_paused(dev)
        !           772:                && np->cur_tx - np->dirty_tx > 1
        !           773:                && jiffies - dev->trans_start > TX_TIMEOUT)
        !           774:                tx_timeout(dev);
        !           775: 
1.1       root      776:        check_duplex(dev);
                    777: 
1.1.1.2 ! root      778:        np->timer.expires = jiffies + next_tick;
1.1       root      779:        add_timer(&np->timer);
                    780: }
                    781: 
1.1.1.2 ! root      782: static void tx_timeout(struct net_device *dev)
1.1       root      783: {
                    784:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                    785:        long ioaddr = dev->base_addr;
                    786: 
                    787:        printk(KERN_WARNING "%s: Transmit timed out, status %4.4x, PHY status "
                    788:                   "%4.4x, resetting...\n",
                    789:                   dev->name, readw(ioaddr + IntrStatus),
                    790:                   mdio_read(dev, np->phys[0], 1));
                    791: 
1.1.1.2 ! root      792:        /* Perhaps we should reinitialize the hardware here. */
        !           793:        dev->if_port = 0;
        !           794:        /* Restart the chip's Tx processes . */
        !           795:        writel(virt_to_bus(np->tx_ring + (np->dirty_tx % TX_RING_SIZE)),
        !           796:                   ioaddr + TxRingPtr);
        !           797:        writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
        !           798: 
        !           799:        /* Trigger an immediate transmit demand. */
        !           800: 
        !           801:        dev->trans_start = jiffies;
        !           802:        np->stats.tx_errors++;
        !           803:        return;
1.1       root      804: }
                    805: 
                    806: 
                    807: /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1.1.1.2 ! root      808: static void init_ring(struct net_device *dev)
1.1       root      809: {
                    810:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                    811:        int i;
                    812: 
                    813:        np->tx_full = 0;
                    814:        np->cur_rx = np->cur_tx = 0;
                    815:        np->dirty_rx = np->dirty_tx = 0;
                    816: 
1.1.1.2 ! root      817:        /* Use 1518/+18 if the CRC is transferred. */
        !           818:        np->rx_buf_sz = dev->mtu + 14;
        !           819:        if (np->rx_buf_sz < PKT_BUF_SZ)
        !           820:                np->rx_buf_sz = PKT_BUF_SZ;
1.1       root      821:        np->rx_head_desc = &np->rx_ring[0];
                    822: 
                    823:        for (i = 0; i < RX_RING_SIZE; i++) {
                    824:                np->rx_ring[i].rx_status = 0;
1.1.1.2 ! root      825:                np->rx_ring[i].desc_length = cpu_to_le32(np->rx_buf_sz);
        !           826:                np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]);
1.1       root      827:                np->rx_skbuff[i] = 0;
                    828:        }
                    829:        /* Mark the last entry as wrapping the ring. */
1.1.1.2 ! root      830:        np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]);
1.1       root      831: 
1.1.1.2 ! root      832:        /* Fill in the Rx buffers.  Handle allocation failure gracefully. */
1.1       root      833:        for (i = 0; i < RX_RING_SIZE; i++) {
                    834:                struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
                    835:                np->rx_skbuff[i] = skb;
                    836:                if (skb == NULL)
                    837:                        break;
                    838:                skb->dev = dev;                 /* Mark as being used by this device. */
1.1.1.2 ! root      839:                np->rx_ring[i].addr = virt_to_le32desc(skb->tail);
        !           840:                np->rx_ring[i].rx_status = cpu_to_le32(DescOwn);
1.1       root      841:        }
                    842:        np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
                    843: 
                    844:        for (i = 0; i < TX_RING_SIZE; i++) {
                    845:                np->tx_skbuff[i] = 0;
1.1.1.2 ! root      846:                np->tx_ring[i].tx_status = 0;
        !           847:                np->tx_ring[i].desc_length = cpu_to_le32(0x00e08000);
        !           848:                np->tx_ring[i].next_desc = virt_to_le32desc(&np->tx_ring[i+1]);
        !           849:                np->tx_buf[i] = 0;              /* Allocated as/if needed. */
1.1       root      850:        }
1.1.1.2 ! root      851:        np->tx_ring[i-1].next_desc = virt_to_le32desc(&np->tx_ring[0]);
1.1       root      852: 
                    853:        return;
                    854: }
                    855: 
1.1.1.2 ! root      856: static int start_tx(struct sk_buff *skb, struct net_device *dev)
1.1       root      857: {
                    858:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                    859:        unsigned entry;
                    860: 
1.1.1.2 ! root      861:        /* Block a timer-based transmit from overlapping.  This happens when
        !           862:           packets are presumed lost, and we use this check the Tx status. */
        !           863:        if (netif_pause_tx_queue(dev) != 0) {
        !           864:                /* This watchdog code is redundant with the media monitor timer. */
        !           865:                if (jiffies - dev->trans_start > TX_TIMEOUT)
        !           866:                        tx_timeout(dev);
1.1       root      867:                return 1;
                    868:        }
                    869: 
1.1.1.2 ! root      870:        /* Caution: the write order is important here, set the descriptor word
        !           871:           with the "ownership" bit last.  No SMP locking is needed if the
        !           872:           cur_tx is incremented after the descriptor is consistent.  */
1.1       root      873: 
                    874:        /* Calculate the next Tx descriptor entry. */
                    875:        entry = np->cur_tx % TX_RING_SIZE;
                    876: 
                    877:        np->tx_skbuff[entry] = skb;
                    878: 
1.1.1.2 ! root      879:        if ((np->drv_flags & ReqTxAlign)  && ((long)skb->data & 3)) {
        !           880:                /* Must use alignment buffer. */
1.1       root      881:                if (np->tx_buf[entry] == NULL &&
                    882:                        (np->tx_buf[entry] = kmalloc(PKT_BUF_SZ, GFP_KERNEL)) == NULL)
                    883:                        return 1;
                    884:                memcpy(np->tx_buf[entry], skb->data, skb->len);
1.1.1.2 ! root      885:                np->tx_ring[entry].addr = virt_to_le32desc(np->tx_buf[entry]);
1.1       root      886:        } else
1.1.1.2 ! root      887:                np->tx_ring[entry].addr = virt_to_le32desc(skb->data);
        !           888:        /* Explicitly flush packet data cache lines here. */
1.1       root      889: 
1.1.1.2 ! root      890:        np->tx_ring[entry].desc_length =
        !           891:                cpu_to_le32(0x00E08000 | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
        !           892:        np->tx_ring[entry].tx_status = cpu_to_le32(DescOwn);
1.1       root      893: 
                    894:        np->cur_tx++;
                    895: 
1.1.1.2 ! root      896:        /* Explicitly flush descriptor cache lines here. */
1.1       root      897: 
                    898:        /* Wake the potentially-idle transmit channel. */
                    899:        writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
                    900: 
1.1.1.2 ! root      901:        if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) {
1.1       root      902:                np->tx_full = 1;
1.1.1.2 ! root      903:                /* Check for a just-cleared queue. */
        !           904:                if (np->cur_tx - (volatile unsigned int)np->dirty_tx
        !           905:                        < TX_QUEUE_LEN - 2) {
        !           906:                        np->tx_full = 0;
        !           907:                        netif_unpause_tx_queue(dev);
        !           908:                } else
        !           909:                        netif_stop_tx_queue(dev);
        !           910:        } else
        !           911:                netif_unpause_tx_queue(dev);            /* Typical path */
        !           912: 
1.1       root      913:        dev->trans_start = jiffies;
                    914: 
1.1.1.2 ! root      915:        if (np->msg_level & NETIF_MSG_TX_QUEUED) {
1.1       root      916:                printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
                    917:                           dev->name, np->cur_tx, entry);
                    918:        }
                    919:        return 0;
                    920: }
                    921: 
                    922: /* The interrupt handler does all of the Rx thread work and cleans up
                    923:    after the Tx thread. */
                    924: static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
                    925: {
1.1.1.2 ! root      926:        struct net_device *dev = (struct net_device *)dev_instance;
        !           927:        struct netdev_private *np = (void *)dev->priv;
        !           928:        long ioaddr = dev->base_addr;
        !           929:        int boguscnt = np->max_interrupt_work;
1.1       root      930: 
                    931:        do {
                    932:                u32 intr_status = readw(ioaddr + IntrStatus);
                    933: 
                    934:                /* Acknowledge all of the current interrupt sources ASAP. */
                    935:                writew(intr_status & 0xffff, ioaddr + IntrStatus);
                    936: 
1.1.1.2 ! root      937:                if (np->msg_level & NETIF_MSG_INTR)
1.1       root      938:                        printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
                    939:                                   dev->name, intr_status);
                    940: 
                    941:                if (intr_status == 0)
                    942:                        break;
                    943: 
                    944:                if (intr_status & (IntrRxDone | IntrRxErr | IntrRxDropped |
                    945:                                                   IntrRxWakeUp | IntrRxEmpty | IntrRxNoBuf))
                    946:                        netdev_rx(dev);
                    947: 
                    948:                for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
                    949:                        int entry = np->dirty_tx % TX_RING_SIZE;
1.1.1.2 ! root      950:                        int txstatus = le32_to_cpu(np->tx_ring[entry].tx_status);
        !           951:                        if (txstatus & DescOwn)
1.1       root      952:                                break;
1.1.1.2 ! root      953:                        if (np->msg_level & NETIF_MSG_TX_DONE)
        !           954:                                printk(KERN_DEBUG "  Tx scavenge %d status %4.4x.\n",
1.1       root      955:                                           entry, txstatus);
                    956:                        if (txstatus & 0x8000) {
1.1.1.2 ! root      957:                                if (np->msg_level & NETIF_MSG_TX_ERR)
1.1       root      958:                                        printk(KERN_DEBUG "%s: Transmit error, Tx status %4.4x.\n",
                    959:                                                   dev->name, txstatus);
                    960:                                np->stats.tx_errors++;
                    961:                                if (txstatus & 0x0400) np->stats.tx_carrier_errors++;
                    962:                                if (txstatus & 0x0200) np->stats.tx_window_errors++;
                    963:                                if (txstatus & 0x0100) np->stats.tx_aborted_errors++;
                    964:                                if (txstatus & 0x0080) np->stats.tx_heartbeat_errors++;
                    965:                                if (txstatus & 0x0002) np->stats.tx_fifo_errors++;
                    966: #ifdef ETHER_STATS
                    967:                                if (txstatus & 0x0100) np->stats.collisions16++;
                    968: #endif
                    969:                                /* Transmitter restarted in 'abnormal' handler. */
                    970:                        } else {
                    971: #ifdef ETHER_STATS
                    972:                                if (txstatus & 0x0001) np->stats.tx_deferred++;
                    973: #endif
1.1.1.2 ! root      974:                                if (np->drv_flags & HasV1TxStat)
        !           975:                                        np->stats.collisions += (txstatus >> 3) & 15;
        !           976:                                else
        !           977:                                        np->stats.collisions += txstatus & 15;
1.1       root      978: #if defined(NETSTATS_VER2)
1.1.1.2 ! root      979:                                np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1.1       root      980: #endif
                    981:                                np->stats.tx_packets++;
                    982:                        }
                    983:                        /* Free the original skb. */
1.1.1.2 ! root      984:                        dev_free_skb_irq(np->tx_skbuff[entry]);
1.1       root      985:                        np->tx_skbuff[entry] = 0;
                    986:                }
1.1.1.2 ! root      987:                /* Note the 4 slot hysteresis in mark the queue non-full. */
        !           988:                if (np->tx_full  &&  np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) {
        !           989:                        /* The ring is no longer full, allow new TX entries. */
1.1       root      990:                        np->tx_full = 0;
1.1.1.2 ! root      991:                        netif_resume_tx_queue(dev);
1.1       root      992:                }
                    993: 
                    994:                /* Abnormal error summary/uncommon events handlers. */
                    995:                if (intr_status & (IntrPCIErr | IntrLinkChange | IntrMIIChange |
                    996:                                                   IntrStatsMax | IntrTxAbort | IntrTxUnderrun))
                    997:                        netdev_error(dev, intr_status);
                    998: 
                    999:                if (--boguscnt < 0) {
                   1000:                        printk(KERN_WARNING "%s: Too much work at interrupt, "
                   1001:                                   "status=0x%4.4x.\n",
                   1002:                                   dev->name, intr_status);
                   1003:                        break;
                   1004:                }
                   1005:        } while (1);
                   1006: 
1.1.1.2 ! root     1007:        if (np->msg_level & NETIF_MSG_INTR)
1.1       root     1008:                printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
1.1.1.2 ! root     1009:                           dev->name, (int)readw(ioaddr + IntrStatus));
1.1       root     1010: 
                   1011:        return;
                   1012: }
                   1013: 
                   1014: /* This routine is logically part of the interrupt handler, but isolated
                   1015:    for clarity and better register allocation. */
1.1.1.2 ! root     1016: static int netdev_rx(struct net_device *dev)
1.1       root     1017: {
                   1018:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                   1019:        int entry = np->cur_rx % RX_RING_SIZE;
                   1020:        int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
                   1021: 
1.1.1.2 ! root     1022:        if (np->msg_level & NETIF_MSG_RX_STATUS) {
        !          1023:                printk(KERN_DEBUG " In netdev_rx(), entry %d status %8.8x.\n",
        !          1024:                           entry, np->rx_head_desc->rx_status);
1.1       root     1025:        }
                   1026: 
                   1027:        /* If EOP is set on the next entry, it's a new packet. Send it up. */
1.1.1.2 ! root     1028:        while ( ! (np->rx_head_desc->rx_status & cpu_to_le32(DescOwn))) {
1.1       root     1029:                struct rx_desc *desc = np->rx_head_desc;
1.1.1.2 ! root     1030:                u32 desc_status = le32_to_cpu(desc->rx_status);
        !          1031:                int data_size = desc_status >> 16;
1.1       root     1032: 
1.1.1.2 ! root     1033:                if (np->msg_level & NETIF_MSG_RX_STATUS)
1.1       root     1034:                        printk(KERN_DEBUG "  netdev_rx() status is %4.4x.\n",
                   1035:                                   desc_status);
                   1036:                if (--boguscnt < 0)
                   1037:                        break;
                   1038:                if ( (desc_status & (RxWholePkt | RxErr)) !=  RxWholePkt) {
                   1039:                        if ((desc_status & RxWholePkt) !=  RxWholePkt) {
                   1040:                                printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
                   1041:                                           "multiple buffers, entry %#x length %d status %4.4x!\n",
                   1042:                                           dev->name, np->cur_rx, data_size, desc_status);
                   1043:                                printk(KERN_WARNING "%s: Oversized Ethernet frame %p vs %p.\n",
                   1044:                                           dev->name, np->rx_head_desc,
                   1045:                                           &np->rx_ring[np->cur_rx % RX_RING_SIZE]);
                   1046:                                np->stats.rx_length_errors++;
                   1047:                        } else if (desc_status & RxErr) {
                   1048:                                /* There was a error. */
1.1.1.2 ! root     1049:                                if (np->msg_level & NETIF_MSG_RX_ERR)
1.1       root     1050:                                        printk(KERN_DEBUG "  netdev_rx() Rx error was %8.8x.\n",
                   1051:                                                   desc_status);
                   1052:                                np->stats.rx_errors++;
                   1053:                                if (desc_status & 0x0030) np->stats.rx_length_errors++;
                   1054:                                if (desc_status & 0x0048) np->stats.rx_fifo_errors++;
                   1055:                                if (desc_status & 0x0004) np->stats.rx_frame_errors++;
                   1056:                                if (desc_status & 0x0002) np->stats.rx_crc_errors++;
                   1057:                        }
                   1058:                } else {
                   1059:                        struct sk_buff *skb;
                   1060:                        /* Length should omit the CRC */
1.1.1.2 ! root     1061:                        int pkt_len = data_size - 4;
1.1       root     1062: 
                   1063:                        /* Check if the packet is long enough to accept without copying
                   1064:                           to a minimally-sized skbuff. */
1.1.1.2 ! root     1065:                        if (pkt_len < np->rx_copybreak
1.1       root     1066:                                && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
                   1067:                                skb->dev = dev;
                   1068:                                skb_reserve(skb, 2);    /* 16 byte align the IP header */
1.1.1.2 ! root     1069: #if HAS_IP_COPYSUM                     /* Call copy + cksum if available. */
        !          1070:                                eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0);
1.1       root     1071:                                skb_put(skb, pkt_len);
                   1072: #else
1.1.1.2 ! root     1073:                                memcpy(skb_put(skb, pkt_len), np->rx_skbuff[entry]->tail,
        !          1074:                                           pkt_len);
1.1       root     1075: #endif
                   1076:                        } else {
                   1077:                                skb_put(skb = np->rx_skbuff[entry], pkt_len);
                   1078:                                np->rx_skbuff[entry] = NULL;
                   1079:                        }
                   1080:                        skb->protocol = eth_type_trans(skb, dev);
1.1.1.2 ! root     1081:                        {                                       /* Use hardware checksum info. */
        !          1082:                                int rxtype = le32_to_cpu(desc->desc_length);
        !          1083:                                int csum_bits = rxtype & RxTypeCsumMask;
        !          1084:                                if (csum_bits == RxTypeUDPSumOK ||
        !          1085:                                        csum_bits == RxTypeTCPSumOK)
        !          1086:                                        skb->ip_summed = CHECKSUM_UNNECESSARY;
        !          1087:                        }
1.1       root     1088:                        netif_rx(skb);
                   1089:                        dev->last_rx = jiffies;
1.1.1.2 ! root     1090: #if defined(NETSTATS_VER2)
        !          1091:                        np->stats.rx_bytes += pkt_len;
        !          1092: #endif
1.1       root     1093:                        np->stats.rx_packets++;
                   1094:                }
                   1095:                entry = (++np->cur_rx) % RX_RING_SIZE;
                   1096:                np->rx_head_desc = &np->rx_ring[entry];
                   1097:        }
                   1098: 
                   1099:        /* Refill the Rx ring buffers. */
                   1100:        for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
                   1101:                struct sk_buff *skb;
                   1102:                entry = np->dirty_rx % RX_RING_SIZE;
                   1103:                if (np->rx_skbuff[entry] == NULL) {
                   1104:                        skb = dev_alloc_skb(np->rx_buf_sz);
                   1105:                        np->rx_skbuff[entry] = skb;
                   1106:                        if (skb == NULL)
                   1107:                                break;                  /* Better luck next round. */
                   1108:                        skb->dev = dev;                 /* Mark as being used by this device. */
1.1.1.2 ! root     1109:                        np->rx_ring[entry].addr = virt_to_le32desc(skb->tail);
1.1       root     1110:                }
1.1.1.2 ! root     1111:                np->rx_ring[entry].rx_status = cpu_to_le32(DescOwn);
1.1       root     1112:        }
                   1113: 
                   1114:        /* Pre-emptively restart Rx engine. */
                   1115:        writew(CmdRxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
                   1116:        return 0;
                   1117: }
                   1118: 
1.1.1.2 ! root     1119: static void netdev_error(struct net_device *dev, int intr_status)
1.1       root     1120: {
                   1121:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                   1122:        long ioaddr = dev->base_addr;
                   1123: 
                   1124:        if (intr_status & (IntrMIIChange | IntrLinkChange)) {
1.1.1.2 ! root     1125:                if (readb(ioaddr + MIIStatus) & 0x02) {
1.1       root     1126:                        /* Link failed, restart autonegotiation. */
1.1.1.2 ! root     1127:                        if (np->drv_flags & HasDavicomPhy)
        !          1128:                                mdio_write(dev, np->phys[0], 0, 0x3300);
        !          1129:                        netif_link_down(dev);
        !          1130:                } else {
        !          1131:                        netif_link_up(dev);
1.1       root     1132:                        check_duplex(dev);
1.1.1.2 ! root     1133:                }
        !          1134:                if (np->msg_level & NETIF_MSG_LINK)
1.1       root     1135:                        printk(KERN_ERR "%s: MII status changed: Autonegotiation "
                   1136:                                   "advertising %4.4x  partner %4.4x.\n", dev->name,
                   1137:                           mdio_read(dev, np->phys[0], 4),
                   1138:                           mdio_read(dev, np->phys[0], 5));
                   1139:        }
                   1140:        if (intr_status & IntrStatsMax) {
                   1141:                np->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
                   1142:                np->stats.rx_missed_errors      += readw(ioaddr + RxMissed);
1.1.1.2 ! root     1143:                writel(0, ioaddr + RxMissed);
1.1       root     1144:        }
                   1145:        if (intr_status & IntrTxAbort) {
                   1146:                /* Stats counted in Tx-done handler, just restart Tx. */
1.1.1.2 ! root     1147:                writel(virt_to_bus(&np->tx_ring[np->dirty_tx % TX_RING_SIZE]),
        !          1148:                           ioaddr + TxRingPtr);
1.1       root     1149:                writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
                   1150:        }
                   1151:        if (intr_status & IntrTxUnderrun) {
                   1152:                if (np->tx_thresh < 0xE0)
                   1153:                        writeb(np->tx_thresh += 0x20, ioaddr + TxConfig);
1.1.1.2 ! root     1154:                if (np->msg_level & NETIF_MSG_TX_ERR)
1.1       root     1155:                        printk(KERN_INFO "%s: Transmitter underrun, increasing Tx "
                   1156:                                   "threshold setting to %2.2x.\n", dev->name, np->tx_thresh);
                   1157:        }
1.1.1.2 ! root     1158:        if ((intr_status & ~(IntrLinkChange | IntrMIIChange | IntrStatsMax |
        !          1159:                                                 IntrTxAbort|IntrTxAborted | IntrNormalSummary))
        !          1160:                 && (np->msg_level & NETIF_MSG_DRV)) {
1.1       root     1161:                printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n",
                   1162:                           dev->name, intr_status);
                   1163:                /* Recovery for other fault sources not known. */
                   1164:                writew(CmdTxDemand | np->chip_cmd, dev->base_addr + ChipCmd);
                   1165:        }
                   1166: }
                   1167: 
1.1.1.2 ! root     1168: static struct net_device_stats *get_stats(struct net_device *dev)
1.1       root     1169: {
                   1170:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                   1171:        long ioaddr = dev->base_addr;
                   1172: 
                   1173:        /* Nominally we should lock this segment of code for SMP, although
                   1174:           the vulnerability window is very small and statistics are
                   1175:           non-critical. */
                   1176:        np->stats.rx_crc_errors += readw(ioaddr + RxCRCErrs);
                   1177:        np->stats.rx_missed_errors      += readw(ioaddr + RxMissed);
1.1.1.2 ! root     1178:        writel(0, ioaddr + RxMissed);
1.1       root     1179: 
                   1180:        return &np->stats;
                   1181: }
                   1182: 
                   1183: /* The big-endian AUTODIN II ethernet CRC calculation.
                   1184:    N.B. Do not use for bulk data, use a table-based routine instead.
                   1185:    This is common code and should be moved to net/core/crc.c */
                   1186: static unsigned const ethernet_polynomial = 0x04c11db7U;
                   1187: static inline u32 ether_crc(int length, unsigned char *data)
                   1188: {
1.1.1.2 ! root     1189:        int crc = -1;
1.1       root     1190: 
1.1.1.2 ! root     1191:        while(--length >= 0) {
1.1       root     1192:                unsigned char current_octet = *data++;
                   1193:                int bit;
                   1194:                for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
                   1195:                        crc = (crc << 1) ^
                   1196:                                ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
                   1197:                }
1.1.1.2 ! root     1198:        }
        !          1199:        return crc;
1.1       root     1200: }
                   1201: 
1.1.1.2 ! root     1202: static void set_rx_mode(struct net_device *dev)
1.1       root     1203: {
                   1204:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                   1205:        long ioaddr = dev->base_addr;
                   1206:        u32 mc_filter[2];                       /* Multicast hash filter */
                   1207:        u8 rx_mode;                                     /* Note: 0x02=accept runt, 0x01=accept errs */
                   1208: 
                   1209:        if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
                   1210:                /* Unconditionally log net taps. */
                   1211:                printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
                   1212:                rx_mode = 0x1C;
1.1.1.2 ! root     1213:        } else if ((dev->mc_count > np->multicast_filter_limit)
1.1       root     1214:                           ||  (dev->flags & IFF_ALLMULTI)) {
                   1215:                /* Too many to match, or accept all multicasts. */
1.1.1.2 ! root     1216:                writel(0xffffffff, ioaddr + MulticastFilter0);
        !          1217:                writel(0xffffffff, ioaddr + MulticastFilter1);
1.1       root     1218:                rx_mode = 0x0C;
                   1219:        } else {
                   1220:                struct dev_mc_list *mclist;
                   1221:                int i;
                   1222:                memset(mc_filter, 0, sizeof(mc_filter));
                   1223:                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
                   1224:                         i++, mclist = mclist->next) {
                   1225:                        set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26,
                   1226:                                        mc_filter);
                   1227:                }
                   1228:                writel(mc_filter[0], ioaddr + MulticastFilter0);
                   1229:                writel(mc_filter[1], ioaddr + MulticastFilter1);
                   1230:                rx_mode = 0x0C;
                   1231:        }
                   1232:        writeb(np->rx_thresh | rx_mode, ioaddr + RxConfig);
                   1233: }
                   1234: 
1.1.1.2 ! root     1235: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1.1       root     1236: {
1.1.1.2 ! root     1237:        struct netdev_private *np = (struct netdev_private *)dev->priv;
1.1       root     1238:        u16 *data = (u16 *)&rq->ifr_data;
1.1.1.2 ! root     1239:        u32 *data32 = (void *)&rq->ifr_data;
1.1       root     1240: 
                   1241:        switch(cmd) {
1.1.1.2 ! root     1242:        case 0x8947: case 0x89F0:
        !          1243:                /* SIOCGMIIPHY: Get the address of the PHY in use. */
        !          1244:                data[0] = np->phys[0] & 0x1f;
1.1       root     1245:                /* Fall Through */
1.1.1.2 ! root     1246:        case 0x8948: case 0x89F1:
        !          1247:                /* SIOCGMIIREG: Read the specified MII register. */
1.1       root     1248:                data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
                   1249:                return 0;
1.1.1.2 ! root     1250:        case 0x8949: case 0x89F2:
        !          1251:                /* SIOCSMIIREG: Write the specified MII register */
        !          1252:                if (!capable(CAP_NET_ADMIN))
1.1       root     1253:                        return -EPERM;
1.1.1.2 ! root     1254:                /* Note: forced media tracking is done in mdio_write(). */
1.1       root     1255:                mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
                   1256:                return 0;
1.1.1.2 ! root     1257:        case SIOCGPARAMS:
        !          1258:                data32[0] = np->msg_level;
        !          1259:                data32[1] = np->multicast_filter_limit;
        !          1260:                data32[2] = np->max_interrupt_work;
        !          1261:                data32[3] = np->rx_copybreak;
        !          1262:                return 0;
        !          1263:        case SIOCSPARAMS:
        !          1264:                if (!capable(CAP_NET_ADMIN))
        !          1265:                        return -EPERM;
        !          1266:                np->msg_level = data32[0];
        !          1267:                np->multicast_filter_limit = data32[1];
        !          1268:                np->max_interrupt_work = data32[2];
        !          1269:                np->rx_copybreak = data32[3];
        !          1270:                return 0;
1.1       root     1271:        default:
                   1272:                return -EOPNOTSUPP;
                   1273:        }
                   1274: }
                   1275: 
1.1.1.2 ! root     1276: static int netdev_close(struct net_device *dev)
1.1       root     1277: {
                   1278:        long ioaddr = dev->base_addr;
                   1279:        struct netdev_private *np = (struct netdev_private *)dev->priv;
                   1280:        int i;
                   1281: 
1.1.1.2 ! root     1282:        netif_stop_tx_queue(dev);
1.1       root     1283: 
1.1.1.2 ! root     1284:        if (np->msg_level & NETIF_MSG_IFDOWN)
1.1       root     1285:                printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.\n",
                   1286:                           dev->name, readw(ioaddr + ChipCmd));
                   1287: 
1.1.1.2 ! root     1288:        /* Switch to loopback mode to avoid hardware races. */
        !          1289:        writeb(np->tx_thresh | 0x01, ioaddr + TxConfig);
        !          1290: 
1.1       root     1291:        /* Disable interrupts by clearing the interrupt mask. */
                   1292:        writew(0x0000, ioaddr + IntrEnable);
                   1293: 
                   1294:        /* Stop the chip's Tx and Rx processes. */
1.1.1.2 ! root     1295:        np->chip_cmd = CmdStop;
1.1       root     1296:        writew(CmdStop, ioaddr + ChipCmd);
                   1297: 
                   1298:        del_timer(&np->timer);
                   1299: 
                   1300:        free_irq(dev->irq, dev);
                   1301: 
                   1302:        /* Free all the skbuffs in the Rx queue. */
                   1303:        for (i = 0; i < RX_RING_SIZE; i++) {
1.1.1.2 ! root     1304:                np->rx_ring[i].rx_status = 0;
1.1       root     1305:                np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */
                   1306:                if (np->rx_skbuff[i]) {
                   1307: #if LINUX_VERSION_CODE < 0x20100
                   1308:                        np->rx_skbuff[i]->free = 1;
                   1309: #endif
                   1310:                        dev_free_skb(np->rx_skbuff[i]);
                   1311:                }
                   1312:                np->rx_skbuff[i] = 0;
                   1313:        }
                   1314:        for (i = 0; i < TX_RING_SIZE; i++) {
                   1315:                if (np->tx_skbuff[i])
                   1316:                        dev_free_skb(np->tx_skbuff[i]);
                   1317:                np->tx_skbuff[i] = 0;
1.1.1.2 ! root     1318:                if (np->tx_buf[i]) {
        !          1319:                        kfree(np->tx_buf[i]);
        !          1320:                        np->tx_buf[i] = 0;
        !          1321:                }
1.1       root     1322:        }
                   1323: 
                   1324:        MOD_DEC_USE_COUNT;
                   1325: 
                   1326:        return 0;
                   1327: }
                   1328: 
1.1.1.2 ! root     1329: static int via_pwr_event(void *dev_instance, int event)
        !          1330: {
        !          1331:        struct net_device *dev = dev_instance;
        !          1332:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1333:        long ioaddr = dev->base_addr;
        !          1334: 
        !          1335:        if (np->msg_level & NETIF_MSG_LINK)
        !          1336:                printk(KERN_DEBUG "%s: Handling power event %d.\n", dev->name, event);
        !          1337:        switch(event) {
        !          1338:        case DRV_ATTACH:
        !          1339:                MOD_INC_USE_COUNT;
        !          1340:                break;
        !          1341:        case DRV_SUSPEND:
        !          1342:                /* Disable interrupts, stop Tx and Rx. */
        !          1343:                writew(0x0000, ioaddr + IntrEnable);
        !          1344:                /* Stop the chip's Tx and Rx processes. */
        !          1345:                writew(CmdStop, ioaddr + ChipCmd);
        !          1346:                break;
        !          1347:        case DRV_RESUME:
        !          1348:                /* This is incomplete: the actions are very chip specific. */
        !          1349:                set_rx_mode(dev);
        !          1350:                netif_start_tx_queue(dev);
        !          1351:                writew(np->chip_cmd, ioaddr + ChipCmd);
        !          1352:                writew(np->intr_enable, ioaddr + IntrEnable);
        !          1353:                break;
        !          1354:        case DRV_DETACH: {
        !          1355:                struct net_device **devp, **next;
        !          1356:                if (dev->flags & IFF_UP) {
        !          1357:                        /* Some, but not all, kernel versions close automatically. */
        !          1358:                        dev_close(dev);
        !          1359:                        dev->flags &= ~(IFF_UP|IFF_RUNNING);
        !          1360:                }
        !          1361:                unregister_netdev(dev);
        !          1362:                release_region(dev->base_addr, pci_tbl[np->chip_id].io_size);
        !          1363: #ifndef USE_IO_OPS
        !          1364:                iounmap((char *)dev->base_addr);
        !          1365: #endif
        !          1366:                for (devp = &root_net_dev; *devp; devp = next) {
        !          1367:                        next = &((struct netdev_private *)(*devp)->priv)->next_module;
        !          1368:                        if (*devp == dev) {
        !          1369:                                *devp = *next;
        !          1370:                                break;
        !          1371:                        }
        !          1372:                }
        !          1373:                if (np->priv_addr)
        !          1374:                        kfree(np->priv_addr);
        !          1375:                kfree(dev);
        !          1376:                MOD_DEC_USE_COUNT;
        !          1377:                break;
        !          1378:        }
        !          1379:        }
        !          1380: 
        !          1381:        return 0;
        !          1382: }
        !          1383: 
1.1       root     1384: 
                   1385: #ifdef MODULE
                   1386: int init_module(void)
                   1387: {
1.1.1.2 ! root     1388:        if (debug >= NETIF_MSG_DRV)     /* Emit version even if no cards detected. */
        !          1389:                printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
        !          1390:        return pci_drv_register(&via_rhine_drv_id, NULL);
1.1       root     1391: }
                   1392: 
                   1393: void cleanup_module(void)
                   1394: {
1.1.1.2 ! root     1395:        struct net_device *next_dev;
1.1       root     1396: 
1.1.1.2 ! root     1397:        pci_drv_unregister(&via_rhine_drv_id);
1.1       root     1398: 
                   1399:        /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
                   1400:        while (root_net_dev) {
1.1.1.2 ! root     1401:                struct netdev_private *np = (void *)(root_net_dev->priv);
1.1       root     1402:                unregister_netdev(root_net_dev);
1.1.1.2 ! root     1403: #ifdef USE_IO_OPS
1.1       root     1404:                release_region(root_net_dev->base_addr, pci_tbl[np->chip_id].io_size);
                   1405: #else
                   1406:                iounmap((char *)(root_net_dev->base_addr));
                   1407: #endif
1.1.1.2 ! root     1408:                next_dev = np->next_module;
        !          1409:                if (np->priv_addr)
        !          1410:                        kfree(np->priv_addr);
1.1       root     1411:                kfree(root_net_dev);
1.1.1.2 ! root     1412:                root_net_dev = next_dev;
1.1       root     1413:        }
                   1414: }
                   1415: 
                   1416: #endif  /* MODULE */
                   1417: 
                   1418: /*
                   1419:  * Local variables:
1.1.1.2 ! root     1420:  *  compile-command: "make KERNVER=`uname -r` via-rhine.o"
        !          1421:  *  compile-cmd: "gcc -DMODULE -Wall -Wstrict-prototypes -O6 -c via-rhine.c"
        !          1422:  *  simple-compile-command: "gcc -DMODULE -O6 -c via-rhine.c"
1.1       root     1423:  *  c-indent-level: 4
                   1424:  *  c-basic-offset: 4
                   1425:  *  tab-width: 4
                   1426:  * End:
                   1427:  */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.