- David Miller: sparc and net updates. Fix merge_segments.
[davej-history.git] / drivers / net / sis900.c
blob72949ff74fa2608214e970122f9c7330056f3d26
1 /* sis900.c: A SiS 900/7016 PCI Fast Ethernet driver for Linux.
2 Copyright 1999 Silicon Integrated System Corporation
3 Revision: 1.07.06 Nov. 7 2000
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 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.
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
21 Rev 1.07.06 Nov. 7 2000 Jeff Garzik <jgarzik@mandrakesoft.com> some bug fix and cleaning
22 Rev 1.07.05 Nov. 6 2000 metapirat<metapirat@gmx.de> contribute media type select by ifconfig
23 Rev 1.07.04 Sep. 6 2000 Lei-Chun Chang added ICS1893 PHY support
24 Rev 1.07.03 Aug. 24 2000 Lei-Chun Chang (lcchang@sis.com.tw) modified 630E eqaulizer workaroung rule
25 Rev 1.07.01 Aug. 08 2000 Ollie Lho minor update for SiS 630E and SiS 630E A1
26 Rev 1.07 Mar. 07 2000 Ollie Lho bug fix in Rx buffer ring
27 Rev 1.06.04 Feb. 11 2000 Jeff Garzik <jgarzik@mandrakesoft.com> softnet and init for kernel 2.4
28 Rev 1.06.03 Dec. 23 1999 Ollie Lho Third release
29 Rev 1.06.02 Nov. 23 1999 Ollie Lho bug in mac probing fixed
30 Rev 1.06.01 Nov. 16 1999 Ollie Lho CRC calculation provide by Joseph Zbiciak (im14u2c@primenet.com)
31 Rev 1.06 Nov. 4 1999 Ollie Lho (ollie@sis.com.tw) Second release
32 Rev 1.05.05 Oct. 29 1999 Ollie Lho (ollie@sis.com.tw) Single buffer Tx/Rx
33 Chin-Shan Li (lcs@sis.com.tw) Added AMD Am79c901 HomePNA PHY support
34 Rev 1.05 Aug. 7 1999 Jim Huang (cmhuang@sis.com.tw) Initial release
37 #include <linux/module.h>
38 #include <linux/version.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/errno.h>
44 #include <linux/ioport.h>
45 #include <linux/malloc.h>
46 #include <linux/interrupt.h>
47 #include <linux/pci.h>
48 #include <linux/netdevice.h>
49 #include <linux/init.h>
51 #include <linux/etherdevice.h>
52 #include <linux/skbuff.h>
53 #include <asm/processor.h> /* Processor type for cache alignment. */
54 #include <asm/bitops.h>
55 #include <asm/io.h>
56 #include <linux/delay.h>
58 #include "sis900.h"
60 static const char *version =
61 "sis900.c: v1.07.06 11/07/2000\n";
63 static int max_interrupt_work = 20;
64 static int multicast_filter_limit = 128;
66 #define sis900_debug debug
67 static int sis900_debug;
69 /* Time in jiffies before concluding the transmitter is hung. */
70 #define TX_TIMEOUT (4*HZ)
71 /* SiS 900 is capable of 32 bits BM DMA */
72 #define SIS900_DMA_MASK 0xffffffff
74 enum {
75 SIS_900 = 0,
76 SIS_7016
78 static char * card_names[] = {
79 "SiS 900 PCI Fast Ethernet",
80 "SiS 7016 PCI Fast Ethernet"
82 static struct pci_device_id sis900_pci_tbl [] __devinitdata = {
83 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_900,
84 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_900},
85 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7016,
86 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7016},
87 {0,}
89 MODULE_DEVICE_TABLE (pci, sis900_pci_tbl);
91 static void sis900_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex);
92 static void amd79c901_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex);
93 static void ics1893_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex);
95 static struct mii_chip_info {
96 const char * name;
97 u16 phy_id0;
98 u16 phy_id1;
99 void (*read_mode) (struct net_device *net_dev, int phy_addr, int *speed, int *duplex);
100 } mii_chip_table[] = {
101 {"SiS 900 Internal MII PHY", 0x001d, 0x8000, sis900_read_mode},
102 {"SiS 7014 Physical Layer Solution", 0x0016, 0xf830,sis900_read_mode},
103 {"AMD 79C901 10BASE-T PHY", 0x0000, 0x35b9, amd79c901_read_mode},
104 {"AMD 79C901 HomePNA PHY", 0x0000, 0x35c8, amd79c901_read_mode},
105 {"ICS 1893 Integrated PHYceiver" , 0x0015, 0xf441,ics1893_read_mode},
106 {0,},
109 struct mii_phy {
110 struct mii_phy * next;
111 struct mii_chip_info * chip_info;
112 int phy_addr;
113 u16 status;
116 typedef struct _BufferDesc {
117 u32 link;
118 u32 cmdsts;
119 u32 bufptr;
120 } BufferDesc;
122 struct sis900_private {
123 struct net_device_stats stats;
124 struct pci_dev * pci_dev;
126 spinlock_t lock;
128 struct mii_phy * mii;
129 unsigned int cur_phy;
131 struct timer_list timer; /* Link status detection timer. */
133 unsigned int cur_rx, dirty_rx; /* producer/comsumer pointers for Tx/Rx ring */
134 unsigned int cur_tx, dirty_tx;
136 /* The saved address of a sent/receive-in-place packet buffer */
137 struct sk_buff *tx_skbuff[NUM_TX_DESC];
138 struct sk_buff *rx_skbuff[NUM_RX_DESC];
139 BufferDesc tx_ring[NUM_TX_DESC];
140 BufferDesc rx_ring[NUM_RX_DESC];
142 unsigned int tx_full; /* The Tx queue is full. */
145 MODULE_AUTHOR("Jim Huang <cmhuang@sis.com.tw>, Ollie Lho <ollie@sis.com.tw>");
146 MODULE_DESCRIPTION("SiS 900 PCI Fast Ethernet driver");
147 MODULE_PARM(multicast_filter_limit, "i");
148 MODULE_PARM(max_interrupt_work, "i");
149 MODULE_PARM(debug, "i");
151 static int sis900_open(struct net_device *net_dev);
152 static int sis900_mii_probe (struct net_device * net_dev);
153 static void sis900_init_rxfilter (struct net_device * net_dev);
154 static u16 read_eeprom(long ioaddr, int location);
155 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location);
156 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int val);
157 static void sis900_timer(unsigned long data);
158 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy);
159 static void sis900_tx_timeout(struct net_device *net_dev);
160 static void sis900_init_tx_ring(struct net_device *net_dev);
161 static void sis900_init_rx_ring(struct net_device *net_dev);
162 static int sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev);
163 static int sis900_rx(struct net_device *net_dev);
164 static void sis900_finish_xmit (struct net_device *net_dev);
165 static void sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
166 static int sis900_close(struct net_device *net_dev);
167 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd);
168 static struct net_device_stats *sis900_get_stats(struct net_device *net_dev);
169 static u16 sis900_compute_hashtable_index(u8 *addr);
170 static void set_rx_mode(struct net_device *net_dev);
171 static void sis900_reset(struct net_device *net_dev);
172 static void sis630e_set_eq(struct net_device *net_dev);
173 static int sis900_set_config(struct net_device *dev, struct ifmap *map);
175 /* older SiS900 and friends, use EEPROM to store MAC address */
176 static int __devinit sis900_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
178 long ioaddr = pci_resource_start(pci_dev, 0);
179 u16 signature;
180 int i;
182 /* check to see if we have sane EEPROM */
183 signature = (u16) read_eeprom(ioaddr, EEPROMSignature);
184 if (signature == 0xffff || signature == 0x0000) {
185 printk (KERN_INFO "%s: Error EERPOM read %x\n",
186 net_dev->name, signature);
187 return 0;
190 /* get MAC address from EEPROM */
191 for (i = 0; i < 3; i++)
192 ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
194 return 1;
197 /* SiS630E model, use APC CMOS RAM to store MAC address */
198 static int __devinit sis630e_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
200 struct pci_dev *isa_bridge = NULL;
201 u8 reg;
202 int i;
204 if ((isa_bridge = pci_find_device(0x1039, 0x0008, isa_bridge)) == NULL) {
205 printk("%s: Can not find ISA bridge\n", net_dev->name);
206 return 0;
208 pci_read_config_byte(isa_bridge, 0x48, &reg);
209 pci_write_config_byte(isa_bridge, 0x48, reg | 0x40);
211 for (i = 0; i < 6; i++) {
212 outb(0x09 + i, 0x70);
213 ((u8 *)(net_dev->dev_addr))[i] = inb(0x71);
215 pci_write_config_byte(isa_bridge, 0x48, reg & ~0x40);
217 return 1;
220 static int __devinit sis900_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
222 struct sis900_private *sis_priv;
223 long ioaddr = pci_resource_start(pci_dev, 0);
224 struct net_device *net_dev;
225 int irq = pci_dev->irq;
226 int i, ret = 0;
227 u8 revision;
228 char *card_name = card_names[pci_id->driver_data];
230 if (!pci_dma_supported(pci_dev, SIS900_DMA_MASK)) {
231 printk(KERN_ERR "sis900.c: architecture does not support "
232 "32bit PCI busmaster DMA\n");
233 return -ENODEV;
236 /* setup various bits in PCI command register */
237 if (pci_enable_device (pci_dev))
238 return -ENODEV;
239 pci_set_master(pci_dev);
241 net_dev = init_etherdev(NULL, sizeof(struct sis900_private));
242 if (!net_dev)
243 return -ENOMEM;
244 SET_MODULE_OWNER(net_dev);
246 if (!request_region(ioaddr, SIS900_TOTAL_SIZE, net_dev->name)) {
247 printk(KERN_ERR "sis900.c: can't allocate I/O space at 0x%lX\n", ioaddr);
248 ret = -EBUSY;
249 goto err_out;
252 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
253 if (revision == SIS630E_REV || revision == SIS630EA1_REV)
254 ret = sis630e_get_mac_addr(pci_dev, net_dev);
255 else if (revision == SIS630S_REV)
256 ret = sis630e_get_mac_addr(pci_dev, net_dev);
257 else
258 ret = sis900_get_mac_addr(pci_dev, net_dev);
260 if (ret == 0) {
261 ret = -ENODEV;
262 goto err_out_region;
265 /* print some information about our NIC */
266 printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ", net_dev->name,
267 card_name, ioaddr, irq);
268 for (i = 0; i < 5; i++)
269 printk("%2.2x:", (u8)net_dev->dev_addr[i]);
270 printk("%2.2x.\n", net_dev->dev_addr[i]);
272 sis_priv = net_dev->priv;
274 /* We do a request_region() to register /proc/ioports info. */
275 net_dev->base_addr = ioaddr;
276 net_dev->irq = irq;
277 sis_priv->pci_dev = pci_dev;
278 spin_lock_init(&sis_priv->lock);
280 /* probe for mii transciver */
281 if (sis900_mii_probe(net_dev) == 0) {
282 ret = -ENODEV;
283 goto err_out_region;
286 pci_dev->driver_data = net_dev;
287 pci_dev->dma_mask = SIS900_DMA_MASK;
289 /* The SiS900-specific entries in the device structure. */
290 net_dev->open = &sis900_open;
291 net_dev->hard_start_xmit = &sis900_start_xmit;
292 net_dev->stop = &sis900_close;
293 net_dev->get_stats = &sis900_get_stats;
294 net_dev->set_config = &sis900_set_config;
295 net_dev->set_multicast_list = &set_rx_mode;
296 net_dev->do_ioctl = &mii_ioctl;
297 net_dev->tx_timeout = sis900_tx_timeout;
298 net_dev->watchdog_timeo = TX_TIMEOUT;
300 return 0;
302 err_out_region:
303 release_region(ioaddr, SIS900_TOTAL_SIZE);
304 err_out:
305 unregister_netdev(net_dev);
306 kfree(net_dev);
307 return ret;
310 static int __init sis900_mii_probe (struct net_device * net_dev)
312 struct sis900_private * sis_priv = (struct sis900_private *)net_dev->priv;
313 int phy_addr;
314 u8 revision;
316 sis_priv->mii = NULL;
318 /* search for total of 32 possible mii phy addresses */
319 for (phy_addr = 0; phy_addr < 32; phy_addr++) {
320 u16 mii_status;
321 u16 phy_id0, phy_id1;
322 int i;
324 mii_status = mdio_read(net_dev, phy_addr, MII_STATUS);
325 if (mii_status == 0xffff || mii_status == 0x0000)
326 /* the mii is not accessable, try next one */
327 continue;
329 phy_id0 = mdio_read(net_dev, phy_addr, MII_PHY_ID0);
330 phy_id1 = mdio_read(net_dev, phy_addr, MII_PHY_ID1);
332 /* search our mii table for the current mii */
333 for (i = 0; mii_chip_table[i].phy_id1; i++)
334 if (phy_id0 == mii_chip_table[i].phy_id0) {
335 struct mii_phy * mii_phy;
337 printk(KERN_INFO
338 "%s: %s transceiver found at address %d.\n",
339 net_dev->name, mii_chip_table[i].name,
340 phy_addr);
341 if ((mii_phy = kmalloc(sizeof(struct mii_phy), GFP_KERNEL)) != NULL) {
342 mii_phy->chip_info = mii_chip_table+i;
343 mii_phy->phy_addr = phy_addr;
344 mii_phy->status = mdio_read(net_dev, phy_addr,
345 MII_STATUS);
346 mii_phy->next = sis_priv->mii;
347 sis_priv->mii = mii_phy;
349 /* the current mii is on our mii_info_table,
350 try next address */
351 break;
355 if (sis_priv->mii == NULL) {
356 printk(KERN_INFO "%s: No MII transceivers found!\n",
357 net_dev->name);
358 return 0;
361 /* arbitrary choose that last PHY as current PHY */
362 sis_priv->cur_phy = sis_priv->mii->phy_addr;
363 printk(KERN_INFO "%s: Using %s as default\n", net_dev->name,
364 sis_priv->mii->chip_info->name);
366 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
367 if (revision == SIS630E_REV) {
368 /* SiS 630E has some bugs on default value of PHY registers */
369 mdio_write(net_dev, sis_priv->cur_phy, MII_ANADV, 0x05e1);
370 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG1, 0x22);
371 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG2, 0xff00);
372 mdio_write(net_dev, sis_priv->cur_phy, MII_MASK, 0xffc0);
373 mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, 0x1000);
376 if (sis_priv->mii->status & MII_STAT_LINK)
377 netif_carrier_on(net_dev);
378 else
379 netif_carrier_off(net_dev);
381 return 1;
384 /* Delay between EEPROM clock transitions. */
385 #define eeprom_delay() inl(ee_addr)
387 /* Read Serial EEPROM through EEPROM Access Register, Note that location is
388 in word (16 bits) unit */
389 static u16 read_eeprom(long ioaddr, int location)
391 int i;
392 u16 retval = 0;
393 long ee_addr = ioaddr + mear;
394 u32 read_cmd = location | EEread;
396 outl(0, ee_addr);
397 eeprom_delay();
398 outl(EECLK, ee_addr);
399 eeprom_delay();
401 /* Shift the read command (9) bits out. */
402 for (i = 8; i >= 0; i--) {
403 u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
404 outl(dataval, ee_addr);
405 eeprom_delay();
406 outl(dataval | EECLK, ee_addr);
407 eeprom_delay();
409 outb(EECS, ee_addr);
410 eeprom_delay();
412 /* read the 16-bits data in */
413 for (i = 16; i > 0; i--) {
414 outl(EECS, ee_addr);
415 eeprom_delay();
416 outl(EECS | EECLK, ee_addr);
417 eeprom_delay();
418 retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
419 eeprom_delay();
422 /* Terminate the EEPROM access. */
423 outl(0, ee_addr);
424 eeprom_delay();
425 outl(EECLK, ee_addr);
427 return (retval);
430 /* Read and write the MII management registers using software-generated
431 serial MDIO protocol. Note that the command bits and data bits are
432 send out seperately */
433 #define mdio_delay() inl(mdio_addr)
435 static void mdio_idle(long mdio_addr)
437 outl(MDIO | MDDIR, mdio_addr);
438 mdio_delay();
439 outl(MDIO | MDDIR | MDC, mdio_addr);
442 /* Syncronize the MII management interface by shifting 32 one bits out. */
443 static void mdio_reset(long mdio_addr)
445 int i;
447 for (i = 31; i >= 0; i--) {
448 outl(MDDIR | MDIO, mdio_addr);
449 mdio_delay();
450 outl(MDDIR | MDIO | MDC, mdio_addr);
451 mdio_delay();
453 return;
456 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location)
458 long mdio_addr = net_dev->base_addr + mear;
459 int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
460 u16 retval = 0;
461 int i;
463 mdio_reset(mdio_addr);
464 mdio_idle(mdio_addr);
466 for (i = 15; i >= 0; i--) {
467 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
468 outl(dataval, mdio_addr);
469 mdio_delay();
470 outl(dataval | MDC, mdio_addr);
471 mdio_delay();
474 /* Read the 16 data bits. */
475 for (i = 16; i > 0; i--) {
476 outl(0, mdio_addr);
477 mdio_delay();
478 retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
479 outl(MDC, mdio_addr);
480 mdio_delay();
482 outl(0x00, mdio_addr);
484 return retval;
487 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int value)
489 long mdio_addr = net_dev->base_addr + mear;
490 int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
491 int i;
493 mdio_reset(mdio_addr);
494 mdio_idle(mdio_addr);
496 /* Shift the command bits out. */
497 for (i = 15; i >= 0; i--) {
498 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
499 outb(dataval, mdio_addr);
500 mdio_delay();
501 outb(dataval | MDC, mdio_addr);
502 mdio_delay();
504 mdio_delay();
506 /* Shift the value bits out. */
507 for (i = 15; i >= 0; i--) {
508 int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
509 outl(dataval, mdio_addr);
510 mdio_delay();
511 outl(dataval | MDC, mdio_addr);
512 mdio_delay();
514 mdio_delay();
516 /* Clear out extra bits. */
517 for (i = 2; i > 0; i--) {
518 outb(0, mdio_addr);
519 mdio_delay();
520 outb(MDC, mdio_addr);
521 mdio_delay();
523 outl(0x00, mdio_addr);
525 return;
528 static int
529 sis900_open(struct net_device *net_dev)
531 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
532 long ioaddr = net_dev->base_addr;
533 u8 revision;
534 int ret;
536 /* Soft reset the chip. */
537 sis900_reset(net_dev);
539 /* Equalizer workaroung Rule */
540 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
541 if (revision == SIS630E_REV || revision == SIS630EA1_REV)
542 sis630e_set_eq(net_dev);
544 ret = request_irq(net_dev->irq, &sis900_interrupt, SA_SHIRQ, net_dev->name, net_dev);
545 if (ret)
546 return ret;
548 sis900_init_rxfilter(net_dev);
550 sis900_init_tx_ring(net_dev);
551 sis900_init_rx_ring(net_dev);
553 set_rx_mode(net_dev);
555 netif_start_queue(net_dev);
557 /* Enable all known interrupts by setting the interrupt mask. */
558 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
559 outl(RxENA, ioaddr + cr);
560 outl(IE, ioaddr + ier);
562 sis900_check_mode(net_dev, sis_priv->mii);
564 /* Set the timer to switch to check for link beat and perhaps switch
565 to an alternate media type. */
566 init_timer(&sis_priv->timer);
567 sis_priv->timer.expires = jiffies + HZ;
568 sis_priv->timer.data = (unsigned long)net_dev;
569 sis_priv->timer.function = &sis900_timer;
570 add_timer(&sis_priv->timer);
572 return 0;
575 /* set receive filter address to our MAC address */
576 static void
577 sis900_init_rxfilter (struct net_device * net_dev)
579 long ioaddr = net_dev->base_addr;
580 u32 rfcrSave;
581 u32 i;
583 rfcrSave = inl(rfcr + ioaddr);
585 /* disable packet filtering before setting filter */
586 outl(rfcrSave & ~RFEN, rfcr);
588 /* load MAC addr to filter data register */
589 for (i = 0 ; i < 3 ; i++) {
590 u32 w;
592 w = (u32) *((u16 *)(net_dev->dev_addr)+i);
593 outl((i << RFADDR_shift), ioaddr + rfcr);
594 outl(w, ioaddr + rfdr);
596 if (sis900_debug > 2) {
597 printk(KERN_INFO "%s: Receive Filter Addrss[%d]=%x\n",
598 net_dev->name, i, inl(ioaddr + rfdr));
602 /* enable packet filitering */
603 outl(rfcrSave | RFEN, rfcr + ioaddr);
606 /* Initialize the Tx ring. */
607 static void
608 sis900_init_tx_ring(struct net_device *net_dev)
610 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
611 long ioaddr = net_dev->base_addr;
612 int i;
614 sis_priv->tx_full = 0;
615 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
617 for (i = 0; i < NUM_TX_DESC; i++) {
618 sis_priv->tx_skbuff[i] = NULL;
620 sis_priv->tx_ring[i].link = (u32) virt_to_bus(&sis_priv->tx_ring[i+1]);
621 sis_priv->tx_ring[i].cmdsts = 0;
622 sis_priv->tx_ring[i].bufptr = 0;
624 sis_priv->tx_ring[i-1].link = (u32) virt_to_bus(&sis_priv->tx_ring[0]);
626 /* load Transmit Descriptor Register */
627 outl(virt_to_bus(&sis_priv->tx_ring[0]), ioaddr + txdp);
628 if (sis900_debug > 2)
629 printk(KERN_INFO "%s: TX descriptor register loaded with: %8.8x\n",
630 net_dev->name, inl(ioaddr + txdp));
633 /* Initialize the Rx descriptor ring, pre-allocate recevie buffers */
634 static void
635 sis900_init_rx_ring(struct net_device *net_dev)
637 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
638 long ioaddr = net_dev->base_addr;
639 int i;
641 sis_priv->cur_rx = 0;
642 sis_priv->dirty_rx = 0;
644 /* init RX descriptor */
645 for (i = 0; i < NUM_RX_DESC; i++) {
646 sis_priv->rx_skbuff[i] = NULL;
648 sis_priv->rx_ring[i].link = (u32) virt_to_bus(&sis_priv->rx_ring[i+1]);
649 sis_priv->rx_ring[i].cmdsts = 0;
650 sis_priv->rx_ring[i].bufptr = 0;
652 sis_priv->rx_ring[i-1].link = (u32) virt_to_bus(&sis_priv->rx_ring[0]);
654 /* allocate sock buffers */
655 for (i = 0; i < NUM_RX_DESC; i++) {
656 struct sk_buff *skb;
658 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
659 /* not enough memory for skbuff, this makes a "hole"
660 on the buffer ring, it is not clear how the
661 hardware will react to this kind of degenerated
662 buffer */
663 break;
665 skb->dev = net_dev;
666 sis_priv->rx_skbuff[i] = skb;
667 sis_priv->rx_ring[i].cmdsts = RX_BUF_SIZE;
668 sis_priv->rx_ring[i].bufptr = virt_to_bus(skb->tail);
670 sis_priv->dirty_rx = (unsigned int) (i - NUM_RX_DESC);
672 /* load Receive Descriptor Register */
673 outl(virt_to_bus(&sis_priv->rx_ring[0]), ioaddr + rxdp);
674 if (sis900_debug > 2)
675 printk(KERN_INFO "%s: RX descriptor register loaded with: %8.8x\n",
676 net_dev->name, inl(ioaddr + rxdp));
679 /* 630E equalizer workaroung rule(Cyrus Huang 08/15)
680 PHY register 14h(Test)
681 Bit 14: 0 -- Automatically dectect (default)
682 1 -- Manually set Equalizer filter
683 Bit 13: 0 -- (Default)
684 1 -- Speed up convergence of equalizer setting
685 Bit 9 : 0 -- (Default)
686 1 -- Disable Baseline Wander
687 Bit 3~7 -- Equalizer filter setting
689 Link ON: Set Bit 9, 13 to 1, Bit 14 to 0
690 Then calculate equalizer value
691 Then set equalizer value, and set Bit 14 to 1, Bit 9 to 0
692 Link Off:Set Bit 13 to 1, Bit 14 to 0
694 Calculate Equalizer value:
695 When Link is ON and Bit 14 is 0, SIS900PHY will auto-dectect proper equalizer value.
696 When the equalizer is stable, this value is not a fixed value. It will be within
697 a small range(eg. 7~9). Then we get a minimum and a maximum value(eg. min=7, max=9)
698 0 <= max <= 4 --> set equalizer to max
699 5 <= max <= 14 --> set equalizer to max+1 or
700 set equalizer to max+2 if max == min
701 max >= 15 --> set equalizer to max+5 or
702 set equalizer to max+6 if max == min
704 static void sis630e_set_eq(struct net_device *net_dev)
706 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
707 u16 reg14h, eq_value, max_value=0, min_value=0;
708 int i, maxcount=10;
710 if (netif_carrier_ok(net_dev)) {
711 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
712 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (0x2200 | reg14h) & 0xBFFF);
713 for (i=0; i < maxcount; i++) {
714 eq_value=(0x00F8 & mdio_read(net_dev, sis_priv->cur_phy, MII_RESV)) >> 3;
715 max_value=(eq_value > max_value) ? eq_value : max_value;
716 min_value=(eq_value < min_value) ? eq_value : min_value;
718 if (max_value < 5)
719 eq_value=max_value;
720 else if (max_value >= 5 && max_value < 15)
721 eq_value=(max_value == min_value) ? max_value+2 : max_value+1;
722 else if (max_value >= 15)
723 eq_value=(max_value == min_value) ? max_value+6 : max_value+5;
724 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
725 reg14h=(reg14h & 0xFF07) | ((eq_value << 3) & 0x00F8);
726 reg14h=(reg14h | 0x6000) & 0xFDFF;
727 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, reg14h);
729 else {
730 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
731 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2000) & 0xBFFF);
733 return;
736 /* on each timer ticks we check two things, Link Status (ON/OFF) and
737 Link Mode (10/100/Full/Half)
739 static void sis900_timer(unsigned long data)
741 struct net_device *net_dev = (struct net_device *)data;
742 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
743 struct mii_phy *mii_phy = sis_priv->mii;
744 static int next_tick = 5*HZ;
745 u16 status;
746 u8 revision;
748 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
749 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
751 /* current mii phy is failed to link, try another one */
752 while (!(status & MII_STAT_LINK)) {
753 if (mii_phy->next == NULL) {
754 if (netif_carrier_ok(net_dev)) {
755 /* link stat change from ON to OFF */
756 next_tick = HZ;
757 netif_carrier_off(net_dev);
759 /* Equalizer workaroung Rule */
760 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
761 if (revision == SIS630E_REV || revision == SIS630EA1_REV)
762 sis630e_set_eq(net_dev);
764 printk(KERN_INFO "%s: Media Link Off\n",
765 net_dev->name);
767 sis_priv->timer.expires = jiffies + next_tick;
768 add_timer(&sis_priv->timer);
769 return;
771 mii_phy = mii_phy->next;
772 status = mdio_read(net_dev, mii_phy->phy_addr, MII_STATUS);
775 if (!netif_carrier_ok(net_dev)) {
776 /* link stat change forn OFF to ON, read and report link mode */
777 netif_carrier_on(net_dev);
778 next_tick = 5*HZ;
780 /* Equalizer workaroung Rule */
781 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
782 if (revision == SIS630E_REV || revision == SIS630EA1_REV)
783 sis630e_set_eq(net_dev);
785 /* change what cur_phy means */
786 if (mii_phy->phy_addr != sis_priv->cur_phy) {
787 printk(KERN_INFO "%s: Changing transceiver to %s\n",
788 net_dev->name, mii_phy->chip_info->name);
789 /* disable previous PHY */
790 status = mdio_read(net_dev, sis_priv->cur_phy, MII_CONTROL);
791 mdio_write(net_dev, sis_priv->cur_phy,
792 MII_CONTROL, status | MII_CNTL_ISOLATE);
793 /* enable next PHY */
794 status = mdio_read(net_dev, mii_phy->phy_addr, MII_CONTROL);
795 mdio_write(net_dev, mii_phy->phy_addr,
796 MII_CONTROL, status & ~MII_CNTL_ISOLATE);
797 sis_priv->cur_phy = mii_phy->phy_addr;
799 sis900_check_mode(net_dev, mii_phy);
802 sis_priv->timer.expires = jiffies + next_tick;
803 add_timer(&sis_priv->timer);
805 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy)
807 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
808 long ioaddr = net_dev->base_addr;
809 int speed, duplex;
810 u32 tx_flags = 0, rx_flags = 0;
812 mii_phy->chip_info->read_mode(net_dev, sis_priv->cur_phy, &speed, &duplex);
814 tx_flags = TxATP | (TX_DMA_BURST << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
815 rx_flags = RX_DMA_BURST << RxMXDMA_shift;
817 if (speed == HW_SPEED_HOME || speed == HW_SPEED_10_MBPS ) {
818 rx_flags |= (RxDRNT_10 << RxDRNT_shift);
819 tx_flags |= (TxDRNT_10 << TxDRNT_shift);
821 else {
822 rx_flags |= (RxDRNT_100 << RxDRNT_shift);
823 tx_flags |= (TxDRNT_100 << TxDRNT_shift);
826 if (duplex == FDX_CAPABLE_FULL_SELECTED) {
827 tx_flags |= (TxCSI | TxHBI);
828 rx_flags |= RxATX;
831 outl (tx_flags, ioaddr + txcfg);
832 outl (rx_flags, ioaddr + rxcfg);
834 static void sis900_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex)
836 int i = 0;
837 u32 status;
839 /* STSOUT register is Latched on Transition, read operation updates it */
840 while (i++ < 2)
841 status = mdio_read(net_dev, phy_addr, MII_STSOUT);
843 if (status & MII_STSOUT_SPD)
844 *speed = HW_SPEED_100_MBPS;
845 else
846 *speed = HW_SPEED_10_MBPS;
848 if (status & MII_STSOUT_DPLX)
849 *duplex = FDX_CAPABLE_FULL_SELECTED;
850 else
851 *duplex = FDX_CAPABLE_HALF_SELECTED;
853 if (status & MII_STSOUT_LINK_FAIL)
854 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
855 else
856 printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
857 net_dev->name,
858 *speed == HW_SPEED_100_MBPS ?
859 "100mbps" : "10mbps",
860 *duplex == FDX_CAPABLE_FULL_SELECTED ?
861 "full" : "half");
863 static void amd79c901_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex)
865 int i;
866 u16 status;
868 for (i = 0; i < 2; i++)
869 status = mdio_read(net_dev, phy_addr, MII_STATUS);
871 if (status & MII_STAT_CAN_AUTO) {
872 /* 10BASE-T PHY */
873 for (i = 0; i < 2; i++)
874 status = mdio_read(net_dev, phy_addr, MII_STATUS_SUMMARY);
875 if (status & MII_STSSUM_SPD)
876 *speed = HW_SPEED_100_MBPS;
877 else
878 *speed = HW_SPEED_10_MBPS;
879 if (status & MII_STSSUM_DPLX)
880 *duplex = FDX_CAPABLE_FULL_SELECTED;
881 else
882 *duplex = FDX_CAPABLE_HALF_SELECTED;
884 if (status & MII_STSSUM_LINK)
885 printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
886 net_dev->name,
887 *speed == HW_SPEED_100_MBPS ?
888 "100mbps" : "10mbps",
889 *duplex == FDX_CAPABLE_FULL_SELECTED ?
890 "full" : "half");
891 else
892 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
894 else {
895 /* HomePNA */
896 *speed = HW_SPEED_HOME;
897 *duplex = FDX_CAPABLE_HALF_SELECTED;
898 if (status & MII_STAT_LINK)
899 printk(KERN_INFO "%s: Media Link On 1mbps half-duplex \n",
900 net_dev->name);
901 else
902 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
905 /* ICS1893 PHY use Quick Poll Detailed Status Register to get its status */
906 static void ics1893_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex)
908 int i = 0;
909 u32 status;
911 /* MII_QPDSTS is Latched, read twice in succession will reflect the current state */
912 for (i = 0; i < 2; i++)
913 status = mdio_read(net_dev, phy_addr, MII_QPDSTS);
915 if (status & MII_STSICS_SPD)
916 *speed = HW_SPEED_100_MBPS;
917 else
918 *speed = HW_SPEED_10_MBPS;
920 if (status & MII_STSICS_DPLX)
921 *duplex = FDX_CAPABLE_FULL_SELECTED;
922 else
923 *duplex = FDX_CAPABLE_HALF_SELECTED;
925 if (status & MII_STSICS_LINKSTS)
926 printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
927 net_dev->name,
928 *speed == HW_SPEED_100_MBPS ?
929 "100mbps" : "10mbps",
930 *duplex == FDX_CAPABLE_FULL_SELECTED ?
931 "full" : "half");
932 else
933 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
936 static void sis900_tx_timeout(struct net_device *net_dev)
938 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
939 long ioaddr = net_dev->base_addr;
940 unsigned long flags;
941 int i;
943 printk(KERN_INFO "%s: Transmit timeout, status %8.8x %8.8x \n",
944 net_dev->name, inl(ioaddr + cr), inl(ioaddr + isr));
946 /* Disable interrupts by clearing the interrupt mask. */
947 outl(0x0000, ioaddr + imr);
949 /* use spinlock to prevent interrupt handler accessing buffer ring */
950 spin_lock_irqsave(&sis_priv->lock, flags);
952 /* discard unsent packets */
953 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
954 for (i = 0; i < NUM_TX_DESC; i++) {
955 if (sis_priv->tx_skbuff[i] != NULL) {
956 dev_kfree_skb(sis_priv->tx_skbuff[i]);
957 sis_priv->tx_skbuff[i] = 0;
958 sis_priv->tx_ring[i].cmdsts = 0;
959 sis_priv->tx_ring[i].bufptr = 0;
960 sis_priv->stats.tx_dropped++;
963 sis_priv->tx_full = 0;
964 netif_wake_queue(net_dev);
966 spin_unlock_irqrestore(&sis_priv->lock, flags);
968 net_dev->trans_start = jiffies;
970 /* FIXME: Should we restart the transmission thread here ?? */
971 outl(TxENA, ioaddr + cr);
973 /* Enable all known interrupts by setting the interrupt mask. */
974 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
975 return;
978 static int
979 sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
981 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
982 long ioaddr = net_dev->base_addr;
983 unsigned int entry;
984 unsigned long flags;
986 spin_lock_irqsave(&sis_priv->lock, flags);
988 /* Calculate the next Tx descriptor entry. */
989 entry = sis_priv->cur_tx % NUM_TX_DESC;
990 sis_priv->tx_skbuff[entry] = skb;
992 /* set the transmit buffer descriptor and enable Transmit State Machine */
993 sis_priv->tx_ring[entry].bufptr = virt_to_bus(skb->data);
994 sis_priv->tx_ring[entry].cmdsts = (OWN | skb->len);
995 outl(TxENA, ioaddr + cr);
997 if (++sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC) {
998 /* Typical path, tell upper layer that more transmission is possible */
999 netif_start_queue(net_dev);
1000 } else {
1001 /* buffer full, tell upper layer no more transmission */
1002 sis_priv->tx_full = 1;
1003 netif_stop_queue(net_dev);
1006 spin_unlock_irqrestore(&sis_priv->lock, flags);
1008 net_dev->trans_start = jiffies;
1010 if (sis900_debug > 3)
1011 printk(KERN_INFO "%s: Queued Tx packet at %p size %d "
1012 "to slot %d.\n",
1013 net_dev->name, skb->data, (int)skb->len, entry);
1015 return 0;
1018 /* The interrupt handler does all of the Rx thread work and cleans up
1019 after the Tx thread. */
1020 static void sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1022 struct net_device *net_dev = (struct net_device *)dev_instance;
1023 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1024 int boguscnt = max_interrupt_work;
1025 long ioaddr = net_dev->base_addr;
1026 u32 status;
1028 spin_lock (&sis_priv->lock);
1030 do {
1031 status = inl(ioaddr + isr);
1033 if ((status & (HIBERR|TxURN|TxERR|TxIDLE|RxORN|RxERR|RxOK)) == 0)
1034 /* nothing intresting happened */
1035 break;
1037 /* why dow't we break after Tx/Rx case ?? keyword: full-duplex */
1038 if (status & (RxORN | RxERR | RxOK))
1039 /* Rx interrupt */
1040 sis900_rx(net_dev);
1042 if (status & (TxURN | TxERR | TxIDLE))
1043 /* Tx interrupt */
1044 sis900_finish_xmit(net_dev);
1046 /* something strange happened !!! */
1047 if (status & HIBERR) {
1048 printk(KERN_INFO "%s: Abnormal interrupt,"
1049 "status %#8.8x.\n", net_dev->name, status);
1050 break;
1052 if (--boguscnt < 0) {
1053 printk(KERN_INFO "%s: Too much work at interrupt, "
1054 "interrupt status = %#8.8x.\n",
1055 net_dev->name, status);
1056 break;
1058 } while (1);
1060 if (sis900_debug > 3)
1061 printk(KERN_INFO "%s: exiting interrupt, "
1062 "interrupt status = 0x%#8.8x.\n",
1063 net_dev->name, inl(ioaddr + isr));
1065 spin_unlock (&sis_priv->lock);
1066 return;
1069 /* Process receive interrupt events, put buffer to higher layer and refill buffer pool
1070 Note: This fucntion is called by interrupt handler, don't do "too much" work here */
1071 static int sis900_rx(struct net_device *net_dev)
1073 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1074 long ioaddr = net_dev->base_addr;
1075 unsigned int entry = sis_priv->cur_rx % NUM_RX_DESC;
1076 u32 rx_status = sis_priv->rx_ring[entry].cmdsts;
1078 if (sis900_debug > 3)
1079 printk(KERN_INFO "sis900_rx, cur_rx:%4.4d, dirty_rx:%4.4d "
1080 "status:0x%8.8x\n",
1081 sis_priv->cur_rx, sis_priv->dirty_rx, rx_status);
1083 while (rx_status & OWN) {
1084 unsigned int rx_size;
1086 rx_size = (rx_status & DSIZE) - CRC_SIZE;
1088 if (rx_status & (ABORT|OVERRUN|TOOLONG|RUNT|RXISERR|CRCERR|FAERR)) {
1089 /* corrupted packet received */
1090 if (sis900_debug > 3)
1091 printk(KERN_INFO "%s: Corrupted packet "
1092 "received, buffer status = 0x%8.8x.\n",
1093 net_dev->name, rx_status);
1094 sis_priv->stats.rx_errors++;
1095 if (rx_status & OVERRUN)
1096 sis_priv->stats.rx_over_errors++;
1097 if (rx_status & (TOOLONG|RUNT))
1098 sis_priv->stats.rx_length_errors++;
1099 if (rx_status & (RXISERR | FAERR))
1100 sis_priv->stats.rx_frame_errors++;
1101 if (rx_status & CRCERR)
1102 sis_priv->stats.rx_crc_errors++;
1103 /* reset buffer descriptor state */
1104 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1105 } else {
1106 struct sk_buff * skb;
1108 /* This situation should never happen, but due to
1109 some unknow bugs, it is possible that
1110 we are working on NULL sk_buff :-( */
1111 if (sis_priv->rx_skbuff[entry] == NULL) {
1112 printk(KERN_INFO "%s: NULL pointer "
1113 "encountered in Rx ring, skipping\n",
1114 net_dev->name);
1115 break;
1118 /* gvie the socket buffer to upper layers */
1119 skb = sis_priv->rx_skbuff[entry];
1120 skb_put(skb, rx_size);
1121 skb->protocol = eth_type_trans(skb, net_dev);
1122 netif_rx(skb);
1124 /* some network statistics */
1125 if ((rx_status & BCAST) == MCAST)
1126 sis_priv->stats.multicast++;
1127 net_dev->last_rx = jiffies;
1128 sis_priv->stats.rx_bytes += rx_size;
1129 sis_priv->stats.rx_packets++;
1131 /* refill the Rx buffer, what if there is not enought memory for
1132 new socket buffer ?? */
1133 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1134 /* not enough memory for skbuff, this makes a "hole"
1135 on the buffer ring, it is not clear how the
1136 hardware will react to this kind of degenerated
1137 buffer */
1138 printk(KERN_INFO "%s: Memory squeeze,"
1139 "deferring packet.\n",
1140 net_dev->name);
1141 sis_priv->rx_skbuff[entry] = NULL;
1142 /* reset buffer descriptor state */
1143 sis_priv->rx_ring[entry].cmdsts = 0;
1144 sis_priv->rx_ring[entry].bufptr = 0;
1145 sis_priv->stats.rx_dropped++;
1146 break;
1148 skb->dev = net_dev;
1149 sis_priv->rx_skbuff[entry] = skb;
1150 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1151 sis_priv->rx_ring[entry].bufptr = virt_to_bus(skb->tail);
1152 sis_priv->dirty_rx++;
1154 sis_priv->cur_rx++;
1155 entry = sis_priv->cur_rx % NUM_RX_DESC;
1156 rx_status = sis_priv->rx_ring[entry].cmdsts;
1157 } // while
1159 /* refill the Rx buffer, what if the rate of refilling is slower than
1160 consuming ?? */
1161 for (;sis_priv->cur_rx - sis_priv->dirty_rx > 0; sis_priv->dirty_rx++) {
1162 struct sk_buff *skb;
1164 entry = sis_priv->dirty_rx % NUM_RX_DESC;
1166 if (sis_priv->rx_skbuff[entry] == NULL) {
1167 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1168 /* not enough memory for skbuff, this makes a "hole"
1169 on the buffer ring, it is not clear how the
1170 hardware will react to this kind of degenerated
1171 buffer */
1172 printk(KERN_INFO "%s: Memory squeeze,"
1173 "deferring packet.\n",
1174 net_dev->name);
1175 sis_priv->stats.rx_dropped++;
1176 break;
1178 skb->dev = net_dev;
1179 sis_priv->rx_skbuff[entry] = skb;
1180 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1181 sis_priv->rx_ring[entry].bufptr = virt_to_bus(skb->tail);
1184 /* re-enable the potentially idle receive state matchine */
1185 outl(RxENA , ioaddr + cr );
1187 return 0;
1190 /* finish up transmission of packets, check for error condition and free skbuff etc.
1191 Note: This fucntion is called by interrupt handler, don't do "too much" work here */
1192 static void sis900_finish_xmit (struct net_device *net_dev)
1194 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1196 for (; sis_priv->dirty_tx < sis_priv->cur_tx; sis_priv->dirty_tx++) {
1197 unsigned int entry;
1198 u32 tx_status;
1200 entry = sis_priv->dirty_tx % NUM_TX_DESC;
1201 tx_status = sis_priv->tx_ring[entry].cmdsts;
1203 if (tx_status & OWN) {
1204 /* The packet is not transmited yet (owned by hardware) !
1205 Note: the interrupt is generated only when Tx Machine
1206 is idle, so this is an almost impossible case */
1207 break;
1210 if (tx_status & (ABORT | UNDERRUN | OWCOLL)) {
1211 /* packet unsuccessfully transmited */
1212 if (sis900_debug > 3)
1213 printk(KERN_INFO "%s: Transmit "
1214 "error, Tx status %8.8x.\n",
1215 net_dev->name, tx_status);
1216 sis_priv->stats.tx_errors++;
1217 if (tx_status & UNDERRUN)
1218 sis_priv->stats.tx_fifo_errors++;
1219 if (tx_status & ABORT)
1220 sis_priv->stats.tx_aborted_errors++;
1221 if (tx_status & NOCARRIER)
1222 sis_priv->stats.tx_carrier_errors++;
1223 if (tx_status & OWCOLL)
1224 sis_priv->stats.tx_window_errors++;
1225 } else {
1226 /* packet successfully transmited */
1227 sis_priv->stats.collisions += (tx_status & COLCNT) >> 16;
1228 sis_priv->stats.tx_bytes += tx_status & DSIZE;
1229 sis_priv->stats.tx_packets++;
1231 /* Free the original skb. */
1232 dev_kfree_skb_irq(sis_priv->tx_skbuff[entry]);
1233 sis_priv->tx_skbuff[entry] = NULL;
1234 sis_priv->tx_ring[entry].bufptr = 0;
1235 sis_priv->tx_ring[entry].cmdsts = 0;
1238 if (sis_priv->tx_full && netif_queue_stopped(net_dev) &&
1239 sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC - 4) {
1240 /* The ring is no longer full, clear tx_full and schedule more transmission
1241 by netif_wake_queue(net_dev) */
1242 sis_priv->tx_full = 0;
1243 netif_wake_queue (net_dev);
1247 static int
1248 sis900_close(struct net_device *net_dev)
1250 long ioaddr = net_dev->base_addr;
1251 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1252 int i;
1254 netif_stop_queue(net_dev);
1256 /* Disable interrupts by clearing the interrupt mask. */
1257 outl(0x0000, ioaddr + imr);
1258 outl(0x0000, ioaddr + ier);
1260 /* Stop the chip's Tx and Rx Status Machine */
1261 outl(RxDIS | TxDIS, ioaddr + cr);
1263 del_timer(&sis_priv->timer);
1265 free_irq(net_dev->irq, net_dev);
1267 /* Free Tx and RX skbuff */
1268 for (i = 0; i < NUM_RX_DESC; i++) {
1269 if (sis_priv->rx_skbuff[i] != NULL)
1270 dev_kfree_skb(sis_priv->rx_skbuff[i]);
1271 sis_priv->rx_skbuff[i] = 0;
1273 for (i = 0; i < NUM_TX_DESC; i++) {
1274 if (sis_priv->tx_skbuff[i] != NULL)
1275 dev_kfree_skb(sis_priv->tx_skbuff[i]);
1276 sis_priv->tx_skbuff[i] = 0;
1279 /* Green! Put the chip in low-power mode. */
1281 return 0;
1284 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
1286 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1287 u16 *data = (u16 *)&rq->ifr_data;
1289 switch(cmd) {
1290 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
1291 data[0] = sis_priv->mii->phy_addr;
1292 /* Fall Through */
1293 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
1294 data[3] = mdio_read(net_dev, data[0] & 0x1f, data[1] & 0x1f);
1295 return 0;
1296 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
1297 if (!capable(CAP_NET_ADMIN))
1298 return -EPERM;
1299 mdio_write(net_dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
1300 return 0;
1301 default:
1302 return -EOPNOTSUPP;
1306 static struct net_device_stats *
1307 sis900_get_stats(struct net_device *net_dev)
1309 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1311 return &sis_priv->stats;
1314 /* Support for media type changes via net_device->set_config */
1315 static int sis900_set_config(struct net_device *dev, struct ifmap *map)
1317 struct sis900_private *sis_priv = (struct sis900_private *)dev->priv;
1318 struct mii_phy *mii_phy = sis_priv->mii;
1320 u16 status;
1322 /* we support only port changes. All other runtime configuration
1323 changes will be ignored (io base and interrupt changes for example)*/
1324 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1325 /* we switch on the ifmap->port field. I couldn't find anything
1326 like a definition or standard for the values of that field.
1327 I think the meaning of those values is device specific. But
1328 since I would like to change the media type via the ifconfig
1329 command I use the definition from linux/netdevice.h
1330 (which seems to be different from the ifport(pcmcia) definition)
1332 switch(map->port){
1333 case IF_PORT_UNKNOWN: /* use auto here */
1334 dev->if_port = map->port;
1335 /* we are going to change the media type, so the Link will
1336 be temporary down and we need to reflect that here. When
1337 the Link comes up again, it will be sensed by the sis_timer
1338 procedure, which also does all the rest for us */
1339 netif_carrier_off(dev);
1341 /* read current state */
1342 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1344 /* enable auto negotiation and reset the negotioation
1345 (I dont really know what the auto negatiotiation reset
1346 really means, but it sounds for me right to do one here)*/
1347 mdio_write(dev, mii_phy->phy_addr,
1348 MII_CONTROL, status | MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
1350 break;
1352 case IF_PORT_10BASET: /* 10BaseT */
1353 dev->if_port = map->port;
1355 /* we are going to change the media type, so the Link will
1356 be temporary down and we need to reflect that here. When
1357 the Link comes up again, it will be sensed by the sis_timer
1358 procedure, which also does all the rest for us */
1359 netif_carrier_off(dev);
1361 /* set Speed to 10Mbps */
1362 /* read current state */
1363 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1365 /* disable auto negotiation and force 10MBit mode*/
1366 mdio_write(dev, mii_phy->phy_addr,
1367 MII_CONTROL, status & ~(MII_CNTL_SPEED | MII_CNTL_AUTO));
1368 break;
1370 case IF_PORT_100BASET: /* 100BaseT */
1371 case IF_PORT_100BASETX: /* 100BaseTx */
1372 dev->if_port = map->port;
1374 /* we are going to change the media type, so the Link will
1375 be temporary down and we need to reflect that here. When
1376 the Link comes up again, it will be sensed by the sis_timer
1377 procedure, which also does all the rest for us */
1378 netif_carrier_off(dev);
1380 /* set Speed to 100Mbps */
1381 /* disable auto negotiation and enable 100MBit Mode */
1382 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1383 mdio_write(dev, mii_phy->phy_addr,
1384 MII_CONTROL, (status & ~MII_CNTL_SPEED) | MII_CNTL_SPEED);
1386 break;
1388 case IF_PORT_10BASE2: /* 10Base2 */
1389 case IF_PORT_AUI: /* AUI */
1390 case IF_PORT_100BASEFX: /* 100BaseFx */
1391 /* These Modes are not supported (are they?)*/
1392 printk(KERN_INFO "Not supported");
1393 return -EOPNOTSUPP;
1394 break;
1396 default:
1397 printk(KERN_INFO "Invalid");
1398 return -EINVAL;
1401 return 0;
1404 /* SiS 900 uses the most sigificant 7 bits to index a 128 bits multicast hash table, which makes
1405 this function a little bit different from other drivers */
1406 static u16 sis900_compute_hashtable_index(u8 *addr)
1409 /* what is the correct value of the POLYNOMIAL ??
1410 Donald Becker use 0x04C11DB7U
1411 Joseph Zbiciak im14u2c@primenet.com gives me the
1412 correct answer, thank you Joe !! */
1413 #define POLYNOMIAL 0x04C11DB7L
1414 u32 crc = 0xffffffff, msb;
1415 int i, j;
1416 u32 byte;
1418 for (i = 0; i < 6; i++) {
1419 byte = *addr++;
1420 for (j = 0; j < 8; j++) {
1421 msb = crc >> 31;
1422 crc <<= 1;
1423 if (msb ^ (byte & 1)) {
1424 crc ^= POLYNOMIAL;
1426 byte >>= 1;
1429 /* leave 7 most siginifant bits */
1430 return ((int)(crc >> 25));
1433 static void set_rx_mode(struct net_device *net_dev)
1435 long ioaddr = net_dev->base_addr;
1436 u16 mc_filter[8]; /* 128 bits multicast hash table */
1437 int i;
1438 u32 rx_mode;
1440 if (net_dev->flags & IFF_PROMISC) {
1441 /* Accept any kinds of packets */
1442 rx_mode = RFPromiscuous;
1443 for (i = 0; i < 8; i++)
1444 mc_filter[i] = 0xffff;
1445 } else if ((net_dev->mc_count > multicast_filter_limit) ||
1446 (net_dev->flags & IFF_ALLMULTI)) {
1447 /* too many multicast addresses or accept all multicast packet */
1448 rx_mode = RFAAB | RFAAM;
1449 for (i = 0; i < 8; i++)
1450 mc_filter[i] = 0xffff;
1451 } else {
1452 /* Accept Broadcast packet, destination address matchs our MAC address,
1453 use Receive Filter to reject unwanted MCAST packet */
1454 struct dev_mc_list *mclist;
1455 rx_mode = RFAAB;
1456 for (i = 0; i < 8; i++)
1457 mc_filter[i]=0;
1458 for (i = 0, mclist = net_dev->mc_list; mclist && i < net_dev->mc_count;
1459 i++, mclist = mclist->next)
1460 set_bit(sis900_compute_hashtable_index(mclist->dmi_addr),
1461 mc_filter);
1464 /* update Multicast Hash Table in Receive Filter */
1465 for (i = 0; i < 8; i++) {
1466 /* why plus 0x04 ??, That makes the correct value for hash table. */
1467 outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
1468 outl(mc_filter[i], ioaddr + rfdr);
1471 outl(RFEN | rx_mode, ioaddr + rfcr);
1473 /* sis900 is capatable of looping back packet at MAC level for debugging purpose */
1474 if (net_dev->flags & IFF_LOOPBACK) {
1475 u32 cr_saved;
1476 /* We must disable Tx/Rx before setting loopback mode */
1477 cr_saved = inl(ioaddr + cr);
1478 outl(cr_saved | TxDIS | RxDIS, ioaddr + cr);
1479 /* enable loopback */
1480 outl(inl(ioaddr + txcfg) | TxMLB, ioaddr + txcfg);
1481 outl(inl(ioaddr + rxcfg) | RxATX, ioaddr + rxcfg);
1482 /* restore cr */
1483 outl(cr_saved, ioaddr + cr);
1486 return;
1489 static void sis900_reset(struct net_device *net_dev)
1491 long ioaddr = net_dev->base_addr;
1492 int i = 0;
1493 u32 status = TxRCMP | RxRCMP;
1495 outl(0, ioaddr + ier);
1496 outl(0, ioaddr + imr);
1497 outl(0, ioaddr + rfcr);
1499 outl(RxRESET | TxRESET | RESET, ioaddr + cr);
1501 /* Check that the chip has finished the reset. */
1502 while (status && (i++ < 1000)) {
1503 status ^= (inl(isr + ioaddr) & status);
1506 outl(PESEL, ioaddr + cfg);
1509 static void __devexit sis900_remove(struct pci_dev *pci_dev)
1511 struct net_device *net_dev = pci_dev->driver_data;
1513 unregister_netdev(net_dev);
1514 release_region(net_dev->base_addr, SIS900_TOTAL_SIZE);
1515 kfree(net_dev);
1518 #define SIS900_MODULE_NAME "sis900"
1520 static struct pci_driver sis900_pci_driver = {
1521 name: SIS900_MODULE_NAME,
1522 id_table: sis900_pci_tbl,
1523 probe: sis900_probe,
1524 remove: sis900_remove,
1527 static int __init sis900_init_module(void)
1529 printk(KERN_INFO "%s", version);
1531 return pci_module_init(&sis900_pci_driver);
1534 static void __exit sis900_cleanup_module(void)
1536 pci_unregister_driver(&sis900_pci_driver);
1539 module_init(sis900_init_module);
1540 module_exit(sis900_cleanup_module);