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 
2229baa3c34SBenoit Taine static const struct pci_device_id 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 
239779c1a85SBill Pemberton static const struct pci_id_info pci_id_tbl[] = {
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 
3611dd06ae8SGreg Kroah-Hartman static int w840_probe1(struct pci_dev *pdev, const struct pci_device_id *ent)
362a88394cfSJeff Kirsher {
363a88394cfSJeff Kirsher 	struct net_device *dev;
364a88394cfSJeff Kirsher 	struct netdev_private *np;
365a88394cfSJeff Kirsher 	static int find_cnt;
366a88394cfSJeff Kirsher 	int chip_idx = ent->driver_data;
367a88394cfSJeff Kirsher 	int irq;
368a88394cfSJeff Kirsher 	int i, option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;
369a88394cfSJeff Kirsher 	void __iomem *ioaddr;
370a88394cfSJeff Kirsher 
371a88394cfSJeff Kirsher 	i = pci_enable_device(pdev);
372a88394cfSJeff Kirsher 	if (i) return i;
373a88394cfSJeff Kirsher 
374a88394cfSJeff Kirsher 	pci_set_master(pdev);
375a88394cfSJeff Kirsher 
376a88394cfSJeff Kirsher 	irq = pdev->irq;
377a88394cfSJeff Kirsher 
378a88394cfSJeff Kirsher 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
379a88394cfSJeff Kirsher 		pr_warn("Device %s disabled due to DMA limitations\n",
380a88394cfSJeff Kirsher 			pci_name(pdev));
381a88394cfSJeff Kirsher 		return -EIO;
382a88394cfSJeff Kirsher 	}
383a88394cfSJeff Kirsher 	dev = alloc_etherdev(sizeof(*np));
384a88394cfSJeff Kirsher 	if (!dev)
385a88394cfSJeff Kirsher 		return -ENOMEM;
386a88394cfSJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
387a88394cfSJeff Kirsher 
388a88394cfSJeff Kirsher 	if (pci_request_regions(pdev, DRV_NAME))
389a88394cfSJeff Kirsher 		goto err_out_netdev;
390a88394cfSJeff Kirsher 
391a88394cfSJeff Kirsher 	ioaddr = pci_iomap(pdev, TULIP_BAR, netdev_res_size);
392a88394cfSJeff Kirsher 	if (!ioaddr)
393a88394cfSJeff Kirsher 		goto err_out_free_res;
394a88394cfSJeff Kirsher 
395a88394cfSJeff Kirsher 	for (i = 0; i < 3; i++)
396a88394cfSJeff Kirsher 		((__le16 *)dev->dev_addr)[i] = cpu_to_le16(eeprom_read(ioaddr, i));
397a88394cfSJeff Kirsher 
398a88394cfSJeff Kirsher 	/* Reset the chip to erase previous misconfiguration.
399a88394cfSJeff Kirsher 	   No hold time required! */
400a88394cfSJeff Kirsher 	iowrite32(0x00000001, ioaddr + PCIBusCfg);
401a88394cfSJeff Kirsher 
402a88394cfSJeff Kirsher 	np = netdev_priv(dev);
403a88394cfSJeff Kirsher 	np->pci_dev = pdev;
404a88394cfSJeff Kirsher 	np->chip_id = chip_idx;
405a88394cfSJeff Kirsher 	np->drv_flags = pci_id_tbl[chip_idx].drv_flags;
406a88394cfSJeff Kirsher 	spin_lock_init(&np->lock);
407a88394cfSJeff Kirsher 	np->mii_if.dev = dev;
408a88394cfSJeff Kirsher 	np->mii_if.mdio_read = mdio_read;
409a88394cfSJeff Kirsher 	np->mii_if.mdio_write = mdio_write;
410a88394cfSJeff Kirsher 	np->base_addr = ioaddr;
411a88394cfSJeff Kirsher 
412a88394cfSJeff Kirsher 	pci_set_drvdata(pdev, dev);
413a88394cfSJeff Kirsher 
414a88394cfSJeff Kirsher 	if (dev->mem_start)
415a88394cfSJeff Kirsher 		option = dev->mem_start;
416a88394cfSJeff Kirsher 
417a88394cfSJeff Kirsher 	/* The lower four bits are the media type. */
418a88394cfSJeff Kirsher 	if (option > 0) {
419a88394cfSJeff Kirsher 		if (option & 0x200)
420a88394cfSJeff Kirsher 			np->mii_if.full_duplex = 1;
421a88394cfSJeff Kirsher 		if (option & 15)
422a88394cfSJeff Kirsher 			dev_info(&dev->dev,
423a88394cfSJeff Kirsher 				 "ignoring user supplied media type %d",
424a88394cfSJeff Kirsher 				 option & 15);
425a88394cfSJeff Kirsher 	}
426a88394cfSJeff Kirsher 	if (find_cnt < MAX_UNITS  &&  full_duplex[find_cnt] > 0)
427a88394cfSJeff Kirsher 		np->mii_if.full_duplex = 1;
428a88394cfSJeff Kirsher 
429a88394cfSJeff Kirsher 	if (np->mii_if.full_duplex)
430a88394cfSJeff Kirsher 		np->mii_if.force_media = 1;
431a88394cfSJeff Kirsher 
432a88394cfSJeff Kirsher 	/* The chip-specific entries in the device structure. */
433a88394cfSJeff Kirsher 	dev->netdev_ops = &netdev_ops;
434a88394cfSJeff Kirsher 	dev->ethtool_ops = &netdev_ethtool_ops;
435a88394cfSJeff Kirsher 	dev->watchdog_timeo = TX_TIMEOUT;
436a88394cfSJeff Kirsher 
437a88394cfSJeff Kirsher 	i = register_netdev(dev);
438a88394cfSJeff Kirsher 	if (i)
439a88394cfSJeff Kirsher 		goto err_out_cleardev;
440a88394cfSJeff Kirsher 
441a88394cfSJeff Kirsher 	dev_info(&dev->dev, "%s at %p, %pM, IRQ %d\n",
442a88394cfSJeff Kirsher 		 pci_id_tbl[chip_idx].name, ioaddr, dev->dev_addr, irq);
443a88394cfSJeff Kirsher 
444a88394cfSJeff Kirsher 	if (np->drv_flags & CanHaveMII) {
445a88394cfSJeff Kirsher 		int phy, phy_idx = 0;
446a88394cfSJeff Kirsher 		for (phy = 1; phy < 32 && phy_idx < MII_CNT; phy++) {
447a88394cfSJeff Kirsher 			int mii_status = mdio_read(dev, phy, MII_BMSR);
448a88394cfSJeff Kirsher 			if (mii_status != 0xffff  &&  mii_status != 0x0000) {
449a88394cfSJeff Kirsher 				np->phys[phy_idx++] = phy;
450a88394cfSJeff Kirsher 				np->mii_if.advertising = mdio_read(dev, phy, MII_ADVERTISE);
451a88394cfSJeff Kirsher 				np->mii = (mdio_read(dev, phy, MII_PHYSID1) << 16)+
452a88394cfSJeff Kirsher 						mdio_read(dev, phy, MII_PHYSID2);
453a88394cfSJeff Kirsher 				dev_info(&dev->dev,
454a88394cfSJeff Kirsher 					 "MII PHY %08xh found at address %d, status 0x%04x advertising %04x\n",
455a88394cfSJeff Kirsher 					 np->mii, phy, mii_status,
456a88394cfSJeff Kirsher 					 np->mii_if.advertising);
457a88394cfSJeff Kirsher 			}
458a88394cfSJeff Kirsher 		}
459a88394cfSJeff Kirsher 		np->mii_cnt = phy_idx;
460a88394cfSJeff Kirsher 		np->mii_if.phy_id = np->phys[0];
461a88394cfSJeff Kirsher 		if (phy_idx == 0) {
462a88394cfSJeff Kirsher 			dev_warn(&dev->dev,
463a88394cfSJeff Kirsher 				 "MII PHY not found -- this device may not operate correctly\n");
464a88394cfSJeff Kirsher 		}
465a88394cfSJeff Kirsher 	}
466a88394cfSJeff Kirsher 
467a88394cfSJeff Kirsher 	find_cnt++;
468a88394cfSJeff Kirsher 	return 0;
469a88394cfSJeff Kirsher 
470a88394cfSJeff Kirsher err_out_cleardev:
471a88394cfSJeff Kirsher 	pci_iounmap(pdev, ioaddr);
472a88394cfSJeff Kirsher err_out_free_res:
473a88394cfSJeff Kirsher 	pci_release_regions(pdev);
474a88394cfSJeff Kirsher err_out_netdev:
475a88394cfSJeff Kirsher 	free_netdev (dev);
476a88394cfSJeff Kirsher 	return -ENODEV;
477a88394cfSJeff Kirsher }
478a88394cfSJeff Kirsher 
479a88394cfSJeff Kirsher 
480a88394cfSJeff Kirsher /* Read the EEPROM and MII Management Data I/O (MDIO) interfaces.  These are
481a88394cfSJeff Kirsher    often serial bit streams generated by the host processor.
482a88394cfSJeff Kirsher    The example below is for the common 93c46 EEPROM, 64 16 bit words. */
483a88394cfSJeff Kirsher 
484a88394cfSJeff Kirsher /* Delay between EEPROM clock transitions.
485a88394cfSJeff Kirsher    No extra delay is needed with 33Mhz PCI, but future 66Mhz access may need
486a88394cfSJeff Kirsher    a delay.  Note that pre-2.0.34 kernels had a cache-alignment bug that
487a88394cfSJeff Kirsher    made udelay() unreliable.
488a88394cfSJeff Kirsher    The old method of using an ISA access as a delay, __SLOW_DOWN_IO__, is
489a88394cfSJeff Kirsher    deprecated.
490a88394cfSJeff Kirsher */
491a88394cfSJeff Kirsher #define eeprom_delay(ee_addr)	ioread32(ee_addr)
492a88394cfSJeff Kirsher 
493a88394cfSJeff Kirsher enum EEPROM_Ctrl_Bits {
494a88394cfSJeff Kirsher 	EE_ShiftClk=0x02, EE_Write0=0x801, EE_Write1=0x805,
495a88394cfSJeff Kirsher 	EE_ChipSelect=0x801, EE_DataIn=0x08,
496a88394cfSJeff Kirsher };
497a88394cfSJeff Kirsher 
498a88394cfSJeff Kirsher /* The EEPROM commands include the alway-set leading bit. */
499a88394cfSJeff Kirsher enum EEPROM_Cmds {
500a88394cfSJeff Kirsher 	EE_WriteCmd=(5 << 6), EE_ReadCmd=(6 << 6), EE_EraseCmd=(7 << 6),
501a88394cfSJeff Kirsher };
502a88394cfSJeff Kirsher 
503a88394cfSJeff Kirsher static int eeprom_read(void __iomem *addr, int location)
504a88394cfSJeff Kirsher {
505a88394cfSJeff Kirsher 	int i;
506a88394cfSJeff Kirsher 	int retval = 0;
507a88394cfSJeff Kirsher 	void __iomem *ee_addr = addr + EECtrl;
508a88394cfSJeff Kirsher 	int read_cmd = location | EE_ReadCmd;
509a88394cfSJeff Kirsher 	iowrite32(EE_ChipSelect, ee_addr);
510a88394cfSJeff Kirsher 
511a88394cfSJeff Kirsher 	/* Shift the read command bits out. */
512a88394cfSJeff Kirsher 	for (i = 10; i >= 0; i--) {
513a88394cfSJeff Kirsher 		short dataval = (read_cmd & (1 << i)) ? EE_Write1 : EE_Write0;
514a88394cfSJeff Kirsher 		iowrite32(dataval, ee_addr);
515a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
516a88394cfSJeff Kirsher 		iowrite32(dataval | EE_ShiftClk, ee_addr);
517a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
518a88394cfSJeff Kirsher 	}
519a88394cfSJeff Kirsher 	iowrite32(EE_ChipSelect, ee_addr);
520a88394cfSJeff Kirsher 	eeprom_delay(ee_addr);
521a88394cfSJeff Kirsher 
522a88394cfSJeff Kirsher 	for (i = 16; i > 0; i--) {
523a88394cfSJeff Kirsher 		iowrite32(EE_ChipSelect | EE_ShiftClk, ee_addr);
524a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
525a88394cfSJeff Kirsher 		retval = (retval << 1) | ((ioread32(ee_addr) & EE_DataIn) ? 1 : 0);
526a88394cfSJeff Kirsher 		iowrite32(EE_ChipSelect, ee_addr);
527a88394cfSJeff Kirsher 		eeprom_delay(ee_addr);
528a88394cfSJeff Kirsher 	}
529a88394cfSJeff Kirsher 
530a88394cfSJeff Kirsher 	/* Terminate the EEPROM access. */
531a88394cfSJeff Kirsher 	iowrite32(0, ee_addr);
532a88394cfSJeff Kirsher 	return retval;
533a88394cfSJeff Kirsher }
534a88394cfSJeff Kirsher 
535a88394cfSJeff Kirsher /*  MII transceiver control section.
536a88394cfSJeff Kirsher 	Read and write the MII registers using software-generated serial
537a88394cfSJeff Kirsher 	MDIO protocol.  See the MII specifications or DP83840A data sheet
538a88394cfSJeff Kirsher 	for details.
539a88394cfSJeff Kirsher 
540a88394cfSJeff Kirsher 	The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
541a88394cfSJeff Kirsher 	met by back-to-back 33Mhz PCI cycles. */
542a88394cfSJeff Kirsher #define mdio_delay(mdio_addr) ioread32(mdio_addr)
543a88394cfSJeff Kirsher 
544a88394cfSJeff Kirsher /* Set iff a MII transceiver on any interface requires mdio preamble.
545a88394cfSJeff Kirsher    This only set with older transceivers, so the extra
546a88394cfSJeff Kirsher    code size of a per-interface flag is not worthwhile. */
547a88394cfSJeff Kirsher static char mii_preamble_required = 1;
548a88394cfSJeff Kirsher 
549a88394cfSJeff Kirsher #define MDIO_WRITE0 (MDIO_EnbOutput)
550a88394cfSJeff Kirsher #define MDIO_WRITE1 (MDIO_DataOut | MDIO_EnbOutput)
551a88394cfSJeff Kirsher 
552a88394cfSJeff Kirsher /* Generate the preamble required for initial synchronization and
553a88394cfSJeff Kirsher    a few older transceivers. */
554a88394cfSJeff Kirsher static void mdio_sync(void __iomem *mdio_addr)
555a88394cfSJeff Kirsher {
556a88394cfSJeff Kirsher 	int bits = 32;
557a88394cfSJeff Kirsher 
558a88394cfSJeff Kirsher 	/* Establish sync by sending at least 32 logic ones. */
559a88394cfSJeff Kirsher 	while (--bits >= 0) {
560a88394cfSJeff Kirsher 		iowrite32(MDIO_WRITE1, mdio_addr);
561a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
562a88394cfSJeff Kirsher 		iowrite32(MDIO_WRITE1 | MDIO_ShiftClk, mdio_addr);
563a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
564a88394cfSJeff Kirsher 	}
565a88394cfSJeff Kirsher }
566a88394cfSJeff Kirsher 
567a88394cfSJeff Kirsher static int mdio_read(struct net_device *dev, int phy_id, int location)
568a88394cfSJeff Kirsher {
569a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
570a88394cfSJeff Kirsher 	void __iomem *mdio_addr = np->base_addr + MIICtrl;
571a88394cfSJeff Kirsher 	int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
572a88394cfSJeff Kirsher 	int i, retval = 0;
573a88394cfSJeff Kirsher 
574a88394cfSJeff Kirsher 	if (mii_preamble_required)
575a88394cfSJeff Kirsher 		mdio_sync(mdio_addr);
576a88394cfSJeff Kirsher 
577a88394cfSJeff Kirsher 	/* Shift the read command bits out. */
578a88394cfSJeff Kirsher 	for (i = 15; i >= 0; i--) {
579a88394cfSJeff Kirsher 		int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
580a88394cfSJeff Kirsher 
581a88394cfSJeff Kirsher 		iowrite32(dataval, mdio_addr);
582a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
583a88394cfSJeff Kirsher 		iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
584a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
585a88394cfSJeff Kirsher 	}
586a88394cfSJeff Kirsher 	/* Read the two transition, 16 data, and wire-idle bits. */
587a88394cfSJeff Kirsher 	for (i = 20; i > 0; i--) {
588a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn, mdio_addr);
589a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
590a88394cfSJeff Kirsher 		retval = (retval << 1) | ((ioread32(mdio_addr) & MDIO_DataIn) ? 1 : 0);
591a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
592a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
593a88394cfSJeff Kirsher 	}
594a88394cfSJeff Kirsher 	return (retval>>1) & 0xffff;
595a88394cfSJeff Kirsher }
596a88394cfSJeff Kirsher 
597a88394cfSJeff Kirsher static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
598a88394cfSJeff Kirsher {
599a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
600a88394cfSJeff Kirsher 	void __iomem *mdio_addr = np->base_addr + MIICtrl;
601a88394cfSJeff Kirsher 	int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
602a88394cfSJeff Kirsher 	int i;
603a88394cfSJeff Kirsher 
604a88394cfSJeff Kirsher 	if (location == 4  &&  phy_id == np->phys[0])
605a88394cfSJeff Kirsher 		np->mii_if.advertising = value;
606a88394cfSJeff Kirsher 
607a88394cfSJeff Kirsher 	if (mii_preamble_required)
608a88394cfSJeff Kirsher 		mdio_sync(mdio_addr);
609a88394cfSJeff Kirsher 
610a88394cfSJeff Kirsher 	/* Shift the command bits out. */
611a88394cfSJeff Kirsher 	for (i = 31; i >= 0; i--) {
612a88394cfSJeff Kirsher 		int dataval = (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
613a88394cfSJeff Kirsher 
614a88394cfSJeff Kirsher 		iowrite32(dataval, mdio_addr);
615a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
616a88394cfSJeff Kirsher 		iowrite32(dataval | MDIO_ShiftClk, mdio_addr);
617a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
618a88394cfSJeff Kirsher 	}
619a88394cfSJeff Kirsher 	/* Clear out extra bits. */
620a88394cfSJeff Kirsher 	for (i = 2; i > 0; i--) {
621a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn, mdio_addr);
622a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
623a88394cfSJeff Kirsher 		iowrite32(MDIO_EnbIn | MDIO_ShiftClk, mdio_addr);
624a88394cfSJeff Kirsher 		mdio_delay(mdio_addr);
625a88394cfSJeff Kirsher 	}
626a88394cfSJeff Kirsher }
627a88394cfSJeff Kirsher 
628a88394cfSJeff Kirsher 
629a88394cfSJeff Kirsher static int netdev_open(struct net_device *dev)
630a88394cfSJeff Kirsher {
631a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
632a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
633c0bd55efSFrancois Romieu 	const int irq = np->pci_dev->irq;
634a88394cfSJeff Kirsher 	int i;
635a88394cfSJeff Kirsher 
636a88394cfSJeff Kirsher 	iowrite32(0x00000001, ioaddr + PCIBusCfg);		/* Reset */
637a88394cfSJeff Kirsher 
638a88394cfSJeff Kirsher 	netif_device_detach(dev);
639c0bd55efSFrancois Romieu 	i = request_irq(irq, intr_handler, IRQF_SHARED, dev->name, dev);
640a88394cfSJeff Kirsher 	if (i)
641a88394cfSJeff Kirsher 		goto out_err;
642a88394cfSJeff Kirsher 
643a88394cfSJeff Kirsher 	if (debug > 1)
644c0bd55efSFrancois Romieu 		netdev_dbg(dev, "w89c840_open() irq %d\n", irq);
645a88394cfSJeff Kirsher 
646a88394cfSJeff Kirsher 	if((i=alloc_ringdesc(dev)))
647a88394cfSJeff Kirsher 		goto out_err;
648a88394cfSJeff Kirsher 
649a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
650a88394cfSJeff Kirsher 	netif_device_attach(dev);
651a88394cfSJeff Kirsher 	init_registers(dev);
652a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
653a88394cfSJeff Kirsher 
654a88394cfSJeff Kirsher 	netif_start_queue(dev);
655a88394cfSJeff Kirsher 	if (debug > 2)
656a88394cfSJeff Kirsher 		netdev_dbg(dev, "Done netdev_open()\n");
657a88394cfSJeff Kirsher 
658a88394cfSJeff Kirsher 	/* Set the timer to check for link beat. */
659a88394cfSJeff Kirsher 	init_timer(&np->timer);
660a88394cfSJeff Kirsher 	np->timer.expires = jiffies + 1*HZ;
661a88394cfSJeff Kirsher 	np->timer.data = (unsigned long)dev;
662a88394cfSJeff Kirsher 	np->timer.function = netdev_timer;				/* timer handler */
663a88394cfSJeff Kirsher 	add_timer(&np->timer);
664a88394cfSJeff Kirsher 	return 0;
665a88394cfSJeff Kirsher out_err:
666a88394cfSJeff Kirsher 	netif_device_attach(dev);
667a88394cfSJeff Kirsher 	return i;
668a88394cfSJeff Kirsher }
669a88394cfSJeff Kirsher 
670a88394cfSJeff Kirsher #define MII_DAVICOM_DM9101	0x0181b800
671a88394cfSJeff Kirsher 
672a88394cfSJeff Kirsher static int update_link(struct net_device *dev)
673a88394cfSJeff Kirsher {
674a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
675a88394cfSJeff Kirsher 	int duplex, fasteth, result, mii_reg;
676a88394cfSJeff Kirsher 
677a88394cfSJeff Kirsher 	/* BSMR */
678a88394cfSJeff Kirsher 	mii_reg = mdio_read(dev, np->phys[0], MII_BMSR);
679a88394cfSJeff Kirsher 
680a88394cfSJeff Kirsher 	if (mii_reg == 0xffff)
681a88394cfSJeff Kirsher 		return np->csr6;
682a88394cfSJeff Kirsher 	/* reread: the link status bit is sticky */
683a88394cfSJeff Kirsher 	mii_reg = mdio_read(dev, np->phys[0], MII_BMSR);
684a88394cfSJeff Kirsher 	if (!(mii_reg & 0x4)) {
685a88394cfSJeff Kirsher 		if (netif_carrier_ok(dev)) {
686a88394cfSJeff Kirsher 			if (debug)
687a88394cfSJeff Kirsher 				dev_info(&dev->dev,
688a88394cfSJeff Kirsher 					 "MII #%d reports no link. Disabling watchdog\n",
689a88394cfSJeff Kirsher 					 np->phys[0]);
690a88394cfSJeff Kirsher 			netif_carrier_off(dev);
691a88394cfSJeff Kirsher 		}
692a88394cfSJeff Kirsher 		return np->csr6;
693a88394cfSJeff Kirsher 	}
694a88394cfSJeff Kirsher 	if (!netif_carrier_ok(dev)) {
695a88394cfSJeff Kirsher 		if (debug)
696a88394cfSJeff Kirsher 			dev_info(&dev->dev,
697a88394cfSJeff Kirsher 				 "MII #%d link is back. Enabling watchdog\n",
698a88394cfSJeff Kirsher 				 np->phys[0]);
699a88394cfSJeff Kirsher 		netif_carrier_on(dev);
700a88394cfSJeff Kirsher 	}
701a88394cfSJeff Kirsher 
702a88394cfSJeff Kirsher 	if ((np->mii & ~0xf) == MII_DAVICOM_DM9101) {
703a88394cfSJeff Kirsher 		/* If the link partner doesn't support autonegotiation
704a88394cfSJeff Kirsher 		 * the MII detects it's abilities with the "parallel detection".
705a88394cfSJeff Kirsher 		 * Some MIIs update the LPA register to the result of the parallel
706a88394cfSJeff Kirsher 		 * detection, some don't.
707a88394cfSJeff Kirsher 		 * The Davicom PHY [at least 0181b800] doesn't.
708a88394cfSJeff Kirsher 		 * Instead bit 9 and 13 of the BMCR are updated to the result
709a88394cfSJeff Kirsher 		 * of the negotiation..
710a88394cfSJeff Kirsher 		 */
711a88394cfSJeff Kirsher 		mii_reg = mdio_read(dev, np->phys[0], MII_BMCR);
712a88394cfSJeff Kirsher 		duplex = mii_reg & BMCR_FULLDPLX;
713a88394cfSJeff Kirsher 		fasteth = mii_reg & BMCR_SPEED100;
714a88394cfSJeff Kirsher 	} else {
715a88394cfSJeff Kirsher 		int negotiated;
716a88394cfSJeff Kirsher 		mii_reg	= mdio_read(dev, np->phys[0], MII_LPA);
717a88394cfSJeff Kirsher 		negotiated = mii_reg & np->mii_if.advertising;
718a88394cfSJeff Kirsher 
719a88394cfSJeff Kirsher 		duplex = (negotiated & LPA_100FULL) || ((negotiated & 0x02C0) == LPA_10FULL);
720a88394cfSJeff Kirsher 		fasteth = negotiated & 0x380;
721a88394cfSJeff Kirsher 	}
722a88394cfSJeff Kirsher 	duplex |= np->mii_if.force_media;
723a88394cfSJeff Kirsher 	/* remove fastether and fullduplex */
724a88394cfSJeff Kirsher 	result = np->csr6 & ~0x20000200;
725a88394cfSJeff Kirsher 	if (duplex)
726a88394cfSJeff Kirsher 		result |= 0x200;
727a88394cfSJeff Kirsher 	if (fasteth)
728a88394cfSJeff Kirsher 		result |= 0x20000000;
729a88394cfSJeff Kirsher 	if (result != np->csr6 && debug)
730a88394cfSJeff Kirsher 		dev_info(&dev->dev,
731a88394cfSJeff Kirsher 			 "Setting %dMBit-%s-duplex based on MII#%d\n",
732a88394cfSJeff Kirsher 			 fasteth ? 100 : 10, duplex ? "full" : "half",
733a88394cfSJeff Kirsher 			 np->phys[0]);
734a88394cfSJeff Kirsher 	return result;
735a88394cfSJeff Kirsher }
736a88394cfSJeff Kirsher 
737a88394cfSJeff Kirsher #define RXTX_TIMEOUT	2000
738a88394cfSJeff Kirsher static inline void update_csr6(struct net_device *dev, int new)
739a88394cfSJeff Kirsher {
740a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
741a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
742a88394cfSJeff Kirsher 	int limit = RXTX_TIMEOUT;
743a88394cfSJeff Kirsher 
744a88394cfSJeff Kirsher 	if (!netif_device_present(dev))
745a88394cfSJeff Kirsher 		new = 0;
746a88394cfSJeff Kirsher 	if (new==np->csr6)
747a88394cfSJeff Kirsher 		return;
748a88394cfSJeff Kirsher 	/* stop both Tx and Rx processes */
749a88394cfSJeff Kirsher 	iowrite32(np->csr6 & ~0x2002, ioaddr + NetworkConfig);
750a88394cfSJeff Kirsher 	/* wait until they have really stopped */
751a88394cfSJeff Kirsher 	for (;;) {
752a88394cfSJeff Kirsher 		int csr5 = ioread32(ioaddr + IntrStatus);
753a88394cfSJeff Kirsher 		int t;
754a88394cfSJeff Kirsher 
755a88394cfSJeff Kirsher 		t = (csr5 >> 17) & 0x07;
756a88394cfSJeff Kirsher 		if (t==0||t==1) {
757a88394cfSJeff Kirsher 			/* rx stopped */
758a88394cfSJeff Kirsher 			t = (csr5 >> 20) & 0x07;
759a88394cfSJeff Kirsher 			if (t==0||t==1)
760a88394cfSJeff Kirsher 				break;
761a88394cfSJeff Kirsher 		}
762a88394cfSJeff Kirsher 
763a88394cfSJeff Kirsher 		limit--;
764a88394cfSJeff Kirsher 		if(!limit) {
765a88394cfSJeff Kirsher 			dev_info(&dev->dev,
766a88394cfSJeff Kirsher 				 "couldn't stop rxtx, IntrStatus %xh\n", csr5);
767a88394cfSJeff Kirsher 			break;
768a88394cfSJeff Kirsher 		}
769a88394cfSJeff Kirsher 		udelay(1);
770a88394cfSJeff Kirsher 	}
771a88394cfSJeff Kirsher 	np->csr6 = new;
772a88394cfSJeff Kirsher 	/* and restart them with the new configuration */
773a88394cfSJeff Kirsher 	iowrite32(np->csr6, ioaddr + NetworkConfig);
774a88394cfSJeff Kirsher 	if (new & 0x200)
775a88394cfSJeff Kirsher 		np->mii_if.full_duplex = 1;
776a88394cfSJeff Kirsher }
777a88394cfSJeff Kirsher 
778a88394cfSJeff Kirsher static void netdev_timer(unsigned long data)
779a88394cfSJeff Kirsher {
780a88394cfSJeff Kirsher 	struct net_device *dev = (struct net_device *)data;
781a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
782a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
783a88394cfSJeff Kirsher 
784a88394cfSJeff Kirsher 	if (debug > 2)
785a88394cfSJeff Kirsher 		netdev_dbg(dev, "Media selection timer tick, status %08x config %08x\n",
786a88394cfSJeff Kirsher 			   ioread32(ioaddr + IntrStatus),
787a88394cfSJeff Kirsher 			   ioread32(ioaddr + NetworkConfig));
788a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
789a88394cfSJeff Kirsher 	update_csr6(dev, update_link(dev));
790a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
791a88394cfSJeff Kirsher 	np->timer.expires = jiffies + 10*HZ;
792a88394cfSJeff Kirsher 	add_timer(&np->timer);
793a88394cfSJeff Kirsher }
794a88394cfSJeff Kirsher 
795a88394cfSJeff Kirsher static void init_rxtx_rings(struct net_device *dev)
796a88394cfSJeff Kirsher {
797a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
798a88394cfSJeff Kirsher 	int i;
799a88394cfSJeff Kirsher 
800a88394cfSJeff Kirsher 	np->rx_head_desc = &np->rx_ring[0];
801a88394cfSJeff Kirsher 	np->tx_ring = (struct w840_tx_desc*)&np->rx_ring[RX_RING_SIZE];
802a88394cfSJeff Kirsher 
803a88394cfSJeff Kirsher 	/* Initial all Rx descriptors. */
804a88394cfSJeff Kirsher 	for (i = 0; i < RX_RING_SIZE; i++) {
805a88394cfSJeff Kirsher 		np->rx_ring[i].length = np->rx_buf_sz;
806a88394cfSJeff Kirsher 		np->rx_ring[i].status = 0;
807a88394cfSJeff Kirsher 		np->rx_skbuff[i] = NULL;
808a88394cfSJeff Kirsher 	}
809a88394cfSJeff Kirsher 	/* Mark the last entry as wrapping the ring. */
810a88394cfSJeff Kirsher 	np->rx_ring[i-1].length |= DescEndRing;
811a88394cfSJeff Kirsher 
812a88394cfSJeff Kirsher 	/* Fill in the Rx buffers.  Handle allocation failure gracefully. */
813a88394cfSJeff Kirsher 	for (i = 0; i < RX_RING_SIZE; i++) {
81421a4e469SPradeep A Dalvi 		struct sk_buff *skb = netdev_alloc_skb(dev, np->rx_buf_sz);
815a88394cfSJeff Kirsher 		np->rx_skbuff[i] = skb;
816a88394cfSJeff Kirsher 		if (skb == NULL)
817a88394cfSJeff Kirsher 			break;
818a88394cfSJeff Kirsher 		np->rx_addr[i] = pci_map_single(np->pci_dev,skb->data,
819a88394cfSJeff Kirsher 					np->rx_buf_sz,PCI_DMA_FROMDEVICE);
820a88394cfSJeff Kirsher 
821a88394cfSJeff Kirsher 		np->rx_ring[i].buffer1 = np->rx_addr[i];
822a88394cfSJeff Kirsher 		np->rx_ring[i].status = DescOwned;
823a88394cfSJeff Kirsher 	}
824a88394cfSJeff Kirsher 
825a88394cfSJeff Kirsher 	np->cur_rx = 0;
826a88394cfSJeff Kirsher 	np->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
827a88394cfSJeff Kirsher 
828a88394cfSJeff Kirsher 	/* Initialize the Tx descriptors */
829a88394cfSJeff Kirsher 	for (i = 0; i < TX_RING_SIZE; i++) {
830a88394cfSJeff Kirsher 		np->tx_skbuff[i] = NULL;
831a88394cfSJeff Kirsher 		np->tx_ring[i].status = 0;
832a88394cfSJeff Kirsher 	}
833a88394cfSJeff Kirsher 	np->tx_full = 0;
834a88394cfSJeff Kirsher 	np->tx_q_bytes = np->dirty_tx = np->cur_tx = 0;
835a88394cfSJeff Kirsher 
836a88394cfSJeff Kirsher 	iowrite32(np->ring_dma_addr, np->base_addr + RxRingPtr);
837a88394cfSJeff Kirsher 	iowrite32(np->ring_dma_addr+sizeof(struct w840_rx_desc)*RX_RING_SIZE,
838a88394cfSJeff Kirsher 		np->base_addr + TxRingPtr);
839a88394cfSJeff Kirsher 
840a88394cfSJeff Kirsher }
841a88394cfSJeff Kirsher 
842a88394cfSJeff Kirsher static void free_rxtx_rings(struct netdev_private* np)
843a88394cfSJeff Kirsher {
844a88394cfSJeff Kirsher 	int i;
845a88394cfSJeff Kirsher 	/* Free all the skbuffs in the Rx queue. */
846a88394cfSJeff Kirsher 	for (i = 0; i < RX_RING_SIZE; i++) {
847a88394cfSJeff Kirsher 		np->rx_ring[i].status = 0;
848a88394cfSJeff Kirsher 		if (np->rx_skbuff[i]) {
849a88394cfSJeff Kirsher 			pci_unmap_single(np->pci_dev,
850a88394cfSJeff Kirsher 						np->rx_addr[i],
851a88394cfSJeff Kirsher 						np->rx_skbuff[i]->len,
852a88394cfSJeff Kirsher 						PCI_DMA_FROMDEVICE);
853a88394cfSJeff Kirsher 			dev_kfree_skb(np->rx_skbuff[i]);
854a88394cfSJeff Kirsher 		}
855a88394cfSJeff Kirsher 		np->rx_skbuff[i] = NULL;
856a88394cfSJeff Kirsher 	}
857a88394cfSJeff Kirsher 	for (i = 0; i < TX_RING_SIZE; i++) {
858a88394cfSJeff Kirsher 		if (np->tx_skbuff[i]) {
859a88394cfSJeff Kirsher 			pci_unmap_single(np->pci_dev,
860a88394cfSJeff Kirsher 						np->tx_addr[i],
861a88394cfSJeff Kirsher 						np->tx_skbuff[i]->len,
862a88394cfSJeff Kirsher 						PCI_DMA_TODEVICE);
863a88394cfSJeff Kirsher 			dev_kfree_skb(np->tx_skbuff[i]);
864a88394cfSJeff Kirsher 		}
865a88394cfSJeff Kirsher 		np->tx_skbuff[i] = NULL;
866a88394cfSJeff Kirsher 	}
867a88394cfSJeff Kirsher }
868a88394cfSJeff Kirsher 
869a88394cfSJeff Kirsher static void init_registers(struct net_device *dev)
870a88394cfSJeff Kirsher {
871a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
872a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
873a88394cfSJeff Kirsher 	int i;
874a88394cfSJeff Kirsher 
875a88394cfSJeff Kirsher 	for (i = 0; i < 6; i++)
876a88394cfSJeff Kirsher 		iowrite8(dev->dev_addr[i], ioaddr + StationAddr + i);
877a88394cfSJeff Kirsher 
878a88394cfSJeff Kirsher 	/* Initialize other registers. */
879a88394cfSJeff Kirsher #ifdef __BIG_ENDIAN
880a88394cfSJeff Kirsher 	i = (1<<20);	/* Big-endian descriptors */
881a88394cfSJeff Kirsher #else
882a88394cfSJeff Kirsher 	i = 0;
883a88394cfSJeff Kirsher #endif
884a88394cfSJeff Kirsher 	i |= (0x04<<2);		/* skip length 4 u32 */
885a88394cfSJeff Kirsher 	i |= 0x02;		/* give Rx priority */
886a88394cfSJeff Kirsher 
887a88394cfSJeff Kirsher 	/* Configure the PCI bus bursts and FIFO thresholds.
888a88394cfSJeff Kirsher 	   486: Set 8 longword cache alignment, 8 longword burst.
889a88394cfSJeff Kirsher 	   586: Set 16 longword cache alignment, no burst limit.
890a88394cfSJeff Kirsher 	   Cache alignment bits 15:14	     Burst length 13:8
891a88394cfSJeff Kirsher 		0000	<not allowed> 		0000 align to cache	0800 8 longwords
892a88394cfSJeff Kirsher 		4000	8  longwords		0100 1 longword		1000 16 longwords
893a88394cfSJeff Kirsher 		8000	16 longwords		0200 2 longwords	2000 32 longwords
894a88394cfSJeff Kirsher 		C000	32  longwords		0400 4 longwords */
895a88394cfSJeff Kirsher 
896a88394cfSJeff Kirsher #if defined (__i386__) && !defined(MODULE)
897a88394cfSJeff Kirsher 	/* When not a module we can work around broken '486 PCI boards. */
898a88394cfSJeff Kirsher 	if (boot_cpu_data.x86 <= 4) {
899a88394cfSJeff Kirsher 		i |= 0x4800;
900a88394cfSJeff Kirsher 		dev_info(&dev->dev,
901a88394cfSJeff Kirsher 			 "This is a 386/486 PCI system, setting cache alignment to 8 longwords\n");
902a88394cfSJeff Kirsher 	} else {
903a88394cfSJeff Kirsher 		i |= 0xE000;
904a88394cfSJeff Kirsher 	}
905a88394cfSJeff Kirsher #elif defined(__powerpc__) || defined(__i386__) || defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
906a88394cfSJeff Kirsher 	i |= 0xE000;
90798830dd0SArnd Bergmann #elif defined(CONFIG_SPARC) || defined (CONFIG_PARISC) || defined(CONFIG_ARM)
908a88394cfSJeff Kirsher 	i |= 0x4800;
909a88394cfSJeff Kirsher #else
910a88394cfSJeff Kirsher #warning Processor architecture undefined
911a88394cfSJeff Kirsher 	i |= 0x4800;
912a88394cfSJeff Kirsher #endif
913a88394cfSJeff Kirsher 	iowrite32(i, ioaddr + PCIBusCfg);
914a88394cfSJeff Kirsher 
915a88394cfSJeff Kirsher 	np->csr6 = 0;
916a88394cfSJeff Kirsher 	/* 128 byte Tx threshold;
917a88394cfSJeff Kirsher 		Transmit on; Receive on; */
918a88394cfSJeff Kirsher 	update_csr6(dev, 0x00022002 | update_link(dev) | __set_rx_mode(dev));
919a88394cfSJeff Kirsher 
920a88394cfSJeff Kirsher 	/* Clear and Enable interrupts by setting the interrupt mask. */
921a88394cfSJeff Kirsher 	iowrite32(0x1A0F5, ioaddr + IntrStatus);
922a88394cfSJeff Kirsher 	iowrite32(0x1A0F5, ioaddr + IntrEnable);
923a88394cfSJeff Kirsher 
924a88394cfSJeff Kirsher 	iowrite32(0, ioaddr + RxStartDemand);
925a88394cfSJeff Kirsher }
926a88394cfSJeff Kirsher 
927a88394cfSJeff Kirsher static void tx_timeout(struct net_device *dev)
928a88394cfSJeff Kirsher {
929a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
930a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
931c0bd55efSFrancois Romieu 	const int irq = np->pci_dev->irq;
932a88394cfSJeff Kirsher 
933a88394cfSJeff Kirsher 	dev_warn(&dev->dev, "Transmit timed out, status %08x, resetting...\n",
934a88394cfSJeff Kirsher 		 ioread32(ioaddr + IntrStatus));
935a88394cfSJeff Kirsher 
936a88394cfSJeff Kirsher 	{
937a88394cfSJeff Kirsher 		int i;
938a88394cfSJeff Kirsher 		printk(KERN_DEBUG "  Rx ring %p: ", np->rx_ring);
939a88394cfSJeff Kirsher 		for (i = 0; i < RX_RING_SIZE; i++)
940a88394cfSJeff Kirsher 			printk(KERN_CONT " %08x", (unsigned int)np->rx_ring[i].status);
941a88394cfSJeff Kirsher 		printk(KERN_CONT "\n");
942a88394cfSJeff Kirsher 		printk(KERN_DEBUG "  Tx ring %p: ", np->tx_ring);
943a88394cfSJeff Kirsher 		for (i = 0; i < TX_RING_SIZE; i++)
944a88394cfSJeff Kirsher 			printk(KERN_CONT " %08x", np->tx_ring[i].status);
945a88394cfSJeff Kirsher 		printk(KERN_CONT "\n");
946a88394cfSJeff Kirsher 	}
947a88394cfSJeff Kirsher 	printk(KERN_DEBUG "Tx cur %d Tx dirty %d Tx Full %d, q bytes %d\n",
948a88394cfSJeff Kirsher 	       np->cur_tx, np->dirty_tx, np->tx_full, np->tx_q_bytes);
949a88394cfSJeff Kirsher 	printk(KERN_DEBUG "Tx Descriptor addr %xh\n", ioread32(ioaddr+0x4C));
950a88394cfSJeff Kirsher 
951c0bd55efSFrancois Romieu 	disable_irq(irq);
952a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
953a88394cfSJeff Kirsher 	/*
954a88394cfSJeff Kirsher 	 * Under high load dirty_tx and the internal tx descriptor pointer
955a88394cfSJeff Kirsher 	 * come out of sync, thus perform a software reset and reinitialize
956a88394cfSJeff Kirsher 	 * everything.
957a88394cfSJeff Kirsher 	 */
958a88394cfSJeff Kirsher 
959a88394cfSJeff Kirsher 	iowrite32(1, np->base_addr+PCIBusCfg);
960a88394cfSJeff Kirsher 	udelay(1);
961a88394cfSJeff Kirsher 
962a88394cfSJeff Kirsher 	free_rxtx_rings(np);
963a88394cfSJeff Kirsher 	init_rxtx_rings(dev);
964a88394cfSJeff Kirsher 	init_registers(dev);
965a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
966c0bd55efSFrancois Romieu 	enable_irq(irq);
967a88394cfSJeff Kirsher 
968a88394cfSJeff Kirsher 	netif_wake_queue(dev);
969a88394cfSJeff Kirsher 	dev->trans_start = jiffies; /* prevent tx timeout */
970a88394cfSJeff Kirsher 	np->stats.tx_errors++;
971a88394cfSJeff Kirsher }
972a88394cfSJeff Kirsher 
973a88394cfSJeff Kirsher /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
974a88394cfSJeff Kirsher static int alloc_ringdesc(struct net_device *dev)
975a88394cfSJeff Kirsher {
976a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
977a88394cfSJeff Kirsher 
978a88394cfSJeff Kirsher 	np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
979a88394cfSJeff Kirsher 
980a88394cfSJeff Kirsher 	np->rx_ring = pci_alloc_consistent(np->pci_dev,
981a88394cfSJeff Kirsher 			sizeof(struct w840_rx_desc)*RX_RING_SIZE +
982a88394cfSJeff Kirsher 			sizeof(struct w840_tx_desc)*TX_RING_SIZE,
983a88394cfSJeff Kirsher 			&np->ring_dma_addr);
984a88394cfSJeff Kirsher 	if(!np->rx_ring)
985a88394cfSJeff Kirsher 		return -ENOMEM;
986a88394cfSJeff Kirsher 	init_rxtx_rings(dev);
987a88394cfSJeff Kirsher 	return 0;
988a88394cfSJeff Kirsher }
989a88394cfSJeff Kirsher 
990a88394cfSJeff Kirsher static void free_ringdesc(struct netdev_private *np)
991a88394cfSJeff Kirsher {
992a88394cfSJeff Kirsher 	pci_free_consistent(np->pci_dev,
993a88394cfSJeff Kirsher 			sizeof(struct w840_rx_desc)*RX_RING_SIZE +
994a88394cfSJeff Kirsher 			sizeof(struct w840_tx_desc)*TX_RING_SIZE,
995a88394cfSJeff Kirsher 			np->rx_ring, np->ring_dma_addr);
996a88394cfSJeff Kirsher 
997a88394cfSJeff Kirsher }
998a88394cfSJeff Kirsher 
999a88394cfSJeff Kirsher static netdev_tx_t start_tx(struct sk_buff *skb, struct net_device *dev)
1000a88394cfSJeff Kirsher {
1001a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1002a88394cfSJeff Kirsher 	unsigned entry;
1003a88394cfSJeff Kirsher 
1004a88394cfSJeff Kirsher 	/* Caution: the write order is important here, set the field
1005a88394cfSJeff Kirsher 	   with the "ownership" bits last. */
1006a88394cfSJeff Kirsher 
1007a88394cfSJeff Kirsher 	/* Calculate the next Tx descriptor entry. */
1008a88394cfSJeff Kirsher 	entry = np->cur_tx % TX_RING_SIZE;
1009a88394cfSJeff Kirsher 
1010a88394cfSJeff Kirsher 	np->tx_addr[entry] = pci_map_single(np->pci_dev,
1011a88394cfSJeff Kirsher 				skb->data,skb->len, PCI_DMA_TODEVICE);
1012a88394cfSJeff Kirsher 	np->tx_skbuff[entry] = skb;
1013a88394cfSJeff Kirsher 
1014a88394cfSJeff Kirsher 	np->tx_ring[entry].buffer1 = np->tx_addr[entry];
1015a88394cfSJeff Kirsher 	if (skb->len < TX_BUFLIMIT) {
1016a88394cfSJeff Kirsher 		np->tx_ring[entry].length = DescWholePkt | skb->len;
1017a88394cfSJeff Kirsher 	} else {
1018a88394cfSJeff Kirsher 		int len = skb->len - TX_BUFLIMIT;
1019a88394cfSJeff Kirsher 
1020a88394cfSJeff Kirsher 		np->tx_ring[entry].buffer2 = np->tx_addr[entry]+TX_BUFLIMIT;
1021a88394cfSJeff Kirsher 		np->tx_ring[entry].length = DescWholePkt | (len << 11) | TX_BUFLIMIT;
1022a88394cfSJeff Kirsher 	}
1023a88394cfSJeff Kirsher 	if(entry == TX_RING_SIZE-1)
1024a88394cfSJeff Kirsher 		np->tx_ring[entry].length |= DescEndRing;
1025a88394cfSJeff Kirsher 
1026a88394cfSJeff Kirsher 	/* Now acquire the irq spinlock.
1027a88394cfSJeff Kirsher 	 * The difficult race is the ordering between
1028a88394cfSJeff Kirsher 	 * increasing np->cur_tx and setting DescOwned:
1029a88394cfSJeff Kirsher 	 * - if np->cur_tx is increased first the interrupt
1030a88394cfSJeff Kirsher 	 *   handler could consider the packet as transmitted
1031a88394cfSJeff Kirsher 	 *   since DescOwned is cleared.
1032a88394cfSJeff Kirsher 	 * - If DescOwned is set first the NIC could report the
1033a88394cfSJeff Kirsher 	 *   packet as sent, but the interrupt handler would ignore it
1034a88394cfSJeff Kirsher 	 *   since the np->cur_tx was not yet increased.
1035a88394cfSJeff Kirsher 	 */
1036a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1037a88394cfSJeff Kirsher 	np->cur_tx++;
1038a88394cfSJeff Kirsher 
1039a88394cfSJeff Kirsher 	wmb(); /* flush length, buffer1, buffer2 */
1040a88394cfSJeff Kirsher 	np->tx_ring[entry].status = DescOwned;
1041a88394cfSJeff Kirsher 	wmb(); /* flush status and kick the hardware */
1042a88394cfSJeff Kirsher 	iowrite32(0, np->base_addr + TxStartDemand);
1043a88394cfSJeff Kirsher 	np->tx_q_bytes += skb->len;
1044a88394cfSJeff Kirsher 	/* Work around horrible bug in the chip by marking the queue as full
1045a88394cfSJeff Kirsher 	   when we do not have FIFO room for a maximum sized packet. */
1046a88394cfSJeff Kirsher 	if (np->cur_tx - np->dirty_tx > TX_QUEUE_LEN ||
1047a88394cfSJeff Kirsher 		((np->drv_flags & HasBrokenTx) && np->tx_q_bytes > TX_BUG_FIFO_LIMIT)) {
1048a88394cfSJeff Kirsher 		netif_stop_queue(dev);
1049a88394cfSJeff Kirsher 		wmb();
1050a88394cfSJeff Kirsher 		np->tx_full = 1;
1051a88394cfSJeff Kirsher 	}
1052a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1053a88394cfSJeff Kirsher 
1054a88394cfSJeff Kirsher 	if (debug > 4) {
1055a88394cfSJeff Kirsher 		netdev_dbg(dev, "Transmit frame #%d queued in slot %d\n",
1056a88394cfSJeff Kirsher 			   np->cur_tx, entry);
1057a88394cfSJeff Kirsher 	}
1058a88394cfSJeff Kirsher 	return NETDEV_TX_OK;
1059a88394cfSJeff Kirsher }
1060a88394cfSJeff Kirsher 
1061a88394cfSJeff Kirsher static void netdev_tx_done(struct net_device *dev)
1062a88394cfSJeff Kirsher {
1063a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1064a88394cfSJeff Kirsher 	for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) {
1065a88394cfSJeff Kirsher 		int entry = np->dirty_tx % TX_RING_SIZE;
1066a88394cfSJeff Kirsher 		int tx_status = np->tx_ring[entry].status;
1067a88394cfSJeff Kirsher 
1068a88394cfSJeff Kirsher 		if (tx_status < 0)
1069a88394cfSJeff Kirsher 			break;
1070a88394cfSJeff Kirsher 		if (tx_status & 0x8000) { 	/* There was an error, log it. */
1071a88394cfSJeff Kirsher #ifndef final_version
1072a88394cfSJeff Kirsher 			if (debug > 1)
1073a88394cfSJeff Kirsher 				netdev_dbg(dev, "Transmit error, Tx status %08x\n",
1074a88394cfSJeff Kirsher 					   tx_status);
1075a88394cfSJeff Kirsher #endif
1076a88394cfSJeff Kirsher 			np->stats.tx_errors++;
1077a88394cfSJeff Kirsher 			if (tx_status & 0x0104) np->stats.tx_aborted_errors++;
1078a88394cfSJeff Kirsher 			if (tx_status & 0x0C80) np->stats.tx_carrier_errors++;
1079a88394cfSJeff Kirsher 			if (tx_status & 0x0200) np->stats.tx_window_errors++;
1080a88394cfSJeff Kirsher 			if (tx_status & 0x0002) np->stats.tx_fifo_errors++;
1081a88394cfSJeff Kirsher 			if ((tx_status & 0x0080) && np->mii_if.full_duplex == 0)
1082a88394cfSJeff Kirsher 				np->stats.tx_heartbeat_errors++;
1083a88394cfSJeff Kirsher 		} else {
1084a88394cfSJeff Kirsher #ifndef final_version
1085a88394cfSJeff Kirsher 			if (debug > 3)
1086a88394cfSJeff Kirsher 				netdev_dbg(dev, "Transmit slot %d ok, Tx status %08x\n",
1087a88394cfSJeff Kirsher 					   entry, tx_status);
1088a88394cfSJeff Kirsher #endif
1089a88394cfSJeff Kirsher 			np->stats.tx_bytes += np->tx_skbuff[entry]->len;
1090a88394cfSJeff Kirsher 			np->stats.collisions += (tx_status >> 3) & 15;
1091a88394cfSJeff Kirsher 			np->stats.tx_packets++;
1092a88394cfSJeff Kirsher 		}
1093a88394cfSJeff Kirsher 		/* Free the original skb. */
1094a88394cfSJeff Kirsher 		pci_unmap_single(np->pci_dev,np->tx_addr[entry],
1095a88394cfSJeff Kirsher 					np->tx_skbuff[entry]->len,
1096a88394cfSJeff Kirsher 					PCI_DMA_TODEVICE);
1097a88394cfSJeff Kirsher 		np->tx_q_bytes -= np->tx_skbuff[entry]->len;
1098a88394cfSJeff Kirsher 		dev_kfree_skb_irq(np->tx_skbuff[entry]);
1099a88394cfSJeff Kirsher 		np->tx_skbuff[entry] = NULL;
1100a88394cfSJeff Kirsher 	}
1101a88394cfSJeff Kirsher 	if (np->tx_full &&
1102a88394cfSJeff Kirsher 		np->cur_tx - np->dirty_tx < TX_QUEUE_LEN_RESTART &&
1103a88394cfSJeff Kirsher 		np->tx_q_bytes < TX_BUG_FIFO_LIMIT) {
1104a88394cfSJeff Kirsher 		/* The ring is no longer full, clear tbusy. */
1105a88394cfSJeff Kirsher 		np->tx_full = 0;
1106a88394cfSJeff Kirsher 		wmb();
1107a88394cfSJeff Kirsher 		netif_wake_queue(dev);
1108a88394cfSJeff Kirsher 	}
1109a88394cfSJeff Kirsher }
1110a88394cfSJeff Kirsher 
1111a88394cfSJeff Kirsher /* The interrupt handler does all of the Rx thread work and cleans up
1112a88394cfSJeff Kirsher    after the Tx thread. */
1113a88394cfSJeff Kirsher static irqreturn_t intr_handler(int irq, void *dev_instance)
1114a88394cfSJeff Kirsher {
1115a88394cfSJeff Kirsher 	struct net_device *dev = (struct net_device *)dev_instance;
1116a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1117a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1118a88394cfSJeff Kirsher 	int work_limit = max_interrupt_work;
1119a88394cfSJeff Kirsher 	int handled = 0;
1120a88394cfSJeff Kirsher 
1121a88394cfSJeff Kirsher 	if (!netif_device_present(dev))
1122a88394cfSJeff Kirsher 		return IRQ_NONE;
1123a88394cfSJeff Kirsher 	do {
1124a88394cfSJeff Kirsher 		u32 intr_status = ioread32(ioaddr + IntrStatus);
1125a88394cfSJeff Kirsher 
1126a88394cfSJeff Kirsher 		/* Acknowledge all of the current interrupt sources ASAP. */
1127a88394cfSJeff Kirsher 		iowrite32(intr_status & 0x001ffff, ioaddr + IntrStatus);
1128a88394cfSJeff Kirsher 
1129a88394cfSJeff Kirsher 		if (debug > 4)
1130a88394cfSJeff Kirsher 			netdev_dbg(dev, "Interrupt, status %04x\n", intr_status);
1131a88394cfSJeff Kirsher 
1132a88394cfSJeff Kirsher 		if ((intr_status & (NormalIntr|AbnormalIntr)) == 0)
1133a88394cfSJeff Kirsher 			break;
1134a88394cfSJeff Kirsher 
1135a88394cfSJeff Kirsher 		handled = 1;
1136a88394cfSJeff Kirsher 
1137a88394cfSJeff Kirsher 		if (intr_status & (RxIntr | RxNoBuf))
1138a88394cfSJeff Kirsher 			netdev_rx(dev);
1139a88394cfSJeff Kirsher 		if (intr_status & RxNoBuf)
1140a88394cfSJeff Kirsher 			iowrite32(0, ioaddr + RxStartDemand);
1141a88394cfSJeff Kirsher 
1142a88394cfSJeff Kirsher 		if (intr_status & (TxNoBuf | TxIntr) &&
1143a88394cfSJeff Kirsher 			np->cur_tx != np->dirty_tx) {
1144a88394cfSJeff Kirsher 			spin_lock(&np->lock);
1145a88394cfSJeff Kirsher 			netdev_tx_done(dev);
1146a88394cfSJeff Kirsher 			spin_unlock(&np->lock);
1147a88394cfSJeff Kirsher 		}
1148a88394cfSJeff Kirsher 
1149a88394cfSJeff Kirsher 		/* Abnormal error summary/uncommon events handlers. */
1150a88394cfSJeff Kirsher 		if (intr_status & (AbnormalIntr | TxFIFOUnderflow | SystemError |
1151a88394cfSJeff Kirsher 						   TimerInt | TxDied))
1152a88394cfSJeff Kirsher 			netdev_error(dev, intr_status);
1153a88394cfSJeff Kirsher 
1154a88394cfSJeff Kirsher 		if (--work_limit < 0) {
1155a88394cfSJeff Kirsher 			dev_warn(&dev->dev,
1156a88394cfSJeff Kirsher 				 "Too much work at interrupt, status=0x%04x\n",
1157a88394cfSJeff Kirsher 				 intr_status);
1158a88394cfSJeff Kirsher 			/* Set the timer to re-enable the other interrupts after
1159a88394cfSJeff Kirsher 			   10*82usec ticks. */
1160a88394cfSJeff Kirsher 			spin_lock(&np->lock);
1161a88394cfSJeff Kirsher 			if (netif_device_present(dev)) {
1162a88394cfSJeff Kirsher 				iowrite32(AbnormalIntr | TimerInt, ioaddr + IntrEnable);
1163a88394cfSJeff Kirsher 				iowrite32(10, ioaddr + GPTimer);
1164a88394cfSJeff Kirsher 			}
1165a88394cfSJeff Kirsher 			spin_unlock(&np->lock);
1166a88394cfSJeff Kirsher 			break;
1167a88394cfSJeff Kirsher 		}
1168a88394cfSJeff Kirsher 	} while (1);
1169a88394cfSJeff Kirsher 
1170a88394cfSJeff Kirsher 	if (debug > 3)
1171a88394cfSJeff Kirsher 		netdev_dbg(dev, "exiting interrupt, status=%#4.4x\n",
1172a88394cfSJeff Kirsher 			   ioread32(ioaddr + IntrStatus));
1173a88394cfSJeff Kirsher 	return IRQ_RETVAL(handled);
1174a88394cfSJeff Kirsher }
1175a88394cfSJeff Kirsher 
1176a88394cfSJeff Kirsher /* This routine is logically part of the interrupt handler, but separated
1177a88394cfSJeff Kirsher    for clarity and better register allocation. */
1178a88394cfSJeff Kirsher static int netdev_rx(struct net_device *dev)
1179a88394cfSJeff Kirsher {
1180a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1181a88394cfSJeff Kirsher 	int entry = np->cur_rx % RX_RING_SIZE;
1182a88394cfSJeff Kirsher 	int work_limit = np->dirty_rx + RX_RING_SIZE - np->cur_rx;
1183a88394cfSJeff Kirsher 
1184a88394cfSJeff Kirsher 	if (debug > 4) {
1185a88394cfSJeff Kirsher 		netdev_dbg(dev, " In netdev_rx(), entry %d status %04x\n",
1186a88394cfSJeff Kirsher 			   entry, np->rx_ring[entry].status);
1187a88394cfSJeff Kirsher 	}
1188a88394cfSJeff Kirsher 
1189a88394cfSJeff Kirsher 	/* If EOP is set on the next entry, it's a new packet. Send it up. */
1190a88394cfSJeff Kirsher 	while (--work_limit >= 0) {
1191a88394cfSJeff Kirsher 		struct w840_rx_desc *desc = np->rx_head_desc;
1192a88394cfSJeff Kirsher 		s32 status = desc->status;
1193a88394cfSJeff Kirsher 
1194a88394cfSJeff Kirsher 		if (debug > 4)
1195a88394cfSJeff Kirsher 			netdev_dbg(dev, "  netdev_rx() status was %08x\n",
1196a88394cfSJeff Kirsher 				   status);
1197a88394cfSJeff Kirsher 		if (status < 0)
1198a88394cfSJeff Kirsher 			break;
1199a88394cfSJeff Kirsher 		if ((status & 0x38008300) != 0x0300) {
1200a88394cfSJeff Kirsher 			if ((status & 0x38000300) != 0x0300) {
1201a88394cfSJeff Kirsher 				/* Ingore earlier buffers. */
1202a88394cfSJeff Kirsher 				if ((status & 0xffff) != 0x7fff) {
1203a88394cfSJeff Kirsher 					dev_warn(&dev->dev,
1204a88394cfSJeff Kirsher 						 "Oversized Ethernet frame spanned multiple buffers, entry %#x status %04x!\n",
1205a88394cfSJeff Kirsher 						 np->cur_rx, status);
1206a88394cfSJeff Kirsher 					np->stats.rx_length_errors++;
1207a88394cfSJeff Kirsher 				}
1208a88394cfSJeff Kirsher 			} else if (status & 0x8000) {
1209a88394cfSJeff Kirsher 				/* There was a fatal error. */
1210a88394cfSJeff Kirsher 				if (debug > 2)
1211a88394cfSJeff Kirsher 					netdev_dbg(dev, "Receive error, Rx status %08x\n",
1212a88394cfSJeff Kirsher 						   status);
1213a88394cfSJeff Kirsher 				np->stats.rx_errors++; /* end of a packet.*/
1214a88394cfSJeff Kirsher 				if (status & 0x0890) np->stats.rx_length_errors++;
1215a88394cfSJeff Kirsher 				if (status & 0x004C) np->stats.rx_frame_errors++;
1216a88394cfSJeff Kirsher 				if (status & 0x0002) np->stats.rx_crc_errors++;
1217a88394cfSJeff Kirsher 			}
1218a88394cfSJeff Kirsher 		} else {
1219a88394cfSJeff Kirsher 			struct sk_buff *skb;
1220a88394cfSJeff Kirsher 			/* Omit the four octet CRC from the length. */
1221a88394cfSJeff Kirsher 			int pkt_len = ((status >> 16) & 0x7ff) - 4;
1222a88394cfSJeff Kirsher 
1223a88394cfSJeff Kirsher #ifndef final_version
1224a88394cfSJeff Kirsher 			if (debug > 4)
1225a88394cfSJeff Kirsher 				netdev_dbg(dev, "  netdev_rx() normal Rx pkt length %d status %x\n",
1226a88394cfSJeff Kirsher 					   pkt_len, status);
1227a88394cfSJeff Kirsher #endif
1228a88394cfSJeff Kirsher 			/* Check if the packet is long enough to accept without copying
1229a88394cfSJeff Kirsher 			   to a minimally-sized skbuff. */
1230a88394cfSJeff Kirsher 			if (pkt_len < rx_copybreak &&
123121a4e469SPradeep A Dalvi 			    (skb = netdev_alloc_skb(dev, pkt_len + 2)) != NULL) {
1232a88394cfSJeff Kirsher 				skb_reserve(skb, 2);	/* 16 byte align the IP header */
1233a88394cfSJeff Kirsher 				pci_dma_sync_single_for_cpu(np->pci_dev,np->rx_addr[entry],
1234a88394cfSJeff Kirsher 							    np->rx_skbuff[entry]->len,
1235a88394cfSJeff Kirsher 							    PCI_DMA_FROMDEVICE);
1236a88394cfSJeff Kirsher 				skb_copy_to_linear_data(skb, np->rx_skbuff[entry]->data, pkt_len);
1237a88394cfSJeff Kirsher 				skb_put(skb, pkt_len);
1238a88394cfSJeff Kirsher 				pci_dma_sync_single_for_device(np->pci_dev,np->rx_addr[entry],
1239a88394cfSJeff Kirsher 							       np->rx_skbuff[entry]->len,
1240a88394cfSJeff Kirsher 							       PCI_DMA_FROMDEVICE);
1241a88394cfSJeff Kirsher 			} else {
1242a88394cfSJeff Kirsher 				pci_unmap_single(np->pci_dev,np->rx_addr[entry],
1243a88394cfSJeff Kirsher 							np->rx_skbuff[entry]->len,
1244a88394cfSJeff Kirsher 							PCI_DMA_FROMDEVICE);
1245a88394cfSJeff Kirsher 				skb_put(skb = np->rx_skbuff[entry], pkt_len);
1246a88394cfSJeff Kirsher 				np->rx_skbuff[entry] = NULL;
1247a88394cfSJeff Kirsher 			}
1248a88394cfSJeff Kirsher #ifndef final_version				/* Remove after testing. */
1249a88394cfSJeff Kirsher 			/* You will want this info for the initial debug. */
1250a88394cfSJeff Kirsher 			if (debug > 5)
1251a88394cfSJeff Kirsher 				netdev_dbg(dev, "  Rx data %pM %pM %02x%02x %pI4\n",
1252a88394cfSJeff Kirsher 					   &skb->data[0], &skb->data[6],
1253a88394cfSJeff Kirsher 					   skb->data[12], skb->data[13],
1254a88394cfSJeff Kirsher 					   &skb->data[14]);
1255a88394cfSJeff Kirsher #endif
1256a88394cfSJeff Kirsher 			skb->protocol = eth_type_trans(skb, dev);
1257a88394cfSJeff Kirsher 			netif_rx(skb);
1258a88394cfSJeff Kirsher 			np->stats.rx_packets++;
1259a88394cfSJeff Kirsher 			np->stats.rx_bytes += pkt_len;
1260a88394cfSJeff Kirsher 		}
1261a88394cfSJeff Kirsher 		entry = (++np->cur_rx) % RX_RING_SIZE;
1262a88394cfSJeff Kirsher 		np->rx_head_desc = &np->rx_ring[entry];
1263a88394cfSJeff Kirsher 	}
1264a88394cfSJeff Kirsher 
1265a88394cfSJeff Kirsher 	/* Refill the Rx ring buffers. */
1266a88394cfSJeff Kirsher 	for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) {
1267a88394cfSJeff Kirsher 		struct sk_buff *skb;
1268a88394cfSJeff Kirsher 		entry = np->dirty_rx % RX_RING_SIZE;
1269a88394cfSJeff Kirsher 		if (np->rx_skbuff[entry] == NULL) {
127021a4e469SPradeep A Dalvi 			skb = netdev_alloc_skb(dev, np->rx_buf_sz);
1271a88394cfSJeff Kirsher 			np->rx_skbuff[entry] = skb;
1272a88394cfSJeff Kirsher 			if (skb == NULL)
1273a88394cfSJeff Kirsher 				break;			/* Better luck next round. */
1274a88394cfSJeff Kirsher 			np->rx_addr[entry] = pci_map_single(np->pci_dev,
1275a88394cfSJeff Kirsher 							skb->data,
1276a88394cfSJeff Kirsher 							np->rx_buf_sz, PCI_DMA_FROMDEVICE);
1277a88394cfSJeff Kirsher 			np->rx_ring[entry].buffer1 = np->rx_addr[entry];
1278a88394cfSJeff Kirsher 		}
1279a88394cfSJeff Kirsher 		wmb();
1280a88394cfSJeff Kirsher 		np->rx_ring[entry].status = DescOwned;
1281a88394cfSJeff Kirsher 	}
1282a88394cfSJeff Kirsher 
1283a88394cfSJeff Kirsher 	return 0;
1284a88394cfSJeff Kirsher }
1285a88394cfSJeff Kirsher 
1286a88394cfSJeff Kirsher static void netdev_error(struct net_device *dev, int intr_status)
1287a88394cfSJeff Kirsher {
1288a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1289a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1290a88394cfSJeff Kirsher 
1291a88394cfSJeff Kirsher 	if (debug > 2)
1292a88394cfSJeff Kirsher 		netdev_dbg(dev, "Abnormal event, %08x\n", intr_status);
1293a88394cfSJeff Kirsher 	if (intr_status == 0xffffffff)
1294a88394cfSJeff Kirsher 		return;
1295a88394cfSJeff Kirsher 	spin_lock(&np->lock);
1296a88394cfSJeff Kirsher 	if (intr_status & TxFIFOUnderflow) {
1297a88394cfSJeff Kirsher 		int new;
1298a88394cfSJeff Kirsher 		/* Bump up the Tx threshold */
1299a88394cfSJeff Kirsher #if 0
1300a88394cfSJeff Kirsher 		/* This causes lots of dropped packets,
1301a88394cfSJeff Kirsher 		 * and under high load even tx_timeouts
1302a88394cfSJeff Kirsher 		 */
1303a88394cfSJeff Kirsher 		new = np->csr6 + 0x4000;
1304a88394cfSJeff Kirsher #else
1305a88394cfSJeff Kirsher 		new = (np->csr6 >> 14)&0x7f;
1306a88394cfSJeff Kirsher 		if (new < 64)
1307a88394cfSJeff Kirsher 			new *= 2;
1308a88394cfSJeff Kirsher 		 else
1309a88394cfSJeff Kirsher 		 	new = 127; /* load full packet before starting */
1310a88394cfSJeff Kirsher 		new = (np->csr6 & ~(0x7F << 14)) | (new<<14);
1311a88394cfSJeff Kirsher #endif
1312a88394cfSJeff Kirsher 		netdev_dbg(dev, "Tx underflow, new csr6 %08x\n", new);
1313a88394cfSJeff Kirsher 		update_csr6(dev, new);
1314a88394cfSJeff Kirsher 	}
1315a88394cfSJeff Kirsher 	if (intr_status & RxDied) {		/* Missed a Rx frame. */
1316a88394cfSJeff Kirsher 		np->stats.rx_errors++;
1317a88394cfSJeff Kirsher 	}
1318a88394cfSJeff Kirsher 	if (intr_status & TimerInt) {
1319a88394cfSJeff Kirsher 		/* Re-enable other interrupts. */
1320a88394cfSJeff Kirsher 		if (netif_device_present(dev))
1321a88394cfSJeff Kirsher 			iowrite32(0x1A0F5, ioaddr + IntrEnable);
1322a88394cfSJeff Kirsher 	}
1323a88394cfSJeff Kirsher 	np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1324a88394cfSJeff Kirsher 	iowrite32(0, ioaddr + RxStartDemand);
1325a88394cfSJeff Kirsher 	spin_unlock(&np->lock);
1326a88394cfSJeff Kirsher }
1327a88394cfSJeff Kirsher 
1328a88394cfSJeff Kirsher static struct net_device_stats *get_stats(struct net_device *dev)
1329a88394cfSJeff Kirsher {
1330a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1331a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1332a88394cfSJeff Kirsher 
1333a88394cfSJeff Kirsher 	/* The chip only need report frame silently dropped. */
1334a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1335a88394cfSJeff Kirsher 	if (netif_running(dev) && netif_device_present(dev))
1336a88394cfSJeff Kirsher 		np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1337a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1338a88394cfSJeff Kirsher 
1339a88394cfSJeff Kirsher 	return &np->stats;
1340a88394cfSJeff Kirsher }
1341a88394cfSJeff Kirsher 
1342a88394cfSJeff Kirsher 
1343a88394cfSJeff Kirsher static u32 __set_rx_mode(struct net_device *dev)
1344a88394cfSJeff Kirsher {
1345a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1346a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1347a88394cfSJeff Kirsher 	u32 mc_filter[2];			/* Multicast hash filter */
1348a88394cfSJeff Kirsher 	u32 rx_mode;
1349a88394cfSJeff Kirsher 
1350a88394cfSJeff Kirsher 	if (dev->flags & IFF_PROMISC) {			/* Set promiscuous. */
1351a88394cfSJeff Kirsher 		memset(mc_filter, 0xff, sizeof(mc_filter));
1352a88394cfSJeff Kirsher 		rx_mode = RxAcceptBroadcast | AcceptMulticast | RxAcceptAllPhys
1353a88394cfSJeff Kirsher 			| AcceptMyPhys;
1354a88394cfSJeff Kirsher 	} else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
1355a88394cfSJeff Kirsher 		   (dev->flags & IFF_ALLMULTI)) {
1356a88394cfSJeff Kirsher 		/* Too many to match, or accept all multicasts. */
1357a88394cfSJeff Kirsher 		memset(mc_filter, 0xff, sizeof(mc_filter));
1358a88394cfSJeff Kirsher 		rx_mode = RxAcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1359a88394cfSJeff Kirsher 	} else {
1360a88394cfSJeff Kirsher 		struct netdev_hw_addr *ha;
1361a88394cfSJeff Kirsher 
1362a88394cfSJeff Kirsher 		memset(mc_filter, 0, sizeof(mc_filter));
1363a88394cfSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
1364a88394cfSJeff Kirsher 			int filbit;
1365a88394cfSJeff Kirsher 
1366a88394cfSJeff Kirsher 			filbit = (ether_crc(ETH_ALEN, ha->addr) >> 26) ^ 0x3F;
1367a88394cfSJeff Kirsher 			filbit &= 0x3f;
1368a88394cfSJeff Kirsher 			mc_filter[filbit >> 5] |= 1 << (filbit & 31);
1369a88394cfSJeff Kirsher 		}
1370a88394cfSJeff Kirsher 		rx_mode = RxAcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1371a88394cfSJeff Kirsher 	}
1372a88394cfSJeff Kirsher 	iowrite32(mc_filter[0], ioaddr + MulticastFilter0);
1373a88394cfSJeff Kirsher 	iowrite32(mc_filter[1], ioaddr + MulticastFilter1);
1374a88394cfSJeff Kirsher 	return rx_mode;
1375a88394cfSJeff Kirsher }
1376a88394cfSJeff Kirsher 
1377a88394cfSJeff Kirsher static void set_rx_mode(struct net_device *dev)
1378a88394cfSJeff Kirsher {
1379a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1380a88394cfSJeff Kirsher 	u32 rx_mode = __set_rx_mode(dev);
1381a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1382a88394cfSJeff Kirsher 	update_csr6(dev, (np->csr6 & ~0x00F8) | rx_mode);
1383a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1384a88394cfSJeff Kirsher }
1385a88394cfSJeff Kirsher 
1386a88394cfSJeff Kirsher static void netdev_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1387a88394cfSJeff Kirsher {
1388a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1389a88394cfSJeff Kirsher 
139068aad78cSRick Jones 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
139168aad78cSRick Jones 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
139268aad78cSRick Jones 	strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
1393a88394cfSJeff Kirsher }
1394a88394cfSJeff Kirsher 
1395a88394cfSJeff Kirsher static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1396a88394cfSJeff Kirsher {
1397a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1398a88394cfSJeff Kirsher 	int rc;
1399a88394cfSJeff Kirsher 
1400a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1401a88394cfSJeff Kirsher 	rc = mii_ethtool_gset(&np->mii_if, cmd);
1402a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1403a88394cfSJeff Kirsher 
1404a88394cfSJeff Kirsher 	return rc;
1405a88394cfSJeff Kirsher }
1406a88394cfSJeff Kirsher 
1407a88394cfSJeff Kirsher static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1408a88394cfSJeff Kirsher {
1409a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1410a88394cfSJeff Kirsher 	int rc;
1411a88394cfSJeff Kirsher 
1412a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1413a88394cfSJeff Kirsher 	rc = mii_ethtool_sset(&np->mii_if, cmd);
1414a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1415a88394cfSJeff Kirsher 
1416a88394cfSJeff Kirsher 	return rc;
1417a88394cfSJeff Kirsher }
1418a88394cfSJeff Kirsher 
1419a88394cfSJeff Kirsher static int netdev_nway_reset(struct net_device *dev)
1420a88394cfSJeff Kirsher {
1421a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1422a88394cfSJeff Kirsher 	return mii_nway_restart(&np->mii_if);
1423a88394cfSJeff Kirsher }
1424a88394cfSJeff Kirsher 
1425a88394cfSJeff Kirsher static u32 netdev_get_link(struct net_device *dev)
1426a88394cfSJeff Kirsher {
1427a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1428a88394cfSJeff Kirsher 	return mii_link_ok(&np->mii_if);
1429a88394cfSJeff Kirsher }
1430a88394cfSJeff Kirsher 
1431a88394cfSJeff Kirsher static u32 netdev_get_msglevel(struct net_device *dev)
1432a88394cfSJeff Kirsher {
1433a88394cfSJeff Kirsher 	return debug;
1434a88394cfSJeff Kirsher }
1435a88394cfSJeff Kirsher 
1436a88394cfSJeff Kirsher static void netdev_set_msglevel(struct net_device *dev, u32 value)
1437a88394cfSJeff Kirsher {
1438a88394cfSJeff Kirsher 	debug = value;
1439a88394cfSJeff Kirsher }
1440a88394cfSJeff Kirsher 
1441a88394cfSJeff Kirsher static const struct ethtool_ops netdev_ethtool_ops = {
1442a88394cfSJeff Kirsher 	.get_drvinfo		= netdev_get_drvinfo,
1443a88394cfSJeff Kirsher 	.get_settings		= netdev_get_settings,
1444a88394cfSJeff Kirsher 	.set_settings		= netdev_set_settings,
1445a88394cfSJeff Kirsher 	.nway_reset		= netdev_nway_reset,
1446a88394cfSJeff Kirsher 	.get_link		= netdev_get_link,
1447a88394cfSJeff Kirsher 	.get_msglevel		= netdev_get_msglevel,
1448a88394cfSJeff Kirsher 	.set_msglevel		= netdev_set_msglevel,
1449a88394cfSJeff Kirsher };
1450a88394cfSJeff Kirsher 
1451a88394cfSJeff Kirsher static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1452a88394cfSJeff Kirsher {
1453a88394cfSJeff Kirsher 	struct mii_ioctl_data *data = if_mii(rq);
1454a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1455a88394cfSJeff Kirsher 
1456a88394cfSJeff Kirsher 	switch(cmd) {
1457a88394cfSJeff Kirsher 	case SIOCGMIIPHY:		/* Get address of MII PHY in use. */
1458a88394cfSJeff Kirsher 		data->phy_id = ((struct netdev_private *)netdev_priv(dev))->phys[0] & 0x1f;
1459a88394cfSJeff Kirsher 		/* Fall Through */
1460a88394cfSJeff Kirsher 
1461a88394cfSJeff Kirsher 	case SIOCGMIIREG:		/* Read MII PHY register. */
1462a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1463a88394cfSJeff Kirsher 		data->val_out = mdio_read(dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
1464a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1465a88394cfSJeff Kirsher 		return 0;
1466a88394cfSJeff Kirsher 
1467a88394cfSJeff Kirsher 	case SIOCSMIIREG:		/* Write MII PHY register. */
1468a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1469a88394cfSJeff Kirsher 		mdio_write(dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
1470a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1471a88394cfSJeff Kirsher 		return 0;
1472a88394cfSJeff Kirsher 	default:
1473a88394cfSJeff Kirsher 		return -EOPNOTSUPP;
1474a88394cfSJeff Kirsher 	}
1475a88394cfSJeff Kirsher }
1476a88394cfSJeff Kirsher 
1477a88394cfSJeff Kirsher static int netdev_close(struct net_device *dev)
1478a88394cfSJeff Kirsher {
1479a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1480a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1481a88394cfSJeff Kirsher 
1482a88394cfSJeff Kirsher 	netif_stop_queue(dev);
1483a88394cfSJeff Kirsher 
1484a88394cfSJeff Kirsher 	if (debug > 1) {
1485a88394cfSJeff Kirsher 		netdev_dbg(dev, "Shutting down ethercard, status was %08x Config %08x\n",
1486a88394cfSJeff Kirsher 			   ioread32(ioaddr + IntrStatus),
1487a88394cfSJeff Kirsher 			   ioread32(ioaddr + NetworkConfig));
1488a88394cfSJeff Kirsher 		netdev_dbg(dev, "Queue pointers were Tx %d / %d,  Rx %d / %d\n",
1489a88394cfSJeff Kirsher 			   np->cur_tx, np->dirty_tx,
1490a88394cfSJeff Kirsher 			   np->cur_rx, np->dirty_rx);
1491a88394cfSJeff Kirsher 	}
1492a88394cfSJeff Kirsher 
1493a88394cfSJeff Kirsher  	/* Stop the chip's Tx and Rx processes. */
1494a88394cfSJeff Kirsher 	spin_lock_irq(&np->lock);
1495a88394cfSJeff Kirsher 	netif_device_detach(dev);
1496a88394cfSJeff Kirsher 	update_csr6(dev, 0);
1497a88394cfSJeff Kirsher 	iowrite32(0x0000, ioaddr + IntrEnable);
1498a88394cfSJeff Kirsher 	spin_unlock_irq(&np->lock);
1499a88394cfSJeff Kirsher 
1500c0bd55efSFrancois Romieu 	free_irq(np->pci_dev->irq, dev);
1501a88394cfSJeff Kirsher 	wmb();
1502a88394cfSJeff Kirsher 	netif_device_attach(dev);
1503a88394cfSJeff Kirsher 
1504a88394cfSJeff Kirsher 	if (ioread32(ioaddr + NetworkConfig) != 0xffffffff)
1505a88394cfSJeff Kirsher 		np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1506a88394cfSJeff Kirsher 
1507a88394cfSJeff Kirsher #ifdef __i386__
1508a88394cfSJeff Kirsher 	if (debug > 2) {
1509a88394cfSJeff Kirsher 		int i;
1510a88394cfSJeff Kirsher 
1511a88394cfSJeff Kirsher 		printk(KERN_DEBUG"  Tx ring at %p:\n", np->tx_ring);
1512a88394cfSJeff Kirsher 		for (i = 0; i < TX_RING_SIZE; i++)
1513a88394cfSJeff Kirsher 			printk(KERN_DEBUG " #%d desc. %04x %04x %08x\n",
1514a88394cfSJeff Kirsher 			       i, np->tx_ring[i].length,
1515a88394cfSJeff Kirsher 			       np->tx_ring[i].status, np->tx_ring[i].buffer1);
1516a88394cfSJeff Kirsher 		printk(KERN_DEBUG "  Rx ring %p:\n", np->rx_ring);
1517a88394cfSJeff Kirsher 		for (i = 0; i < RX_RING_SIZE; i++) {
1518a88394cfSJeff Kirsher 			printk(KERN_DEBUG " #%d desc. %04x %04x %08x\n",
1519a88394cfSJeff Kirsher 			       i, np->rx_ring[i].length,
1520a88394cfSJeff Kirsher 			       np->rx_ring[i].status, np->rx_ring[i].buffer1);
1521a88394cfSJeff Kirsher 		}
1522a88394cfSJeff Kirsher 	}
1523a88394cfSJeff Kirsher #endif /* __i386__ debugging only */
1524a88394cfSJeff Kirsher 
1525a88394cfSJeff Kirsher 	del_timer_sync(&np->timer);
1526a88394cfSJeff Kirsher 
1527a88394cfSJeff Kirsher 	free_rxtx_rings(np);
1528a88394cfSJeff Kirsher 	free_ringdesc(np);
1529a88394cfSJeff Kirsher 
1530a88394cfSJeff Kirsher 	return 0;
1531a88394cfSJeff Kirsher }
1532a88394cfSJeff Kirsher 
1533779c1a85SBill Pemberton static void w840_remove1(struct pci_dev *pdev)
1534a88394cfSJeff Kirsher {
1535a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata(pdev);
1536a88394cfSJeff Kirsher 
1537a88394cfSJeff Kirsher 	if (dev) {
1538a88394cfSJeff Kirsher 		struct netdev_private *np = netdev_priv(dev);
1539a88394cfSJeff Kirsher 		unregister_netdev(dev);
1540a88394cfSJeff Kirsher 		pci_release_regions(pdev);
1541a88394cfSJeff Kirsher 		pci_iounmap(pdev, np->base_addr);
1542a88394cfSJeff Kirsher 		free_netdev(dev);
1543a88394cfSJeff Kirsher 	}
1544a88394cfSJeff Kirsher }
1545a88394cfSJeff Kirsher 
1546a88394cfSJeff Kirsher #ifdef CONFIG_PM
1547a88394cfSJeff Kirsher 
1548a88394cfSJeff Kirsher /*
1549a88394cfSJeff Kirsher  * suspend/resume synchronization:
1550a88394cfSJeff Kirsher  * - open, close, do_ioctl:
1551a88394cfSJeff Kirsher  * 	rtnl_lock, & netif_device_detach after the rtnl_unlock.
1552a88394cfSJeff Kirsher  * - get_stats:
1553a88394cfSJeff Kirsher  * 	spin_lock_irq(np->lock), doesn't touch hw if not present
1554a88394cfSJeff Kirsher  * - start_xmit:
1555a88394cfSJeff Kirsher  * 	synchronize_irq + netif_tx_disable;
1556a88394cfSJeff Kirsher  * - tx_timeout:
1557a88394cfSJeff Kirsher  * 	netif_device_detach + netif_tx_disable;
1558a88394cfSJeff Kirsher  * - set_multicast_list
1559a88394cfSJeff Kirsher  * 	netif_device_detach + netif_tx_disable;
1560a88394cfSJeff Kirsher  * - interrupt handler
1561a88394cfSJeff Kirsher  * 	doesn't touch hw if not present, synchronize_irq waits for
1562a88394cfSJeff Kirsher  * 	running instances of the interrupt handler.
1563a88394cfSJeff Kirsher  *
1564a88394cfSJeff Kirsher  * Disabling hw requires clearing csr6 & IntrEnable.
1565a88394cfSJeff Kirsher  * update_csr6 & all function that write IntrEnable check netif_device_present
1566a88394cfSJeff Kirsher  * before settings any bits.
1567a88394cfSJeff Kirsher  *
1568a88394cfSJeff Kirsher  * Detach must occur under spin_unlock_irq(), interrupts from a detached
1569a88394cfSJeff Kirsher  * device would cause an irq storm.
1570a88394cfSJeff Kirsher  */
1571a88394cfSJeff Kirsher static int w840_suspend (struct pci_dev *pdev, pm_message_t state)
1572a88394cfSJeff Kirsher {
1573a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata (pdev);
1574a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1575a88394cfSJeff Kirsher 	void __iomem *ioaddr = np->base_addr;
1576a88394cfSJeff Kirsher 
1577a88394cfSJeff Kirsher 	rtnl_lock();
1578a88394cfSJeff Kirsher 	if (netif_running (dev)) {
1579a88394cfSJeff Kirsher 		del_timer_sync(&np->timer);
1580a88394cfSJeff Kirsher 
1581a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1582a88394cfSJeff Kirsher 		netif_device_detach(dev);
1583a88394cfSJeff Kirsher 		update_csr6(dev, 0);
1584a88394cfSJeff Kirsher 		iowrite32(0, ioaddr + IntrEnable);
1585a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1586a88394cfSJeff Kirsher 
1587c0bd55efSFrancois Romieu 		synchronize_irq(np->pci_dev->irq);
1588a88394cfSJeff Kirsher 		netif_tx_disable(dev);
1589a88394cfSJeff Kirsher 
1590a88394cfSJeff Kirsher 		np->stats.rx_missed_errors += ioread32(ioaddr + RxMissed) & 0xffff;
1591a88394cfSJeff Kirsher 
1592a88394cfSJeff Kirsher 		/* no more hardware accesses behind this line. */
1593a88394cfSJeff Kirsher 
1594a88394cfSJeff Kirsher 		BUG_ON(np->csr6 || ioread32(ioaddr + IntrEnable));
1595a88394cfSJeff Kirsher 
1596a88394cfSJeff Kirsher 		/* pci_power_off(pdev, -1); */
1597a88394cfSJeff Kirsher 
1598a88394cfSJeff Kirsher 		free_rxtx_rings(np);
1599a88394cfSJeff Kirsher 	} else {
1600a88394cfSJeff Kirsher 		netif_device_detach(dev);
1601a88394cfSJeff Kirsher 	}
1602a88394cfSJeff Kirsher 	rtnl_unlock();
1603a88394cfSJeff Kirsher 	return 0;
1604a88394cfSJeff Kirsher }
1605a88394cfSJeff Kirsher 
1606a88394cfSJeff Kirsher static int w840_resume (struct pci_dev *pdev)
1607a88394cfSJeff Kirsher {
1608a88394cfSJeff Kirsher 	struct net_device *dev = pci_get_drvdata (pdev);
1609a88394cfSJeff Kirsher 	struct netdev_private *np = netdev_priv(dev);
1610a88394cfSJeff Kirsher 	int retval = 0;
1611a88394cfSJeff Kirsher 
1612a88394cfSJeff Kirsher 	rtnl_lock();
1613a88394cfSJeff Kirsher 	if (netif_device_present(dev))
1614a88394cfSJeff Kirsher 		goto out; /* device not suspended */
1615a88394cfSJeff Kirsher 	if (netif_running(dev)) {
1616a88394cfSJeff Kirsher 		if ((retval = pci_enable_device(pdev))) {
1617a88394cfSJeff Kirsher 			dev_err(&dev->dev,
1618a88394cfSJeff Kirsher 				"pci_enable_device failed in resume\n");
1619a88394cfSJeff Kirsher 			goto out;
1620a88394cfSJeff Kirsher 		}
1621a88394cfSJeff Kirsher 		spin_lock_irq(&np->lock);
1622a88394cfSJeff Kirsher 		iowrite32(1, np->base_addr+PCIBusCfg);
1623a88394cfSJeff Kirsher 		ioread32(np->base_addr+PCIBusCfg);
1624a88394cfSJeff Kirsher 		udelay(1);
1625a88394cfSJeff Kirsher 		netif_device_attach(dev);
1626a88394cfSJeff Kirsher 		init_rxtx_rings(dev);
1627a88394cfSJeff Kirsher 		init_registers(dev);
1628a88394cfSJeff Kirsher 		spin_unlock_irq(&np->lock);
1629a88394cfSJeff Kirsher 
1630a88394cfSJeff Kirsher 		netif_wake_queue(dev);
1631a88394cfSJeff Kirsher 
1632a88394cfSJeff Kirsher 		mod_timer(&np->timer, jiffies + 1*HZ);
1633a88394cfSJeff Kirsher 	} else {
1634a88394cfSJeff Kirsher 		netif_device_attach(dev);
1635a88394cfSJeff Kirsher 	}
1636a88394cfSJeff Kirsher out:
1637a88394cfSJeff Kirsher 	rtnl_unlock();
1638a88394cfSJeff Kirsher 	return retval;
1639a88394cfSJeff Kirsher }
1640a88394cfSJeff Kirsher #endif
1641a88394cfSJeff Kirsher 
1642a88394cfSJeff Kirsher static struct pci_driver w840_driver = {
1643a88394cfSJeff Kirsher 	.name		= DRV_NAME,
1644a88394cfSJeff Kirsher 	.id_table	= w840_pci_tbl,
1645a88394cfSJeff Kirsher 	.probe		= w840_probe1,
1646779c1a85SBill Pemberton 	.remove		= w840_remove1,
1647a88394cfSJeff Kirsher #ifdef CONFIG_PM
1648a88394cfSJeff Kirsher 	.suspend	= w840_suspend,
1649a88394cfSJeff Kirsher 	.resume		= w840_resume,
1650a88394cfSJeff Kirsher #endif
1651a88394cfSJeff Kirsher };
1652a88394cfSJeff Kirsher 
1653a88394cfSJeff Kirsher static int __init w840_init(void)
1654a88394cfSJeff Kirsher {
1655a88394cfSJeff Kirsher 	printk(version);
1656a88394cfSJeff Kirsher 	return pci_register_driver(&w840_driver);
1657a88394cfSJeff Kirsher }
1658a88394cfSJeff Kirsher 
1659a88394cfSJeff Kirsher static void __exit w840_exit(void)
1660a88394cfSJeff Kirsher {
1661a88394cfSJeff Kirsher 	pci_unregister_driver(&w840_driver);
1662a88394cfSJeff Kirsher }
1663a88394cfSJeff Kirsher 
1664a88394cfSJeff Kirsher module_init(w840_init);
1665a88394cfSJeff Kirsher module_exit(w840_exit);
1666