Skip to content
Snippets Groups Projects
3c509.c 41.9 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
/* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
/*
	Written 1993-2000 by Donald Becker.

	Copyright 1994-2000 by Donald Becker.
	Copyright 1993 United States Government as represented by the
	Director, National Security Agency.	 This software may be used and
	distributed according to the terms of the GNU General Public License,
	incorporated herein by reference.

	This driver is for the 3Com EtherLinkIII series.

	The author may be reached as becker@scyld.com, or C/O
	Scyld Computing Corporation
	410 Severn Ave., Suite 210
	Annapolis MD 21403

	Known limitations:
	Because of the way 3c509 ISA detection works it's difficult to predict
	a priori which of several ISA-mode cards will be detected first.

	This driver does not use predictive interrupt mode, resulting in higher
	packet latency but lower overhead.  If interrupts are disabled for an
	unusually long time it could also result in missed packets, but in
	practice this rarely happens.


	FIXES:
		Alan Cox:       Removed the 'Unexpected interrupt' bug.
		Michael Meskes:	Upgraded to Donald Becker's version 1.07.
		Alan Cox:	Increased the eeprom delay. Regardless of
Linus Torvalds's avatar
Linus Torvalds committed
				what the docs say some people definitely
				get problems with lower (but in card spec)
				delays
		v1.10 4/21/97 Fixed module code so that multiple cards may be detected,
				other cleanups.  -djb
		Andrea Arcangeli:	Upgraded to Donald Becker's version 1.12.
		Rick Payne:	Fixed SMP race condition
		v1.13 9/8/97 Made 'max_interrupt_work' an insmod-settable variable -djb
		v1.14 10/15/97 Avoided waiting..discard message for fast machines -djb
		v1.15 1/31/98 Faster recovery for Tx errors. -djb
		v1.16 2/3/98 Different ID port handling to avoid sound cards. -djb
		v1.18 12Mar2001 Andrew Morton
Linus Torvalds's avatar
Linus Torvalds committed
			- Avoid bogus detect of 3c590's (Andrzej Krzysztofowicz)
			- Reviewed against 1.18 from scyld.com
		v1.18a 17Nov2001 Jeff Garzik <jgarzik@pobox.com>
			- ethtool support
		v1.18b 1Mar2002 Zwane Mwaikambo <zwane@commfireservices.com>
			- Power Management support
		v1.18c 1Mar2002 David Ruggiero <jdr@farfalle.com>
			- Full duplex support
		v1.19  16Oct2002 Zwane Mwaikambo <zwane@linuxpower.ca>
			- Additional ethtool features
		v1.19a 28Oct2002 Davud Ruggiero <jdr@farfalle.com>
			- Increase *read_eeprom udelay to workaround oops with 2 cards.
		v1.19b 08Nov2002 Marc Zyngier <maz@wild-wind.fr.eu.org>
			- Introduce driver model for EISA cards.
		v1.20  04Feb2008 Ondrej Zary <linux@rainbow-software.org>
			- convert to isa_driver and pnp_driver and some cleanups
Linus Torvalds's avatar
Linus Torvalds committed
*/

#define DRV_NAME	"3c509"
#define DRV_VERSION	"1.20"
#define DRV_RELDATE	"04Feb2008"
Linus Torvalds's avatar
Linus Torvalds committed

/* A few values that may be tweaked. */

/* Time in jiffies before concluding the transmitter is hung. */
#define TX_TIMEOUT  (400*HZ/1000)

#include <linux/module.h>
#include <linux/mca.h>
#include <linux/isa.h>
#include <linux/pnp.h>
Linus Torvalds's avatar
Linus Torvalds committed
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/in.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/pm.h>
#include <linux/skbuff.h>
#include <linux/delay.h>	/* for udelay() */
#include <linux/spinlock.h>
#include <linux/ethtool.h>
#include <linux/device.h>
#include <linux/eisa.h>
#include <linux/bitops.h>

#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h>

static char version[] __devinitdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
Linus Torvalds's avatar
Linus Torvalds committed

#ifdef EL3_DEBUG
static int el3_debug = EL3_DEBUG;
#else
static int el3_debug = 2;
#endif

/* Used to do a global count of all the cards in the system.  Must be
 * a global variable so that the mca/eisa probe routines can increment
 * it */
static int el3_cards = 0;
#define EL3_MAX_CARDS 8
Linus Torvalds's avatar
Linus Torvalds committed

/* To minimize the size of the driver source I only define operating
   constants if they are used several times.  You'll need the manual
   anyway if you want to understand driver details. */
