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