OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [net/] [3c505.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
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.1 2005-12-20 10:17:14 jcastillo 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 and DMA version by Philip Blundell
34
 */
35
 
36
/* Theory of operation:
37
 
38
 * The 3c505 is quite an intelligent board.  All communication with it is done
39
 * by means of Primary Command Blocks (PCBs); these are transferred using PIO
40
 * through the command register.  The card has 256k of on-board RAM, which is
41
 * used to buffer received packets.  It might seem at first that more buffers
42
 * are better, but in fact this isn't true.  From my tests, it seems that
43
 * more than about 10 buffers are unnecessary, and there is a noticeable
44
 * performance hit in having more active on the card.  So the majority of the
45
 * card's memory isn't, in fact, used.
46
 *
47
 * We keep up to 4 "receive packet" commands active on the board at a time.
48
 * When a packet comes in, so long as there is a receive command active, the
49
 * board will send us a "packet received" PCB and then add the data for that
50
 * packet to the DMA queue.  If a DMA transfer is not already in progress, we
51
 * set one up to start uploading the data.  We have to maintain a list of
52
 * backlogged receive packets, because the card may decide to tell us about
53
 * a newly-arrived packet at any time, and we may not be able to start a DMA
54
 * transfer immediately (ie one may already be going on).  We can't NAK the
55
 * PCB, because then it would throw the packet away.
56
 *
57
 * Trying to send a PCB to the card at the wrong moment seems to have bad
58
 * effects.  If we send it a transmit PCB while a receive DMA is happening,
59
 * it will just NAK the PCB and so we will have wasted our time.  Worse, it
60
 * sometimes seems to interrupt the transfer.  The majority of the low-level
61
 * code is protected by one huge semaphore -- "busy" -- which is set whenever
62
 * it probably isn't safe to do anything to the card.  The receive routine
63
 * must gain a lock on "busy" before it can start a DMA transfer, and the
64
 * transmit routine must gain a lock before it sends the first PCB to the card.
65
 * The send_pcb() routine also has an internal semaphore to protect it against
66
 * being re-entered (which would be disastrous) -- this is needed because
67
 * several things can happen asynchronously (re-priming the receiver and
68
 * asking the card for statistics, for example).  send_pcb() will also refuse
69
 * to talk to the card at all if a DMA upload is happening.  The higher-level
70
 * networking code will reschedule a later retry if some part of the driver
71
 * is blocked.  In practice, this doesn't seem to happen very often.
72
 */
73
 
74
/* This driver will not work with revision 2 hardware, because the host
75
 * control register is write-only.  It should be fairly easy to arrange to
76
 * keep our own soft-copy of the intended contents of this register, if
77
 * somebody has the time.  There may be firmware differences that cause
78
 * other problems, though, and I don't have an old card to test.
79
 */
80
 
81
/* The driver is a mess.  I took Craig's and Juha's code, and hacked it firstly
82
 * to make it more reliable, and secondly to add DMA mode.  Many things could
83
 * probably be done better; the concurrency protection is particularly awful.
84
 */
85
 
86
#include <linux/module.h>
87
 
88
#include <linux/kernel.h>
89
#include <linux/sched.h>
90
#include <linux/string.h>
91
#include <linux/interrupt.h>
92
#include <linux/ptrace.h>
93
#include <linux/errno.h>
94
#include <linux/in.h>
95
#include <linux/malloc.h>
96
#include <linux/ioport.h>
97
#include <asm/bitops.h>
98
#include <asm/io.h>
99
#include <asm/dma.h>
100
 
101
#include <linux/netdevice.h>
102
#include <linux/etherdevice.h>
103
#include <linux/skbuff.h>
104
 
105
#include "3c505.h"
106
 
107
#define ELP_DMA      6          /* DMA channel to use */
108
#define ELP_RX_PCBS  4
109
 
110
/*********************************************************
111
 *
112
 *  define debug messages here as common strings to reduce space
113
 *
114
 *********************************************************/
115
 
116
static const char *filename = __FILE__;
117
 
118
static const char *timeout_msg = "*** timeout at %s:%s (line %d) ***\n";
119
#define TIMEOUT_MSG(lineno) \
120
        printk(timeout_msg, filename,__FUNCTION__,(lineno))
121
 
122
static const char *invalid_pcb_msg =
123
"*** invalid pcb length %d at %s:%s (line %d) ***\n";
124
#define INVALID_PCB_MSG(len) \
125
        printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
126
 
127
static const char *search_msg = "%s: Looking for 3c505 adapter at address %#x...";
128
 
129
static const char *stilllooking_msg = "still looking...";
130
 
131
static const char *found_msg = "found.\n";
132
 
133
static const char *notfound_msg = "not found (reason = %d)\n";
134
 
135
static const char *couldnot_msg = "%s: 3c505 not found\n";
136
 
137
/*********************************************************
138
 *
139
 *  various other debug stuff
140
 *
141
 *********************************************************/
142
 
143
#ifdef ELP_DEBUG
144
static const int elp_debug = ELP_DEBUG;
145
#else
146
static const int elp_debug = 0;
147
#endif
148
 
149
/*
150
 *  0 = no messages (well, some)
151
 *  1 = messages when high level commands performed
152
 *  2 = messages when low level commands performed
153
 *  3 = messages when interrupts received
154
 */
155
 
156
/*****************************************************************
157
 *
158
 * useful macros
159
 *
160
 *****************************************************************/
161
 
162
#ifndef TRUE
163
#define TRUE    1
164
#endif
165
 
166
#ifndef FALSE
167
#define FALSE   0
168
#endif
169
 
170
 
171
/*****************************************************************
172
 *
173
 * List of I/O-addresses we try to auto-sense
174
 * Last element MUST BE 0!
175
 *****************************************************************/
176
 
177
const int addr_list[] = {0x300, 0x280, 0x310, 0};
178
 
179
/* Dma Memory related stuff */
180
 
181
/* Pure 2^n version of get_order */
182
static inline int __get_order(unsigned long size)
183
{
184
        int order;
185
 
186
        size = (size - 1) >> (PAGE_SHIFT - 1);
187
        order = -1;
188
        do {
189
                size >>= 1;
190
                order++;
191
        } while (size);
192
        return order;
193
}
194
 
195
static unsigned long dma_mem_alloc(int size)
196
{
197
        int order = __get_order(size);
198
 
199
        return __get_dma_pages(GFP_KERNEL, order);
200
}
201
 
202
 
203
/*****************************************************************
204
 *
205
 * Functions for I/O (note the inline !)
206
 *
207
 *****************************************************************/
208
 
209
static inline unsigned char inb_status(unsigned int base_addr)
210
{
211
        return inb(base_addr + PORT_STATUS);
212
}
213
 
214
static inline unsigned char inb_control(unsigned int base_addr)
215
{
216
        return inb(base_addr + PORT_CONTROL);
217
}
218
 
219
static inline int inb_command(unsigned int base_addr)
220
{
221
        return inb(base_addr + PORT_COMMAND);
222
}
223
 
224
static inline void outb_control(unsigned char val, unsigned int base_addr)
225
{
226
        outb(val, base_addr + PORT_CONTROL);
227
}
228
 
229
static inline void outb_command(unsigned char val, unsigned int base_addr)
230
{
231
        outb(val, base_addr + PORT_COMMAND);
232
}
233
 
234
static inline unsigned int inw_data(unsigned int base_addr)
235
{
236
        return inw(base_addr + PORT_DATA);
237
}
238
 
239
static inline void outw_data(unsigned int val, unsigned int base_addr)
240
{
241
        outw(val, base_addr + PORT_DATA);
242
}
243
 
244
 
245
/*****************************************************************
246
 *
247
 *  structure to hold context information for adapter
248
 *
249
 *****************************************************************/
250
 
251
#define DMA_BUFFER_SIZE  1600
252
#define BACKLOG_SIZE      4
253
 
254
typedef struct {
255
        volatile short got[NUM_TRANSMIT_CMDS];  /* flags for command completion */
256
        pcb_struct tx_pcb;      /* PCB for foreground sending */
257
        pcb_struct rx_pcb;      /* PCB for foreground receiving */
258
        pcb_struct itx_pcb;     /* PCB for background sending */
259
        pcb_struct irx_pcb;     /* PCB for background receiving */
260
        struct enet_statistics stats;
261
 
262
        void *dma_buffer;
263
 
264
        struct {
265
                unsigned int length[BACKLOG_SIZE];
266
                unsigned int in;
267
                unsigned int out;
268
        } rx_backlog;
269
 
270
        struct {
271
                unsigned int direction;
272
                unsigned int length;
273
                unsigned int copy_flag;
274
                struct sk_buff *skb;
275
                long int start_time;
276
        } current_dma;
277
 
278
        /* flags */
279
        unsigned long send_pcb_semaphore;
280
        unsigned int dmaing;
281
        unsigned long busy;
282
 
283
        unsigned int rx_active;  /* number of receive PCBs */
284
} elp_device;
285
 
286
static inline unsigned int backlog_next(unsigned int n)
287
{
288
        return (n + 1) % BACKLOG_SIZE;
289
}
290
 
291
/*****************************************************************
292
 *
293
 *  useful functions for accessing the adapter
294
 *
295
 *****************************************************************/
296
 
297
/*
298
 * use this routine when accessing the ASF bits as they are
299
 * changed asynchronously by the adapter
300
 */
301
 
302
/* get adapter PCB status */
303
#define GET_ASF(addr) \
304
        (get_status(addr)&ASF_PCB_MASK)
305
 
306
static inline int get_status(unsigned int base_addr)
307
{
308
        int timeout = jiffies + 10;
309
        register int stat1;
310
        do {
311
                stat1 = inb_status(base_addr);
312
        } while (stat1 != inb_status(base_addr) && jiffies < timeout);
313
        if (jiffies >= timeout)
314
                TIMEOUT_MSG(__LINE__);
315
        return stat1;
316
}
317
 
318
static inline void set_hsf(unsigned int base_addr, int hsf)
319
{
320
        cli();
321
        outb_control((inb_control(base_addr) & ~HSF_PCB_MASK) | hsf, base_addr);
322
        sti();
323
}
324
 
325
static int start_receive(struct device *, pcb_struct *);
326
 
327
inline static void adapter_reset(struct device *dev)
328
{
329
        int timeout;
330
        unsigned char orig_hcr = inb_control(dev->base_addr);
331
 
332
        elp_device *adapter = dev->priv;
333
 
334
        outb_control(0, dev->base_addr);
335
 
336
        if (inb_status(dev->base_addr) & ACRF) {
337
                do {
338
                        inb_command(dev->base_addr);
339
                        timeout = jiffies + 2;
340
                        while ((jiffies <= timeout) && !(inb_status(dev->base_addr) & ACRF));
341
                } while (inb_status(dev->base_addr) & ACRF);
342
                set_hsf(dev->base_addr, HSF_PCB_NAK);
343
        }
344
        outb_control(inb_control(dev->base_addr) | ATTN | DIR, dev->base_addr);
345
        timeout = jiffies + 1;
346
        while (jiffies <= timeout);
347
        outb_control(inb_control(dev->base_addr) & ~ATTN, dev->base_addr);
348
        timeout = jiffies + 1;
349
        while (jiffies <= timeout);
350
        outb_control(inb_control(dev->base_addr) | FLSH, dev->base_addr);
351
        timeout = jiffies + 1;
352
        while (jiffies <= timeout);
353
        outb_control(inb_control(dev->base_addr) & ~FLSH, dev->base_addr);
354
        timeout = jiffies + 1;
355
        while (jiffies <= timeout);
356
 
357
        outb_control(orig_hcr, dev->base_addr);
358
        if (!start_receive(dev, &adapter->tx_pcb))
359
                printk("%s: start receive command failed \n", dev->name);
360
}
361
 
362
/* Check to make sure that a DMA transfer hasn't timed out.  This should never happen
363
 * in theory, but seems to occur occasionally if the card gets prodded at the wrong
364
 * time.
365
 */
366
static inline void check_dma(struct device *dev)
367
{
368
        elp_device *adapter = dev->priv;
369
        if (adapter->dmaing && (jiffies > (adapter->current_dma.start_time + 10))) {
370
                unsigned long flags;
371
                printk("%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
372
                save_flags(flags);
373
                cli();
374
                adapter->dmaing = 0;
375
                adapter->busy = 0;
376
                disable_dma(dev->dma);
377
                if (adapter->rx_active)
378
                        adapter->rx_active--;
379
                outb_control(inb_control(dev->base_addr) & ~(DMAE | TCEN | DIR), dev->base_addr);
380
                restore_flags(flags);
381
        }
382
}
383
 
384
/* Primitive functions used by send_pcb() */
385
static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
386
{
387
        unsigned int timeout;
388
        outb_command(byte, base_addr);
389
        for (timeout = jiffies + 5; jiffies < timeout;) {
390
                if (inb_status(base_addr) & HCRE)
391
                        return FALSE;
392
        }
393
        printk("3c505: send_pcb_slow timed out\n");
394
        return TRUE;
395
}
396
 
397
static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
398
{
399
        unsigned int timeout;
400
        outb_command(byte, base_addr);
401
        for (timeout = 0; timeout < 40000; timeout++) {
402
                if (inb_status(base_addr) & HCRE)
403
                        return FALSE;
404
        }
405
        printk("3c505: send_pcb_fast timed out\n");
406
        return TRUE;
407
}
408
 
409
/* Check to see if the receiver needs restarting, and kick it if so */
410
static inline void prime_rx(struct device *dev)
411
{
412
        elp_device *adapter = dev->priv;
413
        while (adapter->rx_active < ELP_RX_PCBS && dev->start) {
414
                if (!start_receive(dev, &adapter->itx_pcb))
415
                        break;
416
        }
417
}
418
 
419
/*****************************************************************
420
 *
421
 * send_pcb
422
 *   Send a PCB to the adapter.
423
 *
424
 *      output byte to command reg  --<--+
425
 *      wait until HCRE is non zero      |
426
 *      loop until all bytes sent   -->--+
427
 *      set HSF1 and HSF2 to 1
428
 *      output pcb length
429
 *      wait until ASF give ACK or NAK
430
 *      set HSF1 and HSF2 to 0
431
 *
432
 *****************************************************************/
433
 
434
/* This can be quite slow -- the adapter is allowed to take up to 40ms
435
 * to respond to the initial interrupt.
436
 *
437
 * We run initially with interrupts turned on, but with a semaphore set
438
 * so that nobody tries to re-enter this code.  Once the first byte has
439
 * gone through, we turn interrupts off and then send the others (the
440
 * timeout is reduced to 500us).
441
 */
442
 
443
static int send_pcb(struct device *dev, pcb_struct * pcb)
444
{
445
        int i;
446
        int timeout;
447
        elp_device *adapter = dev->priv;
448
 
449
        check_dma(dev);
450
 
451
        if (adapter->dmaing && adapter->current_dma.direction == 0)
452
                return FALSE;
453
 
454
        /* Avoid contention */
455
        if (set_bit(1, &adapter->send_pcb_semaphore)) {
456
                if (elp_debug >= 3) {
457
                        printk("%s: send_pcb entered while threaded\n", dev->name);
458
                }
459
                return FALSE;
460
        }
461
        /*
462
         * load each byte into the command register and
463
         * wait for the HCRE bit to indicate the adapter
464
         * had read the byte
465
         */
466
        set_hsf(dev->base_addr, 0);
467
 
468
        if (send_pcb_slow(dev->base_addr, pcb->command))
469
                goto abort;
470
 
471
        cli();
472
 
473
        if (send_pcb_fast(dev->base_addr, pcb->length))
474
                goto sti_abort;
475
 
476
        for (i = 0; i < pcb->length; i++) {
477
                if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
478
                        goto sti_abort;
479
        }
480
 
481
        outb_control(inb_control(dev->base_addr) | 3, dev->base_addr);  /* signal end of PCB */
482
        outb_command(2 + pcb->length, dev->base_addr);
483
 
484
        /* now wait for the acknowledgement */
485
        sti();
486
 
487
        for (timeout = jiffies + 5; jiffies < timeout;) {
488
                switch (GET_ASF(dev->base_addr)) {
489
                case ASF_PCB_ACK:
490
                        adapter->send_pcb_semaphore = 0;
491
                        return TRUE;
492
                        break;
493
                case ASF_PCB_NAK:
494
                        printk("%s: send_pcb got NAK\n", dev->name);
495
                        goto abort;
496
                        break;
497
                }
498
        }
499
 
500
        if (elp_debug >= 1)
501
                printk("%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
502
 
503
      sti_abort:
504
        sti();
505
      abort:
506
        adapter->send_pcb_semaphore = 0;
507
        return FALSE;
508
}
509
 
510
 
511
/*****************************************************************
512
 *
513
 * receive_pcb
514
 *   Read a PCB from the adapter
515
 *
516
 *      wait for ACRF to be non-zero        ---<---+
517
 *      input a byte                               |
518
 *      if ASF1 and ASF2 were not both one         |
519
 *              before byte was read, loop      --->---+
520
 *      set HSF1 and HSF2 for ack
521
 *
522
 *****************************************************************/
523
 
524
static int receive_pcb(struct device *dev, pcb_struct * pcb)
525
{
526
        int i, j;
527
        int total_length;
528
        int stat;
529
        int timeout;
530
 
531
        elp_device *adapter = dev->priv;
532
 
533
        set_hsf(dev->base_addr, 0);
534
 
535
        /* get the command code */
536
        timeout = jiffies + 2;
537
        while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && jiffies < timeout);
538
        if (jiffies >= timeout) {
539
                TIMEOUT_MSG(__LINE__);
540
                return FALSE;
541
        }
542
        pcb->command = inb_command(dev->base_addr);
543
 
544
        /* read the data length */
545
        timeout = jiffies + 3;
546
        while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && jiffies < timeout);
547
        if (jiffies >= timeout) {
548
                TIMEOUT_MSG(__LINE__);
549
                printk("%s: status %02x\n", dev->name, stat);
550
                return FALSE;
551
        }
552
        pcb->length = inb_command(dev->base_addr);
553
 
554
        if (pcb->length > MAX_PCB_DATA) {
555
                INVALID_PCB_MSG(pcb->length);
556
                adapter_reset(dev);
557
                return FALSE;
558
        }
559
        /* read the data */
560
        cli();
561
        i = 0;
562
        do {
563
                j = 0;
564
                while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
565
                pcb->data.raw[i++] = inb_command(dev->base_addr);
566
                if (i > MAX_PCB_DATA)
567
                        INVALID_PCB_MSG(i);
568
        } while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
569
        sti();
570
        if (j >= 20000) {
571
                TIMEOUT_MSG(__LINE__);
572
                return FALSE;
573
        }
574
        /* woops, the last "data" byte was really the length! */
575
        total_length = pcb->data.raw[--i];
576
 
577
        /* safety check total length vs data length */
578
        if (total_length != (pcb->length + 2)) {
579
                if (elp_debug >= 2)
580
                        printk("%s: mangled PCB received\n", dev->name);
581
                set_hsf(dev->base_addr, HSF_PCB_NAK);
582
                return FALSE;
583
        }
584
 
585
        if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
586
                if (set_bit(0, (void *) &adapter->busy)) {
587
                        if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
588
                                set_hsf(dev->base_addr, HSF_PCB_NAK);
589
                                printk("%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
590
                                pcb->command = 0;
591
                                return TRUE;
592
                        } else {
593
                                pcb->command = 0xff;
594
                        }
595
                }
596
        }
597
        set_hsf(dev->base_addr, HSF_PCB_ACK);
598
        return TRUE;
599
}
600
 
