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