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