linux-bk/drivers/net/3c501.c
<<
>>
Prefs
   1/* 3c501.c: A 3Com 3c501 Ethernet driver for Linux. */
   2/*
   3    Written 1992,1993,1994  Donald Becker
   4
   5    Copyright 1993 United States Government as represented by the
   6    Director, National Security Agency.  This software may be used and
   7    distributed according to the terms of the GNU General Public License,
   8    incorporated herein by reference.
   9
  10    This is a device driver for the 3Com Etherlink 3c501.
  11    Do not purchase this card, even as a joke.  It's performance is horrible,
  12    and it breaks in many ways.
  13
  14    The original author may be reached as becker@scyld.com, or C/O
  15        Scyld Computing Corporation
  16        410 Severn Ave., Suite 210
  17        Annapolis MD 21403
  18
  19    Fixed (again!) the missing interrupt locking on TX/RX shifting.
  20                Alan Cox <Alan.Cox@linux.org>
  21
  22    Removed calls to init_etherdev since they are no longer needed, and
  23    cleaned up modularization just a bit. The driver still allows only
  24    the default address for cards when loaded as a module, but that's
  25    really less braindead than anyone using a 3c501 board. :)
  26                    19950208 (invid@msen.com)
  27
  28    Added traps for interrupts hitting the window as we clear and TX load
  29    the board. Now getting 150K/second FTP with a 3c501 card. Still playing
  30    with a TX-TX optimisation to see if we can touch 180-200K/second as seems
  31    theoretically maximum.
  32                19950402 Alan Cox <Alan.Cox@linux.org>
  33                
  34    Cleaned up for 2.3.x because we broke SMP now. 
  35                20000208 Alan Cox <alan@redhat.com>
  36
  37    Check up pass for 2.5. Nothing significant changed
  38                20021009 Alan Cox <alan@redhat.com>
  39
  40    Fixed zero fill corner case 
  41                20030104 Alan Cox <alan@redhat.com>
  42                
  43                
  44   For the avoidance of doubt the "preferred form" of this code is one which
  45   is in an open non patent encumbered format. Where cryptographic key signing
  46   forms part of the process of creating an executable the information
  47   including keys needed to generate an equivalently functional executable
  48   are deemed to be part of the source code.
  49
  50*/
  51
  52
  53/**
  54 * DOC: 3c501 Card Notes
  55 *
  56 *  Some notes on this thing if you have to hack it.  [Alan]
  57 *
  58 *  Some documentation is available from 3Com. Due to the boards age
  59 *  standard responses when you ask for this will range from 'be serious'
  60 *  to 'give it to a museum'. The documentation is incomplete and mostly
  61 *  of historical interest anyway. 
  62 *
  63 *  The basic system is a single buffer which can be used to receive or
  64 *  transmit a packet. A third command mode exists when you are setting
  65 *  things up.
  66 *
  67 *  If it's transmitting it's not receiving and vice versa. In fact the
  68 *  time to get the board back into useful state after an operation is
  69 *  quite large.
  70 *
  71 *  The driver works by keeping the board in receive mode waiting for a
  72 *  packet to arrive. When one arrives it is copied out of the buffer
  73 *  and delivered to the kernel. The card is reloaded and off we go.
  74 *
  75 *  When transmitting lp->txing is set and the card is reset (from
  76 *  receive mode) [possibly losing a packet just received] to command
  77 *  mode. A packet is loaded and transmit mode triggered. The interrupt
  78 *  handler runs different code for transmit interrupts and can handle
  79 *  returning to receive mode or retransmissions (yes you have to help
  80 *  out with those too).
  81 *
  82 * DOC: Problems
  83 *  
  84 *  There are a wide variety of undocumented error returns from the card
  85 *  and you basically have to kick the board and pray if they turn up. Most
  86 *  only occur under extreme load or if you do something the board doesn't
  87 *  like (eg touching a register at the wrong time).
  88 *
  89 *  The driver is less efficient than it could be. It switches through
  90 *  receive mode even if more transmits are queued. If this worries you buy
  91 *  a real Ethernet card.
  92 *
  93 *  The combination of slow receive restart and no real multicast
  94 *  filter makes the board unusable with a kernel compiled for IP
  95 *  multicasting in a real multicast environment. That's down to the board,
  96 *  but even with no multicast programs running a multicast IP kernel is
  97 *  in group 224.0.0.1 and you will therefore be listening to all multicasts.
  98 *  One nv conference running over that Ethernet and you can give up.
  99 *
 100 */
 101
 102#define DRV_NAME        "3c501"
 103#define DRV_VERSION     "2002/10/09"
 104
 105
 106static const char version[] =
 107        DRV_NAME ".c: " DRV_VERSION " Alan Cox (alan@redhat.com).\n";
 108
 109/*
 110 *      Braindamage remaining:
 111 *      The 3c501 board.
 112 */
 113
 114#include <linux/module.h>
 115
 116#include <linux/kernel.h>
 117#include <linux/fcntl.h>
 118#include <linux/ioport.h>
 119#include <linux/interrupt.h>
 120#include <linux/slab.h>
 121#include <linux/string.h>
 122#include <linux/errno.h>
 123#include <linux/config.h>       /* for CONFIG_IP_MULTICAST */
 124#include <linux/spinlock.h>
 125#include <linux/ethtool.h>
 126#include <linux/delay.h>
 127
 128#include <asm/uaccess.h>
 129#include <asm/bitops.h>
 130#include <asm/io.h>
 131
 132#include <linux/netdevice.h>
 133#include <linux/etherdevice.h>
 134#include <linux/skbuff.h>
 135#include <linux/init.h>
 136
 137#include "3c501.h"
 138
 139/* A zero-terminated list of I/O addresses to be probed.
 140   The 3c501 can be at many locations, but here are the popular ones. */
 141static unsigned int netcard_portlist[] __initdata = { 
 142        0x280, 0x300, 0
 143};
 144
 145
 146/*
 147 *      The boilerplate probe code.
 148 */
 149
 150/**
 151 * el1_probe:           -       probe for a 3c501
 152 * @dev: The device structure passed in to probe. 
 153 *
 154 * This can be called from two places. The network layer will probe using
 155 * a device structure passed in with the probe information completed. For a
 156 * modular driver we use #init_module to fill in our own structure and probe
 157 * for it.
 158 *
 159 * Returns 0 on success. ENXIO if asked not to probe and ENODEV if asked to
 160 * probe and failing to find anything.
 161 */
 162 
 163int __init el1_probe(struct net_device *dev)
 164{
 165        int i;
 166        int base_addr = dev->base_addr;
 167
 168        SET_MODULE_OWNER(dev);
 169
 170        if (base_addr > 0x1ff)  /* Check a single specified location. */
 171                return el1_probe1(dev, base_addr);
 172        else if (base_addr != 0)        /* Don't probe at all. */
 173                return -ENXIO;
 174
 175        for (i = 0; netcard_portlist[i]; i++)
 176                if (el1_probe1(dev, netcard_portlist[i]) == 0)
 177                        return 0;
 178
 179        return -ENODEV;
 180}
 181
 182/**
 183 *      el1_probe1: 
 184 *      @dev: The device structure to use
 185 *      @ioaddr: An I/O address to probe at.
 186 *
 187 *      The actual probe. This is iterated over by #el1_probe in order to
 188 *      check all the applicable device locations.
 189 *
 190 *      Returns 0 for a success, in which case the device is activated,
 191 *      EAGAIN if the IRQ is in use by another driver, and ENODEV if the
 192 *      board cannot be found.
 193 */
 194
 195static int __init el1_probe1(struct net_device *dev, int ioaddr)
 196{
 197        struct net_local *lp;
 198        const char *mname;              /* Vendor name */
 199        unsigned char station_addr[6];
 200        int autoirq = 0;
 201        int i;
 202
 203        /*
 204         *      Reserve I/O resource for exclusive use by this driver
 205         */
 206
 207        if (!request_region(ioaddr, EL1_IO_EXTENT, dev->name))
 208                return -ENODEV;
 209
 210        /*
 211         *      Read the station address PROM data from the special port.
 212         */
 213
 214        for (i = 0; i < 6; i++)
 215        {
 216                outw(i, ioaddr + EL1_DATAPTR);
 217                station_addr[i] = inb(ioaddr + EL1_SAPROM);
 218        }
 219        /*
 220         *      Check the first three octets of the S.A. for 3Com's prefix, or
 221         *      for the Sager NP943 prefix.
 222         */
 223
 224        if (station_addr[0] == 0x02  &&  station_addr[1] == 0x60
 225                && station_addr[2] == 0x8c)
 226        {
 227                mname = "3c501";
 228        } else if (station_addr[0] == 0x00  &&  station_addr[1] == 0x80
 229        && station_addr[2] == 0xC8)
 230        {
 231                mname = "NP943";
 232        }
 233        else {
 234                release_region(ioaddr, EL1_IO_EXTENT);
 235                return -ENODEV;
 236        }
 237
 238        /*
 239         *      We auto-IRQ by shutting off the interrupt line and letting it float
 240         *      high.
 241         */
 242
 243        if (dev->irq < 2)
 244        {
 245                unsigned long irq_mask;
 246
 247                irq_mask = probe_irq_on();
 248                inb(RX_STATUS);         /* Clear pending interrupts. */
 249                inb(TX_STATUS);
 250                outb(AX_LOOP + 1, AX_CMD);
 251
 252                outb(0x00, AX_CMD);
 253
 254                mdelay(20);
 255                autoirq = probe_irq_off(irq_mask);
 256
 257                if (autoirq == 0)
 258                {
 259                        printk(KERN_WARNING "%s probe at %#x failed to detect IRQ line.\n",
 260                                mname, ioaddr);
 261                        release_region(ioaddr, EL1_IO_EXTENT);
 262                        return -EAGAIN;
 263                }
 264        }
 265
 266        outb(AX_RESET+AX_LOOP, AX_CMD);                 /* Loopback mode. */
 267        dev->base_addr = ioaddr;
 268        memcpy(dev->dev_addr, station_addr, ETH_ALEN);
 269
 270        if (dev->mem_start & 0xf)
 271                el_debug = dev->mem_start & 0x7;
 272        if (autoirq)
 273                dev->irq = autoirq;
 274
 275        printk(KERN_INFO "%s: %s EtherLink at %#lx, using %sIRQ %d.\n", dev->name, mname, dev->base_addr,
 276                        autoirq ? "auto":"assigned ", dev->irq);
 277
 278#ifdef CONFIG_IP_MULTICAST
 279        printk(KERN_WARNING "WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
 280#endif
 281
 282        if (el_debug)
 283                printk(KERN_DEBUG "%s", version);
 284
 285        /*
 286         *      Initialize the device structure.
 287         */
 288
 289        dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
 290        if (dev->priv == NULL) {
 291                release_region(ioaddr, EL1_IO_EXTENT);
 292                return -ENOMEM;
 293        }
 294        memset(dev->priv, 0, sizeof(struct net_local));
 295
 296        lp=dev->priv;
 297        spin_lock_init(&lp->lock);
 298        
 299        /*
 300         *      The EL1-specific entries in the device structure.
 301         */
 302
 303        dev->open = &el_open;
 304        dev->hard_start_xmit = &el_start_xmit;
 305        dev->tx_timeout = &el_timeout;
 306        dev->watchdog_timeo = HZ;
 307        dev->stop = &el1_close;
 308        dev->get_stats = &el1_get_stats;
 309        dev->set_multicast_list = &set_multicast_list;
 310        dev->ethtool_ops = &netdev_ethtool_ops;
 311
 312        /*
 313         *      Setup the generic properties
 314         */
 315
 316        ether_setup(dev);
 317
 318        return 0;
 319}
 320
 321/**
 322 *      el1_open:
 323 *      @dev: device that is being opened
 324 *
 325 *      When an ifconfig is issued which changes the device flags to include
 326 *      IFF_UP this function is called. It is only called when the change 
 327 *      occurs, not when the interface remains up. #el1_close will be called
 328 *      when it goes down.
 329 *
 330 *      Returns 0 for a successful open, or -EAGAIN if someone has run off
 331 *      with our interrupt line.
 332 */
 333
 334static int el_open(struct net_device *dev)
 335{
 336        int retval;
 337        int ioaddr = dev->base_addr;
 338        struct net_local *lp = (struct net_local *)dev->priv;
 339        unsigned long flags;
 340
 341        if (el_debug > 2)
 342                printk(KERN_DEBUG "%s: Doing el_open()...", dev->name);
 343
 344        if ((retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev)))
 345                return retval;
 346
 347        spin_lock_irqsave(&lp->lock, flags);
 348        el_reset(dev);
 349        spin_unlock_irqrestore(&lp->lock, flags);
 350
 351        lp->txing = 0;          /* Board in RX mode */
 352        outb(AX_RX, AX_CMD);    /* Aux control, irq and receive enabled */
 353        netif_start_queue(dev);
 354        return 0;
 355}
 356
 357/**
 358 * el_timeout:
 359 * @dev: The 3c501 card that has timed out
 360 *
 361 * Attempt to restart the board. This is basically a mixture of extreme
 362 * violence and prayer
 363 *
 364 */
 365 
 366static void el_timeout(struct net_device *dev)
 367{
 368        struct net_local *lp = (struct net_local *)dev->priv;
 369        int ioaddr = dev->base_addr;
 370 
 371        if (el_debug)
 372                printk (KERN_DEBUG "%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
 373                        dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
 374        lp->stats.tx_errors++;
 375        outb(TX_NORM, TX_CMD);
 376        outb(RX_NORM, RX_CMD);
 377        outb(AX_OFF, AX_CMD);   /* Just trigger a false interrupt. */
 378        outb(AX_RX, AX_CMD);    /* Aux control, irq and receive enabled */
 379        lp->txing = 0;          /* Ripped back in to RX */
 380        netif_wake_queue(dev);
 381}
 382
 383 
 384/**
 385 * el_start_xmit:
 386 * @skb: The packet that is queued to be sent
 387 * @dev: The 3c501 card we want to throw it down
 388 *
 389 * Attempt to send a packet to a 3c501 card. There are some interesting
 390 * catches here because the 3c501 is an extremely old and therefore
 391 * stupid piece of technology.
 392 *
 393 * If we are handling an interrupt on the other CPU we cannot load a packet
 394 * as we may still be attempting to retrieve the last RX packet buffer.
 395 *
 396 * When a transmit times out we dump the card into control mode and just
 397 * start again. It happens enough that it isnt worth logging.
 398 *
 399 * We avoid holding the spin locks when doing the packet load to the board.
 400 * The device is very slow, and its DMA mode is even slower. If we held the
 401 * lock while loading 1500 bytes onto the controller we would drop a lot of
 402 * serial port characters. This requires we do extra locking, but we have
 403 * no real choice.
 404 */
 405
 406static int el_start_xmit(struct sk_buff *skb, struct net_device *dev)
 407{
 408        struct net_local *lp = (struct net_local *)dev->priv;
 409        int ioaddr = dev->base_addr;
 410        unsigned long flags;
 411
 412        /*
 413         *      Avoid incoming interrupts between us flipping txing and flipping
 414         *      mode as the driver assumes txing is a faithful indicator of card
 415         *      state
 416         */
 417
 418        spin_lock_irqsave(&lp->lock, flags);
 419        
 420        /*
 421         *      Avoid timer-based retransmission conflicts.
 422         */
 423
 424        netif_stop_queue(dev);
 425
 426        do
 427        {
 428                int len = skb->len;
 429                int pad = 0;
 430                int gp_start;
 431                unsigned char *buf = skb->data;
 432                
 433                if (len < ETH_ZLEN)
 434                        pad = ETH_ZLEN - len;
 435                        
 436                gp_start = 0x800 - ( len + pad );
 437
 438                lp->tx_pkt_start = gp_start;
 439                lp->collisions = 0;
 440
 441                lp->stats.tx_bytes += skb->len;
 442
 443                /*
 444                 *      Command mode with status cleared should [in theory]
 445                 *      mean no more interrupts can be pending on the card.
 446                 */
 447
 448                outb_p(AX_SYS, AX_CMD);
 449                inb_p(RX_STATUS);
 450                inb_p(TX_STATUS);
 451
 452                lp->loading = 1;
 453                lp->txing = 1;
 454
 455                /*
 456                 *      Turn interrupts back on while we spend a pleasant afternoon
 457                 *      loading bytes into the board
 458                 */
 459
 460                spin_unlock_irqrestore(&lp->lock, flags);
 461                
 462                outw(0x00, RX_BUF_CLR);         /* Set rx packet area to 0. */
 463                outw(gp_start, GP_LOW);         /* aim - packet will be loaded into buffer start */
 464                outsb(DATAPORT,buf,len);        /* load buffer (usual thing each byte increments the pointer) */
 465                if (pad) {
 466                        while(pad--)            /* Zero fill buffer tail */
 467                                outb(0, DATAPORT);
 468                }
 469                outw(gp_start, GP_LOW);         /* the board reuses the same register */
 470        
 471                if(lp->loading != 2)
 472                {
 473                        outb(AX_XMIT, AX_CMD);          /* fire ... Trigger xmit.  */
 474                        lp->loading=0;
 475                        dev->trans_start = jiffies;
 476                        if (el_debug > 2)
 477                                printk(KERN_DEBUG " queued xmit.\n");
 478                        dev_kfree_skb (skb);
 479                        return 0;
 480                }
 481                /* A receive upset our load, despite our best efforts */
 482                if(el_debug>2)
 483                        printk(KERN_DEBUG "%s: burped during tx load.\n", dev->name);
 484                spin_lock_irqsave(&lp->lock, flags);
 485        }
 486        while(1);
 487
 488}
 489
 490/**
 491 * el_interrupt:
 492 * @irq: Interrupt number
 493 * @dev_id: The 3c501 that burped
 494 * @regs: Register data (surplus to our requirements)
 495 *
 496 * Handle the ether interface interrupts. The 3c501 needs a lot more 
 497 * hand holding than most cards. In particular we get a transmit interrupt
 498 * with a collision error because the board firmware isnt capable of rewinding
 499 * its own transmit buffer pointers. It can however count to 16 for us.
 500 *
 501 * On the receive side the card is also very dumb. It has no buffering to
 502 * speak of. We simply pull the packet out of its PIO buffer (which is slow)
 503 * and queue it for the kernel. Then we reset the card for the next packet.
 504 *
 505 * We sometimes get suprise interrupts late both because the SMP IRQ delivery
 506 * is message passing and because the card sometimes seems to deliver late. I
 507 * think if it is part way through a receive and the mode is changed it carries
 508 * on receiving and sends us an interrupt. We have to band aid all these cases
 509 * to get a sensible 150kbytes/second performance. Even then you want a small
 510 * TCP window.
 511 */
 512
 513static irqreturn_t el_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 514{
 515        struct net_device *dev = dev_id;
 516        struct net_local *lp;
 517        int ioaddr;
 518        int axsr;                       /* Aux. status reg. */
 519
 520        ioaddr = dev->base_addr;
 521        lp = (struct net_local *)dev->priv;
 522
 523        spin_lock(&lp->lock);
 524        
 525        /*
 526         *      What happened ?
 527         */
 528
 529        axsr = inb(AX_STATUS);
 530
 531        /*
 532         *      Log it
 533         */
 534
 535        if (el_debug > 3)
 536                printk(KERN_DEBUG "%s: el_interrupt() aux=%#02x", dev->name, axsr);
 537
 538        if(lp->loading==1 && !lp->txing)
 539                printk(KERN_WARNING "%s: Inconsistent state loading while not in tx\n",
 540                        dev->name);
 541
 542        if (lp->txing)
 543        {
 544
 545                /*
 546                 *      Board in transmit mode. May be loading. If we are
 547                 *      loading we shouldn't have got this.
 548                 */
 549
 550                int txsr = inb(TX_STATUS);
 551
 552                if(lp->loading==1)
 553                {
 554                        if(el_debug > 2)
 555                        {
 556                                printk(KERN_DEBUG "%s: Interrupt while loading [", dev->name);
 557                                printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x]\n", txsr, inw(GP_LOW),inw(RX_LOW));
 558                        }
 559                        lp->loading=2;          /* Force a reload */
 560                        spin_unlock(&lp->lock);
 561                        goto out;
 562                }
 563
 564                if (el_debug > 6)
 565                        printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),inw(RX_LOW));
 566
 567                if ((axsr & 0x80) && (txsr & TX_READY) == 0)
 568                {
 569                        /*
 570                         *      FIXME: is there a logic to whether to keep on trying or
 571                         *      reset immediately ?
 572                         */
 573                        if(el_debug>1)
 574                                printk(KERN_DEBUG "%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
 575                                        " gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
 576                        inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
 577                        lp->txing = 0;
 578                        netif_wake_queue(dev);
 579                }
 580                else if (txsr & TX_16COLLISIONS)
 581                {
 582                        /*
 583                         *      Timed out
 584                         */
 585                        if (el_debug)
 586                                printk (KERN_DEBUG "%s: Transmit failed 16 times, Ethernet jammed?\n",dev->name);
 587                        outb(AX_SYS, AX_CMD);
 588                        lp->txing = 0;
 589                        lp->stats.tx_aborted_errors++;
 590                        netif_wake_queue(dev);
 591                }
 592                else if (txsr & TX_COLLISION)
 593                {
 594                        /*
 595                         *      Retrigger xmit.
 596                         */
 597
 598                        if (el_debug > 6)
 599                                printk(KERN_DEBUG " retransmitting after a collision.\n");
 600                        /*
 601                         *      Poor little chip can't reset its own start pointer
 602                         */
 603
 604                        outb(AX_SYS, AX_CMD);
 605                        outw(lp->tx_pkt_start, GP_LOW);
 606                        outb(AX_XMIT, AX_CMD);
 607                        lp->stats.collisions++;
 608                        spin_unlock(&lp->lock);
 609                        goto out;
 610                }
 611                else
 612                {
 613                        /*
 614                         *      It worked.. we will now fall through and receive
 615                         */
 616                        lp->stats.tx_packets++;
 617                        if (el_debug > 6)
 618                                printk(KERN_DEBUG " Tx succeeded %s\n",
 619                                        (txsr & TX_RDY) ? "." : "but tx is busy!");
 620                        /*
 621                         *      This is safe the interrupt is atomic WRT itself.
 622                         */
 623
 624                        lp->txing = 0;
 625                        netif_wake_queue(dev);  /* In case more to transmit */
 626                }
 627        }
 628        else
 629        {
 630                /*
 631                 *      In receive mode.
 632                 */
 633
 634                int rxsr = inb(RX_STATUS);
 635                if (el_debug > 5)
 636                        printk(KERN_DEBUG " rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),inw(RX_LOW));
 637                /*
 638                 *      Just reading rx_status fixes most errors.
 639                 */
 640                if (rxsr & RX_MISSED)
 641                        lp->stats.rx_missed_errors++;
 642                else if (rxsr & RX_RUNT)
 643                {       /* Handled to avoid board lock-up. */
 644                        lp->stats.rx_length_errors++;
 645                        if (el_debug > 5)
 646                                printk(KERN_DEBUG " runt.\n");
 647                }
 648                else if (rxsr & RX_GOOD)
 649                {
 650                        /*
 651                         *      Receive worked.
 652                         */
 653                        el_receive(dev);
 654                }
 655                else
 656                {
 657                        /*
 658                         *      Nothing?  Something is broken!
 659                         */
 660                        if (el_debug > 2)
 661                                printk(KERN_DEBUG "%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
 662                                        dev->name, rxsr);
 663                        el_reset(dev);
 664                }
 665                if (el_debug > 3)
 666                        printk(KERN_DEBUG ".\n");
 667        }
 668
 669        /*
 670         *      Move into receive mode
 671         */
 672
 673        outb(AX_RX, AX_CMD);
 674        outw(0x00, RX_BUF_CLR);
 675        inb(RX_STATUS);         /* Be certain that interrupts are cleared. */
 676        inb(TX_STATUS);
 677        spin_unlock(&lp->lock);
 678out:
 679        return IRQ_HANDLED;
 680}
 681
 682
 683/**
 684 * el_receive:
 685 * @dev: Device to pull the packets from
 686 *
 687 * We have a good packet. Well, not really "good", just mostly not broken.
 688 * We must check everything to see if it is good. In particular we occasionally
 689 * get wild packet sizes from the card. If the packet seems sane we PIO it
 690 * off the card and queue it for the protocol layers.
 691 */
 692
 693static void el_receive(struct net_device *dev)
 694{
 695        struct net_local *lp = (struct net_local *)dev->priv;
 696        int ioaddr = dev->base_addr;
 697        int pkt_len;
 698        struct sk_buff *skb;
 699
 700        pkt_len = inw(RX_LOW);
 701
 702        if (el_debug > 4)
 703                printk(KERN_DEBUG " el_receive %d.\n", pkt_len);
 704
 705        if ((pkt_len < 60)  ||  (pkt_len > 1536))
 706        {
 707                if (el_debug)
 708                        printk(KERN_DEBUG "%s: bogus packet, length=%d\n", dev->name, pkt_len);
 709                lp->stats.rx_over_errors++;
 710                return;
 711        }
 712
 713        /*
 714         *      Command mode so we can empty the buffer
 715         */
 716
 717        outb(AX_SYS, AX_CMD);
 718        skb = dev_alloc_skb(pkt_len+2);
 719
 720        /*
 721         *      Start of frame
 722         */
 723
 724        outw(0x00, GP_LOW);
 725        if (skb == NULL)
 726        {
 727                printk(KERN_INFO "%s: Memory squeeze, dropping packet.\n", dev->name);
 728                lp->stats.rx_dropped++;
 729                return;
 730        }
 731        else
 732        {
 733                skb_reserve(skb,2);     /* Force 16 byte alignment */
 734                skb->dev = dev;
 735                /*
 736                 *      The read increments through the bytes. The interrupt
 737                 *      handler will fix the pointer when it returns to
 738                 *      receive mode.
 739                 */
 740                insb(DATAPORT, skb_put(skb,pkt_len), pkt_len);
 741                skb->protocol=eth_type_trans(skb,dev);
 742                netif_rx(skb);
 743                dev->last_rx = jiffies;
 744                lp->stats.rx_packets++;
 745                lp->stats.rx_bytes+=pkt_len;
 746        }
 747        return;
 748}
 749
 750/**
 751 * el_reset: Reset a 3c501 card
 752 * @dev: The 3c501 card about to get zapped
 753 *
 754 * Even resetting a 3c501 isnt simple. When you activate reset it loses all
 755 * its configuration. You must hold the lock when doing this. The function
 756 * cannot take the lock itself as it is callable from the irq handler.
 757 */
 758
 759static void  el_reset(struct net_device *dev)
 760{
 761        struct net_local *lp = (struct net_local *)dev->priv;
 762        int ioaddr = dev->base_addr;
 763
 764        if (el_debug> 2)
 765                printk(KERN_INFO "3c501 reset...");
 766        outb(AX_RESET, AX_CMD);         /* Reset the chip */
 767        outb(AX_LOOP, AX_CMD);          /* Aux control, irq and loopback enabled */
 768        {
 769                int i;
 770                for (i = 0; i < 6; i++) /* Set the station address. */
 771                        outb(dev->dev_addr[i], ioaddr + i);
 772        }
 773
 774        outw(0, RX_BUF_CLR);            /* Set rx packet area to 0. */
 775        outb(TX_NORM, TX_CMD);          /* tx irq on done, collision */
 776        outb(RX_NORM, RX_CMD);          /* Set Rx commands. */
 777        inb(RX_STATUS);                 /* Clear status. */
 778        inb(TX_STATUS);
 779        lp->txing = 0;
 780}
 781
 782/**
 783 * el1_close:
 784 * @dev: 3c501 card to shut down
 785 *
 786 * Close a 3c501 card. The IFF_UP flag has been cleared by the user via
 787 * the SIOCSIFFLAGS ioctl. We stop any further transmissions being queued,
 788 * and then disable the interrupts. Finally we reset the chip. The effects
 789 * of the rest will be cleaned up by #el1_open. Always returns 0 indicating
 790 * a success.
 791 */
 792 
 793static int el1_close(struct net_device *dev)
 794{
 795        int ioaddr = dev->base_addr;
 796
 797        if (el_debug > 2)
 798                printk(KERN_INFO "%s: Shutting down Ethernet card at %#x.\n", dev->name, ioaddr);
 799
 800        netif_stop_queue(dev);
 801        
 802        /*
 803         *      Free and disable the IRQ.
 804         */
 805
 806        free_irq(dev->irq, dev);
 807        outb(AX_RESET, AX_CMD);         /* Reset the chip */
 808
 809        return 0;
 810}
 811
 812/**
 813 * el1_get_stats:
 814 * @dev: The card to get the statistics for
 815 *
 816 * In smarter devices this function is needed to pull statistics off the
 817 * board itself. The 3c501 has no hardware statistics. We maintain them all
 818 * so they are by definition always up to date.
 819 *
 820 * Returns the statistics for the card from the card private data
 821 */
 822 
 823static struct net_device_stats *el1_get_stats(struct net_device *dev)
 824{
 825        struct net_local *lp = (struct net_local *)dev->priv;
 826        return &lp->stats;
 827}
 828
 829/**
 830 * set_multicast_list:
 831 * @dev: The device to adjust
 832 *
 833 * Set or clear the multicast filter for this adaptor to use the best-effort 
 834 * filtering supported. The 3c501 supports only three modes of filtering.
 835 * It always receives broadcasts and packets for itself. You can choose to
 836 * optionally receive all packets, or all multicast packets on top of this.
 837 */
 838
 839static void set_multicast_list(struct net_device *dev)
 840{
 841        int ioaddr = dev->base_addr;
 842
 843        if(dev->flags&IFF_PROMISC)
 844        {
 845                outb(RX_PROM, RX_CMD);
 846                inb(RX_STATUS);
 847        }
 848        else if (dev->mc_list || dev->flags&IFF_ALLMULTI)
 849        {
 850                outb(RX_MULT, RX_CMD);  /* Multicast or all multicast is the same */
 851                inb(RX_STATUS);         /* Clear status. */
 852        }
 853        else
 854        {
 855                outb(RX_NORM, RX_CMD);
 856                inb(RX_STATUS);
 857        }
 858}
 859
 860
 861static void netdev_get_drvinfo(struct net_device *dev,
 862                               struct ethtool_drvinfo *info)
 863{
 864        strcpy(info->driver, DRV_NAME);
 865        strcpy(info->version, DRV_VERSION);
 866        sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
 867}
 868
 869static u32 netdev_get_msglevel(struct net_device *dev)
 870{
 871        return debug;
 872}
 873
 874static void netdev_set_msglevel(struct net_device *dev, u32 level)
 875{
 876        debug = level;
 877}
 878
 879static struct ethtool_ops netdev_ethtool_ops = {
 880        .get_drvinfo            = netdev_get_drvinfo,
 881        .get_msglevel           = netdev_get_msglevel,
 882        .set_msglevel           = netdev_set_msglevel,
 883};
 884
 885#ifdef MODULE
 886
 887static struct net_device dev_3c501 = {
 888        .init           = el1_probe,
 889        .base_addr      = 0x280,
 890        .irq            = 5,
 891};
 892
 893static int io=0x280;
 894static int irq=5;
 895MODULE_PARM(io, "i");
 896MODULE_PARM(irq, "i");
 897MODULE_PARM_DESC(io, "EtherLink I/O base address");
 898MODULE_PARM_DESC(irq, "EtherLink IRQ number");
 899
 900/**
 901 * init_module:
 902 *
 903 * When the driver is loaded as a module this function is called. We fake up
 904 * a device structure with the base I/O and interrupt set as if it were being
 905 * called from Space.c. This minimises the extra code that would otherwise
 906 * be required.
 907 *
 908 * Returns 0 for success or -EIO if a card is not found. Returning an error
 909 * here also causes the module to be unloaded
 910 */
 911 
 912int init_module(void)
 913{
 914        dev_3c501.irq=irq;
 915        dev_3c501.base_addr=io;
 916        if (register_netdev(&dev_3c501) != 0)
 917                return -EIO;
 918        return 0;
 919}
 920
 921/**
 922 * cleanup_module:
 923 * 
 924 * The module is being unloaded. We unhook our network device from the system
 925 * and then free up the resources we took when the card was found.
 926 */
 927 
 928void cleanup_module(void)
 929{
 930        unregister_netdev(&dev_3c501);
 931
 932        /*
 933         *      Free up the private structure, or leak memory :-)
 934         */
 935
 936        kfree(dev_3c501.priv);
 937        dev_3c501.priv = NULL;  /* gets re-allocated by el1_probe1 */
 938
 939        /*
 940         *      If we don't do this, we can't re-insmod it later.
 941         */
 942        release_region(dev_3c501.base_addr, EL1_IO_EXTENT);
 943}
 944
 945#endif /* MODULE */
 946
 947MODULE_AUTHOR("Donald Becker, Alan Cox");
 948MODULE_DESCRIPTION("Support for the ancient 3Com 3c501 ethernet card");
 949MODULE_LICENSE("GPL");
 950
 951
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.