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