/* Offsets from base I/O address. */
#define EL3_DATA 0x00
#define EL3_CMD 0x0e
#define EL3_STATUS 0x0e
#define	EEPROM_READ 0x80
Linus Torvalds's avatar
Linus Torvalds committed

#define EL3_IO_EXTENT	16

#define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)


/* The top five bits written to EL3_CMD are a command, the lower
   11 bits are the parameter, if applicable. */
enum c509cmd {
	TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
	RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
	TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
	FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
	SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
	SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
	StatsDisable = 22<<11, StopCoax = 23<<11, PowerUp = 27<<11,
	PowerDown = 28<<11, PowerAuto = 29<<11};

enum c509status {
	IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
	TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
	IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000, };

/* The SetRxFilter command accepts the following classes: */
enum RxFilter {
	RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8 };

/* Register window 1 offsets, the window used in normal operation. */
#define TX_FIFO		0x00
#define RX_FIFO		0x00
#define RX_STATUS 	0x08
#define TX_STATUS 	0x0B
#define TX_FREE		0x0C		/* Remaining free bytes in Tx buffer. */

#define WN0_CONF_CTRL	0x04		/* Window 0: Configuration control register */
#define WN0_ADDR_CONF	0x06		/* Window 0: Address configuration register */
#define WN0_IRQ		0x08		/* Window 0: Set IRQ line in bits 12-15. */
#define WN4_MEDIA	0x0A		/* Window 4: Various transcvr/media bits. */
#define	MEDIA_TP	0x00C0		/* Enable link beat and jabber for 10baseT. */
#define WN4_NETDIAG	0x06		/* Window 4: Net diagnostic */
#define FD_ENABLE	0x8000		/* Enable full-duplex ("external loopback") */
Linus Torvalds's avatar
Linus Torvalds committed

/*
 * Must be a power of two (we use a binary and in the
 * circular queue)
 */
#define SKB_QUEUE_SIZE	64

enum el3_cardtype { EL3_ISA, EL3_PNP, EL3_MCA, EL3_EISA };

Linus Torvalds's avatar
Linus Torvalds committed
struct el3_private {
	spinlock_t lock;
	/* skb send-queue */
	int head, size;
	struct sk_buff *queue[SKB_QUEUE_SIZE];
	enum el3_cardtype type;
Linus Torvalds's avatar
Linus Torvalds committed
};
static int id_port;
static int current_tag;
static struct net_device *el3_devs[EL3_MAX_CARDS];

/* Parameters that may be passed into the module. */
static int debug = -1;
static int irq[] = {-1, -1, -1, -1, -1, -1, -1, -1};
/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
static int max_interrupt_work = 10;
#ifdef CONFIG_PNP
static int nopnp;
#endif
Linus Torvalds's avatar
Linus Torvalds committed

