1 /* A Linux device driver for PCI NE2000 clones.
2  *
3  * Authors and other copyright holders:
4  * 1992-2000 by Donald Becker, NE2000 core and various modifications.
5  * 1995-1998 by Paul Gortmaker, core modifications and PCI support.
6  * Copyright 1993 assigned to the United States Government as represented
7  * by the Director, National Security Agency.
8  *
9  * This software may be used and distributed according to the terms of
10  * the GNU General Public License (GPL), incorporated herein by reference.
11  * Drivers based on or derived from this code fall under the GPL and must
12  * retain the authorship, copyright and license notice.  This file is not
13  * a complete program and may only be used when the entire operating
14  * system is licensed under the GPL.
15  *
16  * The author may be reached as becker@scyld.com, or C/O
17  * Scyld Computing Corporation
18  * 410 Severn Ave., Suite 210
19  * Annapolis MD 21403
20  *
21  * Issues remaining:
22  * People are making PCI NE2000 clones! Oh the horror, the horror...
23  * Limited full-duplex support.
24  */
25 
26 #define DRV_NAME	"ne2k-pci"
27 #define DRV_DESCRIPTION	"PCI NE2000 clone driver"
28 #define DRV_AUTHOR	"Donald Becker / Paul Gortmaker"
29 #define DRV_VERSION	"1.03"
30 #define DRV_RELDATE	"9/22/2003"
31 
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 
34 /* The user-configurable values.
35  * These may be modified when a driver module is loaded.
36  */
37 
38 /* More are supported, limit only on options */
39 #define MAX_UNITS 8
40 
41 /* Used to pass the full-duplex flag, etc. */
42 static int full_duplex[MAX_UNITS];
43 static int options[MAX_UNITS];
44 
45 /* Force a non std. amount of memory.  Units are 256 byte pages. */
46 /* #define PACKETBUF_MEMSIZE	0x40 */
47 
48 
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/errno.h>
52 #include <linux/pci.h>
53 #include <linux/init.h>
54 #include <linux/interrupt.h>
55 #include <linux/ethtool.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 
59 #include <linux/io.h>
60 #include <asm/irq.h>
61 #include <linux/uaccess.h>
62 
63 #include "8390.h"
64 
65 static u32 ne2k_msg_enable;
66 
67 #if defined(__powerpc__)
68 #define inl_le(addr)  le32_to_cpu(inl(addr))
69 #define inw_le(addr)  le16_to_cpu(inw(addr))
70 #endif
71 
72 MODULE_AUTHOR(DRV_AUTHOR);
73 MODULE_DESCRIPTION(DRV_DESCRIPTION);
74 MODULE_VERSION(DRV_VERSION);
75 MODULE_LICENSE("GPL");
76 
77 module_param_named(msg_enable, ne2k_msg_enable, uint, 0444);
78 module_param_array(options, int, NULL, 0);
79 module_param_array(full_duplex, int, NULL, 0);
80 MODULE_PARM_DESC(msg_enable, "Debug message level (see linux/netdevice.h for bitmap)");
81 MODULE_PARM_DESC(options, "Bit 5: full duplex");
82 MODULE_PARM_DESC(full_duplex, "full duplex setting(s) (1)");
83 
84 /* Some defines that people can play with if so inclined.
85  */
86 
87 /* Use 32 bit data-movement operations instead of 16 bit. */
88 #define USE_LONGIO
89 
90 /* Do we implement the read before write bugfix ? */
91 /* #define NE_RW_BUGFIX */
92 
93 /* Flags.  We rename an existing ei_status field to store flags!
94  * Thus only the low 8 bits are usable for non-init-time flags.
95  */
96 #define ne2k_flags reg0
97 
98 enum {
99 	/* Chip can do only 16/32-bit xfers. */
100 	ONLY_16BIT_IO = 8, ONLY_32BIT_IO = 4,
101 	/* User override. */
102 	FORCE_FDX = 0x20,
103 	REALTEK_FDX = 0x40, HOLTEK_FDX = 0x80,
104 	STOP_PG_0x60 = 0x100,
105 };
106 
107 enum ne2k_pci_chipsets {
108 	CH_RealTek_RTL_8029 = 0,
109 	CH_Winbond_89C940,
110 	CH_Compex_RL2000,
111 	CH_KTI_ET32P2,
112 	CH_NetVin_NV5000SC,
113 	CH_Via_86C926,
114 	CH_SureCom_NE34,
115 	CH_Winbond_W89C940F,
116 	CH_Holtek_HT80232,
117 	CH_Holtek_HT80229,
118 	CH_Winbond_89C940_8c4a,
119 };
120 
121 
122 static struct {
123 	char *name;
124 	int flags;
125 } pci_clone_list[] = {
126 	{"RealTek RTL-8029(AS)", REALTEK_FDX},
127 	{"Winbond 89C940", 0},
128 	{"Compex RL2000", 0},
129 	{"KTI ET32P2", 0},
130 	{"NetVin NV5000SC", 0},
131 	{"Via 86C926", ONLY_16BIT_IO},
132 	{"SureCom NE34", 0},
133 	{"Winbond W89C940F", 0},
134 	{"Holtek HT80232", ONLY_16BIT_IO | HOLTEK_FDX},
135 	{"Holtek HT80229", ONLY_32BIT_IO | HOLTEK_FDX | STOP_PG_0x60 },
136 	{"Winbond W89C940(misprogrammed)", 0},
137 	{NULL,}
138 };
139 
140 
141 static const struct pci_device_id ne2k_pci_tbl[] = {
142 	{ 0x10ec, 0x8029, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RealTek_RTL_8029 },
143 	{ 0x1050, 0x0940, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_89C940 },
144 	{ 0x11f6, 0x1401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Compex_RL2000 },
145 	{ 0x8e2e, 0x3000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_KTI_ET32P2 },
146 	{ 0x4a14, 0x5000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_NetVin_NV5000SC },
147 	{ 0x1106, 0x0926, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Via_86C926 },
148 	{ 0x10bd, 0x0e34, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_SureCom_NE34 },
149 	{ 0x1050, 0x5a5a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_W89C940F },
150 	{ 0x12c3, 0x0058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Holtek_HT80232 },
151 	{ 0x12c3, 0x5598, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Holtek_HT80229 },
152 	{ 0x8c4a, 0x1980, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_89C940_8c4a },
153 	{ 0, }
154 };
155 
156 MODULE_DEVICE_TABLE(pci, ne2k_pci_tbl);
157 
158 
159 /* ---- No user-serviceable parts below ---- */
160 
161 #define NE_BASE	 (dev->base_addr)
162 #define NE_CMD		0x00
163 #define NE_DATAPORT	0x10	/* NatSemi-defined port window offset. */
164 #define NE_RESET	0x1f	/* Issue a read to reset, a write to clear. */
165 #define NE_IO_EXTENT	0x20
166 
167 #define NESM_START_PG	0x40	/* First page of TX buffer */
168 #define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
169 
170 
171 static int ne2k_pci_open(struct net_device *dev);
172 static int ne2k_pci_close(struct net_device *dev);
173 
174 static void ne2k_pci_reset_8390(struct net_device *dev);
175 static void ne2k_pci_get_8390_hdr(struct net_device *dev,
176 				  struct e8390_pkt_hdr *hdr, int ring_page);
177 static void ne2k_pci_block_input(struct net_device *dev, int count,
178 				 struct sk_buff *skb, int ring_offset);
179 static void ne2k_pci_block_output(struct net_device *dev, const int count,
180 				  const unsigned char *buf,
181 				  const int start_page);
182 static const struct ethtool_ops ne2k_pci_ethtool_ops;
183 
184 
185 
186 /* There is no room in the standard 8390 structure for extra info we need,
187  * so we build a meta/outer-wrapper structure..
188  */
189 struct ne2k_pci_card {
190 	struct net_device *dev;
191 	struct pci_dev *pci_dev;
192 };
193 
194 
195 
196 /* NEx000-clone boards have a Station Address (SA) PROM (SAPROM) in the packet
197  * buffer memory space.  By-the-spec NE2000 clones have 0x57,0x57 in bytes
198  * 0x0e,0x0f of the SAPROM, while other supposed NE2000 clones must be
199  * detected by their SA prefix.
200  *
201  * Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
202  * mode results in doubled values, which can be detected and compensated for.
203  *
204  * The probe is also responsible for initializing the card and filling
205  * in the 'dev' and 'ei_status' structures.
206  */
207 
208 static const struct net_device_ops ne2k_netdev_ops = {
209 	.ndo_open		= ne2k_pci_open,
210 	.ndo_stop		= ne2k_pci_close,
211 	.ndo_start_xmit		= ei_start_xmit,
212 	.ndo_tx_timeout		= ei_tx_timeout,
213 	.ndo_get_stats		= ei_get_stats,
214 	.ndo_set_rx_mode	= ei_set_multicast_list,
215 	.ndo_validate_addr	= eth_validate_addr,
216 	.ndo_set_mac_address	= eth_mac_addr,
217 #ifdef CONFIG_NET_POLL_CONTROLLER
218 	.ndo_poll_controller = ei_poll,
219 #endif
220 };
221 
222 static int ne2k_pci_init_one(struct pci_dev *pdev,
223 			     const struct pci_device_id *ent)
224 {
225 	struct net_device *dev;
226 	int i;
227 	unsigned char SA_prom[32];
228 	int start_page, stop_page;
229 	int irq, reg0, chip_idx = ent->driver_data;
230 	static unsigned int fnd_cnt;
231 	long ioaddr;
232 	int flags = pci_clone_list[chip_idx].flags;
233 	struct ei_device *ei_local;
234 
235 	fnd_cnt++;
236 
237 	i = pci_enable_device(pdev);
238 	if (i)
239 		return i;
240 
241 	ioaddr = pci_resource_start(pdev, 0);
242 	irq = pdev->irq;
243 
244 	if (!ioaddr || ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) == 0)) {
245 		dev_err(&pdev->dev, "no I/O resource at PCI BAR #0\n");
246 		goto err_out;
247 	}
248 
249 	if (!request_region(ioaddr, NE_IO_EXTENT, DRV_NAME)) {
250 		dev_err(&pdev->dev, "I/O resource 0x%x @ 0x%lx busy\n",
251 			NE_IO_EXTENT, ioaddr);
252 		goto err_out;
253 	}
254 
255 	reg0 = inb(ioaddr);
256 	if (reg0 == 0xFF)
257 		goto err_out_free_res;
258 
259 	/* Do a preliminary verification that we have a 8390. */
260 	{
261 		int regd;
262 
263 		outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
264 		regd = inb(ioaddr + 0x0d);
265 		outb(0xff, ioaddr + 0x0d);
266 		outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
267 		/* Clear the counter by reading. */
268 		inb(ioaddr + EN0_COUNTER0);
269 		if (inb(ioaddr + EN0_COUNTER0) != 0) {
270 			outb(reg0, ioaddr);
271 			/*  Restore the old values. */
272 			outb(regd, ioaddr + 0x0d);
273 			goto err_out_free_res;
274 		}
275 	}
276 
277 	/* Allocate net_device, dev->priv; fill in 8390 specific dev fields. */
278 	dev = alloc_ei_netdev();
279 	if (!dev) {
280 		dev_err(&pdev->dev, "cannot allocate ethernet device\n");
281 		goto err_out_free_res;
282 	}
283 	dev->netdev_ops = &ne2k_netdev_ops;
284 	ei_local = netdev_priv(dev);
285 	ei_local->msg_enable = ne2k_msg_enable;
286 
287 	SET_NETDEV_DEV(dev, &pdev->dev);
288 
289 	/* Reset card. Who knows what dain-bramaged state it was left in. */
290 	{
291 		unsigned long reset_start_time = jiffies;
292 
293 		outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
294 
295 		/* This looks like a horrible timing loop, but it should never
296 		 * take more than a few cycles.
297 		 */
298 		while ((inb(ioaddr + EN0_ISR) & ENISR_RESET) == 0)
299 			/* Limit wait: '2' avoids jiffy roll-over. */
300 			if (jiffies - reset_start_time > 2) {
301 				dev_err(&pdev->dev,
302 					"Card failure (no reset ack).\n");
303 				goto err_out_free_netdev;
304 			}
305 		/* Ack all intr. */
306 		outb(0xff, ioaddr + EN0_ISR);
307 	}
308 
309 	/* Read the 16 bytes of station address PROM.
310 	 * We must first initialize registers, similar
311 	 * to NS8390_init(eifdev, 0).
312 	 * We can't reliably read the SAPROM address without this.
313 	 * (I learned the hard way!).
314 	 */
315 	{
316 		struct {unsigned char value, offset; } program_seq[] = {
317 			/* Select page 0 */
318 			{E8390_NODMA + E8390_PAGE0 + E8390_STOP, E8390_CMD},
319 			/* Set word-wide access */
320 			{0x49,	EN0_DCFG},
321 			/* Clear the count regs. */
322 			{0x00,	EN0_RCNTLO},
323 			/* Mask completion IRQ */
324 			{0x00,	EN0_RCNTHI},
325 			{0x00,	EN0_IMR},
326 			{0xFF,	EN0_ISR},
327 			/* 0x20 Set to monitor */
328 			{E8390_RXOFF, EN0_RXCR},
329 			/* 0x02 and loopback mode */
330 			{E8390_TXOFF, EN0_TXCR},
331 			{32,	EN0_RCNTLO},
332 			{0x00,	EN0_RCNTHI},
333 			/* DMA starting at 0x0000 */
334 			{0x00,	EN0_RSARLO},
335 			{0x00,	EN0_RSARHI},
336 			{E8390_RREAD+E8390_START, E8390_CMD},
337 		};
338 		for (i = 0; i < ARRAY_SIZE(program_seq); i++)
339 			outb(program_seq[i].value,
340 			     ioaddr + program_seq[i].offset);
341 
342 	}
343 
344 	/* Note: all PCI cards have at least 16 bit access, so we don't have
345 	 * to check for 8 bit cards.  Most cards permit 32 bit access.
346 	 */
347 	if (flags & ONLY_32BIT_IO) {
348 		for (i = 0; i < 4 ; i++)
349 			((u32 *)SA_prom)[i] = le32_to_cpu(inl(ioaddr + NE_DATAPORT));
350 	} else
351 		for (i = 0; i < 32 /* sizeof(SA_prom )*/; i++)
352 			SA_prom[i] = inb(ioaddr + NE_DATAPORT);
353 
354 	/* We always set the 8390 registers for word mode. */
355 	outb(0x49, ioaddr + EN0_DCFG);
356 	start_page = NESM_START_PG;
357 
358 	stop_page = flags & STOP_PG_0x60 ? 0x60 : NESM_STOP_PG;
359 
360 	/* Set up the rest of the parameters. */
361 	dev->irq = irq;
362 	dev->base_addr = ioaddr;
363 	pci_set_drvdata(pdev, dev);
364 
365 	ei_status.name = pci_clone_list[chip_idx].name;
366 	ei_status.tx_start_page = start_page;
367 	ei_status.stop_page = stop_page;
368 	ei_status.word16 = 1;
369 	ei_status.ne2k_flags = flags;
370 	if (fnd_cnt < MAX_UNITS) {
371 		if (full_duplex[fnd_cnt] > 0 || (options[fnd_cnt] & FORCE_FDX))
372 			ei_status.ne2k_flags |= FORCE_FDX;
373 	}
374 
375 	ei_status.rx_start_page = start_page + TX_PAGES;
376 #ifdef PACKETBUF_MEMSIZE
377 	/* Allow the packet buffer size to be overridden by know-it-alls. */
378 	ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
379 #endif
380 
381 	ei_status.reset_8390 = &ne2k_pci_reset_8390;
382 	ei_status.block_input = &ne2k_pci_block_input;
383 	ei_status.block_output = &ne2k_pci_block_output;
384 	ei_status.get_8390_hdr = &ne2k_pci_get_8390_hdr;
385 	ei_status.priv = (unsigned long) pdev;
386 
387 	dev->ethtool_ops = &ne2k_pci_ethtool_ops;
388 	NS8390_init(dev, 0);
389 
390 	memcpy(dev->dev_addr, SA_prom, dev->addr_len);
391 
392 	i = register_netdev(dev);
393 	if (i)
394 		goto err_out_free_netdev;
395 
396 	netdev_info(dev, "%s found at %#lx, IRQ %d, %pM.\n",
397 		    pci_clone_list[chip_idx].name, ioaddr, dev->irq,
398 		    dev->dev_addr);
399 
400 	return 0;
401 
402 err_out_free_netdev:
403 	free_netdev(dev);
404 err_out_free_res:
405 	release_region(ioaddr, NE_IO_EXTENT);
406 err_out:
407 	pci_disable_device(pdev);
408 	return -ENODEV;
409 }
410 
411 /* Magic incantation sequence for full duplex on the supported cards.
412  */
413 static inline int set_realtek_fdx(struct net_device *dev)
414 {
415 	long ioaddr = dev->base_addr;
416 
417 	outb(0xC0 + E8390_NODMA, ioaddr + NE_CMD); /* Page 3 */
418 	outb(0xC0, ioaddr + 0x01); /* Enable writes to CONFIG3 */
419 	outb(0x40, ioaddr + 0x06); /* Enable full duplex */
420 	outb(0x00, ioaddr + 0x01); /* Disable writes to CONFIG3 */
421 	outb(E8390_PAGE0 + E8390_NODMA, ioaddr + NE_CMD); /* Page 0 */
422 	return 0;
423 }
424 
425 static inline int set_holtek_fdx(struct net_device *dev)
426 {
427 	long ioaddr = dev->base_addr;
428 
429 	outb(inb(ioaddr + 0x20) | 0x80, ioaddr + 0x20);
430 	return 0;
431 }
432 
433 static int ne2k_pci_set_fdx(struct net_device *dev)
434 {
435 	if (ei_status.ne2k_flags & REALTEK_FDX)
436 		return set_realtek_fdx(dev);
437 	else if (ei_status.ne2k_flags & HOLTEK_FDX)
438 		return set_holtek_fdx(dev);
439 
440 	return -EOPNOTSUPP;
441 }
442 
443 static int ne2k_pci_open(struct net_device *dev)
444 {
445 	int ret = request_irq(dev->irq, ei_interrupt, IRQF_SHARED,
446 			      dev->name, dev);
447 
448 	if (ret)
449 		return ret;
450 
451 	if (ei_status.ne2k_flags & FORCE_FDX)
452 		ne2k_pci_set_fdx(dev);
453 
454 	ei_open(dev);
455 	return 0;
456 }
457 
458 static int ne2k_pci_close(struct net_device *dev)
459 {
460 	ei_close(dev);
461 	free_irq(dev->irq, dev);
462 	return 0;
463 }
464 
465 /* Hard reset the card.  This used to pause for the same period that a
466  * 8390 reset command required, but that shouldn't be necessary.
467  */
468 static void ne2k_pci_reset_8390(struct net_device *dev)
469 {
470 	unsigned long reset_start_time = jiffies;
471 	struct ei_device *ei_local = netdev_priv(dev);
472 
473 	netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n",
474 		  jiffies);
475 
476 	outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
477 
478 	ei_status.txing = 0;
479 	ei_status.dmaing = 0;
480 
481 	/* This check _should_not_ be necessary, omit eventually. */
482 	while ((inb(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
483 		if (jiffies - reset_start_time > 2) {
484 			netdev_err(dev, "%s did not complete.\n", __func__);
485 			break;
486 		}
487 	/* Ack intr. */
488 	outb(ENISR_RESET, NE_BASE + EN0_ISR);
489 }
490 
491 /* Grab the 8390 specific header. Similar to the block_input routine, but
492  * we don't need to be concerned with ring wrap as the header will be at
493  * the start of a page, so we optimize accordingly.
494  */
495 
496 static void ne2k_pci_get_8390_hdr(struct net_device *dev,
497 				  struct e8390_pkt_hdr *hdr, int ring_page)
498 {
499 
500 	long nic_base = dev->base_addr;
501 
502 	/* This *shouldn't* happen. If it does, it's the last thing you'll see
503 	 */
504 	if (ei_status.dmaing) {
505 		netdev_err(dev, "DMAing conflict in %s [DMAstat:%d][irqlock:%d].\n",
506 			   __func__, ei_status.dmaing, ei_status.irqlock);
507 		return;
508 	}
509 
510 	ei_status.dmaing |= 0x01;
511 	outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
512 	outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
513 	outb(0, nic_base + EN0_RCNTHI);
514 	outb(0, nic_base + EN0_RSARLO);		/* On page boundary */
515 	outb(ring_page, nic_base + EN0_RSARHI);
516 	outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
517 
518 	if (ei_status.ne2k_flags & ONLY_16BIT_IO) {
519 		insw(NE_BASE + NE_DATAPORT, hdr,
520 		     sizeof(struct e8390_pkt_hdr) >> 1);
521 	} else {
522 		*(u32 *)hdr = le32_to_cpu(inl(NE_BASE + NE_DATAPORT));
523 		le16_to_cpus(&hdr->count);
524 	}
525 	/* Ack intr. */
526 	outb(ENISR_RDC, nic_base + EN0_ISR);
527 	ei_status.dmaing &= ~0x01;
528 }
529 
530 /* Block input and output, similar to the Crynwr packet driver.  If you
531  *are porting to a new ethercard, look at the packet driver source for hints.
532  *The NEx000 doesn't share the on-board packet memory -- you have to put
533  *the packet out through the "remote DMA" dataport using outb.
534  */
535 
536 static void ne2k_pci_block_input(struct net_device *dev, int count,
537 				 struct sk_buff *skb, int ring_offset)
538 {
539 	long nic_base = dev->base_addr;
540 	char *buf = skb->data;
541 
542 	/* This *shouldn't* happen.
543 	 * If it does, it's the last thing you'll see.
544 	 */
545 	if (ei_status.dmaing) {
546 		netdev_err(dev, "DMAing conflict in %s [DMAstat:%d][irqlock:%d]\n",
547 			   __func__, ei_status.dmaing, ei_status.irqlock);
548 		return;
549 	}
550 	ei_status.dmaing |= 0x01;
551 	if (ei_status.ne2k_flags & ONLY_32BIT_IO)
552 		count = (count + 3) & 0xFFFC;
553 	outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
554 	outb(count & 0xff, nic_base + EN0_RCNTLO);
555 	outb(count >> 8, nic_base + EN0_RCNTHI);
556 	outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
557 	outb(ring_offset >> 8, nic_base + EN0_RSARHI);
558 	outb(E8390_RREAD + E8390_START, nic_base + NE_CMD);
559 
560 	if (ei_status.ne2k_flags & ONLY_16BIT_IO) {
561 		insw(NE_BASE + NE_DATAPORT, buf, count >> 1);
562 		if (count & 0x01)
563 			buf[count-1] = inb(NE_BASE + NE_DATAPORT);
564 	} else {
565 		insl(NE_BASE + NE_DATAPORT, buf, count >> 2);
566 		if (count & 3) {
567 			buf += count & ~3;
568 			if (count & 2) {
569 				__le16 *b = (__le16 *)buf;
570 
571 				*b++ = cpu_to_le16(inw(NE_BASE + NE_DATAPORT));
572 				buf = (char *)b;
573 			}
574 			if (count & 1)
575 				*buf = inb(NE_BASE + NE_DATAPORT);
576 		}
577 	}
578 	/* Ack intr. */
579 	outb(ENISR_RDC, nic_base + EN0_ISR);
580 	ei_status.dmaing &= ~0x01;
581 }
582 
583 static void ne2k_pci_block_output(struct net_device *dev, int count,
584 		const unsigned char *buf, const int start_page)
585 {
586 	long nic_base = NE_BASE;
587 	unsigned long dma_start;
588 
589 	/* On little-endian it's always safe to round the count up for
590 	 * word writes.
591 	 */
592 	if (ei_status.ne2k_flags & ONLY_32BIT_IO)
593 		count = (count + 3) & 0xFFFC;
594 	else
595 		if (count & 0x01)
596 			count++;
597 
598 	/* This *shouldn't* happen.
599 	 * If it does, it's the last thing you'll see.
600 	 */
601 	if (ei_status.dmaing) {
602 		netdev_err(dev, "DMAing conflict in %s [DMAstat:%d][irqlock:%d]\n",
603 			   __func__, ei_status.dmaing, ei_status.irqlock);
604 		return;
605 	}
606 	ei_status.dmaing |= 0x01;
607 	/* We should already be in page 0, but to be safe... */
608 	outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
609 
610 #ifdef NE8390_RW_BUGFIX
611 	/* Handle the read-before-write bug the same way as the
612 	 * Crynwr packet driver -- the NatSemi method doesn't work.
613 	 * Actually this doesn't always work either, but if you have
614 	 * problems with your NEx000 this is better than nothing!
615 	 */
616 	outb(0x42, nic_base + EN0_RCNTLO);
617 	outb(0x00, nic_base + EN0_RCNTHI);
618 	outb(0x42, nic_base + EN0_RSARLO);
619 	outb(0x00, nic_base + EN0_RSARHI);
620 	outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
621 #endif
622 	outb(ENISR_RDC, nic_base + EN0_ISR);
623 
624 	/* Now the normal output. */
625 	outb(count & 0xff, nic_base + EN0_RCNTLO);
626 	outb(count >> 8,   nic_base + EN0_RCNTHI);
627 	outb(0x00, nic_base + EN0_RSARLO);
628 	outb(start_page, nic_base + EN0_RSARHI);
629 	outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
630 	if (ei_status.ne2k_flags & ONLY_16BIT_IO) {
631 		outsw(NE_BASE + NE_DATAPORT, buf, count >> 1);
632 	} else {
633 		outsl(NE_BASE + NE_DATAPORT, buf, count >> 2);
634 		if (count & 3) {
635 			buf += count & ~3;
636 			if (count & 2) {
637 				__le16 *b = (__le16 *)buf;
638 
639 				outw(le16_to_cpu(*b++), NE_BASE + NE_DATAPORT);
640 				buf = (char *)b;
641 			}
642 		}
643 	}
644 
645 	dma_start = jiffies;
646 
647 	while ((inb(nic_base + EN0_ISR) & ENISR_RDC) == 0)
648 		/* Avoid clock roll-over. */
649 		if (jiffies - dma_start > 2) {
650 			netdev_warn(dev, "timeout waiting for Tx RDC.\n");
651 			ne2k_pci_reset_8390(dev);
652 			NS8390_init(dev, 1);
653 			break;
654 		}
655 	/* Ack intr. */
656 	outb(ENISR_RDC, nic_base + EN0_ISR);
657 	ei_status.dmaing &= ~0x01;
658 }
659 
660 static void ne2k_pci_get_drvinfo(struct net_device *dev,
661 				 struct ethtool_drvinfo *info)
662 {
663 	struct ei_device *ei = netdev_priv(dev);
664 	struct pci_dev *pci_dev = (struct pci_dev *) ei->priv;
665 
666 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
667 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
668 	strscpy(info->bus_info, pci_name(pci_dev), sizeof(info->bus_info));
669 }
670 
671 static u32 ne2k_pci_get_msglevel(struct net_device *dev)
672 {
673 	struct ei_device *ei_local = netdev_priv(dev);
674 
675 	return ei_local->msg_enable;
676 }
677 
678 static void ne2k_pci_set_msglevel(struct net_device *dev, u32 v)
679 {
680 	struct ei_device *ei_local = netdev_priv(dev);
681 
682 	ei_local->msg_enable = v;
683 }
684 
685 static const struct ethtool_ops ne2k_pci_ethtool_ops = {
686 	.get_drvinfo		= ne2k_pci_get_drvinfo,
687 	.get_msglevel		= ne2k_pci_get_msglevel,
688 	.set_msglevel		= ne2k_pci_set_msglevel,
689 };
690 
691 static void ne2k_pci_remove_one(struct pci_dev *pdev)
692 {
693 	struct net_device *dev = pci_get_drvdata(pdev);
694 
695 	BUG_ON(!dev);
696 	unregister_netdev(dev);
697 	release_region(dev->base_addr, NE_IO_EXTENT);
698 	free_netdev(dev);
699 	pci_disable_device(pdev);
700 }
701 
702 #ifdef CONFIG_PM
703 static int ne2k_pci_suspend(struct pci_dev *pdev, pm_message_t state)
704 {
705 	struct net_device *dev = pci_get_drvdata(pdev);
706 
707 	netif_device_detach(dev);
708 	pci_save_state(pdev);
709 	pci_disable_device(pdev);
710 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
711 
712 	return 0;
713 }
714 
715 static int ne2k_pci_resume(struct pci_dev *pdev)
716 {
717 	struct net_device *dev = pci_get_drvdata(pdev);
718 	int rc;
719 
720 	pci_set_power_state(pdev, PCI_D0);
721 	pci_restore_state(pdev);
722 
723 	rc = pci_enable_device(pdev);
724 	if (rc)
725 		return rc;
726 
727 	NS8390_init(dev, 1);
728 	netif_device_attach(dev);
729 
730 	return 0;
731 }
732 
733 #endif /* CONFIG_PM */
734 
735 
736 static struct pci_driver ne2k_driver = {
737 	.name		= DRV_NAME,
738 	.probe		= ne2k_pci_init_one,
739 	.remove		= ne2k_pci_remove_one,
740 	.id_table	= ne2k_pci_tbl,
741 #ifdef CONFIG_PM
742 	.suspend	= ne2k_pci_suspend,
743 	.resume		= ne2k_pci_resume,
744 #endif
745 
746 };
747 
748 
749 static int __init ne2k_pci_init(void)
750 {
751 	return pci_register_driver(&ne2k_driver);
752 }
753 
754 
755 static void __exit ne2k_pci_cleanup(void)
756 {
757 	pci_unregister_driver(&ne2k_driver);
758 }
759 
760 module_init(ne2k_pci_init);
761 module_exit(ne2k_pci_cleanup);
762