* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / 3c505.c
blob1bd65d54fbb9fbddfb32375697f397130a073cbc
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 <asm/bitops.h>
105 #include <asm/io.h>
106 #include <asm/dma.h>
108 #include <linux/netdevice.h>
109 #include <linux/etherdevice.h>
110 #include <linux/skbuff.h>
111 #include <linux/init.h>
113 #include "3c505.h"
115 /*********************************************************
117 * define debug messages here as common strings to reduce space
119 *********************************************************/
121 static const char *filename = __FILE__;
123 static const char *timeout_msg = "*** timeout at %s:%s (line %d) ***\n";
124 #define TIMEOUT_MSG(lineno) \
125 printk(timeout_msg, filename,__FUNCTION__,(lineno))
127 static const char *invalid_pcb_msg =
128 "*** invalid pcb length %d at %s:%s (line %d) ***\n";
129 #define INVALID_PCB_MSG(len) \
130 printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
132 static const char *search_msg = "%s: Looking for 3c505 adapter at address %#x...";
134 static const char *stilllooking_msg = "still looking...";
136 static const char *found_msg = "found.\n";
138 static const char *notfound_msg = "not found (reason = %d)\n";
140 static const char *couldnot_msg = "%s: 3c505 not found\n";
142 /*********************************************************
144 * various other debug stuff
146 *********************************************************/
148 #ifdef ELP_DEBUG
149 static const int elp_debug = ELP_DEBUG;
150 #else
151 static const int elp_debug = 0;
152 #endif
155 * 0 = no messages (well, some)
156 * 1 = messages when high level commands performed
157 * 2 = messages when low level commands performed
158 * 3 = messages when interrupts received
161 /*****************************************************************
163 * useful macros
165 *****************************************************************/
167 #ifndef TRUE
168 #define TRUE 1
169 #endif
171 #ifndef FALSE
172 #define FALSE 0
173 #endif
176 /*****************************************************************
178 * List of I/O-addresses we try to auto-sense
179 * Last element MUST BE 0!
180 *****************************************************************/
182 static const int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
184 /* Dma Memory related stuff */
186 /* Pure 2^n version of get_order */
187 static inline int __get_order(unsigned long size)
189 int order;
191 size = (size - 1) >> (PAGE_SHIFT - 1);
192 order = -1;
193 do {
194 size >>= 1;
195 order++;
196 } while (size);
197 return order;
200 static unsigned long dma_mem_alloc(int size)
202 int order = __get_order(size);
204 return __get_dma_pages(GFP_KERNEL, order);
208 /*****************************************************************
210 * Functions for I/O (note the inline !)
212 *****************************************************************/
214 static inline unsigned char inb_status(unsigned int base_addr)
216 return inb(base_addr + PORT_STATUS);
219 static inline int inb_command(unsigned int base_addr)
221 return inb(base_addr + PORT_COMMAND);
224 static inline void outb_control(unsigned char val, struct net_device *dev)
226 outb(val, dev->base_addr + PORT_CONTROL);
227 ((elp_device *)(dev->priv))->hcr_val = val;
230 #define HCR_VAL(x) (((elp_device *)((x)->priv))->hcr_val)
232 static inline void outb_command(unsigned char val, unsigned int base_addr)
234 outb(val, base_addr + PORT_COMMAND);
237 static inline unsigned int inw_data(unsigned int base_addr)
239 return inw(base_addr + PORT_DATA);
242 static inline void outw_data(unsigned int val, unsigned int base_addr)
244 outw(val, base_addr + PORT_DATA);
247 static inline unsigned int backlog_next(unsigned int n)
249 return (n + 1) % BACKLOG_SIZE;
252 /*****************************************************************
254 * useful functions for accessing the adapter
256 *****************************************************************/
259 * use this routine when accessing the ASF bits as they are
260 * changed asynchronously by the adapter
263 /* get adapter PCB status */
264 #define GET_ASF(addr) \
265 (get_status(addr)&ASF_PCB_MASK)
267 static inline int get_status(unsigned int base_addr)
269 int timeout = jiffies + 10*HZ/100;
270 register int stat1;
271 do {
272 stat1 = inb_status(base_addr);
273 } while (stat1 != inb_status(base_addr) && time_before(jiffies, timeout));
274 if (time_after_eq(jiffies, timeout))
275 TIMEOUT_MSG(__LINE__);
276 return stat1;
279 static inline void set_hsf(struct net_device *dev, int hsf)
281 cli();
282 outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
283 sti();
286 static int start_receive(struct net_device *, pcb_struct *);
288 inline static void adapter_reset(struct net_device *dev)
290 int timeout;
291 elp_device *adapter = dev->priv;
292 unsigned char orig_hcr = adapter->hcr_val;
294 outb_control(0, dev);
296 if (inb_status(dev->base_addr) & ACRF) {
297 do {
298 inb_command(dev->base_addr);
299 timeout = jiffies + 2*HZ/100;
300 while (time_before_eq(jiffies, timeout) && !(inb_status(dev->base_addr) & ACRF));
301 } while (inb_status(dev->base_addr) & ACRF);
302 set_hsf(dev, HSF_PCB_NAK);
304 outb_control(adapter->hcr_val | ATTN | DIR, dev);
305 timeout = jiffies + 1*HZ/100;
306 while (time_before_eq(jiffies, timeout));
307 outb_control(adapter->hcr_val & ~ATTN, dev);
308 timeout = jiffies + 1*HZ/100;
309 while (time_before_eq(jiffies, timeout));
310 outb_control(adapter->hcr_val | FLSH, dev);
311 timeout = jiffies + 1*HZ/100;
312 while (time_before_eq(jiffies, timeout));
313 outb_control(adapter->hcr_val & ~FLSH, dev);
314 timeout = jiffies + 1*HZ/100;
315 while (time_before_eq(jiffies, timeout));
317 outb_control(orig_hcr, dev);
318 if (!start_receive(dev, &adapter->tx_pcb))
319 printk("%s: start receive command failed \n", dev->name);
322 /* Check to make sure that a DMA transfer hasn't timed out. This should
323 * never happen in theory, but seems to occur occasionally if the card gets
324 * prodded at the wrong time.
326 static inline void check_3c505_dma(struct net_device *dev)
328 elp_device *adapter = dev->priv;
329 if (adapter->dmaing && time_after(jiffies, adapter->current_dma.start_time + 10)) {
330 unsigned long flags, f;
331 printk("%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
332 save_flags(flags);
333 cli();
334 adapter->dmaing = 0;
335 adapter->busy = 0;
337 f=claim_dma_lock();
338 disable_dma(dev->dma);
339 release_dma_lock(f);
341 if (adapter->rx_active)
342 adapter->rx_active--;
343 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
344 restore_flags(flags);
348 /* Primitive functions used by send_pcb() */
349 static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
351 unsigned int timeout;
352 outb_command(byte, base_addr);
353 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
354 if (inb_status(base_addr) & HCRE)
355 return FALSE;
357 printk("3c505: send_pcb_slow timed out\n");
358 return TRUE;
361 static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
363 unsigned int timeout;
364 outb_command(byte, base_addr);
365 for (timeout = 0; timeout < 40000; timeout++) {
366 if (inb_status(base_addr) & HCRE)
367 return FALSE;
369 printk("3c505: send_pcb_fast timed out\n");
370 return TRUE;
373 /* Check to see if the receiver needs restarting, and kick it if so */
374 static inline void prime_rx(struct net_device *dev)
376 elp_device *adapter = dev->priv;
377 while (adapter->rx_active < ELP_RX_PCBS && dev->start) {
378 if (!start_receive(dev, &adapter->itx_pcb))
379 break;
383 /*****************************************************************
385 * send_pcb
386 * Send a PCB to the adapter.
388 * output byte to command reg --<--+
389 * wait until HCRE is non zero |
390 * loop until all bytes sent -->--+
391 * set HSF1 and HSF2 to 1
392 * output pcb length
393 * wait until ASF give ACK or NAK
394 * set HSF1 and HSF2 to 0
396 *****************************************************************/
398 /* This can be quite slow -- the adapter is allowed to take up to 40ms
399 * to respond to the initial interrupt.
401 * We run initially with interrupts turned on, but with a semaphore set
402 * so that nobody tries to re-enter this code. Once the first byte has
403 * gone through, we turn interrupts off and then send the others (the
404 * timeout is reduced to 500us).
407 static int send_pcb(struct net_device *dev, pcb_struct * pcb)
409 int i;
410 int timeout;
411 elp_device *adapter = dev->priv;
413 check_3c505_dma(dev);
415 if (adapter->dmaing && adapter->current_dma.direction == 0)
416 return FALSE;
418 /* Avoid contention */
419 if (test_and_set_bit(1, &adapter->send_pcb_semaphore)) {
420 if (elp_debug >= 3) {
421 printk("%s: send_pcb entered while threaded\n", dev->name);
423 return FALSE;
426 * load each byte into the command register and
427 * wait for the HCRE bit to indicate the adapter
428 * had read the byte
430 set_hsf(dev, 0);
432 if (send_pcb_slow(dev->base_addr, pcb->command))
433 goto abort;
435 cli();
437 if (send_pcb_fast(dev->base_addr, pcb->length))
438 goto sti_abort;
440 for (i = 0; i < pcb->length; i++) {
441 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
442 goto sti_abort;
445 outb_control(adapter->hcr_val | 3, dev); /* signal end of PCB */
446 outb_command(2 + pcb->length, dev->base_addr);
448 /* now wait for the acknowledgement */
449 sti();
451 for (timeout = jiffies + 5*HZ/100; time_before(jiffies, timeout);) {
452 switch (GET_ASF(dev->base_addr)) {
453 case ASF_PCB_ACK:
454 adapter->send_pcb_semaphore = 0;
455 return TRUE;
456 break;
457 case ASF_PCB_NAK:
458 #ifdef ELP_DEBUG
459 printk(KERN_DEBUG "%s: send_pcb got NAK\n", dev->name);
460 #endif
461 goto abort;
462 break;
466 if (elp_debug >= 1)
467 printk("%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
469 sti_abort:
470 sti();
471 abort:
472 adapter->send_pcb_semaphore = 0;
473 return FALSE;
477 /*****************************************************************
479 * receive_pcb
480 * Read a PCB from the adapter
482 * wait for ACRF to be non-zero ---<---+
483 * input a byte |
484 * if ASF1 and ASF2 were not both one |
485 * before byte was read, loop --->---+
486 * set HSF1 and HSF2 for ack
488 *****************************************************************/
490 static int receive_pcb(struct net_device *dev, pcb_struct * pcb)
492 int i, j;
493 int total_length;
494 int stat;
495 int timeout;
497 elp_device *adapter = dev->priv;
499 set_hsf(dev, 0);
501 /* get the command code */
502 timeout = jiffies + 2*HZ/100;
503 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
504 if (time_after_eq(jiffies, timeout)) {
505 TIMEOUT_MSG(__LINE__);
506 return FALSE;
508 pcb->command = inb_command(dev->base_addr);
510 /* read the data length */
511 timeout = jiffies + 3*HZ/100;
512 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && time_before(jiffies, timeout));
513 if (time_after_eq(jiffies, timeout)) {
514 TIMEOUT_MSG(__LINE__);
515 printk("%s: status %02x\n", dev->name, stat);
516 return FALSE;
518 pcb->length = inb_command(dev->base_addr);
520 if (pcb->length > MAX_PCB_DATA) {
521 INVALID_PCB_MSG(pcb->length);
522 adapter_reset(dev);
523 return FALSE;
525 /* read the data */
526 cli();
527 i = 0;
528 do {
529 j = 0;
530 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
531 pcb->data.raw[i++] = inb_command(dev->base_addr);
532 if (i > MAX_PCB_DATA)
533 INVALID_PCB_MSG(i);
534 } while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
535 sti();
536 if (j >= 20000) {
537 TIMEOUT_MSG(__LINE__);
538 return FALSE;
540 /* woops, the last "data" byte was really the length! */
541 total_length = pcb->data.raw[--i];
543 /* safety check total length vs data length */
544 if (total_length != (pcb->length + 2)) {
545 if (elp_debug >= 2)
546 printk("%s: mangled PCB received\n", dev->name);
547 set_hsf(dev, HSF_PCB_NAK);
548 return FALSE;
551 if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
552 if (test_and_set_bit(0, (void *) &adapter->busy)) {
553 if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
554 set_hsf(dev, HSF_PCB_NAK);
555 printk("%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
556 pcb->command = 0;
557 return TRUE;
558 } else {
559 pcb->command = 0xff;
563 set_hsf(dev, HSF_PCB_ACK);
564 return TRUE;
567 /******************************************************
569 * queue a receive command on the adapter so we will get an
570 * interrupt when a packet is received.
572 ******************************************************/
574 static int start_receive(struct net_device *dev, pcb_struct * tx_pcb)
576 int status;
577 elp_device *adapter = dev->priv;
579 if (elp_debug >= 3)
580 printk("%s: restarting receiver\n", dev->name);
581 tx_pcb->command = CMD_RECEIVE_PACKET;
582 tx_pcb->length = sizeof(struct Rcv_pkt);
583 tx_pcb->data.rcv_pkt.buf_seg
584 = tx_pcb->data.rcv_pkt.buf_ofs = 0; /* Unused */
585 tx_pcb->data.rcv_pkt.buf_len = 1600;
586 tx_pcb->data.rcv_pkt.timeout = 0; /* set timeout to zero */
587 status = send_pcb(dev, tx_pcb);
588 if (status)
589 adapter->rx_active++;
590 return status;
593 /******************************************************
595 * extract a packet from the adapter
596 * this routine is only called from within the interrupt
597 * service routine, so no cli/sti calls are needed
598 * note that the length is always assumed to be even
600 ******************************************************/
602 static void receive_packet(struct net_device *dev, int len)
604 int rlen;
605 elp_device *adapter = dev->priv;
606 void *target;
607 struct sk_buff *skb;
608 unsigned long flags;
610 rlen = (len + 1) & ~1;
611 skb = dev_alloc_skb(rlen + 2);
613 if (!skb) {
614 printk("%s: memory squeeze, dropping packet\n", dev->name);
615 target = adapter->dma_buffer;
616 adapter->current_dma.target = NULL;
617 } else {
618 skb_reserve(skb, 2);
619 target = skb_put(skb, rlen);
620 if (virt_to_bus(target + rlen) >= MAX_DMA_ADDRESS) {
621 adapter->current_dma.target = target;
622 target = adapter->dma_buffer;
623 } else {
624 adapter->current_dma.target = NULL;
628 /* if this happens, we die */
629 if (test_and_set_bit(0, (void *) &adapter->dmaing))
630 printk("%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
632 skb->dev = dev;
633 adapter->current_dma.direction = 0;
634 adapter->current_dma.length = rlen;
635 adapter->current_dma.skb = skb;
636 adapter->current_dma.start_time = jiffies;
638 outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
640 flags=claim_dma_lock();
641 disable_dma(dev->dma);
642 clear_dma_ff(dev->dma);
643 set_dma_mode(dev->dma, 0x04); /* dma read */
644 set_dma_addr(dev->dma, virt_to_bus(target));
645 set_dma_count(dev->dma, rlen);
646 enable_dma(dev->dma);
647 release_dma_lock(flags);
649 if (elp_debug >= 3) {
650 printk("%s: rx DMA transfer started\n", dev->name);
653 if (adapter->rx_active)
654 adapter->rx_active--;
656 if (!adapter->busy)
657 printk("%s: receive_packet called, busy not set.\n", dev->name);
660 /******************************************************
662 * interrupt handler
664 ******************************************************/
666 static void elp_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
668 int len;
669 int dlen;
670 int icount = 0;
671 struct net_device *dev;
672 elp_device *adapter;
673 int timeout;
675 if (irq < 0 || irq > 15) {
676 printk("elp_interrupt(): illegal IRQ number found in interrupt routine (%i)\n", irq);
677 return;
679 dev = dev_id;
681 if (dev == NULL) {
682 printk("elp_interrupt(): irq %d for unknown device.\n", irq);
683 return;
685 adapter = (elp_device *) dev->priv;
687 if (dev->interrupt) {
688 printk("%s: re-entering the interrupt handler!\n", dev->name);
689 return;
691 dev->interrupt = 1;
693 do {
695 * has a DMA transfer finished?
697 if (inb_status(dev->base_addr) & DONE) {
698 if (!adapter->dmaing) {
699 printk("%s: phantom DMA completed\n", dev->name);
701 if (elp_debug >= 3) {
702 printk("%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
705 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR),
706 dev);
707 if (adapter->current_dma.direction) {
708 dev_kfree_skb(adapter->current_dma.skb);
709 } else {
710 struct sk_buff *skb = adapter->current_dma.skb;
711 if (skb) {
712 if (adapter->current_dma.target) {
713 /* have already done the skb_put() */
714 memcpy(adapter->current_dma.target, adapter->dma_buffer, adapter->current_dma.length);
716 skb->protocol = eth_type_trans(skb,dev);
717 adapter->stats.rx_bytes += skb->len;
718 netif_rx(skb);
721 adapter->dmaing = 0;
722 if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
723 int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
724 adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
725 if (elp_debug >= 2)
726 printk("%s: receiving backlogged packet (%d)\n", dev->name, t);
727 receive_packet(dev, t);
728 } else {
729 adapter->busy = 0;
731 } else {
732 /* has one timed out? */
733 check_3c505_dma(dev);
736 sti();
739 * receive a PCB from the adapter
741 timeout = jiffies + 3*HZ/100;
742 while ((inb_status(dev->base_addr) & ACRF) != 0 && time_before(jiffies, timeout)) {
743 if (receive_pcb(dev, &adapter->irx_pcb)) {
744 switch (adapter->irx_pcb.command) {
745 case 0:
746 break;
748 * received a packet - this must be handled fast
750 case 0xff:
751 case CMD_RECEIVE_PACKET_COMPLETE:
752 /* if the device isn't open, don't pass packets up the stack */
753 if (dev->start == 0)
754 break;
755 cli();
756 len = adapter->irx_pcb.data.rcv_resp.pkt_len;
757 dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
758 if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
759 printk("%s: interrupt - packet not received correctly\n", dev->name);
760 sti();
761 } else {
762 if (elp_debug >= 3) {
763 sti();
764 printk("%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
765 cli();
767 if (adapter->irx_pcb.command == 0xff) {
768 if (elp_debug >= 2)
769 printk("%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
770 adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
771 adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
772 } else {
773 receive_packet(dev, dlen);
775 sti();
776 if (elp_debug >= 3)
777 printk("%s: packet received\n", dev->name);
779 break;
782 * 82586 configured correctly
784 case CMD_CONFIGURE_82586_RESPONSE:
785 adapter->got[CMD_CONFIGURE_82586] = 1;
786 if (elp_debug >= 3)
787 printk("%s: interrupt - configure response received\n", dev->name);
788 break;
791 * Adapter memory configuration
793 case CMD_CONFIGURE_ADAPTER_RESPONSE:
794 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
795 if (elp_debug >= 3)
796 printk("%s: Adapter memory configuration %s.\n", dev->name,
797 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
798 break;
801 * Multicast list loading
803 case CMD_LOAD_MULTICAST_RESPONSE:
804 adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
805 if (elp_debug >= 3)
806 printk("%s: Multicast address list loading %s.\n", dev->name,
807 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
808 break;
811 * Station address setting
813 case CMD_SET_ADDRESS_RESPONSE:
814 adapter->got[CMD_SET_STATION_ADDRESS] = 1;
815 if (elp_debug >= 3)
816 printk("%s: Ethernet address setting %s.\n", dev->name,
817 adapter->irx_pcb.data.failed ? "failed" : "succeeded");
818 break;
822 * received board statistics
824 case CMD_NETWORK_STATISTICS_RESPONSE:
825 adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
826 adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
827 adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
828 adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
829 adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
830 adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
831 adapter->got[CMD_NETWORK_STATISTICS] = 1;
832 if (elp_debug >= 3)
833 printk("%s: interrupt - statistics response received\n", dev->name);
834 break;
837 * sent a packet
839 case CMD_TRANSMIT_PACKET_COMPLETE:
840 if (elp_debug >= 3)
841 printk("%s: interrupt - packet sent\n", dev->name);
842 if (dev->start == 0)
843 break;
844 switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
845 case 0xffff:
846 adapter->stats.tx_aborted_errors++;
847 printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
848 break;
849 case 0xfffe:
850 adapter->stats.tx_fifo_errors++;
851 printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
852 break;
854 dev->tbusy = 0;
855 mark_bh(NET_BH);
856 break;
859 * some unknown PCB
861 default:
862 printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
863 break;
865 } else {
866 printk("%s: failed to read PCB on interrupt\n", dev->name);
867 adapter_reset(dev);
871 } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
873 prime_rx(dev);
876 * indicate no longer in interrupt routine
878 dev->interrupt = 0;
882 /******************************************************
884 * open the board
886 ******************************************************/
888 static int elp_open(struct net_device *dev)
890 elp_device *adapter;
892 adapter = dev->priv;
894 if (elp_debug >= 3)
895 printk("%s: request to open device\n", dev->name);
898 * make sure we actually found the device
900 if (adapter == NULL) {
901 printk("%s: Opening a non-existent physical device\n", dev->name);
902 return -EAGAIN;
905 * disable interrupts on the board
907 outb_control(0, dev);
910 * clear any pending interrupts
912 inb_command(dev->base_addr);
913 adapter_reset(dev);
916 * interrupt routine not entered
918 dev->interrupt = 0;
921 * transmitter not busy
923 dev->tbusy = 0;
926 * no receive PCBs active
928 adapter->rx_active = 0;
930 adapter->busy = 0;
931 adapter->send_pcb_semaphore = 0;
932 adapter->rx_backlog.in = 0;
933 adapter->rx_backlog.out = 0;
936 * install our interrupt service routine
938 if (request_irq(dev->irq, &elp_interrupt, 0, "3c505", dev)) {
939 return -EAGAIN;
941 if (request_dma(dev->dma, "3c505")) {
942 printk("%s: could not allocate DMA channel\n", dev->name);
943 return -EAGAIN;
945 adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
946 if (!adapter->dma_buffer) {
947 printk("Could not allocate DMA buffer\n");
949 adapter->dmaing = 0;
952 * enable interrupts on the board
954 outb_control(CMDE, dev);
957 * configure adapter memory: we need 10 multicast addresses, default==0
959 if (elp_debug >= 3)
960 printk("%s: sending 3c505 memory configuration command\n", dev->name);
961 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
962 adapter->tx_pcb.data.memconf.cmd_q = 10;
963 adapter->tx_pcb.data.memconf.rcv_q = 20;
964 adapter->tx_pcb.data.memconf.mcast = 10;
965 adapter->tx_pcb.data.memconf.frame = 20;
966 adapter->tx_pcb.data.memconf.rcv_b = 20;
967 adapter->tx_pcb.data.memconf.progs = 0;
968 adapter->tx_pcb.length = sizeof(struct Memconf);
969 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
970 if (!send_pcb(dev, &adapter->tx_pcb))
971 printk("%s: couldn't send memory configuration command\n", dev->name);
972 else {
973 int timeout = jiffies + TIMEOUT;
974 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
975 if (time_after_eq(jiffies, timeout))
976 TIMEOUT_MSG(__LINE__);
981 * configure adapter to receive broadcast messages and wait for response
983 if (elp_debug >= 3)
984 printk("%s: sending 82586 configure command\n", dev->name);
985 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
986 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
987 adapter->tx_pcb.length = 2;
988 adapter->got[CMD_CONFIGURE_82586] = 0;
989 if (!send_pcb(dev, &adapter->tx_pcb))
990 printk("%s: couldn't send 82586 configure command\n", dev->name);
991 else {
992 int timeout = jiffies + TIMEOUT;
993 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
994 if (time_after_eq(jiffies, timeout))
995 TIMEOUT_MSG(__LINE__);
998 /* enable burst-mode DMA */
999 /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
1002 * queue receive commands to provide buffering
1004 prime_rx(dev);
1005 if (elp_debug >= 3)
1006 printk("%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
1009 * device is now officially open!
1011 dev->start = 1;
1013 MOD_INC_USE_COUNT;
1015 return 0; /* Always succeed */
1019 /******************************************************
1021 * send a packet to the adapter
1023 ******************************************************/
1025 static int send_packet(struct net_device *dev, struct sk_buff *skb)
1027 elp_device *adapter = dev->priv;
1028 unsigned long target;
1029 unsigned long flags;
1032 * make sure the length is even and no shorter than 60 bytes
1034 unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
1036 if (test_and_set_bit(0, (void *) &adapter->busy)) {
1037 if (elp_debug >= 2)
1038 printk("%s: transmit blocked\n", dev->name);
1039 return FALSE;
1042 adapter->stats.tx_bytes += nlen;
1045 * send the adapter a transmit packet command. Ignore segment and offset
1046 * and make sure the length is even
1048 adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1049 adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1050 adapter->tx_pcb.data.xmit_pkt.buf_ofs
1051 = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
1052 adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1054 if (!send_pcb(dev, &adapter->tx_pcb)) {
1055 adapter->busy = 0;
1056 return FALSE;
1058 /* if this happens, we die */
1059 if (test_and_set_bit(0, (void *) &adapter->dmaing))
1060 printk("%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1062 adapter->current_dma.direction = 1;
1063 adapter->current_dma.start_time = jiffies;
1065 target = virt_to_bus(skb->data);
1066 if ((target + nlen) >= MAX_DMA_ADDRESS) {
1067 memcpy(adapter->dma_buffer, skb->data, nlen);
1068 target = virt_to_bus(adapter->dma_buffer);
1070 adapter->current_dma.skb = skb;
1072 flags=claim_dma_lock();
1073 disable_dma(dev->dma);
1074 clear_dma_ff(dev->dma);
1075 set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
1076 set_dma_addr(dev->dma, target);
1077 set_dma_count(dev->dma, nlen);
1078 outb_control(adapter->hcr_val | DMAE | TCEN, dev);
1079 enable_dma(dev->dma);
1080 release_dma_lock(flags);
1082 if (elp_debug >= 3)
1083 printk("%s: DMA transfer started\n", dev->name);
1085 return TRUE;
1088 /******************************************************
1090 * start the transmitter
1091 * return 0 if sent OK, else return 1
1093 ******************************************************/
1095 static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1097 if (dev->interrupt) {
1098 printk("%s: start_xmit aborted (in irq)\n", dev->name);
1099 return 1;
1102 check_3c505_dma(dev);
1105 * if the transmitter is still busy, we have a transmit timeout...
1107 if (dev->tbusy) {
1108 elp_device *adapter = dev->priv;
1109 int tickssofar = jiffies - dev->trans_start;
1110 int stat;
1112 if (tickssofar < 1000)
1113 return 1;
1115 stat = inb_status(dev->base_addr);
1116 printk("%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1117 if (elp_debug >= 1)
1118 printk("%s: status %#02x\n", dev->name, stat);
1119 dev->trans_start = jiffies;
1120 dev->tbusy = 0;
1121 adapter->stats.tx_dropped++;
1124 if (elp_debug >= 3)
1125 printk("%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1127 if (test_and_set_bit(0, (void *) &dev->tbusy)) {
1128 printk("%s: transmitter access conflict\n", dev->name);
1129 return 1;
1132 * send the packet at skb->data for skb->len
1134 if (!send_packet(dev, skb)) {
1135 if (elp_debug >= 2) {
1136 printk("%s: failed to transmit packet\n", dev->name);
1138 dev->tbusy = 0;
1139 return 1;
1141 if (elp_debug >= 3)
1142 printk("%s: packet of length %d sent\n", dev->name, (int) skb->len);
1145 * start the transmit timeout
1147 dev->trans_start = jiffies;
1149 prime_rx(dev);
1151 return 0;
1154 /******************************************************
1156 * return statistics on the board
1158 ******************************************************/
1160 static struct net_device_stats *elp_get_stats(struct net_device *dev)
1162 elp_device *adapter = (elp_device *) dev->priv;
1164 if (elp_debug >= 3)
1165 printk("%s: request for stats\n", dev->name);
1167 /* If the device is closed, just return the latest stats we have,
1168 - we cannot ask from the adapter without interrupts */
1169 if (!dev->start)
1170 return &adapter->stats;
1172 /* send a get statistics command to the board */
1173 adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1174 adapter->tx_pcb.length = 0;
1175 adapter->got[CMD_NETWORK_STATISTICS] = 0;
1176 if (!send_pcb(dev, &adapter->tx_pcb))
1177 printk("%s: couldn't send get statistics command\n", dev->name);
1178 else {
1179 int timeout = jiffies + TIMEOUT;
1180 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
1181 if (time_after_eq(jiffies, timeout)) {
1182 TIMEOUT_MSG(__LINE__);
1183 return &adapter->stats;
1187 /* statistics are now up to date */
1188 return &adapter->stats;
1191 /******************************************************
1193 * close the board
1195 ******************************************************/
1197 static int elp_close(struct net_device *dev)
1199 elp_device *adapter;
1201 adapter = dev->priv;
1203 if (elp_debug >= 3)
1204 printk("%s: request to close device\n", dev->name);
1206 /* Someone may request the device statistic information even when
1207 * the interface is closed. The following will update the statistics
1208 * structure in the driver, so we'll be able to give current statistics.
1210 (void) elp_get_stats(dev);
1213 * disable interrupts on the board
1215 outb_control(0, dev);
1218 * flag transmitter as busy (i.e. not available)
1220 dev->tbusy = 1;
1223 * indicate device is closed
1225 dev->start = 0;
1228 * release the IRQ
1230 free_irq(dev->irq, dev);
1232 free_dma(dev->dma);
1233 free_pages((unsigned long) adapter->dma_buffer, __get_order(DMA_BUFFER_SIZE));
1235 MOD_DEC_USE_COUNT;
1237 return 0;
1241 /************************************************************
1243 * Set multicast list
1244 * num_addrs==0: clear mc_list
1245 * num_addrs==-1: set promiscuous mode
1246 * num_addrs>0: set mc_list
1248 ************************************************************/
1250 static void elp_set_mc_list(struct net_device *dev)
1252 elp_device *adapter = (elp_device *) dev->priv;
1253 struct dev_mc_list *dmi = dev->mc_list;
1254 int i;
1256 if (elp_debug >= 3)
1257 printk("%s: request to set multicast list\n", dev->name);
1259 if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1260 /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1261 /* if num_addrs==0 the list will be cleared */
1262 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1263 adapter->tx_pcb.length = 6 * dev->mc_count;
1264 for (i = 0; i < dev->mc_count; i++) {
1265 memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1266 dmi = dmi->next;
1268 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1269 if (!send_pcb(dev, &adapter->tx_pcb))
1270 printk("%s: couldn't send set_multicast command\n", dev->name);
1271 else {
1272 int timeout = jiffies + TIMEOUT;
1273 while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
1274 if (time_after_eq(jiffies, timeout)) {
1275 TIMEOUT_MSG(__LINE__);
1278 if (dev->mc_count)
1279 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1280 else /* num_addrs == 0 */
1281 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1282 } else
1283 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1285 * configure adapter to receive messages (as specified above)
1286 * and wait for response
1288 if (elp_debug >= 3)
1289 printk("%s: sending 82586 configure command\n", dev->name);
1290 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1291 adapter->tx_pcb.length = 2;
1292 adapter->got[CMD_CONFIGURE_82586] = 0;
1293 if (!send_pcb(dev, &adapter->tx_pcb))
1294 printk("%s: couldn't send 82586 configure command\n", dev->name);
1295 else {
1296 int timeout = jiffies + TIMEOUT;
1297 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
1298 if (time_after_eq(jiffies, timeout))
1299 TIMEOUT_MSG(__LINE__);
1303 /******************************************************
1305 * initialise Etherlink Plus board
1307 ******************************************************/
1309 static inline void elp_init(struct net_device *dev)
1311 elp_device *adapter = dev->priv;
1314 * set ptrs to various functions
1316 dev->open = elp_open; /* local */
1317 dev->stop = elp_close; /* local */
1318 dev->get_stats = elp_get_stats; /* local */
1319 dev->hard_start_xmit = elp_start_xmit; /* local */
1320 dev->set_multicast_list = elp_set_mc_list; /* local */
1322 /* Setup the generic properties */
1323 ether_setup(dev);
1326 * setup ptr to adapter specific information
1328 memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
1331 * memory information
1333 dev->mem_start = dev->mem_end = dev->rmem_end = dev->rmem_start = 0;
1336 /************************************************************
1338 * A couple of tests to see if there's 3C505 or not
1339 * Called only by elp_autodetect
1340 ************************************************************/
1342 static int __init elp_sense(struct net_device *dev)
1344 int timeout;
1345 int addr = dev->base_addr;
1346 const char *name = dev->name;
1347 long flags;
1348 byte orig_HSR;
1350 if (check_region(addr, 0xf))
1351 return -1;
1353 orig_HSR = inb_status(addr);
1355 if (elp_debug > 0)
1356 printk(search_msg, name, addr);
1358 if (orig_HSR == 0xff) {
1359 if (elp_debug > 0)
1360 printk(notfound_msg, 1);
1361 return -1;
1363 /* Enable interrupts - we need timers! */
1364 save_flags(flags);
1365 sti();
1367 /* Wait for a while; the adapter may still be booting up */
1368 if (elp_debug > 0)
1369 printk(stilllooking_msg);
1371 if (orig_HSR & DIR) {
1372 /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1373 outb(0, dev->base_addr + PORT_CONTROL);
1374 timeout = jiffies + 30*HZ/100;
1375 while (time_before(jiffies, timeout));
1376 restore_flags(flags);
1377 if (inb_status(addr) & DIR) {
1378 if (elp_debug > 0)
1379 printk(notfound_msg, 2);
1380 return -1;
1382 } else {
1383 /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1384 outb(DIR, dev->base_addr + PORT_CONTROL);
1385 timeout = jiffies + 30*HZ/100;
1386 while (time_before(jiffies, timeout));
1387 restore_flags(flags);
1388 if (!(inb_status(addr) & DIR)) {
1389 if (elp_debug > 0)
1390 printk(notfound_msg, 3);
1391 return -1;
1395 * It certainly looks like a 3c505.
1397 if (elp_debug > 0)
1398 printk(found_msg);
1400 return 0;
1403 /*************************************************************
1405 * Search through addr_list[] and try to find a 3C505
1406 * Called only by eplus_probe
1407 *************************************************************/
1409 static int __init elp_autodetect(struct net_device *dev)
1411 int idx = 0;
1413 /* if base address set, then only check that address
1414 otherwise, run through the table */
1415 if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
1416 if (elp_sense(dev) == 0)
1417 return dev->base_addr;
1418 } else
1419 while ((dev->base_addr = addr_list[idx++])) {
1420 if (elp_sense(dev) == 0)
1421 return dev->base_addr;
1424 /* could not find an adapter */
1425 if (elp_debug > 0)
1426 printk(couldnot_msg, dev->name);
1428 return 0; /* Because of this, the layer above will return -ENODEV */
1432 /******************************************************
1434 * probe for an Etherlink Plus board at the specified address
1436 ******************************************************/
1438 /* There are three situations we need to be able to detect here:
1440 * a) the card is idle
1441 * b) the card is still booting up
1442 * c) the card is stuck in a strange state (some DOS drivers do this)
1444 * In case (a), all is well. In case (b), we wait 10 seconds to see if the
1445 * card finishes booting, and carry on if so. In case (c), we do a hard reset,
1446 * loop round, and hope for the best.
1448 * This is all very unpleasant, but hopefully avoids the problems with the old
1449 * probe code (which had a 15-second delay if the card was idle, and didn't
1450 * work at all if it was in a weird state).
1453 int __init elplus_probe(struct net_device *dev)
1455 elp_device *adapter;
1456 int i, tries, tries1, timeout, okay;
1459 * setup adapter structure
1462 dev->base_addr = elp_autodetect(dev);
1463 if (!(dev->base_addr))
1464 return -ENODEV;
1467 * setup ptr to adapter specific information
1469 adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1470 if (adapter == NULL) {
1471 printk("%s: out of memory\n", dev->name);
1472 return -ENODEV;
1475 adapter->send_pcb_semaphore = 0;
1477 for (tries1 = 0; tries1 < 3; tries1++) {
1478 outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1479 /* First try to write just one byte, to see if the card is
1480 * responding at all normally.
1482 timeout = jiffies + 5*HZ/100;
1483 okay = 0;
1484 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1485 if ((inb_status(dev->base_addr) & HCRE)) {
1486 outb_command(0, dev->base_addr); /* send a spurious byte */
1487 timeout = jiffies + 5*HZ/100;
1488 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1489 if (inb_status(dev->base_addr) & HCRE)
1490 okay = 1;
1492 if (!okay) {
1493 /* Nope, it's ignoring the command register. This means that
1494 * either it's still booting up, or it's died.
1496 printk("%s: command register wouldn't drain, ", dev->name);
1497 if ((inb_status(dev->base_addr) & 7) == 3) {
1498 /* If the adapter status is 3, it *could* still be booting.
1499 * Give it the benefit of the doubt for 10 seconds.
1501 printk("assuming 3c505 still starting\n");
1502 timeout = jiffies + 10*HZ;
1503 while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1504 if (inb_status(dev->base_addr) & 7) {
1505 printk("%s: 3c505 failed to start\n", dev->name);
1506 } else {
1507 okay = 1; /* It started */
1509 } else {
1510 /* Otherwise, it must just be in a strange
1511 * state. We probably need to kick it.
1513 printk("3c505 is sulking\n");
1516 for (tries = 0; tries < 5 && okay; tries++) {
1519 * Try to set the Ethernet address, to make sure that the board
1520 * is working.
1522 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1523 adapter->tx_pcb.length = 0;
1524 autoirq_setup(0);
1525 if (!send_pcb(dev, &adapter->tx_pcb)) {
1526 printk("%s: could not send first PCB\n", dev->name);
1527 autoirq_report(0);
1528 continue;
1530 if (!receive_pcb(dev, &adapter->rx_pcb)) {
1531 printk("%s: could not read first PCB\n", dev->name);
1532 autoirq_report(0);
1533 continue;
1535 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1536 (adapter->rx_pcb.length != 6)) {
1537 printk("%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1538 autoirq_report(0);
1539 continue;
1541 goto okay;
1543 /* It's broken. Do a hard reset to re-initialise the board,
1544 * and try again.
1546 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1547 outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1548 outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1550 printk("%s: failed to initialise 3c505\n", dev->name);
1551 return -ENODEV;
1553 okay:
1554 if (dev->irq) { /* Is there a preset IRQ? */
1555 int rpt = autoirq_report(0);
1556 if (dev->irq != rpt) {
1557 printk("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1559 /* if dev->irq == autoirq_report(0), all is well */
1560 } else /* No preset IRQ; just use what we can detect */
1561 dev->irq = autoirq_report(0);
1562 switch (dev->irq) { /* Legal, sane? */
1563 case 0:
1564 printk("%s: IRQ probe failed: check 3c505 jumpers.\n",
1565 dev->name);
1566 return -ENODEV;
1567 case 1:
1568 case 6:
1569 case 8:
1570 case 13:
1571 printk("%s: Impossible IRQ %d reported by autoirq_report().\n",
1572 dev->name, dev->irq);
1573 return -ENODEV;
1576 * Now we have the IRQ number so we can disable the interrupts from
1577 * the board until the board is opened.
1579 outb_control(adapter->hcr_val & ~CMDE, dev);
1582 * copy Ethernet address into structure
1584 for (i = 0; i < 6; i++)
1585 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1587 /* find a DMA channel */
1588 if (!dev->dma) {
1589 if (dev->mem_start) {
1590 dev->dma = dev->mem_start & 7;
1592 else {
1593 printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
1594 dev->dma = ELP_DMA;
1599 * print remainder of startup message
1601 printk("%s: 3c505 at %#lx, irq %d, dma %d, ",
1602 dev->name, dev->base_addr, dev->irq, dev->dma);
1603 printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1604 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1605 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1608 * read more information from the adapter
1611 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1612 adapter->tx_pcb.length = 0;
1613 if (!send_pcb(dev, &adapter->tx_pcb) ||
1614 !receive_pcb(dev, &adapter->rx_pcb) ||
1615 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1616 (adapter->rx_pcb.length != 10)) {
1617 printk("not responding to second PCB\n");
1619 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);
1622 * reconfigure the adapter memory to better suit our purposes
1624 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1625 adapter->tx_pcb.length = 12;
1626 adapter->tx_pcb.data.memconf.cmd_q = 8;
1627 adapter->tx_pcb.data.memconf.rcv_q = 8;
1628 adapter->tx_pcb.data.memconf.mcast = 10;
1629 adapter->tx_pcb.data.memconf.frame = 10;
1630 adapter->tx_pcb.data.memconf.rcv_b = 10;
1631 adapter->tx_pcb.data.memconf.progs = 0;
1632 if (!send_pcb(dev, &adapter->tx_pcb) ||
1633 !receive_pcb(dev, &adapter->rx_pcb) ||
1634 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1635 (adapter->rx_pcb.length != 2)) {
1636 printk("%s: could not configure adapter memory\n", dev->name);
1638 if (adapter->rx_pcb.data.configure) {
1639 printk("%s: adapter configuration failed\n", dev->name);
1642 * and reserve the address region
1644 request_region(dev->base_addr, ELP_IO_EXTENT, "3c505");
1647 * initialise the device
1649 elp_init(dev);
1651 return 0;
1654 #ifdef MODULE
1655 #define NAMELEN 9
1656 static char devicename[ELP_MAX_CARDS][NAMELEN] = {{0,}};
1657 static struct net_device dev_3c505[ELP_MAX_CARDS] =
1659 { NULL, /* device name is inserted by net_init.c */
1660 0, 0, 0, 0,
1661 0, 0,
1662 0, 0, 0, NULL, elplus_probe},
1665 static int io[ELP_MAX_CARDS] = { 0, };
1666 static int irq[ELP_MAX_CARDS] = { 0, };
1667 static int dma[ELP_MAX_CARDS] = { 0, };
1668 MODULE_PARM(io, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1669 MODULE_PARM(irq, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1670 MODULE_PARM(dma, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1672 int init_module(void)
1674 int this_dev, found = 0;
1676 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1677 struct net_device *dev = &dev_3c505[this_dev];
1678 dev->name = devicename[this_dev];
1679 dev->irq = irq[this_dev];
1680 dev->base_addr = io[this_dev];
1681 if (dma[this_dev]) {
1682 dev->dma = dma[this_dev];
1683 } else {
1684 dev->dma = ELP_DMA;
1685 printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
1687 if (io[this_dev] == 0) {
1688 if (this_dev) break;
1689 printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
1691 if (register_netdev(dev) != 0) {
1692 printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
1693 if (found != 0) return 0;
1694 return -ENXIO;
1696 found++;
1698 return 0;
1701 void cleanup_module(void)
1703 int this_dev;
1705 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1706 struct net_device *dev = &dev_3c505[this_dev];
1707 if (dev->priv != NULL) {
1708 unregister_netdev(dev);
1709 kfree(dev->priv);
1710 dev->priv = NULL;
1711 release_region(dev->base_addr, ELP_IO_EXTENT);
1716 #endif /* MODULE */