static int __devinit el3_common_init(struct net_device *dev);
static void el3_common_remove(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
static ushort id_read_eeprom(int index);
static ushort read_eeprom(int ioaddr, int index);
static int el3_open(struct net_device *dev);
static netdev_tx_t el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
static irqreturn_t el3_interrupt(int irq, void *dev_id);
Linus Torvalds's avatar
Linus Torvalds committed
static void update_stats(struct net_device *dev);
static struct net_device_stats *el3_get_stats(struct net_device *dev);
static int el3_rx(struct net_device *dev);
static int el3_close(struct net_device *dev);
static void set_multicast_list(struct net_device *dev);
static void el3_tx_timeout (struct net_device *dev);
static void el3_down(struct net_device *dev);
static void el3_up(struct net_device *dev);
static const struct ethtool_ops ethtool_ops;
#ifdef CONFIG_PM
static int el3_suspend(struct device *, pm_message_t);
static int el3_resume(struct device *);
#else
#define el3_suspend NULL
#define el3_resume NULL
Linus Torvalds's avatar
Linus Torvalds committed
#endif
Linus Torvalds's avatar
Linus Torvalds committed
/* generic device remove for all device types */
static int el3_device_remove (struct device *device);
#ifdef CONFIG_NET_POLL_CONTROLLER
static void el3_poll_controller(struct net_device *dev);
#endif

/* Return 0 on success, 1 on error, 2 when found already detected PnP card */
static int el3_isa_id_sequence(__be16 *phys_addr)
{
	short lrs_state = 0xff;
	int i;

	/* ISA boards are detected by sending the ID sequence to the
	   ID_PORT.  We find cards past the first by setting the 'current_tag'
	   on cards as they are found.  Cards with their tag set will not
	   respond to subsequent ID sequences. */

	outb(0x00, id_port);
	outb(0x00, id_port);
	for (i = 0; i < 255; i++) {
		outb(lrs_state, id_port);
		lrs_state <<= 1;
		lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
	}
	/* For the first probe, clear all board's tag registers. */
	if (current_tag == 0)
		outb(0xd0, id_port);
	else			/* Otherwise kill off already-found boards. */
		outb(0xd8, id_port);
	if (id_read_eeprom(7) != 0x6d50)
		return 1;
	/* Read in EEPROM data, which does contention-select.
	   Only the lowest address board will stay "on-line".
	   3Com got the byte order backwards. */
	for (i = 0; i < 3; i++)
		phys_addr[i] = htons(id_read_eeprom(i));
#ifdef CONFIG_PNP
	if (!nopnp) {
		/* The ISA PnP 3c509 cards respond to the ID sequence too.
		   This check is needed in order not to register them twice. */
		for (i = 0; i < el3_cards; i++) {
			struct el3_private *lp = netdev_priv(el3_devs[i]);
			if (lp->type == EL3_PNP &&
			    !memcmp(phys_addr, el3_devs[i]->dev_addr,
				    ETH_ALEN)) {
				if (el3_debug > 3)
					pr_debug("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
						phys_addr[0] & 0xff, phys_addr[0] >> 8,
						phys_addr[1] & 0xff, phys_addr[1] >> 8,
						phys_addr[2] & 0xff, phys_addr[2] >> 8);
				/* Set the adaptor tag so that the next card can be found. */
				outb(0xd0 + ++current_tag, id_port);
				return 2;
			}
		}
	}
#endif /* CONFIG_PNP */
	return 0;

}

static void __devinit el3_dev_fill(struct net_device *dev, __be16 *phys_addr,
				   int ioaddr, int irq, int if_port,
				   enum el3_cardtype type)
{
	struct el3_private *lp = netdev_priv(dev);

	memcpy(dev->dev_addr, phys_addr, ETH_ALEN);
	dev->base_addr = ioaddr;
	dev->irq = irq;
	dev->if_port = if_port;
	lp->type = type;
}

static int __devinit el3_isa_match(struct device *pdev,
				   unsigned int ndev)
{
	struct net_device *dev;
	int ioaddr, isa_irq, if_port, err;
	unsigned int iobase;
	__be16 phys_addr[3];

	while ((err = el3_isa_id_sequence(phys_addr)) == 2)
		;	/* Skip to next card when PnP card found */
	if (err == 1)
		return 0;

	iobase = id_read_eeprom(8);
	if_port = iobase >> 14;
	ioaddr = 0x200 + ((iobase & 0x1f) << 4);
	if (irq[el3_cards] > 1 && irq[el3_cards] < 16)
		isa_irq = irq[el3_cards];
	else
		isa_irq = id_read_eeprom(9) >> 12;

	dev = alloc_etherdev(sizeof(struct el3_private));
	if (!dev)
		return -ENOMEM;

	netdev_boot_setup_check(dev);

	if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509-isa")) {
		free_netdev(dev);
		return 0;
	}

	/* Set the adaptor tag so that the next card can be found. */
	outb(0xd0 + ++current_tag, id_port);

	/* Activate the adaptor at the EEPROM location. */
	outb((ioaddr >> 4) | 0xe0, id_port);

	EL3WINDOW(0);
	if (inw(ioaddr) != 0x6d50) {
		free_netdev(dev);
		return 0;
	}

	/* Free the interrupt so that some other card can use it. */
	outw(0x0f00, ioaddr + WN0_IRQ);

	el3_dev_fill(dev, phys_addr, ioaddr, isa_irq, if_port, EL3_ISA);
	dev_set_drvdata(pdev, dev);
	if (el3_common_init(dev)) {
		free_netdev(dev);
		return 0;
	}

	el3_devs[el3_cards++] = dev;
	return 1;
}

static int __devexit el3_isa_remove(struct device *pdev,
				    unsigned int ndev)
{
	el3_device_remove(pdev);
	dev_set_drvdata(pdev, NULL);
	return 0;
}

#ifdef CONFIG_PM
static int el3_isa_suspend(struct device *dev, unsigned int n,
			   pm_message_t state)
{
	current_tag = 0;
	return el3_suspend(dev, state);
}

