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