root/drivers/net/3c505.c

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

DEFINITIONS

This source file includes following definitions.
  1. __get_order
  2. dma_mem_alloc
  3. inb_status
  4. inb_control
  5. inb_command
  6. outb_control
  7. outb_command
  8. inw_data
  9. outw_data
  10. backlog_next
  11. get_status
  12. set_hsf
  13. adapter_reset
  14. check_dma
  15. send_pcb_slow
  16. send_pcb_fast
  17. prime_rx
  18. send_pcb
  19. receive_pcb
  20. start_receive
  21. receive_packet
  22. elp_interrupt
  23. elp_open
  24. send_packet
  25. elp_start_xmit
  26. elp_get_stats
  27. elp_close
  28. elp_set_mc_list
  29. elp_init
  30. elp_sense
  31. elp_autodetect
  32. elplus_probe
  33. init_module
  34. cleanup_module

   1 /*
   2  * Linux ethernet device driver for the 3Com Etherlink Plus (3C505)
   3  *      By Craig Southeren, Juha Laiho and Philip Blundell
   4  *
   5  * 3c505.c      This module implements an interface to the 3Com
   6  *              Etherlink Plus (3c505) ethernet card. Linux device 
   7  *              driver interface reverse engineered from the Linux 3C509
   8  *              device drivers. Some 3C505 information gleaned from
   9  *              the Crynwr packet driver. Still this driver would not
  10  *              be here without 3C505 technical reference provided by
  11  *              3Com.
  12  *
  13  * $Id: 3c505.c,v 1.10 1996/04/16 13:06:27 phil Exp $
  14  *
  15  * Authors:     Linux 3c505 device driver by
  16  *                      Craig Southeren, <craigs@ineluki.apana.org.au>
  17  *              Final debugging by
  18  *                      Andrew Tridgell, <tridge@nimbus.anu.edu.au>
  19  *              Auto irq/address, tuning, cleanup and v1.1.4+ kernel mods by
  20  *                      Juha Laiho, <jlaiho@ichaos.nullnet.fi>
  21  *              Linux 3C509 driver by
  22  *                      Donald Becker, <becker@super.org>
  23  *              Crynwr packet driver by
  24  *                      Krishnan Gopalan and Gregg Stefancik,
  25  *                      Clemson University Engineering Computer Operations.
  26  *                      Portions of the code have been adapted from the 3c505
  27  *                         driver for NCSA Telnet by Bruce Orchard and later
  28  *                         modified by Warren Van Houten and krus@diku.dk.
  29  *              3C505 technical information provided by
  30  *                      Terry Murphy, of 3Com Network Adapter Division
  31  *              Linux 1.3.0 changes by
  32  *                      Alan Cox <Alan.Cox@linux.org>
  33  *              More debugging and DMA version by Philip Blundell
  34  */
  35 
  36 /* Theory of operation:
  37 
  38  * The 3c505 is quite an intelligent board.  All communication with it is done
  39  * by means of Primary Command Blocks (PCBs); these are transferred using PIO
  40  * through the command register.  The card has 256k of on-board RAM, which is
  41  * used to buffer received packets.  It might seem at first that more buffers
  42  * are better, but in fact this isn't true.  From my tests, it seems that
  43  * more than about 10 buffers are unnecessary, and there is a noticeable
  44  * performance hit in having more active on the card.  So the majority of the
  45  * card's memory isn't, in fact, used.
  46  *
  47  * We keep up to 4 "receive packet" commands active on the board at a time.
  48  * When a packet comes in, so long as there is a receive command active, the
  49  * board will send us a "packet received" PCB and then add the data for that
  50  * packet to the DMA queue.  If a DMA transfer is not already in progress, we
  51  * set one up to start uploading the data.  We have to maintain a list of
  52  * backlogged receive packets, because the card may decide to tell us about
  53  * a newly-arrived packet at any time, and we may not be able to start a DMA
  54  * transfer immediately (ie one may already be going on).  We can't NAK the
  55  * PCB, because then it would throw the packet away.
  56  *
  57  * Trying to send a PCB to the card at the wrong moment seems to have bad
  58  * effects.  If we send it a transmit PCB while a receive DMA is happening,
  59  * it will just NAK the PCB and so we will have wasted our time.  Worse, it
  60  * sometimes seems to interrupt the transfer.  The majority of the low-level
  61  * code is protected by one huge semaphore -- "busy" -- which is set whenever
  62  * it probably isn't safe to do anything to the card.  The receive routine
  63  * must gain a lock on "busy" before it can start a DMA transfer, and the
  64  * transmit routine must gain a lock before it sends the first PCB to the card.
  65  * The send_pcb() routine also has an internal semaphore to protect it against
  66  * being re-entered (which would be disastrous) -- this is needed because
  67  * several things can happen asynchronously (re-priming the receiver and
  68  * asking the card for statistics, for example).  send_pcb() will also refuse
  69  * to talk to the card at all if a DMA upload is happening.  The higher-level
  70  * networking code will reschedule a later retry if some part of the driver
  71  * is blocked.  In practice, this doesn't seem to happen very often.
  72  */
  73 
  74 /* This driver will not work with revision 2 hardware, because the host
  75  * control register is write-only.  It should be fairly easy to arrange to
  76  * keep our own soft-copy of the intended contents of this register, if
  77  * somebody has the time.  There may be firmware differences that cause
  78  * other problems, though, and I don't have an old card to test.
  79  */
  80 
  81 /* The driver is a mess.  I took Craig's and Juha's code, and hacked it firstly
  82  * to make it more reliable, and secondly to add DMA mode.  Many things could
  83  * probably be done better; the concurrency protection is particularly awful.
  84  */
  85 
  86 #include <linux/module.h>
  87 
  88 #include <linux/kernel.h>
  89 #include <linux/sched.h>
  90 #include <linux/string.h>
  91 #include <linux/interrupt.h>
  92 #include <linux/ptrace.h>
  93 #include <linux/errno.h>
  94 #include <linux/in.h>
  95 #include <linux/malloc.h>
  96 #include <linux/ioport.h>
  97 #include <asm/bitops.h>
  98 #include <asm/io.h>
  99 #include <asm/dma.h>
 100 
 101 #include <linux/netdevice.h>
 102 #include <linux/etherdevice.h>
 103 #include <linux/skbuff.h>
 104 
 105 #include "3c505.h"
 106 
 107 #define ELP_DMA      6          /* DMA channel to use */
 108 #define ELP_RX_PCBS  4
 109 
 110 /*********************************************************
 111  *
 112  *  define debug messages here as common strings to reduce space
 113  *
 114  *********************************************************/
 115 
 116 static const char *filename = __FILE__;
 117 
 118 static const char *timeout_msg = "*** timeout at %s:%s (line %d) ***\n";
 119 #define TIMEOUT_MSG(lineno) \
 120         printk(timeout_msg, filename,__FUNCTION__,(lineno))
 121 
 122 static const char *invalid_pcb_msg =
 123 "*** invalid pcb length %d at %s:%s (line %d) ***\n";
 124 #define INVALID_PCB_MSG(len) \
 125         printk(invalid_pcb_msg, (len),filename,__FUNCTION__,__LINE__)
 126 
 127 static const char *search_msg = "%s: Looking for 3c505 adapter at address %#x...";
 128 
 129 static const char *stilllooking_msg = "still looking...";
 130 
 131 static const char *found_msg = "found.\n";
 132 
 133 static const char *notfound_msg = "not found (reason = %d)\n";
 134 
 135 static const char *couldnot_msg = "%s: 3c505 not found\n";
 136 
 137 /*********************************************************
 138  *
 139  *  various other debug stuff
 140  *
 141  *********************************************************/
 142 
 143 #ifdef ELP_DEBUG
 144 static const int elp_debug = ELP_DEBUG;
 145 #else
 146 static const int elp_debug = 0;
 147 #endif
 148 
 149 /*
 150  *  0 = no messages (well, some)
 151  *  1 = messages when high level commands performed
 152  *  2 = messages when low level commands performed
 153  *  3 = messages when interrupts received
 154  */
 155 
 156 /*****************************************************************
 157  *
 158  * useful macros
 159  *
 160  *****************************************************************/
 161 
 162 #ifndef TRUE
 163 #define TRUE    1
 164 #endif
 165 
 166 #ifndef FALSE
 167 #define FALSE   0
 168 #endif
 169 
 170 
 171 /*****************************************************************
 172  *
 173  * List of I/O-addresses we try to auto-sense
 174  * Last element MUST BE 0!
 175  *****************************************************************/
 176 
 177 const int addr_list[] = {0x300, 0x280, 0x310, 0};
 178 
 179 /* Dma Memory related stuff */
 180 
 181 /* Pure 2^n version of get_order */
 182 static inline int __get_order(unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184         int order;
 185 
 186         size = (size - 1) >> (PAGE_SHIFT - 1);
 187         order = -1;
 188         do {
 189                 size >>= 1;
 190                 order++;
 191         } while (size);
 192         return order;
 193 }
 194 
 195 static unsigned long dma_mem_alloc(int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 196 {
 197         int order = __get_order(size);
 198 
 199         return __get_dma_pages(GFP_KERNEL, order);
 200 }
 201 
 202 
 203 /*****************************************************************
 204  *
 205  * Functions for I/O (note the inline !)
 206  *
 207  *****************************************************************/
 208 
 209 static inline unsigned char inb_status(unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 210 {
 211         return inb(base_addr + PORT_STATUS);
 212 }
 213 
 214 static inline unsigned char inb_control(unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216         return inb(base_addr + PORT_CONTROL);
 217 }
 218 
 219 static inline int inb_command(unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 {
 221         return inb(base_addr + PORT_COMMAND);
 222 }
 223 
 224 static inline void outb_control(unsigned char val, unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 225 {
 226         outb(val, base_addr + PORT_CONTROL);
 227 }
 228 
 229 static inline void outb_command(unsigned char val, unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 230 {
 231         outb(val, base_addr + PORT_COMMAND);
 232 }
 233 
 234 static inline unsigned int inw_data(unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236         return inw(base_addr + PORT_DATA);
 237 }
 238 
 239 static inline void outw_data(unsigned int val, unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241         outw(val, base_addr + PORT_DATA);
 242 }
 243 
 244 
 245 /*****************************************************************
 246  *
 247  *  structure to hold context information for adapter
 248  *
 249  *****************************************************************/
 250 
 251 #define DMA_BUFFER_SIZE  1600
 252 #define BACKLOG_SIZE      4
 253 
 254 typedef struct {
 255         volatile short got[NUM_TRANSMIT_CMDS];  /* flags for command completion */
 256         pcb_struct tx_pcb;      /* PCB for foreground sending */
 257         pcb_struct rx_pcb;      /* PCB for foreground receiving */
 258         pcb_struct itx_pcb;     /* PCB for background sending */
 259         pcb_struct irx_pcb;     /* PCB for background receiving */
 260         struct enet_statistics stats;
 261 
 262         void *dma_buffer;
 263 
 264         struct {
 265                 unsigned int length[BACKLOG_SIZE];
 266                 unsigned int in;
 267                 unsigned int out;
 268         } rx_backlog;
 269 
 270         struct {
 271                 unsigned int direction;
 272                 unsigned int length;
 273                 unsigned int copy_flag;
 274                 struct sk_buff *skb;
 275                 long int start_time;
 276         } current_dma;
 277 
 278         /* flags */
 279         unsigned long send_pcb_semaphore;
 280         unsigned int dmaing;
 281         unsigned long busy;
 282 
 283         unsigned int rx_active;  /* number of receive PCBs */
 284 } elp_device;
 285 
 286 static inline unsigned int backlog_next(unsigned int n)
     /* [previous][next][first][last][top][bottom][index][help] */
 287 {
 288         return (n + 1) % BACKLOG_SIZE;
 289 }
 290 
 291 /*****************************************************************
 292  *
 293  *  useful functions for accessing the adapter
 294  *
 295  *****************************************************************/
 296 
 297 /*
 298  * use this routine when accessing the ASF bits as they are
 299  * changed asynchronously by the adapter
 300  */
 301 
 302 /* get adapter PCB status */
 303 #define GET_ASF(addr) \
 304         (get_status(addr)&ASF_PCB_MASK)
 305 
 306 static inline int get_status(unsigned int base_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308         int timeout = jiffies + 10;
 309         register int stat1;
 310         do {
 311                 stat1 = inb_status(base_addr);
 312         } while (stat1 != inb_status(base_addr) && jiffies < timeout);
 313         if (jiffies >= timeout)
 314                 TIMEOUT_MSG(__LINE__);
 315         return stat1;
 316 }
 317 
 318 static inline void set_hsf(unsigned int base_addr, int hsf)
     /* [previous][next][first][last][top][bottom][index][help] */
 319 {
 320         cli();
 321         outb_control((inb_control(base_addr) & ~HSF_PCB_MASK) | hsf, base_addr);
 322         sti();
 323 }
 324 
 325 static int start_receive(struct device *, pcb_struct *);
 326 
 327 inline static void adapter_reset(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 328 {
 329         int timeout;
 330         unsigned char orig_hcr = inb_control(dev->base_addr);
 331 
 332         elp_device *adapter = dev->priv;
 333 
 334         outb_control(0, dev->base_addr);
 335 
 336         if (inb_status(dev->base_addr) & ACRF) {
 337                 do {
 338                         inb_command(dev->base_addr);
 339                         timeout = jiffies + 2;
 340                         while ((jiffies <= timeout) && !(inb_status(dev->base_addr) & ACRF));
 341                 } while (inb_status(dev->base_addr) & ACRF);
 342                 set_hsf(dev->base_addr, HSF_PCB_NAK);
 343         }
 344         outb_control(inb_control(dev->base_addr) | ATTN | DIR, dev->base_addr);
 345         timeout = jiffies + 1;
 346         while (jiffies <= timeout);
 347         outb_control(inb_control(dev->base_addr) & ~ATTN, dev->base_addr);
 348         timeout = jiffies + 1;
 349         while (jiffies <= timeout);
 350         outb_control(inb_control(dev->base_addr) | FLSH, dev->base_addr);
 351         timeout = jiffies + 1;
 352         while (jiffies <= timeout);
 353         outb_control(inb_control(dev->base_addr) & ~FLSH, dev->base_addr);
 354         timeout = jiffies + 1;
 355         while (jiffies <= timeout);
 356 
 357         outb_control(orig_hcr, dev->base_addr);
 358         if (!start_receive(dev, &adapter->tx_pcb))
 359                 printk("%s: start receive command failed \n", dev->name);
 360 }
 361 
 362 /* Check to make sure that a DMA transfer hasn't timed out.  This should never happen
 363  * in theory, but seems to occur occasionally if the card gets prodded at the wrong
 364  * time.
 365  */
 366 static inline void check_dma(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 367 {
 368         elp_device *adapter = dev->priv;
 369         if (adapter->dmaing && (jiffies > (adapter->current_dma.start_time + 10))) {
 370                 unsigned long flags;
 371                 printk("%s: DMA %s timed out, %d bytes left\n", dev->name, adapter->current_dma.direction ? "download" : "upload", get_dma_residue(dev->dma));
 372                 save_flags(flags);
 373                 cli();
 374                 adapter->dmaing = 0;
 375                 adapter->busy = 0;
 376                 disable_dma(dev->dma);
 377                 if (adapter->rx_active)
 378                         adapter->rx_active--;
 379                 outb_control(inb_control(dev->base_addr) & ~(DMAE | TCEN | DIR), dev->base_addr);
 380                 restore_flags(flags);
 381         }
 382 }
 383 
 384 /* Primitive functions used by send_pcb() */
 385 static inline unsigned int send_pcb_slow(unsigned int base_addr, unsigned char byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 386 {
 387         unsigned int timeout;
 388         outb_command(byte, base_addr);
 389         for (timeout = jiffies + 5; jiffies < timeout;) {
 390                 if (inb_status(base_addr) & HCRE)
 391                         return FALSE;
 392         }
 393         printk("3c505: send_pcb_slow timed out\n");
 394         return TRUE;
 395 }
 396 
 397 static inline unsigned int send_pcb_fast(unsigned int base_addr, unsigned char byte)
     /* [previous][next][first][last][top][bottom][index][help] */
 398 {
 399         unsigned int timeout;
 400         outb_command(byte, base_addr);
 401         for (timeout = 0; timeout < 40000; timeout++) {
 402                 if (inb_status(base_addr) & HCRE)
 403                         return FALSE;
 404         }
 405         printk("3c505: send_pcb_fast timed out\n");
 406         return TRUE;
 407 }
 408 
 409 /* Check to see if the receiver needs restarting, and kick it if so */
 410 static inline void prime_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 411 {
 412         elp_device *adapter = dev->priv;
 413         while (adapter->rx_active < ELP_RX_PCBS && dev->start) {
 414                 if (!start_receive(dev, &adapter->itx_pcb))
 415                         break;
 416         }
 417 }
 418 
 419 /*****************************************************************
 420  *
 421  * send_pcb
 422  *   Send a PCB to the adapter. 
 423  *
 424  *      output byte to command reg  --<--+
 425  *      wait until HCRE is non zero      |
 426  *      loop until all bytes sent   -->--+
 427  *      set HSF1 and HSF2 to 1
 428  *      output pcb length
 429  *      wait until ASF give ACK or NAK
 430  *      set HSF1 and HSF2 to 0
 431  *
 432  *****************************************************************/
 433 
 434 /* This can be quite slow -- the adapter is allowed to take up to 40ms
 435  * to respond to the initial interrupt.
 436  *
 437  * We run initially with interrupts turned on, but with a semaphore set
 438  * so that nobody tries to re-enter this code.  Once the first byte has
 439  * gone through, we turn interrupts off and then send the others (the
 440  * timeout is reduced to 500us).
 441  */
 442 
 443 static int send_pcb(struct device *dev, pcb_struct * pcb)
     /* [previous][next][first][last][top][bottom][index][help] */
 444 {
 445         int i;
 446         int timeout;
 447         elp_device *adapter = dev->priv;
 448 
 449         check_dma(dev);
 450 
 451         if (adapter->dmaing && adapter->current_dma.direction == 0)
 452                 return FALSE;
 453 
 454         /* Avoid contention */
 455         if (set_bit(1, &adapter->send_pcb_semaphore)) {
 456                 if (elp_debug >= 3) {
 457                         printk("%s: send_pcb entered while threaded\n", dev->name);
 458                 }
 459                 return FALSE;
 460         }
 461         /*
 462          * load each byte into the command register and
 463          * wait for the HCRE bit to indicate the adapter
 464          * had read the byte
 465          */
 466         set_hsf(dev->base_addr, 0);
 467 
 468         if (send_pcb_slow(dev->base_addr, pcb->command))
 469                 goto abort;
 470 
 471         cli();
 472 
 473         if (send_pcb_fast(dev->base_addr, pcb->length))
 474                 goto sti_abort;
 475 
 476         for (i = 0; i < pcb->length; i++) {
 477                 if (send_pcb_fast(dev->base_addr, pcb->data.raw[i]))
 478                         goto sti_abort;
 479         }
 480 
 481         outb_control(inb_control(dev->base_addr) | 3, dev->base_addr);  /* signal end of PCB */
 482         outb_command(2 + pcb->length, dev->base_addr);
 483 
 484         /* now wait for the acknowledgement */
 485         sti();
 486 
 487         for (timeout = jiffies + 5; jiffies < timeout;) {
 488                 switch (GET_ASF(dev->base_addr)) {
 489                 case ASF_PCB_ACK:
 490                         adapter->send_pcb_semaphore = 0;
 491                         return TRUE;
 492                         break;
 493                 case ASF_PCB_NAK:
 494                         cli();
 495                         printk("%s: send_pcb got NAK\n", dev->name);
 496                         goto abort;
 497                         break;
 498                 }
 499         }
 500 
 501         if (elp_debug >= 1)
 502                 printk("%s: timeout waiting for PCB acknowledge (status %02x)\n", dev->name, inb_status(dev->base_addr));
 503 
 504       sti_abort:
 505         sti();
 506       abort:
 507         adapter->send_pcb_semaphore = 0;
 508         return FALSE;
 509 }
 510 
 511 
 512 /*****************************************************************
 513  *
 514  * receive_pcb
 515  *   Read a PCB from the adapter
 516  *
 517  *      wait for ACRF to be non-zero        ---<---+
 518  *      input a byte                               |
 519  *      if ASF1 and ASF2 were not both one         |
 520  *              before byte was read, loop      --->---+
 521  *      set HSF1 and HSF2 for ack
 522  *
 523  *****************************************************************/
 524 
 525 static int receive_pcb(struct device *dev, pcb_struct * pcb)
     /* [previous][next][first][last][top][bottom][index][help] */
 526 {
 527         int i, j;
 528         int total_length;
 529         int stat;
 530         int timeout;
 531 
 532         elp_device *adapter = dev->priv;
 533 
 534         set_hsf(dev->base_addr, 0);
 535 
 536         /* get the command code */
 537         timeout = jiffies + 2;
 538         while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && jiffies < timeout);
 539         if (jiffies >= timeout) {
 540                 TIMEOUT_MSG(__LINE__);
 541                 return FALSE;
 542         }
 543         pcb->command = inb_command(dev->base_addr);
 544 
 545         /* read the data length */
 546         timeout = jiffies + 3;
 547         while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && jiffies < timeout);
 548         if (jiffies >= timeout) {
 549                 TIMEOUT_MSG(__LINE__);
 550                 printk("%s: status %02x\n", dev->name, stat);
 551                 return FALSE;
 552         }
 553         pcb->length = inb_command(dev->base_addr);
 554 
 555         if (pcb->length > MAX_PCB_DATA) {
 556                 INVALID_PCB_MSG(pcb->length);
 557                 adapter_reset(dev);
 558                 return FALSE;
 559         }
 560         /* read the data */
 561         cli();
 562         i = 0;
 563         do {
 564                 j = 0;
 565                 while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
 566                 pcb->data.raw[i++] = inb_command(dev->base_addr);
 567                 if (i > MAX_PCB_DATA)
 568                         INVALID_PCB_MSG(i);
 569         } while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
 570         sti();
 571         if (j >= 20000) {
 572                 TIMEOUT_MSG(__LINE__);
 573                 return FALSE;
 574         }
 575         /* woops, the last "data" byte was really the length! */
 576         total_length = pcb->data.raw[--i];
 577 
 578         /* safety check total length vs data length */
 579         if (total_length != (pcb->length + 2)) {
 580                 if (elp_debug >= 2)
 581                         printk("%s: mangled PCB received\n", dev->name);
 582                 set_hsf(dev->base_addr, HSF_PCB_NAK);
 583                 return FALSE;
 584         }
 585 
 586         if (pcb->command == CMD_RECEIVE_PACKET_COMPLETE) {
 587                 if (set_bit(0, (void *) &adapter->busy)) {
 588                         if (backlog_next(adapter->rx_backlog.in) == adapter->rx_backlog.out) {
 589                                 set_hsf(dev->base_addr, HSF_PCB_NAK);
 590                                 printk("%s: PCB rejected, transfer in progress and backlog full\n", dev->name);
 591                                 pcb->command = 0;
 592                                 return TRUE;
 593                         } else {
 594                                 pcb->command = 0xff;
 595                         }
 596                 }
 597         }
 598         set_hsf(dev->base_addr, HSF_PCB_ACK);
 599         return TRUE;
 600 }
 601 
 602 /******************************************************
 603  *
 604  *  queue a receive command on the adapter so we will get an
 605  *  interrupt when a packet is received.
 606  *
 607  ******************************************************/
 608 
 609 static int start_receive(struct device *dev, pcb_struct * tx_pcb)
     /* [previous][next][first][last][top][bottom][index][help] */
 610 {
 611         int status;
 612         elp_device *adapter = dev->priv;
 613 
 614         if (elp_debug >= 3)
 615                 printk("%s: restarting receiver\n", dev->name);
 616         tx_pcb->command = CMD_RECEIVE_PACKET;
 617         tx_pcb->length = sizeof(struct Rcv_pkt);
 618         tx_pcb->data.rcv_pkt.buf_seg
 619             = tx_pcb->data.rcv_pkt.buf_ofs = 0;         /* Unused */
 620         tx_pcb->data.rcv_pkt.buf_len = 1600;
 621         tx_pcb->data.rcv_pkt.timeout = 0;       /* set timeout to zero */
 622         status = send_pcb(dev, tx_pcb);
 623         if (status)
 624                 adapter->rx_active++;
 625         return status;
 626 }
 627 
 628 /******************************************************
 629  *
 630  * extract a packet from the adapter
 631  * this routine is only called from within the interrupt
 632  * service routine, so no cli/sti calls are needed
 633  * note that the length is always assumed to be even
 634  *
 635  ******************************************************/
 636 
 637 static void receive_packet(struct device *dev, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 638 {
 639         int rlen;
 640         elp_device *adapter = dev->priv;
 641         unsigned long target;
 642         struct sk_buff *skb;
 643 
 644         rlen = (len + 1) & ~1;
 645         skb = dev_alloc_skb(rlen + 2);
 646 
 647         adapter->current_dma.copy_flag = 0;
 648 
 649         if (!skb) {
 650           printk("%s: memory squeeze, dropping packet\n", dev->name);
 651           target = virt_to_bus(adapter->dma_buffer);
 652         } else {
 653           skb_reserve(skb, 2);
 654           target = virt_to_bus(skb_put(skb, rlen));
 655           if ((target + rlen) >= MAX_DMA_ADDRESS) {
 656             target = virt_to_bus(adapter->dma_buffer);
 657             adapter->current_dma.copy_flag = 1;
 658           }
 659         }
 660         /* if this happens, we die */
 661         if (set_bit(0, (void *) &adapter->dmaing))
 662                 printk("%s: rx blocked, DMA in progress, dir %d\n", dev->name, adapter->current_dma.direction);
 663 
 664         adapter->current_dma.direction = 0;
 665         adapter->current_dma.length = rlen;
 666         adapter->current_dma.skb = skb;
 667         adapter->current_dma.start_time = jiffies;
 668 
 669         outb_control(inb_control(dev->base_addr) | DIR | TCEN | DMAE, dev->base_addr);
 670 
 671         disable_dma(dev->dma);
 672         clear_dma_ff(dev->dma);
 673         set_dma_mode(dev->dma, 0x04);   /* dma read */
 674         set_dma_addr(dev->dma, target);
 675         set_dma_count(dev->dma, rlen);
 676         enable_dma(dev->dma);
 677 
 678         if (elp_debug >= 3) {
 679                 printk("%s: rx DMA transfer started\n", dev->name);
 680         }
 681         if (adapter->rx_active)
 682                 adapter->rx_active--;
 683 
 684         if (!adapter->busy)
 685                 printk("%s: receive_packet called, busy not set.\n", dev->name);
 686 }
 687 
 688 /******************************************************
 689  *
 690  * interrupt handler
 691  *
 692  ******************************************************/
 693 
 694 static void elp_interrupt(int irq, void *dev_id, struct pt_regs *reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 695 {
 696         int len;
 697         int dlen;
 698         int icount = 0;
 699         struct device *dev;
 700         elp_device *adapter;
 701         int timeout;
 702 
 703         if (irq < 0 || irq > 15) {
 704                 printk("elp_interrupt(): illegal IRQ number found in interrupt routine (%i)\n", irq);
 705                 return;
 706         }
 707         dev = irq2dev_map[irq];
 708 
 709         if (dev == NULL) {
 710                 printk("elp_interrupt(): irq %d for unknown device.\n", irq);
 711                 return;
 712         }
 713         adapter = (elp_device *) dev->priv;
 714 
 715         if (dev->interrupt) {
 716                 printk("%s: re-entering the interrupt handler!\n", dev->name);
 717                 return;
 718         }
 719         dev->interrupt = 1;
 720 
 721         do {
 722                 /*
 723                  * has a DMA transfer finished?
 724                  */
 725                 if (inb_status(dev->base_addr) & DONE) {
 726                         if (!adapter->dmaing) {
 727                                 printk("%s: phantom DMA completed\n", dev->name);
 728                         }
 729                         if (elp_debug >= 3) {
 730                                 printk("%s: %s DMA complete, status %02x\n", dev->name, adapter->current_dma.direction ? "tx" : "rx", inb_status(dev->base_addr));
 731                         }
 732 
 733                         outb_control(inb_control(dev->base_addr) & ~(DMAE | TCEN | DIR), dev->base_addr);
 734                         if (adapter->current_dma.direction) {
 735                                 dev_kfree_skb(adapter->current_dma.skb, FREE_WRITE);
 736                         } else {
 737                                 struct sk_buff *skb = adapter->current_dma.skb;
 738                                 if (skb) {
 739                                   skb->dev = dev;
 740                                   if (adapter->current_dma.copy_flag) {
 741                                     memcpy(skb_put(skb, adapter->current_dma.length), adapter->dma_buffer, adapter->current_dma.length);
 742                                   }
 743                                   skb->protocol = eth_type_trans(skb,dev);
 744                                   netif_rx(skb);
 745                                 }
 746                         }
 747                         adapter->dmaing = 0;
 748                         if (adapter->rx_backlog.in != adapter->rx_backlog.out) {
 749                                 int t = adapter->rx_backlog.length[adapter->rx_backlog.out];
 750                                 adapter->rx_backlog.out = backlog_next(adapter->rx_backlog.out);
 751                                 if (elp_debug >= 2)
 752                                         printk("%s: receiving backlogged packet (%d)\n", dev->name, t);
 753                                 receive_packet(dev, t);
 754                         } else {
 755                                 adapter->busy = 0;
 756                         }
 757                 } else {
 758                         /* has one timed out? */
 759                         check_dma(dev);
 760                 }
 761 
 762                 sti();
 763 
 764                 /*
 765                  * receive a PCB from the adapter
 766                  */
 767                 timeout = jiffies + 3;
 768                 while ((inb_status(dev->base_addr) & ACRF) != 0 && jiffies < timeout) {
 769                         if (receive_pcb(dev, &adapter->irx_pcb)) {
 770                                 switch (adapter->irx_pcb.command) {
 771                                 case 0:
 772                                         break;
 773                                         /*
 774                                          * received a packet - this must be handled fast
 775                                          */
 776                                 case 0xff:
 777                                 case CMD_RECEIVE_PACKET_COMPLETE:
 778                                         /* if the device isn't open, don't pass packets up the stack */
 779                                         if (dev->start == 0)
 780                                                 break;
 781                                         cli();
 782                                         len = adapter->irx_pcb.data.rcv_resp.pkt_len;
 783                                         dlen = adapter->irx_pcb.data.rcv_resp.buf_len;
 784                                         if (adapter->irx_pcb.data.rcv_resp.timeout != 0) {
 785                                                 printk("%s: interrupt - packet not received correctly\n", dev->name);
 786                                                 sti();
 787                                         } else {
 788                                                 if (elp_debug >= 3) {
 789                                                         sti();
 790                                                         printk("%s: interrupt - packet received of length %i (%i)\n", dev->name, len, dlen);
 791                                                         cli();
 792                                                 }
 793                                                 if (adapter->irx_pcb.command == 0xff) {
 794                                                         if (elp_debug >= 2)
 795                                                                 printk("%s: adding packet to backlog (len = %d)\n", dev->name, dlen);
 796                                                         adapter->rx_backlog.length[adapter->rx_backlog.in] = dlen;
 797                                                         adapter->rx_backlog.in = backlog_next(adapter->rx_backlog.in);
 798                                                 } else {
 799                                                         receive_packet(dev, dlen);
 800                                                 }
 801                                                 sti();
 802                                                 if (elp_debug >= 3)
 803                                                         printk("%s: packet received\n", dev->name);
 804                                         }
 805                                         break;
 806 
 807                                         /*
 808                                          * 82586 configured correctly
 809                                          */
 810                                 case CMD_CONFIGURE_82586_RESPONSE:
 811                                         adapter->got[CMD_CONFIGURE_82586] = 1;
 812                                         if (elp_debug >= 3)
 813                                                 printk("%s: interrupt - configure response received\n", dev->name);
 814                                         break;
 815 
 816                                         /*
 817                                          * Adapter memory configuration
 818                                          */
 819                                 case CMD_CONFIGURE_ADAPTER_RESPONSE:
 820                                         adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 1;
 821                                         if (elp_debug >= 3)
 822                                                 printk("%s: Adapter memory configuration %s.\n", dev->name,
 823                                                        adapter->irx_pcb.data.failed ? "failed" : "succeeded");
 824                                         break;
 825 
 826                                         /*
 827                                          * Multicast list loading
 828                                          */
 829                                 case CMD_LOAD_MULTICAST_RESPONSE:
 830                                         adapter->got[CMD_LOAD_MULTICAST_LIST] = 1;
 831                                         if (elp_debug >= 3)
 832                                                 printk("%s: Multicast address list loading %s.\n", dev->name,
 833                                                        adapter->irx_pcb.data.failed ? "failed" : "succeeded");
 834                                         break;
 835 
 836                                         /*
 837                                          * Station address setting
 838                                          */
 839                                 case CMD_SET_ADDRESS_RESPONSE:
 840                                         adapter->got[CMD_SET_STATION_ADDRESS] = 1;
 841                                         if (elp_debug >= 3)
 842                                                 printk("%s: Ethernet address setting %s.\n", dev->name,
 843                                                        adapter->irx_pcb.data.failed ? "failed" : "succeeded");
 844                                         break;
 845 
 846 
 847                                         /*
 848                                          * received board statistics
 849                                          */
 850                                 case CMD_NETWORK_STATISTICS_RESPONSE:
 851                                         adapter->stats.rx_packets += adapter->irx_pcb.data.netstat.tot_recv;
 852                                         adapter->stats.tx_packets += adapter->irx_pcb.data.netstat.tot_xmit;
 853                                         adapter->stats.rx_crc_errors += adapter->irx_pcb.data.netstat.err_CRC;
 854                                         adapter->stats.rx_frame_errors += adapter->irx_pcb.data.netstat.err_align;
 855                                         adapter->stats.rx_fifo_errors += adapter->irx_pcb.data.netstat.err_ovrrun;
 856                                         adapter->stats.rx_over_errors += adapter->irx_pcb.data.netstat.err_res;
 857                                         adapter->got[CMD_NETWORK_STATISTICS] = 1;
 858                                         if (elp_debug >= 3)
 859                                                 printk("%s: interrupt - statistics response received\n", dev->name);
 860                                         break;
 861 
 862                                         /*
 863                                          * sent a packet
 864                                          */
 865                                 case CMD_TRANSMIT_PACKET_COMPLETE:
 866                                         if (elp_debug >= 3)
 867                                                 printk("%s: interrupt - packet sent\n", dev->name);
 868                                         if (dev->start == 0)
 869                                                 break;
 870                                         switch (adapter->irx_pcb.data.xmit_resp.c_stat) {
 871                                         case 0xffff:
 872                                                 adapter->stats.tx_aborted_errors++;
 873                                                 printk(KERN_INFO "%s: transmit timed out, network cable problem?\n", dev->name);
 874                                                 break;
 875                                         case 0xfffe:
 876                                                 adapter->stats.tx_fifo_errors++;
 877                                                 printk(KERN_INFO "%s: transmit timed out, FIFO underrun\n", dev->name);
 878                                                 break;
 879                                         }
 880                                         dev->tbusy = 0;
 881                                         mark_bh(NET_BH);
 882                                         break;
 883 
 884                                         /*
 885                                          * some unknown PCB
 886                                          */
 887                                 default:
 888                                         printk(KERN_DEBUG "%s: unknown PCB received - %2.2x\n", dev->name, adapter->irx_pcb.command);
 889                                         break;
 890                                 }
 891                         } else {
 892                                 printk("%s: failed to read PCB on interrupt\n", dev->name);
 893                                 adapter_reset(dev);
 894                         }
 895                 }
 896 
 897         } while (icount++ < 5 && (inb_status(dev->base_addr) & (ACRF | DONE)));
 898 
 899         prime_rx(dev);
 900 
 901         /*
 902          * indicate no longer in interrupt routine
 903          */
 904         dev->interrupt = 0;
 905 }
 906 
 907 
 908 /******************************************************
 909  *
 910  * open the board
 911  *
 912  ******************************************************/
 913 
 914 static int elp_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 915 {
 916         elp_device *adapter;
 917 
 918         adapter = dev->priv;
 919 
 920         if (elp_debug >= 3)
 921                 printk("%s: request to open device\n", dev->name);
 922 
 923         /*
 924          * make sure we actually found the device
 925          */
 926         if (adapter == NULL) {
 927                 printk("%s: Opening a non-existent physical device\n", dev->name);
 928                 return -EAGAIN;
 929         }
 930         /*
 931          * disable interrupts on the board
 932          */
 933         outb_control(0x00, dev->base_addr);
 934 
 935         /*
 936          * clear any pending interrupts
 937          */
 938         inb_command(dev->base_addr);
 939         adapter_reset(dev);
 940 
 941         /*
 942          * interrupt routine not entered
 943          */
 944         dev->interrupt = 0;
 945 
 946         /*
 947          *  transmitter not busy 
 948          */
 949         dev->tbusy = 0;
 950 
 951         /*
 952          * no receive PCBs active
 953          */
 954         adapter->rx_active = 0;
 955 
 956         adapter->busy = 0;
 957         adapter->send_pcb_semaphore = 0;
 958         adapter->rx_backlog.in = 0;
 959         adapter->rx_backlog.out = 0;
 960 
 961         /*
 962          * make sure we can find the device header given the interrupt number
 963          */
 964         irq2dev_map[dev->irq] = dev;
 965 
 966         /*
 967          * install our interrupt service routine
 968          */
 969         if (request_irq(dev->irq, &elp_interrupt, 0, "3c505", NULL)) {
 970                 irq2dev_map[dev->irq] = NULL;
 971                 return -EAGAIN;
 972         }
 973         if (request_dma(dev->dma, "3c505")) {
 974                 printk("%s: could not allocate DMA channel\n", dev->name);
 975                 return -EAGAIN;
 976         }
 977         adapter->dma_buffer = (void *) dma_mem_alloc(DMA_BUFFER_SIZE);
 978         if (!adapter->dma_buffer) {
 979                 printk("Could not allocate DMA buffer\n");
 980         }
 981         adapter->dmaing = 0;
 982 
 983         /*
 984          * enable interrupts on the board
 985          */
 986         outb_control(CMDE, dev->base_addr);
 987 
 988         /*
 989          * device is now officially open!
 990          */
 991         dev->start = 1;
 992 
 993         /*
 994          * configure adapter memory: we need 10 multicast addresses, default==0
 995          */
 996         if (elp_debug >= 3)
 997                 printk("%s: sending 3c505 memory configuration command\n", dev->name);
 998         adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
 999         adapter->tx_pcb.data.memconf.cmd_q = 10;
1000         adapter->tx_pcb.data.memconf.rcv_q = 20;
1001         adapter->tx_pcb.data.memconf.mcast = 10;
1002         adapter->tx_pcb.data.memconf.frame = 20;
1003         adapter->tx_pcb.data.memconf.rcv_b = 20;
1004         adapter->tx_pcb.data.memconf.progs = 0;
1005         adapter->tx_pcb.length = sizeof(struct Memconf);
1006         adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] = 0;
1007         if (!send_pcb(dev, &adapter->tx_pcb))
1008                 printk("%s: couldn't send memory configuration command\n", dev->name);
1009         else {
1010                 int timeout = jiffies + TIMEOUT;
1011                 while (adapter->got[CMD_CONFIGURE_ADAPTER_MEMORY] == 0 && jiffies < timeout);
1012                 if (jiffies >= timeout)
1013                         TIMEOUT_MSG(__LINE__);
1014         }
1015 
1016 
1017         /*
1018          * configure adapter to receive broadcast messages and wait for response
1019          */
1020         if (elp_debug >= 3)
1021                 printk("%s: sending 82586 configure command\n", dev->name);
1022         adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1023         adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1024         adapter->tx_pcb.length = 2;
1025         adapter->got[CMD_CONFIGURE_82586] = 0;
1026         if (!send_pcb(dev, &adapter->tx_pcb))
1027                 printk("%s: couldn't send 82586 configure command\n", dev->name);
1028         else {
1029                 int timeout = jiffies + TIMEOUT;
1030                 while (adapter->got[CMD_CONFIGURE_82586] == 0 && jiffies < timeout);
1031                 if (jiffies >= timeout)
1032                         TIMEOUT_MSG(__LINE__);
1033         }
1034 
1035         /* enable burst-mode DMA */
1036         outb(0x1, dev->base_addr + PORT_AUXDMA);
1037 
1038         /*
1039          * queue receive commands to provide buffering
1040          */
1041         prime_rx(dev);
1042         if (elp_debug >= 3)
1043                 printk("%s: %d receive PCBs active\n", dev->name, adapter->rx_active);
1044 
1045         MOD_INC_USE_COUNT;
1046 
1047         return 0;               /* Always succeed */
1048 }
1049 
1050 
1051 /******************************************************
1052  *
1053  * send a packet to the adapter
1054  *
1055  ******************************************************/
1056 
1057 static int send_packet(struct device *dev, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
1058 {
1059         elp_device *adapter = dev->priv;
1060         unsigned long target;
1061 
1062         /*
1063          * make sure the length is even and no shorter than 60 bytes
1064          */
1065         unsigned int nlen = (((skb->len < 60) ? 60 : skb->len) + 1) & (~1);
1066 
1067         if (set_bit(0, (void *) &adapter->busy)) {
1068                 if (elp_debug >= 2)
1069                         printk("%s: transmit blocked\n", dev->name);
1070                 return FALSE;
1071         }
1072         adapter = dev->priv;
1073 
1074         /*
1075          * send the adapter a transmit packet command. Ignore segment and offset
1076          * and make sure the length is even
1077          */
1078         adapter->tx_pcb.command = CMD_TRANSMIT_PACKET;
1079         adapter->tx_pcb.length = sizeof(struct Xmit_pkt);
1080         adapter->tx_pcb.data.xmit_pkt.buf_ofs
1081             = adapter->tx_pcb.data.xmit_pkt.buf_seg = 0;        /* Unused */
1082         adapter->tx_pcb.data.xmit_pkt.pkt_len = nlen;
1083 
1084         if (!send_pcb(dev, &adapter->tx_pcb)) {
1085                 adapter->busy = 0;
1086                 return FALSE;
1087         }
1088         /* if this happens, we die */
1089         if (set_bit(0, (void *) &adapter->dmaing))
1090                 printk("%s: tx: DMA %d in progress\n", dev->name, adapter->current_dma.direction);
1091 
1092         adapter->current_dma.direction = 1;
1093         adapter->current_dma.start_time = jiffies;
1094 
1095         target = virt_to_bus(skb->data);
1096         if ((target + nlen) >= MAX_DMA_ADDRESS) {
1097                 memcpy(adapter->dma_buffer, skb->data, nlen);
1098                 target = virt_to_bus(adapter->dma_buffer);
1099         }
1100         adapter->current_dma.skb = skb;
1101         cli();
1102         disable_dma(dev->dma);
1103         clear_dma_ff(dev->dma);
1104         set_dma_mode(dev->dma, 0x08);   /* dma memory -> io */
1105         set_dma_addr(dev->dma, target);
1106         set_dma_count(dev->dma, nlen);
1107         enable_dma(dev->dma);
1108         outb_control(inb_control(dev->base_addr) | DMAE | TCEN, dev->base_addr);
1109         if (elp_debug >= 3)
1110                 printk("%s: DMA transfer started\n", dev->name);
1111 
1112         return TRUE;
1113 }
1114 
1115 /******************************************************
1116  *
1117  * start the transmitter
1118  *    return 0 if sent OK, else return 1
1119  *
1120  ******************************************************/
1121 
1122 static int elp_start_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1123 {
1124         if (dev->interrupt) {
1125                 printk("%s: start_xmit aborted (in irq)\n", dev->name);
1126                 return 1;
1127         }
1128 
1129         check_dma(dev);
1130 
1131         /*
1132          * if the transmitter is still busy, we have a transmit timeout...
1133          */
1134         if (dev->tbusy) {
1135                 elp_device *adapter = dev->priv;
1136                 int tickssofar = jiffies - dev->trans_start;
1137                 int stat;
1138 
1139                 if (tickssofar < 1000)
1140                         return 1;
1141 
1142                 stat = inb_status(dev->base_addr);
1143                 printk("%s: transmit timed out, lost %s?\n", dev->name, (stat & ACRF) ? "interrupt" : "command");
1144                 if (elp_debug >= 1)
1145                         printk("%s: status %#02x\n", dev->name, stat);
1146                 dev->trans_start = jiffies;
1147                 dev->tbusy = 0;
1148                 adapter->stats.tx_dropped++;
1149         }
1150 
1151         /* Some upper layer thinks we've missed a tx-done interrupt */
1152         if (skb == NULL) {
1153                 dev_tint(dev);
1154                 return 0;
1155         }
1156 
1157         if (skb->len <= 0)
1158                 return 0;
1159 
1160         if (elp_debug >= 3)
1161                 printk("%s: request to send packet of length %d\n", dev->name, (int) skb->len);
1162 
1163         if (set_bit(0, (void *) &dev->tbusy)) {
1164                 printk("%s: transmitter access conflict\n", dev->name);
1165                 return 1;
1166         }
1167         /*
1168          * send the packet at skb->data for skb->len
1169          */
1170         if (!send_packet(dev, skb)) {
1171                 if (elp_debug >= 2) {
1172                         printk("%s: failed to transmit packet\n", dev->name);
1173                 }
1174                 dev->tbusy = 0;
1175                 return 1;
1176         }
1177         if (elp_debug >= 3)
1178                 printk("%s: packet of length %d sent\n", dev->name, (int) skb->len);
1179 
1180         /*
1181          * start the transmit timeout
1182          */
1183         dev->trans_start = jiffies;
1184 
1185         prime_rx(dev);
1186 
1187         return 0;
1188 }
1189 
1190 /******************************************************
1191  *
1192  * return statistics on the board
1193  *
1194  ******************************************************/
1195 
1196 static struct enet_statistics *elp_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1197 {
1198         elp_device *adapter = (elp_device *) dev->priv;
1199 
1200         if (elp_debug >= 3)
1201                 printk("%s: request for stats\n", dev->name);
1202 
1203         /* If the device is closed, just return the latest stats we have,
1204            - we cannot ask from the adapter without interrupts */
1205         if (!dev->start)
1206                 return &adapter->stats;
1207 
1208         /* send a get statistics command to the board */
1209         adapter->tx_pcb.command = CMD_NETWORK_STATISTICS;
1210         adapter->tx_pcb.length = 0;
1211         adapter->got[CMD_NETWORK_STATISTICS] = 0;
1212         if (!send_pcb(dev, &adapter->tx_pcb))
1213                 printk("%s: couldn't send get statistics command\n", dev->name);
1214         else {
1215                 int timeout = jiffies + TIMEOUT;
1216                 while (adapter->got[CMD_NETWORK_STATISTICS] == 0 && jiffies < timeout);
1217                 if (jiffies >= timeout) {
1218                         TIMEOUT_MSG(__LINE__);
1219                         return &adapter->stats;
1220                 }
1221         }
1222 
1223         /* statistics are now up to date */
1224         return &adapter->stats;
1225 }
1226 
1227 /******************************************************
1228  *
1229  * close the board
1230  *
1231  ******************************************************/
1232 
1233 static int elp_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1234 {
1235         elp_device *adapter;
1236 
1237         adapter = dev->priv;
1238 
1239         if (elp_debug >= 3)
1240                 printk("%s: request to close device\n", dev->name);
1241 
1242         /* Someone may request the device statistic information even when
1243          * the interface is closed. The following will update the statistics
1244          * structure in the driver, so we'll be able to give current statistics.
1245          */
1246         (void) elp_get_stats(dev);
1247 
1248         /*
1249          * disable interrupts on the board
1250          */
1251         outb_control(0x00, dev->base_addr);
1252 
1253         /*
1254          *  flag transmitter as busy (i.e. not available)
1255          */
1256         dev->tbusy = 1;
1257 
1258         /*
1259          *  indicate device is closed
1260          */
1261         dev->start = 0;
1262 
1263         /*
1264          * release the IRQ
1265          */
1266         free_irq(dev->irq, NULL);
1267 
1268         /*
1269          * and we no longer have to map irq to dev either
1270          */
1271         irq2dev_map[dev->irq] = 0;
1272 
1273         free_dma(dev->dma);
1274         free_pages((unsigned long) adapter->dma_buffer, __get_order(DMA_BUFFER_SIZE));
1275 
1276         MOD_DEC_USE_COUNT;
1277 
1278         return 0;
1279 }
1280 
1281 
1282 /************************************************************
1283  *
1284  * Set multicast list
1285  * num_addrs==0: clear mc_list
1286  * num_addrs==-1: set promiscuous mode
1287  * num_addrs>0: set mc_list
1288  *
1289  ************************************************************/
1290 
1291 static void elp_set_mc_list(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1292 {
1293         elp_device *adapter = (elp_device *) dev->priv;
1294         struct dev_mc_list *dmi = dev->mc_list;
1295         int i;
1296 
1297         if (elp_debug >= 3)
1298                 printk("%s: request to set multicast list\n", dev->name);
1299 
1300         if (!(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1301                 /* send a "load multicast list" command to the board, max 10 addrs/cmd */
1302                 /* if num_addrs==0 the list will be cleared */
1303                 adapter->tx_pcb.command = CMD_LOAD_MULTICAST_LIST;
1304                 adapter->tx_pcb.length = 6 * dev->mc_count;
1305                 for (i = 0; i < dev->mc_count; i++) {
1306                         memcpy(adapter->tx_pcb.data.multicast[i], dmi->dmi_addr, 6);
1307                         dmi = dmi->next;
1308                 }
1309                 adapter->got[CMD_LOAD_MULTICAST_LIST] = 0;
1310                 if (!send_pcb(dev, &adapter->tx_pcb))
1311                         printk("%s: couldn't send set_multicast command\n", dev->name);
1312                 else {
1313                         int timeout = jiffies + TIMEOUT;
1314                         while (adapter->got[CMD_LOAD_MULTICAST_LIST] == 0 && jiffies < timeout);
1315                         if (jiffies >= timeout) {
1316                                 TIMEOUT_MSG(__LINE__);
1317                         }
1318                 }
1319                 if (dev->mc_count)
1320                         adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD | RECV_MULTI;
1321                 else            /* num_addrs == 0 */
1322                         adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_BROAD;
1323         } else
1324                 adapter->tx_pcb.data.configure = NO_LOOPBACK | RECV_PROMISC;
1325         /*
1326          * configure adapter to receive messages (as specified above)
1327          * and wait for response
1328          */
1329         if (elp_debug >= 3)
1330                 printk("%s: sending 82586 configure command\n", dev->name);
1331         adapter->tx_pcb.command = CMD_CONFIGURE_82586;
1332         adapter->tx_pcb.length = 2;
1333         adapter->got[CMD_CONFIGURE_82586] = 0;
1334         if (!send_pcb(dev, &adapter->tx_pcb))
1335                 printk("%s: couldn't send 82586 configure command\n", dev->name);
1336         else {
1337                 int timeout = jiffies + TIMEOUT;
1338                 while (adapter->got[CMD_CONFIGURE_82586] == 0 && jiffies < timeout);
1339                 if (jiffies >= timeout)
1340                         TIMEOUT_MSG(__LINE__);
1341         }
1342 }
1343 
1344 /******************************************************
1345  *
1346  * initialise Etherlink Plus board
1347  *
1348  ******************************************************/
1349 
1350 static void elp_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1351 {
1352         elp_device *adapter = dev->priv;
1353 
1354         /*
1355          * set ptrs to various functions
1356          */
1357         dev->open = elp_open;   /* local */
1358         dev->stop = elp_close;  /* local */
1359         dev->get_stats = elp_get_stats;         /* local */
1360         dev->hard_start_xmit = elp_start_xmit;  /* local */
1361         dev->set_multicast_list = elp_set_mc_list;      /* local */
1362 
1363         /* Setup the generic properties */
1364         ether_setup(dev);
1365 
1366         /*
1367          * setup ptr to adapter specific information
1368          */
1369         memset(&(adapter->stats), 0, sizeof(struct enet_statistics));
1370 
1371         /*
1372          * memory information
1373          */
1374         dev->mem_start = dev->mem_end = dev->rmem_end = dev->rmem_start = 0;
1375 }
1376 
1377 /************************************************************
1378  *
1379  * A couple of tests to see if there's 3C505 or not
1380  * Called only by elp_autodetect
1381  ************************************************************/
1382 
1383 static int elp_sense(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1384 {
1385         int timeout;
1386         int addr = dev->base_addr;
1387         const char *name = dev->name;
1388         long flags;
1389         byte orig_HCR, orig_HSR;
1390 
1391         if (check_region(addr, 0xf))
1392                 return -1;
1393 
1394         orig_HCR = inb_control(addr);
1395         orig_HSR = inb_status(addr);
1396 
1397         if (elp_debug > 0)
1398                 printk(search_msg, name, addr);
1399 
1400         if (((orig_HCR == 0xff) && (orig_HSR == 0xff)) ||
1401             ((orig_HCR & DIR) != (orig_HSR & DIR))) {
1402                 if (elp_debug > 0)
1403                         printk(notfound_msg, 1);
1404                 return -1;      /* It can't be 3c505 if HCR.DIR != HSR.DIR */
1405         }
1406         /* Enable interrupts - we need timers! */
1407         save_flags(flags);
1408         sti();
1409 
1410         /* Wait for a while; the adapter may still be booting up */
1411         if (elp_debug > 0)
1412                 printk(stilllooking_msg);
1413         if (orig_HCR & DIR) {
1414                 /* If HCR.DIR is up, we pull it down. HSR.DIR should follow. */
1415                 outb_control(orig_HCR & ~DIR, addr);
1416                 timeout = jiffies + 30;
1417                 while (jiffies < timeout);
1418                 restore_flags(flags);
1419                 if (inb_status(addr) & DIR) {
1420                         outb_control(orig_HCR, addr);
1421                         if (elp_debug > 0)
1422                                 printk(notfound_msg, 2);
1423                         return -1;
1424                 }
1425         } else {
1426                 /* If HCR.DIR is down, we pull it up. HSR.DIR should follow. */
1427                 outb_control(orig_HCR | DIR, addr);
1428                 timeout = jiffies + 30;
1429                 while (jiffies < timeout);
1430                 restore_flags(flags);
1431                 if (!(inb_status(addr) & DIR)) {
1432                         outb_control(orig_HCR, addr);
1433                         if (elp_debug > 0)
1434                                 printk(notfound_msg, 3);
1435                         return -1;
1436                 }
1437         }
1438         /*
1439          * It certainly looks like a 3c505. If it has DMA enabled, it needs
1440          * a hard reset. Also, do a hard reset if selected at the compile time.
1441          */
1442         if (elp_debug > 0)
1443                 printk(found_msg);
1444 
1445         return 0;
1446 }
1447 
1448 /*************************************************************
1449  *
1450  * Search through addr_list[] and try to find a 3C505
1451  * Called only by eplus_probe
1452  *************************************************************/
1453 
1454 static int elp_autodetect(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1455 {
1456         int idx = 0;
1457 
1458         /* if base address set, then only check that address
1459            otherwise, run through the table */
1460         if (dev->base_addr != 0) {      /* dev->base_addr == 0 ==> plain autodetect */
1461                 if (elp_sense(dev) == 0)
1462                         return dev->base_addr;
1463         } else
1464                 while ((dev->base_addr = addr_list[idx++])) {
1465                         if (elp_sense(dev) == 0)
1466                                 return dev->base_addr;
1467                 }
1468 
1469         /* could not find an adapter */
1470         if (elp_debug > 0)
1471                 printk(couldnot_msg, dev->name);
1472 
1473         return 0;               /* Because of this, the layer above will return -ENODEV */
1474 }
1475 
1476 
1477 /******************************************************
1478  *
1479  * probe for an Etherlink Plus board at the specified address
1480  *
1481  ******************************************************/
1482 
1483 /* There are three situations we need to be able to detect here:
1484 
1485  *  a) the card is idle
1486  *  b) the card is still booting up
1487  *  c) the card is stuck in a strange state (some DOS drivers do this)
1488  *
1489  * In case (a), all is well.  In case (b), we wait 10 seconds to see if the
1490  * card finishes booting, and carry on if so.  In case (c), we do a hard reset,
1491  * loop round, and hope for the best.
1492  *
1493  * This is all very unpleasant, but hopefully avoids the problems with the old
1494  * probe code (which had a 15-second delay if the card was idle, and didn't
1495  * work at all if it was in a weird state).
1496  */
1497 
1498 int elplus_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1499 {
1500         elp_device *adapter;
1501         int i, tries, tries1, timeout, okay;
1502 
1503         /*
1504          *  setup adapter structure
1505          */
1506 
1507         dev->base_addr = elp_autodetect(dev);
1508         if (!(dev->base_addr))
1509                 return -ENODEV;
1510 
1511         /*
1512          * setup ptr to adapter specific information
1513          */
1514         adapter = (elp_device *) (dev->priv = kmalloc(sizeof(elp_device), GFP_KERNEL));
1515         if (adapter == NULL) {
1516                 printk("%s: out of memory\n", dev->name);
1517                 return -ENODEV;
1518         }
1519 
1520         for (tries1 = 0; tries1 < 3; tries1++) {
1521                 outb_control((inb_control(dev->base_addr) | CMDE) & ~DIR, dev->base_addr);
1522                 /* First try to write just one byte, to see if the card is
1523                  * responding at all normally.
1524                  */
1525                 timeout = jiffies + 5;
1526                 okay = 0;
1527                 while (jiffies < timeout && !(inb_status(dev->base_addr) & HCRE));
1528                 if ((inb_status(dev->base_addr) & HCRE)) {
1529                         outb_command(0, dev->base_addr);        /* send a spurious byte */
1530                         timeout = jiffies + 5;
1531                         while (jiffies < timeout && !(inb_status(dev->base_addr) & HCRE));
1532                         if (inb_status(dev->base_addr) & HCRE)
1533                                 okay = 1;
1534                 }
1535                 if (!okay) {
1536                         /* Nope, it's ignoring the command register.  This means that
1537                          * either it's still booting up, or it's died.
1538                          */
1539                         printk("%s: command register wouldn't drain, ", dev->name);
1540                         if ((inb_status(dev->base_addr) & 7) == 3) {
1541                                 /* If the adapter status is 3, it *could* still be booting.
1542                                  * Give it the benefit of the doubt for 10 seconds.
1543                                  */
1544                                 printk("assuming 3c505 still starting\n");
1545                                 timeout = jiffies + 10 * HZ;
1546                                 while (jiffies < timeout && (inb_status(dev->base_addr) & 7));
1547                                 if (inb_status(dev->base_addr) & 7) {
1548                                         printk("%s: 3c505 failed to start\n", dev->name);
1549                                 } else {
1550                                         okay = 1;  /* It started */
1551                                 }
1552                         } else {
1553                                 /* Otherwise, it must just be in a strange state.  We probably
1554                                  * need to kick it.
1555                                  */
1556                                 printk("3c505 is sulking\n");
1557                         }
1558                 }
1559                 for (tries = 0; tries < 5 && okay; tries++) {
1560 
1561                         /*
1562                          * Try to set the Ethernet address, to make sure that the board
1563                          * is working.
1564                          */
1565                         adapter->tx_pcb.command = CMD_STATION_ADDRESS;
1566                         adapter->tx_pcb.length = 0;
1567                         autoirq_setup(0);
1568                         if (!send_pcb(dev, &adapter->tx_pcb)) {
1569                                 printk("%s: could not send first PCB\n", dev->name);
1570                                 autoirq_report(0);
1571                                 continue;
1572                         }
1573                         if (!receive_pcb(dev, &adapter->rx_pcb)) {
1574                                 printk("%s: could not read first PCB\n", dev->name);
1575                                 autoirq_report(0);
1576                                 continue;
1577                         }
1578                         if ((adapter->rx_pcb.command != CMD_ADDRESS_RESPONSE) ||
1579                             (adapter->rx_pcb.length != 6)) {
1580                                 printk("%s: first PCB wrong (%d, %d)\n", dev->name, adapter->rx_pcb.command, adapter->rx_pcb.length);
1581                                 autoirq_report(0);
1582                                 continue;
1583                         }
1584                         goto okay;
1585                 }
1586                 /* It's broken.  Do a hard reset to re-initialise the board,
1587                  * and try again.
1588                  */
1589                 printk(KERN_INFO "%s: resetting adapter\n", dev->name);
1590                 outb_control(inb_control(dev->base_addr) | FLSH | ATTN, dev->base_addr);
1591                 outb_control(inb_control(dev->base_addr) & ~(FLSH | ATTN), dev->base_addr);
1592         }
1593         printk("%s: failed to initialise 3c505\n", dev->name);
1594         return -ENODEV;
1595 
1596       okay:
1597         if (dev->irq) {         /* Is there a preset IRQ? */
1598                 int rpt = autoirq_report(0);
1599                 if (dev->irq != rpt) {
1600                         printk("%s: warning, irq %d configured but %d detected\n", dev->name, dev->irq, rpt);
1601                         return -ENODEV;
1602                 }
1603                 /* if dev->irq == autoirq_report(0), all is well */
1604         } else                  /* No preset IRQ; just use what we can detect */
1605                 dev->irq = autoirq_report(0);
1606         switch (dev->irq) {     /* Legal, sane? */
1607         case 0:
1608                 printk("%s: No IRQ reported by autoirq_report().\n", dev->name);
1609                 printk("%s: Check the jumpers of your 3c505 board.\n", dev->name);
1610                 return -ENODEV;
1611         case 1:
1612         case 6:
1613         case 8:
1614         case 13:
1615                 printk("%s: Impossible IRQ %d reported by autoirq_report().\n",
1616                        dev->name, dev->irq);
1617                 return -ENODEV;
1618         }
1619         /*
1620          *  Now we have the IRQ number so we can disable the interrupts from
1621          *  the board until the board is opened.
1622          */
1623         outb_control(inb_control(dev->base_addr) & ~CMDE, dev->base_addr);
1624 
1625         /*
1626          * copy ethernet address into structure
1627          */
1628         for (i = 0; i < 6; i++)
1629                 dev->dev_addr[i] = adapter->rx_pcb.data.eth_addr[i];
1630 
1631         /* set up the DMA channel */
1632         dev->dma = ELP_DMA;
1633 
1634         /*
1635          * print remainder of startup message
1636          */
1637         printk("%s: 3c505 at %#lx, irq %d, dma %d, ",
1638                dev->name, dev->base_addr, dev->irq, dev->dma);
1639         printk("addr %02x:%02x:%02x:%02x:%02x:%02x, ",
1640                dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1641                dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1642 
1643         /*
1644          * read more information from the adapter
1645          */
1646 
1647         adapter->tx_pcb.command = CMD_ADAPTER_INFO;
1648         adapter->tx_pcb.length = 0;
1649         if (!send_pcb(dev, &adapter->tx_pcb) ||
1650             !receive_pcb(dev, &adapter->rx_pcb) ||
1651             (adapter->rx_pcb.command != CMD_ADAPTER_INFO_RESPONSE) ||
1652             (adapter->rx_pcb.length != 10)) {
1653                 printk("%s: not responding to second PCB\n", dev->name);
1654         }
1655         printk("rev %d.%d, %dk\n", adapter->rx_pcb.data.info.major_vers, adapter->rx_pcb.data.info.minor_vers, adapter->rx_pcb.data.info.RAM_sz);
1656 
1657         /*
1658          * reconfigure the adapter memory to better suit our purposes
1659          */
1660         adapter->tx_pcb.command = CMD_CONFIGURE_ADAPTER_MEMORY;
1661         adapter->tx_pcb.length = 12;
1662         adapter->tx_pcb.data.memconf.cmd_q = 8;
1663         adapter->tx_pcb.data.memconf.rcv_q = 8;
1664         adapter->tx_pcb.data.memconf.mcast = 10;
1665         adapter->tx_pcb.data.memconf.frame = 10;
1666         adapter->tx_pcb.data.memconf.rcv_b = 10;
1667         adapter->tx_pcb.data.memconf.progs = 0;
1668         if (!send_pcb(dev, &adapter->tx_pcb) ||
1669             !receive_pcb(dev, &adapter->rx_pcb) ||
1670             (adapter->rx_pcb.command != CMD_CONFIGURE_ADAPTER_RESPONSE) ||
1671             (adapter->rx_pcb.length != 2)) {
1672                 printk("%s: could not configure adapter memory\n", dev->name);
1673         }
1674         if (adapter->rx_pcb.data.configure) {
1675                 printk("%s: adapter configuration failed\n", dev->name);
1676         }
1677         /*
1678          * and reserve the address region
1679          */
1680         request_region(dev->base_addr, ELP_IO_EXTENT, "3c505");
1681 
1682         /*
1683          * initialise the device
1684          */
1685         elp_init(dev);
1686 
1687         return 0;
1688 }
1689 
1690 #ifdef MODULE
1691 static char devicename[9] = {0,};
1692 static struct device dev_3c505 =
1693 {
1694         devicename,             /* device name is inserted by linux/drivers/net/net_init.c */
1695         0, 0, 0, 0,
1696         0, 0,
1697         0, 0, 0, NULL, elplus_probe};
1698 
1699 int io = 0x300;
1700 int irq = 0;
1701 
1702 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1703 {
1704         if (io == 0)
1705                 printk("3c505: You should not use auto-probing with insmod!\n");
1706         dev_3c505.base_addr = io;
1707         dev_3c505.irq = irq;
1708         if (register_netdev(&dev_3c505) != 0) {
1709                 return -EIO;
1710         }
1711         return 0;
1712 }
1713 
1714 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1715 {
1716         unregister_netdev(&dev_3c505);
1717         kfree(dev_3c505.priv);
1718         dev_3c505.priv = NULL;
1719 
1720         /* If we don't do this, we can't re-insmod it later. */
1721         release_region(dev_3c505.base_addr, ELP_IO_EXTENT);
1722 }
1723 
1724 #endif                          /* MODULE */
1725 
1726 
1727 /*
1728  * Local Variables:
1729  *  c-file-style: "linux"
1730  *  tab-width: 8
1731  *  compile-command: "gcc -D__KERNEL__ -I/discs/bibble/src/linux-1.3.69/include  -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strength-reduce -pipe -m486 -DCPU=486 -DMODULE  -c 3c505.c"
1732  * End:
1733  */

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