static int el3_isa_resume(struct device *dev, unsigned int n)
{
	struct net_device *ndev = dev_get_drvdata(dev);
	int ioaddr = ndev->base_addr, err;
	__be16 phys_addr[3];

	while ((err = el3_isa_id_sequence(phys_addr)) == 2)
		;	/* Skip to next card when PnP card found */
	if (err == 1)
		return 0;
	/* Set the adaptor tag so that the next card can be found. */
	outb(0xd0 + ++current_tag, id_port);
	/* Enable the card */
	outb((ioaddr >> 4) | 0xe0, id_port);
	EL3WINDOW(0);
	if (inw(ioaddr) != 0x6d50)
		return 1;
	/* Free the interrupt so that some other card can use it. */
	outw(0x0f00, ioaddr + WN0_IRQ);
	return el3_resume(dev);
}
#endif

static struct isa_driver el3_isa_driver = {
	.match		= el3_isa_match,
	.remove		= __devexit_p(el3_isa_remove),
#ifdef CONFIG_PM
	.suspend	= el3_isa_suspend,
	.resume		= el3_isa_resume,
#endif
	.driver		= {
		.name	= "3c509"
	},
};
static int isa_registered;

#ifdef CONFIG_PNP
static struct pnp_device_id el3_pnp_ids[] = {
	{ .id = "TCM5090" }, /* 3Com Etherlink III (TP) */
	{ .id = "TCM5091" }, /* 3Com Etherlink III */
	{ .id = "TCM5094" }, /* 3Com Etherlink III (combo) */
	{ .id = "TCM5095" }, /* 3Com Etherlink III (TPO) */
	{ .id = "TCM5098" }, /* 3Com Etherlink III (TPC) */
	{ .id = "PNP80f7" }, /* 3Com Etherlink III compatible */
	{ .id = "PNP80f8" }, /* 3Com Etherlink III compatible */
	{ .id = "" }
};
MODULE_DEVICE_TABLE(pnp, el3_pnp_ids);

static int __devinit el3_pnp_probe(struct pnp_dev *pdev,
				    const struct pnp_device_id *id)
{
	short i;
	int ioaddr, irq, if_port;
	__be16 phys_addr[3];
	struct net_device *dev = NULL;
	int err;

	ioaddr = pnp_port_start(pdev, 0);
	if (!request_region(ioaddr, EL3_IO_EXTENT, "3c509-pnp"))
		return -EBUSY;
	irq = pnp_irq(pdev, 0);
	EL3WINDOW(0);
	for (i = 0; i < 3; i++)
		phys_addr[i] = htons(read_eeprom(ioaddr, i));
	if_port = read_eeprom(ioaddr, 8) >> 14;
	dev = alloc_etherdev(sizeof(struct el3_private));
	if (!dev) {
		release_region(ioaddr, EL3_IO_EXTENT);
		return -ENOMEM;
	}
	SET_NETDEV_DEV(dev, &pdev->dev);
	netdev_boot_setup_check(dev);

	el3_dev_fill(dev, phys_addr, ioaddr, irq, if_port, EL3_PNP);
	pnp_set_drvdata(pdev, dev);
	err = el3_common_init(dev);

	if (err) {
		pnp_set_drvdata(pdev, NULL);
		free_netdev(dev);
		return err;
	}

	el3_devs[el3_cards++] = dev;
	return 0;
}

static void __devexit el3_pnp_remove(struct pnp_dev *pdev)
{
	el3_common_remove(pnp_get_drvdata(pdev));
	pnp_set_drvdata(pdev, NULL);
}

#ifdef CONFIG_PM
static int el3_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
{
	return el3_suspend(&pdev->dev, state);
}

static int el3_pnp_resume(struct pnp_dev *pdev)
{
	return el3_resume(&pdev->dev);
}
#endif

static struct pnp_driver el3_pnp_driver = {
	.name		= "3c509",
	.id_table	= el3_pnp_ids,
	.probe		= el3_pnp_probe,
	.remove		= __devexit_p(el3_pnp_remove),
#ifdef CONFIG_PM
	.suspend	= el3_pnp_suspend,
	.resume		= el3_pnp_resume,
#endif
};
static int pnp_registered;
#endif /* CONFIG_PNP */

Linus Torvalds's avatar
Linus Torvalds committed
#ifdef CONFIG_EISA
static struct eisa_device_id el3_eisa_ids[] = {
		{ "TCM5090" },
		{ "TCM5091" },
Linus Torvalds's avatar
Linus Torvalds committed
		{ "TCM5092" },
		{ "TCM5093" },
		{ "TCM5094" },
		{ "TCM5098" },
Linus Torvalds's avatar
Linus Torvalds committed
		{ "" }
};
MODULE_DEVICE_TABLE(eisa, el3_eisa_ids);
Linus Torvalds's avatar
Linus Torvalds committed

static int el3_eisa_probe (struct device *device);

