Import 2.3.50pre1
[davej-history.git] / drivers / net / 3c505.c
blobb333ad08c6c0e3b173dd5016d9c6019a66d7c2e7
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 const char *search_msg = "%s: Looking for 3c505 adapter at address %#x...";
135 static const char *stilllooking_msg = "still looking...";
137 static const char *found_msg = "found.\n";
139 static const char *notfound_msg = "not found (reason = %d)\n";
141 static const char *couldnot_msg = "%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 const 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 (check_region(addr, 0xf))
1306 return -1;
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 return -1;
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 return -1;
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 return -1;
1350 * It certainly looks like a 3c505.
1352 if (elp_debug > 0)
1353 printk(found_msg);
1355 return 0;
1358 /*************************************************************
1360 * Search through addr_list[] and try to find a 3C505
1361 * Called only by eplus_probe
1362 *************************************************************/
1364 static int __init elp_autodetect(struct net_device *dev)
1366 int idx = 0;
1368 /* if base address set, then only check that address
1369 otherwise, run through the table */
1370 if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
1371 if (elp_sense(dev) == 0)
1372 return dev->base_addr;
1373 } else
1374 while ((dev->base_addr = addr_list[idx++])) {
1375 if (elp_sense(dev) == 0)
1376 return dev->base_addr;
1379 /* could not find an adapter */
1380 if (elp_debug > 0)
1381 printk(couldnot_msg, dev->name);
1383 return 0; /* Because of this, the layer above will return -ENODEV */
1387 /******************************************************
1389 * probe for an Etherlink Plus board at the specified address
1391 ******************************************************/
1393 /* There are three situations we need to be able to detect here:
1395 * a) the card is idle
1396 * b) the card is still booting up
1397 * c) the card is stuck in a strange state (some DOS drivers do this)
1399 * In case (a), all is well. In case (b), we wait 10 seconds to see if the
1400 * card finishes booting, and carry on if so. In case (c), we do a hard reset,
1401 * loop round, and hope for the best.
1403 * This is all very unpleasant, but hopefully avoids the problems with the old
1404 * probe code (which had a 15-second delay if the card was idle, and didn't
1405 * work at all if it was in a weird state).
1408 int __init elplus_probe(struct net_device *dev)
1410 elp_device *adapter;
1411 int i, tries, tries1, timeout, okay;
1414 * setup adapter structure
1417 dev->base_addr = elp_autodetect(dev);
1418 if (!(dev->base_addr))
1419 return -ENODEV;
1422 * setup ptr to adapter specific information
1424 adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1425 if (adapter == NULL) {
1426 printk("%s: out of memory\n", dev->name);
1427 return -ENODEV;
1430 adapter->send_pcb_semaphore = 0;
1432 for (tries1 = 0; tries1 < 3; tries1++) {
1433 outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1434 /* First try to write just one byte, to see if the card is
1435 * responding at all normally.
1437 timeout = jiffies + 5*HZ/100;
1438 okay = 0;
1439 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1440 if ((inb_status(dev->base_addr) & HCRE)) {
1441 outb_command(0, dev->base_addr); /* send a spurious byte */
1442 timeout = jiffies + 5*HZ/100;
1443 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1444 if (inb_status(dev->base_addr) & HCRE)
1445 okay = 1;
1447 if (!okay) {
1448 /* Nope, it's ignoring the command register. This means that
1449 * either it's still booting up, or it's died.
1451 printk("%s: command register wouldn't drain, ", dev->name);
1452 if ((inb_status(dev->base_addr) & 7) == 3) {
1453 /* If the adapter status is 3, it *could* still be booting.
1454 * Give it the benefit of the doubt for 10 seconds.
1456 printk("assuming 3c505 still starting\n");
1457 timeout = jiffies + 10*HZ;
1458 while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1459 if (inb_status(dev->base_addr) & 7) {
1460 printk("%s: 3c505 failed to start\n", dev->name);
1461 } else {
1462 okay = 1; /* It started */
1464 } else {
1465 /* Otherwise, it must just be in a strange
1466 * state. We probably need to kick it.
1468 printk("3c505 is sulking\n");
1471 for (tries = 0; tries < 5 && okay; tries++) {
1474 * Try to set the Ethernet address, to make sure that the board
1475 * is working.
1477 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1478 adapter->tx_pcb.length = 0;
1479 autoirq_setup(0);
1480 if (!send_pcb(dev, &adapter->tx_pcb)) {
1481 printk("%s: could not send first PCB\n", dev->name);
1482 autoirq_report(0);
1483 continue;
1485 if (!receive_pcb(dev, &adapter->rx_pcb)) {
1486 printk("%s: could not read first PCB\n", dev->name);
1487 autoirq_report(0);
1488 continue;
1490 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1491 (adapter->rx_pcb.length != 6)) {
1492 printk("%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1493 autoirq_report(0);
1494 continue;
1496 goto okay;
1498 /* It's broken. Do a hard reset to re-initialise the board,
1499 * and try again.
1501 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1502 outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1503 outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1505 printk("%s: failed to initialise 3c505\n", dev->name);
1506 return -ENODEV;
1508 okay:
1509 if (dev->irq) { /* Is there a preset IRQ? */
1510 int rpt = autoirq_report(0);
1511 if (dev->irq != rpt) {
1512 printk("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1514 /* if dev->irq == autoirq_report(0), all is well */
1515 } else /* No preset IRQ; just use what we can detect */
1516 dev->irq = autoirq_report(0);
1517 switch (dev->irq) { /* Legal, sane? */
1518 case 0:
1519 printk("%s: IRQ probe failed: check 3c505 jumpers.\n",
1520 dev->name);
1521 return -ENODEV;
1522 case 1:
1523 case 6:
1524 case 8:
1525 case 13:
1526 printk("%s: Impossible IRQ %d reported by autoirq_report().\n",
1527 dev->name, dev->irq);
1528 return -ENODEV;
1531 * Now we have the IRQ number so we can disable the interrupts from
1532 * the board until the board is opened.
1534 outb_control(adapter->hcr_val & ~CMDE, dev);
1537 * copy Ethernet address into structure
1539 for (i = 0; i < 6; i++)
1540 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1542 /* find a DMA channel */
1543 if (!dev->dma) {
1544 if (dev->mem_start) {
1545 dev->dma = dev->mem_start & 7;
1547 else {
1548 printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
1549 dev->dma = ELP_DMA;
1554 * print remainder of startup message
1556 printk("%s: 3c505 at %#lx, irq %d, dma %d, ",
1557 dev->name, dev->base_addr, dev->irq, dev->dma);
1558 printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1559 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1560 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1563 * read more information from the adapter
1566 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1567 adapter->tx_pcb.length = 0;
1568 if (!send_pcb(dev, &adapter->tx_pcb) ||
1569 !receive_pcb(dev, &adapter->rx_pcb) ||
1570 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1571 (adapter->rx_pcb.length != 10)) {
1572 printk("not responding to second PCB\n");
1574 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);
1577 * reconfigure the adapter memory to better suit our purposes
1579 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1580 adapter->tx_pcb.length = 12;
1581 adapter->tx_pcb.data.memconf.cmd_q = 8;
1582 adapter->tx_pcb.data.memconf.rcv_q = 8;
1583 adapter->tx_pcb.data.memconf.mcast = 10;
1584 adapter->tx_pcb.data.memconf.frame = 10;
1585 adapter->tx_pcb.data.memconf.rcv_b = 10;
1586 adapter->tx_pcb.data.memconf.progs = 0;
1587 if (!send_pcb(dev, &adapter->tx_pcb) ||
1588 !receive_pcb(dev, &adapter->rx_pcb) ||
1589 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1590 (adapter->rx_pcb.length != 2)) {
1591 printk("%s: could not configure adapter memory\n", dev->name);
1593 if (adapter->rx_pcb.data.configure) {
1594 printk("%s: adapter configuration failed\n", dev->name);
1597 * and reserve the address region
1599 request_region(dev->base_addr, ELP_IO_EXTENT, "3c505");
1602 * initialise the device
1604 elp_init(dev);
1606 return 0;
1609 #ifdef MODULE
1610 #define NAMELEN 9
1611 static char devicename[ELP_MAX_CARDS][NAMELEN] = {{0,}};
1612 static struct net_device dev_3c505[ELP_MAX_CARDS] =
1614 { NULL, /* device name is inserted by net_init.c */
1615 0, 0, 0, 0,
1616 0, 0,
1617 0, 0, 0, NULL, elplus_probe},
1620 static int io[ELP_MAX_CARDS] = { 0, };
1621 static int irq[ELP_MAX_CARDS] = { 0, };
1622 static int dma[ELP_MAX_CARDS] = { 0, };
1623 MODULE_PARM(io, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1624 MODULE_PARM(irq, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1625 MODULE_PARM(dma, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1627 int init_module(void)
1629 int this_dev, found = 0;
1631 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1632 struct net_device *dev = &dev_3c505[this_dev];
1633 dev->name = devicename[this_dev];
1634 dev->irq = irq[this_dev];
1635 dev->base_addr = io[this_dev];
1636 if (dma[this_dev]) {
1637 dev->dma = dma[this_dev];
1638 } else {
1639 dev->dma = ELP_DMA;
1640 printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
1642 if (io[this_dev] == 0) {
1643 if (this_dev) break;
1644 printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
1646 if (register_netdev(dev) != 0) {
1647 printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
1648 if (found != 0) return 0;
1649 return -ENXIO;
1651 found++;
1653 return 0;
1656 void cleanup_module(void)
1658 int this_dev;
1660 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1661 struct net_device *dev = &dev_3c505[this_dev];
1662 if (dev->priv != NULL) {
1663 unregister_netdev(dev);
1664 kfree(dev->priv);
1665 dev->priv = NULL;
1666 release_region(dev->base_addr, ELP_IO_EXTENT);
1671 #endif /* MODULE */