3c509.c 41.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* 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.
31
		Alan Cox:	Increased the eeprom delay. Regardless of
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42
				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
43
		v1.18 12Mar2001 Andrew Morton
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
			- 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>
57 58 59
			- 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
L
Linus Torvalds 已提交
60 61 62
*/

#define DRV_NAME	"3c509"
63 64
#define DRV_VERSION	"1.20"
#define DRV_RELDATE	"04Feb2008"
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72

/* 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>
73 74
#include <linux/isa.h>
#include <linux/pnp.h>
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#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>

96
static char version[] __devinitdata = DRV_NAME ".c:" DRV_VERSION " " DRV_RELDATE " becker@scyld.com\n";
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107

#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;
108
#define EL3_MAX_CARDS 8
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116

/* 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
117
#define	EEPROM_READ 0x80
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

#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 */
158
#define FD_ENABLE	0x8000		/* Enable full-duplex ("external loopback") */
L
Linus Torvalds 已提交
159 160 161 162 163 164 165

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

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

L
Linus Torvalds 已提交
168 169 170 171 172
struct el3_private {
	spinlock_t lock;
	/* skb send-queue */
	int head, size;
	struct sk_buff *queue[SKB_QUEUE_SIZE];
173
	enum el3_cardtype type;
L
Linus Torvalds 已提交
174
};
175 176 177 178 179 180 181 182 183 184 185 186
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
L
Linus Torvalds 已提交
187

188
static int __devinit el3_common_init(struct net_device *dev);
189
static void el3_common_remove(struct net_device *dev);
L
Linus Torvalds 已提交
190 191 192
static ushort id_read_eeprom(int index);
static ushort read_eeprom(int ioaddr, int index);
static int el3_open(struct net_device *dev);
193
static netdev_tx_t el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
194
static irqreturn_t el3_interrupt(int irq, void *dev_id);
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202
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);
203
static const struct ethtool_ops ethtool_ops;
204
#ifdef CONFIG_PM
205 206 207 208 209
static int el3_suspend(struct device *, pm_message_t);
static int el3_resume(struct device *);
#else
#define el3_suspend NULL
#define el3_resume NULL
L
Linus Torvalds 已提交
210
#endif
211 212


L
Linus Torvalds 已提交
213 214 215 216 217 218
/* 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

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/* 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]);
255 256 257
			if (lp->type == EL3_PNP &&
			    !memcmp(phys_addr, el3_devs[i]->dev_addr,
				    ETH_ALEN)) {
258
				if (el3_debug > 3)
259
					pr_debug("3c509 with address %02x %02x %02x %02x %02x %02x was found by ISAPnP\n",
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
						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
398
static struct pnp_device_id el3_pnp_ids[] = {
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	{ .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;
A
Al Viro 已提交
415
	__be16 phys_addr[3];
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	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 */

L
Linus Torvalds 已提交
480
#ifdef CONFIG_EISA
481
static struct eisa_device_id el3_eisa_ids[] = {
M
Maciej W. Rozycki 已提交
482 483
		{ "TCM5090" },
		{ "TCM5091" },
L
Linus Torvalds 已提交
484 485
		{ "TCM5092" },
		{ "TCM5093" },
M
Maciej W. Rozycki 已提交
486
		{ "TCM5094" },
487
		{ "TCM5095" },
M
Maciej W. Rozycki 已提交
488
		{ "TCM5098" },
L
Linus Torvalds 已提交
489 490
		{ "" }
};
491
MODULE_DEVICE_TABLE(eisa, el3_eisa_ids);
L
Linus Torvalds 已提交
492 493 494 495 496 497

static int el3_eisa_probe (struct device *device);

static struct eisa_driver el3_eisa_driver = {
		.id_table = el3_eisa_ids,
		.driver   = {
498
				.name    = "3c579",
L
Linus Torvalds 已提交
499
				.probe   = el3_eisa_probe,
500 501 502
				.remove  = __devexit_p (el3_device_remove),
				.suspend = el3_suspend,
				.resume  = el3_resume,
L
Linus Torvalds 已提交
503 504
		}
};
505
static int eisa_registered;
L
Linus Torvalds 已提交
506 507 508 509 510
#endif

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

511
static short el3_mca_adapter_ids[] __initdata = {
L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519
		0x627c,
		0x627d,
		0x62db,
		0x62f6,
		0x62f7,
		0x0000
};