static struct eisa_driver el3_eisa_driver = {
		.id_table = el3_eisa_ids,
		.driver   = {
				.name    = "3c579",
Linus Torvalds's avatar
Linus Torvalds committed
				.probe   = el3_eisa_probe,
				.remove  = __devexit_p (el3_device_remove),
				.suspend = el3_suspend,
				.resume  = el3_resume,
Linus Torvalds's avatar
Linus Torvalds committed
		}
};
static int eisa_registered;
Linus Torvalds's avatar
Linus Torvalds committed
#endif

#ifdef CONFIG_MCA
static int el3_mca_probe(struct device *dev);

static short el3_mca_adapter_ids[] __initdata = {
Linus Torvalds's avatar
Linus Torvalds committed
		0x627c,
		0x627d,
		0x62db,
		0x62f6,
		0x62f7,
		0x0000
};

static char *el3_mca_adapter_names[] __initdata = {
Linus Torvalds's avatar
Linus Torvalds committed
		"3Com 3c529 EtherLink III (10base2)",
		"3Com 3c529 EtherLink III (10baseT)",
		"3Com 3c529 EtherLink III (test mode)",
		"3Com 3c529 EtherLink III (TP or coax)",
		"3Com 3c529 EtherLink III (TP)",
		NULL
};

static struct mca_driver el3_mca_driver = {
		.id_table = el3_mca_adapter_ids,
		.driver = {
				.name = "3c529",
				.bus = &mca_bus_type,
				.probe = el3_mca_probe,
				.remove = __devexit_p(el3_device_remove),
				.suspend = el3_suspend,
				.resume  = el3_resume,
Linus Torvalds's avatar
Linus Torvalds committed
		},
};
static int mca_registered;
Linus Torvalds's avatar
Linus Torvalds committed
#endif /* CONFIG_MCA */

static const struct net_device_ops netdev_ops = {
	.ndo_open 		= el3_open,
	.ndo_stop	 	= el3_close,
	.ndo_start_xmit 	= el3_start_xmit,
	.ndo_get_stats 		= el3_get_stats,
	.ndo_set_multicast_list = set_multicast_list,
	.ndo_tx_timeout 	= el3_tx_timeout,
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_set_mac_address 	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= el3_poll_controller,
#endif
};

