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