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