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

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