Linux Audio

Check our new training course

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