601
/******************************************************
602
 *
603
 *  queue a receive command on the adapter so we will get an
604
 *  interrupt when a packet is received.
605
 *
606
 ******************************************************/
607
 
608
static int start_receive(struct device *dev, pcb_struct * tx_pcb)
609
{
610
        int status;
611
        elp_device *adapter = dev->priv;
612
 
613
        if (elp_debug >= 3)
614
                printk("%s: restarting receiver\n", dev->name);
615
        tx_pcb->command = CMD_RECEIVE_PACKET;
616
        tx_pcb->length = sizeof(struct Rcv_pkt);
617
        tx_pcb->data.rcv_pkt.buf_seg
618
            = tx_pcb->data.rcv_pkt.buf_ofs = 0;          /* Unused */
619
        tx_pcb->data.rcv_pkt.buf_len = 1600;
620
        tx_pcb->data.rcv_pkt.timeout = 0;        /* set timeout to zero */
621
        status = send_pcb(dev, tx_pcb);
622
        if (status)
623
                adapter->rx_active++;
624
        return status;
625
}
626
 
627
/******************************************************
628
 *
629
 * extract a packet from the adapter
630
 * this routine is only called from within the interrupt
631
 * service routine, so no cli/sti calls are needed
632
 * note that the length is always assumed to be even
633
 *
634
 ******************************************************/
