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

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