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