520
static char *el3_mca_adapter_names[] __initdata = {
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
		"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),
536 537
				.suspend = el3_suspend,
				.resume  = el3_resume,
L
Linus Torvalds 已提交
538 539
		},
};
540
static int mca_registered;
L
Linus Torvalds 已提交
541 542
#endif /* CONFIG_MCA */

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
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
};

558
static int __devinit el3_common_init(struct net_device *dev)
L
Linus Torvalds 已提交
559 560 561
{
	struct el3_private *lp = netdev_priv(dev);
	int err;
562
	const char *if_names[] = {"10baseT", "AUI", "undefined", "BNC"};
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573

	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. */
574
	dev->netdev_ops = &netdev_ops;
L
Linus Torvalds 已提交
575 576 577 578 579
	dev->watchdog_timeo = TX_TIMEOUT;
	SET_ETHTOOL_OPS(dev, &ethtool_ops);

	err = register_netdev(dev);
	if (err) {
580
		pr_err("Failed to register 3c5x9 at %#3.3lx, IRQ %d.\n",
L
Linus Torvalds 已提交
581 582 583 584 585
			dev->base_addr, dev->irq);
		release_region(dev->base_addr, EL3_IO_EXTENT);
		return err;
	}

586
	pr_info("%s: 3c5x9 found at %#3.3lx, %s port, address %pM, IRQ %d.\n",
587
	       dev->name, dev->base_addr, if_names[(dev->if_port & 0x03)],
J
Johannes Berg 已提交
588
	       dev->dev_addr, dev->irq);
L
Linus Torvalds 已提交
589 590

	if (el3_debug > 0)
591
		pr_info("%s", version);
L
Linus Torvalds 已提交
592 593 594 595 596 597 598 599 600 601 602 603
	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
604
static int __init el3_mca_probe(struct device *device)
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613 614 615
{
	/* 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;
A
Al Viro 已提交
616
	__be16 phys_addr[3];
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629
	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;


630 631
	pr_info("3c529: found %s at slot %d\n",
		el3_mca_adapter_names[mdev->index], slot + 1);
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639 640

	/* 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);
641
	ioaddr = mca_device_transform_ioport(mdev, ioaddr);
L
Linus Torvalds 已提交
642
	if (el3_debug > 2) {
643
		pr_debug("3c529: irq %d  ioaddr 0x%x  ifport %d\n", irq, ioaddr, if_port);
L
Linus Torvalds 已提交
644 645
	}
	EL3WINDOW(0);
A
Al Viro 已提交
646 647
	for (i = 0; i < 3; i++)
		phys_addr[i] = htons(read_eeprom(ioaddr, i));
L
Linus Torvalds 已提交
648 649 650

	dev = alloc_etherdev(sizeof (struct el3_private));
	if (dev == NULL) {
A
Al Viro 已提交
651 652
		release_region(ioaddr, EL3_IO_EXTENT);
		return -ENOMEM;
L
Linus Torvalds 已提交
653 654 655 656
	}

	netdev_boot_setup_check(dev);

657
	el3_dev_fill(dev, phys_addr, ioaddr, irq, if_port, EL3_MCA);
658
	dev_set_drvdata(device, dev);
L
Linus Torvalds 已提交
659 660 661
	err = el3_common_init(dev);

	if (err) {
662
		dev_set_drvdata(device, NULL);
L
Linus Torvalds 已提交
663 664 665 666
		free_netdev(dev);
		return -ENOMEM;
	}

667
	el3_devs[el3_cards++] = dev;
L
Linus Torvalds 已提交
668 669
	return 0;
}
670

L
Linus Torvalds 已提交
671 672 673
#endif /* CONFIG_MCA */

#ifdef CONFIG_EISA
674
static int __init el3_eisa_probe (struct device *device)
L
Linus Torvalds 已提交
675 676 677
{
	short i;
	int ioaddr, irq, if_port;
A
Al Viro 已提交
678
	__be16 phys_addr[3];
L
Linus Torvalds 已提交
679 680 681 682 683 684 685
	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;
686

687
	if (!request_region(ioaddr, EL3_IO_EXTENT, "3c579-eisa"))
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
		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);

709
	el3_dev_fill(dev, phys_addr, ioaddr, irq, if_port, EL3_EISA);
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717 718
	eisa_set_drvdata (edev, dev);
	err = el3_common_init(dev);

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

719
	el3_devs[el3_cards++] = dev;
L
Linus Torvalds 已提交
720 721 722 723 724 725
	return 0;
}
#endif

/* This remove works for all device types.
 *
726
 * The net dev must be stored in the driver data field */
L
Linus Torvalds 已提交
727 728 729 730
static int __devexit el3_device_remove (struct device *device)
{
	struct net_device *dev;

731
	dev = dev_get_drvdata(device);
L
Linus Torvalds 已提交
732 733 734 735 736 737 738 739 740 741 742

	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);
743
	/* Pause for at least 162 us. for the read to take place.
L
Linus Torvalds 已提交
744 745 746 747 748 749
	   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. */
750
static ushort id_read_eeprom(int index)
L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758 759 760
{
	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);
761

L
Linus Torvalds 已提交
762 763 764 765
	for (bit = 15; bit >= 0; bit--)
		word = (word << 1) + (inb(id_port) & 0x01);

	if (el3_debug > 3)
766
		pr_debug("  3c509 EEPROM word %d %#4.4x.\n", index, word);
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

	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);

