root/drivers/net/eexpress.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. express_probe
  2. eexp_probe1
  3. eexp_open
  4. eexp_send_packet
  5. eexp_interrupt
  6. eexp_close
  7. eexp_get_stats
  8. set_multicast_list
  9. read_eeprom
  10. init_82586_mem
  11. init_rx_bufs
  12. hardware_send_packet
  13. eexp_rx

   1 /* eexpress.c: Intel EtherExpress device driver for Linux. */
   2 /*
   3         Written 1993 by Donald Becker.
   4         Copyright 1993 United States Government as represented by the Director,
   5         National Security Agency.  This software may only be used and distributed
   6         according to the terms of the GNU Public License as modified by SRC,
   7         incorported herein by reference.
   8 
   9         The author may be reached as becker@super.org or
  10         C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  11 
  12         Things remaining to do:
  13         Check that the 586 and ASIC are reset/unreset at the right times.
  14         Check tx and rx buffer setup.
  15         The current Tx is single-buffer-only.
  16         Move the theory of operation and memory map documentation.
  17         Rework the board error reset
  18         The statistics need to be updated correctly.
  19 */
  20 
  21 static char *version =
  22         "eexpress.c:v0.06 10/27/93 Donald Becker (becker@super.org)\n";
  23 
  24 #include <linux/config.h>
  25 
  26 /*
  27   Sources:
  28         This driver wouldn't have been written with the availability of the
  29         Crynwr driver source code.      It provided a known-working implementation
  30         that filled in the gaping holes of the Intel documention.  Three cheers
  31         for Russ Nelson.
  32 
  33         Intel Microcommunications Databook, Vol. 1, 1990. It provides just enough
  34         info that the casual reader might think that it documents the i82586.
  35 */
  36 
  37 #include <linux/kernel.h>
  38 #include <linux/sched.h>
  39 #include <linux/types.h>
  40 #include <linux/fcntl.h>
  41 #include <linux/interrupt.h>
  42 #include <linux/ptrace.h>
  43 #include <linux/ioport.h>
  44 #include <linux/in.h>
  45 #include <asm/system.h>
  46 #include <asm/bitops.h>
  47 #include <asm/io.h>
  48 #include <asm/dma.h>
  49 #include <errno.h>
  50 #include <memory.h>
  51 
  52 #include "dev.h"
  53 #include "eth.h"
  54 #include "skbuff.h"
  55 #include "arp.h"
  56 
  57 #ifndef HAVE_ALLOC_SKB
  58 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
  59 #else
  60 /* This isn't quite right, but it's the best version define I can find right now. */
  61 #include <linux/malloc.h>
  62 #endif
  63 
  64 /* use 0 for production, 1 for verification, 2..7 for debug */
  65 #ifndef NET_DEBUG
  66 #define NET_DEBUG 2
  67 #endif
  68 static unsigned int net_debug = NET_DEBUG;
  69 
  70 /*
  71                         Details of the i82586.
  72 
  73    You'll really need the databook to understand the details of this part,
  74    but the outline is that the i82586 has two seperate processing units.
  75 
  76    The Rx unit uses a list of frame descriptors and a list of data buffer
  77    descriptors.  We use full-sized (1518 byte) data buffers, so there is
  78    a one-to-one pairing of frame descriptors to buffer descriptors.
  79 
  80    The Tx ("command") unit executes a list of commands that look like:
  81                 Status word             Written by the 82586 when the command is done.
  82                 Command word    Command in lower 3 bits, post-command action in upper 3
  83                 Link word               The address of the next command.
  84                 Parameters              (as needed).
  85 
  86         Some definitions related to the Command Word are:
  87  */
  88 #define CMD_EOL         0x8000                  /* The last command of the list, stop. */
  89 #define CMD_SUSP        0x4000                  /* Suspend after doing cmd. */
  90 #define CMD_INTR        0x2000                  /* Interrupt after doing cmd. */
  91 
  92 enum commands {
  93         CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
  94         CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
  95 
  96 /* Information that need to be kept for each board. */
  97 struct net_local {
  98         struct enet_statistics stats;
  99         int last_restart;
 100         short rx_head;
 101         short rx_tail;
 102         short tx_head;
 103         short tx_cmd_link;
 104         short tx_reap;
 105 };
 106 
 107 /*
 108                 Details of the EtherExpress Implementation
 109   The EtherExpress takes an unusual approach to host access to packet buffer
 110   memory.  The host can use either the Dataport, with independent
 111   autoincrementing read and write pointers, or it can I/O map 32 bytes of the
 112   memory using the "Shadow Memory Pointer" (SMB) as follows:
 113                         ioaddr                                          Normal EtherExpress registers
 114                         ioaddr+0x4000...0x400f          Buffer Memory at SMB...SMB+15
 115                         ioaddr+0x8000...0x800f          Buffer Memory at SMB+16...SMB+31
 116                         ioaddr+0xC000...0xC007          "" SMB+16...SMB+23 (hardware flaw?)
 117                         ioaddr+0xC008...0xC00f          Buffer Memory at 0x0008...0x000f
 118   The last I/O map set is useful if you put the i82586 System Command Block
 119   (the command mailbox) exactly at 0x0008.  (There seems to be some
 120   undocumented init structure at 0x0000-7, so I had to use the Crywnr memory
 121   setup verbatim for those four words anyway.)
 122 
 123   A problem with using either one of these mechanisms is that you must run
 124   single-threaded, or the interrupt handler must restore a changed value of
 125   the read, write, or SMB pointers.
 126 
 127   Unlike the Crynwr driver, my driver mostly ignores the I/O mapped "feature"
 128   and relies heavily on the dataport for buffer memory access.  To minimize
 129   switching, the read_pointer is dedicated to the Rx interrupt handler, and
 130   the write_pointer is used by the send_packet() routine (it's carefully saved
 131   and restored when it's needed by the interrupt handler).
 132   */
 133 
 134 /* Offsets from the base I/O address. */
 135 #define DATAPORT        0       /* Data Transfer Register. */
 136 #define WRITE_PTR       2       /* Write Address Pointer. */
 137 #define READ_PTR        4       /* Read Address Pointer. */
 138 #define SIGNAL_CA       6       /* Frob the 82586 Channel Attention line. */
 139 #define SET_IRQ         7       /* IRQ Select. */
 140 #define SHADOW_PTR      8       /* Shadow Memory Bank Pointer. */
 141 #define MEM_Ctrl        11
 142 #define MEM_Page_Ctrl   12
 143 #define Config          13
 144 #define EEPROM_Ctrl             14
 145 #define ID_PORT         15
 146 
 147 /*      EEPROM_Ctrl bits. */
 148 
 149 #define EE_SHIFT_CLK    0x01    /* EEPROM shift clock. */
 150 #define EE_CS                   0x02    /* EEPROM chip select. */
 151 #define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
 152 #define EE_DATA_READ    0x08    /* EEPROM chip data out. */
 153 #define EE_CTRL_BITS    (EE_SHIFT_CLK | EE_CS | EE_DATA_WRITE | EE_DATA_READ)
 154 #define ASIC_RESET              0x40
 155 #define _586_RESET              0x80
 156 
 157 /* Offsets into the System Control Block structure. */
 158 #define SCB_STATUS      0xc008
 159 #define SCB_CMD         0xc00A
 160 #define  CUC_START       0x0100
 161 #define  CUC_RESUME      0x0200
 162 #define  CUC_SUSPEND 0x0300
 163 #define  RX_START        0x0010
 164 #define  RX_RESUME       0x0020
 165 #define  RX_SUSPEND      0x0030
 166 #define SCB_CBL         0xc00C  /* Command BLock offset. */
 167 #define SCB_RFA         0xc00E  /* Rx Frame Area offset. */
 168 
 169 /*
 170   What follows in 'init_words[]' is the "program" that is downloaded to the
 171   82586 memory.  It's mostly tables and command blocks, and starts at the
 172   reset address 0xfffff6.
 173 
 174   Even with the additional "don't care" values, doing it this way takes less
 175   program space than initializing the individual tables, and I feel it's much
 176   cleaner.
 177 
 178   The databook is particularly useless for the first two structures, I had
 179   to use the Crynwr driver as an example.
 180 
 181    The memory setup is as follows:
 182    */
 183 
 184 #define CONFIG_CMD      0x0018
 185 #define SET_SA_CMD      0x0024
 186 #define SA_OFFSET       0x002A
 187 #define IDLELOOP        0x30
 188 #define TDR_CMD         0x38
 189 #define TDR_TIME        0x3C
 190 #define DUMP_CMD        0x40
 191 #define DIAG_CMD        0x48
 192 #define SET_MC_CMD      0x4E
 193 #define DUMP_DATA       0x56    /* A 170 byte buffer for dump and Set-MC into. */
 194 
 195 #define TX_BUF_START    0x0100
 196 #define NUM_TX_BUFS     4
 197 #define TX_BUF_SIZE     (1518+14+20+16) /* packet+header+TBD */
 198 
 199 #define RX_BUF_START    0x2000
 200 #define RX_BUF_SIZE     (0x640) /* packet+header+RBD+extra */
 201 #define RX_BUF_END              0x8000
 202 
 203 /*
 204   That's it: only 86 bytes to set up the beast, including every extra
 205   command available.  The 170 byte buffer at DUMP_DATA is shared between the
 206   Dump command (called only by the diagnostic program) and the SetMulticastList
 207   command. 
 208 
 209   To complete the memory setup you only have to write the station address at
 210   SA_OFFSET and create the Tx & Rx buffer lists.
 211 
 212   The Tx command chain and buffer list is setup as follows:
 213   A Tx command table, with the data buffer pointing to...
 214   A Tx data buffer descriptor.  The packet is in a single buffer, rather than
 215      chaining together several smaller buffers.
 216   A NoOp command, which initially points to itself,
 217   And the packet data.
 218 
 219   A transmit is done by filling in the Tx command table and data buffer,
 220   re-writing the NoOp command, and finally changing the offset of the last
 221   command to point to the current Tx command.  When the Tx command is finished,
 222   it jumps to the NoOp, when it loops until the next Tx command changes the
 223   "link offset" in the NoOp.  This way the 82586 never has to go through the
 224   slow restart sequence.
 225 
 226   The Rx buffer list is set up in the obvious ring structure.  We have enough
 227   memory (and low enough interrupt latency) that we can avoid the complicated
 228   Rx buffer linked lists by alway associating a full-size Rx data buffer with
 229   each Rx data frame.
 230 
 231   I current use four transmit buffers starting at TX_BUF_START (0x0100), and
 232   use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
 233 
 234   */
 235 
 236 static short init_words[] = {
 237         0x0000,                                 /* Set bus size to 16 bits. */
 238         0x0000,0x0000,                  /* Set control mailbox (SCB) addr. */
 239         0,0,                                    /* pad to 0x000000. */
 240         0x0001,                                 /* Status word that's cleared when init is done. */
 241         0x0008,0,0,                             /* SCB offset, (skip, skip) */
 242 
 243         0,0xf000|RX_START|CUC_START,    /* SCB status and cmd. */
 244         CONFIG_CMD,                             /* Command list pointer, points to Configure. */
 245         RX_BUF_START,                           /* Rx block list. */
 246         0,0,0,0,                                /* Error count: CRC, align, buffer, overrun. */
 247 
 248         /* 0x0018: Configure command.  Change to put MAC data with packet. */
 249         0, CmdConfigure,                /* Status, command.             */
 250         SET_SA_CMD,                             /* Next command is Set Station Addr. */
 251         0x0804,                                 /* "4" bytes of config data, 8 byte FIFO. */
 252         0x2e40,                                 /* Magic values, including MAC data location. */
 253         0,                                              /* Unused pad word. */
 254 
 255         /* 0x0024: Setup station address command. */
 256         0, CmdSASetup,
 257         SET_MC_CMD,                             /* Next command. */
 258         0xaa00,0xb000,0x0bad,   /* Station address (to be filled in) */
 259 
 260         /* 0x0030: NOP, looping back to itself.  Point to first Tx buffer to Tx. */
 261         0, CmdNOp, IDLELOOP, 0 /* pad */,
 262 
 263         /* 0x0038: A unused Time-Domain Reflectometer command. */
 264         0, CmdTDR, IDLELOOP, 0,
 265 
 266         /* 0x0040: An unused Dump State command. */
 267         0, CmdDump, IDLELOOP, DUMP_DATA,
 268 
 269         /* 0x0048: An unused Diagnose command. */
 270         0, CmdDiagnose, IDLELOOP,
 271 
 272         /* 0x004E: An empty set-multicast-list command. */
 273 #ifdef initial_text_tx
 274         0, CmdMulticastList, DUMP_DATA, 0,
 275 #else
 276         0, CmdMulticastList, IDLELOOP, 0,
 277 #endif
 278 
 279         /* 0x0056: A continuous transmit command, only here for testing. */
 280         0, CmdTx, DUMP_DATA, DUMP_DATA+8, 0x803ff, -1, DUMP_DATA, 0,
 281 };
 282 
 283 /* Index to functions, as function prototypes. */
 284 
 285 extern int express_probe(struct device *dev);   /* Called from Space.c */
 286 
 287 static int      eexp_probe1(struct device *dev, short ioaddr);
 288 static int      eexp_open(struct device *dev);
 289 static int      eexp_send_packet(struct sk_buff *skb, struct device *dev);
 290 static void     eexp_interrupt(int reg_ptr);
 291 static void eexp_rx(struct device *dev);
 292 static int      eexp_close(struct device *dev);
 293 static struct enet_statistics *eexp_get_stats(struct device *dev);
 294 #ifdef HAVE_MULTICAST
 295 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 296 #endif
 297 
 298 static int read_eeprom(int ioaddr, int location);
 299 static void hardware_send_packet(struct device *dev, void *buf, short length);
 300 static void init_82586_mem(struct device *dev);
 301 static void init_rx_bufs(struct device *dev);
 302 
 303 
 304 /* Check for a network adaptor of this type, and return '0' iff one exists.
 305    If dev->base_addr == 0, probe all likely locations.
 306    If dev->base_addr == 1, always return failure.
 307    If dev->base_addr == 2, (detachable devices only) alloate space for the
 308    device and return success.
 309    */
 310 int
 311 express_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313         /* Don't probe all settable addresses, 0x[23][0-7]0, just common ones. */
 314         int *port, ports[] = {0x300, 0x270, 0x320, 0x340, 0};
 315         int base_addr = dev->base_addr;
 316 
 317         if (base_addr > 0x1ff)  /* Check a single specified location. */
 318                 return eexp_probe1(dev, base_addr);
 319         else if (base_addr > 0)
 320                 return ENXIO;           /* Don't probe at all. */
 321 
 322         for (port = &ports[0]; *port; port++) {
 323                 short id_addr = *port + ID_PORT;
 324                 unsigned short sum = 0;
 325                 int i;
 326 #ifdef notdef
 327                 for (i = 16; i > 0; i--)
 328                         sum += inb(id_addr);
 329 #else
 330                 for (i = 4; i > 0; i--) {
 331                         short id_val = inb(id_addr);
 332                         sum |= (id_val >> 4) << ((id_val & 3) << 2);
 333                 }
 334 #endif
 335                 if (sum == 0xbaba
 336                         && eexp_probe1(dev, *port) == 0)
 337                         return 0;
 338         }
 339 
 340         return ENODEV;                  /* ENODEV would be more accurate. */
 341 }
 342 
 343 int eexp_probe1(struct device *dev, short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345         unsigned short station_addr[3];
 346         int i;
 347 
 348         printk("%s: EtherExpress at %#x,", dev->name, ioaddr);
 349 
 350         /* The station address is stored !backwards! in the EEPROM, reverse
 351            after reading.  (Hmmm, a little brain-damage there at Intel, eh?) */
 352         station_addr[0] = read_eeprom(ioaddr, 2);
 353         station_addr[1] = read_eeprom(ioaddr, 3);
 354         station_addr[2] = read_eeprom(ioaddr, 4);
 355 
 356         /* Check the first three octets of the S.A. for the manufactor's code. */ 
 357         if (station_addr[2] != 0x00aa || (station_addr[1] & 0xff00) != 0x0000) {
 358                 printk(" rejected (invalid address %04x%04x%04x).\n",
 359                            station_addr[2], station_addr[1], station_addr[0]);
 360                 return ENODEV;
 361         }
 362 
 363         /* We've committed to using the board, and can start filling in *dev. */
 364         snarf_region(ioaddr, 16);
 365         dev->base_addr = ioaddr;
 366 
 367         for (i = 0; i < 6; i++) {
 368                 dev->dev_addr[i] = ((unsigned char*)station_addr)[5-i];
 369                 printk(" %02x", dev->dev_addr[i]);
 370         }
 371 
 372         /* There is no reason for the driver to care, but I print out the
 373            interface to minimize bogus bug reports. */
 374         {
 375                 char irqmap[] = {0, 9, 3, 4, 5, 10, 11, 0};
 376                 char *ifmap[] = {"AUI", "BNC", "10baseT"};
 377                 enum iftype {AUI=0, BNC=1, TP=2};
 378                 unsigned short setupval = read_eeprom(ioaddr, 0);
 379 
 380                 dev->irq = irqmap[setupval >> 13];
 381                 dev->if_port = (setupval & 0x1000) == 0 ? AUI :
 382                         read_eeprom(ioaddr, 5) & 0x1 ? TP : BNC;
 383                 printk(", IRQ %d, Interface %s.\n", dev->irq, ifmap[dev->if_port]);
 384                 /* Release the IRQ line so that it can be shared if we don't use the
 385                    ethercard. */
 386                 outb(0x00, ioaddr + SET_IRQ);
 387         }
 388 
 389         /* It's now OK to leave the board in reset, pending the open(). */
 390         outb(ASIC_RESET, ioaddr + EEPROM_Ctrl);
 391 
 392         if ((dev->mem_start & 0xf) > 0)
 393                 net_debug = dev->mem_start & 7;
 394 
 395         if (net_debug)
 396                 printk(version);
 397 
 398         /* Initialize the device structure. */
 399         dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
 400         memset(dev->priv, 0, sizeof(struct net_local));
 401 
 402         dev->open               = eexp_open;
 403         dev->stop               = eexp_close;
 404         dev->hard_start_xmit = eexp_send_packet;
 405         dev->get_stats  = eexp_get_stats;
 406 #ifdef HAVE_MULTICAST
 407         dev->set_multicast_list = &set_multicast_list;
 408 #endif
 409 
 410         /* Fill in the fields of the device structure with ethernet-generic values.
 411            This should be in a common file instead of per-driver.  */
 412         for (i = 0; i < DEV_NUMBUFFS; i++)
 413                 dev->buffs[i] = NULL;
 414 
 415         dev->hard_header        = eth_header;
 416         dev->add_arp    = eth_add_arp;
 417         dev->queue_xmit = dev_queue_xmit;
 418         dev->rebuild_header = eth_rebuild_header;
 419         dev->type_trans = eth_type_trans;
 420 
 421         dev->type               = ARPHRD_ETHER;
 422         dev->hard_header_len = ETH_HLEN;
 423         dev->mtu                = 1500; /* eth_mtu */
 424         dev->addr_len   = ETH_ALEN;
 425         for (i = 0; i < ETH_ALEN; i++) {
 426                 dev->broadcast[i]=0xff;
 427         }
 428 
 429         /* New-style flags. */
 430         dev->flags              = IFF_BROADCAST;
 431         dev->family             = AF_INET;
 432         dev->pa_addr    = 0;
 433         dev->pa_brdaddr = 0;
 434         dev->pa_mask    = 0;
 435         dev->pa_alen    = sizeof(unsigned long);
 436 
 437         return 0;
 438 }
 439 
 440 
 441 /* Reverse IRQ map: the value to put in the SET_IRQ reg. for IRQ<index>. */
 442 static char irqrmap[]={0,0,1,2,3,4,0,0,0,1,5,6,0,0,0,0};
 443 
 444 static int
 445 eexp_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 446 {
 447         int ioaddr = dev->base_addr;
 448 
 449         if (dev->irq == 0  ||  irqrmap[dev->irq] == 0)
 450                 return -ENXIO;
 451 
 452         if (irq2dev_map[dev->irq] != 0
 453                 /* This is always true, but avoid the false IRQ. */
 454                 || (irq2dev_map[dev->irq] = dev) == 0
 455                 || request_irq(dev->irq, &eexp_interrupt)) {
 456                 return -EAGAIN;
 457         }
 458 
 459         /* Initialize the 82586 memory and start it. */
 460         init_82586_mem(dev);
 461 
 462         /* Enable the interrupt line. */
 463         outb(irqrmap[dev->irq] | 0x08, ioaddr + SET_IRQ);
 464 
 465         dev->tbusy = 0;
 466         dev->interrupt = 0;
 467         dev->start = 1;
 468         return 0;
 469 }
 470 
 471 static int
 472 eexp_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 473 {
 474         struct net_local *lp = (struct net_local *)dev->priv;
 475         int ioaddr = dev->base_addr;
 476 
 477         if (dev->tbusy) {
 478                 /* If we get here, some higher level has decided we are broken.
 479                    There should really be a "kick me" function call instead. */
 480                 int tickssofar = jiffies - dev->trans_start;
 481                 if (tickssofar < 5)
 482                         return 1;
 483                 if (net_debug > 1)
 484                         printk("%s: transmit timed out, %s?  ", dev->name,
 485                                    inw(ioaddr+SCB_STATUS) & 0x8000 ? "IRQ conflict" :
 486                                    "network cable problem");
 487                 lp->stats.tx_errors++;
 488                 /* Try to restart the adaptor. */
 489                 if (lp->last_restart == lp->stats.tx_packets) {
 490                         if (net_debug > 1) printk("Resetting board.\n");
 491                         /* Completely reset the adaptor. */
 492                         init_82586_mem(dev);
 493                 } else {
 494                         /* Issue the channel attention signal and hope it "gets better". */
 495                         if (net_debug > 1) printk("Kicking board.\n");
 496                         outw(0xf000|CUC_START|RX_START, ioaddr + SCB_CMD);
 497                         outb(0, ioaddr + SIGNAL_CA);
 498                         lp->last_restart = lp->stats.tx_packets;
 499                 }
 500                 dev->tbusy=0;
 501                 dev->trans_start = jiffies;
 502         }
 503 
 504         /* If some higher layer thinks we've missed an tx-done interrupt
 505            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 506            itself. */
 507         if (skb == NULL) {
 508                 dev_tint(dev);
 509                 return 0;
 510         }
 511 
 512         /* For ethernet, fill in the header.  This should really be done by a
 513            higher level, rather than duplicated for each ethernet adaptor. */
 514         if (!skb->arp  &&  dev->rebuild_header(skb+1, dev)) {
 515                 skb->dev = dev;
 516                 arp_queue (skb);
 517                 return 0;
 518         }
 519         skb->arp=1;
 520 
 521         /* Block a timer-based transmit from overlapping. */
 522         if (set_bit(0, (void*)&dev->tbusy) != 0)
 523                 printk("%s: Transmitter access conflict.\n", dev->name);
 524         else {
 525                 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
 526                 unsigned char *buf = (void *)(skb+1);
 527 
 528                 /* Disable the 82586's input to the interrupt line. */
 529                 outb(irqrmap[dev->irq], ioaddr + SET_IRQ);
 530                 hardware_send_packet(dev, buf, length);
 531                 dev->trans_start = jiffies;
 532                 /* Enable the 82586 interrupt input. */
 533                 outb(0x08 | irqrmap[dev->irq], ioaddr + SET_IRQ);
 534         }
 535 
 536         if (skb->free)
 537                 kfree_skb (skb, FREE_WRITE);
 538 
 539         /* You might need to clean up and record Tx statistics here. */
 540         lp->stats.tx_aborted_errors++;
 541 
 542         return 0;
 543 }
 544 
 545 /*      The typical workload of the driver:
 546         Handle the network interface interrupts. */
 547 static void
 548 eexp_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 549 {
 550         int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 551         struct device *dev = (struct device *)(irq2dev_map[irq]);
 552         struct net_local *lp;
 553         int ioaddr, status, boguscount = 0;
 554         short ack_cmd = 0;
 555         
 556         if (dev == NULL) {
 557                 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
 558                 return;
 559         }
 560         dev->interrupt = 1;
 561         
 562         ioaddr = dev->base_addr;
 563         lp = (struct net_local *)dev->priv;
 564         
 565         status = inw(ioaddr + SCB_STATUS);
 566         
 567     if (net_debug > 4) {
 568                 printk("%s: EExp interrupt, status %4.4x.\n", dev->name, status);
 569     }
 570 
 571         /* Disable the 82586's input to the interrupt line. */
 572         outb(irqrmap[dev->irq], ioaddr + SET_IRQ);
 573 
 574         /* Reap the Tx packet buffers. */
 575         while (lp->tx_reap != lp->tx_head) {    /* if (status & 0x8000) */
 576                 unsigned short tx_status;
 577                 outw(lp->tx_reap, ioaddr + READ_PTR);
 578                 tx_status = inw(ioaddr);
 579                 if (tx_status == 0) {
 580                         if (net_debug > 5)  printk("Couldn't reap %#x.\n", lp->tx_reap);
 581                         break;
 582                 }
 583                 if (tx_status & 0x2000) {
 584                         lp->stats.tx_packets++;
 585                         lp->stats.collisions += tx_status & 0xf;
 586                         dev->tbusy = 0;
 587                         mark_bh(INET_BH);       /* Inform upper layers. */
 588                 } else {
 589                         lp->stats.tx_errors++;
 590                         if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
 591                         if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
 592                         if (!(tx_status & 0x0040))  lp->stats.tx_heartbeat_errors++;
 593                         if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
 594                 }
 595                 if (net_debug > 5)
 596                         printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
 597                 lp->tx_reap += TX_BUF_SIZE;
 598                 if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
 599                         lp->tx_reap = TX_BUF_START;
 600                 if (++boguscount > 4)
 601                         break;
 602         }
 603 
 604         if (status & 0x4000) { /* Packet received. */
 605                 if (net_debug > 5)
 606                         printk("Received packet, rx_head %04x.\n", lp->rx_head);
 607                 eexp_rx(dev);
 608         }
 609 
 610         /* Acknowledge the interrupt sources. */
 611         ack_cmd = status & 0xf000;
 612 
 613         if ((status & 0x0700) != 0x0200  &&  dev->start) {
 614                 if (net_debug)
 615                         printk("%s: Command unit stopped, status %04x, restarting.\n",
 616                                    dev->name, status);
 617                 /* If this ever occurs we should really re-write the idle loop, reset
 618                    the Tx list, and do a complete restart of the command unit.
 619                    For now we rely on the Tx timeout if the resume doesn't work. */
 620                 ack_cmd |= CUC_RESUME;
 621         }
 622 
 623         if ((status & 0x0070) != 0x0040  &&  dev->start) {
 624                 short saved_write_ptr = inw(ioaddr + WRITE_PTR);
 625                 /* The Rx unit is not ready, it must be hung.  Restart the receiver by
 626                    initializing the rx buffers, and issuing an Rx start command. */
 627                 if (net_debug)
 628                         printk("%s: Rx unit stopped, status %04x, restarting.\n",
 629                                    dev->name, status);
 630                 init_rx_bufs(dev);
 631                 outw(RX_BUF_START, SCB_RFA);
 632                 outw(saved_write_ptr, ioaddr + WRITE_PTR);
 633                 ack_cmd |= RX_START;
 634         }
 635 
 636         outw(ack_cmd, ioaddr + SCB_CMD);
 637         outb(0, ioaddr + SIGNAL_CA);
 638 
 639     if (net_debug > 5) {
 640                 printk("%s: EExp exiting interrupt, status %4.4x.\n", dev->name,
 641                            inw(ioaddr + SCB_CMD));
 642     }
 643         /* Enable the 82586's input to the interrupt line. */
 644         outb(irqrmap[dev->irq] | 0x08, ioaddr + SET_IRQ);
 645         return;
 646 }
 647 
 648 static int
 649 eexp_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 650 {
 651         int ioaddr = dev->base_addr;
 652 
 653         dev->tbusy = 1;
 654         dev->start = 0;
 655 
 656         /* Flush the Tx and disable Rx. */
 657         outw(RX_SUSPEND | CUC_SUSPEND, ioaddr + SCB_CMD);
 658         outb(0, ioaddr + SIGNAL_CA);
 659 
 660         /* Disable the physical interrupt line. */
 661         outb(0, ioaddr + SET_IRQ);
 662 
 663         free_irq(dev->irq);
 664 
 665         irq2dev_map[dev->irq] = 0;
 666 
 667         /* Update the statistics here. */
 668 
 669         return 0;
 670 }
 671 
 672 /* Get the current statistics.  This may be called with the card open or
 673    closed. */
 674 static struct enet_statistics *
 675 eexp_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 676 {
 677         struct net_local *lp = (struct net_local *)dev->priv;
 678 
 679         /* ToDo: decide if there are any useful statistics from the SCB. */
 680 
 681         return &lp->stats;
 682 }
 683 
 684 #ifdef HAVE_MULTICAST
 685 /* Set or clear the multicast filter for this adaptor.
 686    num_addrs == -1      Promiscuous mode, receive all packets
 687    num_addrs == 0       Normal mode, clear multicast list
 688    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 689                         best-effort filtering.
 690  */
 691 static void
 692 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 693 {
 694         short ioaddr = dev->base_addr;
 695         if (num_addrs < 0) {
 696                 /* Not written yet, this requires expanding the init_words config
 697                    cmd. */
 698         } else if (num_addrs > 0) {
 699                 /* Fill in the SET_MC_CMD with the number of address bytes, followed
 700                    by the list of multicast addresses to be accepted. */
 701                 outw(SET_MC_CMD + 6, ioaddr + WRITE_PTR);
 702                 outw(num_addrs * 6, ioaddr);
 703                 outsw(ioaddr, addrs, num_addrs*3);              /* 3 = addr len in words */
 704                 /* We must trigger a whole 586 reset due to a bug. */
 705         } else {
 706                 /* Not written yet, this requires expanding the init_words config
 707                    cmd. */
 708                 outw(99, ioaddr);               /* Disable promiscuous mode, use normal mode */
 709         }
 710 }
 711 #endif
 712 
 713 /* The horrible routine to read a word from the serial EEPROM. */
 714 
 715 /* The delay between EEPROM clock transitions. */
 716 #define eeprom_delay()  { int _i = 40; while (--_i > 0) { __SLOW_DOWN_IO; }}
 717 #define EE_READ_CMD (6 << 6)
 718 
 719 int
 720 read_eeprom(int ioaddr, int location)
     /* [previous][next][first][last][top][bottom][index][help] */
 721 {
 722         int i;
 723         unsigned short retval = 0;
 724         short ee_addr = ioaddr + EEPROM_Ctrl;
 725         int read_cmd = location | EE_READ_CMD;
 726         short ctrl_val = EE_CS | _586_RESET;
 727         
 728         outb(ctrl_val, ee_addr);
 729         
 730         /* Shift the read command bits out. */
 731         for (i = 8; i >= 0; i--) {
 732                 short outval = (read_cmd & (1 << i)) ? ctrl_val | EE_DATA_WRITE
 733                         : ctrl_val;
 734                 outb(outval, ee_addr);
 735                 outb(outval | EE_SHIFT_CLK, ee_addr);   /* EEPROM clock tick. */
 736                 eeprom_delay();
 737                 outb(outval, ee_addr);  /* Finish EEPROM a clock tick. */
 738                 eeprom_delay();
 739         }
 740         outb(ctrl_val, ee_addr);
 741         
 742         for (i = 16; i > 0; i--) {
 743                 outb(ctrl_val | EE_SHIFT_CLK, ee_addr);  eeprom_delay();
 744                 retval = (retval << 1) | ((inb(ee_addr) & EE_DATA_READ) ? 1 : 0);
 745                 outb(ctrl_val, ee_addr);  eeprom_delay();
 746         }
 747 
 748         /* Terminate the EEPROM access. */
 749         ctrl_val &= ~EE_CS;
 750         outb(ctrl_val | EE_SHIFT_CLK, ee_addr);
 751         eeprom_delay();
 752         outb(ctrl_val, ee_addr);
 753         eeprom_delay();
 754         return retval;
 755 }
 756 
 757 static void
 758 init_82586_mem(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 759 {
 760         struct net_local *lp = (struct net_local *)dev->priv;
 761         short ioaddr = dev->base_addr;
 762 
 763         /* Enable loopback to protect the wire while starting up.
 764            This is Superstition From Crynwr. */
 765         outb(inb(ioaddr + Config) | 0x02, ioaddr + Config);
 766 
 767         /* Hold the 586 in reset during the memory initialization. */
 768         outb(_586_RESET, ioaddr + EEPROM_Ctrl);
 769 
 770         /* Place the write pointer at 0xfff6 (address-aliased to 0xfffff6). */
 771         outw(0xfff6, ioaddr + WRITE_PTR);
 772         outsw(ioaddr, init_words, sizeof(init_words)>>1);
 773 
 774         /* Fill in the station address. */
 775         outw(SA_OFFSET, ioaddr + WRITE_PTR);
 776         outsw(ioaddr, dev->dev_addr, 3);
 777 
 778         /* The Tx-block list is written as needed.  We just set up the values. */
 779 #ifdef initial_text_tx
 780         lp->tx_cmd_link = DUMP_DATA + 4;
 781 #else
 782         lp->tx_cmd_link = IDLELOOP + 4;
 783 #endif
 784         lp->tx_head = lp->tx_reap = TX_BUF_START;
 785 
 786         init_rx_bufs(dev);
 787 
 788         /* Start the 586 by releasing the reset line. */
 789         outb(0x00, ioaddr + EEPROM_Ctrl);
 790 
 791         /* This was time consuming to track down: you need to give two channel
 792            attention signals to reliably start up the i82586. */
 793         outb(0, ioaddr + SIGNAL_CA);
 794 
 795         {
 796                 int boguscnt = 50;
 797                 while (inw(ioaddr + SCB_STATUS) == 0)
 798                         if (--boguscnt == 0) {
 799                                 printk("%s: i82586 initialization timed out with status %04x, cmd %04x.\n",
 800                                            dev->name, inw(ioaddr + SCB_STATUS), inw(ioaddr + SCB_CMD));
 801                                 break;
 802                         }
 803                 /* Issue channel-attn -- the 82586 won't start without it. */
 804                 outb(0, ioaddr + SIGNAL_CA);
 805         }
 806 
 807         /* Disable loopback. */
 808         outb(inb(ioaddr + Config) & ~0x02, ioaddr + Config);
 809         if (net_debug > 4)
 810                 printk("%s: Initialized 82586, status %04x.\n", dev->name,
 811                            inw(ioaddr + SCB_STATUS));
 812         return;
 813 }
 814 
 815 /* Initialize the Rx-block list. */
 816 static void
 817 init_rx_bufs(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 818 {
 819         struct net_local *lp = (struct net_local *)dev->priv;
 820         short ioaddr = dev->base_addr;
 821 
 822         int cur_rxbuf = lp->rx_head = RX_BUF_START;
 823         
 824         /* Initialize each Rx frame + data buffer. */
 825         do {    /* While there is room for one more. */
 826                 outw(cur_rxbuf, ioaddr + WRITE_PTR);
 827                 outw(0x0000, ioaddr);                           /* Status */
 828                 outw(0x0000, ioaddr);                           /* Command */
 829                 outw(cur_rxbuf + RX_BUF_SIZE, ioaddr); /* Link */
 830                 outw(cur_rxbuf + 22, ioaddr);           /* Buffer offset */
 831                 outw(0x0000, ioaddr);                           /* Pad for dest addr. */
 832                 outw(0x0000, ioaddr);
 833                 outw(0x0000, ioaddr);
 834                 outw(0x0000, ioaddr);                           /* Pad for source addr. */
 835                 outw(0x0000, ioaddr);
 836                 outw(0x0000, ioaddr);
 837                 outw(0x0000, ioaddr);                           /* Pad for protocol. */
 838                 
 839                 outw(0x0000, ioaddr);                           /* Buffer: Actual count */
 840                 outw(-1, ioaddr);                                       /* Buffer: Next (none). */
 841                 outw(cur_rxbuf + 0x20, ioaddr);         /* Buffer: Address low */
 842                 outw(0x0000, ioaddr);
 843                 /* Finally, the number of bytes in the buffer. */
 844                 outw(0x8000 + RX_BUF_SIZE-0x20, ioaddr);
 845                 
 846                 lp->rx_tail = cur_rxbuf;
 847                 cur_rxbuf += RX_BUF_SIZE;
 848         } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
 849         
 850         /* Terminate the list by setting the EOL bit, and wrap the pointer to make
 851            the list a ring. */
 852         outw(lp->rx_tail + 2, ioaddr + WRITE_PTR);
 853         outw(0xC000, ioaddr);                                   /* Command, mark as last. */
 854         outw(lp->rx_head, ioaddr);                              /* Link */
 855 }
 856 
 857 static void
 858 hardware_send_packet(struct device *dev, void *buf, short length)
     /* [previous][next][first][last][top][bottom][index][help] */
 859 {
 860         struct net_local *lp = (struct net_local *)dev->priv;
 861         short ioaddr = dev->base_addr;
 862         short tx_block = lp->tx_head;
 863 
 864         /* Set the write pointer to the Tx block, and put out the header. */
 865         outw(tx_block, ioaddr + WRITE_PTR);
 866         outw(0x0000, ioaddr);           /* Tx status */
 867         outw(CMD_INTR|CmdTx, ioaddr);           /* Tx command */
 868         outw(tx_block+16, ioaddr);      /* Next command is a NoOp. */
 869         outw(tx_block+8, ioaddr);       /* Data Buffer offset. */
 870 
 871         /* Output the data buffer descriptor. */
 872         outw(length | 0x8000, ioaddr); /* Byte count parameter. */
 873         outw(-1, ioaddr);                       /* No next data buffer. */
 874         outw(tx_block+22, ioaddr);      /* Buffer follows the NoOp command. */
 875         outw(0x0000, ioaddr);           /* Buffer address high bits (always zero). */
 876 
 877         /* Output the Loop-back NoOp command. */
 878         outw(0x0000, ioaddr);           /* Tx status */
 879         outw(CmdNOp, ioaddr);           /* Tx command */
 880         outw(tx_block+16, ioaddr);      /* Next is myself. */
 881 
 882         /* Output the packet using the write pointer.
 883            Hmmm, it feels a little like a 3c501! */
 884         outsw(ioaddr + DATAPORT, buf, (length + 1) >> 1);
 885 
 886         /* Set the old command link pointing to this send packet. */
 887         outw(lp->tx_cmd_link, ioaddr + WRITE_PTR);
 888         outw(tx_block, ioaddr);
 889         lp->tx_cmd_link = tx_block + 20;
 890 
 891         /* Set the next free tx region. */
 892         lp->tx_head = tx_block + TX_BUF_SIZE;
 893         if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
 894                 lp->tx_head = TX_BUF_START;
 895 
 896     if (net_debug > 4) {
 897                 printk("%s: EExp @%x send length = %d, tx_block %3x, next %3x, "
 898                            "reap %4x status %4.4x.\n", dev->name, ioaddr, length,
 899                            tx_block, lp->tx_head, lp->tx_reap, inw(ioaddr + SCB_STATUS));
 900     }
 901 
 902         if (lp->tx_head != lp->tx_reap)
 903                 dev->tbusy = 0;
 904 }
 905 
 906 static void
 907 eexp_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 908 {
 909         struct net_local *lp = (struct net_local *)dev->priv;
 910         short ioaddr = dev->base_addr;
 911         short saved_write_ptr = inw(ioaddr + WRITE_PTR);
 912         short rx_head = lp->rx_head;
 913         short rx_tail = lp->rx_tail;
 914         short boguscount = 10;
 915         short frame_status;
 916 
 917         /* Set the read pointer to the Rx frame. */
 918         outw(rx_head, ioaddr + READ_PTR);
 919         while ((frame_status = inw(ioaddr)) < 0) {              /* Command complete */
 920                 short rfd_cmd = inw(ioaddr);
 921                 short next_rx_frame = inw(ioaddr);
 922                 short data_buffer_addr = inw(ioaddr);
 923                 short pkt_len;
 924                 
 925                 /* Set the read pointer the data buffer. */
 926                 outw(data_buffer_addr, ioaddr + READ_PTR);
 927                 pkt_len = inw(ioaddr);
 928 
 929                 if (rfd_cmd != 0  ||  data_buffer_addr != rx_head + 22
 930                         ||  pkt_len & 0xC000 != 0xC000) {
 931                         printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
 932                                    "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
 933                                    frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
 934                                    pkt_len);
 935                 } else if ((frame_status & 0x2000) == 0) {
 936                         /* Frame Rxed, but with error. */
 937                         lp->stats.rx_errors++;
 938                         if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
 939                         if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
 940                         if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
 941                         if (frame_status & 0x0100) lp->stats.rx_over_errors++;
 942                         if (frame_status & 0x0080) lp->stats.rx_length_errors++;
 943                 } else {
 944                         /* Malloc up new buffer. */
 945                         int sksize;
 946                         struct sk_buff *skb;
 947 
 948                         pkt_len &= 0x3fff;
 949                         sksize = sizeof(struct sk_buff) + pkt_len;
 950                         skb = alloc_skb(sksize, GFP_ATOMIC);
 951                         if (skb == NULL) {
 952                                 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
 953                                 lp->stats.rx_dropped++;
 954                                 break;
 955                         }
 956                         skb->mem_len = sksize;
 957                         skb->mem_addr = skb;
 958                         skb->len = pkt_len;
 959                         skb->dev = dev;
 960 
 961                         outw(data_buffer_addr + 10, ioaddr + READ_PTR);
 962 
 963                         insw(ioaddr, (void *)(skb+1), (pkt_len + 1) >> 1);
 964                 
 965 #ifdef HAVE_NETIF_RX
 966                         netif_rx(skb);
 967 #else
 968                         skb->lock = 0;
 969                         if (dev_rint((unsigned char*)skb, pkt_len, IN_SKBUFF, dev) != 0) {
 970                                 kfree_s(skb, sksize);
 971                                 lp->stats.rx_dropped++;
 972                                 break;
 973                         }
 974 #endif
 975                         lp->stats.rx_packets++;
 976                 }
 977 
 978                 /* Clear the status word and set End-of-List on the rx frame. */
 979                 outw(rx_head, ioaddr + WRITE_PTR);
 980                 outw(0x0000, ioaddr);
 981                 outw(0xC000, ioaddr);
 982 #ifndef final_version
 983                 if (next_rx_frame != rx_head + RX_BUF_SIZE
 984                         && next_rx_frame != RX_BUF_START) {
 985                         printk("%s: Rx next frame at %#x is %#x instead of %#x.\n", dev->name,
 986                                    rx_head, next_rx_frame, rx_head + RX_BUF_SIZE);
 987                         next_rx_frame = rx_head + RX_BUF_SIZE;
 988                         if (next_rx_frame >= RX_BUF_END - RX_BUF_SIZE) 
 989                                 next_rx_frame = RX_BUF_START;
 990                 }
 991 #endif
 992                 outw(rx_tail+2, ioaddr + WRITE_PTR);
 993                 outw(0x0000, ioaddr);   /* Clear the end-of-list on the prev. RFD. */
 994 
 995 #ifndef final_version
 996                 outw(rx_tail+4, ioaddr + READ_PTR);
 997                 if (inw(ioaddr) != rx_head) {
 998                         printk("%s: Rx buf link mismatch, at %04x link %04x instead of %04x.\n",
 999                                    dev->name, rx_tail, (outw(rx_tail+4, ioaddr + READ_PTR),inw(ioaddr)),
1000                                    rx_head);
1001                         outw(rx_head, ioaddr);
1002                 }
1003 #endif
1004 
1005                 rx_tail = rx_head;
1006                 rx_head = next_rx_frame;
1007                 if (--boguscount == 0)
1008                         break;
1009                 outw(rx_head, ioaddr + READ_PTR);
1010         }
1011         
1012         lp->rx_head = rx_head;
1013         lp->rx_tail = rx_tail;
1014         
1015         /* Restore the original write pointer. */
1016         outw(saved_write_ptr, ioaddr + WRITE_PTR);
1017 }
1018 
1019 /*
1020  * Local variables:
1021  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -I/usr/src/linux/drivers/net -Wall -Wstrict-prototypes -O6 -m486 -c eexpress.c"
1022  *  version-control: t
1023  *  kept-new-versions: 5
1024  *  tab-width: 4
1025  * End:
1026  */

/* [previous][next][first][last][top][bottom][index][help] */