Ok, test10-final is out there now. This has no _known_ bugs that I
[davej-history.git] / drivers / net / 3c505.c
blob22485f1d8a87a29830d1dc87a0f0b2b6fbfcaf66
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 * Crynwr packet driver by
24 * Krishnan Gopalan and Gregg Stefancik,
25 * Clemson University Engineering Computer Operations.
26 * Portions of the code have been adapted from the 3c505
27 * driver for NCSA Telnet by Bruce Orchard and later
28 * modified by Warren Van Houten and krus@diku.dk.
29 * 3C505 technical information provided by
30 * Terry Murphy, of 3Com Network Adapter Division
31 * Linux 1.3.0 changes by
32 * Alan Cox <Alan.Cox@linux.org>
33 * More debugging, DMA support, currently maintained by
34 * Philip Blundell <Philip.Blundell@pobox.com>
35 * Multicard/soft configurable dma channel/rev 2 hardware support
36 * by Christopher Collins <ccollins@pcug.org.au>
39 /* Theory of operation:
41 * The 3c505 is quite an intelligent board. All communication with it is done
42 * by means of Primary Command Blocks (PCBs); these are transferred using PIO
43 * through the command register. The card has 256k of on-board RAM, which is
44 * used to buffer received packets. It might seem at first that more buffers
45 * are better, but in fact this isn't true. From my tests, it seems that
46 * more than about 10 buffers are unnecessary, and there is a noticeable
47 * performance hit in having more active on the card. So the majority of the
48 * card's memory isn't, in fact, used. Sadly, the card only has one transmit
49 * buffer and, short of loading our own firmware into it (which is what some
50 * drivers resort to) there's nothing we can do about this.
52 * We keep up to 4 "receive packet" commands active on the board at a time.
53 * When a packet comes in, so long as there is a receive command active, the
54 * board will send us a "packet received" PCB and then add the data for that
55 * packet to the DMA queue. If a DMA transfer is not already in progress, we
56 * set one up to start uploading the data. We have to maintain a list of
57 * backlogged receive packets, because the card may decide to tell us about
58 * a newly-arrived packet at any time, and we may not be able to start a DMA
59 * transfer immediately (ie one may already be going on). We can't NAK the
60 * PCB, because then it would throw the packet away.
62 * Trying to send a PCB to the card at the wrong moment seems to have bad
63 * effects. If we send it a transmit PCB while a receive DMA is happening,
64 * it will just NAK the PCB and so we will have wasted our time. Worse, it
65 * sometimes seems to interrupt the transfer. The majority of the low-level
66 * code is protected by one huge semaphore -- "busy" -- which is set whenever
67 * it probably isn't safe to do anything to the card. The receive routine
68 * must gain a lock on "busy" before it can start a DMA transfer, and the
69 * transmit routine must gain a lock before it sends the first PCB to the card.
70 * The send_pcb() routine also has an internal semaphore to protect it against
71 * being re-entered (which would be disastrous) -- this is needed because
72 * several things can happen asynchronously (re-priming the receiver and
73 * asking the card for statistics, for example). send_pcb() will also refuse
74 * to talk to the card at all if a DMA upload is happening. The higher-level
75 * networking code will reschedule a later retry if some part of the driver
76 * is blocked. In practice, this doesn't seem to happen very often.
79 /* This driver may now work with revision 2.x hardware, since all the read
80 * operations on the HCR have been removed (we now keep our own softcopy).
81 * But I don't have an old card to test it on.
83 * This has had the bad effect that the autoprobe routine is now a bit
84 * less friendly to other devices. However, it was never very good.
85 * before, so I doubt it will hurt anybody.
88 /* The driver is a mess. I took Craig's and Juha's code, and hacked it firstly
89 * to make it more reliable, and secondly to add DMA mode. Many things could
90 * probably be done better; the concurrency protection is particularly awful.
93 #include <linux/module.h>
95 #include <linux/kernel.h>
96 #include <linux/sched.h>
97 #include <linux/string.h>
98 #include <linux/interrupt.h>
99 #include <linux/ptrace.h>
100 #include <linux/errno.h>
101 #include <linux/in.h>
102 #include <linux/malloc.h>
103 #include <linux/ioport.h>
104 #include <linux/spinlock.h>
105 #include <asm/bitops.h>
106 #include <asm/io.h>
107 #include <asm/dma.h>
109 #include <linux/netdevice.h>
110 #include <linux/etherdevice.h>
111 #include <linux/skbuff.h>
112 #include <linux/init.h>
114 #include "3c505.h"
116 /*********************************************************
118 * define debug messages here as common strings to reduce space
120 *********************************************************/
122 static const char *filename = __FILE__;
124 static const char *timeout_msg = "*** timeout at %s:%s (line %d) ***\n";
125 #define TIMEOUT_MSG(lineno) \
126 printk(timeout_msg, filename,__FUNCTION__,(lineno))
128 static const char *invalid_pcb_msg =
129 "*** invalid pcb length %d at %s:%s (line %d) ***\n";
130 #define INVALID_PCB_MSG(len) \
131 printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
133 static char search_msg[] __initdata = "%s: Looking for 3c505 adapter at address %#x...";
135 static char stilllooking_msg[] __initdata = "still looking...";
137 static char found_msg[] __initdata = "found.\n";
139 static char notfound_msg[] __initdata = "not found (reason = %d)\n";
141 static char couldnot_msg[] __initdata = "%s: 3c505 not found\n";
143 /*********************************************************
145 * various other debug stuff
147 *********************************************************/
149 #ifdef ELP_DEBUG
150 static const int elp_debug = ELP_DEBUG;
151 #else
152 static const int elp_debug = 0;
153 #endif
156 * 0 = no messages (well, some)
157 * 1 = messages when high level commands performed
158 * 2 = messages when low level commands performed
159 * 3 = messages when interrupts received
162 /*****************************************************************
164 * useful macros
166 *****************************************************************/
168 #ifndef TRUE
169 #define TRUE 1
170 #endif
172 #ifndef FALSE
173 #define FALSE 0
174 #endif
177 /*****************************************************************
179 * List of I/O-addresses we try to auto-sense
180 * Last element MUST BE 0!
181 *****************************************************************/
183 static int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
185 /* Dma Memory related stuff */
187 static unsigned long dma_mem_alloc(int size)
189 int order = get_order(size);
191 return __get_dma_pages(GFP_KERNEL, order);
195 /*****************************************************************
197 * Functions for I/O (note the inline !)
199 *****************************************************************/
201 static inline unsigned char inb_status(unsigned int base_addr)
203 return inb(base_addr + PORT_STATUS);
206 static inline int inb_command(unsigned int base_addr)
208 return inb(base_addr + PORT_COMMAND);
211 static inline void outb_control(unsigned char val, struct net_device *dev)
213 outb(val, dev->base_addr + PORT_CONTROL);
214 ((elp_device *)(dev->priv))->hcr_val = val;
217 #define HCR_VAL(x) (((elp_device *)((x)->priv))->hcr_val)
219 static inline void outb_command(unsigned char val, unsigned int base_addr)
221 outb(val, base_addr + PORT_COMMAND);
224 static inline unsigned int inw_data(unsigned int base_addr)
226 return inw(base_addr + PORT_DATA);
229 static inline void outw_data(unsigned int val, unsigned int base_addr)
231 outw(val, base_addr + PORT_DATA);
234 static inline unsigned int backlog_next(unsigned int n)
236 return (n + 1) % BACKLOG_SIZE;
239 /*****************************************************************
241 * useful functions for accessing the adapter
243 *****************************************************************/
246 * use this routine when accessing the ASF bits as they are
247 * changed asynchronously by the adapter
250 /* get adapter PCB status */
251 #define GET_ASF(addr) \
252 (get_status(addr)&ASF_PCB_MASK)
254 static inline int get_status(unsigned int base_addr)
256 int timeout = jiffies + 10*HZ/100;
257 register int stat1;
258 do {
259 stat1 = inb_status(base_addr);
260 } while (stat1 != inb_status(base_addr) && time_before(jiffies, timeout));
261 if (time_after_eq(jiffies, timeout))
262 TIMEOUT_MSG(__LINE__);
263 return stat1;
266 static inline void set_hsf(struct net_device *dev, int hsf)
268 cli();
269 outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
270 sti();
273 static int start_receive(struct net_device *, pcb_struct *);
275 inline static void adapter_reset(struct net_device *dev)
277 int timeout;
278 elp_device *adapter = dev->priv;
279 unsigned char orig_hcr = adapter->hcr_val;
281 outb_control(0, dev);
283 if (inb_status(dev->base_addr) & ACRF) {
284 do {
285 inb_command(dev->base_addr);
286 timeout = jiffies + 2*HZ/100;
287 while (time_before_eq(jiffies, timeout) && !(inb_status(dev->base_addr) & ACRF));
288 } while (inb_status(dev->base_addr) & ACRF);
289 set_hsf(dev, HSF_PCB_NAK);
291 outb_control(adapter->hcr_val | ATTN | DIR, dev);
292 timeout = jiffies + 1*HZ/100;
293 while (time_before_eq(jiffies, timeout));
294 outb_control(adapter->hcr_val & ~ATTN, dev);
295 timeout = jiffies + 1*HZ/100;
296 while (time_before_eq(jiffies, timeout));
297 outb_control(adapter->hcr_val | FLSH, dev);
298 timeout = jiffies + 1*HZ/100;
299 while (time_before_eq(jiffies, timeout));
300 outb_control(adapter->hcr_val & ~FLSH, dev);
301 timeout = jiffies + 1*HZ/100;
302 while (time_before_eq(jiffies, timeout));
304 outb_control(orig_hcr, dev);
305 if (!start_receive(dev, &adapter->tx_pcb))
306 printk("%s: start receive command failed \n", dev->name);
309 /* Check to make sure that a DMA transfer hasn't timed out. This should
310 * never happen in theory, but seems to occur occasionally if the card gets
311 * prodded at the wrong time.
313 static inline void check_3c505_dma(struct net_device *dev)
315 elp_device *adapter = dev->priv;
316 if (adapter->dmaing && time_after(jiffies, adapter->current_dma.start_time + 10)) {
317 unsigned long flags, f;
318 printk("%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
319 save_flags(flags);
320 cli();
321 adapter->dmaing = 0;
322 adapter->busy = 0;
324 f=claim_dma_lock();
325 disable_dma(dev->dma);
326 release_dma_lock(f);
328 if (adapter->rx_active)
329 adapter->rx_active--;
330 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
331 restore_flags(flags);
335 /* Primitive functions used by send_pcb() */
336 static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
338 unsigned int timeout;
339 outb_command(byte, base_addr);
340 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
341 if (inb_status(base_addr) & HCRE)
342 return FALSE;
344 printk("3c505: send_pcb_slow timed out\n");
345 return TRUE;
348 static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
350 unsigned int timeout;
351 outb_command(byte, base_addr);
352 for (timeout = 0; timeout < 40000; timeout++) {
353 if (inb_status(base_addr) & HCRE)
354 return FALSE;
356 printk("3c505: send_pcb_fast timed out\n");
357 return TRUE;
360 /* Check to see if the receiver needs restarting, and kick it if so */
361 static inline void prime_rx(struct net_device *dev)
363 elp_device *adapter = dev->priv;
364 while (adapter->rx_active < ELP_RX_PCBS && netif_running(dev)) {
365 if (!start_receive(dev, &adapter->itx_pcb))
366 break;
370 /*****************************************************************
372 * send_pcb
373 * Send a PCB to the adapter.
375 * output byte to command reg --<--+
376 * wait until HCRE is non zero |
377 * loop until all bytes sent -->--+
378 * set HSF1 and HSF2 to 1
379 * output pcb length
380 * wait until ASF give ACK or NAK
381 * set HSF1 and HSF2 to 0
383 *****************************************************************/
385 /* This can be quite slow -- the adapter is allowed to take up to 40ms
386 * to respond to the initial interrupt.
388 * We run initially with interrupts turned on, but with a semaphore set
389 * so that nobody tries to re-enter this code. Once the first byte has
390 * gone through, we turn interrupts off and then send the others (the
391 * timeout is reduced to 500us).
394 static int send_pcb(struct net_device *dev, pcb_struct * pcb)
396 int i;
397 int timeout;
398 elp_device *adapter = dev->priv;
400 check_3c505_dma(dev);
402 if (adapter->dmaing && adapter->current_dma.direction == 0)
403 return FALSE;
405 /* Avoid contention */
406 if (test_and_set_bit(1, &adapter->send_pcb_semaphore)) {
407 if (elp_debug >= 3) {
408 printk("%s: send_pcb entered while threaded\n", dev->name);
410 return FALSE;
413 * load each byte into the command register and
414 * wait for the HCRE bit to indicate the adapter
415 * had read the byte
417 set_hsf(dev, 0);
419 if (send_pcb_slow(dev->base_addr, pcb->command))
420 goto abort;
422 cli();
424 if (send_pcb_fast(dev->base_addr, pcb->length))
425 goto sti_abort;
427 for (i = 0; i < pcb->length; i++) {
428 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
429 goto sti_abort;
432 outb_control(adapter->hcr_val | 3, dev); /* signal end of PCB */
433 outb_command(2 + pcb->length, dev->base_addr);
435 /* now wait for the acknowledgement */
436 sti();
438 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
439 switch (GET_ASF(dev->base_addr)) {
440 case ASF_PCB_ACK:
441 adapter->send_pcb_semaphore = 0;
442 return TRUE;
443 break;
444 case ASF_PCB_NAK:
445 #ifdef ELP_DEBUG
446 printk(KERN_DEBUG "%s: send_pcb got NAK\n", dev->name);
447 #endif
448 goto abort;
449 break;
453 if (elp_debug >= 1)
454 printk("%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
456 sti_abort:
457 sti();
458 abort:
459 adapter->send_pcb_semaphore = 0;
460 return FALSE;
464 /*****************************************************************
466 * receive_pcb
467 * Read a PCB from the adapter
469 * wait for ACRF to be non-zero ---<---+
470 * input a byte |
471 * if ASF1 and ASF2 were not both one |
472 * before byte was read, loop --->---+
473 * set HSF1 and HSF2 for ack
475 *****************************************************************/
477 static int receive_pcb(struct net_device *dev, pcb_struct * pcb)
479 int i, j;
480 int total_length;
481 int stat;
482 int timeout;
484 elp_device *adapter = dev->priv;
486 set_hsf(dev, 0);
488 /* get the command code */
489 timeout = jiffies + 2*HZ/100;
490 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
491 if (time_after_eq(jiffies, timeout)) {
492 TIMEOUT_MSG(__LINE__);
493 return FALSE;
495 pcb->command = inb_command(dev->base_addr);
497 /* read the data length */
498 timeout = jiffies + 3*HZ/100;
499 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
500 if (time_after_eq(jiffies, timeout)) {
501 TIMEOUT_MSG(__LINE__);
502 printk("%s: status %02x\n", dev->name, stat);
503 return FALSE;
505 pcb->length = inb_command(dev->base_addr);
507 if (pcb->length > MAX_PCB_DATA) {
508 INVALID_PCB_MSG(pcb->length);
509 adapter_reset(dev);
510 return FALSE;
512 /* read the data */
513 cli();
514 i = 0;
515 do {
516 j = 0;
517 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
518 pcb->data.raw[i++] = inb_command(dev->base_addr);
519 if (i > MAX_PCB_DATA)
520 INVALID_PCB_MSG(i);
521 } while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
522 sti();
523 if (j >= 20000) {
524 TIMEOUT_MSG(__LINE__);
525 return FALSE;
527 /* woops, the last "data" byte was really the length! */
528 total_length = pcb->data.raw[--i];
530 /* safety check total length vs data length */
531 if (total_length != (pcb->length + 2)) {
532 if (elp_debug >= 2)
533 printk("%s: mangled PCB received\n", dev->name);
534 set_hsf(dev, HSF_PCB_NAK);
535 return FALSE;
538 if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
539 if (test_and_set_bit(0, (void *) &adapter->busy)) {
540 if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
541 set_hsf(dev, HSF_PCB_NAK);
542 printk("%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
543 pcb->command = 0;
544 return TRUE;
545 } else {
546 pcb->command = 0xff;
550 set_hsf(dev, HSF_PCB_ACK);
551 return TRUE;
554 /******************************************************
556 * queue a receive command on the adapter so we will get an
557 * interrupt when a packet is received.
559 ******************************************************/
561 static int start_receive(struct net_device *dev, pcb_struct * tx_pcb)
563 int status;
564 elp_device *adapter = dev->priv;
566 if (elp_debug >= 3)
567 printk("%s: restarting receiver\n", dev->name);
568 tx_pcb->command = CMD_RECEIVE_PACKET;
569 tx_pcb->length = sizeof(struct Rcv_pkt);
570 tx_pcb->data.rcv_pkt.buf_seg
571 = tx_pcb->data.rcv_pkt.buf_ofs = 0; /* Unused */
572 tx_pcb->data.rcv_pkt.buf_len = 1600;
573 tx_pcb->data.rcv_pkt.timeout = 0; /* set timeout to zero */
574 status = send_pcb(dev, tx_pcb);
575 if (status)
576 adapter->rx_active++;
577 return status;
580 /******************************************************
582 * extract a packet from the adapter
583 * this routine is only called from within the interrupt
584 * service routine, so no cli/sti calls are needed
585 * note that the length is always assumed to be even
587 ******************************************************/
589 static void receive_packet(struct net_device *dev, int len)
591 int rlen;
592 elp_device *adapter = dev->priv;
593 void *target;
594 struct sk_buff *skb;
595 unsigned long flags;
597 rlen = (len + 1) & ~1;
598 skb = dev_alloc_skb(rlen + 2);
600 if (!skb) {
601 printk("%s: memory squeeze, dropping packet\n", dev->name);
602 target = adapter->dma_buffer;
603 adapter->current_dma.target = NULL;
604 } else {
605 skb_reserve(skb, 2);
606 target = skb_put(skb, rlen);
607 if (virt_to_bus(target + rlen) >= MAX_DMA_ADDRESS) {
608 adapter->current_dma.target = target;
609 target = adapter->dma_buffer;
610 } else {
611 adapter->current_dma.target = NULL;
615 /* if this happens, we die */
616 if (test_and_set_bit(0, (void *) &adapter->dmaing))
617 printk("%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
619 skb->dev = dev;
620 adapter->current_dma.direction = 0;
621 adapter->current_dma.length = rlen;
622 adapter->current_dma.skb = skb;
623 adapter->current_dma.start_time = jiffies;
625 outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
627 flags=claim_dma_lock();
628 disable_dma(dev->dma);
629 clear_dma_ff(dev->dma);
630 set_dma_mode(dev->dma, 0x04); /* dma read */
631 set_dma_addr(dev->dma, virt_to_bus(target));
632 set_dma_count(dev->dma, rlen);
633 enable_dma(dev->dma);
634 release_dma_lock(flags);
636 if (elp_debug >= 3) {
637 printk("%s: rx DMA transfer started\n", dev->name);
640 if (adapter->rx_active)
641 adapter->rx_active--;
643 if (!adapter->busy)
644 printk("%s: receive_packet called, busy not set.\n", dev->name);
647 /******************************************************
649 * interrupt handler
651 ******************************************************/
653 static void elp_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
655 int len;
656 int dlen;
657 int icount = 0;
658 struct net_device *dev;
659 elp_device *adapter;
660 int timeout;
662 dev = dev_id;
663 adapter = (elp_device *) dev->priv;
665 spin_lock(&adapter->lock);
667 do {
669 * has a DMA transfer finished?
671 if (inb_status(dev->base_addr) & DONE) {
672 if (!adapter->dmaing) {
673 printk("%s: phantom DMA completed\n", dev->name);
675 if (elp_debug >= 3) {
676 printk("%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
679 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
680 if (adapter->current_dma.direction) {
681 dev_kfree_skb_irq(adapter->current_dma.skb);
682 } else {
683 struct sk_buff *skb = adapter->current_dma.skb;
684 if (skb) {
685 if (adapter->current_dma.target) {
686 /* have already done the skb_put() */
687 memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
689 skb->protocol = eth_type_trans(skb,dev);
690 adapter->stats.rx_bytes += skb->len;
691 netif_rx(skb);
694 adapter->dmaing = 0;
695 if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
696 int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
697 adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
698 if (elp_debug >= 2)
699 printk("%s: receiving backlogged packet (%d)\n", dev->name, t);
700 receive_packet(dev, t);
701 } else {
702 adapter->busy = 0;
704 } else {
705 /* has one timed out? */
706 check_3c505_dma(dev);
710 * receive a PCB from the adapter
712 timeout = jiffies + 3*HZ/100;
713 while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
714 if (receive_pcb(dev, &adapter->irx_pcb)) {
715 switch (adapter->irx_pcb.command)
717 case 0:
718 break;
720 * received a packet - this must be handled fast
722 case 0xff:
723 case CMD_RECEIVE_PACKET_COMPLETE:
724 /* if the device isn't open, don't pass packets up the stack */
725 if (!netif_running(dev))
726 break;
727 len = adapter->irx_pcb.data.rcv_resp.pkt_len;
728 dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
729 if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
730 printk(KERN_ERR "%s: interrupt - packet not received correctly\n", dev->name);
731 } else {
732 if (elp_debug >= 3) {
733 printk("%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
735 if (adapter->irx_pcb.command == 0xff) {
736 if (elp_debug >= 2)
737 printk("%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
738 adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
739 adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
740 } else {
741 receive_packet(dev, dlen);
743 if (elp_debug >= 3)
744 printk("%s: packet received\n", dev->name);
746 break;
749 * 82586 configured correctly
751 case CMD_CONFIGURE_82586_RESPONSE:
752 adapter->got[CMD_CONFIGURE_82586] = 1;
753 if (elp_debug >= 3)
754 printk("%s: interrupt - configure response received\n", dev->name);
755 break;
758 * Adapter memory configuration
760 case CMD_CONFIGURE_ADAPTER_RESPONSE:
761 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
762 if (elp_debug >= 3)
763 printk("%s: Adapter memory configuration %s.\n", dev->name,
764 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
765 break;
768 * Multicast list loading
770 case CMD_LOAD_MULTICAST_RESPONSE:
771 adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
772 if (elp_debug >= 3)
773 printk("%s: Multicast address list loading %s.\n", dev->name,
774 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
775 break;
778 * Station address setting
780 case CMD_SET_ADDRESS_RESPONSE:
781 adapter->got[CMD_SET_STATION_ADDRESS] = 1;
782 if (elp_debug >= 3)
783 printk("%s: Ethernet address setting %s.\n", dev->name,
784 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
785 break;
789 * received board statistics
791 case CMD_NETWORK_STATISTICS_RESPONSE:
792 adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
793 adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
794 adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
795 adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
796 adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
797 adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
798 adapter->got[CMD_NETWORK_STATISTICS] = 1;
799 if (elp_debug >= 3)
800 printk("%s: interrupt - statistics response received\n", dev->name);
801 break;
804 * sent a packet
806 case CMD_TRANSMIT_PACKET_COMPLETE:
807 if (elp_debug >= 3)
808 printk("%s: interrupt - packet sent\n", dev->name);
809 if (!netif_running(dev))
810 break;
811 switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
812 case 0xffff:
813 adapter->stats.tx_aborted_errors++;
814 printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
815 break;
816 case 0xfffe:
817 adapter->stats.tx_fifo_errors++;
818 printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
819 break;
821 netif_wake_queue(dev);
822 break;
825 * some unknown PCB
827 default:
828 printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
829 break;
831 } else {
832 printk("%s: failed to read PCB on interrupt\n", dev->name);
833 adapter_reset(dev);
837 } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
839 prime_rx(dev);
842 * indicate no longer in interrupt routine
844 spin_unlock(&adapter->lock);
848 /******************************************************
850 * open the board
852 ******************************************************/
854 static int elp_open(struct net_device *dev)
856 elp_device *adapter;
858 adapter = dev->priv;
860 if (elp_debug >= 3)
861 printk("%s: request to open device\n", dev->name);
864 * make sure we actually found the device
866 if (adapter == NULL) {
867 printk("%s: Opening a non-existent physical device\n", dev->name);
868 return -EAGAIN;
871 * disable interrupts on the board
873 outb_control(0, dev);
876 * clear any pending interrupts
878 inb_command(dev->base_addr);
879 adapter_reset(dev);
882 * no receive PCBs active
884 adapter->rx_active = 0;
886 adapter->busy = 0;
887 adapter->send_pcb_semaphore = 0;
888 adapter->rx_backlog.in = 0;
889 adapter->rx_backlog.out = 0;
891 spin_lock_init(&adapter->lock);
894 * install our interrupt service routine
896 if (request_irq(dev->irq, &elp_interrupt, 0, "3c505", dev)) {
897 return -EAGAIN;
899 if (request_dma(dev->dma, "3c505")) {
900 printk("%s: could not allocate DMA channel\n", dev->name);
901 return -EAGAIN;
903 adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
904 if (!adapter->dma_buffer) {
905 printk("Could not allocate DMA buffer\n");
907 adapter->dmaing = 0;
910 * enable interrupts on the board
912 outb_control(CMDE, dev);
915 * configure adapter memory: we need 10 multicast addresses, default==0
917 if (elp_debug >= 3)
918 printk("%s: sending 3c505 memory configuration command\n", dev->name);
919 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
920 adapter->tx_pcb.data.memconf.cmd_q = 10;
921 adapter->tx_pcb.data.memconf.rcv_q = 20;
922 adapter->tx_pcb.data.memconf.mcast = 10;
923 adapter->tx_pcb.data.memconf.frame = 20;
924 adapter->tx_pcb.data.memconf.rcv_b = 20;
925 adapter->tx_pcb.data.memconf.progs = 0;
926 adapter->tx_pcb.length = sizeof(struct Memconf);
927 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
928 if (!send_pcb(dev, &adapter->tx_pcb))
929 printk("%s: couldn't send memory configuration command\n", dev->name);
930 else {
931 int timeout = jiffies + TIMEOUT;
932 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
933 if (time_after_eq(jiffies, timeout))
934 TIMEOUT_MSG(__LINE__);
939 * configure adapter to receive broadcast messages and wait for response
941 if (elp_debug >= 3)
942 printk("%s: sending 82586 configure command\n", dev->name);
943 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
944 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
945 adapter->tx_pcb.length = 2;
946 adapter->got[CMD_CONFIGURE_82586] = 0;
947 if (!send_pcb(dev, &adapter->tx_pcb))
948 printk("%s: couldn't send 82586 configure command\n", dev->name);
949 else {
950 int timeout = jiffies + TIMEOUT;
951 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
952 if (time_after_eq(jiffies, timeout))
953 TIMEOUT_MSG(__LINE__);
956 /* enable burst-mode DMA */
957 /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
960 * queue receive commands to provide buffering
962 prime_rx(dev);
963 if (elp_debug >= 3)
964 printk("%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
967 * device is now officially open!
970 netif_wake_queue(dev);
971 MOD_INC_USE_COUNT;
973 return 0; /* Always succeed */
977 /******************************************************
979 * send a packet to the adapter
981 ******************************************************/
983 static int send_packet(struct net_device *dev, struct sk_buff *skb)
985 elp_device *adapter = dev->priv;
986 unsigned long target;
987 unsigned long flags;
990 * make sure the length is even and no shorter than 60 bytes
992 unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
994 if (test_and_set_bit(0, (void *) &adapter->busy)) {
995 if (elp_debug >= 2)
996 printk("%s: transmit blocked\n", dev->name);
997 return FALSE;
1000 adapter->stats.tx_bytes += nlen;
1003 * send the adapter a transmit packet command. Ignore segment and offset
1004 * and make sure the length is even
1006 adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1007 adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1008 adapter->tx_pcb.data.xmit_pkt.buf_ofs
1009 = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
1010 adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1012 if (!send_pcb(dev, &adapter->tx_pcb)) {
1013 adapter->busy = 0;
1014 return FALSE;
1016 /* if this happens, we die */
1017 if (test_and_set_bit(0, (void *) &adapter->dmaing))
1018 printk("%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1020 adapter->current_dma.direction = 1;
1021 adapter->current_dma.start_time = jiffies;
1023 target = virt_to_bus(skb->data);
1024 if ((target + nlen) >= MAX_DMA_ADDRESS) {
1025 memcpy(adapter->dma_buffer, skb->data, nlen);
1026 target = virt_to_bus(adapter->dma_buffer);
1028 adapter->current_dma.skb = skb;
1030 flags=claim_dma_lock();
1031 disable_dma(dev->dma);
1032 clear_dma_ff(dev->dma);
1033 set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
1034 set_dma_addr(dev->dma, target);
1035 set_dma_count(dev->dma, nlen);
1036 outb_control(adapter->hcr_val | DMAE | TCEN, dev);
1037 enable_dma(dev->dma);
1038 release_dma_lock(flags);
1040 if (elp_debug >= 3)
1041 printk("%s: DMA transfer started\n", dev->name);
1043 return TRUE;
1047 * The upper layer thinks we timed out
1050 static void elp_timeout(struct net_device *dev)
1052 elp_device *adapter = dev->priv;
1053 int stat;
1055 stat = inb_status(dev->base_addr);
1056 printk(KERN_WARNING "%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1057 if (elp_debug >= 1)
1058 printk("%s: status %#02x\n", dev->name, stat);
1059 dev->trans_start = jiffies;
1060 adapter->stats.tx_dropped++;
1061 netif_wake_queue(dev);
1064 /******************************************************
1066 * start the transmitter
1067 * return 0 if sent OK, else return 1
1069 ******************************************************/
1071 static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1073 unsigned long flags;
1074 elp_device *adapter = dev->priv;
1076 spin_lock_irqsave(&adapter->lock, flags);
1077 check_3c505_dma(dev);
1079 if (elp_debug >= 3)
1080 printk("%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1082 netif_stop_queue(dev);
1085 * send the packet at skb->data for skb->len
1087 if (!send_packet(dev, skb)) {
1088 if (elp_debug >= 2) {
1089 printk("%s: failed to transmit packet\n", dev->name);
1091 spin_unlock_irqrestore(&adapter->lock, flags);
1092 return 1;
1094 if (elp_debug >= 3)
1095 printk("%s: packet of length %d sent\n", dev->name, (int) skb->len);
1098 * start the transmit timeout
1100 dev->trans_start = jiffies;
1102 prime_rx(dev);
1103 spin_unlock_irqrestore(&adapter->lock, flags);
1104 netif_start_queue(dev);
1105 return 0;
1108 /******************************************************
1110 * return statistics on the board
1112 ******************************************************/
1114 static struct net_device_stats *elp_get_stats(struct net_device *dev)
1116 elp_device *adapter = (elp_device *) dev->priv;
1118 if (elp_debug >= 3)
1119 printk("%s: request for stats\n", dev->name);
1121 /* If the device is closed, just return the latest stats we have,
1122 - we cannot ask from the adapter without interrupts */
1123 if (!netif_running(dev))
1124 return &adapter->stats;
1126 /* send a get statistics command to the board */
1127 adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1128 adapter->tx_pcb.length = 0;
1129 adapter->got[CMD_NETWORK_STATISTICS] = 0;
1130 if (!send_pcb(dev, &adapter->tx_pcb))
1131 printk("%s: couldn't send get statistics command\n", dev->name);
1132 else {
1133 int timeout = jiffies + TIMEOUT;
1134 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
1135 if (time_after_eq(jiffies, timeout)) {
1136 TIMEOUT_MSG(__LINE__);
1137 return &adapter->stats;
1141 /* statistics are now up to date */
1142 return &adapter->stats;
1145 /******************************************************
1147 * close the board
1149 ******************************************************/
1151 static int elp_close(struct net_device *dev)
1153 elp_device *adapter;
1155 adapter = dev->priv;
1157 if (elp_debug >= 3)
1158 printk("%s: request to close device\n", dev->name);
1160 netif_stop_queue(dev);
1162 /* Someone may request the device statistic information even when
1163 * the interface is closed. The following will update the statistics
1164 * structure in the driver, so we'll be able to give current statistics.
1166 (void) elp_get_stats(dev);
1169 * disable interrupts on the board
1171 outb_control(0, dev);
1174 * release the IRQ
1176 free_irq(dev->irq, dev);
1178 free_dma(dev->dma);
1179 free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
1181 MOD_DEC_USE_COUNT;
1183 return 0;
1187 /************************************************************
1189 * Set multicast list
1190 * num_addrs==0: clear mc_list
1191 * num_addrs==-1: set promiscuous mode
1192 * num_addrs>0: set mc_list
1194 ************************************************************/
1196 static void elp_set_mc_list(struct net_device *dev)
1198 elp_device *adapter = (elp_device *) dev->priv;
1199 struct dev_mc_list *dmi = dev->mc_list;
1200 int i;
1201 unsigned long flags;
1203 if (elp_debug >= 3)
1204 printk("%s: request to set multicast list\n", dev->name);
1206 spin_lock_irqsave(&adapter->lock, flags);
1208 if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1209 /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1210 /* if num_addrs==0 the list will be cleared */
1211 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1212 adapter->tx_pcb.length = 6 * dev->mc_count;
1213 for (i = 0; i < dev->mc_count; i++) {
1214 memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1215 dmi = dmi->next;
1217 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1218 if (!send_pcb(dev, &adapter->tx_pcb))
1219 printk("%s: couldn't send set_multicast command\n", dev->name);
1220 else {
1221 int timeout = jiffies + TIMEOUT;
1222 while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
1223 if (time_after_eq(jiffies, timeout)) {
1224 TIMEOUT_MSG(__LINE__);
1227 if (dev->mc_count)
1228 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1229 else /* num_addrs == 0 */
1230 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1231 } else
1232 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1234 * configure adapter to receive messages (as specified above)
1235 * and wait for response
1237 if (elp_debug >= 3)
1238 printk("%s: sending 82586 configure command\n", dev->name);
1239 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1240 adapter->tx_pcb.length = 2;
1241 adapter->got[CMD_CONFIGURE_82586] = 0;
1242 if (!send_pcb(dev, &adapter->tx_pcb))
1244 spin_unlock_irqrestore(&adapter->lock, flags);
1245 printk("%s: couldn't send 82586 configure command\n", dev->name);
1247 else {
1248 int timeout = jiffies + TIMEOUT;
1249 spin_unlock_irqrestore(&adapter->lock, flags);
1250 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
1251 if (time_after_eq(jiffies, timeout))
1252 TIMEOUT_MSG(__LINE__);
1256 /******************************************************
1258 * initialise Etherlink Plus board
1260 ******************************************************/
1262 static inline void elp_init(struct net_device *dev)
1264 elp_device *adapter = dev->priv;
1267 * set ptrs to various functions
1269 dev->open = elp_open; /* local */
1270 dev->stop = elp_close; /* local */
1271 dev->get_stats = elp_get_stats; /* local */
1272 dev->hard_start_xmit = elp_start_xmit; /* local */
1273 dev->tx_timeout = elp_timeout; /* local */
1274 dev->watchdog_timeo = 10*HZ;
1275 dev->set_multicast_list = elp_set_mc_list; /* local */
1277 /* Setup the generic properties */
1278 ether_setup(dev);
1281 * setup ptr to adapter specific information
1283 memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
1286 * memory information
1288 dev->mem_start = dev->mem_end = dev->rmem_end = dev->rmem_start = 0;
1291 /************************************************************
1293 * A couple of tests to see if there's 3C505 or not
1294 * Called only by elp_autodetect
1295 ************************************************************/
1297 static int __init elp_sense(struct net_device *dev)
1299 int timeout;
1300 int addr = dev->base_addr;
1301 const char *name = dev->name;
1302 long flags;
1303 byte orig_HSR;
1305 if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
1306 return -ENODEV;
1308 orig_HSR = inb_status(addr);
1310 if (elp_debug > 0)
1311 printk(search_msg, name, addr);
1313 if (orig_HSR == 0xff) {
1314 if (elp_debug > 0)
1315 printk(notfound_msg, 1);
1316 goto out;
1318 /* Enable interrupts - we need timers! */
1319 save_flags(flags);
1320 sti();
1322 /* Wait for a while; the adapter may still be booting up */
1323 if (elp_debug > 0)
1324 printk(stilllooking_msg);
1326 if (orig_HSR & DIR) {
1327 /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1328 outb(0, dev->base_addr + PORT_CONTROL);
1329 timeout = jiffies + 30*HZ/100;
1330 while (time_before(jiffies, timeout));
1331 restore_flags(flags);
1332 if (inb_status(addr) & DIR) {
1333 if (elp_debug > 0)
1334 printk(notfound_msg, 2);
1335 goto out;
1337 } else {
1338 /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1339 outb(DIR, dev->base_addr + PORT_CONTROL);
1340 timeout = jiffies + 30*HZ/100;
1341 while (time_before(jiffies, timeout));
1342 restore_flags(flags);
1343 if (!(inb_status(addr) & DIR)) {
1344 if (elp_debug > 0)
1345 printk(notfound_msg, 3);
1346 goto out;
1350 * It certainly looks like a 3c505.
1352 if (elp_debug > 0)
1353 printk(found_msg);
1355 return 0;
1356 out:
1357 release_region(addr, ELP_IO_EXTENT);
1358 return -ENODEV;
1361 /*************************************************************
1363 * Search through addr_list[] and try to find a 3C505
1364 * Called only by eplus_probe
1365 *************************************************************/
1367 static int __init elp_autodetect(struct net_device *dev)
1369 int idx = 0;
1371 /* if base address set, then only check that address
1372 otherwise, run through the table */
1373 if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
1374 if (elp_sense(dev) == 0)
1375 return dev->base_addr;
1376 } else
1377 while ((dev->base_addr = addr_list[idx++])) {
1378 if (elp_sense(dev) == 0)
1379 return dev->base_addr;
1382 /* could not find an adapter */
1383 if (elp_debug > 0)
1384 printk(couldnot_msg, dev->name);
1386 return 0; /* Because of this, the layer above will return -ENODEV */
1390 /******************************************************
1392 * probe for an Etherlink Plus board at the specified address
1394 ******************************************************/
1396 /* There are three situations we need to be able to detect here:
1398 * a) the card is idle
1399 * b) the card is still booting up
1400 * c) the card is stuck in a strange state (some DOS drivers do this)
1402 * In case (a), all is well. In case (b), we wait 10 seconds to see if the
1403 * card finishes booting, and carry on if so. In case (c), we do a hard reset,
1404 * loop round, and hope for the best.
1406 * This is all very unpleasant, but hopefully avoids the problems with the old
1407 * probe code (which had a 15-second delay if the card was idle, and didn't
1408 * work at all if it was in a weird state).
1411 int __init elplus_probe(struct net_device *dev)
1413 elp_device *adapter;
1414 int i, tries, tries1, timeout, okay;
1417 * setup adapter structure
1420 dev->base_addr = elp_autodetect(dev);
1421 if (!(dev->base_addr))
1422 return -ENODEV;
1425 * setup ptr to adapter specific information
1427 adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1428 if (adapter == NULL) {
1429 printk("%s: out of memory\n", dev->name);
1430 return -ENODEV;
1433 adapter->send_pcb_semaphore = 0;
1435 for (tries1 = 0; tries1 < 3; tries1++) {
1436 outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1437 /* First try to write just one byte, to see if the card is
1438 * responding at all normally.
1440 timeout = jiffies + 5*HZ/100;
1441 okay = 0;
1442 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1443 if ((inb_status(dev->base_addr) & HCRE)) {
1444 outb_command(0, dev->base_addr); /* send a spurious byte */
1445 timeout = jiffies + 5*HZ/100;
1446 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1447 if (inb_status(dev->base_addr) & HCRE)
1448 okay = 1;
1450 if (!okay) {
1451 /* Nope, it's ignoring the command register. This means that
1452 * either it's still booting up, or it's died.
1454 printk("%s: command register wouldn't drain, ", dev->name);
1455 if ((inb_status(dev->base_addr) & 7) == 3) {
1456 /* If the adapter status is 3, it *could* still be booting.
1457 * Give it the benefit of the doubt for 10 seconds.
1459 printk("assuming 3c505 still starting\n");
1460 timeout = jiffies + 10*HZ;
1461 while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1462 if (inb_status(dev->base_addr) & 7) {
1463 printk("%s: 3c505 failed to start\n", dev->name);
1464 } else {
1465 okay = 1; /* It started */
1467 } else {
1468 /* Otherwise, it must just be in a strange
1469 * state. We probably need to kick it.
1471 printk("3c505 is sulking\n");
1474 for (tries = 0; tries < 5 && okay; tries++) {
1477 * Try to set the Ethernet address, to make sure that the board
1478 * is working.
1480 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1481 adapter->tx_pcb.length = 0;
1482 autoirq_setup(0);
1483 if (!send_pcb(dev, &adapter->tx_pcb)) {
1484 printk("%s: could not send first PCB\n", dev->name);
1485 autoirq_report(0);
1486 continue;
1488 if (!receive_pcb(dev, &adapter->rx_pcb)) {
1489 printk("%s: could not read first PCB\n", dev->name);
1490 autoirq_report(0);
1491 continue;
1493 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1494 (adapter->rx_pcb.length != 6)) {
1495 printk("%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1496 autoirq_report(0);
1497 continue;
1499 goto okay;
1501 /* It's broken. Do a hard reset to re-initialise the board,
1502 * and try again.
1504 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1505 outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1506 outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1508 printk("%s: failed to initialise 3c505\n", dev->name);
1509 release_region(dev->base_addr, ELP_IO_EXTENT);
1510 return -ENODEV;
1512 okay:
1513 if (dev->irq) { /* Is there a preset IRQ? */
1514 int rpt = autoirq_report(0);
1515 if (dev->irq != rpt) {
1516 printk("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1518 /* if dev->irq == autoirq_report(0), all is well */
1519 } else /* No preset IRQ; just use what we can detect */
1520 dev->irq = autoirq_report(0);
1521 switch (dev->irq) { /* Legal, sane? */
1522 case 0:
1523 printk("%s: IRQ probe failed: check 3c505 jumpers.\n",
1524 dev->name);
1525 return -ENODEV;
1526 case 1:
1527 case 6:
1528 case 8:
1529 case 13:
1530 printk("%s: Impossible IRQ %d reported by autoirq_report().\n",
1531 dev->name, dev->irq);
1532 return -ENODEV;
1535 * Now we have the IRQ number so we can disable the interrupts from
1536 * the board until the board is opened.
1538 outb_control(adapter->hcr_val & ~CMDE, dev);
1541 * copy Ethernet address into structure
1543 for (i = 0; i < 6; i++)
1544 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1546 /* find a DMA channel */
1547 if (!dev->dma) {
1548 if (dev->mem_start) {
1549 dev->dma = dev->mem_start & 7;
1551 else {
1552 printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
1553 dev->dma = ELP_DMA;
1558 * print remainder of startup message
1560 printk("%s: 3c505 at %#lx, irq %d, dma %d, ",
1561 dev->name, dev->base_addr, dev->irq, dev->dma);
1562 printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1563 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1564 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1567 * read more information from the adapter
1570 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1571 adapter->tx_pcb.length = 0;
1572 if (!send_pcb(dev, &adapter->tx_pcb) ||
1573 !receive_pcb(dev, &adapter->rx_pcb) ||
1574 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1575 (adapter->rx_pcb.length != 10)) {
1576 printk("not responding to second PCB\n");
1578 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);
1581 * reconfigure the adapter memory to better suit our purposes
1583 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1584 adapter->tx_pcb.length = 12;
1585 adapter->tx_pcb.data.memconf.cmd_q = 8;
1586 adapter->tx_pcb.data.memconf.rcv_q = 8;
1587 adapter->tx_pcb.data.memconf.mcast = 10;
1588 adapter->tx_pcb.data.memconf.frame = 10;
1589 adapter->tx_pcb.data.memconf.rcv_b = 10;
1590 adapter->tx_pcb.data.memconf.progs = 0;
1591 if (!send_pcb(dev, &adapter->tx_pcb) ||
1592 !receive_pcb(dev, &adapter->rx_pcb) ||
1593 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1594 (adapter->rx_pcb.length != 2)) {
1595 printk("%s: could not configure adapter memory\n", dev->name);
1597 if (adapter->rx_pcb.data.configure) {
1598 printk("%s: adapter configuration failed\n", dev->name);
1602 * initialise the device
1604 elp_init(dev);
1606 return 0;
1609 #ifdef MODULE
1610 static struct net_device dev_3c505[ELP_MAX_CARDS] =
1612 { "", /* device name is inserted by net_init.c */
1613 0, 0, 0, 0,
1614 0, 0,
1615 0, 0, 0, NULL, elplus_probe},
1618 static int io[ELP_MAX_CARDS] = { 0, };
1619 static int irq[ELP_MAX_CARDS] = { 0, };
1620 static int dma[ELP_MAX_CARDS] = { 0, };
1621 MODULE_PARM(io, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1622 MODULE_PARM(irq, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1623 MODULE_PARM(dma, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1625 int init_module(void)
1627 int this_dev, found = 0;
1629 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1630 struct net_device *dev = &dev_3c505[this_dev];
1631 dev->irq = irq[this_dev];
1632 dev->base_addr = io[this_dev];
1633 if (dma[this_dev]) {
1634 dev->dma = dma[this_dev];
1635 } else {
1636 dev->dma = ELP_DMA;
1637 printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
1639 if (io[this_dev] == 0) {
1640 if (this_dev) break;
1641 printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
1643 if (register_netdev(dev) != 0) {
1644 printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
1645 if (found != 0) return 0;
1646 return -ENXIO;
1648 found++;
1650 return 0;
1653 void cleanup_module(void)
1655 int this_dev;
1657 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1658 struct net_device *dev = &dev_3c505[this_dev];
1659 if (dev->priv != NULL) {
1660 unregister_netdev(dev);
1661 kfree(dev->priv);
1662 dev->priv = NULL;
1663 release_region(dev->base_addr, ELP_IO_EXTENT);
1668 #endif /* MODULE */