782
	i = request_irq(dev->irq, el3_interrupt, 0, dev->name, dev);
L
Linus Torvalds 已提交
783 784 785 786 787
	if (i)
		return i;

	EL3WINDOW(0);
	if (el3_debug > 3)
788
		pr_debug("%s: Opening, IRQ %d	 status@%x %4.4x.\n", dev->name,
L
Linus Torvalds 已提交
789 790 791 792 793
			   dev->irq, ioaddr + EL3_STATUS, inw(ioaddr + EL3_STATUS));

	el3_up(dev);

	if (el3_debug > 3)
794
		pr_debug("%s: Opened 3c509  IRQ %d  status %4.4x.\n",
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802 803 804 805
			   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. */
806
	pr_warning("%s: transmit timed out, Tx_status %2.2x status %4.4x Tx FIFO room %d.\n",
L
Linus Torvalds 已提交
807 808
		   dev->name, inb(ioaddr + TX_STATUS), inw(ioaddr + EL3_STATUS),
		   inw(ioaddr + TX_FREE));
809
	dev->stats.tx_errors++;
E
Eric Dumazet 已提交
810
	dev->trans_start = jiffies; /* prevent tx timeout */
L
Linus Torvalds 已提交
811 812 813 814 815 816 817
	/* Issue TX_RESET and TX_START commands. */
	outw(TxReset, ioaddr + EL3_CMD);
	outw(TxEnable, ioaddr + EL3_CMD);
	netif_wake_queue(dev);
}


818
static netdev_tx_t
L
Linus Torvalds 已提交
819 820 821 822 823 824 825 826
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);

827
	dev->stats.tx_bytes += skb->len;
828

L
Linus Torvalds 已提交
829
	if (el3_debug > 4) {
830
		pr_debug("%s: el3_start_xmit(length = %u) called, status %4.4x.\n",
L
Linus Torvalds 已提交
831 832 833 834 835 836
			   dev->name, skb->len, inw(ioaddr + EL3_STATUS));
	}
#if 0
#ifndef final_version
	{	/* Error-checking code, delete someday. */
		ushort status = inw(ioaddr + EL3_STATUS);
837 838
		if (status & 0x0001 && 		/* IRQ line active, missed one. */
		    inw(ioaddr + EL3_STATUS) & 1) { 			/* Make sure. */
839
			pr_debug("%s: Missed interrupt, status then %04x now %04x"
L
Linus Torvalds 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
				   "  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) {
887
			if (tx_status & 0x38) dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
888 889 890 891 892
			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. */
		}
	}
893
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
894 895 896 897
}

/* The EL3 interrupt handler. */
static irqreturn_t
898
el3_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
899
{
900
	struct net_device *dev = dev_id;
L
Linus Torvalds 已提交
901 902 903 904 905 906 907 908 909 910 911
	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);
912
		pr_debug("%s: interrupt, status %4.4x.\n", dev->name, status);
L
Linus Torvalds 已提交
913 914 915 916 917 918 919 920 921 922
	}

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

		if (status & RxComplete)
			el3_rx(dev);

		if (status & TxAvailable) {
			if (el3_debug > 5)
923
				pr_debug("	TX room bit was handled.\n");
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
			/* 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) {
941
					if (tx_status & 0x38) dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
					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) {
961
			pr_err("%s: Infinite loop in interrupt, status %4.4x.\n",
L
Linus Torvalds 已提交
962 963 964 965 966 967 968 969 970 971
				   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) {
972
		pr_debug("%s: exiting interrupt, status %4.4x.\n", dev->name,
L
Linus Torvalds 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
			   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);
988
	el3_interrupt(dev->irq, dev);
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	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
	 *	stuff.
	 */
1003

L
Linus Torvalds 已提交
1004 1005 1006
	spin_lock_irqsave(&lp->lock, flags);
	update_stats(dev);
	spin_unlock_irqrestore(&lp->lock, flags);
1007
	return &dev->stats;
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
}

