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