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
5 Modified from the driver which is originally written by Donald Becker.
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.
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
21 Rev 1.08.07 Nov. 2 2003 Daniele Venzano <webvenza@libero.it> add suspend/resume support
22 Rev 1.08.06 Sep. 24 2002 Mufasa Yang bug fix for Tx timeout & add SiS963 support
23 Rev 1.08.05 Jun. 6 2002 Mufasa Yang bug fix for read_eeprom & Tx descriptor over-boundary
24 Rev 1.08.04 Apr. 25 2002 Mufasa Yang <mufasa@sis.com.tw> added SiS962 support
25 Rev 1.08.03 Feb. 1 2002 Matt Domsch <Matt_Domsch@dell.com> update to use library crc32 function
26 Rev 1.08.02 Nov. 30 2001 Hui-Fen Hsu workaround for EDB & bug fix for dhcp problem
27 Rev 1.08.01 Aug. 25 2001 Hui-Fen Hsu update for 630ET & workaround for ICS1893 PHY
28 Rev 1.08.00 Jun. 11 2001 Hui-Fen Hsu workaround for RTL8201 PHY and some bug fix
29 Rev 1.07.11 Apr. 2 2001 Hui-Fen Hsu updates PCI drivers to use the new pci_set_dma_mask for kernel 2.4.3
30 Rev 1.07.10 Mar. 1 2001 Hui-Fen Hsu <hfhsu@sis.com.tw> some bug fix & 635M/B support
31 Rev 1.07.09 Feb. 9 2001 Dave Jones <davej@suse.de> PCI enable cleanup
32 Rev 1.07.08 Jan. 8 2001 Lei-Chun Chang added RTL8201 PHY support
33 Rev 1.07.07 Nov. 29 2000 Lei-Chun Chang added kernel-doc extractable documentation and 630 workaround fix
34 Rev 1.07.06 Nov. 7 2000 Jeff Garzik <jgarzik@pobox.com> some bug fix and cleaning
35 Rev 1.07.05 Nov. 6 2000 metapirat<metapirat@gmx.de> contribute media type select by ifconfig
36 Rev 1.07.04 Sep. 6 2000 Lei-Chun Chang added ICS1893 PHY support
37 Rev 1.07.03 Aug. 24 2000 Lei-Chun Chang (lcchang@sis.com.tw) modified 630E eqaulizer workaround rule
38 Rev 1.07.01 Aug. 08 2000 Ollie Lho minor update for SiS 630E and SiS 630E A1
39 Rev 1.07 Mar. 07 2000 Ollie Lho bug fix in Rx buffer ring
40 Rev 1.06.04 Feb. 11 2000 Jeff Garzik <jgarzik@pobox.com> softnet and init for kernel 2.4
41 Rev 1.06.03 Dec. 23 1999 Ollie Lho Third release
42 Rev 1.06.02 Nov. 23 1999 Ollie Lho bug in mac probing fixed
43 Rev 1.06.01 Nov. 16 1999 Ollie Lho CRC calculation provide by Joseph Zbiciak (im14u2c@primenet.com)
44 Rev 1.06 Nov. 4 1999 Ollie Lho (ollie@sis.com.tw) Second release
45 Rev 1.05.05 Oct. 29 1999 Ollie Lho (ollie@sis.com.tw) Single buffer Tx/Rx
46 Chin-Shan Li (lcs@sis.com.tw) Added AMD Am79c901 HomePNA PHY support
47 Rev 1.05 Aug. 7 1999 Jim Huang (cmhuang@sis.com.tw) Initial release
50 #include <linux/module.h>
51 #include <linux/kernel.h>
52 #include <linux/string.h>
53 #include <linux/timer.h>
54 #include <linux/errno.h>
55 #include <linux/ioport.h>
56 #include <linux/slab.h>
57 #include <linux/interrupt.h>
58 #include <linux/pci.h>
59 #include <linux/netdevice.h>
60 #include <linux/init.h>
61 #include <linux/mii.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
64 #include <linux/delay.h>
65 #include <linux/ethtool.h>
66 #include <linux/crc32.h>
68 #include <asm/processor.h> /* Processor type for cache alignment. */
69 #include <asm/bitops.h>
71 #include <asm/uaccess.h> /* User space memory access functions */
75 #define SIS900_MODULE_NAME "sis900"
76 #define SIS900_DRV_VERSION "v1.08.07 11/02/2003"
78 static char version[] __devinitdata =
79 KERN_INFO "sis900.c: " SIS900_DRV_VERSION "\n";
81 static int max_interrupt_work = 40;
82 static int multicast_filter_limit = 128;
84 #define sis900_debug debug
85 static int sis900_debug;
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
96 static char * card_names[] = {
97 "SiS 900 PCI Fast Ethernet",
98 "SiS 7016 PCI Fast Ethernet"
100 static struct pci_device_id sis900_pci_tbl [] = {
101 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_900,
102 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_900},
103 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7016,
104 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7016},
107 MODULE_DEVICE_TABLE (pci, sis900_pci_tbl);
109 static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex);
111 static struct mii_chip_info {
120 } mii_chip_table[] = {
121 { "SiS 900 Internal MII PHY", 0x001d, 0x8000, LAN },
122 { "SiS 7014 Physical Layer Solution", 0x0016, 0xf830, LAN },
123 { "AMD 79C901 10BASE-T PHY", 0x0000, 0x6B70, LAN },
124 { "AMD 79C901 HomePNA PHY", 0x0000, 0x6B90, HOME},
125 { "ICS LAN PHY", 0x0015, 0xF440, LAN },
126 { "NS 83851 PHY", 0x2000, 0x5C20, MIX },
127 { "Realtek RTL8201 PHY", 0x0000, 0x8200, LAN },
128 { "VIA 6103 PHY", 0x0101, 0x8f20, LAN },
133 struct mii_phy * next;
141 typedef struct _BufferDesc {
147 struct sis900_private {
148 struct net_device_stats stats;
149 struct pci_dev * pci_dev;
153 struct mii_phy * mii;
154 struct mii_phy * first_mii; /* record the first mii structure */
155 unsigned int cur_phy;
157 struct timer_list timer; /* Link status detection timer. */
158 u8 autong_complete; /* 1: auto-negotiate complete */
160 unsigned int cur_rx, dirty_rx; /* producer/comsumer pointers for Tx/Rx ring */
161 unsigned int cur_tx, dirty_tx;
163 /* The saved address of a sent/receive-in-place packet buffer */
164 struct sk_buff *tx_skbuff[NUM_TX_DESC];
165 struct sk_buff *rx_skbuff[NUM_RX_DESC];
169 dma_addr_t tx_ring_dma;
170 dma_addr_t rx_ring_dma;
172 unsigned int tx_full; /* The Tx queue is full. */
177 MODULE_AUTHOR("Jim Huang <cmhuang@sis.com.tw>, Ollie Lho <ollie@sis.com.tw>");
178 MODULE_DESCRIPTION("SiS 900 PCI Fast Ethernet driver");
179 MODULE_LICENSE("GPL");
181 MODULE_PARM(multicast_filter_limit, "i");
182 MODULE_PARM(max_interrupt_work, "i");
183 MODULE_PARM(debug, "i");
184 MODULE_PARM_DESC(multicast_filter_limit, "SiS 900/7016 maximum number of filtered multicast addresses");
185 MODULE_PARM_DESC(max_interrupt_work, "SiS 900/7016 maximum events handled per interrupt");
186 MODULE_PARM_DESC(debug, "SiS 900/7016 debug level (2-4)");
188 static int sis900_open(struct net_device *net_dev);
189 static int sis900_mii_probe (struct net_device * net_dev);
190 static void sis900_init_rxfilter (struct net_device * net_dev);
191 static u16 read_eeprom(long ioaddr, int location);
192 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location);
193 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int val);
194 static void sis900_timer(unsigned long data);
195 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy);
196 static void sis900_tx_timeout(struct net_device *net_dev);
197 static void sis900_init_tx_ring(struct net_device *net_dev);
198 static void sis900_init_rx_ring(struct net_device *net_dev);
199 static int sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev);
200 static int sis900_rx(struct net_device *net_dev);
201 static void sis900_finish_xmit (struct net_device *net_dev);
202 static irqreturn_t sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
203 static int sis900_close(struct net_device *net_dev);
204 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd);
205 static struct net_device_stats *sis900_get_stats(struct net_device *net_dev);
206 static u16 sis900_mcast_bitnr(u8 *addr, u8 revision);
207 static void set_rx_mode(struct net_device *net_dev);
208 static void sis900_reset(struct net_device *net_dev);
209 static void sis630_set_eq(struct net_device *net_dev, u8 revision);
210 static int sis900_set_config(struct net_device *dev, struct ifmap *map);
211 static u16 sis900_default_phy(struct net_device * net_dev);
212 static void sis900_set_capability( struct net_device *net_dev ,struct mii_phy *phy);
213 static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr);
214 static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr);
215 static void sis900_set_mode (long ioaddr, int speed, int duplex);
216 static struct ethtool_ops sis900_ethtool_ops;
219 * sis900_get_mac_addr - Get MAC address for stand alone SiS900 model
220 * @pci_dev: the sis900 pci device
221 * @net_dev: the net device to get address for
223 * Older SiS900 and friends, use EEPROM to store MAC address.
224 * MAC address is read from read_eeprom() into @net_dev->dev_addr.
227 static int __devinit sis900_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
229 long ioaddr = pci_resource_start(pci_dev, 0);
233 /* check to see if we have sane EEPROM */
234 signature = (u16) read_eeprom(ioaddr, EEPROMSignature);
235 if (signature == 0xffff || signature == 0x0000) {
236 printk (KERN_INFO "%s: Error EERPOM read %x\n",
237 net_dev->name, signature);
241 /* get MAC address from EEPROM */
242 for (i = 0; i < 3; i++)
243 ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
249 * sis630e_get_mac_addr - Get MAC address for SiS630E model
250 * @pci_dev: the sis900 pci device
251 * @net_dev: the net device to get address for
253 * SiS630E model, use APC CMOS RAM to store MAC address.
254 * APC CMOS RAM is accessed through ISA bridge.
255 * MAC address is read into @net_dev->dev_addr.
258 static int __devinit sis630e_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
260 struct pci_dev *isa_bridge = NULL;
264 isa_bridge = pci_find_device(PCI_VENDOR_ID_SI, 0x0008, isa_bridge);
266 isa_bridge = pci_find_device(PCI_VENDOR_ID_SI, 0x0018, isa_bridge);
268 printk("%s: Can not find ISA bridge\n", net_dev->name);
272 pci_read_config_byte(isa_bridge, 0x48, ®);
273 pci_write_config_byte(isa_bridge, 0x48, reg | 0x40);
275 for (i = 0; i < 6; i++) {
276 outb(0x09 + i, 0x70);
277 ((u8 *)(net_dev->dev_addr))[i] = inb(0x71);
279 pci_write_config_byte(isa_bridge, 0x48, reg & ~0x40);
286 * sis635_get_mac_addr - Get MAC address for SIS635 model
287 * @pci_dev: the sis900 pci device
288 * @net_dev: the net device to get address for
290 * SiS635 model, set MAC Reload Bit to load Mac address from APC
291 * to rfdr. rfdr is accessed through rfcr. MAC address is read into
292 * @net_dev->dev_addr.
295 static int __devinit sis635_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
297 long ioaddr = net_dev->base_addr;
301 rfcrSave = inl(rfcr + ioaddr);
303 outl(rfcrSave | RELOAD, ioaddr + cr);
304 outl(0, ioaddr + cr);
306 /* disable packet filtering before setting filter */
307 outl(rfcrSave & ~RFEN, rfcr + ioaddr);
309 /* load MAC addr to filter data register */
310 for (i = 0 ; i < 3 ; i++) {
311 outl((i << RFADDR_shift), ioaddr + rfcr);
312 *( ((u16 *)net_dev->dev_addr) + i) = inw(ioaddr + rfdr);
315 /* enable packet filtering */
316 outl(rfcrSave | RFEN, rfcr + ioaddr);
322 * sis96x_get_mac_addr - Get MAC address for SiS962 or SiS963 model
323 * @pci_dev: the sis900 pci device
324 * @net_dev: the net device to get address for
326 * SiS962 or SiS963 model, use EEPROM to store MAC address. And EEPROM
328 * LAN and 1394. When access EEPROM, send EEREQ signal to hardware first
329 * and wait for EEGNT. If EEGNT is ON, EEPROM is permitted to be access
330 * by LAN, otherwise is not. After MAC address is read from EEPROM, send
331 * EEDONE signal to refuse EEPROM access by LAN.
332 * The EEPROM map of SiS962 or SiS963 is different to SiS900.
333 * The signature field in SiS962 or SiS963 spec is meaningless.
334 * MAC address is read into @net_dev->dev_addr.
337 static int __devinit sis96x_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
339 long ioaddr = net_dev->base_addr;
340 long ee_addr = ioaddr + mear;
344 outl(EEREQ, ee_addr);
345 while(waittime < 2000) {
346 if(inl(ee_addr) & EEGNT) {
348 /* get MAC address from EEPROM */
349 for (i = 0; i < 3; i++)
350 ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
352 outl(EEDONE, ee_addr);
359 outl(EEDONE, ee_addr);
364 * sis900_probe - Probe for sis900 device
365 * @pci_dev: the sis900 pci device
366 * @pci_id: the pci device ID
368 * Check and probe sis900 net device for @pci_dev.
369 * Get mac address according to the chip revision,
370 * and assign SiS900-specific entries in the device structure.
371 * ie: sis900_open(), sis900_start_xmit(), sis900_close(), etc.
374 static int __devinit sis900_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
376 struct sis900_private *sis_priv;
377 struct net_device *net_dev;
384 char *card_name = card_names[pci_id->driver_data];
386 /* when built into the kernel, we only print version if device is found */
388 static int printed_version;
389 if (!printed_version++)
393 /* setup various bits in PCI command register */
394 ret = pci_enable_device(pci_dev);
397 i = pci_set_dma_mask(pci_dev, SIS900_DMA_MASK);
399 printk(KERN_ERR "sis900.c: architecture does not support"
400 "32bit PCI busmaster DMA\n");
404 pci_set_master(pci_dev);
406 net_dev = alloc_etherdev(sizeof(struct sis900_private));
409 SET_MODULE_OWNER(net_dev);
410 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
412 /* We do a request_region() to register /proc/ioports info. */
413 ioaddr = pci_resource_start(pci_dev, 0);
414 ret = pci_request_regions(pci_dev, "sis900");
418 sis_priv = net_dev->priv;
419 net_dev->base_addr = ioaddr;
420 net_dev->irq = pci_dev->irq;
421 sis_priv->pci_dev = pci_dev;
422 spin_lock_init(&sis_priv->lock);
424 pci_set_drvdata(pci_dev, net_dev);
426 ring_space = pci_alloc_consistent(pci_dev, TX_TOTAL_SIZE, &ring_dma);
429 goto err_out_cleardev;
431 sis_priv->tx_ring = (BufferDesc *)ring_space;
432 sis_priv->tx_ring_dma = ring_dma;
434 ring_space = pci_alloc_consistent(pci_dev, RX_TOTAL_SIZE, &ring_dma);
439 sis_priv->rx_ring = (BufferDesc *)ring_space;
440 sis_priv->rx_ring_dma = ring_dma;
442 /* The SiS900-specific entries in the device structure. */
443 net_dev->open = &sis900_open;
444 net_dev->hard_start_xmit = &sis900_start_xmit;
445 net_dev->stop = &sis900_close;
446 net_dev->get_stats = &sis900_get_stats;
447 net_dev->set_config = &sis900_set_config;
448 net_dev->set_multicast_list = &set_rx_mode;
449 net_dev->do_ioctl = &mii_ioctl;
450 net_dev->tx_timeout = sis900_tx_timeout;
451 net_dev->watchdog_timeo = TX_TIMEOUT;
452 net_dev->ethtool_ops = &sis900_ethtool_ops;
454 ret = register_netdev(net_dev);
458 /* Get Mac address according to the chip revision */
459 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
462 if (revision == SIS630E_900_REV)
463 ret = sis630e_get_mac_addr(pci_dev, net_dev);
464 else if ((revision > 0x81) && (revision <= 0x90) )
465 ret = sis635_get_mac_addr(pci_dev, net_dev);
466 else if (revision == SIS96x_900_REV)
467 ret = sis96x_get_mac_addr(pci_dev, net_dev);
469 ret = sis900_get_mac_addr(pci_dev, net_dev);
473 goto err_out_unregister;
476 /* 630ET : set the mii access mode as software-mode */
477 if (revision == SIS630ET_900_REV)
478 outl(ACCESSMODE | inl(ioaddr + cr), ioaddr + cr);
480 /* probe for mii transceiver */
481 if (sis900_mii_probe(net_dev) == 0) {
483 goto err_out_unregister;
486 /* save our host bridge revision */
487 dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_630, NULL);
489 pci_read_config_byte(dev, PCI_CLASS_REVISION, &sis_priv->host_bridge_rev);
491 /* print some information about our NIC */
492 printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ", net_dev->name,
493 card_name, ioaddr, net_dev->irq);
494 for (i = 0; i < 5; i++)
495 printk("%2.2x:", (u8)net_dev->dev_addr[i]);
496 printk("%2.2x.\n", net_dev->dev_addr[i]);
501 unregister_netdev(net_dev);
503 pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
504 sis_priv->rx_ring_dma);
506 pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
507 sis_priv->tx_ring_dma);
509 pci_set_drvdata(pci_dev, NULL);
510 pci_release_regions(pci_dev);
512 free_netdev(net_dev);
517 * sis900_mii_probe - Probe MII PHY for sis900
518 * @net_dev: the net device to probe for
520 * Search for total of 32 possible mii phy addresses.
521 * Identify and set current phy if found one,
522 * return error if it failed to found.
525 static int __init sis900_mii_probe (struct net_device * net_dev)
527 struct sis900_private * sis_priv = net_dev->priv;
528 u16 poll_bit = MII_STAT_LINK, status = 0;
529 unsigned long timeout = jiffies + 5 * HZ;
533 sis_priv->mii = NULL;
535 /* search for total of 32 possible mii phy addresses */
536 for (phy_addr = 0; phy_addr < 32; phy_addr++) {
537 struct mii_phy * mii_phy = NULL;
542 for(i = 0; i < 2; i++)
543 mii_status = mdio_read(net_dev, phy_addr, MII_STATUS);
545 if (mii_status == 0xffff || mii_status == 0x0000)
546 /* the mii is not accessible, try next one */
549 if ((mii_phy = kmalloc(sizeof(struct mii_phy), GFP_KERNEL)) == NULL) {
550 printk(KERN_INFO "Cannot allocate mem for struct mii_phy\n");
551 mii_phy = sis_priv->first_mii;
555 mii_phy = mii_phy->next;
561 mii_phy->phy_id0 = mdio_read(net_dev, phy_addr, MII_PHY_ID0);
562 mii_phy->phy_id1 = mdio_read(net_dev, phy_addr, MII_PHY_ID1);
563 mii_phy->phy_addr = phy_addr;
564 mii_phy->status = mii_status;
565 mii_phy->next = sis_priv->mii;
566 sis_priv->mii = mii_phy;
567 sis_priv->first_mii = mii_phy;
569 for (i = 0; mii_chip_table[i].phy_id1; i++)
570 if ((mii_phy->phy_id0 == mii_chip_table[i].phy_id0 ) &&
571 ((mii_phy->phy_id1 & 0xFFF0) == mii_chip_table[i].phy_id1)){
572 mii_phy->phy_types = mii_chip_table[i].phy_types;
573 if (mii_chip_table[i].phy_types == MIX)
575 (mii_status & (MII_STAT_CAN_TX_FDX | MII_STAT_CAN_TX)) ? LAN : HOME;
576 printk(KERN_INFO "%s: %s transceiver found at address %d.\n",
577 net_dev->name, mii_chip_table[i].name, phy_addr);
581 if( !mii_chip_table[i].phy_id1 ) {
582 printk(KERN_INFO "%s: Unknown PHY transceiver found at address %d.\n",
583 net_dev->name, phy_addr);
584 mii_phy->phy_types = UNKNOWN;
588 if (sis_priv->mii == NULL) {
589 printk(KERN_INFO "%s: No MII transceivers found!\n",
594 /* select default PHY for mac */
595 sis_priv->mii = NULL;
596 sis900_default_phy( net_dev );
598 /* Reset phy if default phy is internal sis900 */
599 if ((sis_priv->mii->phy_id0 == 0x001D) &&
600 ((sis_priv->mii->phy_id1&0xFFF0) == 0x8000))
601 status = sis900_reset_phy(net_dev, sis_priv->cur_phy);
603 /* workaround for ICS1893 PHY */
604 if ((sis_priv->mii->phy_id0 == 0x0015) &&
605 ((sis_priv->mii->phy_id1&0xFFF0) == 0xF440))
606 mdio_write(net_dev, sis_priv->cur_phy, 0x0018, 0xD200);
608 if(status & MII_STAT_LINK){
612 poll_bit ^= (mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS) & poll_bit);
613 if (time_after_eq(jiffies, timeout)) {
614 printk(KERN_WARNING "%s: reset phy and link down now\n", net_dev->name);
620 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
621 if (revision == SIS630E_900_REV) {
622 /* SiS 630E has some bugs on default value of PHY registers */
623 mdio_write(net_dev, sis_priv->cur_phy, MII_ANADV, 0x05e1);
624 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG1, 0x22);
625 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG2, 0xff00);
626 mdio_write(net_dev, sis_priv->cur_phy, MII_MASK, 0xffc0);
627 //mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, 0x1000);
630 if (sis_priv->mii->status & MII_STAT_LINK)
631 netif_carrier_on(net_dev);
633 netif_carrier_off(net_dev);
639 * sis900_default_phy - Select default PHY for sis900 mac.
640 * @net_dev: the net device to probe for
642 * Select first detected PHY with link as default.
643 * If no one is link on, select PHY whose types is HOME as default.
644 * If HOME doesn't exist, select LAN.
647 static u16 sis900_default_phy(struct net_device * net_dev)
649 struct sis900_private * sis_priv = net_dev->priv;
650 struct mii_phy *phy = NULL, *phy_home = NULL, *default_phy = NULL, *phy_lan = NULL;
653 for( phy=sis_priv->first_mii; phy; phy=phy->next ){
654 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
655 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
657 /* Link ON & Not select default PHY & not ghost PHY */
658 if ( (status & MII_STAT_LINK) && !default_phy && (phy->phy_types != UNKNOWN) )
661 status = mdio_read(net_dev, phy->phy_addr, MII_CONTROL);
662 mdio_write(net_dev, phy->phy_addr, MII_CONTROL,
663 status | MII_CNTL_AUTO | MII_CNTL_ISOLATE);
664 if( phy->phy_types == HOME )
666 else if (phy->phy_types == LAN)
671 if( !default_phy && phy_home )
672 default_phy = phy_home;
673 else if( !default_phy && phy_lan )
674 default_phy = phy_lan;
675 else if ( !default_phy )
676 default_phy = sis_priv->first_mii;
678 if( sis_priv->mii != default_phy ){
679 sis_priv->mii = default_phy;
680 sis_priv->cur_phy = default_phy->phy_addr;
681 printk(KERN_INFO "%s: Using transceiver found at address %d as default\n", net_dev->name,sis_priv->cur_phy);
684 status = mdio_read(net_dev, sis_priv->cur_phy, MII_CONTROL);
685 status &= (~MII_CNTL_ISOLATE);
687 mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, status);
688 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
689 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
696 * sis900_set_capability - set the media capability of network adapter.
697 * @net_dev : the net device to probe for
700 * Set the media capability of network adapter according to
701 * mii status register. It's necessary before auto-negotiate.
704 static void sis900_set_capability( struct net_device *net_dev , struct mii_phy *phy )
709 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
710 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
712 cap = MII_NWAY_CSMA_CD |
713 ((phy->status & MII_STAT_CAN_TX_FDX)? MII_NWAY_TX_FDX:0) |
714 ((phy->status & MII_STAT_CAN_TX) ? MII_NWAY_TX:0) |
715 ((phy->status & MII_STAT_CAN_T_FDX) ? MII_NWAY_T_FDX:0)|
716 ((phy->status & MII_STAT_CAN_T) ? MII_NWAY_T:0);
718 mdio_write(net_dev, phy->phy_addr, MII_ANADV, cap);
722 /* Delay between EEPROM clock transitions. */
723 #define eeprom_delay() inl(ee_addr)
726 * read_eeprom - Read Serial EEPROM
727 * @ioaddr: base i/o address
728 * @location: the EEPROM location to read
730 * Read Serial EEPROM through EEPROM Access Register.
731 * Note that location is in word (16 bits) unit
734 static u16 __devinit read_eeprom(long ioaddr, int location)
738 long ee_addr = ioaddr + mear;
739 u32 read_cmd = location | EEread;
746 /* Shift the read command (9) bits out. */
747 for (i = 8; i >= 0; i--) {
748 u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
749 outl(dataval, ee_addr);
751 outl(dataval | EECLK, ee_addr);
757 /* read the 16-bits data in */
758 for (i = 16; i > 0; i--) {
761 outl(EECS | EECLK, ee_addr);
763 retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
767 /* Terminate the EEPROM access. */
774 /* Read and write the MII management registers using software-generated
775 serial MDIO protocol. Note that the command bits and data bits are
776 send out separately */
777 #define mdio_delay() inl(mdio_addr)
779 static void mdio_idle(long mdio_addr)
781 outl(MDIO | MDDIR, mdio_addr);
783 outl(MDIO | MDDIR | MDC, mdio_addr);
786 /* Syncronize the MII management interface by shifting 32 one bits out. */
787 static void mdio_reset(long mdio_addr)
791 for (i = 31; i >= 0; i--) {
792 outl(MDDIR | MDIO, mdio_addr);
794 outl(MDDIR | MDIO | MDC, mdio_addr);
801 * mdio_read - read MII PHY register
802 * @net_dev: the net device to read
803 * @phy_id: the phy address to read
804 * @location: the phy regiester id to read
806 * Read MII registers through MDIO and MDC
807 * using MDIO management frame structure and protocol(defined by ISO/IEC).
808 * Please see SiS7014 or ICS spec
811 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location)
813 long mdio_addr = net_dev->base_addr + mear;
814 int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
818 mdio_reset(mdio_addr);
819 mdio_idle(mdio_addr);
821 for (i = 15; i >= 0; i--) {
822 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
823 outl(dataval, mdio_addr);
825 outl(dataval | MDC, mdio_addr);
829 /* Read the 16 data bits. */
830 for (i = 16; i > 0; i--) {
833 retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
834 outl(MDC, mdio_addr);
837 outl(0x00, mdio_addr);
843 * mdio_write - write MII PHY register
844 * @net_dev: the net device to write
845 * @phy_id: the phy address to write
846 * @location: the phy regiester id to write
847 * @value: the register value to write with
849 * Write MII registers with @value through MDIO and MDC
850 * using MDIO management frame structure and protocol(defined by ISO/IEC)
851 * please see SiS7014 or ICS spec
854 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int value)
856 long mdio_addr = net_dev->base_addr + mear;
857 int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
860 mdio_reset(mdio_addr);
861 mdio_idle(mdio_addr);
863 /* Shift the command bits out. */
864 for (i = 15; i >= 0; i--) {
865 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
866 outb(dataval, mdio_addr);
868 outb(dataval | MDC, mdio_addr);
873 /* Shift the value bits out. */
874 for (i = 15; i >= 0; i--) {
875 int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
876 outl(dataval, mdio_addr);
878 outl(dataval | MDC, mdio_addr);
883 /* Clear out extra bits. */
884 for (i = 2; i > 0; i--) {
887 outb(MDC, mdio_addr);
890 outl(0x00, mdio_addr);
897 * sis900_reset_phy - reset sis900 mii phy.
898 * @net_dev: the net device to write
899 * @phy_addr: default phy address
901 * Some specific phy can't work properly without reset.
902 * This function will be called during initialization and
903 * link status change from ON to DOWN.
906 static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr)
912 status = mdio_read(net_dev, phy_addr, MII_STATUS);
914 mdio_write( net_dev, phy_addr, MII_CONTROL, MII_CNTL_RESET );
920 * sis900_open - open sis900 device
921 * @net_dev: the net device to open
923 * Do some initialization and start net interface.
924 * enable interrupts and set sis900 timer.
928 sis900_open(struct net_device *net_dev)
930 struct sis900_private *sis_priv = net_dev->priv;
931 long ioaddr = net_dev->base_addr;
935 /* Soft reset the chip. */
936 sis900_reset(net_dev);
938 /* Equalizer workaround Rule */
939 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
940 sis630_set_eq(net_dev, revision);
942 ret = request_irq(net_dev->irq, &sis900_interrupt, SA_SHIRQ, net_dev->name, net_dev);
946 sis900_init_rxfilter(net_dev);
948 sis900_init_tx_ring(net_dev);
949 sis900_init_rx_ring(net_dev);
951 set_rx_mode(net_dev);
953 netif_start_queue(net_dev);
955 /* Workaround for EDB */
956 sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
958 /* Enable all known interrupts by setting the interrupt mask. */
959 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
960 outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
961 outl(IE, ioaddr + ier);
963 sis900_check_mode(net_dev, sis_priv->mii);
965 /* Set the timer to switch to check for link beat and perhaps switch
966 to an alternate media type. */
967 init_timer(&sis_priv->timer);
968 sis_priv->timer.expires = jiffies + HZ;
969 sis_priv->timer.data = (unsigned long)net_dev;
970 sis_priv->timer.function = &sis900_timer;
971 add_timer(&sis_priv->timer);
977 * sis900_init_rxfilter - Initialize the Rx filter
978 * @net_dev: the net device to initialize for
980 * Set receive filter address to our MAC address
981 * and enable packet filtering.
985 sis900_init_rxfilter (struct net_device * net_dev)
987 long ioaddr = net_dev->base_addr;
991 rfcrSave = inl(rfcr + ioaddr);
993 /* disable packet filtering before setting filter */
994 outl(rfcrSave & ~RFEN, rfcr + ioaddr);
996 /* load MAC addr to filter data register */
997 for (i = 0 ; i < 3 ; i++) {
1000 w = (u32) *((u16 *)(net_dev->dev_addr)+i);
1001 outl((i << RFADDR_shift), ioaddr + rfcr);
1002 outl(w, ioaddr + rfdr);
1004 if (sis900_debug > 2) {
1005 printk(KERN_INFO "%s: Receive Filter Addrss[%d]=%x\n",
1006 net_dev->name, i, inl(ioaddr + rfdr));
1010 /* enable packet filtering */
1011 outl(rfcrSave | RFEN, rfcr + ioaddr);
1015 * sis900_init_tx_ring - Initialize the Tx descriptor ring
1016 * @net_dev: the net device to initialize for
1018 * Initialize the Tx descriptor ring,
1022 sis900_init_tx_ring(struct net_device *net_dev)
1024 struct sis900_private *sis_priv = net_dev->priv;
1025 long ioaddr = net_dev->base_addr;
1028 sis_priv->tx_full = 0;
1029 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
1031 for (i = 0; i < NUM_TX_DESC; i++) {
1032 sis_priv->tx_skbuff[i] = NULL;
1034 sis_priv->tx_ring[i].link = sis_priv->tx_ring_dma +
1035 ((i+1)%NUM_TX_DESC)*sizeof(BufferDesc);
1036 sis_priv->tx_ring[i].cmdsts = 0;
1037 sis_priv->tx_ring[i].bufptr = 0;
1040 /* load Transmit Descriptor Register */
1041 outl(sis_priv->tx_ring_dma, ioaddr + txdp);
1042 if (sis900_debug > 2)
1043 printk(KERN_INFO "%s: TX descriptor register loaded with: %8.8x\n",
1044 net_dev->name, inl(ioaddr + txdp));
1048 * sis900_init_rx_ring - Initialize the Rx descriptor ring
1049 * @net_dev: the net device to initialize for
1051 * Initialize the Rx descriptor ring,
1052 * and pre-allocate recevie buffers (socket buffer)
1056 sis900_init_rx_ring(struct net_device *net_dev)
1058 struct sis900_private *sis_priv = net_dev->priv;
1059 long ioaddr = net_dev->base_addr;
1062 sis_priv->cur_rx = 0;
1063 sis_priv->dirty_rx = 0;
1065 /* init RX descriptor */
1066 for (i = 0; i < NUM_RX_DESC; i++) {
1067 sis_priv->rx_skbuff[i] = NULL;
1069 sis_priv->rx_ring[i].link = sis_priv->rx_ring_dma +
1070 ((i+1)%NUM_RX_DESC)*sizeof(BufferDesc);
1071 sis_priv->rx_ring[i].cmdsts = 0;
1072 sis_priv->rx_ring[i].bufptr = 0;
1075 /* allocate sock buffers */
1076 for (i = 0; i < NUM_RX_DESC; i++) {
1077 struct sk_buff *skb;
1079 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1080 /* not enough memory for skbuff, this makes a "hole"
1081 on the buffer ring, it is not clear how the
1082 hardware will react to this kind of degenerated
1087 sis_priv->rx_skbuff[i] = skb;
1088 sis_priv->rx_ring[i].cmdsts = RX_BUF_SIZE;
1089 sis_priv->rx_ring[i].bufptr = pci_map_single(sis_priv->pci_dev,
1090 skb->tail, RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1092 sis_priv->dirty_rx = (unsigned int) (i - NUM_RX_DESC);
1094 /* load Receive Descriptor Register */
1095 outl(sis_priv->rx_ring_dma, ioaddr + rxdp);
1096 if (sis900_debug > 2)
1097 printk(KERN_INFO "%s: RX descriptor register loaded with: %8.8x\n",
1098 net_dev->name, inl(ioaddr + rxdp));
1102 * sis630_set_eq - set phy equalizer value for 630 LAN
1103 * @net_dev: the net device to set equalizer value
1104 * @revision: 630 LAN revision number
1106 * 630E equalizer workaround rule(Cyrus Huang 08/15)
1107 * PHY register 14h(Test)
1108 * Bit 14: 0 -- Automatically dectect (default)
1109 * 1 -- Manually set Equalizer filter
1110 * Bit 13: 0 -- (Default)
1111 * 1 -- Speed up convergence of equalizer setting
1112 * Bit 9 : 0 -- (Default)
1113 * 1 -- Disable Baseline Wander
1114 * Bit 3~7 -- Equalizer filter setting
1115 * Link ON: Set Bit 9, 13 to 1, Bit 14 to 0
1116 * Then calculate equalizer value
1117 * Then set equalizer value, and set Bit 14 to 1, Bit 9 to 0
1118 * Link Off:Set Bit 13 to 1, Bit 14 to 0
1119 * Calculate Equalizer value:
1120 * When Link is ON and Bit 14 is 0, SIS900PHY will auto-dectect proper equalizer value.
1121 * When the equalizer is stable, this value is not a fixed value. It will be within
1122 * a small range(eg. 7~9). Then we get a minimum and a maximum value(eg. min=7, max=9)
1123 * 0 <= max <= 4 --> set equalizer to max
1124 * 5 <= max <= 14 --> set equalizer to max+1 or set equalizer to max+2 if max == min
1125 * max >= 15 --> set equalizer to max+5 or set equalizer to max+6 if max == min
1128 static void sis630_set_eq(struct net_device *net_dev, u8 revision)
1130 struct sis900_private *sis_priv = net_dev->priv;
1131 u16 reg14h, eq_value=0, max_value=0, min_value=0;
1134 if ( !(revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
1135 revision == SIS630A_900_REV || revision == SIS630ET_900_REV) )
1138 if (netif_carrier_ok(net_dev)) {
1139 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1140 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (0x2200 | reg14h) & 0xBFFF);
1141 for (i=0; i < maxcount; i++) {
1142 eq_value=(0x00F8 & mdio_read(net_dev, sis_priv->cur_phy, MII_RESV)) >> 3;
1144 max_value=min_value=eq_value;
1145 max_value=(eq_value > max_value) ? eq_value : max_value;
1146 min_value=(eq_value < min_value) ? eq_value : min_value;
1148 /* 630E rule to determine the equalizer value */
1149 if (revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
1150 revision == SIS630ET_900_REV) {
1153 else if (max_value >= 5 && max_value < 15)
1154 eq_value=(max_value == min_value) ? max_value+2 : max_value+1;
1155 else if (max_value >= 15)
1156 eq_value=(max_value == min_value) ? max_value+6 : max_value+5;
1158 /* 630B0&B1 rule to determine the equalizer value */
1159 if (revision == SIS630A_900_REV &&
1160 (sis_priv->host_bridge_rev == SIS630B0 ||
1161 sis_priv->host_bridge_rev == SIS630B1)) {
1165 eq_value=(max_value+min_value+1)/2;
1167 /* write equalizer value and setting */
1168 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1169 reg14h=(reg14h & 0xFF07) | ((eq_value << 3) & 0x00F8);
1170 reg14h=(reg14h | 0x6000) & 0xFDFF;
1171 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, reg14h);
1174 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1175 if (revision == SIS630A_900_REV &&
1176 (sis_priv->host_bridge_rev == SIS630B0 ||
1177 sis_priv->host_bridge_rev == SIS630B1))
1178 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2200) & 0xBFFF);
1180 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2000) & 0xBFFF);
1186 * sis900_timer - sis900 timer routine
1187 * @data: pointer to sis900 net device
1189 * On each timer ticks we check two things,
1190 * link status (ON/OFF) and link mode (10/100/Full/Half)
1193 static void sis900_timer(unsigned long data)
1195 struct net_device *net_dev = (struct net_device *)data;
1196 struct sis900_private *sis_priv = net_dev->priv;
1197 struct mii_phy *mii_phy = sis_priv->mii;
1198 static int next_tick = 5*HZ;
1202 if (!sis_priv->autong_complete){
1203 int speed, duplex = 0;
1205 sis900_read_mode(net_dev, &speed, &duplex);
1207 sis900_set_mode(net_dev->base_addr, speed, duplex);
1208 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
1209 sis630_set_eq(net_dev, revision);
1210 netif_start_queue(net_dev);
1213 sis_priv->timer.expires = jiffies + HZ;
1214 add_timer(&sis_priv->timer);
1218 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
1219 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
1221 /* Link OFF -> ON */
1222 if (!netif_carrier_ok(net_dev)) {
1224 /* Search for new PHY */
1225 status = sis900_default_phy(net_dev);
1226 mii_phy = sis_priv->mii;
1228 if (status & MII_STAT_LINK){
1229 sis900_check_mode(net_dev, mii_phy);
1230 netif_carrier_on(net_dev);
1233 /* Link ON -> OFF */
1235 if (!(status & MII_STAT_LINK)){
1236 netif_carrier_off(net_dev);
1237 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
1239 /* Change mode issue */
1240 if ((mii_phy->phy_id0 == 0x001D) &&
1241 ((mii_phy->phy_id1 & 0xFFF0) == 0x8000))
1242 sis900_reset_phy(net_dev, sis_priv->cur_phy);
1244 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
1245 sis630_set_eq(net_dev, revision);
1251 sis_priv->timer.expires = jiffies + next_tick;
1252 add_timer(&sis_priv->timer);
1256 * sis900_check_mode - check the media mode for sis900
1257 * @net_dev: the net device to be checked
1258 * @mii_phy: the mii phy
1260 * Older driver gets the media mode from mii status output
1261 * register. Now we set our media capability and auto-negotiate
1262 * to get the upper bound of speed and duplex between two ends.
1263 * If the types of mii phy is HOME, it doesn't need to auto-negotiate
1264 * and autong_complete should be set to 1.
1267 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy)
1269 struct sis900_private *sis_priv = net_dev->priv;
1270 long ioaddr = net_dev->base_addr;
1273 if( mii_phy->phy_types == LAN ){
1274 outl( ~EXD & inl( ioaddr + cfg ), ioaddr + cfg);
1275 sis900_set_capability(net_dev , mii_phy);
1276 sis900_auto_negotiate(net_dev, sis_priv->cur_phy);
1278 outl(EXD | inl( ioaddr + cfg ), ioaddr + cfg);
1279 speed = HW_SPEED_HOME;
1280 duplex = FDX_CAPABLE_HALF_SELECTED;
1281 sis900_set_mode(ioaddr, speed, duplex);
1282 sis_priv->autong_complete = 1;
1287 * sis900_set_mode - Set the media mode of mac register.
1288 * @ioaddr: the address of the device
1289 * @speed : the transmit speed to be determined
1290 * @duplex: the duplex mode to be determined
1292 * Set the media mode of mac register txcfg/rxcfg according to
1293 * speed and duplex of phy. Bit EDB_MASTER_EN indicates the EDB
1294 * bus is used instead of PCI bus. When this bit is set 1, the
1295 * Max DMA Burst Size for TX/RX DMA should be no larger than 16
1299 static void sis900_set_mode (long ioaddr, int speed, int duplex)
1301 u32 tx_flags = 0, rx_flags = 0;
1303 if( inl(ioaddr + cfg) & EDB_MASTER_EN ){
1304 tx_flags = TxATP | (DMA_BURST_64 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
1305 rx_flags = DMA_BURST_64 << RxMXDMA_shift;
1308 tx_flags = TxATP | (DMA_BURST_512 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
1309 rx_flags = DMA_BURST_512 << RxMXDMA_shift;
1312 if (speed == HW_SPEED_HOME || speed == HW_SPEED_10_MBPS ) {
1313 rx_flags |= (RxDRNT_10 << RxDRNT_shift);
1314 tx_flags |= (TxDRNT_10 << TxDRNT_shift);
1317 rx_flags |= (RxDRNT_100 << RxDRNT_shift);
1318 tx_flags |= (TxDRNT_100 << TxDRNT_shift);
1321 if (duplex == FDX_CAPABLE_FULL_SELECTED) {
1322 tx_flags |= (TxCSI | TxHBI);
1326 outl (tx_flags, ioaddr + txcfg);
1327 outl (rx_flags, ioaddr + rxcfg);
1331 * sis900_auto_negotiate - Set the Auto-Negotiation Enable/Reset bit.
1332 * @net_dev: the net device to read mode for
1333 * @phy_addr: mii phy address
1335 * If the adapter is link-on, set the auto-negotiate enable/reset bit.
1336 * autong_complete should be set to 0 when starting auto-negotiation.
1337 * autong_complete should be set to 1 if we didn't start auto-negotiation.
1338 * sis900_timer will wait for link on again if autong_complete = 0.
1341 static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr)
1343 struct sis900_private *sis_priv = net_dev->priv;
1348 status = mdio_read(net_dev, phy_addr, MII_STATUS);
1350 if (!(status & MII_STAT_LINK)){
1351 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
1352 sis_priv->autong_complete = 1;
1353 netif_carrier_off(net_dev);
1357 /* (Re)start AutoNegotiate */
1358 mdio_write(net_dev, phy_addr, MII_CONTROL,
1359 MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
1360 sis_priv->autong_complete = 0;
1365 * sis900_read_mode - read media mode for sis900 internal phy
1366 * @net_dev: the net device to read mode for
1367 * @speed : the transmit speed to be determined
1368 * @duplex : the duplex mode to be determined
1370 * The capability of remote end will be put in mii register autorec
1371 * after auto-negotiation. Use AND operation to get the upper bound
1372 * of speed and duplex between two ends.
1375 static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex)
1377 struct sis900_private *sis_priv = net_dev->priv;
1378 struct mii_phy *phy = sis_priv->mii;
1379 int phy_addr = sis_priv->cur_phy;
1381 u16 autoadv, autorec;
1385 status = mdio_read(net_dev, phy_addr, MII_STATUS);
1387 if (!(status & MII_STAT_LINK))
1390 /* AutoNegotiate completed */
1391 autoadv = mdio_read(net_dev, phy_addr, MII_ANADV);
1392 autorec = mdio_read(net_dev, phy_addr, MII_ANLPAR);
1393 status = autoadv & autorec;
1395 *speed = HW_SPEED_10_MBPS;
1396 *duplex = FDX_CAPABLE_HALF_SELECTED;
1398 if (status & (MII_NWAY_TX | MII_NWAY_TX_FDX))
1399 *speed = HW_SPEED_100_MBPS;
1400 if (status & ( MII_NWAY_TX_FDX | MII_NWAY_T_FDX))
1401 *duplex = FDX_CAPABLE_FULL_SELECTED;
1403 sis_priv->autong_complete = 1;
1405 /* Workaround for Realtek RTL8201 PHY issue */
1406 if((phy->phy_id0 == 0x0000) && ((phy->phy_id1 & 0xFFF0) == 0x8200)){
1407 if(mdio_read(net_dev, phy_addr, MII_CONTROL) & MII_CNTL_FDX)
1408 *duplex = FDX_CAPABLE_FULL_SELECTED;
1409 if(mdio_read(net_dev, phy_addr, 0x0019) & 0x01)
1410 *speed = HW_SPEED_100_MBPS;
1413 printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
1415 *speed == HW_SPEED_100_MBPS ?
1416 "100mbps" : "10mbps",
1417 *duplex == FDX_CAPABLE_FULL_SELECTED ?
1422 * sis900_tx_timeout - sis900 transmit timeout routine
1423 * @net_dev: the net device to transmit
1425 * print transmit timeout status
1426 * disable interrupts and do some tasks
1429 static void sis900_tx_timeout(struct net_device *net_dev)
1431 struct sis900_private *sis_priv = net_dev->priv;
1432 long ioaddr = net_dev->base_addr;
1433 unsigned long flags;
1436 printk(KERN_INFO "%s: Transmit timeout, status %8.8x %8.8x \n",
1437 net_dev->name, inl(ioaddr + cr), inl(ioaddr + isr));
1439 /* Disable interrupts by clearing the interrupt mask. */
1440 outl(0x0000, ioaddr + imr);
1442 /* use spinlock to prevent interrupt handler accessing buffer ring */
1443 spin_lock_irqsave(&sis_priv->lock, flags);
1445 /* discard unsent packets */
1446 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
1447 for (i = 0; i < NUM_TX_DESC; i++) {
1448 struct sk_buff *skb = sis_priv->tx_skbuff[i];
1451 pci_unmap_single(sis_priv->pci_dev,
1452 sis_priv->tx_ring[i].bufptr, skb->len,
1454 dev_kfree_skb_irq(skb);
1455 sis_priv->tx_skbuff[i] = 0;
1456 sis_priv->tx_ring[i].cmdsts = 0;
1457 sis_priv->tx_ring[i].bufptr = 0;
1458 sis_priv->stats.tx_dropped++;
1461 sis_priv->tx_full = 0;
1462 netif_wake_queue(net_dev);
1464 spin_unlock_irqrestore(&sis_priv->lock, flags);
1466 net_dev->trans_start = jiffies;
1468 /* load Transmit Descriptor Register */
1469 outl(sis_priv->tx_ring_dma, ioaddr + txdp);
1471 /* Enable all known interrupts by setting the interrupt mask. */
1472 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
1477 * sis900_start_xmit - sis900 start transmit routine
1478 * @skb: socket buffer pointer to put the data being transmitted
1479 * @net_dev: the net device to transmit with
1481 * Set the transmit buffer descriptor,
1482 * and write TxENA to enable transmit state machine.
1483 * tell upper layer if the buffer is full
1487 sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
1489 struct sis900_private *sis_priv = net_dev->priv;
1490 long ioaddr = net_dev->base_addr;
1492 unsigned long flags;
1493 unsigned int index_cur_tx, index_dirty_tx;
1494 unsigned int count_dirty_tx;
1496 /* Don't transmit data before the complete of auto-negotiation */
1497 if(!sis_priv->autong_complete){
1498 netif_stop_queue(net_dev);
1502 spin_lock_irqsave(&sis_priv->lock, flags);
1504 /* Calculate the next Tx descriptor entry. */
1505 entry = sis_priv->cur_tx % NUM_TX_DESC;
1506 sis_priv->tx_skbuff[entry] = skb;
1508 /* set the transmit buffer descriptor and enable Transmit State Machine */
1509 sis_priv->tx_ring[entry].bufptr = pci_map_single(sis_priv->pci_dev,
1510 skb->data, skb->len, PCI_DMA_TODEVICE);
1511 sis_priv->tx_ring[entry].cmdsts = (OWN | skb->len);
1512 outl(TxENA | inl(ioaddr + cr), ioaddr + cr);
1514 sis_priv->cur_tx ++;
1515 index_cur_tx = sis_priv->cur_tx;
1516 index_dirty_tx = sis_priv->dirty_tx;
1518 for (count_dirty_tx = 0; index_cur_tx != index_dirty_tx; index_dirty_tx++)
1521 if (index_cur_tx == index_dirty_tx) {
1522 /* dirty_tx is met in the cycle of cur_tx, buffer full */
1523 sis_priv->tx_full = 1;
1524 netif_stop_queue(net_dev);
1525 } else if (count_dirty_tx < NUM_TX_DESC) {
1526 /* Typical path, tell upper layer that more transmission is possible */
1527 netif_start_queue(net_dev);
1529 /* buffer full, tell upper layer no more transmission */
1530 sis_priv->tx_full = 1;
1531 netif_stop_queue(net_dev);
1534 spin_unlock_irqrestore(&sis_priv->lock, flags);
1536 net_dev->trans_start = jiffies;
1538 if (sis900_debug > 3)
1539 printk(KERN_INFO "%s: Queued Tx packet at %p size %d "
1541 net_dev->name, skb->data, (int)skb->len, entry);
1547 * sis900_interrupt - sis900 interrupt handler
1548 * @irq: the irq number
1549 * @dev_instance: the client data object
1550 * @regs: snapshot of processor context
1552 * The interrupt handler does all of the Rx thread work,
1553 * and cleans up after the Tx thread
1556 static irqreturn_t sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1558 struct net_device *net_dev = dev_instance;
1559 struct sis900_private *sis_priv = net_dev->priv;
1560 int boguscnt = max_interrupt_work;
1561 long ioaddr = net_dev->base_addr;
1563 unsigned int handled = 0;
1565 spin_lock (&sis_priv->lock);
1568 status = inl(ioaddr + isr);
1570 if ((status & (HIBERR|TxURN|TxERR|TxIDLE|RxORN|RxERR|RxOK)) == 0)
1571 /* nothing intresting happened */
1575 /* why dow't we break after Tx/Rx case ?? keyword: full-duplex */
1576 if (status & (RxORN | RxERR | RxOK))
1580 if (status & (TxURN | TxERR | TxIDLE))
1582 sis900_finish_xmit(net_dev);
1584 /* something strange happened !!! */
1585 if (status & HIBERR) {
1586 printk(KERN_INFO "%s: Abnormal interrupt,"
1587 "status %#8.8x.\n", net_dev->name, status);
1590 if (--boguscnt < 0) {
1591 printk(KERN_INFO "%s: Too much work at interrupt, "
1592 "interrupt status = %#8.8x.\n",
1593 net_dev->name, status);
1598 if (sis900_debug > 3)
1599 printk(KERN_INFO "%s: exiting interrupt, "
1600 "interrupt status = 0x%#8.8x.\n",
1601 net_dev->name, inl(ioaddr + isr));
1603 spin_unlock (&sis_priv->lock);
1604 return IRQ_RETVAL(handled);
1608 * sis900_rx - sis900 receive routine
1609 * @net_dev: the net device which receives data
1611 * Process receive interrupt events,
1612 * put buffer to higher layer and refill buffer pool
1613 * Note: This fucntion is called by interrupt handler,
1614 * don't do "too much" work here
1617 static int sis900_rx(struct net_device *net_dev)
1619 struct sis900_private *sis_priv = net_dev->priv;
1620 long ioaddr = net_dev->base_addr;
1621 unsigned int entry = sis_priv->cur_rx % NUM_RX_DESC;
1622 u32 rx_status = sis_priv->rx_ring[entry].cmdsts;
1624 if (sis900_debug > 3)
1625 printk(KERN_INFO "sis900_rx, cur_rx:%4.4d, dirty_rx:%4.4d "
1627 sis_priv->cur_rx, sis_priv->dirty_rx, rx_status);
1629 while (rx_status & OWN) {
1630 unsigned int rx_size;
1632 rx_size = (rx_status & DSIZE) - CRC_SIZE;
1634 if (rx_status & (ABORT|OVERRUN|TOOLONG|RUNT|RXISERR|CRCERR|FAERR)) {
1635 /* corrupted packet received */
1636 if (sis900_debug > 3)
1637 printk(KERN_INFO "%s: Corrupted packet "
1638 "received, buffer status = 0x%8.8x.\n",
1639 net_dev->name, rx_status);
1640 sis_priv->stats.rx_errors++;
1641 if (rx_status & OVERRUN)
1642 sis_priv->stats.rx_over_errors++;
1643 if (rx_status & (TOOLONG|RUNT))
1644 sis_priv->stats.rx_length_errors++;
1645 if (rx_status & (RXISERR | FAERR))
1646 sis_priv->stats.rx_frame_errors++;
1647 if (rx_status & CRCERR)
1648 sis_priv->stats.rx_crc_errors++;
1649 /* reset buffer descriptor state */
1650 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1652 struct sk_buff * skb;
1654 /* This situation should never happen, but due to
1655 some unknow bugs, it is possible that
1656 we are working on NULL sk_buff :-( */
1657 if (sis_priv->rx_skbuff[entry] == NULL) {
1658 printk(KERN_INFO "%s: NULL pointer "
1659 "encountered in Rx ring, skipping\n",
1664 pci_unmap_single(sis_priv->pci_dev,
1665 sis_priv->rx_ring[entry].bufptr, RX_BUF_SIZE,
1666 PCI_DMA_FROMDEVICE);
1667 /* give the socket buffer to upper layers */
1668 skb = sis_priv->rx_skbuff[entry];
1669 skb_put(skb, rx_size);
1670 skb->protocol = eth_type_trans(skb, net_dev);
1673 /* some network statistics */
1674 if ((rx_status & BCAST) == MCAST)
1675 sis_priv->stats.multicast++;
1676 net_dev->last_rx = jiffies;
1677 sis_priv->stats.rx_bytes += rx_size;
1678 sis_priv->stats.rx_packets++;
1680 /* refill the Rx buffer, what if there is not enought memory for
1681 new socket buffer ?? */
1682 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1683 /* not enough memory for skbuff, this makes a "hole"
1684 on the buffer ring, it is not clear how the
1685 hardware will react to this kind of degenerated
1687 printk(KERN_INFO "%s: Memory squeeze,"
1688 "deferring packet.\n",
1690 sis_priv->rx_skbuff[entry] = NULL;
1691 /* reset buffer descriptor state */
1692 sis_priv->rx_ring[entry].cmdsts = 0;
1693 sis_priv->rx_ring[entry].bufptr = 0;
1694 sis_priv->stats.rx_dropped++;
1698 sis_priv->rx_skbuff[entry] = skb;
1699 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1700 sis_priv->rx_ring[entry].bufptr =
1701 pci_map_single(sis_priv->pci_dev, skb->tail,
1702 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1703 sis_priv->dirty_rx++;
1706 entry = sis_priv->cur_rx % NUM_RX_DESC;
1707 rx_status = sis_priv->rx_ring[entry].cmdsts;
1710 /* refill the Rx buffer, what if the rate of refilling is slower than
1712 for (;sis_priv->cur_rx - sis_priv->dirty_rx > 0; sis_priv->dirty_rx++) {
1713 struct sk_buff *skb;
1715 entry = sis_priv->dirty_rx % NUM_RX_DESC;
1717 if (sis_priv->rx_skbuff[entry] == NULL) {
1718 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1719 /* not enough memory for skbuff, this makes a "hole"
1720 on the buffer ring, it is not clear how the
1721 hardware will react to this kind of degenerated
1723 printk(KERN_INFO "%s: Memory squeeze,"
1724 "deferring packet.\n",
1726 sis_priv->stats.rx_dropped++;
1730 sis_priv->rx_skbuff[entry] = skb;
1731 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1732 sis_priv->rx_ring[entry].bufptr =
1733 pci_map_single(sis_priv->pci_dev, skb->tail,
1734 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1737 /* re-enable the potentially idle receive state matchine */
1738 outl(RxENA | inl(ioaddr + cr), ioaddr + cr );
1744 * sis900_finish_xmit - finish up transmission of packets
1745 * @net_dev: the net device to be transmitted on
1747 * Check for error condition and free socket buffer etc
1748 * schedule for more transmission as needed
1749 * Note: This fucntion is called by interrupt handler,
1750 * don't do "too much" work here
1753 static void sis900_finish_xmit (struct net_device *net_dev)
1755 struct sis900_private *sis_priv = net_dev->priv;
1757 for (; sis_priv->dirty_tx != sis_priv->cur_tx; sis_priv->dirty_tx++) {
1758 struct sk_buff *skb;
1762 entry = sis_priv->dirty_tx % NUM_TX_DESC;
1763 tx_status = sis_priv->tx_ring[entry].cmdsts;
1765 if (tx_status & OWN) {
1766 /* The packet is not transmitted yet (owned by hardware) !
1767 Note: the interrupt is generated only when Tx Machine
1768 is idle, so this is an almost impossible case */
1772 if (tx_status & (ABORT | UNDERRUN | OWCOLL)) {
1773 /* packet unsuccessfully transmitted */
1774 if (sis900_debug > 3)
1775 printk(KERN_INFO "%s: Transmit "
1776 "error, Tx status %8.8x.\n",
1777 net_dev->name, tx_status);
1778 sis_priv->stats.tx_errors++;
1779 if (tx_status & UNDERRUN)
1780 sis_priv->stats.tx_fifo_errors++;
1781 if (tx_status & ABORT)
1782 sis_priv->stats.tx_aborted_errors++;
1783 if (tx_status & NOCARRIER)
1784 sis_priv->stats.tx_carrier_errors++;
1785 if (tx_status & OWCOLL)
1786 sis_priv->stats.tx_window_errors++;
1788 /* packet successfully transmitted */
1789 sis_priv->stats.collisions += (tx_status & COLCNT) >> 16;
1790 sis_priv->stats.tx_bytes += tx_status & DSIZE;
1791 sis_priv->stats.tx_packets++;
1793 /* Free the original skb. */
1794 skb = sis_priv->tx_skbuff[entry];
1795 pci_unmap_single(sis_priv->pci_dev,
1796 sis_priv->tx_ring[entry].bufptr, skb->len,
1798 dev_kfree_skb_irq(skb);
1799 sis_priv->tx_skbuff[entry] = NULL;
1800 sis_priv->tx_ring[entry].bufptr = 0;
1801 sis_priv->tx_ring[entry].cmdsts = 0;
1804 if (sis_priv->tx_full && netif_queue_stopped(net_dev) &&
1805 sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC - 4) {
1806 /* The ring is no longer full, clear tx_full and schedule more transmission
1807 by netif_wake_queue(net_dev) */
1808 sis_priv->tx_full = 0;
1809 netif_wake_queue (net_dev);
1814 * sis900_close - close sis900 device
1815 * @net_dev: the net device to be closed
1817 * Disable interrupts, stop the Tx and Rx Status Machine
1818 * free Tx and RX socket buffer
1822 sis900_close(struct net_device *net_dev)
1824 long ioaddr = net_dev->base_addr;
1825 struct sis900_private *sis_priv = net_dev->priv;
1826 struct sk_buff *skb;
1829 netif_stop_queue(net_dev);
1831 /* Disable interrupts by clearing the interrupt mask. */
1832 outl(0x0000, ioaddr + imr);
1833 outl(0x0000, ioaddr + ier);
1835 /* Stop the chip's Tx and Rx Status Machine */
1836 outl(RxDIS | TxDIS | inl(ioaddr + cr), ioaddr + cr);
1838 del_timer(&sis_priv->timer);
1840 free_irq(net_dev->irq, net_dev);
1842 /* Free Tx and RX skbuff */
1843 for (i = 0; i < NUM_RX_DESC; i++) {
1844 skb = sis_priv->rx_skbuff[i];
1846 pci_unmap_single(sis_priv->pci_dev,
1847 sis_priv->rx_ring[i].bufptr,
1848 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1850 sis_priv->rx_skbuff[i] = 0;
1853 for (i = 0; i < NUM_TX_DESC; i++) {
1854 skb = sis_priv->tx_skbuff[i];
1856 pci_unmap_single(sis_priv->pci_dev,
1857 sis_priv->tx_ring[i].bufptr, skb->len,
1860 sis_priv->tx_skbuff[i] = 0;
1864 /* Green! Put the chip in low-power mode. */
1870 * sis900_get_drvinfo - Return information about driver
1871 * @net_dev: the net device to probe
1872 * @info: container for info returned
1874 * Process ethtool command such as "ehtool -i" to show information
1877 static void sis900_get_drvinfo(struct net_device *net_dev,
1878 struct ethtool_drvinfo *info)
1880 struct sis900_private *sis_priv = net_dev->priv;
1882 strcpy (info->driver, SIS900_MODULE_NAME);
1883 strcpy (info->version, SIS900_DRV_VERSION);
1884 strcpy (info->bus_info, pci_name(sis_priv->pci_dev));
1887 static struct ethtool_ops sis900_ethtool_ops = {
1888 .get_drvinfo = sis900_get_drvinfo,
1892 * mii_ioctl - process MII i/o control command
1893 * @net_dev: the net device to command for
1894 * @rq: parameter for command
1895 * @cmd: the i/o command
1897 * Process MII command like read/write MII register
1900 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
1902 struct sis900_private *sis_priv = net_dev->priv;
1903 struct mii_ioctl_data *data = if_mii(rq);
1906 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1907 data->phy_id = sis_priv->mii->phy_addr;
1910 case SIOCGMIIREG: /* Read MII PHY register. */
1911 data->val_out = mdio_read(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
1914 case SIOCSMIIREG: /* Write MII PHY register. */
1915 if (!capable(CAP_NET_ADMIN))
1917 mdio_write(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
1925 * sis900_get_stats - Get sis900 read/write statistics
1926 * @net_dev: the net device to get statistics for
1928 * get tx/rx statistics for sis900
1931 static struct net_device_stats *
1932 sis900_get_stats(struct net_device *net_dev)
1934 struct sis900_private *sis_priv = net_dev->priv;
1936 return &sis_priv->stats;
1940 * sis900_set_config - Set media type by net_device.set_config
1941 * @dev: the net device for media type change
1942 * @map: ifmap passed by ifconfig
1944 * Set media type to 10baseT, 100baseT or 0(for auto) by ifconfig
1945 * we support only port changes. All other runtime configuration
1946 * changes will be ignored
1949 static int sis900_set_config(struct net_device *dev, struct ifmap *map)
1951 struct sis900_private *sis_priv = dev->priv;
1952 struct mii_phy *mii_phy = sis_priv->mii;
1956 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1957 /* we switch on the ifmap->port field. I couldn't find anything
1958 like a definition or standard for the values of that field.
1959 I think the meaning of those values is device specific. But
1960 since I would like to change the media type via the ifconfig
1961 command I use the definition from linux/netdevice.h
1962 (which seems to be different from the ifport(pcmcia) definition)
1965 case IF_PORT_UNKNOWN: /* use auto here */
1966 dev->if_port = map->port;
1967 /* we are going to change the media type, so the Link will
1968 be temporary down and we need to reflect that here. When
1969 the Link comes up again, it will be sensed by the sis_timer
1970 procedure, which also does all the rest for us */
1971 netif_carrier_off(dev);
1973 /* read current state */
1974 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1976 /* enable auto negotiation and reset the negotioation
1977 (I don't really know what the auto negatiotiation reset
1978 really means, but it sounds for me right to do one here)*/
1979 mdio_write(dev, mii_phy->phy_addr,
1980 MII_CONTROL, status | MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
1984 case IF_PORT_10BASET: /* 10BaseT */
1985 dev->if_port = map->port;
1987 /* we are going to change the media type, so the Link will
1988 be temporary down and we need to reflect that here. When
1989 the Link comes up again, it will be sensed by the sis_timer
1990 procedure, which also does all the rest for us */
1991 netif_carrier_off(dev);
1993 /* set Speed to 10Mbps */
1994 /* read current state */
1995 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1997 /* disable auto negotiation and force 10MBit mode*/
1998 mdio_write(dev, mii_phy->phy_addr,
1999 MII_CONTROL, status & ~(MII_CNTL_SPEED | MII_CNTL_AUTO));
2002 case IF_PORT_100BASET: /* 100BaseT */
2003 case IF_PORT_100BASETX: /* 100BaseTx */
2004 dev->if_port = map->port;
2006 /* we are going to change the media type, so the Link will
2007 be temporary down and we need to reflect that here. When
2008 the Link comes up again, it will be sensed by the sis_timer
2009 procedure, which also does all the rest for us */
2010 netif_carrier_off(dev);
2012 /* set Speed to 100Mbps */
2013 /* disable auto negotiation and enable 100MBit Mode */
2014 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
2015 mdio_write(dev, mii_phy->phy_addr,
2016 MII_CONTROL, (status & ~MII_CNTL_SPEED) | MII_CNTL_SPEED);
2020 case IF_PORT_10BASE2: /* 10Base2 */
2021 case IF_PORT_AUI: /* AUI */
2022 case IF_PORT_100BASEFX: /* 100BaseFx */
2023 /* These Modes are not supported (are they?)*/
2024 printk(KERN_INFO "Not supported");
2029 printk(KERN_INFO "Invalid");
2037 * sis900_mcast_bitnr - compute hashtable index
2038 * @addr: multicast address
2039 * @revision: revision id of chip
2041 * SiS 900 uses the most sigificant 7 bits to index a 128 bits multicast
2042 * hash table, which makes this function a little bit different from other drivers
2043 * SiS 900 B0 & 635 M/B uses the most significat 8 bits to index 256 bits
2044 * multicast hash table.
2047 static inline u16 sis900_mcast_bitnr(u8 *addr, u8 revision)
2050 u32 crc = ether_crc(6, addr);
2052 /* leave 8 or 7 most siginifant bits */
2053 if ((revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV))
2054 return ((int)(crc >> 24));
2056 return ((int)(crc >> 25));
2060 * set_rx_mode - Set SiS900 receive mode
2061 * @net_dev: the net device to be set
2063 * Set SiS900 receive mode for promiscuous, multicast, or broadcast mode.
2064 * And set the appropriate multicast filter.
2065 * Multicast hash table changes from 128 to 256 bits for 635M/B & 900B0.
2068 static void set_rx_mode(struct net_device *net_dev)
2070 long ioaddr = net_dev->base_addr;
2071 struct sis900_private * sis_priv = net_dev->priv;
2072 u16 mc_filter[16] = {0}; /* 256/128 bits multicast hash table */
2073 int i, table_entries;
2077 /* 635 Hash Table entires = 256(2^16) */
2078 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
2079 if((revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV))
2084 if (net_dev->flags & IFF_PROMISC) {
2085 /* Accept any kinds of packets */
2086 rx_mode = RFPromiscuous;
2087 for (i = 0; i < table_entries; i++)
2088 mc_filter[i] = 0xffff;
2089 } else if ((net_dev->mc_count > multicast_filter_limit) ||
2090 (net_dev->flags & IFF_ALLMULTI)) {
2091 /* too many multicast addresses or accept all multicast packet */
2092 rx_mode = RFAAB | RFAAM;
2093 for (i = 0; i < table_entries; i++)
2094 mc_filter[i] = 0xffff;
2096 /* Accept Broadcast packet, destination address matchs our MAC address,
2097 use Receive Filter to reject unwanted MCAST packet */
2098 struct dev_mc_list *mclist;
2100 for (i = 0, mclist = net_dev->mc_list; mclist && i < net_dev->mc_count;
2101 i++, mclist = mclist->next) {
2102 unsigned int bit_nr =
2103 sis900_mcast_bitnr(mclist->dmi_addr, revision);
2104 mc_filter[bit_nr >> 4] |= (1 << (bit_nr & 0xf));
2108 /* update Multicast Hash Table in Receive Filter */
2109 for (i = 0; i < table_entries; i++) {
2110 /* why plus 0x04 ??, That makes the correct value for hash table. */
2111 outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
2112 outl(mc_filter[i], ioaddr + rfdr);
2115 outl(RFEN | rx_mode, ioaddr + rfcr);
2117 /* sis900 is capatable of looping back packet at MAC level for debugging purpose */
2118 if (net_dev->flags & IFF_LOOPBACK) {
2120 /* We must disable Tx/Rx before setting loopback mode */
2121 cr_saved = inl(ioaddr + cr);
2122 outl(cr_saved | TxDIS | RxDIS, ioaddr + cr);
2123 /* enable loopback */
2124 outl(inl(ioaddr + txcfg) | TxMLB, ioaddr + txcfg);
2125 outl(inl(ioaddr + rxcfg) | RxATX, ioaddr + rxcfg);
2127 outl(cr_saved, ioaddr + cr);
2134 * sis900_reset - Reset sis900 MAC
2135 * @net_dev: the net device to reset
2137 * reset sis900 MAC and wait until finished
2138 * reset through command register
2139 * change backoff algorithm for 900B0 & 635 M/B
2142 static void sis900_reset(struct net_device *net_dev)
2144 struct sis900_private * sis_priv = net_dev->priv;
2145 long ioaddr = net_dev->base_addr;
2147 u32 status = TxRCMP | RxRCMP;
2150 outl(0, ioaddr + ier);
2151 outl(0, ioaddr + imr);
2152 outl(0, ioaddr + rfcr);
2154 outl(RxRESET | TxRESET | RESET | inl(ioaddr + cr), ioaddr + cr);
2156 /* Check that the chip has finished the reset. */
2157 while (status && (i++ < 1000)) {
2158 status ^= (inl(isr + ioaddr) & status);
2161 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
2162 if( (revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV) )
2163 outl(PESEL | RND_CNT, ioaddr + cfg);
2165 outl(PESEL, ioaddr + cfg);
2169 * sis900_remove - Remove sis900 device
2170 * @pci_dev: the pci device to be removed
2172 * remove and release SiS900 net device
2175 static void __devexit sis900_remove(struct pci_dev *pci_dev)
2177 struct net_device *net_dev = pci_get_drvdata(pci_dev);
2178 struct sis900_private * sis_priv = net_dev->priv;
2179 struct mii_phy *phy = NULL;
2181 while (sis_priv->first_mii) {
2182 phy = sis_priv->first_mii;
2183 sis_priv->first_mii = phy->next;
2187 pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
2188 sis_priv->rx_ring_dma);
2189 pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
2190 sis_priv->tx_ring_dma);
2191 unregister_netdev(net_dev);
2192 free_netdev(net_dev);
2193 pci_release_regions(pci_dev);
2194 pci_set_drvdata(pci_dev, NULL);
2199 static int sis900_suspend(struct pci_dev *pci_dev, u32 state)
2201 struct net_device *net_dev = pci_get_drvdata(pci_dev);
2202 struct sis900_private *sis_priv = net_dev->priv;
2203 long ioaddr = net_dev->base_addr;
2205 if(!netif_running(net_dev))
2208 netif_stop_queue(net_dev);
2209 netif_device_detach(net_dev);
2211 /* Stop the chip's Tx and Rx Status Machine */
2212 outl(RxDIS | TxDIS | inl(ioaddr + cr), ioaddr + cr);
2214 pci_set_power_state(pci_dev, 3);
2215 pci_save_state(pci_dev, sis_priv->pci_state);
2220 static int sis900_resume(struct pci_dev *pci_dev)
2222 struct net_device *net_dev = pci_get_drvdata(pci_dev);
2223 struct sis900_private *sis_priv = net_dev->priv;
2224 long ioaddr = net_dev->base_addr;
2226 if(!netif_running(net_dev))
2228 pci_restore_state(pci_dev, sis_priv->pci_state);
2229 pci_set_power_state(pci_dev, 0);
2231 sis900_init_rxfilter(net_dev);
2233 sis900_init_tx_ring(net_dev);
2234 sis900_init_rx_ring(net_dev);
2236 set_rx_mode(net_dev);
2238 netif_device_attach(net_dev);
2239 netif_start_queue(net_dev);
2241 /* Workaround for EDB */
2242 sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
2244 /* Enable all known interrupts by setting the interrupt mask. */
2245 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
2246 outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
2247 outl(IE, ioaddr + ier);
2249 sis900_check_mode(net_dev, sis_priv->mii);
2253 #endif /* CONFIG_PM */
2255 static struct pci_driver sis900_pci_driver = {
2256 .name = SIS900_MODULE_NAME,
2257 .id_table = sis900_pci_tbl,
2258 .probe = sis900_probe,
2259 .remove = __devexit_p(sis900_remove),
2261 .suspend = sis900_suspend,
2262 .resume = sis900_resume,
2263 #endif /* CONFIG_PM */
2266 static int __init sis900_init_module(void)
2268 /* when a module, this is printed whether or not devices are found in probe */
2273 return pci_module_init(&sis900_pci_driver);
2276 static void __exit sis900_cleanup_module(void)
2278 pci_unregister_driver(&sis900_pci_driver);
2281 module_init(sis900_init_module);
2282 module_exit(sis900_cleanup_module);