1dc3eb2f4SBagas Sanjaya // SPDX-License-Identifier: GPL-1.0+
253c0ec4fSArmin Wolf /* A Linux device driver for PCI NE2000 clones.
353c0ec4fSArmin Wolf  *
453c0ec4fSArmin Wolf  * Authors and other copyright holders:
553c0ec4fSArmin Wolf  * 1992-2000 by Donald Becker, NE2000 core and various modifications.
653c0ec4fSArmin Wolf  * 1995-1998 by Paul Gortmaker, core modifications and PCI support.
753c0ec4fSArmin Wolf  * Copyright 1993 assigned to the United States Government as represented
853c0ec4fSArmin Wolf  * by the Director, National Security Agency.
953c0ec4fSArmin Wolf  *
1053c0ec4fSArmin Wolf  * This software may be used and distributed according to the terms of
1153c0ec4fSArmin Wolf  * the GNU General Public License (GPL), incorporated herein by reference.
1253c0ec4fSArmin Wolf  * Drivers based on or derived from this code fall under the GPL and must
1353c0ec4fSArmin Wolf  * retain the authorship, copyright and license notice.  This file is not
1453c0ec4fSArmin Wolf  * a complete program and may only be used when the entire operating
1553c0ec4fSArmin Wolf  * system is licensed under the GPL.
1653c0ec4fSArmin Wolf  *
1753c0ec4fSArmin Wolf  * The author may be reached as becker@scyld.com, or C/O
1853c0ec4fSArmin Wolf  * Scyld Computing Corporation
1953c0ec4fSArmin Wolf  * 410 Severn Ave., Suite 210
2053c0ec4fSArmin Wolf  * Annapolis MD 21403
2153c0ec4fSArmin Wolf  *
2253c0ec4fSArmin Wolf  * Issues remaining:
2353c0ec4fSArmin Wolf  * People are making PCI NE2000 clones! Oh the horror, the horror...
2453c0ec4fSArmin Wolf  * Limited full-duplex support.
25644570b8SJeff Kirsher  */
26644570b8SJeff Kirsher 
27644570b8SJeff Kirsher #define DRV_NAME	"ne2k-pci"
2853c0ec4fSArmin Wolf #define DRV_DESCRIPTION	"PCI NE2000 clone driver"
2953c0ec4fSArmin Wolf #define DRV_AUTHOR	"Donald Becker / Paul Gortmaker"
30644570b8SJeff Kirsher #define DRV_VERSION	"1.03"
31644570b8SJeff Kirsher #define DRV_RELDATE	"9/22/2003"
32644570b8SJeff Kirsher 
3353c0ec4fSArmin Wolf #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34644570b8SJeff Kirsher 
35644570b8SJeff Kirsher /* The user-configurable values.
3653c0ec4fSArmin Wolf  * These may be modified when a driver module is loaded.
3753c0ec4fSArmin Wolf  */
38644570b8SJeff Kirsher 
3953c0ec4fSArmin Wolf /* More are supported, limit only on options */
4053c0ec4fSArmin Wolf #define MAX_UNITS 8
4153c0ec4fSArmin Wolf 
42644570b8SJeff Kirsher /* Used to pass the full-duplex flag, etc. */
43644570b8SJeff Kirsher static int full_duplex[MAX_UNITS];
44644570b8SJeff Kirsher static int options[MAX_UNITS];
45644570b8SJeff Kirsher 
46644570b8SJeff Kirsher /* Force a non std. amount of memory.  Units are 256 byte pages. */
47644570b8SJeff Kirsher /* #define PACKETBUF_MEMSIZE	0x40 */
48644570b8SJeff Kirsher 
49644570b8SJeff Kirsher 
50644570b8SJeff Kirsher #include <linux/module.h>
51644570b8SJeff Kirsher #include <linux/kernel.h>
52644570b8SJeff Kirsher #include <linux/errno.h>
53644570b8SJeff Kirsher #include <linux/pci.h>
54644570b8SJeff Kirsher #include <linux/init.h>
55644570b8SJeff Kirsher #include <linux/interrupt.h>
56644570b8SJeff Kirsher #include <linux/ethtool.h>
57644570b8SJeff Kirsher #include <linux/netdevice.h>
58644570b8SJeff Kirsher #include <linux/etherdevice.h>
59644570b8SJeff Kirsher 
6053c0ec4fSArmin Wolf #include <linux/io.h>
61644570b8SJeff Kirsher #include <asm/irq.h>
627c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
63644570b8SJeff Kirsher 
64644570b8SJeff Kirsher #include "8390.h"
65644570b8SJeff Kirsher 
66a050d82fSArmin Wolf static int ne2k_msg_enable;
67a050d82fSArmin Wolf 
68a050d82fSArmin Wolf static const int default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
69a050d82fSArmin Wolf 				      NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR);
70c45f812fSMatthew Whitehead 
71644570b8SJeff Kirsher #if defined(__powerpc__)
72644570b8SJeff Kirsher #define inl_le(addr)  le32_to_cpu(inl(addr))
73644570b8SJeff Kirsher #define inw_le(addr)  le16_to_cpu(inw(addr))
74644570b8SJeff Kirsher #endif
75644570b8SJeff Kirsher 
7653c0ec4fSArmin Wolf MODULE_AUTHOR(DRV_AUTHOR);
7753c0ec4fSArmin Wolf MODULE_DESCRIPTION(DRV_DESCRIPTION);
7853c0ec4fSArmin Wolf MODULE_VERSION(DRV_VERSION);
79644570b8SJeff Kirsher MODULE_LICENSE("GPL");
80644570b8SJeff Kirsher 
81a050d82fSArmin Wolf module_param_named(msg_enable, ne2k_msg_enable, int, 0444);
82644570b8SJeff Kirsher module_param_array(options, int, NULL, 0);
83644570b8SJeff Kirsher module_param_array(full_duplex, int, NULL, 0);
84c45f812fSMatthew Whitehead MODULE_PARM_DESC(msg_enable, "Debug message level (see linux/netdevice.h for bitmap)");
85644570b8SJeff Kirsher MODULE_PARM_DESC(options, "Bit 5: full duplex");
86644570b8SJeff Kirsher MODULE_PARM_DESC(full_duplex, "full duplex setting(s) (1)");
87644570b8SJeff Kirsher 
8853c0ec4fSArmin Wolf /* Some defines that people can play with if so inclined.
8953c0ec4fSArmin Wolf  */
90644570b8SJeff Kirsher 
91644570b8SJeff Kirsher /* Use 32 bit data-movement operations instead of 16 bit. */
92644570b8SJeff Kirsher #define USE_LONGIO
93644570b8SJeff Kirsher 
94644570b8SJeff Kirsher /* Do we implement the read before write bugfix ? */
95644570b8SJeff Kirsher /* #define NE_RW_BUGFIX */
96644570b8SJeff Kirsher 
9753c0ec4fSArmin Wolf /* Flags.  We rename an existing ei_status field to store flags!
9853c0ec4fSArmin Wolf  * Thus only the low 8 bits are usable for non-init-time flags.
9953c0ec4fSArmin Wolf  */
100644570b8SJeff Kirsher #define ne2k_flags reg0
10153c0ec4fSArmin Wolf 
102644570b8SJeff Kirsher enum {
10353c0ec4fSArmin Wolf 	/* Chip can do only 16/32-bit xfers. */
10453c0ec4fSArmin Wolf 	ONLY_16BIT_IO = 8, ONLY_32BIT_IO = 4,
10553c0ec4fSArmin Wolf 	/* User override. */
10653c0ec4fSArmin Wolf 	FORCE_FDX = 0x20,
107644570b8SJeff Kirsher 	REALTEK_FDX = 0x40, HOLTEK_FDX = 0x80,
108644570b8SJeff Kirsher 	STOP_PG_0x60 = 0x100,
109644570b8SJeff Kirsher };
110644570b8SJeff Kirsher 
111644570b8SJeff Kirsher enum ne2k_pci_chipsets {
112644570b8SJeff Kirsher 	CH_RealTek_RTL_8029 = 0,
113644570b8SJeff Kirsher 	CH_Winbond_89C940,
114644570b8SJeff Kirsher 	CH_Compex_RL2000,
115644570b8SJeff Kirsher 	CH_KTI_ET32P2,
116644570b8SJeff Kirsher 	CH_NetVin_NV5000SC,
117644570b8SJeff Kirsher 	CH_Via_86C926,
118644570b8SJeff Kirsher 	CH_SureCom_NE34,
119644570b8SJeff Kirsher 	CH_Winbond_W89C940F,
120644570b8SJeff Kirsher 	CH_Holtek_HT80232,
121644570b8SJeff Kirsher 	CH_Holtek_HT80229,
122644570b8SJeff Kirsher 	CH_Winbond_89C940_8c4a,
123644570b8SJeff Kirsher };
124644570b8SJeff Kirsher 
125644570b8SJeff Kirsher 
126644570b8SJeff Kirsher static struct {
127644570b8SJeff Kirsher 	char *name;
128644570b8SJeff Kirsher 	int flags;
1294168ac0eSBill Pemberton } pci_clone_list[] = {
13053c0ec4fSArmin Wolf 	{"RealTek RTL-8029(AS)", REALTEK_FDX},
131644570b8SJeff Kirsher 	{"Winbond 89C940", 0},
132644570b8SJeff Kirsher 	{"Compex RL2000", 0},
133644570b8SJeff Kirsher 	{"KTI ET32P2", 0},
134644570b8SJeff Kirsher 	{"NetVin NV5000SC", 0},
135644570b8SJeff Kirsher 	{"Via 86C926", ONLY_16BIT_IO},
136644570b8SJeff Kirsher 	{"SureCom NE34", 0},
137644570b8SJeff Kirsher 	{"Winbond W89C940F", 0},
138644570b8SJeff Kirsher 	{"Holtek HT80232", ONLY_16BIT_IO | HOLTEK_FDX},
139644570b8SJeff Kirsher 	{"Holtek HT80229", ONLY_32BIT_IO | HOLTEK_FDX | STOP_PG_0x60 },
140644570b8SJeff Kirsher 	{"Winbond W89C940(misprogrammed)", 0},
141644570b8SJeff Kirsher 	{NULL,}
142644570b8SJeff Kirsher };
143644570b8SJeff Kirsher 
144644570b8SJeff Kirsher 
1459baa3c34SBenoit Taine static const struct pci_device_id ne2k_pci_tbl[] = {
146644570b8SJeff Kirsher 	{ 0x10ec, 0x8029, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RealTek_RTL_8029 },
147644570b8SJeff Kirsher 	{ 0x1050, 0x0940, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_89C940 },
148644570b8SJeff Kirsher 	{ 0x11f6, 0x1401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Compex_RL2000 },
149644570b8SJeff Kirsher 	{ 0x8e2e, 0x3000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_KTI_ET32P2 },
150644570b8SJeff Kirsher 	{ 0x4a14, 0x5000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_NetVin_NV5000SC },
151644570b8SJeff Kirsher 	{ 0x1106, 0x0926, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Via_86C926 },
152644570b8SJeff Kirsher 	{ 0x10bd, 0x0e34, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_SureCom_NE34 },
153644570b8SJeff Kirsher 	{ 0x1050, 0x5a5a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_W89C940F },
154644570b8SJeff Kirsher 	{ 0x12c3, 0x0058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Holtek_HT80232 },
155644570b8SJeff Kirsher 	{ 0x12c3, 0x5598, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Holtek_HT80229 },
156644570b8SJeff Kirsher 	{ 0x8c4a, 0x1980, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_89C940_8c4a },
157644570b8SJeff Kirsher 	{ 0, }
158644570b8SJeff Kirsher };
15953c0ec4fSArmin Wolf 
160644570b8SJeff Kirsher MODULE_DEVICE_TABLE(pci, ne2k_pci_tbl);
161644570b8SJeff Kirsher 
162644570b8SJeff Kirsher 
163644570b8SJeff Kirsher /* ---- No user-serviceable parts below ---- */
164644570b8SJeff Kirsher 
165644570b8SJeff Kirsher #define NE_BASE	 (dev->base_addr)
166644570b8SJeff Kirsher #define NE_CMD		0x00
167644570b8SJeff Kirsher #define NE_DATAPORT	0x10	/* NatSemi-defined port window offset. */
168644570b8SJeff Kirsher #define NE_RESET	0x1f	/* Issue a read to reset, a write to clear. */
169644570b8SJeff Kirsher #define NE_IO_EXTENT	0x20
170644570b8SJeff Kirsher 
171644570b8SJeff Kirsher #define NESM_START_PG	0x40	/* First page of TX buffer */
172644570b8SJeff Kirsher #define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
173644570b8SJeff Kirsher 
174644570b8SJeff Kirsher 
175644570b8SJeff Kirsher static int ne2k_pci_open(struct net_device *dev);
176644570b8SJeff Kirsher static int ne2k_pci_close(struct net_device *dev);
177644570b8SJeff Kirsher 
178644570b8SJeff Kirsher static void ne2k_pci_reset_8390(struct net_device *dev);
17953c0ec4fSArmin Wolf static void ne2k_pci_get_8390_hdr(struct net_device *dev,
18053c0ec4fSArmin Wolf 				  struct e8390_pkt_hdr *hdr, int ring_page);
181644570b8SJeff Kirsher static void ne2k_pci_block_input(struct net_device *dev, int count,
182644570b8SJeff Kirsher 				 struct sk_buff *skb, int ring_offset);
183644570b8SJeff Kirsher static void ne2k_pci_block_output(struct net_device *dev, const int count,
18453c0ec4fSArmin Wolf 				  const unsigned char *buf,
18553c0ec4fSArmin Wolf 				  const int start_page);
186644570b8SJeff Kirsher static const struct ethtool_ops ne2k_pci_ethtool_ops;
187644570b8SJeff Kirsher 
188644570b8SJeff Kirsher 
189644570b8SJeff Kirsher 
190644570b8SJeff Kirsher /* There is no room in the standard 8390 structure for extra info we need,
19153c0ec4fSArmin Wolf  * so we build a meta/outer-wrapper structure..
19253c0ec4fSArmin Wolf  */
193644570b8SJeff Kirsher struct ne2k_pci_card {
194644570b8SJeff Kirsher 	struct net_device *dev;
195644570b8SJeff Kirsher 	struct pci_dev *pci_dev;
196644570b8SJeff Kirsher };
197644570b8SJeff Kirsher 
198644570b8SJeff Kirsher 
199644570b8SJeff Kirsher 
20053c0ec4fSArmin Wolf /* NEx000-clone boards have a Station Address (SA) PROM (SAPROM) in the packet
20153c0ec4fSArmin Wolf  * buffer memory space.  By-the-spec NE2000 clones have 0x57,0x57 in bytes
20253c0ec4fSArmin Wolf  * 0x0e,0x0f of the SAPROM, while other supposed NE2000 clones must be
20353c0ec4fSArmin Wolf  * detected by their SA prefix.
20453c0ec4fSArmin Wolf  *
20553c0ec4fSArmin Wolf  * Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
20653c0ec4fSArmin Wolf  * mode results in doubled values, which can be detected and compensated for.
20753c0ec4fSArmin Wolf  *
20853c0ec4fSArmin Wolf  * The probe is also responsible for initializing the card and filling
20953c0ec4fSArmin Wolf  * in the 'dev' and 'ei_status' structures.
210644570b8SJeff Kirsher  */
211644570b8SJeff Kirsher 
212644570b8SJeff Kirsher static const struct net_device_ops ne2k_netdev_ops = {
213644570b8SJeff Kirsher 	.ndo_open		= ne2k_pci_open,
214644570b8SJeff Kirsher 	.ndo_stop		= ne2k_pci_close,
215644570b8SJeff Kirsher 	.ndo_start_xmit		= ei_start_xmit,
216644570b8SJeff Kirsher 	.ndo_tx_timeout		= ei_tx_timeout,
217644570b8SJeff Kirsher 	.ndo_get_stats		= ei_get_stats,
218afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= ei_set_multicast_list,
219644570b8SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
220644570b8SJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
221644570b8SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
222644570b8SJeff Kirsher 	.ndo_poll_controller = ei_poll,
223644570b8SJeff Kirsher #endif
224644570b8SJeff Kirsher };
225644570b8SJeff Kirsher 
ne2k_pci_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)2264168ac0eSBill Pemberton static int ne2k_pci_init_one(struct pci_dev *pdev,
227644570b8SJeff Kirsher 			     const struct pci_device_id *ent)
228644570b8SJeff Kirsher {
229644570b8SJeff Kirsher 	struct net_device *dev;
230644570b8SJeff Kirsher 	int i;
231644570b8SJeff Kirsher 	unsigned char SA_prom[32];
232644570b8SJeff Kirsher 	int start_page, stop_page;
233644570b8SJeff Kirsher 	int irq, reg0, chip_idx = ent->driver_data;
234644570b8SJeff Kirsher 	static unsigned int fnd_cnt;
235644570b8SJeff Kirsher 	long ioaddr;
236644570b8SJeff Kirsher 	int flags = pci_clone_list[chip_idx].flags;
237c45f812fSMatthew Whitehead 	struct ei_device *ei_local;
238644570b8SJeff Kirsher 
239644570b8SJeff Kirsher 	fnd_cnt++;
240644570b8SJeff Kirsher 
241644570b8SJeff Kirsher 	i = pci_enable_device(pdev);
242644570b8SJeff Kirsher 	if (i)
243644570b8SJeff Kirsher 		return i;
244644570b8SJeff Kirsher 
245644570b8SJeff Kirsher 	ioaddr = pci_resource_start(pdev, 0);
246644570b8SJeff Kirsher 	irq = pdev->irq;
247644570b8SJeff Kirsher 
248644570b8SJeff Kirsher 	if (!ioaddr || ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) == 0)) {
249644570b8SJeff Kirsher 		dev_err(&pdev->dev, "no I/O resource at PCI BAR #0\n");
250eb69c5bfSJia-Ju Bai 		goto err_out;
251644570b8SJeff Kirsher 	}
252644570b8SJeff Kirsher 
25353c0ec4fSArmin Wolf 	if (!request_region(ioaddr, NE_IO_EXTENT, DRV_NAME)) {
254644570b8SJeff Kirsher 		dev_err(&pdev->dev, "I/O resource 0x%x @ 0x%lx busy\n",
255644570b8SJeff Kirsher 			NE_IO_EXTENT, ioaddr);
256eb69c5bfSJia-Ju Bai 		goto err_out;
257644570b8SJeff Kirsher 	}
258644570b8SJeff Kirsher 
259644570b8SJeff Kirsher 	reg0 = inb(ioaddr);
260644570b8SJeff Kirsher 	if (reg0 == 0xFF)
261644570b8SJeff Kirsher 		goto err_out_free_res;
262644570b8SJeff Kirsher 
263644570b8SJeff Kirsher 	/* Do a preliminary verification that we have a 8390. */
264644570b8SJeff Kirsher 	{
265644570b8SJeff Kirsher 		int regd;
26653c0ec4fSArmin Wolf 
267644570b8SJeff Kirsher 		outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
268644570b8SJeff Kirsher 		regd = inb(ioaddr + 0x0d);
269644570b8SJeff Kirsher 		outb(0xff, ioaddr + 0x0d);
270644570b8SJeff Kirsher 		outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
27153c0ec4fSArmin Wolf 		/* Clear the counter by reading. */
27253c0ec4fSArmin Wolf 		inb(ioaddr + EN0_COUNTER0);
273644570b8SJeff Kirsher 		if (inb(ioaddr + EN0_COUNTER0) != 0) {
274644570b8SJeff Kirsher 			outb(reg0, ioaddr);
27553c0ec4fSArmin Wolf 			/*  Restore the old values. */
27653c0ec4fSArmin Wolf 			outb(regd, ioaddr + 0x0d);
277644570b8SJeff Kirsher 			goto err_out_free_res;
278644570b8SJeff Kirsher 		}
279644570b8SJeff Kirsher 	}
280644570b8SJeff Kirsher 
281644570b8SJeff Kirsher 	/* Allocate net_device, dev->priv; fill in 8390 specific dev fields. */
282644570b8SJeff Kirsher 	dev = alloc_ei_netdev();
283644570b8SJeff Kirsher 	if (!dev) {
284644570b8SJeff Kirsher 		dev_err(&pdev->dev, "cannot allocate ethernet device\n");
285644570b8SJeff Kirsher 		goto err_out_free_res;
286644570b8SJeff Kirsher 	}
287644570b8SJeff Kirsher 	dev->netdev_ops = &ne2k_netdev_ops;
288c45f812fSMatthew Whitehead 	ei_local = netdev_priv(dev);
289a050d82fSArmin Wolf 	ei_local->msg_enable = netif_msg_init(ne2k_msg_enable, default_msg_level);
290644570b8SJeff Kirsher 
291644570b8SJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
292644570b8SJeff Kirsher 
293644570b8SJeff Kirsher 	/* Reset card. Who knows what dain-bramaged state it was left in. */
294644570b8SJeff Kirsher 	{
295644570b8SJeff Kirsher 		unsigned long reset_start_time = jiffies;
296644570b8SJeff Kirsher 
297644570b8SJeff Kirsher 		outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
298644570b8SJeff Kirsher 
29953c0ec4fSArmin Wolf 		/* This looks like a horrible timing loop, but it should never
30053c0ec4fSArmin Wolf 		 * take more than a few cycles.
301644570b8SJeff Kirsher 		 */
302644570b8SJeff Kirsher 		while ((inb(ioaddr + EN0_ISR) & ENISR_RESET) == 0)
303644570b8SJeff Kirsher 			/* Limit wait: '2' avoids jiffy roll-over. */
304644570b8SJeff Kirsher 			if (jiffies - reset_start_time > 2) {
305644570b8SJeff Kirsher 				dev_err(&pdev->dev,
306644570b8SJeff Kirsher 					"Card failure (no reset ack).\n");
307644570b8SJeff Kirsher 				goto err_out_free_netdev;
308644570b8SJeff Kirsher 			}
30953c0ec4fSArmin Wolf 		/* Ack all intr. */
31053c0ec4fSArmin Wolf 		outb(0xff, ioaddr + EN0_ISR);
311644570b8SJeff Kirsher 	}
312644570b8SJeff Kirsher 
313644570b8SJeff Kirsher 	/* Read the 16 bytes of station address PROM.
31453c0ec4fSArmin Wolf 	 * We must first initialize registers, similar
31553c0ec4fSArmin Wolf 	 * to NS8390_init(eifdev, 0).
31653c0ec4fSArmin Wolf 	 * We can't reliably read the SAPROM address without this.
31753c0ec4fSArmin Wolf 	 * (I learned the hard way!).
31853c0ec4fSArmin Wolf 	 */
319644570b8SJeff Kirsher 	{
320644570b8SJeff Kirsher 		struct {unsigned char value, offset; } program_seq[] = {
32153c0ec4fSArmin Wolf 			/* Select page 0 */
32253c0ec4fSArmin Wolf 			{E8390_NODMA + E8390_PAGE0 + E8390_STOP, E8390_CMD},
32353c0ec4fSArmin Wolf 			/* Set word-wide access */
32453c0ec4fSArmin Wolf 			{0x49,	EN0_DCFG},
32553c0ec4fSArmin Wolf 			/* Clear the count regs. */
32653c0ec4fSArmin Wolf 			{0x00,	EN0_RCNTLO},
32753c0ec4fSArmin Wolf 			/* Mask completion IRQ */
328644570b8SJeff Kirsher 			{0x00,	EN0_RCNTHI},
32953c0ec4fSArmin Wolf 			{0x00,	EN0_IMR},
330644570b8SJeff Kirsher 			{0xFF,	EN0_ISR},
33153c0ec4fSArmin Wolf 			/* 0x20 Set to monitor */
33253c0ec4fSArmin Wolf 			{E8390_RXOFF, EN0_RXCR},
33353c0ec4fSArmin Wolf 			/* 0x02 and loopback mode */
33453c0ec4fSArmin Wolf 			{E8390_TXOFF, EN0_TXCR},
335644570b8SJeff Kirsher 			{32,	EN0_RCNTLO},
336644570b8SJeff Kirsher 			{0x00,	EN0_RCNTHI},
33753c0ec4fSArmin Wolf 			/* DMA starting at 0x0000 */
33853c0ec4fSArmin Wolf 			{0x00,	EN0_RSARLO},
339644570b8SJeff Kirsher 			{0x00,	EN0_RSARHI},
340644570b8SJeff Kirsher 			{E8390_RREAD+E8390_START, E8390_CMD},
341644570b8SJeff Kirsher 		};
342644570b8SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(program_seq); i++)
34353c0ec4fSArmin Wolf 			outb(program_seq[i].value,
34453c0ec4fSArmin Wolf 			     ioaddr + program_seq[i].offset);
345644570b8SJeff Kirsher 
346644570b8SJeff Kirsher 	}
347644570b8SJeff Kirsher 
348644570b8SJeff Kirsher 	/* Note: all PCI cards have at least 16 bit access, so we don't have
34953c0ec4fSArmin Wolf 	 * to check for 8 bit cards.  Most cards permit 32 bit access.
35053c0ec4fSArmin Wolf 	 */
351644570b8SJeff Kirsher 	if (flags & ONLY_32BIT_IO) {
352644570b8SJeff Kirsher 		for (i = 0; i < 4 ; i++)
353644570b8SJeff Kirsher 			((u32 *)SA_prom)[i] = le32_to_cpu(inl(ioaddr + NE_DATAPORT));
354644570b8SJeff Kirsher 	} else
355644570b8SJeff Kirsher 		for (i = 0; i < 32 /* sizeof(SA_prom )*/; i++)
356644570b8SJeff Kirsher 			SA_prom[i] = inb(ioaddr + NE_DATAPORT);
357644570b8SJeff Kirsher 
358644570b8SJeff Kirsher 	/* We always set the 8390 registers for word mode. */
359644570b8SJeff Kirsher 	outb(0x49, ioaddr + EN0_DCFG);
360644570b8SJeff Kirsher 	start_page = NESM_START_PG;
361644570b8SJeff Kirsher 
362644570b8SJeff Kirsher 	stop_page = flags & STOP_PG_0x60 ? 0x60 : NESM_STOP_PG;
363644570b8SJeff Kirsher 
364644570b8SJeff Kirsher 	/* Set up the rest of the parameters. */
365644570b8SJeff Kirsher 	dev->irq = irq;
366644570b8SJeff Kirsher 	dev->base_addr = ioaddr;
367644570b8SJeff Kirsher 	pci_set_drvdata(pdev, dev);
368644570b8SJeff Kirsher 
369644570b8SJeff Kirsher 	ei_status.name = pci_clone_list[chip_idx].name;
370644570b8SJeff Kirsher 	ei_status.tx_start_page = start_page;
371644570b8SJeff Kirsher 	ei_status.stop_page = stop_page;
372644570b8SJeff Kirsher 	ei_status.word16 = 1;
373644570b8SJeff Kirsher 	ei_status.ne2k_flags = flags;
374644570b8SJeff Kirsher 	if (fnd_cnt < MAX_UNITS) {
375644570b8SJeff Kirsher 		if (full_duplex[fnd_cnt] > 0 || (options[fnd_cnt] & FORCE_FDX))
376644570b8SJeff Kirsher 			ei_status.ne2k_flags |= FORCE_FDX;
377644570b8SJeff Kirsher 	}
378644570b8SJeff Kirsher 
379644570b8SJeff Kirsher 	ei_status.rx_start_page = start_page + TX_PAGES;
380644570b8SJeff Kirsher #ifdef PACKETBUF_MEMSIZE
381644570b8SJeff Kirsher 	/* Allow the packet buffer size to be overridden by know-it-alls. */
382644570b8SJeff Kirsher 	ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
383644570b8SJeff Kirsher #endif
384644570b8SJeff Kirsher 
385644570b8SJeff Kirsher 	ei_status.reset_8390 = &ne2k_pci_reset_8390;
386644570b8SJeff Kirsher 	ei_status.block_input = &ne2k_pci_block_input;
387644570b8SJeff Kirsher 	ei_status.block_output = &ne2k_pci_block_output;
388644570b8SJeff Kirsher 	ei_status.get_8390_hdr = &ne2k_pci_get_8390_hdr;
389644570b8SJeff Kirsher 	ei_status.priv = (unsigned long) pdev;
390644570b8SJeff Kirsher 
391644570b8SJeff Kirsher 	dev->ethtool_ops = &ne2k_pci_ethtool_ops;
392644570b8SJeff Kirsher 	NS8390_init(dev, 0);
393644570b8SJeff Kirsher 
394a05e4c0aSJakub Kicinski 	eth_hw_addr_set(dev, SA_prom);
395644570b8SJeff Kirsher 
396644570b8SJeff Kirsher 	i = register_netdev(dev);
397644570b8SJeff Kirsher 	if (i)
398644570b8SJeff Kirsher 		goto err_out_free_netdev;
399644570b8SJeff Kirsher 
400c45f812fSMatthew Whitehead 	netdev_info(dev, "%s found at %#lx, IRQ %d, %pM.\n",
401c45f812fSMatthew Whitehead 		    pci_clone_list[chip_idx].name, ioaddr, dev->irq,
402644570b8SJeff Kirsher 		    dev->dev_addr);
403644570b8SJeff Kirsher 
404644570b8SJeff Kirsher 	return 0;
405644570b8SJeff Kirsher 
406644570b8SJeff Kirsher err_out_free_netdev:
407644570b8SJeff Kirsher 	free_netdev(dev);
408644570b8SJeff Kirsher err_out_free_res:
409644570b8SJeff Kirsher 	release_region(ioaddr, NE_IO_EXTENT);
410eb69c5bfSJia-Ju Bai err_out:
411eb69c5bfSJia-Ju Bai 	pci_disable_device(pdev);
412644570b8SJeff Kirsher 	return -ENODEV;
413644570b8SJeff Kirsher }
414644570b8SJeff Kirsher 
41553c0ec4fSArmin Wolf /* Magic incantation sequence for full duplex on the supported cards.
416644570b8SJeff Kirsher  */
set_realtek_fdx(struct net_device * dev)417644570b8SJeff Kirsher static inline int set_realtek_fdx(struct net_device *dev)
418644570b8SJeff Kirsher {
419644570b8SJeff Kirsher 	long ioaddr = dev->base_addr;
420644570b8SJeff Kirsher 
421644570b8SJeff Kirsher 	outb(0xC0 + E8390_NODMA, ioaddr + NE_CMD); /* Page 3 */
422644570b8SJeff Kirsher 	outb(0xC0, ioaddr + 0x01); /* Enable writes to CONFIG3 */
423644570b8SJeff Kirsher 	outb(0x40, ioaddr + 0x06); /* Enable full duplex */
424644570b8SJeff Kirsher 	outb(0x00, ioaddr + 0x01); /* Disable writes to CONFIG3 */
425644570b8SJeff Kirsher 	outb(E8390_PAGE0 + E8390_NODMA, ioaddr + NE_CMD); /* Page 0 */
426644570b8SJeff Kirsher 	return 0;
427644570b8SJeff Kirsher }
428644570b8SJeff Kirsher 
set_holtek_fdx(struct net_device * dev)429644570b8SJeff Kirsher static inline int set_holtek_fdx(struct net_device *dev)
430644570b8SJeff Kirsher {
431644570b8SJeff Kirsher 	long ioaddr = dev->base_addr;
432644570b8SJeff Kirsher 
433644570b8SJeff Kirsher 	outb(inb(ioaddr + 0x20) | 0x80, ioaddr + 0x20);
434644570b8SJeff Kirsher 	return 0;
435644570b8SJeff Kirsher }
436644570b8SJeff Kirsher 
ne2k_pci_set_fdx(struct net_device * dev)437644570b8SJeff Kirsher static int ne2k_pci_set_fdx(struct net_device *dev)
438644570b8SJeff Kirsher {
439644570b8SJeff Kirsher 	if (ei_status.ne2k_flags & REALTEK_FDX)
440644570b8SJeff Kirsher 		return set_realtek_fdx(dev);
441644570b8SJeff Kirsher 	else if (ei_status.ne2k_flags & HOLTEK_FDX)
442644570b8SJeff Kirsher 		return set_holtek_fdx(dev);
443644570b8SJeff Kirsher 
444644570b8SJeff Kirsher 	return -EOPNOTSUPP;
445644570b8SJeff Kirsher }
446644570b8SJeff Kirsher 
ne2k_pci_open(struct net_device * dev)447644570b8SJeff Kirsher static int ne2k_pci_open(struct net_device *dev)
448644570b8SJeff Kirsher {
44953c0ec4fSArmin Wolf 	int ret = request_irq(dev->irq, ei_interrupt, IRQF_SHARED,
45053c0ec4fSArmin Wolf 			      dev->name, dev);
45153c0ec4fSArmin Wolf 
452644570b8SJeff Kirsher 	if (ret)
453644570b8SJeff Kirsher 		return ret;
454644570b8SJeff Kirsher 
455644570b8SJeff Kirsher 	if (ei_status.ne2k_flags & FORCE_FDX)
456644570b8SJeff Kirsher 		ne2k_pci_set_fdx(dev);
457644570b8SJeff Kirsher 
458644570b8SJeff Kirsher 	ei_open(dev);
459644570b8SJeff Kirsher 	return 0;
460644570b8SJeff Kirsher }
461644570b8SJeff Kirsher 
ne2k_pci_close(struct net_device * dev)462644570b8SJeff Kirsher static int ne2k_pci_close(struct net_device *dev)
463644570b8SJeff Kirsher {
464644570b8SJeff Kirsher 	ei_close(dev);
465644570b8SJeff Kirsher 	free_irq(dev->irq, dev);
466644570b8SJeff Kirsher 	return 0;
467644570b8SJeff Kirsher }
468644570b8SJeff Kirsher 
469644570b8SJeff Kirsher /* Hard reset the card.  This used to pause for the same period that a
47053c0ec4fSArmin Wolf  * 8390 reset command required, but that shouldn't be necessary.
47153c0ec4fSArmin Wolf  */
ne2k_pci_reset_8390(struct net_device * dev)472644570b8SJeff Kirsher static void ne2k_pci_reset_8390(struct net_device *dev)
473644570b8SJeff Kirsher {
474644570b8SJeff Kirsher 	unsigned long reset_start_time = jiffies;
475c45f812fSMatthew Whitehead 	struct ei_device *ei_local = netdev_priv(dev);
476644570b8SJeff Kirsher 
477c45f812fSMatthew Whitehead 	netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n",
478c45f812fSMatthew Whitehead 		  jiffies);
479644570b8SJeff Kirsher 
480644570b8SJeff Kirsher 	outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
481644570b8SJeff Kirsher 
482644570b8SJeff Kirsher 	ei_status.txing = 0;
483644570b8SJeff Kirsher 	ei_status.dmaing = 0;
484644570b8SJeff Kirsher 
485644570b8SJeff Kirsher 	/* This check _should_not_ be necessary, omit eventually. */
486644570b8SJeff Kirsher 	while ((inb(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
487644570b8SJeff Kirsher 		if (jiffies - reset_start_time > 2) {
48853c0ec4fSArmin Wolf 			netdev_err(dev, "%s did not complete.\n", __func__);
489644570b8SJeff Kirsher 			break;
490644570b8SJeff Kirsher 		}
49153c0ec4fSArmin Wolf 	/* Ack intr. */
49253c0ec4fSArmin Wolf 	outb(ENISR_RESET, NE_BASE + EN0_ISR);
493644570b8SJeff Kirsher }
494644570b8SJeff Kirsher 
495644570b8SJeff Kirsher /* Grab the 8390 specific header. Similar to the block_input routine, but
49653c0ec4fSArmin Wolf  * we don't need to be concerned with ring wrap as the header will be at
49753c0ec4fSArmin Wolf  * the start of a page, so we optimize accordingly.
49853c0ec4fSArmin Wolf  */
499644570b8SJeff Kirsher 
ne2k_pci_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)50053c0ec4fSArmin Wolf static void ne2k_pci_get_8390_hdr(struct net_device *dev,
50153c0ec4fSArmin Wolf 				  struct e8390_pkt_hdr *hdr, int ring_page)
502644570b8SJeff Kirsher {
503644570b8SJeff Kirsher 
504644570b8SJeff Kirsher 	long nic_base = dev->base_addr;
505644570b8SJeff Kirsher 
50653c0ec4fSArmin Wolf 	/* This *shouldn't* happen. If it does, it's the last thing you'll see
50753c0ec4fSArmin Wolf 	 */
508644570b8SJeff Kirsher 	if (ei_status.dmaing) {
50953c0ec4fSArmin Wolf 		netdev_err(dev, "DMAing conflict in %s [DMAstat:%d][irqlock:%d].\n",
51053c0ec4fSArmin Wolf 			   __func__, ei_status.dmaing, ei_status.irqlock);
511644570b8SJeff Kirsher 		return;
512644570b8SJeff Kirsher 	}
513644570b8SJeff Kirsher 
514644570b8SJeff Kirsher 	ei_status.dmaing |= 0x01;
515644570b8SJeff Kirsher 	outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
516644570b8SJeff Kirsher 	outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
517644570b8SJeff Kirsher 	outb(0, nic_base + EN0_RCNTHI);
518644570b8SJeff Kirsher 	outb(0, nic_base + EN0_RSARLO);		/* On page boundary */
519644570b8SJeff Kirsher 	outb(ring_page, nic_base + EN0_RSARHI);
520644570b8SJeff Kirsher 	outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
521644570b8SJeff Kirsher 
522644570b8SJeff Kirsher 	if (ei_status.ne2k_flags & ONLY_16BIT_IO) {
52353c0ec4fSArmin Wolf 		insw(NE_BASE + NE_DATAPORT, hdr,
52453c0ec4fSArmin Wolf 		     sizeof(struct e8390_pkt_hdr) >> 1);
525644570b8SJeff Kirsher 	} else {
526644570b8SJeff Kirsher 		*(u32 *)hdr = le32_to_cpu(inl(NE_BASE + NE_DATAPORT));
527644570b8SJeff Kirsher 		le16_to_cpus(&hdr->count);
528644570b8SJeff Kirsher 	}
52953c0ec4fSArmin Wolf 	/* Ack intr. */
53053c0ec4fSArmin Wolf 	outb(ENISR_RDC, nic_base + EN0_ISR);
531644570b8SJeff Kirsher 	ei_status.dmaing &= ~0x01;
532644570b8SJeff Kirsher }
533644570b8SJeff Kirsher 
534644570b8SJeff Kirsher /* Block input and output, similar to the Crynwr packet driver.  If you
53553c0ec4fSArmin Wolf  *are porting to a new ethercard, look at the packet driver source for hints.
53653c0ec4fSArmin Wolf  *The NEx000 doesn't share the on-board packet memory -- you have to put
53753c0ec4fSArmin Wolf  *the packet out through the "remote DMA" dataport using outb.
53853c0ec4fSArmin Wolf  */
539644570b8SJeff Kirsher 
ne2k_pci_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)540644570b8SJeff Kirsher static void ne2k_pci_block_input(struct net_device *dev, int count,
541644570b8SJeff Kirsher 				 struct sk_buff *skb, int ring_offset)
542644570b8SJeff Kirsher {
543644570b8SJeff Kirsher 	long nic_base = dev->base_addr;
544644570b8SJeff Kirsher 	char *buf = skb->data;
545644570b8SJeff Kirsher 
54653c0ec4fSArmin Wolf 	/* This *shouldn't* happen.
54753c0ec4fSArmin Wolf 	 * If it does, it's the last thing you'll see.
54853c0ec4fSArmin Wolf 	 */
549644570b8SJeff Kirsher 	if (ei_status.dmaing) {
55053c0ec4fSArmin Wolf 		netdev_err(dev, "DMAing conflict in %s [DMAstat:%d][irqlock:%d]\n",
55153c0ec4fSArmin Wolf 			   __func__, ei_status.dmaing, ei_status.irqlock);
552644570b8SJeff Kirsher 		return;
553644570b8SJeff Kirsher 	}
554644570b8SJeff Kirsher 	ei_status.dmaing |= 0x01;
555644570b8SJeff Kirsher 	if (ei_status.ne2k_flags & ONLY_32BIT_IO)
556644570b8SJeff Kirsher 		count = (count + 3) & 0xFFFC;
557644570b8SJeff Kirsher 	outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
558644570b8SJeff Kirsher 	outb(count & 0xff, nic_base + EN0_RCNTLO);
559644570b8SJeff Kirsher 	outb(count >> 8, nic_base + EN0_RCNTHI);
560644570b8SJeff Kirsher 	outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
561644570b8SJeff Kirsher 	outb(ring_offset >> 8, nic_base + EN0_RSARHI);
562644570b8SJeff Kirsher 	outb(E8390_RREAD + E8390_START, nic_base + NE_CMD);
563644570b8SJeff Kirsher 
564644570b8SJeff Kirsher 	if (ei_status.ne2k_flags & ONLY_16BIT_IO) {
565644570b8SJeff Kirsher 		insw(NE_BASE + NE_DATAPORT, buf, count >> 1);
56653c0ec4fSArmin Wolf 		if (count & 0x01)
567644570b8SJeff Kirsher 			buf[count-1] = inb(NE_BASE + NE_DATAPORT);
568644570b8SJeff Kirsher 	} else {
569644570b8SJeff Kirsher 		insl(NE_BASE + NE_DATAPORT, buf, count >> 2);
570644570b8SJeff Kirsher 		if (count & 3) {
571644570b8SJeff Kirsher 			buf += count & ~3;
572644570b8SJeff Kirsher 			if (count & 2) {
573644570b8SJeff Kirsher 				__le16 *b = (__le16 *)buf;
574644570b8SJeff Kirsher 
575644570b8SJeff Kirsher 				*b++ = cpu_to_le16(inw(NE_BASE + NE_DATAPORT));
576644570b8SJeff Kirsher 				buf = (char *)b;
577644570b8SJeff Kirsher 			}
578644570b8SJeff Kirsher 			if (count & 1)
579644570b8SJeff Kirsher 				*buf = inb(NE_BASE + NE_DATAPORT);
580644570b8SJeff Kirsher 		}
581644570b8SJeff Kirsher 	}
58253c0ec4fSArmin Wolf 	/* Ack intr. */
58353c0ec4fSArmin Wolf 	outb(ENISR_RDC, nic_base + EN0_ISR);
584644570b8SJeff Kirsher 	ei_status.dmaing &= ~0x01;
585644570b8SJeff Kirsher }
586644570b8SJeff Kirsher 
ne2k_pci_block_output(struct net_device * dev,int count,const unsigned char * buf,const int start_page)587644570b8SJeff Kirsher static void ne2k_pci_block_output(struct net_device *dev, int count,
588644570b8SJeff Kirsher 		const unsigned char *buf, const int start_page)
589644570b8SJeff Kirsher {
590644570b8SJeff Kirsher 	long nic_base = NE_BASE;
591644570b8SJeff Kirsher 	unsigned long dma_start;
592644570b8SJeff Kirsher 
593644570b8SJeff Kirsher 	/* On little-endian it's always safe to round the count up for
59453c0ec4fSArmin Wolf 	 * word writes.
59553c0ec4fSArmin Wolf 	 */
596644570b8SJeff Kirsher 	if (ei_status.ne2k_flags & ONLY_32BIT_IO)
597644570b8SJeff Kirsher 		count = (count + 3) & 0xFFFC;
598644570b8SJeff Kirsher 	else
599644570b8SJeff Kirsher 		if (count & 0x01)
600644570b8SJeff Kirsher 			count++;
601644570b8SJeff Kirsher 
60253c0ec4fSArmin Wolf 	/* This *shouldn't* happen.
60353c0ec4fSArmin Wolf 	 * If it does, it's the last thing you'll see.
60453c0ec4fSArmin Wolf 	 */
605644570b8SJeff Kirsher 	if (ei_status.dmaing) {
60653c0ec4fSArmin Wolf 		netdev_err(dev, "DMAing conflict in %s [DMAstat:%d][irqlock:%d]\n",
60753c0ec4fSArmin Wolf 			   __func__, ei_status.dmaing, ei_status.irqlock);
608644570b8SJeff Kirsher 		return;
609644570b8SJeff Kirsher 	}
610644570b8SJeff Kirsher 	ei_status.dmaing |= 0x01;
611644570b8SJeff Kirsher 	/* We should already be in page 0, but to be safe... */
612644570b8SJeff Kirsher 	outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
613644570b8SJeff Kirsher 
614c24672cfSArmin Wolf #ifdef NE_RW_BUGFIX
615644570b8SJeff Kirsher 	/* Handle the read-before-write bug the same way as the
61653c0ec4fSArmin Wolf 	 * Crynwr packet driver -- the NatSemi method doesn't work.
61753c0ec4fSArmin Wolf 	 * Actually this doesn't always work either, but if you have
61853c0ec4fSArmin Wolf 	 * problems with your NEx000 this is better than nothing!
61953c0ec4fSArmin Wolf 	 */
620644570b8SJeff Kirsher 	outb(0x42, nic_base + EN0_RCNTLO);
621644570b8SJeff Kirsher 	outb(0x00, nic_base + EN0_RCNTHI);
622644570b8SJeff Kirsher 	outb(0x42, nic_base + EN0_RSARLO);
623644570b8SJeff Kirsher 	outb(0x00, nic_base + EN0_RSARHI);
624644570b8SJeff Kirsher 	outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
625644570b8SJeff Kirsher #endif
626644570b8SJeff Kirsher 	outb(ENISR_RDC, nic_base + EN0_ISR);
627644570b8SJeff Kirsher 
628644570b8SJeff Kirsher 	/* Now the normal output. */
629644570b8SJeff Kirsher 	outb(count & 0xff, nic_base + EN0_RCNTLO);
630644570b8SJeff Kirsher 	outb(count >> 8,   nic_base + EN0_RCNTHI);
631644570b8SJeff Kirsher 	outb(0x00, nic_base + EN0_RSARLO);
632644570b8SJeff Kirsher 	outb(start_page, nic_base + EN0_RSARHI);
633644570b8SJeff Kirsher 	outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
634644570b8SJeff Kirsher 	if (ei_status.ne2k_flags & ONLY_16BIT_IO) {
635644570b8SJeff Kirsher 		outsw(NE_BASE + NE_DATAPORT, buf, count >> 1);
636644570b8SJeff Kirsher 	} else {
637644570b8SJeff Kirsher 		outsl(NE_BASE + NE_DATAPORT, buf, count >> 2);
638644570b8SJeff Kirsher 		if (count & 3) {
639644570b8SJeff Kirsher 			buf += count & ~3;
640644570b8SJeff Kirsher 			if (count & 2) {
641644570b8SJeff Kirsher 				__le16 *b = (__le16 *)buf;
642644570b8SJeff Kirsher 
643644570b8SJeff Kirsher 				outw(le16_to_cpu(*b++), NE_BASE + NE_DATAPORT);
644644570b8SJeff Kirsher 				buf = (char *)b;
645644570b8SJeff Kirsher 			}
646644570b8SJeff Kirsher 		}
647644570b8SJeff Kirsher 	}
648644570b8SJeff Kirsher 
649644570b8SJeff Kirsher 	dma_start = jiffies;
650644570b8SJeff Kirsher 
651644570b8SJeff Kirsher 	while ((inb(nic_base + EN0_ISR) & ENISR_RDC) == 0)
65253c0ec4fSArmin Wolf 		/* Avoid clock roll-over. */
65353c0ec4fSArmin Wolf 		if (jiffies - dma_start > 2) {
654c45f812fSMatthew Whitehead 			netdev_warn(dev, "timeout waiting for Tx RDC.\n");
655644570b8SJeff Kirsher 			ne2k_pci_reset_8390(dev);
656644570b8SJeff Kirsher 			NS8390_init(dev, 1);
657644570b8SJeff Kirsher 			break;
658644570b8SJeff Kirsher 		}
65953c0ec4fSArmin Wolf 	/* Ack intr. */
66053c0ec4fSArmin Wolf 	outb(ENISR_RDC, nic_base + EN0_ISR);
661644570b8SJeff Kirsher 	ei_status.dmaing &= ~0x01;
662644570b8SJeff Kirsher }
663644570b8SJeff Kirsher 
ne2k_pci_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)664644570b8SJeff Kirsher static void ne2k_pci_get_drvinfo(struct net_device *dev,
665644570b8SJeff Kirsher 				 struct ethtool_drvinfo *info)
666644570b8SJeff Kirsher {
667644570b8SJeff Kirsher 	struct ei_device *ei = netdev_priv(dev);
668644570b8SJeff Kirsher 	struct pci_dev *pci_dev = (struct pci_dev *) ei->priv;
669644570b8SJeff Kirsher 
67053c0ec4fSArmin Wolf 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
67153c0ec4fSArmin Wolf 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
67253c0ec4fSArmin Wolf 	strscpy(info->bus_info, pci_name(pci_dev), sizeof(info->bus_info));
673644570b8SJeff Kirsher }
674644570b8SJeff Kirsher 
ne2k_pci_get_msglevel(struct net_device * dev)675c45f812fSMatthew Whitehead static u32 ne2k_pci_get_msglevel(struct net_device *dev)
676c45f812fSMatthew Whitehead {
677c45f812fSMatthew Whitehead 	struct ei_device *ei_local = netdev_priv(dev);
678c45f812fSMatthew Whitehead 
679c45f812fSMatthew Whitehead 	return ei_local->msg_enable;
680c45f812fSMatthew Whitehead }
681c45f812fSMatthew Whitehead 
ne2k_pci_set_msglevel(struct net_device * dev,u32 v)682c45f812fSMatthew Whitehead static void ne2k_pci_set_msglevel(struct net_device *dev, u32 v)
683c45f812fSMatthew Whitehead {
684c45f812fSMatthew Whitehead 	struct ei_device *ei_local = netdev_priv(dev);
685c45f812fSMatthew Whitehead 
686c45f812fSMatthew Whitehead 	ei_local->msg_enable = v;
687c45f812fSMatthew Whitehead }
688c45f812fSMatthew Whitehead 
689644570b8SJeff Kirsher static const struct ethtool_ops ne2k_pci_ethtool_ops = {
690644570b8SJeff Kirsher 	.get_drvinfo		= ne2k_pci_get_drvinfo,
691c45f812fSMatthew Whitehead 	.get_msglevel		= ne2k_pci_get_msglevel,
692c45f812fSMatthew Whitehead 	.set_msglevel		= ne2k_pci_set_msglevel,
693644570b8SJeff Kirsher };
694644570b8SJeff Kirsher 
ne2k_pci_remove_one(struct pci_dev * pdev)6954168ac0eSBill Pemberton static void ne2k_pci_remove_one(struct pci_dev *pdev)
696644570b8SJeff Kirsher {
697644570b8SJeff Kirsher 	struct net_device *dev = pci_get_drvdata(pdev);
698644570b8SJeff Kirsher 
699644570b8SJeff Kirsher 	BUG_ON(!dev);
700644570b8SJeff Kirsher 	unregister_netdev(dev);
701644570b8SJeff Kirsher 	release_region(dev->base_addr, NE_IO_EXTENT);
702644570b8SJeff Kirsher 	free_netdev(dev);
703644570b8SJeff Kirsher 	pci_disable_device(pdev);
704644570b8SJeff Kirsher }
705644570b8SJeff Kirsher 
ne2k_pci_suspend(struct device * dev_d)70633b7a252SVaibhav Gupta static int __maybe_unused ne2k_pci_suspend(struct device *dev_d)
707644570b8SJeff Kirsher {
70833b7a252SVaibhav Gupta 	struct net_device *dev = dev_get_drvdata(dev_d);
709644570b8SJeff Kirsher 
710644570b8SJeff Kirsher 	netif_device_detach(dev);
711644570b8SJeff Kirsher 
712644570b8SJeff Kirsher 	return 0;
713644570b8SJeff Kirsher }
714644570b8SJeff Kirsher 
ne2k_pci_resume(struct device * dev_d)71533b7a252SVaibhav Gupta static int __maybe_unused ne2k_pci_resume(struct device *dev_d)
716644570b8SJeff Kirsher {
71733b7a252SVaibhav Gupta 	struct net_device *dev = dev_get_drvdata(dev_d);
718644570b8SJeff Kirsher 
719644570b8SJeff Kirsher 	NS8390_init(dev, 1);
720644570b8SJeff Kirsher 	netif_device_attach(dev);
721644570b8SJeff Kirsher 
722644570b8SJeff Kirsher 	return 0;
723644570b8SJeff Kirsher }
724644570b8SJeff Kirsher 
72533b7a252SVaibhav Gupta static SIMPLE_DEV_PM_OPS(ne2k_pci_pm_ops, ne2k_pci_suspend, ne2k_pci_resume);
726644570b8SJeff Kirsher 
727644570b8SJeff Kirsher static struct pci_driver ne2k_driver = {
728644570b8SJeff Kirsher 	.name		= DRV_NAME,
729644570b8SJeff Kirsher 	.probe		= ne2k_pci_init_one,
7304168ac0eSBill Pemberton 	.remove		= ne2k_pci_remove_one,
731644570b8SJeff Kirsher 	.id_table	= ne2k_pci_tbl,
73233b7a252SVaibhav Gupta 	.driver.pm	= &ne2k_pci_pm_ops,
733644570b8SJeff Kirsher };
734*5604ac35SYang Yingliang module_pci_driver(ne2k_driver);
735