linux-bk/drivers/net/3c507.c
<<
>>
Prefs
   1/* 3c507.c: An EtherLink16 device driver for Linux. */
   2/*
   3        Written 1993,1994 by Donald Becker.
   4
   5        Copyright 1993 United States Government as represented by the
   6        Director, National Security Agency.
   7
   8        This software may be used and distributed according to the terms
   9        of the GNU General Public License, incorporated herein by reference.
  10
  11        The author may be reached as becker@scyld.com, or C/O
  12        Scyld Computing Corporation
  13        410 Severn Ave., Suite 210
  14        Annapolis MD 21403
  15
  16
  17        Thanks go to jennings@Montrouge.SMR.slb.com ( Patrick Jennings)
  18        and jrs@world.std.com (Rick Sladkey) for testing and bugfixes.
  19        Mark Salazar <leslie@access.digex.net> made the changes for cards with
  20        only 16K packet buffers.
  21
  22        Things remaining to do:
  23        Verify that the tx and rx buffers don't have fencepost errors.
  24        Move the theory of operation and memory map documentation.
  25        The statistics need to be updated correctly.
  26*/
  27
  28#define DRV_NAME                "3c507"
  29#define DRV_VERSION             "1.10a"
  30#define DRV_RELDATE             "11/17/2001"
  31
  32static const char version[] =
  33        DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Donald Becker (becker@scyld.com)\n";
  34
  35
  36#include <linux/module.h>
  37
  38/*
  39  Sources:
  40        This driver wouldn't have been written with the availability of the
  41        Crynwr driver source code.      It provided a known-working implementation
  42        that filled in the gaping holes of the Intel documentation.  Three cheers
  43        for Russ Nelson.
  44
  45        Intel Microcommunications Databook, Vol. 1, 1990.  It provides just enough
  46        info that the casual reader might think that it documents the i82586 :-<.
  47*/
  48
  49#include <linux/kernel.h>
  50#include <linux/sched.h>
  51#include <linux/types.h>
  52#include <linux/fcntl.h>
  53#include <linux/interrupt.h>
  54#include <linux/ioport.h>
  55#include <linux/in.h>
  56#include <linux/string.h>
  57#include <linux/spinlock.h>
  58#include <linux/ethtool.h>
  59
  60#include <asm/uaccess.h>
  61#include <asm/system.h>
  62#include <asm/bitops.h>
  63#include <asm/io.h>
  64#include <asm/dma.h>
  65#include <linux/errno.h>
  66
  67#include <linux/netdevice.h>
  68#include <linux/etherdevice.h>
  69#include <linux/skbuff.h>
  70#include <linux/slab.h>
  71#include <linux/init.h>
  72
  73
  74/* use 0 for production, 1 for verification, 2..7 for debug */
  75#ifndef NET_DEBUG
  76#define NET_DEBUG 1
  77#endif
  78static unsigned int net_debug = NET_DEBUG;
  79#define debug net_debug
  80
  81
  82/* A zero-terminated list of common I/O addresses to be probed. */
  83static unsigned int netcard_portlist[] __initdata =
  84        { 0x300, 0x320, 0x340, 0x280, 0};
  85
  86/*
  87                        Details of the i82586.
  88
  89   You'll really need the databook to understand the details of this part,
  90   but the outline is that the i82586 has two separate processing units.
  91   Both are started from a list of three configuration tables, of which only
  92   the last, the System Control Block (SCB), is used after reset-time.  The SCB
  93   has the following fields:
  94                Status word
  95                Command word
  96                Tx/Command block addr.
  97                Rx block addr.
  98   The command word accepts the following controls for the Tx and Rx units:
  99  */
 100
 101#define  CUC_START       0x0100
 102#define  CUC_RESUME      0x0200
 103#define  CUC_SUSPEND 0x0300
 104#define  RX_START        0x0010
 105#define  RX_RESUME       0x0020
 106#define  RX_SUSPEND      0x0030
 107
 108/* The Rx unit uses a list of frame descriptors and a list of data buffer
 109   descriptors.  We use full-sized (1518 byte) data buffers, so there is
 110   a one-to-one pairing of frame descriptors to buffer descriptors.
 111
 112   The Tx ("command") unit executes a list of commands that look like:
 113                Status word             Written by the 82586 when the command is done.
 114                Command word    Command in lower 3 bits, post-command action in upper 3
 115                Link word               The address of the next command.
 116                Parameters              (as needed).
 117
 118        Some definitions related to the Command Word are:
 119 */
 120#define CMD_EOL         0x8000                  /* The last command of the list, stop. */
 121#define CMD_SUSP        0x4000                  /* Suspend after doing cmd. */
 122#define CMD_INTR        0x2000                  /* Interrupt after doing cmd. */
 123
 124enum commands {
 125        CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
 126        CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
 127
 128/* Information that need to be kept for each board. */
 129struct net_local {
 130        struct net_device_stats stats;
 131        int last_restart;
 132        ushort rx_head;
 133        ushort rx_tail;
 134        ushort tx_head;
 135        ushort tx_cmd_link;
 136        ushort tx_reap;
 137        ushort tx_pkts_in_ring;
 138        spinlock_t lock;
 139};
 140
 141/*
 142                Details of the EtherLink16 Implementation
 143  The 3c507 is a generic shared-memory i82586 implementation.
 144  The host can map 16K, 32K, 48K, or 64K of the 64K memory into
 145  0x0[CD][08]0000, or all 64K into 0xF[02468]0000.
 146  */
 147
 148/* Offsets from the base I/O address. */
 149#define SA_DATA         0       /* Station address data, or 3Com signature. */
 150#define MISC_CTRL       6       /* Switch the SA_DATA banks, and bus config bits. */
 151#define RESET_IRQ       10      /* Reset the latched IRQ line. */
 152#define SIGNAL_CA       11      /* Frob the 82586 Channel Attention line. */
 153#define ROM_CONFIG      13
 154#define MEM_CONFIG      14
 155#define IRQ_CONFIG      15
 156#define EL16_IO_EXTENT 16
 157
 158/* The ID port is used at boot-time to locate the ethercard. */
 159#define ID_PORT         0x100
 160
 161/* Offsets to registers in the mailbox (SCB). */
 162#define iSCB_STATUS     0x8
 163#define iSCB_CMD                0xA
 164#define iSCB_CBL                0xC     /* Command BLock offset. */
 165#define iSCB_RFA                0xE     /* Rx Frame Area offset. */
 166
 167/*  Since the 3c507 maps the shared memory window so that the last byte is
 168        at 82586 address FFFF, the first byte is at 82586 address 0, 16K, 32K, or
 169        48K corresponding to window sizes of 64K, 48K, 32K and 16K respectively.
 170        We can account for this be setting the 'SBC Base' entry in the ISCP table
 171        below for all the 16 bit offset addresses, and also adding the 'SCB Base'
 172        value to all 24 bit physical addresses (in the SCP table and the TX and RX
 173        Buffer Descriptors).
 174                                        -Mark
 175        */
 176#define SCB_BASE                ((unsigned)64*1024 - (dev->mem_end - dev->mem_start))
 177
 178/*
 179  What follows in 'init_words[]' is the "program" that is downloaded to the
 180  82586 memory.  It's mostly tables and command blocks, and starts at the
 181  reset address 0xfffff6.  This is designed to be similar to the EtherExpress,
 182  thus the unusual location of the SCB at 0x0008.
 183
 184  Even with the additional "don't care" values, doing it this way takes less
 185  program space than initializing the individual tables, and I feel it's much
 186  cleaner.
 187
 188  The databook is particularly useless for the first two structures, I had
 189  to use the Crynwr driver as an example.
 190
 191   The memory setup is as follows:
 192   */
 193
 194#define CONFIG_CMD      0x0018
 195#define SET_SA_CMD      0x0024
 196#define SA_OFFSET       0x002A
 197#define IDLELOOP        0x30
 198#define TDR_CMD         0x38
 199#define TDR_TIME        0x3C
 200#define DUMP_CMD        0x40
 201#define DIAG_CMD        0x48
 202#define SET_MC_CMD      0x4E
 203#define DUMP_DATA       0x56    /* A 170 byte buffer for dump and Set-MC into. */
 204
 205#define TX_BUF_START    0x0100
 206#define NUM_TX_BUFS     5
 207#define TX_BUF_SIZE     (1518+14+20+16) /* packet+header+TBD */
 208
 209#define RX_BUF_START    0x2000
 210#define RX_BUF_SIZE     (1518+14+18)    /* packet+header+RBD */
 211#define RX_BUF_END              (dev->mem_end - dev->mem_start)
 212
 213#define TX_TIMEOUT 5
 214
 215/*
 216  That's it: only 86 bytes to set up the beast, including every extra
 217  command available.  The 170 byte buffer at DUMP_DATA is shared between the
 218  Dump command (called only by the diagnostic program) and the SetMulticastList
 219  command.
 220
 221  To complete the memory setup you only have to write the station address at
 222  SA_OFFSET and create the Tx & Rx buffer lists.
 223
 224  The Tx command chain and buffer list is setup as follows:
 225  A Tx command table, with the data buffer pointing to...
 226  A Tx data buffer descriptor.  The packet is in a single buffer, rather than
 227        chaining together several smaller buffers.
 228  A NoOp command, which initially points to itself,
 229  And the packet data.
 230
 231  A transmit is done by filling in the Tx command table and data buffer,
 232  re-writing the NoOp command, and finally changing the offset of the last
 233  command to point to the current Tx command.  When the Tx command is finished,
 234  it jumps to the NoOp, when it loops until the next Tx command changes the
 235  "link offset" in the NoOp.  This way the 82586 never has to go through the
 236  slow restart sequence.
 237
 238  The Rx buffer list is set up in the obvious ring structure.  We have enough
 239  memory (and low enough interrupt latency) that we can avoid the complicated
 240  Rx buffer linked lists by alway associating a full-size Rx data buffer with
 241  each Rx data frame.
 242
 243  I current use four transmit buffers starting at TX_BUF_START (0x0100), and
 244  use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
 245
 246  */
 247
 248static unsigned short init_words[] = {
 249        /*      System Configuration Pointer (SCP). */
 250        0x0000,                                 /* Set bus size to 16 bits. */
 251        0,0,                                    /* pad words. */
 252        0x0000,0x0000,                  /* ISCP phys addr, set in init_82586_mem(). */
 253
 254        /*      Intermediate System Configuration Pointer (ISCP). */
 255        0x0001,                                 /* Status word that's cleared when init is done. */
 256        0x0008,0,0,                             /* SCB offset, (skip, skip) */
 257
 258        /* System Control Block (SCB). */
 259        0,0xf000|RX_START|CUC_START,    /* SCB status and cmd. */
 260        CONFIG_CMD,                             /* Command list pointer, points to Configure. */
 261        RX_BUF_START,                           /* Rx block list. */
 262        0,0,0,0,                                /* Error count: CRC, align, buffer, overrun. */
 263
 264        /* 0x0018: Configure command.  Change to put MAC data with packet. */
 265        0, CmdConfigure,                /* Status, command.             */
 266        SET_SA_CMD,                             /* Next command is Set Station Addr. */
 267        0x0804,                                 /* "4" bytes of config data, 8 byte FIFO. */
 268        0x2e40,                                 /* Magic values, including MAC data location. */
 269        0,                                              /* Unused pad word. */
 270
 271        /* 0x0024: Setup station address command. */
 272        0, CmdSASetup,
 273        SET_MC_CMD,                             /* Next command. */
 274        0xaa00,0xb000,0x0bad,   /* Station address (to be filled in) */
 275
 276        /* 0x0030: NOP, looping back to itself.  Point to first Tx buffer to Tx. */
 277        0, CmdNOp, IDLELOOP, 0 /* pad */,
 278
 279        /* 0x0038: A unused Time-Domain Reflectometer command. */
 280        0, CmdTDR, IDLELOOP, 0,
 281
 282        /* 0x0040: An unused Dump State command. */
 283        0, CmdDump, IDLELOOP, DUMP_DATA,
 284
 285        /* 0x0048: An unused Diagnose command. */
 286        0, CmdDiagnose, IDLELOOP,
 287
 288        /* 0x004E: An empty set-multicast-list command. */
 289        0, CmdMulticastList, IDLELOOP, 0,
 290};
 291
 292/* Index to functions, as function prototypes. */
 293
 294extern int el16_probe(struct net_device *dev);  /* Called from Space.c */
 295
 296static int      el16_probe1(struct net_device *dev, int ioaddr);
 297static int      el16_open(struct net_device *dev);
 298static int      el16_send_packet(struct sk_buff *skb, struct net_device *dev);
 299static void     el16_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 300static void el16_rx(struct net_device *dev);
 301static int      el16_close(struct net_device *dev);
 302static struct net_device_stats *el16_get_stats(struct net_device *dev);
 303static void el16_tx_timeout (struct net_device *dev);
 304
 305static void hardware_send_packet(struct net_device *dev, void *buf, short length);
 306static void init_82586_mem(struct net_device *dev);
 307static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
 308
 309
 310/* Check for a network adaptor of this type, and return '0' iff one exists.
 311        If dev->base_addr == 0, probe all likely locations.
 312        If dev->base_addr == 1, always return failure.
 313        If dev->base_addr == 2, (detachable devices only) allocate space for the
 314        device and return success.
 315        */
 316
 317int __init el16_probe(struct net_device *dev)
 318{
 319        int base_addr = dev->base_addr;
 320        int i;
 321
 322        SET_MODULE_OWNER(dev);
 323
 324        if (base_addr > 0x1ff)  /* Check a single specified location. */
 325                return el16_probe1(dev, base_addr);
 326        else if (base_addr != 0)
 327                return -ENXIO;          /* Don't probe at all. */
 328
 329        for (i = 0; netcard_portlist[i]; i++)
 330                if (el16_probe1(dev, netcard_portlist[i]) == 0)
 331                        return 0;
 332
 333        return -ENODEV;
 334}
 335
 336static int __init el16_probe1(struct net_device *dev, int ioaddr)
 337{
 338        static unsigned char init_ID_done, version_printed;
 339        int i, irq, irqval, retval;
 340        struct net_local *lp;
 341
 342        if (init_ID_done == 0) {
 343                ushort lrs_state = 0xff;
 344                /* Send the ID sequence to the ID_PORT to enable the board(s). */
 345                outb(0x00, ID_PORT);
 346                for(i = 0; i < 255; i++) {
 347                        outb(lrs_state, ID_PORT);
 348                        lrs_state <<= 1;
 349                        if (lrs_state & 0x100)
 350                                lrs_state ^= 0xe7;
 351                }
 352                outb(0x00, ID_PORT);
 353                init_ID_done = 1;
 354        }
 355
 356        if (!request_region(ioaddr, EL16_IO_EXTENT, dev->name))
 357                return -ENODEV;
 358
 359        if ((inb(ioaddr) != '*') || (inb(ioaddr + 1) != '3') || 
 360            (inb(ioaddr + 2) != 'C') || (inb(ioaddr + 3) != 'O')) {
 361                retval = -ENODEV;
 362                goto out;
 363        }
 364
 365        if (net_debug  &&  version_printed++ == 0)
 366                printk(version);
 367
 368        printk("%s: 3c507 at %#x,", dev->name, ioaddr);
 369
 370        /* We should make a few more checks here, like the first three octets of
 371           the S.A. for the manufacturer's code. */
 372
 373        irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
 374
 375        irqval = request_irq(irq, &el16_interrupt, 0, dev->name, dev);
 376        if (irqval) {
 377                printk ("unable to get IRQ %d (irqval=%d).\n", irq, irqval);
 378                retval = -EAGAIN;
 379                goto out;
 380        }
 381
 382        /* We've committed to using the board, and can start filling in *dev. */
 383        dev->base_addr = ioaddr;
 384
 385        outb(0x01, ioaddr + MISC_CTRL);
 386        for (i = 0; i < 6; i++) {
 387                dev->dev_addr[i] = inb(ioaddr + i);
 388                printk(" %02x", dev->dev_addr[i]);
 389        }
 390
 391        if ((dev->mem_start & 0xf) > 0)
 392                net_debug = dev->mem_start & 7;
 393
 394#ifdef MEM_BASE
 395        dev->mem_start = MEM_BASE;
 396        dev->mem_end = dev->mem_start + 0x10000;
 397#else
 398        {
 399                int base;
 400                int size;
 401                char mem_config = inb(ioaddr + MEM_CONFIG);
 402                if (mem_config & 0x20) {
 403                        size = 64*1024;
 404                        base = 0xf00000 + (mem_config & 0x08 ? 0x080000
 405                                                           : ((mem_config & 3) << 17));
 406                } else {
 407                        size = ((mem_config & 3) + 1) << 14;
 408                        base = 0x0c0000 + ( (mem_config & 0x18) << 12);
 409                }
 410                dev->mem_start = base;
 411                dev->mem_end = base + size;
 412        }
 413#endif
 414
 415        dev->if_port = (inb(ioaddr + ROM_CONFIG) & 0x80) ? 1 : 0;
 416        dev->irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
 417
 418        printk(", IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->irq,
 419                   dev->if_port ? "ex" : "in", dev->mem_start, dev->mem_end-1);
 420
 421        if (net_debug)
 422                printk(version);
 423
 424        /* Initialize the device structure. */
 425        lp = dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
 426        if (dev->priv == NULL) {
 427                retval = -ENOMEM;
 428                goto out;
 429        }
 430        memset(dev->priv, 0, sizeof(struct net_local));
 431        spin_lock_init(&lp->lock);
 432
 433        dev->open               = el16_open;
 434        dev->stop               = el16_close;
 435        dev->hard_start_xmit = el16_send_packet;
 436        dev->get_stats  = el16_get_stats;
 437        dev->tx_timeout = el16_tx_timeout;
 438        dev->watchdog_timeo = TX_TIMEOUT;
 439        dev->do_ioctl = netdev_ioctl;
 440
 441        ether_setup(dev);       /* Generic ethernet behaviour */
 442
 443        dev->flags&=~IFF_MULTICAST;     /* Multicast doesn't work */
 444
 445        return 0;
 446out:
 447        release_region(ioaddr, EL16_IO_EXTENT);
 448        return retval;
 449}
 450
 451static int el16_open(struct net_device *dev)
 452{
 453        /* Initialize the 82586 memory and start it. */
 454        init_82586_mem(dev);
 455
 456        netif_start_queue(dev);
 457        return 0;
 458}
 459
 460
 461static void el16_tx_timeout (struct net_device *dev)
 462{
 463        struct net_local *lp = (struct net_local *) dev->priv;
 464        int ioaddr = dev->base_addr;
 465        unsigned long shmem = dev->mem_start;
 466
 467        if (net_debug > 1)
 468                printk ("%s: transmit timed out, %s?  ", dev->name,
 469                        isa_readw (shmem + iSCB_STATUS) & 0x8000 ? "IRQ conflict" :
 470                        "network cable problem");
 471        /* Try to restart the adaptor. */
 472        if (lp->last_restart == lp->stats.tx_packets) {
 473                if (net_debug > 1)
 474                        printk ("Resetting board.\n");
 475                /* Completely reset the adaptor. */
 476                init_82586_mem (dev);
 477                lp->tx_pkts_in_ring = 0;
 478        } else {
 479                /* Issue the channel attention signal and hope it "gets better". */
 480                if (net_debug > 1)
 481                        printk ("Kicking board.\n");
 482                isa_writew (0xf000 | CUC_START | RX_START, shmem + iSCB_CMD);
 483                outb (0, ioaddr + SIGNAL_CA);   /* Issue channel-attn. */
 484                lp->last_restart = lp->stats.tx_packets;
 485        }
 486        dev->trans_start = jiffies;
 487        netif_wake_queue (dev);
 488}
 489
 490
 491static int el16_send_packet (struct sk_buff *skb, struct net_device *dev)
 492{
 493        struct net_local *lp = (struct net_local *) dev->priv;
 494        int ioaddr = dev->base_addr;
 495        unsigned long flags;
 496        short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
 497        unsigned char *buf = skb->data;
 498
 499        netif_stop_queue (dev);
 500
 501        spin_lock_irqsave (&lp->lock, flags);
 502
 503        lp->stats.tx_bytes += length;
 504        /* Disable the 82586's input to the interrupt line. */
 505        outb (0x80, ioaddr + MISC_CTRL);
 506
 507        hardware_send_packet (dev, buf, length);
 508
 509        dev->trans_start = jiffies;
 510        /* Enable the 82586 interrupt input. */
 511        outb (0x84, ioaddr + MISC_CTRL);
 512
 513        spin_unlock_irqrestore (&lp->lock, flags);
 514
 515        dev_kfree_skb (skb);
 516
 517        /* You might need to clean up and record Tx statistics here. */
 518
 519        return 0;
 520}
 521
 522/*      The typical workload of the driver:
 523        Handle the network interface interrupts. */
 524static void el16_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 525{
 526        struct net_device *dev = dev_id;
 527        struct net_local *lp;
 528        int ioaddr, status, boguscount = 0;
 529        ushort ack_cmd = 0;
 530        unsigned long shmem;
 531
 532        if (dev == NULL) {
 533                printk ("net_interrupt(): irq %d for unknown device.\n", irq);
 534                return;
 535        }
 536
 537        ioaddr = dev->base_addr;
 538        lp = (struct net_local *)dev->priv;
 539        shmem = dev->mem_start;
 540
 541        spin_lock(&lp->lock);
 542
 543        status = isa_readw(shmem+iSCB_STATUS);
 544
 545        if (net_debug > 4) {
 546                printk("%s: 3c507 interrupt, status %4.4x.\n", dev->name, status);
 547        }
 548
 549        /* Disable the 82586's input to the interrupt line. */
 550        outb(0x80, ioaddr + MISC_CTRL);
 551
 552        /* Reap the Tx packet buffers. */
 553        while (lp->tx_pkts_in_ring) {
 554          unsigned short tx_status = isa_readw(shmem+lp->tx_reap);
 555          if (!(tx_status & 0x8000)) {
 556                if (net_debug > 5) 
 557                        printk("Tx command incomplete (%#x).\n", lp->tx_reap);
 558                break;
 559          }
 560          /* Tx unsuccessful or some interesting status bit set. */
 561          if (!(tx_status & 0x2000) || (tx_status & 0x0f3f)) {
 562                lp->stats.tx_errors++;
 563                if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
 564                if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
 565                if (!(tx_status & 0x0040))  lp->stats.tx_heartbeat_errors++;
 566                if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
 567                lp->stats.collisions += tx_status & 0xf;
 568          }
 569          lp->stats.tx_packets++;
 570          if (net_debug > 5)
 571                  printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
 572          lp->tx_reap += TX_BUF_SIZE;
 573          if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
 574                lp->tx_reap = TX_BUF_START;
 575
 576          lp->tx_pkts_in_ring--;
 577          /* There is always more space in the Tx ring buffer now. */
 578          netif_wake_queue(dev);
 579
 580          if (++boguscount > 10)
 581                break;
 582        }
 583
 584        if (status & 0x4000) { /* Packet received. */
 585                if (net_debug > 5)
 586                        printk("Received packet, rx_head %04x.\n", lp->rx_head);
 587                el16_rx(dev);
 588        }
 589
 590        /* Acknowledge the interrupt sources. */
 591        ack_cmd = status & 0xf000;
 592
 593        if ((status & 0x0700) != 0x0200 && netif_running(dev)) {
 594                if (net_debug)
 595                        printk("%s: Command unit stopped, status %04x, restarting.\n",
 596                                   dev->name, status);
 597                /* If this ever occurs we should really re-write the idle loop, reset
 598                   the Tx list, and do a complete restart of the command unit.
 599                   For now we rely on the Tx timeout if the resume doesn't work. */
 600                ack_cmd |= CUC_RESUME;
 601        }
 602
 603        if ((status & 0x0070) != 0x0040 && netif_running(dev)) {
 604                static void init_rx_bufs(struct net_device *);
 605                /* The Rx unit is not ready, it must be hung.  Restart the receiver by
 606                   initializing the rx buffers, and issuing an Rx start command. */
 607                if (net_debug)
 608                        printk("%s: Rx unit stopped, status %04x, restarting.\n",
 609                                   dev->name, status);
 610                init_rx_bufs(dev);
 611                isa_writew(RX_BUF_START,shmem+iSCB_RFA);
 612                ack_cmd |= RX_START;
 613        }
 614
 615        isa_writew(ack_cmd,shmem+iSCB_CMD);
 616        outb(0, ioaddr + SIGNAL_CA);                    /* Issue channel-attn. */
 617
 618        /* Clear the latched interrupt. */
 619        outb(0, ioaddr + RESET_IRQ);
 620
 621        /* Enable the 82586's interrupt input. */
 622        outb(0x84, ioaddr + MISC_CTRL);
 623        spin_unlock(&lp->lock);
 624}
 625
 626static int el16_close(struct net_device *dev)
 627{
 628        int ioaddr = dev->base_addr;
 629        unsigned long shmem = dev->mem_start;
 630
 631        netif_stop_queue(dev);
 632
 633        /* Flush the Tx and disable Rx. */
 634        isa_writew(RX_SUSPEND | CUC_SUSPEND,shmem+iSCB_CMD);
 635        outb(0, ioaddr + SIGNAL_CA);
 636
 637        /* Disable the 82586's input to the interrupt line. */
 638        outb(0x80, ioaddr + MISC_CTRL);
 639
 640        /* We always physically use the IRQ line, so we don't do free_irq(). */
 641
 642        /* Update the statistics here. */
 643
 644        return 0;
 645}
 646
 647/* Get the current statistics.  This may be called with the card open or
 648   closed. */
 649static struct net_device_stats *el16_get_stats(struct net_device *dev)
 650{
 651        struct net_local *lp = (struct net_local *)dev->priv;
 652
 653        /* ToDo: decide if there are any useful statistics from the SCB. */
 654
 655        return &lp->stats;
 656}
 657
 658/* Initialize the Rx-block list. */
 659static void init_rx_bufs(struct net_device *dev)
 660{
 661        struct net_local *lp = (struct net_local *)dev->priv;
 662        unsigned long write_ptr;
 663        unsigned short SCB_base = SCB_BASE;
 664
 665        int cur_rxbuf = lp->rx_head = RX_BUF_START;
 666
 667        /* Initialize each Rx frame + data buffer. */
 668        do {    /* While there is room for one more. */
 669
 670          write_ptr = dev->mem_start + cur_rxbuf;
 671
 672                isa_writew(0x0000,write_ptr);                   /* Status */
 673                isa_writew(0x0000,write_ptr+=2);                        /* Command */
 674                isa_writew(cur_rxbuf + RX_BUF_SIZE,write_ptr+=2);       /* Link */
 675                isa_writew(cur_rxbuf + 22,write_ptr+=2);                /* Buffer offset */
 676                isa_writew(0x0000,write_ptr+=2);                        /* Pad for dest addr. */
 677                isa_writew(0x0000,write_ptr+=2);
 678                isa_writew(0x0000,write_ptr+=2);
 679                isa_writew(0x0000,write_ptr+=2);                        /* Pad for source addr. */
 680                isa_writew(0x0000,write_ptr+=2);
 681                isa_writew(0x0000,write_ptr+=2);
 682                isa_writew(0x0000,write_ptr+=2);                        /* Pad for protocol. */
 683
 684                isa_writew(0x0000,write_ptr+=2);                        /* Buffer: Actual count */
 685                isa_writew(-1,write_ptr+=2);                    /* Buffer: Next (none). */
 686                isa_writew(cur_rxbuf + 0x20 + SCB_base,write_ptr+=2);/* Buffer: Address low */
 687                isa_writew(0x0000,write_ptr+=2);
 688                /* Finally, the number of bytes in the buffer. */
 689                isa_writew(0x8000 + RX_BUF_SIZE-0x20,write_ptr+=2);
 690
 691                lp->rx_tail = cur_rxbuf;
 692                cur_rxbuf += RX_BUF_SIZE;
 693        } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
 694
 695        /* Terminate the list by setting the EOL bit, and wrap the pointer to make
 696           the list a ring. */
 697        write_ptr = dev->mem_start + lp->rx_tail + 2;
 698        isa_writew(0xC000,write_ptr);                           /* Command, mark as last. */
 699        isa_writew(lp->rx_head,write_ptr+2);                    /* Link */
 700}
 701
 702static void init_82586_mem(struct net_device *dev)
 703{
 704        struct net_local *lp = (struct net_local *)dev->priv;
 705        short ioaddr = dev->base_addr;
 706        unsigned long shmem = dev->mem_start;
 707
 708        /* Enable loopback to protect the wire while starting up,
 709           and hold the 586 in reset during the memory initialization. */
 710        outb(0x20, ioaddr + MISC_CTRL);
 711
 712        /* Fix the ISCP address and base. */
 713        init_words[3] = SCB_BASE;
 714        init_words[7] = SCB_BASE;
 715
 716        /* Write the words at 0xfff6 (address-aliased to 0xfffff6). */
 717        isa_memcpy_toio(dev->mem_end-10, init_words, 10);
 718
 719        /* Write the words at 0x0000. */
 720        isa_memcpy_toio(dev->mem_start, init_words + 5, sizeof(init_words) - 10);
 721
 722        /* Fill in the station address. */
 723        isa_memcpy_toio(dev->mem_start+SA_OFFSET, dev->dev_addr,
 724                   sizeof(dev->dev_addr));
 725
 726        /* The Tx-block list is written as needed.  We just set up the values. */
 727        lp->tx_cmd_link = IDLELOOP + 4;
 728        lp->tx_head = lp->tx_reap = TX_BUF_START;
 729
 730        init_rx_bufs(dev);
 731
 732        /* Start the 586 by releasing the reset line, but leave loopback. */
 733        outb(0xA0, ioaddr + MISC_CTRL);
 734
 735        /* This was time consuming to track down: you need to give two channel
 736           attention signals to reliably start up the i82586. */
 737        outb(0, ioaddr + SIGNAL_CA);
 738
 739        {
 740                int boguscnt = 50;
 741                while (isa_readw(shmem+iSCB_STATUS) == 0)
 742                        if (--boguscnt == 0) {
 743                                printk("%s: i82586 initialization timed out with status %04x,"
 744                                           "cmd %04x.\n", dev->name,
 745                                           isa_readw(shmem+iSCB_STATUS), isa_readw(shmem+iSCB_CMD));
 746                                break;
 747                        }
 748                /* Issue channel-attn -- the 82586 won't start. */
 749                outb(0, ioaddr + SIGNAL_CA);
 750        }
 751
 752        /* Disable loopback and enable interrupts. */
 753        outb(0x84, ioaddr + MISC_CTRL);
 754        if (net_debug > 4)
 755                printk("%s: Initialized 82586, status %04x.\n", dev->name,
 756                           isa_readw(shmem+iSCB_STATUS));
 757        return;
 758}
 759
 760static void hardware_send_packet(struct net_device *dev, void *buf, short length)
 761{
 762        struct net_local *lp = (struct net_local *)dev->priv;
 763        short ioaddr = dev->base_addr;
 764        ushort tx_block = lp->tx_head;
 765        unsigned long write_ptr = dev->mem_start + tx_block;
 766
 767        /* Set the write pointer to the Tx block, and put out the header. */
 768        isa_writew(0x0000,write_ptr);                   /* Tx status */
 769        isa_writew(CMD_INTR|CmdTx,write_ptr+=2);                /* Tx command */
 770        isa_writew(tx_block+16,write_ptr+=2);           /* Next command is a NoOp. */
 771        isa_writew(tx_block+8,write_ptr+=2);                    /* Data Buffer offset. */
 772
 773        /* Output the data buffer descriptor. */
 774        isa_writew(length | 0x8000,write_ptr+=2);               /* Byte count parameter. */
 775        isa_writew(-1,write_ptr+=2);                    /* No next data buffer. */
 776        isa_writew(tx_block+22+SCB_BASE,write_ptr+=2);  /* Buffer follows the NoOp command. */
 777        isa_writew(0x0000,write_ptr+=2);                        /* Buffer address high bits (always zero). */
 778
 779        /* Output the Loop-back NoOp command. */
 780        isa_writew(0x0000,write_ptr+=2);                        /* Tx status */
 781        isa_writew(CmdNOp,write_ptr+=2);                        /* Tx command */
 782        isa_writew(tx_block+16,write_ptr+=2);           /* Next is myself. */
 783
 784        /* Output the packet at the write pointer. */
 785        isa_memcpy_toio(write_ptr+2, buf, length);
 786
 787        /* Set the old command link pointing to this send packet. */
 788        isa_writew(tx_block,dev->mem_start + lp->tx_cmd_link);
 789        lp->tx_cmd_link = tx_block + 20;
 790
 791        /* Set the next free tx region. */
 792        lp->tx_head = tx_block + TX_BUF_SIZE;
 793        if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
 794                lp->tx_head = TX_BUF_START;
 795
 796        if (net_debug > 4) {
 797                printk("%s: 3c507 @%x send length = %d, tx_block %3x, next %3x.\n",
 798                           dev->name, ioaddr, length, tx_block, lp->tx_head);
 799        }
 800
 801        /* Grimly block further packets if there has been insufficient reaping. */
 802        if (++lp->tx_pkts_in_ring < NUM_TX_BUFS) 
 803                netif_wake_queue(dev);
 804}
 805
 806static void el16_rx(struct net_device *dev)
 807{
 808        struct net_local *lp = (struct net_local *)dev->priv;
 809        unsigned long shmem = dev->mem_start;
 810        ushort rx_head = lp->rx_head;
 811        ushort rx_tail = lp->rx_tail;
 812        ushort boguscount = 10;
 813        short frame_status;
 814
 815        while ((frame_status = isa_readw(shmem+rx_head)) < 0) {   /* Command complete */
 816                unsigned long read_frame = dev->mem_start + rx_head;
 817                ushort rfd_cmd = isa_readw(read_frame+2);
 818                ushort next_rx_frame = isa_readw(read_frame+4);
 819                ushort data_buffer_addr = isa_readw(read_frame+6);
 820                unsigned long data_frame = dev->mem_start + data_buffer_addr;
 821                ushort pkt_len = isa_readw(data_frame);
 822
 823                if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22
 824                        || (pkt_len & 0xC000) != 0xC000) {
 825                        printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
 826                                   "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
 827                                   frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
 828                                   pkt_len);
 829                } else if ((frame_status & 0x2000) == 0) {
 830                        /* Frame Rxed, but with error. */
 831                        lp->stats.rx_errors++;
 832                        if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
 833                        if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
 834                        if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
 835                        if (frame_status & 0x0100) lp->stats.rx_over_errors++;
 836                        if (frame_status & 0x0080) lp->stats.rx_length_errors++;
 837                } else {
 838                        /* Malloc up new buffer. */
 839                        struct sk_buff *skb;
 840
 841                        pkt_len &= 0x3fff;
 842                        skb = dev_alloc_skb(pkt_len+2);
 843                        if (skb == NULL) {
 844                                printk("%s: Memory squeeze, dropping packet.\n", dev->name);
 845                                lp->stats.rx_dropped++;
 846                                break;
 847                        }
 848
 849                        skb_reserve(skb,2);
 850                        skb->dev = dev;
 851
 852                        /* 'skb->data' points to the start of sk_buff data area. */
 853                        isa_memcpy_fromio(skb_put(skb,pkt_len), data_frame + 10, pkt_len);
 854
 855                        skb->protocol=eth_type_trans(skb,dev);
 856                        netif_rx(skb);
 857                        dev->last_rx = jiffies;
 858                        lp->stats.rx_packets++;
 859                        lp->stats.rx_bytes += pkt_len;
 860                }
 861
 862                /* Clear the status word and set End-of-List on the rx frame. */
 863                isa_writew(0,read_frame);
 864                isa_writew(0xC000,read_frame+2);
 865                /* Clear the end-of-list on the prev. RFD. */
 866                isa_writew(0x0000,dev->mem_start + rx_tail + 2);
 867
 868                rx_tail = rx_head;
 869                rx_head = next_rx_frame;
 870                if (--boguscount == 0)
 871                        break;
 872        }
 873
 874        lp->rx_head = rx_head;
 875        lp->rx_tail = rx_tail;
 876}
 877
 878/**
 879 * netdev_ethtool_ioctl: Handle network interface SIOCETHTOOL ioctls
 880 * @dev: network interface on which out-of-band action is to be performed
 881 * @useraddr: userspace address to which data is to be read and returned
 882 *
 883 * Process the various commands of the SIOCETHTOOL interface.
 884 */
 885
 886static int netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
 887{
 888        u32 ethcmd;
 889
 890        /* dev_ioctl() in ../../net/core/dev.c has already checked
 891           capable(CAP_NET_ADMIN), so don't bother with that here.  */
 892
 893        if (get_user(ethcmd, (u32 *)useraddr))
 894                return -EFAULT;
 895
 896        switch (ethcmd) {
 897
 898        case ETHTOOL_GDRVINFO: {
 899                struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
 900                strcpy (info.driver, DRV_NAME);
 901                strcpy (info.version, DRV_VERSION);
 902                sprintf(info.bus_info, "ISA 0x%lx", dev->base_addr);
 903                if (copy_to_user (useraddr, &info, sizeof (info)))
 904                        return -EFAULT;
 905                return 0;
 906        }
 907
 908        /* get message-level */
 909        case ETHTOOL_GMSGLVL: {
 910                struct ethtool_value edata = {ETHTOOL_GMSGLVL};
 911                edata.data = debug;
 912                if (copy_to_user(useraddr, &edata, sizeof(edata)))
 913                        return -EFAULT;
 914                return 0;
 915        }
 916        /* set message-level */
 917        case ETHTOOL_SMSGLVL: {
 918                struct ethtool_value edata;
 919                if (copy_from_user(&edata, useraddr, sizeof(edata)))
 920                        return -EFAULT;
 921                debug = edata.data;
 922                return 0;
 923        }
 924
 925        default:
 926                break;
 927        }
 928
 929        return -EOPNOTSUPP;
 930}
 931
 932/**
 933 * netdev_ioctl: Handle network interface ioctls
 934 * @dev: network interface on which out-of-band action is to be performed
 935 * @rq: user request data
 936 * @cmd: command issued by user
 937 *
 938 * Process the various out-of-band ioctls passed to this driver.
 939 */
 940
 941static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
 942{
 943        int rc = 0;
 944
 945        switch (cmd) {
 946        case SIOCETHTOOL:
 947                rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
 948                break;
 949
 950        default:
 951                rc = -EOPNOTSUPP;
 952                break;
 953        }
 954
 955        return rc;
 956}
 957 
 958
 959#ifdef MODULE
 960static struct net_device dev_3c507;
 961static int io = 0x300;
 962static int irq;
 963MODULE_PARM(io, "i");
 964MODULE_PARM(irq, "i");
 965MODULE_PARM_DESC(io, "EtherLink16 I/O base address");
 966MODULE_PARM_DESC(irq, "(ignored)");
 967
 968int init_module(void)
 969{
 970        if (io == 0)
 971                printk("3c507: You should not use auto-probing with insmod!\n");
 972        dev_3c507.base_addr = io;
 973        dev_3c507.irq       = irq;
 974        dev_3c507.init      = el16_probe;
 975        if (register_netdev(&dev_3c507) != 0) {
 976                printk("3c507: register_netdev() returned non-zero.\n");
 977                return -EIO;
 978        }
 979        return 0;
 980}
 981
 982void
 983cleanup_module(void)
 984{
 985        unregister_netdev(&dev_3c507);
 986        kfree(dev_3c507.priv);
 987        dev_3c507.priv = NULL;
 988
 989        /* If we don't do this, we can't re-insmod it later. */
 990        free_irq(dev_3c507.irq, &dev_3c507);
 991        release_region(dev_3c507.base_addr, EL16_IO_EXTENT);
 992}
 993#endif /* MODULE */
 994MODULE_LICENSE("GPL");
 995
 996
 997/*
 998 * Local variables:
 999 *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -I/usr/src/linux/drivers/net -Wall -Wstrict-prototypes -O6 -m486 -c 3c507.c"
1000 *  version-control: t
1001 *  kept-new-versions: 5
1002 *  tab-width: 4
1003 *  c-indent-level: 4
1004 * End:
1005 */
1006
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.