linux-bk/drivers/net/sis900.c
<<
>>
Prefs
   1/* sis900.c: A SiS 900/7016 PCI Fast Ethernet driver for Linux.
   2   Copyright 1999 Silicon Integrated System Corporation 
   3   Revision:    1.08.06 Sep. 24 2002
   4   
   5   Modified from the driver which is originally written by Donald Becker.
   6   
   7   This software may be used and distributed according to the terms
   8   of the GNU General Public License (GPL), incorporated herein by reference.
   9   Drivers based on this skeleton fall under the GPL and must retain
  10   the authorship (implicit copyright) notice.
  11   
  12   References:
  13   SiS 7016 Fast Ethernet PCI Bus 10/100 Mbps LAN Controller with OnNow Support,
  14   preliminary Rev. 1.0 Jan. 14, 1998
  15   SiS 900 Fast Ethernet PCI Bus 10/100 Mbps LAN Single Chip with OnNow Support,
  16   preliminary Rev. 1.0 Nov. 10, 1998
  17   SiS 7014 Single Chip 100BASE-TX/10BASE-T Physical Layer Solution,
  18   preliminary Rev. 1.0 Jan. 18, 1998
  19   http://www.sis.com.tw/support/databook.htm
  20
  21   Rev 1.08.06 Sep. 24 2002 Mufasa Yang bug fix for Tx timeout & add SiS963 support
  22   Rev 1.08.05 Jun. 6 2002 Mufasa Yang bug fix for read_eeprom & Tx descriptor over-boundary 
  23   Rev 1.08.04 Apr. 25 2002 Mufasa Yang <mufasa@sis.com.tw> added SiS962 support
  24   Rev 1.08.03 Feb. 1 2002 Matt Domsch <Matt_Domsch@dell.com> update to use library crc32 function
  25   Rev 1.08.02 Nov. 30 2001 Hui-Fen Hsu workaround for EDB & bug fix for dhcp problem
  26   Rev 1.08.01 Aug. 25 2001 Hui-Fen Hsu update for 630ET & workaround for ICS1893 PHY
  27   Rev 1.08.00 Jun. 11 2001 Hui-Fen Hsu workaround for RTL8201 PHY and some bug fix
  28   Rev 1.07.11 Apr.  2 2001 Hui-Fen Hsu updates PCI drivers to use the new pci_set_dma_mask for kernel 2.4.3
  29   Rev 1.07.10 Mar.  1 2001 Hui-Fen Hsu <hfhsu@sis.com.tw> some bug fix & 635M/B support 
  30   Rev 1.07.09 Feb.  9 2001 Dave Jones <davej@suse.de> PCI enable cleanup
  31   Rev 1.07.08 Jan.  8 2001 Lei-Chun Chang added RTL8201 PHY support
  32   Rev 1.07.07 Nov. 29 2000 Lei-Chun Chang added kernel-doc extractable documentation and 630 workaround fix
  33   Rev 1.07.06 Nov.  7 2000 Jeff Garzik <jgarzik@pobox.com> some bug fix and cleaning
  34   Rev 1.07.05 Nov.  6 2000 metapirat<metapirat@gmx.de> contribute media type select by ifconfig
  35   Rev 1.07.04 Sep.  6 2000 Lei-Chun Chang added ICS1893 PHY support
  36   Rev 1.07.03 Aug. 24 2000 Lei-Chun Chang (lcchang@sis.com.tw) modified 630E eqaulizer workaround rule
  37   Rev 1.07.01 Aug. 08 2000 Ollie Lho minor update for SiS 630E and SiS 630E A1
  38   Rev 1.07    Mar. 07 2000 Ollie Lho bug fix in Rx buffer ring
  39   Rev 1.06.04 Feb. 11 2000 Jeff Garzik <jgarzik@pobox.com> softnet and init for kernel 2.4
  40   Rev 1.06.03 Dec. 23 1999 Ollie Lho Third release
  41   Rev 1.06.02 Nov. 23 1999 Ollie Lho bug in mac probing fixed
  42   Rev 1.06.01 Nov. 16 1999 Ollie Lho CRC calculation provide by Joseph Zbiciak (im14u2c@primenet.com)
  43   Rev 1.06 Nov. 4 1999 Ollie Lho (ollie@sis.com.tw) Second release
  44   Rev 1.05.05 Oct. 29 1999 Ollie Lho (ollie@sis.com.tw) Single buffer Tx/Rx
  45   Chin-Shan Li (lcs@sis.com.tw) Added AMD Am79c901 HomePNA PHY support
  46   Rev 1.05 Aug. 7 1999 Jim Huang (cmhuang@sis.com.tw) Initial release
  47*/
  48
  49#include <linux/module.h>
  50#include <linux/kernel.h>
  51#include <linux/string.h>
  52#include <linux/timer.h>
  53#include <linux/errno.h>
  54#include <linux/ioport.h>
  55#include <linux/slab.h>
  56#include <linux/interrupt.h>
  57#include <linux/pci.h>
  58#include <linux/netdevice.h>
  59#include <linux/init.h>
  60#include <linux/mii.h>
  61#include <linux/etherdevice.h>
  62#include <linux/skbuff.h>
  63#include <linux/delay.h>
  64#include <linux/ethtool.h>
  65#include <linux/crc32.h>
  66
  67#include <asm/processor.h>      /* Processor type for cache alignment. */
  68#include <asm/bitops.h>
  69#include <asm/io.h>
  70#include <asm/uaccess.h>        /* User space memory access functions */
  71
  72#include "sis900.h"
  73
  74#define SIS900_MODULE_NAME "sis900"
  75#define SIS900_DRV_VERSION "v1.08.06 9/24/2002"
  76
  77static char version[] __devinitdata =
  78KERN_INFO "sis900.c: " SIS900_DRV_VERSION "\n";
  79
  80static int max_interrupt_work = 40;
  81static int multicast_filter_limit = 128;
  82
  83#define sis900_debug debug
  84static int sis900_debug;
  85
  86/* Time in jiffies before concluding the transmitter is hung. */
  87#define TX_TIMEOUT  (4*HZ)
  88/* SiS 900 is capable of 32 bits BM DMA */
  89#define SIS900_DMA_MASK 0xffffffff
  90
  91enum {
  92        SIS_900 = 0,
  93        SIS_7016
  94};
  95static char * card_names[] = {
  96        "SiS 900 PCI Fast Ethernet",
  97        "SiS 7016 PCI Fast Ethernet"
  98};
  99static struct pci_device_id sis900_pci_tbl [] = {
 100        {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_900,
 101         PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_900},
 102        {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7016,
 103         PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7016},
 104        {0,}
 105};
 106MODULE_DEVICE_TABLE (pci, sis900_pci_tbl);
 107
 108static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex);
 109
 110static struct mii_chip_info {
 111        const char * name;
 112        u16 phy_id0;
 113        u16 phy_id1;
 114        u8  phy_types;
 115#define HOME    0x0001
 116#define LAN     0x0002
 117#define MIX     0x0003
 118} mii_chip_table[] = {
 119        { "SiS 900 Internal MII PHY",           0x001d, 0x8000, LAN },
 120        { "SiS 7014 Physical Layer Solution",   0x0016, 0xf830, LAN },
 121        { "AMD 79C901 10BASE-T PHY",            0x0000, 0x6B70, LAN },
 122        { "AMD 79C901 HomePNA PHY",             0x0000, 0x6B90, HOME},
 123        { "ICS LAN PHY",                        0x0015, 0xF440, LAN },
 124        { "NS 83851 PHY",                       0x2000, 0x5C20, MIX },
 125        { "Realtek RTL8201 PHY",                0x0000, 0x8200, LAN },
 126        { "VIA 6103 PHY",                       0x0101, 0x8f20, LAN },
 127        {0,},
 128};
 129
 130struct mii_phy {
 131        struct mii_phy * next;
 132        int phy_addr;
 133        u16 phy_id0;
 134        u16 phy_id1;
 135        u16 status;
 136        u8  phy_types;
 137};
 138
 139typedef struct _BufferDesc {
 140        u32     link;
 141        u32     cmdsts;
 142        u32     bufptr;
 143} BufferDesc;
 144
 145struct sis900_private {
 146        struct net_device_stats stats;
 147        struct pci_dev * pci_dev;
 148
 149        spinlock_t lock;
 150
 151        struct mii_phy * mii;
 152        struct mii_phy * first_mii; /* record the first mii structure */
 153        unsigned int cur_phy;
 154
 155        struct timer_list timer; /* Link status detection timer. */
 156        u8     autong_complete; /* 1: auto-negotiate complete  */
 157
 158        unsigned int cur_rx, dirty_rx; /* producer/comsumer pointers for Tx/Rx ring */
 159        unsigned int cur_tx, dirty_tx;
 160
 161        /* The saved address of a sent/receive-in-place packet buffer */
 162        struct sk_buff *tx_skbuff[NUM_TX_DESC];
 163        struct sk_buff *rx_skbuff[NUM_RX_DESC];
 164        BufferDesc *tx_ring;
 165        BufferDesc *rx_ring;
 166
 167        dma_addr_t tx_ring_dma;
 168        dma_addr_t rx_ring_dma;
 169
 170        unsigned int tx_full;                   /* The Tx queue is full.    */
 171        u8 host_bridge_rev;
 172};
 173
 174MODULE_AUTHOR("Jim Huang <cmhuang@sis.com.tw>, Ollie Lho <ollie@sis.com.tw>");
 175MODULE_DESCRIPTION("SiS 900 PCI Fast Ethernet driver");
 176MODULE_LICENSE("GPL");
 177
 178MODULE_PARM(multicast_filter_limit, "i");
 179MODULE_PARM(max_interrupt_work, "i");
 180MODULE_PARM(debug, "i");
 181MODULE_PARM_DESC(multicast_filter_limit, "SiS 900/7016 maximum number of filtered multicast addresses");
 182MODULE_PARM_DESC(max_interrupt_work, "SiS 900/7016 maximum events handled per interrupt");
 183MODULE_PARM_DESC(debug, "SiS 900/7016 debug level (2-4)");
 184
 185static int sis900_open(struct net_device *net_dev);
 186static int sis900_mii_probe (struct net_device * net_dev);
 187static void sis900_init_rxfilter (struct net_device * net_dev);
 188static u16 read_eeprom(long ioaddr, int location);
 189static u16 mdio_read(struct net_device *net_dev, int phy_id, int location);
 190static void mdio_write(struct net_device *net_dev, int phy_id, int location, int val);
 191static void sis900_timer(unsigned long data);
 192static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy);
 193static void sis900_tx_timeout(struct net_device *net_dev);
 194static void sis900_init_tx_ring(struct net_device *net_dev);
 195static void sis900_init_rx_ring(struct net_device *net_dev);
 196static int sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev);
 197static int sis900_rx(struct net_device *net_dev);
 198static void sis900_finish_xmit (struct net_device *net_dev);
 199static irqreturn_t sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
 200static int sis900_close(struct net_device *net_dev);
 201static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd);
 202static struct net_device_stats *sis900_get_stats(struct net_device *net_dev);
 203static u16 sis900_mcast_bitnr(u8 *addr, u8 revision);
 204static void set_rx_mode(struct net_device *net_dev);
 205static void sis900_reset(struct net_device *net_dev);
 206static void sis630_set_eq(struct net_device *net_dev, u8 revision);
 207static int sis900_set_config(struct net_device *dev, struct ifmap *map);
 208static u16 sis900_default_phy(struct net_device * net_dev);
 209static void sis900_set_capability( struct net_device *net_dev ,struct mii_phy *phy);
 210static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr);
 211static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr);
 212static void sis900_set_mode (long ioaddr, int speed, int duplex);
 213static struct ethtool_ops sis900_ethtool_ops;
 214
 215/**
 216 *      sis900_get_mac_addr - Get MAC address for stand alone SiS900 model
 217 *      @pci_dev: the sis900 pci device
 218 *      @net_dev: the net device to get address for 
 219 *
 220 *      Older SiS900 and friends, use EEPROM to store MAC address.
 221 *      MAC address is read from read_eeprom() into @net_dev->dev_addr.
 222 */
 223
 224static int __devinit sis900_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
 225{
 226        long ioaddr = pci_resource_start(pci_dev, 0);
 227        u16 signature;
 228        int i;
 229
 230        /* check to see if we have sane EEPROM */
 231        signature = (u16) read_eeprom(ioaddr, EEPROMSignature);    
 232        if (signature == 0xffff || signature == 0x0000) {
 233                printk (KERN_INFO "%s: Error EERPOM read %x\n", 
 234                        net_dev->name, signature);
 235                return 0;
 236        }
 237
 238        /* get MAC address from EEPROM */
 239        for (i = 0; i < 3; i++)
 240                ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
 241
 242        return 1;
 243}
 244
 245/**
 246 *      sis630e_get_mac_addr - Get MAC address for SiS630E model
 247 *      @pci_dev: the sis900 pci device
 248 *      @net_dev: the net device to get address for 
 249 *
 250 *      SiS630E model, use APC CMOS RAM to store MAC address.
 251 *      APC CMOS RAM is accessed through ISA bridge.
 252 *      MAC address is read into @net_dev->dev_addr.
 253 */
 254
 255static int __devinit sis630e_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
 256{
 257        struct pci_dev *isa_bridge = NULL;
 258        u8 reg;
 259        int i;
 260
 261        if ((isa_bridge = pci_find_device(0x1039, 0x0008, isa_bridge)) == NULL) {
 262                printk("%s: Can not find ISA bridge\n", net_dev->name);
 263                return 0;
 264        }
 265        pci_read_config_byte(isa_bridge, 0x48, &reg);
 266        pci_write_config_byte(isa_bridge, 0x48, reg | 0x40);
 267
 268        for (i = 0; i < 6; i++) {
 269                outb(0x09 + i, 0x70);
 270                ((u8 *)(net_dev->dev_addr))[i] = inb(0x71); 
 271        }
 272        pci_write_config_byte(isa_bridge, 0x48, reg & ~0x40);
 273
 274        return 1;
 275}
 276
 277
 278/**
 279 *      sis635_get_mac_addr - Get MAC address for SIS635 model
 280 *      @pci_dev: the sis900 pci device
 281 *      @net_dev: the net device to get address for 
 282 *
 283 *      SiS635 model, set MAC Reload Bit to load Mac address from APC
 284 *      to rfdr. rfdr is accessed through rfcr. MAC address is read into 
 285 *      @net_dev->dev_addr.
 286 */
 287
 288static int __devinit sis635_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
 289{
 290        long ioaddr = net_dev->base_addr;
 291        u32 rfcrSave;
 292        u32 i;
 293
 294        rfcrSave = inl(rfcr + ioaddr);
 295
 296        outl(rfcrSave | RELOAD, ioaddr + cr);
 297        outl(0, ioaddr + cr);
 298
 299        /* disable packet filtering before setting filter */
 300        outl(rfcrSave & ~RFEN, rfcr + ioaddr);
 301
 302        /* load MAC addr to filter data register */
 303        for (i = 0 ; i < 3 ; i++) {
 304                outl((i << RFADDR_shift), ioaddr + rfcr);
 305                *( ((u16 *)net_dev->dev_addr) + i) = inw(ioaddr + rfdr);
 306        }
 307
 308        /* enable packet filitering */
 309        outl(rfcrSave | RFEN, rfcr + ioaddr);
 310
 311        return 1;
 312}
 313
 314/**
 315 *      sis96x_get_mac_addr - Get MAC address for SiS962 or SiS963 model
 316 *      @pci_dev: the sis900 pci device
 317 *      @net_dev: the net device to get address for 
 318 *
 319 *      SiS962 or SiS963 model, use EEPROM to store MAC address. And EEPROM 
 320 *      is shared by
 321 *      LAN and 1394. When access EEPROM, send EEREQ signal to hardware first 
 322 *      and wait for EEGNT. If EEGNT is ON, EEPROM is permitted to be access 
 323 *      by LAN, otherwise is not. After MAC address is read from EEPROM, send
 324 *      EEDONE signal to refuse EEPROM access by LAN. 
 325 *      The EEPROM map of SiS962 or SiS963 is different to SiS900. 
 326 *      The signature field in SiS962 or SiS963 spec is meaningless. 
 327 *      MAC address is read into @net_dev->dev_addr.
 328 */
 329
 330static int __devinit sis96x_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
 331{
 332        long ioaddr = net_dev->base_addr;
 333        long ee_addr = ioaddr + mear;
 334        u32 waittime = 0;
 335        int i;
 336        
 337        outl(EEREQ, ee_addr);
 338        while(waittime < 2000) {
 339                if(inl(ee_addr) & EEGNT) {
 340
 341                        /* get MAC address from EEPROM */
 342                        for (i = 0; i < 3; i++)
 343                                ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
 344
 345                        outl(EEDONE, ee_addr);
 346                        return 1;
 347                } else {
 348                        udelay(1);      
 349                        waittime ++;
 350                }
 351        }
 352        outl(EEDONE, ee_addr);
 353        return 0;
 354}
 355
 356/**
 357 *      sis900_probe - Probe for sis900 device
 358 *      @pci_dev: the sis900 pci device
 359 *      @pci_id: the pci device ID
 360 *
 361 *      Check and probe sis900 net device for @pci_dev.
 362 *      Get mac address according to the chip revision, 
 363 *      and assign SiS900-specific entries in the device structure.
 364 *      ie: sis900_open(), sis900_start_xmit(), sis900_close(), etc.
 365 */
 366
 367static int __devinit sis900_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
 368{
 369        struct sis900_private *sis_priv;
 370        struct net_device *net_dev;
 371        struct pci_dev *dev;
 372        dma_addr_t ring_dma;
 373        void *ring_space;
 374        long ioaddr;
 375        int i, ret;
 376        u8 revision;
 377        char *card_name = card_names[pci_id->driver_data];
 378
 379/* when built into the kernel, we only print version if device is found */
 380#ifndef MODULE
 381        static int printed_version;
 382        if (!printed_version++)
 383                printk(version);
 384#endif
 385
 386        /* setup various bits in PCI command register */
 387        ret = pci_enable_device(pci_dev);
 388        if(ret) return ret;
 389        
 390        i = pci_set_dma_mask(pci_dev, SIS900_DMA_MASK);
 391        if(i){
 392                printk(KERN_ERR "sis900.c: architecture does not support"
 393                        "32bit PCI busmaster DMA\n");
 394                return i;
 395        }
 396        
 397        pci_set_master(pci_dev);
 398        
 399        net_dev = alloc_etherdev(sizeof(struct sis900_private));
 400        if (!net_dev)
 401                return -ENOMEM;
 402        SET_MODULE_OWNER(net_dev);
 403        SET_NETDEV_DEV(net_dev, &pci_dev->dev);
 404
 405        /* We do a request_region() to register /proc/ioports info. */
 406        ioaddr = pci_resource_start(pci_dev, 0);        
 407        ret = pci_request_regions(pci_dev, "sis900");
 408        if (ret)
 409                goto err_out;
 410
 411        sis_priv = net_dev->priv;
 412        net_dev->base_addr = ioaddr;
 413        net_dev->irq = pci_dev->irq;
 414        sis_priv->pci_dev = pci_dev;
 415        spin_lock_init(&sis_priv->lock);
 416
 417        pci_set_drvdata(pci_dev, net_dev);
 418
 419        ring_space = pci_alloc_consistent(pci_dev, TX_TOTAL_SIZE, &ring_dma);
 420        if (!ring_space) {
 421                ret = -ENOMEM;
 422                goto err_out_cleardev;
 423        }
 424        sis_priv->tx_ring = (BufferDesc *)ring_space;
 425        sis_priv->tx_ring_dma = ring_dma;
 426
 427        ring_space = pci_alloc_consistent(pci_dev, RX_TOTAL_SIZE, &ring_dma);
 428        if (!ring_space) {
 429                ret = -ENOMEM;
 430                goto err_unmap_tx;
 431        }
 432        sis_priv->rx_ring = (BufferDesc *)ring_space;
 433        sis_priv->rx_ring_dma = ring_dma;
 434                
 435        /* The SiS900-specific entries in the device structure. */
 436        net_dev->open = &sis900_open;
 437        net_dev->hard_start_xmit = &sis900_start_xmit;
 438        net_dev->stop = &sis900_close;
 439        net_dev->get_stats = &sis900_get_stats;
 440        net_dev->set_config = &sis900_set_config;
 441        net_dev->set_multicast_list = &set_rx_mode;
 442        net_dev->do_ioctl = &mii_ioctl;
 443        net_dev->tx_timeout = sis900_tx_timeout;
 444        net_dev->watchdog_timeo = TX_TIMEOUT;
 445        net_dev->ethtool_ops = &sis900_ethtool_ops;
 446        
 447        ret = register_netdev(net_dev);
 448        if (ret)
 449                goto err_unmap_rx;
 450                
 451        /* Get Mac address according to the chip revision */
 452        pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
 453        ret = 0;
 454
 455        if (revision == SIS630E_900_REV)
 456                ret = sis630e_get_mac_addr(pci_dev, net_dev);
 457        else if ((revision > 0x81) && (revision <= 0x90) )
 458                ret = sis635_get_mac_addr(pci_dev, net_dev);
 459        else if (revision == SIS96x_900_REV)
 460                ret = sis96x_get_mac_addr(pci_dev, net_dev);
 461        else
 462                ret = sis900_get_mac_addr(pci_dev, net_dev);
 463
 464        if (ret == 0) {
 465                ret = -ENODEV;
 466                goto err_out_unregister;
 467        }
 468        
 469        /* 630ET : set the mii access mode as software-mode */
 470        if (revision == SIS630ET_900_REV)
 471                outl(ACCESSMODE | inl(ioaddr + cr), ioaddr + cr);
 472
 473        /* probe for mii transceiver */
 474        if (sis900_mii_probe(net_dev) == 0) {
 475                ret = -ENODEV;
 476                goto err_out_unregister;
 477        }
 478
 479        /* save our host bridge revision */
 480        dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_630, NULL);
 481        if (dev)
 482                pci_read_config_byte(dev, PCI_CLASS_REVISION, &sis_priv->host_bridge_rev);
 483
 484        /* print some information about our NIC */
 485        printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ", net_dev->name,
 486               card_name, ioaddr, net_dev->irq);
 487        for (i = 0; i < 5; i++)
 488                printk("%2.2x:", (u8)net_dev->dev_addr[i]);
 489        printk("%2.2x.\n", net_dev->dev_addr[i]);
 490
 491        return 0;
 492
 493 err_out_unregister:
 494        unregister_netdev(net_dev);
 495 err_unmap_rx:
 496        pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
 497                sis_priv->rx_ring_dma);
 498 err_unmap_tx:
 499        pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
 500                sis_priv->tx_ring_dma);
 501 err_out_cleardev:
 502        pci_set_drvdata(pci_dev, NULL);
 503        pci_release_regions(pci_dev);
 504 err_out:
 505        free_netdev(net_dev);
 506        return ret;
 507}
 508
 509/**
 510 *      sis900_mii_probe - Probe MII PHY for sis900
 511 *      @net_dev: the net device to probe for
 512 *      
 513 *      Search for total of 32 possible mii phy addresses.
 514 *      Identify and set current phy if found one,
 515 *      return error if it failed to found.
 516 */
 517
 518static int __init sis900_mii_probe (struct net_device * net_dev)
 519{
 520        struct sis900_private * sis_priv = net_dev->priv;
 521        u16 poll_bit = MII_STAT_LINK, status = 0;
 522        unsigned long timeout = jiffies + 5 * HZ;
 523        int phy_addr;
 524        u8 revision;
 525
 526        sis_priv->mii = NULL;
 527
 528        /* search for total of 32 possible mii phy addresses */
 529        for (phy_addr = 0; phy_addr < 32; phy_addr++) { 
 530                struct mii_phy * mii_phy = NULL;
 531                u16 mii_status;
 532                int i;
 533
 534                mii_phy = NULL;
 535                for(i = 0; i < 2; i++)
 536                        mii_status = mdio_read(net_dev, phy_addr, MII_STATUS);
 537
 538                if (mii_status == 0xffff || mii_status == 0x0000)
 539                        /* the mii is not accessible, try next one */
 540                        continue;
 541                
 542                if ((mii_phy = kmalloc(sizeof(struct mii_phy), GFP_KERNEL)) == NULL) {
 543                        printk(KERN_INFO "Cannot allocate mem for struct mii_phy\n");
 544                        mii_phy = sis_priv->first_mii;
 545                        while (mii_phy) {
 546                                struct mii_phy *phy;
 547                                phy = mii_phy;
 548                                mii_phy = mii_phy->next;
 549                                kfree(phy);
 550                        }
 551                        return 0;
 552                }
 553                
 554                mii_phy->phy_id0 = mdio_read(net_dev, phy_addr, MII_PHY_ID0);
 555                mii_phy->phy_id1 = mdio_read(net_dev, phy_addr, MII_PHY_ID1);           
 556                mii_phy->phy_addr = phy_addr;
 557                mii_phy->status = mii_status;
 558                mii_phy->next = sis_priv->mii;
 559                sis_priv->mii = mii_phy;
 560                sis_priv->first_mii = mii_phy;
 561
 562                for (i = 0; mii_chip_table[i].phy_id1; i++)
 563                        if ((mii_phy->phy_id0 == mii_chip_table[i].phy_id0 ) &&
 564                            ((mii_phy->phy_id1 & 0xFFF0) == mii_chip_table[i].phy_id1)){
 565                                mii_phy->phy_types = mii_chip_table[i].phy_types;
 566                                if (mii_chip_table[i].phy_types == MIX)
 567                                        mii_phy->phy_types =
 568                                                (mii_status & (MII_STAT_CAN_TX_FDX | MII_STAT_CAN_TX)) ? LAN : HOME;
 569                                printk(KERN_INFO "%s: %s transceiver found at address %d.\n",
 570                                       net_dev->name, mii_chip_table[i].name, phy_addr);
 571                                break;
 572                        }
 573                        
 574                if( !mii_chip_table[i].phy_id1 )
 575                        printk(KERN_INFO "%s: Unknown PHY transceiver found at address %d.\n",
 576                               net_dev->name, phy_addr);                        
 577        }
 578        
 579        if (sis_priv->mii == NULL) {
 580                printk(KERN_INFO "%s: No MII transceivers found!\n",
 581                       net_dev->name);
 582                return 0;
 583        }
 584
 585        /* select default PHY for mac */
 586        sis_priv->mii = NULL;
 587        sis900_default_phy( net_dev );
 588
 589        /* Reset phy if default phy is internal sis900 */
 590        if ((sis_priv->mii->phy_id0 == 0x001D) &&
 591            ((sis_priv->mii->phy_id1&0xFFF0) == 0x8000))
 592                status = sis900_reset_phy(net_dev, sis_priv->cur_phy);
 593        
 594        /* workaround for ICS1893 PHY */
 595        if ((sis_priv->mii->phy_id0 == 0x0015) &&
 596            ((sis_priv->mii->phy_id1&0xFFF0) == 0xF440))
 597                mdio_write(net_dev, sis_priv->cur_phy, 0x0018, 0xD200);
 598
 599        if(status & MII_STAT_LINK){
 600                while (poll_bit) {
 601                        yield();
 602
 603                        poll_bit ^= (mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS) & poll_bit);
 604                        if (time_after_eq(jiffies, timeout)) {
 605                                printk(KERN_WARNING "%s: reset phy and link down now\n", net_dev->name);
 606                                return -ETIME;
 607                        }
 608                }
 609        }
 610
 611        pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
 612        if (revision == SIS630E_900_REV) {
 613                /* SiS 630E has some bugs on default value of PHY registers */
 614                mdio_write(net_dev, sis_priv->cur_phy, MII_ANADV, 0x05e1);
 615                mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG1, 0x22);
 616                mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG2, 0xff00);
 617                mdio_write(net_dev, sis_priv->cur_phy, MII_MASK, 0xffc0);
 618                //mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, 0x1000);  
 619        }
 620
 621        if (sis_priv->mii->status & MII_STAT_LINK)
 622                netif_carrier_on(net_dev);
 623        else
 624                netif_carrier_off(net_dev);
 625
 626        return 1;
 627}
 628
 629/**
 630 *      sis900_default_phy - Select default PHY for sis900 mac.
 631 *      @net_dev: the net device to probe for
 632 *
 633 *      Select first detected PHY with link as default.
 634 *      If no one is link on, select PHY whose types is HOME as default.
 635 *      If HOME doesn't exist, select LAN.
 636 */
 637
 638static u16 sis900_default_phy(struct net_device * net_dev)
 639{
 640        struct sis900_private * sis_priv = net_dev->priv;
 641        struct mii_phy *phy = NULL, *phy_home = NULL, *default_phy = NULL;
 642        u16 status;
 643
 644        for( phy=sis_priv->first_mii; phy; phy=phy->next ){
 645                status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
 646                status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
 647
 648                /* Link ON & Not select deafalut PHY */
 649                 if ( (status & MII_STAT_LINK) && !(default_phy) )
 650                        default_phy = phy;
 651                 else{
 652                        status = mdio_read(net_dev, phy->phy_addr, MII_CONTROL);
 653                        mdio_write(net_dev, phy->phy_addr, MII_CONTROL,
 654                                status | MII_CNTL_AUTO | MII_CNTL_ISOLATE);
 655                        if( phy->phy_types == HOME )
 656                                phy_home = phy;
 657                 }
 658        }
 659
 660        if( (!default_phy) && phy_home )
 661                default_phy = phy_home;
 662        else if(!default_phy)
 663                default_phy = sis_priv->first_mii;
 664
 665        if( sis_priv->mii != default_phy ){
 666                sis_priv->mii = default_phy;
 667                sis_priv->cur_phy = default_phy->phy_addr;
 668                printk(KERN_INFO "%s: Using transceiver found at address %d as default\n", net_dev->name,sis_priv->cur_phy);
 669        }
 670        
 671        status = mdio_read(net_dev, sis_priv->cur_phy, MII_CONTROL);
 672        status &= (~MII_CNTL_ISOLATE);
 673
 674        mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, status);    
 675        status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
 676        status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
 677
 678        return status;  
 679}
 680
 681
 682/**
 683 *      sis900_set_capability - set the media capability of network adapter.
 684 *      @net_dev : the net device to probe for
 685 *      @phy : default PHY
 686 *
 687 *      Set the media capability of network adapter according to
 688 *      mii status register. It's necessary before auto-negotiate.
 689 */
 690 
 691static void sis900_set_capability( struct net_device *net_dev , struct mii_phy *phy )
 692{
 693        u16 cap;
 694        u16 status;
 695        
 696        status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
 697        status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
 698        
 699        cap = MII_NWAY_CSMA_CD |
 700                ((phy->status & MII_STAT_CAN_TX_FDX)? MII_NWAY_TX_FDX:0) |
 701                ((phy->status & MII_STAT_CAN_TX)    ? MII_NWAY_TX:0) |
 702                ((phy->status & MII_STAT_CAN_T_FDX) ? MII_NWAY_T_FDX:0)|
 703                ((phy->status & MII_STAT_CAN_T)     ? MII_NWAY_T:0);
 704
 705        mdio_write(net_dev, phy->phy_addr, MII_ANADV, cap);
 706}
 707
 708
 709/* Delay between EEPROM clock transitions. */
 710#define eeprom_delay()  inl(ee_addr)
 711
 712/**
 713 *      read_eeprom - Read Serial EEPROM
 714 *      @ioaddr: base i/o address
 715 *      @location: the EEPROM location to read
 716 *
 717 *      Read Serial EEPROM through EEPROM Access Register.
 718 *      Note that location is in word (16 bits) unit
 719 */
 720
 721static u16 __devinit read_eeprom(long ioaddr, int location)
 722{
 723        int i;
 724        u16 retval = 0;
 725        long ee_addr = ioaddr + mear;
 726        u32 read_cmd = location | EEread;
 727
 728        outl(0, ee_addr);
 729        eeprom_delay();
 730        outl(EECS, ee_addr);
 731        eeprom_delay();
 732
 733        /* Shift the read command (9) bits out. */
 734        for (i = 8; i >= 0; i--) {
 735                u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
 736                outl(dataval, ee_addr);
 737                eeprom_delay();
 738                outl(dataval | EECLK, ee_addr);
 739                eeprom_delay();
 740        }
 741        outl(EECS, ee_addr);
 742        eeprom_delay();
 743
 744        /* read the 16-bits data in */
 745        for (i = 16; i > 0; i--) {
 746                outl(EECS, ee_addr);
 747                eeprom_delay();
 748                outl(EECS | EECLK, ee_addr);
 749                eeprom_delay();
 750                retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
 751                eeprom_delay();
 752        }
 753
 754        /* Terminate the EEPROM access. */
 755        outl(0, ee_addr);
 756        eeprom_delay();
 757
 758        return (retval);
 759}
 760
 761/* Read and write the MII management registers using software-generated
 762   serial MDIO protocol. Note that the command bits and data bits are
 763   send out separately */
 764#define mdio_delay()    inl(mdio_addr)
 765
 766static void mdio_idle(long mdio_addr)
 767{
 768        outl(MDIO | MDDIR, mdio_addr);
 769        mdio_delay();
 770        outl(MDIO | MDDIR | MDC, mdio_addr);
 771}
 772
 773/* Syncronize the MII management interface by shifting 32 one bits out. */
 774static void mdio_reset(long mdio_addr)
 775{
 776        int i;
 777
 778        for (i = 31; i >= 0; i--) {
 779                outl(MDDIR | MDIO, mdio_addr);
 780                mdio_delay();
 781                outl(MDDIR | MDIO | MDC, mdio_addr);
 782                mdio_delay();
 783        }
 784        return;
 785}
 786
 787/**
 788 *      mdio_read - read MII PHY register
 789 *      @net_dev: the net device to read
 790 *      @phy_id: the phy address to read
 791 *      @location: the phy regiester id to read
 792 *
 793 *      Read MII registers through MDIO and MDC
 794 *      using MDIO management frame structure and protocol(defined by ISO/IEC).
 795 *      Please see SiS7014 or ICS spec
 796 */
 797
 798static u16 mdio_read(struct net_device *net_dev, int phy_id, int location)
 799{
 800        long mdio_addr = net_dev->base_addr + mear;
 801        int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
 802        u16 retval = 0;
 803        int i;
 804
 805        mdio_reset(mdio_addr);
 806        mdio_idle(mdio_addr);
 807
 808        for (i = 15; i >= 0; i--) {
 809                int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
 810                outl(dataval, mdio_addr);
 811                mdio_delay();
 812                outl(dataval | MDC, mdio_addr);
 813                mdio_delay();
 814        }
 815
 816        /* Read the 16 data bits. */
 817        for (i = 16; i > 0; i--) {
 818                outl(0, mdio_addr);
 819                mdio_delay();
 820                retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
 821                outl(MDC, mdio_addr);
 822                mdio_delay();
 823        }
 824        outl(0x00, mdio_addr);
 825
 826        return retval;
 827}
 828
 829/**
 830 *      mdio_write - write MII PHY register
 831 *      @net_dev: the net device to write
 832 *      @phy_id: the phy address to write
 833 *      @location: the phy regiester id to write
 834 *      @value: the register value to write with
 835 *
 836 *      Write MII registers with @value through MDIO and MDC
 837 *      using MDIO management frame structure and protocol(defined by ISO/IEC)
 838 *      please see SiS7014 or ICS spec
 839 */
 840
 841static void mdio_write(struct net_device *net_dev, int phy_id, int location, int value)
 842{
 843        long mdio_addr = net_dev->base_addr + mear;
 844        int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
 845        int i;
 846
 847        mdio_reset(mdio_addr);
 848        mdio_idle(mdio_addr);
 849
 850        /* Shift the command bits out. */
 851        for (i = 15; i >= 0; i--) {
 852                int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
 853                outb(dataval, mdio_addr);
 854                mdio_delay();
 855                outb(dataval | MDC, mdio_addr);
 856                mdio_delay();
 857        }
 858        mdio_delay();
 859
 860        /* Shift the value bits out. */
 861        for (i = 15; i >= 0; i--) {
 862                int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
 863                outl(dataval, mdio_addr);
 864                mdio_delay();
 865                outl(dataval | MDC, mdio_addr);
 866                mdio_delay();
 867        }
 868        mdio_delay();
 869
 870        /* Clear out extra bits. */
 871        for (i = 2; i > 0; i--) {
 872                outb(0, mdio_addr);
 873                mdio_delay();
 874                outb(MDC, mdio_addr);
 875                mdio_delay();
 876        }
 877        outl(0x00, mdio_addr);
 878
 879        return;
 880}
 881
 882
 883/**
 884 *      sis900_reset_phy - reset sis900 mii phy.
 885 *      @net_dev: the net device to write
 886 *      @phy_addr: default phy address
 887 *
 888 *      Some specific phy can't work properly without reset.
 889 *      This function will be called during initialization and
 890 *      link status change from ON to DOWN.
 891 */
 892
 893static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr)
 894{
 895        int i = 0;
 896        u16 status;
 897
 898        while (i++ < 2)
 899                status = mdio_read(net_dev, phy_addr, MII_STATUS);
 900
 901        mdio_write( net_dev, phy_addr, MII_CONTROL, MII_CNTL_RESET );
 902        
 903        return status;
 904}
 905
 906/**
 907 *      sis900_open - open sis900 device
 908 *      @net_dev: the net device to open
 909 *
 910 *      Do some initialization and start net interface.
 911 *      enable interrupts and set sis900 timer.
 912 */
 913
 914static int
 915sis900_open(struct net_device *net_dev)
 916{
 917        struct sis900_private *sis_priv = net_dev->priv;
 918        long ioaddr = net_dev->base_addr;
 919        u8 revision;
 920        int ret;
 921
 922        /* Soft reset the chip. */
 923        sis900_reset(net_dev);
 924
 925        /* Equalizer workaround Rule */
 926        pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
 927        sis630_set_eq(net_dev, revision);
 928
 929        ret = request_irq(net_dev->irq, &sis900_interrupt, SA_SHIRQ, net_dev->name, net_dev);
 930        if (ret)
 931                return ret;
 932
 933        sis900_init_rxfilter(net_dev);
 934
 935        sis900_init_tx_ring(net_dev);
 936        sis900_init_rx_ring(net_dev);
 937
 938        set_rx_mode(net_dev);
 939
 940        netif_start_queue(net_dev);
 941
 942        /* Workaround for EDB */
 943        sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
 944
 945        /* Enable all known interrupts by setting the interrupt mask. */
 946        outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
 947        outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
 948        outl(IE, ioaddr + ier);
 949
 950        sis900_check_mode(net_dev, sis_priv->mii);
 951
 952        /* Set the timer to switch to check for link beat and perhaps switch
 953           to an alternate media type. */
 954        init_timer(&sis_priv->timer);
 955        sis_priv->timer.expires = jiffies + HZ;
 956        sis_priv->timer.data = (unsigned long)net_dev;
 957        sis_priv->timer.function = &sis900_timer;
 958        add_timer(&sis_priv->timer);
 959
 960        return 0;
 961}
 962
 963/**
 964 *      sis900_init_rxfilter - Initialize the Rx filter
 965 *      @net_dev: the net device to initialize for
 966 *
 967 *      Set receive filter address to our MAC address
 968 *      and enable packet filtering.
 969 */
 970
 971static void
 972sis900_init_rxfilter (struct net_device * net_dev)
 973{
 974        long ioaddr = net_dev->base_addr;
 975        u32 rfcrSave;
 976        u32 i;
 977
 978        rfcrSave = inl(rfcr + ioaddr);
 979
 980        /* disable packet filtering before setting filter */
 981        outl(rfcrSave & ~RFEN, rfcr + ioaddr);
 982
 983        /* load MAC addr to filter data register */
 984        for (i = 0 ; i < 3 ; i++) {
 985                u32 w;
 986
 987                w = (u32) *((u16 *)(net_dev->dev_addr)+i);
 988                outl((i << RFADDR_shift), ioaddr + rfcr);
 989                outl(w, ioaddr + rfdr);
 990
 991                if (sis900_debug > 2) {
 992                        printk(KERN_INFO "%s: Receive Filter Addrss[%d]=%x\n",
 993                               net_dev->name, i, inl(ioaddr + rfdr));
 994                }
 995        }
 996
 997        /* enable packet filitering */
 998        outl(rfcrSave | RFEN, rfcr + ioaddr);
 999}