/*  Update statistics.  We change to register window 6, so this should be run
	single-threaded if the device is active. This is expected to be a rare
	operation, and it's simpler for the rest of the driver to assume that
	window 1 is always valid rather than use a special window-state variable.
	*/
static void update_stats(struct net_device *dev)
{
	int ioaddr = dev->base_addr;

	if (el3_debug > 5)
1020
		pr_debug("   Updating the statistics.\n");
L
Linus Torvalds 已提交
1021 1022 1023 1024
	/* Turn off statistics updates while reading. */
	outw(StatsDisable, ioaddr + EL3_CMD);
	/* Switch to the stats window, and read everything. */
	EL3WINDOW(6);
1025 1026
	dev->stats.tx_carrier_errors 	+= inb(ioaddr + 0);
	dev->stats.tx_heartbeat_errors	+= inb(ioaddr + 1);
L
Linus Torvalds 已提交
1027
	/* Multiple collisions. */	   inb(ioaddr + 2);
1028 1029 1030 1031
	dev->stats.collisions		+= inb(ioaddr + 3);
	dev->stats.tx_window_errors	+= inb(ioaddr + 4);
	dev->stats.rx_fifo_errors	+= inb(ioaddr + 5);
	dev->stats.tx_packets		+= inb(ioaddr + 6);
L
Linus Torvalds 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	/* Rx packets	*/		   inb(ioaddr + 7);
	/* Tx deferrals */		   inb(ioaddr + 8);
	inw(ioaddr + 10);	/* Total Rx and Tx octets. */
	inw(ioaddr + 12);

	/* Back to window 1, and turn statistics back on. */
	EL3WINDOW(1);
	outw(StatsEnable, ioaddr + EL3_CMD);
}

static int
el3_rx(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	short rx_status;

	if (el3_debug > 5)
1049
		pr_debug("   In rx_packet(), status %4.4x, rx_status %4.4x.\n",
L
Linus Torvalds 已提交
1050 1051 1052 1053 1054 1055
			   inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
	while ((rx_status = inw(ioaddr + RX_STATUS)) > 0) {
		if (rx_status & 0x4000) { /* Error, update stats. */
			short error = rx_status & 0x3800;

			outw(RxDiscard, ioaddr + EL3_CMD);
1056
			dev->stats.rx_errors++;
L
Linus Torvalds 已提交
1057
			switch (error) {
1058 1059 1060 1061 1062 1063
			case 0x0000:		dev->stats.rx_over_errors++; break;
			case 0x0800:		dev->stats.rx_length_errors++; break;
			case 0x1000:		dev->stats.rx_frame_errors++; break;
			case 0x1800:		dev->stats.rx_length_errors++; break;
			case 0x2000:		dev->stats.rx_frame_errors++; break;
			case 0x2800:		dev->stats.rx_crc_errors++; break;
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070
			}
		} else {
			short pkt_len = rx_status & 0x7ff;
			struct sk_buff *skb;

			skb = dev_alloc_skb(pkt_len+5);
			if (el3_debug > 4)
1071
				pr_debug("Receiving packet size %d status %4.4x.\n",
L
Linus Torvalds 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
					   pkt_len, rx_status);
			if (skb != NULL) {
				skb_reserve(skb, 2);     /* Align IP on 16 byte */

				/* 'skb->data' points to the start of sk_buff data area. */
				insl(ioaddr + RX_FIFO, skb_put(skb,pkt_len),
					 (pkt_len + 3) >> 2);

				outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
				skb->protocol = eth_type_trans(skb,dev);
				netif_rx(skb);
1083
				dev->stats.rx_bytes += pkt_len;
1084
				dev->stats.rx_packets++;
L
Linus Torvalds 已提交
1085 1086 1087
				continue;
			}
			outw(RxDiscard, ioaddr + EL3_CMD);
1088
			dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
1089
			if (el3_debug)
1090
				pr_debug("%s: Couldn't allocate a sk_buff of size %d.\n",
L
Linus Torvalds 已提交
1091 1092 1093 1094
					   dev->name, pkt_len);
		}
		inw(ioaddr + EL3_STATUS); 				/* Delay. */
		while (inw(ioaddr + EL3_STATUS) & 0x1000)
1095
			pr_debug("	Waiting for 3c509 to discard packet, status %x.\n",
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
				   inw(ioaddr + EL3_STATUS) );
	}

	return 0;
}

/*
 *     Set or clear the multicast filter for this adaptor.
 */
static void
set_multicast_list(struct net_device *dev)
{
	unsigned long flags;
	struct el3_private *lp = netdev_priv(dev);
	int ioaddr = dev->base_addr;
1111
	int mc_count = netdev_mc_count(dev);
L
Linus Torvalds 已提交
1112 1113 1114

	if (el3_debug > 1) {
		static int old;
1115 1116 1117 1118
		if (old != mc_count) {
			old = mc_count;
			pr_debug("%s: Setting Rx mode to %d addresses.\n",
				 dev->name, mc_count);
L
Linus Torvalds 已提交
1119 1120 1121 1122 1123 1124 1125
		}
	}
	spin_lock_irqsave(&lp->lock, flags);
	if (dev->flags&IFF_PROMISC) {
		outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
			 ioaddr + EL3_CMD);
	}
1126
	else if (mc_count || (dev->flags&IFF_ALLMULTI)) {
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
		outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast, ioaddr + EL3_CMD);
	}
	else
		outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
	spin_unlock_irqrestore(&lp->lock, flags);
}

