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