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