static int
el3_close(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	struct el3_private *lp = netdev_priv(dev);
1139

L
Linus Torvalds 已提交
1140
	if (el3_debug > 2)
1141
		pr_debug("%s: Shutting down ethercard.\n", dev->name);
L
Linus Torvalds 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

	el3_down(dev);

	free_irq(dev->irq, dev);
	/* Switching back to window 0 disables the IRQ. */
	EL3WINDOW(0);
	if (lp->type != EL3_EISA) {
		/* But we explicitly zero the IRQ line select anyway. Don't do
		 * it on EISA cards, it prevents the module from getting an
		 * IRQ after unload+reload... */
		outw(0x0f00, ioaddr + WN0_IRQ);
	}

	return 0;
}

1158
static int
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
el3_link_ok(struct net_device *dev)
{
	int ioaddr = dev->base_addr;
	u16 tmp;

	EL3WINDOW(4);
	tmp = inw(ioaddr + WN4_MEDIA);
	EL3WINDOW(1);
	return tmp & (1<<11);
}

static int
el3_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
{
	u16 tmp;
	int ioaddr = dev->base_addr;
1175

L
Linus Torvalds 已提交
1176
	EL3WINDOW(0);
1177
	/* obtain current transceiver via WN4_MEDIA? */
L
Linus Torvalds 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	tmp = inw(ioaddr + WN0_ADDR_CONF);
	ecmd->transceiver = XCVR_INTERNAL;
	switch (tmp >> 14) {
	case 0:
		ecmd->port = PORT_TP;
		break;
	case 1:
		ecmd->port = PORT_AUI;
		ecmd->transceiver = XCVR_EXTERNAL;
		break;
	case 3:
		ecmd->port = PORT_BNC;
	default:
		break;
	}

	ecmd->duplex = DUPLEX_HALF;
	ecmd->supported = 0;
	tmp = inw(ioaddr + WN0_CONF_CTRL);
	if (tmp & (1<<13))
		ecmd->supported |= SUPPORTED_AUI;
	if (tmp & (1<<12))
		ecmd->supported |= SUPPORTED_BNC;
	if (tmp & (1<<9)) {
		ecmd->supported |= SUPPORTED_TP | SUPPORTED_10baseT_Half |
				SUPPORTED_10baseT_Full;	/* hmm... */
		EL3WINDOW(4);
		tmp = inw(ioaddr + WN4_NETDIAG);
		if (tmp & FD_ENABLE)
			ecmd->duplex = DUPLEX_FULL;
	}

1210
	ethtool_cmd_speed_set(ecmd, SPEED_10);
L
Linus Torvalds 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	EL3WINDOW(1);
	return 0;
}

