1/*
2 * Linux Ethernet device driver for the 3Com Etherlink Plus (3C505)
3 *      By Craig Southeren, Juha Laiho and Philip Blundell
4 *
5 * 3c505.c      This module implements an interface to the 3Com
6 *              Etherlink Plus (3c505) Ethernet card. Linux device
7 *              driver interface reverse engineered from the Linux 3C509
8 *              device drivers. Some 3C505 information gleaned from
9 *              the Crynwr packet driver. Still this driver would not
10 *              be here without 3C505 technical reference provided by
11 *              3Com.
12 *
13 * $Id: 3c505.c,v 1.1.1.1 2007/08/03 18:52:44 Exp $
14 *
15 * Authors:     Linux 3c505 device driver by
16 *                      Craig Southeren, <craigs@ineluki.apana.org.au>
17 *              Final debugging by
18 *                      Andrew Tridgell, <tridge@nimbus.anu.edu.au>
19 *              Auto irq/address, tuning, cleanup and v1.1.4+ kernel mods by
20 *                      Juha Laiho, <jlaiho@ichaos.nullnet.fi>
21 *              Linux 3C509 driver by
22 *                      Donald Becker, <becker@super.org>
23 *			(Now at <becker@scyld.com>)
24 *              Crynwr packet driver by
25 *                      Krishnan Gopalan and Gregg Stefancik,
26 *                      Clemson University Engineering Computer Operations.
27 *                      Portions of the code have been adapted from the 3c505
28 *                         driver for NCSA Telnet by Bruce Orchard and later
29 *                         modified by Warren Van Houten and krus@diku.dk.
30 *              3C505 technical information provided by
31 *                      Terry Murphy, of 3Com Network Adapter Division
32 *              Linux 1.3.0 changes by
33 *                      Alan Cox <Alan.Cox@linux.org>
34 *              More debugging, DMA support, currently maintained by
35 *                      Philip Blundell <philb@gnu.org>
36 *              Multicard/soft configurable dma channel/rev 2 hardware support
37 *                      by Christopher Collins <ccollins@pcug.org.au>
38 *		Ethtool support (jgarzik), 11/17/2001
39 */
40
41#define DRV_NAME	"3c505"
42#define DRV_VERSION	"1.10a"
43
44
45/* Theory of operation:
46 *
47 * The 3c505 is quite an intelligent board.  All communication with it is done
48 * by means of Primary Command Blocks (PCBs); these are transferred using PIO
49 * through the command register.  The card has 256k of on-board RAM, which is
50 * used to buffer received packets.  It might seem at first that more buffers
51 * are better, but in fact this isn't true.  From my tests, it seems that
52 * more than about 10 buffers are unnecessary, and there is a noticeable
53 * performance hit in having more active on the card.  So the majority of the
54 * card's memory isn't, in fact, used.  Sadly, the card only has one transmit
55 * buffer and, short of loading our own firmware into it (which is what some
56 * drivers resort to) there's nothing we can do about this.
57 *
58 * We keep up to 4 "receive packet" commands active on the board at a time.
59 * When a packet comes in, so long as there is a receive command active, the
60 * board will send us a "packet received" PCB and then add the data for that
61 * packet to the DMA queue.  If a DMA transfer is not already in progress, we
62 * set one up to start uploading the data.  We have to maintain a list of
63 * backlogged receive packets, because the card may decide to tell us about
64 * a newly-arrived packet at any time, and we may not be able to start a DMA
65 * transfer immediately (ie one may already be going on).  We can't NAK the
66 * PCB, because then it would throw the packet away.
67 *
68 * Trying to send a PCB to the card at the wrong moment seems to have bad
69 * effects.  If we send it a transmit PCB while a receive DMA is happening,
70 * it will just NAK the PCB and so we will have wasted our time.  Worse, it
71 * sometimes seems to interrupt the transfer.  The majority of the low-level
72 * code is protected by one huge semaphore -- "busy" -- which is set whenever
73 * it probably isn't safe to do anything to the card.  The receive routine
74 * must gain a lock on "busy" before it can start a DMA transfer, and the
75 * transmit routine must gain a lock before it sends the first PCB to the card.
76 * The send_pcb() routine also has an internal semaphore to protect it against
77 * being re-entered (which would be disastrous) -- this is needed because
78 * several things can happen asynchronously (re-priming the receiver and
79 * asking the card for statistics, for example).  send_pcb() will also refuse
80 * to talk to the card at all if a DMA upload is happening.  The higher-level
81 * networking code will reschedule a later retry if some part of the driver
82 * is blocked.  In practice, this doesn't seem to happen very often.
83 */
84
85/* This driver may now work with revision 2.x hardware, since all the read
86 * operations on the HCR have been removed (we now keep our own softcopy).
87 * But I don't have an old card to test it on.
88 *
89 * This has had the bad effect that the autoprobe routine is now a bit
90 * less friendly to other devices.  However, it was never very good.
91 * before, so I doubt it will hurt anybody.
92 */
93
94/* The driver is a mess.  I took Craig's and Juha's code, and hacked it firstly
95 * to make it more reliable, and secondly to add DMA mode.  Many things could
96 * probably be done better; the concurrency protection is particularly awful.
97 */
98
99#include <linux/module.h>
100#include <linux/kernel.h>
101#include <linux/string.h>
102#include <linux/interrupt.h>
103#include <linux/errno.h>
104#include <linux/in.h>
105#include <linux/slab.h>
106#include <linux/ioport.h>
107#include <linux/spinlock.h>
108#include <linux/ethtool.h>
109#include <linux/delay.h>
110#include <linux/bitops.h>
111
112#include <asm/uaccess.h>
113#include <asm/io.h>
114#include <asm/dma.h>
115
116#include <linux/netdevice.h>
117#include <linux/etherdevice.h>
118#include <linux/skbuff.h>
119#include <linux/init.h>
120
121#include "3c505.h"
122
123/*********************************************************
124 *
125 *  define debug messages here as common strings to reduce space
126 *
127 *********************************************************/
128
129static const char filename[] = __FILE__;
130
131static const char timeout_msg[] = "*** timeout at %s:%s (line %d) ***\n";
132#define TIMEOUT_MSG(lineno) \
133	printk(timeout_msg, filename,__FUNCTION__,(lineno))
134
135static const char invalid_pcb_msg[] =
136"*** invalid pcb length %d at %s:%s (line %d) ***\n";
137#define INVALID_PCB_MSG(len) \
138	printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
139
140static char search_msg[] __initdata = KERN_INFO "%s: Looking for 3c505 adapter at address %#x...";
141
142static char stilllooking_msg[] __initdata = "still looking...";
143
144static char found_msg[] __initdata = "found.\n";
145
146static char notfound_msg[] __initdata = "not found (reason = %d)\n";
147
148static char couldnot_msg[] __initdata = KERN_INFO "%s: 3c505 not found\n";
149
150/*********************************************************
151 *
152 *  various other debug stuff
153 *
154 *********************************************************/
155
156#ifdef ELP_DEBUG
157static int elp_debug = ELP_DEBUG;
158#else
159static int elp_debug;
160#endif
161#define debug elp_debug
162
163/*
164 *  0 = no messages (well, some)
165 *  1 = messages when high level commands performed
166 *  2 = messages when low level commands performed
167 *  3 = messages when interrupts received
168 */
169
170/*****************************************************************
171 *
172 * useful macros
173 *
174 *****************************************************************/
175
176#ifndef	TRUE
177#define	TRUE	1
178#endif
179
180#ifndef	FALSE
181#define	FALSE	0
182#endif
183
184
185/*****************************************************************
186 *
187 * List of I/O-addresses we try to auto-sense
188 * Last element MUST BE 0!
189 *****************************************************************/
190
191static int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
192
193/* Dma Memory related stuff */
194
195static unsigned long dma_mem_alloc(int size)
196{
197	int order = get_order(size);
198	return __get_dma_pages(GFP_KERNEL, order);
199}
200
201
202/*****************************************************************
203 *
204 * Functions for I/O (note the inline !)
205 *
206 *****************************************************************/
207
208static inline unsigned char inb_status(unsigned int base_addr)
209{
210	return inb(base_addr + PORT_STATUS);
211}
212
213static inline int inb_command(unsigned int base_addr)
214{
215	return inb(base_addr + PORT_COMMAND);
216}
217
218static inline void outb_control(unsigned char val, struct net_device *dev)
219{
220	outb(val, dev->base_addr + PORT_CONTROL);
221	((elp_device *)(dev->priv))->hcr_val = val;
222}
223
224#define HCR_VAL(x)   (((elp_device *)((x)->priv))->hcr_val)
225
226static inline void outb_command(unsigned char val, unsigned int base_addr)
227{
228	outb(val, base_addr + PORT_COMMAND);
229}
230
231static inline unsigned int backlog_next(unsigned int n)
232{
233	return (n + 1) % BACKLOG_SIZE;
234}
235
236/*****************************************************************
237 *
238 *  useful functions for accessing the adapter
239 *
240 *****************************************************************/
241
242/*
243 * use this routine when accessing the ASF bits as they are
244 * changed asynchronously by the adapter
245 */
246
247/* get adapter PCB status */
248#define	GET_ASF(addr) \
249	(get_status(addr)&ASF_PCB_MASK)
250
251static inline int get_status(unsigned int base_addr)
252{
253	unsigned long timeout = jiffies + 10*HZ/100;
254	register int stat1;
255	do {
256		stat1 = inb_status(base_addr);
257	} while (stat1 != inb_status(base_addr) && time_before(jiffies, timeout));
258	if (time_after_eq(jiffies, timeout))
259		TIMEOUT_MSG(__LINE__);
260	return stat1;
261}
262
263static inline void set_hsf(struct net_device *dev, int hsf)
264{
265	elp_device *adapter = dev->priv;
266	unsigned long flags;
267
268	spin_lock_irqsave(&adapter->lock, flags);
269	outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
270	spin_unlock_irqrestore(&adapter->lock, flags);
271}
272
273static int start_receive(struct net_device *, pcb_struct *);
274
275static inline void adapter_reset(struct net_device *dev)
276{
277	unsigned long timeout;
278	elp_device *adapter = dev->priv;
279	unsigned char orig_hcr = adapter->hcr_val;
280
281	outb_control(0, dev);
282
283	if (inb_status(dev->base_addr) & ACRF) {
284		do {
285			inb_command(dev->base_addr);
286			timeout = jiffies + 2*HZ/100;
287			while (time_before_eq(jiffies, timeout) && !(inb_status(dev->base_addr) & ACRF));
288		} while (inb_status(dev->base_addr) & ACRF);
289		set_hsf(dev, HSF_PCB_NAK);
290	}
291	outb_control(adapter->hcr_val | ATTN | DIR, dev);
292	mdelay(10);
293	outb_control(adapter->hcr_val & ~ATTN, dev);
294	mdelay(10);
295	outb_control(adapter->hcr_val | FLSH, dev);
296	mdelay(10);
297	outb_control(adapter->hcr_val & ~FLSH, dev);
298	mdelay(10);
299
300	outb_control(orig_hcr, dev);
301	if (!start_receive(dev, &adapter->tx_pcb))
302		printk(KERN_ERR "%s: start receive command failed \n", dev->name);
303}
304
305/* Check to make sure that a DMA transfer hasn't timed out.  This should
306 * never happen in theory, but seems to occur occasionally if the card gets
307 * prodded at the wrong time.
308 */
309static inline void check_3c505_dma(struct net_device *dev)
310{
311	elp_device *adapter = dev->priv;
312	if (adapter->dmaing && time_after(jiffies, adapter->current_dma.start_time + 10)) {
313		unsigned long flags, f;
314		printk(KERN_ERR "%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
315		spin_lock_irqsave(&adapter->lock, flags);
316		adapter->dmaing = 0;
317		adapter->busy = 0;
318
319		f=claim_dma_lock();
320		disable_dma(dev->dma);
321		release_dma_lock(f);
322
323		if (adapter->rx_active)
324			adapter->rx_active--;
325		outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
326		spin_unlock_irqrestore(&adapter->lock, flags);
327	}
328}
329
330/* Primitive functions used by send_pcb() */
331static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
332{
333	unsigned long timeout;
334	outb_command(byte, base_addr);
335	for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
336		if (inb_status(base_addr) & HCRE)
337			return FALSE;
338	}
339	printk(KERN_WARNING "3c505: send_pcb_slow timed out\n");
340	return TRUE;
341}
342
343static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
344{
345	unsigned int timeout;
346	outb_command(byte, base_addr);
347	for (timeout = 0; timeout < 40000; timeout++) {
348		if (inb_status(base_addr) & HCRE)
349			return FALSE;
350	}
351	printk(KERN_WARNING "3c505: send_pcb_fast timed out\n");
352	return TRUE;
353}
354
355/* Check to see if the receiver needs restarting, and kick it if so */
356static inline void prime_rx(struct net_device *dev)
357{
358	elp_device *adapter = dev->priv;
359	while (adapter->rx_active < ELP_RX_PCBS && netif_running(dev)) {
360		if (!start_receive(dev, &adapter->itx_pcb))
361			break;
362	}
363}
364
365/*****************************************************************
366 *
367 * send_pcb
368 *   Send a PCB to the adapter.
369 *
370 *	output byte to command reg  --<--+
371 *	wait until HCRE is non zero      |
372 *	loop until all bytes sent   -->--+
373 *	set HSF1 and HSF2 to 1
374 *	output pcb length
375 *	wait until ASF give ACK or NAK
376 *	set HSF1 and HSF2 to 0
377 *
378 *****************************************************************/
379
380/* This can be quite slow -- the adapter is allowed to take up to 40ms
381 * to respond to the initial interrupt.
382 *
383 * We run initially with interrupts turned on, but with a semaphore set
384 * so that nobody tries to re-enter this code.  Once the first byte has
385 * gone through, we turn interrupts off and then send the others (the
386 * timeout is reduced to 500us).
387 */
388
389static int send_pcb(struct net_device *dev, pcb_struct * pcb)
390{
391	int i;
392	unsigned long timeout;
393	elp_device *adapter = dev->priv;
394	unsigned long flags;
395
396	check_3c505_dma(dev);
397
398	if (adapter->dmaing && adapter->current_dma.direction == 0)
399		return FALSE;
400
401	/* Avoid contention */
402	if (test_and_set_bit(1, &adapter->send_pcb_semaphore)) {
403		if (elp_debug >= 3) {
404			printk(KERN_DEBUG "%s: send_pcb entered while threaded\n", dev->name);
405		}
406		return FALSE;
407	}
408	/*
409	 * load each byte into the command register and
410	 * wait for the HCRE bit to indicate the adapter
411	 * had read the byte
412	 */
413	set_hsf(dev, 0);
414
415	if (send_pcb_slow(dev->base_addr, pcb->command))
416		goto abort;
417
418	spin_lock_irqsave(&adapter->lock, flags);
419
420	if (send_pcb_fast(dev->base_addr, pcb->length))
421		goto sti_abort;
422
423	for (i = 0; i < pcb->length; i++) {
424		if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
425			goto sti_abort;
426	}
427
428	outb_control(adapter->hcr_val | 3, dev);	/* signal end of PCB */
429	outb_command(2 + pcb->length, dev->base_addr);
430
431	/* now wait for the acknowledgement */
432	spin_unlock_irqrestore(&adapter->lock, flags);
433
434	for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
435		switch (GET_ASF(dev->base_addr)) {
436		case ASF_PCB_ACK:
437			adapter->send_pcb_semaphore = 0;
438			return TRUE;
439
440		case ASF_PCB_NAK:
441#ifdef ELP_DEBUG
442			printk(KERN_DEBUG "%s: send_pcb got NAK\n", dev->name);
443#endif
444			goto abort;
445		}
446	}
447
448	if (elp_debug >= 1)
449		printk(KERN_DEBUG "%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
450	goto abort;
451
452      sti_abort:
453	spin_unlock_irqrestore(&adapter->lock, flags);
454      abort:
455	adapter->send_pcb_semaphore = 0;
456	return FALSE;
457}
458
459
460/*****************************************************************
461 *
462 * receive_pcb
463 *   Read a PCB from the adapter
464 *
465 *	wait for ACRF to be non-zero        ---<---+
466 *	input a byte                               |
467 *	if ASF1 and ASF2 were not both one         |
468 *		before byte was read, loop      --->---+
469 *	set HSF1 and HSF2 for ack
470 *
471 *****************************************************************/
472
473static int receive_pcb(struct net_device *dev, pcb_struct * pcb)
474{
475	int i, j;
476	int total_length;
477	int stat;
478	unsigned long timeout;
479	unsigned long flags;
480
481	elp_device *adapter = dev->priv;
482
483	set_hsf(dev, 0);
484
485	/* get the command code */
486	timeout = jiffies + 2*HZ/100;
487	while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
488	if (time_after_eq(jiffies, timeout)) {
489		TIMEOUT_MSG(__LINE__);
490		return FALSE;
491	}
492	pcb->command = inb_command(dev->base_addr);
493
494	/* read the data length */
495	timeout = jiffies + 3*HZ/100;
496	while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
497	if (time_after_eq(jiffies, timeout)) {
498		TIMEOUT_MSG(__LINE__);
499		printk(KERN_INFO "%s: status %02x\n", dev->name, stat);
500		return FALSE;
501	}
502	pcb->length = inb_command(dev->base_addr);
503
504	if (pcb->length > MAX_PCB_DATA) {
505		INVALID_PCB_MSG(pcb->length);
506		adapter_reset(dev);
507		return FALSE;
508	}
509	/* read the data */
510	spin_lock_irqsave(&adapter->lock, flags);
511	i = 0;
512	do {
513		j = 0;
514		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
515		pcb->data.raw[i++] = inb_command(dev->base_addr);
516		if (i > MAX_PCB_DATA)
517			INVALID_PCB_MSG(i);
518	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
519	spin_unlock_irqrestore(&adapter->lock, flags);
520	if (j >= 20000) {
521		TIMEOUT_MSG(__LINE__);
522		return FALSE;
523	}
524	/* woops, the last "data" byte was really the length! */
525	total_length = pcb->data.raw[--i];
526
527	/* safety check total length vs data length */
528	if (total_length != (pcb->length + 2)) {
529		if (elp_debug >= 2)
530			printk(KERN_WARNING "%s: mangled PCB received\n", dev->name);
531		set_hsf(dev, HSF_PCB_NAK);
532		return FALSE;
533	}
534
535	if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
536		if (test_and_set_bit(0, (void *) &adapter->busy)) {
537			if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
538				set_hsf(dev, HSF_PCB_NAK);
539				printk(KERN_WARNING "%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
540				pcb->command = 0;
541				return TRUE;
542			} else {
543				pcb->command = 0xff;
544			}
545		}
546	}
547	set_hsf(dev, HSF_PCB_ACK);
548	return TRUE;
549}
550
551/******************************************************
552 *
553 *  queue a receive command on the adapter so we will get an
554 *  interrupt when a packet is received.
555 *
556 ******************************************************/
557
558static int start_receive(struct net_device *dev, pcb_struct * tx_pcb)
559{
560	int status;
561	elp_device *adapter = dev->priv;
562
563	if (elp_debug >= 3)
564		printk(KERN_DEBUG "%s: restarting receiver\n", dev->name);
565	tx_pcb->command = CMD_RECEIVE_PACKET;
566	tx_pcb->length = sizeof(struct Rcv_pkt);
567	tx_pcb->data.rcv_pkt.buf_seg
568	    = tx_pcb->data.rcv_pkt.buf_ofs = 0;		/* Unused */
569	tx_pcb->data.rcv_pkt.buf_len = 1600;
570	tx_pcb->data.rcv_pkt.timeout = 0;	/* set timeout to zero */
571	status = send_pcb(dev, tx_pcb);
572	if (status)
573		adapter->rx_active++;
574	return status;
575}
576
577/******************************************************
578 *
579 * extract a packet from the adapter
580 * this routine is only called from within the interrupt
581 * service routine, so no cli/sti calls are needed
582 * note that the length is always assumed to be even
583 *
584 ******************************************************/
585
586static void receive_packet(struct net_device *dev, int len)
587{
588	int rlen;
589	elp_device *adapter = dev->priv;
590	void *target;
591	struct sk_buff *skb;
592	unsigned long flags;
593
594	rlen = (len + 1) & ~1;
595	skb = dev_alloc_skb(rlen + 2);
596
597	if (!skb) {
598		printk(KERN_WARNING "%s: memory squeeze, dropping packet\n", dev->name);
599		target = adapter->dma_buffer;
600		adapter->current_dma.target = NULL;
601		return;
602	}
603
604	skb_reserve(skb, 2);
605	target = skb_put(skb, rlen);
606	if ((unsigned long)(target + rlen) >= MAX_DMA_ADDRESS) {
607		adapter->current_dma.target = target;
608		target = adapter->dma_buffer;
609	} else {
610		adapter->current_dma.target = NULL;
611	}
612
613	/* if this happens, we die */
614	if (test_and_set_bit(0, (void *) &adapter->dmaing))
615		printk(KERN_ERR "%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
616
617	adapter->current_dma.direction = 0;
618	adapter->current_dma.length = rlen;
619	adapter->current_dma.skb = skb;
620	adapter->current_dma.start_time = jiffies;
621
622	outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
623
624	flags=claim_dma_lock();
625	disable_dma(dev->dma);
626	clear_dma_ff(dev->dma);
627	set_dma_mode(dev->dma, 0x04);	/* dma read */
628	set_dma_addr(dev->dma, isa_virt_to_bus(target));
629	set_dma_count(dev->dma, rlen);
630	enable_dma(dev->dma);
631	release_dma_lock(flags);
632
633	if (elp_debug >= 3) {
634		printk(KERN_DEBUG "%s: rx DMA transfer started\n", dev->name);
635	}
636
637	if (adapter->rx_active)
638		adapter->rx_active--;
639
640	if (!adapter->busy)
641		printk(KERN_WARNING "%s: receive_packet called, busy not set.\n", dev->name);
642}
643
644/******************************************************
645 *
646 * interrupt handler
647 *
648 ******************************************************/
649
650static irqreturn_t elp_interrupt(int irq, void *dev_id)
651{
652	int len;
653	int dlen;
654	int icount = 0;
655	struct net_device *dev;
656	elp_device *adapter;
657	unsigned long timeout;
658
659	dev = dev_id;
660	adapter = (elp_device *) dev->priv;
661
662	spin_lock(&adapter->lock);
663
664	do {
665		/*
666		 * has a DMA transfer finished?
667		 */
668		if (inb_status(dev->base_addr) & DONE) {
669			if (!adapter->dmaing) {
670				printk(KERN_WARNING "%s: phantom DMA completed\n", dev->name);
671			}
672			if (elp_debug >= 3) {
673				printk(KERN_DEBUG "%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
674			}
675
676			outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
677			if (adapter->current_dma.direction) {
678				dev_kfree_skb_irq(adapter->current_dma.skb);
679			} else {
680				struct sk_buff *skb = adapter->current_dma.skb;
681				if (skb) {
682					if (adapter->current_dma.target) {
683				  	/* have already done the skb_put() */
684				  	memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
685					}
686					skb->protocol = eth_type_trans(skb,dev);
687					adapter->stats.rx_bytes += skb->len;
688					netif_rx(skb);
689					dev->last_rx = jiffies;
690				}
691			}
692			adapter->dmaing = 0;
693			if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
694				int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
695				adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
696				if (elp_debug >= 2)
697					printk(KERN_DEBUG "%s: receiving backlogged packet (%d)\n", dev->name, t);
698				receive_packet(dev, t);
699			} else {
700				adapter->busy = 0;
701			}
702		} else {
703			/* has one timed out? */
704			check_3c505_dma(dev);
705		}
706
707		/*
708		 * receive a PCB from the adapter
709		 */
710		timeout = jiffies + 3*HZ/100;
711		while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
712			if (receive_pcb(dev, &adapter->irx_pcb)) {
713				switch (adapter->irx_pcb.command)
714				{
715				case 0:
716					break;
717					/*
718					 * received a packet - this must be handled fast
719					 */
720				case 0xff:
721				case CMD_RECEIVE_PACKET_COMPLETE:
722					/* if the device isn't open, don't pass packets up the stack */
723					if (!netif_running(dev))
724						break;
725					len = adapter->irx_pcb.data.rcv_resp.pkt_len;
726					dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
727					if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
728						printk(KERN_ERR "%s: interrupt - packet not received correctly\n", dev->name);
729					} else {
730						if (elp_debug >= 3) {
731							printk(KERN_DEBUG "%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
732						}
733						if (adapter->irx_pcb.command == 0xff) {
734							if (elp_debug >= 2)
735								printk(KERN_DEBUG "%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
736							adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
737							adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
738						} else {
739							receive_packet(dev, dlen);
740						}
741						if (elp_debug >= 3)
742							printk(KERN_DEBUG "%s: packet received\n", dev->name);
743					}
744					break;
745
746					/*
747					 * 82586 configured correctly
748					 */
749				case CMD_CONFIGURE_82586_RESPONSE:
750					adapter->got[CMD_CONFIGURE_82586] = 1;
751					if (elp_debug >= 3)
752						printk(KERN_DEBUG "%s: interrupt - configure response received\n", dev->name);
753					break;
754
755					/*
756					 * Adapter memory configuration
757					 */
758				case CMD_CONFIGURE_ADAPTER_RESPONSE:
759					adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
760					if (elp_debug >= 3)
761						printk(KERN_DEBUG "%s: Adapter memory configuration %s.\n", dev->name,
762						       adapter->irx_pcb.data.failed ? "failed" : "succeeded");
763					break;
764
765					/*
766					 * Multicast list loading
767					 */
768				case CMD_LOAD_MULTICAST_RESPONSE:
769					adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
770					if (elp_debug >= 3)
771						printk(KERN_DEBUG "%s: Multicast address list loading %s.\n", dev->name,
772						       adapter->irx_pcb.data.failed ? "failed" : "succeeded");
773					break;
774
775					/*
776					 * Station address setting
777					 */
778				case CMD_SET_ADDRESS_RESPONSE:
779					adapter->got[CMD_SET_STATION_ADDRESS] = 1;
780					if (elp_debug >= 3)
781						printk(KERN_DEBUG "%s: Ethernet address setting %s.\n", dev->name,
782						       adapter->irx_pcb.data.failed ? "failed" : "succeeded");
783					break;
784
785
786					/*
787					 * received board statistics
788					 */
789				case CMD_NETWORK_STATISTICS_RESPONSE:
790					adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
791					adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
792					adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
793					adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
794					adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
795					adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
796					adapter->got[CMD_NETWORK_STATISTICS] = 1;
797					if (elp_debug >= 3)
798						printk(KERN_DEBUG "%s: interrupt - statistics response received\n", dev->name);
799					break;
800
801					/*
802					 * sent a packet
803					 */
804				case CMD_TRANSMIT_PACKET_COMPLETE:
805					if (elp_debug >= 3)
806						printk(KERN_DEBUG "%s: interrupt - packet sent\n", dev->name);
807					if (!netif_running(dev))
808						break;
809					switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
810					case 0xffff:
811						adapter->stats.tx_aborted_errors++;
812						printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
813						break;
814					case 0xfffe:
815						adapter->stats.tx_fifo_errors++;
816						printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
817						break;
818					}
819					netif_wake_queue(dev);
820					break;
821
822					/*
823					 * some unknown PCB
824					 */
825				default:
826					printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
827					break;
828				}
829			} else {
830				printk(KERN_WARNING "%s: failed to read PCB on interrupt\n", dev->name);
831				adapter_reset(dev);
832			}
833		}
834
835	} while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
836
837	prime_rx(dev);
838
839	/*
840	 * indicate no longer in interrupt routine
841	 */
842	spin_unlock(&adapter->lock);
843	return IRQ_HANDLED;
844}
845
846
847/******************************************************
848 *
849 * open the board
850 *
851 ******************************************************/
852
853static int elp_open(struct net_device *dev)
854{
855	elp_device *adapter;
856	int retval;
857
858	adapter = dev->priv;
859
860	if (elp_debug >= 3)
861		printk(KERN_DEBUG "%s: request to open device\n", dev->name);
862
863	/*
864	 * make sure we actually found the device
865	 */
866	if (adapter == NULL) {
867		printk(KERN_ERR "%s: Opening a non-existent physical device\n", dev->name);
868		return -EAGAIN;
869	}
870	/*
871	 * disable interrupts on the board
872	 */
873	outb_control(0, dev);
874
875	/*
876	 * clear any pending interrupts
877	 */
878	inb_command(dev->base_addr);
879	adapter_reset(dev);
880
881	/*
882	 * no receive PCBs active
883	 */
884	adapter->rx_active = 0;
885
886	adapter->busy = 0;
887	adapter->send_pcb_semaphore = 0;
888	adapter->rx_backlog.in = 0;
889	adapter->rx_backlog.out = 0;
890
891	spin_lock_init(&adapter->lock);
892
893	/*
894	 * install our interrupt service routine
895	 */
896	if ((retval = request_irq(dev->irq, &elp_interrupt, 0, dev->name, dev))) {
897		printk(KERN_ERR "%s: could not allocate IRQ%d\n", dev->name, dev->irq);
898		return retval;
899	}
900	if ((retval = request_dma(dev->dma, dev->name))) {
901		free_irq(dev->irq, dev);
902		printk(KERN_ERR "%s: could not allocate DMA%d channel\n", dev->name, dev->dma);
903		return retval;
904	}
905	adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
906	if (!adapter->dma_buffer) {
907		printk(KERN_ERR "%s: could not allocate DMA buffer\n", dev->name);
908		free_dma(dev->dma);
909		free_irq(dev->irq, dev);
910		return -ENOMEM;
911	}
912	adapter->dmaing = 0;
913
914	/*
915	 * enable interrupts on the board
916	 */
917	outb_control(CMDE, dev);
918
919	/*
920	 * configure adapter memory: we need 10 multicast addresses, default==0
921	 */
922	if (elp_debug >= 3)
923		printk(KERN_DEBUG "%s: sending 3c505 memory configuration command\n", dev->name);
924	adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
925	adapter->tx_pcb.data.memconf.cmd_q = 10;
926	adapter->tx_pcb.data.memconf.rcv_q = 20;
927	adapter->tx_pcb.data.memconf.mcast = 10;
928	adapter->tx_pcb.data.memconf.frame = 20;
929	adapter->tx_pcb.data.memconf.rcv_b = 20;
930	adapter->tx_pcb.data.memconf.progs = 0;
931	adapter->tx_pcb.length = sizeof(struct Memconf);
932	adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
933	if (!send_pcb(dev, &adapter->tx_pcb))
934		printk(KERN_ERR "%s: couldn't send memory configuration command\n", dev->name);
935	else {
936		unsigned long timeout = jiffies + TIMEOUT;
937		while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
938		if (time_after_eq(jiffies, timeout))
939			TIMEOUT_MSG(__LINE__);
940	}
941
942
943	/*
944	 * configure adapter to receive broadcast messages and wait for response
945	 */
946	if (elp_debug >= 3)
947		printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
948	adapter->tx_pcb.command = CMD_CONFIGURE_82586;
949	adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
950	adapter->tx_pcb.length = 2;
951	adapter->got[CMD_CONFIGURE_82586] = 0;
952	if (!send_pcb(dev, &adapter->tx_pcb))
953		printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
954	else {
955		unsigned long timeout = jiffies + TIMEOUT;
956		while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
957		if (time_after_eq(jiffies, timeout))
958			TIMEOUT_MSG(__LINE__);
959	}
960
961	/* enable burst-mode DMA */
962	/* outb(0x1, dev->base_addr + PORT_AUXDMA); */
963
964	/*
965	 * queue receive commands to provide buffering
966	 */
967	prime_rx(dev);
968	if (elp_debug >= 3)
969		printk(KERN_DEBUG "%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
970
971	/*
972	 * device is now officially open!
973	 */
974
975	netif_start_queue(dev);
976	return 0;
977}
978
979
980/******************************************************
981 *
982 * send a packet to the adapter
983 *
984 ******************************************************/
985
986static int send_packet(struct net_device *dev, struct sk_buff *skb)
987{
988	elp_device *adapter = dev->priv;
989	unsigned long target;
990	unsigned long flags;
991
992	/*
993	 * make sure the length is even and no shorter than 60 bytes
994	 */
995	unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
996
997	if (test_and_set_bit(0, (void *) &adapter->busy)) {
998		if (elp_debug >= 2)
999			printk(KERN_DEBUG "%s: transmit blocked\n", dev->name);
1000		return FALSE;
1001	}
1002
1003	adapter->stats.tx_bytes += nlen;
1004
1005	/*
1006	 * send the adapter a transmit packet command. Ignore segment and offset
1007	 * and make sure the length is even
1008	 */
1009	adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1010	adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1011	adapter->tx_pcb.data.xmit_pkt.buf_ofs
1012	    = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0;	/* Unused */
1013	adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1014
1015	if (!send_pcb(dev, &adapter->tx_pcb)) {
1016		adapter->busy = 0;
1017		return FALSE;
1018	}
1019	/* if this happens, we die */
1020	if (test_and_set_bit(0, (void *) &adapter->dmaing))
1021		printk(KERN_DEBUG "%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1022
1023	adapter->current_dma.direction = 1;
1024	adapter->current_dma.start_time = jiffies;
1025
1026	if ((unsigned long)(skb->data + nlen) >= MAX_DMA_ADDRESS || nlen != skb->len) {
1027		skb_copy_from_linear_data(skb, adapter->dma_buffer, nlen);
1028		memset(adapter->dma_buffer+skb->len, 0, nlen-skb->len);
1029		target = isa_virt_to_bus(adapter->dma_buffer);
1030	}
1031	else {
1032		target = isa_virt_to_bus(skb->data);
1033	}
1034	adapter->current_dma.skb = skb;
1035
1036	flags=claim_dma_lock();
1037	disable_dma(dev->dma);
1038	clear_dma_ff(dev->dma);
1039	set_dma_mode(dev->dma, 0x48);	/* dma memory -> io */
1040	set_dma_addr(dev->dma, target);
1041	set_dma_count(dev->dma, nlen);
1042	outb_control(adapter->hcr_val | DMAE | TCEN, dev);
1043	enable_dma(dev->dma);
1044	release_dma_lock(flags);
1045
1046	if (elp_debug >= 3)
1047		printk(KERN_DEBUG "%s: DMA transfer started\n", dev->name);
1048
1049	return TRUE;
1050}
1051
1052/*
1053 *	The upper layer thinks we timed out
1054 */
1055
1056static void elp_timeout(struct net_device *dev)
1057{
1058	elp_device *adapter = dev->priv;
1059	int stat;
1060
1061	stat = inb_status(dev->base_addr);
1062	printk(KERN_WARNING "%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1063	if (elp_debug >= 1)
1064		printk(KERN_DEBUG "%s: status %#02x\n", dev->name, stat);
1065	dev->trans_start = jiffies;
1066	adapter->stats.tx_dropped++;
1067	netif_wake_queue(dev);
1068}
1069
1070/******************************************************
1071 *
1072 * start the transmitter
1073 *    return 0 if sent OK, else return 1
1074 *
1075 ******************************************************/
1076
1077static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1078{
1079	unsigned long flags;
1080	elp_device *adapter = dev->priv;
1081
1082	spin_lock_irqsave(&adapter->lock, flags);
1083	check_3c505_dma(dev);
1084
1085	if (elp_debug >= 3)
1086		printk(KERN_DEBUG "%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1087
1088	netif_stop_queue(dev);
1089
1090	/*
1091	 * send the packet at skb->data for skb->len
1092	 */
1093	if (!send_packet(dev, skb)) {
1094		if (elp_debug >= 2) {
1095			printk(KERN_DEBUG "%s: failed to transmit packet\n", dev->name);
1096		}
1097		spin_unlock_irqrestore(&adapter->lock, flags);
1098		return 1;
1099	}
1100	if (elp_debug >= 3)
1101		printk(KERN_DEBUG "%s: packet of length %d sent\n", dev->name, (int) skb->len);
1102
1103	/*
1104	 * start the transmit timeout
1105	 */
1106	dev->trans_start = jiffies;
1107
1108	prime_rx(dev);
1109	spin_unlock_irqrestore(&adapter->lock, flags);
1110	netif_start_queue(dev);
1111	return 0;
1112}
1113
1114/******************************************************
1115 *
1116 * return statistics on the board
1117 *
1118 ******************************************************/
1119
1120static struct net_device_stats *elp_get_stats(struct net_device *dev)
1121{
1122	elp_device *adapter = (elp_device *) dev->priv;
1123
1124	if (elp_debug >= 3)
1125		printk(KERN_DEBUG "%s: request for stats\n", dev->name);
1126
1127	/* If the device is closed, just return the latest stats we have,
1128	   - we cannot ask from the adapter without interrupts */
1129	if (!netif_running(dev))
1130		return &adapter->stats;
1131
1132	/* send a get statistics command to the board */
1133	adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1134	adapter->tx_pcb.length = 0;
1135	adapter->got[CMD_NETWORK_STATISTICS] = 0;
1136	if (!send_pcb(dev, &adapter->tx_pcb))
1137		printk(KERN_ERR "%s: couldn't send get statistics command\n", dev->name);
1138	else {
1139		unsigned long timeout = jiffies + TIMEOUT;
1140		while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
1141		if (time_after_eq(jiffies, timeout)) {
1142			TIMEOUT_MSG(__LINE__);
1143			return &adapter->stats;
1144		}
1145	}
1146
1147	/* statistics are now up to date */
1148	return &adapter->stats;
1149}
1150
1151
1152static void netdev_get_drvinfo(struct net_device *dev,
1153			       struct ethtool_drvinfo *info)
1154{
1155	strcpy(info->driver, DRV_NAME);
1156	strcpy(info->version, DRV_VERSION);
1157	sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
1158}
1159
1160static u32 netdev_get_msglevel(struct net_device *dev)
1161{
1162	return debug;
1163}
1164
1165static void netdev_set_msglevel(struct net_device *dev, u32 level)
1166{
1167	debug = level;
1168}
1169
1170static const struct ethtool_ops netdev_ethtool_ops = {
1171	.get_drvinfo		= netdev_get_drvinfo,
1172	.get_msglevel		= netdev_get_msglevel,
1173	.set_msglevel		= netdev_set_msglevel,
1174};
1175
1176/******************************************************
1177 *
1178 * close the board
1179 *
1180 ******************************************************/
1181
1182static int elp_close(struct net_device *dev)
1183{
1184	elp_device *adapter;
1185
1186	adapter = dev->priv;
1187
1188	if (elp_debug >= 3)
1189		printk(KERN_DEBUG "%s: request to close device\n", dev->name);
1190
1191	netif_stop_queue(dev);
1192
1193	/* Someone may request the device statistic information even when
1194	 * the interface is closed. The following will update the statistics
1195	 * structure in the driver, so we'll be able to give current statistics.
1196	 */
1197	(void) elp_get_stats(dev);
1198
1199	/*
1200	 * disable interrupts on the board
1201	 */
1202	outb_control(0, dev);
1203
1204	/*
1205	 * release the IRQ
1206	 */
1207	free_irq(dev->irq, dev);
1208
1209	free_dma(dev->dma);
1210	free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
1211
1212	return 0;
1213}
1214
1215
1216/************************************************************
1217 *
1218 * Set multicast list
1219 * num_addrs==0: clear mc_list
1220 * num_addrs==-1: set promiscuous mode
1221 * num_addrs>0: set mc_list
1222 *
1223 ************************************************************/
1224
1225static void elp_set_mc_list(struct net_device *dev)
1226{
1227	elp_device *adapter = (elp_device *) dev->priv;
1228	struct dev_mc_list *dmi = dev->mc_list;
1229	int i;
1230	unsigned long flags;
1231
1232	if (elp_debug >= 3)
1233		printk(KERN_DEBUG "%s: request to set multicast list\n", dev->name);
1234
1235	spin_lock_irqsave(&adapter->lock, flags);
1236
1237	if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1238		/* send a "load multicast list" command to the board, max 10 addrs/cmd */
1239		/* if num_addrs==0 the list will be cleared */
1240		adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1241		adapter->tx_pcb.length = 6 * dev->mc_count;
1242		for (i = 0; i < dev->mc_count; i++) {
1243			memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1244			dmi = dmi->next;
1245		}
1246		adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1247		if (!send_pcb(dev, &adapter->tx_pcb))
1248			printk(KERN_ERR "%s: couldn't send set_multicast command\n", dev->name);
1249		else {
1250			unsigned long timeout = jiffies + TIMEOUT;
1251			while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
1252			if (time_after_eq(jiffies, timeout)) {
1253				TIMEOUT_MSG(__LINE__);
1254			}
1255		}
1256		if (dev->mc_count)
1257			adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1258		else		/* num_addrs == 0 */
1259			adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1260	} else
1261		adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1262	/*
1263	 * configure adapter to receive messages (as specified above)
1264	 * and wait for response
1265	 */
1266	if (elp_debug >= 3)
1267		printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
1268	adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1269	adapter->tx_pcb.length = 2;
1270	adapter->got[CMD_CONFIGURE_82586] = 0;
1271	if (!send_pcb(dev, &adapter->tx_pcb))
1272	{
1273		spin_unlock_irqrestore(&adapter->lock, flags);
1274		printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
1275	}
1276	else {
1277		unsigned long timeout = jiffies + TIMEOUT;
1278		spin_unlock_irqrestore(&adapter->lock, flags);
1279		while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
1280		if (time_after_eq(jiffies, timeout))
1281			TIMEOUT_MSG(__LINE__);
1282	}
1283}
1284
1285/************************************************************
1286 *
1287 * A couple of tests to see if there's 3C505 or not
1288 * Called only by elp_autodetect
1289 ************************************************************/
1290
1291static int __init elp_sense(struct net_device *dev)
1292{
1293	int addr = dev->base_addr;
1294	const char *name = dev->name;
1295	byte orig_HSR;
1296
1297	if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
1298		return -ENODEV;
1299
1300	orig_HSR = inb_status(addr);
1301
1302	if (elp_debug > 0)
1303		printk(search_msg, name, addr);
1304
1305	if (orig_HSR == 0xff) {
1306		if (elp_debug > 0)
1307			printk(notfound_msg, 1);
1308		goto out;
1309	}
1310
1311	/* Wait for a while; the adapter may still be booting up */
1312	if (elp_debug > 0)
1313		printk(stilllooking_msg);
1314
1315	if (orig_HSR & DIR) {
1316		/* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1317		outb(0, dev->base_addr + PORT_CONTROL);
1318		msleep(300);
1319		if (inb_status(addr) & DIR) {
1320			if (elp_debug > 0)
1321				printk(notfound_msg, 2);
1322			goto out;
1323		}
1324	} else {
1325		/* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1326		outb(DIR, dev->base_addr + PORT_CONTROL);
1327		msleep(300);
1328		if (!(inb_status(addr) & DIR)) {
1329			if (elp_debug > 0)
1330				printk(notfound_msg, 3);
1331			goto out;
1332		}
1333	}
1334	/*
1335	 * It certainly looks like a 3c505.
1336	 */
1337	if (elp_debug > 0)
1338		printk(found_msg);
1339
1340	return 0;
1341out:
1342	release_region(addr, ELP_IO_EXTENT);
1343	return -ENODEV;
1344}
1345
1346/*************************************************************
1347 *
1348 * Search through addr_list[] and try to find a 3C505
1349 * Called only by eplus_probe
1350 *************************************************************/
1351
1352static int __init elp_autodetect(struct net_device *dev)
1353{
1354	int idx = 0;
1355
1356	/* if base address set, then only check that address
1357	   otherwise, run through the table */
1358	if (dev->base_addr != 0) {	/* dev->base_addr == 0 ==> plain autodetect */
1359		if (elp_sense(dev) == 0)
1360			return dev->base_addr;
1361	} else
1362		while ((dev->base_addr = addr_list[idx++])) {
1363			if (elp_sense(dev) == 0)
1364				return dev->base_addr;
1365		}
1366
1367	/* could not find an adapter */
1368	if (elp_debug > 0)
1369		printk(couldnot_msg, dev->name);
1370
1371	return 0;		/* Because of this, the layer above will return -ENODEV */
1372}
1373
1374
1375/******************************************************
1376 *
1377 * probe for an Etherlink Plus board at the specified address
1378 *
1379 ******************************************************/
1380
1381/* There are three situations we need to be able to detect here:
1382
1383 *  a) the card is idle
1384 *  b) the card is still booting up
1385 *  c) the card is stuck in a strange state (some DOS drivers do this)
1386 *
1387 * In case (a), all is well.  In case (b), we wait 10 seconds to see if the
1388 * card finishes booting, and carry on if so.  In case (c), we do a hard reset,
1389 * loop round, and hope for the best.
1390 *
1391 * This is all very unpleasant, but hopefully avoids the problems with the old
1392 * probe code (which had a 15-second delay if the card was idle, and didn't
1393 * work at all if it was in a weird state).
1394 */
1395
1396static int __init elplus_setup(struct net_device *dev)
1397{
1398	elp_device *adapter = dev->priv;
1399	int i, tries, tries1, okay;
1400	unsigned long timeout;
1401	unsigned long cookie = 0;
1402	int err = -ENODEV;
1403
1404	SET_MODULE_OWNER(dev);
1405
1406	/*
1407	 *  setup adapter structure
1408	 */
1409
1410	dev->base_addr = elp_autodetect(dev);
1411	if (!dev->base_addr)
1412		return -ENODEV;
1413
1414	adapter->send_pcb_semaphore = 0;
1415
1416	for (tries1 = 0; tries1 < 3; tries1++) {
1417		outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1418		/* First try to write just one byte, to see if the card is
1419		 * responding at all normally.
1420		 */
1421		timeout = jiffies + 5*HZ/100;
1422		okay = 0;
1423		while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1424		if ((inb_status(dev->base_addr) & HCRE)) {
1425			outb_command(0, dev->base_addr);	/* send a spurious byte */
1426			timeout = jiffies + 5*HZ/100;
1427			while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1428			if (inb_status(dev->base_addr) & HCRE)
1429				okay = 1;
1430		}
1431		if (!okay) {
1432			/* Nope, it's ignoring the command register.  This means that
1433			 * either it's still booting up, or it's died.
1434			 */
1435			printk(KERN_ERR "%s: command register wouldn't drain, ", dev->name);
1436			if ((inb_status(dev->base_addr) & 7) == 3) {
1437				/* If the adapter status is 3, it *could* still be booting.
1438				 * Give it the benefit of the doubt for 10 seconds.
1439				 */
1440				printk("assuming 3c505 still starting\n");
1441				timeout = jiffies + 10*HZ;
1442				while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1443				if (inb_status(dev->base_addr) & 7) {
1444					printk(KERN_ERR "%s: 3c505 failed to start\n", dev->name);
1445				} else {
1446					okay = 1;  /* It started */
1447				}
1448			} else {
1449				/* Otherwise, it must just be in a strange
1450				 * state.  We probably need to kick it.
1451				 */
1452				printk("3c505 is sulking\n");
1453			}
1454		}
1455		for (tries = 0; tries < 5 && okay; tries++) {
1456
1457			/*
1458			 * Try to set the Ethernet address, to make sure that the board
1459			 * is working.
1460			 */
1461			adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1462			adapter->tx_pcb.length = 0;
1463			cookie = probe_irq_on();
1464			if (!send_pcb(dev, &adapter->tx_pcb)) {
1465				printk(KERN_ERR "%s: could not send first PCB\n", dev->name);
1466				probe_irq_off(cookie);
1467				continue;
1468			}
1469			if (!receive_pcb(dev, &adapter->rx_pcb)) {
1470				printk(KERN_ERR "%s: could not read first PCB\n", dev->name);
1471				probe_irq_off(cookie);
1472				continue;
1473			}
1474			if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1475			    (adapter->rx_pcb.length != 6)) {
1476				printk(KERN_ERR "%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1477				probe_irq_off(cookie);
1478				continue;
1479			}
1480			goto okay;
1481		}
1482		/* It's broken.  Do a hard reset to re-initialise the board,
1483		 * and try again.
1484		 */
1485		printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1486		outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1487		outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1488	}
1489	printk(KERN_ERR "%s: failed to initialise 3c505\n", dev->name);
1490	goto out;
1491
1492      okay:
1493	if (dev->irq) {		/* Is there a preset IRQ? */
1494		int rpt = probe_irq_off(cookie);
1495		if (dev->irq != rpt) {
1496			printk(KERN_WARNING "%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1497		}
1498		/* if dev->irq == probe_irq_off(cookie), all is well */
1499	} else		       /* No preset IRQ; just use what we can detect */
1500		dev->irq = probe_irq_off(cookie);
1501	switch (dev->irq) {    /* Legal, sane? */
1502	case 0:
1503		printk(KERN_ERR "%s: IRQ probe failed: check 3c505 jumpers.\n",
1504		       dev->name);
1505		goto out;
1506	case 1:
1507	case 6:
1508	case 8:
1509	case 13:
1510		printk(KERN_ERR "%s: Impossible IRQ %d reported by probe_irq_off().\n",
1511		       dev->name, dev->irq);
1512		       goto out;
1513	}
1514	/*
1515	 *  Now we have the IRQ number so we can disable the interrupts from
1516	 *  the board until the board is opened.
1517	 */
1518	outb_control(adapter->hcr_val & ~CMDE, dev);
1519
1520	/*
1521	 * copy Ethernet address into structure
1522	 */
1523	for (i = 0; i < 6; i++)
1524		dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1525
1526	/* find a DMA channel */
1527	if (!dev->dma) {
1528		if (dev->mem_start) {
1529			dev->dma = dev->mem_start & 7;
1530		}
1531		else {
1532			printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
1533			dev->dma = ELP_DMA;
1534		}
1535	}
1536
1537	/*
1538	 * print remainder of startup message
1539	 */
1540	printk(KERN_INFO "%s: 3c505 at %#lx, irq %d, dma %d, ",
1541	       dev->name, dev->base_addr, dev->irq, dev->dma);
1542	printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1543	       dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1544	       dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1545
1546	/*
1547	 * read more information from the adapter
1548	 */
1549
1550	adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1551	adapter->tx_pcb.length = 0;
1552	if (!send_pcb(dev, &adapter->tx_pcb) ||
1553	    !receive_pcb(dev, &adapter->rx_pcb) ||
1554	    (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1555	    (adapter->rx_pcb.length != 10)) {
1556		printk("not responding to second PCB\n");
1557	}
1558	printk("rev %d.%d, %dk\n", adapter->rx_pcb.data.info.major_vers, adapter->rx_pcb.data.info.minor_vers, adapter->rx_pcb.data.info.RAM_sz);
1559
1560	/*
1561	 * reconfigure the adapter memory to better suit our purposes
1562	 */
1563	adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1564	adapter->tx_pcb.length = 12;
1565	adapter->tx_pcb.data.memconf.cmd_q = 8;
1566	adapter->tx_pcb.data.memconf.rcv_q = 8;
1567	adapter->tx_pcb.data.memconf.mcast = 10;
1568	adapter->tx_pcb.data.memconf.frame = 10;
1569	adapter->tx_pcb.data.memconf.rcv_b = 10;
1570	adapter->tx_pcb.data.memconf.progs = 0;
1571	if (!send_pcb(dev, &adapter->tx_pcb) ||
1572	    !receive_pcb(dev, &adapter->rx_pcb) ||
1573	    (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1574	    (adapter->rx_pcb.length != 2)) {
1575		printk(KERN_ERR "%s: could not configure adapter memory\n", dev->name);
1576	}
1577	if (adapter->rx_pcb.data.configure) {
1578		printk(KERN_ERR "%s: adapter configuration failed\n", dev->name);
1579	}
1580
1581	dev->open = elp_open;				/* local */
1582	dev->stop = elp_close;				/* local */
1583	dev->get_stats = elp_get_stats;			/* local */
1584	dev->hard_start_xmit = elp_start_xmit;		/* local */
1585	dev->tx_timeout = elp_timeout;			/* local */
1586	dev->watchdog_timeo = 10*HZ;
1587	dev->set_multicast_list = elp_set_mc_list;	/* local */
1588	dev->ethtool_ops = &netdev_ethtool_ops;		/* local */
1589
1590	memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
1591	dev->mem_start = dev->mem_end = 0;
1592
1593	err = register_netdev(dev);
1594	if (err)
1595		goto out;
1596
1597	return 0;
1598out:
1599	release_region(dev->base_addr, ELP_IO_EXTENT);
1600	return err;
1601}
1602
1603#ifndef MODULE
1604struct net_device * __init elplus_probe(int unit)
1605{
1606	struct net_device *dev = alloc_etherdev(sizeof(elp_device));
1607	int err;
1608	if (!dev)
1609		return ERR_PTR(-ENOMEM);
1610
1611	sprintf(dev->name, "eth%d", unit);
1612	netdev_boot_setup_check(dev);
1613
1614	err = elplus_setup(dev);
1615	if (err) {
1616		free_netdev(dev);
1617		return ERR_PTR(err);
1618	}
1619	return dev;
1620}
1621
1622#else
1623static struct net_device *dev_3c505[ELP_MAX_CARDS];
1624static int io[ELP_MAX_CARDS];
1625static int irq[ELP_MAX_CARDS];
1626static int dma[ELP_MAX_CARDS];
1627module_param_array(io, int, NULL, 0);
1628module_param_array(irq, int, NULL, 0);
1629module_param_array(dma, int, NULL, 0);
1630MODULE_PARM_DESC(io, "EtherLink Plus I/O base address(es)");
1631MODULE_PARM_DESC(irq, "EtherLink Plus IRQ number(s) (assigned)");
1632MODULE_PARM_DESC(dma, "EtherLink Plus DMA channel(s)");
1633
1634int __init init_module(void)
1635{
1636	int this_dev, found = 0;
1637
1638	for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1639		struct net_device *dev = alloc_etherdev(sizeof(elp_device));
1640		if (!dev)
1641			break;
1642
1643		dev->irq = irq[this_dev];
1644		dev->base_addr = io[this_dev];
1645		if (dma[this_dev]) {
1646			dev->dma = dma[this_dev];
1647		} else {
1648			dev->dma = ELP_DMA;
1649			printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
1650		}
1651		if (io[this_dev] == 0) {
1652			if (this_dev) {
1653				free_netdev(dev);
1654				break;
1655			}
1656			printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
1657		}
1658		if (elplus_setup(dev) != 0) {
1659			printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
1660			free_netdev(dev);
1661			break;
1662		}
1663		dev_3c505[this_dev] = dev;
1664		found++;
1665	}
1666	if (!found)
1667		return -ENODEV;
1668	return 0;
1669}
1670
1671void __exit cleanup_module(void)
1672{
1673	int this_dev;
1674
1675	for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1676		struct net_device *dev = dev_3c505[this_dev];
1677		if (dev) {
1678			unregister_netdev(dev);
1679			release_region(dev->base_addr, ELP_IO_EXTENT);
1680			free_netdev(dev);
1681		}
1682	}
1683}
1684
1685#endif				/* MODULE */
1686MODULE_LICENSE("GPL");
1687