Import 2.4.0-test6pre10
[davej-history.git] / drivers / net / sis900.c
blob3a03fa964bebc229883b23987f8a358a4cacc259
1 /* sis900.c: A SiS 900/7016 PCI Fast Ethernet driver for Linux.
2 Copyright 1999 Silicon Integrated System Corporation
3 Revision: 1.07.01 Aug. 08 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.01 Aug. 08 2000 Ollie Lho minor update fro SiS 630E and SiS 630E A1
22 Rev 1.07 Mar. 07 2000 Ollie Lho bug fix in Rx buffer ring
23 Rev 1.06.04 Feb. 11 2000 Jeff Garzik <jgarzik@mandrakesoft.com> softnet and init for kernel 2.4
24 Rev 1.06.03 Dec. 23 1999 Ollie Lho Third release
25 Rev 1.06.02 Nov. 23 1999 Ollie Lho bug in mac probing fixed
26 Rev 1.06.01 Nov. 16 1999 Ollie Lho CRC calculation provide by Joseph Zbiciak (im14u2c@primenet.com)
27 Rev 1.06 Nov. 4 1999 Ollie Lho (ollie@sis.com.tw) Second release
28 Rev 1.05.05 Oct. 29 1999 Ollie Lho (ollie@sis.com.tw) Single buffer Tx/Rx
29 Chin-Shan Li (lcs@sis.com.tw) Added AMD Am79c901 HomePNA PHY support
30 Rev 1.05 Aug. 7 1999 Jim Huang (cmhuang@sis.com.tw) Initial release
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/string.h>
38 #include <linux/timer.h>
39 #include <linux/errno.h>
40 #include <linux/ioport.h>
41 #include <linux/malloc.h>
42 #include <linux/interrupt.h>
43 #include <linux/pci.h>
44 #include <linux/netdevice.h>
45 #include <linux/init.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <asm/processor.h> /* Processor type for cache alignment. */
50 #include <asm/bitops.h>
51 #include <asm/io.h>
52 #include <linux/delay.h>
54 #include "sis900.h"
56 static const char *version =
57 "sis900.c: v1.07.01 08/08/2000\n";
59 static int max_interrupt_work = 20;
60 static int multicast_filter_limit = 128;
62 #define sis900_debug debug
63 static int sis900_debug = 0;
65 /* Time in jiffies before concluding the transmitter is hung. */
66 #define TX_TIMEOUT (4*HZ)
67 /* SiS 900 is capable of 32 bits BM DMA */
68 #define SIS900_DMA_MASK 0xffffffff
70 static struct net_device * sis900_mac_probe (struct pci_dev * pci_dev,
71 char *card_name);
72 enum {
73 SIS_900 = 0,
74 SIS_7016
76 static char * card_names[] = {
77 "SiS 900 PCI Fast Ethernet",
78 "SiS 7016 PCI Fast Ethernet"
80 static struct pci_device_id sis900_pci_tbl [] __devinitdata = {
81 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_900,
82 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_900},
83 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7016,
84 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7016},
85 {0,}
87 MODULE_DEVICE_TABLE (pci, sis900_pci_tbl);
89 static void sis900_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex);
90 static void amd79c901_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex);
92 static struct mii_chip_info {
93 const char * name;
94 u16 phy_id0;
95 u16 phy_id1;
96 void (*read_mode) (struct net_device *net_dev, int phy_addr, int *speed, int *duplex);
97 } mii_chip_table[] = {
98 {"SiS 900 Internal MII PHY", 0x001d, 0x8000, sis900_read_mode},
99 {"SiS 7014 Physical Layer Solution", 0x0016, 0xf830,sis900_read_mode},
100 {"AMD 79C901 10BASE-T PHY", 0x0000, 0x35b9, amd79c901_read_mode},
101 {"AMD 79C901 HomePNA PHY", 0x0000, 0x35c8, amd79c901_read_mode},
102 {0,},
105 struct mii_phy {
106 struct mii_phy * next;
107 struct mii_chip_info * chip_info;
108 int phy_addr;
109 u16 status;
112 typedef struct _BufferDesc {
113 u32 link;
114 u32 cmdsts;
115 u32 bufptr;
116 } BufferDesc;
118 struct sis900_private {
119 struct net_device_stats stats;
120 struct pci_dev * pci_dev;
122 spinlock_t lock;
124 struct mii_phy * mii;
125 unsigned int cur_phy;
127 struct timer_list timer; /* Link status detection timer. */
129 unsigned int cur_rx, dirty_rx; /* producer/comsumer pointers for Tx/Rx ring */
130 unsigned int cur_tx, dirty_tx;
132 /* The saved address of a sent/receive-in-place packet buffer */
133 struct sk_buff *tx_skbuff[NUM_TX_DESC];
134 struct sk_buff *rx_skbuff[NUM_RX_DESC];
135 BufferDesc tx_ring[NUM_TX_DESC];
136 BufferDesc rx_ring[NUM_RX_DESC];
138 unsigned int tx_full; /* The Tx queue is full. */
139 int LinkOn;
142 MODULE_AUTHOR("Jim Huang <cmhuang@sis.com.tw>, Ollie Lho <ollie@sis.com.tw>");
143 MODULE_DESCRIPTION("SiS 900 PCI Fast Ethernet driver");
144 MODULE_PARM(multicast_filter_limit, "i");
145 MODULE_PARM(max_interrupt_work, "i");
146 MODULE_PARM(debug, "i");
148 static int sis900_open(struct net_device *net_dev);
149 static int sis900_mii_probe (struct net_device * net_dev);
150 static void sis900_init_rxfilter (struct net_device * net_dev);
151 static u16 read_eeprom(long ioaddr, int location);
152 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location);
153 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int val);
154 static void sis900_timer(unsigned long data);
155 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy);
156 static void sis900_tx_timeout(struct net_device *net_dev);
157 static void sis900_init_tx_ring(struct net_device *net_dev);
158 static void sis900_init_rx_ring(struct net_device *net_dev);
159 static int sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev);
160 static int sis900_rx(struct net_device *net_dev);
161 static void sis900_finish_xmit (struct net_device *net_dev);
162 static void sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
163 static int sis900_close(struct net_device *net_dev);
164 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd);
165 static struct net_device_stats *sis900_get_stats(struct net_device *net_dev);
166 static u16 sis900_compute_hashtable_index(u8 *addr);
167 static void set_rx_mode(struct net_device *net_dev);
168 static void sis900_reset(struct net_device *net_dev);
170 /* walk through every ethernet PCI devices to see if some of them are matched with our card list*/
171 static int __init sis900_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
173 u32 pci_io_base;
175 if (!pci_dma_supported(pci_dev, SIS900_DMA_MASK)) {
176 printk(KERN_ERR "sis900.c: architecture does not support "
177 "32bit PCI busmaster DMA\n");
178 return -ENODEV;
181 pci_io_base = pci_resource_start(pci_dev, 0);
182 if (check_region(pci_io_base, SIS900_TOTAL_SIZE)) {
183 printk(KERN_ERR "sis900.c: can't allocate I/O space at 0x%08x\n",
184 pci_io_base);
185 return -ENODEV;
188 /* setup various bits in PCI command register */
189 if (pci_enable_device (pci_dev))
190 return -ENODEV;
191 pci_set_master(pci_dev);
193 /* do the real low level jobs */
194 if (sis900_mac_probe(pci_dev, card_names[pci_id->driver_data]) == NULL)
195 return -ENODEV;
197 return 0;
200 /* older SiS900 and friends, use EEPROM to store MAC address */
201 static int sis900_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
203 long ioaddr = pci_resource_start(pci_dev, 0);
204 u16 signature;
205 int i;
207 /* check to see if we have sane EEPROM */
208 signature = (u16) read_eeprom(ioaddr, EEPROMSignature);
209 if (signature == 0xffff || signature == 0x0000) {
210 printk (KERN_INFO "%s: Error EERPOM read %x\n",
211 net_dev->name, signature);
212 return 0;
215 /* get MAC address from EEPROM */
216 for (i = 0; i < 3; i++)
217 ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
219 return 1;
222 /* SiS630E model, use APC CMOS RAM to store MAC address */
223 static int sis630e_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
225 struct pci_dev *isa_bridge = NULL;
226 u8 reg;
227 int i;
229 if ((isa_bridge = pci_find_device(0x1039, 0x0008, isa_bridge)) == NULL) {
230 printk("%s: Can not find ISA bridge\n", net_dev->name);
231 return 0;
233 pci_read_config_byte(isa_bridge, 0x48, &reg);
234 pci_write_config_byte(isa_bridge, 0x48, reg | 0x40);
236 for (i = 0; i < 6; i++) {
237 outb(0x09 + i, 0x70);
238 ((u8 *)(net_dev->dev_addr))[i] = inb(0x71);
240 pci_write_config_byte(isa_bridge, 0x48, reg & ~0x40);
242 return 1;
245 /* SiS630E A1, The Mac address is hardcoded in the RFCR register so it is actually not necessary to
246 probe the MAC address */
247 static int sis630ea1_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
249 long ioaddr = pci_resource_start(pci_dev, 0);
250 u32 reg;
251 int i;
253 /* reload MAC address */
254 reg = inl(ioaddr + cr);
255 outl(reg | RELOAD, ioaddr + cr);
257 reg = inl(ioaddr + cr);
258 outl(reg & ~RELOAD, ioaddr + cr);
260 for (i = 0; i < 3; i++) {
261 outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
262 ((u16 *)(net_dev->dev_addr))[i] = inl(ioaddr + rfdr);
265 return 1;
268 static struct net_device * __init sis900_mac_probe (struct pci_dev * pci_dev, char * card_name)
270 struct sis900_private *sis_priv;
271 long ioaddr = pci_resource_start(pci_dev, 0);
272 struct net_device *net_dev = NULL;
273 int irq = pci_dev->irq;
274 int i, ret = 0;
275 u8 revision;
277 if ((net_dev = init_etherdev(net_dev, 0)) == NULL)
278 return NULL;
280 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
281 if (revision == SIS630E_REV)
282 ret = sis630e_get_mac_addr(pci_dev, net_dev);
283 else if (revision == SIS630EA1_REV)
284 ret = sis630ea1_get_mac_addr(pci_dev, net_dev);
285 else
286 ret = sis900_get_mac_addr(pci_dev, net_dev);
288 if (ret == 0) {
289 unregister_netdevice(net_dev);
290 return NULL;
293 /* print some information about our NIC */
294 printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ", net_dev->name,
295 card_name, ioaddr, irq);
296 for (i = 0; i < 5; i++)
297 printk("%2.2x:", (u8)net_dev->dev_addr[i]);
298 printk("%2.2x.\n", net_dev->dev_addr[i]);
300 if ((net_dev->priv = kmalloc(sizeof(struct sis900_private), GFP_KERNEL)) == NULL) {
301 unregister_netdevice(net_dev);
302 return NULL;
305 sis_priv = net_dev->priv;
306 memset(sis_priv, 0, sizeof(struct sis900_private));
308 /* We do a request_region() to register /proc/ioports info. */
309 request_region(ioaddr, SIS900_TOTAL_SIZE, net_dev->name);
310 net_dev->base_addr = ioaddr;
311 net_dev->irq = irq;
312 sis_priv->pci_dev = pci_dev;
313 spin_lock_init(&sis_priv->lock);
315 /* probe for mii transciver */
316 if (sis900_mii_probe(net_dev) == 0) {
317 unregister_netdev(net_dev);
318 kfree(sis_priv);
319 release_region(ioaddr, SIS900_TOTAL_SIZE);
320 return NULL;
323 pci_dev->driver_data = net_dev;
324 pci_dev->dma_mask = SIS900_DMA_MASK;
326 /* The SiS900-specific entries in the device structure. */
327 net_dev->open = &sis900_open;
328 net_dev->hard_start_xmit = &sis900_start_xmit;
329 net_dev->stop = &sis900_close;
330 net_dev->get_stats = &sis900_get_stats;
331 net_dev->set_multicast_list = &set_rx_mode;
332 net_dev->do_ioctl = &mii_ioctl;
333 net_dev->tx_timeout = sis900_tx_timeout;
334 net_dev->watchdog_timeo = TX_TIMEOUT;
336 return net_dev;
339 static int __init sis900_mii_probe (struct net_device * net_dev)
341 struct sis900_private * sis_priv = (struct sis900_private *)net_dev->priv;
342 int phy_addr;
343 u8 revision;
345 sis_priv->mii = NULL;
347 /* search for total of 32 possible mii phy addresses */
348 for (phy_addr = 0; phy_addr < 32; phy_addr++) {
349 u16 mii_status;
350 u16 phy_id0, phy_id1;
351 int i;
353 mii_status = mdio_read(net_dev, phy_addr, MII_STATUS);
354 if (mii_status == 0xffff || mii_status == 0x0000)
355 /* the mii is not accessable, try next one */
356 continue;
358 phy_id0 = mdio_read(net_dev, phy_addr, MII_PHY_ID0);
359 phy_id1 = mdio_read(net_dev, phy_addr, MII_PHY_ID1);
361 /* search our mii table for the current mii */
362 for (i = 0; mii_chip_table[i].phy_id1; i++)
363 if (phy_id0 == mii_chip_table[i].phy_id0) {
364 struct mii_phy * mii_phy;
366 printk(KERN_INFO
367 "%s: %s transceiver found at address %d.\n",
368 net_dev->name, mii_chip_table[i].name,
369 phy_addr);;
370 if ((mii_phy = kmalloc(sizeof(struct mii_phy), GFP_KERNEL)) != NULL) {
371 mii_phy->chip_info = mii_chip_table+i;
372 mii_phy->phy_addr = phy_addr;
373 mii_phy->status = mdio_read(net_dev, phy_addr,
374 MII_STATUS);
375 mii_phy->next = sis_priv->mii;
376 sis_priv->mii = mii_phy;
378 /* the current mii is on our mii_info_table,
379 try next address */
380 break;
384 if (sis_priv->mii == NULL) {
385 printk(KERN_INFO "%s: No MII transceivers found!\n",
386 net_dev->name);
387 return 0;
390 /* arbitrary choose that last PHY as current PHY */
391 sis_priv->cur_phy = sis_priv->mii->phy_addr;
392 printk(KERN_INFO "%s: Using %s as default\n", net_dev->name,
393 sis_priv->mii->chip_info->name);
395 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
396 if (revision == SIS630E_REV) {
397 /* SiS 630E has some bugs on default value of PHY registers */
398 mdio_write(net_dev, sis_priv->cur_phy, MII_ANADV, 0x05e1);
399 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG1, 0x22);
400 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG2, 0xff00);
401 mdio_write(net_dev, sis_priv->cur_phy, MII_MASK, 0xffc0);
402 mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, 0x1000);
405 if (sis_priv->mii->status & MII_STAT_LINK)
406 sis_priv->LinkOn = TRUE;
407 else
408 sis_priv->LinkOn = FALSE;
410 return 1;
413 /* Delay between EEPROM clock transitions. */
414 #define eeprom_delay() inl(ee_addr)
416 /* Read Serial EEPROM through EEPROM Access Register, Note that location is
417 in word (16 bits) unit */
418 static u16 read_eeprom(long ioaddr, int location)
420 int i;
421 u16 retval = 0;
422 long ee_addr = ioaddr + mear;
423 u32 read_cmd = location | EEread;
425 outl(0, ee_addr);
426 eeprom_delay();
427 outl(EECLK, ee_addr);
428 eeprom_delay();
430 /* Shift the read command (9) bits out. */
431 for (i = 8; i >= 0; i--) {
432 u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
433 outl(dataval, ee_addr);
434 eeprom_delay();
435 outl(dataval | EECLK, ee_addr);
436 eeprom_delay();
438 outb(EECS, ee_addr);
439 eeprom_delay();
441 /* read the 16-bits data in */
442 for (i = 16; i > 0; i--) {
443 outl(EECS, ee_addr);
444 eeprom_delay();
445 outl(EECS | EECLK, ee_addr);
446 eeprom_delay();
447 retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
448 eeprom_delay();
451 /* Terminate the EEPROM access. */
452 outl(0, ee_addr);
453 eeprom_delay();
454 outl(EECLK, ee_addr);
456 return (retval);
459 /* Read and write the MII management registers using software-generated
460 serial MDIO protocol. Note that the command bits and data bits are
461 send out seperately */
462 #define mdio_delay() inl(mdio_addr)
464 static void mdio_idle(long mdio_addr)
466 outl(MDIO | MDDIR, mdio_addr);
467 mdio_delay();
468 outl(MDIO | MDDIR | MDC, mdio_addr);
471 /* Syncronize the MII management interface by shifting 32 one bits out. */
472 static void mdio_reset(long mdio_addr)
474 int i;
476 for (i = 31; i >= 0; i--) {
477 outl(MDDIR | MDIO, mdio_addr);
478 mdio_delay();
479 outl(MDDIR | MDIO | MDC, mdio_addr);
480 mdio_delay();
482 return;
485 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location)
487 long mdio_addr = net_dev->base_addr + mear;
488 int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
489 u16 retval = 0;
490 int i;
492 mdio_reset(mdio_addr);
493 mdio_idle(mdio_addr);
495 for (i = 15; i >= 0; i--) {
496 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
497 outl(dataval, mdio_addr);
498 mdio_delay();
499 outl(dataval | MDC, mdio_addr);
500 mdio_delay();
503 /* Read the 16 data bits. */
504 for (i = 16; i > 0; i--) {
505 outl(0, mdio_addr);
506 mdio_delay();
507 retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
508 outl(MDC, mdio_addr);
509 mdio_delay();
511 outl(0x00, mdio_addr);
513 return retval;
516 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int value)
518 long mdio_addr = net_dev->base_addr + mear;
519 int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
520 int i;
522 mdio_reset(mdio_addr);
523 mdio_idle(mdio_addr);
525 /* Shift the command bits out. */
526 for (i = 15; i >= 0; i--) {
527 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
528 outb(dataval, mdio_addr);
529 mdio_delay();
530 outb(dataval | MDC, mdio_addr);
531 mdio_delay();
533 mdio_delay();
535 /* Shift the value bits out. */
536 for (i = 15; i >= 0; i--) {
537 int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
538 outl(dataval, mdio_addr);
539 mdio_delay();
540 outl(dataval | MDC, mdio_addr);
541 mdio_delay();
543 mdio_delay();
545 /* Clear out extra bits. */
546 for (i = 2; i > 0; i--) {
547 outb(0, mdio_addr);
548 mdio_delay();
549 outb(MDC, mdio_addr);
550 mdio_delay();
552 outl(0x00, mdio_addr);
554 return;
557 static int
558 sis900_open(struct net_device *net_dev)
560 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
561 long ioaddr = net_dev->base_addr;
563 MOD_INC_USE_COUNT;
565 /* Soft reset the chip. */
566 sis900_reset(net_dev);
568 if (request_irq(net_dev->irq, &sis900_interrupt, SA_SHIRQ, net_dev->name, net_dev)) {
569 MOD_DEC_USE_COUNT;
570 return -EAGAIN;
573 sis900_init_rxfilter(net_dev);
575 sis900_init_tx_ring(net_dev);
576 sis900_init_rx_ring(net_dev);
578 set_rx_mode(net_dev);
580 netif_start_queue(net_dev);
582 /* Enable all known interrupts by setting the interrupt mask. */
583 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
584 outl(RxENA, ioaddr + cr);
585 outl(IE, ioaddr + ier);
587 sis900_check_mode(net_dev, sis_priv->mii);
589 /* Set the timer to switch to check for link beat and perhaps switch
590 to an alternate media type. */
591 init_timer(&sis_priv->timer);
592 sis_priv->timer.expires = jiffies + HZ;
593 sis_priv->timer.data = (unsigned long)net_dev;
594 sis_priv->timer.function = &sis900_timer;
595 add_timer(&sis_priv->timer);
597 return 0;
600 /* set receive filter address to our MAC address */
601 static void
602 sis900_init_rxfilter (struct net_device * net_dev)
604 long ioaddr = net_dev->base_addr;
605 u32 rfcrSave;
606 u32 i;
608 rfcrSave = inl(rfcr + ioaddr);
610 /* disable packet filtering before setting filter */
611 outl(rfcrSave & ~RFEN, rfcr);
613 /* load MAC addr to filter data register */
614 for (i = 0 ; i < 3 ; i++) {
615 u32 w;
617 w = (u32) *((u16 *)(net_dev->dev_addr)+i);
618 outl((i << RFADDR_shift), ioaddr + rfcr);
619 outl(w, ioaddr + rfdr);
621 if (sis900_debug > 2) {
622 printk(KERN_INFO "%s: Receive Filter Addrss[%d]=%x\n",
623 net_dev->name, i, inl(ioaddr + rfdr));
627 /* enable packet filitering */
628 outl(rfcrSave | RFEN, rfcr + ioaddr);
631 /* Initialize the Tx ring. */
632 static void
633 sis900_init_tx_ring(struct net_device *net_dev)
635 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
636 long ioaddr = net_dev->base_addr;
637 int i;
639 sis_priv->tx_full = 0;
640 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
642 for (i = 0; i < NUM_TX_DESC; i++) {
643 sis_priv->tx_skbuff[i] = NULL;
645 sis_priv->tx_ring[i].link = (u32) virt_to_bus(&sis_priv->tx_ring[i+1]);
646 sis_priv->tx_ring[i].cmdsts = 0;
647 sis_priv->tx_ring[i].bufptr = 0;
649 sis_priv->tx_ring[i-1].link = (u32) virt_to_bus(&sis_priv->tx_ring[0]);
651 /* load Transmit Descriptor Register */
652 outl(virt_to_bus(&sis_priv->tx_ring[0]), ioaddr + txdp);
653 if (sis900_debug > 2)
654 printk(KERN_INFO "%s: TX descriptor register loaded with: %8.8x\n",
655 net_dev->name, inl(ioaddr + txdp));
658 /* Initialize the Rx descriptor ring, pre-allocate recevie buffers */
659 static void
660 sis900_init_rx_ring(struct net_device *net_dev)
662 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
663 long ioaddr = net_dev->base_addr;
664 int i;
666 sis_priv->cur_rx = 0;
667 sis_priv->dirty_rx = 0;
669 /* init RX descriptor */
670 for (i = 0; i < NUM_RX_DESC; i++) {
671 sis_priv->rx_skbuff[i] = NULL;
673 sis_priv->rx_ring[i].link = (u32) virt_to_bus(&sis_priv->rx_ring[i+1]);
674 sis_priv->rx_ring[i].cmdsts = 0;
675 sis_priv->rx_ring[i].bufptr = 0;
677 sis_priv->rx_ring[i-1].link = (u32) virt_to_bus(&sis_priv->rx_ring[0]);
679 /* allocate sock buffers */
680 for (i = 0; i < NUM_RX_DESC; i++) {
681 struct sk_buff *skb;
683 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
684 /* not enough memory for skbuff, this makes a "hole"
685 on the buffer ring, it is not clear how the
686 hardware will react to this kind of degenerated
687 buffer */
688 break;
690 skb->dev = net_dev;
691 sis_priv->rx_skbuff[i] = skb;
692 sis_priv->rx_ring[i].cmdsts = RX_BUF_SIZE;
693 sis_priv->rx_ring[i].bufptr = virt_to_bus(skb->tail);
695 sis_priv->dirty_rx = (unsigned int) (i - NUM_RX_DESC);
697 /* load Receive Descriptor Register */
698 outl(virt_to_bus(&sis_priv->rx_ring[0]), ioaddr + rxdp);
699 if (sis900_debug > 2)
700 printk(KERN_INFO "%s: RX descriptor register loaded with: %8.8x\n",
701 net_dev->name, inl(ioaddr + rxdp));
703 /* on each timer ticks we check two things, Link Status (ON/OFF) and
704 Link Mode (10/100/Full/Half)
706 static void sis900_timer(unsigned long data)
708 struct net_device *net_dev = (struct net_device *)data;
709 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
710 struct mii_phy *mii_phy = sis_priv->mii;
711 static int next_tick = 5*HZ;
712 u16 status;
714 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
715 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
717 /* current mii phy is failed to link, try another one */
718 while (!(status & MII_STAT_LINK)) {
719 if (mii_phy->next == NULL) {
720 if (sis_priv->LinkOn) {
721 /* link stat change from ON to OFF */
722 next_tick = HZ;
723 sis_priv->LinkOn = FALSE;
724 printk(KERN_INFO "%s: Media Link Off\n",
725 net_dev->name);
727 sis_priv->timer.expires = jiffies + next_tick;
728 add_timer(&sis_priv->timer);
729 return;
731 mii_phy = mii_phy->next;
732 status = mdio_read(net_dev, mii_phy->phy_addr, MII_STATUS);
735 if (!sis_priv->LinkOn) {
736 /* link stat change forn OFF to ON, read and report link mode */
737 sis_priv->LinkOn = TRUE;
738 next_tick = 5*HZ;
739 /* change what cur_phy means */
740 if (mii_phy->phy_addr != sis_priv->cur_phy) {
741 printk(KERN_INFO "%s: Changing transceiver to %s\n",
742 net_dev->name, mii_phy->chip_info->name);
743 /* disable previous PHY */
744 status = mdio_read(net_dev, sis_priv->cur_phy, MII_CONTROL);
745 mdio_write(net_dev, sis_priv->cur_phy,
746 MII_CONTROL, status | MII_CNTL_ISOLATE);
747 /* enable next PHY */
748 status = mdio_read(net_dev, mii_phy->phy_addr, MII_CONTROL);
749 mdio_write(net_dev, mii_phy->phy_addr,
750 MII_CONTROL, status & ~MII_CNTL_ISOLATE);
751 sis_priv->cur_phy = mii_phy->phy_addr;
753 sis900_check_mode(net_dev, mii_phy);
756 sis_priv->timer.expires = jiffies + next_tick;
757 add_timer(&sis_priv->timer);
759 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy)
761 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
762 long ioaddr = net_dev->base_addr;
763 int speed, duplex;
764 u32 tx_flags = 0, rx_flags = 0;
766 mii_phy->chip_info->read_mode(net_dev, sis_priv->cur_phy, &speed, &duplex);
768 tx_flags = TxATP | (TX_DMA_BURST << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
769 rx_flags = RX_DMA_BURST << RxMXDMA_shift;
771 if (speed == HW_SPEED_HOME || speed == HW_SPEED_10_MBPS ) {
772 rx_flags |= (RxDRNT_10 << RxDRNT_shift);
773 tx_flags |= (TxDRNT_10 << TxDRNT_shift);
775 else {
776 rx_flags |= (RxDRNT_100 << RxDRNT_shift);
777 tx_flags |= (TxDRNT_100 << TxDRNT_shift);
780 if (duplex == FDX_CAPABLE_FULL_SELECTED) {
781 tx_flags |= (TxCSI | TxHBI);
782 rx_flags |= RxATX;
785 outl (tx_flags, ioaddr + txcfg);
786 outl (rx_flags, ioaddr + rxcfg);
788 static void sis900_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex)
790 int i = 0;
791 u32 status;
793 /* STSOUT register is Latched on Transition, read operation updates it */
794 while (i++ < 2)
795 status = mdio_read(net_dev, phy_addr, MII_STSOUT);
797 if (status & MII_STSOUT_SPD)
798 *speed = HW_SPEED_100_MBPS;
799 else
800 *speed = HW_SPEED_10_MBPS;
802 if (status & MII_STSOUT_DPLX)
803 *duplex = FDX_CAPABLE_FULL_SELECTED;
804 else
805 *duplex = FDX_CAPABLE_HALF_SELECTED;
807 if (status & MII_STSOUT_LINK_FAIL)
808 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
809 else
810 printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
811 net_dev->name,
812 *speed == HW_SPEED_100_MBPS ?
813 "100mbps" : "10mbps",
814 *duplex == FDX_CAPABLE_FULL_SELECTED ?
815 "full" : "half");
817 static void amd79c901_read_mode(struct net_device *net_dev, int phy_addr, int *speed, int *duplex)
819 int i;
820 u16 status;
822 for (i = 0; i < 2; i++)
823 status = mdio_read(net_dev, phy_addr, MII_STATUS);
825 if (status & MII_STAT_CAN_AUTO) {
826 /* 10BASE-T PHY */
827 for (i = 0; i < 2; i++)
828 status = mdio_read(net_dev, phy_addr, MII_STATUS_SUMMARY);
829 if (status & MII_STSSUM_SPD)
830 *speed = HW_SPEED_100_MBPS;
831 else
832 *speed = HW_SPEED_10_MBPS;
833 if (status & MII_STSSUM_DPLX)
834 *duplex = FDX_CAPABLE_FULL_SELECTED;
835 else
836 *duplex = FDX_CAPABLE_HALF_SELECTED;
838 if (status & MII_STSSUM_LINK)
839 printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
840 net_dev->name,
841 *speed == HW_SPEED_100_MBPS ?
842 "100mbps" : "10mbps",
843 *duplex == FDX_CAPABLE_FULL_SELECTED ?
844 "full" : "half");
845 else
846 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
848 else {
849 /* HomePNA */
850 *speed = HW_SPEED_HOME;
851 *duplex = FDX_CAPABLE_HALF_SELECTED;
852 if (status & MII_STAT_LINK)
853 printk(KERN_INFO "%s: Media Link On 1mbps half-duplex \n",
854 net_dev->name);
855 else
856 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
859 static void sis900_tx_timeout(struct net_device *net_dev)
861 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
862 long ioaddr = net_dev->base_addr;
863 unsigned long flags;
864 int i;
866 printk(KERN_INFO "%s: Transmit timeout, status %8.8x %8.8x \n",
867 net_dev->name, inl(ioaddr + cr), inl(ioaddr + isr));
869 /* Disable interrupts by clearing the interrupt mask. */
870 outl(0x0000, ioaddr + imr);
872 /* use spinlock to prevent interrupt handler accessing buffer ring */
873 spin_lock_irqsave(&sis_priv->lock, flags);
875 /* discard unsent packets */
876 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
877 for (i = 0; i < NUM_TX_DESC; i++) {
878 if (sis_priv->tx_skbuff[i] != NULL) {
879 dev_kfree_skb(sis_priv->tx_skbuff[i]);
880 sis_priv->tx_skbuff[i] = 0;
881 sis_priv->tx_ring[i].cmdsts = 0;
882 sis_priv->tx_ring[i].bufptr = 0;
883 sis_priv->stats.tx_dropped++;
886 sis_priv->tx_full = 0;
887 netif_wake_queue(net_dev);
889 spin_unlock_irqrestore(&sis_priv->lock, flags);
891 net_dev->trans_start = jiffies;
893 /* FIXME: Should we restart the transmission thread here ?? */
894 outl(TxENA, ioaddr + cr);
896 /* Enable all known interrupts by setting the interrupt mask. */
897 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
898 return;
901 static int
902 sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
904 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
905 long ioaddr = net_dev->base_addr;
906 unsigned int entry;
907 unsigned long flags;
909 spin_lock_irqsave(&sis_priv->lock, flags);
911 /* Calculate the next Tx descriptor entry. */
912 entry = sis_priv->cur_tx % NUM_TX_DESC;
913 sis_priv->tx_skbuff[entry] = skb;
915 /* set the transmit buffer descriptor and enable Transmit State Machine */
916 sis_priv->tx_ring[entry].bufptr = virt_to_bus(skb->data);
917 sis_priv->tx_ring[entry].cmdsts = (OWN | skb->len);
918 outl(TxENA, ioaddr + cr);
920 if (++sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC) {
921 /* Typical path, tell upper layer that more transmission is possible */
922 netif_start_queue(net_dev);
923 } else {
924 /* buffer full, tell upper layer no more transmission */
925 sis_priv->tx_full = 1;
926 netif_stop_queue(net_dev);
929 spin_unlock_irqrestore(&sis_priv->lock, flags);
931 net_dev->trans_start = jiffies;
933 if (sis900_debug > 3)
934 printk(KERN_INFO "%s: Queued Tx packet at %p size %d "
935 "to slot %d.\n",
936 net_dev->name, skb->data, (int)skb->len, entry);
938 return 0;
941 /* The interrupt handler does all of the Rx thread work and cleans up
942 after the Tx thread. */
943 static void sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
945 struct net_device *net_dev = (struct net_device *)dev_instance;
946 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
947 int boguscnt = max_interrupt_work;
948 long ioaddr = net_dev->base_addr;
949 u32 status;
951 spin_lock (&sis_priv->lock);
953 do {
954 status = inl(ioaddr + isr);
956 if ((status & (HIBERR|TxURN|TxERR|TxIDLE|RxORN|RxERR|RxOK)) == 0)
957 /* nothing intresting happened */
958 break;
960 /* why dow't we break after Tx/Rx case ?? keyword: full-duplex */
961 if (status & (RxORN | RxERR | RxOK))
962 /* Rx interrupt */
963 sis900_rx(net_dev);
965 if (status & (TxURN | TxERR | TxIDLE))
966 /* Tx interrupt */
967 sis900_finish_xmit(net_dev);
969 /* something strange happened !!! */
970 if (status & HIBERR) {
971 printk(KERN_INFO "%s: Abnormal interrupt,"
972 "status %#8.8x.\n", net_dev->name, status);
973 break;
975 if (--boguscnt < 0) {
976 printk(KERN_INFO "%s: Too much work at interrupt, "
977 "interrupt status = %#8.8x.\n",
978 net_dev->name, status);
979 break;
981 } while (1);
983 if (sis900_debug > 3)
984 printk(KERN_INFO "%s: exiting interrupt, "
985 "interrupt status = 0x%#8.8x.\n",
986 net_dev->name, inl(ioaddr + isr));
988 spin_unlock (&sis_priv->lock);
989 return;
992 /* Process receive interrupt events, put buffer to higher layer and refill buffer pool
993 Note: This fucntion is called by interrupt handler, don't do "too much" work here */
994 static int sis900_rx(struct net_device *net_dev)
996 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
997 long ioaddr = net_dev->base_addr;
998 unsigned int entry = sis_priv->cur_rx % NUM_RX_DESC;
999 u32 rx_status = sis_priv->rx_ring[entry].cmdsts;
1001 if (sis900_debug > 3)
1002 printk(KERN_INFO "sis900_rx, cur_rx:%4.4d, dirty_rx:%4.4d "
1003 "status:0x%8.8x\n",
1004 sis_priv->cur_rx, sis_priv->dirty_rx, rx_status);
1006 while (rx_status & OWN) {
1007 unsigned int rx_size;
1009 rx_size = (rx_status & DSIZE) - CRC_SIZE;
1011 if (rx_status & (ABORT|OVERRUN|TOOLONG|RUNT|RXISERR|CRCERR|FAERR)) {
1012 /* corrupted packet received */
1013 if (sis900_debug > 3)
1014 printk(KERN_INFO "%s: Corrupted packet "
1015 "received, buffer status = 0x%8.8x.\n",
1016 net_dev->name, rx_status);
1017 sis_priv->stats.rx_errors++;
1018 if (rx_status & OVERRUN)
1019 sis_priv->stats.rx_over_errors++;
1020 if (rx_status & (TOOLONG|RUNT))
1021 sis_priv->stats.rx_length_errors++;
1022 if (rx_status & (RXISERR | FAERR))
1023 sis_priv->stats.rx_frame_errors++;
1024 if (rx_status & CRCERR)
1025 sis_priv->stats.rx_crc_errors++;
1026 /* reset buffer descriptor state */
1027 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1028 } else {
1029 struct sk_buff * skb;
1031 /* This situation should never happen, but due to
1032 some unknow bugs, it is possible that
1033 we are working on NULL sk_buff :-( */
1034 if (sis_priv->rx_skbuff[entry] == NULL) {
1035 printk(KERN_INFO "%s: NULL pointer "
1036 "encountered in Rx ring, skipping\n",
1037 net_dev->name);
1038 break;
1041 /* gvie the socket buffer to upper layers */
1042 skb = sis_priv->rx_skbuff[entry];
1043 skb_put(skb, rx_size);
1044 skb->protocol = eth_type_trans(skb, net_dev);
1045 netif_rx(skb);
1047 /* some network statistics */
1048 if ((rx_status & BCAST) == MCAST)
1049 sis_priv->stats.multicast++;
1050 net_dev->last_rx = jiffies;
1051 sis_priv->stats.rx_bytes += rx_size;
1052 sis_priv->stats.rx_packets++;
1054 /* refill the Rx buffer, what if there is not enought memory for
1055 new socket buffer ?? */
1056 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1057 /* not enough memory for skbuff, this makes a "hole"
1058 on the buffer ring, it is not clear how the
1059 hardware will react to this kind of degenerated
1060 buffer */
1061 printk(KERN_INFO "%s: Memory squeeze,"
1062 "deferring packet.\n",
1063 net_dev->name);
1064 sis_priv->rx_skbuff[entry] = NULL;
1065 /* reset buffer descriptor state */
1066 sis_priv->rx_ring[entry].cmdsts = 0;
1067 sis_priv->rx_ring[entry].bufptr = 0;
1068 sis_priv->stats.rx_dropped++;
1069 break;
1071 skb->dev = net_dev;
1072 sis_priv->rx_skbuff[entry] = skb;
1073 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1074 sis_priv->rx_ring[entry].bufptr = virt_to_bus(skb->tail);
1075 sis_priv->dirty_rx++;
1077 sis_priv->cur_rx++;
1078 entry = sis_priv->cur_rx % NUM_RX_DESC;
1079 rx_status = sis_priv->rx_ring[entry].cmdsts;
1080 } // while
1082 /* refill the Rx buffer, what if the rate of refilling is slower than
1083 consuming ?? */
1084 for (;sis_priv->cur_rx - sis_priv->dirty_rx > 0; sis_priv->dirty_rx++) {
1085 struct sk_buff *skb;
1087 entry = sis_priv->dirty_rx % NUM_RX_DESC;
1089 if (sis_priv->rx_skbuff[entry] == NULL) {
1090 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1091 /* not enough memory for skbuff, this makes a "hole"
1092 on the buffer ring, it is not clear how the
1093 hardware will react to this kind of degenerated
1094 buffer */
1095 printk(KERN_INFO "%s: Memory squeeze,"
1096 "deferring packet.\n",
1097 net_dev->name);
1098 sis_priv->stats.rx_dropped++;
1099 break;
1101 skb->dev = net_dev;
1102 sis_priv->rx_skbuff[entry] = skb;
1103 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1104 sis_priv->rx_ring[entry].bufptr = virt_to_bus(skb->tail);
1107 /* re-enable the potentially idle receive state matchine */
1108 outl(RxENA , ioaddr + cr );
1110 return 0;
1113 /* finish up transmission of packets, check for error condition and free skbuff etc.
1114 Note: This fucntion is called by interrupt handler, don't do "too much" work here */
1115 static void sis900_finish_xmit (struct net_device *net_dev)
1117 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1119 for (; sis_priv->dirty_tx < sis_priv->cur_tx; sis_priv->dirty_tx++) {
1120 unsigned int entry;
1121 u32 tx_status;
1123 entry = sis_priv->dirty_tx % NUM_TX_DESC;
1124 tx_status = sis_priv->tx_ring[entry].cmdsts;
1126 if (tx_status & OWN) {
1127 /* The packet is not transmited yet (owned by hardware) !
1128 Note: the interrupt is generated only when Tx Machine
1129 is idle, so this is an almost impossible case */
1130 break;
1133 if (tx_status & (ABORT | UNDERRUN | OWCOLL)) {
1134 /* packet unsuccessfully transmited */
1135 if (sis900_debug > 3)
1136 printk(KERN_INFO "%s: Transmit "
1137 "error, Tx status %8.8x.\n",
1138 net_dev->name, tx_status);
1139 sis_priv->stats.tx_errors++;
1140 if (tx_status & UNDERRUN)
1141 sis_priv->stats.tx_fifo_errors++;
1142 if (tx_status & ABORT)
1143 sis_priv->stats.tx_aborted_errors++;
1144 if (tx_status & NOCARRIER)
1145 sis_priv->stats.tx_carrier_errors++;
1146 if (tx_status & OWCOLL)
1147 sis_priv->stats.tx_window_errors++;
1148 } else {
1149 /* packet successfully transmited */
1150 sis_priv->stats.collisions += (tx_status & COLCNT) >> 16;
1151 sis_priv->stats.tx_bytes += tx_status & DSIZE;
1152 sis_priv->stats.tx_packets++;
1154 /* Free the original skb. */
1155 dev_kfree_skb_irq(sis_priv->tx_skbuff[entry]);
1156 sis_priv->tx_skbuff[entry] = NULL;
1157 sis_priv->tx_ring[entry].bufptr = 0;
1158 sis_priv->tx_ring[entry].cmdsts = 0;
1161 if (sis_priv->tx_full && netif_queue_stopped(net_dev) &&
1162 sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC - 4) {
1163 /* The ring is no longer full, clear tx_full and schedule more transmission
1164 by netif_wake_queue(net_dev) */
1165 sis_priv->tx_full = 0;
1166 netif_wake_queue (net_dev);
1170 static int
1171 sis900_close(struct net_device *net_dev)
1173 long ioaddr = net_dev->base_addr;
1174 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1175 int i;
1177 netif_stop_queue(net_dev);
1179 /* Disable interrupts by clearing the interrupt mask. */
1180 outl(0x0000, ioaddr + imr);
1181 outl(0x0000, ioaddr + ier);
1183 /* Stop the chip's Tx and Rx Status Machine */
1184 outl(RxDIS | TxDIS, ioaddr + cr);
1186 del_timer(&sis_priv->timer);
1188 free_irq(net_dev->irq, net_dev);
1190 /* Free Tx and RX skbuff */
1191 for (i = 0; i < NUM_RX_DESC; i++) {
1192 if (sis_priv->rx_skbuff[i] != NULL)
1193 dev_kfree_skb(sis_priv->rx_skbuff[i]);
1194 sis_priv->rx_skbuff[i] = 0;
1196 for (i = 0; i < NUM_TX_DESC; i++) {
1197 if (sis_priv->tx_skbuff[i] != NULL)
1198 dev_kfree_skb(sis_priv->tx_skbuff[i]);
1199 sis_priv->tx_skbuff[i] = 0;
1202 /* Green! Put the chip in low-power mode. */
1204 MOD_DEC_USE_COUNT;
1206 return 0;
1209 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
1211 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1212 u16 *data = (u16 *)&rq->ifr_data;
1214 switch(cmd) {
1215 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
1216 data[0] = sis_priv->mii->phy_addr;
1217 /* Fall Through */
1218 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
1219 data[3] = mdio_read(net_dev, data[0] & 0x1f, data[1] & 0x1f);
1220 return 0;
1221 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
1222 if (!capable(CAP_NET_ADMIN))
1223 return -EPERM;
1224 mdio_write(net_dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
1225 return 0;
1226 default:
1227 return -EOPNOTSUPP;
1231 static struct net_device_stats *
1232 sis900_get_stats(struct net_device *net_dev)
1234 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1236 return &sis_priv->stats;
1239 /* SiS 900 uses the most sigificant 7 bits to index a 128 bits multicast hash table, which makes
1240 this function a little bit different from other drivers */
1241 static u16 sis900_compute_hashtable_index(u8 *addr)
1244 /* what is the correct value of the POLYNOMIAL ??
1245 Donald Becker use 0x04C11DB7U
1246 Joseph Zbiciak im14u2c@primenet.com gives me the
1247 correct answer, thank you Joe !! */
1248 #define POLYNOMIAL 0x04C11DB7L
1249 u32 crc = 0xffffffff, msb;
1250 int i, j;
1251 u32 byte;
1253 for (i = 0; i < 6; i++) {
1254 byte = *addr++;
1255 for (j = 0; j < 8; j++) {
1256 msb = crc >> 31;
1257 crc <<= 1;
1258 if (msb ^ (byte & 1)) {
1259 crc ^= POLYNOMIAL;
1261 byte >>= 1;
1264 /* leave 7 most siginifant bits */
1265 return ((int)(crc >> 25));
1268 static void set_rx_mode(struct net_device *net_dev)
1270 long ioaddr = net_dev->base_addr;
1271 u16 mc_filter[8]; /* 128 bits multicast hash table */
1272 int i;
1273 u32 rx_mode;
1275 if (net_dev->flags & IFF_PROMISC) {
1276 /* Accept any kinds of packets */
1277 rx_mode = RFPromiscuous;
1278 for (i = 0; i < 8; i++)
1279 mc_filter[i] = 0xffff;
1280 } else if ((net_dev->mc_count > multicast_filter_limit) ||
1281 (net_dev->flags & IFF_ALLMULTI)) {
1282 /* too many multicast addresses or accept all multicast packet */
1283 rx_mode = RFAAB | RFAAM;
1284 for (i = 0; i < 8; i++)
1285 mc_filter[i] = 0xffff;
1286 } else {
1287 /* Accept Broadcast packet, destination address matchs our MAC address,
1288 use Receive Filter to reject unwanted MCAST packet */
1289 struct dev_mc_list *mclist;
1290 rx_mode = RFAAB;
1291 for (i = 0; i < 8; i++)
1292 mc_filter[i]=0;
1293 for (i = 0, mclist = net_dev->mc_list; mclist && i < net_dev->mc_count;
1294 i++, mclist = mclist->next)
1295 set_bit(sis900_compute_hashtable_index(mclist->dmi_addr),
1296 mc_filter);
1299 /* update Multicast Hash Table in Receive Filter */
1300 for (i = 0; i < 8; i++) {
1301 /* why plus 0x04 ??, That makes the correct value for hash table. */
1302 outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
1303 outl(mc_filter[i], ioaddr + rfdr);
1306 outl(RFEN | rx_mode, ioaddr + rfcr);
1308 /* sis900 is capatable of looping back packet at MAC level for debugging purpose */
1309 if (net_dev->flags & IFF_LOOPBACK) {
1310 u32 cr_saved;
1311 /* We must disable Tx/Rx before setting loopback mode */
1312 cr_saved = inl(ioaddr + cr);
1313 outl(cr_saved | TxDIS | RxDIS, ioaddr + cr);
1314 /* enable loopback */
1315 outl(inl(ioaddr + txcfg) | TxMLB, ioaddr + txcfg);
1316 outl(inl(ioaddr + rxcfg) | RxATX, ioaddr + rxcfg);
1317 /* restore cr */
1318 outl(cr_saved, ioaddr + cr);
1321 return;
1324 static void sis900_reset(struct net_device *net_dev)
1326 long ioaddr = net_dev->base_addr;
1327 int i = 0;
1328 u32 status = TxRCMP | RxRCMP;
1330 outl(0, ioaddr + ier);
1331 outl(0, ioaddr + imr);
1332 outl(0, ioaddr + rfcr);
1334 outl(RxRESET | TxRESET | RESET, ioaddr + cr);
1336 /* Check that the chip has finished the reset. */
1337 while (status && (i++ < 1000)) {
1338 status ^= (inl(isr + ioaddr) & status);
1341 outl(PESEL, ioaddr + cfg);
1344 static void __exit sis900_remove(struct pci_dev *pci_dev)
1346 struct net_device *net_dev = pci_dev->driver_data;
1347 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
1349 unregister_netdev(net_dev);
1350 release_region(net_dev->base_addr, SIS900_TOTAL_SIZE);
1352 kfree(sis_priv);
1353 kfree(net_dev);
1356 #define SIS900_MODULE_NAME "sis900"
1358 static struct pci_driver sis900_pci_driver = {
1359 name: SIS900_MODULE_NAME,
1360 id_table: sis900_pci_tbl,
1361 probe: sis900_probe,
1362 remove: sis900_remove,
1365 static int __init sis900_init_module(void)
1367 if (!pci_present()) /* No PCI bus in this machine! */
1368 return -ENODEV;
1370 printk(KERN_INFO "%s", version);
1372 if (!pci_register_driver(&sis900_pci_driver)) {
1373 pci_unregister_driver(&sis900_pci_driver);
1374 return -ENODEV;
1376 return 0;
1379 static void __exit sis900_cleanup_module(void)
1381 pci_unregister_driver(&sis900_pci_driver);
1384 module_init(sis900_init_module);
1385 module_exit(sis900_cleanup_module);