home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / drivers / net / eexpress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-06  |  32.4 KB  |  1,049 lines

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