More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / 3c505.c
blobde4ebe6d650485706fcac50441a55e6386b34291
1 /*
2 * Linux Ethernet device driver for the 3Com Etherlink Plus (3C505)
3 * By Craig Southeren, Juha Laiho and Philip Blundell
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.
13 * $Id: 3c505.c,v 1.10 1996/04/16 13:06:27 phil Exp $
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
41 #define DRV_NAME "3c505"
42 #define DRV_VERSION "1.10a"
45 /* Theory of operation:
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.
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.
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.
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.
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.
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.
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>
110 #include <asm/uaccess.h>
111 #include <asm/bitops.h>
112 #include <asm/io.h>
113 #include <asm/dma.h>
115 #include <linux/netdevice.h>
116 #include <linux/etherdevice.h>
117 #include <linux/skbuff.h>
118 #include <linux/init.h>
120 #include "3c505.h"
122 /*********************************************************
124 * define debug messages here as common strings to reduce space
126 *********************************************************/
128 static const char filename[] = __FILE__;
130 static const char timeout_msg[] = "*** timeout at %s:%s (line %d) ***\n";
131 #define TIMEOUT_MSG(lineno) \
132 printk(timeout_msg, filename,__FUNCTION__,(lineno))
134 static const char invalid_pcb_msg[] =
135 "*** invalid pcb length %d at %s:%s (line %d) ***\n";
136 #define INVALID_PCB_MSG(len) \
137 printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
139 static char search_msg[] __initdata = KERN_INFO "%s: Looking for 3c505 adapter at address %#x...";
141 static char stilllooking_msg[] __initdata = "still looking...";
143 static char found_msg[] __initdata = "found.\n";
145 static char notfound_msg[] __initdata = "not found (reason = %d)\n";
147 static char couldnot_msg[] __initdata = KERN_INFO "%s: 3c505 not found\n";
149 /*********************************************************
151 * various other debug stuff
153 *********************************************************/
155 #ifdef ELP_DEBUG
156 static int elp_debug = ELP_DEBUG;
157 #else
158 static int elp_debug;
159 #endif
160 #define debug elp_debug
163 * 0 = no messages (well, some)
164 * 1 = messages when high level commands performed
165 * 2 = messages when low level commands performed
166 * 3 = messages when interrupts received
169 /*****************************************************************
171 * useful macros
173 *****************************************************************/
175 #ifndef TRUE
176 #define TRUE 1
177 #endif
179 #ifndef FALSE
180 #define FALSE 0
181 #endif
184 /*****************************************************************
186 * List of I/O-addresses we try to auto-sense
187 * Last element MUST BE 0!
188 *****************************************************************/
190 static int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
192 /* Dma Memory related stuff */
194 static unsigned long dma_mem_alloc(int size)
196 int order = get_order(size);
197 return __get_dma_pages(GFP_KERNEL, order);
201 /*****************************************************************
203 * Functions for I/O (note the inline !)
205 *****************************************************************/
207 static inline unsigned char inb_status(unsigned int base_addr)
209 return inb(base_addr + PORT_STATUS);
212 static inline int inb_command(unsigned int base_addr)
214 return inb(base_addr + PORT_COMMAND);
217 static inline void outb_control(unsigned char val, struct net_device *dev)
219 outb(val, dev->base_addr + PORT_CONTROL);
220 ((elp_device *)(dev->priv))->hcr_val = val;
223 #define HCR_VAL(x) (((elp_device *)((x)->priv))->hcr_val)
225 static inline void outb_command(unsigned char val, unsigned int base_addr)
227 outb(val, base_addr + PORT_COMMAND);
230 static inline unsigned int inw_data(unsigned int base_addr)
232 return inw(base_addr + PORT_DATA);
235 static inline void outw_data(unsigned int val, unsigned int base_addr)
237 outw(val, base_addr + PORT_DATA);
240 static inline unsigned int backlog_next(unsigned int n)
242 return (n + 1) % BACKLOG_SIZE;
245 /*****************************************************************
247 * useful functions for accessing the adapter
249 *****************************************************************/
252 * use this routine when accessing the ASF bits as they are
253 * changed asynchronously by the adapter
256 /* get adapter PCB status */
257 #define GET_ASF(addr) \
258 (get_status(addr)&ASF_PCB_MASK)
260 static inline int get_status(unsigned int base_addr)
262 unsigned long timeout = jiffies + 10*HZ/100;
263 register int stat1;
264 do {
265 stat1 = inb_status(base_addr);
266 } while (stat1 != inb_status(base_addr) && time_before(jiffies, timeout));
267 if (time_after_eq(jiffies, timeout))
268 TIMEOUT_MSG(__LINE__);
269 return stat1;
272 static inline void set_hsf(struct net_device *dev, int hsf)
274 elp_device *adapter = dev->priv;
275 unsigned long flags;
277 spin_lock_irqsave(&adapter->lock, flags);
278 outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
279 spin_unlock_irqrestore(&adapter->lock, flags);
282 static int start_receive(struct net_device *, pcb_struct *);
284 inline static void adapter_reset(struct net_device *dev)
286 unsigned long timeout;
287 elp_device *adapter = dev->priv;
288 unsigned char orig_hcr = adapter->hcr_val;
290 outb_control(0, dev);
292 if (inb_status(dev->base_addr) & ACRF) {
293 do {
294 inb_command(dev->base_addr);
295 timeout = jiffies + 2*HZ/100;
296 while (time_before_eq(jiffies, timeout) && !(inb_status(dev->base_addr) & ACRF));
297 } while (inb_status(dev->base_addr) & ACRF);
298 set_hsf(dev, HSF_PCB_NAK);
300 outb_control(adapter->hcr_val | ATTN | DIR, dev);
301 timeout = jiffies + 1*HZ/100;
302 while (time_before_eq(jiffies, timeout));
303 outb_control(adapter->hcr_val & ~ATTN, dev);
304 timeout = jiffies + 1*HZ/100;
305 while (time_before_eq(jiffies, timeout));
306 outb_control(adapter->hcr_val | FLSH, dev);
307 timeout = jiffies + 1*HZ/100;
308 while (time_before_eq(jiffies, timeout));
309 outb_control(adapter->hcr_val & ~FLSH, dev);
310 timeout = jiffies + 1*HZ/100;
311 while (time_before_eq(jiffies, timeout));
313 outb_control(orig_hcr, dev);
314 if (!start_receive(dev, &adapter->tx_pcb))
315 printk(KERN_ERR "%s: start receive command failed \n", dev->name);
318 /* Check to make sure that a DMA transfer hasn't timed out. This should
319 * never happen in theory, but seems to occur occasionally if the card gets
320 * prodded at the wrong time.
322 static inline void check_3c505_dma(struct net_device *dev)
324 elp_device *adapter = dev->priv;
325 if (adapter->dmaing && time_after(jiffies, adapter->current_dma.start_time + 10)) {
326 unsigned long flags, f;
327 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));
328 spin_lock_irqsave(&adapter->lock, flags);
329 adapter->dmaing = 0;
330 adapter->busy = 0;
332 f=claim_dma_lock();
333 disable_dma(dev->dma);
334 release_dma_lock(f);
336 if (adapter->rx_active)
337 adapter->rx_active--;
338 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
339 spin_unlock_irqrestore(&adapter->lock, flags);
343 /* Primitive functions used by send_pcb() */
344 static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
346 unsigned long timeout;
347 outb_command(byte, base_addr);
348 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
349 if (inb_status(base_addr) & HCRE)
350 return FALSE;
352 printk(KERN_WARNING "3c505: send_pcb_slow timed out\n");
353 return TRUE;
356 static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
358 unsigned int timeout;
359 outb_command(byte, base_addr);
360 for (timeout = 0; timeout < 40000; timeout++) {
361 if (inb_status(base_addr) & HCRE)
362 return FALSE;
364 printk(KERN_WARNING "3c505: send_pcb_fast timed out\n");
365 return TRUE;
368 /* Check to see if the receiver needs restarting, and kick it if so */
369 static inline void prime_rx(struct net_device *dev)
371 elp_device *adapter = dev->priv;
372 while (adapter->rx_active < ELP_RX_PCBS && netif_running(dev)) {
373 if (!start_receive(dev, &adapter->itx_pcb))
374 break;
378 /*****************************************************************
380 * send_pcb
381 * Send a PCB to the adapter.
383 * output byte to command reg --<--+
384 * wait until HCRE is non zero |
385 * loop until all bytes sent -->--+
386 * set HSF1 and HSF2 to 1
387 * output pcb length
388 * wait until ASF give ACK or NAK
389 * set HSF1 and HSF2 to 0
391 *****************************************************************/
393 /* This can be quite slow -- the adapter is allowed to take up to 40ms
394 * to respond to the initial interrupt.
396 * We run initially with interrupts turned on, but with a semaphore set
397 * so that nobody tries to re-enter this code. Once the first byte has
398 * gone through, we turn interrupts off and then send the others (the
399 * timeout is reduced to 500us).
402 static int send_pcb(struct net_device *dev, pcb_struct * pcb)
404 int i;
405 unsigned long timeout;
406 elp_device *adapter = dev->priv;
407 unsigned long flags;
409 check_3c505_dma(dev);
411 if (adapter->dmaing && adapter->current_dma.direction == 0)
412 return FALSE;
414 /* Avoid contention */
415 if (test_and_set_bit(1, &adapter->send_pcb_semaphore)) {
416 if (elp_debug >= 3) {
417 printk(KERN_DEBUG "%s: send_pcb entered while threaded\n", dev->name);
419 return FALSE;
422 * load each byte into the command register and
423 * wait for the HCRE bit to indicate the adapter
424 * had read the byte
426 set_hsf(dev, 0);
428 if (send_pcb_slow(dev->base_addr, pcb->command))
429 goto abort;
431 spin_lock_irqsave(&adapter->lock, flags);
433 if (send_pcb_fast(dev->base_addr, pcb->length))
434 goto sti_abort;
436 for (i = 0; i < pcb->length; i++) {
437 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
438 goto sti_abort;
441 outb_control(adapter->hcr_val | 3, dev); /* signal end of PCB */
442 outb_command(2 + pcb->length, dev->base_addr);
444 /* now wait for the acknowledgement */
445 spin_unlock_irqrestore(&adapter->lock, flags);
447 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
448 switch (GET_ASF(dev->base_addr)) {
449 case ASF_PCB_ACK:
450 adapter->send_pcb_semaphore = 0;
451 return TRUE;
452 break;
453 case ASF_PCB_NAK:
454 #ifdef ELP_DEBUG
455 printk(KERN_DEBUG "%s: send_pcb got NAK\n", dev->name);
456 #endif
457 goto abort;
458 break;
462 if (elp_debug >= 1)
463 printk(KERN_DEBUG "%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
465 sti_abort:
466 spin_unlock_irqrestore(&adapter->lock, flags);
467 abort:
468 adapter->send_pcb_semaphore = 0;
469 return FALSE;
473 /*****************************************************************
475 * receive_pcb
476 * Read a PCB from the adapter
478 * wait for ACRF to be non-zero ---<---+
479 * input a byte |
480 * if ASF1 and ASF2 were not both one |
481 * before byte was read, loop --->---+
482 * set HSF1 and HSF2 for ack
484 *****************************************************************/
486 static int receive_pcb(struct net_device *dev, pcb_struct * pcb)
488 int i, j;
489 int total_length;
490 int stat;
491 unsigned long timeout;
492 unsigned long flags;
494 elp_device *adapter = dev->priv;
496 set_hsf(dev, 0);
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;
505 pcb->command = inb_command(dev->base_addr);
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(KERN_INFO "%s: status %02x\n", dev->name, stat);
513 return FALSE;
515 pcb->length = inb_command(dev->base_addr);
517 if (pcb->length > MAX_PCB_DATA) {
518 INVALID_PCB_MSG(pcb->length);
519 adapter_reset(dev);
520 return FALSE;
522 /* read the data */
523 spin_lock_irqsave(&adapter->lock, flags);
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 spin_unlock_irqrestore(&adapter->lock, flags);
533 if (j >= 20000) {
534 TIMEOUT_MSG(__LINE__);
535 return FALSE;
537 /* woops, the last "data" byte was really the length! */
538 total_length = pcb->data.raw[--i];
540 /* safety check total length vs data length */
541 if (total_length != (pcb->length + 2)) {
542 if (elp_debug >= 2)
543 printk(KERN_WARNING "%s: mangled PCB received\n", dev->name);
544 set_hsf(dev, HSF_PCB_NAK);
545 return FALSE;
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(KERN_WARNING "%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;
560 set_hsf(dev, HSF_PCB_ACK);
561 return TRUE;
564 /******************************************************
566 * queue a receive command on the adapter so we will get an
567 * interrupt when a packet is received.
569 ******************************************************/
571 static int start_receive(struct net_device *dev, pcb_struct * tx_pcb)
573 int status;
574 elp_device *adapter = dev->priv;
576 if (elp_debug >= 3)
577 printk(KERN_DEBUG "%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;
590 /******************************************************
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
597 ******************************************************/
599 static void receive_packet(struct net_device *dev, int len)
601 int rlen;
602 elp_device *adapter = dev->priv;
603 void *target;
604 struct sk_buff *skb;
605 unsigned long flags;
607 rlen = (len + 1) & ~1;
608 skb = dev_alloc_skb(rlen + 2);
610 if (!skb) {
611 printk(KERN_WARNING "%s: memory squeeze, dropping packet\n", dev->name);
612 target = adapter->dma_buffer;
613 adapter->current_dma.target = NULL;
614 /* FIXME: stats */
615 return;
618 skb_reserve(skb, 2);
619 target = skb_put(skb, rlen);
620 if ((unsigned long)(target + rlen) >= MAX_DMA_ADDRESS) {
621 adapter->current_dma.target = target;
622 target = adapter->dma_buffer;
623 } else {
624 adapter->current_dma.target = NULL;
627 /* if this happens, we die */
628 if (test_and_set_bit(0, (void *) &adapter->dmaing))
629 printk(KERN_ERR "%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
631 skb->dev = dev;
632 adapter->current_dma.direction = 0;
633 adapter->current_dma.length = rlen;
634 adapter->current_dma.skb = skb;
635 adapter->current_dma.start_time = jiffies;
637 outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
639 flags=claim_dma_lock();
640 disable_dma(dev->dma);
641 clear_dma_ff(dev->dma);
642 set_dma_mode(dev->dma, 0x04); /* dma read */
643 set_dma_addr(dev->dma, isa_virt_to_bus(target));
644 set_dma_count(dev->dma, rlen);
645 enable_dma(dev->dma);
646 release_dma_lock(flags);
648 if (elp_debug >= 3) {
649 printk(KERN_DEBUG "%s: rx DMA transfer started\n", dev->name);
652 if (adapter->rx_active)
653 adapter->rx_active--;
655 if (!adapter->busy)
656 printk(KERN_WARNING "%s: receive_packet called, busy not set.\n", dev->name);
659 /******************************************************
661 * interrupt handler
663 ******************************************************/
665 static irqreturn_t elp_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
667 int len;
668 int dlen;
669 int icount = 0;
670 struct net_device *dev;
671 elp_device *adapter;
672 unsigned long timeout;
674 dev = dev_id;
675 adapter = (elp_device *) dev->priv;
677 spin_lock(&adapter->lock);
679 do {
681 * has a DMA transfer finished?
683 if (inb_status(dev->base_addr) & DONE) {
684 if (!adapter->dmaing) {
685 printk(KERN_WARNING "%s: phantom DMA completed\n", dev->name);
687 if (elp_debug >= 3) {
688 printk(KERN_DEBUG "%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
691 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
692 if (adapter->current_dma.direction) {
693 dev_kfree_skb_irq(adapter->current_dma.skb);
694 } else {
695 struct sk_buff *skb = adapter->current_dma.skb;
696 if (skb) {
697 if (adapter->current_dma.target) {
698 /* have already done the skb_put() */
699 memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
701 skb->protocol = eth_type_trans(skb,dev);
702 adapter->stats.rx_bytes += skb->len;
703 netif_rx(skb);
704 dev->last_rx = jiffies;
707 adapter->dmaing = 0;
708 if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
709 int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
710 adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
711 if (elp_debug >= 2)
712 printk(KERN_DEBUG "%s: receiving backlogged packet (%d)\n", dev->name, t);
713 receive_packet(dev, t);
714 } else {
715 adapter->busy = 0;
717 } else {
718 /* has one timed out? */
719 check_3c505_dma(dev);
723 * receive a PCB from the adapter
725 timeout = jiffies + 3*HZ/100;
726 while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
727 if (receive_pcb(dev, &adapter->irx_pcb)) {
728 switch (adapter->irx_pcb.command)
730 case 0:
731 break;
733 * received a packet - this must be handled fast
735 case 0xff:
736 case CMD_RECEIVE_PACKET_COMPLETE:
737 /* if the device isn't open, don't pass packets up the stack */
738 if (!netif_running(dev))
739 break;
740 len = adapter->irx_pcb.data.rcv_resp.pkt_len;
741 dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
742 if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
743 printk(KERN_ERR "%s: interrupt - packet not received correctly\n", dev->name);
744 } else {
745 if (elp_debug >= 3) {
746 printk(KERN_DEBUG "%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
748 if (adapter->irx_pcb.command == 0xff) {
749 if (elp_debug >= 2)
750 printk(KERN_DEBUG "%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
751 adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
752 adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
753 } else {
754 receive_packet(dev, dlen);
756 if (elp_debug >= 3)
757 printk(KERN_DEBUG "%s: packet received\n", dev->name);
759 break;
762 * 82586 configured correctly
764 case CMD_CONFIGURE_82586_RESPONSE:
765 adapter->got[CMD_CONFIGURE_82586] = 1;
766 if (elp_debug >= 3)
767 printk(KERN_DEBUG "%s: interrupt - configure response received\n", dev->name);
768 break;
771 * Adapter memory configuration
773 case CMD_CONFIGURE_ADAPTER_RESPONSE:
774 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
775 if (elp_debug >= 3)
776 printk(KERN_DEBUG "%s: Adapter memory configuration %s.\n", dev->name,
777 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
778 break;
781 * Multicast list loading
783 case CMD_LOAD_MULTICAST_RESPONSE:
784 adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
785 if (elp_debug >= 3)
786 printk(KERN_DEBUG "%s: Multicast address list loading %s.\n", dev->name,
787 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
788 break;
791 * Station address setting
793 case CMD_SET_ADDRESS_RESPONSE:
794 adapter->got[CMD_SET_STATION_ADDRESS] = 1;
795 if (elp_debug >= 3)
796 printk(KERN_DEBUG "%s: Ethernet address setting %s.\n", dev->name,
797 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
798 break;
802 * received board statistics
804 case CMD_NETWORK_STATISTICS_RESPONSE:
805 adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
806 adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
807 adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
808 adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
809 adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
810 adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
811 adapter->got[CMD_NETWORK_STATISTICS] = 1;
812 if (elp_debug >= 3)
813 printk(KERN_DEBUG "%s: interrupt - statistics response received\n", dev->name);
814 break;
817 * sent a packet
819 case CMD_TRANSMIT_PACKET_COMPLETE:
820 if (elp_debug >= 3)
821 printk(KERN_DEBUG "%s: interrupt - packet sent\n", dev->name);
822 if (!netif_running(dev))
823 break;
824 switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
825 case 0xffff:
826 adapter->stats.tx_aborted_errors++;
827 printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
828 break;
829 case 0xfffe:
830 adapter->stats.tx_fifo_errors++;
831 printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
832 break;
834 netif_wake_queue(dev);
835 break;
838 * some unknown PCB
840 default:
841 printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
842 break;
844 } else {
845 printk(KERN_WARNING "%s: failed to read PCB on interrupt\n", dev->name);
846 adapter_reset(dev);
850 } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
852 prime_rx(dev);
855 * indicate no longer in interrupt routine
857 spin_unlock(&adapter->lock);
858 return IRQ_HANDLED;
862 /******************************************************
864 * open the board
866 ******************************************************/
868 static int elp_open(struct net_device *dev)
870 elp_device *adapter;
871 int retval;
873 adapter = dev->priv;
875 if (elp_debug >= 3)
876 printk(KERN_DEBUG "%s: request to open device\n", dev->name);
879 * make sure we actually found the device
881 if (adapter == NULL) {
882 printk(KERN_ERR "%s: Opening a non-existent physical device\n", dev->name);
883 return -EAGAIN;
886 * disable interrupts on the board
888 outb_control(0, dev);
891 * clear any pending interrupts
893 inb_command(dev->base_addr);
894 adapter_reset(dev);
897 * no receive PCBs active
899 adapter->rx_active = 0;
901 adapter->busy = 0;
902 adapter->send_pcb_semaphore = 0;
903 adapter->rx_backlog.in = 0;
904 adapter->rx_backlog.out = 0;
906 spin_lock_init(&adapter->lock);
909 * install our interrupt service routine
911 if ((retval = request_irq(dev->irq, &elp_interrupt, 0, dev->name, dev))) {
912 printk(KERN_ERR "%s: could not allocate IRQ%d\n", dev->name, dev->irq);
913 return retval;
915 if ((retval = request_dma(dev->dma, dev->name))) {
916 free_irq(dev->irq, dev);
917 printk(KERN_ERR "%s: could not allocate DMA%d channel\n", dev->name, dev->dma);
918 return retval;
920 adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
921 if (!adapter->dma_buffer) {
922 printk(KERN_ERR "%s: could not allocate DMA buffer\n", dev->name);
923 free_dma(dev->dma);
924 free_irq(dev->irq, dev);
925 return -ENOMEM;
927 adapter->dmaing = 0;
930 * enable interrupts on the board
932 outb_control(CMDE, dev);
935 * configure adapter memory: we need 10 multicast addresses, default==0
937 if (elp_debug >= 3)
938 printk(KERN_DEBUG "%s: sending 3c505 memory configuration command\n", dev->name);
939 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
940 adapter->tx_pcb.data.memconf.cmd_q = 10;
941 adapter->tx_pcb.data.memconf.rcv_q = 20;
942 adapter->tx_pcb.data.memconf.mcast = 10;
943 adapter->tx_pcb.data.memconf.frame = 20;
944 adapter->tx_pcb.data.memconf.rcv_b = 20;
945 adapter->tx_pcb.data.memconf.progs = 0;
946 adapter->tx_pcb.length = sizeof(struct Memconf);
947 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
948 if (!send_pcb(dev, &adapter->tx_pcb))
949 printk(KERN_ERR "%s: couldn't send memory configuration command\n", dev->name);
950 else {
951 unsigned long timeout = jiffies + TIMEOUT;
952 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
953 if (time_after_eq(jiffies, timeout))
954 TIMEOUT_MSG(__LINE__);
959 * configure adapter to receive broadcast messages and wait for response
961 if (elp_debug >= 3)
962 printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
963 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
964 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
965 adapter->tx_pcb.length = 2;
966 adapter->got[CMD_CONFIGURE_82586] = 0;
967 if (!send_pcb(dev, &adapter->tx_pcb))
968 printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
969 else {
970 unsigned long timeout = jiffies + TIMEOUT;
971 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
972 if (time_after_eq(jiffies, timeout))
973 TIMEOUT_MSG(__LINE__);
976 /* enable burst-mode DMA */
977 /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
980 * queue receive commands to provide buffering
982 prime_rx(dev);
983 if (elp_debug >= 3)
984 printk(KERN_DEBUG "%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
987 * device is now officially open!
990 netif_start_queue(dev);
991 return 0;
995 /******************************************************
997 * send a packet to the adapter
999 ******************************************************/
1001 static int send_packet(struct net_device *dev, struct sk_buff *skb)
1003 elp_device *adapter = dev->priv;
1004 unsigned long target;
1005 unsigned long flags;
1008 * make sure the length is even and no shorter than 60 bytes
1010 unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
1012 if (test_and_set_bit(0, (void *) &adapter->busy)) {
1013 if (elp_debug >= 2)
1014 printk(KERN_DEBUG "%s: transmit blocked\n", dev->name);
1015 return FALSE;
1018 adapter->stats.tx_bytes += nlen;
1021 * send the adapter a transmit packet command. Ignore segment and offset
1022 * and make sure the length is even
1024 adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1025 adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1026 adapter->tx_pcb.data.xmit_pkt.buf_ofs
1027 = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
1028 adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1030 if (!send_pcb(dev, &adapter->tx_pcb)) {
1031 adapter->busy = 0;
1032 return FALSE;
1034 /* if this happens, we die */
1035 if (test_and_set_bit(0, (void *) &adapter->dmaing))
1036 printk(KERN_DEBUG "%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1038 adapter->current_dma.direction = 1;
1039 adapter->current_dma.start_time = jiffies;
1041 if ((unsigned long)(skb->data + nlen) >= MAX_DMA_ADDRESS || nlen != skb->len) {
1042 memcpy(adapter->dma_buffer, skb->data, nlen);
1043 memset(adapter->dma_buffer+skb->len, 0, nlen-skb->len);
1044 target = isa_virt_to_bus(adapter->dma_buffer);
1046 else {
1047 target = isa_virt_to_bus(skb->data);
1049 adapter->current_dma.skb = skb;
1051 flags=claim_dma_lock();
1052 disable_dma(dev->dma);
1053 clear_dma_ff(dev->dma);
1054 set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
1055 set_dma_addr(dev->dma, target);
1056 set_dma_count(dev->dma, nlen);
1057 outb_control(adapter->hcr_val | DMAE | TCEN, dev);
1058 enable_dma(dev->dma);
1059 release_dma_lock(flags);
1061 if (elp_debug >= 3)
1062 printk(KERN_DEBUG "%s: DMA transfer started\n", dev->name);
1064 return TRUE;
1068 * The upper layer thinks we timed out
1071 static void elp_timeout(struct net_device *dev)
1073 elp_device *adapter = dev->priv;
1074 int stat;
1076 stat = inb_status(dev->base_addr);
1077 printk(KERN_WARNING "%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1078 if (elp_debug >= 1)
1079 printk(KERN_DEBUG "%s: status %#02x\n", dev->name, stat);
1080 dev->trans_start = jiffies;
1081 adapter->stats.tx_dropped++;
1082 netif_wake_queue(dev);
1085 /******************************************************
1087 * start the transmitter
1088 * return 0 if sent OK, else return 1
1090 ******************************************************/
1092 static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1094 unsigned long flags;
1095 elp_device *adapter = dev->priv;
1097 spin_lock_irqsave(&adapter->lock, flags);
1098 check_3c505_dma(dev);
1100 if (elp_debug >= 3)
1101 printk(KERN_DEBUG "%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1103 netif_stop_queue(dev);
1106 * send the packet at skb->data for skb->len
1108 if (!send_packet(dev, skb)) {
1109 if (elp_debug >= 2) {
1110 printk(KERN_DEBUG "%s: failed to transmit packet\n", dev->name);
1112 spin_unlock_irqrestore(&adapter->lock, flags);
1113 return 1;
1115 if (elp_debug >= 3)
1116 printk(KERN_DEBUG "%s: packet of length %d sent\n", dev->name, (int) skb->len);
1119 * start the transmit timeout
1121 dev->trans_start = jiffies;
1123 prime_rx(dev);
1124 spin_unlock_irqrestore(&adapter->lock, flags);
1125 netif_start_queue(dev);
1126 return 0;
1129 /******************************************************
1131 * return statistics on the board
1133 ******************************************************/
1135 static struct net_device_stats *elp_get_stats(struct net_device *dev)
1137 elp_device *adapter = (elp_device *) dev->priv;
1139 if (elp_debug >= 3)
1140 printk(KERN_DEBUG "%s: request for stats\n", dev->name);
1142 /* If the device is closed, just return the latest stats we have,
1143 - we cannot ask from the adapter without interrupts */
1144 if (!netif_running(dev))
1145 return &adapter->stats;
1147 /* send a get statistics command to the board */
1148 adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1149 adapter->tx_pcb.length = 0;
1150 adapter->got[CMD_NETWORK_STATISTICS] = 0;
1151 if (!send_pcb(dev, &adapter->tx_pcb))
1152 printk(KERN_ERR "%s: couldn't send get statistics command\n", dev->name);
1153 else {
1154 unsigned long timeout = jiffies + TIMEOUT;
1155 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
1156 if (time_after_eq(jiffies, timeout)) {
1157 TIMEOUT_MSG(__LINE__);
1158 return &adapter->stats;
1162 /* statistics are now up to date */
1163 return &adapter->stats;
1167 * netdev_ethtool_ioctl: Handle network interface SIOCETHTOOL ioctls
1168 * @dev: network interface on which out-of-band action is to be performed
1169 * @useraddr: userspace address to which data is to be read and returned
1171 * Process the various commands of the SIOCETHTOOL interface.
1174 static int netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
1176 u32 ethcmd;
1178 /* dev_ioctl() in ../../net/core/dev.c has already checked
1179 capable(CAP_NET_ADMIN), so don't bother with that here. */
1181 if (get_user(ethcmd, (u32 *)useraddr))
1182 return -EFAULT;
1184 switch (ethcmd) {
1186 case ETHTOOL_GDRVINFO: {
1187 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
1188 strcpy (info.driver, DRV_NAME);
1189 strcpy (info.version, DRV_VERSION);
1190 sprintf(info.bus_info, "ISA 0x%lx", dev->base_addr);
1191 if (copy_to_user (useraddr, &info, sizeof (info)))
1192 return -EFAULT;
1193 return 0;
1196 /* get message-level */
1197 case ETHTOOL_GMSGLVL: {
1198 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
1199 edata.data = debug;
1200 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1201 return -EFAULT;
1202 return 0;
1204 /* set message-level */
1205 case ETHTOOL_SMSGLVL: {
1206 struct ethtool_value edata;
1207 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1208 return -EFAULT;
1209 debug = edata.data;
1210 return 0;
1213 default:
1214 break;
1217 return -EOPNOTSUPP;
1221 * netdev_ioctl: Handle network interface ioctls
1222 * @dev: network interface on which out-of-band action is to be performed
1223 * @rq: user request data
1224 * @cmd: command issued by user
1226 * Process the various out-of-band ioctls passed to this driver.
1229 static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1231 int rc = 0;
1233 switch (cmd) {
1234 case SIOCETHTOOL:
1235 rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
1236 break;
1238 default:
1239 rc = -EOPNOTSUPP;
1240 break;
1243 return rc;
1247 /******************************************************
1249 * close the board
1251 ******************************************************/
1253 static int elp_close(struct net_device *dev)
1255 elp_device *adapter;
1257 adapter = dev->priv;
1259 if (elp_debug >= 3)
1260 printk(KERN_DEBUG "%s: request to close device\n", dev->name);
1262 netif_stop_queue(dev);
1264 /* Someone may request the device statistic information even when
1265 * the interface is closed. The following will update the statistics
1266 * structure in the driver, so we'll be able to give current statistics.
1268 (void) elp_get_stats(dev);
1271 * disable interrupts on the board
1273 outb_control(0, dev);
1276 * release the IRQ
1278 free_irq(dev->irq, dev);
1280 free_dma(dev->dma);
1281 free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
1283 return 0;
1287 /************************************************************
1289 * Set multicast list
1290 * num_addrs==0: clear mc_list
1291 * num_addrs==-1: set promiscuous mode
1292 * num_addrs>0: set mc_list
1294 ************************************************************/
1296 static void elp_set_mc_list(struct net_device *dev)
1298 elp_device *adapter = (elp_device *) dev->priv;
1299 struct dev_mc_list *dmi = dev->mc_list;
1300 int i;
1301 unsigned long flags;
1303 if (elp_debug >= 3)
1304 printk(KERN_DEBUG "%s: request to set multicast list\n", dev->name);
1306 spin_lock_irqsave(&adapter->lock, flags);
1308 if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1309 /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1310 /* if num_addrs==0 the list will be cleared */
1311 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1312 adapter->tx_pcb.length = 6 * dev->mc_count;
1313 for (i = 0; i < dev->mc_count; i++) {
1314 memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1315 dmi = dmi->next;
1317 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1318 if (!send_pcb(dev, &adapter->tx_pcb))
1319 printk(KERN_ERR "%s: couldn't send set_multicast command\n", dev->name);
1320 else {
1321 unsigned long timeout = jiffies + TIMEOUT;
1322 while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
1323 if (time_after_eq(jiffies, timeout)) {
1324 TIMEOUT_MSG(__LINE__);
1327 if (dev->mc_count)
1328 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1329 else /* num_addrs == 0 */
1330 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1331 } else
1332 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1334 * configure adapter to receive messages (as specified above)
1335 * and wait for response
1337 if (elp_debug >= 3)
1338 printk(KERN_DEBUG "%s: sending 82586 configure command\n", dev->name);
1339 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1340 adapter->tx_pcb.length = 2;
1341 adapter->got[CMD_CONFIGURE_82586] = 0;
1342 if (!send_pcb(dev, &adapter->tx_pcb))
1344 spin_unlock_irqrestore(&adapter->lock, flags);
1345 printk(KERN_ERR "%s: couldn't send 82586 configure command\n", dev->name);
1347 else {
1348 unsigned long timeout = jiffies + TIMEOUT;
1349 spin_unlock_irqrestore(&adapter->lock, flags);
1350 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
1351 if (time_after_eq(jiffies, timeout))
1352 TIMEOUT_MSG(__LINE__);
1356 /******************************************************
1358 * initialise Etherlink Plus board
1360 ******************************************************/
1362 static inline void elp_init(struct net_device *dev)
1364 elp_device *adapter = dev->priv;
1367 * set ptrs to various functions
1369 dev->open = elp_open; /* local */
1370 dev->stop = elp_close; /* local */
1371 dev->get_stats = elp_get_stats; /* local */
1372 dev->hard_start_xmit = elp_start_xmit; /* local */
1373 dev->tx_timeout = elp_timeout; /* local */
1374 dev->watchdog_timeo = 10*HZ;
1375 dev->set_multicast_list = elp_set_mc_list; /* local */
1376 dev->do_ioctl = netdev_ioctl; /* local */
1378 /* Setup the generic properties */
1379 ether_setup(dev);
1382 * setup ptr to adapter specific information
1384 memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
1387 * memory information
1389 dev->mem_start = dev->mem_end = 0;
1392 /************************************************************
1394 * A couple of tests to see if there's 3C505 or not
1395 * Called only by elp_autodetect
1396 ************************************************************/
1398 static int __init elp_sense(struct net_device *dev)
1400 int addr = dev->base_addr;
1401 const char *name = dev->name;
1402 byte orig_HSR;
1404 if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
1405 return -ENODEV;
1407 orig_HSR = inb_status(addr);
1409 if (elp_debug > 0)
1410 printk(search_msg, name, addr);
1412 if (orig_HSR == 0xff) {
1413 if (elp_debug > 0)
1414 printk(notfound_msg, 1);
1415 goto out;
1418 /* Wait for a while; the adapter may still be booting up */
1419 if (elp_debug > 0)
1420 printk(stilllooking_msg);
1422 if (orig_HSR & DIR) {
1423 /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1424 outb(0, dev->base_addr + PORT_CONTROL);
1425 set_current_state(TASK_UNINTERRUPTIBLE);
1426 schedule_timeout(30*HZ/100);
1427 if (inb_status(addr) & DIR) {
1428 if (elp_debug > 0)
1429 printk(notfound_msg, 2);
1430 goto out;
1432 } else {
1433 /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1434 outb(DIR, dev->base_addr + PORT_CONTROL);
1435 set_current_state(TASK_UNINTERRUPTIBLE);
1436 schedule_timeout(30*HZ/100);
1437 if (!(inb_status(addr) & DIR)) {
1438 if (elp_debug > 0)
1439 printk(notfound_msg, 3);
1440 goto out;
1444 * It certainly looks like a 3c505.
1446 if (elp_debug > 0)
1447 printk(found_msg);
1449 return 0;
1450 out:
1451 release_region(addr, ELP_IO_EXTENT);
1452 return -ENODEV;
1455 /*************************************************************
1457 * Search through addr_list[] and try to find a 3C505
1458 * Called only by eplus_probe
1459 *************************************************************/
1461 static int __init elp_autodetect(struct net_device *dev)
1463 int idx = 0;
1465 /* if base address set, then only check that address
1466 otherwise, run through the table */
1467 if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
1468 if (elp_sense(dev) == 0)
1469 return dev->base_addr;
1470 } else
1471 while ((dev->base_addr = addr_list[idx++])) {
1472 if (elp_sense(dev) == 0)
1473 return dev->base_addr;
1476 /* could not find an adapter */
1477 if (elp_debug > 0)
1478 printk(couldnot_msg, dev->name);
1480 return 0; /* Because of this, the layer above will return -ENODEV */
1484 /******************************************************
1486 * probe for an Etherlink Plus board at the specified address
1488 ******************************************************/
1490 /* There are three situations we need to be able to detect here:
1492 * a) the card is idle
1493 * b) the card is still booting up
1494 * c) the card is stuck in a strange state (some DOS drivers do this)
1496 * In case (a), all is well. In case (b), we wait 10 seconds to see if the
1497 * card finishes booting, and carry on if so. In case (c), we do a hard reset,
1498 * loop round, and hope for the best.
1500 * This is all very unpleasant, but hopefully avoids the problems with the old
1501 * probe code (which had a 15-second delay if the card was idle, and didn't
1502 * work at all if it was in a weird state).
1505 int __init elplus_probe(struct net_device *dev)
1507 elp_device *adapter;
1508 int i, tries, tries1, okay;
1509 unsigned long timeout;
1510 unsigned long cookie = 0;
1512 SET_MODULE_OWNER(dev);
1515 * setup adapter structure
1518 dev->base_addr = elp_autodetect(dev);
1519 if (!(dev->base_addr))
1520 return -ENODEV;
1523 * setup ptr to adapter specific information
1525 adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1526 if (adapter == NULL) {
1527 printk(KERN_ERR "%s: out of memory\n", dev->name);
1528 return -ENODEV;
1531 adapter->send_pcb_semaphore = 0;
1533 for (tries1 = 0; tries1 < 3; tries1++) {
1534 outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1535 /* First try to write just one byte, to see if the card is
1536 * responding at all normally.
1538 timeout = jiffies + 5*HZ/100;
1539 okay = 0;
1540 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1541 if ((inb_status(dev->base_addr) & HCRE)) {
1542 outb_command(0, dev->base_addr); /* send a spurious byte */
1543 timeout = jiffies + 5*HZ/100;
1544 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1545 if (inb_status(dev->base_addr) & HCRE)
1546 okay = 1;
1548 if (!okay) {
1549 /* Nope, it's ignoring the command register. This means that
1550 * either it's still booting up, or it's died.
1552 printk(KERN_ERR "%s: command register wouldn't drain, ", dev->name);
1553 if ((inb_status(dev->base_addr) & 7) == 3) {
1554 /* If the adapter status is 3, it *could* still be booting.
1555 * Give it the benefit of the doubt for 10 seconds.
1557 printk("assuming 3c505 still starting\n");
1558 timeout = jiffies + 10*HZ;
1559 while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1560 if (inb_status(dev->base_addr) & 7) {
1561 printk(KERN_ERR "%s: 3c505 failed to start\n", dev->name);
1562 } else {
1563 okay = 1; /* It started */
1565 } else {
1566 /* Otherwise, it must just be in a strange
1567 * state. We probably need to kick it.
1569 printk("3c505 is sulking\n");
1572 for (tries = 0; tries < 5 && okay; tries++) {
1575 * Try to set the Ethernet address, to make sure that the board
1576 * is working.
1578 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1579 adapter->tx_pcb.length = 0;
1580 cookie = probe_irq_on();
1581 if (!send_pcb(dev, &adapter->tx_pcb)) {
1582 printk(KERN_ERR "%s: could not send first PCB\n", dev->name);
1583 probe_irq_off(cookie);
1584 continue;
1586 if (!receive_pcb(dev, &adapter->rx_pcb)) {
1587 printk(KERN_ERR "%s: could not read first PCB\n", dev->name);
1588 probe_irq_off(cookie);
1589 continue;
1591 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1592 (adapter->rx_pcb.length != 6)) {
1593 printk(KERN_ERR "%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1594 probe_irq_off(cookie);
1595 continue;
1597 goto okay;
1599 /* It's broken. Do a hard reset to re-initialise the board,
1600 * and try again.
1602 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1603 outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1604 outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1606 printk(KERN_ERR "%s: failed to initialise 3c505\n", dev->name);
1607 release_region(dev->base_addr, ELP_IO_EXTENT);
1608 return -ENODEV;
1610 okay:
1611 if (dev->irq) { /* Is there a preset IRQ? */
1612 int rpt = probe_irq_off(cookie);
1613 if (dev->irq != rpt) {
1614 printk(KERN_WARNING "%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1616 /* if dev->irq == probe_irq_off(cookie), all is well */
1617 } else /* No preset IRQ; just use what we can detect */
1618 dev->irq = probe_irq_off(cookie);
1619 switch (dev->irq) { /* Legal, sane? */
1620 case 0:
1621 printk(KERN_ERR "%s: IRQ probe failed: check 3c505 jumpers.\n",
1622 dev->name);
1623 return -ENODEV;
1624 case 1:
1625 case 6:
1626 case 8:
1627 case 13:
1628 printk(KERN_ERR "%s: Impossible IRQ %d reported by probe_irq_off().\n",
1629 dev->name, dev->irq);
1630 return -ENODEV;
1633 * Now we have the IRQ number so we can disable the interrupts from
1634 * the board until the board is opened.
1636 outb_control(adapter->hcr_val & ~CMDE, dev);
1639 * copy Ethernet address into structure
1641 for (i = 0; i < 6; i++)
1642 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1644 /* find a DMA channel */
1645 if (!dev->dma) {
1646 if (dev->mem_start) {
1647 dev->dma = dev->mem_start & 7;
1649 else {
1650 printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
1651 dev->dma = ELP_DMA;
1656 * print remainder of startup message
1658 printk(KERN_INFO "%s: 3c505 at %#lx, irq %d, dma %d, ",
1659 dev->name, dev->base_addr, dev->irq, dev->dma);
1660 printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1661 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1662 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1665 * read more information from the adapter
1668 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1669 adapter->tx_pcb.length = 0;
1670 if (!send_pcb(dev, &adapter->tx_pcb) ||
1671 !receive_pcb(dev, &adapter->rx_pcb) ||
1672 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1673 (adapter->rx_pcb.length != 10)) {
1674 printk("not responding to second PCB\n");
1676 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);
1679 * reconfigure the adapter memory to better suit our purposes
1681 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1682 adapter->tx_pcb.length = 12;
1683 adapter->tx_pcb.data.memconf.cmd_q = 8;
1684 adapter->tx_pcb.data.memconf.rcv_q = 8;
1685 adapter->tx_pcb.data.memconf.mcast = 10;
1686 adapter->tx_pcb.data.memconf.frame = 10;
1687 adapter->tx_pcb.data.memconf.rcv_b = 10;
1688 adapter->tx_pcb.data.memconf.progs = 0;
1689 if (!send_pcb(dev, &adapter->tx_pcb) ||
1690 !receive_pcb(dev, &adapter->rx_pcb) ||
1691 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1692 (adapter->rx_pcb.length != 2)) {
1693 printk(KERN_ERR "%s: could not configure adapter memory\n", dev->name);
1695 if (adapter->rx_pcb.data.configure) {
1696 printk(KERN_ERR "%s: adapter configuration failed\n", dev->name);
1700 * initialise the device
1702 elp_init(dev);
1704 return 0;
1707 #ifdef MODULE
1708 static struct net_device dev_3c505[ELP_MAX_CARDS];
1709 static int io[ELP_MAX_CARDS];
1710 static int irq[ELP_MAX_CARDS];
1711 static int dma[ELP_MAX_CARDS];
1712 MODULE_PARM(io, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1713 MODULE_PARM(irq, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1714 MODULE_PARM(dma, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1715 MODULE_PARM_DESC(io, "EtherLink Plus I/O base address(es)");
1716 MODULE_PARM_DESC(irq, "EtherLink Plus IRQ number(s) (assigned)");
1717 MODULE_PARM_DESC(dma, "EtherLink Plus DMA channel(s)");
1719 int init_module(void)
1721 int this_dev, found = 0;
1723 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1724 struct net_device *dev = &dev_3c505[this_dev];
1725 dev->irq = irq[this_dev];
1726 dev->base_addr = io[this_dev];
1727 dev->init = elplus_probe;
1728 if (dma[this_dev]) {
1729 dev->dma = dma[this_dev];
1730 } else {
1731 dev->dma = ELP_DMA;
1732 printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
1734 if (io[this_dev] == 0) {
1735 if (this_dev) break;
1736 printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
1738 if (register_netdev(dev) != 0) {
1739 printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
1740 if (found != 0) return 0;
1741 return -ENXIO;
1743 found++;
1745 return 0;
1748 void cleanup_module(void)
1750 int this_dev;
1752 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1753 struct net_device *dev = &dev_3c505[this_dev];
1754 if (dev->priv != NULL) {
1755 unregister_netdev(dev);
1756 kfree(dev->priv);
1757 dev->priv = NULL;
1758 release_region(dev->base_addr, ELP_IO_EXTENT);
1763 #endif /* MODULE */
1764 MODULE_LICENSE("GPL");