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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [net/] [3c505.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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