static int __devinit el3_common_init(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
{
	struct el3_private *lp = netdev_priv(dev);
	int err;
	const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
Linus Torvalds's avatar
Linus Torvalds committed

	spin_lock_init(&lp->lock);

	if (dev->mem_start & 0x05) { /* xcvr codes 1/3/4/12 */
		dev->if_port = (dev->mem_start & 0x0f);
	} else { /* xcvr codes 0/8 */
		/* use eeprom value, but save user's full-duplex selection */
		dev->if_port |= (dev->mem_start & 0x08);
	}

	/* The EL3-specific entries in the device structure. */
	dev->netdev_ops = &netdev_ops;
Linus Torvalds's avatar
Linus Torvalds committed
	dev->watchdog_timeo = TX_TIMEOUT;
	SET_ETHTOOL_OPS(dev, &ethtool_ops);

	err = register_netdev(dev);
	if (err) {
		pr_err("Failed to register 3c5x9 at %#3.3lx, IRQ %d.\n",
Linus Torvalds's avatar
Linus Torvalds committed
			dev->base_addr, dev->irq);
		release_region(dev->base_addr, EL3_IO_EXTENT);
		return err;
	}

	pr_info("%s: 3c5x9 found at %#3.3lx, %s port, address %pM, IRQ %d.\n",
	       dev->name, dev->base_addr, if_names[(dev->if_port & 0x03)],
	       dev->dev_addr, dev->irq);
Linus Torvalds's avatar
Linus Torvalds committed

	if (el3_debug > 0)
		pr_info("%s", version);
Linus Torvalds's avatar
Linus Torvalds committed
	return 0;

}

static void el3_common_remove (struct net_device *dev)
{
	unregister_netdev (dev);
	release_region(dev->base_addr, EL3_IO_EXTENT);
	free_netdev (dev);
}

#ifdef CONFIG_MCA
static int __init el3_mca_probe(struct device *device)
Linus Torvalds's avatar
Linus Torvalds committed
{
	/* Based on Erik Nygren's (nygren@mit.edu) 3c529 patch,
	 * heavily modified by Chris Beauregard
	 * (cpbeaure@csclub.uwaterloo.ca) to support standard MCA
	 * probing.
	 *
	 * redone for multi-card detection by ZP Gu (zpg@castle.net)
	 * now works as a module */

	short i;
	int ioaddr, irq, if_port;
	__be16 phys_addr[3];
Linus Torvalds's avatar
Linus Torvalds committed
	struct net_device *dev = NULL;
	u_char pos4, pos5;
	struct mca_device *mdev = to_mca_device(device);
	int slot = mdev->slot;
	int err;

	pos4 = mca_device_read_stored_pos(mdev, 4);
	pos5 = mca_device_read_stored_pos(mdev, 5);

	ioaddr = ((short)((pos4&0xfc)|0x02)) << 8;
	irq = pos5 & 0x0f;


	pr_info("3c529: found %s at slot %d\n",
		el3_mca_adapter_names[mdev->index], slot + 1);
Linus Torvalds's avatar
Linus Torvalds committed

	/* claim the slot */
	strncpy(mdev->name, el3_mca_adapter_names[mdev->index],
			sizeof(mdev->name));
	mca_device_set_claim(mdev, 1);

	if_port = pos4 & 0x03;

	irq = mca_device_transform_irq(mdev, irq);
	ioaddr = mca_device_transform_ioport(mdev, ioaddr);
Linus Torvalds's avatar
Linus Torvalds committed
	if (el3_debug > 2) {
		pr_debug("3c529: irq %d  ioaddr 0x%x  ifport %d\n", irq, ioaddr, if_port);
Linus Torvalds's avatar
Linus Torvalds committed
	}
	EL3WINDOW(0);
	for (i = 0; i < 3; i++)
		phys_addr[i] = htons(read_eeprom(ioaddr, i));
Linus Torvalds's avatar
Linus Torvalds committed

	dev = alloc_etherdev(sizeof (struct el3_private));
	if (dev == NULL) {
		release_region(ioaddr, EL3_IO_EXTENT);
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
	}

	netdev_boot_setup_check(dev);

	el3_dev_fill(dev, phys_addr, ioaddr, irq, if_port, EL3_MCA);
	dev_set_drvdata(device, dev);
Linus Torvalds's avatar
Linus Torvalds committed
	err = el3_common_init(dev);

	if (err) {
		dev_set_drvdata(device, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
		free_netdev(dev);
		return -ENOMEM;
	}

	el3_devs[el3_cards++] = dev;
Linus Torvalds's avatar
Linus Torvalds committed
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
#endif /* CONFIG_MCA */

#ifdef CONFIG_EISA
static int __init el3_eisa_probe (struct device *device)
Linus Torvalds's avatar
Linus Torvalds committed
{
	short i;
	int ioaddr, irq, if_port;
	__be16 phys_addr[3];
Linus Torvalds's avatar
Linus Torvalds committed
	struct net_device *dev = NULL;
	struct eisa_device *edev;
	int err;

	/* Yeepee, The driver framework is calling us ! */
	edev = to_eisa_device (device);
	ioaddr = edev->base_addr;
	if (!request_region(ioaddr, EL3_IO_EXTENT, "3c579-eisa"))
Linus Torvalds's avatar
Linus Torvalds committed
		return -EBUSY;

	/* Change the register set to the configuration window 0. */
	outw(SelectWindow | 0, ioaddr + 0xC80 + EL3_CMD);

	irq = inw(ioaddr + WN0_IRQ) >> 12;
	if_port = inw(ioaddr + 6)>>14;
	for (i = 0; i < 3; i++)
		phys_addr[i] = htons(read_eeprom(ioaddr, i));

	/* Restore the "Product ID" to the EEPROM read register. */
	read_eeprom(ioaddr, 3);

	dev = alloc_etherdev(sizeof (struct el3_private));
	if (dev == NULL) {
		release_region(ioaddr, EL3_IO_EXTENT);
		return -ENOMEM;
	}

	netdev_boot_setup_check(dev);

	el3_dev_fill(dev, phys_addr, ioaddr, irq, if_port, EL3_EISA);
Linus Torvalds's avatar
Linus Torvalds committed
	eisa_set_drvdata (edev, dev);
	err = el3_common_init(dev);

	if (err) {
		eisa_set_drvdata (edev, NULL);
		free_netdev(dev);
		return err;
	}

	el3_devs[el3_cards++] = dev;
Linus Torvalds's avatar
Linus Torvalds committed
	return 0;
}
#endif

/* This remove works for all device types.
 *
 * The net dev must be stored in the driver data field */
Linus Torvalds's avatar
Linus Torvalds committed
static int __devexit el3_device_remove (struct device *device)
{
	struct net_device *dev;

	dev = dev_get_drvdata(device);
Linus Torvalds's avatar
Linus Torvalds committed

	el3_common_remove (dev);
	return 0;
}

/* Read a word from the EEPROM using the regular EEPROM access register.
   Assume that we are in register window zero.
 */
static ushort read_eeprom(int ioaddr, int index)
{
	outw(EEPROM_READ + index, ioaddr + 10);
	/* Pause for at least 162 us. for the read to take place.
Linus Torvalds's avatar
Linus Torvalds committed
	   Some chips seem to require much longer */
	mdelay(2);
	return inw(ioaddr + 12);
}

/* Read a word from the EEPROM when in the ISA ID probe state. */
static ushort id_read_eeprom(int index)
Linus Torvalds's avatar
Linus Torvalds committed
{
	int bit, word = 0;

	/* Issue read command, and pause for at least 162 us. for it to complete.
	   Assume extra-fast 16Mhz bus. */
	outb(EEPROM_READ + index, id_port);

	/* Pause for at least 162 us. for the read to take place. */
	/* Some chips seem to require much longer */
	mdelay(4);
Linus Torvalds's avatar
Linus Torvalds committed
	for (bit = 15; bit >= 0; bit--)
		word = (word << 1) + (inb(id_port) & 0x01);

	if (el3_debug > 3)
		pr_debug("  3c509 EEPROM word %d %#4.4x.\n", index, word);
Linus Torvalds's avatar
Linus Torvalds committed

	return word;
}


static int
el3_open(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	int i;

	outw(TxReset, ioaddr + EL3_CMD);
	outw(RxReset, ioaddr + EL3_CMD);
	outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);

	i = request_irq(dev->irq, el3_interrupt, 0, dev->name, dev);
Linus Torvalds's avatar
Linus Torvalds committed
	if (i)
		return i;

	EL3WINDOW(0);
	if (el3_debug > 3)
		pr_debug("%s: Opening, IRQ %d	 status@%x %4.4x.\n", dev->name,
Linus Torvalds's avatar
Linus Torvalds committed
			   dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));

	el3_up(dev);

	if (el3_debug > 3)
		pr_debug("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
Linus Torvalds's avatar
Linus Torvalds committed
			   dev->name, dev->irq, inw(ioaddr + EL3_STATUS));

	return 0;
}

static void
el3_tx_timeout (struct net_device *dev)
{
	int ioaddr = dev->base_addr;

	/* Transmitter timeout, serious problems. */
	pr_warning("%s: transmit timed out, Tx_status %2.2x status %4.4x Tx FIFO room %d.\n",
Linus Torvalds's avatar
Linus Torvalds committed
		   dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
		   inw(ioaddr + TX_FREE));
	dev->stats.tx_errors++;
Eric Dumazet's avatar
Eric Dumazet committed
	dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds's avatar
Linus Torvalds committed
	/* Issue TX_RESET and TX_START commands. */
	outw(TxReset, ioaddr + EL3_CMD);
	outw(TxEnable, ioaddr + EL3_CMD);
	netif_wake_queue(dev);
}


static netdev_tx_t
Linus Torvalds's avatar
Linus Torvalds committed
el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct el3_private *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;
	unsigned long flags;

	netif_stop_queue (dev);

	dev->stats.tx_bytes += skb->len;
Linus Torvalds's avatar
Linus Torvalds committed
	if (el3_debug > 4) {
		pr_debug("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
Linus Torvalds's avatar
Linus Torvalds committed
			   dev->name, skb->len, inw(ioaddr + EL3_STATUS));
	}
#if 0
#ifndef final_version
	{	/* Error-checking code, delete someday. */
		ushort status = inw(ioaddr + EL3_STATUS);
		if (status & 0x0001 && 		/* IRQ line active, missed one. */
		    inw(ioaddr + EL3_STATUS) & 1) { 			/* Make sure. */
			pr_debug("%s: Missed interrupt, status then %04x now %04x"
Linus Torvalds's avatar
Linus Torvalds committed
				   "  Tx %2.2x Rx %4.4x.\n", dev->name, status,
				   inw(ioaddr + EL3_STATUS), inb(ioaddr + TX_STATUS),
				   inw(ioaddr + RX_STATUS));
			/* Fake interrupt trigger by masking, acknowledge interrupts. */
			outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
			outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
				 ioaddr + EL3_CMD);
			outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
		}
	}