635
 
636
static void receive_packet(struct device *dev, int len)
637
{
638
        int rlen;
639
        elp_device *adapter = dev->priv;
640
        unsigned long target;
641
        struct sk_buff *skb;
642
 
643
        rlen = (len + 1) & ~1;
644
        skb = dev_alloc_skb(rlen + 2);
645
 
646
        adapter->current_dma.copy_flag = 0;
647
 
648
        if (!skb) {
649
          printk("%s: memory squeeze, dropping packet\n", dev->name);
650
          target = virt_to_bus(adapter->dma_buffer);
651
        } else {
652
          skb_reserve(skb, 2);
653
          target = virt_to_bus(skb_put(skb, rlen));
654
          if ((target + rlen) >= MAX_DMA_ADDRESS) {
655
            target = virt_to_bus(adapter->dma_buffer);
656
            adapter->current_dma.copy_flag = 1;
657
          }
658
        }
659
        /* if this happens, we die */
660
        if (set_bit(0, (void *) &adapter->dmaing))
661
                printk("%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
662
 
663
        adapter->current_dma.direction = 0;
664
        adapter->current_dma.length = rlen;
665
        adapter->current_dma.skb = skb;
666
        adapter->current_dma.start_time = jiffies;
667
 
668
        outb_control(inb_control(dev->base_addr) | DIR | TCEN | DMAE, dev->base_addr);
669
 
670
        disable_dma(dev->dma);
671
        clear_dma_ff(dev->dma);
672
        set_dma_mode(dev->dma, 0x04);   /* dma read */
673
        set_dma_addr(dev->dma, target);
674
        set_dma_count(dev->dma, rlen);
675
        enable_dma(dev->dma);
676
 
677
        if (elp_debug >= 3) {
678
                printk("%s: rx DMA transfer started\n", dev->name);
679
        }
680
        if (adapter->rx_active)
681
                adapter->rx_active--;
682
 
683
        if (!adapter->busy)
684
                printk("%s: receive_packet called, busy not set.\n", dev->name);
685
}
686
 
687
/******************************************************
688
 *
689
 * interrupt handler
690
 *
691
 ******************************************************/
692
 
693
static void elp_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
694
{
695
        int len;
696
        int dlen;
697
        int icount = 0;
698
        struct device *dev;
699
        elp_device *adapter;
700
        int timeout;
701
 
702
        if (irq < 0 || irq > 15) {
703
                printk("elp_interrupt(): illegal IRQ number found in interrupt routine (%i)\n", irq);
704
                return;
705
        }
706
        dev = irq2dev_map[irq];
707
 
708
        if (dev == NULL) {
709
                printk("elp_interrupt(): irq %d for unknown device.\n", irq);
710
                return;
711
        }
712
        adapter = (elp_device *) dev->priv;
713
 
714
        if (dev->interrupt) {
715
                printk("%s: re-entering the interrupt handler!\n", dev->name);
716
                return;
717
        }
718
        dev->interrupt = 1;
719
 
720
        do {
721
                /*
722
                 * has a DMA transfer finished?
723
                 */
724
                if (inb_status(dev->base_addr) & DONE) {
725
                        if (!adapter->dmaing) {
726
                                printk("%s: phantom DMA completed\n", dev->name);
727
                        }
728
                        if (elp_debug >= 3) {
729
                                printk("%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
730
                        }
731
 
732
                        outb_control(inb_control(dev->base_addr) & ~(DMAE | TCEN | DIR), dev->base_addr);
733
                        if (adapter->current_dma.direction) {
734
                                dev_kfree_skb(adapter->current_dma.skb, FREE_WRITE);
735
                        } else {
736
                                struct sk_buff *skb = adapter->current_dma.skb;
737
                                if (skb) {
738
                                  skb->dev = dev;
739
                                  if (adapter->current_dma.copy_flag) {
740
                                    memcpy(skb_put(skb, adapter->current_dma.length), adapter->dma_buffer, adapter->current_dma.length);
741
                                  }
742
                                  skb->protocol = eth_type_trans(skb,dev);
743
                                  netif_rx(skb);
744
                                }
745
                        }
746
                        adapter->dmaing = 0;
747
                        if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
748
                                int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
749
                                adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
750
                                if (elp_debug >= 2)
751
                                        printk("%s: receiving backlogged packet (%d)\n", dev->name, t);
752
                                receive_packet(dev, t);
753
                        } else {
754
                                adapter->busy = 0;
755
                        }
756
                } else {
757
                        /* has one timed out? */
758
                        check_dma(dev);
759
                }
760
 
761
                sti();
762
 
763
                /*
764
                 * receive a PCB from the adapter
765
                 */
766
                timeout = jiffies + 3;
767
                while ((inb_status(dev->base_addr) & ACRF) != 0 && jiffies < timeout) {
768
                        if (receive_pcb(dev, &adapter->irx_pcb)) {
769
                                switch (adapter->irx_pcb.command) {
770
                                case 0:
771
                                        break;
772
                                        /*
773
                                         * received a packet - this must be handled fast
774
                                         */
775
                                case 0xff:
776
                                case CMD_RECEIVE_PACKET_COMPLETE:
777
                                        /* if the device isn't open, don't pass packets up the stack */
778
                                        if (dev->start == 0)
779
                                                break;
780
                                        cli();
781
                                        len = adapter->irx_pcb.data.rcv_resp.pkt_len;
782
                                        dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
783
                                        if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
784
                                                printk("%s: interrupt - packet not received correctly\n", dev->name);
785
                                                sti();
786
                                        } else {
787
                                                if (elp_debug >= 3) {
788
                                                        sti();
789
                                                        printk("%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
790
                                                        cli();
791
                                                }
792
                                                if (adapter->irx_pcb.command == 0xff) {
793
                                                        if (elp_debug >= 2)
794
                                                                printk("%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
795
                                                        adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
796
                                                        adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
797
                                                } else {
798
                                                        receive_packet(dev, dlen);
799
                                                }
800
                                                sti();
801
                                                if (elp_debug >= 3)
802
                                                        printk("%s: packet received\n", dev->name);
803
                                        }
804
                                        break;
805
 
806
                                        /*
807
                                         * 82586 configured correctly
808
                                         */
809
                                case CMD_CONFIGURE_82586_RESPONSE:
810
                                        adapter->got[CMD_CONFIGURE_82586] = 1;
811
                                        if (elp_debug >= 3)
812
                                                printk("%s: interrupt - configure response received\n", dev->name);
813
                                        break;
814
 
815
                                        /*
816
                                         * Adapter memory configuration
817
                                         */
818
                                case CMD_CONFIGURE_ADAPTER_RESPONSE:
819
                                        adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
820
                                        if (elp_debug >= 3)
821
                                                printk("%s: Adapter memory configuration %s.\n", dev->name,
822
                                                       adapter->irx_pcb.data.failed ? "failed" : "succeeded");
823
                                        break;
824
 
825
                                        /*
826
                                         * Multicast list loading
827
                                         */
828
                                case CMD_LOAD_MULTICAST_RESPONSE:
829
                                        adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
830
                                        if (elp_debug >= 3)
831
                                                printk("%s: Multicast address list loading %s.\n", dev->name,
832
                                                       adapter->irx_pcb.data.failed ? "failed" : "succeeded");
833
                                        break;
834
 
835
                                        /*
836
                                         * Station address setting
837
                                         */
838
                                case CMD_SET_ADDRESS_RESPONSE:
839
                                        adapter->got[CMD_SET_STATION_ADDRESS] = 1;
840
                                        if (elp_debug >= 3)
841
                                                printk("%s: Ethernet address setting %s.\n", dev->name,
842
                                                       adapter->irx_pcb.data.failed ? "failed" : "succeeded");
843
                                        break;
844
 
845
 
846
                                        /*
847
                                         * received board statistics
848
                                         */
849
                                case CMD_NETWORK_STATISTICS_RESPONSE:
850
                                        adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
851
                                        adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
852
                                        adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
853
                                        adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
854
                                        adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
855
                                        adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
856
                                        adapter->got[CMD_NETWORK_STATISTICS] = 1;
857
                                        if (elp_debug >= 3)
858
                                                printk("%s: interrupt - statistics response received\n", dev->name);
859
                                        break;
860
 
861
                                        /*
862
                                         * sent a packet
863
                                         */
864
                                case CMD_TRANSMIT_PACKET_COMPLETE:
865
                                        if (elp_debug >= 3)
866
                                                printk("%s: interrupt - packet sent\n", dev->name);
867
                                        if (dev->start == 0)
868
                                                break;
869
                                        switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
870
                                        case 0xffff:
871
                                                adapter->stats.tx_aborted_errors++;
872
                                                printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
873
                                                break;
874
                                        case 0xfffe:
875
                                                adapter->stats.tx_fifo_errors++;
876
                                                printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
877
                                                break;
878
                                        }
879
                                        dev->tbusy = 0;
880
                                        mark_bh(NET_BH);
881
                                        break;
882
 
883
                                        /*
884
                                         * some unknown PCB
885
                                         */
886
                                default:
887
                                        printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
888
                                        break;
889
                                }
890
                        } else {
891
                                printk("%s: failed to read PCB on interrupt\n", dev->name);
892
                                adapter_reset(dev);
893
                        }
894
                }
895
 
896
        } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
897
 
898
        prime_rx(dev);
899
 
900
        /*
901
         * indicate no longer in interrupt routine
902
         */
903
        dev->interrupt = 0;
904
}
905
 
906
 
907
/******************************************************
908
 *
909
 * open the board
910
 *
911
 ******************************************************/
912
 
913
static int elp_open(struct device *dev)
914
{
915
        elp_device *adapter;
916
 
917
        adapter = dev->priv;
918
 
919
        if (elp_debug >= 3)
920
                printk("%s: request to open device\n", dev->name);
921
 
922
        /*
923
         * make sure we actually found the device
924
         */
925
        if (adapter == NULL) {
926
                printk("%s: Opening a non-existent physical device\n", dev->name);
927
                return -EAGAIN;
928
        }
929
        /*
930
         * disable interrupts on the board
931
         */
932
        outb_control(0x00, dev->base_addr);
933
 
934
        /*
935
         * clear any pending interrupts
936
         */
937
        inb_command(dev->base_addr);
938
        adapter_reset(dev);
939
 
940
        /*
941
         * interrupt routine not entered
942
         */
943
        dev->interrupt = 0;
944
 
945
        /*
946
         *  transmitter not busy
947
         */
948
        dev->tbusy = 0;
949
 
950
        /*
951
         * no receive PCBs active
952
         */
953
        adapter->rx_active = 0;
954
 
955
        adapter->busy = 0;
956
        adapter->send_pcb_semaphore = 0;
957
        adapter->rx_backlog.in = 0;
958
        adapter->rx_backlog.out = 0;
959
 
960
        /*
961
         * make sure we can find the device header given the interrupt number
962
         */
963
        irq2dev_map[dev->irq] = dev;
964
 
965
        /*
966
         * install our interrupt service routine
967
         */
968
        if (request_irq(dev->irq, &elp_interrupt, 0, "3c505", NULL)) {
969
                irq2dev_map[dev->irq] = NULL;
970
                return -EAGAIN;
971
        }
972
        if (request_dma(dev->dma, "3c505")) {
973
                printk("%s: could not allocate DMA channel\n", dev->name);
974
                return -EAGAIN;
975
        }
976
        adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
977
        if (!adapter->dma_buffer) {
978
                printk("Could not allocate DMA buffer\n");
979
        }
980
        adapter->dmaing = 0;
981
 
982
        /*
983
         * enable interrupts on the board
984
         */
985
        outb_control(CMDE, dev->base_addr);
986
 
987
        /*
988
         * device is now officially open!
989
         */
990
        dev->start = 1;
991
 
992
        /*
993
         * configure adapter memory: we need 10 multicast addresses, default==0
994
         */
995
        if (elp_debug >= 3)
996
                printk("%s: sending 3c505 memory configuration command\n", dev->name);
997
        adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
998
        adapter->tx_pcb.data.memconf.cmd_q = 10;
999
        adapter->tx_pcb.data.memconf.rcv_q = 20;
1000
        adapter->tx_pcb.data.memconf.mcast = 10;
1001
        adapter->tx_pcb.data.memconf.frame = 20;
1002
        adapter->tx_pcb.data.memconf.rcv_b = 20;
1003
        adapter->tx_pcb.data.memconf.progs = 0;
1004
        adapter->tx_pcb.length = sizeof(struct Memconf);
1005
        adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
1006
        if (!send_pcb(dev, &adapter->tx_pcb))
1007
                printk("%s: couldn't send memory configuration command\n", dev->name);
1008
        else {
1009
                int timeout = jiffies + TIMEOUT;
1010
                while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && jiffies < timeout);
1011
                if (jiffies >= timeout)
1012
                        TIMEOUT_MSG(__LINE__);
1013
        }
1014
 
1015
 
1016
        /*
1017
         * configure adapter to receive broadcast messages and wait for response
1018
         */
1019
        if (elp_debug >= 3)
1020
                printk("%s: sending 82586 configure command\n", dev->name);
1021
        adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1022
        adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1023
        adapter->tx_pcb.length = 2;
1024
        adapter->got[CMD_CONFIGURE_82586] = 0;
1025
        if (!send_pcb(dev, &adapter->tx_pcb))
1026
                printk("%s: couldn't send 82586 configure command\n", dev->name);
1027
        else {
1028
                int timeout = jiffies + TIMEOUT;
1029
                while (adapter->got[CMD_CONFIGURE_82586] == 0 && jiffies < timeout);
1030
                if (jiffies >= timeout)
1031
                        TIMEOUT_MSG(__LINE__);
1032
        }
1033
 
1034
        /* enable burst-mode DMA */
1035
        outb(0x1, dev->base_addr + PORT_AUXDMA);
1036
 
1037
        /*
1038
         * queue receive commands to provide buffering
1039
         */
1040
        prime_rx(dev);
1041
        if (elp_debug >= 3)
1042
                printk("%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
1043
 
1044
        MOD_INC_USE_COUNT;
1045
 
1046
        return 0;                /* Always succeed */
1047
}
1048
 
1049
 
1050
/******************************************************
1051
 *
1052
 * send a packet to the adapter
1053
 *
1054
 ******************************************************/
1055
 
1056
static int send_packet(struct device *dev, struct sk_buff *skb)
1057
{
1058
        elp_device *adapter = dev->priv;
1059
        unsigned long target;
1060
 
1061
        /*
1062
         * make sure the length is even and no shorter than 60 bytes
1063
         */
1064
        unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
1065
 
1066
        if (set_bit(0, (void *) &adapter->busy)) {
1067
                if (elp_debug >= 2)
1068
                        printk("%s: transmit blocked\n", dev->name);
1069
                return FALSE;
1070
        }
1071
        adapter = dev->priv;
1072
 
1073
        /*
1074
         * send the adapter a transmit packet command. Ignore segment and offset
1075
         * and make sure the length is even
1076
         */
1077
        adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1078
        adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1079
        adapter->tx_pcb.data.xmit_pkt.buf_ofs
1080
            = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0; /* Unused */
1081
        adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1082
 
1083
        if (!send_pcb(dev, &adapter->tx_pcb)) {
1084
                adapter->busy = 0;
1085
                return FALSE;
1086
        }
1087
        /* if this happens, we die */
1088
        if (set_bit(0, (void *) &adapter->dmaing))
1089
                printk("%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1090
 
1091
        adapter->current_dma.direction = 1;
1092
        adapter->current_dma.start_time = jiffies;
1093
 
1094
        target = virt_to_bus(skb->data);
1095
        if ((target + nlen) >= MAX_DMA_ADDRESS) {
1096
                memcpy(adapter->dma_buffer, skb->data, nlen);
1097
                target = virt_to_bus(adapter->dma_buffer);
1098
        }
1099
        adapter->current_dma.skb = skb;
1100
        cli();
1101
        disable_dma(dev->dma);
1102
        clear_dma_ff(dev->dma);
1103
        set_dma_mode(dev->dma, 0x08);   /* dma memory -> io */
1104
        set_dma_addr(dev->dma, target);
1105
        set_dma_count(dev->dma, nlen);
1106
        enable_dma(dev->dma);
1107
        outb_control(inb_control(dev->base_addr) | DMAE | TCEN, dev->base_addr);
1108
        if (elp_debug >= 3)
1109
                printk("%s: DMA transfer started\n", dev->name);
1110
 
1111
        return TRUE;
1112
}
1113
 
1114
/******************************************************
1115
 *
1116
 * start the transmitter
1117
 *    return 0 if sent OK, else return 1
1118
 *
1119
 ******************************************************/
1120
 
1121
static int elp_start_xmit(struct sk_buff *skb, struct device *dev)
1122
{
1123
        if (dev->interrupt) {
1124
                printk("%s: start_xmit aborted (in irq)\n", dev->name);
1125
                return 1;
1126
        }
1127
 
1128
        check_dma(dev);
1129
 
1130
        /*
1131
         * if the transmitter is still busy, we have a transmit timeout...
1132
         */
1133
        if (dev->tbusy) {
1134
                elp_device *adapter = dev->priv;
1135
                int tickssofar = jiffies - dev->trans_start;
1136
                int stat;
1137
 
1138
                if (tickssofar < 1000)
1139
                        return 1;
1140
 
1141
                stat = inb_status(dev->base_addr);
1142
                printk("%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1143
                if (elp_debug >= 1)
1144
                        printk("%s: status %#02x\n", dev->name, stat);
1145
                dev->trans_start = jiffies;
1146
                dev->tbusy = 0;
1147
                adapter->stats.tx_dropped++;
1148
        }
1149
 
1150
        /* Some upper layer thinks we've missed a tx-done interrupt */
1151
        if (skb == NULL) {
1152
                dev_tint(dev);
1153
                return 0;
1154
        }
1155
 
1156
        if (skb->len <= 0)
1157
                return 0;
1158
 
1159
        if (elp_debug >= 3)
1160
                printk("%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1161
 
1162
        if (set_bit(0, (void *) &dev->tbusy)) {
1163
                printk("%s: transmitter access conflict\n", dev->name);
1164
                return 1;
1165
        }
1166
        /*
1167
         * send the packet at skb->data for skb->len
1168
         */
1169
        if (!send_packet(dev, skb)) {
1170
                if (elp_debug >= 2) {
1171
                        printk("%s: failed to transmit packet\n", dev->name);
1172
                }
1173
                dev->tbusy = 0;
1174
                return 1;
1175
        }
1176
        if (elp_debug >= 3)
1177
                printk("%s: packet of length %d sent\n", dev->name, (int) skb->len);
1178
 
1179
        /*
1180
         * start the transmit timeout
1181
         */
1182
        dev->trans_start = jiffies;
1183
 
1184
        prime_rx(dev);
1185
 
1186
        return 0;
1187
}
1188
 
1189
/******************************************************
1190
 *
1191
 * return statistics on the board
1192
 *
1193
 ******************************************************/
1194
 
1195
static struct enet_statistics *elp_get_stats(struct device *dev)
1196
{
1197
        elp_device *adapter = (elp_device *) dev->priv;
1198
 
1199
        if (elp_debug >= 3)
1200
                printk("%s: request for stats\n", dev->name);
1201
 
1202
        /* If the device is closed, just return the latest stats we have,
1203
           - we cannot ask from the adapter without interrupts */
1204
        if (!dev->start)
1205
                return &adapter->stats;
1206
 
1207
        /* send a get statistics command to the board */
1208
        adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1209
        adapter->tx_pcb.length = 0;
1210
        adapter->got[CMD_NETWORK_STATISTICS] = 0;
1211
        if (!send_pcb(dev, &adapter->tx_pcb))
1212
                printk("%s: couldn't send get statistics command\n", dev->name);
1213
        else {
1214
                int timeout = jiffies + TIMEOUT;
1215
                while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && jiffies < timeout);
1216
                if (jiffies >= timeout) {
1217
                        TIMEOUT_MSG(__LINE__);
1218
                        return &adapter->stats;
1219
                }
1220
        }
1221
 
1222
        /* statistics are now up to date */
1223
        return &adapter->stats;
1224
}
1225
 
1226
/******************************************************
1227
 *
1228
 * close the board
1229
 *
1230
 ******************************************************/
1231
 
1232
static int elp_close(struct device *dev)
1233
{
1234
        elp_device *adapter;
1235
 
1236
        adapter = dev->priv;
1237
 
1238
        if (elp_debug >= 3)
1239
                printk("%s: request to close device\n", dev->name);
1240
 
1241
        /* Someone may request the device statistic information even when
1242
         * the interface is closed. The following will update the statistics
1243
         * structure in the driver, so we'll be able to give current statistics.
1244
         */
1245
        (void) elp_get_stats(dev);
1246
 
1247
        /*
1248
         * disable interrupts on the board
1249
         */
1250
        outb_control(0x00, dev->base_addr);
1251
 
1252
        /*
1253
         *  flag transmitter as busy (i.e. not available)
1254
         */
1255
        dev->tbusy = 1;
1256
 
1257
        /*
1258
         *  indicate device is closed
1259
         */
1260
        dev->start = 0;
1261
 
1262
        /*
1263
         * release the IRQ
1264
         */
1265
        free_irq(dev->irq, NULL);
1266
 
1267
        /*
1268
         * and we no longer have to map irq to dev either
1269
         */
1270
        irq2dev_map[dev->irq] = 0;
1271
 
1272
        free_dma(dev->dma);
1273
        free_pages((unsigned long) adapter->dma_buffer, __get_order(DMA_BUFFER_SIZE));
1274
 
1275
        MOD_DEC_USE_COUNT;
1276
 
1277
        return 0;
1278
}
1279
 
1280
 
1281
/************************************************************
1282
 *
1283
 * Set multicast list
1284
 * num_addrs==0: clear mc_list
1285
 * num_addrs==-1: set promiscuous mode
1286
 * num_addrs>0: set mc_list
1287
 *
1288
 ************************************************************/
1289
 
1290
static void elp_set_mc_list(struct device *dev)
1291
{
1292
        elp_device *adapter = (elp_device *) dev->priv;
1293
        struct dev_mc_list *dmi = dev->mc_list;
1294
        int i;
1295
 
1296
        if (elp_debug >= 3)
1297
                printk("%s: request to set multicast list\n", dev->name);
1298
 
1299
        if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1300
                /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1301
                /* if num_addrs==0 the list will be cleared */
1302
                adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1303
                adapter->tx_pcb.length = 6 * dev->mc_count;
1304
                for (i = 0; i < dev->mc_count; i++) {
1305
                        memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1306
                        dmi = dmi->next;
1307
                }
1308
                adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1309
                if (!send_pcb(dev, &adapter->tx_pcb))
1310
                        printk("%s: couldn't send set_multicast command\n", dev->name);
1311
                else {
1312
                        int timeout = jiffies + TIMEOUT;
1313
                        while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && jiffies < timeout);
1314
                        if (jiffies >= timeout) {
1315
                                TIMEOUT_MSG(__LINE__);
1316
                        }
1317
                }
1318
                if (dev->mc_count)
1319
                        adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1320
                else            /* num_addrs == 0 */
1321
                        adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1322
        } else
1323
                adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1324
        /*
1325
         * configure adapter to receive messages (as specified above)
1326
         * and wait for response
1327
         */
1328
        if (elp_debug >= 3)
1329
                printk("%s: sending 82586 configure command\n", dev->name);
1330
        adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1331
        adapter->tx_pcb.length = 2;
1332
        adapter->got[CMD_CONFIGURE_82586] = 0;
1333
        if (!send_pcb(dev, &adapter->tx_pcb))
1334
                printk("%s: couldn't send 82586 configure command\n", dev->name);
1335
        else {
1336
                int timeout = jiffies + TIMEOUT;
1337
                while (adapter->got[CMD_CONFIGURE_82586] == 0 && jiffies < timeout);
1338
                if (jiffies >= timeout)
1339
                        TIMEOUT_MSG(__LINE__);
1340
        }
1341
}
1342
 
1343
/******************************************************
1344
 *
1345
 * initialise Etherlink Plus board
1346
 *
1347
 ******************************************************/
1348
 
1349
static void elp_init(struct device *dev)
1350
{
1351
        elp_device *adapter = dev->priv;
1352
 
1353
        /*
1354
         * set ptrs to various functions
1355
         */
1356
        dev->open = elp_open;   /* local */
1357
        dev->stop = elp_close;  /* local */
1358
        dev->get_stats = elp_get_stats;         /* local */
1359
        dev->hard_start_xmit = elp_start_xmit;  /* local */
1360
        dev->set_multicast_list = elp_set_mc_list;      /* local */
1361
 
1362
        /* Setup the generic properties */
1363
        ether_setup(dev);
1364
 
1365
        /*
1366
         * setup ptr to adapter specific information
1367
         */
1368
        memset(&(adapter->stats), 0, sizeof(struct enet_statistics));
1369
 
1370
        /*
1371
         * memory information
1372
         */
1373
        dev->mem_start = dev->mem_end = dev->rmem_end = dev->rmem_start = 0;
1374
}
1375
 
1376
/************************************************************
1377
 *
1378
 * A couple of tests to see if there's 3C505 or not
1379
 * Called only by elp_autodetect
1380
 ************************************************************/
1381
 
1382
static int elp_sense(struct device *dev)
1383
{
1384
        int timeout;
1385
        int addr = dev->base_addr;
1386
        const char *name = dev->name;
1387
        long flags;
1388
        byte orig_HCR, orig_HSR;
1389
 
1390
        if (check_region(addr, 0xf))
1391
                return -1;
1392
 
1393
        orig_HCR = inb_control(addr);
1394
        orig_HSR = inb_status(addr);
1395
 
1396
        if (elp_debug > 0)
1397
                printk(search_msg, name, addr);
1398
 
1399
        if (((orig_HCR == 0xff) && (orig_HSR == 0xff)) ||
1400
            ((orig_HCR & DIR) != (orig_HSR & DIR))) {
1401
                if (elp_debug > 0)
1402
                        printk(notfound_msg, 1);
1403
                return -1;      /* It can't be 3c505 if HCR.DIR != HSR.DIR */
1404
        }
1405
        /* Enable interrupts - we need timers! */
1406
        save_flags(flags);
1407
        sti();
1408
 
1409
        /* Wait for a while; the adapter may still be booting up */
1410
        if (elp_debug > 0)
1411
                printk(stilllooking_msg);
1412
        if (orig_HCR & DIR) {
1413
                /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1414
                outb_control(orig_HCR & ~DIR, addr);
1415
                timeout = jiffies + 30;
1416
                while (jiffies < timeout);
1417
                restore_flags(flags);
1418
                if (inb_status(addr) & DIR) {
1419
                        outb_control(orig_HCR, addr);
1420
                        if (elp_debug > 0)
1421
                                printk(notfound_msg, 2);
1422
                        return -1;
1423
                }
1424
        } else {
1425
                /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1426
                outb_control(orig_HCR | DIR, addr);
1427
                timeout = jiffies + 30;
1428
                while (jiffies < timeout);
1429
                restore_flags(flags);
1430
                if (!(inb_status(addr) & DIR)) {
1431
                        outb_control(orig_HCR, addr);
1432
                        if (elp_debug > 0)
1433
                                printk(notfound_msg, 3);
1434
                        return -1;
1435
                }
1436
        }
1437
        /*
1438
         * It certainly looks like a 3c505. If it has DMA enabled, it needs
1439
         * a hard reset. Also, do a hard reset if selected at the compile time.
1440
         */
1441
        if (elp_debug > 0)
1442
                printk(found_msg);
1443
 
1444
        return 0;
1445
}
1446
 
1447
/*************************************************************
1448
 *
1449
 * Search through addr_list[] and try to find a 3C505
1450
 * Called only by eplus_probe
1451
 *************************************************************/
1452
 
1453
static int elp_autodetect(struct device *dev)
1454
{
1455
        int idx = 0;
1456
 
1457
        /* if base address set, then only check that address
1458
           otherwise, run through the table */
1459
        if (dev->base_addr != 0) {       /* dev->base_addr == 0 ==> plain autodetect */
1460
                if (elp_sense(dev) == 0)
1461
                        return dev->base_addr;
1462
        } else
1463
                while ((dev->base_addr = addr_list[idx++])) {
1464
                        if (elp_sense(dev) == 0)
1465
                                return dev->base_addr;
1466
                }
1467
 
1468
        /* could not find an adapter */
1469
        if (elp_debug > 0)
1470
                printk(couldnot_msg, dev->name);
1471
 
1472
        return 0;                /* Because of this, the layer above will return -ENODEV */
1473
}
1474
 
1475
 
1476
/******************************************************
1477
 *
1478
 * probe for an Etherlink Plus board at the specified address
1479
 *
1480
 ******************************************************/
1481
 
1482
/* There are three situations we need to be able to detect here:
1483
 
1484
 *  a) the card is idle
1485
 *  b) the card is still booting up
1486
 *  c) the card is stuck in a strange state (some DOS drivers do this)
1487
 *
1488
 * In case (a), all is well.  In case (b), we wait 10 seconds to see if the
1489
 * card finishes booting, and carry on if so.  In case (c), we do a hard reset,
1490
 * loop round, and hope for the best.
1491
 *
1492
 * This is all very unpleasant, but hopefully avoids the problems with the old
1493
 * probe code (which had a 15-second delay if the card was idle, and didn't
1494
 * work at all if it was in a weird state).
1495
 */
1496
 
1497
int elplus_probe(struct device *dev)
1498
{
1499
        elp_device *adapter;
1500
        int i, tries, tries1, timeout, okay;
1501
 
1502
        /*
1503
         *  setup adapter structure
1504
         */
1505
 
1506
        dev->base_addr = elp_autodetect(dev);
1507
        if (!(dev->base_addr))
1508
                return -ENODEV;
1509
 
1510
        /*
1511
         * setup ptr to adapter specific information
1512
         */
1513
        adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1514
        if (adapter == NULL) {
1515
                printk("%s: out of memory\n", dev->name);
1516
                return -ENODEV;
1517
        }
1518
 
1519
        for (tries1 = 0; tries1 < 3; tries1++) {
1520
                outb_control((inb_control(dev->base_addr) | CMDE) & ~DIR, dev->base_addr);
1521
                /* First try to write just one byte, to see if the card is
1522
                 * responding at all normally.
1523
                 */
1524
                timeout = jiffies + 5;
1525
                okay = 0;
1526
                while (jiffies < timeout && !(inb_status(dev->base_addr) & HCRE));
1527
                if ((inb_status(dev->base_addr) & HCRE)) {
1528
                        outb_command(0, dev->base_addr); /* send a spurious byte */
1529
                        timeout = jiffies + 5;
1530
                        while (jiffies < timeout && !(inb_status(dev->base_addr) & HCRE));
1531
                        if (inb_status(dev->base_addr) & HCRE)
1532
                                okay = 1;
1533
                }
1534
                if (!okay) {
1535
                        /* Nope, it's ignoring the command register.  This means that
1536
                         * either it's still booting up, or it's died.
1537
                         */
1538
                        printk("%s: command register wouldn't drain, ", dev->name);
1539
                        if ((inb_status(dev->base_addr) & 7) == 3) {
1540
                                /* If the adapter status is 3, it *could* still be booting.
1541
                                 * Give it the benefit of the doubt for 10 seconds.
1542
                                 */
1543
                                printk("assuming 3c505 still starting\n");
1544
                                timeout = jiffies + 10 * HZ;
1545
                                while (jiffies < timeout && (inb_status(dev->base_addr) & 7));
1546
                                if (inb_status(dev->base_addr) & 7) {
1547
                                        printk("%s: 3c505 failed to start\n", dev->name);
1548
                                } else {
1549
                                        okay = 1;  /* It started */
1550
                                }
1551
                        } else {
1552
                                /* Otherwise, it must just be in a strange state.  We probably
1553
                                 * need to kick it.
1554
                                 */
1555
                                printk("3c505 is sulking\n");
1556
                        }
1557
                }
1558
                for (tries = 0; tries < 5 && okay; tries++) {
1559
 
1560
                        /*
1561
                         * Try to set the Ethernet address, to make sure that the board
1562
                         * is working.
1563
                         */
1564
                        adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1565
                        adapter->tx_pcb.length = 0;
1566
                        autoirq_setup(0);
1567
                        if (!send_pcb(dev, &adapter->tx_pcb)) {
1568
                                printk("%s: could not send first PCB\n", dev->name);
1569
                                autoirq_report(0);
1570
                                continue;
1571
                        }
1572
                        if (!receive_pcb(dev, &adapter->rx_pcb)) {
1573
                                printk("%s: could not read first PCB\n", dev->name);
1574
                                autoirq_report(0);
1575
                                continue;
1576
                        }
1577
                        if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1578
                            (adapter->rx_pcb.length != 6)) {
1579
                                printk("%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1580
                                autoirq_report(0);
1581
                                continue;
1582
                        }
1583
                        goto okay;
1584
                }
1585
                /* It's broken.  Do a hard reset to re-initialise the board,
1586
                 * and try again.
1587
                 */
1588
                printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1589
                outb_control(inb_control(dev->base_addr) | FLSH | ATTN, dev->base_addr);
1590
                outb_control(inb_control(dev->base_addr) & ~(FLSH | ATTN), dev->base_addr);
1591
        }
1592
        printk("%s: failed to initialise 3c505\n", dev->name);
1593
        return -ENODEV;
1594
 
1595
      okay:
1596
        if (dev->irq) {         /* Is there a preset IRQ? */
1597
                int rpt = autoirq_report(0);
1598
                if (dev->irq != rpt) {
1599
                        printk("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1600
                        return -ENODEV;
1601
                }
1602
                /* if dev->irq == autoirq_report(0), all is well */
1603
        } else                  /* No preset IRQ; just use what we can detect */
1604
                dev->irq = autoirq_report(0);
1605
        switch (dev->irq) {     /* Legal, sane? */
1606
        case 0:
1607
                printk("%s: No IRQ reported by autoirq_report().\n", dev->name);
1608
                printk("%s: Check the jumpers of your 3c505 board.\n", dev->name);
1609
                return -ENODEV;
1610
        case 1:
1611
        case 6:
1612
        case 8:
1613
        case 13:
1614
                printk("%s: Impossible IRQ %d reported by autoirq_report().\n",
1615
                       dev->name, dev->irq);
1616
                return -ENODEV;
1617
        }
1618
        /*
1619
         *  Now we have the IRQ number so we can disable the interrupts from
1620
         *  the board until the board is opened.
1621
         */
1622
        outb_control(inb_control(dev->base_addr) & ~CMDE, dev->base_addr);
1623
 
1624
        /*
1625
         * copy ethernet address into structure
1626
         */
1627
        for (i = 0; i < 6; i++)
1628
                dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1629
 
1630
        /* set up the DMA channel */
1631
        dev->dma = ELP_DMA;
1632
 
1633
        /*
1634
         * print remainder of startup message
1635
         */
1636
        printk("%s: 3c505 at %#lx, irq %d, dma %d, ",
1637
               dev->name, dev->base_addr, dev->irq, dev->dma);
1638
        printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1639
               dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1640
               dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1641
 
1642
        /*
1643
         * read more information from the adapter
1644
         */
1645
 
1646
        adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1647
        adapter->tx_pcb.length = 0;
1648
        if (!send_pcb(dev, &adapter->tx_pcb) ||
1649
            !receive_pcb(dev, &adapter->rx_pcb) ||
1650
            (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1651
            (adapter->rx_pcb.length != 10)) {
1652
                printk("%s: not responding to second PCB\n", dev->name);
1653
        }
1654
        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);
1655
 
1656
        /*
1657
         * reconfigure the adapter memory to better suit our purposes
1658
         */
1659
        adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1660
        adapter->tx_pcb.length = 12;
1661
        adapter->tx_pcb.data.memconf.cmd_q = 8;
1662
        adapter->tx_pcb.data.memconf.rcv_q = 8;
1663
        adapter->tx_pcb.data.memconf.mcast = 10;
1664
        adapter->tx_pcb.data.memconf.frame = 10;
1665
        adapter->tx_pcb.data.memconf.rcv_b = 10;
1666
        adapter->tx_pcb.data.memconf.progs = 0;
1667
        if (!send_pcb(dev, &adapter->tx_pcb) ||
1668
            !receive_pcb(dev, &adapter->rx_pcb) ||
1669
            (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1670
            (adapter->rx_pcb.length != 2)) {
1671
                printk("%s: could not configure adapter memory\n", dev->name);
1672
        }
1673
        if (adapter->rx_pcb.data.configure) {
1674
                printk("%s: adapter configuration failed\n", dev->name);
1675
        }
1676
        /*
1677
         * and reserve the address region
1678
         */
1679
        request_region(dev->base_addr, ELP_IO_EXTENT, "3c505");
1680
 
1681
        /*
1682
         * initialise the device
1683
         */
1684
        elp_init(dev);
1685
 
1686
        return 0;
1687
}
1688
 
1689
#ifdef MODULE
1690
static char devicename[9] = {0,};
1691
static struct device dev_3c505 =
1692
{
1693
        devicename,             /* device name is inserted by linux/drivers/net/net_init.c */
1694
        0, 0, 0, 0,
1695
        0, 0,
1696
        0, 0, 0, NULL, elplus_probe};
1697
 
1698
int io = 0x300;
1699
int irq = 0;
1700
 
1701
int init_module(void)
1702
{
1703
        if (io == 0)
1704
                printk("3c505: You should not use auto-probing with insmod!\n");
1705
        dev_3c505.base_addr = io;
1706
        dev_3c505.irq = irq;
1707
        if (register_netdev(&dev_3c505) != 0) {
1708
                return -EIO;
1709
        }
1710
        return 0;
1711
}
1712
 
1713
void cleanup_module(void)
1714
{
1715
        unregister_netdev(&dev_3c505);
1716
        kfree(dev_3c505.priv);
1717
        dev_3c505.priv = NULL;
1718
 
1719
        /* If we don't do this, we can't re-insmod it later. */
1720
        release_region(dev_3c505.base_addr, ELP_IO_EXTENT);
1721
}
1722
 
1723
#endif                          /* MODULE */
1724
 
1725
 
1726
/*
1727
 * Local Variables:
1728
 *  c-file-style: "linux"
1729
 *  tab-width: 8
1730
 *  compile-command: "gcc -D__KERNEL__ -I/discs/bibble/src/linux-1.3.69/include  -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strength-reduce -pipe -m486 -DCPU=486 -DMODULE  -c 3c505.c"
1731
 * End:
1732
 */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.