Annotation of Gnu-Mach/linux/src/drivers/net/winbond-840.c, revision 1.1

1.1     ! root        1: /* winbond-840.c: A Linux network device driver for the Winbond W89c840. */
        !             2: /*
        !             3:        Written 1998-2003 by Donald Becker.
        !             4: 
        !             5:        This software may be used and distributed according to the terms of
        !             6:        the GNU General Public License (GPL), incorporated herein by reference.
        !             7:        Drivers based on or derived from this code fall under the GPL and must
        !             8:        retain the authorship, copyright and license notice.  This file is not
        !             9:        a complete program and may only be used when the entire operating
        !            10:        system is licensed under the GPL.
        !            11: 
        !            12:        The author may be reached as [email protected], or C/O
        !            13:        Scyld Computing Corporation
        !            14:        914 Bay Ridge Road, Suite 220
        !            15:        Annapolis MD 21403
        !            16: 
        !            17:        Support information and updates available at
        !            18:                http://www.scyld.com/network/drivers.html
        !            19:        The information and support mailing lists are based at
        !            20:                http://www.scyld.com/mailman/listinfo/
        !            21: 
        !            22:        Do not remove the copyright infomation.
        !            23:        Do not change the version information unless an improvement has been made.
        !            24:        Merely removing my name, as Compex has done in the past, does not count
        !            25:        as an improvement.
        !            26: */
        !            27: 
        !            28: /* These identify the driver base version and may not be removed. */
        !            29: static const char version1[] =
        !            30: "winbond-840.c:v1.10 7/22/2003  Donald Becker <[email protected]>\n";
        !            31: static const char version2[] =
        !            32: "  http://www.scyld.com/network/drivers.html\n";
        !            33: 
        !            34: /* Automatically extracted configuration info:
        !            35: probe-func: winbond840_probe
        !            36: config-in: tristate 'Winbond W89c840 Ethernet support' CONFIG_WINBOND_840
        !            37: 
        !            38: c-help-name: Winbond W89c840 PCI Ethernet support
        !            39: c-help-symbol: CONFIG_WINBOND_840
        !            40: c-help: The winbond-840.c driver is for the Winbond W89c840 chip.
        !            41: c-help: This chip is named TX9882 on the Compex RL100-ATX board.
        !            42: c-help: More specific information and updates are available from
        !            43: c-help: http://www.scyld.com/network/drivers.html
        !            44: */
        !            45: 
        !            46: /* The user-configurable values.
        !            47:    These may be modified when a driver module is loaded.*/
        !            48: 
        !            49: /* Message enable level: 0..31 = no..all messages.  See NETIF_MSG docs. */
        !            50: static int debug = 2;
        !            51: 
        !            52: /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
        !            53: static int max_interrupt_work = 20;
        !            54: 
        !            55: /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
        !            56:    The '840 uses a 64 element hash table based on the Ethernet CRC.  */
        !            57: static int multicast_filter_limit = 32;
        !            58: 
        !            59: /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
        !            60:    Setting to > 1518 effectively disables this feature. */
        !            61: static int rx_copybreak = 0;
        !            62: 
        !            63: /* Used to pass the media type, etc.
        !            64:    Both 'options[]' and 'full_duplex[]' should exist for driver
        !            65:    interoperability, however setting full_duplex[] is deprecated.
        !            66:    The media type is usually passed in 'options[]'.
        !            67:     The default is autonegotation for speed and duplex.
        !            68:        This should rarely be overridden.
        !            69:     Use option values 0x10/0x20 for 10Mbps, 0x100,0x200 for 100Mbps.
        !            70:     Use option values 0x10 and 0x100 for forcing half duplex fixed speed.
        !            71:     Use option values 0x20 and 0x200 for forcing full duplex operation.
        !            72: */
        !            73: #define MAX_UNITS 8            /* More are supported, limit only on options */
        !            74: static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
        !            75: static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
        !            76: 
        !            77: /* Operational parameters that are set at compile time. */
        !            78: 
        !            79: /* Keep the ring sizes a power of two for compile efficiency.
        !            80:    The compiler will convert <unsigned>'%'<2^N> into a bit mask.
        !            81:    Making the Tx ring too large decreases the effectiveness of channel
        !            82:    bonding and packet priority, confuses the system network buffer limits,
        !            83:    and wastes memory.
        !            84:    Larger receive rings merely waste memory.
        !            85: */
        !            86: #define TX_RING_SIZE   16
        !            87: #define TX_QUEUE_LEN   10              /* Limit ring entries actually used, min 4. */
        !            88: #define RX_RING_SIZE   32
        !            89: 
        !            90: /* The presumed FIFO size for working around the Tx-FIFO-overflow bug.
        !            91:    To avoid overflowing we don't queue again until we have room for a
        !            92:    full-size packet.
        !            93:  */
        !            94: #define TX_FIFO_SIZE (2048)
        !            95: #define TX_BUG_FIFO_LIMIT (TX_FIFO_SIZE-1514-16)
        !            96: 
        !            97: /* Operational parameters that usually are not changed. */
        !            98: /* Time in jiffies before concluding the transmitter is hung.
        !            99:    Re-autonegotiation may take up to 3 seconds.
        !           100:  */
        !           101: #define TX_TIMEOUT  (6*HZ)
        !           102: 
        !           103: /* Allocation size of Rx buffers with normal sized Ethernet frames.
        !           104:    Do not change this value without good reason.  This is not a limit,
        !           105:    but a way to keep a consistent allocation size among drivers.
        !           106:  */
        !           107: #define PKT_BUF_SZ             1536
        !           108: 
        !           109: #ifndef __KERNEL__
        !           110: #define __KERNEL__
        !           111: #endif
        !           112: #if !defined(__OPTIMIZE__)
        !           113: #warning  You must compile this file with the correct options!
        !           114: #warning  See the last lines of the source file.
        !           115: #error You must compile this driver with "-O".
        !           116: #endif
        !           117: 
        !           118: /* Include files, designed to support most kernel versions 2.0.0 and later. */
        !           119: #include <linux/config.h>
        !           120: #if defined(CONFIG_SMP) && ! defined(__SMP__)
        !           121: #define __SMP__
        !           122: #endif
        !           123: #if defined(MODULE) && defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
        !           124: #define MODVERSIONS
        !           125: #endif
        !           126: 
        !           127: #include <linux/version.h>
        !           128: #if defined(MODVERSIONS)
        !           129: #include <linux/modversions.h>
        !           130: #endif
        !           131: #include <linux/module.h>
        !           132: 
        !           133: #include <linux/kernel.h>
        !           134: #include <linux/string.h>
        !           135: #include <linux/timer.h>
        !           136: #include <linux/errno.h>
        !           137: #include <linux/ioport.h>
        !           138: #if LINUX_VERSION_CODE >= 0x20400
        !           139: #include <linux/slab.h>
        !           140: #else
        !           141: #include <linux/malloc.h>
        !           142: #endif
        !           143: #include <linux/interrupt.h>
        !           144: #include <linux/pci.h>
        !           145: #include <linux/netdevice.h>
        !           146: #include <linux/etherdevice.h>
        !           147: #include <linux/skbuff.h>
        !           148: #include <asm/processor.h>             /* Processor type for cache alignment. */
        !           149: #include <asm/bitops.h>
        !           150: #include <asm/io.h>
        !           151: 
        !           152: #ifdef INLINE_PCISCAN
        !           153: #include "k_compat.h"
        !           154: #else
        !           155: #include "pci-scan.h"
        !           156: #include "kern_compat.h"
        !           157: #endif
        !           158: 
        !           159: /* Configure the PCI bus bursts and FIFO thresholds.
        !           160:           486: Set 8 longword cache alignment, 8 longword burst.
        !           161:           586: Set 16 longword cache alignment, no burst limit.
        !           162:           Cache alignment bits 15:14        Burst length 13:8
        !           163:                0000    <not allowed>           0000 align to cache     0800 8 longwords
        !           164:                4000    8  longwords            0100 1 longword         1000 16 longwords
        !           165:                8000    16 longwords            0200 2 longwords        2000 32 longwords
        !           166:                C000    32  longwords           0400 4 longwords
        !           167:        Wait the specified 50 PCI cycles after a reset by initializing
        !           168:        Tx and Rx queues and the address filter list. */
        !           169: #define TX_DESC_SIZE   16
        !           170: #if defined(__powerpc__) || defined(__sparc__)         /* Big endian */
        !           171: static int csr0 = 0x00100000 | 0xE000 | TX_DESC_SIZE;
        !           172: #elif defined(__alpha__) || defined(__x86_64) || defined(__ia64)
        !           173: static int csr0 = 0xE000 | TX_DESC_SIZE;
        !           174: #elif defined(__i386__)
        !           175: static int csr0 = 0xE000 | TX_DESC_SIZE;
        !           176: #else
        !           177: static int csr0 = 0xE000 | TX_DESC_SIZE;
        !           178: #warning Processor architecture unknown!
        !           179: #endif
        !           180: 
        !           181: 
        !           182: 
        !           183: #if (LINUX_VERSION_CODE >= 0x20100)  &&  defined(MODULE)
        !           184: char kernel_version[] = UTS_RELEASE;
        !           185: #endif
        !           186: 
        !           187: MODULE_AUTHOR("Donald Becker <[email protected]>");
        !           188: MODULE_DESCRIPTION("Winbond W89c840 Ethernet driver");
        !           189: MODULE_LICENSE("GPL");
        !           190: MODULE_PARM(max_interrupt_work, "i");
        !           191: MODULE_PARM(debug, "i");
        !           192: MODULE_PARM(rx_copybreak, "i");
        !           193: MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
        !           194: MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
        !           195: MODULE_PARM(multicast_filter_limit, "i");
        !           196: MODULE_PARM_DESC(debug, "Driver message level (0-31)");
        !           197: MODULE_PARM_DESC(options, "Force transceiver type or fixed speed+duplex");
        !           198: MODULE_PARM_DESC(max_interrupt_work,
        !           199:                                 "Driver maximum events handled per interrupt");
        !           200: MODULE_PARM_DESC(full_duplex, "Non-zero to set forced full duplex.");
        !           201: MODULE_PARM_DESC(rx_copybreak,
        !           202:                                 "Breakpoint in bytes for copy-only-tiny-frames");
        !           203: MODULE_PARM_DESC(multicast_filter_limit,
        !           204:                                 "Multicast addresses before switching to Rx-all-multicast");
        !           205: 
        !           206: /*
        !           207:                                Theory of Operation
        !           208: 
        !           209: I. Board Compatibility
        !           210: 
        !           211: This driver is for the Winbond w89c840 chip.
        !           212: 
        !           213: II. Board-specific settings
        !           214: 
        !           215: None.
        !           216: 
        !           217: III. Driver operation
        !           218: 
        !           219: This chip is very similar to the Digital 21*4* "Tulip" family.  The first
        !           220: twelve registers and the descriptor format are nearly identical.  Read a
        !           221: Tulip manual for operational details.
        !           222: 
        !           223: A significant difference is that the multicast filter and station address are
        !           224: stored in registers rather than loaded through a pseudo-transmit packet.
        !           225: 
        !           226: Unlike the Tulip, transmit buffers are limited to 1KB.  To transmit a
        !           227: full-sized packet we must use both data buffers in a descriptor.  Thus the
        !           228: driver uses ring mode where descriptors are implicitly sequential in memory,
        !           229: rather than using the second descriptor address as a chain pointer to
        !           230: subsequent descriptors.
        !           231: 
        !           232: IV. Notes
        !           233: 
        !           234: If you are going to almost clone a Tulip, why not go all the way and avoid
        !           235: the need for a new driver?
        !           236: 
        !           237: IVb. References
        !           238: 
        !           239: http://www.scyld.com/expert/100mbps.html
        !           240: http://www.scyld.com/expert/NWay.html
        !           241: http://www.winbond.com.tw/
        !           242: 
        !           243: IVc. Errata
        !           244: 
        !           245: A horrible bug exists in the transmit FIFO.  Apparently the chip doesn't
        !           246: correctly detect a full FIFO, and queuing more than 2048 bytes may result in
        !           247: silent data corruption.
        !           248: 
        !           249: */
        !           250: 
        !           251: 
        !           252: 
        !           253: /*
        !           254:   PCI probe table.
        !           255: */
        !           256: static void *w840_probe1(struct pci_dev *pdev, void *init_dev,
        !           257:                                                 long ioaddr, int irq, int chip_idx, int find_cnt);
        !           258: static int winbond_pwr_event(void *dev_instance, int event);
        !           259: enum chip_capability_flags {
        !           260:        CanHaveMII=1, HasBrokenTx=2, AlwaysFDX=4, FDXOnNoMII=8,};
        !           261: #ifdef USE_IO_OPS
        !           262: #define W840_FLAGS (PCI_USES_IO | PCI_ADDR0 | PCI_USES_MASTER)
        !           263: #else
        !           264: #define W840_FLAGS (PCI_USES_MEM | PCI_ADDR1 | PCI_USES_MASTER)
        !           265: #endif
        !           266: 
        !           267: static struct pci_id_info pci_id_tbl[] = {
        !           268:        {"Winbond W89c840",                     /* Sometime a Level-One switch card. */
        !           269:         { 0x08401050, 0xffffffff, 0x81530000, 0xffff0000 },
        !           270:         W840_FLAGS, 128, CanHaveMII | HasBrokenTx | FDXOnNoMII},
        !           271:        {"Winbond W89c840", { 0x08401050, 0xffffffff, },
        !           272:         W840_FLAGS, 128, CanHaveMII | HasBrokenTx},
        !           273:        {"Compex RL100-ATX", { 0x201111F6, 0xffffffff,},
        !           274:         W840_FLAGS, 128, CanHaveMII | HasBrokenTx},
        !           275:        {0,},                                           /* 0 terminated list. */
        !           276: };
        !           277: 
        !           278: struct drv_id_info winbond840_drv_id = {
        !           279:        "winbond-840", PCI_HOTSWAP, PCI_CLASS_NETWORK_ETHERNET<<8, pci_id_tbl,
        !           280:        w840_probe1, winbond_pwr_event };
        !           281: 
        !           282: /* This driver was written to use PCI memory space, however some x86 systems
        !           283:    work only with I/O space accesses.  Pass -DUSE_IO_OPS to use PCI I/O space
        !           284:    accesses instead of memory space. */
        !           285: 
        !           286: #ifdef USE_IO_OPS
        !           287: #undef readb
        !           288: #undef readw
        !           289: #undef readl
        !           290: #undef writeb
        !           291: #undef writew
        !           292: #undef writel
        !           293: #define readb inb
        !           294: #define readw inw
        !           295: #define readl inl
        !           296: #define writeb outb
        !           297: #define writew outw
        !           298: #define writel outl
        !           299: #endif
        !           300: 
        !           301: /* Offsets to the Command and Status Registers, "CSRs".
        !           302:    While similar to the Tulip, these registers are longword aligned.
        !           303:    Note: It's not useful to define symbolic names for every register bit in
        !           304:    the device.  The name can only partially document the semantics and make
        !           305:    the driver longer and more difficult to read.
        !           306: */
        !           307: enum w840_offsets {
        !           308:        PCIBusCfg=0x00, TxStartDemand=0x04, RxStartDemand=0x08,
        !           309:        RxRingPtr=0x0C, TxRingPtr=0x10,
        !           310:        IntrStatus=0x14, NetworkConfig=0x18, IntrEnable=0x1C,
        !           311:        RxMissed=0x20, EECtrl=0x24, MIICtrl=0x24, BootRom=0x28, GPTimer=0x2C,
        !           312:        CurRxDescAddr=0x30, CurRxBufAddr=0x34,                  /* Debug use */
        !           313:        MulticastFilter0=0x38, MulticastFilter1=0x3C, StationAddr=0x40,
        !           314:        CurTxDescAddr=0x4C, CurTxBufAddr=0x50,
        !           315: };
        !           316: 
        !           317: /* Bits in the interrupt status/enable registers. */
        !           318: /* The bits in the Intr Status/Enable registers, mostly interrupt sources. */
        !           319: enum intr_status_bits {
        !           320:        NormalIntr=0x10000, AbnormalIntr=0x8000,
        !           321:        IntrPCIErr=0x2000, TimerInt=0x800,
        !           322:        IntrRxDied=0x100, RxNoBuf=0x80, IntrRxDone=0x40,
        !           323:        TxFIFOUnderflow=0x20, RxErrIntr=0x10,
        !           324:        TxIdle=0x04, IntrTxStopped=0x02, IntrTxDone=0x01,
        !           325: };
        !           326: 
        !           327: /* Bits in the NetworkConfig register. */
        !           328: enum rx_mode_bits {
        !           329:        TxOn=0x2000, RxOn=0x0002, FullDuplex=0x0200,
        !           330:        AcceptErr=0x80, AcceptRunt=0x40,                /* Not used */
        !           331:        AcceptBroadcast=0x20, AcceptMulticast=0x10, AcceptAllPhys=0x08,
        !           332: };
        !           333: 
        !           334: enum mii_reg_bits {
        !           335:        MDIO_ShiftClk=0x10000, MDIO_DataIn=0x80000, MDIO_DataOut=0x20000,
        !           336:        MDIO_EnbOutput=0x40000, MDIO_EnbIn = 0x00000,
        !           337: };
        !           338: 
        !           339: /* The Tulip-like Rx and Tx buffer descriptors. */
        !           340: struct w840_rx_desc {
        !           341:        s32 status;
        !           342:        s32 length;
        !           343:        u32 buffer1;
        !           344:        u32 next_desc;
        !           345: };
        !           346: 
        !           347: struct w840_tx_desc {
        !           348:        s32 status;
        !           349:        s32 length;
        !           350:        u32 buffer1, buffer2;                           /* We use only buffer 1.  */
        !           351:        char pad[TX_DESC_SIZE - 16];
        !           352: };
        !           353: 
        !           354: /* Bits in network_desc.status */
        !           355: enum desc_status_bits {
        !           356:        DescOwn=0x80000000, DescEndRing=0x02000000, DescUseLink=0x01000000,
        !           357:        DescWholePkt=0x60000000, DescStartPkt=0x20000000, DescEndPkt=0x40000000,
        !           358:        DescIntr=0x80000000,
        !           359: };
        !           360: 
        !           361: #define PRIV_ALIGN     15      /* Required alignment mask */
        !           362: struct netdev_private {
        !           363:        /* Descriptor rings first for alignment. */
        !           364:        struct w840_rx_desc rx_ring[RX_RING_SIZE];
        !           365:        struct w840_tx_desc tx_ring[TX_RING_SIZE];
        !           366:        struct net_device *next_module;         /* Link for devices of this type. */
        !           367:        void *priv_addr;                                        /* Unaligned address for kfree */
        !           368:        const char *product_name;
        !           369:        /* The addresses of receive-in-place skbuffs. */
        !           370:        struct sk_buff* rx_skbuff[RX_RING_SIZE];
        !           371:        /* The saved address of a sent-in-place packet/buffer, for later free(). */
        !           372:        struct sk_buff* tx_skbuff[TX_RING_SIZE];
        !           373:        struct net_device_stats stats;
        !           374:        struct timer_list timer;        /* Media monitoring timer. */
        !           375:        /* Frequently used values: keep some adjacent for cache effect. */
        !           376:        int msg_level;
        !           377:        int chip_id, drv_flags;
        !           378:        struct pci_dev *pci_dev;
        !           379:        int csr0, csr6;
        !           380:        unsigned int polling;                           /* Switched to polling mode. */
        !           381:        int max_interrupt_work;
        !           382: 
        !           383:        struct w840_rx_desc *rx_head_desc;
        !           384:        unsigned int rx_ring_size;
        !           385:        unsigned int cur_rx, dirty_rx;          /* Producer/consumer ring indices */
        !           386:        unsigned int rx_buf_sz;                         /* Based on MTU+slack. */
        !           387:        int rx_copybreak;
        !           388: 
        !           389:        unsigned int tx_ring_size;
        !           390:        unsigned int cur_tx, dirty_tx;
        !           391:        unsigned int tx_q_bytes, tx_unq_bytes;
        !           392:        unsigned int tx_full:1;                         /* The Tx queue is full. */
        !           393: 
        !           394:        /* These values track of the transceiver/media in use. */
        !           395:        unsigned int full_duplex:1;                     /* Full-duplex operation requested. */
        !           396:        unsigned int duplex_lock:1;
        !           397:        unsigned int medialock:1;                       /* Do not sense media. */
        !           398:        unsigned int default_port;                      /* Last dev->if_port value. */
        !           399:        /* Rx filter. */
        !           400:        u32 cur_rx_mode;
        !           401:        u32 rx_filter[2];
        !           402:        int multicast_filter_limit;
        !           403: 
        !           404:        /* MII transceiver section. */
        !           405:        int mii_cnt;                                            /* MII device addresses. */
        !           406:        u16 advertising;                                        /* NWay media advertisement */
        !           407:        unsigned char phys[2];                          /* MII device addresses. */
        !           408: };
        !           409: 
        !           410: static int  eeprom_read(long ioaddr, int location);
        !           411: static int  mdio_read(struct net_device *dev, int phy_id, int location);
        !           412: static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
        !           413: static int  netdev_open(struct net_device *dev);
        !           414: static void check_duplex(struct net_device *dev);
        !           415: static void netdev_timer(unsigned long data);
        !           416: static void tx_timeout(struct net_device *dev);
        !           417: static void init_ring(struct net_device *dev);
        !           418: static int  start_tx(struct sk_buff *skb, struct net_device *dev);
        !           419: static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);
        !           420: static void netdev_error(struct net_device *dev, int intr_status);
        !           421: static int  netdev_rx(struct net_device *dev);
        !           422: static void netdev_error(struct net_device *dev, int intr_status);
        !           423: static inline unsigned ether_crc(int length, unsigned char *data);
        !           424: static void set_rx_mode(struct net_device *dev);
        !           425: static struct net_device_stats *get_stats(struct net_device *dev);
        !           426: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
        !           427: static int  netdev_close(struct net_device *dev);
        !           428: 
        !           429: 
        !           430: 
        !           431: /* A list of our installed devices, for removing the driver module. */
        !           432: static struct net_device *root_net_dev = NULL;
        !           433: 
        !           434: static void *w840_probe1(struct pci_dev *pdev, void *init_dev,
        !           435:                                                 long ioaddr, int irq, int chip_idx, int card_idx)
        !           436: {
        !           437:        struct net_device *dev;
        !           438:        struct netdev_private *np;
        !           439:        void *priv_mem;
        !           440:        int i, option = card_idx < MAX_UNITS ? options[card_idx] : 0;
        !           441: 
        !           442:        dev = init_etherdev(init_dev, 0);
        !           443:        if (!dev)
        !           444:                return NULL;
        !           445: 
        !           446: #if LINUX_VERSION_CODE < 0x20155
        !           447:        printk(KERN_INFO "%s: %s at 0x%lx, %2.2x:%2.2x",
        !           448:                   dev->name, pci_id_tbl[chip_idx].name, ioaddr,
        !           449:                   pci_bus_number(pdev), pci_devfn(pdev)>>3);
        !           450: #else
        !           451:        printk(KERN_INFO "%s: %s at 0x%lx, %2.2x:%2.2x",
        !           452:                   dev->name, pci_id_tbl[chip_idx].name, ioaddr,
        !           453:                   pdev->bus->number, pdev->devfn>>3);
        !           454: #endif
        !           455: 
        !           456:        /* Warning: validate for big-endian machines. */
        !           457:        for (i = 0; i < 3; i++)
        !           458:                ((u16 *)dev->dev_addr)[i] = le16_to_cpu(eeprom_read(ioaddr, i));
        !           459: 
        !           460:        for (i = 0; i < 5; i++)
        !           461:                printk("%2.2x:", dev->dev_addr[i]);
        !           462:        printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);
        !           463: 
        !           464:        priv_mem = kmalloc(sizeof(*np) + PRIV_ALIGN, GFP_KERNEL);
        !           465:        /* Out of memory is very unlikely. */
        !           466:        if (priv_mem == NULL)
        !           467:                return NULL;
        !           468: 
        !           469: #ifdef USE_IO_OPS
        !           470:        request_region(ioaddr, pci_id_tbl[chip_idx].io_size, dev->name);
        !           471: #endif
        !           472: 
        !           473:        /* Reset the chip to erase previous misconfiguration.
        !           474:           No hold time required! */
        !           475:        writel(0x00000001, ioaddr + PCIBusCfg);
        !           476: 
        !           477:        dev->base_addr = ioaddr;
        !           478:        dev->irq = irq;
        !           479: 
        !           480:        /* The descriptor lists must be aligned. */
        !           481:        dev->priv = np = (void *)(((long)priv_mem + PRIV_ALIGN) & ~PRIV_ALIGN);
        !           482:        memset(np, 0, sizeof(*np));
        !           483:        np->priv_addr = priv_mem;
        !           484: 
        !           485:        np->next_module = root_net_dev;
        !           486:        root_net_dev = dev;
        !           487: 
        !           488:        np->pci_dev = pdev;
        !           489:        np->chip_id = chip_idx;
        !           490:        np->drv_flags = pci_id_tbl[chip_idx].drv_flags;
        !           491:        np->msg_level = (1 << debug) - 1;
        !           492:        np->rx_copybreak = rx_copybreak;
        !           493:        np->max_interrupt_work = max_interrupt_work;
        !           494:        np->multicast_filter_limit = multicast_filter_limit;
        !           495:        np->tx_ring_size = TX_RING_SIZE;
        !           496:        np->rx_ring_size = RX_RING_SIZE;
        !           497: 
        !           498:        if (dev->mem_start)
        !           499:                option = dev->mem_start;
        !           500: 
        !           501:        if ((card_idx < MAX_UNITS  &&  full_duplex[card_idx] > 0)
        !           502:                || (np->drv_flags & AlwaysFDX))
        !           503:                np->full_duplex = 1;
        !           504: 
        !           505:        /* The chip-specific entries in the device structure. */
        !           506:        dev->open = &netdev_open;
        !           507:        dev->hard_start_xmit = &start_tx;
        !           508:        dev->stop = &netdev_close;
        !           509:        dev->get_stats = &get_stats;
        !           510:        dev->set_multicast_list = &set_rx_mode;
        !           511:        dev->do_ioctl = &mii_ioctl;
        !           512: 
        !           513:        if (np->drv_flags & CanHaveMII) {
        !           514:                int phy, phy_idx = 0;
        !           515:                for (phy = 1; phy < 32 && phy_idx < 4; phy++) {
        !           516:                        int mii_status = mdio_read(dev, phy, 1);
        !           517:                        if (mii_status != 0xffff  &&  mii_status != 0x0000) {
        !           518:                                np->phys[phy_idx++] = phy;
        !           519:                                np->advertising = mdio_read(dev, phy, 4);
        !           520:                                printk(KERN_INFO "%s: MII PHY found at address %d, status "
        !           521:                                           "0x%4.4x advertising %4.4x.\n",
        !           522:                                           dev->name, phy, mii_status, np->advertising);
        !           523:                        }
        !           524:                }
        !           525:                np->mii_cnt = phy_idx;
        !           526:                if (phy_idx == 0) {
        !           527:                        printk(KERN_WARNING "%s: MII PHY not found -- this device may "
        !           528:                                   "not operate correctly.\n"
        !           529:                                   KERN_WARNING "%s:  If this is a switch card, explicitly "
        !           530:                                   "force full duplex on this interface.\n",
        !           531:                                   dev->name, dev->name);
        !           532:                        if (np->drv_flags & FDXOnNoMII) {
        !           533:                                printk(KERN_INFO "%s:  Assuming a switch card, forcing full "
        !           534:                                           "duplex.\n", dev->name);
        !           535:                                np->full_duplex = np->duplex_lock = 1;
        !           536:                        }
        !           537:                }
        !           538:        }
        !           539:        /* Allow forcing the media type. */
        !           540:        if (np->full_duplex) {
        !           541:                printk(KERN_INFO "%s: Set to forced full duplex, autonegotiation"
        !           542:                           " disabled.\n", dev->name);
        !           543:                np->duplex_lock = 1;
        !           544:        }
        !           545:        if (option > 0) {
        !           546:                if (option & 0x220)
        !           547:                        np->full_duplex = 1;
        !           548:                np->default_port = option & 0x3ff;
        !           549:                if (np->default_port & 0x330) {
        !           550:                        np->medialock = 1;
        !           551:                        printk(KERN_INFO "  Forcing %dMbs %s-duplex operation.\n",
        !           552:                                   (option & 0x300 ? 100 : 10),
        !           553:                                   (np->full_duplex ? "full" : "half"));
        !           554:                        if (np->mii_cnt)
        !           555:                                mdio_write(dev, np->phys[0], 0,
        !           556:                                                   ((option & 0x300) ? 0x2000 : 0) |    /* 100mbps? */
        !           557:                                                   (np->full_duplex ? 0x0100 : 0)); /* Full duplex? */
        !           558:                }
        !           559:        }
        !           560: 
        !           561:        return dev;
        !           562: }
        !           563: 
        !           564: 
        !           565: /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.
        !           566:    The Winbond NIC uses serial bit streams generated by the host processor. */
        !           567: 
        !           568: /* Delay between EEPROM clock transitions.
        !           569:    This "delay" is to force out buffered PCI writes. */
        !           570: #define eeprom_delay(ee_addr)  readl(ee_addr)
        !           571: 
        !           572: enum EEPROM_Ctrl_Bits {
        !           573:        EE_ShiftClk=0x02, EE_Write0=0x801, EE_Write1=0x805,
        !           574:        EE_ChipSelect=0x801, EE_DataIn=0x08,
        !           575: };
        !           576: 
        !           577: /* The EEPROM commands always start with 01.. preamble bits.
        !           578:    Commands are prepended to the variable-length address. */
        !           579: enum EEPROM_Cmds {
        !           580:        EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
        !           581: };
        !           582: 
        !           583: static int eeprom_read(long addr, int location)
        !           584: {
        !           585:        int i;
        !           586:        int retval = 0;
        !           587:        long ee_addr = addr + EECtrl;
        !           588:        int read_cmd = location | EE_ReadCmd;
        !           589: 
        !           590:        writel(EE_ChipSelect, ee_addr);
        !           591:        /* Shift the read command bits out. */
        !           592:        for (i = 10; i >= 0; i--) {
        !           593:                short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
        !           594:                writel(dataval, ee_addr);
        !           595:                eeprom_delay(ee_addr);
        !           596:                writel(dataval | EE_ShiftClk, ee_addr);
        !           597:                eeprom_delay(ee_addr);
        !           598:        }
        !           599:        writel(EE_ChipSelect, ee_addr);
        !           600:        eeprom_delay(ee_addr);
        !           601: 
        !           602:        for (i = 16; i > 0; i--) {
        !           603:                writel(EE_ChipSelect | EE_ShiftClk, ee_addr);
        !           604:                eeprom_delay(ee_addr);
        !           605:                retval = (retval << 1) | ((readl(ee_addr) & EE_DataIn) ? 1 : 0);
        !           606:                writel(EE_ChipSelect, ee_addr);
        !           607:                eeprom_delay(ee_addr);
        !           608:        }
        !           609: 
        !           610:        /* Terminate the EEPROM access. */
        !           611:        writel(0, ee_addr);
        !           612:        return retval;
        !           613: }
        !           614: 
        !           615: /*  MII transceiver control section.
        !           616:        Read and write the MII registers using software-generated serial
        !           617:        MDIO protocol.  See the MII specifications or DP83840A data sheet
        !           618:        for details.
        !           619: 
        !           620:        The maximum data clock rate is 2.5 Mhz.
        !           621:        The timing is decoupled from the processor clock by flushing the write
        !           622:        from the CPU write buffer with a following read, and using PCI
        !           623:        transaction time. */
        !           624: #define mdio_in(mdio_addr) readl(mdio_addr)
        !           625: #define mdio_out(value, mdio_addr) writel(value, mdio_addr)
        !           626: #define mdio_delay(mdio_addr) readl(mdio_addr)
        !           627: 
        !           628: /* Set iff a MII transceiver on any interface requires mdio preamble.
        !           629:    This only set with older tranceivers, so the extra
        !           630:    code size of a per-interface flag is not worthwhile. */
        !           631: static char mii_preamble_required = 1;
        !           632: 
        !           633: #define MDIO_WRITE0 (MDIO_EnbOutput)
        !           634: #define MDIO_WRITE1 (MDIO_DataOut | MDIO_EnbOutput)
        !           635: 
        !           636: /* Generate the preamble required for initial synchronization and
        !           637:    a few older transceivers. */
        !           638: static void mdio_sync(long mdio_addr)
        !           639: {
        !           640:        int bits = 32;
        !           641: 
        !           642:        /* Establish sync by sending at least 32 logic ones. */
        !           643:        while (--bits >= 0) {
        !           644:                mdio_out(MDIO_WRITE1, mdio_addr);
        !           645:                mdio_delay(mdio_addr);
        !           646:                mdio_out(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
        !           647:                mdio_delay(mdio_addr);
        !           648:        }
        !           649: }
        !           650: 
        !           651: static int mdio_read(struct net_device *dev, int phy_id, int location)
        !           652: {
        !           653:        long mdio_addr = dev->base_addr + MIICtrl;
        !           654:        int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
        !           655:        int i, retval = 0;
        !           656: 
        !           657:        if (mii_preamble_required)
        !           658:                mdio_sync(mdio_addr);
        !           659: 
        !           660:        /* Shift the read command bits out. */
        !           661:        for (i = 15; i >= 0; i--) {
        !           662:                int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
        !           663: 
        !           664:                mdio_out(dataval, mdio_addr);
        !           665:                mdio_delay(mdio_addr);
        !           666:                mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
        !           667:                mdio_delay(mdio_addr);
        !           668:        }
        !           669:        /* Read the two transition, 16 data, and wire-idle bits. */
        !           670:        for (i = 20; i > 0; i--) {
        !           671:                mdio_out(MDIO_EnbIn, mdio_addr);
        !           672:                mdio_delay(mdio_addr);
        !           673:                retval = (retval << 1) | ((mdio_in(mdio_addr) & MDIO_DataIn) ? 1 : 0);
        !           674:                mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
        !           675:                mdio_delay(mdio_addr);
        !           676:        }
        !           677:        return (retval>>1) & 0xffff;
        !           678: }
        !           679: 
        !           680: static void mdio_write(struct net_device *dev, int phy_id, int reg, int value)
        !           681: {
        !           682:        long mdio_addr = dev->base_addr + MIICtrl;
        !           683:        int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (reg<<18) | value;
        !           684:        int i;
        !           685: 
        !           686:        if (mii_preamble_required)
        !           687:                mdio_sync(mdio_addr);
        !           688: 
        !           689:        /* Shift the command bits out. */
        !           690:        for (i = 31; i >= 0; i--) {
        !           691:                int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
        !           692: 
        !           693:                mdio_out(dataval, mdio_addr);
        !           694:                mdio_delay(mdio_addr);
        !           695:                mdio_out(dataval | MDIO_ShiftClk, mdio_addr);
        !           696:                mdio_delay(mdio_addr);
        !           697:        }
        !           698:        /* Clear out extra bits. */
        !           699:        for (i = 2; i > 0; i--) {
        !           700:                mdio_out(MDIO_EnbIn, mdio_addr);
        !           701:                mdio_delay(mdio_addr);
        !           702:                mdio_out(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
        !           703:                mdio_delay(mdio_addr);
        !           704:        }
        !           705:        return;
        !           706: }
        !           707: 
        !           708: 
        !           709: static int netdev_open(struct net_device *dev)
        !           710: {
        !           711:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !           712:        long ioaddr = dev->base_addr;
        !           713:        int i;
        !           714: 
        !           715:        writel(0x00000001, ioaddr + PCIBusCfg);         /* Reset */
        !           716: 
        !           717:        MOD_INC_USE_COUNT;
        !           718: 
        !           719:        if (request_irq(dev->irq, &intr_handler, SA_SHIRQ, dev->name, dev)) {
        !           720:                MOD_DEC_USE_COUNT;
        !           721:                return -EAGAIN;
        !           722:        }
        !           723: 
        !           724:        if (np->msg_level & NETIF_MSG_IFUP)
        !           725:                printk(KERN_DEBUG "%s: w89c840_open() irq %d.\n",
        !           726:                           dev->name, dev->irq);
        !           727: 
        !           728:        init_ring(dev);
        !           729: 
        !           730:        writel(virt_to_bus(np->rx_ring), ioaddr + RxRingPtr);
        !           731:        writel(virt_to_bus(np->tx_ring), ioaddr + TxRingPtr);
        !           732: 
        !           733:        for (i = 0; i < 6; i++)
        !           734:                writeb(dev->dev_addr[i], ioaddr + StationAddr + i);
        !           735: 
        !           736:        /* Initialize other registers. */
        !           737:        np->csr0 = csr0;
        !           738:        writel(np->csr0, ioaddr + PCIBusCfg);
        !           739: 
        !           740:        if (dev->if_port == 0)
        !           741:                dev->if_port = np->default_port;
        !           742: 
        !           743:        writel(0, ioaddr + RxStartDemand);
        !           744:        np->csr6 = np->full_duplex ? 0x20022202 : 0x20022002;
        !           745:        check_duplex(dev);
        !           746:        set_rx_mode(dev);
        !           747: 
        !           748:        netif_start_tx_queue(dev);
        !           749: 
        !           750:        /* Clear and Enable interrupts by setting the interrupt mask.
        !           751:           See enum intr_status_bits above for bit guide.
        !           752:           We omit: TimerInt, IntrRxDied, IntrTxStopped
        !           753:        */
        !           754:        writel(0x1A0F5, ioaddr + IntrStatus);
        !           755:        writel(0x1A0F5, ioaddr + IntrEnable);
        !           756: 
        !           757:        if (np->msg_level & NETIF_MSG_IFUP)
        !           758:                printk(KERN_DEBUG "%s: Done netdev_open().\n", dev->name);
        !           759: 
        !           760:        /* Set the timer to check for link beat. */
        !           761:        init_timer(&np->timer);
        !           762:        np->timer.expires = jiffies + 3*HZ;
        !           763:        np->timer.data = (unsigned long)dev;
        !           764:        np->timer.function = &netdev_timer;                             /* timer handler */
        !           765:        add_timer(&np->timer);
        !           766: 
        !           767:        return 0;
        !           768: }
        !           769: 
        !           770: static void check_duplex(struct net_device *dev)
        !           771: {
        !           772:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !           773:        int mii_reg5 = mdio_read(dev, np->phys[0], 5);
        !           774:        int negotiated =  mii_reg5 & np->advertising;
        !           775:        int duplex;
        !           776: 
        !           777:        if (np->duplex_lock  ||  mii_reg5 == 0xffff)
        !           778:                return;
        !           779:        duplex = (negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040;
        !           780:        if (np->full_duplex != duplex) {
        !           781:                np->full_duplex = duplex;
        !           782:                if (np->msg_level & NETIF_MSG_LINK)
        !           783:                        printk(KERN_INFO "%s: Setting %s-duplex based on MII #%d "
        !           784:                                   "negotiated capability %4.4x.\n", dev->name,
        !           785:                                   duplex ? "full" : "half", np->phys[0], negotiated);
        !           786:                np->csr6 &= ~0x200;
        !           787:                np->csr6 |= duplex ? 0x200 : 0;
        !           788:        }
        !           789: }
        !           790: 
        !           791: static void netdev_timer(unsigned long data)
        !           792: {
        !           793:        struct net_device *dev = (struct net_device *)data;
        !           794:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !           795:        long ioaddr = dev->base_addr;
        !           796:        int next_tick = 10*HZ;
        !           797:        int old_csr6 = np->csr6;
        !           798:        u32 intr_status = readl(ioaddr + IntrStatus);
        !           799: 
        !           800:        if (np->msg_level & NETIF_MSG_TIMER)
        !           801:                printk(KERN_DEBUG "%s: Media selection timer tick, status %8.8x "
        !           802:                           "config %8.8x.\n",
        !           803:                           dev->name, intr_status, (int)readl(ioaddr + NetworkConfig));
        !           804:        /* Check for blocked interrupts. */
        !           805:        if (np->polling) {
        !           806:                if (intr_status & 0x1ffff) {
        !           807:                        intr_handler(dev->irq, dev, 0);
        !           808:                        next_tick = 1;
        !           809:                        np->polling = 1;
        !           810:                } else if (++np->polling > 10*HZ)
        !           811:                        np->polling = 0;
        !           812:                else
        !           813:                        next_tick = 2;
        !           814:        } else if ((intr_status & 0x1ffff)) {
        !           815:                np->polling = 1;
        !           816:        }
        !           817: 
        !           818:        if (netif_queue_paused(dev)  &&
        !           819:                np->cur_tx - np->dirty_tx > 1  &&
        !           820:                (jiffies - dev->trans_start) > TX_TIMEOUT) {
        !           821:                tx_timeout(dev);
        !           822:        }
        !           823:        check_duplex(dev);
        !           824:        if (np->csr6 != old_csr6) {
        !           825:                writel(np->csr6 & ~0x0002, ioaddr + NetworkConfig);
        !           826:                writel(np->csr6 | 0x2002, ioaddr + NetworkConfig);
        !           827:        }
        !           828:        np->timer.expires = jiffies + next_tick;
        !           829:        add_timer(&np->timer);
        !           830: }
        !           831: 
        !           832: static void tx_timeout(struct net_device *dev)
        !           833: {
        !           834:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !           835:        long ioaddr = dev->base_addr;
        !           836: 
        !           837:        printk(KERN_WARNING "%s: Transmit timed out, status %8.8x,"
        !           838:                   " resetting...\n", dev->name, (int)readl(ioaddr + IntrStatus));
        !           839: 
        !           840: #ifndef __alpha__
        !           841:        if (np->msg_level & NETIF_MSG_TX_ERR) {
        !           842:                int i;
        !           843:                printk(KERN_DEBUG "  Rx ring %p: ", np->rx_ring);
        !           844:                for (i = 0; i < np->rx_ring_size; i++)
        !           845:                        printk(" %8.8x", (unsigned int)np->rx_ring[i].status);
        !           846:                printk("\n"KERN_DEBUG"  Tx ring %p: ", np->tx_ring);
        !           847:                for (i = 0; i < np->tx_ring_size; i++)
        !           848:                        printk(" %8.8x", np->tx_ring[i].status);
        !           849:                printk("\n");
        !           850:        }
        !           851: #endif
        !           852: 
        !           853:        /* Perhaps we should reinitialize the hardware here.  Just trigger a
        !           854:           Tx demand for now. */
        !           855:        writel(0, ioaddr + TxStartDemand);
        !           856:        dev->if_port = 0;
        !           857:        /* Stop and restart the chip's Tx processes . */
        !           858: 
        !           859:        dev->trans_start = jiffies;
        !           860:        np->stats.tx_errors++;
        !           861:        return;
        !           862: }
        !           863: 
        !           864: 
        !           865: /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
        !           866: static void init_ring(struct net_device *dev)
        !           867: {
        !           868:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !           869:        int i;
        !           870: 
        !           871:        np->tx_full = 0;
        !           872:        np->cur_tx = np->dirty_tx = 0;
        !           873:        np->tx_q_bytes = np->tx_unq_bytes = 0;
        !           874: 
        !           875:        np->cur_rx = np->dirty_rx = 0;
        !           876:        np->rx_buf_sz = (dev->mtu <= 1522 ? PKT_BUF_SZ : dev->mtu + 14);
        !           877:        np->rx_head_desc = &np->rx_ring[0];
        !           878: 
        !           879:        /* Initialize all Rx descriptors. */
        !           880:        for (i = 0; i < np->rx_ring_size; i++) {
        !           881:                np->rx_ring[i].length = np->rx_buf_sz;
        !           882:                np->rx_ring[i].status = 0;
        !           883:                np->rx_ring[i].next_desc = virt_to_bus(&np->rx_ring[i+1]);
        !           884:                np->rx_skbuff[i] = 0;
        !           885:        }
        !           886:        /* Mark the last entry as wrapping the ring. */
        !           887:        np->rx_ring[i-1].length |= DescEndRing;
        !           888:        np->rx_ring[i-1].next_desc = virt_to_bus(&np->rx_ring[0]);
        !           889: 
        !           890:        /* Fill in the Rx buffers.  Handle allocation failure gracefully. */
        !           891:        for (i = 0; i < np->rx_ring_size; i++) {
        !           892:                struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
        !           893:                np->rx_skbuff[i] = skb;
        !           894:                if (skb == NULL)
        !           895:                        break;
        !           896:                skb->dev = dev;                 /* Mark as being used by this device. */
        !           897:                np->rx_ring[i].buffer1 = virt_to_bus(skb->tail);
        !           898:                np->rx_ring[i].status = DescOwn | DescIntr;
        !           899:        }
        !           900:        np->dirty_rx = (unsigned int)(i - np->rx_ring_size);
        !           901: 
        !           902:        for (i = 0; i < np->tx_ring_size; i++) {
        !           903:                np->tx_skbuff[i] = 0;
        !           904:                np->tx_ring[i].status = 0;
        !           905:        }
        !           906:        return;
        !           907: }
        !           908: 
        !           909: static int start_tx(struct sk_buff *skb, struct net_device *dev)
        !           910: {
        !           911:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !           912:        unsigned entry;
        !           913: 
        !           914:        /* Block a timer-based transmit from overlapping. */
        !           915:        if (netif_pause_tx_queue(dev) != 0) {
        !           916:                /* This watchdog code is redundant with the media monitor timer. */
        !           917:                if (jiffies - dev->trans_start > TX_TIMEOUT)
        !           918:                        tx_timeout(dev);
        !           919:                return 1;
        !           920:        }
        !           921: 
        !           922:        /* Note: Ordering is important here, set the field with the
        !           923:           "ownership" bit last, and only then increment cur_tx. */
        !           924: 
        !           925:        /* Calculate the next Tx descriptor entry. */
        !           926:        entry = np->cur_tx % np->tx_ring_size;
        !           927: 
        !           928:        np->tx_skbuff[entry] = skb;
        !           929:        np->tx_ring[entry].buffer1 = virt_to_bus(skb->data);
        !           930: 
        !           931: #define one_buffer
        !           932: #define BPT 1022
        !           933: #if defined(one_buffer)
        !           934:        np->tx_ring[entry].length = DescWholePkt | skb->len;
        !           935:        if (entry >= np->tx_ring_size-1)                 /* Wrap ring */
        !           936:                np->tx_ring[entry].length |= DescIntr | DescEndRing;
        !           937:        np->tx_ring[entry].status = DescOwn;
        !           938:        np->cur_tx++;
        !           939: #elif defined(two_buffer)
        !           940:        if (skb->len > BPT) {
        !           941:                unsigned int entry1 = ++np->cur_tx % np->tx_ring_size;
        !           942:                np->tx_ring[entry].length = DescStartPkt | BPT;
        !           943:                np->tx_ring[entry1].length = DescEndPkt | (skb->len - BPT);
        !           944:                np->tx_ring[entry1].buffer1 = virt_to_bus((skb->data) + BPT);
        !           945:                np->tx_ring[entry1].status = DescOwn;
        !           946:                np->tx_ring[entry].status = DescOwn;
        !           947:                if (entry >= np->tx_ring_size-1)
        !           948:                        np->tx_ring[entry].length |= DescIntr|DescEndRing;
        !           949:                else if (entry1 >= np->tx_ring_size-1)
        !           950:                        np->tx_ring[entry1].length |= DescIntr|DescEndRing;
        !           951:                np->cur_tx++;
        !           952:        } else {
        !           953:                np->tx_ring[entry].length = DescWholePkt | skb->len;
        !           954:                if (entry >= np->tx_ring_size-1)                 /* Wrap ring */
        !           955:                        np->tx_ring[entry].length |= DescIntr | DescEndRing;
        !           956:                np->tx_ring[entry].status = DescOwn;
        !           957:                np->cur_tx++;
        !           958:        }
        !           959: #elif defined(split_buffer)
        !           960:        {
        !           961:                /* Work around the Tx-FIFO-full bug by splitting our transmit packet
        !           962:                   into two pieces, the first which may be loaded without overflowing
        !           963:                   the FIFO, and the second which contains the remainder of the
        !           964:                   packet.  When we get a Tx-done interrupt that frees enough room
        !           965:                   in the FIFO we mark the remainder of the packet as loadable.
        !           966: 
        !           967:                   This has the problem that the Tx descriptors are written both
        !           968:                   here and in the interrupt handler.
        !           969:                */
        !           970: 
        !           971:                int buf1size = TX_FIFO_SIZE - (np->tx_q_bytes - np->tx_unq_bytes);
        !           972:                int buf2size = skb->len - buf1size;
        !           973: 
        !           974:                if (buf2size <= 0) {            /* We fit into one descriptor. */
        !           975:                        np->tx_ring[entry].length = DescWholePkt | skb->len;
        !           976:                } else {                                /* We must use two descriptors. */
        !           977:                        unsigned int entry2;
        !           978:                        np->tx_ring[entry].length = DescIntr | DescStartPkt | buf1size;
        !           979:                        if (entry >= np->tx_ring_size-1) {               /* Wrap ring */
        !           980:                                np->tx_ring[entry].length |= DescEndRing;
        !           981:                                entry2 = 0;
        !           982:                        } else
        !           983:                                entry2 = entry + 1;
        !           984:                        np->cur_tx++;
        !           985:                        np->tx_ring[entry2].buffer1 =
        !           986:                                virt_to_bus(skb->data + buf1size);
        !           987:                        np->tx_ring[entry2].length = DescEndPkt | buf2size;
        !           988:                        if (entry2 >= np->tx_ring_size-1)                /* Wrap ring */
        !           989:                                np->tx_ring[entry2].length |= DescEndRing;
        !           990:                }
        !           991:                np->tx_ring[entry].status = DescOwn;
        !           992:                np->cur_tx++;
        !           993:        }
        !           994: #endif
        !           995:        np->tx_q_bytes += skb->len;
        !           996:        writel(0, dev->base_addr + TxStartDemand);
        !           997: 
        !           998:        /* Work around horrible bug in the chip by marking the queue as full
        !           999:           when we do not have FIFO room for a maximum sized packet. */
        !          1000:        if (np->cur_tx - np->dirty_tx > TX_QUEUE_LEN) {
        !          1001:                np->tx_full = 1;
        !          1002:                netif_stop_tx_queue(dev);
        !          1003:        } else if ((np->drv_flags & HasBrokenTx)
        !          1004:                           && np->tx_q_bytes - np->tx_unq_bytes > TX_BUG_FIFO_LIMIT) {
        !          1005:                np->tx_full = 1;
        !          1006:                netif_stop_tx_queue(dev);
        !          1007:        } else
        !          1008:                netif_unpause_tx_queue(dev);            /* Typical path */
        !          1009: 
        !          1010:        dev->trans_start = jiffies;
        !          1011: 
        !          1012:        if (np->msg_level & NETIF_MSG_TX_QUEUED) {
        !          1013:                printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n",
        !          1014:                           dev->name, np->cur_tx, entry);
        !          1015:        }
        !          1016:        return 0;
        !          1017: }
        !          1018: 
        !          1019: /* The interrupt handler does all of the Rx thread work and cleans up
        !          1020:    after the Tx thread. */
        !          1021: static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs)
        !          1022: {
        !          1023:        struct net_device *dev = (struct net_device *)dev_instance;
        !          1024:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1025:        long ioaddr = dev->base_addr;
        !          1026:        int work_limit = np->max_interrupt_work;
        !          1027: 
        !          1028:        do {
        !          1029:                u32 intr_status = readl(ioaddr + IntrStatus);
        !          1030: 
        !          1031:                /* Acknowledge all of the current interrupt sources ASAP. */
        !          1032:                writel(intr_status & 0x0001ffff, ioaddr + IntrStatus);
        !          1033: 
        !          1034:                if (np->msg_level & NETIF_MSG_INTR)
        !          1035:                        printk(KERN_DEBUG "%s: Interrupt, status %4.4x.\n",
        !          1036:                                   dev->name, intr_status);
        !          1037: 
        !          1038:                if ((intr_status & (NormalIntr|AbnormalIntr)) == 0
        !          1039:                        || intr_status == 0xffffffff)
        !          1040:                        break;
        !          1041: 
        !          1042:                if (intr_status & (IntrRxDone | RxNoBuf))
        !          1043:                        netdev_rx(dev);
        !          1044: 
        !          1045:                for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
        !          1046:                        int entry = np->dirty_tx % np->tx_ring_size;
        !          1047:                        int tx_status = np->tx_ring[entry].status;
        !          1048: 
        !          1049:                        if (tx_status < 0)
        !          1050:                                break;
        !          1051:                        if (np->msg_level & NETIF_MSG_TX_DONE)
        !          1052:                                printk(KERN_DEBUG "%s: Transmit done, Tx status %8.8x.\n",
        !          1053:                                           dev->name, tx_status);
        !          1054:                        if (tx_status & 0x8000) {               /* There was an error, log it. */
        !          1055:                                if (np->msg_level & NETIF_MSG_TX_ERR)
        !          1056:                                        printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
        !          1057:                                                   dev->name, tx_status);
        !          1058:                                np->stats.tx_errors++;
        !          1059:                                if (tx_status & 0x0104) np->stats.tx_aborted_errors++;
        !          1060:                                if (tx_status & 0x0C80) np->stats.tx_carrier_errors++;
        !          1061:                                if (tx_status & 0x0200) np->stats.tx_window_errors++;
        !          1062:                                if (tx_status & 0x0002) np->stats.tx_fifo_errors++;
        !          1063:                                if ((tx_status & 0x0080) && np->full_duplex == 0)
        !          1064:                                        np->stats.tx_heartbeat_errors++;
        !          1065: #ifdef ETHER_STATS
        !          1066:                                if (tx_status & 0x0100) np->stats.collisions16++;
        !          1067: #endif
        !          1068:                        } else {
        !          1069: #ifdef ETHER_STATS
        !          1070:                                if (tx_status & 0x0001) np->stats.tx_deferred++;
        !          1071: #endif
        !          1072: #if LINUX_VERSION_CODE > 0x20127
        !          1073:                                np->stats.tx_bytes += np->tx_skbuff[entry]->len;
        !          1074: #endif
        !          1075:                                np->stats.collisions += (tx_status >> 3) & 15;
        !          1076:                                np->stats.tx_packets++;
        !          1077:                        }
        !          1078:                        /* Free the original skb. */
        !          1079:                        np->tx_unq_bytes += np->tx_skbuff[entry]->len;
        !          1080:                        dev_free_skb_irq(np->tx_skbuff[entry]);
        !          1081:                        np->tx_skbuff[entry] = 0;
        !          1082:                }
        !          1083:                if (np->tx_full &&
        !          1084:                        np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4
        !          1085:                        &&  np->tx_q_bytes - np->tx_unq_bytes < TX_BUG_FIFO_LIMIT) {
        !          1086:                        /* The ring is no longer full, allow new TX entries. */
        !          1087:                        np->tx_full = 0;
        !          1088:                        netif_resume_tx_queue(dev);
        !          1089:                }
        !          1090: 
        !          1091:                /* Abnormal error summary/uncommon events handlers. */
        !          1092:                if (intr_status & (AbnormalIntr | TxFIFOUnderflow | IntrPCIErr |
        !          1093:                                                   TimerInt | IntrTxStopped))
        !          1094:                        netdev_error(dev, intr_status);
        !          1095: 
        !          1096:                if (--work_limit < 0) {
        !          1097:                        printk(KERN_WARNING "%s: Too much work at interrupt, "
        !          1098:                                   "status=0x%4.4x.\n", dev->name, intr_status);
        !          1099:                        /* Set the timer to re-enable the other interrupts after
        !          1100:                           10*82usec ticks. */
        !          1101:                        writel(AbnormalIntr | TimerInt, ioaddr + IntrEnable);
        !          1102:                        writel(10, ioaddr + GPTimer);
        !          1103:                        break;
        !          1104:                }
        !          1105:        } while (1);
        !          1106: 
        !          1107:        if (np->msg_level & NETIF_MSG_INTR)
        !          1108:                printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n",
        !          1109:                           dev->name, (int)readl(ioaddr + IntrStatus));
        !          1110: 
        !          1111:        return;
        !          1112: }
        !          1113: 
        !          1114: /* This routine is logically part of the interrupt handler, but separated
        !          1115:    for clarity and better register allocation. */
        !          1116: static int netdev_rx(struct net_device *dev)
        !          1117: {
        !          1118:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1119:        int entry = np->cur_rx % np->rx_ring_size;
        !          1120:        int work_limit = np->dirty_rx + np->rx_ring_size - np->cur_rx;
        !          1121: 
        !          1122:        if (np->msg_level & NETIF_MSG_RX_STATUS) {
        !          1123:                printk(KERN_DEBUG " In netdev_rx(), entry %d status %4.4x.\n",
        !          1124:                           entry, np->rx_ring[entry].status);
        !          1125:        }
        !          1126: 
        !          1127:        /* If EOP is set on the next entry, it's a new packet. Send it up. */
        !          1128:        while (--work_limit >= 0) {
        !          1129:                struct w840_rx_desc *desc = np->rx_head_desc;
        !          1130:                s32 status = desc->status;
        !          1131: 
        !          1132:                if (np->msg_level & NETIF_MSG_RX_STATUS)
        !          1133:                        printk(KERN_DEBUG "  netdev_rx() status was %8.8x.\n",
        !          1134:                                   status);
        !          1135:                if (status < 0)
        !          1136:                        break;
        !          1137:                if ((status & 0x38008300) != 0x0300) {
        !          1138:                        if ((status & 0x38000300) != 0x0300) {
        !          1139:                                /* Ingore earlier buffers. */
        !          1140:                                if ((status & 0xffff) != 0x7fff) {
        !          1141:                                        printk(KERN_WARNING "%s: Oversized Ethernet frame spanned "
        !          1142:                                                   "multiple buffers, entry %#x status %4.4x!\n",
        !          1143:                                                   dev->name, np->cur_rx, status);
        !          1144:                                        np->stats.rx_length_errors++;
        !          1145:                                }
        !          1146:                        } else if (status & 0x8000) {
        !          1147:                                /* There was a fatal error. */
        !          1148:                                if (np->msg_level & NETIF_MSG_RX_ERR)
        !          1149:                                        printk(KERN_DEBUG "%s: Receive error, Rx status %8.8x.\n",
        !          1150:                                                   dev->name, status);
        !          1151:                                np->stats.rx_errors++; /* end of a packet.*/
        !          1152:                                if (status & 0x0890) np->stats.rx_length_errors++;
        !          1153:                                if (status & 0x004C) np->stats.rx_frame_errors++;
        !          1154:                                if (status & 0x0002) np->stats.rx_crc_errors++;
        !          1155:                        }
        !          1156:                } else {
        !          1157:                        struct sk_buff *skb;
        !          1158:                        /* Omit the four octet CRC from the length. */
        !          1159:                        int pkt_len = ((status >> 16) & 0x7ff) - 4;
        !          1160: 
        !          1161:                        if (np->msg_level & NETIF_MSG_RX_STATUS)
        !          1162:                                printk(KERN_DEBUG "  netdev_rx() normal Rx pkt length %d"
        !          1163:                                           " status %x.\n", pkt_len, status);
        !          1164:                        /* Check if the packet is long enough to accept without copying
        !          1165:                           to a minimally-sized skbuff. */
        !          1166:                        if (pkt_len < np->rx_copybreak
        !          1167:                                && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
        !          1168:                                skb->dev = dev;
        !          1169:                                skb_reserve(skb, 2);    /* 16 byte align the IP header */
        !          1170:                                /* Call copy + cksum if available. */
        !          1171: #if HAS_IP_COPYSUM
        !          1172:                                eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0);
        !          1173:                                skb_put(skb, pkt_len);
        !          1174: #else
        !          1175:                                memcpy(skb_put(skb, pkt_len), np->rx_skbuff[entry]->tail,
        !          1176:                                           pkt_len);
        !          1177: #endif
        !          1178:                        } else {
        !          1179:                                char *temp = skb_put(skb = np->rx_skbuff[entry], pkt_len);
        !          1180:                                np->rx_skbuff[entry] = NULL;
        !          1181: #ifndef final_version                          /* Remove after testing. */
        !          1182:                                if (bus_to_virt(desc->buffer1) != temp)
        !          1183:                                        printk(KERN_ERR "%s: Internal fault: The skbuff addresses "
        !          1184:                                                   "do not match in netdev_rx: %p vs. %p / %p.\n",
        !          1185:                                                   dev->name, bus_to_virt(desc->buffer1),
        !          1186:                                                   skb->head, temp);
        !          1187: #endif
        !          1188:                        }
        !          1189:                        skb->protocol = eth_type_trans(skb, dev);
        !          1190:                        netif_rx(skb);
        !          1191:                        dev->last_rx = jiffies;
        !          1192:                        np->stats.rx_packets++;
        !          1193: #if LINUX_VERSION_CODE > 0x20127
        !          1194:                        np->stats.rx_bytes += pkt_len;
        !          1195: #endif
        !          1196:                }
        !          1197:                entry = (++np->cur_rx) % np->rx_ring_size;
        !          1198:                np->rx_head_desc = &np->rx_ring[entry];
        !          1199:        }
        !          1200: 
        !          1201:        /* Refill the Rx ring buffers. */
        !          1202:        for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
        !          1203:                struct sk_buff *skb;
        !          1204:                entry = np->dirty_rx % np->rx_ring_size;
        !          1205:                if (np->rx_skbuff[entry] == NULL) {
        !          1206:                        skb = dev_alloc_skb(np->rx_buf_sz);
        !          1207:                        np->rx_skbuff[entry] = skb;
        !          1208:                        if (skb == NULL)
        !          1209:                                break;                          /* Better luck next round. */
        !          1210:                        skb->dev = dev;                 /* Mark as being used by this device. */
        !          1211:                        np->rx_ring[entry].buffer1 = virt_to_bus(skb->tail);
        !          1212:                }
        !          1213:                np->rx_ring[entry].status = DescOwn;
        !          1214:        }
        !          1215: 
        !          1216:        return 0;
        !          1217: }
        !          1218: 
        !          1219: static void netdev_error(struct net_device *dev, int intr_status)
        !          1220: {
        !          1221:        long ioaddr = dev->base_addr;
        !          1222:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1223: 
        !          1224:        if (np->msg_level & NETIF_MSG_MISC)
        !          1225:                printk(KERN_DEBUG "%s: Abnormal event, %8.8x.\n",
        !          1226:                           dev->name, intr_status);
        !          1227:        if (intr_status == 0xffffffff)
        !          1228:                return;
        !          1229:        if (intr_status & TxFIFOUnderflow) {
        !          1230:                np->csr6 += 0x4000;     /* Bump up the Tx threshold */
        !          1231:                if (np->msg_level & NETIF_MSG_TX_ERR)
        !          1232:                        printk(KERN_DEBUG "%s: Tx underflow, increasing threshold to "
        !          1233:                                   "%8.8x.\n", dev->name, np->csr6);
        !          1234:                writel(np->csr6, ioaddr + NetworkConfig);
        !          1235:        }
        !          1236:        if (intr_status & IntrRxDied) {         /* Missed a Rx frame. */
        !          1237:                np->stats.rx_errors++;
        !          1238:        }
        !          1239:        if (intr_status & TimerInt) {
        !          1240:                /* Re-enable other interrupts. */
        !          1241:                writel(0x1A0F5, ioaddr + IntrEnable);
        !          1242:        }
        !          1243:        np->stats.rx_missed_errors += readl(ioaddr + RxMissed) & 0xffff;
        !          1244:        writel(0, ioaddr + RxStartDemand);
        !          1245: }
        !          1246: 
        !          1247: static struct net_device_stats *get_stats(struct net_device *dev)
        !          1248: {
        !          1249:        long ioaddr = dev->base_addr;
        !          1250:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1251: 
        !          1252:        /* The chip only need report frame silently dropped. */
        !          1253:        if (netif_running(dev))
        !          1254:                np->stats.rx_missed_errors += readl(ioaddr + RxMissed) & 0xffff;
        !          1255: 
        !          1256:        return &np->stats;
        !          1257: }
        !          1258: 
        !          1259: static unsigned const ethernet_polynomial = 0x04c11db7U;
        !          1260: static inline u32 ether_crc(int length, unsigned char *data)
        !          1261: {
        !          1262:     int crc = -1;
        !          1263: 
        !          1264:     while(--length >= 0) {
        !          1265:                unsigned char current_octet = *data++;
        !          1266:                int bit;
        !          1267:                for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
        !          1268:                        crc = (crc << 1) ^
        !          1269:                                ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
        !          1270:                }
        !          1271:     }
        !          1272:     return crc;
        !          1273: }
        !          1274: 
        !          1275: static void set_rx_mode(struct net_device *dev)
        !          1276: {
        !          1277:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1278:        long ioaddr = dev->base_addr;
        !          1279:        u32 mc_filter[2];                       /* Multicast hash filter */
        !          1280:        u32 rx_mode;
        !          1281: 
        !          1282:        if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
        !          1283:                /* Unconditionally log net taps. */
        !          1284:                printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
        !          1285:                memset(mc_filter, ~0, sizeof(mc_filter));
        !          1286:                rx_mode = AcceptBroadcast | AcceptMulticast | AcceptAllPhys;
        !          1287:        } else if ((dev->mc_count > np->multicast_filter_limit)
        !          1288:                           ||  (dev->flags & IFF_ALLMULTI)) {
        !          1289:                /* Too many to match, or accept all multicasts. */
        !          1290:                memset(mc_filter, 0xff, sizeof(mc_filter));
        !          1291:                rx_mode = AcceptBroadcast | AcceptMulticast;
        !          1292:        } else {
        !          1293:                struct dev_mc_list *mclist;
        !          1294:                int i;
        !          1295:                memset(mc_filter, 0, sizeof(mc_filter));
        !          1296:                for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
        !          1297:                         i++, mclist = mclist->next) {
        !          1298:                        set_bit((ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26) ^ 0x3F,
        !          1299:                                        mc_filter);
        !          1300:                }
        !          1301:                rx_mode = AcceptBroadcast | AcceptMulticast;
        !          1302:        }
        !          1303:        writel(mc_filter[0], ioaddr + MulticastFilter0);
        !          1304:        writel(mc_filter[1], ioaddr + MulticastFilter1);
        !          1305:        np->csr6 &= ~0x00F8;
        !          1306:        np->csr6 |= rx_mode;
        !          1307:        writel(np->csr6, ioaddr + NetworkConfig);
        !          1308: }
        !          1309: 
        !          1310: static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
        !          1311: {
        !          1312:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1313:        u16 *data = (u16 *)&rq->ifr_data;
        !          1314:        u32 *data32 = (void *)&rq->ifr_data;
        !          1315: 
        !          1316:        switch(cmd) {
        !          1317:        case 0x8947: case 0x89F0:
        !          1318:                /* SIOCGMIIPHY: Get the address of the PHY in use. */
        !          1319:                data[0] = np->phys[0] & 0x1f;
        !          1320:                /* Fall Through */
        !          1321:        case 0x8948: case 0x89F1:
        !          1322:                /* SIOCGMIIREG: Read the specified MII register. */
        !          1323:                data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
        !          1324:                return 0;
        !          1325:        case 0x8949: case 0x89F2:
        !          1326:                /* SIOCSMIIREG: Write the specified MII register */
        !          1327:                if (!capable(CAP_NET_ADMIN))
        !          1328:                        return -EPERM;
        !          1329:                if (data[0] == np->phys[0]) {
        !          1330:                        u16 value = data[2];
        !          1331:                        switch (data[1]) {
        !          1332:                        case 0:
        !          1333:                                /* Check for autonegotiation on or reset. */
        !          1334:                                np->medialock = (value & 0x9000) ? 0 : 1;
        !          1335:                                if (np->medialock)
        !          1336:                                        np->full_duplex = (value & 0x0100) ? 1 : 0;
        !          1337:                                break;
        !          1338:                        case 4: np->advertising = value; break;
        !          1339:                        }
        !          1340:                        /* Perhaps check_duplex(dev), depending on chip semantics. */
        !          1341:                }
        !          1342:                mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
        !          1343:                return 0;
        !          1344:        case SIOCGPARAMS:
        !          1345:                data32[0] = np->msg_level;
        !          1346:                data32[1] = np->multicast_filter_limit;
        !          1347:                data32[2] = np->max_interrupt_work;
        !          1348:                data32[3] = np->rx_copybreak;
        !          1349:                return 0;
        !          1350:        case SIOCSPARAMS:
        !          1351:                if (!capable(CAP_NET_ADMIN))
        !          1352:                        return -EPERM;
        !          1353:                np->msg_level = data32[0];
        !          1354:                np->multicast_filter_limit = data32[1];
        !          1355:                np->max_interrupt_work = data32[2];
        !          1356:                np->rx_copybreak = data32[3];
        !          1357:                return 0;
        !          1358:        default:
        !          1359:                return -EOPNOTSUPP;
        !          1360:        }
        !          1361: }
        !          1362: 
        !          1363: 
        !          1364: static void empty_rings(struct net_device *dev)
        !          1365: {
        !          1366:        struct netdev_private *np = (void *)dev->priv;
        !          1367:        int i;
        !          1368: 
        !          1369:        /* Free all the skbuffs in the Rx queue. */
        !          1370:        for (i = 0; i < np->rx_ring_size; i++) {
        !          1371:                np->rx_ring[i].status = 0;
        !          1372:                if (np->rx_skbuff[i]) {
        !          1373: #if LINUX_VERSION_CODE < 0x20100
        !          1374:                        np->rx_skbuff[i]->free = 1;
        !          1375: #endif
        !          1376:                        dev_free_skb(np->rx_skbuff[i]);
        !          1377:                }
        !          1378:                np->rx_skbuff[i] = 0;
        !          1379:        }
        !          1380:        for (i = 0; i < np->tx_ring_size; i++) {
        !          1381:                if (np->tx_skbuff[i])
        !          1382:                        dev_free_skb(np->tx_skbuff[i]);
        !          1383:                np->tx_skbuff[i] = 0;
        !          1384:        }
        !          1385: }
        !          1386: 
        !          1387: static int netdev_close(struct net_device *dev)
        !          1388: {
        !          1389:        long ioaddr = dev->base_addr;
        !          1390:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1391: 
        !          1392:        netif_stop_tx_queue(dev);
        !          1393: 
        !          1394:        if (np->msg_level & NETIF_MSG_IFDOWN) {
        !          1395:                printk(KERN_DEBUG "%s: Shutting down ethercard, status was %8.8x "
        !          1396:                           "Config %8.8x.\n", dev->name, (int)readl(ioaddr + IntrStatus),
        !          1397:                           (int)readl(ioaddr + NetworkConfig));
        !          1398:                printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d,  Rx %d / %d.\n",
        !          1399:                           dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx);
        !          1400:        }
        !          1401: 
        !          1402:        /* Disable interrupts by clearing the interrupt mask. */
        !          1403:        writel(0x0000, ioaddr + IntrEnable);
        !          1404: 
        !          1405:        /* Stop the chip's Tx and Rx processes. */
        !          1406:        writel(np->csr6 &= ~0x20FA, ioaddr + NetworkConfig);
        !          1407: 
        !          1408:        del_timer(&np->timer);
        !          1409:        if (readl(ioaddr + NetworkConfig) != 0xffffffff)
        !          1410:                np->stats.rx_missed_errors += readl(ioaddr + RxMissed) & 0xffff;
        !          1411: 
        !          1412: #ifdef __i386__
        !          1413:        if (np->msg_level & NETIF_MSG_IFDOWN) {
        !          1414:                int i;
        !          1415:                printk("\n"KERN_DEBUG"  Tx ring at %8.8x:\n",
        !          1416:                           (int)virt_to_bus(np->tx_ring));
        !          1417:                for (i = 0; i < np->tx_ring_size; i++)
        !          1418:                        printk(KERN_DEBUG " #%d desc. %4.4x %8.8x %8.8x.\n",
        !          1419:                                   i, np->tx_ring[i].length,
        !          1420:                                   np->tx_ring[i].status, np->tx_ring[i].buffer1);
        !          1421:                printk(KERN_DEBUG "\n" KERN_DEBUG "  Rx ring %8.8x:\n",
        !          1422:                           (int)virt_to_bus(np->rx_ring));
        !          1423:                for (i = 0; i < np->rx_ring_size; i++) {
        !          1424:                        printk(KERN_DEBUG " #%d desc. %4.4x %4.4x %8.8x\n",
        !          1425:                                   i, np->rx_ring[i].length,
        !          1426:                                   np->rx_ring[i].status, np->rx_ring[i].buffer1);
        !          1427:                }
        !          1428:        }
        !          1429: #endif /* __i386__ debugging only */
        !          1430: 
        !          1431:        free_irq(dev->irq, dev);
        !          1432:        empty_rings(dev);
        !          1433: 
        !          1434:        MOD_DEC_USE_COUNT;
        !          1435: 
        !          1436:        return 0;
        !          1437: }
        !          1438: 
        !          1439: static int winbond_pwr_event(void *dev_instance, int event)
        !          1440: {
        !          1441:        struct net_device *dev = dev_instance;
        !          1442:        struct netdev_private *np = (struct netdev_private *)dev->priv;
        !          1443:        long ioaddr = dev->base_addr;
        !          1444: 
        !          1445:        if (np->msg_level & NETIF_MSG_LINK)
        !          1446:                printk(KERN_DEBUG "%s: Handling power event %d.\n", dev->name, event);
        !          1447:        switch(event) {
        !          1448:        case DRV_ATTACH:
        !          1449:                MOD_INC_USE_COUNT;
        !          1450:                break;
        !          1451:        case DRV_SUSPEND: {
        !          1452:                int csr6 = readl(ioaddr + NetworkConfig);
        !          1453:                /* Disable interrupts, stop the chip, gather stats. */
        !          1454:                if (csr6 != 0xffffffff) {
        !          1455:                        int csr8 = readl(ioaddr + RxMissed);
        !          1456:                        writel(0x00000000, ioaddr + IntrEnable);
        !          1457:                        writel(csr6 & ~TxOn & ~RxOn, ioaddr + NetworkConfig);
        !          1458:                        np->stats.rx_missed_errors += (unsigned short)csr8;
        !          1459:                }
        !          1460:                empty_rings(dev);
        !          1461:                break;
        !          1462:        }
        !          1463:        case DRV_RESUME:
        !          1464:                writel(np->csr0, ioaddr + PCIBusCfg);
        !          1465:                init_ring(dev);
        !          1466:                writel(virt_to_bus(np->rx_ring), ioaddr + RxRingPtr);
        !          1467:                writel(virt_to_bus(np->tx_ring), ioaddr + TxRingPtr);
        !          1468:                writel(0x1A0F5, ioaddr + IntrStatus);
        !          1469:                writel(0x1A0F5, ioaddr + IntrEnable);
        !          1470:                writel(np->csr6 | TxOn | RxOn, ioaddr + NetworkConfig);
        !          1471:                writel(0, ioaddr + RxStartDemand);              /* Rx poll demand */
        !          1472:                set_rx_mode(dev);
        !          1473:                break;
        !          1474:        case DRV_DETACH: {
        !          1475:                struct net_device **devp, **next;
        !          1476:                if (dev->flags & IFF_UP) {
        !          1477:                        printk(KERN_ERR "%s: Winbond-840 NIC removed while still "
        !          1478:                                   "active.\n", dev->name);
        !          1479:                        dev_close(dev);
        !          1480:                        dev->flags &= ~(IFF_UP|IFF_RUNNING);
        !          1481:                }
        !          1482:                unregister_netdev(dev);
        !          1483:                release_region(dev->base_addr, pci_id_tbl[np->chip_id].io_size);
        !          1484: #ifndef USE_IO_OPS
        !          1485:                iounmap((char *)dev->base_addr);
        !          1486: #endif
        !          1487:                for (devp = &root_net_dev; *devp; devp = next) {
        !          1488:                        next = &((struct netdev_private *)(*devp)->priv)->next_module;
        !          1489:                        if (*devp == dev) {
        !          1490:                                *devp = *next;
        !          1491:                                break;
        !          1492:                        }
        !          1493:                }
        !          1494:                if (np->priv_addr)
        !          1495:                        kfree(np->priv_addr);
        !          1496:                kfree(dev);
        !          1497:                MOD_DEC_USE_COUNT;
        !          1498:                break;
        !          1499:        }
        !          1500:        default:
        !          1501:                break;
        !          1502:        }
        !          1503: 
        !          1504:        return 0;
        !          1505: }
        !          1506: 
        !          1507: 
        !          1508: #ifdef MODULE
        !          1509: int init_module(void)
        !          1510: {
        !          1511:        if (debug >= NETIF_MSG_DRV)     /* Emit version even if no cards detected. */
        !          1512:                printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
        !          1513:        return pci_drv_register(&winbond840_drv_id, NULL);
        !          1514: }
        !          1515: 
        !          1516: void cleanup_module(void)
        !          1517: {
        !          1518:        struct net_device *next_dev;
        !          1519: 
        !          1520:        pci_drv_unregister(&winbond840_drv_id);
        !          1521: 
        !          1522:        /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
        !          1523:        while (root_net_dev) {
        !          1524:                struct netdev_private *np = (void *)(root_net_dev->priv);
        !          1525:                unregister_netdev(root_net_dev);
        !          1526: #ifdef USE_IO_OPS
        !          1527:                release_region(root_net_dev->base_addr,
        !          1528:                                           pci_id_tbl[np->chip_id].io_size);
        !          1529: #else
        !          1530:                iounmap((char *)(root_net_dev->base_addr));
        !          1531: #endif
        !          1532:                next_dev = np->next_module;
        !          1533:                if (np->priv_addr)
        !          1534:                        kfree(np->priv_addr);
        !          1535:                kfree(root_net_dev);
        !          1536:                root_net_dev = next_dev;
        !          1537:        }
        !          1538: }
        !          1539: #else
        !          1540: int winbond840_probe(struct net_device *dev)
        !          1541: {
        !          1542:        if (pci_drv_register(&winbond840_drv_id, dev) < 0)
        !          1543:                return -ENODEV;
        !          1544:        printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
        !          1545:        return 0;
        !          1546: }
        !          1547: #endif  /* MODULE */
        !          1548: 
        !          1549: 
        !          1550: /*
        !          1551:  * Local variables:
        !          1552:  *  compile-command: "make KERNVER=`uname -r` winbond-840.o"
        !          1553:  *  compile-cmd: "gcc -DMODULE -Wall -Wstrict-prototypes -O6 -c winbond-840.c"
        !          1554:  *  c-indent-level: 4
        !          1555:  *  c-basic-offset: 4
        !          1556:  *  tab-width: 4
        !          1557:  * End:
        !          1558:  */

unix.superglobalmegacorp.com

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