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