root/drivers/net/depca.c

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

DEFINITIONS

This source file includes following definitions.
  1. depca_probe
  2. depca_probe1
  3. depca_open
  4. depca_init_ring
  5. depca_start_xmit
  6. depca_interrupt
  7. depca_rx
  8. depca_tx
  9. depca_close
  10. LoadCSRs
  11. InitRestartDepca
  12. depca_get_stats
  13. set_multicast_list
  14. SetMulticastFilter
  15. isa_probe
  16. eisa_probe
  17. alloc_device
  18. DepcaSignature
  19. DevicePresent
  20. EISA_signature
  21. init_module
  22. cleanup_module

   1 /*  depca.c: A DIGITAL DEPCA  & EtherWORKS ethernet driver for linux.
   2 
   3     Written 1994 by David C. Davies.
   4 
   5 
   6                       Copyright 1994 David C. Davies
   7                                    and 
   8                          United States Government
   9          (as represented by the Director, National Security Agency).  
  10 
  11 
  12     This software may be used and distributed according to the terms of
  13     the GNU Public License, incorporated herein by reference.
  14 
  15     This driver is written for the Digital Equipment Corporation series
  16     of DEPCA and EtherWORKS ethernet cards:
  17 
  18         DEPCA       (the original)
  19         DE100
  20         DE101
  21         DE200 Turbo
  22         DE201 Turbo
  23         DE202 Turbo (TP BNC)
  24         DE210
  25         DE422       (EISA)
  26 
  27     The  driver has been tested on DE100, DE200 and DE202 cards  in  a
  28     relatively busy network. The DE422 has been tested a little.
  29 
  30     This  driver will NOT work   for the DE203,  DE204  and DE205 series  of
  31     cards,  since they have  a  new custom ASIC in   place of the AMD  LANCE
  32     chip. 
  33 
  34     The author may be reached as davies@wanton.lkg.dec.com or
  35     Digital Equipment Corporation, 550 King Street, Littleton MA 01460.
  36 
  37     =========================================================================
  38     The driver was based on the 'lance.c' driver from Donald Becker which is
  39     included with the standard driver distribution for linux. Modifications
  40     were made to most routines and the hardware recognition routines were
  41     written from scratch. Primary references used were:
  42 
  43     1) Lance.c code in /linux/drivers/net/
  44     2) "Ethernet/IEEE 802.3 Family. 1992 World Network Data Book/Handbook",
  45        AMD, 1992 [(800) 222-9323].
  46     3) "Am79C90 CMOS Local Area Network Controller for Ethernet (C-LANCE)",
  47        AMD, Pub. #17881, May 1993.
  48     4) "Am79C960 PCnet-ISA(tm), Single-Chip Ethernet Controller for ISA",
  49        AMD, Pub. #16907, May 1992
  50     5) "DEC EtherWORKS LC Ethernet Controller Owners Manual",
  51        Digital Equipment corporation, 1990, Pub. #EK-DE100-OM.003
  52     6) "DEC EtherWORKS Turbo Ethernet Controller Owners Manual",
  53        Digital Equipment corporation, 1990, Pub. #EK-DE200-OM.003
  54     7) "DEPCA Hardware Reference Manual", Pub. #EK-DEPCA-PR
  55        Digital Equipment Corporation, 1989
  56     8) "DEC EtherWORKS Turbo_(TP BNC) Ethernet Controller Owners Manual",
  57        Digital Equipment corporation, 1991, Pub. #EK-DE202-OM.001
  58     
  59     Peter Bauer's depca.c (V0.5) was referred to when debugging this driver.
  60     The hash filter code was  derived from Reference  3 and has been  tested
  61     only to the extent that the Table  A-1, page A-7,  was confirmed to fill
  62     the   filter bit   positions  correctly.  Hash   filtering  is  not  yet
  63     implemented in the current driver set.
  64 
  65     The original DEPCA  card requires that the  ethernet ROM address counter
  66     be enabled to count and has an 8 bit NICSR.  The ROM counter enabling is
  67     only  done when a  0x08 is read as the  first address octet (to minimise
  68     the chances  of writing over some  other hardware's  I/O register).  The
  69     NICSR accesses   have been changed  to  byte accesses  for all the cards
  70     supported by this driver, since there is only one  useful bit in the MSB
  71     (remote boot timeout) and it  is not used.  Also, there  is a maximum of
  72     only 48kB network  RAM for this  card.  My thanks  to Torbjorn Lindh for
  73     help debugging all this (and holding my feet to  the fire until I got it
  74     right).
  75 
  76     The DE200  series  boards have  on-board 64kB  RAM for  use  as a shared
  77     memory network  buffer. Only the DE100  cards make use  of a  2kB buffer
  78     mode which has not  been implemented in  this driver (only the 32kB  and
  79     64kB modes are supported [16kB/48kB for the original DEPCA]).
  80 
  81     At the most only 2 DEPCA cards can  be supported on  the ISA bus because
  82     there is only provision  for two I/O base addresses  on each card (0x300
  83     and 0x200). The I/O address is detected by searching for a byte sequence
  84     in the Ethernet station address PROM at the expected I/O address for the
  85     Ethernet  PROM.   The shared memory  base   address  is 'autoprobed'  by
  86     looking  for the self  test PROM  and detecting the  card name.   When a
  87     second  DEPCA is  detected,  information  is   placed in the   base_addr
  88     variable of the  next device structure (which  is created if necessary),
  89     thus  enabling ethif_probe  initialization  for the device.  More than 2
  90     EISA cards can  be  supported, but  care will  be  needed assigning  the
  91     shared memory to ensure that each slot has the  correct IRQ, I/O address
  92     and shared memory address assigned.
  93 
  94     ************************************************************************
  95 
  96     NOTE: If you are using two  ISA DEPCAs, it is  important that you assign
  97     the base memory addresses correctly.   The  driver autoprobes I/O  0x300
  98     then 0x200.  The  base memory address for  the first device must be less
  99     than that of the second so that the auto probe will correctly assign the
 100     I/O and memory addresses on the same card.  I can't think of a way to do
 101     this unambiguously at the moment, since there is nothing on the cards to
 102     tie I/O and memory information together.
 103 
 104     I am unable  to  test  2 cards   together for now,    so this  code   is
 105     unchecked. All reports, good or bad, are welcome.
 106 
 107     ************************************************************************
 108 
 109     The board IRQ   setting must be  at an  unused IRQ which  is auto-probed
 110     using Donald Becker's autoprobe routines. DEPCA and DE100 board IRQs are
 111     {2,3,4,5,7}, whereas the  DE200 is at {5,9,10,11,15}.  Note that IRQ2 is
 112     really IRQ9 in machines with 16 IRQ lines.
 113 
 114     No 16MB memory  limitation should exist with this  driver as DMA is  not
 115     used and the common memory area is in low memory on the network card (my
 116     current system has 20MB and I've not had problems yet).
 117 
 118     The ability to load this driver as a loadable module has been added. To
 119     utilise this ability, you have to do <8 things:
 120 
 121     1) copy depca.c from the  /linux/drivers/net directory to your favourite
 122     temporary directory.
 123     2) edit the  source code near  line 1530 to reflect  the I/O address and
 124     IRQ you're using.
 125     3) compile  depca.c, but include -DMODULE in  the command line to ensure
 126     that the correct bits are compiled (see end of source code).
 127     4) if you are wanting to add a new  card, goto 5. Otherwise, recompile a
 128     kernel with the depca configuration turned off and reboot.
 129     5) insmod depca.o
 130     6) run the net startup bits for your eth?? interface manually 
 131     (usually /etc/rc.inet[12] at boot time). 
 132     7) enjoy!
 133 
 134     Note that autoprobing is not allowed in loadable modules - the system is
 135     already up and running and you're messing with interrupts. Also, there
 136     is no way to check on the number of depcas installed at the moment.
 137 
 138     To unload a module, turn off the associated interface 
 139     'ifconfig eth?? down' then 'rmmod depca'.
 140 
 141     [Alan Cox: Changed to split off the module values as ints for insmod
 142      
 143      you can now do insmod depca.c irq=7 io=0x200 ]
 144      
 145 
 146     TO DO:
 147     ------
 148 
 149     1. Implement the 2k buffer mode - does anyone need it??
 150 
 151     Revision History
 152     ----------------
 153 
 154     Version   Date        Description
 155   
 156       0.1     25-jan-94   Initial writing.
 157       0.2     27-jan-94   Added LANCE TX hardware buffer chaining.
 158       0.3      1-feb-94   Added multiple DEPCA support.
 159       0.31     4-feb-94   Added DE202 recognition.
 160       0.32    19-feb-94   Tidy up. Improve multi-DEPCA support.
 161       0.33    25-feb-94   Fix DEPCA ethernet ROM counter enable.
 162                           Add jabber packet fix from murf@perftech.com
 163                           and becker@super.org
 164       0.34     7-mar-94   Fix DEPCA max network memory RAM & NICSR access.
 165       0.35     8-mar-94   Added DE201 recognition. Tidied up.
 166       0.351   30-apr-94   Added EISA support. Added DE422 recognition.
 167       0.36    16-may-94   DE422 fix released.
 168       0.37    22-jul-94   Added MODULE support
 169       0.38    15-aug-94   Added DBR ROM switch in depca_close(). 
 170                           Multi DEPCA bug fix.
 171       0.38axp 15-sep-94   Special version for Alpha AXP Linux V1.0.
 172       0.381   12-dec-94   Added DE101 recognition, fix multicast bug.
 173       0.382    9-feb-95   Fix recognition bug reported by <bkm@star.rl.ac.uk>.
 174       0.383   22-feb-95   Fix for conflict with VESA SCSI reported by
 175                           <stromain@alf.dec.com>
 176 
 177     =========================================================================
 178 */
 179 
 180 static char *version = "depca.c:v0.383 2/22/94 davies@wanton.lkg.dec.com\n";
 181 
 182 #include <linux/config.h>
 183 #ifdef MODULE
 184 #include <linux/module.h>
 185 #include <linux/version.h>
 186 #else
 187 #define MOD_INC_USE_COUNT
 188 #define MOD_DEC_USE_COUNT
 189 #endif /* MODULE */
 190 
 191 #include <linux/kernel.h>
 192 #include <linux/sched.h>
 193 #include <linux/string.h>
 194 #include <linux/ptrace.h>
 195 #include <linux/errno.h>
 196 #include <linux/ioport.h>
 197 #include <linux/malloc.h>
 198 #include <linux/interrupt.h>
 199 #include <asm/bitops.h>
 200 #include <asm/io.h>
 201 #include <asm/dma.h>
 202 
 203 #include <linux/netdevice.h>
 204 #include <linux/etherdevice.h>
 205 #include <linux/skbuff.h>
 206 
 207 #include "depca.h"
 208 
 209 #ifdef DEPCA_DEBUG
 210 static int depca_debug = DEPCA_DEBUG;
 211 #else
 212 static int depca_debug = 1;
 213 #endif
 214 
 215 #ifndef PROBE_LENGTH
 216 #define PROBE_LENGTH    32
 217 #endif
 218 
 219 #define ETH_PROM_SIG    0xAA5500FFUL
 220 
 221 #ifndef DEPCA_SIGNATURE
 222 #define DEPCA_SIGNATURE {"DEPCA",\
 223                          "DE100","DE101",\
 224                          "DE200","DE201","DE202",\
 225                          "DE210",\
 226                          "DE422",\
 227                          ""}
 228 #define DEPCA_NAME_LENGTH 8
 229 #endif
 230 
 231 #ifndef DEPCA_RAM_BASE_ADDRESSES
 232 #define DEPCA_RAM_BASE_ADDRESSES {0xc0000,0xd0000,0xe0000,0x00000}
 233 #endif
 234 static short mem_chkd = 0;               /* holds which base addrs have been */
 235                                          /* checked, for multi-DEPCA case */
 236 
 237 #ifndef DEPCA_IO_PORTS
 238 #define DEPCA_IO_PORTS {0x300, 0x200, 0}
 239 #endif
 240 
 241 #ifndef DEPCA_TOTAL_SIZE
 242 #define DEPCA_TOTAL_SIZE 0x10
 243 #endif
 244 
 245 #ifndef MAX_NUM_DEPCAS
 246 #define MAX_NUM_DEPCAS 2
 247 #endif
 248 
 249 #ifndef DEPCA_EISA_IO_PORTS 
 250 #define DEPCA_EISA_IO_PORTS 0x0c00       /* I/O port base address, slot 0 */
 251 #endif
 252 
 253 #ifndef MAX_EISA_SLOTS
 254 #define MAX_EISA_SLOTS 8
 255 #endif
 256 
 257 /*
 258 ** Set the number of Tx and Rx buffers. 
 259 */
 260 #ifndef DEPCA_BUFFER_LOG_SZ
 261 #define RING_SIZE       16              /* 16 buffers */
 262 #else
 263 #define RING_SIZE       (1 << (DEPCA_BUFFERS_LOG_SZ))
 264 #endif  /* DEPCA_BUFFER_LOG_SZ */
 265 
 266 #define PKT_BUF_SZ      1544            /* Buffer size for each Tx/Rx buffer */
 267 #define PKT_SZ          1514            /* Maximum ethernet packet length */
 268 #define DAT_SZ          1500            /* Maximum ethernet data length */
 269 #define PKT_HDR_LEN     14              /* Addresses and data length info */
 270 
 271 #ifdef HAVE_MULTICAST
 272 #ifndef CRC_POLYNOMIAL
 273 #define CRC_POLYNOMIAL 0x04c11db7       /* Ethernet CRC polynomial */
 274 #endif /* CRC_POLYNOMIAL */
 275 #endif /* HAVE_MULTICAST */
 276 
 277 /*
 278 ** The DEPCA Rx and Tx ring descriptors. 
 279 */
 280 struct depca_rx_head {
 281     volatile long base;
 282     short buf_length;           /* This length is negative 2's complement! */
 283     short msg_length;           /* This length is "normal". */
 284 };
 285 
 286 struct depca_tx_head {
 287     volatile long base;
 288     short length;               /* This length is negative 2's complement! */
 289     short misc;                 /* Errors and TDR info */
 290 };
 291 
 292 #define LA_MASK 0x0000ffff      /* LANCE address mask for mapping network RAM
 293                                    to LANCE memory address space */
 294 
 295 /*
 296 ** The Lance initialization block, described in databook, in common memory.
 297 */
 298 struct depca_init {
 299     unsigned short mode;        /* Mode register */
 300     unsigned char phys_addr[ETH_ALEN];  /* Physical ethernet address */
 301     unsigned short filter[4];   /* Multicast filter. */
 302     unsigned long rx_ring;      /* Rx ring base pointer & ring length */
 303     unsigned long tx_ring;      /* Tx ring base pointer & ring length */
 304 };
 305 
 306 struct depca_private {
 307     char devname[8];            /* Device Product String */
 308     struct depca_rx_head *rx_ring; /* Pointer to start of RX descriptor ring */
 309     struct depca_tx_head *tx_ring; /* Pointer to start of TX descriptor ring */
 310     struct depca_init   init_block;/* Initialization block */
 311     long bus_offset;            /* (E)ISA bus address offset vs LANCE */
 312     long dma_buffs;             /* Start address of Rx and Tx buffers. */
 313     int cur_rx, cur_tx;         /* The next free ring entry */
 314     int dirty_rx, dirty_tx;     /* The ring entries to be free()ed. */
 315     int dma;
 316     struct enet_statistics stats;
 317     char depca_na;              /* NICSR access width: 0=>byte, 1=>word */
 318     short ringSize;             /* ring size based on available memory */
 319     short rmask;                /* modulus mask based on ring size */
 320     long rlen;                  /* log2(ringSize) for the descriptors */
 321 };
 322 
 323 /*
 324 ** Public Functions
 325 */
 326 static int depca_open(struct device *dev);
 327 static int depca_start_xmit(struct sk_buff *skb, struct device *dev);
 328 static void depca_interrupt(int irq, struct pt_regs * regs);
 329 static int depca_close(struct device *dev);
 330 static struct enet_statistics *depca_get_stats(struct device *dev);
 331 #ifdef HAVE_MULTICAST
 332 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 333 #endif
 334 
 335 /*
 336 ** Private functions
 337 */
 338 static int  depca_probe1(struct device *dev, short ioaddr);
 339 static void depca_init_ring(struct device *dev);
 340 static int  depca_rx(struct device *dev);
 341 static int  depca_tx(struct device *dev);
 342 
 343 static void LoadCSRs(struct device *dev);
 344 static int  InitRestartDepca(struct device *dev);
 345 static char *DepcaSignature(unsigned long mem_addr);
 346 static int  DevicePresent(short ioaddr);
 347 static int  EISA_signature(short iobase);
 348 #ifdef HAVE_MULTICAST
 349 static void SetMulticastFilter(int num_addrs, char *addrs, char *multicast_table);
 350 #endif
 351 
 352 #ifndef MODULE
 353 static struct device *isa_probe(struct device *dev);
 354 static struct device *eisa_probe(struct device *dev);
 355 static struct device *alloc_device(struct device *dev, int ioaddr);
 356 
 357 static int num_depcas = 0, num_eth = 0, autoprobed = 0;
 358 
 359 #else
 360 int  init_module(void);
 361 void cleanup_module(void);
 362 
 363 #endif /* MODULE */
 364 
 365 /*
 366 ** Miscellaneous defines...
 367 */
 368 #define STOP_DEPCA \
 369     outw(CSR0, DEPCA_ADDR);\
 370     outw(STOP, DEPCA_DATA)
 371 
 372 
 373 
 374 
 375 int depca_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377     short base_addr = dev->base_addr;
 378     int status = -ENODEV;
 379 #ifndef MODULE
 380     struct device *eth0;
 381 #endif
 382 
 383     if (base_addr > 0x1ff) {          /* Check a single specified location. */
 384       if (DevicePresent(base_addr) == 0) { /* Is DEPCA really here? */
 385         status = depca_probe1(dev, base_addr);
 386       }
 387     } else if (base_addr > 0) {       /* Don't probe at all. */
 388       status = -ENXIO;
 389 
 390 #ifdef MODULE
 391     } else {
 392       printk("Autoprobing is not supported when loading a module based driver.\n");
 393       status = -EIO;
 394 #else
 395     } else if (!autoprobed) {         /* First probe for the DEPCA test */
 396                                       /* pattern in ROM */
 397       eth0=isa_probe(dev);
 398       eth0=eisa_probe(eth0);
 399       if (dev->priv) status=0;
 400       autoprobed = 1;
 401     } else {
 402       status = -ENXIO;
 403 #endif /* MODULE */
 404 
 405     }
 406 
 407     if (status) dev->base_addr = base_addr;
 408 
 409     return status;                      /* ENODEV would be more accurate. */
 410 }
 411 
 412 static int
 413 depca_probe1(struct device *dev, short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 414 {
 415     struct depca_private *lp;
 416     int i,j, status=0;
 417     unsigned long mem_start, mem_base[] = DEPCA_RAM_BASE_ADDRESSES;
 418     char *name = NULL;
 419     unsigned int nicsr, offset, netRAM;
 420 
 421 
 422     /*
 423     ** Stop the DEPCA. Enable the DBR ROM. Disable interrupts and remote boot.
 424     */
 425     STOP_DEPCA;
 426 
 427     nicsr = inb(DEPCA_NICSR);
 428     nicsr = ((nicsr & ~SHE & ~RBE & ~IEN) | IM);
 429     outb(nicsr, DEPCA_NICSR);
 430 
 431     if (inw(DEPCA_DATA) == STOP) {
 432 
 433     /* Now find out what kind of DEPCA we have. The DE100 uses a different
 434     ** addressing scheme for some registers compared to the DE2xx series.
 435     ** Note that a base address location is marked as checked if no DEPCA is
 436     ** there or one is found (when the search is immediately terminated). This
 437     ** shortens the search time a little for multiple DEPCAs.
 438     */
 439 
 440       for (j = 0, i = 0; mem_base[i] && (j == 0);i++) {
 441         if (((mem_chkd >> i) & 0x01) == 0) { /* has the memory been checked? */
 442           name = DepcaSignature(mem_base[i]);/* check for a DEPCA here */
 443           mem_chkd |= (0x01 << i);           /* mark location checked */
 444           if (*name != '\0') {               /* one found? */
 445             j = 1;                           /* set exit flag */
 446             --i;
 447           }
 448         }
 449       }
 450 
 451       if (*name != '\0') {                   /* found a DEPCA device */
 452         mem_start = mem_base[i];
 453         dev->base_addr = ioaddr;
 454 
 455         if ((ioaddr&0x0fff)==DEPCA_EISA_IO_PORTS) {/* EISA slot address */
 456           printk("%s: %s at %#3x (EISA slot %d)", 
 457                                  dev->name, name, ioaddr, ((ioaddr>>12)&0x0f));
 458         } else {                             /* ISA port address */
 459           printk("%s: %s at %#3x", dev->name, name, ioaddr);
 460         }
 461 
 462       /* There is a 32 byte station address PROM at DEPCA_PROM address.
 463          The first six bytes are the station address. They can be read
 464          directly since the signature search set up the ROM address 
 465          counter correctly just before this function.
 466 
 467          For the DE100 we have to be careful about which port is used to
 468          read the ROM info.
 469       */
 470 
 471         if (strstr(name,"DE100")!= NULL) {
 472           j = 1;
 473         } else {
 474           j = 0;
 475         }
 476 
 477         printk(", h/w address ");
 478         for (i = 0; i < ETH_ALEN - 1; i++) { /* get the ethernet address */
 479           printk("%2.2x:", dev->dev_addr[i] = inb(DEPCA_PROM + j));
 480         }
 481         printk("%2.2x", dev->dev_addr[i] = inb(DEPCA_PROM + j));
 482 
 483         for (;i<32;i++) {                /* leave ROM counter in known state */
 484           j=inb(DEPCA_PROM);
 485         }
 486 
 487         request_region(ioaddr, DEPCA_TOTAL_SIZE, dev->name);
 488 
 489         /*
 490         ** Set up the maximum amount of network RAM(kB)
 491         */
 492         if (strstr(name,"DEPCA")== NULL) {
 493           netRAM=64;
 494         } else {
 495           netRAM=48;
 496         }
 497 
 498         /* 
 499         ** Determine the base address for the DEPCA RAM from the NI-CSR
 500         ** and make up a DEPCA-specific-data structure. 
 501         */
 502 
 503         if (nicsr & BUF) {
 504           offset = 0x8000;              /* 32kbyte RAM offset*/
 505           nicsr &= ~BS;                 /* DEPCA RAM in top 32k */
 506           printk(",\n      has %dkB RAM", netRAM - 32);
 507         } else  if ((nicsr & _128KB) && (netRAM!=48)) {
 508           offset = 0x0000;
 509           printk(",\n      has 128kB RAM");
 510         } else {
 511           offset = 0x0000;              /* 64k/48k bytes RAM */
 512           printk(",\n      has %dkB RAM", netRAM);
 513         }
 514 
 515         mem_start += offset;            /* (E)ISA start address */
 516         printk(" at 0x%.5lx", mem_start);
 517 
 518         /*
 519         ** Enable the shadow RAM.
 520         */
 521         if (strstr(name,"DEPCA") == NULL) {
 522           nicsr |= SHE;
 523           outb(nicsr, DEPCA_NICSR);
 524         }
 525  
 526         /*
 527         ** Calculate the ring size based on the available RAM
 528         ** found above. Allocate an equal number of buffers, each
 529         ** of size PKT_BUF_SZ (1544 bytes) to the Tx and Rx, allowing one
 530         ** descriptor entry (8 bytes) for each buffer. Make sure
 531         ** that this ring size is <= RING_SIZE. The ring size must be
 532         ** a power of 2.
 533         */
 534 
 535         j = (((netRAM << 10) - offset - sizeof(struct depca_private)) / 
 536                                                        (PKT_BUF_SZ + 8)) >> 1;
 537         for (i=0;j>1;i++) {
 538           j >>= 1;
 539         }
 540 
 541         /* Hold the ring size information here before the depca
 542         ** private structure is allocated. Need this for the memory
 543         ** space calculations.
 544         */
 545         j = 1 << i;
 546 
 547         /*
 548         ** Set up memory information in the device structure.
 549         ** Align the descriptor rings on an 8 byte (quadword) boundary.
 550         **
 551         **     depca_private area
 552         **     rx ring descriptors
 553         **     tx ring descriptors
 554         **     rx buffers
 555         **     tx buffers
 556         **
 557         */
 558 
 559         /* private area & initialise */
 560         dev->priv = (void *)((mem_start + 0x07) & ~0x07);      
 561         lp = (struct depca_private *)dev->priv;
 562         memset(dev->priv, 0, sizeof(struct depca_private));
 563         strcpy(lp->devname,name);
 564 
 565         /* Tx & Rx descriptors (aligned to a quadword boundary) */
 566         mem_start = ((((unsigned long)dev->priv + 
 567                         sizeof(struct depca_private)) +
 568                         (unsigned long)0x07) & (unsigned long)~0x07);
 569         lp->rx_ring = (struct depca_rx_head *)mem_start;
 570 
 571         mem_start += (sizeof(struct depca_rx_head) * j);
 572         lp->tx_ring = (struct depca_tx_head *)mem_start;
 573 
 574         mem_start += (sizeof(struct depca_tx_head) * j);
 575         lp->bus_offset = mem_start & 0x00ff0000;
 576         mem_start &= LA_MASK;           /* LANCE re-mapped start address */
 577 
 578         lp->dma_buffs = mem_start;
 579 
 580         mem_start += (PKT_BUF_SZ * j);
 581         /* (mem_start now points to the start of the Tx buffers) */
 582 
 583         /* Initialise the data structures wrt CPU */
 584         memset(lp->rx_ring, 0, sizeof(struct depca_rx_head)*j);
 585         memset(lp->tx_ring, 0, sizeof(struct depca_tx_head)*j);
 586 
 587         /* This should never happen. */
 588         if ((long)(lp->rx_ring) & 0x07) {
 589           printk("\n **ERROR** DEPCA Rx and Tx descriptor rings not on a quadword boundary.\n");
 590           return -ENXIO;
 591         }
 592 
 593         /*
 594         ** Finish initialising the ring information.
 595         */
 596         lp->ringSize = j;
 597         if (lp->ringSize > RING_SIZE) lp->ringSize = RING_SIZE;
 598         lp->rmask = lp->ringSize - 1;
 599 
 600         /*
 601         ** calculate the real RLEN size for the descriptors. It is
 602         ** log2(ringSize).
 603         */
 604         for (i=0, j = lp->ringSize; j>1; i++) {
 605           j >>= 1;
 606         }
 607         lp->rlen = (unsigned long)(i << 29);
 608 
 609         /*
 610         ** load the initialisation block
 611         */
 612         depca_init_ring(dev);
 613 
 614         /*
 615         ** Initialise the control and status registers
 616         */
 617         LoadCSRs(dev);
 618 
 619         /*
 620         ** Enable DEPCA board interrupts for autoprobing
 621         */
 622         nicsr = ((nicsr & ~IM)|IEN);
 623         outb(nicsr, DEPCA_NICSR);
 624 
 625         /* The DMA channel may be passed in on this parameter. */
 626         dev->dma = 0;
 627 
 628         /* To auto-IRQ we enable the initialization-done and DMA err,
 629          interrupts. For now we will always get a DMA error. */
 630         if (dev->irq < 2) {
 631 #ifndef MODULE
 632           autoirq_setup(0);
 633 
 634           /* Trigger an initialization just for the interrupt. */
 635           outw(INEA | INIT, DEPCA_DATA);
 636           
 637           dev->irq = autoirq_report(1);
 638           if (dev->irq) {
 639             printk(" and uses IRQ%d.\n", dev->irq);
 640           } else {
 641             printk(" and failed to detect IRQ line.\n");
 642             status = -EAGAIN;
 643           }
 644 #endif /* MODULE */
 645         } else {
 646           printk(" and assigned IRQ%d.\n", dev->irq);
 647         }
 648       } else {
 649         status = -ENXIO;
 650       }
 651       if (!status) {
 652         if (depca_debug > 0) {
 653           printk(version);
 654         }
 655 
 656         /* The DEPCA-specific entries in the device structure. */
 657         dev->open = &depca_open;
 658         dev->hard_start_xmit = &depca_start_xmit;
 659         dev->stop = &depca_close;
 660         dev->get_stats = &depca_get_stats;
 661 #ifdef HAVE_MULTICAST
 662         dev->set_multicast_list = &set_multicast_list;
 663 #endif
 664 
 665         dev->mem_start = 0;
 666         
 667         /* Fill in the generic field of the device structure. */
 668         ether_setup(dev);
 669       }
 670     } else {
 671       status = -ENXIO;
 672     }
 673 
 674     return status;
 675 }
 676 
 677 
 678 static int
 679 depca_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 680 {
 681     struct depca_private *lp = (struct depca_private *)dev->priv;
 682     int i,nicsr,ioaddr = dev->base_addr;
 683 
 684     if (request_irq(dev->irq, &depca_interrupt, 0, "depca")) {
 685         printk("depca_open(): Requested IRQ%d is busy\n",dev->irq);
 686         return -EAGAIN;
 687     }
 688 
 689     irq2dev_map[dev->irq] = dev;
 690 
 691     /*
 692     ** Stop the DEPCA & get the board status information.  
 693     */
 694     STOP_DEPCA;
 695     nicsr = inb(DEPCA_NICSR);
 696 
 697     /*
 698     ** Make sure the shadow RAM is enabled
 699     */
 700     if (strstr(lp->devname,"DEPCA") == NULL) {
 701       nicsr |= SHE;
 702       outb(nicsr, DEPCA_NICSR);
 703     }
 704 
 705     /* 
 706     ** Re-initialize the DEPCA... 
 707     */
 708     depca_init_ring(dev);                 /* initialize the descriptor rings */
 709     LoadCSRs(dev);
 710 
 711     if (depca_debug > 1){
 712       printk("%s: depca open with irq %d\n",dev->name,dev->irq);
 713       printk("Descriptor head addresses:\n");
 714       printk("\t0x%8.8lx  0x%8.8lx\n",(long)lp->rx_ring,(long)lp->tx_ring);
 715       printk("Descriptor addresses:\n");
 716       for (i=0;i<lp->ringSize;i++){
 717         printk("\t0x%8.8lx  0x%8.8lx\n",(long)&lp->rx_ring[i].base,
 718                                         (long)&lp->tx_ring[i].base);
 719       }
 720       printk("Buffer addresses:\n");
 721       for (i=0;i<lp->ringSize;i++){
 722         printk("\t0x%8.8lx  0x%8.8lx\n",(long)lp->rx_ring[i].base,
 723                                         (long)lp->tx_ring[i].base);
 724       }
 725       printk("Initialisation block at 0x%8.8lx\n",(long)&lp->init_block);
 726       printk("\tmode: 0x%4.4x\n",lp->init_block.mode);
 727       printk("\tphysical address: ");
 728       for (i=0;i<6;i++){
 729         printk("%2.2x:",(short)lp->init_block.phys_addr[i]);
 730       }
 731       printk("\n\tlogical address filter: 0x");
 732       for (i=0;i<4;i++){
 733         printk("%2.2x",(short)lp->init_block.filter[i]);
 734       }
 735       printk("\n\trx_ring at: 0x%8.8lx\n",(long)lp->init_block.rx_ring);
 736       printk("\ttx_ring at: 0x%8.8lx\n",(long)lp->init_block.tx_ring);
 737       printk("dma_buffs: 0x%8.8lx\n",(long)lp->dma_buffs);
 738       printk("Ring size: %d\nMask: 0x%2.2x\nLog2(ringSize): 0x%8.8lx\n", 
 739                                          (short)lp->ringSize, 
 740                                           (char)lp->rmask,
 741                                           (long)lp->rlen);
 742       outw(CSR2,DEPCA_ADDR);
 743       printk("CSR2&1: 0x%4.4x",inw(DEPCA_DATA));
 744       outw(CSR1,DEPCA_ADDR);
 745       printk("%4.4x\n",inw(DEPCA_DATA));
 746       outw(CSR3,DEPCA_ADDR);
 747       printk("CSR3: 0x%4.4x\n",inw(DEPCA_DATA));
 748     }
 749 
 750     /*
 751     ** Enable DEPCA board interrupts and turn off LED
 752     */
 753     nicsr = ((nicsr & ~IM & ~LED)|IEN);
 754     outb(nicsr, DEPCA_NICSR);
 755     outw(CSR0,DEPCA_ADDR);
 756 
 757     dev->tbusy = 0;                         
 758     dev->interrupt = 0;
 759     dev->start = 1;
 760 
 761     InitRestartDepca(dev);                /* ignore the return status */
 762 
 763     if (depca_debug > 1){
 764       printk("CSR0: 0x%4.4x\n",inw(DEPCA_DATA));
 765       printk("nicsr: 0x%02x\n",inb(DEPCA_NICSR));
 766     }
 767 
 768     MOD_INC_USE_COUNT;
 769 
 770     return 0;                             /* Always succeed */
 771 }
 772 
 773 /* Initialize the lance Rx and Tx descriptor rings. */
 774 static void
 775 depca_init_ring(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 776 {
 777     struct depca_private *lp = (struct depca_private *)dev->priv;
 778     unsigned long i;
 779 
 780     lp->init_block.mode = DTX | DRX;         /* Disable Rx and Tx. */
 781     lp->cur_rx = lp->cur_tx = 0;
 782     lp->dirty_rx = lp->dirty_tx = 0;
 783 
 784     /* Initialize the base addresses and length of each buffer in the ring */
 785     for (i = 0; i < lp->ringSize; i++) {
 786         lp->rx_ring[i].base = (lp->dma_buffs + i*PKT_BUF_SZ) | R_OWN;
 787         lp->rx_ring[i].buf_length = -PKT_BUF_SZ;
 788         lp->tx_ring[i].base = (lp->dma_buffs + (i+lp->ringSize) * PKT_BUF_SZ) &
 789                                                    (unsigned long)(0x00ffffff);
 790     }
 791 
 792     /* Set up the initialization block */
 793     for (i = 0; i < ETH_ALEN; i++) {
 794       lp->init_block.phys_addr[i] = dev->dev_addr[i];
 795     }
 796     for (i = 0; i < 4; i++) {
 797       lp->init_block.filter[i] = 0x0000;
 798     }
 799     lp->init_block.rx_ring = ((unsigned long)lp->rx_ring & LA_MASK) | lp->rlen;
 800     lp->init_block.tx_ring = ((unsigned long)lp->tx_ring & LA_MASK) | lp->rlen;
 801 
 802     lp->init_block.mode = 0x0000;            /* Enable the Tx and Rx */ 
 803 }
 804 
 805 /* 
 806 ** Writes a socket buffer to TX descriptor ring and starts transmission 
 807 */
 808 static int
 809 depca_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 810 {
 811     struct depca_private *lp = (struct depca_private *)dev->priv;
 812     int ioaddr = dev->base_addr;
 813     int status = 0;
 814 
 815     /* Transmitter timeout, serious problems. */
 816     if (dev->tbusy) {
 817       int tickssofar = jiffies - dev->trans_start;
 818       if (tickssofar < 10) {
 819         status = -1;
 820       } else {
 821         printk("%s: transmit timed out, status %04x, resetting.\n",
 822                dev->name, inw(DEPCA_DATA));
 823         
 824         STOP_DEPCA;
 825         depca_init_ring(dev);
 826         LoadCSRs(dev);
 827         InitRestartDepca(dev);
 828         dev->tbusy=0;
 829         dev->trans_start = jiffies;
 830       }
 831       return status;
 832     }
 833 
 834     if (skb == NULL) {
 835         dev_tint(dev);
 836         return 0;
 837     }
 838 
 839     if (skb->len <= 0) {
 840       return 0;
 841     }
 842 
 843     if (depca_debug > 3) {
 844         outw(CSR0, DEPCA_ADDR);
 845         printk("%s: depca_start_xmit() called, csr0 %4.4x.\n", dev->name,
 846                inw(DEPCA_DATA));
 847     }
 848 
 849     /* Block a timer-based transmit from overlapping.  This could better be
 850        done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 851     if (set_bit(0, (void*)&dev->tbusy) != 0)
 852         printk("%s: Transmitter access conflict.\n", dev->name);
 853 
 854     /*
 855     ** The TX buffer, skb, has to be copied into the local network RAM
 856     ** for the LANCE to access it. The skb may be at > 16MB for large 
 857     ** (memory) systems.
 858     */
 859     {                           /* Fill in a Tx ring entry */
 860       unsigned char *buf;
 861       int entry = lp->cur_tx++;
 862       int len;
 863       long skbL = skb->len;
 864       char *p = (char *) skb->data;
 865 
 866       entry &= lp->rmask;                   /* Ring around buffer number. */
 867       buf = (unsigned char *)((lp->tx_ring[entry].base+lp->bus_offset) & 
 868                                                                    0x00ffffff);
 869 
 870       /* Wait for a full ring to free up */
 871       while (lp->tx_ring[entry].base < 0);
 872 
 873       /* 
 874       ** Caution: the write order is important here... don't set up the
 875       ** ownership rights until all the other information is in place.
 876       */
 877       len = ((skbL > PKT_SZ) ? PKT_SZ : skbL); /* skb too long */
 878       if (len < ETH_ZLEN) len = ETH_ZLEN;      /* len too short */
 879       skbL -= len;
 880       lp->tx_ring[entry].length = -len;
 881 
 882       /* Clears various error flags */
 883       lp->tx_ring[entry].misc = 0x0000;
 884 
 885       /* copy the data from the socket buffer to the net memory */
 886       memcpy((unsigned char *)(buf), skb->data, len);
 887 
 888       /* Hand over buffer ownership to the LANCE */
 889       if (skbL <= 0) lp->tx_ring[entry].base |= (T_ENP);
 890       lp->tx_ring[entry].base |= (T_OWN|T_STP);
 891 
 892       /* Trigger an immediate send demand. */
 893       outw(CSR0, DEPCA_ADDR);
 894       outw(INEA | TDMD, DEPCA_DATA);
 895 
 896       dev->trans_start = jiffies;
 897 
 898       for (p += len; skbL > 0; p += len) {
 899 
 900         /* Get new buffer pointer */
 901         entry = lp->cur_tx++;
 902         entry &= lp->rmask;                 /* Ring around buffer number. */
 903         buf = (unsigned char *)((lp->tx_ring[entry].base+lp->bus_offset) &
 904                                                                    0x00ffffff);
 905 
 906         /* Wait for a full ring to free up */
 907         while (lp->tx_ring[entry].base < 0);
 908         dev->tbusy=0;
 909 
 910         /* Copy ethernet header to the new buffer */
 911         memcpy((unsigned char *)buf, skb->data, PKT_HDR_LEN);
 912 
 913         /* Determine length of data buffer */
 914         len = ((skbL > DAT_SZ) ? DAT_SZ : skbL); /* skbL too long */
 915         if (len < ETH_ZLEN) len = ETH_ZLEN;      /* len too short */
 916         skbL -= len;
 917         lp->tx_ring[entry].length = -len;
 918 
 919         /* Clears various error flags */
 920         lp->tx_ring[entry].misc = 0x0000;
 921 
 922         /* copy the data from the socket buffer to the net memory */
 923         memcpy((unsigned char *)(buf + PKT_HDR_LEN), (unsigned char *)p, len);
 924 
 925         /* Hand over buffer ownership to the LANCE */
 926         if (skbL <= 0) lp->tx_ring[entry].base |= T_ENP;
 927         lp->tx_ring[entry].base |= T_OWN;
 928       }
 929 
 930       if (depca_debug > 4) {
 931         unsigned char *pkt =
 932           (unsigned char *)((lp->tx_ring[entry].base+lp->bus_offset) &
 933                                                                    0x00ffffff);
 934 
 935         printk("%s: tx ring[%d], %#lx, sk_buf %#lx len %d.\n",
 936                dev->name, entry, (unsigned long) &lp->tx_ring[entry],
 937                lp->tx_ring[entry].base, -lp->tx_ring[entry].length);
 938         printk("%s:  Tx %2.2x %2.2x %2.2x ... %2.2x  %2.2x %2.2x %2.2x...%2.2x len %2.2x %2.2x  %2.2x %2.2x.\n",
 939                dev->name, pkt[0], pkt[1], pkt[2], pkt[5], pkt[6],
 940                pkt[7], pkt[8], pkt[11], pkt[12], pkt[13],
 941                pkt[14], pkt[15]);
 942       }
 943       
 944       /* Check if the TX ring is full or not - 'tbusy' cleared if not full. */
 945       if (lp->tx_ring[(entry+1) & lp->rmask].base >= 0) {
 946         dev->tbusy=0;
 947       }
 948 
 949       dev_kfree_skb (skb, FREE_WRITE);
 950     }
 951 
 952     return 0;
 953 }
 954 
 955 /*
 956 ** The DEPCA interrupt handler. 
 957 */
 958 static void
 959 depca_interrupt(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 960 {
 961     struct device *dev = (struct device *)(irq2dev_map[irq]);
 962     struct depca_private *lp;
 963     int csr0, ioaddr, nicsr;
 964 
 965     if (dev == NULL) {
 966         printk ("depca_interrupt(): irq %d for unknown device.\n", irq);
 967     } else {
 968       lp = (struct depca_private *)dev->priv;
 969       ioaddr = dev->base_addr;
 970 
 971       if (dev->interrupt)
 972         printk("%s: Re-entering the interrupt handler.\n", dev->name);
 973 
 974       dev->interrupt = MASK_INTERRUPTS;
 975 
 976       /* mask the DEPCA board interrupts and turn on the LED */
 977       nicsr = inb(DEPCA_NICSR);
 978       nicsr |= (IM|LED);
 979       outb(nicsr, DEPCA_NICSR);
 980 
 981       outw(CSR0, DEPCA_ADDR);
 982       csr0 = inw(DEPCA_DATA);
 983 
 984       /* Acknowledge all of the current interrupt sources ASAP. */
 985       outw(csr0 & ~(INEA|TDMD|STOP|STRT|INIT), DEPCA_DATA);
 986 
 987       if (depca_debug > 5)
 988         printk("%s: interrupt  csr0=%#2.2x new csr=%#2.2x.\n",
 989                dev->name, csr0, inw(DEPCA_DATA));
 990 
 991       if (csr0 & RINT)          /* Rx interrupt (packet arrived) */
 992         depca_rx(dev);
 993 
 994       if (csr0 & TINT)          /* Tx interrupt (packet sent) */
 995         depca_tx(dev);
 996 
 997       /* Clear the interrupts we've handled. */
 998       outw(CSR0, DEPCA_ADDR);
 999       outw(BABL|CERR|MISS|MERR|RINT|TINT|IDON|INEA, DEPCA_DATA);
1000 
1001       if (depca_debug > 4) {
1002         printk("%s: exiting interrupt, csr%d=%#4.4x.\n",
1003                dev->name, inw(DEPCA_ADDR),
1004                inw(DEPCA_DATA));
1005       }
1006 
1007       /* Unmask the DEPCA board interrupts and turn off the LED */
1008       nicsr = (nicsr & ~IM & ~LED);
1009       outb(nicsr, DEPCA_NICSR);
1010       dev->interrupt = UNMASK_INTERRUPTS;
1011     }
1012 
1013     return;
1014 }
1015 
1016 static int
1017 depca_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1018 {
1019     struct depca_private *lp = (struct depca_private *)dev->priv;
1020     int entry = lp->cur_rx & lp->rmask;
1021 
1022     /* If we own the next entry, it's a new packet. Send it up. */
1023     for (; lp->rx_ring[entry].base >= 0; entry = (++lp->cur_rx) & lp->rmask) {
1024         int status = lp->rx_ring[entry].base >> 16 ;
1025         int chained;
1026 
1027         /*
1028         ** There is a tricky error noted by John Murphy, <murf@perftech.com>
1029         ** to Russ Nelson: even with full-sized buffers, it's possible for a
1030         ** jabber packet to use two buffers, with only the last one correctly
1031         ** noting the error.
1032         */
1033 
1034         /* Check for a chaining buffer */
1035         chained = 0;
1036         if (status == R_STP) { 
1037           chained = 1;
1038 
1039           /* 
1040           ** Wait for next buffer to complete to check for errors. This
1041           ** is slow but infrequent and allows for correct hardware buffer
1042           ** chaining (whilst defeating the chaining's purpose).
1043           */
1044           while ((status=(lp->rx_ring[(entry+1)&lp->rmask].base >> 16)) < 0);
1045 
1046           /* NB: 'status' now comes from the buffer following 'entry'. */
1047         }
1048           
1049         if (status & R_ERR) {                  /* There was an error. */
1050             lp->stats.rx_errors++;             /* Update the error stats. */
1051             if (status & R_FRAM) lp->stats.rx_frame_errors++;
1052             if (status & R_OFLO) lp->stats.rx_over_errors++;
1053             if (status & R_CRC)  lp->stats.rx_crc_errors++;
1054             if (status & R_BUFF) lp->stats.rx_fifo_errors++;
1055         } else {          /* Malloc up new buffer, compatible  with net-2e. */
1056             short pkt_len = lp->rx_ring[entry].msg_length;
1057             struct sk_buff *skb;
1058 
1059             skb = alloc_skb(pkt_len, GFP_ATOMIC);
1060             if (skb == NULL) {
1061                 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1062                 lp->stats.rx_dropped++; /* Really, deferred. */
1063                 break;
1064             }
1065             skb->len = pkt_len;
1066             skb->dev = dev;
1067             memcpy(skb->data,
1068                   (unsigned char *)((lp->rx_ring[entry].base+lp->bus_offset) &
1069                                                                    0x00ffffff),
1070                    pkt_len);
1071             /* 
1072             ** Notify the upper protocol layers that there is another 
1073             ** packet to handle
1074             */
1075             skb->protocol=eth_type_trans(skb,dev);
1076             netif_rx(skb);
1077             lp->stats.rx_packets++;
1078         }
1079 
1080         /* turn over ownership of the current entry back to the LANCE */
1081         lp->rx_ring[entry].base |= R_OWN;
1082         if (chained && (status & R_ERR)) {          /* next entry also bad */
1083           entry = (++lp->cur_rx) & lp->rmask;
1084           lp->rx_ring[entry].base |= R_OWN;
1085         }
1086     }
1087 
1088     /* 
1089     ** We should check that at least two ring entries are free.  If not,
1090     ** we should free one and mark stats->rx_dropped++. 
1091     */
1092 
1093     return 0;
1094 }
1095 
1096 /*
1097 ** Buffer sent - check for buffer errors.
1098 */
1099 static int
1100 depca_tx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1101 {
1102   struct depca_private *lp = (struct depca_private *)dev->priv;
1103   int dirty_tx = lp->dirty_tx & lp->rmask;
1104 
1105   if (depca_debug > 5)
1106     printk("%s: Cleaning tx ring, dirty %d clean %d.\n",
1107            dev->name, dirty_tx, (lp->cur_tx & lp->rmask));
1108   
1109   /* 
1110   ** While the dirty entry is not the current one AND 
1111   ** the LANCE doesn't own it... 
1112   */
1113   for (; dirty_tx!=(lp->cur_tx & lp->rmask) && lp->tx_ring[dirty_tx].base>0;
1114                                        dirty_tx = ++lp->dirty_tx & lp->rmask) {
1115     unsigned long *tmdp = (unsigned long *)(&lp->tx_ring[dirty_tx]);
1116     int status = lp->tx_ring[dirty_tx].base >> 16;
1117 
1118     if (status < 0) {                          /* Packet not yet sent! */
1119       printk("interrupt for packet not yet sent!\n");
1120       break;
1121     }
1122     if (status & T_ERR) { /* There was an major error, log it. */
1123       int err_status = lp->tx_ring[dirty_tx].misc;
1124 
1125       lp->stats.tx_errors++;
1126       if (err_status & TMD3_RTRY) lp->stats.tx_aborted_errors++;
1127       if (err_status & TMD3_LCAR) lp->stats.tx_carrier_errors++;
1128       if (err_status & TMD3_LCOL) lp->stats.tx_window_errors++;
1129       if (err_status & TMD3_UFLO) lp->stats.tx_fifo_errors++;
1130       /* We should re-init() after the FIFO error. */
1131     } else if (status & (T_MORE | T_ONE)) {
1132       lp->stats.collisions++;
1133     } else {
1134       lp->stats.tx_packets++;
1135     }
1136 
1137     if (depca_debug > 5)
1138       printk("%s: Tx done entry %d, %4.4lx %4.4lx %4.4lx %4.4lx.\n",
1139              dev->name, dirty_tx,
1140              tmdp[0], tmdp[1], tmdp[2], tmdp[3]);
1141   }
1142   /*mark_bh(INET_BH);*/
1143   return 0;
1144 }
1145 
1146 static int
1147 depca_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1148 {
1149   struct depca_private *lp = (struct depca_private *)dev->priv;
1150   int nicsr, ioaddr = dev->base_addr;
1151 
1152   dev->start = 0;
1153   dev->tbusy = 1;
1154 
1155   outw(CSR0, DEPCA_ADDR);
1156 
1157   if (depca_debug > 1) {
1158     printk("%s: Shutting down ethercard, status was %2.2x.\n",
1159            dev->name, inw(DEPCA_DATA));
1160   }
1161 
1162   /* 
1163   ** We stop the DEPCA here -- it occasionally polls
1164   ** memory if we don't. 
1165   */
1166   outw(STOP, DEPCA_DATA);
1167 
1168   /*
1169   ** Give back the ROM in case the user wants to go to DOS
1170   */
1171   if (strstr(lp->devname,"DEPCA") == NULL) {
1172     nicsr = inb(DEPCA_NICSR);
1173     nicsr &= ~SHE;
1174     outb(nicsr, DEPCA_NICSR);
1175   }
1176 
1177   free_irq(dev->irq);
1178 
1179   irq2dev_map[dev->irq] = 0;
1180 
1181   MOD_DEC_USE_COUNT;
1182 
1183   return 0;
1184 }
1185 
1186 static void LoadCSRs(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1187 {
1188   struct depca_private *lp = (struct depca_private *)dev->priv;
1189   int ioaddr = dev->base_addr;
1190 
1191   outw(CSR1, DEPCA_ADDR);                /* initialisation block address LSW */
1192   outw((unsigned short)((unsigned long)(&lp->init_block) & LA_MASK), 
1193                                                                    DEPCA_DATA);
1194   outw(CSR2, DEPCA_ADDR);                /* initialisation block address MSW */
1195   outw((unsigned short)(((unsigned long)(&lp->init_block) & LA_MASK) >> 16), 
1196                                                                    DEPCA_DATA);
1197   outw(CSR3, DEPCA_ADDR);                /* ALE control */
1198   outw(ACON, DEPCA_DATA);
1199   outw(CSR0, DEPCA_ADDR);                /* point back to CSR0 */
1200 }
1201 
1202 static int InitRestartDepca(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1203 {
1204   struct depca_private *lp = (struct depca_private *)dev->priv;
1205   int ioaddr = dev->base_addr;
1206   int i, status=0;
1207 
1208   outw(CSR0, DEPCA_ADDR);                /* point back to CSR0 */
1209   outw(INIT, DEPCA_DATA);                /* initialize DEPCA */
1210 
1211   /* wait for lance to complete initialisation */
1212   for (i=0;(i<100) && !(inw(DEPCA_DATA) & IDON); i++); 
1213 
1214   if (i!=100) {
1215     /* clear IDON by writing a "1", enable interrupts and start lance */
1216     outw(IDON | INEA | STRT, DEPCA_DATA);
1217     if (depca_debug > 2) {
1218       printk("%s: DEPCA open after %d ticks, init block %#lx csr0 %4.4x.\n",
1219              dev->name, i, (long) &lp->init_block, inw(DEPCA_DATA));
1220     }
1221   } else {
1222     status = -1;
1223     printk("%s: DEPCA unopened after %d ticks, init block %#lx csr0 %4.4x.\n",
1224            dev->name, i, (long) &lp->init_block, inw(DEPCA_DATA));
1225   }
1226 
1227   return status;
1228 }
1229 
1230 static struct enet_statistics *
1231 depca_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1232 {
1233     struct depca_private *lp = (struct depca_private *)dev->priv;
1234 
1235     /* Null body since there is no framing error counter */
1236 
1237     return &lp->stats;
1238 }
1239 
1240 #ifdef HAVE_MULTICAST
1241 /*
1242 ** Set or clear the multicast filter for this adaptor.
1243 ** num_addrs == -1      Promiscuous mode, receive all packets
1244 ** num_addrs == 0       Normal mode, clear multicast list
1245 ** num_addrs > 0        Multicast mode, receive normal and MC packets, and do
1246 **                      best-effort filtering.
1247 */
1248 #define hash_filter lp->init_block.filter
1249 
1250 static void
1251 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
1252 {
1253   short ioaddr = dev->base_addr;
1254   struct depca_private *lp = (struct depca_private *)dev->priv;
1255   
1256   if (irq2dev_map[dev->irq] != NULL) {
1257     STOP_DEPCA;                       /* Temporarily stop the depca.  */
1258     depca_init_ring(dev);             /* Initialize the descriptor rings */
1259 
1260     if (num_addrs >= 0) {
1261       SetMulticastFilter(num_addrs, (char *)addrs, (char *)hash_filter);
1262       lp->init_block.mode &= ~PROM;   /* Unset promiscuous mode */
1263     } else {
1264       lp->init_block.mode |= PROM;    /* Set promiscuous mode */
1265     }
1266 
1267     LoadCSRs(dev);                    /* Reload CSR3 */
1268     InitRestartDepca(dev);            /* Resume normal operation. */
1269   }
1270 }
1271 
1272 /*
1273 ** Calculate the hash code and update the logical address filter
1274 ** from a list of ethernet multicast addresses.
1275 ** Derived from a 'C' program in the AMD data book:
1276 ** "Am79C90 CMOS Local Area Network Controller for Ethernet (C-LANCE)", 
1277 ** Pub #17781, Rev. A, May 1993
1278 */
1279 static void SetMulticastFilter(int num_addrs, char *addrs, char *multicast_table)
     /* [previous][next][first][last][top][bottom][index][help] */
1280 {
1281   char j, ctrl, bit, octet, hashcode;
1282   short int i;
1283   long int CRC, poly = (long int) CRC_POLYNOMIAL;
1284 
1285   for (i=0;i<num_addrs;i++) {                /* for each address in the list */
1286     if (((char) *(addrs+ETH_ALEN*i) & 0x01) == 1) {/* is multicast address? */ 
1287       CRC = (long int) 0xffffffff;           /* init CRC for each address */
1288       for (octet=0;octet<ETH_ALEN;octet++) { /* for each address octet */
1289         for(j=0;j<8;j++) {                   /* process each address bit */
1290           bit = (((char)* (addrs+ETH_ALEN*i+octet)) >> j) & 0x01;
1291           ctrl = ((CRC < 0) ? 1 : 0);        /* shift the control bit */
1292           CRC <<= 1;                         /* shift the CRC */
1293           if (bit ^ ctrl) {                  /* (bit) XOR (control bit) */
1294             CRC ^= poly;                     /* (CRC) XOR (polynomial) */
1295           }
1296         }
1297       }
1298       hashcode = (CRC & 0x00000001);         /* hashcode is 6 LSb of CRC ... */
1299       for (j=0;j<5;j++) {                    /* ... in reverse order. */
1300         hashcode <<= 1;
1301         CRC >>= 1;
1302         hashcode |= (CRC & 0x00000001);
1303       }                                      
1304       octet = hashcode >> 3;                  /* bit[3-5] -> octet in filter */
1305                                               /* bit[0-2] -> bit in octet */
1306       multicast_table[octet] |= (1 << (hashcode & 0x07));
1307     }
1308   }
1309   return;
1310 }
1311 
1312 #endif  /* HAVE_MULTICAST */
1313 
1314 #ifndef MODULE
1315 /*
1316 ** ISA bus I/O device probe
1317 */
1318 static struct device *isa_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1319 {
1320   int *port, ports[] = DEPCA_IO_PORTS;
1321   int status;
1322 
1323   for (status = -ENODEV, port = &ports[0]; 
1324                              *port && (num_depcas < MAX_NUM_DEPCAS); port++) {
1325     int ioaddr = *port;
1326 
1327     if (DevicePresent(ioaddr) == 0) {
1328       if (num_depcas > 0) {        /* only gets here in autoprobe */
1329         dev = alloc_device(dev, ioaddr);
1330       } else {
1331         if ((status = depca_probe1(dev, ioaddr)) == 0) {
1332           num_depcas++;
1333         }
1334       }
1335       num_eth++;
1336     }
1337   }
1338   return dev;
1339 }
1340 
1341 /*
1342 ** EISA bus I/O device probe. Probe from slot 1 since slot 0 is usually
1343 ** the motherboard.
1344 */
1345 static struct device *eisa_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1346 {
1347   int i, ioaddr = DEPCA_EISA_IO_PORTS;
1348   int status;
1349 
1350   ioaddr+=0x1000;                         /* get the first slot address */
1351   for (status = -ENODEV, i=1; i<MAX_EISA_SLOTS; i++, ioaddr+=0x1000) {
1352 
1353     if (EISA_signature(DEPCA_EISA_ID) == 0) {
1354       if (num_depcas > 0) {        /* only gets here in autoprobe */
1355         dev = alloc_device(dev, ioaddr);
1356       } else {
1357         if ((status = depca_probe1(dev, ioaddr)) == 0) {
1358           num_depcas++;
1359         }
1360       }
1361       num_eth++;
1362     }
1363   }
1364   return dev;
1365 }
1366 
1367 /*
1368 ** Allocate the device by pointing to the next available space in the
1369 ** device structure. Should one not be available, it is created.
1370 */
1371 static struct device *alloc_device(struct device *dev, int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1372 {
1373   /*
1374   ** Check the device structures for an end of list or unused device
1375   */
1376   while (dev->next != NULL) {
1377     if (dev->next->base_addr == 0xffe0) break;
1378     dev = dev->next;         /* walk through eth device list */
1379     num_eth++;               /* increment eth device number */
1380   }
1381 
1382   /*
1383   ** If no more device structures, malloc one up. If memory could
1384   ** not be allocated, print an error message.
1385   */
1386   if (dev->next == NULL) {
1387     dev->next = (struct device *)kmalloc(sizeof(struct device) + 8,
1388                                          GFP_KERNEL);
1389     if (dev->next == NULL) {
1390       printk("eth%d: Device not initialised, insufficient memory\n",
1391              num_eth);
1392     }
1393   }
1394   
1395   /*
1396   ** If the memory was allocated, point to the new memory area
1397   ** and initialize it (name, I/O address, next device (NULL) and
1398   ** initialisation probe routine).
1399   */
1400   if ((dev->next != NULL) &&
1401       (num_eth > 0) && (num_eth < 9999)) {
1402     dev = dev->next;                    /* point to the new device */
1403     dev->name = (char *)(dev + 1);
1404     sprintf(dev->name,"eth%d", num_eth);/* New device name */
1405     dev->base_addr = ioaddr;            /* assign the io address */
1406     dev->next = NULL;                   /* mark the end of list */
1407     dev->init = &depca_probe;           /* initialisation routine */
1408     num_depcas++;
1409   }
1410 
1411   return dev;
1412 }
1413 #endif    /* MODULE */
1414 
1415 /*
1416 ** Look for a particular board name in the on-board Remote Diagnostics
1417 ** and Boot (RDB) ROM. This will also give us a clue to the network RAM
1418 ** base address.
1419 */
1420 static char *DepcaSignature(unsigned long mem_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1421 {
1422   unsigned long i,j,k;
1423   static char signatures[][DEPCA_NAME_LENGTH] = DEPCA_SIGNATURE;
1424   static char thisName[DEPCA_NAME_LENGTH];
1425   char tmpstr[17];
1426 
1427   for (i=0;i<16;i++) {                  /* copy the first 16 bytes of ROM to */
1428     tmpstr[i] = *(unsigned char *)(mem_addr+0xc000+i); /* a temporary string */
1429   }
1430   tmpstr[i]='\0';
1431 
1432   strcpy(thisName,"");
1433   for (i=0;*signatures[i]!='\0' && *thisName=='\0';i++) {
1434     for (j=0,k=0;j<16 && k<strlen(signatures[i]);j++) {
1435       if (signatures[i][k] == tmpstr[j]) {              /* track signature */
1436         k++;
1437       } else {                     /* lost signature; begin search again */
1438         k=0;
1439       }
1440     }
1441     if (k == strlen(signatures[i])) {
1442       strcpy(thisName,signatures[i]);
1443     }
1444   }
1445 
1446   return thisName;                    /* return the device name string */
1447 }
1448 
1449 /*
1450 ** Look for a special sequence in the Ethernet station address PROM that
1451 ** is common across all DEPCA products. Note that the original DEPCA needs
1452 ** its ROM address counter to be initialized and enabled. Only enable
1453 ** if the first address octet is a 0x08 - this minimises the chances of
1454 ** messing around with some other hardware, but it assumes that this DEPCA
1455 ** card initialized itself correctly.
1456 ** 
1457 ** Search the Ethernet address ROM for the signature. Since the ROM address
1458 ** counter can start at an arbitrary point, the search must include the entire
1459 ** probe sequence length plus the (length_of_the_signature - 1).
1460 ** Stop the search IMMEDIATELY after the signature is found so that the
1461 ** PROM address counter is correctly positioned at the start of the
1462 ** ethernet address for later read out.
1463 */
1464 static int DevicePresent(short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
1465 {
1466   union {
1467     struct {
1468       u_long a;
1469       u_long b;
1470     } llsig;
1471     char Sig[sizeof(long) << 1];
1472   } dev;
1473   short sigLength=0;
1474   char data;
1475   int i, j, nicsr, status = 0;
1476 
1477   data = inb(DEPCA_PROM);                /* clear counter on DEPCA */
1478   data = inb(DEPCA_PROM);                /* read data */
1479 
1480   if (data == 0x08) {                    /* Enable counter on DEPCA */
1481     nicsr = inb(DEPCA_NICSR);
1482     nicsr |= AAC;
1483     outb(nicsr, DEPCA_NICSR);
1484   }
1485   
1486   dev.llsig.a = ETH_PROM_SIG;
1487   dev.llsig.b = ETH_PROM_SIG;
1488   sigLength = sizeof(long) << 1;
1489 
1490   for (i=0,j=0;j<sigLength && i<PROBE_LENGTH+sigLength-1;i++) {
1491     data = inb(DEPCA_PROM);
1492     if (dev.Sig[j] == data) {    /* track signature */
1493       j++;
1494     } else {                    /* lost signature; begin search again */
1495       if (data == dev.Sig[0]) {  /* rare case.... */
1496         j=1;
1497       } else {
1498         j=0;
1499       }
1500     }
1501   }
1502 
1503   if (j!=sigLength) {
1504     status = -ENODEV;           /* search failed */
1505   }
1506 
1507   return status;
1508 }
1509 
1510 /*
1511 ** Look for a particular board name in the EISA configuration space
1512 */
1513 static int EISA_signature(short iobase)
     /* [previous][next][first][last][top][bottom][index][help] */
1514 {
1515   unsigned long i;
1516   int status;
1517   char *signatures[] = DEPCA_SIGNATURE;
1518   char ManCode[8];
1519   union {
1520     u_long ID;
1521     u_char Id[4];
1522   } Eisa;
1523 
1524   for (i=0; i<4; i++) {
1525     Eisa.Id[i] = inb(iobase + i);
1526   }
1527 
1528   ManCode[0]=(((Eisa.Id[0]>>2)&0x1f)+0x40);
1529   ManCode[1]=(((Eisa.Id[1]&0xe0)>>5)+((Eisa.Id[0]&0x03)<<3)+0x40);
1530   ManCode[2]=(((Eisa.Id[2]>>4)&0x0f)+0x30);
1531   ManCode[3]=((Eisa.Id[2]&0x0f)+0x30);
1532   ManCode[4]=(((Eisa.Id[3]>>4)&0x0f)+0x30);
1533   ManCode[5]='\0';
1534 
1535   for (status = -ENXIO, i=0;*signatures[i] != '\0' && status;i++) {
1536     if (strstr(ManCode, signatures[i]) != NULL) {
1537       status = 0;
1538     }
1539   }
1540   
1541   return status;                            /* return the device name string */
1542 }
1543 
1544 #ifdef MODULE
1545 char kernel_version[] = UTS_RELEASE;
1546 static struct device thisDepca = {
1547   "        ",  /* device name inserted by /linux/drivers/net/net_init.c */
1548   0, 0, 0, 0,
1549   0x200, 7,   /* I/O address, IRQ */
1550   0, 0, 0, NULL, depca_probe };
1551 
1552 /*
1553  *      This is a tweak to keep the insmod program happy. It can only
1554  *      set int values with var=value so we split these out.
1555  */
1556  
1557 int irq=7;      /* EDIT THESE LINE FOR YOUR CONFIGURATION */
1558 int io=0x200;   /* Or use the irq= io= options to insmod */
1559         
1560 int
1561 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1562 {
1563   thisDepca.irq=irq;
1564   thisDepca.base_addr=io;
1565   if (register_netdev(&thisDepca) != 0)
1566     return -EIO;
1567   return 0;
1568 }
1569 
1570 void
1571 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1572 {
1573   if (MOD_IN_USE) {
1574     printk("%s: device busy, remove delayed\n",thisDepca.name);
1575   } else {
1576     release_region(thisDepca.base_addr, DEPCA_TOTAL_SIZE);
1577     unregister_netdev(&thisDepca);
1578   }
1579 }
1580 #endif /* MODULE */
1581 
1582 
1583 /*
1584  * Local variables:
1585  *  kernel-compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O2 -m486 -c depca.c"
1586  *
1587  *  module-compile-command: "gcc -D__KERNEL__ -DMODULE -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O2 -m486 -c depca.c"
1588  * End:
1589  */

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