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