static int
el3_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
{
	u16 tmp;
	int ioaddr = dev->base_addr;

	if (ecmd->speed != SPEED_10)
		return -EINVAL;
	if ((ecmd->duplex != DUPLEX_HALF) && (ecmd->duplex != DUPLEX_FULL))
		return -EINVAL;
	if ((ecmd->transceiver != XCVR_INTERNAL) && (ecmd->transceiver != XCVR_EXTERNAL))
		return -EINVAL;

	/* change XCVR type */
	EL3WINDOW(0);
	tmp = inw(ioaddr + WN0_ADDR_CONF);
	switch (ecmd->port) {
	case PORT_TP:
		tmp &= ~(3<<14);
		dev->if_port = 0;
		break;
	case PORT_AUI:
		tmp |= (1<<14);
		dev->if_port = 1;
		break;
	case PORT_BNC:
		tmp |= (3<<14);
		dev->if_port = 3;
		break;
	default:
		return -EINVAL;
	}

	outw(tmp, ioaddr + WN0_ADDR_CONF);
	if (dev->if_port == 3) {
		/* fire up the DC-DC convertor if BNC gets enabled */
		tmp = inw(ioaddr + WN0_ADDR_CONF);
		if (tmp & (3 << 14)) {
			outw(StartCoax, ioaddr + EL3_CMD);
			udelay(800);
		} else
			return -EIO;
	}

	EL3WINDOW(4);
	tmp = inw(ioaddr + WN4_NETDIAG);
	if (ecmd->duplex == DUPLEX_FULL)
		tmp |= FD_ENABLE;
	else
		tmp &= ~FD_ENABLE;
	outw(tmp, ioaddr + WN4_NETDIAG);
	EL3WINDOW(1);

	return 0;
}

static void el3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
	strcpy(info->driver, DRV_NAME);
	strcpy(info->version, DRV_VERSION);
}

static int el3_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
{
	struct el3_private *lp = netdev_priv(dev);
	int ret;

	spin_lock_irq(&lp->lock);
	ret = el3_netdev_get_ecmd(dev, ecmd);
	spin_unlock_irq(&lp->lock);
	return ret;
}

static int el3_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
{
	struct el3_private *lp = netdev_priv(dev);
	int ret;

	spin_lock_irq(&lp->lock);
	ret = el3_netdev_set_ecmd(dev, ecmd);
	spin_unlock_irq(&lp->lock);
	return ret;
}

static u32 el3_get_link(struct net_device *dev)
{
	struct el3_private *lp = netdev_priv(dev);
	u32 ret;

	spin_lock_irq(&lp->lock);
	ret = el3_link_ok(dev);
	spin_unlock_irq(&lp->lock);
	return ret;
}

static u32 el3_get_msglevel(struct net_device *dev)
{
	return el3_debug;
}

static void el3_set_msglevel(struct net_device *dev, u32 v)
{
	el3_debug = v;
}

1320
static const struct ethtool_ops ethtool_ops = {
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	.get_drvinfo = el3_get_drvinfo,
	.get_settings = el3_get_settings,
	.set_settings = el3_set_settings,
	.get_link = el3_get_link,
	.get_msglevel = el3_get_msglevel,
	.set_msglevel = el3_set_msglevel,
};

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

	netif_stop_queue(dev);

	/* Turn off statistics ASAP.  We update lp->stats below. */
	outw(StatsDisable, ioaddr + EL3_CMD);

	/* Disable the receiver and transmitter. */
	outw(RxDisable, ioaddr + EL3_CMD);
	outw(TxDisable, ioaddr + EL3_CMD);

	if (dev->if_port == 3)
		/* Turn off thinnet power.  Green! */
		outw(StopCoax, ioaddr + EL3_CMD);
	else if (dev->if_port == 0) {
		/* Disable link beat and jabber, if_port may change here next open(). */
		EL3WINDOW(4);
		outw(inw(ioaddr + WN4_MEDIA) & ~MEDIA_TP, ioaddr + WN4_MEDIA);
	}

	outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);

	update_stats(dev);
}

