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