1000
1001/**
1002 *      sis900_init_tx_ring - Initialize the Tx descriptor ring
1003 *      @net_dev: the net device to initialize for
1004 *
1005 *      Initialize the Tx descriptor ring, 
1006 */
1007
1008static void
1009sis900_init_tx_ring(struct net_device *net_dev)
1010{
1011        struct sis900_private *sis_priv = net_dev->priv;
1012        long ioaddr = net_dev->base_addr;
1013        int i;
1014
1015        sis_priv->tx_full = 0;
1016        sis_priv->dirty_tx = sis_priv->cur_tx = 0;
1017
1018        for (i = 0; i < NUM_TX_DESC; i++) {
1019                sis_priv->tx_skbuff[i] = NULL;
1020
1021                sis_priv->tx_ring[i].link = sis_priv->tx_ring_dma +
1022                        ((i+1)%NUM_TX_DESC)*sizeof(BufferDesc);
1023                sis_priv->tx_ring[i].cmdsts = 0;
1024                sis_priv->tx_ring[i].bufptr = 0;
1025        }
1026
1027        /* load Transmit Descriptor Register */
1028        outl(sis_priv->tx_ring_dma, ioaddr + txdp);
1029        if (sis900_debug > 2)
1030                printk(KERN_INFO "%s: TX descriptor register loaded with: %8.8x\n",
1031                       net_dev->name, inl(ioaddr + txdp));
1032}
1033
1034/**
1035 *      sis900_init_rx_ring - Initialize the Rx descriptor ring
1036 *      @net_dev: the net device to initialize for
1037 *
1038 *      Initialize the Rx descriptor ring, 
1039 *      and pre-allocate recevie buffers (socket buffer)
1040 */
1041
1042static void 
1043sis900_init_rx_ring(struct net_device *net_dev)
1044{
1045        struct sis900_private *sis_priv = net_dev->priv;
1046        long ioaddr = net_dev->base_addr;
1047        int i;
1048
1049        sis_priv->cur_rx = 0;
1050        sis_priv->dirty_rx = 0;
1051
1052        /* init RX descriptor */
1053        for (i = 0; i < NUM_RX_DESC; i++) {
1054                sis_priv->rx_skbuff[i] = NULL;
1055
1056                sis_priv->rx_ring[i].link = sis_priv->rx_ring_dma +
1057                        ((i+1)%NUM_RX_DESC)*sizeof(BufferDesc);
1058                sis_priv->rx_ring[i].cmdsts = 0;
1059                sis_priv->rx_ring[i].bufptr = 0;
1060        }
1061
1062        /* allocate sock buffers */
1063        for (i = 0; i < NUM_RX_DESC; i++) {
1064                struct sk_buff *skb;
1065
1066                if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1067                        /* not enough memory for skbuff, this makes a "hole"
1068                           on the buffer ring, it is not clear how the
1069                           hardware will react to this kind of degenerated
1070                           buffer */
1071                        break;
1072                }
1073                skb->dev = net_dev;
1074                sis_priv->rx_skbuff[i] = skb;
1075                sis_priv->rx_ring[i].cmdsts = RX_BUF_SIZE;
1076                sis_priv->rx_ring[i].bufptr = pci_map_single(sis_priv->pci_dev,
1077                        skb->tail, RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1078        }
1079        sis_priv->dirty_rx = (unsigned int) (i - NUM_RX_DESC);
1080
1081        /* load Receive Descriptor Register */
1082        outl(sis_priv->rx_ring_dma, ioaddr + rxdp);
1083        if (sis900_debug > 2)
1084                printk(KERN_INFO "%s: RX descriptor register loaded with: %8.8x\n",
1085                       net_dev->name, inl(ioaddr + rxdp));
1086}
1087
1088/**
1089 *      sis630_set_eq - set phy equalizer value for 630 LAN
1090 *      @net_dev: the net device to set equalizer value
1091 *      @revision: 630 LAN revision number
1092 *
1093 *      630E equalizer workaround rule(Cyrus Huang 08/15)
1094 *      PHY register 14h(Test)
1095 *      Bit 14: 0 -- Automatically dectect (default)
1096 *              1 -- Manually set Equalizer filter
1097 *      Bit 13: 0 -- (Default)
1098 *              1 -- Speed up convergence of equalizer setting
1099 *      Bit 9 : 0 -- (Default)
1100 *              1 -- Disable Baseline Wander
1101 *      Bit 3~7   -- Equalizer filter setting
1102 *      Link ON: Set Bit 9, 13 to 1, Bit 14 to 0
1103 *      Then calculate equalizer value
1104 *      Then set equalizer value, and set Bit 14 to 1, Bit 9 to 0
1105 *      Link Off:Set Bit 13 to 1, Bit 14 to 0
1106 *      Calculate Equalizer value:
1107 *      When Link is ON and Bit 14 is 0, SIS900PHY will auto-dectect proper equalizer value.
1108 *      When the equalizer is stable, this value is not a fixed value. It will be within
1109 *      a small range(eg. 7~9). Then we get a minimum and a maximum value(eg. min=7, max=9)
1110 *      0 <= max <= 4  --> set equalizer to max
1111 *      5 <= max <= 14 --> set equalizer to max+1 or set equalizer to max+2 if max == min
1112 *      max >= 15      --> set equalizer to max+5 or set equalizer to max+6 if max == min
1113 */
1114
1115static void sis630_set_eq(struct net_device *net_dev, u8 revision)
1116{
1117        struct sis900_private *sis_priv = net_dev->priv;
1118        u16 reg14h, eq_value=0, max_value=0, min_value=0;
1119        int i, maxcount=10;
1120
1121        if ( !(revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
1122               revision == SIS630A_900_REV || revision ==  SIS630ET_900_REV) )
1123                return;
1124
1125        if (netif_carrier_ok(net_dev)) {
1126                reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1127                mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (0x2200 | reg14h) & 0xBFFF);
1128                for (i=0; i < maxcount; i++) {
1129                        eq_value=(0x00F8 & mdio_read(net_dev, sis_priv->cur_phy, MII_RESV)) >> 3;
1130                        if (i == 0)
1131                                max_value=min_value=eq_value;
1132                        max_value=(eq_value > max_value) ? eq_value : max_value;
1133                        min_value=(eq_value < min_value) ? eq_value : min_value;
1134                }
1135                /* 630E rule to determine the equalizer value */
1136                if (revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
1137                    revision == SIS630ET_900_REV) {
1138                        if (max_value < 5)
1139                                eq_value=max_value;
1140                        else if (max_value >= 5 && max_value < 15)
1141                                eq_value=(max_value == min_value) ? max_value+2 : max_value+1;
1142                        else if (max_value >= 15)
1143                                eq_value=(max_value == min_value) ? max_value+6 : max_value+5;
1144                }
1145                /* 630B0&B1 rule to determine the equalizer value */
1146                if (revision == SIS630A_900_REV && 
1147                    (sis_priv->host_bridge_rev == SIS630B0 || 
1148                     sis_priv->host_bridge_rev == SIS630B1)) {
1149                        if (max_value == 0)
1150                                eq_value=3;
1151                        else
1152                                eq_value=(max_value+min_value+1)/2;
1153                }
1154                /* write equalizer value and setting */
1155                reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1156                reg14h=(reg14h & 0xFF07) | ((eq_value << 3) & 0x00F8);
1157                reg14h=(reg14h | 0x6000) & 0xFDFF;
1158                mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, reg14h);
1159        }
1160        else {
1161                reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1162                if (revision == SIS630A_900_REV && 
1163                    (sis_priv->host_bridge_rev == SIS630B0 || 
1164                     sis_priv->host_bridge_rev == SIS630B1)) 
1165                        mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2200) & 0xBFFF);
1166                else
1167                        mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2000) & 0xBFFF);
1168        }
1169        return;
1170}
1171
1172/**
1173 *      sis900_timer - sis900 timer routine
1174 *      @data: pointer to sis900 net device
1175 *
1176 *      On each timer ticks we check two things, 
1177 *      link status (ON/OFF) and link mode (10/100/Full/Half)
1178 */
1179
1180static void sis900_timer(unsigned long data)
1181{
1182        struct net_device *net_dev = (struct net_device *)data;
1183        struct sis900_private *sis_priv = net_dev->priv;
1184        struct mii_phy *mii_phy = sis_priv->mii;
1185        static int next_tick = 5*HZ;
1186        u16 status;
1187        u8 revision;
1188
1189        if (!sis_priv->autong_complete){
1190                int speed, duplex = 0;
1191
1192                sis900_read_mode(net_dev, &speed, &duplex);
1193                if (duplex){
1194                        sis900_set_mode(net_dev->base_addr, speed, duplex);
1195                        pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
1196                        sis630_set_eq(net_dev, revision);
1197                        netif_start_queue(net_dev);
1198                }
1199
1200                sis_priv->timer.expires = jiffies + HZ;
1201                add_timer(&sis_priv->timer);
1202                return;
1203        }
1204
1205        status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
1206        status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
1207
1208        /* Link OFF -> ON */
1209        if (!netif_carrier_ok(net_dev)) {
1210        LookForLink:
1211                /* Search for new PHY */
1212                status = sis900_default_phy(net_dev);
1213                mii_phy = sis_priv->mii;
1214
1215                if (status & MII_STAT_LINK){
1216                        sis900_check_mode(net_dev, mii_phy);
1217                        netif_carrier_on(net_dev);
1218                }
1219        }
1220        /* Link ON -> OFF */
1221        else {
1222                if (!(status & MII_STAT_LINK)){
1223                        netif_carrier_off(net_dev);
1224                        printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
1225
1226                        /* Change mode issue */
1227                        if ((mii_phy->phy_id0 == 0x001D) && 
1228                            ((mii_phy->phy_id1 & 0xFFF0) == 0x8000))
1229                                sis900_reset_phy(net_dev,  sis_priv->cur_phy);
1230  
1231                        pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
1232                        sis630_set_eq(net_dev, revision);
1233  
1234                        goto LookForLink;
1235                }
1236        }
1237
1238        sis_priv->timer.expires = jiffies + next_tick;
1239        add_timer(&sis_priv->timer);
1240}
1241
1242/**
1243 *      sis900_check_mode - check the media mode for sis900
1244 *      @net_dev: the net device to be checked
1245 *      @mii_phy: the mii phy
1246 *
1247 *      Older driver gets the media mode from mii status output
1248 *      register. Now we set our media capability and auto-negotiate
1249 *      to get the upper bound of speed and duplex between two ends.
1250 *      If the types of mii phy is HOME, it doesn't need to auto-negotiate
1251 *      and autong_complete should be set to 1.
1252 */
1253
1254static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy)
1255{
1256        struct sis900_private *sis_priv = net_dev->priv;
1257        long ioaddr = net_dev->base_addr;
1258        int speed, duplex;
1259
1260        if( mii_phy->phy_types == LAN  ){
1261                outl( ~EXD & inl( ioaddr + cfg ), ioaddr + cfg);
1262                sis900_set_capability(net_dev , mii_phy);
1263                sis900_auto_negotiate(net_dev, sis_priv->cur_phy);
1264        }else{
1265                outl(EXD | inl( ioaddr + cfg ), ioaddr + cfg);
1266                speed = HW_SPEED_HOME;
1267                duplex = FDX_CAPABLE_HALF_SELECTED;
1268                sis900_set_mode(ioaddr, speed, duplex);
1269                sis_priv->autong_complete = 1;
1270        }
1271}
1272
1273/**
1274 *      sis900_set_mode - Set the media mode of mac register.
1275 *      @ioaddr: the address of the device
1276 *      @speed : the transmit speed to be determined
1277 *      @duplex: the duplex mode to be determined
1278 *
1279 *      Set the media mode of mac register txcfg/rxcfg according to
1280 *      speed and duplex of phy. Bit EDB_MASTER_EN indicates the EDB
1281 *      bus is used instead of PCI bus. When this bit is set 1, the
1282 *      Max DMA Burst Size for TX/RX DMA should be no larger than 16
1283 *      double words.
1284 */
1285
1286static void sis900_set_mode (long ioaddr, int speed, int duplex)
1287{
1288        u32 tx_flags = 0, rx_flags = 0;
1289
1290        if( inl(ioaddr + cfg) & EDB_MASTER_EN ){
1291                tx_flags = TxATP | (DMA_BURST_64 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
1292                rx_flags = DMA_BURST_64 << RxMXDMA_shift;
1293        }
1294        else{
1295                tx_flags = TxATP | (DMA_BURST_512 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
1296                rx_flags = DMA_BURST_512 << RxMXDMA_shift;
1297        }
1298
1299        if (speed == HW_SPEED_HOME || speed == HW_SPEED_10_MBPS ) {
1300                rx_flags |= (RxDRNT_10 << RxDRNT_shift);
1301                tx_flags |= (TxDRNT_10 << TxDRNT_shift);
1302        }
1303        else {
1304                rx_flags |= (RxDRNT_100 << RxDRNT_shift);
1305                tx_flags |= (TxDRNT_100 << TxDRNT_shift);
1306        }
1307
1308        if (duplex == FDX_CAPABLE_FULL_SELECTED) {
1309                tx_flags |= (TxCSI | TxHBI);
1310                rx_flags |= RxATX;
1311        }
1312
1313        outl (tx_flags, ioaddr + txcfg);
1314        outl (rx_flags, ioaddr + rxcfg);
1315}
1316
1317/**
1318 *      sis900_auto_negotiate - Set the Auto-Negotiation Enable/Reset bit.
1319 *      @net_dev: the net device to read mode for
1320 *      @phy_addr: mii phy address
1321 *
1322 *      If the adapter is link-on, set the auto-negotiate enable/reset bit.
1323 *      autong_complete should be set to 0 when starting auto-negotiation.
1324 *      autong_complete should be set to 1 if we didn't start auto-negotiation.
1325 *      sis900_timer will wait for link on again if autong_complete = 0.
1326 */
1327
1328static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr)
1329{
1330        struct sis900_private *sis_priv = net_dev->priv;
1331        int i = 0;
1332        u32 status;
1333        
1334        while (i++ < 2)
1335                status = mdio_read(net_dev, phy_addr, MII_STATUS);
1336
1337        if (!(status & MII_STAT_LINK)){
1338                printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
1339                sis_priv->autong_complete = 1;
1340                netif_carrier_off(net_dev);
1341                return;
1342        }
1343
1344        /* (Re)start AutoNegotiate */
1345        mdio_write(net_dev, phy_addr, MII_CONTROL,
1346                   MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
1347        sis_priv->autong_complete = 0;
1348}
1349
1350
1351/**
1352 *      sis900_read_mode - read media mode for sis900 internal phy
1353 *      @net_dev: the net device to read mode for
1354 *      @speed  : the transmit speed to be determined
1355 *      @duplex : the duplex mode to be determined
1356 *
1357 *      The capability of remote end will be put in mii register autorec
1358 *      after auto-negotiation. Use AND operation to get the upper bound
1359 *      of speed and duplex between two ends.
1360 */
1361
1362static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex)
1363{
1364        struct sis900_private *sis_priv = net_dev->priv;
1365        struct mii_phy *phy = sis_priv->mii;
1366        int phy_addr = sis_priv->cur_phy;
1367        u32 status;
1368        u16 autoadv, autorec;
1369        int i = 0;
1370
1371        while (i++ < 2)
1372                status = mdio_read(net_dev, phy_addr, MII_STATUS);
1373
1374        if (!(status & MII_STAT_LINK))
1375                return;
1376
1377        /* AutoNegotiate completed */
1378        autoadv = mdio_read(net_dev, phy_addr, MII_ANADV);
1379        autorec = mdio_read(net_dev, phy_addr, MII_ANLPAR);
1380        status = autoadv & autorec;
1381        
1382        *speed = HW_SPEED_10_MBPS;
1383        *duplex = FDX_CAPABLE_HALF_SELECTED;
1384
1385        if (status & (MII_NWAY_TX | MII_NWAY_TX_FDX))
1386                *speed = HW_SPEED_100_MBPS;
1387        if (status & ( MII_NWAY_TX_FDX | MII_NWAY_T_FDX))
1388                *duplex = FDX_CAPABLE_FULL_SELECTED;
1389        
1390        sis_priv->autong_complete = 1;
1391
1392        /* Workaround for Realtek RTL8201 PHY issue */
1393        if((phy->phy_id0 == 0x0000) && ((phy->phy_id1 & 0xFFF0) == 0x8200)){
1394                if(mdio_read(net_dev, phy_addr, MII_CONTROL) & MII_CNTL_FDX)
1395                        *duplex = FDX_CAPABLE_FULL_SELECTED;
1396                if(mdio_read(net_dev, phy_addr, 0x0019) & 0x01)
1397                        *speed = HW_SPEED_100_MBPS;
1398        }
1399
1400        printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
1401               net_dev->name,
1402               *speed == HW_SPEED_100_MBPS ?
1403               "100mbps" : "10mbps",
1404               *duplex == FDX_CAPABLE_FULL_SELECTED ?
1405               "full" : "half");
1406}
1407
1408/**
1409 *      sis900_tx_timeout - sis900 transmit timeout routine
1410 *      @net_dev: the net device to transmit
1411 *
1412 *      print transmit timeout status
1413 *      disable interrupts and do some tasks
1414 */
1415
1416static void sis900_tx_timeout(struct net_device *net_dev)
1417{
1418        struct sis900_private *sis_priv = net_dev->priv;
1419        long ioaddr = net_dev->base_addr;
1420        unsigned long flags;
1421        int i;
1422
1423        printk(KERN_INFO "%s: Transmit timeout, status %8.8x %8.8x \n",
1424               net_dev->name, inl(ioaddr + cr), inl(ioaddr + isr));
1425
1426        /* Disable interrupts by clearing the interrupt mask. */
1427        outl(0x0000, ioaddr + imr);
1428
1429        /* use spinlock to prevent interrupt handler accessing buffer ring */
1430        spin_lock_irqsave(&sis_priv->lock, flags);
1431
1432        /* discard unsent packets */
1433        sis_priv->dirty_tx = sis_priv->cur_tx = 0;
1434        for (i = 0; i < NUM_TX_DESC; i++) {
1435                struct sk_buff *skb = sis_priv->tx_skbuff[i];
1436
1437                if (skb) {
1438                        pci_unmap_single(sis_priv->pci_dev, 
1439                                sis_priv->tx_ring[i].bufptr, skb->len,
1440                                PCI_DMA_TODEVICE);
1441                        dev_kfree_skb_irq(skb);
1442                        sis_priv->tx_skbuff[i] = 0;
1443                        sis_priv->tx_ring[i].cmdsts = 0;
1444                        sis_priv->tx_ring[i].bufptr = 0;
1445                        sis_priv->stats.tx_dropped++;
1446                }
1447        }
1448        sis_priv->tx_full = 0;
1449        netif_wake_queue(net_dev);
1450
1451        spin_unlock_irqrestore(&sis_priv->lock, flags);
1452
1453        net_dev->trans_start = jiffies;
1454
1455        /* load Transmit Descriptor Register */
1456        outl(sis_priv->tx_ring_dma, ioaddr + txdp);
1457
1458        /* Enable all known interrupts by setting the interrupt mask. */
1459        outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
1460        return;
1461}
1462
1463/**
1464 *      sis900_start_xmit - sis900 start transmit routine
1465 *      @skb: socket buffer pointer to put the data being transmitted
1466 *      @net_dev: the net device to transmit with
1467 *
1468 *      Set the transmit buffer descriptor, 
1469 *      and write TxENA to enable transimt state machine.
1470 *      tell upper layer if the buffer is full
1471 */
1472
1473static int
1474sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
1475{
1476        struct sis900_private *sis_priv = net_dev->priv;
1477        long ioaddr = net_dev->base_addr;
1478        unsigned int  entry;
1479        unsigned long flags;
1480        unsigned int  index_cur_tx, index_dirty_tx;
1481        unsigned int  count_dirty_tx;
1482
1483        /* Don't transmit data before the complete of auto-negotiation */
1484        if(!sis_priv->autong_complete){
1485                netif_stop_queue(net_dev);
1486                return 1;
1487        }
1488
1489        spin_lock_irqsave(&sis_priv->lock, flags);
1490
1491        /* Calculate the next Tx descriptor entry. */
1492        entry = sis_priv->cur_tx % NUM_TX_DESC;
1493        sis_priv->tx_skbuff[entry] = skb;
1494
1495        /* set the transmit buffer descriptor and enable Transmit State Machine */
1496        sis_priv->tx_ring[entry].bufptr = pci_map_single(sis_priv->pci_dev,
1497                skb->data, skb->len, PCI_DMA_TODEVICE);
1498        sis_priv->tx_ring[entry].cmdsts = (OWN | skb->len);
1499        outl(TxENA | inl(ioaddr + cr), ioaddr + cr);
1500
1501        sis_priv->cur_tx ++;
1502        index_cur_tx = sis_priv->cur_tx;
1503        index_dirty_tx = sis_priv->dirty_tx;
1504
1505        for (count_dirty_tx = 0; index_cur_tx != index_dirty_tx; index_dirty_tx++)
1506                count_dirty_tx ++;
1507
1508        if (index_cur_tx == index_dirty_tx) {
1509                /* dirty_tx is met in the cycle of cur_tx, buffer full */
1510                sis_priv->tx_full = 1;
1511                netif_stop_queue(net_dev);
1512        } else if (count_dirty_tx < NUM_TX_DESC) { 
1513                /* Typical path, tell upper layer that more transmission is possible */
1514                netif_start_queue(net_dev);
1515        } else {
1516                /* buffer full, tell upper layer no more transmission */
1517                sis_priv->tx_full = 1;
1518                netif_stop_queue(net_dev);
1519        }
1520
1521        spin_unlock_irqrestore(&sis_priv->lock, flags);
1522
1523        net_dev->trans_start = jiffies;
1524
1525        if (sis900_debug > 3)
1526                printk(KERN_INFO "%s: Queued Tx packet at %p size %d "
1527                       "to slot %d.\n",
1528                       net_dev->name, skb->data, (int)skb->len, entry);
1529
1530        return 0;
1531}
1532
1533/**
1534 *      sis900_interrupt - sis900 interrupt handler
1535 *      @irq: the irq number
1536 *      @dev_instance: the client data object
1537 *      @regs: snapshot of processor context
1538 *
1539 *      The interrupt handler does all of the Rx thread work, 
1540 *      and cleans up after the Tx thread
1541 */
1542
1543static irqreturn_t sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1544{
1545        struct net_device *net_dev = dev_instance;
1546        struct sis900_private *sis_priv = net_dev->priv;
1547        int boguscnt = max_interrupt_work;
1548        long ioaddr = net_dev->base_addr;
1549        u32 status;
1550        unsigned int handled = 0;
1551
1552        spin_lock (&sis_priv->lock);
1553
1554        do {
1555                status = inl(ioaddr + isr);
1556
1557                if ((status & (HIBERR|TxURN|TxERR|TxIDLE|RxORN|RxERR|RxOK)) == 0)
1558                        /* nothing intresting happened */
1559                        break;
1560                handled = 1;
1561
1562                /* why dow't we break after Tx/Rx case ?? keyword: full-duplex */
1563                if (status & (RxORN | RxERR | RxOK))
1564                        /* Rx interrupt */
1565                        sis900_rx(net_dev);
1566
1567                if (status & (TxURN | TxERR | TxIDLE))
1568                        /* Tx interrupt */
1569                        sis900_finish_xmit(net_dev);
1570
1571                /* something strange happened !!! */
1572                if (status & HIBERR) {
1573                        printk(KERN_INFO "%s: Abnormal interrupt,"
1574                               "status %#8.8x.\n", net_dev->name, status);
1575                        break;
1576                }
1577                if (--boguscnt < 0) {
1578                        printk(KERN_INFO "%s: Too much work at interrupt, "
1579                               "interrupt status = %#8.8x.\n",
1580                               net_dev->name, status);
1581                        break;
1582                }
1583        } while (1);
1584
1585        if (sis900_debug > 3)
1586                printk(KERN_INFO "%s: exiting interrupt, "
1587                       "interrupt status = 0x%#8.8x.\n",
1588                       net_dev->name, inl(ioaddr + isr));
1589        
1590        spin_unlock (&sis_priv->lock);
1591        return IRQ_RETVAL(handled);
1592}
1593
1594/**
1595 *      sis900_rx - sis900 receive routine
1596 *      @net_dev: the net device which receives data
1597 *
1598 *      Process receive interrupt events, 
1599 *      put buffer to higher layer and refill buffer pool
1600 *      Note: This fucntion is called by interrupt handler, 
1601 *      don't do "too much" work here
1602 */
1603
1604static int sis900_rx(struct net_device *net_dev)
1605{
1606        struct sis900_private *sis_priv = net_dev->priv;
1607        long ioaddr = net_dev->base_addr;
1608        unsigned int entry = sis_priv->cur_rx % NUM_RX_DESC;
1609        u32 rx_status = sis_priv->rx_ring[entry].cmdsts;
1610
1611        if (sis900_debug > 3)
1612                printk(KERN_INFO "sis900_rx, cur_rx:%4.4d, dirty_rx:%4.4d "
1613                       "status:0x%8.8x\n",
1614                       sis_priv->cur_rx, sis_priv->dirty_rx, rx_status);
1615
1616        while (rx_status & OWN) {
1617                unsigned int rx_size;
1618
1619                rx_size = (rx_status & DSIZE) - CRC_SIZE;
1620
1621                if (rx_status & (ABORT|OVERRUN|TOOLONG|RUNT|RXISERR|CRCERR|FAERR)) {
1622                        /* corrupted packet received */
1623                        if (sis900_debug > 3)
1624                                printk(KERN_INFO "%s: Corrupted packet "
1625                                       "received, buffer status = 0x%8.8x.\n",
1626                                       net_dev->name, rx_status);
1627                        sis_priv->stats.rx_errors++;
1628                        if (rx_status & OVERRUN)
1629                                sis_priv->stats.rx_over_errors++;
1630                        if (rx_status & (TOOLONG|RUNT))
1631                                sis_priv->stats.rx_length_errors++;
1632                        if (rx_status & (RXISERR | FAERR))
1633                                sis_priv->stats.rx_frame_errors++;
1634                        if (rx_status & CRCERR) 
1635                                sis_priv->stats.rx_crc_errors++;
1636                        /* reset buffer descriptor state */
1637                        sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1638                } else {
1639                        struct sk_buff * skb;
1640
1641                        /* This situation should never happen, but due to
1642                           some unknow bugs, it is possible that
1643                           we are working on NULL sk_buff :-( */
1644                        if (sis_priv->rx_skbuff[entry] == NULL) {
1645                                printk(KERN_INFO "%s: NULL pointer " 
1646                                       "encountered in Rx ring, skipping\n",
1647                                       net_dev->name);
1648                                break;
1649                        }
1650
1651                        pci_dma_sync_single(sis_priv->pci_dev, 
1652                                sis_priv->rx_ring[entry].bufptr, RX_BUF_SIZE, 
1653                                PCI_DMA_FROMDEVICE);
1654                        pci_unmap_single(sis_priv->pci_dev, 
1655                                sis_priv->rx_ring[entry].bufptr, RX_BUF_SIZE, 
1656                                PCI_DMA_FROMDEVICE);
1657                        /* give the socket buffer to upper layers */
1658                        skb = sis_priv->rx_skbuff[entry];
1659                        skb_put(skb, rx_size);
1660                        skb->protocol = eth_type_trans(skb, net_dev);
1661                        netif_rx(skb);
1662
1663                        /* some network statistics */
1664                        if ((rx_status & BCAST) == MCAST)
1665                                sis_priv->stats.multicast++;
1666                        net_dev->last_rx = jiffies;
1667                        sis_priv->stats.rx_bytes += rx_size;
1668                        sis_priv->stats.rx_packets++;
1669
1670                        /* refill the Rx buffer, what if there is not enought memory for
1671                           new socket buffer ?? */
1672                        if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1673                                /* not enough memory for skbuff, this makes a "hole"
1674                                   on the buffer ring, it is not clear how the
1675                                   hardware will react to this kind of degenerated
1676                                   buffer */
1677                                printk(KERN_INFO "%s: Memory squeeze,"
1678                                       "deferring packet.\n",
1679                                       net_dev->name);
1680                                sis_priv->rx_skbuff[entry] = NULL;
1681                                /* reset buffer descriptor state */
1682                                sis_priv->rx_ring[entry].cmdsts = 0;
1683                                sis_priv->rx_ring[entry].bufptr = 0;
1684                                sis_priv->stats.rx_dropped++;
1685                                break;
1686                        }
1687                        skb->dev = net_dev;
1688                        sis_priv->rx_skbuff[entry] = skb;
1689                        sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1690                        sis_priv->rx_ring[entry].bufptr = 
1691                                pci_map_single(sis_priv->pci_dev, skb->tail, 
1692                                        RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1693                        sis_priv->dirty_rx++;
1694                }
1695                sis_priv->cur_rx++;
1696                entry = sis_priv->cur_rx % NUM_RX_DESC;
1697                rx_status = sis_priv->rx_ring[entry].cmdsts;
1698        } // while
1699
1700        /* refill the Rx buffer, what if the rate of refilling is slower than 
1701           consuming ?? */
1702        for (;sis_priv->cur_rx - sis_priv->dirty_rx > 0; sis_priv->dirty_rx++) {
1703                struct sk_buff *skb;
1704
1705                entry = sis_priv->dirty_rx % NUM_RX_DESC;
1706
1707                if (sis_priv->rx_skbuff[entry] == NULL) {
1708                        if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1709                                /* not enough memory for skbuff, this makes a "hole"
1710                                   on the buffer ring, it is not clear how the 
1711                                   hardware will react to this kind of degenerated 
1712                                   buffer */
1713                                printk(KERN_INFO "%s: Memory squeeze,"
1714                                       "deferring packet.\n",
1715                                       net_dev->name);
1716                                sis_priv->stats.rx_dropped++;
1717                                break;
1718                        }
1719                        skb->dev = net_dev;
1720                        sis_priv->rx_skbuff[entry] = skb;
1721                        sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1722                        sis_priv->rx_ring[entry].bufptr =
1723                                pci_map_single(sis_priv->pci_dev, skb->tail,
1724                                        RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1725                }
1726        }
1727        /* re-enable the potentially idle receive state matchine */
1728        outl(RxENA | inl(ioaddr + cr), ioaddr + cr );
1729
1730        return 0;
1731}
1732
1733/**
1734 *      sis900_finish_xmit - finish up transmission of packets
1735 *      @net_dev: the net device to be transmitted on
1736 *
1737 *      Check for error condition and free socket buffer etc 
1738 *      schedule for more transmission as needed
1739 *      Note: This fucntion is called by interrupt handler, 
1740 *      don't do "too much" work here
1741 */
1742
1743static void sis900_finish_xmit (struct net_device *net_dev)
1744{
1745        struct sis900_private *sis_priv = net_dev->priv;
1746
1747        for (; sis_priv->dirty_tx != sis_priv->cur_tx; sis_priv->dirty_tx++) {
1748                struct sk_buff *skb;
1749                unsigned int entry;
1750                u32 tx_status;
1751
1752                entry = sis_priv->dirty_tx % NUM_TX_DESC;
1753                tx_status = sis_priv->tx_ring[entry].cmdsts;
1754
1755                if (tx_status & OWN) {
1756                        /* The packet is not transmitted yet (owned by hardware) !
1757                           Note: the interrupt is generated only when Tx Machine
1758                           is idle, so this is an almost impossible case */
1759                        break;
1760                }
1761
1762                if (tx_status & (ABORT | UNDERRUN | OWCOLL)) {
1763                        /* packet unsuccessfully transmitted */
1764                        if (sis900_debug > 3)
1765                                printk(KERN_INFO "%s: Transmit "
1766                                       "error, Tx status %8.8x.\n",
1767                                       net_dev->name, tx_status);
1768                        sis_priv->stats.tx_errors++;
1769                        if (tx_status & UNDERRUN)
1770                                sis_priv->stats.tx_fifo_errors++;
1771                        if (tx_status & ABORT)
1772                                sis_priv->stats.tx_aborted_errors++;
1773                        if (tx_status & NOCARRIER)
1774                                sis_priv->stats.tx_carrier_errors++;
1775                        if (tx_status & OWCOLL)
1776                                sis_priv->stats.tx_window_errors++;
1777                } else {
1778                        /* packet successfully transmitted */
1779                        sis_priv->stats.collisions += (tx_status & COLCNT) >> 16;
1780                        sis_priv->stats.tx_bytes += tx_status & DSIZE;
1781                        sis_priv->stats.tx_packets++;
1782                }
1783                /* Free the original skb. */
1784                skb = sis_priv->tx_skbuff[entry];
1785                pci_unmap_single(sis_priv->pci_dev, 
1786                        sis_priv->tx_ring[entry].bufptr, skb->len,
1787                        PCI_DMA_TODEVICE);
1788                dev_kfree_skb_irq(skb);
1789                sis_priv->tx_skbuff[entry] = NULL;
1790                sis_priv->tx_ring[entry].bufptr = 0;
1791                sis_priv->tx_ring[entry].cmdsts = 0;
1792        }
1793
1794        if (sis_priv->tx_full && netif_queue_stopped(net_dev) &&
1795            sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC - 4) {
1796                /* The ring is no longer full, clear tx_full and schedule more transmission
1797                   by netif_wake_queue(net_dev) */
1798                sis_priv->tx_full = 0;
1799                netif_wake_queue (net_dev);
1800        }
1801}
1802
1803/**
1804 *      sis900_close - close sis900 device 
1805 *      @net_dev: the net device to be closed
1806 *
1807 *      Disable interrupts, stop the Tx and Rx Status Machine 
1808 *      free Tx and RX socket buffer
1809 */
1810
1811static int
1812sis900_close(struct net_device *net_dev)
1813{
1814        long ioaddr = net_dev->base_addr;
1815        struct sis900_private *sis_priv = net_dev->priv;
1816        struct sk_buff *skb;
1817        int i;
1818
1819        netif_stop_queue(net_dev);
1820
1821        /* Disable interrupts by clearing the interrupt mask. */
1822        outl(0x0000, ioaddr + imr);
1823        outl(0x0000, ioaddr + ier);
1824
1825        /* Stop the chip's Tx and Rx Status Machine */
1826        outl(RxDIS | TxDIS | inl(ioaddr + cr), ioaddr + cr);
1827
1828        del_timer(&sis_priv->timer);
1829
1830        free_irq(net_dev->irq, net_dev);
1831
1832        /* Free Tx and RX skbuff */
1833        for (i = 0; i < NUM_RX_DESC; i++) {
1834                skb = sis_priv->rx_skbuff[i];
1835                if (skb) {
1836                        pci_unmap_single(sis_priv->pci_dev, 
1837                                sis_priv->rx_ring[i].bufptr,
1838                                RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1839                        dev_kfree_skb(skb);
1840                        sis_priv->rx_skbuff[i] = 0;
1841                }
1842        }
1843        for (i = 0; i < NUM_TX_DESC; i++) {
1844                skb = sis_priv->tx_skbuff[i];
1845                if (skb) {
1846                        pci_unmap_single(sis_priv->pci_dev, 
1847                                sis_priv->tx_ring[i].bufptr, skb->len,
1848                                PCI_DMA_TODEVICE);
1849                        dev_kfree_skb(skb);
1850                        sis_priv->tx_skbuff[i] = 0;
1851                }
1852        }
1853
1854        /* Green! Put the chip in low-power mode. */
1855
1856        return 0;
1857}
1858
1859/**
1860 *      sis900_get_drvinfo - Return information about driver
1861 *      @net_dev: the net device to probe
1862 *      @info: container for info returned
1863 *
1864 *      Process ethtool command such as "ehtool -i" to show information
1865 */
1866 
1867static void sis900_get_drvinfo(struct net_device *net_dev,
1868                               struct ethtool_drvinfo *info)
1869{
1870        struct sis900_private *sis_priv = net_dev->priv;
1871
1872        strcpy (info->driver, SIS900_MODULE_NAME);
1873        strcpy (info->version, SIS900_DRV_VERSION);
1874        strcpy (info->bus_info, pci_name(sis_priv->pci_dev));
1875}
1876
1877static struct ethtool_ops sis900_ethtool_ops = {
1878        .get_drvinfo =          sis900_get_drvinfo,
1879};
1880
1881/**
1882 *      mii_ioctl - process MII i/o control command 
1883 *      @net_dev: the net device to command for
1884 *      @rq: parameter for command
1885 *      @cmd: the i/o command
1886 *
1887 *      Process MII command like read/write MII register
1888 */
1889
1890static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
1891{
1892        struct sis900_private *sis_priv = net_dev->priv;
1893        struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
1894
1895        switch(cmd) {
1896        case SIOCGMIIPHY:               /* Get address of MII PHY in use. */
1897                data->phy_id = sis_priv->mii->phy_addr;
1898                /* Fall Through */
1899
1900        case SIOCGMIIREG:               /* Read MII PHY register. */
1901                data->val_out = mdio_read(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
1902                return 0;
1903
1904        case SIOCSMIIREG:               /* Write MII PHY register. */
1905                if (!capable(CAP_NET_ADMIN))
1906                        return -EPERM;
1907                mdio_write(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
1908                return 0;
1909        default:
1910                return -EOPNOTSUPP;
1911        }
1912}
1913
1914/**
1915 *      sis900_get_stats - Get sis900 read/write statistics 
1916 *      @net_dev: the net device to get statistics for
1917 *
1918 *      get tx/rx statistics for sis900
1919 */
1920
1921static struct net_device_stats *
1922sis900_get_stats(struct net_device *net_dev)
1923{
1924        struct sis900_private *sis_priv = net_dev->priv;
1925
1926        return &sis_priv->stats;
1927}
1928
1929/**
1930 *      sis900_set_config - Set media type by net_device.set_config 
1931 *      @dev: the net device for media type change
1932 *      @map: ifmap passed by ifconfig
1933 *
1934 *      Set media type to 10baseT, 100baseT or 0(for auto) by ifconfig
1935 *      we support only port changes. All other runtime configuration
1936 *      changes will be ignored
1937 */
1938
1939static int sis900_set_config(struct net_device *dev, struct ifmap *map)
1940{    
1941        struct sis900_private *sis_priv = dev->priv;
1942        struct mii_phy *mii_phy = sis_priv->mii;
1943        
1944        u16 status;
1945
1946        if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1947                /* we switch on the ifmap->port field. I couldn't find anything
1948                   like a definition or standard for the values of that field.
1949                   I think the meaning of those values is device specific. But
1950                   since I would like to change the media type via the ifconfig
1951                   command I use the definition from linux/netdevice.h 
1952                   (which seems to be different from the ifport(pcmcia) definition) 
1953                */
1954                switch(map->port){
1955                case IF_PORT_UNKNOWN: /* use auto here */   
1956                        dev->if_port = map->port;
1957                        /* we are going to change the media type, so the Link will
1958                           be temporary down and we need to reflect that here. When
1959                           the Link comes up again, it will be sensed by the sis_timer
1960                           procedure, which also does all the rest for us */
1961                        netif_carrier_off(dev);
1962                
1963                        /* read current state */
1964                        status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1965                
1966                        /* enable auto negotiation and reset the negotioation
1967                           (I don't really know what the auto negatiotiation reset
1968                           really means, but it sounds for me right to do one here)*/
1969                        mdio_write(dev, mii_phy->phy_addr,
1970                                   MII_CONTROL, status | MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
1971
1972                        break;
1973            
1974                case IF_PORT_10BASET: /* 10BaseT */         
1975                        dev->if_port = map->port;
1976                
1977                        /* we are going to change the media type, so the Link will
1978                           be temporary down and we need to reflect that here. When
1979                           the Link comes up again, it will be sensed by the sis_timer
1980                           procedure, which also does all the rest for us */
1981                        netif_carrier_off(dev);
1982        
1983                        /* set Speed to 10Mbps */
1984                        /* read current state */
1985                        status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1986                
1987                        /* disable auto negotiation and force 10MBit mode*/
1988                        mdio_write(dev, mii_phy->phy_addr,
1989                                   MII_CONTROL, status & ~(MII_CNTL_SPEED | MII_CNTL_AUTO));
1990                        break;
1991            
1992                case IF_PORT_100BASET: /* 100BaseT */
1993                case IF_PORT_100BASETX: /* 100BaseTx */ 
1994                        dev->if_port = map->port;
1995                
1996                        /* we are going to change the media type, so the Link will
1997                           be temporary down and we need to reflect that here. When
1998                           the Link comes up again, it will be sensed by the sis_timer
1999                           procedure, which also does all the rest for us */
2000                        netif_carrier_off(dev);
2001                
2002                        /* set Speed to 100Mbps */
2003                        /* disable auto negotiation and enable 100MBit Mode */
2004                        status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
2005                        mdio_write(dev, mii_phy->phy_addr,
2006                                   MII_CONTROL, (status & ~MII_CNTL_SPEED) | MII_CNTL_SPEED);
2007                
2008                        break;
2009            
2010                case IF_PORT_10BASE2: /* 10Base2 */
2011                case IF_PORT_AUI: /* AUI */
2012                case IF_PORT_100BASEFX: /* 100BaseFx */
2013                        /* These Modes are not supported (are they?)*/
2014                        printk(KERN_INFO "Not supported");
2015                        return -EOPNOTSUPP;
2016                        break;
2017            
2018                default:
2019                        printk(KERN_INFO "Invalid");
2020                        return -EINVAL;
2021                }
2022        }
2023        return 0;
2024}
2025
2026/**
2027 *      sis900_mcast_bitnr - compute hashtable index 
2028 *      @addr: multicast address
2029 *      @revision: revision id of chip
2030 *
2031 *      SiS 900 uses the most sigificant 7 bits to index a 128 bits multicast
2032 *      hash table, which makes this function a little bit different from other drivers
2033 *      SiS 900 B0 & 635 M/B uses the most significat 8 bits to index 256 bits
2034 *      multicast hash table. 
2035 */
2036
2037static inline u16 sis900_mcast_bitnr(u8 *addr, u8 revision)
2038{
2039
2040        u32 crc = ether_crc(6, addr);
2041
2042        /* leave 8 or 7 most siginifant bits */
2043        if ((revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV))
2044                return ((int)(crc >> 24));
2045        else
2046                return ((int)(crc >> 25));
2047}
2048
2049/**
2050 *      set_rx_mode - Set SiS900 receive mode 
2051 *      @net_dev: the net device to be set
2052 *
2053 *      Set SiS900 receive mode for promiscuous, multicast, or broadcast mode.
2054 *      And set the appropriate multicast filter.
2055 *      Multicast hash table changes from 128 to 256 bits for 635M/B & 900B0.
2056 */
2057
2058static void set_rx_mode(struct net_device *net_dev)
2059{
2060        long ioaddr = net_dev->base_addr;
2061        struct sis900_private * sis_priv = net_dev->priv;
2062        u16 mc_filter[16] = {0};        /* 256/128 bits multicast hash table */
2063        int i, table_entries;
2064        u32 rx_mode;
2065        u8 revision;
2066
2067        /* 635 Hash Table entires = 256(2^16) */
2068        pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
2069        if((revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV))
2070                table_entries = 16;
2071        else
2072                table_entries = 8;
2073
2074        if (net_dev->flags & IFF_PROMISC) {
2075                /* Accept any kinds of packets */
2076                rx_mode = RFPromiscuous;
2077                for (i = 0; i < table_entries; i++)
2078                        mc_filter[i] = 0xffff;
2079        } else if ((net_dev->mc_count > multicast_filter_limit) ||
2080                   (net_dev->flags & IFF_ALLMULTI)) {
2081                /* too many multicast addresses or accept all multicast packet */
2082                rx_mode = RFAAB | RFAAM;
2083                for (i = 0; i < table_entries; i++)
2084                        mc_filter[i] = 0xffff;
2085        } else {
2086                /* Accept Broadcast packet, destination address matchs our MAC address,
2087                   use Receive Filter to reject unwanted MCAST packet */
2088                struct dev_mc_list *mclist;
2089                rx_mode = RFAAB;
2090                for (i = 0, mclist = net_dev->mc_list; mclist && i < net_dev->mc_count;
2091                     i++, mclist = mclist->next) {
2092                        unsigned int bit_nr =
2093                                sis900_mcast_bitnr(mclist->dmi_addr, revision);
2094                        mc_filter[bit_nr >> 4] |= (1 << bit_nr);
2095                }
2096        }
2097
2098        /* update Multicast Hash Table in Receive Filter */
2099        for (i = 0; i < table_entries; i++) {
2100                /* why plus 0x04 ??, That makes the correct value for hash table. */
2101                outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
2102                outl(mc_filter[i], ioaddr + rfdr);
2103        }
2104
2105        outl(RFEN | rx_mode, ioaddr + rfcr);
2106
2107        /* sis900 is capatable of looping back packet at MAC level for debugging purpose */
2108        if (net_dev->flags & IFF_LOOPBACK) {
2109                u32 cr_saved;
2110                /* We must disable Tx/Rx before setting loopback mode */
2111                cr_saved = inl(ioaddr + cr);
2112                outl(cr_saved | TxDIS | RxDIS, ioaddr + cr);
2113                /* enable loopback */
2114                outl(inl(ioaddr + txcfg) | TxMLB, ioaddr + txcfg);
2115                outl(inl(ioaddr + rxcfg) | RxATX, ioaddr + rxcfg);
2116                /* restore cr */
2117                outl(cr_saved, ioaddr + cr);
2118        }
2119
2120        return;
2121}
2122
2123/**
2124 *      sis900_reset - Reset sis900 MAC 
2125 *      @net_dev: the net device to reset
2126 *
2127 *      reset sis900 MAC and wait until finished
2128 *      reset through command register
2129 *      change backoff algorithm for 900B0 & 635 M/B
2130 */
2131
2132static void sis900_reset(struct net_device *net_dev)
2133{
2134        struct sis900_private * sis_priv = net_dev->priv;
2135        long ioaddr = net_dev->base_addr;
2136        int i = 0;
2137        u32 status = TxRCMP | RxRCMP;
2138        u8  revision;
2139
2140        outl(0, ioaddr + ier);
2141        outl(0, ioaddr + imr);
2142        outl(0, ioaddr + rfcr);
2143
2144        outl(RxRESET | TxRESET | RESET | inl(ioaddr + cr), ioaddr + cr);
2145        
2146        /* Check that the chip has finished the reset. */
2147        while (status && (i++ < 1000)) {
2148                status ^= (inl(isr + ioaddr) & status);
2149        }
2150
2151        pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
2152        if( (revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV) )
2153                outl(PESEL | RND_CNT, ioaddr + cfg);
2154        else
2155                outl(PESEL, ioaddr + cfg);
2156}
2157
2158/**
2159 *      sis900_remove - Remove sis900 device 
2160 *      @pci_dev: the pci device to be removed
2161 *
2162 *      remove and release SiS900 net device
2163 */
2164
2165static void __devexit sis900_remove(struct pci_dev *pci_dev)
2166{
2167        struct net_device *net_dev = pci_get_drvdata(pci_dev);
2168        struct sis900_private * sis_priv = net_dev->priv;
2169        struct mii_phy *phy = NULL;
2170
2171        while (sis_priv->first_mii) {
2172                phy = sis_priv->first_mii;
2173                sis_priv->first_mii = phy->next;
2174                kfree(phy);
2175        }
2176
2177        pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
2178                sis_priv->rx_ring_dma);
2179        pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
2180                sis_priv->tx_ring_dma);
2181        unregister_netdev(net_dev);
2182        free_netdev(net_dev);
2183        pci_release_regions(pci_dev);
2184        pci_set_drvdata(pci_dev, NULL);
2185}
2186
2187static struct pci_driver sis900_pci_driver = {
2188        .name           = SIS900_MODULE_NAME,
2189        .id_table       = sis900_pci_tbl,
2190        .probe          = sis900_probe,
2191        .remove         = __devexit_p(sis900_remove),
2192};
2193
2194static int __init sis900_init_module(void)
2195{
2196/* when a module, this is printed whether or not devices are found in probe */
2197#ifdef MODULE
2198        printk(version);
2199#endif
2200
2201        return pci_module_init(&sis900_pci_driver);
2202}
2203
2204static void __exit sis900_cleanup_module(void)
2205{
2206        pci_unregister_driver(&sis900_pci_driver);
2207}
2208
2209module_init(sis900_init_module);
2210module_exit(sis900_cleanup_module);
2211
2212
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.