static void
el3_up(struct net_device *dev)
{
	int i, sw_info, net_diag;
	int ioaddr = dev->base_addr;
1362

L
Linus Torvalds 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
	/* Activating the board required and does no harm otherwise */
	outw(0x0001, ioaddr + 4);

	/* Set the IRQ line. */
	outw((dev->irq << 12) | 0x0f00, ioaddr + WN0_IRQ);

	/* Set the station address in window 2 each time opened. */
	EL3WINDOW(2);

	for (i = 0; i < 6; i++)
		outb(dev->dev_addr[i], ioaddr + i);

	if ((dev->if_port & 0x03) == 3) /* BNC interface */
		/* Start the thinnet transceiver. We should really wait 50ms...*/
		outw(StartCoax, ioaddr + EL3_CMD);
	else if ((dev->if_port & 0x03) == 0) { /* 10baseT interface */
		/* Combine secondary sw_info word (the adapter level) and primary
			sw_info word (duplex setting plus other useless bits) */
		EL3WINDOW(0);
1382
		sw_info = (read_eeprom(ioaddr, 0x14) & 0x400f) |
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387
			(read_eeprom(ioaddr, 0x0d) & 0xBff0);

		EL3WINDOW(4);
		net_diag = inw(ioaddr + WN4_NETDIAG);
		net_diag = (net_diag | FD_ENABLE); /* temporarily assume full-duplex will be set */
1388
		pr_info("%s: ", dev->name);
L
Linus Torvalds 已提交
1389 1390 1391 1392
		switch (dev->if_port & 0x0c) {
			case 12:
				/* force full-duplex mode if 3c5x9b */
				if (sw_info & 0x000f) {
1393
					pr_cont("Forcing 3c5x9b full-duplex mode");
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398
					break;
				}
			case 8:
				/* set full-duplex mode based on eeprom config setting */
				if ((sw_info & 0x000f) && (sw_info & 0x8000)) {
1399
					pr_cont("Setting 3c5x9b full-duplex mode (from EEPROM configuration bit)");
L
Linus Torvalds 已提交
1400 1401 1402 1403
					break;
				}
			default:
				/* xcvr=(0 || 4) OR user has an old 3c5x9 non "B" model */
1404
				pr_cont("Setting 3c5x9/3c5x9B half-duplex mode");
L
Linus Torvalds 已提交
1405 1406 1407 1408
				net_diag = (net_diag & ~FD_ENABLE); /* disable full duplex */
		}

		outw(net_diag, ioaddr + WN4_NETDIAG);
1409
		pr_cont(" if_port: %d, sw_info: %4.4x\n", dev->if_port, sw_info);
L
Linus Torvalds 已提交
1410
		if (el3_debug > 3)
1411
			pr_debug("%s: 3c5x9 net diag word is now: %4.4x.\n", dev->name, net_diag);
L
Linus Torvalds 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
		/* Enable link beat and jabber check. */
		outw(inw(ioaddr + WN4_MEDIA) | MEDIA_TP, ioaddr + WN4_MEDIA);
	}

	/* Switch to the stats window, and clear all stats by reading. */
	outw(StatsDisable, ioaddr + EL3_CMD);
	EL3WINDOW(6);
	for (i = 0; i < 9; i++)
		inb(ioaddr + i);
	inw(ioaddr + 10);
	inw(ioaddr + 12);

	/* Switch to register set 1 for normal use. */
	EL3WINDOW(1);

	/* Accept b-case and phys addr only. */
	outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
	outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */

	outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
	outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
	/* Allow status bits to be seen. */
	outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
	/* Ack all pending events, and set active indicator mask. */
	outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
		 ioaddr + EL3_CMD);
	outw(SetIntrEnb | IntLatch|TxAvailable|TxComplete|RxComplete|StatsFull,
		 ioaddr + EL3_CMD);

	netif_start_queue(dev);
}

/* Power Management support functions */
1445
#ifdef CONFIG_PM
L
Linus Torvalds 已提交
1446 1447

static int
1448
el3_suspend(struct device *pdev, pm_message_t state)
L
Linus Torvalds 已提交
1449 1450 1451 1452 1453
{
	unsigned long flags;
	struct net_device *dev;
	struct el3_private *lp;
	int ioaddr;
1454

1455
	dev = dev_get_drvdata(pdev);
L
Linus Torvalds 已提交
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	lp = netdev_priv(dev);
	ioaddr = dev->base_addr;

	spin_lock_irqsave(&lp->lock, flags);

	if (netif_running(dev))
		netif_device_detach(dev);

	el3_down(dev);
	outw(PowerDown, ioaddr + EL3_CMD);

	spin_unlock_irqrestore(&lp->lock, flags);
	return 0;
}

