1/* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
2
3#define DRV_NAME	"3c509"
4#define DRV_VERSION	"1.19b"
5#define DRV_RELDATE	"08Nov2002"
6
7/* A few values that may be tweaked. */
8
9/* Time in jiffies before concluding the transmitter is hung. */
10#define TX_TIMEOUT  (400*HZ/1000)
11/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
12static int max_interrupt_work = 10;
13
14#include <linux/module.h>
15#ifdef CONFIG_MCA
16#include <linux/mca.h>
17#endif
18#include <linux/isapnp.h>
19#include <linux/string.h>
20#include <linux/interrupt.h>
21#include <linux/errno.h>
22#include <linux/in.h>
23#include <linux/slab.h>
24#include <linux/ioport.h>
25#include <linux/init.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/pm.h>
29#include <linux/skbuff.h>
30#include <linux/delay.h>	/* for udelay() */
31#include <linux/spinlock.h>
32#include <linux/ethtool.h>
33#include <linux/device.h>
34#include <linux/eisa.h>
35#include <linux/bitops.h>
36
37#include <asm/uaccess.h>
38#include <asm/io.h>
39#include <asm/irq.h>
40
41static char version[] __initdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
42
43#if defined(CONFIG_PM) && (defined(CONFIG_MCA) || defined(CONFIG_EISA))
44#define EL3_SUSPEND
45#endif
46
47#ifdef EL3_DEBUG
48static int el3_debug = EL3_DEBUG;
49#else
50static int el3_debug = 2;
51#endif
52
53/* Used to do a global count of all the cards in the system.  Must be
54 * a global variable so that the mca/eisa probe routines can increment
55 * it */
56static int el3_cards = 0;
57
58/* To minimize the size of the driver source I only define operating
59   constants if they are used several times.  You'll need the manual
60   anyway if you want to understand driver details. */
61/* Offsets from base I/O address. */
62#define EL3_DATA 0x00
63#define EL3_CMD 0x0e
64#define EL3_STATUS 0x0e
65#define	 EEPROM_READ 0x80
66
67#define EL3_IO_EXTENT	16
68
69#define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
70
71
72/* The top five bits written to EL3_CMD are a command, the lower
73   11 bits are the parameter, if applicable. */
74enum c509cmd {
75	TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
76	RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
77	TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
78	FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
79	SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
80	SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
81	StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
82	PowerDown = 28<<11, PowerAuto = 29<<11};
83
84enum c509status {
85	IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
86	TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
87	IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };
88
89/* The SetRxFilter command accepts the following classes: */
90enum RxFilter {
91	RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };
92
93/* Register window 1 offsets, the window used in normal operation. */
94#define TX_FIFO		0x00
95#define RX_FIFO		0x00
96#define RX_STATUS 	0x08
97#define TX_STATUS 	0x0B
98#define TX_FREE		0x0C		/* Remaining free bytes in Tx buffer. */
99
100#define WN0_CONF_CTRL	0x04		/* Window 0: Configuration control register */
101#define WN0_ADDR_CONF	0x06		/* Window 0: Address configuration register */
102#define WN0_IRQ		0x08		/* Window 0: Set IRQ line in bits 12-15. */
103#define WN4_MEDIA	0x0A		/* Window 4: Various transcvr/media bits. */
104#define	MEDIA_TP	0x00C0		/* Enable link beat and jabber for 10baseT. */
105#define WN4_NETDIAG	0x06		/* Window 4: Net diagnostic */
106#define FD_ENABLE	0x8000		/* Enable full-duplex ("external loopback") */
107
108/*
109 * Must be a power of two (we use a binary and in the
110 * circular queue)
111 */
112#define SKB_QUEUE_SIZE	64
113
114struct el3_private {
115	struct net_device_stats stats;
116	struct net_device *next_dev;
117	spinlock_t lock;
118	/* skb send-queue */
119	int head, size;
120	struct sk_buff *queue[SKB_QUEUE_SIZE];
121	enum {
122		EL3_MCA,
123		EL3_PNP,
124		EL3_EISA,
125	} type;						/* type of device */
126	struct device *dev;
127};
128static int id_port __initdata = 0x110;	/* Start with 0x110 to avoid new sound cards.*/
129static struct net_device *el3_root_dev;
130
131static ushort id_read_eeprom(int index);
132static ushort read_eeprom(int ioaddr, int index);
133static int el3_open(struct net_device *dev);
134static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
135static irqreturn_t el3_interrupt(int irq, void *dev_id);
136static void update_stats(struct net_device *dev);
137static struct net_device_stats *el3_get_stats(struct net_device *dev);
138static int el3_rx(struct net_device *dev);
139static int el3_close(struct net_device *dev);
140static void set_multicast_list(struct net_device *dev);
141static void el3_tx_timeout (struct net_device *dev);
142static void el3_down(struct net_device *dev);
143static void el3_up(struct net_device *dev);
144static const struct ethtool_ops ethtool_ops;
145#ifdef EL3_SUSPEND
146static int el3_suspend(struct device *, pm_message_t);
147static int el3_resume(struct device *);
148#else
149#define el3_suspend NULL
150#define el3_resume NULL
151#endif
152
153
154/* generic device remove for all device types */
155#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
156static int el3_device_remove (struct device *device);
157#endif
158#ifdef CONFIG_NET_POLL_CONTROLLER
159static void el3_poll_controller(struct net_device *dev);
160#endif
161
162#ifdef CONFIG_EISA
163static struct eisa_device_id el3_eisa_ids[] = {
164		{ "TCM5092" },
165		{ "TCM5093" },
166		{ "TCM5095" },
167		{ "" }
168};
169MODULE_DEVICE_TABLE(eisa, el3_eisa_ids);
170
171static int el3_eisa_probe (struct device *device);
172
173static struct eisa_driver el3_eisa_driver = {
174		.id_table = el3_eisa_ids,
175		.driver   = {
176				.name    = "3c509",
177				.probe   = el3_eisa_probe,
178				.remove  = __devexit_p (el3_device_remove),
179				.suspend = el3_suspend,
180				.resume  = el3_resume,
181		}
182};
183#endif
184
185#ifdef CONFIG_MCA
186static int el3_mca_probe(struct device *dev);
187
188static short el3_mca_adapter_ids[] __initdata = {
189		0x627c,
190		0x627d,
191		0x62db,
192		0x62f6,
193		0x62f7,
194		0x0000
195};
196
197static char *el3_mca_adapter_names[] __initdata = {
198		"3Com 3c529 EtherLink III (10base2)",
199		"3Com 3c529 EtherLink III (10baseT)",
200		"3Com 3c529 EtherLink III (test mode)",
201		"3Com 3c529 EtherLink III (TP or coax)",
202		"3Com 3c529 EtherLink III (TP)",
203		NULL
204};
205
206static struct mca_driver el3_mca_driver = {
207		.id_table = el3_mca_adapter_ids,
208		.driver = {
209				.name = "3c529",
210				.bus = &mca_bus_type,
211				.probe = el3_mca_probe,
212				.remove = __devexit_p(el3_device_remove),
213				.suspend = el3_suspend,
214				.resume  = el3_resume,
215		},
216};
217#endif /* CONFIG_MCA */
218
219#if defined(__ISAPNP__)
220static struct isapnp_device_id el3_isapnp_adapters[] __initdata = {
221	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
222		ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5090),
223		(long) "3Com Etherlink III (TP)" },
224	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
225		ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5091),
226		(long) "3Com Etherlink III" },
227	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
228		ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5094),
229		(long) "3Com Etherlink III (combo)" },
230	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
231		ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5095),
232		(long) "3Com Etherlink III (TPO)" },
233	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
234		ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5098),
235		(long) "3Com Etherlink III (TPC)" },
236	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
237		ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f7),
238		(long) "3Com Etherlink III compatible" },
239	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
240		ISAPNP_VENDOR('P', 'N', 'P'), ISAPNP_FUNCTION(0x80f8),
241		(long) "3Com Etherlink III compatible" },
242	{ }	/* terminate list */
243};
244
245static u16 el3_isapnp_phys_addr[8][3];
246static int nopnp;
247#endif /* __ISAPNP__ */
248
249/* With the driver model introduction for EISA devices, both init
250 * and cleanup have been split :
251 * - EISA devices probe/remove starts in el3_eisa_probe/el3_device_remove
252 * - MCA/ISA still use el3_probe
253 *
254 * Both call el3_common_init/el3_common_remove. */
255
256static int __init el3_common_init(struct net_device *dev)
257{
258	struct el3_private *lp = netdev_priv(dev);
259	short i;
260	int err;
261
262	spin_lock_init(&lp->lock);
263
264	if (dev->mem_start & 0x05) { /* xcvr codes 1/3/4/12 */
265		dev->if_port = (dev->mem_start & 0x0f);
266	} else { /* xcvr codes 0/8 */
267		/* use eeprom value, but save user's full-duplex selection */
268		dev->if_port |= (dev->mem_start & 0x08);
269	}
270
271	/* The EL3-specific entries in the device structure. */
272	dev->open = &el3_open;
273	dev->hard_start_xmit = &el3_start_xmit;
274	dev->stop = &el3_close;
275	dev->get_stats = &el3_get_stats;
276	dev->set_multicast_list = &set_multicast_list;
277	dev->tx_timeout = el3_tx_timeout;
278	dev->watchdog_timeo = TX_TIMEOUT;
279#ifdef CONFIG_NET_POLL_CONTROLLER
280	dev->poll_controller = el3_poll_controller;
281#endif
282	SET_ETHTOOL_OPS(dev, &ethtool_ops);
283
284	err = register_netdev(dev);
285	if (err) {
286		printk(KERN_ERR "Failed to register 3c5x9 at %#3.3lx, IRQ %d.\n",
287			dev->base_addr, dev->irq);
288		release_region(dev->base_addr, EL3_IO_EXTENT);
289		return err;
290	}
291
292	{
293		const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
294		printk("%s: 3c5x9 found at %#3.3lx, %s port, address ",
295			dev->name, dev->base_addr,
296			if_names[(dev->if_port & 0x03)]);
297	}
298
299	/* Read in the station address. */
300	for (i = 0; i < 6; i++)
301		printk(" %2.2x", dev->dev_addr[i]);
302	printk(", IRQ %d.\n", dev->irq);
303
304	if (el3_debug > 0)
305		printk(KERN_INFO "%s", version);
306	return 0;
307
308}
309
310static void el3_common_remove (struct net_device *dev)
311{
312	struct el3_private *lp = netdev_priv(dev);
313
314	(void) lp;				/* Keep gcc quiet... */
315#if defined(__ISAPNP__)
316	if (lp->type == EL3_PNP)
317		pnp_device_detach(to_pnp_dev(lp->dev));
318#endif
319
320	unregister_netdev (dev);
321	release_region(dev->base_addr, EL3_IO_EXTENT);
322	free_netdev (dev);
323}
324
325static int __init el3_probe(int card_idx)
326{
327	struct net_device *dev;
328	struct el3_private *lp;
329	short lrs_state = 0xff, i;
330	int ioaddr, irq, if_port;
331	u16 phys_addr[3];
332	static int current_tag;
333	int err = -ENODEV;
334#if defined(__ISAPNP__)
335	static int pnp_cards;
336	struct pnp_dev *idev = NULL;
337
338	if (nopnp == 1)
339		goto no_pnp;
340
341	for (i=0; el3_isapnp_adapters[i].vendor != 0; i++) {
342		int j;
343		while ((idev = pnp_find_dev(NULL,
344					    el3_isapnp_adapters[i].vendor,
345					    el3_isapnp_adapters[i].function,
346					    idev))) {
347			if (pnp_device_attach(idev) < 0)
348				continue;
349			if (pnp_activate_dev(idev) < 0) {
350__again:
351				pnp_device_detach(idev);
352				continue;
353			}
354			if (!pnp_port_valid(idev, 0) || !pnp_irq_valid(idev, 0))
355				goto __again;
356			ioaddr = pnp_port_start(idev, 0);
357			if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509 PnP")) {
358				pnp_device_detach(idev);
359				return -EBUSY;
360			}
361			irq = pnp_irq(idev, 0);
362			if (el3_debug > 3)
363				printk ("ISAPnP reports %s at i/o 0x%x, irq %d\n",
364					(char*) el3_isapnp_adapters[i].driver_data, ioaddr, irq);
365			EL3WINDOW(0);
366			for (j = 0; j < 3; j++)
367				el3_isapnp_phys_addr[pnp_cards][j] =
368					phys_addr[j] =
369						htons(read_eeprom(ioaddr, j));
370			if_port = read_eeprom(ioaddr, 8) >> 14;
371			dev = alloc_etherdev(sizeof (struct el3_private));
372			if (!dev) {
373					release_region(ioaddr, EL3_IO_EXTENT);
374					pnp_device_detach(idev);
375					return -ENOMEM;
376			}
377
378			SET_MODULE_OWNER(dev);
379			SET_NETDEV_DEV(dev, &idev->dev);
380			pnp_cards++;
381
382			netdev_boot_setup_check(dev);
383			goto found;
384		}
385	}
386no_pnp:
387#endif /* __ISAPNP__ */
388
389	/* Select an open I/O location at 0x1*0 to do contention select. */
390	for ( ; id_port < 0x200; id_port += 0x10) {
391		if (!request_region(id_port, 1, "3c509"))
392			continue;
393		outb(0x00, id_port);
394		outb(0xff, id_port);
395		if (inb(id_port) & 0x01){
396			release_region(id_port, 1);
397			break;
398		} else
399			release_region(id_port, 1);
400	}
401	if (id_port >= 0x200) {
402		/* Rare -- do we really need a warning? */
403		printk(" WARNING: No I/O port available for 3c509 activation.\n");
404		return -ENODEV;
405	}
406
407	/* Next check for all ISA bus boards by sending the ID sequence to the
408	   ID_PORT.  We find cards past the first by setting the 'current_tag'
409	   on cards as they are found.  Cards with their tag set will not
410	   respond to subsequent ID sequences. */
411
412	outb(0x00, id_port);
413	outb(0x00, id_port);
414	for(i = 0; i < 255; i++) {
415		outb(lrs_state, id_port);
416		lrs_state <<= 1;
417		lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
418	}
419
420	/* For the first probe, clear all board's tag registers. */
421	if (current_tag == 0)
422		outb(0xd0, id_port);
423	else				/* Otherwise kill off already-found boards. */
424		outb(0xd8, id_port);
425
426	if (id_read_eeprom(7) != 0x6d50) {
427		return -ENODEV;
428	}
429
430	/* Read in EEPROM data, which does contention-select.
431	   Only the lowest address board will stay "on-line".
432	   3Com got the byte order backwards. */
433	for (i = 0; i < 3; i++) {
434		phys_addr[i] = htons(id_read_eeprom(i));
435	}
436
437#if defined(__ISAPNP__)
438	if (nopnp == 0) {
439		/* The ISA PnP 3c509 cards respond to the ID sequence.
440		   This check is needed in order not to register them twice. */
441		for (i = 0; i < pnp_cards; i++) {
442			if (phys_addr[0] == el3_isapnp_phys_addr[i][0] &&
443			    phys_addr[1] == el3_isapnp_phys_addr[i][1] &&
444			    phys_addr[2] == el3_isapnp_phys_addr[i][2])
445			{
446				if (el3_debug > 3)
447					printk("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
448						phys_addr[0] & 0xff, phys_addr[0] >> 8,
449						phys_addr[1] & 0xff, phys_addr[1] >> 8,
450						phys_addr[2] & 0xff, phys_addr[2] >> 8);
451				/* Set the adaptor tag so that the next card can be found. */
452				outb(0xd0 + ++current_tag, id_port);
453				goto no_pnp;
454			}
455		}
456	}
457#endif /* __ISAPNP__ */
458
459	{
460		unsigned int iobase = id_read_eeprom(8);
461		if_port = iobase >> 14;
462		ioaddr = 0x200 + ((iobase & 0x1f) << 4);
463	}
464	irq = id_read_eeprom(9) >> 12;
465
466	dev = alloc_etherdev(sizeof (struct el3_private));
467	if (!dev)
468		return -ENOMEM;
469
470	SET_MODULE_OWNER(dev);
471
472	netdev_boot_setup_check(dev);
473
474	/* Set passed-in IRQ or I/O Addr. */
475	if (dev->irq > 1  &&  dev->irq < 16)
476			irq = dev->irq;
477
478	if (dev->base_addr) {
479		if (dev->mem_end == 0x3c509 	/* Magic key */
480		    && dev->base_addr >= 0x200  &&  dev->base_addr <= 0x3e0)
481			ioaddr = dev->base_addr & 0x3f0;
482		else if (dev->base_addr != ioaddr)
483			goto out;
484	}
485
486	if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509")) {
487		err = -EBUSY;
488		goto out;
489	}
490
491	/* Set the adaptor tag so that the next card can be found. */
492	outb(0xd0 + ++current_tag, id_port);
493
494	/* Activate the adaptor at the EEPROM location. */
495	outb((ioaddr >> 4) | 0xe0, id_port);
496
497	EL3WINDOW(0);
498	if (inw(ioaddr) != 0x6d50)
499		goto out1;
500
501	/* Free the interrupt so that some other card can use it. */
502	outw(0x0f00, ioaddr + WN0_IRQ);
503
504#if defined(__ISAPNP__)
505 found:							/* PNP jumps here... */
506#endif /* __ISAPNP__ */
507
508	memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
509	dev->base_addr = ioaddr;
510	dev->irq = irq;
511	dev->if_port = if_port;
512	lp = netdev_priv(dev);
513#if defined(__ISAPNP__)
514	lp->dev = &idev->dev;
515#endif
516	err = el3_common_init(dev);
517
518	if (err)
519		goto out1;
520
521	el3_cards++;
522	lp->next_dev = el3_root_dev;
523	el3_root_dev = dev;
524	return 0;
525
526out1:
527#if defined(__ISAPNP__)
528	if (idev)
529		pnp_device_detach(idev);
530#endif
531out:
532	free_netdev(dev);
533	return err;
534}
535
536#ifdef CONFIG_MCA
537static int __init el3_mca_probe(struct device *device)
538{
539	/* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch,
540	 * heavily modified by Chris Beauregard
541	 * (cpbeaure@csclub.uwaterloo.ca) to support standard MCA
542	 * probing.
543	 *
544	 * redone for multi-card detection by ZP Gu (zpg@castle.net)
545	 * now works as a module */
546
547	struct el3_private *lp;
548	short i;
549	int ioaddr, irq, if_port;
550	u16 phys_addr[3];
551	struct net_device *dev = NULL;
552	u_char pos4, pos5;
553	struct mca_device *mdev = to_mca_device(device);
554	int slot = mdev->slot;
555	int err;
556
557	pos4 = mca_device_read_stored_pos(mdev, 4);
558	pos5 = mca_device_read_stored_pos(mdev, 5);
559
560	ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
561	irq = pos5 & 0x0f;
562
563
564	printk("3c529: found %s at slot %d\n",
565		   el3_mca_adapter_names[mdev->index], slot + 1);
566
567	/* claim the slot */
568	strncpy(mdev->name, el3_mca_adapter_names[mdev->index],
569			sizeof(mdev->name));
570	mca_device_set_claim(mdev, 1);
571
572	if_port = pos4 & 0x03;
573
574	irq = mca_device_transform_irq(mdev, irq);
575	ioaddr = mca_device_transform_ioport(mdev, ioaddr);
576	if (el3_debug > 2) {
577			printk("3c529: irq %d  ioaddr 0x%x  ifport %d\n", irq, ioaddr, if_port);
578	}
579	EL3WINDOW(0);
580	for (i = 0; i < 3; i++) {
581			phys_addr[i] = htons(read_eeprom(ioaddr, i));
582	}
583
584	dev = alloc_etherdev(sizeof (struct el3_private));
585	if (dev == NULL) {
586			release_region(ioaddr, EL3_IO_EXTENT);
587			return -ENOMEM;
588	}
589
590	SET_MODULE_OWNER(dev);
591	netdev_boot_setup_check(dev);
592
593	memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
594	dev->base_addr = ioaddr;
595	dev->irq = irq;
596	dev->if_port = if_port;
597	lp = netdev_priv(dev);
598	lp->dev = device;
599	lp->type = EL3_MCA;
600	device->driver_data = dev;
601	err = el3_common_init(dev);
602
603	if (err) {
604		device->driver_data = NULL;
605		free_netdev(dev);
606		return -ENOMEM;
607	}
608
609	el3_cards++;
610	return 0;
611}
612
613#endif /* CONFIG_MCA */
614
615#ifdef CONFIG_EISA
616static int __init el3_eisa_probe (struct device *device)
617{
618	struct el3_private *lp;
619	short i;
620	int ioaddr, irq, if_port;
621	u16 phys_addr[3];
622	struct net_device *dev = NULL;
623	struct eisa_device *edev;
624	int err;
625
626	/* Yeepee, The driver framework is calling us ! */
627	edev = to_eisa_device (device);
628	ioaddr = edev->base_addr;
629
630	if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509"))
631		return -EBUSY;
632
633	/* Change the register set to the configuration window 0. */
634	outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);
635
636	irq = inw(ioaddr + WN0_IRQ) >> 12;
637	if_port = inw(ioaddr + 6)>>14;
638	for (i = 0; i < 3; i++)
639		phys_addr[i] = htons(read_eeprom(ioaddr, i));
640
641	/* Restore the "Product ID" to the EEPROM read register. */
642	read_eeprom(ioaddr, 3);
643
644	dev = alloc_etherdev(sizeof (struct el3_private));
645	if (dev == NULL) {
646		release_region(ioaddr, EL3_IO_EXTENT);
647		return -ENOMEM;
648	}
649
650	SET_MODULE_OWNER(dev);
651
652	netdev_boot_setup_check(dev);
653
654	memcpy(dev->dev_addr, phys_addr, sizeof(phys_addr));
655	dev->base_addr = ioaddr;
656	dev->irq = irq;
657	dev->if_port = if_port;
658	lp = netdev_priv(dev);
659	lp->dev = device;
660	lp->type = EL3_EISA;
661	eisa_set_drvdata (edev, dev);
662	err = el3_common_init(dev);
663
664	if (err) {
665		eisa_set_drvdata (edev, NULL);
666		free_netdev(dev);
667		return err;
668	}
669
670	el3_cards++;
671	return 0;
672}
673#endif
674
675#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
676/* This remove works for all device types.
677 *
678 * The net dev must be stored in the driver_data field */
679static int __devexit el3_device_remove (struct device *device)
680{
681	struct net_device *dev;
682
683	dev  = device->driver_data;
684
685	el3_common_remove (dev);
686	return 0;
687}
688#endif
689
690/* Read a word from the EEPROM using the regular EEPROM access register.
691   Assume that we are in register window zero.
692 */
693static ushort read_eeprom(int ioaddr, int index)
694{
695	outw(EEPROM_READ + index, ioaddr + 10);
696	/* Pause for at least 162 us. for the read to take place.
697	   Some chips seem to require much longer */
698	mdelay(2);
699	return inw(ioaddr + 12);
700}
701
702/* Read a word from the EEPROM when in the ISA ID probe state. */
703static ushort __init id_read_eeprom(int index)
704{
705	int bit, word = 0;
706
707	/* Issue read command, and pause for at least 162 us. for it to complete.
708	   Assume extra-fast 16Mhz bus. */
709	outb(EEPROM_READ + index, id_port);
710
711	/* Pause for at least 162 us. for the read to take place. */
712	/* Some chips seem to require much longer */
713	mdelay(4);
714
715	for (bit = 15; bit >= 0; bit--)
716		word = (word << 1) + (inb(id_port) & 0x01);
717
718	if (el3_debug > 3)
719		printk("  3c509 EEPROM word %d %#4.4x.\n", index, word);
720
721	return word;
722}
723
724
725static int
726el3_open(struct net_device *dev)
727{
728	int ioaddr = dev->base_addr;
729	int i;
730
731	outw(TxReset, ioaddr + EL3_CMD);
732	outw(RxReset, ioaddr + EL3_CMD);
733	outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
734
735	i = request_irq(dev->irq, &el3_interrupt, 0, dev->name, dev);
736	if (i)
737		return i;
738
739	EL3WINDOW(0);
740	if (el3_debug > 3)
741		printk("%s: Opening, IRQ %d	 status@%x %4.4x.\n", dev->name,
742			   dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));
743
744	el3_up(dev);
745
746	if (el3_debug > 3)
747		printk("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
748			   dev->name, dev->irq, inw(ioaddr + EL3_STATUS));
749
750	return 0;
751}
752
753static void
754el3_tx_timeout (struct net_device *dev)
755{
756	struct el3_private *lp = netdev_priv(dev);
757	int ioaddr = dev->base_addr;
758
759	/* Transmitter timeout, serious problems. */
760	printk("%s: transmit timed out, Tx_status %2.2x status %4.4x "
761		   "Tx FIFO room %d.\n",
762		   dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
763		   inw(ioaddr + TX_FREE));
764	lp->stats.tx_errors++;
765	dev->trans_start = jiffies;
766	/* Issue TX_RESET and TX_START commands. */
767	outw(TxReset, ioaddr + EL3_CMD);
768	outw(TxEnable, ioaddr + EL3_CMD);
769	netif_wake_queue(dev);
770}
771
772
773static int
774el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
775{
776	struct el3_private *lp = netdev_priv(dev);
777	int ioaddr = dev->base_addr;
778	unsigned long flags;
779
780	netif_stop_queue (dev);
781
782	lp->stats.tx_bytes += skb->len;
783
784	if (el3_debug > 4) {
785		printk("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
786			   dev->name, skb->len, inw(ioaddr + EL3_STATUS));
787	}
788	/*
789	 *	We lock the driver against other processors. Note
790	 *	we don't need to lock versus the IRQ as we suspended
791	 *	that. This means that we lose the ability to take
792	 *	an RX during a TX upload. That sucks a bit with SMP
793	 *	on an original 3c509 (2K buffer)
794	 *
795	 *	Using disable_irq stops us crapping on other
796	 *	time sensitive devices.
797	 */
798
799	spin_lock_irqsave(&lp->lock, flags);
800
801	/* Put out the doubleword header... */
802	outw(skb->len, ioaddr + TX_FIFO);
803	outw(0x00, ioaddr + TX_FIFO);
804	/* ... and the packet rounded to a doubleword. */
805	outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
806
807	dev->trans_start = jiffies;
808	if (inw(ioaddr + TX_FREE) > 1536)
809		netif_start_queue(dev);
810	else
811		/* Interrupt us when the FIFO has room for max-sized packet. */
812		outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
813
814	spin_unlock_irqrestore(&lp->lock, flags);
815
816	dev_kfree_skb (skb);
817
818	/* Clear the Tx status stack. */
819	{
820		short tx_status;
821		int i = 4;
822
823		while (--i > 0	&&	(tx_status = inb(ioaddr + TX_STATUS)) > 0) {
824			if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
825			if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
826			if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
827			outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
828		}
829	}
830	return 0;
831}
832
833/* The EL3 interrupt handler. */
834static irqreturn_t
835el3_interrupt(int irq, void *dev_id)
836{
837	struct net_device *dev = dev_id;
838	struct el3_private *lp;
839	int ioaddr, status;
840	int i = max_interrupt_work;
841
842	lp = netdev_priv(dev);
843	spin_lock(&lp->lock);
844
845	ioaddr = dev->base_addr;
846
847	if (el3_debug > 4) {
848		status = inw(ioaddr + EL3_STATUS);
849		printk("%s: interrupt, status %4.4x.\n", dev->name, status);
850	}
851
852	while ((status = inw(ioaddr + EL3_STATUS)) &
853		   (IntLatch | RxComplete | StatsFull)) {
854
855		if (status & RxComplete)
856			el3_rx(dev);
857
858		if (status & TxAvailable) {
859			if (el3_debug > 5)
860				printk("	TX room bit was handled.\n");
861			/* There's room in the FIFO for a full-sized packet. */
862			outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
863			netif_wake_queue (dev);
864		}
865		if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
866			/* Handle all uncommon interrupts. */
867			if (status & StatsFull)				/* Empty statistics. */
868				update_stats(dev);
869			if (status & RxEarly) {				/* Rx early is unused. */
870				el3_rx(dev);
871				outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
872			}
873			if (status & TxComplete) {			/* Really Tx error. */
874				struct el3_private *lp = netdev_priv(dev);
875				short tx_status;
876				int i = 4;
877
878				while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
879					if (tx_status & 0x38) lp->stats.tx_aborted_errors++;
880					if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
881					if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
882					outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
883				}
884			}
885			if (status & AdapterFailure) {
886				/* Adapter failure requires Rx reset and reinit. */
887				outw(RxReset, ioaddr + EL3_CMD);
888				/* Set the Rx filter to the current state. */
889				outw(SetRxFilter | RxStation | RxBroadcast
890					 | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
891					 | (dev->flags & IFF_PROMISC ? RxProm : 0),
892					 ioaddr + EL3_CMD);
893				outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
894				outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
895			}
896		}
897
898		if (--i < 0) {
899			printk("%s: Infinite loop in interrupt, status %4.4x.\n",
900				   dev->name, status);
901			/* Clear all interrupts. */
902			outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
903			break;
904		}
905		/* Acknowledge the IRQ. */
906		outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
907	}
908
909	if (el3_debug > 4) {
910		printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
911			   inw(ioaddr + EL3_STATUS));
912	}
913	spin_unlock(&lp->lock);
914	return IRQ_HANDLED;
915}
916
917
918#ifdef CONFIG_NET_POLL_CONTROLLER
919/*
920 * Polling receive - used by netconsole and other diagnostic tools
921 * to allow network i/o with interrupts disabled.
922 */
923static void el3_poll_controller(struct net_device *dev)
924{
925	disable_irq(dev->irq);
926	el3_interrupt(dev->irq, dev);
927	enable_irq(dev->irq);
928}
929#endif
930
931static struct net_device_stats *
932el3_get_stats(struct net_device *dev)
933{
934	struct el3_private *lp = netdev_priv(dev);
935	unsigned long flags;
936
937	/*
938	 *	This is fast enough not to bother with disable IRQ
939	 *	stuff.
940	 */
941
942	spin_lock_irqsave(&lp->lock, flags);
943	update_stats(dev);
944	spin_unlock_irqrestore(&lp->lock, flags);
945	return &lp->stats;
946}
947
948/*  Update statistics.  We change to register window 6, so this should be run
949	single-threaded if the device is active. This is expected to be a rare
950	operation, and it's simpler for the rest of the driver to assume that
951	window 1 is always valid rather than use a special window-state variable.
952	*/
953static void update_stats(struct net_device *dev)
954{
955	struct el3_private *lp = netdev_priv(dev);
956	int ioaddr = dev->base_addr;
957
958	if (el3_debug > 5)
959		printk("   Updating the statistics.\n");
960	/* Turn off statistics updates while reading. */
961	outw(StatsDisable, ioaddr + EL3_CMD);
962	/* Switch to the stats window, and read everything. */
963	EL3WINDOW(6);
964	lp->stats.tx_carrier_errors 	+= inb(ioaddr + 0);
965	lp->stats.tx_heartbeat_errors	+= inb(ioaddr + 1);
966	/* Multiple collisions. */	   inb(ioaddr + 2);
967	lp->stats.collisions		+= inb(ioaddr + 3);
968	lp->stats.tx_window_errors	+= inb(ioaddr + 4);
969	lp->stats.rx_fifo_errors	+= inb(ioaddr + 5);
970	lp->stats.tx_packets		+= inb(ioaddr + 6);
971	/* Rx packets	*/		   inb(ioaddr + 7);
972	/* Tx deferrals */		   inb(ioaddr + 8);
973	inw(ioaddr + 10);	/* Total Rx and Tx octets. */
974	inw(ioaddr + 12);
975
976	/* Back to window 1, and turn statistics back on. */
977	EL3WINDOW(1);
978	outw(StatsEnable, ioaddr + EL3_CMD);
979	return;
980}
981
982static int
983el3_rx(struct net_device *dev)
984{
985	struct el3_private *lp = netdev_priv(dev);
986	int ioaddr = dev->base_addr;
987	short rx_status;
988
989	if (el3_debug > 5)
990		printk("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
991			   inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
992	while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
993		if (rx_status & 0x4000) { /* Error, update stats. */
994			short error = rx_status & 0x3800;
995
996			outw(RxDiscard, ioaddr + EL3_CMD);
997			lp->stats.rx_errors++;
998			switch (error) {
999			case 0x0000:		lp->stats.rx_over_errors++; break;
1000			case 0x0800:		lp->stats.rx_length_errors++; break;
1001			case 0x1000:		lp->stats.rx_frame_errors++; break;
1002			case 0x1800:		lp->stats.rx_length_errors++; break;
1003			case 0x2000:		lp->stats.rx_frame_errors++; break;
1004			case 0x2800:		lp->stats.rx_crc_errors++; break;
1005			}
1006		} else {
1007			short pkt_len = rx_status & 0x7ff;
1008			struct sk_buff *skb;
1009
1010			skb = dev_alloc_skb(pkt_len+5);
1011			lp->stats.rx_bytes += pkt_len;
1012			if (el3_debug > 4)
1013				printk("Receiving packet size %d status %4.4x.\n",
1014					   pkt_len, rx_status);
1015			if (skb != NULL) {
1016				skb_reserve(skb, 2);     /* Align IP on 16 byte */
1017
1018				/* 'skb->data' points to the start of sk_buff data area. */
1019				insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
1020					 (pkt_len + 3) >> 2);
1021
1022				outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
1023				skb->protocol = eth_type_trans(skb,dev);
1024				netif_rx(skb);
1025				dev->last_rx = jiffies;
1026				lp->stats.rx_packets++;
1027				continue;
1028			}
1029			outw(RxDiscard, ioaddr + EL3_CMD);
1030			lp->stats.rx_dropped++;
1031			if (el3_debug)
1032				printk("%s: Couldn't allocate a sk_buff of size %d.\n",
1033					   dev->name, pkt_len);
1034		}
1035		inw(ioaddr + EL3_STATUS); 				/* Delay. */
1036		while (inw(ioaddr + EL3_STATUS) & 0x1000)
1037			printk(KERN_DEBUG "	Waiting for 3c509 to discard packet, status %x.\n",
1038				   inw(ioaddr + EL3_STATUS) );
1039	}
1040
1041	return 0;
1042}
1043
1044/*
1045 *     Set or clear the multicast filter for this adaptor.
1046 */
1047static void
1048set_multicast_list(struct net_device *dev)
1049{
1050	unsigned long flags;
1051	struct el3_private *lp = netdev_priv(dev);
1052	int ioaddr = dev->base_addr;
1053
1054	if (el3_debug > 1) {
1055		static int old;
1056		if (old != dev->mc_count) {
1057			old = dev->mc_count;
1058			printk("%s: Setting Rx mode to %d addresses.\n", dev->name, dev->mc_count);
1059		}
1060	}
1061	spin_lock_irqsave(&lp->lock, flags);
1062	if (dev->flags&IFF_PROMISC) {
1063		outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1064			 ioaddr + EL3_CMD);
1065	}
1066	else if (dev->mc_count || (dev->flags&IFF_ALLMULTI)) {
1067		outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
1068	}
1069	else
1070		outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1071	spin_unlock_irqrestore(&lp->lock, flags);
1072}
1073
1074static int
1075el3_close(struct net_device *dev)
1076{
1077	int ioaddr = dev->base_addr;
1078	struct el3_private *lp = netdev_priv(dev);
1079
1080	if (el3_debug > 2)
1081		printk("%s: Shutting down ethercard.\n", dev->name);
1082
1083	el3_down(dev);
1084
1085	free_irq(dev->irq, dev);
1086	/* Switching back to window 0 disables the IRQ. */
1087	EL3WINDOW(0);
1088	if (lp->type != EL3_EISA) {
1089		/* But we explicitly zero the IRQ line select anyway. Don't do
1090		 * it on EISA cards, it prevents the module from getting an
1091		 * IRQ after unload+reload... */
1092		outw(0x0f00, ioaddr + WN0_IRQ);
1093	}
1094
1095	return 0;
1096}
1097
1098static int
1099el3_link_ok(struct net_device *dev)
1100{
1101	int ioaddr = dev->base_addr;
1102	u16 tmp;
1103
1104	EL3WINDOW(4);
1105	tmp = inw(ioaddr + WN4_MEDIA);
1106	EL3WINDOW(1);
1107	return tmp & (1<<11);
1108}
1109
1110static int
1111el3_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1112{
1113	u16 tmp;
1114	int ioaddr = dev->base_addr;
1115
1116	EL3WINDOW(0);
1117	/* obtain current transceiver via WN4_MEDIA? */
1118	tmp = inw(ioaddr + WN0_ADDR_CONF);
1119	ecmd->transceiver = XCVR_INTERNAL;
1120	switch (tmp >> 14) {
1121	case 0:
1122		ecmd->port = PORT_TP;
1123		break;
1124	case 1:
1125		ecmd->port = PORT_AUI;
1126		ecmd->transceiver = XCVR_EXTERNAL;
1127		break;
1128	case 3:
1129		ecmd->port = PORT_BNC;
1130	default:
1131		break;
1132	}
1133
1134	ecmd->duplex = DUPLEX_HALF;
1135	ecmd->supported = 0;
1136	tmp = inw(ioaddr + WN0_CONF_CTRL);
1137	if (tmp & (1<<13))
1138		ecmd->supported |= SUPPORTED_AUI;
1139	if (tmp & (1<<12))
1140		ecmd->supported |= SUPPORTED_BNC;
1141	if (tmp & (1<<9)) {
1142		ecmd->supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half |
1143				SUPPORTED_10baseT_Full;	/* hmm... */
1144		EL3WINDOW(4);
1145		tmp = inw(ioaddr + WN4_NETDIAG);
1146		if (tmp & FD_ENABLE)
1147			ecmd->duplex = DUPLEX_FULL;
1148	}
1149
1150	ecmd->speed = SPEED_10;
1151	EL3WINDOW(1);
1152	return 0;
1153}
1154
1155static int
1156el3_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1157{
1158	u16 tmp;
1159	int ioaddr = dev->base_addr;
1160
1161	if (ecmd->speed != SPEED_10)
1162		return -EINVAL;
1163	if ((ecmd->duplex != DUPLEX_HALF) && (ecmd->duplex != DUPLEX_FULL))
1164		return -EINVAL;
1165	if ((ecmd->transceiver != XCVR_INTERNAL) && (ecmd->transceiver != XCVR_EXTERNAL))
1166		return -EINVAL;
1167
1168	/* change XCVR type */
1169	EL3WINDOW(0);
1170	tmp = inw(ioaddr + WN0_ADDR_CONF);
1171	switch (ecmd->port) {
1172	case PORT_TP:
1173		tmp &= ~(3<<14);
1174		dev->if_port = 0;
1175		break;
1176	case PORT_AUI:
1177		tmp |= (1<<14);
1178		dev->if_port = 1;
1179		break;
1180	case PORT_BNC:
1181		tmp |= (3<<14);
1182		dev->if_port = 3;
1183		break;
1184	default:
1185		return -EINVAL;
1186	}
1187
1188	outw(tmp, ioaddr + WN0_ADDR_CONF);
1189	if (dev->if_port == 3) {
1190		/* fire up the DC-DC convertor if BNC gets enabled */
1191		tmp = inw(ioaddr + WN0_ADDR_CONF);
1192		if (tmp & (3 << 14)) {
1193			outw(StartCoax, ioaddr + EL3_CMD);
1194			udelay(800);
1195		} else
1196			return -EIO;
1197	}
1198
1199	EL3WINDOW(4);
1200	tmp = inw(ioaddr + WN4_NETDIAG);
1201	if (ecmd->duplex == DUPLEX_FULL)
1202		tmp |= FD_ENABLE;
1203	else
1204		tmp &= ~FD_ENABLE;
1205	outw(tmp, ioaddr + WN4_NETDIAG);
1206	EL3WINDOW(1);
1207
1208	return 0;
1209}
1210
1211static void el3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1212{
1213	strcpy(info->driver, DRV_NAME);
1214	strcpy(info->version, DRV_VERSION);
1215}
1216
1217static int el3_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1218{
1219	struct el3_private *lp = netdev_priv(dev);
1220	int ret;
1221
1222	spin_lock_irq(&lp->lock);
1223	ret = el3_netdev_get_ecmd(dev, ecmd);
1224	spin_unlock_irq(&lp->lock);
1225	return ret;
1226}
1227
1228static int el3_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1229{
1230	struct el3_private *lp = netdev_priv(dev);
1231	int ret;
1232
1233	spin_lock_irq(&lp->lock);
1234	ret = el3_netdev_set_ecmd(dev, ecmd);
1235	spin_unlock_irq(&lp->lock);
1236	return ret;
1237}
1238
1239static u32 el3_get_link(struct net_device *dev)
1240{
1241	struct el3_private *lp = netdev_priv(dev);
1242	u32 ret;
1243
1244	spin_lock_irq(&lp->lock);
1245	ret = el3_link_ok(dev);
1246	spin_unlock_irq(&lp->lock);
1247	return ret;
1248}
1249
1250static u32 el3_get_msglevel(struct net_device *dev)
1251{
1252	return el3_debug;
1253}
1254
1255static void el3_set_msglevel(struct net_device *dev, u32 v)
1256{
1257	el3_debug = v;
1258}
1259
1260static const struct ethtool_ops ethtool_ops = {
1261	.get_drvinfo = el3_get_drvinfo,
1262	.get_settings = el3_get_settings,
1263	.set_settings = el3_set_settings,
1264	.get_link = el3_get_link,
1265	.get_msglevel = el3_get_msglevel,
1266	.set_msglevel = el3_set_msglevel,
1267};
1268
1269static void
1270el3_down(struct net_device *dev)
1271{
1272	int ioaddr = dev->base_addr;
1273
1274	netif_stop_queue(dev);
1275
1276	/* Turn off statistics ASAP.  We update lp->stats below. */
1277	outw(StatsDisable, ioaddr + EL3_CMD);
1278
1279	/* Disable the receiver and transmitter. */
1280	outw(RxDisable, ioaddr + EL3_CMD);
1281	outw(TxDisable, ioaddr + EL3_CMD);
1282
1283	if (dev->if_port == 3)
1284		/* Turn off thinnet power.  Green! */
1285		outw(StopCoax, ioaddr + EL3_CMD);
1286	else if (dev->if_port == 0) {
1287		/* Disable link beat and jabber, if_port may change here next open(). */
1288		EL3WINDOW(4);
1289		outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
1290	}
1291
1292	outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1293
1294	update_stats(dev);
1295}
1296
1297static void
1298el3_up(struct net_device *dev)
1299{
1300	int i, sw_info, net_diag;
1301	int ioaddr = dev->base_addr;
1302
1303	/* Activating the board required and does no harm otherwise */
1304	outw(0x0001, ioaddr + 4);
1305
1306	/* Set the IRQ line. */
1307	outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);
1308
1309	/* Set the station address in window 2 each time opened. */
1310	EL3WINDOW(2);
1311
1312	for (i = 0; i < 6; i++)
1313		outb(dev->dev_addr[i], ioaddr + i);
1314
1315	if ((dev->if_port & 0x03) == 3) /* BNC interface */
1316		/* Start the thinnet transceiver. We should really wait 50ms...*/
1317		outw(StartCoax, ioaddr + EL3_CMD);
1318	else if ((dev->if_port & 0x03) == 0) { /* 10baseT interface */
1319		/* Combine secondary sw_info word (the adapter level) and primary
1320			sw_info word (duplex setting plus other useless bits) */
1321		EL3WINDOW(0);
1322		sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) |
1323			(read_eeprom(ioaddr, 0x0d) & 0xBff0);
1324
1325		EL3WINDOW(4);
1326		net_diag = inw(ioaddr + WN4_NETDIAG);
1327		net_diag = (net_diag | FD_ENABLE); /* temporarily assume full-duplex will be set */
1328		printk("%s: ", dev->name);
1329		switch (dev->if_port & 0x0c) {
1330			case 12:
1331				/* force full-duplex mode if 3c5x9b */
1332				if (sw_info & 0x000f) {
1333					printk("Forcing 3c5x9b full-duplex mode");
1334					break;
1335				}
1336			case 8:
1337				/* set full-duplex mode based on eeprom config setting */
1338				if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
1339					printk("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
1340					break;
1341				}
1342			default:
1343				/* xcvr=(0 || 4) OR user has an old 3c5x9 non "B" model */
1344				printk("Setting 3c5x9/3c5x9B half-duplex mode");
1345				net_diag = (net_diag & ~FD_ENABLE); /* disable full duplex */
1346		}
1347
1348		outw(net_diag, ioaddr + WN4_NETDIAG);
1349		printk(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
1350		if (el3_debug > 3)
1351			printk("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
1352		/* Enable link beat and jabber check. */
1353		outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
1354	}
1355
1356	/* Switch to the stats window, and clear all stats by reading. */
1357	outw(StatsDisable, ioaddr + EL3_CMD);
1358	EL3WINDOW(6);
1359	for (i = 0; i < 9; i++)
1360		inb(ioaddr + i);
1361	inw(ioaddr + 10);
1362	inw(ioaddr + 12);
1363
1364	/* Switch to register set 1 for normal use. */
1365	EL3WINDOW(1);
1366
1367	/* Accept b-case and phys addr only. */
1368	outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1369	outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
1370
1371	outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
1372	outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
1373	/* Allow status bits to be seen. */
1374	outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
1375	/* Ack all pending events, and set active indicator mask. */
1376	outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
1377		 ioaddr + EL3_CMD);
1378	outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
1379		 ioaddr + EL3_CMD);
1380
1381	netif_start_queue(dev);
1382}
1383
1384/* Power Management support functions */
1385#ifdef EL3_SUSPEND
1386
1387static int
1388el3_suspend(struct device *pdev, pm_message_t state)
1389{
1390	unsigned long flags;
1391	struct net_device *dev;
1392	struct el3_private *lp;
1393	int ioaddr;
1394
1395	dev = pdev->driver_data;
1396	lp = netdev_priv(dev);
1397	ioaddr = dev->base_addr;
1398
1399	spin_lock_irqsave(&lp->lock, flags);
1400
1401	if (netif_running(dev))
1402		netif_device_detach(dev);
1403
1404	el3_down(dev);
1405	outw(PowerDown, ioaddr + EL3_CMD);
1406
1407	spin_unlock_irqrestore(&lp->lock, flags);
1408	return 0;
1409}
1410
1411static int
1412el3_resume(struct device *pdev)
1413{
1414	unsigned long flags;
1415	struct net_device *dev;
1416	struct el3_private *lp;
1417	int ioaddr;
1418
1419	dev = pdev->driver_data;
1420	lp = netdev_priv(dev);
1421	ioaddr = dev->base_addr;
1422
1423	spin_lock_irqsave(&lp->lock, flags);
1424
1425	outw(PowerUp, ioaddr + EL3_CMD);
1426	el3_up(dev);
1427
1428	if (netif_running(dev))
1429		netif_device_attach(dev);
1430
1431	spin_unlock_irqrestore(&lp->lock, flags);
1432	return 0;
1433}
1434
1435#endif /* EL3_SUSPEND */
1436
1437/* Parameters that may be passed into the module. */
1438static int debug = -1;
1439static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
1440static int xcvr[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1441
1442module_param(debug,int, 0);
1443module_param_array(irq, int, NULL, 0);
1444module_param_array(xcvr, int, NULL, 0);
1445module_param(max_interrupt_work, int, 0);
1446MODULE_PARM_DESC(debug, "debug level (0-6)");
1447MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
1448MODULE_PARM_DESC(xcvr,"transceiver(s) (0=internal, 1=external)");
1449MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
1450#if defined(__ISAPNP__)
1451module_param(nopnp, int, 0);
1452MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
1453MODULE_DEVICE_TABLE(isapnp, el3_isapnp_adapters);
1454#endif	/* __ISAPNP__ */
1455MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B) ISA/PnP ethernet driver");
1456MODULE_LICENSE("GPL");
1457
1458static int __init el3_init_module(void)
1459{
1460	int ret = 0;
1461	el3_cards = 0;
1462
1463	if (debug >= 0)
1464		el3_debug = debug;
1465
1466	el3_root_dev = NULL;
1467	while (el3_probe(el3_cards) == 0) {
1468		if (irq[el3_cards] > 1)
1469			el3_root_dev->irq = irq[el3_cards];
1470		if (xcvr[el3_cards] >= 0)
1471			el3_root_dev->if_port = xcvr[el3_cards];
1472		el3_cards++;
1473	}
1474
1475#ifdef CONFIG_EISA
1476	ret = eisa_driver_register(&el3_eisa_driver);
1477#endif
1478#ifdef CONFIG_MCA
1479	{
1480		int err = mca_register_driver(&el3_mca_driver);
1481		if (ret == 0)
1482			ret = err;
1483	}
1484#endif
1485	return ret;
1486}
1487
1488static void __exit el3_cleanup_module(void)
1489{
1490	struct net_device *next_dev;
1491
1492	while (el3_root_dev) {
1493		struct el3_private *lp = netdev_priv(el3_root_dev);
1494
1495		next_dev = lp->next_dev;
1496		el3_common_remove (el3_root_dev);
1497		el3_root_dev = next_dev;
1498	}
1499
1500#ifdef CONFIG_EISA
1501	eisa_driver_unregister (&el3_eisa_driver);
1502#endif
1503#ifdef CONFIG_MCA
1504	mca_unregister_driver(&el3_mca_driver);
1505#endif
1506}
1507
1508module_init (el3_init_module);
1509module_exit (el3_cleanup_module);
1510