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

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