static int
1472
el3_resume(struct device *pdev)
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477
{
	unsigned long flags;
	struct net_device *dev;
	struct el3_private *lp;
	int ioaddr;
1478

1479
	dev = dev_get_drvdata(pdev);
L
Linus Torvalds 已提交
1480 1481 1482 1483 1484 1485
	lp = netdev_priv(dev);
	ioaddr = dev->base_addr;

	spin_lock_irqsave(&lp->lock, flags);

	outw(PowerUp, ioaddr + EL3_CMD);
1486
	EL3WINDOW(0);
L
Linus Torvalds 已提交
1487 1488 1489 1490
	el3_up(dev);

	if (netif_running(dev))
		netif_device_attach(dev);
1491

L
Linus Torvalds 已提交
1492 1493 1494 1495
	spin_unlock_irqrestore(&lp->lock, flags);
	return 0;
}

1496
#endif /* CONFIG_PM */
L
Linus Torvalds 已提交
1497 1498 1499 1500 1501 1502 1503

module_param(debug,int, 0);
module_param_array(irq, int, NULL, 0);
module_param(max_interrupt_work, int, 0);
MODULE_PARM_DESC(debug, "debug level (0-6)");
MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
MODULE_PARM_DESC(max_interrupt_work, "maximum events handled per interrupt");
1504
#ifdef CONFIG_PNP
L
Linus Torvalds 已提交
1505 1506
module_param(nopnp, int, 0);
MODULE_PARM_DESC(nopnp, "disable ISA PnP support (0-1)");
1507 1508
#endif	/* CONFIG_PNP */
MODULE_DESCRIPTION("3Com Etherlink III (3c509, 3c509B, 3c529, 3c579) ethernet driver");
L
Linus Torvalds 已提交
1509 1510 1511 1512
MODULE_LICENSE("GPL");

static int __init el3_init_module(void)
{
1513
	int ret = 0;
L
Linus Torvalds 已提交
1514 1515 1516 1517

	if (debug >= 0)
		el3_debug = debug;

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
#ifdef CONFIG_PNP
	if (!nopnp) {
		ret = pnp_register_driver(&el3_pnp_driver);
		if (!ret)
			pnp_registered = 1;
	}
#endif
	/* Select an open I/O location at 0x1*0 to do ISA contention select. */
	/* Start with 0x110 to avoid some sound cards.*/
	for (id_port = 0x110 ; id_port < 0x200; id_port += 0x10) {
		if (!request_region(id_port, 1, "3c509-control"))
			continue;
		outb(0x00, id_port);
		outb(0xff, id_port);
		if (inb(id_port) & 0x01)
			break;
		else
			release_region(id_port, 1);
	}
	if (id_port >= 0x200) {
		id_port = 0;
1539
		pr_err("No I/O port available for 3c509 activation.\n");
1540 1541 1542 1543
	} else {
		ret = isa_register_driver(&el3_isa_driver, EL3_MAX_CARDS);
		if (!ret)
			isa_registered = 1;
L
Linus Torvalds 已提交
1544 1545
	}
#ifdef CONFIG_EISA
1546
	ret = eisa_driver_register(&el3_eisa_driver);
1547 1548
	if (!ret)
		eisa_registered = 1;
L
Linus Torvalds 已提交
1549 1550
#endif
#ifdef CONFIG_MCA
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
	ret = mca_register_driver(&el3_mca_driver);
	if (!ret)
		mca_registered = 1;
#endif

#ifdef CONFIG_PNP
	if (pnp_registered)
		ret = 0;
#endif
	if (isa_registered)
		ret = 0;
#ifdef CONFIG_EISA
	if (eisa_registered)
		ret = 0;
#endif
#ifdef CONFIG_MCA
	if (mca_registered)
		ret = 0;
L
Linus Torvalds 已提交
1569
#endif
1570
	return ret;
L
Linus Torvalds 已提交
1571 1572 1573 1574
}

static void __exit el3_cleanup_module(void)
{
1575 1576 1577 1578 1579 1580 1581 1582
#ifdef CONFIG_PNP
	if (pnp_registered)
		pnp_unregister_driver(&el3_pnp_driver);
#endif
	if (isa_registered)
		isa_unregister_driver(&el3_isa_driver);
	if (id_port)
		release_region(id_port, 1);
L
Linus Torvalds 已提交
1583
#ifdef CONFIG_EISA
1584 1585
	if (eisa_registered)
		eisa_driver_unregister(&el3_eisa_driver);
L
Linus Torvalds 已提交
1586 1587
#endif
#ifdef CONFIG_MCA
1588 1589
	if (mca_registered)
		mca_unregister_driver(&el3_mca_driver);
L
Linus Torvalds 已提交
1590 1591 1592 1593 1594
#endif
}

module_init (el3_init_module);
module_exit (el3_cleanup_module);