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