1 /*
2 * Linux Ethernet device driver for the 3Com Etherlink Plus (3C505)
3 * By Craig Southeren, Juha Laiho and Philip Blundell
4 *
5 * 3c505.c This module implements an interface to the 3Com
6 * Etherlink Plus (3c505) Ethernet card. Linux device
7 * driver interface reverse engineered from the Linux 3C509
8 * device drivers. Some 3C505 information gleaned from
9 * the Crynwr packet driver. Still this driver would not
10 * be here without 3C505 technical reference provided by
11 * 3Com.
12 *
13 * $Id: 3c505.c,v 1.10 1996/04/16 13:06:27 phil Exp $
14 *
15 * Authors: Linux 3c505 device driver by
16 * Craig Southeren, <craigs@ineluki.apana.org.au>
17 * Final debugging by
18 * Andrew Tridgell, <tridge@nimbus.anu.edu.au>
19 * Auto irq/address, tuning, cleanup and v1.1.4+ kernel mods by
20 * Juha Laiho, <jlaiho@ichaos.nullnet.fi>
21 * Linux 3C509 driver by
22 * Donald Becker, <becker@super.org>
23 * 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>
37 */
38
39 /* Theory of operation:
40 *
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.
51 *
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.
61 *
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.
77 */
78
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.
82 *
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.
86 */
87
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.
91 */
92
93 #include <linux/module.h>
94
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>
108
109 #include <linux/netdevice.h>
110 #include <linux/etherdevice.h>
111 #include <linux/skbuff.h>
112 #include <linux/init.h>
113
114 #include "3c505.h"
115
116 /*********************************************************
117 *
118 * define debug messages here as common strings to reduce space
119 *
120 *********************************************************/
121
122 static const char *filename = __FILE__;
123
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))
127
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__)
132
133 static char search_msg[] __initdata = "%s: Looking for 3c505 adapter at address %#x...";
134
135 static char stilllooking_msg[] __initdata = "still looking...";
136
137 static char found_msg[] __initdata = "found.\n";
138
139 static char notfound_msg[] __initdata = "not found (reason = %d)\n";
140
141 static char couldnot_msg[] __initdata = "%s: 3c505 not found\n";
142
143 /*********************************************************
144 *
145 * various other debug stuff
146 *
147 *********************************************************/
148
149 #ifdef ELP_DEBUG
150 static const int elp_debug = ELP_DEBUG;
151 #else
152 static const int elp_debug = 0;
153 #endif
154
155 /*
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
160 */
161
162 /*****************************************************************
163 *
164 * useful macros
165 *
166 *****************************************************************/
167
168 #ifndef TRUE
169 #define TRUE 1
170 #endif
171
172 #ifndef FALSE
173 #define FALSE 0
174 #endif
175
176
177 /*****************************************************************
178 *
179 * List of I/O-addresses we try to auto-sense
180 * Last element MUST BE 0!
181 *****************************************************************/
182
183 static int addr_list[] __initdata = {0x300, 0x280, 0x310, 0};
184
185 /* Dma Memory related stuff */
186
187 static unsigned long dma_mem_alloc(int size)
188 {
189 int order = get_order(size);
190
191 return __get_dma_pages(GFP_KERNEL, order);
192 }
193
194
195 /*****************************************************************
196 *
197 * Functions for I/O (note the inline !)
198 *
199 *****************************************************************/
200
201 static inline unsigned char inb_status(unsigned int base_addr)
202 {
203 return inb(base_addr + PORT_STATUS);
204 }
205
206 static inline int inb_command(unsigned int base_addr)
207 {
208 return inb(base_addr + PORT_COMMAND);
209 }
210
211 static inline void outb_control(unsigned char val, struct net_device *dev)
212 {
213 outb(val, dev->base_addr + PORT_CONTROL);
214 ((elp_device *)(dev->priv))->hcr_val = val;
215 }
216
217 #define HCR_VAL(x) (((elp_device *)((x)->priv))->hcr_val)
218
219 static inline void outb_command(unsigned char val, unsigned int base_addr)
220 {
221 outb(val, base_addr + PORT_COMMAND);
222 }
223
224 static inline unsigned int inw_data(unsigned int base_addr)
225 {
226 return inw(base_addr + PORT_DATA);
227 }
228
229 static inline void outw_data(unsigned int val, unsigned int base_addr)
230 {
231 outw(val, base_addr + PORT_DATA);
232 }
233
234 static inline unsigned int backlog_next(unsigned int n)
235 {
236 return (n + 1) % BACKLOG_SIZE;
237 }
238
239 /*****************************************************************
240 *
241 * useful functions for accessing the adapter
242 *
243 *****************************************************************/
244
245 /*
246 * use this routine when accessing the ASF bits as they are
247 * changed asynchronously by the adapter
248 */
249
250 /* get adapter PCB status */
251 #define GET_ASF(addr) \
252 (get_status(addr)&ASF_PCB_MASK)
253
254 static inline int get_status(unsigned int base_addr)
255 {
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;
264 }
265
266 static inline void set_hsf(struct net_device *dev, int hsf)
267 {
268 cli();
269 outb_control((HCR_VAL(dev) & ~HSF_PCB_MASK) | hsf, dev);
270 sti();
271 }
272
273 static int start_receive(struct net_device *, pcb_struct *);
274
275 inline static void adapter_reset(struct net_device *dev)
276 {
277 int timeout;
278 elp_device *adapter = dev->priv;
279 unsigned char orig_hcr = adapter->hcr_val;
280
281 outb_control(0, dev);
282
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);
290 }
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));
303
304 outb_control(orig_hcr, dev);
305 if (!start_receive(dev, &adapter->tx_pcb))
306 printk("%s: start receive command failed \n", dev->name);
307 }
308
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.
312 */
313 static inline void check_3c505_dma(struct net_device *dev)
314 {
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;
323
324 f=claim_dma_lock();
325 disable_dma(dev->dma);
326 release_dma_lock(f);
327
328 if (adapter->rx_active)
329 adapter->rx_active--;
330 outb_control(adapter->hcr_val & ~(DMAE | TCEN | DIR), dev);
331 restore_flags(flags);
332 }
333 }
334
335 /* Primitive functions used by send_pcb() */
336 static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
337 {
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;
343 }
344 printk("3c505: send_pcb_slow timed out\n");
345 return TRUE;
346 }
347
348 static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
349 {
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;
355 }
356 printk("3c505: send_pcb_fast timed out\n");
357 return TRUE;
358 }
359
360 /* Check to see if the receiver needs restarting, and kick it if so */
361 static inline void prime_rx(struct net_device *dev)
362 {
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;
367 }
368 }
369
370 /*****************************************************************
371 *
372 * send_pcb
373 * Send a PCB to the adapter.
374 *
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
382 *
383 *****************************************************************/
384
385 /* This can be quite slow -- the adapter is allowed to take up to 40ms
386 * to respond to the initial interrupt.
387 *
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).
392 */
393
394 static int send_pcb(struct net_device *dev, pcb_struct * pcb)
395 {
396 int i;
397 int timeout;
398 elp_device *adapter = dev->priv;
399
400 check_3c505_dma(dev);
401
402 if (adapter->dmaing && adapter->current_dma.direction == 0)
403 return FALSE;
404
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);
409 }
410 return FALSE;
411 }
412 /*
413 * load each byte into the command register and
414 * wait for the HCRE bit to indicate the adapter
415 * had read the byte
416 */
417 set_hsf(dev, 0);
418
419 if (send_pcb_slow(dev->base_addr, pcb->command))
420 goto abort;
421
422 cli();
423
424 if (send_pcb_fast(dev->base_addr, pcb->length))
425 goto sti_abort;
426
427 for (i = 0; i < pcb->length; i++) {
428 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
429 goto sti_abort;
430 }
431
432 outb_control(adapter->hcr_val | 3, dev); /* signal end of PCB */
433 outb_command(2 + pcb->length, dev->base_addr);
434
435 /* now wait for the acknowledgement */
436 sti();
437
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;
450 }
451 }
452
453 if (elp_debug >= 1)
454 printk("%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
455
456 sti_abort:
457 sti();
458 abort:
459 adapter->send_pcb_semaphore = 0;
460 return FALSE;
461 }
462
463
464 /*****************************************************************
465 *
466 * receive_pcb
467 * Read a PCB from the adapter
468 *
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
474 *
475 *****************************************************************/
476
477 static int receive_pcb(struct net_device *dev, pcb_struct * pcb)
478 {
479 int i, j;
480 int total_length;
481 int stat;
482 int timeout;
483
484 elp_device *adapter = dev->priv;
485
486 set_hsf(dev, 0);
487
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;
494 }
495 pcb->command = inb_command(dev->base_addr);
496
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;
504 }
505 pcb->length = inb_command(dev->base_addr);
506
507 if (pcb->length > MAX_PCB_DATA) {
508 INVALID_PCB_MSG(pcb->length);
509 adapter_reset(dev);
510 return FALSE;
511 }
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;
526 }
527 /* woops, the last "data" byte was really the length! */
528 total_length = pcb->data.raw[--i];
529
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;
536 }
537
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;
547 }
548 }
549 }
550 set_hsf(dev, HSF_PCB_ACK);
551 return TRUE;
552 }
553
554 /******************************************************
555 *
556 * queue a receive command on the adapter so we will get an
557 * interrupt when a packet is received.
558 *
559 ******************************************************/
560
561 static int start_receive(struct net_device *dev, pcb_struct * tx_pcb)
562 {
563 int status;
564 elp_device *adapter = dev->priv;
565
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;
578 }
579
580 /******************************************************
581 *
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
586 *
587 ******************************************************/
588
589 static void receive_packet(struct net_device *dev, int len)
590 {
591 int rlen;
592 elp_device *adapter = dev->priv;
593 void *target;
594 struct sk_buff *skb;
595 unsigned long flags;
596
597 rlen = (len + 1) & ~1;
598 skb = dev_alloc_skb(rlen + 2);
599
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;
612 }
613 }
614
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);
618
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;
624
625 outb_control(adapter->hcr_val | DIR | TCEN | DMAE, dev);
626
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);
635
636 if (elp_debug >= 3) {
637 printk("%s: rx DMA transfer started\n", dev->name);
638 }
639
640 if (adapter->rx_active)
641 adapter->rx_active--;
642
643 if (!adapter->busy)
644 printk("%s: receive_packet called, busy not set.\n", dev->name);
645 }
646
647 /******************************************************
648 *
649 * interrupt handler
650 *
651 ******************************************************/
652
653 static void elp_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
654 {
655 int len;
656 int dlen;
657 int icount = 0;
658 struct net_device *dev;
659 elp_device *adapter;
660 int timeout;
661
662 dev = dev_id;
663 adapter = (elp_device *) dev->priv;
664
665 spin_lock(&adapter->lock);
666
667 do {
668 /*
669 * has a DMA transfer finished?
670 */
671 if (inb_status(dev->base_addr) & DONE) {
672 if (!adapter->dmaing) {
673 printk("%s: phantom DMA completed\n", dev->name);
674 }
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));
677 }
678
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);
688 }
689 skb->protocol = eth_type_trans(skb,dev);
690 adapter->stats.rx_bytes += skb->len;
691 netif_rx(skb);
692 }
693 }
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;
703 }
704 } else {
705 /* has one timed out? */
706 check_3c505_dma(dev);
707 }
708
709 /*
710 * receive a PCB from the adapter
711 */
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)
716 {
717 case 0:
718 break;
719 /*
720 * received a packet - this must be handled fast
721 */
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);
734 }
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);
742 }
743 if (elp_debug >= 3)
744 printk("%s: packet received\n", dev->name);
745 }
746 break;
747
748 /*
749 * 82586 configured correctly
750 */
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;
756
757 /*
758 * Adapter memory configuration
759 */
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;
766
767 /*
768 * Multicast list loading
769 */
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;
776
777 /*
778 * Station address setting
779 */
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;
786
787
788 /*
789 * received board statistics
790 */
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;
802
803 /*
804 * sent a packet
805 */
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;
820 }
821 netif_wake_queue(dev);
822 break;
823
824 /*
825 * some unknown PCB
826 */
827 default:
828 printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
829 break;
830 }
831 } else {
832 printk("%s: failed to read PCB on interrupt\n", dev->name);
833 adapter_reset(dev);
834 }
835 }
836
837 } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
838
839 prime_rx(dev);
840
841 /*
842 * indicate no longer in interrupt routine
843 */
844 spin_unlock(&adapter->lock);
845 }
846
847
848 /******************************************************
849 *
850 * open the board
851 *
852 ******************************************************/
853
854 static int elp_open(struct net_device *dev)
855 {
856 elp_device *adapter;
857 int retval;
858
859 adapter = dev->priv;
860
861 if (elp_debug >= 3)
862 printk("%s: request to open device\n", dev->name);
863
864 /*
865 * make sure we actually found the device
866 */
867 if (adapter == NULL) {
868 printk("%s: Opening a non-existent physical device\n", dev->name);
869 return -EAGAIN;
870 }
871 /*
872 * disable interrupts on the board
873 */
874 outb_control(0, dev);
875
876 /*
877 * clear any pending interrupts
878 */
879 inb_command(dev->base_addr);
880 adapter_reset(dev);
881
882 /*
883 * no receive PCBs active
884 */
885 adapter->rx_active = 0;
886
887 adapter->busy = 0;
888 adapter->send_pcb_semaphore = 0;
889 adapter->rx_backlog.in = 0;
890 adapter->rx_backlog.out = 0;
891
892 spin_lock_init(&adapter->lock);
893
894 /*
895 * install our interrupt service routine
896 */
897 if ((retval = request_irq(dev->irq, &elp_interrupt, 0, dev->name, dev))) {
898 printk(KERN_ERR "%s: could not allocate IRQ%d\n", dev->name, dev->irq);
899 return retval;
900 }
901 if ((retval = request_dma(dev->dma, dev->name))) {
902 free_irq(dev->irq, dev);
903 printk(KERN_ERR "%s: could not allocate DMA%d channel\n", dev->name, dev->dma);
904 return retval;
905 }
906 adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
907 if (!adapter->dma_buffer) {
908 printk(KERN_ERR "%s: could not allocate DMA buffer\n", dev->name);
909 free_dma(dev->dma);
910 free_irq(dev->irq, dev);
911 return -ENOMEM;
912 }
913 adapter->dmaing = 0;
914
915 /*
916 * enable interrupts on the board
917 */
918 outb_control(CMDE, dev);
919
920 /*
921 * configure adapter memory: we need 10 multicast addresses, default==0
922 */
923 if (elp_debug >= 3)
924 printk(KERN_DEBUG "%s: sending 3c505 memory configuration command\n", dev->name);
925 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
926 adapter->tx_pcb.data.memconf.cmd_q = 10;
927 adapter->tx_pcb.data.memconf.rcv_q = 20;
928 adapter->tx_pcb.data.memconf.mcast = 10;
929 adapter->tx_pcb.data.memconf.frame = 20;
930 adapter->tx_pcb.data.memconf.rcv_b = 20;
931 adapter->tx_pcb.data.memconf.progs = 0;
932 adapter->tx_pcb.length = sizeof(struct Memconf);
933 adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
934 if (!send_pcb(dev, &adapter->tx_pcb))
935 printk("%s: couldn't send memory configuration command\n", dev->name);
936 else {
937 int timeout = jiffies + TIMEOUT;
938 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && time_before(jiffies, timeout));
939 if (time_after_eq(jiffies, timeout))
940 TIMEOUT_MSG(__LINE__);
941 }
942
943
944 /*
945 * configure adapter to receive broadcast messages and wait for response
946 */
947 if (elp_debug >= 3)
948 printk("%s: sending 82586 configure command\n", dev->name);
949 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
950 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
951 adapter->tx_pcb.length = 2;
952 adapter->got[CMD_CONFIGURE_82586] = 0;
953 if (!send_pcb(dev, &adapter->tx_pcb))
954 printk("%s: couldn't send 82586 configure command\n", dev->name);
955 else {
956 int timeout = jiffies + TIMEOUT;
957 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
958 if (time_after_eq(jiffies, timeout))
959 TIMEOUT_MSG(__LINE__);
960 }
961
962 /* enable burst-mode DMA */
963 /* outb(0x1, dev->base_addr + PORT_AUXDMA); */
964
965 /*
966 * queue receive commands to provide buffering
967 */
968 prime_rx(dev);
969 if (elp_debug >= 3)
970 printk("%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
971
972 /*
973 * device is now officially open!
974 */
975
976 netif_start_queue(dev);
977 return 0;
978 }
979
980
981 /******************************************************
982 *
983 * send a packet to the adapter
984 *
985 ******************************************************/
986
987 static int send_packet(struct net_device *dev, struct sk_buff *skb)
988 {
989 elp_device *adapter = dev->priv;
990 unsigned long target;
991 unsigned long flags;
992
993 /*
994 * make sure the length is even and no shorter than 60 bytes
995 */
996 unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
997
998 if (test_and_set_bit(0, (void *) &adapter->busy)) {
999 if (elp_debug >= 2)
1000 printk("%s: transmit blocked\n", dev->name);
1001 return FALSE;
1002 }
1003
1004 adapter->stats.tx_bytes += nlen;
1005
1006 /*
1007 * send the adapter a transmit packet command. Ignore segment and offset
1008 * and make sure the length is even
1009 */
1010 adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1011 adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1012 adapter->tx_pcb.data.xmit_pkt.buf_ofs
1013 = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
1014 adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1015
1016 if (!send_pcb(dev, &adapter->tx_pcb)) {
1017 adapter->busy = 0;
1018 return FALSE;
1019 }
1020 /* if this happens, we die */
1021 if (test_and_set_bit(0, (void *) &adapter->dmaing))
1022 printk("%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1023
1024 adapter->current_dma.direction = 1;
1025 adapter->current_dma.start_time = jiffies;
1026
1027 target = virt_to_bus(skb->data);
1028 if ((target + nlen) >= MAX_DMA_ADDRESS) {
1029 memcpy(adapter->dma_buffer, skb->data, nlen);
1030 target = virt_to_bus(adapter->dma_buffer);
1031 }
1032 adapter->current_dma.skb = skb;
1033
1034 flags=claim_dma_lock();
1035 disable_dma(dev->dma);
1036 clear_dma_ff(dev->dma);
1037 set_dma_mode(dev->dma, 0x48); /* dma memory -> io */
1038 set_dma_addr(dev->dma, target);
1039 set_dma_count(dev->dma, nlen);
1040 outb_control(adapter->hcr_val | DMAE | TCEN, dev);
1041 enable_dma(dev->dma);
1042 release_dma_lock(flags);
1043
1044 if (elp_debug >= 3)
1045 printk("%s: DMA transfer started\n", dev->name);
1046
1047 return TRUE;
1048 }
1049
1050 /*
1051 * The upper layer thinks we timed out
1052 */
1053
1054 static void elp_timeout(struct net_device *dev)
1055 {
1056 elp_device *adapter = dev->priv;
1057 int stat;
1058
1059 stat = inb_status(dev->base_addr);
1060 printk(KERN_WARNING "%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1061 if (elp_debug >= 1)
1062 printk("%s: status %#02x\n", dev->name, stat);
1063 dev->trans_start = jiffies;
1064 adapter->stats.tx_dropped++;
1065 netif_wake_queue(dev);
1066 }
1067
1068 /******************************************************
1069 *
1070 * start the transmitter
1071 * return 0 if sent OK, else return 1
1072 *
1073 ******************************************************/
1074
1075 static int elp_start_xmit(struct sk_buff *skb, struct net_device *dev)
1076 {
1077 unsigned long flags;
1078 elp_device *adapter = dev->priv;
1079
1080 spin_lock_irqsave(&adapter->lock, flags);
1081 check_3c505_dma(dev);
1082
1083 if (elp_debug >= 3)
1084 printk("%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1085
1086 netif_stop_queue(dev);
1087
1088 /*
1089 * send the packet at skb->data for skb->len
1090 */
1091 if (!send_packet(dev, skb)) {
1092 if (elp_debug >= 2) {
1093 printk("%s: failed to transmit packet\n", dev->name);
1094 }
1095 spin_unlock_irqrestore(&adapter->lock, flags);
1096 return 1;
1097 }
1098 if (elp_debug >= 3)
1099 printk("%s: packet of length %d sent\n", dev->name, (int) skb->len);
1100
1101 /*
1102 * start the transmit timeout
1103 */
1104 dev->trans_start = jiffies;
1105
1106 prime_rx(dev);
1107 spin_unlock_irqrestore(&adapter->lock, flags);
1108 netif_start_queue(dev);
1109 return 0;
1110 }
1111
1112 /******************************************************
1113 *
1114 * return statistics on the board
1115 *
1116 ******************************************************/
1117
1118 static struct net_device_stats *elp_get_stats(struct net_device *dev)
1119 {
1120 elp_device *adapter = (elp_device *) dev->priv;
1121
1122 if (elp_debug >= 3)
1123 printk("%s: request for stats\n", dev->name);
1124
1125 /* If the device is closed, just return the latest stats we have,
1126 - we cannot ask from the adapter without interrupts */
1127 if (!netif_running(dev))
1128 return &adapter->stats;
1129
1130 /* send a get statistics command to the board */
1131 adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1132 adapter->tx_pcb.length = 0;
1133 adapter->got[CMD_NETWORK_STATISTICS] = 0;
1134 if (!send_pcb(dev, &adapter->tx_pcb))
1135 printk("%s: couldn't send get statistics command\n", dev->name);
1136 else {
1137 int timeout = jiffies + TIMEOUT;
1138 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && time_before(jiffies, timeout));
1139 if (time_after_eq(jiffies, timeout)) {
1140 TIMEOUT_MSG(__LINE__);
1141 return &adapter->stats;
1142 }
1143 }
1144
1145 /* statistics are now up to date */
1146 return &adapter->stats;
1147 }
1148
1149 /******************************************************
1150 *
1151 * close the board
1152 *
1153 ******************************************************/
1154
1155 static int elp_close(struct net_device *dev)
1156 {
1157 elp_device *adapter;
1158
1159 adapter = dev->priv;
1160
1161 if (elp_debug >= 3)
1162 printk("%s: request to close device\n", dev->name);
1163
1164 netif_stop_queue(dev);
1165
1166 /* Someone may request the device statistic information even when
1167 * the interface is closed. The following will update the statistics
1168 * structure in the driver, so we'll be able to give current statistics.
1169 */
1170 (void) elp_get_stats(dev);
1171
1172 /*
1173 * disable interrupts on the board
1174 */
1175 outb_control(0, dev);
1176
1177 /*
1178 * release the IRQ
1179 */
1180 free_irq(dev->irq, dev);
1181
1182 free_dma(dev->dma);
1183 free_pages((unsigned long) adapter->dma_buffer, get_order(DMA_BUFFER_SIZE));
1184
1185 return 0;
1186 }
1187
1188
1189 /************************************************************
1190 *
1191 * Set multicast list
1192 * num_addrs==0: clear mc_list
1193 * num_addrs==-1: set promiscuous mode
1194 * num_addrs>0: set mc_list
1195 *
1196 ************************************************************/
1197
1198 static void elp_set_mc_list(struct net_device *dev)
1199 {
1200 elp_device *adapter = (elp_device *) dev->priv;
1201 struct dev_mc_list *dmi = dev->mc_list;
1202 int i;
1203 unsigned long flags;
1204
1205 if (elp_debug >= 3)
1206 printk("%s: request to set multicast list\n", dev->name);
1207
1208 spin_lock_irqsave(&adapter->lock, flags);
1209
1210 if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1211 /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1212 /* if num_addrs==0 the list will be cleared */
1213 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1214 adapter->tx_pcb.length = 6 * dev->mc_count;
1215 for (i = 0; i < dev->mc_count; i++) {
1216 memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1217 dmi = dmi->next;
1218 }
1219 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1220 if (!send_pcb(dev, &adapter->tx_pcb))
1221 printk("%s: couldn't send set_multicast command\n", dev->name);
1222 else {
1223 int timeout = jiffies + TIMEOUT;
1224 while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && time_before(jiffies, timeout));
1225 if (time_after_eq(jiffies, timeout)) {
1226 TIMEOUT_MSG(__LINE__);
1227 }
1228 }
1229 if (dev->mc_count)
1230 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1231 else /* num_addrs == 0 */
1232 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1233 } else
1234 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1235 /*
1236 * configure adapter to receive messages (as specified above)
1237 * and wait for response
1238 */
1239 if (elp_debug >= 3)
1240 printk("%s: sending 82586 configure command\n", dev->name);
1241 adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1242 adapter->tx_pcb.length = 2;
1243 adapter->got[CMD_CONFIGURE_82586] = 0;
1244 if (!send_pcb(dev, &adapter->tx_pcb))
1245 {
1246 spin_unlock_irqrestore(&adapter->lock, flags);
1247 printk("%s: couldn't send 82586 configure command\n", dev->name);
1248 }
1249 else {
1250 int timeout = jiffies + TIMEOUT;
1251 spin_unlock_irqrestore(&adapter->lock, flags);
1252 while (adapter->got[CMD_CONFIGURE_82586] == 0 && time_before(jiffies, timeout));
1253 if (time_after_eq(jiffies, timeout))
1254 TIMEOUT_MSG(__LINE__);
1255 }
1256 }
1257
1258 /******************************************************
1259 *
1260 * initialise Etherlink Plus board
1261 *
1262 ******************************************************/
1263
1264 static inline void elp_init(struct net_device *dev)
1265 {
1266 elp_device *adapter = dev->priv;
1267
1268 /*
1269 * set ptrs to various functions
1270 */
1271 dev->open = elp_open; /* local */
1272 dev->stop = elp_close; /* local */
1273 dev->get_stats = elp_get_stats; /* local */
1274 dev->hard_start_xmit = elp_start_xmit; /* local */
1275 dev->tx_timeout = elp_timeout; /* local */
1276 dev->watchdog_timeo = 10*HZ;
1277 dev->set_multicast_list = elp_set_mc_list; /* local */
1278
1279 /* Setup the generic properties */
1280 ether_setup(dev);
1281
1282 /*
1283 * setup ptr to adapter specific information
1284 */
1285 memset(&(adapter->stats), 0, sizeof(struct net_device_stats));
1286
1287 /*
1288 * memory information
1289 */
1290 dev->mem_start = dev->mem_end = dev->rmem_end = dev->rmem_start = 0;
1291 }
1292
1293 /************************************************************
1294 *
1295 * A couple of tests to see if there's 3C505 or not
1296 * Called only by elp_autodetect
1297 ************************************************************/
1298
1299 static int __init elp_sense(struct net_device *dev)
1300 {
1301 int timeout;
1302 int addr = dev->base_addr;
1303 const char *name = dev->name;
1304 long flags;
1305 byte orig_HSR;
1306
1307 if (!request_region(addr, ELP_IO_EXTENT, "3c505"))
1308 return -ENODEV;
1309
1310 orig_HSR = inb_status(addr);
1311
1312 if (elp_debug > 0)
1313 printk(search_msg, name, addr);
1314
1315 if (orig_HSR == 0xff) {
1316 if (elp_debug > 0)
1317 printk(notfound_msg, 1);
1318 goto out;
1319 }
1320 /* Enable interrupts - we need timers! */
1321 save_flags(flags);
1322 sti();
1323
1324 /* Wait for a while; the adapter may still be booting up */
1325 if (elp_debug > 0)
1326 printk(stilllooking_msg);
1327
1328 if (orig_HSR & DIR) {
1329 /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1330 outb(0, dev->base_addr + PORT_CONTROL);
1331 timeout = jiffies + 30*HZ/100;
1332 while (time_before(jiffies, timeout));
1333 restore_flags(flags);
1334 if (inb_status(addr) & DIR) {
1335 if (elp_debug > 0)
1336 printk(notfound_msg, 2);
1337 goto out;
1338 }
1339 } else {
1340 /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1341 outb(DIR, dev->base_addr + PORT_CONTROL);
1342 timeout = jiffies + 30*HZ/100;
1343 while (time_before(jiffies, timeout));
1344 restore_flags(flags);
1345 if (!(inb_status(addr) & DIR)) {
1346 if (elp_debug > 0)
1347 printk(notfound_msg, 3);
1348 goto out;
1349 }
1350 }
1351 /*
1352 * It certainly looks like a 3c505.
1353 */
1354 if (elp_debug > 0)
1355 printk(found_msg);
1356
1357 return 0;
1358 out:
1359 release_region(addr, ELP_IO_EXTENT);
1360 return -ENODEV;
1361 }
1362
1363 /*************************************************************
1364 *
1365 * Search through addr_list[] and try to find a 3C505
1366 * Called only by eplus_probe
1367 *************************************************************/
1368
1369 static int __init elp_autodetect(struct net_device *dev)
1370 {
1371 int idx = 0;
1372
1373 /* if base address set, then only check that address
1374 otherwise, run through the table */
1375 if (dev->base_addr != 0) { /* dev->base_addr == 0 ==> plain autodetect */
1376 if (elp_sense(dev) == 0)
1377 return dev->base_addr;
1378 } else
1379 while ((dev->base_addr = addr_list[idx++])) {
1380 if (elp_sense(dev) == 0)
1381 return dev->base_addr;
1382 }
1383
1384 /* could not find an adapter */
1385 if (elp_debug > 0)
1386 printk(couldnot_msg, dev->name);
1387
1388 return 0; /* Because of this, the layer above will return -ENODEV */
1389 }
1390
1391
1392 /******************************************************
1393 *
1394 * probe for an Etherlink Plus board at the specified address
1395 *
1396 ******************************************************/
1397
1398 /* There are three situations we need to be able to detect here:
1399
1400 * a) the card is idle
1401 * b) the card is still booting up
1402 * c) the card is stuck in a strange state (some DOS drivers do this)
1403 *
1404 * In case (a), all is well. In case (b), we wait 10 seconds to see if the
1405 * card finishes booting, and carry on if so. In case (c), we do a hard reset,
1406 * loop round, and hope for the best.
1407 *
1408 * This is all very unpleasant, but hopefully avoids the problems with the old
1409 * probe code (which had a 15-second delay if the card was idle, and didn't
1410 * work at all if it was in a weird state).
1411 */
1412
1413 int __init elplus_probe(struct net_device *dev)
1414 {
1415 elp_device *adapter;
1416 int i, tries, tries1, timeout, okay;
1417 unsigned long cookie = 0;
1418
1419 SET_MODULE_OWNER(dev);
1420
1421 /*
1422 * setup adapter structure
1423 */
1424
1425 dev->base_addr = elp_autodetect(dev);
1426 if (!(dev->base_addr))
1427 return -ENODEV;
1428
1429 /*
1430 * setup ptr to adapter specific information
1431 */
1432 adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1433 if (adapter == NULL) {
1434 printk("%s: out of memory\n", dev->name);
1435 return -ENODEV;
1436 }
1437
1438 adapter->send_pcb_semaphore = 0;
1439
1440 for (tries1 = 0; tries1 < 3; tries1++) {
1441 outb_control((adapter->hcr_val | CMDE) & ~DIR, dev);
1442 /* First try to write just one byte, to see if the card is
1443 * responding at all normally.
1444 */
1445 timeout = jiffies + 5*HZ/100;
1446 okay = 0;
1447 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1448 if ((inb_status(dev->base_addr) & HCRE)) {
1449 outb_command(0, dev->base_addr); /* send a spurious byte */
1450 timeout = jiffies + 5*HZ/100;
1451 while (time_before(jiffies, timeout) && !(inb_status(dev->base_addr) & HCRE));
1452 if (inb_status(dev->base_addr) & HCRE)
1453 okay = 1;
1454 }
1455 if (!okay) {
1456 /* Nope, it's ignoring the command register. This means that
1457 * either it's still booting up, or it's died.
1458 */
1459 printk("%s: command register wouldn't drain, ", dev->name);
1460 if ((inb_status(dev->base_addr) & 7) == 3) {
1461 /* If the adapter status is 3, it *could* still be booting.
1462 * Give it the benefit of the doubt for 10 seconds.
1463 */
1464 printk("assuming 3c505 still starting\n");
1465 timeout = jiffies + 10*HZ;
1466 while (time_before(jiffies, timeout) && (inb_status(dev->base_addr) & 7));
1467 if (inb_status(dev->base_addr) & 7) {
1468 printk("%s: 3c505 failed to start\n", dev->name);
1469 } else {
1470 okay = 1; /* It started */
1471 }
1472 } else {
1473 /* Otherwise, it must just be in a strange
1474 * state. We probably need to kick it.
1475 */
1476 printk("3c505 is sulking\n");
1477 }
1478 }
1479 for (tries = 0; tries < 5 && okay; tries++) {
1480
1481 /*
1482 * Try to set the Ethernet address, to make sure that the board
1483 * is working.
1484 */
1485 adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1486 adapter->tx_pcb.length = 0;
1487 cookie = probe_irq_on();
1488 if (!send_pcb(dev, &adapter->tx_pcb)) {
1489 printk("%s: could not send first PCB\n", dev->name);
1490 probe_irq_off(cookie);
1491 continue;
1492 }
1493 if (!receive_pcb(dev, &adapter->rx_pcb)) {
1494 printk("%s: could not read first PCB\n", dev->name);
1495 probe_irq_off(cookie);
1496 continue;
1497 }
1498 if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1499 (adapter->rx_pcb.length != 6)) {
1500 printk("%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1501 probe_irq_off(cookie);
1502 continue;
1503 }
1504 goto okay;
1505 }
1506 /* It's broken. Do a hard reset to re-initialise the board,
1507 * and try again.
1508 */
1509 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1510 outb_control(adapter->hcr_val | FLSH | ATTN, dev);
1511 outb_control(adapter->hcr_val & ~(FLSH | ATTN), dev);
1512 }
1513 printk("%s: failed to initialise 3c505\n", dev->name);
1514 release_region(dev->base_addr, ELP_IO_EXTENT);
1515 return -ENODEV;
1516
1517 okay:
1518 if (dev->irq) { /* Is there a preset IRQ? */
1519 int rpt = probe_irq_off(cookie);
1520 if (dev->irq != rpt) {
1521 printk("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1522 }
1523 /* if dev->irq == probe_irq_off(cookie), all is well */
1524 } else /* No preset IRQ; just use what we can detect */
1525 dev->irq = probe_irq_off(cookie);
1526 switch (dev->irq) { /* Legal, sane? */
1527 case 0:
1528 printk("%s: IRQ probe failed: check 3c505 jumpers.\n",
1529 dev->name);
1530 return -ENODEV;
1531 case 1:
1532 case 6:
1533 case 8:
1534 case 13:
1535 printk("%s: Impossible IRQ %d reported by probe_irq_off().\n",
1536 dev->name, dev->irq);
1537 return -ENODEV;
1538 }
1539 /*
1540 * Now we have the IRQ number so we can disable the interrupts from
1541 * the board until the board is opened.
1542 */
1543 outb_control(adapter->hcr_val & ~CMDE, dev);
1544
1545 /*
1546 * copy Ethernet address into structure
1547 */
1548 for (i = 0; i < 6; i++)
1549 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1550
1551 /* find a DMA channel */
1552 if (!dev->dma) {
1553 if (dev->mem_start) {
1554 dev->dma = dev->mem_start & 7;
1555 }
1556 else {
1557 printk(KERN_WARNING "%s: warning, DMA channel not specified, using default\n", dev->name);
1558 dev->dma = ELP_DMA;
1559 }
1560 }
1561
1562 /*
1563 * print remainder of startup message
1564 */
1565 printk("%s: 3c505 at %#lx, irq %d, dma %d, ",
1566 dev->name, dev->base_addr, dev->irq, dev->dma);
1567 printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1568 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1569 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1570
1571 /*
1572 * read more information from the adapter
1573 */
1574
1575 adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1576 adapter->tx_pcb.length = 0;
1577 if (!send_pcb(dev, &adapter->tx_pcb) ||
1578 !receive_pcb(dev, &adapter->rx_pcb) ||
1579 (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1580 (adapter->rx_pcb.length != 10)) {
1581 printk("not responding to second PCB\n");
1582 }
1583 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);
1584
1585 /*
1586 * reconfigure the adapter memory to better suit our purposes
1587 */
1588 adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1589 adapter->tx_pcb.length = 12;
1590 adapter->tx_pcb.data.memconf.cmd_q = 8;
1591 adapter->tx_pcb.data.memconf.rcv_q = 8;
1592 adapter->tx_pcb.data.memconf.mcast = 10;
1593 adapter->tx_pcb.data.memconf.frame = 10;
1594 adapter->tx_pcb.data.memconf.rcv_b = 10;
1595 adapter->tx_pcb.data.memconf.progs = 0;
1596 if (!send_pcb(dev, &adapter->tx_pcb) ||
1597 !receive_pcb(dev, &adapter->rx_pcb) ||
1598 (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1599 (adapter->rx_pcb.length != 2)) {
1600 printk("%s: could not configure adapter memory\n", dev->name);
1601 }
1602 if (adapter->rx_pcb.data.configure) {
1603 printk("%s: adapter configuration failed\n", dev->name);
1604 }
1605
1606 /*
1607 * initialise the device
1608 */
1609 elp_init(dev);
1610
1611 return 0;
1612 }
1613
1614 #ifdef MODULE
1615 static struct net_device dev_3c505[ELP_MAX_CARDS];
1616 static int io[ELP_MAX_CARDS];
1617 static int irq[ELP_MAX_CARDS];
1618 static int dma[ELP_MAX_CARDS];
1619 MODULE_PARM(io, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1620 MODULE_PARM(irq, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1621 MODULE_PARM(dma, "1-" __MODULE_STRING(ELP_MAX_CARDS) "i");
1622
1623 int init_module(void)
1624 {
1625 int this_dev, found = 0;
1626
1627 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1628 struct net_device *dev = &dev_3c505[this_dev];
1629 dev->irq = irq[this_dev];
1630 dev->base_addr = io[this_dev];
1631 dev->init = elplus_probe;
1632 if (dma[this_dev]) {
1633 dev->dma = dma[this_dev];
1634 } else {
1635 dev->dma = ELP_DMA;
1636 printk(KERN_WARNING "3c505.c: warning, using default DMA channel,\n");
1637 }
1638 if (io[this_dev] == 0) {
1639 if (this_dev) break;
1640 printk(KERN_NOTICE "3c505.c: module autoprobe not recommended, give io=xx.\n");
1641 }
1642 if (register_netdev(dev) != 0) {
1643 printk(KERN_WARNING "3c505.c: Failed to register card at 0x%x.\n", io[this_dev]);
1644 if (found != 0) return 0;
1645 return -ENXIO;
1646 }
1647 found++;
1648 }
1649 return 0;
1650 }
1651
1652 void cleanup_module(void)
1653 {
1654 int this_dev;
1655
1656 for (this_dev = 0; this_dev < ELP_MAX_CARDS; this_dev++) {
1657 struct net_device *dev = &dev_3c505[this_dev];
1658 if (dev->priv != NULL) {
1659 unregister_netdev(dev);
1660 kfree(dev->priv);
1661 dev->priv = NULL;
1662 release_region(dev->base_addr, ELP_IO_EXTENT);
1663 }
1664 }
1665 }
1666
1667 #endif /* MODULE */
1668
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.