#endif
#endif
	/*
	 *	We lock the driver against other processors. Note
	 *	we don't need to lock versus the IRQ as we suspended
	 *	that. This means that we lose the ability to take
	 *	an RX during a TX upload. That sucks a bit with SMP
	 *	on an original 3c509 (2K buffer)
	 *
	 *	Using disable_irq stops us crapping on other
	 *	time sensitive devices.
	 */

	spin_lock_irqsave(&lp->lock, flags);

	/* Put out the doubleword header... */
	outw(skb->len, ioaddr + TX_FIFO);
	outw(0x00, ioaddr + TX_FIFO);
	/* ... and the packet rounded to a doubleword. */
	outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);

	if (inw(ioaddr + TX_FREE) > 1536)
		netif_start_queue(dev);
	else
		/* Interrupt us when the FIFO has room for max-sized packet. */
		outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);

	spin_unlock_irqrestore(&lp->lock, flags);

	dev_kfree_skb (skb);

	/* Clear the Tx status stack. */
	{
		short tx_status;
		int i = 4;

		while (--i > 0	&&	(tx_status = inb(ioaddr + TX_STATUS)) > 0) {
			if (tx_status & 0x38) dev->stats.tx_aborted_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
			if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
			if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
			outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
}

/* The EL3 interrupt handler. */
static irqreturn_t
el3_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
{
	struct net_device *dev = dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
	struct el3_private *lp;
	int ioaddr, status;
	int i = max_interrupt_work;

	lp = netdev_priv(dev);
	spin_lock(&lp->lock);

	ioaddr = dev->base_addr;

	if (el3_debug > 4) {
		status = inw(ioaddr + EL3_STATUS);
		pr_debug("%s: interrupt, status %4.4x.\n", dev->name, status);
Linus Torvalds's avatar
Linus Torvalds committed
	}

	while ((status = inw(ioaddr + EL3_STATUS)) &
		   (IntLatch | RxComplete | StatsFull)) {

		if (status & RxComplete)
			el3_rx(dev);

		if (status & TxAvailable) {
			if (el3_debug > 5)
				pr_debug("	TX room bit was handled.\n");
Linus Torvalds's avatar
Linus Torvalds committed
			/* There's room in the FIFO for a full-sized packet. */
			outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
			netif_wake_queue (dev);
		}
		if (status & (AdapterFailure | RxEarly | StatsFull | TxComplete)) {
			/* Handle all uncommon interrupts. */
			if (status & StatsFull)				/* Empty statistics. */
				update_stats(dev);
			if (status & RxEarly) {				/* Rx early is unused. */
				el3_rx(dev);
				outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
			}
			if (status & TxComplete) {			/* Really Tx error. */
				short tx_status;
				int i = 4;

				while (--i>0 && (tx_status = inb(ioaddr + TX_STATUS)) > 0) {
					if (tx_status & 0x38) dev->stats.tx_aborted_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
					if (tx_status & 0x30) outw(TxReset, ioaddr + EL3_CMD);
					if (tx_status & 0x3C) outw(TxEnable, ioaddr + EL3_CMD);
					outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
				}
			}
			if (status & AdapterFailure) {
				/* Adapter failure requires Rx reset and reinit. */
				outw(RxReset, ioaddr + EL3_CMD);
				/* Set the Rx filter to the current state. */
				outw(SetRxFilter | RxStation | RxBroadcast
					 | (dev->flags & IFF_ALLMULTI ? RxMulticast : 0)
					 | (dev->flags & IFF_PROMISC ? RxProm : 0),
					 ioaddr + EL3_CMD);
				outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
				outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
			}
		}

		if (--i < 0) {
			pr_err("%s: Infinite loop in interrupt, status %4.4x.\n",
Linus Torvalds's avatar
Linus Torvalds committed
				   dev->name, status);
			/* Clear all interrupts. */
			outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
			break;
		}
		/* Acknowledge the IRQ. */
		outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD); /* Ack IRQ */
	}

	if (el3_debug > 4) {
		pr_debug("%s: exiting interrupt, status %4.4x.\n", dev->name,
Linus Torvalds's avatar
Linus Torvalds committed
			   inw(ioaddr + EL3_STATUS));
	}
	spin_unlock(&lp->lock);
	return IRQ_HANDLED;
}


#ifdef CONFIG_NET_POLL_CONTROLLER
/*
 * Polling receive - used by netconsole and other diagnostic tools
 * to allow network i/o with interrupts disabled.
 */
static void el3_poll_controller(struct net_device *dev)
{
	disable_irq(dev->irq);
Linus Torvalds's avatar
Linus Torvalds committed
	enable_irq(dev->irq);
}
#endif

static struct net_device_stats *
el3_get_stats(struct net_device *dev)
{
	struct el3_private *lp = netdev_priv(dev);
	unsigned long flags;

	/*
	 *	This is fast enough not to bother with disable IRQ