linux-bk/drivers/net/3c527.c
<<
>>
Prefs
   1/* 3c527.c: 3Com Etherlink/MC32 driver for Linux 2.4
   2 *
   3 *      (c) Copyright 1998 Red Hat Software Inc
   4 *      Written by Alan Cox. 
   5 *      Further debugging by Carl Drougge.
   6 *      Modified by Richard Procter (rnp@netlink.co.nz)
   7 *
   8 *      Based on skeleton.c written 1993-94 by Donald Becker and ne2.c
   9 *      (for the MCA stuff) written by Wim Dumon.
  10 *
  11 *      Thanks to 3Com for making this possible by providing me with the
  12 *      documentation.
  13 *
  14 *      This software may be used and distributed according to the terms
  15 *      of the GNU General Public License, incorporated herein by reference.
  16 *
  17 */
  18
  19#define DRV_NAME                "3c527"
  20#define DRV_VERSION             "0.6a"
  21#define DRV_RELDATE             "2001/11/17"
  22
  23static const char *version =
  24DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Richard Proctor (rnp@netlink.co.nz)\n";
  25
  26/**
  27 * DOC: Traps for the unwary
  28 *
  29 *      The diagram (Figure 1-1) and the POS summary disagree with the
  30 *      "Interrupt Level" section in the manual.
  31 *
  32 *      The manual contradicts itself when describing the minimum number 
  33 *      buffers in the 'configure lists' command. 
  34 *      My card accepts a buffer config of 4/4. 
  35 *
  36 *      Setting the SAV BP bit does not save bad packets, but
  37 *      only enables RX on-card stats collection. 
  38 *
  39 *      The documentation in places seems to miss things. In actual fact
  40 *      I've always eventually found everything is documented, it just
  41 *      requires careful study.
  42 *
  43 * DOC: Theory Of Operation
  44 *
  45 *      The 3com 3c527 is a 32bit MCA bus mastering adapter with a large
  46 *      amount of on board intelligence that housekeeps a somewhat dumber
  47 *      Intel NIC. For performance we want to keep the transmit queue deep
  48 *      as the card can transmit packets while fetching others from main
  49 *      memory by bus master DMA. Transmission and reception are driven by
  50 *      circular buffer queues.
  51 *
  52 *      The mailboxes can be used for controlling how the card traverses
  53 *      its buffer rings, but are used only for inital setup in this
  54 *      implementation.  The exec mailbox allows a variety of commands to
  55 *      be executed. Each command must complete before the next is
  56 *      executed. Primarily we use the exec mailbox for controlling the
  57 *      multicast lists.  We have to do a certain amount of interesting
  58 *      hoop jumping as the multicast list changes can occur in interrupt
  59 *      state when the card has an exec command pending. We defer such
  60 *      events until the command completion interrupt.
  61 *
  62 *      A copy break scheme (taken from 3c59x.c) is employed whereby
  63 *      received frames exceeding a configurable length are passed
  64 *      directly to the higher networking layers without incuring a copy,
  65 *      in what amounts to a time/space trade-off.
  66 *       
  67 *      The card also keeps a large amount of statistical information
  68 *      on-board. In a perfect world, these could be used safely at no
  69 *      cost. However, lacking information to the contrary, processing
  70 *      them without races would involve so much extra complexity as to
  71 *      make it unworthwhile to do so. In the end, a hybrid SW/HW
  72 *      implementation was made necessary --- see mc32_update_stats().  
  73 *
  74 * DOC: Notes
  75 *      
  76 *      It should be possible to use two or more cards, but at this stage
  77 *      only by loading two copies of the same module.
  78 *
  79 *      The on-board 82586 NIC has trouble receiving multiple
  80 *      back-to-back frames and so is likely to drop packets from fast
  81 *      senders.
  82**/
  83
  84#include <linux/module.h>
  85
  86#include <linux/errno.h>
  87#include <linux/netdevice.h>
  88#include <linux/etherdevice.h>
  89#include <linux/if_ether.h>
  90#include <linux/init.h>
  91#include <linux/kernel.h>
  92#include <linux/types.h>
  93#include <linux/fcntl.h>
  94#include <linux/interrupt.h>
  95#include <linux/mca-legacy.h>
  96#include <linux/ioport.h>
  97#include <linux/in.h>
  98#include <linux/skbuff.h>
  99#include <linux/slab.h>
 100#include <linux/string.h>
 101#include <linux/wait.h>
 102#include <linux/ethtool.h>
 103
 104#include <asm/uaccess.h>
 105#include <asm/system.h>
 106#include <asm/bitops.h>
 107#include <asm/io.h>
 108#include <asm/dma.h>
 109
 110#include "3c527.h"
 111
 112MODULE_LICENSE("GPL");
 113
 114/*
 115 * The name of the card. Is used for messages and in the requests for
 116 * io regions, irqs and dma channels
 117 */
 118static const char* cardname = DRV_NAME;
 119
 120/* use 0 for production, 1 for verification, >2 for debug */
 121#ifndef NET_DEBUG
 122#define NET_DEBUG 2
 123#endif
 124
 125#undef DEBUG_IRQ
 126
 127static unsigned int mc32_debug = NET_DEBUG;
 128
 129/* The number of low I/O ports used by the ethercard. */
 130#define MC32_IO_EXTENT  8
 131
 132/* As implemented, values must be a power-of-2 -- 4/8/16/32 */ 
 133#define TX_RING_LEN     32       /* Typically the card supports 37  */
 134#define RX_RING_LEN     8        /*     "       "        "          */
 135
 136/* Copy break point, see above for details. 
 137 * Setting to > 1512 effectively disables this feature. */          
 138#define RX_COPYBREAK    200      /* Value from 3c59x.c */
 139
 140/* Issue the 82586 workaround command - this is for "busy lans", but
 141 * basically means for all lans now days - has a performance (latency) 
 142 * cost, but best set. */ 
 143static const int WORKAROUND_82586=1;
 144
 145/* Pointers to buffers and their on-card records */
 146
 147struct mc32_ring_desc 
 148{
 149        volatile struct skb_header *p;                    
 150        struct sk_buff *skb;          
 151};
 152
 153
 154/* Information that needs to be kept for each board. */
 155struct mc32_local 
 156{
 157        struct net_device_stats net_stats;
 158        int slot;
 159        volatile struct mc32_mailbox *rx_box;
 160        volatile struct mc32_mailbox *tx_box;
 161        volatile struct mc32_mailbox *exec_box;
 162        volatile struct mc32_stats *stats;    /* Start of on-card statistics */
 163        u16 tx_chain;           /* Transmit list start offset */
 164        u16 rx_chain;           /* Receive list start offset */
 165        u16 tx_len;             /* Transmit list count */ 
 166        u16 rx_len;             /* Receive list count */
 167
 168        u32 base;
 169        u16 exec_pending;
 170        u16 mc_reload_wait;     /* a multicast load request is pending */
 171        u32 mc_list_valid;      /* True when the mclist is set */
 172        u16 xceiver_state;      /* Current transceiver state. bitmapped */ 
 173        u16 desired_state;      /* The state we want the transceiver to be in */ 
 174        atomic_t tx_count;      /* buffers left */
 175        wait_queue_head_t event;
 176
 177        struct mc32_ring_desc tx_ring[TX_RING_LEN];     /* Host Transmit ring */
 178        struct mc32_ring_desc rx_ring[RX_RING_LEN];     /* Host Receive ring */
 179
 180        u16 tx_ring_tail;       /* index to tx de-queue end */
 181        u16 tx_ring_head;       /* index to tx en-queue end */
 182
 183        u16 rx_ring_tail;       /* index to rx de-queue end */ 
 184};
 185
 186/* The station (ethernet) address prefix, used for a sanity check. */
 187#define SA_ADDR0 0x02
 188#define SA_ADDR1 0x60
 189#define SA_ADDR2 0xAC
 190
 191struct mca_adapters_t {
 192        unsigned int    id;
 193        char            *name;
 194};
 195
 196const struct mca_adapters_t mc32_adapters[] = {
 197        { 0x0041, "3COM EtherLink MC/32" },
 198        { 0x8EF5, "IBM High Performance Lan Adapter" },
 199        { 0x0000, NULL }
 200};
 201
 202
 203/* Macros for ring index manipulations */ 
 204static inline u16 next_rx(u16 rx) { return (rx+1)&(RX_RING_LEN-1); };
 205static inline u16 prev_rx(u16 rx) { return (rx-1)&(RX_RING_LEN-1); };
 206
 207static inline u16 next_tx(u16 tx) { return (tx+1)&(TX_RING_LEN-1); };
 208
 209
 210/* Index to functions, as function prototypes. */
 211extern int mc32_probe(struct net_device *dev);
 212
 213static int      mc32_probe1(struct net_device *dev, int ioaddr);
 214static int      mc32_command(struct net_device *dev, u16 cmd, void *data, int len);
 215static int      mc32_open(struct net_device *dev);
 216static void     mc32_timeout(struct net_device *dev);
 217static int      mc32_send_packet(struct sk_buff *skb, struct net_device *dev);
 218static irqreturn_t mc32_interrupt(int irq, void *dev_id, struct pt_regs *regs);
 219static int      mc32_close(struct net_device *dev);
 220static struct   net_device_stats *mc32_get_stats(struct net_device *dev);
 221static void     mc32_set_multicast_list(struct net_device *dev);
 222static void     mc32_reset_multicast_list(struct net_device *dev);
 223static struct ethtool_ops netdev_ethtool_ops;
 224
 225/**
 226 * mc32_probe   -       Search for supported boards
 227 * @dev: device to probe
 228 *
 229 * Because MCA bus is a real bus and we can scan for cards we could do a
 230 * single scan for all boards here. Right now we use the passed in device
 231 * structure and scan for only one board. This needs fixing for modules
 232 * in particular.
 233 */
 234
 235int __init mc32_probe(struct net_device *dev)
 236{
 237        static int current_mca_slot = -1;
 238        int i;
 239        int adapter_found = 0;
 240
 241        SET_MODULE_OWNER(dev);
 242
 243        /* Do not check any supplied i/o locations. 
 244           POS registers usually don't fail :) */
 245
 246        /* MCA cards have POS registers.  
 247           Autodetecting MCA cards is extremely simple. 
 248           Just search for the card. */
 249
 250        for(i = 0; (mc32_adapters[i].name != NULL) && !adapter_found; i++) {
 251                current_mca_slot = 
 252                        mca_find_unused_adapter(mc32_adapters[i].id, 0);
 253
 254                if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
 255                        if(!mc32_probe1(dev, current_mca_slot))
 256                        {
 257                                mca_set_adapter_name(current_mca_slot, 
 258                                                mc32_adapters[i].name);
 259                                mca_mark_as_used(current_mca_slot);
 260                                return 0;
 261                        }
 262                        
 263                }
 264        }
 265        return -ENODEV;
 266}
 267
 268/**
 269 * mc32_probe1  -       Check a given slot for a board and test the card
 270 * @dev:  Device structure to fill in
 271 * @slot: The MCA bus slot being used by this card
 272 *
 273 * Decode the slot data and configure the card structures. Having done this we
 274 * can reset the card and configure it. The card does a full self test cycle
 275 * in firmware so we have to wait for it to return and post us either a 
 276 * failure case or some addresses we use to find the board internals.
 277 */
 278
 279static int __init mc32_probe1(struct net_device *dev, int slot)
 280{
 281        static unsigned version_printed;
 282        int i, err;
 283        u8 POS;
 284        u32 base;
 285        struct mc32_local *lp;
 286        static u16 mca_io_bases[]={
 287                0x7280,0x7290,
 288                0x7680,0x7690,
 289                0x7A80,0x7A90,
 290                0x7E80,0x7E90
 291        };
 292        static u32 mca_mem_bases[]={
 293                0x00C0000,
 294                0x00C4000,
 295                0x00C8000,
 296                0x00CC000,
 297                0x00D0000,
 298                0x00D4000,
 299                0x00D8000,
 300                0x00DC000
 301        };
 302        static char *failures[]={
 303                "Processor instruction",
 304                "Processor data bus",
 305                "Processor data bus",
 306                "Processor data bus",
 307                "Adapter bus",
 308                "ROM checksum",
 309                "Base RAM",
 310                "Extended RAM",
 311                "82586 internal loopback",
 312                "82586 initialisation failure",
 313                "Adapter list configuration error"
 314        };
 315
 316        /* Time to play MCA games */
 317
 318        if (mc32_debug  &&  version_printed++ == 0)
 319                printk(KERN_DEBUG "%s", version);
 320
 321        printk(KERN_INFO "%s: %s found in slot %d:", dev->name, cardname, slot);
 322
 323        POS = mca_read_stored_pos(slot, 2);
 324        
 325        if(!(POS&1))
 326        {
 327                printk(" disabled.\n");
 328                return -ENODEV;
 329        }
 330
 331        /* Fill in the 'dev' fields. */
 332        dev->base_addr = mca_io_bases[(POS>>1)&7];
 333        dev->mem_start = mca_mem_bases[(POS>>4)&7];
 334        
 335        POS = mca_read_stored_pos(slot, 4);
 336        if(!(POS&1))
 337        {
 338                printk("memory window disabled.\n");
 339                return -ENODEV;
 340        }
 341
 342        POS = mca_read_stored_pos(slot, 5);
 343        
 344        i=(POS>>4)&3;
 345        if(i==3)
 346        {
 347                printk("invalid memory window.\n");
 348                return -ENODEV;
 349        }
 350        
 351        i*=16384;
 352        i+=16384;
 353        
 354        dev->mem_end=dev->mem_start + i;
 355        
 356        dev->irq = ((POS>>2)&3)+9;
 357        
 358        if(!request_region(dev->base_addr, MC32_IO_EXTENT, cardname))
 359        {
 360                printk("io 0x%3lX, which is busy.\n", dev->base_addr);
 361                return -EBUSY;
 362        }
 363
 364        printk("io 0x%3lX irq %d mem 0x%lX (%dK)\n",
 365                dev->base_addr, dev->irq, dev->mem_start, i/1024);
 366        
 367        
 368        /* We ought to set the cache line size here.. */
 369        
 370        
 371        /*
 372         *      Go PROM browsing
 373         */
 374         
 375        printk("%s: Address ", dev->name);
 376         
 377        /* Retrieve and print the ethernet address. */
 378        for (i = 0; i < 6; i++)
 379        {
 380                mca_write_pos(slot, 6, i+12);
 381                mca_write_pos(slot, 7, 0);
 382        
 383                printk(" %2.2x", dev->dev_addr[i] = mca_read_pos(slot,3));
 384        }
 385
 386        mca_write_pos(slot, 6, 0);
 387        mca_write_pos(slot, 7, 0);
 388
 389        POS = mca_read_stored_pos(slot, 4);
 390        
 391        if(POS&2)
 392                printk(" : BNC port selected.\n");
 393        else 
 394                printk(" : AUI port selected.\n");
 395                
 396        POS=inb(dev->base_addr+HOST_CTRL);
 397        POS|=HOST_CTRL_ATTN|HOST_CTRL_RESET;
 398        POS&=~HOST_CTRL_INTE;
 399        outb(POS, dev->base_addr+HOST_CTRL);
 400        /* Reset adapter */
 401        udelay(100);
 402        /* Reset off */
 403        POS&=~(HOST_CTRL_ATTN|HOST_CTRL_RESET);
 404        outb(POS, dev->base_addr+HOST_CTRL);
 405        
 406        udelay(300);
 407        
 408        /*
 409         *      Grab the IRQ
 410         */
 411
 412        i = request_irq(dev->irq, &mc32_interrupt, SA_SHIRQ, dev->name, dev);
 413        if (i) {
 414                release_region(dev->base_addr, MC32_IO_EXTENT);
 415                printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq);
 416                return i;
 417        }
 418
 419
 420        /* Initialize the device structure. */
 421        dev->priv = kmalloc(sizeof(struct mc32_local), GFP_KERNEL);
 422        if (dev->priv == NULL)
 423        {
 424                err = -ENOMEM;
 425                goto err_exit_irq; 
 426        }
 427
 428        memset(dev->priv, 0, sizeof(struct mc32_local));
 429        lp = dev->priv;
 430        lp->slot = slot;
 431
 432        i=0;
 433
 434        base = inb(dev->base_addr);
 435        
 436        while(base == 0xFF)
 437        {
 438                i++;
 439                if(i == 1000)
 440                {
 441                        printk(KERN_ERR "%s: failed to boot adapter.\n", dev->name);
 442                        err = -ENODEV; 
 443                        goto err_exit_free;
 444                }
 445                udelay(1000);
 446                if(inb(dev->base_addr+2)&(1<<5))
 447                        base = inb(dev->base_addr);
 448        }
 449
 450        if(base>0)
 451        {
 452                if(base < 0x0C)
 453                        printk(KERN_ERR "%s: %s%s.\n", dev->name, failures[base-1],
 454                                base<0x0A?" test failure":"");
 455                else
 456                        printk(KERN_ERR "%s: unknown failure %d.\n", dev->name, base);
 457                err = -ENODEV; 
 458                goto err_exit_free;
 459        }
 460        
 461        base=0;
 462        for(i=0;i<4;i++)
 463        {
 464                int n=0;
 465        
 466                while(!(inb(dev->base_addr+2)&(1<<5)))
 467                {
 468                        n++;
 469                        udelay(50);
 470                        if(n>100)
 471                        {
 472                                printk(KERN_ERR "%s: mailbox read fail (%d).\n", dev->name, i);
 473                                err = -ENODEV;
 474                                goto err_exit_free;
 475                        }
 476                }
 477
 478                base|=(inb(dev->base_addr)<<(8*i));
 479        }
 480        
 481        lp->exec_box=isa_bus_to_virt(dev->mem_start+base);
 482        
 483        base=lp->exec_box->data[1]<<16|lp->exec_box->data[0];  
 484        
 485        lp->base = dev->mem_start+base;
 486        
 487        lp->rx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[2]); 
 488        lp->tx_box=isa_bus_to_virt(lp->base + lp->exec_box->data[3]);
 489        
 490        lp->stats = isa_bus_to_virt(lp->base + lp->exec_box->data[5]);
 491
 492        /*
 493         *      Descriptor chains (card relative)
 494         */
 495         
 496        lp->tx_chain            = lp->exec_box->data[8];   /* Transmit list start offset */
 497        lp->rx_chain            = lp->exec_box->data[10];  /* Receive list start offset */
 498        lp->tx_len              = lp->exec_box->data[9];   /* Transmit list count */ 
 499        lp->rx_len              = lp->exec_box->data[11];  /* Receive list count */
 500
 501        init_waitqueue_head(&lp->event);
 502        
 503        printk("%s: Firmware Rev %d. %d RX buffers, %d TX buffers. Base of 0x%08X.\n",
 504                dev->name, lp->exec_box->data[12], lp->rx_len, lp->tx_len, lp->base);
 505
 506        dev->open               = mc32_open;
 507        dev->stop               = mc32_close;
 508        dev->hard_start_xmit    = mc32_send_packet;
 509        dev->get_stats          = mc32_get_stats;
 510        dev->set_multicast_list = mc32_set_multicast_list;
 511        dev->tx_timeout         = mc32_timeout;
 512        dev->watchdog_timeo     = HZ*5; /* Board does all the work */
 513        dev->ethtool_ops        = &netdev_ethtool_ops;
 514        
 515        lp->xceiver_state = HALTED; 
 516        
 517        lp->tx_ring_tail=lp->tx_ring_head=0;
 518
 519        /* Fill in the fields of the device structure with ethernet values. */
 520        ether_setup(dev);
 521        
 522        return 0;
 523
 524err_exit_free:
 525        kfree(dev->priv);
 526err_exit_irq:
 527        free_irq(dev->irq, dev);
 528        release_region(dev->base_addr, MC32_IO_EXTENT);
 529        return err;
 530}
 531
 532
 533/**
 534 *      mc32_ready_poll         -       wait until we can feed it a command
 535 *      @dev:   The device to wait for
 536 *      
 537 *      Wait until the card becomes ready to accept a command via the
 538 *      command register. This tells us nothing about the completion
 539 *      status of any pending commands and takes very little time at all.
 540 */
 541 
 542static void mc32_ready_poll(struct net_device *dev)
 543{
 544        int ioaddr = dev->base_addr;
 545        while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
 546}
 547
 548
 549/**
 550 *      mc32_command_nowait     -       send a command non blocking
 551 *      @dev: The 3c527 to issue the command to
 552 *      @cmd: The command word to write to the mailbox
 553 *      @data: A data block if the command expects one
 554 *      @len: Length of the data block
 555 *
 556 *      Send a command from interrupt state. If there is a command
 557 *      currently being executed then we return an error of -1. It simply
 558 *      isn't viable to wait around as commands may be slow. Providing we
 559 *      get in, we busy wait for the board to become ready to accept the
 560 *      command and issue it. We do not wait for the command to complete
 561 *      --- the card will interrupt us when it's done.
 562 */
 563
 564static int mc32_command_nowait(struct net_device *dev, u16 cmd, void *data, int len)
 565{
 566        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 567        int ioaddr = dev->base_addr;
 568
 569        if(lp->exec_pending)
 570                return -1;
 571        
 572        lp->exec_pending=3;
 573        lp->exec_box->mbox=0;
 574        lp->exec_box->mbox=cmd;
 575        memcpy((void *)lp->exec_box->data, data, len);
 576        barrier();      /* the memcpy forgot the volatile so be sure */
 577
 578        /* Send the command */
 579        while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
 580        outb(1<<6, ioaddr+HOST_CMD);    
 581        return 0;
 582}
 583
 584
 585/**
 586 *      mc32_command    -       send a command and sleep until completion
 587 *      @dev: The 3c527 card to issue the command to
 588 *      @cmd: The command word to write to the mailbox
 589 *      @data: A data block if the command expects one
 590 *      @len: Length of the data block
 591 *
 592 *      Sends exec commands in a user context. This permits us to wait around
 593 *      for the replies and also to wait for the command buffer to complete
 594 *      from a previous command before we execute our command. After our 
 595 *      command completes we will complete any pending multicast reload
 596 *      we blocked off by hogging the exec buffer.
 597 *
 598 *      You feed the card a command, you wait, it interrupts you get a 
 599 *      reply. All well and good. The complication arises because you use
 600 *      commands for filter list changes which come in at bh level from things
 601 *      like IPV6 group stuff.
 602 *
 603 *      We have a simple state machine
 604 *
 605 *      0       - nothing issued
 606 *
 607 *      1       - command issued, wait reply
 608 *
 609 *      2       - reply waiting - reader then goes to state 0
 610 *
 611 *      3       - command issued, trash reply. In which case the irq
 612 *                takes it back to state 0
 613 *
 614 */
 615  
 616static int mc32_command(struct net_device *dev, u16 cmd, void *data, int len)
 617{
 618        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 619        int ioaddr = dev->base_addr;
 620        unsigned long flags;
 621        int ret = 0;
 622        
 623        /*
 624         *      Wait for a command
 625         */
 626         
 627        save_flags(flags);
 628        cli();
 629         
 630        while(lp->exec_pending)
 631                sleep_on(&lp->event);
 632                
 633        /*
 634         *      Issue mine
 635         */
 636
 637        lp->exec_pending=1;
 638        
 639        restore_flags(flags);
 640        
 641        lp->exec_box->mbox=0;
 642        lp->exec_box->mbox=cmd;
 643        memcpy((void *)lp->exec_box->data, data, len);
 644        barrier();      /* the memcpy forgot the volatile so be sure */
 645
 646        /* Send the command */
 647        while(!(inb(ioaddr+HOST_STATUS)&HOST_STATUS_CRR));
 648        outb(1<<6, ioaddr+HOST_CMD);    
 649
 650        save_flags(flags);
 651        cli();
 652
 653        while(lp->exec_pending!=2)
 654                sleep_on(&lp->event);
 655        lp->exec_pending=0;
 656        restore_flags(flags);
 657        
 658        if(lp->exec_box->mbox&(1<<13))
 659                ret = -1;
 660
 661        /*
 662         *      A multicast set got blocked - do it now
 663         */
 664                
 665        if(lp->mc_reload_wait)
 666        {
 667                mc32_reset_multicast_list(dev);
 668        }
 669
 670        return ret;
 671}
 672
 673
 674/**
 675 *      mc32_start_transceiver  -       tell board to restart tx/rx
 676 *      @dev: The 3c527 card to issue the command to
 677 *
 678 *      This may be called from the interrupt state, where it is used
 679 *      to restart the rx ring if the card runs out of rx buffers. 
 680 *      
 681 *      First, we check if it's ok to start the transceiver. We then show
 682 *      the card where to start in the rx ring and issue the
 683 *      commands to start reception and transmission. We don't wait
 684 *      around for these to complete.
 685 */ 
 686
 687static void mc32_start_transceiver(struct net_device *dev) {
 688
 689        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 690        int ioaddr = dev->base_addr;
 691
 692        /* Ignore RX overflow on device closure */ 
 693        if (lp->desired_state==HALTED)  
 694                return; 
 695
 696        mc32_ready_poll(dev); 
 697
 698        lp->tx_box->mbox=0;
 699        lp->rx_box->mbox=0;
 700
 701        /* Give the card the offset to the post-EOL-bit RX descriptor */ 
 702        lp->rx_box->data[0]=lp->rx_ring[prev_rx(lp->rx_ring_tail)].p->next; 
 703
 704        outb(HOST_CMD_START_RX, ioaddr+HOST_CMD);      
 705
 706        mc32_ready_poll(dev); 
 707        outb(HOST_CMD_RESTRT_TX, ioaddr+HOST_CMD);   /* card ignores this on RX restart */ 
 708        
 709        /* We are not interrupted on start completion */ 
 710        lp->xceiver_state=RUNNING; 
 711}
 712
 713
 714/**
 715 *      mc32_halt_transceiver   -       tell board to stop tx/rx
 716 *      @dev: The 3c527 card to issue the command to
 717 *
 718 *      We issue the commands to halt the card's transceiver. In fact,
 719 *      after some experimenting we now simply tell the card to
 720 *      suspend. When issuing aborts occasionally odd things happened.
 721 *
 722 *      We then sleep until the card has notified us that both rx and
 723 *      tx have been suspended.
 724 */ 
 725
 726static void mc32_halt_transceiver(struct net_device *dev) 
 727{
 728        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 729        int ioaddr = dev->base_addr;
 730        unsigned long flags;
 731
 732        mc32_ready_poll(dev);   
 733
 734        lp->tx_box->mbox=0;
 735        lp->rx_box->mbox=0;
 736
 737        outb(HOST_CMD_SUSPND_RX, ioaddr+HOST_CMD);                      
 738        mc32_ready_poll(dev); 
 739        outb(HOST_CMD_SUSPND_TX, ioaddr+HOST_CMD);      
 740                
 741        save_flags(flags);
 742        cli();
 743                
 744        while(lp->xceiver_state!=HALTED) 
 745                sleep_on(&lp->event); 
 746                
 747        restore_flags(flags);   
 748} 
 749
 750
 751/**
 752 *      mc32_load_rx_ring       -       load the ring of receive buffers
 753 *      @dev: 3c527 to build the ring for
 754 *
 755 *      This initalises the on-card and driver datastructures to
 756 *      the point where mc32_start_transceiver() can be called.
 757 *
 758 *      The card sets up the receive ring for us. We are required to use the
 759 *      ring it provides although we can change the size of the ring.
 760 *
 761 *      We allocate an sk_buff for each ring entry in turn and
 762 *      initalise its house-keeping info. At the same time, we read
 763 *      each 'next' pointer in our rx_ring array. This reduces slow
 764 *      shared-memory reads and makes it easy to access predecessor
 765 *      descriptors.
 766 *
 767 *      We then set the end-of-list bit for the last entry so that the
 768 *      card will know when it has run out of buffers.
 769 */
 770         
 771static int mc32_load_rx_ring(struct net_device *dev)
 772{
 773        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 774        int i;
 775        u16 rx_base;
 776        volatile struct skb_header *p;
 777        
 778        rx_base=lp->rx_chain;
 779
 780        for(i=0;i<RX_RING_LEN;i++)
 781        {
 782                lp->rx_ring[i].skb=alloc_skb(1532, GFP_KERNEL);
 783                skb_reserve(lp->rx_ring[i].skb, 18);  
 784
 785                if(lp->rx_ring[i].skb==NULL)
 786                {
 787                        for(;i>=0;i--)
 788                                kfree_skb(lp->rx_ring[i].skb);
 789                        return -ENOBUFS;
 790                }
 791                
 792                p=isa_bus_to_virt(lp->base+rx_base);
 793                                
 794                p->control=0;
 795                p->data=isa_virt_to_bus(lp->rx_ring[i].skb->data);
 796                p->status=0;
 797                p->length=1532;
 798        
 799                lp->rx_ring[i].p=p; 
 800                rx_base=p->next; 
 801        }
 802
 803        lp->rx_ring[i-1].p->control |= CONTROL_EOL;
 804
 805        lp->rx_ring_tail=0;
 806
 807        return 0;
 808}       
 809
 810
 811/**
 812 *      mc32_flush_rx_ring      -       free the ring of receive buffers
 813 *      @lp: Local data of 3c527 to flush the rx ring of
 814 *
 815 *      Free the buffer for each ring slot. This may be called 
 816 *      before mc32_load_rx_ring(), eg. on error in mc32_open().
 817 */
 818
 819static void mc32_flush_rx_ring(struct net_device *dev)
 820{
 821        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 822        
 823        struct sk_buff *skb;
 824        int i; 
 825
 826        for(i=0; i < RX_RING_LEN; i++) 
 827        { 
 828                skb = lp->rx_ring[i].skb;
 829                if (skb!=NULL) {
 830                        kfree_skb(skb);
 831                        skb=NULL; 
 832                }
 833                lp->rx_ring[i].p=NULL; 
 834        } 
 835}
 836
 837
 838/**
 839 *      mc32_load_tx_ring       -       load transmit ring
 840 *      @dev: The 3c527 card to issue the command to
 841 *
 842 *      This sets up the host transmit data-structures. 
 843 *
 844 *      First, we obtain from the card it's current postion in the tx
 845 *      ring, so that we will know where to begin transmitting
 846 *      packets.
 847 *      
 848 *      Then, we read the 'next' pointers from the on-card tx ring into
 849 *      our tx_ring array to reduce slow shared-mem reads. Finally, we
 850 *      intitalise the tx house keeping variables.
 851 * 
 852 */ 
 853
 854static void mc32_load_tx_ring(struct net_device *dev)
 855{ 
 856        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 857        volatile struct skb_header *p;
 858        int i; 
 859        u16 tx_base;
 860
 861        tx_base=lp->tx_box->data[0]; 
 862
 863        for(i=0;i<lp->tx_len;i++) 
 864        {
 865                p=isa_bus_to_virt(lp->base+tx_base);
 866                lp->tx_ring[i].p=p; 
 867                lp->tx_ring[i].skb=NULL;
 868
 869                tx_base=p->next;
 870        }
 871
 872        /* -1 so that tx_ring_head cannot "lap" tx_ring_tail,           */
 873        /* which would be bad news for mc32_tx_ring as cur. implemented */ 
 874
 875        atomic_set(&lp->tx_count, TX_RING_LEN-1); 
 876        lp->tx_ring_head=lp->tx_ring_tail=0; 
 877} 
 878
 879
 880/**
 881 *      mc32_flush_tx_ring      -       free transmit ring
 882 *      @lp: Local data of 3c527 to flush the tx ring of
 883 *
 884 *      We have to consider two cases here. We want to free the pending
 885 *      buffers only. If the ring buffer head is past the start then the
 886 *      ring segment we wish to free wraps through zero. The tx ring 
 887 *      house-keeping variables are then reset.
 888 */
 889
 890static void mc32_flush_tx_ring(struct net_device *dev)
 891{
 892        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 893        
 894        if(lp->tx_ring_tail!=lp->tx_ring_head)
 895        {
 896                int i;  
 897                if(lp->tx_ring_tail < lp->tx_ring_head)
 898                {
 899                        for(i=lp->tx_ring_tail;i<lp->tx_ring_head;i++)
 900                        {
 901                                dev_kfree_skb(lp->tx_ring[i].skb);
 902                                lp->tx_ring[i].skb=NULL;
 903                                lp->tx_ring[i].p=NULL; 
 904                        }
 905                }
 906                else
 907                {
 908                        for(i=lp->tx_ring_tail; i<TX_RING_LEN; i++) 
 909                        {
 910                                dev_kfree_skb(lp->tx_ring[i].skb);
 911                                lp->tx_ring[i].skb=NULL;
 912                                lp->tx_ring[i].p=NULL; 
 913                        }
 914                        for(i=0; i<lp->tx_ring_head; i++) 
 915                        {
 916                                dev_kfree_skb(lp->tx_ring[i].skb);
 917                                lp->tx_ring[i].skb=NULL;
 918                                lp->tx_ring[i].p=NULL; 
 919                        }
 920                }
 921        }
 922        
 923        atomic_set(&lp->tx_count, 0); 
 924        lp->tx_ring_tail=lp->tx_ring_head=0;
 925}
 926        
 927
 928/**
 929 *      mc32_open       -       handle 'up' of card
 930 *      @dev: device to open
 931 *
 932 *      The user is trying to bring the card into ready state. This requires
 933 *      a brief dialogue with the card. Firstly we enable interrupts and then
 934 *      'indications'. Without these enabled the card doesn't bother telling
 935 *      us what it has done. This had me puzzled for a week.
 936 *
 937 *      We configure the number of card descriptors, then load the network
 938 *      address and multicast filters. Turn on the workaround mode. This
 939 *      works around a bug in the 82586 - it asks the firmware to do
 940 *      so. It has a performance (latency) hit but is needed on busy
 941 *      [read most] lans. We load the ring with buffers then we kick it
 942 *      all off.
 943 */
 944
 945static int mc32_open(struct net_device *dev)
 946{
 947        int ioaddr = dev->base_addr;
 948        struct mc32_local *lp = (struct mc32_local *)dev->priv;
 949        u8 one=1;
 950        u8 regs;
 951        u16 descnumbuffs[2] = {TX_RING_LEN, RX_RING_LEN};
 952
 953        /*
 954         *      Interrupts enabled
 955         */
 956
 957        regs=inb(ioaddr+HOST_CTRL);
 958        regs|=HOST_CTRL_INTE;
 959        outb(regs, ioaddr+HOST_CTRL);
 960        
 961
 962        /*
 963         *      Send the indications on command
 964         */
 965
 966        mc32_command(dev, 4, &one, 2);
 967
 968        /*
 969         *      Poke it to make sure it's really dead. 
 970         */
 971
 972        mc32_halt_transceiver(dev); 
 973        mc32_flush_tx_ring(dev); 
 974
 975        /* 
 976         *      Ask card to set up on-card descriptors to our spec 
 977         */ 
 978
 979        if(mc32_command(dev, 8, descnumbuffs, 4)) { 
 980                printk("%s: %s rejected our buffer configuration!\n",
 981                       dev->name, cardname);
 982                mc32_close(dev); 
 983                return -ENOBUFS; 
 984        }
 985        
 986        /* Report new configuration */ 
 987        mc32_command(dev, 6, NULL, 0); 
 988
 989        lp->tx_chain            = lp->exec_box->data[8];   /* Transmit list start offset */
 990        lp->rx_chain            = lp->exec_box->data[10];  /* Receive list start offset */
 991        lp->tx_len              = lp->exec_box->data[9];   /* Transmit list count */ 
 992        lp->rx_len              = lp->exec_box->data[11];  /* Receive list count */
 993 
 994        /* Set Network Address */
 995        mc32_command(dev, 1, dev->dev_addr, 6);
 996        
 997        /* Set the filters */
 998        mc32_set_multicast_list(dev);
 999                   
1000        if (WORKAROUND_82586) { 
1001                u16 zero_word=0;
1002                mc32_command(dev, 0x0D, &zero_word, 2);   /* 82586 bug workaround on  */
1003        }
1004
1005        mc32_load_tx_ring(dev);
1006        
1007        if(mc32_load_rx_ring(dev)) 
1008        {
1009                mc32_close(dev);
1010                return -ENOBUFS;
1011        }
1012
1013        lp->desired_state = RUNNING; 
1014        
1015        /* And finally, set the ball rolling... */
1016        mc32_start_transceiver(dev);
1017
1018        netif_start_queue(dev);
1019
1020        return 0;
1021}
1022
1023
1024/**
1025 *      mc32_timeout    -       handle a timeout from the network layer
1026 *      @dev: 3c527 that timed out
1027 *
1028 *      Handle a timeout on transmit from the 3c527. This normally means
1029 *      bad things as the hardware handles cable timeouts and mess for
1030 *      us.
1031 *
1032 */
1033
1034static void mc32_timeout(struct net_device *dev)
1035{
1036        printk(KERN_WARNING "%s: transmit timed out?\n", dev->name);
1037        /* Try to restart the adaptor. */
1038        netif_wake_queue(dev);
1039}
1040
1041
1042/**
1043 *      mc32_send_packet        -       queue a frame for transmit
1044 *      @skb: buffer to transmit
1045 *      @dev: 3c527 to send it out of
1046 *
1047 *      Transmit a buffer. This normally means throwing the buffer onto
1048 *      the transmit queue as the queue is quite large. If the queue is
1049 *      full then we set tx_busy and return. Once the interrupt handler
1050 *      gets messages telling it to reclaim transmit queue entries we will
1051 *      clear tx_busy and the kernel will start calling this again.
1052 *
1053 *      We use cli rather than spinlocks. Since I have no access to an SMP
1054 *      MCA machine I don't plan to change it. It is probably the top 
1055 *      performance hit for this driver on SMP however.
1056 */
1057
1058static int mc32_send_packet(struct sk_buff *skb, struct net_device *dev)
1059{
1060        struct mc32_local *lp = (struct mc32_local *)dev->priv;
1061        unsigned long flags;
1062
1063        volatile struct skb_header *p, *np;
1064
1065        netif_stop_queue(dev);
1066
1067        save_flags(flags);
1068        cli();
1069                
1070        if(atomic_read(&lp->tx_count)==0)
1071        {
1072                restore_flags(flags);
1073                return 1;
1074        }
1075
1076        atomic_dec(&lp->tx_count); 
1077
1078        /* P is the last sending/sent buffer as a pointer */
1079        p=lp->tx_ring[lp->tx_ring_head].p; 
1080                
1081        lp->tx_ring_head=next_tx(lp->tx_ring_head); 
1082
1083        /* NP is the buffer we will be loading */
1084        np=lp->tx_ring[lp->tx_ring_head].p; 
1085
1086        if (skb->len < ETH_ZLEN) {
1087                skb = skb_padto(skb, ETH_ZLEN);
1088                if (skb == NULL)
1089                        goto out;
1090        }
1091
1092        /* We will need this to flush the buffer out */
1093        lp->tx_ring[lp->tx_ring_head].skb = skb;
1094           
1095        np->length = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len; 
1096                        
1097        np->data        = isa_virt_to_bus(skb->data);
1098        np->status      = 0;
1099        np->control     = CONTROL_EOP | CONTROL_EOL;     
1100        wmb();
1101                
1102        p->control     &= ~CONTROL_EOL;     /* Clear EOL on p */ 
1103out:    
1104        restore_flags(flags);
1105
1106        netif_wake_queue(dev);
1107        return 0;
1108}
1109
1110
1111/**
1112 *      mc32_update_stats       -       pull off the on board statistics
1113 *      @dev: 3c527 to service
1114 *
1115 * 
1116 *      Query and reset the on-card stats. There's the small possibility
1117 *      of a race here, which would result in an underestimation of
1118 *      actual errors. As such, we'd prefer to keep all our stats
1119 *      collection in software. As a rule, we do. However it can't be
1120 *      used for rx errors and collisions as, by default, the card discards
1121 *      bad rx packets. 
1122 *
1123 *      Setting the SAV BP in the rx filter command supposedly
1124 *      stops this behaviour. However, testing shows that it only seems to
1125 *      enable the collation of on-card rx statistics --- the driver
1126 *      never sees an RX descriptor with an error status set.
1127 *
1128 */
1129
1130static void mc32_update_stats(struct net_device *dev)
1131{
1132        struct mc32_local *lp = (struct mc32_local *)dev->priv;
1133        volatile struct mc32_stats *st = lp->stats; 
1134
1135        u32 rx_errors=0; 
1136      
1137        rx_errors+=lp->net_stats.rx_crc_errors   +=st->rx_crc_errors;         
1138                                                   st->rx_crc_errors=0;
1139        rx_errors+=lp->net_stats.rx_fifo_errors  +=st->rx_overrun_errors;   
1140                                                   st->rx_overrun_errors=0; 
1141        rx_errors+=lp->net_stats.rx_frame_errors +=st->rx_alignment_errors; 
1142                                                   st->rx_alignment_errors=0;
1143        rx_errors+=lp->net_stats.rx_length_errors+=st->rx_tooshort_errors; 
1144                                                   st->rx_tooshort_errors=0;
1145        rx_errors+=lp->net_stats.rx_missed_errors+=st->rx_outofresource_errors;
1146                                                   st->rx_outofresource_errors=0; 
1147        lp->net_stats.rx_errors=rx_errors; 
1148                                                   
1149        /* Number of packets which saw one collision */
1150        lp->net_stats.collisions+=st->dataC[10];
1151        st->dataC[10]=0; 
1152
1153        /* Number of packets which saw 2--15 collisions */ 
1154        lp->net_stats.collisions+=st->dataC[11]; 
1155        st->dataC[11]=0; 
1156}       
1157
1158
1159/**
1160 *      mc32_rx_ring    -       process the receive ring
1161 *      @dev: 3c527 that needs its receive ring processing
1162 *
1163 *
1164 *      We have received one or more indications from the card that a
1165 *      receive has completed. The buffer ring thus contains dirty
1166 *      entries. We walk the ring by iterating over the circular rx_ring
1167 *      array, starting at the next dirty buffer (which happens to be the
1168 *      one we finished up at last time around).
1169 *
1170 *      For each completed packet, we will either copy it and pass it up
1171 *      the stack or, if the packet is near MTU sized, we allocate
1172 *      another buffer and flip the old one up the stack.
1173 * 
1174 *      We must succeed in keeping a buffer on the ring. If necessary we
1175 *      will toss a received packet rather than lose a ring entry. Once
1176 *      the first uncompleted descriptor is found, we move the
1177 *      End-Of-List bit to include the buffers just processed.
1178 *
1179 */
1180
1181static void mc32_rx_ring(struct net_device *dev)
1182{
1183        struct mc32_local *lp=dev->priv;                
1184        volatile struct skb_header *p;
1185        u16 rx_ring_tail = lp->rx_ring_tail;
1186        u16 rx_old_tail = rx_ring_tail; 
1187
1188        int x=0;
1189        
1190        do
1191        { 
1192                p=lp->rx_ring[rx_ring_tail].p; 
1193
1194                if(!(p->status & (1<<7))) { /* Not COMPLETED */ 
1195                        break;
1196                } 
1197                if(p->status & (1<<6)) /* COMPLETED_OK */
1198                {                       
1199
1200                        u16 length=p->length;
1201                        struct sk_buff *skb; 
1202                        struct sk_buff *newskb; 
1203
1204                        /* Try to save time by avoiding a copy on big frames */
1205
1206                        if ((length > RX_COPYBREAK) 
1207                            && ((newskb=dev_alloc_skb(1532)) != NULL)) 
1208                        { 
1209                                skb=lp->rx_ring[rx_ring_tail].skb;
1210                                skb_put(skb, length);
1211                                
1212                                skb_reserve(newskb,18); 
1213                                lp->rx_ring[rx_ring_tail].skb=newskb;  
1214                                p->data=isa_virt_to_bus(newskb->data);  
1215                        } 
1216                        else 
1217                        {
1218                                skb=dev_alloc_skb(length+2);  
1219
1220                                if(skb==NULL) {
1221                                        lp->net_stats.rx_dropped++; 
1222                                        goto dropped; 
1223                                }
1224
1225                                skb_reserve(skb,2);
1226                                memcpy(skb_put(skb, length),
1227                                       lp->rx_ring[rx_ring_tail].skb->data, length);
1228                        }
1229                        
1230                        skb->protocol=eth_type_trans(skb,dev); 
1231                        skb->dev=dev; 
1232                        dev->last_rx = jiffies;
1233                        lp->net_stats.rx_packets++; 
1234                        lp->net_stats.rx_bytes += length; 
1235                        netif_rx(skb);
1236                }
1237
1238        dropped:
1239                p->length = 1532; 
1240                p->status = 0;
1241                
1242                rx_ring_tail=next_rx(rx_ring_tail); 
1243        }
1244        while(x++<48);  
1245
1246        /* If there was actually a frame to be processed, place the EOL bit */ 
1247        /* at the descriptor prior to the one to be filled next */ 
1248
1249        if (rx_ring_tail != rx_old_tail) 
1250        { 
1251                lp->rx_ring[prev_rx(rx_ring_tail)].p->control |=  CONTROL_EOL; 
1252                lp->rx_ring[prev_rx(rx_old_tail)].p->control  &= ~CONTROL_EOL; 
1253
1254                lp->rx_ring_tail=rx_ring_tail; 
1255        }
1256}
1257
1258
1259/**
1260 *      mc32_tx_ring    -       process completed transmits
1261 *      @dev: 3c527 that needs its transmit ring processing
1262 *
1263 *
1264 *      This operates in a similar fashion to mc32_rx_ring. We iterate
1265 *      over the transmit ring. For each descriptor which has been
1266 *      processed by the card, we free its associated buffer and note
1267 *      any errors. This continues until the transmit ring is emptied
1268 *      or we reach a descriptor that hasn't yet been processed by the
1269 *      card.
1270 * 
1271 */
1272
1273static void mc32_tx_ring(struct net_device *dev) 
1274{
1275        struct mc32_local *lp=(struct mc32_local *)dev->priv;
1276        volatile struct skb_header *np;
1277
1278        /* NB: lp->tx_count=TX_RING_LEN-1 so that tx_ring_head cannot "lap" tail here */
1279
1280        while (lp->tx_ring_tail != lp->tx_ring_head)  
1281        {   
1282                u16 t; 
1283
1284                t=next_tx(lp->tx_ring_tail); 
1285                np=lp->tx_ring[t].p; 
1286
1287                if(!(np->status & (1<<7))) 
1288                {
1289                        /* Not COMPLETED */ 
1290                        break; 
1291                } 
1292                lp->net_stats.tx_packets++;
1293                if(!(np->status & (1<<6))) /* Not COMPLETED_OK */
1294                {
1295                        lp->net_stats.tx_errors++;   
1296
1297                        switch(np->status&0x0F)
1298                        {
1299                                case 1:
1300                                        lp->net_stats.tx_aborted_errors++;
1301                                        break; /* Max collisions */ 
1302                                case 2:
1303                                        lp->net_stats.tx_fifo_errors++;
1304                                        break;
1305                                case 3:
1306                                        lp->net_stats.tx_carrier_errors++;
1307                                        break;
1308                                case 4:
1309                                        lp->net_stats.tx_window_errors++;
1310                                        break;  /* CTS Lost */ 
1311                                case 5:
1312                                        lp->net_stats.tx_aborted_errors++;
1313                                        break; /* Transmit timeout */ 
1314                        }
1315                }
1316                /* Packets are sent in order - this is
1317                    basically a FIFO queue of buffers matching
1318                    the card ring */
1319                lp->net_stats.tx_bytes+=lp->tx_ring[t].skb->len;
1320                dev_kfree_skb_irq(lp->tx_ring[t].skb);
1321                lp->tx_ring[t].skb=NULL;
1322                atomic_inc(&lp->tx_count);
1323                netif_wake_queue(dev);
1324
1325                lp->tx_ring_tail=t; 
1326        }
1327
1328} 
1329
1330
1331/**
1332 *      mc32_interrupt          -       handle an interrupt from a 3c527
1333 *      @irq: Interrupt number
1334 *      @dev_id: 3c527 that requires servicing
1335 *      @regs: Registers (unused)
1336 *
1337 *
1338 *      An interrupt is raised whenever the 3c527 writes to the command
1339 *      register. This register contains the message it wishes to send us
1340 *      packed into a single byte field. We keep reading status entries
1341 *      until we have processed all the control items, but simply count
1342 *      transmit and receive reports. When all reports are in we empty the
1343 *      transceiver rings as appropriate. This saves the overhead of
1344 *      multiple command requests.
1345 *
1346 *      Because MCA is level-triggered, we shouldn't miss indications.
1347 *      Therefore, we needn't ask the card to suspend interrupts within
1348 *      this handler. The card receives an implicit acknowledgment of the
1349 *      current interrupt when we read the command register.
1350 *
1351 */
1352
1353static irqreturn_t mc32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1354{
1355        struct net_device *dev = dev_id;
1356        struct mc32_local *lp;
1357        int ioaddr, status, boguscount = 0;
1358        int rx_event = 0;
1359        int tx_event = 0; 
1360        
1361        if (dev == NULL) {
1362                printk(KERN_WARNING "%s: irq %d for unknown device.\n", cardname, irq);
1363                return IRQ_NONE;
1364        }
1365 
1366        ioaddr = dev->base_addr;
1367        lp = (struct mc32_local *)dev->priv;
1368
1369        /* See whats cooking */
1370
1371        while((inb(ioaddr+HOST_STATUS)&HOST_STATUS_CWR) && boguscount++<2000)
1372        {
1373                status=inb(ioaddr+HOST_CMD);
1374
1375#ifdef DEBUG_IRQ                
1376                printk("Status TX%d RX%d EX%d OV%d BC%d\n",
1377                        (status&7), (status>>3)&7, (status>>6)&1,
1378                        (status>>7)&1, boguscount);
1379#endif
1380                        
1381                switch(status&7)
1382                {
1383                        case 0:
1384                                break;
1385                        case 6: /* TX fail */
1386                        case 2: /* TX ok */
1387                                tx_event = 1; 
1388                                break;
1389                        case 3: /* Halt */
1390                        case 4: /* Abort */
1391                                lp->xceiver_state |= TX_HALTED; 
1392                                wake_up(&lp->event);
1393                                break;
1394                        default:
1395                                printk("%s: strange tx ack %d\n", dev->name, status&7);
1396                }
1397                status>>=3;
1398                switch(status&7)
1399                {
1400                        case 0:
1401                                break;
1402                        case 2: /* RX */
1403                                rx_event=1; 
1404                                break;
1405                        case 3: /* Halt */
1406                        case 4: /* Abort */
1407                                lp->xceiver_state |= RX_HALTED;
1408                                wake_up(&lp->event);
1409                                break;
1410                        case 6:
1411                                /* Out of RX buffers stat */
1412                                /* Must restart rx */
1413                                lp->net_stats.rx_dropped++;
1414                                mc32_rx_ring(dev); 
1415                                mc32_start_transceiver(dev); 
1416                                break;
1417                        default:
1418                                printk("%s: strange rx ack %d\n", 
1419                                        dev->name, status&7);                   
1420                }
1421                status>>=3;
1422                if(status&1)
1423                {
1424
1425                        /* 0=no 1=yes 2=replied, get cmd, 3 = wait reply & dump it */
1426                        
1427                        if(lp->exec_pending!=3) {
1428                                lp->exec_pending=2;
1429                                wake_up(&lp->event);
1430                        }
1431                        else 
1432                        {                               
1433                                lp->exec_pending=0;
1434
1435                                /* A new multicast set may have been
1436                                   blocked while the old one was
1437                                   running. If so, do it now. */
1438                                   
1439                                if (lp->mc_reload_wait) 
1440                                        mc32_reset_multicast_list(dev);
1441                                else 
1442                                        wake_up(&lp->event);                           
1443                        }
1444                }
1445                if(status&2)
1446                {
1447                        /*
1448                         *      We get interrupted once per
1449                         *      counter that is about to overflow. 
1450                         */
1451
1452                        mc32_update_stats(dev);                 
1453                }
1454        }
1455
1456
1457        /*
1458         *      Process the transmit and receive rings 
1459         */
1460
1461        if(tx_event) 
1462                mc32_tx_ring(dev);
1463         
1464        if(rx_event) 
1465                mc32_rx_ring(dev);
1466
1467        return IRQ_HANDLED;
1468}
1469
1470
1471/**
1472 *      mc32_close      -       user configuring the 3c527 down
1473 *      @dev: 3c527 card to shut down
1474 *
1475 *      The 3c527 is a bus mastering device. We must be careful how we
1476 *      shut it down. It may also be running shared interrupt so we have
1477 *      to be sure to silence it properly
1478 *
1479 *      We indicate that the card is closing to the rest of the
1480 *      driver.  Otherwise, it is possible that the card may run out
1481 *      of receive buffers and restart the transceiver while we're
1482 *      trying to close it.
1483 * 
1484 *      We abort any receive and transmits going on and then wait until
1485 *      any pending exec commands have completed in other code threads.
1486 *      In theory we can't get here while that is true, in practice I am
1487 *      paranoid
1488 *
1489 *      We turn off the interrupt enable for the board to be sure it can't
1490 *      intefere with other devices.
1491 */
1492
1493static int mc32_close(struct net_device *dev)
1494{
1495        struct mc32_local *lp = (struct mc32_local *)dev->priv;
1496
1497        int ioaddr = dev->base_addr;
1498        u8 regs;
1499        u16 one=1;
1500        
1501        lp->desired_state = HALTED;
1502        netif_stop_queue(dev);
1503
1504        /*
1505         *      Send the indications on command (handy debug check)
1506         */
1507
1508        mc32_command(dev, 4, &one, 2);
1509
1510        /* Shut down the transceiver */
1511
1512        mc32_halt_transceiver(dev); 
1513        
1514        /* Catch any waiting commands */
1515        
1516        while(lp->exec_pending==1)
1517                sleep_on(&lp->event);
1518               
1519        /* Ok the card is now stopping */       
1520        
1521        regs=inb(ioaddr+HOST_CTRL);
1522        regs&=~HOST_CTRL_INTE;
1523        outb(regs, ioaddr+HOST_CTRL);
1524
1525        mc32_flush_rx_ring(dev);
1526        mc32_flush_tx_ring(dev);
1527                
1528        mc32_update_stats(dev); 
1529
1530        return 0;
1531}
1532
1533
1534/**
1535 *      mc32_get_stats          -       hand back stats to network layer
1536 *      @dev: The 3c527 card to handle
1537 *
1538 *      We've collected all the stats we can in software already. Now
1539 *      it's time to update those kept on-card and return the lot. 
1540 * 
1541 */
1542
1543static struct net_device_stats *mc32_get_stats(struct net_device *dev)
1544{
1545        struct mc32_local *lp;
1546        
1547        mc32_update_stats(dev); 
1548
1549        lp = (struct mc32_local *)dev->priv;
1550
1551        return &lp->net_stats;
1552}
1553
1554
1555/**
1556 *      do_mc32_set_multicast_list      -       attempt to update multicasts
1557 *      @dev: 3c527 device to load the list on
1558 *      @retry: indicates this is not the first call. 
1559 *
1560 *
1561 *      Actually set or clear the multicast filter for this adaptor. The
1562 *      locking issues are handled by this routine. We have to track
1563 *      state as it may take multiple calls to get the command sequence
1564 *      completed. We just keep trying to schedule the loads until we
1565 *      manage to process them all.
1566 * 
1567 *      num_addrs == -1 Promiscuous mode, receive all packets
1568 * 
1569 *      num_addrs == 0  Normal mode, clear multicast list
1570 * 
1571 *      num_addrs > 0   Multicast mode, receive normal and MC packets, 
1572 *                      and do best-effort filtering. 
1573 *
1574 *      See mc32_update_stats() regards setting the SAV BP bit. 
1575 *
1576 */
1577
1578static void do_mc32_set_multicast_list(struct net_device *dev, int retry)
1579{
1580        struct mc32_local *lp = (struct mc32_local *)dev->priv;
1581        u16 filt = (1<<2); /* Save Bad Packets, for stats purposes */ 
1582
1583        if (dev->flags&IFF_PROMISC)
1584                /* Enable promiscuous mode */
1585                filt |= 1;
1586        else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > 10)
1587        {
1588                dev->flags|=IFF_PROMISC;
1589                filt |= 1;
1590        }
1591        else if(dev->mc_count)
1592        {
1593                unsigned char block[62];
1594                unsigned char *bp;
1595                struct dev_mc_list *dmc=dev->mc_list;
1596                
1597                int i;
1598               
1599                if(retry==0)
1600                        lp->mc_list_valid = 0;
1601                if(!lp->mc_list_valid)
1602                {
1603                        block[1]=0;
1604                        block[0]=dev->mc_count;
1605                        bp=block+2;
1606                
1607                        for(i=0;i<dev->mc_count;i++)
1608                        {
1609                                memcpy(bp, dmc->dmi_addr, 6);
1610                                bp+=6;
1611                                dmc=dmc->next;
1612                        }
1613                        if(mc32_command_nowait(dev, 2, block, 2+6*dev->mc_count)==-1)
1614                        {
1615                                lp->mc_reload_wait = 1;
1616                                return;
1617                        }
1618                        lp->mc_list_valid=1;
1619                }
1620        }
1621        
1622        if(mc32_command_nowait(dev, 0, &filt, 2)==-1) 
1623        {
1624                lp->mc_reload_wait = 1;
1625        } 
1626        else { 
1627                lp->mc_reload_wait = 0;
1628        }
1629}
1630
1631
1632/**
1633 *      mc32_set_multicast_list -       queue multicast list update
1634 *      @dev: The 3c527 to use
1635 *
1636 *      Commence loading the multicast list. This is called when the kernel
1637 *      changes the lists. It will override any pending list we are trying to
1638 *      load.
1639 */
1640
1641static void mc32_set_multicast_list(struct net_device *dev)
1642{
1643        do_mc32_set_multicast_list(dev,0);
1644}
1645
1646
1647/**
1648 *      mc32_reset_multicast_list       -       reset multicast list
1649 *      @dev: The 3c527 to use
1650 *
1651 *      Attempt the next step in loading the multicast lists. If this attempt
1652 *      fails to complete then it will be scheduled and this function called
1653 *      again later from elsewhere.
1654 */
1655
1656static void mc32_reset_multicast_list(struct net_device *dev)
1657{
1658        do_mc32_set_multicast_list(dev,1);
1659}
1660
1661static void netdev_get_drvinfo(struct net_device *dev,
1662                               struct ethtool_drvinfo *info)
1663{
1664        strcpy(info->driver, DRV_NAME);
1665        strcpy(info->version, DRV_VERSION);
1666        sprintf(info->bus_info, "MCA 0x%lx", dev->base_addr);
1667}
1668
1669static u32 netdev_get_msglevel(struct net_device *dev)
1670{
1671        return mc32_debug;
1672}
1673
1674static void netdev_set_msglevel(struct net_device *dev, u32 level)
1675{
1676        mc32_debug = level;
1677}
1678
1679static struct ethtool_ops netdev_ethtool_ops = {
1680        .get_drvinfo            = netdev_get_drvinfo,
1681        .get_msglevel           = netdev_get_msglevel,
1682        .set_msglevel           = netdev_set_msglevel,
1683};
1684
1685#ifdef MODULE
1686
1687static struct net_device this_device;
1688
1689/**
1690 *      init_module             -       entry point
1691 *
1692 *      Probe and locate a 3c527 card. This really should probe and locate
1693 *      all the 3c527 cards in the machine not just one of them. Yes you can
1694 *      insmod multiple modules for now but it's a hack.
1695 */
1696
1697int init_module(void)
1698{
1699        int result;
1700        
1701        this_device.init = mc32_probe;
1702        if ((result = register_netdev(&this_device)) != 0)
1703                return result;
1704
1705        return 0;
1706}
1707
1708/**
1709 *      cleanup_module  -       free resources for an unload
1710 *
1711 *      Unloading time. We release the MCA bus resources and the interrupt
1712 *      at which point everything is ready to unload. The card must be stopped
1713 *      at this point or we would not have been called. When we unload we
1714 *      leave the card stopped but not totally shut down. When the card is
1715 *      initialized it must be rebooted or the rings reloaded before any
1716 *      transmit operations are allowed to start scribbling into memory.
1717 */
1718
1719void cleanup_module(void)
1720{
1721        int slot;
1722        
1723        unregister_netdev(&this_device);
1724
1725        /*
1726         * If we don't do this, we can't re-insmod it later.
1727         */
1728         
1729        if (this_device.priv)
1730        {
1731                struct mc32_local *lp=this_device.priv;
1732                slot = lp->slot;
1733                mca_mark_as_unused(slot);
1734                mca_set_adapter_name(slot, NULL);
1735                kfree(this_device.priv);
1736        }
1737        free_irq(this_device.irq, &this_device);
1738        release_region(this_device.base_addr, MC32_IO_EXTENT);
1739}
1740
1741#endif /* MODULE */
1742
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.