root/net/inet/ne.c

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

DEFINITIONS

This source file includes following definitions.
  1. neprobe
  2. neprobe1
  3. ne_reset_8390
  4. ne_block_input
  5. ne_block_output

   1 /* ne.c: A general non-shared-memory NS8390 ethernet driver for linux. */
   2 /*
   3     Written 1992,1993 by Donald Becker. This is alpha test code.
   4     Copyright 1993 United States Government as represented by the
   5     Director, National Security Agency.  This software may be used and
   6     distributed according to the terms of the GNU Public License,
   7     incorporated herein by reference.
   8     
   9     This driver should work with many 8390-based ethernet boards.  Currently
  10     it support the NE1000, NE2000 (and clones), and some Cabletron products.
  11 
  12     The Author may be reached as becker@super.org or
  13     C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  14 */
  15 
  16 /* Routines for the NatSemi-based designs (NE[12]000). */
  17 
  18 static char *version =
  19     "ne.c:v0.99-10 5/28/93 Donald Becker (becker@super.org)\n";
  20 
  21 #include <linux/config.h>
  22 #include <linux/kernel.h>
  23 #include <linux/sched.h>
  24 #include <asm/io.h>
  25 #include <asm/system.h>
  26 
  27 #include "dev.h"
  28 #include "8390.h"
  29 
  30 /* These should be in <asm/io.h> someday, borrowed from blk_drv/hd.c. */
  31 #define port_read(port,buf,nr) \
  32 __asm__("cld;rep;insw": :"d" (port),"D" (buf),"c" (nr):"cx","di")
  33 #define port_write(port,buf,nr) \
  34 __asm__("cld;rep;outsw": :"d" (port),"S" (buf),"c" (nr):"cx","si")
  35 
  36 #define port_read_b(port,buf,nr) \
  37 __asm__("cld;rep;insb": :"d" (port),"D" (buf),"c" (nr):"cx","di")
  38 #define port_write_b(port,buf,nr) \
  39 __asm__("cld;rep;outsb": :"d" (port),"S" (buf),"c" (nr):"cx","si")
  40 
  41 #define EN_CMD   (dev->base_addr)
  42 #define NE_BASE  (dev->base_addr)
  43 #define NE_DATAPORT     0x10    /* NatSemi-defined port window offset. */
  44 #define NE_RESET        0x1f    /* Issue a read to reset, a write to clear. */
  45 
  46 #define NE1SM_START_PG  0x20    /* First page of TX buffer */
  47 #define NE1SM_STOP_PG   0x40    /* Last page +1 of RX ring */
  48 #define NESM_START_PG   0x40    /* First page of TX buffer */
  49 #define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
  50 
  51 extern void NS8390_init(struct device *dev, int startp);
  52 extern int ei_debug;
  53 extern struct sigaction ei_sigaction;
  54 
  55 int neprobe(int ioaddr, struct device *dev);
  56 static int neprobe1(int ioaddr, struct device *dev, int verbose);
  57 
  58 static void ne_reset_8390(struct device *dev);
  59 static int ne_block_input(struct device *dev, int count,
  60                           char *buf, int ring_offset);
  61 static void ne_block_output(struct device *dev, const int count,
  62                 const unsigned char *buf, const int start_page);
  63 
  64 
  65 /*  Probe for various non-shared-memory ethercards.
  66    
  67    NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
  68    buffer memory space.  NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
  69    the SAPROM, while other supposed NE2000 clones must be detected by their
  70    SA prefix.
  71 
  72    Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
  73    mode results in doubled values, which can be detected and compansated for.
  74 
  75    The probe is also responsible for initializing the card and filling
  76    in the 'dev' and 'ei_status' structures.
  77 
  78    We use the minimum memory size for some ethercard product lines, iff we can't
  79    distinguish models.  You can increase the packet buffer size by setting
  80    PACKETBUF_MEMSIZE.  Reported Cabletron packet buffer locations are:
  81         E1010   starts at 0x100 and ends at 0x2000.
  82         E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
  83         E2010    starts at 0x100 and ends at 0x4000.
  84         E2010-x starts at 0x100 and ends at 0xffff.  */
  85 
  86 int neprobe(int ioaddr,  struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88     int *port, ports[] = {0x300, 0x280, 0x320, 0x340, 0x360, 0};
  89 
  90     if (ioaddr > 0x100)
  91         return neprobe1(ioaddr, dev, 1);
  92 
  93     for (port = &ports[0]; *port; port++)
  94         if (inb_p(*port) != 0xff && neprobe1(*port, dev, 0))
  95             return dev->base_addr = *port;
  96     return 0;
  97 }
  98 
  99 static int neprobe1(int ioaddr, struct device *dev, int verbose)
     /* [previous][next][first][last][top][bottom][index][help] */
 100 {
 101     int i;
 102     unsigned char SA_prom[32];
 103     int wordlength = 2;
 104     int neX000, ctron, dlink;
 105     
 106     
 107     if ( inb_p(ioaddr) == 0xFF) {
 108         if (verbose) printk("8390 ethercard probe at %#3x failed.\n", ioaddr);
 109         return 0;
 110     }
 111 
 112     printk("8390 ethercard probe at %#3x:", ioaddr);
 113 
 114     /* Read the 16 bytes of station address prom, returning 1 for
 115        an eight-bit interface and 2 for a 16-bit interface.
 116        We must first initialize registers, similar to NS8390_init(eifdev, 0).
 117        We can't reliably read the SAPROM address without this.
 118        (I learned the hard way!). */
 119     {
 120         struct {char value, offset; } program_seq[] = {
 121             {E8390_NODMA+E8390_PAGE0+E8390_STOP, EN_CMD}, /* Select page 0 */
 122             {0x48,      EN0_DCFG},      /* Set byte-wide (0x48) access. */
 123             {0x00,      EN0_RCNTLO},    /* Clear the count regs. */
 124             {0x00,      EN0_RCNTHI},
 125             {0x00,      EN0_IMR},       /* Mask completion irq. */
 126             {0xFF,      EN0_ISR},
 127             {E8390_RXOFF, EN0_RXCR},    /* 0x20  Set to monitor */
 128             {E8390_TXOFF, EN0_TXCR},    /* 0x02  and loopback mode. */
 129             {32,                EN0_RCNTLO},
 130             {0x00,      EN0_RCNTHI},
 131             {0x00,      EN0_RSARLO},    /* DMA starting at 0x0000. */
 132             {0x00,      EN0_RSARHI},
 133             {E8390_RREAD+E8390_START, EN_CMD},
 134         };
 135         for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
 136             outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
 137     }
 138     for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
 139         SA_prom[i] = inb_p(ioaddr + NE_DATAPORT);
 140         SA_prom[i+1] = inb_p(ioaddr + NE_DATAPORT);
 141         if (SA_prom[i] != SA_prom[i+1])
 142             wordlength = 1;
 143     }
 144     
 145     if (wordlength == 2) {
 146         /* We must set the 8390 for word mode, AND RESET IT. */
 147         int tmp;
 148         outb_p(0x49, ioaddr + EN0_DCFG);
 149         tmp = inb_p(NE_BASE + NE_RESET);
 150         ei_status.word16 = 1;
 151         outb(tmp, NE_BASE + NE_RESET);
 152         /* Un-double the SA_prom values. */
 153         for (i = 0; i < 16; i++)
 154             SA_prom[i] = SA_prom[i+i];
 155     } else
 156         ei_status.word16 = 0;
 157 
 158 #if defined(show_all_SAPROM)
 159     /* If your ethercard isn't detected define this to see the SA_PROM. */
 160     for(i = 0; i < sizeof(SA_prom); i++)
 161         printk(" %2.2x", SA_prom[i]);
 162 #else
 163     for(i = 0; i < ETHER_ADDR_LEN; i++) {
 164         dev->dev_addr[i] = SA_prom[i];
 165         printk(" %2.2x", SA_prom[i]);
 166     }
 167 #endif
 168     
 169     neX000 =  (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
 170     ctron =  (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
 171     dlink =  (SA_prom[0] == 0x00 && SA_prom[1] == 0xDE && SA_prom[2] == 0x01);
 172     
 173     /* Set up the rest of the parameters. */
 174     if (neX000 && wordlength == 2) {
 175         ei_status.name = "NE2000";
 176         ei_status.tx_start_page = NESM_START_PG;
 177         ei_status.stop_page = NESM_STOP_PG;
 178     } else if (neX000 || dlink) {
 179         ei_status.name = neX000 ? "NE1000" : "D-Link";
 180         ei_status.tx_start_page = NE1SM_START_PG;
 181         ei_status.stop_page = NE1SM_STOP_PG;
 182     } else if (ctron) {
 183         ei_status.name = "Cabletron";
 184         ei_status.tx_start_page = 0x01;
 185         ei_status.stop_page = (wordlength == 2) ? 0x40 : 0x20;
 186     } else {
 187         printk(" not found.\n");
 188         return 0;
 189     }
 190     ei_status.rx_start_page = ei_status.tx_start_page + TX_PAGES;
 191 #ifdef PACKETBUF_MEMSIZE
 192     /* Allow the packet buffer size to be overridden by know-it-alls. */
 193     ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
 194 #endif
 195     dev->base_addr = ioaddr;
 196     
 197     if (dev->irq < 2) {
 198         int nic_base = dev->base_addr;
 199         autoirq_setup(0);
 200         outb_p(0x50, nic_base + EN0_IMR);       /* Enable one interrupt. */
 201         outb_p(0x00, nic_base + EN0_RCNTLO);
 202         outb_p(0x00, nic_base + EN0_RCNTHI);
 203         outb_p(E8390_RREAD+E8390_START, nic_base); /* Trigger it... */
 204         outb_p(0x00, nic_base + EN0_IMR);               /* Mask it again. */
 205         dev->irq = autoirq_report(0);
 206         if (ei_debug > 2)
 207             printk(" autoirq is %d", dev->irq);
 208     } else if (dev->irq == 2)
 209         /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
 210            or don't know which one to set. */
 211         dev->irq = 9;
 212     
 213     /* Snarf the interrupt now.  There's no point in waiting since we cannot
 214        share and the board will usually be enabled. */
 215     { int irqval = irqaction (dev->irq, &ei_sigaction);
 216       if (irqval) {
 217           printk (" unable to get IRQ %d (irqval=%d).\n", dev->irq, irqval);
 218           return 0;
 219       }
 220   }
 221     
 222     printk("\n%s: %s found, using IRQ %d.\n",
 223            dev->name, ei_status.name, dev->irq);
 224     if (ei_debug > 1)
 225         printk(version);
 226     ei_status.reset_8390 = &ne_reset_8390;
 227     ei_status.block_input = &ne_block_input;
 228     ei_status.block_output = &ne_block_output;
 229     NS8390_init(dev, 0);
 230     return dev->base_addr;
 231 }
 232 
 233 /* Hard reset the card.  This used to pause for the same period that a
 234    8390 reset command required, but that shouldn't be necessary. */
 235 static void
 236 ne_reset_8390(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 237 {
 238     int tmp = inb_p(NE_BASE + NE_RESET);
 239     int reset_start_time = jiffies;
 240 
 241     if (ei_debug > 1) printk("resetting the 8390 t=%d...", jiffies);
 242     ei_status.txing = 0;
 243 
 244     outb_p(tmp, NE_BASE + NE_RESET);
 245     /* This check _should_not_ be necessary, omit eventually. */
 246     while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
 247         if (jiffies - reset_start_time > 2) {
 248             printk("%s: ne_reset_8390() did not complete.\n", dev->name);
 249             break;
 250         }
 251 }
 252 
 253 /* Block input and output, similar to the Crynwr packet driver.  If you
 254    porting to a new ethercard look at the packet driver source for hints.
 255    The NEx000 doesn't share it on-board packet memory -- you have to put
 256    the packet out through the "remote DMA" dataport using outb. */
 257 
 258 static int
 259 ne_block_input(struct device *dev, int count, char *buf, int ring_offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 260 {
 261     int xfer_count = count;
 262     int nic_base = dev->base_addr;
 263 
 264     if (ei_status.dmaing) {
 265         if (ei_debug > 0)
 266             printk("%s: DMAing conflict in ne_block_input."
 267                    "[DMAstat:%1x][irqlock:%1x]\n",
 268                    dev->name, ei_status.dmaing, ei_status.irqlock);
 269         return 0;
 270     }
 271     ei_status.dmaing |= 0x01;
 272     outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, EN_CMD);
 273     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
 274     outb_p(count >> 8, nic_base + EN0_RCNTHI);
 275     outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
 276     outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
 277     outb_p(E8390_RREAD+E8390_START, EN_CMD);
 278     if (ei_status.word16) {
 279       port_read(NE_BASE + NE_DATAPORT,buf,count>>1);
 280       if (count & 0x01)
 281         buf[count-1] = inb(NE_BASE + NE_DATAPORT), xfer_count++;
 282     } else {
 283         port_read_b(NE_BASE + NE_DATAPORT, buf, count);
 284     }
 285 
 286     /* This was for the ALPHA version only, but enough people have
 287        encountering problems that it is still here. */
 288     if (ei_debug > 0) {         /* DMA termination address check... */
 289         int addr, tries = 20;
 290         do {
 291             /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
 292                -- it's broken! Check the "DMA" address instead. */
 293             int high = inb_p(nic_base + EN0_RSARHI);
 294             int low = inb_p(nic_base + EN0_RSARLO);
 295             addr = (high << 8) + low;
 296             if (((ring_offset + xfer_count) & 0xff) == low)
 297                 break;
 298         } while (--tries > 0);
 299         if (tries <= 0)
 300             printk("%s: RX transfer address mismatch,"
 301                    "%#4.4x (should be) vs. %#4.4x (actual).\n",
 302                    dev->name, ring_offset + xfer_count, addr);
 303     }
 304     ei_status.dmaing &= ~0x01;
 305     return ring_offset + count;
 306 }
 307 
 308 static void
 309 ne_block_output(struct device *dev, int count,
     /* [previous][next][first][last][top][bottom][index][help] */
 310                 const unsigned char *buf, const int start_page)
 311 {
 312     int retries = 0;
 313     int nic_base = NE_BASE;
 314 
 315     /* Round the count up for word writes.  Do we need to do this?
 316        What effect will an odd byte count have on the 8390?
 317        I should check someday. */
 318     if (ei_status.word16 && (count & 0x01))
 319       count++;
 320     if (ei_status.dmaing) {
 321         if (ei_debug > 0) 
 322             printk("%s: DMAing conflict in ne_block_output."
 323                    "[DMAstat:%1x][irqlock:%1x]\n",
 324                    dev->name, ei_status.dmaing, ei_status.irqlock);
 325         return;
 326     }
 327     ei_status.dmaing |= 0x02;
 328     /* We should already be in page 0, but to be safe... */
 329     outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, EN_CMD);
 330 
 331  retry:
 332 #if defined(rw_bugfix)
 333     /* Handle the read-before-write bug the same way as the
 334        Crynwr packet driver -- the NatSemi method doesn't work.
 335        Actually this doesn't aways work either, but if you have
 336        problems with your NEx000 this is better than nothing! */
 337     outb_p(0x42, nic_base + EN0_RCNTLO);
 338     outb_p(0x00,   nic_base + EN0_RCNTHI);
 339     outb_p(0x42, nic_base + EN0_RSARLO);
 340     outb_p(0x00, nic_base + EN0_RSARHI);
 341     outb_p(E8390_RREAD+E8390_START, EN_CMD);
 342     /* Make certain that the dummy read has occured. */
 343     SLOW_DOWN_IO;
 344     SLOW_DOWN_IO;
 345     SLOW_DOWN_IO;
 346 #endif  /* rw_bugfix */
 347 
 348     /* Now the normal output. */
 349     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
 350     outb_p(count >> 8,   nic_base + EN0_RCNTHI);
 351     outb_p(0x00, nic_base + EN0_RSARLO);
 352     outb_p(start_page, nic_base + EN0_RSARHI);
 353 
 354     outb_p(E8390_RWRITE+E8390_START, EN_CMD);
 355     if (ei_status.word16) {
 356         port_write(NE_BASE + NE_DATAPORT, buf, count>>1);
 357     } else {
 358         port_write_b(NE_BASE + NE_DATAPORT, buf, count);
 359     }
 360 
 361     /* This was for the ALPHA version only, but enough people have
 362        encountering problems that it is still here. */
 363     if (ei_debug > 0) {         /* DMA termination address check... */
 364         int addr, tries = 20;
 365         do {
 366             /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
 367                -- it's broken! Check the "DMA" address instead. */
 368             int high = inb_p(nic_base + EN0_RSARHI);
 369             int low = inb_p(nic_base + EN0_RSARLO);
 370             addr = (high << 8) + low;
 371             if ((start_page << 8) + count == addr)
 372                 break;
 373         } while (--tries > 0);
 374         if (tries <= 0) {
 375             printk("%s: Packet buffer transfer address mismatch on TX,"
 376                    "%#4.4x vs. %#4.4x.\n",
 377                    dev->name, (start_page << 8) + count, addr);
 378             if (retries++ == 0)
 379                 goto retry;
 380         }
 381     }
 382     ei_status.dmaing &= ~0x02;
 383     return;
 384 }
 385 
 386 
 387 /*
 388  * Local variables:
 389  *  compile-command: "gcc -DKERNEL -Wall -O6 -fomit-frame-pointer -I/usr/src/linux/net/tcp -c ne.c"
 390  *  version-control: t
 391  *  kept-new-versions: 5
 392  * End:
 393  */

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