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