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
  14. init_module
  15. cleanup_module

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

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