1a88394cfSJeff Kirsher /* winbond-840.c: A Linux PCI network adapter device driver. */
2a88394cfSJeff Kirsher /*
3a88394cfSJeff Kirsher 	Written 1998-2001 by Donald Becker.
4a88394cfSJeff Kirsher 
5a88394cfSJeff Kirsher 	This software may be used and distributed according to the terms of
6a88394cfSJeff Kirsher 	the GNU General Public License (GPL), incorporated herein by reference.
7a88394cfSJeff Kirsher 	Drivers based on or derived from this code fall under the GPL and must
8a88394cfSJeff Kirsher 	retain the authorship, copyright and license notice.  This file is not
9a88394cfSJeff Kirsher 	a complete program and may only be used when the entire operating
10a88394cfSJeff Kirsher 	system is licensed under the GPL.
11a88394cfSJeff Kirsher 
12a88394cfSJeff Kirsher 	The author may be reached as becker@scyld.com, or C/O
13a88394cfSJeff Kirsher 	Scyld Computing Corporation
14a88394cfSJeff Kirsher 	410 Severn Ave., Suite 210
15a88394cfSJeff Kirsher 	Annapolis MD 21403
16a88394cfSJeff Kirsher 
17a88394cfSJeff Kirsher 	Support and updates available at
18a88394cfSJeff Kirsher 	http://www.scyld.com/network/drivers.html
19a88394cfSJeff Kirsher 
20a88394cfSJeff Kirsher 	Do not remove the copyright information.
21a88394cfSJeff Kirsher 	Do not change the version information unless an improvement has been made.
22a88394cfSJeff Kirsher 	Merely removing my name, as Compex has done in the past, does not count
23a88394cfSJeff Kirsher 	as an improvement.
24a88394cfSJeff Kirsher 
25a88394cfSJeff Kirsher 	Changelog:
26a88394cfSJeff Kirsher 	* ported to 2.4
27a88394cfSJeff Kirsher 		???
28a88394cfSJeff Kirsher 	* spin lock update, memory barriers, new style dma mappings
29a88394cfSJeff Kirsher 		limit each tx buffer to < 1024 bytes
30a88394cfSJeff Kirsher 		remove DescIntr from Rx descriptors (that's an Tx flag)
31a88394cfSJeff Kirsher 		remove next pointer from Tx descriptors
32a88394cfSJeff Kirsher 		synchronize tx_q_bytes
33a88394cfSJeff Kirsher 		software reset in tx_timeout
34a88394cfSJeff Kirsher 			Copyright (C) 2000 Manfred Spraul
35a88394cfSJeff Kirsher 	* further cleanups
36a88394cfSJeff Kirsher 		power management.
37a88394cfSJeff Kirsher 		support for big endian descriptors
38a88394cfSJeff Kirsher 			Copyright (C) 2001 Manfred Spraul
39a88394cfSJeff Kirsher   	* ethtool support (jgarzik)
40a88394cfSJeff Kirsher 	* Replace some MII-related magic numbers with constants (jgarzik)
41a88394cfSJeff Kirsher 
42a88394cfSJeff Kirsher 	TODO:
43a88394cfSJeff Kirsher 	* enable pci_power_off
44a88394cfSJeff Kirsher 	* Wake-On-LAN
45a88394cfSJeff Kirsher */
46a88394cfSJeff Kirsher 
47a88394cfSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48a88394cfSJeff Kirsher 
49a88394cfSJeff Kirsher #define DRV_NAME	"winbond-840"
50a88394cfSJeff Kirsher #define DRV_VERSION	"1.01-e"
51a88394cfSJeff Kirsher #define DRV_RELDATE	"Sep-11-2006"
52a88394cfSJeff Kirsher 
53a88394cfSJeff Kirsher 
54a88394cfSJeff Kirsher /* Automatically extracted configuration info:
55a88394cfSJeff Kirsher probe-func: winbond840_probe
56a88394cfSJeff Kirsher config-in: tristate 'Winbond W89c840 Ethernet support' CONFIG_WINBOND_840
57a88394cfSJeff Kirsher 
58a88394cfSJeff Kirsher c-help-name: Winbond W89c840 PCI Ethernet support
59a88394cfSJeff Kirsher c-help-symbol: CONFIG_WINBOND_840
60a88394cfSJeff Kirsher c-help: This driver is for the Winbond W89c840 chip.  It also works with
61a88394cfSJeff Kirsher c-help: the TX9882 chip on the Compex RL100-ATX board.
62a88394cfSJeff Kirsher c-help: More specific information and updates are available from
63a88394cfSJeff Kirsher c-help: http://www.scyld.com/network/drivers.html
64a88394cfSJeff Kirsher */
65a88394cfSJeff Kirsher 
66a88394cfSJeff Kirsher /* The user-configurable values.
67a88394cfSJeff Kirsher    These may be modified when a driver module is loaded.*/
68a88394cfSJeff Kirsher 
69a88394cfSJeff Kirsher static int debug = 1;			/* 1 normal messages, 0 quiet .. 7 verbose. */
70a88394cfSJeff Kirsher static int max_interrupt_work = 20;
71a88394cfSJeff Kirsher /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
72a88394cfSJeff Kirsher    The '840 uses a 64 element hash table based on the Ethernet CRC.  */
73a88394cfSJeff Kirsher static int multicast_filter_limit = 32;
74a88394cfSJeff Kirsher 
75a88394cfSJeff Kirsher /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
76a88394cfSJeff Kirsher    Setting to > 1518 effectively disables this feature. */
77a88394cfSJeff Kirsher static int rx_copybreak;
78a88394cfSJeff Kirsher 
79a88394cfSJeff Kirsher /* Used to pass the media type, etc.
80a88394cfSJeff Kirsher    Both 'options[]' and 'full_duplex[]' should exist for driver
81a88394cfSJeff Kirsher    interoperability.
82a88394cfSJeff Kirsher    The media type is usually passed in 'options[]'.
83a88394cfSJeff Kirsher */
84a88394cfSJeff Kirsher #define MAX_UNITS 8		/* More are supported, limit only on options */
85a88394cfSJeff Kirsher static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
86a88394cfSJeff Kirsher static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
87a88394cfSJeff Kirsher 
88a88394cfSJeff Kirsher /* Operational parameters that are set at compile time. */
89a88394cfSJeff Kirsher 
90a88394cfSJeff Kirsher /* Keep the ring sizes a power of two for compile efficiency.
91a88394cfSJeff Kirsher    The compiler will convert <unsigned>'%'<2^N> into a bit mask.
92a88394cfSJeff Kirsher    Making the Tx ring too large decreases the effectiveness of channel
93a88394cfSJeff Kirsher    bonding and packet priority.
94a88394cfSJeff Kirsher    There are no ill effects from too-large receive rings. */
95a88394cfSJeff Kirsher #define TX_QUEUE_LEN	10		/* Limit ring entries actually used.  */
96a88394cfSJeff Kirsher #define TX_QUEUE_LEN_RESTART	5
97a88394cfSJeff Kirsher 
98a88394cfSJeff Kirsher #define TX_BUFLIMIT	(1024-128)
99a88394cfSJeff Kirsher 
100a88394cfSJeff Kirsher /* The presumed FIFO size for working around the Tx-FIFO-overflow bug.
101a88394cfSJeff Kirsher    To avoid overflowing we don't queue again until we have room for a
102a88394cfSJeff Kirsher    full-size packet.
103a88394cfSJeff Kirsher  */
104a88394cfSJeff Kirsher #define TX_FIFO_SIZE (2048)
105a88394cfSJeff Kirsher #define TX_BUG_FIFO_LIMIT (TX_FIFO_SIZE-1514-16)
106a88394cfSJeff Kirsher 
107a88394cfSJeff Kirsher 
108a88394cfSJeff Kirsher /* Operational parameters that usually are not changed. */
109a88394cfSJeff Kirsher /* Time in jiffies before concluding the transmitter is hung. */
110a88394cfSJeff Kirsher #define TX_TIMEOUT  (2*HZ)
111a88394cfSJeff Kirsher 
112a88394cfSJeff Kirsher /* Include files, designed to support most kernel versions 2.0.0 and later. */
113a88394cfSJeff Kirsher #include <linux/module.h>
114a88394cfSJeff Kirsher #include <linux/kernel.h>
115a88394cfSJeff Kirsher #include <linux/string.h>
116a88394cfSJeff Kirsher #include <linux/timer.h>
117a88394cfSJeff Kirsher #include <linux/errno.h>
118a88394cfSJeff Kirsher #include <linux/ioport.h>
119a88394cfSJeff Kirsher #include <linux/interrupt.h>
120a88394cfSJeff Kirsher #include <linux/pci.h>
121a88394cfSJeff Kirsher #include <linux/dma-mapping.h>
122a88394cfSJeff Kirsher #include <linux/netdevice.h>
123a88394cfSJeff Kirsher #include <linux/etherdevice.h>
124a88394cfSJeff Kirsher #include <linux/skbuff.h>
125a88394cfSJeff Kirsher #include <linux/init.h>
126a88394cfSJeff Kirsher #include <linux/delay.h>
127a88394cfSJeff Kirsher #include <linux/ethtool.h>
128a88394cfSJeff Kirsher #include <linux/mii.h>
129a88394cfSJeff Kirsher #include <linux/rtnetlink.h>
130a88394cfSJeff Kirsher #include <linux/crc32.h>
131a88394cfSJeff Kirsher #include <linux/bitops.h>
132a88394cfSJeff Kirsher #include <asm/uaccess.h>
133a88394cfSJeff Kirsher #include <asm/processor.h>		/* Processor type for cache alignment. */
134a88394cfSJeff Kirsher #include <asm/io.h>
135a88394cfSJeff Kirsher #include <asm/irq.h>
136a88394cfSJeff Kirsher 
137a88394cfSJeff Kirsher #include "tulip.h"
138a88394cfSJeff Kirsher 
139a88394cfSJeff Kirsher #undef PKT_BUF_SZ			/* tulip.h also defines this */
140a88394cfSJeff Kirsher #define PKT_BUF_SZ		1536	/* Size of each temporary Rx buffer.*/
141a88394cfSJeff Kirsher 
142a88394cfSJeff Kirsher /* These identify the driver base version and may not be removed. */
143a88394cfSJeff Kirsher static const char version[] __initconst =
144a88394cfSJeff Kirsher 	"v" DRV_VERSION " (2.4 port) "
145a88394cfSJeff Kirsher 	DRV_RELDATE "  Donald Becker <becker@scyld.com>\n"
146a88394cfSJeff Kirsher 	"  http://www.scyld.com/network/drivers.html\n";
147a88394cfSJeff Kirsher 
148a88394cfSJeff Kirsher MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
149a88394cfSJeff Kirsher MODULE_DESCRIPTION("Winbond W89c840 Ethernet driver");
150a88394cfSJeff Kirsher MODULE_LICENSE("GPL");
151a88394cfSJeff Kirsher MODULE_VERSION(DRV_VERSION);
152a88394cfSJeff Kirsher 
153a88394cfSJeff Kirsher module_param(max_interrupt_work, int, 0);
154a88394cfSJeff Kirsher module_param(debug, int, 0);
155a88394cfSJeff Kirsher module_param(rx_copybreak, int, 0);
156a88394cfSJeff Kirsher module_param(multicast_filter_limit, int, 0);
157a88394cfSJeff Kirsher module_param_array(options, int, NULL, 0);
158a88394cfSJeff Kirsher module_param_array(full_duplex, int, NULL, 0);
159a88394cfSJeff Kirsher MODULE_PARM_DESC(max_interrupt_work, "winbond-840 maximum events handled per interrupt");
160a88394cfSJeff Kirsher MODULE_PARM_DESC(debug, "winbond-840 debug level (0-6)");
161a88394cfSJeff Kirsher MODULE_PARM_DESC(rx_copybreak, "winbond-840 copy breakpoint for copy-only-tiny-frames");
162a88394cfSJeff Kirsher MODULE_PARM_DESC(multicast_filter_limit, "winbond-840 maximum number of filtered multicast addresses");
163a88394cfSJeff Kirsher MODULE_PARM_DESC(options, "winbond-840: Bits 0-3: media type, bit 17: full duplex");
164a88394cfSJeff Kirsher MODULE_PARM_DESC(full_duplex, "winbond-840 full duplex setting(s) (1)");
165a88394cfSJeff Kirsher 
166a88394cfSJeff Kirsher /*
167a88394cfSJeff Kirsher 				Theory of Operation
168a88394cfSJeff Kirsher 
169a88394cfSJeff Kirsher I. Board Compatibility
170a88394cfSJeff Kirsher 
171a88394cfSJeff Kirsher This driver is for the Winbond w89c840 chip.
172a88394cfSJeff Kirsher 
173a88394cfSJeff Kirsher II. Board-specific settings
174a88394cfSJeff Kirsher 
175a88394cfSJeff Kirsher None.
176a88394cfSJeff Kirsher 
177a88394cfSJeff Kirsher III. Driver operation
178a88394cfSJeff Kirsher 
179a88394cfSJeff Kirsher This chip is very similar to the Digital 21*4* "Tulip" family.  The first
180a88394cfSJeff Kirsher twelve registers and the descriptor format are nearly identical.  Read a
181a88394cfSJeff Kirsher Tulip manual for operational details.
182a88394cfSJeff Kirsher 
183a88394cfSJeff Kirsher A significant difference is that the multicast filter and station address are
184a88394cfSJeff Kirsher stored in registers rather than loaded through a pseudo-transmit packet.
185a88394cfSJeff Kirsher 
186a88394cfSJeff Kirsher Unlike the Tulip, transmit buffers are limited to 1KB.  To transmit a
187a88394cfSJeff Kirsher full-sized packet we must use both data buffers in a descriptor.  Thus the
188a88394cfSJeff Kirsher driver uses ring mode where descriptors are implicitly sequential in memory,
189a88394cfSJeff Kirsher rather than using the second descriptor address as a chain pointer to
190a88394cfSJeff Kirsher subsequent descriptors.
191a88394cfSJeff Kirsher 
192a88394cfSJeff Kirsher IV. Notes
193a88394cfSJeff Kirsher 
194a88394cfSJeff Kirsher If you are going to almost clone a Tulip, why not go all the way and avoid
195a88394cfSJeff Kirsher the need for a new driver?
196a88394cfSJeff Kirsher 
197a88394cfSJeff Kirsher IVb. References
198a88394cfSJeff Kirsher 
199a88394cfSJeff Kirsher http://www.scyld.com/expert/100mbps.html
200a88394cfSJeff Kirsher http://www.scyld.com/expert/NWay.html
201a88394cfSJeff Kirsher http://www.winbond.com.tw/
202a88394cfSJeff Kirsher 
203a88394cfSJeff Kirsher IVc. Errata
204a88394cfSJeff Kirsher 
205a88394cfSJeff Kirsher A horrible bug exists in the transmit FIFO.  Apparently the chip doesn't
206a88394cfSJeff Kirsher correctly detect a full FIFO, and queuing more than 2048 bytes may result in
207a88394cfSJeff Kirsher silent data corruption.
208a88394cfSJeff Kirsher 
209a88394cfSJeff Kirsher Test with 'ping -s 10000' on a fast computer.
210a88394cfSJeff Kirsher 
211a88394cfSJeff Kirsher */
212a88394cfSJeff Kirsher 
213a88394cfSJeff Kirsher 
214a88394cfSJeff Kirsher 
215a88394cfSJeff Kirsher /*
216a88394cfSJeff Kirsher   PCI probe table.
217a88394cfSJeff Kirsher */
218a88394cfSJeff Kirsher enum chip_capability_flags {
219a88394cfSJeff Kirsher 	CanHaveMII=1, HasBrokenTx=2, AlwaysFDX=4, FDXOnNoMII=8,
220a88394cfSJeff Kirsher };
221a88394cfSJeff Kirsher 
222a88394cfSJeff Kirsher static DEFINE_PCI_DEVICE_TABLE(w840_pci_tbl) = {
223a88394cfSJeff Kirsher 	{ 0x1050, 0x0840, PCI_ANY_ID, 0x8153,     0, 0, 0 },
224a88394cfSJeff Kirsher 	{ 0x1050, 0x0840, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
225a88394cfSJeff Kirsher 	{ 0x11f6, 0x2011, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
226a88394cfSJeff Kirsher 	{ }
227a88394cfSJeff Kirsher };
228a88394cfSJeff Kirsher MODULE_DEVICE_TABLE(pci, w840_pci_tbl);
229a88394cfSJeff Kirsher 
230a88394cfSJeff Kirsher enum {
231a88394cfSJeff Kirsher 	netdev_res_size		= 128,	/* size of PCI BAR resource */
232a88394cfSJeff Kirsher };
233a88394cfSJeff Kirsher 
234a88394cfSJeff Kirsher struct pci_id_info {
235a88394cfSJeff Kirsher         const char *name;
236a88394cfSJeff Kirsher         int drv_flags;		/* Driver use, intended as capability flags. */
237a88394cfSJeff Kirsher };
238a88394cfSJeff Kirsher 
239a88394cfSJeff Kirsher static const struct pci_id_info pci_id_tbl[] __devinitdata = {
240a88394cfSJeff Kirsher 	{ 				/* Sometime a Level-One switch card. */
241a88394cfSJeff Kirsher 	  "Winbond W89c840",	CanHaveMII | HasBrokenTx | FDXOnNoMII},
242a88394cfSJeff Kirsher 	{ "Winbond W89c840",	CanHaveMII | HasBrokenTx},
243a88394cfSJeff Kirsher 	{ "Compex RL100-ATX",	CanHaveMII | HasBrokenTx},
244a88394cfSJeff Kirsher 	{ }	/* terminate list. */
245a88394cfSJeff Kirsher };
246a88394cfSJeff Kirsher 
247a88394cfSJeff Kirsher /* This driver was written to use PCI memory space, however some x86 systems
248a88394cfSJeff Kirsher    work only with I/O space accesses. See CONFIG_TULIP_MMIO in .config
249a88394cfSJeff Kirsher */
250a88394cfSJeff Kirsher 
251a88394cfSJeff Kirsher /* Offsets to the Command and Status Registers, "CSRs".
252a88394cfSJeff Kirsher    While similar to the Tulip, these registers are longword aligned.
253a88394cfSJeff Kirsher    Note: It's not useful to define symbolic names for every register bit in
254a88394cfSJeff Kirsher    the device.  The name can only partially document the semantics and make
255a88394cfSJeff Kirsher    the driver longer and more difficult to read.
256a88394cfSJeff Kirsher */
257a88394cfSJeff Kirsher enum w840_offsets {
258a88394cfSJeff Kirsher 	PCIBusCfg=0x00, TxStartDemand=0x04, RxStartDemand=0x08,
259a88394cfSJeff Kirsher 	RxRingPtr=0x0C, TxRingPtr=0x10,
260a88394cfSJeff Kirsher 	IntrStatus=0x14, NetworkConfig=0x18, IntrEnable=0x1C,
261a88394cfSJeff Kirsher 	RxMissed=0x20, EECtrl=0x24, MIICtrl=0x24, BootRom=0x28, GPTimer=0x2C,
262a88394cfSJeff Kirsher 	CurRxDescAddr=0x30, CurRxBufAddr=0x34,			/* Debug use */
263a88394cfSJeff Kirsher 	MulticastFilter0=0x38, MulticastFilter1=0x3C, StationAddr=0x40,
264a88394cfSJeff Kirsher 	CurTxDescAddr=0x4C, CurTxBufAddr=0x50,
265a88394cfSJeff Kirsher };
266a88394cfSJeff Kirsher 
267a88394cfSJeff Kirsher /* Bits in the NetworkConfig register. */
268a88394cfSJeff Kirsher enum rx_mode_bits {
269a88394cfSJeff Kirsher 	AcceptErr=0x80,
270a88394cfSJeff Kirsher 	RxAcceptBroadcast=0x20, AcceptMulticast=0x10,
271a88394cfSJeff Kirsher 	RxAcceptAllPhys=0x08, AcceptMyPhys=0x02,
272a88394cfSJeff Kirsher };
273a88394cfSJeff Kirsher 
274a88394cfSJeff Kirsher enum mii_reg_bits {
275a88394cfSJeff Kirsher 	MDIO_ShiftClk=0x10000, MDIO_DataIn=0x80000, MDIO_DataOut=0x20000,
276a88394cfSJeff Kirsher 	MDIO_EnbOutput=0x40000, MDIO_EnbIn = 0x00000,
277a88394cfSJeff Kirsher };
278a88394cfSJeff Kirsher 
279a88394cfSJeff Kirsher /* The Tulip Rx and Tx buffer descriptors. */
280a88394cfSJeff Kirsher struct w840_rx_desc {
281a88394cfSJeff Kirsher 	s32 status;
282a88394cfSJeff Kirsher 	s32 length;
283a88394cfSJeff Kirsher 	u32 buffer1;
284a88394cfSJeff Kirsher 	u32 buffer2;
285a88394cfSJeff Kirsher };
286a88394cfSJeff Kirsher 
287a88394cfSJeff Kirsher struct w840_tx_desc {
288a88394cfSJeff Kirsher 	s32 status;
289a88394cfSJeff Kirsher 	s32 length;
290a88394cfSJeff Kirsher 	u32 buffer1, buffer2;
291a88394cfSJeff Kirsher };
292a88394cfSJeff Kirsher 
293a88394cfSJeff Kirsher #define MII_CNT		1 /* winbond only supports one MII */
294a88394cfSJeff Kirsher struct netdev_private {
295a88394cfSJeff Kirsher 	struct w840_rx_desc *rx_ring;
296a88394cfSJeff Kirsher 	dma_addr_t	rx_addr[RX_RING_SIZE];
297a88394cfSJeff Kirsher 	struct w840_tx_desc *tx_ring;
298a88394cfSJeff Kirsher 	dma_addr_t	tx_addr[TX_RING_SIZE];
299a88394cfSJeff Kirsher 	dma_addr_t ring_dma_addr;
300a88394cfSJeff Kirsher 	/* The addresses of receive-in-place skbuffs. */
301a88394cfSJeff Kirsher 	struct sk_buff* rx_skbuff[RX_RING_SIZE];
302a88394cfSJeff Kirsher 	/* The saved address of a sent-in-place packet/buffer, for later free(). */
303a88394cfSJeff Kirsher 	struct sk_buff* tx_skbuff[TX_RING_SIZE];
304a88394cfSJeff Kirsher 	struct net_device_stats stats;
305a88394cfSJeff Kirsher 	struct timer_list timer;	/* Media monitoring timer. */
306a88394cfSJeff Kirsher 	/* Frequently used values: keep some adjacent for cache effect. */
307a88394cfSJeff Kirsher 	spinlock_t lock;
308a88394cfSJeff Kirsher 	int chip_id, drv_flags;
309a88394cfSJeff Kirsher 	struct pci_dev *pci_dev;
310a88394cfSJeff Kirsher 	int csr6;
311a88394cfSJeff Kirsher 	struct w840_rx_desc *rx_head_desc;
312a88394cfSJeff Kirsher 	unsigned int cur_rx, dirty_rx;		/* Producer/consumer ring indices */
313a88394cfSJeff Kirsher 	unsigned int rx_buf_sz;				/* Based on MTU+slack. */
314a88394cfSJeff Kirsher 	unsigned int cur_tx, dirty_tx;
315a88394cfSJeff Kirsher 	unsigned int tx_q_bytes;
316a88394cfSJeff Kirsher 	unsigned int tx_full;				/* The Tx queue is full. */
317a88394cfSJeff Kirsher 	/* MII transceiver section. */
318a88394cfSJeff Kirsher 	int mii_cnt;						/* MII device addresses. */
319a88394cfSJeff Kirsher 	unsigned char phys[MII_CNT];		/* MII device addresses, but only the first is used */
320a88394cfSJeff Kirsher 	u32 mii;
321a88394cfSJeff Kirsher 	struct mii_if_info mii_if;
322a88394cfSJeff Kirsher 	void __iomem *base_addr;
323a88394cfSJeff Kirsher };
324a88394cfSJeff Kirsher 
325a88394cfSJeff Kirsher static int  eeprom_read(void __iomem *ioaddr, int location);
326a88394cfSJeff Kirsher static int  mdio_read(struct net_device *dev, int phy_id, int location);
327a88394cfSJeff Kirsher static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
328a88394cfSJeff Kirsher static int  netdev_open(struct net_device *dev);
329a88394cfSJeff Kirsher static int  update_link(struct net_device *dev);
330a88394cfSJeff Kirsher static void netdev_timer(unsigned long data);
331a88394cfSJeff Kirsher static void init_rxtx_rings(struct net_device *dev);
332a88394cfSJeff Kirsher static void free_rxtx_rings(struct netdev_private *np);
333a88394cfSJeff Kirsher static void init_registers(struct net_device *dev);
334a88394cfSJeff Kirsher static void tx_timeout(struct net_device *dev);
335a88394cfSJeff Kirsher static int alloc_ringdesc(struct net_device *dev);
336a88394cfSJeff Kirsher static void free_ringdesc(struct netdev_private *np);
337a88394cfSJeff Kirsher static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev);
338a88394cfSJeff Kirsher static irqreturn_t intr_handler(int irq, void *dev_instance);
339a88394cfSJeff Kirsher static void netdev_error(struct net_device *dev, int intr_status);
340a88394cfSJeff Kirsher static int  netdev_rx(struct net_device *dev);
341a88394cfSJeff Kirsher static u32 __set_rx_mode(struct net_device *dev);
342a88394cfSJeff Kirsher static void set_rx_mode(struct net_device *dev);
343a88394cfSJeff Kirsher static struct net_device_stats *get_stats(struct net_device *dev);
344a88394cfSJeff Kirsher static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
345a88394cfSJeff Kirsher static const struct ethtool_ops netdev_ethtool_ops;
346a88394cfSJeff Kirsher static int  netdev_close(struct net_device *dev);
347a88394cfSJeff Kirsher 
348a88394cfSJeff Kirsher static const struct net_device_ops netdev_ops = {
349a88394cfSJeff Kirsher 	.ndo_open		= netdev_open,
350a88394cfSJeff Kirsher 	.ndo_stop		= netdev_close,
351a88394cfSJeff Kirsher 	.ndo_start_xmit		= start_tx,
352a88394cfSJeff Kirsher 	.ndo_get_stats		= get_stats,
353afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= set_rx_mode,
354a88394cfSJeff Kirsher 	.ndo_do_ioctl		= netdev_ioctl,
355a88394cfSJeff Kirsher 	.ndo_tx_timeout		= tx_timeout,
356a88394cfSJeff Kirsher 	.ndo_change_mtu		= eth_change_mtu,
357a88394cfSJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
358a88394cfSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
359a88394cfSJeff Kirsher };
360a88394cfSJeff Kirsher 
361a88394cfSJeff Kirsher static int __devinit w840_probe1 (struct pci_dev *pdev,
362a88394cfSJeff Kirsher 				  const struct pci_device_id *ent)
363a88394cfSJeff Kirsher {
364a88394cfSJeff Kirsher 	struct net_device *dev;
365a88394cfSJeff Kirsher 	struct netdev_private *np;
366a88394cfSJeff Kirsher 	static int find_cnt;
367a88394cfSJeff Kirsher 	int chip_idx = ent->driver_data;
368a88394cfSJeff Kirsher 	int irq;
369a88394cfSJeff Kirsher 	int i, option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
370a88394cfSJeff Kirsher 	void __iomem *ioaddr;
371a88394cfSJeff Kirsher 
372a88394cfSJeff Kirsher 	i = pci_enable_device(pdev);
373a88394cfSJeff Kirsher 	if (i) return i;
374a88394cfSJeff Kirsher 
375a88394cfSJeff Kirsher 	pci_set_master(pdev);
376a88394cfSJeff Kirsher 
377a88394cfSJeff Kirsher 	irq = pdev->irq;
378a88394cfSJeff Kirsher 
379a88394cfSJeff Kirsher 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
380a88394cfSJeff Kirsher 		pr_warn("Device %s disabled due to DMA limitations\n",
381a88394cfSJeff Kirsher 			pci_name(pdev));
382a88394cfSJeff Kirsher 		return -EIO;
383a88394cfSJeff Kirsher 	}
384a88394cfSJeff Kirsher 	dev = alloc_etherdev(sizeof(*np));
385a88394cfSJeff Kirsher 	if (!dev)
386a88394cfSJeff Kirsher 		return -ENOMEM;
387a88394cfSJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
388a88394cfSJeff Kirsher 
389a88394cfSJeff Kirsher 	if (pci_request_regions(pdev, DRV_NAME))
390a88394cfSJeff Kirsher 		goto err_out_netdev;
391a88394cfSJeff Kirsher 
392a88394cfSJeff Kirsher 	ioaddr = pci_iomap(pdev, TULIP_BAR, netdev_res_size);
393a88394cfSJeff Kirsher 	if (!ioaddr)
394a88394cfSJeff Kirsher 		goto err_out_free_res;
395a88394cfSJeff Kirsher 
396a88394cfSJeff Kirsher 	for (i = 0; i < 3; i++)
397a88394cfSJeff Kirsher 		((__le16 *)dev->dev_addr)[i] = cpu_to_le16(eeprom_read(ioaddr, i));
398a88394cfSJeff Kirsher 
399a88394cfSJeff Kirsher 	/* Reset the chip to erase previous misconfiguration.
400a88394cfSJeff Kirsher 	   No hold time required! */
401a88394cfSJeff Kirsher 	iowrite32(0x00000001, ioaddr + PCIBusCfg);
402a88394cfSJeff Kirsher 
403a88394cfSJeff Kirsher 	dev->base_addr = (unsigned long)ioaddr;
404a88394cfSJeff Kirsher 	dev->irq = irq;
405a88394cfSJeff Kirsher 
406a88394cfSJeff Kirsher 	np = netdev_priv(dev);
407a88394cfSJeff Kirsher 	np->pci_dev = pdev;
408a88394cfSJeff Kirsher 	np->chip_id = chip_idx;
409a88394cfSJeff Kirsher 	np->drv_flags = pci_id_tbl[chip_idx].drv_flags;
410a88394cfSJeff Kirsher 	spin_lock_init(&np->lock);
411a88394cfSJeff Kirsher 	np->mii_if.dev = dev;
412a88394cfSJeff Kirsher 	np->mii_if.mdio_read = mdio_read;
413a88394cfSJeff Kirsher 	np->mii_if.mdio_write = mdio_write;
414a88394cfSJeff Kirsher 	np->base_addr = ioaddr;
415a88394cfSJeff Kirsher 
416a88394cfSJeff Kirsher 	pci_set_drvdata(pdev, dev);
417a88394cfSJeff Kirsher 
418a88394cfSJeff Kirsher 	if (dev->mem_start)
419a88394cfSJeff Kirsher 		option = dev->mem_start;
420a88394cfSJeff Kirsher 
421a88394cfSJeff Kirsher 	/* The lower four bits are the media type. */
422a88394cfSJeff Kirsher 	if (option > 0) {
423a88394cfSJeff Kirsher 		if (option & 0x200)
424a88394cfSJeff Kirsher 			np->mii_if.full_duplex = 1;
425a88394cfSJeff Kirsher 		if (option & 15)
426a88394cfSJeff Kirsher 			dev_info(&dev->dev,
427a88394cfSJeff Kirsher 				 "ignoring user supplied media type %d",
428a88394cfSJeff Kirsher 				 option & 15);
429a88394cfSJeff Kirsher 	}
430a88394cfSJeff Kirsher 	if (find_cnt < MAX_UNITS  &&  full_duplex[find_cnt] > 0)
431a88394cfSJeff Kirsher 		np->mii_if.full_duplex = 1;
432a88394cfSJeff Kirsher 
433a88394cfSJeff Kirsher 	if (np->mii_if.full_duplex)
434a88394cfSJeff Kirsher 		np->mii_if.force_media = 1;
435a88394cfSJeff Kirsher 
436a88394cfSJeff Kirsher 	/* The chip-specific entries in the device structure. */
437a88394cfSJeff Kirsher 	dev->netdev_ops = &netdev_ops;
438a88394cfSJeff Kirsher 	dev->ethtool_ops = &netdev_ethtool_ops;
439a88394cfSJeff Kirsher 	dev->watchdog_timeo = TX_TIMEOUT;
440a88394cfSJeff Kirsher 
441a88394cfSJeff Kirsher 	i = register_netdev(dev);
442a88394cfSJeff Kirsher 	if (i)
443a88394cfSJeff Kirsher 		goto err_out_cleardev;
444a88394cfSJeff Kirsher 
445a88394cfSJeff Kirsher 	dev_info(&dev->dev, "%s at %p, %pM, IRQ %d\n",
446a88394cfSJeff Kirsher 		 pci_id_tbl[chip_idx].name, ioaddr, dev->dev_addr, irq);
447a88394cfSJeff Kirsher 
448a88394cfSJeff Kirsher 	if (np->drv_flags & CanHaveMII) {
449a88394cfSJeff Kirsher 		int phy, phy_idx = 0;
450a88394cfSJeff Kirsher 		for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
451a88394cfSJeff Kirsher 			int mii_status = mdio_read(dev, phy, MII_BMSR);
452a88394cfSJeff Kirsher 			if (mii_status != 0xffff  &&  mii_status != 0x0000) {
453a88394cfSJeff Kirsher 				np->phys[phy_idx++] = phy;
454a88394cfSJeff Kirsher 				np->mii_if.advertising = mdio_read(dev, phy, MII_ADVERTISE);
455a88394cfSJeff Kirsher 				np->mii = (mdio_read(dev, phy, MII_PHYSID1) << 16)+
456a88394cfSJeff Kirsher 						mdio_read(dev, phy, MII_PHYSID2);
457a88394cfSJeff Kirsher 				dev_info(&dev->dev,
458a88394cfSJeff Kirsher 					 "MII PHY %08xh found at address %d, status 0x%04x advertising %04x\n",
459a88394cfSJeff Kirsher 					 np->mii, phy, mii_status,
460a88394cfSJeff Kirsher 					 np->mii_if.advertising);
461a88394cfSJeff Kirsher 			}
462a88394cfSJeff Kirsher 		}
463a88394cfSJeff Kirsher 		np->mii_cnt = phy_idx;
464a88394cfSJeff Kirsher 		np->mii_if.phy_id = np->phys[0];
465a88394cfSJeff Kirsher 		if (phy_idx == 0) {
466a88394cfSJeff Kirsher 			dev_warn(&dev->dev,
467a88394cfSJeff Kirsher 				 "MII PHY not found -- this device may not operate correctly\n");
468a88394cfSJeff Kirsher 		}
469a88394cfSJeff Kirsher 	}
470a88394cfSJeff Kirsher 
471a88394cfSJeff Kirsher 	find_cnt++;
472a88394cfSJeff Kirsher 	return 0;
473a88394cfSJeff Kirsher 
474a88394cfSJeff Kirsher err_out_cleardev:
475a88394cfSJeff Kirsher 	pci_set_drvdata(pdev, NULL);
476a88394cfSJeff Kirsher 	pci_iounmap(pdev, ioaddr);
477a88394cfSJeff Kirsher err_out_free_res:
478a88394cfSJeff Kirsher 	pci_release_regions(pdev);
479a88394cfSJeff Kirsher err_out_netdev:
480a88394cfSJeff Kirsher 	free_netdev (dev);
481a88394cfSJeff Kirsher 	return -ENODEV;
482a88394cfSJeff Kirsher }
483a88394cfSJeff Kirsher 
484a88394cfSJeff Kirsher 
485a88394cfSJeff Kirsher /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.  These are
486a88394cfSJeff Kirsher    often serial bit streams generated by the host processor.
487a88394cfSJeff Kirsher    The example below is for the common 93c46 EEPROM, 64 16 bit words. */
488a88394cfSJeff Kirsher 
489a88394cfSJeff Kirsher /* Delay between EEPROM clock transitions.
490a88394cfSJeff Kirsher    No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
491a88394cfSJeff Kirsher    a delay.  Note that pre-2.0.34 kernels had a cache-alignment bug that
492a88394cfSJeff Kirsher    made udelay() unreliable.
493a88394cfSJeff Kirsher    The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
494a88394cfSJeff Kirsher    deprecated.
495a88394cfSJeff Kirsher */
496a88394cfSJeff Kirsher #define eeprom_delay(ee_addr)	ioread32(ee_addr)
497a88394cfSJeff Kirsher 
498a88394cfSJeff Kirsher enum EEPROM_Ctrl_Bits {
499a88394cfSJeff Kirsher 	EE_ShiftClk=0x02, EE_Write0=0x801, EE_Write1=0x805,
500a88394cfSJeff Kirsher 	EE_ChipSelect=0x801, EE_DataIn=0x08,
501a88394cfSJeff Kirsher };
502a88394cfSJeff Kirsher 
503a88394cfSJeff Kirsher /* The EEPROM commands include the alway-set leading bit. */
504a88394cfSJeff Kirsher enum EEPROM_Cmds {
505a88394cfSJeff Kirsher 	EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
506a88394cfSJeff Kirsher };
507a88394cfSJeff Kirsher 
508a88394cfSJeff Kirsher static int eeprom_read(void __iomem *addr, int location)
509a88394cfSJeff Kirsher {
510a88394cfSJeff Kirsher 	int i;
511a88394cfSJeff Kirsher 	int retval = 0;
512a88394cfSJeff Kirsher 	void __iomem *ee_addr = addr + EECtrl;
513a88394cfSJeff Kirsher 	int read_cmd = location | EE_ReadCmd;
514a88394cfSJeff Kirsher 	iowrite32(EE_ChipSelect, ee_addr);
515a88394cfSJeff Kirsher 
516a88394cfSJeff Kirsher 	/* Shift the read command bits out. */
517a88394cfSJeff Kirsher 	for (i = 10; i >= 0; i--) {
518a88394cfSJeff Kirsher 		short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
519a88394cfSJeff Kirsher 		iowrite32(dataval, ee_addr);
520a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
521a88394cfSJeff Kirsher 		iowrite32(dataval | EE_ShiftClk, ee_addr);
522a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
523a88394cfSJeff Kirsher 	}
524a88394cfSJeff Kirsher 	iowrite32(EE_ChipSelect, ee_addr);
525a88394cfSJeff Kirsher 	eeprom_delay(ee_addr);
526a88394cfSJeff Kirsher 
527a88394cfSJeff Kirsher 	for (i = 16; i > 0; i--) {
528a88394cfSJeff Kirsher 		iowrite32(EE_ChipSelect | EE_ShiftClk, ee_addr);
529a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
530a88394cfSJeff Kirsher 		retval = (retval << 1) | ((ioread32(ee_addr) & EE_DataIn) ? 1 : 0);
531a88394cfSJeff Kirsher 		iowrite32(EE_ChipSelect, ee_addr);
532a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
533a88394cfSJeff Kirsher 	}
534a88394cfSJeff Kirsher 
535a88394cfSJeff Kirsher 	/* Terminate the EEPROM access. */
536a88394cfSJeff Kirsher 	iowrite32(0, ee_addr);
537a88394cfSJeff Kirsher 	return retval;
538a88394cfSJeff Kirsher }
539a88394cfSJeff Kirsher 
540a88394cfSJeff Kirsher /*  MII transceiver control section.
541a88394cfSJeff Kirsher 	Read and write the MII registers using software-generated serial
542a88394cfSJeff Kirsher 	MDIO protocol.  See the MII specifications or DP83840A data sheet
543a88394cfSJeff Kirsher 	for details.
544a88394cfSJeff Kirsher 
545a88394cfSJeff Kirsher 	The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
546a88394cfSJeff Kirsher 	met by back-to-back 33Mhz PCI cycles. */
547a88394cfSJeff Kirsher #define mdio_delay(mdio_addr) ioread32(mdio_addr)
548a88394cfSJeff Kirsher 
549a88394cfSJeff Kirsher /* Set iff a MII transceiver on any interface requires mdio preamble.
550a88394cfSJeff Kirsher    This only set with older transceivers, so the extra
551a88394cfSJeff Kirsher    code size of a per-interface flag is not worthwhile. */
552a88394cfSJeff Kirsher static char mii_preamble_required = 1;
553a88394cfSJeff Kirsher 
554a88394cfSJeff Kirsher #define MDIO_WRITE0 (MDIO_EnbOutput)
555a88394cfSJeff Kirsher #define MDIO_WRITE1 (MDIO_DataOut | MDIO_EnbOutput)
556a88394cfSJeff Kirsher 
557a88394cfSJeff Kirsher /* Generate the preamble required for initial synchronization and
558a88394cfSJeff Kirsher    a few older transceivers. */
559a88394cfSJeff Kirsher static void mdio_sync(void __iomem *mdio_addr)
560a88394cfSJeff Kirsher {
561a88394cfSJeff Kirsher 	int bits = 32;
562a88394cfSJeff Kirsher 
563a88394cfSJeff Kirsher 	/* Establish sync by sending at least 32 logic ones. */
564a88394cfSJeff Kirsher 	while (--bits >= 0) {
565a88394cfSJeff Kirsher 		iowrite32(MDIO_WRITE1, mdio_addr);
566a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
567a88394cfSJeff Kirsher 		iowrite32(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
568a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
569a88394cfSJeff Kirsher 	}
570a88394cfSJeff Kirsher }
571a88394cfSJeff Kirsher 
572a88394cfSJeff Kirsher static int mdio_read(struct net_device *dev, int phy_id, int location)
573a88394cfSJeff Kirsher {
574a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
575a88394cfSJeff Kirsher 	void __iomem *mdio_addr = np->base_addr + MIICtrl;
576a88394cfSJeff Kirsher 	int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
577a88394cfSJeff Kirsher 	int i, retval = 0;
578a88394cfSJeff Kirsher 
579a88394cfSJeff Kirsher 	if (mii_preamble_required)
580a88394cfSJeff Kirsher 		mdio_sync(mdio_addr);
581a88394cfSJeff Kirsher 
582a88394cfSJeff Kirsher 	/* Shift the read command bits out. */
583a88394cfSJeff Kirsher 	for (i = 15; i >= 0; i--) {
584a88394cfSJeff Kirsher 		int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
585a88394cfSJeff Kirsher 
586a88394cfSJeff Kirsher 		iowrite32(dataval, mdio_addr);
587a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
588a88394cfSJeff Kirsher 		iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
589a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
590a88394cfSJeff Kirsher 	}
591a88394cfSJeff Kirsher 	/* Read the two transition, 16 data, and wire-idle bits. */
592a88394cfSJeff Kirsher 	for (i = 20; i > 0; i--) {
593a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn, mdio_addr);
594a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
595a88394cfSJeff Kirsher 		retval = (retval << 1) | ((ioread32(mdio_addr) & MDIO_DataIn) ? 1 : 0);
596a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
597a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
598a88394cfSJeff Kirsher 	}
599a88394cfSJeff Kirsher 	return (retval>>1) & 0xffff;
600a88394cfSJeff Kirsher }
601a88394cfSJeff Kirsher 
602a88394cfSJeff Kirsher static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
603a88394cfSJeff Kirsher {
604a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
605a88394cfSJeff Kirsher 	void __iomem *mdio_addr = np->base_addr + MIICtrl;
606a88394cfSJeff Kirsher 	int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
607a88394cfSJeff Kirsher 	int i;
608a88394cfSJeff Kirsher 
609a88394cfSJeff Kirsher 	if (location == 4  &&  phy_id == np->phys[0])
610a88394cfSJeff Kirsher 		np->mii_if.advertising = value;
611a88394cfSJeff Kirsher 
612a88394cfSJeff Kirsher 	if (mii_preamble_required)
613a88394cfSJeff Kirsher 		mdio_sync(mdio_addr);
614a88394cfSJeff Kirsher 
615a88394cfSJeff Kirsher 	/* Shift the command bits out. */
616a88394cfSJeff Kirsher 	for (i = 31; i >= 0; i--) {
617a88394cfSJeff Kirsher 		int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
618a88394cfSJeff Kirsher 
619a88394cfSJeff Kirsher 		iowrite32(dataval, mdio_addr);
620a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
621a88394cfSJeff Kirsher 		iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
622a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
623a88394cfSJeff Kirsher 	}
624a88394cfSJeff Kirsher 	/* Clear out extra bits. */
625a88394cfSJeff Kirsher 	for (i = 2; i > 0; i--) {
626a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn, mdio_addr);
627a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
628a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
629a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
630a88394cfSJeff Kirsher 	}
631a88394cfSJeff Kirsher }
632a88394cfSJeff Kirsher 
633a88394cfSJeff Kirsher 
634a88394cfSJeff Kirsher static int netdev_open(struct net_device *dev)
635a88394cfSJeff Kirsher {
636a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
637a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
638a88394cfSJeff Kirsher 	int i;
639a88394cfSJeff Kirsher 
640a88394cfSJeff Kirsher 	iowrite32(0x00000001, ioaddr + PCIBusCfg);		/* Reset */
641a88394cfSJeff Kirsher 
642a88394cfSJeff Kirsher 	netif_device_detach(dev);
643a88394cfSJeff Kirsher 	i = request_irq(dev->irq, intr_handler, IRQF_SHARED, dev->name, dev);
644a88394cfSJeff Kirsher 	if (i)
645a88394cfSJeff Kirsher 		goto out_err;
646a88394cfSJeff Kirsher 
647a88394cfSJeff Kirsher 	if (debug > 1)
648a88394cfSJeff Kirsher 		netdev_dbg(dev, "w89c840_open() irq %d\n", dev->irq);
649a88394cfSJeff Kirsher 
650a88394cfSJeff Kirsher 	if((i=alloc_ringdesc(dev)))
651a88394cfSJeff Kirsher 		goto out_err;
652a88394cfSJeff Kirsher 
653a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
654a88394cfSJeff Kirsher 	netif_device_attach(dev);
655a88394cfSJeff Kirsher 	init_registers(dev);
656a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
657a88394cfSJeff Kirsher 
658a88394cfSJeff Kirsher 	netif_start_queue(dev);
659a88394cfSJeff Kirsher 	if (debug > 2)
660a88394cfSJeff Kirsher 		netdev_dbg(dev, "Done netdev_open()\n");
661a88394cfSJeff Kirsher 
662a88394cfSJeff Kirsher 	/* Set the timer to check for link beat. */
663a88394cfSJeff Kirsher 	init_timer(&np->timer);
664a88394cfSJeff Kirsher 	np->timer.expires = jiffies + 1*HZ;
665a88394cfSJeff Kirsher 	np->timer.data = (unsigned long)dev;
666a88394cfSJeff Kirsher 	np->timer.function = netdev_timer;				/* timer handler */
667a88394cfSJeff Kirsher 	add_timer(&np->timer);
668a88394cfSJeff Kirsher 	return 0;
669a88394cfSJeff Kirsher out_err:
670a88394cfSJeff Kirsher 	netif_device_attach(dev);
671a88394cfSJeff Kirsher 	return i;
672a88394cfSJeff Kirsher }
673a88394cfSJeff Kirsher 
674a88394cfSJeff Kirsher #define MII_DAVICOM_DM9101	0x0181b800
675a88394cfSJeff Kirsher 
676a88394cfSJeff Kirsher static int update_link(struct net_device *dev)
677a88394cfSJeff Kirsher {
678a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
679a88394cfSJeff Kirsher 	int duplex, fasteth, result, mii_reg;
680a88394cfSJeff Kirsher 
681a88394cfSJeff Kirsher 	/* BSMR */
682a88394cfSJeff Kirsher 	mii_reg = mdio_read(dev, np->phys[0], MII_BMSR);
683a88394cfSJeff Kirsher 
684a88394cfSJeff Kirsher 	if (mii_reg == 0xffff)
685a88394cfSJeff Kirsher 		return np->csr6;
686a88394cfSJeff Kirsher 	/* reread: the link status bit is sticky */
687a88394cfSJeff Kirsher 	mii_reg = mdio_read(dev, np->phys[0], MII_BMSR);
688a88394cfSJeff Kirsher 	if (!(mii_reg & 0x4)) {
689a88394cfSJeff Kirsher 		if (netif_carrier_ok(dev)) {
690a88394cfSJeff Kirsher 			if (debug)
691a88394cfSJeff Kirsher 				dev_info(&dev->dev,
692a88394cfSJeff Kirsher 					 "MII #%d reports no link. Disabling watchdog\n",
693a88394cfSJeff Kirsher 					 np->phys[0]);
694a88394cfSJeff Kirsher 			netif_carrier_off(dev);
695a88394cfSJeff Kirsher 		}
696a88394cfSJeff Kirsher 		return np->csr6;
697a88394cfSJeff Kirsher 	}
698a88394cfSJeff Kirsher 	if (!netif_carrier_ok(dev)) {
699a88394cfSJeff Kirsher 		if (debug)
700a88394cfSJeff Kirsher 			dev_info(&dev->dev,
701a88394cfSJeff Kirsher 				 "MII #%d link is back. Enabling watchdog\n",
702a88394cfSJeff Kirsher 				 np->phys[0]);
703a88394cfSJeff Kirsher 		netif_carrier_on(dev);
704a88394cfSJeff Kirsher 	}
705a88394cfSJeff Kirsher 
706a88394cfSJeff Kirsher 	if ((np->mii & ~0xf) == MII_DAVICOM_DM9101) {
707a88394cfSJeff Kirsher 		/* If the link partner doesn't support autonegotiation
708a88394cfSJeff Kirsher 		 * the MII detects it's abilities with the "parallel detection".
709a88394cfSJeff Kirsher 		 * Some MIIs update the LPA register to the result of the parallel
710a88394cfSJeff Kirsher 		 * detection, some don't.
711a88394cfSJeff Kirsher 		 * The Davicom PHY [at least 0181b800] doesn't.
712a88394cfSJeff Kirsher 		 * Instead bit 9 and 13 of the BMCR are updated to the result
713a88394cfSJeff Kirsher 		 * of the negotiation..
714a88394cfSJeff Kirsher 		 */
715a88394cfSJeff Kirsher 		mii_reg = mdio_read(dev, np->phys[0], MII_BMCR);
716a88394cfSJeff Kirsher 		duplex = mii_reg & BMCR_FULLDPLX;
717a88394cfSJeff Kirsher 		fasteth = mii_reg & BMCR_SPEED100;
718a88394cfSJeff Kirsher 	} else {
719a88394cfSJeff Kirsher 		int negotiated;
720a88394cfSJeff Kirsher 		mii_reg	= mdio_read(dev, np->phys[0], MII_LPA);
721a88394cfSJeff Kirsher 		negotiated = mii_reg & np->mii_if.advertising;
722a88394cfSJeff Kirsher 
723a88394cfSJeff Kirsher 		duplex = (negotiated & LPA_100FULL) || ((negotiated & 0x02C0) == LPA_10FULL);
724a88394cfSJeff Kirsher 		fasteth = negotiated & 0x380;
725a88394cfSJeff Kirsher 	}
726a88394cfSJeff Kirsher 	duplex |= np->mii_if.force_media;
727a88394cfSJeff Kirsher 	/* remove fastether and fullduplex */
728a88394cfSJeff Kirsher 	result = np->csr6 & ~0x20000200;
729a88394cfSJeff Kirsher 	if (duplex)
730a88394cfSJeff Kirsher 		result |= 0x200;
731a88394cfSJeff Kirsher 	if (fasteth)
732a88394cfSJeff Kirsher 		result |= 0x20000000;
733a88394cfSJeff Kirsher 	if (result != np->csr6 && debug)
734a88394cfSJeff Kirsher 		dev_info(&dev->dev,
735a88394cfSJeff Kirsher 			 "Setting %dMBit-%s-duplex based on MII#%d\n",
736a88394cfSJeff Kirsher 			 fasteth ? 100 : 10, duplex ? "full" : "half",
737a88394cfSJeff Kirsher 			 np->phys[0]);
738a88394cfSJeff Kirsher 	return result;
739a88394cfSJeff Kirsher }
740a88394cfSJeff Kirsher 
741a88394cfSJeff Kirsher #define RXTX_TIMEOUT	2000
742a88394cfSJeff Kirsher static inline void update_csr6(struct net_device *dev, int new)
743a88394cfSJeff Kirsher {
744a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
745a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
746a88394cfSJeff Kirsher 	int limit = RXTX_TIMEOUT;
747a88394cfSJeff Kirsher 
748a88394cfSJeff Kirsher 	if (!netif_device_present(dev))
749a88394cfSJeff Kirsher 		new = 0;
750a88394cfSJeff Kirsher 	if (new==np->csr6)
751a88394cfSJeff Kirsher 		return;
752a88394cfSJeff Kirsher 	/* stop both Tx and Rx processes */
753a88394cfSJeff Kirsher 	iowrite32(np->csr6 & ~0x2002, ioaddr + NetworkConfig);
754a88394cfSJeff Kirsher 	/* wait until they have really stopped */
755a88394cfSJeff Kirsher 	for (;;) {
756a88394cfSJeff Kirsher 		int csr5 = ioread32(ioaddr + IntrStatus);
757a88394cfSJeff Kirsher 		int t;
758a88394cfSJeff Kirsher 
759a88394cfSJeff Kirsher 		t = (csr5 >> 17) & 0x07;
760a88394cfSJeff Kirsher 		if (t==0||t==1) {
761a88394cfSJeff Kirsher 			/* rx stopped */
762a88394cfSJeff Kirsher 			t = (csr5 >> 20) & 0x07;
763a88394cfSJeff Kirsher 			if (t==0||t==1)
764a88394cfSJeff Kirsher 				break;
765a88394cfSJeff Kirsher 		}
766a88394cfSJeff Kirsher 
767a88394cfSJeff Kirsher 		limit--;
768a88394cfSJeff Kirsher 		if(!limit) {
769a88394cfSJeff Kirsher 			dev_info(&dev->dev,
770a88394cfSJeff Kirsher 				 "couldn't stop rxtx, IntrStatus %xh\n", csr5);
771a88394cfSJeff Kirsher 			break;
772a88394cfSJeff Kirsher 		}
773a88394cfSJeff Kirsher 		udelay(1);
774a88394cfSJeff Kirsher 	}
775a88394cfSJeff Kirsher 	np->csr6 = new;
776a88394cfSJeff Kirsher 	/* and restart them with the new configuration */
777a88394cfSJeff Kirsher 	iowrite32(np->csr6, ioaddr + NetworkConfig);
778a88394cfSJeff Kirsher 	if (new & 0x200)
779a88394cfSJeff Kirsher 		np->mii_if.full_duplex = 1;
780a88394cfSJeff Kirsher }
781a88394cfSJeff Kirsher 
782a88394cfSJeff Kirsher static void netdev_timer(unsigned long data)
783a88394cfSJeff Kirsher {
784a88394cfSJeff Kirsher 	struct net_device *dev = (struct net_device *)data;
785a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
786a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
787a88394cfSJeff Kirsher 
788a88394cfSJeff Kirsher 	if (debug > 2)
789a88394cfSJeff Kirsher 		netdev_dbg(dev, "Media selection timer tick, status %08x config %08x\n",
790a88394cfSJeff Kirsher 			   ioread32(ioaddr + IntrStatus),
791a88394cfSJeff Kirsher 			   ioread32(ioaddr + NetworkConfig));
792a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
793a88394cfSJeff Kirsher 	update_csr6(dev, update_link(dev));
794a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
795a88394cfSJeff Kirsher 	np->timer.expires = jiffies + 10*HZ;
796a88394cfSJeff Kirsher 	add_timer(&np->timer);
797a88394cfSJeff Kirsher }
798a88394cfSJeff Kirsher 
799a88394cfSJeff Kirsher static void init_rxtx_rings(struct net_device *dev)
800a88394cfSJeff Kirsher {
801a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
802a88394cfSJeff Kirsher 	int i;
803a88394cfSJeff Kirsher 
804a88394cfSJeff Kirsher 	np->rx_head_desc = &np->rx_ring[0];
805a88394cfSJeff Kirsher 	np->tx_ring = (struct w840_tx_desc*)&np->rx_ring[RX_RING_SIZE];
806a88394cfSJeff Kirsher 
807a88394cfSJeff Kirsher 	/* Initial all Rx descriptors. */
808a88394cfSJeff Kirsher 	for (i = 0; i < RX_RING_SIZE; i++) {
809a88394cfSJeff Kirsher 		np->rx_ring[i].length = np->rx_buf_sz;
810a88394cfSJeff Kirsher 		np->rx_ring[i].status = 0;
811a88394cfSJeff Kirsher 		np->rx_skbuff[i] = NULL;
812a88394cfSJeff Kirsher 	}
813a88394cfSJeff Kirsher 	/* Mark the last entry as wrapping the ring. */
814a88394cfSJeff Kirsher 	np->rx_ring[i-1].length |= DescEndRing;
815a88394cfSJeff Kirsher 
816a88394cfSJeff Kirsher 	/* Fill in the Rx buffers.  Handle allocation failure gracefully. */
817a88394cfSJeff Kirsher 	for (i = 0; i < RX_RING_SIZE; i++) {
81821a4e469SPradeep A Dalvi 		struct sk_buff *skb = netdev_alloc_skb(dev, np->rx_buf_sz);
819a88394cfSJeff Kirsher 		np->rx_skbuff[i] = skb;
820a88394cfSJeff Kirsher 		if (skb == NULL)
821a88394cfSJeff Kirsher 			break;
822a88394cfSJeff Kirsher 		np->rx_addr[i] = pci_map_single(np->pci_dev,skb->data,
823a88394cfSJeff Kirsher 					np->rx_buf_sz,PCI_DMA_FROMDEVICE);
824a88394cfSJeff Kirsher 
825a88394cfSJeff Kirsher 		np->rx_ring[i].buffer1 = np->rx_addr[i];
826a88394cfSJeff Kirsher 		np->rx_ring[i].status = DescOwned;
827a88394cfSJeff Kirsher 	}
828a88394cfSJeff Kirsher 
829a88394cfSJeff Kirsher 	np->cur_rx = 0;
830a88394cfSJeff Kirsher 	np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
831a88394cfSJeff Kirsher 
832a88394cfSJeff Kirsher 	/* Initialize the Tx descriptors */
833a88394cfSJeff Kirsher 	for (i = 0; i < TX_RING_SIZE; i++) {
834a88394cfSJeff Kirsher 		np->tx_skbuff[i] = NULL;
835a88394cfSJeff Kirsher 		np->tx_ring[i].status = 0;
836a88394cfSJeff Kirsher 	}
837a88394cfSJeff Kirsher 	np->tx_full = 0;
838a88394cfSJeff Kirsher 	np->tx_q_bytes = np->dirty_tx = np->cur_tx = 0;
839a88394cfSJeff Kirsher 
840a88394cfSJeff Kirsher 	iowrite32(np->ring_dma_addr, np->base_addr + RxRingPtr);
841a88394cfSJeff Kirsher 	iowrite32(np->ring_dma_addr+sizeof(struct w840_rx_desc)*RX_RING_SIZE,
842a88394cfSJeff Kirsher 		np->base_addr + TxRingPtr);
843a88394cfSJeff Kirsher 
844a88394cfSJeff Kirsher }
845a88394cfSJeff Kirsher 
846a88394cfSJeff Kirsher static void free_rxtx_rings(struct netdev_private* np)
847a88394cfSJeff Kirsher {
848a88394cfSJeff Kirsher 	int i;
849a88394cfSJeff Kirsher 	/* Free all the skbuffs in the Rx queue. */
850a88394cfSJeff Kirsher 	for (i = 0; i < RX_RING_SIZE; i++) {
851a88394cfSJeff Kirsher 		np->rx_ring[i].status = 0;
852a88394cfSJeff Kirsher 		if (np->rx_skbuff[i]) {
853a88394cfSJeff Kirsher 			pci_unmap_single(np->pci_dev,
854a88394cfSJeff Kirsher 						np->rx_addr[i],
855a88394cfSJeff Kirsher 						np->rx_skbuff[i]->len,
856a88394cfSJeff Kirsher 						PCI_DMA_FROMDEVICE);
857a88394cfSJeff Kirsher 			dev_kfree_skb(np->rx_skbuff[i]);
858a88394cfSJeff Kirsher 		}
859a88394cfSJeff Kirsher 		np->rx_skbuff[i] = NULL;
860a88394cfSJeff Kirsher 	}
861a88394cfSJeff Kirsher 	for (i = 0; i < TX_RING_SIZE; i++) {
862a88394cfSJeff Kirsher 		if (np->tx_skbuff[i]) {
863a88394cfSJeff Kirsher 			pci_unmap_single(np->pci_dev,
864a88394cfSJeff Kirsher 						np->tx_addr[i],
865a88394cfSJeff Kirsher 						np->tx_skbuff[i]->len,
866a88394cfSJeff Kirsher 						PCI_DMA_TODEVICE);
867a88394cfSJeff Kirsher 			dev_kfree_skb(np->tx_skbuff[i]);
868a88394cfSJeff Kirsher 		}
869a88394cfSJeff Kirsher 		np->tx_skbuff[i] = NULL;
870a88394cfSJeff Kirsher 	}
871a88394cfSJeff Kirsher }
872a88394cfSJeff Kirsher 
873a88394cfSJeff Kirsher static void init_registers(struct net_device *dev)
874a88394cfSJeff Kirsher {
875a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
876a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
877a88394cfSJeff Kirsher 	int i;
878a88394cfSJeff Kirsher 
879a88394cfSJeff Kirsher 	for (i = 0; i < 6; i++)
880a88394cfSJeff Kirsher 		iowrite8(dev->dev_addr[i], ioaddr + StationAddr + i);
881a88394cfSJeff Kirsher 
882a88394cfSJeff Kirsher 	/* Initialize other registers. */
883a88394cfSJeff Kirsher #ifdef __BIG_ENDIAN
884a88394cfSJeff Kirsher 	i = (1<<20);	/* Big-endian descriptors */
885a88394cfSJeff Kirsher #else
886a88394cfSJeff Kirsher 	i = 0;
887a88394cfSJeff Kirsher #endif
888a88394cfSJeff Kirsher 	i |= (0x04<<2);		/* skip length 4 u32 */
889a88394cfSJeff Kirsher 	i |= 0x02;		/* give Rx priority */
890a88394cfSJeff Kirsher 
891a88394cfSJeff Kirsher 	/* Configure the PCI bus bursts and FIFO thresholds.
892a88394cfSJeff Kirsher 	   486: Set 8 longword cache alignment, 8 longword burst.
893a88394cfSJeff Kirsher 	   586: Set 16 longword cache alignment, no burst limit.
894a88394cfSJeff Kirsher 	   Cache alignment bits 15:14	     Burst length 13:8
895a88394cfSJeff Kirsher 		0000	<not allowed> 		0000 align to cache	0800 8 longwords
896a88394cfSJeff Kirsher 		4000	8  longwords		0100 1 longword		1000 16 longwords
897a88394cfSJeff Kirsher 		8000	16 longwords		0200 2 longwords	2000 32 longwords
898a88394cfSJeff Kirsher 		C000	32  longwords		0400 4 longwords */
899a88394cfSJeff Kirsher 
900a88394cfSJeff Kirsher #if defined (__i386__) && !defined(MODULE)
901a88394cfSJeff Kirsher 	/* When not a module we can work around broken '486 PCI boards. */
902a88394cfSJeff Kirsher 	if (boot_cpu_data.x86 <= 4) {
903a88394cfSJeff Kirsher 		i |= 0x4800;
904a88394cfSJeff Kirsher 		dev_info(&dev->dev,
905a88394cfSJeff Kirsher 			 "This is a 386/486 PCI system, setting cache alignment to 8 longwords\n");
906a88394cfSJeff Kirsher 	} else {
907a88394cfSJeff Kirsher 		i |= 0xE000;
908a88394cfSJeff Kirsher 	}
909a88394cfSJeff Kirsher #elif defined(__powerpc__) || defined(__i386__) || defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
910a88394cfSJeff Kirsher 	i |= 0xE000;
911a88394cfSJeff Kirsher #elif defined(CONFIG_SPARC) || defined (CONFIG_PARISC)
912a88394cfSJeff Kirsher 	i |= 0x4800;
913a88394cfSJeff Kirsher #else
914a88394cfSJeff Kirsher #warning Processor architecture undefined
915a88394cfSJeff Kirsher 	i |= 0x4800;
916a88394cfSJeff Kirsher #endif
917a88394cfSJeff Kirsher 	iowrite32(i, ioaddr + PCIBusCfg);
918a88394cfSJeff Kirsher 
919a88394cfSJeff Kirsher 	np->csr6 = 0;
920a88394cfSJeff Kirsher 	/* 128 byte Tx threshold;
921a88394cfSJeff Kirsher 		Transmit on; Receive on; */
922a88394cfSJeff Kirsher 	update_csr6(dev, 0x00022002 | update_link(dev) | __set_rx_mode(dev));
923a88394cfSJeff Kirsher 
924a88394cfSJeff Kirsher 	/* Clear and Enable interrupts by setting the interrupt mask. */
925a88394cfSJeff Kirsher 	iowrite32(0x1A0F5, ioaddr + IntrStatus);
926a88394cfSJeff Kirsher 	iowrite32(0x1A0F5, ioaddr + IntrEnable);
927a88394cfSJeff Kirsher 
928a88394cfSJeff Kirsher 	iowrite32(0, ioaddr + RxStartDemand);
929a88394cfSJeff Kirsher }
930a88394cfSJeff Kirsher 
931a88394cfSJeff Kirsher static void tx_timeout(struct net_device *dev)
932a88394cfSJeff Kirsher {
933a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
934a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
935a88394cfSJeff Kirsher 
936a88394cfSJeff Kirsher 	dev_warn(&dev->dev, "Transmit timed out, status %08x, resetting...\n",
937a88394cfSJeff Kirsher 		 ioread32(ioaddr + IntrStatus));
938a88394cfSJeff Kirsher 
939a88394cfSJeff Kirsher 	{
940a88394cfSJeff Kirsher 		int i;
941a88394cfSJeff Kirsher 		printk(KERN_DEBUG "  Rx ring %p: ", np->rx_ring);
942a88394cfSJeff Kirsher 		for (i = 0; i < RX_RING_SIZE; i++)
943a88394cfSJeff Kirsher 			printk(KERN_CONT " %08x", (unsigned int)np->rx_ring[i].status);
944a88394cfSJeff Kirsher 		printk(KERN_CONT "\n");
945a88394cfSJeff Kirsher 		printk(KERN_DEBUG "  Tx ring %p: ", np->tx_ring);
946a88394cfSJeff Kirsher 		for (i = 0; i < TX_RING_SIZE; i++)
947a88394cfSJeff Kirsher 			printk(KERN_CONT " %08x", np->tx_ring[i].status);
948a88394cfSJeff Kirsher 		printk(KERN_CONT "\n");
949a88394cfSJeff Kirsher 	}
950a88394cfSJeff Kirsher 	printk(KERN_DEBUG "Tx cur %d Tx dirty %d Tx Full %d, q bytes %d\n",
951a88394cfSJeff Kirsher 	       np->cur_tx, np->dirty_tx, np->tx_full, np->tx_q_bytes);
952a88394cfSJeff Kirsher 	printk(KERN_DEBUG "Tx Descriptor addr %xh\n", ioread32(ioaddr+0x4C));
953a88394cfSJeff Kirsher 
954a88394cfSJeff Kirsher 	disable_irq(dev->irq);
955a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
956a88394cfSJeff Kirsher 	/*
957a88394cfSJeff Kirsher 	 * Under high load dirty_tx and the internal tx descriptor pointer
958a88394cfSJeff Kirsher 	 * come out of sync, thus perform a software reset and reinitialize
959a88394cfSJeff Kirsher 	 * everything.
960a88394cfSJeff Kirsher 	 */
961a88394cfSJeff Kirsher 
962a88394cfSJeff Kirsher 	iowrite32(1, np->base_addr+PCIBusCfg);
963a88394cfSJeff Kirsher 	udelay(1);
964a88394cfSJeff Kirsher 
965a88394cfSJeff Kirsher 	free_rxtx_rings(np);
966a88394cfSJeff Kirsher 	init_rxtx_rings(dev);
967a88394cfSJeff Kirsher 	init_registers(dev);
968a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
969a88394cfSJeff Kirsher 	enable_irq(dev->irq);
970a88394cfSJeff Kirsher 
971a88394cfSJeff Kirsher 	netif_wake_queue(dev);
972a88394cfSJeff Kirsher 	dev->trans_start = jiffies; /* prevent tx timeout */
973a88394cfSJeff Kirsher 	np->stats.tx_errors++;
974a88394cfSJeff Kirsher }
975a88394cfSJeff Kirsher 
976a88394cfSJeff Kirsher /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
977a88394cfSJeff Kirsher static int alloc_ringdesc(struct net_device *dev)
978a88394cfSJeff Kirsher {
979a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
980a88394cfSJeff Kirsher 
981a88394cfSJeff Kirsher 	np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
982a88394cfSJeff Kirsher 
983a88394cfSJeff Kirsher 	np->rx_ring = pci_alloc_consistent(np->pci_dev,
984a88394cfSJeff Kirsher 			sizeof(struct w840_rx_desc)*RX_RING_SIZE +
985a88394cfSJeff Kirsher 			sizeof(struct w840_tx_desc)*TX_RING_SIZE,
986a88394cfSJeff Kirsher 			&np->ring_dma_addr);
987a88394cfSJeff Kirsher 	if(!np->rx_ring)
988a88394cfSJeff Kirsher 		return -ENOMEM;
989a88394cfSJeff Kirsher 	init_rxtx_rings(dev);
990a88394cfSJeff Kirsher 	return 0;
991a88394cfSJeff Kirsher }
992a88394cfSJeff Kirsher 
993a88394cfSJeff Kirsher static void free_ringdesc(struct netdev_private *np)
994a88394cfSJeff Kirsher {
995a88394cfSJeff Kirsher 	pci_free_consistent(np->pci_dev,
996a88394cfSJeff Kirsher 			sizeof(struct w840_rx_desc)*RX_RING_SIZE +
997a88394cfSJeff Kirsher 			sizeof(struct w840_tx_desc)*TX_RING_SIZE,
998a88394cfSJeff Kirsher 			np->rx_ring, np->ring_dma_addr);
999a88394cfSJeff Kirsher 
1000a88394cfSJeff Kirsher }
1001a88394cfSJeff Kirsher 
1002a88394cfSJeff Kirsher static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev)
1003a88394cfSJeff Kirsher {
1004a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1005a88394cfSJeff Kirsher 	unsigned entry;
1006a88394cfSJeff Kirsher 
1007a88394cfSJeff Kirsher 	/* Caution: the write order is important here, set the field
1008a88394cfSJeff Kirsher 	   with the "ownership" bits last. */
1009a88394cfSJeff Kirsher 
1010a88394cfSJeff Kirsher 	/* Calculate the next Tx descriptor entry. */
1011a88394cfSJeff Kirsher 	entry = np->cur_tx % TX_RING_SIZE;
1012a88394cfSJeff Kirsher 
1013a88394cfSJeff Kirsher 	np->tx_addr[entry] = pci_map_single(np->pci_dev,
1014a88394cfSJeff Kirsher 				skb->data,skb->len, PCI_DMA_TODEVICE);
1015a88394cfSJeff Kirsher 	np->tx_skbuff[entry] = skb;
1016a88394cfSJeff Kirsher 
1017a88394cfSJeff Kirsher 	np->tx_ring[entry].buffer1 = np->tx_addr[entry];
1018a88394cfSJeff Kirsher 	if (skb->len < TX_BUFLIMIT) {
1019a88394cfSJeff Kirsher 		np->tx_ring[entry].length = DescWholePkt | skb->len;
1020a88394cfSJeff Kirsher 	} else {
1021a88394cfSJeff Kirsher 		int len = skb->len - TX_BUFLIMIT;
1022a88394cfSJeff Kirsher 
1023a88394cfSJeff Kirsher 		np->tx_ring[entry].buffer2 = np->tx_addr[entry]+TX_BUFLIMIT;
1024a88394cfSJeff Kirsher 		np->tx_ring[entry].length = DescWholePkt | (len << 11) | TX_BUFLIMIT;
1025a88394cfSJeff Kirsher 	}
1026a88394cfSJeff Kirsher 	if(entry == TX_RING_SIZE-1)
1027a88394cfSJeff Kirsher 		np->tx_ring[entry].length |= DescEndRing;
1028a88394cfSJeff Kirsher 
1029a88394cfSJeff Kirsher 	/* Now acquire the irq spinlock.
1030a88394cfSJeff Kirsher 	 * The difficult race is the ordering between
1031a88394cfSJeff Kirsher 	 * increasing np->cur_tx and setting DescOwned:
1032a88394cfSJeff Kirsher 	 * - if np->cur_tx is increased first the interrupt
1033a88394cfSJeff Kirsher 	 *   handler could consider the packet as transmitted
1034a88394cfSJeff Kirsher 	 *   since DescOwned is cleared.
1035a88394cfSJeff Kirsher 	 * - If DescOwned is set first the NIC could report the
1036a88394cfSJeff Kirsher 	 *   packet as sent, but the interrupt handler would ignore it
1037a88394cfSJeff Kirsher 	 *   since the np->cur_tx was not yet increased.
1038a88394cfSJeff Kirsher 	 */
1039a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1040a88394cfSJeff Kirsher 	np->cur_tx++;
1041a88394cfSJeff Kirsher 
1042a88394cfSJeff Kirsher 	wmb(); /* flush length, buffer1, buffer2 */
1043a88394cfSJeff Kirsher 	np->tx_ring[entry].status = DescOwned;
1044a88394cfSJeff Kirsher 	wmb(); /* flush status and kick the hardware */
1045a88394cfSJeff Kirsher 	iowrite32(0, np->base_addr + TxStartDemand);
1046a88394cfSJeff Kirsher 	np->tx_q_bytes += skb->len;
1047a88394cfSJeff Kirsher 	/* Work around horrible bug in the chip by marking the queue as full
1048a88394cfSJeff Kirsher 	   when we do not have FIFO room for a maximum sized packet. */
1049a88394cfSJeff Kirsher 	if (np->cur_tx - np->dirty_tx > TX_QUEUE_LEN ||
1050a88394cfSJeff Kirsher 		((np->drv_flags & HasBrokenTx) && np->tx_q_bytes > TX_BUG_FIFO_LIMIT)) {
1051a88394cfSJeff Kirsher 		netif_stop_queue(dev);
1052a88394cfSJeff Kirsher 		wmb();
1053a88394cfSJeff Kirsher 		np->tx_full = 1;
1054a88394cfSJeff Kirsher 	}
1055a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1056a88394cfSJeff Kirsher 
1057a88394cfSJeff Kirsher 	if (debug > 4) {
1058a88394cfSJeff Kirsher 		netdev_dbg(dev, "Transmit frame #%d queued in slot %d\n",
1059a88394cfSJeff Kirsher 			   np->cur_tx, entry);
1060a88394cfSJeff Kirsher 	}
1061a88394cfSJeff Kirsher 	return NETDEV_TX_OK;
1062a88394cfSJeff Kirsher }
1063a88394cfSJeff Kirsher 
1064a88394cfSJeff Kirsher static void netdev_tx_done(struct net_device *dev)
1065a88394cfSJeff Kirsher {
1066a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1067a88394cfSJeff Kirsher 	for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1068a88394cfSJeff Kirsher 		int entry = np->dirty_tx % TX_RING_SIZE;
1069a88394cfSJeff Kirsher 		int tx_status = np->tx_ring[entry].status;
1070a88394cfSJeff Kirsher 
1071a88394cfSJeff Kirsher 		if (tx_status < 0)
1072a88394cfSJeff Kirsher 			break;
1073a88394cfSJeff Kirsher 		if (tx_status & 0x8000) { 	/* There was an error, log it. */
1074a88394cfSJeff Kirsher #ifndef final_version
1075a88394cfSJeff Kirsher 			if (debug > 1)
1076a88394cfSJeff Kirsher 				netdev_dbg(dev, "Transmit error, Tx status %08x\n",
1077a88394cfSJeff Kirsher 					   tx_status);
1078a88394cfSJeff Kirsher #endif
1079a88394cfSJeff Kirsher 			np->stats.tx_errors++;
1080a88394cfSJeff Kirsher 			if (tx_status & 0x0104) np->stats.tx_aborted_errors++;
1081a88394cfSJeff Kirsher 			if (tx_status & 0x0C80) np->stats.tx_carrier_errors++;
1082a88394cfSJeff Kirsher 			if (tx_status & 0x0200) np->stats.tx_window_errors++;
1083a88394cfSJeff Kirsher 			if (tx_status & 0x0002) np->stats.tx_fifo_errors++;
1084a88394cfSJeff Kirsher 			if ((tx_status & 0x0080) && np->mii_if.full_duplex == 0)
1085a88394cfSJeff Kirsher 				np->stats.tx_heartbeat_errors++;
1086a88394cfSJeff Kirsher 		} else {
1087a88394cfSJeff Kirsher #ifndef final_version
1088a88394cfSJeff Kirsher 			if (debug > 3)
1089a88394cfSJeff Kirsher 				netdev_dbg(dev, "Transmit slot %d ok, Tx status %08x\n",
1090a88394cfSJeff Kirsher 					   entry, tx_status);
1091a88394cfSJeff Kirsher #endif
1092a88394cfSJeff Kirsher 			np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1093a88394cfSJeff Kirsher 			np->stats.collisions += (tx_status >> 3) & 15;
1094a88394cfSJeff Kirsher 			np->stats.tx_packets++;
1095a88394cfSJeff Kirsher 		}
1096a88394cfSJeff Kirsher 		/* Free the original skb. */
1097a88394cfSJeff Kirsher 		pci_unmap_single(np->pci_dev,np->tx_addr[entry],
1098a88394cfSJeff Kirsher 					np->tx_skbuff[entry]->len,
1099a88394cfSJeff Kirsher 					PCI_DMA_TODEVICE);
1100a88394cfSJeff Kirsher 		np->tx_q_bytes -= np->tx_skbuff[entry]->len;
1101a88394cfSJeff Kirsher 		dev_kfree_skb_irq(np->tx_skbuff[entry]);
1102a88394cfSJeff Kirsher 		np->tx_skbuff[entry] = NULL;
1103a88394cfSJeff Kirsher 	}
1104a88394cfSJeff Kirsher 	if (np->tx_full &&
1105a88394cfSJeff Kirsher 		np->cur_tx - np->dirty_tx < TX_QUEUE_LEN_RESTART &&
1106a88394cfSJeff Kirsher 		np->tx_q_bytes < TX_BUG_FIFO_LIMIT) {
1107a88394cfSJeff Kirsher 		/* The ring is no longer full, clear tbusy. */
1108a88394cfSJeff Kirsher 		np->tx_full = 0;
1109a88394cfSJeff Kirsher 		wmb();
1110a88394cfSJeff Kirsher 		netif_wake_queue(dev);
1111a88394cfSJeff Kirsher 	}
1112a88394cfSJeff Kirsher }
1113a88394cfSJeff Kirsher 
1114a88394cfSJeff Kirsher /* The interrupt handler does all of the Rx thread work and cleans up
1115a88394cfSJeff Kirsher    after the Tx thread. */
1116a88394cfSJeff Kirsher static irqreturn_t intr_handler(int irq, void *dev_instance)
1117a88394cfSJeff Kirsher {
1118a88394cfSJeff Kirsher 	struct net_device *dev = (struct net_device *)dev_instance;
1119a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1120a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1121a88394cfSJeff Kirsher 	int work_limit = max_interrupt_work;
1122a88394cfSJeff Kirsher 	int handled = 0;
1123a88394cfSJeff Kirsher 
1124a88394cfSJeff Kirsher 	if (!netif_device_present(dev))
1125a88394cfSJeff Kirsher 		return IRQ_NONE;
1126a88394cfSJeff Kirsher 	do {
1127a88394cfSJeff Kirsher 		u32 intr_status = ioread32(ioaddr + IntrStatus);
1128a88394cfSJeff Kirsher 
1129a88394cfSJeff Kirsher 		/* Acknowledge all of the current interrupt sources ASAP. */
1130a88394cfSJeff Kirsher 		iowrite32(intr_status & 0x001ffff, ioaddr + IntrStatus);
1131a88394cfSJeff Kirsher 
1132a88394cfSJeff Kirsher 		if (debug > 4)
1133a88394cfSJeff Kirsher 			netdev_dbg(dev, "Interrupt, status %04x\n", intr_status);
1134a88394cfSJeff Kirsher 
1135a88394cfSJeff Kirsher 		if ((intr_status & (NormalIntr|AbnormalIntr)) == 0)
1136a88394cfSJeff Kirsher 			break;
1137a88394cfSJeff Kirsher 
1138a88394cfSJeff Kirsher 		handled = 1;
1139a88394cfSJeff Kirsher 
1140a88394cfSJeff Kirsher 		if (intr_status & (RxIntr | RxNoBuf))
1141a88394cfSJeff Kirsher 			netdev_rx(dev);
1142a88394cfSJeff Kirsher 		if (intr_status & RxNoBuf)
1143a88394cfSJeff Kirsher 			iowrite32(0, ioaddr + RxStartDemand);
1144a88394cfSJeff Kirsher 
1145a88394cfSJeff Kirsher 		if (intr_status & (TxNoBuf | TxIntr) &&
1146a88394cfSJeff Kirsher 			np->cur_tx != np->dirty_tx) {
1147a88394cfSJeff Kirsher 			spin_lock(&np->lock);
1148a88394cfSJeff Kirsher 			netdev_tx_done(dev);
1149a88394cfSJeff Kirsher 			spin_unlock(&np->lock);
1150a88394cfSJeff Kirsher 		}
1151a88394cfSJeff Kirsher 
1152a88394cfSJeff Kirsher 		/* Abnormal error summary/uncommon events handlers. */
1153a88394cfSJeff Kirsher 		if (intr_status & (AbnormalIntr | TxFIFOUnderflow | SystemError |
1154a88394cfSJeff Kirsher 						   TimerInt | TxDied))
1155a88394cfSJeff Kirsher 			netdev_error(dev, intr_status);
1156a88394cfSJeff Kirsher 
1157a88394cfSJeff Kirsher 		if (--work_limit < 0) {
1158a88394cfSJeff Kirsher 			dev_warn(&dev->dev,
1159a88394cfSJeff Kirsher 				 "Too much work at interrupt, status=0x%04x\n",
1160a88394cfSJeff Kirsher 				 intr_status);
1161a88394cfSJeff Kirsher 			/* Set the timer to re-enable the other interrupts after
1162a88394cfSJeff Kirsher 			   10*82usec ticks. */
1163a88394cfSJeff Kirsher 			spin_lock(&np->lock);
1164a88394cfSJeff Kirsher 			if (netif_device_present(dev)) {
1165a88394cfSJeff Kirsher 				iowrite32(AbnormalIntr | TimerInt, ioaddr + IntrEnable);
1166a88394cfSJeff Kirsher 				iowrite32(10, ioaddr + GPTimer);
1167a88394cfSJeff Kirsher 			}
1168a88394cfSJeff Kirsher 			spin_unlock(&np->lock);
1169a88394cfSJeff Kirsher 			break;
1170a88394cfSJeff Kirsher 		}
1171a88394cfSJeff Kirsher 	} while (1);
1172a88394cfSJeff Kirsher 
1173a88394cfSJeff Kirsher 	if (debug > 3)
1174a88394cfSJeff Kirsher 		netdev_dbg(dev, "exiting interrupt, status=%#4.4x\n",
1175a88394cfSJeff Kirsher 			   ioread32(ioaddr + IntrStatus));
1176a88394cfSJeff Kirsher 	return IRQ_RETVAL(handled);
1177a88394cfSJeff Kirsher }
1178a88394cfSJeff Kirsher 
1179a88394cfSJeff Kirsher /* This routine is logically part of the interrupt handler, but separated
1180a88394cfSJeff Kirsher    for clarity and better register allocation. */
1181a88394cfSJeff Kirsher static int netdev_rx(struct net_device *dev)
1182a88394cfSJeff Kirsher {
1183a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1184a88394cfSJeff Kirsher 	int entry = np->cur_rx % RX_RING_SIZE;
1185a88394cfSJeff Kirsher 	int work_limit = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1186a88394cfSJeff Kirsher 
1187a88394cfSJeff Kirsher 	if (debug > 4) {
1188a88394cfSJeff Kirsher 		netdev_dbg(dev, " In netdev_rx(), entry %d status %04x\n",
1189a88394cfSJeff Kirsher 			   entry, np->rx_ring[entry].status);
1190a88394cfSJeff Kirsher 	}
1191a88394cfSJeff Kirsher 
1192a88394cfSJeff Kirsher 	/* If EOP is set on the next entry, it's a new packet. Send it up. */
1193a88394cfSJeff Kirsher 	while (--work_limit >= 0) {
1194a88394cfSJeff Kirsher 		struct w840_rx_desc *desc = np->rx_head_desc;
1195a88394cfSJeff Kirsher 		s32 status = desc->status;
1196a88394cfSJeff Kirsher 
1197a88394cfSJeff Kirsher 		if (debug > 4)
1198a88394cfSJeff Kirsher 			netdev_dbg(dev, "  netdev_rx() status was %08x\n",
1199a88394cfSJeff Kirsher 				   status);
1200a88394cfSJeff Kirsher 		if (status < 0)
1201a88394cfSJeff Kirsher 			break;
1202a88394cfSJeff Kirsher 		if ((status & 0x38008300) != 0x0300) {
1203a88394cfSJeff Kirsher 			if ((status & 0x38000300) != 0x0300) {
1204a88394cfSJeff Kirsher 				/* Ingore earlier buffers. */
1205a88394cfSJeff Kirsher 				if ((status & 0xffff) != 0x7fff) {
1206a88394cfSJeff Kirsher 					dev_warn(&dev->dev,
1207a88394cfSJeff Kirsher 						 "Oversized Ethernet frame spanned multiple buffers, entry %#x status %04x!\n",
1208a88394cfSJeff Kirsher 						 np->cur_rx, status);
1209a88394cfSJeff Kirsher 					np->stats.rx_length_errors++;
1210a88394cfSJeff Kirsher 				}
1211a88394cfSJeff Kirsher 			} else if (status & 0x8000) {
1212a88394cfSJeff Kirsher 				/* There was a fatal error. */
1213a88394cfSJeff Kirsher 				if (debug > 2)
1214a88394cfSJeff Kirsher 					netdev_dbg(dev, "Receive error, Rx status %08x\n",
1215a88394cfSJeff Kirsher 						   status);
1216a88394cfSJeff Kirsher 				np->stats.rx_errors++; /* end of a packet.*/
1217a88394cfSJeff Kirsher 				if (status & 0x0890) np->stats.rx_length_errors++;
1218a88394cfSJeff Kirsher 				if (status & 0x004C) np->stats.rx_frame_errors++;
1219a88394cfSJeff Kirsher 				if (status & 0x0002) np->stats.rx_crc_errors++;
1220a88394cfSJeff Kirsher 			}
1221a88394cfSJeff Kirsher 		} else {
1222a88394cfSJeff Kirsher 			struct sk_buff *skb;
1223a88394cfSJeff Kirsher 			/* Omit the four octet CRC from the length. */
1224a88394cfSJeff Kirsher 			int pkt_len = ((status >> 16) & 0x7ff) - 4;
1225a88394cfSJeff Kirsher 
1226a88394cfSJeff Kirsher #ifndef final_version
1227a88394cfSJeff Kirsher 			if (debug > 4)
1228a88394cfSJeff Kirsher 				netdev_dbg(dev, "  netdev_rx() normal Rx pkt length %d status %x\n",
1229a88394cfSJeff Kirsher 					   pkt_len, status);
1230a88394cfSJeff Kirsher #endif
1231a88394cfSJeff Kirsher 			/* Check if the packet is long enough to accept without copying
1232a88394cfSJeff Kirsher 			   to a minimally-sized skbuff. */
1233a88394cfSJeff Kirsher 			if (pkt_len < rx_copybreak &&
123421a4e469SPradeep A Dalvi 			    (skb = netdev_alloc_skb(dev, pkt_len + 2)) != NULL) {
1235a88394cfSJeff Kirsher 				skb_reserve(skb, 2);	/* 16 byte align the IP header */
1236a88394cfSJeff Kirsher 				pci_dma_sync_single_for_cpu(np->pci_dev,np->rx_addr[entry],
1237a88394cfSJeff Kirsher 							    np->rx_skbuff[entry]->len,
1238a88394cfSJeff Kirsher 							    PCI_DMA_FROMDEVICE);
1239a88394cfSJeff Kirsher 				skb_copy_to_linear_data(skb, np->rx_skbuff[entry]->data, pkt_len);
1240a88394cfSJeff Kirsher 				skb_put(skb, pkt_len);
1241a88394cfSJeff Kirsher 				pci_dma_sync_single_for_device(np->pci_dev,np->rx_addr[entry],
1242a88394cfSJeff Kirsher 							       np->rx_skbuff[entry]->len,
1243a88394cfSJeff Kirsher 							       PCI_DMA_FROMDEVICE);
1244a88394cfSJeff Kirsher 			} else {
1245a88394cfSJeff Kirsher 				pci_unmap_single(np->pci_dev,np->rx_addr[entry],
1246a88394cfSJeff Kirsher 							np->rx_skbuff[entry]->len,
1247a88394cfSJeff Kirsher 							PCI_DMA_FROMDEVICE);
1248a88394cfSJeff Kirsher 				skb_put(skb = np->rx_skbuff[entry], pkt_len);
1249a88394cfSJeff Kirsher 				np->rx_skbuff[entry] = NULL;
1250a88394cfSJeff Kirsher 			}
1251a88394cfSJeff Kirsher #ifndef final_version				/* Remove after testing. */
1252a88394cfSJeff Kirsher 			/* You will want this info for the initial debug. */
1253a88394cfSJeff Kirsher 			if (debug > 5)
1254a88394cfSJeff Kirsher 				netdev_dbg(dev, "  Rx data %pM %pM %02x%02x %pI4\n",
1255a88394cfSJeff Kirsher 					   &skb->data[0], &skb->data[6],
1256a88394cfSJeff Kirsher 					   skb->data[12], skb->data[13],
1257a88394cfSJeff Kirsher 					   &skb->data[14]);
1258a88394cfSJeff Kirsher #endif
1259a88394cfSJeff Kirsher 			skb->protocol = eth_type_trans(skb, dev);
1260a88394cfSJeff Kirsher 			netif_rx(skb);
1261a88394cfSJeff Kirsher 			np->stats.rx_packets++;
1262a88394cfSJeff Kirsher 			np->stats.rx_bytes += pkt_len;
1263a88394cfSJeff Kirsher 		}
1264a88394cfSJeff Kirsher 		entry = (++np->cur_rx) % RX_RING_SIZE;
1265a88394cfSJeff Kirsher 		np->rx_head_desc = &np->rx_ring[entry];
1266a88394cfSJeff Kirsher 	}
1267a88394cfSJeff Kirsher 
1268a88394cfSJeff Kirsher 	/* Refill the Rx ring buffers. */
1269a88394cfSJeff Kirsher 	for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1270a88394cfSJeff Kirsher 		struct sk_buff *skb;
1271a88394cfSJeff Kirsher 		entry = np->dirty_rx % RX_RING_SIZE;
1272a88394cfSJeff Kirsher 		if (np->rx_skbuff[entry] == NULL) {
127321a4e469SPradeep A Dalvi 			skb = netdev_alloc_skb(dev, np->rx_buf_sz);
1274a88394cfSJeff Kirsher 			np->rx_skbuff[entry] = skb;
1275a88394cfSJeff Kirsher 			if (skb == NULL)
1276a88394cfSJeff Kirsher 				break;			/* Better luck next round. */
1277a88394cfSJeff Kirsher 			np->rx_addr[entry] = pci_map_single(np->pci_dev,
1278a88394cfSJeff Kirsher 							skb->data,
1279a88394cfSJeff Kirsher 							np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1280a88394cfSJeff Kirsher 			np->rx_ring[entry].buffer1 = np->rx_addr[entry];
1281a88394cfSJeff Kirsher 		}
1282a88394cfSJeff Kirsher 		wmb();
1283a88394cfSJeff Kirsher 		np->rx_ring[entry].status = DescOwned;
1284a88394cfSJeff Kirsher 	}
1285a88394cfSJeff Kirsher 
1286a88394cfSJeff Kirsher 	return 0;
1287a88394cfSJeff Kirsher }
1288a88394cfSJeff Kirsher 
1289a88394cfSJeff Kirsher static void netdev_error(struct net_device *dev, int intr_status)
1290a88394cfSJeff Kirsher {
1291a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1292a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1293a88394cfSJeff Kirsher 
1294a88394cfSJeff Kirsher 	if (debug > 2)
1295a88394cfSJeff Kirsher 		netdev_dbg(dev, "Abnormal event, %08x\n", intr_status);
1296a88394cfSJeff Kirsher 	if (intr_status == 0xffffffff)
1297a88394cfSJeff Kirsher 		return;
1298a88394cfSJeff Kirsher 	spin_lock(&np->lock);
1299a88394cfSJeff Kirsher 	if (intr_status & TxFIFOUnderflow) {
1300a88394cfSJeff Kirsher 		int new;
1301a88394cfSJeff Kirsher 		/* Bump up the Tx threshold */
1302a88394cfSJeff Kirsher #if 0
1303a88394cfSJeff Kirsher 		/* This causes lots of dropped packets,
1304a88394cfSJeff Kirsher 		 * and under high load even tx_timeouts
1305a88394cfSJeff Kirsher 		 */
1306a88394cfSJeff Kirsher 		new = np->csr6 + 0x4000;
1307a88394cfSJeff Kirsher #else
1308a88394cfSJeff Kirsher 		new = (np->csr6 >> 14)&0x7f;
1309a88394cfSJeff Kirsher 		if (new < 64)
1310a88394cfSJeff Kirsher 			new *= 2;
1311a88394cfSJeff Kirsher 		 else
1312a88394cfSJeff Kirsher 		 	new = 127; /* load full packet before starting */
1313a88394cfSJeff Kirsher 		new = (np->csr6 & ~(0x7F << 14)) | (new<<14);
1314a88394cfSJeff Kirsher #endif
1315a88394cfSJeff Kirsher 		netdev_dbg(dev, "Tx underflow, new csr6 %08x\n", new);
1316a88394cfSJeff Kirsher 		update_csr6(dev, new);
1317a88394cfSJeff Kirsher 	}
1318a88394cfSJeff Kirsher 	if (intr_status & RxDied) {		/* Missed a Rx frame. */
1319a88394cfSJeff Kirsher 		np->stats.rx_errors++;
1320a88394cfSJeff Kirsher 	}
1321a88394cfSJeff Kirsher 	if (intr_status & TimerInt) {
1322a88394cfSJeff Kirsher 		/* Re-enable other interrupts. */
1323a88394cfSJeff Kirsher 		if (netif_device_present(dev))
1324a88394cfSJeff Kirsher 			iowrite32(0x1A0F5, ioaddr + IntrEnable);
1325a88394cfSJeff Kirsher 	}
1326a88394cfSJeff Kirsher 	np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1327a88394cfSJeff Kirsher 	iowrite32(0, ioaddr + RxStartDemand);
1328a88394cfSJeff Kirsher 	spin_unlock(&np->lock);
1329a88394cfSJeff Kirsher }
1330a88394cfSJeff Kirsher 
1331a88394cfSJeff Kirsher static struct net_device_stats *get_stats(struct net_device *dev)
1332a88394cfSJeff Kirsher {
1333a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1334a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1335a88394cfSJeff Kirsher 
1336a88394cfSJeff Kirsher 	/* The chip only need report frame silently dropped. */
1337a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1338a88394cfSJeff Kirsher 	if (netif_running(dev) && netif_device_present(dev))
1339a88394cfSJeff Kirsher 		np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1340a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1341a88394cfSJeff Kirsher 
1342a88394cfSJeff Kirsher 	return &np->stats;
1343a88394cfSJeff Kirsher }
1344a88394cfSJeff Kirsher 
1345a88394cfSJeff Kirsher 
1346a88394cfSJeff Kirsher static u32 __set_rx_mode(struct net_device *dev)
1347a88394cfSJeff Kirsher {
1348a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1349a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1350a88394cfSJeff Kirsher 	u32 mc_filter[2];			/* Multicast hash filter */
1351a88394cfSJeff Kirsher 	u32 rx_mode;
1352a88394cfSJeff Kirsher 
1353a88394cfSJeff Kirsher 	if (dev->flags & IFF_PROMISC) {			/* Set promiscuous. */
1354a88394cfSJeff Kirsher 		memset(mc_filter, 0xff, sizeof(mc_filter));
1355a88394cfSJeff Kirsher 		rx_mode = RxAcceptBroadcast | AcceptMulticast | RxAcceptAllPhys
1356a88394cfSJeff Kirsher 			| AcceptMyPhys;
1357a88394cfSJeff Kirsher 	} else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
1358a88394cfSJeff Kirsher 		   (dev->flags & IFF_ALLMULTI)) {
1359a88394cfSJeff Kirsher 		/* Too many to match, or accept all multicasts. */
1360a88394cfSJeff Kirsher 		memset(mc_filter, 0xff, sizeof(mc_filter));
1361a88394cfSJeff Kirsher 		rx_mode = RxAcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1362a88394cfSJeff Kirsher 	} else {
1363a88394cfSJeff Kirsher 		struct netdev_hw_addr *ha;
1364a88394cfSJeff Kirsher 
1365a88394cfSJeff Kirsher 		memset(mc_filter, 0, sizeof(mc_filter));
1366a88394cfSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
1367a88394cfSJeff Kirsher 			int filbit;
1368a88394cfSJeff Kirsher 
1369a88394cfSJeff Kirsher 			filbit = (ether_crc(ETH_ALEN, ha->addr) >> 26) ^ 0x3F;
1370a88394cfSJeff Kirsher 			filbit &= 0x3f;
1371a88394cfSJeff Kirsher 			mc_filter[filbit >> 5] |= 1 << (filbit & 31);
1372a88394cfSJeff Kirsher 		}
1373a88394cfSJeff Kirsher 		rx_mode = RxAcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1374a88394cfSJeff Kirsher 	}
1375a88394cfSJeff Kirsher 	iowrite32(mc_filter[0], ioaddr + MulticastFilter0);
1376a88394cfSJeff Kirsher 	iowrite32(mc_filter[1], ioaddr + MulticastFilter1);
1377a88394cfSJeff Kirsher 	return rx_mode;
1378a88394cfSJeff Kirsher }
1379a88394cfSJeff Kirsher 
1380a88394cfSJeff Kirsher static void set_rx_mode(struct net_device *dev)
1381a88394cfSJeff Kirsher {
1382a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1383a88394cfSJeff Kirsher 	u32 rx_mode = __set_rx_mode(dev);
1384a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1385a88394cfSJeff Kirsher 	update_csr6(dev, (np->csr6 & ~0x00F8) | rx_mode);
1386a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1387a88394cfSJeff Kirsher }
1388a88394cfSJeff Kirsher 
1389a88394cfSJeff Kirsher static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1390a88394cfSJeff Kirsher {
1391a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1392a88394cfSJeff Kirsher 
139368aad78cSRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
139468aad78cSRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
139568aad78cSRick Jones 	strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
1396a88394cfSJeff Kirsher }
1397a88394cfSJeff Kirsher 
1398a88394cfSJeff Kirsher static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1399a88394cfSJeff Kirsher {
1400a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1401a88394cfSJeff Kirsher 	int rc;
1402a88394cfSJeff Kirsher 
1403a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1404a88394cfSJeff Kirsher 	rc = mii_ethtool_gset(&np->mii_if, cmd);
1405a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1406a88394cfSJeff Kirsher 
1407a88394cfSJeff Kirsher 	return rc;
1408a88394cfSJeff Kirsher }
1409a88394cfSJeff Kirsher 
1410a88394cfSJeff Kirsher static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1411a88394cfSJeff Kirsher {
1412a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1413a88394cfSJeff Kirsher 	int rc;
1414a88394cfSJeff Kirsher 
1415a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1416a88394cfSJeff Kirsher 	rc = mii_ethtool_sset(&np->mii_if, cmd);
1417a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1418a88394cfSJeff Kirsher 
1419a88394cfSJeff Kirsher 	return rc;
1420a88394cfSJeff Kirsher }
1421a88394cfSJeff Kirsher 
1422a88394cfSJeff Kirsher static int netdev_nway_reset(struct net_device *dev)
1423a88394cfSJeff Kirsher {
1424a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1425a88394cfSJeff Kirsher 	return mii_nway_restart(&np->mii_if);
1426a88394cfSJeff Kirsher }
1427a88394cfSJeff Kirsher 
1428a88394cfSJeff Kirsher static u32 netdev_get_link(struct net_device *dev)
1429a88394cfSJeff Kirsher {
1430a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1431a88394cfSJeff Kirsher 	return mii_link_ok(&np->mii_if);
1432a88394cfSJeff Kirsher }
1433a88394cfSJeff Kirsher 
1434a88394cfSJeff Kirsher static u32 netdev_get_msglevel(struct net_device *dev)
1435a88394cfSJeff Kirsher {
1436a88394cfSJeff Kirsher 	return debug;
1437a88394cfSJeff Kirsher }
1438a88394cfSJeff Kirsher 
1439a88394cfSJeff Kirsher static void netdev_set_msglevel(struct net_device *dev, u32 value)
1440a88394cfSJeff Kirsher {
1441a88394cfSJeff Kirsher 	debug = value;
1442a88394cfSJeff Kirsher }
1443a88394cfSJeff Kirsher 
1444a88394cfSJeff Kirsher static const struct ethtool_ops netdev_ethtool_ops = {
1445a88394cfSJeff Kirsher 	.get_drvinfo		= netdev_get_drvinfo,
1446a88394cfSJeff Kirsher 	.get_settings		= netdev_get_settings,
1447a88394cfSJeff Kirsher 	.set_settings		= netdev_set_settings,
1448a88394cfSJeff Kirsher 	.nway_reset		= netdev_nway_reset,
1449a88394cfSJeff Kirsher 	.get_link		= netdev_get_link,
1450a88394cfSJeff Kirsher 	.get_msglevel		= netdev_get_msglevel,
1451a88394cfSJeff Kirsher 	.set_msglevel		= netdev_set_msglevel,
1452a88394cfSJeff Kirsher };
1453a88394cfSJeff Kirsher 
1454a88394cfSJeff Kirsher static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1455a88394cfSJeff Kirsher {
1456a88394cfSJeff Kirsher 	struct mii_ioctl_data *data = if_mii(rq);
1457a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1458a88394cfSJeff Kirsher 
1459a88394cfSJeff Kirsher 	switch(cmd) {
1460a88394cfSJeff Kirsher 	case SIOCGMIIPHY:		/* Get address of MII PHY in use. */
1461a88394cfSJeff Kirsher 		data->phy_id = ((struct netdev_private *)netdev_priv(dev))->phys[0] & 0x1f;
1462a88394cfSJeff Kirsher 		/* Fall Through */
1463a88394cfSJeff Kirsher 
1464a88394cfSJeff Kirsher 	case SIOCGMIIREG:		/* Read MII PHY register. */
1465a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1466a88394cfSJeff Kirsher 		data->val_out = mdio_read(dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
1467a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1468a88394cfSJeff Kirsher 		return 0;
1469a88394cfSJeff Kirsher 
1470a88394cfSJeff Kirsher 	case SIOCSMIIREG:		/* Write MII PHY register. */
1471a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1472a88394cfSJeff Kirsher 		mdio_write(dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
1473a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1474a88394cfSJeff Kirsher 		return 0;
1475a88394cfSJeff Kirsher 	default:
1476a88394cfSJeff Kirsher 		return -EOPNOTSUPP;
1477a88394cfSJeff Kirsher 	}
1478a88394cfSJeff Kirsher }
1479a88394cfSJeff Kirsher 
1480a88394cfSJeff Kirsher static int netdev_close(struct net_device *dev)
1481a88394cfSJeff Kirsher {
1482a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1483a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1484a88394cfSJeff Kirsher 
1485a88394cfSJeff Kirsher 	netif_stop_queue(dev);
1486a88394cfSJeff Kirsher 
1487a88394cfSJeff Kirsher 	if (debug > 1) {
1488a88394cfSJeff Kirsher 		netdev_dbg(dev, "Shutting down ethercard, status was %08x Config %08x\n",
1489a88394cfSJeff Kirsher 			   ioread32(ioaddr + IntrStatus),
1490a88394cfSJeff Kirsher 			   ioread32(ioaddr + NetworkConfig));
1491a88394cfSJeff Kirsher 		netdev_dbg(dev, "Queue pointers were Tx %d / %d,  Rx %d / %d\n",
1492a88394cfSJeff Kirsher 			   np->cur_tx, np->dirty_tx,
1493a88394cfSJeff Kirsher 			   np->cur_rx, np->dirty_rx);
1494a88394cfSJeff Kirsher 	}
1495a88394cfSJeff Kirsher 
1496a88394cfSJeff Kirsher  	/* Stop the chip's Tx and Rx processes. */
1497a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1498a88394cfSJeff Kirsher 	netif_device_detach(dev);
1499a88394cfSJeff Kirsher 	update_csr6(dev, 0);
1500a88394cfSJeff Kirsher 	iowrite32(0x0000, ioaddr + IntrEnable);
1501a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1502a88394cfSJeff Kirsher 
1503a88394cfSJeff Kirsher 	free_irq(dev->irq, dev);
1504a88394cfSJeff Kirsher 	wmb();
1505a88394cfSJeff Kirsher 	netif_device_attach(dev);
1506a88394cfSJeff Kirsher 
1507a88394cfSJeff Kirsher 	if (ioread32(ioaddr + NetworkConfig) != 0xffffffff)
1508a88394cfSJeff Kirsher 		np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1509a88394cfSJeff Kirsher 
1510a88394cfSJeff Kirsher #ifdef __i386__
1511a88394cfSJeff Kirsher 	if (debug > 2) {
1512a88394cfSJeff Kirsher 		int i;
1513a88394cfSJeff Kirsher 
1514a88394cfSJeff Kirsher 		printk(KERN_DEBUG"  Tx ring at %p:\n", np->tx_ring);
1515a88394cfSJeff Kirsher 		for (i = 0; i < TX_RING_SIZE; i++)
1516a88394cfSJeff Kirsher 			printk(KERN_DEBUG " #%d desc. %04x %04x %08x\n",
1517a88394cfSJeff Kirsher 			       i, np->tx_ring[i].length,
1518a88394cfSJeff Kirsher 			       np->tx_ring[i].status, np->tx_ring[i].buffer1);
1519a88394cfSJeff Kirsher 		printk(KERN_DEBUG "  Rx ring %p:\n", np->rx_ring);
1520a88394cfSJeff Kirsher 		for (i = 0; i < RX_RING_SIZE; i++) {
1521a88394cfSJeff Kirsher 			printk(KERN_DEBUG " #%d desc. %04x %04x %08x\n",
1522a88394cfSJeff Kirsher 			       i, np->rx_ring[i].length,
1523a88394cfSJeff Kirsher 			       np->rx_ring[i].status, np->rx_ring[i].buffer1);
1524a88394cfSJeff Kirsher 		}
1525a88394cfSJeff Kirsher 	}
1526a88394cfSJeff Kirsher #endif /* __i386__ debugging only */
1527a88394cfSJeff Kirsher 
1528a88394cfSJeff Kirsher 	del_timer_sync(&np->timer);
1529a88394cfSJeff Kirsher 
1530a88394cfSJeff Kirsher 	free_rxtx_rings(np);
1531a88394cfSJeff Kirsher 	free_ringdesc(np);
1532a88394cfSJeff Kirsher 
1533a88394cfSJeff Kirsher 	return 0;
1534a88394cfSJeff Kirsher }
1535a88394cfSJeff Kirsher 
1536a88394cfSJeff Kirsher static void __devexit w840_remove1 (struct pci_dev *pdev)
1537a88394cfSJeff Kirsher {
1538a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata(pdev);
1539a88394cfSJeff Kirsher 
1540a88394cfSJeff Kirsher 	if (dev) {
1541a88394cfSJeff Kirsher 		struct netdev_private *np = netdev_priv(dev);
1542a88394cfSJeff Kirsher 		unregister_netdev(dev);
1543a88394cfSJeff Kirsher 		pci_release_regions(pdev);
1544a88394cfSJeff Kirsher 		pci_iounmap(pdev, np->base_addr);
1545a88394cfSJeff Kirsher 		free_netdev(dev);
1546a88394cfSJeff Kirsher 	}
1547a88394cfSJeff Kirsher 
1548a88394cfSJeff Kirsher 	pci_set_drvdata(pdev, NULL);
1549a88394cfSJeff Kirsher }
1550a88394cfSJeff Kirsher 
1551a88394cfSJeff Kirsher #ifdef CONFIG_PM
1552a88394cfSJeff Kirsher 
1553a88394cfSJeff Kirsher /*
1554a88394cfSJeff Kirsher  * suspend/resume synchronization:
1555a88394cfSJeff Kirsher  * - open, close, do_ioctl:
1556a88394cfSJeff Kirsher  * 	rtnl_lock, & netif_device_detach after the rtnl_unlock.
1557a88394cfSJeff Kirsher  * - get_stats:
1558a88394cfSJeff Kirsher  * 	spin_lock_irq(np->lock), doesn't touch hw if not present
1559a88394cfSJeff Kirsher  * - start_xmit:
1560a88394cfSJeff Kirsher  * 	synchronize_irq + netif_tx_disable;
1561a88394cfSJeff Kirsher  * - tx_timeout:
1562a88394cfSJeff Kirsher  * 	netif_device_detach + netif_tx_disable;
1563a88394cfSJeff Kirsher  * - set_multicast_list
1564a88394cfSJeff Kirsher  * 	netif_device_detach + netif_tx_disable;
1565a88394cfSJeff Kirsher  * - interrupt handler
1566a88394cfSJeff Kirsher  * 	doesn't touch hw if not present, synchronize_irq waits for
1567a88394cfSJeff Kirsher  * 	running instances of the interrupt handler.
1568a88394cfSJeff Kirsher  *
1569a88394cfSJeff Kirsher  * Disabling hw requires clearing csr6 & IntrEnable.
1570a88394cfSJeff Kirsher  * update_csr6 & all function that write IntrEnable check netif_device_present
1571a88394cfSJeff Kirsher  * before settings any bits.
1572a88394cfSJeff Kirsher  *
1573a88394cfSJeff Kirsher  * Detach must occur under spin_unlock_irq(), interrupts from a detached
1574a88394cfSJeff Kirsher  * device would cause an irq storm.
1575a88394cfSJeff Kirsher  */
1576a88394cfSJeff Kirsher static int w840_suspend (struct pci_dev *pdev, pm_message_t state)
1577a88394cfSJeff Kirsher {
1578a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata (pdev);
1579a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1580a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1581a88394cfSJeff Kirsher 
1582a88394cfSJeff Kirsher 	rtnl_lock();
1583a88394cfSJeff Kirsher 	if (netif_running (dev)) {
1584a88394cfSJeff Kirsher 		del_timer_sync(&np->timer);
1585a88394cfSJeff Kirsher 
1586a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1587a88394cfSJeff Kirsher 		netif_device_detach(dev);
1588a88394cfSJeff Kirsher 		update_csr6(dev, 0);
1589a88394cfSJeff Kirsher 		iowrite32(0, ioaddr + IntrEnable);
1590a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1591a88394cfSJeff Kirsher 
1592a88394cfSJeff Kirsher 		synchronize_irq(dev->irq);
1593a88394cfSJeff Kirsher 		netif_tx_disable(dev);
1594a88394cfSJeff Kirsher 
1595a88394cfSJeff Kirsher 		np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1596a88394cfSJeff Kirsher 
1597a88394cfSJeff Kirsher 		/* no more hardware accesses behind this line. */
1598a88394cfSJeff Kirsher 
1599a88394cfSJeff Kirsher 		BUG_ON(np->csr6 || ioread32(ioaddr + IntrEnable));
1600a88394cfSJeff Kirsher 
1601a88394cfSJeff Kirsher 		/* pci_power_off(pdev, -1); */
1602a88394cfSJeff Kirsher 
1603a88394cfSJeff Kirsher 		free_rxtx_rings(np);
1604a88394cfSJeff Kirsher 	} else {
1605a88394cfSJeff Kirsher 		netif_device_detach(dev);
1606a88394cfSJeff Kirsher 	}
1607a88394cfSJeff Kirsher 	rtnl_unlock();
1608a88394cfSJeff Kirsher 	return 0;
1609a88394cfSJeff Kirsher }
1610a88394cfSJeff Kirsher 
1611a88394cfSJeff Kirsher static int w840_resume (struct pci_dev *pdev)
1612a88394cfSJeff Kirsher {
1613a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata (pdev);
1614a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1615a88394cfSJeff Kirsher 	int retval = 0;
1616a88394cfSJeff Kirsher 
1617a88394cfSJeff Kirsher 	rtnl_lock();
1618a88394cfSJeff Kirsher 	if (netif_device_present(dev))
1619a88394cfSJeff Kirsher 		goto out; /* device not suspended */
1620a88394cfSJeff Kirsher 	if (netif_running(dev)) {
1621a88394cfSJeff Kirsher 		if ((retval = pci_enable_device(pdev))) {
1622a88394cfSJeff Kirsher 			dev_err(&dev->dev,
1623a88394cfSJeff Kirsher 				"pci_enable_device failed in resume\n");
1624a88394cfSJeff Kirsher 			goto out;
1625a88394cfSJeff Kirsher 		}
1626a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1627a88394cfSJeff Kirsher 		iowrite32(1, np->base_addr+PCIBusCfg);
1628a88394cfSJeff Kirsher 		ioread32(np->base_addr+PCIBusCfg);
1629a88394cfSJeff Kirsher 		udelay(1);
1630a88394cfSJeff Kirsher 		netif_device_attach(dev);
1631a88394cfSJeff Kirsher 		init_rxtx_rings(dev);
1632a88394cfSJeff Kirsher 		init_registers(dev);
1633a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1634a88394cfSJeff Kirsher 
1635a88394cfSJeff Kirsher 		netif_wake_queue(dev);
1636a88394cfSJeff Kirsher 
1637a88394cfSJeff Kirsher 		mod_timer(&np->timer, jiffies + 1*HZ);
1638a88394cfSJeff Kirsher 	} else {
1639a88394cfSJeff Kirsher 		netif_device_attach(dev);
1640a88394cfSJeff Kirsher 	}
1641a88394cfSJeff Kirsher out:
1642a88394cfSJeff Kirsher 	rtnl_unlock();
1643a88394cfSJeff Kirsher 	return retval;
1644a88394cfSJeff Kirsher }
1645a88394cfSJeff Kirsher #endif
1646a88394cfSJeff Kirsher 
1647a88394cfSJeff Kirsher static struct pci_driver w840_driver = {
1648a88394cfSJeff Kirsher 	.name		= DRV_NAME,
1649a88394cfSJeff Kirsher 	.id_table	= w840_pci_tbl,
1650a88394cfSJeff Kirsher 	.probe		= w840_probe1,
1651a88394cfSJeff Kirsher 	.remove		= __devexit_p(w840_remove1),
1652a88394cfSJeff Kirsher #ifdef CONFIG_PM
1653a88394cfSJeff Kirsher 	.suspend	= w840_suspend,
1654a88394cfSJeff Kirsher 	.resume		= w840_resume,
1655a88394cfSJeff Kirsher #endif
1656a88394cfSJeff Kirsher };
1657a88394cfSJeff Kirsher 
1658a88394cfSJeff Kirsher static int __init w840_init(void)
1659a88394cfSJeff Kirsher {
1660a88394cfSJeff Kirsher 	printk(version);
1661a88394cfSJeff Kirsher 	return pci_register_driver(&w840_driver);
1662a88394cfSJeff Kirsher }
1663a88394cfSJeff Kirsher 
1664a88394cfSJeff Kirsher static void __exit w840_exit(void)
1665a88394cfSJeff Kirsher {
1666a88394cfSJeff Kirsher 	pci_unregister_driver(&w840_driver);
1667a88394cfSJeff Kirsher }
1668a88394cfSJeff Kirsher 
1669a88394cfSJeff Kirsher module_init(w840_init);
1670a88394cfSJeff Kirsher module_exit(w840_exit);
1671