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